started work on the CHIP-8 interpreter

This commit is contained in:
2024-11-26 06:52:49 -05:00
parent 3e5da27250
commit 7069e0ae49
7 changed files with 270 additions and 16 deletions

160
Interpreter.cpp Normal file
View File

@@ -0,0 +1,160 @@
#include "Interpreter.hpp"
#include <cstring>
#include <stdexcept>
namespace chocochip8 {
Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad):
mvMemory(scMemorySize),
mCallStack{},
mrDisplay{display},
mrBuzzer{buzzer},
mrKeypad{keypad},
mcTicksPerSecond{ticksPerSecond},
mvSpecialReg{},
mvReg{} {}
void Interpreter::tick() {
constexpr Opcode opcodeMap[16] = {
Opcode::SET, Opcode::OR, Opcode::AND, Opcode::XOR,
Opcode::ADD, Opcode::SUB, Opcode::RSH, Opcode::SUB2,
Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL, Opcode::UNIMPL,
Opcode::UNIMPL, Opcode::UNIMPL, Opcode::LSH, Opcode::UNIMPL,
};
unsigned pc = mvSpecialReg[SR_PC];
unsigned inst = (mvMemory[pc] << 8) | mvMemory[pc + 1];
unsigned reg1 = (inst & 0x0F00) >> 8;
unsigned reg2 = (inst & 0x00F0) >> 4;
unsigned imm1 = (inst & 0x000F);
unsigned imm2 = (inst & 0x00FF);
unsigned imm3 = (inst & 0x0FFF);
mvSpecialReg[SR_PC] += 2;
switch(inst & 0xF000) {
case 0x0000: // 0NNN - call machine language routine
if(inst == 0x00E0) {
// clear display
for(auto &scanline : *mrDisplay.mpFramebuffer) {
scanline.reset();
}
} else if(inst == 0x00EE) {
// return from subroutine
mvSpecialReg[SR_PC] = mCallStack.top();
mCallStack.pop();
} else {
throw std::invalid_argument("not implemented");
}
break;
case 0x1000: // 1NNN - unconditional jump
mvSpecialReg[SR_PC] = imm3;
break;
case 0x2000: // 2NNN - call subroutine
mCallStack.push(pc);
mvSpecialReg[SR_PC] = imm3;
break;
case 0x3000: // 3XNN - skip if equal immediate
executeArithmetic(Opcode::JEQ, reg1, imm2);
break;
case 0x4000: // 4XNN - skip if nonequal immediate
executeArithmetic(Opcode::JNEQ, reg1, imm2);
break;
case 0x5000: // 5XY0 - skip if equal
executeArithmetic(Opcode::JEQ, reg1, mvReg[reg2]);
break;
case 0x6000: // 6XNN - load immediate
executeArithmetic(Opcode::SET, reg1, imm2);
break;
case 0x7000: // 7XNN - increment
executeArithmetic(Opcode::ADD, reg1, imm2);
break;
case 0x8000: // 8XNN - general arithmetic
executeArithmetic(opcodeMap[imm1], reg1, mvReg[reg2]);
break;
case 0x9000: // 9XY0 - skip if nonequal
executeArithmetic(Opcode::JNEQ, reg1, mvReg[reg2]);
break;
case 0xA000: // ANNN - load I
mvSpecialReg[SR_I] = imm3;
break;
case 0xB000: // BNNN - jump indirect
mvSpecialReg[SR_I] = mvReg[R_V0] + imm3;
break;
case 0xC000: // CXNN - load random
executeArithmetic(Opcode::RAND, reg1, imm2);
break;
case 0xD000: // DXYN - draw
break;
case 0xE000: // EX9E, EXA1 - keypad access
break;
case 0xF000: // several unique instructions
break;
}
if(mvSpecialReg[SR_T1] > 0) {
mvSpecialReg[SR_T1] -= 1;
}
if(mvSpecialReg[SR_T2] > 0) {
mvSpecialReg[SR_T2] -= 1;
if(mvSpecialReg[SR_T2] == 0) {
mrBuzzer.off();
}
}
}
void Interpreter::loadProgram(uint16_t where, char const* data, size_t count) {
if(where + count > scMemorySize) {
throw std::out_of_range("program exceeds memory bounds or capacity");
}
memcpy(mvMemory.data(), data, count);
}
void Interpreter::executeArithmetic(Opcode opcode, int iReg, uint8_t operand) {
uint8_t tmp;
switch(opcode) {
case Opcode::SET: mvReg[iReg] = operand; break;
case Opcode::AND: mvReg[iReg] &= operand; break;
case Opcode::OR: mvReg[iReg] |= operand; break;
case Opcode::XOR: mvReg[iReg] ^= operand; break;
case Opcode::RAND: mvReg[iReg] = rand() & operand; break;
case Opcode::LSH:
mvReg[R_VF] = (mvReg[iReg] & 0x80) ? 1 : 0;
mvReg[iReg] <<= 1;
break;
case Opcode::RSH:
mvReg[R_VF] = (mvReg[iReg] & 0x01) ? 1 : 0;
mvReg[iReg] >>= 1;
break;
case Opcode::ADD:
tmp = mvReg[iReg] + operand;
mvReg[R_VF] = (tmp < mvReg[iReg]) ? 1 : 0;
mvReg[iReg] = tmp;
break;
case Opcode::SUB:
tmp = mvReg[iReg] - operand;
mvReg[R_VF] = (tmp > mvReg[iReg]) ? 1 : 0;
mvReg[iReg] = tmp;
break;
case Opcode::SUB2:
tmp = operand - mvReg[iReg];
mvReg[R_VF] = (tmp > operand) ? 1 : 0;
mvReg[iReg] = tmp;
break;
case Opcode::JEQ:
if(mvReg[iReg] == operand) {
mvSpecialReg[SR_PC] += 2;
}
break;
case Opcode::JNEQ:
if(mvReg[iReg] != operand) {
mvSpecialReg[SR_PC] += 2;
}
break;
case Opcode::UNIMPL:
throw std::invalid_argument("invalid opcode");
break;
}
}
}; // namespace chochochip8