rather invasive reduction of SPR regfile size
[soc.git] / src / soc / simple / test / test_runner.py
1 """TestRunner class, runs TestIssuer instructions
2
3 related bugs:
4
5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
6 """
7 from nmigen import Module, Signal, Cat, ClockSignal
8
9 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
10 # Also, check out the cxxsim nmigen branch, and latest yosys from git
11 from nmutil.sim_tmp_alternative import Simulator, Settle
12
13 from nmutil.formaltest import FHDLTestCase
14 from nmutil.gtkw import write_gtkw
15 from nmigen.cli import rtlil
16 from soc.decoder.isa.caller import special_sprs, SVP64State
17 from soc.decoder.isa.all import ISA
18 from soc.config.endian import bigendian
19
20 from soc.decoder.power_decoder import create_pdecode
21 from soc.decoder.power_decoder2 import PowerDecode2
22 from soc.regfile.regfiles import StateRegs
23
24 from soc.simple.issuer import TestIssuerInternal
25
26 from soc.config.test.test_loadstore import TestMemPspec
27 from soc.simple.test.test_core import (setup_regs, check_regs,
28 wait_for_busy_clear,
29 wait_for_busy_hi)
30 from soc.fu.compunits.test.test_compunit import (setup_test_memory,
31 check_sim_memory)
32 from soc.debug.dmi import DBGCore, DBGCtrl, DBGStat
33 from nmutil.util import wrap
34 from soc.experiment.test.test_mmu_dcache import wb_get
35
36
37 def setup_i_memory(imem, startaddr, instructions):
38 mem = imem
39 print("insn before, init mem", mem.depth, mem.width, mem,
40 len(instructions))
41 for i in range(mem.depth):
42 yield mem._array[i].eq(0)
43 yield Settle()
44 startaddr //= 4 # instructions are 32-bit
45 if mem.width == 32:
46 mask = ((1 << 32)-1)
47 for ins in instructions:
48 if isinstance(ins, tuple):
49 insn, code = ins
50 else:
51 insn, code = ins, ''
52 insn = insn & 0xffffffff
53 yield mem._array[startaddr].eq(insn)
54 yield Settle()
55 if insn != 0:
56 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
57 startaddr += 1
58 startaddr = startaddr & mask
59 return
60
61 # 64 bit
62 mask = ((1 << 64)-1)
63 for ins in instructions:
64 if isinstance(ins, tuple):
65 insn, code = ins
66 else:
67 insn, code = ins, ''
68 insn = insn & 0xffffffff
69 msbs = (startaddr >> 1) & mask
70 val = yield mem._array[msbs]
71 if insn != 0:
72 print("before set", hex(4*startaddr),
73 hex(msbs), hex(val), hex(insn))
74 lsb = 1 if (startaddr & 1) else 0
75 val = (val | (insn << (lsb*32)))
76 val = val & mask
77 yield mem._array[msbs].eq(val)
78 yield Settle()
79 if insn != 0:
80 print("after set", hex(4*startaddr), hex(msbs), hex(val))
81 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
82 startaddr += 1
83 startaddr = startaddr & mask
84
85
86 def set_dmi(dmi, addr, data):
87 yield dmi.req_i.eq(1)
88 yield dmi.addr_i.eq(addr)
89 yield dmi.din.eq(data)
90 yield dmi.we_i.eq(1)
91 while True:
92 ack = yield dmi.ack_o
93 if ack:
94 break
95 yield
96 yield
97 yield dmi.req_i.eq(0)
98 yield dmi.addr_i.eq(0)
99 yield dmi.din.eq(0)
100 yield dmi.we_i.eq(0)
101 yield
102
103
104 def get_dmi(dmi, addr):
105 yield dmi.req_i.eq(1)
106 yield dmi.addr_i.eq(addr)
107 yield dmi.din.eq(0)
108 yield dmi.we_i.eq(0)
109 while True:
110 ack = yield dmi.ack_o
111 if ack:
112 break
113 yield
114 yield # wait one
115 data = yield dmi.dout # get data after ack valid for 1 cycle
116 yield dmi.req_i.eq(0)
117 yield dmi.addr_i.eq(0)
118 yield dmi.we_i.eq(0)
119 yield
120 return data
121
122
123 class TestRunner(FHDLTestCase):
124 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
125 svp64=True):
126 super().__init__("run_all")
127 self.test_data = tst_data
128 self.microwatt_mmu = microwatt_mmu
129 self.rom = rom
130 self.svp64 = svp64
131
132 def run_all(self):
133 m = Module()
134 comb = m.d.comb
135 pc_i = Signal(32)
136 svstate_i = Signal(32)
137
138 pspec = TestMemPspec(ldst_ifacetype='test_bare_wb',
139 imem_ifacetype='test_bare_wb',
140 addr_wid=48,
141 mask_wid=8,
142 imem_reg_wid=64,
143 # wb_data_width=32,
144 use_pll=False,
145 nocore=False,
146 xics=False,
147 gpio=False,
148 regreduce=True,
149 svp64=self.svp64,
150 mmu=self.microwatt_mmu,
151 reg_wid=64)
152 m.submodules.issuer = issuer = TestIssuerInternal(pspec)
153 imem = issuer.imem._get_memory()
154 core = issuer.core
155 dmi = issuer.dbg.dmi
156 pdecode2 = issuer.pdecode2
157 l0 = core.l0
158 regreduce_en = pspec.regreduce_en == True
159
160 # copy of the decoder for simulator
161 simdec = create_pdecode()
162 simdec2 = PowerDecode2(simdec, regreduce_en=regreduce_en)
163 m.submodules.simdec2 = simdec2 # pain in the neck
164
165 # run core clock at same rate as test clock
166 intclk = ClockSignal("coresync")
167 comb += intclk.eq(ClockSignal())
168
169 comb += issuer.pc_i.data.eq(pc_i)
170 comb += issuer.svstate_i.data.eq(svstate_i)
171
172 # nmigen Simulation
173 sim = Simulator(m)
174 sim.add_clock(1e-6)
175
176 def process():
177
178 # start in stopped
179 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
180 yield
181 yield
182
183 # get each test, completely reset the core, and run it
184
185 for test in self.test_data:
186
187 # pull a reset
188 # yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
189
190 # set up bigendian (TODO: don't do this, use MSR)
191 yield issuer.core_bigendian_i.eq(bigendian)
192 yield Settle()
193
194 yield
195 yield
196 yield
197 yield
198
199 print(test.name)
200 program = test.program
201 self.subTest(test.name)
202 print("regs", test.regs)
203 print("sprs", test.sprs)
204 print("cr", test.cr)
205 print("mem", test.mem)
206 print("msr", test.msr)
207 print("assem", program.assembly)
208 gen = list(program.generate_instructions())
209 insncode = program.assembly.splitlines()
210 instructions = list(zip(gen, insncode))
211
212 # set up the Simulator (which must track TestIssuer exactly)
213 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
214 test.msr,
215 initial_insns=gen, respect_pc=True,
216 disassembly=insncode,
217 bigendian=bigendian,
218 initial_svstate=test.svstate)
219
220 # establish the TestIssuer context (mem, regs etc)
221
222 pc = 0 # start address
223 counter = 0 # test to pause/start
224
225 yield from setup_i_memory(imem, pc, instructions)
226 yield from setup_test_memory(l0, sim)
227 yield from setup_regs(pdecode2, core, test)
228
229 # set PC and SVSTATE
230 yield pc_i.eq(pc)
231 yield issuer.pc_i.ok.eq(1)
232
233 initial_svstate = test.svstate
234 if isinstance(initial_svstate, int):
235 initial_svstate = SVP64State(initial_svstate)
236 yield svstate_i.eq(initial_svstate.spr.value)
237 yield issuer.svstate_i.ok.eq(1)
238 yield
239
240 print("instructions", instructions)
241
242 # run the loop of the instructions on the current test
243 index = sim.pc.CIA.value//4
244 while index < len(instructions):
245 ins, code = instructions[index]
246
247 print("instruction: 0x{:X}".format(ins & 0xffffffff))
248 print(index, code)
249
250 if counter == 0:
251 # start the core
252 yield
253 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.START)
254 yield issuer.pc_i.ok.eq(0) # no change PC after this
255 yield issuer.svstate_i.ok.eq(0) # ditto
256 yield
257 yield
258
259 counter = counter + 1
260
261 # wait until executed
262 # wait for insn_done high
263 while not (yield issuer.insn_done):
264 yield
265 # wait for insn_done low
266 while (yield issuer.insn_done):
267 yield
268
269 # set up simulated instruction (in simdec2)
270 try:
271 yield from sim.setup_one()
272 except KeyError: # indicates instruction not in imem: stop
273 break
274 yield Settle()
275
276 # call simulated operation
277 print("sim", code)
278 yield from sim.execute_one()
279 yield Settle()
280 index = sim.pc.CIA.value//4
281
282 terminated = yield issuer.dbg.terminated_o
283 print("terminated", terminated)
284
285 if index >= len(instructions):
286 print ("index over, send dmi stop")
287 # stop at end
288 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
289 yield
290 yield
291
292 # wait one cycle for registers to settle
293 yield
294
295 # register check
296 yield from check_regs(self, sim, core, test, code)
297
298 # Memory check
299 yield from check_sim_memory(self, l0, sim, code)
300
301 terminated = yield issuer.dbg.terminated_o
302 print("terminated(2)", terminated)
303 if terminated:
304 break
305
306 # stop at end
307 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
308 yield
309 yield
310
311 # get CR
312 cr = yield from get_dmi(dmi, DBGCore.CR)
313 print("after test %s cr value %x" % (test.name, cr))
314
315 # get XER
316 xer = yield from get_dmi(dmi, DBGCore.XER)
317 print("after test %s XER value %x" % (test.name, xer))
318
319 # test of dmi reg get
320 for int_reg in range(32):
321 yield from set_dmi(dmi, DBGCore.GSPR_IDX, int_reg)
322 value = yield from get_dmi(dmi, DBGCore.GSPR_DATA)
323
324 print("after test %s reg %2d value %x" %
325 (test.name, int_reg, value))
326
327 styles = {
328 'dec': {'base': 'dec'},
329 'bin': {'base': 'bin'},
330 'closed': {'closed': True}
331 }
332
333 traces = [
334 'clk',
335 ('state machines', 'closed', [
336 'fetch_pc_valid_i', 'fetch_pc_ready_o',
337 'fetch_fsm_state',
338 'fetch_insn_valid_o', 'fetch_insn_ready_i',
339 'pred_insn_valid_i', 'pred_insn_ready_o',
340 'fetch_predicate_state',
341 'pred_mask_valid_o', 'pred_mask_ready_i',
342 'issue_fsm_state',
343 'exec_insn_valid_i', 'exec_insn_ready_o',
344 'exec_fsm_state',
345 'exec_pc_valid_o', 'exec_pc_ready_i',
346 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
347 'is_last', 'dec2.no_out_vec']),
348 {'comment': 'fetch and decode'},
349 (None, 'dec', [
350 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
351 'cur_pc[63:0]', 'core_core_cia[63:0]']),
352 'raw_insn_i[31:0]',
353 'raw_opcode_in[31:0]', 'insn_type',
354 ('svp64 decoding', 'closed', [
355 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
356 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
357 'dec2.sv_rm_dec.ptype_in',
358 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
359 'dstmask[63:0]', 'srcmask[63:0]',
360 'dregread[4:0]', 'dinvert',
361 'sregread[4:0]', 'sinvert',
362 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
363 'core.int.pred__ren']),
364 ('register augmentation', 'dec', 'closed', [
365 {'comment': 'v3.0b registers'},
366 'dec2.dec_o.RT[4:0]',
367 'dec2.dec_a.RA[4:0]',
368 'dec2.dec_b.RB[4:0]',
369 ('Rdest', [
370 'dec2.o_svdec.reg_in[4:0]',
371 ('dec2.o_svdec.spec[2:0]', 'bin'),
372 'dec2.o_svdec.reg_out[6:0]']),
373 ('Rsrc1', [
374 'dec2.in1_svdec.reg_in[4:0]',
375 ('dec2.in1_svdec.spec[2:0]', 'bin'),
376 'dec2.in1_svdec.reg_out[6:0]']),
377 ('Rsrc1', [
378 'dec2.in2_svdec.reg_in[4:0]',
379 ('dec2.in2_svdec.spec[2:0]', 'bin'),
380 'dec2.in2_svdec.reg_out[6:0]']),
381 {'comment': 'SVP64 registers'},
382 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
383 ]),
384 {'comment': 'svp64 context'},
385 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
386 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
387 'core_core_dststep[6:0]',
388 {'comment': 'issue and execute'},
389 'core.core_core_insn_type',
390 (None, 'dec', [
391 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
392 'issue_i', 'busy_o',
393 {'comment': 'dmi'},
394 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
395 {'comment': 'instruction memory'},
396 'imem.sram.rdport.memory(0)[63:0]',
397 {'comment': 'registers'},
398 'core.int.rp_src1.memory(0)[63:0]',
399 'core.int.rp_src1.memory(1)[63:0]',
400 'core.int.rp_src1.memory(2)[63:0]',
401 'core.int.rp_src1.memory(3)[63:0]',
402 'core.int.rp_src1.memory(4)[63:0]',
403 'core.int.rp_src1.memory(5)[63:0]',
404 'core.int.rp_src1.memory(6)[63:0]',
405 'core.int.rp_src1.memory(7)[63:0]',
406 'core.int.rp_src1.memory(9)[63:0]',
407 'core.int.rp_src1.memory(10)[63:0]',
408 'core.int.rp_src1.memory(13)[63:0]',
409 ]
410
411 if self.microwatt_mmu:
412 traces += [
413 {'comment': 'microwatt_mmu'},
414 'core.fus.mmu0.alu_mmu0.illegal',
415 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
416 'core.fus.mmu0.alu_mmu0.mmu.state',
417 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
418 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
419 {'comment': 'wishbone_memory'},
420 'core.fus.mmu0.alu_mmu0.dcache.stb',
421 'core.fus.mmu0.alu_mmu0.dcache.cyc',
422 'core.fus.mmu0.alu_mmu0.dcache.we',
423 'core.fus.mmu0.alu_mmu0.dcache.ack',
424 'core.fus.mmu0.alu_mmu0.dcache.stall,'
425 ]
426
427 write_gtkw("issuer_simulator.gtkw",
428 "issuer_simulator.vcd",
429 traces, styles, module='top.issuer')
430
431 # add run of instructions
432 sim.add_sync_process(process)
433
434 # optionally, if a wishbone-based ROM is passed in, run that as an
435 # extra emulated process
436 if self.rom is not None:
437 dcache = core.fus.fus["mmu0"].alu.dcache
438 default_mem = self.rom
439 sim.add_sync_process(wrap(wb_get(dcache, default_mem, "DCACHE")))
440
441 with sim.write_vcd("issuer_simulator.vcd"):
442 sim.run()