Debian Codegraph @

himbaechel/uarch/ng-ultra/csv.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Beyond Authors.
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 <algorithm>
21 #include <boost/algorithm/string.hpp>
22 #include <fstream>
23 #include <regex>
24
25 #include "extra_data.h"
26 #include "himbaechel_api.h"
27 #include "log.h"
28 #include "nextpnr.h"
29 #include "util.h"
30
31 #include "ng_ultra.h"
32
33 #define HIMBAECHEL_CONSTIDS "uarch/ng-ultra/constids.inc"
34 #include "himbaechel_constids.h"
35
36 NEXTPNR_NAMESPACE_BEGIN
37
38 void NgUltraImpl::parse_csv(const std::string &filename)
39 {
40 std::ifstream in(filename);
41 if (!in)
42 log_error("failed to open CSV file '%s'\n", filename.c_str());
43 log_info("Parsing CSV file..\n");
44 std::string line;
45 std::string linebuf;
46 int lineno = 0;
47 enum uint8_t
48 {
49 IO_PADS = 0,
50 IO_BANKS,
51 IO_GCKS,
52 IO_ERROR
53 } line_type;
54
55 auto isempty = [](const std::string &str) {
56 return std::all_of(str.begin(), str.end(), [](char c) { return std::isspace(c); });
57 };
58 auto is_number = [](const std::string &str) {
59 return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
60 };
61 auto get_cells = [&](std::string str) {
62 std::vector<CellInfo *> tgt_cells;
63 IdString cellname = ctx->id(str);
64 if (ctx->cells.count(cellname))
65 tgt_cells.push_back(ctx->cells.at(cellname).get());
66 return tgt_cells;
67 };
68
69 line_type = IO_PADS;
70 pool<std::string> banks_used;
71 bool old_format = false;
72 while (std::getline(in, line)) {
73 ++lineno;
74 // Trim comments, from # until end of the line
75 size_t cstart = line.find('#');
76 if (cstart != std::string::npos)
77 line = line.substr(0, cstart);
78 if (isempty(line))
79 continue;
80
81 std::vector<std::string> arguments;
82 boost::split(arguments, line, boost::is_any_of(","));
83 if (arguments.empty())
84 continue;
85 switch (line_type) {
86 case IO_PADS: {
87 if (arguments.size() == 1 && arguments[0][0] == '!') {
88 line_type = IO_BANKS;
89 continue;
90 }
91 if (arguments.size() != 15)
92 log_error("number of parameters in line %d must be 15\n", lineno);
93
94 std::string arg_iobname = arguments.at(0);
95 std::string arg_location = arguments.at(1);
96 std::string arg_standard = arguments.at(2);
97 std::string arg_drive = arguments.at(3);
98 std::string arg_slewRate = arguments.at(4);
99 std::string arg_inputDelayLine = arguments.at(5);
100 std::string arg_outputDelayLine = arguments.at(6);
101 std::string arg_differential = arguments.at(7);
102 std::string arg_weakTermination = arguments.at(8);
103 std::string arg_termination = arguments.at(9);
104 std::string arg_terminationReference = arguments.at(10);
105 std::string arg_turbo = arguments.at(11);
106 std::string arg_inputSignalSlope = arguments.at(12);
107 std::string arg_outputCapacity = arguments.at(13);
108 std::string arg_registered = arguments.at(14);
109
110 // TODO: Remove this block
111 const std::vector<std::string> weak_values_check = {"None", "PullDown", "PullUp", "Keeper"};
112 auto it2 = std::find(std::begin(weak_values_check), std::end(weak_values_check), arguments.at(4));
113 if (it2 != std::end(weak_values_check)) {
114 if (!old_format) {
115 log_warning("Old CSV format detected. Please update file.\n");
116 old_format = true;
117 }
118 arg_weakTermination = arguments.at(4);
119 arg_slewRate = arguments.at(5);
120 arg_termination = arguments.at(6);
121 arg_inputDelayLine = arguments.at(7);
122 arg_outputDelayLine = arguments.at(8);
123 arg_differential = arguments.at(9);
124 }
125 // End of block
126
127 if (!(boost::starts_with(arg_location, "IOB") && boost::contains(arg_location, "_D")))
128 log_error("invalid location name '%s' must start with 'IOB' in line %d\n", arg_location.c_str(),
129 lineno);
130
131 const std::vector<std::string> standard_values = {"LVDS", "LVCMOS", "SSTL", "HSTL"}; // , "POD"
132 auto it = std::find(std::begin(standard_values), std::end(standard_values), arg_standard);
133 if (it == std::end(standard_values))
134 log_error("unknown standard value '%s' in line %d\n", arg_standard.c_str(), lineno);
135
136 const std::vector<std::string> drive_values = {"2mA", "4mA", "8mA", "16mA",
137 "CatI", "CatII", "Undefined"}; // "6mA", "12mA",
138 it = std::find(std::begin(drive_values), std::end(drive_values), arg_drive);
139 if (it == std::end(drive_values))
140 log_error("unknown drive value '%s' in line %d\n", arg_drive.c_str(), lineno);
141
142 const std::vector<std::string> slew_values = {"Slow", "Medium", "Fast"};
143 it = std::find(std::begin(slew_values), std::end(slew_values), arg_slewRate);
144 if (it == std::end(slew_values))
145 log_error("unknown weak termination value '%s' in line %d\n", arg_slewRate.c_str(), lineno);
146
147 if (!is_number(arg_inputDelayLine)) {
148 log_error("input delay must be number, value '%s' in line %d\n", arg_inputDelayLine.c_str(), lineno);
149 } else {
150 int delay = std::stoi(arg_inputDelayLine);
151 if (delay < 0 || delay > 63)
152 log_error("input delay value must be in range from 0 to 63 in line %d\n", lineno);
153 }
154 if (!is_number(arg_outputDelayLine)) {
155 log_error("output delay must be number, value '%s' in line %d\n", arg_outputDelayLine.c_str(), lineno);
156 } else {
157 int delay = std::stoi(arg_outputDelayLine);
158 if (delay < 0 || delay > 63)
159 log_error("output delay value must be in range from 0 to 63 in line %d\n", lineno);
160 }
161
162 if (!arg_differential.empty() && arg_differential != "True" && arg_differential != "False")
163 log_error("differential must be boolean, value '%s' in line %d\n", arg_differential.c_str(), lineno);
164
165 const std::vector<std::string> weak_values = {"None", "PullDown", "PullUp", "Keeper"};
166 it = std::find(std::begin(weak_values), std::end(weak_values), arg_weakTermination);
167 if (it == std::end(weak_values))
168 log_error("unknown weak termination value '%s' in line %d\n", arg_weakTermination.c_str(), lineno);
169
170 if (!arg_termination.empty()) {
171 if (!is_number(arg_termination)) {
172 log_error("termination must be string containing int, value '%s' in line %d\n",
173 arg_termination.c_str(), lineno);
174 } else {
175 int termination = std::stoi(arg_termination);
176 if (termination < 30 || termination > 80)
177 log_error("termination value must be in range from 30 to 80 in line %d\n", lineno);
178 }
179 }
180
181 const std::vector<std::string> termref_values = {"Floating", "VT"};
182 it = std::find(std::begin(termref_values), std::end(termref_values), arg_terminationReference);
183 if (it == std::end(termref_values))
184 log_error("unknown termination reference value '%s' in line %d\n", arg_terminationReference.c_str(),
185 lineno);
186
187 if (!arg_turbo.empty() && arg_turbo != "True" && arg_turbo != "False")
188 log_error("turbo must be boolean, value '%s' in line %d\n", arg_turbo.c_str(), lineno);
189
190 if (!arg_inputSignalSlope.empty() && !is_number(arg_inputSignalSlope))
191 log_error("signal slope must be number, value '%s' in line %d\n", arg_inputSignalSlope.c_str(), lineno);
192 if (!arg_outputCapacity.empty() && !is_number(arg_outputCapacity))
193 log_error("output capacity must be number, value '%s' in line %d\n", arg_outputCapacity.c_str(),
194 lineno);
195
196 const std::vector<std::string> registered_values = {"Auto", "I", "IC", "O", "OC", "IO", "IOC"};
197 it = std::find(std::begin(registered_values), std::end(registered_values), arg_registered);
198 if (it == std::end(registered_values))
199 log_error("unknown registered value '%s' in line %d\n", arg_registered.c_str(), lineno);
200
201 if (arg_standard == "LVDS" && arg_drive != "Undefined")
202 log_error("for port in line %d when standard is 'LVDS' drive must be 'Undefined'\n", lineno);
203 if (arg_standard == "LVCMOS" && !boost::ends_with(arg_drive, "mA"))
204 log_error("for port in line %d when standard is 'LVCMOS' drive current must be in mA\n", lineno);
205 if ((arg_standard == "SSTL" || arg_standard == "HSTL") && !boost::starts_with(arg_drive, "Cat"))
206 log_error("for port in line %d when standard is 'SSTL' or 'HSTL' drive current must be in 'CatI' or "
207 "'CatII'\n",
208 lineno);
209
210 if (arg_terminationReference == "Floating") {
211 if (!(arg_differential == "True" && arg_weakTermination == "None")) {
212 log_error("for floating termination, differential myst be 'True' and weakTermination must be "
213 "'None' in line %d\n",
214 lineno);
215 }
216 }
217 std::vector<CellInfo *> dest = get_cells(arg_iobname);
218 for (auto c : dest) {
219 c->params[id_iobname] = arg_iobname;
220 c->params[id_location] = arg_location;
221 c->params[id_standard] = arg_standard;
222 c->params[id_drive] = arg_drive;
223 c->params[id_slewRate] = arg_slewRate;
224 c->params[id_inputDelayLine] = arg_inputDelayLine;
225 c->params[id_outputDelayLine] = arg_outputDelayLine;
226 c->params[id_inputDelayOn] = std::string((std::stoi(arg_inputDelayLine) != 0) ? "True" : "False");
227 c->params[id_outputDelayOn] = std::string((std::stoi(arg_outputDelayLine) != 0) ? "True" : "False");
228 c->params[id_differential] = arg_differential;
229 c->params[id_weakTermination] = arg_weakTermination;
230 if (!arg_termination.empty()) {
231 c->params[id_termination] = arg_termination;
232 c->params[id_terminationReference] = arg_terminationReference;
233 }
234 c->params[id_turbo] = arg_turbo;
235 c->params[id_inputSignalSlope] = arg_inputSignalSlope;
236 c->params[id_outputCapacity] = arg_outputCapacity;
237 c->params[id_registered] = arg_registered;
238 }
239 if (dest.size() == 0)
240 log_warning("Pad with name '%s' not found in netlist.\n", arg_iobname.c_str());
241
242 std::string bank_name = arg_location.substr(0, arg_location.find_first_of('_'));
243 banks_used.emplace(bank_name);
244 } break;
245 case IO_BANKS: {
246 if (arguments.size() == 1 && arguments[0][0] == '!') {
247 line_type = IO_GCKS;
248 continue;
249 }
250 if (arguments.size() != 3)
251 log_error("number of parameters in line %d must be 3\n", lineno);
252
253 if (!boost::starts_with(arguments.at(0), "IOB"))
254 log_error("wrong bank name '%s' in line %d\n", arguments.at(0).c_str(), lineno);
255
256 const char *voltages[] = {"1.2V", "1.5V", "1.8V", "2.5V", "3.3V"};
257 auto it = std::find(std::begin(voltages), std::end(voltages), arguments.at(1));
258 if (it == std::end(voltages))
259 log_error("unknown voltage level '%s' in line %d\n", arguments.at(1).c_str(), lineno);
260
261 const char *direct_io_voltages[] = {"1.8V", "2.5V", "3.3V"};
262 const char *complex_io_voltages[] = {"1.2V", "1.5V", "1.8V"};
263
264 int bank = std::stoi(arguments.at(0).substr(3));
265 switch (bank) {
266 // direct
267 case 0:
268 case 1:
269 case 6:
270 case 7: {
271 auto it = std::find(std::begin(direct_io_voltages), std::end(direct_io_voltages), arguments.at(1));
272 if (it == std::end(direct_io_voltages))
273 log_error("unsupported voltage level '%s' for bank '%s'\n", arguments.at(1).c_str(),
274 arguments.at(0).c_str());
275 } break;
276 // complex
277 default:
278 auto it = std::find(std::begin(complex_io_voltages), std::end(complex_io_voltages), arguments.at(1));
279 if (it == std::end(complex_io_voltages))
280 log_error("unsupported voltage level '%s' for bank '%s'\n", arguments.at(1).c_str(),
281 arguments.at(0).c_str());
282 }
283 bank_voltage[arguments.at(0)] = arguments.at(1);
284 } break;
285 case IO_GCKS: {
286 if (arguments.size() == 1 && arguments[0][0] == '!') {
287 line_type = IO_ERROR;
288 continue;
289 }
290 if (arguments.size() != 2)
291 log_error("number of parameters in line %d must be 2\n", lineno);
292 } break;
293 default:
294 log_error("switching to unknown block of data in line %d\n", lineno);
295 }
296 }
297 for (auto &bank_name : banks_used) {
298 if (bank_voltage.count(bank_name) == 0) {
299 log_error("IO for bank '%s' defined, but no bank configuration.\n", bank_name.c_str());
300 }
301 }
302 }
303
304 NEXTPNR_NAMESPACE_END