1 """Computation Unit (aka "ALU Manager").
3 Manages a Pipeline or FSM, ensuring that the start and end time are 100%
4 monitored. At no time may the ALU proceed without this module notifying
5 the Dependency Matrices. At no time is a result production "abandoned".
6 This module blocks (indicates busy) starting from when it first receives
7 an opcode until it receives notification that
8 its result(s) have been successfully stored in the regfile(s)
10 Documented at http://libre-soc.org/3d_gpu/architecture/compunit
13 from soc
.experiment
.alu_fsm
import Shifter
, CompFSMOpSubset
14 from soc
.fu
.alu
.alu_input_record
import CompALUOpSubset
15 from soc
.experiment
.alu_hier
import ALU
, DummyALU
16 from soc
.experiment
.compalu_multi
import MultiCompUnit
17 from soc
.decoder
.power_enums
import MicrOp
18 from nmigen
import Module
19 from nmigen
.cli
import rtlil
21 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
22 # Also, check out the cxxsim nmigen branch, and latest yosys from git
23 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
32 def op_sim_fsm(dut
, a
, b
, direction
):
33 print("op_sim_fsm", a
, b
, direction
)
34 yield dut
.issue_i
.eq(0)
36 yield dut
.src_i
[0].eq(a
)
37 yield dut
.src_i
[1].eq(b
)
38 yield dut
.oper_i
.sdir
.eq(direction
)
39 yield dut
.issue_i
.eq(1)
41 yield dut
.issue_i
.eq(0)
44 yield dut
.rd
.go_i
.eq(0b11)
47 rd_rel_o
= yield dut
.rd
.rel_o
48 print("rd_rel", rd_rel_o
)
51 yield dut
.rd
.go_i
.eq(0)
53 req_rel_o
= yield dut
.wr
.rel_o
54 result
= yield dut
.data_o
55 print("req_rel", req_rel_o
, result
)
57 req_rel_o
= yield dut
.wr
.rel_o
58 result
= yield dut
.data_o
59 print("req_rel", req_rel_o
, result
)
63 yield dut
.wr
.go_i
[0].eq(1)
65 result
= yield dut
.data_o
67 print("result", result
)
68 yield dut
.wr
.go_i
[0].eq(0)
73 def op_sim(dut
, a
, b
, op
, inv_a
=0, imm
=0, imm_ok
=0, zero_a
=0):
74 yield dut
.issue_i
.eq(0)
76 yield dut
.src_i
[0].eq(a
)
77 yield dut
.src_i
[1].eq(b
)
78 yield dut
.oper_i
.insn_type
.eq(op
)
79 yield dut
.oper_i
.invert_in
.eq(inv_a
)
80 yield dut
.oper_i
.imm_data
.data
.eq(imm
)
81 yield dut
.oper_i
.imm_data
.ok
.eq(imm_ok
)
82 yield dut
.oper_i
.zero_a
.eq(zero_a
)
83 yield dut
.issue_i
.eq(1)
85 yield dut
.issue_i
.eq(0)
87 if not imm_ok
or not zero_a
:
88 yield dut
.rd
.go_i
.eq(0b11)
91 rd_rel_o
= yield dut
.rd
.rel_o
92 print("rd_rel", rd_rel_o
)
95 yield dut
.rd
.go_i
.eq(0)
99 if len(dut
.src_i
) == 3:
100 yield dut
.rd
.go_i
.eq(0b100)
103 rd_rel_o
= yield dut
.rd
.rel_o
104 print("rd_rel", rd_rel_o
)
107 yield dut
.rd
.go_i
.eq(0)
111 req_rel_o
= yield dut
.wr
.rel_o
112 result
= yield dut
.data_o
113 print("req_rel", req_rel_o
, result
)
115 req_rel_o
= yield dut
.wr
.rel_o
116 result
= yield dut
.data_o
117 print("req_rel", req_rel_o
, result
)
121 yield dut
.wr
.go_i
[0].eq(1)
123 result
= yield dut
.data_o
125 print("result", result
)
126 yield dut
.wr
.go_i
[0].eq(0)
131 def scoreboard_sim_fsm(dut
):
132 result
= yield from op_sim_fsm(dut
, 13, 2, 1)
133 assert result
== 3, result
135 result
= yield from op_sim_fsm(dut
, 3, 4, 0)
136 assert result
== 48, result
138 result
= yield from op_sim_fsm(dut
, 21, 0, 0)
139 assert result
== 21, result
142 def scoreboard_sim_dummy(dut
):
143 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_NOP
, inv_a
=0,
145 assert result
== 5, result
147 result
= yield from op_sim(dut
, 9, 2, MicrOp
.OP_NOP
, inv_a
=0,
149 assert result
== 9, result
152 def scoreboard_sim(dut
):
153 # zero (no) input operands test
154 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, zero_a
=1,
158 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, inv_a
=0,
162 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
)
165 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, inv_a
=1)
166 assert result
== 65532
168 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, zero_a
=1)
171 # test combinatorial zero-delay operation
172 # In the test ALU, any operation other than ADD, MUL or SHR
173 # is zero-delay, and do a subtraction.
174 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_NOP
)
178 def test_compunit_fsm():
182 dut
= MultiCompUnit(8, alu
, CompFSMOpSubset
)
183 m
.submodules
.cu
= dut
185 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
186 with
open("test_compunit_fsm1.il", "w") as f
:
192 sim
.add_sync_process(wrap(scoreboard_sim_fsm(dut
)))
193 sim_writer
= sim
.write_vcd('test_compunit_fsm1.vcd')
202 dut
= MultiCompUnit(16, alu
, CompALUOpSubset
)
203 m
.submodules
.cu
= dut
205 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
206 with
open("test_compunit1.il", "w") as f
:
212 sim
.add_sync_process(wrap(scoreboard_sim(dut
)))
213 sim_writer
= sim
.write_vcd('test_compunit1.vcd')
218 class CompUnitParallelTest
:
219 def __init__(self
, dut
):
222 # Operation cycle should not take longer than this:
223 self
.MAX_BUSY_WAIT
= 50
225 # Minimum duration in which issue_i will be kept inactive,
226 # during which busy_o must remain low.
227 self
.MIN_BUSY_LOW
= 5
229 # Number of cycles to stall until the assertion of go.
230 # One value, for each port. Can be zero, for no delay.
231 self
.RD_GO_DELAY
= [0, 3]
233 # store common data for the input operation of the processes
236 self
.inv_a
= self
.zero_a
= 0
237 self
.imm
= self
.imm_ok
= 0
238 self
.imm_control
= (0, 0)
239 self
.rdmaskn
= (0, 0)
241 self
.operands
= (0, 0)
243 # Indicates completion of the sub-processes
244 self
.rd_complete
= [False, False]
247 print("Begin parallel test.")
248 yield from self
.operation(5, 2, MicrOp
.OP_ADD
)
250 def operation(self
, a
, b
, op
, inv_a
=0, imm
=0, imm_ok
=0, zero_a
=0,
252 # store data for the operation
253 self
.operands
= (a
, b
)
259 self
.imm_control
= (zero_a
, imm_ok
)
260 self
.rdmaskn
= rdmaskn
262 # Initialize completion flags
263 self
.rd_complete
= [False, False]
265 # trigger operation cycle
266 yield from self
.issue()
268 # check that the sub-processes completed, before the busy_o cycle ended
269 for completion
in self
.rd_complete
:
273 # issue_i starts inactive
274 yield self
.dut
.issue_i
.eq(0)
276 for n
in range(self
.MIN_BUSY_LOW
):
278 # busy_o must remain inactive. It cannot rise on its own.
279 busy_o
= yield self
.dut
.busy_o
282 # activate issue_i to begin the operation cycle
283 yield self
.dut
.issue_i
.eq(1)
285 # at the same time, present the operation
286 yield self
.dut
.oper_i
.insn_type
.eq(self
.op
)
287 yield self
.dut
.oper_i
.invert_in
.eq(self
.inv_a
)
288 yield self
.dut
.oper_i
.imm_data
.data
.eq(self
.imm
)
289 yield self
.dut
.oper_i
.imm_data
.ok
.eq(self
.imm_ok
)
290 yield self
.dut
.oper_i
.zero_a
.eq(self
.zero_a
)
291 rdmaskn
= self
.rdmaskn
[0] |
(self
.rdmaskn
[1] << 1)
292 yield self
.dut
.rdmaskn
.eq(rdmaskn
)
294 # give one cycle for the CompUnit to latch the data
297 # busy_o must keep being low in this cycle, because issue_i was
298 # low on the previous cycle.
299 # It cannot rise on its own.
300 # Also, busy_o and issue_i must never be active at the same time, ever.
301 busy_o
= yield self
.dut
.busy_o
305 yield self
.dut
.issue_i
.eq(0)
307 # deactivate inputs along with issue_i, so we can be sure the data
308 # was latched at the correct cycle
309 # note: rdmaskn must be held, while busy_o is active
310 # TODO: deactivate rdmaskn when the busy_o cycle ends
311 yield self
.dut
.oper_i
.insn_type
.eq(0)
312 yield self
.dut
.oper_i
.invert_in
.eq(0)
313 yield self
.dut
.oper_i
.imm_data
.data
.eq(0)
314 yield self
.dut
.oper_i
.imm_data
.ok
.eq(0)
315 yield self
.dut
.oper_i
.zero_a
.eq(0)
318 # wait for busy_o to lower
319 # timeout after self.MAX_BUSY_WAIT cycles
320 for n
in range(self
.MAX_BUSY_WAIT
):
321 # sample busy_o in the current cycle
322 busy_o
= yield self
.dut
.busy_o
324 # operation cycle ends when busy_o becomes inactive
328 # if busy_o is still active, a timeout has occurred
329 # TODO: Uncomment this, once the test is complete:
333 print("If you are reading this, "
334 "it's because the above test failed, as expected,\n"
335 "with a timeout. It must pass, once the test is complete.")
338 print("If you are reading this, "
339 "it's because the above test unexpectedly passed.")
341 def rd(self
, rd_idx
):
342 # wait for issue_i to rise
344 issue_i
= yield self
.dut
.issue_i
347 # issue_i has not risen yet, so rd must keep low
348 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
352 # we do not want rd to rise on an immediate operand
353 # if it is immediate, exit the process
354 # likewise, if the read mask is active
355 # TODO: don't exit the process, monitor rd instead to ensure it
356 # doesn't rise on its own
357 if self
.rdmaskn
[rd_idx
] or self
.imm_control
[rd_idx
]:
358 self
.rd_complete
[rd_idx
] = True
361 # issue_i has risen. rel must rise on the next cycle
362 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
365 # stall for additional cycles. Check that rel doesn't fall on its own
366 for n
in range(self
.RD_GO_DELAY
[rd_idx
]):
368 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
371 # Before asserting "go", make sure "rel" has risen.
372 # The use of Settle allows "go" to be set combinatorially,
373 # rising on the same cycle as "rel".
375 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
378 # assert go for one cycle, passing along the operand value
379 yield self
.dut
.rd
.go_i
[rd_idx
].eq(1)
380 yield self
.dut
.src_i
[rd_idx
].eq(self
.operands
[rd_idx
])
381 # check that the operand was sent to the alu
382 # TODO: Properly check the alu protocol
384 alu_input
= yield self
.dut
.get_in(rd_idx
)
385 assert alu_input
== self
.operands
[rd_idx
]
388 # rel must keep high, since go was inactive in the last cycle
389 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
392 # finish the go one-clock pulse
393 yield self
.dut
.rd
.go_i
[rd_idx
].eq(0)
394 yield self
.dut
.src_i
[rd_idx
].eq(0)
397 # rel must have gone low in response to go being high
398 # on the previous cycle
399 rel
= yield self
.dut
.rd
.rel_o
[rd_idx
]
402 self
.rd_complete
[rd_idx
] = True
404 # TODO: check that rel doesn't rise again until the end of the
407 def wr(self
, wr_idx
):
408 # monitor self.dut.wr.req[rd_idx] and sets dut.wr.go[idx] for one cycle
410 # TODO: also when dut.wr.go is set, check the output against the
411 # self.expected_o and assert. use dut.get_out(wr_idx) to do so.
413 def run_simulation(self
, vcd_name
):
415 m
.submodules
.cu
= self
.dut
419 sim
.add_sync_process(wrap(self
.driver()))
420 sim
.add_sync_process(wrap(self
.rd(0)))
421 sim
.add_sync_process(wrap(self
.rd(1)))
422 sim
.add_sync_process(wrap(self
.wr(0)))
423 sim_writer
= sim
.write_vcd(vcd_name
)
428 def test_compunit_regspec2_fsm():
430 inspec
= [('INT', 'a', '0:15'),
431 ('INT', 'b', '0:15'),
433 outspec
= [('INT', 'o', '0:15'),
436 regspec
= (inspec
, outspec
)
440 dut
= MultiCompUnit(regspec
, alu
, CompFSMOpSubset
)
441 m
.submodules
.cu
= dut
446 sim
.add_sync_process(wrap(scoreboard_sim_fsm(dut
)))
447 sim_writer
= sim
.write_vcd('test_compunit_regspec2_fsm.vcd')
452 def test_compunit_regspec3():
454 inspec
= [('INT', 'a', '0:15'),
455 ('INT', 'b', '0:15'),
456 ('INT', 'c', '0:15')]
457 outspec
= [('INT', 'o', '0:15'),
460 regspec
= (inspec
, outspec
)
464 dut
= MultiCompUnit(regspec
, alu
, CompALUOpSubset
)
465 m
.submodules
.cu
= dut
470 sim
.add_sync_process(wrap(scoreboard_sim_dummy(dut
)))
471 sim_writer
= sim
.write_vcd('test_compunit_regspec3.vcd')
476 def test_compunit_regspec1():
478 inspec
= [('INT', 'a', '0:15'),
479 ('INT', 'b', '0:15')]
480 outspec
= [('INT', 'o', '0:15'),
483 regspec
= (inspec
, outspec
)
487 dut
= MultiCompUnit(regspec
, alu
, CompALUOpSubset
)
488 m
.submodules
.cu
= dut
490 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
491 with
open("test_compunit_regspec1.il", "w") as f
:
497 sim
.add_sync_process(wrap(scoreboard_sim(dut
)))
498 sim_writer
= sim
.write_vcd('test_compunit_regspec1.vcd')
502 test
= CompUnitParallelTest(dut
)
503 test
.run_simulation("test_compunit_parallel.vcd")
506 if __name__
== '__main__':
509 test_compunit_regspec1()
510 test_compunit_regspec3()