1
0
mirror of https://github.com/myhdl/myhdl.git synced 2024-12-14 07:44:38 +08:00

set slice with left index open implementation and test

This commit is contained in:
jand 2003-07-02 08:01:14 +00:00
parent de0c9aa0df
commit 78dc0cb7b4
2 changed files with 33 additions and 4 deletions

View File

@ -121,10 +121,10 @@ class intbv(object):
def __setslice__(self, i, j, val):
if j == maxint: # default
j = 0
## if i == 0: # default
## q = self._val % (2**j)
## self._val = val * 2**j + q
## return
if i == 0: # default
q = self._val % (2**j)
self._val = val * 2**j + q
return
if i <= j or i < 1 or j < 0:
raise ValueError, "intbv[i:j] = v: requires i > j >= 0" \
" i, j ,v == %s, %s %s" % (i, j, val)

View File

@ -123,6 +123,14 @@ def setSlice(s, i, j, val):
si = len(exts)-i
sj = len(exts)-j
return exts[:si] + val[si-sj:] + exts[sj:]
def setSliceLeftOpen(s, j, val):
ext = '0' * (j-len(s)+1)
exts = ext + s
if j:
return val + exts[-j:]
else:
return val
class TestIntBvIndexing(TestCase):
@ -250,6 +258,27 @@ class TestIntBvIndexing(TestCase):
refi = ~long(setSlice(s, i, j, extv), 2)
self.assertEqual(bvi, refi)
def testSetSliceLeftOpen(self):
self.seqsSetup()
toggle = 0
for s in self.seqs:
n = long(s, 2)
for j in range(0, len(s)+5):
for v in self.seqv:
bv = intbv(n)
bvi = intbv(~n)
val = long(v, 2)
toggle ^= 1
if toggle:
val = intbv(val)
bv[:j] = val
bvi[:j] = -1-val
ref = long(setSliceLeftOpen(s, j, v), 2)
self.assertEqual(bv, ref)
refi = ~long(setSliceLeftOpen(s, j, v), 2)
self.assertEqual(bvi, refi)
class TestIntBvAsInt(TestCase):