Finished implentation of devices and added Opcode class

This commit is contained in:
aljazbrodar. 2025-11-27 14:44:07 +01:00
parent 9cc0c10e35
commit 3f39c6cb71
78 changed files with 262 additions and 90 deletions

View file

@ -1,9 +1,22 @@
#include "machine.h"
#include "device.h"
#include <stdexcept>
using namespace std;
Machine::Machine() {
//nastavi registre
A = B = L = T = S = X = PC = SW = 0;
F = 0.0;
//nastavi naprave
devices[0] = new InputDevice(cin);
devices[1] = new OutputDevice(cout);
devices[2] = new OutputDevice(cerr);
for (int i = 3; i < 256; i++) {
string filename = to_string(i) + ".dev";
devices[i] = new FileDevice(filename);
}
}
int Machine::getReg(int reg)
@ -32,13 +45,10 @@ void Machine::setReg(int reg, int val)
case 4: S = val; break;
case 5: T = val; break;
case 8: PC = val; break;
case 9: PC = val; break;
case 9: SW = val; break;
}
}
int getWord(int addr);
void setWord(int addr, int val);
unsigned char Machine::readByte(unsigned int address) {
if (address > MAX_ADDRESS) {
throw std::out_of_range("Memory read out of range");
@ -55,7 +65,7 @@ void Machine::writeByte(unsigned int address, unsigned char val) {
unsigned int Machine::getWord(unsigned int address) {
if (address + 2 > MAX_ADDRESS) {
throw std::out_of_range("Memory write out of range");
throw std::out_of_range("Memory read out of range");
}
unsigned int B1 = memory[address + 2];
unsigned int B2 = memory[address + 1];
@ -71,3 +81,24 @@ void Machine::setWord(unsigned int address, unsigned int val) {
memory[address + 1] = (val >> 8) & 0xFF;
memory[address] = (val >> 16) & 0xFF;
}
Device* Machine::getDevice(int num) {
if (num < 0 || num > 255) { return nullptr; }
return devices[num];
}
void Machine::setDevice(int num, Device* device) {
if (num < 0 || num > 255 ) { return; }
delete devices[num];
devices[num] = device;
}