functional backend: make Memory in the C++ simulation library read-only again

This commit is contained in:
Emily Schmidt 2024-07-24 18:50:03 +01:00
parent 99effb6789
commit 95d28c22a2
2 changed files with 17 additions and 5 deletions

View File

@ -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;
@ -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");

View File

@ -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>()];
@ -410,9 +412,6 @@ public:
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