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
9 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
10 # Also, check out the cxxsim nmigen branch, and latest yosys from git
11 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
13 from nmutil
.formaltest
import FHDLTestCase
14 from nmutil
.gtkw
import write_gtkw
15 from nmigen
.cli
import rtlil
16 from openpower
.decoder
.isa
.caller
import special_sprs
, SVP64State
17 from openpower
.decoder
.isa
.all
import ISA
18 from openpower
.endian
import bigendian
20 from openpower
.decoder
.power_decoder
import create_pdecode
21 from openpower
.decoder
.power_decoder2
import PowerDecode2
22 from soc
.regfile
.regfiles
import StateRegs
24 from soc
.simple
.issuer
import TestIssuerInternal
26 from soc
.config
.test
.test_loadstore
import TestMemPspec
27 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
,
30 from soc
.fu
.compunits
.test
.test_compunit
import (setup_test_memory
,
32 from soc
.debug
.dmi
import DBGCore
, DBGCtrl
, DBGStat
33 from nmutil
.util
import wrap
34 from soc
.experiment
.test
.test_mmu_dcache
import wb_get
37 def setup_i_memory(imem
, startaddr
, instructions
):
39 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
41 for i
in range(mem
.depth
):
42 yield mem
._array
[i
].eq(0)
44 startaddr
//= 4 # instructions are 32-bit
47 for ins
in instructions
:
48 if isinstance(ins
, tuple):
52 insn
= insn
& 0xffffffff
53 yield mem
._array
[startaddr
].eq(insn
)
56 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
58 startaddr
= startaddr
& mask
63 for ins
in instructions
:
64 if isinstance(ins
, tuple):
68 insn
= insn
& 0xffffffff
69 msbs
= (startaddr
>> 1) & mask
70 val
= yield mem
._array
[msbs
]
72 print("before set", hex(4*startaddr
),
73 hex(msbs
), hex(val
), hex(insn
))
74 lsb
= 1 if (startaddr
& 1) else 0
75 val
= (val |
(insn
<< (lsb
*32)))
77 yield mem
._array
[msbs
].eq(val
)
80 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
81 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
83 startaddr
= startaddr
& mask
86 def set_dmi(dmi
, addr
, data
):
88 yield dmi
.addr_i
.eq(addr
)
89 yield dmi
.din
.eq(data
)
98 yield dmi
.addr_i
.eq(0)
104 def get_dmi(dmi
, addr
):
105 yield dmi
.req_i
.eq(1)
106 yield dmi
.addr_i
.eq(addr
)
110 ack
= yield dmi
.ack_o
115 data
= yield dmi
.dout
# get data after ack valid for 1 cycle
116 yield dmi
.req_i
.eq(0)
117 yield dmi
.addr_i
.eq(0)
123 class TestRunner(FHDLTestCase
):
124 def __init__(self
, tst_data
, microwatt_mmu
=False, rom
=None,
126 super().__init
__("run_all")
127 self
.test_data
= tst_data
128 self
.microwatt_mmu
= microwatt_mmu
136 svstate_i
= Signal(32)
138 ldst_ifacetype
= 'mmu_cache_wb' if microwatt_mmu
else 'test_bare_wb'
139 imem_ifacetype
= 'test_bare_wb'
141 pspec
= TestMemPspec(ldst_ifacetype
=ldst_ifacetype
,
142 imem_ifacetype
=imem_ifacetype
,
153 mmu
=self
.microwatt_mmu
,
155 m
.submodules
.issuer
= issuer
= TestIssuerInternal(pspec
)
156 imem
= issuer
.imem
._get
_memory
()
159 pdecode2
= issuer
.pdecode2
161 regreduce_en
= pspec
.regreduce_en
== True
163 # copy of the decoder for simulator
164 simdec
= create_pdecode()
165 simdec2
= PowerDecode2(simdec
, regreduce_en
=regreduce_en
)
166 m
.submodules
.simdec2
= simdec2
# pain in the neck
168 # run core clock at same rate as test clock
169 intclk
= ClockSignal("coresync")
170 comb
+= intclk
.eq(ClockSignal())
172 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
173 comb
+= issuer
.svstate_i
.data
.eq(svstate_i
)
182 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
186 # get each test, completely reset the core, and run it
188 for test
in self
.test_data
:
191 # yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
193 # set up bigendian (TODO: don't do this, use MSR)
194 yield issuer
.core_bigendian_i
.eq(bigendian
)
203 program
= test
.program
204 self
.subTest(test
.name
)
205 print("regs", test
.regs
)
206 print("sprs", test
.sprs
)
208 print("mem", test
.mem
)
209 print("msr", test
.msr
)
210 print("assem", program
.assembly
)
211 gen
= list(program
.generate_instructions())
212 insncode
= program
.assembly
.splitlines()
213 instructions
= list(zip(gen
, insncode
))
215 # set up the Simulator (which must track TestIssuer exactly)
216 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
218 initial_insns
=gen
, respect_pc
=True,
219 disassembly
=insncode
,
221 initial_svstate
=test
.svstate
)
223 # establish the TestIssuer context (mem, regs etc)
225 pc
= 0 # start address
226 counter
= 0 # test to pause/start
228 yield from setup_i_memory(imem
, pc
, instructions
)
229 yield from setup_test_memory(l0
, sim
)
230 yield from setup_regs(pdecode2
, core
, test
)
234 yield issuer
.pc_i
.ok
.eq(1)
236 initial_svstate
= test
.svstate
237 if isinstance(initial_svstate
, int):
238 initial_svstate
= SVP64State(initial_svstate
)
239 yield svstate_i
.eq(initial_svstate
.spr
.value
)
240 yield issuer
.svstate_i
.ok
.eq(1)
243 print("instructions", instructions
)
245 # run the loop of the instructions on the current test
246 index
= sim
.pc
.CIA
.value
//4
247 while index
< len(instructions
):
248 ins
, code
= instructions
[index
]
250 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
256 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.START
)
257 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
258 yield issuer
.svstate_i
.ok
.eq(0) # ditto
262 counter
= counter
+ 1
264 # wait until executed
265 while not (yield issuer
.insn_done
):
268 # set up simulated instruction (in simdec2)
270 yield from sim
.setup_one()
271 except KeyError: # indicates instruction not in imem: stop
275 # call simulated operation
277 yield from sim
.execute_one()
279 index
= sim
.pc
.CIA
.value
//4
281 terminated
= yield issuer
.dbg
.terminated_o
282 print("terminated", terminated
)
284 if index
>= len(instructions
):
285 print ("index over, send dmi stop")
287 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
292 yield from check_regs(self
, sim
, core
, test
, code
)
295 yield from check_sim_memory(self
, l0
, sim
, code
)
297 terminated
= yield issuer
.dbg
.terminated_o
298 print("terminated(2)", terminated
)
303 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
308 cr
= yield from get_dmi(dmi
, DBGCore
.CR
)
309 print("after test %s cr value %x" % (test
.name
, cr
))
312 xer
= yield from get_dmi(dmi
, DBGCore
.XER
)
313 print("after test %s XER value %x" % (test
.name
, xer
))
315 # test of dmi reg get
316 for int_reg
in range(32):
317 yield from set_dmi(dmi
, DBGCore
.GSPR_IDX
, int_reg
)
318 value
= yield from get_dmi(dmi
, DBGCore
.GSPR_DATA
)
320 print("after test %s reg %2d value %x" %
321 (test
.name
, int_reg
, value
))
324 'dec': {'base': 'dec'},
325 'bin': {'base': 'bin'},
326 'closed': {'closed': True}
331 ('state machines', 'closed', [
332 'fetch_pc_valid_i', 'fetch_pc_ready_o',
334 'fetch_insn_valid_o', 'fetch_insn_ready_i',
335 'pred_insn_valid_i', 'pred_insn_ready_o',
336 'fetch_predicate_state',
337 'pred_mask_valid_o', 'pred_mask_ready_i',
339 'exec_insn_valid_i', 'exec_insn_ready_o',
341 'exec_pc_valid_o', 'exec_pc_ready_i',
342 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
343 'is_last', 'dec2.no_out_vec']),
344 {'comment': 'fetch and decode'},
346 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
347 'cur_pc[63:0]', 'core_core_cia[63:0]']),
349 'raw_opcode_in[31:0]', 'insn_type',
350 ('svp64 decoding', 'closed', [
351 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
352 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
353 'dec2.sv_rm_dec.ptype_in',
354 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
355 'dstmask[63:0]', 'srcmask[63:0]',
356 'dregread[4:0]', 'dinvert',
357 'sregread[4:0]', 'sinvert',
358 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
359 'core.int.pred__ren']),
360 ('register augmentation', 'dec', 'closed', [
361 {'comment': 'v3.0b registers'},
362 'dec2.dec_o.RT[4:0]',
363 'dec2.dec_a.RA[4:0]',
364 'dec2.dec_b.RB[4:0]',
366 'dec2.o_svdec.reg_in[4:0]',
367 ('dec2.o_svdec.spec[2:0]', 'bin'),
368 'dec2.o_svdec.reg_out[6:0]']),
370 'dec2.in1_svdec.reg_in[4:0]',
371 ('dec2.in1_svdec.spec[2:0]', 'bin'),
372 'dec2.in1_svdec.reg_out[6:0]']),
374 'dec2.in2_svdec.reg_in[4:0]',
375 ('dec2.in2_svdec.spec[2:0]', 'bin'),
376 'dec2.in2_svdec.reg_out[6:0]']),
377 {'comment': 'SVP64 registers'},
378 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
380 {'comment': 'svp64 context'},
381 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
382 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
383 'core_core_dststep[6:0]',
384 {'comment': 'issue and execute'},
385 'core.core_core_insn_type',
387 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
390 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
391 {'comment': 'instruction memory'},
392 'imem.sram.rdport.memory(0)[63:0]',
393 {'comment': 'registers'},
394 # match with soc.regfile.regfiles.IntRegs port names
395 'core.int.rp_src1.memory(0)[63:0]',
396 'core.int.rp_src1.memory(1)[63:0]',
397 'core.int.rp_src1.memory(2)[63:0]',
398 'core.int.rp_src1.memory(3)[63:0]',
399 'core.int.rp_src1.memory(4)[63:0]',
400 'core.int.rp_src1.memory(5)[63:0]',
401 'core.int.rp_src1.memory(6)[63:0]',
402 'core.int.rp_src1.memory(7)[63:0]',
403 'core.int.rp_src1.memory(9)[63:0]',
404 'core.int.rp_src1.memory(10)[63:0]',
405 'core.int.rp_src1.memory(13)[63:0]',
408 if self
.microwatt_mmu
:
410 {'comment': 'microwatt_mmu'},
411 'core.fus.mmu0.alu_mmu0.illegal',
412 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
413 'core.fus.mmu0.alu_mmu0.mmu.state',
414 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
415 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
416 {'comment': 'wishbone_memory'},
417 'core.fus.mmu0.alu_mmu0.dcache.stb',
418 'core.fus.mmu0.alu_mmu0.dcache.cyc',
419 'core.fus.mmu0.alu_mmu0.dcache.we',
420 'core.fus.mmu0.alu_mmu0.dcache.ack',
421 'core.fus.mmu0.alu_mmu0.dcache.stall,'
424 write_gtkw("issuer_simulator.gtkw",
425 "issuer_simulator.vcd",
426 traces
, styles
, module
='top.issuer')
428 # add run of instructions
429 sim
.add_sync_process(process
)
431 # optionally, if a wishbone-based ROM is passed in, run that as an
432 # extra emulated process
433 if self
.rom
is not None:
434 dcache
= core
.fus
.fus
["mmu0"].alu
.dcache
435 default_mem
= self
.rom
436 sim
.add_sync_process(wrap(wb_get(dcache
, default_mem
, "DCACHE")))
438 with sim
.write_vcd("issuer_simulator.vcd"):