split out rounding to separate module
[ieee754fpu.git] / src / add / 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, Cat, Mux, Array, Const
6 from nmigen.lib.coding import PriorityEncoder
7 from nmigen.cli import main, verilog
8 from math import log
9
10 from fpbase import FPNumIn, FPNumOut, FPOp, Overflow, FPBase, FPNumBase
11 from fpbase import MultiShiftRMerge, Trigger
12 from singlepipe import (ControlBase, StageChain, UnbufferedPipeline,
13 PassThroughStage)
14 from multipipe import CombMuxOutPipe
15 from multipipe import PriorityCombMuxInPipe
16
17 from fpbase import FPState, FPID
18 from fpcommon.postnormalise import FPNorm1Data
19
20
21 class FPRoundData:
22
23 def __init__(self, width, id_wid):
24 self.z = FPNumBase(width, False)
25 self.out_do_z = Signal(reset_less=True)
26 self.oz = Signal(width, reset_less=True)
27 self.mid = Signal(id_wid, reset_less=True)
28
29 def eq(self, i):
30 return [self.z.eq(i.z), self.out_do_z.eq(i.out_do_z), self.oz.eq(i.oz),
31 self.mid.eq(i.mid)]
32
33
34 class FPRoundMod:
35
36 def __init__(self, width, id_wid):
37 self.width = width
38 self.id_wid = id_wid
39 self.i = self.ispec()
40 self.out_z = self.ospec()
41
42 def ispec(self):
43 return FPNorm1Data(self.width, self.id_wid)
44
45 def ospec(self):
46 return FPRoundData(self.width, self.id_wid)
47
48 def process(self, i):
49 return self.out_z
50
51 def setup(self, m, i):
52 m.submodules.roundz = self
53 m.d.comb += self.i.eq(i)
54
55 def elaborate(self, platform):
56 m = Module()
57 m.d.comb += self.out_z.eq(self.i) # copies mid, z, out_do_z
58 with m.If(~self.i.out_do_z):
59 with m.If(self.i.roundz):
60 m.d.comb += self.out_z.z.m.eq(self.i.z.m + 1) # mantissa up
61 with m.If(self.i.z.m == self.i.z.m1s): # all 1s
62 m.d.comb += self.out_z.z.e.eq(self.i.z.e + 1) # exponent up
63
64 return m
65
66
67 class FPRound(FPState):
68
69 def __init__(self, width, id_wid):
70 FPState.__init__(self, "round")
71 self.mod = FPRoundMod(width)
72 self.out_z = self.ospec()
73
74 def ispec(self):
75 return self.mod.ispec()
76
77 def ospec(self):
78 return self.mod.ospec()
79
80 def setup(self, m, i):
81 """ links module to inputs and outputs
82 """
83 self.mod.setup(m, i)
84
85 self.idsync(m)
86 m.d.sync += self.out_z.eq(self.mod.out_z)
87 m.d.sync += self.out_z.mid.eq(self.mod.o.mid)
88
89 def action(self, m):
90 m.next = "corrections"