From 977fc5951c013d7f71425ad1e8884d81f955a60c Mon Sep 17 00:00:00 2001 From: maelemiel Date: Thu, 6 Mar 2025 15:41:58 +0100 Subject: [PATCH 1/5] [FIX] skip displaying inputs for boolean and undefined states in Circuit --- src/Circuit.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Circuit.cpp b/src/Circuit.cpp index cb80bf1..0330478 100644 --- a/src/Circuit.cpp +++ b/src/Circuit.cpp @@ -55,6 +55,8 @@ void nts::Circuit::displayInputs() const { for (const auto &component : _components) { + if (component.first == "true" || component.first == "false" || component.first == "undefined") + continue; auto clock = dynamic_cast(component.second.get()); if (clock != nullptr) { From 132da3b8c2236975f0c74b61fbc9ebd3db2f4279 Mon Sep 17 00:00:00 2001 From: maelemiel Date: Fri, 7 Mar 2025 15:49:07 +0100 Subject: [PATCH 2/5] [TEST] enable and complete toggle simulation tests for ClockComponent --- tests/test_clock.cpp | 52 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/test_clock.cpp b/tests/test_clock.cpp index 7a8e846..07a4978 100644 --- a/tests/test_clock.cpp +++ b/tests/test_clock.cpp @@ -75,37 +75,37 @@ Test(ClockComponent, setLink_invalid) { cr_assert_throw(clock.setLink(2, mock, 1), std::invalid_argument, "Setting link to invalid pin should throw"); } -// Test(ClockComponent, simulate_toggle) { -// nts::ClockComponent clock("clock"); +Test(ClockComponent, simulate_toggle) { + nts::ClockComponent clock("clock"); -// // Set initial state -// clock.setState(nts::TRUE); -// cr_assert_eq(clock.getState(), nts::TRUE, "Initial state should be TRUE"); + // Set initial state + clock.setState(nts::TRUE); + cr_assert_eq(clock.getState(), nts::TRUE, "Initial state should be TRUE"); -// // First simulate call should toggle state -// clock.simulate(1); -// cr_assert_eq(clock.getState(), nts::FALSE, "State should toggle to FALSE after simulate"); + // First simulate call should toggle state + clock.simulate(1); + cr_assert_eq(clock.getState(), nts::FALSE, "State should toggle to FALSE after simulate"); -// // Second simulate call should toggle state again -// clock.simulate(2); -// cr_assert_eq(clock.getState(), nts::TRUE, "State should toggle to TRUE after simulate"); + // Second simulate call should toggle state again + clock.simulate(2); + cr_assert_eq(clock.getState(), nts::TRUE, "State should toggle to TRUE after simulate"); -// // Simulate with same tick should not change state -// clock.simulate(2); -// cr_assert_eq(clock.getState(), nts::TRUE, "State should not change when tick is the same"); -// } + // Simulate with same tick should not change state + clock.simulate(2); + cr_assert_eq(clock.getState(), nts::TRUE, "State should not change when tick is the same"); +} -// Test(ClockComponent, compute) { -// nts::ClockComponent clock("clock"); +Test(ClockComponent, compute) { + nts::ClockComponent clock("clock"); -// // Set initial state -// clock.setState(nts::FALSE); + // Set initial state + clock.setState(nts::FALSE); -// // Compute should return the current state after simulation -// nts::Tristate result = clock.compute(1); -// cr_assert_eq(result, nts::TRUE, "Compute should toggle and return TRUE"); + // Compute should return the current state after simulation + nts::Tristate result = clock.compute(1); + cr_assert_eq(result, nts::TRUE, "Compute should toggle and return TRUE"); -// // Next compute should toggle again -// result = clock.compute(2); -// cr_assert_eq(result, nts::FALSE, "Compute should toggle and return FALSE"); -// } + // Next compute should toggle again + result = clock.compute(2); + cr_assert_eq(result, nts::FALSE, "Compute should toggle and return FALSE"); +} From 9579dad69aa14cfba6e0d1a5b14af7d1b2b434a6 Mon Sep 17 00:00:00 2001 From: Samuel Giret Date: Fri, 7 Mar 2025 16:28:10 +0100 Subject: [PATCH 3/5] [FEATURE] add isTrueFalseComponent function and update Circuit and OutputComponent to utilize it --- include/IComponent.hpp | 2 ++ src/Circuit.cpp | 4 ++++ src/main.cpp | 6 ++++++ src/specialComponents/Output.cpp | 2 ++ 4 files changed, 14 insertions(+) diff --git a/include/IComponent.hpp b/include/IComponent.hpp index 712a125..d6cc958 100644 --- a/include/IComponent.hpp +++ b/include/IComponent.hpp @@ -31,4 +31,6 @@ namespace nts }; } +bool isTrueFalseComponent(const nts::IComponent& component); + #endif // ICOMPONENT_HPP \ No newline at end of file diff --git a/src/Circuit.cpp b/src/Circuit.cpp index fa796e0..5179f07 100644 --- a/src/Circuit.cpp +++ b/src/Circuit.cpp @@ -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(component.second.get()); diff --git a/src/main.cpp b/src/main.cpp index 19e61f4..14faa6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,12 @@ #include "../include/Circuit.hpp" #include "../include/ShellLoop.hpp" +bool isTrueFalseComponent(const nts::IComponent& component) +{ + return dynamic_cast(&component) != nullptr + || dynamic_cast(&component) != nullptr; +} + void displayComponents(const std::vector& lines) { std::cout << "\n=== COMPONENTS ===\n"; bool inChipsetSection = false; diff --git a/src/specialComponents/Output.cpp b/src/specialComponents/Output.cpp index 54988f4..1285edf 100644 --- a/src/specialComponents/Output.cpp +++ b/src/specialComponents/Output.cpp @@ -34,4 +34,6 @@ void nts::OutputComponent::setLink(std::size_t pin, std::shared_ptr_pins[pin - 1] = other; + if (isTrueFalseComponent(*other)) + this->setState(other->compute(0)); } \ No newline at end of file From a618a3318f2c7488e4980e48a625a38bad877bd4 Mon Sep 17 00:00:00 2001 From: Samuel Giret Date: Fri, 7 Mar 2025 17:36:55 +0100 Subject: [PATCH 4/5] [FIX] initialize lastTick to 1 in AComponent constructor and simplify OutputComponent setLink method --- src/AComponent.cpp | 2 +- src/specialComponents/Output.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/AComponent.cpp b/src/AComponent.cpp index 6c380b5..abf6e0b 100644 --- a/src/AComponent.cpp +++ b/src/AComponent.cpp @@ -9,7 +9,7 @@ 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>(nbPins); } diff --git a/src/specialComponents/Output.cpp b/src/specialComponents/Output.cpp index 1285edf..c98484e 100644 --- a/src/specialComponents/Output.cpp +++ b/src/specialComponents/Output.cpp @@ -34,6 +34,5 @@ void nts::OutputComponent::setLink(std::size_t pin, std::shared_ptr_pins[pin - 1] = other; - if (isTrueFalseComponent(*other)) - this->setState(other->compute(0)); + this->setState(other->compute(0)); } \ No newline at end of file From 9ea68f3f8f218773ea09cc69934a9ab82dfc78de Mon Sep 17 00:00:00 2001 From: Samuel Giret Date: Fri, 7 Mar 2025 17:57:38 +0100 Subject: [PATCH 5/5] [FEATURE] implement isTrueFalseComponent function and initialize circuit simulation in ShellLoop and tests --- src/AComponent.cpp | 10 +++++++++- src/ShellLoop.cpp | 1 + src/main.cpp | 6 ------ src/specialComponents/Output.cpp | 1 - tests/test_elementaryComponents.cpp | 8 ++++++++ tests/test_special_components.cpp | 3 ++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/AComponent.cpp b/src/AComponent.cpp index abf6e0b..96876f1 100644 --- a/src/AComponent.cpp +++ b/src/AComponent.cpp @@ -6,6 +6,8 @@ */ #include "../include/AComponent.hpp" +#include "../include/specialComponents/True.hpp" +#include "../include/specialComponents/False.hpp" namespace nts { @@ -33,4 +35,10 @@ namespace nts { _state = state; } -} \ No newline at end of file +} + +bool isTrueFalseComponent(const nts::IComponent& component) +{ + return dynamic_cast(&component) != nullptr + || dynamic_cast(&component) != nullptr; +} diff --git a/src/ShellLoop.cpp b/src/ShellLoop.cpp index 7271618..4965e46 100644 --- a/src/ShellLoop.cpp +++ b/src/ShellLoop.cpp @@ -82,6 +82,7 @@ void nts::ShellLoop::run() { std::string input; + _circuit.simulate(0); while (_running) { std::cout << "> "; if (!std::getline(std::cin, input)) { diff --git a/src/main.cpp b/src/main.cpp index 14faa6e..19e61f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,12 +15,6 @@ #include "../include/Circuit.hpp" #include "../include/ShellLoop.hpp" -bool isTrueFalseComponent(const nts::IComponent& component) -{ - return dynamic_cast(&component) != nullptr - || dynamic_cast(&component) != nullptr; -} - void displayComponents(const std::vector& lines) { std::cout << "\n=== COMPONENTS ===\n"; bool inChipsetSection = false; diff --git a/src/specialComponents/Output.cpp b/src/specialComponents/Output.cpp index c98484e..54988f4 100644 --- a/src/specialComponents/Output.cpp +++ b/src/specialComponents/Output.cpp @@ -34,5 +34,4 @@ void nts::OutputComponent::setLink(std::size_t pin, std::shared_ptr_pins[pin - 1] = other; - this->setState(other->compute(0)); } \ No newline at end of file diff --git a/tests/test_elementaryComponents.cpp b/tests/test_elementaryComponents.cpp index 66fa578..fbcd6ef 100644 --- a/tests/test_elementaryComponents.cpp +++ b/tests/test_elementaryComponents.cpp @@ -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(circuit.getComponent("In1").get()), nts::TRUE); circuit.setInputState(*dynamic_cast(circuit.getComponent("In2").get()), nts::TRUE); @@ -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(circuit.getComponent("In1").get()), nts::TRUE); circuit.setInputState(*dynamic_cast(circuit.getComponent("In2").get()), nts::FALSE); circuit.simulate(1); @@ -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(circuit.getComponent("In1").get()), nts::TRUE); circuit.simulate(1); nts::Tristate state = circuit.compute("Out1"); @@ -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(circuit.getComponent("In1").get()), nts::TRUE); circuit.setInputState(*dynamic_cast(circuit.getComponent("In2").get()), nts::FALSE); circuit.simulate(1); diff --git a/tests/test_special_components.cpp b/tests/test_special_components.cpp index 98992a5..ec90729 100644 --- a/tests/test_special_components.cpp +++ b/tests/test_special_components.cpp @@ -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(circuit.getComponent("input1").get()), nts::TRUE); circuit.setInputState(*dynamic_cast(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")); } \ No newline at end of file