From a7373589193fc0a65420185367e96b256bc80095 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Tue, 17 Jun 2014 11:25:10 +0200 Subject: [PATCH] host: split read/export and add csv export --- miscope/host/drivers.py | 44 +++++++++-------- miscope/host/{vcd.py => export.py} | 77 ++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 23 deletions(-) rename miscope/host/{vcd.py => export.py} (77%) diff --git a/miscope/host/drivers.py b/miscope/host/drivers.py index 7668af67..423b7d39 100644 --- a/miscope/host/drivers.py +++ b/miscope/host/drivers.py @@ -1,7 +1,7 @@ import csv import time import sys -from miscope.host.vcd import * +from miscope.host.export import * from miscope.host.truthtable import * class MiIoDriver(): @@ -23,20 +23,19 @@ class MiIoDriver(): return self.miio_i.read() 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.name = name self.use_rle = use_rle - - if csv_name is None: - self.csv = name + ".csv" + if config_csv is None: + self.config_csv = name + ".csv" self.get_config() self.get_layout() self.build_mila() - self.dat = VcdDat(self.width) + self.dat = Dat(self.width) 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: t, n, v = item if t == "config": @@ -44,7 +43,7 @@ class MiLaDriver(): def get_layout(self): 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: t, n, v = item if t == "layout": @@ -64,10 +63,8 @@ class MiLaDriver(): setattr(self, name+"_m", (2**length-1) << value) value += length - def show_state(self, s, last=False): - print(s, end="") - if not last: - print("-->", end="") + def show_state(self, s): + print(s, end="|") sys.stdout.flush() def prog_term(self, port, trigger, mask): @@ -116,8 +113,8 @@ class MiLaDriver(): self.mila_recorder_length.write(length) self.mila_recorder_trigger.write(1) - def read(self, vcd=None): - self.show_state("READ", last=not vcd) + def read(self): + self.show_state("READ") empty = self.mila_recorder_read_empty.read() while(not empty): self.dat.append(self.mila_recorder_read_dat.read()) @@ -125,8 +122,17 @@ class MiLaDriver(): self.mila_recorder_read_en.write(1) if self.use_rle: self.dat = self.dat.decode_rle() - if vcd: - self.show_state("OUTPUT", last=True) - _vcd = Vcd() - _vcd.add_from_layout(self.layout, self.dat) - _vcd.write(vcd) + return self.dat + + def export(self, export_fn=None): + self.show_state("EXPORT") + 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 diff --git a/miscope/host/vcd.py b/miscope/host/export.py similarity index 77% rename from miscope/host/vcd.py rename to miscope/host/export.py index 0259e609..d9b80509 100644 --- a/miscope/host/vcd.py +++ b/miscope/host/export.py @@ -26,7 +26,7 @@ def get_bits(values, width, low, high=None): r.append(t) return r -class VcdDat(list): +class Dat(list): def __init__(self, width): self.width = width @@ -54,7 +54,7 @@ class VcdDat(list): rle_bit = self[-1] rle_dat = self[:self.width-1] - dat = VcdDat(self.width) + dat = Dat(self.width) i=0 last = 0 for d in self: @@ -98,7 +98,7 @@ class Var: return r return r -class Vcd: +class VCD: def __init__(self, timescale="1ps", comment=""): self.timescale = timescale self.comment = comment @@ -229,8 +229,67 @@ class Vcd: f.write(str(self)) 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(): - myvcd = Vcd() + myvcd = VCD() 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("foo3", 3)) @@ -238,6 +297,16 @@ def main(): ramp = [i%128 for i in range(1024)] myvcd.add(Var("ramp", 16, ramp)) 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__': main() -- 2.30.2