Skip to content

Commit

Permalink
functional backend: make Memory in the C++ simulation library read-on…
Browse files Browse the repository at this point in the history
…ly again
  • Loading branch information
aiju committed Jul 24, 2024
1 parent a468cf3 commit 547b3e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 15 additions & 2 deletions backends/functional/cxx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ template<class NodePrinter> struct CxxPrintVisitor : public FunctionalIR::Abstra
void memory_write(Node, Node mem, Node addr, Node data) override { print("{}.write({}, {})", mem, addr, data); }
};

bool equal_def(RTLIL::Const const &a, RTLIL::Const const &b) {
if(a.size() != b.size()) return false;
for(int i = 0; i < a.size(); i++)
if((a[i] == State::S1) != (b[i] == State::S1))
return false;
return true;
}

struct CxxModule {
FunctionalIR ir;
CxxStruct input_struct, output_struct, state_struct;
Expand Down Expand Up @@ -193,11 +201,16 @@ struct CxxModule {
if (sort.is_signal())
f.print("\tstate.{} = {};\n", state_struct[name], cxx_const(ir.get_initial_state_signal(name)));
else if (sort.is_memory()) {
f.print("\t{{\n");
f.print("\t\tstd::array<Signal<{}>, {}> mem;\n", sort.data_width(), 1<<sort.addr_width());
const auto &contents = ir.get_initial_state_memory(name);
f.print("\tstate.{}.fill({});\n", state_struct[name], cxx_const(contents.default_value()));
f.print("\t\tmem.fill({});\n", cxx_const(contents.default_value()));
for(auto range : contents)
for(auto addr = range.base(); addr < range.limit(); addr++)
f.print("\tstate.{}[{}] = {};\n", state_struct[name], addr, cxx_const(range[addr]));
if(!equal_def(range[addr], contents.default_value()))
f.print("\t\tmem[{}] = {};\n", addr, cxx_const(range[addr]));
f.print("\t\tstate.{} = mem;\n", state_struct[name]);
f.print("\t}}\n");
}
}
f.print("}}\n\n");
Expand Down
5 changes: 2 additions & 3 deletions backends/functional/cxx_runtime/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ template<size_t a, size_t d>
class Memory {
std::array<Signal<d>, 1<<a> _contents;
public:
Memory() {}
Memory(std::array<Signal<d>, 1<<a> const &contents) : _contents(contents) {}
Signal<d> read(Signal<a> addr) const
{
return _contents[addr.template as_numeric<size_t>()];
Expand All @@ -410,9 +412,6 @@ class Memory {
ret._contents[addr.template as_numeric<size_t>()] = data;
return ret;
}
// mutating methods for initializing a state
void fill(Signal<d> data) { _contents.fill(data); }
Signal<d> &operator[](Signal<a> addr) { return _contents[addr.template as_numeric<size_t>()]; }
};

#endif

0 comments on commit 547b3e4

Please sign in to comment.