reorganise imports
[ieee754fpu.git] / src / ieee754 / fpcommon / roundz.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
8 from fpbase import FPNumBase
9 from fpbase import FPState
10 from .postnormalise import FPNorm1Data
11
12
13 class FPRoundData:
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 self.mid = Signal(id_wid, reset_less=True)
20
21 def eq(self, i):
22 return [self.z.eq(i.z), self.out_do_z.eq(i.out_do_z), self.oz.eq(i.oz),
23 self.mid.eq(i.mid)]
24
25
26 class FPRoundMod(Elaboratable):
27
28 def __init__(self, width, id_wid):
29 self.width = width
30 self.id_wid = id_wid
31 self.i = self.ispec()
32 self.out_z = self.ospec()
33
34 def ispec(self):
35 return FPNorm1Data(self.width, self.id_wid)
36
37 def ospec(self):
38 return FPRoundData(self.width, self.id_wid)
39
40 def process(self, i):
41 return self.out_z
42
43 def setup(self, m, i):
44 m.submodules.roundz = self
45 m.d.comb += self.i.eq(i)
46
47 def elaborate(self, platform):
48 m = Module()
49 m.d.comb += self.out_z.eq(self.i) # copies mid, z, out_do_z
50 with m.If(~self.i.out_do_z):
51 with m.If(self.i.roundz):
52 m.d.comb += self.out_z.z.m.eq(self.i.z.m + 1) # mantissa up
53 with m.If(self.i.z.m == self.i.z.m1s): # all 1s
54 m.d.comb += self.out_z.z.e.eq(self.i.z.e + 1) # exponent up
55
56 return m
57
58
59 class FPRound(FPState):
60
61 def __init__(self, width, id_wid):
62 FPState.__init__(self, "round")
63 self.mod = FPRoundMod(width)
64 self.out_z = self.ospec()
65
66 def ispec(self):
67 return self.mod.ispec()
68
69 def ospec(self):
70 return self.mod.ospec()
71
72 def setup(self, m, i):
73 """ links module to inputs and outputs
74 """
75 self.mod.setup(m, i)
76
77 self.idsync(m)
78 m.d.sync += self.out_z.eq(self.mod.out_z)
79 m.d.sync += self.out_z.mid.eq(self.mod.o.mid)
80
81 def action(self, m):
82 m.next = "corrections"