-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNORComponent.cpp
More file actions
54 lines (47 loc) · 1.47 KB
/
NORComponent.cpp
File metadata and controls
54 lines (47 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();
}
}