HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
genlib_writer.cpp
Go to the documentation of this file.
2 
5 
6 #include <clocale>
7 #include <fstream>
8 
9 namespace hal
10 {
11  namespace
12  {
13  std::string double_to_string(double d)
14  {
15  std::ostringstream oss;
16  oss << std::fixed << std::setprecision(4) << d;
17  return oss.str();
18  }
19  } // namespace
20 
21  bool GenlibWriter::write(const GateLibrary* gate_lib, const std::filesystem::path& file_path)
22  {
23  std::setlocale(LC_NUMERIC, "en_US");
24 
25  // set name
26  std::string file_str = "#" + gate_lib->get_name() + "\n";
27 
28  // TODO since we are using the standard gate library writer interface we can not pass any other parameters,
29  // in the future it would be nice if we could for example pass a list of gate types that are being written.
30 
31  // process combinational gates
32  for (const auto& [name, gt] : gate_lib->get_gate_types([](const GateType* gt) { return gt->has_property(GateTypeProperty::combinational); }))
33  {
34  if (gt->get_boolean_functions().empty())
35  {
36  log_warning("GenlibWriter", "Skipping gate type {} because eventhough it is marked as combinational it does not have any boolean functions.", name);
37  continue;
38  }
39 
40  if (gt->get_boolean_functions().size() > 1)
41  {
42  log_warning("GenlibWriter",
43  "Skipping gate type {} because it contains {} Boolean functions, but the genlib format only supports single output cells.",
44  name,
45  gt->get_boolean_functions().size());
46  continue;
47  }
48 
49  // TODO make this read out the area from the gate library (currently not implemented)
50  u32 base_value = gt->get_input_pins().size();
51  u32 mux_value = (base_value - 1) * 0.15 + 1;
52  u32 other_values = base_value * 0.3 + 1;
53  const double gate_area = gt->has_property(GateTypeProperty::c_mux) ? mux_value : other_values;
54  auto [output_pin, bf] = *(gt->get_boolean_functions().begin());
55 
56  // simplify to get rid of XOR which we currently cannot translate to genlib
57  bf = bf.simplify();
58  auto bf_str = bf.is_constant() ? (bf.has_constant_value(0) ? "CONST0" : "CONST1") : bf.to_string();
59 
60  // TODO: this is the stupid way, but i do not care at the moment
61  bf_str = utils::replace(bf_str, std::string("|"), std::string("+"));
62  bf_str = utils::replace(bf_str, std::string("&"), std::string("*"));
63 
64  std::string gate_str = "GATE " + name + " " + double_to_string(gate_area) + " " + output_pin + "=" + bf_str + ";\n";
65 
66  for (const auto& pin : gt->get_input_pins())
67  {
68  const std::string phase = (bf.get_top_level_node().is_operation() && (bf.get_top_level_node().type == BooleanFunction::NodeType::Not)) ? "INV" : "NONINV";
69  const std::string pin_str = "PIN " + pin->get_name() + " " + phase + " 1 999 1 0 1 0";
70  gate_str += pin_str + "\n";
71  }
72 
73  file_str += gate_str;
74  }
75 
76  // process sequential gates
77  for (const auto& [name, gt] : gate_lib->get_gate_types([](const GateType* gt) { return gt->has_property(GateTypeProperty::sequential); }))
78  {
79  log_warning("GenlibWriter", "Skipping gate type {} because sequential gates are currently not supported", name);
80  continue;
81  }
82 
83  // write generated string to file
84  std::ofstream file(file_path);
85  file << file_str;
86  file.close();
87 
88  return true;
89  }
90 
91 } // namespace hal
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
std::string get_name() const
bool has_property(GateTypeProperty property) const
Definition: gate_type.cpp:101
std::vector< GatePin * > get_input_pins() const
Definition: gate_type.cpp:269
const std::unordered_map< std::string, BooleanFunction > & get_boolean_functions() const
Definition: gate_type.cpp:746
bool write(const GateLibrary *gate_lib, const std::filesystem::path &file_path) override
Write all single output combinational gate types of the gate library to a genlib file at the provided...
uint32_t u32
Definition: defines.h:41
#define log_warning(channel,...)
Definition: log.h:76
T replace(const T &str, const T &search, const T &replace)
Definition: utils.h:384
Definition: defines.h:45
std::string name