Compare commits

..

3 Commits

9 changed files with 145 additions and 74 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)
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 {
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);
SDL_LockSurface(pSurface);
if(mDoClear) {
for(auto &scanline : *mpDisplayState) {
scanline.reset();
}
SDL_FillRect(pSurface, NULL, mBgColor);
mDoClear = false;
doWindowUpdate = true;
}
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
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;
}
// 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);
for(int x = 0; x < rNewScanline.size(); x++) {
bool isSet = rNewScanline._Unchecked_test(x);
bool wasSet = rOldScanline._Unchecked_test(x);
if(isSet == wasSet) {
continue;
}
// 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;
// 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;
}
}
SDL_UnlockSurface(pSurface);
SDL_UpdateWindowSurface(mpWindow);
if(doWindowUpdate) {
*mpDisplayState = *mpFramebuffer;
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

@@ -44,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} {
@@ -87,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();
@@ -145,16 +144,16 @@ void Interpreter::tick() {
case 0xF000: // several unique instructions
switch(inst & 0xF0FF) {
case 0xF007: // FX07 - read timer register
mvReg[iRegDst] = (mvSpecialReg[SR_T1] * scTimerFreq + (mcTicksPerSecond - 1)) / mcTicksPerSecond;
mvReg[iRegDst] = mrDelayTimer.get();
break;
case 0xF015: // FX15 - set timer register
mvSpecialReg[SR_T1] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
mrDelayTimer.set(mvReg[iRegDst]);
break;
case 0xF018: // FX18 - set sound timer register
if(mvSpecialReg[SR_T2] == 0 && mvReg[iRegDst] != 0) {
if(mvReg[iRegDst] != 0) {
mrBuzzer.on();
}
mvSpecialReg[SR_T2] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
mrSoundTimer.set(mvReg[iRegDst]);
break;
case 0xF01E: // FX1E - add to I
mvSpecialReg[SR_I] += mvReg[iRegDst];
@@ -184,15 +183,8 @@ void Interpreter::tick() {
break;
}
// 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) {
mrBuzzer.off();
}
if(mrSoundTimer.get() == 0) {
mrBuzzer.off();
}
}
@@ -292,12 +284,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

@@ -16,7 +16,6 @@ private:
public:
constexpr static sreg_t scLowRestFontAddr = 0x0000;
constexpr static sreg_t scResetVector = 0x0200;
constexpr static sreg_t scTimerFreq = 60;
constexpr static size_t scMemorySize = 4096;
enum {
@@ -26,7 +25,7 @@ public:
};
enum {
SR_PC, SR_I, SR_T1, SR_T2,
SR_PC, SR_I, /*SR_T1, SR_T2,*/
SR_COUNT
};
@@ -35,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);
@@ -49,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;

View File

@@ -1,15 +1,12 @@
#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 {
KEY_0, KEY_1, KEY_2, KEY_3,
@@ -20,11 +17,9 @@ namespace chocochip8 {
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 {
@@ -40,4 +35,11 @@ namespace chocochip8 {
virtual bool isKeyPressed(Key key) = 0;
};
class CountdownTimer {
public:
virtual ~CountdownTimer() = default;
virtual void set(unsigned value) = 0;
virtual unsigned get() const = 0;
};
}; // namespace chocochip8

View File

@@ -6,11 +6,11 @@
#include <SDL2/SDL.h>
#include "BuzzerSDL.hpp"
#include "CountdownTimerSDL.hpp"
#include "DisplaySDL.hpp"
#include "Interpreter.hpp"
#include "Peripherals.hpp"
#include "SDL_events.h"
#include "SDL_timer.h"
#include "SDL2/SDL_events.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
@@ -23,9 +23,13 @@ int main(int argc, char* argv[]) {
};
BuzzerSDL buzzer(440);
DisplaySDL display(1280, 640);
int N = 4;
DisplaySDL display(128*N, 64*N);
TestKeypad keypad;
chocochip8::Interpreter chip8(90, display, buzzer, keypad);
CountdownTimerSDL delayTimer(60);
CountdownTimerSDL soundTimer(60);
CountdownTimerSDL displayTimer(20);
chocochip8::Interpreter chip8(display, buzzer, keypad, delayTimer, soundTimer);
auto rom = std::vector<char>();
auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary);
@@ -45,12 +49,13 @@ int main(int argc, char* argv[]) {
}
}
chip8.tick();
display.updateWindow();
SDL_Delay(1000.0 / 60);
if(displayTimer.get() == 0) {
display.updateWindow();
displayTimer.set(1);
}
}
buzzer.off();
SDL_Quit();
return 0;
}