Merge branch 'master' of ssh://git.libre-riscv.org:922/soc
[soc.git] / src / soc / experiment / test / test_compalu_multi.py
1 """Computation Unit (aka "ALU Manager").
2
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)
9
10 Documented at http://libre-soc.org/3d_gpu/architecture/compunit
11 """
12
13 from nmigen.compat.sim import run_simulation, Settle
14 from nmigen.cli import rtlil
15 from nmigen import Module
16
17 from soc.decoder.power_enums import InternalOp
18
19 from soc.experiment.compalu_multi import MultiCompUnit
20 from soc.experiment.alu_hier import ALU, DummyALU
21 from soc.fu.alu.alu_input_record import CompALUOpSubset
22
23
24 def op_sim(dut, a, b, op, inv_a=0, imm=0, imm_ok=0, zero_a=0):
25 yield dut.issue_i.eq(0)
26 yield
27 yield dut.src_i[0].eq(a)
28 yield dut.src_i[1].eq(b)
29 yield dut.oper_i.insn_type.eq(op)
30 yield dut.oper_i.invert_a.eq(inv_a)
31 yield dut.oper_i.imm_data.imm.eq(imm)
32 yield dut.oper_i.imm_data.imm_ok.eq(imm_ok)
33 yield dut.oper_i.zero_a.eq(zero_a)
34 yield dut.issue_i.eq(1)
35 yield
36 yield dut.issue_i.eq(0)
37 yield
38 if not imm_ok or not zero_a:
39 yield dut.rd.go.eq(0b11)
40 while True:
41 yield
42 rd_rel_o = yield dut.rd.rel
43 print ("rd_rel", rd_rel_o)
44 if rd_rel_o:
45 break
46 yield dut.rd.go.eq(0)
47 if len(dut.src_i) == 3:
48 yield dut.rd.go.eq(0b100)
49 while True:
50 yield
51 rd_rel_o = yield dut.rd.rel
52 print ("rd_rel", rd_rel_o)
53 if rd_rel_o:
54 break
55 yield dut.rd.go.eq(0)
56
57 req_rel_o = yield dut.wr.rel
58 result = yield dut.data_o
59 print ("req_rel", req_rel_o, result)
60 while True:
61 req_rel_o = yield dut.wr.rel
62 result = yield dut.data_o
63 print ("req_rel", req_rel_o, result)
64 if req_rel_o:
65 break
66 yield
67 yield dut.wr.go[0].eq(1)
68 yield Settle()
69 result = yield dut.data_o
70 yield
71 print ("result", result)
72 yield dut.wr.go[0].eq(0)
73 yield
74 return result
75
76
77 def scoreboard_sim_dummy(dut):
78 result = yield from op_sim(dut, 5, 2, InternalOp.OP_NOP, inv_a=0,
79 imm=8, imm_ok=1)
80 assert result == 5, result
81
82 result = yield from op_sim(dut, 9, 2, InternalOp.OP_NOP, inv_a=0,
83 imm=8, imm_ok=1)
84 assert result == 9, result
85
86
87 def scoreboard_sim(dut):
88 result = yield from op_sim(dut, 5, 2, InternalOp.OP_ADD, inv_a=0,
89 imm=8, imm_ok=1)
90 assert result == 13
91
92 result = yield from op_sim(dut, 5, 2, InternalOp.OP_ADD)
93 assert result == 7
94
95 result = yield from op_sim(dut, 5, 2, InternalOp.OP_ADD, inv_a=1)
96 assert result == 65532
97
98 result = yield from op_sim(dut, 5, 2, InternalOp.OP_ADD, zero_a=1,
99 imm=8, imm_ok=1)
100 assert result == 8
101
102 result = yield from op_sim(dut, 5, 2, InternalOp.OP_ADD, zero_a=1)
103 assert result == 2
104
105
106 def test_compunit():
107
108 m = Module()
109 alu = ALU(16)
110 dut = MultiCompUnit(16, alu, CompALUOpSubset)
111 m.submodules.cu = dut
112
113 vl = rtlil.convert(dut, ports=dut.ports())
114 with open("test_compunit1.il", "w") as f:
115 f.write(vl)
116
117 run_simulation(m, scoreboard_sim(dut), vcd_name='test_compunit1.vcd')
118
119
120 class CompUnitParallelTest:
121 def __init__(self, dut):
122 self.dut = dut
123
124 # Operation cycle should not take longer than this:
125 self.MAX_BUSY_WAIT = 50
126
127 # Minimum duration in which issue_i will be kept inactive,
128 # during which busy_o must remain low.
129 self.MIN_BUSY_LOW = 5
130
131 # Number of cycles to stall until the assertion of go.
132 # One value, for each port. Can be zero, for no delay.
133 self.RD_GO_DELAY = [0, 3]
134
135 # store common data for the input operation of the processes
136 # input operation:
137 self.op = 0
138 self.inv_a = self.zero_a = 0
139 self.imm = self.imm_ok = 0
140 self.imm_control = (0, 0)
141 self.rdmaskn = (0, 0)
142 # input data:
143 self.operands = (0, 0)
144
145 # Indicates completion of the sub-processes
146 self.rd_complete = [False, False]
147
148 def driver(self):
149 print("Begin parallel test.")
150 yield from self.operation(5, 2, InternalOp.OP_ADD, inv_a=0,
151 imm=8, imm_ok=0, rdmaskn=(1, 0))
152
153 def operation(self, a, b, op, inv_a=0, imm=0, imm_ok=0, zero_a=0,
154 rdmaskn=(0, 0)):
155 # store data for the operation
156 self.operands = (a, b)
157 self.op = op
158 self.inv_a = inv_a
159 self.imm = imm
160 self.imm_ok = imm_ok
161 self.zero_a = zero_a
162 self.imm_control = (zero_a, imm_ok)
163 self.rdmaskn = rdmaskn
164
165 # Initialize completion flags
166 self.rd_complete = [False, False]
167
168 # trigger operation cycle
169 yield from self.issue()
170
171 # check that the sub-processes completed, before the busy_o cycle ended
172 for completion in self.rd_complete:
173 assert completion
174
175 def issue(self):
176 # issue_i starts inactive
177 yield self.dut.issue_i.eq(0)
178
179 for n in range(self.MIN_BUSY_LOW):
180 yield
181 # busy_o must remain inactive. It cannot rise on its own.
182 busy_o = yield self.dut.busy_o
183 assert not busy_o
184
185 # activate issue_i to begin the operation cycle
186 yield self.dut.issue_i.eq(1)
187
188 # at the same time, present the operation
189 yield self.dut.oper_i.insn_type.eq(self.op)
190 yield self.dut.oper_i.invert_a.eq(self.inv_a)
191 yield self.dut.oper_i.imm_data.imm.eq(self.imm)
192 yield self.dut.oper_i.imm_data.imm_ok.eq(self.imm_ok)
193 yield self.dut.oper_i.zero_a.eq(self.zero_a)
194 rdmaskn = self.rdmaskn[0] | (self.rdmaskn[1] << 1)
195 yield self.dut.rdmaskn.eq(rdmaskn)
196
197 # give one cycle for the CompUnit to latch the data
198 yield
199
200 # busy_o must keep being low in this cycle, because issue_i was
201 # low on the previous cycle.
202 # It cannot rise on its own.
203 # Also, busy_o and issue_i must never be active at the same time, ever.
204 busy_o = yield self.dut.busy_o
205 assert not busy_o
206
207 # Lower issue_i
208 yield self.dut.issue_i.eq(0)
209
210 # deactivate inputs along with issue_i, so we can be sure the data
211 # was latched at the correct cycle
212 # note: rdmaskn must be held, while busy_o is active
213 # TODO: deactivate rdmaskn when the busy_o cycle ends
214 yield self.dut.oper_i.insn_type.eq(0)
215 yield self.dut.oper_i.invert_a.eq(0)
216 yield self.dut.oper_i.imm_data.imm.eq(0)
217 yield self.dut.oper_i.imm_data.imm_ok.eq(0)
218 yield self.dut.oper_i.zero_a.eq(0)
219 yield
220
221 # wait for busy_o to lower
222 # timeout after self.MAX_BUSY_WAIT cycles
223 for n in range(self.MAX_BUSY_WAIT):
224 # sample busy_o in the current cycle
225 busy_o = yield self.dut.busy_o
226 if not busy_o:
227 # operation cycle ends when busy_o becomes inactive
228 break
229 yield
230
231 # if busy_o is still active, a timeout has occurred
232 # TODO: Uncomment this, once the test is complete:
233 # assert not busy_o
234
235 if busy_o:
236 print("If you are reading this, "
237 "it's because the above test failed, as expected,\n"
238 "with a timeout. It must pass, once the test is complete.")
239 return
240
241 print("If you are reading this, "
242 "it's because the above test unexpectedly passed.")
243
244 def rd(self, rd_idx):
245 # wait for issue_i to rise
246 while True:
247 issue_i = yield self.dut.issue_i
248 if issue_i:
249 break
250 # issue_i has not risen yet, so rd must keep low
251 rel = yield self.dut.rd.rel[rd_idx]
252 assert not rel
253 yield
254
255 # we do not want rd to rise on an immediate operand
256 # if it is immediate, exit the process
257 # likewise, if the read mask is active
258 # TODO: don't exit the process, monitor rd instead to ensure it
259 # doesn't rise on its own
260 if self.rdmaskn[rd_idx] or self.imm_control[rd_idx]:
261 self.rd_complete[rd_idx] = True
262 return
263
264 # issue_i has risen. rel must rise on the next cycle
265 rel = yield self.dut.rd.rel[rd_idx]
266 assert not rel
267
268 # stall for additional cycles. Check that rel doesn't fall on its own
269 for n in range(self.RD_GO_DELAY[rd_idx]):
270 yield
271 rel = yield self.dut.rd.rel[rd_idx]
272 assert rel
273
274 # Before asserting "go", make sure "rel" has risen.
275 # The use of Settle allows "go" to be set combinatorially,
276 # rising on the same cycle as "rel".
277 yield Settle()
278 rel = yield self.dut.rd.rel[rd_idx]
279 assert rel
280
281 # assert go for one cycle, passing along the operand value
282 yield self.dut.rd.go[rd_idx].eq(1)
283 yield self.dut.src_i[rd_idx].eq(self.operands[rd_idx])
284 # check that the operand was sent to the alu
285 # TODO: Properly check the alu protocol
286 yield Settle()
287 alu_input = yield self.dut.get_in(rd_idx)
288 assert alu_input == self.operands[rd_idx]
289 yield
290
291 # rel must keep high, since go was inactive in the last cycle
292 rel = yield self.dut.rd.rel[rd_idx]
293 assert rel
294
295 # finish the go one-clock pulse
296 yield self.dut.rd.go[rd_idx].eq(0)
297 yield self.dut.src_i[rd_idx].eq(0)
298 yield
299
300 # rel must have gone low in response to go being high
301 # on the previous cycle
302 rel = yield self.dut.rd.rel[rd_idx]
303 assert not rel
304
305 self.rd_complete[rd_idx] = True
306
307 # TODO: check that rel doesn't rise again until the end of the
308 # busy_o cycle
309
310 def wr(self, wr_idx):
311 # monitor self.dut.wr.req[rd_idx] and sets dut.wr.go[idx] for one cycle
312 yield
313 # TODO: also when dut.wr.go is set, check the output against the
314 # self.expected_o and assert. use dut.get_out(wr_idx) to do so.
315
316 def run_simulation(self, vcd_name):
317 run_simulation(self.dut, [self.driver(),
318 self.rd(0), # one read port (a)
319 self.rd(1), # one read port (b)
320 self.wr(0), # one write port (o)
321 ],
322 vcd_name=vcd_name)
323
324
325 def test_compunit_regspec3():
326
327 inspec = [('INT', 'a', '0:15'),
328 ('INT', 'b', '0:15'),
329 ('INT', 'c', '0:15')]
330 outspec = [('INT', 'o', '0:15'),
331 ]
332
333 regspec = (inspec, outspec)
334
335 m = Module()
336 alu = DummyALU(16)
337 dut = MultiCompUnit(regspec, alu, CompALUOpSubset)
338 m.submodules.cu = dut
339
340 run_simulation(m, scoreboard_sim_dummy(dut),
341 vcd_name='test_compunit_regspec3.vcd')
342
343
344 def test_compunit_regspec1():
345
346 inspec = [('INT', 'a', '0:15'),
347 ('INT', 'b', '0:15')]
348 outspec = [('INT', 'o', '0:15'),
349 ]
350
351 regspec = (inspec, outspec)
352
353 m = Module()
354 alu = ALU(16)
355 dut = MultiCompUnit(regspec, alu, CompALUOpSubset)
356 m.submodules.cu = dut
357
358 vl = rtlil.convert(dut, ports=dut.ports())
359 with open("test_compunit_regspec1.il", "w") as f:
360 f.write(vl)
361
362 run_simulation(m, scoreboard_sim(dut),
363 vcd_name='test_compunit_regspec1.vcd')
364
365 test = CompUnitParallelTest(dut)
366 test.run_simulation("test_compunit_parallel.vcd")
367
368
369 if __name__ == '__main__':
370 test_compunit()
371 test_compunit_regspec1()
372 test_compunit_regspec3()