1 from nmigen
import Module
, Signal
2 from nmigen
.back
.pysim
import Simulator
, Delay
, Settle
3 from nmigen
.test
.utils
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
)
10 from soc
.decoder
.selectable_int
import SelectableInt
11 from soc
.simulator
.program
import Program
12 from soc
.decoder
.isa
.all
import ISA
14 from soc
.fu
.alu
.test
.test_pipe_caller
import TestCase
, ALUTestCase
, test_data
15 from soc
.fu
.compunits
.compunits
import ALUFunctionUnit
16 from soc
.experiment
.compalu_multi
import find_ok
# hack
19 def set_cu_input(cu
, idx
, data
):
20 rdop
= cu
.get_in_name(idx
)
21 yield cu
.src_i
[idx
].eq(data
)
23 rd_rel_o
= yield cu
.rd
.rel
[idx
]
24 print ("rd_rel %d wait HI" % idx
, rd_rel_o
, rdop
, hex(data
))
28 yield cu
.rd
.go
[idx
].eq(1)
31 rd_rel_o
= yield cu
.rd
.rel
[idx
]
34 print ("rd_rel %d wait HI" % idx
, rd_rel_o
)
36 yield cu
.rd
.go
[idx
].eq(0)
39 def get_cu_output(cu
, idx
, code
):
40 wrmask
= yield cu
.wrmask
41 wrop
= cu
.get_out_name(idx
)
42 wrok
= cu
.get_out(idx
)
43 fname
= find_ok(wrok
.fields
)
44 wrok
= yield getattr(wrok
, fname
)
45 print ("wr_rel mask", repr(code
), idx
, wrop
, bin(wrmask
), fname
, wrok
)
46 assert wrmask
& (1<<idx
), \
47 "get_cu_output '%s': mask bit %d not set\n" \
48 "write-operand '%s' Data.ok likely not set (%s)" \
49 % (code
, idx
, wrop
, hex(wrok
))
51 wr_relall_o
= yield cu
.wr
.rel
52 wr_rel_o
= yield cu
.wr
.rel
[idx
]
53 print ("wr_rel %d wait" % idx
, hex(wr_relall_o
), wr_rel_o
)
57 yield cu
.wr
.go
[idx
].eq(1)
59 result
= yield cu
.dest
[idx
]
61 yield cu
.wr
.go
[idx
].eq(0)
62 print ("result", repr(code
), idx
, wrop
, wrok
, hex(result
))
66 def get_cu_rd_mask(dec2
):
68 mask
= 0b1100 # XER CA/SO
70 reg3_ok
= yield dec2
.e
.read_reg3
.ok
71 reg1_ok
= yield dec2
.e
.read_reg1
.ok
73 if reg3_ok
or reg1_ok
:
76 # If there's an immediate, set the B operand to that
77 reg2_ok
= yield dec2
.e
.read_reg2
.ok
84 def set_cu_inputs(cu
, dec2
, sim
):
85 # TODO: see https://bugs.libre-soc.org/show_bug.cgi?id=305#c43
86 # detect the immediate here (with m.If(self.i.ctx.op.imm_data.imm_ok))
87 # and place it into data_i.b
89 reg3_ok
= yield dec2
.e
.read_reg3
.ok
90 reg1_ok
= yield dec2
.e
.read_reg1
.ok
91 assert reg3_ok
!= reg1_ok
93 data1
= yield dec2
.e
.read_reg3
.data
94 data1
= sim
.gpr(data1
).value
96 data1
= yield dec2
.e
.read_reg1
.data
97 data1
= sim
.gpr(data1
).value
101 if reg3_ok
or reg1_ok
:
102 yield from set_cu_input(cu
, 0, data1
)
104 # If there's an immediate, set the B operand to that
105 reg2_ok
= yield dec2
.e
.read_reg2
.ok
107 data2
= yield dec2
.e
.read_reg2
.data
108 data2
= sim
.gpr(data2
).value
113 yield from set_cu_input(cu
, 1, data2
)
116 def set_operand(cu
, dec2
, sim
):
117 yield from cu
.oper_i
.eq_from_execute1(dec2
.e
)
118 yield cu
.issue_i
.eq(1)
120 yield cu
.issue_i
.eq(0)
124 def set_extra_cu_inputs(cu
, dec2
, sim
):
125 carry
= 1 if sim
.spr
['XER'][XER_bits
['CA']] else 0
126 carry32
= 1 if sim
.spr
['XER'][XER_bits
['CA32']] else 0
127 yield from set_cu_input(cu
, 3, carry |
(carry32
<<1))
128 so
= 1 if sim
.spr
['XER'][XER_bits
['SO']] else 0
129 yield from set_cu_input(cu
, 2, so
)
133 class TestRunner(FHDLTestCase
):
134 def __init__(self
, test_data
):
135 super().__init
__("run_all")
136 self
.test_data
= test_data
141 instruction
= Signal(32)
143 pdecode
= create_pdecode()
145 m
.submodules
.pdecode2
= pdecode2
= PowerDecode2(pdecode
)
146 m
.submodules
.cu
= cu
= ALUFunctionUnit()
148 comb
+= pdecode2
.dec
.raw_opcode_in
.eq(instruction
)
154 yield cu
.issue_i
.eq(0)
157 for test
in self
.test_data
:
159 program
= test
.program
160 self
.subTest(test
.name
)
161 sim
= ISA(pdecode2
, test
.regs
, test
.sprs
, 0)
162 gen
= program
.generate_instructions()
163 instructions
= list(zip(gen
, program
.assembly
.splitlines()))
165 index
= sim
.pc
.CIA
.value
//4
166 while index
< len(instructions
):
167 ins
, code
= instructions
[index
]
169 print("0x{:X}".format(ins
& 0xffffffff))
172 # ask the decoder to decode this binary data (endian'd)
173 yield pdecode2
.dec
.bigendian
.eq(0) # little / big?
174 yield instruction
.eq(ins
) # raw binary instr.
176 fn_unit
= yield pdecode2
.e
.fn_unit
177 self
.assertEqual(fn_unit
, Function
.ALU
.value
)
178 # reset read-operand mask
179 rdmask
= yield from get_cu_rd_mask(pdecode2
)
180 yield cu
.rdmaskn
.eq(~rdmask
)
181 yield from set_operand(cu
, pdecode2
, sim
)
182 rd_rel_o
= yield cu
.rd
.rel
183 wr_rel_o
= yield cu
.wr
.rel
184 print ("before inputs, rd_rel, wr_rel: ",
185 bin(rd_rel_o
), bin(wr_rel_o
))
186 yield from set_cu_inputs(cu
, pdecode2
, sim
)
187 yield from set_extra_cu_inputs(cu
, pdecode2
, sim
)
189 rd_rel_o
= yield cu
.rd
.rel
190 wr_rel_o
= yield cu
.wr
.rel
191 wrmask
= yield cu
.wrmask
192 print ("after inputs, rd_rel, wr_rel, wrmask: ",
193 bin(rd_rel_o
), bin(wr_rel_o
), bin(wrmask
))
194 opname
= code
.split(' ')[0]
195 yield from sim
.call(opname
)
196 index
= sim
.pc
.CIA
.value
//4
198 out_reg_valid
= yield pdecode2
.e
.write_reg
.ok
203 write_reg_idx
= yield pdecode2
.e
.write_reg
.data
204 expected
= sim
.gpr(write_reg_idx
).value
205 cu_out
= yield from get_cu_output(cu
, 0, code
)
206 print(f
"expected {expected:x}, actual: {cu_out:x}")
207 self
.assertEqual(expected
, cu_out
, code
)
211 yield from self
.check_extra_cu_outputs(cu
, pdecode2
,
215 busy_o
= yield cu
.busy_o
217 for i
in range(cu
.n_dst
):
218 wr_rel_o
= yield cu
.wr
.rel
[i
]
220 print ("discard output", i
)
221 discard
= yield from get_cu_output(cu
, i
, code
)
224 sim
.add_sync_process(process
)
225 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
229 def check_extra_cu_outputs(self
, cu
, dec2
, sim
, code
):
230 rc
= yield dec2
.e
.rc
.data
231 op
= yield dec2
.e
.insn_type
232 cridx_ok
= yield dec2
.e
.write_cr
.ok
233 cridx
= yield dec2
.e
.write_cr
.data
235 print ("check extra output", repr(code
), cridx_ok
, cridx
)
238 self
.assertEqual(cridx_ok
, 1, code
)
239 self
.assertEqual(cridx
, 0, code
)
242 cr_expected
= sim
.crl
[cridx
].get_range().value
243 cr_actual
= yield from get_cu_output(cu
, 1, code
)
244 print ("CR", cridx
, cr_expected
, cr_actual
)
245 self
.assertEqual(cr_expected
, cr_actual
, "CR%d %s" % (cridx
, code
))
247 cry_out
= yield dec2
.e
.output_carry
249 expected_carry
= 1 if sim
.spr
['XER'][XER_bits
['CA']] else 0
250 xer_ca
= yield from get_cu_output(cu
, 2, code
)
251 real_carry
= xer_ca
& 0b1 # XXX CO not CO32
252 self
.assertEqual(expected_carry
, real_carry
, code
)
253 expected_carry32
= 1 if sim
.spr
['XER'][XER_bits
['CA32']] else 0
254 real_carry32
= bool(xer_ca
& 0b10) # XXX CO32
255 self
.assertEqual(expected_carry32
, real_carry32
, code
)
258 #xer_ov = yield from get_cu_output(cu, 3, code)
259 #xer_so = yield from get_cu_output(cu, 4, code)
262 if __name__
== "__main__":
263 unittest
.main(exit
=False)
264 suite
= unittest
.TestSuite()
265 suite
.addTest(TestRunner(test_data
))
267 runner
= unittest
.TextTestRunner()