Fixed the buffer overflow by moving the screen space modulus to where the sprite is drawn.

master
Zed A. Shaw 1 month ago
parent 1c5f5de100
commit 948bef0bf9
  1. 8
      Makefile
  2. 11
      src/chip8.cpp

@ -1,7 +1,7 @@
all: build all: build
reset: reset:
powershell -executionpolicy bypass .\scripts\reset_build.ps1 powershell cutionpolicy bypass .\scripts\reset_build.ps1
build: build:
meson compile -j 4 -C builddir meson compile -j 4 -C builddir
@ -15,13 +15,13 @@ debug_build:
meson compile -j 4 -C builddir meson compile -j 4 -C builddir
run: build run: build
./builddir/b8rk.exe 1 120 ./roms/pong.ch8 ./builddir/b8rk 1 120 ./roms/pong.ch8
debug: build debug: build
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe gdb --nx -x .gdbinit --ex run --args builddir/runtests
debug_run: build debug_run: build
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/b8rk.exe 1 120 ./roms/pong.ch8 gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/b8rk 1 120 ./roms/pong.ch8
clean: clean:
meson compile --clean -C builddir meson compile --clean -C builddir

@ -283,9 +283,8 @@ void Chip8::OP_Dxyn() {
uint8_t Vy = (opcode & 0x00F0u) >> 4u; uint8_t Vy = (opcode & 0x00F0u) >> 4u;
uint8_t height = opcode & 0x00Fu; uint8_t height = opcode & 0x00Fu;
// wrap if going beyond the screen boundaries uint8_t xPos = registers[Vx];
uint8_t xPos = registers[Vx] % CHIP8_WIDTH; uint8_t yPos = registers[Vy];
uint8_t yPos = registers[Vy] % CHIP8_HEIGHT;
registers[0xF] = 0; registers[0xF] = 0;
@ -295,7 +294,11 @@ void Chip8::OP_Dxyn() {
for(size_t col = 0; col < 8; ++col) { for(size_t col = 0; col < 8; ++col) {
uint8_t spritePixel = spriteByte & (0x80u >> col); uint8_t spritePixel = spriteByte & (0x80u >> col);
size_t pixel_at = (yPos + row) * CHIP8_WIDTH + (xPos + col); // wrap if going beyond the screen boundaries
size_t draw_row = (yPos + row) % CHIP8_HEIGHT;
size_t draw_col = (xPos + col) % CHIP8_WIDTH;
size_t pixel_at = draw_row * CHIP8_WIDTH + draw_col;
uint32_t* screenPixel = &video[pixel_at]; uint32_t* screenPixel = &video[pixel_at];

Loading…
Cancel
Save