Compare commits
4 Commits
8c49ec6d86
...
optimizati
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d3e843653 | |||
| 4f7083d7c7 | |||
| af39b2ab07 | |||
| 3c30d2a04f |
@@ -35,43 +35,47 @@ DisplaySDL::~DisplaySDL() {
|
||||
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 {
|
||||
if(miTopDirtyScanline > miBottomDirtyScanline) {
|
||||
// No changes since the last update
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
int sx, sy; // SDL Surface coordinates
|
||||
int lx, ly; // Last-used ChocoChip8 Framebuffer coordinates
|
||||
Uint32 lc; // Last-used SDL color
|
||||
for(int x = 0; x < chocochip8::gcWidth; x++) {
|
||||
// Map framebuffer pixel to SDL surface rectangle
|
||||
int x1 = ( x * pSurface->w) / chocochip8::gcWidth;
|
||||
int x2 = ((x + 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;
|
||||
|
||||
// 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;
|
||||
// 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_UnlockSurface(pSurface);
|
||||
miTopDirtyScanline = chocochip8::gcHeight - 1;
|
||||
miBottomDirtyScanline = 0;
|
||||
SDL_UpdateWindowSurface(mpWindow);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ class DisplaySDL : public chocochip8::Display {
|
||||
public:
|
||||
DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000);
|
||||
~DisplaySDL() override;
|
||||
void clear() override;
|
||||
void updateWindow() const;
|
||||
|
||||
private:
|
||||
|
||||
@@ -71,7 +71,7 @@ void Interpreter::tick() {
|
||||
|
||||
// fetch instruction
|
||||
sreg_t iInstAddr = mvSpecialReg[SR_PC];
|
||||
unsigned inst = (mvMemory[iInstAddr] << 8) | mvMemory[iInstAddr + 1];
|
||||
unsigned inst = (mvMemory.at(iInstAddr) << 8) | mvMemory.at(iInstAddr + 1);
|
||||
|
||||
// increment program counter
|
||||
mvSpecialReg[SR_PC] += 2;
|
||||
@@ -87,9 +87,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 +143,16 @@ void Interpreter::tick() {
|
||||
case 0xF000: // several unique instructions
|
||||
switch(inst & 0xF0FF) {
|
||||
case 0xF007: // FX07 - read timer register
|
||||
mvReg[iRegDst] = (mvSpecialReg[SR_T1] + mcTicksPerSecond - 1) / mcTicksPerSecond;
|
||||
mvReg[iRegDst] = (mvSpecialReg[SR_T1] * scTimerFreq + (mcTicksPerSecond - 1)) / mcTicksPerSecond;
|
||||
break;
|
||||
case 0xF015: // FX15 - set timer register
|
||||
mvSpecialReg[SR_T1] = mcTicksPerSecond * mvReg[iRegDst];
|
||||
mvSpecialReg[SR_T1] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
|
||||
break;
|
||||
case 0xF018: // FX18 - set sound timer register
|
||||
if(mvSpecialReg[SR_T1] == 0 && mvReg[iRegDst] != 0) {
|
||||
if(mvSpecialReg[SR_T2] == 0 && mvReg[iRegDst] != 0) {
|
||||
mrBuzzer.on();
|
||||
}
|
||||
mvSpecialReg[SR_T1] = mcTicksPerSecond * mvReg[iRegDst];
|
||||
mvSpecialReg[SR_T2] = (mcTicksPerSecond * mvReg[iRegDst]) / scTimerFreq;
|
||||
break;
|
||||
case 0xF01E: // FX1E - add to I
|
||||
mvSpecialReg[SR_I] += mvReg[iRegDst];
|
||||
@@ -163,18 +161,18 @@ void Interpreter::tick() {
|
||||
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;
|
||||
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[mvSpecialReg[SR_I]++] = mvReg[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[mvSpecialReg[SR_I]++];
|
||||
mvReg[R_V0 + i] = mvMemory.at(mvSpecialReg[SR_I]++);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -272,14 +270,14 @@ void Interpreter::executeDraw(uint8_t x, uint8_t y, uint8_t n) {
|
||||
uint16_t spriteRowBits;
|
||||
if(mIsHighResMode) {
|
||||
// draws an 8xN sprite
|
||||
spriteRowBits = mvMemory[iMemAddr++] << 8;
|
||||
spriteRowBits = mvMemory.at(iMemAddr++) << 8;
|
||||
if(n == 0) {
|
||||
// draws an 16xN sprite, so fetch another byte from sprite data
|
||||
spriteRowBits |= mvMemory[iMemAddr++];
|
||||
spriteRowBits |= mvMemory.at(iMemAddr++);
|
||||
}
|
||||
} else {
|
||||
// in low-res mode, each sprite pixel draws two on-screen pixels
|
||||
spriteRowBits = gcvLowResToHighResRowLookupTable[mvMemory[iMemAddr++]];
|
||||
spriteRowBits = gcvLowResToHighResRowLookupTable[mvMemory.at(iMemAddr++)];
|
||||
}
|
||||
|
||||
// convert to bitset and shift into absolute horizontal position on the screen
|
||||
@@ -292,7 +290,7 @@ 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);
|
||||
Scanline &targetScanline = mrDisplay.getModifyableScanline(y + i * (mIsHighResMode ? 1 : 2) + j);
|
||||
if((targetScanline & spriteScanline) != 0) {
|
||||
collisionCount += 1;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ 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 {
|
||||
|
||||
@@ -20,11 +20,32 @@ namespace chocochip8 {
|
||||
|
||||
class Display {
|
||||
public:
|
||||
friend class Interpreter;
|
||||
Display(): mpFramebuffer{std::make_unique<Framebuffer>()} {}
|
||||
Display():
|
||||
mpFramebuffer{std::make_unique<Framebuffer>()},
|
||||
miTopDirtyScanline{0},
|
||||
miBottomDirtyScanline{gcHeight - 1} {}
|
||||
|
||||
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:
|
||||
std::unique_ptr<Framebuffer> mpFramebuffer;
|
||||
mutable size_t miTopDirtyScanline;
|
||||
mutable size_t miBottomDirtyScanline;
|
||||
};
|
||||
|
||||
class Buzzer {
|
||||
|
||||
10
main.cpp
10
main.cpp
@@ -25,7 +25,7 @@ int main(int argc, char* argv[]) {
|
||||
BuzzerSDL buzzer(440);
|
||||
DisplaySDL display(1280, 640);
|
||||
TestKeypad keypad;
|
||||
chocochip8::Interpreter chip8(90, display, buzzer, 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);
|
||||
@@ -38,6 +38,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
SDL_Event event;
|
||||
bool done = false;
|
||||
Uint64 start, end;
|
||||
start = SDL_GetTicks64();
|
||||
while(!done) {
|
||||
while(SDL_PollEvent(&event)) {
|
||||
if(event.type == SDL_QUIT) {
|
||||
@@ -46,7 +48,11 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
chip8.tick();
|
||||
display.updateWindow();
|
||||
SDL_Delay(1000.0 / 60);
|
||||
SDL_Delay(1);
|
||||
end = SDL_GetTicks64();
|
||||
if(end - start > 3)
|
||||
std::cout << "Frame took too long: " << end - start << "\n";
|
||||
start = end;
|
||||
}
|
||||
|
||||
buzzer.off();
|
||||
|
||||
Reference in New Issue
Block a user