Popravil fileDevice.cpp tako da dejansko naredi datoteke, če še ne obstajajo
This commit is contained in:
parent
8222f8dd0a
commit
74cc571eef
270 changed files with 104 additions and 74 deletions
24
ass2/headers/cpu.h
Normal file
24
ass2/headers/cpu.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
#include "machine.h"
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
class cpu {
|
||||
private:
|
||||
std::atomic<bool> running;
|
||||
std::thread nitUre;
|
||||
int hitrostKhz;
|
||||
int operacijeNaTick;
|
||||
machine *m;
|
||||
|
||||
public:
|
||||
cpu(machine *m) : running(false), hitrostKhz(1), operacijeNaTick(10), m(m) {}
|
||||
void start();
|
||||
void stop();
|
||||
bool isRunning();
|
||||
void setSpeed(int kHz);
|
||||
int getSpeed();
|
||||
private:
|
||||
void zankaUre();
|
||||
};
|
||||
12
ass2/headers/device.h
Normal file
12
ass2/headers/device.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
class Device {
|
||||
public:
|
||||
virtual ~Device() = default;
|
||||
virtual void write(uint8_t val);
|
||||
virtual uint8_t read();
|
||||
virtual bool test();
|
||||
};
|
||||
13
ass2/headers/fileDevice.h
Normal file
13
ass2/headers/fileDevice.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
#include <fstream>
|
||||
#include "device.h"
|
||||
|
||||
class fileDevice : public Device{
|
||||
private:
|
||||
std::fstream file;
|
||||
public:
|
||||
fileDevice(const std::string& fileName);
|
||||
uint8_t read() override;
|
||||
void write(uint8_t val) override;
|
||||
bool test() override;
|
||||
};
|
||||
15
ass2/headers/inputDevice.h
Normal file
15
ass2/headers/inputDevice.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include "device.h"
|
||||
|
||||
class InputDevice : public Device {
|
||||
|
||||
public:
|
||||
void write(uint8_t value) override;
|
||||
uint8_t read() override;
|
||||
bool test() override;
|
||||
InputDevice(std::istream& input);
|
||||
private:
|
||||
std::istream& in;
|
||||
};
|
||||
92
ass2/headers/machine.h
Normal file
92
ass2/headers/machine.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include "machine.h"
|
||||
#include "device.h"
|
||||
#include "inputDevice.h"
|
||||
#include "outputDevice.h"
|
||||
#include "fileDevice.h"
|
||||
#include "opcode.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
class machine {
|
||||
public:
|
||||
// Register indices
|
||||
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 int F = 6;
|
||||
static const int PC = 8;
|
||||
static const int SW = 9;
|
||||
|
||||
// Condition codes
|
||||
static const int CC_LT = 0x00;
|
||||
static const int CC_EQ = 0x40;
|
||||
static const int CC_GT = 0x80;
|
||||
|
||||
static const int MAX_ADRESS = 1048575;
|
||||
|
||||
private:
|
||||
std::array<int, 10> registri{};
|
||||
double F_val = 0.0;
|
||||
|
||||
std::array<uint8_t, MAX_ADRESS> pomnilnik{};
|
||||
std::array<std::unique_ptr<Device>, 256> naprave{};
|
||||
std::unordered_map<std::string, std::function<bool(int,int)>> ukaziF2;
|
||||
std::unordered_map<std::string, std::function<bool(int)>> ukaziSICF3F4;
|
||||
|
||||
public:
|
||||
machine();
|
||||
|
||||
// getters-setters
|
||||
int getA(); void setA(int a);
|
||||
int getX(); void setX(int x);
|
||||
int getL(); void setL(int l);
|
||||
int getB(); void setB(int b);
|
||||
int getS(); void setS(int s);
|
||||
int getT(); void setT(int t);
|
||||
|
||||
double getF(); void setF(double f);
|
||||
int getPC(); void setPC(int pc);
|
||||
int getSW(); void setSW(int sw);
|
||||
|
||||
int getReg(int r);
|
||||
void setReg(int r, int val);
|
||||
|
||||
// memory
|
||||
int getByte(int adr);
|
||||
void setByte(int adr, int val);
|
||||
int getWord(int adr);
|
||||
void setWord(int adr, int val);
|
||||
int getUN(int n, int i, int x, int b, int p, int e, int operand);
|
||||
|
||||
// devices
|
||||
Device& getDevice(uint8_t dev);
|
||||
void setDevice(uint8_t num, std::unique_ptr<Device> dev);
|
||||
void setFileDevice(uint8_t num, const std::string& filename);
|
||||
|
||||
// error helpers
|
||||
void notImplemented(const std::string& mnemonic);
|
||||
void invalidOpcode(int opcode);
|
||||
void invalidAdressing();
|
||||
void invalidRegister(const std::string& mnemonic, int r1, int r2);
|
||||
void outOfMemoryRange(int mem);
|
||||
void divisionByZero();
|
||||
|
||||
// execution
|
||||
uint8_t fetch();
|
||||
void execute();
|
||||
bool execF1(uint8_t opcode, const std::string& mnemonic);
|
||||
bool execF2(uint8_t opcode, uint8_t operand, const std::string& mnemonic);
|
||||
bool execSIC_F3_F4(uint8_t b1, uint8_t b2, uint8_t b3, const std::string& mnemonic);
|
||||
};
|
||||
73
ass2/headers/opcode.h
Normal file
73
ass2/headers/opcode.h
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
struct InstructionInfo {
|
||||
std::string mnemonic;
|
||||
uint8_t format; // 1, 2, or 3
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
static const std::unordered_map<int, InstructionInfo> OPCODES;
|
||||
};
|
||||
16
ass2/headers/outputDevice.h
Normal file
16
ass2/headers/outputDevice.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
#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;
|
||||
uint8_t read() override;
|
||||
bool test() override;
|
||||
};
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue