def testSetSlice(self): self.seqsSetup() toggle = 0 for s in self.seqs: n = long(s, 2) for i in range(1, len(s) + 5): for j in range(0, len(s) + 5): for v in self.seqv: ext = "0" * (i - j - len(v)) extv = ext + v bv = intbv(n) bvi = intbv(~n) val = long(v, 2) toggle ^= 1 if toggle: val = intbv(val) try: bv[i:j] = val except ValueError: assert i <= j or val >= 2 ** (i - j) continue else: ref = long(setSlice(s, i, j, extv), 2) assert bv == ref mask = (2 ** (i - j)) - 1 vali = val ^ mask try: bvi[i:j] = vali except ValueError: assert vali >= 2 ** (i - j) continue else: refi = ~long(setSlice(s, i, j, extv), 2) assert bvi == refi
def __init__(self, *args): assert len(args) >= 2 self._args = args self._sigargs = sigargs = [] nrbits = 0 val = 0 for a in args: if isinstance(a, intbv): w = a._nrbits v = a._val elif isinstance(a, _Signal): sigargs.append(a) w = a._nrbits if isinstance(a._val, intbv): v = a._val._val else: v = a._val elif isinstance(a, bool): w = 1 v = a elif isinstance(a, str): w = len(a) v = long(a, 2) else: raise TypeError("ConcatSignal: inappropriate argument type: %s" \ % type(a)) nrbits += w val = val << w | v & (long(1) << w) - 1 self._initval = val ini = intbv(val)[nrbits:] _ShadowSignal.__init__(self, ini) gen = self.genfunc() self._waiter = _SignalTupleWaiter(gen)
def testSetSlice(self): self.seqsSetup() toggle = 0 for s in self.seqs: n = long(s, 2) for i in range(1, len(s) + 5): for j in range(0, len(s) + 5): for v in self.seqv: ext = '0' * (i - j - len(v)) extv = ext + v bv = intbv(n) bvi = intbv(~n) val = long(v, 2) toggle ^= 1 if toggle: val = intbv(val) try: bv[i:j] = val except ValueError: self.assertTrue(i <= j or val >= 2**(i - j)) continue else: ref = long(setSlice(s, i, j, extv), 2) self.assertEqual(bv, ref) mask = (2**(i - j)) - 1 vali = val ^ mask try: bvi[i:j] = vali except ValueError: self.assertTrue(vali >= 2**(i - j)) continue else: refi = ~long(setSlice(s, i, j, extv), 2) self.assertEqual(bvi, refi)
def __init__(self, *args): assert len(args) >= 2 self._args = args self._sigargs = sigargs = [] nrbits = 0 val = 0 for a in args: if isinstance(a, intbv): w = a._nrbits v = a._val elif isinstance(a, _Signal): sigargs.append(a) w = a._nrbits if isinstance(a._val, intbv): v = a._val._val else: v = a._val elif isinstance(a, bool): w = 1 v = a elif isinstance(a, str): w = len(a) v = long(a, 2) else: raise TypeError("ConcatSignal: inappropriate argument type: %s" \ % type(a)) nrbits += w val = val << w | v & (long(1) << w)-1 self._initval = val ini = intbv(val)[nrbits:] _ShadowSignal.__init__(self, ini) gen = self.genfunc() self._waiter = _SignalTupleWaiter(gen)
def concat(base, *args): if isinstance(base, intbv): basewidth = base._nrbits val = base._val elif isinstance(base, integer_types): if isinstance(base, bool): basewidth = 1 else: basewidth = 0 val = base elif isinstance(base, _Signal): basewidth = base._nrbits if isinstance(base._val, intbv): val = base._val._val else: val = base._val elif isinstance(base, str): basewidth = len(base) val = long(base, 2) else: raise TypeError("concat: inappropriate first argument type: %s" % type(base)) width = 0 for i, arg in enumerate(args): if isinstance(arg, intbv): w = arg._nrbits v = arg._val elif isinstance(arg, _Signal): w = arg._nrbits if isinstance(arg._val, intbv): v = arg._val._val else: v = arg._val elif isinstance(arg, bool): w = 1 v = arg elif isinstance(arg, str): w = len(arg) v = long(arg, 2) else: raise TypeError("concat: inappropriate argument type: %s" % type(arg)) if not w: raise TypeError("concat: arg on pos %d should have length" % (i + 1)) width += w val = val << w | v & (long(1) << w) - 1 if basewidth: return intbv(val, _nrbits=basewidth + width) else: return intbv(val)
def concat(base, *args): if isinstance(base, intbv): basewidth = base._nrbits val = base._val elif isinstance(base, integer_types): if isinstance(base, bool): basewidth = 1 else: basewidth = 0 val = base elif isinstance(base, _Signal): basewidth = base._nrbits if isinstance(base._val, intbv): val = base._val._val else: val = base._val elif isinstance(base, str): basewidth = len(base) val = long(base, 2) else: raise TypeError("concat: inappropriate first argument type: %s" \ % type(base)) width = 0 for i, arg in enumerate(args): if isinstance(arg, intbv): w = arg._nrbits v = arg._val elif isinstance(arg, _Signal): w = arg._nrbits if isinstance(arg._val, intbv): v = arg._val._val else: v = arg._val elif isinstance(arg, bool): w = 1 v = arg elif isinstance(arg, str): w = len(arg) v = long(arg, 2) else: raise TypeError("concat: inappropriate argument type: %s" \ % type(arg)) if not w: raise TypeError("concat: arg on pos %d should have length" % (i+1)) width += w val = val << w | v & (long(1) << w)-1 if basewidth: return intbv(val, _nrbits=basewidth + width) else: return intbv(val)
def testConcatMixToUnsizedBase(self): bases = [] for base in self.bases: seq = (long(base, 2), Signal(long(base, 2))) bases.append(random.choice(seq)) extslist = [] for exts in self.extslist: newexts = [] for ext in exts: seq = (ext, intbv(ext), Signal(intbv(ext))) newexts.append(random.choice(seq)) extslist.append(newexts) self.ConcatToUnsizedBase(bases, extslist)
def testGetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for i in range(len(s) + 20): ref = long(getItem(s, i), 2) res = bv[i] resi = bvi[i] assert res == ref assert type(res) == bool assert resi == ref ^ 1 assert type(resi) == bool
def testGetSliceLeftOpen(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for j in range(0, len(s) + 20): res = bv[:j] resi = bvi[:j] ref = long(getSliceLeftOpen(s, j), 2) assert res == ref assert type(res) == intbv assert resi + ref == -1 assert type(res) == intbv
def testGetSliceLeftOpen(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for j in range(0,len(s)+20): res = bv[:j] resi = bvi[:j] ref = long(getSliceLeftOpen(s, j), 2) self.assertEqual(res, ref) self.assertEqual(type(res), intbv) self.assertEqual(resi+ref, -1) self.assertEqual(type(res), intbv)
def testGetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) sbv = Signal(intbv(n)) sbvi = Signal(intbv(~n)) for i in range(len(s)+20): ref = long(getItem(s, i), 2) res = sbv[i] resi = sbvi[i] self.assertEqual(res, ref) self.assertEqual(type(res), bool) self.assertEqual(resi, ref^1) self.assertEqual(type(resi), bool)
def testGetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for i in range(len(s) + 20): ref = long(getItem(s, i), 2) res = bv[i] resi = bvi[i] self.assertEqual(res, ref) self.assertEqual(type(res), bool) self.assertEqual(resi, ref ^ 1) self.assertEqual(type(resi), bool)
def testGetSliceLeftOpen(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for j in range(0, len(s) + 20): res = bv[:j] resi = bvi[:j] ref = long(getSliceLeftOpen(s, j), 2) self.assertEqual(res, ref) self.assertEqual(type(res), intbv) self.assertEqual(resi + ref, -1) self.assertEqual(type(res), intbv)
def testGetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) sbv = Signal(intbv(n)) sbvi = Signal(intbv(~n)) for i in range(len(s) + 20): ref = long(getItem(s, i), 2) res = sbv[i] resi = sbvi[i] assert res == ref assert type(res) == bool assert resi == ref ^ 1 assert type(resi) == bool
def binaryCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bi = intbv(long(i)) bj = intbv(j) ref = op(long(i), j) r1 = op(bi, j) r2 = op(long(i), bj) r3 = op(bi, bj) # self.assertEqual(type(r1), intbv) # self.assertEqual(type(r2), intbv) # self.assertEqual(type(r3), intbv) assert r1 == ref assert r2 == ref assert r3 == ref
def binaryCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bi = Signal(long(i)) bj = Signal(long(j)) ref = op(long(i), j) r1 = op(bi, j) r2 = op(long(i), bj) r3 = op(bi, bj) self.assertEqual(type(r1), type(ref)) self.assertEqual(type(r2), type(ref)) self.assertEqual(type(r3), type(ref)) self.assertEqual(r1, ref) self.assertEqual(r2, ref) self.assertEqual(r3, ref)
def binaryCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bi = Signal(long(i)) bj = Signal(long(j)) ref = op(long(i), j) r1 = op(bi, j) r2 = op(long(i), bj) r3 = op(bi, bj) assert type(r1) == type(ref) assert type(r2) == type(ref) assert type(r3) == type(ref) assert r1 == ref assert r2 == ref assert r3 == ref
def binaryCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bi = intbv(long(i)) bj = intbv(j) ref = op(long(i), j) r1 = op(bi, j) r2 = op(long(i), bj) r3 = op(bi, bj) #self.assertEqual(type(r1), intbv) #self.assertEqual(type(r2), intbv) #self.assertEqual(type(r3), intbv) self.assertEqual(r1, ref) self.assertEqual(r2, ref) self.assertEqual(r3, ref)
def bin(num, width=0, underscores=False): """Return a binary string representation. num -- number to convert Optional parameter: width -- specifies the desired string (sign bit padding) """ num = long(num) t = _int2bitstring(num) if width: pad = '0' if num < 0: pad = '1' t = (width - len(t)) * pad + t if underscores: rem = len(t) % 4 s = '' if rem != 0: s = ''.join((t[:rem], '_' if len(t) > 4 else '')) for i in range(rem, len(t), 4): s = ''.join((s, t[i:i + 4])) if i != len(t) - 4: s = ''.join((s, '_')) return s else: return t
def __init__(self, val=0, min=None, max=None, _nrbits=0): if _nrbits: self._min = 0 self._max = 2**_nrbits else: self._min = min self._max = max if max is not None and min is not None: if min >= 0: _nrbits = len(bin(max - 1)) elif max <= 1: _nrbits = len(bin(min)) else: # make sure there is a leading zero bit in positive numbers _nrbits = builtins.max(len(bin(max - 1)) + 1, len(bin(min))) if isinstance(val, integer_types): self._val = val elif isinstance(val, string_types): mval = val.replace('_', '') self._val = long(mval, 2) _nrbits = len(mval) elif isinstance(val, intbv): self._val = val._val self._min = val._min self._max = val._max _nrbits = val._nrbits else: raise TypeError("intbv constructor arg should be int or string") self._nrbits = _nrbits self._handleBounds()
def __init__(self, val=0, min=None, max=None, _nrbits=0): if _nrbits: self._min = 0 self._max = 2**_nrbits else: self._min = min self._max = max if max is not None and min is not None: if min >= 0: _nrbits = len(bin(max-1)) elif max <= 1: _nrbits = len(bin(min)) else: # make sure there is a leading zero bit in positive numbers _nrbits = builtins.max(len(bin(max-1))+1, len(bin(min))) if isinstance(val, integer_types): self._val = val elif isinstance(val, string_types): mval = val.replace('_', '') self._val = long(mval, 2) _nrbits = len(mval) elif isinstance(val, intbv): self._val = val._val self._min = val._min self._max = val._max _nrbits = val._nrbits else: raise TypeError("intbv constructor arg should be int or string") self._nrbits = _nrbits self._handleBounds()
def __ilshift__(self, other): self._val = long(self._val) if isinstance(other, intbv): self._val <<= other._val else: self._val <<= other self._handleBounds() return self
def _setNextIntbv(self, val): if isinstance(val, integer_types): lval = val elif isinstance(val, intbv): lval = val._val elif isinstance(val, string_types): mval = val.replace('_', '') lval = long(mval, 2) elif isinstance(val, tuple): # tuple assignment in stead of 'concat' # print('Signal: Tuple Assignment') lval = 0 for v in val: if isinstance(v, integer_types): lval <<= 1 lval += 1 if v else 0 elif isinstance(v, intbv): lval <<= len(v) lval += v elif isinstance(v, string_types): mval = v.replace('_', '') lval <<= len(mval) lval += long(mval, 2) elif isinstance(v, _Signal): if isinstance(v._val, intbv): lval <<= len(v) lval += v else: # bool lval <<= 1 lval += 1 if v else 0 # elif not isinstance(val, integer_types): elif isinstance(val, EnumItemType): lval = int(val) else: raise TypeError("Expected int or intbv, got %s" % type(val)) self._next._val = lval # self._next._handleBounds(self) self._next._handleBounds()
def augmentedAssignCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bj = Signal(j) ref = long(i) ref = op(ref, j) r1 = bi1 = Signal(i) with pytest.raises(TypeError): r1 = op(r1, j) r2 = long(i) r2 = op(r2, bj) r3 = bi3 = Signal(i) with pytest.raises(TypeError): r3 = op(r3, bj) assert r2 == ref
def ConcatToUnsizedBase(self, bases, extslist): for base, basestr in zip(bases, self.bases): for exts, extstr in zip(extslist, self.extslist): bv = concat(base, *exts) refstr = basestr + reduce(operator.add, extstr) ref = long(refstr, 2) assert bv == ref assert len(bv) == 0
def ConcatToUnsizedBase(self, bases, extslist): for base, basestr in zip(bases, self.bases): for exts, extstr in zip(extslist, self.extslist): bv = concat(base, *exts) refstr = basestr + reduce(operator.add, extstr) ref = long(refstr, 2) self.assertEqual(bv, ref) self.assertEqual(len(bv), 0)
def augmentedAssignCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bj = intbv(j) ref = long(i) ref = op(ref, j) r1 = bi1 = intbv(long(i)) r1 = op(r1, j) r2 = long(i) r2 = op(r2, bj) r3 = bi3 = intbv(long(i)) r3 = op(r3, bj) self.assertEqual(type(r1), intbv) self.assertEqual(type(r3), intbv) self.assertEqual(r1, ref) self.assertEqual(r2, ref) self.assertEqual(r3, ref) self.assertTrue(r1 is bi1) self.assertTrue(r3 is bi3)
def augmentedAssignCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bj = intbv(j) ref = long(i) ref = op(ref, j) r1 = bi1 = intbv(long(i)) r1 = op(r1, j) r2 = long(i) r2 = op(r2, bj) r3 = bi3 = intbv(long(i)) r3 = op(r3, bj) assert type(r1) == intbv assert type(r3) == intbv assert r1 == ref assert r2 == ref assert r3 == ref assert r1 is bi1 assert r3 is bi3
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)
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) assert bv == ref refi = ~long(setSliceLeftOpen(s, j, v), 2) assert bvi == refi
def testGetSlice(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) sbv = Signal(intbv(n)) sbvi = Signal(intbv(~n)) for i in range(1, len(s) + 20): for j in range(0, len(s) + 20): try: res = sbv[i:j] resi = sbvi[i:j] except ValueError: assert i <= j continue ref = long(getSlice(s, i, j), 2) assert res == ref assert type(res) == intbv mask = (2**(i - j)) - 1 assert resi == ref ^ mask assert type(resi) == intbv
def testGetSlice(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) sbv = Signal(intbv(n)) sbvi = Signal(intbv(~n)) for i in range(1, len(s)+20): for j in range(0,len(s)+20): try: res = sbv[i:j] resi = sbvi[i:j] except ValueError: self.assertTrue(i<=j) continue ref = long(getSlice(s, i, j), 2) self.assertEqual(res, ref) self.assertEqual(type(res), intbv) mask = (2**(i-j))-1 self.assertEqual(resi, ref ^ mask) self.assertEqual(type(resi), intbv)
def testGetSlice(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for i in range(1, len(s) + 20): for j in range(0, len(s) + 20): try: res = bv[i:j] resi = bvi[i:j] except ValueError: assert i <= j continue ref = long(getSlice(s, i, j), 2) assert res == ref assert type(res) == intbv mask = (2 ** (i - j)) - 1 assert resi == ref ^ mask assert type(resi) == intbv
def testGetSlice(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) bv = intbv(n) bvi = intbv(~n) for i in range(1, len(s) + 20): for j in range(0, len(s) + 20): try: res = bv[i:j] resi = bvi[i:j] except ValueError: self.assertTrue(i <= j) continue ref = long(getSlice(s, i, j), 2) self.assertEqual(res, ref) self.assertEqual(type(res), intbv) mask = (2**(i - j)) - 1 self.assertEqual(resi, ref ^ mask) self.assertEqual(type(resi), intbv)
def __init__(self, *args): assert len(args) >= 2 self._args = args self._sigargs = sigargs = [] nrbits = 0 val = 0 for a in args: # print( 'ConcatSignal', repr( a )) if isinstance(a, intbv): w = a._nrbits v = a._val elif isinstance(a, _Signal): # a._read = True sigargs.append(a) w = a._nrbits if isinstance(a._val, intbv): v = a._val._val else: v = a._val a._read = True elif isinstance(a, bool): w = 1 v = a elif isinstance(a, str): # remove underscores aa = a.replace('_', '') w = len(aa) v = long(aa, 2) else: raise TypeError( "ConcatSignal: inappropriate argument type: %s" % type(a)) nrbits += w val = val << w | v & (long(1) << w) - 1 self._initval = val ini = intbv(val)[nrbits:] _ShadowSignal.__init__(self, ini) self._driven = 'wire' gen = self.genfunc() self._waiter = _SignalTupleWaiter(gen)
def __setitem__(self, key, val): # convert val to int to avoid confusion with intbv or Signals val = int(val) if isinstance(key, slice): i, j = key.start, key.stop if j is None: # default j = 0 j = int(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 % (long(1) << j) self._val = val * (long(1) << j) + q self._handleBounds() return i = int(i) if i <= j: raise ValueError("intbv[i:j] = v requires i > j\n" " i, j, v == %s, %s, %s" % (i, j, val)) lim = (long(1) << (i - j)) if val >= lim or val < -lim: raise ValueError("intbv[i:j] = v abs(v) too large\n" " i, j, v == %s, %s, %s" % (i, j, val)) mask = (lim - 1) << j self._val &= ~mask self._val |= (val << j) self._handleBounds() else: i = int(key) if val == 1: self._val |= (long(1) << i) elif val == 0: self._val &= ~(long(1) << i) else: raise ValueError("intbv[i] = v requires v in (0, 1)\n" " i == %s " % i) self._handleBounds()
def check(): for i in range(2**len(a)): for j in (0, 1): for k in range(2**len(c)): for m in range(2**len(d)): for n in range(2**len(e)): a.next = i b.next = j c.next = k d.next = m e.next = n yield delay(10) assert s[29:27] == long(c1, 2) assert s[27:22] == a assert s[21] == long(c2, 2) assert s[20] == b assert s[20:17] == c3 assert s[17:14] == c assert s[13] == c4 assert s[13:9] == d assert s[9:1] == c5 assert s[1:] == e
def __setitem__(self, key, val): # convert val to int to avoid confusion with intbv or Signals val = int(val) if isinstance(key, slice): i, j = key.start, key.stop if j is None: # default j = 0 j = int(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 % (long(1) << j) self._val = val * (long(1) << j) + q self._handleBounds() return i = int(i) if i <= j: raise ValueError("intbv[i:j] = v requires i > j\n" \ " i, j, v == %s, %s, %s" % (i, j, val)) lim = (long(1) << (i-j)) if val >= lim or val < -lim: raise ValueError("intbv[i:j] = v abs(v) too large\n" \ " i, j, v == %s, %s, %s" % (i, j, val)) mask = (lim-1) << j self._val &= ~mask self._val |= (val << j) self._handleBounds() else: i = int(key) if val == 1: self._val |= (long(1) << i) elif val == 0: self._val &= ~(long(1) << i) else: raise ValueError("intbv[i] = v requires v in (0, 1)\n" \ " i == %s " % i) self._handleBounds()
def testSetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) for it in (int, intbv): for i in range(len(s) + 20): # print i bv0 = intbv(n) bv1 = intbv(n) bv0i = intbv(~n) bv1i = intbv(~n) bv0[i] = it(0) bv1[i] = it(1) bv0i[i] = it(0) bv1i[i] = it(1) ref0 = long(setItem(s, i, "0"), 2) ref1 = long(setItem(s, i, "1"), 2) ref0i = ~long(setItem(s, i, "1"), 2) ref1i = ~long(setItem(s, i, "0"), 2) assert bv0 == ref0 assert bv1 == ref1 assert bv0i == ref0i assert bv1i == ref1i
def testSetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) for it in (int, intbv): for i in range(len(s)+20): # print i bv0 = intbv(n) bv1 = intbv(n) bv0i = intbv(~n) bv1i = intbv(~n) bv0[i] = it(0) bv1[i] = it(1) bv0i[i] = it(0) bv1i[i] = it(1) ref0 = long(setItem(s, i, '0'), 2) ref1 = long(setItem(s, i, '1'), 2) ref0i = ~long(setItem(s, i, '1'), 2) ref1i = ~long(setItem(s, i, '0'), 2) self.assertEqual(bv0, ref0) self.assertEqual(bv1, ref1) self.assertEqual(bv0i, ref0i) self.assertEqual(bv1i, ref1i)
def augmentedAssignCheck(self, op, imin=0, imax=None, jmin=0, jmax=None): self.seqSetup(imin=imin, imax=imax, jmin=jmin, jmax=jmax) for i, j in zip(self.seqi, self.seqj): bj = Signal(j) ref = long(i) ref = op(ref, j) r1 = bi1 = Signal(i) try: r1 = op(r1, j) except TypeError: pass else: self.fail() r2 = long(i) r2 = op(r2, bj) r3 = bi3 = Signal(i) try: r3 = op(r3, bj) except TypeError: pass else: self.fail() self.assertEqual(r2, ref)
def testSetItem(self): self.seqsSetup() for s in self.seqs: n = long(s, 2) for it in (int, intbv): for i in range(len(s) + 20): # print i bv0 = intbv(n) bv1 = intbv(n) bv0i = intbv(~n) bv1i = intbv(~n) bv0[i] = it(0) bv1[i] = it(1) bv0i[i] = it(0) bv1i[i] = it(1) ref0 = long(setItem(s, i, '0'), 2) ref1 = long(setItem(s, i, '1'), 2) ref0i = ~long(setItem(s, i, '1'), 2) ref1i = ~long(setItem(s, i, '0'), 2) self.assertEqual(bv0, ref0) self.assertEqual(bv1, ref1) self.assertEqual(bv0i, ref0i) self.assertEqual(bv1i, ref1i)
def bin(num, width=0): """Return a binary string representation. num -- number to convert Optional parameter: width -- specifies the desired string (sign bit padding) """ num = long(num) s = _int2bitstring(num) if width: pad = '0' if num < 0: pad = '1' return (width - len(s)) * pad + s return s
def __getitem__(self, key): if isinstance(key, slice): i, j = key.start, key.stop if j is None: # default j = 0 j = int(j) if j < 0: raise ValueError("intbv[i:j] requires j >= 0\n" \ " j == %s" % j) if i is None: # default return intbv(self._val >> j) i = int(i) if i <= j: raise ValueError("intbv[i:j] requires i > j\n" \ " i, j == %s, %s" % (i, j)) res = intbv((self._val & (long(1) << i)-1) >> j, _nrbits=i-j) return res else: i = int(key) res = bool((self._val >> i) & 0x1) return res
def __getitem__(self, key): if isinstance(key, slice): i, j = key.start, key.stop if j is None: # default j = 0 j = int(j) if j < 0: raise ValueError("intbv[i:j] requires j >= 0\n" " j == %s" % j) if i is None: # default return intbv(self._val >> j) i = int(i) if i <= j: raise ValueError("intbv[i:j] requires i > j\n" " i, j == %s, %s" % (i, j)) res = intbv((self._val & (long(1) << i) - 1) >> j, _nrbits=i - j) return res else: i = int(key) res = bool((self._val >> i) & 0x1) return res
def __long__(self): return long(self._val)
def __invert__(self): if self._nrbits and self._min >= 0: return intbv(~self._val & (long(1) << self._nrbits)-1) else: return intbv(~self._val)