3 #include "rapidjson/filewritestream.h"
4 #include "rapidjson/prettywriter.h"
9 #include <system_error>
19 const size_t MAX_LUT_INPUTS = 31;
21 const std::map<std::string, std::string> LEGACY_TYPE_RENAMES = {{
"buffer",
"c_buffer"}, {
"carry",
"c_carry"}, {
"mux",
"c_mux"}, {
"lut",
"c_lut"}};
24 using ParameterSizes = std::vector<std::pair<std::string, u64>>;
26 const rapidjson::Value* find_member(
const rapidjson::Value&
object,
const char*
name)
28 if (!
object.IsObject())
32 auto it =
object.FindMember(
name);
33 return (it !=
object.MemberEnd()) ? &it->value :
nullptr;
36 std::string get_string(
const rapidjson::Value&
object,
const char*
name)
38 const rapidjson::Value* value = find_member(
object,
name);
39 return (value !=
nullptr && value->IsString()) ? value->GetString() :
"";
42 u64 get_uint(
const rapidjson::Value&
object,
const char*
name)
44 const rapidjson::Value* value = find_member(
object,
name);
45 return (value !=
nullptr && value->IsUint64()) ? value->GetUint64() : 0;
48 rapidjson::Value make_string(
const std::string& str, rapidjson::Document::AllocatorType& allocator)
50 return rapidjson::Value(str.c_str(), str.length(), allocator);
53 void collect_pins(
const rapidjson::Value& cell, std::vector<std::string>& input_pins, std::vector<std::string>& lut_output_pins)
55 const rapidjson::Value* pin_groups = find_member(cell,
"pin_groups");
56 if (pin_groups ==
nullptr || !pin_groups->IsArray())
61 for (
const auto& pin_group : pin_groups->GetArray())
63 const std::string
direction = get_string(pin_group,
"direction");
64 const rapidjson::Value*
pins = find_member(pin_group,
"pins");
65 if (
pins ==
nullptr || !
pins->IsArray())
70 for (
const auto& pin :
pins->GetArray())
74 input_pins.push_back(get_string(pin,
"name"));
76 else if (
direction ==
"output" && get_string(pin,
"type") ==
"lut")
78 lut_output_pins.push_back(get_string(pin,
"name"));
84 void rename_legacy_types(rapidjson::Value& cell, rapidjson::Document::AllocatorType& allocator)
86 if (!cell.HasMember(
"types") || !cell[
"types"].IsArray())
91 for (
auto&
type : cell[
"types"].GetArray())
97 if (
auto it = LEGACY_TYPE_RENAMES.find(
type.GetString()); it != LEGACY_TYPE_RENAMES.end())
99 type = make_string(it->second, allocator);
104 bool has_legacy_lut_config(
const rapidjson::Value& cell)
106 const rapidjson::Value* lut_config = find_member(cell,
"lut_config");
107 return lut_config !=
nullptr && lut_config->IsObject() && !lut_config->HasMember(
"output_pins");
118 std::string upgrade_lut_config(rapidjson::Value& cell,
const std::string& cell_name, rapidjson::Document::AllocatorType& allocator, std::vector<std::string>& warnings)
120 if (!has_legacy_lut_config(cell))
126 rapidjson::Value& lut_config = cell[
"lut_config"];
127 const rapidjson::Value* data_identifier = find_member(lut_config,
"data_identifier");
128 if (data_identifier ==
nullptr || !data_identifier->IsString())
130 return "cell '" + cell_name +
"': LUT configuration has no data identifier";
133 std::vector<std::string> input_pins;
134 std::vector<std::string> lut_output_pins;
135 collect_pins(cell, input_pins, lut_output_pins);
137 if (lut_output_pins.empty())
139 return "cell '" + cell_name +
"': no output pin with type 'lut' found";
142 if (input_pins.empty())
144 return "cell '" + cell_name +
"': no input pins found";
147 if (input_pins.size() > MAX_LUT_INPUTS)
149 return "cell '" + cell_name +
"': " + std::to_string(input_pins.size()) +
" input pins exceed the supported maximum of " + std::to_string(MAX_LUT_INPUTS) +
" for a LUT";
152 if (lut_output_pins.size() > 1)
154 warnings.push_back(
"cell '" + cell_name +
"': each of its " + std::to_string(lut_output_pins.size())
155 +
" LUT output pins was migrated to depend on all input pins and the whole INIT value, as before. If an output uses only part of "
156 "them, e.g., output O5 of a Xilinx LUT6_2, adjust its 'input_pins' and 'bit_count' manually.");
159 const rapidjson::Value* bit_order = find_member(lut_config,
"bit_order");
160 const bool is_ascending = (bit_order ==
nullptr) || (bit_order->IsString() && std::string(bit_order->GetString()) ==
"ascending");
162 rapidjson::Value output_pins(rapidjson::kArrayType);
163 for (
const auto& lut_output_pin : lut_output_pins)
165 rapidjson::Value inputs(rapidjson::kArrayType);
166 for (
const auto& pin : input_pins)
168 inputs.PushBack(make_string(pin, allocator), allocator);
171 rapidjson::Value output_pin(rapidjson::kObjectType);
172 output_pin.AddMember(
"pin", make_string(lut_output_pin, allocator), allocator);
173 output_pin.AddMember(
"data_identifier", rapidjson::Value(*data_identifier, allocator), allocator);
174 output_pin.AddMember(
"bit_offset", 0, allocator);
175 output_pin.AddMember(
"bit_count",
u64(1) << input_pins.size(), allocator);
176 output_pin.AddMember(
"input_pins", inputs, allocator);
177 output_pin.AddMember(
"is_ascending", is_ascending, allocator);
178 output_pins.PushBack(output_pin, allocator);
181 rapidjson::Value new_lut_config(rapidjson::kObjectType);
182 if (bit_order !=
nullptr)
184 new_lut_config.AddMember(
"bit_order", rapidjson::Value(*bit_order, allocator), allocator);
186 if (
const rapidjson::Value* data_category = find_member(lut_config,
"data_category"); data_category !=
nullptr)
188 new_lut_config.AddMember(
"data_category", rapidjson::Value(*data_category, allocator), allocator);
190 new_lut_config.AddMember(
"output_pins", output_pins, allocator);
193 lut_config = new_lut_config;
197 u64* find_size(ParameterSizes& sizes,
const std::string&
identifier)
199 for (
auto& [ident,
size] : sizes)
211 if (
u64* existing = find_size(sizes,
identifier); existing !=
nullptr)
221 void add_parameters(rapidjson::Value& cell, rapidjson::Document::AllocatorType& allocator)
223 if (cell.HasMember(
"parameters"))
228 ParameterSizes sizes;
230 if (
const rapidjson::Value* lut_config = find_member(cell,
"lut_config"); lut_config !=
nullptr)
232 if (
const rapidjson::Value* output_pins = find_member(*lut_config,
"output_pins"); output_pins !=
nullptr && output_pins->IsArray())
234 for (
const auto& output_pin : output_pins->GetArray())
236 const std::string
identifier = get_string(output_pin,
"data_identifier");
237 const u64 required = get_uint(output_pin,
"bit_offset") + get_uint(output_pin,
"bit_count");
239 set_size(sizes,
identifier, (existing !=
nullptr) ? std::max(*existing, required) : required);
245 for (
const char* config_name : {
"ff_config",
"latch_config"})
247 if (
const rapidjson::Value* config = find_member(cell, config_name); config !=
nullptr && config->HasMember(
"data_identifier"))
249 const std::string
identifier = get_string(*config,
"data_identifier");
257 if (
const rapidjson::Value* ram_config = find_member(cell,
"ram_config"); ram_config !=
nullptr)
259 const rapidjson::Value* identifiers = find_member(*ram_config,
"data_identifiers");
260 if (identifiers !=
nullptr && identifiers->IsArray() && !identifiers->Empty() && ram_config->HasMember(
"bit_size"))
262 const u64 size = get_uint(*ram_config,
"bit_size") / identifiers->Size();
263 for (
const auto&
identifier : identifiers->GetArray())
275 rapidjson::Value parameters(rapidjson::kArrayType);
278 rapidjson::Value parameter(rapidjson::kObjectType);
279 parameter.AddMember(
"name", make_string(
identifier, allocator), allocator);
280 parameter.AddMember(
"type",
"bit_vector", allocator);
281 parameter.AddMember(
"size",
size, allocator);
282 parameter.AddMember(
"default",
"0x0", allocator);
283 parameters.PushBack(parameter, allocator);
285 cell.AddMember(
"parameters", parameters, allocator);
291 if (!document.IsObject())
293 return ERR(
"document is not a JSON object");
295 if (!document.HasMember(
"version"))
299 if (!document[
"version"].IsUint())
301 return ERR(
"invalid version entry");
303 return OK(document[
"version"].GetUint());
309 if (version.is_error())
317 if (version.get() >
TARGET_VERSION || !document.HasMember(
"cells") || !document[
"cells"].IsArray())
323 const auto& cells = document[
"cells"].GetArray();
324 return std::any_of(cells.begin(), cells.end(), [](
const rapidjson::Value& cell) { return has_legacy_lut_config(cell); });
330 if (version_res.is_error())
332 return ERR_APPEND(version_res.get_error(),
"could not upgrade HGL document");
334 const u32 version = version_res.get();
336 std::vector<std::string> warnings;
344 return ERR(
"could not upgrade HGL document: version " + std::to_string(version) +
" uses a legacy pin format that cannot be upgraded automatically");
347 rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
348 std::vector<std::string> errors;
350 if (document.HasMember(
"cells") && document[
"cells"].IsArray())
352 for (
auto& cell : document[
"cells"].GetArray())
354 if (!cell.IsObject())
359 const std::string cell_name = cell.HasMember(
"name") && cell[
"name"].IsString() ? cell[
"name"].GetString() :
"(unknown)";
361 rename_legacy_types(cell, allocator);
362 if (std::string
error = upgrade_lut_config(cell, cell_name, allocator, warnings); !
error.empty())
364 errors.push_back(
error);
367 add_parameters(cell, allocator);
374 std::string message =
"could not upgrade HGL document, the following cells have to be migrated manually: ";
375 for (
size_t i = 0; i < errors.size(); ++i)
377 message += (i == 0 ?
"" :
"; ") + errors[i];
382 if (document.HasMember(
"version"))
396 std::filesystem::path backup_path = file_path;
397 backup_path.replace_extension(
".json");
401 for (
u32 i = 1; std::filesystem::exists(backup_path, ec); ++i)
403 backup_path = file_path;
404 backup_path.replace_extension(
"." + std::to_string(i) +
".json");
414 if (std::filesystem::exists(backup_path, ec))
416 return ERR(
"could not upgrade HGL file '" + file_path.string() +
"': cannot keep the original as '" + backup_path.string() +
"', that file already exists");
419 std::filesystem::rename(file_path, backup_path, ec);
422 return ERR(
"could not upgrade HGL file '" + file_path.string() +
"': unable to rename it to '" + backup_path.string() +
"': " + ec.message());
425 FILE* fp = fopen(file_path.string().c_str(),
"w");
426 bool written =
false;
430 rapidjson::FileWriteStream os(fp, buffer,
sizeof(buffer));
431 rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
432 written = document.Accept(writer);
434 written = (fclose(fp) == 0) && written;
441 std::filesystem::rename(backup_path, file_path, ec);
442 return ERR(
"could not upgrade HGL file '" + file_path.string() +
"': unable to write the upgraded file");
445 return OK(backup_path);
#define ERR_APPEND(prev_error, message)
void remove(std::filesystem::path file_path)
bool needs_upgrade(const rapidjson::Document &document)
std::filesystem::path get_backup_path(const std::filesystem::path &file_path)
Result< u32 > get_version(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::error_code error_code
std::vector< PinInformation > pins