8 Commits

Author SHA1 Message Date
bba46296b1 implemented keypad 2025-03-15 01:58:27 -04:00
19506dd218 decoupled timers from interpreter tick rate 2025-03-14 20:41:08 -04:00
22526c1b90 display now only draws chipxels that have changed 2025-03-14 17:35:31 -04:00
ac5313a6ca redesigned display peripheral 2025-03-14 15:48:15 -04:00
af39b2ab07 fixed ticks to delay timer conversion 2025-03-14 01:21:12 -04:00
3c30d2a04f interpreter memory vector access now always uses bound checking 2024-11-29 20:14:59 -05:00
8c49ec6d86 fixed arithmetic and logic (8XXX) instructions
interpreter now passes Timendus' flags test
2024-11-29 19:32:43 -05:00
830ab9eda7 Implemented most FXXX instructions.
It now passes the Corax+ test.
2024-11-29 18:44:31 -05:00
11 changed files with 307 additions and 123 deletions

View File

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

18
CountdownTimerSDL.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "CountdownTimerSDL.hpp"
CountdownTimerSDL::CountdownTimerSDL(unsigned frequency):
mDesiredFrequency{frequency},
mSDLFrequency{SDL_GetPerformanceFrequency()},
mStartTime{0},
mStartValue{0} {}
void CountdownTimerSDL::set(unsigned value) {
mStartTime = SDL_GetPerformanceCounter();
mStartValue = value;
}
unsigned CountdownTimerSDL::get() const {
Uint64 elapsedTime = SDL_GetPerformanceCounter() - mStartTime;
Uint64 elapsedTicks = (elapsedTime * mDesiredFrequency) / mSDLFrequency;
return elapsedTicks >= mStartValue ? 0 : mStartValue - elapsedTicks;
}

17
CountdownTimerSDL.hpp Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <SDL2/SDL_timer.h>
#include "Peripherals.hpp"
class CountdownTimerSDL : public chocochip8::CountdownTimer {
public:
CountdownTimerSDL(unsigned frequency);
void set(unsigned value) override;
unsigned get() const override;
private:
const Uint64 mDesiredFrequency;
const Uint64 mSDLFrequency;
Uint64 mStartTime;
Uint64 mStartValue;
};

View File

@@ -1,8 +1,13 @@
#include "DisplaySDL.hpp"
#include "Peripherals.hpp"
#include <SDL2/SDL_surface.h>
#include <stdexcept>
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor) {
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor):
mpFramebuffer{std::make_unique<Framebuffer>()},
mpDisplayState{std::make_unique<Framebuffer>()},
mDoClear{true} {
// Create SDL Window
mpWindow = SDL_CreateWindow(
"ChocoChip-8",
@@ -35,43 +40,69 @@ DisplaySDL::~DisplaySDL() {
SDL_DestroyWindow(mpWindow);
}
void DisplaySDL::updateWindow() const {
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
SDL_LockSurface(pSurface);
int fx, fy; // ChocoChip8 Framebuffer coordinates
int sx, sy; // SDL Surface coordinates
int lx, ly; // Last-used ChocoChip8 Framebuffer coordinates
Uint32 lc; // Last-used SDL color
// Fill the entire SDL surface, one pixel at a time
lx = -1;
ly = -1;
for(sy = 0; sy < pSurface->h; sy++) {
for(sx = 0; sx < pSurface->w; sx++) {
// Map SDL surface coordinates to Chocochip8 Framebuffer coordinates
fx = sx * (double(chocochip8::gcWidth) / pSurface->w);
fy = sy * (double(chocochip8::gcHeight) / pSurface->h);
// Reuse color if this screen pixel maps to the same ChocoChip8 pixel as the last
if(fx != lx || fy != ly) {
lx = fx;
ly = ly;
// Read Chocochip8 Framebuffer and choose appropriate color,
// note that the MSB of an scanline's bitset is the leftmost pixel.
lc = (mpFramebuffer->at(fy)[(chocochip8::gcWidth - 1) - fx] ? mFgColor : mBgColor);
void DisplaySDL::clear() {
for(auto &scanline : *mpFramebuffer) {
scanline.reset();
}
mDoClear = true;
}
int DisplaySDL::blit(const chocochip8::Scanline &spriteScanline, int y) {
using chocochip8::Scanline;
Scanline &targetScanline = mpFramebuffer->at(y);
bool collision = (spriteScanline & targetScanline).any();
targetScanline ^= spriteScanline;
return collision;
}
void DisplaySDL::updateWindow(bool doWindowUpdate) const {
using chocochip8::Scanline;
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
if(mDoClear) {
for(auto &scanline : *mpDisplayState) {
scanline.reset();
}
SDL_FillRect(pSurface, NULL, mBgColor);
mDoClear = false;
doWindowUpdate = true;
}
for(int y = 0; y < mpFramebuffer->size(); y++) {
Scanline &rNewScanline = (*mpFramebuffer)[y];
Scanline &rOldScanline = (*mpDisplayState)[y];
if(rNewScanline == rOldScanline) {
// Skip scanlines that haven't changed since the last update
continue;
}
for(int x = 0; x < rNewScanline.size(); x++) {
bool isSet = rNewScanline._Unchecked_test(x);
bool wasSet = rOldScanline._Unchecked_test(x);
if(isSet == wasSet) {
continue;
}
// Map framebuffer pixel to SDL surface rectangle,
// note that the MSB of an scanline's bitset is the leftmost pixel.
int z = (chocochip8::gcWidth - 1) - x;
int x1 = ( z * pSurface->w) / chocochip8::gcWidth;
int x2 = ((z + 1) * pSurface->w) / chocochip8::gcWidth;
int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
SDL_Rect rect;
rect.x = x1;
rect.y = y1;
rect.w = x2 - x1;
rect.h = y2 - y1;
Uint32 color = (isSet ? mFgColor : mBgColor);
SDL_FillRect(pSurface, &rect, color);
doWindowUpdate = true;
}
}
if(doWindowUpdate) {
*mpDisplayState = *mpFramebuffer;
SDL_UpdateWindowSurface(mpWindow);
}
// Convert (x, y) indexes into SDL Surface pixel array index
Uint32 *pPixel = static_cast<Uint32*>(static_cast<void*>(
static_cast<char*>(pSurface->pixels)
+ sy * pSurface->pitch
+ sx * pSurface->format->BytesPerPixel
));
*pPixel = lc;
}
}
SDL_UnlockSurface(pSurface);
SDL_UpdateWindowSurface(mpWindow);
}

View File

@@ -2,17 +2,25 @@
#include "Peripherals.hpp"
#include <array>
#include <cstdint>
#include <memory>
#include <SDL2/SDL_video.h>
class DisplaySDL : public chocochip8::Display {
public:
DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000);
~DisplaySDL() override;
void updateWindow() const;
void clear() override;
int blit(const chocochip8::Scanline &scanline, int y) override;
void updateWindow(bool forceWindowUpdate = false) const;
private:
using Framebuffer = std::array<chocochip8::Scanline, chocochip8::gcHeight>;
std::unique_ptr<Framebuffer> mpFramebuffer;
mutable std::unique_ptr<Framebuffer> mpDisplayState;
SDL_Window *mpWindow;
Uint32 mFgColor;
Uint32 mBgColor;
mutable bool mDoClear;
};

View File

@@ -4,6 +4,7 @@
namespace chocochip8 {
// converts any 8-bit sprite row to its high-res 16-bit equivalent
constexpr uint16_t gcvLowResToHighResRowLookupTable[256] = {
0x0000, 0x0003, 0x000C, 0x000F, 0x0030, 0x0033, 0x003C, 0x003F, 0x00C0, 0x00C3, 0x00CC, 0x00CF, 0x00F0, 0x00F3, 0x00FC, 0x00FF,
0x0300, 0x0303, 0x030C, 0x030F, 0x0330, 0x0333, 0x033C, 0x033F, 0x03C0, 0x03C3, 0x03CC, 0x03CF, 0x03F0, 0x03F3, 0x03FC, 0x03FF,
@@ -23,6 +24,7 @@ constexpr uint16_t gcvLowResToHighResRowLookupTable[256] = {
0xFF00, 0xFF03, 0xFF0C, 0xFF0F, 0xFF30, 0xFF33, 0xFF3C, 0xFF3F, 0xFFC0, 0xFFC3, 0xFFCC, 0xFFCF, 0xFFF0, 0xFFF3, 0xFFFC, 0xFFFF
};
// 4x5 sprites for hex digits 0-F
constexpr uint8_t gcvLowResFontData[80] = {
0xF0, 0x90, 0x90, 0x90, 0xF0,
0x20, 0x60, 0x20, 0x20, 0x70,
@@ -42,13 +44,14 @@ constexpr uint8_t gcvLowResFontData[80] = {
0xF0, 0x80, 0xF0, 0x80, 0x80
};
Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad):
Interpreter::Interpreter(Display &display, Buzzer &buzzer, Keypad &keypad, CountdownTimer &delayTimer, CountdownTimer &soundTimer):
mvMemory(scMemorySize),
mCallStack{},
mrDisplay{display},
mrBuzzer{buzzer},
mrKeypad{keypad},
mcTicksPerSecond{ticksPerSecond},
mrDelayTimer(delayTimer),
mrSoundTimer(soundTimer),
mvSpecialReg{},
mvReg{},
mIsHighResMode{false} {
@@ -68,8 +71,11 @@ void Interpreter::tick() {
};
// fetch instruction
sreg_t pc = mvSpecialReg[SR_PC];
unsigned inst = (mvMemory[pc] << 8) | mvMemory[pc + 1];
sreg_t iInstAddr = mvSpecialReg[SR_PC];
unsigned inst = (mvMemory.at(iInstAddr) << 8) | mvMemory.at(iInstAddr + 1);
// increment program counter
mvSpecialReg[SR_PC] += 2;
// extract fields
unsigned iRegDst = (inst & 0x0F00) >> 8; // destination register index
@@ -82,9 +88,7 @@ void Interpreter::tick() {
case 0x0000: // 0NNN - call machine language routine
switch(inst) {
case 0x00E0: // clear display
for(auto &scanline : *mrDisplay.mpFramebuffer) {
scanline.reset();
}
mrDisplay.clear();
break;
case 0x00EE: // return from subroutine
mvSpecialReg[SR_PC] = mCallStack.top();
@@ -99,7 +103,7 @@ void Interpreter::tick() {
mvSpecialReg[SR_PC] = imm12;
break;
case 0x2000: // 2NNN - call subroutine
mCallStack.push(pc);
mCallStack.push(mvSpecialReg[SR_PC]);
mvSpecialReg[SR_PC] = imm12;
break;
case 0x3000: // 3XNN - skip if equal immediate
@@ -135,25 +139,77 @@ void Interpreter::tick() {
case 0xD000: // DXYN - draw
executeDraw(mvReg[iRegDst], mvReg[iRegSrc], opcode);
break;
case 0xE000: // EX9E, EXA1 - keypad access
case 0xE000: // keypad access
switch(inst & 0xF0FF) {
case 0xE09E: // EX9E - skip if key pressed
if(mrKeypad.isKeyPressed(mvReg[iRegDst])) {
mvSpecialReg[SR_PC] += 2;
}
break;
case 0xE0A1: // EXA1 - skip if key not pressed
if(!mrKeypad.isKeyPressed(mvReg[iRegDst])) {
mvSpecialReg[SR_PC] += 2;
}
break;
default:
throw std::invalid_argument("not implemented");
break;
}
break;
case 0xF000: // several unique instructions
switch(inst & 0xF0FF) {
case 0xF007: // FX07 - read timer register
mvReg[iRegDst] = mrDelayTimer.get();
break;
case 0xF00A: // FX0A - wait for a keypress
mvSpecialReg[SR_PC] -= 2;
for(int i = KEY_0; i < KEY_0 + KEY_COUNT; i++) {
if(mrKeypad.isKeyPressed(i)) {
mvSpecialReg[SR_PC] += 2;
break;
}
}
break;
case 0xF015: // FX15 - set timer register
mrDelayTimer.set(mvReg[iRegDst]);
break;
case 0xF018: // FX18 - set sound timer register
if(mvReg[iRegDst] != 0) {
mrBuzzer.on();
}
mrSoundTimer.set(mvReg[iRegDst]);
break;
case 0xF01E: // FX1E - add to I
mvSpecialReg[SR_I] += mvReg[iRegDst];
break;
case 0xF029: // FX29 - set I to address of font sprite data for digit X
mvSpecialReg[SR_I] = scLowRestFontAddr + 5 * mvReg[iRegDst];
break;
case 0xF033: // FX33 - convert to bcd
mvMemory.at(mvSpecialReg[SR_I]) = (mvReg[iRegDst] / 100) % 10;
mvMemory.at(mvSpecialReg[SR_I] + 1) = (mvReg[iRegDst] / 10) % 10;
mvMemory.at(mvSpecialReg[SR_I] + 2) = mvReg[iRegDst] % 10;
break;
case 0xF055: // FX55 - dump registers
for(int i = 0; i <= iRegDst - R_V0; i++) {
mvMemory.at(mvSpecialReg[SR_I]++) = mvReg[R_V0 + i];
}
break;
case 0xF065: // FX65 - restore registers
for(int i = 0; i <= iRegDst - R_V0; i++) {
mvReg[R_V0 + i] = mvMemory.at(mvSpecialReg[SR_I]++);
}
break;
default:
throw std::invalid_argument("not implemented");
break;
}
break;
}
// increment PC
mvSpecialReg[SR_PC] += 2;
// decrement timers
if(mvSpecialReg[SR_T1] > 0) {
mvSpecialReg[SR_T1] -= 1;
}
if(mvSpecialReg[SR_T2] > 0) {
mvSpecialReg[SR_T2] -= 1;
if(mvSpecialReg[SR_T2] == 0) {
if(mrSoundTimer.get() == 0) {
mrBuzzer.off();
}
}
}
void Interpreter::loadProgram(char const* data, size_t count, size_t where) {
@@ -172,27 +228,30 @@ void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) {
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;
mvReg[iReg] = operand << 1;
// VF = shifted out bit
mvReg[R_VF] = (operand & 0x80) ? 1 : 0;
break;
case Opcode::RSH:
mvReg[R_VF] = (mvReg[iReg] & 0x01) ? 1 : 0;
mvReg[iReg] >>= 1;
mvReg[iReg] = operand >> 1;
// VF = shifted out bit
mvReg[R_VF] = (operand & 0x01) ? 1 : 0;
break;
case Opcode::ADD:
tmp = mvReg[iReg] + operand;
mvReg[R_VF] = (tmp < mvReg[iReg]) ? 1 : 0;
mvReg[iReg] = tmp;
mvReg[iReg] = mvReg[iReg] + operand;
// VF = 1 if carry occurs, VF = 0 if no carry
mvReg[R_VF] = (mvReg[iReg] < operand) ? 1 : 0;
break;
case Opcode::SUB:
tmp = mvReg[iReg] - operand;
mvReg[R_VF] = (tmp > mvReg[iReg]) ? 1 : 0;
mvReg[iReg] = tmp;
tmp = mvReg[iReg];
mvReg[iReg] = mvReg[iReg] - operand;
// VF = 0 if borrow occurs, VF = 1 if no borrow
mvReg[R_VF] = (mvReg[iReg] > tmp) ? 0 : 1;
break;
case Opcode::SUB2:
tmp = operand - mvReg[iReg];
mvReg[R_VF] = (tmp > operand) ? 1 : 0;
mvReg[iReg] = tmp;
mvReg[iReg] = operand - mvReg[iReg];
// VF = 0 if borrow occurs, VF = 1 if no borrow
mvReg[R_VF] = (mvReg[iReg] > operand) ? 0 : 1;
break;
case Opcode::JEQ:
if(mvReg[iReg] == operand) {
@@ -229,14 +288,14 @@ void Interpreter::executeDraw(uint8_t x, uint8_t y, uint8_t n) {
uint16_t spriteRowBits;
if(mIsHighResMode) {
// draws an 8xN sprite
spriteRowBits = mvMemory[iMemAddr++] << 8;
spriteRowBits = mvMemory.at(iMemAddr++) << 8;
if(n == 0) {
// draws an 16xN sprite, so fetch another byte from sprite data
spriteRowBits |= mvMemory[iMemAddr++];
spriteRowBits |= mvMemory.at(iMemAddr++);
}
} else {
// in low-res mode, each sprite pixel draws two on-screen pixels
spriteRowBits = gcvLowResToHighResRowLookupTable[mvMemory[iMemAddr++]];
spriteRowBits = gcvLowResToHighResRowLookupTable[mvMemory.at(iMemAddr++)];
}
// convert to bitset and shift into absolute horizontal position on the screen
@@ -249,12 +308,10 @@ void Interpreter::executeDraw(uint8_t x, uint8_t y, uint8_t n) {
// we draw one scanline per sprite row in high-res mode, but twice in low-res mode
for(int j = 0; j < (mIsHighResMode ? 1 : 2); j++) {
Scanline &targetScanline = mrDisplay.mpFramebuffer->at(y + i * (mIsHighResMode ? 1 : 2) + j);
if((targetScanline & spriteScanline) != 0) {
// blit the sprite bitset into the screen
if(mrDisplay.blit(spriteScanline, y + i * (mIsHighResMode ? 1 : 2) + j) != 0) {
collisionCount += 1;
}
// blit the sprite bitset into the screen
targetScanline ^= spriteScanline;
}
// update state flags

View File

@@ -25,7 +25,7 @@ public:
};
enum {
SR_PC, SR_I, SR_T1, SR_T2,
SR_PC, SR_I, /*SR_T1, SR_T2,*/
SR_COUNT
};
@@ -34,7 +34,7 @@ public:
};
public:
Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad);
Interpreter(Display &display, Buzzer &buzzer, Keypad &keypad, CountdownTimer &delayTimer, CountdownTimer &soundTimer);
void tick();
void loadProgram(char const* data, size_t count, size_t where = scResetVector);
@@ -48,7 +48,8 @@ private:
Display &mrDisplay;
Buzzer &mrBuzzer;
Keypad &mrKeypad;
const unsigned mcTicksPerSecond;
CountdownTimer &mrDelayTimer;
CountdownTimer &mrSoundTimer;
sreg_t mvSpecialReg[SR_COUNT];
reg_t mvReg[R_COUNT];
bool mIsHighResMode;

36
KeypadSDL.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "KeypadSDL.hpp"
bool KeypadSDL::isKeyPressed(int key) const {
return mvKeyDown.at(key);
}
void KeypadSDL::processEvent(SDL_Event &e) {
auto keymap = [](int sdlKeyCode) -> int {
switch(sdlKeyCode) {
case SDLK_1: return chocochip8::KEY_1;
case SDLK_2: return chocochip8::KEY_2;
case SDLK_3: return chocochip8::KEY_3;
case SDLK_4: return chocochip8::KEY_C;
case SDLK_q: return chocochip8::KEY_4;
case SDLK_w: return chocochip8::KEY_5;
case SDLK_e: return chocochip8::KEY_6;
case SDLK_r: return chocochip8::KEY_D;
case SDLK_a: return chocochip8::KEY_7;
case SDLK_s: return chocochip8::KEY_8;
case SDLK_d: return chocochip8::KEY_9;
case SDLK_f: return chocochip8::KEY_E;
case SDLK_z: return chocochip8::KEY_A;
case SDLK_x: return chocochip8::KEY_0;
case SDLK_c: return chocochip8::KEY_B;
case SDLK_v: return chocochip8::KEY_F;
default : return -1;
}
};
if(e.type == SDL_KEYUP || e.type == SDL_KEYDOWN) {
int k = keymap(e.key.keysym.sym);
if(k != -1) {
mvKeyDown[k] = (e.type == SDL_KEYDOWN);
}
}
}

12
KeypadSDL.hpp Normal file
View File

@@ -0,0 +1,12 @@
#include "Peripherals.hpp"
#include <SDL2/SDL_events.h>
#include <array>
class KeypadSDL : public chocochip8::Keypad {
public:
bool isKeyPressed(int key) const override;
void processEvent(SDL_Event &e);
private:
std::array<bool, chocochip8::KEY_COUNT> mvKeyDown;
};

View File

@@ -1,30 +1,26 @@
#pragma once
#include <array>
#include <bitset>
#include <memory>
namespace chocochip8 {
constexpr size_t gcWidth = 128;
constexpr size_t gcHeight = 64;
using Scanline = std::bitset<gcWidth>;
using Framebuffer = std::array<Scanline, gcHeight>;
enum class Key {
enum {
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
KEY_C, KEY_D, KEY_E, KEY_F,
KEY_COUNT
};
class Display {
public:
friend class Interpreter;
Display(): mpFramebuffer{std::make_unique<Framebuffer>()} {}
virtual ~Display() = default;
protected:
std::unique_ptr<Framebuffer> mpFramebuffer;
virtual int blit(const Scanline& spriteScanline, int y) = 0;
virtual void clear() = 0;
};
class Buzzer {
@@ -37,7 +33,14 @@ namespace chocochip8 {
class Keypad {
public:
virtual ~Keypad() = default;
virtual bool isKeyPressed(Key key) = 0;
virtual bool isKeyPressed(int key) const = 0;
};
class CountdownTimer {
public:
virtual ~CountdownTimer() = default;
virtual void set(unsigned value) = 0;
virtual unsigned get() const = 0;
};
}; // namespace chocochip8

View File

@@ -1,57 +1,58 @@
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h>
#include "BuzzerSDL.hpp"
#include "CountdownTimerSDL.hpp"
#include "DisplaySDL.hpp"
#include "KeypadSDL.hpp"
#include "Interpreter.hpp"
#include "Peripherals.hpp"
int main(int argc, char* args[]) {
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << '\n';
return 1;
}
class TestKeypad : public chocochip8::Keypad {
bool isKeyPressed(chocochip8::Key) override { return false; }
};
BuzzerSDL buzzer(440);
DisplaySDL display(1280, 640);
TestKeypad keypad;
chocochip8::Interpreter chip8(90, display, buzzer, keypad);
int N = 4;
DisplaySDL display(128*N, 64*N);
KeypadSDL keypad;
CountdownTimerSDL delayTimer(60);
CountdownTimerSDL soundTimer(60);
CountdownTimerSDL displayTimer(60);
chocochip8::Interpreter chip8(display, buzzer, keypad, delayTimer, soundTimer);
uint8_t prog[] = {
0xA0, 0x00, // LD I,0
0x60, 0x00, // LD $0,0
0x61, 0x00, // LD $1,0
0xD0, 0x15, // DRW $0, $1, 5
0xA0, 0x05, // LD I,5
0x60, 0x3C, // LD $0,60
0x61, 0x00, // LD $1,0
0xD0, 0x15, // DRW $0, $1, 5
0xA0, 0x0A, // LD I,10
0x60, 0x00, // LD $0,0
0x61, 0x1B, // LD $1,27
0xD0, 0x15, // DRW $0, $1, 5
0xA0, 0x0F, // LD I,15
0x60, 0x3C, // LD $0,60
0x61, 0x1B, // LD $1,27
0xD0, 0x15, // DRW $0, $1, 5
};
chip8.loadProgram((char*)prog, sizeof(prog));
for(int i = 0; i < sizeof(prog) / 2; i++) {
chip8.tick();
}
auto rom = std::vector<char>();
auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary);
std::copy(
std::istreambuf_iterator<char>(romfile),
std::istreambuf_iterator<char>(),
std::back_insert_iterator(rom)
);
chip8.loadProgram(rom.data(), rom.size());
SDL_Event event;
while(SDL_WaitEvent(&event) && event.type != SDL_QUIT) {
bool done = false;
while(!done) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
done = true;
} else {
keypad.processEvent(event);
}
}
chip8.tick();
if(displayTimer.get() == 0) {
display.updateWindow();
displayTimer.set(1);
}
}
buzzer.off();
SDL_Quit();
return 0;
}