host: split read/export and add csv export

This commit is contained in:
Florent Kermarrec 2014-06-17 11:25:10 +02:00
parent 8719206a3a
commit a737358919
2 changed files with 98 additions and 23 deletions

View File

@ -1,7 +1,7 @@
import csv import csv
import time import time
import sys import sys
from miscope.host.vcd import * from miscope.host.export import *
from miscope.host.truthtable import * from miscope.host.truthtable import *
class MiIoDriver(): class MiIoDriver():
@ -23,20 +23,19 @@ class MiIoDriver():
return self.miio_i.read() return self.miio_i.read()
class MiLaDriver(): class MiLaDriver():
def __init__(self, regs, name, csv_name=None, use_rle=True): def __init__(self, regs, name, config_csv=None, use_rle=True):
self.regs = regs self.regs = regs
self.name = name self.name = name
self.use_rle = use_rle self.use_rle = use_rle
if config_csv is None:
if csv_name is None: self.config_csv = name + ".csv"
self.csv = name + ".csv"
self.get_config() self.get_config()
self.get_layout() self.get_layout()
self.build_mila() self.build_mila()
self.dat = VcdDat(self.width) self.dat = Dat(self.width)
def get_config(self): def get_config(self):
csv_reader = csv.reader(open(self.csv), delimiter=',', quotechar='#') csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
for item in csv_reader: for item in csv_reader:
t, n, v = item t, n, v = item
if t == "config": if t == "config":
@ -44,7 +43,7 @@ class MiLaDriver():
def get_layout(self): def get_layout(self):
self.layout = [] self.layout = []
csv_reader = csv.reader(open(self.csv), delimiter=',', quotechar='#') csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
for item in csv_reader: for item in csv_reader:
t, n, v = item t, n, v = item
if t == "layout": if t == "layout":
@ -64,10 +63,8 @@ class MiLaDriver():
setattr(self, name+"_m", (2**length-1) << value) setattr(self, name+"_m", (2**length-1) << value)
value += length value += length
def show_state(self, s, last=False): def show_state(self, s):
print(s, end="") print(s, end="|")
if not last:
print("-->", end="")
sys.stdout.flush() sys.stdout.flush()
def prog_term(self, port, trigger, mask): def prog_term(self, port, trigger, mask):
@ -116,8 +113,8 @@ class MiLaDriver():
self.mila_recorder_length.write(length) self.mila_recorder_length.write(length)
self.mila_recorder_trigger.write(1) self.mila_recorder_trigger.write(1)
def read(self, vcd=None): def read(self):
self.show_state("READ", last=not vcd) self.show_state("READ")
empty = self.mila_recorder_read_empty.read() empty = self.mila_recorder_read_empty.read()
while(not empty): while(not empty):
self.dat.append(self.mila_recorder_read_dat.read()) self.dat.append(self.mila_recorder_read_dat.read())
@ -125,8 +122,17 @@ class MiLaDriver():
self.mila_recorder_read_en.write(1) self.mila_recorder_read_en.write(1)
if self.use_rle: if self.use_rle:
self.dat = self.dat.decode_rle() self.dat = self.dat.decode_rle()
if vcd: return self.dat
self.show_state("OUTPUT", last=True)
_vcd = Vcd() def export(self, export_fn=None):
_vcd.add_from_layout(self.layout, self.dat) self.show_state("EXPORT")
_vcd.write(vcd) if ".vcd" in export_fn:
vcd = VCD()
vcd.add_from_layout(self.layout, self.dat)
vcd.write(export_fn)
elif ".csv" in export_fn:
csv = CSV()
csv.add_from_layout(self.layout, self.dat)
csv.write(export_fn)
else:
raise NotImplementedError

View File

@ -26,7 +26,7 @@ def get_bits(values, width, low, high=None):
r.append(t) r.append(t)
return r return r
class VcdDat(list): class Dat(list):
def __init__(self, width): def __init__(self, width):
self.width = width self.width = width
@ -54,7 +54,7 @@ class VcdDat(list):
rle_bit = self[-1] rle_bit = self[-1]
rle_dat = self[:self.width-1] rle_dat = self[:self.width-1]
dat = VcdDat(self.width) dat = Dat(self.width)
i=0 i=0
last = 0 last = 0
for d in self: for d in self:
@ -98,7 +98,7 @@ class Var:
return r return r
return r return r
class Vcd: class VCD:
def __init__(self, timescale="1ps", comment=""): def __init__(self, timescale="1ps", comment=""):
self.timescale = timescale self.timescale = timescale
self.comment = comment self.comment = comment
@ -229,8 +229,67 @@ class Vcd:
f.write(str(self)) f.write(str(self))
f.close() f.close()
class CSV:
def __init__(self):
self.vars = []
self.cnt = -1
def add(self, var):
self.vars.append(var)
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
def __len__(self):
l = 0
for var in self.vars:
l = max(len(var),l)
return l
def p_vars(self):
r = ""
for var in self.vars:
r += var.name
r += ","
r += "\n"
for var in self.vars:
r += str(var.width)
r += ","
r += "\n"
return r
def p_dumpvars(self):
r = ""
for i in range(len(self)):
for var in self.vars:
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
def write(self, filename):
f = open(filename, "w")
f.write(str(self))
f.close()
def main(): def main():
myvcd = Vcd() myvcd = VCD()
myvcd.add(Var("foo1", 1, [0,1,0,1,0,1])) myvcd.add(Var("foo1", 1, [0,1,0,1,0,1]))
myvcd.add(Var("foo2", 2, [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0])) myvcd.add(Var("foo2", 2, [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]))
myvcd.add(Var("foo3", 3)) myvcd.add(Var("foo3", 3))
@ -238,6 +297,16 @@ def main():
ramp = [i%128 for i in range(1024)] ramp = [i%128 for i in range(1024)]
myvcd.add(Var("ramp", 16, ramp)) myvcd.add(Var("ramp", 16, ramp))
print(myvcd) print(myvcd)
mycsv = CSV()
mycsv.add(Var("foo1", 1, [0,1,0,1,0,1]))
mycsv.add(Var("foo2", 2, [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]))
mycsv.add(Var("foo3", 3))
mycsv.add(Var("foo4", 4))
ramp = [i%128 for i in range(1024)]
mycsv.add(Var("ramp", 16, ramp))
print(mycsv)
if __name__ == '__main__': if __name__ == '__main__':
main() main()