small fixes

This commit is contained in:
2024-02-05 19:53:38 -08:00
parent 5c528eb8d2
commit e8d8c0fb3d
5 changed files with 114 additions and 122 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
cmake-build-debug/ cmake-build-debug/
build/
.idea .idea
roms/ roms/

View File

@@ -47,11 +47,6 @@ void GameBoy::addCycles(uint8_t ticks)
lastOpTicks = ticks; lastOpTicks = ticks;
} }
void test()
{
printf("");
}
void GameBoy::start(std::string bootrom, std::string game) void GameBoy::start(std::string bootrom, std::string game)
{ {
addressSpace.loadBootrom(bootrom); addressSpace.loadBootrom(bootrom);
@@ -61,28 +56,22 @@ void GameBoy::start(std::string bootrom, std::string game)
while(!quit) while(!quit)
{ {
// Event loop: Check and handle SDL events // Event loop: Check and handle SDL events
// if(SDL_PollEvent(&event)) // if(SDL_PollEvent(&event))
// { // {
// if(event.type == SDL_QUIT) // if(event.type == SDL_QUIT)
// { // {
// quit = true; // Set the quit flag when the close button is hit // quit = true; // Set the quit flag when the close button is hit
// } // }
// } // }
opcodeHandler(); opcodeHandler();
interruptHandler(); interruptHandler();
//timing(); //timing();
ppuUpdate(); ppuUpdate();
if(PC == 0x8c && DE.hi == 0)
test();
if(PC > 0xFF && addressSpace.getBootromState()) if(PC > 0xFF && addressSpace.getBootromState())
{ {
addressSpace.unmapBootrom(); addressSpace.unmapBootrom();
} }
if(PC > 0x2FF)
{
test();
}
int cyclesSince = cyclesSinceLastRefresh(); int cyclesSince = cyclesSinceLastRefresh();
if(cyclesSince > FRAME_DURATION) if(cyclesSince > FRAME_DURATION)
{ {

View File

@@ -21,10 +21,10 @@ enum Colour
enum PPUMode enum PPUMode
{ {
mode0, // Horizontal Blank (Mode 0): No access to video RAM, occurs during horizontal blanking period. mode0, // Horizontal Blank (Mode 0): No access to video RAM, occurs during horizontal blanking period.
mode1, // Vertical Blank (Mode 1): No access to video RAM, occurs during vertical blanking period. mode1, // Vertical Blank (Mode 1): No access to video RAM, occurs during vertical blanking period.
mode2, // OAM Search (Mode 2): Access to OAM (Object Attribute Memory) only, sprite evaluation. mode2, // OAM Search (Mode 2): Access to OAM (Object Attribute Memory) only, sprite evaluation.
mode3 // Pixel Transfer (Mode 3): Access to both OAM and video RAM, actual pixel transfer to the screen. mode3 // Pixel Transfer (Mode 3): Access to both OAM and video RAM, actual pixel transfer to the screen.
}; };
union RegisterPair union RegisterPair
@@ -168,7 +168,7 @@ class GameBoy {
Byte* LCDC = &addressSpace[0xFF40]; Byte* LCDC = &addressSpace[0xFF40];
Byte* STAT = &addressSpace[0xFF41]; Byte* STAT = &addressSpace[0xFF41];
Byte* SCY = &addressSpace[0xFF42]; Byte* SCY = &addressSpace[0xFF42];
Byte* SCX = &addressSpace[0xF43]; Byte* SCX = &addressSpace[0xFF43];
Byte* LY = &addressSpace[0xFF44]; Byte* LY = &addressSpace[0xFF44];
Byte* LYC = &addressSpace[0xFF45]; Byte* LYC = &addressSpace[0xFF45];
Byte* DMA = &addressSpace[0xFF46]; Byte* DMA = &addressSpace[0xFF46];
@@ -194,8 +194,8 @@ class GameBoy {
void checkPPUMode(); void checkPPUMode();
void setPPUMode(PPUMode mode); void setPPUMode(PPUMode mode);
int cyclesSinceLastScanline(); uint32_t cyclesSinceLastScanline();
int cyclesSinceLastRefresh(); uint32_t cyclesSinceLastRefresh();
void interruptHandler(); void interruptHandler();
bool testInterruptEnabled(Byte interrupt); bool testInterruptEnabled(Byte interrupt);

View File

@@ -1,17 +1,11 @@
#include <iostream>
#include <filesystem>
#include <string> #include <string>
#include <optional>
#include "gameboy.hpp" #include "gameboy.hpp"
#include "defines.hpp"
namespace fs = std::filesystem;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
auto* gb = new GameBoy(); auto* gb = new GameBoy();
gb->SDL2setup(); gb->SDL2setup();
gb->start("/home/braiden/Code/GBpp/bootrom.bin", "/home/braiden/Code/GBpp/roms/DrMario.gb"); gb->start("../bootrom.bin", "../roms/DrMario.gb");
gb->SDL2destroy(); gb->SDL2destroy();
delete gb; delete gb;
return 0; return 0;

View File

@@ -1,38 +1,31 @@
#include "gameboy.hpp" #include "gameboy.hpp"
#include "defines.hpp" #include "defines.hpp"
#include <algorithm>
#include <cstdint>
void GameBoy::ppuUpdate() {
void GameBoy::ppuUpdate()
{
//test HBlank //test HBlank
checkPPUMode(); checkPPUMode();
if(cyclesToStayInHblank != -1) if (cyclesToStayInHblank != -1) {
{ if (cyclesToStayInHblank < cyclesSinceLastScanline()) {
if (cyclesToStayInHblank < cyclesSinceLastScanline())
{
return; return;
} }
if(cyclesToStayInHblank >= cyclesSinceLastScanline()) if (cyclesToStayInHblank >= cyclesSinceLastScanline()) {
{
lastScanline = cycles; lastScanline = cycles;
cyclesToStayInHblank = -1; cyclesToStayInHblank = -1;
} }
} }
// Check the PPU mode (HBlank, VBlank, OAM Search, or Pixel Transfer) // Check the PPU mode (HBlank, VBlank, OAM Search, or Pixel Transfer)
Byte mode = (*STAT)&0x03; Byte mode = (*STAT) & 0x03;
switch(mode) switch (mode) {
{
case 0: case 0:
if(cyclesSinceLastScanline() > MODE2_DURATION + MODE3_BASE_DURATION) if (cyclesSinceLastScanline() > MODE2_DURATION + MODE3_BASE_DURATION) {
{
drawLine(); drawLine();
cyclesToStayInHblank = SCANLINE_DURATION - cyclesSinceLastScanline(); cyclesToStayInHblank = SCANLINE_DURATION - cyclesSinceLastScanline();
lastScanline = cycles; lastScanline = cycles;
(*LY)++; (*LY)++;
if((*LY) > 153) if ((*LY) > 153)
(*LY) = 0; (*LY) = 0;
} }
currentMode = PPUMode::mode0; currentMode = PPUMode::mode0;
@@ -40,15 +33,15 @@ void GameBoy::ppuUpdate()
//vblank //vblank
case 1: case 1:
if(currentMode != PPUMode::mode1) if (currentMode != PPUMode::mode1) {
{ setPPUMode(PPUMode::mode1);
drawLine(); //drawLine();
*IF |= 0x1;
} }
if(cyclesSinceLastScanline() > SCANLINE_DURATION) if (cyclesSinceLastScanline() > SCANLINE_DURATION) {
{
lastScanline = cycles; lastScanline = cycles;
(*LY)++; (*LY)++;
if((*LY) > 153) if ((*LY) > 153)
(*LY) = 0; (*LY) = 0;
} }
currentMode = PPUMode::mode1; currentMode = PPUMode::mode1;
@@ -60,66 +53,45 @@ void GameBoy::ppuUpdate()
currentMode = PPUMode::mode3; currentMode = PPUMode::mode3;
break; break;
} }
if ((*LY) == (*LYC) || (*STAT) & (1 << 6)) {
if ((*LY) == (*LYC) || (*STAT)&(1 << 6))
{
// Request STAT interrupt if LY matches LYC // Request STAT interrupt if LY matches LYC
//bug on DMG models triggers a STAT interrupt anytime the STAT register is written //bug on DMG models triggers a STAT interrupt anytime the STAT register is written
//Road Rage and Zerd no Denetsu rely on this //Road Rage and Zerd no Denetsu rely on this
(*STAT) |= (1 << 2); (*STAT) |= (1 << 2);
} else {
(*STAT) &= ~(1 << 2);
} }
// Check for STAT interrupts and request if needed (e.g., when entering specific modes) // Check for STAT interrupts and request if needed (e.g., when entering specific modes)
bool hBlankInterruptEnabled = (*STAT)&(1 << 3); bool hBlankInterruptEnabled = (*STAT) & (1 << 3);
bool vBlankInterruptEnabled = (*STAT)&(1 << 4);/* Determine if VBlank interrupt is enabled */ bool vBlankInterruptEnabled = (*STAT) & (1 << 4); /* Determine if VBlank interrupt is enabled */
bool oamInterruptEnabled = (*STAT)&(1 << 5);/* Determine if OAM Search interrupt is enabled */ bool oamInterruptEnabled = (*STAT) & (1 << 5); /* Determine if OAM Search interrupt is enabled */
if (currentMode == PPUMode::mode0 && hBlankInterruptEnabled) if (currentMode == PPUMode::mode0 && hBlankInterruptEnabled ||
{ currentMode == PPUMode::mode1 && vBlankInterruptEnabled ||
// Request HBlank interrupt currentMode == PPUMode::mode2 && oamInterruptEnabled) {
} *IF |= 0x2;
else if (currentMode == PPUMode::mode1 && vBlankInterruptEnabled)
{
// Request VBlank interrupt
}
else if (currentMode == PPUMode::mode2 && oamInterruptEnabled)
{
// Request OAM Search interrupt
} }
} }
int GameBoy::cyclesSinceLastScanline() uint32_t GameBoy::cyclesSinceLastScanline() {
{ uint32_t difference = cycles - lastScanline;
int difference = cycles - lastScanline;
if (difference < 0)
{
// Handle the case when cycles has wrapped around (overflowed)
difference += T_CLOCK_FREQ;
}
return difference; return difference;
} }
int GameBoy::cyclesSinceLastRefresh() uint32_t GameBoy::cyclesSinceLastRefresh() {
{ uint32_t difference = cycles - lastRefresh;
int difference = cycles - lastRefresh;
if (difference < 0)
{
// Handle the case when cycles has wrapped around (overflowed)
difference += T_CLOCK_FREQ;
}
return difference; return difference;
} }
void GameBoy::checkPPUMode() void GameBoy::checkPPUMode() {
{
int oamFetchTime = 0; int oamFetchTime = 0;
if ((*LY) < 144) if ((*LY) < 144) {
{ uint32_t currentDuration = cyclesSinceLastScanline();
int currentDuration = cyclesSinceLastScanline();
// Active Display Period (HBlank, OAM Search, and Pixel Transfer) // Active Display Period (HBlank, OAM Search, and Pixel Transfer)
if(currentDuration < MODE2_DURATION) if (currentDuration < MODE2_DURATION)
setPPUMode(PPUMode::mode2); setPPUMode(PPUMode::mode2);
else if(currentDuration < MODE2_DURATION + MODE3_BASE_DURATION + oamFetchTime) else if (currentDuration < MODE2_DURATION + MODE3_BASE_DURATION + oamFetchTime)
setPPUMode(PPUMode::mode3); setPPUMode(PPUMode::mode3);
else else
setPPUMode(PPUMode::mode0); setPPUMode(PPUMode::mode0);
@@ -129,18 +101,14 @@ void GameBoy::checkPPUMode()
setPPUMode(PPUMode::mode1); setPPUMode(PPUMode::mode1);
} }
void GameBoy::setPPUMode(PPUMode mode) void GameBoy::setPPUMode(PPUMode mode) {
{ switch (mode) {
switch(mode)
{
case PPUMode::mode0: case PPUMode::mode0:
(*STAT) &= ~0x03; (*STAT) &= ~0x03;
break; break;
case PPUMode::mode1: case PPUMode::mode1:
(*STAT) &= ~0x03; (*STAT) &= ~0x03;
(*STAT) |= 0x01; (*STAT) |= 0x01;
//set vblank interrupt flag
(*IF) |= 0x01;
break; break;
case PPUMode::mode2: case PPUMode::mode2:
(*STAT) &= ~0x03; (*STAT) &= ~0x03;
@@ -151,10 +119,11 @@ void GameBoy::setPPUMode(PPUMode mode)
(*STAT) |= 0x03; (*STAT) |= 0x03;
break; break;
} }
//7th bit is unused but always set
(*STAT) |= 0x80;
} }
void GameBoy::drawLine() void GameBoy::drawLine() {
{
uint8_t line = (*LY); uint8_t line = (*LY);
// Calculate the starting index of the current scanline in the framebuffer // Calculate the starting index of the current scanline in the framebuffer
@@ -163,43 +132,82 @@ void GameBoy::drawLine()
// Pointer to the current line's pixel data in the framebuffer // Pointer to the current line's pixel data in the framebuffer
uint32_t* currentLinePixels = framebuffer + lineStartIndex; uint32_t* currentLinePixels = framebuffer + lineStartIndex;
// For example, if you are setting the entire scanline to a color (e.g., white): if (!(*LCDC & 0x1)) {
for (int i = 0; i < RESOLUTION_X; i++) std::fill_n(currentLinePixels, RESOLUTION_X, 0xFFFFFFFF); // Fill line with white if BG display is off.
{ return;
// Assuming white color is represented as 0xFFFFFFFF (ARGB format) }
// if(currentLinePixels[i] == 0xFFFFFFFF)
// currentLinePixels[i] = 0xFF000000; uint16_t backgroundMapAddr = (*LCDC & 0x08) ? 0x9C00 : 0x9800;
// else uint16_t tileDataTableAddr = (*LCDC & 0x10) ? 0x8000 : 0x8800;
currentLinePixels[i] = 0xFFFFFFFF; bool signedIndex = !(*LCDC & 0x10);
for (int pixel = 0; pixel < RESOLUTION_X; pixel++) {
uint8_t xPos = (pixel + (*SCX)) % 256; // 256 pixels in total BG width
uint8_t yPos = (line + (*SCY)) % 256; // 256 pixels in total BG height
uint16_t tileRow = (yPos / 8) * 32;
uint16_t tileCol = xPos / 8;
uint16_t tileIndex = tileRow + tileCol;
uint16_t tileAddr = backgroundMapAddr + tileIndex;
int8_t tileID = signedIndex ? static_cast<int8_t>(addressSpace[tileAddr]) : addressSpace[tileAddr];
uint16_t tileDataAddr;
if (signedIndex) {
tileDataAddr = tileDataTableAddr + (((tileID + 128) % 256) * 16); // For signed, wrap around
}
else {
tileDataAddr = tileDataTableAddr + (tileID * 16);
}
uint8_t lineOffset = yPos % 8;
uint8_t tileRowData1 = addressSpace[tileDataAddr + (lineOffset * 2)];
uint8_t tileRowData2 = addressSpace[tileDataAddr + (lineOffset * 2) + 1];
uint8_t colourBit = 7 - (xPos % 8);
uint8_t colourNum = ((tileRowData2 >> colourBit) & 0x1) << 1 | ((tileRowData1 >> colourBit) & 0x1);
// Apply the BGP register for palette mapping
uint8_t palette = (*BGP >> (colourNum * 2)) & 0x3;
if (xPos == 0)
palette = 0x3;
switch (palette) {
case 0: currentLinePixels[pixel] = 0xFFFFFFFF;
break; // White
case 1: currentLinePixels[pixel] = 0xAAAAAAFF;
break; // Light gray
case 2: currentLinePixels[pixel] = 0x555555FF;
break; // Dark gray
case 3: currentLinePixels[pixel] = 0x000000FF;
break; // Black
default: break; // Default case for safety, should not be reached
}
} }
} }
void GameBoy::SDL2setup() void GameBoy::SDL2setup() {
{
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_CreateWindow("GBpp", screen = SDL_CreateWindow("GBpp",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
RESOLUTION_X, RESOLUTION_Y, RESOLUTION_X, RESOLUTION_Y,
SDL_WINDOW_OPENGL); SDL_WINDOW_OPENGL);
// Create an SDL renderer to draw on the window // Create an SDL renderer to draw on the window
renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED); renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
// Create an SDL texture to hold the framebuffer data // Create an SDL texture to hold the framebuffer data
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, RESOLUTION_X, RESOLUTION_Y); SDL_TEXTUREACCESS_STREAMING, RESOLUTION_X, RESOLUTION_Y);
} }
void GameBoy::SDL2destroy() void GameBoy::SDL2destroy() {
{
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen); SDL_DestroyWindow(screen);
SDL_Quit(); SDL_Quit();
} }
void GameBoy::SDL2present() void GameBoy::SDL2present() {
{
// Update the SDL texture with the framebuffer data // Update the SDL texture with the framebuffer data
SDL_UpdateTexture(texture, NULL, framebuffer, RESOLUTION_X * sizeof(uint32_t)); SDL_UpdateTexture(texture, NULL, framebuffer, RESOLUTION_X * sizeof(uint32_t));
@@ -209,4 +217,4 @@ void GameBoy::SDL2present()
// Present the renderer on the screen // Present the renderer on the screen
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }