Skip to content

Commit

Permalink
Merge pull request #386 from emsec/cleanup/enum_cleanup
Browse files Browse the repository at this point in the history
moved and refactored PinType and PinDirection
  • Loading branch information
SJulianS authored Mar 2, 2021
2 parents ed5242d + ae134e3 commit 5a2a490
Show file tree
Hide file tree
Showing 27 changed files with 736 additions and 705 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* changed function `get_base_type` of class `GateType` to `get_properties`
* changed HGL gate libraries to support multiple properties
* changed function `create_gate_type` of class `GateLibrary` to support multiple properties
* added `sequential`, `power`, `ground`, `buffer`, `mux`, and `carry` base types to class `GateType`
* added `sequential`, `power`, `ground`, `buffer`, `mux`, and `carry` gate type properties to enum `GateTypeProperty`
* moved enums `PinType` and `PinDirection` from class `GateType` into global scope
* added `get_path` to `netlist_utils` to retrieve all gates on the predecessor/successor path from a start gate/net to gates of a specified property
* made `optimize_constants` of class `BooleanFunction` publicly accessible
* refined buffer removal in `netlist_utils::remove_buffers` to take constant `0` and `1` inputs into account
* changes to `z3_utils` (WIP)
* added high-impedance state `Z` to class `BooleanFunction` and added basic support to `evaluate`
* cleaned up and refined some logger outputs and streamlined log channel names
* disabled extended logging again
* changes to `z3_utils` (WIP)
* fixed crash related to GraphicsScene destructor
* fixed overlapping gates in cone view (and subsequent segfault) by suppressing gate coordinates when adding to cone view
* fixed crash caused when gate pointer cannot be found in netlist (but in module)
* fixed `get_gate_by_id` and `get_gates` of class `Netlist` returning only gates contained within one of its modules (causing a GUI crash upon deleting gates from a module)
* fixed nets of old module not updating when moving gate from one module to another

## [3.2.5] - 2021-01-29 13:15:00+02:00 (urgency: medium)
* **WARNING:** temporarily enabled extended logging (includes taking screenshots) for university course purposes. Note that no data leaves your machine unless you actively provide it to us.
Expand Down
11 changes: 8 additions & 3 deletions include/hal_core/netlist/boolean_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include "hal_core/defines.h"
#include "hal_core/utilities/enums.h"
#include "z3++.h"

#include <algorithm>
Expand All @@ -48,9 +49,10 @@ namespace hal
*/
enum Value
{
X = -1, /**< Represents an undefined value. */
ZERO = 0, /**< Represents a logical 0. */
ONE = 1 /**< Represents a logical 1 */
ZERO = 0, /**< Represents a logical 0. */
ONE = 1, /**< Represents a logical 1 */
Z, /**< Represents an undefined value. */
X /**< Represents an undefined value. */
};

/**
Expand Down Expand Up @@ -388,4 +390,7 @@ namespace hal
operation m_op;
std::vector<BooleanFunction> m_operands;
};

template<>
std::vector<std::string> EnumStrings<BooleanFunction::Value>::data;
} // namespace hal
120 changes: 51 additions & 69 deletions include/hal_core/netlist/gate_library/gate_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,51 @@ namespace hal
dsp, /**< DSP gate type. **/
mux, /**< MUX gate type. **/
buffer, /**< Buffer gate type. **/
carry, /**< Carry gate type. **/
invalid /**< Invalid property. **/
carry /**< Carry gate type. **/
};

template<>
std::vector<std::string> EnumStrings<GateTypeProperty>::data;

/**
* Defines the direction of a pin.
*/
enum class PinDirection
{
none, /**< Invalid pin. **/
input, /**< Input pin. **/
output, /**< Output pin. **/
inout, /**< Inout pin. **/
internal /**< Internal pin. **/
};

template<>
std::vector<std::string> EnumStrings<PinDirection>::data;

/**
* Defines the type of a pin.
*/
enum class PinType
{
none, /**< Default pin. **/
power, /**< Power pin. **/
ground, /**< Ground pin. **/
lut, /**< Pin that generates output from LUT initialization string. **/
state, /**< Pin that generates output from internal state. **/
neg_state, /**< Pin that generates output from negated internal state. **/
clock, /**< Clock pin. **/
enable, /**< Enable pin. **/
set, /**< Set/preset pin. **/
reset, /**< Reset/clear pin. **/
data, /**< Data pin. **/
address, /**< Address pin. **/
io_pad, /**< IO pad pin. **/
select /**< Select pin. **/
};

template<>
std::vector<std::string> EnumStrings<PinType>::data;

/**
* A gate type contains information about its internals such as input and output pins as well as its Boolean functions.
*
Expand All @@ -64,50 +106,17 @@ namespace hal
class NETLIST_API GateType
{
public:
/**
* Defines the direction of a pin.
*/
enum class PinDirection
{
none, /**< Invalid pin. **/
input, /**< Input pin. **/
output, /**< Output pin. **/
inout, /**< Inout pin. **/
internal /**< Internal pin. **/
};

/**
* Defines the type of a pin.
*/
enum class PinType
{
none, /**< Default pin. **/
power, /**< Power pin. **/
ground, /**< Ground pin. **/
lut, /**< Pin that generates output from LUT initialization string. **/
state, /**< Pin that generates output from internal state. **/
neg_state, /**< Pin that generates output from negated internal state. **/
clock, /**< Clock pin. **/
enable, /**< Enable pin. **/
set, /**< Set/preset pin. **/
reset, /**< Reset/clear pin. **/
data, /**< Data pin. **/
address, /**< Address pin. **/
io_pad, /**< IO pad pin. **/
select /**< Select pin. **/
};

/**
* Defines the behavior of the gate type in case both clear and preset are active at the same time.
*/
enum class ClearPresetBehavior
{
L, /**< Set the internal state to \p 0. **/
H, /**< Set the internal state to \p 1. **/
N, /**< Do not change the internal state. **/
T, /**< Toggle, i.e., invert the internal state. **/
X, /**< Set the internal state to \p X. **/
invalid /**< Invalid behavior, used by default. **/
L, /**< Set the internal state to \p 0. **/
H, /**< Set the internal state to \p 1. **/
N, /**< Do not change the internal state. **/
T, /**< Toggle, i.e., invert the internal state. **/
X, /**< Set the internal state to \p X. **/
undef /**< Invalid behavior, used by default. **/
};

/**
Expand Down Expand Up @@ -162,24 +171,6 @@ namespace hal
*/
friend std::ostream& operator<<(std::ostream& os, const GateType& gate_type);

/**
* Insert the pin direction string representation to an output stream.
*
* @param[in] os - The output stream.
* @param[in] direction - The pin direction.
* @returns An output stream.
*/
friend std::ostream& operator<<(std::ostream& os, PinDirection direction);

/**
* Insert the pin type string representation to an output stream.
*
* @param[in] os - The output stream.
* @param[in] pin_type - The pin type.
* @returns An output stream.
*/
friend std::ostream& operator<<(std::ostream& os, PinType pin_type);

/**
* Check whether two gate types are equal.
*
Expand Down Expand Up @@ -431,10 +422,6 @@ namespace hal
std::string m_name;
std::set<GateTypeProperty> m_properties;

// enum to string
static const std::unordered_map<PinDirection, std::string> m_pin_direction_to_string;
static const std::unordered_map<PinType, std::string> m_pin_type_to_string;

// pins
std::vector<std::string> m_pins;
std::unordered_set<std::string> m_pins_set;
Expand All @@ -455,7 +442,7 @@ namespace hal
std::unordered_map<std::string, BooleanFunction> m_functions;

// sequential and LUT stuff
std::pair<ClearPresetBehavior, ClearPresetBehavior> m_clear_preset_behavior = {ClearPresetBehavior::invalid, ClearPresetBehavior::invalid};
std::pair<ClearPresetBehavior, ClearPresetBehavior> m_clear_preset_behavior = {ClearPresetBehavior::undef, ClearPresetBehavior::undef};
std::string m_config_data_category = "";
std::string m_config_data_identifier = "";
bool m_ascending = true;
Expand All @@ -466,11 +453,6 @@ namespace hal
GateType& operator=(const GateType&) = delete;
};

template<>
std::vector<std::string> EnumStrings<GateTypeProperty>::data;

template<>
std::vector<std::string> EnumStrings<GateType::ClearPresetBehavior>::data;


} // namespace hal
29 changes: 26 additions & 3 deletions include/hal_core/utilities/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "hal_core/defines.h"

#include <iostream>
#include <vector>

namespace hal
{
Expand All @@ -28,14 +29,14 @@ namespace hal
auto index = static_cast<size_t>(e);
if (index >= EnumStrings<T>::data.size())
{
throw std::runtime_error("no string for enum with value '" + std::to_string(index) + "'");
throw std::runtime_error("no string for enum with value '" + std::to_string(index) + "'.");
}
return EnumStrings<T>::data.at(index);
}

/**
* Translates a string into an enum value if possible.
* Assumes the last enum entry to represent an invalid state, as it defaults to this value.
* Throws an exception if no matching enum value is found.
*
* @param[in] str - The string.
* @returns The enum value.
Expand All @@ -51,7 +52,29 @@ namespace hal
}
}

return static_cast<T>(EnumStrings<T>::data.size() - 1);
throw std::runtime_error("no enum value for string `" + str + "` available.");
}

/**
* Translates a string into an enum value if possible.
* Defaults to the given default value if no matching enum value is found.
*
* @param[in] str - The string.
* @param[in] default_val - The default value.
* @returns The enum value.
*/
template<typename T, typename = typename std::enable_if<std::is_enum<T>::value, T>::type>
T enum_from_string(const std::string& str, const T default_val) noexcept
{
for (size_t i = 0; i < EnumStrings<T>::data.size(); i++)
{
if (EnumStrings<T>::data.at(i) == str)
{
return static_cast<T>(i);
}
}

return default_val;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugins/boolean_influence/src/plugin_boolean_influence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace hal
}

// Check for the data port pin
std::unordered_set<std::string> d_ports = gate->get_type()->get_pins_of_type(GateType::PinType::data);
std::unordered_set<std::string> d_ports = gate->get_type()->get_pins_of_type(PinType::data);
if (d_ports.size() != 1)
{
log_error("boolean_influence", "Can only handle flip flops with exactly 1 data port, but found {}.", d_ports.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,25 @@ namespace hal
auto id = sg->get_id();
sg->get_name();

for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(GateType::PinType::clock), true))
for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(PinType::clock), true))
{
netlist_abstr.gate_to_clock_signals[id].insert(net->get_id());
fingerprint.push_back(net->get_id());
}

for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(GateType::PinType::enable), true))
for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(PinType::enable), true))
{
netlist_abstr.gate_to_enable_signals[id].insert(net->get_id());
fingerprint.push_back(net->get_id());
}

for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(GateType::PinType::reset), true))
for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(PinType::reset), true))
{
netlist_abstr.gate_to_reset_signals[id].insert(net->get_id());
fingerprint.push_back(net->get_id());
}

for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(GateType::PinType::set), true))
for (auto net : netlist_utils::get_nets_at_pins(sg, sg->get_type()->get_pins_of_type(PinType::set), true))
{
netlist_abstr.gate_to_set_signals[id].insert(net->get_id());
fingerprint.push_back(net->get_id());
Expand Down
6 changes: 2 additions & 4 deletions plugins/hgl_parser/include/hgl_parser/hgl_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace hal
struct PinCtx
{
std::vector<std::string> pins;
std::unordered_map<std::string, GateType::PinType> pin_to_type;
std::unordered_map<std::string, GateType::PinDirection> pin_to_direction;
std::unordered_map<std::string,PinType> pin_to_type;
std::unordered_map<std::string, PinDirection> pin_to_direction;
std::unordered_map<std::string, std::string> boolean_functions;
};

Expand All @@ -66,8 +66,6 @@ namespace hal

std::set<std::string> m_cell_names;

static const std::unordered_map<std::string, GateType::PinType> m_string_to_pin_type;

bool parse_gate_library(const rapidjson::Document& document);
bool parse_gate_type(const rapidjson::Value& gate_type);
bool parse_pin(PinCtx& pin_ctx, const rapidjson::Value& pin, const std::string& gt_name);
Expand Down
Loading

0 comments on commit 5a2a490

Please sign in to comment.