debugging test_issuer.py general test cases
[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
16
17 from soc.simple.issuer import TestIssuer
18 from soc.experiment.compalu_multi import find_ok # hack
19
20 from soc.simple.test.test_core import (setup_regs, check_regs,
21 wait_for_busy_clear,
22 wait_for_busy_hi)
23 from soc.fu.compunits.test.test_compunit import (setup_test_memory,
24 check_sim_memory)
25
26 # test with ALU data and Logical data
27 from soc.fu.alu.test.test_pipe_caller import ALUTestCase
28 from soc.fu.logical.test.test_pipe_caller import LogicalTestCase
29 from soc.fu.shift_rot.test.test_pipe_caller import ShiftRotTestCase
30 from soc.fu.cr.test.test_pipe_caller import CRTestCase
31 from soc.fu.branch.test.test_pipe_caller import BranchTestCase
32 from soc.fu.ldst.test.test_pipe_caller import LDSTTestCase
33 from soc.simulator.test_sim import GeneralTestCases
34
35
36 def setup_i_memory(imem, startaddr, instructions):
37 mem = imem
38 print ("insn before, init mem", mem.depth, mem.width, mem)
39 for i in range(mem.depth):
40 yield mem._array[i].eq(0)
41 startaddr //= 4 # assume i-mem is 32-bit wide
42 for insn, code in instructions:
43 print ("instr: %06x 0x%x %s" % (4*startaddr, insn, code))
44 yield mem._array[startaddr].eq(insn)
45 startaddr += 1
46
47
48 class TestRunner(FHDLTestCase):
49 def __init__(self, tst_data):
50 super().__init__("run_all")
51 self.test_data = tst_data
52
53 def run_all(self):
54 m = Module()
55 comb = m.d.comb
56 go_insn_i = Signal()
57 pc_i = Signal(32)
58
59 m.submodules.issuer = issuer = TestIssuer()
60 imem = issuer.imem.mem
61 core = issuer.core
62 pdecode2 = core.pdecode2
63 l0 = core.l0
64
65 comb += issuer.pc_i.data.eq(pc_i)
66 comb += issuer.go_insn_i.eq(go_insn_i)
67
68 # nmigen Simulation
69 sim = Simulator(m)
70 sim.add_clock(1e-6)
71
72 def process():
73
74 for test in self.test_data:
75 print(test.name)
76 program = test.program
77 self.subTest(test.name)
78 print ("regs", test.regs)
79 print ("sprs", test.sprs)
80 print ("cr", test.cr)
81 print ("mem", test.mem)
82 print ("msr", test.msr)
83 print ("assem", program.assembly)
84 sim = ISA(pdecode2, test.regs, test.sprs, test.cr, test.mem,
85 test.msr)
86 gen = list(program.generate_instructions())
87 instructions = list(zip(gen, program.assembly.splitlines()))
88
89 pc = 0 # start address
90
91 yield from setup_i_memory(imem, pc, instructions)
92 yield from setup_test_memory(l0, sim)
93 yield from setup_regs(core, test)
94
95 yield pc_i.eq(pc)
96 yield issuer.pc_i.ok.eq(1)
97
98 index = sim.pc.CIA.value//4
99 while index < len(instructions):
100 ins, code = instructions[index]
101
102 print("instruction: 0x{:X}".format(ins & 0xffffffff))
103 print(code)
104
105 # start the instruction
106 yield go_insn_i.eq(1)
107 yield
108 yield issuer.pc_i.ok.eq(0) # don't change PC from now on
109 yield go_insn_i.eq(0) # and don't issue a new insn
110
111 # wait until executed
112 yield from wait_for_busy_hi(core)
113 yield from wait_for_busy_clear(core)
114
115 print ("sim", code)
116 # call simulated operation
117 opname = code.split(' ')[0]
118 yield from sim.call(opname)
119 index = sim.pc.CIA.value//4
120
121 # register check
122 yield from check_regs(self, sim, core, test, code)
123
124 # Memory check
125 yield from check_sim_memory(self, l0, sim, code)
126
127 sim.add_sync_process(process)
128 with sim.write_vcd("issuer_simulator.vcd",
129 traces=[]):
130 sim.run()
131
132
133 if __name__ == "__main__":
134 unittest.main(exit=False)
135 suite = unittest.TestSuite()
136 #suite.addTest(TestRunner(GeneralTestCases.test_data))
137 suite.addTest(TestRunner(LDSTTestCase.test_data))
138 suite.addTest(TestRunner(CRTestCase.test_data))
139 suite.addTest(TestRunner(ShiftRotTestCase.test_data))
140 suite.addTest(TestRunner(LogicalTestCase.test_data))
141 suite.addTest(TestRunner(ALUTestCase.test_data))
142 suite.addTest(TestRunner(BranchTestCase.test_data))
143
144 runner = unittest.TextTestRunner()
145 runner.run(suite)
146