Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔁 Handle the program execution #46

Merged
merged 14 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CXX_SOURCES = src/main.cpp \
src/components/XorComponent.cpp \
src/FileContainer.cpp \
src/Error.cpp \
src/Simulation.cpp \

# Compiler and linker settings
NAME = nanotekspice
Expand Down
31 changes: 15 additions & 16 deletions src/FileContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ nts::FileContainer::FileContainer(const std::string &filename)
this->_filename = filename;
this->_chipsets = std::vector<std::string>();
this->_links = std::vector<std::string>();
this->_pins = std::unordered_map<std::string, IComponent *>();
this->_pins = std::map<std::string, IComponent *>();
}

void nts::FileContainer::extractFileContent()
Expand Down Expand Up @@ -56,7 +56,7 @@ std::vector<std::string> nts::FileContainer::getLinks(void) const
return _links;
}

std::unordered_map<std::string, nts::IComponent *> nts::FileContainer::getMap(void) const
std::map<std::string, nts::IComponent *> nts::FileContainer::getMap(void) const
{
return _pins;
}
Expand All @@ -70,11 +70,11 @@ void nts::FileContainer::buildMap(ComponentFactory &factory)
type = strtok((char *)this->_chipsets[i].c_str(), " ");
name = strtok(NULL, " ");
if (name == NULL || type == NULL)
throw nts::Error("Invalid file format.");
if (!factory.isRegistered(name))
throw nts::Error("Component type does not exist.");
throw nts::Error("Invalid file format in the chipset section.");
if (!factory.isRegistered(type))
throw nts::Error("Component type: " + std::string(name) + " does not exist.");
if (this->_pins.find(name) != this->_pins.end())
throw nts::Error("Component name already exists.");
throw nts::Error("Component name: " + std::string(name) + " already exists.");
this->_pins[name] = factory.createComponent(type);
}
}
Expand All @@ -92,13 +92,12 @@ void nts::FileContainer::setlinks(void)
name2 = strtok(NULL, " ");
pin2 = strtok(NULL, " ");
if (name == NULL || pin == NULL || name2 == NULL || pin2 == NULL)
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the links section.");
if (this->_pins.find(name) == this->_pins.end())
throw nts::Error("First component name does not exist.");
throw nts::Error("First component name: " + std::string(name) + " does not exist.");
if (this->_pins[name2] == NULL)
throw nts::Error("Second component name does not exist.");
throw nts::Error("Second component name: " + std::string(name2) + " does not exist.");
this->_pins[name]->setLink(std::stoi(pin), *this->_pins[name2], std::stoi(pin2));
// The `setLink` method already has a check for the pin existence and if it is already linked
}
}

Expand All @@ -113,7 +112,7 @@ std::string nts::FileContainer::removeComments(std::string &content) const

void nts::FileContainer::extractChipsetsAndLinks(const std::string &content)
{
std::regex reg("(\\.chipsets:\n([a-zA-Z0-9_ #]+\n?)+\n)\n*(\\.links:\n([a-zA-Z0-9_: #]+\n?)+\n+)$");
std::regex reg("(\\.chipsets:\n([a-zA-Z0-9_ #]+\n?)+\n)\n*(\\.links:\n([a-zA-Z0-9_: #]+\n?)+)(\n+)?$");
std::smatch match;
std::string str1;
std::string str2;
Expand All @@ -124,7 +123,7 @@ void nts::FileContainer::extractChipsetsAndLinks(const std::string &content)
str2 = match[3].str();
this->fillLinks(str2);
} else {
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the chipset and/or links section.");
}
}

Expand All @@ -138,14 +137,14 @@ void nts::FileContainer::fillChipsets(std::string &str)
token = strtok((char *)str.c_str(), "\n");
token = strtok(NULL, "\n");
if (token == NULL)
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the chipset section.");
while (token != NULL) {
str2 = std::string(token);
if (std::regex_search(str2, match2, reg)) {
str2 = match2[1].str() + " " + match2[2].str();
this->_chipsets.push_back(str2);
} else {
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the chipset section.");
}
token = strtok(NULL, "\n");
}
Expand All @@ -161,14 +160,14 @@ void nts::FileContainer::fillLinks(std::string &str)
token = strtok((char *)str.c_str(), "\n");
token = strtok(NULL, "\n");
if (token == NULL)
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the links section.");
while (token != NULL) {
str2 = std::string(token);
if (std::regex_search(str2, match2, reg)) {
str2 = match2[1].str() + " " + match2[5].str() + " " + match2[6].str() + " " + match2[10].str();
this->_links.push_back(str2);
} else {
throw nts::Error("Invalid file format.");
throw nts::Error("Invalid file format in the links section.");
}
token = strtok(NULL, "\n");
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace nts

std::vector<std::string> getChipsets(void) const;
std::vector<std::string> getLinks(void) const;
std::unordered_map<std::string, IComponent *> getMap(void) const;
std::map<std::string, IComponent *> getMap(void) const;

void buildMap(ComponentFactory &factory);
void setlinks(void);
Expand All @@ -33,7 +33,7 @@ namespace nts
std::string _filename;
std::vector<std::string> _chipsets;
std::vector<std::string> _links;
std::unordered_map<std::string, IComponent *> _pins;
std::map<std::string, IComponent *> _pins;

std::string removeComments(std::string &content) const;
void extractChipsetsAndLinks(const std::string &content);
Expand Down
15 changes: 12 additions & 3 deletions src/IComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@

#include "IComponent.hpp"

using namespace nts;

Tristate nts::operator!(Tristate const &tristate)
nts::Tristate nts::operator!(Tristate const &tristate)
{
if (tristate == UNDEFINED)
return UNDEFINED;
return (tristate == TRUE) ? FALSE : TRUE;
}

std::ostream &operator<<(std::ostream &os, nts::Tristate const &a)
{
if (a == nts::UNDEFINED)
os << "U";
else if (a == nts::TRUE)
os << "1";
else
os << "0";
return os;
}
3 changes: 3 additions & 0 deletions src/IComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once
#include <cstddef>
#include <ostream>

namespace nts {
enum Tristate {
Expand All @@ -29,3 +30,5 @@ namespace nts {
[[nodiscard]] virtual IComponent *clone() const = 0;
};
}

std::ostream &operator<<(std::ostream &os, nts::Tristate const &a);
175 changes: 175 additions & 0 deletions src/Simulation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** execSimulation
*/

#include "Simulation.hpp"
#include "Error.hpp"
#include "components/InputComponent.hpp"
#include "components/ClockComponent.hpp"
// #include "OutputComponent.hpp"
// This include is in comments since the Output component is not implemented yet
#include <string.h>
#include <signal.h>
#include <fstream>

nts::Simulation::Simulation(std::map<std::string, IComponent *> pins)
{
this->_pins = pins;
this->_exit = false;
this->_commands["exit\n"] = &nts::Simulation::exit;
this->_commands["display\n"] = &nts::Simulation::display;
this->_commands["simulate\n"] = &nts::Simulation::simulate;
this->_commands["loop\n"] = &nts::Simulation::loop;
this->_ticks = 0;
}

nts::Simulation::~Simulation()
{
}

void nts::Simulation::execSimulation()
{
char *line = NULL;
size_t len = 0;

while (!this->_exit) {
std::cout << "> " << std::flush;
if (getline(&line, &len, stdin) == -1)
return;
if (isKnownCommand(line))
continue;
handleInputs(line);
}
}

void nts::Simulation::stopLoop()
{
this->_loop = false;
}

bool nts::Simulation::isKnownCommand(char *line)
{
std::string str = line;
if (this->_commands[line] != nullptr) {
(this->*_commands[line])();
return true;
}
return false;
}

void nts::Simulation::exit()
{
this->_exit = true;
}

void nts::Simulation::display()
{
std::cout << "tick: " << this->_ticks << std::endl;
std::cout << "input(s):" << std::endl;
for (auto &pin : this->_pins) {
if (dynamic_cast<nts::Components::InputComponent *>(pin.second) != nullptr)
std::cout << " " << pin.first << ": " << pin.second->compute(1) << std::endl;
}
std::cout << "output(s):" << std::endl;
// for (auto &pin : this->_pins) {
// if (dynamic_cast<nts::Components::OutputComponent *>(pin.second) == nullptr)
// std::cout << " " << pin.first << ": " << pin.second->compute(1) << std::endl;
// }
// This part is in comments since the Output component is not implemented yet
}

void nts::Simulation::simulate()
{
std::unordered_map<std::string, IComponent *> pins;
// for (auto &pin : this->_pins) {
// if (dynamic_cast<nts::Components::OutputComponent *>(pin.second) != nullptr)
// pins.push_back(pin);
// else
// pin.second->simulate(1);
// }
// for (auto &pin : pins) {
// pin.second->simulate(1);
// }
// This part is in comments since the Output component is not implemented yet
this->_ticks++;
}

static void handleLoop(void *arg)
{
static nts::Simulation *sim;

if (arg == NULL && sim == NULL)
return;
if (arg != NULL)
sim = (nts::Simulation *)arg;
if (arg == NULL)
sim->stopLoop();
}

static void signalHandler(int signum)
{
if (signum == SIGINT)
handleLoop(NULL);
}

void nts::Simulation::loop()
{
struct sigaction sigIntHandler;

sigIntHandler.sa_handler = signalHandler;
this->_loop = true;
handleLoop(this);
while (this->_loop) {
simulate();
display();
sigaction(SIGINT, &sigIntHandler, NULL);
}
sigIntHandler.sa_handler = SIG_DFL;
sigaction(SIGINT, &sigIntHandler, NULL);
}

void nts::Simulation::handleInputs(char *line)
{
std::stringstream ss(line);
std::string name;
std::string value;

if (!getline(ss, name, '=')) {
return;
}
if (this->_pins[name] == nullptr ||
(dynamic_cast<nts::Components::InputComponent *>(this->_pins[name]) == nullptr &&
dynamic_cast<nts::Components::ClockComponent *>(this->_pins[name]) == nullptr)) {
return;
}
if (!getline(ss, value)) {
return;
}
if (value != "0" && value != "1" && value != "U") {
return;
}
setValues(name, value);
}

void nts::Simulation::setValues(std::string name, std::string value)
{
if (dynamic_cast<nts::Components::ClockComponent *>(this->_pins[name]) != nullptr) {
if (value == "0")
dynamic_cast<nts::Components::ClockComponent *>(this->_pins[name])->setValue(nts::FALSE);
else if (value == "1")
dynamic_cast<nts::Components::ClockComponent *>(this->_pins[name])->setValue(nts::TRUE);
else
dynamic_cast<nts::Components::ClockComponent *>(this->_pins[name])->setValue(nts::UNDEFINED);
}
if (dynamic_cast<nts::Components::InputComponent *>(this->_pins[name]) != nullptr) {
if (value == "0")
dynamic_cast<nts::Components::InputComponent *>(this->_pins[name])->setValue(nts::FALSE);
else if (value == "1")
dynamic_cast<nts::Components::InputComponent *>(this->_pins[name])->setValue(nts::TRUE);
else
dynamic_cast<nts::Components::InputComponent *>(this->_pins[name])->setValue(nts::UNDEFINED);
}
}
37 changes: 37 additions & 0 deletions src/Simulation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
** EPITECH PROJECT, 2024
** NanoTekSpice
** File description:
** Simulation
*/

#pragma once

#include "FileContainer.hpp"

namespace nts {
class Simulation {
public:
Simulation(std::map<std::string, IComponent *>);
~Simulation();

void execSimulation();
void stopLoop(void);
protected:
private:
std::map<std::string, IComponent *> _pins;
std::unordered_map<std::string, void (nts::Simulation::*)()> _commands;
bool _exit;
size_t _ticks;
bool _loop;

bool isKnownCommand(char *line);
void exit(void);
void display(void);
void simulate(void);
void loop(void);

void handleInputs(char *line);
void setValues(std::string name, std::string value);
};
};
4 changes: 4 additions & 0 deletions src/components/ClockComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ void ClockComponent::simulate(std::size_t tick)
_value = !_value;
}
}

void ClockComponent::setValue(nts::Tristate value) {
_value = value;
}
Loading
Loading