add option to use new mmu_cache_wb ConfigMemoryPortInterface
[soc.git] / src / soc / simple / test / test_runner.py
1 """TestRunner class, runs TestIssuer instructions
2
3 related bugs:
4
5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
6 """
7 from nmigen import Module, Signal, Cat, ClockSignal
8
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
12
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
19
20 from openpower.decoder.power_decoder import create_pdecode
21 from openpower.decoder.power_decoder2 import PowerDecode2
22 from soc.regfile.regfiles import StateRegs
23
24 from soc.simple.issuer import TestIssuerInternal
25
26 from soc.config.test.test_loadstore import TestMemPspec
27 from soc.simple.test.test_core import (setup_regs, check_regs,
28 wait_for_busy_clear,
29 wait_for_busy_hi)
30 from soc.fu.compunits.test.test_compunit import (setup_test_memory,
31 check_sim_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
35
36
37 def setup_i_memory(imem, startaddr, instructions):
38 mem = imem
39 print("insn before, init mem", mem.depth, mem.width, mem,
40 len(instructions))
41 for i in range(mem.depth):
42 yield mem._array[i].eq(0)
43 yield Settle()
44 startaddr //= 4 # instructions are 32-bit
45 if mem.width == 32:
46 mask = ((1 << 32)-1)
47 for ins in instructions:
48 if isinstance(ins, tuple):
49 insn, code = ins
50 else:
51 insn, code = ins, ''
52 insn = insn & 0xffffffff
53 yield mem._array[startaddr].eq(insn)
54 yield Settle()
55 if insn != 0:
56 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
57 startaddr += 1
58 startaddr = startaddr & mask
59 return
60
61 # 64 bit
62 mask = ((1 << 64)-1)
63 for ins in instructions:
64 if isinstance(ins, tuple):
65 insn, code = ins
66 else:
67 insn, code = ins, ''
68 insn = insn & 0xffffffff
69 msbs = (startaddr >> 1) & mask
70 val = yield mem._array[msbs]
71 if insn != 0:
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)))
76 val = val & mask
77 yield mem._array[msbs].eq(val)
78 yield Settle()
79 if insn != 0:
80 print("after set", hex(4*startaddr), hex(msbs), hex(val))
81 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
82 startaddr += 1
83 startaddr = startaddr & mask
84
85
86 def set_dmi(dmi, addr, data):
87 yield dmi.req_i.eq(1)
88 yield dmi.addr_i.eq(addr)
89 yield dmi.din.eq(data)
90 yield dmi.we_i.eq(1)
91 while True:
92 ack = yield dmi.ack_o
93 if ack:
94 break
95 yield
96 yield
97 yield dmi.req_i.eq(0)
98 yield dmi.addr_i.eq(0)
99 yield dmi.din.eq(0)
100 yield dmi.we_i.eq(0)
101 yield
102
103
104 def get_dmi(dmi, addr):
105 yield dmi.req_i.eq(1)
106 yield dmi.addr_i.eq(addr)
107 yield dmi.din.eq(0)
108 yield dmi.we_i.eq(0)
109 while True:
110 ack = yield dmi.ack_o
111 if ack:
112 break
113 yield
114 yield # wait one
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)
118 yield dmi.we_i.eq(0)
119 yield
120 return data
121
122
123 class TestRunner(FHDLTestCase):
124 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
125 svp64=True):
126 super().__init__("run_all")
127 self.test_data = tst_data
128 self.microwatt_mmu = microwatt_mmu
129 self.rom = rom
130 self.svp64 = svp64
131
132 def run_all(self):
133 m = Module()
134 comb = m.d.comb
135 pc_i = Signal(32)
136 svstate_i = Signal(32)
137
138 ldst_ifacetype = 'mmu_cache_wb' if microwatt_mmu else 'test_bare_wb'
139 imem_ifacetype = 'test_bare_wb'
140
141 pspec = TestMemPspec(ldst_ifacetype=ldst_ifacetype,
142 imem_ifacetype=imem_ifacetype,
143 addr_wid=48,
144 mask_wid=8,
145 imem_reg_wid=64,
146 # wb_data_width=32,
147 use_pll=False,
148 nocore=False,
149 xics=False,
150 gpio=False,
151 regreduce=True,
152 svp64=self.svp64,
153 mmu=self.microwatt_mmu,
154 reg_wid=64)
155 m.submodules.issuer = issuer = TestIssuerInternal(pspec)
156 imem = issuer.imem._get_memory()
157 core = issuer.core
158 dmi = issuer.dbg.dmi
159 pdecode2 = issuer.pdecode2
160 l0 = core.l0
161 regreduce_en = pspec.regreduce_en == True
162
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
167
168 # run core clock at same rate as test clock
169 intclk = ClockSignal("coresync")
170 comb += intclk.eq(ClockSignal())
171
172 comb += issuer.pc_i.data.eq(pc_i)
173 comb += issuer.svstate_i.data.eq(svstate_i)
174
175 # nmigen Simulation
176 sim = Simulator(m)
177 sim.add_clock(1e-6)
178
179 def process():
180
181 # start in stopped
182 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
183 yield
184 yield
185
186 # get each test, completely reset the core, and run it
187
188 for test in self.test_data:
189
190 # pull a reset
191 # yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
192
193 # set up bigendian (TODO: don't do this, use MSR)
194 yield issuer.core_bigendian_i.eq(bigendian)
195 yield Settle()
196
197 yield
198 yield
199 yield
200 yield
201
202 print(test.name)
203 program = test.program
204 self.subTest(test.name)
205 print("regs", test.regs)
206 print("sprs", test.sprs)
207 print("cr", test.cr)
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))
214
215 # set up the Simulator (which must track TestIssuer exactly)
216 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
217 test.msr,
218 initial_insns=gen, respect_pc=True,
219 disassembly=insncode,
220 bigendian=bigendian,
221 initial_svstate=test.svstate)
222
223 # establish the TestIssuer context (mem, regs etc)
224
225 pc = 0 # start address
226 counter = 0 # test to pause/start
227
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)
231
232 # set PC and SVSTATE
233 yield pc_i.eq(pc)
234 yield issuer.pc_i.ok.eq(1)
235
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)
241 yield
242
243 print("instructions", instructions)
244
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]
249
250 print("instruction: 0x{:X}".format(ins & 0xffffffff))
251 print(index, code)
252
253 if counter == 0:
254 # start the core
255 yield
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
259 yield
260 yield
261
262 counter = counter + 1
263
264 # wait until executed
265 while not (yield issuer.insn_done):
266 yield
267
268 # set up simulated instruction (in simdec2)
269 try:
270 yield from sim.setup_one()
271 except KeyError: # indicates instruction not in imem: stop
272 break
273 yield Settle()
274
275 # call simulated operation
276 print("sim", code)
277 yield from sim.execute_one()
278 yield Settle()
279 index = sim.pc.CIA.value//4
280
281 terminated = yield issuer.dbg.terminated_o
282 print("terminated", terminated)
283
284 if index >= len(instructions):
285 print ("index over, send dmi stop")
286 # stop at end
287 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
288 yield
289 yield
290
291 # register check
292 yield from check_regs(self, sim, core, test, code)
293
294 # Memory check
295 yield from check_sim_memory(self, l0, sim, code)
296
297 terminated = yield issuer.dbg.terminated_o
298 print("terminated(2)", terminated)
299 if terminated:
300 break
301
302 # stop at end
303 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
304 yield
305 yield
306
307 # get CR
308 cr = yield from get_dmi(dmi, DBGCore.CR)
309 print("after test %s cr value %x" % (test.name, cr))
310
311 # get XER
312 xer = yield from get_dmi(dmi, DBGCore.XER)
313 print("after test %s XER value %x" % (test.name, xer))
314
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)
319
320 print("after test %s reg %2d value %x" %
321 (test.name, int_reg, value))
322
323 styles = {
324 'dec': {'base': 'dec'},
325 'bin': {'base': 'bin'},
326 'closed': {'closed': True}
327 }
328
329 traces = [
330 'clk',
331 ('state machines', 'closed', [
332 'fetch_pc_valid_i', 'fetch_pc_ready_o',
333 'fetch_fsm_state',
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',
338 'issue_fsm_state',
339 'exec_insn_valid_i', 'exec_insn_ready_o',
340 'exec_fsm_state',
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'},
345 (None, 'dec', [
346 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
347 'cur_pc[63:0]', 'core_core_cia[63:0]']),
348 'raw_insn_i[31: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]',
365 ('Rdest', [
366 'dec2.o_svdec.reg_in[4:0]',
367 ('dec2.o_svdec.spec[2:0]', 'bin'),
368 'dec2.o_svdec.reg_out[6:0]']),
369 ('Rsrc1', [
370 'dec2.in1_svdec.reg_in[4:0]',
371 ('dec2.in1_svdec.spec[2:0]', 'bin'),
372 'dec2.in1_svdec.reg_out[6:0]']),
373 ('Rsrc1', [
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]'
379 ]),
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',
386 (None, 'dec', [
387 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
388 'issue_i', 'busy_o',
389 {'comment': 'dmi'},
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]',
406 ]
407
408 if self.microwatt_mmu:
409 traces += [
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,'
422 ]
423
424 write_gtkw("issuer_simulator.gtkw",
425 "issuer_simulator.vcd",
426 traces, styles, module='top.issuer')
427
428 # add run of instructions
429 sim.add_sync_process(process)
430
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")))
437
438 with sim.write_vcd("issuer_simulator.vcd"):
439 sim.run()