1 """simple core test, runs instructions from a TestMemory
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 soc
.decoder
.isa
.caller
import special_sprs
13 from soc
.decoder
.isa
.all
import ISA
14 from soc
.decoder
.power_enums
import Function
, XER_bits
15 from soc
.config
.endian
import bigendian
17 from soc
.simple
.issuer
import TestIssuer
18 from soc
.experiment
.compalu_multi
import find_ok
# hack
20 from soc
.config
.test
.test_loadstore
import TestMemPspec
21 from soc
.simple
.test
.test_core
import (setup_regs
, check_regs
,
24 from soc
.fu
.compunits
.test
.test_compunit
import (setup_test_memory
,
26 from soc
.debug
.dmi
import DBGCore
, DBGCtrl
, DBGStat
28 # test with ALU data and Logical data
29 #from soc.fu.alu.test.test_pipe_caller import ALUTestCase
30 #from soc.fu.div.test.test_pipe_caller import DivTestCase
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.spr.test.test_pipe_caller import SPRTestCase
36 from soc
.fu
.ldst
.test
.test_pipe_caller
import LDSTTestCase
37 from soc
.simulator
.test_sim
import (GeneralTestCases
, AttnTestCase
)
38 #from soc.simulator.test_helloworld_sim import HelloTestCases
41 def setup_i_memory(imem
, startaddr
, instructions
):
43 print("insn before, init mem", mem
.depth
, mem
.width
, mem
,
45 for i
in range(mem
.depth
):
46 yield mem
._array
[i
].eq(0)
48 startaddr
//= 4 # instructions are 32-bit
51 for ins
in instructions
:
52 if isinstance(ins
, tuple):
56 insn
= insn
& 0xffffffff
57 yield mem
._array
[startaddr
].eq(insn
)
60 print("instr: %06x 0x%x %s" % (4*startaddr
, insn
, code
))
62 startaddr
= startaddr
& mask
67 for ins
in instructions
:
68 if isinstance(ins
, tuple):
72 insn
= insn
& 0xffffffff
73 msbs
= (startaddr
>> 1) & mask
74 val
= yield mem
._array
[msbs
]
76 print("before set", hex(4*startaddr
),
77 hex(msbs
), hex(val
), hex(insn
))
78 lsb
= 1 if (startaddr
& 1) else 0
79 val
= (val |
(insn
<< (lsb
*32)))
81 yield mem
._array
[msbs
].eq(val
)
84 print("after set", hex(4*startaddr
), hex(msbs
), hex(val
))
85 print("instr: %06x 0x%x %s %08x" % (4*startaddr
, insn
, code
, val
))
87 startaddr
= startaddr
& mask
90 def set_dmi(dmi
, addr
, data
):
92 yield dmi
.addr_i
.eq(addr
)
93 yield dmi
.din
.eq(data
)
100 yield dmi
.req_i
.eq(0)
101 yield dmi
.addr_i
.eq(0)
106 class TestRunner(FHDLTestCase
):
107 def __init__(self
, tst_data
):
108 super().__init
__("run_all")
109 self
.test_data
= tst_data
116 pspec
= TestMemPspec(ldst_ifacetype
='test_bare_wb',
117 imem_ifacetype
='test_bare_wb',
122 m
.submodules
.issuer
= issuer
= TestIssuer(pspec
)
123 imem
= issuer
.imem
._get
_memory
()
126 pdecode2
= core
.pdecode2
129 comb
+= issuer
.pc_i
.data
.eq(pc_i
)
138 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.STOP
)
142 for test
in self
.test_data
:
145 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.RESET
)
147 # set up bigendian (TODO: don't do this, use MSR)
148 yield issuer
.core_bigendian_i
.eq(bigendian
)
157 program
= test
.program
158 self
.subTest(test
.name
)
159 print("regs", test
.regs
)
160 print("sprs", test
.sprs
)
162 print("mem", test
.mem
)
163 print("msr", test
.msr
)
164 print("assem", program
.assembly
)
165 gen
= list(program
.generate_instructions())
166 insncode
= program
.assembly
.splitlines()
167 instructions
= list(zip(gen
, insncode
))
168 sim
= ISA(pdecode2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
170 initial_insns
=gen
, respect_pc
=True,
171 disassembly
=insncode
,
174 pc
= 0 # start address
175 counter
= 0 # test to pause/start
177 yield from setup_i_memory(imem
, pc
, instructions
)
178 yield from setup_test_memory(l0
, sim
)
179 yield from setup_regs(core
, test
)
182 yield issuer
.pc_i
.ok
.eq(1)
184 print("instructions", instructions
)
186 index
= sim
.pc
.CIA
.value
//4
187 while index
< len(instructions
):
188 ins
, code
= instructions
[index
]
190 print("instruction: 0x{:X}".format(ins
& 0xffffffff))
196 yield from set_dmi(dmi
, DBGCore
.CTRL
, 1<<DBGCtrl
.START
)
197 yield issuer
.pc_i
.ok
.eq(0) # no change PC after this
201 counter
= counter
+ 1
203 # wait until executed
204 yield from wait_for_busy_hi(core
)
205 yield from wait_for_busy_clear(core
)
207 terminated
= yield issuer
.dbg
.terminated_o
208 print("terminated", terminated
)
211 # call simulated operation
212 opname
= code
.split(' ')[0]
213 yield from sim
.call(opname
)
215 index
= sim
.pc
.CIA
.value
//4
218 yield from check_regs(self
, sim
, core
, test
, code
)
221 yield from check_sim_memory(self
, l0
, sim
, code
)
223 terminated
= yield issuer
.dbg
.terminated_o
227 sim
.add_sync_process(process
)
228 with sim
.write_vcd("issuer_simulator.vcd",
233 if __name__
== "__main__":
234 unittest
.main(exit
=False)
235 suite
= unittest
.TestSuite()
236 # suite.addTest(TestRunner(HelloTestCases.test_data))
237 # suite.addTest(TestRunner(DivTestCase.test_data))
238 # suite.addTest(TestRunner(AttnTestCase.test_data))
239 suite
.addTest(TestRunner(GeneralTestCases
.test_data
))
240 # suite.addTest(TestRunner(LDSTTestCase().test_data))
241 # suite.addTest(TestRunner(CRTestCase().test_data))
242 # suite.addTest(TestRunner(ShiftRotTestCase.test_data))
243 # suite.addTest(TestRunner(LogicalTestCase.test_data))
244 # suite.addTest(TestRunner(ALUTestCase.test_data))
245 # suite.addTest(TestRunner(BranchTestCase.test_data))
246 # suite.addTest(TestRunner(SPRTestCase.test_data))
248 runner
= unittest
.TextTestRunner()