Debian Codegraph @

generic/viaduct/fabulous/validity_check.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2021-22 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 "validity_check.h"
21 #include "log.h"
22 #include "util.h"
23
24 #define VIADUCT_CONSTIDS "viaduct/fabulous/constids.inc"
25 #include "viaduct_constids.h"
26
27 NEXTPNR_NAMESPACE_BEGIN
28
29 CLBState::CLBState(const LogicConfig &cfg)
30 {
31 // TODO: more than one per LC if in split-SLICE mode with fracturable LUTs
32 lc_comb = std::make_unique<CellInfo *[]>(cfg.lc_per_clb);
33 if (cfg.split_lc) {
34 ff = std::make_unique<CellInfo *[]>(cfg.lc_per_clb * cfg.ff_per_lc);
35 }
36 mux = std::make_unique<CellInfo *[]>(cfg.lc_per_clb);
37 // TODO: mux
38 }
39
40 void CellTagger::assign_for(const Context *ctx, const FabricConfig &cfg, const CellInfo *ci)
41 {
42 if (int(data.size()) <= ci->flat_index)
43 data.resize(ci->flat_index + 1);
44 auto &t = data.at(ci->flat_index);
45 // Use the same logic to handle both packed and split LC modes
46 if (ci->type.in(id_FABULOUS_COMB, id_FABULOUS_LC)) {
47 unsigned lut_input_count = 0;
48 for (unsigned i = 0; i < cfg.clb.lut_k; i++)
49 if (ci->getPort(ctx->idf("I%d", i)))
50 lut_input_count = i + 1;
51 t.comb.lut_inputs = SSOArray<IdString, MAX_LUTK>(lut_input_count, IdString());
52 for (unsigned i = 0; i < lut_input_count; i++) {
53 const NetInfo *sig = ci->getPort(ctx->idf("I%d", i));
54 t.comb.lut_inputs[i] = sig ? sig->name : IdString();
55 }
56 t.comb.carry_used = ci->getPort(id_Ci) || ci->getPort(id_Co); // TODO
57 t.comb.lut_out = ci->getPort(id_O);
58 }
59 if (ci->type.in(id_FABULOUS_FF, id_FABULOUS_LC)) {
60 if (ci->type == id_FABULOUS_FF || bool_or_default(ci->params, id_FF)) {
61 t.ff.ff_used = true;
62 auto get_ctrlsig = [&](IdString name) {
63 const NetInfo *sig = ci->getPort(name);
64 bool invert = sig && bool_or_default(ci->params, ctx->idf("NEG_%s", name.c_str(ctx)));
65 if (sig && sig->driver.cell && sig->driver.cell->type.in(id__CONST0_DRV, id__CONST1_DRV)) {
66 return ControlSig(((sig->driver.cell->type == id__CONST1_DRV) ^ invert) ? id__CONST1 : id__CONST0,
67 false);
68 }
69 return ControlSig(sig ? sig->name : id___disconnected, invert);
70 };
71 t.ff.clk = get_ctrlsig(id_CLK);
72 t.ff.sr = get_ctrlsig(id_SR);
73 t.ff.en = get_ctrlsig(id_EN);
74 t.ff.async = bool_or_default(ci->params, id_ASYNC_SR);
75 t.ff.latch = bool_or_default(ci->params, id_LATCH_NOFF);
76 t.ff.d = ci->getPort(id_D);
77 t.ff.q = ci->getPort(id_Q);
78 } else {
79 t.ff.ff_used = false;
80 }
81 }
82 }
83
84 void BlockTracker::set_bel_type(BelId bel, BelFlags::BlockType block, BelFlags::FuncType func, uint8_t index)
85 {
86 Loc loc = ctx->getBelLocation(bel);
87 if (int(tiles.size()) <= loc.y)
88 tiles.resize(loc.y + 1);
89 auto &row = tiles.at(loc.y);
90 if (int(row.size()) <= loc.x)
91 row.resize(loc.x + 1);
92 auto &tile = row.at(loc.x);
93 if (block == BelFlags::BLOCK_CLB) {
94 if (!tile.clb)
95 tile.clb = std::make_unique<CLBState>(cfg.clb);
96 }
97 if (int(bel_data.size()) <= bel.index)
98 bel_data.resize(bel.index + 1);
99 auto &flags = bel_data.at(bel.index);
100 flags.block = block;
101 flags.func = func;
102 flags.index = index;
103 }
104
105 void BlockTracker::update_bel(BelId bel, CellInfo *old_cell, CellInfo *new_cell)
106 {
107 if (bel.index >= int(bel_data.size()))
108 return; // some kind of bel not being tracked
109 auto flags = bel_data.at(bel.index);
110 if (flags.block == BelFlags::BLOCK_OTHER)
111 return; // no structures to update
112 Loc loc = ctx->getBelLocation(bel);
113 if (loc.y >= int(tiles.size()))
114 return; // some kind of bel not being tracked
115 const auto &row = tiles.at(loc.y);
116 if (loc.x >= int(row.size()))
117 return; // some kind of bel not being tracked
118 const auto &entry = row.at(loc.x);
119 if (flags.block == BelFlags::BLOCK_CLB) {
120 NPNR_ASSERT(entry.clb);
121 // TODO: incremental validity check updates might care about this in the future, hence keeping it in the
122 // interface for now
123 NPNR_UNUSED(old_cell);
124 if (flags.func == BelFlags::FUNC_LC_COMB)
125 entry.clb->lc_comb[flags.index] = new_cell;
126 else if (flags.func == BelFlags::FUNC_FF)
127 entry.clb->ff[flags.index] = new_cell;
128 else if (flags.func == BelFlags::FUNC_MUX)
129 entry.clb->mux[flags.index] = new_cell;
130 }
131 }
132
133 bool CLBState::check_validity(const LogicConfig &cfg, const CellTagger &cell_data)
134 {
135 SSOArray<ControlSig, 2> used_clk(cfg.clk.routing.size()), used_sr(cfg.sr.routing.size()),
136 used_en(cfg.en.routing.size());
137 auto check_ctrlsig = [&](unsigned idx, ControlSig actual, const ControlSetConfig &ctrl,
138 SSOArray<ControlSig, 2> &used) {
139 if (ctrl.can_mask != -1) {
140 // Using the per-entry control signal masking
141 if (actual.net == id___disconnected || (actual.net == id__CONST0 && ctrl.can_mask == 0) ||
142 (actual.net == id__CONST1 && ctrl.can_mask == 0)) {
143 return true;
144 }
145 }
146 // see if we have an already-matching signal available
147 for (unsigned i = 0; i < ctrl.routing.size(); i++) {
148 // doesn't route to this pin
149 if (((ctrl.routing.at(i) >> unsigned(idx)) & 0x1U) == 0)
150 continue;
151 if (used[i] == actual)
152 return true;
153 }
154 // see if we have a free slot available
155 for (unsigned i = 0; i < ctrl.routing.size(); i++) {
156 // doesn't route to this pin
157 if (((ctrl.routing.at(i) >> unsigned(idx)) & 0x1U) == 0)
158 continue;
159 if (used[i] != ControlSig())
160 continue;
161 used[i] = actual;
162 return true;
163 }
164 // no option available
165 return false;
166 };
167 for (unsigned z = 0; z < cfg.lc_per_clb; z++) {
168 // flipflop control set checking
169 if (cfg.split_lc) {
170 NPNR_ASSERT_FALSE("unimplemented!"); // TODO
171 } else {
172 NPNR_ASSERT(cfg.ff_per_lc == 1); // split-slice mode must be used for more
173 const CellInfo *lc = lc_comb[z];
174 if (!lc)
175 continue;
176 auto &lct = cell_data.get(lc);
177 if (lct.ff.ff_used) {
178 // check shared control signals
179 if (!check_ctrlsig(z, lct.ff.clk, cfg.clk, used_clk))
180 return false;
181 if (cfg.en.have_signal && !check_ctrlsig(z, lct.ff.en, cfg.en, used_en))
182 return false;
183 if (cfg.sr.have_signal && !check_ctrlsig(z, lct.ff.sr, cfg.sr, used_sr))
184 return false;
185 }
186 }
187 }
188 // don't allow mixed MUX types in the classic fabulous arch where ctrl sigs are shared
189 int tile_mux_type = 0;
190 for (unsigned z = 0; z < cfg.lc_per_clb; z++) {
191 const CellInfo *m = mux[z];
192 if (!m)
193 continue;
194 int this_mux = 0;
195 if (m->type == id_FABULOUS_MUX2)
196 this_mux = 2;
197 else if (m->type == id_FABULOUS_MUX4)
198 this_mux = 4;
199 else if (m->type == id_FABULOUS_MUX8)
200 this_mux = 8;
201 else
202 NPNR_ASSERT_FALSE("unknown mux type");
203 if (tile_mux_type == 0)
204 tile_mux_type = this_mux;
205 else if (tile_mux_type != this_mux)
206 return false;
207 }
208 // TODO: other checks...
209 return true;
210 }
211
212 bool BlockTracker::check_validity(BelId bel, const FabricConfig &cfg, const CellTagger &cell_data)
213 {
214 if (bel.index >= int(bel_data.size()))
215 return true; // some kind of bel not being tracked
216 auto flags = bel_data.at(bel.index);
217 if (flags.block == BelFlags::BLOCK_OTHER)
218 return true; // no structures to update
219 Loc loc = ctx->getBelLocation(bel);
220 if (loc.y >= int(tiles.size()))
221 return true; // some kind of bel not being tracked
222 const auto &row = tiles.at(loc.y);
223 if (loc.x >= int(row.size()))
224 return true; // some kind of bel not being tracked
225 const auto &entry = row.at(loc.x);
226 if (flags.block == BelFlags::BLOCK_CLB) {
227 return entry.clb->check_validity(cfg.clb, cell_data);
228 } else {
229 return true;
230 }
231 }
232
233 NEXTPNR_NAMESPACE_END