move pc_i and svstate_i inside if self.run_hdl
[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
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 def run_hdl_state(dut, test, issuer, pc_i, svstate_i, instructions):
128 """run_hdl_state - runs a TestIssuer nmigen HDL simulation
129 """
130
131 imem = issuer.imem._get_memory()
132 core = issuer.core
133 dmi = issuer.dbg.dmi
134 pdecode2 = issuer.pdecode2
135 l0 = core.l0
136 hdl_states = []
137
138 # establish the TestIssuer context (mem, regs etc)
139
140 pc = 0 # start address
141 counter = 0 # test to pause/start
142
143 yield from setup_i_memory(imem, pc, instructions)
144 yield from setup_tst_memory(l0, test.mem)
145 yield from setup_regs(pdecode2, core, test)
146
147 # set PC and SVSTATE
148 yield pc_i.eq(pc)
149 yield issuer.pc_i.ok.eq(1)
150
151 # copy initial SVSTATE
152 initial_svstate = copy(test.svstate)
153 if isinstance(initial_svstate, int):
154 initial_svstate = SVP64State(initial_svstate)
155 yield svstate_i.eq(initial_svstate.value)
156 yield issuer.svstate_i.ok.eq(1)
157 yield
158
159 print("instructions", instructions)
160
161 # run the loop of the instructions on the current test
162 index = (yield issuer.cur_state.pc) // 4
163 while index < len(instructions):
164 ins, code = instructions[index]
165
166 print("hdl instr: 0x{:X}".format(ins & 0xffffffff))
167 print(index, code)
168
169 if counter == 0:
170 # start the core
171 yield
172 yield from set_dmi(dmi, DBGCore.CTRL,
173 1<<DBGCtrl.START)
174 yield issuer.pc_i.ok.eq(0) # no change PC after this
175 yield issuer.svstate_i.ok.eq(0) # ditto
176 yield
177 yield
178
179 counter = counter + 1
180
181 # wait until executed
182 while not (yield issuer.insn_done):
183 yield
184
185 yield Settle()
186
187 index = (yield issuer.cur_state.pc) // 4
188
189 terminated = yield issuer.dbg.terminated_o
190 print("terminated", terminated)
191
192 if index < len(instructions):
193 # Get HDL mem and state
194 state = yield from TestState("hdl", core, dut,
195 code)
196 hdl_states.append(state)
197
198 if index >= len(instructions):
199 print ("index over, send dmi stop")
200 # stop at end
201 yield from set_dmi(dmi, DBGCore.CTRL,
202 1<<DBGCtrl.STOP)
203 yield
204 yield
205
206 terminated = yield issuer.dbg.terminated_o
207 print("terminated(2)", terminated)
208 if terminated:
209 break
210
211 return hdl_states
212
213
214 def run_sim_state(dut, test, simdec2, instructions, gen, insncode):
215 """run_sim_state - runs an ISACaller simulation
216 """
217
218 sim_states = []
219
220 # set up the Simulator (which must track TestIssuer exactly)
221 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
222 test.msr,
223 initial_insns=gen, respect_pc=True,
224 disassembly=insncode,
225 bigendian=bigendian,
226 initial_svstate=test.svstate)
227
228 # run the loop of the instructions on the current test
229 index = sim.pc.CIA.value//4
230 while index < len(instructions):
231 ins, code = instructions[index]
232
233 print("sim instr: 0x{:X}".format(ins & 0xffffffff))
234 print(index, code)
235
236 # set up simulated instruction (in simdec2)
237 try:
238 yield from sim.setup_one()
239 except KeyError: # instruction not in imem: stop
240 break
241 yield Settle()
242
243 # call simulated operation
244 print("sim", code)
245 yield from sim.execute_one()
246 yield Settle()
247 index = sim.pc.CIA.value//4
248
249 # get sim register and memory TestState, add to list
250 state = yield from TestState("sim", sim, dut, code)
251 sim_states.append(state)
252
253 return sim_states
254
255
256 class TestRunner(FHDLTestCase):
257 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
258 svp64=True, run_hdl=True, run_sim=True):
259 super().__init__("run_all")
260 self.test_data = tst_data
261 self.microwatt_mmu = microwatt_mmu
262 self.rom = rom
263 self.svp64 = svp64
264 self.run_hdl = run_hdl
265 self.run_sim = run_sim
266
267 def run_all(self):
268 m = Module()
269 comb = m.d.comb
270 if self.microwatt_mmu:
271 ldst_ifacetype = 'test_mmu_cache_wb'
272 else:
273 ldst_ifacetype = 'test_bare_wb'
274 imem_ifacetype = 'test_bare_wb'
275
276 pspec = TestMemPspec(ldst_ifacetype=ldst_ifacetype,
277 imem_ifacetype=imem_ifacetype,
278 addr_wid=48,
279 mask_wid=8,
280 imem_reg_wid=64,
281 # wb_data_width=32,
282 use_pll=False,
283 nocore=False,
284 xics=False,
285 gpio=False,
286 regreduce=True,
287 svp64=self.svp64,
288 mmu=self.microwatt_mmu,
289 reg_wid=64)
290
291 ###### SETUP PHASE #######
292 # StateRunner.setup_for_test()
293
294 if self.run_hdl:
295
296 #hard_reset = Signal(reset_less=True)
297 issuer = TestIssuerInternal(pspec)
298 # use DMI RESET command instead, this does actually work though
299 #issuer = ResetInserter({'coresync': hard_reset,
300 # 'sync': hard_reset})(issuer)
301 m.submodules.issuer = issuer
302 dmi = issuer.dbg.dmi
303
304 if self.run_sim:
305 regreduce_en = pspec.regreduce_en == True
306 simdec2 = PowerDecode2(None, regreduce_en=regreduce_en)
307 m.submodules.simdec2 = simdec2 # pain in the neck
308
309 # run core clock at same rate as test clock
310 intclk = ClockSignal("coresync")
311 comb += intclk.eq(ClockSignal())
312
313 if self.run_hdl:
314 pc_i = Signal(32)
315 svstate_i = Signal(64)
316
317 comb += issuer.pc_i.data.eq(pc_i)
318 comb += issuer.svstate_i.data.eq(svstate_i)
319
320 # nmigen Simulation - everything runs around this, so it
321 # still has to be created.
322 sim = Simulator(m)
323 sim.add_clock(1e-6)
324
325 def process():
326
327 ###### PREPARATION PHASE AT START OF RUNNING #######
328 # StateRunner.setup_during_test()
329
330 if self.run_hdl:
331 # start in stopped
332 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
333 yield
334
335 # get each test, completely reset the core, and run it
336
337 for test in self.test_data:
338
339 with self.subTest(test.name):
340
341 ###### PREPARATION PHASE AT START OF TEST #######
342 # StateRunner.prepare_for_test()
343
344 if self.run_hdl:
345 # set up bigendian (TODO: don't do this, use MSR)
346 yield issuer.core_bigendian_i.eq(bigendian)
347 yield Settle()
348
349 yield
350 yield
351 yield
352 yield
353
354 print(test.name)
355 program = test.program
356 print("regs", test.regs)
357 print("sprs", test.sprs)
358 print("cr", test.cr)
359 print("mem", test.mem)
360 print("msr", test.msr)
361 print("assem", program.assembly)
362 gen = list(program.generate_instructions())
363 insncode = program.assembly.splitlines()
364 instructions = list(zip(gen, insncode))
365
366 ###### RUNNING OF EACH TEST #######
367 # StateRunner.step_test()
368
369 # Run two tests (TODO, move these to functions)
370 # * first the Simulator, collate a batch of results
371 # * then the HDL, likewise
372 # (actually, the other way round because running
373 # Simulator somehow modifies the test state!)
374 # * finally, compare all the results
375
376 ##########
377 # 1. HDL
378 ##########
379 if self.run_hdl:
380 hdl_states = yield from run_hdl_state(self, test,
381 issuer,
382 pc_i, svstate_i,
383 instructions)
384
385 ##########
386 # 2. Simulator
387 ##########
388
389 if self.run_sim:
390 sim_states = yield from run_sim_state(self, test,
391 simdec2,
392 instructions, gen,
393 insncode)
394
395 ###### COMPARING THE TESTS #######
396
397 ###############
398 # 3. Compare
399 ###############
400
401 if self.run_sim:
402 last_sim = copy(sim_states[-1])
403 elif self.run_hdl:
404 last_sim = copy(hdl_states[-1])
405 else:
406 last_sim = None # err what are you doing??
407
408 if self.run_hdl and self.run_sim:
409 for simstate, hdlstate in zip(sim_states, hdl_states):
410 simstate.compare(hdlstate) # register check
411 simstate.compare_mem(hdlstate) # memory check
412
413 if self.run_hdl:
414 print ("hdl_states")
415 for state in hdl_states:
416 print (state)
417
418 if self.run_sim:
419 print ("sim_states")
420 for state in sim_states:
421 print (state)
422
423 # compare against expected results
424 if test.expected is not None:
425 # have to put these in manually
426 test.expected.to_test = test.expected
427 test.expected.dut = self
428 test.expected.state_type = "expected"
429 test.expected.code = 0
430 # do actual comparison, against last item
431 last_sim.compare(test.expected)
432
433 if self.run_hdl and self.run_sim:
434 self.assertTrue(len(hdl_states) == len(sim_states),
435 "number of instructions run not the same")
436
437 ###### END OF A TEST #######
438 # StateRunner.end_test()
439
440 if self.run_hdl:
441 # stop at end
442 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
443 yield
444 yield
445
446 # TODO, here is where the static (expected) results
447 # can be checked: register check (TODO, memory check)
448 # see https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
449 # yield from check_regs(self, sim, core, test, code,
450 # >>>expected_data<<<)
451
452 # get CR
453 cr = yield from get_dmi(dmi, DBGCore.CR)
454 print("after test %s cr value %x" % (test.name, cr))
455
456 # get XER
457 xer = yield from get_dmi(dmi, DBGCore.XER)
458 print("after test %s XER value %x" % (test.name, xer))
459
460 # test of dmi reg get
461 for int_reg in range(32):
462 yield from set_dmi(dmi, DBGCore.GSPR_IDX, int_reg)
463 value = yield from get_dmi(dmi, DBGCore.GSPR_DATA)
464
465 print("after test %s reg %2d value %x" %
466 (test.name, int_reg, value))
467
468 # pull a reset
469 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
470 yield
471
472 ###### END OF EVERYTHING (but none needs doing, still call fn) #######
473 # StateRunner.cleanup()
474
475 styles = {
476 'dec': {'base': 'dec'},
477 'bin': {'base': 'bin'},
478 'closed': {'closed': True}
479 }
480
481 traces = [
482 'clk',
483 ('state machines', 'closed', [
484 'fetch_pc_i_valid', 'fetch_pc_o_ready',
485 'fetch_fsm_state',
486 'fetch_insn_o_valid', 'fetch_insn_i_ready',
487 'pred_insn_i_valid', 'pred_insn_o_ready',
488 'fetch_predicate_state',
489 'pred_mask_o_valid', 'pred_mask_i_ready',
490 'issue_fsm_state',
491 'exec_insn_i_valid', 'exec_insn_o_ready',
492 'exec_fsm_state',
493 'exec_pc_o_valid', 'exec_pc_i_ready',
494 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
495 'is_last', 'dec2.no_out_vec']),
496 {'comment': 'fetch and decode'},
497 (None, 'dec', [
498 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
499 'cur_pc[63:0]', 'core_core_cia[63:0]']),
500 'raw_insn_i[31:0]',
501 'raw_opcode_in[31:0]', 'insn_type', 'dec2.dec2_exc_happened',
502 ('svp64 decoding', 'closed', [
503 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
504 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
505 'dec2.sv_rm_dec.ptype_in',
506 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
507 'dstmask[63:0]', 'srcmask[63:0]',
508 'dregread[4:0]', 'dinvert',
509 'sregread[4:0]', 'sinvert',
510 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
511 'core.int.pred__ren']),
512 ('register augmentation', 'dec', 'closed', [
513 {'comment': 'v3.0b registers'},
514 'dec2.dec_o.RT[4:0]',
515 'dec2.dec_a.RA[4:0]',
516 'dec2.dec_b.RB[4:0]',
517 ('Rdest', [
518 'dec2.o_svdec.reg_in[4:0]',
519 ('dec2.o_svdec.spec[2:0]', 'bin'),
520 'dec2.o_svdec.reg_out[6:0]']),
521 ('Rsrc1', [
522 'dec2.in1_svdec.reg_in[4:0]',
523 ('dec2.in1_svdec.spec[2:0]', 'bin'),
524 'dec2.in1_svdec.reg_out[6:0]']),
525 ('Rsrc1', [
526 'dec2.in2_svdec.reg_in[4:0]',
527 ('dec2.in2_svdec.spec[2:0]', 'bin'),
528 'dec2.in2_svdec.reg_out[6:0]']),
529 {'comment': 'SVP64 registers'},
530 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
531 ]),
532 {'comment': 'svp64 context'},
533 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
534 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
535 'core_core_dststep[6:0]',
536 {'comment': 'issue and execute'},
537 'core.core_core_insn_type',
538 (None, 'dec', [
539 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
540 'issue_i', 'busy_o',
541 {'comment': 'dmi'},
542 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
543 {'comment': 'instruction memory'},
544 'imem.sram.rdport.memory(0)[63:0]',
545 {'comment': 'registers'},
546 # match with soc.regfile.regfiles.IntRegs port names
547 'core.int.rp_src1.memory(0)[63:0]',
548 'core.int.rp_src1.memory(1)[63:0]',
549 'core.int.rp_src1.memory(2)[63:0]',
550 'core.int.rp_src1.memory(3)[63:0]',
551 'core.int.rp_src1.memory(4)[63:0]',
552 'core.int.rp_src1.memory(5)[63:0]',
553 'core.int.rp_src1.memory(6)[63:0]',
554 'core.int.rp_src1.memory(7)[63:0]',
555 'core.int.rp_src1.memory(9)[63:0]',
556 'core.int.rp_src1.memory(10)[63:0]',
557 'core.int.rp_src1.memory(13)[63:0]'
558 ]
559
560 # PortInterface module path varies depending on MMU option
561 if self.microwatt_mmu:
562 pi_module = 'core.ldst0'
563 else:
564 pi_module = 'core.fus.ldst0'
565
566 traces += [('ld/st port interface', {'submodule': pi_module}, [
567 'oper_r__insn_type',
568 'ldst_port0_is_ld_i',
569 'ldst_port0_is_st_i',
570 'ldst_port0_busy_o',
571 'ldst_port0_addr_i[47:0]',
572 'ldst_port0_addr_i_ok',
573 'ldst_port0_addr_ok_o',
574 'ldst_port0_exc_happened',
575 'ldst_port0_st_data_i[63:0]',
576 'ldst_port0_st_data_i_ok',
577 'ldst_port0_ld_data_o[63:0]',
578 'ldst_port0_ld_data_o_ok',
579 'exc_o_happened',
580 'cancel'
581 ])]
582
583 if self.microwatt_mmu:
584 traces += [
585 {'comment': 'microwatt_mmu'},
586 'core.fus.mmu0.alu_mmu0.illegal',
587 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
588 'core.fus.mmu0.alu_mmu0.mmu.state',
589 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
590 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
591 {'comment': 'wishbone_memory'},
592 'core.fus.mmu0.alu_mmu0.dcache.stb',
593 'core.fus.mmu0.alu_mmu0.dcache.cyc',
594 'core.fus.mmu0.alu_mmu0.dcache.we',
595 'core.fus.mmu0.alu_mmu0.dcache.ack',
596 'core.fus.mmu0.alu_mmu0.dcache.stall,'
597 ]
598
599 write_gtkw("issuer_simulator.gtkw",
600 "issuer_simulator.vcd",
601 traces, styles, module='top.issuer')
602
603 # add run of instructions
604 sim.add_sync_process(process)
605
606 # optionally, if a wishbone-based ROM is passed in, run that as an
607 # extra emulated process
608 if self.rom is not None:
609 dcache = core.fus.fus["mmu0"].alu.dcache
610 default_mem = self.rom
611 sim.add_sync_process(wrap(wb_get(dcache, default_mem, "DCACHE")))
612
613 with sim.write_vcd("issuer_simulator.vcd"):
614 sim.run()