Skip to content

Commit

Permalink
added function to gate library
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Jun 22, 2024
1 parent bde4d1c commit 1415ab1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ All notable changes to this project will be documented in this file.
* added pin types `status`, `error`, `error_detection`, `done`, and `control`
* added optional filter to `Net::get_num_of_sources` and `Net::get_num_of_destinations`
* added function `unify_ff_outputs` to netlist preprocessing plugin
* added function `replace_gate_type` to gate library
* changed supported input file formats for import from hard coded list to list provided by loadable parser plugins
* changed behavior of import netlist dialog, suggest only non-existing directory names and loop until an acceptable name was entered
* changed appearance and behavior of import project dialog, make sure existing hal projects don't get overwritten
Expand Down
12 changes: 12 additions & 0 deletions include/hal_core/netlist/gate_library/gate_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ namespace hal
*/
GateType* create_gate_type(const std::string& name, std::set<GateTypeProperty> properties = {GateTypeProperty::combinational}, std::unique_ptr<GateTypeComponent> component = nullptr);

/**
* TODO pybind
* Replace gate type with given ID, might change name, properties, component, and return it.
*
* @param[in] id - The ID of gate type
* @param[in] name - The name of the gate type.
* @param[in] properties - The properties of the gate type.
* @param[in] component - A component adding additional functionality to the gate type.
* @returns The new gate type instance on success, a nullptr otherwise.
*/
GateType* replace_gate_type(u32 id, const std::string& name, std::set<GateTypeProperty> properties = {GateTypeProperty::combinational}, std::unique_ptr<GateTypeComponent> component = nullptr);

/**
* Check whether the given gate type is contained in this library.
*
Expand Down
34 changes: 34 additions & 0 deletions src/netlist/gate_library/gate_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ namespace hal
return res;
}

GateType* GateLibrary::replace_gate_type(u32 id, const std::string& name, std::set<GateTypeProperty> properties, std::unique_ptr<GateTypeComponent> component)
{
// must not insert duplicate name
auto it = m_gate_type_map.find(name);
if (it != m_gate_type_map.end() && it->second->get_id() != id)
{
log_error("gate_library", "could not replace gate type ID={} since new name '{}' exists already within gate library '{}'.", id, name, m_name);
return nullptr;
}

auto jt = m_gate_types.begin();
while (jt != m_gate_types.end())
{
if (jt->get()->get_id() == id) break;
++jt;
}
if (jt == m_gate_types.end())
{
log_error("gate_library", "could not replace gate type ID={}, no gate with this ID found within gate library", id, m_name);
return nullptr;
}
auto nt = m_gate_type_map.find(jt->get()->get_name());
if (nt != m_gate_type_map.end())
m_gate_type_map.erase(nt);
m_gate_types.erase(jt);

std::unique_ptr<GateType> gt = std::unique_ptr<GateType>(new GateType(this, id, name, properties, std::move(component)));

auto res = gt.get();
m_gate_type_map.emplace(name, res);
m_gate_types.push_back(std::move(gt));
return res;
}

bool GateLibrary::contains_gate_type(GateType* gate_type) const
{
if (gate_type == nullptr)
Expand Down
10 changes: 10 additions & 0 deletions tests/netlist/gate_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ namespace hal
gl->add_include("another.include");
gl->add_include("last.include");
EXPECT_EQ(gl->get_includes(), std::vector<std::string>({"in.clu.de", "another.include", "last.include"}));

// Replace gate type
u32 repl_id = gt_and->get_id();
EXPECT_EQ(gl->replace_gate_type(repl_id, "gt_or"), nullptr); // must avoid name collision
gt_and = gl->replace_gate_type(repl_id, "gt_and"); // same name as before is ok
ASSERT_TRUE(gt_and != nullptr);
gt_and = gl->replace_gate_type(repl_id, "gt_and_repl"); // new name also ok
ASSERT_TRUE(gt_and != nullptr);
EXPECT_EQ(gl->get_gate_type_by_name("gt_and"), nullptr); // no longer in gate library
EXPECT_EQ(gl->get_gate_type_by_name("gt_and_repl"), gt_and); // replaced by type with new name
}
TEST_END
}
Expand Down

0 comments on commit 1415ab1

Please sign in to comment.