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