2013-07-23 09:19:34 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-07-23 09:19:34 -05: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
|
|
|
*
|
2013-07-23 09:19:34 -05: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 "blifparse.h"
|
2014-09-27 09:17:53 -05:00
|
|
|
|
|
|
|
YOSYS_NAMESPACE_BEGIN
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2015-05-17 08:10:37 -05:00
|
|
|
static bool read_next_line(char *&buffer, size_t &buffer_size, int &line_count, std::istream &f)
|
2013-11-13 08:46:28 -06:00
|
|
|
{
|
2016-10-19 05:44:58 -05:00
|
|
|
string strbuf;
|
2013-12-31 14:58:35 -06:00
|
|
|
int buffer_len = 0;
|
2013-11-13 08:46:28 -06:00
|
|
|
buffer[0] = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-12-31 14:58:35 -06:00
|
|
|
buffer_len += strlen(buffer + buffer_len);
|
2013-11-13 08:46:28 -06:00
|
|
|
while (buffer_len > 0 && (buffer[buffer_len-1] == ' ' || buffer[buffer_len-1] == '\t' ||
|
|
|
|
buffer[buffer_len-1] == '\r' || buffer[buffer_len-1] == '\n'))
|
|
|
|
buffer[--buffer_len] = 0;
|
|
|
|
|
2013-12-31 14:58:35 -06:00
|
|
|
if (buffer_size-buffer_len < 4096) {
|
|
|
|
buffer_size *= 2;
|
|
|
|
buffer = (char*)realloc(buffer, buffer_size);
|
|
|
|
}
|
|
|
|
|
2013-11-13 08:46:28 -06:00
|
|
|
if (buffer_len == 0 || buffer[buffer_len-1] == '\\') {
|
2014-03-11 07:09:01 -05:00
|
|
|
if (buffer_len > 0 && buffer[buffer_len-1] == '\\')
|
2013-11-13 08:46:28 -06:00
|
|
|
buffer[--buffer_len] = 0;
|
|
|
|
line_count++;
|
2016-10-19 05:44:58 -05:00
|
|
|
if (!std::getline(f, strbuf))
|
2013-11-13 08:46:28 -06:00
|
|
|
return false;
|
2016-10-19 05:44:58 -05:00
|
|
|
while (buffer_size-buffer_len < strbuf.size()+1) {
|
|
|
|
buffer_size *= 2;
|
|
|
|
buffer = (char*)realloc(buffer, buffer_size);
|
|
|
|
}
|
|
|
|
strcpy(buffer+buffer_len, strbuf.c_str());
|
2013-11-13 08:46:28 -06:00
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:48:03 -06:00
|
|
|
static std::pair<RTLIL::IdString, int> wideports_split(std::string name)
|
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
|
|
|
|
if (name.empty() || name.back() != ']')
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
for (int i = 0; i+1 < GetSize(name); i++) {
|
|
|
|
if (name[i] == '[')
|
|
|
|
pos = i;
|
2021-05-05 13:31:27 -05:00
|
|
|
else if (name[i] != '-' && (name[i] < '0' || name[i] > '9'))
|
|
|
|
pos = -1;
|
|
|
|
else if (name[i] == '-' && ((i != pos+1) || name[i+1] == ']'))
|
|
|
|
pos = -1;
|
|
|
|
else if (i == pos+2 && name[i] == '0' && name[i-1] == '-')
|
2017-02-06 07:48:03 -06:00
|
|
|
pos = -1;
|
2017-07-21 09:21:04 -05:00
|
|
|
else if (i == pos+1 && name[i] == '0' && name[i+1] != ']')
|
2017-02-06 07:48:03 -06:00
|
|
|
pos = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos >= 0)
|
2021-05-05 13:31:27 -05:00
|
|
|
return std::pair<RTLIL::IdString, int>("\\" + name.substr(0, pos), atoi(name.c_str() + pos+1));
|
2017-02-06 07:48:03 -06:00
|
|
|
|
|
|
|
failed:
|
2021-05-05 13:31:27 -05:00
|
|
|
return std::pair<RTLIL::IdString, int>(RTLIL::IdString(), 0);
|
2017-02-06 07:48:03 -06:00
|
|
|
}
|
|
|
|
|
2019-08-15 12:26:24 -05:00
|
|
|
void parse_blif(RTLIL::Design *design, std::istream &f, IdString dff_name, bool run_clean, bool sop_mode, bool wideports)
|
2013-07-23 09:19:34 -05:00
|
|
|
{
|
2015-05-17 08:10:37 -05:00
|
|
|
RTLIL::Module *module = nullptr;
|
2013-07-23 09:19:34 -05:00
|
|
|
RTLIL::Const *lutptr = NULL;
|
2016-06-17 06:46:01 -05:00
|
|
|
RTLIL::Cell *sopcell = NULL;
|
2018-08-02 16:33:39 -05:00
|
|
|
RTLIL::Cell *lastcell = nullptr;
|
2013-07-23 09:19:34 -05:00
|
|
|
RTLIL::State lut_default_state = RTLIL::State::Sx;
|
2018-08-08 12:41:47 -05:00
|
|
|
std::string err_reason;
|
2016-06-17 06:46:01 -05:00
|
|
|
int blif_maxnum = 0, sopmode = -1;
|
2015-12-20 06:12:24 -06:00
|
|
|
|
|
|
|
auto blif_wire = [&](const std::string &wire_name) -> Wire*
|
|
|
|
{
|
|
|
|
if (wire_name[0] == '$')
|
|
|
|
{
|
|
|
|
for (int i = 0; i+1 < GetSize(wire_name); i++)
|
|
|
|
{
|
|
|
|
if (wire_name[i] != '$')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int len = 0;
|
|
|
|
while (i+len+1 < GetSize(wire_name) && '0' <= wire_name[i+len+1] && wire_name[i+len+1] <= '9')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
string num_str = wire_name.substr(i+1, len);
|
|
|
|
int num = atoi(num_str.c_str()) & 0x0fffffff;
|
|
|
|
blif_maxnum = std::max(blif_maxnum, num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IdString wire_id = RTLIL::escape_id(wire_name);
|
|
|
|
Wire *wire = module->wire(wire_id);
|
|
|
|
|
|
|
|
if (wire == nullptr)
|
|
|
|
wire = module->addWire(wire_id);
|
|
|
|
|
|
|
|
return wire;
|
|
|
|
};
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2015-05-24 01:03:21 -05:00
|
|
|
dict<RTLIL::IdString, RTLIL::Const> *obj_attributes = nullptr;
|
|
|
|
dict<RTLIL::IdString, RTLIL::Const> *obj_parameters = nullptr;
|
|
|
|
|
2017-02-06 07:48:03 -06:00
|
|
|
dict<RTLIL::IdString, std::pair<int, bool>> wideports_cache;
|
|
|
|
|
2013-12-31 14:58:35 -06:00
|
|
|
size_t buffer_size = 4096;
|
|
|
|
char *buffer = (char*)malloc(buffer_size);
|
2013-07-23 09:19:34 -05:00
|
|
|
int line_count = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2015-05-17 08:10:37 -05:00
|
|
|
if (!read_next_line(buffer, buffer_size, line_count, f)) {
|
|
|
|
if (module != nullptr)
|
|
|
|
goto error;
|
|
|
|
free(buffer);
|
|
|
|
return;
|
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2013-11-13 08:46:28 -06:00
|
|
|
continue_without_read:
|
2013-07-23 09:19:34 -05:00
|
|
|
if (buffer[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (buffer[0] == '.')
|
|
|
|
{
|
|
|
|
if (lutptr) {
|
|
|
|
for (auto &bit : lutptr->bits)
|
|
|
|
if (bit == RTLIL::State::Sx)
|
|
|
|
bit = lut_default_state;
|
|
|
|
lutptr = NULL;
|
|
|
|
lut_default_state = RTLIL::State::Sx;
|
|
|
|
}
|
|
|
|
|
2016-06-17 06:46:01 -05:00
|
|
|
if (sopcell) {
|
|
|
|
sopcell = NULL;
|
|
|
|
sopmode = -1;
|
|
|
|
}
|
|
|
|
|
2013-07-23 09:19:34 -05:00
|
|
|
char *cmd = strtok(buffer, " \t\r\n");
|
|
|
|
|
2015-05-17 08:10:37 -05:00
|
|
|
if (!strcmp(cmd, ".model")) {
|
|
|
|
if (module != nullptr)
|
|
|
|
goto error;
|
|
|
|
module = new RTLIL::Module;
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = nullptr;
|
2015-05-17 08:10:37 -05:00
|
|
|
module->name = RTLIL::escape_id(strtok(NULL, " \t\r\n"));
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = &module->attributes;
|
|
|
|
obj_parameters = nullptr;
|
2015-05-17 08:10:37 -05:00
|
|
|
if (design->module(module->name))
|
|
|
|
log_error("Duplicate definition of module %s in line %d!\n", log_id(module->name), line_count);
|
|
|
|
design->add(module);
|
2013-07-23 09:19:34 -05:00
|
|
|
continue;
|
2015-05-17 08:10:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (module == nullptr)
|
|
|
|
goto error;
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2019-10-15 17:00:27 -05:00
|
|
|
if (!strcmp(cmd, ".blackbox"))
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
module->attributes[ID::blackbox] = RTLIL::Const(1);
|
2019-10-15 17:00:27 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-20 06:12:24 -06:00
|
|
|
if (!strcmp(cmd, ".end"))
|
|
|
|
{
|
2017-02-06 07:48:03 -06:00
|
|
|
for (auto &wp : wideports_cache)
|
|
|
|
{
|
|
|
|
auto name = wp.first;
|
|
|
|
int width = wp.second.first;
|
|
|
|
bool isinput = wp.second.second;
|
|
|
|
|
|
|
|
RTLIL::Wire *wire = module->addWire(name, width);
|
|
|
|
wire->port_input = isinput;
|
|
|
|
wire->port_output = !isinput;
|
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
RTLIL::IdString other_name = name.str() + stringf("[%d]", i);
|
|
|
|
RTLIL::Wire *other_wire = module->wire(other_name);
|
|
|
|
if (other_wire) {
|
|
|
|
other_wire->port_input = false;
|
|
|
|
other_wire->port_output = false;
|
|
|
|
if (isinput)
|
|
|
|
module->connect(other_wire, SigSpec(wire, i));
|
|
|
|
else
|
|
|
|
module->connect(SigSpec(wire, i), other_wire);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 09:13:42 -05:00
|
|
|
module->fixup_ports();
|
2017-02-06 07:48:03 -06:00
|
|
|
wideports_cache.clear();
|
2015-12-20 06:12:24 -06:00
|
|
|
|
|
|
|
if (run_clean)
|
|
|
|
{
|
|
|
|
Const buffer_lut(vector<RTLIL::State>({State::S0, State::S1}));
|
|
|
|
vector<Cell*> remove_cells;
|
|
|
|
|
|
|
|
for (auto cell : module->cells())
|
2020-04-02 11:51:32 -05:00
|
|
|
if (cell->type == ID($lut) && cell->getParam(ID::LUT) == buffer_lut) {
|
2020-03-12 14:57:01 -05:00
|
|
|
module->connect(cell->getPort(ID::Y), cell->getPort(ID::A));
|
2015-12-20 06:12:24 -06:00
|
|
|
remove_cells.push_back(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto cell : remove_cells)
|
|
|
|
module->remove(cell);
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
Wire *true_wire = module->wire(ID($true));
|
|
|
|
Wire *false_wire = module->wire(ID($false));
|
|
|
|
Wire *undef_wire = module->wire(ID($undef));
|
2015-12-20 06:12:24 -06:00
|
|
|
|
|
|
|
if (true_wire != nullptr)
|
|
|
|
module->rename(true_wire, stringf("$true$%d", ++blif_maxnum));
|
|
|
|
|
|
|
|
if (false_wire != nullptr)
|
|
|
|
module->rename(false_wire, stringf("$false$%d", ++blif_maxnum));
|
|
|
|
|
|
|
|
if (undef_wire != nullptr)
|
|
|
|
module->rename(undef_wire, stringf("$undef$%d", ++blif_maxnum));
|
|
|
|
|
|
|
|
autoidx = std::max(autoidx, blif_maxnum+1);
|
|
|
|
blif_maxnum = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-17 08:10:37 -05:00
|
|
|
module = nullptr;
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = nullptr;
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = nullptr;
|
|
|
|
obj_parameters = nullptr;
|
2015-05-17 08:10:37 -05:00
|
|
|
continue;
|
2013-12-31 14:58:35 -06:00
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2017-02-06 07:48:03 -06:00
|
|
|
if (!strcmp(cmd, ".inputs") || !strcmp(cmd, ".outputs"))
|
|
|
|
{
|
2013-07-23 09:19:34 -05:00
|
|
|
char *p;
|
2017-02-06 07:48:03 -06:00
|
|
|
while ((p = strtok(NULL, " \t\r\n")) != NULL)
|
|
|
|
{
|
2015-12-20 06:12:24 -06:00
|
|
|
RTLIL::IdString wire_name(stringf("\\%s", p));
|
|
|
|
RTLIL::Wire *wire = module->wire(wire_name);
|
|
|
|
if (wire == nullptr)
|
|
|
|
wire = module->addWire(wire_name);
|
2013-07-23 09:19:34 -05:00
|
|
|
if (!strcmp(cmd, ".inputs"))
|
|
|
|
wire->port_input = true;
|
|
|
|
else
|
|
|
|
wire->port_output = true;
|
2017-02-06 07:48:03 -06:00
|
|
|
|
|
|
|
if (wideports) {
|
|
|
|
std::pair<RTLIL::IdString, int> wp = wideports_split(p);
|
2021-05-05 13:31:27 -05:00
|
|
|
if (!wp.first.empty() && wp.second >= 0) {
|
|
|
|
wideports_cache[wp.first].first = std::max(wideports_cache[wp.first].first, wp.second + 1);
|
2017-02-06 07:48:03 -06:00
|
|
|
wideports_cache[wp.first].second = !strcmp(cmd, ".inputs");
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
}
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = nullptr;
|
|
|
|
obj_parameters = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:33:39 -05:00
|
|
|
if (!strcmp(cmd, ".cname"))
|
|
|
|
{
|
|
|
|
char *p = strtok(NULL, " \t\r\n");
|
|
|
|
if (p == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if(lastcell == nullptr || module == nullptr)
|
|
|
|
{
|
2018-12-07 13:14:07 -06:00
|
|
|
err_reason = stringf("No primitive object to attach .cname %s.", p);
|
2018-08-08 12:22:55 -05:00
|
|
|
goto error_with_reason;
|
2018-08-02 16:33:39 -05:00
|
|
|
}
|
|
|
|
|
2019-10-16 02:06:57 -05:00
|
|
|
module->rename(lastcell, RTLIL::escape_id(p));
|
2018-08-02 16:33:39 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:03:21 -05:00
|
|
|
if (!strcmp(cmd, ".attr") || !strcmp(cmd, ".param")) {
|
|
|
|
char *n = strtok(NULL, " \t\r\n");
|
|
|
|
char *v = strtok(NULL, "\r\n");
|
|
|
|
IdString id_n = RTLIL::escape_id(n);
|
|
|
|
Const const_v;
|
|
|
|
if (v[0] == '"') {
|
|
|
|
std::string str(v+1);
|
|
|
|
if (str.back() == '"')
|
2015-09-01 05:51:23 -05:00
|
|
|
str.resize(str.size()-1);
|
2015-05-24 01:03:21 -05:00
|
|
|
const_v = Const(str);
|
|
|
|
} else {
|
|
|
|
int n = strlen(v);
|
|
|
|
const_v.bits.resize(n);
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
const_v.bits[i] = v[n-i-1] != '0' ? State::S1 : State::S0;
|
|
|
|
}
|
|
|
|
if (!strcmp(cmd, ".attr")) {
|
2018-08-02 16:33:39 -05:00
|
|
|
if (obj_attributes == nullptr) {
|
2018-08-08 12:41:47 -05:00
|
|
|
err_reason = stringf("No object to attach .attr too.");
|
2018-08-08 12:22:55 -05:00
|
|
|
goto error_with_reason;
|
2018-08-02 16:33:39 -05:00
|
|
|
}
|
2015-05-24 01:03:21 -05:00
|
|
|
(*obj_attributes)[id_n] = const_v;
|
|
|
|
} else {
|
2018-08-02 16:33:39 -05:00
|
|
|
if (obj_parameters == nullptr) {
|
2018-08-08 12:41:47 -05:00
|
|
|
err_reason = stringf("No object to attach .param too.");
|
2018-08-08 12:22:55 -05:00
|
|
|
goto error_with_reason;
|
2018-08-02 16:33:39 -05:00
|
|
|
}
|
2015-05-24 01:03:21 -05:00
|
|
|
(*obj_parameters)[id_n] = const_v;
|
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-31 14:25:09 -06:00
|
|
|
if (!strcmp(cmd, ".latch"))
|
|
|
|
{
|
|
|
|
char *d = strtok(NULL, " \t\r\n");
|
|
|
|
char *q = strtok(NULL, " \t\r\n");
|
2015-05-17 11:58:24 -05:00
|
|
|
char *edge = strtok(NULL, " \t\r\n");
|
|
|
|
char *clock = strtok(NULL, " \t\r\n");
|
|
|
|
char *init = strtok(NULL, " \t\r\n");
|
2015-05-24 01:03:21 -05:00
|
|
|
RTLIL::Cell *cell = nullptr;
|
2013-12-31 14:25:09 -06:00
|
|
|
|
2015-05-17 11:58:24 -05:00
|
|
|
if (clock == nullptr && edge != nullptr) {
|
|
|
|
init = edge;
|
|
|
|
edge = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init != nullptr && (init[0] == '0' || init[0] == '1'))
|
2020-04-02 11:51:32 -05:00
|
|
|
blif_wire(q)->attributes[ID::init] = Const(init[0] == '1' ? 1 : 0, 1);
|
2015-05-17 11:58:24 -05:00
|
|
|
|
|
|
|
if (clock == nullptr)
|
|
|
|
goto no_latch_clock;
|
|
|
|
|
|
|
|
if (!strcmp(edge, "re"))
|
2015-12-20 06:12:24 -06:00
|
|
|
cell = module->addDff(NEW_ID, blif_wire(clock), blif_wire(d), blif_wire(q));
|
2015-05-17 11:58:24 -05:00
|
|
|
else if (!strcmp(edge, "fe"))
|
2015-12-20 06:12:24 -06:00
|
|
|
cell = module->addDff(NEW_ID, blif_wire(clock), blif_wire(d), blif_wire(q), false);
|
2016-04-22 11:02:55 -05:00
|
|
|
else if (!strcmp(edge, "ah"))
|
|
|
|
cell = module->addDlatch(NEW_ID, blif_wire(clock), blif_wire(d), blif_wire(q));
|
|
|
|
else if (!strcmp(edge, "al"))
|
|
|
|
cell = module->addDlatch(NEW_ID, blif_wire(clock), blif_wire(d), blif_wire(q), false);
|
2015-05-17 11:58:24 -05:00
|
|
|
else {
|
|
|
|
no_latch_clock:
|
2016-10-11 18:18:39 -05:00
|
|
|
if (dff_name.empty()) {
|
|
|
|
cell = module->addFf(NEW_ID, blif_wire(d), blif_wire(q));
|
|
|
|
} else {
|
|
|
|
cell = module->addCell(NEW_ID, dff_name);
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setPort(ID::D, blif_wire(d));
|
|
|
|
cell->setPort(ID::Q, blif_wire(q));
|
2016-10-11 18:18:39 -05:00
|
|
|
}
|
2015-05-17 11:58:24 -05:00
|
|
|
}
|
|
|
|
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = cell;
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = &cell->attributes;
|
|
|
|
obj_parameters = &cell->parameters;
|
2013-12-31 14:25:09 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-17 08:25:03 -05:00
|
|
|
if (!strcmp(cmd, ".gate") || !strcmp(cmd, ".subckt"))
|
2013-12-31 06:41:16 -06:00
|
|
|
{
|
|
|
|
char *p = strtok(NULL, " \t\r\n");
|
|
|
|
if (p == NULL)
|
|
|
|
goto error;
|
2014-07-25 08:05:18 -05:00
|
|
|
|
2014-12-29 08:17:40 -06:00
|
|
|
IdString celltype = RTLIL::escape_id(p);
|
|
|
|
RTLIL::Cell *cell = module->addCell(NEW_ID, celltype);
|
2021-05-05 13:31:27 -05:00
|
|
|
RTLIL::Module *cell_mod = design->module(celltype);
|
2013-12-31 06:41:16 -06:00
|
|
|
|
2017-07-21 09:21:04 -05:00
|
|
|
dict<RTLIL::IdString, dict<int, SigBit>> cell_wideports_cache;
|
|
|
|
|
|
|
|
while ((p = strtok(NULL, " \t\r\n")) != NULL)
|
|
|
|
{
|
2013-12-31 06:41:16 -06:00
|
|
|
char *q = strchr(p, '=');
|
2016-02-24 02:16:43 -06:00
|
|
|
if (q == NULL || !q[0])
|
2013-12-31 06:41:16 -06:00
|
|
|
goto error;
|
|
|
|
*(q++) = 0;
|
2017-07-21 09:21:04 -05:00
|
|
|
|
|
|
|
if (wideports) {
|
|
|
|
std::pair<RTLIL::IdString, int> wp = wideports_split(p);
|
2021-05-05 13:31:27 -05:00
|
|
|
if (wp.first.empty())
|
2017-07-21 09:21:04 -05:00
|
|
|
cell->setPort(RTLIL::escape_id(p), *q ? blif_wire(q) : SigSpec());
|
2021-05-05 13:31:27 -05:00
|
|
|
else
|
|
|
|
cell_wideports_cache[wp.first][wp.second] = blif_wire(q);
|
2017-07-21 09:21:04 -05:00
|
|
|
} else {
|
|
|
|
cell->setPort(RTLIL::escape_id(p), *q ? blif_wire(q) : SigSpec());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &it : cell_wideports_cache)
|
|
|
|
{
|
|
|
|
int width = 0;
|
2021-05-05 13:31:27 -05:00
|
|
|
int offset = 0;
|
|
|
|
bool upto = false;
|
2017-07-21 09:21:04 -05:00
|
|
|
for (auto &b : it.second)
|
|
|
|
width = std::max(width, b.first + 1);
|
|
|
|
|
2021-05-05 13:31:27 -05:00
|
|
|
if (cell_mod) {
|
|
|
|
Wire *cell_port = cell_mod->wire(it.first);
|
|
|
|
if (cell_port && (cell_port->port_input || cell_port->port_output)) {
|
|
|
|
offset = cell_port->start_offset;
|
|
|
|
upto = cell_port->upto;
|
|
|
|
width = cell_port->width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 09:21:04 -05:00
|
|
|
SigSpec sig;
|
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
2021-05-05 13:31:27 -05:00
|
|
|
int idx = offset + (upto ? width - 1 - i: i);
|
|
|
|
if (it.second.count(idx))
|
|
|
|
sig.append(it.second.at(idx));
|
2017-07-21 09:21:04 -05:00
|
|
|
else
|
|
|
|
sig.append(module->addWire(NEW_ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
cell->setPort(it.first, sig);
|
2013-12-31 06:41:16 -06:00
|
|
|
}
|
2015-05-24 01:03:21 -05:00
|
|
|
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = cell;
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = &cell->attributes;
|
|
|
|
obj_parameters = &cell->parameters;
|
2013-12-31 06:41:16 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-24 01:03:21 -05:00
|
|
|
obj_attributes = nullptr;
|
|
|
|
obj_parameters = nullptr;
|
|
|
|
|
2018-08-02 16:33:39 -05:00
|
|
|
if (!strcmp(cmd, ".barbuf") || !strcmp(cmd, ".conn"))
|
2015-05-12 23:45:12 -05:00
|
|
|
{
|
|
|
|
char *p = strtok(NULL, " \t\r\n");
|
|
|
|
if (p == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
char *q = strtok(NULL, " \t\r\n");
|
|
|
|
if (q == NULL)
|
|
|
|
goto error;
|
|
|
|
|
2015-12-20 06:12:24 -06:00
|
|
|
module->connect(blif_wire(q), blif_wire(p));
|
2015-05-12 23:45:12 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-23 09:19:34 -05:00
|
|
|
if (!strcmp(cmd, ".names"))
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
RTLIL::SigSpec input_sig, output_sig;
|
2015-12-20 06:12:24 -06:00
|
|
|
while ((p = strtok(NULL, " \t\r\n")) != NULL)
|
|
|
|
input_sig.append(blif_wire(p));
|
2014-07-22 13:15:14 -05:00
|
|
|
output_sig = input_sig.extract(input_sig.size()-1, 1);
|
|
|
|
input_sig = input_sig.extract(0, input_sig.size()-1);
|
2013-07-23 09:19:34 -05:00
|
|
|
|
2015-12-20 06:12:24 -06:00
|
|
|
if (input_sig.size() == 0)
|
|
|
|
{
|
2013-11-13 08:46:28 -06:00
|
|
|
RTLIL::State state = RTLIL::State::Sa;
|
|
|
|
while (1) {
|
2013-12-31 14:58:35 -06:00
|
|
|
if (!read_next_line(buffer, buffer_size, line_count, f))
|
2013-11-13 08:46:28 -06:00
|
|
|
goto error;
|
|
|
|
for (int i = 0; buffer[i]; i++) {
|
|
|
|
if (buffer[i] == ' ' || buffer[i] == '\t')
|
|
|
|
continue;
|
|
|
|
if (i == 0 && buffer[i] == '.')
|
|
|
|
goto finished_parsing_constval;
|
|
|
|
if (buffer[i] == '0') {
|
|
|
|
if (state == RTLIL::State::S1)
|
|
|
|
goto error;
|
|
|
|
state = RTLIL::State::S0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (buffer[i] == '1') {
|
|
|
|
if (state == RTLIL::State::S0)
|
|
|
|
goto error;
|
|
|
|
state = RTLIL::State::S1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2015-12-20 06:12:24 -06:00
|
|
|
|
2013-11-13 08:46:28 -06:00
|
|
|
finished_parsing_constval:
|
|
|
|
if (state == RTLIL::State::Sa)
|
2015-05-24 01:03:21 -05:00
|
|
|
state = RTLIL::State::S0;
|
2020-04-02 11:51:32 -05:00
|
|
|
if (output_sig.as_wire()->name == ID($undef))
|
2015-12-20 06:12:24 -06:00
|
|
|
state = RTLIL::State::Sx;
|
2014-07-26 07:32:50 -05:00
|
|
|
module->connect(RTLIL::SigSig(output_sig, state));
|
2013-11-13 08:46:28 -06:00
|
|
|
goto continue_without_read;
|
|
|
|
}
|
|
|
|
|
2016-06-17 06:46:01 -05:00
|
|
|
if (sop_mode)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
sopcell = module->addCell(NEW_ID, ID($sop));
|
|
|
|
sopcell->parameters[ID::WIDTH] = RTLIL::Const(input_sig.size());
|
|
|
|
sopcell->parameters[ID::DEPTH] = 0;
|
|
|
|
sopcell->parameters[ID::TABLE] = RTLIL::Const();
|
2020-03-12 14:57:01 -05:00
|
|
|
sopcell->setPort(ID::A, input_sig);
|
|
|
|
sopcell->setPort(ID::Y, output_sig);
|
2016-06-17 06:46:01 -05:00
|
|
|
sopmode = -1;
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = sopcell;
|
2016-06-17 06:46:01 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
RTLIL::Cell *cell = module->addCell(NEW_ID, ID($lut));
|
|
|
|
cell->parameters[ID::WIDTH] = RTLIL::Const(input_sig.size());
|
|
|
|
cell->parameters[ID::LUT] = RTLIL::Const(RTLIL::State::Sx, 1 << input_sig.size());
|
2020-03-12 14:57:01 -05:00
|
|
|
cell->setPort(ID::A, input_sig);
|
|
|
|
cell->setPort(ID::Y, output_sig);
|
2020-04-02 11:51:32 -05:00
|
|
|
lutptr = &cell->parameters.at(ID::LUT);
|
2016-06-17 06:46:01 -05:00
|
|
|
lut_default_state = RTLIL::State::Sx;
|
2018-08-02 16:33:39 -05:00
|
|
|
lastcell = cell;
|
2016-06-17 06:46:01 -05:00
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-06-17 06:46:01 -05:00
|
|
|
if (lutptr == NULL && sopcell == NULL)
|
2013-07-23 09:19:34 -05:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
char *input = strtok(buffer, " \t\r\n");
|
|
|
|
char *output = strtok(NULL, " \t\r\n");
|
|
|
|
|
|
|
|
if (input == NULL || output == NULL || (strcmp(output, "0") && strcmp(output, "1")))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
int input_len = strlen(input);
|
|
|
|
|
2016-06-17 06:46:01 -05:00
|
|
|
if (sopcell)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
log_assert(sopcell->parameters[ID::WIDTH].as_int() == input_len);
|
|
|
|
sopcell->parameters[ID::DEPTH] = sopcell->parameters[ID::DEPTH].as_int() + 1;
|
2016-06-17 06:46:01 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < input_len; i++)
|
|
|
|
switch (input[i]) {
|
|
|
|
case '0':
|
2020-04-02 11:51:32 -05:00
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S1);
|
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S0);
|
2016-06-17 06:46:01 -05:00
|
|
|
break;
|
|
|
|
case '1':
|
2020-04-02 11:51:32 -05:00
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S0);
|
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S1);
|
2016-06-17 06:46:01 -05:00
|
|
|
break;
|
|
|
|
default:
|
2020-04-02 11:51:32 -05:00
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S0);
|
|
|
|
sopcell->parameters[ID::TABLE].bits.push_back(State::S0);
|
2016-06-17 06:46:01 -05:00
|
|
|
break;
|
2013-07-23 09:19:34 -05:00
|
|
|
}
|
2016-06-17 06:46:01 -05:00
|
|
|
|
|
|
|
if (sopmode == -1) {
|
|
|
|
sopmode = (*output == '1');
|
|
|
|
if (!sopmode) {
|
2020-03-12 14:57:01 -05:00
|
|
|
SigSpec outnet = sopcell->getPort(ID::Y);
|
2016-06-17 06:46:01 -05:00
|
|
|
SigSpec tempnet = module->addWire(NEW_ID);
|
|
|
|
module->addNotGate(NEW_ID, tempnet, outnet);
|
2020-03-12 14:57:01 -05:00
|
|
|
sopcell->setPort(ID::Y, tempnet);
|
2016-06-17 06:46:01 -05:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
log_assert(sopmode == (*output == '1'));
|
2013-07-23 09:19:34 -05:00
|
|
|
}
|
|
|
|
|
2016-06-17 06:46:01 -05:00
|
|
|
if (lutptr)
|
|
|
|
{
|
2017-09-27 08:27:42 -05:00
|
|
|
if (input_len > 12)
|
2016-06-17 06:46:01 -05:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (int i = 0; i < (1 << input_len); i++) {
|
|
|
|
for (int j = 0; j < input_len; j++) {
|
|
|
|
char c1 = input[j];
|
|
|
|
if (c1 != '-') {
|
|
|
|
char c2 = (i & (1 << j)) != 0 ? '1' : '0';
|
|
|
|
if (c1 != c2)
|
|
|
|
goto try_next_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lutptr->bits.at(i) = !strcmp(output, "0") ? RTLIL::State::S0 : RTLIL::State::S1;
|
|
|
|
try_next_value:;
|
|
|
|
}
|
|
|
|
|
|
|
|
lut_default_state = !strcmp(output, "0") ? RTLIL::State::S1 : RTLIL::State::S0;
|
|
|
|
}
|
2013-07-23 09:19:34 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:48:03 -06:00
|
|
|
return;
|
|
|
|
|
2013-07-23 09:19:34 -05:00
|
|
|
error:
|
|
|
|
log_error("Syntax error in line %d!\n", line_count);
|
2018-08-08 12:22:55 -05:00
|
|
|
error_with_reason:
|
2018-08-08 12:41:47 -05:00
|
|
|
log_error("Syntax error in line %d: %s\n", line_count, err_reason.c_str());
|
2013-07-23 09:19:34 -05:00
|
|
|
}
|
|
|
|
|
2015-05-17 08:25:03 -05:00
|
|
|
struct BlifFrontend : public Frontend {
|
|
|
|
BlifFrontend() : Frontend("blif", "read BLIF file") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2015-05-17 08:25:03 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2019-02-08 14:41:39 -06:00
|
|
|
log(" read_blif [options] [filename]\n");
|
2015-05-17 08:25:03 -05:00
|
|
|
log("\n");
|
|
|
|
log("Load modules from a BLIF file into the current design.\n");
|
|
|
|
log("\n");
|
2016-06-18 05:33:13 -05:00
|
|
|
log(" -sop\n");
|
|
|
|
log(" Create $sop cells instead of $lut cells\n");
|
|
|
|
log("\n");
|
2017-02-06 07:48:03 -06:00
|
|
|
log(" -wideports\n");
|
|
|
|
log(" Merge ports that match the pattern 'name[int]' into a single\n");
|
|
|
|
log(" multi-bit port 'name'.\n");
|
|
|
|
log("\n");
|
2015-05-17 08:25:03 -05:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
|
2015-05-17 08:25:03 -05:00
|
|
|
{
|
2016-06-18 05:33:13 -05:00
|
|
|
bool sop_mode = false;
|
2017-02-06 07:48:03 -06:00
|
|
|
bool wideports = false;
|
2016-06-18 05:33:13 -05:00
|
|
|
|
2016-04-21 16:28:37 -05:00
|
|
|
log_header(design, "Executing BLIF frontend.\n");
|
2015-05-17 08:25:03 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++) {
|
|
|
|
std::string arg = args[argidx];
|
2016-06-18 05:33:13 -05:00
|
|
|
if (arg == "-sop") {
|
|
|
|
sop_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-06 07:48:03 -06:00
|
|
|
if (arg == "-wideports") {
|
|
|
|
wideports = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-17 08:25:03 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
2017-02-06 07:48:03 -06:00
|
|
|
parse_blif(design, *f, "", true, sop_mode, wideports);
|
2015-05-17 08:25:03 -05:00
|
|
|
}
|
|
|
|
} BlifFrontend;
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
YOSYS_NAMESPACE_END
|
|
|
|
|