2 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
9 changed files with 90 additions and 131 deletions

View File

@@ -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 DisplaySDL.cpp BuzzerSDL.cpp CountdownTimerSDL.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)

View File

@@ -1,18 +0,0 @@
#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;
}

View File

@@ -1,17 +0,0 @@
#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,13 +1,8 @@
#include "DisplaySDL.hpp" #include "DisplaySDL.hpp"
#include "Peripherals.hpp"
#include <SDL2/SDL_surface.h>
#include <stdexcept> #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 // Create SDL Window
mpWindow = SDL_CreateWindow( mpWindow = SDL_CreateWindow(
"ChocoChip-8", "ChocoChip-8",
@@ -41,52 +36,30 @@ DisplaySDL::~DisplaySDL() {
} }
void DisplaySDL::clear() { void DisplaySDL::clear() {
for(auto &scanline : *mpFramebuffer) { Display::clear();
scanline.reset(); SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor);
SDL_UpdateWindowSurface(mpWindow);
miTopDirtyScanline = chocochip8::gcHeight - 1;
miBottomDirtyScanline = 0;
}
void DisplaySDL::updateWindow() const {
if(miTopDirtyScanline > miBottomDirtyScanline) {
// No changes since the last update
return;
} }
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_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
if(mDoClear) { for(int y = 0; y < chocochip8::gcHeight; y++) {
for(auto &scanline : *mpDisplayState) { if(y < miTopDirtyScanline || miBottomDirtyScanline < y) {
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 // Skip scanlines that haven't changed since the last update
continue; continue;
} }
for(int x = 0; x < rNewScanline.size(); x++) { for(int x = 0; x < chocochip8::gcWidth; x++) {
bool isSet = rNewScanline._Unchecked_test(x); // Map framebuffer pixel to SDL surface rectangle
bool wasSet = rOldScanline._Unchecked_test(x); int x1 = ( x * pSurface->w) / chocochip8::gcWidth;
if(isSet == wasSet) { int x2 = ((x + 1) * pSurface->w) / chocochip8::gcWidth;
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 y1 = ( y * pSurface->h) / chocochip8::gcHeight;
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight; int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
SDL_Rect rect; SDL_Rect rect;
@@ -95,14 +68,14 @@ void DisplaySDL::updateWindow(bool doWindowUpdate) const {
rect.w = x2 - x1; rect.w = x2 - x1;
rect.h = y2 - y1; rect.h = y2 - y1;
Uint32 color = (isSet ? mFgColor : mBgColor); // Read Chocochip8 Framebuffer and choose appropriate color,
// note that the MSB of an scanline's bitset is the leftmost pixel.
Uint32 color = (mpFramebuffer->at(y)[(chocochip8::gcWidth - 1) - x] ? mFgColor : mBgColor);
SDL_FillRect(pSurface, &rect, color); SDL_FillRect(pSurface, &rect, color);
doWindowUpdate = true;
} }
} }
if(doWindowUpdate) { miTopDirtyScanline = chocochip8::gcHeight - 1;
*mpDisplayState = *mpFramebuffer; miBottomDirtyScanline = 0;
SDL_UpdateWindowSurface(mpWindow); SDL_UpdateWindowSurface(mpWindow);
}
} }

View File

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

View File

@@ -44,14 +44,13 @@ constexpr uint8_t gcvLowResFontData[80] = {
0xF0, 0x80, 0xF0, 0x80, 0x80 0xF0, 0x80, 0xF0, 0x80, 0x80
}; };
Interpreter::Interpreter(Display &display, Buzzer &buzzer, Keypad &keypad, CountdownTimer &delayTimer, CountdownTimer &soundTimer): Interpreter::Interpreter(unsigned ticksPerSecond, Display &display, Buzzer &buzzer, Keypad &keypad):
mvMemory(scMemorySize), mvMemory(scMemorySize),
mCallStack{}, mCallStack{},
mrDisplay{display}, mrDisplay{display},
mrBuzzer{buzzer}, mrBuzzer{buzzer},
mrKeypad{keypad}, mrKeypad{keypad},
mrDelayTimer(delayTimer), mcTicksPerSecond{ticksPerSecond},
mrSoundTimer(soundTimer),
mvSpecialReg{}, mvSpecialReg{},
mvReg{}, mvReg{},
mIsHighResMode{false} { mIsHighResMode{false} {
@@ -144,16 +143,16 @@ void Interpreter::tick() {
case 0xF000: // several unique instructions case 0xF000: // several unique instructions
switch(inst & 0xF0FF) { switch(inst & 0xF0FF) {
case 0xF007: // FX07 - read timer register case 0xF007: // FX07 - read timer register
mvReg[iRegDst] = mrDelayTimer.get(); mvReg[iRegDst] = (mvSpecialReg[SR_T1] * scTimerFreq + (mcTicksPerSecond - 1)) / mcTicksPerSecond;
break; break;
case 0xF015: // FX15 - set timer register case 0xF015: // FX15 - set timer register
mrDelayTimer.set(mvReg[iRegDst]); mvSpecialReg[SR_T1] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
break; break;
case 0xF018: // FX18 - set sound timer register case 0xF018: // FX18 - set sound timer register
if(mvReg[iRegDst] != 0) { if(mvSpecialReg[SR_T2] == 0 && mvReg[iRegDst] != 0) {
mrBuzzer.on(); mrBuzzer.on();
} }
mrSoundTimer.set(mvReg[iRegDst]); mvSpecialReg[SR_T2] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
break; break;
case 0xF01E: // FX1E - add to I case 0xF01E: // FX1E - add to I
mvSpecialReg[SR_I] += mvReg[iRegDst]; mvSpecialReg[SR_I] += mvReg[iRegDst];
@@ -183,9 +182,16 @@ void Interpreter::tick() {
break; break;
} }
if(mrSoundTimer.get() == 0) { // 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(); mrBuzzer.off();
} }
}
} }
void Interpreter::loadProgram(char const* data, size_t count, size_t where) { void Interpreter::loadProgram(char const* data, size_t count, size_t where) {
@@ -284,10 +290,12 @@ 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 // 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++) { for(int j = 0; j < (mIsHighResMode ? 1 : 2); j++) {
// blit the sprite bitset into the screen Scanline &targetScanline = mrDisplay.getModifyableScanline(y + i * (mIsHighResMode ? 1 : 2) + j);
if(mrDisplay.blit(spriteScanline, y + i * (mIsHighResMode ? 1 : 2) + j) != 0) { if((targetScanline & spriteScanline) != 0) {
collisionCount += 1; collisionCount += 1;
} }
// blit the sprite bitset into the screen
targetScanline ^= spriteScanline;
} }
// update state flags // update state flags

View File

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

View File

@@ -1,12 +1,15 @@
#pragma once #pragma once
#include <array>
#include <bitset> #include <bitset>
#include <memory>
namespace chocochip8 { namespace chocochip8 {
constexpr size_t gcWidth = 128; constexpr size_t gcWidth = 128;
constexpr size_t gcHeight = 64; constexpr size_t gcHeight = 64;
using Scanline = std::bitset<gcWidth>; 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,
@@ -17,9 +20,32 @@ namespace chocochip8 {
class Display { class Display {
public: public:
Display():
mpFramebuffer{std::make_unique<Framebuffer>()},
miTopDirtyScanline{0},
miBottomDirtyScanline{gcHeight - 1} {}
virtual ~Display() = default; virtual ~Display() = default;
virtual int blit(const Scanline& spriteScanline, int y) = 0;
virtual void clear() = 0; 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:
std::unique_ptr<Framebuffer> mpFramebuffer;
mutable size_t miTopDirtyScanline;
mutable size_t miBottomDirtyScanline;
}; };
class Buzzer { class Buzzer {
@@ -35,11 +61,4 @@ namespace chocochip8 {
virtual bool isKeyPressed(Key key) = 0; 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 }; // namespace chocochip8

View File

@@ -6,11 +6,11 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "BuzzerSDL.hpp" #include "BuzzerSDL.hpp"
#include "CountdownTimerSDL.hpp"
#include "DisplaySDL.hpp" #include "DisplaySDL.hpp"
#include "Interpreter.hpp" #include "Interpreter.hpp"
#include "Peripherals.hpp" #include "Peripherals.hpp"
#include "SDL2/SDL_events.h" #include "SDL_events.h"
#include "SDL_timer.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
@@ -23,13 +23,9 @@ int main(int argc, char* argv[]) {
}; };
BuzzerSDL buzzer(440); BuzzerSDL buzzer(440);
int N = 4; DisplaySDL display(1280, 640);
DisplaySDL display(128*N, 64*N);
TestKeypad keypad; TestKeypad keypad;
CountdownTimerSDL delayTimer(60); chocochip8::Interpreter chip8(1000, display, buzzer, keypad);
CountdownTimerSDL soundTimer(60);
CountdownTimerSDL displayTimer(20);
chocochip8::Interpreter chip8(display, buzzer, keypad, delayTimer, soundTimer);
auto rom = std::vector<char>(); auto rom = std::vector<char>();
auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary); auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary);
@@ -42,6 +38,8 @@ int main(int argc, char* argv[]) {
SDL_Event event; SDL_Event event;
bool done = false; bool done = false;
Uint64 start, end;
start = SDL_GetTicks64();
while(!done) { while(!done) {
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) { if(event.type == SDL_QUIT) {
@@ -49,13 +47,16 @@ int main(int argc, char* argv[]) {
} }
} }
chip8.tick(); chip8.tick();
if(displayTimer.get() == 0) {
display.updateWindow(); display.updateWindow();
displayTimer.set(1); 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();
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }