HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
lut_function.cpp
Go to the documentation of this file.
10 #include "verilator/verilator.h"
11 
12 #include <filesystem>
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  std::vector<GatePin*> input_pins = gt->get_input_pins();
26  std::vector<GatePin*> output_pins = gt->get_output_pins();
27  u32 lut_size = input_pins.size();
28 
29  // check if LUTComponent exists, if not abort
30  if (LUTComponent* lut_component = gt->get_component_as<LUTComponent>([](const GateTypeComponent* c) { return LUTComponent::is_class_of(c); }); lut_component != nullptr)
31  {
32  log_debug("verilator", "valid LUT, got LUT component");
33  }
34  else
35  {
36  log_error("verilator", "cannot get LUTComponent, aborting...");
37  return function.str();
38  }
39 
40  if (output_pins.size() > 1)
41  {
42  log_error("verilator", "unsupported reached: currently only supporting LUTs with one output, split them into two and set the LUT_INIT string correctly :) ! aborting...");
43  return function.str();
44  }
45 
46  std::reverse(input_pins.begin(), input_pins.end()); // needs to be reverted due to access in LUT_INIT string
47  function << "wire [" << lut_size - 1 << ":0] lut_lookup = {";
48  for (const auto input_pin : input_pins)
49  {
50  function << input_pin->get_name() << ", ";
51  }
52  function.seekp(-2, function.cur); // remove the additional colon and space
53  function << "};" << std::endl;
54 
55  InitComponent* init_component = gt->get_component_as<InitComponent>([](const GateTypeComponent* c) { return InitComponent::is_class_of(c); });
56 
57  if (init_component == nullptr)
58  {
59  log_error("verilator", "Could not get init component for gate type {}!", gt->get_name());
60  }
61 
62  for (const auto& output_pin : output_pins)
63  {
64  function << "assign " << output_pin->get_name() << " = " << init_component->get_init_identifiers().front() << "[lut_lookup];" << std::endl;
65  }
66 
67  return function.str();
68  }
69 
70  } // namespace converter
71  } // namespace verilator
72 } // namespace hal
std::vector< GatePin * > get_output_pins() const
Definition: gate_type.cpp:285
const std::string & get_name() const
Definition: gate_type.cpp:64
std::vector< GatePin * > get_input_pins() const
Definition: gate_type.cpp:269
T * get_component_as(const std::function< bool(const GateTypeComponent *)> &filter=nullptr) const
Definition: gate_type.h:89
static bool is_class_of(const GateTypeComponent *component)
const std::vector< std::string > & get_init_identifiers() const
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
#define log_debug(channel,...)
Definition: log.h:74
std::string get_function_for_lut(const GateType *gt)
Definition: defines.h:45