spo/simulator_SIC_XE/include/machine.h
2025-11-11 11:03:25 +01:00

145 lines
No EOL
3 KiB
C++

#ifndef MACHINE_H
#define MACHINE_H
#include <string>
#include <iostream>
#include <vector>
#include "device.h"
#include "input_device.h"
#include "output_device.h"
#include "file_device.h"
#include "opcode.h"
#include <memory>
#define MEMORY_SIZE 65536
#define NUM_DEVICES 256
using std::string;
using std::cerr;
using std::endl;
using std::cout;
class Machine {
public:
Machine();
~Machine();
// Accessor methods for registers
int getA() const { return A; }
void setA(int value) { A = value; }
int getB() const { return B; }
void setB(int value) { B = value; }
int getX() const { return X; }
void setX(int value) { X = value; }
int getL() const { return L; }
void setL(int value) { L = value; }
int getS() const { return S; }
void setS(int value) { S = value; }
int getT() const { return T; }
void setT(int value) { T = value; }
int getPC() const { return PC; }
void setPC(int value) { PC = value; }
int getSW() const { return SW; }
void setSW(int value) { SW = value; }
double getF() const { return F; }
void setF(double value) { F = value; }
int getReg(int regNum) const;
void setReg(int regNum, int value);
// Memory access methods
int getByte(int address);
void setByte(int address, int value);
int getWord(int address);
void setWord(int address, int value);
double getFloat(int address);
void setFloat(int address, double value);
// Device access methods
Device& getDevice(int num);
void setDevice(int num, std::shared_ptr<Device> device);
// Set a file device at index `num` using the provided filename.
void setFileDevice(int num, const std::string &filename);
// Fetch and execute instructions
int fetch();
void execute();
bool execF1(int opcode);
bool execF2(int opcode, int operand);
bool execSICF3F4(int opcode, int ni, int operand);
// error handling methods
void notImplemented(string mnemonic);
void invalidOpcode(int opcode);
void invalidAddressing();
void divisionByZero(int opcode);
void undefinedHandler(int opcode);
private:
// registers
int A, B, X, L, S, T, PC, SW;
double F;
// memory
unsigned char memory[MEMORY_SIZE];
// devices
std::vector<std::shared_ptr<Device>> devices;
// fallback device returned when device slot is empty/invalid
Device fallbackDevice;
};
// Convert integer to 24-bit signed SIC representation
inline int toSIC24(int value) {
value &= 0xFFFFFF;
if (value & 0x800000) {
value -= 0x1000000;
}
return value;
}
inline int setCC(int sw, int cc) {
sw &= ~CC_MASK;
sw |= (cc & CC_MASK);
return sw;
}
inline int sic_comp(int a, int b, int sw) {
int sa = toSIC24(a);
int sb = toSIC24(b);
int cc;
if (sa < sb) {
cc = CC_LT;
} else if (sa == sb) {
cc = CC_EQ;
} else {
cc = CC_GT;
}
return setCC(sw, cc);
}
inline int getCC(int sw) {
return sw & CC_MASK;
}
#endif // MACHINE_H