Skip to content
Merged
2 changes: 2 additions & 0 deletions include/IComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ namespace nts
};
}

bool isTrueFalseComponent(const nts::IComponent& component);

#endif // ICOMPONENT_HPP
12 changes: 10 additions & 2 deletions src/AComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/

#include "../include/AComponent.hpp"
#include "../include/specialComponents/True.hpp"
#include "../include/specialComponents/False.hpp"

namespace nts
{
AComponent::AComponent(const std::string &name, std::size_t nbPins) : _name(name), _lastTick(0), _nbPins(nbPins), _state(nts::UNDEFINED)
AComponent::AComponent(const std::string &name, std::size_t nbPins) : _name(name), _lastTick(1), _nbPins(nbPins), _state(nts::UNDEFINED)
{
_pins = std::vector<std::weak_ptr<nts::IComponent>>(nbPins);
}
Expand All @@ -33,4 +35,10 @@ namespace nts
{
_state = state;
}
}
}

bool isTrueFalseComponent(const nts::IComponent& component)
{
return dynamic_cast<const nts::TrueComponent*>(&component) != nullptr
|| dynamic_cast<const nts::FalseComponent*>(&component) != nullptr;
}
4 changes: 4 additions & 0 deletions src/Circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ void nts::Circuit::displayInputs() const
{
for (const auto &component : _components)
{
//check if it is true/false or if it is an actual input/clock
if (isTrueFalseComponent(*component.second))
continue;

if (component.first == "true" || component.first == "false" || component.first == "undefined")
continue;
auto clock = dynamic_cast<nts::ClockComponent *>(component.second.get());
Expand Down
1 change: 1 addition & 0 deletions src/ShellLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void nts::ShellLoop::run()
{
std::string input;

_circuit.simulate(0);
while (_running) {
std::cout << "> ";
if (!std::getline(std::cin, input)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/test_elementaryComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Test(Circuit, and_gate_behavior) {
circuit.linkComponents("In2", "And1", 1, 2);
circuit.linkComponents("And1", "Out1", 3, 1);

circuit.simulate(0); // init components' values

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In1").get()), nts::TRUE);

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In2").get()), nts::TRUE);
Expand Down Expand Up @@ -98,6 +100,8 @@ Test(Circuit, or_gate_behavior) {
circuit.linkComponents("In2", "Or1", 1, 2);
circuit.linkComponents("Or1", "Out1", 3, 1);

circuit.simulate(0); // init components' values

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In1").get()), nts::TRUE);
circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In2").get()), nts::FALSE);
circuit.simulate(1);
Expand Down Expand Up @@ -158,6 +162,8 @@ Test(Circuit, not_gate_behavior) {
circuit.linkComponents("In1", "Not1", 1, 1);
circuit.linkComponents("Not1", "Out1", 2, 1);

circuit.simulate(0); // init components' values

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In1").get()), nts::TRUE);
circuit.simulate(1);
nts::Tristate state = circuit.compute("Out1");
Expand All @@ -180,6 +186,8 @@ Test(Circuit, xor_gate_behavior) {
circuit.linkComponents("In2", "Xor1", 1, 2);
circuit.linkComponents("Xor1", "Out1", 3, 1);

circuit.simulate(0); // init components' values

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In1").get()), nts::TRUE);
circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("In2").get()), nts::FALSE);
circuit.simulate(1);
Expand Down
3 changes: 2 additions & 1 deletion tests/test_special_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ Test(Circuit, and_behavior)
circuit.linkComponents("input1", "and", 1, 1);
circuit.linkComponents("input2", "and", 1, 2);
circuit.linkComponents("and", "output", 3, 1);
circuit.simulate(0); //done in the shellLoop to init components' values

circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("input1").get()), nts::TRUE);
circuit.setInputState(*dynamic_cast<nts::InputComponent*>(circuit.getComponent("input2").get()), nts::TRUE);
circuit.simulate(1);
cr_assert_eq(circuit.compute("output"), nts::TRUE, "Output should be TRUE");
cr_assert_eq(circuit.compute("output"), nts::TRUE, "Output should be TRUE but is %d", circuit.compute("output"));
}
Loading