fetch instructions from bare wishbone fetch unit
[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 yield Settle()
42 startaddr //= 4 # instructions are 32-bit
43 mask = ((1<<64)-1)
44 for insn, code in instructions:
45 msbs = (startaddr>>1) & mask
46 val = yield mem._array[msbs]
47 print ("before set", hex(startaddr), hex(msbs), hex(val))
48 lsb = 1 if (startaddr & 1) else 0
49 val = (val | (insn << (lsb*32))) & mask
50 yield mem._array[msbs].eq(val)
51 yield Settle()
52 print ("after set", hex(startaddr), hex(msbs), hex(val))
53 print ("instr: %06x 0x%x %s %08x" % (4*startaddr, insn, code, val))
54 startaddr += 1
55 startaddr = startaddr & mask
56
57
58 class TestRunner(FHDLTestCase):
59 def __init__(self, tst_data):
60 super().__init__("run_all")
61 self.test_data = tst_data
62
63 def run_all(self):
64 m = Module()
65 comb = m.d.comb
66 go_insn_i = Signal()
67 pc_i = Signal(32)
68
69 m.submodules.issuer = issuer = TestIssuer(ifacetype="test_bare_wb",
70 imemtype="test_bare_wb")
71 imem = issuer.imem._get_memory()
72 core = issuer.core
73 pdecode2 = core.pdecode2
74 l0 = core.l0
75
76 comb += issuer.pc_i.data.eq(pc_i)
77 comb += issuer.go_insn_i.eq(go_insn_i)
78
79 # nmigen Simulation
80 sim = Simulator(m)
81 sim.add_clock(1e-6)
82
83 def process():
84
85 for test in self.test_data:
86 print(test.name)
87 program = test.program
88 self.subTest(test.name)
89 print ("regs", test.regs)
90 print ("sprs", test.sprs)
91 print ("cr", test.cr)
92 print ("mem", test.mem)
93 print ("msr", test.msr)
94 print ("assem", program.assembly)
95 gen = list(program.generate_instructions())
96 insncode = program.assembly.splitlines()
97 instructions = list(zip(gen, insncode))
98 sim = ISA(pdecode2, test.regs, test.sprs, test.cr, test.mem,
99 test.msr,
100 initial_insns=gen, respect_pc=True,
101 disassembly=insncode)
102
103 pc = 0 # start address
104
105 yield from setup_i_memory(imem, pc, instructions)
106 yield from setup_test_memory(l0, sim)
107 yield from setup_regs(core, test)
108
109 yield pc_i.eq(pc)
110 yield issuer.pc_i.ok.eq(1)
111
112 index = sim.pc.CIA.value//4
113 while index < len(instructions):
114 ins, code = instructions[index]
115
116 print("instruction: 0x{:X}".format(ins & 0xffffffff))
117 print(index, code)
118
119 # start the instruction
120 yield go_insn_i.eq(1)
121 yield
122 yield issuer.pc_i.ok.eq(0) # don't change PC from now on
123 yield go_insn_i.eq(0) # and don't issue a new insn
124
125 # wait until executed
126 yield from wait_for_busy_hi(core)
127 yield from wait_for_busy_clear(core)
128
129 print ("sim", code)
130 # call simulated operation
131 opname = code.split(' ')[0]
132 yield from sim.call(opname)
133 yield Settle()
134 index = sim.pc.CIA.value//4
135
136 # register check
137 yield from check_regs(self, sim, core, test, code)
138
139 # Memory check
140 yield from check_sim_memory(self, l0, sim, code)
141
142 sim.add_sync_process(process)
143 with sim.write_vcd("issuer_simulator.vcd",
144 traces=[]):
145 sim.run()
146
147
148 if __name__ == "__main__":
149 unittest.main(exit=False)
150 suite = unittest.TestSuite()
151 suite.addTest(TestRunner(GeneralTestCases.test_data))
152 suite.addTest(TestRunner(LDSTTestCase.test_data))
153 suite.addTest(TestRunner(CRTestCase.test_data))
154 suite.addTest(TestRunner(ShiftRotTestCase.test_data))
155 suite.addTest(TestRunner(LogicalTestCase.test_data))
156 suite.addTest(TestRunner(ALUTestCase.test_data))
157 suite.addTest(TestRunner(BranchTestCase.test_data))
158
159 runner = unittest.TextTestRunner()
160 runner.run(suite)
161