[myhdl-list] [PATCH 1 of 2 RFC] intbv: add support for negative indexes and slice limits
Brought to you by:
jandecaluwe
From: Angel E. <ang...@gm...> - 2012-10-13 00:12:13
|
# HG changeset patch # User Angel Ezquerra <ang...@gm...> # Date 1350077846 -7200 # Branch 0.8-dev # Node ID c641c1ed333d6d03d9cee4a63df5111fe98574c8 # Parent 6f6571dbd495f197531f0406763ff10e4ce3b3ee intbv: add support for negative indexes and slice limits Negative indexes are supported on regular python lists. They are very useful when you want to refer to a position from the end of the list. This same concept can be applied to intbv indexes and slices. Supporting negative indexes reduces the need to use the len() function to calculate a position from the MSB of the intbv. Note that converting negative indexes is not supported yet. diff --git a/myhdl/_intbv.py b/myhdl/_intbv.py --- a/myhdl/_intbv.py +++ b/myhdl/_intbv.py @@ -127,11 +127,15 @@ j = 0 j = int(j) if j < 0: - raise ValueError, "intbv[i:j] requires j >= 0\n" \ - " j == %s" % j + j = self._nrbits + j + if j < 0: + raise ValueError, "intbv[i:j] requires j >= 0\n" \ + " j == %s" % j if i is None: # default return type(self)(self._val >> j) i = int(i) + if i < 0: + i = self._nrbits + i if i <= j: raise ValueError, "intbv[i:j] requires i > j\n" \ " i, j == %s, %s" % (i, j) @@ -139,6 +143,8 @@ return res else: i = int(key) + if i < 0: + i = self._nrbits + i res = bool((self._val >> i) & 0x1) return res @@ -153,14 +159,18 @@ j = 0 j = int(j) if j < 0: - raise ValueError, "intbv[i:j] = v requires j >= 0\n" \ - " j == %s" % j + j = self._nrbits + j + if j < 0: + raise ValueError, "intbv[i:j] = v requires j >= 0\n" \ + " j == %s" % j if i is None: # default q = self._val % (1L << j) self._val = val * (1L << j) + q self._handleBounds() return i = int(i) + if i < 0: + i = self._nrbits + i if i <= j: raise ValueError, "intbv[i:j] = v requires i > j\n" \ " i, j, v == %s, %s, %s" % (i, j, val) @@ -174,6 +184,8 @@ self._handleBounds() else: i = int(key) + if i < 0: + i = self._nrbits + i if val == 1: self._val |= (1L << i) elif val == 0: |