Compare commits
2 Commits
7069e0ae49
...
5429c5fac1
| Author | SHA1 | Date | |
|---|---|---|---|
| 5429c5fac1 | |||
| e4636e4901 |
@@ -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 Interpreter.cpp BuzzerSDL.cpp)
|
add_executable(chocochip8 main.cpp Interpreter.cpp DisplaySDL.cpp BuzzerSDL.cpp)
|
||||||
target_link_libraries(chocochip8 SDL2::SDL2-static)
|
target_link_libraries(chocochip8 SDL2::SDL2-static)
|
||||||
|
|||||||
77
DisplaySDL.cpp
Normal file
77
DisplaySDL.cpp
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
#include "DisplaySDL.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor) {
|
||||||
|
// Create SDL Window
|
||||||
|
mpWindow = SDL_CreateWindow(
|
||||||
|
"ChocoChip-8",
|
||||||
|
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
w, h,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
if(mpWindow == NULL) {
|
||||||
|
throw std::runtime_error(SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert our foreground/background colors to the format expected by SDL
|
||||||
|
SDL_PixelFormat *fmt = SDL_AllocFormat(SDL_GetWindowPixelFormat(mpWindow));
|
||||||
|
mFgColor = SDL_MapRGB(
|
||||||
|
fmt,
|
||||||
|
(fgColor & 0x00FF0000) >> 16,
|
||||||
|
(fgColor & 0x0000FF00) >> 8,
|
||||||
|
(fgColor & 0x000000FF)
|
||||||
|
);
|
||||||
|
mBgColor = SDL_MapRGB(
|
||||||
|
fmt,
|
||||||
|
(bgColor & 0x00FF0000) >> 16,
|
||||||
|
(bgColor & 0x0000FF00) >> 8,
|
||||||
|
(bgColor & 0x000000FF)
|
||||||
|
);
|
||||||
|
SDL_FreeFormat(fmt);
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
18
DisplaySDL.hpp
Normal file
18
DisplaySDL.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Peripherals.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
|
||||||
|
class DisplaySDL : public chocochip8::Display {
|
||||||
|
public:
|
||||||
|
DisplaySDL(int w, int h, uint32_t fgCol, uint32_t bgCol);
|
||||||
|
~DisplaySDL() override;
|
||||||
|
void updateWindow() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_Window *mpWindow;
|
||||||
|
Uint32 mFgColor;
|
||||||
|
Uint32 mBgColor;
|
||||||
|
};
|
||||||
@@ -15,6 +15,7 @@ Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzz
|
|||||||
mvReg{} {}
|
mvReg{} {}
|
||||||
|
|
||||||
void Interpreter::tick() {
|
void Interpreter::tick() {
|
||||||
|
// decodes all possible machine code arithmetic opcodes
|
||||||
constexpr Opcode opcodeMap[16] = {
|
constexpr Opcode opcodeMap[16] = {
|
||||||
Opcode::SET, Opcode::OR, Opcode::AND, Opcode::XOR,
|
Opcode::SET, Opcode::OR, Opcode::AND, Opcode::XOR,
|
||||||
Opcode::ADD, Opcode::SUB, Opcode::RSH, Opcode::SUB2,
|
Opcode::ADD, Opcode::SUB, Opcode::RSH, Opcode::SUB2,
|
||||||
@@ -22,66 +23,70 @@ void Interpreter::tick() {
|
|||||||
Opcode::UNIMPL, Opcode::UNIMPL, Opcode::LSH, Opcode::UNIMPL,
|
Opcode::UNIMPL, Opcode::UNIMPL, Opcode::LSH, Opcode::UNIMPL,
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned pc = mvSpecialReg[SR_PC];
|
// fetch instruction
|
||||||
|
sreg_t pc = mvSpecialReg[SR_PC];
|
||||||
unsigned inst = (mvMemory[pc] << 8) | mvMemory[pc + 1];
|
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;
|
// extract fields
|
||||||
|
unsigned regDst = (inst & 0x0F00) >> 8; // destination register index
|
||||||
|
unsigned regSrc = (inst & 0x00F0) >> 4; // source register index
|
||||||
|
unsigned opcode = (inst & 0x000F); // arithmetic opcode
|
||||||
|
unsigned imm8 = (inst & 0x00FF); // 8-bit immediate
|
||||||
|
unsigned imm12 = (inst & 0x0FFF); // 12-bit immediate
|
||||||
|
|
||||||
switch(inst & 0xF000) {
|
switch(inst & 0xF000) {
|
||||||
case 0x0000: // 0NNN - call machine language routine
|
case 0x0000: // 0NNN - call machine language routine
|
||||||
if(inst == 0x00E0) {
|
switch(inst) {
|
||||||
// clear display
|
case 0x00E0: // clear display
|
||||||
for(auto &scanline : *mrDisplay.mpFramebuffer) {
|
for(auto &scanline : *mrDisplay.mpFramebuffer) {
|
||||||
scanline.reset();
|
scanline.reset();
|
||||||
}
|
}
|
||||||
} else if(inst == 0x00EE) {
|
break;
|
||||||
// return from subroutine
|
case 0x00EE: // return from subroutine
|
||||||
mvSpecialReg[SR_PC] = mCallStack.top();
|
mvSpecialReg[SR_PC] = mCallStack.top();
|
||||||
mCallStack.pop();
|
mCallStack.pop();
|
||||||
} else {
|
break;
|
||||||
|
default:
|
||||||
throw std::invalid_argument("not implemented");
|
throw std::invalid_argument("not implemented");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 0x1000: // 1NNN - unconditional jump
|
case 0x1000: // 1NNN - unconditional jump
|
||||||
mvSpecialReg[SR_PC] = imm3;
|
mvSpecialReg[SR_PC] = imm12;
|
||||||
break;
|
break;
|
||||||
case 0x2000: // 2NNN - call subroutine
|
case 0x2000: // 2NNN - call subroutine
|
||||||
mCallStack.push(pc);
|
mCallStack.push(pc);
|
||||||
mvSpecialReg[SR_PC] = imm3;
|
mvSpecialReg[SR_PC] = imm12;
|
||||||
break;
|
break;
|
||||||
case 0x3000: // 3XNN - skip if equal immediate
|
case 0x3000: // 3XNN - skip if equal immediate
|
||||||
executeArithmetic(Opcode::JEQ, reg1, imm2);
|
executeArithmetic(Opcode::JEQ, regDst, imm8);
|
||||||
break;
|
break;
|
||||||
case 0x4000: // 4XNN - skip if nonequal immediate
|
case 0x4000: // 4XNN - skip if nonequal immediate
|
||||||
executeArithmetic(Opcode::JNEQ, reg1, imm2);
|
executeArithmetic(Opcode::JNEQ, regDst, imm8);
|
||||||
break;
|
break;
|
||||||
case 0x5000: // 5XY0 - skip if equal
|
case 0x5000: // 5XY0 - skip if equal
|
||||||
executeArithmetic(Opcode::JEQ, reg1, mvReg[reg2]);
|
executeArithmetic(Opcode::JEQ, regDst, mvReg[regSrc]);
|
||||||
break;
|
break;
|
||||||
case 0x6000: // 6XNN - load immediate
|
case 0x6000: // 6XNN - load immediate
|
||||||
executeArithmetic(Opcode::SET, reg1, imm2);
|
executeArithmetic(Opcode::SET, regDst, imm8);
|
||||||
break;
|
break;
|
||||||
case 0x7000: // 7XNN - increment
|
case 0x7000: // 7XNN - increment
|
||||||
executeArithmetic(Opcode::ADD, reg1, imm2);
|
executeArithmetic(Opcode::ADD, regDst, imm8);
|
||||||
break;
|
break;
|
||||||
case 0x8000: // 8XNN - general arithmetic
|
case 0x8000: // 8XNN - general arithmetic
|
||||||
executeArithmetic(opcodeMap[imm1], reg1, mvReg[reg2]);
|
executeArithmetic(opcodeMap[opcode], regDst, mvReg[regSrc]);
|
||||||
break;
|
break;
|
||||||
case 0x9000: // 9XY0 - skip if nonequal
|
case 0x9000: // 9XY0 - skip if nonequal
|
||||||
executeArithmetic(Opcode::JNEQ, reg1, mvReg[reg2]);
|
executeArithmetic(Opcode::JNEQ, regDst, mvReg[regSrc]);
|
||||||
break;
|
break;
|
||||||
case 0xA000: // ANNN - load I
|
case 0xA000: // ANNN - load I
|
||||||
mvSpecialReg[SR_I] = imm3;
|
mvSpecialReg[SR_I] = imm12;
|
||||||
break;
|
break;
|
||||||
case 0xB000: // BNNN - jump indirect
|
case 0xB000: // BNNN - jump indirect
|
||||||
mvSpecialReg[SR_I] = mvReg[R_V0] + imm3;
|
mvSpecialReg[SR_I] = mvReg[R_V0] + imm12;
|
||||||
break;
|
break;
|
||||||
case 0xC000: // CXNN - load random
|
case 0xC000: // CXNN - load random
|
||||||
executeArithmetic(Opcode::RAND, reg1, imm2);
|
executeArithmetic(Opcode::RAND, regDst, imm8);
|
||||||
break;
|
break;
|
||||||
case 0xD000: // DXYN - draw
|
case 0xD000: // DXYN - draw
|
||||||
break;
|
break;
|
||||||
@@ -91,10 +96,13 @@ void Interpreter::tick() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// increment PC
|
||||||
|
mvSpecialReg[SR_PC] += 2;
|
||||||
|
|
||||||
|
// decrement timers
|
||||||
if(mvSpecialReg[SR_T1] > 0) {
|
if(mvSpecialReg[SR_T1] > 0) {
|
||||||
mvSpecialReg[SR_T1] -= 1;
|
mvSpecialReg[SR_T1] -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mvSpecialReg[SR_T2] > 0) {
|
if(mvSpecialReg[SR_T2] > 0) {
|
||||||
mvSpecialReg[SR_T2] -= 1;
|
mvSpecialReg[SR_T2] -= 1;
|
||||||
if(mvSpecialReg[SR_T2] == 0) {
|
if(mvSpecialReg[SR_T2] == 0) {
|
||||||
@@ -103,15 +111,15 @@ void Interpreter::tick() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Interpreter::loadProgram(uint16_t where, char const* data, size_t count) {
|
void Interpreter::loadProgram(size_t where, char const* data, size_t count) {
|
||||||
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(), data, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Interpreter::executeArithmetic(Opcode opcode, int iReg, uint8_t operand) {
|
void Interpreter::executeArithmetic(Opcode opcode, int iReg, reg_t operand) {
|
||||||
uint8_t tmp;
|
reg_t tmp;
|
||||||
switch(opcode) {
|
switch(opcode) {
|
||||||
case Opcode::SET: mvReg[iReg] = operand; break;
|
case Opcode::SET: mvReg[iReg] = operand; break;
|
||||||
case Opcode::AND: mvReg[iReg] &= operand; break;
|
case Opcode::AND: mvReg[iReg] &= operand; break;
|
||||||
|
|||||||
@@ -9,11 +9,14 @@
|
|||||||
namespace chocochip8 {
|
namespace chocochip8 {
|
||||||
|
|
||||||
class Interpreter {
|
class Interpreter {
|
||||||
|
private:
|
||||||
|
using reg_t = uint8_t;
|
||||||
|
using sreg_t = unsigned;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
constexpr static uint16_t scResetVector = 0x0200;
|
constexpr static sreg_t scResetVector = 0x0200;
|
||||||
constexpr static size_t scMemorySize = 4096;
|
constexpr static size_t scMemorySize = 4096;
|
||||||
|
|
||||||
private:
|
|
||||||
enum {
|
enum {
|
||||||
R_V0, R_V1, R_V2, R_V3, R_V4, R_V5, R_V6, R_V7,
|
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_V8, R_V9, R_VA, R_VB, R_VC, R_VD, R_VE, R_VF,
|
||||||
@@ -32,20 +35,20 @@ private:
|
|||||||
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(uint16_t where, char const* data, size_t count);
|
void loadProgram(size_t where, char const* data, size_t count);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void executeArithmetic(Opcode opcode, int iReg, uint8_t operand);
|
void executeArithmetic(Opcode opcode, int iReg, reg_t operand);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<uint8_t> mvMemory;
|
std::vector<uint8_t> mvMemory;
|
||||||
std::stack<uint16_t> mCallStack;
|
std::stack<sreg_t> mCallStack;
|
||||||
Display &mrDisplay;
|
Display &mrDisplay;
|
||||||
Buzzer &mrBuzzer;
|
Buzzer &mrBuzzer;
|
||||||
Keypad &mrKeypad;
|
Keypad &mrKeypad;
|
||||||
const unsigned mcTicksPerSecond;
|
const unsigned mcTicksPerSecond;
|
||||||
unsigned mvSpecialReg[SR_COUNT];
|
sreg_t mvSpecialReg[SR_COUNT];
|
||||||
uint8_t mvReg[R_COUNT];
|
reg_t mvReg[R_COUNT];
|
||||||
}; // class Interpreter
|
}; // class Interpreter
|
||||||
|
|
||||||
}; // namespace chocohip8
|
}; // namespace chocohip8
|
||||||
|
|||||||
@@ -6,8 +6,10 @@
|
|||||||
|
|
||||||
namespace chocochip8 {
|
namespace chocochip8 {
|
||||||
|
|
||||||
using Scanline = std::bitset<128>;
|
constexpr size_t gcWidth = 128;
|
||||||
using Framebuffer = std::array<Scanline, 64>;
|
constexpr size_t gcHeight = 64;
|
||||||
|
using Scanline = std::bitset<gcWidth>;
|
||||||
|
using Framebuffer = std::array<Scanline, gcHeight>;
|
||||||
|
|
||||||
enum class Key {
|
enum class Key {
|
||||||
KEY_0, KEY_1, KEY_2, KEY_3,
|
KEY_0, KEY_1, KEY_2, KEY_3,
|
||||||
|
|||||||
15
main.cpp
15
main.cpp
@@ -2,6 +2,8 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
#include "BuzzerSDL.hpp"
|
#include "BuzzerSDL.hpp"
|
||||||
|
#include "DisplaySDL.hpp"
|
||||||
|
#include "Peripherals.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,11 +11,22 @@ int main(int argc, char* args[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DisplayTest : public DisplaySDL {
|
||||||
|
public:
|
||||||
|
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();
|
//buzzer.on();
|
||||||
|
DisplayTest display;
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while(SDL_WaitEvent(&event) && event.type != SDL_QUIT) {
|
while(SDL_WaitEvent(&event) && event.type != SDL_QUIT) {
|
||||||
|
display.updateWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
buzzer.off();
|
buzzer.off();
|
||||||
|
|||||||
Reference in New Issue
Block a user