1 """*Experimental* ALU: based on nmigen alu_hier.py, includes branch-compare ALU
3 This ALU is *deliberately* designed to add in (unnecessary) delays into
4 different operations so as to be able to test the 6600-style matrices
5 and the CompUnits. Countdown timers wait for (defined) periods before
6 indicating that the output is valid
8 A "real" integer ALU would place the answers onto the output bus after
12 from nmigen
import Elaboratable
, Signal
, Module
, Const
, Mux
, Array
13 from nmigen
.hdl
.rec
import Record
, Layout
14 from nmigen
.cli
import main
15 from nmigen
.cli
import verilog
, rtlil
16 from nmigen
.compat
.sim
import run_simulation
18 from soc
.decoder
.power_enums
import InternalOp
, Function
, CryIn
20 from soc
.fu
.alu
.alu_input_record
import CompALUOpSubset
27 class Adder(Elaboratable
):
28 def __init__(self
, width
):
29 self
.invert_a
= Signal()
30 self
.a
= Signal(width
)
31 self
.b
= Signal(width
)
32 self
.o
= Signal(width
)
34 def elaborate(self
, platform
):
36 with m
.If(self
.invert_a
):
37 m
.d
.comb
+= self
.o
.eq((~self
.a
) + self
.b
)
39 m
.d
.comb
+= self
.o
.eq(self
.a
+ self
.b
)
43 class Subtractor(Elaboratable
):
44 def __init__(self
, width
):
45 self
.a
= Signal(width
)
46 self
.b
= Signal(width
)
47 self
.o
= Signal(width
)
49 def elaborate(self
, platform
):
51 m
.d
.comb
+= self
.o
.eq(self
.a
- self
.b
)
55 class Multiplier(Elaboratable
):
56 def __init__(self
, width
):
57 self
.a
= Signal(width
)
58 self
.b
= Signal(width
)
59 self
.o
= Signal(width
)
61 def elaborate(self
, platform
):
63 m
.d
.comb
+= self
.o
.eq(self
.a
* self
.b
)
67 class Shifter(Elaboratable
):
68 def __init__(self
, width
):
70 self
.a
= Signal(width
)
71 self
.b
= Signal(width
)
72 self
.o
= Signal(width
)
74 def elaborate(self
, platform
):
76 btrunc
= Signal(self
.width
)
77 m
.d
.comb
+= btrunc
.eq(self
.b
& Const((1<<self
.width
)-1))
78 m
.d
.comb
+= self
.o
.eq(self
.a
>> btrunc
)
82 class ALU(Elaboratable
):
83 def __init__(self
, width
):
84 self
.p_valid_i
= Signal()
85 self
.p_ready_o
= Signal()
86 self
.n_ready_i
= Signal()
87 self
.n_valid_o
= Signal()
88 self
.counter
= Signal(4)
89 self
.op
= CompALUOpSubset()
91 i
.append(Signal(width
, name
="i1"))
92 i
.append(Signal(width
, name
="i2"))
94 self
.a
, self
.b
= i
[0], i
[1]
95 self
.out
= Array([Signal(width
)])
99 def elaborate(self
, platform
):
101 add
= Adder(self
.width
)
102 mul
= Multiplier(self
.width
)
103 shf
= Shifter(self
.width
)
105 m
.submodules
.add
= add
106 m
.submodules
.mul
= mul
107 m
.submodules
.shf
= shf
109 # really should not activate absolutely all ALU inputs like this
110 for mod
in [add
, mul
, shf
]:
116 # pass invert (and carry later)
117 m
.d
.comb
+= add
.invert_a
.eq(self
.op
.invert_a
)
119 go_now
= Signal(reset_less
=True) # testing no-delay ALU
121 with m
.If(self
.p_valid_i
):
122 # input is valid. next check, if we already said "ready" or not
123 with m
.If(~self
.p_ready_o
):
124 # we didn't say "ready" yet, so say so and initialise
125 m
.d
.sync
+= self
.p_ready_o
.eq(1)
127 # as this is a "fake" pipeline, just grab the output right now
128 with m
.If(self
.op
.insn_type
== InternalOp
.OP_ADD
):
129 m
.d
.sync
+= self
.o
.eq(add
.o
)
130 with m
.Elif(self
.op
.insn_type
== InternalOp
.OP_MUL_L64
):
131 m
.d
.sync
+= self
.o
.eq(mul
.o
)
132 with m
.Elif(self
.op
.insn_type
== InternalOp
.OP_SHR
):
133 m
.d
.sync
+= self
.o
.eq(shf
.o
)
136 # NOTE: all of these are fake, just something to test
138 # MUL, to take 5 instructions
139 with m
.If(self
.op
.insn_type
== InternalOp
.OP_MUL_L64
):
140 m
.d
.sync
+= self
.counter
.eq(5)
142 with m
.Elif(self
.op
.insn_type
== InternalOp
.OP_SHR
):
143 m
.d
.sync
+= self
.counter
.eq(7)
144 # ADD/SUB to take 2, straight away
145 with m
.If(self
.op
.insn_type
== InternalOp
.OP_ADD
):
146 m
.d
.sync
+= self
.counter
.eq(3)
147 # others to take 1, straight away
149 m
.d
.comb
+= go_now
.eq(1)
150 m
.d
.sync
+= self
.counter
.eq(1)
153 # input says no longer valid, so drop ready as well.
154 # a "proper" ALU would have had to sync in the opcode and a/b ops
155 m
.d
.sync
+= self
.p_ready_o
.eq(0)
157 # ok so the counter's running: when it gets to 1, fire the output
158 with m
.If((self
.counter
== 1) | go_now
):
159 # set the output as valid if the recipient is ready for it
160 m
.d
.sync
+= self
.n_valid_o
.eq(1)
161 with m
.If(self
.n_ready_i
& self
.n_valid_o
):
162 m
.d
.sync
+= self
.n_valid_o
.eq(0)
163 # recipient said it was ready: reset back to known-good.
164 m
.d
.sync
+= self
.counter
.eq(0) # reset the counter
165 m
.d
.sync
+= self
.o
.eq(0) # clear the output for tidiness sake
167 # countdown to 1 (transition from 1 to 0 only on acknowledgement)
168 with m
.If(self
.counter
> 1):
169 m
.d
.sync
+= self
.counter
.eq(self
.counter
- 1)
174 yield from self
.op
.ports()
183 class BranchOp(Elaboratable
):
184 def __init__(self
, width
, op
):
185 self
.a
= Signal(width
)
186 self
.b
= Signal(width
)
187 self
.o
= Signal(width
)
190 def elaborate(self
, platform
):
192 m
.d
.comb
+= self
.o
.eq(Mux(self
.op(self
.a
, self
.b
), 1, 0))
196 class BranchALU(Elaboratable
):
197 def __init__(self
, width
):
198 self
.p_valid_i
= Signal()
199 self
.p_ready_o
= Signal()
200 self
.n_ready_i
= Signal()
201 self
.n_valid_o
= Signal()
202 self
.counter
= Signal(4)
205 i
.append(Signal(width
, name
="i1"))
206 i
.append(Signal(width
, name
="i2"))
208 self
.a
, self
.b
= i
[0], i
[1]
209 self
.out
= Array([Signal(width
)])
213 def elaborate(self
, platform
):
215 bgt
= BranchOp(self
.width
, operator
.gt
)
216 blt
= BranchOp(self
.width
, operator
.lt
)
217 beq
= BranchOp(self
.width
, operator
.eq
)
218 bne
= BranchOp(self
.width
, operator
.ne
)
220 m
.submodules
.bgt
= bgt
221 m
.submodules
.blt
= blt
222 m
.submodules
.beq
= beq
223 m
.submodules
.bne
= bne
224 for mod
in [bgt
, blt
, beq
, bne
]:
230 go_now
= Signal(reset_less
=True) # testing no-delay ALU
231 with m
.If(self
.p_valid_i
):
232 # input is valid. next check, if we already said "ready" or not
233 with m
.If(~self
.p_ready_o
):
234 # we didn't say "ready" yet, so say so and initialise
235 m
.d
.sync
+= self
.p_ready_o
.eq(1)
237 # as this is a "fake" pipeline, just grab the output right now
238 with m
.Switch(self
.op
):
239 for i
, mod
in enumerate([bgt
, blt
, beq
, bne
]):
241 m
.d
.sync
+= self
.o
.eq(mod
.o
)
242 m
.d
.sync
+= self
.counter
.eq(5) # branch to take 5 cycles (fake)
243 #m.d.comb += go_now.eq(1)
245 # input says no longer valid, so drop ready as well.
246 # a "proper" ALU would have had to sync in the opcode and a/b ops
247 m
.d
.sync
+= self
.p_ready_o
.eq(0)
249 # ok so the counter's running: when it gets to 1, fire the output
250 with m
.If((self
.counter
== 1) | go_now
):
251 # set the output as valid if the recipient is ready for it
252 m
.d
.sync
+= self
.n_valid_o
.eq(1)
253 with m
.If(self
.n_ready_i
& self
.n_valid_o
):
254 m
.d
.sync
+= self
.n_valid_o
.eq(0)
255 # recipient said it was ready: reset back to known-good.
256 m
.d
.sync
+= self
.counter
.eq(0) # reset the counter
257 m
.d
.sync
+= self
.o
.eq(0) # clear the output for tidiness sake
259 # countdown to 1 (transition from 1 to 0 only on acknowledgement)
260 with m
.If(self
.counter
> 1):
261 m
.d
.sync
+= self
.counter
.eq(self
.counter
- 1)
274 def run_op(dut
, a
, b
, op
, inv_a
=0):
277 yield dut
.op
.insn_type
.eq(op
)
278 yield dut
.op
.invert_a
.eq(inv_a
)
279 yield dut
.n_ready_i
.eq(0)
280 yield dut
.p_valid_i
.eq(1)
284 n_valid_o
= yield dut
.n_valid_o
290 yield dut
.p_valid_i
.eq(0)
291 yield dut
.n_ready_i
.eq(0)
298 result
= yield from run_op(dut
, 5, 3, InternalOp
.OP_ADD
)
299 print ("alu_sim add", result
)
302 result
= yield from run_op(dut
, 2, 3, InternalOp
.OP_MUL_L64
)
303 print ("alu_sim mul", result
)
306 result
= yield from run_op(dut
, 5, 3, InternalOp
.OP_ADD
, inv_a
=1)
307 print ("alu_sim add-inv", result
)
308 assert (result
== 65533)
313 run_simulation(alu
, alu_sim(alu
), vcd_name
='test_alusim.vcd')
315 vl
= rtlil
.convert(alu
, ports
=alu
.ports())
316 with
open("test_alu.il", "w") as f
:
320 if __name__
== "__main__":
323 alu
= BranchALU(width
=16)
324 vl
= rtlil
.convert(alu
, ports
=alu
.ports())
325 with
open("test_branch_alu.il", "w") as f
: