Initial commit, zbrisal narobe ustvarjeno mapo, naredil prvi del simulatorja

This commit is contained in:
Timon 2025-11-22 14:09:29 +01:00
parent f8404698d2
commit 513cb421ec
12 changed files with 329 additions and 1 deletions

1
ass2

@ -1 +0,0 @@
Subproject commit 729fd0f34695609470905ac45491582f92755e9d

15
ass2/.vscode/c_cpp_properties.json vendored Normal file
View file

@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

10
ass2/device.cpp Normal file
View file

@ -0,0 +1,10 @@
#include <cstdint>
#include "device.h"
using namespace std;
void Device::write(uint8_t val) {}
bool Device::test() {return true;}
uint8_t Device::read() {return 0;}

12
ass2/device.h Normal file
View file

@ -0,0 +1,12 @@
#include <cstdint>
#include <string>
class Device {
public:
virtual ~Device()=default;
virtual void write(uint8_t val);
virtual uint8_t read();
virtual bool test();
private:
};

23
ass2/fileDevice.cpp Normal file
View file

@ -0,0 +1,23 @@
#include <string>
#include <fstream>
#include "fileDevice.h"
#include <stdexcept>
fileDevice::fileDevice(std::string& filename): file(filename, std::ios::in | std::ios::out | std::ios::binary) {
if (!file.is_open()) {
throw std::runtime_error("Datoteke ni mogoče odpreti: " + filename);
}
}
uint8_t fileDevice::read() {
int c = file.get();
if (c == EOF) {
return 0;
}
return static_cast<uint8_t>(c);
}
void fileDevice::write(uint8_t val) {
file.put(static_cast<char>(val));
file.flush();
}

12
ass2/fileDevice.h Normal file
View file

@ -0,0 +1,12 @@
#include <string>
#include <fstream>
#include "device.h"
class fileDevice : public Device{
private:
std::fstream file;
public:
fileDevice(std::string& fileName);
uint8_t read() override;
void write(uint8_t val) override;
};

12
ass2/inputDevice.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <istream>
#include <cstdint>
#include "inputDevice.h"
InputDevice::InputDevice(std::istream& input): in(input){}; //konstruktor
uint8_t InputDevice::read() { //metoda read
int c = in.get();
if (c == EOF) return 0;
else return static_cast<uint8_t>(c);
}

12
ass2/inputDevice.h Normal file
View file

@ -0,0 +1,12 @@
#include <cstdint>
#include <fstream>
#include "device.h"
class InputDevice : public Device {
public:
uint8_t read() override;
InputDevice(std::istream& input);
private:
std::istream& in;
};

149
ass2/machine.cpp Normal file
View file

@ -0,0 +1,149 @@
#include <array>
#include <stdexcept>
#include "device.h"
#include "inputDevice.h"
#include "outputDevice.h"
#include "fileDevice.h"
#include "opcode.h"
#include <memory>
#include <iostream>
using namespace std;
class machine {
public:
//zaporedne stevilke registrov, ne njihove vrednosti
static const int A = 0;
static const int X = 1;
static const int L = 2;
static const int B = 3;
static const int S = 4;
static const int T = 5;
static const double F = 6;
static const int PC = 8;
static const int SW = 9;
static const int CC_LT = 0x00; //manjse
static const int CC_EQ = 0x40; //enako
static const int CC_GT = 0x80; //vecje
static const int MAX_ADRESS = 1048575;
private:
array<int, 10> registri{}; //tukaj so shranjene pa vrednosti registrov
double F_val = 0.0;
array<uint8_t, MAX_ADRESS> pomnilnik{};
array<unique_ptr<Device>, 256> naprave{}; //treba s pointerji zaradi dedovanja
public:
machine() {
registri.fill(0); //konstruktor -> vsi registri so 0 na zacetku
naprave[0] = make_unique<InputDevice>(cin); //0 = std vhod
naprave[1] = make_unique<InputDevice>(cout); //1 = std izhod
naprave[2] = make_unique<InputDevice>(cerr); //2 = std izhod za napake
for (int i = 3; i < 256; i++) { //inicializacija ostalih naprav
string fname = "file" + to_string(i) + ".dat";
naprave[i] = make_unique<fileDevice>(fname);
}
}
//getterji, setterji
int getA() {return registri[A];}
void setA(int a) {registri[A] = a;}
int getX() {return registri[X];}
void setX(int x) {registri[X] = x;}
int getL() {return registri[L];}
void setL(int l) {registri[L] = l;}
int getB() {return registri[B];}
void setB(int b) {registri[B] = b;}
int getS() {return registri[S];}
void setS(int s) {registri[S] = s;}
int getT() {return registri[T];}
void setT(int t) {registri[T] = t;}
double getF() {return F_val;}
void setF(double f) {F_val = f;}
int getPC() {return registri[PC];}
void setPC(int pc) {registri[PC] = pc;}
int getSW() {return registri[SW];}
void setSW(int sw) {registri[SW] = sw;}
int getReg(int r) {
return r!=6 ? registri[r] : F_val;
}
void setReg(int r, int val) {
if (r == 6) {
F_val = val;
} else {
registri[r] = val;
}
}
int getByte(int adr) {
return (adr <= MAX_ADRESS && adr >= 0) ? pomnilnik[adr] : throw out_of_range("Naslov je izven pomnilniškega obmocja: " + to_string(adr));
}
void setByte(int adr, int val) {
if (adr < 0 || adr > MAX_ADRESS) throw out_of_range("Naslov je izven pomnilniškega obmocja: " + to_string(adr));
if (val < 0 || val > 255) throw invalid_argument("Byte ima vrednost med 0 in 255.");
pomnilnik[adr] = static_cast<uint8_t>(val);
}
int getWord(int adr) {
if (adr < 0 || adr + 2 > MAX_ADRESS) throw out_of_range("Beseda je izven pomnilniškega območja: " + to_string(adr+2));
int byte1 = pomnilnik[adr];
int byte2 = pomnilnik[adr+1];
int byte3 = pomnilnik[adr+2];
return (byte1 << 16) | (byte2 << 8) | byte3;
}
void setWord(int adr, int val) {
if (adr < 0 || adr + 2 > MAX_ADRESS) throw out_of_range("Beseda je izven pomnilniškega območja: " + to_string(adr+2));
if (val < 0 || val > 0xFFFFFF) throw invalid_argument("Beseda mora imeti vrednost med 0 in 0xFFFFFF.");
pomnilnik[adr] = (val >> 16) & 0xFF; //val = 0x00123456 -> hočemo 12, val >> 16 -> 0x00000012 & 0x000000ff = 0x12
pomnilnik[adr+1] = (val >> 8) & 0xFF;
pomnilnik[adr+2] = (val) & 0xFF;
}
Device& getDevice(uint8_t dev) {
if (dev > 255)
throw out_of_range("Naprava ne obstaja, izberite napravo med 0 in 255");
if (!naprave[dev])
throw runtime_error("Naprava ni inicializirana");
return *naprave[dev]; //rabmo returnat pointer, ker nase naprave so shranjene kakor unique pointerji
}
void setDevice(uint8_t num, unique_ptr<Device> dev) {
if (num > 255)
throw out_of_range("Naprava ne obstaja, izberite napravo med 0 in 255");
naprave[num] = move(dev);
}
void setFileDevice(uint8_t num, string filename) {
if (num <= 2 || num > 255) {
throw out_of_range("Samo naprave med 3 in 255 imajo lahko poljubno ime");
}
naprave[num] = make_unique<fileDevice>(filename);
}
};
int main() {
return 0;
}

62
ass2/opcode.h Normal file
View file

@ -0,0 +1,62 @@
class Opcode {
public:
static const int ADD = 0x18;
static const int ADDF = 0x58;
static const int ADDR = 0x90;
static const int AND = 0x40;
static const int CLEAR = 0xB4;
static const int COMP = 0x28;
static const int COMPF = 0x88;
static const int COMPR = 0xA0;
static const int DIV = 0x24;
static const int DIVF = 0x64;
static const int DIVR = 0x9C;
static const int FIX = 0xC4;
static const int FLOAT = 0xC0;
static const int HIO = 0xF4;
static const int J = 0x3C;
static const int JEQ = 0x30;
static const int JGT = 0x34;
static const int JLT = 0x38;
static const int JSUB = 0x48;
static const int LDA = 0x00;
static const int LDB = 0x68;
static const int LDCH = 0x50;
static const int LDF = 0x70;
static const int LDL = 0x08;
static const int LDS = 0x6C;
static const int LDT = 0x74;
static const int LDX = 0x04;
static const int LPS = 0xD0;
static const int MUL = 0x20;
static const int MULF = 0x60;
static const int MULR = 0x98;
static const int NORM = 0xC8;
static const int OR = 0x44;
static const int RD = 0xD8;
static const int RMO = 0xAC;
static const int RSUB = 0x4C;
static const int SHIFTL = 0xA4;
static const int SHIFTR = 0xA8;
static const int SIO = 0xF0;
static const int SSK = 0xEC;
static const int STA = 0x0C;
static const int STB = 0x78;
static const int STCH = 0x54;
static const int STF = 0x80;
static const int STI = 0xD4;
static const int STL = 0x14;
static const int STS = 0x7C;
static const int STSW = 0xE8;
static const int STT = 0x84;
static const int STX = 0x10;
static const int SUB = 0x1C;
static const int SUBF = 0x5C;
static const int SUBR = 0x94;
static const int SVC = 0xB0;
static const int TD = 0xE0;
static const int TIO = 0xF8;
static const int TIX = 0x2C;
static const int TIXR = 0xB8;
static const int WD = 0xDC;
};

9
ass2/outputDevice.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "outputDevice.h"
#include <ostream>
outputDevice::outputDevice(std::ostream& output): out(output) {}; //konstruktor
void outputDevice::write(uint8_t value) { //write
out.put(static_cast<char>(value));
out.flush();
}

13
ass2/outputDevice.h Normal file
View file

@ -0,0 +1,13 @@
#include <fstream>
#include <cstdint>
#include "device.h"
class outputDevice : public Device {
private:
std::ostream& out;
public:
outputDevice(std::ostream& output);
void write(uint8_t value) override;
};