Move the reset code outside of the sub-test
[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 from nmigen.hdl.xfrm import ResetInserter
9
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
13
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
20
21 from openpower.decoder.power_decoder import create_pdecode
22 from openpower.decoder.power_decoder2 import PowerDecode2
23 from soc.regfile.regfiles import StateRegs
24
25 from soc.simple.issuer import TestIssuerInternal
26
27 from soc.config.test.test_loadstore import TestMemPspec
28 from soc.simple.test.test_core import (setup_regs, check_regs,
29 wait_for_busy_clear,
30 wait_for_busy_hi)
31 from soc.fu.compunits.test.test_compunit import (setup_test_memory,
32 check_sim_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
36
37
38 def setup_i_memory(imem, startaddr, instructions):
39 mem = imem
40 print("insn before, init mem", mem.depth, mem.width, mem,
41 len(instructions))
42 for i in range(mem.depth):
43 yield mem._array[i].eq(0)
44 yield Settle()
45 startaddr //= 4 # instructions are 32-bit
46 if mem.width == 32:
47 mask = ((1 << 32)-1)
48 for ins in instructions:
49 if isinstance(ins, tuple):
50 insn, code = ins
51 else:
52 insn, code = ins, ''
53 insn = insn & 0xffffffff
54 yield mem._array[startaddr].eq(insn)
55 yield Settle()
56 if insn != 0:
57 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
58 startaddr += 1
59 startaddr = startaddr & mask
60 return
61
62 # 64 bit
63 mask = ((1 << 64)-1)
64 for ins in instructions:
65 if isinstance(ins, tuple):
66 insn, code = ins
67 else:
68 insn, code = ins, ''
69 insn = insn & 0xffffffff
70 msbs = (startaddr >> 1) & mask
71 val = yield mem._array[msbs]
72 if insn != 0:
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)))
77 val = val & mask
78 yield mem._array[msbs].eq(val)
79 yield Settle()
80 if insn != 0:
81 print("after set", hex(4*startaddr), hex(msbs), hex(val))
82 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
83 startaddr += 1
84 startaddr = startaddr & mask
85
86
87 def set_dmi(dmi, addr, data):
88 yield dmi.req_i.eq(1)
89 yield dmi.addr_i.eq(addr)
90 yield dmi.din.eq(data)
91 yield dmi.we_i.eq(1)
92 while True:
93 ack = yield dmi.ack_o
94 if ack:
95 break
96 yield
97 yield
98 yield dmi.req_i.eq(0)
99 yield dmi.addr_i.eq(0)
100 yield dmi.din.eq(0)
101 yield dmi.we_i.eq(0)
102 yield
103
104
105 def get_dmi(dmi, addr):
106 yield dmi.req_i.eq(1)
107 yield dmi.addr_i.eq(addr)
108 yield dmi.din.eq(0)
109 yield dmi.we_i.eq(0)
110 while True:
111 ack = yield dmi.ack_o
112 if ack:
113 break
114 yield
115 yield # wait one
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)
119 yield dmi.we_i.eq(0)
120 yield
121 return data
122
123
124 class TestRunner(FHDLTestCase):
125 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
126 svp64=True):
127 super().__init__("run_all")
128 self.test_data = tst_data
129 self.microwatt_mmu = microwatt_mmu
130 self.rom = rom
131 self.svp64 = svp64
132
133 def run_all(self):
134 m = Module()
135 comb = m.d.comb
136 pc_i = Signal(32)
137 svstate_i = Signal(32)
138
139 if self.microwatt_mmu:
140 ldst_ifacetype = 'test_mmu_cache_wb'
141 else:
142 ldst_ifacetype = 'test_bare_wb'
143 imem_ifacetype = 'test_bare_wb'
144
145 pspec = TestMemPspec(ldst_ifacetype=ldst_ifacetype,
146 imem_ifacetype=imem_ifacetype,
147 addr_wid=48,
148 mask_wid=8,
149 imem_reg_wid=64,
150 # wb_data_width=32,
151 use_pll=False,
152 nocore=False,
153 xics=False,
154 gpio=False,
155 regreduce=True,
156 svp64=self.svp64,
157 mmu=self.microwatt_mmu,
158 reg_wid=64)
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()
166 core = issuer.core
167 dmi = issuer.dbg.dmi
168 pdecode2 = issuer.pdecode2
169 l0 = core.l0
170 regreduce_en = pspec.regreduce_en == True
171
172 # copy of the decoder for simulator
173 simdec = create_pdecode()
174 simdec2 = PowerDecode2(simdec, regreduce_en=regreduce_en)
175 m.submodules.simdec2 = simdec2 # pain in the neck
176
177 # run core clock at same rate as test clock
178 intclk = ClockSignal("coresync")
179 comb += intclk.eq(ClockSignal())
180
181 comb += issuer.pc_i.data.eq(pc_i)
182 comb += issuer.svstate_i.data.eq(svstate_i)
183
184 # nmigen Simulation
185 sim = Simulator(m)
186 sim.add_clock(1e-6)
187
188 def process():
189
190 # start in stopped
191 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
192 yield
193
194 # get each test, completely reset the core, and run it
195
196 for test in self.test_data:
197
198 # set up bigendian (TODO: don't do this, use MSR)
199 yield issuer.core_bigendian_i.eq(bigendian)
200 yield Settle()
201
202 yield
203 yield
204 yield
205 yield
206
207 print(test.name)
208 program = test.program
209 with self.subTest(test.name):
210 print("regs", test.regs)
211 print("sprs", test.sprs)
212 print("cr", test.cr)
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))
219
220 # set up the Simulator (which must track TestIssuer exactly)
221 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
222 test.msr,
223 initial_insns=gen, respect_pc=True,
224 disassembly=insncode,
225 bigendian=bigendian,
226 initial_svstate=test.svstate)
227
228 # establish the TestIssuer context (mem, regs etc)
229
230 pc = 0 # start address
231 counter = 0 # test to pause/start
232
233 yield from setup_i_memory(imem, pc, instructions)
234 yield from setup_test_memory(l0, sim)
235 yield from setup_regs(pdecode2, core, test)
236
237 # set PC and SVSTATE
238 yield pc_i.eq(pc)
239 yield issuer.pc_i.ok.eq(1)
240
241 initial_svstate = test.svstate
242 if isinstance(initial_svstate, int):
243 initial_svstate = SVP64State(initial_svstate)
244 yield svstate_i.eq(initial_svstate.spr.value)
245 yield issuer.svstate_i.ok.eq(1)
246 yield
247
248 print("instructions", instructions)
249
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]
254
255 print("instruction: 0x{:X}".format(ins & 0xffffffff))
256 print(index, code)
257
258 if counter == 0:
259 # start the core
260 yield
261 yield from set_dmi(dmi, DBGCore.CTRL,
262 1<<DBGCtrl.START)
263 yield issuer.pc_i.ok.eq(0) # no change PC after this
264 yield issuer.svstate_i.ok.eq(0) # ditto
265 yield
266 yield
267
268 counter = counter + 1
269
270 # wait until executed
271 while not (yield issuer.insn_done):
272 yield
273
274 # set up simulated instruction (in simdec2)
275 try:
276 yield from sim.setup_one()
277 except KeyError: # instruction not in imem: stop
278 break
279 yield Settle()
280
281 # call simulated operation
282 print("sim", code)
283 yield from sim.execute_one()
284 yield Settle()
285 index = sim.pc.CIA.value//4
286
287 terminated = yield issuer.dbg.terminated_o
288 print("terminated", terminated)
289
290 if index >= len(instructions):
291 print ("index over, send dmi stop")
292 # stop at end
293 yield from set_dmi(dmi, DBGCore.CTRL,
294 1<<DBGCtrl.STOP)
295 yield
296 yield
297
298 # register check
299 yield from check_regs(self, sim, core, test, code)
300
301 # Memory check
302 yield from check_sim_memory(self, l0, sim, code)
303
304 terminated = yield issuer.dbg.terminated_o
305 print("terminated(2)", terminated)
306 if terminated:
307 break
308
309 # stop at end
310 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
311 yield
312 yield
313
314 # get CR
315 cr = yield from get_dmi(dmi, DBGCore.CR)
316 print("after test %s cr value %x" % (test.name, cr))
317
318 # get XER
319 xer = yield from get_dmi(dmi, DBGCore.XER)
320 print("after test %s XER value %x" % (test.name, xer))
321
322 # test of dmi reg get
323 for int_reg in range(32):
324 yield from set_dmi(dmi, DBGCore.GSPR_IDX, int_reg)
325 value = yield from get_dmi(dmi, DBGCore.GSPR_DATA)
326
327 print("after test %s reg %2d value %x" %
328 (test.name, int_reg, value))
329
330 # pull a reset
331 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
332 yield
333
334 styles = {
335 'dec': {'base': 'dec'},
336 'bin': {'base': 'bin'},
337 'closed': {'closed': True}
338 }
339
340 traces = [
341 'clk',
342 ('state machines', 'closed', [
343 'fetch_pc_valid_i', 'fetch_pc_ready_o',
344 'fetch_fsm_state',
345 'fetch_insn_valid_o', 'fetch_insn_ready_i',
346 'pred_insn_valid_i', 'pred_insn_ready_o',
347 'fetch_predicate_state',
348 'pred_mask_valid_o', 'pred_mask_ready_i',
349 'issue_fsm_state',
350 'exec_insn_valid_i', 'exec_insn_ready_o',
351 'exec_fsm_state',
352 'exec_pc_valid_o', 'exec_pc_ready_i',
353 'insn_done', 'core_stop_o', 'pc_i_ok', 'pc_changed',
354 'is_last', 'dec2.no_out_vec']),
355 {'comment': 'fetch and decode'},
356 (None, 'dec', [
357 'cia[63:0]', 'nia[63:0]', 'pc[63:0]',
358 'cur_pc[63:0]', 'core_core_cia[63:0]']),
359 'raw_insn_i[31:0]',
360 'raw_opcode_in[31:0]', 'insn_type',
361 ('svp64 decoding', 'closed', [
362 'svp64_rm[23:0]', ('dec2.extra[8:0]', 'bin'),
363 'dec2.sv_rm_dec.mode', 'dec2.sv_rm_dec.predmode',
364 'dec2.sv_rm_dec.ptype_in',
365 'dec2.sv_rm_dec.dstpred[2:0]', 'dec2.sv_rm_dec.srcpred[2:0]',
366 'dstmask[63:0]', 'srcmask[63:0]',
367 'dregread[4:0]', 'dinvert',
368 'sregread[4:0]', 'sinvert',
369 'core.int.pred__addr[4:0]', 'core.int.pred__data_o[63:0]',
370 'core.int.pred__ren']),
371 ('register augmentation', 'dec', 'closed', [
372 {'comment': 'v3.0b registers'},
373 'dec2.dec_o.RT[4:0]',
374 'dec2.dec_a.RA[4:0]',
375 'dec2.dec_b.RB[4:0]',
376 ('Rdest', [
377 'dec2.o_svdec.reg_in[4:0]',
378 ('dec2.o_svdec.spec[2:0]', 'bin'),
379 'dec2.o_svdec.reg_out[6:0]']),
380 ('Rsrc1', [
381 'dec2.in1_svdec.reg_in[4:0]',
382 ('dec2.in1_svdec.spec[2:0]', 'bin'),
383 'dec2.in1_svdec.reg_out[6:0]']),
384 ('Rsrc1', [
385 'dec2.in2_svdec.reg_in[4:0]',
386 ('dec2.in2_svdec.spec[2:0]', 'bin'),
387 'dec2.in2_svdec.reg_out[6:0]']),
388 {'comment': 'SVP64 registers'},
389 'dec2.rego[6:0]', 'dec2.reg1[6:0]', 'dec2.reg2[6:0]'
390 ]),
391 {'comment': 'svp64 context'},
392 'core_core_vl[6:0]', 'core_core_maxvl[6:0]',
393 'core_core_srcstep[6:0]', 'next_srcstep[6:0]',
394 'core_core_dststep[6:0]',
395 {'comment': 'issue and execute'},
396 'core.core_core_insn_type',
397 (None, 'dec', [
398 'core_rego[6:0]', 'core_reg1[6:0]', 'core_reg2[6:0]']),
399 'issue_i', 'busy_o',
400 {'comment': 'dmi'},
401 'dbg.dmi_req_i', 'dbg.dmi_ack_o',
402 {'comment': 'instruction memory'},
403 'imem.sram.rdport.memory(0)[63:0]',
404 {'comment': 'registers'},
405 # match with soc.regfile.regfiles.IntRegs port names
406 'core.int.rp_src1.memory(0)[63:0]',
407 'core.int.rp_src1.memory(1)[63:0]',
408 'core.int.rp_src1.memory(2)[63:0]',
409 'core.int.rp_src1.memory(3)[63:0]',
410 'core.int.rp_src1.memory(4)[63:0]',
411 'core.int.rp_src1.memory(5)[63:0]',
412 'core.int.rp_src1.memory(6)[63:0]',
413 'core.int.rp_src1.memory(7)[63:0]',
414 'core.int.rp_src1.memory(9)[63:0]',
415 'core.int.rp_src1.memory(10)[63:0]',
416 'core.int.rp_src1.memory(13)[63:0]',
417 ]
418
419 if self.microwatt_mmu:
420 traces += [
421 {'comment': 'microwatt_mmu'},
422 'core.fus.mmu0.alu_mmu0.illegal',
423 'core.fus.mmu0.alu_mmu0.debug0[3:0]',
424 'core.fus.mmu0.alu_mmu0.mmu.state',
425 'core.fus.mmu0.alu_mmu0.mmu.pid[31:0]',
426 'core.fus.mmu0.alu_mmu0.mmu.prtbl[63:0]',
427 {'comment': 'wishbone_memory'},
428 'core.fus.mmu0.alu_mmu0.dcache.stb',
429 'core.fus.mmu0.alu_mmu0.dcache.cyc',
430 'core.fus.mmu0.alu_mmu0.dcache.we',
431 'core.fus.mmu0.alu_mmu0.dcache.ack',
432 'core.fus.mmu0.alu_mmu0.dcache.stall,'
433 ]
434
435 write_gtkw("issuer_simulator.gtkw",
436 "issuer_simulator.vcd",
437 traces, styles, module='top.issuer')
438
439 # add run of instructions
440 sim.add_sync_process(process)
441
442 # optionally, if a wishbone-based ROM is passed in, run that as an
443 # extra emulated process
444 if self.rom is not None:
445 dcache = core.fus.fus["mmu0"].alu.dcache
446 default_mem = self.rom
447 sim.add_sync_process(wrap(wb_get(dcache, default_mem, "DCACHE")))
448
449 with sim.write_vcd("issuer_simulator.vcd"):
450 sim.run()