2012-09-09 14:51:15 -04:00
|
|
|
import sys
|
2015-02-17 17:44:22 -05:00
|
|
|
import os
|
|
|
|
import shutil
|
2012-08-26 14:56:56 -04:00
|
|
|
import datetime
|
2015-02-17 17:44:22 -05:00
|
|
|
import zipfile
|
2012-08-26 14:56:56 -04:00
|
|
|
|
2014-05-22 10:11:32 -04:00
|
|
|
def dec2bin(d, nb=0):
|
|
|
|
if d=="x":
|
|
|
|
return "x"*nb
|
|
|
|
elif d==0:
|
|
|
|
b="0"
|
|
|
|
else:
|
|
|
|
b=""
|
|
|
|
while d!=0:
|
|
|
|
b="01"[d&1]+b
|
|
|
|
d=d>>1
|
|
|
|
return b.zfill(nb)
|
2012-08-26 14:56:56 -04:00
|
|
|
|
2013-03-21 07:23:44 -04:00
|
|
|
def get_bits(values, width, low, high=None):
|
2012-09-17 14:15:35 -04:00
|
|
|
r = []
|
|
|
|
for val in values:
|
2013-03-21 07:23:44 -04:00
|
|
|
t = dec2bin(val, width)[::-1]
|
2012-09-17 14:15:35 -04:00
|
|
|
if high == None:
|
|
|
|
t = t[low]
|
|
|
|
else:
|
|
|
|
t = t[low:high]
|
2014-06-21 13:06:47 -04:00
|
|
|
t = t[::-1]
|
2012-09-17 14:15:35 -04:00
|
|
|
t = int(t,2)
|
|
|
|
r.append(t)
|
|
|
|
return r
|
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
class Dat(list):
|
2013-03-21 07:23:44 -04:00
|
|
|
def __init__(self, width):
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return get_bits(self, self.width, 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, self.width, start, stop)
|
|
|
|
else:
|
|
|
|
raise KeyError
|
|
|
|
|
2013-03-23 08:57:59 -04:00
|
|
|
def decode_rle(self):
|
|
|
|
rle_bit = self[-1]
|
|
|
|
rle_dat = self[:self.width-1]
|
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
dat = Dat(self.width)
|
2013-03-23 08:57:59 -04:00
|
|
|
i=0
|
|
|
|
last = 0
|
|
|
|
for d in self:
|
|
|
|
if rle_bit[i]:
|
|
|
|
if len(dat) >= 1:
|
2013-09-22 12:41:44 -04:00
|
|
|
# FIX ME... why is rle_dat in reverse order...
|
2013-03-23 08:57:59 -04:00
|
|
|
for j in range(int(dec2bin(rle_dat[i])[::-1],2)):
|
|
|
|
dat.append(last)
|
|
|
|
else:
|
|
|
|
dat.append(d)
|
|
|
|
last = d
|
|
|
|
i +=1
|
2014-08-03 02:38:37 -04:00
|
|
|
return dat
|
2013-03-23 08:57:59 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
class Var:
|
2013-03-21 07:23:44 -04:00
|
|
|
def __init__(self, name, width, values=[], type="wire", default="x"):
|
2012-08-26 14:56:56 -04:00
|
|
|
self.type = type
|
|
|
|
self.width = width
|
|
|
|
self.name = name
|
|
|
|
self.val = default
|
|
|
|
self.values = values
|
|
|
|
self.vcd_id = None
|
2015-01-23 09:31:25 -05:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def set_vcd_id(self, s):
|
|
|
|
self.vcd_id = s
|
2015-01-23 09:31:25 -05:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.values)
|
|
|
|
|
|
|
|
def change(self, cnt):
|
|
|
|
r = ""
|
2015-01-23 09:31:25 -05:00
|
|
|
try :
|
2012-08-26 14:56:56 -04:00
|
|
|
if self.values[cnt+1] != self.val:
|
|
|
|
r += "b"
|
2014-06-21 13:06:47 -04:00
|
|
|
r += dec2bin(self.values[cnt+1], self.width)
|
2012-08-26 14:56:56 -04:00
|
|
|
r += " "
|
|
|
|
r += self.vcd_id
|
|
|
|
r += "\n"
|
|
|
|
return r
|
|
|
|
except :
|
|
|
|
return r
|
|
|
|
return r
|
2013-03-21 07:23:44 -04:00
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
class Dump:
|
|
|
|
def __init__(self):
|
2012-08-26 14:56:56 -04:00
|
|
|
self.vars = []
|
|
|
|
self.vcd_id = "!"
|
2015-01-23 09:31:25 -05:00
|
|
|
|
2012-08-26 14:56:56 -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)
|
2013-03-21 07:23:44 -04:00
|
|
|
|
|
|
|
def add_from_layout(self, layout, var):
|
|
|
|
i=0
|
|
|
|
for s, n in layout:
|
|
|
|
self.add(Var(s, n, var[i:i+n]))
|
|
|
|
i += n
|
2015-01-23 09:31:25 -05:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def __len__(self):
|
|
|
|
l = 0
|
|
|
|
for var in self.vars:
|
|
|
|
l = max(len(var),l)
|
|
|
|
return l
|
2014-06-19 07:01:18 -04:00
|
|
|
|
|
|
|
class VCDExport():
|
|
|
|
def __init__(self, dump, timescale="1ps", comment=""):
|
|
|
|
self.dump = dump
|
|
|
|
self.timescale = timescale
|
|
|
|
self.comment = comment
|
|
|
|
self.cnt = -1
|
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def change(self):
|
|
|
|
r = ""
|
|
|
|
c = ""
|
2014-06-19 07:01:18 -04:00
|
|
|
for var in self.dump.vars:
|
2012-08-26 14:56:56 -04:00
|
|
|
c += var.change(self.cnt)
|
|
|
|
if c != "":
|
|
|
|
r += "#"
|
|
|
|
r += str(self.cnt+1)
|
|
|
|
r += "\n"
|
|
|
|
r += c
|
|
|
|
return r
|
|
|
|
|
|
|
|
def p_date(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
r = "$date\n"
|
|
|
|
r += "\t"
|
|
|
|
r += now.strftime("%Y-%m-%d %H:%M")
|
|
|
|
r += "\n"
|
|
|
|
r += "$end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_version(self):
|
|
|
|
r = "$version\n"
|
|
|
|
r += "\tmiscope VCD dump\n"
|
|
|
|
r += "$end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_comment(self):
|
|
|
|
r = "$comment\n"
|
|
|
|
r += self.comment
|
|
|
|
r += "\n$end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_timescale(self):
|
|
|
|
r = "$timescale "
|
|
|
|
r += self.timescale
|
|
|
|
r += " $end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_scope(self):
|
|
|
|
r = "$scope "
|
|
|
|
r += self.timescale
|
|
|
|
r += " $end\n"
|
|
|
|
return r
|
2013-03-21 07:23:44 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_vars(self):
|
|
|
|
r = ""
|
2014-06-19 07:01:18 -04:00
|
|
|
for var in self.dump.vars:
|
2012-08-26 14:56:56 -04:00
|
|
|
r += "$var "
|
|
|
|
r += var.type
|
|
|
|
r += " "
|
|
|
|
r += str(var.width)
|
|
|
|
r += " "
|
|
|
|
r += var.vcd_id
|
|
|
|
r += " "
|
|
|
|
r += var.name
|
|
|
|
r += " $end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_unscope(self):
|
|
|
|
r = "$unscope "
|
|
|
|
r += " $end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_enddefinitions(self):
|
|
|
|
r = "$enddefinitions "
|
|
|
|
r += " $end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_dumpvars(self):
|
|
|
|
r = "$dumpvars\n"
|
2014-06-19 07:01:18 -04:00
|
|
|
for var in self.dump.vars:
|
2012-08-26 14:56:56 -04:00
|
|
|
r += "b"
|
|
|
|
r += dec2bin(var.val, var.width)
|
|
|
|
r += " "
|
|
|
|
r += var.vcd_id
|
|
|
|
r+= "\n"
|
|
|
|
r += "$end\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
def p_valuechange(self):
|
|
|
|
r = ""
|
2014-06-19 07:01:18 -04:00
|
|
|
for i in range(len(self.dump)):
|
2012-08-26 14:56:56 -04:00
|
|
|
r += self.change()
|
|
|
|
self.cnt += 1
|
|
|
|
return r
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = ""
|
|
|
|
r += self.p_date()
|
|
|
|
r += self.p_version()
|
|
|
|
r += self.p_comment()
|
|
|
|
r += self.p_timescale()
|
|
|
|
r += self.p_scope()
|
|
|
|
r += self.p_vars()
|
|
|
|
r += self.p_unscope()
|
|
|
|
r += self.p_enddefinitions()
|
|
|
|
r += self.p_dumpvars()
|
|
|
|
r += self.p_valuechange()
|
|
|
|
return r
|
2014-06-19 07:01:18 -04:00
|
|
|
|
2012-09-14 18:57:52 -04:00
|
|
|
def write(self, filename):
|
|
|
|
f = open(filename, "w")
|
|
|
|
f.write(str(self))
|
|
|
|
f.close()
|
2012-08-26 14:56:56 -04:00
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
class CSVExport():
|
|
|
|
def __init__(self, dump):
|
|
|
|
self.dump = dump
|
2014-06-17 05:25:10 -04:00
|
|
|
|
|
|
|
def p_vars(self):
|
|
|
|
r = ""
|
2014-06-19 07:01:18 -04:00
|
|
|
for var in self.dump.vars:
|
2014-06-17 05:25:10 -04:00
|
|
|
r += var.name
|
|
|
|
r += ","
|
|
|
|
r += "\n"
|
2014-06-19 07:01:18 -04:00
|
|
|
for var in self.dump.vars:
|
2014-06-17 05:25:10 -04:00
|
|
|
r += str(var.width)
|
|
|
|
r += ","
|
|
|
|
r += "\n"
|
|
|
|
return r
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
def p_dumpvars(self):
|
|
|
|
r = ""
|
2014-06-19 07:01:18 -04:00
|
|
|
for i in range(len(self.dump)):
|
|
|
|
for var in self.dump.vars:
|
2014-06-17 05:25:10 -04:00
|
|
|
try:
|
|
|
|
var.val = var.values[i]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if var.val == "x":
|
|
|
|
r += "x"
|
|
|
|
else:
|
|
|
|
r += dec2bin(var.val, var.width)
|
|
|
|
r += ", "
|
|
|
|
r+= "\n"
|
|
|
|
return r
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = ""
|
|
|
|
r += self.p_vars()
|
|
|
|
r += self.p_dumpvars()
|
|
|
|
return r
|
2014-06-19 07:01:18 -04:00
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
def write(self, filename):
|
|
|
|
f = open(filename, "w")
|
|
|
|
f.write(str(self))
|
|
|
|
f.close()
|
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
class PYExport():
|
|
|
|
def __init__(self, dump):
|
|
|
|
self.dump = dump
|
2014-06-17 05:25:10 -04:00
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
def __repr__(self):
|
|
|
|
r = "dump = {\n"
|
|
|
|
for var in self.dump.vars:
|
|
|
|
r += "\"" + var.name + "\""
|
|
|
|
r += " : "
|
|
|
|
r += str(var.values)
|
|
|
|
r += ",\n"
|
|
|
|
r += "}"
|
|
|
|
return r
|
|
|
|
|
|
|
|
def write(self, filename):
|
|
|
|
f = open(filename, "w")
|
|
|
|
f.write(str(self))
|
2014-08-03 02:38:37 -04:00
|
|
|
f.close()
|
2014-06-17 05:25:10 -04:00
|
|
|
|
2015-02-17 17:44:22 -05:00
|
|
|
class SRExport():
|
|
|
|
def __init__(self, dump):
|
|
|
|
self.dump = dump
|
|
|
|
|
|
|
|
def create_version(self):
|
|
|
|
f = open("version", "w")
|
|
|
|
f.write("1")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def create_metadata(self, name):
|
|
|
|
f = open("metadata", "w")
|
|
|
|
r = """
|
|
|
|
[global]
|
|
|
|
sigrok version = 0.2.0
|
|
|
|
[device 1]
|
|
|
|
driver = litescope
|
|
|
|
capturefile = {}
|
|
|
|
unitsize = 1
|
|
|
|
total probes = {}
|
|
|
|
samplerate = {} MHz
|
|
|
|
""".format(
|
|
|
|
name,
|
|
|
|
8, # XXX add parameter
|
|
|
|
50, # XXX add parameter
|
|
|
|
)
|
|
|
|
# XXX add probe names
|
|
|
|
f.write(r)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def create_data(self, name):
|
|
|
|
f = open(name, "wb")
|
|
|
|
# XXX
|
|
|
|
for i in range(16):
|
|
|
|
f.write(bytes(i))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def zip(self, name):
|
|
|
|
def zipdir(path, zip):
|
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for file in files:
|
|
|
|
# XXX
|
|
|
|
os.chdir(root)
|
|
|
|
zip.write(file)
|
|
|
|
os.chdir("..")
|
|
|
|
zipf = zipfile.ZipFile(name + ".sr", 'w')
|
|
|
|
zipdir(name, zipf)
|
|
|
|
zipf.close()
|
|
|
|
|
|
|
|
def write(self, filename):
|
|
|
|
name, ext = os.path.splitext(filename)
|
|
|
|
if os.path.exists(name):
|
|
|
|
shutil.rmtree(name)
|
|
|
|
os.makedirs(name)
|
|
|
|
os.chdir(name)
|
|
|
|
self.create_version()
|
|
|
|
self.create_metadata(name)
|
|
|
|
self.create_data(name)
|
|
|
|
os.chdir("..")
|
|
|
|
self.zip(name)
|
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
def main():
|
|
|
|
dump = Dump()
|
|
|
|
dump.add(Var("foo1", 1, [0,1,0,1,0,1]))
|
|
|
|
dump.add(Var("foo2", 2, [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]))
|
|
|
|
ramp = [i%128 for i in range(1024)]
|
|
|
|
dump.add(Var("ramp", 16, ramp))
|
2015-01-23 09:31:25 -05:00
|
|
|
|
2014-06-19 07:01:18 -04:00
|
|
|
VCDExport(dump).write("mydump.vcd")
|
|
|
|
CSVExport(dump).write("mydump.csv")
|
|
|
|
PYExport(dump).write("mydump.py")
|
2015-02-17 17:44:22 -05:00
|
|
|
SRExport(dump).write("dump.sr")
|
2014-08-03 02:38:37 -04:00
|
|
|
|
2012-08-26 14:56:56 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|