1 """TestRunner class, runs TestIssuer instructions
5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
7 from nmigen
import Module
, Signal
, Cat
, ClockSignal
8 from nmigen
.hdl
.xfrm
import ResetInserter
10 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
11 # Also, check out the cxxsim nmigen branch, and latest yosys from git
12 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
14 from nmutil
.formaltest
import FHDLTestCase
15 from nmutil
.gtkw
import write_gtkw
16 from nmigen
.cli
import rtlil
17 from openpower
.decoder
.isa
.caller
import special_sprs
, SVP64State
18 from openpower
.decoder
.isa
.all
import ISA
19 from openpower
.endian
import bigendian
21 from openpower
.decoder
.power_decoder
import create_pdecode
22 from openpower
.decoder
.power_decoder2
import PowerDecode2
23 from soc
.regfile
.regfiles
import StateRegs
25 from soc
.simple
.issuer
import TestIssuerInternal
27 from soc
.config
.test
.test_loadstore
import TestMemPspec
28 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
,
31 from soc
.fu
.compunits
.test
.test_compunit
import (setup_tst_memory
,
33 from soc
.debug
.dmi
import DBGCore
, DBGCtrl
, DBGStat
34 from nmutil
.util
import wrap
35 from soc
.experiment
.test
.test_mmu_dcache
import wb_get
38 def setup_i_memory(imem
, startaddr
, instructions
):
40 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
42 for i
in range(mem
.depth
):
43 yield mem
._array
[i
].eq(0)
45 startaddr
//= 4 # instructions are 32-bit
48 for ins
in instructions
:
49 if isinstance(ins
, tuple):
53 insn
= insn
& 0xffffffff
54 yield mem
._array
[startaddr
].eq(insn
)
57 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
59 startaddr
= startaddr
& mask
64 for ins
in instructions
:
65 if isinstance(ins
, tuple):
69 insn
= insn
& 0xffffffff
70 msbs
= (startaddr
>> 1) & mask
71 val
= yield mem
._array
[msbs
]
73 print("before set", hex(4*startaddr
),
74 hex(msbs
), hex(val
), hex(insn
))
75 lsb
= 1 if (startaddr
& 1) else 0
76 val
= (val |
(insn
<< (lsb
*32)))
78 yield mem
._array
[msbs
].eq(val
)
81 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
82 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
84 startaddr
= startaddr
& mask
87 def set_dmi(dmi
, addr
, data
):
89 yield dmi
.addr_i
.eq(addr
)
90 yield dmi
.din
.eq(data
)
99 yield dmi
.addr_i
.eq(0)
105 def get_dmi(dmi
, addr
):
106 yield dmi
.req_i
.eq(1)
107 yield dmi
.addr_i
.eq(addr
)
111 ack
= yield dmi
.ack_o
116 data
= yield dmi
.dout
# get data after ack valid for 1 cycle
117 yield dmi
.req_i
.eq(0)
118 yield dmi
.addr_i
.eq(0)
124 class TestRunner(FHDLTestCase
):
125 def __init__(self
, tst_data
, microwatt_mmu
=False, rom
=None,
127 super().__init
__("run_all")
128 self
.test_data
= tst_data
129 self
.microwatt_mmu
= microwatt_mmu
137 svstate_i
= Signal(64)
139 if self
.microwatt_mmu
:
140 ldst_ifacetype
= 'test_mmu_cache_wb'
142 ldst_ifacetype
= 'test_bare_wb'
143 imem_ifacetype
= 'test_bare_wb'
145 pspec
= TestMemPspec(ldst_ifacetype
=ldst_ifacetype
,
146 imem_ifacetype
=imem_ifacetype
,
157 mmu
=self
.microwatt_mmu
,
159 #hard_reset = Signal(reset_less=True)
160 issuer
= TestIssuerInternal(pspec
)
161 # use DMI RESET command instead, this does actually work though
162 #issuer = ResetInserter({'coresync': hard_reset,
163 # 'sync': hard_reset})(issuer)
164 m
.submodules
.issuer
= issuer
165 imem
= issuer
.imem
._get
_memory
()
168 pdecode2
= issuer
.pdecode2
170 regreduce_en
= pspec
.regreduce_en
== True
172 #simdec = create_pdecode()
173 simdec2
= PowerDecode2(None, regreduce_en
=regreduce_en
)
174 m
.submodules
.simdec2
= simdec2
# pain in the neck
176 # run core clock at same rate as test clock
177 intclk
= ClockSignal("coresync")
178 comb
+= intclk
.eq(ClockSignal())
180 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
181 comb
+= issuer
.svstate_i
.data
.eq(svstate_i
)
190 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
193 # get each test, completely reset the core, and run it
195 for test
in self
.test_data
:
197 # set up bigendian (TODO: don't do this, use MSR)
198 yield issuer
.core_bigendian_i
.eq(bigendian
)
207 program
= test
.program
208 with self
.subTest(test
.name
):
209 print("regs", test
.regs
)
210 print("sprs", test
.sprs
)
212 print("mem", test
.mem
)
213 print("msr", test
.msr
)
214 print("assem", program
.assembly
)
215 gen
= list(program
.generate_instructions())
216 insncode
= program
.assembly
.splitlines()
217 instructions
= list(zip(gen
, insncode
))
219 # set up the Simulator (which must track TestIssuer exactly)
220 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
222 initial_insns
=gen
, respect_pc
=True,
223 disassembly
=insncode
,
225 initial_svstate
=test
.svstate
)
227 # establish the TestIssuer context (mem, regs etc)
229 pc
= 0 # start address
230 counter
= 0 # test to pause/start
232 yield from setup_i_memory(imem
, pc
, instructions
)
233 yield from setup_tst_memory(l0
, sim
)
234 yield from setup_regs(pdecode2
, core
, test
)
238 yield issuer
.pc_i
.ok
.eq(1)
240 initial_svstate
= test
.svstate
241 if isinstance(initial_svstate
, int):
242 initial_svstate
= SVP64State(initial_svstate
)
243 yield svstate_i
.eq(initial_svstate
.value
)
244 yield issuer
.svstate_i
.ok
.eq(1)
247 print("instructions", instructions
)
249 # run the loop of the instructions on the current test
250 index
= sim
.pc
.CIA
.value
//4
251 while index
< len(instructions
):
252 ins
, code
= instructions
[index
]
254 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
260 yield from set_dmi(dmi
, DBGCore
.CTRL
,
262 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
263 yield issuer
.svstate_i
.ok
.eq(0) # ditto
267 counter
= counter
+ 1
269 # wait until executed
270 while not (yield issuer
.insn_done
):
273 # set up simulated instruction (in simdec2)
275 yield from sim
.setup_one()
276 except KeyError: # instruction not in imem: stop
280 # call simulated operation
282 yield from sim
.execute_one()
284 index
= sim
.pc
.CIA
.value
//4
286 terminated
= yield issuer
.dbg
.terminated_o
287 print("terminated", terminated
)
289 if index
>= len(instructions
):
290 print ("index over, send dmi stop")
292 yield from set_dmi(dmi
, DBGCore
.CTRL
,
298 yield from check_regs(self
, sim
, core
, test
, code
)
301 yield from check_sim_memory(self
, l0
, sim
, code
)
303 terminated
= yield issuer
.dbg
.terminated_o
304 print("terminated(2)", terminated
)
309 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
314 cr
= yield from get_dmi(dmi
, DBGCore
.CR
)
315 print("after test %s cr value %x" % (test
.name
, cr
))
318 xer
= yield from get_dmi(dmi
, DBGCore
.XER
)
319 print("after test %s XER value %x" % (test
.name
, xer
))
321 # test of dmi reg get
322 for int_reg
in range(32):
323 yield from set_dmi(dmi
, DBGCore
.GSPR_IDX
, int_reg
)
324 value
= yield from get_dmi(dmi
, DBGCore
.GSPR_DATA
)
326 print("after test %s reg %2d value %x" %
327 (test
.name
, int_reg
, value
))
330 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.RESET
)
334 'dec': {'base': 'dec'},
335 'bin': {'base': 'bin'},
336 'closed': {'closed': True}
341 ('state machines', 'closed', [
342 'fetch_pc_valid_i', 'fetch_pc_ready_o',
344 'fetch_insn_valid_o', 'fetch_insn_ready_i',
345 'pred_insn_valid_i', 'pred_insn_ready_o',
346 'fetch_predicate_state',
347 'pred_mask_valid_o', 'pred_mask_ready_i',
349 'exec_insn_valid_i', 'exec_insn_ready_o',
351 'exec_pc_valid_o', 'exec_pc_ready_i',
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]',
357 'cur_pc[63:0]', 'core_core_cia[63:0]']),
359 'raw_opcode_in[31:0]', 'insn_type',
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 {'comment': 'memory port interface'},
417 'core.l0.pimem.ldst_port0_is_ld_i',
418 'core.l0.pimem.ldst_port0_is_st_i',
419 'core.l0.pimem.ldst_port0_busy_o',
420 'core.l0.pimem.ldst_port0_addr_i[47:0]',
421 'core.l0.pimem.ldst_port0_addr_i_ok',
422 'core.l0.pimem.ldst_port0_addr_ok_o',
423 'core.l0.pimem.ldst_port0_st_data_i[63:0]',
424 'core.l0.pimem.ldst_port0_st_data_i_ok',
425 'core.l0.pimem.ldst_port0_ld_data_o[63:0]',
426 'core.l0.pimem.ldst_port0_ld_data_o_ok'
429 if self
.microwatt_mmu
:
431 {'comment': 'microwatt_mmu'},
432 'core.fus.mmu0.alu_mmu0.illegal',
433 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
434 'core.fus.mmu0.alu_mmu0.mmu.state',
435 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
436 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
437 {'comment': 'wishbone_memory'},
438 'core.fus.mmu0.alu_mmu0.dcache.stb',
439 'core.fus.mmu0.alu_mmu0.dcache.cyc',
440 'core.fus.mmu0.alu_mmu0.dcache.we',
441 'core.fus.mmu0.alu_mmu0.dcache.ack',
442 'core.fus.mmu0.alu_mmu0.dcache.stall,'
445 write_gtkw("issuer_simulator.gtkw",
446 "issuer_simulator.vcd",
447 traces
, styles
, module
='top.issuer')
449 # add run of instructions
450 sim
.add_sync_process(process
)
452 # optionally, if a wishbone-based ROM is passed in, run that as an
453 # extra emulated process
454 if self
.rom
is not None:
455 dcache
= core
.fus
.fus
["mmu0"].alu
.dcache
456 default_mem
= self
.rom
457 sim
.add_sync_process(wrap(wb_get(dcache
, default_mem
, "DCACHE")))
459 with sim
.write_vcd("issuer_simulator.vcd"):