Skip to content
Merged
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ SRC = $(SRC_DIR)/Parser.cpp \
$(SRC_DIR)/specialComponents/Undefined.cpp \
$(SRC_DIR)/elementaryComponents/ANDComponent.cpp \
$(SRC_DIR)/elementaryComponents/NOTComponent.cpp \
$(SRC_DIR)/elementaryComponents/ORComponent.cpp \
$(SRC_DIR)/elementaryComponents/ORComponent.cpp \
$(SRC_DIR)/elementaryComponents/XORComponent.cpp \
$(SRC_DIR)/elementaryComponents/NANDComponent.cpp \
$(SRC_DIR)/elementaryComponents/NORComponent.cpp \
$(SRC_DIR)/gateComponents/4001Component.cpp \
$(SRC_DIR)/gateComponents/4011Component.cpp \
$(SRC_DIR)/gateComponents/4030Component.cpp \
$(SRC_DIR)/gateComponents/4069Component.cpp \
$(SRC_DIR)/gateComponents/4071Component.cpp \
$(SRC_DIR)/gateComponents/4081Component.cpp \
$(SRC_DIR)/ShellLoop.cpp \
$(SRC_DIR)/AComponent.cpp \
$(SRC_DIR)/Circuit.cpp
Expand Down
16 changes: 9 additions & 7 deletions include/Factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ namespace nts
std::shared_ptr<nts::IComponent> createOR(const std::string &name) const;
std::shared_ptr<nts::IComponent> createNOT(const std::string &name) const;
std::shared_ptr<nts::IComponent> createXOR(const std::string &name) const;

std::shared_ptr<nts::IComponent> create4001() const;
std::shared_ptr<nts::IComponent> create4011() const;
std::shared_ptr<nts::IComponent> create4030() const;
std::shared_ptr<nts::IComponent> create4069() const;
std::shared_ptr<nts::IComponent> create4071() const;
std::shared_ptr<nts::IComponent> create4081() const;
std::shared_ptr<nts::IComponent> createNOR(const std::string &name) const;
std::shared_ptr<nts::IComponent> createNAND(const std::string &name) const;

std::shared_ptr<nts::IComponent> create4001(const std::string &name) const;
std::shared_ptr<nts::IComponent> create4011(const std::string &name) const;
std::shared_ptr<nts::IComponent> create4030(const std::string &name) const;
std::shared_ptr<nts::IComponent> create4069(const std::string &name) const;
std::shared_ptr<nts::IComponent> create4071(const std::string &name) const;
std::shared_ptr<nts::IComponent> create4081(const std::string &name) const;

std::shared_ptr<nts::IComponent> create4008() const;
std::shared_ptr<nts::IComponent> create4013() const;
Expand Down
2 changes: 0 additions & 2 deletions src/Circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ void nts::Circuit::linkComponents(const std::string &name1, const std::string &n

void nts::Circuit::setInputState(nts::InputComponent &input, nts::Tristate state) const
{
std::shared_ptr<nts::IComponent> tempComponent;

if (state == nts::UNDEFINED)
input.setLink(1, _components.at("undefined"), 1);
else if (state == nts::TRUE)
Expand Down
64 changes: 64 additions & 0 deletions src/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#include "./elementaryComponents/NOTComponent.hpp"
#include "./elementaryComponents/ORComponent.hpp"
#include "./elementaryComponents/XORComponent.hpp"
#include "./elementaryComponents/NANDComponent.hpp"
#include "./elementaryComponents/NORComponent.hpp"
#include "./gateComponents/4001Component.hpp"
#include "./gateComponents/4011Component.hpp"
#include "./gateComponents/4030Component.hpp"
#include "./gateComponents/4069Component.hpp"
#include "./gateComponents/4071Component.hpp"
#include "./gateComponents/4081Component.hpp"
#include "../include/specialComponents/Clock.hpp"
#include "../include/specialComponents/Input.hpp"
#include "../include/specialComponents/Output.hpp"
Expand Down Expand Up @@ -39,10 +47,61 @@ std::shared_ptr<nts::IComponent> nts::Factory::createComponent(const std::string
return createXOR(name);
else if (type == "not")
return createNOT(name);
else if (type == "nand")
return createNAND(name);
else if (type == "nor")
return createNOR(name);
else if (type == "4001")
return create4001(name);
else if (type == "4011")
return create4011(name);
else if (type == "4030")
return create4030(name);
else if (type == "4069")
return create4069(name);
else if (type == "4071")
return create4071(name);
else if (type == "4081")
return create4081(name);
else
return nullptr;
}

std::shared_ptr<nts::IComponent> nts::Factory::create4081(const std::string &name) const
{
return std::make_shared<nts::Component4081>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::create4071(const std::string &name) const
{
return std::make_shared<nts::Component4071>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::create4069(const std::string &name) const
{
return std::make_shared<nts::Component4069>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::create4011(const std::string &name) const
{
return std::make_shared<nts::Component4011>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::create4001(const std::string &name) const
{
return std::make_shared<nts::Component4001>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::createNAND(const std::string &name) const
{
return std::make_shared<nts::NANDComponent>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::createNOR(const std::string &name) const
{
return std::make_shared<nts::NORComponent>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::createUndefined(const std::string &name) const
{
return std::make_shared<nts::UndefinedComponent>(name);
Expand Down Expand Up @@ -91,4 +150,9 @@ std::shared_ptr<nts::IComponent> nts::Factory::createXOR(const std::string &name
std::shared_ptr<nts::IComponent> nts::Factory::createNOT(const std::string &name) const
{
return std::make_shared<nts::NOTComponent>(name);
}

std::shared_ptr<nts::IComponent> nts::Factory::create4030(const std::string &name) const
{
return std::make_shared<nts::Component4030>(name);
}
29 changes: 15 additions & 14 deletions src/elementaryComponents/ANDComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ namespace nts
throw std::invalid_argument("Pin does not exist");
}

Tristate ANDComponent::calculateState(nts::Tristate first, nts::Tristate second)
{
if (first == nts::FALSE || second == nts::FALSE)
return nts::FALSE;
else if (first == nts::UNDEFINED || second == nts::UNDEFINED)
return nts::UNDEFINED;
else
return nts::TRUE;
}

void ANDComponent::simulate(std::size_t tick)
{
nts::Tristate first;
nts::Tristate second;
nts::Tristate first = UNDEFINED;
nts::Tristate second = UNDEFINED;

if (tick == this->_lastTick)
return;
this->_lastTick = tick;
if (this->_pins[0].lock() == nullptr)
first = nts::UNDEFINED;
else
if (this->_pins[0].lock() != nullptr)
first = this->_pins[0].lock()->compute(tick);
if (this->_pins[1].lock() == nullptr)
second = nts::UNDEFINED;
else
if (this->_pins[1].lock() != nullptr)
second = this->_pins[1].lock()->compute(tick);
if (first == nts::FALSE || second == nts::FALSE)
this->setState(nts::FALSE);
else if (first == nts::UNDEFINED || second == nts::UNDEFINED)
this->setState(nts::UNDEFINED);
else
this->setState(nts::TRUE);
this->setState(calculateState(first, second));
}

nts::Tristate ANDComponent::compute(std::size_t tick)
Expand Down
1 change: 1 addition & 0 deletions src/elementaryComponents/ANDComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace nts
void setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin);
void simulate(std::size_t tick) override;
nts::Tristate compute(std::size_t tick) override;
static nts::Tristate calculateState(nts::Tristate first, nts::Tristate second);
};
}

Expand Down
54 changes: 54 additions & 0 deletions src/elementaryComponents/NANDComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "NANDComponent.hpp"
#include <iostream>

namespace nts
{
NANDComponent::NANDComponent(const std::string &name) : AComponent(name, 3)
{
this->setState(nts::UNDEFINED);
}

NANDComponent::~NANDComponent()
{
}

void NANDComponent::setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin)
{
(void)otherPin;
if (pin >= 1 && pin <= 3)
this->_pins[pin - 1] = other;
else
throw std::invalid_argument("Pin does not exist");
}

Tristate NANDComponent::calculateState(nts::Tristate first, nts::Tristate second)
{
if (first == nts::FALSE || second == nts::FALSE)
return nts::TRUE;
else if (first == nts::UNDEFINED || second == nts::UNDEFINED)
return nts::UNDEFINED;
else
return nts::FALSE;
}

void NANDComponent::simulate(std::size_t tick)
{
nts::Tristate first = UNDEFINED;
nts::Tristate second = UNDEFINED;

if (tick == this->_lastTick)
return;
this->_lastTick = tick;
if (this->_pins[0].lock() != nullptr)
first = this->_pins[0].lock()->compute(tick);
if (this->_pins[1].lock() != nullptr)
second = this->_pins[1].lock()->compute(tick);
this->setState(calculateState(first, second));
}

nts::Tristate NANDComponent::compute(std::size_t tick)
{
this->simulate(tick);
return this->getState();
}
}
22 changes: 22 additions & 0 deletions src/elementaryComponents/NANDComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef NANDCOMPONENT_HPP
#define NANDCOMPONENT_HPP

#include <string>
#include <memory>
#include "../include/AComponent.hpp"

namespace nts
{
class NANDComponent : public AComponent
{
public:
NANDComponent(const std::string &name = "nand");
~NANDComponent();
void setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin);
void simulate(std::size_t tick) override;
nts::Tristate compute(std::size_t tick) override;
static nts::Tristate calculateState(nts::Tristate first, nts::Tristate second);
};
}

#endif // NANDCOMPONENT_HPP
54 changes: 54 additions & 0 deletions src/elementaryComponents/NORComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "NORComponent.hpp"
#include <iostream>

namespace nts
{
NORComponent::NORComponent(const std::string &name) : AComponent(name, 3)
{
this->setState(nts::UNDEFINED);
}

NORComponent::~NORComponent()
{
}

void NORComponent::setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin)
{
(void)otherPin;
if (pin >= 1 && pin <= 3)
this->_pins[pin - 1] = other;
else
throw std::invalid_argument("Pin does not exist");
}

Tristate NORComponent::calculateState(nts::Tristate first, nts::Tristate second)
{
if (first == nts::TRUE || second == nts::TRUE)
return nts::FALSE;
else if (first == nts::UNDEFINED || second == nts::UNDEFINED)
return nts::UNDEFINED;
else
return nts::TRUE;
}

void NORComponent::simulate(std::size_t tick)
{
nts::Tristate first = UNDEFINED;
nts::Tristate second = UNDEFINED;

if (tick == this->_lastTick)
return;
this->_lastTick = tick;
if (this->_pins[0].lock() != nullptr)
first = this->_pins[0].lock()->compute(tick);
if (this->_pins[1].lock() != nullptr)
second = this->_pins[1].lock()->compute(tick);
this->setState(calculateState(first, second));
}

nts::Tristate NORComponent::compute(std::size_t tick)
{
this->simulate(tick);
return this->getState();
}
}
22 changes: 22 additions & 0 deletions src/elementaryComponents/NORComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef NORCOMPONENT_HPP
#define NORCOMPONENT_HPP

#include <string>
#include <memory>
#include "../include/AComponent.hpp"

namespace nts
{
class NORComponent : public AComponent
{
public:
NORComponent(const std::string &name = "nor");
~NORComponent();
void setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin);
void simulate(std::size_t tick) override;
nts::Tristate compute(std::size_t tick) override;
static nts::Tristate calculateState(nts::Tristate first, nts::Tristate second);
};
}

#endif // NORCOMPONENT_HPP
18 changes: 15 additions & 3 deletions src/elementaryComponents/NOTComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ namespace nts
throw std::invalid_argument("Pin does not exist");
}

nts::Tristate NOTComponent::calculateState(nts::Tristate state)
{
if (state == nts::TRUE)
return nts::FALSE;
else if (state == nts::FALSE)
return nts::TRUE;
else
return nts::UNDEFINED;
}

void NOTComponent::simulate(std::size_t tick)
{
Tristate state = UNDEFINED;

if (tick == this->_lastTick)
return;
this->_lastTick = tick;
if (this->_pins[0].lock() == nullptr || this->_pins[0].lock()->compute(tick) == nts::UNDEFINED)
return;
this->setState(this->_pins[0].lock()->compute(tick) == nts::TRUE ? nts::FALSE : nts::TRUE);
if (this->_pins[0].lock() != nullptr)
state = this->_pins[0].lock()->compute(tick);
this->setState(calculateState(state));
}

nts::Tristate NOTComponent::compute(std::size_t tick)
Expand Down
1 change: 1 addition & 0 deletions src/elementaryComponents/NOTComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace nts
void setLink(std::size_t pin, std::shared_ptr<nts::IComponent> other, std::size_t otherPin);
void simulate(std::size_t tick) override;
nts::Tristate compute(std::size_t tick) override;
static nts::Tristate calculateState(nts::Tristate state);
};
}

Expand Down
Loading
Loading