git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

commit 88e64fadda5314debe88f67121d4c32ee3ef2cb4
parent c92a2c21314549a90268314991bc31d6790ac9e6
Author: Lior Halphon <LIJI32@gmail.com>
Date:   Fri,  6 Jan 2023 19:06:22 +0200

More accurate and customizable timings

Diffstat:
MCore/gb.c7+++++++
MCore/gb.h13+++++++++----
MCore/memory.c13+++++--------
MCore/timing.c12+++++++++++-
4 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/Core/gb.c b/Core/gb.c @@ -183,6 +183,8 @@ GB_gameboy_t *GB_init(GB_gameboy_t *gb, GB_model_t model) gb->turbo = true; } + gb->data_bus_decay = 12; + GB_reset(gb); load_default_border(gb); return gb; @@ -1991,6 +1993,11 @@ void GB_set_accelerometer_values(GB_gameboy_t *gb, double x, double y) gb->accelerometer_y = y; } +void GB_set_open_bus_decay_time(GB_gameboy_t *gb, uint32_t decay) +{ + gb->data_bus_decay = decay; +} + void GB_get_rom_title(GB_gameboy_t *gb, char *title) { memset(title, 0, 17); diff --git a/Core/gb.h b/Core/gb.h @@ -432,10 +432,11 @@ struct GB_gameboy_internal_s { uint32_t ram_size; // Different between CGB and DMG GB_workboy_t workboy; - int32_t ir_sensor; - bool effective_ir_input; - uint16_t address_bus; - uint8_t data_bus; // cart data bus + int32_t ir_sensor; + bool effective_ir_input; + uint16_t address_bus; + uint8_t data_bus; // cart data bus (MAIN) + uint32_t data_bus_decay_countdown; ) /* DMA and HDMA */ @@ -719,6 +720,7 @@ struct GB_gameboy_internal_s { uint32_t rtc_second_length; uint32_t clock_rate; uint32_t unmultiplied_clock_rate; + uint32_t data_bus_decay; /* Audio */ GB_apu_output_t apu_output; @@ -979,6 +981,9 @@ bool GB_has_accelerometer(GB_gameboy_t *gb); // Values within ±4 recommended void GB_set_accelerometer_values(GB_gameboy_t *gb, double x, double y); +// Time it takes for a value in the data bus to decay to FF, in 8MHz units. (0 to never decay, like e.g. an EverDrive) +void GB_set_open_bus_decay_time(GB_gameboy_t *gb, uint32_t decay); + /* For integration with SFC/SNES emulators */ void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callback); void GB_set_icd_pixel_callback(GB_gameboy_t *gb, GB_icd_pixel_callback_t callback); diff --git a/Core/memory.c b/Core/memory.c @@ -786,16 +786,13 @@ uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr) but should be good enough */ if (bus_for_addr(gb, addr) == GB_BUS_MAIN && addr < 0xFF00) { if (unlikely(gb->returned_open_bus)) { - gb->data_bus = 0xFF; gb->returned_open_bus = false; } else { gb->data_bus = data; + gb->data_bus_decay_countdown = gb->data_bus_decay; } } - else { - gb->data_bus = 0xFF; - } return data; } @@ -1738,9 +1735,7 @@ void GB_write_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } if (bus_for_addr(gb, addr) == GB_BUS_MAIN && addr < 0xFF00) { gb->data_bus = value; - } - else { - gb->data_bus = 0xFF; + gb->data_bus_decay_countdown = gb->data_bus_decay; } if (unlikely(gb->write_memory_callback)) { @@ -1831,10 +1826,13 @@ void GB_dma_run(GB_gameboy_t *gb) void GB_hdma_run(GB_gameboy_t *gb) { unsigned cycles = gb->cgb_double_speed? 4 : 2; + /* TODO: This portion of code is probably inaccurate because it probably depends on my specific GB-Live32 */ + #if 0 /* This is a bit cart, revision and unit specific. TODO: what if PC is in cart RAM? */ if (gb->model < GB_MODEL_CGB_D || gb->pc > 0x8000) { gb->data_bus = 0xFF; } + #endif gb->addr_for_hdma_conflict = 0xFFFF; uint16_t vram_base = gb->cgb_vram_bank? 0x2000 : 0; gb->hdma_in_progress = true; @@ -1877,7 +1875,6 @@ void GB_hdma_run(GB_gameboy_t *gb) } gb->hdma_current_dest++; } - gb->data_bus = 0xFF; if ((gb->hdma_current_dest & 0xF) == 0) { if (--gb->hdma_steps_left == 0 || gb->hdma_current_dest == 0) { diff --git a/Core/timing.c b/Core/timing.c @@ -421,7 +421,7 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) gb->halted = false; } } - + gb->debugger_ticks += cycles; if (gb->speed_switch_freeze) { @@ -450,6 +450,16 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) gb->rumble_on_cycles += gb->rumble_strength & 3; gb->rumble_off_cycles += (gb->rumble_strength & 3) ^ 3; + if (unlikely(gb->data_bus_decay_countdown)) { + if (gb->data_bus_decay_countdown <= cycles) { + gb->data_bus_decay_countdown = 0; + gb->data_bus = 0xFF; + } + else { + gb->data_bus_decay_countdown -= cycles; + } + } + GB_joypad_run(gb, cycles); GB_apu_run(gb, false); GB_display_run(gb, cycles, false);

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.