fix multiply bit-width
[ieee754fpu.git] / src / ieee754 / fpmul / mul0.py
1 # IEEE Floating Point Muler (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
3 # 2013-12-12
4
5 from nmigen import Module, Signal, Cat, Elaboratable
6 from nmigen.cli import main, verilog
7
8 from ieee754.fpcommon.fpbase import FPNumBase
9 from ieee754.fpcommon.fpbase import FPState
10 from ieee754.fpcommon.denorm import FPSCData
11
12
13 class FPMulStage0Data:
14
15 def __init__(self, width, id_wid):
16 self.z = FPNumBase(width, False)
17 self.out_do_z = Signal(reset_less=True)
18 self.oz = Signal(width, reset_less=True)
19 mw = (self.z.m_width)*2 - 1 + 3 # sticky/round/guard bits + (2*mant) - 1
20 self.product = Signal(mw, reset_less=True)
21 self.mid = Signal(id_wid, reset_less=True)
22
23 def eq(self, i):
24 return [self.z.eq(i.z), self.out_do_z.eq(i.out_do_z), self.oz.eq(i.oz),
25 self.product.eq(i.product), self.mid.eq(i.mid)]
26
27
28 class FPMulStage0Mod(Elaboratable):
29
30 def __init__(self, width, id_wid):
31 self.width = width
32 self.id_wid = id_wid
33 self.i = self.ispec()
34 self.o = self.ospec()
35
36 def ispec(self):
37 return FPSCData(self.width, self.id_wid, False)
38
39 def ospec(self):
40 return FPMulStage0Data(self.width, self.id_wid)
41
42 def process(self, i):
43 return self.o
44
45 def setup(self, m, i):
46 """ links module to inputs and outputs
47 """
48 m.submodules.mul0 = self
49 m.d.comb += self.i.eq(i)
50
51 def elaborate(self, platform):
52 m = Module()
53 m.submodules.mul0_in_a = self.i.a
54 m.submodules.mul0_in_b = self.i.b
55 m.submodules.mul0_out_z = self.o.z
56
57 # store intermediate tests (and zero-extended mantissas)
58 am0 = Signal(len(self.i.a.m)+1, reset_less=True)
59 bm0 = Signal(len(self.i.b.m)+1, reset_less=True)
60 m.d.comb += [
61 am0.eq(Cat(self.i.a.m, 0)),
62 bm0.eq(Cat(self.i.b.m, 0))
63 ]
64 # same-sign (both negative or both positive) mul mantissas
65 with m.If(~self.i.out_do_z):
66 m.d.comb += [self.o.z.e.eq(self.i.a.e + self.i.b.e + 1),
67 self.o.product.eq(am0 * bm0 * 4),
68 self.o.z.s.eq(self.i.a.s ^ self.i.b.s)
69 ]
70
71 m.d.comb += self.o.oz.eq(self.i.oz)
72 m.d.comb += self.o.out_do_z.eq(self.i.out_do_z)
73 m.d.comb += self.o.mid.eq(self.i.mid)
74 return m
75
76
77 class FPMulStage0(FPState):
78 """ First stage of mul.
79 """
80
81 def __init__(self, width, id_wid):
82 FPState.__init__(self, "multiply_0")
83 self.mod = FPMulStage0Mod(width)
84 self.o = self.mod.ospec()
85
86 def setup(self, m, i):
87 """ links module to inputs and outputs
88 """
89 self.mod.setup(m, i)
90
91 # NOTE: these could be done as combinatorial (merge mul0+mul1)
92 m.d.sync += self.o.eq(self.mod.o)
93
94 def action(self, m):
95 m.next = "multiply_1"