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 soc
.decoder
.isa
.caller
import special_sprs
17 from soc
.decoder
.isa
.all
import ISA
18 from soc
.config
.endian
import bigendian
20 from soc
.decoder
.power_decoder
import create_pdecode
21 from soc
.decoder
.power_decoder2
import PowerDecode2
23 from soc
.simple
.issuer
import TestIssuerInternal
25 from soc
.config
.test
.test_loadstore
import TestMemPspec
26 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
,
29 from soc
.fu
.compunits
.test
.test_compunit
import (setup_test_memory
,
31 from soc
.debug
.dmi
import DBGCore
, DBGCtrl
, DBGStat
34 def setup_i_memory(imem
, startaddr
, instructions
):
36 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
38 for i
in range(mem
.depth
):
39 yield mem
._array
[i
].eq(0)
41 startaddr
//= 4 # instructions are 32-bit
44 for ins
in instructions
:
45 if isinstance(ins
, tuple):
49 insn
= insn
& 0xffffffff
50 yield mem
._array
[startaddr
].eq(insn
)
53 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
55 startaddr
= startaddr
& mask
60 for ins
in instructions
:
61 if isinstance(ins
, tuple):
65 insn
= insn
& 0xffffffff
66 msbs
= (startaddr
>> 1) & mask
67 val
= yield mem
._array
[msbs
]
69 print("before set", hex(4*startaddr
),
70 hex(msbs
), hex(val
), hex(insn
))
71 lsb
= 1 if (startaddr
& 1) else 0
72 val
= (val |
(insn
<< (lsb
*32)))
74 yield mem
._array
[msbs
].eq(val
)
77 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
78 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
80 startaddr
= startaddr
& mask
83 def set_dmi(dmi
, addr
, data
):
85 yield dmi
.addr_i
.eq(addr
)
86 yield dmi
.din
.eq(data
)
95 yield dmi
.addr_i
.eq(0)
101 def get_dmi(dmi
, addr
):
102 yield dmi
.req_i
.eq(1)
103 yield dmi
.addr_i
.eq(addr
)
107 ack
= yield dmi
.ack_o
112 data
= yield dmi
.dout
# get data after ack valid for 1 cycle
113 yield dmi
.req_i
.eq(0)
114 yield dmi
.addr_i
.eq(0)
120 class TestRunner(FHDLTestCase
):
121 def __init__(self
, tst_data
, microwatt_mmu
=False):
122 super().__init
__("run_all")
123 self
.test_data
= tst_data
124 self
.microwatt_mmu
= microwatt_mmu
131 pspec
= TestMemPspec(ldst_ifacetype
='test_bare_wb',
132 imem_ifacetype
='test_bare_wb',
141 mmu
=self
.microwatt_mmu
,
143 m
.submodules
.issuer
= issuer
= TestIssuerInternal(pspec
)
144 imem
= issuer
.imem
._get
_memory
()
147 pdecode2
= issuer
.pdecode2
150 # copy of the decoder for simulator
151 simdec
= create_pdecode()
152 simdec2
= PowerDecode2(simdec
)
153 m
.submodules
.simdec2
= simdec2
# pain in the neck
155 # run core clock at same rate as test clock
156 intclk
= ClockSignal("coresync")
157 comb
+= intclk
.eq(ClockSignal())
159 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
168 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
172 # get each test, completely reset the core, and run it
174 for test
in self
.test_data
:
177 # yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
179 # set up bigendian (TODO: don't do this, use MSR)
180 yield issuer
.core_bigendian_i
.eq(bigendian
)
189 program
= test
.program
190 self
.subTest(test
.name
)
191 print("regs", test
.regs
)
192 print("sprs", test
.sprs
)
194 print("mem", test
.mem
)
195 print("msr", test
.msr
)
196 print("assem", program
.assembly
)
197 gen
= list(program
.generate_instructions())
198 insncode
= program
.assembly
.splitlines()
199 instructions
= list(zip(gen
, insncode
))
201 # set up the Simulator (which must track TestIssuer exactly)
202 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
204 initial_insns
=gen
, respect_pc
=True,
205 disassembly
=insncode
,
207 initial_svstate
=test
.svstate
)
209 # establish the TestIssuer context (mem, regs etc)
211 pc
= 0 # start address
212 counter
= 0 # test to pause/start
214 yield from setup_i_memory(imem
, pc
, instructions
)
215 yield from setup_test_memory(l0
, sim
)
216 yield from setup_regs(pdecode2
, core
, test
)
217 # TODO, setup svstate here in core.regs.state regfile
218 # https://bugs.libre-soc.org/show_bug.cgi?id=583#c35
221 yield issuer
.pc_i
.ok
.eq(1)
224 print("instructions", instructions
)
226 # run the loop of the instructions on the current test
227 index
= sim
.pc
.CIA
.value
//4
228 while index
< len(instructions
):
229 ins
, code
= instructions
[index
]
231 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
237 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.START
)
238 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
242 counter
= counter
+ 1
244 # wait until executed
245 yield from wait_for_busy_hi(core
)
246 yield from wait_for_busy_clear(core
)
248 # set up simulated instruction (in simdec2)
250 yield from sim
.setup_one()
251 except KeyError: # indicates instruction not in imem: stop
255 # call simulated operation
257 yield from sim
.execute_one()
259 index
= sim
.pc
.CIA
.value
//4
261 terminated
= yield issuer
.dbg
.terminated_o
262 print("terminated", terminated
)
264 if index
>= len(instructions
):
265 print ("index over, send dmi stop")
267 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
271 # wait one cycle for registers to settle
275 yield from check_regs(self
, sim
, core
, test
, code
)
278 yield from check_sim_memory(self
, l0
, sim
, code
)
280 terminated
= yield issuer
.dbg
.terminated_o
281 print("terminated(2)", terminated
)
286 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
291 cr
= yield from get_dmi(dmi
, DBGCore
.CR
)
292 print("after test %s cr value %x" % (test
.name
, cr
))
295 xer
= yield from get_dmi(dmi
, DBGCore
.XER
)
296 print("after test %s XER value %x" % (test
.name
, xer
))
298 # test of dmi reg get
299 for int_reg
in range(32):
300 yield from set_dmi(dmi
, DBGCore
.GSPR_IDX
, int_reg
)
301 value
= yield from get_dmi(dmi
, DBGCore
.GSPR_DATA
)
303 print("after test %s reg %2d value %x" %
304 (test
.name
, int_reg
, value
))
307 'dec': {'base': 'dec'},
308 'bin': {'base': 'bin'}
313 {'comment': 'state machines'},
314 'fetch_pc_valid_i', 'fetch_pc_ready_o', 'fetch_fsm_state',
315 'fetch_insn_valid_o', 'fetch_insn_ready_i', 'fsm_state',
316 {'comment': 'fetch and decode'},
317 'cia[63:0]', 'nia[63:0]', 'pc[63:0]', 'raw_insn_i[31:0]',
318 'raw_opcode_in[31:0]', 'insn_type',
319 {'comment': 'svp64 decoding'},
321 ('dec2.extra[8:0]', 'bin'),
322 ('register augmentation', 'dec', [
323 {'comment': 'v3.0b registers'},
324 'dec2.dec_o.RT[4:0]',
325 'dec2.dec_a.RA[4:0]',
326 'dec2.dec_b.RB[4:0]',
328 'dec2.o_svdec.reg_in[4:0]',
329 ('dec2.o_svdec.spec[2:0]', 'bin'),
330 'dec2.o_svdec.reg_out[6:0]']),
332 'dec2.in1_svdec.reg_in[4:0]',
333 ('dec2.in1_svdec.spec[2:0]', 'bin'),
334 'dec2.in1_svdec.reg_out[6:0]']),
336 'dec2.in2_svdec.reg_in[4:0]',
337 ('dec2.in2_svdec.spec[2:0]', 'bin'),
338 'dec2.in2_svdec.reg_out[6:0]']),
339 {'comment': 'SVP64 registers'},
340 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
342 {'comment': 'issue and execute'},
343 'core.core_core_insn_type',
345 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
348 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
349 {'comment': 'instruction memory'},
350 'imem.sram.rdport.memory(0)[63:0]',
351 {'comment': 'registers'},
352 'core.int.rp_src1.memory(0)[63:0]',
353 'core.int.rp_src1.memory(1)[63:0]',
354 'core.int.rp_src1.memory(2)[63:0]',
355 'core.int.rp_src1.memory(3)[63:0]',
356 'core.int.rp_src1.memory(4)[63:0]',
357 'core.int.rp_src1.memory(5)[63:0]',
358 'core.int.rp_src1.memory(6)[63:0]',
359 'core.int.rp_src1.memory(7)[63:0]',
360 'core.int.rp_src1.memory(9)[63:0]',
361 'core.int.rp_src1.memory(10)[63:0]',
362 'core.int.rp_src1.memory(13)[63:0]',
365 if self
.microwatt_mmu
:
367 {'comment': 'microwatt_mmu'},
368 'core.fus.mmu0.alu_mmu0.illegal',
369 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
370 'core.fus.mmu0.alu_mmu0.mmu.state',
371 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
372 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]'
375 write_gtkw("issuer_simulator.gtkw",
376 "issuer_simulator.vcd",
377 traces
, styles
, module
='top.issuer')
379 sim
.add_sync_process(process
)
380 with sim
.write_vcd("issuer_simulator.vcd"):