move microwatt_mmu bool variable to pspec
[soc.git] / src / soc / simple / test / test_issuer.py
1 """simple core test, runs instructions from a TestMemory
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 nmigen.cli import rtlil
15 import unittest
16 from soc.decoder.isa.caller import special_sprs
17 from soc.decoder.isa.all import ISA
18 from soc.decoder.power_enums import Function, XER_bits
19 from soc.config.endian import bigendian
20
21 from soc.decoder.power_decoder import create_pdecode
22 from soc.decoder.power_decoder2 import PowerDecode2
23
24 from soc.simple.issuer import TestIssuerInternal
25 from soc.experiment.compalu_multi import find_ok # hack
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
35 # test with ALU data and Logical data
36 from soc.fu.alu.test.test_pipe_caller import ALUTestCase
37 from soc.fu.div.test.test_pipe_caller import DivTestCases
38 from soc.fu.logical.test.test_pipe_caller import LogicalTestCase
39 from soc.fu.shift_rot.test.test_pipe_caller import ShiftRotTestCase
40 from soc.fu.cr.test.test_pipe_caller import CRTestCase
41 #from soc.fu.branch.test.test_pipe_caller import BranchTestCase
42 #from soc.fu.spr.test.test_pipe_caller import SPRTestCase
43 from soc.fu.ldst.test.test_pipe_caller import LDSTTestCase
44 from soc.simulator.test_sim import (GeneralTestCases, AttnTestCase)
45 #from soc.simulator.test_helloworld_sim import HelloTestCases
46
47
48 def setup_i_memory(imem, startaddr, instructions):
49 mem = imem
50 print("insn before, init mem", mem.depth, mem.width, mem,
51 len(instructions))
52 for i in range(mem.depth):
53 yield mem._array[i].eq(0)
54 yield Settle()
55 startaddr //= 4 # instructions are 32-bit
56 if mem.width == 32:
57 mask = ((1 << 32)-1)
58 for ins in instructions:
59 if isinstance(ins, tuple):
60 insn, code = ins
61 else:
62 insn, code = ins, ''
63 insn = insn & 0xffffffff
64 yield mem._array[startaddr].eq(insn)
65 yield Settle()
66 if insn != 0:
67 print("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
68 startaddr += 1
69 startaddr = startaddr & mask
70 return
71
72 # 64 bit
73 mask = ((1 << 64)-1)
74 for ins in instructions:
75 if isinstance(ins, tuple):
76 insn, code = ins
77 else:
78 insn, code = ins, ''
79 insn = insn & 0xffffffff
80 msbs = (startaddr >> 1) & mask
81 val = yield mem._array[msbs]
82 if insn != 0:
83 print("before set", hex(4*startaddr),
84 hex(msbs), hex(val), hex(insn))
85 lsb = 1 if (startaddr & 1) else 0
86 val = (val | (insn << (lsb*32)))
87 val = val & mask
88 yield mem._array[msbs].eq(val)
89 yield Settle()
90 if insn != 0:
91 print("after set", hex(4*startaddr), hex(msbs), hex(val))
92 print("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
93 startaddr += 1
94 startaddr = startaddr & mask
95
96
97 def set_dmi(dmi, addr, data):
98 yield dmi.req_i.eq(1)
99 yield dmi.addr_i.eq(addr)
100 yield dmi.din.eq(data)
101 yield dmi.we_i.eq(1)
102 while True:
103 ack = yield dmi.ack_o
104 if ack:
105 break
106 yield
107 yield
108 yield dmi.req_i.eq(0)
109 yield dmi.addr_i.eq(0)
110 yield dmi.din.eq(0)
111 yield dmi.we_i.eq(0)
112 yield
113
114
115 def get_dmi(dmi, addr):
116 yield dmi.req_i.eq(1)
117 yield dmi.addr_i.eq(addr)
118 yield dmi.din.eq(0)
119 yield dmi.we_i.eq(0)
120 while True:
121 ack = yield dmi.ack_o
122 if ack:
123 break
124 yield
125 yield # wait one
126 data = yield dmi.dout # get data after ack valid for 1 cycle
127 yield dmi.req_i.eq(0)
128 yield dmi.addr_i.eq(0)
129 yield dmi.we_i.eq(0)
130 yield
131 return data
132
133
134 class TestRunner(FHDLTestCase):
135 def __init__(self, tst_data, microwatt_mmu=False):
136 super().__init__("run_all")
137 self.test_data = tst_data
138 self.microwatt_mmu = microwatt_mmu
139
140 def run_all(self):
141 m = Module()
142 comb = m.d.comb
143 pc_i = Signal(32)
144
145 pspec = TestMemPspec(ldst_ifacetype='test_bare_wb',
146 imem_ifacetype='test_bare_wb',
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 mmu=self.microwatt_mmu,
156 reg_wid=64)
157 m.submodules.issuer = issuer = TestIssuerInternal(pspec)
158 imem = issuer.imem._get_memory()
159 core = issuer.core
160 dmi = issuer.dbg.dmi
161 pdecode2 = issuer.pdecode2
162 l0 = core.l0
163
164 # copy of the decoder for simulator
165 simdec = create_pdecode()
166 simdec2 = PowerDecode2(simdec)
167 m.submodules.simdec2 = simdec2 # pain in the neck
168
169 # run core clock at same rate as test clock
170 intclk = ClockSignal("coresync")
171 comb += intclk.eq(ClockSignal())
172
173 comb += issuer.pc_i.data.eq(pc_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 for test in self.test_data:
187
188 # pull a reset
189 #yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.RESET)
190
191 # set up bigendian (TODO: don't do this, use MSR)
192 yield issuer.core_bigendian_i.eq(bigendian)
193 yield Settle()
194
195 yield
196 yield
197 yield
198 yield
199
200 print(test.name)
201 program = test.program
202 self.subTest(test.name)
203 print("regs", test.regs)
204 print("sprs", test.sprs)
205 print("cr", test.cr)
206 print("mem", test.mem)
207 print("msr", test.msr)
208 print("assem", program.assembly)
209 gen = list(program.generate_instructions())
210 insncode = program.assembly.splitlines()
211 instructions = list(zip(gen, insncode))
212 sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
213 test.msr,
214 initial_insns=gen, respect_pc=True,
215 disassembly=insncode,
216 bigendian=bigendian)
217
218 pc = 0 # start address
219 counter = 0 # test to pause/start
220
221 yield from setup_i_memory(imem, pc, instructions)
222 yield from setup_test_memory(l0, sim)
223 yield from setup_regs(pdecode2, core, test)
224
225 yield pc_i.eq(pc)
226 yield issuer.pc_i.ok.eq(1)
227 yield
228
229 print("instructions", instructions)
230
231 index = sim.pc.CIA.value//4
232 while index < len(instructions):
233 ins, code = instructions[index]
234
235 print("instruction: 0x{:X}".format(ins & 0xffffffff))
236 print(index, code)
237
238 if counter == 0:
239 # start the core
240 yield
241 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.START)
242 yield issuer.pc_i.ok.eq(0) # no change PC after this
243 yield
244 yield
245
246 counter = counter + 1
247
248 # wait until executed
249 yield from wait_for_busy_hi(core)
250 yield from wait_for_busy_clear(core)
251
252 # set up simulated instruction (in simdec2)
253 try:
254 yield from sim.setup_one()
255 except KeyError: # indicates instruction not in imem: stop
256 break
257 yield Settle()
258
259 # call simulated operation
260 print("sim", code)
261 yield from sim.execute_one()
262 yield Settle()
263 index = sim.pc.CIA.value//4
264
265 terminated = yield issuer.dbg.terminated_o
266 print("terminated", terminated)
267
268 if index >= len(instructions):
269 print ("index over, send dmi stop")
270 # stop at end
271 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
272 yield
273 yield
274
275 # wait one cycle for registers to settle
276 yield
277
278 # register check
279 yield from check_regs(self, sim, core, test, code)
280
281 # Memory check
282 yield from check_sim_memory(self, l0, sim, code)
283
284 terminated = yield issuer.dbg.terminated_o
285 print("terminated(2)", terminated)
286 if terminated:
287 break
288
289 # stop at end
290 yield from set_dmi(dmi, DBGCore.CTRL, 1<<DBGCtrl.STOP)
291 yield
292 yield
293
294 # get CR
295 cr = yield from get_dmi(dmi, DBGCore.CR)
296 print ("after test %s cr value %x" % (test.name, cr))
297
298 # get XER
299 xer = yield from get_dmi(dmi, DBGCore.XER)
300 print ("after test %s XER value %x" % (test.name, xer))
301
302 # test of dmi reg get
303 for int_reg in range(32):
304 yield from set_dmi(dmi, DBGCore.GSPR_IDX, int_reg)
305 value = yield from get_dmi(dmi, DBGCore.GSPR_DATA)
306
307 print ("after test %s reg %2d value %x" % \
308 (test.name, int_reg, value))
309
310 sim.add_sync_process(process)
311 with sim.write_vcd("issuer_simulator.vcd","issuer_simulator.gtkw",
312 traces=[]):
313 sim.run()
314
315
316 if __name__ == "__main__":
317 unittest.main(exit=False)
318 suite = unittest.TestSuite()
319 # suite.addTest(TestRunner(HelloTestCases.test_data))
320 suite.addTest(TestRunner(DivTestCases().test_data))
321 # suite.addTest(TestRunner(AttnTestCase.test_data))
322 suite.addTest(TestRunner(GeneralTestCases.test_data))
323 suite.addTest(TestRunner(LDSTTestCase().test_data))
324 suite.addTest(TestRunner(CRTestCase().test_data))
325 suite.addTest(TestRunner(ShiftRotTestCase().test_data))
326 suite.addTest(TestRunner(LogicalTestCase().test_data))
327 suite.addTest(TestRunner(ALUTestCase().test_data))
328 # suite.addTest(TestRunner(BranchTestCase.test_data))
329 # suite.addTest(TestRunner(SPRTestCase.test_data))
330
331 runner = unittest.TextTestRunner()
332 runner.run(suite)