File: ql_ioff.cc

package info (click to toggle)
yosys 0.52-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 69,796 kB
  • sloc: ansic: 696,955; cpp: 239,736; python: 14,617; yacc: 3,529; sh: 2,175; makefile: 1,945; lex: 697; perl: 445; javascript: 323; tcl: 162; vhdl: 115
file content (125 lines) | stat: -rw-r--r-- 4,294 bytes parent folder | download
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
#include "kernel/log.h"
#include "kernel/modtools.h"
#include "kernel/register.h"
#include "kernel/rtlil.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct QlIoffPass : public Pass {
	QlIoffPass() : Pass("ql_ioff", "Infer I/O FFs for qlf_k6n10f architecture") {}

	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    ql_ioff [selection]\n");
		log("\n");
		log("This pass promotes qlf_k6n10f registers directly connected to a top-level I/O\n");
		log("port to I/O FFs.\n");
		log("\n");
	}

	void execute(std::vector<std::string>, RTLIL::Design *design) override
	{
		log_header(design, "Executing QL_IOFF pass.\n");

		ModWalker modwalker(design);
		Module *module = design->top_module();
		if (!module)
			return;
		modwalker.setup(module);
		pool<RTLIL::Cell *> input_ffs;
		dict<RTLIL::Wire *, std::vector<Cell*>> output_ffs;
		dict<SigBit, pool<SigBit>> output_bit_aliases;

		for (Wire* wire : module->wires())
			if (wire->port_output) {
				output_ffs[wire].resize(wire->width, nullptr);
				for (SigBit bit : SigSpec(wire))
					output_bit_aliases[modwalker.sigmap(bit)].insert(bit);
			}

		for (auto cell : module->selected_cells()) {
			if (cell->type.in(ID(dffsre), ID(sdffsre))) {
				log_debug("Checking cell %s.\n", cell->name.c_str());
				bool e_const = cell->getPort(ID::E).is_fully_ones();
				bool r_const = cell->getPort(ID::R).is_fully_ones();
				bool s_const = cell->getPort(ID::S).is_fully_ones();

				if (!(e_const && r_const && s_const)) {
					log_debug("not promoting: E, R, or S is used\n");
					continue;
				}

				SigSpec d = cell->getPort(ID::D);
				log_assert(GetSize(d) == 1);
				if (modwalker.has_inputs(d)) {
					log_debug("Cell %s is potentially eligible for promotion to input IOFF.\n", cell->name.c_str());
					// check that d_sig has no other consumers
					pool<ModWalker::PortBit> portbits;
					modwalker.get_consumers(portbits, d);
					if (GetSize(portbits) > 1) {
						log_debug("not promoting: D has other consumers\n");
						continue;
					}
					input_ffs.insert(cell);
					continue; // prefer input FFs over output FFs
				}

				SigSpec q = cell->getPort(ID::Q);
				log_assert(GetSize(q) == 1);
				if (modwalker.has_outputs(q) && !modwalker.has_consumers(q)) {
					log_debug("Cell %s is potentially eligible for promotion to output IOFF.\n", cell->name.c_str());
					for (SigBit bit : output_bit_aliases[modwalker.sigmap(q)]) {
						log_assert(bit.is_wire());
						output_ffs[bit.wire][bit.offset] = cell;
					}

				}
			}
		}

		for (auto cell : input_ffs) {
			log("Promoting register %s to input IOFF.\n", log_signal(cell->getPort(ID::Q)));
			cell->type = ID(dff);
			cell->unsetPort(ID::E);
			cell->unsetPort(ID::R);
			cell->unsetPort(ID::S);
		}
		for (auto & [old_port_output, ioff_cells] : output_ffs) {
			if (std::any_of(ioff_cells.begin(), ioff_cells.end(), [](Cell * c) { return c != nullptr; }))
			{
				// create replacement output wire
				RTLIL::Wire* new_port_output = module->addWire(NEW_ID, old_port_output->width);
				new_port_output->start_offset = old_port_output->start_offset;
				module->swap_names(old_port_output, new_port_output);
				std::swap(old_port_output->port_id, new_port_output->port_id);
				std::swap(old_port_output->port_input, new_port_output->port_input);
				std::swap(old_port_output->port_output, new_port_output->port_output);
				std::swap(old_port_output->upto, new_port_output->upto);
				std::swap(old_port_output->is_signed, new_port_output->is_signed);
				std::swap(old_port_output->attributes, new_port_output->attributes);

				// create new output FFs
				SigSpec sig_o(old_port_output);
				SigSpec sig_n(new_port_output);
				for (int i = 0; i < new_port_output->width; i++) {
					if (ioff_cells[i]) {
						log("Promoting %s to output IOFF.\n", log_signal(sig_n[i]));

						RTLIL::Cell *new_cell = module->addCell(NEW_ID, ID(dff));
						new_cell->setPort(ID::C, ioff_cells[i]->getPort(ID::C));
						new_cell->setPort(ID::D, ioff_cells[i]->getPort(ID::D));
						new_cell->setPort(ID::Q, sig_n[i]);
						new_cell->set_bool_attribute(ID::keep);
					} else {
						module->connect(sig_n[i], sig_o[i]);
					}
				}
			}
		}
	}
} QlIoffPass;

PRIVATE_NAMESPACE_END