Skip to content

Commit

Permalink
cxxrtl: store comb $print cell last EN/ARGS in module
Browse files Browse the repository at this point in the history
statics were obviously wrong -- may be multiple instantiations of any
given module.  Extend test to cover this.
  • Loading branch information
charlottia authored and wanda-phi committed Aug 11, 2023
1 parent 843ad93 commit 4ffdee6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
35 changes: 13 additions & 22 deletions backends/cxxrtl/cxxrtl_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1219,12 +1219,13 @@ struct CxxrtlWorker {
log_assert(!for_debug);

auto trg_enable = cell->getParam(ID::TRG_ENABLE).as_bool();
static int cell_counter = 0;

if (!trg_enable) {
++cell_counter;
f << indent << "static bool last_print_" << cell_counter << "_known = false;\n";
f << indent << "static value<1> last_print_" << cell_counter << "_en;\n";
f << indent << "static value<" << cell->getPort(ID::ARGS).size() << "> last_print_" << cell_counter << "_args;\n";
f << indent << "auto " << mangle(cell) << "_curr = ";
dump_sigspec_rhs(cell->getPort(ID::EN));
f << ".concat(";
dump_sigspec_rhs(cell->getPort(ID::ARGS));
f << ").val();\n";
}

f << indent << "if (";
Expand All @@ -1246,16 +1247,7 @@ struct CxxrtlWorker {
}
f << ") && ";
} else {
f << '(';
f << "!last_print_" << cell_counter << "_known || ";
f << '(';
f << "last_print_" << cell_counter << "_en != ";
dump_sigspec_rhs(cell->getPort(ID::EN));

f << " || last_print_" << cell_counter << "_args != ";
dump_sigspec_rhs(cell->getPort(ID::ARGS));
f << ')';
f << ") && ";
f << mangle(cell) << " != " << mangle(cell) << "_curr && ";
}
dump_sigspec_rhs(cell->getPort(ID::EN));
f << " == value<1>{1u}) {\n";
Expand All @@ -1265,13 +1257,7 @@ struct CxxrtlWorker {
f << indent << "}\n";

if (!trg_enable) {
f << indent << "last_print_" << cell_counter << "_known = true;\n";
f << indent << "last_print_" << cell_counter << "_en = ";
dump_sigspec_rhs(cell->getPort(ID::EN));
f << ";\n";
f << indent << "last_print_" << cell_counter << "_args = ";
dump_sigspec_rhs(cell->getPort(ID::ARGS));
f << ";\n";
f << indent << mangle(cell) << " = " << mangle(cell) << "_curr;\n";
}
// Flip-flops
} else if (is_ff_cell(cell->type)) {
Expand Down Expand Up @@ -2362,6 +2348,11 @@ struct CxxrtlWorker {
f << "\n";
bool has_cells = false;
for (auto cell : module->cells()) {
if (cell->type == ID($print) && !cell->getParam(ID::TRG_ENABLE).as_bool()) {
// comb $print cell -- store the last EN/ARGS values to know when they change.
dump_attrs(cell);
f << indent << "value<" << (1 + cell->getParam(ID::ARGS_WIDTH).as_int()) << "> " << mangle(cell) << ";\n";
}
if (is_internal_cell(cell->type))
continue;
dump_attrs(cell);
Expand Down
9 changes: 6 additions & 3 deletions tests/fmt/always_comb_tb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

int main()
{
cxxrtl_design::p_top uut;
cxxrtl_design::p_top uut1, uut2;

for (int i = 0; i < 20; ++i) {
uut.p_clk.set(!uut.p_clk);
uut.step();
uut1.p_clk.set(!uut1.p_clk);
uut1.step();

uut2.p_clk.set(!uut2.p_clk);
uut2.step();
}

return 0;
Expand Down
3 changes: 2 additions & 1 deletion tests/fmt/always_comb_tb.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module tb;
reg clk = 0;

top uut (.clk(clk));
top uut1 (.clk(clk));
top uut2 (.clk(clk));

always #1 clk <= ~clk;
initial #20 $finish;
Expand Down

0 comments on commit 4ffdee6

Please sign in to comment.