Skip to content

Commit 70c9f6e

Browse files
authored
Merge pull request #394 from pllab/html-trace-update
Fix html trace visualization when value is a boolean and/or supplied …
2 parents fd25635 + 6b6283e commit 70c9f6e

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

pyrtl/simulation.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numbers
88
import collections
99
import copy
10+
import six
1011

1112
from .pyrtlexceptions import PyrtlError, PyrtlInternalError
1213
from .core import working_block, PostSynthBlock, _PythonSanitizer
@@ -627,9 +628,18 @@ def step_multiple(self, provided_inputs={}, expected_outputs={}, nsteps=None,
627628
"any expected outputs must have a supplied value "
628629
"each step of simulation")
629630

631+
def to_num(v):
632+
if isinstance(v, six.string_types):
633+
# Don't use infer_val_and_bitwidth because they aren't in
634+
# Verilog-style format, but are instead in plain decimal.
635+
return int(v)
636+
# Don't just call int(v) on all of them since it's nice
637+
# to retain class info if they were a subclass of int.
638+
return v
639+
630640
failed = []
631641
for i in range(nsteps):
632-
self.step({w: int(v[i]) for w, v in provided_inputs.items()})
642+
self.step({w: to_num(v[i]) for w, v in provided_inputs.items()})
633643

634644
for expvar in expected_outputs.keys():
635645
expected = int(expected_outputs[expvar][i])

pyrtl/visualization.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -504,31 +504,31 @@ def extract(w):
504504
wavelist = []
505505
datalist = []
506506
last = None
507+
507508
for i, value in enumerate(trace[w]):
508509
if last == value:
509510
wavelist.append('.')
510511
else:
511-
if len(simtrace._wires[w]) == 1:
512-
wavelist.append(str(value))
512+
f = repr_per_name.get(w)
513+
if f is not None:
514+
wavelist.append('=')
515+
datalist.append(str(f(value)))
516+
elif len(simtrace._wires[w]) == 1:
517+
# int() to convert True/False to 0/1
518+
wavelist.append(str(int(value)))
513519
else:
514520
wavelist.append('=')
515-
datalist.append((value, w))
516-
last = value
521+
datalist.append(str(repr_func(value)))
517522

518-
def to_str(v, name):
519-
f = repr_per_name.get(name)
520-
if f is not None:
521-
return str(f(v))
522-
else:
523-
return str(repr_func(v))
523+
last = value
524524

525525
wavestring = ''.join(wavelist)
526-
datastring = ', '.join(['"%s"' % to_str(data, name) for data, name in datalist])
527-
if len(simtrace._wires[w]) == 1:
526+
datastring = ', '.join(['"%s"' % data for data in datalist])
527+
if repr_per_name.get(w) is None and len(simtrace._wires[w]) == 1:
528528
vallens.append(1) # all are the same length
529529
return bool_signal_template % (w, wavestring)
530530
else:
531-
vallens.extend([len(to_str(data, name)) for data, name in datalist])
531+
vallens.extend([len(data) for data in datalist])
532532
return int_signal_template % (w, wavestring, datastring)
533533

534534
bool_signal_template = ' { name: "%s", wave: "%s" },'

tests/test_visualization.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import random
33
import io
44
import pyrtl
5-
from .test_importexport import full_adder_blif
65

76

87
graphviz_string_detailed = """\
@@ -177,16 +176,19 @@ def setUp(self):
177176
pyrtl.reset_working_block()
178177

179178
def test_output_to_tgf_does_not_throw_error(self):
179+
from .test_importexport import full_adder_blif
180180
with io.StringIO() as vfile:
181181
pyrtl.input_from_blif(full_adder_blif)
182182
pyrtl.output_to_trivialgraph(vfile)
183183

184184
def test_output_to_graphviz_does_not_throw_error(self):
185+
from .test_importexport import full_adder_blif
185186
with io.StringIO() as vfile:
186187
pyrtl.input_from_blif(full_adder_blif)
187188
pyrtl.output_to_graphviz(vfile)
188189

189190
def test_output_to_graphviz_with_custom_namer_does_not_throw_error(self):
191+
from .test_importexport import full_adder_blif
190192
with io.StringIO() as vfile:
191193
pyrtl.input_from_blif(full_adder_blif)
192194
timing = pyrtl.TimingAnalysis()
@@ -426,6 +428,44 @@ class Foo(IntEnum):
426428
)
427429
self.assertEqual(htmlstring, expected)
428430

431+
def test_trace_to_html_repr_per_name_enum_is_bool(self):
432+
from enum import IntEnum
433+
434+
class Foo(IntEnum):
435+
A = 0
436+
B = 1
437+
438+
i = pyrtl.Input(2, 'i')
439+
state = pyrtl.Register(max(Foo).bit_length(), name='state')
440+
o = pyrtl.Output(name='o')
441+
o <<= state
442+
443+
with pyrtl.conditional_assignment:
444+
with i == 0b01:
445+
state.next |= Foo.A
446+
with i == 0b10:
447+
state.next |= Foo.B
448+
449+
sim = pyrtl.Simulation()
450+
sim.step_multiple({
451+
'i': [1, 2, 1, 2, 2]
452+
})
453+
454+
htmlstring = pyrtl.trace_to_html(sim.tracer, repr_per_name={'state': Foo})
455+
expected = (
456+
'<script type="WaveDrom">\n'
457+
'{\n'
458+
' signal : [\n'
459+
' { name: "i", wave: "====.", data: ["0x1", "0x2", "0x1", "0x2"] },\n'
460+
' { name: "o", wave: "0.101" },\n'
461+
' { name: "state", wave: "=.===", data: ["Foo.A", "Foo.B", "Foo.A", "Foo.B"] },\n'
462+
' ],\n'
463+
' config: { hscale: 2 }\n'
464+
'}\n'
465+
'</script>\n'
466+
)
467+
self.assertEqual(htmlstring, expected)
468+
429469

430470
if __name__ == "__main__":
431471
unittest.main()

0 commit comments

Comments
 (0)