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