if (!port.clk_enable) {
if (port.addr.is_fully_const() && port.en.is_fully_ones()) {
for (int sub = 0; sub < (1 << port.wide_log2); sub++)
- static_cells_map[port.addr.as_int() - mem.start_offset + sub] = port.data.extract(sub * mem.width, mem.width);
+ static_cells_map[port.addr.as_int() + sub] = port.data.extract(sub * mem.width, mem.width);
static_ports.insert(i);
continue;
}
log("Mapping memory %s in module %s:\n", mem.memid.c_str(), module->name.c_str());
- std::vector<RTLIL::SigSpec> data_reg_in;
- std::vector<RTLIL::SigSpec> data_reg_out;
+ int abits = ceil_log2(mem.size);
+ std::vector<RTLIL::SigSpec> data_reg_in(1 << abits);
+ std::vector<RTLIL::SigSpec> data_reg_out(1 << abits);
int count_static = 0;
for (int i = 0; i < mem.size; i++)
{
- if (static_cells_map.count(i) > 0)
+ int addr = i + mem.start_offset;
+ int idx = addr & ((1 << abits) - 1);
+ if (static_cells_map.count(addr) > 0)
{
- data_reg_in.push_back(RTLIL::SigSpec(RTLIL::State::Sz, mem.width));
- data_reg_out.push_back(static_cells_map[i]);
+ data_reg_out[idx] = static_cells_map[addr];
count_static++;
}
else
{
- RTLIL::Cell *c = module->addCell(genid(mem.memid, "", i), ID($dff));
+ RTLIL::Cell *c = module->addCell(genid(mem.memid, "", addr), ID($dff));
c->parameters[ID::WIDTH] = mem.width;
if (GetSize(refclock) != 0) {
c->parameters[ID::CLK_POLARITY] = RTLIL::Const(refclock_pol);
c->setPort(ID::CLK, RTLIL::SigSpec(RTLIL::State::S0));
}
- RTLIL::Wire *w_in = module->addWire(genid(mem.memid, "", i, "$d"), mem.width);
- data_reg_in.push_back(RTLIL::SigSpec(w_in));
- c->setPort(ID::D, data_reg_in.back());
+ RTLIL::Wire *w_in = module->addWire(genid(mem.memid, "", addr, "$d"), mem.width);
+ data_reg_in[idx] = w_in;
+ c->setPort(ID::D, w_in);
- std::string w_out_name = stringf("%s[%d]", mem.memid.c_str(), i);
+ std::string w_out_name = stringf("%s[%d]", mem.memid.c_str(), addr);
if (module->wires_.count(w_out_name) > 0)
- w_out_name = genid(mem.memid, "", i, "$q");
+ w_out_name = genid(mem.memid, "", addr, "$q");
RTLIL::Wire *w_out = module->addWire(w_out_name, mem.width);
SigSpec w_init = init_data.extract(i*mem.width, mem.width);
if (!w_init.is_fully_undef())
w_out->attributes[ID::init] = w_init.as_const();
- data_reg_out.push_back(RTLIL::SigSpec(w_out));
- c->setPort(ID::Q, data_reg_out.back());
+ data_reg_out[idx] = w_out;
+ c->setPort(ID::Q, w_out);
}
}
int count_dff = 0, count_mux = 0, count_wrmux = 0;
- int abits = ceil_log2(mem.size);
for (int i = 0; i < GetSize(mem.rd_ports); i++)
{
auto &port = mem.rd_ports[i];
RTLIL::SigSpec rd_addr = port.addr;
rd_addr.extend_u0(abits, false);
- if (mem.start_offset)
- rd_addr = module->Sub(NEW_ID, rd_addr, SigSpec(mem.start_offset, abits));
-
std::vector<RTLIL::SigSpec> rd_signals;
rd_signals.push_back(port.data);
next_rd_signals.swap(rd_signals);
}
- for (int j = 0; j < mem.size; j++)
- module->connect(RTLIL::SigSig(rd_signals[j >> port.wide_log2].extract((j & ((1 << port.wide_log2) - 1)) * mem.width, mem.width), data_reg_out[j]));
+ for (int j = 0; j < (1 << abits); j++)
+ if (data_reg_out[j] != SigSpec())
+ module->connect(RTLIL::SigSig(rd_signals[j >> port.wide_log2].extract((j & ((1 << port.wide_log2) - 1)) * mem.width, mem.width), data_reg_out[j]));
}
log(" read interface: %d $dff and %d $mux cells.\n", count_dff, count_mux);
for (int i = 0; i < mem.size; i++)
{
- if (static_cells_map.count(i) > 0)
+ int addr = i + mem.start_offset;
+ int idx = addr & ((1 << abits) - 1);
+ if (static_cells_map.count(addr) > 0)
continue;
- RTLIL::SigSpec sig = data_reg_out[i];
+ RTLIL::SigSpec sig = data_reg_out[idx];
for (int j = 0; j < GetSize(mem.wr_ports); j++)
{
auto &port = mem.wr_ports[j];
- RTLIL::SigSpec wr_addr = port.addr;
-
- if (mem.start_offset)
- wr_addr = module->Sub(NEW_ID, wr_addr, SigSpec(mem.start_offset, GetSize(wr_addr)));
-
- wr_addr = wr_addr.extract_end(port.wide_log2);
- RTLIL::Wire *w_seladdr = addr_decode(wr_addr, RTLIL::SigSpec(i >> port.wide_log2, GetSize(wr_addr)));
+ RTLIL::SigSpec wr_addr = port.addr.extract_end(port.wide_log2);
+ RTLIL::Wire *w_seladdr = addr_decode(wr_addr, RTLIL::SigSpec(addr >> port.wide_log2, GetSize(wr_addr)));
- int sub = i & ((1 << port.wide_log2) - 1);
+ int sub = addr & ((1 << port.wide_log2) - 1);
int wr_offset = 0;
while (wr_offset < mem.width)
if (wr_bit != State::S1)
{
- RTLIL::Cell *c = module->addCell(genid(mem.memid, "$wren", i, "", j, "", wr_offset), ID($and));
+ RTLIL::Cell *c = module->addCell(genid(mem.memid, "$wren", addr, "", j, "", wr_offset), ID($and));
c->parameters[ID::A_SIGNED] = RTLIL::Const(0);
c->parameters[ID::B_SIGNED] = RTLIL::Const(0);
c->parameters[ID::A_WIDTH] = RTLIL::Const(1);
c->setPort(ID::A, w);
c->setPort(ID::B, wr_bit);
- w = module->addWire(genid(mem.memid, "$wren", i, "", j, "", wr_offset, "$y"));
+ w = module->addWire(genid(mem.memid, "$wren", addr, "", j, "", wr_offset, "$y"));
c->setPort(ID::Y, RTLIL::SigSpec(w));
}
- RTLIL::Cell *c = module->addCell(genid(mem.memid, "$wrmux", i, "", j, "", wr_offset), ID($mux));
+ RTLIL::Cell *c = module->addCell(genid(mem.memid, "$wrmux", addr, "", j, "", wr_offset), ID($mux));
c->parameters[ID::WIDTH] = wr_width;
c->setPort(ID::A, sig.extract(wr_offset, wr_width));
c->setPort(ID::B, port.data.extract(wr_offset + sub * mem.width, wr_width));
c->setPort(ID::S, RTLIL::SigSpec(w));
- w = module->addWire(genid(mem.memid, "$wrmux", i, "", j, "", wr_offset, "$y"), wr_width);
+ w = module->addWire(genid(mem.memid, "$wrmux", addr, "", j, "", wr_offset, "$y"), wr_width);
c->setPort(ID::Y, w);
sig.replace(wr_offset, w);
}
}
- module->connect(RTLIL::SigSig(data_reg_in[i], sig));
+ module->connect(RTLIL::SigSig(data_reg_in[idx], sig));
}
log(" write interface: %d write mux blocks.\n", count_wrmux);