From: Michael Nolan Date: Thu, 7 May 2020 17:44:33 +0000 (-0400) Subject: Make FieldSelectableInt accept slices for set and get X-Git-Tag: div_pipeline~1358 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=566ac0b4e38ea09a48e984953ec8292a8e990785;p=soc.git Make FieldSelectableInt accept slices for set and get --- diff --git a/src/soc/decoder/power_fields.py b/src/soc/decoder/power_fields.py index 8fa21f29..3f539549 100644 --- a/src/soc/decoder/power_fields.py +++ b/src/soc/decoder/power_fields.py @@ -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) diff --git a/src/soc/decoder/selectable_int.py b/src/soc/decoder/selectable_int.py index b6414f90..6ee92535 100644 --- a/src/soc/decoder/selectable_int.py +++ b/src/soc/decoder/selectable_int.py @@ -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):