allow int in addition and subtraction
[soc.git] / src / soc / decoder / selectable_int.py
index a73ceb5dd5885df78c473e53a9b6dbc7f1bc4989..1dd415117f5ffd1f0ed14e8ae32c4b05d13253d3 100644 (file)
@@ -1,4 +1,5 @@
 import unittest
+from copy import copy
 
 
 class SelectableInt:
@@ -8,10 +9,14 @@ class SelectableInt:
         self.bits = bits
 
     def __add__(self, b):
+        if isinstance(b, int):
+            b = SelectableInt(b, self.bits)
         assert b.bits == self.bits
         return SelectableInt(self.value + b.value, self.bits)
 
     def __sub__(self, b):
+        if isinstance(b, int):
+            b = SelectableInt(b, self.bits)
         assert b.bits == self.bits
         return SelectableInt(self.value - b.value, self.bits)
 
@@ -19,6 +24,14 @@ class SelectableInt:
         assert b.bits == self.bits
         return SelectableInt(self.value * b.value, self.bits)
 
+    def __div__(self, b):
+        assert b.bits == self.bits
+        return SelectableInt(self.value / b.value, self.bits)
+
+    def __mod__(self, b):
+        assert b.bits == self.bits
+        return SelectableInt(self.value % b.value, self.bits)
+
     def __or__(self, b):
         assert b.bits == self.bits
         return SelectableInt(self.value | b.value, self.bits)
@@ -54,7 +67,7 @@ class SelectableInt:
             stop = self.bits - key.start
             start = self.bits - key.stop
 
-            bits = stop - start
+            bits = stop - start + 1
             mask = (1 << bits) - 1
             value = (self.value >> start) & mask
             return SelectableInt(value, bits)
@@ -80,25 +93,112 @@ class SelectableInt:
             stop = self.bits - key.start
             start = self.bits - key.stop
 
-            bits = stop - start
+            bits = stop - start + 1
             if isinstance(value, SelectableInt):
-                assert value.bits == bits
+                assert value.bits == bits, "%d into %d" % (value.bits, bits)
                 value = value.value
             mask = ((1 << bits) - 1) << start
             value = value << start
             self.value = (self.value & ~mask) | (value & mask)
 
+    def __ge__(self, other):
+        if isinstance(other, SelectableInt):
+            assert other.bits == self.bits
+            other = other.value
+        if isinstance(other, int):
+            return other >= self.value
+        assert False
+
+    def __le__(self, other):
+        if isinstance(other, SelectableInt):
+            assert other.bits == self.bits
+            other = other.value
+        if isinstance(other, int):
+            return onebit(other <= self.value)
+        assert False
+
+    def __gt__(self, other):
+        if isinstance(other, SelectableInt):
+            assert other.bits == self.bits
+            other = other.value
+        if isinstance(other, int):
+            return onebit(other > self.value)
+        assert False
+
+    def __lt__(self, other):
+        if isinstance(other, SelectableInt):
+            assert other.bits == self.bits
+            other = other.value
+        if isinstance(other, int):
+            return onebit(other < self.value)
+        assert False
+
     def __eq__(self, other):
         if isinstance(other, SelectableInt):
-            return other.value == self.value and other.bits == self.bits
+            assert other.bits == self.bits
+            other = other.value
         if isinstance(other, int):
-            return other == self.value
+            return onebit(other == self.value)
         assert False
 
+    def __bool__(self):
+        return self.value != 0
+
     def __repr__(self):
         return "SelectableInt(value={:x}, bits={})".format(self.value,
                                                            self.bits)
 
+def onebit(bit):
+    return SelectableInt(1 if bit else 0, 1)
+
+def selectltu(lhs, rhs):
+    """ less-than (unsigned)
+    """
+    if isinstance(rhs, SelectableInt):
+        rhs = rhs.value
+    return onebit(lhs.value < rhs)
+
+def selectgtu(lhs, rhs):
+    """ greater-than (unsigned)
+    """
+    if isinstance(rhs, SelectableInt):
+        rhs = rhs.value
+    return onebit(lhs.value > rhs)
+
+
+# XXX this probably isn't needed...
+def selectassign(lhs, idx, rhs):
+    if isinstance(idx, tuple):
+        if len(idx) == 2:
+            lower, upper = idx
+            step = None
+        else:
+            lower, upper, step = idx
+        toidx = range(lower, upper, step)
+        fromidx = range(0, upper-lower, step) # XXX eurgh...
+    else:
+        toidx = [idx]
+        fromidx = [0]
+    for t, f in zip(toidx, fromidx):
+        lhs[t] = rhs[f]
+
+
+def selectconcat(*args, repeat=1):
+    if repeat != 1 and len(args) == 1 and isinstance(args[0], int):
+        args = [SelectableInt(args[0], 1)]
+    if repeat != 1: # multiplies the incoming arguments
+        tmp = []
+        for i in range(repeat):
+            tmp += args
+        args = tmp
+    res = copy(args[0])
+    for i in args[1:]:
+        assert isinstance(i, SelectableInt), "can only concat SIs, sorry"
+        res.bits += i.bits
+        res.value = (res.value << i.bits) | i.value
+    print ("concat", repeat, res)
+    return res
+
 
 class SelectableIntTestCase(unittest.TestCase):
     def test_arith(self):
@@ -127,7 +227,6 @@ class SelectableIntTestCase(unittest.TestCase):
         self.assertEqual(d.value, a.value | b.value)
         self.assertEqual(e.value, a.value ^ b.value)
         self.assertEqual(f.value, 0xF0)
-                          
 
     def test_get(self):
         a = SelectableInt(0xa2, 8)
@@ -145,8 +244,7 @@ class SelectableIntTestCase(unittest.TestCase):
         a[0:4] = 3
         self.assertEqual(a, 0x39)
         a[0:4] = a[4:8]
-        self.assertEqual(a, 0x99)
-
+        self.assertEqual(a, 0x199)
 
 if __name__ == "__main__":
     unittest.main()