Skip to content

Commit

Permalink
Merge branch 'feature/word_level_structures' of github.com:emsec/hal …
Browse files Browse the repository at this point in the history
…into feature/word_level_structures
  • Loading branch information
SimonKlx committed Nov 15, 2023
2 parents bbcacd0 + 9e2ea39 commit 41d0c74
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 11 deletions.
8 changes: 6 additions & 2 deletions include/hal_core/netlist/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ namespace hal

/**
* Get the number of sources of the net.
* The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
*
* @param[in] filter - An optional filter.
* @returns The number of sources.
*/
u32 get_num_of_sources() const;
u32 get_num_of_sources(const std::function<bool(Endpoint* ep)>& filter = nullptr) const;

/**
* Get a vector of sources of the net.
Expand Down Expand Up @@ -318,10 +320,12 @@ namespace hal

/**
* Get the number of destinations of the net.
* The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
*
* @param[in] filter - An optional filter.
* @returns The number of destinations.
*/
u32 get_num_of_destinations() const;
u32 get_num_of_destinations(const std::function<bool(Endpoint* ep)>& filter = nullptr) const;

/**
* Get a vector of destinations of the net.
Expand Down
14 changes: 14 additions & 0 deletions include/hal_core/utilities/finite_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ namespace hal
return true;
}

template<typename InputIt>
bool insert(InputIt begin, InputIt end)
{
for (InputIt it = begin; it != end; ++it)
{
if (!insert(*it))
{
return false;
}
}

return true;
}

bool erase(const u32 index)
{
if (index >= m_size)
Expand Down
191 changes: 191 additions & 0 deletions plugins/gate_libraries/definitions/helper_libs/aix_hal_i2.hgl
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"version": 3,
"library": "basic_hal_i2",
"gate_locations": {
"data_category": "generic",
"data_x_identifier": "X_COORDINATE",
"data_y_identifier": "Y_COORDINATE"
},
"cells": [
{
"name": "HAL_BUF",
"types": [
"combinational",
"c_buffer"
],
"pin_groups": [
{
"name": "A",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "A",
"direction": "input",
"type": "none"
}
]
},
{
"name": "O",
"direction": "output",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "O",
"direction": "output",
"type": "none",
"function": "A"
}
]
}
]
},
{
"name": "HAL_INV",
"types": [
"combinational",
"c_inverter"
],
"pin_groups": [
{
"name": "A",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "A",
"direction": "input",
"type": "none"
}
]
},
{
"name": "O",
"direction": "output",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "O",
"direction": "output",
"type": "none",
"function": "(! A)"
}
]
}
]
},
{
"name": "HAL_AND2",
"types": [
"combinational",
"c_and"
],
"pin_groups": [
{
"name": "A",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "A",
"direction": "input",
"type": "none"
}
]
},
{
"name": "B",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "B",
"direction": "input",
"type": "none"
}
]
},
{
"name": "O",
"direction": "output",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "O",
"direction": "output",
"type": "none",
"function": "(A & B)"
}
]
}
]
},
{
"name": "HAL_XOR2",
"types": [
"combinational",
"c_xor"
],
"pin_groups": [
{
"name": "A",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "A",
"direction": "input",
"type": "none"
}
]
},
{
"name": "B",
"direction": "input",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "B",
"direction": "input",
"type": "none"
}
]
},
{
"name": "O",
"direction": "output",
"type": "none",
"ascending": false,
"start_index": 0,
"pins": [
{
"name": "O",
"direction": "output",
"type": "none",
"function": "(A ^ B)"
}
]
}
]
}
]
}
34 changes: 30 additions & 4 deletions src/netlist/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,22 @@ namespace hal
return std::find(m_sources_raw.begin(), m_sources_raw.end(), ep) != m_sources_raw.end();
}

u32 Net::get_num_of_sources() const
u32 Net::get_num_of_sources(const std::function<bool(Endpoint* ep)>& filter) const
{
return (u32)m_sources_raw.size();
if (!filter)
{
return (u32)m_sources_raw.size();
}

u32 num = 0;
for (auto dst : m_sources_raw)
{
if (filter(dst))
{
num++;
}
}
return num;
}

std::vector<Endpoint*> Net::get_sources(const std::function<bool(Endpoint* ep)>& filter) const
Expand Down Expand Up @@ -392,9 +405,22 @@ namespace hal
return std::find(m_destinations_raw.begin(), m_destinations_raw.end(), ep) != m_destinations_raw.end();
}

u32 Net::get_num_of_destinations() const
u32 Net::get_num_of_destinations(const std::function<bool(Endpoint* ep)>& filter) const
{
return (u32)m_destinations_raw.size();
if (!filter)
{
return (u32)m_destinations_raw.size();
}

u32 num = 0;
for (auto dst : m_destinations_raw)
{
if (filter(dst))
{
num++;
}
}
return num;
}

std::vector<Endpoint*> Net::get_destinations(const std::function<bool(Endpoint* ep)>& filter) const
Expand Down
14 changes: 10 additions & 4 deletions src/python_bindings/bindings/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,18 @@ namespace hal
:rtype: bool
)");

py_net.def_property_readonly("num_of_sources", &Net::get_num_of_sources, R"(
py_net.def_property_readonly(
"num_of_sources", [](Net* n) { return n->get_num_of_sources(); }, R"(
The number of sources of the net.
:type: int
)");

py_net.def("get_num_of_sources", &Net::get_num_of_sources, R"(
py_net.def("get_num_of_sources", &Net::get_num_of_sources, py::arg("filter") = nullptr, R"(
Get the number of sources of the net.
The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
:param lambda filter: An optional filter.
:returns: The number of sources.
:rtype: int
)");
Expand Down Expand Up @@ -279,15 +282,18 @@ namespace hal
:rtype: bool
)");

py_net.def_property_readonly("num_of_destinations", &Net::get_num_of_destinations, R"(
py_net.def_property_readonly(
"num_of_destinations", [](Net* n) { return n->get_num_of_destinations(); }, R"(
The number of destinations of the net.
:type: int
)");

py_net.def("get_num_of_destinations", &Net::get_num_of_destinations, R"(
py_net.def("get_num_of_destinations", &Net::get_num_of_destinations, py::arg("filter") = nullptr, R"(
Get the number of destinations of the net.
The optional filter is evaluated on every candidate such that the result only contains those matching the specified condition.
:param filter: An optional filter.
:returns: The number of destinations.
:rtype: int
)");
Expand Down
7 changes: 6 additions & 1 deletion tests/netlist/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ namespace hal {
Net* test_net = nl->create_net("test_net");
ASSERT_NE(test_net, nullptr);
EXPECT_TRUE(test_net->get_destinations().empty());
}
EXPECT_EQ(test_net->get_num_of_destinations(), 0);
}
{
// add destination to net
auto nl = test_utils::create_empty_netlist();
Expand All @@ -461,9 +462,11 @@ namespace hal {
Gate* test_gate = nl->create_gate(nl->get_gate_library()->get_gate_type_by_name("BUF"), "test_gate");
ASSERT_NE(test_gate, nullptr);
EXPECT_TRUE(test_net->get_destinations().empty());
EXPECT_EQ(test_net->get_num_of_destinations(), 0);
EXPECT_NE(test_net->add_destination(test_gate, "I"), nullptr);
ASSERT_EQ(test_net->get_destinations().size(), 1);
EXPECT_EQ(test_net->get_destinations().at(0), test_gate->get_fan_in_endpoint("I"));
EXPECT_EQ(test_net->get_num_of_destinations(), 1);
}
{
// get multiple destinations (no filter applied)
Expand All @@ -478,6 +481,7 @@ namespace hal {
EXPECT_NE(test_net->add_destination(test_gate, "DATA_IN(2)"), nullptr);
EXPECT_NE(test_net->add_destination(test_gate, "DATA_IN(3)"), nullptr);
EXPECT_EQ(test_net->get_destinations(), std::vector<Endpoint*>(test_gate->get_fan_in_endpoints()));
EXPECT_EQ(test_net->get_num_of_destinations(), 4);
}
{
// get multiple destinations (filter applied)
Expand All @@ -495,6 +499,7 @@ namespace hal {
EXPECT_NE(test_net->add_destination(test_gate_2, "DATA_IN(1)"), nullptr);
EXPECT_EQ(test_net->get_destinations([](const Endpoint* ep){return ep->get_gate()->get_name() == "test_gate_1";}),
std::vector<Endpoint*>(test_gate_1->get_fan_in_endpoints()));
EXPECT_EQ(test_net->get_num_of_destinations([](const Endpoint* ep){return ep->get_gate()->get_name() == "test_gate_1";} ), 2);
}
{
// remove a destination by specifying gate and pin
Expand Down

0 comments on commit 41d0c74

Please sign in to comment.