Add test for prtyw pseudocode
[soc.git] / src / soc / decoder / selectable_int.py
index 33cbb74e7ba8e371597596bb95bf85847b43a12e..ce7c2ebbb9dc585e25036ba2a62e9fd46fe01fae 100644 (file)
@@ -48,14 +48,24 @@ class FieldSelectableInt:
         print ("getitem", key, self.br)
         if isinstance(key, SelectableInt):
             key = key.value
-        key = self.br[key] # don't do POWER 1.3.4 bit-inversion
-        return self.si[key]
+        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)
@@ -110,6 +120,32 @@ 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):
@@ -135,6 +171,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
@@ -166,12 +216,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)
@@ -235,7 +298,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):
@@ -246,7 +309,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):
@@ -257,7 +320,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):
@@ -268,7 +331,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):
@@ -342,6 +405,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
@@ -411,5 +476,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()