fix up FPNumBase by creating a Record class (not derived from Elaboratable)
[ieee754fpu.git] / src / ieee754 / fpcommon / denorm.py
1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
3 # 2013-12-12
4
5 from nmigen import Module, Signal, Elaboratable
6 from nmigen.cli import main, verilog
7 from math import log
8
9 from ieee754.fpcommon.fpbase import FPNumIn, FPNumOut, FPNumBaseRecord
10 from ieee754.fpcommon.fpbase import FPState, FPNumBase
11
12
13 class FPSCData:
14
15 def __init__(self, width, id_wid, m_extra=True):
16 self.a = FPNumBaseRecord(width, m_extra)
17 self.b = FPNumBaseRecord(width, m_extra)
18 self.z = FPNumBaseRecord(width, False)
19 self.oz = Signal(width, reset_less=True)
20 self.out_do_z = Signal(reset_less=True)
21 self.mid = Signal(id_wid, reset_less=True)
22
23 def __iter__(self):
24 yield from self.a
25 yield from self.b
26 yield from self.z
27 yield self.oz
28 yield self.out_do_z
29 yield self.mid
30
31 def eq(self, i):
32 return [self.z.eq(i.z), self.out_do_z.eq(i.out_do_z), self.oz.eq(i.oz),
33 self.a.eq(i.a), self.b.eq(i.b), self.mid.eq(i.mid)]
34
35
36 class FPAddDeNormMod(FPState, Elaboratable):
37
38 def __init__(self, width, id_wid, m_extra=True):
39 self.width = width
40 self.id_wid = id_wid
41 self.m_extra = m_extra
42 self.i = self.ispec()
43 self.o = self.ospec()
44
45 def ispec(self):
46 return FPSCData(self.width, self.id_wid, self.m_extra)
47
48 def ospec(self):
49 return FPSCData(self.width, self.id_wid, self.m_extra)
50
51 def process(self, i):
52 return self.o
53
54 def setup(self, m, i):
55 """ links module to inputs and outputs
56 """
57 m.submodules.denormalise = self
58 m.d.comb += self.i.eq(i)
59
60 def elaborate(self, platform):
61 m = Module()
62 m.submodules.denorm_in_a = in_a = FPNumBase(self.i.a)
63 m.submodules.denorm_in_b = in_b = FPNumBase(self.i.b)
64 #m.submodules.denorm_out_a = self.o.a
65 #m.submodules.denorm_out_b = self.o.b
66 #m.submodules.denorm_out_z = self.o.z
67
68 with m.If(~self.i.out_do_z):
69 # XXX hmmm, don't like repeating identical code
70 m.d.comb += self.o.a.eq(self.i.a)
71 with m.If(in_a.exp_n127):
72 m.d.comb += self.o.a.e.eq(self.i.a.N126) # limit a exponent
73 with m.Else():
74 m.d.comb += self.o.a.m[-1].eq(1) # set top mantissa bit
75
76 m.d.comb += self.o.b.eq(self.i.b)
77 with m.If(in_b.exp_n127):
78 m.d.comb += self.o.b.e.eq(self.i.b.N126) # limit a exponent
79 with m.Else():
80 m.d.comb += self.o.b.m[-1].eq(1) # set top mantissa bit
81
82 m.d.comb += self.o.mid.eq(self.i.mid)
83 m.d.comb += self.o.z.eq(self.i.z)
84 m.d.comb += self.o.out_do_z.eq(self.i.out_do_z)
85 m.d.comb += self.o.oz.eq(self.i.oz)
86
87 return m
88
89
90 class FPAddDeNorm(FPState):
91
92 def __init__(self, width, id_wid):
93 FPState.__init__(self, "denormalise")
94 self.mod = FPAddDeNormMod(width)
95 self.out_a = FPNumBaseRecord(width)
96 self.out_b = FPNumBaseRecord(width)
97
98 def setup(self, m, i):
99 """ links module to inputs and outputs
100 """
101 self.mod.setup(m, i)
102
103 m.d.sync += self.out_a.eq(self.mod.out_a)
104 m.d.sync += self.out_b.eq(self.mod.out_b)
105
106 def action(self, m):
107 # Denormalised Number checks
108 m.next = "align"
109
110