sorting out bigendian/littleendian including in qemu
[soc.git] / src / soc / fu / compunits / test / test_compunit.py
index ee3c6fce44905acb3dc4f1eec641b426c909f2d4..7a73cb83f5f61d29d612b18a0b522e68ff2738ef 100644 (file)
@@ -9,6 +9,7 @@ from soc.decoder.power_enums import Function
 from soc.decoder.isa.all import ISA
 
 from soc.experiment.compalu_multi import find_ok # hack
+from soc.config.test.test_loadstore import TestMemPspec
 
 
 def set_cu_input(cu, idx, data):
@@ -61,6 +62,7 @@ def get_cu_output(cu, idx, code):
 
 
 def set_cu_inputs(cu, inp):
+    print ("set_cu_inputs", inp)
     for idx, data in inp.items():
         yield from set_cu_input(cu, idx, data)
 
@@ -103,14 +105,56 @@ def get_inp_indexed(cu, inp):
             res[i] = inp[wrop]
     return res
 
+def get_l0_mem(l0): # BLECH!
+    if hasattr(l0.pimem, 'lsui'):
+        return l0.pimem.lsui.mem
+    return l0.pimem.mem.mem
+
+def setup_test_memory(l0, sim):
+    mem = get_l0_mem(l0)
+    print ("before, init mem", mem.depth, mem.width, mem)
+    for i in range(mem.depth):
+        data = sim.mem.ld(i*8, 8, False)
+        print ("init ", i, hex(data))
+        yield mem._array[i].eq(data)
+    yield Settle()
+    for k, v in sim.mem.mem.items():
+        print ("    %6x %016x" % (k, v))
+    print ("before, nmigen mem dump")
+    for i in range(mem.depth):
+        actual_mem = yield mem._array[i]
+        print ("    %6i %016x" % (i, actual_mem))
+
+
+def dump_sim_memory(dut, l0, sim, code):
+    mem = get_l0_mem(l0)
+    print ("sim mem dump")
+    for k, v in sim.mem.mem.items():
+        print ("    %6x %016x" % (k, v))
+    print ("nmigen mem dump")
+    for i in range(mem.depth):
+        actual_mem = yield mem._array[i]
+        print ("    %6i %016x" % (i, actual_mem))
+
+
+def check_sim_memory(dut, l0, sim, code):
+    mem = get_l0_mem(l0)
+
+    for i in range(mem.depth):
+        expected_mem = sim.mem.ld(i*8, 8, False)
+        actual_mem = yield mem._array[i]
+        dut.assertEqual(expected_mem, actual_mem,
+                "%s %d %x %x" % (code, i,
+                                 expected_mem, actual_mem))
 
 class TestRunner(FHDLTestCase):
-    def __init__(self, test_data, fukls, iodef, funit):
+    def __init__(self, test_data, fukls, iodef, funit, bigendian):
         super().__init__("run_all")
         self.test_data = test_data
         self.fukls = fukls
         self.iodef = iodef
         self.funit = funit
+        self.bigendian = bigendian
 
     def run_all(self):
         m = Module()
@@ -118,17 +162,26 @@ class TestRunner(FHDLTestCase):
         instruction = Signal(32)
 
         pdecode = create_pdecode()
-
         m.submodules.pdecode2 = pdecode2 = PowerDecode2(pdecode)
+
+        # copy of the decoder for simulator
+        simdec = create_pdecode()
+        simdec2 = PowerDecode2(simdec)
+        m.submodules.simdec2 = simdec2 # pain in the neck
+
         if self.funit == Function.LDST:
             from soc.experiment.l0_cache import TstL0CacheBuffer
-            m.submodules.l0 = l0 = TstL0CacheBuffer(n_units=1, regwid=64)
-            pi = l0.l0.dports[0].pi
-            m.submodules.cu = cu = self.fukls(pi, awid=4)
+            pspec = TestMemPspec(ldst_ifacetype='test_bare_wb',
+                                 addr_wid=48,
+                                 mask_wid=8,
+                                 reg_wid=64)
+            m.submodules.l0 = l0 = TstL0CacheBuffer(pspec, n_units=1)
+            pi = l0.l0.dports[0]
+            m.submodules.cu = cu = self.fukls(pi, idx=0, awid=3)
             m.d.comb += cu.ad.go.eq(cu.ad.rel) # link addr-go direct to rel
             m.d.comb += cu.st.go.eq(cu.st.rel) # link store-go direct to rel
         else:
-            m.submodules.cu = cu = self.fukls()
+            m.submodules.cu = cu = self.fukls(0)
 
         comb += pdecode2.dec.raw_opcode_in.eq(instruction)
         sim = Simulator(m)
@@ -144,39 +197,40 @@ class TestRunner(FHDLTestCase):
                 program = test.program
                 self.subTest(test.name)
                 print ("test", test.name, test.mem)
-                sim = ISA(pdecode2, test.regs, test.sprs, test.cr, test.mem,
-                          test.msr)
-                gen = program.generate_instructions()
-                instructions = list(zip(gen, program.assembly.splitlines()))
+                gen = list(program.generate_instructions())
+                insncode = program.assembly.splitlines()
+                instructions = list(zip(gen, insncode))
+                sim = ISA(simdec2, test.regs, test.sprs, test.cr, test.mem,
+                          test.msr,
+                          initial_insns=gen, respect_pc=False,
+                          disassembly=insncode,
+                          bigendian=self.bigendian)
 
                 # initialise memory
                 if self.funit == Function.LDST:
-                    mem = l0.mem.mem
-                    memlist = []
-                    for i in range(mem.depth//2):
-                        data = sim.mem.ld(i*16, 8)
-                        data1 = sim.mem.ld(i*16+8, 8)
-                        yield mem._array[i].eq(data | (data1<<32))
-                    print (mem, mem.depth, mem.width)
-                    print ("mem init", list(map(hex,memlist)))
+                    yield from setup_test_memory(l0, sim)
 
                 index = sim.pc.CIA.value//4
-                while index < len(instructions):
+                while True:
+                    try:
+                        yield from sim.setup_one()
+                    except KeyError: # indicates instruction not in imem: stop
+                        break
+                    yield Settle()
                     ins, code = instructions[index]
-
-                    print("0x{:X}".format(ins & 0xffffffff))
-                    print(code)
+                    print(index, code)
 
                     # ask the decoder to decode this binary data (endian'd)
-                    yield pdecode2.dec.bigendian.eq(0)  # little / big?
+                    yield pdecode2.dec.bigendian.eq(self.bigendian)  # le / be?
                     yield instruction.eq(ins)          # raw binary instr.
                     yield Settle()
-                    fn_unit = yield pdecode2.e.fn_unit
+                    fn_unit = yield pdecode2.e.do.fn_unit
                     fuval = self.funit.value
                     self.assertEqual(fn_unit & fuval, fuval)
 
                     # set operand and get inputs
                     yield from set_operand(cu, pdecode2, sim)
+                    yield Settle()
                     iname = yield from self.iodef.get_cu_inputs(pdecode2, sim)
                     inp = get_inp_indexed(cu, iname)
 
@@ -204,7 +258,6 @@ class TestRunner(FHDLTestCase):
                                 "respec %s" % \
                                 (bin(wr_rel_o), cu.rwid[1])
                     yield from set_cu_inputs(cu, inp)
-                    yield
                     rd_rel_o = yield cu.rd.rel
                     wr_rel_o = yield cu.wr.rel
                     wrmask = yield cu.wrmask
@@ -212,11 +265,10 @@ class TestRunner(FHDLTestCase):
                             bin(rd_rel_o), bin(wr_rel_o), bin(wrmask))
 
                     # call simulated operation
-                    opname = code.split(' ')[0]
-                    yield from sim.call(opname)
+                    yield from sim.execute_one()
+                    yield Settle()
                     index = sim.pc.CIA.value//4
 
-                    yield Settle()
                     # get all outputs (one by one, just "because")
                     res = yield from get_cu_outputs(cu, code)
                     wrmask = yield cu.wrmask
@@ -233,12 +285,21 @@ class TestRunner(FHDLTestCase):
                             break
                         yield
 
-                    yield from self.iodef.check_cu_outputs(res, pdecode2,
-                                                            sim, code)
+                    if self.funit == Function.LDST:
+                        yield from dump_sim_memory(self, l0, sim, code)
+
 
                     # sigh.  hard-coded.  test memory
                     if self.funit == Function.LDST:
-                        print ("mem dump", sim.mem.mem)
+                        yield from check_sim_memory(self, l0, sim, code)
+                        yield from self.iodef.check_cu_outputs(res, pdecode2,
+                                                                sim, cu,
+                                                                code)
+                    else:
+                        yield from self.iodef.check_cu_outputs(res, pdecode2,
+                                                                sim, cu.alu,
+                                                                code)
+
 
         sim.add_sync_process(process)