migen.fhdl.size: verify fslice for negative values
authorRobert Jordens <jordens@gmail.com>
Tue, 3 Dec 2013 02:32:13 +0000 (19:32 -0700)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Tue, 3 Dec 2013 20:39:37 +0000 (21:39 +0100)
migen/fhdl/size.py
migen/test/test_size.py

index e771cccad99722bb57936065795946fe823afc57..345c9c54d518d24c97abd91fa520e467c32a0a85 100644 (file)
@@ -161,16 +161,20 @@ def fslice(v, s):
 
        Examples
        --------
-       >>> fslice(Signal(2), 1) #doctest: +ELLIPSIS
+       >>> fslice(f.Signal(2), 1) #doctest: +ELLIPSIS
        <migen.fhdl.structure._Slice object at 0x...>
        >>> bin(fslice(0b1101, slice(1, None, 2)))
        '0b10'
+       >>> fslice(-1, slice(0, 4))
+       1
+       >>> fslice(-7, slice(None))
+       9
        """
        if isinstance(v, (bool, int)):
                if isinstance(s, int):
                        s = slice(s)
-               idx = range(*s.indices(flen(v)))
-               return sum(((v>>i) & 1) << j for j, i in enumerate(idx))
+               idx = range(*s.indices(bits_for(v)))
+               return sum(((v >> i) & 1) << j for j, i in enumerate(idx))
        elif isinstance(v, f.Value):
                return v[s]
        else:
@@ -190,8 +194,8 @@ def freversed(v):
 
        Examples
        --------
-       >>> freversed(Signal(2)) #doctest: +ELLIPSIS
-       <migen.fhdl.structure._Slice object at 0x...>
+       >>> freversed(f.Signal(2)) #doctest: +ELLIPSIS
+       <migen.fhdl.structure.Cat object at 0x...>
        >>> bin(freversed(0b1011))
        '0b1101'
        """
index 07b305247a9d9204f346227c4e2aa5cce0d73938..a55ad4308a8e7e062d4de312fab616689eee411c 100644 (file)
@@ -32,6 +32,9 @@ class SignalSizeCase(unittest.TestCase):
                fslice(self.s, sl)
                self.assertEqual(fslice(self.i, sl), 15)
                self.assertEqual(fslice(self.j, sl), 8)
+               self.assertEqual(fslice(-1, 9), 1)
+               self.assertEqual(fslice(-1, slice(0, 4)), 0b1)
+               self.assertEqual(fslice(-7, slice(0, None, 1)), 0b1001)
 
        def test_fslice_type(self):
                self.assertRaises(TypeError, fslice, [], 3)