096bb88c119c447e38b2de930547d275f5fea6e4
[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 * https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
7 """
8 from nmigen import Module, Signal, Cat, ClockSignal
9 from nmigen.hdl.xfrm import ResetInserter
10 from copy import copy
11
12 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
13 # Also, check out the cxxsim nmigen branch, and latest yosys from git
14 from nmutil.sim_tmp_alternative import Simulator, Settle
15
16 from nmutil.formaltest import FHDLTestCase
17 from nmutil.gtkw import write_gtkw
18 from nmigen.cli import rtlil
19 from openpower.decoder.isa.caller import special_sprs, SVP64State
20 from openpower.decoder.isa.all import ISA
21 from openpower.endian import bigendian
22
23 from openpower.decoder.power_decoder import create_pdecode
24 from openpower.decoder.power_decoder2 import PowerDecode2
25 from soc.regfile.regfiles import StateRegs
26
27 from soc.simple.issuer import TestIssuerInternal
28
29 from soc.config.test.test_loadstore import TestMemPspec
30 from soc.simple.test.test_core import (setup_regs, check_regs, check_mem,
31 wait_for_busy_clear,
32 wait_for_busy_hi)
33 from soc.fu.compunits.test.test_compunit import (setup_tst_memory,
34 check_sim_memory)
35 from soc.debug.dmi import DBGCore, DBGCtrl, DBGStat
36 from nmutil.util import wrap
37 from soc.experiment.test.test_mmu_dcache import wb_get
38 from openpower.test.state import TestState, StateRunner
39
40
41 def setup_i_memory(imem, startaddr, instructions):
42 mem = imem
43 print("insn before, init mem", mem.depth, mem.width, mem,
44 len(instructions))
45 for i in range(mem.depth):
46 yield mem._array[i].eq(0)
47 yield Settle()
48 startaddr //= 4 # instructions are 32-bit
49 if mem.width == 32:
50 mask = ((1 << 32)-1)
51 for ins in instructions:
52 if isinstance(ins, tuple):
53 insn, code = ins
54 else:
55 insn, code = ins, ''
56 insn = insn & 0xffffffff
57 yield mem._array[startaddr].eq(insn)
58 yield Settle()
59 if insn != 0:
60 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
61 startaddr += 1
62 startaddr = startaddr & mask
63 return
64
65 # 64 bit
66 mask = ((1 << 64)-1)
67 for ins in instructions:
68 if isinstance(ins, tuple):
69 insn, code = ins
70 else:
71 insn, code = ins, ''
72 insn = insn & 0xffffffff
73 msbs = (startaddr >> 1) & mask
74 val = yield mem._array[msbs]
75 if insn != 0:
76 print("before set", hex(4*startaddr),
77 hex(msbs), hex(val), hex(insn))
78 lsb = 1 if (startaddr & 1) else 0
79 val = (val | (insn << (lsb*32)))
80 val = val & mask
81 yield mem._array[msbs].eq(val)
82 yield Settle()
83 if insn != 0:
84 print("after set", hex(4*startaddr), hex(msbs), hex(val))
85 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
86 startaddr += 1
87 startaddr = startaddr & mask
88
89
90 def set_dmi(dmi, addr, data):
91 yield dmi.req_i.eq(1)
92 yield dmi.addr_i.eq(addr)
93 yield dmi.din.eq(data)
94 yield dmi.we_i.eq(1)
95 while True:
96 ack = yield dmi.ack_o
97 if ack:
98 break
99 yield
100 yield
101 yield dmi.req_i.eq(0)
102 yield dmi.addr_i.eq(0)
103 yield dmi.din.eq(0)
104 yield dmi.we_i.eq(0)
105 yield
106
107
108 def get_dmi(dmi, addr):
109 yield dmi.req_i.eq(1)
110 yield dmi.addr_i.eq(addr)
111 yield dmi.din.eq(0)
112 yield dmi.we_i.eq(0)
113 while True:
114 ack = yield dmi.ack_o
115 if ack:
116 break
117 yield
118 yield # wait one
119 data = yield dmi.dout # get data after ack valid for 1 cycle
120 yield dmi.req_i.eq(0)
121 yield dmi.addr_i.eq(0)
122 yield dmi.we_i.eq(0)
123 yield
124 return data
125
126
127 class SimRunner(StateRunner):
128 def __init__(self, dut, m, pspec):
129 super().__init__("sim", SimRunner)
130 self.dut = dut
131
132 regreduce_en = pspec.regreduce_en == True
133 self.simdec2 = simdec2 = PowerDecode2(None, regreduce_en=regreduce_en)
134 m.submodules.simdec2 = simdec2 # pain in the neck
135
136 def prepare_for_test(self, test):
137 self.test = test
138 if False:
139 yield
140
141 def run_test(self, instructions, gen, insncode):
142 """run_sim_state - runs an ISACaller simulation
143 """
144
145 dut, test, simdec2 = self.dut, self.test, self.simdec2
146 sim_states = []
147
148 # set up the Simulator (which must track TestIssuer exactly)
149 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
150 test.msr,
151 initial_insns=gen, respect_pc=True,
152 disassembly=insncode,
153 bigendian=bigendian,
154 initial_svstate=test.svstate)
155
156 # run the loop of the instructions on the current test
157 index = sim.pc.CIA.value//4
158 while index < len(instructions):
159 ins, code = instructions[index]
160
161 print("sim instr: 0x{:X}".format(ins & 0xffffffff))
162 print(index, code)
163
164 # set up simulated instruction (in simdec2)
165 try:
166 yield from sim.setup_one()
167 except KeyError: # instruction not in imem: stop
168 break
169 yield Settle()
170
171 # call simulated operation
172 print("sim", code)
173 yield from sim.execute_one()
174 yield Settle()
175 index = sim.pc.CIA.value//4
176
177 # get sim register and memory TestState, add to list
178 state = yield from TestState("sim", sim, dut, code)
179 sim_states.append(state)
180
181 return sim_states
182
183
184 class HDLRunner(StateRunner):
185 def __init__(self, dut, m, pspec):
186 super().__init__("hdl", HDLRunner)
187
188 self.dut = dut
189 self.pc_i = Signal(32)
190 self.svstate_i = Signal(64)
191
192 #hard_reset = Signal(reset_less=True)
193 self.issuer = TestIssuerInternal(pspec)
194 # use DMI RESET command instead, this does actually work though
195 #issuer = ResetInserter({'coresync': hard_reset,
196 # 'sync': hard_reset})(issuer)
197 m.submodules.issuer = self.issuer
198 self.dmi = self.issuer.dbg.dmi
199
200 comb = m.d.comb
201 comb += self.issuer.pc_i.data.eq(self.pc_i)
202 comb += self.issuer.svstate_i.data.eq(self.svstate_i)
203
204 def prepare_for_test(self, test):
205 self.test = test
206
207 # set up bigendian (TODO: don't do this, use MSR)
208 yield self.issuer.core_bigendian_i.eq(bigendian)
209 yield Settle()
210
211 yield
212 yield
213 yield
214 yield
215
216 def setup_during_test(self):
217 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
218 yield
219
220 def run_test(self, instructions):
221 """run_hdl_state - runs a TestIssuer nmigen HDL simulation
222 """
223
224 imem = self.issuer.imem._get_memory()
225 core = self.issuer.core
226 dmi = self.issuer.dbg.dmi
227 pdecode2 = self.issuer.pdecode2
228 l0 = core.l0
229 hdl_states = []
230
231 # establish the TestIssuer context (mem, regs etc)
232
233 pc = 0 # start address
234 counter = 0 # test to pause/start
235
236 yield from setup_i_memory(imem, pc, instructions)
237 yield from setup_tst_memory(l0, self.test.mem)
238 yield from setup_regs(pdecode2, core, self.test)
239
240 # set PC and SVSTATE
241 yield self.pc_i.eq(pc)
242 yield self.issuer.pc_i.ok.eq(1)
243
244 # copy initial SVSTATE
245 initial_svstate = copy(self.test.svstate)
246 if isinstance(initial_svstate, int):
247 initial_svstate = SVP64State(initial_svstate)
248 yield self.svstate_i.eq(initial_svstate.value)
249 yield self.issuer.svstate_i.ok.eq(1)
250 yield
251
252 print("instructions", instructions)
253
254 # run the loop of the instructions on the current test
255 index = (yield self.issuer.cur_state.pc) // 4
256 while index < len(instructions):
257 ins, code = instructions[index]
258
259 print("hdl instr: 0x{:X}".format(ins & 0xffffffff))
260 print(index, code)
261
262 if counter == 0:
263 # start the core
264 yield
265 yield from set_dmi(dmi, DBGCore.CTRL,
266 1<<DBGCtrl.START)
267 yield self.issuer.pc_i.ok.eq(0) # no change PC after this
268 yield self.issuer.svstate_i.ok.eq(0) # ditto
269 yield
270 yield
271
272 counter = counter + 1
273
274 # wait until executed
275 while not (yield self.issuer.insn_done):
276 yield
277
278 yield Settle()
279
280 index = (yield self.issuer.cur_state.pc) // 4
281
282 terminated = yield self.issuer.dbg.terminated_o
283 print("terminated", terminated)
284
285 if index < len(instructions):
286 # Get HDL mem and state
287 state = yield from TestState("hdl", core, self.dut,
288 code)
289 hdl_states.append(state)
290
291 if index >= len(instructions):
292 print ("index over, send dmi stop")
293 # stop at end
294 yield from set_dmi(dmi, DBGCore.CTRL,
295 1<<DBGCtrl.STOP)
296 yield
297 yield
298
299 terminated = yield self.issuer.dbg.terminated_o
300 print("terminated(2)", terminated)
301 if terminated:
302 break
303
304 return hdl_states
305
306 def end_test(self):
307 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
308 yield
309 yield
310
311 # TODO, here is where the static (expected) results
312 # can be checked: register check (TODO, memory check)
313 # see https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
314 # yield from check_regs(self, sim, core, test, code,
315 # >>>expected_data<<<)
316
317 # get CR
318 cr = yield from get_dmi(self.dmi, DBGCore.CR)
319 print("after test %s cr value %x" % (self.test.name, cr))
320
321 # get XER
322 xer = yield from get_dmi(self.dmi, DBGCore.XER)
323 print("after test %s XER value %x" % (self.test.name, xer))
324
325 # test of dmi reg get
326 for int_reg in range(32):
327 yield from set_dmi(self.dmi, DBGCore.GSPR_IDX, int_reg)
328 value = yield from get_dmi(self.dmi, DBGCore.GSPR_DATA)
329
330 print("after test %s reg %2d value %x" %
331 (self.test.name, int_reg, value))
332
333 # pull a reset
334 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
335 yield
336
337
338 class TestRunner(FHDLTestCase):
339 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
340 svp64=True, run_hdl=True, run_sim=True):
341 super().__init__("run_all")
342 self.test_data = tst_data
343 self.microwatt_mmu = microwatt_mmu
344 self.rom = rom
345 self.svp64 = svp64
346 self.run_hdl = run_hdl
347 self.run_sim = run_sim
348
349 def run_all(self):
350 m = Module()
351 comb = m.d.comb
352 if self.microwatt_mmu:
353 ldst_ifacetype = 'test_mmu_cache_wb'
354 else:
355 ldst_ifacetype = 'test_bare_wb'
356 imem_ifacetype = 'test_bare_wb'
357
358 pspec = TestMemPspec(ldst_ifacetype=ldst_ifacetype,
359 imem_ifacetype=imem_ifacetype,
360 addr_wid=48,
361 mask_wid=8,
362 imem_reg_wid=64,
363 # wb_data_width=32,
364 use_pll=False,
365 nocore=False,
366 xics=False,
367 gpio=False,
368 regreduce=True,
369 svp64=self.svp64,
370 mmu=self.microwatt_mmu,
371 reg_wid=64)
372
373 ###### SETUP PHASE #######
374 # StateRunner.setup_for_test()
375
376 state_list = []
377
378 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
379 if self.run_hdl:
380 hdlrun = HDLRunner(self, m, pspec)
381 state_list.append(hdlrun)
382
383 if self.run_sim:
384 simrun = SimRunner(self, m, pspec)
385 state_list.append(simrun)
386
387 # run core clock at same rate as test clock
388 # XXX this has to stay here! TODO, work out why,
389 # but Simulation-only fails without it
390 intclk = ClockSignal("coresync")
391 comb += intclk.eq(ClockSignal())
392
393 # nmigen Simulation - everything runs around this, so it
394 # still has to be created.
395 sim = Simulator(m)
396 sim.add_clock(1e-6)
397
398 def process():
399
400 ###### PREPARATION PHASE AT START OF RUNNING #######
401 # StateRunner.setup_during_test()
402 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
403 # but "normalise" the APIs, make openpower-isa StateRunner
404 # dummy "yield" functions so if they're not provided at least
405 # there is a fallback which can be "yielded".
406
407 for runner in state_list:
408 yield from runner.setup_during_test()
409
410 # get each test, completely reset the core, and run it
411
412 for test in self.test_data:
413
414 with self.subTest(test.name):
415
416 ###### PREPARATION PHASE AT START OF TEST #######
417 # StateRunner.prepare_for_test()
418 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
419
420 for runner in state_list:
421 yield from runner.prepare_for_test(test)
422
423 print(test.name)
424 program = test.program
425 print("regs", test.regs)
426 print("sprs", test.sprs)
427 print("cr", test.cr)
428 print("mem", test.mem)
429 print("msr", test.msr)
430 print("assem", program.assembly)
431 gen = list(program.generate_instructions())
432 insncode = program.assembly.splitlines()
433 instructions = list(zip(gen, insncode))
434
435 ###### RUNNING OF EACH TEST #######
436 # StateRunner.step_test()
437
438 # Run two tests (TODO, move these to functions)
439 # * first the Simulator, collate a batch of results
440 # * then the HDL, likewise
441 # (actually, the other way round because running
442 # Simulator somehow modifies the test state!)
443 # * finally, compare all the results
444
445 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
446
447 ##########
448 # 1. HDL
449 ##########
450 if self.run_hdl:
451 hdl_states = yield from hdlrun.run_test(instructions)
452
453 ##########
454 # 2. Simulator
455 ##########
456
457 if self.run_sim:
458 sim_states = yield from simrun.run_test(
459 instructions, gen,
460 insncode)
461
462 ###### COMPARING THE TESTS #######
463
464 ###############
465 # 3. Compare
466 ###############
467
468 # TODO: here just grab one entry from list_of_sim_runners
469 # (doesn't matter which one, honestly)
470 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
471
472 if self.run_sim:
473 last_sim = copy(sim_states[-1])
474 elif self.run_hdl:
475 last_sim = copy(hdl_states[-1])
476 else:
477 last_sim = None # err what are you doing??
478
479 if self.run_hdl:
480 print ("hdl_states")
481 for state in hdl_states:
482 print (state)
483
484 if self.run_sim:
485 print ("sim_states")
486 for state in sim_states:
487 print (state)
488
489 if self.run_hdl and self.run_sim:
490 for simstate, hdlstate in zip(sim_states, hdl_states):
491 simstate.compare(hdlstate) # register check
492 simstate.compare_mem(hdlstate) # memory check
493
494 # compare against expected results
495 if test.expected is not None:
496 # have to put these in manually
497 test.expected.to_test = test.expected
498 test.expected.dut = self
499 test.expected.state_type = "expected"
500 test.expected.code = 0
501 # do actual comparison, against last item
502 last_sim.compare(test.expected)
503
504 if self.run_hdl and self.run_sim:
505 self.assertTrue(len(hdl_states) == len(sim_states),
506 "number of instructions run not the same")
507
508 ###### END OF A TEST #######
509 # StateRunner.end_test()
510 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
511
512 for runner in state_list:
513 yield from runner.end_test() # TODO, some arguments?
514
515 ###### END OF EVERYTHING (but none needs doing, still call fn) ####
516 # StateRunner.cleanup()
517 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
518
519 for runner in state_list:
520 yield from runner.cleanup() # TODO, some arguments?
521
522 styles = {
523 'dec': {'base': 'dec'},
524 'bin': {'base': 'bin'},
525 'closed': {'closed': True}
526 }
527
528 traces = [
529 'clk',
530 ('state machines', 'closed', [
531 'fetch_pc_i_valid', 'fetch_pc_o_ready',
532 'fetch_fsm_state',
533 'fetch_insn_o_valid', 'fetch_insn_i_ready',
534 'pred_insn_i_valid', 'pred_insn_o_ready',
535 'fetch_predicate_state',
536 'pred_mask_o_valid', 'pred_mask_i_ready',
537 'issue_fsm_state',
538 'exec_insn_i_valid', 'exec_insn_o_ready',
539 'exec_fsm_state',
540 'exec_pc_o_valid', 'exec_pc_i_ready',
541 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
542 'is_last', 'dec2.no_out_vec']),
543 {'comment': 'fetch and decode'},
544 (None, 'dec', [
545 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
546 'cur_pc[63:0]', 'core_core_cia[63:0]']),
547 'raw_insn_i[31:0]',
548 'raw_opcode_in[31:0]', 'insn_type', 'dec2.dec2_exc_happened',
549 ('svp64 decoding', 'closed', [
550 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
551 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
552 'dec2.sv_rm_dec.ptype_in',
553 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
554 'dstmask[63:0]', 'srcmask[63:0]',
555 'dregread[4:0]', 'dinvert',
556 'sregread[4:0]', 'sinvert',
557 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
558 'core.int.pred__ren']),
559 ('register augmentation', 'dec', 'closed', [
560 {'comment': 'v3.0b registers'},
561 'dec2.dec_o.RT[4:0]',
562 'dec2.dec_a.RA[4:0]',
563 'dec2.dec_b.RB[4:0]',
564 ('Rdest', [
565 'dec2.o_svdec.reg_in[4:0]',
566 ('dec2.o_svdec.spec[2:0]', 'bin'),
567 'dec2.o_svdec.reg_out[6:0]']),
568 ('Rsrc1', [
569 'dec2.in1_svdec.reg_in[4:0]',
570 ('dec2.in1_svdec.spec[2:0]', 'bin'),
571 'dec2.in1_svdec.reg_out[6:0]']),
572 ('Rsrc1', [
573 'dec2.in2_svdec.reg_in[4:0]',
574 ('dec2.in2_svdec.spec[2:0]', 'bin'),
575 'dec2.in2_svdec.reg_out[6:0]']),
576 {'comment': 'SVP64 registers'},
577 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
578 ]),
579 {'comment': 'svp64 context'},
580 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
581 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
582 'core_core_dststep[6:0]',
583 {'comment': 'issue and execute'},
584 'core.core_core_insn_type',
585 (None, 'dec', [
586 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
587 'issue_i', 'busy_o',
588 {'comment': 'dmi'},
589 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
590 {'comment': 'instruction memory'},
591 'imem.sram.rdport.memory(0)[63:0]',
592 {'comment': 'registers'},
593 # match with soc.regfile.regfiles.IntRegs port names
594 'core.int.rp_src1.memory(0)[63:0]',
595 'core.int.rp_src1.memory(1)[63:0]',
596 'core.int.rp_src1.memory(2)[63:0]',
597 'core.int.rp_src1.memory(3)[63:0]',
598 'core.int.rp_src1.memory(4)[63:0]',
599 'core.int.rp_src1.memory(5)[63:0]',
600 'core.int.rp_src1.memory(6)[63:0]',
601 'core.int.rp_src1.memory(7)[63:0]',
602 'core.int.rp_src1.memory(9)[63:0]',
603 'core.int.rp_src1.memory(10)[63:0]',
604 'core.int.rp_src1.memory(13)[63:0]'
605 ]
606
607 # PortInterface module path varies depending on MMU option
608 if self.microwatt_mmu:
609 pi_module = 'core.ldst0'
610 else:
611 pi_module = 'core.fus.ldst0'
612
613 traces += [('ld/st port interface', {'submodule': pi_module}, [
614 'oper_r__insn_type',
615 'ldst_port0_is_ld_i',
616 'ldst_port0_is_st_i',
617 'ldst_port0_busy_o',
618 'ldst_port0_addr_i[47:0]',
619 'ldst_port0_addr_i_ok',
620 'ldst_port0_addr_ok_o',
621 'ldst_port0_exc_happened',
622 'ldst_port0_st_data_i[63:0]',
623 'ldst_port0_st_data_i_ok',
624 'ldst_port0_ld_data_o[63:0]',
625 'ldst_port0_ld_data_o_ok',
626 'exc_o_happened',
627 'cancel'
628 ])]
629
630 if self.microwatt_mmu:
631 traces += [
632 {'comment': 'microwatt_mmu'},
633 'core.fus.mmu0.alu_mmu0.illegal',
634 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
635 'core.fus.mmu0.alu_mmu0.mmu.state',
636 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
637 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
638 {'comment': 'wishbone_memory'},
639 'core.fus.mmu0.alu_mmu0.dcache.stb',
640 'core.fus.mmu0.alu_mmu0.dcache.cyc',
641 'core.fus.mmu0.alu_mmu0.dcache.we',
642 'core.fus.mmu0.alu_mmu0.dcache.ack',
643 'core.fus.mmu0.alu_mmu0.dcache.stall,'
644 ]
645
646 write_gtkw("issuer_simulator.gtkw",
647 "issuer_simulator.vcd",
648 traces, styles, module='top.issuer')
649
650 # add run of instructions
651 sim.add_sync_process(process)
652
653 # optionally, if a wishbone-based ROM is passed in, run that as an
654 # extra emulated process
655 if self.rom is not None:
656 dcache = core.fus.fus["mmu0"].alu.dcache
657 default_mem = self.rom
658 sim.add_sync_process(wrap(wb_get(dcache, default_mem, "DCACHE")))
659
660 with sim.write_vcd("issuer_simulator.vcd"):
661 sim.run()