add with carry cleanup and test case
authorTobias Platen <tplaten@posteo.de>
Mon, 20 Apr 2020 14:50:42 +0000 (16:50 +0200)
committerTobias Platen <tplaten@posteo.de>
Mon, 20 Apr 2020 14:50:42 +0000 (16:50 +0200)
src/soc/simulator/internalop_sim.py
src/soc/simulator/test_sim.py

index 726124a343e5c64c8b782569152d19762b46679f..b54b29b0854e1e04dbdc0234b77723f3e67308ec 100644 (file)
@@ -59,7 +59,7 @@ class RegFile:
         self.sprs = {}
 
     def write_reg(self, regnum, value):
-        all1s = (1<<64)-1 # 64 bits worth of 1s
+        all1s = (1 << 64)-1  # 64 bits worth of 1s
         value &= all1s
         print("Writing {:x} to reg r{}".format(value, regnum))
         self.regfile[regnum] = value
@@ -106,7 +106,7 @@ class InternalOpSimulator:
             assert False, "Not implemented"
 
     def alu_op(self, pdecode2):
-        all1s = (1<<64)-1 # 64 bits worth of 1s
+        all1s = (1 << 64)-1  # 64 bits worth of 1s
         internal_op = yield pdecode2.dec.op.internal_op
         operand1 = 0
         operand2 = 0
@@ -142,14 +142,11 @@ class InternalOpSimulator:
                                      carry=carry)
 
         cry_out = yield pdecode2.dec.op.cry_out
-        ## TODO   yield pdecode2.dec.op.rc
-        if(cry_out==1):
-            if(result > 0xFFFFFFFF):
-                self.carry_out = 1
-            else:
-                self.carry_out = 0
-            
-            
+        # TODO   yield pdecode2.dec.op.rc
+        if cry_out == 1:
+            self.carry_out = (result >> 64)
+            print("setting carry_out", self.carry_out)
+
         ro_ok = yield pdecode2.e.write_reg.ok
         if ro_ok:
             ro_sel = yield pdecode2.e.write_reg.data
index 068beacd8a1f6d25ce33b00efc661601b5d5152d..c170397e27fdde64cc62341e477cd4fe32c22101 100644 (file)
@@ -97,6 +97,17 @@ class DecoderTestCase(FHDLTestCase):
         with Program(lst) as program:
             self.run_tst_program(program, [1, 2, 3, 4, 5])
 
+    def test_add_with_carry(self):
+        lst = ["addi 1, 0, 5",
+               "neg 1, 1",
+               "addi 2, 0, 7",
+               "neg 2, 2",
+               "addc 3, 2, 1",
+               "addi 3, 3, 1"
+               ]
+        with Program(lst) as program:
+            self.run_tst_program(program, [1, 2, 3])
+
     def run_tst_program(self, prog, reglist):
         simulator = InternalOpSimulator()
         self.run_tst(prog, simulator)