Skip to content

Commit 711ac42

Browse files
committed
Verilog: fix assignment for module output ports
This flips the assignments generated by synthesis for module output ports to read c := port for a connected signal c, instead of port := c.
1 parent ada2100 commit 711ac42

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/verilog/verilog_synthesis.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ Function: verilog_synthesist::instantiate_port
14701470
\*******************************************************************/
14711471

14721472
void verilog_synthesist::instantiate_port(
1473+
bool is_output,
14731474
const symbol_exprt &port,
14741475
const exprt &value,
14751476
const replace_mapt &replace_map,
@@ -1486,10 +1487,24 @@ void verilog_synthesist::instantiate_port(
14861487
<< "failed to find port symbol " << port_identifier << " in replace_map";
14871488
}
14881489

1489-
// Much like always @(*) port = value.
1490+
// Much like
1491+
// always @(*) port = value for an input, and
1492+
// always @(*) value = port for an output.
14901493
// Note that the types need not match.
1491-
verilog_forcet assignment(
1492-
it->second, typecast_exprt::conditional_cast(value, it->second.type()));
1494+
exprt lhs, rhs;
1495+
1496+
if(is_output)
1497+
{
1498+
lhs = value;
1499+
rhs = typecast_exprt::conditional_cast(it->second, value.type());
1500+
}
1501+
else
1502+
{
1503+
lhs = it->second;
1504+
rhs = typecast_exprt::conditional_cast(value, it->second.type());
1505+
}
1506+
1507+
verilog_forcet assignment{lhs, rhs};
14931508

14941509
assignment.add_source_location() = source_location;
14951510

@@ -1520,15 +1535,21 @@ void verilog_synthesist::instantiate_ports(
15201535
const replace_mapt &replace_map,
15211536
transt &trans)
15221537
{
1523-
const irept::subt &ports=symbol.type.find(ID_ports).get_sub();
1524-
15251538
if(inst.operands().size()==0)
15261539
return;
15271540

15281541
// named port connection?
15291542

15301543
if(to_multi_ary_expr(inst).op0().id() == ID_named_port_connection)
15311544
{
1545+
const irept::subt &ports = symbol.type.find(ID_ports).get_sub();
1546+
1547+
std::set<irep_idt> output_identifiers;
1548+
for(auto &port : ports)
1549+
if(port.get_bool(ID_output))
1550+
output_identifiers.insert(
1551+
to_symbol_expr((const exprt &)(port)).get_identifier());
1552+
15321553
// no requirement that all ports are connected
15331554
for(const auto &o_it : inst.operands())
15341555
{
@@ -1538,13 +1559,19 @@ void verilog_synthesist::instantiate_ports(
15381559
const exprt &op1 = to_binary_expr(o_it).op1();
15391560

15401561
if(op1.is_not_nil())
1562+
{
1563+
bool is_output = output_identifiers.find(op0.get_identifier()) !=
1564+
output_identifiers.end();
15411565
instantiate_port(
1542-
op0, op1, replace_map, inst.source_location(), trans);
1566+
is_output, op0, op1, replace_map, inst.source_location(), trans);
1567+
}
15431568
}
15441569
}
15451570
}
15461571
else // just a list without names
15471572
{
1573+
const irept::subt &ports = symbol.type.find(ID_ports).get_sub();
1574+
15481575
if(inst.operands().size()!=ports.size())
15491576
{
15501577
throw errort().with_location(inst.source_location())
@@ -1561,7 +1588,10 @@ void verilog_synthesist::instantiate_ports(
15611588

15621589
auto &port = to_symbol_expr((const exprt &)(*p_it));
15631590

1564-
instantiate_port(port, o_it, replace_map, inst.source_location(), trans);
1591+
bool is_output = port.get_bool(ID_output);
1592+
1593+
instantiate_port(
1594+
is_output, port, o_it, replace_map, inst.source_location(), trans);
15651595
p_it++;
15661596
}
15671597
}

src/verilog/verilog_synthesis_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ class verilog_synthesist:
320320
void replace_symbols(const irep_idt &target, exprt &dest);
321321

322322
void instantiate_port(
323+
bool is_output,
323324
const symbol_exprt &port,
324325
const exprt &value,
325326
const replace_mapt &,

0 commit comments

Comments
 (0)