Skip to content

Commit

Permalink
Merge pull request #31 from Ben1152000/code-formatting
Browse files Browse the repository at this point in the history
Code formatting & minor bugfixes
  • Loading branch information
Ben1152000 authored Jan 17, 2022
2 parents efec012 + 226b773 commit 4145e9e
Show file tree
Hide file tree
Showing 15 changed files with 1,158 additions and 788 deletions.
92 changes: 61 additions & 31 deletions sootty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,104 @@
from .storage import WireTrace
from .visualizer import Visualizer


def main():

parser = argparse.ArgumentParser(description='Converts .vcd wiretraces to .svg format.')
parser.add_argument('filename', metavar='FILENAME', type=str, help='input .vcd file')
parser.add_argument('-s', '--start',
type=str, metavar='FORMULA', dest='start',
parser = argparse.ArgumentParser(
description="Converts .vcd wiretraces to .svg format."
)
parser.add_argument(
"filename", metavar="FILENAME", type=str, help="input .vcd file"
)
parser.add_argument(
"-s",
"--start",
type=str,
metavar="FORMULA",
dest="start",
help='formula for the start of the window (ex: \
"time 4", \
"after time 2 & clk", \
"x == y | z == const 15")')
parser.add_argument('-e', '--end',
type=str, metavar='FORMULA', dest='end',
help='formula for the end of the window')
parser.add_argument('-b', '--break',
type=str, metavar='FORMULA', dest='breakpoints',
help='formula for the points in time to be highlighted')
parser.add_argument('-l', '--length',
type=int, dest='length',
help='number of cycles to display')
parser.add_argument('-d', '--display',
action='store_true',
help='display to command line')
parser.add_argument('-w', '--wires',
type=str, metavar='LIST', dest='wires',
help='comma-separated list of wires to view')
"x == y | z == const 15")',
)
parser.add_argument(
"-e",
"--end",
type=str,
metavar="FORMULA",
dest="end",
help="formula for the end of the window",
)
parser.add_argument(
"-b",
"--break",
type=str,
metavar="FORMULA",
dest="breakpoints",
help="formula for the points in time to be highlighted",
)
parser.add_argument(
"-l", "--length", type=int, dest="length", help="number of cycles to display"
)
parser.add_argument(
"-d", "--display", action="store_true", help="display to command line"
)
parser.add_argument(
"-w",
"--wires",
type=str,
metavar="LIST",
dest="wires",
help="comma-separated list of wires to view",
)
args = parser.parse_args()

# Load vcd file into wiretrace object.
wiretrace = WireTrace.from_vcd(args.filename)

# Check that window bounds are well-defined.
if args.end is not None and args.length is not None:
raise Exception('Length and end flags should not be provided simultaneously.')
raise Exception("Length and end flags should not be provided simultaneously.")

# Calculate window bounds.
if args.end is not None:
if args.start is not None:
start, end = wiretrace.compute_limits(args.start, args.end)
else:
start, end = wiretrace.compute_limits('time 0', args.end)
start, end = wiretrace.compute_limits("time 0", args.end)
length = end - start
else:
if args.start is not None:
start, end = wiretrace.compute_limits(args.start, 'time 0')
start, end = wiretrace.compute_limits(args.start, "time 0")
else:
start = 0
length = args.length if args.length is not None else wiretrace.length() - start

# Calculate breakpoints
breakpoints = None
if args.breakpoints is not None:
breakpoints = wiretrace.evaluate(args.breakpoints)

wires = None
if args.wires:
wires = set([name.strip() for name in args.wires.split(',')])
wires = set([name.strip() for name in args.wires.split(",")])

# Convert wiretrace to graphical vector image.
image = Visualizer().to_svg(wiretrace, start=start, length=length, wires=wires, breakpoints=breakpoints)
image = Visualizer().to_svg(
wiretrace, start=start, length=length, wires=wires, breakpoints=breakpoints
)

if wires is not None and len(wires):
raise Exception(f'Unknown wires {wires.__repr__()}\nThe following wires were detected in the wiretrace:\n{wiretrace.get_wire_names()}')

raise Exception(
f"Unknown wires {wires.__repr__()}\nThe following wires were detected in the wiretrace:\n{wiretrace.get_wire_names()}"
)

if args.display:
image.display()

else:
print(image.source)


if __name__ == "__main__":
main()
16 changes: 9 additions & 7 deletions sootty/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from subprocess import call, Popen, STDOUT, PIPE
from re import match


class VectorImage:
"""Encapsulates logic to store and display an SVG to the terminal."""

Expand All @@ -10,13 +11,14 @@ def __init__(self, source):

def __str__(self):
return self.source

def display(self):
# TODO: fix!!! these lines are for hacking together width relative to display
result = match(r'<svg viewBox="0 0 (\d+) (\d+)".*', self.source)
ratio = float(result.group(2)) / float(result.group(1))
width = 24000
height = int(ratio * width * 5 / 12)

process = Popen(f"rsvg-convert -z 4 | viu - -w {width} -h {height}", shell=True, stdin=PIPE, stdout=sys.stdout)
# result = match(r'<svg viewBox="0 0 (\d+) (\d+)".*', self.source)
# ratio = float(result.group(2)) / float(result.group(1))
# width = 24000
# height = int(ratio * width * 5 / 12)
process = Popen(
f"rsvg-convert -z 4 | viu -", shell=True, stdin=PIPE, stdout=sys.stdout
)
process.communicate(input=str.encode(self.source))
9 changes: 5 additions & 4 deletions sootty/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

class SoottyError(Exception):
""" Raised on any user-facing error in this module. """
"""Raised on any user-facing error in this module."""

pass


class SoottyInternalError(Exception):
""" Raised on any Sootty internal failure. """
pass
"""Raised on any Sootty internal failure."""

pass
3 changes: 2 additions & 1 deletion sootty/limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Read and interpret grammar file.
from . import static

parser = Lark(pkg_resources.open_text(static, "grammar.lark").read())


Expand All @@ -26,7 +27,7 @@ def binexp(self, tree):

def start(self, tree):
self.binexp(tree)

def lexp(self, tree):
self.binexp(tree)

Expand Down
24 changes: 17 additions & 7 deletions sootty/storage/valuechange.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class ValueChange(SortedDict):

def __init__(self, width=1, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self.width = width
Expand All @@ -23,7 +22,12 @@ def length(self):
"""Returns the time duration of the wire."""
return next(self.irange(reverse=True)) if len(self) > 0 else 0

def search(self, function=lambda value: type(value) is int and value > 0, start=None, end=None):
def search(
self,
function=lambda value: type(value) is int and value > 0,
start=None,
end=None,
):
"""Returns a list of times that satisfy the function, between start and end times."""
indices = []
prev = None
Expand All @@ -42,7 +46,9 @@ def search(self, function=lambda value: type(value) is int and value > 0, start=
def __invert__(self):
data = ValueChange(width=self.width)
for key in self:
data[key] = None if self[key] == None else (~self[key] & (2 << self.width - 1) - 1)
data[key] = (
None if self[key] == None else (~self[key] & (2 << self.width - 1) - 1)
)
return data

def __neg__(self):
Expand All @@ -62,7 +68,11 @@ def _binop(self, other, binop, width):
values[0] = self[key]
if key in other:
values[1] = other[key]
reduced = None if (values[0] is None or values[1] is None) else binop(values[0], values[1])
reduced = (
None
if (values[0] is None or values[1] is None)
else binop(values[0], values[1])
)
if reduced != values[2]:
values[2] = reduced
data[key] = reduced
Expand Down Expand Up @@ -106,10 +116,10 @@ def __add__(self, other):

def __sub__(self, other):
return self._binop(other, lambda x, y: x - y, max(self.width, other.width) + 1)

def __mod__(self, other):
return self._binop(other, lambda x, y: x % y, self.width)

def _from(self):
data = ValueChange(width=1)
data[0] = 0
Expand All @@ -136,7 +146,7 @@ def _until(self):
data[key + 1] = 0
break
return data

def _before(self):
data = ValueChange(width=1)
data[0] = 1
Expand Down
Loading

0 comments on commit 4145e9e

Please sign in to comment.