1d9dfb5067f5dbd9be923c724c8c298d9f6ad1e9
1 from nmigen
import Module
, Signal
2 from nmigen
.back
.pysim
import Simulator
, Delay
, Settle
3 from nmutil
.formaltest
import FHDLTestCase
4 from nmigen
.cli
import rtlil
6 from soc
.decoder
.isa
.caller
import ISACaller
, special_sprs
7 from soc
.decoder
.power_decoder
import (create_pdecode
)
8 from soc
.decoder
.power_decoder2
import (PowerDecode2
)
9 from soc
.decoder
.power_enums
import (XER_bits
, Function
)
10 from soc
.decoder
.selectable_int
import SelectableInt
11 from soc
.simulator
.program
import Program
12 from soc
.decoder
.isa
.all
import ISA
14 from soc
.fu
.test
.common
import TestCase
, ALUHelpers
15 from soc
.fu
.logical
.pipeline
import LogicalBasePipe
16 from soc
.fu
.logical
.pipe_data
import LogicalPipeSpec
20 def get_cu_inputs(dec2
, sim
):
21 """naming (res) must conform to LogicalFunctionUnit input regspec
25 yield from ALUHelpers
.get_sim_int_ra(res
, sim
, dec2
) # RA
26 yield from ALUHelpers
.get_sim_int_rb(res
, sim
, dec2
) # RB
31 def set_alu_inputs(alu
, dec2
, sim
):
32 # TODO: see https://bugs.libre-soc.org/show_bug.cgi?id=305#c43
33 # detect the immediate here (with m.If(self.i.ctx.op.imm_data.imm_ok))
34 # and place it into data_i.b
36 inp
= yield from get_cu_inputs(dec2
, sim
)
37 yield from ALUHelpers
.set_int_ra(alu
, dec2
, inp
)
38 yield from ALUHelpers
.set_int_rb(alu
, dec2
, inp
)
41 # This test bench is a bit different than is usual. Initially when I
42 # was writing it, I had all of the tests call a function to create a
43 # device under test and simulator, initialize the dut, run the
44 # simulation for ~2 cycles, and assert that the dut output what it
45 # should have. However, this was really slow, since it needed to
46 # create and tear down the dut and simulator for every test case.
48 # Now, instead of doing that, every test case in ALUTestCase puts some
49 # data into the test_data list below, describing the instructions to
50 # be tested and the initial state. Once all the tests have been run,
51 # test_data gets passed to TestRunner which then sets up the DUT and
52 # simulator once, runs all the data through it, and asserts that the
53 # results match the pseudocode sim at every cycle.
55 # By doing this, I've reduced the time it takes to run the test suite
56 # massively. Before, it took around 1 minute on my computer, now it
57 # takes around 3 seconds
60 class LogicalTestCase(FHDLTestCase
):
62 def __init__(self
, name
):
63 super().__init
__(name
)
66 def run_tst_program(self
, prog
, initial_regs
=None, initial_sprs
=None):
67 tc
= TestCase(prog
, self
.test_name
, initial_regs
, initial_sprs
)
68 self
.test_data
.append(tc
)
71 insns
= ["and", "or", "xor"]
73 choice
= random
.choice(insns
)
74 lst
= [f
"{choice} 3, 1, 2"]
75 initial_regs
= [0] * 32
76 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
77 initial_regs
[2] = random
.randint(0, (1 << 64)-1)
78 self
.run_tst_program(Program(lst
), initial_regs
)
80 def test_rand_imm_logical(self
):
81 insns
= ["andi.", "andis.", "ori", "oris", "xori", "xoris"]
83 choice
= random
.choice(insns
)
84 imm
= random
.randint(0, (1 << 16)-1)
85 lst
= [f
"{choice} 3, 1, {imm}"]
87 initial_regs
= [0] * 32
88 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
89 self
.run_tst_program(Program(lst
), initial_regs
)
92 insns
= ["cntlzd", "cnttzd", "cntlzw", "cnttzw"]
94 choice
= random
.choice(insns
)
95 lst
= [f
"{choice} 3, 1"]
97 initial_regs
= [0] * 32
98 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
99 self
.run_tst_program(Program(lst
), initial_regs
)
101 def test_parity(self
):
102 insns
= ["prtyw", "prtyd"]
104 choice
= random
.choice(insns
)
105 lst
= [f
"{choice} 3, 1"]
107 initial_regs
= [0] * 32
108 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
109 self
.run_tst_program(Program(lst
), initial_regs
)
111 def test_popcnt(self
):
112 insns
= ["popcntb", "popcntw", "popcntd"]
114 choice
= random
.choice(insns
)
115 lst
= [f
"{choice} 3, 1"]
117 initial_regs
= [0] * 32
118 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
119 self
.run_tst_program(Program(lst
), initial_regs
)
121 def test_popcnt_edge(self
):
122 insns
= ["popcntb", "popcntw", "popcntd"]
124 lst
= [f
"{choice} 3, 1"]
125 initial_regs
= [0] * 32
127 self
.run_tst_program(Program(lst
), initial_regs
)
130 lst
= ["cmpb 3, 1, 2"]
131 initial_regs
= [0] * 32
132 initial_regs
[1] = 0xdeadbeefcafec0de
133 initial_regs
[2] = 0xd0adb0000afec1de
134 self
.run_tst_program(Program(lst
), initial_regs
)
136 def test_bpermd(self
):
137 lst
= ["bpermd 3, 1, 2"]
139 initial_regs
= [0] * 32
140 initial_regs
[1] = 1<<random
.randint(0,63)
141 initial_regs
[2] = 0xdeadbeefcafec0de
142 self
.run_tst_program(Program(lst
), initial_regs
)
144 def test_ilang(self
):
145 pspec
= LogicalPipeSpec(id_wid
=2)
146 alu
= LogicalBasePipe(pspec
)
147 vl
= rtlil
.convert(alu
, ports
=alu
.ports())
148 with
open("logical_pipeline.il", "w") as f
:
152 class TestRunner(FHDLTestCase
):
153 def __init__(self
, test_data
):
154 super().__init
__("run_all")
155 self
.test_data
= test_data
160 instruction
= Signal(32)
162 pdecode
= create_pdecode()
164 m
.submodules
.pdecode2
= pdecode2
= PowerDecode2(pdecode
)
166 pspec
= LogicalPipeSpec(id_wid
=2)
167 m
.submodules
.alu
= alu
= LogicalBasePipe(pspec
)
169 comb
+= alu
.p
.data_i
.ctx
.op
.eq_from_execute1(pdecode2
.e
)
170 comb
+= alu
.p
.valid_i
.eq(1)
171 comb
+= alu
.n
.ready_i
.eq(1)
172 comb
+= pdecode2
.dec
.raw_opcode_in
.eq(instruction
)
178 for test
in self
.test_data
:
180 program
= test
.program
181 self
.subTest(test
.name
)
182 simulator
= ISA(pdecode2
, test
.regs
, test
.sprs
, test
.cr
,
184 gen
= program
.generate_instructions()
185 instructions
= list(zip(gen
, program
.assembly
.splitlines()))
187 index
= simulator
.pc
.CIA
.value
//4
188 while index
< len(instructions
):
189 ins
, code
= instructions
[index
]
191 print("0x{:X}".format(ins
& 0xffffffff))
194 # ask the decoder to decode this binary data (endian'd)
195 yield pdecode2
.dec
.bigendian
.eq(0) # little / big?
196 yield instruction
.eq(ins
) # raw binary instr.
198 fn_unit
= yield pdecode2
.e
.fn_unit
199 self
.assertEqual(fn_unit
, Function
.LOGICAL
.value
, code
)
200 yield from set_alu_inputs(alu
, pdecode2
, simulator
)
202 opname
= code
.split(' ')[0]
203 yield from simulator
.call(opname
)
204 index
= simulator
.pc
.CIA
.value
//4
206 vld
= yield alu
.n
.valid_o
209 vld
= yield alu
.n
.valid_o
212 yield from self
.check_alu_outputs(alu
, pdecode2
,
215 sim
.add_sync_process(process
)
216 with sim
.write_vcd("logical_simulator.vcd", "logical_simulator.gtkw",
220 def check_alu_outputs(self
, alu
, dec2
, sim
, code
):
222 rc
= yield dec2
.e
.rc
.data
223 cridx_ok
= yield dec2
.e
.write_cr
.ok
224 cridx
= yield dec2
.e
.write_cr
.data
226 print ("check extra output", repr(code
), cridx_ok
, cridx
)
228 self
.assertEqual(cridx
, 0, code
)
233 yield from ALUHelpers
.get_cr_a(res
, alu
, dec2
)
234 yield from ALUHelpers
.get_int_o(res
, alu
, dec2
)
236 yield from ALUHelpers
.get_sim_int_o(sim_o
, sim
, dec2
)
237 yield from ALUHelpers
.get_wr_sim_cr_a(sim_o
, sim
, dec2
)
239 ALUHelpers
.check_cr_a(self
, res
, sim_o
, "CR%d %s" % (cridx
, code
))
240 ALUHelpers
.check_int_o(self
, res
, sim_o
, code
)
243 if __name__
== "__main__":
244 unittest
.main(exit
=False)
245 suite
= unittest
.TestSuite()
246 suite
.addTest(TestRunner(LogicalTestCase
.test_data
))
248 runner
= unittest
.TextTestRunner()