HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
lut_function.cpp
Go to the documentation of this file.
9 #include "verilator/verilator.h"
10 
11 #include <algorithm>
12 #include <cctype>
13 #include <sstream>
14 #include <vector>
15 
16 namespace hal
17 {
18  namespace verilator
19  {
20  namespace converter
21  {
22  std::string get_function_for_lut(const GateType* gt)
23  {
24  std::stringstream function;
25 
26  const LUTComponent* lut_component = gt->get_component_as<LUTComponent>([](const GateTypeComponent* c) { return LUTComponent::is_class_of(c); });
27  if (lut_component == nullptr)
28  {
29  log_error("verilator", "cannot get LUTComponent for gate type '{}', aborting...", gt->get_name());
30  return {};
31  }
32 
33  const auto& pin_cfgs = lut_component->get_output_pin_configs();
34  if (pin_cfgs.empty())
35  {
36  log_error("verilator", "LUT gate type '{}' has no output pin configs, aborting...", gt->get_name());
37  return {};
38  }
39 
40  for (const auto& [pin_name, cfg] : pin_cfgs)
41  {
42  if (cfg.input_pins.empty())
43  {
44  log_error("verilator", "LUT gate type '{}': output pin '{}' has no input pins configured, aborting...", gt->get_name(), pin_name);
45  return {};
46  }
47 
48  // Sanitize pin name for use as a Verilog wire identifier
49  std::string safe_name = pin_name;
50  for (char& c : safe_name)
51  {
52  if (!std::isalnum(static_cast<unsigned char>(c)))
53  {
54  c = '_';
55  }
56  }
57  const std::string lookup_wire = "lut_lookup_" + safe_name;
58 
59  // Reverse input order so that the LSB input lands at index 0,
60  // matching the INIT bit addressing convention used in get_lut_function.
61  std::vector<std::string> inputs = cfg.input_pins;
62  std::reverse(inputs.begin(), inputs.end());
63 
64  function << "wire [" << inputs.size() - 1 << ":0] " << lookup_wire << " = {";
65  for (const auto& ipin : inputs)
66  {
67  function << ipin << ", ";
68  }
69  function.seekp(-2, function.cur);
70  function << "};\n";
71 
72  function << "assign " << pin_name << " = " << cfg.init_identifier << "[" << lookup_wire;
73  if (cfg.bit_offset > 0)
74  {
75  function << " + " << cfg.bit_offset;
76  }
77  function << "];\n";
78  }
79 
80  return function.str();
81  }
82 
83  } // namespace converter
84  } // namespace verilator
85 } // namespace hal
const std::string & get_name() const
Definition: gate_type.cpp:64
T * get_component_as(const std::function< bool(const GateTypeComponent *)> &filter=nullptr) const
Definition: gate_type.h:89
const std::unordered_map< std::string, LUTOutputConfig > & get_output_pin_configs() const
static bool is_class_of(const GateTypeComponent *component)
#define log_error(channel,...)
Definition: log.h:78
std::string get_function_for_lut(const GateType *gt)
Definition: defines.h:45