1 from nmigen
import Module
, Signal
, ResetSignal
3 # NOTE: to use cxxsim, export NMIGEN_SIM_MODE=cxxsim from the shell
4 # Also, check out the cxxsim nmigen branch, and latest yosys from git
5 from nmutil
.sim_tmp_alternative
import Simulator
, Settle
7 from nmutil
.formaltest
import FHDLTestCase
8 from nmigen
.cli
import rtlil
10 from soc
.decoder
.power_decoder
import create_pdecode
11 from soc
.decoder
.power_decoder2
import PowerDecode2
, get_rdflags
12 from soc
.decoder
.power_enums
import Function
13 from soc
.decoder
.isa
.all
import ISA
15 from soc
.experiment
.compalu_multi
import find_ok
# hack
16 from soc
.config
.test
.test_loadstore
import TestMemPspec
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_o
[idx
]
24 print("rd_rel %d wait HI" % idx
, rd_rel_o
, rdop
, hex(data
))
28 yield cu
.rd
.go_i
[idx
].eq(1)
31 rd_rel_o
= yield cu
.rd
.rel_o
[idx
]
34 print("rd_rel %d wait HI" % idx
, rd_rel_o
)
36 yield cu
.rd
.go_i
[idx
].eq(0)
37 yield cu
.src_i
[idx
].eq(0)
40 def get_cu_output(cu
, idx
, code
):
41 wrmask
= yield cu
.wrmask
42 wrop
= cu
.get_out_name(idx
)
43 wrok
= cu
.get_out(idx
)
44 fname
= find_ok(wrok
.fields
)
45 wrok
= yield getattr(wrok
, fname
)
46 print("wr_rel mask", repr(code
), idx
, wrop
, bin(wrmask
), fname
, wrok
)
47 assert wrmask
& (1 << idx
), \
48 "get_cu_output '%s': mask bit %d not set\n" \
49 "write-operand '%s' Data.ok likely not set (%s)" \
50 % (code
, idx
, wrop
, hex(wrok
))
52 wr_relall_o
= yield cu
.wr
.rel_o
53 wr_rel_o
= yield cu
.wr
.rel_o
[idx
]
54 print("wr_rel %d wait" % idx
, hex(wr_relall_o
), wr_rel_o
)
58 yield cu
.wr
.go_i
[idx
].eq(1)
60 result
= yield cu
.dest
[idx
]
62 yield cu
.wr
.go_i
[idx
].eq(0)
63 print("result", repr(code
), idx
, wrop
, wrok
, hex(result
))
68 def set_cu_inputs(cu
, inp
):
69 print("set_cu_inputs", inp
)
70 for idx
, data
in inp
.items():
71 yield from set_cu_input(cu
, idx
, data
)
72 # gets out of sync when checking busy if there is no wait, here.
74 yield # wait one cycle
77 def set_operand(cu
, dec2
, sim
):
78 yield from cu
.oper_i
.eq_from_execute1(dec2
.e
)
79 yield cu
.issue_i
.eq(1)
81 yield cu
.issue_i
.eq(0)
85 def get_cu_outputs(cu
, code
):
87 # wait for pipeline to indicate valid. this because for long
88 # pipelines (or FSMs) the write mask is only valid at that time.
89 if hasattr(cu
, "alu"): # ALU CompUnits
91 valid_o
= yield cu
.alu
.n
.valid_o
96 # not a lot can be done about this - simply wait a few cycles
100 wrmask
= yield cu
.wrmask
101 wr_rel_o
= yield cu
.wr
.rel_o
102 print("get_cu_outputs", cu
.n_dst
, wrmask
, wr_rel_o
)
103 # no point waiting (however really should doublecheck wr.rel)
106 # wait for at least one result
108 wr_rel_o
= yield cu
.wr
.rel_o
112 for i
in range(cu
.n_dst
):
113 wr_rel_o
= yield cu
.wr
.rel_o
[i
]
115 result
= yield from get_cu_output(cu
, i
, code
)
116 wrop
= cu
.get_out_name(i
)
117 print("output", i
, wrop
, hex(result
))
122 def get_inp_indexed(cu
, inp
):
124 for i
in range(cu
.n_src
):
125 wrop
= cu
.get_in_name(i
)
131 def get_l0_mem(l0
): # BLECH!
132 if hasattr(l0
.pimem
, 'lsui'):
133 return l0
.pimem
.lsui
.mem
134 return l0
.pimem
.mem
.mem
137 def setup_test_memory(l0
, sim
):
139 print("before, init mem", mem
.depth
, mem
.width
, mem
)
140 for i
in range(mem
.depth
):
141 data
= sim
.mem
.ld(i
*8, 8, False)
142 print("init ", i
, hex(data
))
143 yield mem
._array
[i
].eq(data
)
145 for k
, v
in sim
.mem
.mem
.items():
146 print(" %6x %016x" % (k
, v
))
147 print("before, nmigen mem dump")
148 for i
in range(mem
.depth
):
149 actual_mem
= yield mem
._array
[i
]
150 print(" %6i %016x" % (i
, actual_mem
))
153 def dump_sim_memory(dut
, l0
, sim
, code
):
155 print("sim mem dump")
156 for k
, v
in sim
.mem
.mem
.items():
157 print(" %6x %016x" % (k
, v
))
158 print("nmigen mem dump")
159 for i
in range(mem
.depth
):
160 actual_mem
= yield mem
._array
[i
]
161 print(" %6i %016x" % (i
, actual_mem
))
164 def check_sim_memory(dut
, l0
, sim
, code
):
167 for i
in range(mem
.depth
):
168 expected_mem
= sim
.mem
.ld(i
*8, 8, False)
169 actual_mem
= yield mem
._array
[i
]
170 dut
.assertEqual(expected_mem
, actual_mem
,
171 "%s %d %x %x" % (code
, i
,
172 expected_mem
, actual_mem
))
175 class TestRunner(FHDLTestCase
):
176 def __init__(self
, test_data
, fukls
, iodef
, funit
, bigendian
):
177 super().__init
__("run_all")
178 self
.test_data
= test_data
182 self
.bigendian
= bigendian
184 def execute(self
, cu
, l0
, instruction
, pdecode2
, simdec2
, test
):
186 program
= test
.program
187 print("test", test
.name
, test
.mem
)
188 gen
= list(program
.generate_instructions())
189 insncode
= program
.assembly
.splitlines()
190 instructions
= list(zip(gen
, insncode
))
191 sim
= ISA(simdec2
, test
.regs
, test
.sprs
, test
.cr
, test
.mem
,
193 initial_insns
=gen
, respect_pc
=True,
194 disassembly
=insncode
,
195 bigendian
=self
.bigendian
)
198 if self
.funit
== Function
.LDST
:
199 yield from setup_test_memory(l0
, sim
)
201 pc
= sim
.pc
.CIA
.value
205 print("instr pc", pc
)
207 yield from sim
.setup_one()
208 except KeyError: # indicates instruction not in imem: stop
211 ins
, code
= instructions
[index
]
212 print("instruction @", index
, code
)
214 # ask the decoder to decode this binary data (endian'd)
215 yield pdecode2
.dec
.bigendian
.eq(self
.bigendian
) # le / be?
216 yield pdecode2
.state
.msr
.eq(msr
) # set MSR "state"
217 yield pdecode2
.state
.pc
.eq(pc
) # set PC "state"
218 yield instruction
.eq(ins
) # raw binary instr.
220 # debugging issue with branch
221 if self
.funit
== Function
.BRANCH
:
222 lk
= yield pdecode2
.e
.do
.lk
223 fast_out2
= yield pdecode2
.e
.write_fast2
.data
224 fast_out2_ok
= yield pdecode2
.e
.write_fast2
.ok
225 print("lk:", lk
, fast_out2
, fast_out2_ok
)
226 op_lk
= yield cu
.alu
.pipe1
.p
.data_i
.ctx
.op
.lk
227 print("op_lk:", op_lk
)
228 print(dir(cu
.alu
.pipe1
.n
.data_o
))
229 fn_unit
= yield pdecode2
.e
.do
.fn_unit
230 fuval
= self
.funit
.value
231 self
.assertEqual(fn_unit
& fuval
, fuval
)
233 # set operand and get inputs
234 yield from set_operand(cu
, pdecode2
, sim
)
235 # reset read-operand mask
236 rdmask
= get_rdflags(pdecode2
.e
, cu
)
237 #print ("hardcoded rdmask", cu.rdflags(pdecode2.e))
238 #print ("decoder rdmask", rdmask)
239 yield cu
.rdmaskn
.eq(~rdmask
)
242 iname
= yield from self
.iodef
.get_cu_inputs(pdecode2
, sim
)
243 inp
= get_inp_indexed(cu
, iname
)
245 # reset write-operand mask
246 for idx
in range(cu
.n_dst
):
247 wrok
= cu
.get_out(idx
)
248 fname
= find_ok(wrok
.fields
)
249 yield getattr(wrok
, fname
).eq(0)
254 rd_rel_o
= yield cu
.rd
.rel_o
255 wr_rel_o
= yield cu
.wr
.rel_o
256 print("before inputs, rd_rel, wr_rel: ",
257 bin(rd_rel_o
), bin(wr_rel_o
))
258 assert wr_rel_o
== 0, "wr.rel %s must be zero. "\
259 "previous instr not written all regs\n"\
261 (bin(wr_rel_o
), cu
.rwid
[1])
262 yield from set_cu_inputs(cu
, inp
)
263 rd_rel_o
= yield cu
.rd
.rel_o
264 wr_rel_o
= yield cu
.wr
.rel_o
265 wrmask
= yield cu
.wrmask
266 print("after inputs, rd_rel, wr_rel, wrmask: ",
267 bin(rd_rel_o
), bin(wr_rel_o
), bin(wrmask
))
269 # call simulated operation
270 yield from sim
.execute_one()
272 pc
= sim
.pc
.CIA
.value
276 # get all outputs (one by one, just "because")
277 res
= yield from get_cu_outputs(cu
, code
)
278 wrmask
= yield cu
.wrmask
279 rd_rel_o
= yield cu
.rd
.rel_o
280 wr_rel_o
= yield cu
.wr
.rel_o
281 print("after got outputs, rd_rel, wr_rel, wrmask: ",
282 bin(rd_rel_o
), bin(wr_rel_o
), bin(wrmask
))
284 # wait for busy to go low
286 busy_o
= yield cu
.busy_o
287 print("busy", busy_o
)
292 # reset read-mask. IMPORTANT when there are no operands
293 yield cu
.rdmaskn
.eq(0)
296 # debugging issue with branch
297 if self
.funit
== Function
.BRANCH
:
298 lr
= yield cu
.alu
.pipe1
.n
.data_o
.lr
.data
299 lr_ok
= yield cu
.alu
.pipe1
.n
.data_o
.lr
.ok
300 print("lr:", hex(lr
), lr_ok
)
302 if self
.funit
== Function
.LDST
:
303 yield from dump_sim_memory(self
, l0
, sim
, code
)
305 # sigh. hard-coded. test memory
306 if self
.funit
== Function
.LDST
:
307 yield from check_sim_memory(self
, l0
, sim
, code
)
308 yield from self
.iodef
.check_cu_outputs(res
, pdecode2
,
312 yield from self
.iodef
.check_cu_outputs(res
, pdecode2
,
319 instruction
= Signal(32)
321 pdecode
= create_pdecode()
322 m
.submodules
.pdecode2
= pdecode2
= PowerDecode2(pdecode
)
324 # copy of the decoder for simulator
325 simdec
= create_pdecode()
326 simdec2
= PowerDecode2(simdec
)
327 m
.submodules
.simdec2
= simdec2
# pain in the neck
329 if self
.funit
== Function
.LDST
:
330 from soc
.experiment
.l0_cache
import TstL0CacheBuffer
331 pspec
= TestMemPspec(ldst_ifacetype
='test_bare_wb',
335 m
.submodules
.l0
= l0
= TstL0CacheBuffer(pspec
, n_units
=1)
337 m
.submodules
.cu
= cu
= self
.fukls(pi
, idx
=0, awid
=3)
338 m
.d
.comb
+= cu
.ad
.go_i
.eq(cu
.ad
.rel_o
) # link addr direct to rel
339 m
.d
.comb
+= cu
.st
.go_i
.eq(cu
.st
.rel_o
) # link store direct to rel
341 m
.submodules
.cu
= cu
= self
.fukls(0)
344 comb
+= pdecode2
.dec
.raw_opcode_in
.eq(instruction
)
350 yield cu
.issue_i
.eq(0)
353 for test
in self
.test_data
:
355 with self
.subTest(test
.name
):
356 yield from self
.execute(cu
, l0
, instruction
,
360 sim
.add_sync_process(process
)
362 name
= self
.funit
.name
.lower()
363 with sim
.write_vcd("%s_simulator.vcd" % name
,