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
11 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
12 # Also, check out the cxxsim nmigen branch, and latest yosys from git
13 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
15 from nmutil
.formaltest
import FHDLTestCase
16 from nmutil
.gtkw
import write_gtkw
17 from nmigen
.cli
import rtlil
18 from openpower
.decoder
.isa
.caller
import special_sprs
, SVP64State
19 from openpower
.decoder
.isa
.all
import ISA
20 from openpower
.endian
import bigendian
22 from openpower
.decoder
.power_decoder
import create_pdecode
23 from openpower
.decoder
.power_decoder2
import PowerDecode2
24 from soc
.regfile
.regfiles
import StateRegs
26 from soc
.simple
.issuer
import TestIssuerInternal
28 from soc
.config
.test
.test_loadstore
import TestMemPspec
29 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
, check_mem
,
32 from soc
.fu
.compunits
.test
.test_compunit
import (setup_tst_memory
,
34 from soc
.debug
.dmi
import DBGCore
, DBGCtrl
, DBGStat
35 from nmutil
.util
import wrap
36 from soc
.experiment
.test
.test_mmu_dcache
import wb_get
39 def setup_i_memory(imem
, startaddr
, instructions
):
41 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
43 for i
in range(mem
.depth
):
44 yield mem
._array
[i
].eq(0)
46 startaddr
//= 4 # instructions are 32-bit
49 for ins
in instructions
:
50 if isinstance(ins
, tuple):
54 insn
= insn
& 0xffffffff
55 yield mem
._array
[startaddr
].eq(insn
)
58 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
60 startaddr
= startaddr
& mask
65 for ins
in instructions
:
66 if isinstance(ins
, tuple):
70 insn
= insn
& 0xffffffff
71 msbs
= (startaddr
>> 1) & mask
72 val
= yield mem
._array
[msbs
]
74 print("before set", hex(4*startaddr
),
75 hex(msbs
), hex(val
), hex(insn
))
76 lsb
= 1 if (startaddr
& 1) else 0
77 val
= (val |
(insn
<< (lsb
*32)))
79 yield mem
._array
[msbs
].eq(val
)
82 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
83 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
85 startaddr
= startaddr
& mask
88 def set_dmi(dmi
, addr
, data
):
90 yield dmi
.addr_i
.eq(addr
)
91 yield dmi
.din
.eq(data
)
100 yield dmi
.addr_i
.eq(0)
106 def get_dmi(dmi
, addr
):
107 yield dmi
.req_i
.eq(1)
108 yield dmi
.addr_i
.eq(addr
)
112 ack
= yield dmi
.ack_o
117 data
= yield dmi
.dout
# get data after ack valid for 1 cycle
118 yield dmi
.req_i
.eq(0)
119 yield dmi
.addr_i
.eq(0)
125 class TestRunner(FHDLTestCase
):
126 def __init__(self
, tst_data
, microwatt_mmu
=False, rom
=None,
128 super().__init
__("run_all")
129 self
.test_data
= tst_data
130 self
.microwatt_mmu
= microwatt_mmu
138 svstate_i
= Signal(64)
140 if self
.microwatt_mmu
:
141 ldst_ifacetype
= 'test_mmu_cache_wb'
143 ldst_ifacetype
= 'test_bare_wb'
144 imem_ifacetype
= 'test_bare_wb'
146 pspec
= TestMemPspec(ldst_ifacetype
=ldst_ifacetype
,
147 imem_ifacetype
=imem_ifacetype
,
158 mmu
=self
.microwatt_mmu
,
160 #hard_reset = Signal(reset_less=True)
161 issuer
= TestIssuerInternal(pspec
)
162 # use DMI RESET command instead, this does actually work though
163 #issuer = ResetInserter({'coresync': hard_reset,
164 # 'sync': hard_reset})(issuer)
165 m
.submodules
.issuer
= issuer
166 imem
= issuer
.imem
._get
_memory
()
169 pdecode2
= issuer
.pdecode2
171 regreduce_en
= pspec
.regreduce_en
== True
173 #simdec = create_pdecode()
174 simdec2
= PowerDecode2(None, regreduce_en
=regreduce_en
)
175 m
.submodules
.simdec2
= simdec2
# pain in the neck
177 # run core clock at same rate as test clock
178 intclk
= ClockSignal("coresync")
179 comb
+= intclk
.eq(ClockSignal())
181 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
182 comb
+= issuer
.svstate_i
.data
.eq(svstate_i
)
191 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
194 # get each test, completely reset the core, and run it
196 for test
in self
.test_data
:
198 # set up bigendian (TODO: don't do this, use MSR)
199 yield issuer
.core_bigendian_i
.eq(bigendian
)
208 program
= test
.program
209 with self
.subTest(test
.name
):
210 print("regs", test
.regs
)
211 print("sprs", test
.sprs
)
213 print("mem", test
.mem
)
214 print("msr", test
.msr
)
215 print("assem", program
.assembly
)
216 gen
= list(program
.generate_instructions())
217 insncode
= program
.assembly
.splitlines()
218 instructions
= list(zip(gen
, insncode
))
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 # establish the TestIssuer context (mem, regs etc)
230 pc
= 0 # start address
231 counter
= 0 # test to pause/start
233 yield from setup_i_memory(imem
, pc
, instructions
)
234 yield from setup_tst_memory(l0
, test
.mem
)
235 yield from setup_regs(pdecode2
, core
, test
)
239 yield issuer
.pc_i
.ok
.eq(1)
241 initial_svstate
= test
.svstate
242 if isinstance(initial_svstate
, int):
243 initial_svstate
= SVP64State(initial_svstate
)
244 yield svstate_i
.eq(initial_svstate
.value
)
245 yield issuer
.svstate_i
.ok
.eq(1)
248 print("instructions", instructions
)
250 # run the loop of the instructions on the current test
251 index
= sim
.pc
.CIA
.value
//4
252 while index
< len(instructions
):
253 ins
, code
= instructions
[index
]
255 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
261 yield from set_dmi(dmi
, DBGCore
.CTRL
,
263 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
264 yield issuer
.svstate_i
.ok
.eq(0) # ditto
268 counter
= counter
+ 1
270 # wait until executed
271 while not (yield issuer
.insn_done
):
274 # set up simulated instruction (in simdec2)
276 yield from sim
.setup_one()
277 except KeyError: # instruction not in imem: stop
281 # call simulated operation
283 yield from sim
.execute_one()
285 index
= sim
.pc
.CIA
.value
//4
287 terminated
= yield issuer
.dbg
.terminated_o
288 print("terminated", terminated
)
290 if index
>= len(instructions
):
291 print ("index over, send dmi stop")
293 yield from set_dmi(dmi
, DBGCore
.CTRL
,
299 yield from check_regs(self
, sim
, core
, test
, code
)
302 yield from check_mem(self
, sim
, core
, test
, code
)
304 terminated
= yield issuer
.dbg
.terminated_o
305 print("terminated(2)", terminated
)
310 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
314 # TODO, here is where the static (expected) results
315 # can be checked: register check (TODO, memory check)
316 # see https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
317 # yield from check_regs(self, sim, core, test, code,
318 # >>>expected_data<<<)
321 cr
= yield from get_dmi(dmi
, DBGCore
.CR
)
322 print("after test %s cr value %x" % (test
.name
, cr
))
325 xer
= yield from get_dmi(dmi
, DBGCore
.XER
)
326 print("after test %s XER value %x" % (test
.name
, xer
))
328 # test of dmi reg get
329 for int_reg
in range(32):
330 yield from set_dmi(dmi
, DBGCore
.GSPR_IDX
, int_reg
)
331 value
= yield from get_dmi(dmi
, DBGCore
.GSPR_DATA
)
333 print("after test %s reg %2d value %x" %
334 (test
.name
, int_reg
, value
))
337 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.RESET
)
341 'dec': {'base': 'dec'},
342 'bin': {'base': 'bin'},
343 'closed': {'closed': True}
348 ('state machines', 'closed', [
349 'fetch_pc_i_valid', 'fetch_pc_o_ready',
351 'fetch_insn_o_valid', 'fetch_insn_i_ready',
352 'pred_insn_i_valid', 'pred_insn_o_ready',
353 'fetch_predicate_state',
354 'pred_mask_o_valid', 'pred_mask_i_ready',
356 'exec_insn_i_valid', 'exec_insn_o_ready',
358 'exec_pc_o_valid', 'exec_pc_i_ready',
359 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
360 'is_last', 'dec2.no_out_vec']),
361 {'comment': 'fetch and decode'},
363 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
364 'cur_pc[63:0]', 'core_core_cia[63:0]']),
366 'raw_opcode_in[31:0]', 'insn_type', 'dec2.dec2_exc_happened',
367 ('svp64 decoding', 'closed', [
368 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
369 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
370 'dec2.sv_rm_dec.ptype_in',
371 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
372 'dstmask[63:0]', 'srcmask[63:0]',
373 'dregread[4:0]', 'dinvert',
374 'sregread[4:0]', 'sinvert',
375 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
376 'core.int.pred__ren']),
377 ('register augmentation', 'dec', 'closed', [
378 {'comment': 'v3.0b registers'},
379 'dec2.dec_o.RT[4:0]',
380 'dec2.dec_a.RA[4:0]',
381 'dec2.dec_b.RB[4:0]',
383 'dec2.o_svdec.reg_in[4:0]',
384 ('dec2.o_svdec.spec[2:0]', 'bin'),
385 'dec2.o_svdec.reg_out[6:0]']),
387 'dec2.in1_svdec.reg_in[4:0]',
388 ('dec2.in1_svdec.spec[2:0]', 'bin'),
389 'dec2.in1_svdec.reg_out[6:0]']),
391 'dec2.in2_svdec.reg_in[4:0]',
392 ('dec2.in2_svdec.spec[2:0]', 'bin'),
393 'dec2.in2_svdec.reg_out[6:0]']),
394 {'comment': 'SVP64 registers'},
395 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
397 {'comment': 'svp64 context'},
398 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
399 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
400 'core_core_dststep[6:0]',
401 {'comment': 'issue and execute'},
402 'core.core_core_insn_type',
404 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
407 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
408 {'comment': 'instruction memory'},
409 'imem.sram.rdport.memory(0)[63:0]',
410 {'comment': 'registers'},
411 # match with soc.regfile.regfiles.IntRegs port names
412 'core.int.rp_src1.memory(0)[63:0]',
413 'core.int.rp_src1.memory(1)[63:0]',
414 'core.int.rp_src1.memory(2)[63:0]',
415 'core.int.rp_src1.memory(3)[63:0]',
416 'core.int.rp_src1.memory(4)[63:0]',
417 'core.int.rp_src1.memory(5)[63:0]',
418 'core.int.rp_src1.memory(6)[63:0]',
419 'core.int.rp_src1.memory(7)[63:0]',
420 'core.int.rp_src1.memory(9)[63:0]',
421 'core.int.rp_src1.memory(10)[63:0]',
422 'core.int.rp_src1.memory(13)[63:0]'
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
}, [
433 'ldst_port0_is_ld_i',
434 'ldst_port0_is_st_i',
436 'ldst_port0_addr_i[47:0]',
437 'ldst_port0_addr_i_ok',
438 'ldst_port0_addr_ok_o',
439 'ldst_port0_exc_happened',
440 'ldst_port0_st_data_i[63:0]',
441 'ldst_port0_st_data_i_ok',
442 'ldst_port0_ld_data_o[63:0]',
443 'ldst_port0_ld_data_o_ok',
448 if self
.microwatt_mmu
:
450 {'comment': 'microwatt_mmu'},
451 'core.fus.mmu0.alu_mmu0.illegal',
452 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
453 'core.fus.mmu0.alu_mmu0.mmu.state',
454 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
455 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
456 {'comment': 'wishbone_memory'},
457 'core.fus.mmu0.alu_mmu0.dcache.stb',
458 'core.fus.mmu0.alu_mmu0.dcache.cyc',
459 'core.fus.mmu0.alu_mmu0.dcache.we',
460 'core.fus.mmu0.alu_mmu0.dcache.ack',
461 'core.fus.mmu0.alu_mmu0.dcache.stall,'
464 write_gtkw("issuer_simulator.gtkw",
465 "issuer_simulator.vcd",
466 traces
, styles
, module
='top.issuer')
468 # add run of instructions
469 sim
.add_sync_process(process
)
471 # optionally, if a wishbone-based ROM is passed in, run that as an
472 # extra emulated process
473 if self
.rom
is not None:
474 dcache
= core
.fus
.fus
["mmu0"].alu
.dcache
475 default_mem
= self
.rom
476 sim
.add_sync_process(wrap(wb_get(dcache
, default_mem
, "DCACHE")))
478 with sim
.write_vcd("issuer_simulator.vcd"):