From d316948c8764075e9b1c355eb8bb5ab6c1324ef4 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Sun, 27 Sep 2015 17:54:28 +0200 Subject: [PATCH] software/dump: cleanup imports --- litescope/software/driver/la.py | 4 -- litescope/software/dump/__init__.py | 95 ++--------------------------- litescope/software/dump/common.py | 90 +++++++++++++++++++++++++++ litescope/software/dump/csv.py | 2 +- litescope/software/dump/python.py | 2 +- litescope/software/dump/sigrok.py | 2 +- litescope/software/dump/vcd.py | 2 +- test/dump_tb.py | 12 ++-- 8 files changed, 103 insertions(+), 106 deletions(-) create mode 100644 litescope/software/dump/common.py diff --git a/litescope/software/driver/la.py b/litescope/software/driver/la.py index 18b2f48..48b1f08 100644 --- a/litescope/software/driver/la.py +++ b/litescope/software/driver/la.py @@ -125,16 +125,12 @@ class LiteScopeLADriver(): print("saving to " + filename) name, ext = os.path.splitext(filename) if ext == ".vcd": - from litescope.software.dump.vcd import VCDDump dump = VCDDump() elif ext == ".csv": - from litescope.software.dump.csv import CSVDump dump = CSVDump() elif ext == ".py": - from litescope.software.dump.python import PythonDump dump = PythonDump() elif ext == ".sr": - from litescope.software.dump.sigrok import SigrokDump if self.samplerate is None: raise ValueError("Unable to automatically retrieve clk_freq, clk_freq parameter required") dump = SigrokDump(samplerate=self.samplerate) diff --git a/litescope/software/dump/__init__.py b/litescope/software/dump/__init__.py index 20fb9b1..0183987 100644 --- a/litescope/software/dump/__init__.py +++ b/litescope/software/dump/__init__.py @@ -1,90 +1,5 @@ -def dec2bin(d, width=0): - if d == "x": - return "x"*width - elif d == 0: - b = "0" - else: - b = "" - while d != 0: - b = "01"[d&1] + b - d = d >> 1 - return b.zfill(width) - - -def get_bits(values, low, high=None): - r = [] - if high is None: - high = low + 1 - for val in values: - t = (val >> low) & (2**(high - low) - 1) - r.append(t) - return r - - -class DumpData(list): - def __init__(self, width): - self.width = width - - 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 - - def decode_rle(self): - datas = DumpData(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 - - -class DumpVariable: - def __init__(self, name, width, values=[]): - self.width = width - self.name = name - self.values = [int(v)%2**width for v in values] - - def __len__(self): - return len(self.values) - - -class Dump: - def __init__(self): - self.variables = [] - - def add(self, variable): - self.variables.append(variable) - - def add_from_layout(self, layout, variable): - i = 0 - for s, n in layout: - self.add(DumpVariable(s, n, variable[i:i+n])) - i += n - - def __len__(self): - l = 0 - for variable in self.variables: - l = max(len(variable), l) - return l +from litescope.software.dump.common import DumpData, DumpVariable, Dump +from litescope.software.dump.csv import CSVDump +from litescope.software.dump.python import PythonDump +from litescope.software.dump.sigrok import SigrokDump +from litescope.software.dump.vcd import VCDDump diff --git a/litescope/software/dump/common.py b/litescope/software/dump/common.py new file mode 100644 index 0000000..20fb9b1 --- /dev/null +++ b/litescope/software/dump/common.py @@ -0,0 +1,90 @@ +def dec2bin(d, width=0): + if d == "x": + return "x"*width + elif d == 0: + b = "0" + else: + b = "" + while d != 0: + b = "01"[d&1] + b + d = d >> 1 + return b.zfill(width) + + +def get_bits(values, low, high=None): + r = [] + if high is None: + high = low + 1 + for val in values: + t = (val >> low) & (2**(high - low) - 1) + r.append(t) + return r + + +class DumpData(list): + def __init__(self, width): + self.width = width + + 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 + + def decode_rle(self): + datas = DumpData(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 + + +class DumpVariable: + def __init__(self, name, width, values=[]): + self.width = width + self.name = name + self.values = [int(v)%2**width for v in values] + + def __len__(self): + return len(self.values) + + +class Dump: + def __init__(self): + self.variables = [] + + def add(self, variable): + self.variables.append(variable) + + def add_from_layout(self, layout, variable): + i = 0 + for s, n in layout: + self.add(DumpVariable(s, n, variable[i:i+n])) + i += n + + def __len__(self): + l = 0 + for variable in self.variables: + l = max(len(variable), l) + return l diff --git a/litescope/software/dump/csv.py b/litescope/software/dump/csv.py index 7e575ea..93cad4f 100644 --- a/litescope/software/dump/csv.py +++ b/litescope/software/dump/csv.py @@ -1,4 +1,4 @@ -from litescope.software.dump import * +from litescope.software.dump.common import Dump, dec2bin class CSVDump(Dump): diff --git a/litescope/software/dump/python.py b/litescope/software/dump/python.py index fb96e63..7150b4e 100644 --- a/litescope/software/dump/python.py +++ b/litescope/software/dump/python.py @@ -1,4 +1,4 @@ -from litescope.software.dump import * +from litescope.software.dump.common import Dump class PythonDump(Dump): diff --git a/litescope/software/dump/sigrok.py b/litescope/software/dump/sigrok.py index a35c2d4..ecfcfb4 100644 --- a/litescope/software/dump/sigrok.py +++ b/litescope/software/dump/sigrok.py @@ -5,7 +5,7 @@ import zipfile import re from collections import OrderedDict -from litescope.software.dump import * +from litescope.software.dump.common import Dump, DumpVariable class SigrokDump(Dump): diff --git a/litescope/software/dump/vcd.py b/litescope/software/dump/vcd.py index 7f94354..55e0deb 100644 --- a/litescope/software/dump/vcd.py +++ b/litescope/software/dump/vcd.py @@ -1,5 +1,5 @@ import datetime -from litescope.software.dump import * +from litescope.software.dump.common import Dump, dec2bin class VCDDump(Dump): diff --git a/test/dump_tb.py b/test/dump_tb.py index 3fb3067..abff5a1 100644 --- a/test/dump_tb.py +++ b/test/dump_tb.py @@ -10,19 +10,15 @@ dump.add(DumpVariable("sinus", 8, [128+128*sin(j/(2*pi*16)) for j in range(1024) dump.add(DumpVariable("cosinus", 8, [128+128*cos(j/(2*pi*16)) for j in range(1024)])) print("csv export test") -from litescope.software.dump.csv import CSVDump CSVDump(dump).write("dump.csv") print("python export test...") -from litescope.software.dump.python import PythonDump PythonDump(dump).write("dump.py") -print("vcd export test...") -from litescope.software.dump.vcd import VCDDump -VCDDump(dump).write("dump.vcd") - print("sigrok export/import test...") -from litescope.software.dump.sigrok import SigrokDump SigrokDump(dump).write("dump.sr") SigrokDump(dump).read("dump.sr") -SigrokDump(dump).write("dump.sr") \ No newline at end of file +SigrokDump(dump).write("dump.sr") + +print("vcd export test...") +VCDDump(dump).write("dump.vcd") \ No newline at end of file