2016-03-30 03:07:20 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2017-05-22 21:39:55 -05:00
|
|
|
* Copyright (C) 2017 Clifford Wolf <clifford@clifford.at>
|
2016-03-30 03:07:20 -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"
|
|
|
|
#include "kernel/modtools.h"
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//get the list of cells hooked up to at least one bit of a given net
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> get_other_cells(const RTLIL::SigSpec& port, ModIndex& index, Cell* src)
|
2016-03-30 03:07:20 -05:00
|
|
|
{
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> rval;
|
2016-03-30 03:07:20 -05:00
|
|
|
for(auto b : port)
|
|
|
|
{
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(b);
|
|
|
|
for(auto x : ports)
|
|
|
|
{
|
|
|
|
if(x.cell == src)
|
|
|
|
continue;
|
|
|
|
rval.insert(x.cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
//return true if there is a full-width bus connection from cell a port ap to cell b port bp
|
|
|
|
//if other_conns_allowed is false, then we require a strict point to point connection (no other links)
|
|
|
|
bool is_full_bus(
|
|
|
|
const RTLIL::SigSpec& sig,
|
|
|
|
ModIndex& index,
|
|
|
|
Cell* a,
|
|
|
|
RTLIL::IdString ap,
|
|
|
|
Cell* b,
|
|
|
|
RTLIL::IdString bp,
|
|
|
|
bool other_conns_allowed = false)
|
2016-03-30 03:07:20 -05:00
|
|
|
{
|
|
|
|
for(auto s : sig)
|
|
|
|
{
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(s);
|
|
|
|
bool found_a = false;
|
|
|
|
bool found_b = false;
|
|
|
|
for(auto x : ports)
|
|
|
|
{
|
|
|
|
if( (x.cell == a) && (x.port == ap) )
|
|
|
|
found_a = true;
|
|
|
|
else if( (x.cell == b) && (x.port == bp) )
|
|
|
|
found_b = true;
|
2016-03-30 23:54:23 -05:00
|
|
|
else if(!other_conns_allowed)
|
2016-03-30 03:07:20 -05:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 03:07:20 -05:00
|
|
|
if( (!found_a) || (!found_b) )
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 03:07:20 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//return true if the signal connects to one port only (nothing on the other end)
|
|
|
|
bool is_unconnected(const RTLIL::SigSpec& port, ModIndex& index)
|
|
|
|
{
|
|
|
|
for(auto b : port)
|
|
|
|
{
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(b);
|
|
|
|
if(ports.size() > 1)
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 03:07:20 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
struct CounterExtraction
|
|
|
|
{
|
2017-05-22 21:39:55 -05:00
|
|
|
int width; //counter width
|
2020-02-17 04:08:57 -06:00
|
|
|
bool count_is_up; //count up (else down)
|
2017-05-22 21:39:55 -05:00
|
|
|
RTLIL::Wire* rwire; //the register output
|
|
|
|
bool has_reset; //true if we have a reset
|
2017-09-13 12:58:41 -05:00
|
|
|
bool has_ce; //true if we have a clock enable
|
2020-02-17 03:02:25 -06:00
|
|
|
bool ce_inverted; //true if clock enable is active low
|
2017-05-22 21:39:55 -05:00
|
|
|
RTLIL::SigSpec rst; //reset pin
|
2017-09-13 17:32:20 -05:00
|
|
|
bool rst_inverted; //true if reset is active low
|
2017-09-13 17:57:17 -05:00
|
|
|
bool rst_to_max; //true if we reset to max instead of 0
|
2017-05-22 21:39:55 -05:00
|
|
|
int count_value; //value we count from
|
2017-09-13 12:58:41 -05:00
|
|
|
RTLIL::SigSpec ce; //clock signal
|
|
|
|
RTLIL::SigSpec clk; //clock enable, if any
|
2020-02-17 02:44:46 -06:00
|
|
|
RTLIL::SigSpec outsig; //counter overflow output signal
|
|
|
|
RTLIL::SigSpec poutsig; //counter parallel output signal
|
|
|
|
bool has_pout; //whether parallel output is used
|
2017-05-22 21:39:55 -05:00
|
|
|
RTLIL::Cell* count_mux; //counter mux
|
|
|
|
RTLIL::Cell* count_reg; //counter register
|
2020-02-17 04:08:57 -06:00
|
|
|
RTLIL::Cell* overflow_cell; //cell for counter overflow (either inverter reduction or $eq)
|
2017-05-22 21:39:55 -05:00
|
|
|
pool<ModIndex::PortInfo> pouts; //Ports that take a parallel output from us
|
2016-04-01 20:07:59 -05:00
|
|
|
};
|
|
|
|
|
2020-02-17 02:06:50 -06:00
|
|
|
struct CounterExtractionSettings
|
|
|
|
{
|
|
|
|
pool<RTLIL::IdString>& parallel_cells;
|
|
|
|
int maxwidth;
|
|
|
|
int minwidth;
|
2020-02-17 02:11:06 -06:00
|
|
|
bool allow_arst;
|
2020-02-17 04:08:57 -06:00
|
|
|
int allowed_dirs; //0 = down, 1 = up, 2 = both
|
2020-02-17 02:06:50 -06:00
|
|
|
};
|
|
|
|
|
2017-05-22 21:39:55 -05:00
|
|
|
//attempt to extract a counter centered on the given adder cell
|
2017-08-28 23:48:20 -05:00
|
|
|
//For now we only support DOWN counters.
|
|
|
|
//TODO: up/down support
|
2017-08-29 00:13:36 -05:00
|
|
|
int counter_tryextract(
|
|
|
|
ModIndex& index,
|
|
|
|
Cell *cell,
|
|
|
|
CounterExtraction& extract,
|
2020-02-17 02:06:50 -06:00
|
|
|
CounterExtractionSettings settings)
|
2016-03-30 03:07:20 -05:00
|
|
|
{
|
2016-03-30 23:54:23 -05:00
|
|
|
SigMap& sigmap = index.sigmap;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
//Both inputs must be unsigned, so don't extract anything with a signed input
|
2020-04-02 11:51:32 -05:00
|
|
|
bool a_sign = cell->getParam(ID::A_SIGNED).as_bool();
|
|
|
|
bool b_sign = cell->getParam(ID::B_SIGNED).as_bool();
|
2016-03-30 23:54:23 -05:00
|
|
|
if(a_sign || b_sign)
|
2016-04-01 20:07:59 -05:00
|
|
|
return 3;
|
2016-03-30 23:54:23 -05:00
|
|
|
|
|
|
|
//CO and X must be unconnected (exactly one connection to each port)
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!is_unconnected(sigmap(cell->getPort(ID::CO)), index))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 7;
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!is_unconnected(sigmap(cell->getPort(ID::X)), index))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 8;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
//true if $alu is performing A - B, else A + B
|
|
|
|
bool alu_is_subtract;
|
|
|
|
|
|
|
|
//BI and CI must be both constant 0 or both constant 1 as well
|
2020-04-02 11:51:32 -05:00
|
|
|
const RTLIL::SigSpec bi_port = sigmap(cell->getPort(ID::BI));
|
|
|
|
const RTLIL::SigSpec ci_port = sigmap(cell->getPort(ID::CI));
|
2020-02-17 04:08:57 -06:00
|
|
|
if(bi_port.is_fully_const() && bi_port.as_int() == 1 &&
|
|
|
|
ci_port.is_fully_const() && ci_port.as_int() == 1)
|
|
|
|
{
|
|
|
|
alu_is_subtract = true;
|
|
|
|
}
|
|
|
|
else if(bi_port.is_fully_const() && bi_port.as_int() == 0 &&
|
|
|
|
ci_port.is_fully_const() && ci_port.as_int() == 0)
|
|
|
|
{
|
|
|
|
alu_is_subtract = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
//false -> port B connects to value
|
|
|
|
//true -> port A connects to value
|
|
|
|
bool alu_port_use_a = false;
|
|
|
|
|
|
|
|
if(alu_is_subtract)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
const int a_width = cell->getParam(ID::A_WIDTH).as_int();
|
|
|
|
const int b_width = cell->getParam(ID::B_WIDTH).as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
const RTLIL::SigSpec b_port = sigmap(cell->getPort(ID::B));
|
|
|
|
|
|
|
|
// down, cnt <= cnt - 1
|
|
|
|
if (b_width == 1 && b_port.is_fully_const() && b_port.as_int() == 1)
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = true;
|
|
|
|
extract.count_is_up = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// up, cnt <= cnt - -1
|
|
|
|
else if (b_width == a_width && b_port.is_fully_const() && b_port.is_fully_ones())
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = true;
|
|
|
|
extract.count_is_up = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ???
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
const int a_width = cell->getParam(ID::A_WIDTH).as_int();
|
|
|
|
const int b_width = cell->getParam(ID::B_WIDTH).as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
const RTLIL::SigSpec a_port = sigmap(cell->getPort(ID::A));
|
|
|
|
const RTLIL::SigSpec b_port = sigmap(cell->getPort(ID::B));
|
|
|
|
|
|
|
|
// down, cnt <= cnt + -1
|
|
|
|
if (b_width == a_width && b_port.is_fully_const() && b_port.is_fully_ones())
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = true;
|
|
|
|
extract.count_is_up = false;
|
|
|
|
}
|
|
|
|
else if (a_width == b_width && a_port.is_fully_const() && a_port.is_fully_ones())
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = false;
|
|
|
|
extract.count_is_up = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// up, cnt <= cnt + 1
|
|
|
|
else if (b_width == 1 && b_port.is_fully_const() && b_port.as_int() == 1)
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = true;
|
|
|
|
extract.count_is_up = true;
|
|
|
|
}
|
|
|
|
else if (a_width == 1 && a_port.is_fully_const() && a_port.as_int() == 1)
|
|
|
|
{
|
|
|
|
// OK
|
|
|
|
alu_port_use_a = false;
|
|
|
|
extract.count_is_up = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ???
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extract.count_is_up && settings.allowed_dirs == 0)
|
|
|
|
return 26;
|
|
|
|
if (!extract.count_is_up && settings.allowed_dirs == 1)
|
|
|
|
return 26;
|
|
|
|
|
|
|
|
//Check if counter is an appropriate size
|
|
|
|
int count_width;
|
|
|
|
if (alu_port_use_a)
|
2020-04-02 11:51:32 -05:00
|
|
|
count_width = cell->getParam(ID::A_WIDTH).as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
else
|
2020-04-02 11:51:32 -05:00
|
|
|
count_width = cell->getParam(ID::B_WIDTH).as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
extract.width = count_width;
|
|
|
|
if( (count_width < settings.minwidth) || (count_width > settings.maxwidth) )
|
|
|
|
return 1;
|
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
//Y must have exactly one connection, and it has to be a $mux cell.
|
|
|
|
//We must have a direct bus connection from our Y to their A.
|
2019-08-15 16:50:10 -05:00
|
|
|
const RTLIL::SigSpec aluy = sigmap(cell->getPort(ID::Y));
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> y_loads = get_other_cells(aluy, index, cell);
|
|
|
|
if(y_loads.size() != 1)
|
2016-04-01 20:07:59 -05:00
|
|
|
return 9;
|
2016-03-30 23:54:23 -05:00
|
|
|
Cell* count_mux = *y_loads.begin();
|
2016-04-01 20:07:59 -05:00
|
|
|
extract.count_mux = count_mux;
|
2019-08-15 12:05:08 -05:00
|
|
|
if(count_mux->type != ID($mux))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 10;
|
2019-08-15 16:50:10 -05:00
|
|
|
if(!is_full_bus(aluy, index, cell, ID::Y, count_mux, ID::A))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 11;
|
2016-03-30 23:54:23 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
if (extract.count_is_up)
|
|
|
|
{
|
|
|
|
//B connection of the mux must be 0
|
|
|
|
const RTLIL::SigSpec underflow = sigmap(count_mux->getPort(ID::B));
|
|
|
|
if(!(underflow.is_fully_const() && underflow.is_fully_zero()))
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//B connection of the mux is our underflow value
|
|
|
|
const RTLIL::SigSpec underflow = sigmap(count_mux->getPort(ID::B));
|
|
|
|
if(!underflow.is_fully_const())
|
|
|
|
return 12;
|
|
|
|
extract.count_value = underflow.as_int();
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
//S connection of the mux must come from an inverter if down, eq if up
|
|
|
|
//(need not be the only load)
|
2020-04-02 11:51:32 -05:00
|
|
|
const RTLIL::SigSpec muxsel = sigmap(count_mux->getPort(ID::S));
|
2016-04-01 20:07:59 -05:00
|
|
|
extract.outsig = muxsel;
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> muxsel_conns = get_other_cells(muxsel, index, count_mux);
|
2020-02-17 04:08:57 -06:00
|
|
|
Cell* overflow_cell = NULL;
|
2016-03-30 23:54:23 -05:00
|
|
|
for(auto c : muxsel_conns)
|
2017-04-08 22:54:31 -05:00
|
|
|
{
|
2020-02-17 04:08:57 -06:00
|
|
|
if(extract.count_is_up && c->type != ID($eq))
|
|
|
|
continue;
|
|
|
|
if(!extract.count_is_up && c->type != ID($logic_not))
|
2016-03-30 23:54:23 -05:00
|
|
|
continue;
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!is_full_bus(muxsel, index, c, ID::Y, count_mux, ID::S, true))
|
2016-03-30 23:54:23 -05:00
|
|
|
continue;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
overflow_cell = c;
|
2016-03-30 23:54:23 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-02-17 04:08:57 -06:00
|
|
|
if(overflow_cell == NULL)
|
2016-04-01 20:07:59 -05:00
|
|
|
return 13;
|
2020-02-17 04:08:57 -06:00
|
|
|
extract.overflow_cell = overflow_cell;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2017-09-13 12:58:41 -05:00
|
|
|
//Y connection of the mux must have exactly one load, the counter's internal register, if there's no clock enable
|
|
|
|
//If we have a clock enable, Y drives the B input of a mux. A of that mux must come from our register
|
2019-08-15 16:50:10 -05:00
|
|
|
const RTLIL::SigSpec muxy = sigmap(count_mux->getPort(ID::Y));
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> muxy_loads = get_other_cells(muxy, index, count_mux);
|
|
|
|
if(muxy_loads.size() != 1)
|
2016-04-01 20:07:59 -05:00
|
|
|
return 14;
|
2017-09-13 12:58:41 -05:00
|
|
|
Cell* muxload = *muxy_loads.begin();
|
|
|
|
Cell* count_reg = muxload;
|
|
|
|
Cell* cemux = NULL;
|
|
|
|
RTLIL::SigSpec cey;
|
2019-08-15 12:05:08 -05:00
|
|
|
if(muxload->type == ID($mux))
|
2017-09-13 12:58:41 -05:00
|
|
|
{
|
|
|
|
//This mux is probably a clock enable mux.
|
|
|
|
//Find our count register (should be our only load)
|
|
|
|
cemux = muxload;
|
2019-08-15 16:50:10 -05:00
|
|
|
cey = sigmap(cemux->getPort(ID::Y));
|
2017-09-13 12:58:41 -05:00
|
|
|
pool<Cell*> cey_loads = get_other_cells(cey, index, cemux);
|
|
|
|
if(cey_loads.size() != 1)
|
|
|
|
return 24;
|
|
|
|
count_reg = *cey_loads.begin();
|
|
|
|
|
2020-04-02 11:51:32 -05:00
|
|
|
if(sigmap(cemux->getPort(ID::Y)) != sigmap(count_reg->getPort(ID::D)))
|
2017-09-13 12:58:41 -05:00
|
|
|
return 24;
|
2020-02-17 03:02:25 -06:00
|
|
|
//Mux should have A driven by count Q, and B by muxy
|
|
|
|
//if A and B are swapped, CE polarity is inverted
|
|
|
|
if(sigmap(cemux->getPort(ID::B)) == muxy &&
|
2020-04-02 11:51:32 -05:00
|
|
|
sigmap(cemux->getPort(ID::A)) == sigmap(count_reg->getPort(ID::Q)))
|
2020-02-17 03:02:25 -06:00
|
|
|
{
|
|
|
|
extract.ce_inverted = false;
|
|
|
|
}
|
|
|
|
else if(sigmap(cemux->getPort(ID::A)) == muxy &&
|
2020-04-02 11:51:32 -05:00
|
|
|
sigmap(cemux->getPort(ID::B)) == sigmap(count_reg->getPort(ID::Q)))
|
2020-02-17 03:02:25 -06:00
|
|
|
{
|
|
|
|
extract.ce_inverted = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 24;
|
|
|
|
}
|
2017-09-13 12:58:41 -05:00
|
|
|
|
|
|
|
//Select of the mux is our clock enable
|
|
|
|
extract.has_ce = true;
|
2020-04-02 11:51:32 -05:00
|
|
|
extract.ce = sigmap(cemux->getPort(ID::S));
|
2017-09-13 12:58:41 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
extract.has_ce = false;
|
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
extract.count_reg = count_reg;
|
2019-08-15 12:05:08 -05:00
|
|
|
if(count_reg->type == ID($dff))
|
2016-04-01 20:07:59 -05:00
|
|
|
extract.has_reset = false;
|
2019-08-15 12:05:08 -05:00
|
|
|
else if(count_reg->type == ID($adff))
|
2016-04-01 20:07:59 -05:00
|
|
|
{
|
2020-02-17 02:11:06 -06:00
|
|
|
if (!settings.allow_arst)
|
|
|
|
return 25;
|
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
extract.has_reset = true;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2017-09-13 17:57:17 -05:00
|
|
|
//Check polarity of reset - we may have to add an inverter later on!
|
2020-04-02 11:51:32 -05:00
|
|
|
extract.rst_inverted = (count_reg->getParam(ID::ARST_POLARITY).as_int() != 1);
|
2017-09-13 17:57:17 -05:00
|
|
|
|
|
|
|
//Verify ARST_VALUE is zero or full scale
|
2020-04-02 11:51:32 -05:00
|
|
|
int rst_value = count_reg->getParam(ID::ARST_VALUE).as_int();
|
2017-09-13 17:57:17 -05:00
|
|
|
if(rst_value == 0)
|
|
|
|
extract.rst_to_max = false;
|
|
|
|
else if(rst_value == extract.count_value)
|
|
|
|
extract.rst_to_max = true;
|
|
|
|
else
|
2016-04-01 20:07:59 -05:00
|
|
|
return 23;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Save the reset
|
2020-04-02 11:51:32 -05:00
|
|
|
extract.rst = sigmap(count_reg->getPort(ID::ARST));
|
2016-04-01 20:07:59 -05:00
|
|
|
}
|
|
|
|
//TODO: support synchronous reset
|
|
|
|
else
|
|
|
|
return 15;
|
2017-09-13 12:58:41 -05:00
|
|
|
|
|
|
|
//Sanity check that we use the ALU output properly
|
|
|
|
if(extract.has_ce)
|
|
|
|
{
|
2020-02-17 03:02:25 -06:00
|
|
|
if(!extract.ce_inverted && !is_full_bus(muxy, index, count_mux, ID::Y, cemux, ID::B))
|
|
|
|
return 16;
|
|
|
|
if(extract.ce_inverted && !is_full_bus(muxy, index, count_mux, ID::Y, cemux, ID::A))
|
2017-09-13 12:58:41 -05:00
|
|
|
return 16;
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!is_full_bus(cey, index, cemux, ID::Y, count_reg, ID::D))
|
2017-09-13 12:58:41 -05:00
|
|
|
return 16;
|
|
|
|
}
|
2020-04-02 11:51:32 -05:00
|
|
|
else if(!is_full_bus(muxy, index, count_mux, ID::Y, count_reg, ID::D))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 16;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//TODO: Verify count_reg CLK_POLARITY is 1
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
//Register output must have exactly two loads, the inverter and ALU
|
2017-05-22 21:39:55 -05:00
|
|
|
//(unless we have a parallel output!)
|
2017-09-13 12:58:41 -05:00
|
|
|
//If we have a clock enable, 3 is OK
|
2020-04-02 11:51:32 -05:00
|
|
|
const RTLIL::SigSpec qport = count_reg->getPort(ID::Q);
|
2020-02-17 02:44:46 -06:00
|
|
|
extract.poutsig = qport;
|
|
|
|
extract.has_pout = false;
|
2017-05-22 21:39:55 -05:00
|
|
|
const RTLIL::SigSpec cnout = sigmap(qport);
|
2016-03-30 23:54:23 -05:00
|
|
|
pool<Cell*> cnout_loads = get_other_cells(cnout, index, count_reg);
|
2017-09-13 12:58:41 -05:00
|
|
|
unsigned int max_loads = 2;
|
|
|
|
if(extract.has_ce)
|
|
|
|
max_loads = 3;
|
|
|
|
if(cnout_loads.size() > max_loads)
|
2017-05-22 21:39:55 -05:00
|
|
|
{
|
2017-09-14 12:18:49 -05:00
|
|
|
for(auto c : cnout_loads)
|
2017-05-22 21:39:55 -05:00
|
|
|
{
|
2020-02-17 04:08:57 -06:00
|
|
|
if(c == overflow_cell)
|
2017-09-14 12:18:49 -05:00
|
|
|
continue;
|
|
|
|
if(c == cell)
|
|
|
|
continue;
|
|
|
|
if(c == muxload)
|
|
|
|
continue;
|
2017-08-28 23:48:20 -05:00
|
|
|
|
2017-09-14 12:18:49 -05:00
|
|
|
//If we specified a limited set of cells for parallel output, check that we only drive them
|
2020-02-17 02:06:50 -06:00
|
|
|
if(!settings.parallel_cells.empty())
|
2017-09-14 12:18:49 -05:00
|
|
|
{
|
2017-08-28 23:48:20 -05:00
|
|
|
//Make sure we're in the whitelist
|
2020-02-17 02:06:50 -06:00
|
|
|
if( settings.parallel_cells.find(c->type) == settings.parallel_cells.end())
|
2017-08-28 23:48:20 -05:00
|
|
|
return 17;
|
2017-09-14 12:18:49 -05:00
|
|
|
}
|
2017-08-28 23:48:20 -05:00
|
|
|
|
2017-09-14 12:18:49 -05:00
|
|
|
//Figure out what port(s) are driven by it
|
|
|
|
//TODO: this can probably be done more efficiently w/o multiple iterations over our whole net?
|
2020-02-17 02:44:46 -06:00
|
|
|
//TODO: For what purpose do we actually need extract.pouts?
|
2017-09-14 12:18:49 -05:00
|
|
|
for(auto b : qport)
|
|
|
|
{
|
|
|
|
pool<ModIndex::PortInfo> ports = index.query_ports(b);
|
|
|
|
for(auto x : ports)
|
2017-05-22 21:39:55 -05:00
|
|
|
{
|
2017-09-14 12:18:49 -05:00
|
|
|
if(x.cell != c)
|
|
|
|
continue;
|
|
|
|
extract.pouts.insert(ModIndex::PortInfo(c, x.port, 0));
|
2020-02-17 02:44:46 -06:00
|
|
|
extract.has_pout = true;
|
2017-05-22 21:39:55 -05:00
|
|
|
}
|
2017-08-28 23:48:20 -05:00
|
|
|
}
|
2017-05-22 21:39:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-17 02:44:46 -06:00
|
|
|
for (auto b : qport)
|
|
|
|
{
|
|
|
|
if(index.query_is_output(b))
|
|
|
|
{
|
|
|
|
// Parallel out goes out of module
|
|
|
|
extract.has_pout = true;
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 04:08:57 -06:00
|
|
|
if(!extract.count_is_up)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!is_full_bus(cnout, index, count_reg, ID::Q, overflow_cell, ID::A, true))
|
2020-02-17 04:08:57 -06:00
|
|
|
return 18;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
if(is_full_bus(cnout, index, count_reg, ID::Q, overflow_cell, ID::A, true))
|
2020-02-17 04:08:57 -06:00
|
|
|
{
|
|
|
|
// B must be the overflow value
|
|
|
|
const RTLIL::SigSpec overflow = sigmap(overflow_cell->getPort(ID::B));
|
|
|
|
if(!overflow.is_fully_const())
|
|
|
|
return 12;
|
|
|
|
extract.count_value = overflow.as_int();
|
|
|
|
}
|
2020-04-02 11:51:32 -05:00
|
|
|
else if(is_full_bus(cnout, index, count_reg, ID::Q, overflow_cell, ID::B, true))
|
2020-02-17 04:08:57 -06:00
|
|
|
{
|
|
|
|
// A must be the overflow value
|
|
|
|
const RTLIL::SigSpec overflow = sigmap(overflow_cell->getPort(ID::A));
|
|
|
|
if(!overflow.is_fully_const())
|
|
|
|
return 12;
|
|
|
|
extract.count_value = overflow.as_int();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 18;
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 11:51:32 -05:00
|
|
|
if(alu_port_use_a && !is_full_bus(cnout, index, count_reg, ID::Q, cell, ID::A, true))
|
2020-02-17 04:08:57 -06:00
|
|
|
return 19;
|
2020-04-02 11:51:32 -05:00
|
|
|
if(!alu_port_use_a && !is_full_bus(cnout, index, count_reg, ID::Q, cell, ID::B, true))
|
2016-04-01 20:07:59 -05:00
|
|
|
return 19;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-31 00:40:14 -05:00
|
|
|
//Look up the clock from the register
|
2020-04-02 11:51:32 -05:00
|
|
|
extract.clk = sigmap(count_reg->getPort(ID::CLK));
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
if(!extract.count_is_up)
|
|
|
|
{
|
|
|
|
//Register output net must have an INIT attribute equal to the count value
|
|
|
|
extract.rwire = cnout.as_wire();
|
2020-04-02 11:51:32 -05:00
|
|
|
if(extract.rwire->attributes.find(ID::init) == extract.rwire->attributes.end())
|
2020-02-17 04:08:57 -06:00
|
|
|
return 20;
|
2020-04-02 11:51:32 -05:00
|
|
|
int rinit = extract.rwire->attributes[ID::init].as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
if(rinit != extract.count_value)
|
|
|
|
return 21;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Register output net must not have an INIT attribute or it must be zero
|
|
|
|
extract.rwire = cnout.as_wire();
|
2020-04-02 11:51:32 -05:00
|
|
|
if(extract.rwire->attributes.find(ID::init) == extract.rwire->attributes.end())
|
2020-02-17 04:08:57 -06:00
|
|
|
return 0;
|
2020-04-02 11:51:32 -05:00
|
|
|
int rinit = extract.rwire->attributes[ID::init].as_int();
|
2020-02-17 04:08:57 -06:00
|
|
|
if(rinit != 0)
|
|
|
|
return 21;
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-28 22:52:08 -05:00
|
|
|
void counter_worker(
|
2016-04-01 20:07:59 -05:00
|
|
|
ModIndex& index,
|
|
|
|
Cell *cell,
|
|
|
|
unsigned int& total_counters,
|
2017-06-24 16:54:07 -05:00
|
|
|
pool<Cell*>& cells_to_remove,
|
2017-08-28 23:48:20 -05:00
|
|
|
pool<pair<Cell*, string>>& cells_to_rename,
|
2020-02-17 02:06:50 -06:00
|
|
|
CounterExtractionSettings settings)
|
2016-04-01 20:07:59 -05:00
|
|
|
{
|
|
|
|
SigMap& sigmap = index.sigmap;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Core of the counter must be an ALU
|
2019-08-15 12:05:08 -05:00
|
|
|
if (cell->type != ID($alu))
|
2016-03-30 23:54:23 -05:00
|
|
|
return;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-07 01:42:22 -05:00
|
|
|
//A input is the count value. Check if it has COUNT_EXTRACT set.
|
|
|
|
//If it's not a wire, don't even try
|
2019-08-15 16:50:10 -05:00
|
|
|
auto port = sigmap(cell->getPort(ID::A));
|
2016-04-07 01:42:22 -05:00
|
|
|
if(!port.is_wire())
|
2020-02-17 04:08:57 -06:00
|
|
|
{
|
|
|
|
port = sigmap(cell->getPort(ID::B));
|
|
|
|
if(!port.is_wire())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RTLIL::Wire* port_wire = port.as_wire();
|
2016-04-01 20:07:59 -05:00
|
|
|
bool force_extract = false;
|
|
|
|
bool never_extract = false;
|
2020-04-02 11:51:32 -05:00
|
|
|
string count_reg_src = port_wire->attributes[ID::src].decode_string().c_str();
|
2020-02-17 04:08:57 -06:00
|
|
|
if(port_wire->attributes.find(ID(COUNT_EXTRACT)) != port_wire->attributes.end())
|
2016-04-01 20:07:59 -05:00
|
|
|
{
|
2020-02-17 04:08:57 -06:00
|
|
|
pool<string> sa = port_wire->get_strpool_attribute(ID(COUNT_EXTRACT));
|
2016-04-01 20:07:59 -05:00
|
|
|
string extract_value;
|
|
|
|
if(sa.size() >= 1)
|
|
|
|
{
|
|
|
|
extract_value = *sa.begin();
|
|
|
|
log(" Signal %s declared at %s has COUNT_EXTRACT = %s\n",
|
2020-02-17 04:08:57 -06:00
|
|
|
log_id(port_wire),
|
2016-04-01 20:07:59 -05:00
|
|
|
count_reg_src.c_str(),
|
|
|
|
extract_value.c_str());
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
if(extract_value == "FORCE")
|
|
|
|
force_extract = true;
|
|
|
|
else if(extract_value == "NO")
|
|
|
|
never_extract = true;
|
|
|
|
else if(extract_value == "AUTO")
|
|
|
|
{} //default
|
|
|
|
else
|
|
|
|
log_error(" Illegal COUNT_EXTRACT value %s (must be one of FORCE, NO, AUTO)\n",
|
|
|
|
extract_value.c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//If we're explicitly told not to extract, don't infer a counter
|
|
|
|
if(never_extract)
|
|
|
|
return;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Attempt to extract a counter
|
|
|
|
CounterExtraction extract;
|
2020-02-17 02:06:50 -06:00
|
|
|
int reason = counter_tryextract(index, cell, extract, settings);
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Nonzero code - we could not find a matchable counter.
|
|
|
|
//Do nothing, unless extraction was forced in which case give an error
|
|
|
|
if(reason != 0)
|
|
|
|
{
|
2020-02-17 02:11:06 -06:00
|
|
|
static const char* reasons[]=
|
2016-04-01 20:07:59 -05:00
|
|
|
{
|
|
|
|
"no problem", //0
|
2017-08-28 23:48:20 -05:00
|
|
|
"counter is too large/small", //1
|
2016-04-01 20:07:59 -05:00
|
|
|
"counter does not count by one", //2
|
|
|
|
"counter uses signed math", //3
|
2020-02-17 04:08:57 -06:00
|
|
|
"RESERVED, not implemented", //4
|
|
|
|
"ALU is not an adder/subtractor", //5
|
|
|
|
"RESERVED, not implemented", //6
|
2016-04-01 20:07:59 -05:00
|
|
|
"ALU ports used outside counter", //7
|
|
|
|
"ALU ports used outside counter", //8
|
|
|
|
"ALU output used outside counter", //9
|
|
|
|
"ALU output is not a mux", //10
|
|
|
|
"ALU output is not full bus", //11
|
|
|
|
"Underflow value is not constant", //12
|
|
|
|
"No underflow detector found", //13
|
|
|
|
"Mux output is used outside counter", //14
|
|
|
|
"Counter reg is not DFF/ADFF", //15
|
|
|
|
"Counter input is not full bus", //16
|
2017-08-28 23:48:20 -05:00
|
|
|
"Count register is used outside counter, but not by an allowed cell", //17
|
2016-04-01 20:07:59 -05:00
|
|
|
"Register output is not full bus", //18
|
|
|
|
"Register output is not full bus", //19
|
|
|
|
"No init value found", //20
|
|
|
|
"Underflow value is not equal to init value", //21
|
2017-09-13 17:32:20 -05:00
|
|
|
"RESERVED, not implemented", //22, kept for compatibility but not used anymore
|
2017-09-13 17:57:17 -05:00
|
|
|
"Reset is not to zero or COUNT_TO", //23
|
2020-02-17 02:11:06 -06:00
|
|
|
"Clock enable configuration is unsupported", //24
|
2020-02-17 04:08:57 -06:00
|
|
|
"Async reset used but not permitted", //25
|
|
|
|
"Count direction is not allowed" //26
|
2016-04-01 20:07:59 -05:00
|
|
|
};
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
if(force_extract)
|
|
|
|
{
|
|
|
|
log_error(
|
|
|
|
"Counter extraction is set to FORCE on register %s, but a counter could not be inferred (%s)\n",
|
2020-02-17 04:08:57 -06:00
|
|
|
log_id(port_wire),
|
2016-04-01 20:07:59 -05:00
|
|
|
reasons[reason]);
|
|
|
|
}
|
2016-03-30 23:54:23 -05:00
|
|
|
return;
|
2016-04-01 20:07:59 -05:00
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2017-06-24 16:54:07 -05:00
|
|
|
//Get new cell name
|
2017-08-30 18:27:18 -05:00
|
|
|
string countname = string("$COUNTx$") + log_id(extract.rwire->name.str());
|
2017-06-24 16:54:07 -05:00
|
|
|
|
2016-03-31 00:40:14 -05:00
|
|
|
//Wipe all of the old connections to the ALU
|
2019-08-15 16:50:10 -05:00
|
|
|
cell->unsetPort(ID::A);
|
|
|
|
cell->unsetPort(ID::B);
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->unsetPort(ID::BI);
|
|
|
|
cell->unsetPort(ID::CI);
|
|
|
|
cell->unsetPort(ID::CO);
|
|
|
|
cell->unsetPort(ID::X);
|
2019-08-15 16:50:10 -05:00
|
|
|
cell->unsetPort(ID::Y);
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->unsetParam(ID::A_SIGNED);
|
|
|
|
cell->unsetParam(ID::A_WIDTH);
|
|
|
|
cell->unsetParam(ID::B_SIGNED);
|
|
|
|
cell->unsetParam(ID::B_WIDTH);
|
|
|
|
cell->unsetParam(ID::Y_WIDTH);
|
2016-04-02 01:39:32 -05:00
|
|
|
|
2016-03-31 00:40:14 -05:00
|
|
|
//Change the cell type
|
2019-08-15 12:05:08 -05:00
|
|
|
cell->type = ID($__COUNT_);
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Hook up resets
|
|
|
|
if(extract.has_reset)
|
|
|
|
{
|
|
|
|
//TODO: support other kinds of reset
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(RESET_MODE), RTLIL::Const("LEVEL"));
|
2017-09-13 17:32:20 -05:00
|
|
|
|
|
|
|
//If the reset is active low, infer an inverter ($__COUNT_ cells always have active high reset)
|
|
|
|
if(extract.rst_inverted)
|
|
|
|
{
|
|
|
|
auto realreset = cell->module->addWire(NEW_ID);
|
|
|
|
cell->module->addNot(NEW_ID, extract.rst, RTLIL::SigSpec(realreset));
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setPort(ID(RST), realreset);
|
2017-09-13 17:32:20 -05:00
|
|
|
}
|
|
|
|
else
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setPort(ID(RST), extract.rst);
|
2016-04-01 20:07:59 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(RESET_MODE), RTLIL::Const("RISING"));
|
|
|
|
cell->setPort(ID(RST), RTLIL::SigSpec(false));
|
2016-04-01 20:07:59 -05:00
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Hook up other stuff
|
2019-08-15 12:25:54 -05:00
|
|
|
//cell->setParam(ID(CLKIN_DIVIDE), RTLIL::Const(1));
|
|
|
|
cell->setParam(ID(COUNT_TO), RTLIL::Const(extract.count_value));
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setParam(ID::WIDTH, RTLIL::Const(extract.width));
|
|
|
|
cell->setPort(ID::CLK, extract.clk);
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setPort(ID(OUT), extract.outsig);
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2017-09-13 12:58:41 -05:00
|
|
|
//Hook up clock enable
|
|
|
|
if(extract.has_ce)
|
|
|
|
{
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(HAS_CE), RTLIL::Const(1));
|
2020-02-17 03:02:25 -06:00
|
|
|
if(extract.ce_inverted)
|
|
|
|
{
|
|
|
|
auto realce = cell->module->addWire(NEW_ID);
|
|
|
|
cell->module->addNot(NEW_ID, extract.ce, RTLIL::SigSpec(realce));
|
|
|
|
cell->setPort(ID(CE), realce);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cell->setPort(ID(CE), extract.ce);
|
2017-09-13 12:58:41 -05:00
|
|
|
}
|
|
|
|
else
|
2020-02-17 02:54:33 -06:00
|
|
|
{
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(HAS_CE), RTLIL::Const(0));
|
2020-02-17 02:54:33 -06:00
|
|
|
cell->setPort(ID(CE), RTLIL::Const(1));
|
|
|
|
}
|
2017-09-13 12:58:41 -05:00
|
|
|
|
2020-02-17 04:08:57 -06:00
|
|
|
if(extract.count_is_up)
|
|
|
|
{
|
|
|
|
cell->setParam(ID(DIRECTION), RTLIL::Const("UP"));
|
|
|
|
//XXX: What is this supposed to do?
|
|
|
|
cell->setPort(ID(UP), RTLIL::Const(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell->setParam(ID(DIRECTION), RTLIL::Const("DOWN"));
|
|
|
|
cell->setPort(ID(UP), RTLIL::Const(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Hook up hard-wired ports, default to no parallel output
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(HAS_POUT), RTLIL::Const(0));
|
|
|
|
cell->setParam(ID(RESET_TO_MAX), RTLIL::Const(0));
|
2017-08-28 23:48:20 -05:00
|
|
|
|
2017-05-22 21:39:55 -05:00
|
|
|
//Hook up any parallel outputs
|
|
|
|
for(auto load : extract.pouts)
|
|
|
|
{
|
|
|
|
log(" Counter has parallel output to cell %s port %s\n", log_id(load.cell->name), log_id(load.port));
|
2020-02-17 02:44:46 -06:00
|
|
|
}
|
|
|
|
if(extract.has_pout)
|
|
|
|
{
|
2017-05-22 21:39:55 -05:00
|
|
|
//Connect it to our parallel output
|
2020-02-17 02:44:46 -06:00
|
|
|
cell->setPort(ID(POUT), extract.poutsig);
|
2019-08-15 12:25:54 -05:00
|
|
|
cell->setParam(ID(HAS_POUT), RTLIL::Const(1));
|
2017-05-22 21:39:55 -05:00
|
|
|
}
|
|
|
|
|
2016-03-31 00:40:14 -05:00
|
|
|
//Delete the cells we've replaced (let opt_clean handle deleting the now-redundant wires)
|
2016-04-01 20:07:59 -05:00
|
|
|
cells_to_remove.insert(extract.count_mux);
|
|
|
|
cells_to_remove.insert(extract.count_reg);
|
2020-02-17 04:08:57 -06:00
|
|
|
cells_to_remove.insert(extract.overflow_cell);
|
2017-06-24 16:54:07 -05:00
|
|
|
|
2017-08-30 20:14:22 -05:00
|
|
|
//Log it
|
|
|
|
total_counters ++;
|
|
|
|
string reset_type = "non-resettable";
|
|
|
|
if(extract.has_reset)
|
|
|
|
{
|
2017-09-14 12:18:49 -05:00
|
|
|
if(extract.rst_inverted)
|
|
|
|
reset_type = "negative";
|
|
|
|
else
|
|
|
|
reset_type = "positive";
|
|
|
|
|
2017-08-30 20:14:22 -05:00
|
|
|
//TODO: support other kind of reset
|
2017-09-14 12:18:49 -05:00
|
|
|
reset_type += " async resettable";
|
2017-08-30 20:14:22 -05:00
|
|
|
}
|
2020-02-17 04:08:57 -06:00
|
|
|
log(" Found %d-bit (%s) %s counter %s (counting %s %d) for register %s, declared at %s\n",
|
2017-08-30 20:14:22 -05:00
|
|
|
extract.width,
|
|
|
|
reset_type.c_str(),
|
2020-02-17 04:08:57 -06:00
|
|
|
extract.count_is_up ? "up" : "down",
|
2017-08-30 20:14:22 -05:00
|
|
|
countname.c_str(),
|
2020-02-17 04:08:57 -06:00
|
|
|
extract.count_is_up ? "to" : "from",
|
2017-08-30 20:14:22 -05:00
|
|
|
extract.count_value,
|
|
|
|
log_id(extract.rwire->name),
|
|
|
|
count_reg_src.c_str());
|
|
|
|
|
|
|
|
//Optimize the counter
|
|
|
|
//If we have no parallel output, and we have redundant bits, shrink us
|
2020-02-17 02:44:46 -06:00
|
|
|
if(!extract.has_pout)
|
2017-08-30 20:14:22 -05:00
|
|
|
{
|
|
|
|
//TODO: Need to update this when we add support for counters with nonzero reset values
|
|
|
|
//to make sure the reset value fits in our bit space too
|
|
|
|
|
|
|
|
//Optimize it
|
|
|
|
int newbits = ceil(log2(extract.count_value));
|
|
|
|
if(extract.width != newbits)
|
|
|
|
{
|
2020-04-02 11:51:32 -05:00
|
|
|
cell->setParam(ID::WIDTH, RTLIL::Const(newbits));
|
2017-08-30 20:14:22 -05:00
|
|
|
log(" Optimizing out %d unused high-order bits (new width is %d)\n",
|
|
|
|
extract.width - newbits,
|
|
|
|
newbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 16:54:07 -05:00
|
|
|
//Finally, rename the cell
|
|
|
|
cells_to_rename.insert(pair<Cell*, string>(cell, countname));
|
2016-03-30 03:07:20 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 22:52:08 -05:00
|
|
|
struct ExtractCounterPass : public Pass {
|
|
|
|
ExtractCounterPass() : Pass("extract_counter", "Extract GreenPak4 counter cells") { }
|
2018-07-21 01:41:18 -05:00
|
|
|
void help() YS_OVERRIDE
|
2016-03-30 03:07:20 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2017-08-28 22:52:08 -05:00
|
|
|
log(" extract_counter [options] [selection]\n");
|
2016-03-30 03:07:20 -05:00
|
|
|
log("\n");
|
2017-08-28 22:52:08 -05:00
|
|
|
log("This pass converts non-resettable or async resettable down counters to\n");
|
2017-08-28 23:48:20 -05:00
|
|
|
log("counter cells. Use a target-specific 'techmap' map file to convert those cells\n");
|
|
|
|
log("to the actual target cells.\n");
|
|
|
|
log("\n");
|
2017-08-29 00:13:36 -05:00
|
|
|
log(" -maxwidth N\n");
|
2020-02-17 02:06:50 -06:00
|
|
|
log(" Only extract counters up to N bits wide (default 64)\n");
|
|
|
|
log("\n");
|
|
|
|
log(" -minwidth N\n");
|
|
|
|
log(" Only extract counters at least N bits wide (default 2)\n");
|
2017-08-30 18:27:18 -05:00
|
|
|
log("\n");
|
2020-02-17 02:11:06 -06:00
|
|
|
log(" -allow_arst yes|no\n");
|
|
|
|
log(" Allow counters to have async reset (default yes)\n");
|
|
|
|
log("\n");
|
2020-02-17 04:08:57 -06:00
|
|
|
log(" -dir up|down|both\n");
|
|
|
|
log(" Look for up-counters, down-counters, or both (default down)\n");
|
|
|
|
log("\n");
|
2017-08-28 23:48:20 -05:00
|
|
|
log(" -pout X,Y,...\n");
|
|
|
|
log(" Only allow parallel output from the counter to the listed cell types\n");
|
|
|
|
log(" (if not specified, parallel outputs are not restricted)\n");
|
|
|
|
log("\n");
|
2016-03-30 03:07:20 -05:00
|
|
|
log("\n");
|
|
|
|
}
|
2018-07-21 01:41:18 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
2016-03-30 03:07:20 -05:00
|
|
|
{
|
2017-08-28 22:52:08 -05:00
|
|
|
log_header(design, "Executing EXTRACT_COUNTER pass (find counters in netlist).\n");
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 02:06:50 -06:00
|
|
|
pool<RTLIL::IdString> _parallel_cells;
|
|
|
|
CounterExtractionSettings settings
|
|
|
|
{
|
|
|
|
.parallel_cells = _parallel_cells,
|
|
|
|
.maxwidth = 64,
|
|
|
|
.minwidth = 2,
|
2020-02-17 02:11:06 -06:00
|
|
|
.allow_arst = true,
|
2020-02-17 04:08:57 -06:00
|
|
|
.allowed_dirs = 0,
|
2020-02-17 02:06:50 -06:00
|
|
|
};
|
|
|
|
|
2016-03-30 03:07:20 -05:00
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
2017-08-28 23:48:20 -05:00
|
|
|
if (args[argidx] == "-pout")
|
|
|
|
{
|
|
|
|
if(argidx + 1 >= args.size())
|
|
|
|
{
|
|
|
|
log_error("extract_counter -pout requires an argument\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string pouts = args[++argidx];
|
|
|
|
std::string tmp;
|
|
|
|
for(size_t i=0; i<pouts.length(); i++)
|
|
|
|
{
|
|
|
|
if(pouts[i] == ',')
|
|
|
|
{
|
2020-02-17 02:06:50 -06:00
|
|
|
settings.parallel_cells.insert(RTLIL::escape_id(tmp));
|
2017-08-28 23:48:20 -05:00
|
|
|
tmp = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp += pouts[i];
|
|
|
|
}
|
2020-02-17 02:06:50 -06:00
|
|
|
settings.parallel_cells.insert(RTLIL::escape_id(tmp));
|
2017-08-29 00:13:36 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[argidx] == "-maxwidth" && argidx+1 < args.size())
|
|
|
|
{
|
2020-02-17 02:06:50 -06:00
|
|
|
settings.maxwidth = atoi(args[++argidx].c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[argidx] == "-minwidth" && argidx+1 < args.size())
|
|
|
|
{
|
|
|
|
settings.minwidth = atoi(args[++argidx].c_str());
|
2017-08-29 00:13:36 -05:00
|
|
|
continue;
|
2017-08-28 23:48:20 -05:00
|
|
|
}
|
2020-02-17 02:11:06 -06:00
|
|
|
|
|
|
|
if (args[argidx] == "-allow_arst" && argidx+1 < args.size())
|
|
|
|
{
|
2020-02-17 04:08:57 -06:00
|
|
|
auto arg = args[++argidx];
|
|
|
|
if (arg == "yes")
|
|
|
|
settings.allow_arst = true;
|
|
|
|
else if (arg == "no")
|
|
|
|
settings.allow_arst = false;
|
|
|
|
else
|
|
|
|
log_error("Invalid -allow_arst value \"%s\"\n", arg.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[argidx] == "-dir" && argidx+1 < args.size())
|
|
|
|
{
|
|
|
|
auto arg = args[++argidx];
|
|
|
|
if (arg == "up")
|
|
|
|
settings.allowed_dirs = 1;
|
|
|
|
else if (arg == "down")
|
|
|
|
settings.allowed_dirs = 0;
|
|
|
|
else if (arg == "both")
|
|
|
|
settings.allowed_dirs = 2;
|
|
|
|
else
|
|
|
|
log_error("Invalid -dir value \"%s\"\n", arg.c_str());
|
2020-02-17 02:11:06 -06:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-30 03:07:20 -05:00
|
|
|
}
|
|
|
|
extra_args(args, argidx, design);
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2020-02-17 02:06:50 -06:00
|
|
|
if (settings.minwidth < 2)
|
|
|
|
{
|
|
|
|
//A counter with less than 2 bits makes no sense
|
|
|
|
log_warning("Minimum counter width is 2 bits wide\n");
|
|
|
|
settings.minwidth = 2;
|
|
|
|
}
|
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
//Extract all of the counters we could find
|
2016-03-30 23:54:23 -05:00
|
|
|
unsigned int total_counters = 0;
|
|
|
|
for (auto module : design->selected_modules())
|
|
|
|
{
|
2016-04-01 20:07:59 -05:00
|
|
|
pool<Cell*> cells_to_remove;
|
2017-06-24 16:54:07 -05:00
|
|
|
pool<pair<Cell*, string>> cells_to_rename;
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
ModIndex index(module);
|
2016-03-30 03:07:20 -05:00
|
|
|
for (auto cell : module->selected_cells())
|
2020-02-17 02:06:50 -06:00
|
|
|
counter_worker(index, cell, total_counters, cells_to_remove, cells_to_rename, settings);
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-04-01 20:07:59 -05:00
|
|
|
for(auto cell : cells_to_remove)
|
2017-06-24 16:54:07 -05:00
|
|
|
{
|
|
|
|
//log("Removing cell %s\n", log_id(cell->name));
|
2016-04-01 20:07:59 -05:00
|
|
|
module->remove(cell);
|
2017-06-24 16:54:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for(auto cpair : cells_to_rename)
|
|
|
|
{
|
|
|
|
//log("Renaming cell %s to %s\n", log_id(cpair.first->name), cpair.second.c_str());
|
|
|
|
module->rename(cpair.first, cpair.second);
|
|
|
|
}
|
2016-03-30 03:07:20 -05:00
|
|
|
}
|
2017-04-08 22:54:31 -05:00
|
|
|
|
2016-03-30 23:54:23 -05:00
|
|
|
if(total_counters)
|
|
|
|
log("Extracted %u counters\n", total_counters);
|
2016-03-30 03:07:20 -05:00
|
|
|
}
|
2017-08-28 22:52:08 -05:00
|
|
|
} ExtractCounterPass;
|
2016-03-30 03:07:20 -05:00
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|