2015-02-18 09:32:34 -05:00
|
|
|
def dec2bin(d, nb=0):
|
2015-04-13 07:37:46 -04:00
|
|
|
if d == "x":
|
2015-04-13 07:09:44 -04:00
|
|
|
return "x"*nb
|
2015-04-13 07:37:46 -04:00
|
|
|
elif d == 0:
|
|
|
|
b = "0"
|
2015-04-13 07:09:44 -04:00
|
|
|
else:
|
2015-04-13 07:37:46 -04:00
|
|
|
b = ""
|
|
|
|
while d != 0:
|
|
|
|
b = "01"[d&1] + b
|
|
|
|
d = d >> 1
|
2015-04-13 07:09:44 -04:00
|
|
|
return b.zfill(nb)
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-02-22 18:20:17 -05:00
|
|
|
def get_bits(values, low, high=None):
|
2015-04-13 07:09:44 -04:00
|
|
|
r = []
|
|
|
|
if high is None:
|
|
|
|
high = low+1
|
|
|
|
for val in values:
|
|
|
|
t = (val >> low) & (2**(high-low)-1)
|
|
|
|
r.append(t)
|
|
|
|
return r
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-02-18 09:32:34 -05:00
|
|
|
class Dat(list):
|
2015-04-13 07:09:44 -04:00
|
|
|
def __init__(self, width):
|
|
|
|
self.width = width
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return get_bits(self, key)
|
|
|
|
elif isinstance(key, slice):
|
|
|
|
if key.start != None:
|
|
|
|
start = key.start
|
|
|
|
else:
|
|
|
|
start = 0
|
|
|
|
if key.stop != None:
|
|
|
|
stop = key.stop
|
|
|
|
else:
|
|
|
|
stop = self.width
|
|
|
|
if stop > self.width:
|
|
|
|
stop = self.width
|
|
|
|
if key.step != None:
|
|
|
|
raise KeyError
|
|
|
|
return get_bits(self, start, stop)
|
|
|
|
else:
|
|
|
|
raise KeyError
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def decode_rle(self):
|
|
|
|
datas = Dat(self.width-1)
|
|
|
|
last_data = 0
|
|
|
|
for data in self:
|
|
|
|
rle = data >> (self.width-1)
|
|
|
|
data = data & (2**(self.width-1)-1)
|
|
|
|
if rle:
|
|
|
|
for i in range(data):
|
|
|
|
datas.append(last_data)
|
|
|
|
else:
|
|
|
|
datas.append(data)
|
|
|
|
last_data = data
|
|
|
|
return datas
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-02-18 09:32:34 -05:00
|
|
|
class Var:
|
2015-04-13 07:09:44 -04:00
|
|
|
def __init__(self, name, width, values=[], type="wire", default="x"):
|
|
|
|
self.type = type
|
|
|
|
self.width = width
|
|
|
|
self.name = name
|
|
|
|
self.val = default
|
|
|
|
self.values = values
|
|
|
|
self.vcd_id = None
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def set_vcd_id(self, s):
|
|
|
|
self.vcd_id = s
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.values)
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def change(self, cnt):
|
|
|
|
r = ""
|
2015-04-13 07:25:27 -04:00
|
|
|
try:
|
2015-04-13 07:09:44 -04:00
|
|
|
if self.values[cnt+1] != self.val:
|
|
|
|
r += "b"
|
|
|
|
r += dec2bin(self.values[cnt+1], self.width)
|
|
|
|
r += " "
|
|
|
|
r += self.vcd_id
|
|
|
|
r += "\n"
|
|
|
|
return r
|
2015-04-13 07:25:27 -04:00
|
|
|
except:
|
2015-04-13 07:09:44 -04:00
|
|
|
return r
|
|
|
|
return r
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-02-18 09:32:34 -05:00
|
|
|
class Dump:
|
2015-04-13 07:09:44 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.vars = []
|
|
|
|
self.vcd_id = "!"
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def add(self, var):
|
|
|
|
var.set_vcd_id(self.vcd_id)
|
|
|
|
self.vcd_id = chr(ord(self.vcd_id)+1)
|
|
|
|
self.vars.append(var)
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def add_from_layout(self, layout, var):
|
2015-04-13 07:37:46 -04:00
|
|
|
i = 0
|
2015-04-13 07:09:44 -04:00
|
|
|
for s, n in layout:
|
|
|
|
self.add(Var(s, n, var[i:i+n]))
|
|
|
|
i += n
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def __len__(self):
|
|
|
|
l = 0
|
|
|
|
for var in self.vars:
|
2015-04-13 07:23:48 -04:00
|
|
|
l = max(len(var), l)
|
2015-04-13 07:09:44 -04:00
|
|
|
return l
|