Skip to content

Commit

Permalink
Bugfix: permutation order of truth table
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Mar 3, 2024
1 parent b7fa127 commit 6f97b6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace hal
if (getColumnNumber(gate)-1 > 8)
{
mTableWidget->hide();
mDisclaimer->setText(QString("Cannot calculate truth table\nfor gate with %1 input pins\nand %2 output pins")
.arg(gate->get_input_pins().size())
.arg(gate->get_output_pins().size()));
mDisclaimer->show();
return;
}

Expand All @@ -41,64 +45,50 @@ namespace hal
mTableWidget->verticalHeader()->hide();

BooleanFunction boolFunc = gate->get_boolean_function();
QStringList header;
std::vector<std::string> inputs = gate->get_input_pin_names();
std::vector<std::string> outputs = gate->get_output_pin_names();
auto truthTable = boolFunc.compute_truth_table(inputs, false).get().at(0);

for (uint column = 0; column < inputs.size(); column++) {

QTableWidgetItem* item = new QTableWidgetItem(QString::fromStdString(inputs[column]));
mTableWidget->setItem(0, column, item);
}
for (uint column = inputs.size(); column < outputs.size()+inputs.size(); column++) {
for (const std::string& inputPinName : inputs)
header << "Input\n" + QString::fromStdString(inputPinName);
for (const std::string& outputPinName : outputs)
header << "Output\n" + QString::fromStdString(outputPinName);
mTableWidget->setHorizontalHeaderLabels(header);

QTableWidgetItem* item = new QTableWidgetItem(QString::fromStdString(outputs[column-inputs.size()]));
mTableWidget->setItem(0, column, item);
}
auto truthTable = boolFunc.compute_truth_table(inputs, false).get().at(0);

for (uint truthTableIdx = 0; truthTableIdx < truthTable.size(); truthTableIdx++)
for (uint irow = 0; irow < truthTable.size(); irow++)
{
//iterate from 0..0 to 1..1
for (uint i = 0; i < gate->get_input_pins().size(); i++)
for (uint icol = 0; icol < gate->get_input_pins().size(); icol++)
{
u32 shift = gate->get_input_pins().size() - i - 1;
u8 inputBit = u8((truthTableIdx >> shift) & 1);
if(inputBit == 0)
{
QTableWidgetItem* item = new QTableWidgetItem("L");
mTableWidget->setItem(truthTableIdx+1, i, item);
}
else
{
QTableWidgetItem* item = new QTableWidgetItem("H");
mTableWidget->setItem(truthTableIdx+1, i, item);
}


u8 inputBit = u8((irow >> icol) & 1);
QTableWidgetItem* item = new QTableWidgetItem(inputBit ? "H" : "L");
mTableWidget->setItem(irow, icol, item);
}

//fill the output columns
for (int i = gate->get_input_pins().size(); i < gate->get_output_pins().size()+gate->get_input_pins().size(); i++)
for (uint icol = gate->get_input_pins().size(); icol < gate->get_output_pins().size()+gate->get_input_pins().size(); icol++)
{
BooleanFunction::Value val = truthTable[truthTableIdx];
BooleanFunction::Value val = truthTable[irow];
if (val == BooleanFunction::Value::ZERO)
{
QTableWidgetItem* item = new QTableWidgetItem("L");
mTableWidget->setItem(truthTableIdx+1, i, item);
mTableWidget->setItem(irow, icol, item);
}
else if (val == BooleanFunction::Value::ONE)
{
QTableWidgetItem* item = new QTableWidgetItem("H");
mTableWidget->setItem(truthTableIdx+1, i, item);
mTableWidget->setItem(irow, icol, item);
}
else if (val == BooleanFunction::Value::Z)
{
QTableWidgetItem* item = new QTableWidgetItem("Z");
mTableWidget->setItem(truthTableIdx+1, i, item);
mTableWidget->setItem(irow, icol, item);
}
else
{
QTableWidgetItem* item = new QTableWidgetItem("X");
mTableWidget->setItem(truthTableIdx+1, i, item);
mTableWidget->setItem(irow, icol, item);
undefinedResult = true;
}
}
Expand All @@ -125,7 +115,7 @@ namespace hal

int GateLibraryTabTruthTable::getRowNumber(GateType* gate)
{
return pow(2, gate->get_input_pins().size())+1; //iterate from 0..0 to 2^n
return pow(2, gate->get_input_pins().size()); //iterate from 0..0 to 2^n
}

int GateLibraryTabTruthTable::getColumnNumber(GateType* gate)
Expand All @@ -136,5 +126,4 @@ namespace hal
}
return 0;
}

}
12 changes: 0 additions & 12 deletions tests/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,20 +1293,8 @@ namespace hal {

// Trigger the event
trigger_event[event_idx]();
Gate* gg = test_nl->get_gate_by_id(MIN_GATE_ID);
Module* mm = test_mod;
if (gg && mm)
{
for (ModulePin* mp : mm->get_pins())
std::cerr << "Module pin " << mp->get_id() << " <" << mp->get_name() << ">" << std::endl;
for (Endpoint* ep : gg->get_fan_in_endpoints())
std::cerr << "Gate in pin " << ep->get_pin()->get_id() << " <" << ep->get_pin()->get_name() << ">" << std::endl;
for (Endpoint* ep : gg->get_fan_out_endpoints())
std::cerr << "Gate out pin " << ep->get_pin()->get_id() << " <" << ep->get_pin()->get_name() << ">" << std::endl;
}

EXPECT_EQ(listener.get_event_count(), 1);
std::cerr << "Event " << event_idx << " <" << std::get<2>(listener.get_last_parameters()) << "> exp=<" << std::get<2>(expected_parameter[event_idx]) << ">" << std::endl;
EXPECT_EQ(listener.get_last_parameters(), expected_parameter[event_idx]);

// Unregister the callback
Expand Down

0 comments on commit 6f97b6d

Please sign in to comment.