67f7d9bafcd827f8d47b798b4d8e83769ab0eb75
[soc.git] / src / soc / fu / shift_rot / test / test_pipe_caller.py
1 import random
2 from soc.fu.shift_rot.pipe_data import ShiftRotPipeSpec
3 from soc.fu.shift_rot.pipeline import ShiftRotBasePipe
4 from openpower.test.common import TestAccumulatorBase, TestCase, ALUHelpers
5 from openpower.endian import bigendian
6 from openpower.decoder.isa.all import ISA
7 from openpower.simulator.program import Program
8 from openpower.decoder.power_enums import (XER_bits, Function, CryIn)
9 from openpower.decoder.power_decoder2 import (PowerDecode2)
10 from openpower.decoder.power_decoder import (create_pdecode)
11 import unittest
12 from nmigen.cli import rtlil
13 from nmigen import Module, Signal
14
15 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
16 # Also, check out the cxxsim nmigen branch, and latest yosys from git
17 from nmutil.sim_tmp_alternative import Simulator, Settle
18
19 from openpower.test.shift_rot.shift_rot_cases import ShiftRotTestCase
20 from openpower.test.bitmanip.bitmanip_cases import BitManipTestCase
21
22
23 def get_cu_inputs(dec2, sim):
24 """naming (res) must conform to ShiftRotFunctionUnit input regspec
25 """
26 res = {}
27
28 yield from ALUHelpers.get_sim_int_ra(res, sim, dec2) # RA
29 yield from ALUHelpers.get_sim_int_rb(res, sim, dec2) # RB
30 yield from ALUHelpers.get_sim_int_rc(res, sim, dec2) # RC
31 yield from ALUHelpers.get_rd_sim_xer_ca(res, sim, dec2) # XER.ca
32 yield from ALUHelpers.get_sim_xer_so(res, sim, dec2) # XER.so
33
34 print("alu get_cu_inputs", res)
35
36 return res
37
38
39 def set_alu_inputs(alu, dec2, sim):
40 # TODO: see https://bugs.libre-soc.org/show_bug.cgi?id=305#c43
41 # detect the immediate here (with m.If(self.i.ctx.op.imm_data.imm_ok))
42 # and place it into i_data.b
43
44 inp = yield from get_cu_inputs(dec2, sim)
45 yield from ALUHelpers.set_int_ra(alu, dec2, inp)
46 yield from ALUHelpers.set_int_rb(alu, dec2, inp)
47 yield from ALUHelpers.set_int_rc(alu, dec2, inp)
48 yield from ALUHelpers.set_xer_ca(alu, dec2, inp)
49 yield from ALUHelpers.set_xer_so(alu, dec2, inp)
50
51
52 # This test bench is a bit different than is usual. Initially when I
53 # was writing it, I had all of the tests call a function to create a
54 # device under test and simulator, initialize the dut, run the
55 # simulation for ~2 cycles, and assert that the dut output what it
56 # should have. However, this was really slow, since it needed to
57 # create and tear down the dut and simulator for every test case.
58
59 # Now, instead of doing that, every test case in ShiftRotTestCase puts some
60 # data into the test_data list below, describing the instructions to
61 # be tested and the initial state. Once all the tests have been run,
62 # test_data gets passed to TestRunner which then sets up the DUT and
63 # simulator once, runs all the data through it, and asserts that the
64 # results match the pseudocode sim at every cycle.
65
66 # By doing this, I've reduced the time it takes to run the test suite
67 # massively. Before, it took around 1 minute on my computer, now it
68 # takes around 3 seconds
69
70
71 class ShiftRotIlangCase(TestAccumulatorBase):
72
73 def case_ilang(self):
74 pspec = ShiftRotPipeSpec(id_wid=2, parent_pspec=None)
75 pspec.draft_bitmanip = True
76 alu = ShiftRotBasePipe(pspec)
77 vl = rtlil.convert(alu, ports=alu.ports())
78 with open("shift_rot_pipeline.il", "w") as f:
79 f.write(vl)
80
81
82 class TestRunner(unittest.TestCase):
83 def __init__(self, test_data):
84 super().__init__("run_all")
85 self.test_data = test_data
86
87 def execute(self, alu, instruction, pdecode2, test):
88 program = test.program
89 simulator = ISA(pdecode2, test.regs, test.sprs, test.cr,
90 test.mem, test.msr,
91 bigendian=bigendian)
92 gen = program.generate_instructions()
93 instructions = list(zip(gen, program.assembly.splitlines()))
94
95 index = simulator.pc.CIA.value//4
96 while index < len(instructions):
97 ins, code = instructions[index]
98
99 print("0x{:X}".format(ins & 0xffffffff))
100 print(code)
101
102 # ask the decoder to decode this binary data (endian'd)
103 yield pdecode2.dec.bigendian.eq(bigendian) # little / big?
104 yield instruction.eq(ins) # raw binary instr.
105 yield Settle()
106 fn_unit = yield pdecode2.e.do.fn_unit
107 self.assertEqual(fn_unit, Function.SHIFT_ROT.value)
108 yield from set_alu_inputs(alu, pdecode2, simulator)
109
110 # set valid for one cycle, propagate through pipeline...
111 yield alu.p.i_valid.eq(1)
112 yield
113 yield alu.p.i_valid.eq(0)
114
115 opname = code.split(' ')[0]
116 yield from simulator.call(opname)
117 index = simulator.pc.CIA.value//4
118
119 vld = yield alu.n.o_valid
120 while not vld:
121 yield
122 vld = yield alu.n.o_valid
123 yield
124 alu_out = yield alu.n.o_data.o.data
125
126 yield from self.check_alu_outputs(alu, pdecode2,
127 simulator, code)
128 yield Settle()
129
130 def run_all(self):
131 m = Module()
132 comb = m.d.comb
133 instruction = Signal(32)
134
135 fn_name = "SHIFT_ROT"
136 opkls = ShiftRotPipeSpec.opsubsetkls
137
138 m.submodules.pdecode2 = pdecode2 = PowerDecode2(None, opkls, fn_name)
139 pdecode = pdecode2.dec
140
141 pspec = ShiftRotPipeSpec(id_wid=2, parent_pspec=None)
142 pspec.draft_bitmanip = True
143 m.submodules.alu = alu = ShiftRotBasePipe(pspec)
144
145 comb += alu.p.i_data.ctx.op.eq_from_execute1(pdecode2.do)
146 comb += alu.n.i_ready.eq(1)
147 comb += pdecode2.dec.raw_opcode_in.eq(instruction)
148 sim = Simulator(m)
149
150 sim.add_clock(1e-6)
151
152 def process():
153 for test in self.test_data:
154 print(test.name)
155 program = test.program
156 with self.subTest(test.name):
157 yield from self.execute(alu, instruction, pdecode2, test)
158
159 sim.add_sync_process(process)
160 with sim.write_vcd("shift_rot_simulator.vcd"):
161 sim.run()
162
163 def check_alu_outputs(self, alu, dec2, sim, code):
164
165 rc = yield dec2.e.do.rc.rc
166 cridx_ok = yield dec2.e.write_cr.ok
167 cridx = yield dec2.e.write_cr.data
168
169 print("check extra output", repr(code), cridx_ok, cridx)
170 if rc:
171 self.assertEqual(cridx, 0, code)
172
173 sim_o = {}
174 res = {}
175
176 yield from ALUHelpers.get_cr_a(res, alu, dec2)
177 yield from ALUHelpers.get_xer_ca(res, alu, dec2)
178 yield from ALUHelpers.get_int_o(res, alu, dec2)
179
180 print("hw outputs", res)
181
182 yield from ALUHelpers.get_sim_int_o(sim_o, sim, dec2)
183 yield from ALUHelpers.get_wr_sim_cr_a(sim_o, sim, dec2)
184 yield from ALUHelpers.get_wr_sim_xer_ca(sim_o, sim, dec2)
185
186 print("sim outputs", sim_o)
187
188 ALUHelpers.check_cr_a(self, res, sim_o, "CR%d %s" % (cridx, code))
189 ALUHelpers.check_xer_ca(self, res, sim_o, code)
190 ALUHelpers.check_int_o(self, res, sim_o, code)
191
192
193 if __name__ == "__main__":
194 unittest.main(exit=False)
195 suite = unittest.TestSuite()
196 suite.addTest(TestRunner(ShiftRotTestCase().test_data))
197 suite.addTest(TestRunner(BitManipTestCase().test_data))
198 suite.addTest(TestRunner(ShiftRotIlangCase().test_data))
199
200 runner = unittest.TextTestRunner()
201 runner.run(suite)