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
, InternalOp
, CryIn
)
10 from soc
.decoder
.selectable_int
import SelectableInt
11 from soc
.simulator
.program
import Program
12 from soc
.decoder
.isa
.all
import ISA
15 from soc
.fu
.test
.common
import (TestCase
, ALUHelpers
)
16 from soc
.fu
.div
.pipeline
import DIVBasePipe
17 from soc
.fu
.div
.pipe_data
import DIVPipeSpec
21 def get_cu_inputs(dec2
, sim
):
22 """naming (res) must conform to DIVFunctionUnit input regspec
26 yield from ALUHelpers
.get_sim_int_ra(res
, sim
, dec2
) # RA
27 yield from ALUHelpers
.get_sim_int_rb(res
, sim
, dec2
) # RB
28 yield from ALUHelpers
.get_sim_xer_so(res
, sim
, dec2
) # XER.so
30 print ("alu get_cu_inputs", res
)
36 def set_alu_inputs(alu
, dec2
, sim
):
37 # TODO: see https://bugs.libre-soc.org/show_bug.cgi?id=305#c43
38 # detect the immediate here (with m.If(self.i.ctx.op.imm_data.imm_ok))
39 # and place it into data_i.b
41 inp
= yield from get_cu_inputs(dec2
, sim
)
42 yield from ALUHelpers
.set_int_ra(alu
, dec2
, inp
)
43 yield from ALUHelpers
.set_int_rb(alu
, dec2
, inp
)
45 yield from ALUHelpers
.set_xer_so(alu
, dec2
, inp
)
48 # This test bench is a bit different than is usual. Initially when I
49 # was writing it, I had all of the tests call a function to create a
50 # device under test and simulator, initialize the dut, run the
51 # simulation for ~2 cycles, and assert that the dut output what it
52 # should have. However, this was really slow, since it needed to
53 # create and tear down the dut and simulator for every test case.
55 # Now, instead of doing that, every test case in DIVTestCase puts some
56 # data into the test_data list below, describing the instructions to
57 # be tested and the initial state. Once all the tests have been run,
58 # test_data gets passed to TestRunner which then sets up the DUT and
59 # simulator once, runs all the data through it, and asserts that the
60 # results match the pseudocode sim at every cycle.
62 # By doing this, I've reduced the time it takes to run the test suite
63 # massively. Before, it took around 1 minute on my computer, now it
64 # takes around 3 seconds
67 class DIVTestCase(FHDLTestCase
):
70 def __init__(self
, name
):
71 super().__init
__(name
)
74 def run_tst_program(self
, prog
, initial_regs
=None, initial_sprs
=None):
75 tc
= TestCase(prog
, self
.test_name
, initial_regs
, initial_sprs
)
76 self
.test_data
.append(tc
)
78 def test_0_regression(self
):
80 lst
= ["divwo 3, 1, 2"]
81 initial_regs
= [0] * 32
82 initial_regs
[1] = 0xbc716835f32ac00c
83 initial_regs
[2] = 0xcdf69a7f7042db66
84 self
.run_tst_program(Program(lst
), initial_regs
)
86 def test_1_regression(self
):
87 lst
= ["divwo 3, 1, 2"]
88 initial_regs
= [0] * 32
89 initial_regs
[1] = 0x10000000000000000-4
90 initial_regs
[2] = 0x10000000000000000-2
91 self
.run_tst_program(Program(lst
), initial_regs
)
93 def test_2_regression(self
):
94 lst
= ["divwo 3, 1, 2"]
95 initial_regs
= [0] * 32
96 initial_regs
[1] = 0xffffffffffff9321
97 initial_regs
[2] = 0xffffffffffff7012
98 self
.run_tst_program(Program(lst
), initial_regs
)
100 def test_3_regression(self
):
101 lst
= ["divwo. 3, 1, 2"]
102 initial_regs
= [0] * 32
103 initial_regs
[1] = 0x1b8e32f2458746af
104 initial_regs
[2] = 0x6b8aee2ccf7d62e9
105 self
.run_tst_program(Program(lst
), initial_regs
)
107 def test_4_regression(self
):
108 lst
= ["divw 3, 1, 2"]
109 initial_regs
= [0] * 32
110 initial_regs
[1] = 0x1c4e6c2f3aa4a05c
111 initial_regs
[2] = 0xe730c2eed6cc8dd7
112 self
.run_tst_program(Program(lst
), initial_regs
)
114 def test_5_regression(self
):
115 lst
= ["divw 3, 1, 2",
117 initial_regs
= [0] * 32
118 initial_regs
[1] = 0x1c4e6c2f3aa4a05c
119 initial_regs
[2] = 0xe730c2eed6cc8dd7
120 initial_regs
[4] = 0x1b8e32f2458746af
121 initial_regs
[5] = 0x6b8aee2ccf7d62e9
122 self
.run_tst_program(Program(lst
), initial_regs
)
124 def test_6_regression(self
):
125 # CR0 not getting set properly for this one
126 # turns out that overflow is not set correctly in
127 # fu/div/output_stage.py calc_overflow
128 # https://bugs.libre-soc.org/show_bug.cgi?id=425
129 lst
= ["divw. 3, 1, 2"]
130 initial_regs
= [0] * 32
131 initial_regs
[1] = 0x61c1cc3b80f2a6af
132 initial_regs
[2] = 0x9dc66a7622c32bc0
133 self
.run_tst_program(Program(lst
), initial_regs
)
135 def test_7_regression(self
):
136 # https://bugs.libre-soc.org/show_bug.cgi?id=425
137 lst
= ["divw. 3, 1, 2"]
138 initial_regs
= [0] * 32
139 initial_regs
[1] = 0xf1791627e05e8096
140 initial_regs
[2] = 0xffc868bf4573da0b
141 self
.run_tst_program(Program(lst
), initial_regs
)
143 def test_divw_by_zero_1(self
):
144 lst
= ["divw. 3, 1, 2"]
145 initial_regs
= [0] * 32
146 initial_regs
[1] = 0x1
147 initial_regs
[2] = 0x0
148 self
.run_tst_program(Program(lst
), initial_regs
)
150 def test_divw_overflow2(self
):
151 lst
= ["divw. 3, 1, 2"]
152 initial_regs
= [0] * 32
153 initial_regs
[1] = 0x80000000
154 initial_regs
[2] = 0xffffffffffffffff # top bits don't seem to matter
155 self
.run_tst_program(Program(lst
), initial_regs
)
157 def test_divw_overflow3(self
):
158 lst
= ["divw. 3, 1, 2"]
159 initial_regs
= [0] * 32
160 initial_regs
[1] = 0x80000000
161 initial_regs
[2] = 0xffffffff
162 self
.run_tst_program(Program(lst
), initial_regs
)
164 def test_rand_divw(self
):
165 insns
= ["divw", "divw.", "divwo", "divwo."]
167 choice
= random
.choice(insns
)
168 lst
= [f
"{choice} 3, 1, 2"]
169 initial_regs
= [0] * 32
170 initial_regs
[1] = random
.randint(0, (1<<64)-1)
171 initial_regs
[2] = random
.randint(0, (1<<64)-1)
172 self
.run_tst_program(Program(lst
), initial_regs
)
174 def test_rand_divwu(self
):
175 insns
= ["divwu", "divwu.", "divwuo", "divwuo."]
177 choice
= random
.choice(insns
)
178 lst
= [f
"{choice} 3, 1, 2"]
179 initial_regs
= [0] * 32
180 initial_regs
[1] = random
.randint(0, (1<<64)-1)
181 initial_regs
[2] = random
.randint(0, (1<<64)-1)
182 self
.run_tst_program(Program(lst
), initial_regs
)
184 def test_ilang(self
):
185 pspec
= DIVPipeSpec(id_wid
=2)
186 alu
= DIVBasePipe(pspec
)
187 vl
= rtlil
.convert(alu
, ports
=alu
.ports())
188 with
open("div_pipeline.il", "w") as f
:
192 class TestRunner(FHDLTestCase
):
193 def __init__(self
, test_data
):
194 super().__init
__("run_all")
195 self
.test_data
= test_data
200 instruction
= Signal(32)
202 pdecode
= create_pdecode()
204 m
.submodules
.pdecode2
= pdecode2
= PowerDecode2(pdecode
)
206 pspec
= DIVPipeSpec(id_wid
=2)
207 m
.submodules
.alu
= alu
= DIVBasePipe(pspec
)
209 comb
+= alu
.p
.data_i
.ctx
.op
.eq_from_execute1(pdecode2
.e
)
210 comb
+= alu
.n
.ready_i
.eq(1)
211 comb
+= pdecode2
.dec
.raw_opcode_in
.eq(instruction
)
216 for test
in self
.test_data
:
218 program
= test
.program
219 self
.subTest(test
.name
)
220 sim
= ISA(pdecode2
, test
.regs
, test
.sprs
, test
.cr
,
222 gen
= program
.generate_instructions()
223 instructions
= list(zip(gen
, program
.assembly
.splitlines()))
226 index
= sim
.pc
.CIA
.value
//4
227 while index
< len(instructions
):
228 ins
, code
= instructions
[index
]
230 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
233 so
= 1 if sim
.spr
['XER'][XER_bits
['SO']] else 0
234 ov
= 1 if sim
.spr
['XER'][XER_bits
['OV']] else 0
235 ov32
= 1 if sim
.spr
['XER'][XER_bits
['OV32']] else 0
236 print ("before: so/ov/32", so
, ov
, ov32
)
238 # ask the decoder to decode this binary data (endian'd)
239 yield pdecode2
.dec
.bigendian
.eq(0) # little / big?
240 yield instruction
.eq(ins
) # raw binary instr.
242 fn_unit
= yield pdecode2
.e
.do
.fn_unit
243 self
.assertEqual(fn_unit
, Function
.DIV
.value
)
244 yield from set_alu_inputs(alu
, pdecode2
, sim
)
246 # set valid for one cycle, propagate through pipeline...
247 yield alu
.p
.valid_i
.eq(1)
249 yield alu
.p
.valid_i
.eq(0)
251 opname
= code
.split(' ')[0]
252 yield from sim
.call(opname
)
253 index
= sim
.pc
.CIA
.value
//4
255 vld
= yield alu
.n
.valid_o
258 vld
= yield alu
.n
.valid_o
259 # bug #425 investigation
260 do
= alu
.pipe_end
.div_out
262 is_32bit
= yield ctx_op
.is_32bit
263 is_signed
= yield ctx_op
.is_signed
264 quotient_root
= yield do
.i
.core
.quotient_root
265 quotient_65
= yield do
.quotient_65
266 dive_abs_ov32
= yield do
.i
.dive_abs_ov32
267 div_by_zero
= yield do
.i
.div_by_zero
268 quotient_neg
= yield do
.quotient_neg
269 print ("32bit", hex(is_32bit
))
270 print ("signed", hex(is_signed
))
271 print ("quotient_root", hex(quotient_root
))
272 print ("quotient_65", hex(quotient_65
))
273 print ("div_by_zero", hex(div_by_zero
))
274 print ("dive_abs_ov32", hex(dive_abs_ov32
))
275 print ("quotient_neg", hex(quotient_neg
))
279 yield from self
.check_alu_outputs(alu
, pdecode2
, sim
, code
)
282 sim
.add_sync_process(process
)
283 with sim
.write_vcd("div_simulator.vcd", "div_simulator.gtkw",
287 def check_alu_outputs(self
, alu
, dec2
, sim
, code
):
289 rc
= yield dec2
.e
.do
.rc
.data
290 cridx_ok
= yield dec2
.e
.write_cr
.ok
291 cridx
= yield dec2
.e
.write_cr
.data
293 print ("check extra output", repr(code
), cridx_ok
, cridx
)
295 self
.assertEqual(cridx
, 0, code
)
300 yield from ALUHelpers
.get_cr_a(res
, alu
, dec2
)
301 yield from ALUHelpers
.get_xer_ov(res
, alu
, dec2
)
302 yield from ALUHelpers
.get_int_o(res
, alu
, dec2
)
303 yield from ALUHelpers
.get_xer_so(res
, alu
, dec2
)
305 print ("res output", res
)
307 yield from ALUHelpers
.get_sim_int_o(sim_o
, sim
, dec2
)
308 yield from ALUHelpers
.get_wr_sim_cr_a(sim_o
, sim
, dec2
)
309 yield from ALUHelpers
.get_sim_xer_ov(sim_o
, sim
, dec2
)
310 yield from ALUHelpers
.get_sim_xer_so(sim_o
, sim
, dec2
)
312 print ("sim output", sim_o
)
314 ALUHelpers
.check_int_o(self
, res
, sim_o
, code
)
315 ALUHelpers
.check_cr_a(self
, res
, sim_o
, "CR%d %s" % (cridx
, code
))
316 ALUHelpers
.check_xer_ov(self
, res
, sim_o
, code
)
317 ALUHelpers
.check_xer_so(self
, res
, sim_o
, code
)
319 oe
= yield dec2
.e
.do
.oe
.oe
320 oe_ok
= yield dec2
.e
.do
.oe
.ok
321 print ("oe, oe_ok", oe
, oe_ok
)
322 if not oe
or not oe_ok
:
323 # if OE not enabled, XER SO and OV must not be activated
324 so_ok
= yield alu
.n
.data_o
.xer_so
.ok
325 ov_ok
= yield alu
.n
.data_o
.xer_ov
.ok
326 print ("so, ov", so_ok
, ov_ok
)
327 self
.assertEqual(ov_ok
, False, code
)
328 self
.assertEqual(so_ok
, False, code
)
331 if __name__
== "__main__":
332 unittest
.main(exit
=False)
333 suite
= unittest
.TestSuite()
334 suite
.addTest(TestRunner(DIVTestCase
.test_data
))
336 runner
= unittest
.TextTestRunner()