1 """TestRunner class, part of the Test API
5 Copyright (C) 2020,2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
6 Copyright (C) 2021 Kyle Lehman <klehman9@comcast.net>
7 Copyright (C) 2021 Jonathan Neuschäfer <j.neuschaefer@gmx.net>
8 Copyright (C) 2021 Tobias Platen <tplaten@posteo.de>
9 Copyright (C) 2021 Cesar Strauss <cestrauss@gmail.com>
13 * https://bugs.libre-soc.org/show_bug.cgi?id=363
14 * https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
17 from nmigen
import Module
, ClockSignal
19 from pprint
import pprint
21 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
22 # Also, check out the cxxsim nmigen branch, and latest yosys from git
23 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
25 from nmutil
.formaltest
import FHDLTestCase
26 from nmutil
.gtkw
import write_gtkw
27 from openpower
.decoder
.isa
.all
import ISA
28 from openpower
.endian
import bigendian
30 from openpower
.decoder
.power_decoder2
import PowerDecode2
32 from soc
.config
.test
.test_loadstore
import TestMemPspec
33 from nmutil
.util
import wrap
34 from openpower
.test
.wb_get
import wb_get
35 import openpower
.test
.wb_get
as wbget
36 from openpower
.test
.state
import TestState
, StateRunner
, ExpectedState
39 class SimRunner(StateRunner
):
40 """SimRunner: Implements methods for the setup, preparation, and
41 running of tests using ISACaller simulation
43 def __init__(self
, dut
, m
, pspec
):
44 super().__init
__("sim", SimRunner
)
47 self
.mmu
= pspec
.mmu
== True
48 regreduce_en
= pspec
.regreduce_en
== True
49 self
.simdec2
= simdec2
= PowerDecode2(None, regreduce_en
=regreduce_en
)
50 m
.submodules
.simdec2
= simdec2
# pain in the neck
52 def prepare_for_test(self
, test
):
57 def run_test(self
, instructions
, gen
, insncode
):
58 """run_sim_state - runs an ISACaller simulation
61 dut
, test
, simdec2
= self
.dut
, self
.test
, self
.simdec2
64 # set up the Simulator (which must track TestIssuer exactly)
65 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
67 initial_insns
=gen
, respect_pc
=True,
70 initial_svstate
=test
.svstate
,
73 # run the loop of the instructions on the current test
74 index
= sim
.pc
.CIA
.value
//4
75 while index
< len(instructions
):
76 ins
, code
= instructions
[index
]
78 print("sim instr: 0x{:X}".format(ins
& 0xffffffff))
81 # set up simulated instruction (in simdec2)
83 yield from sim
.setup_one()
84 except KeyError: # instruction not in imem: stop
88 # call simulated operation
90 yield from sim
.execute_one()
92 index
= sim
.pc
.CIA
.value
//4
94 # get sim register and memory TestState, add to list
95 state
= yield from TestState("sim", sim
, dut
, code
)
96 sim_states
.append(state
)
98 if self
.dut
.allow_overlap
:
99 # get last state, at end of run
100 state
= yield from TestState("sim", sim
, dut
, code
)
101 sim_states
.append(state
)
106 class TestRunnerBase(FHDLTestCase
):
107 """TestRunnerBase: Sets up and executes the running of tests
108 contained in tst_data. run_hdl (if provided) is an HDLRunner
109 object. If not provided, hdl simulation is skipped.
111 ISACaller simulation can be skipped by setting run_sim=False.
113 When using an Expected state to test with, the expected state
114 is passed in with tst_data.
116 def __init__(self
, tst_data
, microwatt_mmu
=False, rom
=None,
117 svp64
=True, run_hdl
=None, run_sim
=True,
118 allow_overlap
=False):
119 super().__init
__("run_all")
120 self
.test_data
= tst_data
121 self
.microwatt_mmu
= microwatt_mmu
124 self
.allow_overlap
= allow_overlap
125 self
.run_hdl
= run_hdl
126 self
.run_sim
= run_sim
131 if self
.microwatt_mmu
:
132 # do not wire these up to anything if wb_get is to be used
133 if self
.rom
is not None:
134 ldst_ifacetype
= 'mmu_cache_wb'
135 imem_ifacetype
= 'mmu_cache_wb'
137 ldst_ifacetype
= 'test_mmu_cache_wb'
138 imem_ifacetype
= 'test_bare_wb'
140 ldst_ifacetype
= 'test_bare_wb'
141 imem_ifacetype
= 'test_bare_wb'
143 pspec
= TestMemPspec(ldst_ifacetype
=ldst_ifacetype
,
144 imem_ifacetype
=imem_ifacetype
,
153 regreduce
=not self
.allow_overlap
,
154 core_domain
="sync", # no alternative domain
156 allow_overlap
=self
.allow_overlap
,
157 mmu
=self
.microwatt_mmu
,
160 ###### SETUP PHASE #######
161 # Determine the simulations needed and add to state_list
162 # for setup and running
163 # The methods contained in the respective Runner classes are
164 # called using this list when possible
166 # allow wb_get to run
167 if self
.rom
is not None:
173 hdlrun
= self
.run_hdl(self
, m
, pspec
)
174 state_list
.append(hdlrun
)
177 simrun
= SimRunner(self
, m
, pspec
)
178 state_list
.append(simrun
)
180 # run core clock at same rate as test clock
181 # XXX this has to stay here! TODO, work out why,
182 # but Simulation-only fails without it
183 intclk
= ClockSignal("coresync")
184 comb
+= intclk
.eq(ClockSignal())
185 dbgclk
= ClockSignal("dbgsync")
186 comb
+= dbgclk
.eq(ClockSignal())
188 # nmigen Simulation - everything runs around this, so it
189 # still has to be created.
195 ###### PREPARATION PHASE AT START OF RUNNING #######
197 for runner
in state_list
:
198 yield from runner
.setup_during_test()
200 # get each test, completely reset the core, and run it
202 for test
in self
.test_data
:
204 with self
.subTest(test
.name
):
206 ###### PREPARATION PHASE AT START OF TEST #######
208 for runner
in state_list
:
209 yield from runner
.prepare_for_test(test
)
212 program
= test
.program
213 print("regs", test
.regs
)
214 print("sprs", test
.sprs
)
216 print("mem", test
.mem
)
217 print("msr", test
.msr
)
218 print("assem", program
.assembly
)
219 gen
= list(program
.generate_instructions())
220 insncode
= program
.assembly
.splitlines()
221 instructions
= list(zip(gen
, insncode
))
223 ###### RUNNING OF EACH TEST #######
224 # StateRunner.step_test()
226 # Run two tests (TODO, move these to functions)
227 # * first the Simulator, collate a batch of results
228 # * then the HDL, likewise
229 # (actually, the other way round because running
230 # Simulator somehow modifies the test state!)
231 # * finally, compare all the results
233 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
239 hdl_states
= yield from hdlrun
.run_test(instructions
)
246 sim_states
= yield from simrun
.run_test(
250 ###### COMPARING THE TESTS #######
256 # TODO: here just grab one entry from list_of_sim_runners
257 # (doesn't matter which one, honestly)
258 # TODO https://bugs.libre-soc.org/show_bug.cgi?id=686#c73
261 last_sim
= copy(sim_states
[-1])
263 last_sim
= copy(hdl_states
[-1])
265 last_sim
= None # err what are you doing??
269 for state
in hdl_states
:
274 for state
in sim_states
:
278 if self
.run_hdl
and self
.run_sim
:
279 # if allow_overlap is enabled, because allow_overlap
280 # can commit out-of-order, only compare the last ones
281 if self
.allow_overlap
:
282 print ("allow_overlap: truncating %d %d "
283 "states to last" % (len(sim_states
),
285 sim_states
= sim_states
[-1:]
286 hdl_states
= hdl_states
[-1:]
287 sim_states
[-1].dump_state_tofile()
288 print ("allow_overlap: last hdl_state")
289 hdl_states
[-1].dump_state_tofile()
290 for simstate
, hdlstate
in zip(sim_states
, hdl_states
):
291 simstate
.compare(hdlstate
) # register check
292 simstate
.compare_mem(hdlstate
) # memory check
294 # if no expected, create /tmp/case_name.py with code
295 # setting expected state to last_sim
296 if test
.expected
is None:
297 last_sim
.dump_state_tofile(test
.name
, test
.test_file
)
299 # compare against expected results
300 if test
.expected
is not None:
301 # have to put these in manually
302 test
.expected
.to_test
= test
.expected
303 test
.expected
.dut
= self
304 test
.expected
.state_type
= "expected"
305 test
.expected
.code
= 0
306 # do actual comparison, against last item
307 last_sim
.compare(test
.expected
)
309 # check number of instructions run (sanity)
310 if self
.run_hdl
and self
.run_sim
:
311 n_hdl
= len(hdl_states
)
312 n_sim
= len(sim_states
)
313 self
.assertTrue(n_hdl
== n_sim
,
314 "number of instructions %d %d "
315 "run not the same" % (n_hdl
, n_sim
))
317 ###### END OF A TEST #######
318 # StateRunner.end_test()
320 for runner
in state_list
:
321 yield from runner
.end_test() # TODO, some arguments?
323 ###### END OF EVERYTHING (but none needs doing, still call fn) ####
324 # StateRunner.cleanup()
326 for runner
in state_list
:
327 yield from runner
.cleanup() # TODO, some arguments?
329 # finally stop wb_get from going
330 if self
.rom
is not None:
334 'dec': {'base': 'dec'},
335 'bin': {'base': 'bin'},
336 'closed': {'closed': True}
341 ('state machines', 'closed', [
342 'fetch_pc_i_valid', 'fetch_pc_o_ready',
344 'fetch_insn_o_valid', 'fetch_insn_i_ready',
345 'pred_insn_i_valid', 'pred_insn_o_ready',
346 'fetch_predicate_state',
347 'pred_mask_o_valid', 'pred_mask_i_ready',
349 'exec_insn_i_valid', 'exec_insn_o_ready',
351 'exec_pc_o_valid', 'exec_pc_i_ready',
352 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
353 'is_last', 'dec2.no_out_vec']),
354 {'comment': 'fetch and decode'},
356 'cia[63:0]', 'nia[63:0]', 'pc[63:0]', 'msr[63:0]',
357 'cur_pc[63:0]', 'core_core_cia[63:0]']),
359 'raw_opcode_in[31:0]', 'insn_type', 'dec2.dec2_exc_happened',
360 ('svp64 decoding', 'closed', [
361 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
362 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
363 'dec2.sv_rm_dec.ptype_in',
364 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
365 'dstmask[63:0]', 'srcmask[63:0]',
366 'dregread[4:0]', 'dinvert',
367 'sregread[4:0]', 'sinvert',
368 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
369 'core.int.pred__ren']),
370 ('register augmentation', 'dec', 'closed', [
371 {'comment': 'v3.0b registers'},
372 'dec2.dec_o.RT[4:0]',
373 'dec2.dec_a.RA[4:0]',
374 'dec2.dec_b.RB[4:0]',
376 'dec2.o_svdec.reg_in[4:0]',
377 ('dec2.o_svdec.spec[2:0]', 'bin'),
378 'dec2.o_svdec.reg_out[6:0]']),
380 'dec2.in1_svdec.reg_in[4:0]',
381 ('dec2.in1_svdec.spec[2:0]', 'bin'),
382 'dec2.in1_svdec.reg_out[6:0]']),
384 'dec2.in2_svdec.reg_in[4:0]',
385 ('dec2.in2_svdec.spec[2:0]', 'bin'),
386 'dec2.in2_svdec.reg_out[6:0]']),
387 {'comment': 'SVP64 registers'},
388 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
390 {'comment': 'svp64 context'},
391 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
392 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
393 'core_core_dststep[6:0]',
394 {'comment': 'issue and execute'},
395 'core.core_core_insn_type',
397 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
400 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
401 {'comment': 'instruction memory'},
402 'imem.sram.rdport.memory(0)[63:0]',
403 {'comment': 'registers'},
404 # match with soc.regfile.regfiles.IntRegs port names
405 'core.int.rp_src1.memory(0)[63:0]',
406 'core.int.rp_src1.memory(1)[63:0]',
407 'core.int.rp_src1.memory(2)[63:0]',
408 'core.int.rp_src1.memory(3)[63:0]',
409 'core.int.rp_src1.memory(4)[63:0]',
410 'core.int.rp_src1.memory(5)[63:0]',
411 'core.int.rp_src1.memory(6)[63:0]',
412 'core.int.rp_src1.memory(7)[63:0]',
413 'core.int.rp_src1.memory(9)[63:0]',
414 'core.int.rp_src1.memory(10)[63:0]',
415 'core.int.rp_src1.memory(13)[63:0]',
416 # Exceptions: see list archive for description of the chain
417 # http://lists.libre-soc.org/pipermail/libre-soc-dev/2021-December/004220.html
418 ('exceptions', 'closed', [
420 'pdecode2.exc_happened',
422 'core.fus.ldst0.exc_o_happened']),
425 # PortInterface module path varies depending on MMU option
426 if self
.microwatt_mmu
:
427 pi_module
= 'core.ldst0'
429 pi_module
= 'core.fus.ldst0'
431 traces
+= [('ld/st port interface', {'submodule': pi_module
}, [
434 'ldst_port0_is_ld_i',
435 'ldst_port0_is_st_i',
437 'ldst_port0_addr_i[47:0]',
438 'ldst_port0_addr_i_ok',
439 'ldst_port0_addr_ok_o',
440 'ldst_port0_exc_happened',
441 'ldst_port0_st_data_i[63:0]',
442 'ldst_port0_st_data_i_ok',
443 'ldst_port0_ld_data_o[63:0]',
444 'ldst_port0_ld_data_o_ok',
450 if self
.microwatt_mmu
:
452 {'comment': 'microwatt_mmu'},
453 'core.fus.mmu0.alu_mmu0.illegal',
454 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
455 'core.fus.mmu0.alu_mmu0.mmu.state',
456 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
457 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
458 {'comment': 'wishbone_memory'},
459 'core.l0.pimem.bus__ack',
460 'core.l0.pimem.bus__adr[4:0]',
461 'core.l0.pimem.bus__bte',
462 'core.l0.pimem.bus__cti',
463 'core.l0.pimem.bus__cyc',
464 'core.l0.pimem.bus__dat_r[63:0]',
465 'core.l0.pimem.bus__dat_w[63:0]',
466 'core.l0.pimem.bus__dat_err',
467 'core.l0.pimem.bus__dat_sel[7:0]',
468 'core.l0.pimem.bus__dat_stb',
469 'core.l0.pimem.bus__dat_we',
472 write_gtkw("issuer_simulator.gtkw",
473 "issuer_simulator.vcd",
474 traces
, styles
, module
='top.issuer')
476 # add run of instructions
477 sim
.add_sync_process(process
)
479 # ARGH oh whoops. TODO, core is not passed in!
480 # urrr... work out a hacky-way to sort this (access run_hdl
481 # core directly for now)
483 # optionally, if a wishbone-based ROM is passed in, run that as an
484 # extra emulated process
485 if self
.rom
is not None:
486 print ("TestRunner with MMU ROM")
488 dcache
= hdlrun
.issuer
.core
.fus
.fus
["mmu0"].alu
.dcache
489 icache
= hdlrun
.issuer
.core
.fus
.fus
["mmu0"].alu
.icache
490 default_mem
= self
.rom
491 sim
.add_sync_process(wrap(wb_get(dcache
.bus
,
492 default_mem
, "DCACHE")))
493 sim
.add_sync_process(wrap(wb_get(icache
.ibus
,
494 default_mem
, "ICACHE")))
496 with sim
.write_vcd("issuer_simulator.vcd"):