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
15 from nmigen
.sim
.cxxsim
import Simulator
, Settle
17 from nmigen
.back
.pysim
import Simulator
, Settle
19 from nmigen
.cli
import rtlil
20 from nmigen
import Module
22 from soc
.decoder
.power_enums
import MicrOp
24 from soc
.experiment
.compalu_multi
import MultiCompUnit
25 from soc
.experiment
.alu_hier
import ALU
, DummyALU
26 from soc
.fu
.alu
.alu_input_record
import CompALUOpSubset
27 from soc
.experiment
.alu_fsm
import Shifter
, CompFSMOpSubset
35 def op_sim_fsm(dut
, a
, b
, direction
):
36 print ("op_sim_fsm", a
, b
, direction
)
37 yield dut
.issue_i
.eq(0)
39 yield dut
.src_i
[0].eq(a
)
40 yield dut
.src_i
[1].eq(b
)
41 yield dut
.oper_i
.sdir
.eq(direction
)
42 yield dut
.issue_i
.eq(1)
44 yield dut
.issue_i
.eq(0)
47 yield dut
.rd
.go
.eq(0b11)
50 rd_rel_o
= yield dut
.rd
.rel
51 print ("rd_rel", rd_rel_o
)
56 req_rel_o
= yield dut
.wr
.rel
57 result
= yield dut
.data_o
58 print ("req_rel", req_rel_o
, result
)
60 req_rel_o
= yield dut
.wr
.rel
61 result
= yield dut
.data_o
62 print ("req_rel", req_rel_o
, result
)
66 yield dut
.wr
.go
[0].eq(1)
68 result
= yield dut
.data_o
70 print ("result", result
)
71 yield dut
.wr
.go
[0].eq(0)
76 def op_sim(dut
, a
, b
, op
, inv_a
=0, imm
=0, imm_ok
=0, zero_a
=0):
77 yield dut
.issue_i
.eq(0)
79 yield dut
.src_i
[0].eq(a
)
80 yield dut
.src_i
[1].eq(b
)
81 yield dut
.oper_i
.insn_type
.eq(op
)
82 yield dut
.oper_i
.invert_a
.eq(inv_a
)
83 yield dut
.oper_i
.imm_data
.imm
.eq(imm
)
84 yield dut
.oper_i
.imm_data
.imm_ok
.eq(imm_ok
)
85 yield dut
.oper_i
.zero_a
.eq(zero_a
)
86 yield dut
.issue_i
.eq(1)
88 yield dut
.issue_i
.eq(0)
90 if not imm_ok
or not zero_a
:
91 yield dut
.rd
.go
.eq(0b11)
94 rd_rel_o
= yield dut
.rd
.rel
95 print ("rd_rel", rd_rel_o
)
99 if len(dut
.src_i
) == 3:
100 yield dut
.rd
.go
.eq(0b100)
103 rd_rel_o
= yield dut
.rd
.rel
104 print ("rd_rel", rd_rel_o
)
107 yield dut
.rd
.go
.eq(0)
109 req_rel_o
= yield dut
.wr
.rel
110 result
= yield dut
.data_o
111 print ("req_rel", req_rel_o
, result
)
113 req_rel_o
= yield dut
.wr
.rel
114 result
= yield dut
.data_o
115 print ("req_rel", req_rel_o
, result
)
119 yield dut
.wr
.go
[0].eq(1)
121 result
= yield dut
.data_o
123 print ("result", result
)
124 yield dut
.wr
.go
[0].eq(0)
129 def scoreboard_sim_fsm(dut
):
130 result
= yield from op_sim_fsm(dut
, 13, 2, 1)
131 assert result
== 3, result
133 result
= yield from op_sim_fsm(dut
, 3, 4, 0)
134 assert result
== 48, result
136 result
= yield from op_sim_fsm(dut
, 21, 0, 0)
137 assert result
== 21, result
140 def scoreboard_sim_dummy(dut
):
141 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_NOP
, inv_a
=0,
143 assert result
== 5, result
145 result
= yield from op_sim(dut
, 9, 2, MicrOp
.OP_NOP
, inv_a
=0,
147 assert result
== 9, result
151 def scoreboard_sim(dut
):
152 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, inv_a
=0,
156 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
)
159 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, inv_a
=1)
160 assert result
== 65532
162 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, zero_a
=1,
166 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_ADD
, zero_a
=1)
169 # test combinatorial zero-delay operation
170 # In the test ALU, any operation other than ADD, MUL or SHR
171 # is zero-delay, and do a subtraction.
172 result
= yield from op_sim(dut
, 5, 2, MicrOp
.OP_NOP
)
176 def test_compunit_fsm():
180 dut
= MultiCompUnit(8, alu
, CompFSMOpSubset
)
181 m
.submodules
.cu
= dut
183 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
184 with
open("test_compunit_fsm1.il", "w") as f
:
190 sim
.add_sync_process(wrap(scoreboard_sim_fsm(dut
)))
191 sim_writer
= sim
.write_vcd('test_compunit_fsm1.vcd')
200 dut
= MultiCompUnit(16, alu
, CompALUOpSubset
)
201 m
.submodules
.cu
= dut
203 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
204 with
open("test_compunit1.il", "w") as f
:
210 sim
.add_sync_process(wrap(scoreboard_sim(dut
)))
211 sim_writer
= sim
.write_vcd('test_compunit1.vcd')
216 class CompUnitParallelTest
:
217 def __init__(self
, dut
):
220 # Operation cycle should not take longer than this:
221 self
.MAX_BUSY_WAIT
= 50
223 # Minimum duration in which issue_i will be kept inactive,
224 # during which busy_o must remain low.
225 self
.MIN_BUSY_LOW
= 5
227 # Number of cycles to stall until the assertion of go.
228 # One value, for each port. Can be zero, for no delay.
229 self
.RD_GO_DELAY
= [0, 3]
231 # store common data for the input operation of the processes
234 self
.inv_a
= self
.zero_a
= 0
235 self
.imm
= self
.imm_ok
= 0
236 self
.imm_control
= (0, 0)
237 self
.rdmaskn
= (0, 0)
239 self
.operands
= (0, 0)
241 # Indicates completion of the sub-processes
242 self
.rd_complete
= [False, False]
245 print("Begin parallel test.")
246 yield from self
.operation(5, 2, MicrOp
.OP_ADD
)
248 def operation(self
, a
, b
, op
, inv_a
=0, imm
=0, imm_ok
=0, zero_a
=0,
250 # store data for the operation
251 self
.operands
= (a
, b
)
257 self
.imm_control
= (zero_a
, imm_ok
)
258 self
.rdmaskn
= rdmaskn
260 # Initialize completion flags
261 self
.rd_complete
= [False, False]
263 # trigger operation cycle
264 yield from self
.issue()
266 # check that the sub-processes completed, before the busy_o cycle ended
267 for completion
in self
.rd_complete
:
271 # issue_i starts inactive
272 yield self
.dut
.issue_i
.eq(0)
274 for n
in range(self
.MIN_BUSY_LOW
):
276 # busy_o must remain inactive. It cannot rise on its own.
277 busy_o
= yield self
.dut
.busy_o
280 # activate issue_i to begin the operation cycle
281 yield self
.dut
.issue_i
.eq(1)
283 # at the same time, present the operation
284 yield self
.dut
.oper_i
.insn_type
.eq(self
.op
)
285 yield self
.dut
.oper_i
.invert_a
.eq(self
.inv_a
)
286 yield self
.dut
.oper_i
.imm_data
.imm
.eq(self
.imm
)
287 yield self
.dut
.oper_i
.imm_data
.imm_ok
.eq(self
.imm_ok
)
288 yield self
.dut
.oper_i
.zero_a
.eq(self
.zero_a
)
289 rdmaskn
= self
.rdmaskn
[0] |
(self
.rdmaskn
[1] << 1)
290 yield self
.dut
.rdmaskn
.eq(rdmaskn
)
292 # give one cycle for the CompUnit to latch the data
295 # busy_o must keep being low in this cycle, because issue_i was
296 # low on the previous cycle.
297 # It cannot rise on its own.
298 # Also, busy_o and issue_i must never be active at the same time, ever.
299 busy_o
= yield self
.dut
.busy_o
303 yield self
.dut
.issue_i
.eq(0)
305 # deactivate inputs along with issue_i, so we can be sure the data
306 # was latched at the correct cycle
307 # note: rdmaskn must be held, while busy_o is active
308 # TODO: deactivate rdmaskn when the busy_o cycle ends
309 yield self
.dut
.oper_i
.insn_type
.eq(0)
310 yield self
.dut
.oper_i
.invert_a
.eq(0)
311 yield self
.dut
.oper_i
.imm_data
.imm
.eq(0)
312 yield self
.dut
.oper_i
.imm_data
.imm_ok
.eq(0)
313 yield self
.dut
.oper_i
.zero_a
.eq(0)
316 # wait for busy_o to lower
317 # timeout after self.MAX_BUSY_WAIT cycles
318 for n
in range(self
.MAX_BUSY_WAIT
):
319 # sample busy_o in the current cycle
320 busy_o
= yield self
.dut
.busy_o
322 # operation cycle ends when busy_o becomes inactive
326 # if busy_o is still active, a timeout has occurred
327 # TODO: Uncomment this, once the test is complete:
331 print("If you are reading this, "
332 "it's because the above test failed, as expected,\n"
333 "with a timeout. It must pass, once the test is complete.")
336 print("If you are reading this, "
337 "it's because the above test unexpectedly passed.")
339 def rd(self
, rd_idx
):
340 # wait for issue_i to rise
342 issue_i
= yield self
.dut
.issue_i
345 # issue_i has not risen yet, so rd must keep low
346 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
350 # we do not want rd to rise on an immediate operand
351 # if it is immediate, exit the process
352 # likewise, if the read mask is active
353 # TODO: don't exit the process, monitor rd instead to ensure it
354 # doesn't rise on its own
355 if self
.rdmaskn
[rd_idx
] or self
.imm_control
[rd_idx
]:
356 self
.rd_complete
[rd_idx
] = True
359 # issue_i has risen. rel must rise on the next cycle
360 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
363 # stall for additional cycles. Check that rel doesn't fall on its own
364 for n
in range(self
.RD_GO_DELAY
[rd_idx
]):
366 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
369 # Before asserting "go", make sure "rel" has risen.
370 # The use of Settle allows "go" to be set combinatorially,
371 # rising on the same cycle as "rel".
373 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
376 # assert go for one cycle, passing along the operand value
377 yield self
.dut
.rd
.go
[rd_idx
].eq(1)
378 yield self
.dut
.src_i
[rd_idx
].eq(self
.operands
[rd_idx
])
379 # check that the operand was sent to the alu
380 # TODO: Properly check the alu protocol
382 alu_input
= yield self
.dut
.get_in(rd_idx
)
383 assert alu_input
== self
.operands
[rd_idx
]
386 # rel must keep high, since go was inactive in the last cycle
387 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
390 # finish the go one-clock pulse
391 yield self
.dut
.rd
.go
[rd_idx
].eq(0)
392 yield self
.dut
.src_i
[rd_idx
].eq(0)
395 # rel must have gone low in response to go being high
396 # on the previous cycle
397 rel
= yield self
.dut
.rd
.rel
[rd_idx
]
400 self
.rd_complete
[rd_idx
] = True
402 # TODO: check that rel doesn't rise again until the end of the
405 def wr(self
, wr_idx
):
406 # monitor self.dut.wr.req[rd_idx] and sets dut.wr.go[idx] for one cycle
408 # TODO: also when dut.wr.go is set, check the output against the
409 # self.expected_o and assert. use dut.get_out(wr_idx) to do so.
411 def run_simulation(self
, vcd_name
):
413 m
.submodules
.cu
= self
.dut
417 sim
.add_sync_process(wrap(self
.driver()))
418 sim
.add_sync_process(wrap(self
.rd(0)))
419 sim
.add_sync_process(wrap(self
.rd(1)))
420 sim
.add_sync_process(wrap(self
.wr(0)))
421 sim_writer
= sim
.write_vcd(vcd_name
)
426 def test_compunit_regspec2_fsm():
428 inspec
= [('INT', 'a', '0:15'),
429 ('INT', 'b', '0:15'),
431 outspec
= [('INT', 'o', '0:15'),
434 regspec
= (inspec
, outspec
)
438 dut
= MultiCompUnit(regspec
, alu
, CompFSMOpSubset
)
439 m
.submodules
.cu
= dut
444 sim
.add_sync_process(wrap(scoreboard_sim_fsm(dut
)))
445 sim_writer
= sim
.write_vcd('test_compunit_regspec2_fsm.vcd')
450 def test_compunit_regspec3():
452 inspec
= [('INT', 'a', '0:15'),
453 ('INT', 'b', '0:15'),
454 ('INT', 'c', '0:15')]
455 outspec
= [('INT', 'o', '0:15'),
458 regspec
= (inspec
, outspec
)
462 dut
= MultiCompUnit(regspec
, alu
, CompALUOpSubset
)
463 m
.submodules
.cu
= dut
468 sim
.add_sync_process(wrap(scoreboard_sim_dummy(dut
)))
469 sim_writer
= sim
.write_vcd('test_compunit_regspec3.vcd')
474 def test_compunit_regspec1():
476 inspec
= [('INT', 'a', '0:15'),
477 ('INT', 'b', '0:15')]
478 outspec
= [('INT', 'o', '0:15'),
481 regspec
= (inspec
, outspec
)
485 dut
= MultiCompUnit(regspec
, alu
, CompALUOpSubset
)
486 m
.submodules
.cu
= dut
488 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
489 with
open("test_compunit_regspec1.il", "w") as f
:
495 sim
.add_sync_process(wrap(scoreboard_sim(dut
)))
496 sim_writer
= sim
.write_vcd('test_compunit_regspec1.vcd')
500 test
= CompUnitParallelTest(dut
)
501 test
.run_simulation("test_compunit_parallel.vcd")
504 if __name__
== '__main__':
507 test_compunit_regspec1()
508 test_compunit_regspec3()