Make FieldSelectableInt accept slices for set and get
authorMichael Nolan <mtnolan2640@gmail.com>
Thu, 7 May 2020 17:44:33 +0000 (13:44 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Thu, 7 May 2020 17:44:33 +0000 (13:44 -0400)
src/soc/decoder/power_fields.py
src/soc/decoder/selectable_int.py

index 8fa21f29329da122179b6181873604b05b3accff..3f53954915bcc0c482651bfe234a5bbeffe0e54a 100644 (file)
@@ -8,7 +8,7 @@ class BitRange(OrderedDict):
 
     def __getitem__(self, subscript):
         if isinstance(subscript, slice):
-            return list(self)[subscript]
+            return list(self.values())[subscript]
         else:
             return OrderedDict.__getitem__(self, subscript)
 
index b6414f90d9483d5a4ecdbe02b965aae3ca9ffb5b..6ee92535d70c43e649e2f9adbae4cb34067beae2 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):