aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/coolrunner2/coolrunner2_fixup.cc
blob: 50710e2bdfeb6dff13fc374ead77ec8aa5013312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
/dts-v1/;

#include "qca955x_zyxel_nbg6x16.dtsi"

/ {
	compatible = "zyxel,nbg6716", "qca,qca9558";
	model = "ZyXEL NBG6716";

	aliases {
		led-boot = &led_power;
		led-failsafe = &led_power;
		led-running = &led_power;
		led-upgrade = &led_power;
	};

	leds {
		compatible = "gpio-leds";

		led_power: power {
			label = "nbg6716:white:power";
			gpios = <&gpio 15 GPIO_ACTIVE_LOW>;
		};

		internet {
			label = "nbg6716:white:internet";
			gpios = <&gpio 18 GPIO_ACTIVE_LOW>;
		};

		usb1 {
			label = "nbg6716:white:usb1";
			gpios = <&gpio 4 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "usbport";
			trigger-sources = <&hub_port1>;
		};

		usb2 {
			label = "nbg6716:white:usb2";
			gpios = <&gpio 13 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "usbport";
			trigger-sources = <&hub_port0>;
		};

		wifi2g {
			label = "nbg6716:white:wifi2g";
			gpios = <&gpio 19 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "phy1tpt";
		};

		wifi5g {
			label = "nbg6716:white:wifi5g";
			gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
			linux,default-trigger = "phy0tpt";
		};

		wps {
			label =<
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2020 R. Ou <rqou@robertou.com>
 *
 *  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

RTLIL::Wire *makexorbuffer(RTLIL::Module *module, SigBit inwire, const char *cellname)
{
	RTLIL::Wire *outwire = nullptr;

	if (inwire == SigBit(true))
	{
		// Constant 1
		outwire = module->addWire(
			module->uniquify(stringf("$xc2fix$%s_BUF1_XOR_OUT", cellname)));
		auto xor_cell = module->addCell(
			module->uniquify(stringf("$xc2fix$%s_BUF1_XOR", cellname)),
			ID(MACROCELL_XOR));
		xor_cell->setParam(ID(INVERT_OUT), true);
		xor_cell->setPort(ID(OUT), outwire);
	}
	else if (inwire == SigBit(false))
	{
		// Constant 0
		outwire = module->addWire(
			module->uniquify(stringf("$xc2fix$%s_BUF0_XOR_OUT", cellname)));
		auto xor_cell = module->addCell(
			module->uniquify(stringf("$xc2fix$%s_BUF0_XOR", cellname)),
			ID(MACROCELL_XOR));
		xor_cell->setParam(ID(INVERT_OUT), false);
		xor_cell->setPort(ID(OUT), outwire);
	}
	else if (inwire == SigBit(RTLIL::State::Sx))
	{
		// x; treat as 0
		log_warning("While buffering, changing x to 0 into cell %s\n", cellname);
		outwire = module->addWire(
			module->uniquify(stringf("$xc2fix$%s_BUF0_XOR_OUT", cellname)));
		auto xor_cell = module->addCell(
			module->uniquify(stringf("$xc2fix$%s_BUF0_XOR", cellname)),
			ID(MACROCELL_XOR));
		xor_cell->setParam(ID(INVERT_OUT), false);
		xor_cell->setPort(ID(OUT), outwire);
	}
	else
	{
		auto inwire_name = inwire.wire->name.c_str();

		outwire = module->addWire(
			module->uniquify(stringf("$xc2fix$%s_BUF_XOR_OUT", inwire_name)));

		auto and_to_xor_wire = module->addWire(
			module->uniquify(stringf("$xc2fix$%s_BUF_AND_OUT", inwire_name)));

		auto and_cell = module->addCell(
			module->uniquify(stringf("$xc2fix$%s_BUF_AND", inwire_name)),
			ID(ANDTERM));
		and_cell->setParam(ID(TRUE_INP), 1);
		and_cell->setParam(ID(COMP_INP), 0);
		and_cell->setPort(ID(OUT), and_to_xor_wire);
		and_cell->setPort(ID(IN), inwire);
		and_cell->setPort(ID(IN_B), SigSpec());

		auto xor_cell = module->addCell(
			module->uniquify(stringf("$xc2fix$%s_BUF_XOR", inwire_name)),
			ID(MACROCELL_XOR));
		xor_cell->setParam(ID(INVERT_OUT), false);
		xor_cell->setPort(ID(IN_PTC), and_to_xor_wire);
		xor_cell->setPort(ID(OUT), outwire);
	}

	return outwire;
}

RTLIL::Wire *makeptermbuffer(RTLIL::Module *module, SigBit inwire)
{
	auto inwire_name = inwire.wire->name.c_str();

	auto outwire = module->addWire(
		module->uniquify(stringf("$xc2fix$%s_BUF_AND_OUT", inwire_name)));

	auto and_cell = module->addCell(
		module->uniquify(stringf("$xc2fix$%s_BUF_AND", inwire_name)),
		ID(ANDTERM));
	and_cell->setParam(ID(TRUE_INP), 1);
	and_cell->setParam(ID(COMP_INP), 0);
	and_cell->setPort(ID(OUT), outwire);
	and_cell->setPort(ID(IN), inwire);
	and_cell->setPort(ID(IN_B), SigSpec());

	return outwire;
}

struct Coolrunner2FixupPass : public Pass {
	Coolrunner2FixupPass() : Pass("coolrunner2_fixup", "insert necessary buffer cells for CoolRunner-II architecture") { }
	void help() override
	{
		log("\n");
		log("    coolrunner2_fixup [options] [selection]\n");
		log("\n");
		log("Insert necessary buffer cells for CoolRunner-II architecture.\n");
		log("\n");
	}
	void execute(std::vector<std::string> args, RTLIL::Design *design) override
	{
		log_header(design, "Executing COOLRUNNER2_FIXUP pass (insert necessary buffer cells for CoolRunner-II architecture).\n");
		extra_args(args, 1, design);

		for (auto module : design->selected_modules())
		{
			SigMap sigmap(module);

			// Find all the FF outputs
			pool<SigBit> sig_fed_by_ff;
			for (auto cell : module->selected_cells())
			{
				if (cell->type.in(ID(FDCP), ID(FDCP_N), ID(FDDCP), ID(LDCP), ID(LDCP_N),
							ID(FTCP), ID(FTCP_N), ID(FTDCP), ID(FDCPE), ID(FDCPE_N), ID(FDDCPE)))
				{
					auto output = sigmap(cell->getPort(ID::Q)[0]);
					sig_fed_by_ff.insert(output);
				}
			}

			// Find all the XOR outputs
			pool<SigBit> sig_fed_by_xor;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(MACROCELL_XOR))
				{
					auto output = sigmap(cell->getPort(ID(OUT))[0]);
					sig_fed_by_xor.insert(output);
				}
			}

			// Find all the input/inout outputs
			pool<SigBit> sig_fed_by_io;
			for (auto cell : module->selected_cells())
			{
				if (cell->type.in(ID(IBUF), ID(IOBUFE)))
				{
					if (cell->hasPort(ID::O)) {
						auto output = sigmap(cell->getPort(ID::O)[0]);
						sig_fed_by_io.insert(output);
					}
				}
			}

			// Find all the pterm outputs
			pool<SigBit> sig_fed_by_pterm;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(ANDTERM))
				{
					auto output = sigmap(cell->getPort(ID(OUT))[0]);
					sig_fed_by_pterm.insert(output);
				}
			}

			// Find all the bufg outputs
			pool<SigBit> sig_fed_by_bufg;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(BUFG))
				{
					auto output = sigmap(cell->getPort(ID::O)[0]);
					sig_fed_by_bufg.insert(output);
				}
			}

			// Find all the bufgsr outputs
			pool<SigBit> sig_fed_by_bufgsr;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(BUFGSR))
				{
					auto output = sigmap(cell->getPort(ID::O)[0]);
					sig_fed_by_bufgsr.insert(output);
				}
			}

			// Find all the bufgts outputs
			pool<SigBit> sig_fed_by_bufgts;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(BUFGTS))
				{
					auto output = sigmap(cell->getPort(ID::O)[0]);
					sig_fed_by_bufgts.insert(output);
				}
			}

			// This is used to fix the input -> FF -> output scenario
			pool<SigBit> sig_fed_by_ibuf;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(IBUF))
				{
					auto output = sigmap(cell->getPort(ID::O)[0]);
					sig_fed_by_ibuf.insert(output);
				}
			}

			// Find all of the sinks for each output from an IBUF
			dict<SigBit, std::pair<int, RTLIL::Cell *>> ibuf_fanouts;
			for (auto cell : module->selected_cells())
			{
				for (auto &conn : cell->connections())
				{
					if (cell->input(conn.first))
					{
						for (auto wire_in : sigmap(conn.second))
						{
							if (sig_fed_by_ibuf[wire_in])
							{
								auto existing_count = ibuf_fanouts[wire_in].first;
								ibuf_fanouts[wire_in] =
									std::pair<int, RTLIL::Cell *>(existing_count + 1, cell);
							}
						}
					}
				}
			}

			dict<SigBit, RTLIL::Cell *> ibuf_out_to_packed_reg_cell;
			pool<SigBit> packed_reg_out;
			for (auto x : ibuf_fanouts)
			{
				auto ibuf_out_wire = x.first;
				auto fanout_count = x.second.first;
				auto maybe_ff_cell = x.second.second;

				// The register can be packed with the IBUF only if it's
				// actually a register and it's the only fanout. Otherwise,
				// the pad-to-zia path has to be used up and the register
				// can't be packed with the ibuf.
				if (fanout_count == 1 && maybe_ff_cell->type.in(
					ID(FDCP), ID(FDCP_N), ID(FDDCP), ID(LDCP), ID(LDCP_N),
					ID(FTCP), ID(FTCP_N), ID(FTDCP), ID(FDCPE), ID(FDCPE_N), ID(FDDCPE)))
				{
					SigBit input;
					if (maybe_ff_cell->type.in(ID(FTCP), ID(FTCP_N), ID(FTDCP)))
						input = sigmap(maybe_ff_cell->getPort(ID::T)[0]);
					else
						input = sigmap(maybe_ff_cell->getPort(ID::D)[0]);
					SigBit output = sigmap(maybe_ff_cell->getPort(ID::Q)[0]);

					if (input == ibuf_out_wire)
					{
						log("Found IBUF %s that can be packed with FF %s (type %s)\n",
							ibuf_out_wire.wire->name.c_str(),
							maybe_ff_cell->name.c_str(),
							maybe_ff_cell->type.c_str());

						ibuf_out_to_packed_reg_cell[ibuf_out_wire] = maybe_ff_cell;
						packed_reg_out.insert(output);
					}
				}
			}

			for (auto cell : module->selected_cells())
			{
				if (cell->type.in(ID(FDCP), ID(FDCP_N), ID(FDDCP), ID(LDCP), ID(LDCP_N),
							ID(FTCP), ID(FTCP_N), ID(FTDCP), ID(FDCPE), ID(FDCPE_N), ID(FDDCPE)))
				{
					// Buffering FF inputs. FF inputs can only come from either
					// an IO pin or from an XOR. Otherwise AND/XOR cells need
					// to be inserted.
					SigBit input;
					if (cell->type.in(ID(FTCP), ID(FTCP_N), ID(FTDCP)))
						input = sigmap(cell->getPort(ID::T)[0]);
					else
						input = sigmap(cell->getPort(ID::D)[0]);

					// If the input wasn't an XOR nor an IO, then a buffer
					// definitely needs to be added.
					// Otherwise, if it is an IO, only leave unbuffered
					// if we're being packed with the IO.
					if ((!sig_fed_by_xor[input] && !sig_fed_by_io[input]) ||
						(sig_fed_by_io[input] && ibuf_out_to_packed_reg_cell[input] != cell))
					{
						log("Buffering input to \"%s\"\n", cell->name.c_str());

						auto xor_to_ff_wire = makexorbuffer(module, input, cell->name.c_str());

						if (cell->type.in(ID(FTCP), ID(FTCP_N), ID(FTDCP)))
							cell->setPort(ID::T, xor_to_ff_wire);
						else
							cell->setPort(ID::D, xor_to_ff_wire);
					}

					// Buffering FF clocks. FF clocks can only come from either
					// a pterm or a bufg. In some cases this will be handled
					// in coolrunner2_sop (e.g. if clock is generated from
					// AND-ing two signals) but not in all cases.
					SigBit clock;
					if (cell->type.in(ID(LDCP), ID(LDCP_N)))
						clock = sigmap(cell->getPort(ID::G)[0]);
					else
						clock = sigmap(cell->getPort(ID::C)[0]);

					if (!sig_fed_by_pterm[clock] && !sig_fed_by_bufg[clock])
					{
						log("Buffering clock to \"%s\"\n", cell->name.c_str());

						auto pterm_to_ff_wire = makeptermbuffer(module, clock);

						if (cell->type.in(ID(LDCP), ID(LDCP_N)))
							cell->setPort(ID::G, pterm_to_ff_wire);
						else
							cell->setPort(ID::C, pterm_to_ff_wire);
					}

					// Buffering FF set/reset. This can only come from either
					// a pterm or a bufgsr.
					SigBit set;
					set = sigmap(cell->getPort(ID(PRE))[0]);
					if (set != SigBit(false))
					{
						if (!sig_fed_by_pterm[set] && !sig_fed_by_bufgsr[set])
						{
							log("Buffering set to \"%s\"\n", cell->name.c_str());

							auto pterm_to_ff_wire = makeptermbuffer(module, set);

							cell->setPort(ID(PRE), pterm_to_ff_wire);
						}
					}

					SigBit reset;
					reset = sigmap(cell->getPort(ID::CLR)[0]);
					if (reset != SigBit(false))
					{
						if (!sig_fed_by_pterm[reset] && !sig_fed_by_bufgsr[reset])
						{
							log("Buffering reset to \"%s\"\n", cell->name.c_str());

							auto pterm_to_ff_wire = makeptermbuffer(module, reset);

							cell->setPort(ID::CLR, pterm_to_ff_wire);
						}
					}

					// Buffering FF clock enable
					// FIXME: This doesn't fully fix PTC conflicts
					// FIXME: Need to ensure constant enables are optimized out
					if (cell->type.in(ID(FDCPE), ID(FDCPE_N), ID(FDDCPE)))
					{
						SigBit ce;
						ce = sigmap(cell->getPort(ID(CE))[0]);
						if (!sig_fed_by_pterm[ce])
						{
							log("Buffering clock enable to \"%s\"\n", cell->name.c_str());

							auto pterm_to_ff_wire = makeptermbuffer(module, ce);

							cell->setPort(ID(CE), pterm_to_ff_wire);
						}
					}
				}
			}

			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(IOBUFE))
				{
					// Buffer IOBUFE inputs. This can only be fed from an XOR or FF.
					SigBit input = sigmap(cell->getPort(ID::I)[0]);

					if ((!sig_fed_by_xor[input] && !sig_fed_by_ff[input]) ||
						packed_reg_out[input])
					{
						log("Buffering input to \"%s\"\n", cell->name.c_str());

						auto xor_to_io_wire = makexorbuffer(module, input, cell->name.c_str());

						cell->setPort(ID::I, xor_to_io_wire);
					}

					// Buffer IOBUFE enables. This can only be fed from a pterm
					// or a bufgts.
					if (cell->hasPort(ID::E))
					{
						SigBit oe;
						oe = sigmap(cell->getPort(ID::E)[0]);
						if (!sig_fed_by_pterm[oe] && !sig_fed_by_bufgts[oe])
						{
							log("Buffering output enable to \"%s\"\n", cell->name.c_str());

							auto pterm_to_oe_wire = makeptermbuffer(module, oe);

							cell->setPort(ID::E, pterm_to_oe_wire);
						}
					}
				}
			}

			// Now we have to fix up some cases where shared logic can
			// cause XORs to have multiple fanouts to something other than
			// pterms (which is not ok)

			// Find all the XOR outputs
			dict<SigBit, RTLIL::Cell *> xor_out_to_xor_cell;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(MACROCELL_XOR))
				{
					auto output = sigmap(cell->getPort(ID(OUT))[0]);
					xor_out_to_xor_cell[output] = cell;
				}
			}

			// Find all of the sinks for each output from an XOR
			pool<SigBit> xor_fanout_once;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(ANDTERM))
					continue;

				for (auto &conn : cell->connections())
				{
					if (cell->input(conn.first))
					{
						for (auto wire_in : sigmap(conn.second))
						{
							auto xor_cell = xor_out_to_xor_cell[wire_in];
							if (xor_cell)
							{
								if (xor_fanout_once[wire_in])
								{
									log("Additional fanout found for %s into %s (type %s), duplicating\n",
										xor_cell->name.c_str(),
										cell->name.c_str(),
										cell->type.c_str());

									auto new_xor_cell = module->addCell(
										module->uniquify(xor_cell->name), xor_cell);
									auto new_wire = module->addWire(
										module->uniquify(wire_in.wire->name));
									new_xor_cell->setPort(ID(OUT), new_wire);
									cell->setPort(conn.first, new_wire);
								}
								xor_fanout_once.insert(wire_in);
							}
						}
					}
				}
			}

			// Do the same fanout fixing for OR terms. By doing this
			// after doing XORs, both pieces will be duplicated when necessary.

			// Find all the OR outputs
			dict<SigBit, RTLIL::Cell *> or_out_to_or_cell;
			for (auto cell : module->selected_cells())
			{
				if (cell->type == ID(ORTERM))
				{
					auto output = sigmap(cell->getPort(ID(OUT))[0]);
					or_out_to_or_cell[output] = cell;
				}
			}

			// Find all of the sinks for each output from an OR
			pool<SigBit> or_fanout_once;
			for (auto cell : module->selected_cells())
			{
				for (auto &conn : cell->connections())
				{
					if (cell->input(conn.first))
					{
						for (auto wire_in : sigmap(conn.second))
						{
							auto or_cell = or_out_to_or_cell[wire_in];
							if (or_cell)
							{
								if (or_fanout_once[wire_in])
								{
									log("Additional fanout found for %s into %s (type %s), duplicating\n",
										or_cell->name.c_str(),
										cell->name.c_str(),
										cell->type.c_str());

									auto new_or_cell = module->addCell(
										module->uniquify(or_cell->name), or_cell);
									auto new_wire = module->addWire(
										module->uniquify(wire_in.wire->name));
									new_or_cell->setPort(ID(OUT), new_wire);
									cell->setPort(conn.first, new_wire);
								}
								or_fanout_once.insert(wire_in);
							}
						}
					}
				}
			}
		}
	}
} Coolrunner2FixupPass;

PRIVATE_NAMESPACE_END