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