File: ql_bram_merge.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 (216 lines) | stat: -rw-r--r-- 6,830 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2023  N. Engelhardt <nak@yosyshq.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/log.h"
#include "kernel/register.h"
#include "kernel/rtlil.h"
#include "kernel/sigtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

// ============================================================================



struct QlBramMergeWorker {

	// can be used to record parameter values that have to match on both sides
	typedef dict<RTLIL::IdString, RTLIL::Const> MergeableGroupKeyType;

	RTLIL::Module *module;
	dict<MergeableGroupKeyType, pool<RTLIL::Cell*>> mergeable_groups;

	QlBramMergeWorker(RTLIL::Module* module) : module(module)
	{
		const RTLIL::IdString split_cell_type = ID($__QLF_TDP36K);

		for (RTLIL::Cell* cell : module->selected_cells())
		{
			if(cell->type != split_cell_type) continue;
			if(!cell->hasParam(ID(OPTION_SPLIT))) continue;
			if(cell->getParam(ID(OPTION_SPLIT)) != RTLIL::Const(1, 32)) continue;
			mergeable_groups[get_key(cell)].insert(cell);
		}
	}

	static MergeableGroupKeyType get_key(RTLIL::Cell* cell)
	{
		MergeableGroupKeyType key;
		// For now, there are no restrictions on which cells can be merged
		(void) cell;
		return key;
	}

	const dict<RTLIL::IdString, RTLIL::IdString>& param_map(bool second)
	{
		static const dict<RTLIL::IdString, RTLIL::IdString> bram1_map = {
			{ ID(INIT),                     ID(INIT1) },
			{ ID(PORT_A_WIDTH),             ID(PORT_A1_WIDTH) },
			{ ID(PORT_B_WIDTH),             ID(PORT_B1_WIDTH) },
			{ ID(PORT_A_WR_BE_WIDTH),       ID(PORT_A1_WR_BE_WIDTH) },
			{ ID(PORT_B_WR_BE_WIDTH),       ID(PORT_B1_WR_BE_WIDTH) }
		};
		static const dict<RTLIL::IdString, RTLIL::IdString> bram2_map = {
			{ ID(INIT),                     ID(INIT2) },
			{ ID(PORT_A_WIDTH),             ID(PORT_A2_WIDTH) },
			{ ID(PORT_B_WIDTH),             ID(PORT_B2_WIDTH) },
			{ ID(PORT_A_WR_BE_WIDTH),       ID(PORT_A2_WR_BE_WIDTH) },
			{ ID(PORT_B_WR_BE_WIDTH),       ID(PORT_B2_WR_BE_WIDTH) }
		};

		if(second)
			return bram2_map;
		else
			return bram1_map;
	}

	const dict<RTLIL::IdString, RTLIL::IdString>& port_map(bool second)
	{
		static const dict<RTLIL::IdString, RTLIL::IdString> bram1_map = {
			{ ID(PORT_A_CLK),       ID(PORT_A1_CLK) },
			{ ID(PORT_B_CLK),       ID(PORT_B1_CLK) },
			{ ID(PORT_A_CLK_EN),    ID(PORT_A1_CLK_EN) },
			{ ID(PORT_B_CLK_EN),    ID(PORT_B1_CLK_EN) },
			{ ID(PORT_A_ADDR),      ID(PORT_A1_ADDR) },
			{ ID(PORT_B_ADDR),      ID(PORT_B1_ADDR) },
			{ ID(PORT_A_WR_DATA),   ID(PORT_A1_WR_DATA) },
			{ ID(PORT_B_WR_DATA),   ID(PORT_B1_WR_DATA) },
			{ ID(PORT_A_WR_EN),     ID(PORT_A1_WR_EN) },
			{ ID(PORT_B_WR_EN),     ID(PORT_B1_WR_EN) },
			{ ID(PORT_A_WR_BE),     ID(PORT_A1_WR_BE) },
			{ ID(PORT_B_WR_BE),     ID(PORT_B1_WR_BE) },
			{ ID(PORT_A_RD_DATA),   ID(PORT_A1_RD_DATA) },
			{ ID(PORT_B_RD_DATA),   ID(PORT_B1_RD_DATA) }
		};
		static const dict<RTLIL::IdString, RTLIL::IdString> bram2_map = {
			{ ID(PORT_A_CLK),       ID(PORT_A2_CLK) },
			{ ID(PORT_B_CLK),       ID(PORT_B2_CLK) },
			{ ID(PORT_A_CLK_EN),    ID(PORT_A2_CLK_EN) },
			{ ID(PORT_B_CLK_EN),    ID(PORT_B2_CLK_EN) },
			{ ID(PORT_A_ADDR),      ID(PORT_A2_ADDR) },
			{ ID(PORT_B_ADDR),      ID(PORT_B2_ADDR) },
			{ ID(PORT_A_WR_DATA),   ID(PORT_A2_WR_DATA) },
			{ ID(PORT_B_WR_DATA),   ID(PORT_B2_WR_DATA) },
			{ ID(PORT_A_WR_EN),     ID(PORT_A2_WR_EN) },
			{ ID(PORT_B_WR_EN),     ID(PORT_B2_WR_EN) },
			{ ID(PORT_A_WR_BE),     ID(PORT_A2_WR_BE) },
			{ ID(PORT_B_WR_BE),     ID(PORT_B2_WR_BE) },
			{ ID(PORT_A_RD_DATA),   ID(PORT_A2_RD_DATA) },
			{ ID(PORT_B_RD_DATA),   ID(PORT_B2_RD_DATA) }
		};

		if(second)
			return bram2_map;
		else
			return bram1_map;
	}

	void merge_brams(RTLIL::Cell* bram1, RTLIL::Cell* bram2)
	{
		const RTLIL::IdString merged_cell_type = ID($__QLF_TDP36K_MERGED);

		// Create the new cell
		RTLIL::Cell* merged = module->addCell(NEW_ID, merged_cell_type);
		log_debug("Merging split BRAM cells %s and %s -> %s\n", log_id(bram1->name), log_id(bram2->name), log_id(merged->name));

		for (auto &it : param_map(false))
		{
			if(bram1->hasParam(it.first))
				merged->setParam(it.second, bram1->getParam(it.first));
		}
		for (auto &it : param_map(true))
		{
			if(bram2->hasParam(it.first))
				merged->setParam(it.second, bram2->getParam(it.first));
		}

		for (auto &it : port_map(false))
		{
			if (bram1->hasPort(it.first))
				merged->setPort(it.second, bram1->getPort(it.first));
			else
				log_error("Can't find port %s on cell %s!\n", log_id(it.first), log_id(bram1->name));
		}
		for (auto &it : port_map(true))
		{
			if (bram2->hasPort(it.first))
				merged->setPort(it.second, bram2->getPort(it.first));
			else
				log_error("Can't find port %s on cell %s!\n", log_id(it.first), log_id(bram2->name));
		}
		merged->attributes = bram1->attributes;
		for (auto attr: bram2->attributes)
			if (!merged->has_attribute(attr.first))
				merged->attributes.insert(attr);

		// Remove the old cells
		module->remove(bram1);
		module->remove(bram2);

	}

	void merge_bram_groups()
	{
		for (auto &it : mergeable_groups)
		{
			while (it.second.size() > 1)
			{
				merge_brams(it.second.pop(), it.second.pop());
			}
		}
	}

};

struct QlBramMergePass : public Pass {
	
	QlBramMergePass() : Pass("ql_bram_merge", "Infers QuickLogic k6n10f BRAM pairs that can operate independently") {}

	void help() override
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    ql_bram_merge [selection]\n");
		log("\n");
		log("    This pass identifies k6n10f 18K BRAM cells and packs pairs of them together\n");
		log("    into a TDP36K cell operating in split mode\n");
		log("\n");
	}



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

		size_t argidx = 1;
		extra_args(args, argidx, design);

		for (RTLIL::Module* module : design->selected_modules())
		{
			QlBramMergeWorker worker(module);
			worker.merge_bram_groups();
		}
	}


} QlBramMergePass;

PRIVATE_NAMESPACE_END