Skip to content

Commit

Permalink
Fix module and gate-type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Oct 3, 2023
1 parent 152cc46 commit bd3aca7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 43 deletions.
32 changes: 21 additions & 11 deletions include/hal_core/netlist/pins/pin_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,20 @@ namespace hal
* Remove a pin from the pin group.
*
* @param[in] pin - The pin to remove.
* @returns Ok on success, an error message otherwise.
* @returns true on success, false otherwise.
*/
Result<std::monostate> remove_pin(T* pin)
bool remove_pin(T* pin)
{
if (pin == nullptr)
{
return ERR("'nullptr' given instead of a pin when trying to remove pin from pin group '" + m_name + "' with ID " + std::to_string(m_id));
log_warning("pin_group", "'nullptr' given instead of a pin when trying to remove pin from pin group '{}' with ID {}.", m_name, m_id);
return false;
}

if (pin->m_group.first != this)
{
return ERR("pin '" + pin->get_name() + "' with ID " + std::to_string(pin->get_id()) + " does not belong to pin group '" + m_name + "' with ID " + std::to_string(m_id));
log_warning("pin_group", "pin '{}' with ID {} does not belong to pin group '{}' with ID {}.", pin->get_name(), pin->get_id(), m_name, m_id);
return false;
}

i32 index = pin->m_group.second;
Expand All @@ -454,16 +456,24 @@ namespace hal
}
else
{
auto it = std::next(m_pins.begin(), m_start_index - index);
it = m_pins.erase(it);
for (; it != m_pins.end(); it++)
if (m_pins.size()==1)
{
std::get<1>((*it)->m_group)++;
m_pins.clear();
m_next_index++;
}
m_next_index++;
}
else
{
auto it = m_pins.begin();
for (int i=m_start_index; i>index;i--)
{
std::get<1>((*(it++))->m_group)--;
}
m_pins.erase(it);
--m_start_index;
}
}

return OK({});
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/netlist/decorators/netlist_modification_decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ namespace hal
// remove pin from current pin group
auto current_pin_group = pin->get_group().first;
// This get is safe, since we make sure that the pin is a valid pointer and part of the group
current_pin_group->remove_pin(pin).get();
current_pin_group->remove_pin(pin);

// delete old pin group incase it is now empty
if (current_pin_group->get_pins().empty())
Expand Down
33 changes: 24 additions & 9 deletions src/netlist/gate_library/gate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ namespace hal
}

PinGroup<GatePin>* pin_group;
if (!ascending && !pins.empty())
{
// compensate for shifting the start index
start_index -= (pins.size()-1);
}

if (auto res = create_pin_group_internal(id, name, direction, type, ascending, start_index); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not create pin group '" + name + "' for gate type '" + m_name + "' with ID " + std::to_string(m_id));
Expand All @@ -379,13 +385,23 @@ namespace hal
pin_group = res.get();
}

for (auto* pin : pins)
if (ascending)
{
if (auto res = assign_pin_to_group(pin_group, pin, delete_empty_groups); res.is_error())
{
assert(delete_pin_group(pin_group));
return ERR(res.get_error());
}
for (auto it = pins.begin(); it != pins.end(); ++it)
if (auto res = assign_pin_to_group(pin_group, *it, delete_empty_groups); res.is_error())
{
assert(delete_pin_group(pin_group));
return ERR(res.get_error());
}
}
else
{
for (auto it = pins.rbegin(); it != pins.rend(); ++it)
if (auto res = assign_pin_to_group(pin_group, *it, delete_empty_groups); res.is_error())
{
assert(delete_pin_group(pin_group));
return ERR(res.get_error());
}
}

return OK(pin_group);
Expand Down Expand Up @@ -488,10 +504,9 @@ namespace hal
if (PinGroup<GatePin>* pg = pin->get_group().first; pg != nullptr)
{
// remove from old group and potentially delete old group if empty
if (auto res = pg->remove_pin(pin); res.is_error())
if (!pg->remove_pin(pin))
{
return ERR_APPEND(res.get_error(),
"could not assign pin '" + pin->get_name() + "' with ID " + std::to_string(pin->get_id()) + " to pin group '" + pin_group->get_name() + "' with ID "
return ERR("could not assign pin '" + pin->get_name() + "' with ID " + std::to_string(pin->get_id()) + " to pin group '" + pin_group->get_name() + "' with ID "
+ std::to_string(pin_group->get_id()) + " of gate type '" + m_name + "' with ID " + std::to_string(m_id) + ": unable to remove pin from pin group '"
+ pg->get_name() + "' with ID " + std::to_string(pg->get_id()));
}
Expand Down
32 changes: 24 additions & 8 deletions src/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,12 @@ namespace hal
}

PinGroup<ModulePin>* pin_group;
if (!ascending && !pins.empty())
{
// compensate for shifting the start index
start_index -= (pins.size()-1);
}

if (auto res = create_pin_group_internal(id, name, direction, type, ascending, start_index); res.is_error())
{
return ERR_APPEND(res.get_error(), "could not create pin group '" + name + "' for module '" + m_name + "' with ID " + std::to_string(m_id));
Expand All @@ -1252,13 +1258,23 @@ namespace hal
pin_group = res.get();
}

for (auto* pin : pins)
if (ascending)
{
if (!assign_pin_to_group(pin_group, pin, delete_empty_groups))
{
assert(delete_pin_group(pin_group));
return ERR("Assign pin to group failed.");
}
for (auto it = pins.begin(); it != pins.end(); ++it)
if (!assign_pin_to_group(pin_group, *it, delete_empty_groups))
{
assert(delete_pin_group(pin_group));
return ERR("Assign pin to group failed.");
}
}
else
{
for (auto it = pins.rbegin(); it != pins.rend(); ++it)
if (!assign_pin_to_group(pin_group, *it, delete_empty_groups))
{
assert(delete_pin_group(pin_group));
return ERR("Assign pin to group failed.");
}
}

PinChangedEvent(this,PinEvent::GroupCreate,pin_group->get_id()).send();
Expand Down Expand Up @@ -1390,7 +1406,7 @@ namespace hal
if (PinGroup<ModulePin>* pg = pin->get_group().first; pg != nullptr)
{
// remove from old group and potentially delete old group if empty
if (auto res = pg->remove_pin(pin); res.is_error())
if (!pg->remove_pin(pin))
{
log_warning("module",
"could not assign pin '{}' with ID {} to pin group '{}' with ID {} of module '{}' with ID {}: unable to remove pin from pin group '{}' with ID {}",
Expand Down Expand Up @@ -1574,7 +1590,7 @@ namespace hal
PinGroup<ModulePin>* pin_group = pin->get_group().first;
assert(pin_group != nullptr);

if (auto res = pin_group->remove_pin(pin); res.is_error())
if (!pin_group->remove_pin(pin))
{
log_warning("module", "could not remove pin '{}' with ID {} from net '{}' with ID {}: failed to remove pin from pin group '{}' with ID {}", pin->get_name(), pin->get_id(), net->get_name(), net->get_id(), pin_group->get_name(), pin_group->get_id());
return false;
Expand Down
45 changes: 31 additions & 14 deletions tests/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ namespace hal {
// create input pin group
auto res = m_1->create_pin_group("I", {in_pin_0, in_pin_1}, PinDirection::input, PinType::none, false, 2);
ASSERT_TRUE(res.is_ok());
// descending group start index 2
// in_pin_0 "I0" index 2
// in_pin_1 "I1" index 1
PinGroup<ModulePin>* in_group = res.get();
ASSERT_NE(in_group, nullptr);
EXPECT_EQ(m_1->get_pin_groups().size(), 3);
Expand Down Expand Up @@ -1138,41 +1141,55 @@ namespace hal {

// move pins within group
EXPECT_TRUE(m_1->move_pin_within_group(in_group, in_pin_1, 2));
// descending group start index 2
// in_pin_1 "I1" index 2
// in_pin_0 "I0" index 1
EXPECT_EQ(in_pin_0->get_group(), std::pair(in_group, i32(1)));
EXPECT_EQ(in_group->get_index(in_pin_0).get(), 1);
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(2)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 2);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_0);
EXPECT_EQ(in_group->get_pin_at_index(2).get(), in_pin_1);

EXPECT_TRUE(m_1->move_pin_within_group(in_group, in_pin_1, 1));
EXPECT_TRUE(m_1->move_pin_within_group(in_group, in_pin_0, 2));
// descending group start index 2
// in_pin_0 "I0" index 2
// in_pin_1 "I1" index 1
EXPECT_EQ(in_pin_0->get_group(), std::pair(in_group, i32(2)));
EXPECT_EQ(in_group->get_index(in_pin_0).get(), 2);
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(1)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 1);
EXPECT_EQ(in_group->get_pin_at_index(2).get(), in_pin_0);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_1);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_1);

// remove pin from group
EXPECT_TRUE(m_1->remove_pin_from_group(in_group, in_pin_0));
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(2)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 2);
EXPECT_EQ(in_group->get_pin_at_index(2).get(), in_pin_1);
// descending group start index 1
// in_pin_1 "I1" index 1
EXPECT_EQ(in_group->get_start_index(), 1);
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(1)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 1);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_1);
EXPECT_EQ(in_pin_0->get_group().first->get_name(), in_pin_0->get_name());
EXPECT_EQ(in_pin_0->get_group().second, 0);

// assign pin to group
EXPECT_TRUE(m_1->assign_pin_to_group(in_group, in_pin_0));
// descending group start index 2
// in_pin_0 "I0" index 2
// in_pin_1 "I1" index 1
EXPECT_EQ(in_group->size(), 2);

// assign pin to group
// assign same pin twice should not do anything
EXPECT_TRUE(m_1->assign_pin_to_group(in_group, in_pin_0));
EXPECT_EQ(m_1->get_pin_groups().size(), 3);
EXPECT_EQ(in_group->size(), 2);
EXPECT_EQ(in_pin_0->get_group(), std::pair(in_group, i32(1)));
EXPECT_EQ(in_group->get_index(in_pin_0).get(), 1);
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(2)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 2);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_0);
EXPECT_EQ(in_group->get_pin_at_index(2).get(), in_pin_1);
EXPECT_EQ(in_pin_0->get_group(), std::pair(in_group, i32(2)));
EXPECT_EQ(in_group->get_index(in_pin_0).get(), 2);
EXPECT_EQ(in_pin_1->get_group(), std::pair(in_group, i32(1)));
EXPECT_EQ(in_group->get_index(in_pin_1).get(), 1);
EXPECT_EQ(in_group->get_pin_at_index(2).get(), in_pin_0);
EXPECT_EQ(in_group->get_pin_at_index(1).get(), in_pin_1);
}
}
TEST_END
Expand Down Expand Up @@ -1257,8 +1274,8 @@ namespace hal {
std::make_tuple(ModuleEvent::event::submodule_added, test_mod, other_mod_sub->get_id()),
std::make_tuple(ModuleEvent::event::submodule_removed, test_mod, other_mod_sub->get_id()),
std::make_tuple(ModuleEvent::event::gate_assigned, test_mod, test_gate->get_id()),
std::make_tuple(ModuleEvent::event::pin_changed, test_mod, NO_DATA),
std::make_tuple(ModuleEvent::event::pin_changed, test_mod, NO_DATA),
std::make_tuple(ModuleEvent::event::pin_changed, test_mod, PinChangedEvent(test_mod,PinEvent::PinRename,3).associated_data()),
std::make_tuple(ModuleEvent::event::pin_changed, test_mod, PinChangedEvent(test_mod,PinEvent::PinRename,1).associated_data()),
std::make_tuple(ModuleEvent::event::gate_removed, test_mod, test_gate->get_id())
};

Expand Down

0 comments on commit bd3aca7

Please sign in to comment.