Skip to content

Commit e15cf0d

Browse files
authored
Merge pull request #401 from pllab/blif-import-update
Don't generate nands on BLIF import
2 parents 2d9ab35 + 750cc70 commit e15cf0d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pyrtl/importexport.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,21 @@ def twire(w):
325325
output_wire = twire(netio[2])
326326
output_wire <<= twire(netio[0]) | twire(netio[1]) # or gate
327327
elif command['cover_list'].asList() == ['0-', '1', '-0', '1']:
328+
# nand is not really a PyRTL primitive and so should only be added to a netlist
329+
# via a call to nand_synth(). We instead convert it to ~(a & b) rather than
330+
# (~a | ~b) as would be generated if handled by the else case below.
328331
output_wire = twire(netio[2])
329-
output_wire <<= twire(netio[0]).nand(twire(netio[1])) # nand gate
332+
output_wire <<= ~(twire(netio[0]) & twire(netio[1])) # nand gate -> not+and gates
330333
elif command['cover_list'].asList() == ['10', '1', '01', '1']:
331334
output_wire = twire(netio[2])
332335
output_wire <<= twire(netio[0]) ^ twire(netio[1]) # xor gate
333336
else:
334337
# Although the following is fully generic and thus encompasses all of the
335338
# special cases after the simple wire case above, we leave the above in because
336339
# they are commonly found and lead to a slightly cleaner (though equivalent) netlist,
337-
# because we can use nand/xor primitives, or avoid the extra fluff of concat/select
338-
# wires that might be created implicitly as part of rtl_all/rtl_any.
340+
# because we can use the xor primitive/save a gate when converting the nand, or avoid
341+
# the extra fluff of concat/select wires that might be created implicitly as part of
342+
# rtl_all/rtl_any.
339343
def convert_val(ix, val):
340344
wire = twire(netio[ix])
341345
if val == '0':

tests/test_importexport.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,9 @@ def test_blif_or_gate_correct(self):
663663
})
664664
self.assertEqual(sim.tracer.trace['o'], [0, 1, 1, 1])
665665

666-
def test_blif_nand_gate_correct(self):
666+
def test_blif_nand_gate_to_primitives_correct(self):
667+
# This tests that there should be no NAND gates generated during BLIF import;
668+
# they should be converted to AND+NOT.
667669
blif = """\
668670
.model Top
669671
.inputs a b
@@ -675,7 +677,9 @@ def test_blif_nand_gate_correct(self):
675677
"""
676678
pyrtl.input_from_blif(blif)
677679
block = pyrtl.working_block()
678-
self.assertEqual(len(block.logic_subset('n')), 1)
680+
self.assertEqual(len(block.logic_subset('n')), 0)
681+
self.assertEqual(len(block.logic_subset('&')), 1)
682+
self.assertEqual(len(block.logic_subset('~')), 1)
679683
sim = pyrtl.Simulation()
680684
sim.step_multiple({
681685
'a': '0011',

0 commit comments

Comments
 (0)