Implementiral clock, dodal machine.h in popravu pol vsa shit v machine.cpp, naredu ukaze za f1, f2
This commit is contained in:
parent
3cfdd46516
commit
cf3dd766d0
5 changed files with 308 additions and 88 deletions
46
ass2/cpu.cpp
Normal file
46
ass2/cpu.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include "cpu.h"
|
||||
|
||||
void cpu::start() {
|
||||
if (running) return;
|
||||
running = true;
|
||||
nitUre = std::thread(&cpu::zankaUre, this);
|
||||
}
|
||||
|
||||
void cpu::stop() {
|
||||
running = false;
|
||||
if (nitUre.joinable())
|
||||
nitUre.join();
|
||||
}
|
||||
|
||||
bool cpu::isRunning() {
|
||||
return running.load();
|
||||
}
|
||||
|
||||
int cpu::getSpeed() {
|
||||
return hitrostKhz;
|
||||
}
|
||||
|
||||
void cpu::setSpeed(int speedKhz) {
|
||||
if (speedKhz < 1) speedKhz = 1;
|
||||
hitrostKhz = speedKhz;
|
||||
}
|
||||
|
||||
void cpu::zankaUre() {
|
||||
while (running) {
|
||||
// 1. Izvedemo določeno št. ukazov na tick
|
||||
for (int i = 0; i < operacijeNaTick; i++) {
|
||||
cpu::m->execute();
|
||||
}
|
||||
|
||||
// 2. Izračunamo kolikokrat mora "tikati" na sekundo
|
||||
// speed_kHz = n tisoč ukazov / sekundo
|
||||
// operacijeNaTick = koliko ukazov na en tik
|
||||
// ticksPerSecond = (hitrostKhz * 1000) / operacijeNaTick
|
||||
|
||||
double ticksPerSecond = (hitrostKhz * 1000.0) / operacijeNaTick;
|
||||
double sleepTimeSec = 1.0 / ticksPerSecond;
|
||||
|
||||
// 3. Ustavimo nit za izračunani čas
|
||||
std::this_thread::sleep_for(std::chrono::duration<double>(sleepTimeSec));
|
||||
}
|
||||
}
|
||||
24
ass2/cpu.h
Normal file
24
ass2/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();
|
||||
};
|
||||
231
ass2/machine.cpp
231
ass2/machine.cpp
|
|
@ -1,5 +1,6 @@
|
|||
#include <array>
|
||||
#include <stdexcept>
|
||||
#include "machine.h"
|
||||
#include "device.h"
|
||||
#include "inputDevice.h"
|
||||
#include "outputDevice.h"
|
||||
|
|
@ -7,77 +8,77 @@
|
|||
#include "opcode.h"
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
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
|
||||
machine::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
|
||||
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;}
|
||||
for (int i = 3; i < 256; i++) { //inicializacija ostalih naprav
|
||||
string fname = "file" + to_string(i) + ".dat";
|
||||
naprave[i] = make_unique<fileDevice>(fname);
|
||||
}
|
||||
|
||||
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;}
|
||||
ukaziF2 = {
|
||||
{"ADDR", [&](int r1, int r2){setReg(r2, getReg(r1)+getReg(r2)); return true;}},
|
||||
{"SUBR", [&](int r1, int r2){setReg(r2, getReg(r2)-getReg(r1)); return true;}},
|
||||
{"MULR", [&](int r1, int r2){setReg(r2, getReg(r2)*getReg(r1)); return true;}},
|
||||
{"DIVR", [&](int r1, int r2){setReg(r2, getReg(r2)/getReg(r1)); return true;}},
|
||||
{"CLEAR", [&](int r1, int){setReg(r1, 0); return true; }},
|
||||
{"COMPR", [&](int r1, int r2){int v1 = getReg(r1), v2 = getReg(r2);
|
||||
if (v1 < v2) setSW(CC_LT);
|
||||
else if (v1 == v2) setSW(CC_EQ);
|
||||
else setSW(CC_GT);
|
||||
return true;}},
|
||||
{"RMO", [&](int r1, int r2){setReg(r2, getReg(r1)); return true;}},
|
||||
{"SHIFTR", [&](int r1, int n){int val = getReg(r1);setReg(r1, val >> n);return true;}},
|
||||
{"SHIFTL", [&](int r1, int n){int val = getReg(r1);setReg(r1, val << n);return true;}},
|
||||
{"TIXR", [&](int r1, int){int x = getX()+1; setX(x); int v2 = getReg(r1);
|
||||
if (x < v2) setSW(CC_LT);
|
||||
else if (x == v2) setSW(CC_EQ);
|
||||
else setSW(CC_GT);
|
||||
return true;}},
|
||||
};
|
||||
}
|
||||
|
||||
int getReg(int r) {
|
||||
//machine::getterji, setterji
|
||||
int machine::getA() {return registri[A];}
|
||||
void machine::setA(int a) {registri[A] = a;}
|
||||
|
||||
int machine::getX() {return registri[X];}
|
||||
void machine::setX(int x) {registri[X] = x;}
|
||||
|
||||
int machine::getL() {return registri[L];}
|
||||
void machine::setL(int l) {registri[L] = l;}
|
||||
|
||||
int machine::getB() {return registri[B];}
|
||||
void machine::setB(int b) {registri[B] = b;}
|
||||
|
||||
int machine::getS() {return registri[S];}
|
||||
void machine::setS(int s) {registri[S] = s;}
|
||||
|
||||
int machine::getT() {return registri[T];}
|
||||
void machine::setT(int t) {registri[T] = t;}
|
||||
|
||||
double machine::getF() {return F_val;}
|
||||
void machine::setF(double f) {F_val = f;}
|
||||
|
||||
int machine::getPC() {return registri[PC];}
|
||||
void machine::setPC(int pc) {registri[PC] = pc;}
|
||||
|
||||
int machine::getSW() {return registri[SW];}
|
||||
void machine::setSW(int sw) {registri[SW] = sw;}
|
||||
|
||||
int machine::getReg(int r) {
|
||||
return r!=6 ? registri[r] : F_val;
|
||||
}
|
||||
void setReg(int r, int val) {
|
||||
void machine::setReg(int r, int val) {
|
||||
if (r == 6) {
|
||||
F_val = val;
|
||||
} else {
|
||||
|
|
@ -85,18 +86,18 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
int getByte(int adr) {
|
||||
int machine::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));
|
||||
void machine::setByte(int adr, int val) {
|
||||
if (adr < 0 || adr > MAX_ADRESS) outOfMemoryRange(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 machine::getWord(int adr) {
|
||||
if (adr < 0 || adr + 2 > MAX_ADRESS) outOfMemoryRange(adr+2);
|
||||
|
||||
int byte1 = pomnilnik[adr];
|
||||
int byte2 = pomnilnik[adr+1];
|
||||
|
|
@ -105,8 +106,8 @@ public:
|
|||
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));
|
||||
void machine::setWord(int adr, int val) {
|
||||
if (adr < 0 || adr + 2 > MAX_ADRESS) outOfMemoryRange(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
|
||||
|
|
@ -114,7 +115,7 @@ public:
|
|||
pomnilnik[adr+2] = (val) & 0xFF;
|
||||
}
|
||||
|
||||
Device& getDevice(uint8_t dev) {
|
||||
Device& machine::getDevice(uint8_t dev) {
|
||||
if (dev > 255)
|
||||
throw out_of_range("Naprava ne obstaja, izberite napravo med 0 in 255");
|
||||
|
||||
|
|
@ -125,37 +126,57 @@ public:
|
|||
}
|
||||
|
||||
|
||||
void setDevice(uint8_t num, unique_ptr<Device> dev) {
|
||||
void machine::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) {
|
||||
void machine::setFileDevice(uint8_t num, const 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);
|
||||
}
|
||||
|
||||
void notImplemented(string mnemonic) {
|
||||
int getUN(int n, int x, int b, int p, int e, int operand) {
|
||||
//NEED TO IMPLEMENT
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*void incompatibleArgument(const type_info& t1, const type_info& t2) {
|
||||
stringstream ss;
|
||||
ss << "Tipa vrednosti se razlikujeta: " << t1.name() << " vs " << t2.name();
|
||||
throw invalid_argument(ss.str());
|
||||
}*/
|
||||
|
||||
void machine::outOfMemoryRange(int memory) {
|
||||
throw out_of_range("Naslov je izven pomnilniškega obmocja: " + to_string(memory));
|
||||
}
|
||||
|
||||
void machine::notImplemented(const string& mnemonic) {
|
||||
throw runtime_error("Mnemonic ni implementiran: " + mnemonic);
|
||||
}
|
||||
|
||||
void invalidOpcode (int opcode) {
|
||||
void machine::invalidOpcode (int opcode) {
|
||||
throw invalid_argument("Opcode ne obstaja: " + to_string(opcode) + ".");
|
||||
}
|
||||
|
||||
void invalidAdressing() {
|
||||
void machine::invalidAdressing() {
|
||||
throw invalid_argument("Ta način naslavljanja ni podprt.");
|
||||
}
|
||||
|
||||
uint8_t fetch() {
|
||||
return getByte(registri[PC]++);
|
||||
void machine::invalidRegister(const string& mnemonic, int r1, int r2) {
|
||||
throw invalid_argument("Neveljaven register v ukazu" + mnemonic + " r1 = " + to_string(r1) + " r2 = " + to_string(r2));
|
||||
}
|
||||
|
||||
void execute() {
|
||||
uint8_t machine::fetch() {
|
||||
return machine::getByte(registri[PC]++);
|
||||
}
|
||||
|
||||
void machine::execute() {
|
||||
uint8_t opcode8 = fetch();
|
||||
uint8_t opcode6 = opcode8 & 0xFC;
|
||||
|
||||
|
|
@ -190,23 +211,54 @@ public:
|
|||
}
|
||||
|
||||
if (uspesno) return;
|
||||
else {}
|
||||
else {
|
||||
throw runtime_error("Izvajanje ukaza je bilo neuspešno");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool execF1(uint8_t opcode, string mnemonic){
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//TO DO, racunanje uporabnih naslovov!!!!
|
||||
bool machine::execF1(uint8_t opcode, const string& mnemonic){
|
||||
//FIX, FLOAT, HIO, TIO, SIO, NORM
|
||||
auto it = Opcode::OPCODES.find(opcode);
|
||||
InstructionInfo ii = it->second;
|
||||
string name = ii.mnemonic;
|
||||
|
||||
if (name == "FIX") {
|
||||
setA(static_cast<int>(getF()));
|
||||
return true;
|
||||
} else if (name == "FLOAT") {
|
||||
setF(static_cast<double>(getA()));
|
||||
return true;
|
||||
} else if (name == "HIO") {
|
||||
//halt I/0 na kanalu (A)
|
||||
return true;
|
||||
} else if (name == "TIO") {
|
||||
//test I/O na kanalu (A)
|
||||
return true;
|
||||
} else if (name == "SIO") {
|
||||
//start I/O na kanalu (A), adress kanala je dan na (S)
|
||||
return true;
|
||||
} else if (name == "NORM") {
|
||||
//normalizirej (F), v c++ so ze vsa double stevila normalizirana
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool execF2(uint8_t opcode, uint8_t operand, string mnemonic){
|
||||
|
||||
|
||||
bool machine::execF2(uint8_t opcode, uint8_t operand, const string& mnemonic){
|
||||
uint8_t r1 = (operand & 0xF0) >> 4;
|
||||
uint8_t r2 = (operand & 0x0F);
|
||||
|
||||
|
||||
auto it = ukaziF2.find(mnemonic);
|
||||
|
||||
if (it == ukaziF2.end()) notImplemented(mnemonic); return false;
|
||||
if (r1 > 9 || r2 > 9 || r1 < 0 || r2 < 0) invalidRegister(mnemonic, r1, r2); return false;
|
||||
|
||||
return it->second(r1, r2);
|
||||
}
|
||||
|
||||
bool execSIC_F3_F4(uint8_t byte1, uint8_t byte2, uint8_t byte3, string mnemonic) {
|
||||
bool machine::execSIC_F3_F4(uint8_t byte1, uint8_t byte2, uint8_t byte3, const string& mnemonic) {
|
||||
uint8_t n = (byte1 >> 7) & 1;
|
||||
uint8_t i = (byte1 >> 6) & 1;
|
||||
uint8_t x = (byte2 >> 7) & 1;
|
||||
|
|
@ -216,14 +268,17 @@ public:
|
|||
|
||||
if (n == 0 && i == 0) {
|
||||
//imamo format SIC
|
||||
|
||||
} else if (e == 1) {
|
||||
//imamo format 4
|
||||
} else {
|
||||
//imamo format 3
|
||||
}
|
||||
}
|
||||
//TO DO
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
83
ass2/machine.h
Normal file
83
ass2/machine.h
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
class Device;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
};
|
||||
12
ass2/main.cpp
Normal file
12
ass2/main.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include "machine.h"
|
||||
#include "cpu.h"
|
||||
|
||||
int main(int argc, char const *argv[]){
|
||||
machine m;
|
||||
cpu procesor(&m);
|
||||
procesor.setSpeed(100);
|
||||
procesor.start();
|
||||
//stuff
|
||||
procesor.stop();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue