Skip to content

Commit 97394bd

Browse files
authored
Merge pull request #321 from pllab/named-constants
Adding option to name constants
2 parents dc93d88 + 64d7960 commit 97394bd

File tree

8 files changed

+79
-17
lines changed

8 files changed

+79
-17
lines changed

pyrtl/inputoutput.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ def label(v):
558558
return v
559559

560560
if isinstance(node, Const):
561-
return '[label="%d", shape=circle, fillcolor=lightgrey]' % label(node.val)
561+
name = node.name + ': ' if not node.name.startswith('const_') else ''
562+
return '[label="%s", shape=circle, fillcolor=lightgrey]' % label(name + str(node.val))
562563
elif isinstance(node, Input):
563564
return '[label="%s", shape=invhouse, fillcolor=coral]' % label(node.name)
564565
elif isinstance(node, Output):

pyrtl/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ def __init__(self, wires_to_track=None, block=None):
992992
self.block = working_block(block)
993993

994994
def is_internal_name(name):
995-
return (name.startswith('tmp') or name.startswith('const')
995+
return (name.startswith('tmp') or name.startswith('const_')
996996
# or name.startswith('synth_')
997997
or name.endswith("'"))
998998

pyrtl/transform.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def clone_wire(old_wire, name=None):
179179
same name in the same block is not allowed
180180
"""
181181
if isinstance(old_wire, Const):
182-
return Const(old_wire.val, old_wire.bitwidth)
182+
if name is None:
183+
return Const(old_wire.val, old_wire.bitwidth, name=old_wire.name)
184+
return Const(old_wire.val, old_wire.bitwidth, name=name)
183185
else:
184186
if name is None:
185187
return old_wire.__class__(old_wire.bitwidth, name=old_wire.name)

pyrtl/wire.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,10 @@ class Const(WireVector):
579579

580580
_code = 'C'
581581

582-
def __init__(self, val, bitwidth=None, signed=False, block=None):
582+
def __init__(self, val, bitwidth=None, name='', signed=False, block=None):
583583
""" Construct a constant implementation at initialization
584584
585-
:param int, bool, or str val: The value for the const wirevector
585+
:param int, bool, or str val: the value for the const wirevector
586586
:param int: the desired bitwidth of the resulting const
587587
:param signed: specify if bits should be used for twos complement
588588
:return: a wirevector object representing a const wire
@@ -611,7 +611,7 @@ def __init__(self, val, bitwidth=None, signed=False, block=None):
611611
'constant %d returned by infer_val_and_bitwidth somehow not fitting in %d bits'
612612
% (num, bitwidth))
613613

614-
name = _constIndexer.make_valid_string() + '_' + str(val)
614+
name = name if name else _constIndexer.make_valid_string() + '_' + str(val)
615615

616616
super(Const, self).__init__(bitwidth=bitwidth, name=name, block=block)
617617
# add the member "val" to track the value of the constant

tests/test_simulation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,25 @@ class MyEnum(IntEnum):
300300
sim.step({'i': MyEnum.C})
301301
self.assertEqual(sim.inspect(o), 1)
302302

303+
def test_named_consts(self):
304+
in1 = pyrtl.Input(8, "in1")
305+
c1 = pyrtl.Const(1, 1, "c1")
306+
out1 = pyrtl.Output(16, "out1")
307+
out1 <<= in1 + c1
308+
sim_trace = pyrtl.SimulationTrace()
309+
sim = self.sim(tracer=sim_trace)
310+
for i in range(10):
311+
sim.step({
312+
'in1': 2 * i,
313+
})
314+
correct_outp = (" --- Values in base 10 ---\n"
315+
"c1 1 1 1 1 1 1 1 1 1 1\n"
316+
"in1 0 2 4 6 8 10 12 14 16 18\n"
317+
"out1 1 3 5 7 9 11 13 15 17 19\n")
318+
output = six.StringIO()
319+
sim_trace.print_trace(output)
320+
self.assertEqual(output.getvalue(), correct_outp)
321+
303322

304323
class SimInputValidationBase(unittest.TestCase):
305324
def setUp(self):

tests/test_transform.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ def num_wire_of_type(self, wiretype, num, block=None):
2424
self.assertEquals(len(block.wirevector_subset(wiretype)), num)
2525

2626

27+
class WireMemoryNameTestCases(unittest.TestCase):
28+
def setUp(self):
29+
pyrtl.reset_working_block()
30+
31+
def name_wires(self, names, block=None):
32+
block = pyrtl.working_block()
33+
names = set(names.split(' '))
34+
for n in names:
35+
self.assertIn(n, block.wirevector_by_name)
36+
37+
def name_memories(self, names, block=None):
38+
block = pyrtl.working_block()
39+
names = set(names.split(' '))
40+
for n in names:
41+
self.assertIn(n, block.memblock_by_name)
42+
43+
2744
def insert_random_inversions(rate=0.5):
2845
"""
2946
an example transform that can be used for testing
@@ -57,7 +74,7 @@ def test_randomly_replace(self):
5774
self.assertIsNot(new_and_net.dests[0], o)
5875

5976

60-
class TestCopyBlock(NetWireNumTestCases):
77+
class TestCopyBlock(NetWireNumTestCases, WireMemoryNameTestCases):
6178
def num_memories(self, mems_expected, block):
6279
memories = set()
6380
for net in block.logic_subset('m@'):
@@ -70,27 +87,31 @@ def test_blank(self):
7087
self.assert_num_wires(0, block)
7188

7289
def test_block(self):
73-
a = pyrtl.Const(23)
74-
b = pyrtl.Input(5)
75-
o = pyrtl.Output(5)
90+
a = pyrtl.Const(23, name='a')
91+
b = pyrtl.Input(5, name='b')
92+
o = pyrtl.Output(5, name='o')
7693
o <<= ~a & b
7794

7895
old_block = pyrtl.working_block()
7996
old_block.sanity_check()
8097
self.assert_num_wires(5, old_block)
8198
self.assert_num_net(3, old_block)
8299

100+
self.name_wires('a b o', old_block)
101+
83102
new_block = transform.copy_block()
84103
new_block.sanity_check()
85104
self.assert_num_wires(5, new_block)
86-
self.assert_num_net(3, old_block)
105+
self.assert_num_net(3, new_block)
106+
107+
self.name_wires('a b o', new_block)
87108

88109
def test_copy_mem(self):
89110
ins = [pyrtl.Input(5) for i in range(4)]
90111
out = pyrtl.Output(5)
91112

92-
mem1 = pyrtl.MemBlock(5, 5)
93-
mem2 = pyrtl.MemBlock(5, 5)
113+
mem1 = pyrtl.MemBlock(5, 5, name='mem1')
114+
mem2 = pyrtl.MemBlock(5, 5, name='mem2')
94115

95116
mem1_o1 = mem1[ins[0]]
96117
mem1[ins[1]] <<= ins[2]
@@ -104,12 +125,16 @@ def test_copy_mem(self):
104125
self.num_net_of_type('&', 1, old_block)
105126
self.num_memories(2, old_block)
106127

128+
self.name_memories('mem1 mem2', old_block)
129+
107130
new_block = transform.copy_block()
108131
self.num_net_of_type('m', 2, new_block)
109132
self.num_net_of_type('@', 1, new_block)
110133
self.num_net_of_type('&', 1, new_block)
111134
self.num_memories(2, new_block)
112135

136+
self.name_memories('mem1 mem2', new_block)
137+
113138

114139
class TestFastWireReplace(unittest.TestCase):
115140
def setUp(self):
@@ -191,3 +216,6 @@ def probe_cond(wire):
191216
transform_examples.probe_wire_if(probe_cond)
192217
probe.assert_called_once_with(test_wire)
193218
"""
219+
220+
if __name__ == "__main__":
221+
unittest.main()

tests/test_verilog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
1616
wire[3:0] const_0_12;
1717
wire[2:0] const_1_3;
18-
wire[5:0] const_2_38;
18+
wire[5:0] k;
1919
wire[12:0] tmp0;
2020
2121
// Combinational
2222
assign const_0_12 = 12;
2323
assign const_1_3 = 3;
24-
assign const_2_38 = 38;
24+
assign k = 38;
2525
assign o = tmp0;
26-
assign tmp0 = {const_0_12, const_1_3, const_2_38};
26+
assign tmp0 = {const_0_12, const_1_3, k};
2727
2828
endmodule
2929
@@ -642,7 +642,7 @@ def test_romblock_does_not_throw_error(self):
642642
def test_textual_consistency_small(self):
643643
i = pyrtl.Const(0b1100)
644644
j = pyrtl.Const(0b011, bitwidth=3)
645-
k = pyrtl.Const(0b100110)
645+
k = pyrtl.Const(0b100110, name='k')
646646
o = pyrtl.Output(13, 'o')
647647
o <<= pyrtl.concat(i, j, k)
648648

tests/test_wire.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ def test_assignment(self):
316316
c = pyrtl.Const(4)
317317
c <<= 3
318318

319+
def test_named(self):
320+
block = pyrtl.working_block()
321+
c = pyrtl.Const(20, name="archid")
322+
self.assertIn("archid", block.wirevector_by_name)
323+
self.assertIn(c, block.wirevector_set)
324+
self.assertEqual(c.val, 20)
325+
c.name = "vendorid"
326+
self.assertNotIn("archid", block.wirevector_by_name)
327+
self.assertIn("vendorid", block.wirevector_by_name)
328+
self.assertIn(c, block.wirevector_set)
329+
self.assertEqual(c.val, 20)
330+
319331
def check_const(self, val_in, expected_val, expected_bitwidth, **kargs):
320332
c = pyrtl.Const(val_in, **kargs)
321333
self.assertEqual(c.val, expected_val)

0 commit comments

Comments
 (0)