0c5ec46a4f0ebc6bc48c2213832a1e35c160c117
1 from nmigen
import Module
, Signal
2 from nmigen
.back
.pysim
import Simulator
, Delay
3 from nmigen
.test
.utils
import FHDLTestCase
5 from soc
.decoder
.isa
.caller
import ISACaller
6 from soc
.decoder
.power_decoder
import (create_pdecode
)
7 from soc
.decoder
.power_decoder2
import (PowerDecode2
)
8 from soc
.simulator
.program
import Program
9 from soc
.decoder
.isa
.caller
import ISACaller
, inject
10 from soc
.decoder
.selectable_int
import SelectableInt
11 from soc
.decoder
.orderedset
import OrderedSet
12 from soc
.decoder
.isa
.all
import ISA
16 def __init__(self
, num
):
20 class DecoderTestCase(FHDLTestCase
):
22 def run_tst(self
, generator
, initial_regs
):
25 instruction
= Signal(32)
27 pdecode
= create_pdecode()
28 simulator
= ISA(pdecode
, initial_regs
)
30 m
.submodules
.pdecode2
= pdecode2
= PowerDecode2(pdecode
)
31 comb
+= pdecode2
.dec
.raw_opcode_in
.eq(instruction
)
33 gen
= generator
.generate_instructions()
36 for ins
, code
in zip(gen
, generator
.assembly
.splitlines()):
38 print("0x{:X}".format(ins
& 0xffffffff))
41 # ask the decoder to decode this binary data (endian'd)
42 yield pdecode2
.dec
.bigendian
.eq(0) # little / big?
43 yield instruction
.eq(ins
) # raw binary instr.
45 opname
= code
.split(' ')[0]
46 yield from simulator
.call(opname
)
48 sim
.add_process(process
)
49 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
56 initial_regs
= [0] * 32
57 initial_regs
[3] = 0x1234
58 initial_regs
[2] = 0x4321
59 with
Program(lst
) as program
:
60 sim
= self
.run_tst_program(program
, initial_regs
)
61 self
.assertEqual(sim
.gpr(1), SelectableInt(0x5555, 64))
64 lst
= ["addi 3, 0, 0x1234",
67 with
Program(lst
) as program
:
68 sim
= self
.run_tst_program(program
)
70 self
.assertEqual(sim
.gpr(1), SelectableInt(0x5555, 64))
72 def test_load_store(self
):
73 lst
= ["addi 1, 0, 0x0010",
77 with
Program(lst
) as program
:
78 sim
= self
.run_tst_program(program
)
80 self
.assertEqual(sim
.gpr(3), SelectableInt(0x1234, 64))
82 def test_addpcis(self
):
83 lst
= ["addpcis 1, 0x1",
86 with
Program(lst
) as program
:
87 sim
= self
.run_tst_program(program
)
88 self
.assertEqual(sim
.gpr(1), SelectableInt(0x10004, 64))
89 self
.assertEqual(sim
.gpr(2), SelectableInt(0x10008, 64))
90 self
.assertEqual(sim
.gpr(3), SelectableInt(0x1000c, 64))
92 @unittest.skip("broken") # FIXME
95 # 0x7654 gives expected (3+4) (2+4) (1+4) (0+4) for i=3,2,1,0
96 lst
= ["addi %d, 0, 0x7654" % (i
+1),
97 "mtcrf %d, %d" % (1 << i
, i
+1),
99 with
Program(lst
) as program
:
100 sim
= self
.run_tst_program(program
)
104 self
.assertEqual(sim
.cr
, SelectableInt(expected
<< (i
*4), 32))
105 # check CR[0]/1/2/3 as well
106 print("cr%d", sim
.crl
[i
])
107 self
.assertTrue(SelectableInt(expected
, 4) == sim
.crl
[i
])
109 def run_tst_program(self
, prog
, initial_regs
=[0] * 32):
110 simulator
= self
.run_tst(prog
, initial_regs
)
115 if __name__
== "__main__":