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

View File

@@ -1,15 +1,15 @@
#include "Buzzer.hpp" #include "BuzzerSDL.hpp"
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
#include <cstring> #include <cstring>
void SDLCALL Buzzer::audioCallback(void *userdata, Uint8 *stream, int len) { void SDLCALL BuzzerSDL::audioCallback(void *userdata, Uint8 *stream, int len) {
static_cast<Buzzer*>(userdata)->copySamples(stream, len); static_cast<BuzzerSDL*>(userdata)->copySamples(stream, len);
} }
Buzzer::Buzzer(unsigned buzzerFrequency) { BuzzerSDL::BuzzerSDL(unsigned buzzerFrequency) {
SDL_AudioSpec desired; SDL_AudioSpec desired;
SDL_AudioSpec obtained; SDL_AudioSpec obtained;
@@ -42,19 +42,19 @@ Buzzer::Buzzer(unsigned buzzerFrequency) {
miCurrentSample = 0; miCurrentSample = 0;
} }
Buzzer::~Buzzer() { BuzzerSDL::~BuzzerSDL() {
SDL_CloseAudioDevice(mAudioDevice); SDL_CloseAudioDevice(mAudioDevice);
} }
void Buzzer::on() { void BuzzerSDL::on() {
SDL_PauseAudioDevice(mAudioDevice, SDL_FALSE); SDL_PauseAudioDevice(mAudioDevice, SDL_FALSE);
} }
void Buzzer::off() { void BuzzerSDL::off() {
SDL_PauseAudioDevice(mAudioDevice, SDL_TRUE); SDL_PauseAudioDevice(mAudioDevice, SDL_TRUE);
} }
void Buzzer::copySamples(Uint8 *stream, int len) { void BuzzerSDL::copySamples(Uint8 *stream, int len) {
// Copy len samples from the current position of the buffer, // Copy len samples from the current position of the buffer,
// looping back to the start of the buffer when needed. // looping back to the start of the buffer when needed.
while(len > 0) { while(len > 0) {

View File

@@ -3,15 +3,17 @@
#include <vector> #include <vector>
#include <SDL2/SDL_audio.h> #include <SDL2/SDL_audio.h>
class Buzzer { #include "Peripherals.hpp"
class BuzzerSDL : public chocochip8::Buzzer {
public: public:
using sample_t = Sint16; using sample_t = Sint16;
Buzzer(unsigned frequency); BuzzerSDL(unsigned frequency);
~Buzzer(); ~BuzzerSDL();
void on(); void on() override;
void off(); void off() override;
private: private:
static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len); static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len);

View File

@@ -5,5 +5,5 @@ SET(CMAKE_BUILD_TYPE Debug)
add_subdirectory(submodules/sdl2) add_subdirectory(submodules/sdl2)
SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(chocochip8 main.cpp Buzzer.cpp) add_executable(chocochip8 main.cpp Interpreter.cpp BuzzerSDL.cpp)
target_link_libraries(chocochip8 SDL2::SDL2-static) target_link_libraries(chocochip8 SDL2::SDL2-static)

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

51
Interpreter.hpp Normal file
View File

@@ -0,0 +1,51 @@
#pragma once
#include "Peripherals.hpp"
#include <cstdint>
#include <vector>
#include <stack>
namespace chocochip8 {
class Interpreter {
public:
constexpr static uint16_t scResetVector = 0x0200;
constexpr static size_t scMemorySize = 4096;
private:
enum {
R_V0, R_V1, R_V2, R_V3, R_V4, R_V5, R_V6, R_V7,
R_V8, R_V9, R_VA, R_VB, R_VC, R_VD, R_VE, R_VF,
R_COUNT
};
enum {
SR_PC, SR_I, SR_T1, SR_T2,
SR_COUNT
};
enum class Opcode {
SET, ADD, SUB, SUB2, AND, OR, XOR, LSH, RSH, RAND, JEQ, JNEQ, UNIMPL
};
public:
Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad);
void tick();
void loadProgram(uint16_t where, char const* data, size_t count);
private:
void executeArithmetic(Opcode opcode, int iReg, uint8_t operand);
private:
std::vector<uint8_t> mvMemory;
std::stack<uint16_t> mCallStack;
Display &mrDisplay;
Buzzer &mrBuzzer;
Keypad &mrKeypad;
const unsigned mcTicksPerSecond;
unsigned mvSpecialReg[SR_COUNT];
uint8_t mvReg[R_COUNT];
}; // class Interpreter
}; // namespace chocohip8

41
Peripherals.hpp Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <array>
#include <bitset>
#include <memory>
namespace chocochip8 {
using Scanline = std::bitset<128>;
using Framebuffer = std::array<Scanline, 64>;
enum class Key {
KEY_0, KEY_1, KEY_2, KEY_3,
KEY_4, KEY_5, KEY_6, KEY_7,
KEY_8, KEY_9, KEY_A, KEY_B,
KEY_C, KEY_D, KEY_E, KEY_F
};
class Display {
public:
friend class Interpreter;
Display(): mpFramebuffer{std::make_unique<Framebuffer>()} {}
virtual ~Display() = default;
protected:
std::unique_ptr<Framebuffer> mpFramebuffer;
};
class Buzzer {
public:
virtual ~Buzzer() = default;
virtual void on() = 0;
virtual void off() = 0;
};
class Keypad {
public:
virtual ~Keypad() = default;
virtual void isKeyPressed(Key key) = 0;
};
}; // namespace chocochip8

View File

@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "Buzzer.hpp" #include "BuzzerSDL.hpp"
int main(int argc, char* args[]) { int main(int argc, char* args[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
@@ -9,7 +9,7 @@ int main(int argc, char* args[]) {
return 1; return 1;
} }
Buzzer buzzer(440); BuzzerSDL buzzer(440);
buzzer.on(); buzzer.on();
SDL_Event event; SDL_Event event;