diff options
Diffstat (limited to 'passes/techmap')
| -rw-r--r-- | passes/techmap/Makefile.inc | 3 | ||||
| -rw-r--r-- | passes/techmap/abc.cc | 44 | ||||
| -rw-r--r-- | passes/techmap/abc9.cc | 2 | ||||
| -rw-r--r-- | passes/techmap/abc9_exe.cc | 13 | ||||
| -rw-r--r-- | passes/techmap/dff2dffe.cc | 414 | ||||
| -rw-r--r-- | passes/techmap/dff2dffs.cc | 165 | ||||
| -rw-r--r-- | passes/techmap/dffinit.cc | 48 | ||||
| -rw-r--r-- | passes/techmap/dfflegalize.cc | 111 | ||||
| -rw-r--r-- | passes/techmap/dffunmap.cc | 107 | ||||
| -rw-r--r-- | passes/techmap/extract.cc | 10 | ||||
| -rw-r--r-- | passes/techmap/flatten.cc | 7 | ||||
| -rw-r--r-- | passes/techmap/shregmap.cc | 49 | ||||
| -rw-r--r-- | passes/techmap/techmap.cc | 122 | ||||
| -rw-r--r-- | passes/techmap/zinit.cc | 54 | 
14 files changed, 252 insertions, 897 deletions
diff --git a/passes/techmap/Makefile.inc b/passes/techmap/Makefile.inc index e3b3d8dea..035699603 100644 --- a/passes/techmap/Makefile.inc +++ b/passes/techmap/Makefile.inc @@ -27,7 +27,6 @@ OBJS += passes/techmap/extract_fa.o  OBJS += passes/techmap/extract_counter.o  OBJS += passes/techmap/extract_reduce.o  OBJS += passes/techmap/alumacc.o -OBJS += passes/techmap/dff2dffe.o  OBJS += passes/techmap/dffinit.o  OBJS += passes/techmap/pmuxtree.o  OBJS += passes/techmap/muxcover.o @@ -42,7 +41,7 @@ OBJS += passes/techmap/attrmvcp.o  OBJS += passes/techmap/attrmap.o  OBJS += passes/techmap/zinit.o  OBJS += passes/techmap/dfflegalize.o -OBJS += passes/techmap/dff2dffs.o +OBJS += passes/techmap/dffunmap.o  OBJS += passes/techmap/flowmap.o  OBJS += passes/techmap/extractinv.o  endif diff --git a/passes/techmap/abc.cc b/passes/techmap/abc.cc index 0a58fdcc0..192e39372 100644 --- a/passes/techmap/abc.cc +++ b/passes/techmap/abc.cc @@ -44,6 +44,7 @@  #include "kernel/register.h"  #include "kernel/sigtools.h"  #include "kernel/celltypes.h" +#include "kernel/ffinit.h"  #include "kernel/cost.h"  #include "kernel/log.h"  #include <stdlib.h> @@ -111,7 +112,7 @@ SigMap assign_map;  RTLIL::Module *module;  std::vector<gate_t> signal_list;  std::map<RTLIL::SigBit, int> signal_map; -std::map<RTLIL::SigBit, RTLIL::State> signal_init; +FfInitVals initvals;  pool<std::string> enabled_gates;  bool recover_init, cmos_cost; @@ -133,10 +134,7 @@ int map_signal(RTLIL::SigBit bit, gate_type_t gate_type = G(NONE), int in1 = -1,  		gate.in4 = -1;  		gate.is_port = false;  		gate.bit = bit; -		if (signal_init.count(bit)) -			gate.init = signal_init.at(bit); -		else -			gate.init = State::Sx; +		gate.init = initvals(bit);  		signal_list.push_back(gate);  		signal_map[bit] = gate.id;  	} @@ -1468,15 +1466,11 @@ struct AbcPass : public Pass {  		assign_map.clear();  		signal_list.clear();  		signal_map.clear(); -		signal_init.clear(); +		initvals.clear();  		pi_map.clear();  		po_map.clear(); -#ifdef ABCEXTERNAL -		std::string exe_file = ABCEXTERNAL; -#else -		std::string exe_file = proc_self_dirname() + proc_program_prefix() + "yosys-abc"; -#endif +		std::string exe_file = yosys_abc_executable;  		std::string script_file, liberty_file, constr_file, clk_str;  		std::string delay_target, sop_inputs, sop_products, lutin_shared = "-S 1";  		bool fast_mode = false, dff_mode = false, keepff = false, cleanup = true; @@ -1491,13 +1485,6 @@ struct AbcPass : public Pass {  		enabled_gates.clear();  		cmos_cost = false; -#ifdef _WIN32 -#ifndef ABCEXTERNAL -		if (!check_file_exists(exe_file + ".exe") && check_file_exists(proc_self_dirname() + "..\\" + proc_program_prefix()+ "yosys-abc.exe")) -			exe_file = proc_self_dirname() + "..\\" + proc_program_prefix() + "yosys-abc"; -#endif -#endif -  		// get arguments from scratchpad first, then override by command arguments  		std::string lut_arg, luts_arg, g_arg;  		exe_file = design->scratchpad_get_string("abc.exe", exe_file /* inherit default value if not set */); @@ -1854,24 +1841,7 @@ struct AbcPass : public Pass {  			}  			assign_map.set(mod); -			signal_init.clear(); - -			for (Wire *wire : mod->wires()) -				if (wire->attributes.count(ID::init)) { -					SigSpec initsig = assign_map(wire); -					Const initval = wire->attributes.at(ID::init); -					for (int i = 0; i < GetSize(initsig) && i < GetSize(initval); i++) -						switch (initval[i]) { -							case State::S0: -								signal_init[initsig[i]] = State::S0; -								break; -							case State::S1: -								signal_init[initsig[i]] = State::S1; -								break; -							default: -								break; -						} -				} +			initvals.set(&assign_map, mod);  			if (!dff_mode || !clk_str.empty()) {  				abc_module(design, mod, script_file, exe_file, liberty_file, constr_file, cleanup, lut_costs, dff_mode, clk_str, keepff, @@ -2028,7 +1998,7 @@ struct AbcPass : public Pass {  		assign_map.clear();  		signal_list.clear();  		signal_map.clear(); -		signal_init.clear(); +		initvals.clear();  		pi_map.clear();  		po_map.clear(); diff --git a/passes/techmap/abc9.cc b/passes/techmap/abc9.cc index e99c56d8d..7d017ac40 100644 --- a/passes/techmap/abc9.cc +++ b/passes/techmap/abc9.cc @@ -295,7 +295,7 @@ struct Abc9Pass : public ScriptPass  			run("proc");  			run("wbflip");  			run("techmap -wb -map %$abc9 -map +/techmap.v A:abc9_flop"); -			run("opt"); +			run("opt -nodffe -nosdff");  			if (dff_mode || help_mode) {  				if (!help_mode)  					active_design->scratchpad_unset("abc9_ops.prep_dff_submod.did_something"); diff --git a/passes/techmap/abc9_exe.cc b/passes/techmap/abc9_exe.cc index 7355840aa..b916b049d 100644 --- a/passes/techmap/abc9_exe.cc +++ b/passes/techmap/abc9_exe.cc @@ -379,11 +379,7 @@ struct Abc9ExePass : public Pass {  	{  		log_header(design, "Executing ABC9_EXE pass (technology mapping using ABC9).\n"); -#ifdef ABCEXTERNAL -		std::string exe_file = ABCEXTERNAL; -#else -		std::string exe_file = proc_self_dirname() + proc_program_prefix()+ "yosys-abc"; -#endif +		std::string exe_file = yosys_abc_executable;  		std::string script_file, clk_str, box_file, lut_file;  		std::string delay_target, lutin_shared = "-S 1", wire_delay;  		std::string tempdir_name; @@ -396,13 +392,6 @@ struct Abc9ExePass : public Pass {  		show_tempdir = true;  #endif -#ifdef _WIN32 -#ifndef ABCEXTERNAL -		if (!check_file_exists(exe_file + ".exe") && check_file_exists(proc_self_dirname() + "..\\" + proc_program_prefix() + "yosys-abc.exe")) -			exe_file = proc_self_dirname() + "..\\" + proc_program_prefix() + "yosys-abc"; -#endif -#endif -  		std::string lut_arg, luts_arg;  		exe_file = design->scratchpad_get_string("abc9.exe", exe_file /* inherit default value if not set */);  		script_file = design->scratchpad_get_string("abc9.script", script_file); diff --git a/passes/techmap/dff2dffe.cc b/passes/techmap/dff2dffe.cc deleted file mode 100644 index 62ee3fea6..000000000 --- a/passes/techmap/dff2dffe.cc +++ /dev/null @@ -1,414 +0,0 @@ -/* - *  yosys -- Yosys Open SYnthesis Suite - * - *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at> - * - *  Permission to use, copy, modify, and/or distribute this software for any - *  purpose with or without fee is hereby granted, provided that the above - *  copyright notice and this permission notice appear in all copies. - * - *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - */ - -#include "kernel/yosys.h" -#include "kernel/sigtools.h" -#include "kernel/celltypes.h" -#include "passes/techmap/simplemap.h" - -USING_YOSYS_NAMESPACE -PRIVATE_NAMESPACE_BEGIN - -struct Dff2dffeWorker -{ -	const dict<IdString, IdString> &direct_dict; - -	RTLIL::Module *module; -	SigMap sigmap; -	CellTypes ct; - -	typedef std::pair<RTLIL::Cell*, int> cell_int_t; -	std::map<RTLIL::SigBit, cell_int_t> bit2mux; -	std::vector<RTLIL::Cell*> dff_cells; -	std::map<RTLIL::SigBit, int> bitusers; - -	typedef std::map<RTLIL::SigBit, bool> pattern_t; -	typedef std::set<pattern_t> patterns_t; - - -	Dff2dffeWorker(RTLIL::Module *module, const dict<IdString, IdString> &direct_dict) : -			direct_dict(direct_dict), module(module), sigmap(module), ct(module->design) -	{ -		for (auto wire : module->wires()) { -			if (wire->port_output) -				for (auto bit : sigmap(wire)) -					bitusers[bit]++; -		} - -		for (auto cell : module->cells()) { -			if (cell->type.in(ID($mux), ID($pmux), ID($_MUX_))) { -				RTLIL::SigSpec sig_y = sigmap(cell->getPort(ID::Y)); -				for (int i = 0; i < GetSize(sig_y); i++) -					bit2mux[sig_y[i]] = cell_int_t(cell, i); -			} -			if (direct_dict.empty()) { -				if (cell->type.in(ID($dff), ID($_DFF_N_), ID($_DFF_P_))) -					dff_cells.push_back(cell); -			} else { -				if (direct_dict.count(cell->type)) -					dff_cells.push_back(cell); -			} -			for (auto conn : cell->connections()) { -				if (ct.cell_output(cell->type, conn.first)) -					continue; -				for (auto bit : sigmap(conn.second)) -					bitusers[bit]++; -			} -		} -	} - -	patterns_t find_muxtree_feedback_patterns(RTLIL::SigBit d, RTLIL::SigBit q, pattern_t path) -	{ -		patterns_t ret; - -		if (d == q) { -			ret.insert(path); -			return ret; -		} - -		if (bit2mux.count(d) == 0 || bitusers[d] > 1) -			return ret; - -		cell_int_t mux_cell_int = bit2mux.at(d); -		RTLIL::SigSpec sig_a = sigmap(mux_cell_int.first->getPort(ID::A)); -		RTLIL::SigSpec sig_b = sigmap(mux_cell_int.first->getPort(ID::B)); -		RTLIL::SigSpec sig_s = sigmap(mux_cell_int.first->getPort(ID::S)); -		int width = GetSize(sig_a), index = mux_cell_int.second; - -		for (int i = 0; i < GetSize(sig_s); i++) -			if (path.count(sig_s[i]) && path.at(sig_s[i])) -			{ -				ret = find_muxtree_feedback_patterns(sig_b[i*width + index], q, path); - -				if (sig_b[i*width + index] == q) { -					RTLIL::SigSpec s = mux_cell_int.first->getPort(ID::B); -					s[i*width + index] = RTLIL::Sx; -					mux_cell_int.first->setPort(ID::B, s); -				} - -				return ret; -			} - -		pattern_t path_else = path; - -		for (int i = 0; i < GetSize(sig_s); i++) -		{ -			if (path.count(sig_s[i])) -				continue; - -			pattern_t path_this = path; -			path_else[sig_s[i]] = false; -			path_this[sig_s[i]] = true; - -			for (auto &pat : find_muxtree_feedback_patterns(sig_b[i*width + index], q, path_this)) -				ret.insert(pat); - -			if (sig_b[i*width + index] == q) { -				RTLIL::SigSpec s = mux_cell_int.first->getPort(ID::B); -				s[i*width + index] = RTLIL::Sx; -				mux_cell_int.first->setPort(ID::B, s); -			} -		} - -		for (auto &pat : find_muxtree_feedback_patterns(sig_a[index], q, path_else)) -			ret.insert(pat); - -		if (sig_a[index] == q) { -			RTLIL::SigSpec s = mux_cell_int.first->getPort(ID::A); -			s[index] = RTLIL::Sx; -			mux_cell_int.first->setPort(ID::A, s); -		} - -		return ret; -	} - -	void simplify_patterns(patterns_t&) -	{ -		// TBD -	} - -	RTLIL::SigSpec make_patterns_logic(patterns_t patterns, bool make_gates) -	{ -		RTLIL::SigSpec or_input; - -		for (auto pat : patterns) -		{ -			RTLIL::SigSpec s1, s2; -			for (auto it : pat) { -				s1.append(it.first); -				s2.append(it.second); -			} - -			RTLIL::SigSpec y = module->addWire(NEW_ID); -			RTLIL::Cell *c = module->addNe(NEW_ID, s1, s2, y); - -			if (make_gates) { -				simplemap(module, c); -				module->remove(c); -			} - -			or_input.append(y); -		} - -		if (GetSize(or_input) == 0) -			return State::S1; - -		if (GetSize(or_input) == 1) -			return or_input; - -		RTLIL::SigSpec y = module->addWire(NEW_ID); -		RTLIL::Cell *c = module->addReduceAnd(NEW_ID, or_input, y); - -		if (make_gates) { -			simplemap(module, c); -			module->remove(c); -		} - -		return y; -	} - -	void handle_dff_cell(RTLIL::Cell *dff_cell) -	{ -		RTLIL::SigSpec sig_d = sigmap(dff_cell->getPort(ID::D)); -		RTLIL::SigSpec sig_q = sigmap(dff_cell->getPort(ID::Q)); - -		std::map<patterns_t, std::set<int>> grouped_patterns; -		std::set<int> remaining_indices; - -		for (int i = 0 ; i < GetSize(sig_d); i++) { -			patterns_t patterns = find_muxtree_feedback_patterns(sig_d[i], sig_q[i], pattern_t()); -			if (!patterns.empty()) { -				simplify_patterns(patterns); -				grouped_patterns[patterns].insert(i); -			} else -				remaining_indices.insert(i); -		} - -		for (auto &it : grouped_patterns) { -			RTLIL::SigSpec new_sig_d, new_sig_q; -			for (int i : it.second) { -				new_sig_d.append(sig_d[i]); -				new_sig_q.append(sig_q[i]); -			} -			if (!direct_dict.empty()) { -				log("  converting %s cell %s to %s for %s -> %s.\n", log_id(dff_cell->type), log_id(dff_cell), log_id(direct_dict.at(dff_cell->type)), log_signal(new_sig_d), log_signal(new_sig_q)); -				dff_cell->setPort(ID::E, make_patterns_logic(it.first, true)); -				dff_cell->type = direct_dict.at(dff_cell->type); -			} else -			if (dff_cell->type == ID($dff)) { -				RTLIL::Cell *new_cell = module->addDffe(NEW_ID, dff_cell->getPort(ID::CLK), make_patterns_logic(it.first, false), -						new_sig_d, new_sig_q, dff_cell->getParam(ID::CLK_POLARITY).as_bool(), true); -				log("  created $dffe cell %s for %s -> %s.\n", log_id(new_cell), log_signal(new_sig_d), log_signal(new_sig_q)); -			} else { -				RTLIL::Cell *new_cell = module->addDffeGate(NEW_ID, dff_cell->getPort(ID::C), make_patterns_logic(it.first, true), -						new_sig_d, new_sig_q, dff_cell->type == ID($_DFF_P_), true); -				log("  created %s cell %s for %s -> %s.\n", log_id(new_cell->type), log_id(new_cell), log_signal(new_sig_d), log_signal(new_sig_q)); -			} -		} - -		if (!direct_dict.empty()) -			return; - -		if (remaining_indices.empty()) { -			log("  removing now obsolete cell %s.\n", log_id(dff_cell)); -			module->remove(dff_cell); -		} else if (GetSize(remaining_indices) != GetSize(sig_d)) { -			log("  removing %d now obsolete bits from cell %s.\n", GetSize(sig_d) - GetSize(remaining_indices), log_id(dff_cell)); -			RTLIL::SigSpec new_sig_d, new_sig_q; -			for (int i : remaining_indices) { -				new_sig_d.append(sig_d[i]); -				new_sig_q.append(sig_q[i]); -			} -			dff_cell->setPort(ID::D, new_sig_d); -			dff_cell->setPort(ID::Q, new_sig_q); -			dff_cell->setParam(ID::WIDTH, GetSize(remaining_indices)); -		} -	} - -	void run() -	{ -		log("Transforming FF to FF+Enable cells in module %s:\n", log_id(module)); -		for (auto dff_cell : dff_cells) { -			// log("Handling candidate %s:\n", log_id(dff_cell)); -			handle_dff_cell(dff_cell); -		} -	} -}; - -struct Dff2dffePass : public Pass { -	Dff2dffePass() : Pass("dff2dffe", "transform $dff cells to $dffe cells") { } -	void help() override -	{ -		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| -		log("\n"); -		log("    dff2dffe [options] [selection]\n"); -		log("\n"); -		log("This pass transforms $dff cells driven by a tree of multiplexers with one or\n"); -		log("more feedback paths to $dffe cells. It also works on gate-level cells such as\n"); -		log("$_DFF_P_, $_DFF_N_ and $_MUX_.\n"); -		log("\n"); -		log("    -unmap\n"); -		log("        operate in the opposite direction: replace $dffe cells with combinations\n"); -		log("        of $dff and $mux cells. the options below are ignored in unmap mode.\n"); -		log("\n"); -		log("    -unmap-mince N\n"); -		log("        Same as -unmap but only unmap $dffe where the clock enable port\n"); -		log("        signal is used by less $dffe than the specified number\n"); -		log("\n"); -		log("    -direct <internal_gate_type> <external_gate_type>\n"); -		log("        map directly to external gate type. <internal_gate_type> can\n"); -		log("        be any internal gate-level FF cell (except $_DFFE_??_). the\n"); -		log("        <external_gate_type> is the cell type name for a cell with an\n"); -		log("        identical interface to the <internal_gate_type>, except it\n"); -		log("        also has an high-active enable port 'E'.\n"); -		log("          Usually <external_gate_type> is an intermediate cell type\n"); -		log("        that is then translated to the final type using 'techmap'.\n"); -		log("\n"); -		log("    -direct-match <pattern>\n"); -		log("        like -direct for all DFF cell types matching the expression.\n"); -		log("        this will use $_DFFE_* as <external_gate_type> matching the\n"); -		log("        internal gate type $_DFF_*_, and $_SDFFE_* for those matching\n"); -		log("        $_SDFF_*_, except for $_DFF_[NP]_, which is converted to \n"); -		log("        $_DFFE_[NP]_.\n"); -		log("\n"); -	} -	void execute(std::vector<std::string> args, RTLIL::Design *design) override -	{ -		log_header(design, "Executing DFF2DFFE pass (transform $dff to $dffe where applicable).\n"); - -		bool unmap_mode = false; -		int min_ce_use = -1; -		dict<IdString, IdString> direct_dict; - -		size_t argidx; -		for (argidx = 1; argidx < args.size(); argidx++) { -			if (args[argidx] == "-unmap") { -				unmap_mode = true; -				continue; -			} -			if (args[argidx] == "-unmap-mince" && argidx + 1 < args.size()) { -				unmap_mode = true; -				min_ce_use = atoi(args[++argidx].c_str()); -				continue; -			} -			if (args[argidx] == "-direct" && argidx + 2 < args.size()) { -				string direct_from = RTLIL::escape_id(args[++argidx]); -				string direct_to = RTLIL::escape_id(args[++argidx]); -				direct_dict[direct_from] = direct_to; -				continue; -			} -			if (args[argidx] == "-direct-match" && argidx + 1 < args.size()) { -				bool found_match = false; -				const char *pattern = args[++argidx].c_str(); -				if (patmatch(pattern, "$_DFF_P_"  )) found_match = true, direct_dict[ID($_DFF_P_)  ] = ID($_DFFE_PP_); -				if (patmatch(pattern, "$_DFF_N_"  )) found_match = true, direct_dict[ID($_DFF_N_)  ] = ID($_DFFE_NP_); -				if (patmatch(pattern, "$_DFF_NN0_")) found_match = true, direct_dict[ID($_DFF_NN0_)] = ID($_DFFE_NN0P_); -				if (patmatch(pattern, "$_DFF_NN1_")) found_match = true, direct_dict[ID($_DFF_NN1_)] = ID($_DFFE_NN1P_); -				if (patmatch(pattern, "$_DFF_NP0_")) found_match = true, direct_dict[ID($_DFF_NP0_)] = ID($_DFFE_NP0P_); -				if (patmatch(pattern, "$_DFF_NP1_")) found_match = true, direct_dict[ID($_DFF_NP1_)] = ID($_DFFE_NP1P_); -				if (patmatch(pattern, "$_DFF_PN0_")) found_match = true, direct_dict[ID($_DFF_PN0_)] = ID($_DFFE_PN0P_); -				if (patmatch(pattern, "$_DFF_PN1_")) found_match = true, direct_dict[ID($_DFF_PN1_)] = ID($_DFFE_PN1P_); -				if (patmatch(pattern, "$_DFF_PP0_")) found_match = true, direct_dict[ID($_DFF_PP0_)] = ID($_DFFE_PP0P_); -				if (patmatch(pattern, "$_DFF_PP1_")) found_match = true, direct_dict[ID($_DFF_PP1_)] = ID($_DFFE_PP1P_); - -				if (patmatch(pattern, "$_SDFF_NN0_")) found_match = true, direct_dict[ID($_SDFF_NN0_)] = ID($_SDFFE_NN0P_); -				if (patmatch(pattern, "$_SDFF_NN1_")) found_match = true, direct_dict[ID($_SDFF_NN1_)] = ID($_SDFFE_NN1P_); -				if (patmatch(pattern, "$_SDFF_NP0_")) found_match = true, direct_dict[ID($_SDFF_NP0_)] = ID($_SDFFE_NP0P_); -				if (patmatch(pattern, "$_SDFF_NP1_")) found_match = true, direct_dict[ID($_SDFF_NP1_)] = ID($_SDFFE_NP1P_); -				if (patmatch(pattern, "$_SDFF_PN0_")) found_match = true, direct_dict[ID($_SDFF_PN0_)] = ID($_SDFFE_PN0P_); -				if (patmatch(pattern, "$_SDFF_PN1_")) found_match = true, direct_dict[ID($_SDFF_PN1_)] = ID($_SDFFE_PN1P_); -				if (patmatch(pattern, "$_SDFF_PP0_")) found_match = true, direct_dict[ID($_SDFF_PP0_)] = ID($_SDFFE_PP0P_); -				if (patmatch(pattern, "$_SDFF_PP1_")) found_match = true, direct_dict[ID($_SDFF_PP1_)] = ID($_SDFFE_PP1P_); -				if (!found_match) -					log_cmd_error("No cell types matched pattern '%s'.\n", pattern); -				continue; -			} -			break; -		} -		extra_args(args, argidx, design); - -		if (!direct_dict.empty()) { -			log("Selected cell types for direct conversion:\n"); -			for (auto &it : direct_dict) -				log("  %s -> %s\n", log_id(it.first), log_id(it.second)); -		} - -		for (auto mod : design->selected_modules()) -			if (!mod->has_processes_warn()) -			{ -				if (unmap_mode) { -					SigMap sigmap(mod); -					for (auto cell : mod->selected_cells()) { -						if (cell->type == ID($dffe)) { -							if (min_ce_use >= 0) { -								int ce_use = 0; -								for (auto cell_other : mod->selected_cells()) { -									if (cell_other->type != cell->type) -										continue; -									if (sigmap(cell->getPort(ID::EN)) == sigmap(cell_other->getPort(ID::EN))) -										ce_use++; -								} -								if (ce_use >= min_ce_use) -									continue; -							} - -							RTLIL::SigSpec tmp = mod->addWire(NEW_ID, GetSize(cell->getPort(ID::D))); -							mod->addDff(NEW_ID, cell->getPort(ID::CLK), tmp, cell->getPort(ID::Q), cell->getParam(ID::CLK_POLARITY).as_bool()); -							if (cell->getParam(ID::EN_POLARITY).as_bool()) -								mod->addMux(NEW_ID, cell->getPort(ID::Q), cell->getPort(ID::D), cell->getPort(ID::EN), tmp); -							else -								mod->addMux(NEW_ID, cell->getPort(ID::D), cell->getPort(ID::Q), cell->getPort(ID::EN), tmp); -							mod->remove(cell); -							continue; -						} -						if (cell->type.begins_with("$_DFFE_")) { -							if (min_ce_use >= 0) { -								int ce_use = 0; -								for (auto cell_other : mod->selected_cells()) { -									if (cell_other->type != cell->type) -										continue; -									if (sigmap(cell->getPort(ID::E)) == sigmap(cell_other->getPort(ID::E))) -										ce_use++; -								} -								if (ce_use >= min_ce_use) -									continue; -							} - -							bool clk_pol = cell->type.compare(7, 1, "P") == 0; -							bool en_pol = cell->type.compare(8, 1, "P") == 0; -							RTLIL::SigSpec tmp = mod->addWire(NEW_ID); -							mod->addDff(NEW_ID, cell->getPort(ID::C), tmp, cell->getPort(ID::Q), clk_pol); -							if (en_pol) -								mod->addMux(NEW_ID, cell->getPort(ID::Q), cell->getPort(ID::D), cell->getPort(ID::E), tmp); -							else -								mod->addMux(NEW_ID, cell->getPort(ID::D), cell->getPort(ID::Q), cell->getPort(ID::E), tmp); -							mod->remove(cell); -							continue; -						} -					} -					continue; -				} - -				Dff2dffeWorker worker(mod, direct_dict); -				worker.run(); -			} -	} -} Dff2dffePass; - -PRIVATE_NAMESPACE_END diff --git a/passes/techmap/dff2dffs.cc b/passes/techmap/dff2dffs.cc deleted file mode 100644 index 6c2cca4bc..000000000 --- a/passes/techmap/dff2dffs.cc +++ /dev/null @@ -1,165 +0,0 @@ -/* - *  yosys -- Yosys Open SYnthesis Suite - * - *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at> - *  Copyright (C) 2018  David Shah <dave@ds0.me> - * - *  Permission to use, copy, modify, and/or distribute this software for any - *  purpose with or without fee is hereby granted, provided that the above - *  copyright notice and this permission notice appear in all copies. - * - *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - */ - -#include "kernel/yosys.h" -#include "kernel/sigtools.h" - -USING_YOSYS_NAMESPACE -PRIVATE_NAMESPACE_BEGIN - -struct Dff2dffsPass : public Pass { -	Dff2dffsPass() : Pass("dff2dffs", "process sync set/reset with SR over CE priority") { } -	void help() override -	{ -		log("\n"); -		log("    dff2dffs [options] [selection]\n"); -		log("\n"); -		log("Merge synchronous set/reset $_MUX_ cells to create $_SDFF_[NP][NP][01]_, to be run before\n"); -		log("dff2dffe for SR over CE priority.\n"); -		log("\n"); -		log("    -match-init\n"); -		log("        Disallow merging synchronous set/reset that has polarity opposite of the\n"); -		log("        output wire's init attribute (if any).\n"); -		log("\n"); -	} -	void execute(std::vector<std::string> args, RTLIL::Design *design) override -	{ -		log_header(design, "Executing dff2dffs pass (merge synchronous set/reset into FF cells).\n"); - -		bool match_init = false; -		size_t argidx; -		for (argidx = 1; argidx < args.size(); argidx++) -		{ -			// if (args[argidx] == "-singleton") { -			// 	singleton_mode = true; -			// 	continue; -			// } -			if (args[argidx] == "-match-init") { -				match_init = true; -				continue; -			} -			break; -		} -		extra_args(args, argidx, design); - -		pool<IdString> dff_types; -		dff_types.insert(ID($_DFF_N_)); -		dff_types.insert(ID($_DFF_P_)); - -		for (auto module : design->selected_modules()) -		{ -			log("Merging set/reset $_MUX_ cells into DFFs in %s.\n", log_id(module)); - -			SigMap sigmap(module); -			dict<SigBit, Cell*> sr_muxes; -			vector<Cell*> ff_cells; - -			for (auto cell : module->selected_cells()) -			{ -				if (dff_types.count(cell->type)) { -					ff_cells.push_back(cell); -					continue; -				} - -				if (cell->type != ID($_MUX_)) -					continue; - -				SigBit bit_a = sigmap(cell->getPort(ID::A)); -				SigBit bit_b = sigmap(cell->getPort(ID::B)); - -				if (bit_a.wire == nullptr || bit_b.wire == nullptr) -					sr_muxes[sigmap(cell->getPort(ID::Y))] = cell; -			} - -			for (auto cell : ff_cells) -			{ -				SigSpec sig_d = cell->getPort(ID::D); - -				if (GetSize(sig_d) < 1) -					continue; - -				SigBit bit_d = sigmap(sig_d[0]); - -				if (sr_muxes.count(bit_d) == 0) -					continue; - -				Cell *mux_cell = sr_muxes.at(bit_d); -				SigBit bit_a = sigmap(mux_cell->getPort(ID::A)); -				SigBit bit_b = sigmap(mux_cell->getPort(ID::B)); -				SigBit bit_s = sigmap(mux_cell->getPort(ID::S)); - -				SigBit sr_val, sr_sig; -				bool invert_sr; -				sr_sig = bit_s; -				if (bit_a.wire == nullptr) { -					bit_d = bit_b; -					sr_val = bit_a; -					invert_sr = true; -				} else { -					log_assert(bit_b.wire == nullptr); -					bit_d = bit_a; -					sr_val = bit_b; -					invert_sr = false; -				} - -				if (match_init) { -					SigBit bit_q = cell->getPort(ID::Q); -					if (bit_q.wire) { -						auto it = bit_q.wire->attributes.find(ID::init); -						if (it != bit_q.wire->attributes.end()) { -							auto init_val = it->second[bit_q.offset]; -							if (init_val == State::S1 && sr_val != State::S1) -								continue; -							if (init_val == State::S0 && sr_val != State::S0) -								continue; -						} -					} -				} - -				log("  Merging %s (A=%s, B=%s, S=%s) into %s (%s).\n", log_id(mux_cell), -						log_signal(bit_a), log_signal(bit_b), log_signal(bit_s), log_id(cell), log_id(cell->type)); - -				if (sr_val == State::S1) { -					if (cell->type == ID($_DFF_N_)) { -						if (invert_sr) cell->type = ID($_SDFF_NN1_); -						else cell->type = ID($_SDFF_NP1_); -					} else { -						log_assert(cell->type == ID($_DFF_P_)); -						if (invert_sr) cell->type = ID($_SDFF_PN1_); -						else cell->type = ID($_SDFF_PP1_); -					} -				} else { -					if (cell->type == ID($_DFF_N_)) { -						if (invert_sr) cell->type = ID($_SDFF_NN0_); -						else cell->type = ID($_SDFF_NP0_); -					} else { -						log_assert(cell->type == ID($_DFF_P_)); -						if (invert_sr) cell->type = ID($_SDFF_PN0_); -						else cell->type = ID($_SDFF_PP0_); -					} -				} -				cell->setPort(ID::R, sr_sig); -				cell->setPort(ID::D, bit_d); -			} -		} -	} -} Dff2dffsPass; - -PRIVATE_NAMESPACE_END diff --git a/passes/techmap/dffinit.cc b/passes/techmap/dffinit.cc index c60a901c1..44af043db 100644 --- a/passes/techmap/dffinit.cc +++ b/passes/techmap/dffinit.cc @@ -19,6 +19,7 @@  #include "kernel/yosys.h"  #include "kernel/sigtools.h" +#include "kernel/ffinit.h"  USING_YOSYS_NAMESPACE  PRIVATE_NAMESPACE_BEGIN @@ -94,29 +95,10 @@ struct DffinitPass : public Pass {  		for (auto module : design->selected_modules())  		{  			SigMap sigmap(module); -			dict<SigBit, State> init_bits; -			pool<SigBit> cleanup_bits; -			pool<SigBit> used_bits; - -			for (auto wire : module->selected_wires()) { -				if (wire->attributes.count(ID::init)) { -					Const value = wire->attributes.at(ID::init); -					for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++) -						if (value[i] != State::Sx) -							init_bits[sigmap(SigBit(wire, i))] = value[i]; -				} -				if (wire->port_output) -					for (auto bit : sigmap(wire)) -						used_bits.insert(bit); -			} +			FfInitVals initvals(&sigmap, module);  			for (auto cell : module->selected_cells())  			{ -				for (auto it : cell->connections()) -					if (!cell->known() || cell->input(it.first)) -						for (auto bit : sigmap(it.second)) -							used_bits.insert(bit); -  				if (ff_types.count(cell->type) == 0)  					continue; @@ -131,17 +113,18 @@ struct DffinitPass : public Pass {  					if (cell->hasParam(it.second))  						value = cell->getParam(it.second); +					Const initval = initvals(sig); +					initvals.remove_init(sig);  					for (int i = 0; i < GetSize(sig); i++) { -						if (init_bits.count(sig[i]) == 0) +						if (initval[i] == State::Sx)  							continue;  						while (GetSize(value.bits) <= i)  							value.bits.push_back(State::S0); -						if (noreinit && value.bits[i] != State::Sx && value.bits[i] != init_bits.at(sig[i])) +						if (noreinit && value.bits[i] != State::Sx && value.bits[i] != initval[i])  							log_error("Trying to assign a different init value for %s.%s.%s which technically "  									"have a conflicted init value.\n",  									log_id(module), log_id(cell), log_id(it.second)); -						value.bits[i] = init_bits.at(sig[i]); -						cleanup_bits.insert(sig[i]); +						value.bits[i] = initval[i];  					}  					if (highlow_mode && GetSize(value) != 0) { @@ -161,23 +144,6 @@ struct DffinitPass : public Pass {  					}  				}  			} - -			for (auto wire : module->selected_wires()) -				if (wire->attributes.count(ID::init)) { -					Const &value = wire->attributes.at(ID::init); -					bool do_cleanup = true; -					for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++) { -						SigBit bit = sigmap(SigBit(wire, i)); -						if (cleanup_bits.count(bit) || !used_bits.count(bit)) -							value[i] = State::Sx; -						else if (value[i] != State::Sx) -							do_cleanup = false; -					} -					if (do_cleanup) { -						log("Removing init attribute from wire %s.%s.\n", log_id(module), log_id(wire)); -						wire->attributes.erase(ID::init); -					} -				}  		}  	}  } DffinitPass; diff --git a/passes/techmap/dfflegalize.cc b/passes/techmap/dfflegalize.cc index 13ce4f49a..c1e7e557d 100644 --- a/passes/techmap/dfflegalize.cc +++ b/passes/techmap/dfflegalize.cc @@ -19,6 +19,7 @@  #include "kernel/yosys.h"  #include "kernel/sigtools.h" +#include "kernel/ffinit.h"  USING_YOSYS_NAMESPACE  PRIVATE_NAMESPACE_BEGIN @@ -170,7 +171,7 @@ struct DffLegalizePass : public Pass {  	dict<SigBit, int> srst_used;  	SigMap sigmap; -	dict<SigBit, std::pair<State,SigBit>> initbits; +	FfInitVals initvals;  	int flip_initmask(int mask) {  		int res = mask & INIT_X; @@ -303,13 +304,7 @@ struct DffLegalizePass : public Pass {  			return;  		} -		State initval = State::Sx; -		SigBit initbit; -		if (GetSize(sig_q) > 0 && initbits.count(sigmap(sig_q[0]))) { -			const auto &d = initbits.at(sigmap(sig_q[0])); -			initval = d.first; -			initbit = d.second; -		} +		State initval = initvals(sig_q[0]);  		FfInit initmask = INIT_X;  		if (initval == State::S0) @@ -345,12 +340,8 @@ flip_dqi:  							sig_d = cell->module->NotGate(NEW_ID, sig_d[0]);  						SigBit new_q = SigSpec(cell->module->addWire(NEW_ID))[0];  						cell->module->addNotGate(NEW_ID, new_q, sig_q[0]); -						if (initbit.wire) { -							initbit.wire->attributes.at(ID::init)[initbit.offset] = State::Sx; -							initbit = new_q; -							new_q.wire->attributes[ID::init] = initval; -							initbits[new_q] = std::make_pair(initval, new_q); -						} +						initvals.remove_init(sig_q[0]); +						initvals.set_init(new_q, initval);  						sig_q = new_q;  						continue;  					} @@ -427,7 +418,8 @@ unmap_enable:  					ff_type = has_set ? FF_ADFFE1 : FF_ADFFE0;  					break;  				} -				if (supported_dffsr & initmask) { +				if (supported_cells[has_en ? FF_DFFSRE : FF_DFFSR] & initmask) { +adff_to_dffsr:  					// Throw in a set/reset, retry in DFFSR/DFFSRE branch.  					if (has_set) {  						sig_s = sig_r; @@ -450,6 +442,9 @@ unmap_enable:  					ff_type = has_set ? FF_ADFF1 : FF_ADFF0;  					goto unmap_enable;  				} +				if (supported_dffsr & initmask) { +					goto adff_to_dffsr; +				}  				log_assert(!((has_set ? supported_adff1 : supported_adff0) & initmask));  				// Alright, so this particular combination of initval and  				// resetval is not natively supported.  First, try flipping @@ -484,15 +479,12 @@ unmap_enable:  				}  				log_warning("Emulating mismatched async reset and init with several FFs and a mux for %s.%s\n", log_id(cell->module->name), log_id(cell->name)); -				if (initbit.wire) -					initbit.wire->attributes.at(ID::init)[initbit.offset] = State::Sx; +				initvals.remove_init(sig_q[0]);  				Wire *adff_q = cell->module->addWire(NEW_ID);  				Wire *dff_q = cell->module->addWire(NEW_ID);  				Wire *sel_q = cell->module->addWire(NEW_ID); -				dff_q->attributes[ID::init] = initval; -				initbits[SigBit(dff_q, 0)] = std::make_pair(initval, SigBit(dff_q, 0)); -				sel_q->attributes[ID::init] = State::S0; -				initbits[SigBit(sel_q, 0)] = std::make_pair(State::S0, SigBit(sel_q, 0)); +				initvals.set_init(SigBit(dff_q, 0), initval); +				initvals.set_init(SigBit(sel_q, 0), State::S0);  				Cell *cell_dff;  				Cell *cell_adff;  				Cell *cell_sel; @@ -588,21 +580,15 @@ flip_dqisr:;  				}  				log_warning("Emulating async set + reset with several FFs and a mux for %s.%s\n", log_id(cell->module->name), log_id(cell->name)); -				if (initbit.wire) -					initbit.wire->attributes.at(ID::init)[initbit.offset] = State::Sx; +				initvals.remove_init(sig_q[0]);  				Wire *adff0_q = cell->module->addWire(NEW_ID);  				Wire *adff1_q = cell->module->addWire(NEW_ID);  				Wire *sel_q = cell->module->addWire(NEW_ID); -				if (init0) { -					adff0_q->attributes[ID::init] = initval; -					initbits[SigBit(adff0_q, 0)] = std::make_pair(initval, SigBit(adff0_q, 0)); -				} -				if (init1) { -					adff1_q->attributes[ID::init] = initval; -					initbits[SigBit(adff1_q, 0)] = std::make_pair(initval, SigBit(adff1_q, 0)); -				} -				sel_q->attributes[ID::init] = initsel; -				initbits[SigBit(sel_q, 0)] = std::make_pair(initsel, SigBit(sel_q, 0)); +				if (init0) +					initvals.set_init(SigBit(adff0_q, 0), initval); +				if (init1) +					initvals.set_init(SigBit(adff1_q, 0), initval); +				initvals.set_init(SigBit(sel_q, 0), initsel);  				Cell *cell_adff0;  				Cell *cell_adff1;  				Cell *cell_sel; @@ -741,15 +727,12 @@ flip_dqisr:;  				// The only hope left is breaking down to adff + dff + dlatch + mux.  				log_warning("Emulating mismatched async reset and init with several latches and a mux for %s.%s\n", log_id(cell->module->name), log_id(cell->name)); -				if (initbit.wire) -					initbit.wire->attributes.at(ID::init)[initbit.offset] = State::Sx; +				initvals.remove_init(sig_q[0]);  				Wire *adlatch_q = cell->module->addWire(NEW_ID);  				Wire *dlatch_q = cell->module->addWire(NEW_ID);  				Wire *sel_q = cell->module->addWire(NEW_ID); -				dlatch_q->attributes[ID::init] = initval; -				initbits[SigBit(dlatch_q, 0)] = std::make_pair(initval, SigBit(dlatch_q, 0)); -				sel_q->attributes[ID::init] = State::S0; -				initbits[SigBit(sel_q, 0)] = std::make_pair(State::S0, SigBit(sel_q, 0)); +				initvals.set_init(SigBit(dlatch_q, 0), initval); +				initvals.set_init(SigBit(sel_q, 0), State::S0);  				Cell *cell_dlatch;  				Cell *cell_adlatch;  				Cell *cell_sel; @@ -797,21 +780,15 @@ flip_dqisr:;  				}  				log_warning("Emulating async set + reset with several latches and a mux for %s.%s\n", log_id(cell->module->name), log_id(cell->name)); -				if (initbit.wire) -					initbit.wire->attributes.at(ID::init)[initbit.offset] = State::Sx; +				initvals.remove_init(sig_q[0]);  				Wire *adlatch0_q = cell->module->addWire(NEW_ID);  				Wire *adlatch1_q = cell->module->addWire(NEW_ID);  				Wire *sel_q = cell->module->addWire(NEW_ID); -				if (init0) { -					adlatch0_q->attributes[ID::init] = initval; -					initbits[SigBit(adlatch0_q, 0)] = std::make_pair(initval, SigBit(adlatch0_q, 0)); -				} -				if (init1) { -					adlatch1_q->attributes[ID::init] = initval; -					initbits[SigBit(adlatch1_q, 0)] = std::make_pair(initval, SigBit(adlatch1_q, 0)); -				} -				sel_q->attributes[ID::init] = initsel; -				initbits[SigBit(sel_q, 0)] = std::make_pair(initsel, SigBit(sel_q, 0)); +				if (init0) +					initvals.set_init(SigBit(adlatch0_q, 0), initval); +				if (init1) +					initvals.set_init(SigBit(adlatch1_q, 0), initval); +				initvals.set_init(SigBit(sel_q, 0), initsel);  				Cell *cell_adlatch0;  				Cell *cell_adlatch1;  				Cell *cell_sel; @@ -1294,35 +1271,7 @@ unrecognized:  		for (auto module : design->selected_modules())  		{  			sigmap.set(module); -			initbits.clear(); - -			for (auto wire : module->wires()) -			{ -				if (wire->attributes.count(ID::init) == 0) -					continue; - -				SigSpec wirebits = sigmap(wire); -				Const initval = wire->attributes.at(ID::init); - -				for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++) -				{ -					SigBit bit = wirebits[i]; -					State val = initval[i]; - -					if (val != State::S0 && val != State::S1 && bit.wire != nullptr) -						continue; - -					if (initbits.count(bit)) { -						if (initbits.at(bit).first != val) -							log_error("Conflicting init values for signal %s (%s = %s != %s).\n", -									log_signal(bit), log_signal(SigBit(wire, i)), -									log_signal(val), log_signal(initbits.at(bit).first)); -						continue; -					} - -					initbits[bit] = std::make_pair(val,SigBit(wire,i)); -				} -			} +			initvals.set(&sigmap, module);  			if (mince || minsrst) {  				ce_used.clear(); @@ -1365,7 +1314,7 @@ unrecognized:  		}  		sigmap.clear(); -		initbits.clear(); +		initvals.clear();  		ce_used.clear();  		srst_used.clear();  	} diff --git a/passes/techmap/dffunmap.cc b/passes/techmap/dffunmap.cc new file mode 100644 index 000000000..fb107ff75 --- /dev/null +++ b/passes/techmap/dffunmap.cc @@ -0,0 +1,107 @@ +/* + *  yosys -- Yosys Open SYnthesis Suite + * + *  Copyright (C) 2020  Marcelina KoĆcielnicka <mwk@0x04.net> + * + *  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/ff.h" + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN + +struct DffunmapPass : public Pass { +	DffunmapPass() : Pass("dffunmap", "unmap clock enable and synchronous reset from FFs") { } +	void help() override +	{ +		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| +		log("\n"); +		log("    dffunmap [options] [selection]\n"); +		log("\n"); +		log("This pass transforms FF types with clock enable and/or synchronous reset into\n"); +		log("their base type (with neither clock enable nor sync reset) by emulating the clock\n"); +		log("enable and synchronous reset with multiplexers on the cell input.\n"); +		log("\n"); +		log("    -ce-only\n"); +		log("        unmap only clock enables, leave synchronous resets alone.\n"); +		log("\n"); +		log("    -srst-only\n"); +		log("        unmap only synchronous resets, leave clock enables alone.\n"); +		log("\n"); +	} +	void execute(std::vector<std::string> args, RTLIL::Design *design) override +	{ +		log_header(design, "Executing DFFUNMAP pass (unmap clock enable and synchronous reset from FFs).\n"); + +		bool ce_only = false; +		bool srst_only = false; + +		size_t argidx; +		for (argidx = 1; argidx < args.size(); argidx++) { +			if (args[argidx] == "-ce-only") { +				ce_only = true; +				continue; +			} +			if (args[argidx] == "-srst-only") { +				srst_only = true; +				continue; +			} +			break; +		} +		extra_args(args, argidx, design); + +		if (ce_only && srst_only) +			log_cmd_error("Options -ce-only and -srst-only are mutually exclusive!\n"); + +		for (auto mod : design->selected_modules()) +		{ +			SigMap sigmap(mod); +			FfInitVals initvals(&sigmap, mod); + +			for (auto cell : mod->selected_cells()) +			{ +				if (!RTLIL::builtin_ff_cell_types().count(cell->type)) +					continue; + +				FfData ff(&initvals, cell); +				IdString name = cell->name; + +				if (!ff.has_clk) +					continue; + +				if (ce_only) { +					if (!ff.has_en) +						continue; +					ff.unmap_ce(mod); +				} else if (srst_only) { +					if (!ff.has_srst) +						continue; +					ff.unmap_srst(mod); +				} else { +					if (!ff.has_en && !ff.has_srst) +						continue; +					ff.unmap_ce_srst(mod); +				} + +				mod->remove(cell); +				ff.emit(mod, name); +			} +		} +	} +} DffunmapPass; + +PRIVATE_NAMESPACE_END diff --git a/passes/techmap/extract.cc b/passes/techmap/extract.cc index 7278cb680..f5966fac0 100644 --- a/passes/techmap/extract.cc +++ b/passes/techmap/extract.cc @@ -354,7 +354,7 @@ struct ExtractPass : public Pass {  		log("\n");  		log("This pass looks for subcircuits that are isomorphic to any of the modules\n");  		log("in the given map file and replaces them with instances of this modules. The\n"); -		log("map file can be a Verilog source file (*.v) or an ilang file (*.il).\n"); +		log("map file can be a Verilog source file (*.v) or an RTLIL source file (*.il).\n");  		log("\n");  		log("    -map <map_file>\n");  		log("        use the modules in this file as reference. This option can be used\n"); @@ -409,7 +409,7 @@ struct ExtractPass : public Pass {  		log("the following options are to be used instead of the -map option.\n");  		log("\n");  		log("    -mine <out_file>\n"); -		log("        mine for frequent subcircuits and write them to the given ilang file\n"); +		log("        mine for frequent subcircuits and write them to the given RTLIL file\n");  		log("\n");  		log("    -mine_cells_span <min> <max>\n");  		log("        only mine for subcircuits with the specified number of cells\n"); @@ -578,7 +578,7 @@ struct ExtractPass : public Pass {  		}  		if (map_filenames.empty() && mine_outfile.empty()) -			log_cmd_error("Missing option -map <verilog_or_ilang_file> or -mine <output_ilang_file>.\n"); +			log_cmd_error("Missing option -map <verilog_or_rtlil_file> or -mine <output_rtlil_file>.\n");  		RTLIL::Design *map = nullptr; @@ -606,7 +606,7 @@ struct ExtractPass : public Pass {  						delete map;  						log_cmd_error("Can't open map file `%s'.\n", filename.c_str());  					} -					Frontend::frontend_call(map, &f, filename, (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".il") == 0 ? "ilang" : "verilog")); +					Frontend::frontend_call(map, &f, filename, (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".il") == 0 ? "rtlil" : "verilog"));  					f.close();  					if (filename.size() <= 3 || filename.compare(filename.size()-3, std::string::npos, ".il") != 0) { @@ -744,7 +744,7 @@ struct ExtractPass : public Pass {  			f.open(mine_outfile.c_str(), std::ofstream::trunc);  			if (f.fail())  				log_error("Can't open output file `%s'.\n", mine_outfile.c_str()); -			Backend::backend_call(map, &f, mine_outfile, "ilang"); +			Backend::backend_call(map, &f, mine_outfile, "rtlil");  			f.close();  		} diff --git a/passes/techmap/flatten.cc b/passes/techmap/flatten.cc index b5f55cffa..08978f446 100644 --- a/passes/techmap/flatten.cc +++ b/passes/techmap/flatten.cc @@ -152,15 +152,14 @@ struct FlattenWorker  		// Attach port connections of the flattened cell -		SigMap tpl_sigmap(tpl);  		pool<SigBit> tpl_driven;  		for (auto tpl_cell : tpl->cells())  			for (auto &tpl_conn : tpl_cell->connections())  				if (tpl_cell->output(tpl_conn.first)) -					for (auto bit : tpl_sigmap(tpl_conn.second)) +					for (auto bit : tpl_conn.second)  						tpl_driven.insert(bit);  		for (auto &tpl_conn : tpl->connections()) -			for (auto bit : tpl_sigmap(tpl_conn.first)) +			for (auto bit : tpl_conn.first)  				tpl_driven.insert(bit);  		SigMap sigmap(module); @@ -190,7 +189,7 @@ struct FlattenWorker  			} else {  				SigSpec sig_tpl = tpl_wire, sig_mod = port_it.second;  				for (int i = 0; i < GetSize(sig_tpl) && i < GetSize(sig_mod); i++) { -					if (tpl_driven.count(tpl_sigmap(sig_tpl[i]))) { +					if (tpl_driven.count(sig_tpl[i])) {  						new_conn.first.append(sig_mod[i]);  						new_conn.second.append(sig_tpl[i]);  					} else { diff --git a/passes/techmap/shregmap.cc b/passes/techmap/shregmap.cc index 237c261ae..b971068f7 100644 --- a/passes/techmap/shregmap.cc +++ b/passes/techmap/shregmap.cc @@ -19,6 +19,7 @@  #include "kernel/yosys.h"  #include "kernel/sigtools.h" +#include "kernel/ffinit.h"  USING_YOSYS_NAMESPACE  PRIVATE_NAMESPACE_BEGIN @@ -100,9 +101,8 @@ struct ShregmapWorker  	int dff_count, shreg_count;  	pool<Cell*> remove_cells; -	pool<SigBit> remove_init; -	dict<SigBit, bool> sigbit_init; +	FfInitVals initvals;  	dict<SigBit, Cell*> sigbit_chain_next;  	dict<SigBit, Cell*> sigbit_chain_prev;  	pool<SigBit> sigbit_with_non_chain_users; @@ -116,16 +116,6 @@ struct ShregmapWorker  				for (auto bit : sigmap(wire))  					sigbit_with_non_chain_users.insert(bit);  			} - -			if (wire->attributes.count(ID::init)) { -				SigSpec initsig = sigmap(wire); -				Const initval = wire->attributes.at(ID::init); -				for (int i = 0; i < GetSize(initsig) && i < GetSize(initval); i++) -					if (initval[i] == State::S0 && !opts.zinit) -						sigbit_init[initsig[i]] = false; -					else if (initval[i] == State::S1) -						sigbit_init[initsig[i]] = true; -			}  		}  		for (auto cell : module->cells()) @@ -137,8 +127,9 @@ struct ShregmapWorker  				SigBit d_bit = sigmap(cell->getPort(d_port).as_bit());  				SigBit q_bit = sigmap(cell->getPort(q_port).as_bit()); +				State initval = initvals(q_bit); -				if (opts.init || sigbit_init.count(q_bit) == 0) +				if (opts.init || initval == State::Sx || (opts.zinit && initval == State::S0))  				{  					auto r = sigbit_chain_next.insert(std::make_pair(d_bit, cell));  					if (!r.second) { @@ -310,22 +301,17 @@ struct ShregmapWorker  			if (opts.init) {  				vector<State> initval;  				for (int i = depth-1; i >= 0; i--) { -					SigBit bit = sigmap(chain[cursor+i]->getPort(q_port).as_bit()); -					if (sigbit_init.count(bit) == 0) -						initval.push_back(State::Sx); -					else if (sigbit_init.at(bit)) -						initval.push_back(State::S1); -					else -						initval.push_back(State::S0); -					remove_init.insert(bit); +					SigBit bit = chain[cursor+i]->getPort(q_port).as_bit(); +					initval.push_back(initvals(bit)); +					initvals.remove_init(bit);  				}  				first_cell->setParam(ID::INIT, initval);  			}  			if (opts.zinit)  				for (int i = depth-1; i >= 0; i--) { -					SigBit bit = sigmap(chain[cursor+i]->getPort(q_port).as_bit()); -					remove_init.insert(bit); +					SigBit bit = chain[cursor+i]->getPort(q_port).as_bit(); +					initvals.remove_init(bit);  				}  			if (opts.params) @@ -364,22 +350,6 @@ struct ShregmapWorker  		for (auto cell : remove_cells)  			module->remove(cell); -		for (auto wire : module->wires()) -		{ -			if (wire->attributes.count(ID::init) == 0) -				continue; - -			SigSpec initsig = sigmap(wire); -			Const &initval = wire->attributes.at(ID::init); - -			for (int i = 0; i < GetSize(initsig) && i < GetSize(initval); i++) -				if (remove_init.count(initsig[i])) -					initval[i] = State::Sx; - -			if (SigSpec(initval).is_fully_undef()) -				wire->attributes.erase(ID::init); -		} -  		remove_cells.clear();  		sigbit_chain_next.clear();  		sigbit_chain_prev.clear(); @@ -389,6 +359,7 @@ struct ShregmapWorker  	ShregmapWorker(Module *module, const ShregmapOptions &opts) :  			module(module), sigmap(module), opts(opts), dff_count(0), shreg_count(0)  	{ +		initvals.set(&sigmap, module);  		make_sigbit_chain_next_prev();  		find_chain_start_cells(); diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index f98d1564a..d43737c8d 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -20,6 +20,7 @@  #include "kernel/yosys.h"  #include "kernel/utils.h"  #include "kernel/sigtools.h" +#include "kernel/ffinit.h"  #include "libs/sha1/sha1.h"  #include <stdlib.h> @@ -232,16 +233,14 @@ struct TechmapWorker  			}  		} -		SigMap tpl_sigmap(tpl);  		pool<SigBit> tpl_written_bits; -  		for (auto tpl_cell : tpl->cells())  		for (auto &conn : tpl_cell->connections())  			if (tpl_cell->output(conn.first)) -				for (auto bit : tpl_sigmap(conn.second)) +				for (auto bit : conn.second)  					tpl_written_bits.insert(bit);  		for (auto &conn : tpl->connections()) -			for (auto bit : tpl_sigmap(conn.first)) +			for (auto bit : conn.first)  				tpl_written_bits.insert(bit);  		SigMap port_signal_map; @@ -279,7 +278,7 @@ struct TechmapWorker  				SigSpec sig_tpl = w, sig_tpl_pf = w, sig_mod = it.second;  				apply_prefix(cell->name, sig_tpl_pf, module);  				for (int i = 0; i < GetSize(sig_tpl) && i < GetSize(sig_mod); i++) { -					if (tpl_written_bits.count(tpl_sigmap(sig_tpl[i]))) { +					if (tpl_written_bits.count(sig_tpl[i])) {  						c.first.append(sig_mod[i]);  						c.second.append(sig_tpl_pf[i]);  					} else { @@ -426,18 +425,7 @@ struct TechmapWorker  		LogMakeDebugHdl mkdebug;  		SigMap sigmap(module); - -		dict<SigBit, State> init_bits; -		pool<SigBit> remove_init_bits; - -		for (auto wire : module->wires()) { -			if (wire->attributes.count(ID::init)) { -				Const value = wire->attributes.at(ID::init); -				for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++) -					if (value[i] != State::Sx) -						init_bits[sigmap(SigBit(wire, i))] = value[i]; -			} -		} +		FfInitVals initvals(&sigmap, module);  		TopoSort<RTLIL::Cell*, IdString::compare_ptr_by_name<RTLIL::Cell>> cells;  		dict<RTLIL::Cell*, pool<RTLIL::SigBit>> cell_to_inbit; @@ -643,6 +631,8 @@ struct TechmapWorker  				if (tpl->avail_parameters.count(ID::_TECHMAP_CELLTYPE_) != 0)  					parameters.emplace(ID::_TECHMAP_CELLTYPE_, RTLIL::unescape_id(cell->type)); +				if (tpl->avail_parameters.count(ID::_TECHMAP_CELLNAME_) != 0) +					parameters.emplace(ID::_TECHMAP_CELLNAME_, RTLIL::unescape_id(cell->name));  				for (auto &conn : cell->connections()) {  					if (tpl->avail_parameters.count(stringf("\\_TECHMAP_CONSTMSK_%s_", log_id(conn.first))) != 0) { @@ -659,15 +649,7 @@ struct TechmapWorker  						parameters.emplace(stringf("\\_TECHMAP_CONSTVAL_%s_", log_id(conn.first)), RTLIL::SigSpec(v).as_const());  					}  					if (tpl->avail_parameters.count(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first))) != 0) { -						auto sig = sigmap(conn.second); -						RTLIL::Const value(State::Sx, sig.size()); -						for (int i = 0; i < sig.size(); i++) { -							auto it = init_bits.find(sig[i]); -							if (it != init_bits.end()) { -								value[i] = it->second; -							} -						} -						parameters.emplace(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first)), value); +						parameters.emplace(stringf("\\_TECHMAP_WIREINIT_%s_", log_id(conn.first)), initvals(conn.second));  					}  				} @@ -817,11 +799,31 @@ struct TechmapWorker  									}  								} +								// Handle outputs first, as these cannot be remapped. +								for (auto &conn : cell->connections()) +								{ +									Wire *twire = tpl->wire(conn.first); +									if (!twire->port_output) +										continue; + +									for (int i = 0; i < GetSize(conn.second); i++) { +										RTLIL::SigBit bit = sigmap(conn.second[i]); +										RTLIL::SigBit tplbit(twire, i); +										cellbits_to_tplbits[bit] = tplbit; +									} +								} + +								// Now handle inputs, remapping as necessary.  								for (auto &conn : cell->connections()) +								{ +									Wire *twire = tpl->wire(conn.first); +									if (twire->port_output) +										continue; +  									for (int i = 0; i < GetSize(conn.second); i++)  									{  										RTLIL::SigBit bit = sigmap(conn.second[i]); -										RTLIL::SigBit tplbit(tpl->wire(conn.first), i); +										RTLIL::SigBit tplbit(twire, i);  										if (bit.wire == nullptr)  										{ @@ -836,6 +838,7 @@ struct TechmapWorker  										else  											cellbits_to_tplbits[bit] = tplbit;  									} +								}  								RTLIL::SigSig port_conn;  								for (auto &it : port_connmap) { @@ -912,7 +915,7 @@ struct TechmapWorker  								auto sig = sigmap(it->second);  								for (int i = 0; i < sig.size(); i++)  									if (val[i] == State::S1) -										remove_init_bits.insert(sig[i]); +										initvals.remove_init(sig[i]);  							}  						}  					} @@ -961,25 +964,6 @@ struct TechmapWorker  			handled_cells.insert(cell);  		} -		if (!remove_init_bits.empty()) { -			for (auto wire : module->wires()) -				if (wire->attributes.count(ID::init)) { -					Const &value = wire->attributes.at(ID::init); -					bool do_cleanup = true; -					for (int i = 0; i < min(GetSize(value), GetSize(wire)); i++) { -						SigBit bit = sigmap(SigBit(wire, i)); -						if (remove_init_bits.count(bit)) -							value[i] = State::Sx; -						else if (value[i] != State::Sx) -							do_cleanup = false; -					} -					if (do_cleanup) { -						log("Removing init attribute from wire %s.%s.\n", log_id(module), log_id(wire)); -						wire->attributes.erase(ID::init); -					} -				} -		} -  		if (log_continue) {  			log_header(design, "Continuing TECHMAP pass.\n");  			log_continue = false; @@ -999,7 +983,7 @@ struct TechmapPass : public Pass {  		log("    techmap [-map filename] [selection]\n");  		log("\n");  		log("This pass implements a very simple technology mapper that replaces cells in\n"); -		log("the design with implementations given in form of a Verilog or ilang source\n"); +		log("the design with implementations given in form of a Verilog or RTLIL source\n");  		log("file.\n");  		log("\n");  		log("    -map filename\n"); @@ -1042,7 +1026,9 @@ struct TechmapPass : public Pass {  		log("\n");  		log("When a module in the map file has the 'techmap_celltype' attribute set, it will\n");  		log("match cells with a type that match the text value of this attribute. Otherwise\n"); -		log("the module name will be used to match the cell.\n"); +		log("the module name will be used to match the cell.  Multiple space-separated cell\n"); +		log("types can be listed, and wildcards using [] will be expanded (ie. \"$_DFF_[PN]_\"\n"); +		log("is the same as \"$_DFF_P_ $_DFF_N_\").\n");  		log("\n");  		log("When a module in the map file has the 'techmap_simplemap' attribute set, techmap\n");  		log("will use 'simplemap' (see 'help simplemap') to map cells matching the module.\n"); @@ -1111,6 +1097,10 @@ struct TechmapPass : public Pass {  		log("        When a parameter with this name exists, it will be set to the type name\n");  		log("        of the cell that matches the module.\n");  		log("\n"); +		log("    _TECHMAP_CELLNAME_\n"); +		log("        When a parameter with this name exists, it will be set to the name\n"); +		log("        of the cell that matches the module.\n"); +		log("\n");  		log("    _TECHMAP_CONSTMSK_<port-name>_\n");  		log("    _TECHMAP_CONSTVAL_<port-name>_\n");  		log("        When this pair of parameters is available in a module for a port, then\n"); @@ -1220,7 +1210,7 @@ struct TechmapPass : public Pass {  						if (!map->module(mod->name))  							map->add(mod->clone());  				} else { -					Frontend::frontend_call(map, nullptr, fn, (fn.size() > 3 && fn.compare(fn.size()-3, std::string::npos, ".il") == 0 ? "ilang" : verilog_frontend)); +					Frontend::frontend_call(map, nullptr, fn, (fn.size() > 3 && fn.compare(fn.size()-3, std::string::npos, ".il") == 0 ? "rtlil" : verilog_frontend));  				}  		} @@ -1230,8 +1220,27 @@ struct TechmapPass : public Pass {  		for (auto module : map->modules()) {  			if (module->attributes.count(ID::techmap_celltype) && !module->attributes.at(ID::techmap_celltype).bits.empty()) {  				char *p = strdup(module->attributes.at(ID::techmap_celltype).decode_string().c_str()); -				for (char *q = strtok(p, " \t\r\n"); q; q = strtok(nullptr, " \t\r\n")) -					celltypeMap[RTLIL::escape_id(q)].insert(module->name); +				for (char *q = strtok(p, " \t\r\n"); q; q = strtok(nullptr, " \t\r\n")) { +					std::vector<std::string> queue; +					queue.push_back(q); +					while (!queue.empty()) { +						std::string name = queue.back(); +						queue.pop_back(); +						auto pos = name.find('['); +						if (pos == std::string::npos) { +							// No further expansion. +							celltypeMap[RTLIL::escape_id(name)].insert(module->name); +						} else { +							// Expand [] in this name. +							auto epos = name.find(']', pos); +							if (epos == std::string::npos) +								log_error("Malformed techmap_celltype pattern %s\n", q); +							for (size_t i = pos + 1; i < epos; i++) { +								queue.push_back(name.substr(0, pos) + name[i] + name.substr(epos + 1, std::string::npos)); +							} +						} +					} +				}  				free(p);  			} else {  				IdString module_name = module->name.begins_with("\\$") ? @@ -1239,8 +1248,15 @@ struct TechmapPass : public Pass {  				celltypeMap[module_name].insert(module->name);  			}  		} -		for (auto &i : celltypeMap) +		log_debug("Cell type mappings to use:\n"); +		for (auto &i : celltypeMap) {  			i.second.sort(RTLIL::sort_by_id_str()); +			std::string maps = ""; +			for (auto &map : i.second) +				maps += stringf(" %s", log_id(map)); +			log_debug("    %s:%s\n", log_id(i.first), maps.c_str()); +		} +		log_debug("\n");  		for (auto module : design->modules())  			worker.module_queue.insert(module); diff --git a/passes/techmap/zinit.cc b/passes/techmap/zinit.cc index cc0b26bcc..e3b4ae573 100644 --- a/passes/techmap/zinit.cc +++ b/passes/techmap/zinit.cc @@ -19,6 +19,7 @@  #include "kernel/yosys.h"  #include "kernel/sigtools.h" +#include "kernel/ffinit.h"  USING_YOSYS_NAMESPACE  PRIVATE_NAMESPACE_BEGIN @@ -57,35 +58,7 @@ struct ZinitPass : public Pass {  		for (auto module : design->selected_modules())  		{  			SigMap sigmap(module); -			dict<SigBit, std::pair<State,SigBit>> initbits; - -			for (auto wire : module->selected_wires()) -			{ -				if (wire->attributes.count(ID::init) == 0) -					continue; - -				SigSpec wirebits = sigmap(wire); -				Const initval = wire->attributes.at(ID::init); - -				for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++) -				{ -					SigBit bit = wirebits[i]; -					State val = initval[i]; - -					if (val != State::S0 && val != State::S1 && bit.wire != nullptr) -						continue; - -					if (initbits.count(bit)) { -						if (initbits.at(bit).first != val) -							log_error("Conflicting init values for signal %s (%s = %s != %s).\n", -									log_signal(bit), log_signal(SigBit(wire, i)), -									log_signal(val), log_signal(initbits.at(bit).first)); -						continue; -					} - -					initbits[bit] = std::make_pair(val,SigBit(wire,i)); -				} -			} +			FfInitVals initvals(&sigmap, module);  			pool<IdString> dff_types = {  								// FIXME: It would appear that supporting @@ -127,33 +100,28 @@ struct ZinitPass : public Pass {  				if (GetSize(sig_d) < 1 || GetSize(sig_q) < 1)  					continue; -				Const initval; +				Const initval = initvals(sig_q); +				Const newval = initval; +				initvals.remove_init(sig_q); -				for (int i = 0; i < GetSize(sig_q); i++) { -					if (initbits.count(sig_q[i])) { -						const auto &d = initbits.at(sig_q[i]); -						initval.bits.push_back(d.first); -						const auto &b = d.second; -						b.wire->attributes.at(ID::init)[b.offset] = State::Sx; -					} else -						initval.bits.push_back(all_mode ? State::S0 : State::Sx); -				} - -				Wire *initwire = module->addWire(NEW_ID, GetSize(initval)); -				initwire->attributes[ID::init] = initval; +				Wire *initwire = module->addWire(NEW_ID, GetSize(sig_q));  				for (int i = 0; i < GetSize(initwire); i++)  					if (initval[i] == State::S1)  					{  						sig_d[i] = module->NotGate(NEW_ID, sig_d[i]);  						module->addNotGate(NEW_ID, SigSpec(initwire, i), sig_q[i]); -						initwire->attributes[ID::init][i] = State::S0; +						newval[i] = State::S0;  					}  					else  					{  						module->connect(sig_q[i], SigSpec(initwire, i)); +						if (all_mode) +							newval[i] = State::S0;  					} +				initvals.set_init(initwire, newval); +  				log("FF init value for cell %s (%s): %s = %s\n", log_id(cell), log_id(cell->type),  						log_signal(sig_q), log_signal(initval));  | 
