ISACaller, in svp64 mode, read the next 32 bits when SVP64 identified
[soc.git] / src / soc / decoder / selectable_int.py
index 4e091ea3d05f354d17571b1f3afa208423587af1..152764ca08758f3922e991da1b400cee6e47c853 100644 (file)
@@ -114,6 +114,15 @@ class FieldSelectableInt:
     def __repr__(self):
         return "FieldSelectableInt(si=%s, br=%s)" % (self.si, self.br)
 
+    def asint(self, msb0=False):
+        res = 0
+        brlen = len(self.br)
+        for i, key in self.br.items():
+            bit = self.si[key].value
+            #print("asint", i, key, bit)
+            res |= bit << ((brlen-i-1) if msb0 else i)
+        return res
+
 
 class FieldSelectableIntTestCase(unittest.TestCase):
     def test_arith(self):
@@ -153,7 +162,6 @@ class FieldSelectableIntTestCase(unittest.TestCase):
         fs[0:2] = 0b10
         self.assertEqual(fs.get_range(), 0b1011)
 
-
 class SelectableInt:
     """SelectableInt - a class that behaves exactly like python int
 
@@ -172,11 +180,22 @@ class SelectableInt:
         mask = (1 << bits) - 1
         self.value = value & mask
         self.bits = bits
+        self.overflow = (value & ~mask) != 0
 
     def eq(self, b):
         self.value = b.value
         self.bits = b.bits
 
+    def to_signed_int(self):
+        print ("to signed?", self.value & (1<<(self.bits-1)), self.value)
+        if self.value & (1<<(self.bits-1)) != 0: # negative
+            res = self.value - (1<<self.bits)
+            print ("    val -ve:", self.bits, res)
+        else:
+            res = self.value
+            print ("    val +ve:", res)
+        return res
+
     def _op(self, op, b):
         if isinstance(b, int):
             b = SelectableInt(b, self.bits)
@@ -245,7 +264,9 @@ class SelectableInt:
         return SelectableInt(~self.value, self.bits)
 
     def __neg__(self):
-        return SelectableInt(~self.value + 1, self.bits)
+        res = SelectableInt((~self.value) + 1, self.bits)
+        print ("neg", hex(self.value), hex(res.value))
+        return res
 
     def __lshift__(self, b):
         b = check_extsign(self, b)
@@ -266,6 +287,7 @@ class SelectableInt:
             key = self.bits - (key + 1)
 
             value = (self.value >> key) & 1
+            print("getitem", key, self.bits, hex(self.value), value)
             return SelectableInt(value, 1)
         elif isinstance(key, slice):
             assert key.step is None or key.step == 1
@@ -277,14 +299,16 @@ class SelectableInt:
             start = self.bits - key.stop
 
             bits = stop - start
-            #print ("__getitem__ slice num bits", bits)
+            #print ("__getitem__ slice num bits", start, stop, bits)
             mask = (1 << bits) - 1
             value = (self.value >> start) & mask
+            print("getitem", stop, start, self.bits, hex(self.value), value)
             return SelectableInt(value, bits)
 
     def __setitem__(self, key, value):
         if isinstance(key, SelectableInt):
             key = key.value
+        print("setitem", key, self.bits, hex(self.value))
         if isinstance(key, int):
             assert key < self.bits
             assert key >= 0
@@ -320,9 +344,9 @@ class SelectableInt:
         if isinstance(other, SelectableInt):
             other = check_extsign(self, other)
             assert other.bits == self.bits
-            other = other.value
+            other = other.to_signed_int()
         if isinstance(other, int):
-            return onebit(self.value >= other.value)
+            return onebit(self.to_signed_int() >= other)
         assert False
 
     def __le__(self, other):
@@ -331,9 +355,9 @@ class SelectableInt:
         if isinstance(other, SelectableInt):
             other = check_extsign(self, other)
             assert other.bits == self.bits
-            other = other.value
+            other = other.to_signed_int()
         if isinstance(other, int):
-            return onebit(self.value <= other)
+            return onebit(self.to_signed_int() <= other)
         assert False
 
     def __gt__(self, other):
@@ -342,20 +366,24 @@ class SelectableInt:
         if isinstance(other, SelectableInt):
             other = check_extsign(self, other)
             assert other.bits == self.bits
-            other = other.value
+            other = other.to_signed_int()
         if isinstance(other, int):
-            return onebit(self.value > other)
+            return onebit(self.to_signed_int() > other)
         assert False
 
     def __lt__(self, other):
+        print ("SelectableInt lt", self, other)
         if isinstance(other, FieldSelectableInt):
             other = other.get_range()
         if isinstance(other, SelectableInt):
             other = check_extsign(self, other)
             assert other.bits == self.bits
-            other = other.value
+            other = other.to_signed_int()
         if isinstance(other, int):
-            return onebit(self.value < other)
+            a = self.to_signed_int()
+            res = onebit(a  < other)
+            print ("    a < b", a, other, res)
+            return res
         assert False
 
     def __eq__(self, other):
@@ -366,6 +394,7 @@ class SelectableInt:
             other = check_extsign(self, other)
             assert other.bits == self.bits
             other = other.value
+        print ("    eq", other, self.value, other == self.value)
         if isinstance(other, int):
             return onebit(other == self.value)
         assert False