start adding use of DivPipeInputData and DivPipeInterstageData
[ieee754fpu.git] / src / ieee754 / fpdiv / div0.py
1 """IEEE754 Floating Point Divider
2
3 Relevant bugreport: http://bugs.libre-riscv.org/show_bug.cgi?id=99
4 """
5
6 from nmigen import Module, Signal, Cat, Elaboratable
7 from nmigen.cli import main, verilog
8
9 from ieee754.fpcommon.fpbase import (FPNumBaseRecord, Overflow)
10 from ieee754.fpcommon.fpbase import FPState
11 from ieee754.fpcommon.denorm import FPSCData
12 from ieee754.fpcommon.getop import FPPipeContext
13 from ieee754.div_rem_sqrt_rsqrt.div_pipe import DivPipeInputData
14
15
16 # TODO: delete (replace by DivPipeCoreInputData)
17 class FPDivStage0Data:
18
19 def __init__(self, pspec):
20 self.z = FPNumBaseRecord(pspec.width, False)
21 self.out_do_z = Signal(reset_less=True)
22 self.oz = Signal(pspec.width, reset_less=True)
23
24 self.ctx = FPPipeContext(pspec.width, pspec) # context: muxid, operator etc.
25 self.muxid = self.ctx.muxid # annoying. complicated.
26
27 # TODO: here is where Q and R would be put, and passed
28 # down to Stage1 processing.
29
30 mw = (self.z.m_width)*2 - 1 + 3 # sticky/round/guard bits + (2*mant) - 1
31 self.product = Signal(mw, reset_less=True)
32
33 def eq(self, i):
34 return [self.z.eq(i.z), self.out_do_z.eq(i.out_do_z), self.oz.eq(i.oz),
35 self.product.eq(i.product), self.ctx.eq(i.ctx)]
36
37
38 class FPDivStage0Mod(Elaboratable):
39
40 def __init__(self, pspec):
41 self.pspec = pspec
42 self.i = self.ispec()
43 self.o = self.ospec()
44
45 def ispec(self):
46 return FPSCData(self.pspec, False)
47
48 def ospec(self):
49 return DivPipeInputData(self.pspec)
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.div0 = self
58 m.d.comb += self.i.eq(i)
59
60 def elaborate(self, platform):
61 m = Module()
62
63 # XXX TODO, actual DIV code here. this class would be
64 # "step one" which takes the pre-normalised data (see ispec) and
65 # *begins* the processing phase (enters the massive DIV
66 # pipeline chain) - see ospec.
67
68 # INPUT SPEC: FPSCData
69 # OUTPUT SPEC: DivPipeCoreInputData
70
71 # NOTE: this stage does *NOT* do *ACTUAL* DIV processing,
72 # it is PURELY the *ENTRY* point into the chain, performing
73 # "preparation" work.
74
75 with m.If(~self.i.out_do_z):
76 # do conversion here, of both self.i.a and self.i.b,
77 # into DivPipeCoreInputData dividend and divisor.
78
79 m.d.comb += [self.o.z.e.eq(self.i.a.e - self.i.b.e + 1),
80 self.o.z.s.eq(self.i.a.s ^ self.i.b.s)
81 self.o.dividend.eq(self.i.a.m), # TODO: check
82 self.o.divisor_radicand.eq(self.i.b.m), # TODO: check
83 self.o.operation.eq(Const(0)) # TODO (set from ctx.op)
84 ]
85
86 # these are required and must not be touched
87 m.d.comb += self.o.oz.eq(self.i.oz)
88 m.d.comb += self.o.out_do_z.eq(self.i.out_do_z)
89 m.d.comb += self.o.ctx.eq(self.i.ctx)
90
91 return m
92
93
94 class FPDivStage0(FPState):
95 """ First stage of div.
96 """
97
98 def __init__(self, pspec):
99 FPState.__init__(self, "divider_0")
100 self.mod = FPDivStage0Mod(pspec)
101 self.o = self.mod.ospec()
102
103 def setup(self, m, i):
104 """ links module to inputs and outputs
105 """
106 self.mod.setup(m, i)
107
108 # NOTE: these could be done as combinatorial (merge div0+div1)
109 m.d.sync += self.o.eq(self.mod.o)
110
111 def action(self, m):
112 m.next = "divider_1"