added comment to teststate
[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 * https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
7 """
8 from nmigen import Module, Signal
9 from nmigen.hdl.xfrm import ResetInserter
10 from copy import copy
11
12 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
13 # Also, check out the cxxsim nmigen branch, and latest yosys from git
14 from nmutil.sim_tmp_alternative import Simulator, Settle
15
16 from openpower.decoder.isa.caller import SVP64State
17 from openpower.decoder.isa.all import ISA
18 from openpower.endian import bigendian
19
20 from soc.simple.issuer import TestIssuerInternal
21
22 from soc.config.test.test_loadstore import TestMemPspec
23 from soc.simple.test.test_core import (setup_regs, check_regs, check_mem,
24 wait_for_busy_clear,
25 wait_for_busy_hi)
26 from soc.fu.compunits.test.test_compunit import (setup_tst_memory,
27 check_sim_memory)
28 from soc.debug.dmi import DBGCore, DBGCtrl, DBGStat
29 from nmutil.util import wrap
30 from soc.experiment.test.test_mmu_dcache import wb_get
31 from openpower.test.state import TestState, StateRunner
32 from openpower.test.runner import TestRunnerBase
33
34
35 def setup_i_memory(imem, startaddr, instructions):
36 mem = imem
37 print("insn before, init mem", mem.depth, mem.width, mem,
38 len(instructions))
39 for i in range(mem.depth):
40 yield mem._array[i].eq(0)
41 yield Settle()
42 startaddr //= 4 # instructions are 32-bit
43 if mem.width == 32:
44 mask = ((1 << 32)-1)
45 for ins in instructions:
46 if isinstance(ins, tuple):
47 insn, code = ins
48 else:
49 insn, code = ins, ''
50 insn = insn & 0xffffffff
51 yield mem._array[startaddr].eq(insn)
52 yield Settle()
53 if insn != 0:
54 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
55 startaddr += 1
56 startaddr = startaddr & mask
57 return
58
59 # 64 bit
60 mask = ((1 << 64)-1)
61 for ins in instructions:
62 if isinstance(ins, tuple):
63 insn, code = ins
64 else:
65 insn, code = ins, ''
66 insn = insn & 0xffffffff
67 msbs = (startaddr >> 1) & mask
68 val = yield mem._array[msbs]
69 if insn != 0:
70 print("before set", hex(4*startaddr),
71 hex(msbs), hex(val), hex(insn))
72 lsb = 1 if (startaddr & 1) else 0
73 val = (val | (insn << (lsb*32)))
74 val = val & mask
75 yield mem._array[msbs].eq(val)
76 yield Settle()
77 if insn != 0:
78 print("after set", hex(4*startaddr), hex(msbs), hex(val))
79 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
80 startaddr += 1
81 startaddr = startaddr & mask
82
83
84 def set_dmi(dmi, addr, data):
85 yield dmi.req_i.eq(1)
86 yield dmi.addr_i.eq(addr)
87 yield dmi.din.eq(data)
88 yield dmi.we_i.eq(1)
89 while True:
90 ack = yield dmi.ack_o
91 if ack:
92 break
93 yield
94 yield
95 yield dmi.req_i.eq(0)
96 yield dmi.addr_i.eq(0)
97 yield dmi.din.eq(0)
98 yield dmi.we_i.eq(0)
99 yield
100
101
102 def get_dmi(dmi, addr):
103 yield dmi.req_i.eq(1)
104 yield dmi.addr_i.eq(addr)
105 yield dmi.din.eq(0)
106 yield dmi.we_i.eq(0)
107 while True:
108 ack = yield dmi.ack_o
109 if ack:
110 break
111 yield
112 yield # wait one
113 data = yield dmi.dout # get data after ack valid for 1 cycle
114 yield dmi.req_i.eq(0)
115 yield dmi.addr_i.eq(0)
116 yield dmi.we_i.eq(0)
117 yield
118 return data
119
120
121 class HDLRunner(StateRunner):
122 def __init__(self, dut, m, pspec):
123 super().__init__("hdl", HDLRunner)
124
125 self.dut = dut
126 self.pc_i = Signal(32)
127 self.svstate_i = Signal(64)
128
129 #hard_reset = Signal(reset_less=True)
130 self.issuer = TestIssuerInternal(pspec)
131 # use DMI RESET command instead, this does actually work though
132 #issuer = ResetInserter({'coresync': hard_reset,
133 # 'sync': hard_reset})(issuer)
134 m.submodules.issuer = self.issuer
135 self.dmi = self.issuer.dbg.dmi
136
137 comb = m.d.comb
138 comb += self.issuer.pc_i.data.eq(self.pc_i)
139 comb += self.issuer.svstate_i.data.eq(self.svstate_i)
140
141 def prepare_for_test(self, test):
142 self.test = test
143
144 # set up bigendian (TODO: don't do this, use MSR)
145 yield self.issuer.core_bigendian_i.eq(bigendian)
146 yield Settle()
147
148 yield
149 yield
150 yield
151 yield
152
153 def setup_during_test(self):
154 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
155 yield
156
157 def run_test(self, instructions):
158 """run_hdl_state - runs a TestIssuer nmigen HDL simulation
159 """
160
161 imem = self.issuer.imem._get_memory()
162 core = self.issuer.core
163 dmi = self.issuer.dbg.dmi
164 pdecode2 = self.issuer.pdecode2
165 l0 = core.l0
166 hdl_states = []
167
168 # establish the TestIssuer context (mem, regs etc)
169
170 pc = 0 # start address
171 counter = 0 # test to pause/start
172
173 yield from setup_i_memory(imem, pc, instructions)
174 yield from setup_tst_memory(l0, self.test.mem)
175 yield from setup_regs(pdecode2, core, self.test)
176
177 # set PC and SVSTATE
178 yield self.pc_i.eq(pc)
179 yield self.issuer.pc_i.ok.eq(1)
180
181 # copy initial SVSTATE
182 initial_svstate = copy(self.test.svstate)
183 if isinstance(initial_svstate, int):
184 initial_svstate = SVP64State(initial_svstate)
185 yield self.svstate_i.eq(initial_svstate.value)
186 yield self.issuer.svstate_i.ok.eq(1)
187 yield
188
189 print("instructions", instructions)
190
191 # run the loop of the instructions on the current test
192 index = (yield self.issuer.cur_state.pc) // 4
193 while index < len(instructions):
194 ins, code = instructions[index]
195
196 print("hdl instr: 0x{:X}".format(ins & 0xffffffff))
197 print(index, code)
198
199 if counter == 0:
200 # start the core
201 yield
202 yield from set_dmi(dmi, DBGCore.CTRL,
203 1<<DBGCtrl.START)
204 yield self.issuer.pc_i.ok.eq(0) # no change PC after this
205 yield self.issuer.svstate_i.ok.eq(0) # ditto
206 yield
207 yield
208
209 counter = counter + 1
210
211 # wait until executed
212 while not (yield self.issuer.insn_done):
213 yield
214
215 yield Settle()
216
217 index = (yield self.issuer.cur_state.pc) // 4
218
219 terminated = yield self.issuer.dbg.terminated_o
220 print("terminated", terminated)
221
222 if index < len(instructions):
223 # Get HDL mem and state
224 state = yield from TestState("hdl", core, self.dut,
225 code)
226 hdl_states.append(state)
227
228 if index >= len(instructions):
229 print ("index over, send dmi stop")
230 # stop at end
231 yield from set_dmi(dmi, DBGCore.CTRL,
232 1<<DBGCtrl.STOP)
233 yield
234 yield
235
236 terminated = yield self.issuer.dbg.terminated_o
237 print("terminated(2)", terminated)
238 if terminated:
239 break
240
241 return hdl_states
242
243 def end_test(self):
244 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
245 yield
246 yield
247
248 # TODO, here is where the static (expected) results
249 # can be checked: register check (TODO, memory check)
250 # see https://bugs.libre-soc.org/show_bug.cgi?id=686#c51
251 # yield from check_regs(self, sim, core, test, code,
252 # >>>expected_data<<<)
253
254 # get CR
255 cr = yield from get_dmi(self.dmi, DBGCore.CR)
256 print("after test %s cr value %x" % (self.test.name, cr))
257
258 # get XER
259 xer = yield from get_dmi(self.dmi, DBGCore.XER)
260 print("after test %s XER value %x" % (self.test.name, xer))
261
262 # test of dmi reg get
263 for int_reg in range(32):
264 yield from set_dmi(self.dmi, DBGCore.GSPR_IDX, int_reg)
265 value = yield from get_dmi(self.dmi, DBGCore.GSPR_DATA)
266
267 print("after test %s reg %2d value %x" %
268 (self.test.name, int_reg, value))
269
270 # pull a reset
271 yield from set_dmi(self.dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
272 yield
273
274
275 class TestRunner(TestRunnerBase):
276 def __init__(self, tst_data, microwatt_mmu=False, rom=None,
277 svp64=True, run_hdl=True, run_sim=True):
278 if run_hdl:
279 run_hdl = HDLRunner
280 super().__init__(tst_data, microwatt_mmu=microwatt_mmu,
281 rom=rom,
282 svp64=svp64, run_hdl=run_hdl, run_sim=run_sim)
283