big (single-purpose) update: move width arg into pspec
[ieee754fpu.git] / src / ieee754 / fpcommon / pack.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 ieee754.fpcommon.fpbase import FPNumOut, FPNumBaseRecord, FPNumBase
9 from ieee754.fpcommon.fpbase import FPState
10 from .roundz import FPRoundData
11 from nmutil.singlepipe import Object
12 from ieee754.fpcommon.getop import FPPipeContext
13
14
15 class FPPackData:
16
17 def __init__(self, pspec):
18 width = pspec['width']
19 self.z = Signal(width, reset_less=True) # result
20 self.ctx = FPPipeContext(pspec)
21
22 # this is complicated: it's a workaround, due to the
23 # array-indexing not working properly in nmigen.
24 # self.ports() is used to access the ArrayProxy objects by name,
25 # however it doesn't work recursively. the workaround:
26 # drop the sub-objects into *this* scope and they can be
27 # accessed / set. it's horrible.
28 self.muxid = self.ctx.muxid
29 self.op = self.ctx.op
30
31 def eq(self, i):
32 return [self.z.eq(i.z), self.ctx.eq(i.ctx)]
33
34 def __iter__(self):
35 yield self.z
36 yield from self.ctx
37
38 def ports(self):
39 return list(self)
40
41
42 class FPPackMod(Elaboratable):
43
44 def __init__(self, pspec):
45 self.pspec = pspec
46 self.i = self.ispec()
47 self.o = self.ospec()
48
49 def ispec(self):
50 return FPRoundData(self.pspec)
51
52 def ospec(self):
53 return FPPackData(self.pspec)
54
55 def process(self, i):
56 return self.o
57
58 def setup(self, m, in_z):
59 """ links module to inputs and outputs
60 """
61 m.submodules.pack = self
62 m.d.comb += self.i.eq(in_z)
63
64 def elaborate(self, platform):
65 m = Module()
66 z = FPNumBaseRecord(self.pspec['width'], False)
67 m.submodules.pack_in_z = in_z = FPNumBase(self.i.z)
68 #m.submodules.pack_out_z = out_z = FPNumOut(z)
69 m.d.comb += self.o.ctx.eq(self.i.ctx)
70 with m.If(~self.i.out_do_z):
71 with m.If(in_z.is_overflowed):
72 m.d.comb += z.inf(self.i.z.s)
73 with m.Else():
74 m.d.comb += z.create(self.i.z.s, self.i.z.e, self.i.z.m)
75 with m.Else():
76 m.d.comb += z.v.eq(self.i.oz)
77 m.d.comb += self.o.z.eq(z.v)
78 return m
79
80
81 class FPPack(FPState):
82
83 def __init__(self, width, id_wid):
84 FPState.__init__(self, "pack")
85 self.mod = FPPackMod(width)
86 self.out_z = self.ospec()
87
88 def ispec(self):
89 return self.mod.ispec()
90
91 def ospec(self):
92 return self.mod.ospec()
93
94 def setup(self, m, in_z):
95 """ links module to inputs and outputs
96 """
97 self.mod.setup(m, in_z)
98
99 m.d.sync += self.out_z.v.eq(self.mod.out_z.v)
100 m.d.sync += self.out_z.ctx.eq(self.mod.o.ctx)
101
102 def action(self, m):
103 m.next = "pack_put_z"