/* * nextpnr -- Next Generation Place and Route * * Copyright (C) 2018 David Shah * * 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 "chains.h" #include #include #include "cells.h" #include "design_utils.h" #include "log.h" #include "place_common.h" #include "util.h" NEXTPNR_NAMESPACE_BEGIN struct CellChain { std::vector cells; }; // Generic chain finder template std::vector find_chains(const Context *ctx, F1 cell_type_predicate, F2 get_previous, F3 get_next, size_t min_length = 2) { std::set chained; std::vector chains; for (auto cell : sorted(ctx->cells)) { if (chained.find(cell.first) != chained.end()) continue; CellInfo *ci = cell.second; if (cell_type_predicate(ctx, ci)) { CellInfo *start = ci; CellInfo *prev_start = ci; while (prev_start != nullptr) { start = prev_start; prev_start = get_previous(ctx, start); } CellChain chain; CellInfo *end = start; while (end != nullptr) { chain.cells.push_back(end); end = get_next(ctx, end); } if (chain.cells.size() >= min_length) { chains.push_back(chain); for (auto c : chain.cells) chained.insert(c->name); } } } return chains; } class ChainConstrainer { private: Context *ctx; // Split a carry chain into multiple legal chains std::vector split_carry_chain(CellChain &carryc) { bool start_of_chain = true; std::vector chains; std::vector tile; const int max_length = (ctx->chip_info->height - 2) * 8 - 2; auto curr_cell = carryc.cells.begin(); while (curr_cell != carryc.cells.end()) { CellInfo *cell = *curr_cell; if (tile.size() >= 8) { tile.clear(); } if (start_of_chain) { tile.clear(); chains.emplace_back(); start_of_chain = false; if (cell->ports.at(ctx->id("CIN")).net) { // CIN is not constant and not part of a chain. Must feed in from fabric CellInfo *feedin = make_carry_feed_in(cell, cell->ports.at(ctx->id("CIN"))); chains.back().cells.push_back(feedin); tile.push_back(feedin); } } tile.push_back(cell); chains.back().cells.push_back(cell); bool split_chain = (!ctx->logicCellsCompatible(tile.data(), tile.size())) || (int(chains.back().cells.size()) > max_length); if (split_chain) { CellInfo *passout = make_carry_pass_out(cell->ports.at(ctx->id("COUT"))); tile.pop_back(); chains.back().cells.back() = passout; start_of_chain = true; } else { NetInfo *carry_net = cell->ports.at(ctx->id("COUT")).net; bool at_end = (curr_cell == carryc.cells.end() - 1); if (carry_net != nullptr && (carry_net->users.size() > 1 || at_end)) { if (carry_net->users.size() > 2 || (net_only_drives(ctx, carry_net, is_lc, ctx->id("I3"), false) != net_only_drives(ctx, carry_net, is_lc, ctx->id("CIN"), false)) || (at_end && !net_only_drives(ctx, carry_net, is_lc, ctx->id("I3"), true))) { CellInfo *passout = make_carry_pass_out(cell->ports.at(ctx->id("COUT"))); chains.back().cells.push_back(passout); tile.push_back(passout); start_of_chain = true; } } ++curr_cell; } } return chains; } // Insert a logic cell to legalise a COUT->fabric connection CellInfo *make_carry_pass_out(PortInfo &cout_port) { NPNR_ASSERT(cout_port.net != nullptr); std::unique_ptr lc = create_ice_cell(ctx, ctx->id("ICESTORM_LC")); lc->params[ctx->id("LUT_INIT")] = "65280"; // 0xff00: O = I3 lc->params[ctx->id("CARRY_ENABLE")] = "1"; lc->ports.at(ctx->id("O")).net = cout_port.net; std::unique_ptr co_i3_net(new NetInfo()); co_i3_net->name = ctx->id(lc->name.str(ctx) + "$I3"); co_i3_net->driver = cout_port.net->driver; PortRef i3_r; i3_r.port = ctx->id("I3"); i3_r.cell = lc.get(); co_i3_net->users.push_back(i3_r); PortRef o_r; o_r.port = ctx->id("O"); o_r.cell = lc.get(); cout_port.net->driver = o_r; lc->ports.at(ctx->id("I3")).net = co_i3_net.get(); cout_port.net = co_i3_net.get(); IdString co_i3_name = co_i3_net->name; NPNR_ASSERT(ctx->nets.find(co_i3_name) == ctx->nets.end()); ctx->nets[co_i3_name] = std::move(co_i3_net); IdString name = lc->name; ctx->assignCellInfo(lc.get()); ctx->cells[lc->name] = std::move(lc); return ctx->cells[name].get(); } // Insert a logic cell to legalise a CIN->fabric connection CellInfo *make_carry_feed_in(CellInfo *cin_cell, PortInfo &cin_port) { NPNR_ASSERT(cin_port.net != nullptr); std::unique_ptr lc = create_ice_cell(ctx, ctx->id("ICESTORM_LC")); lc->params[ctx->id("CARRY_ENABLE")] = "1"; lc->params[ctx->id("CIN_CONST")] = "1"; lc->params[ctx->id("CIN_SET")] = "1"; lc->ports.at(ctx->id("I1")).net = cin_port.net; cin_port.net->users.erase(std::remove_if(cin_port.net->users.begin(), cin_port.net->users.end(), [cin_cell, cin_port](const PortRef &usr) { return usr.cell == cin_cell && usr.port == cin_port.name; })); PortRef i1_ref; i1_ref.cell = lc.get(); i1_ref.port = ctx->id("I1"); lc->ports.at(ctx->id("I1")).net->users.push_back(i1_ref); std::unique_ptr out_net(new NetInfo()); out_net->name = ctx->id(lc->name.str(ctx) + "$O"); PortRef drv_ref; drv_ref.port = ctx->id("COUT"); drv_ref.cell = lc.get(); out_net->driver = drv_ref; lc->ports.at(ctx->id("COUT")).net = out_net.get(); PortRef usr_ref; usr_ref.port = cin_port.name; usr_ref.cell = cin_cell; out_net->users.push_back(usr_ref); cin_cell->ports.at(cin_port.name).net = out_net.get(); IdString out_net_name = out_net->name; NPNR_ASSERT(ctx->nets.find(out_net_name) == ctx->nets.end()); ctx->nets[out_net_name] = std::move(out_net); IdString name = lc->name; ctx->assignCellInfo(lc.get()); ctx->cells[lc->name] = std::move(lc); return ctx->cells[name].get(); } void process_carries() { std::vector carry_chains = find_chains(ctx, [](const Context *ctx, const CellInfo *cell) { return is_lc(ctx, cell); }, [](const Context *ctx, const CellInfo *cell) { CellInfo *carry_prev = net_driven_by(ctx, cell->ports.at(ctx->id("CIN")).net, is_lc, ctx->id("COUT")); if (carry_prev != nullptr) return carry_prev; /*CellInfo *i3_prev = net_driven_by(ctx, cell->ports.at(ctx->id("I3")).net, is_lc, ctx->id("COUT")); if (i3_prev != nullptr) return i3_prev;*/ return (CellInfo *)nullptr; }, [](const Context *ctx, const CellInfo *cell) { CellInfo *carry_next = net_only_drives(ctx, cell->ports.at(ctx->id("COUT")).net, is_lc, ctx->id("CIN"), false); if (carry_next != nullptr) return carry_next; /*CellInfo *i3_next = net_only_drives(ctx, cell->ports.at(ctx->id("COUT")).net, is_lc, ctx->id("I3"), false); if (i3_next != nullptr) return i3_next;*/ return (CellInfo *)nullptr; }); std::unordered_set chained; for (auto &base_chain : carry_chains) { for (auto c : base_chain.cells) chained.insert(c->name); } // Any cells not in chains, but with carry enabled, must also be put in a single-carry chain // for correct processing for (auto cell : sorted(ctx->cells)) { CellInfo *ci = cell.second; if (chained.find(cell.first) == chained.end() && is_lc(ctx, ci) && bool_or_default(ci->params, ctx->id("CARRY_ENABLE"))) { CellChain sChain; sChain.cells.push_back(ci); chained.insert(cell.first); carry_chains.push_back(sChain); } } std::vector all_chains; // Chain splitting for (auto &base_chain : carry_chains) { if (ctx->verbose) { log_info("Found carry chain: \n"); for (auto entry : base_chain.cells) log_info(" %s\n", entry->name.c_str(ctx)); log_info("\n"); } std::vector split_chains = split_carry_chain(base_chain); for (auto &chain : split_chains) { all_chains.push_back(chain); } } // Actual chain placement for (auto &chain : all_chains) { if (ctx->verbose) log_info("Placing carry chain starting at '%s'\n", chain.cells.front()->name.c_str(ctx)); // Place carry chain chain.
/*
 *  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.
 *
 *  ---
 *
 *  The Verilog frontend.
 *
 *  This frontend is using the AST frontend library (see frontends/ast/).
 *  Thus this frontend does not generate RTLIL code directly but creates an
 *  AST directly from the Verilog parse tree and then passes this AST to
 *  the AST frontend library.
 *
 *  ---
 *
 *  This file contains an ad-hoc parser for Verilog constants. The Verilog
 *  lexer does only recognize a constant but does not actually split it to its
 *  components. I.e. it just passes the Verilog code for the constant to the
 *  bison parser. The parser then uses the function const2ast() from this file
 *  to create an AST node for the constant.
 *
 */

#include "verilog_frontend.h"
#include "kernel/log.h"
#include <string.h>
#include <math.h>

YOSYS_NAMESPACE_BEGIN

using namespace AST;

// divide an arbitrary length decimal number by two and return the rest
static int my_decimal_div_by_two(std::vector<uint8_t> &digits)
{
	int carry = 0;
	for (size_t i = 0; i < digits.size(); i++) {
		if (digits[i] >= 10)
			log_file_error(current_filename, get_line_num(), "Invalid use of [a-fxz?] in decimal constant.\n");
		digits[i] += carry * 10;
		carry = digits[i] % 2;
		digits[i] /= 2;
	}
	while (!digits.empty() && !digits.front())
		digits.erase(digits.begin());
	return carry;
}

// find the number of significant bits in a binary number (not including the sign bit)
static int my_ilog2(int x)
{
	int ret = 0;
	while (x != 0 && x != -1) {
		x = x >> 1;
		ret++;
	}
	return ret;
}

// parse a binary, decimal, hexadecimal or octal number with support for special bits ('x', 'z' and '?')
static void my_strtobin(std::vector<RTLIL::State> &data, const char *str, int len_in_bits, int base, char case_type, bool is_unsized)
{
	// all digits in string (MSB at index 0)
	std::vector<uint8_t> digits;

	while (*str) {
		if ('0' <= *str && *str <= '9')
			digits.push_back(*str - '0');
		else if ('a' <= *str && *str <= 'f')
			digits.push_back(10 + *str - 'a');
		else if ('A' <= *str && *str <= 'F')
			digits.push_back(10 + *str - 'A');
		else if (*str == 'x' || *str == 'X')
			digits.push_back(0xf0);
		else if (*str == 'z' || *str == 'Z' || *str == '?')
			digits.push_back(0xf1);
		str++;
	}

	if (base == 10 && GetSize(digits) == 1 && digits.front() >= 0xf0)
		base = 2;

	data.clear();

	if (base == 10) {
		while (!digits.empty())
			data.push_back(my_decimal_div_by_two(digits) ? State::S1 : State::S0);
	} else {
		int bits_per_digit = my_ilog2(base-1);
		for (auto it = digits.rbegin(), e = digits.rend(); it != e; it++) {
			if (*it > (base-1) && *it < 0xf0)
				log_file_error(current_filename, get_line_num(), "Digit larger than %d used in in base-%d constant.\n",
					       base-1, base);
			for (int i = 0; i < bits_per_digit; i++) {
				int bitmask = 1 << i;
				if (*it == 0xf0)
					data.push_back(case_type == 'x' ? RTLIL::Sa : RTLIL::Sx);
				else if (*it == 0xf1)
					data.push_back(case_type == 'x' || case_type == 'z' ? RTLIL::Sa : RTLIL::Sz);
				else
					data.push_back((*it & bitmask) ? State::S1 : State::S0);
			}
		}
	}

	int len = GetSize(data);
	RTLIL::State msb = data.empty() ? State::S0 : data.back();

	if (len_in_bits < 0) {
		if (len < 32)
			data.resize(32, msb == State::S0 || msb == State::S1 ? RTLIL::S0 : msb);
		return;
	}

	if (is_unsized && (len > len_in_bits))
		log_file_error(current_filename, get_line_num(), "Unsized constant must have width of 1 bit, but have %d bits!\n", len);

	for (len = len - 1; len >= 0; len--)
		if (data[len] == State::S1)
			break;
	if (msb == State::S0 || msb == State::S1) {
		len += 1;
		data.resize(len_in_bits, State::S0);
	} else {
		len += 2;
		data.resize(len_in_bits, msb);
	}

	if (len > len_in_bits)
		log_warning("Literal has a width of %d bit, but value requires %d bit. (%s:%d)\n",
			len_in_bits, len, current_filename.c_str(), get_line_num());
}

// convert the Verilog code for a constant to an AST node
AstNode *VERILOG_FRONTEND::const2ast(std::string code, char case_type, bool warn_z)
{
	if (warn_z) {
		AstNode *ret = const2ast(code, case_type);
		if (ret != nullptr && std::find(ret->bits.begin(), ret->bits.end(), RTLIL::State::Sz) != ret->bits.end())
			log_warning("Yosys has only limited support for tri-state logic at the moment. (%s:%d)\n",
				current_filename.c_str(), get_line_num());
		return ret;
	}

	const char *str = code.c_str();

	// Strings
	if (*str == '"') {
		int len = strlen(str) - 2;
		std::vector<RTLIL::State> data;
		data.reserve(len * 8);
		for (int i = 0; i < len; i++) {
			unsigned char ch = str[len - i];
			for (int j = 0; j < 8; j++) {
				data.push_back((ch & 1) ? State::S1 : State::S0);
				ch = ch >> 1;
			}
		}
		AstNode *ast = AstNode::mkconst_bits(data, false);
		ast->str = code;
		return ast;
	}

	for (size_t i = 0; i < code.size(); i++)
		if (code[i] == '_' || code[i] == ' ' || code[i] == '\t' || code[i] == '\r' || code[i] == '\n')
			code.erase(code.begin()+(i--));
	str = code.c_str();

	char *endptr;
	long len_in_bits = strtol(str, &endptr, 10);

	// Simple base-10 integer
	if (*endptr == 0) {
		std::vector<RTLIL::State> data;
		my_strtobin(data, str, -1, 10, case_type, false);
		if (data.back() == State::S1)
			data.push_back(State::S0);
		return AstNode::mkconst_bits(data, true);
	}

	// unsized constant
	if (str == endptr)
		len_in_bits = -1;

	// The "<bits>'[sS]?[bodhBODH]<digits>" syntax
	if (*endptr == '\'')
	{
		std::vector<RTLIL::State> data;
		bool is_signed = false;
		bool is_unsized = len_in_bits < 0;
		if (*(endptr+1) == 's' || *(endptr+1) == 'S') {
			is_signed = true;
			endptr++;
		}
		switch (*(endptr+1))
		{
		case 'b':
		case 'B':
			my_strtobin(data, endptr+2, len_in_bits, 2, case_type, is_unsized);
			break;
		case 'o':
		case 'O':
			my_strtobin(data, endptr+2, len_in_bits, 8, case_type, is_unsized);
			break;
		case 'd':
		case 'D':
			my_strtobin(data, endptr+2, len_in_bits, 10, case_type, is_unsized);
			break;
		case 'h':
		case 'H':
			my_strtobin(data, endptr+2, len_in_bits, 16, case_type, is_unsized);
			break;
		default:
			char next_char = char(tolower(*(endptr+1)));
			if (next_char == '0' || next_char == '1' || next_char == 'x' || next_char == 'z') {
				is_unsized = true;
				my_strtobin(data, endptr+1, 1, 2, case_type, is_unsized);
			} else {
				return NULL;
			}
		}
		if (len_in_bits < 0) {
			if (is_signed && data.back() == State::S1)
				data.push_back(State::S0);
		}
		return AstNode::mkconst_bits(data, is_signed, is_unsized);
	}

	return NULL;
}

YOSYS_NAMESPACE_END