2019-02-08 16:53:12 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
2019-06-12 11:40:51 -05:00
|
|
|
* 2019 Eddie Hung <eddie@fpgeh.com>
|
2019-02-08 16:53:12 -06: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-06-14 14:40:51 -05:00
|
|
|
// https://stackoverflow.com/a/46137633
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stdlib.h>
|
2019-07-09 11:35:09 -05:00
|
|
|
#define bswap32 _byteswap_ulong
|
2019-06-14 14:40:51 -05:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <libkern/OSByteOrder.h>
|
2019-07-09 11:35:09 -05:00
|
|
|
#define bswap32 OSSwapInt32
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define bswap32 __builtin_bswap32
|
|
|
|
#else
|
2019-06-28 11:59:47 -05:00
|
|
|
#include <cstdint>
|
2019-07-09 11:35:09 -05:00
|
|
|
inline static uint32_t bswap32(uint32_t x)
|
2019-06-28 11:59:47 -05:00
|
|
|
{
|
|
|
|
// https://stackoverflow.com/a/27796212
|
|
|
|
register uint32_t value = number_to_be_reversed;
|
|
|
|
uint8_t lolo = (value >> 0) & 0xFF;
|
|
|
|
uint8_t lohi = (value >> 8) & 0xFF;
|
|
|
|
uint8_t hilo = (value >> 16) & 0xFF;
|
|
|
|
uint8_t hihi = (value >> 24) & 0xFF;
|
|
|
|
return (hihi << 24)
|
|
|
|
| (hilo << 16)
|
|
|
|
| (lohi << 8)
|
|
|
|
| (lolo << 0);
|
|
|
|
}
|
2019-06-14 14:40:51 -05:00
|
|
|
#endif
|
|
|
|
|
2019-02-08 16:53:12 -06:00
|
|
|
#include "kernel/yosys.h"
|
|
|
|
#include "kernel/sigtools.h"
|
2019-04-17 13:08:42 -05:00
|
|
|
#include "kernel/utils.h"
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2019-06-14 14:25:06 -05:00
|
|
|
inline int32_t to_big_endian(int32_t i32) {
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2019-07-31 02:10:24 -05:00
|
|
|
return bswap32(i32);
|
2019-06-14 14:25:06 -05:00
|
|
|
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
return i32;
|
|
|
|
#else
|
|
|
|
#error "Unknown endianness"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:53:12 -06:00
|
|
|
void aiger_encode(std::ostream &f, int x)
|
|
|
|
{
|
|
|
|
log_assert(x >= 0);
|
|
|
|
|
|
|
|
while (x & ~0x7f) {
|
|
|
|
f.put((x & 0x7f) | 0x80);
|
|
|
|
x = x >> 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
f.put(x);
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:18:42 -06:00
|
|
|
struct XAigerWriter
|
2019-02-08 16:53:12 -06:00
|
|
|
{
|
|
|
|
Module *module;
|
|
|
|
SigMap sigmap;
|
|
|
|
|
2020-01-02 17:32:58 -06:00
|
|
|
pool<SigBit> input_bits, output_bits;
|
2019-09-30 17:24:03 -05:00
|
|
|
dict<SigBit, SigBit> not_map, alias_map;
|
2019-02-08 16:53:12 -06:00
|
|
|
dict<SigBit, pair<SigBit, SigBit>> and_map;
|
2019-12-27 17:35:19 -06:00
|
|
|
vector<SigBit> ci_bits, co_bits;
|
2020-01-02 14:36:54 -06:00
|
|
|
dict<SigBit, Cell*> ff_bits;
|
2019-08-19 14:33:24 -05:00
|
|
|
dict<SigBit, float> arrival_times;
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
vector<pair<int, int>> aig_gates;
|
2019-09-30 17:24:03 -05:00
|
|
|
vector<int> aig_outputs;
|
2019-02-08 16:53:12 -06:00
|
|
|
int aig_m = 0, aig_i = 0, aig_l = 0, aig_o = 0, aig_a = 0;
|
|
|
|
|
|
|
|
dict<SigBit, int> aig_map;
|
|
|
|
dict<SigBit, int> ordered_outputs;
|
|
|
|
|
2019-04-12 16:13:11 -05:00
|
|
|
vector<Cell*> box_list;
|
2019-12-31 18:12:40 -06:00
|
|
|
dict<IdString, std::vector<IdString>> box_ports;
|
2019-04-12 16:13:11 -05:00
|
|
|
|
2019-02-08 16:53:12 -06:00
|
|
|
int mkgate(int a0, int a1)
|
|
|
|
{
|
|
|
|
aig_m++, aig_a++;
|
|
|
|
aig_gates.push_back(a0 > a1 ? make_pair(a0, a1) : make_pair(a1, a0));
|
|
|
|
return 2*aig_m;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bit2aig(SigBit bit)
|
|
|
|
{
|
2019-06-21 00:09:13 -05:00
|
|
|
auto it = aig_map.find(bit);
|
|
|
|
if (it != aig_map.end()) {
|
|
|
|
log_assert(it->second >= 0);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-06-21 14:43:20 -05:00
|
|
|
// NB: Cannot use iterator returned from aig_map.insert()
|
|
|
|
// since this function is called recursively
|
|
|
|
|
2019-06-21 00:09:13 -05:00
|
|
|
int a = -1;
|
|
|
|
if (not_map.count(bit)) {
|
|
|
|
a = bit2aig(not_map.at(bit)) ^ 1;
|
|
|
|
} else
|
|
|
|
if (and_map.count(bit)) {
|
|
|
|
auto args = and_map.at(bit);
|
|
|
|
int a0 = bit2aig(args.first);
|
|
|
|
int a1 = bit2aig(args.second);
|
|
|
|
a = mkgate(a0, a1);
|
|
|
|
} else
|
|
|
|
if (alias_map.count(bit)) {
|
|
|
|
a = bit2aig(alias_map.at(bit));
|
|
|
|
}
|
2019-02-08 16:53:12 -06:00
|
|
|
|
2019-06-21 00:09:13 -05:00
|
|
|
if (bit == State::Sx || bit == State::Sz) {
|
2019-06-21 14:46:55 -05:00
|
|
|
log_debug("Design contains 'x' or 'z' bits. Treating as 1'b0.\n");
|
2019-06-21 00:09:13 -05:00
|
|
|
a = aig_map.at(State::S0);
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:09:13 -05:00
|
|
|
log_assert(a >= 0);
|
|
|
|
aig_map[bit] = a;
|
|
|
|
return a;
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
XAigerWriter(Module *module, bool holes_mode=false) : module(module), sigmap(module)
|
2019-02-08 16:53:12 -06:00
|
|
|
{
|
|
|
|
pool<SigBit> undriven_bits;
|
|
|
|
pool<SigBit> unused_bits;
|
|
|
|
|
|
|
|
// promote public wires
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
if (wire->name[0] == '\\')
|
|
|
|
sigmap.add(wire);
|
|
|
|
|
|
|
|
// promote input wires
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
if (wire->port_input)
|
|
|
|
sigmap.add(wire);
|
|
|
|
|
2019-12-06 18:19:10 -06:00
|
|
|
// promote keep wires
|
2019-12-05 19:54:43 -06:00
|
|
|
for (auto wire : module->wires())
|
2019-12-06 18:19:10 -06:00
|
|
|
if (wire->get_bool_attribute(ID::keep))
|
2019-12-05 19:54:43 -06:00
|
|
|
sigmap.add(wire);
|
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
2019-02-08 16:53:12 -06:00
|
|
|
for (int i = 0; i < GetSize(wire); i++)
|
|
|
|
{
|
|
|
|
SigBit wirebit(wire, i);
|
|
|
|
SigBit bit = sigmap(wirebit);
|
|
|
|
|
2019-12-06 18:19:10 -06:00
|
|
|
if (bit.wire == nullptr) {
|
|
|
|
if (wire->port_output) {
|
|
|
|
aig_map[wirebit] = (bit == State::S1) ? 1 : 0;
|
2019-12-31 11:59:17 -06:00
|
|
|
output_bits.insert(wirebit);
|
2019-12-06 18:19:10 -06:00
|
|
|
}
|
2019-12-05 19:54:43 -06:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-02 21:14:30 -05:00
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
undriven_bits.insert(bit);
|
|
|
|
unused_bits.insert(bit);
|
|
|
|
|
2019-12-06 18:19:10 -06:00
|
|
|
if (wire->port_input)
|
|
|
|
input_bits.insert(bit);
|
2019-02-08 16:53:12 -06:00
|
|
|
|
2019-12-05 19:54:43 -06:00
|
|
|
if (wire->port_output) {
|
2019-12-06 18:19:10 -06:00
|
|
|
if (bit != wirebit)
|
|
|
|
alias_map[wirebit] = bit;
|
2019-12-31 11:59:17 -06:00
|
|
|
output_bits.insert(wirebit);
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
2019-12-31 11:59:17 -06:00
|
|
|
}
|
2019-11-27 14:35:25 -06:00
|
|
|
|
2019-06-14 15:34:40 -05:00
|
|
|
// TODO: Speed up toposort -- ultimately we care about
|
|
|
|
// box ordering, but not individual AIG cells
|
2019-06-14 15:28:47 -05:00
|
|
|
dict<SigBit, pool<IdString>> bit_drivers, bit_users;
|
|
|
|
TopoSort<IdString, RTLIL::sort_by_id_str> toposort;
|
2019-10-04 13:04:10 -05:00
|
|
|
bool abc9_box_seen = false;
|
2019-04-17 13:08:42 -05:00
|
|
|
|
2019-06-14 15:07:56 -05:00
|
|
|
for (auto cell : module->selected_cells()) {
|
2019-02-08 16:53:12 -06:00
|
|
|
if (cell->type == "$_NOT_")
|
|
|
|
{
|
|
|
|
SigBit A = sigmap(cell->getPort("\\A").as_bit());
|
|
|
|
SigBit Y = sigmap(cell->getPort("\\Y").as_bit());
|
|
|
|
unused_bits.erase(A);
|
|
|
|
undriven_bits.erase(Y);
|
|
|
|
not_map[Y] = A;
|
2019-12-31 11:59:17 -06:00
|
|
|
toposort.node(cell->name);
|
|
|
|
bit_users[A].insert(cell->name);
|
|
|
|
bit_drivers[Y].insert(cell->name);
|
2019-02-08 16:53:12 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type == "$_AND_")
|
|
|
|
{
|
|
|
|
SigBit A = sigmap(cell->getPort("\\A").as_bit());
|
|
|
|
SigBit B = sigmap(cell->getPort("\\B").as_bit());
|
|
|
|
SigBit Y = sigmap(cell->getPort("\\Y").as_bit());
|
|
|
|
unused_bits.erase(A);
|
|
|
|
unused_bits.erase(B);
|
|
|
|
undriven_bits.erase(Y);
|
|
|
|
and_map[Y] = make_pair(A, B);
|
2019-12-31 11:59:17 -06:00
|
|
|
toposort.node(cell->name);
|
|
|
|
bit_users[A].insert(cell->name);
|
|
|
|
bit_users[B].insert(cell->name);
|
|
|
|
bit_drivers[Y].insert(cell->name);
|
2019-02-08 16:53:12 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-01-02 14:36:54 -06:00
|
|
|
if (cell->type == "$__ABC9_FF_" &&
|
|
|
|
// The presence of an abc9_mergeability attribute indicates
|
|
|
|
// that we do want to pass this flop to ABC
|
|
|
|
cell->attributes.count("\\abc9_mergeability"))
|
2019-08-20 20:17:14 -05:00
|
|
|
{
|
|
|
|
SigBit D = sigmap(cell->getPort("\\D").as_bit());
|
|
|
|
SigBit Q = sigmap(cell->getPort("\\Q").as_bit());
|
|
|
|
unused_bits.erase(D);
|
|
|
|
undriven_bits.erase(Q);
|
|
|
|
alias_map[Q] = D;
|
2020-01-02 14:36:54 -06:00
|
|
|
auto r YS_ATTRIBUTE(unused) = ff_bits.insert(std::make_pair(D, cell));
|
2019-09-29 01:48:17 -05:00
|
|
|
log_assert(r.second);
|
2019-08-20 20:17:14 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:13:17 -05:00
|
|
|
RTLIL::Module* inst_module = module->design->module(cell->type);
|
2019-12-27 13:30:18 -06:00
|
|
|
if (inst_module) {
|
2020-01-01 10:38:23 -06:00
|
|
|
bool abc9_box = inst_module->attributes.count("\\abc9_box_id");
|
|
|
|
bool abc9_flop = inst_module->get_bool_attribute("\\abc9_flop");
|
2020-01-02 14:36:54 -06:00
|
|
|
if (abc9_box && cell->get_bool_attribute("\\abc9_keep"))
|
2020-01-01 10:38:23 -06:00
|
|
|
abc9_box = false;
|
2019-08-20 20:17:14 -05:00
|
|
|
|
|
|
|
for (const auto &conn : cell->connections()) {
|
|
|
|
auto port_wire = inst_module->wire(conn.first);
|
|
|
|
|
2020-01-02 14:36:54 -06:00
|
|
|
if (abc9_box) {
|
|
|
|
// Ignore inout for the sake of topographical ordering
|
|
|
|
if (port_wire->port_input && !port_wire->port_output)
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
bit_users[bit].insert(cell->name);
|
|
|
|
if (port_wire->port_output)
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
bit_drivers[bit].insert(cell->name);
|
|
|
|
|
|
|
|
if (!abc9_flop)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:44:35 -06:00
|
|
|
if (port_wire->port_output) {
|
2019-12-27 17:18:55 -06:00
|
|
|
int arrival = 0;
|
2019-12-17 17:44:35 -06:00
|
|
|
auto it = port_wire->attributes.find("\\abc9_arrival");
|
|
|
|
if (it != port_wire->attributes.end()) {
|
|
|
|
if (it->second.flags != 0)
|
|
|
|
log_error("Attribute 'abc9_arrival' on port '%s' of module '%s' is not an integer.\n", log_id(port_wire), log_id(cell->type));
|
|
|
|
arrival = it->second.as_int();
|
|
|
|
}
|
2019-12-27 17:18:55 -06:00
|
|
|
if (arrival)
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
arrival_times[bit] = arrival;
|
2019-12-27 13:30:18 -06:00
|
|
|
}
|
2019-06-14 15:28:47 -05:00
|
|
|
}
|
2019-09-27 20:41:43 -05:00
|
|
|
|
2020-01-01 10:38:23 -06:00
|
|
|
if (abc9_box) {
|
|
|
|
abc9_box_seen = true;
|
|
|
|
toposort.node(cell->name);
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-19 17:47:36 -05:00
|
|
|
}
|
2019-07-02 21:14:30 -05:00
|
|
|
|
2019-09-27 20:41:43 -05:00
|
|
|
bool cell_known = inst_module || cell->known();
|
|
|
|
for (const auto &c : cell->connections()) {
|
|
|
|
if (c.second.is_fully_const()) continue;
|
|
|
|
auto port_wire = inst_module ? inst_module->wire(c.first) : nullptr;
|
|
|
|
auto is_input = (port_wire && port_wire->port_input) || !cell_known || cell->input(c.first);
|
|
|
|
auto is_output = (port_wire && port_wire->port_output) || !cell_known || cell->output(c.first);
|
|
|
|
if (!is_input && !is_output)
|
|
|
|
log_error("Connection '%s' on cell '%s' (type '%s') not recognised!\n", log_id(c.first), log_id(cell), log_id(cell->type));
|
|
|
|
|
2019-12-31 11:59:17 -06:00
|
|
|
if (is_input)
|
2019-09-27 20:41:43 -05:00
|
|
|
for (auto b : c.second) {
|
|
|
|
Wire *w = b.wire;
|
|
|
|
if (!w) continue;
|
|
|
|
if (!w->port_output || !cell_known) {
|
|
|
|
SigBit I = sigmap(b);
|
|
|
|
if (I != b)
|
|
|
|
alias_map[b] = I;
|
2019-12-31 11:59:17 -06:00
|
|
|
output_bits.insert(b);
|
2019-08-19 15:17:31 -05:00
|
|
|
}
|
2019-09-27 20:41:43 -05:00
|
|
|
}
|
2019-02-15 13:51:21 -06:00
|
|
|
}
|
2019-04-12 16:13:11 -05:00
|
|
|
|
2019-04-12 18:17:48 -05:00
|
|
|
//log_warning("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-10-04 13:04:10 -05:00
|
|
|
if (abc9_box_seen) {
|
2019-04-17 13:08:42 -05:00
|
|
|
for (auto &it : bit_users)
|
|
|
|
if (bit_drivers.count(it.first))
|
|
|
|
for (auto driver_cell : bit_drivers.at(it.first))
|
2019-06-14 15:28:47 -05:00
|
|
|
for (auto user_cell : it.second)
|
|
|
|
toposort.edge(driver_cell, user_cell);
|
|
|
|
|
|
|
|
#if 0
|
2019-06-04 14:01:25 -05:00
|
|
|
toposort.analyze_loops = true;
|
|
|
|
#endif
|
2019-08-11 16:25:46 -05:00
|
|
|
bool no_loops YS_ATTRIBUTE(unused) = toposort.sort();
|
2019-06-14 15:28:47 -05:00
|
|
|
#if 0
|
2019-06-04 14:01:25 -05:00
|
|
|
unsigned i = 0;
|
|
|
|
for (auto &it : toposort.loops) {
|
2019-06-26 22:02:38 -05:00
|
|
|
log(" loop %d\n", i++);
|
|
|
|
for (auto cell_name : it) {
|
|
|
|
auto cell = module->cell(cell_name);
|
|
|
|
log_assert(cell);
|
|
|
|
log("\t%s (%s @ %s)\n", log_id(cell), log_id(cell->type), cell->get_src_attribute().c_str());
|
|
|
|
}
|
2019-06-04 14:01:25 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
log_assert(no_loops);
|
|
|
|
|
2019-04-17 13:08:42 -05:00
|
|
|
for (auto cell_name : toposort.sorted) {
|
|
|
|
RTLIL::Cell *cell = module->cell(cell_name);
|
2019-06-27 18:07:14 -05:00
|
|
|
log_assert(cell);
|
|
|
|
|
2019-04-17 13:08:42 -05:00
|
|
|
RTLIL::Module* box_module = module->design->module(cell->type);
|
2020-01-01 10:38:23 -06:00
|
|
|
if (!box_module || !box_module->attributes.count("\\abc9_box_id"))
|
2019-06-14 15:28:47 -05:00
|
|
|
continue;
|
2019-04-17 13:08:42 -05:00
|
|
|
|
2019-09-23 23:58:40 -05:00
|
|
|
bool blackbox = box_module->get_blackbox_attribute(true /* ignore_wb */);
|
|
|
|
|
2019-12-31 18:12:40 -06:00
|
|
|
auto r = box_ports.insert(cell->type);
|
|
|
|
if (r.second) {
|
|
|
|
// Make carry in the last PI, and carry out the last PO
|
|
|
|
// since ABC requires it this way
|
|
|
|
IdString carry_in, carry_out;
|
|
|
|
for (const auto &port_name : box_module->ports) {
|
|
|
|
auto w = box_module->wire(port_name);
|
|
|
|
log_assert(w);
|
|
|
|
if (w->get_bool_attribute("\\abc9_carry")) {
|
2019-12-31 19:06:03 -06:00
|
|
|
if (w->port_input) {
|
|
|
|
if (carry_in != IdString())
|
|
|
|
log_error("Module '%s' contains more than one 'abc9_carry' input port.\n", log_id(box_module));
|
2019-12-31 18:12:40 -06:00
|
|
|
carry_in = port_name;
|
2019-12-31 19:06:03 -06:00
|
|
|
}
|
|
|
|
if (w->port_output) {
|
|
|
|
if (carry_out != IdString())
|
|
|
|
log_error("Module '%s' contains more than one 'abc9_carry' output port.\n", log_id(box_module));
|
2019-12-31 18:12:40 -06:00
|
|
|
carry_out = port_name;
|
2019-12-31 19:06:03 -06:00
|
|
|
}
|
2019-12-31 18:12:40 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
r.first->second.push_back(port_name);
|
|
|
|
}
|
2019-12-31 19:06:03 -06:00
|
|
|
|
|
|
|
if (carry_in != IdString() && carry_out == IdString())
|
|
|
|
log_error("Module '%s' contains an 'abc9_carry' input port but no output port.\n", log_id(box_module));
|
|
|
|
if (carry_in == IdString() && carry_out != IdString())
|
|
|
|
log_error("Module '%s' contains an 'abc9_carry' output port but no input port.\n", log_id(box_module));
|
2019-12-31 18:12:40 -06:00
|
|
|
if (carry_in != IdString()) {
|
|
|
|
r.first->second.push_back(carry_in);
|
|
|
|
r.first->second.push_back(carry_out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:19:23 -05:00
|
|
|
// Fully pad all unused input connections of this box cell with S0
|
2019-05-26 13:26:38 -05:00
|
|
|
// Fully pad all undriven output connections of this box cell with anonymous wires
|
2019-12-31 18:12:40 -06:00
|
|
|
for (auto port_name : r.first->second) {
|
|
|
|
auto w = box_module->wire(port_name);
|
2019-05-28 01:10:59 -05:00
|
|
|
log_assert(w);
|
|
|
|
auto it = cell->connections_.find(port_name);
|
2019-05-21 18:19:23 -05:00
|
|
|
if (w->port_input) {
|
2019-05-28 01:10:59 -05:00
|
|
|
RTLIL::SigSpec rhs;
|
2019-05-21 18:19:23 -05:00
|
|
|
if (it != cell->connections_.end()) {
|
2019-05-26 13:26:38 -05:00
|
|
|
if (GetSize(it->second) < GetSize(w))
|
2019-08-06 18:23:37 -05:00
|
|
|
it->second.append(RTLIL::SigSpec(State::S0, GetSize(w)-GetSize(it->second)));
|
2019-05-28 01:10:59 -05:00
|
|
|
rhs = it->second;
|
|
|
|
}
|
|
|
|
else {
|
2019-08-06 18:23:37 -05:00
|
|
|
rhs = RTLIL::SigSpec(State::S0, GetSize(w));
|
2019-05-28 01:10:59 -05:00
|
|
|
cell->setPort(port_name, rhs);
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:12:40 -06:00
|
|
|
for (auto b : rhs) {
|
2019-05-28 01:10:59 -05:00
|
|
|
SigBit I = sigmap(b);
|
2019-06-20 12:47:20 -05:00
|
|
|
if (b == RTLIL::Sx)
|
2019-08-06 18:23:37 -05:00
|
|
|
b = State::S0;
|
2019-06-20 12:47:20 -05:00
|
|
|
else if (I != b) {
|
|
|
|
if (I == RTLIL::Sx)
|
2019-08-06 18:23:37 -05:00
|
|
|
alias_map[b] = State::S0;
|
2019-06-20 12:47:20 -05:00
|
|
|
else
|
|
|
|
alias_map[b] = I;
|
|
|
|
}
|
2019-12-27 17:35:19 -06:00
|
|
|
co_bits.emplace_back(b);
|
2019-12-05 19:54:43 -06:00
|
|
|
unused_bits.erase(I);
|
2019-05-21 18:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (w->port_output) {
|
2019-05-28 01:10:59 -05:00
|
|
|
RTLIL::SigSpec rhs;
|
2020-01-08 20:27:09 -06:00
|
|
|
auto it = cell->connections_.find(port_name);
|
2019-05-21 18:19:23 -05:00
|
|
|
if (it != cell->connections_.end()) {
|
2019-05-26 13:26:38 -05:00
|
|
|
if (GetSize(it->second) < GetSize(w))
|
|
|
|
it->second.append(module->addWire(NEW_ID, GetSize(w)-GetSize(it->second)));
|
2019-05-28 01:10:59 -05:00
|
|
|
rhs = it->second;
|
2019-05-21 18:19:23 -05:00
|
|
|
}
|
2019-05-28 01:10:59 -05:00
|
|
|
else {
|
2019-09-23 23:58:40 -05:00
|
|
|
Wire *wire = module->addWire(NEW_ID, GetSize(w));
|
|
|
|
if (blackbox)
|
2019-10-04 13:04:10 -05:00
|
|
|
wire->set_bool_attribute(ID(abc9_padding));
|
2019-09-23 23:58:40 -05:00
|
|
|
rhs = wire;
|
2019-05-28 01:10:59 -05:00
|
|
|
cell->setPort(port_name, rhs);
|
2019-04-17 13:08:42 -05:00
|
|
|
}
|
2019-05-28 01:10:59 -05:00
|
|
|
|
|
|
|
for (const auto &b : rhs.bits()) {
|
2019-04-17 13:08:42 -05:00
|
|
|
SigBit O = sigmap(b);
|
2019-05-28 14:42:17 -05:00
|
|
|
if (O != b)
|
|
|
|
alias_map[O] = b;
|
2019-12-31 18:12:40 -06:00
|
|
|
ci_bits.emplace_back(b);
|
2019-12-06 18:19:10 -06:00
|
|
|
undriven_bits.erase(O);
|
2019-04-17 13:08:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-29 01:48:17 -05:00
|
|
|
|
2020-01-02 14:36:54 -06:00
|
|
|
// Connect <cell>.abc9_ff.Q (inserted by abc9_map.v) as the last input to the flop box
|
2019-09-29 01:48:17 -05:00
|
|
|
if (box_module->get_bool_attribute("\\abc9_flop")) {
|
2020-01-02 14:36:54 -06:00
|
|
|
SigSpec rhs = module->wire(stringf("%s.abc9_ff.Q", cell->name.c_str()));
|
2019-10-05 11:06:13 -05:00
|
|
|
if (rhs.empty())
|
2020-01-02 14:36:54 -06:00
|
|
|
log_error("'%s.abc9_ff.Q' is not a wire present in module '%s'.\n", log_id(cell), log_id(module));
|
2019-09-29 01:48:17 -05:00
|
|
|
|
2019-09-30 18:33:40 -05:00
|
|
|
for (auto b : rhs) {
|
2019-09-29 01:48:17 -05:00
|
|
|
SigBit I = sigmap(b);
|
|
|
|
if (b == RTLIL::Sx)
|
|
|
|
b = State::S0;
|
|
|
|
else if (I != b) {
|
|
|
|
if (I == RTLIL::Sx)
|
|
|
|
alias_map[b] = State::S0;
|
|
|
|
else
|
|
|
|
alias_map[b] = I;
|
|
|
|
}
|
2019-12-30 16:31:42 -06:00
|
|
|
co_bits.emplace_back(b);
|
2019-12-05 19:54:43 -06:00
|
|
|
unused_bits.erase(I);
|
2019-09-29 01:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:08:42 -05:00
|
|
|
box_list.emplace_back(cell);
|
|
|
|
}
|
2019-06-14 15:28:47 -05:00
|
|
|
|
|
|
|
// TODO: Free memory from toposort, bit_drivers, bit_users
|
2019-04-17 13:08:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 18:19:10 -06:00
|
|
|
for (auto bit : input_bits)
|
2020-01-02 19:25:14 -06:00
|
|
|
undriven_bits.erase(bit);
|
2019-12-06 18:19:10 -06:00
|
|
|
for (auto bit : output_bits)
|
|
|
|
unused_bits.erase(sigmap(bit));
|
2019-02-08 16:53:12 -06:00
|
|
|
for (auto bit : unused_bits)
|
2019-11-22 15:24:28 -06:00
|
|
|
undriven_bits.erase(bit);
|
2019-12-31 01:29:14 -06:00
|
|
|
|
|
|
|
// Make all undriven bits a primary input
|
2019-12-31 11:59:17 -06:00
|
|
|
for (auto bit : undriven_bits) {
|
|
|
|
input_bits.insert(bit);
|
|
|
|
undriven_bits.erase(bit);
|
2019-05-26 16:14:13 -05:00
|
|
|
}
|
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
if (holes_mode) {
|
|
|
|
struct sort_by_port_id {
|
|
|
|
bool operator()(const RTLIL::SigBit& a, const RTLIL::SigBit& b) const {
|
2020-01-11 13:49:57 -06:00
|
|
|
return a.wire->port_id < b.wire->port_id ||
|
|
|
|
(a.wire->port_id == b.wire->port_id && a.offset < b.offset);
|
2020-01-03 14:30:22 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
input_bits.sort(sort_by_port_id());
|
|
|
|
output_bits.sort(sort_by_port_id());
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:53:12 -06:00
|
|
|
aig_map[State::S0] = 0;
|
|
|
|
aig_map[State::S1] = 1;
|
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
for (const auto &bit : input_bits) {
|
2019-02-15 13:51:21 -06:00
|
|
|
aig_m++, aig_i++;
|
2019-05-30 18:03:22 -05:00
|
|
|
log_assert(!aig_map.count(bit));
|
2019-04-16 18:37:47 -05:00
|
|
|
aig_map[bit] = 2*aig_m;
|
2019-02-15 13:51:21 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 20:41:43 -05:00
|
|
|
for (const auto &i : ff_bits) {
|
2020-01-02 14:36:54 -06:00
|
|
|
const Cell *cell = i.second;
|
|
|
|
const SigBit &q = sigmap(cell->getPort("\\Q"));
|
2019-08-20 20:17:14 -05:00
|
|
|
aig_m++, aig_i++;
|
2019-12-31 12:21:11 -06:00
|
|
|
log_assert(!aig_map.count(q));
|
|
|
|
aig_map[q] = 2*aig_m;
|
2019-08-20 20:17:14 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 16:31:42 -06:00
|
|
|
for (auto &bit : ci_bits) {
|
2019-02-08 16:53:12 -06:00
|
|
|
aig_m++, aig_i++;
|
2019-12-31 12:21:11 -06:00
|
|
|
log_assert(!aig_map.count(bit));
|
|
|
|
aig_map[bit] = 2*aig_m;
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-12-27 17:35:19 -06:00
|
|
|
for (auto bit : co_bits) {
|
|
|
|
ordered_outputs[bit] = aig_o++;
|
2019-02-15 13:51:21 -06:00
|
|
|
aig_outputs.push_back(bit2aig(bit));
|
|
|
|
}
|
|
|
|
|
2020-01-03 14:30:22 -06:00
|
|
|
for (const auto &bit : output_bits) {
|
2019-04-12 18:17:48 -05:00
|
|
|
ordered_outputs[bit] = aig_o++;
|
2019-02-08 16:53:12 -06:00
|
|
|
aig_outputs.push_back(bit2aig(bit));
|
|
|
|
}
|
|
|
|
|
2019-09-27 20:41:43 -05:00
|
|
|
for (auto &i : ff_bits) {
|
2019-12-31 12:21:11 -06:00
|
|
|
const SigBit &d = i.first;
|
2019-08-20 20:17:14 -05:00
|
|
|
aig_o++;
|
2019-12-31 12:21:11 -06:00
|
|
|
aig_outputs.push_back(aig_map.at(d));
|
2019-08-20 20:17:14 -05:00
|
|
|
}
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2019-06-12 12:00:57 -05:00
|
|
|
void write_aiger(std::ostream &f, bool ascii_mode)
|
2019-02-08 16:53:12 -06:00
|
|
|
{
|
2019-02-14 16:52:47 -06:00
|
|
|
int aig_obc = aig_o;
|
|
|
|
int aig_obcj = aig_obc;
|
|
|
|
int aig_obcjf = aig_obcj;
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
log_assert(aig_m == aig_i + aig_l + aig_a);
|
|
|
|
log_assert(aig_obcjf == GetSize(aig_outputs));
|
|
|
|
|
2019-02-14 16:52:47 -06:00
|
|
|
f << stringf("%s %d %d %d %d %d", ascii_mode ? "aag" : "aig", aig_m, aig_i, aig_l, aig_o, aig_a);
|
|
|
|
f << stringf("\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
if (ascii_mode)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < aig_i; i++)
|
|
|
|
f << stringf("%d\n", 2*i+2);
|
|
|
|
|
|
|
|
for (int i = 0; i < aig_obc; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = aig_obc; i < aig_obcj; i++)
|
|
|
|
f << stringf("1\n");
|
|
|
|
|
|
|
|
for (int i = aig_obc; i < aig_obcj; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = aig_obcj; i < aig_obcjf; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = 0; i < aig_a; i++)
|
|
|
|
f << stringf("%d %d %d\n", 2*(aig_i+aig_l+i)+2, aig_gates.at(i).first, aig_gates.at(i).second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < aig_obc; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = aig_obc; i < aig_obcj; i++)
|
|
|
|
f << stringf("1\n");
|
|
|
|
|
|
|
|
for (int i = aig_obc; i < aig_obcj; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = aig_obcj; i < aig_obcjf; i++)
|
|
|
|
f << stringf("%d\n", aig_outputs.at(i));
|
|
|
|
|
|
|
|
for (int i = 0; i < aig_a; i++) {
|
|
|
|
int lhs = 2*(aig_i+aig_l+i)+2;
|
|
|
|
int rhs0 = aig_gates.at(i).first;
|
|
|
|
int rhs1 = aig_gates.at(i).second;
|
|
|
|
int delta0 = lhs - rhs0;
|
|
|
|
int delta1 = rhs0 - rhs1;
|
|
|
|
aiger_encode(f, delta0);
|
|
|
|
aiger_encode(f, delta1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:13:11 -05:00
|
|
|
f << "c";
|
|
|
|
|
2019-08-19 15:17:31 -05:00
|
|
|
auto write_buffer = [](std::stringstream &buffer, int i32) {
|
|
|
|
int32_t i32_be = to_big_endian(i32);
|
|
|
|
buffer.write(reinterpret_cast<const char*>(&i32_be), sizeof(i32_be));
|
|
|
|
};
|
|
|
|
std::stringstream h_buffer;
|
|
|
|
auto write_h_buffer = std::bind(write_buffer, std::ref(h_buffer), std::placeholders::_1);
|
|
|
|
write_h_buffer(1);
|
2019-08-20 20:17:14 -05:00
|
|
|
log_debug("ciNum = %d\n", GetSize(input_bits) + GetSize(ff_bits) + GetSize(ci_bits));
|
|
|
|
write_h_buffer(input_bits.size() + ff_bits.size() + ci_bits.size());
|
|
|
|
log_debug("coNum = %d\n", GetSize(output_bits) + GetSize(ff_bits) + GetSize(co_bits));
|
|
|
|
write_h_buffer(output_bits.size() + GetSize(ff_bits) + GetSize(co_bits));
|
|
|
|
log_debug("piNum = %d\n", GetSize(input_bits) + GetSize(ff_bits));
|
|
|
|
write_h_buffer(input_bits.size() + ff_bits.size());
|
|
|
|
log_debug("poNum = %d\n", GetSize(output_bits) + GetSize(ff_bits));
|
|
|
|
write_h_buffer(output_bits.size() + ff_bits.size());
|
2019-08-19 15:17:31 -05:00
|
|
|
log_debug("boxNum = %d\n", GetSize(box_list));
|
|
|
|
write_h_buffer(box_list.size());
|
|
|
|
|
|
|
|
auto write_buffer_float = [](std::stringstream &buffer, float f32) {
|
|
|
|
buffer.write(reinterpret_cast<const char*>(&f32), sizeof(f32));
|
|
|
|
};
|
|
|
|
std::stringstream i_buffer;
|
|
|
|
auto write_i_buffer = std::bind(write_buffer_float, std::ref(i_buffer), std::placeholders::_1);
|
|
|
|
for (auto bit : input_bits)
|
|
|
|
write_i_buffer(arrival_times.at(bit, 0));
|
|
|
|
//std::stringstream o_buffer;
|
|
|
|
//auto write_o_buffer = std::bind(write_buffer_float, std::ref(o_buffer), std::placeholders::_1);
|
|
|
|
//for (auto bit : output_bits)
|
|
|
|
// write_o_buffer(0);
|
2019-04-16 17:01:45 -05:00
|
|
|
|
2019-08-20 20:17:14 -05:00
|
|
|
if (!box_list.empty() || !ff_bits.empty()) {
|
2019-08-02 00:21:14 -05:00
|
|
|
RTLIL::Module *holes_module = module->design->addModule("$__holes__");
|
2019-05-21 18:19:23 -05:00
|
|
|
log_assert(holes_module);
|
2019-04-16 17:01:45 -05:00
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
dict<IdString, std::tuple<Cell*,int,int,int>> cell_cache;
|
2019-09-30 15:21:07 -05:00
|
|
|
|
2019-05-26 16:14:13 -05:00
|
|
|
int port_id = 1;
|
2019-05-28 01:10:59 -05:00
|
|
|
int box_count = 0;
|
2019-04-16 17:01:45 -05:00
|
|
|
for (auto cell : box_list) {
|
2019-12-27 18:44:18 -06:00
|
|
|
RTLIL::Module* orig_box_module = module->design->module(cell->type);
|
|
|
|
log_assert(orig_box_module);
|
|
|
|
IdString derived_name = orig_box_module->derive(module->design, cell->parameters);
|
|
|
|
RTLIL::Module* box_module = module->design->module(derived_name);
|
2019-05-21 18:19:23 -05:00
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
auto r = cell_cache.insert(derived_name);
|
|
|
|
auto &v = r.first->second;
|
|
|
|
if (r.second) {
|
|
|
|
if (box_module->has_processes())
|
|
|
|
Pass::call_on_module(module->design, box_module, "proc");
|
|
|
|
|
|
|
|
int box_inputs = 0, box_outputs = 0;
|
|
|
|
if (box_module->get_bool_attribute("\\whitebox")) {
|
|
|
|
auto holes_cell = holes_module->addCell(cell->name, derived_name);
|
|
|
|
for (auto port_name : box_ports.at(cell->type)) {
|
|
|
|
RTLIL::Wire *w = box_module->wire(port_name);
|
|
|
|
log_assert(w);
|
|
|
|
log_assert(!w->port_input || !w->port_output);
|
|
|
|
auto &conn = holes_cell->connections_[port_name];
|
|
|
|
if (w->port_input) {
|
|
|
|
for (int i = 0; i < GetSize(w); i++) {
|
|
|
|
box_inputs++;
|
|
|
|
RTLIL::Wire *holes_wire = holes_module->wire(stringf("\\i%d", box_inputs));
|
|
|
|
if (!holes_wire) {
|
|
|
|
holes_wire = holes_module->addWire(stringf("\\i%d", box_inputs));
|
|
|
|
holes_wire->port_input = true;
|
|
|
|
holes_wire->port_id = port_id++;
|
|
|
|
holes_module->ports.push_back(holes_wire->name);
|
|
|
|
}
|
|
|
|
conn.append(holes_wire);
|
2020-01-08 17:40:37 -06:00
|
|
|
}
|
2019-04-16 17:01:45 -05:00
|
|
|
}
|
2020-01-08 20:27:09 -06:00
|
|
|
else if (w->port_output) {
|
|
|
|
box_outputs += GetSize(w);
|
|
|
|
conn = holes_module->addWire(stringf("%s.%s", derived_name.c_str(), log_id(port_name)), GetSize(w));
|
|
|
|
}
|
2019-04-16 17:01:45 -05:00
|
|
|
}
|
2020-01-08 20:27:09 -06:00
|
|
|
|
|
|
|
// For flops only, create an extra 1-bit input that drives a new wire
|
|
|
|
// called "<cell>.abc9_ff.Q" that is used below
|
|
|
|
if (box_module->get_bool_attribute("\\abc9_flop")) {
|
|
|
|
box_inputs++;
|
|
|
|
Wire *holes_wire = holes_module->wire(stringf("\\i%d", box_inputs));
|
|
|
|
if (!holes_wire) {
|
|
|
|
holes_wire = holes_module->addWire(stringf("\\i%d", box_inputs));
|
|
|
|
holes_wire->port_input = true;
|
|
|
|
holes_wire->port_id = port_id++;
|
|
|
|
holes_module->ports.push_back(holes_wire->name);
|
|
|
|
}
|
|
|
|
Wire *Q = holes_module->addWire(stringf("%s.abc9_ff.Q", cell->name.c_str()));
|
|
|
|
holes_module->connect(Q, holes_wire);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::get<0>(v) = holes_cell;
|
2019-09-30 15:21:07 -05:00
|
|
|
}
|
2020-01-08 20:27:09 -06:00
|
|
|
else {
|
|
|
|
for (auto port_name : box_ports.at(cell->type)) {
|
|
|
|
RTLIL::Wire *w = box_module->wire(port_name);
|
|
|
|
log_assert(w);
|
|
|
|
log_assert(!w->port_input || !w->port_output);
|
|
|
|
if (w->port_input)
|
|
|
|
box_inputs += GetSize(w);
|
|
|
|
else if (w->port_output)
|
|
|
|
box_outputs += GetSize(w);
|
|
|
|
}
|
|
|
|
log_assert(std::get<0>(v) == nullptr);
|
2019-04-16 17:01:45 -05:00
|
|
|
}
|
2020-01-08 20:27:09 -06:00
|
|
|
|
|
|
|
std::get<1>(v) = box_inputs;
|
|
|
|
std::get<2>(v) = box_outputs;
|
|
|
|
std::get<3>(v) = box_module->attributes.at("\\abc9_box_id").as_int();
|
2019-04-16 17:01:45 -05:00
|
|
|
}
|
2019-05-21 18:19:23 -05:00
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
auto holes_cell = std::get<0>(v);
|
|
|
|
for (auto port_name : box_ports.at(cell->type)) {
|
|
|
|
RTLIL::Wire *w = box_module->wire(port_name);
|
|
|
|
log_assert(w);
|
|
|
|
if (!w->port_output)
|
|
|
|
continue;
|
|
|
|
Wire *holes_wire = holes_module->addWire(stringf("$abc%s.%s", cell->name.c_str(), log_id(port_name)), GetSize(w));
|
|
|
|
holes_wire->port_output = true;
|
|
|
|
holes_wire->port_id = port_id++;
|
|
|
|
holes_module->ports.push_back(holes_wire->name);
|
|
|
|
if (holes_cell) // whitebox
|
|
|
|
holes_module->connect(holes_wire, holes_cell->getPort(port_name));
|
|
|
|
else // blackbox
|
|
|
|
holes_module->connect(holes_wire, Const(State::S0, GetSize(w)));
|
2019-09-30 19:02:20 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
write_h_buffer(std::get<1>(v));
|
|
|
|
write_h_buffer(std::get<2>(v));
|
|
|
|
write_h_buffer(std::get<3>(v));
|
2019-05-28 01:10:59 -05:00
|
|
|
write_h_buffer(box_count++);
|
2019-04-12 20:16:25 -05:00
|
|
|
}
|
2019-04-16 17:01:45 -05:00
|
|
|
|
2019-06-15 11:07:03 -05:00
|
|
|
std::stringstream r_buffer;
|
|
|
|
auto write_r_buffer = std::bind(write_buffer, std::ref(r_buffer), std::placeholders::_1);
|
2019-08-20 20:17:14 -05:00
|
|
|
log_debug("flopNum = %d\n", GetSize(ff_bits));
|
|
|
|
write_r_buffer(ff_bits.size());
|
2019-12-03 20:47:44 -06:00
|
|
|
|
|
|
|
std::stringstream s_buffer;
|
|
|
|
auto write_s_buffer = std::bind(write_buffer, std::ref(s_buffer), std::placeholders::_1);
|
|
|
|
write_s_buffer(ff_bits.size());
|
|
|
|
|
2019-09-27 20:41:43 -05:00
|
|
|
for (const auto &i : ff_bits) {
|
2020-01-02 14:36:54 -06:00
|
|
|
const SigBit &d = i.first;
|
|
|
|
const Cell *cell = i.second;
|
|
|
|
|
|
|
|
int mergeability = cell->attributes.at(ID(abc9_mergeability)).as_int();
|
2019-12-03 20:47:44 -06:00
|
|
|
log_assert(mergeability > 0);
|
|
|
|
write_r_buffer(mergeability);
|
2020-01-02 14:36:54 -06:00
|
|
|
|
|
|
|
Const init = cell->attributes.at(ID(abc9_init));
|
|
|
|
log_assert(GetSize(init) == 1);
|
|
|
|
if (init == State::S1)
|
|
|
|
write_s_buffer(1);
|
|
|
|
else if (init == State::S0)
|
|
|
|
write_s_buffer(0);
|
|
|
|
else {
|
|
|
|
log_assert(init == State::Sx);
|
|
|
|
write_s_buffer(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
write_i_buffer(arrival_times.at(d, 0));
|
2019-08-20 20:17:14 -05:00
|
|
|
//write_o_buffer(0);
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:07:03 -05:00
|
|
|
f << "r";
|
2019-08-19 15:17:31 -05:00
|
|
|
std::string buffer_str = r_buffer.str();
|
|
|
|
int32_t buffer_size_be = to_big_endian(buffer_str.size());
|
2019-08-19 14:33:24 -05:00
|
|
|
f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
f.write(buffer_str.data(), buffer_str.size());
|
|
|
|
|
2019-08-20 20:17:14 -05:00
|
|
|
f << "s";
|
|
|
|
buffer_str = s_buffer.str();
|
|
|
|
buffer_size_be = to_big_endian(buffer_str.size());
|
|
|
|
f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
f.write(buffer_str.data(), buffer_str.size());
|
|
|
|
|
2019-04-16 17:01:45 -05:00
|
|
|
if (holes_module) {
|
2019-06-21 19:16:38 -05:00
|
|
|
log_push();
|
|
|
|
|
2019-05-26 16:14:13 -05:00
|
|
|
// NB: fixup_ports() will sort ports by name
|
|
|
|
//holes_module->fixup_ports();
|
|
|
|
holes_module->check();
|
2019-04-16 17:01:45 -05:00
|
|
|
|
2019-12-27 18:05:58 -06:00
|
|
|
// Cannot techmap/aigmap/check all lib_whitebox-es outside of write_xaiger
|
|
|
|
// since boxes may contain parameters in which case `flatten` would have
|
|
|
|
// created a new $paramod ...
|
2019-10-06 00:55:18 -05:00
|
|
|
Pass::call_on_module(holes_module->design, holes_module, "flatten -wb; techmap; aigmap");
|
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
dict<SigSpec, SigSpec> replace;
|
2019-10-06 00:55:18 -05:00
|
|
|
for (auto it = holes_module->cells_.begin(); it != holes_module->cells_.end(); ) {
|
|
|
|
auto cell = it->second;
|
2019-11-19 18:57:26 -06:00
|
|
|
if (cell->type.in("$_DFF_N_", "$_DFF_NN0_", "$_DFF_NN1_", "$_DFF_NP0_", "$_DFF_NP1_",
|
2019-12-06 18:19:10 -06:00
|
|
|
"$_DFF_P_", "$_DFF_PN0_", "$_DFF_PN1", "$_DFF_PP0_", "$_DFF_PP1_")) {
|
2019-10-06 00:55:18 -05:00
|
|
|
SigBit D = cell->getPort("\\D");
|
|
|
|
SigBit Q = cell->getPort("\\Q");
|
2020-01-08 20:27:09 -06:00
|
|
|
// Remove the $_DFF_* cell from what needs to be a combinatorial box
|
2019-10-06 00:55:18 -05:00
|
|
|
it = holes_module->cells_.erase(it);
|
2019-10-07 13:49:06 -05:00
|
|
|
Wire *port;
|
|
|
|
if (GetSize(Q.wire) == 1)
|
|
|
|
port = holes_module->wire(stringf("$abc%s", Q.wire->name.c_str()));
|
|
|
|
else
|
|
|
|
port = holes_module->wire(stringf("$abc%s[%d]", Q.wire->name.c_str(), Q.offset));
|
2019-10-06 00:55:18 -05:00
|
|
|
log_assert(port);
|
2020-01-08 20:27:09 -06:00
|
|
|
// Prepare to replace "assign <port> = $_DFF_*.Q;" with "assign <port> = $_DFF_*.D;"
|
|
|
|
// in order to extract just the combinatorial control logic that feeds the box
|
2019-10-06 00:55:18 -05:00
|
|
|
// (i.e. clock enable, synchronous reset, etc.)
|
2020-01-08 20:27:09 -06:00
|
|
|
replace.insert(std::make_pair(Q,D));
|
2019-10-06 00:55:18 -05:00
|
|
|
// Since `flatten` above would have created wires named "<cell>.Q",
|
|
|
|
// extract the pre-techmap cell name
|
|
|
|
auto pos = Q.wire->name.str().rfind(".");
|
|
|
|
log_assert(pos != std::string::npos);
|
|
|
|
IdString driver = Q.wire->name.substr(0, pos);
|
|
|
|
// And drive the signal that was previously driven by "DFF.Q" (typically
|
2020-01-02 14:36:54 -06:00
|
|
|
// used to implement clock-enable functionality) with the "<cell>.abc9_ff.Q"
|
2020-01-08 20:27:09 -06:00
|
|
|
// wire (which itself is driven by an input port) we inserted above
|
2020-01-02 14:36:54 -06:00
|
|
|
Wire *currQ = holes_module->wire(stringf("%s.abc9_ff.Q", driver.c_str()));
|
2019-10-06 00:55:18 -05:00
|
|
|
log_assert(currQ);
|
|
|
|
holes_module->connect(Q, currQ);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!cell->type.in("$_NOT_", "$_AND_"))
|
2019-06-14 15:08:38 -05:00
|
|
|
log_error("Whitebox contents cannot be represented as AIG. Please verify whiteboxes are synthesisable.\n");
|
2019-10-06 00:55:18 -05:00
|
|
|
++it;
|
|
|
|
}
|
2019-04-16 17:01:45 -05:00
|
|
|
|
2020-01-08 20:27:09 -06:00
|
|
|
SigMap holes_sigmap(holes_module);
|
2019-10-06 00:55:18 -05:00
|
|
|
for (auto &conn : holes_module->connections_) {
|
2020-01-08 20:27:09 -06:00
|
|
|
auto it = replace.find(sigmap(conn.second));
|
2019-10-06 00:55:18 -05:00
|
|
|
if (it != replace.end())
|
2020-01-08 20:27:09 -06:00
|
|
|
conn.second = it->second;
|
2019-10-06 00:55:18 -05:00
|
|
|
}
|
2019-08-02 00:21:14 -05:00
|
|
|
|
|
|
|
// Move into a new (temporary) design so that "clean" will only
|
|
|
|
// operate (and run checks on) this one module
|
|
|
|
RTLIL::Design *holes_design = new RTLIL::Design;
|
2019-10-06 00:55:18 -05:00
|
|
|
module->design->modules_.erase(holes_module->name);
|
2019-08-02 00:21:14 -05:00
|
|
|
holes_design->add(holes_module);
|
2019-12-23 10:35:53 -06:00
|
|
|
Pass::call(holes_design, "opt -purge");
|
2019-04-16 17:01:45 -05:00
|
|
|
|
|
|
|
std::stringstream a_buffer;
|
2020-01-03 14:30:22 -06:00
|
|
|
XAigerWriter writer(holes_module, true /* holes_mode */);
|
2019-06-12 12:00:57 -05:00
|
|
|
writer.write_aiger(a_buffer, false /*ascii_mode*/);
|
2019-08-02 00:21:14 -05:00
|
|
|
delete holes_design;
|
2019-06-14 15:08:38 -05:00
|
|
|
|
2019-04-16 17:01:45 -05:00
|
|
|
f << "a";
|
|
|
|
std::string buffer_str = a_buffer.str();
|
2019-06-14 14:25:06 -05:00
|
|
|
int32_t buffer_size_be = to_big_endian(buffer_str.size());
|
2019-04-16 17:01:45 -05:00
|
|
|
f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
f.write(buffer_str.data(), buffer_str.size());
|
2019-06-21 19:16:38 -05:00
|
|
|
|
|
|
|
log_pop();
|
2019-04-16 17:01:45 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 16:13:11 -05:00
|
|
|
|
2019-08-19 15:17:31 -05:00
|
|
|
f << "h";
|
|
|
|
std::string buffer_str = h_buffer.str();
|
|
|
|
int32_t buffer_size_be = to_big_endian(buffer_str.size());
|
|
|
|
f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
f.write(buffer_str.data(), buffer_str.size());
|
|
|
|
|
|
|
|
f << "i";
|
|
|
|
buffer_str = i_buffer.str();
|
|
|
|
buffer_size_be = to_big_endian(buffer_str.size());
|
|
|
|
f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
f.write(buffer_str.data(), buffer_str.size());
|
|
|
|
//f << "o";
|
|
|
|
//buffer_str = o_buffer.str();
|
|
|
|
//buffer_size_be = to_big_endian(buffer_str.size());
|
|
|
|
//f.write(reinterpret_cast<const char*>(&buffer_size_be), sizeof(buffer_size_be));
|
|
|
|
//f.write(buffer_str.data(), buffer_str.size());
|
|
|
|
|
2019-04-12 16:13:11 -05:00
|
|
|
f << stringf("Generated by %s\n", yosys_version_str);
|
2019-12-30 17:35:33 -06:00
|
|
|
|
|
|
|
module->design->scratchpad_set_int("write_xaiger.num_ands", and_map.size());
|
|
|
|
module->design->scratchpad_set_int("write_xaiger.num_wires", aig_map.size());
|
|
|
|
module->design->scratchpad_set_int("write_xaiger.num_inputs", input_bits.size());
|
|
|
|
module->design->scratchpad_set_int("write_xaiger.num_outputs", output_bits.size());
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:23:04 -06:00
|
|
|
void write_map(std::ostream &f)
|
2019-02-08 16:53:12 -06:00
|
|
|
{
|
|
|
|
dict<int, string> input_lines;
|
|
|
|
dict<int, string> output_lines;
|
|
|
|
|
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
SigSpec sig = sigmap(wire);
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(wire); i++)
|
|
|
|
{
|
2019-02-17 00:22:17 -06:00
|
|
|
RTLIL::SigBit b(wire, i);
|
2019-04-12 18:17:48 -05:00
|
|
|
if (input_bits.count(b)) {
|
2019-04-23 18:11:14 -05:00
|
|
|
int a = aig_map.at(b);
|
2019-02-08 16:53:12 -06:00
|
|
|
log_assert((a & 1) == 0);
|
|
|
|
input_lines[a] += stringf("input %d %d %s\n", (a >> 1)-1, i, log_id(wire));
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:17:48 -05:00
|
|
|
if (output_bits.count(b)) {
|
2019-02-17 00:22:17 -06:00
|
|
|
int o = ordered_outputs.at(b);
|
2019-12-03 20:47:44 -06:00
|
|
|
int init = 2;
|
2019-08-20 20:17:14 -05:00
|
|
|
output_lines[o] += stringf("output %d %d %s %d\n", o - GetSize(co_bits), i, log_id(wire), init);
|
2019-02-20 13:09:13 -06:00
|
|
|
continue;
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input_lines.sort();
|
|
|
|
for (auto &it : input_lines)
|
|
|
|
f << it.second;
|
2019-05-28 01:10:59 -05:00
|
|
|
log_assert(input_lines.size() == input_bits.size());
|
2019-02-08 16:53:12 -06:00
|
|
|
|
2019-05-28 01:10:59 -05:00
|
|
|
int box_count = 0;
|
|
|
|
for (auto cell : box_list)
|
|
|
|
f << stringf("box %d %d %s\n", box_count++, 0, log_id(cell->name));
|
|
|
|
|
2019-02-08 16:53:12 -06:00
|
|
|
output_lines.sort();
|
|
|
|
for (auto &it : output_lines)
|
|
|
|
f << it.second;
|
2019-05-28 01:10:59 -05:00
|
|
|
log_assert(output_lines.size() == output_bits.size());
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-11 17:18:42 -06:00
|
|
|
struct XAigerBackend : public Backend {
|
|
|
|
XAigerBackend() : Backend("xaiger", "write design to XAIGER file") { }
|
2019-02-08 16:53:12 -06:00
|
|
|
void help() YS_OVERRIDE
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
2019-02-11 17:18:42 -06:00
|
|
|
log(" write_xaiger [options] [filename]\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
log("\n");
|
2020-01-06 12:23:04 -06:00
|
|
|
log("Write the top module (according to the (* top *) attribute or if only one module\n");
|
|
|
|
log("is currently selected) to an XAIGER file. Any non $_NOT_, $_AND_, $_ABC9_FF_, or");
|
|
|
|
log("non (* abc9_box_id *) cells will be converted into psuedo-inputs and\n");
|
|
|
|
log("pseudo-outputs.\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
log("\n");
|
|
|
|
log(" -ascii\n");
|
2019-04-18 19:43:13 -05:00
|
|
|
log(" write ASCII version of AIGER format\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
log("\n");
|
|
|
|
log(" -map <filename>\n");
|
2019-10-07 15:09:13 -05:00
|
|
|
log(" write an extra file with port and box symbols\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
log("\n");
|
|
|
|
}
|
|
|
|
void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
|
|
|
|
{
|
|
|
|
bool ascii_mode = false;
|
|
|
|
std::string map_filename;
|
|
|
|
|
2019-02-11 17:18:42 -06:00
|
|
|
log_header(design, "Executing XAIGER backend.\n");
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
if (args[argidx] == "-ascii") {
|
|
|
|
ascii_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (map_filename.empty() && args[argidx] == "-map" && argidx+1 < args.size()) {
|
|
|
|
map_filename = args[++argidx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-09-28 02:28:51 -05:00
|
|
|
extra_args(f, filename, args, argidx, !ascii_mode);
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
Module *top_module = design->top_module();
|
|
|
|
|
|
|
|
if (top_module == nullptr)
|
|
|
|
log_error("Can't find top module in current design!\n");
|
|
|
|
|
2020-01-06 12:23:04 -06:00
|
|
|
if (!design->selected_whole_module(top_module))
|
|
|
|
log_cmd_error("Can't handle partially selected module %s!\n", log_id(top_module));
|
|
|
|
|
|
|
|
if (!top_module->processes.empty())
|
|
|
|
log_error("Found unmapped processes in module %s: unmapped processes are not supported in XAIGER backend!\n", log_id(top_module));
|
|
|
|
if (!top_module->memories.empty())
|
|
|
|
log_error("Found unmapped memories in module %s: unmapped memories are not supported in XAIGER backend!\n", log_id(top_module));
|
|
|
|
|
2019-10-07 13:58:49 -05:00
|
|
|
XAigerWriter writer(top_module);
|
2019-06-12 12:00:57 -05:00
|
|
|
writer.write_aiger(*f, ascii_mode);
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
if (!map_filename.empty()) {
|
|
|
|
std::ofstream mapf;
|
|
|
|
mapf.open(map_filename.c_str(), std::ofstream::trunc);
|
|
|
|
if (mapf.fail())
|
|
|
|
log_error("Can't open file `%s' for writing: %s\n", map_filename.c_str(), strerror(errno));
|
2020-01-06 12:23:04 -06:00
|
|
|
writer.write_map(mapf);
|
2019-02-08 16:53:12 -06:00
|
|
|
}
|
|
|
|
}
|
2019-02-11 17:18:42 -06:00
|
|
|
} XAigerBackend;
|
2019-02-08 16:53:12 -06:00
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|