Skip to content

Commit c9eb984

Browse files
committed
Fix maxlens when signal is a boolean. Also fix old bug in IPython
output. Bug was that it was using the width of the wire name, rather than the wire, to determine if the wire was a boolean (1 bit) or not.
1 parent 9a923aa commit c9eb984

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

pyrtl/visualization.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,15 @@ def trace_to_html(simtrace, trace_list=None, sortkey=None, repr_func=hex):
481481

482482
wave_template = (
483483
"""\
484-
<script type="WaveDrom">
485-
{ signal : [
486-
%s
487-
],
488-
config: {hscale: %d }}
489-
</script>
490-
491-
"""
484+
<script type="WaveDrom">
485+
{
486+
signal : [
487+
%s
488+
],
489+
config: { hscale: %d }
490+
}
491+
</script>
492+
"""
492493
)
493494

494495
vallens = [] # For determining longest value length
@@ -501,7 +502,7 @@ def extract(w):
501502
if last == value:
502503
wavelist.append('.')
503504
else:
504-
if len(w) == 1:
505+
if len(simtrace._wires[w]) == 1:
505506
wavelist.append(str(value))
506507
else:
507508
wavelist.append('=')
@@ -510,14 +511,15 @@ def extract(w):
510511

511512
wavestring = ''.join(wavelist)
512513
datastring = ', '.join(['"%s"' % repr_func(data) for data in datalist])
513-
vallens.extend([len(repr_func(data)) for data in datalist])
514-
if len(w) == 1:
514+
if len(simtrace._wires[w]) == 1:
515+
vallens.append(1) # all are the same length
515516
return bool_signal_template % (w, wavestring)
516517
else:
518+
vallens.extend([len(repr_func(data)) for data in datalist])
517519
return int_signal_template % (w, wavestring, datastring)
518520

519-
bool_signal_template = '{ name: "%s", wave: "%s" },'
520-
int_signal_template = '{ name: "%s", wave: "%s", data: [%s] },'
521+
bool_signal_template = ' { name: "%s", wave: "%s" },'
522+
int_signal_template = ' { name: "%s", wave: "%s", data: [%s] },'
521523
signals = [extract(w) for w in trace_list]
522524
all_signals = '\n'.join(signals)
523525
maxvallen = max(vallens)

tests/test_visualization.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,68 @@ def test_one_bit_adder_matches_expected(self):
340340

341341
htmlstring = pyrtl.trace_to_html(sim_trace) # tests if it compiles or not
342342

343+
def test_trace_to_html(self):
344+
i = pyrtl.Input(1, 'i')
345+
o = pyrtl.Output(2, 'o')
346+
o <<= i + 1
347+
348+
sim = pyrtl.Simulation()
349+
sim.step_multiple({'i': '0100110'})
350+
htmlstring = pyrtl.trace_to_html(sim.tracer)
351+
expected = (
352+
'<script type="WaveDrom">\n'
353+
'{\n'
354+
' signal : [\n'
355+
' { name: "i", wave: "010.1.0" },\n'
356+
' { name: "o", wave: "===.=.=", data: ["0x1", "0x2", "0x1", "0x2", "0x1"] },\n'
357+
' ],\n'
358+
' config: { hscale: 1 }\n'
359+
'}\n'
360+
'</script>\n'
361+
)
362+
self.assertEqual(htmlstring, expected)
363+
364+
def test_trace_to_html_repr_func(self):
365+
i = pyrtl.Input(1, 'i')
366+
o = pyrtl.Output(2, 'o')
367+
o <<= i + 1
368+
369+
sim = pyrtl.Simulation()
370+
sim.step_multiple({'i': '0100110'})
371+
htmlstring = pyrtl.trace_to_html(sim.tracer, repr_func=bin)
372+
expected = (
373+
'<script type="WaveDrom">\n'
374+
'{\n'
375+
' signal : [\n'
376+
' { name: "i", wave: "010.1.0" },\n'
377+
' { name: "o", wave: "===.=.=", data: ["0b1", "0b10", "0b1", "0b10", "0b1"] },\n'
378+
' ],\n'
379+
' config: { hscale: 1 }\n'
380+
'}\n'
381+
'</script>\n'
382+
)
383+
self.assertEqual(htmlstring, expected)
384+
385+
def test_trace_to_html_repr_func_2(self):
386+
i = pyrtl.Input(1, 'i')
387+
o = pyrtl.Output(2, 'o')
388+
o <<= i + 1
389+
390+
sim = pyrtl.Simulation()
391+
sim.step_multiple({'i': '0100110'})
392+
htmlstring = pyrtl.trace_to_html(sim.tracer, repr_func=bin)
393+
expected = (
394+
'<script type="WaveDrom">\n'
395+
'{\n'
396+
' signal : [\n'
397+
' { name: "i", wave: "010.1.0" },\n'
398+
' { name: "o", wave: "===.=.=", data: ["0b1", "0b10", "0b1", "0b10", "0b1"] },\n'
399+
' ],\n'
400+
' config: { hscale: 1 }\n'
401+
'}\n'
402+
'</script>\n'
403+
)
404+
self.assertEqual(htmlstring, expected)
343405

344406
if __name__ == "__main__":
345407
unittest.main()

0 commit comments

Comments
 (0)