Skip to content

Commit

Permalink
Merge pull request #46 from yihuajack/numeral-system
Browse files Browse the repository at this point in the history
Add numeral system for vector change data
  • Loading branch information
yihuajack committed Aug 28, 2022
2 parents f5755b8 + f210168 commit 4b36777
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
27 changes: 23 additions & 4 deletions sootty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def main():
dest="wires",
help="comma-separated list of wires to view",
)
arg_radix = parser.add_argument(
"-r",
"--radix",
type=int,
default=10,
dest="radix",
help="displayed radix of data numbers (2 - 36)",
)
parser.add_argument(
"-S",
"--save",
Expand All @@ -83,6 +91,9 @@ def main():
)
args = parser.parse_args()

if args.radix < 2 or args.radix > 36:
raise argparse.ArgumentError(arg_radix, "radix must be between 2 and 36")

if args.save is not None:
if args.reload:
raise SoottyError(
Expand All @@ -97,6 +108,7 @@ def main():
args.start,
args.end,
args.display,
args.radix,
)

if args.reload is not None:
Expand All @@ -109,6 +121,7 @@ def main():
args.start,
args.end,
args.display,
args.radix,
args.reload,
savefile,
)
Expand All @@ -126,10 +139,11 @@ def main():
args.start,
args.end,
args.display,
args.radix,
)


def compile_query(filename, wires, breakpoints, length, start, end, display):
def compile_query(filename, wires, breakpoints, length, start, end, display, radix):

# Load vcd file into wiretrace object.
wiretrace = WireTrace.from_vcd(filename)
Expand Down Expand Up @@ -161,7 +175,7 @@ def compile_query(filename, wires, breakpoints, length, start, end, display):

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

if wires is not None and len(wires):
Expand All @@ -177,7 +191,7 @@ def compile_query(filename, wires, breakpoints, length, start, end, display):


def reload_query(
parser, filename, wires, breakpoints, length, start, end, display, reload, savefile
parser, filename, wires, breakpoints, length, start, end, display, radix, reload, savefile
):
with open(savefile, "r") as stream:
try:
Expand All @@ -189,7 +203,7 @@ def reload_query(
cmd = dat[reload]["query"]
args = parser.parse_args(shlex.split(cmd)) # using shlex to parse string correctly

# Updating specifc flags for a relaoded query
# Updating specific flags for a reloaded query

if filename is not None:
args.filename = filename
Expand All @@ -203,6 +217,10 @@ def reload_query(
args.start = start
if end is not None:
args.end = end
if display is not None:
args.display = display
if radix is not None:
args.radix = radix

if length is not None and end is not None:
raise SoottyError(
Expand All @@ -217,6 +235,7 @@ def reload_query(
args.start,
args.end,
args.display,
args.radix,
)


Expand Down
18 changes: 10 additions & 8 deletions sootty/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import yaml


def save_query(save, name, wires, br, length, start, end, display):
def save_query(save, name, wires, br, length, start, end, display, radix):
savefile = os.getenv("HOME") + "/.config/sootty/save/queries.yaml"

if is_save_file(savefile):
Expand All @@ -16,7 +16,7 @@ def save_query(save, name, wires, br, length, start, end, display):
if lines is None:
with open(savefile, "w") as stream:
query_write(
savefile, stream, save, name, wires, br, length, start, end, display
savefile, stream, save, name, wires, br, length, start, end, display, radix
)
else:
if save in lines:
Expand All @@ -37,35 +37,35 @@ def save_query(save, name, wires, br, length, start, end, display):

with open(savefile, "a+") as stream:
query_write(
savefile, stream, save, name, wires, br, length, start, end, display
savefile, stream, save, name, wires, br, length, start, end, display, radix
)

else:
# Creating new savefile as no savefiles found for sootty
print("Creating new savefile...")
with open(savefile, "w") as stream:
query_write(
savefile, stream, save, name, wires, br, length, start, end, display
savefile, stream, save, name, wires, br, length, start, end, display, radix
)


def query_write(
savefile_path, savefile, save, name, wires, br, length, start, end, display
savefile_path, savefile, save, name, wires, br, length, start, end, display, radix
):
with open(savefile_path, "r") as stream:
lines = yaml.safe_load(stream)
if lines is None or (not save in lines):
savefile.write(save + ":\n")
savefile.write(" query:")
savefile.write(query_build(name, wires, br, length, start, end, display))
savefile.write(query_build(name, wires, br, length, start, end, display, radix))
savefile.write("\n")
savefile.write(" date: " + str(datetime.datetime.now()) + "\n")
else:
del lines[save] # Deleting outdated query
overwrite_dict = {
save: {
"query": query_build(
name, wires, br, length, start, end, display
name, wires, br, length, start, end, display, radix
),
"date": str(datetime.datetime.now()),
}
Expand All @@ -79,7 +79,7 @@ def query_write(
) # Dumping the overwritten query to the file, forcing no inline output


def query_build(name, wires, br, length, start, end, display):
def query_build(name, wires, br, length, start, end, display, radix):
"""
Constructing the query using conditionals
"""
Expand All @@ -98,6 +98,8 @@ def query_build(name, wires, br, length, start, end, display):
cmd += f' -e "{end}"'
if display:
cmd += f" -d"
if radix:
cmd += f' -r "{radix}"'
return cmd


Expand Down
14 changes: 14 additions & 0 deletions sootty/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from math import ceil, log

# Convert a decimal into any base (2 - 36)
def dec2anybase(input, base, width):
res = str()
while (input > 0):
rem = input % base
if (rem >= 0 and rem <= 9):
res += chr(rem + ord('0'))
else:
res += chr(rem - 10 + ord('A'))
input = int(input / base)

return res[::-1].zfill(ceil(log(2 ** width - 1, base)))
24 changes: 17 additions & 7 deletions sootty/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .display import VectorImage
from .exceptions import SoottyInternalError
from .utils import dec2anybase


class Style:
Expand Down Expand Up @@ -65,16 +66,16 @@ def __init__(self, style=Style.Default):
"""Optionally pass in a style class to control how the visualizer looks."""
self.style = style

def to_svg(self, wiretrace, start=0, length=None, wires=None, breakpoints=None):
def to_svg(self, wiretrace, start=0, length=None, wires=None, breakpoints=None, vector_radix=10):
if length is None:
length = wiretrace.length()
"""Converts the provided wiretrace object to a VectorImage object (svg)."""
return VectorImage(
self._wiretrace_to_svg(wiretrace, start, length, wires, breakpoints)
self._wiretrace_to_svg(wiretrace, start, length, wires, breakpoints, vector_radix)
)

def _wiretrace_to_svg(self, wiretrace, start, length, wires=None, breakpoints=None):
if wires and len(wires) == 0: # include all wires if empty list is provided
def _wiretrace_to_svg(self, wiretrace, start, length, wires=None, breakpoints=None, vector_radix=10):
if wires and len(wires) == 0: # include all wires if empty list provided
wires = None
width = (
2 * self.style.LEFT_MARGIN + self.style.TEXT_WIDTH + self.style.FULL_WIDTH
Expand Down Expand Up @@ -117,6 +118,7 @@ def _wiretrace_to_svg(self, wiretrace, start, length, wires=None, breakpoints=No
start=start,
length=length,
wires=wires,
vector_radix=vector_radix,
)
svg += result[0]
index = result[1]
Expand Down Expand Up @@ -178,7 +180,7 @@ def _breakpoints_to_svg(self, breakpoints, left, top, start, length, height):
)
return svg

def _wiregroup_to_svg(self, wiregroup, left, top, start, length, wires=None):
def _wiregroup_to_svg(self, wiregroup, left, top, start, length, wires=None, vector_radix=10):
svg = ""
index = 0
for wire in wiregroup.wires:
Expand All @@ -192,6 +194,7 @@ def _wiregroup_to_svg(self, wiregroup, left, top, start, length, wires=None):
+ (index * (self.style.WIRE_HEIGHT + self.style.WIRE_MARGIN)),
start=start,
length=length,
vector_radix=vector_radix,
)
index += 1
# recursively call function on nested wiregroups
Expand All @@ -203,12 +206,13 @@ def _wiregroup_to_svg(self, wiregroup, left, top, start, length, wires=None):
start=start,
length=length,
wires=wires,
vector_radix=vector_radix,
)
svg += result[0]
index += result[1]
return svg, index

def _wire_to_svg(self, wire, left, top, start, length):
def _wire_to_svg(self, wire, left, top, start, length, vector_radix=10):
svg = self._shape_to_svg(
{
"name": "text",
Expand All @@ -235,6 +239,7 @@ def _wire_to_svg(self, wire, left, top, start, length):
top=top,
length=length,
initial=(index == start),
vector_radix=vector_radix,
)
return svg

Expand Down Expand Up @@ -268,12 +273,17 @@ def type_from_value(value, width=1):
else:
return Visualizer.ValueType.DATA

def _value_to_svg(self, prev, value, width, left, top, length, initial=False):
def _value_to_svg(self, prev, value, width, left, top, length, initial=False, vector_radix=10):
# deduce types from wire width and value:
prev_type = Visualizer.type_from_value(prev, width)
value_type = Visualizer.type_from_value(value, width)
is_transitioning = prev != value

if vector_radix != 10 and value_type == Visualizer.ValueType.DATA: # VECTOR CHANGE DATA (no dup)
value = dec2anybase(value, vector_radix, width)
# if prev_type == Visualizer.ValueType.DATA:
# prev = dec2anybase(prev, vector_radix, width)

# The following code builds a list of svg objects depending on the
# current and previous value of the wire.
shapes = []
Expand Down

0 comments on commit 4b36777

Please sign in to comment.