2019-07-15 16:46:31 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2019-09-18 14:35:24 -05:00
|
|
|
* 2019 Eddie Hung <eddie@fpgeh.com>
|
2019-07-15 16:46:31 -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.
|
|
|
|
*
|
|
|
|
* 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/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
#include "passes/pmgen/xilinx_dsp_pm.h"
|
|
|
|
|
2019-09-09 22:58:54 -05:00
|
|
|
static Cell* addDsp(Module *module) {
|
2019-09-19 18:13:22 -05:00
|
|
|
Cell *cell = module->addCell(NEW_ID, ID(DSP48E1));
|
|
|
|
cell->setParam(ID(ACASCREG), 0);
|
|
|
|
cell->setParam(ID(ADREG), 0);
|
|
|
|
cell->setParam(ID(A_INPUT), Const("DIRECT"));
|
|
|
|
cell->setParam(ID(ALUMODEREG), 0);
|
|
|
|
cell->setParam(ID(AREG), 0);
|
|
|
|
cell->setParam(ID(BCASCREG), 0);
|
|
|
|
cell->setParam(ID(B_INPUT), Const("DIRECT"));
|
|
|
|
cell->setParam(ID(BREG), 0);
|
|
|
|
cell->setParam(ID(CARRYINREG), 0);
|
|
|
|
cell->setParam(ID(CARRYINSELREG), 0);
|
|
|
|
cell->setParam(ID(CREG), 0);
|
|
|
|
cell->setParam(ID(DREG), 0);
|
|
|
|
cell->setParam(ID(INMODEREG), 0);
|
|
|
|
cell->setParam(ID(MREG), 0);
|
|
|
|
cell->setParam(ID(OPMODEREG), 0);
|
|
|
|
cell->setParam(ID(PREG), 0);
|
|
|
|
cell->setParam(ID(USE_MULT), Const("NONE"));
|
|
|
|
cell->setParam(ID(USE_SIMD), Const("ONE48"));
|
|
|
|
cell->setParam(ID(USE_DPORT), Const("FALSE"));
|
|
|
|
|
|
|
|
cell->setPort(ID(D), Const(0, 24));
|
|
|
|
cell->setPort(ID(INMODE), Const(0, 5));
|
|
|
|
cell->setPort(ID(ALUMODE), Const(0, 4));
|
|
|
|
cell->setPort(ID(OPMODE), Const(0, 7));
|
|
|
|
cell->setPort(ID(CARRYINSEL), Const(0, 3));
|
|
|
|
cell->setPort(ID(ACIN), Const(0, 30));
|
|
|
|
cell->setPort(ID(BCIN), Const(0, 18));
|
|
|
|
cell->setPort(ID(PCIN), Const(0, 48));
|
|
|
|
cell->setPort(ID(CARRYIN), Const(0, 1));
|
2019-09-09 22:58:54 -05:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2019-09-09 22:57:20 -05:00
|
|
|
void pack_xilinx_simd(Module *module, const std::vector<Cell*> &selected_cells)
|
|
|
|
{
|
2019-09-09 23:39:42 -05:00
|
|
|
std::deque<Cell*> simd12_add, simd12_sub;
|
|
|
|
std::deque<Cell*> simd24_add, simd24_sub;
|
2019-09-09 22:57:20 -05:00
|
|
|
|
|
|
|
for (auto cell : selected_cells) {
|
2019-09-19 18:13:22 -05:00
|
|
|
if (!cell->type.in(ID($add), ID($sub)))
|
2019-09-09 22:57:20 -05:00
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec Y = cell->getPort(ID(Y));
|
2019-09-09 22:57:20 -05:00
|
|
|
if (!Y.is_chunk())
|
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
if (!Y.as_chunk().wire->get_strpool_attribute(ID(use_dsp)).count("simd"))
|
2019-09-09 22:57:20 -05:00
|
|
|
continue;
|
|
|
|
if (GetSize(Y) > 25)
|
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec A = cell->getPort(ID(A));
|
|
|
|
SigSpec B = cell->getPort(ID(B));
|
2019-09-09 22:57:20 -05:00
|
|
|
if (GetSize(Y) <= 13) {
|
|
|
|
if (GetSize(A) > 12)
|
|
|
|
continue;
|
|
|
|
if (GetSize(B) > 12)
|
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
if (cell->type == ID($add))
|
2019-09-09 23:39:42 -05:00
|
|
|
simd12_add.push_back(cell);
|
2019-09-19 18:13:22 -05:00
|
|
|
else if (cell->type == ID($sub))
|
2019-09-09 23:39:42 -05:00
|
|
|
simd12_sub.push_back(cell);
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
2019-09-09 23:11:41 -05:00
|
|
|
else if (GetSize(Y) <= 25) {
|
2019-09-09 22:57:20 -05:00
|
|
|
if (GetSize(A) > 24)
|
|
|
|
continue;
|
|
|
|
if (GetSize(B) > 24)
|
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
if (cell->type == ID($add))
|
2019-09-09 23:39:42 -05:00
|
|
|
simd24_add.push_back(cell);
|
2019-09-19 18:13:22 -05:00
|
|
|
else if (cell->type == ID($sub))
|
2019-09-09 23:39:42 -05:00
|
|
|
simd24_sub.push_back(cell);
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
2019-09-09 23:11:41 -05:00
|
|
|
else
|
|
|
|
log_abort();
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
auto f12 = [module](SigSpec &AB, SigSpec &C, SigSpec &P, SigSpec &CARRYOUT, Cell *lane) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec A = lane->getPort(ID(A));
|
|
|
|
SigSpec B = lane->getPort(ID(B));
|
|
|
|
SigSpec Y = lane->getPort(ID(Y));
|
|
|
|
A.extend_u0(12, lane->getParam(ID(A_SIGNED)).as_bool());
|
|
|
|
B.extend_u0(12, lane->getParam(ID(B_SIGNED)).as_bool());
|
2019-09-09 22:57:20 -05:00
|
|
|
AB.append(A);
|
|
|
|
C.append(B);
|
|
|
|
if (GetSize(Y) < 13)
|
|
|
|
Y.append(module->addWire(NEW_ID, 13-GetSize(Y)));
|
|
|
|
else
|
|
|
|
log_assert(GetSize(Y) == 13);
|
|
|
|
P.append(Y.extract(0, 12));
|
|
|
|
CARRYOUT.append(Y[12]);
|
|
|
|
};
|
2019-09-09 23:39:42 -05:00
|
|
|
auto g12 = [&f12,module](std::deque<Cell*> &simd12) {
|
|
|
|
while (simd12.size() > 1) {
|
|
|
|
SigSpec AB, C, P, CARRYOUT;
|
2019-09-09 22:57:20 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
Cell *lane1 = simd12.front();
|
|
|
|
simd12.pop_front();
|
|
|
|
Cell *lane2 = simd12.front();
|
2019-09-09 22:57:20 -05:00
|
|
|
simd12.pop_front();
|
2019-09-09 23:39:42 -05:00
|
|
|
Cell *lane3 = nullptr;
|
|
|
|
Cell *lane4 = nullptr;
|
|
|
|
|
2019-09-09 22:57:20 -05:00
|
|
|
if (!simd12.empty()) {
|
2019-09-09 23:39:42 -05:00
|
|
|
lane3 = simd12.front();
|
2019-09-09 22:57:20 -05:00
|
|
|
simd12.pop_front();
|
2019-09-09 23:39:42 -05:00
|
|
|
if (!simd12.empty()) {
|
|
|
|
lane4 = simd12.front();
|
|
|
|
simd12.pop_front();
|
|
|
|
}
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
log("Analysing %s.%s for Xilinx DSP SIMD12 packing.\n", log_id(module), log_id(lane1));
|
2019-09-09 22:57:20 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
Cell *cell = addDsp(module);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(USE_SIMD), Const("FOUR12"));
|
2019-09-09 23:39:42 -05:00
|
|
|
// X = A:B
|
|
|
|
// Y = 0
|
|
|
|
// Z = C
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(OPMODE), Const::from_string("0110011"));
|
2019-09-09 22:57:20 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
log_assert(lane1);
|
|
|
|
log_assert(lane2);
|
|
|
|
f12(AB, C, P, CARRYOUT, lane1);
|
|
|
|
f12(AB, C, P, CARRYOUT, lane2);
|
|
|
|
if (lane3) {
|
|
|
|
f12(AB, C, P, CARRYOUT, lane3);
|
|
|
|
if (lane4)
|
|
|
|
f12(AB, C, P, CARRYOUT, lane4);
|
|
|
|
else {
|
|
|
|
AB.append(Const(0, 12));
|
|
|
|
C.append(Const(0, 12));
|
|
|
|
P.append(module->addWire(NEW_ID, 12));
|
|
|
|
CARRYOUT.append(module->addWire(NEW_ID, 1));
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 22:57:20 -05:00
|
|
|
else {
|
2019-09-09 23:39:42 -05:00
|
|
|
AB.append(Const(0, 24));
|
|
|
|
C.append(Const(0, 24));
|
|
|
|
P.append(module->addWire(NEW_ID, 24));
|
|
|
|
CARRYOUT.append(module->addWire(NEW_ID, 2));
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
2019-09-09 23:39:42 -05:00
|
|
|
log_assert(GetSize(AB) == 48);
|
|
|
|
log_assert(GetSize(C) == 48);
|
|
|
|
log_assert(GetSize(P) == 48);
|
|
|
|
log_assert(GetSize(CARRYOUT) == 4);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(A), AB.extract(18, 30));
|
|
|
|
cell->setPort(ID(B), AB.extract(0, 18));
|
|
|
|
cell->setPort(ID(C), C);
|
|
|
|
cell->setPort(ID(P), P);
|
|
|
|
cell->setPort(ID(CARRYOUT), CARRYOUT);
|
|
|
|
if (lane1->type == ID($sub))
|
|
|
|
cell->setPort(ID(ALUMODE), Const::from_string("0011"));
|
2019-09-09 22:57:20 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
module->remove(lane1);
|
|
|
|
module->remove(lane2);
|
|
|
|
if (lane3) module->remove(lane3);
|
|
|
|
if (lane4) module->remove(lane4);
|
2019-09-09 22:57:20 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
module->design->select(module, cell);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
g12(simd12_add);
|
|
|
|
g12(simd12_sub);
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
auto f24 = [module](SigSpec &AB, SigSpec &C, SigSpec &P, SigSpec &CARRYOUT, Cell *lane) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec A = lane->getPort(ID(A));
|
|
|
|
SigSpec B = lane->getPort(ID(B));
|
|
|
|
SigSpec Y = lane->getPort(ID(Y));
|
|
|
|
A.extend_u0(24, lane->getParam(ID(A_SIGNED)).as_bool());
|
|
|
|
B.extend_u0(24, lane->getParam(ID(B_SIGNED)).as_bool());
|
2019-09-10 00:06:23 -05:00
|
|
|
C.append(A);
|
2019-09-09 23:39:42 -05:00
|
|
|
AB.append(B);
|
2019-09-09 23:11:41 -05:00
|
|
|
if (GetSize(Y) < 25)
|
|
|
|
Y.append(module->addWire(NEW_ID, 25-GetSize(Y)));
|
|
|
|
else
|
|
|
|
log_assert(GetSize(Y) == 25);
|
|
|
|
P.append(Y.extract(0, 24));
|
|
|
|
CARRYOUT.append(module->addWire(NEW_ID)); // TWO24 uses every other bit
|
|
|
|
CARRYOUT.append(Y[24]);
|
|
|
|
};
|
2019-09-09 23:39:42 -05:00
|
|
|
auto g24 = [&f24,module](std::deque<Cell*> &simd24) {
|
|
|
|
while (simd24.size() > 1) {
|
|
|
|
SigSpec AB;
|
|
|
|
SigSpec C;
|
|
|
|
SigSpec P;
|
|
|
|
SigSpec CARRYOUT;
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
Cell *lane1 = simd24.front();
|
|
|
|
simd24.pop_front();
|
|
|
|
Cell *lane2 = simd24.front();
|
|
|
|
simd24.pop_front();
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
log("Analysing %s.%s for Xilinx DSP SIMD24 packing.\n", log_id(module), log_id(lane1));
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
Cell *cell = addDsp(module);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(USE_SIMD), Const("TWO24"));
|
2019-09-09 23:39:42 -05:00
|
|
|
// X = A:B
|
|
|
|
// Y = 0
|
|
|
|
// Z = C
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(OPMODE), Const::from_string("0110011"));
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
log_assert(lane1);
|
|
|
|
log_assert(lane2);
|
|
|
|
f24(AB, C, P, CARRYOUT, lane1);
|
|
|
|
f24(AB, C, P, CARRYOUT, lane2);
|
|
|
|
log_assert(GetSize(AB) == 48);
|
|
|
|
log_assert(GetSize(C) == 48);
|
|
|
|
log_assert(GetSize(P) == 48);
|
|
|
|
log_assert(GetSize(CARRYOUT) == 4);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(A), AB.extract(18, 30));
|
|
|
|
cell->setPort(ID(B), AB.extract(0, 18));
|
|
|
|
cell->setPort(ID(C), C);
|
|
|
|
cell->setPort(ID(P), P);
|
|
|
|
cell->setPort(ID(CARRYOUT), CARRYOUT);
|
|
|
|
if (lane1->type == ID($sub))
|
|
|
|
cell->setPort(ID(ALUMODE), Const::from_string("0011"));
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
module->remove(lane1);
|
|
|
|
module->remove(lane2);
|
2019-09-09 23:11:41 -05:00
|
|
|
|
2019-09-09 23:39:42 -05:00
|
|
|
module->design->select(module, cell);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
g24(simd24_add);
|
|
|
|
g24(simd24_sub);
|
2019-09-09 22:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-13 19:11:35 -05:00
|
|
|
void pack_xilinx_dsp(dict<SigBit, Cell*> &bit_to_driver, xilinx_dsp_pm &pm)
|
2019-07-15 16:46:31 -05:00
|
|
|
{
|
|
|
|
auto &st = pm.st_xilinx_dsp;
|
|
|
|
|
2019-07-16 16:06:32 -05:00
|
|
|
#if 1
|
2019-07-15 16:46:31 -05:00
|
|
|
log("\n");
|
2019-09-06 16:06:57 -05:00
|
|
|
log("preAdd: %s\n", log_id(st.preAdd, "--"));
|
2019-09-11 12:15:19 -05:00
|
|
|
log("ffAD: %s %s %s\n", log_id(st.ffAD, "--"), log_id(st.ffADcemux, "--"), log_id(st.ffADrstmux, "--"));
|
2019-09-11 18:21:24 -05:00
|
|
|
log("ffA2: %s %s %s\n", log_id(st.ffA2, "--"), log_id(st.ffA2cemux, "--"), log_id(st.ffA2rstmux, "--"));
|
2019-09-11 19:16:46 -05:00
|
|
|
log("ffA1: %s %s %s\n", log_id(st.ffA1, "--"), log_id(st.ffA1cemux, "--"), log_id(st.ffA1rstmux, "--"));
|
2019-09-11 18:21:24 -05:00
|
|
|
log("ffB2: %s %s %s\n", log_id(st.ffB2, "--"), log_id(st.ffB2cemux, "--"), log_id(st.ffB2rstmux, "--"));
|
2019-09-11 19:16:46 -05:00
|
|
|
log("ffB1: %s %s %s\n", log_id(st.ffB1, "--"), log_id(st.ffB1cemux, "--"), log_id(st.ffB1rstmux, "--"));
|
2019-09-11 12:15:19 -05:00
|
|
|
log("ffC: %s %s %s\n", log_id(st.ffC, "--"), log_id(st.ffCcemux, "--"), log_id(st.ffCrstmux, "--"));
|
|
|
|
log("ffD: %s %s %s\n", log_id(st.ffD, "--"), log_id(st.ffDcemux, "--"), log_id(st.ffDrstmux, "--"));
|
2019-09-03 18:24:59 -05:00
|
|
|
log("dsp: %s\n", log_id(st.dsp, "--"));
|
2019-09-11 12:15:19 -05:00
|
|
|
log("ffM: %s %s %s\n", log_id(st.ffM, "--"), log_id(st.ffMcemux, "--"), log_id(st.ffMrstmux, "--"));
|
2019-09-03 18:24:59 -05:00
|
|
|
log("postAdd: %s\n", log_id(st.postAdd, "--"));
|
|
|
|
log("postAddMux: %s\n", log_id(st.postAddMux, "--"));
|
2019-09-11 12:15:19 -05:00
|
|
|
log("ffP: %s %s %s\n", log_id(st.ffP, "--"), log_id(st.ffPcemux, "--"), log_id(st.ffPrstmux, "--"));
|
2019-09-18 11:39:59 -05:00
|
|
|
log("overflow: %s\n", log_id(st.overflow, "--"));
|
2019-07-15 16:46:31 -05:00
|
|
|
#endif
|
|
|
|
|
2019-08-09 17:19:33 -05:00
|
|
|
log("Analysing %s.%s for Xilinx DSP packing.\n", log_id(pm.module), log_id(st.dsp));
|
2019-07-15 16:46:31 -05:00
|
|
|
|
2019-07-16 16:06:32 -05:00
|
|
|
Cell *cell = st.dsp;
|
2019-08-09 17:19:33 -05:00
|
|
|
|
2019-09-06 16:06:57 -05:00
|
|
|
if (st.preAdd) {
|
|
|
|
log(" preadder %s (%s)\n", log_id(st.preAdd), log_id(st.preAdd->type));
|
2019-09-19 18:13:22 -05:00
|
|
|
bool A_SIGNED = st.preAdd->getParam(ID(A_SIGNED)).as_bool();
|
|
|
|
bool D_SIGNED = st.preAdd->getParam(ID(B_SIGNED)).as_bool();
|
|
|
|
if (st.sigA == st.preAdd->getPort(ID(B)))
|
2019-09-06 16:06:57 -05:00
|
|
|
std::swap(A_SIGNED, D_SIGNED);
|
|
|
|
st.sigA.extend_u0(30, A_SIGNED);
|
|
|
|
st.sigD.extend_u0(25, D_SIGNED);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(A), st.sigA);
|
|
|
|
cell->setPort(ID(D), st.sigD);
|
|
|
|
cell->connections_.at(ID(INMODE)) = Const::from_string("00100");
|
2019-09-06 16:06:57 -05:00
|
|
|
|
|
|
|
if (st.ffAD) {
|
2019-09-11 12:15:19 -05:00
|
|
|
if (st.ffADcemux) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec S = st.ffADcemux->getPort(ID(S));
|
|
|
|
cell->setPort(ID(CEAD), st.ffADcepol ? S : pm.module->Not(NEW_ID, S));
|
2019-09-06 16:06:57 -05:00
|
|
|
}
|
|
|
|
else
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(CEAD), State::S1);
|
|
|
|
cell->setParam(ID(ADREG), 1);
|
2019-09-06 16:06:57 -05:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(USE_DPORT), Const("TRUE"));
|
2019-09-06 16:06:57 -05:00
|
|
|
|
|
|
|
pm.autoremove(st.preAdd);
|
|
|
|
}
|
2019-09-03 18:10:16 -05:00
|
|
|
if (st.postAdd) {
|
2019-09-06 16:06:57 -05:00
|
|
|
log(" postadder %s (%s)\n", log_id(st.postAdd), log_id(st.postAdd->type));
|
2019-09-03 16:57:59 -05:00
|
|
|
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &opmode = cell->connections_.at(ID(OPMODE));
|
2019-09-03 18:37:59 -05:00
|
|
|
if (st.postAddMux) {
|
|
|
|
log_assert(st.ffP);
|
2019-09-19 18:13:22 -05:00
|
|
|
opmode[4] = st.postAddMux->getPort(ID(S));
|
2019-09-03 18:24:59 -05:00
|
|
|
pm.autoremove(st.postAddMux);
|
2019-09-03 17:53:10 -05:00
|
|
|
}
|
2019-09-11 12:55:45 -05:00
|
|
|
else if (st.ffP && st.sigC == st.sigP)
|
2019-09-03 16:57:59 -05:00
|
|
|
opmode[4] = State::S0;
|
|
|
|
else
|
|
|
|
opmode[4] = State::S1;
|
|
|
|
opmode[6] = State::S0;
|
|
|
|
opmode[5] = State::S1;
|
|
|
|
|
2019-09-06 23:01:36 -05:00
|
|
|
if (opmode[4] != State::S0) {
|
2019-09-19 18:13:22 -05:00
|
|
|
if (st.postAddMuxAB == ID(A))
|
|
|
|
st.sigC.extend_u0(48, st.postAdd->getParam(ID(B_SIGNED)).as_bool());
|
2019-09-19 17:40:17 -05:00
|
|
|
else
|
2019-09-19 18:13:22 -05:00
|
|
|
st.sigC.extend_u0(48, st.postAdd->getParam(ID(A_SIGNED)).as_bool());
|
|
|
|
cell->setPort(ID(C), st.sigC);
|
2019-09-06 23:01:36 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:10:16 -05:00
|
|
|
pm.autoremove(st.postAdd);
|
2019-09-03 16:57:59 -05:00
|
|
|
}
|
2019-09-18 11:39:59 -05:00
|
|
|
if (st.overflow) {
|
|
|
|
log(" overflow %s (%s)\n", log_id(st.overflow), log_id(st.overflow->type));
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(USE_PATTERN_DETECT), Const("PATDET"));
|
|
|
|
cell->setParam(ID(SEL_PATTERN), Const("PATTERN"));
|
|
|
|
cell->setParam(ID(SEL_MASK), Const("MASK"));
|
2019-09-18 11:39:59 -05:00
|
|
|
|
2019-09-19 18:13:22 -05:00
|
|
|
if (st.overflow->type == ID($ge)) {
|
|
|
|
Const B = st.overflow->getPort(ID(B)).as_const();
|
2019-09-18 14:16:03 -05:00
|
|
|
log_assert(std::count(B.bits.begin(), B.bits.end(), State::S1) == 1);
|
|
|
|
// Since B is an exact power of 2, subtract 1
|
|
|
|
// by inverting all bits up until hitting
|
|
|
|
// that one hi bit
|
|
|
|
for (auto &b : B.bits)
|
|
|
|
if (b == State::S0) b = State::S1;
|
|
|
|
else if (b == State::S1) {
|
|
|
|
b = State::S0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
B.extu(48);
|
2019-09-18 11:39:59 -05:00
|
|
|
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(MASK), B);
|
|
|
|
cell->setParam(ID(PATTERN), Const(0, 48));
|
|
|
|
cell->setPort(ID(OVERFLOW), st.overflow->getPort(ID(Y)));
|
2019-09-18 11:39:59 -05:00
|
|
|
}
|
|
|
|
else log_abort();
|
|
|
|
|
|
|
|
pm.autoremove(st.overflow);
|
|
|
|
}
|
2019-09-03 16:57:59 -05:00
|
|
|
|
2019-07-15 16:46:31 -05:00
|
|
|
if (st.clock != SigBit())
|
|
|
|
{
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(CLK), st.clock);
|
2019-07-15 16:46:31 -05:00
|
|
|
|
2019-09-11 12:55:45 -05:00
|
|
|
auto f = [&pm,cell](SigSpec &A, Cell* ff, Cell* cemux, bool cepol, IdString ceport, Cell* rstmux, bool rstpol, IdString rstport) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec D = ff->getPort(ID(D));
|
|
|
|
SigSpec Q = pm.sigmap(ff->getPort(ID(Q)));
|
2019-09-11 12:55:45 -05:00
|
|
|
if (!A.empty())
|
|
|
|
A.replace(Q, D);
|
2019-09-11 12:15:19 -05:00
|
|
|
if (rstmux) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec Y = rstmux->getPort(ID(Y));
|
|
|
|
SigSpec AB = rstmux->getPort(rstpol ? ID(A) : ID(B));
|
2019-09-11 12:55:45 -05:00
|
|
|
if (!A.empty())
|
|
|
|
A.replace(Y, AB);
|
2019-09-11 19:16:46 -05:00
|
|
|
if (rstport != IdString()) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec S = rstmux->getPort(ID(S));
|
2019-09-11 19:16:46 -05:00
|
|
|
cell->setPort(rstport, rstpol ? S : pm.module->Not(NEW_ID, S));
|
|
|
|
}
|
2019-09-11 12:15:19 -05:00
|
|
|
}
|
2019-09-11 19:16:46 -05:00
|
|
|
else if (rstport != IdString())
|
2019-09-11 12:15:19 -05:00
|
|
|
cell->setPort(rstport, State::S0);
|
|
|
|
if (cemux) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec Y = cemux->getPort(ID(Y));
|
|
|
|
SigSpec BA = cemux->getPort(cepol ? ID(B) : ID(A));
|
|
|
|
SigSpec S = cemux->getPort(ID(S));
|
2019-09-11 12:55:45 -05:00
|
|
|
if (!A.empty())
|
|
|
|
A.replace(Y, BA);
|
2019-09-11 12:15:19 -05:00
|
|
|
cell->setPort(ceport, cepol ? S : pm.module->Not(NEW_ID, S));
|
2019-09-05 12:07:26 -05:00
|
|
|
}
|
|
|
|
else
|
2019-09-11 12:15:19 -05:00
|
|
|
cell->setPort(ceport, State::S1);
|
2019-09-11 12:55:45 -05:00
|
|
|
|
|
|
|
for (auto c : Q.chunks()) {
|
2019-09-19 18:13:22 -05:00
|
|
|
auto it = c.wire->attributes.find(ID(init));
|
2019-09-11 12:55:45 -05:00
|
|
|
if (it == c.wire->attributes.end())
|
|
|
|
continue;
|
|
|
|
for (int i = c.offset; i < c.offset+c.width; i++) {
|
|
|
|
log_assert(it->second[i] == State::S0 || it->second[i] == State::Sx);
|
|
|
|
it->second[i] = State::Sx;
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 12:15:19 -05:00
|
|
|
};
|
2019-09-05 12:46:33 -05:00
|
|
|
|
2019-09-11 18:21:24 -05:00
|
|
|
if (st.ffA2) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &A = cell->connections_.at(ID(A));
|
|
|
|
f(A, st.ffA2, st.ffA2cemux, st.ffA2cepol, ID(CEA2), st.ffA2rstmux, st.ffArstpol, ID(RSTA));
|
2019-09-11 19:16:46 -05:00
|
|
|
pm.add_siguser(A, cell);
|
|
|
|
if (st.ffA1) {
|
2019-09-19 18:13:22 -05:00
|
|
|
f(A, st.ffA1, st.ffA1cemux, st.ffA1cepol, ID(CEA1), st.ffA1rstmux, st.ffArstpol, IdString());
|
|
|
|
cell->setParam(ID(AREG), 2);
|
2019-09-11 19:16:46 -05:00
|
|
|
}
|
|
|
|
else
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(AREG), 1);
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
2019-09-11 18:21:24 -05:00
|
|
|
if (st.ffB2) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &B = cell->connections_.at(ID(B));
|
|
|
|
f(B, st.ffB2, st.ffB2cemux, st.ffB2cepol, ID(CEB2), st.ffB2rstmux, st.ffBrstpol, ID(RSTB));
|
2019-09-11 19:16:46 -05:00
|
|
|
pm.add_siguser(B, cell);
|
|
|
|
if (st.ffB1) {
|
2019-09-19 18:13:22 -05:00
|
|
|
f(B, st.ffB1, st.ffB1cemux, st.ffB1cepol, ID(CEB1), st.ffB1rstmux, st.ffBrstpol, IdString());
|
|
|
|
cell->setParam(ID(BREG), 2);
|
2019-09-11 19:16:46 -05:00
|
|
|
}
|
|
|
|
else
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(BREG), 1);
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
2019-09-06 23:01:36 -05:00
|
|
|
if (st.ffC) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &C = cell->connections_.at(ID(C));
|
|
|
|
f(C, st.ffC, st.ffCcemux, st.ffCcepol, ID(CEC), st.ffCrstmux, st.ffCrstpol, ID(RSTC));
|
2019-09-11 13:46:21 -05:00
|
|
|
pm.add_siguser(C, cell);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(CREG), 1);
|
2019-09-06 23:01:36 -05:00
|
|
|
}
|
2019-09-06 17:32:26 -05:00
|
|
|
if (st.ffD) {
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &D = cell->connections_.at(ID(D));
|
|
|
|
f(D, st.ffD, st.ffDcemux, st.ffDcepol, ID(CED), st.ffDrstmux, st.ffDrstpol, ID(RSTD));
|
2019-09-11 13:46:21 -05:00
|
|
|
pm.add_siguser(D, cell);
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setParam(ID(DREG), 1);
|
2019-09-06 17:32:26 -05:00
|
|
|
}
|
2019-08-30 17:00:56 -05:00
|
|
|
if (st.ffM) {
|
2019-09-11 13:46:21 -05:00
|
|
|
SigSpec M; // unused
|
2019-09-19 18:13:22 -05:00
|
|
|
f(M, st.ffM, st.ffMcemux, st.ffMcepol, ID(CEM), st.ffMrstmux, st.ffMrstpol, ID(RSTM));
|
|
|
|
st.ffM->connections_.at(ID(Q)).replace(st.sigM, pm.module->addWire(NEW_ID, GetSize(st.sigM)));
|
|
|
|
cell->setParam(ID(MREG), State::S1);
|
2019-08-30 17:00:56 -05:00
|
|
|
}
|
2019-07-16 16:06:32 -05:00
|
|
|
if (st.ffP) {
|
2019-09-11 13:46:21 -05:00
|
|
|
SigSpec P; // unused
|
2019-09-19 18:13:22 -05:00
|
|
|
f(P, st.ffP, st.ffPcemux, st.ffPcepol, ID(CEP), st.ffPrstmux, st.ffPrstpol, ID(RSTP));
|
|
|
|
st.ffP->connections_.at(ID(Q)).replace(st.sigP, pm.module->addWire(NEW_ID, GetSize(st.sigP)));
|
|
|
|
cell->setParam(ID(PREG), State::S1);
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
log(" clock: %s (%s)", log_signal(st.clock), "posedge");
|
|
|
|
|
2019-09-11 19:16:46 -05:00
|
|
|
if (st.ffA2) {
|
2019-09-11 18:21:24 -05:00
|
|
|
log(" ffA2:%s", log_id(st.ffA2));
|
2019-09-11 19:16:46 -05:00
|
|
|
if (st.ffA1)
|
|
|
|
log(" ffA1:%s", log_id(st.ffA1));
|
|
|
|
}
|
2019-07-15 16:46:31 -05:00
|
|
|
|
2019-09-06 16:10:12 -05:00
|
|
|
if (st.ffAD)
|
|
|
|
log(" ffAD:%s", log_id(st.ffAD));
|
|
|
|
|
2019-09-11 19:16:46 -05:00
|
|
|
if (st.ffB2) {
|
2019-09-11 18:21:24 -05:00
|
|
|
log(" ffB2:%s", log_id(st.ffB2));
|
2019-09-11 19:16:46 -05:00
|
|
|
if (st.ffB1)
|
|
|
|
log(" ffB1:%s", log_id(st.ffB1));
|
|
|
|
}
|
2019-07-15 16:46:31 -05:00
|
|
|
|
2019-09-06 23:01:36 -05:00
|
|
|
if (st.ffC)
|
|
|
|
log(" ffC:%s", log_id(st.ffC));
|
|
|
|
|
|
|
|
if (st.ffD)
|
|
|
|
log(" ffD:%s", log_id(st.ffD));
|
|
|
|
|
2019-09-05 13:46:38 -05:00
|
|
|
if (st.ffM)
|
|
|
|
log(" ffM:%s", log_id(st.ffM));
|
|
|
|
|
2019-07-16 16:06:32 -05:00
|
|
|
if (st.ffP)
|
2019-08-08 18:33:37 -05:00
|
|
|
log(" ffP:%s", log_id(st.ffP));
|
2019-07-15 16:46:31 -05:00
|
|
|
|
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
|
2019-09-11 12:55:45 -05:00
|
|
|
SigSpec P = st.sigP;
|
2019-08-09 17:19:33 -05:00
|
|
|
if (GetSize(P) < 48)
|
|
|
|
P.append(pm.module->addWire(NEW_ID, 48-GetSize(P)));
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(P), P);
|
2019-08-09 17:19:33 -05:00
|
|
|
|
2019-09-11 15:48:45 -05:00
|
|
|
bit_to_driver.insert(std::make_pair(P[0], cell));
|
|
|
|
bit_to_driver.insert(std::make_pair(P[17], cell));
|
|
|
|
|
2019-07-16 16:06:32 -05:00
|
|
|
pm.blacklist(cell);
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:23:07 -05:00
|
|
|
struct XilinxDspPass : public Pass {
|
2019-09-10 18:33:13 -05:00
|
|
|
XilinxDspPass() : Pass("xilinx_dsp", "Xilinx: pack resources into DSPs") { }
|
2019-07-15 16:46:31 -05:00
|
|
|
void help() YS_OVERRIDE
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" xilinx_dsp [options] [selection]\n");
|
|
|
|
log("\n");
|
2019-09-11 19:16:46 -05:00
|
|
|
log("Pack input registers (A2, A1, B2, B1, C, D, AD; with optional enable/reset),\n");
|
|
|
|
log("pipeline registers (M; with optional enable/reset), output registers (P; with\n");
|
|
|
|
log("optional enable/reset), pre-adder and/or post-adder into Xilinx DSP resources.\n");
|
2019-09-10 18:33:13 -05:00
|
|
|
log("\n");
|
|
|
|
log("Multiply-accumulate operations using the post-adder with feedback on the 'C'\n");
|
|
|
|
log("input will be folded into the DSP. In this scenario only, the 'C' input can be\n");
|
|
|
|
log("used to override the existing accumulation result with a new value.\n");
|
|
|
|
log("\n");
|
2019-09-11 19:16:46 -05:00
|
|
|
log("Use of the dedicated 'PCOUT' -> 'PCIN' cascade path is detected for 'P' -> 'C'\n");
|
|
|
|
log("connections (optionally, where 'P' is right-shifted by 18-bits and used as an\n");
|
|
|
|
log("input to the post-adder -- a pattern common for summing partial products to\n");
|
|
|
|
log("implement wide multipliers).\n");
|
2019-09-10 18:33:13 -05:00
|
|
|
log("\n");
|
|
|
|
log("\n");
|
|
|
|
log("Experimental feature: addition/subtractions less than 12 or 24 bits with the\n");
|
|
|
|
log("'(* use_dsp=\"simd\" *)' attribute attached to the output wire or attached to\n");
|
|
|
|
log("the add/subtract operator will cause those operations to be implemented using\n");
|
|
|
|
log("the 'SIMD' feature of DSPs.\n");
|
2019-07-15 16:46:31 -05:00
|
|
|
log("\n");
|
2019-09-18 14:35:24 -05:00
|
|
|
log("Experimental feature: the presence of a `$ge' cell attached to the registered\n");
|
|
|
|
log("P output implementing the operation \"(P >= <power-of-2>)\" will be transformed\n");
|
|
|
|
log("into using the DSP48E1's pattern detector feature for overflow detection.\n");
|
|
|
|
log("\n");
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
|
|
|
{
|
2019-09-10 18:33:13 -05:00
|
|
|
log_header(design, "Executing XILINX_DSP pass (pack resources into DSPs).\n");
|
2019-07-15 16:46:31 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2019-08-13 19:11:35 -05:00
|
|
|
for (auto module : design->selected_modules()) {
|
2019-09-09 22:57:20 -05:00
|
|
|
pack_xilinx_simd(module, module->selected_cells());
|
|
|
|
|
2019-08-13 19:11:35 -05:00
|
|
|
xilinx_dsp_pm pm(module, module->selected_cells());
|
|
|
|
dict<SigBit, Cell*> bit_to_driver;
|
|
|
|
auto f = [&bit_to_driver](xilinx_dsp_pm &pm){ pack_xilinx_dsp(bit_to_driver, pm); };
|
|
|
|
pm.run_xilinx_dsp(f);
|
|
|
|
|
2019-09-11 15:48:45 -05:00
|
|
|
auto &unextend = pm.ud_xilinx_dsp.unextend;
|
2019-08-13 19:11:35 -05:00
|
|
|
// Look for ability to convert C input from another DSP into PCIN
|
|
|
|
// NB: Needs to be done after pattern matcher has folded all
|
|
|
|
// $add cells into the DSP
|
|
|
|
for (auto cell : module->cells()) {
|
2019-09-19 18:13:22 -05:00
|
|
|
if (cell->type != ID(DSP48E1))
|
2019-08-13 19:11:35 -05:00
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
if (cell->parameters.at(ID(CREG), State::S1).as_bool())
|
2019-09-06 23:01:36 -05:00
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec &opmode = cell->connections_.at(ID(OPMODE));
|
2019-08-13 19:11:35 -05:00
|
|
|
if (opmode.extract(4,3) != Const::from_string("011"))
|
|
|
|
continue;
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec C = unextend(pm.sigmap(cell->getPort(ID(C))));
|
2019-09-11 15:48:45 -05:00
|
|
|
if (!C[0].wire)
|
2019-08-13 19:11:35 -05:00
|
|
|
continue;
|
|
|
|
auto it = bit_to_driver.find(C[0]);
|
|
|
|
if (it == bit_to_driver.end())
|
|
|
|
continue;
|
|
|
|
auto driver = it->second;
|
|
|
|
|
2019-09-19 18:13:22 -05:00
|
|
|
SigSpec P = driver->getPort(ID(P));
|
2019-09-11 15:48:45 -05:00
|
|
|
if (GetSize(P) >= GetSize(C) && P.extract(0, GetSize(C)) == C) {
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(C), Const(0, 48));
|
2019-09-11 15:48:45 -05:00
|
|
|
Wire *cascade = module->addWire(NEW_ID, 48);
|
2019-09-19 18:13:22 -05:00
|
|
|
driver->setPort(ID(PCOUT), cascade);
|
|
|
|
cell->setPort(ID(PCIN), cascade);
|
2019-09-11 15:48:45 -05:00
|
|
|
opmode[6] = State::S0;
|
|
|
|
opmode[5] = State::S0;
|
|
|
|
opmode[4] = State::S1;
|
|
|
|
bit_to_driver.erase(it);
|
|
|
|
}
|
|
|
|
else if (GetSize(P) >= GetSize(C)+17 && P.extract(17, GetSize(C)) == C) {
|
2019-09-19 18:13:22 -05:00
|
|
|
cell->setPort(ID(C), Const(0, 48));
|
2019-08-13 19:11:35 -05:00
|
|
|
Wire *cascade = module->addWire(NEW_ID, 48);
|
2019-09-19 18:13:22 -05:00
|
|
|
driver->setPort(ID(PCOUT), cascade);
|
|
|
|
cell->setPort(ID(PCIN), cascade);
|
2019-08-13 19:11:35 -05:00
|
|
|
opmode[6] = State::S1;
|
|
|
|
opmode[5] = State::S0;
|
|
|
|
opmode[4] = State::S1;
|
|
|
|
bit_to_driver.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 16:46:31 -05:00
|
|
|
}
|
2019-08-13 12:23:07 -05:00
|
|
|
} XilinxDspPass;
|
2019-07-15 16:46:31 -05:00
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|