73 lines
1.8 KiB
C++
73 lines
1.8 KiB
C++
#include "machine.h"
|
|
#include <stdexcept>
|
|
|
|
Machine::Machine() {
|
|
A = B = L = T = S = X = PC = SW = 0;
|
|
F = 0.0;
|
|
}
|
|
|
|
int Machine::getReg(int reg)
|
|
{
|
|
switch (reg) {
|
|
case 0: return A;
|
|
case 1: return X;
|
|
case 2: return L;
|
|
case 3: return B;
|
|
case 4: return S;
|
|
case 5: return T;
|
|
case 6: return F;
|
|
case 8: return PC;
|
|
case 9: return SW;
|
|
default: return -1;
|
|
}
|
|
}
|
|
|
|
void Machine::setReg(int reg, int val)
|
|
{
|
|
switch (reg) {
|
|
case 0: A = val; break;
|
|
case 1: X = val; break;
|
|
case 2: L = val; break;
|
|
case 3: B = val; break;
|
|
case 4: S = val; break;
|
|
case 5: T = val; break;
|
|
case 8: PC = val; break;
|
|
case 9: PC = 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");
|
|
}
|
|
return memory[address];
|
|
}
|
|
|
|
void Machine::writeByte(unsigned int address, unsigned char val) {
|
|
if (address > MAX_ADDRESS) {
|
|
throw std::out_of_range("Memory write out of range");
|
|
}
|
|
memory[address] = val;
|
|
}
|
|
|
|
unsigned int Machine::getWord(unsigned int address) {
|
|
if (address + 2 > MAX_ADDRESS) {
|
|
throw std::out_of_range("Memory write out of range");
|
|
}
|
|
unsigned int B1 = memory[address + 2];
|
|
unsigned int B2 = memory[address + 1];
|
|
unsigned int B3 = memory[address];
|
|
return B1 | (B2 << 8) | (B3 << 16);
|
|
}
|
|
|
|
void Machine::setWord(unsigned int address, unsigned int val) {
|
|
if (address + 2 > MAX_ADDRESS) {
|
|
throw std::out_of_range("Memory write out of range");
|
|
}
|
|
memory[address + 2] = val & 0xFF;
|
|
memory[address + 1] = (val >> 8) & 0xFF;
|
|
memory[address] = (val >> 16) & 0xFF;
|
|
}
|