update to c++17 and bug fixes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
project(riscv_emulator)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(riscv_emulator
|
||||
src/main.cpp
|
||||
|
||||
@@ -4,12 +4,26 @@
|
||||
|
||||
#include "Manager.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <filesystem>
|
||||
|
||||
#include "components/Alu.h"
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
Manager::Manager(std::string path): m_path(std::move(path)) {
|
||||
m_programFile.open(path);
|
||||
std::cout << "Öffne Quellcode Datei: '" << m_path << "'" << "\n" << std::flush;
|
||||
if (!fs::exists(m_path)) {
|
||||
std::cerr << "Datei existiert nicht: " << m_path << "\n" << std::flush;
|
||||
return;
|
||||
}
|
||||
m_programFile.open(m_path);
|
||||
if (!m_programFile.is_open()) {
|
||||
std::cerr << "Datei konnte nicht geöffnet werden: " << m_path << "\n" << std::flush;
|
||||
std::cerr << "fail (z.B. Zugriffsrechte): " << m_programFile.fail() << "\n" << std::flush;
|
||||
std::cerr << "bad (schwerwiegender Fehler): " << m_programFile.bad() << "\n" << std::flush;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Manager::~Manager() {
|
||||
@@ -24,6 +38,8 @@ Manager *Manager::getInstance(const std::string &program_path) {
|
||||
int Manager::run() {
|
||||
std::string line;
|
||||
ProgramLoader::getInstance()->indexFile(m_programFile);
|
||||
m_programFile.clear();
|
||||
m_programFile.seekg(0);
|
||||
while (std::getline(m_programFile, line)) {
|
||||
auto lineVector = ProgramLoader::parseLine(line);
|
||||
Alu::calculate(lineVector);
|
||||
|
||||
@@ -34,14 +34,15 @@ ProgramLoader *ProgramLoader::getInstance() {
|
||||
}
|
||||
|
||||
void ProgramLoader::indexFile(std::ifstream &m_programFile) {
|
||||
m_programFile.clear();
|
||||
m_programFile.seekg(0);
|
||||
std::string line;
|
||||
int lineNumber = 1;
|
||||
/// Parse Zeile für Zeile
|
||||
while (std::getline(m_programFile, line)) {
|
||||
auto lineVector = parseLine(line);
|
||||
const auto first = lineVector.begin();
|
||||
/// Sobald ein Label gefunden wurde, speichere die Zeile
|
||||
if (first->at(first->length() - 1) == ':') {
|
||||
if (const auto first = lineVector.begin(); first->at(first->length() - 1) == ':') {
|
||||
m_labels[first->substr(0, first->length() - 1)] = lineNumber;
|
||||
}
|
||||
lineNumber++;
|
||||
|
||||
@@ -17,11 +17,11 @@ void Alu::calculate(const std::vector<std::string> &commandVector) {
|
||||
/// Wandle Vektor[2] (das zweite Argument) nur um, falls es kein SW/LW Argument ist
|
||||
int arg2 = 0;
|
||||
if (commandVector.at(2).find('(') != std::string::npos) {
|
||||
arg2 = parseArgument(commandVector.at(2));
|
||||
arg2 = parseAddress(commandVector.at(2));
|
||||
}
|
||||
/// Wandle Vektor[3] (das dritte Argument) nur um, falls dieser existiert
|
||||
int arg3 = 0;
|
||||
if (!commandVector.at(3).empty()) {
|
||||
if (commandVector.size() > 3) {
|
||||
arg3 = parseArgument(commandVector.at(3));
|
||||
}
|
||||
|
||||
@@ -66,11 +66,11 @@ void Alu::calculate(const std::vector<std::string> &commandVector) {
|
||||
const auto result = register1 | immediate;
|
||||
Register::getInstance().setRegister(arg1, result);
|
||||
} else if (command == "lw") {
|
||||
const auto address = parseAddress(commandVector.at(2));
|
||||
const auto address = arg2;
|
||||
const auto data = Memory::getInstance()->load(address);
|
||||
Register::getInstance().setRegister(arg1, data);
|
||||
} else if (command == "sw") {
|
||||
const auto address = parseAddress(commandVector.at(2));
|
||||
const auto address = arg2;
|
||||
const auto data = Register::getInstance().getRegister(arg1);
|
||||
Memory::getInstance()->store(address, data);
|
||||
}
|
||||
@@ -89,10 +89,9 @@ int Alu::parseAddress(const std::string &argument) {
|
||||
/// Finde die Position von '(', trenne die den String dort und
|
||||
/// addiere die umgewandelten Integer zur Adresse
|
||||
/// Bsp: 0(x1)
|
||||
const size_t pos = argument.find('(');
|
||||
if (pos != std::string::npos) {
|
||||
const auto immediate = std::stoi(argument.substr(0, pos));
|
||||
const auto register1 = std::stoi(argument.substr(pos + 1));
|
||||
if (const size_t pos = argument.find('('); pos != std::string::npos) {
|
||||
const auto immediate = std::stoi(argument.substr(0, pos + 1));
|
||||
const auto register1 = std::stoi(argument.substr(pos + 2));
|
||||
const auto address = immediate + register1;
|
||||
return address;
|
||||
}
|
||||
|
||||
@@ -5,5 +5,7 @@
|
||||
#include "Manager.h"
|
||||
|
||||
int main() {
|
||||
return Manager::getInstance("/home/black/Nextcloud/Dokumente/Dokumente/Hochschule/2. Semester/Rechnerarchitektur/Projekt/test.txt")->run();
|
||||
return Manager::getInstance(
|
||||
"/home/black/Nextcloud/Dokumente/Dokumente/Hochschule/2. Semester/Rechnerarchitektur/Projekt/test.txt"
|
||||
)->run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user