2014-01-16 17:05:02 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2014-01-16 17:05:02 -06:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2014-01-16 17:05:02 -06:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void handle_memory(RTLIL::Module *module, RTLIL::Cell *memory)
|
2014-01-16 17:05:02 -06:00
|
|
|
{
|
|
|
|
log("Creating $memrd and $memwr for memory `%s' in module `%s':\n",
|
|
|
|
memory->name.c_str(), module->name.c_str());
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::IdString mem_name = RTLIL::escape_id(memory->parameters.at(ID::MEMID).decode_string());
|
2014-01-16 17:05:02 -06:00
|
|
|
|
2014-01-16 17:15:15 -06:00
|
|
|
while (module->memories.count(mem_name) != 0)
|
2014-08-02 06:11:01 -05:00
|
|
|
mem_name = mem_name.str() + stringf("_%d", autoidx++);
|
2014-01-16 17:05:02 -06:00
|
|
|
|
|
|
|
RTLIL::Memory *mem = new RTLIL::Memory;
|
|
|
|
mem->name = mem_name;
|
2020-04-02 11:51:32 -05:00
|
|
|
mem->width = memory->parameters.at(ID::WIDTH).as_int();
|
|
|
|
mem->start_offset = memory->parameters.at(ID::OFFSET).as_int();
|
|
|
|
mem->size = memory->parameters.at(ID::SIZE).as_int();
|
2014-01-16 17:05:02 -06:00
|
|
|
module->memories[mem_name] = mem;
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
int abits = memory->parameters.at(ID::ABITS).as_int();
|
|
|
|
int num_rd_ports = memory->parameters.at(ID::RD_PORTS).as_int();
|
|
|
|
int num_wr_ports = memory->parameters.at(ID::WR_PORTS).as_int();
|
2014-01-16 17:05:02 -06:00
|
|
|
|
|
|
|
for (int i = 0; i < num_rd_ports; i++)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::Cell *cell = module->addCell(NEW_ID, ID($memrd));
|
|
|
|
cell->parameters[ID::MEMID] = mem_name.str();
|
|
|
|
cell->parameters[ID::ABITS] = memory->parameters.at(ID::ABITS);
|
|
|
|
cell->parameters[ID::WIDTH] = memory->parameters.at(ID::WIDTH);
|
|
|
|
cell->parameters[ID::CLK_ENABLE] = RTLIL::SigSpec(memory->parameters.at(ID::RD_CLK_ENABLE)).extract(i, 1).as_const();
|
|
|
|
cell->parameters[ID::CLK_POLARITY] = RTLIL::SigSpec(memory->parameters.at(ID::RD_CLK_POLARITY)).extract(i, 1).as_const();
|
|
|
|
cell->parameters[ID::TRANSPARENT] = RTLIL::SigSpec(memory->parameters.at(ID::RD_TRANSPARENT)).extract(i, 1).as_const();
|
|
|
|
cell->setPort(ID::CLK, memory->getPort(ID::RD_CLK).extract(i, 1));
|
|
|
|
cell->setPort(ID::EN, memory->getPort(ID::RD_EN).extract(i, 1));
|
|
|
|
cell->setPort(ID::ADDR, memory->getPort(ID::RD_ADDR).extract(i*abits, abits));
|
|
|
|
cell->setPort(ID::DATA, memory->getPort(ID::RD_DATA).extract(i*mem->width, mem->width));
|
2014-01-16 17:05:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < num_wr_ports; i++)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::Cell *cell = module->addCell(NEW_ID, ID($memwr));
|
|
|
|
cell->parameters[ID::MEMID] = mem_name.str();
|
|
|
|
cell->parameters[ID::ABITS] = memory->parameters.at(ID::ABITS);
|
|
|
|
cell->parameters[ID::WIDTH] = memory->parameters.at(ID::WIDTH);
|
|
|
|
cell->parameters[ID::CLK_ENABLE] = RTLIL::SigSpec(memory->parameters.at(ID::WR_CLK_ENABLE)).extract(i, 1).as_const();
|
|
|
|
cell->parameters[ID::CLK_POLARITY] = RTLIL::SigSpec(memory->parameters.at(ID::WR_CLK_POLARITY)).extract(i, 1).as_const();
|
|
|
|
cell->parameters[ID::PRIORITY] = i;
|
|
|
|
cell->setPort(ID::CLK, memory->getPort(ID::WR_CLK).extract(i, 1));
|
|
|
|
cell->setPort(ID::EN, memory->getPort(ID::WR_EN).extract(i*mem->width, mem->width));
|
|
|
|
cell->setPort(ID::ADDR, memory->getPort(ID::WR_ADDR).extract(i*abits, abits));
|
|
|
|
cell->setPort(ID::DATA, memory->getPort(ID::WR_DATA).extract(i*mem->width, mem->width));
|
2014-01-16 17:05:02 -06:00
|
|
|
}
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
Const initval = memory->parameters.at(ID::INIT);
|
2015-07-31 03:40:09 -05:00
|
|
|
RTLIL::Cell *last_init_cell = nullptr;
|
|
|
|
SigSpec last_init_data;
|
2015-08-14 15:22:17 -05:00
|
|
|
int last_init_addr=0;
|
2015-07-31 03:40:09 -05:00
|
|
|
|
2015-04-29 12:55:32 -05:00
|
|
|
for (int i = 0; i < GetSize(initval) && i/mem->width < (1 << abits); i += mem->width) {
|
|
|
|
Const val = initval.extract(i, mem->width, State::Sx);
|
|
|
|
for (auto bit : val.bits)
|
|
|
|
if (bit != State::Sx)
|
|
|
|
goto found_non_undef_initval;
|
|
|
|
continue;
|
|
|
|
found_non_undef_initval:
|
2015-07-31 03:40:09 -05:00
|
|
|
if (last_init_cell && last_init_addr+1 == i/mem->width) {
|
2020-04-02 11:51:32 -05:00
|
|
|
last_init_cell->parameters[ID::WORDS] = last_init_cell->parameters[ID::WORDS].as_int() + 1;
|
2015-07-31 03:40:09 -05:00
|
|
|
last_init_data.append(val);
|
|
|
|
last_init_addr++;
|
|
|
|
} else {
|
|
|
|
if (last_init_cell)
|
2020-04-02 11:51:32 -05:00
|
|
|
last_init_cell->setPort(ID::DATA, last_init_data);
|
|
|
|
RTLIL::Cell *cell = module->addCell(NEW_ID, ID($meminit));
|
|
|
|
cell->parameters[ID::MEMID] = mem_name.str();
|
|
|
|
cell->parameters[ID::ABITS] = memory->parameters.at(ID::ABITS);
|
|
|
|
cell->parameters[ID::WIDTH] = memory->parameters.at(ID::WIDTH);
|
|
|
|
cell->parameters[ID::WORDS] = 1;
|
|
|
|
cell->parameters[ID::PRIORITY] = i/mem->width;
|
|
|
|
cell->setPort(ID::ADDR, SigSpec(i/mem->width, abits));
|
2015-07-31 03:40:09 -05:00
|
|
|
last_init_cell = cell;
|
|
|
|
last_init_addr = i/mem->width;
|
|
|
|
last_init_data = val;
|
|
|
|
}
|
2015-04-29 12:55:32 -05:00
|
|
|
}
|
|
|
|
|
2015-07-31 03:40:09 -05:00
|
|
|
if (last_init_cell)
|
2020-04-02 11:51:32 -05:00
|
|
|
last_init_cell->setPort(ID::DATA, last_init_data);
|
2015-07-31 03:40:09 -05:00
|
|
|
|
2014-07-25 08:05:18 -05:00
|
|
|
module->remove(memory);
|
2014-01-16 17:05:02 -06:00
|
|
|
}
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
void handle_module(RTLIL::Design *design, RTLIL::Module *module)
|
2014-01-16 17:05:02 -06:00
|
|
|
{
|
|
|
|
std::vector<RTLIL::IdString> memcells;
|
2020-04-09 00:38:36 -05:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
if (cell->type == ID($mem) && design->selected(module, cell))
|
|
|
|
memcells.push_back(cell->name);
|
2014-01-16 17:05:02 -06:00
|
|
|
for (auto &it : memcells)
|
2020-04-09 00:38:36 -05:00
|
|
|
handle_memory(module, module->cell(it));
|
2014-01-16 17:05:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MemoryUnpackPass : public Pass {
|
|
|
|
MemoryUnpackPass() : Pass("memory_unpack", "unpack multi-port memory cells") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2014-01-16 17:05:02 -06:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" memory_unpack [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("This pass converts the multi-port $mem memory cells into individual $memrd and\n");
|
|
|
|
log("$memwr cells. It is the counterpart to the memory_collect pass.\n");
|
|
|
|
log("\n");
|
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing MEMORY_UNPACK pass (generating $memrd/$memwr cells form $mem cells).\n");
|
2014-01-16 17:05:02 -06:00
|
|
|
extra_args(args, 1, design);
|
2020-04-09 00:38:36 -05:00
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
handle_module(design, module);
|
2014-01-16 17:05:02 -06:00
|
|
|
}
|
|
|
|
} MemoryUnpackPass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|