HAL
gate_library.cpp
Go to the documentation of this file.
2 
5 
6 namespace hal
7 {
8  GateLibrary::GateLibrary(const std::filesystem::path& path, const std::string& name) : m_name(name), m_path(path)
9  {
10  m_next_gate_type_id = 1;
11  }
12 
13  std::string GateLibrary::get_name() const
14  {
15  return m_name;
16  }
17 
18  std::filesystem::path GateLibrary::get_path() const
19  {
20  return m_path;
21  }
22 
23  void GateLibrary::set_path(const std::filesystem::path& modified_path)
24  {
25  m_path = modified_path;
26  }
27 
28  void GateLibrary::set_name(const std::string &modified_name)
29  {
30  m_name = modified_name;
31  }
32 
33  void GateLibrary::set_gate_location_data_category(const std::string& category)
34  {
35  m_gate_location_data_category = category;
36  }
37 
39  {
40  return m_gate_location_data_category;
41  }
42 
43  void GateLibrary::set_gate_location_data_identifiers(const std::string& x_coordinate, const std::string& y_coordinate)
44  {
45  m_gate_location_data_identifiers = std::make_pair(x_coordinate, y_coordinate);
46  }
47 
48  const std::pair<std::string, std::string>& GateLibrary::get_gate_location_data_identifiers() const
49  {
50  return m_gate_location_data_identifiers;
51  }
52 
53  GateType* GateLibrary::create_gate_type(const std::string& name, std::set<GateTypeProperty> properties, std::unique_ptr<GateTypeComponent> component)
54  {
55  if (m_gate_type_map.find(name) != m_gate_type_map.end())
56  {
57  log_error("gate_library", "could not create gate type with name '{}' as a gate type with the same name already exists within gate library '{}'.", name, m_name);
58  return nullptr;
59  }
60 
61  std::unique_ptr<GateType> gt = std::unique_ptr<GateType>(new GateType(this, get_unique_gate_type_id(), name, properties, std::move(component)));
62 
63  auto res = gt.get();
64  m_gate_type_map.emplace(name, res);
65  m_gate_types.push_back(std::move(gt));
66  return res;
67  }
68 
69  GateType* GateLibrary::replace_gate_type(u32 id, const std::string& name, std::set<GateTypeProperty> properties, std::unique_ptr<GateTypeComponent> component)
70  {
71  // must not insert duplicate name
72  auto it = m_gate_type_map.find(name);
73  if (it != m_gate_type_map.end() && it->second->get_id() != id)
74  {
75  log_error("gate_library", "could not replace gate type ID={} since new name '{}' exists already within gate library '{}'.", id, name, m_name);
76  return nullptr;
77  }
78 
79  auto jt = m_gate_types.begin();
80  while (jt != m_gate_types.end())
81  {
82  if (jt->get()->get_id() == id) break;
83  ++jt;
84  }
85  if (jt == m_gate_types.end())
86  {
87  log_error("gate_library", "could not replace gate type ID={}, no gate with this ID found within gate library", id, m_name);
88  return nullptr;
89  }
90  auto nt = m_gate_type_map.find(jt->get()->get_name());
91  if (nt != m_gate_type_map.end())
92  m_gate_type_map.erase(nt);
93  m_gate_types.erase(jt);
94 
95  std::unique_ptr<GateType> gt = std::unique_ptr<GateType>(new GateType(this, id, name, properties, std::move(component)));
96 
97  auto res = gt.get();
98  m_gate_type_map.emplace(name, res);
99  m_gate_types.push_back(std::move(gt));
100  return res;
101  }
102 
104  {
105  if (gate_type == nullptr)
106  {
107  return false;
108  }
109 
110  auto it = m_gate_type_map.find(gate_type->get_name());
111  if (it == m_gate_type_map.end())
112  {
113  return false;
114  }
115  return *(it->second) == *gate_type;
116  }
117 
118  bool GateLibrary::contains_gate_type_by_name(const std::string& name) const
119  {
120  if (auto it = m_gate_type_map.find(name); it != m_gate_type_map.end())
121  {
122  return true;
123  }
124 
125  return false;
126  }
127 
129  {
130  if (auto it = m_gate_type_map.find(name); it != m_gate_type_map.end())
131  {
132  return it->second;
133  }
134 
135  log_error("gate_library", "could not find the specified gate type, as there exists no gate type called '{}' within gate library '{}'.", name, m_name);
136  return nullptr;
137  }
138 
139  std::unordered_map<std::string, GateType*> GateLibrary::get_gate_types(const std::function<bool(const GateType*)>& filter) const
140  {
141  if (filter)
142  {
143  std::unordered_map<std::string, GateType*> res;
144  for (const auto& type : m_gate_types)
145  {
146  if (filter(type.get()))
147  {
148  res[type->get_name()] = type.get();
149  }
150  }
151  return res;
152  }
153 
154  return m_gate_type_map;
155  }
156 
158  {
159  auto out_pins = gate_type->get_output_pins();
160 
161  if (gate_type->get_input_pins().empty() && (out_pins.size() == 1))
162  {
163  auto bf = gate_type->get_boolean_function(out_pins.at(0));
164  if (!bf.is_empty() && bf.has_constant_value(1))
165  {
166  m_vcc_gate_types.emplace(gate_type->get_name(), gate_type);
167  return true;
168  }
169  }
170 
171  return false;
172  }
173 
174  std::unordered_map<std::string, GateType*> GateLibrary::get_vcc_gate_types() const
175  {
176  return m_vcc_gate_types;
177  }
178 
180  {
181  auto out_pins = gate_type->get_output_pins();
182 
183  if (gate_type->get_input_pins().empty() && (out_pins.size() == 1))
184  {
185  auto bf = gate_type->get_boolean_function(out_pins.at(0));
186  if (!bf.is_empty() && bf.has_constant_value(0))
187  {
188  m_gnd_gate_types.emplace(gate_type->get_name(), gate_type);
189  return true;
190  }
191  }
192 
193  return false;
194  }
195 
196  std::unordered_map<std::string, GateType*> GateLibrary::get_gnd_gate_types() const
197  {
198  return m_gnd_gate_types;
199  }
200 
201  std::vector<std::string> GateLibrary::get_includes() const
202  {
203  return m_includes;
204  }
205 
206  void GateLibrary::add_include(const std::string& inc)
207  {
208  m_includes.push_back(inc);
209  }
210 
211  u32 GateLibrary::get_unique_gate_type_id()
212  {
213  return m_next_gate_type_id++;
214  }
215 
216  void GateLibrary::remove_gate_type(const std::string& name)
217  {
218  if (m_gate_type_map.find(name) == m_gate_type_map.end())
219  {
220  log_error("gate_library", "could not remove gate type with name '{}' as a gate type with this name does not exist within gate library '{}'.", name, m_name);
221  }
222  else
223  {
224  auto it = m_gate_type_map.find(name);
225  m_gate_type_map.erase(it);
226  }
227  }
228 } // namespace hal
bool contains_gate_type_by_name(const std::string &name) const
GateType * replace_gate_type(u32 id, const std::string &name, std::set< GateTypeProperty > properties={GateTypeProperty::combinational}, std::unique_ptr< GateTypeComponent > component=nullptr)
std::filesystem::path get_path() const
const std::pair< std::string, std::string > & get_gate_location_data_identifiers() const
bool mark_vcc_gate_type(GateType *gate_type)
void remove_gate_type(const std::string &name)
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
std::unordered_map< std::string, GateType * > get_vcc_gate_types() const
GateType * get_gate_type_by_name(const std::string &name) const
void set_path(const std::filesystem::path &modified_path)
GateLibrary(const std::filesystem::path &path, const std::string &name)
Definition: gate_library.cpp:8
void set_name(const std::string &modified_name)
void set_gate_location_data_category(const std::string &category)
bool mark_gnd_gate_type(GateType *gate_type)
bool contains_gate_type(GateType *gate_type) const
void add_include(const std::string &inc)
std::vector< std::string > get_includes() const
void set_gate_location_data_identifiers(const std::string &x_coordinate, const std::string &y_coordinate)
GateType * create_gate_type(const std::string &name, std::set< GateTypeProperty > properties={GateTypeProperty::combinational}, std::unique_ptr< GateTypeComponent > component=nullptr)
std::unordered_map< std::string, GateType * > get_gnd_gate_types() const
std::string get_name() const
const std::string & get_gate_location_data_category() const
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
BooleanFunction get_boolean_function(const std::string &name) const
Definition: gate_type.cpp:714
#define log_error(channel,...)
Definition: log.h:78
quint32 u32
PinType type
std::string name