1 """TestRunner class, runs TestIssuer instructions
5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
6 * https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
8 from nmigen
import Module
, Signal
, Cat
, ClockSignal
9 from nmigen
.hdl
.xfrm
import ResetInserter
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
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
23 from openpower
.decoder
.power_decoder
import create_pdecode
24 from openpower
.decoder
.power_decoder2
import PowerDecode2
25 from soc
.regfile
.regfiles
import StateRegs
27 from soc
.simple
.issuer
import TestIssuerInternal
29 from soc
.config
.test
.test_loadstore
import TestMemPspec
30 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
, check_mem
,
33 from soc
.fu
.compunits
.test
.test_compunit
import (setup_tst_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
41 def setup_i_memory(imem
, startaddr
, instructions
):
43 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
45 for i
in range(mem
.depth
):
46 yield mem
._array
[i
].eq(0)
48 startaddr
//= 4 # instructions are 32-bit
51 for ins
in instructions
:
52 if isinstance(ins
, tuple):
56 insn
= insn
& 0xffffffff
57 yield mem
._array
[startaddr
].eq(insn
)
60 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
62 startaddr
= startaddr
& mask
67 for ins
in instructions
:
68 if isinstance(ins
, tuple):
72 insn
= insn
& 0xffffffff
73 msbs
= (startaddr
>> 1) & mask
74 val
= yield mem
._array
[msbs
]
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)))
81 yield mem
._array
[msbs
].eq(val
)
84 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
85 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
87 startaddr
= startaddr
& mask
90 def set_dmi(dmi
, addr
, data
):
92 yield dmi
.addr_i
.eq(addr
)
93 yield dmi
.din
.eq(data
)
101 yield dmi
.req_i
.eq(0)
102 yield dmi
.addr_i
.eq(0)
108 def get_dmi(dmi
, addr
):
109 yield dmi
.req_i
.eq(1)
110 yield dmi
.addr_i
.eq(addr
)
114 ack
= yield dmi
.ack_o
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)
127 def run_hdl_state(dut
, test
, issuer
, pc_i
, svstate_i
, instructions
):
128 """run_hdl_state - runs a TestIssuer nmigen HDL simulation
131 imem
= issuer
.imem
._get
_memory
()
134 pdecode2
= issuer
.pdecode2
138 # establish the TestIssuer context (mem, regs etc)
140 pc
= 0 # start address
141 counter
= 0 # test to pause/start
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
)
149 yield issuer
.pc_i
.ok
.eq(1)
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)
159 print("instructions", instructions
)
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
]
166 print("hdl instr: 0x{:X}".format(ins
& 0xffffffff))
172 yield from set_dmi(dmi
, DBGCore
.CTRL
,
174 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
175 yield issuer
.svstate_i
.ok
.eq(0) # ditto
179 counter
= counter
+ 1
181 # wait until executed
182 while not (yield issuer
.insn_done
):
187 index
= (yield issuer
.cur_state
.pc
) // 4
189 terminated
= yield issuer
.dbg
.terminated_o
190 print("terminated", terminated
)
192 if index
< len(instructions
):
193 # Get HDL mem and state
194 state
= yield from TestState("hdl", core
, dut
,
196 hdl_states
.append(state
)
198 if index
>= len(instructions
):
199 print ("index over, send dmi stop")
201 yield from set_dmi(dmi
, DBGCore
.CTRL
,
206 terminated
= yield issuer
.dbg
.terminated_o
207 print("terminated(2)", terminated
)
214 def run_sim_state(dut
, test
, simdec2
, instructions
, gen
, insncode
):
215 """run_sim_state - runs an ISACaller simulation
220 # set up the Simulator (which must track TestIssuer exactly)
221 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
223 initial_insns
=gen
, respect_pc
=True,
224 disassembly
=insncode
,
226 initial_svstate
=test
.svstate
)
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
]
233 print("sim instr: 0x{:X}".format(ins
& 0xffffffff))
236 # set up simulated instruction (in simdec2)
238 yield from sim
.setup_one()
239 except KeyError: # instruction not in imem: stop
243 # call simulated operation
245 yield from sim
.execute_one()
247 index
= sim
.pc
.CIA
.value
//4
249 # get sim register and memory TestState, add to list
250 state
= yield from TestState("sim", sim
, dut
, code
)
251 sim_states
.append(state
)
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
264 self
.run_hdl
= run_hdl
265 self
.run_sim
= run_sim
271 svstate_i
= Signal(64)
273 if self
.microwatt_mmu
:
274 ldst_ifacetype
= 'test_mmu_cache_wb'
276 ldst_ifacetype
= 'test_bare_wb'
277 imem_ifacetype
= 'test_bare_wb'
279 pspec
= TestMemPspec(ldst_ifacetype
=ldst_ifacetype
,
280 imem_ifacetype
=imem_ifacetype
,
291 mmu
=self
.microwatt_mmu
,
294 #hard_reset = Signal(reset_less=True)
295 issuer
= TestIssuerInternal(pspec
)
296 # use DMI RESET command instead, this does actually work though
297 #issuer = ResetInserter({'coresync': hard_reset,
298 # 'sync': hard_reset})(issuer)
299 m
.submodules
.issuer
= issuer
303 regreduce_en
= pspec
.regreduce_en
== True
304 simdec2
= PowerDecode2(None, regreduce_en
=regreduce_en
)
305 m
.submodules
.simdec2
= simdec2
# pain in the neck
307 # run core clock at same rate as test clock
308 intclk
= ClockSignal("coresync")
309 comb
+= intclk
.eq(ClockSignal())
312 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
313 comb
+= issuer
.svstate_i
.data
.eq(svstate_i
)
315 # nmigen Simulation - everything runs around this, so it
316 # still has to be created.
324 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
327 # get each test, completely reset the core, and run it
329 for test
in self
.test_data
:
332 # set up bigendian (TODO: don't do this, use MSR)
333 yield issuer
.core_bigendian_i
.eq(bigendian
)
342 program
= test
.program
343 with self
.subTest(test
.name
):
344 print("regs", test
.regs
)
345 print("sprs", test
.sprs
)
347 print("mem", test
.mem
)
348 print("msr", test
.msr
)
349 print("assem", program
.assembly
)
350 gen
= list(program
.generate_instructions())
351 insncode
= program
.assembly
.splitlines()
352 instructions
= list(zip(gen
, insncode
))
354 # Run two tests (TODO, move these to functions)
355 # * first the Simulator, collate a batch of results
356 # * then the HDL, likewise
357 # (actually, the other way round because running
358 # Simulator somehow modifies the test state!)
359 # * finally, compare all the results
365 hdl_states
= yield from run_hdl_state(self
, test
,
375 sim_states
= yield from run_sim_state(self
, test
,
385 last_sim
= copy(sim_states
[-1])
387 last_sim
= copy(hdl_states
[-1])
389 last_sim
= None # err what are you doing??
391 if self
.run_hdl
and self
.run_sim
:
392 for simstate
, hdlstate
in zip(sim_states
, hdl_states
):
393 simstate
.compare(hdlstate
) # register check
394 simstate
.compare_mem(hdlstate
) # memory check
398 for state
in hdl_states
:
403 for state
in sim_states
:
406 # compare against expected results
407 if test
.expected
is not None:
408 # have to put these in manually
409 test
.expected
.to_test
= test
.expected
410 test
.expected
.dut
= self
411 test
.expected
.state_type
= "expected"
412 test
.expected
.code
= 0
413 # do actual comparison, against last item
414 last_sim
.compare(test
.expected
)
416 if self
.run_hdl
and self
.run_sim
:
417 self
.assertTrue(len(hdl_states
) == len(sim_states
),
418 "number of instructions run not the same")
422 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
426 # TODO, here is where the static (expected) results
427 # can be checked: register check (TODO, memory check)
428 # see https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
429 # yield from check_regs(self, sim, core, test, code,
430 # >>>expected_data<<<)
433 cr
= yield from get_dmi(dmi
, DBGCore
.CR
)
434 print("after test %s cr value %x" % (test
.name
, cr
))
437 xer
= yield from get_dmi(dmi
, DBGCore
.XER
)
438 print("after test %s XER value %x" % (test
.name
, xer
))
440 # test of dmi reg get
441 for int_reg
in range(32):
442 yield from set_dmi(dmi
, DBGCore
.GSPR_IDX
, int_reg
)
443 value
= yield from get_dmi(dmi
, DBGCore
.GSPR_DATA
)
445 print("after test %s reg %2d value %x" %
446 (test
.name
, int_reg
, value
))
449 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.RESET
)
453 'dec': {'base': 'dec'},
454 'bin': {'base': 'bin'},
455 'closed': {'closed': True}
460 ('state machines', 'closed', [
461 'fetch_pc_i_valid', 'fetch_pc_o_ready',
463 'fetch_insn_o_valid', 'fetch_insn_i_ready',
464 'pred_insn_i_valid', 'pred_insn_o_ready',
465 'fetch_predicate_state',
466 'pred_mask_o_valid', 'pred_mask_i_ready',
468 'exec_insn_i_valid', 'exec_insn_o_ready',
470 'exec_pc_o_valid', 'exec_pc_i_ready',
471 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
472 'is_last', 'dec2.no_out_vec']),
473 {'comment': 'fetch and decode'},
475 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
476 'cur_pc[63:0]', 'core_core_cia[63:0]']),
478 'raw_opcode_in[31:0]', 'insn_type', 'dec2.dec2_exc_happened',
479 ('svp64 decoding', 'closed', [
480 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
481 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
482 'dec2.sv_rm_dec.ptype_in',
483 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
484 'dstmask[63:0]', 'srcmask[63:0]',
485 'dregread[4:0]', 'dinvert',
486 'sregread[4:0]', 'sinvert',
487 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
488 'core.int.pred__ren']),
489 ('register augmentation', 'dec', 'closed', [
490 {'comment': 'v3.0b registers'},
491 'dec2.dec_o.RT[4:0]',
492 'dec2.dec_a.RA[4:0]',
493 'dec2.dec_b.RB[4:0]',
495 'dec2.o_svdec.reg_in[4:0]',
496 ('dec2.o_svdec.spec[2:0]', 'bin'),
497 'dec2.o_svdec.reg_out[6:0]']),
499 'dec2.in1_svdec.reg_in[4:0]',
500 ('dec2.in1_svdec.spec[2:0]', 'bin'),
501 'dec2.in1_svdec.reg_out[6:0]']),
503 'dec2.in2_svdec.reg_in[4:0]',
504 ('dec2.in2_svdec.spec[2:0]', 'bin'),
505 'dec2.in2_svdec.reg_out[6:0]']),
506 {'comment': 'SVP64 registers'},
507 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
509 {'comment': 'svp64 context'},
510 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
511 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
512 'core_core_dststep[6:0]',
513 {'comment': 'issue and execute'},
514 'core.core_core_insn_type',
516 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
519 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
520 {'comment': 'instruction memory'},
521 'imem.sram.rdport.memory(0)[63:0]',
522 {'comment': 'registers'},
523 # match with soc.regfile.regfiles.IntRegs port names
524 'core.int.rp_src1.memory(0)[63:0]',
525 'core.int.rp_src1.memory(1)[63:0]',
526 'core.int.rp_src1.memory(2)[63:0]',
527 'core.int.rp_src1.memory(3)[63:0]',
528 'core.int.rp_src1.memory(4)[63:0]',
529 'core.int.rp_src1.memory(5)[63:0]',
530 'core.int.rp_src1.memory(6)[63:0]',
531 'core.int.rp_src1.memory(7)[63:0]',
532 'core.int.rp_src1.memory(9)[63:0]',
533 'core.int.rp_src1.memory(10)[63:0]',
534 'core.int.rp_src1.memory(13)[63:0]'
537 # PortInterface module path varies depending on MMU option
538 if self
.microwatt_mmu
:
539 pi_module
= 'core.ldst0'
541 pi_module
= 'core.fus.ldst0'
543 traces
+= [('ld/st port interface', {'submodule': pi_module
}, [
545 'ldst_port0_is_ld_i',
546 'ldst_port0_is_st_i',
548 'ldst_port0_addr_i[47:0]',
549 'ldst_port0_addr_i_ok',
550 'ldst_port0_addr_ok_o',
551 'ldst_port0_exc_happened',
552 'ldst_port0_st_data_i[63:0]',
553 'ldst_port0_st_data_i_ok',
554 'ldst_port0_ld_data_o[63:0]',
555 'ldst_port0_ld_data_o_ok',
560 if self
.microwatt_mmu
:
562 {'comment': 'microwatt_mmu'},
563 'core.fus.mmu0.alu_mmu0.illegal',
564 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
565 'core.fus.mmu0.alu_mmu0.mmu.state',
566 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
567 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
568 {'comment': 'wishbone_memory'},
569 'core.fus.mmu0.alu_mmu0.dcache.stb',
570 'core.fus.mmu0.alu_mmu0.dcache.cyc',
571 'core.fus.mmu0.alu_mmu0.dcache.we',
572 'core.fus.mmu0.alu_mmu0.dcache.ack',
573 'core.fus.mmu0.alu_mmu0.dcache.stall,'
576 write_gtkw("issuer_simulator.gtkw",
577 "issuer_simulator.vcd",
578 traces
, styles
, module
='top.issuer')
580 # add run of instructions
581 sim
.add_sync_process(process
)
583 # optionally, if a wishbone-based ROM is passed in, run that as an
584 # extra emulated process
585 if self
.rom
is not None:
586 dcache
= core
.fus
.fus
["mmu0"].alu
.dcache
587 default_mem
= self
.rom
588 sim
.add_sync_process(wrap(wb_get(dcache
, default_mem
, "DCACHE")))
590 with sim
.write_vcd("issuer_simulator.vcd"):