b6cc9f6edc66294219826125fb506e0eb2d41c5b
[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(ifacetype="test_bare_wb")
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 gen = list(program.generate_instructions())
85 insncode = program.assembly.splitlines()
86 instructions = list(zip(gen, insncode))
87 sim = ISA(pdecode2, test.regs, test.sprs, test.cr, test.mem,
88 test.msr,
89 initial_insns=gen, respect_pc=True,
90 disassembly=insncode)
91
92 pc = 0 # start address
93
94 yield from setup_i_memory(imem, pc, instructions)
95 yield from setup_test_memory(l0, sim)
96 yield from setup_regs(core, test)
97
98 yield pc_i.eq(pc)
99 yield issuer.pc_i.ok.eq(1)
100
101 index = sim.pc.CIA.value//4
102 while index < len(instructions):
103 ins, code = instructions[index]
104
105 print("instruction: 0x{:X}".format(ins & 0xffffffff))
106 print(index, code)
107
108 # start the instruction
109 yield go_insn_i.eq(1)
110 yield
111 yield issuer.pc_i.ok.eq(0) # don't change PC from now on
112 yield go_insn_i.eq(0) # and don't issue a new insn
113
114 # wait until executed
115 yield from wait_for_busy_hi(core)
116 yield from wait_for_busy_clear(core)
117
118 print ("sim", code)
119 # call simulated operation
120 opname = code.split(' ')[0]
121 yield from sim.call(opname)
122 yield Settle()
123 index = sim.pc.CIA.value//4
124
125 # register check
126 yield from check_regs(self, sim, core, test, code)
127
128 # Memory check
129 yield from check_sim_memory(self, l0, sim, code)
130
131 sim.add_sync_process(process)
132 with sim.write_vcd("issuer_simulator.vcd",
133 traces=[]):
134 sim.run()
135
136
137 if __name__ == "__main__":
138 unittest.main(exit=False)
139 suite = unittest.TestSuite()
140 suite.addTest(TestRunner(GeneralTestCases.test_data))
141 suite.addTest(TestRunner(LDSTTestCase.test_data))
142 suite.addTest(TestRunner(CRTestCase.test_data))
143 suite.addTest(TestRunner(ShiftRotTestCase.test_data))
144 suite.addTest(TestRunner(LogicalTestCase.test_data))
145 suite.addTest(TestRunner(ALUTestCase.test_data))
146 suite.addTest(TestRunner(BranchTestCase.test_data))
147
148 runner = unittest.TextTestRunner()
149 runner.run(suite)
150