HAL
gate.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
34 
35 #include <functional>
36 #include <map>
37 #include <memory>
38 #include <set>
39 #include <unordered_map>
40 #include <vector>
41 
42 namespace hal
43 {
44  /* forward declaration */
45  class Netlist;
46  class Net;
47  class Module;
48  class Grouping;
49  class Endpoint;
50  class NetlistInternalManager;
51 
58  {
59  public:
67  bool operator==(const Gate& other) const;
68 
76  bool operator!=(const Gate& other) const;
77 
83  ssize_t get_hash() const;
84 
90  u32 get_id() const;
91 
97  Netlist* get_netlist() const;
98 
104  const std::string& get_name() const;
105 
111  void set_name(const std::string& name);
112 
118  GateType* get_type() const;
119 
125  bool has_location() const;
126 
133  void set_location_x(i32 x);
134 
141  i32 get_location_x() const;
142 
149  void set_location_y(i32 y);
150 
157  i32 get_location_y() const;
158 
165  void set_location(const std::pair<i32, i32>& location);
166 
174  std::pair<i32, i32> get_location() const;
175 
181  Module* get_module() const;
182 
192  std::vector<Module*> get_modules(const std::function<bool(Module*)>& filter = nullptr, bool recursive = true) const;
193 
200  Grouping* get_grouping() const;
201 
209  BooleanFunction get_boolean_function(const std::string& name) const;
210 
218  BooleanFunction get_boolean_function(const GatePin* pin = nullptr) const;
219 
226  std::unordered_map<std::string, BooleanFunction> get_boolean_functions(bool only_custom_functions = false) const;
227 
236  Result<BooleanFunction> get_resolved_boolean_function(const GatePin* pin, const bool use_net_variables = false) const;
237 
244  bool add_boolean_function(const std::string& name, const BooleanFunction& func);
245 
251  bool mark_vcc_gate();
252 
258  bool mark_gnd_gate();
259 
265  bool unmark_vcc_gate();
266 
272  bool unmark_gnd_gate();
273 
279  bool is_vcc_gate() const;
280 
286  bool is_gnd_gate() const;
287 
288  /*
289  * pin specific functions
290  */
291 
297  const std::vector<Net*>& get_fan_in_nets() const;
298 
306  std::vector<Net*> get_fan_in_nets(const std::function<bool(Net*)>& filter) const;
307 
314  Net* get_fan_in_net(const std::string& pin_name) const;
315 
322  Net* get_fan_in_net(const GatePin* pin) const;
323 
330  bool is_fan_in_net(const Net* net) const;
331 
337  const std::vector<Endpoint*>& get_fan_in_endpoints() const;
338 
346  std::vector<Endpoint*> get_fan_in_endpoints(const std::function<bool(Endpoint*)>& filter) const;
347 
354  Endpoint* get_fan_in_endpoint(const std::string& pin_name) const;
355 
362  Endpoint* get_fan_in_endpoint(const GatePin* pin) const;
363 
370  Endpoint* get_fan_in_endpoint(const Net* net) const;
371 
377  const std::vector<Net*>& get_fan_out_nets() const;
378 
386  std::vector<Net*> get_fan_out_nets(const std::function<bool(Net*)>& filter) const;
387 
394  Net* get_fan_out_net(const std::string& pin_name) const;
395 
402  Net* get_fan_out_net(const GatePin* pin) const;
403 
410  bool is_fan_out_net(const Net* net) const;
411 
417  const std::vector<Endpoint*>& get_fan_out_endpoints() const;
418 
426  std::vector<Endpoint*> get_fan_out_endpoints(const std::function<bool(Endpoint*)>& filter) const;
427 
434  Endpoint* get_fan_out_endpoint(const std::string& pin_name) const;
435 
442  Endpoint* get_fan_out_endpoint(const GatePin* pin) const;
443 
450  Endpoint* get_fan_out_endpoint(const Net* net) const;
451 
459  std::vector<Gate*> get_unique_predecessors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
460 
468  std::vector<Endpoint*> get_predecessors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
469 
477  Endpoint* get_predecessor(const std::string& pin_name) const;
478 
486  Endpoint* get_predecessor(const GatePin* pin) const;
487 
495  std::vector<Gate*> get_unique_successors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
496 
504  std::vector<Endpoint*> get_successors(const std::function<bool(const GatePin* pin, Endpoint* ep)>& filter = nullptr) const;
505 
513  Endpoint* get_successor(const std::string& pin_name) const;
514 
522  Endpoint* get_successor(const GatePin* pin) const;
523 
530  Result<std::vector<std::string>> get_init_data() const;
531 
539  Result<std::monostate> set_init_data(const std::vector<std::string>& init_data);
540 
541  private:
543  Gate(NetlistInternalManager* mgr, EventHandler* event_handler, u32 id, GateType* gt, const std::string& name, i32 x, i32 y);
544 
545  Gate(const Gate&) = delete;
546  Gate(Gate&&) = delete;
547  Gate& operator=(const Gate&) = delete;
548  Gate& operator=(Gate&&) = delete;
549 
550  BooleanFunction get_lut_function(const GatePin* pin) const;
551 
552  /* pointer to corresponding netlist parent */
553  NetlistInternalManager* m_internal_manager;
554 
555  /* id of the gate */
556  u32 m_id;
557 
558  /* name of the gate */
559  std::string m_name;
560 
561  /* type of the gate */
562  GateType* m_type;
563 
564  /* location */
565  i32 m_x = -1;
566  i32 m_y = -1;
567 
568  /* owning module */
569  Module* m_module;
570 
571  /* grouping */
572  Grouping* m_grouping = nullptr;
573 
574  /* connected nets */
575  std::vector<Endpoint*> m_in_endpoints;
576  std::vector<Endpoint*> m_out_endpoints;
577  std::vector<Net*> m_in_nets;
578  std::vector<Net*> m_out_nets;
579 
580  /* dedicated functions */
581  std::unordered_map<std::string, BooleanFunction> m_functions;
582 
583  EventHandler* m_event_handler;
584  };
585 } // namespace hal
#define NETLIST_API
Definition: arch_linux.h:30
Definition: gate.h:58
Definition: net.h:58
int32_t i32
Definition: defines.h:36
quint32 u32
Net * net
std::string name