Naredil opcode seznam in mapper, fetch in execute v machine ter exceptione

This commit is contained in:
Timon 2025-11-23 11:25:32 +01:00
parent 513cb421ec
commit 5dae60dcec
3 changed files with 140 additions and 0 deletions

View file

@ -139,6 +139,72 @@ public:
naprave[num] = make_unique<fileDevice>(filename);
}
void notImplemented(string mnemonic) {
throw runtime_error("Mnemonic ni implementiran: " + mnemonic);
}
void invalidOpcode (int opcode) {
throw invalid_argument("Opcode ne obstaja: " + to_string(opcode) + ".");
}
void invalidAdressing() {
throw invalid_argument("Ta način naslavljanja ni podprt.");
}
uint8_t fetch() {
return getByte(registri[PC]++);
}
void execute() {
uint8_t opcode8 = fetch();
uint8_t opcode6 = opcode8 & 0xFC;
auto it = Opcode::OPCODES.find(opcode6);
if (it == Opcode::OPCODES.end()) {
invalidOpcode(opcode6);
return;
}
InstructionInfo ii = it->second;
switch (ii.format) {
case 1:
execF1(opcode8, ii.mnemonic);
break;
case 2: {
uint8_t operand = fetch();
execF2(opcode8, operand, ii.mnemonic);
break;
}
case 3: {
uint8_t b2 = fetch();
uint8_t b3 = fetch();
execSIC_F3_F4(opcode8, b2, b3, ii.mnemonic);
break;
}
default:
break;
}
}
bool execF1(uint8_t opcode, string mnemonic){
}
bool execF2(uint8_t opcode, uint8_t operand, string mnemonic){
uint8_t r1 = (operand & 0xF0) >> 4;
uint8_t r2 = (operand & 0x0F);
}
bool execSIC_F3_F4(uint8_t opcdode, uint8_t b1, uint8_t b2, string mnemonic) {
}
};

63
ass2/opcode.cpp Normal file
View file

@ -0,0 +1,63 @@
#include "opcode.h"
const std::unordered_map<int, InstructionInfo> OPCODES = {
{0x18, {"ADD", 3}},
{0x58, {"ADDF", 3}},
{0x90, {"ADDR", 2}},
{0x40, {"AND", 3}},
{0xB4, {"CLEAR", 2}},
{0x28, {"COMP", 3}},
{0x88, {"COMPF", 3}},
{0xA0, {"COMPR", 2}},
{0x24, {"DIV", 3}},
{0x64, {"DIVF", 3}},
{0x9C, {"DIVR", 2}},
{0xC4, {"FIX", 1}},
{0xC0, {"FLOAT", 1}},
{0xF4, {"HIO", 1}},
{0x3C, {"J", 3}},
{0x30, {"JEQ", 3}},
{0x34, {"JGT", 3}},
{0x38, {"JLT", 3}},
{0x48, {"JSUB", 3}},
{0x00, {"LDA", 3}},
{0x68, {"LDB", 3}},
{0x50, {"LDCH", 3}},
{0x70, {"LDF", 3}},
{0x08, {"LDL", 3}},
{0x6C, {"LDS", 3}},
{0x74, {"LDT", 3}},
{0x04, {"LDX", 3}},
{0xD0, {"LPS", 3}},
{0x20, {"MUL", 3}},
{0x60, {"MULF", 3}},
{0x98, {"MULR", 2}},
{0xC8, {"NORM", 1}},
{0x44, {"OR", 3}},
{0xD8, {"RD", 3}},
{0xAC, {"RMO", 2}},
{0x4C, {"RSUB", 3}},
{0xA4, {"SHIFTL", 2}},
{0xA8, {"SHIFTR", 2}},
{0xF0, {"SIO", 1}},
{0xEC, {"SSK", 3}},
{0x0C, {"STA", 3}},
{0x78, {"STB", 3}},
{0x54, {"STCH", 3}},
{0x80, {"STF", 3}},
{0xD4, {"STI", 3}},
{0x14, {"STL", 3}},
{0x7C, {"STS", 3}},
{0xE8, {"STSW", 3}},
{0x84, {"STT", 3}},
{0x10, {"STX", 3}},
{0x1C, {"SUB", 3}},
{0x5C, {"SUBF", 3}},
{0x94, {"SUBR", 2}},
{0xB0, {"SVC", 2}},
{0xE0, {"TD", 3}},
{0xF8, {"TIO", 1}},
{0x2C, {"TIX", 3}},
{0xB8, {"TIXR", 2}},
{0xDC, {"WD", 3}},
};

View file

@ -1,3 +1,12 @@
#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;
@ -59,4 +68,6 @@ public:
static const int TIX = 0x2C;
static const int TIXR = 0xB8;
static const int WD = 0xDC;
static const std::unordered_map<int, InstructionInfo> OPCODES;
};