1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
, Mux
, Array
, Const
, Elaboratable
6 from nmigen
.lib
.coding
import PriorityEncoder
7 from nmigen
.cli
import main
, verilog
10 from ieee754
.fpcommon
.fpbase
import FPNumIn
, FPNumOut
, FPOpIn
, Overflow
, FPBase
, FPNumBase
11 from ieee754
.fpcommon
.fpbase
import MultiShiftRMerge
, Trigger
12 from nmutil
.singlepipe
import (ControlBase
, StageChain
, SimpleHandshake
,
13 PassThroughStage
, PrevControl
)
14 from nmutil
.multipipe
import CombMuxOutPipe
15 from nmutil
.multipipe
import PriorityCombMuxInPipe
17 from ieee754
.fpcommon
.fpbase
import FPState
18 from nmutil
import nmoperator
21 class FPGetOpMod(Elaboratable
):
22 def __init__(self
, width
):
23 self
.in_op
= FPOpIn(width
)
24 self
.in_op
.data_i
= Signal(width
)
25 self
.out_op
= Signal(width
)
26 self
.out_decode
= Signal(reset_less
=True)
28 def elaborate(self
, platform
):
30 m
.d
.comb
+= self
.out_decode
.eq((self
.in_op
.ready_o
) & \
31 (self
.in_op
.valid_i_test
))
32 m
.submodules
.get_op_in
= self
.in_op
33 #m.submodules.get_op_out = self.out_op
34 with m
.If(self
.out_decode
):
36 self
.out_op
.eq(self
.in_op
.v
),
41 class FPGetOp(FPState
):
45 def __init__(self
, in_state
, out_state
, in_op
, width
):
46 FPState
.__init
__(self
, in_state
)
47 self
.out_state
= out_state
48 self
.mod
= FPGetOpMod(width
)
50 self
.out_op
= Signal(width
)
51 self
.out_decode
= Signal(reset_less
=True)
53 def setup(self
, m
, in_op
):
54 """ links module to inputs and outputs
56 setattr(m
.submodules
, self
.state_from
, self
.mod
)
57 m
.d
.comb
+= nmoperator
.eq(self
.mod
.in_op
, in_op
)
58 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.out_decode
)
61 with m
.If(self
.out_decode
):
62 m
.next
= self
.out_state
64 self
.in_op
.ready_o
.eq(0),
65 self
.out_op
.eq(self
.mod
.out_op
)
68 m
.d
.sync
+= self
.in_op
.ready_o
.eq(1)
73 def __init__(self
, width
, id_wid
, m_extra
=True):
74 self
.a
= FPNumBase(width
, m_extra
)
75 self
.b
= FPNumBase(width
, m_extra
)
76 self
.muxid
= Signal(id_wid
, reset_less
=True)
79 return [self
.a
.eq(i
.a
), self
.b
.eq(i
.b
), self
.muxid
.eq(i
.muxid
)]
82 return [self
.a
, self
.b
, self
.muxid
]
87 def __init__(self
, width
, pspec
):
90 self
.id_wid
= pspec
['id_wid']
91 self
.op_wid
= pspec
.get('op_wid', 0)
92 self
.muxid
= Signal(self
.id_wid
, reset_less
=True) # RS multiplex ID
93 self
.op
= Signal(self
.op_wid
, reset_less
=True)
96 ret
= [self
.muxid
.eq(i
.muxid
)]
98 ret
.append(self
.op
.eq(i
.op
))
112 def __init__(self
, width
, pspec
, n_ops
=2):
114 self
.ctx
= FPBaseData(width
, pspec
)
116 for i
in range(n_ops
):
117 name
= chr(ord("a")+i
)
118 operand
= Signal(width
, name
=name
)
119 setattr(self
, name
, operand
)
121 self
.muxid
= self
.ctx
.muxid
# make muxid available here: complicated
126 for op1
, op2
in zip(self
.ops
, i
.ops
):
127 ret
.append(op1
.eq(op2
))
128 ret
.append(self
.ctx
.eq(i
.ctx
))
140 class FPGet2OpMod(PrevControl
):
141 def __init__(self
, width
, id_wid
, op_wid
=None):
142 PrevControl
.__init
__(self
)
145 self
.data_i
= self
.ispec()
147 self
.o
= self
.ospec()
150 return FPADDBaseData(self
.width
, self
.id_wid
, self
.op_wid
)
153 return FPADDBaseData(self
.width
, self
.id_wid
, self
.op_wid
)
155 def process(self
, i
):
158 def elaborate(self
, platform
):
159 m
= PrevControl
.elaborate(self
, platform
)
160 with m
.If(self
.trigger
):
162 self
.o
.eq(self
.data_i
),
167 class FPGet2Op(FPState
):
171 def __init__(self
, in_state
, out_state
, width
, id_wid
, op_wid
=None):
172 FPState
.__init
__(self
, in_state
)
173 self
.out_state
= out_state
174 self
.mod
= FPGet2OpMod(width
, id_wid
, op_wid
)
175 self
.o
= self
.ospec()
176 self
.in_stb
= Signal(reset_less
=True)
177 self
.out_ack
= Signal(reset_less
=True)
178 self
.out_decode
= Signal(reset_less
=True)
181 return self
.mod
.ispec()
184 return self
.mod
.ospec()
186 def trigger_setup(self
, m
, in_stb
, in_ack
):
189 m
.d
.comb
+= self
.mod
.valid_i
.eq(in_stb
)
190 m
.d
.comb
+= in_ack
.eq(self
.mod
.ready_o
)
192 def setup(self
, m
, i
):
193 """ links module to inputs and outputs
195 m
.submodules
.get_ops
= self
.mod
196 m
.d
.comb
+= self
.mod
.i
.eq(i
)
197 m
.d
.comb
+= self
.out_ack
.eq(self
.mod
.ready_o
)
198 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.trigger
)
200 def process(self
, i
):
204 with m
.If(self
.out_decode
):
205 m
.next
= self
.out_state
207 self
.mod
.ready_o
.eq(0),
208 self
.o
.eq(self
.mod
.o
),
211 m
.d
.sync
+= self
.mod
.ready_o
.eq(1)