Simplify obtaining the PC from the register file
[soc.git] / src / soc / simple / test / test_core.py
1 """simple core test
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.power_decoder import create_pdecode
14 from soc.decoder.power_decoder2 import PowerDecode2
15 from soc.decoder.selectable_int import SelectableInt
16 from soc.decoder.isa.all import ISA
17 from soc.decoder.power_enums import SPR, spr_dict, Function, XER_bits
18 from soc.config.test.test_loadstore import TestMemPspec
19 from soc.config.endian import bigendian
20
21 from soc.simple.core import NonProductionCore
22 from soc.experiment.compalu_multi import find_ok # hack
23
24 from soc.fu.compunits.test.test_compunit import (setup_test_memory,
25 check_sim_memory)
26
27 # test with ALU data and Logical data
28 from soc.fu.alu.test.test_pipe_caller import ALUTestCase
29 from soc.fu.logical.test.test_pipe_caller import LogicalTestCase
30 from soc.fu.shift_rot.test.test_pipe_caller import ShiftRotTestCase
31 from soc.fu.cr.test.test_pipe_caller import CRTestCase
32 from soc.fu.branch.test.test_pipe_caller import BranchTestCase
33 from soc.fu.ldst.test.test_pipe_caller import LDSTTestCase
34 from soc.regfile.util import spr_to_fast_reg
35
36
37 def setup_regs(pdecode2, core, test):
38
39 # set up INT regfile, "direct" write (bypass rd/write ports)
40 intregs = core.regs.int
41 for i in range(32):
42 if intregs.unary:
43 yield intregs.regs[i].reg.eq(test.regs[i])
44 else:
45 yield intregs.memory._array[i].eq(test.regs[i])
46 yield Settle()
47
48 # set up CR regfile, "direct" write across all CRs
49 cr = test.cr
50 crregs = core.regs.cr
51 #cr = int('{:32b}'.format(cr)[::-1], 2)
52 print("setup cr reg", hex(cr))
53 for i in range(8):
54 #j = 7-i
55 cri = (cr >> (i*4)) & 0xf
56 #cri = int('{:04b}'.format(cri)[::-1], 2)
57 print("setup cr reg", hex(cri), i,
58 crregs.regs[i].reg.shape())
59 yield crregs.regs[i].reg.eq(cri)
60
61 # set up XER. "direct" write (bypass rd/write ports)
62 xregs = core.regs.xer
63 print("setup sprs", test.sprs)
64 xer = None
65 if 'XER' in test.sprs:
66 xer = test.sprs['XER']
67 if 1 in test.sprs:
68 xer = test.sprs[1]
69 if xer is not None:
70 if isinstance(xer, int):
71 xer = SelectableInt(xer, 64)
72 sobit = xer[XER_bits['SO']].value
73 yield xregs.regs[xregs.SO].reg.eq(sobit)
74 cabit = xer[XER_bits['CA']].value
75 ca32bit = xer[XER_bits['CA32']].value
76 yield xregs.regs[xregs.CA].reg.eq(Cat(cabit, ca32bit))
77 ovbit = xer[XER_bits['OV']].value
78 ov32bit = xer[XER_bits['OV32']].value
79 yield xregs.regs[xregs.OV].reg.eq(Cat(ovbit, ov32bit))
80 print("setting XER so %d ca %d ca32 %d ov %d ov32 %d" %
81 (sobit, cabit, ca32bit, ovbit, ov32bit))
82 else:
83 yield xregs.regs[xregs.SO].reg.eq(0)
84 yield xregs.regs[xregs.OV].reg.eq(0)
85 yield xregs.regs[xregs.CA].reg.eq(0)
86
87 # setting both fast and slow SPRs from test data
88
89 fregs = core.regs.fast
90 sregs = core.regs.spr
91 for sprname, val in test.sprs.items():
92 if isinstance(val, SelectableInt):
93 val = val.value
94 if isinstance(sprname, int):
95 sprname = spr_dict[sprname].SPR
96 if sprname == 'XER':
97 continue
98 fast = spr_to_fast_reg(sprname)
99 if fast is None:
100 # match behaviour of SPRMap in power_decoder2.py
101 for i, x in enumerate(SPR):
102 if sprname == x.name:
103 yield sregs[i].reg.eq(val)
104 print("setting slow SPR %d (%s) to %x" %
105 (i, sprname, val))
106 else:
107 yield fregs.regs[fast].reg.eq(val)
108 print("setting fast reg %d (%s) to %x" %
109 (fast, sprname, val))
110
111 # allow changes to settle before reporting on XER
112 yield Settle()
113
114 # XER
115 so = yield xregs.regs[xregs.SO].reg
116 ov = yield xregs.regs[xregs.OV].reg
117 ca = yield xregs.regs[xregs.CA].reg
118 oe = yield pdecode2.e.do.oe.oe
119 oe_ok = yield pdecode2.e.do.oe.oe_ok
120
121 print("before: so/ov-32/ca-32", so, bin(ov), bin(ca))
122 print("oe:", oe, oe_ok)
123
124
125 def check_regs(dut, sim, core, test, code):
126 # int regs
127 intregs = []
128 for i in range(32):
129 if core.regs.int.unary:
130 rval = yield core.regs.int.regs[i].reg
131 else:
132 rval = yield core.regs.int.memory._array[i]
133 intregs.append(rval)
134 print("int regs", list(map(hex, intregs)))
135 for i in range(32):
136 simregval = sim.gpr[i].asint()
137 dut.assertEqual(simregval, intregs[i],
138 "int reg %d not equal %s" % (i, repr(code)))
139
140 # CRs
141 crregs = []
142 for i in range(8):
143 rval = yield core.regs.cr.regs[i].reg
144 crregs.append(rval)
145 print("cr regs", list(map(hex, crregs)))
146 for i in range(8):
147 rval = crregs[i]
148 cri = sim.crl[7-i].get_range().value
149 print("cr reg", i, hex(cri), i, hex(rval))
150 # XXX https://bugs.libre-soc.org/show_bug.cgi?id=363
151 dut.assertEqual(cri, rval,
152 "cr reg %d not equal %s" % (i, repr(code)))
153
154 # XER
155 xregs = core.regs.xer
156 so = yield xregs.regs[xregs.SO].reg
157 ov = yield xregs.regs[xregs.OV].reg
158 ca = yield xregs.regs[xregs.CA].reg
159
160 print("sim SO", sim.spr['XER'][XER_bits['SO']])
161 e_so = sim.spr['XER'][XER_bits['SO']].value
162 e_ov = sim.spr['XER'][XER_bits['OV']].value
163 e_ov32 = sim.spr['XER'][XER_bits['OV32']].value
164 e_ca = sim.spr['XER'][XER_bits['CA']].value
165 e_ca32 = sim.spr['XER'][XER_bits['CA32']].value
166
167 e_ov = e_ov | (e_ov32 << 1)
168 e_ca = e_ca | (e_ca32 << 1)
169
170 print("after: so/ov-32/ca-32", so, bin(ov), bin(ca))
171 dut.assertEqual(e_so, so, "so mismatch %s" % (repr(code)))
172 dut.assertEqual(e_ov, ov, "ov mismatch %s" % (repr(code)))
173 dut.assertEqual(e_ca, ca, "ca mismatch %s" % (repr(code)))
174
175 # Check the PC as well
176 state = core.regs.state
177 pc = yield state.r_ports['cia'].data_o
178 e_pc = sim.pc.CIA.value
179 dut.assertEqual(e_pc, pc)
180
181
182 def wait_for_busy_hi(cu):
183 while True:
184 busy_o = yield cu.busy_o
185 terminate_o = yield cu.core_terminate_o
186 if busy_o:
187 print("busy/terminate:", busy_o, terminate_o)
188 break
189 print("!busy", busy_o, terminate_o)
190 yield
191
192
193 def set_issue(core, dec2, sim):
194 yield core.issue_i.eq(1)
195 yield
196 yield core.issue_i.eq(0)
197 yield from wait_for_busy_hi(core)
198
199
200 def wait_for_busy_clear(cu):
201 while True:
202 busy_o = yield cu.busy_o
203 terminate_o = yield cu.core_terminate_o
204 if not busy_o:
205 print("busy/terminate:", busy_o, terminate_o)
206 break
207 print("busy",)
208 yield
209
210
211 class TestRunner(FHDLTestCase):
212 def __init__(self, tst_data):
213 super().__init__("run_all")
214 self.test_data = tst_data
215
216 def run_all(self):
217 m = Module()
218 comb = m.d.comb
219 instruction = Signal(32)
220 ivalid_i = Signal()
221
222 pspec = TestMemPspec(ldst_ifacetype='testpi',
223 imem_ifacetype='',
224 addr_wid=48,
225 mask_wid=8,
226 reg_wid=64)
227
228 m.submodules.core = core = NonProductionCore(pspec)
229 pdecode2 = core.pdecode2
230 l0 = core.l0
231
232 comb += core.raw_opcode_i.eq(instruction)
233 comb += core.ivalid_i.eq(ivalid_i)
234
235 # temporary hack: says "go" immediately for both address gen and ST
236 ldst = core.fus.fus['ldst0']
237 m.d.comb += ldst.ad.go.eq(ldst.ad.rel) # link addr-go direct to rel
238 m.d.comb += ldst.st.go.eq(ldst.st.rel) # link store-go direct to rel
239
240 # nmigen Simulation
241 sim = Simulator(m)
242 sim.add_clock(1e-6)
243
244 def process():
245 yield core.issue_i.eq(0)
246 yield
247
248 for test in self.test_data:
249 print(test.name)
250 program = test.program
251 self.subTest(test.name)
252 sim = ISA(pdecode2, test.regs, test.sprs, test.cr, test.mem,
253 test.msr,
254 bigendian=bigendian)
255 gen = program.generate_instructions()
256 instructions = list(zip(gen, program.assembly.splitlines()))
257
258 yield from setup_test_memory(l0, sim)
259 yield from setup_regs(core, test)
260
261 index = sim.pc.CIA.value//4
262 while index < len(instructions):
263 ins, code = instructions[index]
264
265 print("instruction: 0x{:X}".format(ins & 0xffffffff))
266 print(code)
267
268 # ask the decoder to decode this binary data (endian'd)
269 yield core.bigendian_i.eq(bigendian) # little / big?
270 yield instruction.eq(ins) # raw binary instr.
271 yield ivalid_i.eq(1)
272 yield Settle()
273 # fn_unit = yield pdecode2.e.fn_unit
274 #fuval = self.funit.value
275 #self.assertEqual(fn_unit & fuval, fuval)
276
277 # set operand and get inputs
278 yield from set_issue(core, pdecode2, sim)
279 yield Settle()
280
281 yield from wait_for_busy_clear(core)
282 yield ivalid_i.eq(0)
283 yield
284
285 print("sim", code)
286 # call simulated operation
287 opname = code.split(' ')[0]
288 yield from sim.call(opname)
289 index = sim.pc.CIA.value//4
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 sim.add_sync_process(process)
298 with sim.write_vcd("core_simulator.vcd", "core_simulator.gtkw",
299 traces=[]):
300 sim.run()
301
302
303 if __name__ == "__main__":
304 unittest.main(exit=False)
305 suite = unittest.TestSuite()
306 suite.addTest(TestRunner(LDSTTestCase().test_data))
307 suite.addTest(TestRunner(CRTestCase().test_data))
308 suite.addTest(TestRunner(ShiftRotTestCase().test_data))
309 suite.addTest(TestRunner(LogicalTestCase().test_data))
310 suite.addTest(TestRunner(ALUTestCase().test_data))
311 suite.addTest(TestRunner(BranchTestCase().test_data))
312
313 runner = unittest.TextTestRunner()
314 runner.run(suite)