Compare commits
2 Commits
ea4b961d53
...
8c49ec6d86
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c49ec6d86 | |||
| 830ab9eda7 |
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
namespace chocochip8 {
|
namespace chocochip8 {
|
||||||
|
|
||||||
|
// converts any 8-bit sprite row to its high-res 16-bit equivalent
|
||||||
constexpr uint16_t gcvLowResToHighResRowLookupTable[256] = {
|
constexpr uint16_t gcvLowResToHighResRowLookupTable[256] = {
|
||||||
0x0000, 0x0003, 0x000C, 0x000F, 0x0030, 0x0033, 0x003C, 0x003F, 0x00C0, 0x00C3, 0x00CC, 0x00CF, 0x00F0, 0x00F3, 0x00FC, 0x00FF,
|
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,
|
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
|
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] = {
|
constexpr uint8_t gcvLowResFontData[80] = {
|
||||||
0xF0, 0x90, 0x90, 0x90, 0xF0,
|
0xF0, 0x90, 0x90, 0x90, 0xF0,
|
||||||
0x20, 0x60, 0x20, 0x20, 0x70,
|
0x20, 0x60, 0x20, 0x20, 0x70,
|
||||||
@@ -68,8 +70,11 @@ 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[iInstAddr] << 8) | mvMemory[iInstAddr + 1];
|
||||||
|
|
||||||
|
// increment program counter
|
||||||
|
mvSpecialReg[SR_PC] += 2;
|
||||||
|
|
||||||
// extract fields
|
// extract fields
|
||||||
unsigned iRegDst = (inst & 0x0F00) >> 8; // destination register index
|
unsigned iRegDst = (inst & 0x0F00) >> 8; // destination register index
|
||||||
@@ -99,7 +104,7 @@ 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
|
||||||
@@ -138,12 +143,47 @@ void Interpreter::tick() {
|
|||||||
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] + mcTicksPerSecond - 1) / mcTicksPerSecond;
|
||||||
|
break;
|
||||||
|
case 0xF015: // FX15 - set timer register
|
||||||
|
mvSpecialReg[SR_T1] = mcTicksPerSecond * mvReg[iRegDst];
|
||||||
|
break;
|
||||||
|
case 0xF018: // FX18 - set sound timer register
|
||||||
|
if(mvSpecialReg[SR_T1] == 0 && mvReg[iRegDst] != 0) {
|
||||||
|
mrBuzzer.on();
|
||||||
|
}
|
||||||
|
mvSpecialReg[SR_T1] = mcTicksPerSecond * 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[mvSpecialReg[SR_I]] = (mvReg[iRegDst] / 100) % 10;
|
||||||
|
mvMemory[mvSpecialReg[SR_I] + 1] = (mvReg[iRegDst] / 10) % 10;
|
||||||
|
mvMemory[mvSpecialReg[SR_I] + 2] = mvReg[iRegDst] % 10;
|
||||||
|
break;
|
||||||
|
case 0xF055: // FX55 - dump registers
|
||||||
|
for(int i = 0; i <= iRegDst - R_V0; i++) {
|
||||||
|
mvMemory[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[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;
|
||||||
@@ -172,27 +212,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) {
|
||||||
|
|||||||
49
main.cpp
49
main.cpp
@@ -1,12 +1,18 @@
|
|||||||
#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 "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;
|
||||||
@@ -21,33 +27,26 @@ int main(int argc, char* args[]) {
|
|||||||
TestKeypad keypad;
|
TestKeypad keypad;
|
||||||
chocochip8::Interpreter chip8(90, display, buzzer, keypad);
|
chocochip8::Interpreter chip8(90, display, buzzer, keypad);
|
||||||
|
|
||||||
uint8_t prog[] = {
|
auto rom = std::vector<char>();
|
||||||
0xA0, 0x00, // LD I,0
|
auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary);
|
||||||
0x60, 0x00, // LD $0,0
|
std::copy(
|
||||||
0x61, 0x00, // LD $1,0
|
std::istreambuf_iterator<char>(romfile),
|
||||||
0xD0, 0x15, // DRW $0, $1, 5
|
std::istreambuf_iterator<char>(),
|
||||||
0xA0, 0x05, // LD I,5
|
std::back_insert_iterator(rom)
|
||||||
0x60, 0x3C, // LD $0,60
|
);
|
||||||
0x61, 0x00, // LD $1,0
|
chip8.loadProgram(rom.data(), rom.size());
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Event event;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chip8.tick();
|
||||||
display.updateWindow();
|
display.updateWindow();
|
||||||
|
SDL_Delay(1000.0 / 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
buzzer.off();
|
buzzer.off();
|
||||||
|
|||||||
Reference in New Issue
Block a user