15 #include "rapidjson/filereadstream.h"
20 bool HGLParser::do_not_upgrade_gatelib(
const rapidjson::Document& document)
const
22 if (!document.IsObject())
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);
27 if (!document.HasMember(
"version"))
29 log_warning(
"gate_library_parser",
"'{}' cannot be upgraded to HGL format version {} since version tag is missing.", m_path.string(), HGL_FORMAT_VERSION);
32 if (!document[
"version"].IsUint())
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);
45 FILE* fp = fopen(file_path.string().c_str(),
"r");
48 return ERR(
"could not parse HGL file '" + m_path.string() +
"' : unable to open file");
52 rapidjson::FileReadStream is(fp, buffer,
sizeof(buffer));
53 rapidjson::Document document;
54 document.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is);
57 if (document.HasParseError())
59 return ERR(
"could not parse HGL file '" + m_path.string() +
"': failed parsing JSON format");
62 if (
auto res = upgrade_outdated_file(document); res.is_error())
64 return ERR_APPEND(res.get_error(),
"could not parse HGL file '" + m_path.string() +
"': failed to upgrade outdated file format");
67 if (
auto res = parse_gate_library(document); res.is_error())
69 return ERR_APPEND(res.get_error(),
"could not parse HGL file '" + m_path.string() +
"': failed to parse gate library");
74 return OK(std::move(m_gate_lib));
79 if (do_not_upgrade_gatelib(document))
85 const u32 version = document[
"version"].GetUint();
91 return ERR(res.get_error());
93 for (
const std::string& warning : res.get())
95 log_warning(
"gate_library_parser",
"upgrade of '{}' to HGL format version {}: {}", m_path.string(), HGL_FORMAT_VERSION, warning);
102 if (version < HGL_FORMAT_VERSION)
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");
106 return ERR(
"HGL file contains LUT configurations in the format of version 5 or earlier, the file needs to be upgraded");
110 if (res_replace.is_error())
112 return ERR(res_replace.get_error());
115 if (version < HGL_FORMAT_VERSION)
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());
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());
126 Result<std::monostate> HGLParser::parse_gate_library(
const rapidjson::Document& document)
128 if (!document.HasMember(
"version") || !document[
"version"].IsUint() || document[
"version"].GetUint() < HGL_FORMAT_VERSION)
130 return ERR(
"could not parse gate library: HGL file format version is outdated (requires version " + std::to_string(HGL_FORMAT_VERSION) +
")");
133 if (!document.HasMember(
"library"))
135 return ERR(
"could not parse gate library: missing 'library' node");
138 const std::string gate_lib_name = document[
"library"].GetString();
139 m_gate_lib = std::make_unique<GateLibrary>(m_path, gate_lib_name);
141 if (document.HasMember(
"gate_locations") && document[
"gate_locations"].IsObject())
143 auto gate_locs = document[
"gate_locations"].GetObject();
145 if (!gate_locs.HasMember(
"data_category") || !gate_locs[
"data_category"].IsString())
147 return ERR(
"could not parse gate library '" + gate_lib_name +
"': missing 'data_category' entry for gate locations");
150 if (!gate_locs.HasMember(
"data_x_identifier") || !gate_locs[
"data_x_identifier"].IsString())
152 return ERR(
"could not parse gate library '" + gate_lib_name +
"': missing 'data_x_identifier' entry for gate locations");
155 if (!gate_locs.HasMember(
"data_y_identifier") || !gate_locs[
"data_y_identifier"].IsString())
157 return ERR(
"could not parse gate library '" + gate_lib_name +
"': missing 'data_y_identifier' entry for gate locations");
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());
164 if (!document.HasMember(
"cells"))
166 return ERR(
"could not parse gate library '" + gate_lib_name +
"': missing 'cells' node");
169 for (
const auto& gate_type : document[
"cells"].GetArray())
171 if (
auto res = parse_gate_type(gate_type); res.is_error())
173 return ERR_APPEND(res.get_error(),
"could not parse gate library '" + gate_lib_name +
"': failed parsing gate type");
180 Result<std::monostate> HGLParser::parse_gate_type(
const rapidjson::Value& gate_type)
182 if (!gate_type.HasMember(
"name") || !gate_type[
"name"].IsString())
184 return ERR(
"could not parse gate type: missing or invalid name");
187 const std::string
name = gate_type[
"name"].GetString();
188 std::set<GateTypeProperty> properties;
189 if (gate_type.HasMember(
"types") && gate_type[
"types"].IsArray())
191 for (
const auto& base_type : gate_type[
"types"].GetArray())
193 const std::string property_str = base_type.GetString();
197 GateTypeProperty property = enum_from_string<GateTypeProperty>(property_str);
198 properties.insert(property);
200 catch (
const std::runtime_error&)
202 return ERR(
"could not parse gate type '" +
name +
"': invalid property '" + base_type.GetString() +
"'");
211 std::unique_ptr<GateTypeComponent> parent_component =
nullptr;
212 if (gate_type.HasMember(
"lut_config") && gate_type[
"lut_config"].IsObject())
214 const auto& lut_cfg = gate_type[
"lut_config"];
215 if (
auto res = parse_lut_config(lut_cfg); res.is_error())
217 return ERR_APPEND(res.get_error(),
"could not parse gate type '" +
name +
"': failed parsing LUT configuration");
221 parent_component = res.get();
224 else if (gate_type.HasMember(
"ff_config") && gate_type[
"ff_config"].IsObject())
226 if (
auto res = parse_ff_config(gate_type[
"ff_config"]); res.is_error())
228 return ERR_APPEND(res.get_error(),
"could not parse gate type '" +
name +
"': failed parsing FF configuration");
232 parent_component = res.get();
235 else if (gate_type.HasMember(
"latch_config") && gate_type[
"latch_config"].IsObject())
237 if (
auto res = parse_latch_config(gate_type[
"latch_config"]); res.is_error())
239 return ERR_APPEND(res.get_error(),
"could not parse gate type '" +
name +
"': failed parsing latch configuration");
243 parent_component = res.get();
246 else if (gate_type.HasMember(
"ram_config") && gate_type[
"ram_config"].IsObject())
248 if (
auto res = parse_ram_config(gate_type[
"ram_config"]); res.is_error())
250 return ERR_APPEND(res.get_error(),
"could not parse gate type '" +
name +
"': failed parsing RAM configuration");
254 parent_component = res.get();
258 GateType* gt = m_gate_lib->create_gate_type(
name, properties, std::move(parent_component));
261 return ERR(
"could not parse gate type '" +
name +
"': failed to create gate type");
266 std::map<std::string, u16> var_sizes;
267 if (gate_type.HasMember(
"parameters") && gate_type[
"parameters"].IsArray())
269 for (
const auto& param_val : gate_type[
"parameters"].GetArray())
271 if (!param_val.HasMember(
"name") || !param_val[
"name"].IsString())
273 return ERR(
"could not parse parameter for gate type '" +
name +
"': missing or invalid name");
275 if (!param_val.HasMember(
"type") || !param_val[
"type"].IsString())
277 return ERR(
"could not parse parameter for gate type '" +
name +
"': missing or invalid type");
279 if (!param_val.HasMember(
"default") || !param_val[
"default"].IsString())
281 return ERR(
"could not parse parameter for gate type '" +
name +
"': missing or invalid default value");
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();
290 if (ptype ==
"boolean")
293 if (param_res.is_error())
295 return ERR(param_res.get_error());
297 param = param_res.get();
299 else if (ptype ==
"bit_vector")
301 if (!param_val.HasMember(
"size") || !param_val[
"size"].IsUint())
303 return ERR(
"could not parse parameter '" + pname +
"' for gate type '" +
name +
"': bit-vector parameter requires a 'size' field");
305 const u32 sz = param_val[
"size"].GetUint();
308 if (param_res.is_error())
310 return ERR(param_res.get_error());
312 param = param_res.get();
314 else if (ptype ==
"logic_vector")
316 if (!param_val.HasMember(
"size") || !param_val[
"size"].IsUint())
318 return ERR(
"could not parse parameter '" + pname +
"' for gate type '" +
name +
"': logic-vector parameter requires a 'size' field");
320 const u32 sz = param_val[
"size"].GetUint();
323 if (param_res.is_error())
325 return ERR(param_res.get_error());
327 param = param_res.get();
329 else if (ptype ==
"integer")
332 if (param_res.is_error())
334 return ERR(param_res.get_error());
336 param = param_res.get();
338 else if (ptype ==
"string")
341 if (param_res.is_error())
343 return ERR(param_res.get_error());
345 param = param_res.get();
347 else if (ptype ==
"float")
350 if (param_res.is_error())
352 return ERR(param_res.get_error());
354 param = param_res.get();
356 else if (ptype ==
"time")
359 if (param_res.is_error())
361 return ERR(param_res.get_error());
363 param = param_res.get();
365 else if (ptype ==
"enum")
367 if (!param_val.HasMember(
"values") || !param_val[
"values"].IsArray())
369 return ERR(
"could not parse parameter '" + pname +
"' for gate type '" +
name +
"': enum parameter requires a 'values' array");
371 std::vector<std::string> values;
372 for (
const auto& v : param_val[
"values"].GetArray())
376 return ERR(
"could not parse parameter '" + pname +
"' for gate type '" +
name +
"': enum 'values' must be strings");
378 values.emplace_back(v.GetString());
382 if (param_res.is_error())
384 return ERR(param_res.get_error());
386 param = param_res.get();
390 return ERR(
"could not parse parameter '" + pname +
"' for gate type '" +
name +
"': unknown type '" + ptype +
"'");
393 if (
auto res = gt->add_parameter(param); res.is_error())
395 return ERR_APPEND(res.get_error(),
"could not parse parameters for gate type '" +
name +
"'");
398 var_sizes[param.get_name()] = param.get_size();
402 if (gate_type.HasMember(
"pin_groups") && gate_type[
"pin_groups"].IsArray())
404 for (
const auto& pg_val : gate_type[
"pin_groups"].GetArray())
406 if (!pg_val.HasMember(
"name") || !pg_val[
"name"].IsString())
408 return ERR(
"could not parse pin group: missing or invalid name");
410 std::string pg_name = pg_val[
"name"].GetString();
412 if (!pg_val.HasMember(
"direction") || !pg_val[
"direction"].IsString())
414 return ERR(
"could not parse pin group '" + pg_name +
"': missing or invalid pin direction");
417 std::string pg_direction_str = pg_val[
"direction"].GetString();
420 pg_direction = enum_from_string<PinDirection>(pg_direction_str);
422 catch (
const std::runtime_error&)
424 return ERR(
"could not parse pin '" + pg_name +
"': invalid pin direction '" + pg_direction_str +
"'");
427 if (!pg_val.HasMember(
"type") || !pg_val[
"type"].IsString())
429 return ERR(
"could not parse pin group '" + pg_name +
"': missing or invalid pin type");
432 std::string pg_type_str = pg_val[
"type"].GetString();
435 pg_type = enum_from_string<PinType>(pg_type_str);
437 catch (
const std::runtime_error&)
439 return ERR(
"could not parse pin group '" + pg_name +
"': invalid pin type '" + pg_type_str +
"'");
442 if (!pg_val.HasMember(
"ascending") || !pg_val[
"ascending"].IsBool())
444 return ERR(
"could not parse pin group '" + pg_name +
"': missing or ascending property");
446 bool ascending = pg_val[
"ascending"].GetBool();
449 if (pg_val.HasMember(
"ordered") && pg_val[
"ordered"].IsBool())
451 ordered = pg_val[
"ordered"].GetBool();
454 if (!pg_val.HasMember(
"start_index") || !pg_val[
"start_index"].IsUint())
456 return ERR(
"could not parse pin group '" + pg_name +
"': missing or start index");
460 if (pg_val.HasMember(
"pins") && pg_val[
"pins"].IsArray())
462 std::vector<GatePin*>
pins;
464 for (
const auto& p_val : pg_val[
"pins"].GetArray())
466 if (!p_val.HasMember(
"name") || !p_val[
"name"].IsString())
468 return ERR(
"could not parse pin: missing or invalid name");
470 std::string p_name = p_val[
"name"].GetString();
472 if (!p_val.HasMember(
"direction") || !p_val[
"direction"].IsString())
474 return ERR(
"could not parse pin '" + p_name +
"': missing or invalid pin direction");
477 std::string p_direction_str = p_val[
"direction"].GetString();
480 p_direction = enum_from_string<PinDirection>(p_direction_str);
482 catch (
const std::runtime_error&)
484 return ERR(
"could not parse pin '" + p_name +
"': invalid pin direction '" + p_direction_str +
"'");
488 if (p_val.HasMember(
"type") && p_val[
"type"].IsString())
490 std::string p_type_str = p_val[
"type"].GetString();
493 p_type = enum_from_string<PinType>(p_type_str);
495 catch (
const std::runtime_error&)
497 return ERR(
"could not parse pin '" + p_name +
"': invalid pin type '" + p_type_str +
"'");
505 auto p_res = gt->create_pin(p_name, p_direction, p_type,
false);
506 if (p_res.is_error())
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 +
"'");
510 pins.push_back(p_res.get());
512 if (p_val.HasMember(
"function") && p_val[
"function"].IsString())
517 "could not parse gate type '" +
name +
"' with ID " + std::to_string(gt->get_id()) +
": failed parsing Boolean function with name '" + p_name
522 gt->add_boolean_function(p_name, res.get());
526 if (p_val.HasMember(
"x_function") && p_val[
"x_function"].IsString())
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");
536 gt->add_boolean_function(p_name +
"_undefined", res.get());
540 if (p_val.HasMember(
"z_function") && p_val[
"z_function"].IsString())
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");
550 gt->add_boolean_function(p_name +
"_tristate", res.get());
556 if (pg_res.is_error())
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 +
"'");
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 +
"'");
571 Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_lut_config(
const rapidjson::Value& lut_config)
574 if (lut_config.HasMember(
"output_pins") && lut_config[
"output_pins"].IsArray())
578 std::string pin_name;
583 std::vector<std::string> input_pins;
585 std::vector<PinCfg> pin_cfgs;
587 for (
const auto& entry : lut_config[
"output_pins"].GetArray())
589 if (!entry.HasMember(
"pin") || !entry[
"pin"].IsString())
591 return ERR(
"could not parse LUT configuration: missing or invalid pin name in output_pins entry");
593 const std::string pin_name = entry[
"pin"].GetString();
595 if (!entry.HasMember(
"data_identifier") || !entry[
"data_identifier"].IsString())
597 return ERR(
"could not parse LUT configuration: missing or invalid data_identifier for pin '" + pin_name +
"'");
599 const std::string
identifier = entry[
"data_identifier"].GetString();
601 if (!entry.HasMember(
"bit_offset") || !entry[
"bit_offset"].IsUint())
603 return ERR(
"could not parse LUT configuration: missing or invalid bit_offset for pin '" + pin_name +
"'");
605 const u32 bit_offset = entry[
"bit_offset"].GetUint();
607 if (!entry.HasMember(
"bit_count") || !entry[
"bit_count"].IsUint())
609 return ERR(
"could not parse LUT configuration: missing or invalid bit_count for pin '" + pin_name +
"'");
611 const u32 bit_count = entry[
"bit_count"].GetUint();
614 if (!entry.HasMember(
"is_ascending") || !entry[
"is_ascending"].IsBool())
616 return ERR(
"could not parse LUT configuration: missing or invalid is_ascending for pin '" + pin_name +
"'");
618 bool pin_ascending = entry[
"is_ascending"].GetBool();
620 if (!entry.HasMember(
"input_pins") || !entry[
"input_pins"].IsArray())
622 return ERR(
"could not parse LUT configuration: missing or invalid input_pins for pin '" + pin_name +
"'");
624 std::vector<std::string> input_pins;
625 for (
const auto& pv : entry[
"input_pins"].GetArray())
629 return ERR(
"could not parse LUT configuration: non-string entry in input_pins for pin '" + pin_name +
"'");
631 input_pins.emplace_back(pv.GetString());
633 if (input_pins.empty())
635 return ERR(
"could not parse LUT configuration: input_pins must be non-empty for pin '" + pin_name +
"'");
638 pin_cfgs.push_back({pin_name,
identifier, bit_offset, bit_count, pin_ascending, std::move(input_pins)});
641 std::unordered_map<std::string, LUTComponent::LUTOutputConfig> cfg_map;
642 for (
const auto& pc : pin_cfgs)
644 cfg_map.emplace(pc.pin_name, LUTComponent::LUTOutputConfig(pc.identifier, pc.bit_offset, pc.bit_count, pc.is_ascending, pc.input_pins));
651 return ERR(
"could not parse LUT configuration: missing or invalid output_pins");
654 Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_ff_config(
const rapidjson::Value& ff_config)
656 if (!ff_config.HasMember(
"state") || !ff_config[
"state"].IsString())
658 return ERR(
"could not parse flip-flop configuration: missing or invalid state identifier");
660 if (!ff_config.HasMember(
"neg_state") || !ff_config[
"neg_state"].IsString())
662 return ERR(
"could not parse flip-flop configuration: missing or invalid negated state identifier");
664 if (!ff_config.HasMember(
"next_state") || !ff_config[
"next_state"].IsString())
666 return ERR(
"could not parse flip-flop configuration: missing or invalid next state function");
668 if (!ff_config.HasMember(
"clocked_on") || !ff_config[
"clocked_on"].IsString())
670 return ERR(
"could not parse flip-flop configuration: missing or invalid clock function");
673 std::unique_ptr<GateTypeComponent> init_component =
nullptr;
674 if (ff_config.HasMember(
"data_category") && ff_config[
"data_category"].IsString())
676 std::vector<std::string> init_identifiers;
677 if (ff_config.HasMember(
"data_identifier") && ff_config[
"data_identifier"].IsString())
679 init_identifiers.push_back(ff_config[
"data_identifier"].GetString());
683 return ERR(
"could not parse flip-flop configuration: missing or invalid data identifier for flip-flop initialization");
687 else if (ff_config.HasMember(
"data_identifier") && ff_config[
"data_identifier"].IsString())
689 return ERR(
"could not parse flip-flop configuration: missing or invalid data category for flip-flop initialization");
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);
697 BooleanFunction next_state_function;
700 return ERR(
"could not parse flip-flop configuration: failed parsing next state function from string");
704 next_state_function = res.get();
706 BooleanFunction clocked_on_function;
709 return ERR(
"could not parse flip-flop configuration: failed parsing clock function from string");
713 clocked_on_function = res.get();
716 std::unique_ptr<GateTypeComponent> component =
FFComponent::create(std::move(state_component), next_state_function, clocked_on_function);
718 FFComponent* ff_component = component->convert_to<FFComponent>();
719 assert(ff_component !=
nullptr);
721 if (ff_config.HasMember(
"clear_on") && ff_config[
"clear_on"].IsString())
725 return ERR(
"could not parse flip-flop configuration: failed parsing asynchronous reset function from string");
729 ff_component->set_async_reset_function(res.get());
733 if (ff_config.HasMember(
"preset_on") && ff_config[
"preset_on"].IsString())
737 return ERR(
"could not parse flip-flop configuration: failed parsing asynchronous set function from string");
741 ff_component->set_async_set_function(res.get());
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();
748 if (has_state && has_neg_state)
758 return ERR(
"could not parse flip-flop configuration: failed parsing state on concurrent asynchronous set and reset from string");
767 return ERR(
"could not parse flip-flop configuration: failed parsing negated state on concurrent asynchronous set and reset from string");
770 ff_component->set_async_set_reset_behavior(cp1, cp2);
772 else if ((has_state && !has_neg_state) || (!has_state && has_neg_state))
774 return ERR(
"could not parse flip-flop configuration: missing state or negated state on concurrent asynchronous set and reset");
777 return OK(std::move(component));
780 Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_latch_config(
const rapidjson::Value& latch_config)
782 if (!latch_config.HasMember(
"state") || !latch_config[
"state"].IsString())
784 return ERR(
"could not parse latch configuration: missing or invalid state identifier");
786 if (!latch_config.HasMember(
"neg_state") || !latch_config[
"neg_state"].IsString())
788 return ERR(
"could not parse latch configuration: missing or invalid negated state identifier");
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);
797 LatchComponent* latch_component = component->convert_to<LatchComponent>();
798 assert(latch_component !=
nullptr);
800 if (latch_config.HasMember(
"data_in") && latch_config[
"data_in"].IsString() && latch_config.HasMember(
"enable_on") && latch_config[
"enable_on"].IsString())
804 return ERR(
"could not parse latch configuration: failed parsing data in function from string");
808 latch_component->set_data_in_function(res.get());
813 return ERR(
"could not parse latch configuration: failed parsing enable function from string");
817 latch_component->set_enable_function(res.get());
820 else if (latch_config.HasMember(
"data_in") && latch_config[
"data_in"].IsString())
822 return ERR(
"could not parse latch configuration: missing or invalid enable function");
824 else if (latch_config.HasMember(
"enable_on") && latch_config[
"enable_on"].IsString())
826 return ERR(
"could not parse latch configuration: missing or invalid data in function");
829 if (latch_config.HasMember(
"clear_on") && latch_config[
"clear_on"].IsString())
833 return ERR(
"could not parse latch configuration: failed parsing asynchronous reset function from string");
837 latch_component->set_async_reset_function(res.get());
841 if (latch_config.HasMember(
"preset_on") && latch_config[
"preset_on"].IsString())
845 return ERR(
"could not parse latch configuration: failed parsing asynchronous set function from string");
849 latch_component->set_async_set_function(res.get());
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();
856 if (has_state && has_neg_state)
866 return ERR(
"could not parse latch configuration: failed parsing state on concurrent asynchronous set and reset from string");
875 return ERR(
"could not parse latch configuration: failed parsing negated state on concurrent asynchronous set and reset from string");
878 latch_component->set_async_set_reset_behavior(cp1, cp2);
880 else if ((has_state && !has_neg_state) || (!has_state && has_neg_state))
882 return ERR(
"could not parse latch configuration: missing state or negated state on concurrent asynchronous set and reset");
885 return OK(std::move(component));
888 Result<std::unique_ptr<GateTypeComponent>> HGLParser::parse_ram_config(
const rapidjson::Value& ram_config)
890 if (!ram_config.HasMember(
"data_identifiers") || !ram_config[
"data_identifiers"].IsArray())
892 return ERR(
"could not parse RAM configuration: missing or invalid data_identifiers");
895 std::vector<std::string> init_identifiers;
896 for (
const auto&
identifier : ram_config[
"data_identifiers"].GetArray())
898 init_identifiers.push_back(
identifier.GetString());
901 std::unique_ptr<GateTypeComponent> sub_component =
nullptr;
903 if (!ram_config.HasMember(
"bit_size") || !ram_config[
"bit_size"].IsUint())
905 return ERR(
"could not parse RAM configuration: missing or invalid bit size");
908 if (!ram_config.HasMember(
"ram_ports") || !ram_config[
"ram_ports"].IsArray())
910 return ERR(
"could not parse RAM configuration: missing or invalid RAM ports");
913 for (
const auto& ram_port : ram_config[
"ram_ports"].GetArray())
915 if (!ram_port.HasMember(
"data_group") || !ram_port[
"data_group"].IsString())
917 return ERR(
"could not parse RAM configuration: missing or invalid data pin groups for RAM port");
920 if (!ram_port.HasMember(
"address_group") || !ram_port[
"address_group"].IsString())
922 return ERR(
"could not parse RAM configuration: missing or invalid address pin groups for RAM port");
925 if (!ram_port.HasMember(
"clocked_on") || !ram_port[
"clocked_on"].IsString())
927 return ERR(
"could not parse RAM configuration: missing or invalid clock function for RAM port");
930 if (!ram_port.HasMember(
"enabled_on") || !ram_port[
"enabled_on"].IsString())
932 return ERR(
"could not parse RAM configuration: missing or invalid enable function for RAM port");
935 if (!ram_port.HasMember(
"is_write") || !ram_port[
"is_write"].IsBool())
937 return ERR(
"could not parse RAM configuration: missing or invalid write flag for RAM port");
940 BooleanFunction clocked_on_function;
943 return ERR(
"could not parse RAM configuration: failed parsing clock function from string");
947 clocked_on_function = res.get();
950 BooleanFunction enabled_on_function;
953 return ERR(
"could not parse RAM configuration: failed parsing clock function from string");
957 enabled_on_function = res.get();
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());
964 std::unique_ptr<GateTypeComponent> component =
RAMComponent::create(std::move(sub_component), ram_config[
"bit_size"].GetUint(), init_identifiers);
966 return OK(std::move(component));
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 bool is_upgrade_requested()
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
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)
#define log_info(channel,...)
#define log_warning(channel,...)
#define ERR_APPEND(prev_error, message)
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)
std::vector< PinInformation > pins
static Result< Parameter > Boolean(const std::string &name, const std::string &default_value)
static Result< Parameter > BitVector(const std::string &name, u16 size, const std::string &default_value)
static Result< Parameter > Integer(const std::string &name, const std::string &default_value)
static Result< Parameter > String(const std::string &name, const std::string &default_value)
static Result< Parameter > LogicVector(const std::string &name, u16 size, const std::string &default_value)
static Result< Parameter > Time(const std::string &name, const std::string &default_value)
static Result< Parameter > Enum(const std::string &name, const std::vector< std::string > &values, const std::string &default_value)
static Result< Parameter > Float(const std::string &name, const std::string &default_value)