Add test for cmpeqb
[soc.git] / src / soc / decoder / selectable_int.py
index c4698c014c3df71382f2c539c940f6fcc47c11d7..e09d85957e345f2fddd74b9460ba4598e53fd76a 100644 (file)
@@ -7,6 +7,8 @@ from operator import (add, sub, mul, truediv, mod, or_, and_, xor, neg, inv)
 def check_extsign(a, b):
     if isinstance(b, FieldSelectableInt):
         b = b.get_range()
+    if isinstance(b, int):
+        return SelectableInt(b, a.bits)
     if b.bits != 256:
         return b
     return SelectableInt(b.value, a.bits)
@@ -44,12 +46,26 @@ class FieldSelectableInt:
 
     def __getitem__(self, key):
         print ("getitem", key, self.br)
-        key = self.br[key] # don't do POWER 1.3.4 bit-inversion
-        return self.si[key]
+        if isinstance(key, SelectableInt):
+            key = key.value
+        if isinstance(key, int):
+            key = self.br[key] # don't do POWER 1.3.4 bit-inversion
+            return self.si[key]
+        if isinstance(key, slice):
+            key = self.br[key]
+            return selectconcat(*[self.si[x] for x in key])
 
     def __setitem__(self, key, value):
+        if isinstance(key, SelectableInt):
+            key = key.value
         key = self.br[key] # don't do POWER 1.3.4 bit-inversion
-        return self.si.__setitem__(key, value)
+        if isinstance(key, int):
+            return self.si.__setitem__(key, value)
+        else:
+            if not isinstance(value, SelectableInt):
+                value = SelectableInt(value, bits=len(key))
+            for i, k in enumerate(key):
+                self.si[k] = value[i]
 
     def __negate__(self):
         return self._op1(negate)
@@ -104,9 +120,37 @@ class FieldSelectableIntTestCase(unittest.TestCase):
         print (c)
         #self.assertEqual(c.value, a.value + b.value)
 
+    def test_select(self):
+        a = SelectableInt(0b00001111, 8)
+        br = BitRange()
+        br[0] = 0
+        br[1] = 1
+        br[2] = 4
+        br[3] = 5
+        fs = FieldSelectableInt(a, br)
+
+        self.assertEqual(fs.get_range(), 0b0011)
+
+    def test_select_range(self):
+        a = SelectableInt(0b00001111, 8)
+        br = BitRange()
+        br[0] = 0
+        br[1] = 1
+        br[2] = 4
+        br[3] = 5
+        fs = FieldSelectableInt(a, br)
+
+        self.assertEqual(fs[2:4], 0b11)
+
+        fs[0:2] = 0b10
+        self.assertEqual(fs.get_range(), 0b1011)
+        
+
 
 class SelectableInt:
     def __init__(self, value, bits):
+        if isinstance(value, SelectableInt):
+            value = value.value
         mask = (1 << bits) - 1
         self.value = value & mask
         self.bits = bits
@@ -129,6 +173,20 @@ class SelectableInt:
         assert b.bits == self.bits
         return SelectableInt(self.value - b.value, self.bits)
 
+    def __rsub__(self, b):
+        if isinstance(b, int):
+            b = SelectableInt(b, self.bits)
+        b = check_extsign(self, b)
+        assert b.bits == self.bits
+        return SelectableInt(b.value - self.value, self.bits)
+
+    def __radd__(self, b):
+        if isinstance(b, int):
+            b = SelectableInt(b, self.bits)
+        b = check_extsign(self, b)
+        assert b.bits == self.bits
+        return SelectableInt(b.value + self.value, self.bits)
+
     def __mul__(self, b):
         b = check_extsign(self, b)
         assert b.bits == self.bits
@@ -160,12 +218,25 @@ class SelectableInt:
         assert b.bits == self.bits
         return SelectableInt(self.value ^ b.value, self.bits)
 
+    def __rxor__(self, b):
+        b = check_extsign(self, b)
+        assert b.bits == self.bits
+        return SelectableInt(self.value ^ b.value, self.bits)
+
     def __invert__(self):
         return SelectableInt(~self.value, self.bits)
 
     def __neg__(self):
         return SelectableInt(~self.value + 1, self.bits)
 
+    def __lshift__(self, b):
+        b = check_extsign(self, b)
+        return SelectableInt(self.value << b.value, self.bits)
+
+    def __rshift__(self, b):
+        b = check_extsign(self, b)
+        return SelectableInt(self.value >> b.value, self.bits)
+
     def __getitem__(self, key):
         if isinstance(key, int):
             assert key < self.bits, "key %d accessing %d" % (key, self.bits)
@@ -229,7 +300,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return other >= self.value
+            return onebit(self.value >= other.value)
         assert False
 
     def __le__(self, other):
@@ -240,7 +311,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other <= self.value)
+            return onebit(self.value <= other)
         assert False
 
     def __gt__(self, other):
@@ -251,7 +322,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other > self.value)
+            return onebit(self.value > other)
         assert False
 
     def __lt__(self, other):
@@ -262,7 +333,7 @@ class SelectableInt:
             assert other.bits == self.bits
             other = other.value
         if isinstance(other, int):
-            return onebit(other < self.value)
+            return onebit(self.value < other)
         assert False
 
     def __eq__(self, other):
@@ -288,6 +359,9 @@ class SelectableInt:
         return "SelectableInt(value=0x{:x}, bits={})".format(self.value,
                                                            self.bits)
 
+    def __len__(self):
+        return self.bits
+
 def onebit(bit):
     return SelectableInt(1 if bit else 0, 1)
 
@@ -333,6 +407,8 @@ def selectconcat(*args, repeat=1):
         args = tmp
     res = copy(args[0])
     for i in args[1:]:
+        if isinstance(i, FieldSelectableInt):
+            i = i.si
         assert isinstance(i, SelectableInt), "can only concat SIs, sorry"
         res.bits += i.bits
         res.value = (res.value << i.bits) | i.value
@@ -402,5 +478,21 @@ class SelectableIntTestCase(unittest.TestCase):
             b = eval(repr(a))
             self.assertEqual(a, b)
 
+    def test_cmp(self):
+        a = SelectableInt(10, bits=8)
+        b = SelectableInt(5, bits=8)
+        self.assertTrue(a > b)
+        self.assertFalse(a < b)
+        self.assertTrue(a != b)
+        self.assertFalse(a == b)
+
+    def test_unsigned(self):
+        a = SelectableInt(0x80, bits=8)
+        b = SelectableInt(0x7f, bits=8)
+        self.assertTrue(a > b)
+        self.assertFalse(a < b)
+        self.assertTrue(a != b)
+        self.assertFalse(a == b)
+
 if __name__ == "__main__":
     unittest.main()