2018-05-26 20:27:21 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 "chip.h"
|
2018-06-10 00:19:20 +08:00
|
|
|
#include "log.h"
|
2018-05-26 20:27:21 +08:00
|
|
|
|
2018-05-26 22:08:20 +08:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
IdString belTypeToId(BelType type)
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
if (type == TYPE_ICESTORM_LC)
|
|
|
|
return "ICESTORM_LC";
|
|
|
|
if (type == TYPE_ICESTORM_RAM)
|
|
|
|
return "ICESTORM_RAM";
|
|
|
|
if (type == TYPE_SB_IO)
|
|
|
|
return "SB_IO";
|
|
|
|
return IdString();
|
2018-05-26 22:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BelType belTypeFromId(IdString id)
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
if (id == "ICESTORM_LC")
|
|
|
|
return TYPE_ICESTORM_LC;
|
|
|
|
if (id == "ICESTORM_RAM")
|
|
|
|
return TYPE_ICESTORM_RAM;
|
|
|
|
if (id == "SB_IO")
|
|
|
|
return TYPE_SB_IO;
|
2018-06-10 00:41:38 +08:00
|
|
|
return TYPE_NONE;
|
2018-05-26 22:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2018-06-10 00:19:20 +08:00
|
|
|
IdString portPinToId(PortPin type)
|
2018-05-26 22:08:20 +08:00
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
#define X(t) \
|
|
|
|
if (type == PIN_##t) \
|
|
|
|
return #t;
|
|
|
|
|
2018-06-07 20:36:35 +08:00
|
|
|
#include "portpins.inc"
|
2018-06-03 22:16:59 +08:00
|
|
|
|
|
|
|
#undef X
|
2018-06-07 18:56:49 +08:00
|
|
|
return IdString();
|
2018-05-26 22:08:20 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 00:19:20 +08:00
|
|
|
PortPin portPinFromId(IdString id)
|
2018-05-26 22:08:20 +08:00
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
#define X(t) \
|
|
|
|
if (id == #t) \
|
|
|
|
return PIN_##t;
|
|
|
|
|
2018-06-07 20:36:35 +08:00
|
|
|
#include "portpins.inc"
|
2018-06-03 22:16:59 +08:00
|
|
|
|
|
|
|
#undef X
|
2018-06-10 00:41:38 +08:00
|
|
|
return PIN_NONE;
|
2018-05-26 22:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2018-06-10 17:56:07 +08:00
|
|
|
Chip::Chip(ChipArgs args) : args(args)
|
2018-05-26 20:27:21 +08:00
|
|
|
{
|
2018-06-07 18:26:02 +08:00
|
|
|
#ifdef ICE40_HX1K_ONLY
|
2018-06-07 18:56:49 +08:00
|
|
|
if (args.type == ChipArgs::HX1K) {
|
|
|
|
chip_info = chip_info_1k;
|
2018-06-10 00:19:20 +08:00
|
|
|
} else {
|
|
|
|
log_error("Unsupported iCE40 chip type.\n");
|
2018-06-07 18:56:49 +08:00
|
|
|
}
|
2018-06-07 18:26:02 +08:00
|
|
|
#else
|
2018-06-07 18:56:49 +08:00
|
|
|
if (args.type == ChipArgs::LP384) {
|
|
|
|
chip_info = chip_info_384;
|
|
|
|
} else if (args.type == ChipArgs::LP1K || args.type == ChipArgs::HX1K) {
|
|
|
|
chip_info = chip_info_1k;
|
|
|
|
} else if (args.type == ChipArgs::UP5K) {
|
|
|
|
chip_info = chip_info_5k;
|
|
|
|
} else if (args.type == ChipArgs::LP8K || args.type == ChipArgs::HX8K) {
|
|
|
|
chip_info = chip_info_8k;
|
|
|
|
} else {
|
2018-06-10 00:19:20 +08:00
|
|
|
log_error("Unsupported iCE40 chip type.\n");
|
2018-06-07 18:56:49 +08:00
|
|
|
}
|
2018-06-07 18:26:02 +08:00
|
|
|
#endif
|
2018-05-26 20:56:30 +08:00
|
|
|
|
2018-06-10 00:19:20 +08:00
|
|
|
bel_to_cell.resize(chip_info.num_bels);
|
|
|
|
wire_to_net.resize(chip_info.num_wires);
|
|
|
|
pip_to_net.resize(chip_info.num_pips);
|
2018-05-26 20:27:21 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 21:13:41 +08:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2018-05-26 22:08:20 +08:00
|
|
|
BelId Chip::getBelByName(IdString name) const
|
2018-05-26 20:27:21 +08:00
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
BelId ret;
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
if (bel_by_name.empty()) {
|
|
|
|
for (int i = 0; i < chip_info.num_bels; i++)
|
|
|
|
bel_by_name[chip_info.bel_data[i].name] = i;
|
|
|
|
}
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
auto it = bel_by_name.find(name);
|
|
|
|
if (it != bel_by_name.end())
|
|
|
|
ret.index = it->second;
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
return ret;
|
2018-05-26 22:08:20 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 21:13:41 +08:00
|
|
|
WireId Chip::getWireBelPin(BelId bel, PortPin pin) const
|
|
|
|
{
|
2018-06-10 00:19:20 +08:00
|
|
|
WireId ret;
|
|
|
|
|
2018-06-10 00:41:38 +08:00
|
|
|
assert(bel != BelId());
|
2018-06-10 00:19:20 +08:00
|
|
|
|
|
|
|
int num_bel_wires = chip_info.bel_data[bel.index].num_bel_wires;
|
|
|
|
BelWirePOD *bel_wires = chip_info.bel_data[bel.index].bel_wires;
|
|
|
|
|
|
|
|
for (int i = 0; i < num_bel_wires; i++)
|
|
|
|
if (bel_wires[i].port == pin) {
|
|
|
|
ret.index = bel_wires[i].wire_index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2018-05-26 22:08:20 +08:00
|
|
|
WireId Chip::getWireByName(IdString name) const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
WireId ret;
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
if (wire_by_name.empty()) {
|
|
|
|
for (int i = 0; i < chip_info.num_wires; i++)
|
|
|
|
wire_by_name[chip_info.wire_data[i].name] = i;
|
|
|
|
}
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
auto it = wire_by_name.find(name);
|
|
|
|
if (it != wire_by_name.end())
|
|
|
|
ret.index = it->second;
|
2018-05-26 22:08:20 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
return ret;
|
2018-05-26 20:27:21 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 21:13:41 +08:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
PipId Chip::getPipByName(IdString name) const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
PipId ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
if (pip_by_name.empty()) {
|
|
|
|
for (int i = 0; i < chip_info.num_pips; i++) {
|
|
|
|
PipId pip;
|
|
|
|
pip.index = i;
|
|
|
|
pip_by_name[getPipName(pip)] = i;
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 21:13:41 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
auto it = pip_by_name.find(name);
|
|
|
|
if (it != pip_by_name.end())
|
|
|
|
ret.index = it->second;
|
2018-06-06 21:13:41 +08:00
|
|
|
|
2018-06-07 18:56:49 +08:00
|
|
|
return ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Chip::getBelPosition(BelId bel, float &x, float &y) const
|
2018-05-26 20:27:21 +08:00
|
|
|
{
|
2018-06-10 00:41:38 +08:00
|
|
|
assert(bel != BelId());
|
2018-06-07 18:56:49 +08:00
|
|
|
x = chip_info.bel_data[bel.index].x;
|
|
|
|
y = chip_info.bel_data[bel.index].y;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chip::getWirePosition(WireId wire, float &x, float &y) const
|
|
|
|
{
|
2018-06-10 00:41:38 +08:00
|
|
|
assert(wire != WireId());
|
2018-06-07 18:56:49 +08:00
|
|
|
x = chip_info.wire_data[wire.index].x;
|
|
|
|
y = chip_info.wire_data[wire.index].y;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 22:42:42 +08:00
|
|
|
void Chip::getPipPosition(PipId pip, float &x, float &y) const
|
2018-06-06 21:13:41 +08:00
|
|
|
{
|
2018-06-10 00:41:38 +08:00
|
|
|
assert(pip != PipId());
|
2018-06-07 18:56:49 +08:00
|
|
|
x = chip_info.pip_data[pip.index].x;
|
|
|
|
y = chip_info.pip_data[pip.index].y;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<GraphicElement> Chip::getBelGraphics(BelId bel) const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
vector<GraphicElement> ret;
|
|
|
|
|
|
|
|
auto bel_type = getBelType(bel);
|
|
|
|
|
|
|
|
if (bel_type == TYPE_ICESTORM_LC) {
|
|
|
|
GraphicElement el;
|
|
|
|
el.type = GraphicElement::G_BOX;
|
|
|
|
el.x1 = chip_info.bel_data[bel.index].x + 0.1;
|
|
|
|
el.x2 = chip_info.bel_data[bel.index].x + 0.9;
|
|
|
|
el.y1 = chip_info.bel_data[bel.index].y + 0.10 +
|
|
|
|
(chip_info.bel_data[bel.index].z) * (0.8 / 8);
|
|
|
|
el.y2 = chip_info.bel_data[bel.index].y + 0.18 +
|
|
|
|
(chip_info.bel_data[bel.index].z) * (0.8 / 8);
|
|
|
|
el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bel_type == TYPE_SB_IO) {
|
|
|
|
if (chip_info.bel_data[bel.index].x == 0 ||
|
|
|
|
chip_info.bel_data[bel.index].x == chip_info.width - 1) {
|
|
|
|
GraphicElement el;
|
|
|
|
el.type = GraphicElement::G_BOX;
|
|
|
|
el.x1 = chip_info.bel_data[bel.index].x + 0.1;
|
|
|
|
el.x2 = chip_info.bel_data[bel.index].x + 0.9;
|
|
|
|
if (chip_info.bel_data[bel.index].z == 0) {
|
|
|
|
el.y1 = chip_info.bel_data[bel.index].y + 0.10;
|
|
|
|
el.y2 = chip_info.bel_data[bel.index].y + 0.45;
|
|
|
|
} else {
|
|
|
|
el.y1 = chip_info.bel_data[bel.index].y + 0.55;
|
|
|
|
el.y2 = chip_info.bel_data[bel.index].y + 0.90;
|
|
|
|
}
|
|
|
|
el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
} else {
|
|
|
|
GraphicElement el;
|
|
|
|
el.type = GraphicElement::G_BOX;
|
|
|
|
if (chip_info.bel_data[bel.index].z == 0) {
|
|
|
|
el.x1 = chip_info.bel_data[bel.index].x + 0.10;
|
|
|
|
el.x2 = chip_info.bel_data[bel.index].x + 0.45;
|
|
|
|
} else {
|
|
|
|
el.x1 = chip_info.bel_data[bel.index].x + 0.55;
|
|
|
|
el.x2 = chip_info.bel_data[bel.index].x + 0.90;
|
|
|
|
}
|
|
|
|
el.y1 = chip_info.bel_data[bel.index].y + 0.1;
|
|
|
|
el.y2 = chip_info.bel_data[bel.index].y + 0.9;
|
|
|
|
el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bel_type == TYPE_ICESTORM_RAM) {
|
|
|
|
GraphicElement el;
|
|
|
|
el.type = GraphicElement::G_BOX;
|
|
|
|
el.x1 = chip_info.bel_data[bel.index].x + 0.1;
|
|
|
|
el.x2 = chip_info.bel_data[bel.index].x + 0.9;
|
|
|
|
el.y1 = chip_info.bel_data[bel.index].y + 0.1;
|
|
|
|
el.y2 = chip_info.bel_data[bel.index].y + 1.9;
|
|
|
|
el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<GraphicElement> Chip::getWireGraphics(WireId wire) const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
vector<GraphicElement> ret;
|
|
|
|
// FIXME
|
|
|
|
return ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<GraphicElement> Chip::getPipGraphics(PipId pip) const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
vector<GraphicElement> ret;
|
|
|
|
// FIXME
|
|
|
|
return ret;
|
2018-06-06 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<GraphicElement> Chip::getFrameGraphics() const
|
|
|
|
{
|
2018-06-07 18:56:49 +08:00
|
|
|
vector<GraphicElement> ret;
|
|
|
|
|
|
|
|
for (int x = 0; x <= chip_info.width; x++)
|
|
|
|
for (int y = 0; y <= chip_info.height; y++) {
|
|
|
|
GraphicElement el;
|
|
|
|
el.type = GraphicElement::G_LINE;
|
|
|
|
el.x1 = x - 0.05, el.x2 = x + 0.05, el.y1 = y, el.y2 = y, el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
el.x1 = x, el.x2 = x, el.y1 = y - 0.05, el.y2 = y + 0.05, el.z = 0;
|
|
|
|
ret.push_back(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2018-05-26 20:27:21 +08:00
|
|
|
}
|