2019-01-13 03:57:11 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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 "kernel/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2019-07-19 12:57:32 -05:00
|
|
|
template<class T> bool includes(const T &lhs, const T &rhs) {
|
|
|
|
return std::includes(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
|
|
|
}
|
2019-04-30 03:51:51 -05:00
|
|
|
#include "passes/pmgen/ice40_dsp_pm.h"
|
|
|
|
|
2019-02-17 08:35:48 -06:00
|
|
|
void create_ice40_dsp(ice40_dsp_pm &pm)
|
|
|
|
{
|
2019-04-29 06:02:05 -05:00
|
|
|
auto &st = pm.st_ice40_dsp;
|
|
|
|
|
2019-07-23 16:21:45 -05:00
|
|
|
#if 0
|
2019-02-17 08:35:48 -06:00
|
|
|
log("\n");
|
2019-07-22 18:12:57 -05:00
|
|
|
log("ffA: %s\n", log_id(st.ffA, "--"));
|
|
|
|
log("ffB: %s\n", log_id(st.ffB, "--"));
|
|
|
|
log("mul: %s\n", log_id(st.mul, "--"));
|
|
|
|
log("ffH: %s\n", log_id(st.ffH, "--"));
|
|
|
|
log("addAB: %s\n", log_id(st.addAB, "--"));
|
|
|
|
log("muxAB: %s\n", log_id(st.muxAB, "--"));
|
|
|
|
log("ffO_lo: %s\n", log_id(st.ffO_lo, "--"));
|
|
|
|
log("ffO_hi: %s\n", log_id(st.ffO_hi, "--"));
|
2019-02-17 08:35:48 -06:00
|
|
|
#endif
|
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
log("Checking %s.%s for iCE40 DSP inference.\n", log_id(pm.module), log_id(st.mul));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
if (GetSize(st.sigA) > 16) {
|
|
|
|
log(" input A (%s) is too large (%d > 16).\n", log_signal(st.sigA), GetSize(st.sigA));
|
2019-02-17 08:35:48 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
if (GetSize(st.sigB) > 16) {
|
|
|
|
log(" input B (%s) is too large (%d > 16).\n", log_signal(st.sigB), GetSize(st.sigB));
|
2019-02-17 08:35:48 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-22 17:08:26 -05:00
|
|
|
if (GetSize(st.sigO) > 32) {
|
|
|
|
log(" accumulator (%s) is too large (%d > 32).\n", log_signal(st.sigO), GetSize(st.sigO));
|
2019-02-17 08:35:48 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-22 17:08:26 -05:00
|
|
|
if (GetSize(st.sigH) > 32) {
|
|
|
|
log(" output (%s) is too large (%d > 32).\n", log_signal(st.sigH), GetSize(st.sigH));
|
2019-02-17 08:35:48 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-19 12:38:13 -05:00
|
|
|
log(" replacing %s with SB_MAC16 cell.\n", log_id(st.mul->type));
|
2019-02-20 04:18:19 -06:00
|
|
|
|
2019-02-17 08:35:48 -06:00
|
|
|
Cell *cell = pm.module->addCell(NEW_ID, "\\SB_MAC16");
|
2019-04-29 06:02:05 -05:00
|
|
|
pm.module->swap_names(cell, st.mul);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
// SB_MAC16 Input Interface
|
2019-07-19 12:38:13 -05:00
|
|
|
bool a_signed = st.mul->getParam("\\A_SIGNED").as_bool();
|
|
|
|
bool b_signed = st.mul->getParam("\\B_SIGNED").as_bool();
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
SigSpec A = st.sigA;
|
2019-07-19 12:38:13 -05:00
|
|
|
A.extend_u0(16, a_signed);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
SigSpec B = st.sigB;
|
2019-07-19 12:38:13 -05:00
|
|
|
B.extend_u0(16, b_signed);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-07-23 16:20:34 -05:00
|
|
|
SigSpec CD = st.sigCD;
|
|
|
|
CD.extend_u0(32, st.sigCD_signed);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
cell->setPort("\\A", A);
|
|
|
|
cell->setPort("\\B", B);
|
2019-07-22 15:01:49 -05:00
|
|
|
cell->setPort("\\C", CD.extract(16, 16));
|
|
|
|
cell->setPort("\\D", CD.extract(0, 16));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
cell->setParam("\\A_REG", st.ffA ? State::S1 : State::S0);
|
|
|
|
cell->setParam("\\B_REG", st.ffB ? State::S1 : State::S0);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
cell->setPort("\\AHOLD", State::S0);
|
|
|
|
cell->setPort("\\BHOLD", State::S0);
|
|
|
|
cell->setPort("\\CHOLD", State::S0);
|
|
|
|
cell->setPort("\\DHOLD", State::S0);
|
|
|
|
|
|
|
|
cell->setPort("\\IRSTTOP", State::S0);
|
|
|
|
cell->setPort("\\IRSTBOT", State::S0);
|
|
|
|
|
2019-07-19 12:57:32 -05:00
|
|
|
if (st.clock != SigBit())
|
2019-02-17 08:35:48 -06:00
|
|
|
{
|
2019-04-29 06:02:05 -05:00
|
|
|
cell->setPort("\\CLK", st.clock);
|
2019-02-17 08:35:48 -06:00
|
|
|
cell->setPort("\\CE", State::S1);
|
2019-04-29 06:02:05 -05:00
|
|
|
cell->setParam("\\NEG_TRIGGER", st.clock_pol ? State::S0 : State::S1);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
log(" clock: %s (%s)", log_signal(st.clock), st.clock_pol ? "posedge" : "negedge");
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
if (st.ffA)
|
|
|
|
log(" ffA:%s", log_id(st.ffA));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
if (st.ffB)
|
|
|
|
log(" ffB:%s", log_id(st.ffB));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-07-22 17:08:26 -05:00
|
|
|
if (st.ffH)
|
|
|
|
log(" ffH:%s", log_id(st.ffH));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-07-22 18:12:57 -05:00
|
|
|
if (st.ffO_lo)
|
|
|
|
log(" ffO_lo:%s", log_id(st.ffO_lo));
|
|
|
|
if (st.ffO_hi)
|
|
|
|
log(" ffO_hi:%s", log_id(st.ffO_hi));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell->setPort("\\CLK", State::S0);
|
|
|
|
cell->setPort("\\CE", State::S0);
|
|
|
|
cell->setParam("\\NEG_TRIGGER", State::S0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SB_MAC16 Cascade Interface
|
|
|
|
|
|
|
|
cell->setPort("\\SIGNEXTIN", State::Sx);
|
|
|
|
cell->setPort("\\SIGNEXTOUT", pm.module->addWire(NEW_ID));
|
|
|
|
|
|
|
|
cell->setPort("\\CI", State::Sx);
|
|
|
|
cell->setPort("\\CO", pm.module->addWire(NEW_ID));
|
|
|
|
|
|
|
|
cell->setPort("\\ACCUMCI", State::Sx);
|
|
|
|
cell->setPort("\\ACCUMCO", pm.module->addWire(NEW_ID));
|
|
|
|
|
|
|
|
// SB_MAC16 Output Interface
|
|
|
|
|
2019-07-23 16:20:34 -05:00
|
|
|
cell->setPort("\\O", st.sigO);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-07-22 17:05:16 -05:00
|
|
|
bool accum = false;
|
2019-04-29 06:02:05 -05:00
|
|
|
if (st.addAB) {
|
2019-07-23 16:20:34 -05:00
|
|
|
if (st.addA)
|
|
|
|
accum = (st.ffO_lo && st.ffO_hi && st.addAB->getPort("\\B") == st.sigO);
|
|
|
|
else if (st.addB)
|
|
|
|
accum = (st.ffO_lo && st.ffO_hi && st.addAB->getPort("\\A") == st.sigO);
|
|
|
|
else log_abort();
|
|
|
|
if (accum)
|
|
|
|
log(" accumulator %s (%s)\n", log_id(st.addAB), log_id(st.addAB->type));
|
|
|
|
else
|
|
|
|
log(" adder %s (%s)\n", log_id(st.addAB), log_id(st.addAB->type));
|
2019-04-29 06:02:05 -05:00
|
|
|
cell->setPort("\\ADDSUBTOP", st.addAB->type == "$add" ? State::S0 : State::S1);
|
|
|
|
cell->setPort("\\ADDSUBBOT", st.addAB->type == "$add" ? State::S0 : State::S1);
|
2019-02-17 08:35:48 -06:00
|
|
|
} else {
|
|
|
|
cell->setPort("\\ADDSUBTOP", State::S0);
|
|
|
|
cell->setPort("\\ADDSUBBOT", State::S0);
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:42:27 -06:00
|
|
|
cell->setPort("\\ORSTTOP", State::S0);
|
|
|
|
cell->setPort("\\ORSTBOT", State::S0);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
cell->setPort("\\OHOLDTOP", State::S0);
|
|
|
|
cell->setPort("\\OHOLDBOT", State::S0);
|
|
|
|
|
|
|
|
SigSpec acc_reset = State::S0;
|
2019-04-29 06:02:05 -05:00
|
|
|
if (st.muxA)
|
|
|
|
acc_reset = st.muxA->getPort("\\S");
|
|
|
|
if (st.muxB)
|
|
|
|
acc_reset = pm.module->Not(NEW_ID, st.muxB->getPort("\\S"));
|
2019-02-17 08:35:48 -06:00
|
|
|
|
|
|
|
cell->setPort("\\OLOADTOP", acc_reset);
|
|
|
|
cell->setPort("\\OLOADBOT", acc_reset);
|
|
|
|
|
|
|
|
// SB_MAC16 Remaining Parameters
|
|
|
|
|
|
|
|
cell->setParam("\\C_REG", State::S0);
|
|
|
|
cell->setParam("\\D_REG", State::S0);
|
|
|
|
|
2019-07-22 17:08:26 -05:00
|
|
|
cell->setParam("\\TOP_8x8_MULT_REG", st.ffH ? State::S1 : State::S0);
|
|
|
|
cell->setParam("\\BOT_8x8_MULT_REG", st.ffH ? State::S1 : State::S0);
|
|
|
|
cell->setParam("\\PIPELINE_16x16_MULT_REG1", st.ffH ? State::S1 : State::S0);
|
2019-02-20 09:42:27 -06:00
|
|
|
cell->setParam("\\PIPELINE_16x16_MULT_REG2", State::S0);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-07-22 18:12:57 -05:00
|
|
|
cell->setParam("\\TOPOUTPUT_SELECT", Const(st.ffO_hi ? 1 : (st.addAB ? 0 : 3), 2));
|
2019-02-17 08:35:48 -06:00
|
|
|
cell->setParam("\\TOPADDSUB_LOWERINPUT", Const(2, 2));
|
2019-07-22 17:05:16 -05:00
|
|
|
cell->setParam("\\TOPADDSUB_UPPERINPUT", accum ? State::S0 : State::S1);
|
2019-02-17 08:35:48 -06:00
|
|
|
cell->setParam("\\TOPADDSUB_CARRYSELECT", Const(3, 2));
|
|
|
|
|
2019-07-22 18:12:57 -05:00
|
|
|
cell->setParam("\\BOTOUTPUT_SELECT", Const(st.ffO_lo ? 1 : (st.addAB ? 0 : 3), 2));
|
2019-02-17 08:35:48 -06:00
|
|
|
cell->setParam("\\BOTADDSUB_LOWERINPUT", Const(2, 2));
|
2019-07-22 17:05:16 -05:00
|
|
|
cell->setParam("\\BOTADDSUB_UPPERINPUT", accum ? State::S0 : State::S1);
|
2019-02-17 08:35:48 -06:00
|
|
|
cell->setParam("\\BOTADDSUB_CARRYSELECT", Const(0, 2));
|
|
|
|
|
|
|
|
cell->setParam("\\MODE_8x8", State::S0);
|
2019-07-19 12:38:13 -05:00
|
|
|
cell->setParam("\\A_SIGNED", a_signed);
|
|
|
|
cell->setParam("\\B_SIGNED", b_signed);
|
2019-02-17 08:35:48 -06:00
|
|
|
|
2019-04-29 06:02:05 -05:00
|
|
|
pm.autoremove(st.mul);
|
2019-07-22 17:08:26 -05:00
|
|
|
pm.autoremove(st.ffH);
|
2019-07-22 15:01:49 -05:00
|
|
|
pm.autoremove(st.addAB);
|
2019-07-23 16:20:34 -05:00
|
|
|
if (st.ffO_lo) {
|
|
|
|
SigSpec O = st.sigO.extract(0,16);
|
|
|
|
st.ffO_lo->connections_.at("\\Q").replace(O, pm.module->addWire(NEW_ID, GetSize(O)));
|
|
|
|
}
|
|
|
|
if (st.ffO_hi) {
|
|
|
|
SigSpec O = st.sigO.extract(16,16);
|
|
|
|
st.ffO_hi->connections_.at("\\Q").replace(O, pm.module->addWire(NEW_ID, GetSize(O)));
|
|
|
|
}
|
2019-02-17 08:35:48 -06:00
|
|
|
}
|
|
|
|
|
2019-01-13 03:57:11 -06:00
|
|
|
struct Ice40DspPass : public Pass {
|
|
|
|
Ice40DspPass() : Pass("ice40_dsp", "iCE40: map multipliers") { }
|
|
|
|
void help() YS_OVERRIDE
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" ice40_dsp [options] [selection]\n");
|
|
|
|
log("\n");
|
2019-02-17 08:35:48 -06:00
|
|
|
log("Map multipliers and multiply-accumulate blocks to iCE40 DSP resources.\n");
|
2019-01-13 03:57:11 -06:00
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
|
|
|
{
|
|
|
|
log_header(design, "Executing ICE40_DSP pass (map multipliers).\n");
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
// if (args[argidx] == "-singleton") {
|
|
|
|
// singleton_mode = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
|
|
|
for (auto module : design->selected_modules())
|
2019-04-29 06:02:05 -05:00
|
|
|
ice40_dsp_pm(module, module->selected_cells()).run_ice40_dsp(create_ice40_dsp);
|
2019-01-13 03:57:11 -06:00
|
|
|
}
|
|
|
|
} Ice40DspPass;
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|