Debian Codegraph @

himbaechel/uarch/example/example.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2023 gatecat <[email protected]>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "himbaechel_api.h"
21 #include "log.h"
22 #include "nextpnr.h"
23 #include "util.h"
24
25 #include "himbaechel_helpers.h"
26
27 #define GEN_INIT_CONSTIDS
28 #define HIMBAECHEL_CONSTIDS "uarch/example/constids.inc"
29 #define HIMBAECHEL_GFXIDS "uarch/example/gfxids.inc"
30 #define HIMBAECHEL_UARCH example
31 #include "himbaechel_constids.h"
32 #include "himbaechel_gfxids.h"
33
34 NEXTPNR_NAMESPACE_BEGIN
35
36 namespace {
37 struct ExampleImpl : HimbaechelAPI
38 {
39
40 static constexpr int K = 4;
41
42 ~ExampleImpl() {};
43
44 po::options_description getUArchOptions()
45 {
46 po::options_description specific("Example specific options");
47 return specific;
48 }
49
50 void init_database(Arch *arch) override
51 {
52 init_uarch_constids(arch);
53 arch->load_chipdb("example/chipdb-example.bin");
54 arch->set_speed_grade("DEFAULT");
55 }
56
57 void init(Context *ctx) override
58 {
59 h.init(ctx);
60 HimbaechelAPI::init(ctx);
61 }
62
63 void prePlace() override { assign_cell_info(); }
64
65 void pack() override
66 {
67 // Trim nextpnr IOBs - assume IO buffer insertion has been done in synthesis
68 const pool<CellTypePort> top_ports{
69 CellTypePort(id_INBUF, id_PAD),
70 CellTypePort(id_OUTBUF, id_PAD),
71 };
72 h.remove_nextpnr_iobs(top_ports);
73 // Replace constants with LUTs
74 const dict<IdString, Property> vcc_params = {{id_INIT, Property(0xFFFF, 16)}};
75 const dict<IdString, Property> gnd_params = {{id_INIT, Property(0x0000, 16)}};
76 h.replace_constants(CellTypePort(id_VCC_DRV, id_VCC), CellTypePort(id_GND_DRV, id_GND), {}, {}, id_VCC, id_GND);
77 // Constrain directly connected LUTs and FFs together to use dedicated resources
78 int lutffs = h.constrain_cell_pairs(pool<CellTypePort>{{id_LUT4, id_F}}, pool<CellTypePort>{{id_DFF, id_D}}, 1);
79 log_info("Constrained %d LUTFF pairs.\n", lutffs);
80 }
81
82 bool isBelLocationValid(BelId bel, bool explain_invalid) const override
83 {
84 Loc l = ctx->getBelLocation(bel);
85 if (ctx->getBelType(bel).in(id_LUT4, id_DFF)) {
86 return slice_valid(l.x, l.y, l.z / 2);
87 } else {
88 return true;
89 }
90 }
91
92 // Bel bucket functions
93 IdString getBelBucketForCellType(IdString cell_type) const override
94 {
95 if (cell_type.in(id_INBUF, id_OUTBUF))
96 return id_IOB;
97 return cell_type;
98 }
99 bool isValidBelForCellType(IdString cell_type, BelId bel) const override
100 {
101 IdString bel_type = ctx->getBelType(bel);
102 if (bel_type == id_IOB)
103 return cell_type.in(id_INBUF, id_OUTBUF);
104 else
105 return (bel_type == cell_type);
106 }
107
108 private:
109 HimbaechelHelpers h;
110
111 // Validity checking
112 struct ExampleCellInfo
113 {
114 const NetInfo *lut_f = nullptr, *ff_d = nullptr;
115 bool lut_i3_used = false;
116 };
117 std::vector<ExampleCellInfo> fast_cell_info;
118 void assign_cell_info()
119 {
120 fast_cell_info.resize(ctx->cells.size());
121 for (auto &cell : ctx->cells) {
122 CellInfo *ci = cell.second.get();
123 auto &fc = fast_cell_info.at(ci->flat_index);
124 if (ci->type == id_LUT4) {
125 fc.lut_f = ci->getPort(id_F);
126 fc.lut_i3_used = (ci->getPort(ctx->idf("I[%d]", K - 1)) != nullptr);
127 } else if (ci->type == id_DFF) {
128 fc.ff_d = ci->getPort(id_D);
129 }
130 }
131 }
132 bool slice_valid(int x, int y, int z) const
133 {
134 const CellInfo *lut = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(x, y, z * 2)));
135 const CellInfo *ff = ctx->getBoundBelCell(ctx->getBelByLocation(Loc(x, y, z * 2 + 1)));
136 if (!lut || !ff)
137 return true; // always valid if only LUT or FF used
138 const auto &lut_data = fast_cell_info.at(lut->flat_index);
139 const auto &ff_data = fast_cell_info.at(ff->flat_index);
140 // In our example arch; the FF D can either be driven from LUT F or LUT I3
141 // so either; FF D must equal LUT F or LUT I3 must be unused
142 if (ff_data.ff_d == lut_data.lut_f)
143 return true;
144 if (lut_data.lut_i3_used)
145 return false;
146 return true;
147 }
148
149 void drawGroup(std::vector<GraphicElement> &g, GroupId group, Loc loc) override
150 {
151 IdString group_type = ctx->getGroupType(group);
152 if (group_type == id_SWITCHBOX) {
153 GraphicElement el;
154 el.type = GraphicElement::TYPE_BOX;
155 el.style = GraphicElement::STYLE_FRAME;
156
157 el.x1 = loc.x + 0.1;
158 el.x2 = el.x1 + 0.4;
159 el.y1 = loc.y + 0.1;
160 el.y2 = el.y1 + 0.8;
161 g.push_back(el);
162 }
163 }
164 void drawBel(std::vector<GraphicElement> &g, GraphicElement::style_t style, IdString bel_type, Loc loc) override
165 {
166 GraphicElement el;
167 el.type = GraphicElement::TYPE_BOX;
168 el.style = style;
169 switch (bel_type.index) {
170 case id_LUT4.index:
171 el.x1 = loc.x + 0.55;
172 el.x2 = el.x1 + 0.15;
173 el.y1 = loc.y + 0.90 - (loc.z / 2) * 0.1;
174 el.y2 = el.y1 - 0.05;
175 g.push_back(el);
176 break;
177 case id_DFF.index:
178 el.x1 = loc.x + 0.75;
179 el.x2 = el.x1 + 0.15;
180 el.y1 = loc.y + 0.90 - (loc.z / 2) * 0.1;
181 el.y2 = el.y1 - 0.05;
182 g.push_back(el);
183 break;
184 case id_GND_DRV.index:
185 case id_VCC_DRV.index:
186 case id_IOB.index:
187 el.x1 = loc.x + 0.55;
188 el.x2 = el.x1 + 0.35;
189 el.y1 = loc.y + 0.90 - loc.z * 0.40;
190 el.y2 = el.y1 - 0.25;
191 g.push_back(el);
192 break;
193 case id_BRAM_512X16.index:
194 el.x1 = loc.x + 0.55;
195 el.x2 = el.x1 + 0.35;
196 el.y1 = loc.y + 0.90;
197 el.y2 = el.y1 - 0.60;
198 g.push_back(el);
199 break;
200 }
201 }
202
203 void drawWire(std::vector<GraphicElement> &g, GraphicElement::style_t style, Loc loc, IdString wire_type,
204 int32_t tilewire, IdString tile_type) override
205 {
206 GraphicElement el;
207 el.type = GraphicElement::TYPE_LINE;
208 el.style = style;
209 int z;
210 switch (tile_type.index) {
211 case id_LOGIC.index:
212 switch (wire_type.index) {
213 case id_LUT_INPUT.index:
214 z = (tilewire - GFX_WIRE_L0_I0) / 4;
215 el.x1 = loc.x + 0.54;
216 el.x2 = el.x1 + 0.01;
217 el.y1 = loc.y + 0.90 - z * 0.1 - ((tilewire - GFX_WIRE_L0_I0) % 4 + 1) * 0.01;
218 el.y2 = el.y1;
219 g.push_back(el);
220 break;
221 case id_LUT_OUT.index:
222 z = tilewire - GFX_WIRE_L0_O;
223 el.x1 = loc.x + 0.70;
224 el.x2 = el.x1 + 0.01;
225 el.y1 = loc.y + 0.90 - z * 0.1 - 0.025;
226 el.y2 = el.y1;
227 g.push_back(el);
228 break;
229 case id_FF_DATA.index:
230 z = tilewire - GFX_WIRE_L0_D;
231 el.x1 = loc.x + 0.74;
232 el.x2 = el.x1 + 0.01;
233 el.y1 = loc.y + 0.90 - z * 0.1 - 0.025;
234 el.y2 = el.y1;
235 g.push_back(el);
236 break;
237 case id_FF_OUT.index:
238 z = tilewire - GFX_WIRE_L0_Q;
239 el.x1 = loc.x + 0.90;
240 el.x2 = el.x1 + 0.01;
241 el.y1 = loc.y + 0.90 - z * 0.1 - 0.025;
242 el.y2 = el.y1;
243 g.push_back(el);
244 break;
245 case id_TILE_CLK.index:
246 for (int i = 0; i < 8; i++) {
247 GraphicElement el;
248 el.type = GraphicElement::TYPE_LINE;
249 el.style = style;
250 el.x1 = loc.x + 0.6;
251 el.x2 = el.x1;
252 el.y1 = loc.y + 0.90 - i * 0.1 - 0.05;
253 el.y2 = el.y1 - 0.05;
254 g.push_back(el);
255 }
256 break;
257 }
258 break;
259 case id_BRAM.index:
260 switch (wire_type.index) {
261 case id_RAM_IN.index:
262 z = tilewire - GFX_WIRE_RAM_WA0;
263 el.x1 = loc.x + 0.54;
264 el.x2 = el.x1 + 0.01;
265 el.y1 = loc.y + 0.90 - z * 0.015 - 0.025;
266 el.y2 = el.y1;
267 g.push_back(el);
268 break;
269 case id_RAM_OUT.index:
270 z = tilewire - GFX_WIRE_RAM_DO0;
271 el.x1 = loc.x + 0.90;
272 el.x2 = el.x1 + 0.01;
273 el.y1 = loc.y + 0.90 - z * 0.015 - 0.025;
274 el.y2 = el.y1;
275 g.push_back(el);
276 break;
277 case id_TILE_CLK.index:
278 el.x1 = loc.x + 0.60;
279 el.x2 = el.x1;
280 el.y1 = loc.y + 0.30;
281 el.y2 = el.y1 - 0.025;
282 g.push_back(el);
283 break;
284 }
285 break;
286 case id_IO.index:
287 switch (wire_type.index) {
288 case id_IO_I.index:
289 break;
290 case id_IO_O.index:
291 break;
292 case id_IO_T.index:
293 break;
294 case id_IO_PAD.index:
295 break;
296 case id_TILE_CLK.index:
297 break;
298 case id_GCLK.index:
299 break;
300 }
301 break;
302 case id_NULL.index:
303 switch (wire_type.index) {
304 case id_CLK_ROUTE.index:
305 break;
306 case id_GND.index:
307 break;
308 case id_VCC.index:
309 break;
310 case id_TILE_CLK.index:
311 break;
312 }
313 break;
314 }
315 }
316
317 void drawPip(std::vector<GraphicElement> &g, GraphicElement::style_t style, Loc loc, WireId src, IdString src_type,
318 int32_t src_id, WireId dst, IdString dst_type, int32_t dst_id) override
319 {
320 GraphicElement el;
321 el.type = GraphicElement::TYPE_ARROW;
322 el.style = style;
323 int z;
324 if (src_type == id_LUT_OUT && dst_type == id_FF_DATA) {
325 z = src_id - GFX_WIRE_L0_O;
326 el.x1 = loc.x + 0.45;
327 el.y1 = loc.y + 0.85 - z * 0.1 - 0.025;
328 el.x2 = loc.x + 0.50;
329 el.y2 = el.y1;
330 g.push_back(el);
331 }
332 }
333 };
334
335 struct ExampleArch : HimbaechelArch
336 {
337 ExampleArch() : HimbaechelArch("example") {};
338 bool match_device(const std::string &device) override { return device == "EXAMPLE"; }
339 std::unique_ptr<HimbaechelAPI> create(const std::string &device) override
340 {
341 return std::make_unique<ExampleImpl>();
342 }
343 } exampleArch;
344 } // namespace
345
346 NEXTPNR_NAMESPACE_END