Skip to content

Commit

Permalink
Add a setting to enable/disable the prefetch buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arignir committed Oct 12, 2024
1 parent 596b3b4 commit bdfd0c1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions accuracy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def main():
"skip_bios": true,
"speed": 1,
"fast_forward": true,
"prefetch_buffer": true,
"backup_storage": {{
"autodetect": true,
"type": 0
Expand Down
3 changes: 3 additions & 0 deletions include/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions include/gba/gba.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions source/app/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -306,6 +310,7 @@ app_config_save(
"show_fps": %B,
"speed": %g,
"alt_speed": %g,
"prefetch_buffer": %B,
"backup_storage": {
"autodetect": %B,
"type": %d
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions source/app/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
5 changes: 4 additions & 1 deletion source/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions source/app/windows/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions source/gba/gba.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
11 changes: 10 additions & 1 deletion source/gba/memory/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit bdfd0c1

Please sign in to comment.