Fix broken unit tests in test_caller
[soc.git] / src / soc / decoder / isa / caller.py
index e2a4f89d1a4b88c5d0ec7d0330f5aa701811d6d4..299b74c0e136a65a0f1a68955e6ccda786e2cf99 100644 (file)
@@ -151,7 +151,8 @@ class SPR(dict):
             return dict.__getitem__(self, key)
         else:
             info = spr_dict[key]
-            return SelectableInt(0, info.length)
+            dict.__setitem__(self, key, SelectableInt(0, info.length))
+            return dict.__getitem__(self, key)
 
     def __setitem__(self, key, value):
         if isinstance(key, SelectableInt):
@@ -167,7 +168,7 @@ class SPR(dict):
 class ISACaller:
     # decoder2 - an instance of power_decoder2
     # regfile - a list of initial values for the registers
-    def __init__(self, decoder2, regfile, initial_sprs={}):
+    def __init__(self, decoder2, regfile, initial_sprs={}, initial_cr=0):
         self.gpr = GPR(decoder2, regfile)
         self.mem = Mem()
         self.pc = PC()
@@ -178,14 +179,15 @@ class ISACaller:
         #            note that mffs, mcrfs, mtfsf "manage" this FPSCR
         # 2.3.1 CR (and sub-fields CR0..CR6 - CR0 SO comes from XER.SO)
         #         note that mfocrf, mfcr, mtcr, mtocrf, mcrxrx "manage" CRs
-        # 2.3.2 LR   (actually SPR #8)
-        # 2.3.3 CTR  (actually SPR #9)
+        #         -- Done
+        # 2.3.2 LR   (actually SPR #8) -- Done
+        # 2.3.3 CTR  (actually SPR #9) -- Done
         # 2.3.4 TAR  (actually SPR #815)
-        # 3.2.2 p45 XER  (actually SPR #0)
+        # 3.2.2 p45 XER  (actually SPR #1) -- Done
         # 3.2.3 p46 p232 VRSAVE (actually SPR #256)
 
         # create CR then allow portions of it to be "selectable" (below)
-        self._cr = SelectableInt(0, 64) # underlying reg
+        self._cr = SelectableInt(initial_cr, 64) # underlying reg
         self.cr = FieldSelectableInt(self._cr, list(range(32,64)))
 
         # "undefined", just set to variable-bit-width int (use exts "max")
@@ -230,7 +232,7 @@ class ISACaller:
             else:
                 sig = getattr(fields, name)
             val = yield sig
-            if name == 'BF':
+            if name in ['BF', 'BFA']:
                 self.namespace[name] = val
             else:
                 self.namespace[name] = SelectableInt(val, sig.width)
@@ -238,17 +240,52 @@ class ISACaller:
         self.namespace['XER'] = self.spr['XER']
         self.namespace['CA'] = self.spr['XER'][XER_bits['CA']].value
 
-    def handle_carry(self, inputs, outputs):
-        inv_a = yield self.dec2.invert_a
+    def handle_carry_(self, inputs, outputs):
+        inv_a = yield self.dec2.e.invert_a
         if inv_a:
             inputs[0] = ~inputs[0]
+
+        imm_ok = yield self.dec2.e.imm_data.ok
+        if imm_ok:
+            imm = yield self.dec2.e.imm_data.data
+            inputs.append(SelectableInt(imm, 64))
         assert len(outputs) >= 1
         output = outputs[0]
-        gts = [(x > output) == SelectableInt(1, 1) for x in inputs]
+        gts = [(x > output) for x in inputs]
         print(gts)
-        if all(gts):
-            return True
-        return False
+        cy = 1 if any(gts) else 0
+        self.spr['XER'][XER_bits['CA']] = cy
+
+
+        # 32 bit carry
+        gts = [(x[32:64] > output[32:64]) == SelectableInt(1, 1)
+               for x in inputs]
+        cy32 = 1 if any(gts) else 0
+        self.spr['XER'][XER_bits['CA32']] = cy32
+
+    def handle_overflow(self, inputs, outputs):
+        inv_a = yield self.dec2.e.invert_a
+        if inv_a:
+            inputs[0] = ~inputs[0]
+
+        imm_ok = yield self.dec2.e.imm_data.ok
+        if imm_ok:
+            imm = yield self.dec2.e.imm_data.data
+            inputs.append(SelectableInt(imm, 64))
+        assert len(outputs) >= 1
+        if len(inputs) >= 2:
+            output = outputs[0]
+            input_sgn = [exts(x.value, x.bits) < 0 for x in inputs]
+            output_sgn = exts(output.value, output.bits) < 0
+            ov = 1 if input_sgn[0] == input_sgn[1] and \
+                output_sgn != input_sgn[0] else 0
+
+            self.spr['XER'][XER_bits['OV']] = ov
+            so = self.spr['XER'][XER_bits['SO']]
+            so = so | ov
+            self.spr['XER'][XER_bits['SO']] = so
+
+
 
     def handle_comparison(self, outputs):
         out = outputs[0]
@@ -256,9 +293,13 @@ class ISACaller:
         zero = SelectableInt(out == 0, 1)
         positive = SelectableInt(out > 0, 1)
         negative = SelectableInt(out < 0, 1)
-        SO = SelectableInt(0, 1)
+        SO = self.spr['XER'][XER_bits['SO']]
         cr_field = selectconcat(negative, positive, zero, SO)
         self.crl[0].eq(cr_field)
+
+    def set_pc(self, pc_val):
+        self.namespace['NIA'] = SelectableInt(pc_val, 64)
+        self.pc.update(self.namespace)
         
 
     def call(self, name):
@@ -291,16 +332,22 @@ class ISACaller:
         results = info.func(self, *inputs)
         print(results)
 
-        carry_en = yield self.dec2.e.rc.data
+        carry_en = yield self.dec2.e.output_carry
         if carry_en:
-            cy = self.handle_carry(inputs, results)
-
+            yield from self.handle_carry_(inputs, results)
+        ov_en = yield self.dec2.e.oe
+        if ov_en:
+            yield from self.handle_overflow(inputs, results)
+        rc_en = yield self.dec2.e.rc.data
+        if rc_en:
             self.handle_comparison(results)
 
         # any modified return results?
         if info.write_regs:
             output_names = create_args(info.write_regs)
             for name, output in zip(output_names, results):
+                if isinstance(output, int):
+                    output = SelectableInt(output, 256)
                 if name in info.special_regs:
                     print('writing special %s' % name, output)
                     if name in special_sprs:
@@ -309,7 +356,7 @@ class ISACaller:
                         self.namespace[name].eq(output)
                 else:
                     regnum = yield getattr(self.decoder, name)
-                    print('writing reg %d' % regnum)
+                    print('writing reg %d %s' % (regnum, str(output)))
                     if output.bits > 64:
                         output = SelectableInt(output.value, 64)
                     self.gpr[regnum] = output