HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
simulation_utils.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
5  namespace simulation_utils
6  {
8  {
9  if (v == 0 || v == 1)
10  {
11  return static_cast<BooleanFunction::Value>(1 - v);
12  }
13  return v;
14  }
15 
17  {
18  if (behavior == AsyncSetResetBehavior::N)
19  {
20  return previous_output;
21  }
22  else if (behavior == AsyncSetResetBehavior::X)
23  {
24  return BooleanFunction::Value::X;
25  }
26  else if (behavior == AsyncSetResetBehavior::L)
27  {
28  return BooleanFunction::Value::ZERO;
29  }
30  else if (behavior == AsyncSetResetBehavior::H)
31  {
32  return BooleanFunction::Value::ONE;
33  }
34  else if (behavior == AsyncSetResetBehavior::T)
35  {
36  return toggle(previous_output);
37  }
38  log_error("hal_simulator", "unsupported set/reset behavior {}.", enum_to_string(behavior));
39  return BooleanFunction::Value::X;
40  }
41 
42  std::vector<BooleanFunction::Value> int_to_values(u32 integer, u32 len)
43  {
44  if (len > 32)
45  {
46  return {};
47  }
48 
49  std::vector<BooleanFunction::Value> res;
50  for (u32 i = 0; i < len; i++)
51  {
52  res.push_back((BooleanFunction::Value)((integer >> i) & 1));
53  }
54 
55  return res;
56  }
57 
58  u32 values_to_int(const std::vector<BooleanFunction::Value>& values)
59  {
60  u32 len = values.size();
61 
62  if (len > 32)
63  {
64  return 0;
65  }
66 
67  u32 res = 0;
68  for (u32 i = 0; i < len; i++)
69  {
70  BooleanFunction::Value val = values.at(i);
71  if (val == BooleanFunction::Value::X || val == BooleanFunction::Value::Z)
72  {
73  return 0;
74  }
75 
76  res ^= (u32)val << i;
77  }
78 
79  return res;
80  }
81 
82  u32 get_int_bus_value(const std::unordered_map<std::string, BooleanFunction::Value>& signal_values, const std::vector<GatePin*>& ordered_pins)
83  {
84  std::vector<BooleanFunction::Value> ordered_values;
85  for (const GatePin* pin : ordered_pins)
86  {
87  ordered_values.push_back(signal_values.at(pin->get_name()));
88  }
89 
90  return simulation_utils::values_to_int(ordered_values);
91  }
92  } // namespace simulation_utils
93 } // namespace hal
Value
represents the type of the node
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
u32 get_int_bus_value(const std::unordered_map< std::string, BooleanFunction::Value > &signal_values, const std::vector< GatePin * > &ordered_pins)
BooleanFunction::Value toggle(BooleanFunction::Value v)
std::vector< BooleanFunction::Value > int_to_values(u32 integer, u32 len)
u32 values_to_int(const std::vector< BooleanFunction::Value > &values)
BooleanFunction::Value process_clear_preset_behavior(AsyncSetResetBehavior behavior, BooleanFunction::Value previous_output)
Definition: defines.h:45
std::string enum_to_string(T e)
Definition: enums.h:53