5 * https://bugs.libre-soc.org/show_bug.cgi?id=363
6 * https://bugs.libre-soc.org/show_bug.cgi?id=686
9 from nmigen
import Module
, Signal
, Cat
10 from nmigen
.back
.pysim
import Simulator
, Delay
, Settle
11 from nmutil
.formaltest
import FHDLTestCase
12 from nmigen
.cli
import rtlil
14 from teststate
import SimState
, HDLState
, TestState
15 from openpower
.decoder
.isa
.caller
import special_sprs
16 from openpower
.decoder
.power_decoder
import create_pdecode
17 from openpower
.decoder
.power_decoder2
import PowerDecode2
18 from openpower
.decoder
.selectable_int
import SelectableInt
19 from openpower
.decoder
.isa
.all
import ISA
21 # note that using SPRreduced has to be done to match the
22 # PowerDecoder2 SPR map
23 from openpower
.decoder
.power_enums
import SPRreduced
as SPR
24 from openpower
.decoder
.power_enums
import spr_dict
, Function
, XER_bits
25 from soc
.config
.test
.test_loadstore
import TestMemPspec
26 from openpower
.endian
import bigendian
28 from soc
.simple
.core
import NonProductionCore
29 from soc
.experiment
.compalu_multi
import find_ok
# hack
31 from soc
.fu
.compunits
.test
.test_compunit
import (setup_tst_memory
,
34 # test with ALU data and Logical data
35 from soc
.fu
.alu
.test
.test_pipe_caller
import ALUTestCase
36 from soc
.fu
.logical
.test
.test_pipe_caller
import LogicalTestCase
37 from soc
.fu
.shift_rot
.test
.test_pipe_caller
import ShiftRotTestCase
38 from soc
.fu
.cr
.test
.test_pipe_caller
import CRTestCase
39 from soc
.fu
.branch
.test
.test_pipe_caller
import BranchTestCase
40 from soc
.fu
.ldst
.test
.test_pipe_caller
import LDSTTestCase
41 from openpower
.util
import spr_to_fast_reg
43 # list of SPRs that are controlled and managed by the MMU
44 mmu_sprs
= ["PRTBL", "DSISR", "DAR", "PIDR"]
47 def set_mmu_spr(name
, i
, val
, core
): # important keep pep8 formatting
48 fsm
= core
.fus
.get_fu("mmu0").alu
49 yield fsm
.mmu
.l_in
.mtspr
.eq(1)
50 yield fsm
.mmu
.l_in
.sprn
.eq(i
)
51 yield fsm
.mmu
.l_in
.rs
.eq(val
)
53 yield fsm
.mmu
.l_in
.mtspr
.eq(0)
54 print("mmu_spr was updated")
57 def setup_regs(pdecode2
, core
, test
):
59 # set up INT regfile, "direct" write (bypass rd/write ports)
60 intregs
= core
.regs
.int
63 yield intregs
.regs
[i
].reg
.eq(test
.regs
[i
])
65 yield intregs
.memory
._array
[i
].eq(test
.regs
[i
])
68 # set up CR regfile, "direct" write across all CRs
71 #cr = int('{:32b}'.format(cr)[::-1], 2)
72 print("setup cr reg", hex(cr
))
75 cri
= (cr
>> (i
* 4)) & 0xf
76 #cri = int('{:04b}'.format(cri)[::-1], 2)
77 print("setup cr reg", hex(cri
), i
,
78 crregs
.regs
[i
].reg
.shape())
79 yield crregs
.regs
[i
].reg
.eq(cri
)
81 # set up XER. "direct" write (bypass rd/write ports)
83 print("setup sprs", test
.sprs
)
85 if 'XER' in test
.sprs
:
86 xer
= test
.sprs
['XER']
90 if isinstance(xer
, int):
91 xer
= SelectableInt(xer
, 64)
92 sobit
= xer
[XER_bits
['SO']].value
93 yield xregs
.regs
[xregs
.SO
].reg
.eq(sobit
)
94 cabit
= xer
[XER_bits
['CA']].value
95 ca32bit
= xer
[XER_bits
['CA32']].value
96 yield xregs
.regs
[xregs
.CA
].reg
.eq(Cat(cabit
, ca32bit
))
97 ovbit
= xer
[XER_bits
['OV']].value
98 ov32bit
= xer
[XER_bits
['OV32']].value
99 yield xregs
.regs
[xregs
.OV
].reg
.eq(Cat(ovbit
, ov32bit
))
100 print("setting XER so %d ca %d ca32 %d ov %d ov32 %d" %
101 (sobit
, cabit
, ca32bit
, ovbit
, ov32bit
))
103 yield xregs
.regs
[xregs
.SO
].reg
.eq(0)
104 yield xregs
.regs
[xregs
.OV
].reg
.eq(0)
105 yield xregs
.regs
[xregs
.CA
].reg
.eq(0)
107 # setting both fast and slow SPRs from test data
109 fregs
= core
.regs
.fast
110 sregs
= core
.regs
.spr
111 for sprname
, val
in test
.sprs
.items():
112 if isinstance(val
, SelectableInt
):
114 if isinstance(sprname
, int):
115 sprname
= spr_dict
[sprname
].SPR
118 fast
= spr_to_fast_reg(sprname
)
120 # match behaviour of SPRMap in power_decoder2.py
121 for i
, x
in enumerate(SPR
):
122 if sprname
== x
.name
:
123 print("setting slow SPR %d (%s) to %x" %
125 if sprname
not in mmu_sprs
:
126 yield sregs
.memory
._array
[i
].eq(val
)
128 yield from set_mmu_spr(sprname
, i
, val
, core
)
130 print("setting fast reg %d (%s) to %x" %
131 (fast
, sprname
, val
))
133 rval
= fregs
.int.regs
[fast
].reg
135 rval
= fregs
.memory
._array
[fast
]
138 # allow changes to settle before reporting on XER
142 so
= yield xregs
.regs
[xregs
.SO
].reg
143 ov
= yield xregs
.regs
[xregs
.OV
].reg
144 ca
= yield xregs
.regs
[xregs
.CA
].reg
145 oe
= yield pdecode2
.e
.do
.oe
.oe
146 oe_ok
= yield pdecode2
.e
.do
.oe
.oe_ok
148 print("before: so/ov-32/ca-32", so
, bin(ov
), bin(ca
))
149 print("oe:", oe
, oe_ok
)
152 def teststate_check_regs(dut
, states
, test
, code
):
153 """teststate_check_regs: compares a set of Power ISA objects
154 to check if they have the same "state" (registers only, at the moment)
157 for stype
, totest
in states
.items():
158 state
= yield from TestState(stype
, totest
, dut
, code
)
160 for i
in range(len(slist
)-1):
161 state
, against
= slist
[i
], slist
[i
+1]
162 state
.compare(against
)
165 def check_regs(dut
, sim
, core
, test
, code
):
166 # create the two states and compare
167 testdic
= {'sim': sim
, 'hdl': core
}
168 yield from teststate_check_regs(dut
, testdic
, test
, code
)
171 def wait_for_busy_hi(cu
):
173 busy_o
= yield cu
.busy_o
174 terminate_o
= yield cu
.core_terminate_o
176 print("busy/terminate:", busy_o
, terminate_o
)
178 print("!busy", busy_o
, terminate_o
)
182 def set_issue(core
, dec2
, sim
):
183 yield core
.issue_i
.eq(1)
185 yield core
.issue_i
.eq(0)
186 yield from wait_for_busy_hi(core
)
189 def wait_for_busy_clear(cu
):
191 busy_o
= yield cu
.busy_o
192 terminate_o
= yield cu
.core_terminate_o
194 print("busy/terminate:", busy_o
, terminate_o
)
200 class TestRunner(FHDLTestCase
):
201 def __init__(self
, tst_data
):
202 super().__init
__("run_all")
203 self
.test_data
= tst_data
208 instruction
= Signal(32)
211 pspec
= TestMemPspec(ldst_ifacetype
='testpi',
217 m
.submodules
.core
= core
= NonProductionCore(pspec
)
218 pdecode2
= core
.pdecode2
221 comb
+= core
.raw_opcode_i
.eq(instruction
)
222 comb
+= core
.ivalid_i
.eq(ivalid_i
)
224 # temporary hack: says "go" immediately for both address gen and ST
225 ldst
= core
.fus
.fus
['ldst0']
226 m
.d
.comb
+= ldst
.ad
.go
.eq(ldst
.ad
.rel
) # link addr-go direct to rel
227 m
.d
.comb
+= ldst
.st
.go
.eq(ldst
.st
.rel
) # link store-go direct to rel
234 yield core
.issue_i
.eq(0)
237 for test
in self
.test_data
:
239 program
= test
.program
240 self
.subTest(test
.name
)
241 sim
= ISA(pdecode2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
244 gen
= program
.generate_instructions()
245 instructions
= list(zip(gen
, program
.assembly
.splitlines()))
247 yield from setup_tst_memory(l0
, sim
)
248 yield from setup_regs(core
, test
)
250 index
= sim
.pc
.CIA
.value
// 4
251 while index
< len(instructions
):
252 ins
, code
= instructions
[index
]
254 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
257 # ask the decoder to decode this binary data (endian'd)
258 yield core
.bigendian_i
.eq(bigendian
) # little / big?
259 yield instruction
.eq(ins
) # raw binary instr.
262 # fn_unit = yield pdecode2.e.fn_unit
263 #fuval = self.funit.value
264 #self.assertEqual(fn_unit & fuval, fuval)
266 # set operand and get inputs
267 yield from set_issue(core
, pdecode2
, sim
)
270 yield from wait_for_busy_clear(core
)
275 # call simulated operation
276 opname
= code
.split(' ')[0]
277 yield from sim
.call(opname
)
278 index
= sim
.pc
.CIA
.value
// 4
281 yield from check_regs(self
, sim
, core
, test
, code
)
284 yield from check_sim_memory(self
, l0
, sim
, code
)
286 sim
.add_sync_process(process
)
287 with sim
.write_vcd("core_simulator.vcd", "core_simulator.gtkw",
292 if __name__
== "__main__":
293 unittest
.main(exit
=False)
294 suite
= unittest
.TestSuite()
295 suite
.addTest(TestRunner(LDSTTestCase().test_data
))
296 suite
.addTest(TestRunner(CRTestCase().test_data
))
297 suite
.addTest(TestRunner(ShiftRotTestCase().test_data
))
298 suite
.addTest(TestRunner(LogicalTestCase().test_data
))
299 suite
.addTest(TestRunner(ALUTestCase().test_data
))
300 suite
.addTest(TestRunner(BranchTestCase().test_data
))
302 runner
= unittest
.TextTestRunner()