From bdfd0c16c8eea6a7467648b2683daec45ee1aadb Mon Sep 17 00:00:00 2001 From: Arignir Date: Sat, 12 Oct 2024 03:29:20 +0200 Subject: [PATCH] Add a setting to enable/disable the prefetch buffer. --- accuracy/check.py | 1 + include/app/app.h | 3 +++ include/gba/gba.h | 3 +++ source/app/config.c | 6 ++++++ source/app/emulator.c | 2 ++ source/app/main.c | 5 ++++- source/app/windows/settings.c | 8 ++++++++ source/gba/gba.c | 5 +++++ source/gba/memory/io.c | 11 ++++++++++- 9 files changed, 42 insertions(+), 2 deletions(-) diff --git a/accuracy/check.py b/accuracy/check.py index 6000cce..f33e29b 100755 --- a/accuracy/check.py +++ b/accuracy/check.py @@ -132,6 +132,7 @@ def main(): "skip_bios": true, "speed": 1, "fast_forward": true, + "prefetch_buffer": true, "backup_storage": {{ "autodetect": true, "type": 0 diff --git a/include/app/app.h b/include/app/app.h index cc7d1b8..2936208 100644 --- a/include/app/app.h +++ b/include/app/app.h @@ -190,6 +190,9 @@ struct settings { // etc. float alt_speed; + // Enable the emulation of the prefetch buffer + bool prefetch_buffer; + // Backup storage struct { bool autodetect; diff --git a/include/gba/gba.h b/include/gba/gba.h index affd9bb..a8a1dad 100644 --- a/include/gba/gba.h +++ b/include/gba/gba.h @@ -85,6 +85,9 @@ struct gba_settings { // Can't be <= 0.0 unless `fast_forward` is true. float speed; + // Enable the emulation of the prefetch buffer + bool prefetch_buffer; + struct { bool enable_bg_layers[4]; bool enable_oam; diff --git a/source/app/config.c b/source/app/config.c index 4a9f129..991af89 100644 --- a/source/app/config.c +++ b/source/app/config.c @@ -92,6 +92,10 @@ app_config_load( app->settings.emulation.alt_speed = d; } + if (mjson_get_bool(data, data_len, "$.emulation.prefetch_buffer", &b)) { + app->settings.emulation.prefetch_buffer = b; + } + if (mjson_get_bool(data, data_len, "$.emulation.backup_storage.autodetect", &b)) { app->settings.emulation.backup_storage.autodetect = b; } @@ -306,6 +310,7 @@ app_config_save( "show_fps": %B, "speed": %g, "alt_speed": %g, + "prefetch_buffer": %B, "backup_storage": { "autodetect": %B, "type": %d @@ -356,6 +361,7 @@ app_config_save( (int)app->settings.emulation.show_fps, app->settings.emulation.speed, app->settings.emulation.alt_speed, + (int)app->settings.emulation.prefetch_buffer, (int)app->settings.emulation.backup_storage.autodetect, (int)app->settings.emulation.backup_storage.type, (int)app->settings.emulation.gpio_device.autodetect, diff --git a/source/app/emulator.c b/source/app/emulator.c index c16a3a9..d8fc689 100644 --- a/source/app/emulator.c +++ b/source/app/emulator.c @@ -196,6 +196,8 @@ app_emulator_fill_gba_settings( settings->speed = speed; } + settings->prefetch_buffer = app->settings.emulation.prefetch_buffer; + settings->ppu.enable_oam = app->settings.video.enable_oam; memcpy(settings->ppu.enable_bg_layers, app->settings.video.enable_bg_layers, sizeof(settings->ppu.enable_bg_layers)); diff --git a/source/app/main.c b/source/app/main.c index c3973ca..1974d9a 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -52,8 +52,12 @@ app_settings_default( ) { free(settings->emulation.bios_path); + settings->emulation.bios_path = strdup("./bios.bin"); + settings->emulation.skip_bios = false; + settings->emulation.show_fps = false; settings->emulation.speed = 1.0; settings->emulation.alt_speed = -1.0; + settings->emulation.prefetch_buffer = true; settings->emulation.backup_storage.autodetect = true; settings->emulation.backup_storage.type = BACKUP_NONE; settings->emulation.gpio_device.autodetect = true; @@ -62,7 +66,6 @@ app_settings_default( memset(settings->video.enable_bg_layers, true, sizeof(settings->video.enable_bg_layers)); memset(settings->audio.enable_psg_channels, true, sizeof(settings->audio.enable_psg_channels)); memset(settings->audio.enable_fifo_channels, true, sizeof(settings->audio.enable_fifo_channels)); - settings->emulation.bios_path = strdup("./bios.bin"); settings->video.display_mode = DISPLAY_MODE_WINDOWED; settings->video.display_size = 3; settings->video.aspect_ratio = ASPECT_RATIO_BORDERS; diff --git a/source/app/windows/settings.c b/source/app/windows/settings.c index eaa4fb7..9c1612a 100644 --- a/source/app/windows/settings.c +++ b/source/app/windows/settings.c @@ -343,6 +343,14 @@ app_win_settings_emulation( igTableSetupColumn("##EmulationSettingsMiscLabel", ImGuiTableColumnFlags_WidthFixed, vp->WorkSize.x / 5.f, 0); igTableSetupColumn("##EmulationSettingsMiscValue", ImGuiTableColumnFlags_WidthStretch, 0.f, 0); + // Emulate the Prefetch Buffer + igTableNextRow(ImGuiTableRowFlags_None, 0.f); + igTableNextColumn(); + igTextWrapped("Emulate the Prefetch Buffer"); + + igTableNextColumn(); + igCheckbox("##PrefetchBuffer", &app->settings.emulation.prefetch_buffer); + // Show FPS igTableNextRow(ImGuiTableRowFlags_None, 0.f); igTableNextColumn(); diff --git a/source/gba/gba.c b/source/gba/gba.c index 235fec0..fe1268c 100644 --- a/source/gba/gba.c +++ b/source/gba/gba.c @@ -434,6 +434,11 @@ gba_process_message( memcpy(&gba->settings, &msg_settings->settings, sizeof(struct gba_settings)); sched_update_speed(gba); + + // If necessary, disable the prefetch buffer + if (!gba->settings.prefetch_buffer) { + memset(&gba->memory.pbuffer, 0, sizeof(struct prefetch_buffer)); + } break; }; case MESSAGE_QUICKSAVE: { diff --git a/source/gba/memory/io.c b/source/gba/memory/io.c index 6a4244c..cd3e774 100644 --- a/source/gba/memory/io.c +++ b/source/gba/memory/io.c @@ -791,8 +791,17 @@ mem_io_write8( }; case IO_REG_WAITCNT: case IO_REG_WAITCNT + 1: { + bool old_pbuffer_enabled; + io->waitcnt.bytes[addr - IO_REG_WAITCNT] = val; - gba->memory.pbuffer.enabled = io->waitcnt.gamepak_prefetch; + old_pbuffer_enabled = gba->memory.pbuffer.enabled; + + if (old_pbuffer_enabled ^ io->waitcnt.gamepak_prefetch) { + memset(&gba->memory.pbuffer, 0, sizeof(struct prefetch_buffer)); + } + + gba->memory.pbuffer.enabled = gba->settings.prefetch_buffer && io->waitcnt.gamepak_prefetch; + mem_update_waitstates(gba); break; };