7 Commits

Author SHA1 Message Date
1d3e843653 display now uses SDL_FillRect 2025-03-14 04:53:47 -04:00
4f7083d7c7 optimized display update region 2025-03-14 02:50:01 -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
ea4b961d53 implemented draw function in interpreter 2024-11-28 19:38:11 -05:00
6 changed files with 281 additions and 83 deletions

View File

@@ -35,43 +35,47 @@ DisplaySDL::~DisplaySDL() {
SDL_DestroyWindow(mpWindow); SDL_DestroyWindow(mpWindow);
} }
void DisplaySDL::clear() {
Display::clear();
SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor);
SDL_UpdateWindowSurface(mpWindow);
miTopDirtyScanline = chocochip8::gcHeight - 1;
miBottomDirtyScanline = 0;
}
void DisplaySDL::updateWindow() const { void DisplaySDL::updateWindow() const {
if(miTopDirtyScanline > miBottomDirtyScanline) {
// No changes since the last update
return;
}
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow); SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
SDL_LockSurface(pSurface); for(int y = 0; y < chocochip8::gcHeight; y++) {
if(y < miTopDirtyScanline || miBottomDirtyScanline < y) {
// Skip scanlines that haven't changed since the last update
continue;
}
int fx, fy; // ChocoChip8 Framebuffer coordinates for(int x = 0; x < chocochip8::gcWidth; x++) {
int sx, sy; // SDL Surface coordinates // Map framebuffer pixel to SDL surface rectangle
int lx, ly; // Last-used ChocoChip8 Framebuffer coordinates int x1 = ( x * pSurface->w) / chocochip8::gcWidth;
Uint32 lc; // Last-used SDL color int x2 = ((x + 1) * pSurface->w) / chocochip8::gcWidth;
int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
// Fill the entire SDL surface, one pixel at a time int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
lx = -1; SDL_Rect rect;
ly = -1; rect.x = x1;
for(sy = 0; sy < pSurface->h; sy++) { rect.y = y1;
for(sx = 0; sx < pSurface->w; sx++) { rect.w = x2 - x1;
// Map SDL surface coordinates to Chocochip8 Framebuffer coordinates rect.h = y2 - y1;
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 // Read Chocochip8 Framebuffer and choose appropriate color,
if(fx != lx || fy != ly) { // note that the MSB of an scanline's bitset is the leftmost pixel.
lx = fx; Uint32 color = (mpFramebuffer->at(y)[(chocochip8::gcWidth - 1) - x] ? mFgColor : mBgColor);
ly = ly; SDL_FillRect(pSurface, &rect, color);
// 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);
}
// 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); miTopDirtyScanline = chocochip8::gcHeight - 1;
miBottomDirtyScanline = 0;
SDL_UpdateWindowSurface(mpWindow); SDL_UpdateWindowSurface(mpWindow);
} }

View File

@@ -7,8 +7,9 @@
class DisplaySDL : public chocochip8::Display { class DisplaySDL : public chocochip8::Display {
public: public:
DisplaySDL(int w, int h, uint32_t fgCol, uint32_t bgCol); DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000);
~DisplaySDL() override; ~DisplaySDL() override;
void clear() override;
void updateWindow() const; void updateWindow() const;
private: private:

View File

@@ -4,6 +4,46 @@
namespace chocochip8 { 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,
0x0C00, 0x0C03, 0x0C0C, 0x0C0F, 0x0C30, 0x0C33, 0x0C3C, 0x0C3F, 0x0CC0, 0x0CC3, 0x0CCC, 0x0CCF, 0x0CF0, 0x0CF3, 0x0CFC, 0x0CFF,
0x0F00, 0x0F03, 0x0F0C, 0x0F0F, 0x0F30, 0x0F33, 0x0F3C, 0x0F3F, 0x0FC0, 0x0FC3, 0x0FCC, 0x0FCF, 0x0FF0, 0x0FF3, 0x0FFC, 0x0FFF,
0x3000, 0x3003, 0x300C, 0x300F, 0x3030, 0x3033, 0x303C, 0x303F, 0x30C0, 0x30C3, 0x30CC, 0x30CF, 0x30F0, 0x30F3, 0x30FC, 0x30FF,
0x3300, 0x3303, 0x330C, 0x330F, 0x3330, 0x3333, 0x333C, 0x333F, 0x33C0, 0x33C3, 0x33CC, 0x33CF, 0x33F0, 0x33F3, 0x33FC, 0x33FF,
0x3C00, 0x3C03, 0x3C0C, 0x3C0F, 0x3C30, 0x3C33, 0x3C3C, 0x3C3F, 0x3CC0, 0x3CC3, 0x3CCC, 0x3CCF, 0x3CF0, 0x3CF3, 0x3CFC, 0x3CFF,
0x3F00, 0x3F03, 0x3F0C, 0x3F0F, 0x3F30, 0x3F33, 0x3F3C, 0x3F3F, 0x3FC0, 0x3FC3, 0x3FCC, 0x3FCF, 0x3FF0, 0x3FF3, 0x3FFC, 0x3FFF,
0xC000, 0xC003, 0xC00C, 0xC00F, 0xC030, 0xC033, 0xC03C, 0xC03F, 0xC0C0, 0xC0C3, 0xC0CC, 0xC0CF, 0xC0F0, 0xC0F3, 0xC0FC, 0xC0FF,
0xC300, 0xC303, 0xC30C, 0xC30F, 0xC330, 0xC333, 0xC33C, 0xC33F, 0xC3C0, 0xC3C3, 0xC3CC, 0xC3CF, 0xC3F0, 0xC3F3, 0xC3FC, 0xC3FF,
0xCC00, 0xCC03, 0xCC0C, 0xCC0F, 0xCC30, 0xCC33, 0xCC3C, 0xCC3F, 0xCCC0, 0xCCC3, 0xCCCC, 0xCCCF, 0xCCF0, 0xCCF3, 0xCCFC, 0xCCFF,
0xCF00, 0xCF03, 0xCF0C, 0xCF0F, 0xCF30, 0xCF33, 0xCF3C, 0xCF3F, 0xCFC0, 0xCFC3, 0xCFCC, 0xCFCF, 0xCFF0, 0xCFF3, 0xCFFC, 0xCFFF,
0xF000, 0xF003, 0xF00C, 0xF00F, 0xF030, 0xF033, 0xF03C, 0xF03F, 0xF0C0, 0xF0C3, 0xF0CC, 0xF0CF, 0xF0F0, 0xF0F3, 0xF0FC, 0xF0FF,
0xF300, 0xF303, 0xF30C, 0xF30F, 0xF330, 0xF333, 0xF33C, 0xF33F, 0xF3C0, 0xF3C3, 0xF3CC, 0xF3CF, 0xF3F0, 0xF3F3, 0xF3FC, 0xF3FF,
0xFC00, 0xFC03, 0xFC0C, 0xFC0F, 0xFC30, 0xFC33, 0xFC3C, 0xFC3F, 0xFCC0, 0xFCC3, 0xFCCC, 0xFCCF, 0xFCF0, 0xFCF3, 0xFCFC, 0xFCFF,
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,
0xF0, 0x10, 0xF0, 0x80, 0xF0,
0xF0, 0x10, 0xF0, 0x10, 0xF0,
0x90, 0x90, 0xF0, 0x10, 0x10,
0xF0, 0x80, 0xF0, 0x10, 0xF0,
0xF0, 0x80, 0xF0, 0x90, 0xF0,
0xF0, 0x10, 0x20, 0x40, 0x40,
0xF0, 0x90, 0xF0, 0x90, 0xF0,
0xF0, 0x90, 0xF0, 0x10, 0xF0,
0xF0, 0x90, 0xF0, 0x90, 0x90,
0xE0, 0x90, 0xE0, 0x90, 0xE0,
0xF0, 0x80, 0x80, 0x80, 0xF0,
0xE0, 0x90, 0x90, 0x90, 0xE0,
0xF0, 0x80, 0xF0, 0x80, 0xF0,
0xF0, 0x80, 0xF0, 0x80, 0x80
};
Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad): Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad):
mvMemory(scMemorySize), mvMemory(scMemorySize),
mCallStack{}, mCallStack{},
@@ -12,7 +52,13 @@ Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzz
mrKeypad{keypad}, mrKeypad{keypad},
mcTicksPerSecond{ticksPerSecond}, mcTicksPerSecond{ticksPerSecond},
mvSpecialReg{}, mvSpecialReg{},
mvReg{} {} mvReg{},
mIsHighResMode{false} {
// Font sprite data goes into the interpreter reserved memory area
memcpy(mvMemory.data() + scLowRestFontAddr, gcvLowResFontData, sizeof(gcvLowResFontData));
// Initialize any non-zero registers at startup
mvSpecialReg[SR_PC] = scResetVector;
}
void Interpreter::tick() { void Interpreter::tick() {
// decodes all possible machine code arithmetic opcodes // decodes all possible machine code arithmetic opcodes
@@ -24,13 +70,16 @@ void Interpreter::tick() {
}; };
// fetch instruction // fetch instruction
sreg_t pc = mvSpecialReg[SR_PC]; sreg_t iInstAddr = mvSpecialReg[SR_PC];
unsigned inst = (mvMemory[pc] << 8) | mvMemory[pc + 1]; unsigned inst = (mvMemory.at(iInstAddr) << 8) | mvMemory.at(iInstAddr + 1);
// increment program counter
mvSpecialReg[SR_PC] += 2;
// extract fields // extract fields
unsigned regDst = (inst & 0x0F00) >> 8; // destination register index unsigned iRegDst = (inst & 0x0F00) >> 8; // destination register index
unsigned regSrc = (inst & 0x00F0) >> 4; // source register index unsigned iRegSrc = (inst & 0x00F0) >> 4; // source register index
unsigned opcode = (inst & 0x000F); // arithmetic opcode unsigned opcode = (inst & 0x000F); // arithmetic opcode or sprite index
unsigned imm8 = (inst & 0x00FF); // 8-bit immediate unsigned imm8 = (inst & 0x00FF); // 8-bit immediate
unsigned imm12 = (inst & 0x0FFF); // 12-bit immediate unsigned imm12 = (inst & 0x0FFF); // 12-bit immediate
@@ -38,9 +87,7 @@ void Interpreter::tick() {
case 0x0000: // 0NNN - call machine language routine case 0x0000: // 0NNN - call machine language routine
switch(inst) { switch(inst) {
case 0x00E0: // clear display case 0x00E0: // clear display
for(auto &scanline : *mrDisplay.mpFramebuffer) { mrDisplay.clear();
scanline.reset();
}
break; break;
case 0x00EE: // return from subroutine case 0x00EE: // return from subroutine
mvSpecialReg[SR_PC] = mCallStack.top(); mvSpecialReg[SR_PC] = mCallStack.top();
@@ -55,29 +102,29 @@ void Interpreter::tick() {
mvSpecialReg[SR_PC] = imm12; mvSpecialReg[SR_PC] = imm12;
break; break;
case 0x2000: // 2NNN - call subroutine case 0x2000: // 2NNN - call subroutine
mCallStack.push(pc); mCallStack.push(mvSpecialReg[SR_PC]);
mvSpecialReg[SR_PC] = imm12; mvSpecialReg[SR_PC] = imm12;
break; break;
case 0x3000: // 3XNN - skip if equal immediate case 0x3000: // 3XNN - skip if equal immediate
executeArithmetic(Opcode::JEQ, regDst, imm8); executeArithmetic(Opcode::JEQ, iRegDst, imm8);
break; break;
case 0x4000: // 4XNN - skip if nonequal immediate case 0x4000: // 4XNN - skip if nonequal immediate
executeArithmetic(Opcode::JNEQ, regDst, imm8); executeArithmetic(Opcode::JNEQ, iRegDst, imm8);
break; break;
case 0x5000: // 5XY0 - skip if equal case 0x5000: // 5XY0 - skip if equal
executeArithmetic(Opcode::JEQ, regDst, mvReg[regSrc]); executeArithmetic(Opcode::JEQ, iRegDst, mvReg[iRegSrc]);
break; break;
case 0x6000: // 6XNN - load immediate case 0x6000: // 6XNN - load immediate
executeArithmetic(Opcode::SET, regDst, imm8); executeArithmetic(Opcode::SET, iRegDst, imm8);
break; break;
case 0x7000: // 7XNN - increment case 0x7000: // 7XNN - increment
executeArithmetic(Opcode::ADD, regDst, imm8); executeArithmetic(Opcode::ADD, iRegDst, imm8);
break; break;
case 0x8000: // 8XNN - general arithmetic case 0x8000: // 8XNN - general arithmetic
executeArithmetic(opcodeMap[opcode], regDst, mvReg[regSrc]); executeArithmetic(opcodeMap[opcode], iRegDst, mvReg[iRegSrc]);
break; break;
case 0x9000: // 9XY0 - skip if nonequal case 0x9000: // 9XY0 - skip if nonequal
executeArithmetic(Opcode::JNEQ, regDst, mvReg[regSrc]); executeArithmetic(Opcode::JNEQ, iRegDst, mvReg[iRegSrc]);
break; break;
case 0xA000: // ANNN - load I case 0xA000: // ANNN - load I
mvSpecialReg[SR_I] = imm12; mvSpecialReg[SR_I] = imm12;
@@ -86,19 +133,55 @@ void Interpreter::tick() {
mvSpecialReg[SR_I] = mvReg[R_V0] + imm12; mvSpecialReg[SR_I] = mvReg[R_V0] + imm12;
break; break;
case 0xC000: // CXNN - load random case 0xC000: // CXNN - load random
executeArithmetic(Opcode::RAND, regDst, imm8); executeArithmetic(Opcode::RAND, iRegDst, imm8);
break; break;
case 0xD000: // DXYN - draw case 0xD000: // DXYN - draw
executeDraw(mvReg[iRegDst], mvReg[iRegSrc], opcode);
break; break;
case 0xE000: // EX9E, EXA1 - keypad access case 0xE000: // EX9E, EXA1 - keypad access
break; break;
case 0xF000: // several unique instructions case 0xF000: // several unique instructions
switch(inst & 0xF0FF) {
case 0xF007: // FX07 - read timer register
mvReg[iRegDst] = (mvSpecialReg[SR_T1] * scTimerFreq + (mcTicksPerSecond - 1)) / mcTicksPerSecond;
break;
case 0xF015: // FX15 - set timer register
mvSpecialReg[SR_T1] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
break;
case 0xF018: // FX18 - set sound timer register
if(mvSpecialReg[SR_T2] == 0 && mvReg[iRegDst] != 0) {
mrBuzzer.on();
}
mvSpecialReg[SR_T2] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
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; break;
} }
// increment PC
mvSpecialReg[SR_PC] += 2;
// decrement timers // decrement timers
if(mvSpecialReg[SR_T1] > 0) { if(mvSpecialReg[SR_T1] > 0) {
mvSpecialReg[SR_T1] -= 1; mvSpecialReg[SR_T1] -= 1;
@@ -111,11 +194,11 @@ void Interpreter::tick() {
} }
} }
void Interpreter::loadProgram(size_t where, char const* data, size_t count) { void Interpreter::loadProgram(char const* data, size_t count, size_t where) {
if(where + count > scMemorySize) { if(where + count > scMemorySize) {
throw std::out_of_range("program exceeds memory bounds or capacity"); throw std::out_of_range("program exceeds memory bounds or capacity");
} }
memcpy(mvMemory.data(), data, count); memcpy(mvMemory.data() + where, data, count);
} }
void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) { void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) {
@@ -127,27 +210,30 @@ void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) {
case Opcode::XOR: mvReg[iReg] ^= operand; break; case Opcode::XOR: mvReg[iReg] ^= operand; break;
case Opcode::RAND: mvReg[iReg] = rand() & operand; break; case Opcode::RAND: mvReg[iReg] = rand() & operand; break;
case Opcode::LSH: case Opcode::LSH:
mvReg[R_VF] = (mvReg[iReg] & 0x80) ? 1 : 0; mvReg[iReg] = operand << 1;
mvReg[iReg] <<= 1; // VF = shifted out bit
mvReg[R_VF] = (operand & 0x80) ? 1 : 0;
break; break;
case Opcode::RSH: case Opcode::RSH:
mvReg[R_VF] = (mvReg[iReg] & 0x01) ? 1 : 0; mvReg[iReg] = operand >> 1;
mvReg[iReg] >>= 1; // VF = shifted out bit
mvReg[R_VF] = (operand & 0x01) ? 1 : 0;
break; break;
case Opcode::ADD: case Opcode::ADD:
tmp = mvReg[iReg] + operand; mvReg[iReg] = mvReg[iReg] + operand;
mvReg[R_VF] = (tmp < mvReg[iReg]) ? 1 : 0; // VF = 1 if carry occurs, VF = 0 if no carry
mvReg[iReg] = tmp; mvReg[R_VF] = (mvReg[iReg] < operand) ? 1 : 0;
break; break;
case Opcode::SUB: case Opcode::SUB:
tmp = mvReg[iReg] - operand; tmp = mvReg[iReg];
mvReg[R_VF] = (tmp > mvReg[iReg]) ? 1 : 0; mvReg[iReg] = mvReg[iReg] - operand;
mvReg[iReg] = tmp; // VF = 0 if borrow occurs, VF = 1 if no borrow
mvReg[R_VF] = (mvReg[iReg] > tmp) ? 0 : 1;
break; break;
case Opcode::SUB2: case Opcode::SUB2:
tmp = operand - mvReg[iReg]; mvReg[iReg] = operand - mvReg[iReg];
mvReg[R_VF] = (tmp > operand) ? 1 : 0; // VF = 0 if borrow occurs, VF = 1 if no borrow
mvReg[iReg] = tmp; mvReg[R_VF] = (mvReg[iReg] > operand) ? 0 : 1;
break; break;
case Opcode::JEQ: case Opcode::JEQ:
if(mvReg[iReg] == operand) { if(mvReg[iReg] == operand) {
@@ -165,4 +251,60 @@ void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) {
} }
} }
void Interpreter::executeDraw(uint8_t x, uint8_t y, uint8_t n) {
size_t iMemAddr = mvSpecialReg[SR_I]; // address of the sprite data in memory
int collisionCount = 0; // number of scanlines in which any pixel changes from on to off
// coordinates are doubled in low resolution mode
if(!mIsHighResMode) {
x *= 2;
y *= 2;
}
x %= gcWidth;
y %= gcHeight;
// make x=0 be the right side of the screen instead of the left
x = gcWidth - x;
// draw each row of the sprite
for(int i = 0; i < (mIsHighResMode && n == 0 ? 16 : n); i++) {
uint16_t spriteRowBits;
if(mIsHighResMode) {
// draws an 8xN sprite
spriteRowBits = mvMemory.at(iMemAddr++) << 8;
if(n == 0) {
// draws an 16xN sprite, so fetch another byte from sprite data
spriteRowBits |= mvMemory.at(iMemAddr++);
}
} else {
// in low-res mode, each sprite pixel draws two on-screen pixels
spriteRowBits = gcvLowResToHighResRowLookupTable[mvMemory.at(iMemAddr++)];
}
// convert to bitset and shift into absolute horizontal position on the screen
Scanline spriteScanline(spriteRowBits);
if(x < 16) {
spriteScanline >>= (16 - x);
} else {
spriteScanline <<= (x - 16);
}
// 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.getModifyableScanline(y + i * (mIsHighResMode ? 1 : 2) + j);
if((targetScanline & spriteScanline) != 0) {
collisionCount += 1;
}
// blit the sprite bitset into the screen
targetScanline ^= spriteScanline;
}
// update state flags
if(mIsHighResMode) {
mvReg[R_VF] = collisionCount;
} else {
mvReg[R_VF] = (collisionCount > 0 ? 1 : 0);
}
}
}
}; // namespace chochochip8 }; // namespace chochochip8

View File

@@ -14,7 +14,9 @@ private:
using sreg_t = unsigned; using sreg_t = unsigned;
public: public:
constexpr static sreg_t scLowRestFontAddr = 0x0000;
constexpr static sreg_t scResetVector = 0x0200; constexpr static sreg_t scResetVector = 0x0200;
constexpr static sreg_t scTimerFreq = 60;
constexpr static size_t scMemorySize = 4096; constexpr static size_t scMemorySize = 4096;
enum { enum {
@@ -35,10 +37,11 @@ public:
public: public:
Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad); Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad);
void tick(); void tick();
void loadProgram(size_t where, char const* data, size_t count); void loadProgram(char const* data, size_t count, size_t where = scResetVector);
private: private:
void executeArithmetic(Opcode opcode, int iReg, reg_t operand); void executeArithmetic(Opcode opcode, int iReg, reg_t operand);
void executeDraw(uint8_t x, uint8_t y, uint8_t n);
private: private:
std::vector<uint8_t> mvMemory; std::vector<uint8_t> mvMemory;
@@ -49,6 +52,7 @@ private:
const unsigned mcTicksPerSecond; const unsigned mcTicksPerSecond;
sreg_t mvSpecialReg[SR_COUNT]; sreg_t mvSpecialReg[SR_COUNT];
reg_t mvReg[R_COUNT]; reg_t mvReg[R_COUNT];
bool mIsHighResMode;
}; // class Interpreter }; // class Interpreter
}; // namespace chocohip8 }; // namespace chocohip8

View File

@@ -20,11 +20,32 @@ namespace chocochip8 {
class Display { class Display {
public: public:
friend class Interpreter; Display():
Display(): mpFramebuffer{std::make_unique<Framebuffer>()} {} mpFramebuffer{std::make_unique<Framebuffer>()},
miTopDirtyScanline{0},
miBottomDirtyScanline{gcHeight - 1} {}
virtual ~Display() = default; virtual ~Display() = default;
virtual void clear() {
for(auto &scanline : *mpFramebuffer) {
scanline.reset();
}
miTopDirtyScanline = 0;
miBottomDirtyScanline = gcHeight - 1;
}
virtual Scanline& getModifyableScanline(size_t y) {
Scanline& res = mpFramebuffer->at(y);
miTopDirtyScanline = std::min(miTopDirtyScanline, y);
miBottomDirtyScanline = std::max(miBottomDirtyScanline, y);
return res;
}
protected: protected:
std::unique_ptr<Framebuffer> mpFramebuffer; std::unique_ptr<Framebuffer> mpFramebuffer;
mutable size_t miTopDirtyScanline;
mutable size_t miBottomDirtyScanline;
}; };
class Buzzer { class Buzzer {
@@ -37,7 +58,7 @@ namespace chocochip8 {
class Keypad { class Keypad {
public: public:
virtual ~Keypad() = default; virtual ~Keypad() = default;
virtual void isKeyPressed(Key key) = 0; virtual bool isKeyPressed(Key key) = 0;
}; };
}; // namespace chocochip8 }; // namespace chocochip8

View File

@@ -1,32 +1,58 @@
#include <iostream> #include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "BuzzerSDL.hpp" #include "BuzzerSDL.hpp"
#include "DisplaySDL.hpp" #include "DisplaySDL.hpp"
#include "Interpreter.hpp"
#include "Peripherals.hpp" #include "Peripherals.hpp"
#include "SDL_events.h"
#include "SDL_timer.h"
int main(int argc, char* args[]) { int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << '\n'; std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << '\n';
return 1; return 1;
} }
class DisplayTest : public DisplaySDL { class TestKeypad : public chocochip8::Keypad {
public: bool isKeyPressed(chocochip8::Key) override { return false; }
DisplayTest() : DisplaySDL(1280, 640, 0xff0000, 0x00ff00) {
for(int i = 0; i < chocochip8::gcHeight; i+=2) {
mpFramebuffer->at(i).set();
}
}
}; };
BuzzerSDL buzzer(440); BuzzerSDL buzzer(440);
//buzzer.on(); DisplaySDL display(1280, 640);
DisplayTest display; TestKeypad keypad;
chocochip8::Interpreter chip8(1000, display, buzzer, keypad);
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; SDL_Event event;
while(SDL_WaitEvent(&event) && event.type != SDL_QUIT) { bool done = false;
Uint64 start, end;
start = SDL_GetTicks64();
while(!done) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
done = true;
}
}
chip8.tick();
display.updateWindow(); display.updateWindow();
SDL_Delay(1);
end = SDL_GetTicks64();
if(end - start > 3)
std::cout << "Frame took too long: " << end - start << "\n";
start = end;
} }
buzzer.off(); buzzer.off();