Skip to content

Commit 0413976

Browse files
committed
Improve Verilog output when a memory doesn't have any writes
1 parent 40f1863 commit 0413976

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

pyrtl/importexport.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -758,24 +758,25 @@ def _to_verilog_memories(file, block, varname):
758758
memories = {n.op_param[1] for n in block.logic_subset('m@')}
759759
for m in sorted(memories, key=lambda m: m.id):
760760
print(' // Memory mem_{}: {}'.format(m.id, m.name), file=file)
761-
writes = _net_sorted(block.logic_subset('@'), varname)
761+
writes = [net for net in _net_sorted(block.logic_subset('@'), varname)
762+
if net.op_param[1] == m]
762763
if writes:
763764
print(' always @(posedge clk)', file=file)
764765
print(' begin', file=file)
765766
for net in writes:
766-
if net.op_param[1] == m:
767-
t = (varname(net.args[2]), net.op_param[0],
768-
varname(net.args[0]), varname(net.args[1]))
769-
print((' if (%s) begin\n'
770-
' mem_%s[%s] <= %s;\n'
771-
' end') % t, file=file)
767+
t = (varname(net.args[2]), net.op_param[0],
768+
varname(net.args[0]), varname(net.args[1]))
769+
print((' if (%s) begin\n'
770+
' mem_%s[%s] <= %s;\n'
771+
' end') % t, file=file)
772772
print(' end', file=file)
773-
for net in _net_sorted(block.logic_subset('m'), varname):
774-
if net.op_param[1] == m:
775-
dest = varname(net.dests[0])
776-
m_id = net.op_param[0]
777-
index = varname(net.args[0])
778-
print(' assign {:s} = mem_{}[{:s}];'.format(dest, m_id, index), file=file)
773+
reads = [net for net in _net_sorted(block.logic_subset('m'), varname)
774+
if net.op_param[1] == m]
775+
for net in reads:
776+
dest = varname(net.dests[0])
777+
m_id = net.op_param[0]
778+
index = varname(net.args[0])
779+
print(' assign {:s} = mem_{}[{:s}];'.format(dest, m_id, index), file=file)
779780
print('', file=file)
780781

781782

tests/test_importexport.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,56 @@ def test_blif_nor_gate_correct(self):
13841384
"""
13851385

13861386

1387+
verilog_output_mems_with_no_writes = """\
1388+
// Generated automatically via PyRTL
1389+
// As one initial test of synthesis, map to FPGA with:
1390+
// yosys -p "synth_xilinx -top toplevel" thisfile.v
1391+
1392+
module toplevel(clk, rst, in1, out1);
1393+
input clk;
1394+
input rst;
1395+
input[2:0] in1;
1396+
output[7:0] out1;
1397+
1398+
reg[7:0] mem_0[7:0]; //tmp0
1399+
reg[7:0] mem_1[255:0]; //tmp1
1400+
1401+
wire const_0_1;
1402+
wire[7:0] const_1_42;
1403+
wire[7:0] tmp2;
1404+
1405+
initial begin
1406+
mem_0[0]=8'ha;
1407+
mem_0[1]=8'h14;
1408+
mem_0[2]=8'h1e;
1409+
mem_0[3]=8'h28;
1410+
mem_0[4]=8'h32;
1411+
mem_0[5]=8'h3c;
1412+
mem_0[6]=8'h0;
1413+
mem_0[7]=8'h0;
1414+
end
1415+
1416+
// Combinational
1417+
assign const_0_1 = 1;
1418+
assign const_1_42 = 42;
1419+
assign out1 = tmp2;
1420+
1421+
// Memory mem_0: tmp0
1422+
assign tmp2 = mem_0[in1];
1423+
1424+
// Memory mem_1: tmp1
1425+
always @(posedge clk)
1426+
begin
1427+
if (const_0_1) begin
1428+
mem_1[tmp2] <= const_1_42;
1429+
end
1430+
end
1431+
1432+
endmodule
1433+
1434+
"""
1435+
1436+
13871437
verilog_output_counter_no_reset = """\
13881438
// Generated automatically via PyRTL
13891439
// As one initial test of synthesis, map to FPGA with:
@@ -1568,6 +1618,21 @@ def test_textual_consistency_large(self):
15681618

15691619
self.assertEqual(buffer.getvalue(), verilog_output_large)
15701620

1621+
def test_mems_with_no_writes(self):
1622+
rdata = {0: 10, 1: 20, 2: 30, 3: 40, 4: 50, 5: 60}
1623+
rom = pyrtl.RomBlock(8, 3, rdata, pad_with_zeros=True)
1624+
mem = pyrtl.MemBlock(8, 8)
1625+
in1 = pyrtl.Input(3, 'in1')
1626+
out1 = pyrtl.Output(8, 'out1')
1627+
w = rom[in1]
1628+
out1 <<= w
1629+
mem[w] <<= 42
1630+
1631+
buffer = io.StringIO()
1632+
pyrtl.output_to_verilog(buffer)
1633+
1634+
self.assertEqual(buffer.getvalue(), verilog_output_mems_with_no_writes)
1635+
15711636
def check_counter_text(self, add_reset, expected):
15721637
r = pyrtl.Register(4, reset_value=2)
15731638
r.next <<= r + 1

0 commit comments

Comments
 (0)