5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
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
12 from openpower
.decoder
.isa
.caller
import special_sprs
13 from openpower
.decoder
.power_decoder
import create_pdecode
14 from openpower
.decoder
.power_decoder2
import PowerDecode2
15 from openpower
.decoder
.selectable_int
import SelectableInt
16 from openpower
.decoder
.isa
.all
import ISA
18 # note that for testing using SPRfull should be ok here
19 from openpower
.decoder
.power_enums
import SPRfull
as SPR
, spr_dict
, Function
, XER_bits
20 from soc
.config
.test
.test_loadstore
import TestMemPspec
21 from openpower
.endian
import bigendian
23 from soc
.simple
.core
import NonProductionCore
24 from soc
.experiment
.compalu_multi
import find_ok
# hack
26 from soc
.fu
.compunits
.test
.test_compunit
import (setup_test_memory
,
29 # test with ALU data and Logical data
30 from soc
.fu
.alu
.test
.test_pipe_caller
import ALUTestCase
31 from soc
.fu
.logical
.test
.test_pipe_caller
import LogicalTestCase
32 from soc
.fu
.shift_rot
.test
.test_pipe_caller
import ShiftRotTestCase
33 from soc
.fu
.cr
.test
.test_pipe_caller
import CRTestCase
34 from soc
.fu
.branch
.test
.test_pipe_caller
import BranchTestCase
35 from soc
.fu
.ldst
.test
.test_pipe_caller
import LDSTTestCase
36 from openpower
.util
import spr_to_fast_reg
40 def set_mmu_spr(name
, i
, val
, core
): #important keep pep8 formatting
41 fsm
= core
.fus
.fus
["mmu0"].alu
42 yield fsm
.mmu
.l_in
.mtspr
.eq(1)
43 yield fsm
.mmu
.l_in
.sprn
.eq(i
)
44 yield fsm
.mmu
.l_in
.rs
.eq(val
)
46 yield fsm
.mmu
.l_in
.mtspr
.eq(0)
47 print("mmu_spr was updated")
49 def setup_regs(pdecode2
, core
, test
):
51 # set up INT regfile, "direct" write (bypass rd/write ports)
52 intregs
= core
.regs
.int
55 yield intregs
.regs
[i
].reg
.eq(test
.regs
[i
])
57 yield intregs
.memory
._array
[i
].eq(test
.regs
[i
])
60 # set up CR regfile, "direct" write across all CRs
63 #cr = int('{:32b}'.format(cr)[::-1], 2)
64 print("setup cr reg", hex(cr
))
67 cri
= (cr
>> (i
*4)) & 0xf
68 #cri = int('{:04b}'.format(cri)[::-1], 2)
69 print("setup cr reg", hex(cri
), i
,
70 crregs
.regs
[i
].reg
.shape())
71 yield crregs
.regs
[i
].reg
.eq(cri
)
73 # set up XER. "direct" write (bypass rd/write ports)
75 print("setup sprs", test
.sprs
)
77 if 'XER' in test
.sprs
:
78 xer
= test
.sprs
['XER']
82 if isinstance(xer
, int):
83 xer
= SelectableInt(xer
, 64)
84 sobit
= xer
[XER_bits
['SO']].value
85 yield xregs
.regs
[xregs
.SO
].reg
.eq(sobit
)
86 cabit
= xer
[XER_bits
['CA']].value
87 ca32bit
= xer
[XER_bits
['CA32']].value
88 yield xregs
.regs
[xregs
.CA
].reg
.eq(Cat(cabit
, ca32bit
))
89 ovbit
= xer
[XER_bits
['OV']].value
90 ov32bit
= xer
[XER_bits
['OV32']].value
91 yield xregs
.regs
[xregs
.OV
].reg
.eq(Cat(ovbit
, ov32bit
))
92 print("setting XER so %d ca %d ca32 %d ov %d ov32 %d" %
93 (sobit
, cabit
, ca32bit
, ovbit
, ov32bit
))
95 yield xregs
.regs
[xregs
.SO
].reg
.eq(0)
96 yield xregs
.regs
[xregs
.OV
].reg
.eq(0)
97 yield xregs
.regs
[xregs
.CA
].reg
.eq(0)
99 # setting both fast and slow SPRs from test data
101 fregs
= core
.regs
.fast
102 sregs
= core
.regs
.spr
103 for sprname
, val
in test
.sprs
.items():
104 if isinstance(val
, SelectableInt
):
106 if isinstance(sprname
, int):
107 sprname
= spr_dict
[sprname
].SPR
110 fast
= spr_to_fast_reg(sprname
)
112 # match behaviour of SPRMap in power_decoder2.py
113 for i
, x
in enumerate(SPR
):
114 if sprname
== x
.name
:
115 print("setting slow SPR %d (%s) to %x" %
117 if not sprname
in mmu_sprs
:
118 yield sregs
.memory
._array
[i
].eq(val
)
120 yield from set_mmu_spr(sprname
, i
, val
, core
)
122 print("setting fast reg %d (%s) to %x" %
123 (fast
, sprname
, val
))
125 rval
= fregs
.int.regs
[fast
].reg
127 rval
= fregs
.memory
._array
[fast
]
130 # allow changes to settle before reporting on XER
134 so
= yield xregs
.regs
[xregs
.SO
].reg
135 ov
= yield xregs
.regs
[xregs
.OV
].reg
136 ca
= yield xregs
.regs
[xregs
.CA
].reg
137 oe
= yield pdecode2
.e
.do
.oe
.oe
138 oe_ok
= yield pdecode2
.e
.do
.oe
.oe_ok
140 print("before: so/ov-32/ca-32", so
, bin(ov
), bin(ca
))
141 print("oe:", oe
, oe_ok
)
144 def check_regs(dut
, sim
, core
, test
, code
):
148 if core
.regs
.int.unary
:
149 rval
= yield core
.regs
.int.regs
[i
].reg
151 rval
= yield core
.regs
.int.memory
._array
[i
]
153 print("int regs", list(map(hex, intregs
)))
155 simregval
= sim
.gpr
[i
].asint()
156 dut
.assertEqual(simregval
, intregs
[i
],
157 "int reg %d not equal %s. got %x expected %x" % \
158 (i
, repr(code
), simregval
, intregs
[i
]))
163 rval
= yield core
.regs
.cr
.regs
[i
].reg
165 print("cr regs", list(map(hex, crregs
)))
168 cri
= sim
.crl
[7-i
].get_range().value
169 print("cr reg", i
, hex(cri
), i
, hex(rval
))
170 # XXX https://bugs.libre-soc.org/show_bug.cgi?id=363
171 dut
.assertEqual(cri
, rval
,
172 "cr reg %d not equal %s" % (i
, repr(code
)))
175 xregs
= core
.regs
.xer
176 so
= yield xregs
.regs
[xregs
.SO
].reg
177 ov
= yield xregs
.regs
[xregs
.OV
].reg
178 ca
= yield xregs
.regs
[xregs
.CA
].reg
180 print("sim SO", sim
.spr
['XER'][XER_bits
['SO']])
181 e_so
= sim
.spr
['XER'][XER_bits
['SO']].value
182 e_ov
= sim
.spr
['XER'][XER_bits
['OV']].value
183 e_ov32
= sim
.spr
['XER'][XER_bits
['OV32']].value
184 e_ca
= sim
.spr
['XER'][XER_bits
['CA']].value
185 e_ca32
= sim
.spr
['XER'][XER_bits
['CA32']].value
187 e_ov
= e_ov |
(e_ov32
<< 1)
188 e_ca
= e_ca |
(e_ca32
<< 1)
190 print("after: so/ov-32/ca-32", so
, bin(ov
), bin(ca
))
191 dut
.assertEqual(e_so
, so
, "so mismatch %s" % (repr(code
)))
192 dut
.assertEqual(e_ov
, ov
, "ov mismatch %s" % (repr(code
)))
193 dut
.assertEqual(e_ca
, ca
, "ca mismatch %s" % (repr(code
)))
195 # Check the PC as well
196 state
= core
.regs
.state
197 pc
= yield state
.r_ports
['cia'].data_o
198 e_pc
= sim
.pc
.CIA
.value
199 dut
.assertEqual(e_pc
, pc
)
202 def wait_for_busy_hi(cu
):
204 busy_o
= yield cu
.busy_o
205 terminate_o
= yield cu
.core_terminate_o
207 print("busy/terminate:", busy_o
, terminate_o
)
209 print("!busy", busy_o
, terminate_o
)
213 def set_issue(core
, dec2
, sim
):
214 yield core
.issue_i
.eq(1)
216 yield core
.issue_i
.eq(0)
217 yield from wait_for_busy_hi(core
)
220 def wait_for_busy_clear(cu
):
222 busy_o
= yield cu
.busy_o
223 terminate_o
= yield cu
.core_terminate_o
225 print("busy/terminate:", busy_o
, terminate_o
)
231 class TestRunner(FHDLTestCase
):
232 def __init__(self
, tst_data
):
233 super().__init
__("run_all")
234 self
.test_data
= tst_data
239 instruction
= Signal(32)
242 pspec
= TestMemPspec(ldst_ifacetype
='testpi',
248 m
.submodules
.core
= core
= NonProductionCore(pspec
)
249 pdecode2
= core
.pdecode2
252 comb
+= core
.raw_opcode_i
.eq(instruction
)
253 comb
+= core
.ivalid_i
.eq(ivalid_i
)
255 # temporary hack: says "go" immediately for both address gen and ST
256 ldst
= core
.fus
.fus
['ldst0']
257 m
.d
.comb
+= ldst
.ad
.go
.eq(ldst
.ad
.rel
) # link addr-go direct to rel
258 m
.d
.comb
+= ldst
.st
.go
.eq(ldst
.st
.rel
) # link store-go direct to rel
265 yield core
.issue_i
.eq(0)
268 for test
in self
.test_data
:
270 program
= test
.program
271 self
.subTest(test
.name
)
272 sim
= ISA(pdecode2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
275 gen
= program
.generate_instructions()
276 instructions
= list(zip(gen
, program
.assembly
.splitlines()))
278 yield from setup_test_memory(l0
, sim
)
279 yield from setup_regs(core
, test
)
281 index
= sim
.pc
.CIA
.value
//4
282 while index
< len(instructions
):
283 ins
, code
= instructions
[index
]
285 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
288 # ask the decoder to decode this binary data (endian'd)
289 yield core
.bigendian_i
.eq(bigendian
) # little / big?
290 yield instruction
.eq(ins
) # raw binary instr.
293 # fn_unit = yield pdecode2.e.fn_unit
294 #fuval = self.funit.value
295 #self.assertEqual(fn_unit & fuval, fuval)
297 # set operand and get inputs
298 yield from set_issue(core
, pdecode2
, sim
)
301 yield from wait_for_busy_clear(core
)
306 # call simulated operation
307 opname
= code
.split(' ')[0]
308 yield from sim
.call(opname
)
309 index
= sim
.pc
.CIA
.value
//4
312 yield from check_regs(self
, sim
, core
, test
, code
)
315 yield from check_sim_memory(self
, l0
, sim
, code
)
317 sim
.add_sync_process(process
)
318 with sim
.write_vcd("core_simulator.vcd", "core_simulator.gtkw",
323 if __name__
== "__main__":
324 unittest
.main(exit
=False)
325 suite
= unittest
.TestSuite()
326 suite
.addTest(TestRunner(LDSTTestCase().test_data
))
327 suite
.addTest(TestRunner(CRTestCase().test_data
))
328 suite
.addTest(TestRunner(ShiftRotTestCase().test_data
))
329 suite
.addTest(TestRunner(LogicalTestCase().test_data
))
330 suite
.addTest(TestRunner(ALUTestCase().test_data
))
331 suite
.addTest(TestRunner(BranchTestCase().test_data
))
333 runner
= unittest
.TextTestRunner()