HAL  v4.5.0-313-g29bd89cc0
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
hgl_parser.cpp
Go to the documentation of this file.
2 
12 #include "hal_core/utilities/log.h"
14 #include "hgl_parser/upgrade_hgl.h"
15 #include "rapidjson/filereadstream.h"
16 
17 namespace hal
18 {
19 
20  bool HGLParser::do_not_upgrade_gatelib(const rapidjson::Document& document) const
21  {
22  if (!document.IsObject())
23  {
24  log_warning("gate_library_parser", "'{}' cannot be upgraded to HGL format version {} since it is not a JSON object.", m_path.string(), HGL_FORMAT_VERSION);
25  return true;
26  }
27  if (!document.HasMember("version"))
28  {
29  log_warning("gate_library_parser", "'{}' cannot be upgraded to HGL format version {} since version tag is missing.", m_path.string(), HGL_FORMAT_VERSION);
30  return true;
31  }
32  if (!document["version"].IsUint())
33  {
34  log_warning("gate_library_parser", "'{}' cannot be upgraded to HGL format version {} since current version is not a number.", m_path.string(), HGL_FORMAT_VERSION);
35  return true;
36  }
37  // also catches files of the current version that still contain LUT configurations in the old format
38  return !hgl_upgrade::needs_upgrade(document);
39  }
40 
41  Result<std::unique_ptr<GateLibrary>> HGLParser::parse(const std::filesystem::path& file_path)
42  {
43  m_path = file_path;
44 
45  FILE* fp = fopen(file_path.string().c_str(), "r");
46  if (fp == NULL)
47  {
48  return ERR("could not parse HGL file '" + m_path.string() + "' : unable to open file");
49  }
50 
51  char buffer[65536];
52  rapidjson::FileReadStream is(fp, buffer, sizeof(buffer));
53  rapidjson::Document document;
54  document.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is);
55  fclose(fp);
56 
57  if (document.HasParseError())
58  {
59  return ERR("could not parse HGL file '" + m_path.string() + "': failed parsing JSON format");
60  }
61 
62  if (auto res = upgrade_outdated_file(document); res.is_error())
63  {
64  return ERR_APPEND(res.get_error(), "could not parse HGL file '" + m_path.string() + "': failed to upgrade outdated file format");
65  }
66 
67  if (auto res = parse_gate_library(document); res.is_error())
68  {
69  return ERR_APPEND(res.get_error(), "could not parse HGL file '" + m_path.string() + "': failed to parse gate library");
70  }
71 
73 
74  return OK(std::move(m_gate_lib));
75  }
76 
77  Result<std::monostate> HGLParser::upgrade_outdated_file(rapidjson::Document& document)
78  {
79  if (do_not_upgrade_gatelib(document))
80  {
81  // up to date, or not upgradable at all, which parse_gate_library reports
82  return OK({});
83  }
84 
85  const u32 version = document["version"].GetUint();
86 
87  // the upgrade happens in memory first, so that the user is not asked for an upgrade that cannot succeed
88  auto res = hgl_upgrade::upgrade_document(document);
89  if (res.is_error())
90  {
91  return ERR(res.get_error());
92  }
93  for (const std::string& warning : res.get())
94  {
95  log_warning("gate_library_parser", "upgrade of '{}' to HGL format version {}: {}", m_path.string(), HGL_FORMAT_VERSION, warning);
96  }
97 
98  // replacing the file requires the consent of the user
99  if (!is_upgrade_requested())
100  {
102  if (version < HGL_FORMAT_VERSION)
103  {
104  return ERR("HGL file format version " + std::to_string(version) + " is outdated (requires version " + std::to_string(HGL_FORMAT_VERSION) + "), the file needs to be upgraded");
105  }
106  return ERR("HGL file contains LUT configurations in the format of version 5 or earlier, the file needs to be upgraded");
107  }
108 
109  auto res_replace = hgl_upgrade::replace_file(document, m_path);
110  if (res_replace.is_error())
111  {
112  return ERR(res_replace.get_error());
113  }
114 
115  if (version < HGL_FORMAT_VERSION)
116  {
117  log_info("gate_library_parser", "upgraded gate library '{}' from HGL format version {} to {}, the original file was kept as '{}'.", m_path.string(), version, HGL_FORMAT_VERSION, res_replace.get().string());
118  }
119  else
120  {
121  log_info("gate_library_parser", "migrated the remaining LUT configurations of gate library '{}', the original file was kept as '{}'.", m_path.string(), res_replace.get().string());
122  }
123  return OK({});
124  }
125 
126  Result<std::monostate> HGLParser::parse_gate_library(const rapidjson::Document& document)
127  {
128  if (!document.HasMember("version") || !document["version"].IsUint() || document["version"].GetUint() < HGL_FORMAT_VERSION)
129  {
130  return ERR("could not parse gate library: HGL file format version is outdated (requires version " + std::to_string(HGL_FORMAT_VERSION) + ")");
131  }
132 
133  if (!document.HasMember("library"))
134  {
135  return ERR("could not parse gate library: missing 'library' node");
136  }
137 
138  const std::string gate_lib_name = document["library"].GetString();
139  m_gate_lib = std::make_unique<GateLibrary>(m_path, gate_lib_name);
140 
141  if (document.HasMember("gate_locations") && document["gate_locations"].IsObject())
142  {
143  auto gate_locs = document["gate_locations"].GetObject();
144 
145  if (!gate_locs.HasMember("data_category") || !gate_locs["data_category"].IsString())
146  {
147  return ERR("could not parse gate library '" + gate_lib_name + "': missing 'data_category' entry for gate locations");
148  }
149 
150  if (!gate_locs.HasMember("data_x_identifier") || !gate_locs["data_x_identifier"].IsString())
151  {
152  return ERR("could not parse gate library '" + gate_lib_name + "': missing 'data_x_identifier' entry for gate locations");
153  }
154 
155  if (!gate_locs.HasMember("data_y_identifier") || !gate_locs["data_y_identifier"].IsString())
156  {
157  return ERR("could not parse gate library '" + gate_lib_name + "': missing 'data_y_identifier' entry for gate locations");
158  }
159 
160  m_gate_lib->set_gate_location_data_category(gate_locs["data_category"].GetString());
161  m_gate_lib->set_gate_location_data_identifiers(gate_locs["data_x_identifier"].GetString(), gate_locs["data_y_identifier"].GetString());
162  }
163 
164  if (!document.HasMember("cells"))
165  {
166  return ERR("could not parse gate library '" + gate_lib_name + "': missing 'cells' node");
167  }
168 
169  for (const auto& gate_type : document["cells"].GetArray())
170  {
171  if (auto res = parse_gate_type(gate_type); res.is_error())
172  {
173  return ERR_APPEND(res.get_error(), "could not parse gate library '" + gate_lib_name + "': failed parsing gate type");
174  }
175  }
176 
177  return OK({});
178  }
179 
180  Result<std::monostate> HGLParser::parse_gate_type(const rapidjson::Value& gate_type)
181  {
182  if (!gate_type.HasMember("name") || !gate_type["name"].IsString())
183  {
184  return ERR("could not parse gate type: missing or invalid name");
185  }
186 
187  const std::string name = gate_type["name"].GetString();
188  std::set<GateTypeProperty> properties;
189  if (gate_type.HasMember("types") && gate_type["types"].IsArray())
190  {
191  for (const auto& base_type : gate_type["types"].GetArray())
192  {
193  const std::string property_str = base_type.GetString();
194 
195  try
196  {
197  GateTypeProperty property = enum_from_string<GateTypeProperty>(property_str);
198  properties.insert(property);
199  }
200  catch (const std::runtime_error&)
201  {
202  return ERR("could not parse gate type '" + name + "': invalid property '" + base_type.GetString() + "'");
203  }
204  }
205  }
206  else
207  {
208  properties = {GateTypeProperty::combinational};
209  }
210 
211  std::unique_ptr<GateTypeComponent> parent_component = nullptr;
212  if (gate_type.HasMember("lut_config") && gate_type["lut_config"].IsObject())
213  {
214  const auto& lut_cfg = gate_type["lut_config"];
215  if (auto res = parse_lut_config(lut_cfg); res.is_error())
216  {
217  return ERR_APPEND(res.get_error(), "could not parse gate type '" + name + "': failed parsing LUT configuration");
218  }
219  else
220  {
221  parent_component = res.get();
222  }
223  }
224  else if (gate_type.HasMember("ff_config") && gate_type["ff_config"].IsObject())
225  {
226  if (auto res = parse_ff_config(gate_type["ff_config"]); res.is_error())
227  {
228  return ERR_APPEND(res.get_error(), "could not parse gate type '" + name + "': failed parsing FF configuration");
229  }
230  else
231  {
232  parent_component = res.get();
233  }
234  }
235  else if (gate_type.HasMember("latch_config") && gate_type["latch_config"].IsObject())
236  {
237  if (auto res = parse_latch_config(gate_type["latch_config"]); res.is_error())
238  {
239  return ERR_APPEND(res.get_error(), "could not parse gate type '" + name + "': failed parsing latch configuration");
240  }
241  else
242  {
243  parent_component = res.get();
244  }
245  }
246  else if (gate_type.HasMember("ram_config") && gate_type["ram_config"].IsObject())
247  {
248  if (auto res = parse_ram_config(gate_type["ram_config"]); res.is_error())
249  {
250  return ERR_APPEND(res.get_error(), "could not parse gate type '" + name + "': failed parsing RAM configuration");
251  }
252  else
253  {
254  parent_component = res.get();
255  }
256  }
257 
258  GateType* gt = m_gate_lib->create_gate_type(name, properties, std::move(parent_component));
259  if (gt == nullptr)
260  {
261  return ERR("could not parse gate type '" + name + "': failed to create gate type");
262  }
263 
264  // Parameters are declared at the gate-type level. Their declared sizes feed the BF parser
265  // for any Boolean functions in this cell that reference parameters as variables.
266  std::map<std::string, u16> var_sizes;
267  if (gate_type.HasMember("parameters") && gate_type["parameters"].IsArray())
268  {
269  for (const auto& param_val : gate_type["parameters"].GetArray())
270  {
271  if (!param_val.HasMember("name") || !param_val["name"].IsString())
272  {
273  return ERR("could not parse parameter for gate type '" + name + "': missing or invalid name");
274  }
275  if (!param_val.HasMember("type") || !param_val["type"].IsString())
276  {
277  return ERR("could not parse parameter for gate type '" + name + "': missing or invalid type");
278  }
279  if (!param_val.HasMember("default") || !param_val["default"].IsString())
280  {
281  return ERR("could not parse parameter for gate type '" + name + "': missing or invalid default value");
282  }
283 
284  const std::string pname = param_val["name"].GetString();
285  const std::string pdefault = param_val["default"].GetString();
286  const std::string ptype = param_val["type"].GetString();
287 
288  Parameter param;
289 
290  if (ptype == "boolean")
291  {
292  auto param_res = Parameter::Boolean(pname, pdefault);
293  if (param_res.is_error())
294  {
295  return ERR(param_res.get_error());
296  }
297  param = param_res.get();
298  }
299  else if (ptype == "bit_vector")
300  {
301  if (!param_val.HasMember("size") || !param_val["size"].IsUint())
302  {
303  return ERR("could not parse parameter '" + pname + "' for gate type '" + name + "': bit-vector parameter requires a 'size' field");
304  }
305  const u32 sz = param_val["size"].GetUint();
306 
307  auto param_res = Parameter::BitVector(pname, static_cast<u16>(sz), pdefault);
308  if (param_res.is_error())
309  {
310  return ERR(param_res.get_error());
311  }
312  param = param_res.get();
313  }
314  else if (ptype == "logic_vector")
315  {
316  if (!param_val.HasMember("size") || !param_val["size"].IsUint())
317  {
318  return ERR("could not parse parameter '" + pname + "' for gate type '" + name + "': logic-vector parameter requires a 'size' field");
319  }
320  const u32 sz = param_val["size"].GetUint();
321 
322  auto param_res = Parameter::LogicVector(pname, static_cast<u16>(sz), pdefault);
323  if (param_res.is_error())
324  {
325  return ERR(param_res.get_error());
326  }
327  param = param_res.get();
328  }
329  else if (ptype == "integer")
330  {
331  auto param_res = Parameter::Integer(pname, pdefault);
332  if (param_res.is_error())
333  {
334  return ERR(param_res.get_error());
335  }
336  param = param_res.get();
337  }
338  else if (ptype == "string")
339  {
340  auto param_res = Parameter::String(pname, pdefault);
341  if (param_res.is_error())
342  {
343  return ERR(param_res.get_error());
344  }
345  param = param_res.get();
346  }
347  else if (ptype == "float")
348  {
349  auto param_res = Parameter::Float(pname, pdefault);
350  if (param_res.is_error())
351  {
352  return ERR(param_res.get_error());
353  }
354  param = param_res.get();
355  }
356  else if (ptype == "time")
357  {
358  auto param_res = Parameter::Time(pname, pdefault);
359  if (param_res.is_error())
360  {
361  return ERR(param_res.get_error());
362  }
363  param = param_res.get();
364  }
365  else if (ptype == "enum")
366  {
367  if (!param_val.HasMember("values") || !param_val["values"].IsArray())
368  {
369  return ERR("could not parse parameter '" + pname + "' for gate type '" + name + "': enum parameter requires a 'values' array");
370  }
371  std::vector<std::string> values;
372  for (const auto& v : param_val["values"].GetArray())
373  {
374  if (!v.IsString())
375  {
376  return ERR("could not parse parameter '" + pname + "' for gate type '" + name + "': enum 'values' must be strings");
377  }
378  values.emplace_back(v.GetString());
379  }
380 
381  auto param_res = Parameter::Enum(pname, std::move(values), pdefault);
382  if (param_res.is_error())
383  {
384  return ERR(param_res.get_error());
385  }
386  param = param_res.get();
387  }
388  else
389  {
390  return ERR("could not parse parameter '" + pname + "' for gate type '" + name + "': unknown type '" + ptype + "'");
391  }
392 
393  if (auto res = gt->add_parameter(param); res.is_error())
394  {
395  return ERR_APPEND(res.get_error(), "could not parse parameters for gate type '" + name + "'");
396  }
397 
398  var_sizes[param.get_name()] = param.get_size();
399  }
400  }
401 
402  if (gate_type.HasMember("pin_groups") && gate_type["pin_groups"].IsArray())
403  {
404  for (const auto& pg_val : gate_type["pin_groups"].GetArray())
405  {
406  if (!pg_val.HasMember("name") || !pg_val["name"].IsString())
407  {
408  return ERR("could not parse pin group: missing or invalid name");
409  }
410  std::string pg_name = pg_val["name"].GetString();
411 
412  if (!pg_val.HasMember("direction") || !pg_val["direction"].IsString())
413  {
414  return ERR("could not parse pin group '" + pg_name + "': missing or invalid pin direction");
415  }
416  PinDirection pg_direction;
417  std::string pg_direction_str = pg_val["direction"].GetString();
418  try
419  {
420  pg_direction = enum_from_string<PinDirection>(pg_direction_str);
421  }
422  catch (const std::runtime_error&)
423  {
424  return ERR("could not parse pin '" + pg_name + "': invalid pin direction '" + pg_direction_str + "'");
425  }
426 
427  if (!pg_val.HasMember("type") || !pg_val["type"].IsString())
428  {
429  return ERR("could not parse pin group '" + pg_name + "': missing or invalid pin type");
430  }
431  PinType pg_type;
432  std::string pg_type_str = pg_val["type"].GetString();
433  try
434  {
435  pg_type = enum_from_string<PinType>(pg_type_str);
436  }
437  catch (const std::runtime_error&)
438  {
439  return ERR("could not parse pin group '" + pg_name + "': invalid pin type '" + pg_type_str + "'");
440  }
441 
442  if (!pg_val.HasMember("ascending") || !pg_val["ascending"].IsBool())
443  {
444  return ERR("could not parse pin group '" + pg_name + "': missing or ascending property");
445  }
446  bool ascending = pg_val["ascending"].GetBool();
447 
448  bool ordered = false;
449  if (pg_val.HasMember("ordered") && pg_val["ordered"].IsBool())
450  {
451  ordered = pg_val["ordered"].GetBool();
452  }
453 
454  if (!pg_val.HasMember("start_index") || !pg_val["start_index"].IsUint())
455  {
456  return ERR("could not parse pin group '" + pg_name + "': missing or start index");
457  }
458  u32 start_index = pg_val["start_index"].GetUint();
459 
460  if (pg_val.HasMember("pins") && pg_val["pins"].IsArray())
461  {
462  std::vector<GatePin*> pins;
463 
464  for (const auto& p_val : pg_val["pins"].GetArray())
465  {
466  if (!p_val.HasMember("name") || !p_val["name"].IsString())
467  {
468  return ERR("could not parse pin: missing or invalid name");
469  }
470  std::string p_name = p_val["name"].GetString();
471 
472  if (!p_val.HasMember("direction") || !p_val["direction"].IsString())
473  {
474  return ERR("could not parse pin '" + p_name + "': missing or invalid pin direction");
475  }
476  PinDirection p_direction;
477  std::string p_direction_str = p_val["direction"].GetString();
478  try
479  {
480  p_direction = enum_from_string<PinDirection>(p_direction_str);
481  }
482  catch (const std::runtime_error&)
483  {
484  return ERR("could not parse pin '" + p_name + "': invalid pin direction '" + p_direction_str + "'");
485  }
486 
487  PinType p_type;
488  if (p_val.HasMember("type") && p_val["type"].IsString())
489  {
490  std::string p_type_str = p_val["type"].GetString();
491  try
492  {
493  p_type = enum_from_string<PinType>(p_type_str);
494  }
495  catch (const std::runtime_error&)
496  {
497  return ERR("could not parse pin '" + p_name + "': invalid pin type '" + p_type_str + "'");
498  }
499  }
500  else
501  {
502  p_type = PinType::none;
503  }
504 
505  auto p_res = gt->create_pin(p_name, p_direction, p_type, false);
506  if (p_res.is_error())
507  {
508  return ERR_APPEND(p_res.get_error(), "could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": failed to create pin '" + p_name + "'");
509  }
510  pins.push_back(p_res.get());
511 
512  if (p_val.HasMember("function") && p_val["function"].IsString())
513  {
514  if (auto res = BooleanFunction::from_string(p_val["function"].GetString(), var_sizes); res.is_error())
515  {
516  return ERR_APPEND(res.get_error(),
517  "could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": failed parsing Boolean function with name '" + p_name
518  + "' from string");
519  }
520  else
521  {
522  gt->add_boolean_function(p_name, res.get());
523  }
524  }
525 
526  if (p_val.HasMember("x_function") && p_val["x_function"].IsString())
527  {
528  if (auto res = BooleanFunction::from_string(p_val["x_function"].GetString(), var_sizes); res.is_error())
529  {
530  return ERR_APPEND(res.get_error(),
531  "could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": failed parsing Boolean function with name '" + p_name
532  + "_undefined' from string");
533  }
534  else
535  {
536  gt->add_boolean_function(p_name + "_undefined", res.get());
537  }
538  }
539 
540  if (p_val.HasMember("z_function") && p_val["z_function"].IsString())
541  {
542  if (auto res = BooleanFunction::from_string(p_val["z_function"].GetString(), var_sizes); res.is_error())
543  {
544  return ERR_APPEND(res.get_error(),
545  "could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": failed parsing Boolean function with name '" + p_name
546  + "_tristate' from string");
547  }
548  else
549  {
550  gt->add_boolean_function(p_name + "_tristate", res.get());
551  }
552  }
553  }
554 
555  auto pg_res = gt->create_pin_group(pg_name, pins, pg_direction, pg_type, ascending, start_index, ordered);
556  if (pg_res.is_error())
557  {
558  return ERR_APPEND(pg_res.get_error(), "could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": failed to create pin group '" + pg_name + "'");
559  }
560  }
561  else
562  {
563  return ERR("could not parse gate type '" + name + "' with ID " + std::to_string(gt->get_id()) + ": no pins given for pin group with name '" + pg_name + "'");
564  }
565  }
566  }
567 
568  return OK({});
569  }
570 
571  Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_lut_config(const rapidjson::Value& lut_config)
572  {
573  // New format: output_pins array with per-pin INIT identifier, bit range, bit order, and input pins.
574  if (lut_config.HasMember("output_pins") && lut_config["output_pins"].IsArray())
575  {
576  struct PinCfg
577  {
578  std::string pin_name;
579  std::string identifier;
580  u32 bit_offset;
581  u32 bit_count;
582  bool is_ascending;
583  std::vector<std::string> input_pins;
584  };
585  std::vector<PinCfg> pin_cfgs;
586 
587  for (const auto& entry : lut_config["output_pins"].GetArray())
588  {
589  if (!entry.HasMember("pin") || !entry["pin"].IsString())
590  {
591  return ERR("could not parse LUT configuration: missing or invalid pin name in output_pins entry");
592  }
593  const std::string pin_name = entry["pin"].GetString();
594 
595  if (!entry.HasMember("data_identifier") || !entry["data_identifier"].IsString())
596  {
597  return ERR("could not parse LUT configuration: missing or invalid data_identifier for pin '" + pin_name + "'");
598  }
599  const std::string identifier = entry["data_identifier"].GetString();
600 
601  if (!entry.HasMember("bit_offset") || !entry["bit_offset"].IsUint())
602  {
603  return ERR("could not parse LUT configuration: missing or invalid bit_offset for pin '" + pin_name + "'");
604  }
605  const u32 bit_offset = entry["bit_offset"].GetUint();
606 
607  if (!entry.HasMember("bit_count") || !entry["bit_count"].IsUint())
608  {
609  return ERR("could not parse LUT configuration: missing or invalid bit_count for pin '" + pin_name + "'");
610  }
611  const u32 bit_count = entry["bit_count"].GetUint();
612 
613  // Per-pin is_ascending (bool) overrides global; falls back to global_ascending.
614  if (!entry.HasMember("is_ascending") || !entry["is_ascending"].IsBool())
615  {
616  return ERR("could not parse LUT configuration: missing or invalid is_ascending for pin '" + pin_name + "'");
617  }
618  bool pin_ascending = entry["is_ascending"].GetBool();
619 
620  if (!entry.HasMember("input_pins") || !entry["input_pins"].IsArray())
621  {
622  return ERR("could not parse LUT configuration: missing or invalid input_pins for pin '" + pin_name + "'");
623  }
624  std::vector<std::string> input_pins;
625  for (const auto& pv : entry["input_pins"].GetArray())
626  {
627  if (!pv.IsString())
628  {
629  return ERR("could not parse LUT configuration: non-string entry in input_pins for pin '" + pin_name + "'");
630  }
631  input_pins.emplace_back(pv.GetString());
632  }
633  if (input_pins.empty())
634  {
635  return ERR("could not parse LUT configuration: input_pins must be non-empty for pin '" + pin_name + "'");
636  }
637 
638  pin_cfgs.push_back({pin_name, identifier, bit_offset, bit_count, pin_ascending, std::move(input_pins)});
639  }
640 
641  std::unordered_map<std::string, LUTComponent::LUTOutputConfig> cfg_map;
642  for (const auto& pc : pin_cfgs)
643  {
644  cfg_map.emplace(pc.pin_name, LUTComponent::LUTOutputConfig(pc.identifier, pc.bit_offset, pc.bit_count, pc.is_ascending, pc.input_pins));
645  }
646 
647  return OK(LUTComponent::create(std::move(cfg_map)));
648  }
649 
650  // the flat format of earlier versions would result in a LUT without any function
651  return ERR("could not parse LUT configuration: missing or invalid output_pins");
652  }
653 
654  Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_ff_config(const rapidjson::Value& ff_config)
655  {
656  if (!ff_config.HasMember("state") || !ff_config["state"].IsString())
657  {
658  return ERR("could not parse flip-flop configuration: missing or invalid state identifier");
659  }
660  if (!ff_config.HasMember("neg_state") || !ff_config["neg_state"].IsString())
661  {
662  return ERR("could not parse flip-flop configuration: missing or invalid negated state identifier");
663  }
664  if (!ff_config.HasMember("next_state") || !ff_config["next_state"].IsString())
665  {
666  return ERR("could not parse flip-flop configuration: missing or invalid next state function");
667  }
668  if (!ff_config.HasMember("clocked_on") || !ff_config["clocked_on"].IsString())
669  {
670  return ERR("could not parse flip-flop configuration: missing or invalid clock function");
671  }
672 
673  std::unique_ptr<GateTypeComponent> init_component = nullptr;
674  if (ff_config.HasMember("data_category") && ff_config["data_category"].IsString())
675  {
676  std::vector<std::string> init_identifiers;
677  if (ff_config.HasMember("data_identifier") && ff_config["data_identifier"].IsString())
678  { // backward compatability
679  init_identifiers.push_back(ff_config["data_identifier"].GetString());
680  }
681  else
682  {
683  return ERR("could not parse flip-flop configuration: missing or invalid data identifier for flip-flop initialization");
684  }
685  init_component = InitComponent::create(ff_config["data_category"].GetString(), init_identifiers);
686  }
687  else if (ff_config.HasMember("data_identifier") && ff_config["data_identifier"].IsString())
688  {
689  return ERR("could not parse flip-flop configuration: missing or invalid data category for flip-flop initialization");
690  }
691 
692  std::string state_identifier = ff_config["state"].GetString();
693  std::string neg_state_identifier = ff_config["neg_state"].GetString();
694  std::unique_ptr<GateTypeComponent> state_component = StateComponent::create(std::move(init_component), state_identifier, neg_state_identifier);
695  assert(state_component != nullptr);
696 
697  BooleanFunction next_state_function;
698  if (auto res = BooleanFunction::from_string(ff_config["next_state"].GetString()); res.is_error())
699  {
700  return ERR("could not parse flip-flop configuration: failed parsing next state function from string");
701  }
702  else
703  {
704  next_state_function = res.get();
705  }
706  BooleanFunction clocked_on_function;
707  if (auto res = BooleanFunction::from_string(ff_config["clocked_on"].GetString()); res.is_error())
708  {
709  return ERR("could not parse flip-flop configuration: failed parsing clock function from string");
710  }
711  else
712  {
713  clocked_on_function = res.get();
714  }
715 
716  std::unique_ptr<GateTypeComponent> component = FFComponent::create(std::move(state_component), next_state_function, clocked_on_function);
717 
718  FFComponent* ff_component = component->convert_to<FFComponent>();
719  assert(ff_component != nullptr);
720 
721  if (ff_config.HasMember("clear_on") && ff_config["clear_on"].IsString())
722  {
723  if (auto res = BooleanFunction::from_string(ff_config["clear_on"].GetString()); res.is_error())
724  {
725  return ERR("could not parse flip-flop configuration: failed parsing asynchronous reset function from string");
726  }
727  else
728  {
729  ff_component->set_async_reset_function(res.get());
730  }
731  }
732 
733  if (ff_config.HasMember("preset_on") && ff_config["preset_on"].IsString())
734  {
735  if (auto res = BooleanFunction::from_string(ff_config["preset_on"].GetString()); res.is_error())
736  {
737  return ERR("could not parse flip-flop configuration: failed parsing asynchronous set function from string");
738  }
739  else
740  {
741  ff_component->set_async_set_function(res.get());
742  }
743  }
744 
745  bool has_state = ff_config.HasMember("state_clear_preset") && ff_config["state_clear_preset"].IsString();
746  bool has_neg_state = ff_config.HasMember("neg_state_clear_preset") && ff_config["neg_state_clear_preset"].IsString();
747 
748  if (has_state && has_neg_state)
749  {
750  AsyncSetResetBehavior cp1, cp2;
751 
752  if (const auto behav = enum_from_string<AsyncSetResetBehavior>(ff_config["state_clear_preset"].GetString(), AsyncSetResetBehavior::undef); behav != AsyncSetResetBehavior::undef)
753  {
754  cp1 = behav;
755  }
756  else
757  {
758  return ERR("could not parse flip-flop configuration: failed parsing state on concurrent asynchronous set and reset from string");
759  }
760 
761  if (const auto behav = enum_from_string<AsyncSetResetBehavior>(ff_config["neg_state_clear_preset"].GetString(), AsyncSetResetBehavior::undef); behav != AsyncSetResetBehavior::undef)
762  {
763  cp2 = behav;
764  }
765  else
766  {
767  return ERR("could not parse flip-flop configuration: failed parsing negated state on concurrent asynchronous set and reset from string");
768  }
769 
770  ff_component->set_async_set_reset_behavior(cp1, cp2);
771  }
772  else if ((has_state && !has_neg_state) || (!has_state && has_neg_state))
773  {
774  return ERR("could not parse flip-flop configuration: missing state or negated state on concurrent asynchronous set and reset");
775  }
776 
777  return OK(std::move(component));
778  }
779 
780  Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_latch_config(const rapidjson::Value& latch_config)
781  {
782  if (!latch_config.HasMember("state") || !latch_config["state"].IsString())
783  {
784  return ERR("could not parse latch configuration: missing or invalid state identifier");
785  }
786  if (!latch_config.HasMember("neg_state") || !latch_config["neg_state"].IsString())
787  {
788  return ERR("could not parse latch configuration: missing or invalid negated state identifier");
789  }
790 
791  std::string state_identifier = latch_config["state"].GetString();
792  std::string neg_state_identifier = latch_config["neg_state"].GetString();
793  std::unique_ptr<GateTypeComponent> state_component = StateComponent::create(nullptr, state_identifier, neg_state_identifier);
794  assert(state_component != nullptr);
795 
796  std::unique_ptr<GateTypeComponent> component = LatchComponent::create(std::move(state_component));
797  LatchComponent* latch_component = component->convert_to<LatchComponent>();
798  assert(latch_component != nullptr);
799 
800  if (latch_config.HasMember("data_in") && latch_config["data_in"].IsString() && latch_config.HasMember("enable_on") && latch_config["enable_on"].IsString())
801  {
802  if (auto res = BooleanFunction::from_string(latch_config["data_in"].GetString()); res.is_error())
803  {
804  return ERR("could not parse latch configuration: failed parsing data in function from string");
805  }
806  else
807  {
808  latch_component->set_data_in_function(res.get());
809  }
810 
811  if (auto res = BooleanFunction::from_string(latch_config["enable_on"].GetString()); res.is_error())
812  {
813  return ERR("could not parse latch configuration: failed parsing enable function from string");
814  }
815  else
816  {
817  latch_component->set_enable_function(res.get());
818  }
819  }
820  else if (latch_config.HasMember("data_in") && latch_config["data_in"].IsString())
821  {
822  return ERR("could not parse latch configuration: missing or invalid enable function");
823  }
824  else if (latch_config.HasMember("enable_on") && latch_config["enable_on"].IsString())
825  {
826  return ERR("could not parse latch configuration: missing or invalid data in function");
827  }
828 
829  if (latch_config.HasMember("clear_on") && latch_config["clear_on"].IsString())
830  {
831  if (auto res = BooleanFunction::from_string(latch_config["clear_on"].GetString()); res.is_error())
832  {
833  return ERR("could not parse latch configuration: failed parsing asynchronous reset function from string");
834  }
835  else
836  {
837  latch_component->set_async_reset_function(res.get());
838  }
839  }
840 
841  if (latch_config.HasMember("preset_on") && latch_config["preset_on"].IsString())
842  {
843  if (auto res = BooleanFunction::from_string(latch_config["preset_on"].GetString()); res.is_error())
844  {
845  return ERR("could not parse latch configuration: failed parsing asynchronous set function from string");
846  }
847  else
848  {
849  latch_component->set_async_set_function(res.get());
850  }
851  }
852 
853  bool has_state = latch_config.HasMember("state_clear_preset") && latch_config["state_clear_preset"].IsString();
854  bool has_neg_state = latch_config.HasMember("neg_state_clear_preset") && latch_config["neg_state_clear_preset"].IsString();
855 
856  if (has_state && has_neg_state)
857  {
858  AsyncSetResetBehavior cp1, cp2;
859 
860  if (const auto behav = enum_from_string<AsyncSetResetBehavior>(latch_config["state_clear_preset"].GetString(), AsyncSetResetBehavior::undef); behav != AsyncSetResetBehavior::undef)
861  {
862  cp1 = behav;
863  }
864  else
865  {
866  return ERR("could not parse latch configuration: failed parsing state on concurrent asynchronous set and reset from string");
867  }
868 
869  if (const auto behav = enum_from_string<AsyncSetResetBehavior>(latch_config["neg_state_clear_preset"].GetString(), AsyncSetResetBehavior::undef); behav != AsyncSetResetBehavior::undef)
870  {
871  cp2 = behav;
872  }
873  else
874  {
875  return ERR("could not parse latch configuration: failed parsing negated state on concurrent asynchronous set and reset from string");
876  }
877 
878  latch_component->set_async_set_reset_behavior(cp1, cp2);
879  }
880  else if ((has_state && !has_neg_state) || (!has_state && has_neg_state))
881  {
882  return ERR("could not parse latch configuration: missing state or negated state on concurrent asynchronous set and reset");
883  }
884 
885  return OK(std::move(component));
886  }
887 
888  Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_ram_config(const rapidjson::Value& ram_config)
889  {
890  if (!ram_config.HasMember("data_identifiers") || !ram_config["data_identifiers"].IsArray())
891  {
892  return ERR("could not parse RAM configuration: missing or invalid data_identifiers");
893  }
894 
895  std::vector<std::string> init_identifiers;
896  for (const auto& identifier : ram_config["data_identifiers"].GetArray())
897  {
898  init_identifiers.push_back(identifier.GetString());
899  }
900 
901  std::unique_ptr<GateTypeComponent> sub_component = nullptr;
902 
903  if (!ram_config.HasMember("bit_size") || !ram_config["bit_size"].IsUint())
904  {
905  return ERR("could not parse RAM configuration: missing or invalid bit size");
906  }
907 
908  if (!ram_config.HasMember("ram_ports") || !ram_config["ram_ports"].IsArray())
909  {
910  return ERR("could not parse RAM configuration: missing or invalid RAM ports");
911  }
912 
913  for (const auto& ram_port : ram_config["ram_ports"].GetArray())
914  {
915  if (!ram_port.HasMember("data_group") || !ram_port["data_group"].IsString())
916  {
917  return ERR("could not parse RAM configuration: missing or invalid data pin groups for RAM port");
918  }
919 
920  if (!ram_port.HasMember("address_group") || !ram_port["address_group"].IsString())
921  {
922  return ERR("could not parse RAM configuration: missing or invalid address pin groups for RAM port");
923  }
924 
925  if (!ram_port.HasMember("clocked_on") || !ram_port["clocked_on"].IsString())
926  {
927  return ERR("could not parse RAM configuration: missing or invalid clock function for RAM port");
928  }
929 
930  if (!ram_port.HasMember("enabled_on") || !ram_port["enabled_on"].IsString())
931  {
932  return ERR("could not parse RAM configuration: missing or invalid enable function for RAM port");
933  }
934 
935  if (!ram_port.HasMember("is_write") || !ram_port["is_write"].IsBool())
936  {
937  return ERR("could not parse RAM configuration: missing or invalid write flag for RAM port");
938  }
939 
940  BooleanFunction clocked_on_function;
941  if (auto res = BooleanFunction::from_string(ram_port["clocked_on"].GetString()); res.is_error())
942  {
943  return ERR("could not parse RAM configuration: failed parsing clock function from string");
944  }
945  else
946  {
947  clocked_on_function = res.get();
948  }
949 
950  BooleanFunction enabled_on_function;
951  if (auto res = BooleanFunction::from_string(ram_port["enabled_on"].GetString()); res.is_error())
952  {
953  return ERR("could not parse RAM configuration: failed parsing clock function from string");
954  }
955  else
956  {
957  enabled_on_function = res.get();
958  }
959 
960  sub_component = RAMPortComponent::create(
961  std::move(sub_component), ram_port["data_group"].GetString(), ram_port["address_group"].GetString(), clocked_on_function, enabled_on_function, ram_port["is_write"].GetBool());
962  }
963 
964  std::unique_ptr<GateTypeComponent> component = RAMComponent::create(std::move(sub_component), ram_config["bit_size"].GetUint(), init_identifiers);
965 
966  return OK(std::move(component));
967  }
968 } // namespace hal
static Result< BooleanFunction > from_string(const std::string &expression)
static std::unique_ptr< FFComponent > create(std::unique_ptr< GateTypeComponent > component, const BooleanFunction &next_state_bf, const BooleanFunction &clock_bf)
static void set_parse_status(ParseStatus status)
@ NoError
The gate library was parsed successfully.
@ NeedsUpgrade
The file format is outdated, it gets upgraded by parsing it again after request_upgrade.
Result< std::unique_ptr< GateLibrary > > parse(const std::filesystem::path &file_path) override
Definition: hgl_parser.cpp:41
static std::unique_ptr< InitComponent > create(const std::string &init_category, const std::vector< std::string > &init_identifiers)
static std::unique_ptr< LUTComponent > create(std::unordered_map< std::string, LUTOutputConfig > configs={})
static std::unique_ptr< LatchComponent > create(std::unique_ptr< GateTypeComponent > component)
static std::unique_ptr< RAMComponent > create(std::unique_ptr< GateTypeComponent > component, const u32 bit_size, const std::vector< std::string > &init_identifiers={})
static std::unique_ptr< RAMPortComponent > create(std::unique_ptr< GateTypeComponent > component, const std::string &data_group, const std::string &addr_group, const BooleanFunction &clock_bf, const BooleanFunction &enable_bf, bool is_write)
static std::unique_ptr< StateComponent > create(std::unique_ptr< GateTypeComponent > component, const std::string &state_identifier, const std::string &neg_state_identifier)
uint16_t u16
Definition: defines.h:40
uint32_t u32
Definition: defines.h:41
#define log_info(channel,...)
Definition: log.h:70
#define log_warning(channel,...)
Definition: log.h:76
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
#define ERR_APPEND(prev_error, message)
Definition: result.h:64
bool needs_upgrade(const rapidjson::Document &document)
Result< std::vector< std::string > > upgrade_document(rapidjson::Document &document)
Result< std::filesystem::path > replace_file(const rapidjson::Document &document, const std::filesystem::path &file_path)
Definition: defines.h:45
PinDirection
Definition: pin_direction.h:36
PinType
Definition: pin_type.h:36
std::vector< PinInformation > pins
bool ordered
u32 start_index
bool ascending
std::string name
std::string identifier
static Result< Parameter > Boolean(const std::string &name, const std::string &default_value)
Definition: parameter.cpp:319
static Result< Parameter > BitVector(const std::string &name, u16 size, const std::string &default_value)
Definition: parameter.cpp:340
static Result< Parameter > Integer(const std::string &name, const std::string &default_value)
Definition: parameter.cpp:390
static Result< Parameter > String(const std::string &name, const std::string &default_value)
Definition: parameter.cpp:411
static Result< Parameter > LogicVector(const std::string &name, u16 size, const std::string &default_value)
Definition: parameter.cpp:365
static Result< Parameter > Time(const std::string &name, const std::string &default_value)
Definition: parameter.cpp:448
static Result< Parameter > Enum(const std::string &name, const std::vector< std::string > &values, const std::string &default_value)
Definition: parameter.cpp:469
static Result< Parameter > Float(const std::string &name, const std::string &default_value)
Definition: parameter.cpp:427