From f8244c8119b2d1adef28ce6932d3a1432771b8de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Zumer?= Date: Mon, 15 Apr 2019 16:39:14 -0400 Subject: [PATCH 001/341] Update libretro GBC memory map --- libretro/libretro.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 937c6e8..15ea093 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -414,7 +414,7 @@ static void init_for_current_model(unsigned id) descs[2].start = 0xC000; descs[2].len = 0x1000; - descs[3].ptr = descs[2].ptr + (bank * 0x1000); + descs[3].ptr = descs[2].ptr + 0x1000; /* GB RAM/GBC RAM bank 1 */ descs[3].start = 0xD000; descs[3].len = 0x1000; @@ -440,9 +440,9 @@ static void init_for_current_model(unsigned id) descs[8].start = 0xFE00; descs[8].len = 0x00A0; - descs[9].ptr = descs[2].ptr + 0x1000; + descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ descs[9].start = 0x10000; - descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x7000 : 0; + descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ struct retro_memory_map mmaps; mmaps.descriptors = descs; From b6e92dc8a74c850df3c5af49310e6c018d6c6707 Mon Sep 17 00:00:00 2001 From: funbars <50187994+funbars@users.noreply.github.com> Date: Tue, 7 May 2019 12:36:04 -0500 Subject: [PATCH 002/341] libretro windows compiler (random) --- libretro/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/libretro/Makefile b/libretro/Makefile index 75ddfc6..fffe71c 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -243,6 +243,7 @@ else CC = gcc TARGET := $(TARGET_NAME)_libretro.dll SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined + CFLAGS += -Drandom=rand endif TARGET := $(CORE_DIR)/build/bin/$(TARGET) From 5ce8cf5016ec16c97521d49dc1ad98232ef21288 Mon Sep 17 00:00:00 2001 From: orbea Date: Thu, 9 May 2019 09:01:15 -0700 Subject: [PATCH 003/341] Makefile: Allow setting CC. --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 5870d42..542fee0 100755 --- a/Makefile +++ b/Makefile @@ -44,9 +44,11 @@ endif # Set tools # Use clang if it's available. +ifeq ($(origin CC),default) ifneq (, $(shell which clang)) CC := clang endif +endif ifeq ($(PLATFORM),windows32) # To force use of the Unix version instead of the Windows version From 2bded45397f39772b57b7a8943ebbd15ea4a927d Mon Sep 17 00:00:00 2001 From: orbea Date: Thu, 9 May 2019 09:08:25 -0700 Subject: [PATCH 004/341] Disable pragmas for gcc. --- Core/apu.c | 6 ++++++ Core/display.c | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/Core/apu.c b/Core/apu.c index 1b3dd2b..9a55f26 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -69,7 +69,9 @@ static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsign static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) { GB_sample_t output = {0,0}; +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = GB_N_CHANNELS; i--;) { double multiplier = CH_STEP; if (!is_DAC_enabled(gb, i)) { @@ -126,7 +128,9 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) unsigned mask = gb->io_registers[GB_IO_NR51]; unsigned left_volume = 0; unsigned right_volume = 0; +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = GB_N_CHANNELS; i--;) { if (gb->apu.is_active[i]) { if (mask & 1) { @@ -374,7 +378,9 @@ void GB_apu_run(GB_gameboy_t *gb) } } +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = GB_SQUARE_2 + 1; i--;) { if (gb->apu.is_active[i]) { uint8_t cycles_left = cycles; diff --git a/Core/display.c b/Core/display.c index 4989cca..fdd4a2f 100644 --- a/Core/display.c +++ b/Core/display.c @@ -27,7 +27,9 @@ static GB_fifo_item_t *fifo_pop(GB_fifo_t *fifo) static void fifo_push_bg_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint8_t palette, bool bg_priority, bool flip_x) { if (!flip_x) { +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = 8; i--;) { fifo->fifo[fifo->write_end] = (GB_fifo_item_t) { (lower >> 7) | ((upper >> 7) << 1), @@ -43,7 +45,9 @@ static void fifo_push_bg_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint } } else { +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = 8; i--;) { fifo->fifo[fifo->write_end] = (GB_fifo_item_t) { (lower & 1) | ((upper & 1) << 1), @@ -70,7 +74,9 @@ static void fifo_overlay_object_row(GB_fifo_t *fifo, uint8_t lower, uint8_t uppe uint8_t flip_xor = flip_x? 0: 0x7; +#ifdef __clang__ #pragma unroll +#endif for (unsigned i = 8; i--;) { uint8_t pixel = (lower >> 7) | ((upper >> 7) << 1); GB_fifo_item_t *target = &fifo->fifo[(fifo->read_end + (i ^ flip_xor)) & (GB_FIFO_LENGTH - 1)]; @@ -1117,7 +1123,9 @@ uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_h } for (unsigned y = 0; y < *sprite_height; y++) { +#ifdef __clang__ #pragma unroll +#endif for (unsigned x = 0; x < 8; x++) { uint8_t color = (((gb->vram[vram_address ] >> ((~x)&7)) & 1 ) | ((gb->vram[vram_address + 1] >> ((~x)&7)) & 1) << 1 ); From 06670fc970dd1cfb2695534fc6e4d92ed5a632f2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 10 May 2019 21:51:11 +0300 Subject: [PATCH 005/341] Fix #172. Allow unroll optimizations when compiling with GCC. --- Core/apu.c | 12 +++--------- Core/display.c | 16 ++++------------ Core/gb.h | 9 +++++++++ Makefile | 2 +- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 9a55f26..e437718 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -69,9 +69,7 @@ static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsign static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) { GB_sample_t output = {0,0}; -#ifdef __clang__ - #pragma unroll -#endif + for (unsigned i = GB_N_CHANNELS; i--;) { double multiplier = CH_STEP; if (!is_DAC_enabled(gb, i)) { @@ -128,9 +126,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) unsigned mask = gb->io_registers[GB_IO_NR51]; unsigned left_volume = 0; unsigned right_volume = 0; -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned i = GB_N_CHANNELS; i--;) { if (gb->apu.is_active[i]) { if (mask & 1) { @@ -378,9 +374,7 @@ void GB_apu_run(GB_gameboy_t *gb) } } -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned i = GB_SQUARE_2 + 1; i--;) { if (gb->apu.is_active[i]) { uint8_t cycles_left = cycles; diff --git a/Core/display.c b/Core/display.c index fdd4a2f..22f6e8d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -27,9 +27,7 @@ static GB_fifo_item_t *fifo_pop(GB_fifo_t *fifo) static void fifo_push_bg_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint8_t palette, bool bg_priority, bool flip_x) { if (!flip_x) { -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned i = 8; i--;) { fifo->fifo[fifo->write_end] = (GB_fifo_item_t) { (lower >> 7) | ((upper >> 7) << 1), @@ -45,9 +43,7 @@ static void fifo_push_bg_row(GB_fifo_t *fifo, uint8_t lower, uint8_t upper, uint } } else { -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned i = 8; i--;) { fifo->fifo[fifo->write_end] = (GB_fifo_item_t) { (lower & 1) | ((upper & 1) << 1), @@ -74,9 +70,7 @@ static void fifo_overlay_object_row(GB_fifo_t *fifo, uint8_t lower, uint8_t uppe uint8_t flip_xor = flip_x? 0: 0x7; -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned i = 8; i--;) { uint8_t pixel = (lower >> 7) | ((upper >> 7) << 1); GB_fifo_item_t *target = &fifo->fifo[(fifo->read_end + (i ^ flip_xor)) & (GB_FIFO_LENGTH - 1)]; @@ -1123,9 +1117,7 @@ uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_h } for (unsigned y = 0; y < *sprite_height; y++) { -#ifdef __clang__ - #pragma unroll -#endif + UNROLL for (unsigned x = 0; x < 8; x++) { uint8_t color = (((gb->vram[vram_address ] >> ((~x)&7)) & 1 ) | ((gb->vram[vram_address + 1] >> ((~x)&7)) & 1) << 1 ); diff --git a/Core/gb.h b/Core/gb.h index f793f30..a4b7256 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -29,6 +29,15 @@ #define GB_MODEL_MGB_FAMILY 0x100 #define GB_MODEL_CGB_FAMILY 0x200 #define GB_MODEL_PAL_BIT 0x1000 + +#if __clang__ +#define UNROLL _Pragma("unroll") +#elif __GNUC__ +#define UNROLL _Pragma("GCC unroll 8") +#else +#define UNROLL +#endif + #endif #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ diff --git a/Makefile b/Makefile index 542fee0..39d77e9 100755 --- a/Makefile +++ b/Makefile @@ -204,7 +204,7 @@ $(BIN)/SameBoy.app/Contents/MacOS/SameBoy: $(CORE_OBJECTS) $(COCOA_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) -framework OpenGL -framework AudioUnit -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework IOKit ifeq ($(CONF), release) - strip $@ + #strip $@ endif $(BIN)/SameBoy.app/Contents/Resources/Base.lproj/%.nib: Cocoa/%.xib From 32361589c9ed1f09349d8943674379dcbf25b77b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 10 May 2019 22:05:03 +0300 Subject: [PATCH 006/341] Fix GCC build --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 Makefile diff --git a/Makefile b/Makefile old mode 100755 new mode 100644 index 39d77e9..dec3a91 --- a/Makefile +++ b/Makefile @@ -65,7 +65,7 @@ endif # Set compilation and linkage flags based on target, platform and configuration -CFLAGS += -Werror -Wall -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES +CFLAGS += -Werror -Wall -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES SDL_LDFLAGS := -lSDL2 -lGL ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows From ddc4e7484b4c09718ac8dd24d25a862c82f8f4a4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 10 May 2019 22:29:30 +0300 Subject: [PATCH 007/341] Fix and restore optimization --- Core/apu.c | 5 +++-- Makefile | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index e437718..a0b9aa1 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -70,7 +70,8 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) { GB_sample_t output = {0,0}; - for (unsigned i = GB_N_CHANNELS; i--;) { + UNROLL + for (unsigned i = 0; i < GB_N_CHANNELS; i++) { double multiplier = CH_STEP; if (!is_DAC_enabled(gb, i)) { gb->apu_output.dac_discharge[i] -= ((double) DAC_DECAY_SPEED) / gb->apu_output.sample_rate; @@ -375,7 +376,7 @@ void GB_apu_run(GB_gameboy_t *gb) } UNROLL - for (unsigned i = GB_SQUARE_2 + 1; i--;) { + for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { if (gb->apu.is_active[i]) { uint8_t cycles_left = cycles; while (unlikely(cycles_left > gb->apu.square_channels[i].sample_countdown)) { diff --git a/Makefile b/Makefile index dec3a91..5a9f9b5 100644 --- a/Makefile +++ b/Makefile @@ -204,7 +204,7 @@ $(BIN)/SameBoy.app/Contents/MacOS/SameBoy: $(CORE_OBJECTS) $(COCOA_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) -framework OpenGL -framework AudioUnit -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework IOKit ifeq ($(CONF), release) - #strip $@ + strip $@ endif $(BIN)/SameBoy.app/Contents/Resources/Base.lproj/%.nib: Cocoa/%.xib From 4a5498544148369f1f0d47c8131f23c9e87b1b6f Mon Sep 17 00:00:00 2001 From: funbars <50187994+funbars@users.noreply.github.com> Date: Fri, 10 May 2019 15:50:16 -0500 Subject: [PATCH 008/341] fix libretro log interface --- libretro/libretro.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 15ea093..f5e4e2e 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -697,6 +697,11 @@ void retro_init(void) snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir); else snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", "."); + + if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) + log_cb = logging.log; + else + log_cb = fallback_log; } void retro_deinit(void) @@ -775,11 +780,6 @@ void retro_set_environment(retro_environment_t cb) { environ_cb = cb; - if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) - log_cb = logging.log; - else - log_cb = fallback_log; - cb(RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO, (void*)subsystems); } From 07bdc60a495c65b07b41e331c106c9df9e522f98 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Sat, 11 May 2019 21:38:32 -0600 Subject: [PATCH 009/341] Use dd instead of non-POSIX head(1) options to trim bootroms. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5a9f9b5..a2df8bf 100644 --- a/Makefile +++ b/Makefile @@ -315,7 +315,7 @@ $(BIN)/BootROMs/%.bin: BootROMs/%.asm -@$(MKDIR) -p $(dir $@) cd BootROMs && rgbasm -o ../$@.tmp ../$< rgblink -o $@.tmp2 $@.tmp - head -c $(if $(findstring dmg,$@)$(findstring sgb,$@), 256, 2304) $@.tmp2 > $@ + dd if=$@.tmp2 of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304) @rm $@.tmp $@.tmp2 # Libretro Core (uses its own build system) From 40f83c8f251c43c3ea61225a39b37af8f7930ca8 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Wed, 15 May 2019 12:39:08 +0200 Subject: [PATCH 010/341] Add APU-related debugger commands This change includes making one of the APU functions public --- Core/apu.c | 164 +++++++++++++++---------------- Core/apu.h | 39 ++++---- Core/debugger.c | 256 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 315 insertions(+), 144 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index a0b9aa1..8d2bf34 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -21,34 +21,34 @@ static void refresh_channel(GB_gameboy_t *gb, unsigned index, unsigned cycles_of gb->apu_output.last_update[index] = gb->apu_output.cycles_since_render + cycles_offset; } -static bool is_DAC_enabled(GB_gameboy_t *gb, unsigned index) +bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index) { switch (index) { case GB_SQUARE_1: return gb->io_registers[GB_IO_NR12] & 0xF8; - + case GB_SQUARE_2: return gb->io_registers[GB_IO_NR22] & 0xF8; - + case GB_WAVE: return gb->apu.wave_channel.enable; - + case GB_NOISE: return gb->io_registers[GB_IO_NR42] & 0xF8; } - + return 0; } static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsigned cycles_offset) { - if (!is_DAC_enabled(gb, index)) { + if (!GB_apu_is_DAC_enabled(gb, index)) { value = gb->apu.samples[index]; } else { gb->apu.samples[index] = value; } - + if (gb->apu_output.sample_rate) { unsigned right_volume = 0; if (gb->io_registers[GB_IO_NR51] & (1 << index)) { @@ -73,7 +73,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) UNROLL for (unsigned i = 0; i < GB_N_CHANNELS; i++) { double multiplier = CH_STEP; - if (!is_DAC_enabled(gb, i)) { + if (!GB_apu_is_DAC_enabled(gb, i)) { gb->apu_output.dac_discharge[i] -= ((double) DAC_DECAY_SPEED) / gb->apu_output.sample_rate; if (gb->apu_output.dac_discharge[i] < 0) { multiplier = 0; @@ -113,7 +113,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) (GB_sample_t) {output.left - gb->apu_output.highpass_diff.left, output.right - gb->apu_output.highpass_diff.right} : output; - + switch (gb->apu_output.highpass_mode) { case GB_HIGHPASS_OFF: gb->apu_output.highpass_diff = (GB_double_sample_t) {0, 0}; @@ -146,10 +146,10 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) gb->apu_output.highpass_diff = (GB_double_sample_t) {left_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.left * gb->apu_output.highpass_rate, right_volume * (1 - gb->apu_output.highpass_rate) + gb->apu_output.highpass_diff.right * gb->apu_output.highpass_rate}; - + case GB_HIGHPASS_MAX:; } - + } if (dest) { *dest = filtered_output; @@ -176,7 +176,7 @@ static uint16_t new_sweep_sample_legnth(GB_gameboy_t *gb) static void update_square_sample(GB_gameboy_t *gb, unsigned index) { if (gb->apu.square_channels[index].current_sample_index & 0x80) return; - + uint8_t duty = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6; update_sample(gb, index, duties[gb->apu.square_channels[index].current_sample_index + duty * 8]? @@ -195,38 +195,38 @@ static void nrx2_glitch(uint8_t *volume, uint8_t value, uint8_t old_value) if (value & 8) { (*volume)++; } - + if (((value ^ old_value) & 8)) { *volume = 0x10 - *volume; } - + if ((value & 7) && !(old_value & 7) && *volume && !(value & 8)) { (*volume)--; } - + if ((old_value & 7) && (value & 8)) { (*volume)--; } - + (*volume) &= 0xF; } static void tick_square_envelope(GB_gameboy_t *gb, enum GB_CHANNELS index) { uint8_t nrx2 = gb->io_registers[index == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; - + if (gb->apu.square_channels[index].volume_countdown || (nrx2 & 7)) { if (!gb->apu.square_channels[index].volume_countdown || !--gb->apu.square_channels[index].volume_countdown) { if ((nrx2 & 8) && gb->apu.square_channels[index].current_volume < 0xF) { gb->apu.square_channels[index].current_volume++; } - + else if (!(nrx2 & 8) && gb->apu.square_channels[index].current_volume > 0) { gb->apu.square_channels[index].current_volume--; } - + gb->apu.square_channels[index].volume_countdown = nrx2 & 7; - + if (gb->apu.is_active[index]) { update_square_sample(gb, index); } @@ -237,19 +237,19 @@ static void tick_square_envelope(GB_gameboy_t *gb, enum GB_CHANNELS index) static void tick_noise_envelope(GB_gameboy_t *gb) { uint8_t nr42 = gb->io_registers[GB_IO_NR42]; - + if (gb->apu.noise_channel.volume_countdown || (nr42 & 7)) { if (!--gb->apu.noise_channel.volume_countdown) { if ((nr42 & 8) && gb->apu.noise_channel.current_volume < 0xF) { gb->apu.noise_channel.current_volume++; } - + else if (!(nr42 & 8) && gb->apu.noise_channel.current_volume > 0) { gb->apu.noise_channel.current_volume--; } - + gb->apu.noise_channel.volume_countdown = nr42 & 7; - + if (gb->apu.is_active[GB_NOISE]) { update_sample(gb, GB_NOISE, (gb->apu.noise_channel.lfsr & 1) ? @@ -276,20 +276,20 @@ void GB_apu_div_event(GB_gameboy_t *gb) tick_square_envelope(gb, i); } } - + if (gb->apu.is_active[GB_NOISE] && gb->apu.noise_channel.volume_countdown == 0 && (gb->io_registers[GB_IO_NR42] & 7)) { tick_noise_envelope(gb); } } - + if ((gb->apu.div_divider & 7) == 0) { for (unsigned i = GB_SQUARE_2 + 1; i--;) { tick_square_envelope(gb, i); } - + tick_noise_envelope(gb); } - + if ((gb->apu.div_divider & 1) == 1) { for (unsigned i = GB_SQUARE_2 + 1; i--;) { if (gb->apu.square_channels[i].length_enabled) { @@ -301,7 +301,7 @@ void GB_apu_div_event(GB_gameboy_t *gb) } } } - + if (gb->apu.wave_channel.length_enabled) { if (gb->apu.wave_channel.pulse_length) { if (!--gb->apu.wave_channel.pulse_length) { @@ -311,7 +311,7 @@ void GB_apu_div_event(GB_gameboy_t *gb) } } } - + if (gb->apu.noise_channel.length_enabled) { if (gb->apu.noise_channel.pulse_length) { if (!--gb->apu.noise_channel.pulse_length) { @@ -321,7 +321,7 @@ void GB_apu_div_event(GB_gameboy_t *gb) } } } - + if ((gb->apu.div_divider & 3) == 3) { if (!gb->apu.sweep_enabled) { return; @@ -333,12 +333,12 @@ void GB_apu_div_event(GB_gameboy_t *gb) gb->apu.shadow_sweep_sample_legnth = gb->apu.new_sweep_sample_legnth; } - + if (gb->io_registers[GB_IO_NR10] & 0x70) { /* Recalculation and overflow check only occurs after a delay */ gb->apu.square_sweep_calculate_countdown = 0x13 - gb->apu.lf_div; } - + gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7); if (!gb->apu.square_sweep_countdown) gb->apu.square_sweep_countdown = 8; } @@ -353,11 +353,11 @@ void GB_apu_run(GB_gameboy_t *gb) uint8_t cycles = gb->apu.apu_cycles >> 2; gb->apu.apu_cycles = 0; if (!cycles) return; - + /* To align the square signal to 1MHz */ gb->apu.lf_div ^= cycles & 1; gb->apu.noise_channel.alignment += cycles; - + if (gb->apu.square_sweep_calculate_countdown) { if (gb->apu.square_sweep_calculate_countdown > cycles) { gb->apu.square_sweep_calculate_countdown -= cycles; @@ -374,7 +374,7 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.square_sweep_calculate_countdown = 0; } } - + UNROLL for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { if (gb->apu.is_active[i]) { @@ -384,7 +384,7 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.square_channels[i].sample_countdown = (gb->apu.square_channels[i].sample_length ^ 0x7FF) * 2 + 1; gb->apu.square_channels[i].current_sample_index++; gb->apu.square_channels[i].current_sample_index &= 0x7; - + update_square_sample(gb, i); } if (cycles_left) { @@ -392,7 +392,7 @@ void GB_apu_run(GB_gameboy_t *gb) } } } - + gb->apu.wave_channel.wave_form_just_read = false; if (gb->apu.is_active[GB_WAVE]) { uint8_t cycles_left = cycles; @@ -413,19 +413,19 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.wave_channel.wave_form_just_read = false; } } - + if (gb->apu.is_active[GB_NOISE]) { uint8_t cycles_left = cycles; while (unlikely(cycles_left > gb->apu.noise_channel.sample_countdown)) { cycles_left -= gb->apu.noise_channel.sample_countdown + 1; gb->apu.noise_channel.sample_countdown = gb->apu.noise_channel.sample_length * 4 + 3; - + /* Step LFSR */ unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; /* Todo: is this formula is different on a GBA? */ bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1; gb->apu.noise_channel.lfsr >>= 1; - + if (new_high_bit) { gb->apu.noise_channel.lfsr |= high_bit_mask; } @@ -433,19 +433,19 @@ void GB_apu_run(GB_gameboy_t *gb) /* This code is not redundent, it's relevant when switching LFSR widths */ gb->apu.noise_channel.lfsr &= ~high_bit_mask; } - + gb->apu.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1; if (gb->model == GB_MODEL_CGB_C) { /* Todo: This was confirmed to happen on a CGB-C. This may or may not happen on pre-CGB models. Because this degrades audio quality, and testing this on a pre-CGB device requires audio records, I'll assume these devices are innocent until proven guilty. - + Also happens on CGB-B, but not on CGB-D. */ gb->apu.current_lfsr_sample &= gb->apu.previous_lfsr_sample; } gb->apu.previous_lfsr_sample = gb->apu.noise_channel.lfsr & 1; - + update_sample(gb, GB_NOISE, gb->apu.current_lfsr_sample ? gb->apu.noise_channel.current_volume : 0, @@ -455,10 +455,10 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.noise_channel.sample_countdown -= cycles_left; } } - + if (gb->apu_output.sample_rate) { gb->apu_output.cycles_since_render += cycles; - + if (gb->apu_output.sample_cycles > gb->apu_output.cycles_per_sample) { gb->apu_output.sample_cycles -= gb->apu_output.cycles_per_sample; render(gb, false, NULL); @@ -475,7 +475,7 @@ void GB_apu_copy_buffer(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) /* TODO: Rewrite this as a proper cyclic buffer. This is a workaround to avoid a very rare crashing race condition */ size_t buffer_position = gb->apu_output.buffer_position; - + if (!gb->apu_output.stream_started) { // Intentionally fail the first copy to sync the stream with the Gameboy. gb->apu_output.stream_started = true; @@ -487,11 +487,11 @@ void GB_apu_copy_buffer(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) // GB_log(gb, "Audio underflow: %d\n", count - gb->apu_output.buffer_position); GB_sample_t output; render(gb, true, &output); - + for (unsigned i = 0; i < count - buffer_position; i++) { dest[buffer_position + i] = output; } - + if (buffer_position) { if (gb->apu_output.buffer_size + (count - buffer_position) < count * 3) { gb->apu_output.buffer_size += count - buffer_position; @@ -579,7 +579,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } reg = GB_IO_WAV_START + gb->apu.wave_channel.current_sample_index / 2; } - + /* Todo: this can and should be rewritten with a function table. */ switch (reg) { /* Globals */ @@ -593,7 +593,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } break; case GB_IO_NR52: { - + uint8_t old_nrx1[] = { gb->io_registers[GB_IO_NR11], gb->io_registers[GB_IO_NR21], @@ -612,10 +612,10 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) memset(gb->io_registers + GB_IO_NR10, 0, GB_IO_WAV_START - GB_IO_NR10); old_nrx1[0] &= 0x3F; old_nrx1[1] &= 0x3F; - + gb->apu.global_enable = false; } - + if (!GB_is_cgb(gb) && (value & 0x80)) { GB_apu_write(gb, GB_IO_NR11, old_nrx1[0]); GB_apu_write(gb, GB_IO_NR21, old_nrx1[1]); @@ -624,7 +624,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } } break; - + /* Square channels */ case GB_IO_NR10: if (gb->apu.sweep_decreasing && !(value & 8)) { @@ -639,7 +639,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_sweep_calculate_countdown = 0; } break; - + case GB_IO_NR11: case GB_IO_NR21: { unsigned index = reg == GB_IO_NR21? GB_SQUARE_2: GB_SQUARE_1; @@ -649,7 +649,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } break; } - + case GB_IO_NR12: case GB_IO_NR22: { unsigned index = reg == GB_IO_NR22? GB_SQUARE_2: GB_SQUARE_1; @@ -667,10 +667,10 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) nrx2_glitch(&gb->apu.square_channels[index].current_volume, value, gb->io_registers[reg]); update_square_sample(gb, index); } - + break; } - + case GB_IO_NR13: case GB_IO_NR23: { unsigned index = reg == GB_IO_NR23? GB_SQUARE_2: GB_SQUARE_1; @@ -678,7 +678,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_channels[index].sample_length |= value & 0xFF; break; } - + case GB_IO_NR14: case GB_IO_NR24: { unsigned index = reg == GB_IO_NR24? GB_SQUARE_2: GB_SQUARE_1; @@ -700,16 +700,16 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 4 - gb->apu.lf_div; } gb->apu.square_channels[index].current_volume = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] >> 4; - + /* The volume changes caused by NRX4 sound start take effect instantly (i.e. the effect the previously started sound). The playback itself is not instant which is why we don't update the sample for other cases. */ if (gb->apu.is_active[index]) { update_square_sample(gb, index); } - + gb->apu.square_channels[index].volume_countdown = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 7; - + if ((gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] & 0xF8) != 0 && !gb->apu.is_active[index]) { gb->apu.is_active[index] = true; update_sample(gb, index, 0, 0); @@ -720,7 +720,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_channels[index].pulse_length = 0x40; gb->apu.square_channels[index].length_enabled = false; } - + if (index == GB_SQUARE_1) { gb->apu.sweep_decreasing = false; if (gb->io_registers[GB_IO_NR10] & 7) { @@ -734,9 +734,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7); if (!gb->apu.square_sweep_countdown) gb->apu.square_sweep_countdown = 8; } - + } - + /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ if ((value & 0x40) && !gb->apu.square_channels[index].length_enabled && @@ -756,7 +756,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_channels[index].length_enabled = value & 0x40; break; } - + /* Wave channel */ case GB_IO_NR30: gb->apu.wave_channel.enable = value & 0x80; @@ -788,12 +788,12 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.wave_channel.sample_countdown == 0 && gb->apu.wave_channel.enable) { unsigned offset = ((gb->apu.wave_channel.current_sample_index + 1) >> 1) & 0xF; - + /* This glitch varies between models and even specific instances: DMG-B: Most of them behave as emulated. A few behave differently. SGB: As far as I know, all tested instances behave as emulated. MGB, SGB2: Most instances behave non-deterministically, a few behave as emulated. - + Additionally, I believe DMGs, including those we behave differently than emulated, are all deterministic. */ if (offset < 4) { @@ -827,7 +827,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) mean differences in the DACs. */ /* Todo: Similar issues may apply to the other channels on the DMG/AGB, test, verify and fix if needed */ } - + /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ if ((value & 0x40) && !gb->apu.wave_channel.length_enabled && @@ -851,14 +851,14 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } break; - + /* Noise Channel */ - + case GB_IO_NR41: { gb->apu.noise_channel.pulse_length = (0x40 - (value & 0x3f)); break; } - + case GB_IO_NR42: { if (((value & 0x7) == 0) && ((gb->io_registers[reg] & 0x7) != 0)) { /* Envelope disabled */ @@ -879,24 +879,24 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } break; } - + case GB_IO_NR43: { gb->apu.noise_channel.narrow = value & 8; unsigned divisor = (value & 0x07) << 1; if (!divisor) divisor = 1; gb->apu.noise_channel.sample_length = (divisor << (value >> 4)) - 1; - + /* Todo: changing the frequency sometimes delays the next sample. This is probably due to how the frequency is actually calculated in the noise channel, which is probably not by calculating the effective sample length and counting simiarly to the other channels. This is not emulated correctly. */ break; } - + case GB_IO_NR44: { if (value & 0x80) { gb->apu.noise_channel.sample_countdown = (gb->apu.noise_channel.sample_length) * 2 + 6 - gb->apu.lf_div; - + /* I'm COMPLETELY unsure about this logic, but it passes all relevant tests. See comment in NR43. */ if ((gb->io_registers[GB_IO_NR43] & 7) && (gb->apu.noise_channel.alignment & 2) == 0) { @@ -910,9 +910,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) if (gb->apu.is_active[GB_NOISE]) { gb->apu.noise_channel.sample_countdown += 2; } - + gb->apu.noise_channel.current_volume = gb->io_registers[GB_IO_NR42] >> 4; - + /* The volume changes caused by NRX4 sound start take effect instantly (i.e. the effect the previously started sound). The playback itself is not instant which is why we don't update the sample for other cases. */ @@ -925,18 +925,18 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.noise_channel.lfsr = 0; gb->apu.current_lfsr_sample = false; gb->apu.noise_channel.volume_countdown = gb->io_registers[GB_IO_NR42] & 7; - + if (!gb->apu.is_active[GB_NOISE] && (gb->io_registers[GB_IO_NR42] & 0xF8) != 0) { gb->apu.is_active[GB_NOISE] = true; update_sample(gb, GB_NOISE, 0, 0); } - + if (gb->apu.noise_channel.pulse_length == 0) { gb->apu.noise_channel.pulse_length = 0x40; gb->apu.noise_channel.length_enabled = false; } } - + /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ if ((value & 0x40) && !gb->apu.noise_channel.length_enabled && @@ -956,7 +956,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.noise_channel.length_enabled = value & 0x40; break; } - + default: if (reg >= GB_IO_WAV_START && reg <= GB_IO_WAV_END) { gb->apu.wave_channel.wave_form[(reg - GB_IO_WAV_START) * 2] = value >> 4; diff --git a/Core/apu.h b/Core/apu.h index bfa3598..56a7369 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -54,23 +54,23 @@ typedef struct { bool global_enable; uint8_t apu_cycles; - + uint8_t samples[GB_N_CHANNELS]; bool is_active[GB_N_CHANNELS]; - + uint8_t div_divider; // The DIV register ticks the APU at 512Hz, but is then divided // once more to generate 128Hz and 64Hz clocks - + uint8_t lf_div; // The APU runs in 2MHz, but channels 1, 2 and 4 run in 1MHZ so we divide // need to divide the signal. - + uint8_t square_sweep_countdown; // In 128Hz uint8_t square_sweep_calculate_countdown; // In 2 MHz uint16_t new_sweep_sample_legnth; uint16_t shadow_sweep_sample_legnth; bool sweep_enabled; bool sweep_decreasing; - + struct { uint16_t pulse_length; // Reloaded from NRX1 (xorred), in 256Hz DIV ticks uint8_t current_volume; // Reloaded from NRX2 @@ -78,44 +78,44 @@ typedef struct uint8_t current_sample_index; /* For save state compatibility, highest bit is reused (See NR14/NR24's write code)*/ - + uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF) uint16_t sample_length; // From NRX3, NRX4, in APU ticks bool length_enabled; // NRX4 } square_channels[2]; - + struct { bool enable; // NR30 uint16_t pulse_length; // Reloaded from NR31 (xorred), in 256Hz DIV ticks uint8_t shift; // NR32 uint16_t sample_length; // NR33, NR34, in APU ticks bool length_enabled; // NR34 - + uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length, xorred $7FF) uint8_t current_sample_index; uint8_t current_sample; // Current sample before shifting. - + int8_t wave_form[32]; bool wave_form_just_read; } wave_channel; - + struct { uint16_t pulse_length; // Reloaded from NR41 (xorred), in 256Hz DIV ticks uint8_t current_volume; // Reloaded from NR42 uint8_t volume_countdown; // Reloaded from NR42 uint16_t lfsr; bool narrow; - + uint16_t sample_countdown; // in APU ticks (Reloaded from sample_length) uint16_t sample_length; // From NR43, in APU ticks bool length_enabled; // NR44 - + uint8_t alignment; // If (NR43 & 7) != 0, samples are aligned to 512KHz clock instead of // 1MHz. This variable keeps track of the alignment. - + } noise_channel; - + bool skip_div_event; bool current_lfsr_sample; bool previous_lfsr_sample; @@ -130,25 +130,25 @@ typedef enum { typedef struct { unsigned sample_rate; - + GB_sample_t *buffer; size_t buffer_size; size_t buffer_position; - + bool stream_started; /* detects first copy request to minimize lag */ volatile bool copy_in_progress; volatile bool lock; - + double sample_cycles; // In 8 MHz units double cycles_per_sample; - + // Samples are NOT normalized to MAX_CH_AMP * 4 at this stage! unsigned cycles_since_render; unsigned last_update[GB_N_CHANNELS]; GB_sample_t current_sample[GB_N_CHANNELS]; GB_sample_t summed_samples[GB_N_CHANNELS]; double dac_discharge[GB_N_CHANNELS]; - + GB_highpass_mode_t highpass_mode; double highpass_rate; GB_double_sample_t highpass_diff; @@ -160,6 +160,7 @@ size_t GB_apu_get_current_buffer_length(GB_gameboy_t *gb); void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode); #ifdef GB_INTERNAL +bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index); void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value); uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg); void GB_apu_div_event(GB_gameboy_t *gb); diff --git a/Core/debugger.c b/Core/debugger.c index 7ea3a3f..b1ceff8 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -214,7 +214,7 @@ static value_t read_lvalue(GB_gameboy_t *gb, lvalue_t lvalue) return r; } return VALUE_16(GB_read_memory(gb, lvalue.memory_address.value)); - + case LVALUE_MEMORY16: if (lvalue.memory_address.has_bank) { banking_state_t state; @@ -254,7 +254,7 @@ static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value) } GB_write_memory(gb, lvalue.memory_address.value, value); return; - + case LVALUE_MEMORY16: if (lvalue.memory_address.has_bank) { banking_state_t state; @@ -288,7 +288,7 @@ static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value) 25 bit address 16 bit value = 25 bit address 16 bit value 25 bit address = 25 bit address 25 bit address 25 bit address = 16 bit value (since adding pointers, for examples, makes no sense) - + Boolean operators always return a 16-bit value */ #define FIX_BANK(x) ((value_t){a.has_bank ^ b.has_bank, a.has_bank? a.bank : b.bank, (x)}) @@ -473,9 +473,9 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, /* Disable watchpoints while evaulating expressions */ uint16_t n_watchpoints = gb->n_watchpoints; gb->n_watchpoints = 0; - + value_t ret = ERROR; - + *error = false; // Strip whitespace while (length && (string[0] == ' ' || string[0] == '\n' || string[0] == '\r' || string[0] == '\t')) { @@ -547,7 +547,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } if (string[i] == '}') depth--; } - + if (depth == 0) { value_t addr = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value); banking_state_t state; @@ -751,7 +751,7 @@ static bool next(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug print_usage(gb, command); return true; } - + gb->debug_stopped = false; gb->debug_next_command = true; gb->debug_call_depth = 0; @@ -791,7 +791,7 @@ static bool stack_leak_detection(GB_gameboy_t *gb, char *arguments, char *modifi { NO_MODIFIERS STOPPED_ONLY - + if (strlen(lstrip(arguments))) { print_usage(gb, command); return true; @@ -854,7 +854,7 @@ static bool breakpoint(GB_gameboy_t *gb, char *arguments, char *modifiers, const print_usage(gb, command); return true; } - + if (strlen(lstrip(arguments)) == 0) { print_usage(gb, command); return true; @@ -913,9 +913,9 @@ static bool breakpoint(GB_gameboy_t *gb, char *arguments, char *modifiers, const gb->breakpoints[index].condition = NULL; } gb->n_breakpoints++; - + gb->breakpoints[index].is_jump_to = is_jump_to; - + if (is_jump_to) { gb->has_jump_to_breakpoints = true; } @@ -957,7 +957,7 @@ static bool delete(GB_gameboy_t *gb, char *arguments, char *modifiers, const deb index = i; } } - + if (index >= gb->n_breakpoints) { GB_log(gb, "No breakpoint set at %s\n", debugger_value_to_string(gb, result, true)); return true; @@ -965,7 +965,7 @@ static bool delete(GB_gameboy_t *gb, char *arguments, char *modifiers, const deb result.bank = gb->breakpoints[index].bank; result.has_bank = gb->breakpoints[index].bank != (uint16_t) -1; - + if (gb->breakpoints[index].condition) { free(gb->breakpoints[index].condition); } @@ -980,7 +980,7 @@ static bool delete(GB_gameboy_t *gb, char *arguments, char *modifiers, const deb } } } - + memmove(&gb->breakpoints[index], &gb->breakpoints[index + 1], (gb->n_breakpoints - index - 1) * sizeof(gb->breakpoints[0])); gb->n_breakpoints--; gb->breakpoints = realloc(gb->breakpoints, gb->n_breakpoints * sizeof(gb->breakpoints[0])); @@ -1140,12 +1140,12 @@ static bool unwatch(GB_gameboy_t *gb, char *arguments, char *modifiers, const de index = i; } } - + if (index >= gb->n_watchpoints) { GB_log(gb, "No watchpoint set at %s\n", debugger_value_to_string(gb, result, true)); return true; } - + result.bank = gb->watchpoints[index].bank; result.has_bank = gb->watchpoints[index].bank != (uint16_t) -1; @@ -1457,7 +1457,7 @@ static bool backtrace(GB_gameboy_t *gb, char *arguments, char *modifiers, const print_usage(gb, command); return true; } - + GB_log(gb, " 1. %s\n", debugger_value_to_string(gb, (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}, true)); for (unsigned int i = gb->backtrace_size; i--;) { GB_log(gb, "%3d. %s\n", gb->backtrace_size - i + 1, debugger_value_to_string(gb, (value_t){true, gb->backtrace_returns[i].bank, gb->backtrace_returns[i].addr}, true)); @@ -1532,7 +1532,7 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, " Background and Window Tileset: %s\n", (gb->io_registers[GB_IO_LCDC] & 16)? "$8000" : "$8800"); GB_log(gb, " Window: %s\n", (gb->io_registers[GB_IO_LCDC] & 32)? "Enabled" : "Disabled"); GB_log(gb, " Window tilemap: %s\n", (gb->io_registers[GB_IO_LCDC] & 64)? "$9C00" : "$9800"); - + GB_log(gb, "\nSTAT:\n"); static const char *modes[] = {"Mode 0, H-Blank", "Mode 1, V-Blank", "Mode 2, OAM", "Mode 3, Rendering"}; GB_log(gb, " Current mode: %s\n", modes[gb->io_registers[GB_IO_STAT] & 3]); @@ -1541,9 +1541,9 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, " V-Blank interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 16)? "Enabled" : "Disabled"); GB_log(gb, " OAM interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 32)? "Enabled" : "Disabled"); GB_log(gb, " LYC interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 64)? "Enabled" : "Disabled"); - - - + + + GB_log(gb, "\nCurrent line: %d\n", gb->current_line); GB_log(gb, "Current state: "); if (!(gb->io_registers[GB_IO_LCDC] & 0x80)) { @@ -1566,7 +1566,173 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, "LYC: %d\n", gb->io_registers[GB_IO_LYC]); GB_log(gb, "Window position: %d, %d\n", (signed) gb->io_registers[GB_IO_WX] - 7 , gb->io_registers[GB_IO_WY]); GB_log(gb, "Interrupt line: %s\n", gb->stat_interrupt_line? "On" : "Off"); - + + return true; +} + +static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) +{ + NO_MODIFIERS + if (strlen(lstrip(arguments))) { + print_usage(gb, command); + return true; + } + + + GB_log(gb, "Current state: "); + if(!gb->apu.global_enable) { + GB_log(gb, "Disabled\n"); + } + else { + GB_log(gb, "Enabled\n"); + for(uint8_t channel = 0; channel < GB_N_CHANNELS; channel++) { + GB_log(gb, "CH%u is %s, DAC %s; current sample = 0x%x\n", channel + 1, + gb->apu.is_active[channel] ? "active " : "inactive", + GB_apu_is_DAC_enabled(gb, channel) ? "active " : "inactive", + gb->apu.samples[channel]); + } + } + + GB_log(gb, "SO1 (left output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x07); + if(gb->io_registers[GB_IO_NR51] & 0x0f) { + for(uint8_t channel = 0, mask = 0x01; channel < GB_N_CHANNELS; channel++, mask <<= 1) { + if(gb->io_registers[GB_IO_NR51] & mask) { + GB_log(gb, " CH%u", channel + 1); + } + } + } else { + GB_log(gb, " no channels"); + } + GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": ""); + + GB_log(gb, "SO2 (right output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x70 >> 4); + if(gb->io_registers[GB_IO_NR51] & 0xf0) { + for(uint8_t channel = 0, mask = 0x10; channel < GB_N_CHANNELS; channel++, mask <<= 1) { + if(gb->io_registers[GB_IO_NR51] & mask) { + GB_log(gb, " CH%u", channel + 1); + } + } + } else { + GB_log(gb, " no channels"); + } + GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": ""); + + + for(uint8_t channel = GB_SQUARE_1; channel <= GB_SQUARE_2; channel++) { + GB_log(gb, "\nCH%u:\n", channel + 1); + GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n", + gb->apu.square_channels[channel].current_volume, + gb->apu.square_channels[channel].sample_length, + gb->apu.square_channels[channel].sample_countdown); + + uint8_t nrx2 = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; + GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n", + gb->apu.square_channels[channel].volume_countdown, + nrx2 & 8 ? "in" : "de", + nrx2 & 7); + + uint8_t duty = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6; + GB_log(gb, " Duty cycle %s%% (%s), current index %u/8%s\n", + (char*[]){"12.5", " 25", " 50", " 75"}[duty], + (char*[]){"_______-", "-______-", "-____---", "_------_"}[duty], + gb->apu.square_channels[channel].current_sample_index & 0x7f, + gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : ""); + + if(channel == GB_SQUARE_1) { + GB_log(gb, " Frequency sweep %s and %s (next in %u APU ticks)\n", + gb->apu.sweep_enabled? "active" : "inactive", + gb->apu.sweep_decreasing? "decreasing" : "increasing", + gb->apu.square_sweep_calculate_countdown); + } + + if(gb->apu.square_channels[channel].length_enabled) { + GB_log(gb, " Channel will end in %u 256 Hz ticks\n", + gb->apu.square_channels[channel].pulse_length); + } + } + + + GB_log(gb, "\nCH3:\n"); + GB_log(gb, " Wave:"); + for(uint8_t i = 0; i < 32; i++) { + GB_log(gb, "%s%X", i%4?"":" ", gb->apu.wave_channel.wave_form[i]); + } + GB_log(gb, ", has %sjust been read\n", gb->apu.wave_channel.wave_form_just_read? "": "not "); + GB_log(gb, " Current position: %u\n", gb->apu.wave_channel.current_sample_index); + + GB_log(gb, " Volume %s (right-shifted %ux)\n", + (char*[]){"100%", "50%", "25%", NULL, "muted"}[gb->apu.wave_channel.shift], + gb->apu.wave_channel.shift); + + GB_log(gb, " Current sample length: %u APU ticks (next in %u ticks)\n", + gb->apu.wave_channel.sample_length, + gb->apu.wave_channel.sample_countdown); + + if(gb->apu.wave_channel.length_enabled) { + GB_log(gb, " Channel will end in %u 256 Hz ticks\n", + gb->apu.wave_channel.pulse_length); + } + + + GB_log(gb, "\nCH4:\n"); + GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n", + gb->apu.noise_channel.current_volume, + gb->apu.noise_channel.sample_length, + gb->apu.noise_channel.sample_countdown); + + GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n", + gb->apu.noise_channel.volume_countdown, + gb->io_registers[GB_IO_NR42] & 8 ? "in" : "de", + gb->io_registers[GB_IO_NR42] & 7); + + GB_log(gb, " LFSR in %u-step mode, current value %%", + gb->apu.noise_channel.narrow? 7 : 15); + for(uint16_t lfsr = gb->apu.noise_channel.lfsr, i = 15; i--; lfsr <<= 1) { + GB_log(gb, "%u%s", (lfsr >> 14) & 1, i%4 ? "" : " "); + } + + if(gb->apu.noise_channel.length_enabled) { + GB_log(gb, " Channel will end in %u 256 Hz ticks\n", + gb->apu.noise_channel.pulse_length); + } + + + GB_log(gb, "\n\nReminder: APU ticks are @ 2 MiHz\n"); + + return true; +} + +static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) +{ + if (strlen(lstrip(arguments)) || (modifiers && !strchr("fcl", modifiers[0]))) { + print_usage(gb, command); + return true; + } + + uint8_t shift_amount = 1, mask; + if(modifiers) { + switch(modifiers[0]) { + case 'c': + shift_amount = 2; + break; + case 'l': + shift_amount = 8; + break; + } + } + mask = (0xf << (shift_amount - 1)) & 0xf; + + for(int8_t cur_val = 0xf & mask; cur_val >= 0; cur_val -= shift_amount) { + for(uint8_t i = 0; i < 32; i++) { + if((gb->apu.wave_channel.wave_form[i] & mask) == cur_val) { + GB_log(gb, "%X", gb->apu.wave_channel.wave_form[i]); + } else { + GB_log(gb, "%c", i%4 == 2 ? '-' : ' '); + } + } + GB_log(gb, "\n"); + } + return true; } @@ -1587,6 +1753,10 @@ static const debugger_command_t commands[] = { {"registers", 1, registers, "Print values of processor registers and other important registers"}, {"cartridge", 2, mbc, "Displays information about the MBC and cartridge"}, {"mbc", 3, }, /* Alias */ + {"apu", 3, apu, "Displays information about the current state of the audio chip"}, + {"wave", 3, wave, "Prints a visual representation of the wave RAM" HELP_NEWLINE + "Modifiers can be used for a (f)ull print (the default)," HELP_NEWLINE + "a more (c)ompact one, or a one-(l)iner"}, {"lcd", 3, lcd, "Displays information about the current state of the LCD controller"}, {"palettes", 3, palettes, "Displays the current CGB palettes"}, {"breakpoint", 1, breakpoint, "Add a new breakpoint at the specified address/expression" HELP_NEWLINE @@ -1875,7 +2045,7 @@ static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *add void GB_debugger_run(GB_gameboy_t *gb) { if (gb->debug_disable) return; - + char *input = NULL; if (gb->debug_next_command && gb->debug_call_depth <= 0) { gb->debug_stopped = true; @@ -1895,11 +2065,11 @@ next_command: GB_log(gb, "Breakpoint: PC = %s\n", value_to_string(gb, gb->pc, true)); GB_cpu_disassemble(gb, gb->pc, 5); } - + if (gb->breakpoints && !gb->debug_stopped) { uint16_t address = 0; jump_to_return_t jump_to_result = test_jump_to_breakpoints(gb, &address); - + bool should_delete_state = true; if (gb->nontrivial_jump_state && should_break(gb, gb->pc, true)) { if (gb->non_trivial_jump_breakpoint_occured) { @@ -1930,7 +2100,7 @@ next_command: else { gb->non_trivial_jump_breakpoint_occured = false; } - + if (should_delete_state) { if (gb->nontrivial_jump_state) { free(gb->nontrivial_jump_state); @@ -2077,17 +2247,17 @@ static bool is_in_trivial_memory(uint16_t addr) if (addr < 0x8000) { return true; } - + /* HRAM */ if (addr >= 0xFF80 && addr < 0xFFFF) { return true; } - + /* RAM */ if (addr >= 0xC000 && addr < 0xE000) { return true; } - + return false; } @@ -2125,7 +2295,7 @@ static bool condition_code(GB_gameboy_t *gb, uint8_t opcode) case 3: return (gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG); } - + return false; } @@ -2134,7 +2304,7 @@ static uint16_t jr_cc_r8(GB_gameboy_t *gb, uint8_t opcode) if (!condition_code(gb, opcode)) { return gb->pc + 2; } - + return gb->pc + 2 + (int8_t)GB_read_memory(gb, gb->pc + 1); } @@ -2221,12 +2391,12 @@ static GB_opcode_address_getter_t *opcodes[256] = { static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *address) { if (!gb->has_jump_to_breakpoints) return JUMP_TO_NONE; - + if (!is_in_trivial_memory(gb->pc) || !is_in_trivial_memory(gb->pc + 2) || !is_in_trivial_memory(gb->registers[GB_REGISTER_SP]) || !is_in_trivial_memory(gb->registers[GB_REGISTER_SP] + 1)) { return JUMP_TO_NONTRIVIAL; } - + /* Interrupts */ if (gb->ime) { for (unsigned i = 0; i < 5; i++) { @@ -2240,38 +2410,38 @@ static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *add } } } - + uint16_t n_watchpoints = gb->n_watchpoints; gb->n_watchpoints = 0; - + uint8_t opcode = GB_read_memory(gb, gb->pc); - + if (opcode == 0x76) { gb->n_watchpoints = n_watchpoints; if (gb->ime) { /* Already handled in above */ return JUMP_TO_NONE; } - + if (gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F) { return JUMP_TO_NONTRIVIAL; /* HALT bug could occur */ } - + return JUMP_TO_NONE; } - + GB_opcode_address_getter_t *getter = opcodes[opcode]; if (!getter) { gb->n_watchpoints = n_watchpoints; return JUMP_TO_NONE; } - + uint16_t new_pc = getter(gb, opcode); - + gb->n_watchpoints = n_watchpoints; - + if (address) { *address = new_pc; } - + return should_break(gb, new_pc, true) ? JUMP_TO_BREAK : JUMP_TO_NONE; } From 91eeb4d9d542a3bfd0de59409026a0f6ef4b1637 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 17 May 2019 00:08:34 +0300 Subject: [PATCH 011/341] Emulate AGB audio mixing --- Core/apu.c | 76 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index a0b9aa1..231b23f 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -23,6 +23,13 @@ static void refresh_channel(GB_gameboy_t *gb, unsigned index, unsigned cycles_of static bool is_DAC_enabled(GB_gameboy_t *gb, unsigned index) { + if (gb->model >= GB_MODEL_AGB) { + /* On the AGB, mixing is done digitally, so there are no per-channel + DACs. Instead, all channels are summed digital regardless of + whatever the DAC state would be on a CGB or earlier model. */ + return true; + } + switch (index) { case GB_SQUARE_1: return gb->io_registers[GB_IO_NR12] & 0xF8; @@ -37,11 +44,45 @@ static bool is_DAC_enabled(GB_gameboy_t *gb, unsigned index) return gb->io_registers[GB_IO_NR42] & 0xF8; } - return 0; + return false; } static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsigned cycles_offset) { + if (gb->model >= GB_MODEL_AGB) { + /* On the AGB, because no analog mixing is done, the behavior of NR51 is a bit different. + A channel that is not connected to a terminal is idenitcal to a connected channel + playing PCM sample 0. */ + gb->apu.samples[index] = value; + + if (gb->apu_output.sample_rate) { + unsigned right_volume = (gb->io_registers[GB_IO_NR50] & 7) + 1; + unsigned left_volume = ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1; + + GB_sample_t output; + if (gb->io_registers[GB_IO_NR51] & (1 << index)) { + output.right = (0xf - value * 2) * right_volume; + } + else { + output.right = 0xf * right_volume; + } + + if (gb->io_registers[GB_IO_NR51] & (0x10 << index)) { + output.left = (0xf - value * 2) * left_volume; + } + else { + output.left = 0xf * left_volume; + } + + if (*(uint32_t *)&(gb->apu_output.current_sample[index]) != *(uint32_t *)&output) { + refresh_channel(gb, index, cycles_offset); + gb->apu_output.current_sample[index] = output; + } + } + + return; + } + if (!is_DAC_enabled(gb, index)) { value = gb->apu.samples[index]; } @@ -73,23 +114,26 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) UNROLL for (unsigned i = 0; i < GB_N_CHANNELS; i++) { double multiplier = CH_STEP; - if (!is_DAC_enabled(gb, i)) { - gb->apu_output.dac_discharge[i] -= ((double) DAC_DECAY_SPEED) / gb->apu_output.sample_rate; - if (gb->apu_output.dac_discharge[i] < 0) { - multiplier = 0; - gb->apu_output.dac_discharge[i] = 0; + + if (gb->model < GB_MODEL_AGB) { + if (!is_DAC_enabled(gb, i)) { + gb->apu_output.dac_discharge[i] -= ((double) DAC_DECAY_SPEED) / gb->apu_output.sample_rate; + if (gb->apu_output.dac_discharge[i] < 0) { + multiplier = 0; + gb->apu_output.dac_discharge[i] = 0; + } + else { + multiplier *= gb->apu_output.dac_discharge[i]; + } } else { - multiplier *= gb->apu_output.dac_discharge[i]; - } - } - else { - gb->apu_output.dac_discharge[i] += ((double) DAC_ATTACK_SPEED) / gb->apu_output.sample_rate; - if (gb->apu_output.dac_discharge[i] > 1) { - gb->apu_output.dac_discharge[i] = 1; - } - else { - multiplier *= gb->apu_output.dac_discharge[i]; + gb->apu_output.dac_discharge[i] += ((double) DAC_ATTACK_SPEED) / gb->apu_output.sample_rate; + if (gb->apu_output.dac_discharge[i] > 1) { + gb->apu_output.dac_discharge[i] = 1; + } + else { + multiplier *= gb->apu_output.dac_discharge[i]; + } } } From 6648a0a84d659b1b6d85075856c80584da00e8ab Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 17 May 2019 20:48:49 +0300 Subject: [PATCH 012/341] Minor adjustments and style fixes to the new APU debug functions --- Core/debugger.c | 53 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index b1ceff8..795e9fe 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1580,12 +1580,12 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, "Current state: "); - if(!gb->apu.global_enable) { + if (!gb->apu.global_enable) { GB_log(gb, "Disabled\n"); } else { GB_log(gb, "Enabled\n"); - for(uint8_t channel = 0; channel < GB_N_CHANNELS; channel++) { + for (uint8_t channel = 0; channel < GB_N_CHANNELS; channel++) { GB_log(gb, "CH%u is %s, DAC %s; current sample = 0x%x\n", channel + 1, gb->apu.is_active[channel] ? "active " : "inactive", GB_apu_is_DAC_enabled(gb, channel) ? "active " : "inactive", @@ -1594,31 +1594,33 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg } GB_log(gb, "SO1 (left output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x07); - if(gb->io_registers[GB_IO_NR51] & 0x0f) { - for(uint8_t channel = 0, mask = 0x01; channel < GB_N_CHANNELS; channel++, mask <<= 1) { - if(gb->io_registers[GB_IO_NR51] & mask) { + if (gb->io_registers[GB_IO_NR51] & 0x0f) { + for (uint8_t channel = 0, mask = 0x01; channel < GB_N_CHANNELS; channel++, mask <<= 1) { + if (gb->io_registers[GB_IO_NR51] & mask) { GB_log(gb, " CH%u", channel + 1); } } - } else { + } + else { GB_log(gb, " no channels"); } GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": ""); GB_log(gb, "SO2 (right output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x70 >> 4); - if(gb->io_registers[GB_IO_NR51] & 0xf0) { - for(uint8_t channel = 0, mask = 0x10; channel < GB_N_CHANNELS; channel++, mask <<= 1) { - if(gb->io_registers[GB_IO_NR51] & mask) { + if (gb->io_registers[GB_IO_NR51] & 0xf0) { + for (uint8_t channel = 0, mask = 0x10; channel < GB_N_CHANNELS; channel++, mask <<= 1) { + if (gb->io_registers[GB_IO_NR51] & mask) { GB_log(gb, " CH%u", channel + 1); } } - } else { + } + else { GB_log(gb, " no channels"); } GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": ""); - for(uint8_t channel = GB_SQUARE_1; channel <= GB_SQUARE_2; channel++) { + for (uint8_t channel = GB_SQUARE_1; channel <= GB_SQUARE_2; channel++) { GB_log(gb, "\nCH%u:\n", channel + 1); GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n", gb->apu.square_channels[channel].current_volume, @@ -1638,14 +1640,14 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg gb->apu.square_channels[channel].current_sample_index & 0x7f, gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : ""); - if(channel == GB_SQUARE_1) { + if (channel == GB_SQUARE_1) { GB_log(gb, " Frequency sweep %s and %s (next in %u APU ticks)\n", gb->apu.sweep_enabled? "active" : "inactive", gb->apu.sweep_decreasing? "decreasing" : "increasing", gb->apu.square_sweep_calculate_countdown); } - if(gb->apu.square_channels[channel].length_enabled) { + if (gb->apu.square_channels[channel].length_enabled) { GB_log(gb, " Channel will end in %u 256 Hz ticks\n", gb->apu.square_channels[channel].pulse_length); } @@ -1654,13 +1656,13 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, "\nCH3:\n"); GB_log(gb, " Wave:"); - for(uint8_t i = 0; i < 32; i++) { + for (uint8_t i = 0; i < 32; i++) { GB_log(gb, "%s%X", i%4?"":" ", gb->apu.wave_channel.wave_form[i]); } - GB_log(gb, ", has %sjust been read\n", gb->apu.wave_channel.wave_form_just_read? "": "not "); + GB_log(gb, "\n"); GB_log(gb, " Current position: %u\n", gb->apu.wave_channel.current_sample_index); - GB_log(gb, " Volume %s (right-shifted %ux)\n", + GB_log(gb, " Volume %s (right-shifted %u times)\n", (char*[]){"100%", "50%", "25%", NULL, "muted"}[gb->apu.wave_channel.shift], gb->apu.wave_channel.shift); @@ -1668,7 +1670,7 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg gb->apu.wave_channel.sample_length, gb->apu.wave_channel.sample_countdown); - if(gb->apu.wave_channel.length_enabled) { + if (gb->apu.wave_channel.length_enabled) { GB_log(gb, " Channel will end in %u 256 Hz ticks\n", gb->apu.wave_channel.pulse_length); } @@ -1685,13 +1687,13 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg gb->io_registers[GB_IO_NR42] & 8 ? "in" : "de", gb->io_registers[GB_IO_NR42] & 7); - GB_log(gb, " LFSR in %u-step mode, current value %%", + GB_log(gb, " LFSR in %u-step mode, current value ", gb->apu.noise_channel.narrow? 7 : 15); - for(uint16_t lfsr = gb->apu.noise_channel.lfsr, i = 15; i--; lfsr <<= 1) { + for (uint16_t lfsr = gb->apu.noise_channel.lfsr, i = 15; i--; lfsr <<= 1) { GB_log(gb, "%u%s", (lfsr >> 14) & 1, i%4 ? "" : " "); } - if(gb->apu.noise_channel.length_enabled) { + if (gb->apu.noise_channel.length_enabled) { GB_log(gb, " Channel will end in %u 256 Hz ticks\n", gb->apu.noise_channel.pulse_length); } @@ -1710,7 +1712,7 @@ static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug } uint8_t shift_amount = 1, mask; - if(modifiers) { + if (modifiers) { switch(modifiers[0]) { case 'c': shift_amount = 2; @@ -1722,11 +1724,12 @@ static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug } mask = (0xf << (shift_amount - 1)) & 0xf; - for(int8_t cur_val = 0xf & mask; cur_val >= 0; cur_val -= shift_amount) { - for(uint8_t i = 0; i < 32; i++) { - if((gb->apu.wave_channel.wave_form[i] & mask) == cur_val) { + for (int8_t cur_val = 0xf & mask; cur_val >= 0; cur_val -= shift_amount) { + for (uint8_t i = 0; i < 32; i++) { + if ((gb->apu.wave_channel.wave_form[i] & mask) == cur_val) { GB_log(gb, "%X", gb->apu.wave_channel.wave_form[i]); - } else { + } + else { GB_log(gb, "%c", i%4 == 2 ? '-' : ' '); } } From 3ee2c648996c6eb3a2fb32113b984215f54b7b02 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 17 May 2019 22:03:10 +0300 Subject: [PATCH 013/341] Make the apu command a bit safer --- Core/debugger.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index 795e9fe..c433f2f 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1635,16 +1635,16 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg uint8_t duty = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6; GB_log(gb, " Duty cycle %s%% (%s), current index %u/8%s\n", - (char*[]){"12.5", " 25", " 50", " 75"}[duty], - (char*[]){"_______-", "-______-", "-____---", "_------_"}[duty], - gb->apu.square_channels[channel].current_sample_index & 0x7f, - gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : ""); + duty > 3? "" : (const char *[]){"12.5", " 25", " 50", " 75"}[duty], + duty > 3? "" : (const char *[]){"_______-", "-______-", "-____---", "_------_"}[duty], + gb->apu.square_channels[channel].current_sample_index & 0x7f, + gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : ""); if (channel == GB_SQUARE_1) { GB_log(gb, " Frequency sweep %s and %s (next in %u APU ticks)\n", - gb->apu.sweep_enabled? "active" : "inactive", - gb->apu.sweep_decreasing? "decreasing" : "increasing", - gb->apu.square_sweep_calculate_countdown); + gb->apu.sweep_enabled? "active" : "inactive", + gb->apu.sweep_decreasing? "decreasing" : "increasing", + gb->apu.square_sweep_calculate_countdown); } if (gb->apu.square_channels[channel].length_enabled) { @@ -1663,8 +1663,8 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, " Current position: %u\n", gb->apu.wave_channel.current_sample_index); GB_log(gb, " Volume %s (right-shifted %u times)\n", - (char*[]){"100%", "50%", "25%", NULL, "muted"}[gb->apu.wave_channel.shift], - gb->apu.wave_channel.shift); + gb->apu.wave_channel.shift > 4? "" : (const char *[]){"100%", "50%", "25%", "", "muted"}[gb->apu.wave_channel.shift], + gb->apu.wave_channel.shift); GB_log(gb, " Current sample length: %u APU ticks (next in %u ticks)\n", gb->apu.wave_channel.sample_length, From ec5d1b7b88452e2c0778cbe28802132fc65a5fc0 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sat, 18 May 2019 02:46:24 +0200 Subject: [PATCH 014/341] Fix sample lengths for CH1, 2 and 4 --- Core/debugger.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index c433f2f..79321a5 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1623,9 +1623,9 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg for (uint8_t channel = GB_SQUARE_1; channel <= GB_SQUARE_2; channel++) { GB_log(gb, "\nCH%u:\n", channel + 1); GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n", - gb->apu.square_channels[channel].current_volume, - gb->apu.square_channels[channel].sample_length, - gb->apu.square_channels[channel].sample_countdown); + gb->apu.square_channels[channel].current_volume, + (gb->apu.square_channels[channel].sample_length ^ 0x7FF) * 2 + 1, + gb->apu.square_channels[channel].sample_countdown); uint8_t nrx2 = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22]; GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n", @@ -1667,7 +1667,7 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg gb->apu.wave_channel.shift); GB_log(gb, " Current sample length: %u APU ticks (next in %u ticks)\n", - gb->apu.wave_channel.sample_length, + gb->apu.wave_channel.sample_length ^ 0x7ff, gb->apu.wave_channel.sample_countdown); if (gb->apu.wave_channel.length_enabled) { @@ -1679,7 +1679,7 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, "\nCH4:\n"); GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n", gb->apu.noise_channel.current_volume, - gb->apu.noise_channel.sample_length, + gb->apu.noise_channel.sample_length * 4 + 3, gb->apu.noise_channel.sample_countdown); GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n", From 3e724afb0a5e42c8520dce054483be0982967733 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 18 May 2019 18:45:31 +0300 Subject: [PATCH 015/341] Basic SGB support in the SDL port --- Core/gb.c | 4 +- Core/gb.h | 4 +- SDL/gui.c | 94 ++++++++++++++++++++++++---------------- SDL/gui.h | 1 + SDL/main.c | 18 ++++++-- SDL/shader.c | 8 ++-- SDL/shader.h | 4 +- Shaders/MasterShader.fsh | 1 - 8 files changed, 84 insertions(+), 50 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 9456f10..cb99d4c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -798,12 +798,12 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb) return CPU_FREQUENCY * gb->clock_multiplier; } -size_t GB_get_screen_width(GB_gameboy_t *gb) +unsigned GB_get_screen_width(GB_gameboy_t *gb) { return GB_is_sgb(gb)? 256 : 160; } -size_t GB_get_screen_height(GB_gameboy_t *gb) +unsigned GB_get_screen_height(GB_gameboy_t *gb) { return GB_is_sgb(gb)? 224 : 144; } diff --git a/Core/gb.h b/Core/gb.h index a4b7256..30654d5 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -689,8 +689,8 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb); #endif void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier); -size_t GB_get_screen_width(GB_gameboy_t *gb); -size_t GB_get_screen_height(GB_gameboy_t *gb); +unsigned GB_get_screen_width(GB_gameboy_t *gb); +unsigned GB_get_screen_height(GB_gameboy_t *gb); unsigned GB_get_player_count(GB_gameboy_t *gb); diff --git a/SDL/gui.c b/SDL/gui.c index 4de76f5..80f1507 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -44,7 +44,9 @@ void render_texture(void *pixels, void *previous) } glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - render_bitmap_with_shader(&shader, _pixels, previous, rect.x, rect.y, rect.w, rect.h); + render_bitmap_with_shader(&shader, _pixels, previous, + GB_get_screen_width(&gb), GB_get_screen_height(&gb), + rect.x, rect.y, rect.w, rect.h); SDL_GL_SwapWindow(window); } } @@ -116,8 +118,8 @@ void update_viewport(void) { int win_width, win_height; SDL_GL_GetDrawableSize(window, &win_width, &win_height); - double x_factor = win_width / 160.0; - double y_factor = win_height / 144.0; + double x_factor = win_width / (double) GB_get_screen_width(&gb); + double y_factor = win_height / (double) GB_get_screen_height(&gb); if (configuration.scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) { x_factor = (int)(x_factor); @@ -133,8 +135,8 @@ void update_viewport(void) } } - unsigned new_width = x_factor * 160; - unsigned new_height = y_factor * 144; + unsigned new_width = x_factor * GB_get_screen_width(&gb); + unsigned new_height = y_factor * GB_get_screen_height(&gb); rect = (SDL_Rect){(win_width - new_width) / 2, (win_height - new_height) /2, new_width, new_height}; @@ -148,7 +150,7 @@ void update_viewport(void) } /* Does NOT check for bounds! */ -static void draw_char(uint32_t *buffer, unsigned char ch, uint32_t color) +static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color) { if (ch < ' ' || ch > font_max) { ch = '?'; @@ -163,11 +165,11 @@ static void draw_char(uint32_t *buffer, unsigned char ch, uint32_t color) } buffer++; } - buffer += 160 - GLYPH_WIDTH; + buffer += width - GLYPH_WIDTH; } } -static void draw_unbordered_text(uint32_t *buffer, unsigned x, unsigned y, const char *string, uint32_t color) +static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, unsigned y, const char *string, uint32_t color) { unsigned orig_x = x; while (*string) { @@ -178,23 +180,23 @@ static void draw_unbordered_text(uint32_t *buffer, unsigned x, unsigned y, const continue; } - if (x > 160 - GLYPH_WIDTH || y == 0 || y > 144 - GLYPH_HEIGHT) { + if (x > width - GLYPH_WIDTH || y == 0 || y > height - GLYPH_HEIGHT) { break; } - draw_char(&buffer[x + 160 * y], *string, color); + draw_char(&buffer[x + width * y], width, height, *string, color); x += GLYPH_WIDTH; string++; } } -static void draw_text(uint32_t *buffer, unsigned x, unsigned y, const char *string, uint32_t color, uint32_t border) +static void draw_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, unsigned y, const char *string, uint32_t color, uint32_t border) { - draw_unbordered_text(buffer, x - 1, y, string, border); - draw_unbordered_text(buffer, x + 1, y, string, border); - draw_unbordered_text(buffer, x, y - 1, string, border); - draw_unbordered_text(buffer, x, y + 1, string, border); - draw_unbordered_text(buffer, x, y, string, color); + draw_unbordered_text(buffer, width, height, x - 1, y, string, border); + draw_unbordered_text(buffer, width, height, x + 1, y, string, border); + draw_unbordered_text(buffer, width, height, x, y - 1, string, border); + draw_unbordered_text(buffer, width, height, x, y + 1, string, border); + draw_unbordered_text(buffer, width, height, x, y, string, color); } enum decoration { @@ -203,17 +205,17 @@ enum decoration { DECORATION_ARROWS, }; -static void draw_text_centered(uint32_t *buffer, unsigned y, const char *string, uint32_t color, uint32_t border, enum decoration decoration) +static void draw_text_centered(uint32_t *buffer, unsigned width, unsigned height, unsigned y, const char *string, uint32_t color, uint32_t border, enum decoration decoration) { - unsigned x = 160 / 2 - (unsigned) strlen(string) * GLYPH_WIDTH / 2; - draw_text(buffer, x, y, string, color, border); + unsigned x = width / 2 - (unsigned) strlen(string) * GLYPH_WIDTH / 2; + draw_text(buffer, width, height, x, y, string, color, border); switch (decoration) { case DECORATION_SELECTION: - draw_text(buffer, x - GLYPH_WIDTH, y, SELECTION_STRING, color, border); + draw_text(buffer, width, height, x - GLYPH_WIDTH, y, SELECTION_STRING, color, border); break; case DECORATION_ARROWS: - draw_text(buffer, x - GLYPH_WIDTH, y, LEFT_ARROW_STRING, color, border); - draw_text(buffer, 160 - x, y, RIGHT_ARROW_STRING, color, border); + draw_text(buffer, width, height, x - GLYPH_WIDTH, y, LEFT_ARROW_STRING, color, border); + draw_text(buffer, width, height, width - x, y, RIGHT_ARROW_STRING, color, border); break; case DECORATION_NONE: @@ -301,7 +303,7 @@ static void cycle_model_backwards(unsigned index) const char *current_model_string(unsigned index) { - return (const char *[]){"Game Boy", "Game Boy Color", "Game Boy Advance"} + return (const char *[]){"Game Boy", "Game Boy Color", "Game Boy Advance" , "Super Game Boy"} [configuration.model]; } @@ -764,7 +766,18 @@ void run_gui(bool is_running) } } - uint32_t pixels[160 * 144]; + unsigned width = GB_get_screen_width(&gb); + unsigned height = GB_get_screen_height(&gb); + unsigned x_offset = (width - 160) / 2; + unsigned y_offset = (height - 144) / 2; + uint32_t pixels[width * height]; + + if (width != 160 || height != 144) { + for (unsigned i = 0; i < width * height; i++) { + pixels[i] = gui_palette_native[0]; + } + } + SDL_Event event = {0,}; gui_state = is_running? SHOWING_MENU : SHOWING_DROP_MESSAGE; bool should_render = true; @@ -994,32 +1007,39 @@ void run_gui(bool is_running) if (should_render) { should_render = false; - memcpy(pixels, converted_background->pixels, sizeof(pixels)); + if (width == 160 && height == 144) { + memcpy(pixels, converted_background->pixels, sizeof(pixels)); + } + else { + for (unsigned y = 0; y < 144; y++) { + memcpy(pixels + x_offset + width * (y + y_offset), ((uint32_t *)converted_background->pixels) + 160 * y, 160 * 4); + } + } switch (gui_state) { case SHOWING_DROP_MESSAGE: - draw_text_centered(pixels, 8, "Press ESC for menu", gui_palette_native[3], gui_palette_native[0], false); - draw_text_centered(pixels, 116, "Drop a GB or GBC", gui_palette_native[3], gui_palette_native[0], false); - draw_text_centered(pixels, 128, "file to play", gui_palette_native[3], gui_palette_native[0], false); + draw_text_centered(pixels, width, height, 8 + y_offset, "Press ESC for menu", gui_palette_native[3], gui_palette_native[0], false); + draw_text_centered(pixels, width, height, 116 + y_offset, "Drop a GB or GBC", gui_palette_native[3], gui_palette_native[0], false); + draw_text_centered(pixels, width, height, 128 + y_offset, "file to play", gui_palette_native[3], gui_palette_native[0], false); break; case SHOWING_MENU: - draw_text_centered(pixels, 8, "SameBoy", gui_palette_native[3], gui_palette_native[0], false); + draw_text_centered(pixels, width, height, 8 + y_offset, "SameBoy", gui_palette_native[3], gui_palette_native[0], false); unsigned i = 0, y = 24; for (const struct menu_item *item = current_menu; item->string; item++, i++) { if (item->value_getter && !item->backwards_handler) { char line[25]; snprintf(line, sizeof(line), "%s%*s", item->string, 24 - (int)strlen(item->string), item->value_getter(i)); - draw_text_centered(pixels, y, line, gui_palette_native[3], gui_palette_native[0], + draw_text_centered(pixels, width, height, y + y_offset, line, gui_palette_native[3], gui_palette_native[0], i == current_selection ? DECORATION_SELECTION : DECORATION_NONE); y += 12; } else { - draw_text_centered(pixels, y, item->string, gui_palette_native[3], gui_palette_native[0], + draw_text_centered(pixels, width, height, y + y_offset, item->string, gui_palette_native[3], gui_palette_native[0], i == current_selection && !item->value_getter ? DECORATION_SELECTION : DECORATION_NONE); y += 12; if (item->value_getter) { - draw_text_centered(pixels, y, item->value_getter(i), gui_palette_native[3], gui_palette_native[0], + draw_text_centered(pixels, width, height, y + y_offset, item->value_getter(i), gui_palette_native[3], gui_palette_native[0], i == current_selection ? DECORATION_ARROWS : DECORATION_NONE); y += 12; } @@ -1027,16 +1047,16 @@ void run_gui(bool is_running) } break; case SHOWING_HELP: - draw_text(pixels, 2, 2, help[current_help_page], gui_palette_native[3], gui_palette_native[0]); + draw_text(pixels, width, height, 2 + x_offset, 2 + y_offset, help[current_help_page], gui_palette_native[3], gui_palette_native[0]); break; case WAITING_FOR_KEY: - draw_text_centered(pixels, 68, "Press a Key", gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); + draw_text_centered(pixels, width, height, 68 + y_offset, "Press a Key", gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); break; case WAITING_FOR_JBUTTON: - draw_text_centered(pixels, 68, + draw_text_centered(pixels, width, height, 68 + y_offset, joypad_configuration_progress != JOYPAD_BUTTONS_MAX ? "Press button for" : "Move the Analog Stick", gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); - draw_text_centered(pixels, 80, + draw_text_centered(pixels, width, height, 80 + y_offset, (const char *[]) { "Right", @@ -1054,7 +1074,7 @@ void run_gui(bool is_running) "", } [joypad_configuration_progress], gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); - draw_text_centered(pixels, 104, "Press Enter to skip", gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); + draw_text_centered(pixels, width, height, 104 + y_offset, "Press Enter to skip", gui_palette_native[3], gui_palette_native[0], DECORATION_NONE); break; } diff --git a/SDL/gui.h b/SDL/gui.h index 4d10614..9711e83 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -77,6 +77,7 @@ typedef struct { MODEL_DMG, MODEL_CGB, MODEL_AGB, + MODEL_SGB, MODEL_MAX, } model; diff --git a/SDL/main.c b/SDL/main.c index 99facf8..8a65a74 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -24,7 +24,7 @@ GB_gameboy_t gb; static bool paused = false; -static uint32_t pixel_buffer_1[160*144], pixel_buffer_2[160*144]; +static uint32_t pixel_buffer_1[256 * 224], pixel_buffer_2[256 * 224]; static uint32_t *active_pixel_buffer = pixel_buffer_1, *previous_pixel_buffer = pixel_buffer_2; static bool underclock_down = false, rewind_down = false, do_rewind = false, rewind_paused = false, turbo_down = false; static double clock_mutliplier = 1.0; @@ -39,7 +39,8 @@ static const GB_model_t sdl_to_internal_model[] = { [MODEL_DMG] = GB_MODEL_DMG_B, [MODEL_CGB] = GB_MODEL_CGB_E, - [MODEL_AGB] = GB_MODEL_AGB + [MODEL_AGB] = GB_MODEL_AGB, + [MODEL_SGB] = GB_MODEL_SGB, }; void set_filename(const char *new_filename, bool new_should_free) @@ -423,9 +424,16 @@ restart: GB_set_rewind_length(&gb, configuration.rewind_length); } + SDL_DestroyTexture(texture); + texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING, + GB_get_screen_width(&gb), GB_get_screen_height(&gb)); + + SDL_SetWindowMinimumSize(window, GB_get_screen_width(&gb), GB_get_screen_height(&gb)); + + bool error = false; start_capturing_logs(); - const char * const boot_roms[] = {"dmg_boot.bin", "cgb_boot.bin", "agb_boot.bin"}; + const char * const boot_roms[] = {"dmg_boot.bin", "cgb_boot.bin", "agb_boot.bin", "sgb_boot.bin"}; error = GB_load_boot_rom(&gb, resource_path(boot_roms[configuration.model])); end_capturing_logs(true, error); @@ -447,7 +455,9 @@ restart: char symbols_path[path_length + 5]; replace_extension(filename, path_length, symbols_path, ".sym"); GB_debugger_load_symbol_file(&gb, symbols_path); - + + update_viewport(); + /* Run emulation */ while (true) { if (paused || rewind_paused) { diff --git a/SDL/shader.c b/SDL/shader.c index ed45c42..37e5be7 100644 --- a/SDL/shader.c +++ b/SDL/shader.c @@ -162,20 +162,22 @@ bool init_shader_with_name(shader_t *shader, const char *name) return true; } -void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous,unsigned x, unsigned y, unsigned w, unsigned h) +void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, + unsigned source_width, unsigned source_height, + unsigned x, unsigned y, unsigned w, unsigned h) { glUseProgram(shader->program); glUniform2f(shader->origin_uniform, x, y); glUniform2f(shader->resolution_uniform, w, h); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, shader->texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 160, 144, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, source_width, source_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); glUniform1i(shader->texture_uniform, 0); glUniform1i(shader->mix_previous_uniform, previous != NULL); if (previous) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, shader->previous_texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 160, 144, 0, GL_RGBA, GL_UNSIGNED_BYTE, previous); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, source_width, source_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, previous); glUniform1i(shader->previous_texture_uniform, 1); } glBindFragDataLocation(shader->program, 0, "frag_color"); diff --git a/SDL/shader.h b/SDL/shader.h index 20baf76..3a1c304 100644 --- a/SDL/shader.h +++ b/SDL/shader.h @@ -17,7 +17,9 @@ typedef struct shader_s { } shader_t; bool init_shader_with_name(shader_t *shader, const char *name); -void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous,unsigned x, unsigned y, unsigned w, unsigned h); +void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, + unsigned source_width, unsigned source_height, + unsigned x, unsigned y, unsigned w, unsigned h); void free_shader(struct shader_s *shader); #endif /* shader_h */ diff --git a/Shaders/MasterShader.fsh b/Shaders/MasterShader.fsh index a489cf7..cd569c2 100644 --- a/Shaders/MasterShader.fsh +++ b/Shaders/MasterShader.fsh @@ -5,7 +5,6 @@ uniform bool mix_previous; uniform vec2 output_resolution; uniform vec2 origin; -const vec2 input_resolution = vec2(160, 144); #define equal(x, y) ((x) == (y)) #define inequal(x, y) ((x) != (y)) From e12e03d9c2c475288336cd1b14094603d61e84e0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 18 May 2019 20:37:41 +0300 Subject: [PATCH 016/341] SGB revision selection in the SDL port --- SDL/gui.c | 26 ++++++++++++++++++++++++++ SDL/gui.h | 8 ++++++++ SDL/main.c | 32 +++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 80f1507..c515273 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -307,6 +307,31 @@ const char *current_model_string(unsigned index) [configuration.model]; } +static void cycle_sgb_revision(unsigned index) +{ + + configuration.sgb_revision++; + if (configuration.sgb_revision == SGB_MAX) { + configuration.sgb_revision = 0; + } + pending_command = GB_SDL_RESET_COMMAND; +} + +static void cycle_sgb_revision_backwards(unsigned index) +{ + if (configuration.sgb_revision == 0) { + configuration.sgb_revision = SGB_MAX; + } + configuration.sgb_revision--; + pending_command = GB_SDL_RESET_COMMAND; +} + +const char *current_sgb_revision_string(unsigned index) +{ + return (const char *[]){"Super Game Boy NTSC", "Super Game Boy PAL", "Super Game Boy 2"} + [configuration.sgb_revision]; +} + static const uint32_t rewind_lengths[] = {0, 10, 30, 60, 60 * 2, 60 * 5, 60 * 10}; static const char *rewind_strings[] = {"Disabled", "10 Seconds", @@ -355,6 +380,7 @@ const char *current_rewind_string(unsigned index) static const struct menu_item emulation_menu[] = { {"Emulated Model:", cycle_model, current_model_string, cycle_model_backwards}, + {"SGB Revision:", cycle_sgb_revision, current_sgb_revision_string, cycle_sgb_revision_backwards}, {"Rewind Length:", cycle_rewind, current_rewind_string, cycle_rewind_backwards}, {"Back", return_to_root_menu}, {NULL,} diff --git a/SDL/gui.h b/SDL/gui.h index 9711e83..de7ddaa 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -86,6 +86,14 @@ typedef struct { SDL_Scancode keys_2[32]; /* Rewind and underclock, + padding for the future */ uint8_t joypad_configuration[32]; /* 12 Keys + padding for the future*/; uint8_t joypad_axises[JOYPAD_AXISES_MAX]; + + /* v0.12 */ + enum { + SGB_NTSC, + SGB_PAL, + SGB_2, + SGB_MAX + } sgb_revision; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 8a65a74..6506a3e 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -35,14 +35,6 @@ static char *battery_save_path_ptr; SDL_AudioDeviceID device_id; -static const GB_model_t sdl_to_internal_model[] = -{ - [MODEL_DMG] = GB_MODEL_DMG_B, - [MODEL_CGB] = GB_MODEL_CGB_E, - [MODEL_AGB] = GB_MODEL_AGB, - [MODEL_SGB] = GB_MODEL_SGB, -}; - void set_filename(const char *new_filename, bool new_should_free) { if (filename && should_free_filename) { @@ -407,13 +399,27 @@ static bool handle_pending_command(void) static void run(void) { + GB_model_t model; pending_command = GB_SDL_NO_COMMAND; restart: + model = (GB_model_t []) + { + [MODEL_DMG] = GB_MODEL_DMG_B, + [MODEL_CGB] = GB_MODEL_CGB_E, + [MODEL_AGB] = GB_MODEL_AGB, + [MODEL_SGB] = (GB_model_t []) + { + [SGB_NTSC] = GB_MODEL_SGB_NTSC, + [SGB_PAL] = GB_MODEL_SGB_PAL, + [SGB_2] = GB_MODEL_SGB2, + }[configuration.sgb_revision], + }[configuration.model]; + if (GB_is_inited(&gb)) { - GB_switch_model_and_reset(&gb, sdl_to_internal_model[configuration.model]); + GB_switch_model_and_reset(&gb, model); } else { - GB_init(&gb, sdl_to_internal_model[configuration.model]); + GB_init(&gb, model); GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_pixels_output(&gb, active_pixel_buffer); @@ -434,7 +440,11 @@ restart: bool error = false; start_capturing_logs(); const char * const boot_roms[] = {"dmg_boot.bin", "cgb_boot.bin", "agb_boot.bin", "sgb_boot.bin"}; - error = GB_load_boot_rom(&gb, resource_path(boot_roms[configuration.model])); + const char *boot_rom = boot_roms[configuration.model]; + if (configuration.model == GB_MODEL_SGB && configuration.sgb_revision == SGB_2) { + boot_rom = "sgb2_boot.bin"; + } + error = GB_load_boot_rom(&gb, resource_path(boot_rom)); end_capturing_logs(true, error); start_capturing_logs(); From c29b5b58007288366b1f2d80d34ab2215e7c75e4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 18 May 2019 20:38:10 +0300 Subject: [PATCH 017/341] Fixed the CRT shader for OpenGL frontends (SDL and older Macs) --- Shaders/CRT.fsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shaders/CRT.fsh b/Shaders/CRT.fsh index cbb1528..8684451 100644 --- a/Shaders/CRT.fsh +++ b/Shaders/CRT.fsh @@ -55,7 +55,7 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou right *= scanline_multiplier; /* Vertical seperator for shadow masks */ - bool odd = (int)(position * input_resolution).x & 1; + bool odd = bool(int((position * input_resolution).x) & 1); if (odd) { pos.y += 0.5; pos.y = fract(pos.y); From 85c43fa81f3c8d3258475da30632918d6ad29d9f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 May 2019 19:12:09 +0300 Subject: [PATCH 018/341] =?UTF-8?q?Fixed=20Channel=203=E2=80=99s=20first?= =?UTF-8?q?=20sample=20behavior,=20update=20analog=20characteristic=20to?= =?UTF-8?q?=20more=20realistic=20values.=20Fixes=20#177?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 24 +++++++++++++----------- Core/apu.h | 8 ++------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index af90d81..b4f4d75 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -107,6 +107,11 @@ static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsign } } +static double smooth(double x) +{ + return 3*x*x - 2*x*x*x; +} + static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) { GB_sample_t output = {0,0}; @@ -123,7 +128,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) gb->apu_output.dac_discharge[i] = 0; } else { - multiplier *= gb->apu_output.dac_discharge[i]; + multiplier *= smooth(gb->apu_output.dac_discharge[i]); } } else { @@ -132,7 +137,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) gb->apu_output.dac_discharge[i] = 1; } else { - multiplier *= gb->apu_output.dac_discharge[i]; + multiplier *= smooth(gb->apu_output.dac_discharge[i]); } } } @@ -350,7 +355,6 @@ void GB_apu_div_event(GB_gameboy_t *gb) if (gb->apu.wave_channel.pulse_length) { if (!--gb->apu.wave_channel.pulse_length) { gb->apu.is_active[GB_WAVE] = false; - gb->apu.wave_channel.current_sample = 0; update_sample(gb, GB_WAVE, 0, 0); } } @@ -806,7 +810,6 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.wave_channel.enable = value & 0x80; if (!gb->apu.wave_channel.enable) { gb->apu.is_active[GB_WAVE] = false; - gb->apu.wave_channel.current_sample = 0; update_sample(gb, GB_WAVE, 0, 0); } break; @@ -815,7 +818,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) break; case GB_IO_NR32: gb->apu.wave_channel.shift = (uint8_t[]){4, 0, 1, 2}[(value >> 5) & 3]; - update_sample(gb, GB_WAVE, gb->apu.wave_channel.current_sample >> gb->apu.wave_channel.shift, 0); + if (gb->apu.is_active[GB_WAVE]) { + update_sample(gb, GB_WAVE, gb->apu.wave_channel.current_sample >> gb->apu.wave_channel.shift, 0); + } break; case GB_IO_NR33: gb->apu.wave_channel.sample_length &= ~0xFF; @@ -856,7 +861,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } if (!gb->apu.is_active[GB_WAVE]) { gb->apu.is_active[GB_WAVE] = true; - update_sample(gb, GB_WAVE, 0, 0); + update_sample(gb, GB_WAVE, + gb->apu.wave_channel.current_sample >> gb->apu.wave_channel.shift, + 0); } gb->apu.wave_channel.sample_countdown = (gb->apu.wave_channel.sample_length ^ 0x7FF) + 3; gb->apu.wave_channel.current_sample_index = 0; @@ -865,11 +872,6 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.wave_channel.length_enabled = false; } /* Note that we don't change the sample just yet! This was verified on hardware. */ - /* Todo: The first sample might *not* beskipped on the DMG, this could be a bug - introduced on the CGB. It appears that the bug was fixed on the AGB, but it's - not reflected by PCM34. This should be probably verified as this could just - mean differences in the DACs. */ - /* Todo: Similar issues may apply to the other channels on the DMG/AGB, test, verify and fix if needed */ } /* APU glitch - if length is enabled while the DIV-divider's LSB is 1, tick the length once. */ diff --git a/Core/apu.h b/Core/apu.h index 56a7369..2a49c04 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -8,12 +8,8 @@ #ifdef GB_INTERNAL /* Speed = 1 / Length (in seconds) */ -/* Todo: Measure these and find the actual curve shapes. - They are known to be incorrect (Some analog test ROM sound different), - but are good enough approximations to fix Cannon Fodder's terrible audio. - It also varies by model. */ -#define DAC_DECAY_SPEED 50000 -#define DAC_ATTACK_SPEED 1000 +#define DAC_DECAY_SPEED 20000 +#define DAC_ATTACK_SPEED 20000 /* Divides nicely and never overflows with 4 channels and 8 (1-8) volume levels */ From 54c353830fa3528a8a3b12667a744ebdaf5f0763 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 31 May 2019 18:33:51 +0300 Subject: [PATCH 019/341] SDL GUI mouse support --- SDL/gui.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index c515273..bad36f2 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -25,7 +25,8 @@ unsigned command_parameter; #endif shader_t shader; -SDL_Rect rect; +static SDL_Rect rect; +static unsigned factor; void render_texture(void *pixels, void *previous) { @@ -118,6 +119,10 @@ void update_viewport(void) { int win_width, win_height; SDL_GL_GetDrawableSize(window, &win_width, &win_height); + int logical_width, logical_height; + SDL_GetWindowSize(window, &logical_width, &logical_height); + factor = win_width / logical_width; + double x_factor = win_width / (double) GB_get_screen_width(&gb); double y_factor = win_height / (double) GB_get_screen_height(&gb); @@ -810,9 +815,60 @@ void run_gui(bool is_running) current_menu = root_menu = is_running? paused_menu : nonpaused_menu; current_selection = 0; do { - /* Convert Joypad events (We only generate down events) */ + /* Convert Joypad and mouse events (We only generate down events) */ if (gui_state != WAITING_FOR_KEY && gui_state != WAITING_FOR_JBUTTON) { switch (event.type) { + case SDL_MOUSEBUTTONDOWN: + if (gui_state == SHOWING_HELP) { + event.type = SDL_KEYDOWN; + event.key.keysym.scancode = SDL_SCANCODE_RETURN; + } + else if (gui_state == SHOWING_DROP_MESSAGE) { + event.type = SDL_KEYDOWN; + event.key.keysym.scancode = SDL_SCANCODE_ESCAPE; + } + else if (gui_state == SHOWING_MENU) { + signed x = (event.button.x - rect.x / factor) * 160 / (rect.w / factor) - x_offset; + signed y = (event.button.y - rect.y / factor) * 144 / (rect.h / factor) - y_offset; + + if (strcmp("CRT", configuration.filter) == 0) { + y = y * 8 / 7; + y -= 144 / 16; + } + + if (x < 0 || x >= 160 || y < 24) { + continue; + } + + unsigned item_y = 24; + unsigned index = 0; + for (const struct menu_item *item = current_menu; item->string; item++, index++) { + if (!item->backwards_handler) { + if (y >= item_y && y < item_y + 12) { + break; + } + item_y += 12; + } + else { + if (y >= item_y && y < item_y + 24) { + break; + } + item_y += 24; + } + } + + if (!current_menu[index].string) continue; + + current_selection = index; + event.type = SDL_KEYDOWN; + if (current_menu[index].backwards_handler) { + event.key.keysym.scancode = x < 80? SDL_SCANCODE_LEFT : SDL_SCANCODE_RIGHT; + } + else { + event.key.keysym.scancode = SDL_SCANCODE_RETURN; + } + + } case SDL_JOYBUTTONDOWN: event.type = SDL_KEYDOWN; joypad_button_t button = get_joypad_button(event.jbutton.button); From f9cc7a3b4662a5a8b4eff848aaec33d4c1786af0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 31 May 2019 18:50:02 +0300 Subject: [PATCH 020/341] Fix SDL mouse support in SGB mode --- SDL/gui.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index bad36f2..39d7357 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -828,8 +828,8 @@ void run_gui(bool is_running) event.key.keysym.scancode = SDL_SCANCODE_ESCAPE; } else if (gui_state == SHOWING_MENU) { - signed x = (event.button.x - rect.x / factor) * 160 / (rect.w / factor) - x_offset; - signed y = (event.button.y - rect.y / factor) * 144 / (rect.h / factor) - y_offset; + signed x = (event.button.x - rect.x / factor) * width / (rect.w / factor) - x_offset; + signed y = (event.button.y - rect.y / factor) * height / (rect.h / factor) - y_offset; if (strcmp("CRT", configuration.filter) == 0) { y = y * 8 / 7; From cdc36f329e5867fa59a99eb7658c9d0bb3c7ff7c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 1 Jun 2019 14:29:46 +0300 Subject: [PATCH 021/341] Added open dialog to the SDL GUI, misc fixes --- BootROMs/sgb_boot.asm | 16 +++---- Core/sm83_cpu.c | 2 +- Makefile | 19 ++++++-- OpenDialog/cocoa.m | 20 +++++++++ OpenDialog/gtk.c | 96 ++++++++++++++++++++++++++++++++++++++++ OpenDialog/open_dialog.h | 6 +++ OpenDialog/windows.c | 28 ++++++++++++ SDL/gui.c | 34 +++++++++++--- SDL/gui.h | 6 +++ SDL/main.c | 29 +++++++----- 10 files changed, 227 insertions(+), 29 deletions(-) create mode 100644 OpenDialog/cocoa.m create mode 100644 OpenDialog/gtk.c create mode 100644 OpenDialog/open_dialog.h create mode 100644 OpenDialog/windows.c diff --git a/BootROMs/sgb_boot.asm b/BootROMs/sgb_boot.asm index 0574d29..108af18 100644 --- a/BootROMs/sgb_boot.asm +++ b/BootROMs/sgb_boot.asm @@ -82,9 +82,9 @@ Start: .sendCommand xor a - ldh [c], a + ld [c], a ld a, $30 - ldh [c], a + ld [c], a ldh a, [$80] call SendByte @@ -112,9 +112,9 @@ Start: ; Done bit ld a, $20 - ldh [c], a + ld [c], a ld a, $30 - ldh [c], a + ld [c], a ; Update command ldh a, [$80] @@ -128,10 +128,10 @@ Start: ; Write to sound registers for DMG compatibility ld c, $13 ld a, $c1 - ldh [c], a + ld [c], a inc c ld a, 7 - ldh [c], a + ld [c], a ; Init BG palette ld a, $fc @@ -168,9 +168,9 @@ SendByte: jr c, .zeroBit add a ; 10 -> 20 .zeroBit - ldh [c], a + ld [c], a ld a, $30 - ldh [c], a + ld [c], a dec d ret z jr .loop diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index fdf357c..1416000 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -606,7 +606,7 @@ static void ld_dhl_d8(GB_gameboy_t *gb, uint8_t opcode) cycle_write(gb, gb->registers[GB_REGISTER_HL], data); } -uint8_t get_src_value(GB_gameboy_t *gb, uint8_t opcode) +static uint8_t get_src_value(GB_gameboy_t *gb, uint8_t opcode) { uint8_t src_register_id; uint8_t src_low; diff --git a/Makefile b/Makefile index a2df8bf..38b3e83 100644 --- a/Makefile +++ b/Makefile @@ -65,14 +65,25 @@ endif # Set compilation and linkage flags based on target, platform and configuration +OPEN_DIALOG = OpenDialog/gtk.c + +ifeq ($(PLATFORM),windows32) +OPEN_DIALOG = OpenDialog/windows.c +endif + +ifeq ($(PLATFORM),Darwin) +OPEN_DIALOG = OpenDialog/cocoa.m +endif + + CFLAGS += -Werror -Wall -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES SDL_LDFLAGS := -lSDL2 -lGL ifeq ($(PLATFORM),windows32) -CFLAGS += -IWindows -LDFLAGS += -lmsvcrt -lSDL2main -Wl,/MANIFESTFILE:NUL +CFLAGS += -IWindows -Drandom=rand +LDFLAGS += -lmsvcrt -lcomdlg32 -lSDL2main -Wl,/MANIFESTFILE:NUL SDL_LDFLAGS := -lSDL2 -lopengl32 else -LDFLAGS += -lc -lm +LDFLAGS += -lc -lm -ldl endif ifeq ($(PLATFORM),Darwin) @@ -120,7 +131,7 @@ all: cocoa sdl tester libretro # Get a list of our source files and their respective object file targets CORE_SOURCES := $(shell ls Core/*.c) -SDL_SOURCES := $(shell ls SDL/*.c) +SDL_SOURCES := $(shell ls SDL/*.c) $(OPEN_DIALOG) TESTER_SOURCES := $(shell ls Tester/*.c) ifeq ($(PLATFORM),Darwin) diff --git a/OpenDialog/cocoa.m b/OpenDialog/cocoa.m new file mode 100644 index 0000000..29a722c --- /dev/null +++ b/OpenDialog/cocoa.m @@ -0,0 +1,20 @@ +#import +#include "open_dialog.h" + + +char *do_open_rom_dialog(void) +{ + @autoreleasepool { + NSWindow *key = [NSApp keyWindow]; + NSOpenPanel *dialog = [NSOpenPanel openPanel]; + dialog.title = @"Open ROM"; + dialog.allowedFileTypes = @[@"gb", @"gbc", @"sgb"]; + [dialog runModal]; + [key makeKeyAndOrderFront:nil]; + NSString *ret = [[[dialog URLs] firstObject] path]; + if (ret) { + return strdup(ret.UTF8String); + } + return NULL; + } +} diff --git a/OpenDialog/gtk.c b/OpenDialog/gtk.c new file mode 100644 index 0000000..2c3bfe7 --- /dev/null +++ b/OpenDialog/gtk.c @@ -0,0 +1,96 @@ +#include "open_dialog.h" +#include +#include +#include +#include +#include + +#define GTK_FILE_CHOOSER_ACTION_OPEN 0 + + +void *_gtk_file_chooser_dialog_new (const char *title, + void *parent, + int action, + const char *first_button_text, + ...); +bool _gtk_init_check (int *argc, char ***argv); +int _gtk_dialog_run(void *); +void _g_free(void *); +void _g_object_unref(void *); +char *_gtk_file_chooser_get_filename(void *); +void _g_log_set_default_handler (void *function, void *data); +void *_gtk_file_filter_new(void); +void _gtk_file_filter_add_pattern(void *filter, const char *pattern); +void _gtk_file_filter_set_name(void *filter, const char *name); +void _gtk_file_chooser_add_filter(void *dialog, void *filter); + +#define LAZY(symbol) static typeof(_##symbol) *symbol = NULL;\ +if (symbol == NULL) symbol = dlsym(handle, #symbol);\ +if (symbol == NULL) goto lazy_error +#define TRY_DLOPEN(name) handle = handle? handle : dlopen(name, RTLD_NOW) + +void nop(){} + +char *do_open_rom_dialog(void) +{ + static void *handle = NULL; + + TRY_DLOPEN("libgtk-3.so"); + TRY_DLOPEN("libgtk-3.so.0"); + TRY_DLOPEN("libgtk-2.so"); + TRY_DLOPEN("libgtk-2.so.0"); + + if (!handle) { + goto lazy_error; + } + + + LAZY(gtk_init_check); + LAZY(gtk_file_chooser_dialog_new); + LAZY(gtk_dialog_run); + LAZY(g_free); + LAZY(g_object_unref); + LAZY(gtk_file_chooser_get_filename); + LAZY(g_log_set_default_handler); + LAZY(gtk_file_filter_new); + LAZY(gtk_file_filter_add_pattern); + LAZY(gtk_file_filter_set_name); + LAZY(gtk_file_chooser_add_filter); + + /* Shut up GTK */ + g_log_set_default_handler(nop, NULL); + + gtk_init_check(0, 0); + + + void *dialog = gtk_file_chooser_dialog_new("Open ROM", + 0, + GTK_FILE_CHOOSER_ACTION_OPEN, + "Open", 0, NULL); + + + void *filter = gtk_file_filter_new(); + gtk_file_filter_add_pattern(filter, "*.gb"); + gtk_file_filter_add_pattern(filter, "*.gbc"); + gtk_file_filter_add_pattern(filter, "*.sgb"); + gtk_file_filter_set_name(filter, "Game Boy ROMs"); + gtk_file_chooser_add_filter(dialog, filter); + + int res = gtk_dialog_run (dialog); + char *ret = NULL; + + if (res == 0) + { + char *filename; + filename = gtk_file_chooser_get_filename(dialog); + ret = strdup(filename); + g_free(filename); + } + + g_object_unref(dialog); + return ret; + +lazy_error: + fprintf(stderr, "Failed to display GTK dialog\n"); + return NULL; +} diff --git a/OpenDialog/open_dialog.h b/OpenDialog/open_dialog.h new file mode 100644 index 0000000..85e5721 --- /dev/null +++ b/OpenDialog/open_dialog.h @@ -0,0 +1,6 @@ +#ifndef open_rom_h +#define open_rom_h + +char *do_open_rom_dialog(void); + +#endif /* open_rom_h */ diff --git a/OpenDialog/windows.c b/OpenDialog/windows.c new file mode 100644 index 0000000..75fe767 --- /dev/null +++ b/OpenDialog/windows.c @@ -0,0 +1,28 @@ +#include +#include "open_dialog.h" + +char *do_open_rom_dialog(void) +{ + OPENFILENAMEW dialog; + wchar_t filename[MAX_PATH] = {0}; + + memset(&dialog, 0, sizeof(dialog)); + dialog.lStructSize = sizeof(dialog); + dialog.lpstrFile = filename; + dialog.nMaxFile = sizeof(filename); + dialog.lpstrFilter = L"Game Boy ROMs\0*.gb;*.gbc;*.sgb\0All files\0*.*\0\0"; + dialog.nFilterIndex = 1; + dialog.lpstrFileTitle = NULL; + dialog.nMaxFileTitle = 0; + dialog.lpstrInitialDir = NULL; + dialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; + + if (GetOpenFileNameW(&dialog) == TRUE) + { + char *ret = malloc(MAX_PATH * 4); + WideCharToMultiByte(CP_UTF8, 0, filename, sizeof(filename), ret, MAX_PATH * 4, NULL, NULL); + return ret; + } + + return NULL; +} diff --git a/SDL/gui.c b/SDL/gui.c index 39d7357..7716760 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -97,11 +98,11 @@ configuration_t configuration = static const char *help[] ={ -"Drop a GB or GBC ROM\n" -"file to play.\n" +"Drop a ROM to play.\n" "\n" "Keyboard Shortcuts:\n" " Open Menu: Escape\n" +" Open ROM: " MODIFIER_NAME "+O\n" " Reset: " MODIFIER_NAME "+R\n" " Pause: " MODIFIER_NAME "+P\n" " Save state: " MODIFIER_NAME "+(0-9)\n" @@ -267,8 +268,19 @@ static void enter_controls_menu(unsigned index); static void enter_joypad_menu(unsigned index); static void enter_audio_menu(unsigned index); +extern void set_filename(const char *new_filename, typeof(free) *new_free_function); +static void open_rom(unsigned index) +{ + char *filename = do_open_rom_dialog(); + if (filename) { + set_filename(filename, free); + pending_command = GB_SDL_NEW_FILE_COMMAND; + } +} + static const struct menu_item paused_menu[] = { {"Resume", NULL}, + {"Open ROM", open_rom}, {"Emulation Options", enter_emulation_menu}, {"Graphic Options", enter_graphics_menu}, {"Audio Options", enter_audio_menu}, @@ -778,7 +790,6 @@ void connect_joypad(void) } } -extern void set_filename(const char *new_filename, bool new_should_free); void run_gui(bool is_running) { connect_joypad(); @@ -818,6 +829,9 @@ void run_gui(bool is_running) /* Convert Joypad and mouse events (We only generate down events) */ if (gui_state != WAITING_FOR_KEY && gui_state != WAITING_FOR_JBUTTON) { switch (event.type) { + case SDL_WINDOWEVENT: + should_render = true; + break; case SDL_MOUSEBUTTONDOWN: if (gui_state == SHOWING_HELP) { event.type = SDL_KEYDOWN; @@ -957,7 +971,7 @@ void run_gui(bool is_running) break; } case SDL_DROPFILE: { - set_filename(event.drop.file, true); + set_filename(event.drop.file, SDL_free); pending_command = GB_SDL_NEW_FILE_COMMAND; return; } @@ -995,7 +1009,17 @@ void run_gui(bool is_running) } case SDL_KEYDOWN: - if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && gui_state == WAITING_FOR_JBUTTON) { + if (event.key.keysym.scancode == SDL_SCANCODE_O) { + if (event.key.keysym.mod & MODIFIER) { + char *filename = do_open_rom_dialog(); + if (filename) { + set_filename(filename, free); + pending_command = GB_SDL_NEW_FILE_COMMAND; + return; + } + } + } + else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && gui_state == WAITING_FOR_JBUTTON) { should_render = true; if (joypad_configuration_progress != JOYPAD_BUTTONS_MAX) { configuration.joypad_configuration[joypad_configuration_progress] = -1; diff --git a/SDL/gui.h b/SDL/gui.h index de7ddaa..6974849 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -9,6 +9,12 @@ #define JOYSTICK_HIGH 0x4000 #define JOYSTICK_LOW 0x3800 +#ifdef __APPLE__ +#define MODIFIER KMOD_GUI +#else +#define MODIFIER KMOD_CTRL +#endif + extern GB_gameboy_t gb; extern SDL_Window *window; diff --git a/SDL/main.c b/SDL/main.c index 6506a3e..451ac9c 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include "utils.h" @@ -30,18 +31,18 @@ static bool underclock_down = false, rewind_down = false, do_rewind = false, rew static double clock_mutliplier = 1.0; static char *filename = NULL; -static bool should_free_filename = false; +static typeof(free) *free_function = NULL; static char *battery_save_path_ptr; SDL_AudioDeviceID device_id; -void set_filename(const char *new_filename, bool new_should_free) +void set_filename(const char *new_filename, typeof(free) *new_free_function) { - if (filename && should_free_filename) { - SDL_free(filename); + if (filename && free_function) { + free_function(filename); } filename = (char *) new_filename; - should_free_filename = new_should_free; + free_function = new_free_function; } static SDL_AudioSpec want_aspec, have_aspec; @@ -101,11 +102,6 @@ static void open_menu(void) static void handle_events(GB_gameboy_t *gb) { -#ifdef __APPLE__ -#define MODIFIER KMOD_GUI -#else -#define MODIFIER KMOD_CTRL -#endif SDL_Event event; while (SDL_PollEvent(&event)) { @@ -115,7 +111,7 @@ static void handle_events(GB_gameboy_t *gb) break; case SDL_DROPFILE: { - set_filename(event.drop.file, true); + set_filename(event.drop.file, SDL_free); pending_command = GB_SDL_NEW_FILE_COMMAND; break; } @@ -226,6 +222,17 @@ static void handle_events(GB_gameboy_t *gb) pending_command = GB_SDL_RESET_COMMAND; } break; + + case SDL_SCANCODE_O: { + if (event.key.keysym.mod & MODIFIER) { + char *filename = do_open_rom_dialog(); + if (filename) { + set_filename(filename, free); + pending_command = GB_SDL_NEW_FILE_COMMAND; + } + } + break; + } case SDL_SCANCODE_P: if (event.key.keysym.mod & MODIFIER) { From 9acb4636db0f6fc6ba9a778073b601a83ed000d7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 1 Jun 2019 16:19:44 +0300 Subject: [PATCH 022/341] Fix various GTK bugs --- OpenDialog/gtk.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/OpenDialog/gtk.c b/OpenDialog/gtk.c index 2c3bfe7..7947ea2 100644 --- a/OpenDialog/gtk.c +++ b/OpenDialog/gtk.c @@ -6,6 +6,8 @@ #include #define GTK_FILE_CHOOSER_ACTION_OPEN 0 +#define GTK_RESPONSE_ACCEPT -3 +#define GTK_RESPONSE_CANCEL -6 void *_gtk_file_chooser_dialog_new (const char *title, @@ -16,13 +18,16 @@ void *_gtk_file_chooser_dialog_new (const char *title, bool _gtk_init_check (int *argc, char ***argv); int _gtk_dialog_run(void *); void _g_free(void *); -void _g_object_unref(void *); +void _gtk_widget_destroy(void *); char *_gtk_file_chooser_get_filename(void *); void _g_log_set_default_handler (void *function, void *data); void *_gtk_file_filter_new(void); void _gtk_file_filter_add_pattern(void *filter, const char *pattern); void _gtk_file_filter_set_name(void *filter, const char *name); void _gtk_file_chooser_add_filter(void *dialog, void *filter); +void _gtk_main_iteration(void); +bool _gtk_events_pending(void); + #define LAZY(symbol) static typeof(_##symbol) *symbol = NULL;\ if (symbol == NULL) symbol = dlsym(handle, #symbol);\ @@ -49,13 +54,15 @@ char *do_open_rom_dialog(void) LAZY(gtk_file_chooser_dialog_new); LAZY(gtk_dialog_run); LAZY(g_free); - LAZY(g_object_unref); + LAZY(gtk_widget_destroy); LAZY(gtk_file_chooser_get_filename); LAZY(g_log_set_default_handler); LAZY(gtk_file_filter_new); LAZY(gtk_file_filter_add_pattern); LAZY(gtk_file_filter_set_name); LAZY(gtk_file_chooser_add_filter); + LAZY(gtk_events_pending); + LAZY(gtk_main_iteration); /* Shut up GTK */ g_log_set_default_handler(nop, NULL); @@ -66,7 +73,9 @@ char *do_open_rom_dialog(void) void *dialog = gtk_file_chooser_dialog_new("Open ROM", 0, GTK_FILE_CHOOSER_ACTION_OPEN, - "Open", 0, NULL); + "_Cancel", GTK_RESPONSE_CANCEL, + "_Open", GTK_RESPONSE_ACCEPT, + NULL ); void *filter = gtk_file_filter_new(); @@ -79,15 +88,23 @@ char *do_open_rom_dialog(void) int res = gtk_dialog_run (dialog); char *ret = NULL; - if (res == 0) + if (res == GTK_RESPONSE_ACCEPT) { char *filename; filename = gtk_file_chooser_get_filename(dialog); ret = strdup(filename); g_free(filename); } - - g_object_unref(dialog); + + while (gtk_events_pending()) { + gtk_main_iteration(); + } + + gtk_widget_destroy(dialog); + + while (gtk_events_pending()) { + gtk_main_iteration(); + } return ret; lazy_error: From 6888047102d86fc1fcf10503fc9bf921ad455cfc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 1 Jun 2019 16:42:17 +0300 Subject: [PATCH 023/341] Show flags in the registers command --- Core/debugger.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 79321a5..ba413ca 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -811,7 +811,12 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const return true; } - GB_log(gb, "AF = $%04x\n", gb->registers[GB_REGISTER_AF]); /* AF can't really be an address */ + + GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */ + (gb->f & GB_CARRY_FLAG)? 'C' : '-', + (gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-', + (gb->f & GB_SUBSTRACT_FLAG)? 'S' : '-', + (gb->f & GB_ZERO_FLAG)? 'Z' : '-'); GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false)); GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false)); GB_log(gb, "HL = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_HL], false)); From 4c34e0a6e063ead12d89bb7e085e79d04702ba57 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 1 Jun 2019 18:22:58 +0300 Subject: [PATCH 024/341] =?UTF-8?q?Turns=20out=20the=20AGB=20inverts=20Cha?= =?UTF-8?q?nnel=203=E2=80=99s=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Core/apu.c b/Core/apu.c index b4f4d75..d5537a6 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -59,6 +59,11 @@ static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsign unsigned right_volume = (gb->io_registers[GB_IO_NR50] & 7) + 1; unsigned left_volume = ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1; + if (index == GB_WAVE) { + /* For some reason, channel 3 is inverted on the AGB */ + value ^= 0xF; + } + GB_sample_t output; if (gb->io_registers[GB_IO_NR51] & (1 << index)) { output.right = (0xf - value * 2) * right_volume; From 64879f5b02b3b583e9da7c8c5f84ca1794644b86 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 13:53:50 +0300 Subject: [PATCH 025/341] Accurate emulation of (most aspects of) stop mode --- Core/apu.c | 192 ++++++++++++++++++++++++------------------------ Core/display.c | 15 +++- Core/gb.h | 1 + Core/joypad.c | 1 - Core/sm83_cpu.c | 20 ++++- Core/timing.c | 12 ++- 6 files changed, 136 insertions(+), 105 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index d5537a6..7a54c43 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -406,106 +406,108 @@ void GB_apu_run(GB_gameboy_t *gb) uint8_t cycles = gb->apu.apu_cycles >> 2; gb->apu.apu_cycles = 0; if (!cycles) return; + + if (likely(!gb->stopped || GB_is_cgb(gb))) { + /* To align the square signal to 1MHz */ + gb->apu.lf_div ^= cycles & 1; + gb->apu.noise_channel.alignment += cycles; - /* To align the square signal to 1MHz */ - gb->apu.lf_div ^= cycles & 1; - gb->apu.noise_channel.alignment += cycles; - - if (gb->apu.square_sweep_calculate_countdown) { - if (gb->apu.square_sweep_calculate_countdown > cycles) { - gb->apu.square_sweep_calculate_countdown -= cycles; - } - else { - /* APU bug: sweep frequency is checked after adding the sweep delta twice */ - gb->apu.new_sweep_sample_legnth = new_sweep_sample_legnth(gb); - if (gb->apu.new_sweep_sample_legnth > 0x7ff) { - gb->apu.is_active[GB_SQUARE_1] = false; - update_sample(gb, GB_SQUARE_1, 0, gb->apu.square_sweep_calculate_countdown - cycles); - gb->apu.sweep_enabled = false; - } - gb->apu.sweep_decreasing |= gb->io_registers[GB_IO_NR10] & 8; - gb->apu.square_sweep_calculate_countdown = 0; - } - } - - UNROLL - for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { - if (gb->apu.is_active[i]) { - uint8_t cycles_left = cycles; - while (unlikely(cycles_left > gb->apu.square_channels[i].sample_countdown)) { - cycles_left -= gb->apu.square_channels[i].sample_countdown + 1; - gb->apu.square_channels[i].sample_countdown = (gb->apu.square_channels[i].sample_length ^ 0x7FF) * 2 + 1; - gb->apu.square_channels[i].current_sample_index++; - gb->apu.square_channels[i].current_sample_index &= 0x7; - - update_square_sample(gb, i); - } - if (cycles_left) { - gb->apu.square_channels[i].sample_countdown -= cycles_left; - } - } - } - - gb->apu.wave_channel.wave_form_just_read = false; - if (gb->apu.is_active[GB_WAVE]) { - uint8_t cycles_left = cycles; - while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) { - cycles_left -= gb->apu.wave_channel.sample_countdown + 1; - gb->apu.wave_channel.sample_countdown = gb->apu.wave_channel.sample_length ^ 0x7FF; - gb->apu.wave_channel.current_sample_index++; - gb->apu.wave_channel.current_sample_index &= 0x1F; - gb->apu.wave_channel.current_sample = - gb->apu.wave_channel.wave_form[gb->apu.wave_channel.current_sample_index]; - update_sample(gb, GB_WAVE, - gb->apu.wave_channel.current_sample >> gb->apu.wave_channel.shift, - cycles - cycles_left); - gb->apu.wave_channel.wave_form_just_read = true; - } - if (cycles_left) { - gb->apu.wave_channel.sample_countdown -= cycles_left; - gb->apu.wave_channel.wave_form_just_read = false; - } - } - - if (gb->apu.is_active[GB_NOISE]) { - uint8_t cycles_left = cycles; - while (unlikely(cycles_left > gb->apu.noise_channel.sample_countdown)) { - cycles_left -= gb->apu.noise_channel.sample_countdown + 1; - gb->apu.noise_channel.sample_countdown = gb->apu.noise_channel.sample_length * 4 + 3; - - /* Step LFSR */ - unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; - /* Todo: is this formula is different on a GBA? */ - bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1; - gb->apu.noise_channel.lfsr >>= 1; - - if (new_high_bit) { - gb->apu.noise_channel.lfsr |= high_bit_mask; + if (gb->apu.square_sweep_calculate_countdown) { + if (gb->apu.square_sweep_calculate_countdown > cycles) { + gb->apu.square_sweep_calculate_countdown -= cycles; } else { - /* This code is not redundent, it's relevant when switching LFSR widths */ - gb->apu.noise_channel.lfsr &= ~high_bit_mask; + /* APU bug: sweep frequency is checked after adding the sweep delta twice */ + gb->apu.new_sweep_sample_legnth = new_sweep_sample_legnth(gb); + if (gb->apu.new_sweep_sample_legnth > 0x7ff) { + gb->apu.is_active[GB_SQUARE_1] = false; + update_sample(gb, GB_SQUARE_1, 0, gb->apu.square_sweep_calculate_countdown - cycles); + gb->apu.sweep_enabled = false; + } + gb->apu.sweep_decreasing |= gb->io_registers[GB_IO_NR10] & 8; + gb->apu.square_sweep_calculate_countdown = 0; } - - gb->apu.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1; - if (gb->model == GB_MODEL_CGB_C) { - /* Todo: This was confirmed to happen on a CGB-C. This may or may not happen on pre-CGB models. - Because this degrades audio quality, and testing this on a pre-CGB device requires audio records, - I'll assume these devices are innocent until proven guilty. - - Also happens on CGB-B, but not on CGB-D. - */ - gb->apu.current_lfsr_sample &= gb->apu.previous_lfsr_sample; - } - gb->apu.previous_lfsr_sample = gb->apu.noise_channel.lfsr & 1; - - update_sample(gb, GB_NOISE, - gb->apu.current_lfsr_sample ? - gb->apu.noise_channel.current_volume : 0, - 0); } - if (cycles_left) { - gb->apu.noise_channel.sample_countdown -= cycles_left; + + UNROLL + for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { + if (gb->apu.is_active[i]) { + uint8_t cycles_left = cycles; + while (unlikely(cycles_left > gb->apu.square_channels[i].sample_countdown)) { + cycles_left -= gb->apu.square_channels[i].sample_countdown + 1; + gb->apu.square_channels[i].sample_countdown = (gb->apu.square_channels[i].sample_length ^ 0x7FF) * 2 + 1; + gb->apu.square_channels[i].current_sample_index++; + gb->apu.square_channels[i].current_sample_index &= 0x7; + + update_square_sample(gb, i); + } + if (cycles_left) { + gb->apu.square_channels[i].sample_countdown -= cycles_left; + } + } + } + + gb->apu.wave_channel.wave_form_just_read = false; + if (gb->apu.is_active[GB_WAVE]) { + uint8_t cycles_left = cycles; + while (unlikely(cycles_left > gb->apu.wave_channel.sample_countdown)) { + cycles_left -= gb->apu.wave_channel.sample_countdown + 1; + gb->apu.wave_channel.sample_countdown = gb->apu.wave_channel.sample_length ^ 0x7FF; + gb->apu.wave_channel.current_sample_index++; + gb->apu.wave_channel.current_sample_index &= 0x1F; + gb->apu.wave_channel.current_sample = + gb->apu.wave_channel.wave_form[gb->apu.wave_channel.current_sample_index]; + update_sample(gb, GB_WAVE, + gb->apu.wave_channel.current_sample >> gb->apu.wave_channel.shift, + cycles - cycles_left); + gb->apu.wave_channel.wave_form_just_read = true; + } + if (cycles_left) { + gb->apu.wave_channel.sample_countdown -= cycles_left; + gb->apu.wave_channel.wave_form_just_read = false; + } + } + + if (gb->apu.is_active[GB_NOISE]) { + uint8_t cycles_left = cycles; + while (unlikely(cycles_left > gb->apu.noise_channel.sample_countdown)) { + cycles_left -= gb->apu.noise_channel.sample_countdown + 1; + gb->apu.noise_channel.sample_countdown = gb->apu.noise_channel.sample_length * 4 + 3; + + /* Step LFSR */ + unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; + /* Todo: is this formula is different on a GBA? */ + bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1; + gb->apu.noise_channel.lfsr >>= 1; + + if (new_high_bit) { + gb->apu.noise_channel.lfsr |= high_bit_mask; + } + else { + /* This code is not redundent, it's relevant when switching LFSR widths */ + gb->apu.noise_channel.lfsr &= ~high_bit_mask; + } + + gb->apu.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1; + if (gb->model == GB_MODEL_CGB_C) { + /* Todo: This was confirmed to happen on a CGB-C. This may or may not happen on pre-CGB models. + Because this degrades audio quality, and testing this on a pre-CGB device requires audio records, + I'll assume these devices are innocent until proven guilty. + + Also happens on CGB-B, but not on CGB-D. + */ + gb->apu.current_lfsr_sample &= gb->apu.previous_lfsr_sample; + } + gb->apu.previous_lfsr_sample = gb->apu.noise_channel.lfsr & 1; + + update_sample(gb, GB_NOISE, + gb->apu.current_lfsr_sample ? + gb->apu.noise_channel.current_volume : 0, + 0); + } + if (cycles_left) { + gb->apu.noise_channel.sample_countdown -= cycles_left; + } } } diff --git a/Core/display.c b/Core/display.c index 22f6e8d..c84b3a9 100644 --- a/Core/display.c +++ b/Core/display.c @@ -137,13 +137,13 @@ static void display_vblank(GB_gameboy_t *gb) if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ if (gb->sgb) { - uint8_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? 0x3 : 0x0; + uint8_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped && GB_is_cgb(gb) ? 0x3 : 0x0; for (unsigned i = 0; i < WIDTH * LINES; i++) { gb->sgb->screen_buffer[i] = color; } } else { - uint32_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? + uint32_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped && GB_is_cgb(gb) ? gb->rgb_encode_callback(gb, 0, 0, 0) : gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); for (unsigned i = 0; i < WIDTH * LINES; i++) { @@ -555,6 +555,15 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) */ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) { + /* The PPU does not advance while in STOP mode on the DMG */ + if (gb->stopped && !GB_is_cgb(gb)) { + gb->cycles_in_stop_mode += cycles; + if (gb->cycles_in_stop_mode >= LCDC_PERIOD) { + gb->cycles_in_stop_mode -= LCDC_PERIOD; + display_vblank(gb); + } + return; + } GB_object_t *objects = (GB_object_t *) &gb->oam; GB_STATE_MACHINE(gb, display, cycles, 2) { @@ -696,7 +705,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) for (gb->oam_search_index = 0; gb->oam_search_index < 40; gb->oam_search_index++) { if (GB_is_cgb(gb)) { add_object_from_index(gb, gb->oam_search_index); - /* The CGB does not care about the accessed OAM row as there's no OAM bug*/ + /* The CGB does not care about the accessed OAM row as there's no OAM bug */ } GB_SLEEP(gb, display, 8, 2); if (!GB_is_cgb(gb)) { diff --git a/Core/gb.h b/Core/gb.h index 30654d5..f429b45 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -478,6 +478,7 @@ struct GB_gameboy_internal_s { bool lyc_interrupt_line; bool cgb_palettes_blocked; uint8_t current_lcd_line; // The LCD can go out of sync since the vsync signal is skipped in some cases. + uint32_t cycles_in_stop_mode; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/joypad.c b/Core/joypad.c index 7713cbd..ebdf2f5 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -56,7 +56,6 @@ void GB_update_joyp(GB_gameboy_t *gb) if (previous_state != (gb->io_registers[GB_IO_JOYP] & 0xF)) { /* The joypad interrupt DOES occur on CGB (Tested on CGB-CPU-06), unlike what some documents say. */ gb->io_registers[GB_IO_IF] |= 0x10; - gb->stopped = false; } gb->io_registers[GB_IO_JOYP] |= 0xC0; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 1416000..cf8bf09 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -234,7 +234,15 @@ static void stop(GB_gameboy_t *gb, uint8_t opcode) } else { - gb->stopped = true; + if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { + /* HW Bug? When STOP is executed while a button is down, the CPU halts forever + yet the other hardware keeps running. */ + gb->interrupt_enable = 0; + gb->halted = true; + } + else { + gb->stopped = true; + } } /* Todo: is PC being actually read? */ @@ -1389,7 +1397,15 @@ void GB_cpu_run(GB_gameboy_t *gb) return; } if (gb->stopped) { - GB_advance_cycles(gb, 64); + GB_advance_cycles(gb, 4); + if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { + gb->stopped = false; + /* The CPU takes more time to wake up then the other components */ + for (unsigned i = 0x800; i--;) { + GB_advance_cycles(gb, 0x40); + } + GB_advance_cycles(gb, 8); + } return; } diff --git a/Core/timing.c b/Core/timing.c index 8affd86..039f750 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -210,8 +210,10 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) // Affected by speed boost gb->dma_cycles += cycles; - GB_timers_run(gb, cycles); - advance_serial(gb, cycles); + if (!gb->stopped) { + GB_timers_run(gb, cycles); + advance_serial(gb, cycles); // TODO: Verify what happens in STOP mode + } gb->debugger_ticks += cycles; @@ -227,8 +229,10 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_since_input_ir_change += cycles; gb->cycles_since_last_sync += cycles; gb->cycles_since_run += cycles; - GB_dma_run(gb); - GB_hdma_run(gb); + if (!gb->stopped) { // TODO: Verify what happens in STOP mode + GB_dma_run(gb); + GB_hdma_run(gb); + } GB_apu_run(gb); GB_display_run(gb, cycles); GB_ir_run(gb); From 2f9de4942c7cf9adfe32309b2927c99c97d12d12 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 18:27:25 +0300 Subject: [PATCH 026/341] Increase input polling frequency in the Cocoa and SDL frontends, should make inputs look less synthetic and potentially reduce input lag --- Core/gb.c | 6 +++++- Core/gb.h | 3 +++ Core/joypad.c | 2 ++ Core/memory.c | 1 + Core/sm83_cpu.c | 6 ++++++ Core/timing.c | 3 +++ SDL/main.c | 1 + 7 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Core/gb.c b/Core/gb.c index cb99d4c..bad1910 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -372,7 +372,6 @@ uint8_t GB_run(GB_gameboy_t *gb) gb->cycles_since_run = 0; GB_cpu_run(gb); if (gb->vblank_just_occured) { - GB_update_joyp(gb); GB_rtc_run(gb); GB_debugger_handle_async_commands(gb); GB_rewind_push(gb); @@ -812,3 +811,8 @@ unsigned GB_get_player_count(GB_gameboy_t *gb) { return GB_is_sgb(gb)? gb->sgb->player_count : 1; } + +void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback) +{ + gb->update_input_hint_callback = callback; +} diff --git a/Core/gb.h b/Core/gb.h index f429b45..b798a04 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -238,6 +238,7 @@ typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on, long cycles_si typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, bool rumble_on); typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool bit_to_send); typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); +typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); typedef struct { bool state; @@ -527,6 +528,7 @@ struct GB_gameboy_internal_s { GB_rumble_callback_t rumble_callback; GB_serial_transfer_bit_start_callback_t serial_transfer_bit_start_callback; GB_serial_transfer_bit_end_callback_t serial_transfer_bit_end_callback; + GB_update_input_hint_callback_t update_input_hint_callback; /* IR */ long cycles_since_ir_change; // In 8MHz units @@ -674,6 +676,7 @@ void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) void GB_set_rgb_encode_callback(GB_gameboy_t *gb, GB_rgb_encode_callback_t callback); void GB_set_infrared_callback(GB_gameboy_t *gb, GB_infrared_callback_t callback); void GB_set_rumble_callback(GB_gameboy_t *gb, GB_rumble_callback_t callback); +void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback); /* These APIs are used when using internal clock */ void GB_set_serial_transfer_bit_start_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_start_callback_t callback); diff --git a/Core/joypad.c b/Core/joypad.c index ebdf2f5..4ae6e67 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -65,6 +65,7 @@ void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed) { assert(index >= 0 && index < GB_KEY_MAX); gb->keys[0][index] = pressed; + GB_update_joyp(gb); } void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed) @@ -72,4 +73,5 @@ void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned play assert(index >= 0 && index < GB_KEY_MAX); assert(player < 4); gb->keys[player][index] = pressed; + GB_update_joyp(gb); } diff --git a/Core/memory.c b/Core/memory.c index c15ae42..9104cbd 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -307,6 +307,7 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) return (gb->apu.is_active[GB_NOISE] ? (gb->apu.samples[GB_NOISE] << 4) : 0) | (gb->apu.is_active[GB_WAVE] ? (gb->apu.samples[GB_WAVE]) : 0); case GB_IO_JOYP: + GB_timing_sync(gb); case GB_IO_TMA: case GB_IO_LCDC: case GB_IO_SCY: diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index cf8bf09..12ca670 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -234,6 +234,7 @@ static void stop(GB_gameboy_t *gb, uint8_t opcode) } else { + GB_timing_sync(gb); if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { /* HW Bug? When STOP is executed while a button is down, the CPU halts forever yet the other hardware keeps running. */ @@ -1397,6 +1398,7 @@ void GB_cpu_run(GB_gameboy_t *gb) return; } if (gb->stopped) { + GB_timing_sync(gb); GB_advance_cycles(gb, 4); if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { gb->stopped = false; @@ -1409,6 +1411,10 @@ void GB_cpu_run(GB_gameboy_t *gb) return; } + if ((gb->interrupt_enable & 0x10) && (gb->ime || gb->halted)) { + GB_timing_sync(gb); + } + if (gb->halted && !GB_is_cgb(gb) && !gb->just_halted) { GB_advance_cycles(gb, 2); } diff --git a/Core/timing.c b/Core/timing.c index 039f750..b14a00b 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -73,6 +73,9 @@ void GB_timing_sync(GB_gameboy_t *gb) } gb->cycles_since_last_sync = 0; + if (gb->update_input_hint_callback) { + gb->update_input_hint_callback(gb); + } } #else diff --git a/SDL/main.c b/SDL/main.c index 451ac9c..be52afd 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -435,6 +435,7 @@ restart: GB_set_color_correction_mode(&gb, configuration.color_correction_mode); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); GB_set_rewind_length(&gb, configuration.rewind_length); + GB_set_update_input_hint_callback(&gb, handle_events); } SDL_DestroyTexture(texture); From 9d8adbb5818d39143a143e18549c84a8f092b618 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 18:37:19 +0300 Subject: [PATCH 027/341] This is not correct, this bug only affects the PCM registers and not actual output. Currently not emulated at all. --- Core/apu.c | 10 ---------- Core/apu.h | 1 - 2 files changed, 11 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 7a54c43..664530d 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -489,16 +489,6 @@ void GB_apu_run(GB_gameboy_t *gb) } gb->apu.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1; - if (gb->model == GB_MODEL_CGB_C) { - /* Todo: This was confirmed to happen on a CGB-C. This may or may not happen on pre-CGB models. - Because this degrades audio quality, and testing this on a pre-CGB device requires audio records, - I'll assume these devices are innocent until proven guilty. - - Also happens on CGB-B, but not on CGB-D. - */ - gb->apu.current_lfsr_sample &= gb->apu.previous_lfsr_sample; - } - gb->apu.previous_lfsr_sample = gb->apu.noise_channel.lfsr & 1; update_sample(gb, GB_NOISE, gb->apu.current_lfsr_sample ? diff --git a/Core/apu.h b/Core/apu.h index 2a49c04..f8f56e9 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -114,7 +114,6 @@ typedef struct bool skip_div_event; bool current_lfsr_sample; - bool previous_lfsr_sample; } GB_apu_t; typedef enum { From 7fc3de69da3d2d3e0d561179f88858cbae5e049f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 18:40:54 +0300 Subject: [PATCH 028/341] Mark CGB-C as experimental --- Cocoa/Preferences.xib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index e754199..8278ee1 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -288,7 +288,7 @@ - + From 274760746e67d7939924cec2bd6ef55057f2c036 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 19:18:07 +0300 Subject: [PATCH 029/341] Fix #165 --- Core/gb.c | 6 +++--- Core/gb.h | 2 +- Core/save_state.c | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index bad1910..2712da9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -95,7 +95,7 @@ void GB_init(GB_gameboy_t *gb, GB_model_t model) memset(gb, 0, sizeof(*gb)); gb->model = model; if (GB_is_cgb(gb)) { - gb->ram = malloc(gb->ram_size = 0x2000 * 8); + gb->ram = malloc(gb->ram_size = 0x1000 * 8); gb->vram = malloc(gb->vram_size = 0x2000 * 2); } else { @@ -632,7 +632,7 @@ void GB_reset(GB_gameboy_t *gb) gb->io_registers[GB_IO_JOYP] = 0xF; gb->mbc_ram_size = mbc_ram_size; if (GB_is_cgb(gb)) { - gb->ram_size = 0x2000 * 8; + gb->ram_size = 0x1000 * 8; gb->vram_size = 0x2000 * 2; memset(gb->vram, 0, gb->vram_size); gb->cgb_mode = true; @@ -703,7 +703,7 @@ void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model) { gb->model = model; if (GB_is_cgb(gb)) { - gb->ram = realloc(gb->ram, gb->ram_size = 0x2000 * 8); + gb->ram = realloc(gb->ram, gb->ram_size = 0x1000 * 8); gb->vram = realloc(gb->vram, gb->vram_size = 0x2000 * 2); } else { diff --git a/Core/gb.h b/Core/gb.h index b798a04..5385710 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -333,6 +333,7 @@ struct GB_gameboy_internal_s { bool infrared_input; GB_printer_t printer; uint8_t extra_oam[0xff00 - 0xfea0]; + uint32_t ram_size; // Different between CGB and DMG ); /* DMA and HDMA */ @@ -593,7 +594,6 @@ struct GB_gameboy_internal_s { bool turbo; bool turbo_dont_skip; bool disable_rendering; - uint32_t ram_size; // Different between CGB and DMG uint8_t boot_rom[0x900]; bool vblank_just_occured; // For slow operations involving syscalls; these should only run once per vblank uint8_t cycles_since_run; // How many cycles have passed since the last call to GB_run(), in 8MHz units diff --git a/Core/save_state.c b/Core/save_state.c index 26c9b52..1cd3458 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -156,11 +156,6 @@ static bool verify_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t *save) return false; } - if (gb->ram_size != save->ram_size) { - GB_log(gb, "The save state has non-matching RAM size. Try changing the emulated model.\n"); - return false; - } - if (gb->vram_size != save->vram_size) { GB_log(gb, "The save state has non-matching VRAM size. Try changing the emulated model.\n"); return false; @@ -171,6 +166,17 @@ static bool verify_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t *save) return false; } + if (gb->ram_size != save->ram_size) { + if (gb->ram_size == 0x1000 * 8 && save->ram_size == 0x2000 * 8) { + /* A bug in versions prior to 0.12 made CGB instances allocate twice the ammount of RAM. + Ignore this issue to retain compatibility with older, 0.11, save states. */ + } + else { + GB_log(gb, "The save state has non-matching RAM size. Try changing the emulated model.\n"); + return false; + } + } + return true; } @@ -182,6 +188,8 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) /* Every unread value should be kept the same. */ memcpy(&save, gb, sizeof(save)); + /* ...Except ram size, we use it to detect old saves with incorrect ram sizes */ + save.ram_size = 0; FILE *f = fopen(path, "rb"); if (!f) { @@ -199,6 +207,17 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) if (!READ_SECTION(&save, f, rtc )) goto error; if (!READ_SECTION(&save, f, video )) goto error; + if (save.ram_size == 0) { + /* Save doesn't have ram size specified, it's a pre 0.12 save state with potentially + incorrect RAM amount if it's a CGB instance */ + if (GB_is_cgb(&save)) { + save.ram_size = 0x2000 * 8; // Incorrect RAM size + } + else { + save.ram_size = gb->ram_size; + } + } + if (!verify_state_compatibility(gb, &save)) { errno = -1; goto error; @@ -218,13 +237,19 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) fclose(f); return EIO; } + + /* Fix for 0.11 save states that allocate twice the amount of RAM in CGB instances */ + fseek(f, save.ram_size - gb->ram_size, SEEK_CUR); if (fread(gb->vram, 1, gb->vram_size, f) != gb->vram_size) { fclose(f); return EIO; } + size_t orig_ram_size = gb->ram_size; memcpy(gb, &save, sizeof(save)); + gb->ram_size = orig_ram_size; + errno = 0; if (gb->cartridge_type->has_rumble && gb->rumble_callback) { @@ -324,6 +349,10 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le return -1; } + /* Fix for 0.11 save states that allocate twice the amount of RAM in CGB instances */ + buffer += save.ram_size - gb->ram_size; + length -= save.ram_size - gb->ram_size; + memcpy(gb, &save, sizeof(save)); if (gb->cartridge_type->has_rumble && gb->rumble_callback) { From d0bd741049b57bb90d6838ee7dfc8ae017575ad6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 7 Jun 2019 23:38:34 +0300 Subject: [PATCH 030/341] Added SCX/SCY display in the VRAM viewer. Closes #168 --- Cocoa/Document.m | 6 ++++++ Cocoa/Document.xib | 39 +++++++++++++++++++++++++-------------- Cocoa/GBImageView.h | 4 +++- Cocoa/GBImageView.m | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 17 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 3d3ad7b..4d79148 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -936,6 +936,7 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, (GB_map_type_t) self.tilemapMapButton.indexOfSelectedItem, (GB_tileset_type_t) self.TilemapSetButton.indexOfSelectedItem); + self.tilemapImageView.scrollRect = NSMakeRect(gb.io_registers[GB_IO_SCX], gb.io_registers[GB_IO_SCY], 160, 144); self.tilemapImageView.image = [Document imageFromData:data width:256 height:256 scale:1.0]; self.tilemapImageView.layer.magnificationFilter = kCAFilterNearest; } @@ -1192,6 +1193,11 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, } } +- (IBAction)toggleScrollingDisplay:(NSButton *)sender +{ + self.tilemapImageView.displayScrollRect = sender.state; +} + - (IBAction)vramTabChanged:(NSSegmentedControl *)sender { [self.vramTabView selectTabViewItemAtIndex:[sender selectedSegment]]; diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index 88c5275..d558216 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -1,8 +1,8 @@ - + - + @@ -83,9 +83,9 @@ - + - + @@ -109,7 +109,7 @@ - + @@ -133,9 +133,9 @@ - + - + @@ -163,9 +163,9 @@ - + - + @@ -243,7 +243,7 @@ - + @@ -260,7 +260,7 @@ - + @@ -285,7 +285,7 @@ - + @@ -298,7 +298,7 @@ - + @@ -377,6 +377,17 @@ + @@ -612,7 +623,7 @@ - + diff --git a/Cocoa/GBImageView.h b/Cocoa/GBImageView.h index 22a8829..d5ee534 100644 --- a/Cocoa/GBImageView.h +++ b/Cocoa/GBImageView.h @@ -11,7 +11,9 @@ @interface GBImageView : NSImageView @property (nonatomic) NSArray *horizontalGrids; @property (nonatomic) NSArray *verticalGrids; -@property (weak) IBOutlet id delegate; +@property (nonatomic) bool displayScrollRect; +@property NSRect scrollRect; +@property (weak) IBOutlet id delegate; @end @protocol GBImageViewDelegate diff --git a/Cocoa/GBImageView.m b/Cocoa/GBImageView.m index 674d4b2..973625e 100644 --- a/Cocoa/GBImageView.m +++ b/Cocoa/GBImageView.m @@ -25,8 +25,8 @@ [conf.color set]; for (CGFloat y = conf.size * y_ratio; y < self.frame.size.height; y += conf.size * y_ratio) { NSBezierPath *line = [NSBezierPath bezierPath]; - [line moveToPoint:NSMakePoint(0, y + 0.5)]; - [line lineToPoint:NSMakePoint(self.frame.size.width, y + 0.5)]; + [line moveToPoint:NSMakePoint(0, y - 0.5)]; + [line lineToPoint:NSMakePoint(self.frame.size.width, y - 0.5)]; [line setLineWidth:1.0]; [line stroke]; } @@ -42,6 +42,35 @@ [line stroke]; } } + + if (self.displayScrollRect) { + NSBezierPath *path = [NSBezierPath bezierPathWithRect:CGRectInfinite]; + for (unsigned x = 0; x < 2; x++) { + for (unsigned y = 0; y < 2; y++) { + NSRect rect = self.scrollRect; + rect.origin.x *= x_ratio; + rect.origin.y *= y_ratio; + rect.size.width *= x_ratio; + rect.size.height *= y_ratio; + rect.origin.y = self.frame.size.height - rect.origin.y - rect.size.height; + + rect.origin.x -= self.frame.size.width * x; + rect.origin.y += self.frame.size.height * y; + + + NSBezierPath *subpath = [NSBezierPath bezierPathWithRect:rect]; + [path appendBezierPath:subpath]; + } + } + [path setWindingRule:NSEvenOddWindingRule]; + [path setLineWidth:4.0]; + [path setLineJoinStyle:NSRoundLineJoinStyle]; + [[NSColor colorWithWhite:0.2 alpha:0.5] set]; + [path fill]; + [path addClip]; + [[NSColor colorWithWhite:0.0 alpha:0.6] set]; + [path stroke]; + } } - (void)setHorizontalGrids:(NSArray *)horizontalGrids @@ -56,6 +85,12 @@ [self setNeedsDisplay]; } +- (void)setDisplayScrollRect:(bool)displayScrollRect +{ + self->_displayScrollRect = displayScrollRect; + [self setNeedsDisplay]; +} + - (void)updateTrackingAreas { if(trackingArea != nil) { From 5cda1f2f5f5e854bf64d63c72f1d665086611f05 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 8 Jun 2019 00:04:58 +0300 Subject: [PATCH 031/341] Fix the last commit --- Cocoa/Document.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 4d79148..731f7c8 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -936,7 +936,9 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, (GB_map_type_t) self.tilemapMapButton.indexOfSelectedItem, (GB_tileset_type_t) self.TilemapSetButton.indexOfSelectedItem); - self.tilemapImageView.scrollRect = NSMakeRect(gb.io_registers[GB_IO_SCX], gb.io_registers[GB_IO_SCY], 160, 144); + self.tilemapImageView.scrollRect = NSMakeRect(GB_read_memory(&gb, 0xFF00 | GB_IO_SCX), + GB_read_memory(&gb, 0xFF00 | GB_IO_SCY), + 160, 144); self.tilemapImageView.image = [Document imageFromData:data width:256 height:256 scale:1.0]; self.tilemapImageView.layer.magnificationFilter = kCAFilterNearest; } From bb7fa95426d25bb03d6e37e6e34412c218acdbf8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 8 Jun 2019 13:37:49 +0300 Subject: [PATCH 032/341] Fix incorrect register values when changing the color palette via the boot ROM --- BootROMs/cgb_boot.asm | 4 ++++ Makefile | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index ee0198a..3e07399 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -758,13 +758,17 @@ ENDC ld a, [$143] bit 7, a call z, EmulateDMG + bit 7, a + ldh [$4C], a ldh a, [TitleChecksum] ld b, a + jr z, .skipDMGForCGBCheck ldh a, [InputPalette] and a jr nz, .emulateDMGForCGBGame +.skipDMGForCGBCheck IF DEF(AGB) ; Set registers to match the original AGB-CGB boot ; AF = $1100, C = 0 diff --git a/Makefile b/Makefile index 38b3e83..e052287 100644 --- a/Makefile +++ b/Makefile @@ -322,6 +322,10 @@ $(BIN)/SDL/Shaders: Shaders # Boot ROMs +$(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm +$(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm +$(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm + $(BIN)/BootROMs/%.bin: BootROMs/%.asm -@$(MKDIR) -p $(dir $@) cd BootROMs && rgbasm -o ../$@.tmp ../$< From 0da293010911d0cfc29de28b2a554a47f9546616 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 8 Jun 2019 14:35:52 +0300 Subject: [PATCH 033/341] Fix #175 --- Core/debugger.c | 2 +- Core/display.c | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index ba413ca..d8872a4 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1529,7 +1529,7 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg } GB_log(gb, "LCDC:\n"); GB_log(gb, " LCD enabled: %s\n",(gb->io_registers[GB_IO_LCDC] & 128)? "Enabled" : "Disabled"); - GB_log(gb, " %s: %s\n", GB_is_cgb(gb)? (gb->cgb_mode? "Sprite priority flags" : "Background and Window") : "Background", + GB_log(gb, " %s: %s\n", (gb->cgb_mode? "Sprite priority flags" : "Background and Window"), (gb->io_registers[GB_IO_LCDC] & 1)? "Enabled" : "Disabled"); GB_log(gb, " Objects: %s\n", (gb->io_registers[GB_IO_LCDC] & 2)? "Enabled" : "Disabled"); GB_log(gb, " Object size: %s\n", (gb->io_registers[GB_IO_LCDC] & 4)? "8x16" : "8x8"); diff --git a/Core/display.c b/Core/display.c index c84b3a9..240e3c7 100644 --- a/Core/display.c +++ b/Core/display.c @@ -112,7 +112,7 @@ typedef struct __attribute__((packed)) { static bool window_enabled(GB_gameboy_t *gb) { if ((gb->io_registers[GB_IO_LCDC] & 0x1) == 0) { - if (!gb->cgb_mode && GB_is_cgb(gb)) { + if (!gb->cgb_mode) { return false; } } @@ -376,9 +376,6 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) bg_enabled = false; } } - if (!GB_is_cgb(gb) && gb->in_window) { - bg_enabled = true; - } { uint8_t pixel = bg_enabled? fifo_item->pixel : 0; From 49d8a5cb440026d4455c5b981567e92acb057694 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 8 Jun 2019 16:08:07 +0300 Subject: [PATCH 034/341] Fixed the parsing of comparison operators as well as their priorities. Fixes #155 --- Core/debugger.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index d8872a4..79b5991 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -350,15 +350,15 @@ static struct { {"&", 1, and}, {"^", 1, xor}, {"<<", 2, shleft}, - {"<=", 3, lower_equals}, - {"<", 3, lower}, + {"<=", -1, lower_equals}, + {"<", -1, lower}, {">>", 2, shright}, - {">=", 3, greater_equals}, - {">", 3, greater}, - {"==", 3, equals}, - {"=", -1, NULL, assign}, - {"!=", 3, different}, - {":", 4, bank}, + {">=", -1, greater_equals}, + {">", -1, greater}, + {"==", -1, equals}, + {"=", -2, NULL, assign}, + {"!=", -1, different}, + {":", 3, bank}, }; value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, @@ -575,9 +575,13 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, for (int j = 0; j < sizeof(operators) / sizeof(operators[0]); j++) { if (strlen(operators[j].string) > length - i) continue; // Operator too big. // Priority higher than what we already have. - if (operator_index != -1 && operators[operator_index].priority < operators[j].priority) continue; unsigned long operator_length = strlen(operators[j].string); if (memcmp(string + i, operators[j].string, operator_length) == 0) { + if (operator_index != -1 && operators[operator_index].priority < operators[j].priority) { + /* for supporting = vs ==, etc*/ + i += operator_length - 1; + continue; + } // Found an operator! operator_pos = i; operator_index = j; From a0c5baecd87426bea56853b85bd9a634e6d7473b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 9 Jun 2019 00:53:44 +0300 Subject: [PATCH 035/341] More realistic initial V/RAM values in the boot ROM. Fixes #150 and #91 --- BootROMs/cgb_boot.asm | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 3e07399..4c073ed 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -5,21 +5,26 @@ Start: ; Init stack pointer ld sp, $fffe +; Clear memory VRAM + ld hl, $8000 + call ClearMemoryPage + ld a, 2 + ld c, $70 + ld [c], a +; Clear RAM Bank 2 (Like the original boot ROM + ld h, $D0 xor a + call ClearMemoryPage + ld [c], a + ; Clear chosen input palette ldh [InputPalette], a ; Clear title checksum ldh [TitleChecksum], a -; Clear memory VRAM - ld hl, $8000 - call ClearMemoryPage - ld h, $d0 - call ClearMemoryPage - + ; Clear OAM - ld hl, $fe00 + ld h, $fe ld c, $a0 - xor a .clearOAMLoop ldi [hl], a dec c @@ -67,7 +72,7 @@ Start: ld hl, $8000 call ClearMemoryPage -; Copy Sameboy Logo +; Copy SameBoy Logo ld de, SameboyLogo ld hl, $8080 ld c, (SameboyLogoEnd - SameboyLogo) / 2 @@ -183,7 +188,7 @@ ENDC ld hl, BgPalettes ld d, 64 ; Length of write - ld e, 0 ; Index of write + ld e, c ; Index of write (C=0) call LoadBGPalettes ; Turn on LCD @@ -1008,7 +1013,7 @@ ClearVRAMViaHDMA: ld hl, $FF51 ; Src - ld a, $D0 + ld a, $88 ld [hli], a xor a ld [hli], a @@ -1021,7 +1026,7 @@ ClearVRAMViaHDMA: ; Do it ld a, $12 - ld [hli], a + ld [hl], a ret GetInputPaletteIndex: From 8b1c1652536c81d8622ec77f6de21f3cbeaa88bc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 9 Jun 2019 13:48:05 +0300 Subject: [PATCH 036/341] Automation fixes --- Tester/main.c | 94 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/Tester/main.c b/Tester/main.c index 643c39f..d9ce165 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -30,7 +30,8 @@ static char *log_filename; static FILE *log_file; static void replace_extension(const char *src, size_t length, char *dest, const char *ext); static bool push_start_a, start_is_not_first, a_is_bad, b_is_confirm, push_faster, push_slower, - do_not_stop, push_a_twice, start_is_bad, allow_weird_sp_values, large_stack, push_right; + do_not_stop, push_a_twice, start_is_bad, allow_weird_sp_values, large_stack, push_right, + semi_random, limit_start, pointer_control; static unsigned int test_length = 60 * 40; GB_gameboy_t gb; @@ -63,35 +64,54 @@ static void vblank(GB_gameboy_t *gb) unsigned combo_length = 40; if (start_is_not_first || push_a_twice) combo_length = 60; /* The start item in the menu is not the first, so also push down */ else if (a_is_bad || start_is_bad) combo_length = 20; /* Pressing A has a negative effect (when trying to start the game). */ - - switch ((push_faster ? frames * 2 : - push_slower ? frames / 2 : - push_a_twice? frames / 4: - frames) % combo_length + (start_is_bad? 20 : 0) ) { - case 0: - gb->keys[0][push_right? 0 : 7] = true; // Start (Or right) down - break; - case 10: - gb->keys[0][push_right? 0 : 7] = false; // Start (Or right) up - break; - case 20: - gb->keys[0][b_is_confirm? 5: 4] = true; // A down (or B) - break; - case 30: - gb->keys[0][b_is_confirm? 5: 4] = false; // A up (or B) - break; - case 40: - if (push_a_twice) { + + if (semi_random) { + if (frames % 10 == 0) { + unsigned key = (((frames / 20) * 0x1337cafe) >> 29) & 7; + gb->keys[0][key] = (frames % 20) == 0; + } + } + else { + switch ((push_faster ? frames * 2 : + push_slower ? frames / 2 : + push_a_twice? frames / 4: + frames) % combo_length + (start_is_bad? 20 : 0) ) { + case 0: + if (!limit_start || frames < 20 * 60) { + gb->keys[0][push_right? 0 : 7] = true; // Start (Or right) down + } + if (pointer_control) { + gb->keys[0][1] = true; // left + gb->keys[0][2] = true; // up + } + + break; + case 10: + gb->keys[0][push_right? 0 : 7] = false; // Start (Or right) up + if (pointer_control) { + gb->keys[0][1] = false; // left + gb->keys[0][2] = false; // up + } + break; + case 20: gb->keys[0][b_is_confirm? 5: 4] = true; // A down (or B) - } - else if (gb->boot_rom_finished) { - gb->keys[0][3] = true; // D-Pad Down down - } - break; - case 50: - gb->keys[0][b_is_confirm? 5: 4] = false; // A down (or B) - gb->keys[0][3] = false; // D-Pad Down up - break; + break; + case 30: + gb->keys[0][b_is_confirm? 5: 4] = false; // A up (or B) + break; + case 40: + if (push_a_twice) { + gb->keys[0][b_is_confirm? 5: 4] = true; // A down (or B) + } + else if (gb->boot_rom_finished) { + gb->keys[0][3] = true; // D-Pad Down down + } + break; + case 50: + gb->keys[0][b_is_confirm? 5: 4] = false; // A down (or B) + gb->keys[0][3] = false; // D-Pad Down up + break; + } } } @@ -337,7 +357,9 @@ int main(int argc, char **argv) strcmp((const char *)(gb.rom + 0x134), "KWIRK") == 0 || strcmp((const char *)(gb.rom + 0x134), "PUZZLE BOY") == 0; start_is_bad = strcmp((const char *)(gb.rom + 0x134), "BLUESALPHA") == 0; - b_is_confirm = strcmp((const char *)(gb.rom + 0x134), "ELITE SOCCER") == 0; + b_is_confirm = strcmp((const char *)(gb.rom + 0x134), "ELITE SOCCER") == 0 || + strcmp((const char *)(gb.rom + 0x134), "SOCCER") == 0 || + strcmp((const char *)(gb.rom + 0x134), "GEX GECKO") == 0; push_faster = strcmp((const char *)(gb.rom + 0x134), "MOGURA DE PON!") == 0; push_slower = strcmp((const char *)(gb.rom + 0x134), "BAKENOU") == 0; do_not_stop = strcmp((const char *)(gb.rom + 0x134), "SPACE INVADERS") == 0; @@ -346,6 +368,9 @@ int main(int argc, char **argv) /* M&M's Minis Madness Demo (which has no menu but the same title as the full game) */ (memcmp((const char *)(gb.rom + 0x134), "MINIMADNESSBMIE", strlen("MINIMADNESSBMIE")) == 0 && gb.rom[0x14e] == 0x6c); + /* This game has some terrible menus. */ + semi_random = strcmp((const char *)(gb.rom + 0x134), "KUKU GAME") == 0; + /* This game temporarily sets SP to OAM RAM */ @@ -356,6 +381,10 @@ int main(int argc, char **argv) /* This game uses some recursive algorithms and therefore requires quite a large call stack */ large_stack = memcmp((const char *)(gb.rom + 0x134), "MICRO EPAK1BM", strlen("MICRO EPAK1BM")) == 0 || strcmp((const char *)(gb.rom + 0x134), "TECMO BOWL") == 0; + /* High quality game that leaks stack whenever you open the menu (with start), + but requires pressing start to play it. */ + limit_start = strcmp((const char *)(gb.rom + 0x134), "DIVA STARS") == 0; + large_stack |= limit_start; /* Pressing start while in the map in Tsuri Sensei will leak an internal screen-stack which will eventually overflow, override an array of jump-table indexes, jump to a random @@ -363,6 +392,10 @@ int main(int argc, char **argv) will prevent this scenario. */ push_a_twice = strcmp((const char *)(gb.rom + 0x134), "TURI SENSEI V1") == 0; + /* Yes, you should totally use a cursor point & click interface for the language select menu. */ + pointer_control = memcmp((const char *)(gb.rom + 0x134), "LEGO ATEAM BLPP", strlen("LEGO ATEAM BLPP")) == 0; + push_faster |= pointer_control; + /* Run emulation */ running = true; gb.turbo = gb.turbo_dont_skip = gb.disable_rendering = true; @@ -376,6 +409,7 @@ int main(int argc, char **argv) } } + if (log_file) { fclose(log_file); log_file = NULL; From 8386aaf12f47fcffd4170777689f09c3f113787a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 9 Jun 2019 15:15:08 +0200 Subject: [PATCH 037/341] Save 20 bytes in the CGB boot ROM --- BootROMs/cgb_boot.asm | 73 ++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 4c073ed..ac9b054 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -103,10 +103,11 @@ Start: ; Load Tilemap ld hl, $98C2 ld b, 3 - ld a, 8 IF DEF(FAST) xor a ldh [$4F], a +ELSE + ld a, 8 ENDC .tilemapLoop @@ -120,8 +121,7 @@ IF !DEF(FAST) ; Switch to second VRAM Bank ld a, 1 ldh [$4F], a - ld a, 8 - ld [hl], a + ld [hl], 8 ; Switch to back first VRAM Bank xor a ldh [$4F], a @@ -628,6 +628,8 @@ ClearMemoryPage: jr z, ClearMemoryPage ret +ReadTwoTileLines: + call ReadTileLine ; c = $f0 for even lines, $f for odd lines. ReadTileLine: ld a, [de] @@ -647,34 +649,28 @@ ReadTileLine: .dontSwap inc hl ldi [hl], a + swap c ret ReadCGBLogoHalfTile: - ld c, $f0 - call ReadTileLine - ld c, $f - call ReadTileLine - inc e - ld c, $f0 - call ReadTileLine - ld c, $f - call ReadTileLine + call .do_twice +.do_twice + call ReadTwoTileLines inc e + ld a, e ret ReadCGBLogoTile: + ld c, $f0 call ReadCGBLogoHalfTile - ld a, e add a, 22 ld e, a call ReadCGBLogoHalfTile - ld a, e sub a, 22 ld e, a ret - ReadTrademarkSymbol: ld de, TrademarkSymbol ld c,$08 @@ -706,19 +702,23 @@ LoadPalettes: jr nz, .loop ret - -AdvanceIntroAnimation: +DoIntroAnimation: + ; Animate the intro + ld a, 1 + ldh [$4F], a + ld d, 26 +.animationLoop + ld b, 2 + call WaitBFrames ld hl, $98C0 ld c, 3 ; Row count .loop ld a, [hl] cp $F ; Already blue jr z, .nextTile - inc a - ld [hl], a + inc [hl] and $7 - cp $1 ; Changed a white tile, go to next line - jr z, .nextLine + jr z, .nextLine ; Changed a white tile, go to next line .nextTile inc hl jr .loop @@ -728,18 +728,7 @@ AdvanceIntroAnimation: ld l, a inc hl dec c - ret z - jr .loop - -DoIntroAnimation: - ; Animate the intro - ld a, 1 - ldh [$4F], a - ld d, 26 -.animationLoop - ld b, 2 - call WaitBFrames - call AdvanceIntroAnimation + jr nz, .loop dec d jr nz, .animationLoop ret @@ -796,7 +785,7 @@ ENDC .emulateDMGForCGBGame call EmulateDMG ldh [$4C], a - ld a, $1; + ld a, $1 ret EmulateDMG: @@ -833,7 +822,7 @@ GetPaletteIndex: ld a, [hl] ; Old Licensee cp $33 jr z, .newLicensee - cp 1 ; Nintendo + dec a ; 1 = Nintendo jr nz, .notNintendo jr .doChecksum .newLicensee @@ -848,22 +837,22 @@ GetPaletteIndex: .doChecksum ld l, $34 ld c, $10 - ld b, 0 + xor a .checksumLoop - ld a, [hli] - add b - ld b, a + add [hl] + inc l dec c jr nz, .checksumLoop + ld b, a ; c = 0 ld hl, TitleChecksums .searchLoop ld a, l - cp ChecksumsEnd & $FF - jr z, .notNintendo + sub LOW(ChecksumsEnd) ; use sub to zero out a + ret z ld a, [hli] cp b jr nz, .searchLoop @@ -1189,4 +1178,4 @@ BgPalettes: InputPalette: ds 1 WaitLoopCounter: - ds 1 \ No newline at end of file + ds 1 From 843683a4928bec1e00c0dc8626295a866e1a7bf4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 9 Jun 2019 18:14:32 +0300 Subject: [PATCH 038/341] Randomize everything! --- Core/gb.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/Core/gb.c b/Core/gb.c index 2712da9..6c0bd6c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -602,6 +602,87 @@ static void reset_ram(GB_gameboy_t *gb) break; } + /* HRAM */ + switch (gb->model) { + case GB_MODEL_CGB_C: + // case GB_MODEL_CGB_D: + case GB_MODEL_CGB_E: + case GB_MODEL_AGB: + for (unsigned i = 0; i < sizeof(gb->hram); i++) { + gb->hram[i] = (random() & 0xFF); + } + break; + + case GB_MODEL_DMG_B: + case GB_MODEL_SGB_NTSC: /* Unverified*/ + case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB2: + for (unsigned i = 0; i < sizeof(gb->hram); i++) { + if (i & 1) { + gb->hram[i] = random() | random() | random(); + } + else { + gb->hram[i] = random() & random() & random(); + } + } + break; + } + + /* OAM */ + switch (gb->model) { + case GB_MODEL_CGB_C: + case GB_MODEL_CGB_E: + case GB_MODEL_AGB: + /* Zero'd out by boot ROM anyway*/ + break; + + case GB_MODEL_DMG_B: + case GB_MODEL_SGB_NTSC: /* Unverified*/ + case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB2: + for (unsigned i = 0; i < 8; i++) { + if (i & 2) { + gb->oam[i] = random() & random() & random(); + } + else { + gb->oam[i] = random() | random() | random(); + } + } + for (unsigned i = 8; i < sizeof(gb->oam); i++) { + gb->oam[i] = gb->oam[i - 8]; + } + break; + } + + /* Wave RAM */ + switch (gb->model) { + case GB_MODEL_CGB_C: + case GB_MODEL_CGB_E: + case GB_MODEL_AGB: + /* Initialized by CGB-A and newer, 0s in CGB-0*/ + break; + + case GB_MODEL_DMG_B: + case GB_MODEL_SGB_NTSC: /* Unverified*/ + case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB2: { + uint8_t temp; + for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) { + if (i & 1) { + temp = random() & random() & random(); + } + else { + temp = random() | random() | random(); + } + gb->apu.wave_channel.wave_form[i * 2] = temp >> 4; + gb->apu.wave_channel.wave_form[i * 2 + 1] = temp & 0xF; + gb->io_registers[GB_IO_WAV_START + i] = temp; + + } + break; + } + } + for (unsigned i = 0; i < sizeof(gb->extra_oam); i++) { gb->extra_oam[i] = (random() & 0xFF); } From e2ef8dbbe03fa94c084ee87769934805de820e92 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 9 Jun 2019 18:43:23 +0300 Subject: [PATCH 039/341] Fix the GUI on some Windows 10 machines (Intel HD?). Fixes #112 --- SDL/gui.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SDL/gui.c b/SDL/gui.c index 7716760..70a449d 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1185,6 +1185,10 @@ void run_gui(bool is_running) } render_texture(pixels, NULL); +#ifdef _WIN32 + /* Required for some Windows 10 machines, god knows why */ + render_texture(pixels, NULL); +#endif } } while (SDL_WaitEvent(&event)); } From c678407d1eae9fec625a644506b3cd66654d9b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Mon, 10 Jun 2019 17:45:14 +0200 Subject: [PATCH 040/341] Compress the Sameboy logo. 117 bytes are now free --- BootROMs/SameboyLogo.1bpp | Bin 384 -> 0 bytes BootROMs/cgb_boot.asm | 175 ++++++++++++++++++++------------------ BootROMs/logo-compress.c | 48 +++++++++++ Makefile | 14 ++- 4 files changed, 151 insertions(+), 86 deletions(-) delete mode 100644 BootROMs/SameboyLogo.1bpp create mode 100644 BootROMs/logo-compress.c diff --git a/BootROMs/SameboyLogo.1bpp b/BootROMs/SameboyLogo.1bpp deleted file mode 100644 index b219f7d2a198da14e871fc1559abfda928c026ed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 384 zcmXYtJ#GRq5QS$&;nEjC_7&1+WM7Fci{!{e6qI{|y5T9yQwX0cF;Mj>ZX4>YBeBtshI-bXqK=bkE%wyuL>49$o&`iZ`_ zyRIXPI1L-xU2fCgr+fPROypf?^O&5sS~X%YjCl?$CXPeQZD3|sv|z1St=e|Ie)k{u z`}O*LI`#b?^ar8>h>XEjo>}RFOSIaH4Kws7&Hxzy#^}8>1}kk|(p#+2GKXopU3Rh7 zF{NQBrCJ${E@Sv`fVua^Eagl|!kc}JQ9NV=gaKhxcxNs1cs%ZL*aB*^#Ruk)aV*~O K*b2q3+4K*8j+-0+ diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index ac9b054..5d492f9 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -57,11 +57,10 @@ Start: .loadLogoLoop ld a, [de] ; Read 2 rows ld b, a - call DoubleBitsAndWriteRow - call DoubleBitsAndWriteRow + call DoubleBitsAndWriteRowTwice inc de ld a, e - xor $34 ; End of logo + cp $34 ; End of logo jr nz, .loadLogoLoop call ReadTrademarkSymbol @@ -71,53 +70,24 @@ Start: xor a ld hl, $8000 call ClearMemoryPage + call LoadTileset -; Copy SameBoy Logo - ld de, SameboyLogo - ld hl, $8080 - ld c, (SameboyLogoEnd - SameboyLogo) / 2 -.sameboyLogoLoop - ld a, [de] - ldi [hl], a - inc hl - inc de - ld a, [de] - ldi [hl], a - inc hl - inc de - dec c - jr nz, .sameboyLogoLoop - -; Copy (unresized) ROM logo - ld de, $104 - ld c, 6 -.CGBROMLogoLoop - push bc - call ReadCGBLogoTile - pop bc - dec c - jr nz, .CGBROMLogoLoop - inc hl - call ReadTrademarkSymbol - -; Load Tilemap - ld hl, $98C2 ld b, 3 IF DEF(FAST) xor a ldh [$4F], a ELSE +; Load Tilemap + ld hl, $98C2 + ld d, 3 ld a, 8 -ENDC .tilemapLoop ld c, $10 .tilemapRowLoop - ld [hl], a push af -IF !DEF(FAST) ; Switch to second VRAM Bank ld a, 1 ldh [$4F], a @@ -125,25 +95,29 @@ IF !DEF(FAST) ; Switch to back first VRAM Bank xor a ldh [$4F], a -ENDC pop af ldi [hl], a - inc a + add d dec c jr nz, .tilemapRowLoop + sub 47 + push de ld de, $10 add hl, de + pop de dec b jr nz, .tilemapLoop - cp $38 - jr nz, .doneTilemap + dec d + jr z, .endTilemap + dec d - ld hl, $99a7 - ld b, 1 - ld c, 7 + ld a, $38 + ld l, $a7 + ld bc, $0107 jr .tilemapRowLoop -.doneTilemap +.endTilemap +ENDC ; Expand Palettes ld de, AnimationColors @@ -187,9 +161,7 @@ ENDC jr nz, .expandPalettesLoop ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, c ; Index of write (C=0) - call LoadBGPalettes + call LoadBGPalettes64 ; Turn on LCD ld a, $91 @@ -560,8 +532,7 @@ TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c SameboyLogo: - incbin "SameboyLogo.1bpp" -SameboyLogoEnd: + incbin "SameboyLogo.rle" AnimationColors: dw $7FFF ; White @@ -578,7 +549,9 @@ DMGPalettes: dw $7FFF, $32BF, $00D0, $0000 ; Helper Functions -DoubleBitsAndWriteRow: +DoubleBitsAndWriteRowTwice: + call .twice +.twice ; Double the most significant 4 bits, b is shifted by 4 ld a, 4 ld c, 0 @@ -661,7 +634,46 @@ ReadCGBLogoHalfTile: ld a, e ret -ReadCGBLogoTile: +LoadTileset: +; Copy SameBoy Logo + ld de, SameboyLogo + ld hl, $8080 +.sameboyLogoLoop + ld a, [de] + inc de + + ld b, a + and $0f + jr z, .skipLiteral + ld c, a + +.literalLoop + ld a, [de] + ldi [hl], a + inc hl + inc de + dec c + jr nz, .literalLoop +.skipLiteral + swap b + ld a, b + and $0f + jr z, .sameboyLogoEnd + ld c, a + ld a, [de] + inc de + +.repeatLoop + ldi [hl], a + inc hl + dec c + jr nz, .repeatLoop + jr .sameboyLogoLoop + +.sameboyLogoEnd +; Copy (unresized) ROM logo + ld de, $104 +.CGBROMLogoLoop ld c, $f0 call ReadCGBLogoHalfTile add a, 22 @@ -669,8 +681,10 @@ ReadCGBLogoTile: call ReadCGBLogoHalfTile sub a, 22 ld e, a - ret - + cp $1c + jr nz, .CGBROMLogoLoop + inc hl + ; fallthrough ReadTrademarkSymbol: ld de, TrademarkSymbol ld c,$08 @@ -687,7 +701,11 @@ LoadObjPalettes: ld c, $6A jr LoadPalettes +LoadBGPalettes64: + ld d, 64 + LoadBGPalettes: + ld e, 0 ld c, $68 LoadPalettes: @@ -735,7 +753,23 @@ DoIntroAnimation: Preboot: IF !DEF(FAST) - call FadeOut + ld b, 32 ; 32 times to fade +.fadeLoop + ld c, 32 ; 32 colors to fade + ld hl, BgPalettes +.frameLoop + push bc + call BrightenColor + pop bc + dec c + jr nz, .frameLoop + + call WaitFrame + call WaitFrame + ld hl, BgPalettes + call LoadBGPalettes64 + dec b + jr nz, .fadeLoop ENDC call ClearVRAMViaHDMA ; Select the first bank @@ -921,9 +955,7 @@ LoadPalettesFromIndex: ; a = index of combination ld c, a add hl, bc ld d, 8 - ld e, 0 - call LoadBGPalettes - ret + jp LoadBGPalettes BrightenColor: ld a, [hli] @@ -976,28 +1008,6 @@ BrightenColor: ld [hli], a ret -FadeOut: - ld b, 32 ; 32 times to fade -.fadeLoop - ld c, 32 ; 32 colors to fade - ld hl, BgPalettes -.frameLoop - push bc - call BrightenColor - pop bc - dec c - jr nz, .frameLoop - - call WaitFrame - call WaitFrame - ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, 0 ; Index of write - call LoadBGPalettes - dec b - ret z - jr .fadeLoop - ClearVRAMViaHDMA: ld hl, $FF51 @@ -1014,8 +1024,7 @@ ClearVRAMViaHDMA: ld [hli], a ; Do it - ld a, $12 - ld [hl], a + ld [hl], $12 ret GetInputPaletteIndex: @@ -1113,9 +1122,7 @@ ChangeAnimationPalette: call WaitFrame ld hl, BgPalettes - ld d, 64 ; Length of write - ld e, 0 ; Index of write - call LoadBGPalettes + call LoadBGPalettes64 ; Delay the wait loop while the user is selecting a palette ld a, 30 ldh [WaitLoopCounter], a diff --git a/BootROMs/logo-compress.c b/BootROMs/logo-compress.c new file mode 100644 index 0000000..e4d2677 --- /dev/null +++ b/BootROMs/logo-compress.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void pair(size_t count, uint8_t byte) { + static size_t unique_count = 0; + static uint8_t unique_data[15]; + if (count == 1) { + unique_data[unique_count++] = byte; + assert(unique_count <= 15); + } else { + assert(count <= 15); + uint8_t control = (count << 4) | unique_count; + putchar(control); + + for (size_t i = 0; i < unique_count; i++) { + putchar(unique_data[i]); + } + + if (count != 0) { + putchar(byte); + } else { + assert(control == 0); + } + + unique_count = 0; + } +} + +int main(int argc, char *argv[]) { + size_t count = 1; + uint8_t byte = getchar(); + int new; + size_t position = 0; + + while ((new = getchar()) != EOF) { + if (byte == new) { + count++; + } else { + pair(count, byte); + byte = new; + count = 1; + } + } + + pair(count, byte); + pair(0, 0); +} diff --git a/Makefile b/Makefile index e052287..b4be355 100644 --- a/Makefile +++ b/Makefile @@ -322,13 +322,23 @@ $(BIN)/SDL/Shaders: Shaders # Boot ROMs +$(OBJ)/%.1bpp: %.png + -@$(MKDIR) -p $(dir $@) + rgbgfx -d 1 -h -o $@ $< + +$(OBJ)/BootROMs/SameboyLogo.rle: $(OBJ)/BootROMs/SameboyLogo.1bpp build/logo-compress + build/logo-compress < $< > $@ + +build/logo-compress: BootROMs/logo-compress.c + $(CC) $< -o $@ + $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm -$(BIN)/BootROMs/%.bin: BootROMs/%.asm +$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameboyLogo.rle -@$(MKDIR) -p $(dir $@) - cd BootROMs && rgbasm -o ../$@.tmp ../$< + rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp dd if=$@.tmp2 of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304) @rm $@.tmp $@.tmp2 From 8389c6a45060a7b6bc9dc909d8f5e36c7d5c7549 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 14 Jun 2019 14:31:17 +0300 Subject: [PATCH 041/341] Long overdue capitalization fixes --- BootROMs/{SameboyLogo.png => SameBoyLogo.png} | Bin BootROMs/cgb_boot.asm | 14 +++++++------- BootROMs/dmg_boot.asm | 2 +- BootROMs/sgb_boot.asm | 2 +- Makefile | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) rename BootROMs/{SameboyLogo.png => SameBoyLogo.png} (100%) diff --git a/BootROMs/SameboyLogo.png b/BootROMs/SameBoyLogo.png similarity index 100% rename from BootROMs/SameboyLogo.png rename to BootROMs/SameBoyLogo.png diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 5d492f9..0472cbe 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -1,4 +1,4 @@ -; Sameboy CGB bootstrap ROM +; SameBoy CGB bootstrap ROM ; Todo: use friendly names for HW registers instead of magic numbers SECTION "BootCode", ROM0[$0] Start: @@ -468,7 +468,7 @@ ENDM palette_comb 4, 3, 28 palette_comb 28, 3, 6 palette_comb 4, 28, 29 - ; Sameboy "Exclusives" + ; SameBoy "Exclusives" palette_comb 30, 30, 30 ; CGA palette_comb 31, 31, 31 ; DMG LCD palette_comb 28, 4, 1 @@ -505,7 +505,7 @@ Palettes: dw $0000, $4200, $037F, $7FFF dw $7FFF, $7E8C, $7C00, $0000 dw $7FFF, $1BEF, $6180, $0000 - ; Sameboy "Exclusives" + ; SameBoy "Exclusives" dw $7FFF, $7FEA, $7D5F, $0000 ; CGA 1 dw $4778, $3290, $1D87, $0861 ; DMG LCD @@ -522,7 +522,7 @@ KeyCombinationPalettes db 7 ; Left + B db 28 ; Up + B db 49 ; Down + B - ; Sameboy "Exclusives" + ; SameBoy "Exclusives" db 51 ; Right + A + B db 52 ; Left + A + B db 53 ; Up + A + B @@ -531,8 +531,8 @@ KeyCombinationPalettes TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c -SameboyLogo: - incbin "SameboyLogo.rle" +SameBoyLogo: + incbin "SameBoyLogo.rle" AnimationColors: dw $7FFF ; White @@ -636,7 +636,7 @@ ReadCGBLogoHalfTile: LoadTileset: ; Copy SameBoy Logo - ld de, SameboyLogo + ld de, SameBoyLogo ld hl, $8080 .sameboyLogoLoop ld a, [de] diff --git a/BootROMs/dmg_boot.asm b/BootROMs/dmg_boot.asm index 46b389a..6fb74fb 100644 --- a/BootROMs/dmg_boot.asm +++ b/BootROMs/dmg_boot.asm @@ -1,4 +1,4 @@ -; Sameboy CGB bootstrap ROM +; SameBoy DMG bootstrap ROM ; Todo: use friendly names for HW registers instead of magic numbers SECTION "BootCode", ROM0[$0] Start: diff --git a/BootROMs/sgb_boot.asm b/BootROMs/sgb_boot.asm index 108af18..cdb9d77 100644 --- a/BootROMs/sgb_boot.asm +++ b/BootROMs/sgb_boot.asm @@ -1,4 +1,4 @@ -; Sameboy CGB bootstrap ROM +; SameBoy SGB bootstrap ROM ; Todo: use friendly names for HW registers instead of magic numbers SECTION "BootCode", ROM0[$0] Start: diff --git a/Makefile b/Makefile index b4be355..ee1e56e 100644 --- a/Makefile +++ b/Makefile @@ -326,7 +326,7 @@ $(OBJ)/%.1bpp: %.png -@$(MKDIR) -p $(dir $@) rgbgfx -d 1 -h -o $@ $< -$(OBJ)/BootROMs/SameboyLogo.rle: $(OBJ)/BootROMs/SameboyLogo.1bpp build/logo-compress +$(OBJ)/BootROMs/SameBoyLogo.rle: $(OBJ)/BootROMs/SameBoyLogo.1bpp build/logo-compress build/logo-compress < $< > $@ build/logo-compress: BootROMs/logo-compress.c @@ -336,7 +336,7 @@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm -$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameboyLogo.rle +$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.rle -@$(MKDIR) -p $(dir $@) rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp From 5a04054145e7215c5afe74cce1fb35b3a50bab26 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 14 Jun 2019 14:34:02 +0300 Subject: [PATCH 042/341] Style changes --- BootROMs/logo-compress.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/BootROMs/logo-compress.c b/BootROMs/logo-compress.c index e4d2677..ff29143 100644 --- a/BootROMs/logo-compress.c +++ b/BootROMs/logo-compress.c @@ -2,13 +2,15 @@ #include #include -void pair(size_t count, uint8_t byte) { +void pair(size_t count, uint8_t byte) +{ static size_t unique_count = 0; static uint8_t unique_data[15]; if (count == 1) { unique_data[unique_count++] = byte; assert(unique_count <= 15); - } else { + } + else { assert(count <= 15); uint8_t control = (count << 4) | unique_count; putchar(control); @@ -19,7 +21,8 @@ void pair(size_t count, uint8_t byte) { if (count != 0) { putchar(byte); - } else { + } + else { assert(control == 0); } @@ -27,7 +30,8 @@ void pair(size_t count, uint8_t byte) { } } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ size_t count = 1; uint8_t byte = getchar(); int new; @@ -36,7 +40,8 @@ int main(int argc, char *argv[]) { while ((new = getchar()) != EOF) { if (byte == new) { count++; - } else { + } + else { pair(count, byte); byte = new; count = 1; From 66b814a226c596faf14ac6c1522bb52413e8d454 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 14 Jun 2019 16:49:41 +0300 Subject: [PATCH 043/341] =?UTF-8?q?Don=E2=80=99t=20use=20libc=E2=80=99s=20?= =?UTF-8?q?random/rand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/gb.c | 34 ++++++++++++++++++---------------- Core/random.c | 38 ++++++++++++++++++++++++++++++++++++++ Core/random.h | 12 ++++++++++++ Core/sgb.c | 3 ++- Tester/main.c | 9 +++------ libretro/Makefile | 1 - 6 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 Core/random.c create mode 100644 Core/random.h diff --git a/Core/gb.c b/Core/gb.c index 6c0bd6c..2f395a9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -9,8 +9,10 @@ #include #include #endif +#include "random.h" #include "gb.h" + #ifdef DISABLE_REWIND #define GB_rewind_free(...) #define GB_rewind_push(...) @@ -565,7 +567,7 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_CGB_E: case GB_MODEL_AGB: /* Unverified */ for (unsigned i = 0; i < gb->ram_size; i++) { - gb->ram[i] = (random() & 0xFF); + gb->ram[i] = GB_random(); } break; @@ -573,12 +575,12 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ for (unsigned i = 0; i < gb->ram_size; i++) { - gb->ram[i] = (random() & 0xFF); + gb->ram[i] = GB_random(); if (i & 0x100) { - gb->ram[i] &= random(); + gb->ram[i] &= GB_random(); } else { - gb->ram[i] |= random(); + gb->ram[i] |= GB_random(); } } break; @@ -586,7 +588,7 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_SGB2: for (unsigned i = 0; i < gb->ram_size; i++) { gb->ram[i] = 0x55; - gb->ram[i] ^= random() & random() & random(); + gb->ram[i] ^= GB_random() & GB_random() & GB_random(); } break; @@ -596,7 +598,7 @@ static void reset_ram(GB_gameboy_t *gb) gb->ram[i] = 0; } else { - gb->ram[i] = (random() | random() | random() | random()) & 0xFF; + gb->ram[i] = GB_random() | GB_random() | GB_random() | GB_random(); } } break; @@ -609,7 +611,7 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_CGB_E: case GB_MODEL_AGB: for (unsigned i = 0; i < sizeof(gb->hram); i++) { - gb->hram[i] = (random() & 0xFF); + gb->hram[i] = GB_random(); } break; @@ -619,10 +621,10 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_SGB2: for (unsigned i = 0; i < sizeof(gb->hram); i++) { if (i & 1) { - gb->hram[i] = random() | random() | random(); + gb->hram[i] = GB_random() | GB_random() | GB_random(); } else { - gb->hram[i] = random() & random() & random(); + gb->hram[i] = GB_random() & GB_random() & GB_random(); } } break; @@ -642,10 +644,10 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_SGB2: for (unsigned i = 0; i < 8; i++) { if (i & 2) { - gb->oam[i] = random() & random() & random(); + gb->oam[i] = GB_random() & GB_random() & GB_random(); } else { - gb->oam[i] = random() | random() | random(); + gb->oam[i] = GB_random() | GB_random() | GB_random(); } } for (unsigned i = 8; i < sizeof(gb->oam); i++) { @@ -669,10 +671,10 @@ static void reset_ram(GB_gameboy_t *gb) uint8_t temp; for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) { if (i & 1) { - temp = random() & random() & random(); + temp = GB_random() & GB_random() & GB_random(); } else { - temp = random() | random() | random(); + temp = GB_random() | GB_random() | GB_random(); } gb->apu.wave_channel.wave_form[i * 2] = temp >> 4; gb->apu.wave_channel.wave_form[i * 2 + 1] = temp & 0xF; @@ -684,13 +686,13 @@ static void reset_ram(GB_gameboy_t *gb) } for (unsigned i = 0; i < sizeof(gb->extra_oam); i++) { - gb->extra_oam[i] = (random() & 0xFF); + gb->extra_oam[i] = GB_random(); } if (GB_is_cgb(gb)) { for (unsigned i = 0; i < 64; i++) { - gb->background_palettes_data[i] = random() & 0xFF; /* Doesn't really matter as the boot ROM overrides it anyway*/ - gb->sprite_palettes_data[i] = random() & 0xFF; + gb->background_palettes_data[i] = GB_random(); /* Doesn't really matter as the boot ROM overrides it anyway*/ + gb->sprite_palettes_data[i] = GB_random(); } for (unsigned i = 0; i < 32; i++) { GB_palette_changed(gb, true, i * 2); diff --git a/Core/random.c b/Core/random.c new file mode 100644 index 0000000..cc4d4d3 --- /dev/null +++ b/Core/random.c @@ -0,0 +1,38 @@ +#include "random.h" +#include + +static uint64_t seed; +static bool enabled = true; + +uint8_t GB_random(void) +{ + if (!enabled) return 0; + + seed *= 0x27BB2EE687B0B0FDL; + seed += 0xB504F32D; + return seed >> 56; +} + +uint32_t GB_random32(void) +{ + GB_random(); + return seed >> 32; +} + +void GB_random_seed(uint64_t new_seed) +{ + seed = new_seed; +} + +void GB_random_set_enabled(bool enable) +{ + enabled = enable; +} + +static void __attribute__((constructor)) init_seed(void) +{ + seed = time(NULL); + for (unsigned i = 64; i--;) { + GB_random(); + } +} diff --git a/Core/random.h b/Core/random.h new file mode 100644 index 0000000..8ab0e50 --- /dev/null +++ b/Core/random.h @@ -0,0 +1,12 @@ +#ifndef random_h +#define random_h + +#include +#include + +uint8_t GB_random(void); +uint32_t GB_random32(void); +void GB_random_seed(uint64_t seed); +void GB_random_set_enabled(bool enable); + +#endif /* random_h */ diff --git a/Core/sgb.c b/Core/sgb.c index 9e7e382..6b1dbd0 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -1,4 +1,5 @@ #include "gb.h" +#include "random.h" #include #define INTRO_ANIMATION_LENGTH 200 @@ -682,7 +683,7 @@ static double fm_sweep(double phase) } static double random_double(void) { - return ((random() % 0x10001) - 0x8000) / (double) 0x8000; + return ((signed)(GB_random32() % 0x10001) - 0x8000) / (double) 0x8000; } bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) diff --git a/Tester/main.c b/Tester/main.c index d9ce165..dd126cb 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -16,12 +16,7 @@ #endif #include - -/* Disable all randomness during automatic tests */ -long random(void) -{ - return 0; -} +#include static bool running = false; static char *filename; @@ -262,6 +257,8 @@ int main(int argc, char **argv) bool dmg = false; const char *boot_rom_path = NULL; + + GB_random_set_enabled(false); for (unsigned i = 1; i < argc; i++) { if (strcmp(argv[i], "--dmg") == 0) { diff --git a/libretro/Makefile b/libretro/Makefile index fffe71c..75ddfc6 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -243,7 +243,6 @@ else CC = gcc TARGET := $(TARGET_NAME)_libretro.dll SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined - CFLAGS += -Drandom=rand endif TARGET := $(CORE_DIR)/build/bin/$(TARGET) From b2397a2e7a25e1f451a389671889c771d4c3b570 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 14 Jun 2019 18:06:15 +0300 Subject: [PATCH 044/341] Joystick hat support in Cocoa --- Cocoa/GBJoystickListener.h | 1 + Cocoa/GBPreferencesWindow.m | 36 ++++++++++++++++++++ Cocoa/GBView.m | 21 ++++++++++++ Cocoa/joypad.m | 68 +++++++++++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 2 deletions(-) diff --git a/Cocoa/GBJoystickListener.h b/Cocoa/GBJoystickListener.h index 690fde9..069db10 100644 --- a/Cocoa/GBJoystickListener.h +++ b/Cocoa/GBJoystickListener.h @@ -4,5 +4,6 @@ - (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state; - (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value; +- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) value; @end diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 982aa45..ecf0311 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -309,6 +309,42 @@ [self advanceConfigurationStateMachine]; } +- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state +{ + /* Hats are always mapped to the D-pad, ignore them on non-Dpad keys and skip the D-pad configuration if used*/ + if (!state) return; + if (joystick_configuration_state == -1) return; + if (joystick_configuration_state > GBDown) return; + if (!joystick_being_configured) { + joystick_being_configured = joystick_name; + } + else if (![joystick_being_configured isEqualToString:joystick_name]) { + return; + } + + NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; + + if (!all_mappings) { + all_mappings = [[NSMutableDictionary alloc] init]; + } + + NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; + + if (!mapping) { + mapping = [[NSMutableDictionary alloc] init]; + } + + for (joystick_configuration_state = 0;; joystick_configuration_state++) { + [mapping removeObjectForKey:GBButtonNames[joystick_configuration_state]]; + if (joystick_configuration_state == GBDown) break; + } + + all_mappings[joystick_name] = mapping; + [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; + [self refreshJoypadMenu:nil]; + [self advanceConfigurationStateMachine]; +} + - (NSButton *)aspectRatioCheckbox { return _aspectRatioCheckbox; diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 7e425c5..7f87901 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -341,6 +341,27 @@ } } +- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state +{ + unsigned player_count = GB_get_player_count(_gb); + + UpdateSystemActivity(UsrActivity); + for (unsigned player = 0; player < player_count; player++) { + NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] + objectForKey:[NSString stringWithFormat:@"%u", player]]; + if (player_count != 1 && // Single player, accpet inputs from all joypads + !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads + ![preferred_joypad isEqualToString:joystick_name]) { + continue; + } + assert(state + 1 < 9); + /* - N NE E SE S SW W NW */ + GB_set_key_state_for_player(_gb, GB_KEY_UP, player, (bool []){0, 1, 1, 0, 0, 0, 0, 0, 1}[state + 1]); + GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, (bool []){0, 0, 1, 1, 1, 0, 0, 0, 0}[state + 1]); + GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, (bool []){0, 0, 0, 0, 1, 1, 1, 0, 0}[state + 1]); + GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, (bool []){0, 0, 0, 0, 0, 0, 1, 1, 1}[state + 1]); + } +} - (BOOL)acceptsFirstResponder { diff --git a/Cocoa/joypad.m b/Cocoa/joypad.m index bca4097..2ffe56b 100755 --- a/Cocoa/joypad.m +++ b/Cocoa/joypad.m @@ -53,6 +53,9 @@ struct _SDL_Joystick int nbuttons; /* Number of buttons on the joystick */ uint8_t *buttons; /* Current button states */ + int nhats; + uint8_t *hats; + struct joystick_hwdata *hwdata; /* Driver dependent information */ int ref_count; /* Reference count for multiple opens */ @@ -93,11 +96,13 @@ struct joystick_hwdata int axes; /* number of axis (calculated, not reported by device) */ int buttons; /* number of buttons (calculated, not reported by device) */ + int hats; int elements; /* number of total elements (should be total of above) (calculated, not reported by device) */ recElement *firstAxis; recElement *firstButton; - + recElement *firstHat; + bool removed; int instance_id; @@ -178,6 +183,30 @@ void SDL_PrivateJoystickButton(SDL_Joystick *joystick, uint8_t button, uint8_t s } } +void SDL_PrivateJoystickHat(SDL_Joystick *joystick, uint8_t hat, uint8_t state) +{ + + /* Make sure we're not getting garbage or duplicate events */ + if (hat >= joystick->nhats) { + return; + } + if (state == joystick->hats[hat]) { + return; + } + + /* Update internal joystick state */ + joystick->hats[hat] = state; + + NSResponder *responder = (typeof(responder)) [[NSApp keyWindow] firstResponder]; + while (responder) { + if ([responder respondsToSelector:@selector(joystick:button:changedState:)]) { + [responder joystick:@(joystick->name) hat:hat changedState:state]; + break; + } + responder = (typeof(responder)) [responder nextResponder]; + } +} + static void FreeElementList(recElement *pElement) { @@ -202,6 +231,7 @@ FreeDevice(recDevice *removeDevice) /* free element lists */ FreeElementList(removeDevice->firstAxis); FreeElementList(removeDevice->firstButton); + FreeElementList(removeDevice->firstHat); free(removeDevice); } @@ -315,6 +345,15 @@ AddHIDElement(const void *value, void *parameter) } } break; + case kHIDUsage_GD_Hatswitch: + if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) { + element = (recElement *) calloc(1, sizeof (recElement)); + if (element) { + pDevice->hats++; + headElement = &(pDevice->firstHat); + } + } + break; case kHIDUsage_GD_DPadUp: case kHIDUsage_GD_DPadDown: @@ -535,6 +574,27 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) element = element->pNext; ++i; } + + element = device->firstHat; + i = 0; + while (element) { + signed range = (element->max - element->min + 1); + value = GetHIDElementState(device, element) - element->min; + if (range == 4) { /* 4 position hatswitch - scale up value */ + value *= 2; + } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */ + value = -1; + } + if ((unsigned)value >= 8) { + value = -1; + } + + SDL_PrivateJoystickHat(joystick, i, value); + + element = element->pNext; + ++i; + } + } static void JoystickInputCallback( @@ -579,13 +639,17 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic joystick->naxes = device->axes; joystick->nbuttons = device->buttons; - + joystick->nhats = device->hats; + if (joystick->naxes > 0) { joystick->axes = (SDL_JoystickAxisInfo *) calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo)); } if (joystick->nbuttons > 0) { joystick->buttons = (uint8_t *) calloc(joystick->nbuttons, 1); } + if (joystick->nhats > 0) { + joystick->hats = (uint8_t *) calloc(joystick->nhats, 1); + } /* Get notified when this device is disconnected. */ IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device); From 8b7922b6798ef51288e5400e7a2e78ab810656d4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Jun 2019 03:42:53 +0300 Subject: [PATCH 045/341] Fix #144 by ignored malformed commands with 0 length --- Core/sgb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/sgb.c b/Core/sgb.c index 6b1dbd0..0944985 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -98,6 +98,9 @@ static void command_ready(GB_gameboy_t *gb) return; } + /* Ignore malformed commands (0 length)*/ + if ((gb->sgb->command[0] & 7) == 0) return; + switch (gb->sgb->command[0] >> 3) { case PAL01: pal_command(gb, 0, 1); From 083b4a2970fe514e23e2e23927b682322299c578 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Jun 2019 12:53:00 +0300 Subject: [PATCH 046/341] Fix joypad hat input in the menu in the SDL port --- SDL/gui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL/gui.c b/SDL/gui.c index 70a449d..9fb5ab2 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -913,6 +913,7 @@ void run_gui(bool is_running) event.key.keysym.scancode = scancode; } } + break; } case SDL_JOYAXISMOTION: { From e268efefeff5720a6a3ae29a7b88283849c05076 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Jun 2019 23:22:27 +0300 Subject: [PATCH 047/341] Redesign and reimplement the audio API, let the frontends handle more stuff. Probably affects #161 --- Cocoa/Document.m | 69 ++++++++++++++++++++++++++++++++- Core/apu.c | 84 +++++++--------------------------------- Core/apu.h | 18 +++------ Core/debugger.c | 42 ++++++++++---------- Core/gb.c | 3 -- Core/gb.h | 2 +- Core/memory.c | 4 +- Core/sgb.c | 25 ++++++++---- Core/sgb.h | 1 - Core/timing.c | 6 +-- SDL/main.c | 19 ++++----- libretro/Makefile.common | 1 + libretro/libretro.c | 21 +++------- 13 files changed, 146 insertions(+), 149 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 731f7c8..b99e4c8 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -55,6 +55,12 @@ enum model { bool rewind; bool modelsChanging; + + NSCondition *audioLock; + GB_sample_t *audioBuffer; + size_t audioBufferSize; + size_t audioBufferPosition; + size_t audioBufferNeeded; } @property GBAudioClient *audioClient; @@ -67,6 +73,7 @@ enum model { - (void) printImage:(uint32_t *)image height:(unsigned) height topMargin:(unsigned) topMargin bottomMargin: (unsigned) bottomMargin exposure:(unsigned) exposure; +- (void) gotNewSample:(GB_sample_t *)sample; @end static void vblank(GB_gameboy_t *gb) @@ -118,6 +125,12 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, [self printImage:image height:height topMargin:top_margin bottomMargin:bottom_margin exposure:exposure]; } +static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + [self gotNewSample:sample]; +} + @implementation Document { GB_gameboy_t gb; @@ -133,6 +146,7 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, has_debugger_input = [[NSConditionLock alloc] initWithCondition:0]; debugger_input_queue = [[NSMutableArray alloc] init]; console_output_lock = [[NSRecursiveLock alloc] init]; + audioLock = [[NSCondition alloc] init]; } return self; } @@ -184,6 +198,7 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, GB_set_camera_update_request_callback(&gb, cameraRequestUpdate); GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]); GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]); + GB_apu_set_sample_callback(&gb, audioCallback); } - (void) vblank @@ -201,13 +216,57 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, } } +- (void)gotNewSample:(GB_sample_t *)sample +{ + [audioLock lock]; + if (self.audioClient.isPlaying) { + if (audioBufferPosition == audioBufferSize) { + if (audioBufferSize >= 0x4000) { + audioBufferPosition = 0; + [audioLock unlock]; + return; + } + + if (audioBufferSize == 0) { + audioBufferSize = 512; + } + else { + audioBufferSize += audioBufferSize >> 2; + } + audioBuffer = realloc(audioBuffer, sizeof(*sample) * audioBufferSize); + } + audioBuffer[audioBufferPosition++] = *sample; + } + if (audioBufferPosition == audioBufferNeeded) { + [audioLock signal]; + audioBufferNeeded = 0; + } + [audioLock unlock]; +} + - (void) run { running = true; GB_set_pixels_output(&gb, self.view.pixels); GB_set_sample_rate(&gb, 96000); self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) { - GB_apu_copy_buffer(&gb, buffer, nFrames); + [audioLock lock]; + + if (audioBufferPosition < nFrames) { + audioBufferNeeded = nFrames; + [audioLock wait]; + } + + if (audioBufferPosition >= nFrames && audioBufferPosition < nFrames + 4800) { + memcpy(buffer, audioBuffer, nFrames * sizeof(*buffer)); + memmove(audioBuffer, audioBuffer + nFrames, (audioBufferPosition - nFrames) * sizeof(*buffer)); + audioBufferPosition = audioBufferPosition - nFrames; + } + else { + memcpy(buffer, audioBuffer + (audioBufferPosition - nFrames), nFrames * sizeof(*buffer)); + audioBufferPosition = 0; + } + [audioLock unlock]; } andSampleRate:96000]; if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Mute"]) { [self.audioClient start]; @@ -227,6 +286,11 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, } } [hex_timer invalidate]; + [audioLock lock]; + memset(audioBuffer, 0, (audioBufferSize - audioBufferPosition) * sizeof(*audioBuffer)); + audioBufferPosition = audioBufferNeeded; + [audioLock signal]; + [audioLock unlock]; [self.audioClient stop]; self.audioClient = nil; self.view.mouseHidingEnabled = NO; @@ -329,6 +393,9 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, if (cameraImage) { CVBufferRelease(cameraImage); } + if (audioBuffer) { + free(audioBuffer); + } } - (void)windowControllerDidLoadNib:(NSWindowController *)aController { diff --git a/Core/apu.c b/Core/apu.c index 664530d..5e975cb 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "gb.h" #define likely(x) __builtin_expect((x), 1) @@ -117,7 +118,7 @@ static double smooth(double x) return 3*x*x - 2*x*x*x; } -static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) +static void render(GB_gameboy_t *gb) { GB_sample_t output = {0,0}; @@ -147,7 +148,7 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) } } - if (likely(gb->apu_output.last_update[i] == 0 || no_downsampling)) { + if (likely(gb->apu_output.last_update[i] == 0)) { output.left += gb->apu_output.current_sample[i].left * multiplier; output.right += gb->apu_output.current_sample[i].right * multiplier; } @@ -205,17 +206,9 @@ static void render(GB_gameboy_t *gb, bool no_downsampling, GB_sample_t *dest) } } - if (dest) { - *dest = filtered_output; - return; - } - - while (gb->apu_output.copy_in_progress); - while (!__sync_bool_compare_and_swap(&gb->apu_output.lock, false, true)); - if (gb->apu_output.buffer_position < gb->apu_output.buffer_size) { - gb->apu_output.buffer[gb->apu_output.buffer_position++] = filtered_output; - } - gb->apu_output.lock = false; + + assert(gb->apu_output.sample_callback); + gb->apu_output.sample_callback(gb, &filtered_output); } static uint16_t new_sweep_sample_legnth(GB_gameboy_t *gb) @@ -504,56 +497,12 @@ void GB_apu_run(GB_gameboy_t *gb) if (gb->apu_output.sample_rate) { gb->apu_output.cycles_since_render += cycles; - if (gb->apu_output.sample_cycles > gb->apu_output.cycles_per_sample) { + if (gb->apu_output.sample_cycles >= gb->apu_output.cycles_per_sample) { gb->apu_output.sample_cycles -= gb->apu_output.cycles_per_sample; - render(gb, false, NULL); + render(gb); } } } - -void GB_apu_copy_buffer(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) -{ - if (gb->sgb) { - if (GB_sgb_render_jingle(gb, dest, count)) return; - } - gb->apu_output.copy_in_progress = true; - - /* TODO: Rewrite this as a proper cyclic buffer. This is a workaround to avoid a very rare crashing race condition */ - size_t buffer_position = gb->apu_output.buffer_position; - - if (!gb->apu_output.stream_started) { - // Intentionally fail the first copy to sync the stream with the Gameboy. - gb->apu_output.stream_started = true; - gb->apu_output.buffer_position = 0; - buffer_position = 0; - } - - if (count > buffer_position) { - // GB_log(gb, "Audio underflow: %d\n", count - gb->apu_output.buffer_position); - GB_sample_t output; - render(gb, true, &output); - - for (unsigned i = 0; i < count - buffer_position; i++) { - dest[buffer_position + i] = output; - } - - if (buffer_position) { - if (gb->apu_output.buffer_size + (count - buffer_position) < count * 3) { - gb->apu_output.buffer_size += count - buffer_position; - gb->apu_output.buffer = realloc(gb->apu_output.buffer, - gb->apu_output.buffer_size * sizeof(*gb->apu_output.buffer)); - gb->apu_output.stream_started = false; - } - } - count = buffer_position; - } - memcpy(dest, gb->apu_output.buffer, count * sizeof(*gb->apu_output.buffer)); - memmove(gb->apu_output.buffer, gb->apu_output.buffer + count, (buffer_position - count) * sizeof(*gb->apu_output.buffer)); - gb->apu_output.buffer_position -= count; - - gb->apu_output.copy_in_progress = false; -} - void GB_apu_init(GB_gameboy_t *gb) { memset(&gb->apu, 0, sizeof(gb->apu)); @@ -1009,26 +958,21 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->io_registers[reg] = value; } -size_t GB_apu_get_current_buffer_length(GB_gameboy_t *gb) +void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate) { - return gb->apu_output.buffer_position; -} -void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate) -{ - if (gb->apu_output.buffer) { - free(gb->apu_output.buffer); - } - gb->apu_output.buffer_size = sample_rate / 25; // 40ms delay - gb->apu_output.buffer = malloc(gb->apu_output.buffer_size * sizeof(*gb->apu_output.buffer)); gb->apu_output.sample_rate = sample_rate; - gb->apu_output.buffer_position = 0; if (sample_rate) { gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate); } GB_apu_update_cycles_per_sample(gb); } +void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback) +{ + gb->apu_output.sample_callback = callback; +} + void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode) { gb->apu_output.highpass_mode = mode; diff --git a/Core/apu.h b/Core/apu.h index f8f56e9..7f8acfc 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -46,6 +46,8 @@ enum GB_CHANNELS { GB_N_CHANNELS }; +typedef void (*GB_sample_callback_t)(GB_gameboy_t *gb, GB_sample_t *sample); + typedef struct { bool global_enable; @@ -126,14 +128,6 @@ typedef enum { typedef struct { unsigned sample_rate; - GB_sample_t *buffer; - size_t buffer_size; - size_t buffer_position; - - bool stream_started; /* detects first copy request to minimize lag */ - volatile bool copy_in_progress; - volatile bool lock; - double sample_cycles; // In 8 MHz units double cycles_per_sample; @@ -147,13 +141,13 @@ typedef struct { GB_highpass_mode_t highpass_mode; double highpass_rate; GB_double_sample_t highpass_diff; + + GB_sample_callback_t sample_callback; } GB_apu_output_t; -void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate); -void GB_apu_copy_buffer(GB_gameboy_t *gb, GB_sample_t *dest, size_t count); -size_t GB_apu_get_current_buffer_length(GB_gameboy_t *gb); +void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate); void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode); - +void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback); #ifdef GB_INTERNAL bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index); void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value); diff --git a/Core/debugger.c b/Core/debugger.c index 79b5991..6874433 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -455,12 +455,12 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string, case 'p': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->pc}; } } - GB_log(gb, "Unknown register: %.*s\n", (unsigned int) length, string); + GB_log(gb, "Unknown register: %.*s\n", (unsigned) length, string); *error = true; return (lvalue_t){0,}; } - GB_log(gb, "Expression is not an lvalue: %.*s\n", (unsigned int) length, string); + GB_log(gb, "Expression is not an lvalue: %.*s\n", (unsigned) length, string); *error = true; return (lvalue_t){0,}; } @@ -564,8 +564,8 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } // Search for lowest priority operator signed int depth = 0; - unsigned int operator_index = -1; - unsigned int operator_pos = 0; + unsigned operator_index = -1; + unsigned operator_pos = 0; for (int i = 0; i < length; i++) { if (string[i] == '(') depth++; else if (string[i] == ')') depth--; @@ -593,7 +593,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } } if (operator_index != -1) { - unsigned int right_start = (unsigned int)(operator_pos + strlen(operators[operator_index].string)); + unsigned right_start = (unsigned)(operator_pos + strlen(operators[operator_index].string)); value_t right = debugger_evaluate(gb, string + right_start, length - right_start, error, watchpoint_address, watchpoint_new_value); if (*error) goto exit; if (operators[operator_index].lvalue_operator) { @@ -661,7 +661,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, goto exit; } - GB_log(gb, "Unknown register or symbol: %.*s\n", (unsigned int) length, string); + GB_log(gb, "Unknown register or symbol: %.*s\n", (unsigned) length, string); *error = true; goto exit; } @@ -675,7 +675,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } uint16_t literal = (uint16_t) (strtol(string, &end, base)); if (end != string + length) { - GB_log(gb, "Failed to parse: %.*s\n", (unsigned int) length, string); + GB_log(gb, "Failed to parse: %.*s\n", (unsigned) length, string); *error = true; goto exit; } @@ -880,13 +880,13 @@ static bool breakpoint(GB_gameboy_t *gb, char *arguments, char *modifiers, const condition += strlen(" if "); /* Verify condition is sane (Todo: This might have side effects!) */ bool error; - debugger_evaluate(gb, condition, (unsigned int)strlen(condition), &error, NULL, NULL); + debugger_evaluate(gb, condition, (unsigned)strlen(condition), &error, NULL, NULL); if (error) return true; } bool error; - value_t result = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint32_t key = BP_KEY(result); if (error) return true; @@ -949,7 +949,7 @@ static bool delete(GB_gameboy_t *gb, char *arguments, char *modifiers, const deb } bool error; - value_t result = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint32_t key = BP_KEY(result); if (error) return true; @@ -1065,13 +1065,13 @@ print_usage: /* To make $new and $old legal */ uint16_t dummy = 0; uint8_t dummy2 = 0; - debugger_evaluate(gb, condition, (unsigned int)strlen(condition), &error, &dummy, &dummy2); + debugger_evaluate(gb, condition, (unsigned)strlen(condition), &error, &dummy, &dummy2); if (error) return true; } bool error; - value_t result = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint32_t key = WP_KEY(result); if (error) return true; @@ -1132,7 +1132,7 @@ static bool unwatch(GB_gameboy_t *gb, char *arguments, char *modifiers, const de } bool error; - value_t result = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint32_t key = WP_KEY(result); if (error) return true; @@ -1234,7 +1234,7 @@ static bool _should_break(GB_gameboy_t *gb, value_t addr, bool jump_to) } bool error; bool condition = debugger_evaluate(gb, gb->breakpoints[index].condition, - (unsigned int)strlen(gb->breakpoints[index].condition), &error, NULL, NULL).value; + (unsigned)strlen(gb->breakpoints[index].condition), &error, NULL, NULL).value; if (error) { /* Should never happen */ GB_log(gb, "An internal error has occured\n"); @@ -1273,7 +1273,7 @@ static bool print(GB_gameboy_t *gb, char *arguments, char *modifiers, const debu } bool error; - value_t result = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); if (!error) { switch (modifiers[0]) { case 'a': @@ -1319,7 +1319,7 @@ static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const de } bool error; - value_t addr = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t addr = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint16_t count = 32; if (modifiers) { @@ -1371,7 +1371,7 @@ static bool disassemble(GB_gameboy_t *gb, char *arguments, char *modifiers, cons } bool error; - value_t addr = debugger_evaluate(gb, arguments, (unsigned int)strlen(arguments), &error, NULL, NULL); + value_t addr = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL); uint16_t count = 5; if (modifiers) { @@ -1468,7 +1468,7 @@ static bool backtrace(GB_gameboy_t *gb, char *arguments, char *modifiers, const } GB_log(gb, " 1. %s\n", debugger_value_to_string(gb, (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}, true)); - for (unsigned int i = gb->backtrace_size; i--;) { + for (unsigned i = gb->backtrace_size; i--;) { GB_log(gb, "%3d. %s\n", gb->backtrace_size - i + 1, debugger_value_to_string(gb, (value_t){true, gb->backtrace_returns[i].bank, gb->backtrace_returns[i].addr}, true)); } @@ -1937,7 +1937,7 @@ static bool _GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, value_t addr, u } bool error; bool condition = debugger_evaluate(gb, gb->watchpoints[index].condition, - (unsigned int)strlen(gb->watchpoints[index].condition), &error, &addr.value, &value).value; + (unsigned)strlen(gb->watchpoints[index].condition), &error, &addr.value, &value).value; if (error) { /* Should never happen */ GB_log(gb, "An internal error has occured\n"); @@ -1982,7 +1982,7 @@ static bool _GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, value_t addr) } bool error; bool condition = debugger_evaluate(gb, gb->watchpoints[index].condition, - (unsigned int)strlen(gb->watchpoints[index].condition), &error, &addr.value, NULL).value; + (unsigned)strlen(gb->watchpoints[index].condition), &error, &addr.value, NULL).value; if (error) { /* Should never happen */ GB_log(gb, "An internal error has occured\n"); @@ -2169,7 +2169,7 @@ void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path) } if (length == 0) continue; - unsigned int bank, address; + unsigned bank, address; char symbol[length]; if (sscanf(line, "%02x:%04x %s", &bank, &address, symbol) == 3) { diff --git a/Core/gb.c b/Core/gb.c index 2f395a9..cbd295c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -135,9 +135,6 @@ void GB_free(GB_gameboy_t *gb) if (gb->rom) { free(gb->rom); } - if (gb->apu_output.buffer) { - free(gb->apu_output.buffer); - } if (gb->breakpoints) { free(gb->breakpoints); } diff --git a/Core/gb.h b/Core/gb.h index 5385710..2d7d599 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -555,7 +555,7 @@ struct GB_gameboy_internal_s { uint16_t addr_for_call_depth[0x200]; /* Backtrace */ - unsigned int backtrace_size; + unsigned backtrace_size; uint16_t backtrace_sps[0x200]; struct { uint16_t bank; diff --git a/Core/memory.c b/Core/memory.c index 9104cbd..99ae190 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -126,13 +126,13 @@ static uint8_t read_rom(GB_gameboy_t *gb, uint16_t addr) if (!gb->rom_size) { return 0xFF; } - unsigned int effective_address = (addr & 0x3FFF) + gb->mbc_rom0_bank * 0x4000; + unsigned effective_address = (addr & 0x3FFF) + gb->mbc_rom0_bank * 0x4000; return gb->rom[effective_address & (gb->rom_size - 1)]; } static uint8_t read_mbc_rom(GB_gameboy_t *gb, uint16_t addr) { - unsigned int effective_address = (addr & 0x3FFF) + gb->mbc_rom_bank * 0x4000; + unsigned effective_address = (addr & 0x3FFF) + gb->mbc_rom_bank * 0x4000; return gb->rom[effective_address & (gb->rom_size - 1)]; } diff --git a/Core/sgb.c b/Core/sgb.c index 0944985..323e10f 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -1,6 +1,7 @@ #include "gb.h" #include "random.h" #include +#include #define INTRO_ANIMATION_LENGTH 200 @@ -461,8 +462,13 @@ static void render_boot_animation (GB_gameboy_t *gb) } } +static void render_jingle(GB_gameboy_t *gb, size_t count); void GB_sgb_render(GB_gameboy_t *gb) { + if (gb->apu_output.sample_rate) { + render_jingle(gb, gb->apu_output.sample_rate / (GB_get_clock_rate(gb) / LCDC_PERIOD)); + } + if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++; if (!gb->screen || !gb->rgb_encode_callback) return; @@ -689,7 +695,7 @@ static double random_double(void) return ((signed)(GB_random32() % 0x10001) - 0x8000) / (double) 0x8000; } -bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) +static void render_jingle(GB_gameboy_t *gb, size_t count) { const double frequencies[7] = { 466.16, // Bb4 @@ -701,15 +707,17 @@ bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) 1567.98, // G6 }; + assert(gb->apu_output.sample_callback); + if (gb->sgb->intro_animation < 0) { + GB_sample_t sample = {0, 0}; for (unsigned i = 0; i < count; i++) { - dest->left = dest->right = 0; - dest++; + gb->apu_output.sample_callback(gb, &sample); } - return true; + return; } - if (gb->sgb->intro_animation >= INTRO_ANIMATION_LENGTH) return false; + if (gb->sgb->intro_animation >= INTRO_ANIMATION_LENGTH) return; signed jingle_stage = (gb->sgb->intro_animation - 64) / 3; double sweep_cutoff_ratio = 2000.0 * pow(2, gb->sgb->intro_animation / 20.0) / gb->apu_output.sample_rate; @@ -718,6 +726,7 @@ bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) sweep_cutoff_ratio = 1; } + GB_sample_t stereo; for (unsigned i = 0; i < count; i++) { double sample = 0; for (signed f = 0; f < 7 && f < jingle_stage; f++) { @@ -738,10 +747,10 @@ bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count) sample += gb->sgb_intro_sweep_previous_sample * pow((120 - gb->sgb->intro_animation) / 120.0, 2) * 0.8; } - dest->left = dest->right = sample * 0x7000; - dest++; + stereo.left = stereo.right = sample * 0x7000; + gb->apu_output.sample_callback(gb, &stereo); } - return true; + return; } diff --git a/Core/sgb.h b/Core/sgb.h index 7e36198..2c6e8ee 100644 --- a/Core/sgb.h +++ b/Core/sgb.h @@ -54,7 +54,6 @@ struct GB_sgb_s { void GB_sgb_write(GB_gameboy_t *gb, uint8_t value); void GB_sgb_render(GB_gameboy_t *gb); void GB_sgb_load_default_data(GB_gameboy_t *gb); -bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count); #endif diff --git a/Core/timing.c b/Core/timing.c index b14a00b..5bc369b 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -8,7 +8,7 @@ #include #endif -static const unsigned int GB_TAC_TRIGGER_BITS[] = {512, 8, 32, 128}; +static const unsigned GB_TAC_TRIGGER_BITS[] = {512, 8, 32, 128}; #ifndef DISABLE_TIMEKEEPING static int64_t get_nanoseconds(void) @@ -251,8 +251,8 @@ void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac) /* Glitch only happens when old_tac is enabled. */ if (!(old_tac & 4)) return; - unsigned int old_clocks = GB_TAC_TRIGGER_BITS[old_tac & 3]; - unsigned int new_clocks = GB_TAC_TRIGGER_BITS[new_tac & 3]; + unsigned old_clocks = GB_TAC_TRIGGER_BITS[old_tac & 3]; + unsigned new_clocks = GB_TAC_TRIGGER_BITS[new_tac & 3]; /* The bit used for overflow testing must have been 1 */ if (gb->div_counter & old_clocks) { diff --git a/SDL/main.c b/SDL/main.c index be52afd..c0a0585 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -33,6 +33,7 @@ static double clock_mutliplier = 1.0; static char *filename = NULL; static typeof(free) *free_function = NULL; static char *battery_save_path_ptr; +static bool skip_audio; SDL_AudioDeviceID device_id; @@ -336,6 +337,8 @@ static void vblank(GB_gameboy_t *gb) } do_rewind = rewind_down; handle_events(gb); + + skip_audio = (SDL_GetQueuedAudioSize(device_id) / sizeof(GB_sample_t)) > have_aspec.freq / 20; } @@ -355,16 +358,12 @@ static void debugger_interrupt(int ignore) GB_debugger_break(&gb); } - -static void audio_callback(void *gb, Uint8 *stream, int len) +static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { - if (GB_is_inited(gb)) { - GB_apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t)); - } - else { - memset(stream, 0, len); - } + if (skip_audio) return; + SDL_QueueAudio(device_id, sample, sizeof(*sample)); } + static bool handle_pending_command(void) { @@ -436,6 +435,7 @@ restart: GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); GB_set_rewind_length(&gb, configuration.rewind_length); GB_set_update_input_hint_callback(&gb, handle_events); + GB_apu_set_sample_callback(&gb, gb_audio_callback); } SDL_DestroyTexture(texture); @@ -612,9 +612,6 @@ int main(int argc, char **argv) } #endif - - want_aspec.callback = audio_callback; - want_aspec.userdata = &gb; device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); /* Start Audio */ diff --git a/libretro/Makefile.common b/libretro/Makefile.common index fb3a02f..3bf1783 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -12,6 +12,7 @@ SOURCES_C := $(CORE_DIR)/Core/gb.c \ $(CORE_DIR)/Core/sm83_cpu.c \ $(CORE_DIR)/Core/joypad.c \ $(CORE_DIR)/Core/save_state.c \ + $(CORE_DIR)/Core/random.c \ $(CORE_DIR)/libretro/agb_boot.c \ $(CORE_DIR)/libretro/cgb_boot.c \ $(CORE_DIR)/libretro/dmg_boot.c \ diff --git a/libretro/libretro.c b/libretro/libretro.c index f5e4e2e..cec93f8 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -150,34 +150,22 @@ static void GB_update_keys_status(GB_gameboy_t *gb, unsigned port) } -static void audio_callback(void *gb) +static void audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { - size_t length = GB_apu_get_current_buffer_length(gb); - - while (length > sizeof(soundbuf) / 4) - { - GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, 1024); - audio_batch_cb(soundbuf, 1024); - length -= 1024; - } - if (length) { - GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, length); - audio_batch_cb(soundbuf, length); + if ((audio_out == GB_1 && gb == &gameboy[0]) || + (audio_out == GB_2 && gb == &gameboy[1])) { + audio_batch_cb((void*)sample, 1); } } static void vblank1(GB_gameboy_t *gb) { vblank1_occurred = true; - if (audio_out == GB_1) - audio_callback(gb); } static void vblank2(GB_gameboy_t *gb) { vblank2_occurred = true; - if (audio_out == GB_2) - audio_callback(gb); } static bool bit_to_send1 = true, bit_to_send2 = true; @@ -383,6 +371,7 @@ static void init_for_current_model(unsigned id) GB_set_pixels_output(&gameboy[i],(unsigned int*)(frame_buf + i * ((model[i] == MODEL_SGB || model[i] == MODEL_SGB2) ? SGB_VIDEO_PIXELS : VIDEO_PIXELS))); GB_set_rgb_encode_callback(&gameboy[i], rgb_encode); GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); + GB_apu_set_sample_callback(&gameboy[i], audio_callback); /* todo: attempt to make these more generic */ GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); From 431f1f8199166578b051651e6ff1c0cbd48766ab Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 18 Jun 2019 23:16:28 +0300 Subject: [PATCH 048/341] Remove redundant calls to display_vblank on non-SGB models and in irregular FPS scenarios. Affects #161 --- Core/display.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index 240e3c7..4972346 100644 --- a/Core/display.c +++ b/Core/display.c @@ -751,7 +751,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; gb->current_lcd_line++; // Todo: unverified timing - if (gb->current_lcd_line == LINES) { + if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { display_vblank(gb); } gb->fetcher_x = ((gb->io_registers[GB_IO_SCX]) / 8) & 0x1f; @@ -915,14 +915,18 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STAT_update(gb); if (gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { + if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { display_vblank(gb); + } gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; } else { gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; + if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { display_vblank(gb); } } + } GB_STAT_update(gb); GB_SLEEP(gb, display, 13, LINE_LENGTH - 4); From 280f609785b78d6d5f57ef4c86749cb22dd16250 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 19 Jun 2019 22:25:59 +0300 Subject: [PATCH 049/341] Fix under clock speed (Should have been 0.5, but ended up as ~0.4 due to rounding errors) --- Cocoa/GBView.m | 4 ++-- SDL/main.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 7f87901..5a851f3 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -148,11 +148,11 @@ - (void) flip { if (underclockKeyDown && clockMultiplier > 0.5) { - clockMultiplier -= 0.1; + clockMultiplier -= 1.0/16; GB_set_clock_multiplier(_gb, clockMultiplier); } if (!underclockKeyDown && clockMultiplier < 1.0) { - clockMultiplier += 0.1; + clockMultiplier += 1.0/16; GB_set_clock_multiplier(_gb, clockMultiplier); } current_buffer = (current_buffer + 1) % self.numberOfBuffers; diff --git a/SDL/main.c b/SDL/main.c index c0a0585..9dbea9b 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -318,11 +318,11 @@ static void handle_events(GB_gameboy_t *gb) static void vblank(GB_gameboy_t *gb) { if (underclock_down && clock_mutliplier > 0.5) { - clock_mutliplier -= 0.1; + clock_mutliplier -= 1.0/16; GB_set_clock_multiplier(gb, clock_mutliplier); } else if (!underclock_down && clock_mutliplier < 1.0) { - clock_mutliplier += 0.1; + clock_mutliplier += 1.0/16; GB_set_clock_multiplier(gb, clock_mutliplier); } if (configuration.blend_frames) { From 91b0e491c5d7867b0faf2b8f4a73a3673f9c6816 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 19 Jun 2019 22:44:54 +0300 Subject: [PATCH 050/341] Increase the minimum required cycles for a sync, fix SGB jingle audio --- Core/sgb.c | 2 +- Core/timing.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/sgb.c b/Core/sgb.c index 323e10f..89218ab 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -466,7 +466,7 @@ static void render_jingle(GB_gameboy_t *gb, size_t count); void GB_sgb_render(GB_gameboy_t *gb) { if (gb->apu_output.sample_rate) { - render_jingle(gb, gb->apu_output.sample_rate / (GB_get_clock_rate(gb) / LCDC_PERIOD)); + render_jingle(gb, gb->apu_output.sample_rate / (GB_get_clock_rate(gb) / (double)LCDC_PERIOD)); } if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++; diff --git a/Core/timing.c b/Core/timing.c index 5bc369b..138819f 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -59,7 +59,7 @@ void GB_timing_sync(GB_gameboy_t *gb) return; } /* Prevent syncing if not enough time has passed.*/ - if (gb->cycles_since_last_sync < LCDC_PERIOD / 4) return; + if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return; uint64_t target_nanoseconds = gb->cycles_since_last_sync * 1000000000LL / 2 / GB_get_clock_rate(gb); /* / 2 because we use 8MHz units */ int64_t nanoseconds = get_nanoseconds(); From 50a6a3e35c0599fc7cea4cf73ec99383a8492ef2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 19 Jun 2019 23:49:43 +0300 Subject: [PATCH 051/341] Fix libretro SGB1 FPS, fix un/serialization memory corruptions in libretro --- Core/gb.c | 5 +++ Core/gb.h | 2 +- Core/sgb.c | 2 +- libretro/libretro.c | 83 +++++++++++++++++++++++++-------------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index cbd295c..dec32a9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -896,3 +896,8 @@ void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_ca { gb->update_input_hint_callback = callback; } + +double GB_get_usual_frame_rate(GB_gameboy_t *gb) +{ + return GB_get_clock_rate(gb) / (double)LCDC_PERIOD; +} diff --git a/Core/gb.h b/Core/gb.h index 2d7d599..b943d85 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -695,7 +695,7 @@ void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier); unsigned GB_get_screen_width(GB_gameboy_t *gb); unsigned GB_get_screen_height(GB_gameboy_t *gb); - +double GB_get_usual_frame_rate(GB_gameboy_t *gb); unsigned GB_get_player_count(GB_gameboy_t *gb); #endif /* GB_h */ diff --git a/Core/sgb.c b/Core/sgb.c index 89218ab..5bcb72d 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -466,7 +466,7 @@ static void render_jingle(GB_gameboy_t *gb, size_t count); void GB_sgb_render(GB_gameboy_t *gb) { if (gb->apu_output.sample_rate) { - render_jingle(gb, gb->apu_output.sample_rate / (GB_get_clock_rate(gb) / (double)LCDC_PERIOD)); + render_jingle(gb, gb->apu_output.sample_rate / GB_get_usual_frame_rate(gb)); } if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++; diff --git a/libretro/libretro.c b/libretro/libretro.c index cec93f8..1dd411c 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -14,8 +14,6 @@ #define AUDIO_FREQUENCY 48000 #endif -#define FRAME_RATE (0x400000 / 70224.0) - #ifdef _WIN32 #include #include @@ -727,7 +725,7 @@ void retro_get_system_info(struct retro_system_info *info) void retro_get_system_av_info(struct retro_system_av_info *info) { struct retro_game_geometry geom; - struct retro_system_timing timing = { FRAME_RATE, AUDIO_FREQUENCY }; + struct retro_system_timing timing = { GB_get_usual_frame_rate(&gameboy[0]), AUDIO_FREQUENCY }; if (emulated_devices == 2) { @@ -947,11 +945,11 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_dual); check_variables(); - frame_buf = (uint32_t*)malloc(emulated_devices * VIDEO_PIXELS * sizeof(uint32_t)); - frame_buf_copy = (uint32_t*)malloc(emulated_devices * VIDEO_PIXELS * sizeof(uint32_t)); + frame_buf = (uint32_t*)malloc(emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); + frame_buf_copy = (uint32_t*)malloc(emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); - memset(frame_buf, 0, emulated_devices * VIDEO_PIXELS * sizeof(uint32_t)); - memset(frame_buf_copy, 0, emulated_devices * VIDEO_PIXELS * sizeof(uint32_t)); + memset(frame_buf, 0, emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); + memset(frame_buf_copy, 0, emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) @@ -987,54 +985,65 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t retro_serialize_size(void) { - if (emulated_devices == 2) - return GB_get_save_state_size(&gameboy[0]) + GB_get_save_state_size(&gameboy[1]); - else - return GB_get_save_state_size(&gameboy[0]); + static size_t maximum_save_size = 0; + if (maximum_save_size) { + return maximum_save_size * 2; + } + + GB_gameboy_t temp; + + GB_init(&temp, GB_MODEL_DMG_B); + maximum_save_size = GB_get_save_state_size(&temp); + GB_free(&temp); + + GB_init(&temp, GB_MODEL_CGB_E); + maximum_save_size = MAX(maximum_save_size, GB_get_save_state_size(&temp)); + GB_free(&temp); + + GB_init(&temp, GB_MODEL_SGB2); + maximum_save_size = MAX(maximum_save_size, GB_get_save_state_size(&temp)); + GB_free(&temp); + + return maximum_save_size * 2; } bool retro_serialize(void *data, size_t size) { - if (!initialized) + if (!initialized || !data) return false; - void* save_data[2]; - size_t state_size[2]; size_t offset = 0; - for (int i = 0; i < emulated_devices; i++) - { - state_size[i] = GB_get_save_state_size(&gameboy[i]); - save_data[i] = (uint8_t*)malloc(state_size[i]); - GB_save_state_to_buffer(&gameboy[i], (uint8_t*) save_data[i]); - memcpy(data + offset, save_data[i], state_size[i]); - offset += state_size[i]; - free(save_data[i]); + for (int i = 0; i < emulated_devices; i++) { + size_t state_size = GB_get_save_state_size(&gameboy[i]); + if (state_size > size) { + return false; + } + + GB_save_state_to_buffer(&gameboy[i], ((uint8_t *) data) + offset); + offset += state_size; + size -= state_size; } - if (data) - return true; - else - return false; + return true; } bool retro_unserialize(const void *data, size_t size) { - void* save_data[2]; - size_t state_size[2]; - int ret; - for (int i = 0; i < emulated_devices; i++) { - state_size[i] = GB_get_save_state_size(&gameboy[i]); - save_data[i] = (uint8_t*)malloc(state_size[i]); - memcpy (save_data[i], data + (state_size[i] * i), state_size[i]); - ret = GB_load_state_from_buffer(&gameboy[i], save_data[i], state_size[i]); - free(save_data[i]); - - if (ret != 0) + size_t state_size = GB_get_save_state_size(&gameboy[i]); + if (state_size > size) { return false; + } + + if (GB_load_state_from_buffer(&gameboy[i], data, state_size)) { + return false; + } + + size -= state_size; + data = ((uint8_t *)data) + state_size; } return true; From 72b1fe05001bdd0c5c0f3f49729ef708195f0e65 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 01:03:52 +0300 Subject: [PATCH 052/341] =?UTF-8?q?Minor=20Fixes=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cocoa/Document.xib | 12 ++++++------ Core/debugger.c | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index d558216..ae9cf90 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -397,8 +397,8 @@ - - + + @@ -430,8 +430,8 @@ - - + + @@ -448,8 +448,8 @@ - - + + diff --git a/Core/debugger.c b/Core/debugger.c index 6874433..d7fbdf8 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -819,7 +819,7 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */ (gb->f & GB_CARRY_FLAG)? 'C' : '-', (gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-', - (gb->f & GB_SUBSTRACT_FLAG)? 'S' : '-', + (gb->f & GB_SUBSTRACT_FLAG)? 'N' : '-', (gb->f & GB_ZERO_FLAG)? 'Z' : '-'); GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false)); GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false)); @@ -1766,9 +1766,9 @@ static const debugger_command_t commands[] = { {"cartridge", 2, mbc, "Displays information about the MBC and cartridge"}, {"mbc", 3, }, /* Alias */ {"apu", 3, apu, "Displays information about the current state of the audio chip"}, - {"wave", 3, wave, "Prints a visual representation of the wave RAM" HELP_NEWLINE + {"wave", 3, wave, "Prints a visual representation of the wave RAM." HELP_NEWLINE "Modifiers can be used for a (f)ull print (the default)," HELP_NEWLINE - "a more (c)ompact one, or a one-(l)iner"}, + "a more (c)ompact one, or a one-(l)iner", "", "(f|c|l)"}, {"lcd", 3, lcd, "Displays information about the current state of the LCD controller"}, {"palettes", 3, palettes, "Displays the current CGB palettes"}, {"breakpoint", 1, breakpoint, "Add a new breakpoint at the specified address/expression" HELP_NEWLINE From 7c61445fe3203c591429d8d123cad6191aa9874f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 03:04:38 +0300 Subject: [PATCH 053/341] Fixed out of bound read in GB_load_state_from_buffer. Closes #104 --- Core/save_state.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/save_state.c b/Core/save_state.c index 1cd3458..5a7d920 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -294,6 +294,8 @@ static bool buffer_read_section(const uint8_t **buffer, size_t *buffer_length, v return false; } + if (saved_size > *buffer_length) return false; + if (saved_size <= size) { if (buffer_read(dest, saved_size, buffer, buffer_length) != saved_size) { return false; From 24b58da8c6dd05c8d138fd55f482f64d7265c593 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 14:18:48 +0300 Subject: [PATCH 054/341] Minor text change --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index d7fbdf8..357a896 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1775,7 +1775,7 @@ static const debugger_command_t commands[] = { "Can also modify the condition of existing breakpoints." HELP_NEWLINE "If the j modifier is used, the breakpoint will occur just before" HELP_NEWLINE "jumping to the target.", - "[ if ]", "(j)"}, + "[ if ]", "j"}, {"delete", 2, delete, "Delete a breakpoint by its address, or all breakpoints", "[]"}, {"watch", 1, watch, "Add a new watchpoint at the specified address/expression." HELP_NEWLINE "Can also modify the condition and type of existing watchpoints." HELP_NEWLINE From 36a87f96bd800fb07f33cb5e4b41a3c27354a88b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 16:58:56 +0300 Subject: [PATCH 055/341] Formatting --- Core/display.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/display.c b/Core/display.c index 4972346..cf53216 100644 --- a/Core/display.c +++ b/Core/display.c @@ -916,17 +916,17 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) if (gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON) { if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { - display_vblank(gb); + display_vblank(gb); } gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; } else { gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { - display_vblank(gb); + display_vblank(gb); + } } } - } GB_STAT_update(gb); GB_SLEEP(gb, display, 13, LINE_LENGTH - 4); From 72d1d9b1540aaf9b3973bc2dcb2f3dae6983f3bd Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 17:08:10 +0300 Subject: [PATCH 056/341] Fix Windows build --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ee1e56e..f666d7f 100644 --- a/Makefile +++ b/Makefile @@ -327,7 +327,7 @@ $(OBJ)/%.1bpp: %.png rgbgfx -d 1 -h -o $@ $< $(OBJ)/BootROMs/SameBoyLogo.rle: $(OBJ)/BootROMs/SameBoyLogo.1bpp build/logo-compress - build/logo-compress < $< > $@ + ./build/logo-compress < $< > $@ build/logo-compress: BootROMs/logo-compress.c $(CC) $< -o $@ From b478b5b568a1b3bbbd3c9547a74917c6aae36c48 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 17:20:18 +0300 Subject: [PATCH 057/341] Fix bugged mouse support on some platforms --- SDL/gui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL/gui.c b/SDL/gui.c index 9fb5ab2..0723384 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -883,6 +883,7 @@ void run_gui(bool is_running) } } + break; case SDL_JOYBUTTONDOWN: event.type = SDL_KEYDOWN; joypad_button_t button = get_joypad_button(event.jbutton.button); From f1b578fd2ee9d5c140b0dcaedfa1a2af9ed6278c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Jun 2019 19:20:51 +0300 Subject: [PATCH 058/341] Update version to 0.12, update copyright year --- Cocoa/Info.plist | 2 +- Cocoa/License.html | 2 +- LICENSE | 2 +- Makefile | 2 +- QuickLook/Info.plist | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cocoa/Info.plist b/Cocoa/Info.plist index 65f0df8..dd801cb 100644 --- a/Cocoa/Info.plist +++ b/Cocoa/Info.plist @@ -70,7 +70,7 @@ LSMinimumSystemVersion 10.9 NSHumanReadableCopyright - Copyright © 2015-2018 Lior Halphon + Copyright © 2015-2019 Lior Halphon NSMainNibFile MainMenu NSPrincipalClass diff --git a/Cocoa/License.html b/Cocoa/License.html index 2673143..49851fd 100644 --- a/Cocoa/License.html +++ b/Cocoa/License.html @@ -30,7 +30,7 @@

SameBoy

MIT License

-

Copyright © 2015-2018 Lior Halphon

+

Copyright © 2015-2019 Lior Halphon

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/LICENSE b/LICENSE index 63c6787..94966be 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2015-2018 Lior Halphon +Copyright (c) 2015-2019 Lior Halphon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile index f666d7f..4bc4f3d 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.11.1 +VERSION := 0.12 export VERSION CONF ?= debug diff --git a/QuickLook/Info.plist b/QuickLook/Info.plist index 051e26c..9540214 100644 --- a/QuickLook/Info.plist +++ b/QuickLook/Info.plist @@ -47,7 +47,7 @@ CFPlugInUnloadFunction NSHumanReadableCopyright - Copyright © 2015-2018 Lior Halphon + Copyright © 2015-2019 Lior Halphon QLNeedsToBeRunInMainThread QLPreviewHeight From 970a5f562b6092f697233dac7e03497908bd710a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Jun 2019 18:16:55 +0300 Subject: [PATCH 059/341] Fix #183 --- BootROMs/logo-compress.c | 13 +++++++++++-- Makefile | 9 ++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/BootROMs/logo-compress.c b/BootROMs/logo-compress.c index ff29143..2274eb2 100644 --- a/BootROMs/logo-compress.c +++ b/BootROMs/logo-compress.c @@ -1,6 +1,10 @@ #include #include -#include +#include +#ifdef _WIN32 +#include +#include +#endif void pair(size_t count, uint8_t byte) { @@ -35,7 +39,12 @@ int main(int argc, char *argv[]) size_t count = 1; uint8_t byte = getchar(); int new; - size_t position = 0; + size_t position = 0; + +#ifdef _WIN32 + _setmode(0,_O_BINARY); + _setmode(1,_O_BINARY); +#endif while ((new = getchar()) != EOF) { if (byte == new) { diff --git a/Makefile b/Makefile index 4bc4f3d..0246616 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,11 @@ ifneq ($(findstring MSYS,$(PLATFORM)),) PLATFORM := windows32 endif +LOGO_COMPRESS := build/logo-compress + ifeq ($(PLATFORM),windows32) _ := $(shell chcp 65001) +LOGO_COMPRESS := build/logo-compress.exe endif ifeq ($(PLATFORM),Darwin) @@ -326,10 +329,10 @@ $(OBJ)/%.1bpp: %.png -@$(MKDIR) -p $(dir $@) rgbgfx -d 1 -h -o $@ $< -$(OBJ)/BootROMs/SameBoyLogo.rle: $(OBJ)/BootROMs/SameBoyLogo.1bpp build/logo-compress - ./build/logo-compress < $< > $@ +$(OBJ)/BootROMs/SameBoyLogo.rle: $(OBJ)/BootROMs/SameBoyLogo.1bpp $(LOGO_COMPRESS) + $(realpath $(LOGO_COMPRESS)) < $< > $@ -build/logo-compress: BootROMs/logo-compress.c +$(LOGO_COMPRESS): BootROMs/logo-compress.c $(CC) $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm From 080fde08b6fd0f4bc2af05345c8dd2f1ef239cce Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Jun 2019 19:01:35 +0300 Subject: [PATCH 060/341] Improve audio quality on the SDL port by being more forgiving to system with bigger buffer sizes --- SDL/main.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index 9dbea9b..e0b40e8 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -33,7 +33,6 @@ static double clock_mutliplier = 1.0; static char *filename = NULL; static typeof(free) *free_function = NULL; static char *battery_save_path_ptr; -static bool skip_audio; SDL_AudioDeviceID device_id; @@ -337,8 +336,6 @@ static void vblank(GB_gameboy_t *gb) } do_rewind = rewind_down; handle_events(gb); - - skip_audio = (SDL_GetQueuedAudioSize(device_id) / sizeof(GB_sample_t)) > have_aspec.freq / 20; } @@ -360,7 +357,9 @@ static void debugger_interrupt(int ignore) static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { - if (skip_audio) return; + if ((SDL_GetQueuedAudioSize(device_id) / sizeof(GB_sample_t)) > have_aspec.freq / 12) { + return; + } SDL_QueueAudio(device_id, sample, sizeof(*sample)); } From 23229f1118f42b76cdbb650961aa62b48ebb846a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Jun 2019 19:12:12 +0300 Subject: [PATCH 061/341] Update version to 0.12.1 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0246616..401fa3a 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.12 +VERSION := 0.12.1 export VERSION CONF ?= debug From 72be66414d82495b28637f1f248aed50879c41ba Mon Sep 17 00:00:00 2001 From: Aaron Kling Date: Thu, 23 May 2019 15:54:39 -0500 Subject: [PATCH 062/341] libretro: jni: Switch stl to c++ in preparation for ndk r20 --- libretro/jni/Application.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretro/jni/Application.mk b/libretro/jni/Application.mk index 219fdf2..a169e74 100644 --- a/libretro/jni/Application.mk +++ b/libretro/jni/Application.mk @@ -1,2 +1,2 @@ -APP_STL := gnustl_static +APP_STL := c++_static APP_ABI := all From 4541efe86ab169cd6dc9895e44f7d830ff8e3c02 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 29 Jun 2019 14:03:42 +0300 Subject: [PATCH 063/341] =?UTF-8?q?Fixed=20a=20bug=20that=20prevented=20wr?= =?UTF-8?q?iting=20to=20the=20wave=20RAM,=20as=20well=20as=20a=20bug=20whe?= =?UTF-8?q?re=20the=20wave=20RAM=20was=20treated=20as=20zeros=20despite=20?= =?UTF-8?q?not=20being=20zero=E2=80=99d=20out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 5e975cb..ee58138 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -506,6 +506,11 @@ void GB_apu_run(GB_gameboy_t *gb) void GB_apu_init(GB_gameboy_t *gb) { memset(&gb->apu, 0, sizeof(gb->apu)); + /* Restore the wave form */ + for (unsigned reg = GB_IO_WAV_START; reg <= GB_IO_WAV_END; reg++) { + gb->apu.wave_channel.wave_form[(reg - GB_IO_WAV_START) * 2] = gb->io_registers[reg] >> 4; + gb->apu.wave_channel.wave_form[(reg - GB_IO_WAV_START) * 2 + 1] = gb->io_registers[reg] & 0xF; + } gb->apu.lf_div = 1; /* APU glitch: When turning the APU on while DIV's bit 4 (or 5 in double speed mode) is on, the first DIV/APU event is skipped. */ @@ -556,14 +561,14 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg) void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) { - if (!gb->apu.global_enable && reg != GB_IO_NR52 && (GB_is_cgb(gb) || - ( - reg != GB_IO_NR11 && - reg != GB_IO_NR21 && - reg != GB_IO_NR31 && - reg != GB_IO_NR41 - ) - )) { + if (!gb->apu.global_enable && reg != GB_IO_NR52 && reg < GB_IO_WAV_START && (GB_is_cgb(gb) || + ( + reg != GB_IO_NR11 && + reg != GB_IO_NR21 && + reg != GB_IO_NR31 && + reg != GB_IO_NR41 + ) + )) { return; } From 6b06d07bcc204d4ef05a358a60243f548e1e068b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 12 Jul 2019 01:53:06 +0300 Subject: [PATCH 064/341] More attempts to improve audio in the SDL frontend --- SDL/main.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index e0b40e8..df6837f 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -94,6 +94,7 @@ static void open_menu(void) } run_gui(true); if (audio_playing) { + SDL_ClearQueuedAudio(device_id); SDL_PauseAudioDevice(device_id, 0); } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); @@ -130,6 +131,7 @@ static void handle_events(GB_gameboy_t *gb) GB_set_key_state(gb, (GB_key_t) button, event.type == SDL_JOYBUTTONDOWN); } else if (button == JOYPAD_BUTTON_TURBO) { + SDL_ClearQueuedAudio(device_id); turbo_down = event.type == SDL_JOYBUTTONDOWN; GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down); } @@ -239,7 +241,6 @@ static void handle_events(GB_gameboy_t *gb) paused = !paused; } break; - case SDL_SCANCODE_M: if (event.key.keysym.mod & MODIFIER) { #ifdef __APPLE__ @@ -253,6 +254,7 @@ static void handle_events(GB_gameboy_t *gb) SDL_PauseAudioDevice(device_id, 1); } else if (!audio_playing) { + SDL_ClearQueuedAudio(device_id); SDL_PauseAudioDevice(device_id, 0); } } @@ -288,6 +290,7 @@ static void handle_events(GB_gameboy_t *gb) case SDL_KEYUP: // Fallthrough if (event.key.keysym.scancode == configuration.keys[8]) { turbo_down = event.type == SDL_KEYDOWN; + SDL_ClearQueuedAudio(device_id); GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down); } else if (event.key.keysym.scancode == configuration.keys_2[0]) { @@ -356,11 +359,24 @@ static void debugger_interrupt(int ignore) } static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) -{ - if ((SDL_GetQueuedAudioSize(device_id) / sizeof(GB_sample_t)) > have_aspec.freq / 12) { +{ + if (turbo_down) { + static unsigned skip = 0; + skip++; + if (skip == have_aspec.freq / 8) { + skip = 0; + } + if (skip > have_aspec.freq / 16) { + return; + } + } + + if (SDL_GetQueuedAudioSize(device_id) / sizeof(*sample) > have_aspec.freq / 4) { return; } + SDL_QueueAudio(device_id, sample, sizeof(*sample)); + } @@ -599,20 +615,14 @@ int main(int argc, char **argv) want_aspec.samples = 2048; } #else - if (sdl_version >= 2006) { - /* SDL 2.0.6 offers WASAPI support which allows for much lower audio buffer lengths which at least - theoretically reduces lagging. */ - want_aspec.samples = 32; - } - else { + if (sdl_version < 2006) { /* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency to 44100 because otherwise we would get garbled audio output.*/ want_aspec.freq = 44100; } #endif - device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); - + device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE); /* Start Audio */ SDL_EventState(SDL_DROPFILE, SDL_ENABLE); From 30a58ecd5cc3e40fe6f0996cd97d6beff7f9141a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 12 Jul 2019 01:53:20 +0300 Subject: [PATCH 065/341] Use color correction in the QL previewer --- QuickLook/get_image_for_rom.c | 1 + 1 file changed, 1 insertion(+) diff --git a/QuickLook/get_image_for_rom.c b/QuickLook/get_image_for_rom.c index e0482ec..3950dac 100755 --- a/QuickLook/get_image_for_rom.c +++ b/QuickLook/get_image_for_rom.c @@ -58,6 +58,7 @@ int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *out GB_set_rgb_encode_callback(&gb, rgb_encode); GB_set_async_input_callback(&gb, async_input_callback); GB_set_log_callback(&gb, log_callback); + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); if (GB_load_rom(&gb, filename)) { GB_free(&gb); From f55c2549595e9380f005f1133f043d3d98db0fe1 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 12 Jul 2019 02:18:25 +0300 Subject: [PATCH 066/341] Fixed a regression that made ly_lyc_0_write and ly_lyc_write fail --- Core/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/memory.c b/Core/memory.c index 99ae190..868a245 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -663,7 +663,7 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* These are the states when LY changes, let the display routine call GB_STAT_update for use so it correctly handles T-cycle accurate LYC writes */ if (!GB_is_cgb(gb) || ( - gb->display_state != 6 && + gb->display_state != 35 && gb->display_state != 26 && gb->display_state != 15 && gb->display_state != 16)) { From 8c8d5afe6230433adbb5d27902980cfde848b9be Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jul 2019 17:17:55 +0300 Subject: [PATCH 067/341] Make the debugger compatible with more sym formats --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 357a896..df480f3 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -2172,7 +2172,7 @@ void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path) unsigned bank, address; char symbol[length]; - if (sscanf(line, "%02x:%04x %s", &bank, &address, symbol) == 3) { + if (sscanf(line, "%x:%x %s", &bank, &address, symbol) == 3) { bank &= 0x1FF; if (!gb->bank_symbols[bank]) { gb->bank_symbols[bank] = GB_map_alloc(); From 9f7255cd239c55af4356d76e22e1c57d20c164ba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jul 2019 20:29:11 +0300 Subject: [PATCH 068/341] Make the automation results more consistent across revisions, and making use of this change as a chance to add color correction to the automation --- Core/gb.h | 2 +- Tester/main.c | 36 ++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index b943d85..7cebbce 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -623,7 +623,7 @@ void GB_free(GB_gameboy_t *gb); void GB_reset(GB_gameboy_t *gb); void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model); -/* Returns the time passed, in 4MHz ticks. */ +/* Returns the time passed, in 8MHz ticks. */ uint8_t GB_run(GB_gameboy_t *gb); /* Returns the time passed since the last frame, in nanoseconds */ uint64_t GB_run_frame(GB_gameboy_t *gb); diff --git a/Tester/main.c b/Tester/main.c index dd126cb..03b9985 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -50,12 +50,12 @@ static char *async_input_callback(GB_gameboy_t *gb) return NULL; } -static void vblank(GB_gameboy_t *gb) +static void handle_buttons(GB_gameboy_t *gb) { /* Do not press any buttons during the last two seconds, this might cause a - screenshot to be taken while the LCD is off if the press makes the game - load graphics. */ - if (push_start_a && (frames < test_length - 120 || do_not_stop)) { + screenshot to be taken while the LCD is off if the press makes the game + load graphics. */ + if (push_start_a && (frames < test_length - 120 || do_not_stop)) { unsigned combo_length = 40; if (start_is_not_first || push_a_twice) combo_length = 60; /* The start item in the menu is not the first, so also push down */ else if (a_is_bad || start_is_bad) combo_length = 20; /* Pressing A has a negative effect (when trying to start the game). */ @@ -109,7 +109,11 @@ static void vblank(GB_gameboy_t *gb) } } } - + +} + +static void vblank(GB_gameboy_t *gb) +{ /* Detect common crashes and stop the test early */ if (frames < test_length - 1) { if (gb->backtrace_size >= 0x200 + (large_stack? 0x80: 0) || (!allow_weird_sp_values && (gb->registers[GB_REGISTER_SP] >= 0xfe00 && gb->registers[GB_REGISTER_SP] < 0xff80))) { @@ -123,7 +127,7 @@ static void vblank(GB_gameboy_t *gb) } } - if (frames >= test_length ) { + if (frames >= test_length && !gb->disable_rendering) { bool is_screen_blank = true; for (unsigned i = 160*144; i--;) { if (bitmap[i] != bitmap[0]) { @@ -147,11 +151,9 @@ static void vblank(GB_gameboy_t *gb) running = false; } } - else if (frames == test_length - 1) { + else if (frames >= test_length - 1) { gb->disable_rendering = false; } - - frames++; } static void log_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes) @@ -339,6 +341,7 @@ int main(int argc, char **argv) GB_set_rgb_encode_callback(&gb, rgb_encode); GB_set_log_callback(&gb, log_callback); GB_set_async_input_callback(&gb, async_input_callback); + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); if (GB_load_rom(&gb, filename)) { perror("Failed to load ROM"); @@ -353,11 +356,14 @@ int main(int argc, char **argv) /* Restarting in Puzzle Boy/Kwirk (Start followed by A) leaks stack. */ strcmp((const char *)(gb.rom + 0x134), "KWIRK") == 0 || strcmp((const char *)(gb.rom + 0x134), "PUZZLE BOY") == 0; - start_is_bad = strcmp((const char *)(gb.rom + 0x134), "BLUESALPHA") == 0; + start_is_bad = strcmp((const char *)(gb.rom + 0x134), "BLUESALPHA") == 0 || + strcmp((const char *)(gb.rom + 0x134), "ONI 5") == 0; b_is_confirm = strcmp((const char *)(gb.rom + 0x134), "ELITE SOCCER") == 0 || strcmp((const char *)(gb.rom + 0x134), "SOCCER") == 0 || strcmp((const char *)(gb.rom + 0x134), "GEX GECKO") == 0; - push_faster = strcmp((const char *)(gb.rom + 0x134), "MOGURA DE PON!") == 0; + push_faster = strcmp((const char *)(gb.rom + 0x134), "MOGURA DE PON!") == 0 || + strcmp((const char *)(gb.rom + 0x134), "HUGO2 1/2") == 0 || + strcmp((const char *)(gb.rom + 0x134), "HUGO") == 0; push_slower = strcmp((const char *)(gb.rom + 0x134), "BAKENOU") == 0; do_not_stop = strcmp((const char *)(gb.rom + 0x134), "SPACE INVADERS") == 0; push_right = memcmp((const char *)(gb.rom + 0x134), "BOB ET BOB", strlen("BOB ET BOB")) == 0 || @@ -397,8 +403,14 @@ int main(int argc, char **argv) running = true; gb.turbo = gb.turbo_dont_skip = gb.disable_rendering = true; frames = 0; + unsigned cycles = 0; while (running) { - GB_run(&gb); + cycles += GB_run(&gb); + if (cycles >= 139810) { /* Approximately 1/60 a second. Intentionally not the actual length of a frame. */ + handle_buttons(&gb); + cycles -= 139810; + frames++; + } /* This early crash test must not run in vblank because PC might not point to the next instruction. */ if (gb.pc == 0x38 && frames < test_length - 1 && GB_read_memory(&gb, 0x38) == 0xFF) { GB_log(&gb, "The game is probably stuck in an FF loop.\n"); From 2bfe9226501191a6ab355792b98b343816843830 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 15 Jul 2019 20:47:16 +0300 Subject: [PATCH 069/341] Allow emulating an SGB without SFC HLE --- Core/display.c | 4 ++-- Core/gb.c | 13 +++++++++---- Core/gb.h | 8 ++++++-- Core/save_state.c | 14 +++++++------- Core/sgb.c | 4 ++++ 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Core/display.c b/Core/display.c index cf53216..cdcdeb2 100644 --- a/Core/display.c +++ b/Core/display.c @@ -123,8 +123,8 @@ static void display_vblank(GB_gameboy_t *gb) { gb->vblank_just_occured = true; - /* TODO: Slow in trubo mode! */ - if (GB_is_sgb(gb)) { + /* TODO: Slow in turbo mode! */ + if (GB_is_hle_sgb(gb)) { GB_sgb_render(gb); } diff --git a/Core/gb.c b/Core/gb.c index dec32a9..6f846ca 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -533,6 +533,11 @@ bool GB_is_cgb(GB_gameboy_t *gb) } bool GB_is_sgb(GB_gameboy_t *gb) +{ + return (gb->model & ~GB_MODEL_PAL_BIT & ~GB_MODEL_NO_SFC_BIT) == GB_MODEL_SGB || (gb->model & ~GB_MODEL_NO_SFC_BIT) == GB_MODEL_SGB2; +} + +bool GB_is_hle_sgb(GB_gameboy_t *gb) { return (gb->model & ~GB_MODEL_PAL_BIT) == GB_MODEL_SGB || gb->model == GB_MODEL_SGB2; } @@ -745,7 +750,7 @@ void GB_reset(GB_gameboy_t *gb) gb->accessed_oam_row = -1; - if (GB_is_sgb(gb)) { + if (GB_is_hle_sgb(gb)) { if (!gb->sgb) { gb->sgb = malloc(sizeof(*gb->sgb)); } @@ -879,17 +884,17 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb) unsigned GB_get_screen_width(GB_gameboy_t *gb) { - return GB_is_sgb(gb)? 256 : 160; + return GB_is_hle_sgb(gb)? 256 : 160; } unsigned GB_get_screen_height(GB_gameboy_t *gb) { - return GB_is_sgb(gb)? 224 : 144; + return GB_is_hle_sgb(gb)? 224 : 144; } unsigned GB_get_player_count(GB_gameboy_t *gb) { - return GB_is_sgb(gb)? gb->sgb->player_count : 1; + return GB_is_hle_sgb(gb)? gb->sgb->player_count : 1; } void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback) diff --git a/Core/gb.h b/Core/gb.h index 7cebbce..6d10283 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -29,6 +29,7 @@ #define GB_MODEL_MGB_FAMILY 0x100 #define GB_MODEL_CGB_FAMILY 0x200 #define GB_MODEL_PAL_BIT 0x1000 +#define GB_MODEL_NO_SFC_BIT 0x2000 #if __clang__ #define UNROLL _Pragma("unroll") @@ -67,9 +68,11 @@ typedef enum { // GB_MODEL_DMG_C = 0x003, GB_MODEL_SGB = 0x004, GB_MODEL_SGB_NTSC = GB_MODEL_SGB, - GB_MODEL_SGB_PAL = 0x1004, + GB_MODEL_SGB_PAL = GB_MODEL_SGB | GB_MODEL_PAL_BIT, + GB_MODEL_SGB_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT, // GB_MODEL_MGB = 0x100, GB_MODEL_SGB2 = 0x101, + GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT, // GB_MODEL_CGB_0 = 0x200, // GB_MODEL_CGB_A = 0x201, // GB_MODEL_CGB_B = 0x202, @@ -617,7 +620,8 @@ __attribute__((__format__ (__printf__, fmtarg, firstvararg))) void GB_init(GB_gameboy_t *gb, GB_model_t model); bool GB_is_inited(GB_gameboy_t *gb); bool GB_is_cgb(GB_gameboy_t *gb); -bool GB_is_sgb(GB_gameboy_t *gb); +bool GB_is_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2 +bool GB_is_hle_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2 and the SFC/SNES side is HLE'd GB_model_t GB_get_model(GB_gameboy_t *gb); void GB_free(GB_gameboy_t *gb); void GB_reset(GB_gameboy_t *gb); diff --git a/Core/save_state.c b/Core/save_state.c index 5a7d920..8ef99ae 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -36,7 +36,7 @@ int GB_save_state(GB_gameboy_t *gb, const char *path) if (!DUMP_SECTION(gb, f, rtc )) goto error; if (!DUMP_SECTION(gb, f, video )) goto error; - if (GB_is_sgb(gb)) { + if (GB_is_hle_sgb(gb)) { if (!dump_section(f, gb->sgb, sizeof(*gb->sgb))) goto error; } @@ -73,7 +73,7 @@ size_t GB_get_save_state_size(GB_gameboy_t *gb) + GB_SECTION_SIZE(apu ) + sizeof(uint32_t) + GB_SECTION_SIZE(rtc ) + sizeof(uint32_t) + GB_SECTION_SIZE(video ) + sizeof(uint32_t) - + (GB_is_sgb(gb)? sizeof(*gb->sgb) + sizeof(uint32_t) : 0) + + (GB_is_hle_sgb(gb)? sizeof(*gb->sgb) + sizeof(uint32_t) : 0) + gb->mbc_ram_size + gb->ram_size + gb->vram_size; @@ -105,7 +105,7 @@ void GB_save_state_to_buffer(GB_gameboy_t *gb, uint8_t *buffer) DUMP_SECTION(gb, buffer, rtc ); DUMP_SECTION(gb, buffer, video ); - if (GB_is_sgb(gb)) { + if (GB_is_hle_sgb(gb)) { buffer_dump_section(&buffer, gb->sgb, sizeof(*gb->sgb)); } @@ -161,8 +161,8 @@ static bool verify_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t *save) return false; } - if (GB_is_sgb(gb) != GB_is_sgb(save)) { - GB_log(gb, "The save state is %sfor a Super Game Boy. Try changing the emulated model.\n", GB_is_sgb(save)? "" : "not "); + if (GB_is_hle_sgb(gb) != GB_is_hle_sgb(save)) { + GB_log(gb, "The save state is %sfor a Super Game Boy. Try changing the emulated model.\n", GB_is_hle_sgb(save)? "" : "not "); return false; } @@ -223,7 +223,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) goto error; } - if (GB_is_sgb(gb)) { + if (GB_is_hle_sgb(gb)) { if (!read_section(f, gb->sgb, sizeof(*gb->sgb))) goto error; } @@ -334,7 +334,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le return -1; } - if (GB_is_sgb(gb)) { + if (GB_is_hle_sgb(gb)) { if (!buffer_read_section(&buffer, &length, gb->sgb, sizeof(*gb->sgb))) return -1; } diff --git a/Core/sgb.c b/Core/sgb.c index 5bcb72d..a422af0 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -296,6 +296,10 @@ static void command_ready(GB_gameboy_t *gb) void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) { if (!GB_is_sgb(gb)) return; + if (!GB_is_hle_sgb(gb)) { + /* Notify via callback */ + return; + } if (gb->sgb->disable_commands) return; if (gb->sgb->command_write_index >= sizeof(gb->sgb->command) * 8) return; From e1873ad2ec5833aaf963fe57e4972c24c5c2a30a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 15 Jul 2019 22:01:41 +0300 Subject: [PATCH 070/341] Add JOYP write callback API --- Core/gb.c | 5 +++++ Core/gb.h | 5 +++++ Core/sgb.c | 3 +++ 3 files changed, 13 insertions(+) diff --git a/Core/gb.c b/Core/gb.c index 6f846ca..a1d0f19 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -906,3 +906,8 @@ double GB_get_usual_frame_rate(GB_gameboy_t *gb) { return GB_get_clock_rate(gb) / (double)LCDC_PERIOD; } + +void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callback) +{ + gb->joyp_write_callback = callback; +} diff --git a/Core/gb.h b/Core/gb.h index 6d10283..b82d743 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -242,6 +242,7 @@ typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, bool rumble_on); typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool bit_to_send); typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); +typedef void (*GB_joyp_write_callback_t)(GB_gameboy_t *gb, uint8_t value); typedef struct { bool state; @@ -533,6 +534,7 @@ struct GB_gameboy_internal_s { GB_serial_transfer_bit_start_callback_t serial_transfer_bit_start_callback; GB_serial_transfer_bit_end_callback_t serial_transfer_bit_end_callback; GB_update_input_hint_callback_t update_input_hint_callback; + GB_joyp_write_callback_t joyp_write_callback; /* IR */ long cycles_since_ir_change; // In 8MHz units @@ -691,6 +693,9 @@ bool GB_serial_get_data_bit(GB_gameboy_t *gb); void GB_serial_set_data_bit(GB_gameboy_t *gb, bool data); void GB_disconnect_serial(GB_gameboy_t *gb); + +/* For integration with SFC/SNES emulators */ +void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callback); #ifdef GB_INTERNAL uint32_t GB_get_clock_rate(GB_gameboy_t *gb); diff --git a/Core/sgb.c b/Core/sgb.c index a422af0..ccc439a 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -295,6 +295,9 @@ static void command_ready(GB_gameboy_t *gb) void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) { + if (gb->joyp_write_callback) { + gb->joyp_write_callback(gb, value); + } if (!GB_is_sgb(gb)) return; if (!GB_is_hle_sgb(gb)) { /* Notify via callback */ From 346e499602babe09288c76386a33e9528eb79d3a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 15 Jul 2019 23:02:58 +0300 Subject: [PATCH 071/341] ICD APIs --- Core/display.c | 24 +++++++++++++++++++----- Core/gb.c | 27 +++++++++++++++++++++++++-- Core/gb.h | 11 +++++++++-- Core/memory.c | 4 ++++ 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Core/display.c b/Core/display.c index cdcdeb2..54c53a5 100644 --- a/Core/display.c +++ b/Core/display.c @@ -137,9 +137,8 @@ static void display_vblank(GB_gameboy_t *gb) if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ if (gb->sgb) { - uint8_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped && GB_is_cgb(gb) ? 0x3 : 0x0; for (unsigned i = 0; i < WIDTH * LINES; i++) { - gb->sgb->screen_buffer[i] = color; + gb->sgb->screen_buffer[i] = 0x0; } } else { @@ -387,9 +386,12 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } if (gb->sgb) { if (gb->current_lcd_line < LINES) { - gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = pixel; + gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; } } + else if (gb->model & GB_MODEL_NO_SFC_BIT) { + gb->icd_row[gb->position_in_line] = pixel; + } else { gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel]; } @@ -403,9 +405,12 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } if (gb->sgb) { if (gb->current_lcd_line < LINES) { - gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = pixel; + gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; } } + else if (gb->model & GB_MODEL_NO_SFC_BIT) { + gb->icd_row[gb->position_in_line] = pixel; + } else { gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; } @@ -890,6 +895,11 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line); gb->mode_for_interrupt = 2; + + /* TODO: Can this timing even be verified? */ + if (gb->icd_row_callback) { + gb->icd_row_callback(gb, gb->icd_row); + } } /* Lines 144 - 152 */ @@ -961,7 +971,11 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->wy_diff = 0; gb->window_disabled_while_active = false; gb->current_line = 0; - gb->current_lcd_line = -1; // TODO: not the correct timing + // TODO: not the correct timing + gb->current_lcd_line = -1; + if (gb->icd_vreset_callback) { + gb->icd_vreset_callback(gb); + } } } diff --git a/Core/gb.c b/Core/gb.c index a1d0f19..ff2fd3c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -112,6 +112,11 @@ void GB_init(GB_gameboy_t *gb, GB_model_t model) gb->cartridge_type = &GB_cart_defs[0]; // Default cartridge type gb->clock_multiplier = 1.0; + if (model & GB_MODEL_NO_SFC_BIT) { + /* Disable time syncing. Timing should be done by the SFC emulator. */ + gb->turbo = true; + } + GB_reset(gb); } @@ -576,6 +581,7 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB_NO_SFC: for (unsigned i = 0; i < gb->ram_size; i++) { gb->ram[i] = GB_random(); if (i & 0x100) { @@ -588,6 +594,7 @@ static void reset_ram(GB_gameboy_t *gb) break; case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: for (unsigned i = 0; i < gb->ram_size; i++) { gb->ram[i] = 0x55; gb->ram[i] ^= GB_random() & GB_random() & GB_random(); @@ -620,7 +627,9 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB_NO_SFC: case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: for (unsigned i = 0; i < sizeof(gb->hram); i++) { if (i & 1) { gb->hram[i] = GB_random() | GB_random() | GB_random(); @@ -641,9 +650,11 @@ static void reset_ram(GB_gameboy_t *gb) break; case GB_MODEL_DMG_B: - case GB_MODEL_SGB_NTSC: /* Unverified*/ + case GB_MODEL_SGB_NTSC: /* Unverified */ case GB_MODEL_SGB_PAL: /* Unverified */ + case GB_MODEL_SGB_NO_SFC: /* Unverified */ case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: for (unsigned i = 0; i < 8; i++) { if (i & 2) { gb->oam[i] = GB_random() & GB_random() & GB_random(); @@ -669,7 +680,9 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ - case GB_MODEL_SGB2: { + case GB_MODEL_SGB_NO_SFC: /* Unverified */ + case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: { uint8_t temp; for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) { if (i & 1) { @@ -911,3 +924,13 @@ void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callb { gb->joyp_write_callback = callback; } + +void GB_set_icd_row_callback(GB_gameboy_t *gb, GB_icd_row_callback_t callback) +{ + gb->icd_row_callback = callback; +} + +void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback) +{ + gb->icd_vreset_callback = callback; +} diff --git a/Core/gb.h b/Core/gb.h index b82d743..eed23e4 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -23,7 +23,6 @@ #define GB_STRUCT_VERSION 13 -#ifdef GB_INTERNAL #define GB_MODEL_FAMILY_MASK 0xF00 #define GB_MODEL_DMG_FAMILY 0x000 #define GB_MODEL_MGB_FAMILY 0x100 @@ -31,6 +30,7 @@ #define GB_MODEL_PAL_BIT 0x1000 #define GB_MODEL_NO_SFC_BIT 0x2000 +#ifdef GB_INTERNAL #if __clang__ #define UNROLL _Pragma("unroll") #elif __GNUC__ @@ -243,6 +243,8 @@ typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool b typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); typedef void (*GB_joyp_write_callback_t)(GB_gameboy_t *gb, uint8_t value); +typedef void (*GB_icd_row_callback_t)(GB_gameboy_t *gb, uint8_t *row); +typedef void (*GB_icd_vreset_callback_t)(GB_gameboy_t *gb); typedef struct { bool state; @@ -420,6 +422,7 @@ struct GB_gameboy_internal_s { uint16_t serial_length; uint8_t double_speed_alignment; uint8_t serial_count; + uint8_t icd_row[160]; ); /* APU */ @@ -535,6 +538,8 @@ struct GB_gameboy_internal_s { GB_serial_transfer_bit_end_callback_t serial_transfer_bit_end_callback; GB_update_input_hint_callback_t update_input_hint_callback; GB_joyp_write_callback_t joyp_write_callback; + GB_icd_row_callback_t icd_row_callback; + GB_icd_vreset_callback_t icd_vreset_callback; /* IR */ long cycles_since_ir_change; // In 8MHz units @@ -696,7 +701,9 @@ void GB_disconnect_serial(GB_gameboy_t *gb); /* 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_row_callback(GB_gameboy_t *gb, GB_icd_row_callback_t callback); +void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback); + #ifdef GB_INTERNAL uint32_t GB_get_clock_rate(GB_gameboy_t *gb); #endif diff --git a/Core/memory.c b/Core/memory.c index 868a245..e5983cb 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -273,7 +273,9 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: case GB_MODEL_SGB_PAL: + case GB_MODEL_SGB_NO_SFC: case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: ; } } @@ -584,7 +586,9 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: case GB_MODEL_SGB_PAL: + case GB_MODEL_SGB_NO_SFC: case GB_MODEL_SGB2: + case GB_MODEL_SGB2_NO_SFC: case GB_MODEL_CGB_E: case GB_MODEL_AGB: break; From ce9ce078172f65deb65fdd0c448696b67f65b690 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 20:44:27 +0300 Subject: [PATCH 072/341] Make the ICD APIs pixel based --- Core/display.c | 12 ++++++++---- Core/gb.c | 10 ++++++++-- Core/gb.h | 13 ++++++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Core/display.c b/Core/display.c index 54c53a5..04790f5 100644 --- a/Core/display.c +++ b/Core/display.c @@ -390,7 +390,9 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } } else if (gb->model & GB_MODEL_NO_SFC_BIT) { - gb->icd_row[gb->position_in_line] = pixel; + if (gb->icd_pixel_callback) { + gb->icd_pixel_callback(gb, pixel); + } } else { gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel]; @@ -409,7 +411,9 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } } else if (gb->model & GB_MODEL_NO_SFC_BIT) { - gb->icd_row[gb->position_in_line] = pixel; + if (gb->icd_pixel_callback) { + gb->icd_pixel_callback(gb, pixel); + } } else { gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; @@ -897,8 +901,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->mode_for_interrupt = 2; /* TODO: Can this timing even be verified? */ - if (gb->icd_row_callback) { - gb->icd_row_callback(gb, gb->icd_row); + if (gb->icd_hreset_callback) { + gb->icd_hreset_callback(gb); } } diff --git a/Core/gb.c b/Core/gb.c index ff2fd3c..70f6c54 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -925,11 +925,17 @@ void GB_set_joyp_write_callback(GB_gameboy_t *gb, GB_joyp_write_callback_t callb gb->joyp_write_callback = callback; } -void GB_set_icd_row_callback(GB_gameboy_t *gb, GB_icd_row_callback_t callback) +void GB_set_icd_pixel_callback(GB_gameboy_t *gb, GB_icd_pixel_callback_t callback) { - gb->icd_row_callback = callback; + gb->icd_pixel_callback = callback; } +void GB_set_icd_hreset_callback(GB_gameboy_t *gb, GB_icd_hreset_callback_t callback) +{ + gb->icd_hreset_callback = callback; +} + + void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback) { gb->icd_vreset_callback = callback; diff --git a/Core/gb.h b/Core/gb.h index eed23e4..976a0aa 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -243,7 +243,8 @@ typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool b typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); typedef void (*GB_joyp_write_callback_t)(GB_gameboy_t *gb, uint8_t value); -typedef void (*GB_icd_row_callback_t)(GB_gameboy_t *gb, uint8_t *row); +typedef void (*GB_icd_pixel_callback_t)(GB_gameboy_t *gb, uint8_t row); +typedef void (*GB_icd_hreset_callback_t)(GB_gameboy_t *gb); typedef void (*GB_icd_vreset_callback_t)(GB_gameboy_t *gb); typedef struct { @@ -422,7 +423,6 @@ struct GB_gameboy_internal_s { uint16_t serial_length; uint8_t double_speed_alignment; uint8_t serial_count; - uint8_t icd_row[160]; ); /* APU */ @@ -451,7 +451,8 @@ struct GB_gameboy_internal_s { /* The LCDC will skip the first frame it renders after turning it on. On the CGB, a frame is not skipped if the previous frame was skipped as well. See https://www.reddit.com/r/EmuDev/comments/6exyxu/ */ - /* TODO: Drop this and properly emulate the dropped vreset signal*/ + + /* TODO: Drop this and properly emulate the dropped vreset signal*/ enum { GB_FRAMESKIP_LCD_TURNED_ON, // On a DMG, the LCD renders a blank screen during this state, // on a CGB, the previous frame is repeated (which might be @@ -538,7 +539,8 @@ struct GB_gameboy_internal_s { GB_serial_transfer_bit_end_callback_t serial_transfer_bit_end_callback; GB_update_input_hint_callback_t update_input_hint_callback; GB_joyp_write_callback_t joyp_write_callback; - GB_icd_row_callback_t icd_row_callback; + GB_icd_pixel_callback_t icd_pixel_callback; + GB_icd_vreset_callback_t icd_hreset_callback; GB_icd_vreset_callback_t icd_vreset_callback; /* IR */ @@ -701,7 +703,8 @@ void GB_disconnect_serial(GB_gameboy_t *gb); /* 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_row_callback(GB_gameboy_t *gb, GB_icd_row_callback_t callback); +void GB_set_icd_pixel_callback(GB_gameboy_t *gb, GB_icd_pixel_callback_t callback); +void GB_set_icd_hreset_callback(GB_gameboy_t *gb, GB_icd_hreset_callback_t callback); void GB_set_icd_vreset_callback(GB_gameboy_t *gb, GB_icd_vreset_callback_t callback); #ifdef GB_INTERNAL From 2d7f54a775a185b239d64fead45b980a529812c6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 21:04:29 +0300 Subject: [PATCH 073/341] Load ROM from buffer API --- Core/gb.c | 18 +++++++++++++++++- Core/gb.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Core/gb.c b/Core/gb.c index 70f6c54..1e940cf 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -198,13 +198,29 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) } gb->rom = malloc(gb->rom_size); memset(gb->rom, 0xFF, gb->rom_size); /* Pad with 0xFFs */ - fread(gb->rom, gb->rom_size, 1, f); + fread(gb->rom, 1, gb->rom_size, f); fclose(f); GB_configure_cart(gb); return 0; } +void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size) +{ + gb->rom_size = (size + 0x3fff) & ~0x3fff; + while (gb->rom_size & (gb->rom_size - 1)) { + gb->rom_size |= gb->rom_size >> 1; + gb->rom_size++; + } + if (gb->rom) { + free(gb->rom); + } + gb->rom = malloc(gb->rom_size); + memset(gb->rom, 0xff, gb->rom_size); + memcpy(gb->rom, buffer, size); + GB_configure_cart(gb); +} + typedef struct { uint8_t seconds; uint8_t padding1[3]; diff --git a/Core/gb.h b/Core/gb.h index 976a0aa..e12fcfa 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -667,6 +667,7 @@ void GB_set_user_data(GB_gameboy_t *gb, void *data); int GB_load_boot_rom(GB_gameboy_t *gb, const char *path); void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size); int GB_load_rom(GB_gameboy_t *gb, const char *path); +void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size); int GB_save_battery(GB_gameboy_t *gb, const char *path); void GB_load_battery(GB_gameboy_t *gb, const char *path); From 9ba6915c85770ffb85b9e52d5dcfc4d4d79fe737 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 21:42:57 +0300 Subject: [PATCH 074/341] ICD JOYP write API --- Core/joypad.c | 17 ++++++++++++++++- Core/joypad.h | 1 + Core/memory.c | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Core/joypad.c b/Core/joypad.c index 4ae6e67..6c0eaff 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -3,6 +3,8 @@ void GB_update_joyp(GB_gameboy_t *gb) { + if (gb->model & GB_MODEL_SGB_NO_SFC) return; + uint8_t key_selection = 0; uint8_t previous_state = 0; @@ -53,14 +55,27 @@ void GB_update_joyp(GB_gameboy_t *gb) break; } + /* Todo: This assumes the keys *always* bounce, which is incorrect when emulating an SGB */ if (previous_state != (gb->io_registers[GB_IO_JOYP] & 0xF)) { - /* The joypad interrupt DOES occur on CGB (Tested on CGB-CPU-06), unlike what some documents say. */ + /* The joypad interrupt DOES occur on CGB (Tested on CGB-E), unlike what some documents say. */ gb->io_registers[GB_IO_IF] |= 0x10; } gb->io_registers[GB_IO_JOYP] |= 0xC0; } +void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value) +{ + uint8_t previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; + gb->io_registers[GB_IO_JOYP] &= 0xF0; + gb->io_registers[GB_IO_JOYP] |= value & 0xF; + + if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { + gb->io_registers[GB_IO_IF] |= 0x10; + } + +} + void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed) { assert(index >= 0 && index < GB_KEY_MAX); diff --git a/Core/joypad.h b/Core/joypad.h index 768d685..21fad53 100644 --- a/Core/joypad.h +++ b/Core/joypad.h @@ -17,6 +17,7 @@ typedef enum { void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed); void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned player, bool pressed); +void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value); #ifdef GB_INTERNAL void GB_update_joyp(GB_gameboy_t *gb); diff --git a/Core/memory.c b/Core/memory.c index e5983cb..76d8821 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -741,8 +741,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; case GB_IO_JOYP: - GB_sgb_write(gb, value); gb->io_registers[GB_IO_JOYP] = value & 0xF0; + GB_sgb_write(gb, value); GB_update_joyp(gb); return; From eb95f1de5598807b07d5ee59771515d6aacc1d6e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 23:14:26 +0300 Subject: [PATCH 075/341] Fixed a bug where the SDL port loaded the incorrect boot ROM for SGB2. Made SameBoy compatible with older SDL versions. --- SDL/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SDL/main.c b/SDL/main.c index df6837f..ba2a5fb 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -23,6 +23,11 @@ #define AUDIO_FREQUENCY 48000 #endif +/* Compatibility with older SDL versions */ +#ifndef SDL_AUDIO_ALLOW_SAMPLES_CHANGE +#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0 +#endif + GB_gameboy_t gb; static bool paused = false; static uint32_t pixel_buffer_1[256 * 224], pixel_buffer_2[256 * 224]; @@ -464,7 +469,7 @@ restart: start_capturing_logs(); const char * const boot_roms[] = {"dmg_boot.bin", "cgb_boot.bin", "agb_boot.bin", "sgb_boot.bin"}; const char *boot_rom = boot_roms[configuration.model]; - if (configuration.model == GB_MODEL_SGB && configuration.sgb_revision == SGB_2) { + if (configuration.model == MODEL_SGB && configuration.sgb_revision == SGB_2) { boot_rom = "sgb2_boot.bin"; } error = GB_load_boot_rom(&gb, resource_path(boot_rom)); From 11a9f1df21288e2d46cbcb224e60fc4ff8816fd2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 23:27:35 +0300 Subject: [PATCH 076/341] Silence some GCC warnings --- Core/gb.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index dec32a9..94e4d57 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -21,7 +21,7 @@ void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args) { char *string = NULL; - vasprintf(&string, fmt, args); + (void)vasprintf(&string, fmt, args); if (string) { if (gb->log_callback) { gb->log_callback(gb, string, attributes); @@ -158,9 +158,12 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) GB_log(gb, "Could not open boot ROM: %s.\n", strerror(errno)); return errno; } - fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f); + int ret = 0; + if (fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f) != 1) { + ret = -1; + } fclose(f); - return 0; + return ret; } void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size) @@ -193,7 +196,7 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) } gb->rom = malloc(gb->rom_size); memset(gb->rom, 0xFF, gb->rom_size); /* Pad with 0xFFs */ - fread(gb->rom, gb->rom_size, 1, f); + (void) fread(gb->rom, gb->rom_size, 1, f); fclose(f); GB_configure_cart(gb); From 9efd20d7cd17eb9b9d1756998f360b6f01b9cc90 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 23:33:07 +0300 Subject: [PATCH 077/341] Revert "Silence some GCC warnings" This reverts commit 11a9f1df21288e2d46cbcb224e60fc4ff8816fd2. --- Core/gb.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 94e4d57..dec32a9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -21,7 +21,7 @@ void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args) { char *string = NULL; - (void)vasprintf(&string, fmt, args); + vasprintf(&string, fmt, args); if (string) { if (gb->log_callback) { gb->log_callback(gb, string, attributes); @@ -158,12 +158,9 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) GB_log(gb, "Could not open boot ROM: %s.\n", strerror(errno)); return errno; } - int ret = 0; - if (fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f) != 1) { - ret = -1; - } + fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f); fclose(f); - return ret; + return 0; } void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size) @@ -196,7 +193,7 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) } gb->rom = malloc(gb->rom_size); memset(gb->rom, 0xFF, gb->rom_size); /* Pad with 0xFFs */ - (void) fread(gb->rom, gb->rom_size, 1, f); + fread(gb->rom, gb->rom_size, 1, f); fclose(f); GB_configure_cart(gb); From 1bf5fb208f20b4338a75d58d278515c038f23138 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 16 Jul 2019 23:41:05 +0300 Subject: [PATCH 078/341] Silence an unwanted GCC warning --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 401fa3a..a835470 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ OPEN_DIALOG = OpenDialog/cocoa.m endif -CFLAGS += -Werror -Wall -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES +CFLAGS += -Werror -Wall -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES SDL_LDFLAGS := -lSDL2 -lGL ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows -Drandom=rand From 4504de828a188b520a324687a1181af6c45a7e3a Mon Sep 17 00:00:00 2001 From: Damian Yerrick Date: Tue, 16 Jul 2019 16:58:16 -0400 Subject: [PATCH 079/341] cgb_boot: Compress logo with PB8 The logo is compressed using PB8, a form of RLE with unary-coded run lengths. Each block representing 8 bytes consists of a control byte, where each bit (MSB to LSB) is 0 for literal or 1 for repeat previous, followed by the literals in that block. PB8 compression is also used in a few NES games. A variant called PB16, where 1 means repeat 2 bytes back, is used in the Game Boy port of 240p Test Suite and in Libbet and the Magic Floor. Switching from logo-compress RLE to PB8 decreases the compressed logo data size from 287 bytes to 253 bytes, saving 34 bytes. The decompression code is also about 10 bytes smaller. --- BootROMs/cgb_boot.asm | 78 ++++++---- BootROMs/pb8.c | 330 ++++++++++++++++++++++++++++++++++++++++++ Makefile | 24 +-- 3 files changed, 389 insertions(+), 43 deletions(-) create mode 100644 BootROMs/pb8.c diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 0472cbe..6ae869b 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -532,7 +532,7 @@ TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c SameBoyLogo: - incbin "SameBoyLogo.rle" + incbin "SameBoyLogo.pb8" AnimationColors: dw $7FFF ; White @@ -634,41 +634,55 @@ ReadCGBLogoHalfTile: ld a, e ret +; LoadTileset using PB8 codec, 2019 Damian Yerrick +; +; The logo is compressed using PB8, a form of RLE with unary-coded +; run lengths. Each block representing 8 bytes consists of a control +; byte, where each bit (MSB to LSB) is 0 for literal or 1 for repeat +; previous, followed by the literals in that block. + +SameBoyLogo_dst = $8080 +SameBoyLogo_length = (128 * 24) / 64 + LoadTileset: -; Copy SameBoy Logo - ld de, SameBoyLogo - ld hl, $8080 -.sameboyLogoLoop - ld a, [de] - inc de - - ld b, a - and $0f - jr z, .skipLiteral - ld c, a - -.literalLoop - ld a, [de] - ldi [hl], a + ld hl, SameBoyLogo + ld de, SameBoyLogo_dst + ld c, SameBoyLogo_length +.pb8BlockLoop: + ; Register map for PB8 decompression + ; HL: source address in boot ROM + ; DE: destination address in VRAM + ; A: Current literal value + ; B: Repeat bits, terminated by 1000... + ; C: Number of 8-byte blocks left in this block + ; Source address in HL lets the repeat bits go straight to B, + ; bypassing A and avoiding spilling registers to the stack. + ld b, [hl] inc hl - inc de - dec c - jr nz, .literalLoop -.skipLiteral - swap b - ld a, b - and $0f - jr z, .sameboyLogoEnd - ld c, a - ld a, [de] - inc de -.repeatLoop - ldi [hl], a - inc hl + ; Shift a 1 into lower bit of shift value. Once this bit + ; reaches the carry, B becomes 0 and the byte is over + scf + rl b + +.pb8BitLoop: + ; If not a repeat, load a literal byte + jr c,.pb8Repeat + ld a, [hli] +.pb8Repeat: + ; Decompressed data uses colors 0 and 1, so write once, inc twice + ld [de], a + inc de + inc de + sla b + jr nz, .pb8BitLoop + dec c - jr nz, .repeatLoop - jr .sameboyLogoLoop + jr nz, .pb8BlockLoop + +; End PB8 decoding. The rest uses HL as the destination + ld h, d + ld l, e .sameboyLogoEnd ; Copy (unresized) ROM logo diff --git a/BootROMs/pb8.c b/BootROMs/pb8.c new file mode 100644 index 0000000..03a196e --- /dev/null +++ b/BootROMs/pb8.c @@ -0,0 +1,330 @@ +/* + +PB8 compressor and decompressor + +Copyright 2019 Damian Yerrick + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. + +*/ + +#include +#include +#include +#include +#include +#include + +// For setting stdin/stdout to binary mode +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) +#include +#define fd_isatty isatty +#elif defined (_WIN32) +#include +#include +#define fd_isatty _isatty +#endif + +/* + +; The logo is compressed using PB8, a form of RLE with unary-coded +; run lengths. Each block representing 8 bytes consists of a control +; byte, where each bit (MSB to LSB) is 0 for literal or 1 for repeat +; previous, followed by the literals in that block. + +SameBoyLogo_dst = $8080 +SameBoyLogo_length = (128 * 24) / 64 + +LoadTileset: + ld hl, SameBoyLogo + ld de, SameBoyLogo_dst + ld c, SameBoyLogo_length +.pb8BlockLoop: + ; Register map for PB8 decompression + ; HL: source address in boot ROM + ; DE: destination address in VRAM + ; A: Current literal value + ; B: Repeat bits, terminated by 1000... + ; C: Number of 8-byte blocks left in this block + ; Source address in HL lets the repeat bits go straight to B, + ; bypassing A and avoiding spilling registers to the stack. + ld b, [hl] + inc hl + + ; Shift a 1 into lower bit of shift value. Once this bit + ; reaches the carry, B becomes 0 and the byte is over + scf + rl b + +.pb8BitLoop: + ; If not a repeat, load a literal byte + jr c,.pb8Repeat + ld a, [hli] +.pb8Repeat: + ; Decompressed data uses colors 0 and 1, so write once, inc twice + ld [de], a + inc de + inc de + sla b + jr nz, .pb8BitLoop + + dec c + jr nz, .pb8BlockLoop + ret + +*/ + +/* Compressor and decompressor *************************************/ + +/** + * Compresses an input stream to PB8 data on an output stream. + * @param infp input stream + * @param outfp output stream + * @param blocklength size of an independent input block in bytes + * @return 0 for reaching infp end of file, or EOF for error + */ +int pb8(FILE *infp, FILE *outfp, size_t blocklength) { + blocklength >>= 3; // convert bytes to blocks + assert(blocklength > 0); + while (1) { + int last_byte = EOF; // value that never occurs in a file + for (size_t blkleft = blocklength; blkleft > 0; --blkleft) { + unsigned int control_byte = 0x0001; + unsigned char literals[8]; + size_t nliterals = 0; + while (control_byte < 0x100) { + int c = fgetc(infp); + if (c == EOF) break; + + control_byte <<= 1; + if (c == last_byte) { + control_byte |= 0x01; + } else { + literals[nliterals++] = last_byte = c; + } + } + if (control_byte > 1) { + // Fill partial block with repeats + while (control_byte < 0x100) { + control_byte = (control_byte << 1) | 1; + } + + // Write control byte and check for write failure + int ok = fputc(control_byte & 0xFF, outfp); + if (ok == EOF) return EOF; + size_t ok2 = fwrite(literals, 1, nliterals, outfp); + if (ok2 < nliterals) return EOF; + } + + // If finished, return success or failure + if (ferror(infp) || ferror(outfp)) return EOF; + if (feof(infp)) return 0; + } // End 8-byte block + } // End packet, resetting last_byte +} + +/** + * Decompresses PB8 data on an input stream to an output stream. + * @param infp input stream + * @param outfp output stream + * @return 0 for reaching infp end of file, or EOF for error + */ +int unpb8(FILE *infp, FILE *outfp) { + int last_byte = 0; + while (1) { + int control_byte = fgetc(infp); + if (control_byte == EOF) { + return feof(infp) ? 0 : EOF; + } + control_byte &= 0xFF; + for (size_t bytesleft = 8; bytesleft > 0; --bytesleft) { + if (!(control_byte & 0x80)) { + last_byte = fgetc(infp); + if (last_byte == EOF) return EOF; // read error + } + control_byte <<= 1; + int ok = fputc(last_byte, outfp); + if (ok == EOF) return EOF; + } + } +} + +/* CLI frontend ****************************************************/ + +static inline void set_fd_binary(unsigned int fd) { +#ifdef _WIN32 + _setmode(fd, _O_BINARY); +#else + (void) fd; +#endif +} + +static const char *usage_msg = +"usage: pb8 [-d] [-l blocklength] [infile [outfile]]\n" +"Compresses a file using RLE with unary run and literal lengths.\n" +"\n" +"options:\n" +" -d decompress\n" +" -l blocklength allow RLE packets to span up to blocklength\n" +" input bytes (multiple of 8; default 8)\n" +" -h, -?, --help show this usage page\n" +" --version show copyright info\n" +"\n" +"If infile is - or missing, it is standard input.\n" +"If outfile is - or missing, it is standard output.\n" +"You cannot compress to or decompress from a terminal.\n" +; +static const char *version_msg = +"PB8 compressor (C version) v0.01\n" +"Copyright 2019 Damian Yerrick \n" +"This software is provided 'as-is', without any express or implied\n" +"warranty.\n" +; +static const char *toomanyfilenames_msg = +"pb8: too many filenames; try pb8 --help\n"; + +int main(int argc, char **argv) { + const char *infilename = NULL; + const char *outfilename = NULL; + bool decompress = false; + size_t blocklength = 8; + + for (int i = 1; i < argc; ++i) { + if (argv[i][0] == '-' && argv[i][1] != 0) { + if (!strcmp(argv[i], "--help")) { + fputs(usage_msg, stdout); + return 0; + } + if (!strcmp(argv[i], "--version")) { + fputs(version_msg, stdout); + return 0; + } + + // -t1 or -t 1 + int argtype = argv[i][1]; + switch (argtype) { + case 'h': + case '?': + fputs(usage_msg, stdout); + return 0; + + case 'd': + decompress = true; + break; + + case 'l': { + const char *argvalue = argv[i][2] ? argv[i] + 2 : argv[++i]; + const char *endptr = NULL; + + unsigned long tvalue = strtoul(argvalue, (char **)&endptr, 10); + if (endptr == argvalue || tvalue == 0 || tvalue > SIZE_MAX) { + fprintf(stderr, "pb8: block length %s not a positive integer\n", + argvalue); + return EXIT_FAILURE; + } + if (tvalue % 8 != 0) { + fprintf(stderr, "pb8: block length %s not a multiple of 8\n", + argvalue); + return EXIT_FAILURE; + } + blocklength = tvalue; + } break; + + default: + fprintf(stderr, "pb8: unknown option -%c\n", argtype); + return EXIT_FAILURE; + } + } else if (!infilename) { + infilename = argv[i]; + } else if (!outfilename) { + outfilename = argv[i]; + } else { + fputs(toomanyfilenames_msg, stderr); + return EXIT_FAILURE; + } + } + if (infilename && !strcmp(infilename, "-")) { + infilename = NULL; + } + if (!infilename && decompress && fd_isatty(0)) { + fputs("pb8: cannot decompress from terminal; try redirecting stdin\n", + stderr); + return EXIT_FAILURE; + } + if (outfilename && !strcmp(outfilename, "-")) { + outfilename = NULL; + } + if (!outfilename && !decompress && fd_isatty(1)) { + fputs("pb8: cannot compress to terminal; try redirecting stdout or pb8 --help\n", + stderr); + return EXIT_FAILURE; + } + + FILE *infp = NULL; + if (infilename) { + infp = fopen(infilename, "rb"); + if (!infp) { + fprintf(stderr, "pb8: error opening %s ", infilename); + perror("for reading"); + return EXIT_FAILURE; + } + } else { + infp = stdin; + set_fd_binary(0); + } + + FILE *outfp = NULL; + if (outfilename) { + outfp = fopen(outfilename, "wb"); + if (!outfp) { + fprintf(stderr, "pb8: error opening %s ", outfilename); + perror("for writing"); + fclose(infp); + return EXIT_FAILURE; + } + } else { + outfp = stdout; + set_fd_binary(1); + } + + int compfailed = 0; + int has_ferror = 0; + if (decompress) { + compfailed = unpb8(infp, outfp); + } else { + compfailed = pb8(infp, outfp, blocklength); + } + fflush(outfp); + if (ferror(infp)) { + fprintf(stderr, "pb8: error reading %s\n", + infilename ? infilename : ""); + has_ferror = EOF; + } + fclose(infp); + if (ferror(outfp)) { + fprintf(stderr, "pb8: error writing %s\n", + outfilename ? outfilename : ""); + has_ferror = EOF; + } + fclose(outfp); + + if (compfailed && !has_ferror) { + fputs("pb8: unknown compression failure\n", stderr); + } + + return (compfailed || has_ferror) ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/Makefile b/Makefile index 401fa3a..5db8c42 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,15 @@ ifneq ($(findstring MSYS,$(PLATFORM)),) PLATFORM := windows32 endif -LOGO_COMPRESS := build/logo-compress - ifeq ($(PLATFORM),windows32) _ := $(shell chcp 65001) -LOGO_COMPRESS := build/logo-compress.exe +EXESUFFIX:=.exe +else +EXESUFFIX:= endif +PB8_COMPRESS := build/pb8$(EXESUFFIX) + ifeq ($(PLATFORM),Darwin) DEFAULT := cocoa else @@ -302,11 +304,11 @@ $(BIN)/tester/sameboy_tester.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(BIN)/SDL/%.bin $(BIN)/tester/%.bin: $(BOOTROMS_DIR)/%.bin -@$(MKDIR) -p $(dir $@) cp -f $^ $@ - + $(BIN)/SameBoy.app/Contents/Resources/%.bin: $(BOOTROMS_DIR)/%.bin -@$(MKDIR) -p $(dir $@) cp -f $^ $@ - + $(BIN)/SDL/LICENSE: LICENSE -@$(MKDIR) -p $(dir $@) cp -f $^ $@ @@ -314,7 +316,7 @@ $(BIN)/SDL/LICENSE: LICENSE $(BIN)/SDL/registers.sym: Misc/registers.sym -@$(MKDIR) -p $(dir $@) cp -f $^ $@ - + $(BIN)/SDL/background.bmp: SDL/background.bmp -@$(MKDIR) -p $(dir $@) cp -f $^ $@ @@ -329,17 +331,17 @@ $(OBJ)/%.1bpp: %.png -@$(MKDIR) -p $(dir $@) rgbgfx -d 1 -h -o $@ $< -$(OBJ)/BootROMs/SameBoyLogo.rle: $(OBJ)/BootROMs/SameBoyLogo.1bpp $(LOGO_COMPRESS) - $(realpath $(LOGO_COMPRESS)) < $< > $@ +$(OBJ)/BootROMs/SameBoyLogo.pb8: $(OBJ)/BootROMs/SameBoyLogo.1bpp $(PB8_COMPRESS) + $(realpath $(PB8_COMPRESS)) -l 384 $< $@ -$(LOGO_COMPRESS): BootROMs/logo-compress.c +$(PB8_COMPRESS): BootROMs/pb8.c $(CC) $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm -$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.rle +$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.pb8 -@$(MKDIR) -p $(dir $@) rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp @@ -349,7 +351,7 @@ $(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.rle # Libretro Core (uses its own build system) libretro: $(MAKE) -C libretro - + # Clean clean: rm -rf build From 26cf9707137971dee988c69d0b5cd09a3f8f8673 Mon Sep 17 00:00:00 2001 From: Damian Yerrick Date: Tue, 16 Jul 2019 17:04:23 -0400 Subject: [PATCH 080/341] don't need logo-compress.c anymore --- BootROMs/logo-compress.c | 62 ---------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 BootROMs/logo-compress.c diff --git a/BootROMs/logo-compress.c b/BootROMs/logo-compress.c deleted file mode 100644 index 2274eb2..0000000 --- a/BootROMs/logo-compress.c +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include -#include -#ifdef _WIN32 -#include -#include -#endif - -void pair(size_t count, uint8_t byte) -{ - static size_t unique_count = 0; - static uint8_t unique_data[15]; - if (count == 1) { - unique_data[unique_count++] = byte; - assert(unique_count <= 15); - } - else { - assert(count <= 15); - uint8_t control = (count << 4) | unique_count; - putchar(control); - - for (size_t i = 0; i < unique_count; i++) { - putchar(unique_data[i]); - } - - if (count != 0) { - putchar(byte); - } - else { - assert(control == 0); - } - - unique_count = 0; - } -} - -int main(int argc, char *argv[]) -{ - size_t count = 1; - uint8_t byte = getchar(); - int new; - size_t position = 0; - -#ifdef _WIN32 - _setmode(0,_O_BINARY); - _setmode(1,_O_BINARY); -#endif - - while ((new = getchar()) != EOF) { - if (byte == new) { - count++; - } - else { - pair(count, byte); - byte = new; - count = 1; - } - } - - pair(count, byte); - pair(0, 0); -} From 23ca39720626cf50ae90183ae3a50921d371ec55 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 17 Jul 2019 00:52:01 +0300 Subject: [PATCH 081/341] Remove unused flag --- libretro/jni/Application.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/libretro/jni/Application.mk b/libretro/jni/Application.mk index a169e74..a252a72 100644 --- a/libretro/jni/Application.mk +++ b/libretro/jni/Application.mk @@ -1,2 +1 @@ -APP_STL := c++_static APP_ABI := all From 597dc72e460f3e95caad40b8c557fb01f53f18ee Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 18 Jul 2019 00:13:41 +0300 Subject: [PATCH 082/341] Fix audio issues with some RetroArch audio drivers. Fixes #189 --- libretro/libretro.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 1dd411c..bd93e97 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -84,7 +84,7 @@ static struct retro_log_callback logging; static retro_log_printf_t log_cb; static retro_video_refresh_t video_cb; -static retro_audio_sample_batch_t audio_batch_cb; +static retro_audio_sample_t audio_sample_cb; static retro_input_poll_t input_poll_cb; static retro_input_state_t input_state_cb; @@ -152,7 +152,7 @@ static void audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { if ((audio_out == GB_1 && gb == &gameboy[0]) || (audio_out == GB_2 && gb == &gameboy[1])) { - audio_batch_cb((void*)sample, 1); + audio_sample_cb(sample->left, sample->right); } } @@ -772,11 +772,11 @@ void retro_set_environment(retro_environment_t cb) void retro_set_audio_sample(retro_audio_sample_t cb) { + audio_sample_cb = cb; } void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { - audio_batch_cb = cb; } void retro_set_input_poll(retro_input_poll_t cb) @@ -850,8 +850,7 @@ void retro_run(void) } else { - int x = GB_run_frame(&gameboy[0]); - log_cb(RETRO_LOG_DEBUG, "%d\n", x); + GB_run_frame(&gameboy[0]); } if (emulated_devices == 2) From 772289c54513ebc63c37b16880923d37c5f3906f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 18 Jul 2019 00:53:11 +0300 Subject: [PATCH 083/341] Fix a silly bug --- Core/display.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 04790f5..1892084 100644 --- a/Core/display.c +++ b/Core/display.c @@ -375,6 +375,8 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) bg_enabled = false; } } + + uint8_t icd_pixel = 0; { uint8_t pixel = bg_enabled? fifo_item->pixel : 0; @@ -391,7 +393,8 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } else if (gb->model & GB_MODEL_NO_SFC_BIT) { if (gb->icd_pixel_callback) { - gb->icd_pixel_callback(gb, pixel); + icd_pixel = pixel; + //gb->icd_pixel_callback(gb, pixel); } } else { @@ -412,13 +415,20 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } else if (gb->model & GB_MODEL_NO_SFC_BIT) { if (gb->icd_pixel_callback) { - gb->icd_pixel_callback(gb, pixel); + icd_pixel = pixel; + //gb->icd_pixel_callback(gb, pixel); } } else { gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; } } + + if (gb->model & GB_MODEL_NO_SFC_BIT) { + if (gb->icd_pixel_callback) { + gb->icd_pixel_callback(gb, icd_pixel); + } + } gb->position_in_line++; } From df7f7d81713636df0ceb7d51a280f480bc1f56d4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 18 Jul 2019 22:55:11 +0300 Subject: [PATCH 084/341] Fix silly desync inaccuracy --- Core/display.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Core/display.c b/Core/display.c index 1892084..2bf00ba 100644 --- a/Core/display.c +++ b/Core/display.c @@ -769,7 +769,13 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_push_bg_row(&gb->bg_fifo, 0, 0, 0, false, false); /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; - gb->current_lcd_line++; // Todo: unverified timing + + // Todo: unverified timing + gb->current_lcd_line++; + if (gb->icd_hreset_callback) { + gb->icd_hreset_callback(gb); + } + if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { display_vblank(gb); } @@ -909,11 +915,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line); gb->mode_for_interrupt = 2; - - /* TODO: Can this timing even be verified? */ - if (gb->icd_hreset_callback) { - gb->icd_hreset_callback(gb); - } } /* Lines 144 - 152 */ From f0809a667fe1369809233589e17455d9599f4804 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 15:50:36 +0300 Subject: [PATCH 085/341] Fixed a potential Cocoa crash when closing a window --- Cocoa/Document.m | 1 + Cocoa/GBViewMetal.m | 1 + 2 files changed, 2 insertions(+) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index b99e4c8..a520d62 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -389,6 +389,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) - (void)dealloc { [cameraSession stopRunning]; + self.view.gb = NULL; GB_free(&gb); if (cameraImage) { CVBufferRelease(cameraImage); diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 9acb11e..94b4975 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -131,6 +131,7 @@ static const vector_float2 rect[] = - (void)drawInMTKView:(nonnull MTKView *)view { if (!(view.window.occlusionState & NSWindowOcclusionStateVisible)) return; + if (!self.gb) return; if (texture.width != GB_get_screen_width(self.gb) || texture.height != GB_get_screen_height(self.gb)) { [self allocateTextures]; From 33198fc7b7b800087f2a5e43bef49c4fa38f224b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 15:50:49 +0300 Subject: [PATCH 086/341] Give SGB its own conflict map --- Core/sm83_cpu.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 12ca670..77248b5 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -33,6 +33,7 @@ static const GB_conflict_t cgb_conflict_map[0x80] = { /* Todo: most values not verified, and probably differ between revisions */ }; +/* Todo: verify on an MGB */ static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, [GB_IO_LYC] = GB_CONFLICT_READ_OLD, @@ -40,7 +41,6 @@ static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_SCY] = GB_CONFLICT_READ_NEW, [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, - /* Todo: these are GB_CONFLICT_READ_NEW on MGB/SGB2 */ [GB_IO_BGP] = GB_CONFLICT_PALETTE_DMG, [GB_IO_OBP0] = GB_CONFLICT_PALETTE_DMG, [GB_IO_OBP1] = GB_CONFLICT_PALETTE_DMG, @@ -51,6 +51,24 @@ static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_SCX] = GB_CONFLICT_READ_NEW, }; +/* Todo: Verify on an SGB1 */ +static const GB_conflict_t sgb_conflict_map[0x80] = { + [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, + [GB_IO_LYC] = GB_CONFLICT_READ_OLD, + [GB_IO_LCDC] = GB_CONFLICT_READ_NEW, + [GB_IO_SCY] = GB_CONFLICT_READ_NEW, + [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, + + [GB_IO_BGP] = GB_CONFLICT_READ_NEW, + [GB_IO_OBP0] = GB_CONFLICT_READ_NEW, + [GB_IO_OBP1] = GB_CONFLICT_READ_NEW, + + /* Todo: these were not verified at all */ + [GB_IO_WY] = GB_CONFLICT_READ_NEW, + [GB_IO_WX] = GB_CONFLICT_READ_NEW, + [GB_IO_SCX] = GB_CONFLICT_READ_NEW, +}; + static uint8_t cycle_read(GB_gameboy_t *gb, uint16_t addr) { if (gb->pending_cycles) { @@ -92,7 +110,17 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) assert(gb->pending_cycles); GB_conflict_t conflict = GB_CONFLICT_READ_OLD; if ((addr & 0xFF80) == 0xFF00) { - conflict = (GB_is_cgb(gb)? cgb_conflict_map : dmg_conflict_map)[addr & 0x7F]; + const GB_conflict_t *map = NULL; + if (GB_is_cgb(gb)) { + map = cgb_conflict_map; + } + else if (GB_is_sgb(gb)) { + map = sgb_conflict_map; + } + else { + map = dmg_conflict_map; + } + conflict = map[addr & 0x7F]; } switch (conflict) { case GB_CONFLICT_READ_OLD: From 4f9c8e93748144a4b63036820892e34744c4d4eb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 20:19:09 +0300 Subject: [PATCH 087/341] Match the HLE timings to the LLE timings --- Core/display.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Core/display.c b/Core/display.c index 2bf00ba..fc23044 100644 --- a/Core/display.c +++ b/Core/display.c @@ -769,13 +769,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_push_bg_row(&gb->bg_fifo, 0, 0, 0, false, false); /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; - - // Todo: unverified timing - gb->current_lcd_line++; - if (gb->icd_hreset_callback) { - gb->icd_hreset_callback(gb); - } - + if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { display_vblank(gb); } @@ -915,6 +909,12 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line); gb->mode_for_interrupt = 2; + + // Todo: unverified timing + gb->current_lcd_line++; + if (gb->icd_hreset_callback) { + gb->icd_hreset_callback(gb); + } } /* Lines 144 - 152 */ @@ -987,7 +987,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->window_disabled_while_active = false; gb->current_line = 0; // TODO: not the correct timing - gb->current_lcd_line = -1; + gb->current_lcd_line = 0; if (gb->icd_vreset_callback) { gb->icd_vreset_callback(gb); } From e634019ac9f418a104d81dc0fc8abeaa6c599aa8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 20:27:53 +0300 Subject: [PATCH 088/341] Fix CGB emulation --- Core/joypad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/joypad.c b/Core/joypad.c index 6c0eaff..124d908 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -3,7 +3,7 @@ void GB_update_joyp(GB_gameboy_t *gb) { - if (gb->model & GB_MODEL_SGB_NO_SFC) return; + if (gb->model & GB_MODEL_NO_SFC_BIT) return; uint8_t key_selection = 0; uint8_t previous_state = 0; From ffb9f1b134e7de017c1a22c8a2ca471c176171b7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 20:34:26 +0300 Subject: [PATCH 089/341] Fix HLE SGB --- Core/display.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Core/display.c b/Core/display.c index fc23044..02dd9a8 100644 --- a/Core/display.c +++ b/Core/display.c @@ -377,7 +377,6 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } uint8_t icd_pixel = 0; - { uint8_t pixel = bg_enabled? fifo_item->pixel : 0; if (pixel && bg_priority) { @@ -394,7 +393,6 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) else if (gb->model & GB_MODEL_NO_SFC_BIT) { if (gb->icd_pixel_callback) { icd_pixel = pixel; - //gb->icd_pixel_callback(gb, pixel); } } else { @@ -770,9 +768,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; - if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { - display_vblank(gb); - } gb->fetcher_x = ((gb->io_registers[GB_IO_SCX]) / 8) & 0x1f; gb->extra_penalty_for_sprite_at_0 = (gb->io_registers[GB_IO_SCX] & 7); @@ -912,6 +907,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) // Todo: unverified timing gb->current_lcd_line++; + if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { + display_vblank(gb); + } if (gb->icd_hreset_callback) { gb->icd_hreset_callback(gb); } From 8c1f76a5948f8e9a8e2fd14fab8d6b0f2c8ab891 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 20:37:58 +0300 Subject: [PATCH 090/341] Fix HLE SGB --- Core/display.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Core/display.c b/Core/display.c index 02dd9a8..25e95e7 100644 --- a/Core/display.c +++ b/Core/display.c @@ -905,11 +905,12 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line); gb->mode_for_interrupt = 2; - // Todo: unverified timing - gb->current_lcd_line++; - if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { - display_vblank(gb); - } + // Todo: unverified timing + gb->current_lcd_line++; + if (gb->current_lcd_line == LINES && GB_is_sgb(gb)) { + display_vblank(gb); + } + if (gb->icd_hreset_callback) { gb->icd_hreset_callback(gb); } From 1a263a3accfb8d40cc13d6f0d2b5eda2cba8c8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Zumer?= Date: Fri, 19 Jul 2019 16:55:59 -0400 Subject: [PATCH 091/341] Fix GBC memory map and add IO port range for cheevos --- libretro/libretro.c | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index bd93e97..e075b2c 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -380,7 +380,7 @@ static void init_for_current_model(unsigned id) set_link_cable_state(true); } - struct retro_memory_descriptor descs[10]; + struct retro_memory_descriptor descs[11]; size_t size; uint16_t bank; @@ -389,47 +389,55 @@ static void init_for_current_model(unsigned id) i = 0; memset(descs, 0, sizeof(descs)); - descs[0].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IE, &size, &bank); - descs[0].start = 0xFFFF; - descs[0].len = 1; + descs[0].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IE, &size, &bank); + descs[0].start = 0xFFFF; + descs[0].len = 1; - descs[1].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_HRAM, &size, &bank); - descs[1].start = 0xFF80; - descs[1].len = 0x0080; + descs[1].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_HRAM, &size, &bank); + descs[1].start = 0xFF80; + descs[1].len = 0x0080; - descs[2].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_RAM, &size, &bank); - descs[2].start = 0xC000; - descs[2].len = 0x1000; + descs[2].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_RAM, &size, &bank); + descs[2].start = 0xC000; + descs[2].len = 0x1000; - descs[3].ptr = descs[2].ptr + 0x1000; /* GB RAM/GBC RAM bank 1 */ - descs[3].start = 0xD000; - descs[3].len = 0x1000; + descs[3].ptr = descs[2].ptr + 0x1000; /* GB RAM/GBC RAM bank 1 */ + descs[3].start = 0xD000; + descs[3].len = 0x1000; - descs[4].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_CART_RAM, &size, &bank); - descs[4].start = 0xA000; - descs[4].len = 0x2000; + descs[4].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_CART_RAM, &size, &bank); + descs[4].start = 0xA000; + descs[4].len = 0x2000; - descs[5].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_VRAM, &size, &bank); - descs[5].start = 0x8000; - descs[5].len = 0x2000; + descs[5].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_VRAM, &size, &bank); + descs[5].start = 0x8000; + descs[5].len = 0x2000; - descs[6].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_ROM, &size, &bank); - descs[6].start = 0x0000; - descs[6].len = 0x4000; - descs[6].flags = RETRO_MEMDESC_CONST; + descs[6].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_ROM, &size, &bank); + descs[6].start = 0x0000; + descs[6].len = 0x4000; + descs[6].flags = RETRO_MEMDESC_CONST; - descs[7].ptr = descs[6].ptr + (bank * 0x4000); - descs[7].start = 0x4000; - descs[7].len = 0x4000; - descs[7].flags = RETRO_MEMDESC_CONST; + descs[7].ptr = descs[6].ptr + (bank * 0x4000); + descs[7].start = 0x4000; + descs[7].len = 0x4000; + descs[7].flags = RETRO_MEMDESC_CONST; - descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank); - descs[8].start = 0xFE00; - descs[8].len = 0x00A0; + descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank); + descs[8].start = 0xFE00; + descs[8].select = 0xFFFFFF00; + descs[8].len = 0x00A0; - descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ - descs[9].start = 0x10000; - descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ + descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ + descs[9].start = 0x10000; + descs[9].select = 0xFFFF0000; + descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ + + descs[10].ptr = descs[8].ptr; + descs[10].offset = 0x100; + descs[10].start = 0xFF00; + descs[10].select = 0xFFFFFF00; + descs[10].len = 0x0080; struct retro_memory_map mmaps; mmaps.descriptors = descs; From d2e9025be65ce95b5ff15683af7dc6204f001588 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jul 2019 23:59:25 +0300 Subject: [PATCH 092/341] Fixed major performence issues in the Cocoa port that affected some Macs, especially when emulating SGB1 --- Cocoa/GBViewMetal.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 94b4975..fde4b7e 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -50,6 +50,7 @@ static const vector_float2 rect[] = view.delegate = self; self.internalView = view; view.paused = YES; + view.enableSetNeedsDisplay = YES; vertices = [device newBufferWithBytes:rect length:sizeof(rect) @@ -206,7 +207,7 @@ static const vector_float2 rect[] = { [super flip]; dispatch_async(dispatch_get_main_queue(), ^{ - [(MTKView *)self.internalView draw]; + [(MTKView *)self.internalView setNeedsDisplay:YES]; }); } From e3672e829339c604952eb4ee13b75828a90bff21 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 20 Jul 2019 16:10:24 +0300 Subject: [PATCH 093/341] Emulate built in SGB palettes --- Core/sgb.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++------ Core/sgb.h | 3 ++ 2 files changed, 93 insertions(+), 10 deletions(-) diff --git a/Core/sgb.c b/Core/sgb.c index ccc439a..7d648f7 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -68,6 +68,75 @@ static inline void load_attribute_file(GB_gameboy_t *gb, unsigned file_index) } } +static const uint16_t built_in_palettes[] = +{ + 0x67BF, 0x265B, 0x10B5, 0x2866, + 0x637B, 0x3AD9, 0x0956, 0x0000, + 0x7F1F, 0x2A7D, 0x30F3, 0x4CE7, + 0x57FF, 0x2618, 0x001F, 0x006A, + 0x5B7F, 0x3F0F, 0x222D, 0x10EB, + 0x7FBB, 0x2A3C, 0x0015, 0x0900, + 0x2800, 0x7680, 0x01EF, 0x2FFF, + 0x73BF, 0x46FF, 0x0110, 0x0066, + 0x533E, 0x2638, 0x01E5, 0x0000, + 0x7FFF, 0x2BBF, 0x00DF, 0x2C0A, + 0x7F1F, 0x463D, 0x74CF, 0x4CA5, + 0x53FF, 0x03E0, 0x00DF, 0x2800, + 0x433F, 0x72D2, 0x3045, 0x0822, + 0x7FFA, 0x2A5F, 0x0014, 0x0003, + 0x1EED, 0x215C, 0x42FC, 0x0060, + 0x7FFF, 0x5EF7, 0x39CE, 0x0000, + 0x4F5F, 0x630E, 0x159F, 0x3126, + 0x637B, 0x121C, 0x0140, 0x0840, + 0x66BC, 0x3FFF, 0x7EE0, 0x2C84, + 0x5FFE, 0x3EBC, 0x0321, 0x0000, + 0x63FF, 0x36DC, 0x11F6, 0x392A, + 0x65EF, 0x7DBF, 0x035F, 0x2108, + 0x2B6C, 0x7FFF, 0x1CD9, 0x0007, + 0x53FC, 0x1F2F, 0x0E29, 0x0061, + 0x36BE, 0x7EAF, 0x681A, 0x3C00, + 0x7BBE, 0x329D, 0x1DE8, 0x0423, + 0x739F, 0x6A9B, 0x7293, 0x0001, + 0x5FFF, 0x6732, 0x3DA9, 0x2481, + 0x577F, 0x3EBC, 0x456F, 0x1880, + 0x6B57, 0x6E1B, 0x5010, 0x0007, + 0x0F96, 0x2C97, 0x0045, 0x3200, + 0x67FF, 0x2F17, 0x2230, 0x1548, +}; + +static const struct { + char name[16]; + unsigned palette_index; +} palette_assignments[] = +{ + {"ZELDA", 5}, + {"SUPER MARIOLAND", 6}, + {"MARIOLAND2", 0x14}, + {"SUPERMARIOLAND3", 2}, + {"KIRBY DREAM LAND", 0xB}, + {"HOSHINOKA-BI", 0xB}, + {"KIRBY'S PINBALL", 3}, + {"YOSSY NO TAMAGO", 0xC}, + {"MARIO & YOSHI", 0xC}, + {"YOSSY NO COOKIE", 4}, + {"YOSHI'S COOKIE", 4}, + {"DR.MARIO", 0x12}, + {"TETRIS", 0x11}, + {"YAKUMAN", 0x13}, + {"METROID2", 0x1F}, + {"KAERUNOTAMENI", 9}, + {"GOLF", 0x18}, + {"ALLEY WAY", 0x16}, + {"BASEBALL", 0xF}, + {"TENNIS", 0x17}, + {"F1RACE", 0x1E}, + {"KID ICARUS", 0xE}, + {"QIX", 0x19}, + {"SOLARSTRIKER", 7}, + {"X", 0x1C}, + {"GBWARS", 0x15}, +}; + static void command_ready(GB_gameboy_t *gb) { /* SGB header commands are used to send the contents of the header to the SNES CPU. @@ -77,6 +146,8 @@ static void command_ready(GB_gameboy_t *gb) 0xE content bytes. The last command, FB, is padded with zeros, so information past the header is not sent. */ if ((gb->sgb->command[0] & 0xF1) == 0xF1) { + if(gb->boot_rom_finished) return; + uint8_t checksum = 0; for (unsigned i = 2; i < 0x10; i++) { checksum += gb->sgb->command[i]; @@ -86,14 +157,23 @@ static void command_ready(GB_gameboy_t *gb) gb->sgb->disable_commands = true; return; } - if (gb->sgb->command[0] == 0xf9) { - if (gb->sgb->command[0xc] != 3) { // SGB Flag - gb->sgb->disable_commands = true; - } + unsigned index = (gb->sgb->command[0] >> 1) & 7; + if (index > 5) { + return; } - else if (gb->sgb->command[0] == 0xfb) { - if (gb->sgb->command[0x3] != 0x33) { // Old licensee code + memcpy(&gb->sgb->received_header[index * 14], &gb->sgb->command[2], 14); + if (gb->sgb->command[0] == 0xfb) { + if (gb->sgb->received_header[0x42] != 3 || gb->sgb->received_header[0x47] != 0x33) { gb->sgb->disable_commands = true; + for (unsigned i = 0; i < sizeof(palette_assignments) / sizeof(palette_assignments[0]); i++) { + if (memcmp(palette_assignments[i].name, &gb->sgb->received_header[0x30], sizeof(palette_assignments[i].name)) == 0) { + gb->sgb->effective_palettes[0] = built_in_palettes[palette_assignments[i].palette_index * 4 - 4]; + gb->sgb->effective_palettes[1] = built_in_palettes[palette_assignments[i].palette_index * 4 + 1 - 4]; + gb->sgb->effective_palettes[2] = built_in_palettes[palette_assignments[i].palette_index * 4 + 2 - 4]; + gb->sgb->effective_palettes[3] = built_in_palettes[palette_assignments[i].palette_index * 4 + 3 - 4]; + break; + } + } } } return; @@ -675,10 +755,10 @@ void GB_sgb_load_default_data(GB_gameboy_t *gb) /* Re-center */ memmove(&gb->sgb->border.map[25 * 32 + 1], &gb->sgb->border.map[25 * 32], (32 * 3 - 1) * sizeof(gb->sgb->border.map[0])); } - gb->sgb->effective_palettes[0] = 0x639E; - gb->sgb->effective_palettes[1] = 0x263A; - gb->sgb->effective_palettes[2] = 0x10D4; - gb->sgb->effective_palettes[3] = 0x2866; + gb->sgb->effective_palettes[0] = built_in_palettes[0]; + gb->sgb->effective_palettes[1] = built_in_palettes[1]; + gb->sgb->effective_palettes[2] = built_in_palettes[2]; + gb->sgb->effective_palettes[3] = built_in_palettes[3]; } static double fm_synth(double phase) diff --git a/Core/sgb.h b/Core/sgb.h index 2c6e8ee..49bf6d9 100644 --- a/Core/sgb.h +++ b/Core/sgb.h @@ -49,6 +49,9 @@ struct GB_sgb_s { /* Intro */ int16_t intro_animation; + + /* GB Header */ + uint8_t received_header[0x54]; }; void GB_sgb_write(GB_gameboy_t *gb, uint8_t value); From eaa1c1cd4a28fbe354286293b3b12aab9ca3c0c2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 16 Aug 2019 17:38:43 +0300 Subject: [PATCH 094/341] =?UTF-8?q?Merge=20bsnes=E2=80=99s=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/gb.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ Core/gb.h | 8 +++- Core/memory.c | 5 +++ Core/memory.h | 3 ++ Core/sgb.c | 4 ++ Core/timing.c | 2 +- 6 files changed, 135 insertions(+), 2 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 1e940cf..e91420c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -251,6 +251,48 @@ typedef union { } vba64; } GB_rtc_save_t; +int GB_save_battery_size(GB_gameboy_t *gb) +{ + if (!gb->cartridge_type->has_battery) return 0; // Nothing to save. + if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */ + + GB_rtc_save_t rtc_save_size; + return gb->mbc_ram_size + (gb->cartridge_type->has_rtc ? sizeof(rtc_save_size.vba64) : 0); +} + +int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size) +{ + if (!gb->cartridge_type->has_battery) return 0; // Nothing to save. + if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */ + + if (size < GB_save_battery_size(gb)) return EIO; + + memcpy(buffer, gb->mbc_ram, gb->mbc_ram_size); + + if (gb->cartridge_type->has_rtc) { + GB_rtc_save_t rtc_save = {{{{0,}},},}; + rtc_save.vba64.rtc_real.seconds = gb->rtc_real.seconds; + rtc_save.vba64.rtc_real.minutes = gb->rtc_real.minutes; + rtc_save.vba64.rtc_real.hours = gb->rtc_real.hours; + rtc_save.vba64.rtc_real.days = gb->rtc_real.days; + rtc_save.vba64.rtc_real.high = gb->rtc_real.high; + rtc_save.vba64.rtc_latched.seconds = gb->rtc_latched.seconds; + rtc_save.vba64.rtc_latched.minutes = gb->rtc_latched.minutes; + rtc_save.vba64.rtc_latched.hours = gb->rtc_latched.hours; + rtc_save.vba64.rtc_latched.days = gb->rtc_latched.days; + rtc_save.vba64.rtc_latched.high = gb->rtc_latched.high; +#ifdef GB_BIG_ENDIAN + rtc_save.vba64.last_rtc_second = __builtin_bswap64(gb->last_rtc_second); +#else + rtc_save.vba64.last_rtc_second = gb->last_rtc_second; +#endif + memcpy(buffer + gb->mbc_ram_size, &rtc_save.vba64, sizeof(rtc_save.vba64)); + } + + errno = 0; + return errno; +} + int GB_save_battery(GB_gameboy_t *gb, const char *path) { if (!gb->cartridge_type->has_battery) return 0; // Nothing to save. @@ -294,6 +336,79 @@ int GB_save_battery(GB_gameboy_t *gb, const char *path) return errno; } +void GB_load_battery_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size) +{ + memcpy(gb->mbc_ram, buffer, MIN(gb->mbc_ram_size, size)); + if (size <= gb->mbc_ram_size) { + goto reset_rtc; + } + + GB_rtc_save_t rtc_save; + memcpy(&rtc_save, buffer + gb->mbc_ram_size, MIN(sizeof(rtc_save), size)); + switch (size - gb->mbc_ram_size) { + case sizeof(rtc_save.sameboy_legacy): + memcpy(&gb->rtc_real, &rtc_save.sameboy_legacy.rtc_real, sizeof(gb->rtc_real)); + memcpy(&gb->rtc_latched, &rtc_save.sameboy_legacy.rtc_real, sizeof(gb->rtc_real)); + gb->last_rtc_second = rtc_save.sameboy_legacy.last_rtc_second; + break; + + case sizeof(rtc_save.vba32): + gb->rtc_real.seconds = rtc_save.vba32.rtc_real.seconds; + gb->rtc_real.minutes = rtc_save.vba32.rtc_real.minutes; + gb->rtc_real.hours = rtc_save.vba32.rtc_real.hours; + gb->rtc_real.days = rtc_save.vba32.rtc_real.days; + gb->rtc_real.high = rtc_save.vba32.rtc_real.high; + gb->rtc_latched.seconds = rtc_save.vba32.rtc_latched.seconds; + gb->rtc_latched.minutes = rtc_save.vba32.rtc_latched.minutes; + gb->rtc_latched.hours = rtc_save.vba32.rtc_latched.hours; + gb->rtc_latched.days = rtc_save.vba32.rtc_latched.days; + gb->rtc_latched.high = rtc_save.vba32.rtc_latched.high; +#ifdef GB_BIG_ENDIAN + gb->last_rtc_second = __builtin_bswap32(rtc_save.vba32.last_rtc_second); +#else + gb->last_rtc_second = rtc_save.vba32.last_rtc_second; +#endif + break; + + case sizeof(rtc_save.vba64): + gb->rtc_real.seconds = rtc_save.vba64.rtc_real.seconds; + gb->rtc_real.minutes = rtc_save.vba64.rtc_real.minutes; + gb->rtc_real.hours = rtc_save.vba64.rtc_real.hours; + gb->rtc_real.days = rtc_save.vba64.rtc_real.days; + gb->rtc_real.high = rtc_save.vba64.rtc_real.high; + gb->rtc_latched.seconds = rtc_save.vba64.rtc_latched.seconds; + gb->rtc_latched.minutes = rtc_save.vba64.rtc_latched.minutes; + gb->rtc_latched.hours = rtc_save.vba64.rtc_latched.hours; + gb->rtc_latched.days = rtc_save.vba64.rtc_latched.days; + gb->rtc_latched.high = rtc_save.vba64.rtc_latched.high; +#ifdef GB_BIG_ENDIAN + gb->last_rtc_second = __builtin_bswap64(rtc_save.vba64.last_rtc_second); +#else + gb->last_rtc_second = rtc_save.vba64.last_rtc_second; +#endif + break; + + default: + goto reset_rtc; + } + if (gb->last_rtc_second > time(NULL)) { + /* We must reset RTC here, or it will not advance. */ + goto reset_rtc; + } + + if (gb->last_rtc_second < 852076800) { /* 1/1/97. There weren't any RTC games that time, + so if the value we read is lower it means it wasn't + really RTC data. */ + goto reset_rtc; + } + goto exit; +reset_rtc: + gb->last_rtc_second = time(NULL); + gb->rtc_real.high |= 0x80; /* This gives the game a hint that the clock should be reset. */ +exit: + return; +} + /* Loading will silently stop if the format is incomplete */ void GB_load_battery(GB_gameboy_t *gb, const char *path) { diff --git a/Core/gb.h b/Core/gb.h index e12fcfa..59a11c2 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -1,5 +1,6 @@ #ifndef GB_h #define GB_h +#define typeof __typeof__ #include #include #include @@ -542,6 +543,7 @@ struct GB_gameboy_internal_s { GB_icd_pixel_callback_t icd_pixel_callback; GB_icd_vreset_callback_t icd_hreset_callback; GB_icd_vreset_callback_t icd_vreset_callback; + GB_read_memory_callback_t read_memory_callback; /* IR */ long cycles_since_ir_change; // In 8MHz units @@ -668,8 +670,12 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path); void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size); int GB_load_rom(GB_gameboy_t *gb, const char *path); void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size); - + +int GB_save_battery_size(GB_gameboy_t *gb); +int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size); int GB_save_battery(GB_gameboy_t *gb, const char *path); + +void GB_load_battery_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size); void GB_load_battery(GB_gameboy_t *gb, const char *path); void GB_set_turbo_mode(GB_gameboy_t *gb, bool on, bool no_frame_skip); diff --git a/Core/memory.c b/Core/memory.c index 76d8821..40ea00f 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -429,6 +429,11 @@ uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr) if (is_addr_in_dma_use(gb, addr)) { addr = gb->dma_current_src; } + if (gb->read_memory_callback) { + uint8_t data = read_map[addr >> 12](gb, addr); + data = gb->read_memory_callback(gb, addr, data); + return data; + } return read_map[addr >> 12](gb, addr); } diff --git a/Core/memory.h b/Core/memory.h index 03d636d..f0d0390 100644 --- a/Core/memory.h +++ b/Core/memory.h @@ -3,6 +3,9 @@ #include "gb_struct_def.h" #include +typedef uint8_t (*GB_read_memory_callback_t)(GB_gameboy_t *gb, uint16_t addr, uint8_t data); +void GB_set_read_memory_callback(GB_gameboy_t *gb, GB_read_memory_callback_t callback); + uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr); void GB_write_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value); #ifdef GB_INTERNAL diff --git a/Core/sgb.c b/Core/sgb.c index 7d648f7..c36bc3c 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -3,6 +3,10 @@ #include #include +#ifndef M_PI + #define M_PI 3.14159265358979323846 +#endif + #define INTRO_ANIMATION_LENGTH 200 enum { diff --git a/Core/timing.c b/Core/timing.c index 138819f..283558c 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -3,7 +3,7 @@ #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0500 #endif -#include +#include #else #include #endif From 4fcc921b46e515b2909fe76eb22ef46f1118353e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 13 Sep 2019 13:13:28 +0300 Subject: [PATCH 095/341] Fix SGB multiplayer, improve multiplayer accuracy --- Core/gb.c | 2 +- Core/joypad.c | 4 ++-- Core/memory.c | 8 +++++--- Core/sgb.c | 13 ++++++++----- Core/sgb.h | 3 +++ 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index e91420c..6604e4f 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -858,7 +858,7 @@ void GB_reset(GB_gameboy_t *gb) gb->mbc_rom_bank = 1; gb->last_rtc_second = time(NULL); gb->cgb_ram_bank = 1; - gb->io_registers[GB_IO_JOYP] = 0xF; + gb->io_registers[GB_IO_JOYP] = 0xCF; gb->mbc_ram_size = mbc_ram_size; if (GB_is_cgb(gb)) { gb->ram_size = 0x1000 * 8; diff --git a/Core/joypad.c b/Core/joypad.c index 124d908..91f6ae3 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -12,7 +12,7 @@ void GB_update_joyp(GB_gameboy_t *gb) previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; key_selection = (gb->io_registers[GB_IO_JOYP] >> 4) & 3; gb->io_registers[GB_IO_JOYP] &= 0xF0; - uint8_t current_player = gb->sgb? gb->sgb->current_player : 0; + uint8_t current_player = gb->sgb? (gb->sgb->current_player & (gb->sgb->player_count - 1)) : 0; switch (key_selection) { case 3: if (gb->sgb && gb->sgb->player_count > 1) { @@ -73,7 +73,7 @@ void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value) if (previous_state & ~(gb->io_registers[GB_IO_JOYP] & 0xF)) { gb->io_registers[GB_IO_IF] |= 0x10; } - + gb->io_registers[GB_IO_JOYP] |= 0xC0; } void GB_set_key_state(GB_gameboy_t *gb, GB_key_t index, bool pressed) diff --git a/Core/memory.c b/Core/memory.c index 40ea00f..a218f8e 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -746,9 +746,11 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; case GB_IO_JOYP: - gb->io_registers[GB_IO_JOYP] = value & 0xF0; - GB_sgb_write(gb, value); - GB_update_joyp(gb); + if ((gb->io_registers[GB_IO_JOYP] & 0x30) != (value & 0x30)) { + gb->io_registers[GB_IO_JOYP] = value & 0xF0; + GB_sgb_write(gb, value); + GB_update_joyp(gb); + } return; case GB_IO_BIOS: diff --git a/Core/sgb.c b/Core/sgb.c index c36bc3c..d0ac472 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -338,7 +338,6 @@ static void command_ready(GB_gameboy_t *gb) break; case MLT_REQ: gb->sgb->player_count = (uint8_t[]){1, 2, 1, 4}[gb->sgb->command[1] & 3]; - gb->sgb->current_player = gb->sgb->player_count - 1; break; case CHR_TRN: gb->sgb->vram_transfer_countdown = 2; @@ -382,13 +381,16 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) if (gb->joyp_write_callback) { gb->joyp_write_callback(gb, value); } + if (!GB_is_sgb(gb)) return; if (!GB_is_hle_sgb(gb)) { /* Notify via callback */ return; } if (gb->sgb->disable_commands) return; - if (gb->sgb->command_write_index >= sizeof(gb->sgb->command) * 8) return; + if (gb->sgb->command_write_index >= sizeof(gb->sgb->command) * 8) { + return; + } uint16_t command_size = (gb->sgb->command[0] & 7 ?: 1) * SGB_PACKET_SIZE * 8; if ((gb->sgb->command[0] & 0xF1) == 0xF1) { @@ -398,10 +400,10 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) switch ((value >> 4) & 3) { case 3: gb->sgb->ready_for_pulse = true; - /* TODO: This is the logic used by BGB which *should* work for most/all games, but a proper test ROM is needed */ - if (gb->sgb->player_count > 1 && (gb->io_registers[GB_IO_JOYP] & 0x30) == 0x10) { + if (gb->sgb->player_count > 1 && !gb->sgb->mlt_lock) { gb->sgb->current_player++; - gb->sgb->current_player &= gb->sgb->player_count - 1; + gb->sgb->current_player &= 3; + gb->sgb->mlt_lock = true; } break; @@ -426,6 +428,7 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) } break; case 1: // One + gb->sgb->mlt_lock ^= true; if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; if (gb->sgb->ready_for_stop) { GB_log(gb, "Corrupt SGB command.\n"); diff --git a/Core/sgb.h b/Core/sgb.h index 49bf6d9..df90253 100644 --- a/Core/sgb.h +++ b/Core/sgb.h @@ -52,6 +52,9 @@ struct GB_sgb_s { /* GB Header */ uint8_t received_header[0x54]; + + /* Multiplayer (cont) */ + bool mlt_lock; }; void GB_sgb_write(GB_gameboy_t *gb, uint8_t value); From 851dbd3ccd5533f2140f11a182ea6ff61557cbc0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 13 Sep 2019 17:13:21 +0300 Subject: [PATCH 096/341] SGB and AGB color correction --- Core/display.c | 41 ++++++++++++++++++++++++++++++++++------- Core/sgb.c | 21 +++------------------ 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Core/display.c b/Core/display.c index 25e95e7..5c1935c 100644 --- a/Core/display.c +++ b/Core/display.c @@ -162,9 +162,20 @@ static inline uint8_t scale_channel(uint8_t x) static inline uint8_t scale_channel_with_curve(uint8_t x) { - return (uint8_t[]){0,2,4,7,12,18,25,34,42,52,62,73,85,97,109,121,134,146,158,170,182,193,203,213,221,230,237,243,248,251,253,255,}[x]; + return (uint8_t[]){0,2,4,7,12,18,25,34,42,52,62,73,85,97,109,121,134,146,158,170,182,193,203,213,221,230,237,243,248,251,253,255}[x]; } +static inline uint8_t scale_channel_with_curve_agb(uint8_t x) +{ + return (uint8_t[]){0,2,5,10,15,20,26,32,38,45,52,60,68,76,84,92,101,110,119,128,138,148,158,168,178,189,199,210,221,232,244,255}[x]; +} + +static inline uint8_t scale_channel_with_curve_sgb(uint8_t x) +{ + return (uint8_t[]){0,2,5,9,15,20,27,34,42,50,58,67,76,85,94,104,114,123,133,143,153,163,173,182,192,202,211,220,229,238,247,255}[x]; +} + + uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color) { uint8_t r = (color) & 0x1F; @@ -177,13 +188,29 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color) b = scale_channel(b); } else { - r = scale_channel_with_curve(r); - g = scale_channel_with_curve(g); - b = scale_channel_with_curve(b); + if (GB_is_sgb(gb)) { + return gb->rgb_encode_callback(gb, + scale_channel_with_curve_sgb(r), + scale_channel_with_curve_sgb(g), + scale_channel_with_curve_sgb(b)); + } + bool agb = gb->model == GB_MODEL_AGB; + r = agb? scale_channel_with_curve_agb(r) : scale_channel_with_curve(r); + g = agb? scale_channel_with_curve_agb(g) : scale_channel_with_curve(g); + b = agb? scale_channel_with_curve_agb(b) : scale_channel_with_curve(b); if (gb->color_correction_mode != GB_COLOR_CORRECTION_CORRECT_CURVES) { - uint8_t new_g = (g * 3 + b) / 4; - uint8_t new_r = r, new_b = b; + uint8_t new_r, new_g, new_b; + if (agb) { + new_r = (r * 7 + g * 1) / 8; + new_g = (g * 3 + b * 1) / 4; + new_b = (b * 7 + r * 1) / 8; + } + else { + new_g = (g * 3 + b) / 4; + new_r = r; + new_b = b; + } if (gb->color_correction_mode == GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS) { uint8_t old_max = MAX(r, MAX(g, b)); uint8_t new_max = MAX(new_r, MAX(new_g, new_b)); @@ -200,7 +227,7 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color) if (new_min != 0xff) { new_r = 0xff - (0xff - new_r) * (0xff - old_min) / (0xff - new_min); new_g = 0xff - (0xff - new_g) * (0xff - old_min) / (0xff - new_min); - new_b = 0xff - (0xff - new_b) * (0xff - old_min) / (0xff - new_min);; + new_b = 0xff - (0xff - new_b) * (0xff - old_min) / (0xff - new_min); } } r = new_r; diff --git a/Core/sgb.c b/Core/sgb.c index d0ac472..18daa47 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -465,22 +465,9 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) } } -static inline uint8_t scale_channel(uint8_t x) -{ - return (x << 3) | (x >> 2); -} - static uint32_t convert_rgb15(GB_gameboy_t *gb, uint16_t color) { - uint8_t r = (color) & 0x1F; - uint8_t g = (color >> 5) & 0x1F; - uint8_t b = (color >> 10) & 0x1F; - - r = scale_channel(r); - g = scale_channel(g); - b = scale_channel(b); - - return gb->rgb_encode_callback(gb, r, g, b); + return GB_convert_rgb15(gb, color); } static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_t fade) @@ -493,11 +480,9 @@ static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_ if (g >= 0x20) g = 0; if (b >= 0x20) b = 0; - r = scale_channel(r); - g = scale_channel(g); - b = scale_channel(b); + color = r | (g << 5) | (b << 10); - return gb->rgb_encode_callback(gb, r, g, b); + return GB_convert_rgb15(gb, color); } #include From 652e52df3dbe5b36e5352a18f50aadf1479dd3c7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 14 Sep 2019 12:31:52 +0300 Subject: [PATCH 097/341] Pass the SGB multiplayer tests --- Core/joypad.c | 2 +- Core/memory.c | 2 +- Core/sgb.c | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Core/joypad.c b/Core/joypad.c index 91f6ae3..b8d4fdb 100644 --- a/Core/joypad.c +++ b/Core/joypad.c @@ -12,7 +12,7 @@ void GB_update_joyp(GB_gameboy_t *gb) previous_state = gb->io_registers[GB_IO_JOYP] & 0xF; key_selection = (gb->io_registers[GB_IO_JOYP] >> 4) & 3; gb->io_registers[GB_IO_JOYP] &= 0xF0; - uint8_t current_player = gb->sgb? (gb->sgb->current_player & (gb->sgb->player_count - 1)) : 0; + uint8_t current_player = gb->sgb? (gb->sgb->current_player & (gb->sgb->player_count - 1) & 3) : 0; switch (key_selection) { case 3: if (gb->sgb && gb->sgb->player_count > 1) { diff --git a/Core/memory.c b/Core/memory.c index a218f8e..4c4a702 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -747,8 +747,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_IO_JOYP: if ((gb->io_registers[GB_IO_JOYP] & 0x30) != (value & 0x30)) { - gb->io_registers[GB_IO_JOYP] = value & 0xF0; GB_sgb_write(gb, value); + gb->io_registers[GB_IO_JOYP] = value & 0xF0; GB_update_joyp(gb); } return; diff --git a/Core/sgb.c b/Core/sgb.c index 18daa47..8539238 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -337,7 +337,15 @@ static void command_ready(GB_gameboy_t *gb) // Not supported, but used by almost all SGB games for hot patching, so let's mute the warning for this break; case MLT_REQ: - gb->sgb->player_count = (uint8_t[]){1, 2, 1, 4}[gb->sgb->command[1] & 3]; + if (gb->sgb->player_count == 1) { + gb->sgb->current_player = 0; + } + gb->sgb->player_count = (gb->sgb->command[1] & 3) + 1; /* Todo: When breaking save state comaptibility, + fix this to be 0 based. */ + if (gb->sgb->player_count == 3) { + gb->sgb->current_player++; + } + gb->sgb->mlt_lock = true; break; case CHR_TRN: gb->sgb->vram_transfer_countdown = 2; @@ -397,10 +405,14 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) command_size = SGB_PACKET_SIZE * 8; } + if ((value & 0x20) == 0 && (gb->io_registers[GB_IO_JOYP] & 0x20) != 0) { + gb->sgb->mlt_lock ^= true; + } + switch ((value >> 4) & 3) { case 3: gb->sgb->ready_for_pulse = true; - if (gb->sgb->player_count > 1 && !gb->sgb->mlt_lock) { + if ((gb->sgb->player_count & 1) == 0 && !gb->sgb->mlt_lock) { gb->sgb->current_player++; gb->sgb->current_player &= 3; gb->sgb->mlt_lock = true; @@ -428,7 +440,6 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) } break; case 1: // One - gb->sgb->mlt_lock ^= true; if (!gb->sgb->ready_for_pulse || !gb->sgb->ready_for_write) return; if (gb->sgb->ready_for_stop) { GB_log(gb, "Corrupt SGB command.\n"); From 0c48ecb3f8292b063df78ded5eace3c675368236 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 14 Sep 2019 20:06:01 +0300 Subject: [PATCH 098/341] Updated version to 0.12.2 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2419061..79b5e14 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.12.1 +VERSION := 0.12.2 export VERSION CONF ?= debug From ac418b9de18765cdd8c0e94d7df46d5ca0820f0f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 30 Sep 2019 00:09:25 +0300 Subject: [PATCH 099/341] Pass channel_1_freq_change_timing --- Core/apu.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Core/apu.c b/Core/apu.c index ee58138..a6247ec 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -681,6 +681,21 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR14: case GB_IO_NR24: { unsigned index = reg == GB_IO_NR24? GB_SQUARE_2: GB_SQUARE_1; + + /* TODO: When the sample length changes right before being updated, the countdown should change to the + old length, but the current sample should not change. Because our write timing isn't accurate to + the T-cycle, we hack around it by stepping the sample index backwards. */ + if ((value & 0x80) == 0 && gb->apu.is_active[index]) { + /* On an AGB, as well as on CGB C and earlier (TODO: Tested: 0, B and C), it behaves slightly different on + double speed. */ + if (gb->model == GB_MODEL_CGB_E /* || gb->model == GB_MODEL_CGB_D */ || gb->apu.square_channels[index].sample_countdown & 1) { + if (gb->apu.square_channels[index].sample_countdown >> 1 == (gb->apu.square_channels[index].sample_length ^ 0x7FF)) { + gb->apu.square_channels[index].current_sample_index--; + gb->apu.square_channels[index].current_sample_index &= 7; + } + } + } + gb->apu.square_channels[index].sample_length &= 0xFF; gb->apu.square_channels[index].sample_length |= (value & 7) << 8; if (index == GB_SQUARE_1) { From ca370eee7e05224edfcc385b3c755b671645e732 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 1 Oct 2019 18:50:59 +0300 Subject: [PATCH 100/341] A bit more accurate AGB audio rendering --- Core/apu.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index a6247ec..55898b2 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -48,6 +48,23 @@ bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index) return false; } +static uint8_t agb_bias_for_channel(GB_gameboy_t *gb, unsigned index) +{ + if (!gb->apu.is_active[index]) return 0; + + switch (index) { + case GB_SQUARE_1: + return gb->apu.square_channels[GB_SQUARE_1].current_volume; + case GB_SQUARE_2: + return gb->apu.square_channels[GB_SQUARE_2].current_volume; + case GB_WAVE: + return 0; + case GB_NOISE: + return gb->apu.noise_channel.current_volume; + } + return 0; +} + static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsigned cycles_offset) { if (gb->model >= GB_MODEL_AGB) { @@ -66,15 +83,17 @@ static void update_sample(GB_gameboy_t *gb, unsigned index, int8_t value, unsign } GB_sample_t output; + uint8_t bias = agb_bias_for_channel(gb, index); + if (gb->io_registers[GB_IO_NR51] & (1 << index)) { - output.right = (0xf - value * 2) * right_volume; + output.right = (0xf - value * 2 + bias) * right_volume; } else { output.right = 0xf * right_volume; } if (gb->io_registers[GB_IO_NR51] & (0x10 << index)) { - output.left = (0xf - value * 2) * left_volume; + output.left = (0xf - value * 2 + bias) * left_volume; } else { output.left = 0xf * left_volume; From c50ea6a63fb76f4111e305048788071dd0541730 Mon Sep 17 00:00:00 2001 From: f21red <54341442+f21red@users.noreply.github.com> Date: Sat, 5 Oct 2019 20:24:32 -0500 Subject: [PATCH 101/341] libretro: sgb color correction --- libretro/libretro.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index e075b2c..8c00ac5 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -486,7 +486,7 @@ static void check_variables() { var.key = "sameboy_color_correction_mode"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gameboy[0])) + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); @@ -540,7 +540,7 @@ static void check_variables() { var.key = "sameboy_color_correction_mode_1"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gameboy[0])) + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); @@ -554,7 +554,7 @@ static void check_variables() var.key = "sameboy_color_correction_mode_2"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gameboy[1])) + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_DISABLED); From 0a7a0ca5fe28e34e479411d00c0ce3bc79c69334 Mon Sep 17 00:00:00 2001 From: f21red <54341442+f21red@users.noreply.github.com> Date: Sat, 5 Oct 2019 20:51:59 -0500 Subject: [PATCH 102/341] libretro: sgb border option --- libretro/libretro.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 8c00ac5..54c7906 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -92,6 +92,7 @@ static unsigned emulated_devices = 1; static bool initialized = false; static unsigned screen_layout = 0; static unsigned audio_out = 0; +static unsigned sgb_border = 1; static bool geometry_updated = false; static bool link_cable_emulation = false; @@ -204,6 +205,7 @@ static const struct retro_variable vars_single[] = { { "sameboy_color_correction_mode", "Color correction; off|correct curves|emulate hardware|preserve brightness" }, { "sameboy_high_pass_filter_mode", "High-pass filter; off|accurate|remove dc offset" }, { "sameboy_model", "Emulated model; Auto|Game Boy|Game Boy Color|Game Boy Advance|Super Game Boy|Super Game Boy 2" }, + { "sameboy_border", "Super Game Boy border; enabled|disabled" }, { NULL } }; @@ -535,6 +537,16 @@ static void check_variables() init_for_current_model(0); } } + + var.key = "sameboy_border"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + if (strcmp(var.value, "enabled") == 0) + sgb_border = 1; + else if (strcmp(var.value, "disabled") == 0) + sgb_border = 0; + } } else { @@ -880,8 +892,15 @@ void retro_run(void) } else { - if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) - video_cb(frame_buf, SGB_VIDEO_WIDTH, SGB_VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); + if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { + if (sgb_border == 1) + video_cb(frame_buf, SGB_VIDEO_WIDTH, SGB_VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); + else { + int crop = SGB_VIDEO_WIDTH * ((SGB_VIDEO_HEIGHT - VIDEO_HEIGHT) / 2) + ((SGB_VIDEO_WIDTH - VIDEO_WIDTH) / 2); + + video_cb(frame_buf + crop, VIDEO_WIDTH, VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); + } + } else video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); } From dee29c118cbded7703c2f7edcbc7230ffb70b844 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 8 Oct 2019 15:10:24 +0300 Subject: [PATCH 103/341] Added GB_set_sample_rate_by_clocks API, split SGB_NO_SFC into PAL and NTSC; now they report the correct clock rate. --- Core/apu.c | 15 +++++++++++++++ Core/apu.h | 3 +++ Core/gb.c | 20 ++++++++++++-------- Core/gb.h | 4 +++- Core/memory.c | 6 ++++-- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 55898b2..3be92d6 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1004,9 +1004,23 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate) if (sample_rate) { gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate); } + gb->apu_output.rate_set_in_clocks = false; GB_apu_update_cycles_per_sample(gb); } +void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, unsigned cycles_per_sample) +{ + + if (cycles_per_sample == 0) { + GB_set_sample_rate(gb, 0); + return; + } + gb->apu_output.cycles_per_sample = cycles_per_sample; + gb->apu_output.sample_rate = GB_get_clock_rate(gb) / cycles_per_sample * 2; + gb->apu_output.highpass_rate = pow(0.999958, cycles_per_sample); + gb->apu_output.rate_set_in_clocks = true; +} + void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback) { gb->apu_output.sample_callback = callback; @@ -1019,6 +1033,7 @@ void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode) void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb) { + if (gb->apu_output.rate_set_in_clocks) return; if (gb->apu_output.sample_rate) { gb->apu_output.cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */ } diff --git a/Core/apu.h b/Core/apu.h index 7f8acfc..ee6055b 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -143,9 +143,12 @@ typedef struct { GB_double_sample_t highpass_diff; GB_sample_callback_t sample_callback; + + bool rate_set_in_clocks; } GB_apu_output_t; void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate); +void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, unsigned cycles_per_sample); /* Cycles are in 8MHz units */ void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode); void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback); #ifdef GB_INTERNAL diff --git a/Core/gb.c b/Core/gb.c index 6604e4f..93ac9d7 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -712,7 +712,8 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ - case GB_MODEL_SGB_NO_SFC: + case GB_MODEL_SGB_NTSC_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_PAL_NO_SFC: /* Unverified */ for (unsigned i = 0; i < gb->ram_size; i++) { gb->ram[i] = GB_random(); if (i & 0x100) { @@ -758,7 +759,8 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ - case GB_MODEL_SGB_NO_SFC: + case GB_MODEL_SGB_NTSC_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_PAL_NO_SFC: /* Unverified */ case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: for (unsigned i = 0; i < sizeof(gb->hram); i++) { @@ -783,7 +785,8 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified */ case GB_MODEL_SGB_PAL: /* Unverified */ - case GB_MODEL_SGB_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_NTSC_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_PAL_NO_SFC: /* Unverified */ case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: for (unsigned i = 0; i < 8; i++) { @@ -811,7 +814,8 @@ static void reset_ram(GB_gameboy_t *gb) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: /* Unverified*/ case GB_MODEL_SGB_PAL: /* Unverified */ - case GB_MODEL_SGB_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_NTSC_NO_SFC: /* Unverified */ + case GB_MODEL_SGB_PAL_NO_SFC: /* Unverified */ case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: { uint8_t temp; @@ -1017,12 +1021,12 @@ void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier) uint32_t GB_get_clock_rate(GB_gameboy_t *gb) { - if (gb->model == GB_MODEL_SGB_NTSC) { - return SGB_NTSC_FREQUENCY * gb->clock_multiplier; - } - if (gb->model == GB_MODEL_SGB_PAL) { + if (gb->model & GB_MODEL_PAL_BIT) { return SGB_PAL_FREQUENCY * gb->clock_multiplier; } + if ((gb->model & ~GB_MODEL_NO_SFC_BIT) == GB_MODEL_SGB) { + return SGB_NTSC_FREQUENCY * gb->clock_multiplier; + } return CPU_FREQUENCY * gb->clock_multiplier; } diff --git a/Core/gb.h b/Core/gb.h index 59a11c2..dbd9e16 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -70,7 +70,9 @@ typedef enum { GB_MODEL_SGB = 0x004, GB_MODEL_SGB_NTSC = GB_MODEL_SGB, GB_MODEL_SGB_PAL = GB_MODEL_SGB | GB_MODEL_PAL_BIT, - GB_MODEL_SGB_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT, + GB_MODEL_SGB_NTSC_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT, + GB_MODEL_SGB_NO_SFC = GB_MODEL_SGB_NTSC_NO_SFC, + GB_MODEL_SGB_PAL_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT | GB_MODEL_PAL_BIT, // GB_MODEL_MGB = 0x100, GB_MODEL_SGB2 = 0x101, GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT, diff --git a/Core/memory.c b/Core/memory.c index 4c4a702..c481ad2 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -273,7 +273,8 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: case GB_MODEL_SGB_PAL: - case GB_MODEL_SGB_NO_SFC: + case GB_MODEL_SGB_NTSC_NO_SFC: + case GB_MODEL_SGB_PAL_NO_SFC: case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: ; @@ -591,7 +592,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_MODEL_DMG_B: case GB_MODEL_SGB_NTSC: case GB_MODEL_SGB_PAL: - case GB_MODEL_SGB_NO_SFC: + case GB_MODEL_SGB_NTSC_NO_SFC: + case GB_MODEL_SGB_PAL_NO_SFC: case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: case GB_MODEL_CGB_E: From 7d6cdf381974819d2c9b94a0d9344058bed90910 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 17 Oct 2019 21:21:10 +0300 Subject: [PATCH 104/341] =?UTF-8?q?Fix=20SGB=20support=20in=20SDL=E2=80=99?= =?UTF-8?q?s=20software=20rendering.=20Fixes=20#208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SDL/gui.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL/gui.c b/SDL/gui.c index 0723384..6cffeaf 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -33,7 +33,7 @@ void render_texture(void *pixels, void *previous) { if (renderer) { if (pixels) { - SDL_UpdateTexture(texture, NULL, pixels, 160 * sizeof (uint32_t)); + SDL_UpdateTexture(texture, NULL, pixels, GB_get_screen_width(&gb) * sizeof (uint32_t)); } SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); From 0ece21bca7b6d4cbcdab87d4ddbdcd1ea8399c1b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 19 Oct 2019 19:26:04 +0300 Subject: [PATCH 105/341] Replace the SDL-derived controller support with my own JoyKit framework. Adds rumble support, LED support, better manual and automatic configurations, analog speed controls. --- Cocoa/AppDelegate.m | 6 + Cocoa/Document.m | 15 +- Cocoa/GBButtons.h | 5 + Cocoa/GBJoystickListener.h | 9 - Cocoa/GBPreferencesWindow.h | 5 +- Cocoa/GBPreferencesWindow.m | 226 +++++---- Cocoa/GBView.h | 5 +- Cocoa/GBView.m | 216 ++++---- Cocoa/Preferences.xib | 72 +-- Cocoa/joypad.m | 748 ---------------------------- Cocoa/main.m | 3 +- Core/debugger.c | 2 +- Core/display.c | 7 + Core/gb.h | 4 +- Core/memory.c | 3 - Core/save_state.c | 8 - Core/timing.c | 8 + JoyKit/ControllerConfiguration.inc | 369 ++++++++++++++ JoyKit/JOYAxes2D.h | 24 + JoyKit/JOYAxes2D.m | 168 +++++++ JoyKit/JOYAxis.h | 29 ++ JoyKit/JOYAxis.m | 90 ++++ JoyKit/JOYButton.h | 42 ++ JoyKit/JOYButton.m | 102 ++++ JoyKit/JOYController.h | 41 ++ JoyKit/JOYController.m | 760 +++++++++++++++++++++++++++++ JoyKit/JOYElement.h | 20 + JoyKit/JOYElement.m | 96 ++++ JoyKit/JOYEmulatedButton.h | 11 + JoyKit/JOYEmulatedButton.m | 91 ++++ JoyKit/JOYHat.h | 11 + JoyKit/JOYHat.m | 60 +++ JoyKit/JOYMultiplayerController.h | 8 + JoyKit/JOYMultiplayerController.m | 44 ++ JoyKit/JOYSubElement.h | 14 + JoyKit/JOYSubElement.m | 99 ++++ JoyKit/JoyKit.h | 6 + Makefile | 2 +- libretro/libretro.c | 15 +- 39 files changed, 2418 insertions(+), 1026 deletions(-) delete mode 100644 Cocoa/GBJoystickListener.h delete mode 100755 Cocoa/joypad.m create mode 100644 JoyKit/ControllerConfiguration.inc create mode 100644 JoyKit/JOYAxes2D.h create mode 100644 JoyKit/JOYAxes2D.m create mode 100644 JoyKit/JOYAxis.h create mode 100644 JoyKit/JOYAxis.m create mode 100644 JoyKit/JOYButton.h create mode 100644 JoyKit/JOYButton.m create mode 100644 JoyKit/JOYController.h create mode 100644 JoyKit/JOYController.m create mode 100644 JoyKit/JOYElement.h create mode 100644 JoyKit/JOYElement.m create mode 100644 JoyKit/JOYEmulatedButton.h create mode 100644 JoyKit/JOYEmulatedButton.m create mode 100644 JoyKit/JOYHat.h create mode 100644 JoyKit/JOYHat.m create mode 100644 JoyKit/JOYMultiplayerController.h create mode 100644 JoyKit/JOYMultiplayerController.m create mode 100644 JoyKit/JOYSubElement.h create mode 100644 JoyKit/JOYSubElement.m create mode 100644 JoyKit/JoyKit.h diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index bbaa3ae..e966615 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -2,6 +2,7 @@ #include "GBButtons.h" #include #import +#import @implementation AppDelegate { @@ -41,6 +42,11 @@ @"GBCGBModel": @(GB_MODEL_CGB_E), @"GBSGBModel": @(GB_MODEL_SGB2), }]; + + [JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{ + JOYAxes2DEmulateButtonsKey: @YES, + JOYHatsEmulateButtonsKey: @YES, + }]; } - (IBAction)toggleDeveloperMode:(id)sender diff --git a/Cocoa/Document.m b/Cocoa/Document.m index a520d62..eef4c7a 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -74,6 +74,7 @@ enum model { topMargin:(unsigned) topMargin bottomMargin: (unsigned) bottomMargin exposure:(unsigned) exposure; - (void) gotNewSample:(GB_sample_t *)sample; +- (void) rumbleChanged:(double)amp; @end static void vblank(GB_gameboy_t *gb) @@ -131,6 +132,12 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self gotNewSample:sample]; } +static void rumbleCallback(GB_gameboy_t *gb, double amp) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + [self rumbleChanged:amp]; +} + @implementation Document { GB_gameboy_t gb; @@ -199,6 +206,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]); GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]); GB_apu_set_sample_callback(&gb, audioCallback); + GB_set_rumble_callback(&gb, rumbleCallback); } - (void) vblank @@ -244,6 +252,11 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [audioLock unlock]; } +- (void)rumbleChanged:(double)amp +{ + [_view setRumble:amp]; +} + - (void) run { running = true; @@ -295,6 +308,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.audioClient = nil; self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + [_view setRumble:false]; stopping = false; } @@ -1563,5 +1577,4 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console – %@", [[fileURL path] lastPathComponent]]; } - @end diff --git a/Cocoa/GBButtons.h b/Cocoa/GBButtons.h index 7b2ea5d..1f8b5af 100644 --- a/Cocoa/GBButtons.h +++ b/Cocoa/GBButtons.h @@ -19,6 +19,11 @@ typedef enum : NSUInteger { extern NSString const *GBButtonNames[GBButtonCount]; +static inline NSString *n2s(uint64_t number) +{ + return [NSString stringWithFormat:@"%llx", number]; +} + static inline NSString *button_to_preference_name(GBButton button, unsigned player) { if (player) { diff --git a/Cocoa/GBJoystickListener.h b/Cocoa/GBJoystickListener.h deleted file mode 100644 index 069db10..0000000 --- a/Cocoa/GBJoystickListener.h +++ /dev/null @@ -1,9 +0,0 @@ -#import - -@protocol GBJoystickListener - -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state; -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value; -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) value; - -@end diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 90eee54..cc308a0 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -1,9 +1,10 @@ #import -#import "GBJoystickListener.h" +#import -@interface GBPreferencesWindow : NSWindow +@interface GBPreferencesWindow : NSWindow @property IBOutlet NSTableView *controlsTableView; @property IBOutlet NSPopUpButton *graphicsFilterPopupButton; +@property (strong) IBOutlet NSButton *analogControlsCheckbox; @property (strong) IBOutlet NSButton *aspectRatioCheckbox; @property (strong) IBOutlet NSPopUpButton *highpassFilterPopupButton; @property (strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index ecf0311..97628f1 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -9,13 +9,14 @@ NSInteger button_being_modified; signed joystick_configuration_state; NSString *joystick_being_configured; - signed last_axis; + bool joypad_wait; NSPopUpButton *_graphicsFilterPopupButton; NSPopUpButton *_highpassFilterPopupButton; NSPopUpButton *_colorCorrectionPopupButton; NSPopUpButton *_rewindPopupButton; NSButton *_aspectRatioCheckbox; + NSButton *_analogControlsCheckbox; NSEventModifierFlags previousModifiers; NSPopUpButton *_dmgPopupButton, *_sgbPopupButton, *_cgbPopupButton; @@ -51,7 +52,7 @@ joystick_configuration_state = -1; [self.configureJoypadButton setEnabled:YES]; [self.skipButton setEnabled:NO]; - [self.configureJoypadButton setTitle:@"Configure Joypad"]; + [self.configureJoypadButton setTitle:@"Configure Controller"]; [super close]; } @@ -184,6 +185,12 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBHighpassFilterChanged" object:nil]; } +- (IBAction)changeAnalogControls:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] == NSOnState + forKey:@"GBAnalogControls"]; +} + - (IBAction)changeAspectRatio:(id)sender { [[NSUserDefaults standardUserDefaults] setBool: [(NSButton *)sender state] != NSOnState @@ -212,7 +219,6 @@ [self.skipButton setEnabled:YES]; joystick_being_configured = nil; [self advanceConfigurationStateMachine]; - last_axis = -1; } - (IBAction) skipButton:(id)sender @@ -223,11 +229,11 @@ - (void) advanceConfigurationStateMachine { joystick_configuration_state++; - if (joystick_configuration_state < GBButtonCount) { - [self.configureJoypadButton setTitle:[NSString stringWithFormat:@"Press Button for %@", GBButtonNames[joystick_configuration_state]]]; + if (joystick_configuration_state == GBUnderclock) { + [self.configureJoypadButton setTitle:@"Press Button for Slo-Mo"]; // Full name is too long :< } - else if (joystick_configuration_state == GBButtonCount) { - [self.configureJoypadButton setTitle:@"Move the Analog Stick"]; + else if (joystick_configuration_state < GBButtonCount) { + [self.configureJoypadButton setTitle:[NSString stringWithFormat:@"Press Button for %@", GBButtonNames[joystick_configuration_state]]]; } else { joystick_configuration_state = -1; @@ -237,112 +243,97 @@ } } -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state +- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button { - if (!state) return; + /* Debounce */ + if (joypad_wait) return; + joypad_wait = true; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + joypad_wait = false; + }); + + NSLog(@"%@", button); + + if (!button.isPressed) return; if (joystick_configuration_state == -1) return; if (joystick_configuration_state == GBButtonCount) return; if (!joystick_being_configured) { - joystick_being_configured = joystick_name; + joystick_being_configured = controller.uniqueID; } - else if (![joystick_being_configured isEqualToString:joystick_name]) { + else if (![joystick_being_configured isEqualToString:controller.uniqueID]) { return; } - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; + NSMutableDictionary *instance_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"] mutableCopy]; - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; + NSMutableDictionary *name_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"] mutableCopy]; + + + if (!instance_mappings) { + instance_mappings = [[NSMutableDictionary alloc] init]; } - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; + if (!name_mappings) { + name_mappings = [[NSMutableDictionary alloc] init]; + } - if (!mapping) { + NSMutableDictionary *mapping = nil; + if (joystick_configuration_state != 0) { + mapping = [instance_mappings[controller.uniqueID] mutableCopy]; + } + else { mapping = [[NSMutableDictionary alloc] init]; } + - mapping[GBButtonNames[joystick_configuration_state]] = @(button); + static const unsigned gb_to_joykit[] = { + [GBRight]=JOYButtonUsageDPadRight, + [GBLeft]=JOYButtonUsageDPadLeft, + [GBUp]=JOYButtonUsageDPadUp, + [GBDown]=JOYButtonUsageDPadDown, + [GBA]=JOYButtonUsageA, + [GBB]=JOYButtonUsageB, + [GBSelect]=JOYButtonUsageSelect, + [GBStart]=JOYButtonUsageStart, + [GBTurbo]=JOYButtonUsageL1, + [GBRewind]=JOYButtonUsageL2, + [GBUnderclock]=JOYButtonUsageR1, + }; - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self refreshJoypadMenu:nil]; + if (joystick_configuration_state == GBUnderclock) { + for (JOYAxis *axis in controller.axes) { + if (axis.value > 0.5) { + mapping[@"AnalogUnderclock"] = @(axis.uniqueID); + } + } + } + + if (joystick_configuration_state == GBTurbo) { + for (JOYAxis *axis in controller.axes) { + if (axis.value > 0.5) { + mapping[@"AnalogTurbo"] = @(axis.uniqueID); + } + } + } + + mapping[n2s(button.uniqueID)] = @(gb_to_joykit[joystick_configuration_state]); + + instance_mappings[controller.uniqueID] = mapping; + name_mappings[controller.deviceName] = mapping; + [[NSUserDefaults standardUserDefaults] setObject:instance_mappings forKey:@"JoyKitInstanceMapping"]; + [[NSUserDefaults standardUserDefaults] setObject:name_mappings forKey:@"JoyKitNameMapping"]; [self advanceConfigurationStateMachine]; } -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value +- (NSButton *)analogControlsCheckbox { - if (abs(value) < 0x4000) return; - if (joystick_configuration_state != GBButtonCount) return; - if (!joystick_being_configured) { - joystick_being_configured = joystick_name; - } - else if (![joystick_being_configured isEqualToString:joystick_name]) { - return; - } - - if (last_axis == -1) { - last_axis = axis; - return; - } - - if (axis == last_axis) { - return; - } - - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; - - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; - } - - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; - - if (!mapping) { - mapping = [[NSMutableDictionary alloc] init]; - } - - mapping[@"XAxis"] = @(MIN(axis, last_axis)); - mapping[@"YAxis"] = @(MAX(axis, last_axis)); - - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self advanceConfigurationStateMachine]; + return _analogControlsCheckbox; } -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state +- (void)setAnalogControlsCheckbox:(NSButton *)analogControlsCheckbox { - /* Hats are always mapped to the D-pad, ignore them on non-Dpad keys and skip the D-pad configuration if used*/ - if (!state) return; - if (joystick_configuration_state == -1) return; - if (joystick_configuration_state > GBDown) return; - if (!joystick_being_configured) { - joystick_being_configured = joystick_name; - } - else if (![joystick_being_configured isEqualToString:joystick_name]) { - return; - } - - NSMutableDictionary *all_mappings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] mutableCopy]; - - if (!all_mappings) { - all_mappings = [[NSMutableDictionary alloc] init]; - } - - NSMutableDictionary *mapping = [[all_mappings objectForKey:joystick_name] mutableCopy]; - - if (!mapping) { - mapping = [[NSMutableDictionary alloc] init]; - } - - for (joystick_configuration_state = 0;; joystick_configuration_state++) { - [mapping removeObjectForKey:GBButtonNames[joystick_configuration_state]]; - if (joystick_configuration_state == GBDown) break; - } - - all_mappings[joystick_name] = mapping; - [[NSUserDefaults standardUserDefaults] setObject:all_mappings forKey:@"GBJoypadMappings"]; - [self refreshJoypadMenu:nil]; - [self advanceConfigurationStateMachine]; + _analogControlsCheckbox = analogControlsCheckbox; + [_analogControlsCheckbox setState: [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]]; } - (NSButton *)aspectRatioCheckbox @@ -361,10 +352,13 @@ [super awakeFromNib]; [self updateBootROMFolderButton]; [[NSDistributedNotificationCenter defaultCenter] addObserver:self.controlsTableView selector:@selector(reloadData) name:(NSString*)kTISNotifySelectedKeyboardInputSourceChanged object:nil]; + [JOYController registerListener:self]; + joystick_configuration_state = -1; } - (void)dealloc { + [JOYController unregisterListener:self]; [[NSDistributedNotificationCenter defaultCenter] removeObserver:self.controlsTableView]; } @@ -483,21 +477,47 @@ return _preferredJoypadButton; } +- (void)controllerConnected:(JOYController *)controller +{ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self refreshJoypadMenu:nil]; + }); +} + +- (void)controllerDisconnected:(JOYController *)controller +{ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self refreshJoypadMenu:nil]; + }); +} + - (IBAction)refreshJoypadMenu:(id)sender { - NSArray *joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"] allKeys]; - for (NSString *joypad in joypads) { - if ([self.preferredJoypadButton indexOfItemWithTitle:joypad] == -1) { - [self.preferredJoypadButton addItemWithTitle:joypad]; + bool preferred_is_connected = false; + NSString *player_string = n2s(self.playerListButton.selectedTag); + NSString *selected_controller = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"][player_string]; + + [self.preferredJoypadButton removeAllItems]; + [self.preferredJoypadButton addItemWithTitle:@"None"]; + for (JOYController *controller in [JOYController allControllers]) { + [self.preferredJoypadButton addItemWithTitle:[NSString stringWithFormat:@"%@ (%@)", controller.deviceName, controller.uniqueID]]; + + self.preferredJoypadButton.lastItem.identifier = controller.uniqueID; + + if ([controller.uniqueID isEqualToString:selected_controller]) { + preferred_is_connected = true; + [self.preferredJoypadButton selectItem:self.preferredJoypadButton.lastItem]; } } - NSString *player_string = [NSString stringWithFormat: @"%ld", (long)self.playerListButton.selectedTag]; - NSString *selected_joypad = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"][player_string]; - if (selected_joypad && [self.preferredJoypadButton indexOfItemWithTitle:selected_joypad] != -1) { - [self.preferredJoypadButton selectItemWithTitle:selected_joypad]; + if (!preferred_is_connected && selected_controller) { + [self.preferredJoypadButton addItemWithTitle:[NSString stringWithFormat:@"Unavailable Controller (%@)", selected_controller]]; + self.preferredJoypadButton.lastItem.identifier = selected_controller; + [self.preferredJoypadButton selectItem:self.preferredJoypadButton.lastItem]; } - else { + + + if (!selected_controller) { [self.preferredJoypadButton selectItemWithTitle:@"None"]; } [self.controlsTableView reloadData]; @@ -505,18 +525,18 @@ - (IBAction)changeDefaultJoypad:(id)sender { - NSMutableDictionary *default_joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] mutableCopy]; + NSMutableDictionary *default_joypads = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"] mutableCopy]; if (!default_joypads) { default_joypads = [[NSMutableDictionary alloc] init]; } - NSString *player_string = [NSString stringWithFormat: @"%ld", self.playerListButton.selectedTag]; + NSString *player_string = n2s(self.playerListButton.selectedTag); if ([[sender titleOfSelectedItem] isEqualToString:@"None"]) { [default_joypads removeObjectForKey:player_string]; } else { - default_joypads[player_string] = [sender titleOfSelectedItem]; + default_joypads[player_string] = [[sender selectedItem] identifier]; } - [[NSUserDefaults standardUserDefaults] setObject:default_joypads forKey:@"GBDefaultJoypads"]; + [[NSUserDefaults standardUserDefaults] setObject:default_joypads forKey:@"JoyKitDefaultControllers"]; } @end diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index f4c5e44..20bc7bf 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -1,8 +1,8 @@ #import #include -#import "GBJoystickListener.h" +#import -@interface GBView : NSView +@interface GBView : NSView - (void) flip; - (uint32_t *) pixels; @property GB_gameboy_t *gb; @@ -14,4 +14,5 @@ - (uint32_t *)currentBuffer; - (uint32_t *)previousBuffer; - (void)screenSizeChanged; +- (void)setRumble: (bool)on; @end diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 5a851f3..adc0781 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -1,4 +1,4 @@ -#import +#import #import "GBView.h" #import "GBViewGL.h" #import "GBViewMetal.h" @@ -18,7 +18,10 @@ bool axisActive[2]; bool underclockKeyDown; double clockMultiplier; + double analogClockMultiplier; + bool analogClockMultiplierValid; NSEventModifierFlags previousModifiers; + JOYController *lastController; } + (instancetype)alloc @@ -55,6 +58,7 @@ [self createInternalView]; [self addSubview:self.internalView]; self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; + [JOYController registerListener:self]; } - (void)screenSizeChanged @@ -100,6 +104,8 @@ [NSCursor unhide]; } [[NSNotificationCenter defaultCenter] removeObserver:self]; + [lastController setRumbleAmplitude:0]; + [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder { @@ -147,13 +153,21 @@ - (void) flip { - if (underclockKeyDown && clockMultiplier > 0.5) { - clockMultiplier -= 1.0/16; - GB_set_clock_multiplier(_gb, clockMultiplier); + if (analogClockMultiplierValid && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]) { + GB_set_clock_multiplier(_gb, analogClockMultiplier); + if (analogClockMultiplier == 1.0) { + analogClockMultiplierValid = false; + } } - if (!underclockKeyDown && clockMultiplier < 1.0) { - clockMultiplier += 1.0/16; - GB_set_clock_multiplier(_gb, clockMultiplier); + else { + if (underclockKeyDown && clockMultiplier > 0.5) { + clockMultiplier -= 1.0/16; + GB_set_clock_multiplier(_gb, clockMultiplier); + } + if (!underclockKeyDown && clockMultiplier < 1.0) { + clockMultiplier += 1.0/16; + GB_set_clock_multiplier(_gb, clockMultiplier); + } } current_buffer = (current_buffer + 1) % self.numberOfBuffers; } @@ -180,6 +194,7 @@ switch (button) { case GBTurbo: GB_set_turbo_mode(_gb, true, self.isRewinding); + analogClockMultiplierValid = false; break; case GBRewind: @@ -189,6 +204,7 @@ case GBUnderclock: underclockKeyDown = true; + analogClockMultiplierValid = false; break; default: @@ -221,6 +237,7 @@ switch (button) { case GBTurbo: GB_set_turbo_mode(_gb, false, false); + analogClockMultiplierValid = false; break; case GBRewind: @@ -229,6 +246,7 @@ case GBUnderclock: underclockKeyDown = false; + analogClockMultiplierValid = false; break; default: @@ -243,123 +261,97 @@ } } -- (void) joystick:(NSString *)joystick_name button: (unsigned)button changedState: (bool) state +- (void)setRumble:(bool)on { - unsigned player_count = GB_get_player_count(_gb); - - UpdateSystemActivity(UsrActivity); - for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; - if (player_count != 1 && // Single player, accpet inputs from all joypads - !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { - continue; - } - NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name]; - - for (GBButton i = 0; i < GBButtonCount; i++) { - NSNumber *mapped_button = [mapping objectForKey:GBButtonNames[i]]; - if (mapped_button && [mapped_button integerValue] == button) { - switch (i) { - case GBTurbo: - GB_set_turbo_mode(_gb, state, state && self.isRewinding); - break; - - case GBRewind: - self.isRewinding = state; - if (state) { - GB_set_turbo_mode(_gb, false, false); - } - break; - - case GBUnderclock: - underclockKeyDown = state; - break; - - default: - GB_set_key_state_for_player(_gb, (GB_key_t)i, player, state); - break; - } - } - } - } + [lastController setRumbleAmplitude:(double)on]; } -- (void) joystick:(NSString *)joystick_name axis: (unsigned)axis movedTo: (signed) value +- (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis { - unsigned player_count = GB_get_player_count(_gb); + if (![self.window isMainWindow]) return; - UpdateSystemActivity(UsrActivity); - for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; - if (player_count != 1 && // Single player, accpet inputs from all joypads - !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { - continue; - } - - NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBJoypadMappings"][joystick_name]; - NSNumber *x_axis = [mapping objectForKey:@"XAxis"]; - NSNumber *y_axis = [mapping objectForKey:@"YAxis"]; - - if (axis == [x_axis integerValue]) { - if (value > JOYSTICK_HIGH) { - axisActive[0] = true; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, true); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, false); - } - else if (value < -JOYSTICK_HIGH) { - axisActive[0] = true; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, true); - } - else if (axisActive[0] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) { - axisActive[0] = false; - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, false); - } - } - else if (axis == [y_axis integerValue]) { - if (value > JOYSTICK_HIGH) { - axisActive[1] = true; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, true); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, false); - } - else if (value < -JOYSTICK_HIGH) { - axisActive[1] = true; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, true); - } - else if (axisActive[1] && value < JOYSTICK_LOW && value > -JOYSTICK_LOW) { - axisActive[1] = false; - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, false); - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, false); - } - } + NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID]; + if (!mapping) { + mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName]; } -} - -- (void) joystick:(NSString *)joystick_name hat: (unsigned)hat changedState: (int8_t) state -{ - unsigned player_count = GB_get_player_count(_gb); - UpdateSystemActivity(UsrActivity); + if ((axis.usage == JOYAxisUsageR1 && !mapping) || + axis.uniqueID == [mapping[@"AnalogUnderclock"] unsignedLongValue]){ + analogClockMultiplier = MIN(MAX(1 - axis.value + 0.2, 1.0 / 3), 1.0); + analogClockMultiplierValid = true; + } + + else if ((axis.usage == JOYAxisUsageL1 && !mapping) || + axis.uniqueID == [mapping[@"AnalogTurbo"] unsignedLongValue]){ + analogClockMultiplier = MIN(MAX(axis.value * 3 + 0.8, 1.0), 3.0); + analogClockMultiplierValid = true; + } +} + +- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button +{ + if (![self.window isMainWindow]) return; + if (controller != lastController) { + [lastController setRumbleAmplitude:0]; + lastController = controller; + } + + + unsigned player_count = GB_get_player_count(_gb); + + IOPMAssertionID assertionID; + IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID); + for (unsigned player = 0; player < player_count; player++) { - NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"GBDefaultJoypads"] - objectForKey:[NSString stringWithFormat:@"%u", player]]; + NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"] + objectForKey:n2s(player)]; if (player_count != 1 && // Single player, accpet inputs from all joypads !(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads - ![preferred_joypad isEqualToString:joystick_name]) { + ![preferred_joypad isEqualToString:controller.uniqueID]) { continue; } - assert(state + 1 < 9); - /* - N NE E SE S SW W NW */ - GB_set_key_state_for_player(_gb, GB_KEY_UP, player, (bool []){0, 1, 1, 0, 0, 0, 0, 0, 1}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, (bool []){0, 0, 1, 1, 1, 0, 0, 0, 0}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, (bool []){0, 0, 0, 0, 1, 1, 1, 0, 0}[state + 1]); - GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, (bool []){0, 0, 0, 0, 0, 0, 1, 1, 1}[state + 1]); + [controller setPlayerLEDs:1 << player]; + NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID]; + if (!mapping) { + mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName]; + } + + JOYButtonUsage usage = ((JOYButtonUsage)[mapping[n2s(button.uniqueID)] unsignedIntValue]) ?: button.usage; + if (!mapping && usage >= JOYButtonUsageGeneric0) { + usage = (const JOYButtonUsage[]){JOYButtonUsageY, JOYButtonUsageA, JOYButtonUsageB, JOYButtonUsageX}[(usage - JOYButtonUsageGeneric0) & 3]; + } + + switch (usage) { + + case JOYButtonUsageNone: break; + case JOYButtonUsageA: GB_set_key_state_for_player(_gb, GB_KEY_A, player, button.isPressed); break; + case JOYButtonUsageB: GB_set_key_state_for_player(_gb, GB_KEY_B, player, button.isPressed); break; + case JOYButtonUsageC: break; + case JOYButtonUsageStart: + case JOYButtonUsageX: GB_set_key_state_for_player(_gb, GB_KEY_START, player, button.isPressed); break; + case JOYButtonUsageSelect: + case JOYButtonUsageY: GB_set_key_state_for_player(_gb, GB_KEY_SELECT, player, button.isPressed); break; + case JOYButtonUsageR2: + case JOYButtonUsageL2: + case JOYButtonUsageZ: { + self.isRewinding = button.isPressed; + if (button.isPressed) { + GB_set_turbo_mode(_gb, false, false); + } + break; + } + + case JOYButtonUsageL1: GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding); break; + + case JOYButtonUsageR1: underclockKeyDown = button.isPressed; break; + case JOYButtonUsageDPadLeft: GB_set_key_state_for_player(_gb, GB_KEY_LEFT, player, button.isPressed); break; + case JOYButtonUsageDPadRight: GB_set_key_state_for_player(_gb, GB_KEY_RIGHT, player, button.isPressed); break; + case JOYButtonUsageDPadUp: GB_set_key_state_for_player(_gb, GB_KEY_UP, player, button.isPressed); break; + case JOYButtonUsageDPadDown: GB_set_key_state_for_player(_gb, GB_KEY_DOWN, player, button.isPressed); break; + + default: + break; + } } } diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 8278ee1..f9dd9c0 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -1,8 +1,8 @@ - + - + @@ -17,7 +17,7 @@ - + @@ -58,6 +58,7 @@ + @@ -369,22 +370,11 @@ - + - - + @@ -393,7 +383,7 @@ - + @@ -441,28 +431,28 @@ -

@@ -476,11 +466,11 @@ - + - + @@ -489,7 +479,7 @@ - + @@ -507,10 +497,21 @@ - + + - + diff --git a/Cocoa/joypad.m b/Cocoa/joypad.m deleted file mode 100755 index 2ffe56b..0000000 --- a/Cocoa/joypad.m +++ /dev/null @@ -1,748 +0,0 @@ -/* - Joypad support is based on a stripped-down version of SDL's Darwin implementation - of the Joystick API, under the following license: -*/ - -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -#include -#include -#include -#include -#include "GBJoystickListener.h" - -typedef signed SDL_JoystickID; -typedef struct _SDL_Joystick SDL_Joystick; - -typedef struct _SDL_JoystickAxisInfo -{ - int16_t initial_value; /* Initial axis state */ - int16_t value; /* Current axis state */ - int16_t zero; /* Zero point on the axis (-32768 for triggers) */ - bool has_initial_value; /* Whether we've seen a value on the axis yet */ - bool sent_initial_value; /* Whether we've sent the initial axis value */ -} SDL_JoystickAxisInfo; - -struct _SDL_Joystick -{ - SDL_JoystickID instance_id; /* Device instance, monotonically increasing from 0 */ - char *name; /* Joystick name - system dependent */ - - int naxes; /* Number of axis controls on the joystick */ - SDL_JoystickAxisInfo *axes; - - int nbuttons; /* Number of buttons on the joystick */ - uint8_t *buttons; /* Current button states */ - - int nhats; - uint8_t *hats; - - struct joystick_hwdata *hwdata; /* Driver dependent information */ - - int ref_count; /* Reference count for multiple opens */ - - bool is_game_controller; - bool force_recentering; /* SDL_TRUE if this device needs to have its state reset to 0 */ - struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */ -}; - -typedef struct { - uint8_t data[16]; -} SDL_JoystickGUID; - -struct recElement -{ - IOHIDElementRef elementRef; - IOHIDElementCookie cookie; - uint32_t usagePage, usage; /* HID usage */ - SInt32 min; /* reported min value possible */ - SInt32 max; /* reported max value possible */ - - /* runtime variables used for auto-calibration */ - SInt32 minReport; /* min returned value */ - SInt32 maxReport; /* max returned value */ - - struct recElement *pNext; /* next element in list */ -}; -typedef struct recElement recElement; - -struct joystick_hwdata -{ - IOHIDDeviceRef deviceRef; /* HIDManager device handle */ - io_service_t ffservice; /* Interface for force feedback, 0 = no ff */ - - char product[256]; /* name of product */ - uint32_t usage; /* usage page from IOUSBHID Parser.h which defines general usage */ - uint32_t usagePage; /* usage within above page from IOUSBHID Parser.h which defines specific usage */ - - int axes; /* number of axis (calculated, not reported by device) */ - int buttons; /* number of buttons (calculated, not reported by device) */ - int hats; - int elements; /* number of total elements (should be total of above) (calculated, not reported by device) */ - - recElement *firstAxis; - recElement *firstButton; - recElement *firstHat; - - bool removed; - - int instance_id; - SDL_JoystickGUID guid; - - SDL_Joystick joystick; -}; -typedef struct joystick_hwdata recDevice; - -/* The base object of the HID Manager API */ -static IOHIDManagerRef hidman = NULL; - -/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */ -static int s_joystick_instance_id = -1; - -#define SDL_JOYSTICK_AXIS_MAX 32767 - -void SDL_PrivateJoystickAxis(SDL_Joystick * joystick, uint8_t axis, int16_t value) -{ - /* Make sure we're not getting garbage or duplicate events */ - if (axis >= joystick->naxes) { - return; - } - if (!joystick->axes[axis].has_initial_value) { - joystick->axes[axis].initial_value = value; - joystick->axes[axis].value = value; - joystick->axes[axis].zero = value; - joystick->axes[axis].has_initial_value = true; - } - if (value == joystick->axes[axis].value) { - return; - } - if (!joystick->axes[axis].sent_initial_value) { - /* Make sure we don't send motion until there's real activity on this axis */ - const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */ - if (abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) { - return; - } - joystick->axes[axis].sent_initial_value = true; - joystick->axes[axis].value = value; /* Just so we pass the check above */ - SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value); - } - - /* Update internal joystick state */ - joystick->axes[axis].value = value; - - NSResponder *responder = (typeof(responder)) [[NSApp keyWindow] firstResponder]; - while (responder) { - if ([responder respondsToSelector:@selector(joystick:axis:movedTo:)]) { - [responder joystick:@(joystick->name) axis:axis movedTo:value]; - break; - } - responder = (typeof(responder)) [responder nextResponder]; - } -} - -void SDL_PrivateJoystickButton(SDL_Joystick *joystick, uint8_t button, uint8_t state) -{ - - /* Make sure we're not getting garbage or duplicate events */ - if (button >= joystick->nbuttons) { - return; - } - if (state == joystick->buttons[button]) { - return; - } - - /* Update internal joystick state */ - joystick->buttons[button] = state; - - NSResponder *responder = (typeof(responder)) [[NSApp keyWindow] firstResponder]; - while (responder) { - if ([responder respondsToSelector:@selector(joystick:button:changedState:)]) { - [responder joystick:@(joystick->name) button:button changedState:state]; - break; - } - responder = (typeof(responder)) [responder nextResponder]; - } -} - -void SDL_PrivateJoystickHat(SDL_Joystick *joystick, uint8_t hat, uint8_t state) -{ - - /* Make sure we're not getting garbage or duplicate events */ - if (hat >= joystick->nhats) { - return; - } - if (state == joystick->hats[hat]) { - return; - } - - /* Update internal joystick state */ - joystick->hats[hat] = state; - - NSResponder *responder = (typeof(responder)) [[NSApp keyWindow] firstResponder]; - while (responder) { - if ([responder respondsToSelector:@selector(joystick:button:changedState:)]) { - [responder joystick:@(joystick->name) hat:hat changedState:state]; - break; - } - responder = (typeof(responder)) [responder nextResponder]; - } -} - -static void -FreeElementList(recElement *pElement) -{ - while (pElement) { - recElement *pElementNext = pElement->pNext; - free(pElement); - pElement = pElementNext; - } -} - - -static recDevice * -FreeDevice(recDevice *removeDevice) -{ - recDevice *pDeviceNext = NULL; - if (removeDevice) { - if (removeDevice->deviceRef) { - IOHIDDeviceUnscheduleFromRunLoop(removeDevice->deviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - removeDevice->deviceRef = NULL; - } - - /* free element lists */ - FreeElementList(removeDevice->firstAxis); - FreeElementList(removeDevice->firstButton); - FreeElementList(removeDevice->firstHat); - - free(removeDevice); - } - return pDeviceNext; -} - -static SInt32 -GetHIDElementState(recDevice *pDevice, recElement *pElement) -{ - SInt32 value = 0; - - if (pDevice && pElement) { - IOHIDValueRef valueRef; - if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) { - value = (SInt32) IOHIDValueGetIntegerValue(valueRef); - - /* record min and max for auto calibration */ - if (value < pElement->minReport) { - pElement->minReport = value; - } - if (value > pElement->maxReport) { - pElement->maxReport = value; - } - } - } - - return value; -} - -static SInt32 -GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max) -{ - const float deviceScale = max - min; - const float readScale = pElement->maxReport - pElement->minReport; - const SInt32 value = GetHIDElementState(pDevice, pElement); - if (readScale == 0) { - return value; /* no scaling at all */ - } - return ((value - pElement->minReport) * deviceScale / readScale) + min; -} - - -static void -JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender) -{ - recDevice *device = (recDevice *) ctx; - device->removed = true; - device->deviceRef = NULL; // deviceRef was invalidated due to the remove - FreeDevice(device); -} - - -static void AddHIDElement(const void *value, void *parameter); - -/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */ -static void -AddHIDElements(CFArrayRef array, recDevice *pDevice) -{ - const CFRange range = { 0, CFArrayGetCount(array) }; - CFArrayApplyFunction(array, range, AddHIDElement, pDevice); -} - -static bool -ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) { - while (listitem) { - if (listitem->cookie == cookie) { - return true; - } - listitem = listitem->pNext; - } - return false; -} - -/* See if we care about this HID element, and if so, note it in our recDevice. */ -static void -AddHIDElement(const void *value, void *parameter) -{ - recDevice *pDevice = (recDevice *) parameter; - IOHIDElementRef refElement = (IOHIDElementRef) value; - const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0; - - if (refElement && (elementTypeID == IOHIDElementGetTypeID())) { - const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement); - const uint32_t usagePage = IOHIDElementGetUsagePage(refElement); - const uint32_t usage = IOHIDElementGetUsage(refElement); - recElement *element = NULL; - recElement **headElement = NULL; - - /* look at types of interest */ - switch (IOHIDElementGetType(refElement)) { - case kIOHIDElementTypeInput_Misc: - case kIOHIDElementTypeInput_Button: - case kIOHIDElementTypeInput_Axis: { - switch (usagePage) { /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */ - case kHIDPage_GenericDesktop: - switch (usage) { - case kHIDUsage_GD_X: - case kHIDUsage_GD_Y: - case kHIDUsage_GD_Z: - case kHIDUsage_GD_Rx: - case kHIDUsage_GD_Ry: - case kHIDUsage_GD_Rz: - case kHIDUsage_GD_Slider: - case kHIDUsage_GD_Dial: - case kHIDUsage_GD_Wheel: - if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) { - element = (recElement *) calloc(1, sizeof (recElement)); - if (element) { - pDevice->axes++; - headElement = &(pDevice->firstAxis); - } - } - break; - case kHIDUsage_GD_Hatswitch: - if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) { - element = (recElement *) calloc(1, sizeof (recElement)); - if (element) { - pDevice->hats++; - headElement = &(pDevice->firstHat); - } - } - break; - - case kHIDUsage_GD_DPadUp: - case kHIDUsage_GD_DPadDown: - case kHIDUsage_GD_DPadRight: - case kHIDUsage_GD_DPadLeft: - case kHIDUsage_GD_Start: - case kHIDUsage_GD_Select: - case kHIDUsage_GD_SystemMainMenu: - if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) { - element = (recElement *) calloc(1, sizeof (recElement)); - if (element) { - pDevice->buttons++; - headElement = &(pDevice->firstButton); - } - } - break; - } - break; - - case kHIDPage_Simulation: - switch (usage) { - case kHIDUsage_Sim_Rudder: - case kHIDUsage_Sim_Throttle: - case kHIDUsage_Sim_Accelerator: - case kHIDUsage_Sim_Brake: - if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) { - element = (recElement *) calloc(1, sizeof (recElement)); - if (element) { - pDevice->axes++; - headElement = &(pDevice->firstAxis); - } - } - break; - - default: - break; - } - break; - - case kHIDPage_Button: - case kHIDPage_Consumer: /* e.g. 'pause' button on Steelseries MFi gamepads. */ - if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) { - element = (recElement *) calloc(1, sizeof (recElement)); - if (element) { - pDevice->buttons++; - headElement = &(pDevice->firstButton); - } - } - break; - - default: - break; - } - } - break; - - case kIOHIDElementTypeCollection: { - CFArrayRef array = IOHIDElementGetChildren(refElement); - if (array) { - AddHIDElements(array, pDevice); - } - } - break; - - default: - break; - } - - if (element && headElement) { /* add to list */ - recElement *elementPrevious = NULL; - recElement *elementCurrent = *headElement; - while (elementCurrent && usage >= elementCurrent->usage) { - elementPrevious = elementCurrent; - elementCurrent = elementCurrent->pNext; - } - if (elementPrevious) { - elementPrevious->pNext = element; - } else { - *headElement = element; - } - - element->elementRef = refElement; - element->usagePage = usagePage; - element->usage = usage; - element->pNext = elementCurrent; - - element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement); - element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement); - element->cookie = IOHIDElementGetCookie(refElement); - - pDevice->elements++; - } - } -} - -static bool -GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice) -{ - const uint16_t BUS_USB = 0x03; - const uint16_t BUS_BLUETOOTH = 0x05; - int32_t vendor = 0; - int32_t product = 0; - int32_t version = 0; - CFTypeRef refCF = NULL; - CFArrayRef array = NULL; - uint16_t *guid16 = (uint16_t *)pDevice->guid.data; - - /* get usage page and usage */ - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey)); - if (refCF) { - CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage); - } - if (pDevice->usagePage != kHIDPage_GenericDesktop) { - return false; /* Filter device list to non-keyboard/mouse stuff */ - } - - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey)); - if (refCF) { - CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage); - } - - if ((pDevice->usage != kHIDUsage_GD_Joystick && - pDevice->usage != kHIDUsage_GD_GamePad && - pDevice->usage != kHIDUsage_GD_MultiAxisController)) { - return false; /* Filter device list to non-keyboard/mouse stuff */ - } - - pDevice->deviceRef = hidDevice; - - /* get device name */ - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey)); - if (!refCF) { - /* Maybe we can't get "AwesomeJoystick2000", but we can get "Logitech"? */ - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey)); - } - if ((!refCF) || (!CFStringGetCString(refCF, pDevice->product, sizeof (pDevice->product), kCFStringEncodingUTF8))) { - strlcpy(pDevice->product, "Unidentified joystick", sizeof (pDevice->product)); - } - - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey)); - if (refCF) { - CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor); - } - - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey)); - if (refCF) { - CFNumberGetValue(refCF, kCFNumberSInt32Type, &product); - } - - refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVersionNumberKey)); - if (refCF) { - CFNumberGetValue(refCF, kCFNumberSInt32Type, &version); - } - - memset(pDevice->guid.data, 0, sizeof(pDevice->guid.data)); - - if (vendor && product) { - *guid16++ = BUS_USB; - *guid16++ = 0; - *guid16++ = vendor; - *guid16++ = 0; - *guid16++ = product; - *guid16++ = 0; - *guid16++ = version; - *guid16++ = 0; - } else { - *guid16++ = BUS_BLUETOOTH; - *guid16++ = 0; - strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4); - } - - array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone); - if (array) { - AddHIDElements(array, pDevice); - CFRelease(array); - } - - return true; -} - -void -SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) -{ - recDevice *device = joystick->hwdata; - recElement *element; - SInt32 value; - int i; - - if (!device) { - return; - } - - if (device->removed) { /* device was unplugged; ignore it. */ - if (joystick->hwdata) { - joystick->force_recentering = true; - joystick->hwdata = NULL; - } - return; - } - - element = device->firstAxis; - i = 0; - while (element) { - value = GetHIDScaledCalibratedState(device, element, -32768, 32767); - SDL_PrivateJoystickAxis(joystick, i, value); - element = element->pNext; - ++i; - } - - element = device->firstButton; - i = 0; - while (element) { - value = GetHIDElementState(device, element); - if (value > 1) { /* handle pressure-sensitive buttons */ - value = 1; - } - SDL_PrivateJoystickButton(joystick, i, value); - element = element->pNext; - ++i; - } - - element = device->firstHat; - i = 0; - while (element) { - signed range = (element->max - element->min + 1); - value = GetHIDElementState(device, element) - element->min; - if (range == 4) { /* 4 position hatswitch - scale up value */ - value *= 2; - } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */ - value = -1; - } - if ((unsigned)value >= 8) { - value = -1; - } - - SDL_PrivateJoystickHat(joystick, i, value); - - element = element->pNext; - ++i; - } - -} - -static void JoystickInputCallback( - SDL_Joystick * joystick, - IOReturn result, - void * _Nullable sender, - IOHIDReportType type, - uint32_t reportID, - uint8_t * report, - CFIndex reportLength) -{ - SDL_SYS_JoystickUpdate(joystick); -} - -static void -JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) -{ - recDevice *device; - io_service_t ioservice; - - if (res != kIOReturnSuccess) { - return; - } - - device = (recDevice *) calloc(1, sizeof(recDevice)); - - if (!device) { - abort(); - return; - } - - if (!GetDeviceInfo(ioHIDDeviceObject, device)) { - free(device); - return; /* not a device we care about, probably. */ - } - - SDL_Joystick *joystick = &device->joystick; - - joystick->instance_id = device->instance_id; - joystick->hwdata = device; - joystick->name = device->product; - - joystick->naxes = device->axes; - joystick->nbuttons = device->buttons; - joystick->nhats = device->hats; - - if (joystick->naxes > 0) { - joystick->axes = (SDL_JoystickAxisInfo *) calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo)); - } - if (joystick->nbuttons > 0) { - joystick->buttons = (uint8_t *) calloc(joystick->nbuttons, 1); - } - if (joystick->nhats > 0) { - joystick->hats = (uint8_t *) calloc(joystick->nhats, 1); - } - - /* Get notified when this device is disconnected. */ - IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device); - static uint8_t junk[80]; - IOHIDDeviceRegisterInputReportCallback(ioHIDDeviceObject, junk, sizeof(junk), (IOHIDReportCallback) JoystickInputCallback, joystick); - IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - - /* Allocate an instance ID for this device */ - device->instance_id = ++s_joystick_instance_id; - - /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */ - ioservice = IOHIDDeviceGetService(ioHIDDeviceObject); -} - -static bool -ConfigHIDManager(CFArrayRef matchingArray) -{ - CFRunLoopRef runloop = CFRunLoopGetCurrent(); - - if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) { - return false; - } - - IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray); - IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL); - IOHIDManagerScheduleWithRunLoop(hidman, runloop, kCFRunLoopDefaultMode); - - return true; /* good to go. */ -} - - -static CFDictionaryRef -CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay) -{ - CFDictionaryRef retval = NULL; - CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); - CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage); - const void *keys[2] = { (void *) CFSTR(kIOHIDDeviceUsagePageKey), (void *) CFSTR(kIOHIDDeviceUsageKey) }; - const void *vals[2] = { (void *) pageNumRef, (void *) usageNumRef }; - - if (pageNumRef && usageNumRef) { - retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - } - - if (pageNumRef) { - CFRelease(pageNumRef); - } - if (usageNumRef) { - CFRelease(usageNumRef); - } - - if (!retval) { - *okay = 0; - } - - return retval; -} - -static bool -CreateHIDManager(void) -{ - bool retval = false; - int okay = 1; - const void *vals[] = { - (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay), - (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay), - (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay), - }; - const size_t numElements = sizeof(vals) / sizeof(vals[0]); - CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL; - size_t i; - - for (i = 0; i < numElements; i++) { - if (vals[i]) { - CFRelease((CFTypeRef) vals[i]); - } - } - - if (array) { - hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); - if (hidman != NULL) { - retval = ConfigHIDManager(array); - } - CFRelease(array); - } - - return retval; -} - - -void __attribute__((constructor)) SDL_SYS_JoystickInit(void) -{ - if (!CreateHIDManager()) { - fprintf(stderr, "Joystick: Couldn't initialize HID Manager"); - } -} diff --git a/Cocoa/main.m b/Cocoa/main.m index 8a6799b..3eb7a37 100644 --- a/Cocoa/main.m +++ b/Cocoa/main.m @@ -1,5 +1,6 @@ #import -int main(int argc, const char * argv[]) { +int main(int argc, const char * argv[]) +{ return NSApplicationMain(argc, argv); } diff --git a/Core/debugger.c b/Core/debugger.c index df480f3..ab0097f 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1448,7 +1448,7 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg } if (cartridge->has_rumble) { - GB_log(gb, "Cart contains a rumble pak\n"); + GB_log(gb, "Cart contains a Rumble Pak\n"); } if (cartridge->has_rtc) { diff --git a/Core/display.c b/Core/display.c index 5c1935c..aa2d610 100644 --- a/Core/display.c +++ b/Core/display.c @@ -151,6 +151,13 @@ static void display_vblank(GB_gameboy_t *gb) } } + if (gb->rumble_callback) { + if (gb->rumble_on_cycles + gb->rumble_off_cycles) { + gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles)); + gb->rumble_on_cycles = gb->rumble_off_cycles = 0; + } + } + gb->vblank_callback(gb); GB_timing_sync(gb); } diff --git a/Core/gb.h b/Core/gb.h index dbd9e16..a66beed 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -241,7 +241,7 @@ typedef void (*GB_log_callback_t)(GB_gameboy_t *gb, const char *string, GB_log_a typedef char *(*GB_input_callback_t)(GB_gameboy_t *gb); typedef uint32_t (*GB_rgb_encode_callback_t)(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b); typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on, long cycles_since_last_update); -typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, bool rumble_on); +typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, double rumble_amplitude); typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool bit_to_send); typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); @@ -614,6 +614,8 @@ struct GB_gameboy_internal_s { bool vblank_just_occured; // For slow operations involving syscalls; these should only run once per vblank uint8_t cycles_since_run; // How many cycles have passed since the last call to GB_run(), in 8MHz units double clock_multiplier; + uint32_t rumble_on_cycles; + uint32_t rumble_off_cycles; ); }; diff --git a/Core/memory.c b/Core/memory.c index c481ad2..9994343 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -478,9 +478,6 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (gb->cartridge_type->has_rumble) { if (!!(value & 8) != gb->rumble_state) { gb->rumble_state = !gb->rumble_state; - if (gb->rumble_callback) { - gb->rumble_callback(gb, gb->rumble_state); - } } value &= 7; } diff --git a/Core/save_state.c b/Core/save_state.c index 8ef99ae..ce844b9 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -252,10 +252,6 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) errno = 0; - if (gb->cartridge_type->has_rumble && gb->rumble_callback) { - gb->rumble_callback(gb, gb->rumble_state); - } - for (unsigned i = 0; i < 32; i++) { GB_palette_changed(gb, false, i * 2); GB_palette_changed(gb, true, i * 2); @@ -357,10 +353,6 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le memcpy(gb, &save, sizeof(save)); - if (gb->cartridge_type->has_rumble && gb->rumble_callback) { - gb->rumble_callback(gb, gb->rumble_state); - } - for (unsigned i = 0; i < 32; i++) { GB_palette_changed(gb, false, i * 2); GB_palette_changed(gb, true, i * 2); diff --git a/Core/timing.c b/Core/timing.c index 283558c..e592080 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -232,6 +232,14 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_since_input_ir_change += cycles; gb->cycles_since_last_sync += cycles; gb->cycles_since_run += cycles; + + if (gb->rumble_state) { + gb->rumble_on_cycles++; + } + else { + gb->rumble_off_cycles++; + } + if (!gb->stopped) { // TODO: Verify what happens in STOP mode GB_dma_run(gb); GB_hdma_run(gb); diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc new file mode 100644 index 0000000..cdbdce3 --- /dev/null +++ b/JoyKit/ControllerConfiguration.inc @@ -0,0 +1,369 @@ +#define BUTTON(x) @(JOYButtonUsageGeneric0 + (x)) +#define AXIS(x) @(JOYAxisUsageGeneric0 + (x)) +#define AXES2D(x) @(JOYAxes2DUsageGeneric0 + (x)) + +hacksByManufacturer = @{ + @(0x045E): @{ // Microsoft + /* Generally untested, but Microsoft goes by the book when it comes to HID report descriptors, so + it should work out of the box. The hack is only here for automatic mapping */ + + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(2), + @(kHIDUsage_GD_Rx): @(1), + @(kHIDUsage_GD_Ry): @(1), + @(kHIDUsage_GD_Rz): @(3), + }, + + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageA), + BUTTON(2): @(JOYButtonUsageB), + BUTTON(3): @(JOYButtonUsageX), + BUTTON(4): @(JOYButtonUsageY), + BUTTON(5): @(JOYButtonUsageL1), + BUTTON(6): @(JOYButtonUsageR1), + BUTTON(7): @(JOYButtonUsageLStick), + BUTTON(8): @(JOYButtonUsageRStick), + BUTTON(9): @(JOYButtonUsageStart), + BUTTON(10): @(JOYButtonUsageSelect), + BUTTON(11): @(JOYButtonUsageHome), + }, + + JOYAxisUsageMapping: @{ + AXIS(3): @(JOYAxisUsageL1), + AXIS(6): @(JOYAxisUsageR1), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(4): @(JOYAxes2DUsageRightStick), + }, + }, + + @(0x054C): @{ // Sony + /* Generally untested, but should work */ + + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(3), + @(kHIDUsage_GD_Rz): @(1), + }, + + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageY), + BUTTON(2): @(JOYButtonUsageB), + BUTTON(3): @(JOYButtonUsageA), + BUTTON(4): @(JOYButtonUsageX), + BUTTON(5): @(JOYButtonUsageL1), + BUTTON(6): @(JOYButtonUsageR1), + BUTTON(7): @(JOYButtonUsageL2), + BUTTON(8): @(JOYButtonUsageR2), + BUTTON(9): @(JOYButtonUsageSelect), + BUTTON(10): @(JOYButtonUsageStart), + BUTTON(11): @(JOYButtonUsageLStick), + BUTTON(12): @(JOYButtonUsageRStick), + BUTTON(13): @(JOYButtonUsageHome), + BUTTON(14): @(JOYButtonUsageMisc), + }, + + JOYAxisUsageMapping: @{ + AXIS(4): @(JOYAxisUsageL1), + AXIS(5): @(JOYAxisUsageR1), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(4): @(JOYAxes2DUsageRightStick), + }, + } +}; + +hacksByName = @{ + @"WUP-028": @{ // Nintendo GameCube Controller Adapter + JOYReportIDFilters: @[@[@1], @[@2], @[@3], @[@4]], + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageA), + BUTTON(2): @(JOYButtonUsageB), + BUTTON(3): @(JOYButtonUsageX), + BUTTON(4): @(JOYButtonUsageY), + BUTTON(5): @(JOYButtonUsageStart), + BUTTON(6): @(JOYButtonUsageZ), + BUTTON(7): @(JOYButtonUsageR1), + BUTTON(8): @(JOYButtonUsageL1), + }, + + JOYAxisUsageMapping: @{ + AXIS(3): @(JOYAxisUsageL1), + AXIS(6): @(JOYAxisUsageR1), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(4): @(JOYAxes2DUsageRightStick), + }, + + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(2), + @(kHIDUsage_GD_Rx): @(1), + @(kHIDUsage_GD_Ry): @(1), + @(kHIDUsage_GD_Rz): @(3), + }, + + JOYRumbleUsage: @1, + JOYRumbleUsagePage: @0xFF00, + + JOYConnectedUsage: @2, + JOYConnectedUsagePage: @0xFF00, + + JOYSubElementStructs: @{ + + // Rumble + @(1364): @[ + @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(2), @"size":@1, @"offset":@1, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(3), @"size":@1, @"offset":@2, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(4), @"size":@1, @"offset":@3, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + ], + + @(11): @[ + + // Player 1 + + @{@"reportID": @(1), @"size":@1, @"offset":@4, @"usagePage":@(0xFF00), @"usage":@2, @"min": @0, @"max": @1}, + + @{@"reportID": @(1), @"size":@1, @"offset":@8, @"usagePage":@(kHIDPage_Button), @"usage":@1}, + @{@"reportID": @(1), @"size":@1, @"offset":@9, @"usagePage":@(kHIDPage_Button), @"usage":@2}, + @{@"reportID": @(1), @"size":@1, @"offset":@10, @"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(1), @"size":@1, @"offset":@11, @"usagePage":@(kHIDPage_Button), @"usage":@4}, + + @{@"reportID": @(1), @"size":@1, @"offset":@12, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + @{@"reportID": @(1), @"size":@1, @"offset":@13, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(1), @"size":@1, @"offset":@14, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, + @{@"reportID": @(1), @"size":@1, @"offset":@15, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, + + + @{@"reportID": @(1), @"size":@1, @"offset":@16, @"usagePage":@(kHIDPage_Button), @"usage":@5}, + @{@"reportID": @(1), @"size":@1, @"offset":@17, @"usagePage":@(kHIDPage_Button), @"usage":@6}, + @{@"reportID": @(1), @"size":@1, @"offset":@18, @"usagePage":@(kHIDPage_Button), @"usage":@7}, + @{@"reportID": @(1), @"size":@1, @"offset":@19, @"usagePage":@(kHIDPage_Button), @"usage":@8}, + + @{@"reportID": @(1), @"size":@8, @"offset":@24, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_X), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@32, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Y), @"min": @255, @"max": @0}, + + @{@"reportID": @(1), @"size":@8, @"offset":@40, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@48, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @255, @"max": @0}, + + @{@"reportID": @(1), @"size":@8, @"offset":@56, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Z), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@64, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rz), @"min": @0, @"max": @255}, + + // Player 2 + + @{@"reportID": @(2), @"size":@1, @"offset":@(4 + 72), @"usagePage":@(0xFF00), @"usage":@2, @"min": @0, @"max": @1}, + + + @{@"reportID": @(2), @"size":@1, @"offset":@(8 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@1}, + @{@"reportID": @(2), @"size":@1, @"offset":@(9 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@2}, + @{@"reportID": @(2), @"size":@1, @"offset":@(10 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(2), @"size":@1, @"offset":@(11 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@4}, + + @{@"reportID": @(2), @"size":@1, @"offset":@(12 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + @{@"reportID": @(2), @"size":@1, @"offset":@(13 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(2), @"size":@1, @"offset":@(14 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, + @{@"reportID": @(2), @"size":@1, @"offset":@(15 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, + + + @{@"reportID": @(2), @"size":@1, @"offset":@(16 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@5}, + @{@"reportID": @(2), @"size":@1, @"offset":@(17 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@6}, + @{@"reportID": @(2), @"size":@1, @"offset":@(18 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@7}, + @{@"reportID": @(2), @"size":@1, @"offset":@(19 + 72), @"usagePage":@(kHIDPage_Button), @"usage":@8}, + + @{@"reportID": @(2), @"size":@8, @"offset":@(24 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_X), @"min": @0, @"max": @255}, + @{@"reportID": @(2), @"size":@8, @"offset":@(32 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Y), @"min": @255, @"max": @0}, + + @{@"reportID": @(2), @"size":@8, @"offset":@(40 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @255}, + @{@"reportID": @(2), @"size":@8, @"offset":@(48 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @255, @"max": @0}, + + @{@"reportID": @(2), @"size":@8, @"offset":@(56 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Z), @"min": @0, @"max": @255}, + @{@"reportID": @(2), @"size":@8, @"offset":@(64 + 72), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rz), @"min": @0, @"max": @255}, + + // Player 3 + + @{@"reportID": @(3), @"size":@1, @"offset":@(4 + 144), @"usagePage":@(0xFF00), @"usage":@2, @"min": @0, @"max": @1}, + + @{@"reportID": @(3), @"size":@1, @"offset":@(8 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@1}, + @{@"reportID": @(3), @"size":@1, @"offset":@(9 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@2}, + @{@"reportID": @(3), @"size":@1, @"offset":@(10 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(3), @"size":@1, @"offset":@(11 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@4}, + + @{@"reportID": @(3), @"size":@1, @"offset":@(12 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + @{@"reportID": @(3), @"size":@1, @"offset":@(13 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(3), @"size":@1, @"offset":@(14 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, + @{@"reportID": @(3), @"size":@1, @"offset":@(15 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, + + + @{@"reportID": @(3), @"size":@1, @"offset":@(16 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@5}, + @{@"reportID": @(3), @"size":@1, @"offset":@(17 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@6}, + @{@"reportID": @(3), @"size":@1, @"offset":@(18 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@7}, + @{@"reportID": @(3), @"size":@1, @"offset":@(19 + 144), @"usagePage":@(kHIDPage_Button), @"usage":@8}, + + @{@"reportID": @(3), @"size":@8, @"offset":@(24 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_X), @"min": @0, @"max": @255}, + @{@"reportID": @(3), @"size":@8, @"offset":@(32 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Y), @"min": @255, @"max": @0}, + + @{@"reportID": @(3), @"size":@8, @"offset":@(40 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @255}, + @{@"reportID": @(3), @"size":@8, @"offset":@(48 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @255, @"max": @0}, + + @{@"reportID": @(3), @"size":@8, @"offset":@(56 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Z), @"min": @0, @"max": @255}, + @{@"reportID": @(3), @"size":@8, @"offset":@(64 + 144), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rz), @"min": @0, @"max": @255}, + + // Player 4 + + @{@"reportID": @(4), @"size":@1, @"offset":@(4 + 216), @"usagePage":@(0xFF00), @"usage":@2, @"min": @0, @"max": @1}, + + @{@"reportID": @(4), @"size":@1, @"offset":@(8 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@1}, + @{@"reportID": @(4), @"size":@1, @"offset":@(9 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@2}, + @{@"reportID": @(4), @"size":@1, @"offset":@(10 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(4), @"size":@1, @"offset":@(11 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@4}, + + @{@"reportID": @(4), @"size":@1, @"offset":@(12 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + @{@"reportID": @(4), @"size":@1, @"offset":@(13 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(4), @"size":@1, @"offset":@(14 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, + @{@"reportID": @(4), @"size":@1, @"offset":@(15 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, + + + @{@"reportID": @(4), @"size":@1, @"offset":@(16 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@5}, + @{@"reportID": @(4), @"size":@1, @"offset":@(17 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@6}, + @{@"reportID": @(4), @"size":@1, @"offset":@(18 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@7}, + @{@"reportID": @(4), @"size":@1, @"offset":@(19 + 216), @"usagePage":@(kHIDPage_Button), @"usage":@8}, + + @{@"reportID": @(4), @"size":@8, @"offset":@(24 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_X), @"min": @0, @"max": @255}, + @{@"reportID": @(4), @"size":@8, @"offset":@(32 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Y), @"min": @255, @"max": @0}, + + @{@"reportID": @(4), @"size":@8, @"offset":@(40 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @255}, + @{@"reportID": @(4), @"size":@8, @"offset":@(48 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @255, @"max": @0}, + + @{@"reportID": @(4), @"size":@8, @"offset":@(56 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Z), @"min": @0, @"max": @255}, + @{@"reportID": @(4), @"size":@8, @"offset":@(64 + 216), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rz), @"min": @0, @"max": @255}, + + + ]}, + }, + + @"GameCube Controller Adapter": @{ // GameCube Controller PC Adapter + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(3), + @(kHIDUsage_GD_Rz): @(1), + }, + JOYReportIDFilters: @[@[@1], @[@2], @[@3], @[@4]], + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageX), + BUTTON(2): @(JOYButtonUsageA), + BUTTON(3): @(JOYButtonUsageB), + BUTTON(4): @(JOYButtonUsageY), + BUTTON(5): @(JOYButtonUsageL1), + BUTTON(6): @(JOYButtonUsageR1), + BUTTON(8): @(JOYButtonUsageZ), + BUTTON(10): @(JOYButtonUsageStart), + BUTTON(13): @(JOYButtonUsageDPadUp), + BUTTON(14): @(JOYButtonUsageDPadRight), + BUTTON(15): @(JOYButtonUsageDPadDown), + BUTTON(16): @(JOYButtonUsageDPadLeft), + }, + + JOYAxisUsageMapping: @{ + AXIS(4): @(JOYAxisUsageL1), + AXIS(5): @(JOYAxisUsageR1), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(3): @(JOYAxes2DUsageRightStick), + }, + + JOYRumbleUsage: @1, + JOYRumbleUsagePage: @0xFF00, + JOYRumbleMin: @0, + JOYRumbleMax: @255, + JOYSwapZRz: @YES, + }, + + @"Twin USB Joystick": @{ // DualShock PC Adapter + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(2), + @(kHIDUsage_GD_Rz): @(1), + }, + JOYReportIDFilters: @[@[@1], @[@2]], + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageX), + BUTTON(2): @(JOYButtonUsageA), + BUTTON(3): @(JOYButtonUsageB), + BUTTON(4): @(JOYButtonUsageY), + BUTTON(5): @(JOYButtonUsageL2), + BUTTON(6): @(JOYButtonUsageR2), + BUTTON(7): @(JOYButtonUsageL1), + BUTTON(8): @(JOYButtonUsageR1), + BUTTON(9): @(JOYButtonUsageSelect), + BUTTON(10): @(JOYButtonUsageStart), + BUTTON(11): @(JOYButtonUsageLStick), + BUTTON(12): @(JOYButtonUsageRStick), + BUTTON(13): @(JOYButtonUsageDPadUp), + BUTTON(14): @(JOYButtonUsageDPadRight), + BUTTON(15): @(JOYButtonUsageDPadDown), + BUTTON(16): @(JOYButtonUsageDPadLeft), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(6): @(JOYAxes2DUsageRightStick), + }, + + JOYSwapZRz: @YES, + }, + + @"Pro Controller": @{ // Switch Pro Controller + JOYIsSwitch: @YES, + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(0), + @(kHIDUsage_GD_Rx): @(1), + @(kHIDUsage_GD_Ry): @(1), + @(kHIDUsage_GD_Rz): @(1), + }, + + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageB), + BUTTON(2): @(JOYButtonUsageA), + BUTTON(3): @(JOYButtonUsageY), + BUTTON(4): @(JOYButtonUsageX), + BUTTON(5): @(JOYButtonUsageL1), + BUTTON(6): @(JOYButtonUsageR1), + BUTTON(7): @(JOYButtonUsageL2), + BUTTON(8): @(JOYButtonUsageR2), + BUTTON(9): @(JOYButtonUsageSelect), + BUTTON(10): @(JOYButtonUsageStart), + BUTTON(11): @(JOYButtonUsageLStick), + BUTTON(12): @(JOYButtonUsageRStick), + BUTTON(13): @(JOYButtonUsageHome), + BUTTON(14): @(JOYButtonUsageMisc), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(4): @(JOYAxes2DUsageRightStick), + }, + }, +}; diff --git a/JoyKit/JOYAxes2D.h b/JoyKit/JOYAxes2D.h new file mode 100644 index 0000000..b6f6d15 --- /dev/null +++ b/JoyKit/JOYAxes2D.h @@ -0,0 +1,24 @@ +#import + +typedef enum { + JOYAxes2DUsageNone, + JOYAxes2DUsageLeftStick, + JOYAxes2DUsageRightStick, + JOYAxes2DUsageMiddleStick, + JOYAxes2DUsagePointer, + JOYAxes2DUsageNonGenericMax, + + JOYAxes2DUsageGeneric0 = 0x10000, +} JOYAxes2DUsage; + +@interface JOYAxes2D : NSObject +- (NSString *)usageString; ++ (NSString *)usageToString: (JOYAxes2DUsage) usage; +- (uint64_t)uniqueID; +- (double)distance; +- (double)angle; +- (NSPoint)value; +@property JOYAxes2DUsage usage; +@end + + diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m new file mode 100644 index 0000000..28ce16c --- /dev/null +++ b/JoyKit/JOYAxes2D.m @@ -0,0 +1,168 @@ +#import "JOYAxes2D.h" +#import "JOYElement.h" + +@implementation JOYAxes2D +{ + JOYElement *_element1, *_element2; + double _state1, _state2; + int32_t initialX, initialY; + int32_t minX, minY; + int32_t maxX, maxY; + +} + ++ (NSString *)usageToString: (JOYAxes2DUsage) usage +{ + if (usage < JOYAxes2DUsageNonGenericMax) { + return (NSString *[]) { + @"None", + @"Left Stick", + @"Right Stick", + @"Middle Stick", + @"Pointer", + }[usage]; + } + if (usage >= JOYAxes2DUsageGeneric0) { + return [NSString stringWithFormat:@"Generic 2D Analog Control %d", usage - JOYAxes2DUsageGeneric0]; + } + + return [NSString stringWithFormat:@"Unknown Usage 2D Axes %d", usage]; +} + +- (NSString *)usageString +{ + return [self.class usageToString:_usage]; +} + +- (uint64_t)uniqueID +{ + return _element1.uniqueID; +} + +- (NSString *)description +{ + return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %.2f%%, %.2f degrees>", self.className, self, self.usageString, self.uniqueID, self.distance * 100, self.angle]; +} + +- (instancetype)initWithFirstElement:(JOYElement *)element1 secondElement:(JOYElement *)element2 +{ + self = [super init]; + if (!self) return self; + + _element1 = element1; + _element2 = element2; + + + if (element1.usagePage == kHIDPage_GenericDesktop) { + uint16_t usage = element1.usage; + _usage = JOYAxes2DUsageGeneric0 + usage - kHIDUsage_GD_X + 1; + } + initialX = [_element1 value]; + initialY = [_element2 value]; + minX = element1.max; + minY = element2.max; + maxX = element1.min; + maxY = element2.min; + + return self; +} + +- (NSPoint)value +{ + return NSMakePoint(_state1, _state2); +} + +-(int32_t) effectiveMinX +{ + int32_t rawMin = _element1.min; + int32_t rawMax = _element1.max; + if (initialX == 0) return rawMin; + if (minX <= (rawMin * 2 + initialX) / 3 && maxX >= (rawMax * 2 + initialX) / 3 ) return minX; + if ((initialX - rawMin) < (rawMax - initialX)) return rawMin; + return initialX - (rawMax - initialX); +} + +-(int32_t) effectiveMinY +{ + int32_t rawMin = _element2.min; + int32_t rawMax = _element2.max; + if (initialY == 0) return rawMin; + if (minX <= (rawMin * 2 + initialY) / 3 && maxY >= (rawMax * 2 + initialY) / 3 ) return minY; + if ((initialY - rawMin) < (rawMax - initialY)) return rawMin; + return initialY - (rawMax - initialY); +} + +-(int32_t) effectiveMaxX +{ + int32_t rawMin = _element1.min; + int32_t rawMax = _element1.max; + if (initialX == 0) return rawMax; + if (minX <= (rawMin * 2 + initialX) / 3 && maxX >= (rawMax * 2 + initialX) / 3 ) return maxX; + if ((initialX - rawMin) > (rawMax - initialX)) return rawMax; + return initialX + (initialX - rawMin); +} + +-(int32_t) effectiveMaxY +{ + int32_t rawMin = _element2.min; + int32_t rawMax = _element2.max; + if (initialY == 0) return rawMax; + if (minX <= (rawMin * 2 + initialY) / 3 && maxY >= (rawMax * 2 + initialY) / 3 ) return maxY; + if ((initialY - rawMin) > (rawMax - initialY)) return rawMax; + return initialY + (initialY - rawMin); +} + +- (bool)updateState +{ + int32_t x = [_element1 value]; + int32_t y = [_element2 value]; + if (x == 0 && y == 0) return false; + + if (initialX == 0 && initialY == 0) { + initialX = x; + initialY = y; + } + + double old1 = _state1, old2 = _state2; + { + double min = [self effectiveMinX]; + double max = [self effectiveMaxX]; + if (min == max) return false; + int32_t value = x; + + if (initialX != 0) { + minX = MIN(value, minX); + maxX = MAX(value, maxX); + } + + _state1 = (value - min) / (max - min) * 2 - 1; + } + + { + double min = [self effectiveMinY]; + double max = [self effectiveMaxY]; + if (min == max) return false; + int32_t value = y; + + if (initialY != 0) { + minY = MIN(value, minY); + maxY = MAX(value, maxY); + } + + _state2 = (value - min) / (max - min) * 2 - 1; + } + + return old1 != _state1 || old2 != _state2; +} + +- (double)distance +{ + return MIN(sqrt(_state1 * _state1 + _state2 * _state2), 1.0); +} + +- (double)angle { + double temp = atan2(_state2, _state1) * 180 / M_PI; + if (temp >= 0) return temp; + return temp + 360; +} +@end diff --git a/JoyKit/JOYAxis.h b/JoyKit/JOYAxis.h new file mode 100644 index 0000000..5a4c166 --- /dev/null +++ b/JoyKit/JOYAxis.h @@ -0,0 +1,29 @@ +#import + +typedef enum { + JOYAxisUsageNone, + JOYAxisUsageL1, + JOYAxisUsageL2, + JOYAxisUsageL3, + JOYAxisUsageR1, + JOYAxisUsageR2, + JOYAxisUsageR3, + JOYAxisUsageWheel, + JOYAxisUsageRudder, + JOYAxisUsageThrottle, + JOYAxisUsageAccelerator, + JOYAxisUsageBrake, + JOYAxisUsageNonGenericMax, + + JOYAxisUsageGeneric0 = 0x10000, +} JOYAxisUsage; + +@interface JOYAxis : NSObject +- (NSString *)usageString; ++ (NSString *)usageToString: (JOYAxisUsage) usage; +- (uint64_t)uniqueID; +- (double)value; +@property JOYAxisUsage usage; +@end + + diff --git a/JoyKit/JOYAxis.m b/JoyKit/JOYAxis.m new file mode 100644 index 0000000..169eaee --- /dev/null +++ b/JoyKit/JOYAxis.m @@ -0,0 +1,90 @@ +#import "JOYAxis.h" +#import "JOYElement.h" + +@implementation JOYAxis +{ + JOYElement *_element; + double _state; + double _min; +} + ++ (NSString *)usageToString: (JOYAxisUsage) usage +{ + if (usage < JOYAxisUsageNonGenericMax) { + return (NSString *[]) { + @"None", + @"Analog L1", + @"Analog L2", + @"Analog L3", + @"Analog R1", + @"Analog R2", + @"Analog R3", + @"Wheel", + @"Rudder", + @"Throttle", + @"Accelerator", + @"Brake", + }[usage]; + } + if (usage >= JOYAxisUsageGeneric0) { + return [NSString stringWithFormat:@"Generic Analog Control %d", usage - JOYAxisUsageGeneric0]; + } + + return [NSString stringWithFormat:@"Unknown Usage Axis %d", usage]; +} + +- (NSString *)usageString +{ + return [self.class usageToString:_usage]; +} + +- (uint64_t)uniqueID +{ + return _element.uniqueID; +} + +- (NSString *)description +{ + return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %f%%>", self.className, self, self.usageString, self.uniqueID, _state * 100]; +} + +- (instancetype)initWithElement:(JOYElement *)element +{ + self = [super init]; + if (!self) return self; + + _element = element; + + + if (element.usagePage == kHIDPage_GenericDesktop) { + uint16_t usage = element.usage; + _usage = JOYAxisUsageGeneric0 + usage - kHIDUsage_GD_X + 1; + } + + _min = 1.0; + + return self; +} + +- (double) value +{ + return _state; +} + +- (bool)updateState +{ + double min = _element.min; + double max = _element.max; + if (min == max) return false; + double old = _state; + double unnormalized = ([_element value] - min) / (max - min); + if (unnormalized < _min) { + _min = unnormalized; + } + if (_min != 1) { + _state = (unnormalized - _min) / (1 - _min); + } + return old != _state; +} + +@end diff --git a/JoyKit/JOYButton.h b/JoyKit/JOYButton.h new file mode 100644 index 0000000..f732c8e --- /dev/null +++ b/JoyKit/JOYButton.h @@ -0,0 +1,42 @@ +#import + + + +typedef enum { + JOYButtonUsageNone, + JOYButtonUsageA, + JOYButtonUsageB, + JOYButtonUsageC, + JOYButtonUsageX, + JOYButtonUsageY, + JOYButtonUsageZ, + JOYButtonUsageStart, + JOYButtonUsageSelect, + JOYButtonUsageHome, + JOYButtonUsageMisc, + JOYButtonUsageLStick, + JOYButtonUsageRStick, + JOYButtonUsageL1, + JOYButtonUsageL2, + JOYButtonUsageL3, + JOYButtonUsageR1, + JOYButtonUsageR2, + JOYButtonUsageR3, + JOYButtonUsageDPadLeft, + JOYButtonUsageDPadRight, + JOYButtonUsageDPadUp, + JOYButtonUsageDPadDown, + JOYButtonUsageNonGenericMax, + + JOYButtonUsageGeneric0 = 0x10000, +} JOYButtonUsage; + +@interface JOYButton : NSObject +- (NSString *)usageString; ++ (NSString *)usageToString: (JOYButtonUsage) usage; +- (uint64_t)uniqueID; +- (bool) isPressed; +@property JOYButtonUsage usage; +@end + + diff --git a/JoyKit/JOYButton.m b/JoyKit/JOYButton.m new file mode 100644 index 0000000..3e6026d --- /dev/null +++ b/JoyKit/JOYButton.m @@ -0,0 +1,102 @@ +#import "JOYButton.h" +#import "JOYElement.h" + +@implementation JOYButton +{ + JOYElement *_element; + bool _state; +} + ++ (NSString *)usageToString: (JOYButtonUsage) usage +{ + if (usage < JOYButtonUsageNonGenericMax) { + return (NSString *[]) { + @"None", + @"A", + @"B", + @"C", + @"X", + @"Y", + @"Z", + @"Start", + @"Select", + @"Home", + @"Misc", + @"Left Stick", + @"Right Stick", + @"L1", + @"L2", + @"L3", + @"R1", + @"R2", + @"R3", + @"D-Pad Left", + @"D-Pad Right", + @"D-Pad Up", + @"D-Pad Down", + }[usage]; + } + if (usage >= JOYButtonUsageGeneric0) { + return [NSString stringWithFormat:@"Generic Button %d", usage - JOYButtonUsageGeneric0]; + } + + return [NSString stringWithFormat:@"Unknown Usage Button %d", usage]; +} + +- (NSString *)usageString +{ + return [self.class usageToString:_usage]; +} + +- (uint64_t)uniqueID +{ + return _element.uniqueID; +} + +- (NSString *)description +{ + return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %s>", self.className, self, self.usageString, self.uniqueID, _state? "Presssed" : "Released"]; +} + +- (instancetype)initWithElement:(JOYElement *)element +{ + self = [super init]; + if (!self) return self; + + _element = element; + + if (element.usagePage == kHIDPage_Button) { + uint16_t usage = element.usage; + _usage = JOYButtonUsageGeneric0 + usage; + } + else if (element.usagePage == kHIDPage_GenericDesktop) { + switch (element.usage) { + case kHIDUsage_GD_DPadUp: _usage = JOYButtonUsageDPadUp; break; + case kHIDUsage_GD_DPadDown: _usage = JOYButtonUsageDPadDown; break; + case kHIDUsage_GD_DPadRight: _usage = JOYButtonUsageDPadRight; break; + case kHIDUsage_GD_DPadLeft: _usage = JOYButtonUsageDPadLeft; break; + case kHIDUsage_GD_Start: _usage = JOYButtonUsageStart; break; + case kHIDUsage_GD_Select: _usage = JOYButtonUsageSelect; break; + case kHIDUsage_GD_SystemMainMenu: _usage = JOYButtonUsageHome; break; + } + } + + return self; +} + +- (bool) isPressed +{ + return _state; +} + +- (bool)updateState +{ + bool state = [_element value]; + if (_state != state) { + _state = state; + return true; + } + return false; +} + +@end diff --git a/JoyKit/JOYController.h b/JoyKit/JOYController.h new file mode 100644 index 0000000..9ed7cf7 --- /dev/null +++ b/JoyKit/JOYController.h @@ -0,0 +1,41 @@ +#import +#import "JOYButton.h" +#import "JOYAxis.h" +#import "JOYAxes2D.h" +#import "JOYHat.h" + +static NSString const *JOYAxesEmulateButtonsKey = @"JOYAxesEmulateButtons"; +static NSString const *JOYAxes2DEmulateButtonsKey = @"JOYAxes2DEmulateButtons"; +static NSString const *JOYHatsEmulateButtonsKey = @"JOYHatsEmulateButtons"; + +@class JOYController; + +@protocol JOYListener + +@optional +-(void) controllerConnected:(JOYController *)controller; +-(void) controllerDisconnected:(JOYController *)controller; +-(void) controller:(JOYController *)controller buttonChangedState:(JOYButton *)button; +-(void) controller:(JOYController *)controller movedAxis:(JOYAxis *)axis; +-(void) controller:(JOYController *)controller movedAxes2D:(JOYAxes2D *)axes; +-(void) controller:(JOYController *)controller movedHat:(JOYHat *)hat; + +@end + +@interface JOYController : NSObject ++ (void)startOnRunLoop:(NSRunLoop *)runloop withOptions: (NSDictionary *)options; ++ (NSArray *) allControllers; ++ (void) registerListener:(id)listener; ++ (void) unregisterListener:(id)listener; +- (NSString *)deviceName; +- (NSString *)uniqueID; +- (NSArray *) buttons; +- (NSArray *) axes; +- (NSArray *) axes2D; +- (NSArray *) hats; +- (void)setRumbleAmplitude:(double)amp; +- (void)setPlayerLEDs:(uint8_t)mask; +@property (readonly, getter=isConnected) bool connected; +@end + + diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m new file mode 100644 index 0000000..d5e1acb --- /dev/null +++ b/JoyKit/JOYController.m @@ -0,0 +1,760 @@ +#import "JOYController.h" +#import "JOYMultiplayerController.h" +#import "JOYElement.h" +#import "JOYSubElement.h" +#import "JOYEmulatedButton.h" +#include + +static NSString const *JOYAxisGroups = @"JOYAxisGroups"; +static NSString const *JOYReportIDFilters = @"JOYReportIDFilters"; +static NSString const *JOYButtonUsageMapping = @"JOYButtonUsageMapping"; +static NSString const *JOYAxisUsageMapping = @"JOYAxisUsageMapping"; +static NSString const *JOYAxes2DUsageMapping = @"JOYAxes2DUsageMapping"; +static NSString const *JOYSubElementStructs = @"JOYSubElementStructs"; +static NSString const *JOYIsSwitch = @"JOYIsSwitch"; +static NSString const *JOYRumbleUsage = @"JOYRumbleUsage"; +static NSString const *JOYRumbleUsagePage = @"JOYRumbleUsagePage"; +static NSString const *JOYConnectedUsage = @"JOYConnectedUsage"; +static NSString const *JOYConnectedUsagePage = @"JOYConnectedUsagePage"; +static NSString const *JOYRumbleMin = @"JOYRumbleMin"; +static NSString const *JOYRumbleMax = @"JOYRumbleMax"; +static NSString const *JOYSwapZRz = @"JOYSwapZRz"; + + +static NSMutableDictionary *controllers; // Physical controllers +static NSMutableArray *exposedControllers; // Logical controllers + +static NSDictionary *hacksByName = nil; +static NSDictionary *hacksByManufacturer = nil; + +static NSMutableSet> *listeners = nil; + +static bool axesEmulateButtons = false; +static bool axes2DEmulateButtons = false; +static bool hatsEmulateButtons = false; + +@interface JOYController () ++ (void)controllerAdded:(IOHIDDeviceRef) device; ++ (void)controllerRemoved:(IOHIDDeviceRef) device; +- (void)elementChanged:(IOHIDElementRef) element; +@end + +@interface JOYButton () +- (instancetype)initWithElement:(JOYElement *)element; +- (bool)updateState; +@end + +@interface JOYAxis () +- (instancetype)initWithElement:(JOYElement *)element; +- (bool)updateState; +@end + +@interface JOYHat () +- (instancetype)initWithElement:(JOYElement *)element; +- (bool)updateState; +@end + +@interface JOYAxes2D () +- (instancetype)initWithFirstElement:(JOYElement *)element1 secondElement:(JOYElement *)element2; +- (bool)updateState; +@end + +static NSDictionary *CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage) +{ + return @{ + @kIOHIDDeviceUsagePageKey: @(page), + @kIOHIDDeviceUsageKey: @(usage), + }; +} + +static void HIDDeviceAdded(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) +{ + [JOYController controllerAdded:device]; +} + +static void HIDDeviceRemoved(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) +{ + [JOYController controllerRemoved:device]; +} + +static void HIDInput(void *context, IOReturn result, void *sender, IOHIDValueRef value) +{ + [(__bridge JOYController *)context elementChanged:IOHIDValueGetElement(value)]; +} + + +typedef struct __attribute__((packed)) { + uint8_t reportID; + uint8_t sequence; + uint8_t rumbleData[8]; + uint8_t command; + uint8_t commandData[26]; +} JOYSwitchPacket; + +@implementation JOYController +{ + IOHIDDeviceRef _device; + NSMutableDictionary *_buttons; + NSMutableDictionary *_axes; + NSMutableDictionary *_axes2D; + NSMutableDictionary *_hats; + NSMutableDictionary *> *_multiElements; + + // Button emulation + NSMutableDictionary *_axisEmulatedButtons; + NSMutableDictionary *> *_axes2DEmulatedButtons; + NSMutableDictionary *> *_hatEmulatedButtons; + + JOYElement *_rumbleElement; + JOYElement *_connectedElement; + NSMutableDictionary *_iokitToJOY; + NSString *_serialSuffix; + bool _isSwitch; // Does thie controller use the Switch protocol? + JOYSwitchPacket _lastSwitchPacket; + NSCondition *_rumblePWMThreadLock; + volatile double _rumblePWMRatio; + bool _physicallyConnected; + bool _logicallyConnected; +} + +- (instancetype)initWithDevice:(IOHIDDeviceRef) device +{ + return [self initWithDevice:device reportIDFilter:nil serialSuffix:nil]; +} + +- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix +{ + self = [super init]; + if (!self) return self; + + _physicallyConnected = true; + _logicallyConnected = true; + _device = device; + _serialSuffix = suffix; + + IOHIDDeviceRegisterInputValueCallback(device, HIDInput, (void *)self); + IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + + NSArray *array = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); + _buttons = [NSMutableDictionary dictionary]; + _axes = [NSMutableDictionary dictionary]; + _axes2D = [NSMutableDictionary dictionary]; + _hats = [NSMutableDictionary dictionary]; + _axisEmulatedButtons = [NSMutableDictionary dictionary]; + _axes2DEmulatedButtons = [NSMutableDictionary dictionary]; + _hatEmulatedButtons = [NSMutableDictionary dictionary]; + _multiElements = [NSMutableDictionary dictionary]; + _iokitToJOY = [NSMutableDictionary dictionary]; + _rumblePWMThreadLock = [[NSCondition alloc] init]; + + + //NSMutableArray *axes3d = [NSMutableArray array]; + + NSDictionary *axisGroups = @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(2), + @(kHIDUsage_GD_Rz): @(1), + }; + NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); + NSDictionary *hacks = hacksByName[name]; + if (!hacks) { + hacks = hacksByManufacturer[(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey))]; + } + axisGroups = hacks[JOYAxisGroups] ?: axisGroups; + _isSwitch = [hacks[JOYIsSwitch] boolValue]; + uint16_t rumbleUsagePage = (uint16_t)[hacks[JOYRumbleUsagePage] unsignedIntValue]; + uint16_t rumbleUsage = (uint16_t)[hacks[JOYRumbleUsage] unsignedIntValue]; + uint16_t connectedUsagePage = (uint16_t)[hacks[JOYConnectedUsagePage] unsignedIntValue]; + uint16_t connectedUsage = (uint16_t)[hacks[JOYConnectedUsage] unsignedIntValue]; + + JOYElement *previousAxisElement = nil; + id previous = nil; + for (id _element in array) { + if (_element == previous) continue; // Some elements are reported twice for some reason + previous = _element; + NSArray *elements = nil; + JOYElement *element = [[JOYElement alloc] initWithElement:(__bridge IOHIDElementRef)_element]; + + NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.uniqueID)]; + + bool isOutput = false; + if (subElementDefs && element.uniqueID != element.parentID) { + elements = [NSMutableArray array]; + for (NSDictionary *virtualInput in subElementDefs) { + if (filter && virtualInput[@"reportID"] && ![filter containsObject:virtualInput[@"reportID"]]) continue; + [(NSMutableArray *)elements addObject:[[JOYSubElement alloc] initWithRealElement:element + size:virtualInput[@"size"].unsignedLongValue + offset:virtualInput[@"offset"].unsignedLongValue + usagePage:virtualInput[@"usagePage"].unsignedLongValue + usage:virtualInput[@"usage"].unsignedLongValue + min:virtualInput[@"min"].unsignedIntValue + max:virtualInput[@"max"].unsignedIntValue]]; + } + isOutput = IOHIDElementGetType((__bridge IOHIDElementRef)_element) == kIOHIDElementTypeOutput; + [_multiElements setObject:elements forKey:element]; + } + else { + if (filter && ![filter containsObject:@(element.reportID)]) continue; + + switch (IOHIDElementGetType((__bridge IOHIDElementRef)_element)) { + /* Handled */ + case kIOHIDElementTypeInput_Misc: + case kIOHIDElementTypeInput_Button: + case kIOHIDElementTypeInput_Axis: + break; + case kIOHIDElementTypeOutput: + isOutput = true; + break; + /* Ignored */ + default: + case kIOHIDElementTypeInput_ScanCodes: + case kIOHIDElementTypeInput_NULL: + case kIOHIDElementTypeFeature: + case kIOHIDElementTypeCollection: + continue; + } + if (IOHIDElementIsArray((__bridge IOHIDElementRef)_element)) continue; + + elements = @[element]; + } + + _iokitToJOY[@(IOHIDElementGetCookie((__bridge IOHIDElementRef)_element))] = element; + + for (JOYElement *element in elements) { + if (isOutput) { + if (!_rumbleElement && rumbleUsage && rumbleUsagePage && element.usage == rumbleUsage && element.usagePage == rumbleUsagePage) { + if (hacks[JOYRumbleMin]) { + element.min = [hacks[JOYRumbleMin] unsignedIntValue]; + } + if (hacks[JOYRumbleMax]) { + element.max = [hacks[JOYRumbleMax] unsignedIntValue]; + } + _rumbleElement = element; + } + continue; + } + else { + if (!_connectedElement && connectedUsage && connectedUsagePage && element.usage == connectedUsage && element.usagePage == connectedUsagePage) { + _connectedElement = element; + _logicallyConnected = element.value != element.min; + continue; + } + } + + if (element.usagePage == kHIDPage_Button) { + button: { + JOYButton *button = [[JOYButton alloc] initWithElement: element]; + [_buttons setObject:button forKey:element]; + NSNumber *replacementUsage = hacks[JOYButtonUsageMapping][@(button.usage)]; + if (replacementUsage) { + button.usage = [replacementUsage unsignedIntValue]; + } + continue; + } + } + else if (element.usagePage == kHIDPage_GenericDesktop) { + switch (element.usage) { + case kHIDUsage_GD_X: + case kHIDUsage_GD_Y: + case kHIDUsage_GD_Z: + case kHIDUsage_GD_Rx: + case kHIDUsage_GD_Ry: + case kHIDUsage_GD_Rz: { + + JOYElement *other = previousAxisElement; + previousAxisElement = element; + if (!other) goto single; + if (other.usage >= element.usage) goto single; + if (other.reportID != element.reportID) goto single; + if (![axisGroups[@(other.usage)] isEqualTo: axisGroups[@(element.usage)]]) goto single; + if (other.parentID != element.parentID) goto single; + + JOYAxes2D *axes = nil; + if (other.usage == kHIDUsage_GD_Z && element.usage == kHIDUsage_GD_Rz && [hacks[JOYSwapZRz] boolValue]) { + axes = [[JOYAxes2D alloc] initWithFirstElement:element secondElement:other]; + } + else { + axes = [[JOYAxes2D alloc] initWithFirstElement:other secondElement:element]; + } + NSNumber *replacementUsage = hacks[JOYAxes2DUsageMapping][@(axes.usage)]; + if (replacementUsage) { + axes.usage = [replacementUsage unsignedIntValue]; + } + + [_axisEmulatedButtons removeObjectForKey:@(_axes[other].uniqueID)]; + [_axes removeObjectForKey:other]; + previousAxisElement = nil; + _axes2D[other] = axes; + _axes2D[element] = axes; + + if (axes2DEmulateButtons) { + _axes2DEmulatedButtons[@(axes.uniqueID)] = @[ + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:axes.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:axes.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:axes.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:axes.uniqueID | 0x400000000L], + ]; + } + + /* + for (NSArray *group in axes2d) { + break; + IOHIDElementRef first = (__bridge IOHIDElementRef)group[0]; + IOHIDElementRef second = (__bridge IOHIDElementRef)group[1]; + if (IOHIDElementGetUsage(first) > element.usage) continue; + if (IOHIDElementGetUsage(second) > element.usage) continue; + if (IOHIDElementGetReportID(first) != IOHIDElementGetReportID(element)) continue; + if ((IOHIDElementGetUsage(first) - kHIDUsage_GD_X) / 3 != (element.usage - kHIDUsage_GD_X) / 3) continue; + if (IOHIDElementGetParent(first) != IOHIDElementGetParent(element)) continue; + + [axes2d removeObject:group]; + [axes3d addObject:@[(__bridge id)first, (__bridge id)second, _element]]; + found = true; + break; + }*/ + break; + } + single: + case kHIDUsage_GD_Slider: + case kHIDUsage_GD_Dial: + case kHIDUsage_GD_Wheel: { + JOYAxis *axis = [[JOYAxis alloc] initWithElement: element]; + [_axes setObject:axis forKey:element]; + + NSNumber *replacementUsage = hacks[JOYAxisUsageMapping][@(axis.usage)]; + if (replacementUsage) { + axis.usage = [replacementUsage unsignedIntValue]; + } + + if (axesEmulateButtons && axis.usage >= JOYAxisUsageL1 && axis.usage <= JOYAxisUsageR3) { + _axisEmulatedButtons[@(axis.uniqueID)] = + [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageL1 + JOYButtonUsageL1 uniqueID:axis.uniqueID]; + } + + if (axesEmulateButtons && axis.usage >= JOYAxisUsageGeneric0) { + _axisEmulatedButtons[@(axis.uniqueID)] = + [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageGeneric0 + JOYButtonUsageGeneric0 uniqueID:axis.uniqueID]; + } + + break; + } + case kHIDUsage_GD_DPadUp: + case kHIDUsage_GD_DPadDown: + case kHIDUsage_GD_DPadRight: + case kHIDUsage_GD_DPadLeft: + case kHIDUsage_GD_Start: + case kHIDUsage_GD_Select: + case kHIDUsage_GD_SystemMainMenu: + goto button; + + case kHIDUsage_GD_Hatswitch: { + JOYHat *hat = [[JOYHat alloc] initWithElement: element]; + [_hats setObject:hat forKey:element]; + if (hatsEmulateButtons) { + _hatEmulatedButtons[@(hat.uniqueID)] = @[ + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:hat.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:hat.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:hat.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:hat.uniqueID | 0x400000000L], + ]; + } + break; + } + } + } + } + } + + [exposedControllers addObject:self]; + if (_logicallyConnected) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controllerConnected:)]) { + [listener controllerConnected:self]; + } + } + } + + return self; +} + +- (NSString *)deviceName +{ + return IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductKey)); +} + +- (NSString *)uniqueID +{ + NSString *serial = (__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDSerialNumberKey)); + if (!serial || [(__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDTransportKey)) isEqualToString:@"USB"]) { + serial = [NSString stringWithFormat:@"%04x%04x%08x", + [(__bridge NSNumber *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDVendorIDKey)) unsignedIntValue], + [(__bridge NSNumber *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductIDKey)) unsignedIntValue], + [(__bridge NSNumber *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDLocationIDKey)) unsignedIntValue]]; + } + if (_serialSuffix) { + return [NSString stringWithFormat:@"%@-%@", serial, _serialSuffix]; + } + return serial; +} + +- (NSString *)description +{ + return [NSString stringWithFormat:@"<%@: %p, %@, %@>", self.className, self, self.deviceName, self.uniqueID]; +} + +- (NSArray *)buttons +{ + NSMutableArray *ret = [[_buttons allValues] mutableCopy]; + [ret addObjectsFromArray:_axisEmulatedButtons.allValues]; + for (NSArray *array in _axes2DEmulatedButtons.allValues) { + [ret addObjectsFromArray:array]; + } + for (NSArray *array in _hatEmulatedButtons.allValues) { + [ret addObjectsFromArray:array]; + } + return ret; +} + +- (NSArray *)axes +{ + return [_axes allValues]; +} + +- (NSArray *)axes2D +{ + return [[NSSet setWithArray:[_axes2D allValues]] allObjects]; +} + +- (NSArray *)hats +{ + return [_hats allValues]; +} + +- (void)elementChanged:(IOHIDElementRef)element +{ + JOYElement *_element = _iokitToJOY[@(IOHIDElementGetCookie(element))]; + if (_element) { + [self _elementChanged:_element]; + } + else { + //NSLog(@"Unhandled usage %x (Cookie: %x, Usage: %x)", IOHIDElementGetUsage(element), IOHIDElementGetCookie(element), IOHIDElementGetUsage(element)); + } +} + +- (void)_elementChanged:(JOYElement *)element +{ + if (element == _connectedElement) { + bool old = self.connected; + _logicallyConnected = _connectedElement.value != _connectedElement.min; + if (!old && self.connected) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controllerConnected:)]) { + [listener controllerConnected:self]; + } + } + } + else if (old && !self.connected) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controllerDisconnected:)]) { + [listener controllerDisconnected:self]; + } + } + } + } + + { + NSArray *subElements = _multiElements[element]; + if (subElements) { + for (JOYElement *subElement in subElements) { + [self _elementChanged:subElement]; + } + return; + } + } + + if (!self.connected) return; + { + JOYButton *button = _buttons[element]; + if (button) { + if ([button updateState]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:buttonChangedState:)]) { + [listener controller:self buttonChangedState:button]; + } + } + } + return; + } + } + + + { + JOYAxis *axis = _axes[element]; + if (axis) { + if ([axis updateState]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:movedAxis:)]) { + [listener controller:self movedAxis:axis]; + } + } + JOYEmulatedButton *button = _axisEmulatedButtons[@(axis.uniqueID)]; + if ([button updateStateFromAxis:axis]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:buttonChangedState:)]) { + [listener controller:self buttonChangedState:button]; + } + } + } + } + return; + } + } + + { + JOYAxes2D *axes = _axes2D[element]; + if (axes) { + if ([axes updateState]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:movedAxes2D:)]) { + [listener controller:self movedAxes2D:axes]; + } + } + NSArray *buttons = _axes2DEmulatedButtons[@(axes.uniqueID)]; + for (JOYEmulatedButton *button in buttons) { + if ([button updateStateFromAxes2D:axes]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:buttonChangedState:)]) { + [listener controller:self buttonChangedState:button]; + } + } + } + } + } + return; + } + } + + { + JOYHat *hat = _hats[element]; + if (hat) { + if ([hat updateState]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:movedHat:)]) { + [listener controller:self movedHat:hat]; + } + } + + NSArray *buttons = _hatEmulatedButtons[@(hat.uniqueID)]; + for (JOYEmulatedButton *button in buttons) { + if ([button updateStateFromHat:hat]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controller:buttonChangedState:)]) { + [listener controller:self buttonChangedState:button]; + } + } + } + } + } + return; + } + } +} + +- (void)disconnected +{ + if (_logicallyConnected && [exposedControllers containsObject:self]) { + for (id listener in listeners) { + if ([listener respondsToSelector:@selector(controllerDisconnected:)]) { + [listener controllerDisconnected:self]; + } + } + } + _physicallyConnected = false; + [self setRumbleAmplitude:0]; // Stop the rumble thread. + [exposedControllers removeObject:self]; + _device = nil; +} + +- (void)sendReport:(NSData *)report +{ + if (!report.length) return; + IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length); +} + +- (void)setPlayerLEDs:(uint8_t)mask +{ + mask &= 0xF; + if (_isSwitch) { + _lastSwitchPacket.reportID = 0x1; // Rumble and LEDs + _lastSwitchPacket.sequence++; + _lastSwitchPacket.sequence &= 0xF; + _lastSwitchPacket.command = 0x30; // LED + _lastSwitchPacket.commandData[0] = mask; + [self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; + } +} + +- (void)pwmThread +{ + while (_rumblePWMRatio != 0) { + [_rumbleElement setValue:1]; + [NSThread sleepForTimeInterval:_rumblePWMRatio / 10]; + [_rumbleElement setValue:0]; + [NSThread sleepForTimeInterval:(1 - _rumblePWMRatio) / 10]; + } + [_rumblePWMThreadLock lock]; + [_rumblePWMThreadLock signal]; + [_rumblePWMThreadLock unlock]; +} + +- (void)setRumbleAmplitude:(double)amp /* andFrequency: (double)frequency */ +{ + double frequency = 144; // I have no idea what I'm doing. + + if (amp < 0) amp = 0; + if (amp > 1) amp = 1; + if (_isSwitch) { + if (amp == 0) { + amp = 1; + frequency = 0; + } + + uint8_t highAmp = amp * 0x64; + uint8_t lowAmp = amp * 0x32 + 0x40; + if (frequency < 0) frequency = 0; + if (frequency > 1252) frequency = 1252; + uint8_t encodedFrequency = (uint8_t)round(log2(frequency / 10.0) * 32.0); + + uint16_t highFreq = (encodedFrequency - 0x60) * 4; + uint8_t lowFreq = encodedFrequency - 0x40; + + //if (frequency < 82 || frequency > 312) { + if (amp) { + highAmp = 0; + } + + if (frequency < 40 || frequency > 626) { + lowAmp = 0; + } + + _lastSwitchPacket.rumbleData[0] = _lastSwitchPacket.rumbleData[4] = highFreq & 0xFF; + _lastSwitchPacket.rumbleData[1] = _lastSwitchPacket.rumbleData[5] = (highAmp << 1) + ((highFreq >> 8) & 0x1); + _lastSwitchPacket.rumbleData[2] = _lastSwitchPacket.rumbleData[6] = lowFreq; + _lastSwitchPacket.rumbleData[3] = _lastSwitchPacket.rumbleData[7] = lowAmp; + + + _lastSwitchPacket.reportID = 0x10; // Rumble only + _lastSwitchPacket.sequence++; + _lastSwitchPacket.sequence &= 0xF; + _lastSwitchPacket.command = 0; // LED + [self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; + } + else { + if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { + if (_rumblePWMRatio == 0) { // PWM thread not running, start it. + if (amp != 0) { + _rumblePWMRatio = amp; + [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; + } + } + else { + if (amp == 0) { // Thread is running, signal it to stop + [_rumblePWMThreadLock lock]; + _rumblePWMRatio = 0; + [_rumblePWMThreadLock wait]; + [_rumblePWMThreadLock unlock]; + } + else { + _rumblePWMRatio = amp; + } + } + } + else { + [_rumbleElement setValue:amp * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; + } + } +} + +- (bool)isConnected +{ + return _logicallyConnected && _physicallyConnected; +} + ++ (void)controllerAdded:(IOHIDDeviceRef) device +{ + NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); + NSDictionary *hacks = hacksByName[name]; + NSArray *filters = hacks[JOYReportIDFilters]; + if (filters) { + JOYController *controller = [[JOYMultiplayerController alloc] initWithDevice:device + reportIDFilters:filters]; + [controllers setObject:controller forKey:[NSValue valueWithPointer:device]]; + } + else { + [controllers setObject:[[JOYController alloc] initWithDevice:device] forKey:[NSValue valueWithPointer:device]]; + } +} + ++ (void)controllerRemoved:(IOHIDDeviceRef) device +{ + [[controllers objectForKey:[NSValue valueWithPointer:device]] disconnected]; + [controllers removeObjectForKey:[NSValue valueWithPointer:device]]; +} + ++ (NSArray *)allControllers +{ + return exposedControllers; +} + ++ (void)load +{ +#include "ControllerConfiguration.inc" +} + ++(void)registerListener:(id)listener +{ + [listeners addObject:listener]; +} + ++(void)unregisterListener:(id)listener +{ + [listeners removeObject:listener]; +} + ++ (void)startOnRunLoop:(NSRunLoop *)runloop withOptions: (NSDictionary *)options +{ + axesEmulateButtons = [options[JOYAxesEmulateButtonsKey] boolValue]; + axes2DEmulateButtons = [options[JOYAxes2DEmulateButtonsKey] boolValue]; + hatsEmulateButtons = [options[JOYHatsEmulateButtonsKey] boolValue]; + + controllers = [NSMutableDictionary dictionary]; + exposedControllers = [NSMutableArray array]; + NSArray *array = @[ + CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick), + CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad), + CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController), + @{@kIOHIDDeviceUsagePageKey: @(kHIDPage_Game)}, + ]; + + listeners = [NSMutableSet set]; + static IOHIDManagerRef manager = nil; + if (manager) { + CFRelease(manager); // Stop the previous session + } + manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); + + if (!manager) return; + if (IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone)) { + CFRelease(manager); + return; + } + + IOHIDManagerSetDeviceMatchingMultiple(manager, (__bridge CFArrayRef)array); + IOHIDManagerRegisterDeviceMatchingCallback(manager, HIDDeviceAdded, NULL); + IOHIDManagerRegisterDeviceRemovalCallback(manager, HIDDeviceRemoved, NULL); + IOHIDManagerScheduleWithRunLoop(manager, [runloop getCFRunLoop], kCFRunLoopDefaultMode); +} +@end diff --git a/JoyKit/JOYElement.h b/JoyKit/JOYElement.h new file mode 100644 index 0000000..860c247 --- /dev/null +++ b/JoyKit/JOYElement.h @@ -0,0 +1,20 @@ +#import +#include + +@interface JOYElement : NSObject +- (instancetype)initWithElement:(IOHIDElementRef)element; +- (int32_t)value; +- (NSData *)dataValue; +- (void)setValue:(uint32_t)value; +- (void)setDataValue:(NSData *)value; +@property (readonly) uint16_t usage; +@property (readonly) uint16_t usagePage; +@property (readonly) uint32_t uniqueID; +@property int32_t min; +@property int32_t max; +@property (readonly) int32_t reportID; +@property (readonly) int32_t parentID; + +@end + + diff --git a/JoyKit/JOYElement.m b/JoyKit/JOYElement.m new file mode 100644 index 0000000..56fcb36 --- /dev/null +++ b/JoyKit/JOYElement.m @@ -0,0 +1,96 @@ +#import "JOYElement.h" +#include + +@implementation JOYElement +{ + id _element; + IOHIDDeviceRef _device; + int32_t _min, _max; +} + +- (int32_t)min +{ + return MIN(_min, _max); +} + +- (int32_t)max +{ + return MAX(_max, _min); +} + +-(void)setMin:(int32_t)min +{ + _min = min; +} + +- (void)setMax:(int32_t)max +{ + _max = max; +} + +- (instancetype)initWithElement:(IOHIDElementRef)element +{ + if ((self = [super init])) { + _element = (__bridge id)element; + _usage = IOHIDElementGetUsage(element); + _usagePage = IOHIDElementGetUsagePage(element); + _uniqueID = (uint32_t)IOHIDElementGetCookie(element); + _min = (int32_t) IOHIDElementGetLogicalMin(element); + _max = (int32_t) IOHIDElementGetLogicalMax(element); + _reportID = IOHIDElementGetReportID(element); + IOHIDElementRef parent = IOHIDElementGetParent(element); + _parentID = parent? (uint32_t)IOHIDElementGetCookie(parent) : -1; + _device = IOHIDElementGetDevice(element); + } + return self; +} + +- (int32_t)value +{ + IOHIDValueRef value = NULL; + IOHIDDeviceGetValue(_device, (__bridge IOHIDElementRef)_element, &value); + if (!value) return 0; + CFRelease(CFRetain(value)); // For some reason, this is required to prevent leaks. + return (int32_t)IOHIDValueGetIntegerValue(value); +} + +- (NSData *)dataValue +{ + IOHIDValueRef value = NULL; + IOHIDDeviceGetValue(_device, (__bridge IOHIDElementRef)_element, &value); + if (!value) return 0; + CFRelease(CFRetain(value)); // For some reason, this is required to prevent leaks. + return [NSData dataWithBytes:IOHIDValueGetBytePtr(value) length:IOHIDValueGetLength(value)]; +} + +- (void)setValue:(uint32_t)value +{ + IOHIDValueRef ivalue = IOHIDValueCreateWithIntegerValue(NULL, (__bridge IOHIDElementRef)_element, 0, value); + IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); + CFRelease(ivalue); +} + +- (void)setDataValue:(NSData *)value +{ + IOHIDValueRef ivalue = IOHIDValueCreateWithBytes(NULL, (__bridge IOHIDElementRef)_element, 0, value.bytes, value.length); + IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); + CFRelease(ivalue); +} + +/* For use as a dictionary key */ + +- (NSUInteger)hash +{ + return self.uniqueID; +} + +- (BOOL)isEqual:(id)object +{ + return self->_element == object; +} + +- (id)copyWithZone:(nullable NSZone *)zone; +{ + return self; +} +@end diff --git a/JoyKit/JOYEmulatedButton.h b/JoyKit/JOYEmulatedButton.h new file mode 100644 index 0000000..491e0c7 --- /dev/null +++ b/JoyKit/JOYEmulatedButton.h @@ -0,0 +1,11 @@ +#import "JOYButton.h" +#import "JOYAxis.h" +#import "JOYAxes2D.h" +#import "JOYHat.h" + +@interface JOYEmulatedButton : JOYButton +- (instancetype)initWithUsage:(JOYButtonUsage)usage uniqueID:(uint64_t)uniqueID; +- (bool)updateStateFromAxis:(JOYAxis *)axis; +- (bool)updateStateFromAxes2D:(JOYAxes2D *)axes; +- (bool)updateStateFromHat:(JOYHat *)hat; +@end diff --git a/JoyKit/JOYEmulatedButton.m b/JoyKit/JOYEmulatedButton.m new file mode 100644 index 0000000..1ebed3a --- /dev/null +++ b/JoyKit/JOYEmulatedButton.m @@ -0,0 +1,91 @@ +#import "JOYEmulatedButton.h" + +@interface JOYButton () +{ + @public bool _state; +} +@end + +@implementation JOYEmulatedButton +{ + uint64_t _uniqueID; +} + +- (instancetype)initWithUsage:(JOYButtonUsage)usage uniqueID:(uint64_t)uniqueID; +{ + self = [super init]; + self.usage = usage; + _uniqueID = uniqueID; + + return self; +} + +- (uint64_t)uniqueID +{ + return _uniqueID; +} + +- (bool)updateStateFromAxis:(JOYAxis *)axis +{ + bool old = _state; + _state = [axis value] > 0.5; + return _state != old; +} + +- (bool)updateStateFromAxes2D:(JOYAxes2D *)axes +{ + bool old = _state; + if (axes.distance < 0.5) { + _state = false; + } + else { + unsigned direction = ((unsigned)round(axes.angle / 360 * 8)) & 7; + switch (self.usage) { + case JOYButtonUsageDPadLeft: + _state = direction >= 3 && direction <= 5; + break; + case JOYButtonUsageDPadRight: + _state = direction <= 1 || direction == 7; + break; + case JOYButtonUsageDPadUp: + _state = direction >= 5; + break; + case JOYButtonUsageDPadDown: + _state = direction <= 3 && direction >= 1; + break; + default: + break; + } + } + return _state != old; +} + +- (bool)updateStateFromHat:(JOYHat *)hat +{ + bool old = _state; + if (!hat.pressed) { + _state = false; + } + else { + unsigned direction = ((unsigned)round(hat.angle / 360 * 8)) & 7; + switch (self.usage) { + case JOYButtonUsageDPadLeft: + _state = direction >= 3 && direction <= 5; + break; + case JOYButtonUsageDPadRight: + _state = direction <= 1 || direction == 7; + break; + case JOYButtonUsageDPadUp: + _state = direction >= 5; + break; + case JOYButtonUsageDPadDown: + _state = direction <= 3 && direction >= 1; + break; + default: + break; + } + } + return _state != old; +} + +@end diff --git a/JoyKit/JOYHat.h b/JoyKit/JOYHat.h new file mode 100644 index 0000000..05a5829 --- /dev/null +++ b/JoyKit/JOYHat.h @@ -0,0 +1,11 @@ +#import + +@interface JOYHat : NSObject +- (uint64_t)uniqueID; +- (double)angle; +- (unsigned)resolution; +@property (readonly, getter=isPressed) bool pressed; + +@end + + diff --git a/JoyKit/JOYHat.m b/JoyKit/JOYHat.m new file mode 100644 index 0000000..743e49c --- /dev/null +++ b/JoyKit/JOYHat.m @@ -0,0 +1,60 @@ +#import "JOYHat.h" +#import "JOYElement.h" + +@implementation JOYHat +{ + JOYElement *_element; + double _state; +} + +- (uint64_t)uniqueID +{ + return _element.uniqueID; +} + +- (NSString *)description +{ + if (self.isPressed) { + return [NSString stringWithFormat:@"<%@: %p (%llu); State: %f degrees>", self.className, self, self.uniqueID, self.angle]; + } + return [NSString stringWithFormat:@"<%@: %p (%llu); State: released>", self.className, self, self.uniqueID]; + +} + +- (instancetype)initWithElement:(JOYElement *)element +{ + self = [super init]; + if (!self) return self; + + _element = element; + + return self; +} + +- (bool)isPressed +{ + return _state >= 0 && _state < 360; +} + +- (double)angle +{ + if (self.isPressed) return fmod((_state + 270), 360); + return -1; +} + +- (unsigned)resolution +{ + return _element.max - _element.min + 1; +} + +- (bool)updateState +{ + unsigned state = ([_element value] - _element.min) * 360.0 / self.resolution; + if (_state != state) { + _state = state; + return true; + } + return false; +} + +@end diff --git a/JoyKit/JOYMultiplayerController.h b/JoyKit/JOYMultiplayerController.h new file mode 100644 index 0000000..24004f5 --- /dev/null +++ b/JoyKit/JOYMultiplayerController.h @@ -0,0 +1,8 @@ +#import "JOYController.h" +#include + +@interface JOYMultiplayerController : JOYController +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters; +@end + + diff --git a/JoyKit/JOYMultiplayerController.m b/JoyKit/JOYMultiplayerController.m new file mode 100644 index 0000000..0952c84 --- /dev/null +++ b/JoyKit/JOYMultiplayerController.m @@ -0,0 +1,44 @@ +#import "JOYMultiplayerController.h" + +@interface JOYController () +- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix; +- (void)elementChanged:(IOHIDElementRef) element toValue:(IOHIDValueRef) value; +- (void)disconnected; +@end + +@implementation JOYMultiplayerController +{ + NSMutableArray *_children; +} + +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters +{ + self = [super init]; + if (!self) return self; + + _children = [NSMutableArray array]; + + unsigned index = 1; + for (NSArray *filter in reportIDFilters) { + JOYController *controller = [[JOYController alloc] initWithDevice:device reportIDFilter:filter serialSuffix:[NSString stringWithFormat:@"%d", index]]; + [_children addObject:controller]; + index++; + } + return self; +} + +- (void)elementChanged:(IOHIDElementRef) element toValue:(IOHIDValueRef) value +{ + for (JOYController *child in _children) { + [child elementChanged:element toValue:value]; + } +} + +- (void)disconnected +{ + for (JOYController *child in _children) { + [child disconnected]; + } +} + +@end diff --git a/JoyKit/JOYSubElement.h b/JoyKit/JOYSubElement.h new file mode 100644 index 0000000..a13b5c7 --- /dev/null +++ b/JoyKit/JOYSubElement.h @@ -0,0 +1,14 @@ +#import "JOYElement.h" + +@interface JOYSubElement : JOYElement +- (instancetype)initWithRealElement:(JOYElement *)element + size:(size_t) size // in bits + offset:(size_t) offset // in bits + usagePage:(uint16_t)usagePage + usage:(uint16_t)usage + min:(int32_t)min + max:(int32_t)max; + +@end + + diff --git a/JoyKit/JOYSubElement.m b/JoyKit/JOYSubElement.m new file mode 100644 index 0000000..55e289e --- /dev/null +++ b/JoyKit/JOYSubElement.m @@ -0,0 +1,99 @@ +#import "JOYSubElement.h" + +@interface JOYElement () +{ + @public uint16_t _usage; + @public uint16_t _usagePage; + @public uint32_t _uniqueID; + @public int32_t _min; + @public int32_t _max; + @public int32_t _reportID; + @public int32_t _parentID; +} +@end + +@implementation JOYSubElement +{ + JOYElement *_parent; + size_t _size; // in bits + size_t _offset; // in bits +} + +- (instancetype)initWithRealElement:(JOYElement *)element + size:(size_t) size // in bits + offset:(size_t) offset // in bits + usagePage:(uint16_t)usagePage + usage:(uint16_t)usage + min:(int32_t)min + max:(int32_t)max +{ + if ((self = [super init])) { + _parent = element; + _size = size; + _offset = offset; + _usage = usage; + _usagePage = usagePage; + _uniqueID = (uint32_t)((_parent.uniqueID << 16) | offset); + _min = min; + _max = max; + _reportID = _parent.reportID; + _parentID = _parent.parentID; + } + return self; +} + +- (int32_t)value +{ + NSData *parentValue = [_parent dataValue]; + if (!parentValue) return 0; + if (_size > 32) return 0; + if (_size + (_offset % 8) > 32) return 0; + size_t parentLength = parentValue.length; + if (_size > parentLength * 8) return 0; + if (_size + _offset >= parentLength * 8) return 0; + const uint8_t *bytes = parentValue.bytes; + + uint8_t temp[4] = {0,}; + memcpy(temp, bytes + _offset / 8, (_offset + _size - 1) / 8 - _offset / 8 + 1); + uint32_t ret = (*(uint32_t *)temp) >> (_offset % 8); + ret &= (1 << _size) - 1; + + if (_max < _min) { + return _max + _min - ret; + } + + return ret; +} + +- (void)setValue: (uint32_t) value +{ + NSMutableData *dataValue = [[_parent dataValue] mutableCopy]; + if (!dataValue) return; + if (_size > 32) return; + if (_size + (_offset % 8) > 32) return; + size_t parentLength = dataValue.length; + if (_size > parentLength * 8) return; + if (_size + _offset >= parentLength * 8) return; + uint8_t *bytes = dataValue.mutableBytes; + + uint8_t temp[4] = {0,}; + memcpy(temp, bytes + _offset / 8, (_offset + _size - 1) / 8 - _offset / 8 + 1); + (*(uint32_t *)temp) &= ~((1 << (_size - 1)) << (_offset % 8)); + (*(uint32_t *)temp) |= (value) << (_offset % 8); + memcpy(bytes + _offset / 8, temp, (_offset + _size - 1) / 8 - _offset / 8 + 1); + [_parent setDataValue:dataValue]; +} + +- (NSData *)dataValue +{ + [self doesNotRecognizeSelector:_cmd]; + return nil; +} + +- (void)setDataValue:(NSData *)data +{ + [self doesNotRecognizeSelector:_cmd]; +} + + +@end diff --git a/JoyKit/JoyKit.h b/JoyKit/JoyKit.h new file mode 100644 index 0000000..d56b505 --- /dev/null +++ b/JoyKit/JoyKit.h @@ -0,0 +1,6 @@ +#ifndef JoyKit_h +#define JoyKit_h + +#include "JOYController.h" + +#endif diff --git a/Makefile b/Makefile index 79b5e14..cc6cae4 100644 --- a/Makefile +++ b/Makefile @@ -140,7 +140,7 @@ SDL_SOURCES := $(shell ls SDL/*.c) $(OPEN_DIALOG) TESTER_SOURCES := $(shell ls Tester/*.c) ifeq ($(PLATFORM),Darwin) -COCOA_SOURCES := $(shell ls Cocoa/*.m) $(shell ls HexFiend/*.m) +COCOA_SOURCES := $(shell ls Cocoa/*.m) $(shell ls HexFiend/*.m) $(shell ls JoyKit/*.m) QUICKLOOK_SOURCES := $(shell ls QuickLook/*.m) $(shell ls QuickLook/*.c) endif diff --git a/libretro/libretro.c b/libretro/libretro.c index e075b2c..6c1fcb6 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -141,12 +141,17 @@ static void GB_update_keys_status(GB_gameboy_t *gb, unsigned port) GB_set_key_state_for_player(gb, GB_KEY_START, emulated_devices == 1 ? port : 0, input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)); - if (gb->rumble_state) - rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 65535); - else - rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 0); } +static void rumble_callback(GB_gameboy_t *gb, double amplitude) +{ + if (gb == &gameboy[0]) { + rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535 * amplitude); + } + else if (gb == &gameboy[1]) { + rumble.set_rumble_state(1, RETRO_RUMBLE_STRONG, 65535 * amplitude); + } +} static void audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { @@ -370,6 +375,8 @@ static void init_for_current_model(unsigned id) GB_set_rgb_encode_callback(&gameboy[i], rgb_encode); GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); GB_apu_set_sample_callback(&gameboy[i], audio_callback); + GB_set_rumble_callback(&gameboy[i], rumble_callback); + /* todo: attempt to make these more generic */ GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); From 70542137f2143e626aa2253f97d360b7e8bd46c5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 29 Oct 2019 20:31:20 +0200 Subject: [PATCH 106/341] Fix #214 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 79b5e14..dfab248 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ ifeq ($(PLATFORM),Darwin) SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null) CFLAGS += -F/Library/Frameworks OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9 -LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit +LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 -framework OpenGL endif CFLAGS += -Wno-deprecated-declarations From 719a92d8a468770a85777134130ad0de6e4cd5bf Mon Sep 17 00:00:00 2001 From: Matthew Coppola Date: Sat, 2 Nov 2019 23:31:23 -0400 Subject: [PATCH 107/341] SDL2: Fix fullscreen viewport bug --- SDL/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL/main.c b/SDL/main.c index ba2a5fb..2993d36 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -123,7 +123,7 @@ static void handle_events(GB_gameboy_t *gb) } case SDL_WINDOWEVENT: { - if (event.window.event == SDL_WINDOWEVENT_RESIZED) { + if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { update_viewport(); } break; From 139ae8cc08b2d882f1e1d21a7a0aafdc3f0e3824 Mon Sep 17 00:00:00 2001 From: Matthew Coppola Date: Sat, 2 Nov 2019 23:50:29 -0400 Subject: [PATCH 108/341] SDL2: Write battery to disk when ROMs are hot-swapped --- SDL/main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index ba2a5fb..cc3fd20 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -406,14 +406,12 @@ static bool handle_pending_command(void) return false; } - case GB_SDL_RESET_COMMAND: - GB_save_battery(&gb, battery_save_path_ptr); - return true; - case GB_SDL_NO_COMMAND: return false; + case GB_SDL_RESET_COMMAND: case GB_SDL_NEW_FILE_COMMAND: + GB_save_battery(&gb, battery_save_path_ptr); return true; case GB_SDL_QUIT_COMMAND: From 2f4a10913b2df3de5279aec58a3bf758b58f3194 Mon Sep 17 00:00:00 2001 From: Matthew Coppola Date: Sat, 2 Nov 2019 23:43:25 -0400 Subject: [PATCH 109/341] SDL2: Hide mouse cursor when menu is not active --- SDL/gui.c | 1 + SDL/main.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/SDL/gui.c b/SDL/gui.c index 6cffeaf..b34bb1a 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -792,6 +792,7 @@ void connect_joypad(void) void run_gui(bool is_running) { + SDL_ShowCursor(SDL_ENABLE); connect_joypad(); /* Draw the background screen */ diff --git a/SDL/main.c b/SDL/main.c index ba2a5fb..d96ee1b 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -98,6 +98,7 @@ static void open_menu(void) SDL_PauseAudioDevice(device_id, 1); } run_gui(true); + SDL_ShowCursor(SDL_DISABLE); if (audio_playing) { SDL_ClearQueuedAudio(device_id); SDL_PauseAudioDevice(device_id, 0); @@ -425,6 +426,7 @@ static bool handle_pending_command(void) static void run(void) { + SDL_ShowCursor(SDL_DISABLE); GB_model_t model; pending_command = GB_SDL_NO_COMMAND; restart: From 143e1f88a8dc3fee4a40dbebd843cbb8cfbd5e84 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 3 Nov 2019 22:02:33 +0200 Subject: [PATCH 110/341] =?UTF-8?q?There=E2=80=99s=20not=20reason=20it=20m?= =?UTF-8?q?ust=20be=20an=20integer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 2 +- Core/apu.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 3be92d6..9afa5c9 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -1008,7 +1008,7 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate) GB_apu_update_cycles_per_sample(gb); } -void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, unsigned cycles_per_sample) +void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample) { if (cycles_per_sample == 0) { diff --git a/Core/apu.h b/Core/apu.h index ee6055b..885e0ce 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -148,7 +148,7 @@ typedef struct { } GB_apu_output_t; void GB_set_sample_rate(GB_gameboy_t *gb, unsigned sample_rate); -void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, unsigned cycles_per_sample); /* Cycles are in 8MHz units */ +void GB_set_sample_rate_by_clocks(GB_gameboy_t *gb, double cycles_per_sample); /* Cycles are in 8MHz units */ void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode); void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback); #ifdef GB_INTERNAL From c8023618009cca1f7f0707e8a6d1188aad574ee5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 8 Nov 2019 16:14:16 +0200 Subject: [PATCH 111/341] Whoops, this function was missing --- Core/memory.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Core/memory.c b/Core/memory.c index c481ad2..7e74a80 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -422,6 +422,11 @@ static GB_read_function_t * const read_map[] = read_ram, read_high_memory, /* EXXX FXXX */ }; +void GB_set_read_memory_callback(GB_gameboy_t *gb, GB_read_memory_callback_t callback) +{ + gb->read_memory_callback = callback; +} + uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr) { if (gb->n_watchpoints) { From 31609319deaab51cea6113d976681b83f628bfb2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 9 Nov 2019 14:45:38 +0200 Subject: [PATCH 112/341] Fix the set_joyp API --- Core/memory.c | 6 +++++- Core/sgb.c | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index 7e74a80..003bb77 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -753,7 +753,11 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; case GB_IO_JOYP: - if ((gb->io_registers[GB_IO_JOYP] & 0x30) != (value & 0x30)) { + if (gb->joyp_write_callback) { + gb->joyp_write_callback(gb, value); + GB_update_joyp(gb); + } + else if ((gb->io_registers[GB_IO_JOYP] & 0x30) != (value & 0x30)) { GB_sgb_write(gb, value); gb->io_registers[GB_IO_JOYP] = value & 0xF0; GB_update_joyp(gb); diff --git a/Core/sgb.c b/Core/sgb.c index 8539238..7ebeae0 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -385,11 +385,7 @@ static void command_ready(GB_gameboy_t *gb) } void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) -{ - if (gb->joyp_write_callback) { - gb->joyp_write_callback(gb, value); - } - +{ if (!GB_is_sgb(gb)) return; if (!GB_is_hle_sgb(gb)) { /* Notify via callback */ From bd9ac204c2cb95427c49d655b35921fb2d2da67b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 20 Nov 2019 22:40:03 +0200 Subject: [PATCH 113/341] Allow SameBoy to compile on 4-byte-bools platforms --- Core/gb.c | 11 +++++++++-- Core/gb.h | 4 ---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 93ac9d7..f29d400 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -18,6 +18,13 @@ #define GB_rewind_push(...) #endif + +static inline uint32_t state_magic(void) +{ + if (sizeof(bool) == 1) return 'SAME'; + return 'S4ME'; +} + void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args) { char *string = NULL; @@ -660,7 +667,7 @@ void GB_disconnect_serial(GB_gameboy_t *gb) bool GB_is_inited(GB_gameboy_t *gb) { - return gb->magic == 'SAME'; + return gb->magic == state_magic(); } bool GB_is_cgb(GB_gameboy_t *gb) @@ -929,7 +936,7 @@ void GB_reset(GB_gameboy_t *gb) gb->nontrivial_jump_state = NULL; } - gb->magic = (uintptr_t)'SAME'; + gb->magic = state_magic(); } void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model) diff --git a/Core/gb.h b/Core/gb.h index dbd9e16..a561111 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -278,10 +278,6 @@ typedef struct { This struct is not packed, but dumped sections exclusively use types that have the same alignment in both 32 and 64 bit platforms. */ -/* We make sure bool is 1 for cross-platform save state compatibility. */ -/* Todo: We might want to typedef our own bool if this prevents SameBoy from working on specific platforms. */ -_Static_assert(sizeof(bool) == 1, "sizeof(bool) != 1"); - #ifdef GB_INTERNAL struct GB_gameboy_s { #else From 436dc0b67a0e392ff0e0e1c05f16e9292f4245eb Mon Sep 17 00:00:00 2001 From: retro-wertz Date: Sat, 13 Jul 2019 22:11:29 +0800 Subject: [PATCH 114/341] Fix GBC memory map and add IO port range for cheevos --- libretro/libretro.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 54c7906..59dfdc4 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -425,21 +425,20 @@ static void init_for_current_model(unsigned id) descs[7].len = 0x4000; descs[7].flags = RETRO_MEMDESC_CONST; - descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank); - descs[8].start = 0xFE00; - descs[8].select = 0xFFFFFF00; - descs[8].len = 0x00A0; + descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank); + descs[8].start = 0xFE00; + descs[8].len = 0x00A0; + descs[8].select= 0xFFFFFF00; - descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ - descs[9].start = 0x10000; - descs[9].select = 0xFFFF0000; - descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ + descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ + descs[9].start = 0x10000; + descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ + descs[9].select= 0xFFFF0000; - descs[10].ptr = descs[8].ptr; - descs[10].offset = 0x100; - descs[10].start = 0xFF00; - descs[10].select = 0xFFFFFF00; - descs[10].len = 0x0080; + descs[10].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IO, &size, &bank); + descs[10].start = 0xFF00; + descs[10].len = 0x0080; + descs[10].select= 0xFFFFFF00; struct retro_memory_map mmaps; mmaps.descriptors = descs; From 7c9508ae961d9ddf6cdf8a8ff638261ddb4b5bb3 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Tue, 25 Jun 2019 21:01:54 -0600 Subject: [PATCH 115/341] Include the canonical SDL2 path, which drops the SDL2/ prefix. Use pkg-config or sdl2-config to determine SDL and GL compilation flags. --- Makefile | 29 +++++++++++++++++++++++++++-- SDL/gui.c | 2 +- SDL/gui.h | 2 +- SDL/main.c | 2 +- SDL/opengl_compat.c | 2 +- SDL/opengl_compat.h | 4 ++-- SDL/utils.c | 2 +- 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index dfab248..bc2b371 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,11 @@ CC := clang endif endif +# Find libraries with pkg-config if available. +ifneq (, $(shell which pkg-config)) +PKG_CONFIG := pkg-config +endif + ifeq ($(PLATFORM),windows32) # To force use of the Unix version instead of the Windows version MKDIR := $(shell which mkdir) @@ -82,7 +87,19 @@ endif CFLAGS += -Werror -Wall -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES -SDL_LDFLAGS := -lSDL2 -lGL +ifeq (,$(PKG_CONFIG)) +SDL_CFLAGS := $(shell sdl2-config --cflags) +SDL_LDFLAGS := $(shell sdl2-config --libs) +else +SDL_CFLAGS := $(shell $(PKG_CONFIG) --cflags sdl2) +SDL_LDFLAGS := $(shell $(PKG_CONFIG) --libs sdl2) +endif +ifeq (,$(PKG_CONFIG)) +GL_LDFLAGS := -lGL +else +GL_CFLAGS := $(shell $(PKG_CONFIG) --cflags gl) +GL_LDFLAGS := $(shell $(PKG_CONFIG) --libs gl || echo -lGL) +endif ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows -Drandom=rand LDFLAGS += -lmsvcrt -lcomdlg32 -lSDL2main -Wl,/MANIFESTFILE:NUL @@ -169,6 +186,10 @@ ifneq ($(filter $(MAKECMDGOALS),cocoa),) endif endif +$(OBJ)/SDL/%.dep: SDL/% + -@$(MKDIR) -p $(dir $@) + $(CC) $(CFLAGS) $(SDL_CFLAGS) $(GL_CFLAGS) -MT $(OBJ)/$^.o -M $^ -c -o $@ + $(OBJ)/%.dep: % -@$(MKDIR) -p $(dir $@) $(CC) $(CFLAGS) -MT $(OBJ)/$^.o -M $^ -c -o $@ @@ -179,6 +200,10 @@ $(OBJ)/Core/%.c.o: Core/%.c -@$(MKDIR) -p $(dir $@) $(CC) $(CFLAGS) -DGB_INTERNAL -c $< -o $@ +$(OBJ)/SDL/%.c.o: SDL/%.c + -@$(MKDIR) -p $(dir $@) + $(CC) $(CFLAGS) $(SDL_CFLAGS) $(GL_CFLAGS) -c $< -o $@ + $(OBJ)/%.c.o: %.c -@$(MKDIR) -p $(dir $@) $(CC) $(CFLAGS) -c $< -o $@ @@ -256,7 +281,7 @@ $(BIN)/SameBoy.qlgenerator/Contents/Resources/cgb_boot_fast.bin: $(BIN)/BootROMs # Unix versions build only one binary $(BIN)/SDL/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS) -@$(MKDIR) -p $(dir $@) - $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) + $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) ifeq ($(CONF), release) strip $@ endif diff --git a/SDL/gui.c b/SDL/gui.c index b34bb1a..3bfd99e 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include diff --git a/SDL/gui.h b/SDL/gui.h index 6974849..b22c74f 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -1,7 +1,7 @@ #ifndef gui_h #define gui_h -#include +#include #include #include #include "shader.h" diff --git a/SDL/main.c b/SDL/main.c index 933237e..a095064 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include "utils.h" #include "gui.h" diff --git a/SDL/opengl_compat.c b/SDL/opengl_compat.c index aed2a76..af7ce6d 100644 --- a/SDL/opengl_compat.c +++ b/SDL/opengl_compat.c @@ -1,5 +1,5 @@ #define GL_GLEXT_PROTOTYPES -#include +#include #ifndef __APPLE__ #define GL_COMPAT_NAME(func) gl_compat_##func diff --git a/SDL/opengl_compat.h b/SDL/opengl_compat.h index 9fd1ca9..15b2a17 100644 --- a/SDL/opengl_compat.h +++ b/SDL/opengl_compat.h @@ -2,8 +2,8 @@ #define opengl_compat_h #define GL_GLEXT_PROTOTYPES -#include -#include +#include +#include #ifndef __APPLE__ #define GL_COMPAT_NAME(func) gl_compat_##func diff --git a/SDL/utils.c b/SDL/utils.c index eee6ce6..8cdd00b 100644 --- a/SDL/utils.c +++ b/SDL/utils.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include "utils.h" From 8a99d41c3199f5e1dcb2f53e51ec424aece5716e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 26 Dec 2019 02:00:58 +0200 Subject: [PATCH 116/341] Fix broken SDL builds on macOS and Windows --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bc2b371..1c23610 100644 --- a/Makefile +++ b/Makefile @@ -103,7 +103,8 @@ endif ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows -Drandom=rand LDFLAGS += -lmsvcrt -lcomdlg32 -lSDL2main -Wl,/MANIFESTFILE:NUL -SDL_LDFLAGS := -lSDL2 -lopengl32 +SDL_LDFLAGS := -lSDL2 +GL_LDFLAGS := -lopengl32 else LDFLAGS += -lc -lm -ldl endif @@ -113,7 +114,8 @@ SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null) CFLAGS += -F/Library/Frameworks OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9 LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 -SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 -framework OpenGL +SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 +GL_LDFLAGS := -framework OpenGL endif CFLAGS += -Wno-deprecated-declarations ifeq ($(PLATFORM),windows32) From 4c243235309dd52fd5bb4469065fedcf417fb7b9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 29 Dec 2019 17:34:43 +0100 Subject: [PATCH 117/341] Fix Game Boy Camera support in macOS Mojave and newer --- Cocoa/Document.m | 17 +++++++++++++++++ Cocoa/Info.plist | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index a520d62..85dd728 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -1171,6 +1171,23 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @try { if (!cameraSession) { + if (@available(macOS 10.14, *)) { + switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) { + case AVAuthorizationStatusAuthorized: + break; + case AVAuthorizationStatusNotDetermined: { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { + [self cameraRequestUpdate]; + }]; + return; + } + case AVAuthorizationStatusDenied: + case AVAuthorizationStatusRestricted: + GB_camera_updated(&gb); + return; + } + } + NSError *error; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice: device error: &error]; diff --git a/Cocoa/Info.plist b/Cocoa/Info.plist index dd801cb..d86b9fd 100644 --- a/Cocoa/Info.plist +++ b/Cocoa/Info.plist @@ -2,6 +2,8 @@ + CFBundleDisplayName + SameBoy CFBundleDevelopmentRegion en CFBundleDocumentTypes @@ -116,6 +118,8 @@ + NSCameraUsageDescription + SameBoy needs to access your camera to emulate the Game Boy Camera NSSupportsAutomaticGraphicsSwitching From e434b625ea14ee8f9937e0cc6e1ea39ec5d040b8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 30 Dec 2019 16:19:06 +0100 Subject: [PATCH 118/341] Allow the fullscreen key combo to work while in the menu --- SDL/gui.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SDL/gui.c b/SDL/gui.c index 3bfd99e..1664e74 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1012,6 +1012,15 @@ void run_gui(bool is_running) } case SDL_KEYDOWN: + if (event.key.keysym.scancode == SDL_SCANCODE_F && event.key.keysym.mod & MODIFIER) { + if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) { + SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); + } + else { + SDL_SetWindowFullscreen(window, 0); + } + } + break; if (event.key.keysym.scancode == SDL_SCANCODE_O) { if (event.key.keysym.mod & MODIFIER) { char *filename = do_open_rom_dialog(); From 7929573dc170670a6cf1e43b6d11483d8c640920 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 1 Jan 2020 00:17:54 +0200 Subject: [PATCH 119/341] Refinements to the last commit --- SDL/gui.c | 2 +- SDL/main.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/SDL/gui.c b/SDL/gui.c index 1664e74..db72c4d 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1019,8 +1019,8 @@ void run_gui(bool is_running) else { SDL_SetWindowFullscreen(window, 0); } + update_viewport(); } - break; if (event.key.keysym.scancode == SDL_SCANCODE_O) { if (event.key.keysym.mod & MODIFIER) { char *filename = do_open_rom_dialog(); diff --git a/SDL/main.c b/SDL/main.c index a095064..51e7c1a 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -274,6 +274,7 @@ static void handle_events(GB_gameboy_t *gb) else { SDL_SetWindowFullscreen(window, 0); } + update_viewport(); } break; From 3882b1b4b9ab34361211cbf7840e2f11d888a16f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 1 Jan 2020 00:27:41 +0200 Subject: [PATCH 120/341] Fix Windows build, hopefully fix High DPI support on Windows 10 (fixes #202) --- Makefile | 6 +++--- SDL/main.c | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) mode change 100755 => 100644 SDL/main.c diff --git a/Makefile b/Makefile index 1c23610..4417528 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ GL_LDFLAGS := $(shell $(PKG_CONFIG) --libs gl || echo -lGL) endif ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows -Drandom=rand -LDFLAGS += -lmsvcrt -lcomdlg32 -lSDL2main -Wl,/MANIFESTFILE:NUL +LDFLAGS += -lmsvcrt -lcomdlg32 -luser32 -lSDL2main -Wl,/MANIFESTFILE:NUL SDL_LDFLAGS := -lSDL2 GL_LDFLAGS := -lopengl32 else @@ -291,11 +291,11 @@ endif # Windows version builds two, one with a conole and one without it $(BIN)/SDL/sameboy.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o -@$(MKDIR) -p $(dir $@) - $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:windows + $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) -Wl,/subsystem:windows $(BIN)/SDL/sameboy_debugger.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o -@$(MKDIR) -p $(dir $@) - $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:console + $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) -Wl,/subsystem:console ifneq ($(USE_WINDRES),) $(OBJ)/%.o: %.rc diff --git a/SDL/main.c b/SDL/main.c old mode 100755 new mode 100644 index 51e7c1a..96f4994 --- a/SDL/main.c +++ b/SDL/main.c @@ -10,6 +10,7 @@ #ifndef _WIN32 +#include #define AUDIO_FREQUENCY 96000 #else /* Windows (well, at least my VM) can't handle 96KHz sound well :( */ @@ -551,6 +552,9 @@ static bool get_arg_flag(const char *flag, int *argc, char **argv) int main(int argc, char **argv) { +#ifdef _WIN32 + SetProcessDPIAware(); +#endif #define str(x) #x #define xstr(x) str(x) fprintf(stderr, "SameBoy v" xstr(VERSION) "\n"); From e9f6667cf52fb55a935ef0620845e7b7ad795126 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 1 Jan 2020 22:57:24 +0200 Subject: [PATCH 121/341] Minor build cleanup --- Makefile | 29 +++++++++++++++++------------ QuickLook/exports.sym | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4417528..bd31875 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ endif # Use clang if it's available. ifeq ($(origin CC),default) ifneq (, $(shell which clang)) -CC := clang +CC := clang endif endif @@ -76,9 +76,11 @@ endif # Set compilation and linkage flags based on target, platform and configuration OPEN_DIALOG = OpenDialog/gtk.c +NULL := /dev/null ifeq ($(PLATFORM),windows32) OPEN_DIALOG = OpenDialog/windows.c +NULL := NUL endif ifeq ($(PLATFORM),Darwin) @@ -110,9 +112,9 @@ LDFLAGS += -lc -lm -ldl endif ifeq ($(PLATFORM),Darwin) -SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null) -CFLAGS += -F/Library/Frameworks -OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9 +SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> $(NULL)) +CFLAGS += -F/Library/Frameworks -mmacosx-version-min=10.9 +OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 GL_LDFLAGS := -framework OpenGL @@ -127,10 +129,16 @@ ifeq ($(CONF),debug) CFLAGS += -g else ifeq ($(CONF), release) CFLAGS += -O3 -DNDEBUG +STRIP := strip +ifeq ($(PLATFORM),Darwin) +LDFLAGS += -Wl,-exported_symbols_list,$(NULL) +STRIP := -@true +endif ifneq ($(PLATFORM),windows32) LDFLAGS += -flto CFLAGS += -flto endif + else $(error Invalid value for CONF: $(CONF). Use "debug", "release" or "native_release") endif @@ -247,7 +255,7 @@ $(BIN)/SameBoy.app/Contents/MacOS/SameBoy: $(CORE_OBJECTS) $(COCOA_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) -framework OpenGL -framework AudioUnit -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework IOKit ifeq ($(CONF), release) - strip $@ + $(STRIP) $@ endif $(BIN)/SameBoy.app/Contents/Resources/Base.lproj/%.nib: Cocoa/%.xib @@ -267,10 +275,7 @@ $(BIN)/SameBoy.qlgenerator: $(BIN)/SameBoy.qlgenerator/Contents/MacOS/SameBoyQL # once in the QL Generator. It should probably become a dylib instead. $(BIN)/SameBoy.qlgenerator/Contents/MacOS/SameBoyQL: $(CORE_OBJECTS) $(QUICKLOOK_OBJECTS) -@$(MKDIR) -p $(dir $@) - $(CC) $^ -o $@ $(LDFLAGS) -bundle -framework Cocoa -framework Quicklook -ifeq ($(CONF), release) - strip -u -r -s QuickLook/exports.sym $@ -endif + $(CC) $^ -o $@ $(LDFLAGS) -Wl,-exported_symbols_list,QuickLook/exports.sym -bundle -framework Cocoa -framework Quicklook # cgb_boot_fast.bin is not a standard boot ROM, we don't expect it to exist in the user-provided # boot ROM directory. @@ -285,7 +290,7 @@ $(BIN)/SDL/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) ifeq ($(CONF), release) - strip $@ + $(STRIP) $@ endif # Windows version builds two, one with a conole and one without it @@ -321,7 +326,7 @@ $(BIN)/tester/sameboy_tester: $(CORE_OBJECTS) $(TESTER_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) ifeq ($(CONF), release) - strip $@ + $(STRIP) $@ endif $(BIN)/tester/sameboy_tester.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) @@ -372,7 +377,7 @@ $(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.pb8 -@$(MKDIR) -p $(dir $@) rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp - dd if=$@.tmp2 of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304) + dd if=$@.tmp2 of=$@ count=1 bs=$(if $(findstring dmg,$@)$(findstring sgb,$@),256,2304) 2> $(NULL) @rm $@.tmp $@.tmp2 # Libretro Core (uses its own build system) diff --git a/QuickLook/exports.sym b/QuickLook/exports.sym index 778b203..f979687 100644 --- a/QuickLook/exports.sym +++ b/QuickLook/exports.sym @@ -1,3 +1,3 @@ _DeallocQuickLookGeneratorPluginType _QuickLookGeneratorQueryInterface -_QuickLookGeneratorPluginFactory \ No newline at end of file +_QuickLookGeneratorPluginFactory From 23c7fb28858677f6cef474fae754898c246de8b0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 1 Jan 2020 23:40:56 +0200 Subject: [PATCH 122/341] Update version, update copyright year --- Cocoa/Info.plist | 2 +- Cocoa/License.html | 2 +- LICENSE | 2 +- Makefile | 2 +- QuickLook/Info.plist | 2 +- Windows/resources.rc | Bin 1210 -> 1210 bytes 6 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cocoa/Info.plist b/Cocoa/Info.plist index d86b9fd..1c7bdb5 100644 --- a/Cocoa/Info.plist +++ b/Cocoa/Info.plist @@ -72,7 +72,7 @@ LSMinimumSystemVersion 10.9 NSHumanReadableCopyright - Copyright © 2015-2019 Lior Halphon + Copyright © 2015-2020 Lior Halphon NSMainNibFile MainMenu NSPrincipalClass diff --git a/Cocoa/License.html b/Cocoa/License.html index 49851fd..b21cf8d 100644 --- a/Cocoa/License.html +++ b/Cocoa/License.html @@ -30,7 +30,7 @@

SameBoy

MIT License

-

Copyright © 2015-2019 Lior Halphon

+

Copyright © 2015-2020 Lior Halphon

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/LICENSE b/LICENSE index 94966be..17619e9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2015-2019 Lior Halphon +Copyright (c) 2015-2020 Lior Halphon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile index bd31875..7c22375 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.12.2 +VERSION := 0.12.3 export VERSION CONF ?= debug diff --git a/QuickLook/Info.plist b/QuickLook/Info.plist index 9540214..2cff196 100644 --- a/QuickLook/Info.plist +++ b/QuickLook/Info.plist @@ -47,7 +47,7 @@ CFPlugInUnloadFunction NSHumanReadableCopyright - Copyright © 2015-2019 Lior Halphon + Copyright © 2015-2020 Lior Halphon QLNeedsToBeRunInMainThread QLPreviewHeight diff --git a/Windows/resources.rc b/Windows/resources.rc index ffa8b3b93a3b02e11c7283a4c4764b7e27b9321f..73c12139e68ff25a1cf03c451e05e25df9d3c05f 100644 GIT binary patch delta 16 XcmdnRxr=i{6ce)%gTdxlrdTEbD~beZ delta 16 XcmdnRxr=i{6ce){gT>}prdTEbE3yP| From 5a1812f237cc3c5501187728f5758949e6a930e5 Mon Sep 17 00:00:00 2001 From: Pixelnarium Date: Thu, 2 Jan 2020 10:50:55 +0100 Subject: [PATCH 123/341] fix SDL build --- SDL/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL/main.c b/SDL/main.c index 96f4994..1646d5c 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -10,9 +10,9 @@ #ifndef _WIN32 -#include #define AUDIO_FREQUENCY 96000 #else +#include /* Windows (well, at least my VM) can't handle 96KHz sound well :( */ /* felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq. From 95af00a7524fda0ba932f89f3aaf232fc20f56e7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 3 Jan 2020 21:11:45 +0200 Subject: [PATCH 124/341] speling is veri difikult --- Core/debugger.c | 2 +- Core/gb.h | 2 +- Core/sm83_cpu.c | 34 +++++++++++++++++----------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index df480f3..8b6d6cf 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -819,7 +819,7 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */ (gb->f & GB_CARRY_FLAG)? 'C' : '-', (gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-', - (gb->f & GB_SUBSTRACT_FLAG)? 'N' : '-', + (gb->f & GB_SUBTRACT_FLAG)? 'N' : '-', (gb->f & GB_ZERO_FLAG)? 'Z' : '-'); GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false)); GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false)); diff --git a/Core/gb.h b/Core/gb.h index a561111..c27b6c6 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -98,7 +98,7 @@ enum { enum { GB_CARRY_FLAG = 16, GB_HALF_CARRY_FLAG = 32, - GB_SUBSTRACT_FLAG = 64, + GB_SUBTRACT_FLAG = 64, GB_ZERO_FLAG = 128, }; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 77248b5..0009a69 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -318,7 +318,7 @@ static void inc_hr(GB_gameboy_t *gb, uint8_t opcode) uint8_t register_id; register_id = ((opcode >> 4) + 1) & 0x03; gb->registers[register_id] += 0x100; - gb->registers[GB_REGISTER_AF] &= ~(GB_SUBSTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); if ((gb->registers[register_id] & 0x0F00) == 0) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; @@ -334,7 +334,7 @@ static void dec_hr(GB_gameboy_t *gb, uint8_t opcode) register_id = ((opcode >> 4) + 1) & 0x03; gb->registers[register_id] -= 0x100; gb->registers[GB_REGISTER_AF] &= ~(GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); - gb->registers[GB_REGISTER_AF] |= GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_SUBTRACT_FLAG; if ((gb->registers[register_id] & 0x0F00) == 0xF00) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; @@ -396,7 +396,7 @@ static void add_hl_rr(GB_gameboy_t *gb, uint8_t opcode) register_id = (opcode >> 4) + 1; rr = gb->registers[register_id]; gb->registers[GB_REGISTER_HL] = hl + rr; - gb->registers[GB_REGISTER_AF] &= ~(GB_SUBSTRACT_FLAG | GB_CARRY_FLAG | GB_HALF_CARRY_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_SUBTRACT_FLAG | GB_CARRY_FLAG | GB_HALF_CARRY_FLAG); /* The meaning of the Half Carry flag is really hard to track -_- */ if (((hl & 0xFFF) + (rr & 0xFFF)) & 0x1000) { @@ -432,7 +432,7 @@ static void inc_lr(GB_gameboy_t *gb, uint8_t opcode) value = (gb->registers[register_id] & 0xFF) + 1; gb->registers[register_id] = (gb->registers[register_id] & 0xFF00) | value; - gb->registers[GB_REGISTER_AF] &= ~(GB_SUBSTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); if ((gb->registers[register_id] & 0x0F) == 0) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; @@ -452,7 +452,7 @@ static void dec_lr(GB_gameboy_t *gb, uint8_t opcode) gb->registers[register_id] = (gb->registers[register_id] & 0xFF00) | value; gb->registers[GB_REGISTER_AF] &= ~(GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); - gb->registers[GB_REGISTER_AF] |= GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_SUBTRACT_FLAG; if ((gb->registers[register_id] & 0x0F) == 0xF) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; @@ -533,7 +533,7 @@ static void daa(GB_gameboy_t *gb, uint8_t opcode) gb->registers[GB_REGISTER_AF] &= ~(0xFF00 | GB_ZERO_FLAG); - if (gb->registers[GB_REGISTER_AF] & GB_SUBSTRACT_FLAG) { + if (gb->registers[GB_REGISTER_AF] & GB_SUBTRACT_FLAG) { if (gb->registers[GB_REGISTER_AF] & GB_HALF_CARRY_FLAG) { result = (result - 0x06) & 0xFF; } @@ -567,19 +567,19 @@ static void daa(GB_gameboy_t *gb, uint8_t opcode) static void cpl(GB_gameboy_t *gb, uint8_t opcode) { gb->registers[GB_REGISTER_AF] ^= 0xFF00; - gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG | GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG; } static void scf(GB_gameboy_t *gb, uint8_t opcode) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; - gb->registers[GB_REGISTER_AF] &= ~(GB_HALF_CARRY_FLAG | GB_SUBSTRACT_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG); } static void ccf(GB_gameboy_t *gb, uint8_t opcode) { gb->registers[GB_REGISTER_AF] ^= GB_CARRY_FLAG; - gb->registers[GB_REGISTER_AF] &= ~(GB_HALF_CARRY_FLAG | GB_SUBSTRACT_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_HALF_CARRY_FLAG | GB_SUBTRACT_FLAG); } static void ld_dhli_a(GB_gameboy_t *gb, uint8_t opcode) @@ -610,7 +610,7 @@ static void inc_dhl(GB_gameboy_t *gb, uint8_t opcode) value = cycle_read(gb, gb->registers[GB_REGISTER_HL]) + 1; cycle_write(gb, gb->registers[GB_REGISTER_HL], value); - gb->registers[GB_REGISTER_AF] &= ~(GB_SUBSTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); + gb->registers[GB_REGISTER_AF] &= ~(GB_SUBTRACT_FLAG | GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); if ((value & 0x0F) == 0) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } @@ -627,7 +627,7 @@ static void dec_dhl(GB_gameboy_t *gb, uint8_t opcode) cycle_write(gb, gb->registers[GB_REGISTER_HL], value); gb->registers[GB_REGISTER_AF] &= ~( GB_ZERO_FLAG | GB_HALF_CARRY_FLAG); - gb->registers[GB_REGISTER_AF] |= GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_SUBTRACT_FLAG; if ((value & 0x0F) == 0x0F) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } @@ -763,7 +763,7 @@ static void sub_a_r(GB_gameboy_t *gb, uint8_t opcode) uint8_t value, a; value = get_src_value(gb, opcode); a = gb->registers[GB_REGISTER_AF] >> 8; - gb->registers[GB_REGISTER_AF] = ((a - value) << 8) | GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] = ((a - value) << 8) | GB_SUBTRACT_FLAG; if (a == value) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; } @@ -781,7 +781,7 @@ static void sbc_a_r(GB_gameboy_t *gb, uint8_t opcode) value = get_src_value(gb, opcode); a = gb->registers[GB_REGISTER_AF] >> 8; carry = (gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG) != 0; - gb->registers[GB_REGISTER_AF] = ((a - value - carry) << 8) | GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] = ((a - value - carry) << 8) | GB_SUBTRACT_FLAG; if ((uint8_t) (a - value - carry) == 0) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; @@ -833,7 +833,7 @@ static void cp_a_r(GB_gameboy_t *gb, uint8_t opcode) value = get_src_value(gb, opcode); a = gb->registers[GB_REGISTER_AF] >> 8; gb->registers[GB_REGISTER_AF] &= 0xFF00; - gb->registers[GB_REGISTER_AF] |= GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_SUBTRACT_FLAG; if (a == value) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; } @@ -962,7 +962,7 @@ static void sub_a_d8(GB_gameboy_t *gb, uint8_t opcode) uint8_t value, a; value = cycle_read_inc_oam_bug(gb, gb->pc++); a = gb->registers[GB_REGISTER_AF] >> 8; - gb->registers[GB_REGISTER_AF] = ((a - value) << 8) | GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] = ((a - value) << 8) | GB_SUBTRACT_FLAG; if (a == value) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; } @@ -980,7 +980,7 @@ static void sbc_a_d8(GB_gameboy_t *gb, uint8_t opcode) value = cycle_read_inc_oam_bug(gb, gb->pc++); a = gb->registers[GB_REGISTER_AF] >> 8; carry = (gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG) != 0; - gb->registers[GB_REGISTER_AF] = ((a - value - carry) << 8) | GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] = ((a - value - carry) << 8) | GB_SUBTRACT_FLAG; if ((uint8_t) (a - value - carry) == 0) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; @@ -1032,7 +1032,7 @@ static void cp_a_d8(GB_gameboy_t *gb, uint8_t opcode) value = cycle_read_inc_oam_bug(gb, gb->pc++); a = gb->registers[GB_REGISTER_AF] >> 8; gb->registers[GB_REGISTER_AF] &= 0xFF00; - gb->registers[GB_REGISTER_AF] |= GB_SUBSTRACT_FLAG; + gb->registers[GB_REGISTER_AF] |= GB_SUBTRACT_FLAG; if (a == value) { gb->registers[GB_REGISTER_AF] |= GB_ZERO_FLAG; } From 163a5ea20c061fe66452017ef8f1b613af3f352f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Jan 2020 14:19:11 +0200 Subject: [PATCH 125/341] Add DMG color palettes (Cocoa) --- Cocoa/Document.m | 28 ++++++++++++++++++++ Cocoa/GBPreferencesWindow.h | 1 + Cocoa/GBPreferencesWindow.m | 21 +++++++++++++++ Cocoa/Preferences.xib | 43 ++++++++++++++++++++++++++----- Core/display.c | 14 +++++++--- Core/gb.c | 51 ++++++++++++++++++++++--------------- Core/gb.h | 15 ++++++++++- 7 files changed, 143 insertions(+), 30 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 85dd728..0c40776 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -184,6 +184,27 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } } +- (void) updatePalette +{ + switch ([[NSUserDefaults standardUserDefaults] integerForKey:@"GBColorPalette"]) { + case 1: + GB_set_palette(&gb, &GB_PALETTE_DMG); + break; + + case 2: + GB_set_palette(&gb, &GB_PALETTE_MGB); + break; + + case 3: + GB_set_palette(&gb, &GB_PALETTE_GBL); + break; + + default: + GB_set_palette(&gb, &GB_PALETTE_GREY); + break; + } +} + - (void) initCommon { GB_init(&gb, [self internalModel]); @@ -193,6 +214,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_set_input_callback(&gb, (GB_input_callback_t) consoleInput); GB_set_async_input_callback(&gb, (GB_input_callback_t) asyncConsoleInput); GB_set_color_correction_mode(&gb, (GB_color_correction_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBColorCorrection"]); + [self updatePalette]; GB_set_rgb_encode_callback(&gb, rgbEncode); GB_set_camera_get_pixel_callback(&gb, cameraGetPixel); GB_set_camera_update_request_callback(&gb, cameraRequestUpdate); @@ -452,6 +474,12 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) name:@"GBColorCorrectionChanged" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updatePalette) + name:@"GBColorPaletteChanged" + object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRewindLength) name:@"GBRewindLengthChanged" diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 90eee54..b34aafa 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -7,6 +7,7 @@ @property (strong) IBOutlet NSButton *aspectRatioCheckbox; @property (strong) IBOutlet NSPopUpButton *highpassFilterPopupButton; @property (strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton; +@property (nonatomic, strong) IBOutlet NSPopUpButton *colorPalettePopupButton; @property (strong) IBOutlet NSPopUpButton *rewindPopupButton; @property (strong) IBOutlet NSButton *configureJoypadButton; @property (strong) IBOutlet NSButton *skipButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index ecf0311..8d3e73d 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -14,6 +14,7 @@ NSPopUpButton *_graphicsFilterPopupButton; NSPopUpButton *_highpassFilterPopupButton; NSPopUpButton *_colorCorrectionPopupButton; + NSPopUpButton *_colorPalettePopupButton; NSPopUpButton *_rewindPopupButton; NSButton *_aspectRatioCheckbox; NSEventModifierFlags previousModifiers; @@ -84,6 +85,18 @@ return _colorCorrectionPopupButton; } +- (void)setColorPalettePopupButton:(NSPopUpButton *)colorPalettePopupButton +{ + _colorPalettePopupButton = colorPalettePopupButton; + NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBColorPalette"]; + [_colorPalettePopupButton selectItemAtIndex:mode]; +} + +- (NSPopUpButton *)colorPalettePopupButton +{ + return _colorPalettePopupButton; +} + - (void)setRewindPopupButton:(NSPopUpButton *)rewindPopupButton { _rewindPopupButton = rewindPopupButton; @@ -199,6 +212,14 @@ } +- (IBAction)colorPaletteChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) + forKey:@"GBColorPalette"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorPaletteChanged" object:nil]; + +} + - (IBAction)rewindLengthChanged:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@([sender selectedTag]) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 8278ee1..2c05a64 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -63,6 +63,7 @@ + @@ -78,11 +79,11 @@ - + - + @@ -91,7 +92,7 @@ - + @@ -127,7 +128,7 @@ - + @@ -136,7 +137,7 @@ - + @@ -156,6 +157,36 @@ + + + + + + + + + + + + + + + +

+ + + + + + + + + + + + + + - + diff --git a/Core/display.c b/Core/display.c index 5c1935c..f0e2ff2 100644 --- a/Core/display.c +++ b/Core/display.c @@ -142,9 +142,17 @@ static void display_vblank(GB_gameboy_t *gb) } } else { - uint32_t color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped && GB_is_cgb(gb) ? - gb->rgb_encode_callback(gb, 0, 0, 0) : - gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); + uint32_t color = 0; + if (GB_is_cgb(gb)) { + color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? + gb->rgb_encode_callback(gb, 0, 0, 0) : + gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); + } + else { + color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? + gb->background_palettes_rgb[3] : + gb->background_palettes_rgb[4]; + } for (unsigned i = 0; i < WIDTH * LINES; i++) { gb ->screen[i] = color; } diff --git a/Core/gb.c b/Core/gb.c index f29d400..d019fc4 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -573,21 +573,41 @@ void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) #endif } +const GB_palette_t GB_PALETTE_GREY = {{{0x00, 0x00, 0x00}, {0x55, 0x55, 0x55}, {0xaa, 0xaa, 0xaa}, {0xff ,0xff, 0xff}, {0xff ,0xff, 0xff}}}; +const GB_palette_t GB_PALETTE_DMG = {{{0x08, 0x18, 0x10}, {0x39, 0x61, 0x39}, {0x84, 0xa5, 0x63}, {0xc6, 0xde, 0x8c}, {0xd2 ,0xe6 ,0xa6}}}; +const GB_palette_t GB_PALETTE_MGB = {{{0x07, 0x10, 0x0e}, {0x3a, 0x4c, 0x3a}, {0x81, 0x8d, 0x66}, {0xc2, 0xce, 0x93}, {0xcf, 0xda, 0xac}}}; +const GB_palette_t GB_PALETTE_GBL = {{{0x0a, 0x1c, 0x15}, {0x35, 0x78, 0x62}, {0x56, 0xb4, 0x95}, {0x7f, 0xe2, 0xc3}, {0x91, 0xea, 0xd0}}}; + +static void update_dmg_palette(GB_gameboy_t *gb) +{ + const GB_palette_t *palette = gb->dmg_palette ?: &GB_PALETTE_GREY; + if (gb->rgb_encode_callback && !GB_is_cgb(gb)) { + gb->sprite_palettes_rgb[4] = gb->sprite_palettes_rgb[0] = gb->background_palettes_rgb[0] = + gb->rgb_encode_callback(gb, palette->colors[3].r, palette->colors[3].g, palette->colors[3].b); + gb->sprite_palettes_rgb[5] = gb->sprite_palettes_rgb[1] = gb->background_palettes_rgb[1] = + gb->rgb_encode_callback(gb, palette->colors[2].r, palette->colors[2].g, palette->colors[2].b); + gb->sprite_palettes_rgb[6] = gb->sprite_palettes_rgb[2] = gb->background_palettes_rgb[2] = + gb->rgb_encode_callback(gb, palette->colors[1].r, palette->colors[1].g, palette->colors[1].b); + gb->sprite_palettes_rgb[7] = gb->sprite_palettes_rgb[3] = gb->background_palettes_rgb[3] = + gb->rgb_encode_callback(gb, palette->colors[0].r, palette->colors[0].g, palette->colors[0].b); + + // LCD off color + gb->background_palettes_rgb[4] = + gb->rgb_encode_callback(gb, palette->colors[4].r, palette->colors[4].g, palette->colors[4].b); + } +} + +void GB_set_palette(GB_gameboy_t *gb, const GB_palette_t *palette) +{ + gb->dmg_palette = palette; + update_dmg_palette(gb); +} void GB_set_rgb_encode_callback(GB_gameboy_t *gb, GB_rgb_encode_callback_t callback) { - if (!gb->rgb_encode_callback && !GB_is_cgb(gb)) { - gb->sprite_palettes_rgb[4] = gb->sprite_palettes_rgb[0] = gb->background_palettes_rgb[0] = - callback(gb, 0xFF, 0xFF, 0xFF); - gb->sprite_palettes_rgb[5] = gb->sprite_palettes_rgb[1] = gb->background_palettes_rgb[1] = - callback(gb, 0xAA, 0xAA, 0xAA); - gb->sprite_palettes_rgb[6] = gb->sprite_palettes_rgb[2] = gb->background_palettes_rgb[2] = - callback(gb, 0x55, 0x55, 0x55); - gb->sprite_palettes_rgb[7] = gb->sprite_palettes_rgb[3] = gb->background_palettes_rgb[3] = - callback(gb, 0, 0, 0); - } gb->rgb_encode_callback = callback; + update_dmg_palette(gb); for (unsigned i = 0; i < 32; i++) { GB_palette_changed(gb, true, i * 2); @@ -882,16 +902,7 @@ void GB_reset(GB_gameboy_t *gb) gb->vram_size = 0x2000; memset(gb->vram, 0, gb->vram_size); - if (gb->rgb_encode_callback) { - gb->sprite_palettes_rgb[4] = gb->sprite_palettes_rgb[0] = gb->background_palettes_rgb[0] = - gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); - gb->sprite_palettes_rgb[5] = gb->sprite_palettes_rgb[1] = gb->background_palettes_rgb[1] = - gb->rgb_encode_callback(gb, 0xAA, 0xAA, 0xAA); - gb->sprite_palettes_rgb[6] = gb->sprite_palettes_rgb[2] = gb->background_palettes_rgb[2] = - gb->rgb_encode_callback(gb, 0x55, 0x55, 0x55); - gb->sprite_palettes_rgb[7] = gb->sprite_palettes_rgb[3] = gb->background_palettes_rgb[3] = - gb->rgb_encode_callback(gb, 0, 0, 0); - } + update_dmg_palette(gb); } reset_ram(gb); diff --git a/Core/gb.h b/Core/gb.h index c27b6c6..7831cb7 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -50,6 +50,17 @@ #error Unable to detect endianess #endif +typedef struct { + struct { + uint8_t r,g,b; + } colors[5]; +} GB_palette_t; + +extern const GB_palette_t GB_PALETTE_GREY; +extern const GB_palette_t GB_PALETTE_DMG; +extern const GB_palette_t GB_PALETTE_MGB; +extern const GB_palette_t GB_PALETTE_GBL; + typedef union { struct { uint8_t seconds; @@ -61,7 +72,6 @@ typedef union { uint8_t data[5]; } GB_rtc_time_t; - typedef enum { // GB_MODEL_DMG_0 = 0x000, // GB_MODEL_DMG_A = 0x001, @@ -513,6 +523,7 @@ struct GB_gameboy_internal_s { uint32_t *screen; uint32_t background_palettes_rgb[0x20]; uint32_t sprite_palettes_rgb[0x20]; + const GB_palette_t *dmg_palette; GB_color_correction_mode_t color_correction_mode; bool keys[4][GB_KEY_MAX]; @@ -696,6 +707,8 @@ void GB_set_infrared_callback(GB_gameboy_t *gb, GB_infrared_callback_t callback) void GB_set_rumble_callback(GB_gameboy_t *gb, GB_rumble_callback_t callback); void GB_set_update_input_hint_callback(GB_gameboy_t *gb, GB_update_input_hint_callback_t callback); +void GB_set_palette(GB_gameboy_t *gb, const GB_palette_t *palette); + /* These APIs are used when using internal clock */ void GB_set_serial_transfer_bit_start_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_start_callback_t callback); void GB_set_serial_transfer_bit_end_callback(GB_gameboy_t *gb, GB_serial_transfer_bit_end_callback_t callback); From 046b09052cd54562de710c1577df05e18713800a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Jan 2020 15:36:19 +0200 Subject: [PATCH 126/341] Add DMG color palettes (SDL), add scrolling to SDL menus --- SDL/gui.c | 81 +++++++++++++++++++++++++++++++++++++++++------------- SDL/gui.h | 3 ++ SDL/main.c | 22 +++++++++++++++ 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index db72c4d..d476175 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -175,8 +175,10 @@ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigne } } +static unsigned scroll = 0; static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, unsigned y, const char *string, uint32_t color) { + y -= scroll; unsigned orig_x = x; while (*string) { if (*string == '\n') { @@ -297,6 +299,7 @@ static void return_to_root_menu(unsigned index) { current_menu = root_menu; current_selection = 0; + scroll = 0; } static void cycle_model(unsigned index) @@ -407,6 +410,7 @@ static void enter_emulation_menu(unsigned index) { current_menu = emulation_menu; current_selection = 0; + scroll = 0; } const char *current_scaling_mode(unsigned index) @@ -421,6 +425,12 @@ const char *current_color_correction_mode(unsigned index) [configuration.color_correction_mode]; } +const char *current_palette(unsigned index) +{ + return (const char *[]){"Greyscale", "Lime (Game Boy)", "Olive (Pocket)", "Teal (Light)"} + [configuration.dmg_palette]; +} + void cycle_scaling(unsigned index) { configuration.scaling_mode++; @@ -463,6 +473,26 @@ static void cycle_color_correction_backwards(unsigned index) } } +static void cycle_palette(unsigned index) +{ + if (configuration.dmg_palette == 3) { + configuration.dmg_palette = 0; + } + else { + configuration.dmg_palette++; + } +} + +static void cycle_palette_backwards(unsigned index) +{ + if (configuration.dmg_palette == 0) { + configuration.dmg_palette = 3; + } + else { + configuration.dmg_palette--; + } +} + struct shader_name { const char *file_name; const char *display_name; @@ -556,6 +586,7 @@ static const struct menu_item graphics_menu[] = { {"Scaling Mode:", cycle_scaling, current_scaling_mode, cycle_scaling_backwards}, {"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards}, {"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards}, + {"Mono Palette:", cycle_palette, current_palette, cycle_palette_backwards}, {"Blend Frames:", toggle_blend_frames, blend_frames_string, toggle_blend_frames}, {"Back", return_to_root_menu}, {NULL,} @@ -565,6 +596,7 @@ static void enter_graphics_menu(unsigned index) { current_menu = graphics_menu; current_selection = 0; + scroll = 0; } const char *highpass_filter_string(unsigned index) @@ -601,6 +633,7 @@ static void enter_audio_menu(unsigned index) { current_menu = audio_menu; current_selection = 0; + scroll = 0; } static void modify_key(unsigned index) @@ -608,7 +641,6 @@ static void modify_key(unsigned index) gui_state = WAITING_FOR_KEY; } -static void enter_controls_menu_2(unsigned index); static const char *key_name(unsigned index); static const struct menu_item controls_menu[] = { @@ -620,12 +652,6 @@ static const struct menu_item controls_menu[] = { {"B:", modify_key, key_name,}, {"Select:", modify_key, key_name,}, {"Start:", modify_key, key_name,}, - {"Next Page", enter_controls_menu_2}, - {"Back", return_to_root_menu}, - {NULL,} -}; - -static const struct menu_item controls_menu_2[] = { {"Turbo:", modify_key, key_name,}, {"Rewind:", modify_key, key_name,}, {"Slow-Motion:", modify_key, key_name,}, @@ -635,11 +661,11 @@ static const struct menu_item controls_menu_2[] = { static const char *key_name(unsigned index) { - if (current_menu == controls_menu_2) { - if (index == 0) { + if (index >= 8) { + if (index == 8) { return SDL_GetScancodeName(configuration.keys[8]); } - return SDL_GetScancodeName(configuration.keys_2[index - 1]); + return SDL_GetScancodeName(configuration.keys_2[index - 9]); } return SDL_GetScancodeName(configuration.keys[index]); } @@ -648,12 +674,7 @@ static void enter_controls_menu(unsigned index) { current_menu = controls_menu; current_selection = 0; -} - -static void enter_controls_menu_2(unsigned index) -{ - current_menu = controls_menu_2; - current_selection = 0; + scroll = 0; } static unsigned joypad_index = 0; @@ -744,6 +765,7 @@ static void enter_joypad_menu(unsigned index) { current_menu = joypad_menu; current_selection = 0; + scroll = 0; } joypad_button_t get_joypad_button(uint8_t physical_button) @@ -826,6 +848,7 @@ void run_gui(bool is_running) bool should_render = true; current_menu = root_menu = is_running? paused_menu : nonpaused_menu; current_selection = 0; + scroll = 0; do { /* Convert Joypad and mouse events (We only generate down events) */ if (gui_state != WAITING_FOR_KEY && gui_state != WAITING_FOR_JBUTTON) { @@ -850,6 +873,7 @@ void run_gui(bool is_running) y = y * 8 / 7; y -= 144 / 16; } + y += scroll; if (x < 0 || x >= 160 || y < 24) { continue; @@ -1058,6 +1082,7 @@ void run_gui(bool is_running) gui_state = SHOWING_DROP_MESSAGE; } current_selection = 0; + scroll = 0; current_menu = root_menu; should_render = true; } @@ -1106,12 +1131,12 @@ void run_gui(bool is_running) should_render = true; } else if (gui_state == WAITING_FOR_KEY) { - if (current_menu == controls_menu_2) { - if (current_selection == 0) { + if (current_selection >= 8) { + if (current_selection == 8) { configuration.keys[8] = event.key.keysym.scancode; } else { - configuration.keys_2[current_selection - 1] = event.key.keysym.scancode; + configuration.keys_2[current_selection - 9] = event.key.keysym.scancode; } } else { @@ -1125,6 +1150,7 @@ void run_gui(bool is_running) if (should_render) { should_render = false; + rerender: if (width == 160 && height == 144) { memcpy(pixels, converted_background->pixels, sizeof(pixels)); } @@ -1144,6 +1170,16 @@ void run_gui(bool is_running) draw_text_centered(pixels, width, height, 8 + y_offset, "SameBoy", gui_palette_native[3], gui_palette_native[0], false); unsigned i = 0, y = 24; for (const struct menu_item *item = current_menu; item->string; item++, i++) { + if (i == current_selection) { + if (y < scroll) { + scroll = y - 4; + goto rerender; + } + } + if (i == current_selection && i == 0 && scroll != 0) { + scroll = 0; + goto rerender; + } if (item->value_getter && !item->backwards_handler) { char line[25]; snprintf(line, sizeof(line), "%s%*s", item->string, 24 - (int)strlen(item->string), item->value_getter(i)); @@ -1162,6 +1198,13 @@ void run_gui(bool is_running) y += 12; } } + if (i == current_selection) { + if (y > scroll + 144) { + scroll = y - 144; + goto rerender; + } + } + } break; case SHOWING_HELP: diff --git a/SDL/gui.h b/SDL/gui.h index b22c74f..ccfdfb9 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -100,6 +100,9 @@ typedef struct { SGB_2, SGB_MAX } sgb_revision; + + /* v0.13 */ + uint8_t dmg_palette; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 1646d5c..e83bfd8 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -92,6 +92,26 @@ static const char *end_capturing_logs(bool show_popup, bool should_exit) return captured_log; } +static void update_palette(void) +{ + switch (configuration.dmg_palette) { + case 1: + GB_set_palette(&gb, &GB_PALETTE_DMG); + break; + + case 2: + GB_set_palette(&gb, &GB_PALETTE_MGB); + break; + + case 3: + GB_set_palette(&gb, &GB_PALETTE_GBL); + break; + + default: + GB_set_palette(&gb, &GB_PALETTE_GREY); + } +} + static void open_menu(void) { bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; @@ -105,6 +125,7 @@ static void open_menu(void) SDL_PauseAudioDevice(device_id, 0); } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); + update_palette(); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); } @@ -454,6 +475,7 @@ restart: GB_set_rgb_encode_callback(&gb, rgb_encode); GB_set_sample_rate(&gb, have_aspec.freq); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); + update_palette(); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); GB_set_rewind_length(&gb, configuration.rewind_length); GB_set_update_input_hint_callback(&gb, handle_events); From 99d2c0258c4da19b6b7a135bd9d0472388c79c33 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Jan 2020 15:51:53 +0200 Subject: [PATCH 127/341] Add monochrome LCD shader --- Cocoa/GBPreferencesWindow.m | 1 + Cocoa/Preferences.xib | 15 ++++++++------- SDL/gui.c | 1 + Shaders/MonoLCD.fsh | 38 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 Shaders/MonoLCD.fsh diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 8d3e73d..5c5d375 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -32,6 +32,7 @@ @"NearestNeighbor", @"Bilinear", @"SmoothBilinear", + @"MonoLCD", @"LCD", @"CRT", @"Scale2x", diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 2c05a64..1062dae 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -1,8 +1,8 @@ - + - + @@ -17,7 +17,7 @@ - + @@ -104,7 +104,8 @@ - + + @@ -428,7 +429,7 @@ - + @@ -472,11 +473,11 @@ - - + - + - + - + - + - + - - - - + + + + @@ -129,16 +130,16 @@ - + - + - + @@ -148,9 +149,9 @@ - - - + + + @@ -159,16 +160,16 @@ - + - + - + @@ -188,10 +189,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -233,7 +263,7 @@ - + @@ -251,12 +281,12 @@ - + - + @@ -275,7 +305,7 @@ - + @@ -303,7 +333,7 @@ - + @@ -333,7 +363,7 @@ - + @@ -371,16 +401,16 @@ - + - + - - + + @@ -391,7 +421,7 @@ - + @@ -429,7 +459,7 @@ - + diff --git a/Core/apu.h b/Core/apu.h index 011412e..933f14e 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -162,6 +162,7 @@ void GB_apu_div_event(GB_gameboy_t *gb); void GB_apu_init(GB_gameboy_t *gb); void GB_apu_run(GB_gameboy_t *gb); void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb); +void GB_borrow_sgb_border(GB_gameboy_t *gb); #endif #endif /* apu_h */ diff --git a/Core/display.c b/Core/display.c index 7f3d23e..b7ddee1 100644 --- a/Core/display.c +++ b/Core/display.c @@ -99,6 +99,8 @@ static void fifo_overlay_object_row(GB_fifo_t *fifo, uint8_t lower, uint8_t uppe #define LINE_LENGTH (456) #define LINES (144) #define WIDTH (160) +#define BORDERED_WIDTH 256 +#define BORDERED_HEIGHT 224 #define FRAME_LENGTH (LCDC_PERIOD) #define VIRTUAL_LINES (FRAME_LENGTH / LINE_LENGTH) // = 154 @@ -134,7 +136,7 @@ static void display_vblank(GB_gameboy_t *gb) } } - if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { + if ((!gb->disable_rendering || gb->sgb) && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ if (gb->sgb) { for (unsigned i = 0; i < WIDTH * LINES; i++) { @@ -153,13 +155,70 @@ static void display_vblank(GB_gameboy_t *gb) gb->background_palettes_rgb[3] : gb->background_palettes_rgb[4]; } - for (unsigned i = 0; i < WIDTH * LINES; i++) { - gb ->screen[i] = color; + if (gb->border_mode == GB_BORDER_ALWAYS) { + for (unsigned y = 0; y < LINES; y++) { + for (unsigned x = 0; x < WIDTH; x++) { + gb ->screen[x + y * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH] = color; + } + } + } + else { + for (unsigned i = 0; i < WIDTH * LINES; i++) { + gb ->screen[i] = color; + } + } + } + } + + if (gb->border_mode == GB_BORDER_ALWAYS && !GB_is_sgb(gb)) { + GB_borrow_sgb_border(gb); + uint32_t border_colors[16 * 4]; + + if (!gb->has_sgb_border && GB_is_cgb(gb) && gb->model != GB_MODEL_AGB) { + static uint16_t colors[] = { + 0x2095, 0x5129, 0x1EAF, 0x1EBA, 0x4648, + 0x30DA, 0x69AD, 0x2B57, 0x2B5D, 0x632C, + 0x1050, 0x3C84, 0x0E07, 0x0E18, 0x2964, + }; + unsigned index = gb->rom[0x14e] % 5; + gb->borrowed_border.palette[0] = colors[index]; + gb->borrowed_border.palette[10] = colors[5 + index]; + gb->borrowed_border.palette[14] = colors[10 + index]; + + } + + for (unsigned i = 0; i < 16 * 4; i++) { + border_colors[i] = GB_convert_rgb15(gb, gb->borrowed_border.palette[i], true); + } + + for (unsigned tile_y = 0; tile_y < 28; tile_y++) { + for (unsigned tile_x = 0; tile_x < 32; tile_x++) { + if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { + continue; + } + uint16_t tile = gb->borrowed_border.map[tile_x + tile_y * 32]; + uint8_t flip_x = (tile & 0x4000)? 0x7 : 0; + uint8_t flip_y = (tile & 0x8000)? 0x7 : 0; + uint8_t palette = (tile >> 10) & 3; + for (unsigned y = 0; y < 8; y++) { + for (unsigned x = 0; x < 8; x++) { + uint8_t color = gb->borrowed_border.tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF; + uint32_t *output = gb->screen + tile_x * 8 + x + (tile_y * 8 + y) * 256; + if (color == 0) { + *output = border_colors[0]; + } + else { + *output = border_colors[color + palette * 16]; + } + } + } } } } - gb->vblank_callback(gb); + if (gb->vblank_callback) { + gb->vblank_callback(gb); + } GB_timing_sync(gb); } @@ -184,19 +243,19 @@ static inline uint8_t scale_channel_with_curve_sgb(uint8_t x) } -uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color) +uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border) { uint8_t r = (color) & 0x1F; uint8_t g = (color >> 5) & 0x1F; uint8_t b = (color >> 10) & 0x1F; - if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED) { + if (gb->color_correction_mode == GB_COLOR_CORRECTION_DISABLED || (for_border && !gb->has_sgb_border)) { r = scale_channel(r); g = scale_channel(g); b = scale_channel(b); } else { - if (GB_is_sgb(gb)) { + if (GB_is_sgb(gb) || for_border) { return gb->rgb_encode_callback(gb, scale_channel_with_curve_sgb(r), scale_channel_with_curve_sgb(g), @@ -253,7 +312,7 @@ void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index uint8_t *palette_data = background_palette? gb->background_palettes_data : gb->sprite_palettes_data; uint16_t color = palette_data[index & ~1] | (palette_data[index | 1] << 8); - (background_palette? gb->background_palettes_rgb : gb->sprite_palettes_rgb)[index / 2] = GB_convert_rgb15(gb, color); + (background_palette? gb->background_palettes_rgb : gb->sprite_palettes_rgb)[index / 2] = GB_convert_rgb15(gb, color, false); } void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t mode) @@ -393,7 +452,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } /* Drop pixels for scrollings */ - if (gb->position_in_line >= 160 || gb->disable_rendering) { + if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { gb->position_in_line++; return; } @@ -412,6 +471,16 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } uint8_t icd_pixel = 0; + uint32_t *dest = NULL; + if (!gb->sgb) { + if (gb->border_mode != GB_BORDER_ALWAYS) { + dest = gb->screen + gb->position_in_line + gb->current_line * WIDTH; + } + else { + dest = gb->screen + gb->position_in_line + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH; + } + } + { uint8_t pixel = bg_enabled? fifo_item->pixel : 0; if (pixel && bg_priority) { @@ -431,7 +500,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } } else { - gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel]; + *dest = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel]; } } @@ -453,7 +522,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } } else { - gb->screen[gb->position_in_line + gb->current_line * WIDTH] = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; + *dest = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; } } diff --git a/Core/display.h b/Core/display.h index 9d1d1b4..69e7905 100644 --- a/Core/display.h +++ b/Core/display.h @@ -49,6 +49,6 @@ typedef enum { void GB_draw_tileset(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index); void GB_draw_tilemap(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index, GB_map_type_t map_type, GB_tileset_type_t tileset_type); uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_height); -uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color); +uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border); void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t mode); #endif /* display_h */ diff --git a/Core/gb.c b/Core/gb.c index 4a9525c..b0eaf68 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -99,6 +99,42 @@ static char *default_async_input_callback(GB_gameboy_t *gb) } #endif +static void load_default_border(GB_gameboy_t *gb) +{ + if (gb->has_sgb_border) return; + + #define LOAD_BORDER() do { \ + memcpy(gb->borrowed_border.map, tilemap, sizeof(tilemap));\ + memcpy(gb->borrowed_border.palette, palette, sizeof(palette));\ + \ + /* Expand tileset */\ + for (unsigned tile = 0; tile < sizeof(tiles) / 32; tile++) {\ + for (unsigned y = 0; y < 8; y++) {\ + for (unsigned x = 0; x < 8; x++) {\ + gb->borrowed_border.tiles[tile * 8 * 8 + y * 8 + x] =\ + (tiles[tile * 32 + y * 2 + 0] & (1 << (7 ^ x)) ? 1 : 0) |\ + (tiles[tile * 32 + y * 2 + 1] & (1 << (7 ^ x)) ? 2 : 0) |\ + (tiles[tile * 32 + y * 2 + 16] & (1 << (7 ^ x)) ? 4 : 0) |\ + (tiles[tile * 32 + y * 2 + 17] & (1 << (7 ^ x)) ? 8 : 0);\ + }\ + }\ + }\ + } while(false); + + if (gb->model == GB_MODEL_AGB) { + #include "graphics/agb_border.inc" + LOAD_BORDER(); + } + else if (GB_is_cgb(gb)) { + #include "graphics/cgb_border.inc" + LOAD_BORDER(); + } + else { + #include "graphics/dmg_border.inc" + LOAD_BORDER(); + } +} + void GB_init(GB_gameboy_t *gb, GB_model_t model) { memset(gb, 0, sizeof(*gb)); @@ -125,6 +161,7 @@ void GB_init(GB_gameboy_t *gb, GB_model_t model) } GB_reset(gb); + load_default_border(gb); } GB_model_t GB_get_model(GB_gameboy_t *gb) @@ -184,6 +221,46 @@ void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, memcpy(gb->boot_rom, buffer, size); } +void GB_borrow_sgb_border(GB_gameboy_t *gb) +{ + if (GB_is_sgb(gb)) return; + if (gb->border_mode != GB_BORDER_ALWAYS) return; + if (gb->tried_loading_sgb_border) return; + gb->tried_loading_sgb_border = true; + if (gb->rom[0x146] != 3) return; // Not an SGB game, nothing to borrow + if (!gb->boot_rom_load_callback) return; // Can't borrow a border without this callback + GB_gameboy_t sgb; + GB_init(&sgb, GB_MODEL_SGB); + sgb.cartridge_type = gb->cartridge_type; + sgb.rom = gb->rom; + sgb.rom_size = gb->rom_size; + sgb.turbo = true; + sgb.turbo_dont_skip = true; + // sgb.disable_rendering = true; + + /* Load the boot ROM using the existing gb object */ + typeof(gb->boot_rom) boot_rom_backup; + memcpy(boot_rom_backup, gb->boot_rom, sizeof(gb->boot_rom)); + gb->boot_rom_load_callback(gb, GB_BOOT_ROM_SGB); + memcpy(sgb.boot_rom, gb->boot_rom, sizeof(gb->boot_rom)); + memcpy(gb->boot_rom, boot_rom_backup, sizeof(gb->boot_rom)); + sgb.sgb->intro_animation = -1; + + for (unsigned i = 600; i--;) { + GB_run_frame(&sgb); + if (sgb.sgb->border_animation) { + gb->has_sgb_border = true; + memcpy(&gb->borrowed_border, &sgb.sgb->pending_border, sizeof(gb->borrowed_border)); + gb->borrowed_border.palette[0] = sgb.sgb->effective_palettes[0]; + break; + } + } + + sgb.rom = NULL; + sgb.rom_size = 0; + GB_free(&sgb); +} + int GB_load_rom(GB_gameboy_t *gb, const char *path) { FILE *f = fopen(path, "rb"); @@ -199,6 +276,9 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) gb->rom_size |= gb->rom_size >> 1; gb->rom_size++; } + if (gb->rom_size == 0) { + gb->rom_size = 0x8000; + } fseek(f, 0, SEEK_SET); if (gb->rom) { free(gb->rom); @@ -208,7 +288,6 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) fread(gb->rom, 1, gb->rom_size, f); fclose(f); GB_configure_cart(gb); - return 0; } @@ -219,6 +298,9 @@ void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t siz gb->rom_size |= gb->rom_size >> 1; gb->rom_size++; } + if (gb->rom_size == 0) { + gb->rom_size = 0x8000; + } if (gb->rom) { free(gb->rom); } @@ -994,6 +1076,7 @@ void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model) } GB_rewind_free(gb); GB_reset(gb); + load_default_border(gb); } void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t *size, uint16_t *bank) @@ -1079,14 +1162,36 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb) return CPU_FREQUENCY * gb->clock_multiplier; } +void GB_set_border_mode(GB_gameboy_t *gb, GB_border_mode_t border_mode) +{ + if (gb->border_mode > GB_BORDER_ALWAYS) return; + gb->border_mode = border_mode; +} + unsigned GB_get_screen_width(GB_gameboy_t *gb) { - return GB_is_hle_sgb(gb)? 256 : 160; + switch (gb->border_mode) { + default: + case GB_BORDER_SGB: + return GB_is_hle_sgb(gb)? 256 : 160; + case GB_BORDER_NEVER: + return 160; + case GB_BORDER_ALWAYS: + return 256; + } } unsigned GB_get_screen_height(GB_gameboy_t *gb) { - return GB_is_hle_sgb(gb)? 224 : 144; + switch (gb->border_mode) { + default: + case GB_BORDER_SGB: + return GB_is_hle_sgb(gb)? 224 : 144; + case GB_BORDER_NEVER: + return 144; + case GB_BORDER_ALWAYS: + return 224; + } } unsigned GB_get_player_count(GB_gameboy_t *gb) diff --git a/Core/gb.h b/Core/gb.h index 487269f..e803f3c 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -112,6 +112,12 @@ enum { GB_ZERO_FLAG = 128, }; +typedef enum { + GB_BORDER_SGB, + GB_BORDER_NEVER, + GB_BORDER_ALWAYS, +} GB_border_mode_t; + #define GB_MAX_IR_QUEUE 256 enum { @@ -538,6 +544,10 @@ struct GB_gameboy_internal_s { const GB_palette_t *dmg_palette; GB_color_correction_mode_t color_correction_mode; bool keys[4][GB_KEY_MAX]; + GB_border_mode_t border_mode; + GB_sgb_border_t borrowed_border; + bool tried_loading_sgb_border; + bool has_sgb_border; /* Timing */ uint64_t last_sync; @@ -707,7 +717,8 @@ void GB_log(GB_gameboy_t *gb, const char *fmt, ...) __printflike(2, 3); void GB_attributed_log(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, ...) __printflike(3, 4); void GB_set_pixels_output(GB_gameboy_t *gb, uint32_t *output); - +void GB_set_border_mode(GB_gameboy_t *gb, GB_border_mode_t border_mode); + void GB_set_infrared_input(GB_gameboy_t *gb, bool state); void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, long cycles_after_previous_change); /* In 8MHz units*/ diff --git a/Core/graphics/agb_border.inc b/Core/graphics/agb_border.inc new file mode 100644 index 0000000..dd4ebbe --- /dev/null +++ b/Core/graphics/agb_border.inc @@ -0,0 +1,522 @@ +static const uint16_t palette[] = { + 0x410A, 0x0421, 0x35AD, 0x4A52, 0x7FFF, 0x2D49, 0x0C42, 0x1484, + 0x18A5, 0x20C6, 0x6718, 0x5D6E, 0x0000, 0x0000, 0x0000, 0x0000, +}; + +static const uint16_t tilemap[] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0001, 0x0002, 0x0003, 0x0003, 0x0003, 0x0004, 0x0004, + 0x0004, 0x0004, 0x0004, 0x0004, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004, 0x0004, 0x0004, + 0x0004, 0x0004, 0x0003, 0x0003, 0x0003, 0x4002, 0x4001, 0x0000, + 0x0000, 0x0006, 0x0007, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x4007, 0x4006, 0x0000, + 0x0000, 0x0009, 0x0008, 0x0008, 0x0008, 0x000A, 0x000B, 0x000B, + 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, + 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, 0x000B, + 0x000B, 0x000B, 0x400A, 0x0008, 0x0008, 0x0008, 0xC009, 0x0000, + 0x0000, 0x000C, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x400C, 0x0000, + 0x0000, 0x000E, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC00E, 0x0000, + 0x0000, 0x000F, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x400F, 0x0000, + 0x0000, 0x0010, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC010, 0x0000, + 0x0000, 0x0010, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC010, 0x0000, + 0x0000, 0x0011, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC011, 0x0000, + 0x0000, 0x0011, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC011, 0x0000, + 0x0000, 0x0012, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x4012, 0x0000, + 0x0000, 0x0013, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC013, 0x0000, + 0x0014, 0x0015, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x4015, 0x4014, + 0x0016, 0x0017, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC017, 0xC016, + 0x0016, 0x0017, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC017, 0xC016, + 0x0018, 0x0019, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x4019, 0x4018, + 0x001A, 0x001B, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0xC01B, 0xC01A, + 0x001C, 0x001D, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x401D, 0x401C, + 0x001E, 0x0008, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x0008, 0xC01E, + 0x001E, 0x0008, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x0008, 0xC01E, + 0x001E, 0x0008, 0x0008, 0x0008, 0x0008, 0x000D, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC00D, 0x0008, 0x0008, 0x0008, 0x0008, 0xC01E, + 0x001F, 0x801D, 0x0008, 0x0008, 0x0008, 0x0020, 0x0021, 0x0022, + 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, + 0x002B, 0x002C, 0x002D, 0x002D, 0x002D, 0x002D, 0x002D, 0x002D, + 0x002E, 0x0021, 0x4020, 0x0008, 0x0008, 0x0008, 0xC01D, 0x401F, + 0x002F, 0x0030, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0031, + 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, + 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 0x0040, 0x0041, + 0x0042, 0x0043, 0x0008, 0x0008, 0x0008, 0x0008, 0x4030, 0x402F, + 0x0044, 0x0045, 0x0046, 0x0047, 0x0008, 0x0008, 0x0048, 0x0049, + 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, + 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, + 0x005A, 0x005B, 0x0008, 0x0008, 0x4047, 0x4046, 0x4045, 0x4044, + 0x0000, 0x0000, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, + 0x0061, 0x0062, 0x0063, 0x0063, 0x0063, 0x0063, 0x0063, 0x0063, + 0x0063, 0x0063, 0x0063, 0x0063, 0x0063, 0x0063, 0x4062, 0x0061, + 0x0061, 0x4060, 0x405F, 0x405E, 0x405D, 0x405C, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, +}; + +const uint8_t tiles[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x1F, 0x01, + 0x7F, 0x1F, 0xFF, 0x7E, 0xFF, 0xE1, 0xFF, 0x9F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x1E, + 0x1F, 0x60, 0x7F, 0x80, 0xFF, 0x00, 0xBF, 0x40, + 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x03, 0x01, 0x07, 0x03, 0x07, 0x03, 0x07, 0x06, + 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, + 0x01, 0x02, 0x03, 0x04, 0x03, 0x04, 0x06, 0x01, + 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, + 0xFF, 0xBF, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xBF, 0x40, 0x7F, 0x80, 0x7F, 0x80, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x0F, 0x0E, 0x0F, 0x0E, 0x0F, 0x0E, 0x0F, 0x0E, + 0x0F, 0x0E, 0x0F, 0x0E, 0x0F, 0x0E, 0x0F, 0x0E, + 0x0E, 0x01, 0x0E, 0x01, 0x0E, 0x01, 0x0E, 0x01, + 0x0E, 0x01, 0x0E, 0x01, 0x0E, 0x01, 0x0E, 0x01, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFE, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, + 0x0F, 0x0E, 0x0F, 0x0E, 0x1F, 0x1B, 0x1F, 0x1B, + 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, + 0x0E, 0x01, 0x0E, 0x01, 0x1B, 0x04, 0x1B, 0x04, + 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, + 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, + 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, + 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, + 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, + 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, + 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, + 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, + 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, + 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, 0x1F, 0x1B, + 0x1F, 0x1B, 0x3F, 0x37, 0x3F, 0x37, 0x3F, 0x37, + 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, 0x1B, 0x04, + 0x1B, 0x04, 0x37, 0x08, 0x37, 0x08, 0x37, 0x08, + 0x3F, 0x37, 0x3F, 0x37, 0x3F, 0x37, 0x3F, 0x37, + 0x3F, 0x37, 0x3F, 0x37, 0x3F, 0x37, 0x3F, 0x37, + 0x37, 0x08, 0x37, 0x08, 0x37, 0x08, 0x37, 0x08, + 0x37, 0x08, 0x37, 0x08, 0x37, 0x08, 0x37, 0x08, + 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, + 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, + 0x6F, 0x10, 0x6F, 0x10, 0x6F, 0x10, 0x6F, 0x10, + 0x6F, 0x10, 0x6F, 0x10, 0x6F, 0x10, 0x6F, 0x10, + 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0xFF, 0xDF, + 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, + 0x6F, 0x10, 0x6F, 0x10, 0x6F, 0x10, 0xDF, 0x20, + 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, + 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, + 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, + 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, + 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, + 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xDF, + 0xFF, 0xDF, 0xFF, 0xDF, 0xFF, 0xBF, 0xFF, 0xBF, + 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, 0xDF, 0x20, + 0xDF, 0x20, 0xDF, 0x20, 0xBF, 0x40, 0xBF, 0x40, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0xFF, 0xBF, 0xFF, 0xBF, 0xFF, 0xBF, 0xFF, 0xBF, + 0xFF, 0xBF, 0xFF, 0xBF, 0xFF, 0xBF, 0xFF, 0xBF, + 0xBF, 0x40, 0xBF, 0x40, 0xBF, 0x40, 0xBF, 0x40, + 0xBF, 0x40, 0xBF, 0x40, 0xBF, 0x40, 0xBF, 0x40, + 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x01, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, + 0xFF, 0xBF, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0xBF, 0x40, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, + 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, + 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, + 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, + 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, + 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, + 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, + 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, 0x07, 0x06, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, 0x06, 0x01, + 0x03, 0x04, 0x03, 0x04, 0x03, 0x04, 0x03, 0x04, + 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFE, 0x01, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xF8, 0xFF, 0xE7, 0xF8, 0xDF, 0xE3, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xF8, 0x00, 0xE4, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x3F, 0xFF, 0xCE, 0x3F, 0xF5, 0x8E, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x3F, 0x00, 0x4E, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x1F, 0xFF, 0xEE, 0x1F, 0xB5, 0x4E, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x1F, 0x00, 0x0E, 0xA0, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x07, 0xFF, 0xFB, 0x07, 0x04, 0x73, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x07, 0x00, 0x03, 0x88, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x80, 0xFF, 0x7F, 0x80, 0x82, 0x39, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x80, 0x00, 0x01, 0x44, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x01, 0xFE, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x83, 0x7C, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x42, 0x01, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFE, 0xFF, 0xBB, 0x7C, 0x4F, 0xB0, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x7C, 0x00, 0xB1, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x07, 0xFF, 0xF9, 0x06, 0xE7, 0xF8, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x06, 0x00, 0x08, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x0E, 0xFF, 0xF5, 0x0E, 0x9B, 0x74, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x0E, 0x00, 0x94, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x1F, 0xFF, 0xF7, 0x0F, 0xBF, 0x47, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x0F, 0x00, 0xA7, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, + 0x07, 0x07, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, + 0x03, 0x04, 0x03, 0x04, 0x03, 0x04, 0x03, 0x04, + 0x03, 0x04, 0x01, 0x02, 0x01, 0x02, 0x00, 0x01, + 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, + 0xFF, 0x7F, 0xFF, 0xBF, 0xFF, 0xBF, 0xFF, 0xDF, + 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, 0x7F, 0x80, + 0x7F, 0x80, 0xBF, 0x40, 0xBF, 0x40, 0xDF, 0x20, + 0xB0, 0xD8, 0xA0, 0xD3, 0x67, 0x84, 0x47, 0xA4, + 0x61, 0x81, 0xA0, 0xD0, 0xB4, 0xCA, 0x7E, 0x81, + 0xD7, 0x08, 0xCC, 0x13, 0x98, 0x20, 0x98, 0x00, + 0x9E, 0x20, 0xCF, 0x00, 0xCD, 0x02, 0x80, 0x01, + 0x32, 0x2D, 0x13, 0x6D, 0x34, 0x48, 0xFC, 0x02, + 0x7C, 0x00, 0x78, 0x05, 0x30, 0x49, 0x20, 0x50, + 0xCD, 0x00, 0xAC, 0x40, 0x49, 0x82, 0x01, 0x02, + 0x07, 0x80, 0xC2, 0x05, 0x86, 0x41, 0x9F, 0x40, + 0x15, 0x2E, 0x09, 0x06, 0x09, 0x16, 0x0B, 0xD4, + 0xC6, 0x49, 0x8E, 0x40, 0xCF, 0xC8, 0x06, 0x01, + 0xCE, 0x20, 0xE6, 0x10, 0xE6, 0x00, 0x24, 0xD0, + 0x39, 0x80, 0x38, 0x01, 0x31, 0x00, 0xF8, 0x00, + 0x0C, 0x8B, 0x85, 0x8A, 0x03, 0x84, 0x27, 0x20, + 0x22, 0x35, 0x12, 0x34, 0x20, 0x12, 0x10, 0x20, + 0x73, 0x00, 0x72, 0x08, 0x7C, 0x80, 0xDC, 0x01, + 0xC8, 0x11, 0xC9, 0x06, 0xCD, 0x22, 0xEF, 0x10, + 0x83, 0x44, 0x86, 0x01, 0x03, 0x85, 0x26, 0x21, + 0x46, 0x69, 0x46, 0x68, 0x8E, 0xCA, 0x86, 0x88, + 0x39, 0x40, 0x78, 0x84, 0x7C, 0x80, 0xD8, 0x01, + 0x90, 0x29, 0xD1, 0x28, 0x73, 0x00, 0xB3, 0x40, + 0x00, 0x01, 0x01, 0x00, 0x3F, 0x00, 0x3F, 0x40, + 0x03, 0x02, 0x01, 0x02, 0x41, 0x7C, 0x7F, 0x00, + 0xFE, 0x00, 0xFF, 0x00, 0xC0, 0x00, 0x80, 0x40, + 0xFC, 0x00, 0xFC, 0x00, 0x80, 0x02, 0xC0, 0x00, + 0xC0, 0x00, 0x80, 0x4C, 0xCC, 0x43, 0x8E, 0x52, + 0x80, 0x4C, 0x80, 0x00, 0x12, 0x1E, 0x9E, 0x00, + 0x7F, 0x00, 0x33, 0x0C, 0x32, 0x01, 0x23, 0x50, + 0x33, 0x4C, 0x7F, 0x00, 0x61, 0x80, 0xF1, 0x00, + 0x7C, 0x02, 0x30, 0x48, 0x31, 0x40, 0x61, 0x50, + 0x87, 0xE4, 0xE3, 0x84, 0x23, 0x44, 0x43, 0x44, + 0x85, 0x42, 0x87, 0x40, 0x8F, 0x50, 0x8C, 0x12, + 0x78, 0x00, 0x18, 0x20, 0xB8, 0x00, 0x98, 0x24, + 0x03, 0x04, 0x03, 0xE0, 0xF1, 0x12, 0xF0, 0x09, + 0xF9, 0x09, 0xF9, 0x08, 0xE1, 0x12, 0xF1, 0x12, + 0xF8, 0x00, 0x1E, 0xE0, 0x0C, 0x02, 0x07, 0x08, + 0x07, 0x00, 0x06, 0x00, 0x1C, 0x02, 0x0C, 0x00, + 0x9F, 0x91, 0x86, 0x88, 0xC4, 0x4C, 0x80, 0x4C, + 0xE1, 0x20, 0xC1, 0x22, 0x23, 0xD4, 0x22, 0xD5, + 0x60, 0x00, 0xFB, 0x00, 0x37, 0x00, 0x73, 0x0C, + 0x1F, 0x00, 0x3C, 0x00, 0xC8, 0x14, 0xC9, 0x14, + 0x16, 0x2F, 0x76, 0x4F, 0x2D, 0xDE, 0xDD, 0xBE, + 0xBA, 0x7D, 0x7A, 0xFD, 0x7A, 0xFD, 0xF4, 0xF8, + 0xCF, 0x00, 0x8F, 0x00, 0x5E, 0x80, 0xBE, 0x00, + 0x7D, 0x00, 0xFC, 0x00, 0xFC, 0x01, 0xF9, 0x02, + 0xFF, 0x00, 0xBF, 0x78, 0x86, 0x09, 0x86, 0x89, + 0x06, 0x25, 0x02, 0x25, 0x42, 0x45, 0x60, 0x11, + 0x00, 0x00, 0x09, 0x00, 0x70, 0x81, 0x70, 0x09, + 0xDC, 0x21, 0xD8, 0x01, 0x98, 0x25, 0xCC, 0x13, + 0xFF, 0x00, 0xF3, 0xF8, 0x02, 0x03, 0x01, 0x30, + 0x39, 0x09, 0x30, 0x09, 0x31, 0x09, 0x20, 0x19, + 0x00, 0x00, 0x01, 0x04, 0xFC, 0x00, 0xCF, 0x30, + 0xE6, 0x00, 0xE6, 0x01, 0xE6, 0x00, 0xF6, 0x08, + 0xFF, 0x00, 0xFA, 0xC7, 0x18, 0x21, 0x09, 0x10, + 0x88, 0x99, 0x93, 0x1A, 0x83, 0x11, 0xC2, 0x41, + 0x00, 0x00, 0x00, 0x20, 0xC6, 0x21, 0xFF, 0x00, + 0x67, 0x00, 0xE4, 0x08, 0x6F, 0x10, 0x3C, 0x00, + 0xFD, 0x02, 0xB5, 0x3A, 0xC7, 0x44, 0x03, 0x84, + 0x83, 0x24, 0x21, 0xB0, 0x21, 0x12, 0x21, 0x02, + 0x02, 0x00, 0x02, 0x40, 0x3C, 0x00, 0xF8, 0x00, + 0xD8, 0x24, 0x4C, 0x92, 0xEC, 0x00, 0xCC, 0x12, + 0xFF, 0x00, 0xFF, 0xF3, 0x1C, 0x14, 0x0C, 0x04, + 0x00, 0x0C, 0x04, 0x24, 0x00, 0x24, 0x10, 0x30, + 0x00, 0x00, 0x10, 0x04, 0xE3, 0x00, 0xFB, 0x00, + 0xF3, 0x08, 0xDB, 0x20, 0xDB, 0x04, 0xCF, 0x00, + 0xFF, 0x00, 0xEC, 0x3E, 0xC1, 0x01, 0x01, 0x8E, + 0x8F, 0x10, 0x0F, 0x90, 0x0F, 0x90, 0x0D, 0x09, + 0x00, 0x00, 0x20, 0x01, 0x7E, 0x00, 0xF1, 0x0E, + 0xE0, 0x10, 0x60, 0x10, 0x60, 0x10, 0x79, 0x82, + 0xFF, 0x00, 0x7F, 0xFC, 0x03, 0x82, 0x01, 0x9E, + 0x13, 0x80, 0x03, 0x80, 0x03, 0x9C, 0x0F, 0x90, + 0x00, 0x00, 0x02, 0x00, 0x7C, 0x80, 0x60, 0x9C, + 0x60, 0x9C, 0x7C, 0x80, 0x60, 0x9C, 0x70, 0x80, + 0xFF, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, + 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xEF, 0xFF, 0xF7, 0x7F, 0x7B, 0x3F, 0x3C, + 0x1F, 0x1F, 0x0F, 0x0F, 0x03, 0x03, 0x00, 0x00, + 0xEF, 0x10, 0x77, 0x88, 0x3B, 0x44, 0x1C, 0x23, + 0x0F, 0x10, 0x03, 0x0C, 0x00, 0x03, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x3F, 0xFF, 0xC3, 0xFF, 0xFC, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x3F, 0xC0, 0xC3, 0x3C, 0xFC, 0x03, 0x3F, 0xC0, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xC1, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x3F, 0xC0, 0xC1, 0x3E, + 0xFF, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xEA, 0x14, 0xC0, 0x00, 0x80, 0x21, 0x7F, 0x92, + 0x9F, 0xE0, 0xE3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x27, 0x18, 0x7F, 0x00, 0x1E, 0x61, 0x9A, 0x04, + 0xE0, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x73, 0x53, 0x47, 0x44, 0x46, 0x25, 0xFD, 0x03, + 0xF9, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x8C, 0x00, 0xD8, 0x20, 0x1D, 0xA0, 0x03, 0x00, + 0x07, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xC7, 0xE1, 0xE6, 0x05, 0x42, 0xA5, 0xBF, 0xC0, + 0x9F, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x18, 0x24, 0x38, 0x01, 0xB8, 0x05, 0xC0, 0x00, + 0xE0, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x21, 0x11, 0x31, 0x49, 0x33, 0x4A, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xDE, 0x00, 0x87, 0x48, 0x84, 0x48, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xCC, 0x02, 0x8E, 0x4A, 0xCC, 0x42, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x71, 0x08, 0x39, 0x00, 0x31, 0x02, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x3D, 0x40, 0x03, 0x02, 0x03, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xBC, 0x02, 0xFC, 0x00, 0xFE, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x12, 0x82, 0x80, 0x80, 0x01, 0x83, 0xFF, 0x00, + 0xFE, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x61, 0x1C, 0x7F, 0x00, 0x7C, 0x82, 0x00, 0x00, + 0x01, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x22, 0x52, 0x30, 0xC0, 0x58, 0xA4, 0x8F, 0x72, + 0x73, 0xFC, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x8C, 0x11, 0x4F, 0x90, 0xA3, 0x0C, 0x73, 0x00, + 0xFC, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x23, 0xA4, 0x06, 0x0D, 0x05, 0x1B, 0xBB, 0x07, + 0xE7, 0x1F, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x98, 0x44, 0xF5, 0x08, 0xEB, 0x00, 0x87, 0x40, + 0x1F, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x66, 0x85, 0xE2, 0xA5, 0x66, 0x81, 0xBF, 0xC1, + 0x99, 0xE7, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x99, 0x00, 0xB9, 0x00, 0x9D, 0x20, 0xC1, 0x00, + 0xE7, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xF6, 0xFA, 0xFC, 0xF2, 0xF7, 0xF8, 0xFB, 0xFC, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xF9, 0x00, 0xF1, 0x02, 0xF8, 0x00, 0xFC, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x52, 0x53, 0x30, 0x23, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x8C, 0x21, 0xCC, 0x13, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x03, 0x03, 0x06, 0xFE, 0x01, 0xF9, 0x07, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFD, 0x02, 0xFA, 0x04, 0x01, 0x00, 0x07, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x86, 0x05, 0x46, 0xA0, 0x5F, 0xB8, 0xBF, 0xC0, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x38, 0x41, 0x99, 0x26, 0xB8, 0x00, 0xC0, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x30, 0x28, 0x09, 0x09, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xC6, 0x09, 0xE6, 0x10, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x20, 0x38, 0x38, 0x20, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xD7, 0x08, 0xCF, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x80, 0x41, 0xA1, 0x61, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x3E, 0x40, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x01, 0x82, 0x01, 0x82, 0xFF, 0x00, 0xFC, 0x03, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x7C, 0x82, 0x7C, 0x82, 0x00, 0x00, 0x03, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x3F, 0x3F, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x3C, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFE, 0xFF, 0xFF, 0x3F, 0x3F, 0x01, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFE, 0x01, 0x3F, 0xC0, 0x01, 0x3E, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x3F, 0xFF, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, + 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3F, 0xC0, 0xC0, 0x3F, 0xFF, 0x00, 0x3F, 0xC0, + 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xC0, 0xFF, 0xFF, + 0xFF, 0xFF, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0x3F, 0xC0, 0xC0, 0x3F, 0xFF, 0x00, + 0x3F, 0xC0, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xFF, 0xFC, + 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0x03, 0xFC, 0xFC, 0x03, + 0xFF, 0x00, 0x03, 0xFC, 0x00, 0x03, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, + 0xFF, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x03, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x03, 0xFC, + 0xFC, 0x03, 0xFF, 0x00, 0x03, 0xFC, 0x00, 0x03, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, +}; diff --git a/Core/graphics/cgb_border.inc b/Core/graphics/cgb_border.inc new file mode 100644 index 0000000..755312a --- /dev/null +++ b/Core/graphics/cgb_border.inc @@ -0,0 +1,446 @@ +static const uint16_t palette[] = { + 0x7C1A, 0x0000, 0x0011, 0x3DEF, 0x6318, 0x7FFF, 0x1EBA, 0x19AF, + 0x1EAF, 0x4648, 0x7FC0, 0x2507, 0x1484, 0x5129, 0x5010, 0x2095, +}; + +static const uint16_t tilemap[] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0001, 0x0002, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, + 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, + 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, + 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x4002, 0x4001, 0x0000, + 0x0000, 0x0004, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x4004, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0007, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, + 0x0008, 0x0008, 0x4007, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x000A, 0x000B, 0x400A, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x800A, 0x000C, 0xC00A, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x0006, 0x0005, 0x0005, 0x0005, 0x0009, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC009, 0x0005, 0x0005, 0x0005, 0xC006, 0x0000, + 0x0000, 0x000D, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x000E, + 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, + 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, + 0x001F, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x400D, 0x0000, + 0x0000, 0x0020, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0021, + 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, + 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, + 0x0032, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x4020, 0x0000, + 0x0000, 0x0033, 0x0034, 0x0035, 0x0036, 0x0005, 0x0005, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0005, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, + 0x0047, 0x0005, 0x0005, 0x4036, 0x4035, 0x4034, 0x4033, 0x0000, + 0x0000, 0x0000, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, + 0x004E, 0x004E, 0x004F, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, + 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x404F, 0x004E, 0x004E, + 0x404D, 0x004C, 0x404B, 0x404A, 0x4049, 0x4048, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +const uint8_t tiles[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x06, 0x00, 0x08, + 0x01, 0x11, 0x06, 0x26, 0x04, 0x24, 0x08, 0x48, + 0x00, 0x00, 0x01, 0x01, 0x07, 0x07, 0x0F, 0x0F, + 0x1E, 0x1F, 0x39, 0x3F, 0x3B, 0x3F, 0x77, 0x7F, + 0x00, 0x7F, 0x00, 0x80, 0x00, 0x00, 0x7F, 0x7F, + 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0xFF, + 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x08, 0x48, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x77, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, + 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, + 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, + 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x42, + 0xBD, 0xBD, 0x7E, 0x66, 0x7E, 0xFF, 0x7E, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD, 0xFF, + 0x7E, 0xFF, 0xFF, 0xE7, 0x7E, 0x7E, 0x7E, 0x7E, + 0x7E, 0xFF, 0x3C, 0xFF, 0x81, 0xFF, 0xC3, 0xFF, + 0x7E, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x7E, 0x7E, 0x3C, 0x3C, 0x00, 0x00, 0x00, 0x81, + 0x81, 0xC3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x08, 0x48, 0x08, 0x48, 0x08, 0x48, 0x08, 0x48, + 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, 0x6F, 0x7F, + 0x37, 0x7F, 0x37, 0x7F, 0x37, 0x7F, 0x37, 0x7F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFE, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x9F, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x70, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x8F, 0xDF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x78, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x87, 0xC7, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x3C, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC3, 0xE3, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9F, 0x9F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x60, 0xE1, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD8, 0xD8, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x27, 0x8F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x70, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x8F, 0x8F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x70, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x8F, 0x9F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3, 0xC2, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBD, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x10, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x3F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xDF, 0xE0, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x84, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x7F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x08, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0xFF, + 0x08, 0x48, 0x08, 0x48, 0x08, 0x48, 0x08, 0x48, + 0x08, 0x48, 0x04, 0x24, 0x04, 0x24, 0x02, 0x12, + 0x37, 0x7F, 0x37, 0x7F, 0x37, 0x7F, 0x37, 0x7F, + 0x37, 0x7F, 0x1B, 0x3F, 0x1B, 0x3F, 0x0D, 0x1F, + 0x0F, 0x08, 0x0E, 0x00, 0x1E, 0x12, 0x1E, 0x12, + 0x1F, 0x10, 0x0F, 0x08, 0x02, 0x02, 0x00, 0x00, + 0xF7, 0xF8, 0xFF, 0xF0, 0xED, 0xE3, 0xED, 0xE1, + 0xEF, 0xE0, 0xF7, 0xF0, 0xFD, 0xFC, 0xFF, 0xFF, + 0xF0, 0x10, 0x40, 0x00, 0x41, 0x41, 0x00, 0x00, + 0x83, 0x82, 0xE3, 0x20, 0xC7, 0x04, 0xC7, 0x00, + 0xEF, 0x1F, 0xFF, 0x1F, 0xBE, 0xFF, 0xFF, 0xFE, + 0x7D, 0x7E, 0xDF, 0x3C, 0xFB, 0x18, 0xFF, 0x18, + 0x60, 0x00, 0x70, 0x00, 0xF8, 0x08, 0xB0, 0x00, + 0xD8, 0x40, 0x3C, 0x24, 0x5C, 0x44, 0xFC, 0x00, + 0xFF, 0x8F, 0xFF, 0x0F, 0xF7, 0x07, 0xFF, 0x07, + 0xBF, 0x47, 0xDB, 0x47, 0xBB, 0x03, 0xFF, 0x03, + 0x3C, 0x04, 0x78, 0x00, 0x78, 0x00, 0xEC, 0x80, + 0xFE, 0x92, 0xE7, 0x83, 0xE5, 0x80, 0x4F, 0x08, + 0xFB, 0x83, 0xFF, 0x83, 0xFF, 0x83, 0x7F, 0x83, + 0x6D, 0x93, 0x7C, 0x10, 0x7F, 0x10, 0xF7, 0x10, + 0x3C, 0x00, 0x7C, 0x40, 0x78, 0x00, 0xC8, 0x80, + 0xFC, 0x24, 0xBC, 0x24, 0xFD, 0x65, 0x3D, 0x25, + 0xFF, 0xC3, 0xBF, 0x83, 0xFF, 0x83, 0x7F, 0x03, + 0xDB, 0x23, 0xDB, 0x23, 0x9A, 0x67, 0xDA, 0x47, + 0xFF, 0x80, 0xFF, 0x80, 0xE0, 0x80, 0x40, 0x00, + 0xFF, 0x01, 0xFF, 0x01, 0xDF, 0x1F, 0xE0, 0x20, + 0x7F, 0x80, 0x7F, 0x00, 0x7F, 0x1F, 0xFF, 0x1F, + 0xFE, 0x00, 0xFE, 0x00, 0xE0, 0x01, 0xDF, 0x3F, + 0xBF, 0xA0, 0xB9, 0xA0, 0x10, 0x00, 0x11, 0x01, + 0x3B, 0x00, 0x3F, 0x00, 0x7E, 0x4E, 0x78, 0x48, + 0x5F, 0x40, 0x5F, 0xC0, 0xFF, 0xC7, 0xFE, 0xC7, + 0xFF, 0xC0, 0xFF, 0xC0, 0xB1, 0xC4, 0xB7, 0x8F, + 0xE3, 0x22, 0xC7, 0x04, 0xCE, 0x08, 0xE6, 0x20, + 0xCE, 0x42, 0xDE, 0x52, 0xFE, 0x32, 0xFC, 0x30, + 0xDD, 0x3E, 0xFB, 0x18, 0xF7, 0x18, 0xDF, 0x31, + 0xBD, 0x31, 0xAD, 0x23, 0xCD, 0x31, 0xCF, 0x11, + 0xFE, 0x02, 0x9E, 0x00, 0x86, 0x80, 0x06, 0x00, + 0x03, 0x00, 0x02, 0x00, 0x07, 0x01, 0x07, 0x01, + 0xFD, 0x03, 0xFF, 0x01, 0x7F, 0xF0, 0xFF, 0xF8, + 0xFF, 0xF8, 0xFF, 0xF8, 0xFE, 0xF8, 0xFE, 0xF1, + 0x38, 0x08, 0x71, 0x41, 0x1F, 0x06, 0x39, 0x20, + 0x0F, 0x00, 0x0F, 0x01, 0x04, 0x00, 0x0C, 0x00, + 0xF7, 0x87, 0xBE, 0xC6, 0xF9, 0xC6, 0xDF, 0xE0, + 0xFF, 0xE0, 0xFE, 0xF1, 0xFF, 0xF1, 0xFF, 0xF1, + 0x70, 0x10, 0xE0, 0x20, 0x80, 0x00, 0x80, 0x00, + 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xEF, 0x1F, 0xDF, 0x1F, 0xFF, 0x3F, 0xFF, 0x7F, + 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x3F, 0x3F, 0x7F, 0x7F, 0x78, 0x78, 0xF0, 0xF0, + 0xF0, 0xF0, 0xE0, 0xE0, 0xE0, 0xE0, 0xF0, 0xF0, + 0xDF, 0xFF, 0xBD, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, + 0xEF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF, + 0xE7, 0xE0, 0xFF, 0xF0, 0xFE, 0xF0, 0x1C, 0x00, + 0x3C, 0x20, 0x3C, 0x24, 0x3C, 0x24, 0x3C, 0x20, + 0xFF, 0xFF, 0xEF, 0xFF, 0x6F, 0xFF, 0xFF, 0xFF, + 0xDF, 0xFF, 0xDB, 0xFF, 0xDB, 0xFF, 0xDF, 0xFF, + 0xF8, 0x00, 0xFC, 0xC0, 0x1F, 0x03, 0x1F, 0x13, + 0x1F, 0x13, 0x1E, 0x02, 0x1E, 0x02, 0x3E, 0x02, + 0xFF, 0xFF, 0x3F, 0xFF, 0xFC, 0xFF, 0xEC, 0xFF, + 0xED, 0xFE, 0xFC, 0xFF, 0xFC, 0xFF, 0xFC, 0xFF, + 0x90, 0x90, 0x00, 0x00, 0x00, 0x00, 0x21, 0x21, + 0x20, 0x21, 0x00, 0x01, 0x00, 0x01, 0x40, 0x41, + 0x8F, 0x7F, 0x1F, 0xFF, 0x1F, 0xFF, 0x3F, 0xDE, + 0x1F, 0xFE, 0x3F, 0xFE, 0x3F, 0xFE, 0x7F, 0xBE, + 0x40, 0x7F, 0x84, 0xFF, 0x11, 0xF1, 0x20, 0xE0, + 0x20, 0xE0, 0x01, 0xC1, 0x01, 0xC1, 0x22, 0xE3, + 0xFF, 0x80, 0xFF, 0x00, 0xFF, 0x0E, 0xFF, 0x1F, + 0xDF, 0x3F, 0xFE, 0x3F, 0xFF, 0x3E, 0xFD, 0x1E, + 0x47, 0xC0, 0x27, 0xE0, 0x2F, 0xE8, 0x0F, 0xE9, + 0x0F, 0xE1, 0x0F, 0xE0, 0x3F, 0xF0, 0x3F, 0xF1, + 0xF8, 0x3F, 0xF8, 0x1F, 0xF0, 0x1F, 0xF0, 0x1F, + 0xF0, 0x1F, 0xF0, 0x1F, 0xE0, 0x1F, 0xC0, 0x3F, + 0xFC, 0x00, 0xFE, 0xE2, 0x1E, 0x12, 0x1E, 0x12, + 0x3E, 0x22, 0xFC, 0x00, 0xF8, 0x08, 0xF0, 0x10, + 0x03, 0xFF, 0x01, 0xFF, 0xE1, 0xFF, 0xE1, 0xFF, + 0xC1, 0xFF, 0x03, 0xFF, 0x07, 0xFF, 0x0F, 0xFF, + 0x01, 0x11, 0x00, 0x08, 0x00, 0x04, 0x00, 0x02, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0E, 0x1F, 0x07, 0x0F, 0x03, 0x07, 0x01, 0x03, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x80, 0x80, 0x40, 0x40, 0x30, 0x30, + 0x0C, 0x0C, 0x03, 0xC3, 0x00, 0x30, 0x00, 0x0C, + 0xFF, 0xFF, 0x7F, 0xFF, 0xBF, 0xFF, 0xCF, 0xFF, + 0xF3, 0xFF, 0x3C, 0xFF, 0x0F, 0x3F, 0x03, 0x0F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xC0, 0xC0, 0x3C, 0x3C, 0x03, 0x03, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x3F, 0xFF, 0xC3, 0xFF, 0xFC, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xFF, + 0x15, 0x15, 0x3F, 0x20, 0x2F, 0x20, 0x06, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xEA, 0xE6, 0xDF, 0xC0, 0xDF, 0xE0, 0xF9, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xE6, 0x20, 0x9E, 0x12, 0x0C, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xDF, 0x30, 0xED, 0x31, 0xFF, 0x63, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x0E, 0x02, 0x1E, 0x12, 0x0D, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFD, 0x03, 0xED, 0xE1, 0xFE, 0xF1, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x4F, 0x08, 0xE6, 0x20, 0xE7, 0x21, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF7, 0x18, 0xDF, 0x18, 0xDE, 0x18, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xB9, 0xA1, 0x11, 0x01, 0x10, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x5E, 0x46, 0xFE, 0xC6, 0xFF, 0xC6, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x3F, 0xFF, 0x01, 0xFF, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xC0, 0x01, 0xFE, 0x00, 0xFE, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x7E, 0x4E, 0x3F, 0x00, 0x3E, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xB1, 0x8E, 0xFF, 0x80, 0xFF, 0x80, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xEE, 0x20, 0x8F, 0x08, 0x85, 0x84, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xDF, 0x30, 0xF7, 0x38, 0x7B, 0x7C, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xAE, 0xA2, 0xF8, 0x00, 0xE8, 0x08, 0xC0, 0xC0, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x5D, 0xE1, 0xFF, 0x03, 0xF7, 0x0F, 0x3F, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x0E, 0x02, 0x1E, 0x12, 0x1E, 0x12, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFD, 0xF1, 0xED, 0xF1, 0xED, 0xE3, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFB, 0xFB, 0x7F, 0x7F, 0x3F, 0x3F, 0x0C, 0x0C, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x75, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF3, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xDF, 0xC1, 0xDF, 0xD0, 0x8F, 0x88, 0x01, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFE, 0xFF, 0xEF, 0xFF, 0x77, 0xFF, 0xFE, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFA, 0x82, 0xF8, 0x08, 0xE0, 0x00, 0x81, 0x81, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x7E, 0xFD, 0xF4, 0xFF, 0xFC, 0xFF, 0x7E, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x7D, 0x7D, 0x02, 0x02, 0x02, 0x02, 0xFC, 0xFC, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0xFE, 0x01, 0xFF, 0x01, 0xFF, 0x03, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x1C, 0xFF, 0x00, 0xFF, 0x41, 0x7F, 0x18, 0x18, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xF7, 0x08, 0xFF, 0x00, 0xFF, 0x80, 0xE7, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x5E, 0xC2, 0x9C, 0x80, 0x1C, 0x04, 0x08, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xE1, 0x3F, 0xE3, 0x7F, 0xE3, 0xFF, 0xF7, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xF0, 0x80, 0x78, 0x08, 0x78, 0x48, 0x10, 0x10, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0F, 0xFF, 0x87, 0xFF, 0x87, 0xFF, 0xEF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xC0, 0x00, 0x3C, 0x00, 0x03, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3F, 0xFF, 0x03, 0x3F, 0x00, 0x03, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x1C, 0x1C, 0x03, 0x03, 0x00, 0xE0, 0x00, 0x1C, + 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xE3, 0xFF, 0xFC, 0xFF, 0x1F, 0xFF, 0x03, 0x1F, + 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xFC, 0xFC, 0x03, 0x03, 0x00, 0x00, + 0x00, 0xFC, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0x03, 0xFF, 0xFC, 0xFF, 0xFF, 0xFF, + 0x03, 0xFF, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x3F, 0x3F, + 0x00, 0x00, 0x00, 0xC0, 0x00, 0x3F, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xC0, 0xFF, + 0xFF, 0xFF, 0x3F, 0xFF, 0x00, 0x3F, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, + 0x3F, 0x3F, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x3F, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, + 0xC0, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0x00, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF +}; diff --git a/Core/graphics/dmg_border.inc b/Core/graphics/dmg_border.inc new file mode 100644 index 0000000..7db0673 --- /dev/null +++ b/Core/graphics/dmg_border.inc @@ -0,0 +1,558 @@ +static const uint16_t palette[] = { + 0x0000, 0x0011, 0x18C6, 0x001A, 0x318C, 0x39CE, 0x5294, 0x5AD6, + 0x739C, 0x45A8, 0x4520, 0x18A5, 0x4A32, 0x2033, 0x20EC, 0x0000, +}; + +static const uint16_t tilemap[] = { + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, + 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, + 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, + 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, + 0x0001, 0x0003, 0x0004, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, + 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x4004, 0x4003, 0x0001, + 0x0001, 0x0006, 0x0007, 0x0007, 0x0007, 0x0008, 0x0009, 0x000A, + 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, 0x0012, + 0x0013, 0x0014, 0x0015, 0x000E, 0x0016, 0x0017, 0x0018, 0x0019, + 0x001A, 0x001B, 0x001C, 0x0007, 0x0007, 0x0007, 0x4006, 0x0001, + 0x0001, 0x001D, 0x001E, 0x001E, 0x001E, 0x001F, 0x0020, 0x0021, + 0x0022, 0x0023, 0x0024, 0x0025, 0x4024, 0x0026, 0x0025, 0x0025, + 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, + 0x002F, 0x0030, 0x0031, 0x001E, 0x001E, 0x001E, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0034, 0x0035, 0x4034, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x8034, 0x0036, 0xC034, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0xC01D, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0x0037, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0032, 0x0038, 0x0001, + 0x0001, 0x001D, 0x0032, 0x0032, 0x0032, 0x0033, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xC033, 0x0032, 0x0032, 0x0039, 0x003A, 0x0001, + 0x0001, 0x003B, 0x003C, 0x0032, 0x0032, 0xC03C, 0x003D, 0x003D, + 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, + 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, 0x003D, + 0x003D, 0x003D, 0x003E, 0x003F, 0x0040, 0x0041, 0x0001, 0x0001, + 0x0001, 0x0042, 0x0043, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, + 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, + 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, 0x0044, + 0x0044, 0x0044, 0x0045, 0x0046, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, + 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x005B, + 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x0001, 0x006C, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, +}; + +const uint8_t tiles[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xFF, 0x01, 0xFE, 0x06, 0xF9, 0x08, 0xF7, + 0x11, 0xEF, 0x22, 0xDB, 0x20, 0xDB, 0x40, 0xB7, + 0xFF, 0x00, 0xFF, 0x00, 0xFE, 0x00, 0xF8, 0x00, + 0xF1, 0x00, 0xE6, 0x04, 0xE4, 0x00, 0xC8, 0x00, + 0x7F, 0x80, 0x80, 0x7F, 0x00, 0xFF, 0x7F, 0xFF, + 0x80, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0x80, 0x00, 0x00, 0x00, 0x7F, 0x00, + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x40, 0xB7, 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, + 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, + 0xC8, 0x00, 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, + 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x03, 0xFF, 0x02, 0xFF, + 0x02, 0xFF, 0x02, 0xFF, 0x02, 0xFF, 0x02, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, + 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0xC1, 0xDD, 0x00, 0xC9, + 0x14, 0xFF, 0x14, 0xFF, 0x14, 0xFF, 0x00, 0xC9, + 0x00, 0x00, 0x00, 0x00, 0xE3, 0x00, 0x36, 0x22, + 0x14, 0x00, 0x14, 0x00, 0x14, 0x00, 0x36, 0x22, + 0x00, 0xFF, 0x00, 0xFF, 0xC7, 0xDF, 0x01, 0xCF, + 0x11, 0xFF, 0x11, 0xFF, 0x11, 0xFF, 0x01, 0xCF, + 0x00, 0x00, 0x00, 0x00, 0xE7, 0x00, 0x31, 0x20, + 0x11, 0x00, 0x11, 0x00, 0x11, 0x00, 0x31, 0x20, + 0x00, 0xFF, 0x00, 0xFF, 0xC2, 0xFF, 0x03, 0xFF, + 0x02, 0xFE, 0x02, 0xFE, 0x02, 0xFF, 0x02, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0xC2, 0x00, 0x03, 0x00, + 0x03, 0x01, 0x03, 0x00, 0x02, 0x00, 0x02, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x08, 0xFF, 0x18, 0xFF, + 0x08, 0x4E, 0x08, 0x4E, 0x09, 0x1F, 0x08, 0x1C, + 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x18, 0x00, + 0xB9, 0x10, 0xB9, 0xA1, 0xE9, 0xA0, 0xEB, 0x41, + 0x00, 0xFF, 0x00, 0xFF, 0x4F, 0xFF, 0x02, 0x1F, + 0x02, 0x4F, 0x02, 0x4F, 0xF2, 0xFF, 0x02, 0xE7, + 0x00, 0x00, 0x00, 0x00, 0x4F, 0x00, 0xE2, 0xA0, + 0xB2, 0xA0, 0xB2, 0x10, 0xF2, 0x00, 0x1A, 0x10, + 0x00, 0xFF, 0x00, 0xFF, 0xBC, 0xFD, 0x22, 0xFB, + 0x22, 0xFB, 0x3C, 0xFD, 0x24, 0xFF, 0x26, 0xF9, + 0x00, 0x00, 0x00, 0x00, 0xBE, 0x00, 0x26, 0x00, + 0x26, 0x00, 0x3E, 0x00, 0x24, 0x00, 0x26, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x50, 0xFF, 0x49, 0xEF, + 0x49, 0xF0, 0x46, 0xFF, 0x49, 0xF0, 0x49, 0xEF, + 0x00, 0x00, 0x00, 0x00, 0x50, 0x00, 0x59, 0x00, + 0x4F, 0x06, 0x46, 0x00, 0x4F, 0x06, 0x59, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x88, 0xFF, 0x00, 0x72, + 0x00, 0xF2, 0x05, 0xFF, 0x00, 0xF8, 0x00, 0x78, + 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x8D, 0x08, + 0x0D, 0x05, 0x05, 0x00, 0x07, 0x05, 0x87, 0x02, + 0x00, 0xFF, 0x00, 0xFF, 0x8A, 0xFF, 0x02, 0x27, + 0x02, 0x27, 0x52, 0xFF, 0x02, 0x8F, 0x02, 0x8F, + 0x00, 0x00, 0x00, 0x00, 0x8A, 0x00, 0xDA, 0x88, + 0xDA, 0x50, 0x52, 0x00, 0x72, 0x50, 0x72, 0x20, + 0x00, 0xFF, 0x00, 0xFF, 0xFA, 0xFF, 0x22, 0xFF, + 0x22, 0xFF, 0x23, 0xFF, 0x22, 0xFF, 0x22, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0xFA, 0x00, 0x22, 0x00, + 0x22, 0x00, 0x23, 0x00, 0x22, 0x00, 0x22, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x20, 0xFF, 0x20, 0xFF, + 0x20, 0xFF, 0xE0, 0xFF, 0x20, 0xFF, 0x20, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, + 0x20, 0x00, 0xE0, 0x00, 0x20, 0x00, 0x20, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x33, 0x37, 0x00, 0x77, + 0x80, 0xFF, 0x20, 0x27, 0x08, 0xFF, 0x00, 0x77, + 0x00, 0x00, 0x00, 0x00, 0xFB, 0x40, 0x88, 0x88, + 0x80, 0x00, 0xF8, 0x50, 0x08, 0x00, 0x88, 0x88, + 0x00, 0xFF, 0x00, 0xFF, 0xEF, 0xFF, 0x88, 0xFF, + 0x88, 0xFF, 0x8F, 0xFF, 0x88, 0xFF, 0x88, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0xEF, 0x00, 0x88, 0x00, + 0x88, 0x00, 0x8F, 0x00, 0x88, 0x00, 0x88, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0xF9, 0xFD, 0x80, 0xF9, + 0x84, 0xFF, 0xF4, 0xFF, 0x84, 0xFF, 0x80, 0xF9, + 0x00, 0x00, 0x00, 0x00, 0xFB, 0x00, 0x86, 0x02, + 0x84, 0x00, 0xF4, 0x00, 0x84, 0x00, 0x86, 0x02, + 0x00, 0xFF, 0x00, 0xFF, 0xC0, 0xDF, 0x00, 0xCF, + 0x10, 0xFF, 0x10, 0xFF, 0x10, 0xFF, 0x00, 0xCF, + 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x30, 0x20, + 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x30, 0x20, + 0x00, 0xFF, 0x00, 0xFF, 0x30, 0x36, 0x00, 0x74, + 0x82, 0xFF, 0x22, 0x27, 0x0A, 0xFF, 0x00, 0x74, + 0x00, 0x00, 0x00, 0x00, 0xF9, 0x40, 0x8B, 0x89, + 0x82, 0x00, 0xFA, 0x50, 0x0A, 0x00, 0x8B, 0x89, + 0x00, 0xFF, 0x00, 0xFF, 0xE2, 0xEF, 0x02, 0xE7, + 0x0A, 0xFF, 0x0A, 0xFF, 0x0A, 0xFF, 0x00, 0xE4, + 0x00, 0x00, 0x00, 0x00, 0xF2, 0x00, 0x1A, 0x10, + 0x0A, 0x00, 0x0A, 0x00, 0x0A, 0x00, 0x1B, 0x12, + 0x00, 0xFF, 0x00, 0xFF, 0x14, 0xFF, 0x16, 0xFF, + 0x14, 0xFC, 0x15, 0xFE, 0x14, 0xFF, 0x04, 0xCF, + 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x16, 0x00, + 0x17, 0x01, 0x15, 0x00, 0x14, 0x00, 0x34, 0x10, + 0x00, 0xFF, 0x00, 0xFF, 0x2F, 0xFF, 0x28, 0xFF, + 0x28, 0xFF, 0xA8, 0x7F, 0x28, 0x3F, 0x68, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x28, 0x00, + 0x28, 0x00, 0xA8, 0x00, 0xE8, 0x80, 0x68, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x3F, + 0x40, 0xFF, 0x40, 0xFF, 0x40, 0xFF, 0x00, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xC0, 0x80, + 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0xC0, 0x80, + 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, + 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, + 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, + 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x01, 0xFF, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0xC1, 0xDD, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xC1, 0xDF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x02, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x4A, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x0A, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x22, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x82, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x20, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x60, 0x67, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xF8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x8F, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x8F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xA2, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xF9, 0xFD, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xC0, 0xDF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x60, 0x66, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xF9, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xE0, 0xEE, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xF1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xC4, 0xDF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0xE4, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x2F, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, + 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0x00, 0x7F, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x80, 0xFF, + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0xFF, 0x01, 0xFF, 0x01, 0xFF, 0x01, 0xFF, + 0x01, 0xFF, 0x01, 0xFF, 0x01, 0xFF, 0x01, 0xFF, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x3C, 0xFF, + 0x7E, 0xFF, 0xE7, 0xE7, 0xFF, 0x7E, 0xFF, 0x7E, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x7E, + 0x81, 0xC3, 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, + 0xFF, 0x7E, 0xFF, 0x3C, 0xFF, 0x00, 0x7E, 0x81, + 0x3C, 0xC3, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0xC3, 0x81, + 0x7E, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xF7, 0x00, 0xF7, 0x00, 0xF7, 0x00, 0xF7, + 0x00, 0xF7, 0x00, 0xED, 0x00, 0xED, 0x00, 0xED, + 0x09, 0x00, 0x09, 0x00, 0x09, 0x00, 0x09, 0x00, + 0x09, 0x00, 0x11, 0x02, 0x11, 0x02, 0x11, 0x02, + 0x00, 0xED, 0x00, 0xDB, 0x00, 0xDB, 0x00, 0xDB, + 0x00, 0xB7, 0x00, 0xB7, 0x00, 0x6F, 0x00, 0x6F, + 0x11, 0x02, 0x23, 0x04, 0x23, 0x04, 0x23, 0x04, + 0x47, 0x08, 0x47, 0x08, 0x8F, 0x10, 0x8F, 0x10, + 0x00, 0xFE, 0x00, 0xFD, 0x00, 0xFB, 0x00, 0xF7, + 0x00, 0xEE, 0x00, 0xDD, 0x00, 0xBB, 0x00, 0x77, + 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, + 0x10, 0x01, 0x21, 0x02, 0x43, 0x04, 0x87, 0x08, + 0x00, 0xDF, 0x00, 0xBF, 0x00, 0xBF, 0x00, 0x7F, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x1F, 0x20, 0x3F, 0x40, 0x3F, 0x40, 0x7F, 0x80, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xEF, 0x00, 0xB7, + 0x00, 0xB7, 0x00, 0xDB, 0x00, 0xDD, 0x00, 0xEE, + 0x90, 0x00, 0x90, 0x00, 0x90, 0x00, 0x88, 0x40, + 0x88, 0x40, 0xC4, 0x20, 0xC2, 0x20, 0xE1, 0x10, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x7F, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFC, + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFC, 0x00, 0xE3, 0x00, 0x1F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x03, 0x00, 0x1C, 0x00, 0xE0, 0x00, + 0x00, 0xFE, 0x00, 0xFD, 0x00, 0xF3, 0x00, 0xEF, + 0x00, 0x1C, 0x00, 0xF3, 0x00, 0xEF, 0x00, 0x1F, + 0x01, 0x00, 0x02, 0x00, 0x0C, 0x00, 0x10, 0x00, + 0xE0, 0x03, 0x03, 0x0C, 0x0F, 0x10, 0x1F, 0xE0, + 0x00, 0xEF, 0x00, 0xDF, 0x00, 0xBF, 0x00, 0x7F, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x0F, 0x10, 0x1F, 0x20, 0x3F, 0x40, 0x7F, 0x80, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xF7, 0x00, 0xF9, 0x00, 0xFE, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xF0, 0x08, 0xF8, 0x06, 0xFE, 0x01, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x80, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x80, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x7F, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x7F, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x03, 0x00, 0xFF, 0x00, 0xFC, 0x00, 0x03, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFC, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0xFC, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xFC, 0x00, 0xE3, 0x00, 0x1F, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x03, 0x03, 0x1C, 0x1F, 0xE0, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, + 0x00, 0xFF, 0x00, 0xFF, 0x21, 0xDE, 0x00, 0x7F, + 0x0C, 0xF3, 0x19, 0xE0, 0x10, 0xEE, 0x08, 0xF7, + 0xFF, 0x00, 0xFF, 0x00, 0xC0, 0x3F, 0x80, 0xFF, + 0x00, 0xFF, 0x0E, 0xF7, 0x1F, 0xE1, 0x07, 0xF8, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0xBF, + 0x40, 0xBE, 0x80, 0x3F, 0x02, 0xFD, 0x00, 0xFB, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x80, 0x7F, 0xC0, + 0x7F, 0x81, 0xFE, 0x41, 0xFC, 0x03, 0xFC, 0x07, + 0x00, 0xFF, 0x00, 0xFF, 0x08, 0xF7, 0x00, 0xFF, + 0x00, 0xFB, 0x04, 0xFB, 0x24, 0xDB, 0x64, 0x9B, + 0xFF, 0x00, 0xFF, 0x00, 0x87, 0x78, 0x07, 0xF8, + 0x07, 0xFC, 0x07, 0xF8, 0x03, 0xFC, 0x43, 0xBC, + 0x00, 0xFF, 0x00, 0xFF, 0x01, 0xDE, 0x20, 0xDF, + 0x20, 0xDF, 0x00, 0xFF, 0x04, 0xFB, 0x04, 0xFB, + 0xFF, 0x00, 0xFF, 0x00, 0xE0, 0x3F, 0xC0, 0x3F, + 0xC0, 0x3F, 0xC0, 0x3F, 0xC0, 0x3F, 0xC0, 0x3F, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFB, 0x00, 0xFF, + 0x00, 0x77, 0x00, 0x7F, 0x80, 0x6F, 0x82, 0x7D, + 0xFF, 0x00, 0xFF, 0x00, 0xFC, 0x07, 0xF8, 0x07, + 0xF8, 0x8F, 0xF0, 0x8F, 0x70, 0x9F, 0x60, 0x9F, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFB, 0x24, 0xCB, + 0x24, 0xDB, 0x20, 0xDF, 0x20, 0xDF, 0x00, 0xDF, + 0xFF, 0x00, 0xFF, 0x00, 0x1C, 0xE7, 0x18, 0xF7, + 0x18, 0xE7, 0x18, 0xE7, 0x38, 0xC7, 0x38, 0xE7, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x02, 0xFC, + 0x7E, 0x81, 0x80, 0x01, 0x80, 0x7F, 0xF8, 0x03, + 0xFF, 0x00, 0xFF, 0x00, 0x01, 0xFE, 0x01, 0xFF, + 0x01, 0xFE, 0x7F, 0xFE, 0x7F, 0x80, 0x07, 0xFC, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xBF, 0x40, 0xBF, + 0x47, 0xB8, 0x08, 0xF0, 0x08, 0xF7, 0x0E, 0xF1, + 0xFF, 0x00, 0xFF, 0x00, 0xC0, 0x7F, 0x80, 0x7F, + 0x80, 0x7F, 0x87, 0x7F, 0x87, 0x78, 0x80, 0x7F, + 0x00, 0xFF, 0x00, 0xFF, 0x40, 0x9F, 0x00, 0xFF, + 0x10, 0xEF, 0x90, 0x6F, 0x10, 0xEB, 0x14, 0xEB, + 0xFF, 0x00, 0xFF, 0x00, 0x3F, 0xE0, 0x1F, 0xE0, + 0x0E, 0xF1, 0x0C, 0xF3, 0x0C, 0xF7, 0x18, 0xE7, + 0x00, 0xFF, 0x00, 0xFF, 0x20, 0x9F, 0x00, 0xFF, + 0x0C, 0xF3, 0x31, 0xC0, 0x60, 0x9F, 0x40, 0xBF, + 0xFF, 0x00, 0xFF, 0x00, 0xC0, 0x7F, 0x00, 0xFF, + 0x00, 0xFF, 0x1E, 0xEF, 0x3F, 0xC0, 0x7F, 0x80, + 0x00, 0xFF, 0x00, 0xFF, 0x80, 0x77, 0x04, 0xDB, + 0x00, 0xFB, 0x10, 0xEF, 0x00, 0xFD, 0x80, 0x77, + 0xFF, 0x00, 0xFF, 0x00, 0x78, 0x8F, 0x38, 0xE7, + 0x1C, 0xE7, 0x0C, 0xF3, 0x0E, 0xF3, 0x0E, 0xF9, + 0x00, 0xFF, 0x00, 0xFF, 0x80, 0x7F, 0x00, 0xFF, + 0x40, 0xB7, 0x00, 0xEF, 0x01, 0xDE, 0x02, 0xFC, + 0xFF, 0x00, 0xFF, 0x00, 0x7C, 0x83, 0x78, 0x87, + 0x38, 0xCF, 0x30, 0xDF, 0x21, 0xFE, 0x03, 0xFD, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xDF, 0x60, 0x9F, + 0xC0, 0x3F, 0x80, 0x7F, 0x00, 0x7F, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0x3F, 0xE0, 0x3F, 0xC0, + 0x7F, 0x80, 0xFF, 0x00, 0xFF, 0x80, 0xFF, 0x00, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFB, 0x01, 0xFC, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFE, 0x01, 0xFC, 0x03, 0xFC, 0x07, 0xFE, 0x03, + 0x00, 0xFF, 0x40, 0x3F, 0x30, 0x8F, 0x00, 0xF7, + 0x80, 0x7F, 0x30, 0xCF, 0x01, 0xFE, 0x87, 0x78, + 0x01, 0xFE, 0x80, 0xFF, 0xE0, 0x5F, 0xF8, 0x0F, + 0x78, 0x87, 0x00, 0xFF, 0x00, 0xFF, 0x03, 0xFC, + 0x00, 0xFF, 0x08, 0xF7, 0x80, 0x6F, 0x80, 0x7F, + 0x80, 0x5F, 0x87, 0x78, 0x04, 0x7B, 0x08, 0x73, + 0xF8, 0x07, 0xF0, 0x0F, 0x70, 0x9F, 0x60, 0x9F, + 0x60, 0xBF, 0xC3, 0x3C, 0x87, 0xF8, 0x87, 0xFC, + 0xA0, 0x1F, 0x80, 0x7D, 0xE2, 0x1D, 0x02, 0xFD, + 0x02, 0xFD, 0xF0, 0x0F, 0x10, 0xEE, 0x11, 0xEE, + 0x43, 0xFC, 0xE3, 0x1E, 0x03, 0xFC, 0x01, 0xFE, + 0x01, 0xFE, 0xE1, 0x1E, 0xE1, 0x1F, 0xE1, 0x1E, + 0x44, 0xBB, 0x48, 0xB3, 0x48, 0xB7, 0x08, 0xF7, + 0x0A, 0xF5, 0x02, 0xF5, 0x80, 0x77, 0x90, 0x67, + 0x84, 0x7B, 0x84, 0x7F, 0x84, 0x7B, 0x84, 0x7B, + 0x8C, 0x73, 0x8C, 0x7B, 0x0E, 0xF9, 0x0E, 0xF9, + 0x86, 0x59, 0x06, 0xF9, 0x48, 0xB3, 0x08, 0xF7, + 0x10, 0xE7, 0x14, 0xEB, 0x24, 0xCB, 0x20, 0xDF, + 0x60, 0xBF, 0x44, 0xBB, 0x04, 0xFF, 0x0C, 0xF3, + 0x0C, 0xFB, 0x18, 0xE7, 0x18, 0xF7, 0x38, 0xC7, + 0x08, 0xD7, 0x48, 0x97, 0x48, 0xB7, 0x41, 0xBE, + 0x41, 0xBE, 0x01, 0xBE, 0x10, 0xAF, 0x90, 0x2F, + 0x30, 0xEF, 0x30, 0xEF, 0x30, 0xCF, 0x30, 0xCF, + 0x70, 0x8F, 0x70, 0xCF, 0x60, 0xDF, 0x60, 0xDF, + 0x04, 0xFB, 0x04, 0xFB, 0xFC, 0x03, 0x00, 0xFF, + 0x00, 0xFF, 0xF8, 0x02, 0x05, 0xFA, 0x05, 0xFA, + 0x03, 0xFC, 0x03, 0xFC, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0x07, 0xFD, 0x02, 0xFD, 0x06, 0xF9, + 0x80, 0x7F, 0x80, 0x7F, 0x0F, 0xF0, 0x10, 0xE7, + 0x10, 0xEE, 0x1E, 0xE1, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x0E, 0xF1, 0x0F, 0xF8, + 0x0F, 0xF1, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x60, 0x8F, 0x00, 0xDF, 0x00, 0xFF, 0x00, 0xEF, + 0x04, 0xEB, 0x20, 0xCF, 0x22, 0xDD, 0xC1, 0x1E, + 0x38, 0xD7, 0x38, 0xE7, 0x18, 0xE7, 0x18, 0xF7, + 0x18, 0xF7, 0x1C, 0xF3, 0x3E, 0xC1, 0x7F, 0xA0, + 0x80, 0x3F, 0x80, 0x7F, 0x80, 0x7F, 0x01, 0xFE, + 0x00, 0xBD, 0x18, 0xE7, 0x00, 0xFF, 0x83, 0x7C, + 0x7F, 0xC0, 0x7F, 0x80, 0x7F, 0x80, 0x7E, 0x81, + 0x7E, 0xC3, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x81, 0x76, 0x80, 0x77, 0x10, 0xE7, 0x10, 0xEF, + 0x10, 0xEF, 0x21, 0xCE, 0x41, 0x9E, 0x81, 0x3E, + 0x0E, 0xF9, 0x0F, 0xF8, 0x0F, 0xF8, 0x0F, 0xF0, + 0x1F, 0xE0, 0x3E, 0xD1, 0x7E, 0xA1, 0xFE, 0x41, + 0x04, 0xF9, 0x08, 0xF3, 0x18, 0xE7, 0x10, 0xEF, + 0x10, 0xEF, 0x10, 0xEF, 0x00, 0xEF, 0x20, 0xCF, + 0x07, 0xFA, 0x07, 0xFC, 0x0F, 0xF0, 0x0F, 0xF0, + 0x0F, 0xF0, 0x1F, 0xE0, 0x1F, 0xF0, 0x1F, 0xF0, + 0x7C, 0x01, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x82, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x78, 0x87, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x0F, 0xF0, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x70, 0x8E, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x01, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xC3, 0x18, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x24, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x8F, 0x70, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xF8, 0x03, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x04, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x00, 0x7F, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x80, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0x3E, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0xC1, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xE0, 0x1F, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, + 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, +}; diff --git a/Core/sgb_animation_logo.inc b/Core/graphics/sgb_animation_logo.inc similarity index 100% rename from Core/sgb_animation_logo.inc rename to Core/graphics/sgb_animation_logo.inc diff --git a/Core/sgb_border.inc b/Core/graphics/sgb_border.inc similarity index 100% rename from Core/sgb_border.inc rename to Core/graphics/sgb_border.inc diff --git a/Core/sgb.c b/Core/sgb.c index 7ebeae0..d712e27 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -474,7 +474,7 @@ void GB_sgb_write(GB_gameboy_t *gb, uint8_t value) static uint32_t convert_rgb15(GB_gameboy_t *gb, uint16_t color) { - return GB_convert_rgb15(gb, color); + return GB_convert_rgb15(gb, color, false); } static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_t fade) @@ -489,14 +489,17 @@ static uint32_t convert_rgb15_with_fade(GB_gameboy_t *gb, uint16_t color, uint8_ color = r | (g << 5) | (b << 10); - return GB_convert_rgb15(gb, color); + return GB_convert_rgb15(gb, color, false); } #include static void render_boot_animation (GB_gameboy_t *gb) { -#include "sgb_animation_logo.inc" - uint32_t *output = &gb->screen[48 + 40 * 256]; +#include "graphics/sgb_animation_logo.inc" + uint32_t *output = gb->screen; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 48 + 40 * 256; + } uint8_t *input = animation_logo; unsigned fade_blue = 0; unsigned fade_red = 0; @@ -544,7 +547,9 @@ static void render_boot_animation (GB_gameboy_t *gb) input++; } } - output += 256 - 160; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 256 - 160; + } } } @@ -556,14 +561,6 @@ void GB_sgb_render(GB_gameboy_t *gb) } if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) gb->sgb->intro_animation++; - - if (!gb->screen || !gb->rgb_encode_callback) return; - - if (gb->sgb->mask_mode != MASK_FREEZE) { - memcpy(gb->sgb->effective_screen_buffer, - gb->sgb->screen_buffer, - sizeof(gb->sgb->effective_screen_buffer)); - } if (gb->sgb->vram_transfer_countdown) { if (--gb->sgb->vram_transfer_countdown == 0) { @@ -626,16 +623,27 @@ void GB_sgb_render(GB_gameboy_t *gb) } } + if (!gb->screen || !gb->rgb_encode_callback || gb->disable_rendering) return; + uint32_t colors[4 * 4]; for (unsigned i = 0; i < 4 * 4; i++) { colors[i] = convert_rgb15(gb, gb->sgb->effective_palettes[i]); } + if (gb->sgb->mask_mode != MASK_FREEZE) { + memcpy(gb->sgb->effective_screen_buffer, + gb->sgb->screen_buffer, + sizeof(gb->sgb->effective_screen_buffer)); + } + if (gb->sgb->intro_animation < INTRO_ANIMATION_LENGTH) { render_boot_animation(gb); } else { - uint32_t *output = &gb->screen[48 + 40 * 256]; + uint32_t *output = gb->screen; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 48 + 40 * 256; + } uint8_t *input = gb->sgb->effective_screen_buffer; switch ((mask_mode_t) gb->sgb->mask_mode) { case MASK_DISABLED: @@ -645,7 +653,9 @@ void GB_sgb_render(GB_gameboy_t *gb) uint8_t palette = gb->sgb->attribute_map[x / 8 + y / 8 * 20] & 3; *(output++) = colors[(*(input++) & 3) + palette * 4]; } - output += 256 - 160; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 256 - 160; + } } break; } @@ -656,7 +666,9 @@ void GB_sgb_render(GB_gameboy_t *gb) for (unsigned x = 0; x < 160; x++) { *(output++) = black; } - output += 256 - 160; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 256 - 160; + } } break; } @@ -666,7 +678,9 @@ void GB_sgb_render(GB_gameboy_t *gb) for (unsigned x = 0; x < 160; x++) { *(output++) = colors[0]; } - output += 256 - 160; + if (gb->border_mode != GB_BORDER_NEVER) { + output += 256 - 160; + } } break; } @@ -703,6 +717,9 @@ void GB_sgb_render(GB_gameboy_t *gb) if (tile_x >= 6 && tile_x < 26 && tile_y >= 5 && tile_y < 23) { gb_area = true; } + else if (gb->border_mode == GB_BORDER_NEVER) { + continue; + } uint16_t tile = gb->sgb->border.map[tile_x + tile_y * 32]; uint8_t flip_x = (tile & 0x4000)? 0x7 : 0; uint8_t flip_y = (tile & 0x8000)? 0x7 : 0; @@ -710,12 +727,19 @@ void GB_sgb_render(GB_gameboy_t *gb) for (unsigned y = 0; y < 8; y++) { for (unsigned x = 0; x < 8; x++) { uint8_t color = gb->sgb->border.tiles[(tile & 0xFF) * 64 + (x ^ flip_x) + (y ^ flip_y) * 8] & 0xF; - if (color == 0) { - if (gb_area) continue; - gb->screen[tile_x * 8 + x + (tile_y * 8 + y) * 0x100] = colors[0]; + uint32_t *output = gb->screen; + if (gb->border_mode == GB_BORDER_NEVER) { + output += (tile_x - 6) * 8 + x + ((tile_y - 5) * 8 + y) * 160; } else { - gb->screen[tile_x * 8 + x + (tile_y * 8 + y) * 0x100] = border_colors[color + palette * 16]; + output += tile_x * 8 + x + (tile_y * 8 + y) * 256; + } + if (color == 0) { + if (gb_area) continue; + *output = colors[0]; + } + else { + *output = border_colors[color + palette * 16]; } } } @@ -726,12 +750,12 @@ void GB_sgb_render(GB_gameboy_t *gb) void GB_sgb_load_default_data(GB_gameboy_t *gb) { -#include "sgb_border.inc" +#include "graphics/sgb_border.inc" memcpy(gb->sgb->border.map, tilemap, sizeof(tilemap)); memcpy(gb->sgb->border.palette, palette, sizeof(palette)); - /* Expend tileset */ + /* Expand tileset */ for (unsigned tile = 0; tile < sizeof(tiles) / 32; tile++) { for (unsigned y = 0; y < 8; y++) { for (unsigned x = 0; x < 8; x++) { diff --git a/Core/sgb.h b/Core/sgb.h index df90253..aae9f75 100644 --- a/Core/sgb.h +++ b/Core/sgb.h @@ -5,6 +5,16 @@ #include typedef struct GB_sgb_s GB_sgb_t; +typedef struct { + uint8_t tiles[0x100 * 8 * 8]; /* High nibble not used*/ + union { + struct { + uint16_t map[32 * 32]; + uint16_t palette[16 * 4]; + }; + uint16_t raw_data[0x440]; + }; +} GB_sgb_border_t; #ifdef GB_INTERNAL struct GB_sgb_s { @@ -29,16 +39,7 @@ struct GB_sgb_s { uint8_t vram_transfer_countdown, transfer_dest; /* Border */ - struct { - uint8_t tiles[0x100 * 8 * 8]; /* High nibble not used*/ - union { - struct { - uint16_t map[32 * 32]; - uint16_t palette[16 * 4]; - }; - uint16_t raw_data[0x440]; - }; - } border, pending_border; + GB_sgb_border_t border, pending_border; uint8_t border_animation; /* Colorization */ diff --git a/SDL/gui.c b/SDL/gui.c index 4e06a75..6646a17 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -429,7 +429,13 @@ const char *current_color_correction_mode(unsigned index) const char *current_palette(unsigned index) { return (const char *[]){"Greyscale", "Lime (Game Boy)", "Olive (Pocket)", "Teal (Light)"} - [configuration.dmg_palette]; + [configuration.dmg_palette]; +} + +const char *current_border_mode(unsigned index) +{ + return (const char *[]){"SGB Only", "Never", "Always"} + [configuration.border_mode]; } void cycle_scaling(unsigned index) @@ -494,6 +500,26 @@ static void cycle_palette_backwards(unsigned index) } } +static void cycle_border_mode(unsigned index) +{ + if (configuration.border_mode == GB_BORDER_ALWAYS) { + configuration.border_mode = GB_BORDER_SGB; + } + else { + configuration.border_mode++; + } +} + +static void cycle_border_mode_backwards(unsigned index) +{ + if (configuration.border_mode == GB_BORDER_SGB) { + configuration.border_mode = GB_BORDER_ALWAYS; + } + else { + configuration.border_mode--; + } +} + struct shader_name { const char *file_name; const char *display_name; @@ -589,6 +615,7 @@ static const struct menu_item graphics_menu[] = { {"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards}, {"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards}, {"Mono Palette:", cycle_palette, current_palette, cycle_palette_backwards}, + {"Display Border:", cycle_border_mode, current_border_mode, cycle_border_mode_backwards}, {"Blend Frames:", toggle_blend_frames, blend_frames_string, toggle_blend_frames}, {"Back", return_to_root_menu}, {NULL,} diff --git a/SDL/gui.h b/SDL/gui.h index ccfdfb9..41e9bf2 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -103,6 +103,7 @@ typedef struct { /* v0.13 */ uint8_t dmg_palette; + GB_border_mode_t border_mode; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index cb9d00f..06159c9 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -112,12 +112,24 @@ static void update_palette(void) } } +static void screen_size_changed(void) +{ + SDL_DestroyTexture(texture); + texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING, + GB_get_screen_width(&gb), GB_get_screen_height(&gb)); + + SDL_SetWindowMinimumSize(window, GB_get_screen_width(&gb), GB_get_screen_height(&gb)); + + update_viewport(); +} + static void open_menu(void) { bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; if (audio_playing) { SDL_PauseAudioDevice(device_id, 1); } + size_t previous_width = GB_get_screen_width(&gb); run_gui(true); SDL_ShowCursor(SDL_DISABLE); if (audio_playing) { @@ -125,8 +137,12 @@ static void open_menu(void) SDL_PauseAudioDevice(device_id, 0); } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); + GB_set_border_mode(&gb, configuration.border_mode); update_palette(); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); + if (previous_width != GB_get_screen_width(&gb)) { + screen_size_changed(); + } } static void handle_events(GB_gameboy_t *gb) @@ -495,19 +511,15 @@ restart: GB_set_sample_rate(&gb, have_aspec.freq); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); update_palette(); + if ((unsigned)configuration.border_mode <= GB_BORDER_ALWAYS) { + GB_set_border_mode(&gb, configuration.border_mode); + } GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); GB_set_rewind_length(&gb, configuration.rewind_length); GB_set_update_input_hint_callback(&gb, handle_events); GB_apu_set_sample_callback(&gb, gb_audio_callback); } - - SDL_DestroyTexture(texture); - texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING, - GB_get_screen_width(&gb), GB_get_screen_height(&gb)); - - SDL_SetWindowMinimumSize(window, GB_get_screen_width(&gb), GB_get_screen_height(&gb)); - bool error = false; start_capturing_logs(); error = GB_load_rom(&gb, filename); @@ -528,7 +540,7 @@ restart: replace_extension(filename, path_length, symbols_path, ".sym"); GB_debugger_load_symbol_file(&gb, symbols_path); - update_viewport(); + screen_size_changed(); /* Run emulation */ while (true) { From dcb3f6db9e13e5b3510d0eadee326f3cf4ddd27b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 8 Feb 2020 14:38:04 +0200 Subject: [PATCH 134/341] Fix minimum window size in the Cocoa frontend --- Cocoa/Document.m | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 6b9915c..f7349cc 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -239,6 +239,15 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_apu_set_sample_callback(&gb, audioCallback); } +- (void) updateMinSize +{ + self.mainWindow.contentMinSize = NSMakeSize(GB_get_screen_width(&gb), GB_get_screen_height(&gb)); + if (self.mainWindow.contentView.bounds.size.width < GB_get_screen_width(&gb) || + self.mainWindow.contentView.bounds.size.width < GB_get_screen_height(&gb)) { + [self.mainWindow zoom:nil]; + } +} + - (void) vblank { [self.view flip]; @@ -248,6 +257,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_set_border_mode(&gb, (GB_border_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBBorderMode"]); if (GB_get_screen_width(&gb) != previous_width) { [self.view screenSizeChanged]; + [self updateMinSize]; } }); borderModeChanged = false; @@ -405,11 +415,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self.view screenSizeChanged]; } - self.mainWindow.contentMinSize = NSMakeSize(GB_get_screen_width(&gb), GB_get_screen_height(&gb)); - if (self.mainWindow.contentView.bounds.size.width < GB_get_screen_width(&gb) || - self.mainWindow.contentView.bounds.size.width < GB_get_screen_height(&gb)) { - [self.mainWindow zoom:nil]; - } + [self updateMinSize]; if ([sender tag] != 0) { /* User explictly selected a model, save the preference */ From 804b9bec63a16d8f94068bcc1fb5c1f30e4e9f34 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 10 Feb 2020 00:21:33 +0200 Subject: [PATCH 135/341] Fixed a bug where HDMA begins in the middle of an instruction while cycles are pending to be flushed. Fixes #230 --- Core/sm83_cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 0009a69..c9da208 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -1512,10 +1512,11 @@ void GB_cpu_run(GB_gameboy_t *gb) opcodes[gb->last_opcode_read](gb, gb->last_opcode_read); } + flush_pending_cycles(gb); + if (gb->hdma_starting) { gb->hdma_starting = false; gb->hdma_on = true; gb->hdma_cycles = -8; } - flush_pending_cycles(gb); } From 8b7805b95de34aec0ece1b482ce1e2f3b016f453 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 10 Feb 2020 20:19:37 +0200 Subject: [PATCH 136/341] Hit ^T --- Core/sm83_cpu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index c9da208..713bb66 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -1454,19 +1454,19 @@ void GB_cpu_run(GB_gameboy_t *gb) } gb->just_halted = false; - bool effecitve_ime = gb->ime; + bool effective_ime = gb->ime; if (gb->ime_toggle) { gb->ime = !gb->ime; gb->ime_toggle = false; } /* Wake up from HALT mode without calling interrupt code. */ - if (gb->halted && !effecitve_ime && interrupt_queue) { + if (gb->halted && !effective_ime && interrupt_queue) { gb->halted = false; } /* Call interrupt */ - else if (effecitve_ime && interrupt_queue) { + else if (effective_ime && interrupt_queue) { gb->halted = false; uint16_t call_addr = gb->pc; From 0677b1d099fd7ae6bf8eec83fae80df93e33a012 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 11 Feb 2020 00:11:17 +0200 Subject: [PATCH 137/341] Update the automation to not use internel APIs for input --- Tester/main.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Tester/main.c b/Tester/main.c index 03b9985..e3b662c 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -73,38 +73,38 @@ static void handle_buttons(GB_gameboy_t *gb) frames) % combo_length + (start_is_bad? 20 : 0) ) { case 0: if (!limit_start || frames < 20 * 60) { - gb->keys[0][push_right? 0 : 7] = true; // Start (Or right) down + GB_set_key_state(gb, push_right? GB_KEY_RIGHT: GB_KEY_START, true); } if (pointer_control) { - gb->keys[0][1] = true; // left - gb->keys[0][2] = true; // up + GB_set_key_state(gb, GB_KEY_LEFT, true); + GB_set_key_state(gb, GB_KEY_UP, true); } break; case 10: - gb->keys[0][push_right? 0 : 7] = false; // Start (Or right) up + GB_set_key_state(gb, push_right? GB_KEY_RIGHT: GB_KEY_START, false); if (pointer_control) { - gb->keys[0][1] = false; // left - gb->keys[0][2] = false; // up + GB_set_key_state(gb, GB_KEY_LEFT, false); + GB_set_key_state(gb, GB_KEY_UP, false); } break; case 20: - gb->keys[0][b_is_confirm? 5: 4] = true; // A down (or B) + GB_set_key_state(gb, b_is_confirm? GB_KEY_B: GB_KEY_A, true); break; case 30: - gb->keys[0][b_is_confirm? 5: 4] = false; // A up (or B) + GB_set_key_state(gb, b_is_confirm? GB_KEY_B: GB_KEY_A, false); break; case 40: if (push_a_twice) { - gb->keys[0][b_is_confirm? 5: 4] = true; // A down (or B) + GB_set_key_state(gb, b_is_confirm? GB_KEY_B: GB_KEY_A, true); } else if (gb->boot_rom_finished) { - gb->keys[0][3] = true; // D-Pad Down down + GB_set_key_state(gb, GB_KEY_DOWN, true); } break; case 50: - gb->keys[0][b_is_confirm? 5: 4] = false; // A down (or B) - gb->keys[0][3] = false; // D-Pad Down up + GB_set_key_state(gb, b_is_confirm? GB_KEY_B: GB_KEY_A, false); + GB_set_key_state(gb, GB_KEY_DOWN, false); break; } } From f550360f1ae8bf470dbb51a0eb879377b351322e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Feb 2020 13:21:21 +0200 Subject: [PATCH 138/341] More accurate CGB color correction curve --- Core/display.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index b7ddee1..842c21f 100644 --- a/Core/display.c +++ b/Core/display.c @@ -229,7 +229,7 @@ static inline uint8_t scale_channel(uint8_t x) static inline uint8_t scale_channel_with_curve(uint8_t x) { - return (uint8_t[]){0,2,4,7,12,18,25,34,42,52,62,73,85,97,109,121,134,146,158,170,182,193,203,213,221,230,237,243,248,251,253,255}[x]; + return (uint8_t[]){0,5,8,11,16,22,28,36,43,51,59,67,77,87,97,107,119,130,141,153,166,177,188,200,209,221,230,238,245,249,252,255}[x]; } static inline uint8_t scale_channel_with_curve_agb(uint8_t x) From 08eb2f3d987cf7a3775c2ecf1b00b74dd4bea7c2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Feb 2020 15:32:06 +0200 Subject: [PATCH 139/341] Correct emulation of FF6C (Turns out it controls object priority) --- Core/display.c | 2 +- Core/display.h | 7 +++++++ Core/gb.c | 2 ++ Core/gb.h | 7 +++---- Core/memory.c | 20 +++++++++++++++----- Core/save_state.c | 8 ++++++++ Misc/registers.sym | 4 ++-- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Core/display.c b/Core/display.c index 842c21f..521a386 100644 --- a/Core/display.c +++ b/Core/display.c @@ -939,7 +939,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->vram[line_address + 1], palette, object->flags & 0x80, - gb->cgb_mode? gb->visible_objs[gb->n_visible_objs - 1] : 0, + gb->object_priority == GB_OBJECT_PRIORITY_INDEX? gb->visible_objs[gb->n_visible_objs - 1] : 0, object->flags & 0x20); gb->n_visible_objs--; diff --git a/Core/display.h b/Core/display.h index 69e7905..b9e3149 100644 --- a/Core/display.h +++ b/Core/display.h @@ -11,6 +11,13 @@ void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index void GB_window_related_write(GB_gameboy_t *gb, uint8_t addr, uint8_t value); void GB_STAT_update(GB_gameboy_t *gb); void GB_lcd_off(GB_gameboy_t *gb); + +enum { + GB_OBJECT_PRIORITY_UNDEFINED, // For save state compatibility + GB_OBJECT_PRIORITY_X, + GB_OBJECT_PRIORITY_INDEX, +}; + #endif typedef enum { diff --git a/Core/gb.c b/Core/gb.c index b0eaf68..31fff0d 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -1008,11 +1008,13 @@ void GB_reset(GB_gameboy_t *gb) gb->vram_size = 0x2000 * 2; memset(gb->vram, 0, gb->vram_size); gb->cgb_mode = true; + gb->object_priority = GB_OBJECT_PRIORITY_INDEX; } else { gb->ram_size = 0x2000; gb->vram_size = 0x2000; memset(gb->vram, 0, gb->vram_size); + gb->object_priority = GB_OBJECT_PRIORITY_X; update_dmg_palette(gb); } diff --git a/Core/gb.h b/Core/gb.h index e803f3c..b413fe5 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -185,7 +185,7 @@ enum { // Unfortunately it is not readable or writable after boot has finished, so research of this // register is quite limited. The value written to this register, however, can be controlled // in some cases. - GB_IO_DMG_EMULATION = 0x4c, + GB_IO_MODE = 0x4c, /* General CGB features */ GB_IO_KEY1 = 0x4d, // CGB Mode Only - Prepare Speed Switch @@ -212,9 +212,7 @@ enum { GB_IO_BGPD = 0x69, // CGB Mode Only - Background Palette Data GB_IO_OBPI = 0x6a, // CGB Mode Only - Sprite Palette Index GB_IO_OBPD = 0x6b, // CGB Mode Only - Sprite Palette Data - - // 1 is written for DMG ROMs on a CGB. Does not appear to have an effect. - GB_IO_DMG_EMULATION_INDICATION = 0x6c, // (FEh) Bit 0 (Read/Write) + GB_IO_OBJECT_PRIORITY = 0x6c, // Affects object priority (X based or index based) /* Missing */ @@ -516,6 +514,7 @@ struct GB_gameboy_internal_s { bool cgb_palettes_blocked; uint8_t current_lcd_line; // The LCD can go out of sync since the vsync signal is skipped in some cases. uint32_t cycles_in_stop_mode; + uint8_t object_priority; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/memory.c b/Core/memory.c index 003bb77..8e254bc 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -295,11 +295,11 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) return gb->io_registers[GB_IO_TAC] | 0xF8; case GB_IO_STAT: return gb->io_registers[GB_IO_STAT] | 0x80; - case GB_IO_DMG_EMULATION_INDICATION: - if (!gb->cgb_mode) { + case GB_IO_OBJECT_PRIORITY: + if (!GB_is_cgb(gb)) { return 0xFF; } - return gb->io_registers[GB_IO_DMG_EMULATION_INDICATION] | 0xFE; + return gb->io_registers[GB_IO_OBJECT_PRIORITY] | 0xFE; case GB_IO_PCM_12: if (!GB_is_cgb(gb)) return 0xFF; @@ -656,13 +656,22 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_IO_OBP1: case GB_IO_WY: case GB_IO_SB: - case GB_IO_DMG_EMULATION_INDICATION: case GB_IO_UNKNOWN2: case GB_IO_UNKNOWN3: case GB_IO_UNKNOWN4: case GB_IO_UNKNOWN5: gb->io_registers[addr & 0xFF] = value; return; + case GB_IO_OBJECT_PRIORITY: + if ((!gb->boot_rom_finished || (gb->io_registers[GB_IO_MODE] & 8)) && GB_is_cgb(gb)) { + gb->io_registers[addr & 0xFF] = value; + gb->object_priority = (value & 1) ? GB_OBJECT_PRIORITY_X : GB_OBJECT_PRIORITY_INDEX; + } + else if (gb->cgb_mode) { + gb->io_registers[addr & 0xFF] = value; + + } + return; case GB_IO_LYC: /* TODO: Probably completely wrong in double speed mode */ @@ -768,9 +777,10 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->boot_rom_finished = true; return; - case GB_IO_DMG_EMULATION: + case GB_IO_MODE: if (GB_is_cgb(gb) && !gb->boot_rom_finished) { gb->cgb_mode = !(value & 0xC); /* The real "contents" of this register aren't quite known yet. */ + gb->io_registers[GB_IO_MODE] = value; } return; diff --git a/Core/save_state.c b/Core/save_state.c index 8ef99ae..931a319 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -266,6 +266,10 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; + if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { + gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; + } + error: fclose(f); return errno; @@ -371,6 +375,10 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; + if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { + gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; + } + return 0; } diff --git a/Misc/registers.sym b/Misc/registers.sym index 3bc95bb..ea76ab3 100644 --- a/Misc/registers.sym +++ b/Misc/registers.sym @@ -55,7 +55,7 @@ 00:FF69 IO_BGPD 00:FF6A IO_OBPI 00:FF6B IO_OBPD -00:FF6C IO_DMG_EMULATION_INDICATION +00:FF6C IO_OBJECT_PRIORITY 00:FF70 IO_SVBK 00:FF72 IO_UNKNOWN2 00:FF73 IO_UNKNOWN3 @@ -64,4 +64,4 @@ 00:FF76 IO_PCM_12 00:FF77 IO_PCM_34 00:FF7F IO_UNKNOWN8 -00:FFFF IO_IE \ No newline at end of file +00:FFFF IO_IE From bec09a012cae8f1efe5723afac401084420d59d9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Feb 2020 19:21:43 +0200 Subject: [PATCH 140/341] More accurate emulation of STOP mode --- Core/display.c | 56 +++++++++++++++++++++++++++++++++---------------- Core/gb.h | 3 +++ Core/sm83_cpu.c | 33 +++++++++++++++++++++-------- Core/timing.c | 7 ++++++- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/Core/display.c b/Core/display.c index 521a386..d5f3c74 100644 --- a/Core/display.c +++ b/Core/display.c @@ -136,23 +136,18 @@ static void display_vblank(GB_gameboy_t *gb) } } - if ((!gb->disable_rendering || gb->sgb) && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || gb->stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { + bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & 0x80; + + if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ - if (gb->sgb) { - for (unsigned i = 0; i < WIDTH * LINES; i++) { - gb->sgb->screen_buffer[i] = 0x0; - } - } - else { + if (!GB_is_sgb(gb)) { uint32_t color = 0; if (GB_is_cgb(gb)) { - color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? - gb->rgb_encode_callback(gb, 0, 0, 0) : - gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); + color = gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); } else { - color = (gb->io_registers[GB_IO_LCDC] & 0x80) && gb->stopped ? - gb->background_palettes_rgb[3] : + color = is_ppu_stopped ? + gb->background_palettes_rgb[0] : gb->background_palettes_rgb[4]; } if (gb->border_mode == GB_BORDER_ALWAYS) { @@ -412,6 +407,10 @@ static void add_object_from_index(GB_gameboy_t *gb, unsigned index) if (gb->dma_steps_left && (gb->dma_cycles >= 0 || gb->is_dma_restarting)) { return; } + + if (gb->oam_ppu_blocked) { + return; + } /* This reverse sorts the visible objects by location and priority */ GB_object_t *objects = (GB_object_t *) &gb->oam; @@ -499,6 +498,9 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) icd_pixel = pixel; } } + else if (gb->cgb_palettes_ppu_blocked) { + *dest = gb->rgb_encode_callback(gb, 0, 0, 0); + } else { *dest = gb->background_palettes_rgb[fifo_item->palette * 4 + pixel]; } @@ -521,6 +523,9 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) //gb->icd_pixel_callback(gb, pixel); } } + else if (gb->cgb_palettes_ppu_blocked) { + *dest = gb->rgb_encode_callback(gb, 0, 0, 0); + } else { *dest = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; } @@ -585,10 +590,16 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_y = y; } gb->current_tile = gb->vram[map + gb->fetcher_x + y / 8 * 32]; + if (gb->vram_ppu_blocked) { + gb->current_tile = 0xFF; + } if (GB_is_cgb(gb)) { /* The CGB actually accesses both the tile index AND the attributes in the same T-cycle. This probably means the CGB has a 16-bit data bus for the VRAM. */ gb->current_tile_attributes = gb->vram[map + gb->fetcher_x + y / 8 * 32 + 0x2000]; + if (gb->vram_ppu_blocked) { + gb->current_tile_attributes = 0xFF; + } } gb->fetcher_x++; gb->fetcher_x &= 0x1f; @@ -615,7 +626,10 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) y_flip = 0x7; } gb->current_tile_data[0] = - gb->vram[tile_address + ((y & 7) ^ y_flip) * 2]; + gb->vram[tile_address + ((y & 7) ^ y_flip) * 2]; + if (gb->vram_ppu_blocked) { + gb->current_tile_data[0] = 0xFF; + } } gb->fetcher_state++; break; @@ -642,7 +656,10 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) y_flip = 0x7; } gb->current_tile_data[1] = - gb->vram[tile_address + ((y & 7) ^ y_flip) * 2 + 1]; + gb->vram[tile_address + ((y & 7) ^ y_flip) * 2 + 1]; + if (gb->vram_ppu_blocked) { + gb->current_tile_data[1] = 0xFF; + } } gb->fetcher_state++; break; @@ -913,7 +930,11 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_for_line += 6; GB_SLEEP(gb, display, 20, 6); /* TODO: what does the PPU read if DMA is active? */ - GB_object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]]; + const GB_object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]]; + if (gb->oam_ppu_blocked) { + static const GB_object_t blocked = {0xFF, 0xFF, 0xFF, 0xFF}; + object = &blocked; + } bool height_16 = (gb->io_registers[GB_IO_LCDC] & 4) != 0; /* Todo: Which T-cycle actually reads this? */ uint8_t tile_y = (gb->current_line - object->y) & (height_16? 0xF : 7); @@ -933,10 +954,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) if (gb->cgb_mode) { palette = object->flags & 0x7; } - fifo_overlay_object_row(&gb->oam_fifo, - gb->vram[line_address], - gb->vram[line_address + 1], + gb->vram_ppu_blocked? 0xFF : gb->vram[line_address], + gb->vram_ppu_blocked? 0xFF : gb->vram[line_address + 1], palette, object->flags & 0x80, gb->object_priority == GB_OBJECT_PRIORITY_INDEX? gb->visible_objs[gb->n_visible_objs - 1] : 0, diff --git a/Core/gb.h b/Core/gb.h index b413fe5..56b0a9a 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -515,6 +515,9 @@ struct GB_gameboy_internal_s { uint8_t current_lcd_line; // The LCD can go out of sync since the vsync signal is skipped in some cases. uint32_t cycles_in_stop_mode; uint8_t object_priority; + bool oam_ppu_blocked; + bool vram_ppu_blocked; + bool cgb_palettes_ppu_blocked; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 713bb66..f30443d 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -236,6 +236,26 @@ static void nop(GB_gameboy_t *gb, uint8_t opcode) { } +static void enter_stop_mode(GB_gameboy_t *gb) +{ + gb->stopped = true; + gb->oam_ppu_blocked = !gb->oam_read_blocked; + gb->vram_ppu_blocked = !gb->vram_read_blocked; + gb->cgb_palettes_ppu_blocked = !gb->cgb_palettes_blocked; +} + +static void leave_stop_mode(GB_gameboy_t *gb) +{ + /* The CPU takes more time to wake up then the other components */ + for (unsigned i = 0x200; i--;) { + GB_advance_cycles(gb, 0x10); + } + gb->stopped = false; + gb->oam_ppu_blocked = false; + gb->vram_ppu_blocked = false; + gb->cgb_palettes_ppu_blocked = false; +} + static void stop(GB_gameboy_t *gb, uint8_t opcode) { if (gb->io_registers[GB_IO_KEY1] & 0x1) { @@ -252,9 +272,8 @@ static void stop(GB_gameboy_t *gb, uint8_t opcode) gb->cgb_double_speed ^= true; gb->io_registers[GB_IO_KEY1] = 0; - for (unsigned i = 0x800; i--;) { - GB_advance_cycles(gb, 0x40); - } + enter_stop_mode(gb); + leave_stop_mode(gb); if (!needs_alignment) { GB_advance_cycles(gb, 0x4); @@ -270,7 +289,7 @@ static void stop(GB_gameboy_t *gb, uint8_t opcode) gb->halted = true; } else { - gb->stopped = true; + enter_stop_mode(gb); } } @@ -1429,11 +1448,7 @@ void GB_cpu_run(GB_gameboy_t *gb) GB_timing_sync(gb); GB_advance_cycles(gb, 4); if ((gb->io_registers[GB_IO_JOYP] & 0xF) != 0xF) { - gb->stopped = false; - /* The CPU takes more time to wake up then the other components */ - for (unsigned i = 0x800; i--;) { - GB_advance_cycles(gb, 0x40); - } + leave_stop_mode(gb); GB_advance_cycles(gb, 8); } return; diff --git a/Core/timing.c b/Core/timing.c index 283558c..1f3f654 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -139,6 +139,11 @@ static void GB_set_internal_div_counter(GB_gameboy_t *gb, uint32_t value) static void GB_timers_run(GB_gameboy_t *gb, uint8_t cycles) { + if (gb->stopped) { + gb->apu.apu_cycles += 4 << !gb->cgb_double_speed; + return; + } + GB_STATE_MACHINE(gb, div, cycles, 1) { GB_STATE(gb, div, 1); GB_STATE(gb, div, 2); @@ -213,8 +218,8 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) // Affected by speed boost gb->dma_cycles += cycles; + GB_timers_run(gb, cycles); if (!gb->stopped) { - GB_timers_run(gb, cycles); advance_serial(gb, cycles); // TODO: Verify what happens in STOP mode } From bf32ae66c60d0401ccbb49031e19cd47e5890fff Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 15 Feb 2020 19:23:04 +0200 Subject: [PATCH 141/341] Another attemp to fix Cocoa deadlocking --- Cocoa/Document.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index f7349cc..df15806 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -374,7 +374,9 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) if (GB_debugger_is_stopped(&gb)) { [self interruptDebugInputRead]; } + [audioLock lock]; stopping = true; + [audioLock unlock]; running = false; while (stopping); GB_debugger_set_disabled(&gb, false); From 0290e704451742741c90b466e9ee34eeadd4ebcc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 17 Feb 2020 23:05:11 +0200 Subject: [PATCH 142/341] Improvements to AGB color correction --- Core/display.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Core/display.c b/Core/display.c index d5f3c74..e0c1a53 100644 --- a/Core/display.c +++ b/Core/display.c @@ -264,15 +264,13 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border) if (gb->color_correction_mode != GB_COLOR_CORRECTION_CORRECT_CURVES) { uint8_t new_r, new_g, new_b; if (agb) { - new_r = (r * 7 + g * 1) / 8; - new_g = (g * 3 + b * 1) / 4; - new_b = (b * 7 + r * 1) / 8; + new_g = (g * 6 + b * 1) / 7; } else { new_g = (g * 3 + b) / 4; - new_r = r; - new_b = b; } + new_r = r; + new_b = b; if (gb->color_correction_mode == GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS) { uint8_t old_max = MAX(r, MAX(g, b)); uint8_t new_max = MAX(new_r, MAX(new_g, new_b)); From a8f63aea3ce47b72805bc59b5d4f35d112d39bad Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 02:55:07 +0200 Subject: [PATCH 143/341] Emulate DMG LCDC write conflicts correctly. This might vary between individual units. --- Core/sm83_cpu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index f30443d..5c491f8 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -18,6 +18,7 @@ typedef enum { GB_CONFLICT_STAT_DMG, GB_CONFLICT_PALETTE_DMG, GB_CONFLICT_PALETTE_CGB, + GB_CONFLICT_READ_DMG_LCDC, } GB_conflict_t; /* Todo: How does double speed mode affect these? */ @@ -37,7 +38,7 @@ static const GB_conflict_t cgb_conflict_map[0x80] = { static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, [GB_IO_LYC] = GB_CONFLICT_READ_OLD, - [GB_IO_LCDC] = GB_CONFLICT_READ_NEW, + [GB_IO_LCDC] = GB_CONFLICT_READ_DMG_LCDC, [GB_IO_SCY] = GB_CONFLICT_READ_NEW, [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, @@ -192,6 +193,17 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->pending_cycles = 6; return; } + + case GB_CONFLICT_READ_DMG_LCDC: { + /* Seems to be affected by screen? Both my DMG (B, blob) and Game Boy Light behave this way though. */ + uint8_t old_value = GB_read_memory(gb, addr); + GB_advance_cycles(gb, gb->pending_cycles - 2); + GB_write_memory(gb, addr, old_value | (value & 1)); + GB_advance_cycles(gb, 1); + GB_write_memory(gb, addr, value); + gb->pending_cycles = 5; + return; + } } } From 56118d2a6701f422d002a6da123768ef409820a9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 14:22:25 +0200 Subject: [PATCH 144/341] Move improvements to LCDC conflicts --- Core/display.c | 2 -- Core/sm83_cpu.c | 9 ++++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Core/display.c b/Core/display.c index e0c1a53..c09764a 100644 --- a/Core/display.c +++ b/Core/display.c @@ -441,7 +441,6 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) if (!gb->oam_fifo_paused && fifo_size(&gb->oam_fifo)) { oam_fifo_item = fifo_pop(&gb->oam_fifo); - /* Todo: Verify access timings */ if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & 2)) { draw_oam = true; bg_priority |= oam_fifo_item->bg_priority; @@ -457,7 +456,6 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) /* Mixing */ - /* Todo: Verify access timings */ if ((gb->io_registers[GB_IO_LCDC] & 0x1) == 0) { if (gb->cgb_mode) { bg_priority = false; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 5c491f8..53bca48 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -18,7 +18,7 @@ typedef enum { GB_CONFLICT_STAT_DMG, GB_CONFLICT_PALETTE_DMG, GB_CONFLICT_PALETTE_CGB, - GB_CONFLICT_READ_DMG_LCDC, + GB_CONFLICT_DMG_LCDC, } GB_conflict_t; /* Todo: How does double speed mode affect these? */ @@ -38,7 +38,7 @@ static const GB_conflict_t cgb_conflict_map[0x80] = { static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, [GB_IO_LYC] = GB_CONFLICT_READ_OLD, - [GB_IO_LCDC] = GB_CONFLICT_READ_DMG_LCDC, + [GB_IO_LCDC] = GB_CONFLICT_DMG_LCDC, [GB_IO_SCY] = GB_CONFLICT_READ_NEW, [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, @@ -194,10 +194,13 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; } - case GB_CONFLICT_READ_DMG_LCDC: { + case GB_CONFLICT_DMG_LCDC: { /* Seems to be affected by screen? Both my DMG (B, blob) and Game Boy Light behave this way though. */ uint8_t old_value = GB_read_memory(gb, addr); GB_advance_cycles(gb, gb->pending_cycles - 2); + if (/* gb->model != GB_MODEL_MGB && */ gb->position_in_line == 0 && (old_value & 2) && !(value & 2)) { + old_value &= ~2; + } GB_write_memory(gb, addr, old_value | (value & 1)); GB_advance_cycles(gb, 1); GB_write_memory(gb, addr, value); From 91404edd138a6a44b4b2057ffc2300c4bfeab910 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 15:14:33 +0200 Subject: [PATCH 145/341] Disgusting hacks to emulate disabling objects while an object is being fetched --- Core/display.c | 12 ++++++++++++ Core/gb.h | 1 + Core/sm83_cpu.c | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index c09764a..99b8185 100644 --- a/Core/display.c +++ b/Core/display.c @@ -912,6 +912,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) advance_fetcher_state_machine(gb); gb->cycles_for_line++; GB_SLEEP(gb, display, 27, 1); + if (!(gb->io_registers[GB_IO_LCDC] & 2) && !GB_is_cgb(gb)) { + goto abort_fetching_object; + } } /* Todo: Measure if penalty occurs before or after waiting for the fetcher. */ @@ -920,11 +923,19 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_for_line += gb->extra_penalty_for_sprite_at_0; GB_SLEEP(gb, display, 28, gb->extra_penalty_for_sprite_at_0); gb->extra_penalty_for_sprite_at_0 = 0; + if (gb->object_fetch_aborted) { + gb->object_fetch_aborted = false; + goto abort_fetching_object; + } } } gb->cycles_for_line += 6; GB_SLEEP(gb, display, 20, 6); + if (gb->object_fetch_aborted) { + gb->object_fetch_aborted = false; + goto abort_fetching_object; + } /* TODO: what does the PPU read if DMA is active? */ const GB_object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]]; if (gb->oam_ppu_blocked) { @@ -961,6 +972,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->n_visible_objs--; } +abort_fetching_object: /* Handle window */ /* Todo: Timing (Including penalty and access timings) not verified by test ROM */ if (!gb->in_window && window_enabled(gb) && diff --git a/Core/gb.h b/Core/gb.h index 56b0a9a..4e31e00 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -518,6 +518,7 @@ struct GB_gameboy_internal_s { bool oam_ppu_blocked; bool vram_ppu_blocked; bool cgb_palettes_ppu_blocked; + bool object_fetch_aborted; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 53bca48..1dbfc5e 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -195,14 +195,51 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } case GB_CONFLICT_DMG_LCDC: { - /* Seems to be affected by screen? Both my DMG (B, blob) and Game Boy Light behave this way though. */ + /* Similar to the palette registers, these interact directly with the LCD, so they appear to be affected by it. Both my DMG (B, blob) and Game Boy Light behave this way though. + + Additionally, LCDC.1 is very nasty because on the it is read both by the FIFO when popping pixels, + and the sprite-fetching state machine, and both behave differently when it comes to access conflicts. + Hacks ahead. + */ + + + uint8_t old_value = GB_read_memory(gb, addr); GB_advance_cycles(gb, gb->pending_cycles - 2); + + if (gb->current_lcd_line == 108) { + + } + + /* Handle disabling objects while already fetching an object */ + if ((old_value & 2) && !(value & 2)) { + if (gb->display_state == 27) { + old_value &= ~2; + } + else if (gb->display_state == 20 || gb->display_state == 28) { + gb->cycles_for_line -= gb->display_cycles; + gb->display_cycles = 0; + gb->object_fetch_aborted = true; + } + } + if (/* gb->model != GB_MODEL_MGB && */ gb->position_in_line == 0 && (old_value & 2) && !(value & 2)) { old_value &= ~2; } + GB_write_memory(gb, addr, old_value | (value & 1)); GB_advance_cycles(gb, 1); + /* Handle disabling objects while already fetching an object */ + if ((old_value & 2) && !(value & 2)) { + if (gb->display_state == 27) { + old_value &= ~2; + } + else if (gb->display_state == 20 || gb->display_state == 28) { + gb->cycles_for_line -= gb->display_cycles; + gb->display_cycles = 0; + gb->object_fetch_aborted = true; + } + } GB_write_memory(gb, addr, value); gb->pending_cycles = 5; return; From 7d51ba3d9745917f7b5855bcc53e28dd7b5d2be2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 16:16:02 +0200 Subject: [PATCH 146/341] More fixes, SGB emulation of the same quirk --- Core/display.c | 19 +++++++++++++------ Core/gb.h | 1 + Core/sm83_cpu.c | 42 ++++++++++++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Core/display.c b/Core/display.c index 99b8185..7523f6d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -735,6 +735,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 36); GB_STATE(gb, display, 37); GB_STATE(gb, display, 38); + GB_STATE(gb, display, 39); } @@ -904,15 +905,16 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->obj_comparators[gb->n_visible_objs - 1] < (uint8_t)(gb->position_in_line + 8)) { gb->n_visible_objs--; } + + gb->during_object_fetch = true; while (gb->n_visible_objs != 0 && (gb->io_registers[GB_IO_LCDC] & 2 || GB_is_cgb(gb)) && gb->obj_comparators[gb->n_visible_objs - 1] == (uint8_t)(gb->position_in_line + 8)) { - while (gb->fetcher_state < 5) { advance_fetcher_state_machine(gb); gb->cycles_for_line++; GB_SLEEP(gb, display, 27, 1); - if (!(gb->io_registers[GB_IO_LCDC] & 2) && !GB_is_cgb(gb)) { + if (gb->object_fetch_aborted) { goto abort_fetching_object; } } @@ -924,18 +926,20 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_SLEEP(gb, display, 28, gb->extra_penalty_for_sprite_at_0); gb->extra_penalty_for_sprite_at_0 = 0; if (gb->object_fetch_aborted) { - gb->object_fetch_aborted = false; goto abort_fetching_object; } } } - gb->cycles_for_line += 6; - GB_SLEEP(gb, display, 20, 6); + gb->cycles_for_line += 5; + GB_SLEEP(gb, display, 20, 5); if (gb->object_fetch_aborted) { - gb->object_fetch_aborted = false; goto abort_fetching_object; } + gb->during_object_fetch = false; + gb->cycles_for_line++; + GB_SLEEP(gb, display, 39, 1); + /* TODO: what does the PPU read if DMA is active? */ const GB_object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]]; if (gb->oam_ppu_blocked) { @@ -973,6 +977,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } abort_fetching_object: + gb->object_fetch_aborted = false; + gb->during_object_fetch = false; + /* Handle window */ /* Todo: Timing (Including penalty and access timings) not verified by test ROM */ if (!gb->in_window && window_enabled(gb) && diff --git a/Core/gb.h b/Core/gb.h index 4e31e00..11bd10e 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -519,6 +519,7 @@ struct GB_gameboy_internal_s { bool vram_ppu_blocked; bool cgb_palettes_ppu_blocked; bool object_fetch_aborted; + bool during_object_fetch; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 1dbfc5e..4caaa34 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -19,6 +19,7 @@ typedef enum { GB_CONFLICT_PALETTE_DMG, GB_CONFLICT_PALETTE_CGB, GB_CONFLICT_DMG_LCDC, + GB_CONFLICT_SGB_LCDC, } GB_conflict_t; /* Todo: How does double speed mode affect these? */ @@ -56,7 +57,7 @@ static const GB_conflict_t dmg_conflict_map[0x80] = { static const GB_conflict_t sgb_conflict_map[0x80] = { [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, [GB_IO_LYC] = GB_CONFLICT_READ_OLD, - [GB_IO_LCDC] = GB_CONFLICT_READ_NEW, + [GB_IO_LCDC] = GB_CONFLICT_SGB_LCDC, [GB_IO_SCY] = GB_CONFLICT_READ_NEW, [GB_IO_STAT] = GB_CONFLICT_STAT_DMG, @@ -207,16 +208,9 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) uint8_t old_value = GB_read_memory(gb, addr); GB_advance_cycles(gb, gb->pending_cycles - 2); - if (gb->current_lcd_line == 108) { - - } - /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { - if (gb->display_state == 27) { - old_value &= ~2; - } - else if (gb->display_state == 20 || gb->display_state == 28) { + if (gb->during_object_fetch) { gb->cycles_for_line -= gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; @@ -231,10 +225,34 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) GB_advance_cycles(gb, 1); /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { - if (gb->display_state == 27) { - old_value &= ~2; + if (gb->during_object_fetch) { + gb->cycles_for_line -= gb->display_cycles; + gb->display_cycles = 0; + gb->object_fetch_aborted = true; } - else if (gb->display_state == 20 || gb->display_state == 28) { + } + GB_write_memory(gb, addr, value); + gb->pending_cycles = 5; + return; + } + + case GB_CONFLICT_SGB_LCDC: { + /* Simplified version of the above */ + + uint8_t old_value = GB_read_memory(gb, addr); + GB_advance_cycles(gb, gb->pending_cycles - 2); + /* Handle disabling objects while already fetching an object */ + if ((old_value & 2) && !(value & 2)) { + if (gb->during_object_fetch) { + gb->cycles_for_line -= gb->display_cycles; + gb->display_cycles = 0; + gb->object_fetch_aborted = true; + } + } + GB_advance_cycles(gb, 1); + /* Handle disabling objects while already fetching an object */ + if ((old_value & 2) && !(value & 2)) { + if (gb->during_object_fetch) { gb->cycles_for_line -= gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; From 8409d3bcfb345f0c510ec2975266b6e22c562a9e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 16:43:51 +0200 Subject: [PATCH 147/341] Emulate changing sprite height mid-fetch --- Core/display.c | 61 ++++++++++++++++++++++++++++++----------------- Core/gb.h | 1 + Core/save_state.c | 2 ++ 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Core/display.c b/Core/display.c index 7523f6d..c79e8bd 100644 --- a/Core/display.c +++ b/Core/display.c @@ -680,6 +680,30 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_state &= 7; } +static uint16_t get_object_line_address(GB_gameboy_t *gb, const GB_object_t *object) +{ + /* TODO: what does the PPU read if DMA is active? */ + if (gb->oam_ppu_blocked) { + static const GB_object_t blocked = {0xFF, 0xFF, 0xFF, 0xFF}; + object = &blocked; + } + + bool height_16 = (gb->io_registers[GB_IO_LCDC] & 4) != 0; /* Todo: Which T-cycle actually reads this? */ + uint8_t tile_y = (gb->current_line - object->y) & (height_16? 0xF : 7); + + if (object->flags & 0x40) { /* Flip Y */ + tile_y ^= height_16? 0xF : 7; + } + + /* Todo: I'm not 100% sure an access to OAM can't trigger the OAM bug while we're accessing this */ + uint16_t line_address = (height_16? object->tile & 0xFE : object->tile) * 0x10 + tile_y * 2; + + if (gb->cgb_mode && (object->flags & 0x8)) { /* Use VRAM bank 2 */ + line_address += 0x2000; + } + return line_address; +} + /* TODO: It seems that the STAT register's mode bits are always "late" by 4 T-cycles. The PPU logic can be greatly simplified if that delay is simply emulated. @@ -736,6 +760,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 37); GB_STATE(gb, display, 38); GB_STATE(gb, display, 39); + GB_STATE(gb, display, 40); } @@ -931,42 +956,34 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } } - gb->cycles_for_line += 5; - GB_SLEEP(gb, display, 20, 5); + gb->cycles_for_line += 4; + GB_SLEEP(gb, display, 20, 4); if (gb->object_fetch_aborted) { goto abort_fetching_object; } - gb->during_object_fetch = false; + + gb->object_low_line_address = get_object_line_address(gb, &objects[gb->visible_objs[gb->n_visible_objs - 1]]); + gb->cycles_for_line++; GB_SLEEP(gb, display, 39, 1); + if (gb->object_fetch_aborted) { + goto abort_fetching_object; + } + + gb->during_object_fetch = false; + gb->cycles_for_line++; + GB_SLEEP(gb, display, 40, 1); - /* TODO: what does the PPU read if DMA is active? */ const GB_object_t *object = &objects[gb->visible_objs[gb->n_visible_objs - 1]]; - if (gb->oam_ppu_blocked) { - static const GB_object_t blocked = {0xFF, 0xFF, 0xFF, 0xFF}; - object = &blocked; - } - bool height_16 = (gb->io_registers[GB_IO_LCDC] & 4) != 0; /* Todo: Which T-cycle actually reads this? */ - uint8_t tile_y = (gb->current_line - object->y) & (height_16? 0xF : 7); - - if (object->flags & 0x40) { /* Flip Y */ - tile_y ^= height_16? 0xF : 7; - } - - /* Todo: I'm not 100% sure an access to OAM can't trigger the OAM bug while we're accessing this */ - uint16_t line_address = (height_16? object->tile & 0xFE : object->tile) * 0x10 + tile_y * 2; - - if (gb->cgb_mode && (object->flags & 0x8)) { /* Use VRAM bank 2 */ - line_address += 0x2000; - } + uint16_t line_address = get_object_line_address(gb, object); uint8_t palette = (object->flags & 0x10) ? 1 : 0; if (gb->cgb_mode) { palette = object->flags & 0x7; } fifo_overlay_object_row(&gb->oam_fifo, - gb->vram_ppu_blocked? 0xFF : gb->vram[line_address], + gb->vram_ppu_blocked? 0xFF : gb->vram[gb->object_low_line_address], gb->vram_ppu_blocked? 0xFF : gb->vram[line_address + 1], palette, object->flags & 0x80, diff --git a/Core/gb.h b/Core/gb.h index 11bd10e..f44b967 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -520,6 +520,7 @@ struct GB_gameboy_internal_s { bool cgb_palettes_ppu_blocked; bool object_fetch_aborted; bool during_object_fetch; + uint16_t object_low_line_address; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/save_state.c b/Core/save_state.c index 931a319..a0d88c2 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -265,6 +265,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->bg_fifo.write_end &= 0xF; gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; + gb->object_low_line_address &= gb->vram_size & ~1; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; @@ -374,6 +375,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->bg_fifo.write_end &= 0xF; gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; + gb->object_low_line_address &= gb->vram_size & ~1; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; From f86e682d2c225b44d30e71b02485c8116bde93fb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 17:22:57 +0200 Subject: [PATCH 148/341] Fix sign --- Core/sm83_cpu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 4caaa34..49115c7 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -211,7 +211,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { if (gb->during_object_fetch) { - gb->cycles_for_line -= gb->display_cycles; + gb->cycles_for_line += gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; } @@ -226,7 +226,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { if (gb->during_object_fetch) { - gb->cycles_for_line -= gb->display_cycles; + gb->cycles_for_line += gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; } @@ -244,7 +244,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { if (gb->during_object_fetch) { - gb->cycles_for_line -= gb->display_cycles; + gb->cycles_for_line += gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; } @@ -253,7 +253,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* Handle disabling objects while already fetching an object */ if ((old_value & 2) && !(value & 2)) { if (gb->during_object_fetch) { - gb->cycles_for_line -= gb->display_cycles; + gb->cycles_for_line += gb->display_cycles; gb->display_cycles = 0; gb->object_fetch_aborted = true; } From ea2f32b255aa1a31abab207f824c72a6f6d20639 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 21:44:44 +0200 Subject: [PATCH 149/341] The fetcher state machine advances even while handling an object --- Core/display.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index c79e8bd..13bd955 100644 --- a/Core/display.c +++ b/Core/display.c @@ -761,6 +761,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 38); GB_STATE(gb, display, 39); GB_STATE(gb, display, 40); + GB_STATE(gb, display, 41); } @@ -956,8 +957,16 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } } - gb->cycles_for_line += 4; - GB_SLEEP(gb, display, 20, 4); + advance_fetcher_state_machine(gb); + gb->cycles_for_line++; + GB_SLEEP(gb, display, 41, 1); + if (gb->object_fetch_aborted) { + goto abort_fetching_object; + } + advance_fetcher_state_machine(gb); + + gb->cycles_for_line += 3; + GB_SLEEP(gb, display, 20, 3); if (gb->object_fetch_aborted) { goto abort_fetching_object; } From 39b88d546ba4c180683e32367e47ab895a9ba192 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 21 Feb 2020 21:59:03 +0200 Subject: [PATCH 150/341] The upper bits of SCX might mid-line --- Core/display.c | 6 ++++-- Core/save_state.c | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 13bd955..62e2acd 100644 --- a/Core/display.c +++ b/Core/display.c @@ -581,18 +581,20 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ uint8_t y = fetcher_y(gb); + uint8_t x = gb->in_window? gb->fetcher_x : + (((gb->io_registers[GB_IO_SCX] / 8) + (gb->position_in_line / 8) + 1) & 0x1F); if (gb->model > GB_MODEL_CGB_C) { /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; } - gb->current_tile = gb->vram[map + gb->fetcher_x + y / 8 * 32]; + gb->current_tile = gb->vram[map + x + y / 8 * 32]; if (gb->vram_ppu_blocked) { gb->current_tile = 0xFF; } if (GB_is_cgb(gb)) { /* The CGB actually accesses both the tile index AND the attributes in the same T-cycle. This probably means the CGB has a 16-bit data bus for the VRAM. */ - gb->current_tile_attributes = gb->vram[map + gb->fetcher_x + y / 8 * 32 + 0x2000]; + gb->current_tile_attributes = gb->vram[map + x + y / 8 * 32 + 0x2000]; if (gb->vram_ppu_blocked) { gb->current_tile_attributes = 0xFF; } diff --git a/Core/save_state.c b/Core/save_state.c index a0d88c2..8f10152 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -266,6 +266,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; + gb->fetcher_x &= 0x1f; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; @@ -376,6 +377,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->oam_fifo.read_end &= 0xF; gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; + gb->fetcher_x &= 0x1f; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; From d8282fe3c9d79533b4feefd3e39db189519c3784 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Feb 2020 00:45:52 +0200 Subject: [PATCH 151/341] Please pretend the last commit never happened --- Core/display.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 62e2acd..14071b1 100644 --- a/Core/display.c +++ b/Core/display.c @@ -582,7 +582,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ uint8_t y = fetcher_y(gb); uint8_t x = gb->in_window? gb->fetcher_x : - (((gb->io_registers[GB_IO_SCX] / 8) + (gb->position_in_line / 8) + 1) & 0x1F); + (((gb->io_registers[GB_IO_SCX] / 8) + gb->fetcher_x) & 0x1F); if (gb->model > GB_MODEL_CGB_C) { /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; @@ -914,7 +914,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; - gb->fetcher_x = ((gb->io_registers[GB_IO_SCX]) / 8) & 0x1f; + gb->fetcher_x = 0; gb->extra_penalty_for_sprite_at_0 = (gb->io_registers[GB_IO_SCX] & 7); From 83ea4edce295b467f9d00fb42acf7da29bd366eb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 23 Feb 2020 00:16:15 +0200 Subject: [PATCH 152/341] Shut up, annoying log message --- Cocoa/Document.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index df15806..0418c56 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -1005,7 +1005,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) { CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) data); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); - CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; + CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGImageRef iref = CGImageCreate(width, From 2be58439bfba66e89655188cdac9aec9ed57ba54 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 23 Feb 2020 00:38:47 +0200 Subject: [PATCH 153/341] =?UTF-8?q?Starting=20over=20=E2=80=93=20removing?= =?UTF-8?q?=20all=20window=20related=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/display.c | 68 +++++--------------------------------------------- Core/display.h | 1 - Core/gb.h | 6 ++--- Core/memory.c | 5 +--- 4 files changed, 10 insertions(+), 70 deletions(-) diff --git a/Core/display.c b/Core/display.c index 14071b1..956bf80 100644 --- a/Core/display.c +++ b/Core/display.c @@ -111,16 +111,6 @@ typedef struct __attribute__((packed)) { uint8_t flags; } GB_object_t; -static bool window_enabled(GB_gameboy_t *gb) -{ - if ((gb->io_registers[GB_IO_LCDC] & 0x1) == 0) { - if (!gb->cgb_mode) { - return false; - } - } - return (gb->io_registers[GB_IO_LCDC] & 0x20) && gb->io_registers[GB_IO_WX] < 167; -} - static void display_vblank(GB_gameboy_t *gb) { gb->vblank_just_occured = true; @@ -388,9 +378,6 @@ void GB_lcd_off(GB_gameboy_t *gb) gb->vram_write_blocked = false; gb->cgb_palettes_blocked = false; - /* Reset window rendering state */ - gb->wy_diff = 0; - gb->window_disabled_while_active = false; gb->current_line = 0; gb->ly_for_comparison = 0; @@ -543,7 +530,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) static inline uint8_t fetcher_y(GB_gameboy_t *gb) { - return gb->current_line + (gb->in_window? - gb->io_registers[GB_IO_WY] - gb->wy_diff : gb->io_registers[GB_IO_SCY]); + return gb->current_line + gb->io_registers[GB_IO_SCY]; } static void advance_fetcher_state_machine(GB_gameboy_t *gb) @@ -572,17 +559,16 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) uint16_t map = 0x1800; /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ - if (gb->io_registers[GB_IO_LCDC] & 0x08 && !gb->in_window) { + if (gb->io_registers[GB_IO_LCDC] & 0x08 /* && !gb->in_window */) { map = 0x1C00; } - else if (gb->io_registers[GB_IO_LCDC] & 0x40 && gb->in_window) { + /* else if (gb->io_registers[GB_IO_LCDC] & 0x40 && gb->in_window) { map = 0x1C00; - } + } */ /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ uint8_t y = fetcher_y(gb); - uint8_t x = gb->in_window? gb->fetcher_x : - (((gb->io_registers[GB_IO_SCX] / 8) + gb->fetcher_x) & 0x1F); + uint8_t x = ((gb->io_registers[GB_IO_SCX] / 8) + gb->fetcher_x) & 0x1F; if (gb->model > GB_MODEL_CGB_C) { /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; @@ -922,7 +908,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->fetcher_state = 0; gb->bg_fifo_paused = false; gb->oam_fifo_paused = false; - gb->in_window = false; while (true) { /* Handle objects */ /* When the sprite enabled bit is off, this proccess is skipped entirely on the DMG, but not on the CGB. @@ -1009,17 +994,7 @@ abort_fetching_object: gb->during_object_fetch = false; /* Handle window */ - /* Todo: Timing (Including penalty and access timings) not verified by test ROM */ - if (!gb->in_window && window_enabled(gb) && - gb->current_line >= gb->io_registers[GB_IO_WY] + gb->wy_diff && - (uint8_t)(gb->position_in_line + 7) == gb->io_registers[GB_IO_WX]) { - gb->in_window = true; - fifo_clear(&gb->bg_fifo); - gb->bg_fifo_paused = true; - gb->oam_fifo_paused = true; - gb->fetcher_x = 0; - gb->fetcher_state = 0; - } + /* TBD */ render_pixel_if_possible(gb); advance_fetcher_state_machine(gb); @@ -1154,9 +1129,6 @@ abort_fetching_object: GB_SLEEP(gb, display, 17, LINE_LENGTH - 24); - /* Reset window rendering state */ - gb->wy_diff = 0; - gb->window_disabled_while_active = false; gb->current_line = 0; // TODO: not the correct timing gb->current_lcd_line = 0; @@ -1343,31 +1315,3 @@ uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_h } return count; } - -/* Called when a write might enable or disable the window */ -void GB_window_related_write(GB_gameboy_t *gb, uint8_t addr, uint8_t value) -{ - bool before = window_enabled(gb); - gb->io_registers[addr] = value; - bool after = window_enabled(gb); - - if (before != after && gb->current_line < LINES) { - /* Window was disabled or enabled outside of vblank */ - if (gb->current_line >= gb->io_registers[GB_IO_WY]) { - if (after) { - if (!gb->window_disabled_while_active) { - /* Window was turned on for the first time this frame while LY > WY, - should start window in the next line */ - gb->wy_diff = gb->current_line + 1 - gb->io_registers[GB_IO_WY]; - } - else { - gb->wy_diff += gb->current_line; - } - } - else { - gb->wy_diff -= gb->current_line; - gb->window_disabled_while_active = true; - } - } - } -} diff --git a/Core/display.h b/Core/display.h index b9e3149..d539881 100644 --- a/Core/display.h +++ b/Core/display.h @@ -8,7 +8,6 @@ #ifdef GB_INTERNAL void GB_display_run(GB_gameboy_t *gb, uint8_t cycles); void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index); -void GB_window_related_write(GB_gameboy_t *gb, uint8_t addr, uint8_t value); void GB_STAT_update(GB_gameboy_t *gb); void GB_lcd_off(GB_gameboy_t *gb); diff --git a/Core/gb.h b/Core/gb.h index f44b967..ccced99 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -472,7 +472,7 @@ struct GB_gameboy_internal_s { uint8_t position_in_line; bool stat_interrupt_line; uint8_t effective_scx; - uint8_t wy_diff; + GB_PADDING(uint8_t,wy_diff); /* The LCDC will skip the first frame it renders after turning it on. On the CGB, a frame is not skipped if the previous frame was skipped as well. See https://www.reddit.com/r/EmuDev/comments/6exyxu/ */ @@ -489,7 +489,7 @@ struct GB_gameboy_internal_s { bool vram_read_blocked; bool oam_write_blocked; bool vram_write_blocked; - bool window_disabled_while_active; + GB_PADDING(bool, window_disabled_while_active); uint8_t current_line; uint16_t ly_for_comparison; GB_fifo_t bg_fifo, oam_fifo; @@ -502,7 +502,7 @@ struct GB_gameboy_internal_s { uint8_t fetcher_state; bool bg_fifo_paused; bool oam_fifo_paused; - bool in_window; + GB_PADDING(bool, in_window); uint8_t visible_objs[10]; uint8_t obj_comparators[10]; uint8_t n_visible_objs; diff --git a/Core/memory.c b/Core/memory.c index 8e254bc..e1dd2d1 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -646,8 +646,6 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) /* Hardware registers */ switch (addr & 0xFF) { case GB_IO_WX: - GB_window_related_write(gb, addr & 0xFF, value); - break; case GB_IO_IF: case GB_IO_SCX: case GB_IO_SCY: @@ -740,8 +738,7 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) GB_timing_sync(gb); GB_lcd_off(gb); } - /* Writing to LCDC might enable to disable the window, so we write it via GB_window_related_write */ - GB_window_related_write(gb, addr & 0xFF, value); + gb->io_registers[GB_IO_LCDC] = value; return; case GB_IO_STAT: From c0ba898ef2ae7197796fa6b0aaf64a19b108fbfb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 23 Feb 2020 23:16:45 +0200 Subject: [PATCH 154/341] Basic window implementation --- Core/display.c | 78 ++++++++++++++++++++++++++++++++++++++++++++------ Core/gb.h | 6 ++-- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Core/display.c b/Core/display.c index 956bf80..cfbcb71 100644 --- a/Core/display.c +++ b/Core/display.c @@ -530,7 +530,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) static inline uint8_t fetcher_y(GB_gameboy_t *gb) { - return gb->current_line + gb->io_registers[GB_IO_SCY]; + return gb->wx_triggered? gb->window_y : gb->current_line + gb->io_registers[GB_IO_SCY]; } static void advance_fetcher_state_machine(GB_gameboy_t *gb) @@ -539,6 +539,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_GET_TILE, GB_FETCHER_GET_TILE_DATA_LOWER, GB_FETCHER_GET_TILE_DATA_HIGH, + GB_ADVANCE_TILES, GB_FETCHER_PUSH, GB_FETCHER_SLEEP, } fetcher_step_t; @@ -550,7 +551,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_GET_TILE_DATA_LOWER, GB_FETCHER_SLEEP, GB_FETCHER_GET_TILE_DATA_HIGH, - GB_FETCHER_SLEEP, + GB_ADVANCE_TILES, GB_FETCHER_PUSH, }; @@ -558,17 +559,27 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) case GB_FETCHER_GET_TILE: { uint16_t map = 0x1800; + if (!(gb->io_registers[GB_IO_LCDC] & 0x20)) { + gb->wx_triggered = false; + } + /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ - if (gb->io_registers[GB_IO_LCDC] & 0x08 /* && !gb->in_window */) { + if (gb->io_registers[GB_IO_LCDC] & 0x08 && !gb->wx_triggered) { map = 0x1C00; } - /* else if (gb->io_registers[GB_IO_LCDC] & 0x40 && gb->in_window) { + else if (gb->io_registers[GB_IO_LCDC] & 0x40 && gb->wx_triggered) { map = 0x1C00; - } */ + } /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ uint8_t y = fetcher_y(gb); - uint8_t x = ((gb->io_registers[GB_IO_SCX] / 8) + gb->fetcher_x) & 0x1F; + uint8_t x = 0; + if (gb->wx_triggered) { + x = gb->window_tile_x; + } + else { + x = ((gb->io_registers[GB_IO_SCX] / 8) + gb->fetcher_x) & 0x1F; + } if (gb->model > GB_MODEL_CGB_C) { /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; @@ -585,8 +596,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->current_tile_attributes = 0xFF; } } - gb->fetcher_x++; - gb->fetcher_x &= 0x1f; } gb->fetcher_state++; break; @@ -648,6 +657,19 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_state++; break; + case GB_ADVANCE_TILES: { + if (gb->wx_triggered) { + gb->window_tile_x++; + gb->window_tile_x &= 0x1f; + } + else { + gb->fetcher_x++; + gb->fetcher_x &= 0x1f; + } + gb->fetcher_state++; + } + + case GB_FETCHER_PUSH: { if (fifo_size(&gb->bg_fifo) > 0) break; fifo_push_bg_row(&gb->bg_fifo, gb->current_tile_data[0], gb->current_tile_data[1], @@ -767,6 +789,15 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* Handle mode 2 on the very first line 0 */ gb->current_line = 0; + gb->window_y = -1; + /* Todo: verify timings */ + if (gb->io_registers[GB_IO_WY] == 0) { + gb->wy_triggered = true; + } + else { + gb->wy_triggered = false; + } + gb->ly_for_comparison = 0; gb->io_registers[GB_IO_STAT] &= ~3; gb->mode_for_interrupt = -1; @@ -813,7 +844,16 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) while (true) { /* Lines 0 - 143 */ + gb->window_y = -1; for (; gb->current_line < LINES; gb->current_line++) { + gb->wx_triggered = false; + /* Todo: verify timings */ + if ((gb->io_registers[GB_IO_WY] == gb->current_line || + (gb->current_line != 0 && gb->io_registers[GB_IO_WY] == gb->current_line - 1))) { + gb->wy_triggered = true; + } + gb->window_tile_x = 0; + gb->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed; gb->accessed_oam_row = 0; @@ -994,7 +1034,18 @@ abort_fetching_object: gb->during_object_fetch = false; /* Handle window */ - /* TBD */ + /* Todo: verify timings */ + if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { + if (gb->io_registers[GB_IO_WX] == gb->position_in_line + 7 || + gb->io_registers[GB_IO_WX] == gb->position_in_line + 6) { + gb->wx_triggered = true; + gb->window_y++; + fifo_clear(&gb->bg_fifo); + gb->bg_fifo_paused = true; + gb->oam_fifo_paused = true; + gb->fetcher_state = 0; + } + } render_pixel_if_possible(gb); advance_fetcher_state_machine(gb); @@ -1130,6 +1181,15 @@ abort_fetching_object: gb->current_line = 0; + /* Todo: verify timings */ + if ((gb->io_registers[GB_IO_LCDC] & 0x20) && + (gb->io_registers[GB_IO_WY] == 0)) { + gb->wy_triggered = true; + } + else { + gb->wy_triggered = false; + } + // TODO: not the correct timing gb->current_lcd_line = 0; if (gb->icd_vreset_callback) { diff --git a/Core/gb.h b/Core/gb.h index ccced99..e02c7ad 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -472,7 +472,7 @@ struct GB_gameboy_internal_s { uint8_t position_in_line; bool stat_interrupt_line; uint8_t effective_scx; - GB_PADDING(uint8_t,wy_diff); + uint8_t window_y; /* The LCDC will skip the first frame it renders after turning it on. On the CGB, a frame is not skipped if the previous frame was skipped as well. See https://www.reddit.com/r/EmuDev/comments/6exyxu/ */ @@ -502,7 +502,7 @@ struct GB_gameboy_internal_s { uint8_t fetcher_state; bool bg_fifo_paused; bool oam_fifo_paused; - GB_PADDING(bool, in_window); + bool wx_triggered; uint8_t visible_objs[10]; uint8_t obj_comparators[10]; uint8_t n_visible_objs; @@ -521,6 +521,8 @@ struct GB_gameboy_internal_s { bool object_fetch_aborted; bool during_object_fetch; uint16_t object_low_line_address; + bool wy_triggered; + uint8_t window_tile_x; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ From c22611c7010caf470156a0d1494c7dc454e4b346 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 23 Feb 2020 23:48:08 +0200 Subject: [PATCH 155/341] Minor bugfix --- Core/display.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index cfbcb71..6ecbf95 100644 --- a/Core/display.c +++ b/Core/display.c @@ -839,8 +839,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->vram_read_blocked = true; gb->vram_write_blocked = true; + gb->wx_triggered = false; goto mode_3_start; - while (true) { /* Lines 0 - 143 */ From 3864ff37e100041e7787920012a286f7509557b6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 24 Feb 2020 00:20:58 +0200 Subject: [PATCH 156/341] Timing improvements --- Core/display.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Core/display.c b/Core/display.c index 6ecbf95..018b581 100644 --- a/Core/display.c +++ b/Core/display.c @@ -539,7 +539,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_GET_TILE, GB_FETCHER_GET_TILE_DATA_LOWER, GB_FETCHER_GET_TILE_DATA_HIGH, - GB_ADVANCE_TILES, GB_FETCHER_PUSH, GB_FETCHER_SLEEP, } fetcher_step_t; @@ -551,8 +550,8 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_GET_TILE_DATA_LOWER, GB_FETCHER_SLEEP, GB_FETCHER_GET_TILE_DATA_HIGH, - GB_ADVANCE_TILES, GB_FETCHER_PUSH, + GB_FETCHER_PUSH, // Compatibility }; switch (fetcher_state_machine[gb->fetcher_state]) { @@ -657,7 +656,10 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_state++; break; - case GB_ADVANCE_TILES: { + + case GB_FETCHER_PUSH: { + if (fifo_size(&gb->bg_fifo) > 0) break; + if (gb->wx_triggered) { gb->window_tile_x++; gb->window_tile_x &= 0x1f; @@ -666,17 +668,12 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_x++; gb->fetcher_x &= 0x1f; } - gb->fetcher_state++; - } - - case GB_FETCHER_PUSH: { - if (fifo_size(&gb->bg_fifo) > 0) break; fifo_push_bg_row(&gb->bg_fifo, gb->current_tile_data[0], gb->current_tile_data[1], gb->current_tile_attributes & 7, gb->current_tile_attributes & 0x80, gb->current_tile_attributes & 0x20); gb->bg_fifo_paused = false; gb->oam_fifo_paused = false; - gb->fetcher_state++; + gb->fetcher_state = 0; } break; @@ -984,12 +981,15 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } } + /* TODO: Can this be deleted? { */ advance_fetcher_state_machine(gb); gb->cycles_for_line++; GB_SLEEP(gb, display, 41, 1); if (gb->object_fetch_aborted) { goto abort_fetching_object; } + /* } */ + advance_fetcher_state_machine(gb); gb->cycles_for_line += 3; @@ -1036,8 +1036,11 @@ abort_fetching_object: /* Handle window */ /* Todo: verify timings */ if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { - if (gb->io_registers[GB_IO_WX] == gb->position_in_line + 7 || - gb->io_registers[GB_IO_WX] == gb->position_in_line + 6) { + if (gb->io_registers[GB_IO_WX] >= 166) { + // Too late to enable the window + } + else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || + gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6)) { gb->wx_triggered = true; gb->window_y++; fifo_clear(&gb->bg_fifo); From 25b51362e9fcfd1b70615051cea05541be750e74 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 24 Feb 2020 00:33:45 +0200 Subject: [PATCH 157/341] Safety first --- Core/save_state.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/save_state.c b/Core/save_state.c index 8f10152..fd7d814 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -267,6 +267,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; gb->fetcher_x &= 0x1f; + gb->fetcher_state &= 7; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; @@ -378,6 +379,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; gb->fetcher_x &= 0x1f; + gb->fetcher_state &= 7; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; From 248e7bc332eb3799d082e3c41f94a982009a6dec Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 24 Feb 2020 20:46:00 +0200 Subject: [PATCH 158/341] Timing improvements --- Core/display.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/display.c b/Core/display.c index 018b581..b4d9f83 100644 --- a/Core/display.c +++ b/Core/display.c @@ -831,8 +831,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_SLEEP(gb, display, 37, 2); gb->cgb_palettes_blocked = true; - gb->cycles_for_line += 2; - GB_SLEEP(gb, display, 38, 2); + gb->cycles_for_line += 3; + GB_SLEEP(gb, display, 38, 3); gb->vram_read_blocked = true; gb->vram_write_blocked = true; @@ -1046,7 +1046,7 @@ abort_fetching_object: fifo_clear(&gb->bg_fifo); gb->bg_fifo_paused = true; gb->oam_fifo_paused = true; - gb->fetcher_state = 0; + gb->fetcher_state = 1; } } From 7456beb7b9ee08465ac13400d73aebbc408ce413 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 24 Feb 2020 21:23:06 +0200 Subject: [PATCH 159/341] Better emulation of negative WX positions --- Core/display.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index b4d9f83..2161328 100644 --- a/Core/display.c +++ b/Core/display.c @@ -434,12 +434,13 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } } + if (gb->bg_fifo_paused) return; + /* Drop pixels for scrollings */ if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { gb->position_in_line++; return; } - if (gb->bg_fifo_paused) return; /* Mixing */ From b37a0b285a4c31013df7d0f1990a97391687ae0f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 24 Feb 2020 23:59:18 +0200 Subject: [PATCH 160/341] Window Y still advances if WX=166 --- Core/display.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Core/display.c b/Core/display.c index 2161328..c9ca8ce 100644 --- a/Core/display.c +++ b/Core/display.c @@ -1037,17 +1037,19 @@ abort_fetching_object: /* Handle window */ /* Todo: verify timings */ if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { - if (gb->io_registers[GB_IO_WX] >= 166) { + if (gb->io_registers[GB_IO_WX] >= 167) { // Too late to enable the window } else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6)) { - gb->wx_triggered = true; + gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6)) { gb->window_y++; - fifo_clear(&gb->bg_fifo); - gb->bg_fifo_paused = true; - gb->oam_fifo_paused = true; - gb->fetcher_state = 1; + if (gb->io_registers[GB_IO_WX] != 166) { + gb->wx_triggered = true; + fifo_clear(&gb->bg_fifo); + gb->bg_fifo_paused = true; + gb->oam_fifo_paused = true; + gb->fetcher_state = 1; + } } } From 9c7a8fdb1bf2e2fdf09aa2f0831ff406107ff271 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 26 Feb 2020 22:24:08 +0200 Subject: [PATCH 161/341] WY is tested every cycle --- Core/display.c | 1 + Core/memory.c | 5 ++++- Core/sm83_cpu.c | 9 ++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Core/display.c b/Core/display.c index c9ca8ce..908c551 100644 --- a/Core/display.c +++ b/Core/display.c @@ -382,6 +382,7 @@ void GB_lcd_off(GB_gameboy_t *gb) gb->ly_for_comparison = 0; gb->accessed_oam_row = -1; + gb->wy_triggered = false; } static void add_object_from_index(GB_gameboy_t *gb, unsigned index) diff --git a/Core/memory.c b/Core/memory.c index e1dd2d1..631b71f 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -645,6 +645,10 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (addr < 0xFF80) { /* Hardware registers */ switch (addr & 0xFF) { + case GB_IO_WY: + if (value == gb->current_line) { + gb->wy_triggered = true; + } case GB_IO_WX: case GB_IO_IF: case GB_IO_SCX: @@ -652,7 +656,6 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_IO_BGP: case GB_IO_OBP0: case GB_IO_OBP1: - case GB_IO_WY: case GB_IO_SB: case GB_IO_UNKNOWN2: case GB_IO_UNKNOWN3: diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 49115c7..b0e28f4 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -31,7 +31,6 @@ static const GB_conflict_t cgb_conflict_map[0x80] = { [GB_IO_OBP0] = GB_CONFLICT_PALETTE_CGB, [GB_IO_OBP1] = GB_CONFLICT_PALETTE_CGB, - /* Todo: most values not verified, and probably differ between revisions */ }; @@ -46,9 +45,9 @@ static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_BGP] = GB_CONFLICT_PALETTE_DMG, [GB_IO_OBP0] = GB_CONFLICT_PALETTE_DMG, [GB_IO_OBP1] = GB_CONFLICT_PALETTE_DMG, - + [GB_IO_WY] = GB_CONFLICT_READ_OLD, + /* Todo: these were not verified at all */ - [GB_IO_WY] = GB_CONFLICT_READ_NEW, [GB_IO_WX] = GB_CONFLICT_READ_NEW, [GB_IO_SCX] = GB_CONFLICT_READ_NEW, }; @@ -64,9 +63,9 @@ static const GB_conflict_t sgb_conflict_map[0x80] = { [GB_IO_BGP] = GB_CONFLICT_READ_NEW, [GB_IO_OBP0] = GB_CONFLICT_READ_NEW, [GB_IO_OBP1] = GB_CONFLICT_READ_NEW, - + [GB_IO_WY] = GB_CONFLICT_READ_OLD, + /* Todo: these were not verified at all */ - [GB_IO_WY] = GB_CONFLICT_READ_NEW, [GB_IO_WX] = GB_CONFLICT_READ_NEW, [GB_IO_SCX] = GB_CONFLICT_READ_NEW, }; From 89303ab04652fe147559772ed2b54016e08f1655 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 27 Feb 2020 00:12:42 +0200 Subject: [PATCH 162/341] WX access conflicts --- Core/display.c | 2 +- Core/gb.h | 3 +++ Core/sm83_cpu.c | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Core/display.c b/Core/display.c index 908c551..256d981 100644 --- a/Core/display.c +++ b/Core/display.c @@ -1042,7 +1042,7 @@ abort_fetching_object: // Too late to enable the window } else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6)) { + (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { gb->window_y++; if (gb->io_registers[GB_IO_WX] != 166) { gb->wx_triggered = true; diff --git a/Core/gb.h b/Core/gb.h index e02c7ad..b8cbd97 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -651,6 +651,9 @@ struct GB_gameboy_internal_s { bool vblank_just_occured; // For slow operations involving syscalls; these should only run once per vblank uint8_t cycles_since_run; // How many cycles have passed since the last call to GB_run(), in 8MHz units double clock_multiplier; + + /* Temporary state */ + bool wx_just_changed; ); }; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index b0e28f4..f25e14b 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -20,6 +20,7 @@ typedef enum { GB_CONFLICT_PALETTE_CGB, GB_CONFLICT_DMG_LCDC, GB_CONFLICT_SGB_LCDC, + GB_CONFLICT_WX, } GB_conflict_t; /* Todo: How does double speed mode affect these? */ @@ -46,9 +47,9 @@ static const GB_conflict_t dmg_conflict_map[0x80] = { [GB_IO_OBP0] = GB_CONFLICT_PALETTE_DMG, [GB_IO_OBP1] = GB_CONFLICT_PALETTE_DMG, [GB_IO_WY] = GB_CONFLICT_READ_OLD, + [GB_IO_WX] = GB_CONFLICT_WX, /* Todo: these were not verified at all */ - [GB_IO_WX] = GB_CONFLICT_READ_NEW, [GB_IO_SCX] = GB_CONFLICT_READ_NEW, }; @@ -64,9 +65,9 @@ static const GB_conflict_t sgb_conflict_map[0x80] = { [GB_IO_OBP0] = GB_CONFLICT_READ_NEW, [GB_IO_OBP1] = GB_CONFLICT_READ_NEW, [GB_IO_WY] = GB_CONFLICT_READ_OLD, + [GB_IO_WX] = GB_CONFLICT_WX, /* Todo: these were not verified at all */ - [GB_IO_WX] = GB_CONFLICT_READ_NEW, [GB_IO_SCX] = GB_CONFLICT_READ_NEW, }; @@ -261,6 +262,15 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->pending_cycles = 5; return; } + + case GB_CONFLICT_WX: + GB_advance_cycles(gb, gb->pending_cycles); + GB_write_memory(gb, addr, value); + gb->wx_just_changed = true; + GB_advance_cycles(gb, 1); + gb->wx_just_changed = false; + gb->pending_cycles = 3; + return; } } From 67d5a53503eafae61f4639bf7d4cfb24499f496e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Thu, 27 Feb 2020 18:11:10 +0100 Subject: [PATCH 163/341] Spell "length" properly --- Core/apu.c | 20 ++++++++++---------- Core/apu.h | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 17aa452..c3be533 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -230,13 +230,13 @@ static void render(GB_gameboy_t *gb) gb->apu_output.sample_callback(gb, &filtered_output); } -static uint16_t new_sweep_sample_legnth(GB_gameboy_t *gb) +static uint16_t new_sweep_sample_length(GB_gameboy_t *gb) { - uint16_t delta = gb->apu.shadow_sweep_sample_legnth >> (gb->io_registers[GB_IO_NR10] & 7); + uint16_t delta = gb->apu.shadow_sweep_sample_length >> (gb->io_registers[GB_IO_NR10] & 7); if (gb->io_registers[GB_IO_NR10] & 8) { - return gb->apu.shadow_sweep_sample_legnth - delta; + return gb->apu.shadow_sweep_sample_length - delta; } - return gb->apu.shadow_sweep_sample_legnth + delta; + return gb->apu.shadow_sweep_sample_length + delta; } static void update_square_sample(GB_gameboy_t *gb, unsigned index) @@ -400,8 +400,8 @@ void GB_apu_div_event(GB_gameboy_t *gb) if (!--gb->apu.square_sweep_countdown) { if ((gb->io_registers[GB_IO_NR10] & 0x70) && (gb->io_registers[GB_IO_NR10] & 0x07)) { gb->apu.square_channels[GB_SQUARE_1].sample_length = - gb->apu.shadow_sweep_sample_legnth = - gb->apu.new_sweep_sample_legnth; + gb->apu.shadow_sweep_sample_length = + gb->apu.new_sweep_sample_length; } if (gb->io_registers[GB_IO_NR10] & 0x70) { @@ -435,8 +435,8 @@ void GB_apu_run(GB_gameboy_t *gb) } else { /* APU bug: sweep frequency is checked after adding the sweep delta twice */ - gb->apu.new_sweep_sample_legnth = new_sweep_sample_legnth(gb); - if (gb->apu.new_sweep_sample_legnth > 0x7ff) { + gb->apu.new_sweep_sample_length = new_sweep_sample_length(gb); + if (gb->apu.new_sweep_sample_length > 0x7ff) { gb->apu.is_active[GB_SQUARE_1] = false; update_sample(gb, GB_SQUARE_1, 0, gb->apu.square_sweep_calculate_countdown - cycles); gb->apu.sweep_enabled = false; @@ -724,8 +724,8 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.square_channels[index].sample_length &= 0xFF; gb->apu.square_channels[index].sample_length |= (value & 7) << 8; if (index == GB_SQUARE_1) { - gb->apu.shadow_sweep_sample_legnth = - gb->apu.new_sweep_sample_legnth = + gb->apu.shadow_sweep_sample_length = + gb->apu.new_sweep_sample_length = gb->apu.square_channels[0].sample_length; } if (value & 0x80) { diff --git a/Core/apu.h b/Core/apu.h index 933f14e..398b903 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -64,8 +64,8 @@ typedef struct uint8_t square_sweep_countdown; // In 128Hz uint8_t square_sweep_calculate_countdown; // In 2 MHz - uint16_t new_sweep_sample_legnth; - uint16_t shadow_sweep_sample_legnth; + uint16_t new_sweep_sample_length; + uint16_t shadow_sweep_sample_length; bool sweep_enabled; bool sweep_decreasing; From 0c716bd970540e57c35868eec6e33b2ad96e3ee6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 27 Feb 2020 22:49:34 +0200 Subject: [PATCH 164/341] More accurate timing emulation of window-objects interaction --- Core/display.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/Core/display.c b/Core/display.c index 256d981..b4455fd 100644 --- a/Core/display.c +++ b/Core/display.c @@ -729,6 +729,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) return; } GB_object_t *objects = (GB_object_t *) &gb->oam; + bool window_got_activated = false; GB_STATE_MACHINE(gb, display, cycles, 2) { GB_STATE(gb, display, 1); @@ -771,6 +772,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 39); GB_STATE(gb, display, 40); GB_STATE(gb, display, 41); + GB_STATE(gb, display, 42); } @@ -948,6 +950,26 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->bg_fifo_paused = false; gb->oam_fifo_paused = false; while (true) { + /* Handle window */ + /* Todo: verify timings */ + if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { + if (gb->io_registers[GB_IO_WX] >= 167) { + // Too late to enable the window + } + else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || + (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { + gb->window_y++; + if (gb->io_registers[GB_IO_WX] != 166) { + gb->wx_triggered = true; + fifo_clear(&gb->bg_fifo); + gb->bg_fifo_paused = true; + gb->oam_fifo_paused = true; + gb->fetcher_state = 1; + window_got_activated = true; + } + } + } + /* Handle objects */ /* When the sprite enabled bit is off, this proccess is skipped entirely on the DMG, but not on the CGB. On the CGB, this bit is checked only when the pixel is actually popped from the FIFO. */ @@ -962,6 +984,15 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) while (gb->n_visible_objs != 0 && (gb->io_registers[GB_IO_LCDC] & 2 || GB_is_cgb(gb)) && gb->obj_comparators[gb->n_visible_objs - 1] == (uint8_t)(gb->position_in_line + 8)) { + + /* TODO: This is wrong. It is only correct for a single object, not for more than one. */ + if (window_got_activated) { + window_got_activated = false; + gb->fetcher_state = 0; + gb->cycles_for_line += 6; + GB_SLEEP(gb, display, 42, 6); + } + while (gb->fetcher_state < 5) { advance_fetcher_state_machine(gb); gb->cycles_for_line++; @@ -1035,25 +1066,6 @@ abort_fetching_object: gb->object_fetch_aborted = false; gb->during_object_fetch = false; - /* Handle window */ - /* Todo: verify timings */ - if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { - if (gb->io_registers[GB_IO_WX] >= 167) { - // Too late to enable the window - } - else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { - gb->window_y++; - if (gb->io_registers[GB_IO_WX] != 166) { - gb->wx_triggered = true; - fifo_clear(&gb->bg_fifo); - gb->bg_fifo_paused = true; - gb->oam_fifo_paused = true; - gb->fetcher_state = 1; - } - } - } - render_pixel_if_possible(gb); advance_fetcher_state_machine(gb); From 40868df7595af9d5e468307337a0e1f688965997 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 28 Feb 2020 14:05:29 +0200 Subject: [PATCH 165/341] Fix this bug again --- Cocoa/Document.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 0418c56..03d4acd 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -309,16 +309,17 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_set_sample_rate(&gb, 96000); self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) { [audioLock lock]; - if (stopping) { - memset(buffer, 0, nFrames * sizeof(*buffer)); - [audioLock unlock]; - } if (audioBufferPosition < nFrames) { audioBufferNeeded = nFrames; [audioLock wait]; } + if (stopping) { + memset(buffer, 0, nFrames * sizeof(*buffer)); + [audioLock unlock]; + } + if (audioBufferPosition >= nFrames && audioBufferPosition < nFrames + 4800) { memcpy(buffer, audioBuffer, nFrames * sizeof(*buffer)); memmove(audioBuffer, audioBuffer + nFrames, (audioBufferPosition - nFrames) * sizeof(*buffer)); @@ -376,6 +377,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } [audioLock lock]; stopping = true; + [audioLock signal]; [audioLock unlock]; running = false; while (stopping); From 2a8f15c68be9b159d8536958ed0830fe58355189 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 28 Feb 2020 18:10:09 +0200 Subject: [PATCH 166/341] The fetcher pushes pixels to the FIFO as soon as it's empty --- Core/display.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Core/display.c b/Core/display.c index b4455fd..d6acaf3 100644 --- a/Core/display.c +++ b/Core/display.c @@ -656,7 +656,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) } } gb->fetcher_state++; - break; + // fallthrough case GB_FETCHER_PUSH: { @@ -964,7 +964,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_clear(&gb->bg_fifo); gb->bg_fifo_paused = true; gb->oam_fifo_paused = true; - gb->fetcher_state = 1; + gb->fetcher_state = 0; window_got_activated = true; } } @@ -988,7 +988,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* TODO: This is wrong. It is only correct for a single object, not for more than one. */ if (window_got_activated) { window_got_activated = false; - gb->fetcher_state = 0; gb->cycles_for_line += 6; GB_SLEEP(gb, display, 42, 6); } From e29246fd914b340b2408ae85059c61293c76f7fd Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 28 Feb 2020 18:28:47 +0200 Subject: [PATCH 167/341] Window tile is reset on WX trigger --- Core/display.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index d6acaf3..9aaf40b 100644 --- a/Core/display.c +++ b/Core/display.c @@ -853,7 +853,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) (gb->current_line != 0 && gb->io_registers[GB_IO_WY] == gb->current_line - 1))) { gb->wy_triggered = true; } - gb->window_tile_x = 0; gb->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed; gb->accessed_oam_row = 0; @@ -961,6 +960,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->window_y++; if (gb->io_registers[GB_IO_WX] != 166) { gb->wx_triggered = true; + gb->window_tile_x = 0; fifo_clear(&gb->bg_fifo); gb->bg_fifo_paused = true; gb->oam_fifo_paused = true; From 955860b4631d4dac7393c0317b6fd22d42fad391 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 28 Feb 2020 22:36:51 +0200 Subject: [PATCH 168/341] Get rid of the FIFO pause flags --- Core/display.c | 26 ++++++++++---------------- Core/gb.h | 6 +++--- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Core/display.c b/Core/display.c index 9aaf40b..85fc858 100644 --- a/Core/display.c +++ b/Core/display.c @@ -422,20 +422,21 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) bool draw_oam = false; bool bg_enabled = true, bg_priority = false; - if (!gb->bg_fifo_paused) { + if (fifo_size(&gb->bg_fifo)) { fifo_item = fifo_pop(&gb->bg_fifo); bg_priority = fifo_item->bg_priority; - } - - if (!gb->oam_fifo_paused && fifo_size(&gb->oam_fifo)) { - oam_fifo_item = fifo_pop(&gb->oam_fifo); - if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & 2)) { - draw_oam = true; - bg_priority |= oam_fifo_item->bg_priority; + + if (fifo_size(&gb->oam_fifo)) { + oam_fifo_item = fifo_pop(&gb->oam_fifo); + if (oam_fifo_item->pixel && (gb->io_registers[GB_IO_LCDC] & 2)) { + draw_oam = true; + bg_priority |= oam_fifo_item->bg_priority; + } } } - if (gb->bg_fifo_paused) return; + + if (!fifo_item) return; /* Drop pixels for scrollings */ if (gb->position_in_line >= 160 || (gb->disable_rendering && !gb->sgb)) { @@ -658,7 +659,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->fetcher_state++; // fallthrough - case GB_FETCHER_PUSH: { if (fifo_size(&gb->bg_fifo) > 0) break; @@ -673,8 +673,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) fifo_push_bg_row(&gb->bg_fifo, gb->current_tile_data[0], gb->current_tile_data[1], gb->current_tile_attributes & 7, gb->current_tile_attributes & 0x80, gb->current_tile_attributes & 0x20); - gb->bg_fifo_paused = false; - gb->oam_fifo_paused = false; gb->fetcher_state = 0; } break; @@ -946,8 +944,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* The actual rendering cycle */ gb->fetcher_state = 0; - gb->bg_fifo_paused = false; - gb->oam_fifo_paused = false; while (true) { /* Handle window */ /* Todo: verify timings */ @@ -962,8 +958,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->wx_triggered = true; gb->window_tile_x = 0; fifo_clear(&gb->bg_fifo); - gb->bg_fifo_paused = true; - gb->oam_fifo_paused = true; gb->fetcher_state = 0; window_got_activated = true; } diff --git a/Core/gb.h b/Core/gb.h index b8cbd97..65cfb4c 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -489,7 +489,7 @@ struct GB_gameboy_internal_s { bool vram_read_blocked; bool oam_write_blocked; bool vram_write_blocked; - GB_PADDING(bool, window_disabled_while_active); + bool fifo_insertion_glitch; uint8_t current_line; uint16_t ly_for_comparison; GB_fifo_t bg_fifo, oam_fifo; @@ -500,8 +500,8 @@ struct GB_gameboy_internal_s { uint8_t current_tile_attributes; uint8_t current_tile_data[2]; uint8_t fetcher_state; - bool bg_fifo_paused; - bool oam_fifo_paused; + GB_PADDING(bool,bg_fifo_paused); + GB_PADDING(bool,oam_fifo_paused); bool wx_triggered; uint8_t visible_objs[10]; uint8_t obj_comparators[10]; From 39b999a68b1b7e34d03efebe0d6bbb931c84de22 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 29 Feb 2020 17:06:08 +0200 Subject: [PATCH 169/341] Emulate the FIFO insertion glitch (WX variant) --- Core/display.c | 11 +++++++++++ Core/gb.h | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index 85fc858..e01c789 100644 --- a/Core/display.c +++ b/Core/display.c @@ -524,6 +524,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } gb->position_in_line++; + gb->window_is_being_fetched = false; } /* All verified CGB timings are based on CGB CPU E. CGB CPUs >= D are known to have @@ -960,9 +961,19 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_clear(&gb->bg_fifo); gb->fetcher_state = 0; window_got_activated = true; + gb->window_is_being_fetched = true; } } } + + if (!GB_is_cgb(gb) && gb->wx_triggered && !gb->window_is_being_fetched && + gb->fetcher_state == 0 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) ) { + // Insert a pixel right at the FIFO's end + gb->bg_fifo.read_end--; + gb->bg_fifo.read_end &= GB_FIFO_LENGTH - 1; + gb->bg_fifo.fifo[gb->bg_fifo.read_end] = (GB_fifo_item_t){0,}; + gb->window_is_being_fetched = false; + } /* Handle objects */ /* When the sprite enabled bit is off, this proccess is skipped entirely on the DMG, but not on the CGB. diff --git a/Core/gb.h b/Core/gb.h index 65cfb4c..15c6442 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -500,7 +500,7 @@ struct GB_gameboy_internal_s { uint8_t current_tile_attributes; uint8_t current_tile_data[2]; uint8_t fetcher_state; - GB_PADDING(bool,bg_fifo_paused); + bool window_is_being_fetched; GB_PADDING(bool,oam_fifo_paused); bool wx_triggered; uint8_t visible_objs[10]; From 5ca602fbd2bb8c76d4635082ab97a4bcd2ad169d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 29 Feb 2020 18:26:16 +0200 Subject: [PATCH 170/341] WX=0 emulation --- Core/display.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index e01c789..3250103 100644 --- a/Core/display.c +++ b/Core/display.c @@ -772,7 +772,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 40); GB_STATE(gb, display, 41); GB_STATE(gb, display, 42); - + GB_STATE(gb, display, 43); } if (!(gb->io_registers[GB_IO_LCDC] & 0x80)) { @@ -948,14 +948,21 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) while (true) { /* Handle window */ /* Todo: verify timings */ + static const uint8_t scx_to_wx0_comparisons[] = {-7, -9, -10, -11, -12, -13, -14, -14}; if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { if (gb->io_registers[GB_IO_WX] >= 167) { // Too late to enable the window } else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { + (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) || + (gb->io_registers[GB_IO_WX] == 0 && (gb->position_in_line) == scx_to_wx0_comparisons[gb->io_registers[GB_IO_SCX] & 7])) { gb->window_y++; if (gb->io_registers[GB_IO_WX] != 166) { + /* TODO: Verify fetcher access timings in this case */ + if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7)) { + gb->cycles_for_line++; + GB_SLEEP(gb, display, 43, 1); + } gb->wx_triggered = true; gb->window_tile_x = 0; fifo_clear(&gb->bg_fifo); From b7194402eb531fdea0fe83eaabc1c814337c3057 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 1 Mar 2020 00:17:45 +0200 Subject: [PATCH 171/341] Accurately emulate Window X = Object X --- Core/display.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Core/display.c b/Core/display.c index 3250103..3310685 100644 --- a/Core/display.c +++ b/Core/display.c @@ -728,7 +728,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) return; } GB_object_t *objects = (GB_object_t *) &gb->oam; - bool window_got_activated = false; GB_STATE_MACHINE(gb, display, cycles, 2) { GB_STATE(gb, display, 1); @@ -772,7 +771,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 40); GB_STATE(gb, display, 41); GB_STATE(gb, display, 42); - GB_STATE(gb, display, 43); } if (!(gb->io_registers[GB_IO_LCDC] & 0x80)) { @@ -961,13 +959,12 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* TODO: Verify fetcher access timings in this case */ if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7)) { gb->cycles_for_line++; - GB_SLEEP(gb, display, 43, 1); + GB_SLEEP(gb, display, 42, 1); } gb->wx_triggered = true; gb->window_tile_x = 0; fifo_clear(&gb->bg_fifo); gb->fetcher_state = 0; - window_got_activated = true; gb->window_is_being_fetched = true; } } @@ -997,14 +994,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) (gb->io_registers[GB_IO_LCDC] & 2 || GB_is_cgb(gb)) && gb->obj_comparators[gb->n_visible_objs - 1] == (uint8_t)(gb->position_in_line + 8)) { - /* TODO: This is wrong. It is only correct for a single object, not for more than one. */ - if (window_got_activated) { - window_got_activated = false; - gb->cycles_for_line += 6; - GB_SLEEP(gb, display, 42, 6); - } - - while (gb->fetcher_state < 5) { + while (gb->fetcher_state < 5 || fifo_size(&gb->bg_fifo) == 0) { advance_fetcher_state_machine(gb); gb->cycles_for_line++; GB_SLEEP(gb, display, 27, 1); From 2a8b26d5e6210bb8c956a25b288344342f5324ca Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 1 Mar 2020 00:23:50 +0200 Subject: [PATCH 172/341] Add TODO --- Core/display.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Core/display.c b/Core/display.c index 3310685..fef764d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -970,6 +970,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } } + /* TODO: What happens when WX=0? */ if (!GB_is_cgb(gb) && gb->wx_triggered && !gb->window_is_being_fetched && gb->fetcher_state == 0 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) ) { // Insert a pixel right at the FIFO's end From e846f4f3b0ab2836e5f16501d547d7fc5e9aea53 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 1 Mar 2020 23:58:28 +0200 Subject: [PATCH 173/341] Hacky, but correct emulation of WX=166 --- Core/display.c | 72 +++++++++++++++++++++++++++++++++++--------------- Core/gb.h | 2 +- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/Core/display.c b/Core/display.c index fef764d..ce9f74f 100644 --- a/Core/display.c +++ b/Core/display.c @@ -564,6 +564,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) if (!(gb->io_registers[GB_IO_LCDC] & 0x20)) { gb->wx_triggered = false; + gb->wx166_glitch = false; } /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ @@ -838,13 +839,13 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->vram_read_blocked = true; gb->vram_write_blocked = true; gb->wx_triggered = false; + gb->wx166_glitch = false; goto mode_3_start; while (true) { /* Lines 0 - 143 */ gb->window_y = -1; for (; gb->current_line < LINES; gb->current_line++) { - gb->wx_triggered = false; /* Todo: verify timings */ if ((gb->io_registers[GB_IO_WY] == gb->current_line || (gb->current_line != 0 && gb->io_registers[GB_IO_WY] == gb->current_line - 1))) { @@ -945,29 +946,48 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->fetcher_state = 0; while (true) { /* Handle window */ - /* Todo: verify timings */ - static const uint8_t scx_to_wx0_comparisons[] = {-7, -9, -10, -11, -12, -13, -14, -14}; + /* TODO: It appears that WX checks if the window beings *next* pixel, not *this* pixel. For this reason, + WX=167 has no effect at all (It checks if the PPU X position is 161, which never happens) and WX=166 + has weird artifacts (It appears to activate the window during HBlank, as PPU X is temporarily 160 at + that point. The code should be updated to represent this, and this will fix the time travel hack in + WX's access conflict code. */ + if (!gb->wx_triggered && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20)) { - if (gb->io_registers[GB_IO_WX] >= 167) { - // Too late to enable the window - } - else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) || - (gb->io_registers[GB_IO_WX] == 0 && (gb->position_in_line) == scx_to_wx0_comparisons[gb->io_registers[GB_IO_SCX] & 7])) { - gb->window_y++; - if (gb->io_registers[GB_IO_WX] != 166) { - /* TODO: Verify fetcher access timings in this case */ - if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7)) { - gb->cycles_for_line++; - GB_SLEEP(gb, display, 42, 1); - } - gb->wx_triggered = true; - gb->window_tile_x = 0; - fifo_clear(&gb->bg_fifo); - gb->fetcher_state = 0; - gb->window_is_being_fetched = true; + bool should_activate_window = false; + if (gb->io_registers[GB_IO_WX] == 0) { + static const uint8_t scx_to_wx0_comparisons[] = {-7, -9, -10, -11, -12, -13, -14, -14}; + if (gb->position_in_line == scx_to_wx0_comparisons[gb->io_registers[GB_IO_SCX] & 7]) { + should_activate_window = true; } } + else if (gb->wx166_glitch) { + static const uint8_t scx_to_wx166_comparisons[] = {-8, -9, -10, -11, -12, -13, -14, -15}; + if (gb->position_in_line == scx_to_wx166_comparisons[gb->io_registers[GB_IO_SCX] & 7]) { + should_activate_window = true; + } + } + else if (gb->io_registers[GB_IO_WX] < 166 + GB_is_cgb(gb)) { + if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || + (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) + should_activate_window = true; + } + + if (should_activate_window) { + gb->window_y++; + /* TODO: Verify fetcher access timings in this case */ + if (gb->io_registers[GB_IO_WX] == 0 && (gb->io_registers[GB_IO_SCX] & 7)) { + gb->cycles_for_line++; + GB_SLEEP(gb, display, 42, 1); + } + gb->wx_triggered = true; + gb->window_tile_x = 0; + fifo_clear(&gb->bg_fifo); + gb->fetcher_state = 0; + gb->window_is_being_fetched = true; + } + else if (!GB_is_cgb(gb) && gb->io_registers[GB_IO_WX] == 166 && gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { + gb->window_y++; + } } /* TODO: What happens when WX=0? */ @@ -1075,6 +1095,14 @@ abort_fetching_object: gb->cycles_for_line++; GB_SLEEP(gb, display, 21, 1); } + /* TODO: Verify timing */ + if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20) && gb->io_registers[GB_IO_WX] == 166) { + gb->wx166_glitch = true; + } + else { + gb->wx166_glitch = false; + } + gb->wx_triggered = false; if (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_C) { gb->cycles_for_line++; @@ -1129,7 +1157,7 @@ abort_fetching_object: gb->icd_hreset_callback(gb); } } - + gb->wx166_glitch = false; /* Lines 144 - 152 */ for (; gb->current_line < VIRTUAL_LINES - 1; gb->current_line++) { gb->io_registers[GB_IO_LY] = gb->current_line; diff --git a/Core/gb.h b/Core/gb.h index 15c6442..db99608 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -501,7 +501,7 @@ struct GB_gameboy_internal_s { uint8_t current_tile_data[2]; uint8_t fetcher_state; bool window_is_being_fetched; - GB_PADDING(bool,oam_fifo_paused); + bool wx166_glitch; bool wx_triggered; uint8_t visible_objs[10]; uint8_t obj_comparators[10]; From 409ab2a6d4ae64a3211a568acdb9a7f42a249fcd Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 3 Mar 2020 02:21:19 +0200 Subject: [PATCH 174/341] Accurate emulation of tilemap advancement timings --- Core/display.c | 34 ++++++++++++++++++++-------------- Core/memory.c | 4 ++++ Core/save_state.c | 2 -- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Core/display.c b/Core/display.c index ce9f74f..cbf4196 100644 --- a/Core/display.c +++ b/Core/display.c @@ -547,7 +547,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_SLEEP, } fetcher_step_t; - fetcher_step_t fetcher_state_machine [8] = { + fetcher_step_t fetcher_state_machine [9] = { GB_FETCHER_SLEEP, GB_FETCHER_GET_TILE, GB_FETCHER_SLEEP, @@ -555,9 +555,13 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_SLEEP, GB_FETCHER_GET_TILE_DATA_HIGH, GB_FETCHER_PUSH, - GB_FETCHER_PUSH, // Compatibility + GB_FETCHER_PUSH, + GB_FETCHER_PUSH, }; - + + if (gb->fetcher_state >= sizeof(fetcher_state_machine)) { + gb->fetcher_state = 0; + } switch (fetcher_state_machine[gb->fetcher_state]) { case GB_FETCHER_GET_TILE: { uint16_t map = 0x1800; @@ -659,19 +663,23 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) } } gb->fetcher_state++; + if (gb->wx_triggered) { + gb->window_tile_x++; + gb->window_tile_x &= 0x1f; + } + // fallthrough - case GB_FETCHER_PUSH: { - if (fifo_size(&gb->bg_fifo) > 0) break; - - if (gb->wx_triggered) { - gb->window_tile_x++; - gb->window_tile_x &= 0x1f; - } - else { + if (gb->fetcher_state == 7) { + /* The background map index increase at this specific point. If this state is not reached, + it will simply not increase. */ gb->fetcher_x++; gb->fetcher_x &= 0x1f; } + if (gb->fetcher_state < 8) { + gb->fetcher_state++; + } + if (fifo_size(&gb->bg_fifo) > 0) break; fifo_push_bg_row(&gb->bg_fifo, gb->current_tile_data[0], gb->current_tile_data[1], gb->current_tile_attributes & 7, gb->current_tile_attributes & 0x80, gb->current_tile_attributes & 0x20); @@ -685,8 +693,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) } break; } - - gb->fetcher_state &= 7; } static uint16_t get_object_line_address(GB_gameboy_t *gb, const GB_object_t *object) @@ -946,7 +952,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->fetcher_state = 0; while (true) { /* Handle window */ - /* TODO: It appears that WX checks if the window beings *next* pixel, not *this* pixel. For this reason, + /* TODO: It appears that WX checks if the window begins *next* pixel, not *this* pixel. For this reason, WX=167 has no effect at all (It checks if the PPU X position is 161, which never happens) and WX=166 has weird artifacts (It appears to activate the window during HBlank, as PPU X is temporarily 160 at that point. The code should be updated to represent this, and this will fix the time travel hack in diff --git a/Core/memory.c b/Core/memory.c index 631b71f..abad063 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -742,6 +742,10 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) GB_lcd_off(gb); } gb->io_registers[GB_IO_LCDC] = value; + if (!(value & 0x20)) { + gb->wx_triggered = false; + gb->wx166_glitch = false; + } return; case GB_IO_STAT: diff --git a/Core/save_state.c b/Core/save_state.c index fd7d814..8f10152 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -267,7 +267,6 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; gb->fetcher_x &= 0x1f; - gb->fetcher_state &= 7; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; @@ -379,7 +378,6 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le gb->oam_fifo.write_end &= 0xF; gb->object_low_line_address &= gb->vram_size & ~1; gb->fetcher_x &= 0x1f; - gb->fetcher_state &= 7; if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; From 1c7351fc8511592454bd8f15e2f28ae836db586a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 4 Mar 2020 23:34:36 +0200 Subject: [PATCH 175/341] Missing braces --- Core/display.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index cbf4196..0ec436d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -974,8 +974,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } else if (gb->io_registers[GB_IO_WX] < 166 + GB_is_cgb(gb)) { if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) - should_activate_window = true; + (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { + should_activate_window = true; + } } if (should_activate_window) { From 4d2f56c42db1ddb6df8498d1dfd140a7794c6cb7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 4 Mar 2020 23:43:05 +0200 Subject: [PATCH 176/341] Minor bug fix --- Core/display.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Core/display.c b/Core/display.c index 0ec436d..392cd93 100644 --- a/Core/display.c +++ b/Core/display.c @@ -547,7 +547,7 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_SLEEP, } fetcher_step_t; - fetcher_step_t fetcher_state_machine [9] = { + fetcher_step_t fetcher_state_machine [8] = { GB_FETCHER_SLEEP, GB_FETCHER_GET_TILE, GB_FETCHER_SLEEP, @@ -556,13 +556,8 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) GB_FETCHER_GET_TILE_DATA_HIGH, GB_FETCHER_PUSH, GB_FETCHER_PUSH, - GB_FETCHER_PUSH, }; - - if (gb->fetcher_state >= sizeof(fetcher_state_machine)) { - gb->fetcher_state = 0; - } - switch (fetcher_state_machine[gb->fetcher_state]) { + switch (fetcher_state_machine[gb->fetcher_state & 7]) { case GB_FETCHER_GET_TILE: { uint16_t map = 0x1800; @@ -662,7 +657,6 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) gb->current_tile_data[1] = 0xFF; } } - gb->fetcher_state++; if (gb->wx_triggered) { gb->window_tile_x++; gb->window_tile_x &= 0x1f; @@ -670,13 +664,13 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) // fallthrough case GB_FETCHER_PUSH: { - if (gb->fetcher_state == 7) { + if (gb->fetcher_state == 6) { /* The background map index increase at this specific point. If this state is not reached, it will simply not increase. */ gb->fetcher_x++; gb->fetcher_x &= 0x1f; } - if (gb->fetcher_state < 8) { + if (gb->fetcher_state < 7) { gb->fetcher_state++; } if (fifo_size(&gb->bg_fifo) > 0) break; From c6f9d051240cd83de0772f8112da118cde7ee930 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 14:41:13 +0200 Subject: [PATCH 177/341] Emulate LCD-PPU horizontal desync on DMGs --- Core/display.c | 38 ++++++++++++++++++++++----- Core/gb.h | 1 + Core/save_state.c | 66 ++++++++++++++++++++--------------------------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/Core/display.c b/Core/display.c index 392cd93..8072f37 100644 --- a/Core/display.c +++ b/Core/display.c @@ -459,10 +459,10 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) uint32_t *dest = NULL; if (!gb->sgb) { if (gb->border_mode != GB_BORDER_ALWAYS) { - dest = gb->screen + gb->position_in_line + gb->current_line * WIDTH; + dest = gb->screen + gb->lcd_x + gb->current_line * WIDTH; } else { - dest = gb->screen + gb->position_in_line + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH; + dest = gb->screen + gb->lcd_x + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH; } } @@ -476,7 +476,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } if (gb->sgb) { if (gb->current_lcd_line < LINES) { - gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; + gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; } } else if (gb->model & GB_MODEL_NO_SFC_BIT) { @@ -500,7 +500,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } if (gb->sgb) { if (gb->current_lcd_line < LINES) { - gb->sgb->screen_buffer[gb->position_in_line + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; + gb->sgb->screen_buffer[gb->lcd_x + gb->current_lcd_line * WIDTH] = gb->stopped? 0 : pixel; } } else if (gb->model & GB_MODEL_NO_SFC_BIT) { @@ -524,6 +524,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) } gb->position_in_line++; + gb->lcd_x++; gb->window_is_being_fetched = false; } @@ -937,6 +938,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) fifo_push_bg_row(&gb->bg_fifo, 0, 0, 0, false, false); /* Todo: find out actual access time of SCX */ gb->position_in_line = - (gb->io_registers[GB_IO_SCX] & 7) - 8; + gb->lcd_x = 0; gb->fetcher_x = 0; gb->extra_penalty_for_sprite_at_0 = (gb->io_registers[GB_IO_SCX] & 7); @@ -967,10 +969,19 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) } } else if (gb->io_registers[GB_IO_WX] < 166 + GB_is_cgb(gb)) { - if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7) || - (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed)) { + if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 7)) { should_activate_window = true; } + else if (gb->io_registers[GB_IO_WX] == (uint8_t) (gb->position_in_line + 6) && !gb->wx_just_changed) { + should_activate_window = true; + /* LCD-PPU horizontal desync! It only appears to happen on DMGs, but not all of them. + This doesn't seem to be CPU revision dependent, but most revisions */ + if ((gb->model & GB_MODEL_FAMILY_MASK) == GB_MODEL_DMG_FAMILY && !GB_is_sgb(gb)) { + if (gb->lcd_x > 0) { + gb->lcd_x--; + } + } + } } if (should_activate_window) { @@ -1096,6 +1107,21 @@ abort_fetching_object: gb->cycles_for_line++; GB_SLEEP(gb, display, 21, 1); } + + while (gb->lcd_x != 160 && !gb->disable_rendering && gb->screen && !gb->sgb) { + /* Oh no! The PPU and LCD desynced! Fill the rest of the line whith white. */ + uint32_t *dest = NULL; + if (gb->border_mode != GB_BORDER_ALWAYS) { + dest = gb->screen + gb->lcd_x + gb->current_line * WIDTH; + } + else { + dest = gb->screen + gb->lcd_x + gb->current_line * BORDERED_WIDTH + (BORDERED_WIDTH - WIDTH) / 2 + (BORDERED_HEIGHT - LINES) / 2 * BORDERED_WIDTH; + } + *dest = gb->background_palettes_rgb[0]; + gb->lcd_x++; + + } + /* TODO: Verify timing */ if (!GB_is_cgb(gb) && gb->wy_triggered && (gb->io_registers[GB_IO_LCDC] & 0x20) && gb->io_registers[GB_IO_WX] == 166) { gb->wx166_glitch = true; diff --git a/Core/gb.h b/Core/gb.h index db99608..88e2991 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -523,6 +523,7 @@ struct GB_gameboy_internal_s { uint16_t object_low_line_address; bool wy_triggered; uint8_t window_tile_x; + uint8_t lcd_x; // The LCD can go out of sync since the push signal is skipped in some cases. ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/save_state.c b/Core/save_state.c index 8f10152..a53ccba 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -180,6 +180,32 @@ static bool verify_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t *save) return true; } +static void sanitize_state(GB_gameboy_t *gb) +{ + if (gb->cartridge_type->has_rumble && gb->rumble_callback) { + gb->rumble_callback(gb, gb->rumble_state); + } + + for (unsigned i = 0; i < 32; i++) { + GB_palette_changed(gb, false, i * 2); + GB_palette_changed(gb, true, i * 2); + } + + gb->bg_fifo.read_end &= 0xF; + gb->bg_fifo.write_end &= 0xF; + gb->oam_fifo.read_end &= 0xF; + gb->oam_fifo.write_end &= 0xF; + gb->object_low_line_address &= gb->vram_size & ~1; + gb->fetcher_x &= 0x1f; + if (gb->lcd_x > gb->position_in_line) { + gb->lcd_x = gb->position_in_line; + } + + if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { + gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; + } +} + #define READ_SECTION(gb, f, section) read_section(f, GB_GET_SECTION(gb, section), GB_SECTION_SIZE(section)) int GB_load_state(GB_gameboy_t *gb, const char *path) @@ -252,25 +278,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path) errno = 0; - if (gb->cartridge_type->has_rumble && gb->rumble_callback) { - gb->rumble_callback(gb, gb->rumble_state); - } - - for (unsigned i = 0; i < 32; i++) { - GB_palette_changed(gb, false, i * 2); - GB_palette_changed(gb, true, i * 2); - } - - gb->bg_fifo.read_end &= 0xF; - gb->bg_fifo.write_end &= 0xF; - gb->oam_fifo.read_end &= 0xF; - gb->oam_fifo.write_end &= 0xF; - gb->object_low_line_address &= gb->vram_size & ~1; - gb->fetcher_x &= 0x1f; - - if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { - gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; - } + sanitize_state(gb); error: fclose(f); @@ -363,25 +371,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le memcpy(gb, &save, sizeof(save)); - if (gb->cartridge_type->has_rumble && gb->rumble_callback) { - gb->rumble_callback(gb, gb->rumble_state); - } - - for (unsigned i = 0; i < 32; i++) { - GB_palette_changed(gb, false, i * 2); - GB_palette_changed(gb, true, i * 2); - } - - gb->bg_fifo.read_end &= 0xF; - gb->bg_fifo.write_end &= 0xF; - gb->oam_fifo.read_end &= 0xF; - gb->oam_fifo.write_end &= 0xF; - gb->object_low_line_address &= gb->vram_size & ~1; - gb->fetcher_x &= 0x1f; - - if (gb->object_priority == GB_OBJECT_PRIORITY_UNDEFINED) { - gb->object_priority = gb->cgb_mode? GB_OBJECT_PRIORITY_INDEX : GB_OBJECT_PRIORITY_X; - } + sanitize_state(gb); return 0; } From 78b552fe822e54140f4d52942fb955fe995e7b34 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 17:37:04 +0200 Subject: [PATCH 178/341] More attempts to fix this bug --- Cocoa/Document.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 03d4acd..8264fb3 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -318,6 +318,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) if (stopping) { memset(buffer, 0, nFrames * sizeof(*buffer)); [audioLock unlock]; + return; } if (audioBufferPosition >= nFrames && audioBufferPosition < nFrames + 4800) { @@ -380,7 +381,11 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [audioLock signal]; [audioLock unlock]; running = false; - while (stopping); + while (stopping) { + [audioLock lock]; + [audioLock signal]; + [audioLock unlock]; + } GB_debugger_set_disabled(&gb, false); } From ee939a378258b647fa95a6c61aaf3314d6b69f42 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 17:37:18 +0200 Subject: [PATCH 179/341] New boot ROM animation in the DMG boot ROM --- BootROMs/dmg_boot.asm | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/BootROMs/dmg_boot.asm b/BootROMs/dmg_boot.asm index 6fb74fb..97a12e7 100644 --- a/BootROMs/dmg_boot.asm +++ b/BootROMs/dmg_boot.asm @@ -24,7 +24,7 @@ Start: ldh [$24], a ; Init BG palette - ld a, $fc + ld a, $54 ldh [$47], a ; Load logo from ROM. @@ -69,14 +69,36 @@ Start: jr .tilemapLoop .tilemapDone + ld a, 30 + ldh [$ff42], a + ; Turn on LCD ld a, $91 ldh [$40], a -; Wait ~0.75 seconds - ld b, 45 - call WaitBFrames - + ld d, (-119) & $FF + ld c, 15 + +.animate + call WaitFrame + ld a, d + sra a + sra a + ldh [$ff42], a + ld a, d + add c + ld d, a + ld a, c + cp 8 + jr nz, .noPaletteChange + ld a, $A8 + ldh [$47], a +.noPaletteChange + dec c + jr nz, .animate + ld a, $fc + ldh [$47], a + ; Play first sound ld a, $83 call PlaySound @@ -85,9 +107,11 @@ Start: ; Play second sound ld a, $c1 call PlaySound + -; Wait ~1.15 seconds - ld b, 70 + +; Wait ~1 second + ld b, 60 call WaitBFrames ; Set registers to match the original DMG boot From 4963ec4cc43529adec95efe79a5e24d9cd5aaf65 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 17:37:29 +0200 Subject: [PATCH 180/341] Gamma correction in the CRT shader --- Shaders/CRT.fsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Shaders/CRT.fsh b/Shaders/CRT.fsh index 8684451..c1ae7ef 100644 --- a/Shaders/CRT.fsh +++ b/Shaders/CRT.fsh @@ -158,6 +158,8 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou ret *= output_resolution.y - pixel_position.y; } + // Gamma correction + ret = pow(ret, 0.72); return ret; } From fe7667a00c92eca2089d1b8058123eb452443e51 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 17:37:47 +0200 Subject: [PATCH 181/341] Add drop shadows to the Monochrome LCD shader --- Shaders/MonoLCD.fsh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Shaders/MonoLCD.fsh b/Shaders/MonoLCD.fsh index 1a641af..009e1db 100644 --- a/Shaders/MonoLCD.fsh +++ b/Shaders/MonoLCD.fsh @@ -34,5 +34,17 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou multiplier *= (1.0 - sub_pos.x) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH); } - return mix(texture(image, position) * multiplier, mix(r1, r2, s.y), BLOOM); + vec4 pre_shadow = mix(texture(image, position) * multiplier, mix(r1, r2, s.y), BLOOM); + pixel += vec2(-0.6, -0.8); + + q11 = texture(image, (floor(pixel) + 0.5) / input_resolution); + q12 = texture(image, (vec2(floor(pixel.x), ceil(pixel.y)) + 0.5) / input_resolution); + q21 = texture(image, (vec2(ceil(pixel.x), floor(pixel.y)) + 0.5) / input_resolution); + q22 = texture(image, (ceil(pixel) + 0.5) / input_resolution); + + r1 = mix(q11, q21, fract(pixel.x)); + r2 = mix(q12, q22, fract(pixel.x)); + + vec4 shadow = mix(r1, r2, fract(pixel.y)); + return mix(min(shadow, pre_shadow), pre_shadow, 0.75); } From 34cf0f558da9f716b277aff5a0bbea6730c1e08c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 18:56:51 +0200 Subject: [PATCH 182/341] It's more reasonable to do it this way --- Core/memory.c | 8 ++++++++ Core/sm83_cpu.c | 17 ----------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index abad063..fccfd86 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -741,6 +741,14 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) GB_timing_sync(gb); GB_lcd_off(gb); } + /* Handle disabling objects while already fetching an object */ + if ((gb->io_registers[GB_IO_LCDC] & 2) && !(value & 2)) { + if (gb->during_object_fetch) { + gb->cycles_for_line += gb->display_cycles; + gb->display_cycles = 0; + gb->object_fetch_aborted = true; + } + } gb->io_registers[GB_IO_LCDC] = value; if (!(value & 0x20)) { gb->wx_triggered = false; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index f25e14b..73e2ee1 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -208,29 +208,12 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) uint8_t old_value = GB_read_memory(gb, addr); GB_advance_cycles(gb, gb->pending_cycles - 2); - /* Handle disabling objects while already fetching an object */ - if ((old_value & 2) && !(value & 2)) { - if (gb->during_object_fetch) { - gb->cycles_for_line += gb->display_cycles; - gb->display_cycles = 0; - gb->object_fetch_aborted = true; - } - } - if (/* gb->model != GB_MODEL_MGB && */ gb->position_in_line == 0 && (old_value & 2) && !(value & 2)) { old_value &= ~2; } GB_write_memory(gb, addr, old_value | (value & 1)); GB_advance_cycles(gb, 1); - /* Handle disabling objects while already fetching an object */ - if ((old_value & 2) && !(value & 2)) { - if (gb->during_object_fetch) { - gb->cycles_for_line += gb->display_cycles; - gb->display_cycles = 0; - gb->object_fetch_aborted = true; - } - } GB_write_memory(gb, addr, value); gb->pending_cycles = 5; return; From e7f6ac8828dc021fcb11389dc787cca1beb84412 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 6 Mar 2020 21:19:53 +0200 Subject: [PATCH 183/341] Do the same for SGB --- Core/sm83_cpu.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 73e2ee1..da980eb 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -224,23 +224,10 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) uint8_t old_value = GB_read_memory(gb, addr); GB_advance_cycles(gb, gb->pending_cycles - 2); - /* Handle disabling objects while already fetching an object */ - if ((old_value & 2) && !(value & 2)) { - if (gb->during_object_fetch) { - gb->cycles_for_line += gb->display_cycles; - gb->display_cycles = 0; - gb->object_fetch_aborted = true; - } - } + /* Hack to force aborting object fetch */ + GB_write_memory(gb, addr, value); + GB_write_memory(gb, addr, old_value); GB_advance_cycles(gb, 1); - /* Handle disabling objects while already fetching an object */ - if ((old_value & 2) && !(value & 2)) { - if (gb->during_object_fetch) { - gb->cycles_for_line += gb->display_cycles; - gb->display_cycles = 0; - gb->object_fetch_aborted = true; - } - } GB_write_memory(gb, addr, value); gb->pending_cycles = 5; return; From 84e8e45b7beaf15d67b32fe2e7bd01f60746e482 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 13 Mar 2020 13:35:54 +0200 Subject: [PATCH 184/341] Implement ATTR_CHR --- Core/sgb.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Core/sgb.c b/Core/sgb.c index d712e27..271b681 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -17,6 +17,7 @@ enum { ATTR_BLK = 0x04, ATTR_LIN = 0x05, ATTR_DIV = 0x06, + ATTR_CHR = 0x07, PAL_SET = 0x0A, PAL_TRN = 0x0B, DATA_SND = 0x0F, @@ -254,6 +255,52 @@ static void command_ready(GB_gameboy_t *gb) } break; } + case ATTR_CHR: { + struct __attribute__((packed)) { + uint8_t x, y; + uint16_t length; + uint8_t direction; + uint8_t data[]; + } *command = (void *)(gb->sgb->command + 1); + + uint16_t count = command->length; +#ifdef GB_BIG_ENDIAN + count = __builtin_bswap16(count); +#endif + uint8_t x = command->x; + uint8_t y = command->y; + if (x >= 20 || y >= 18 || (count + 3) / 4 > sizeof(gb->sgb->command) - sizeof(*command) - 1) { + /* TODO: Verify with the SFC BIOS */ + break; + } + + for (unsigned i = 0; i < count; i++) { + uint8_t palette = (command->data[i / 4] >> (((~i) & 3) << 1)) & 3; + gb->sgb->attribute_map[x + 20 * y] = palette; + if (command->direction) { + y++; + if (y == 18) { + x++; + y = 0; + if (x == 20) { + x = 0; + } + } + } + else { + x++; + if (x == 20) { + y++; + x = 0; + if (y == 18) { + y = 0; + } + } + } + } + + break; + } case ATTR_LIN: { struct { uint8_t count; From e94e7cc501fea44c4d4a37cfd5091bed88cfbc98 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 25 Mar 2020 20:33:13 +0200 Subject: [PATCH 185/341] Add another color correction mode --- Cocoa/Preferences.xib | 5 +++-- Core/display.c | 18 ++++++++++++++++-- Core/display.h | 1 + SDL/gui.c | 6 +++--- libretro/libretro.c | 9 ++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index e7d96aa..7eb0587 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -152,6 +152,7 @@ + @@ -203,7 +204,7 @@ - + @@ -459,7 +460,7 @@ - + diff --git a/Core/display.c b/Core/display.c index 8072f37..0ed973e 100644 --- a/Core/display.c +++ b/Core/display.c @@ -133,7 +133,7 @@ static void display_vblank(GB_gameboy_t *gb) if (!GB_is_sgb(gb)) { uint32_t color = 0; if (GB_is_cgb(gb)) { - color = gb->rgb_encode_callback(gb, 0xFF, 0xFF, 0xFF); + color = GB_convert_rgb15(gb, 0x7FFF, false); } else { color = is_ppu_stopped ? @@ -261,7 +261,21 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border) } new_r = r; new_b = b; - if (gb->color_correction_mode == GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS) { + if (gb->color_correction_mode == GB_COLOR_CORRECTION_REDUCE_CONTRAST) { + r = new_r; + g = new_r; + b = new_r; + + new_r = new_r * 7 / 8 + ( g + b) / 16; + new_g = new_g * 7 / 8 + (r + b) / 16; + new_b = new_b * 7 / 8 + (r + g ) / 16; + + + new_r = new_r * (224 - 32) / 255 + 32; + new_g = new_g * (220 - 36) / 255 + 36; + new_b = new_b * (216 - 40) / 255 + 40; + } + else if (gb->color_correction_mode == GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS) { uint8_t old_max = MAX(r, MAX(g, b)); uint8_t new_max = MAX(new_r, MAX(new_g, new_b)); diff --git a/Core/display.h b/Core/display.h index d539881..4c37f99 100644 --- a/Core/display.h +++ b/Core/display.h @@ -50,6 +50,7 @@ typedef enum { GB_COLOR_CORRECTION_CORRECT_CURVES, GB_COLOR_CORRECTION_EMULATE_HARDWARE, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS, + GB_COLOR_CORRECTION_REDUCE_CONTRAST, } GB_color_correction_mode_t; void GB_draw_tileset(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette_type, uint8_t palette_index); diff --git a/SDL/gui.c b/SDL/gui.c index 6646a17..b6b0f03 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -422,7 +422,7 @@ const char *current_scaling_mode(unsigned index) const char *current_color_correction_mode(unsigned index) { - return (const char *[]){"Disabled", "Correct Color Curves", "Emulate Hardware", "Preserve Brightness"} + return (const char *[]){"Disabled", "Correct Color Curves", "Emulate Hardware", "Preserve Brightness", "Reduce Contrast"} [configuration.color_correction_mode]; } @@ -462,7 +462,7 @@ void cycle_scaling_backwards(unsigned index) static void cycle_color_correction(unsigned index) { - if (configuration.color_correction_mode == GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS) { + if (configuration.color_correction_mode == GB_COLOR_CORRECTION_REDUCE_CONTRAST) { configuration.color_correction_mode = GB_COLOR_CORRECTION_DISABLED; } else { @@ -473,7 +473,7 @@ static void cycle_color_correction(unsigned index) static void cycle_color_correction_backwards(unsigned index) { if (configuration.color_correction_mode == GB_COLOR_CORRECTION_DISABLED) { - configuration.color_correction_mode = GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS; + configuration.color_correction_mode = GB_COLOR_CORRECTION_REDUCE_CONTRAST; } else { configuration.color_correction_mode--; diff --git a/libretro/libretro.c b/libretro/libretro.c index 59dfdc4..8cd1324 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -202,7 +202,7 @@ static retro_environment_t environ_cb; /* variables for single cart mode */ static const struct retro_variable vars_single[] = { - { "sameboy_color_correction_mode", "Color correction; off|correct curves|emulate hardware|preserve brightness" }, + { "sameboy_color_correction_mode", "Color correction; off|correct curves|emulate hardware|preserve brightness|reduce contrast" }, { "sameboy_high_pass_filter_mode", "High-pass filter; off|accurate|remove dc offset" }, { "sameboy_model", "Emulated model; Auto|Game Boy|Game Boy Color|Game Boy Advance|Super Game Boy|Super Game Boy 2" }, { "sameboy_border", "Super Game Boy border; enabled|disabled" }, @@ -497,6 +497,8 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + else if (strcmp(var.value, "reduce_contrast") == 0) + GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); } var.key = "sameboy_high_pass_filter_mode"; @@ -561,6 +563,8 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + else if (strcmp(var.value, "reduce_contrast") == 0) + GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); } var.key = "sameboy_color_correction_mode_2"; @@ -575,6 +579,9 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + else if (strcmp(var.value, "reduce_contrast") == 0) + GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_REDUCE_CONTRAST); + } var.key = "sameboy_high_pass_filter_mode_1"; From 5ecb8456624642e18328450864a6a9cbd015a7ee Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 26 Mar 2020 20:54:18 +0200 Subject: [PATCH 186/341] Add accurate frame blending option --- Cocoa/AppDelegate.m | 2 ++ Cocoa/Document.m | 21 +++++++++-------- Cocoa/GBGLShader.h | 3 ++- Cocoa/GBGLShader.m | 10 ++++---- Cocoa/GBOpenGLView.m | 5 ++-- Cocoa/GBPreferencesWindow.h | 1 + Cocoa/GBPreferencesWindow.m | 22 ++++++++++++++++-- Cocoa/GBView.h | 10 +++++++- Cocoa/GBView.m | 21 +++++++++++++---- Cocoa/GBViewMetal.m | 20 ++++++++-------- Cocoa/Preferences.xib | 44 +++++++++++++++++++++++++++++------ Core/display.c | 10 ++++++++ Core/display.h | 1 + Core/gb.h | 1 + SDL/gui.c | 43 ++++++++++++++++++++++++++++------ SDL/gui.h | 2 +- SDL/main.c | 2 +- SDL/shader.c | 7 +++--- SDL/shader.h | 13 +++++++++-- Shaders/MasterShader.fsh | 46 +++++++++++++++++++++++++++++++------ Shaders/MasterShader.metal | 43 ++++++++++++++++++++++++++++++---- 21 files changed, 258 insertions(+), 69 deletions(-) diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index bbaa3ae..b941eb4 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -1,5 +1,6 @@ #import "AppDelegate.h" #include "GBButtons.h" +#include "GBView.h" #include #import @@ -36,6 +37,7 @@ @"GBColorCorrection": @(GB_COLOR_CORRECTION_EMULATE_HARDWARE), @"GBHighpassFilter": @(GB_HIGHPASS_REMOVE_DC_OFFSET), @"GBRewindLength": @(10), + @"GBFrameBlendingMode": @([defaults boolForKey:@"DisableFrameBlending"]? GB_FRAME_BLENDING_MODE_DISABLED : GB_FRAME_BLENDING_MODE_ACCURATE), @"GBDMGModel": @(GB_MODEL_DMG_B), @"GBCGBModel": @(GB_MODEL_CGB_E), diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 8264fb3..b835233 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -492,7 +492,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.consoleOutput.textContainerInset = NSMakeSize(4, 4); [self.view becomeFirstResponder]; - self.view.shouldBlendFrameWithPrevious = ![[NSUserDefaults standardUserDefaults] boolForKey:@"DisableFrameBlending"]; + self.view.frameBlendingMode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBFrameBlendingMode"]; CGRect window_frame = self.mainWindow.frame; window_frame.size.width = MAX([[NSUserDefaults standardUserDefaults] integerForKey:@"LastWindowWidth"], window_frame.size.width); @@ -521,6 +521,11 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) name:@"GBColorCorrectionChanged" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateFrameBlendingMode) + name:@"GBFrameBlendingModeChanged" + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePalette) name:@"GBColorPaletteChanged" @@ -677,12 +682,6 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [[NSUserDefaults standardUserDefaults] setBool:!self.audioClient.isPlaying forKey:@"Mute"]; } -- (IBAction)toggleBlend:(id)sender -{ - self.view.shouldBlendFrameWithPrevious ^= YES; - [[NSUserDefaults standardUserDefaults] setBool:!self.view.shouldBlendFrameWithPrevious forKey:@"DisableFrameBlending"]; -} - - (BOOL)validateUserInterfaceItem:(id)anItem { if([anItem action] == @selector(mute:)) { @@ -695,9 +694,6 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) else if ([anItem action] == @selector(reset:) && anItem.tag != MODEL_NONE) { [(NSMenuItem*)anItem setState:anItem.tag == current_model]; } - else if ([anItem action] == @selector(toggleBlend:)) { - [(NSMenuItem*)anItem setState:self.view.shouldBlendFrameWithPrevious]; - } else if ([anItem action] == @selector(interrupt:)) { if (![[NSUserDefaults standardUserDefaults] boolForKey:@"DeveloperMode"]) { return false; @@ -1617,6 +1613,11 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } } +- (void) updateFrameBlendingMode +{ + self.view.frameBlendingMode = (GB_frame_blending_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBFrameBlendingMode"]; +} + - (void) updateRewindLength { [self performAtomicBlock:^{ diff --git a/Cocoa/GBGLShader.h b/Cocoa/GBGLShader.h index 1a12617..8e46f93 100644 --- a/Cocoa/GBGLShader.h +++ b/Cocoa/GBGLShader.h @@ -1,6 +1,7 @@ #import +#import "GBView.h" @interface GBGLShader : NSObject - (instancetype)initWithName:(NSString *) shaderName; -- (void) renderBitmap: (void *)bitmap previous:(void*) previous sized:(NSSize)srcSize inSize:(NSSize)dstSize scale: (double) scale; +- (void) renderBitmap: (void *)bitmap previous:(void*) previous sized:(NSSize)srcSize inSize:(NSSize)dstSize scale: (double) scale withBlendingMode: (GB_frame_blending_mode_t)blendingMode; @end diff --git a/Cocoa/GBGLShader.m b/Cocoa/GBGLShader.m index fe636f8..d57f43d 100644 --- a/Cocoa/GBGLShader.m +++ b/Cocoa/GBGLShader.m @@ -21,7 +21,7 @@ void main(void) {\n\ GLuint resolution_uniform; GLuint texture_uniform; GLuint previous_texture_uniform; - GLuint mix_previous_uniform; + GLuint frame_blending_mode_uniform; GLuint position_attribute; GLuint texture; @@ -70,7 +70,7 @@ void main(void) {\n\ glBindTexture(GL_TEXTURE_2D, 0); previous_texture_uniform = glGetUniformLocation(program, "previous_image"); - mix_previous_uniform = glGetUniformLocation(program, "mix_previous"); + frame_blending_mode_uniform = glGetUniformLocation(program, "frame_blending_mode"); // Configure OpenGL [self configureOpenGL]; @@ -79,7 +79,7 @@ void main(void) {\n\ return self; } -- (void) renderBitmap: (void *)bitmap previous:(void*) previous sized:(NSSize)srcSize inSize:(NSSize)dstSize scale: (double) scale +- (void) renderBitmap: (void *)bitmap previous:(void*) previous sized:(NSSize)srcSize inSize:(NSSize)dstSize scale: (double) scale withBlendingMode:(GB_frame_blending_mode_t)blendingMode { glUseProgram(program); glUniform2f(resolution_uniform, dstSize.width * scale, dstSize.height * scale); @@ -87,8 +87,8 @@ void main(void) {\n\ glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, srcSize.width, srcSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); glUniform1i(texture_uniform, 0); - glUniform1i(mix_previous_uniform, previous != NULL); - if (previous) { + glUniform1i(frame_blending_mode_uniform, blendingMode); + if (blendingMode) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, previous_texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, srcSize.width, srcSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, previous); diff --git a/Cocoa/GBOpenGLView.m b/Cocoa/GBOpenGLView.m index 67a9f8d..fd845e1 100644 --- a/Cocoa/GBOpenGLView.m +++ b/Cocoa/GBOpenGLView.m @@ -14,10 +14,11 @@ glViewport(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale); [self.shader renderBitmap:gbview.currentBuffer - previous:gbview.shouldBlendFrameWithPrevious? gbview.previousBuffer : NULL + previous:gbview.frameBlendingMode? gbview.previousBuffer : NULL sized:NSMakeSize(GB_get_screen_width(gbview.gb), GB_get_screen_height(gbview.gb)) inSize:self.bounds.size - scale:scale]; + scale:scale + withBlendingMode:gbview.frameBlendingMode]; glFlush(); } diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 2d6b0fc..27b5aa5 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -7,6 +7,7 @@ @property (strong) IBOutlet NSButton *aspectRatioCheckbox; @property (strong) IBOutlet NSPopUpButton *highpassFilterPopupButton; @property (strong) IBOutlet NSPopUpButton *colorCorrectionPopupButton; +@property (strong) IBOutlet NSPopUpButton *frameBlendingModePopupButton; @property (strong) IBOutlet NSPopUpButton *colorPalettePopupButton; @property (strong) IBOutlet NSPopUpButton *displayBorderPopupButton; @property (strong) IBOutlet NSPopUpButton *rewindPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 0303771..1b5aa4f 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -14,6 +14,7 @@ NSPopUpButton *_graphicsFilterPopupButton; NSPopUpButton *_highpassFilterPopupButton; NSPopUpButton *_colorCorrectionPopupButton; + NSPopUpButton *_frameBlendingModePopupButton; NSPopUpButton *_colorPalettePopupButton; NSPopUpButton *_displayBorderPopupButton; NSPopUpButton *_rewindPopupButton; @@ -87,6 +88,18 @@ return _colorCorrectionPopupButton; } +- (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton +{ + _frameBlendingModePopupButton = frameBlendingModePopupButton; + NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBFrameBlendingMode"]; + [_frameBlendingModePopupButton selectItemAtIndex:mode]; +} + +- (NSPopUpButton *)frameBlendingModePopupButton +{ + return _frameBlendingModePopupButton; +} + - (void)setColorPalettePopupButton:(NSPopUpButton *)colorPalettePopupButton { _colorPalettePopupButton = colorPalettePopupButton; @@ -223,7 +236,14 @@ [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) forKey:@"GBColorCorrection"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorCorrectionChanged" object:nil]; +} +- (IBAction)franeBlendingModeChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) + forKey:@"GBFrameBlendingMode"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBFrameBlendingModeChanged" object:nil]; + } - (IBAction)colorPaletteChanged:(id)sender @@ -231,7 +251,6 @@ [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) forKey:@"GBColorPalette"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorPaletteChanged" object:nil]; - } - (IBAction)displayBorderChanged:(id)sender @@ -239,7 +258,6 @@ [[NSUserDefaults standardUserDefaults] setObject:@([sender selectedItem].tag) forKey:@"GBBorderMode"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"GBBorderModeChanged" object:nil]; - } - (IBAction)rewindLengthChanged:(id)sender diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index f4c5e44..474e3c7 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -2,11 +2,19 @@ #include #import "GBJoystickListener.h" +typedef enum { + GB_FRAME_BLENDING_MODE_DISABLED, + GB_FRAME_BLENDING_MODE_SIMPLE, + GB_FRAME_BLENDING_MODE_ACCURATE, + GB_FRAME_BLENDING_MODE_ACCURATE_EVEN = GB_FRAME_BLENDING_MODE_ACCURATE, + GB_FRAME_BLENDING_MODE_ACCURATE_ODD, +} GB_frame_blending_mode_t; + @interface GBView : NSView - (void) flip; - (uint32_t *) pixels; @property GB_gameboy_t *gb; -@property (nonatomic) BOOL shouldBlendFrameWithPrevious; +@property (nonatomic) GB_frame_blending_mode_t frameBlendingMode; @property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled; @property bool isRewinding; @property NSView *internalView; diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index e3026fd..0267344 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -19,6 +19,7 @@ bool underclockKeyDown; double clockMultiplier; NSEventModifierFlags previousModifiers; + GB_frame_blending_mode_t _frameBlendingMode; } + (instancetype)alloc @@ -43,8 +44,7 @@ } - (void) _init -{ - _shouldBlendFrameWithPrevious = 1; +{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil]; tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){} options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect @@ -79,15 +79,26 @@ [self setFrame:self.superview.frame]; } -- (void) setShouldBlendFrameWithPrevious:(BOOL)shouldBlendFrameWithPrevious +- (void) setFrameBlendingMode:(GB_frame_blending_mode_t)frameBlendingMode { - _shouldBlendFrameWithPrevious = shouldBlendFrameWithPrevious; + _frameBlendingMode = frameBlendingMode; [self setNeedsDisplay:YES]; } + +- (GB_frame_blending_mode_t)frameBlendingMode +{ + if (_frameBlendingMode == GB_FRAME_BLENDING_MODE_ACCURATE) { + if (GB_is_sgb(_gb)) { + return GB_FRAME_BLENDING_MODE_SIMPLE; + } + return GB_is_odd_frame(_gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN; + } + return _frameBlendingMode; +} - (unsigned char) numberOfBuffers { - return _shouldBlendFrameWithPrevious? 3 : 2; + return _frameBlendingMode? 3 : 2; } - (void)dealloc diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index fde4b7e..093ed2a 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -15,7 +15,7 @@ static const vector_float2 rect[] = id vertices; id pipeline_state; id command_queue; - id mix_previous_buffer; + id frame_blending_mode_buffer; id output_resolution_buffer; vector_float2 output_resolution; } @@ -23,7 +23,7 @@ static const vector_float2 rect[] = + (bool)isSupported { if (MTLCopyAllDevices) { - return [MTLCopyAllDevices() count]; + return false; //[MTLCopyAllDevices() count]; } return false; } @@ -56,10 +56,10 @@ static const vector_float2 rect[] = length:sizeof(rect) options:MTLResourceStorageModeShared]; - static const bool default_mix_value = false; - mix_previous_buffer = [device newBufferWithBytes:&default_mix_value - length:sizeof(default_mix_value) - options:MTLResourceStorageModeShared]; + static const GB_frame_blending_mode_t default_blending_mode = GB_FRAME_BLENDING_MODE_DISABLED; + frame_blending_mode_buffer = [device newBufferWithBytes:&default_blending_mode + length:sizeof(default_blending_mode) + options:MTLResourceStorageModeShared]; output_resolution_buffer = [device newBufferWithBytes:&output_resolution length:sizeof(output_resolution) @@ -147,7 +147,7 @@ static const vector_float2 rect[] = mipmapLevel:0 withBytes:[self currentBuffer] bytesPerRow:texture.width * 4]; - if ([self shouldBlendFrameWithPrevious]) { + if ([self frameBlendingMode]) { [previous_texture replaceRegion:region mipmapLevel:0 withBytes:[self previousBuffer] @@ -157,9 +157,9 @@ static const vector_float2 rect[] = MTLRenderPassDescriptor *render_pass_descriptor = view.currentRenderPassDescriptor; id command_buffer = [command_queue commandBuffer]; - if(render_pass_descriptor != nil) + if (render_pass_descriptor != nil) { - *(bool *)[mix_previous_buffer contents] = [self shouldBlendFrameWithPrevious]; + *(GB_frame_blending_mode_t *)[frame_blending_mode_buffer contents] = [self frameBlendingMode]; *(vector_float2 *)[output_resolution_buffer contents] = output_resolution; id render_encoder = @@ -176,7 +176,7 @@ static const vector_float2 rect[] = offset:0 atIndex:0]; - [render_encoder setFragmentBuffer:mix_previous_buffer + [render_encoder setFragmentBuffer:frame_blending_mode_buffer offset:0 atIndex:0]; diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 7eb0587..eeb1cf2 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -69,6 +69,7 @@ + @@ -80,11 +81,11 @@ - + - + @@ -93,7 +94,7 @@ - + @@ -130,7 +131,7 @@ - + @@ -139,7 +140,7 @@ - + @@ -160,6 +161,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -231,7 +261,7 @@ - + @@ -460,7 +490,7 @@ - + diff --git a/Core/display.c b/Core/display.c index 0ed973e..a7dda7d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -797,6 +797,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) return; } + gb->is_odd_frame = false; + if (!GB_is_cgb(gb)) { GB_SLEEP(gb, display, 23, 1); } @@ -1228,6 +1230,7 @@ abort_fetching_object: } else { if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { + gb->is_odd_frame ^= true; display_vblank(gb); } gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; @@ -1236,6 +1239,7 @@ abort_fetching_object: else { gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { + gb->is_odd_frame ^= true; display_vblank(gb); } } @@ -1465,3 +1469,9 @@ uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_h } return count; } + + +bool GB_is_odd_frame(GB_gameboy_t *gb) +{ + return gb->is_odd_frame; +} diff --git a/Core/display.h b/Core/display.h index 4c37f99..5bdeba8 100644 --- a/Core/display.h +++ b/Core/display.h @@ -58,4 +58,5 @@ void GB_draw_tilemap(GB_gameboy_t *gb, uint32_t *dest, GB_palette_type_t palette uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_height); uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border); void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t mode); +bool GB_is_odd_frame(GB_gameboy_t *gb); #endif /* display_h */ diff --git a/Core/gb.h b/Core/gb.h index 88e2991..b4ef658 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -524,6 +524,7 @@ struct GB_gameboy_internal_s { bool wy_triggered; uint8_t window_tile_x; uint8_t lcd_x; // The LCD can go out of sync since the push signal is skipped in some cases. + bool is_odd_frame; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/SDL/gui.c b/SDL/gui.c index b6b0f03..e34028d 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -46,9 +46,22 @@ void render_texture(void *pixels, void *previous) } glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); + GB_frame_blending_mode_t mode = configuration.blending_mode; + if (!previous) { + mode = GB_FRAME_BLENDING_MODE_DISABLED; + } + else if (mode == GB_FRAME_BLENDING_MODE_ACCURATE) { + if (GB_is_sgb(&gb)) { + mode = GB_FRAME_BLENDING_MODE_SIMPLE; + } + else { + mode = GB_is_odd_frame(&gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN; + } + } render_bitmap_with_shader(&shader, _pixels, previous, GB_get_screen_width(&gb), GB_get_screen_height(&gb), - rect.x, rect.y, rect.w, rect.h); + rect.x, rect.y, rect.w, rect.h, + mode); SDL_GL_SwapWindow(window); } } @@ -91,7 +104,7 @@ configuration_t configuration = .color_correction_mode = GB_COLOR_CORRECTION_EMULATE_HARDWARE, .highpass_mode = GB_HIGHPASS_ACCURATE, .scaling_mode = GB_SDL_SCALING_INTEGER_FACTOR, - .blend_frames = true, + .blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE, .rewind_length = 60 * 2, .model = MODEL_CGB }; @@ -600,23 +613,39 @@ const char *current_filter_name(unsigned index) return shaders[i].display_name; } -static void toggle_blend_frames(unsigned index) +static void cycle_blending_mode(unsigned index) { - configuration.blend_frames ^= true; + if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_ACCURATE) { + configuration.blending_mode = GB_FRAME_BLENDING_MODE_DISABLED; + } + else { + configuration.blending_mode++; + } } -const char *blend_frames_string(unsigned index) +static void cycle_blending_mode_backwards(unsigned index) { - return configuration.blend_frames? "Enabled" : "Disabled"; + if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_DISABLED) { + configuration.blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE; + } + else { + configuration.blending_mode--; + } +} + +const char *blending_mode_string(unsigned index) +{ + return (const char *[]){"Disabled", "Simple", "Accurate"} + [configuration.blending_mode]; } static const struct menu_item graphics_menu[] = { {"Scaling Mode:", cycle_scaling, current_scaling_mode, cycle_scaling_backwards}, {"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards}, {"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards}, + {"Frame Blending:", cycle_blending_mode, blending_mode_string, cycle_blending_mode_backwards}, {"Mono Palette:", cycle_palette, current_palette, cycle_palette_backwards}, {"Display Border:", cycle_border_mode, current_border_mode, cycle_border_mode_backwards}, - {"Blend Frames:", toggle_blend_frames, blend_frames_string, toggle_blend_frames}, {"Back", return_to_root_menu}, {NULL,} }; diff --git a/SDL/gui.h b/SDL/gui.h index 41e9bf2..8ef2a68 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -70,7 +70,7 @@ typedef struct { SDL_Scancode keys[9]; GB_color_correction_mode_t color_correction_mode; enum scaling_mode scaling_mode; - bool blend_frames; + uint8_t blending_mode; GB_highpass_mode_t highpass_mode; diff --git a/SDL/main.c b/SDL/main.c index 06159c9..c2b5549 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -372,7 +372,7 @@ static void vblank(GB_gameboy_t *gb) clock_mutliplier += 1.0/16; GB_set_clock_multiplier(gb, clock_mutliplier); } - if (configuration.blend_frames) { + if (configuration.blending_mode) { render_texture(active_pixel_buffer, previous_pixel_buffer); uint32_t *temp = active_pixel_buffer; active_pixel_buffer = previous_pixel_buffer; diff --git a/SDL/shader.c b/SDL/shader.c index 37e5be7..250046b 100644 --- a/SDL/shader.c +++ b/SDL/shader.c @@ -130,7 +130,7 @@ bool init_shader_with_name(shader_t *shader, const char *name) glBindTexture(GL_TEXTURE_2D, 0); shader->previous_texture_uniform = glGetUniformLocation(shader->program, "previous_image"); - shader->mix_previous_uniform = glGetUniformLocation(shader->program, "mix_previous"); + shader->blending_mode_uniform = glGetUniformLocation(shader->program, "frame_blending_mode"); // Program @@ -164,7 +164,8 @@ bool init_shader_with_name(shader_t *shader, const char *name) void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, unsigned source_width, unsigned source_height, - unsigned x, unsigned y, unsigned w, unsigned h) + unsigned x, unsigned y, unsigned w, unsigned h, + GB_frame_blending_mode_t blending_mode) { glUseProgram(shader->program); glUniform2f(shader->origin_uniform, x, y); @@ -173,7 +174,7 @@ void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, glBindTexture(GL_TEXTURE_2D, shader->texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, source_width, source_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap); glUniform1i(shader->texture_uniform, 0); - glUniform1i(shader->mix_previous_uniform, previous != NULL); + glUniform1i(shader->blending_mode_uniform, previous? blending_mode : GB_FRAME_BLENDING_MODE_DISABLED); if (previous) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, shader->previous_texture); diff --git a/SDL/shader.h b/SDL/shader.h index 3a1c304..149958d 100644 --- a/SDL/shader.h +++ b/SDL/shader.h @@ -8,7 +8,7 @@ typedef struct shader_s { GLuint origin_uniform; GLuint texture_uniform; GLuint previous_texture_uniform; - GLuint mix_previous_uniform; + GLuint blending_mode_uniform; GLuint position_attribute; GLuint texture; @@ -16,10 +16,19 @@ typedef struct shader_s { GLuint program; } shader_t; +typedef enum { + GB_FRAME_BLENDING_MODE_DISABLED, + GB_FRAME_BLENDING_MODE_SIMPLE, + GB_FRAME_BLENDING_MODE_ACCURATE, + GB_FRAME_BLENDING_MODE_ACCURATE_EVEN = GB_FRAME_BLENDING_MODE_ACCURATE, + GB_FRAME_BLENDING_MODE_ACCURATE_ODD, +} GB_frame_blending_mode_t; + bool init_shader_with_name(shader_t *shader, const char *name); void render_bitmap_with_shader(shader_t *shader, void *bitmap, void *previous, unsigned source_width, unsigned source_height, - unsigned x, unsigned y, unsigned w, unsigned h); + unsigned x, unsigned y, unsigned w, unsigned h, + GB_frame_blending_mode_t blending_mode); void free_shader(struct shader_s *shader); #endif /* shader_h */ diff --git a/Shaders/MasterShader.fsh b/Shaders/MasterShader.fsh index cd569c2..729ab5f 100644 --- a/Shaders/MasterShader.fsh +++ b/Shaders/MasterShader.fsh @@ -1,7 +1,7 @@ #version 150 uniform sampler2D image; uniform sampler2D previous_image; -uniform bool mix_previous; +uniform int frame_blending_mode; uniform vec2 output_resolution; uniform vec2 origin; @@ -15,6 +15,15 @@ out vec4 frag_color; #line 1 {filter} + +#define BLEND_BIAS (1.0/3.0) + +#define DISABLED 0 +#define SIMPLE 1 +#define ACCURATE 2 +#define ACCURATE_EVEN ACCURATE +#define ACCURATE_ODD 3 + void main() { vec2 position = gl_FragCoord.xy - origin; @@ -22,11 +31,34 @@ void main() position.y = 1 - position.y; vec2 input_resolution = textureSize(image, 0); - if (mix_previous) { - frag_color = mix(scale(image, position, input_resolution, output_resolution), - scale(previous_image, position, input_resolution, output_resolution), 0.5); - } - else { - frag_color = scale(image, position, input_resolution, output_resolution); + float ratio; + switch (frame_blending_mode) { + default: + case DISABLED: + frag_color = scale(image, position, input_resolution, output_resolution); + return; + case SIMPLE: + ratio = 0.5; + break; + case ACCURATE_EVEN: + if ((int(position.y * input_resolution.y) & 1) == 0) { + ratio = BLEND_BIAS; + } + else { + ratio = 1 - BLEND_BIAS; + } + break; + case ACCURATE_ODD: + if ((int(position.y * input_resolution.y) & 1) == 0) { + ratio = 1 - BLEND_BIAS; + } + else { + ratio = BLEND_BIAS; + } + break; } + + frag_color = mix(scale(image, position, input_resolution, output_resolution), + scale(previous_image, position, input_resolution, output_resolution), ratio); + } diff --git a/Shaders/MasterShader.metal b/Shaders/MasterShader.metal index 4cae3ae..ee8dec9 100644 --- a/Shaders/MasterShader.metal +++ b/Shaders/MasterShader.metal @@ -42,19 +42,52 @@ static inline float4 texture(texture2d texture, float2 pos) #line 1 {filter} +#define BLEND_BIAS (1.0/3.0) + +enum frame_blending_mode { + DISABLED, + SIMPLE, + ACCURATE, + ACCURATE_EVEN = ACCURATE, + ACCURATE_ODD, +}; + fragment float4 fragment_shader(rasterizer_data in [[stage_in]], texture2d image [[ texture(0) ]], texture2d previous_image [[ texture(1) ]], - constant bool *mix_previous [[ buffer(0) ]], + constant enum frame_blending_mode *frame_blending_mode [[ buffer(0) ]], constant float2 *output_resolution [[ buffer(1) ]]) { float2 input_resolution = float2(image.get_width(), image.get_height()); in.texcoords.y = 1 - in.texcoords.y; - if (*mix_previous) { - return mix(scale(image, in.texcoords, input_resolution, *output_resolution), - scale(previous_image, in.texcoords, input_resolution, *output_resolution), 0.5); + float ratio; + switch (*frame_blending_mode) { + default: + case DISABLED: + return scale(image, in.texcoords, input_resolution, *output_resolution); + case SIMPLE: + ratio = 0.5; + break; + case ACCURATE_EVEN: + if (((int)(in.texcoords.y * input_resolution.y) & 1) == 0) { + ratio = BLEND_BIAS; + } + else { + ratio = 1 - BLEND_BIAS; + } + break; + case ACCURATE_ODD: + if (((int)(in.texcoords.y * input_resolution.y) & 1) == 0) { + ratio = 1 - BLEND_BIAS; + } + else { + ratio = BLEND_BIAS; + } + break; } - return scale(image, in.texcoords, input_resolution, *output_resolution); + + return mix(scale(image, in.texcoords, input_resolution, *output_resolution), + scale(previous_image, in.texcoords, input_resolution, *output_resolution), ratio); } From 7a807f5cae4ab208a9993d0bce68610994c46214 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 26 Mar 2020 22:18:31 +0200 Subject: [PATCH 187/341] Fix #243 --- HexFiend/HFRepresenterTextViewCallout.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HexFiend/HFRepresenterTextViewCallout.m b/HexFiend/HFRepresenterTextViewCallout.m index ae46bd8..bb4b58e 100644 --- a/HexFiend/HFRepresenterTextViewCallout.m +++ b/HexFiend/HFRepresenterTextViewCallout.m @@ -432,7 +432,7 @@ static double distanceMod1(double a, double b) { // Compute the vertical offset CGFloat textYOffset = (glyphCount == 1 ? 4 : 5); // LOL - if ([_label isEqualToString:@"6"] || [_label isEqualToString:@"7"] == 7) textYOffset -= 1; + if ([_label isEqualToString:@"6"]) textYOffset -= 1; // Apply this text matrix From fa1c84f18fb25dfa987690364b667ae9fb4afc99 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 15:43:30 +0300 Subject: [PATCH 188/341] Remove the Blend Frames menu item --- Cocoa/MainMenu.xib | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index 844aa0c..ee989ee 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -1,8 +1,8 @@ - + - + @@ -342,13 +342,6 @@ - - - - - - - @@ -454,6 +447,7 @@
+
From 4cb56dc76fd7858aef8a558bf5e917433b470359 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 16:35:36 +0300 Subject: [PATCH 189/341] Improve MBC2 emulation. Fixes #238 --- Core/memory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index fccfd86..d3d9aaa 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -456,9 +456,9 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } break; case GB_MBC2: - switch (addr & 0xF000) { - case 0x0000: case 0x1000: if (!(addr & 0x100)) gb->mbc_ram_enable = (value & 0xF) == 0xA; break; - case 0x2000: case 0x3000: if ( addr & 0x100) gb->mbc2.rom_bank = value; break; + switch (addr & 0x4100) { + case 0x0000: gb->mbc_ram_enable = (value & 0xF) == 0xA; break; + case 0x0100: gb->mbc2.rom_bank = value; break; } break; case GB_MBC3: From 588c0734a9bf366aee9520cf6696520b6a823458 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 17:22:50 +0300 Subject: [PATCH 190/341] Fix a crash --- Cocoa/GBView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 0267344..0e52811 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -89,7 +89,7 @@ - (GB_frame_blending_mode_t)frameBlendingMode { if (_frameBlendingMode == GB_FRAME_BLENDING_MODE_ACCURATE) { - if (GB_is_sgb(_gb)) { + if (!_gb || GB_is_sgb(_gb)) { return GB_FRAME_BLENDING_MODE_SIMPLE; } return GB_is_odd_frame(_gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN; From 876b36ac1cf18dff51fd3e2c51b103c94a46cb72 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 17:26:51 +0300 Subject: [PATCH 191/341] More crash fixes, restore Metal support --- Cocoa/GBOpenGLView.m | 14 ++++++++------ Cocoa/GBViewMetal.m | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cocoa/GBOpenGLView.m b/Cocoa/GBOpenGLView.m index fd845e1..8831b62 100644 --- a/Cocoa/GBOpenGLView.m +++ b/Cocoa/GBOpenGLView.m @@ -13,12 +13,14 @@ double scale = self.window.backingScaleFactor; glViewport(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale); - [self.shader renderBitmap:gbview.currentBuffer - previous:gbview.frameBlendingMode? gbview.previousBuffer : NULL - sized:NSMakeSize(GB_get_screen_width(gbview.gb), GB_get_screen_height(gbview.gb)) - inSize:self.bounds.size - scale:scale - withBlendingMode:gbview.frameBlendingMode]; + if (gbview.gb) { + [self.shader renderBitmap:gbview.currentBuffer + previous:gbview.frameBlendingMode? gbview.previousBuffer : NULL + sized:NSMakeSize(GB_get_screen_width(gbview.gb), GB_get_screen_height(gbview.gb)) + inSize:self.bounds.size + scale:scale + withBlendingMode:gbview.frameBlendingMode]; + } glFlush(); } diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 093ed2a..4c8a5d5 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -23,7 +23,7 @@ static const vector_float2 rect[] = + (bool)isSupported { if (MTLCopyAllDevices) { - return false; //[MTLCopyAllDevices() count]; + return [MTLCopyAllDevices() count]; } return false; } From 05403d3a56883b8acee642c5b85e801281ed66d1 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 17:36:55 +0300 Subject: [PATCH 192/341] Fix the Joypad interrupt. Fixes #237 --- Core/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/memory.c b/Core/memory.c index d3d9aaa..148c986 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -780,7 +780,7 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } else if ((gb->io_registers[GB_IO_JOYP] & 0x30) != (value & 0x30)) { GB_sgb_write(gb, value); - gb->io_registers[GB_IO_JOYP] = value & 0xF0; + gb->io_registers[GB_IO_JOYP] = (value & 0xF0) | (gb->io_registers[GB_IO_JOYP] & 0x0F); GB_update_joyp(gb); } return; From 1a3572316f0f9e2fc6b35c4cf0d99b3cfd279b8e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 17:49:14 +0300 Subject: [PATCH 193/341] next now skips over halt, closes #233 --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 8b6d6cf..e7f6881 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -2059,7 +2059,7 @@ void GB_debugger_run(GB_gameboy_t *gb) if (gb->debug_disable) return; char *input = NULL; - if (gb->debug_next_command && gb->debug_call_depth <= 0) { + if (gb->debug_next_command && gb->debug_call_depth <= 0 && !gb->halted) { gb->debug_stopped = true; } if (gb->debug_fin_command && gb->debug_call_depth == -1) { From 2f1b8e5b5785395ccf79eab33c0123ff7fa0a454 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 18:56:47 +0300 Subject: [PATCH 194/341] IME is now available under the registers command --- Core/debugger.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index e7f6881..71f0861 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -816,16 +816,17 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const } - GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */ + GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */ (gb->f & GB_CARRY_FLAG)? 'C' : '-', (gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-', (gb->f & GB_SUBTRACT_FLAG)? 'N' : '-', (gb->f & GB_ZERO_FLAG)? 'Z' : '-'); - GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false)); - GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false)); - GB_log(gb, "HL = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_HL], false)); - GB_log(gb, "SP = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_SP], false)); - GB_log(gb, "PC = %s\n", value_to_string(gb, gb->pc, false)); + GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false)); + GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false)); + GB_log(gb, "HL = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_HL], false)); + GB_log(gb, "SP = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_SP], false)); + GB_log(gb, "PC = %s\n", value_to_string(gb, gb->pc, false)); + GB_log(gb, "IME = %s\n", gb->ime? "Enabled" : "Disabled"); return true; } From 9f3bffd4ddfd4422391d83c83a67268deea555b2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 27 Mar 2020 19:10:42 +0300 Subject: [PATCH 195/341] Add volume control to SDL --- SDL/gui.c | 27 ++++++++++++++++++++++++++- SDL/gui.h | 1 + SDL/main.c | 5 +++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/SDL/gui.c b/SDL/gui.c index e34028d..8bc6551 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -106,7 +106,8 @@ configuration_t configuration = .scaling_mode = GB_SDL_SCALING_INTEGER_FACTOR, .blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE, .rewind_length = 60 * 2, - .model = MODEL_CGB + .model = MODEL_CGB, + .volume = 100, }; @@ -681,8 +682,32 @@ void cycle_highpass_filter_backwards(unsigned index) } } +const char *volume_string(unsigned index) +{ + static char ret[5]; + sprintf(ret, "%d%%", configuration.volume); + return ret; +} + +void increase_volume(unsigned index) +{ + configuration.volume += 5; + if (configuration.volume > 100) { + configuration.volume = 100; + } +} + +void decrease_volume(unsigned index) +{ + configuration.volume -= 5; + if (configuration.volume > 100) { + configuration.volume = 0; + } +} + static const struct menu_item audio_menu[] = { {"Highpass Filter:", cycle_highpass_filter, highpass_filter_string, cycle_highpass_filter_backwards}, + {"Volume:", increase_volume, volume_string, decrease_volume}, {"Back", return_to_root_menu}, {NULL,} }; diff --git a/SDL/gui.h b/SDL/gui.h index 8ef2a68..4a3b55f 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -104,6 +104,7 @@ typedef struct { /* v0.13 */ uint8_t dmg_palette; GB_border_mode_t border_mode; + uint8_t volume; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index c2b5549..35c9a75 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -420,6 +420,11 @@ static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) return; } + if (configuration.volume != 100) { + sample->left = sample->left * configuration.volume / 100; + sample->right = sample->right * configuration.volume / 100; + } + SDL_QueueAudio(device_id, sample, sizeof(*sample)); } From d75b7c00232834cea1b7a756c9e62db1b5d422a0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 28 Mar 2020 22:56:19 +0300 Subject: [PATCH 196/341] Feature request; allow loading prefs.bin relatively --- SDL/main.c | 10 +++++++--- Windows/stdio.h | 6 +++++- Windows/utf8_compat.c | 12 +++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index 35c9a75..4b6afea 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -11,6 +11,7 @@ #ifndef _WIN32 #define AUDIO_FREQUENCY 96000 +#include #else #include /* Windows (well, at least my VM) can't handle 96KHz sound well :( */ @@ -686,9 +687,12 @@ int main(int argc, char **argv) SDL_EventState(SDL_DROPFILE, SDL_ENABLE); - char *prefs_dir = SDL_GetPrefPath("", "SameBoy"); - snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir); - SDL_free(prefs_dir); + strcpy(prefs_path, resource_path("prefs.bin")); + if (access(prefs_path, R_OK | W_OK) != 0) { + char *prefs_dir = SDL_GetPrefPath("", "SameBoy"); + snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir); + SDL_free(prefs_dir); + } FILE *prefs_file = fopen(prefs_path, "rb"); if (prefs_file) { diff --git a/Windows/stdio.h b/Windows/stdio.h index d68c956..0595b01 100755 --- a/Windows/stdio.h +++ b/Windows/stdio.h @@ -2,6 +2,10 @@ #include_next #include +int access(const char *filename, int mode); +#define R_OK 2 +#define W_OK 4 + #ifndef __MINGW32__ #ifndef __LIBRETRO__ static inline int vasprintf(char **str, const char *fmt, va_list args) @@ -72,4 +76,4 @@ static inline size_t getline(char **lineptr, size_t *n, FILE *stream) { return p - bufptr - 1; } -#define snprintf _snprintf \ No newline at end of file +#define snprintf _snprintf diff --git a/Windows/utf8_compat.c b/Windows/utf8_compat.c index 1005f22..0347211 100755 --- a/Windows/utf8_compat.c +++ b/Windows/utf8_compat.c @@ -1,6 +1,7 @@ #include #include #include +#include FILE *fopen(const char *filename, const char *mode) { @@ -11,4 +12,13 @@ FILE *fopen(const char *filename, const char *mode) MultiByteToWideChar(CP_UTF8, 0, mode, -1, w_mode, sizeof(w_mode) / sizeof(w_mode[0])); return _wfopen(w_filename, w_mode); -} \ No newline at end of file +} + +int access(const char *filename, int mode) +{ + wchar_t w_filename[MAX_PATH] = {0,}; + MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename) / sizeof(w_filename[0])); + + return _waccess(w_filename, mode); +} + From 0ed5cf6b3879c0b7d6adfe769354e55fe04d1d1b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 8 Apr 2020 19:07:29 +0300 Subject: [PATCH 197/341] Proper MBC30 support, more accurate MBC3 emulation. Fixes #244 --- Core/debugger.c | 23 ++++++++++++++--------- Core/gb.h | 6 +++--- Core/mbc.c | 11 +++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index 71f0861..8e7151b 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1420,15 +1420,20 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg } if (cartridge->mbc_type) { - static const char * const mapper_names[] = { - [GB_MBC1] = "MBC1", - [GB_MBC2] = "MBC2", - [GB_MBC3] = "MBC3", - [GB_MBC5] = "MBC5", - [GB_HUC1] = "HUC1", - [GB_HUC3] = "HUC3", - }; - GB_log(gb, "%s\n", mapper_names[cartridge->mbc_type]); + if (gb->is_mbc30) { + GB_log(gb, "MBC30\n"); + } + else { + static const char *const mapper_names[] = { + [GB_MBC1] = "MBC1", + [GB_MBC2] = "MBC2", + [GB_MBC3] = "MBC3", + [GB_MBC5] = "MBC5", + [GB_HUC1] = "HUC1", + [GB_HUC3] = "HUC3", + }; + GB_log(gb, "%s\n", mapper_names[cartridge->mbc_type]); + } GB_log(gb, "Current mapped ROM bank: %x\n", gb->mbc_rom_bank); if (cartridge->has_ram) { GB_log(gb, "Current mapped RAM bank: %x\n", gb->mbc_ram_bank); diff --git a/Core/gb.h b/Core/gb.h index b4ef658..0b188d6 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -403,9 +403,8 @@ struct GB_gameboy_internal_s { } mbc2; struct { - uint8_t rom_bank:7; - uint8_t padding:1; - uint8_t ram_bank:4; + uint8_t rom_bank:8; + uint8_t ram_bank:3; } mbc3; struct { @@ -538,6 +537,7 @@ struct GB_gameboy_internal_s { GB_STANDARD_MBC1_WIRING, GB_MBC1M_WIRING, } mbc1_wiring; + bool is_mbc30; unsigned pending_cycles; diff --git a/Core/mbc.c b/Core/mbc.c index d3791a1..2a96219 100644 --- a/Core/mbc.c +++ b/Core/mbc.c @@ -86,6 +86,10 @@ void GB_update_mbc_mappings(GB_gameboy_t *gb) case GB_MBC3: gb->mbc_rom_bank = gb->mbc3.rom_bank; gb->mbc_ram_bank = gb->mbc3.ram_bank; + if (!gb->is_mbc30) { + gb->mbc_rom_bank &= 0x7F; + gb->mbc_ram_bank &= 0x3; + } if (gb->mbc_rom_bank == 0) { gb->mbc_rom_bank = 1; } @@ -147,6 +151,13 @@ void GB_configure_cart(GB_gameboy_t *gb) } } + /* Detect MBC30 */ + if (gb->cartridge_type->mbc_type == GB_MBC3) { + if (gb->rom_size > 0x200000 || gb->mbc_ram_size > 0x8000) { + gb->is_mbc30 = true; + } + } + /* Set MBC5's bank to 1 correctly */ if (gb->cartridge_type->mbc_type == GB_MBC5) { gb->mbc5.rom_bank_low = 1; From d8e89f5114cb58b754baee4e1941bef21d42b861 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 8 Apr 2020 19:17:45 +0300 Subject: [PATCH 198/341] Fix banked 16-bit assignments; fixes #245 --- Core/debugger.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 8e7151b..65d9885 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -220,7 +220,8 @@ static value_t read_lvalue(GB_gameboy_t *gb, lvalue_t lvalue) banking_state_t state; save_banking_state(gb, &state); switch_banking_state(gb, lvalue.memory_address.bank); - value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value)); + value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value) | + (GB_read_memory(gb, lvalue.memory_address.value + 1) * 0x100)); restore_banking_state(gb, &state); return r; } @@ -261,6 +262,7 @@ static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value) save_banking_state(gb, &state); switch_banking_state(gb, lvalue.memory_address.bank); GB_write_memory(gb, lvalue.memory_address.value, value); + GB_write_memory(gb, lvalue.memory_address.value + 1, value >> 8); restore_banking_state(gb, &state); return; } From 92d6cc6394f5c7f8114842af271e2b47ab98ff29 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 02:36:27 +0300 Subject: [PATCH 199/341] Use official register names --- Core/gb.h | 6 +++--- Core/memory.c | 14 +++++++------- Misc/registers.sym | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index 0b188d6..3b83b6d 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -185,7 +185,7 @@ enum { // Unfortunately it is not readable or writable after boot has finished, so research of this // register is quite limited. The value written to this register, however, can be controlled // in some cases. - GB_IO_MODE = 0x4c, + GB_IO_KEY0 = 0x4c, /* General CGB features */ GB_IO_KEY1 = 0x4d, // CGB Mode Only - Prepare Speed Switch @@ -193,7 +193,7 @@ enum { /* Missing */ GB_IO_VBK = 0x4f, // CGB Mode Only - VRAM Bank - GB_IO_BIOS = 0x50, // Write to disable the BIOS mapping + GB_IO_BANK = 0x50, // Write to disable the BIOS mapping /* CGB DMA */ GB_IO_HDMA1 = 0x51, // CGB Mode Only - New DMA Source, High @@ -212,7 +212,7 @@ enum { GB_IO_BGPD = 0x69, // CGB Mode Only - Background Palette Data GB_IO_OBPI = 0x6a, // CGB Mode Only - Sprite Palette Index GB_IO_OBPD = 0x6b, // CGB Mode Only - Sprite Palette Data - GB_IO_OBJECT_PRIORITY = 0x6c, // Affects object priority (X based or index based) + GB_IO_OPRI = 0x6c, // Affects object priority (X based or index based) /* Missing */ diff --git a/Core/memory.c b/Core/memory.c index 148c986..27f58e1 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -295,11 +295,11 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) return gb->io_registers[GB_IO_TAC] | 0xF8; case GB_IO_STAT: return gb->io_registers[GB_IO_STAT] | 0x80; - case GB_IO_OBJECT_PRIORITY: + case GB_IO_OPRI: if (!GB_is_cgb(gb)) { return 0xFF; } - return gb->io_registers[GB_IO_OBJECT_PRIORITY] | 0xFE; + return gb->io_registers[GB_IO_OPRI] | 0xFE; case GB_IO_PCM_12: if (!GB_is_cgb(gb)) return 0xFF; @@ -663,8 +663,8 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case GB_IO_UNKNOWN5: gb->io_registers[addr & 0xFF] = value; return; - case GB_IO_OBJECT_PRIORITY: - if ((!gb->boot_rom_finished || (gb->io_registers[GB_IO_MODE] & 8)) && GB_is_cgb(gb)) { + case GB_IO_OPRI: + if ((!gb->boot_rom_finished || (gb->io_registers[GB_IO_KEY0] & 8)) && GB_is_cgb(gb)) { gb->io_registers[addr & 0xFF] = value; gb->object_priority = (value & 1) ? GB_OBJECT_PRIORITY_X : GB_OBJECT_PRIORITY_INDEX; } @@ -785,14 +785,14 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } return; - case GB_IO_BIOS: + case GB_IO_BANK: gb->boot_rom_finished = true; return; - case GB_IO_MODE: + case GB_IO_KEY0: if (GB_is_cgb(gb) && !gb->boot_rom_finished) { gb->cgb_mode = !(value & 0xC); /* The real "contents" of this register aren't quite known yet. */ - gb->io_registers[GB_IO_MODE] = value; + gb->io_registers[GB_IO_KEY0] = value; } return; diff --git a/Misc/registers.sym b/Misc/registers.sym index ea76ab3..3b31b74 100644 --- a/Misc/registers.sym +++ b/Misc/registers.sym @@ -41,10 +41,10 @@ 00:FF49 IO_OBP1 00:FF4A IO_WY 00:FF4B IO_WX -00:FF4C IO_DMG_EMULATION +00:FF4C IO_KEY0 00:FF4D IO_KEY1 00:FF4F IO_VBK -00:FF50 IO_BIOS +00:FF50 IO_BANK 00:FF51 IO_HDMA1 00:FF52 IO_HDMA2 00:FF53 IO_HDMA3 @@ -55,7 +55,7 @@ 00:FF69 IO_BGPD 00:FF6A IO_OBPI 00:FF6B IO_OBPD -00:FF6C IO_OBJECT_PRIORITY +00:FF6C IO_OPRI 00:FF70 IO_SVBK 00:FF72 IO_UNKNOWN2 00:FF73 IO_UNKNOWN3 From a9cd3f2c1195e874f6368792d057aa61ee1353ba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 14:21:07 +0300 Subject: [PATCH 200/341] Fix operator priorities, fix parsing debugger bug --- Core/debugger.c | 56 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index 65d9885..7d2ae89 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -334,7 +334,7 @@ static value_t bank(value_t a, value_t b) {return (value_t) {true, a.value, b.va static struct { const char *string; - char priority; + int8_t priority; value_t (*operator)(value_t, value_t); value_t (*lvalue_operator)(GB_gameboy_t *, lvalue_t, uint16_t); } operators[] = @@ -352,15 +352,15 @@ static struct { {"&", 1, and}, {"^", 1, xor}, {"<<", 2, shleft}, - {"<=", -1, lower_equals}, - {"<", -1, lower}, + {"<=", 3, lower_equals}, + {"<", 3, lower}, {">>", 2, shright}, - {">=", -1, greater_equals}, - {">", -1, greater}, - {"==", -1, equals}, - {"=", -2, NULL, assign}, - {"!=", -1, different}, - {":", 3, bank}, + {">=", 3, greater_equals}, + {">", 3, greater}, + {"==", 3, equals}, + {"=", -1, NULL, assign}, + {"!=", 3, different}, + {":", 4, bank}, }; value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, @@ -388,8 +388,8 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string, } if (string[0] == '(' && string[length - 1] == ')') { // Attempt to strip parentheses - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '(') depth++; if (depth == 0) { // First and last are not matching @@ -402,8 +402,8 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string, } else if (string[0] == '[' && string[length - 1] == ']') { // Attempt to strip square parentheses (memory dereference) - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '[') depth++; if (depth == 0) { // First and last are not matching @@ -418,8 +418,8 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string, } else if (string[0] == '{' && string[length - 1] == '}') { // Attempt to strip curly parentheses (memory dereference) - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '{') depth++; if (depth == 0) { // First and last are not matching @@ -495,8 +495,8 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } if (string[0] == '(' && string[length - 1] == ')') { // Attempt to strip parentheses - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '(') depth++; if (depth == 0) { // First and last are not matching @@ -512,8 +512,8 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } else if (string[0] == '[' && string[length - 1] == ']') { // Attempt to strip square parentheses (memory dereference) - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '[') depth++; if (depth == 0) { // First and last are not matching @@ -539,8 +539,8 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } else if (string[0] == '{' && string[length - 1] == '}') { // Attempt to strip curly parentheses (memory dereference) - signed int depth = 0; - for (int i = 0; i < length; i++) { + signed depth = 0; + for (unsigned i = 0; i < length; i++) { if (string[i] == '{') depth++; if (depth == 0) { // First and last are not matching @@ -565,7 +565,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } } // Search for lowest priority operator - signed int depth = 0; + signed depth = 0; unsigned operator_index = -1; unsigned operator_pos = 0; for (int i = 0; i < length; i++) { @@ -574,15 +574,15 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, else if (string[i] == '[') depth++; else if (string[i] == ']') depth--; else if (depth == 0) { - for (int j = 0; j < sizeof(operators) / sizeof(operators[0]); j++) { - if (strlen(operators[j].string) > length - i) continue; // Operator too big. - // Priority higher than what we already have. - unsigned long operator_length = strlen(operators[j].string); + for (unsigned j = 0; j < sizeof(operators) / sizeof(operators[0]); j++) { + unsigned operator_length = strlen(operators[j].string); + if (operator_length > length - i) continue; // Operator too long + if (memcmp(string + i, operators[j].string, operator_length) == 0) { if (operator_index != -1 && operators[operator_index].priority < operators[j].priority) { /* for supporting = vs ==, etc*/ i += operator_length - 1; - continue; + break; } // Found an operator! operator_pos = i; @@ -669,7 +669,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, } char *end; - int base = 10; + unsigned base = 10; if (string[0] == '$') { string++; base = 16; From a6567d9ee16141d7e2e39f9b93251564613a99f8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 14:32:52 +0300 Subject: [PATCH 201/341] Update old coding style --- Cocoa/Document.m | 10 +++++----- Core/apu.c | 2 +- Core/camera.c | 6 +++--- Core/debugger.c | 20 ++++++++++---------- Core/display.c | 2 +- Core/gb.c | 2 +- Core/gb.h | 14 +++++++------- Core/mbc.c | 2 +- Core/sm83_cpu.c | 14 +++++++------- Core/symbol_hash.c | 8 ++++---- SDL/gui.c | 6 +++--- SDL/shader.c | 2 +- 12 files changed, 44 insertions(+), 44 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index b835233..92d5f97 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -731,8 +731,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSRect rect = window.contentView.frame; - int titlebarSize = window.contentView.superview.frame.size.height - rect.size.height; - int step = width / [[window screen] backingScaleFactor]; + unsigned titlebarSize = window.contentView.superview.frame.size.height - rect.size.height; + unsigned step = width / [[window screen] backingScaleFactor]; rect.size.width = floor(rect.size.width / step) * step + step; rect.size.height = rect.size.width * height / width + titlebarSize; @@ -1468,7 +1468,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn]; if (tableView == self.paletteTableView) { if (columnIndex == 0) { - return [NSString stringWithFormat:@"%s %d", row >=8 ? "Object" : "Background", (int)(row & 7)]; + return [NSString stringWithFormat:@"%s %u", row >=8 ? "Object" : "Background", (unsigned)(row & 7)]; } uint8_t *palette_data = GB_get_direct_access(&gb, row >= 8? GB_DIRECT_ACCESS_OBP : GB_DIRECT_ACCESS_BGP, NULL, NULL); @@ -1486,9 +1486,9 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) height:oamHeight scale:16.0/oamHeight]; case 1: - return @((int)oamInfo[row].x - 8); + return @((unsigned)oamInfo[row].x - 8); case 2: - return @((int)oamInfo[row].y - 16); + return @((unsigned)oamInfo[row].y - 16); case 3: return [NSString stringWithFormat:@"$%02x", oamInfo[row].tile]; case 4: diff --git a/Core/apu.c b/Core/apu.c index c3be533..feda3c8 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -548,7 +548,7 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg) { if (reg == GB_IO_NR52) { uint8_t value = 0; - for (int i = 0; i < GB_N_CHANNELS; i++) { + for (unsigned i = 0; i < GB_N_CHANNELS; i++) { value >>= 1; if (gb->apu.is_active[i]) { value |= 0x8; diff --git a/Core/camera.c b/Core/camera.c index 9b34998..bef8489 100644 --- a/Core/camera.c +++ b/Core/camera.c @@ -1,17 +1,17 @@ #include "gb.h" -static int noise_seed = 0; +static signed noise_seed = 0; /* This is not a complete emulation of the camera chip. Only the features used by the GameBoy Camera ROMs are supported. We also do not emulate the timing of the real cart, as it might be actually faster than the webcam. */ static uint8_t generate_noise(uint8_t x, uint8_t y) { - int value = (x + y * 128 + noise_seed); + signed value = (x + y * 128 + noise_seed); uint8_t *data = (uint8_t *) &value; unsigned hash = 0; - while ((int *) data != &value + 1) { + while ((signed *) data != &value + 1) { hash ^= (*data << 8); if (hash & 0x8000) { hash ^= 0x8a00; diff --git a/Core/debugger.c b/Core/debugger.c index 7d2ae89..4e3bd63 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -568,7 +568,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, signed depth = 0; unsigned operator_index = -1; unsigned operator_pos = 0; - for (int i = 0; i < length; i++) { + for (unsigned i = 0; i < length; i++) { if (string[i] == '(') depth++; else if (string[i] == ')') depth--; else if (string[i] == '[') depth++; @@ -841,8 +841,8 @@ static uint16_t find_breakpoint(GB_gameboy_t *gb, value_t addr) uint32_t key = BP_KEY(addr); - int min = 0; - int max = gb->n_breakpoints; + unsigned min = 0; + unsigned max = gb->n_breakpoints; while (min < max) { uint16_t pivot = (min + max) / 2; if (gb->breakpoints[pivot].key == key) return pivot; @@ -1008,8 +1008,8 @@ static uint16_t find_watchpoint(GB_gameboy_t *gb, value_t addr) return 0; } uint32_t key = WP_KEY(addr); - int min = 0; - int max = gb->n_watchpoints; + unsigned min = 0; + unsigned max = gb->n_watchpoints; while (min < max) { uint16_t pivot = (min + max) / 2; if (gb->watchpoints[pivot].key == key) return pivot; @@ -1342,7 +1342,7 @@ static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const de while (count) { GB_log(gb, "%02x:%04x: ", addr.bank, addr.value); - for (int i = 0; i < 16 && count; i++) { + for (unsigned i = 0; i < 16 && count; i++) { GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i)); count--; } @@ -1355,7 +1355,7 @@ static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const de else { while (count) { GB_log(gb, "%04x: ", addr.value); - for (int i = 0; i < 16 && count; i++) { + for (unsigned i = 0; i < 16 && count; i++) { GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i)); count--; } @@ -1493,7 +1493,7 @@ static bool ticks(GB_gameboy_t *gb, char *arguments, char *modifiers, const debu return true; } - GB_log(gb, "Ticks: %lu. (Resetting)\n", gb->debugger_ticks); + GB_log(gb, "Ticks: %llu. (Resetting)\n", (unsigned long long)gb->debugger_ticks); gb->debugger_ticks = 0; return true; @@ -2197,13 +2197,13 @@ void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path) void GB_debugger_clear_symbols(GB_gameboy_t *gb) { - for (int i = sizeof(gb->bank_symbols) / sizeof(gb->bank_symbols[0]); i--;) { + for (unsigned i = sizeof(gb->bank_symbols) / sizeof(gb->bank_symbols[0]); i--;) { if (gb->bank_symbols[i]) { GB_map_free(gb->bank_symbols[i]); gb->bank_symbols[i] = 0; } } - for (int i = sizeof(gb->reversed_symbol_map.buckets) / sizeof(gb->reversed_symbol_map.buckets[0]); i--;) { + for (unsigned i = sizeof(gb->reversed_symbol_map.buckets) / sizeof(gb->reversed_symbol_map.buckets[0]); i--;) { while (gb->reversed_symbol_map.buckets[i]) { GB_symbol_t *next = gb->reversed_symbol_map.buckets[i]->next; free(gb->reversed_symbol_map.buckets[i]); diff --git a/Core/display.c b/Core/display.c index a7dda7d..356e742 100644 --- a/Core/display.c +++ b/Core/display.c @@ -1421,7 +1421,7 @@ uint8_t GB_get_oam_info(GB_gameboy_t *gb, GB_oam_info_t *dest, uint8_t *sprite_h GB_object_t *sprite = (GB_object_t *) &gb->oam; uint8_t sprites_in_line = 0; for (uint8_t i = 0; i < 40; i++, sprite++) { - int sprite_y = sprite->y - 16; + signed sprite_y = sprite->y - 16; bool obscured = false; // Is sprite not in this line? if (sprite_y > y || sprite_y + *sprite_height <= y) continue; diff --git a/Core/gb.c b/Core/gb.c index 31fff0d..ba3f20a 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -709,7 +709,7 @@ void GB_set_infrared_input(GB_gameboy_t *gb, bool state) gb->ir_queue_length = 0; } -void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, long cycles_after_previous_change) +void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, uint64_t cycles_after_previous_change) { if (gb->ir_queue_length == GB_MAX_IR_QUEUE) { GB_log(gb, "IR Queue is full\n"); diff --git a/Core/gb.h b/Core/gb.h index 3b83b6d..1f3bacf 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -265,7 +265,7 @@ typedef void (*GB_vblank_callback_t)(GB_gameboy_t *gb); typedef void (*GB_log_callback_t)(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes); typedef char *(*GB_input_callback_t)(GB_gameboy_t *gb); typedef uint32_t (*GB_rgb_encode_callback_t)(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b); -typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on, long cycles_since_last_update); +typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on, uint64_t cycles_since_last_update); typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, bool rumble_on); typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool bit_to_send); typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); @@ -278,7 +278,7 @@ typedef void (*GB_boot_rom_load_callback_t)(GB_gameboy_t *gb, GB_boot_rom_t type typedef struct { bool state; - long delay; + uint64_t delay; } GB_ir_queue_item_t; struct GB_breakpoint_s; @@ -587,8 +587,8 @@ struct GB_gameboy_internal_s { GB_boot_rom_load_callback_t boot_rom_load_callback; /* IR */ - long cycles_since_ir_change; // In 8MHz units - long cycles_since_input_ir_change; // In 8MHz units + uint64_t cycles_since_ir_change; // In 8MHz units + uint64_t cycles_since_input_ir_change; // In 8MHz units GB_ir_queue_item_t ir_queue[GB_MAX_IR_QUEUE]; size_t ir_queue_length; @@ -605,7 +605,7 @@ struct GB_gameboy_internal_s { /* SLD (Todo: merge with backtrace) */ bool stack_leak_detection; - int debug_call_depth; + signed debug_call_depth; uint16_t sp_for_call_depth[0x200]; /* Should be much more than enough */ uint16_t addr_for_call_depth[0x200]; @@ -626,7 +626,7 @@ struct GB_gameboy_internal_s { GB_reversed_symbol_map_t reversed_symbol_map; /* Ticks command */ - unsigned long debugger_ticks; + uint64_t debugger_ticks; /* Rewind */ #define GB_REWIND_FRAMES_PER_KEY 255 @@ -732,7 +732,7 @@ void GB_set_pixels_output(GB_gameboy_t *gb, uint32_t *output); void GB_set_border_mode(GB_gameboy_t *gb, GB_border_mode_t border_mode); void GB_set_infrared_input(GB_gameboy_t *gb, bool state); -void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, long cycles_after_previous_change); /* In 8MHz units*/ +void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, uint64_t cycles_after_previous_change); /* In 8MHz units*/ void GB_set_vblank_callback(GB_gameboy_t *gb, GB_vblank_callback_t callback); void GB_set_log_callback(GB_gameboy_t *gb, GB_log_callback_t callback); diff --git a/Core/mbc.c b/Core/mbc.c index 2a96219..2ee53e8 100644 --- a/Core/mbc.c +++ b/Core/mbc.c @@ -132,7 +132,7 @@ void GB_configure_cart(GB_gameboy_t *gb) gb->mbc_ram_size = 0x200; } else { - static const int ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000}; + static const unsigned ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000}; gb->mbc_ram_size = ram_sizes[gb->rom[0x149]]; } gb->mbc_ram = malloc(gb->mbc_ram_size); diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index da980eb..2b7b1dd 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -471,7 +471,7 @@ static void add_hl_rr(GB_gameboy_t *gb, uint8_t opcode) gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if ( ((unsigned long) hl + (unsigned long) rr) & 0x10000) { + if ( ((unsigned) hl + (unsigned) rr) & 0x10000) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -802,7 +802,7 @@ static void add_a_r(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) + (value & 0xF) > 0x0F) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) + ((unsigned long) value) > 0xFF) { + if (((unsigned) a) + ((unsigned) value) > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -821,7 +821,7 @@ static void adc_a_r(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) + ((unsigned long) value) + carry > 0xFF) { + if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -857,7 +857,7 @@ static void sbc_a_r(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) < (value & 0xF) + carry) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) - ((unsigned long) value) - carry > 0xFF) { + if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -1001,7 +1001,7 @@ static void add_a_d8(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) + (value & 0xF) > 0x0F) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) + ((unsigned long) value) > 0xFF) { + if (((unsigned) a) + ((unsigned) value) > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -1020,7 +1020,7 @@ static void adc_a_d8(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) + (value & 0xF) + carry > 0x0F) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) + ((unsigned long) value) + carry > 0xFF) { + if (((unsigned) a) + ((unsigned) value) + carry > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } @@ -1056,7 +1056,7 @@ static void sbc_a_d8(GB_gameboy_t *gb, uint8_t opcode) if ((a & 0xF) < (value & 0xF) + carry) { gb->registers[GB_REGISTER_AF] |= GB_HALF_CARRY_FLAG; } - if (((unsigned long) a) - ((unsigned long) value) - carry > 0xFF) { + if (((unsigned) a) - ((unsigned) value) - carry > 0xFF) { gb->registers[GB_REGISTER_AF] |= GB_CARRY_FLAG; } } diff --git a/Core/symbol_hash.c b/Core/symbol_hash.c index 208e72d..33e3399 100644 --- a/Core/symbol_hash.c +++ b/Core/symbol_hash.c @@ -71,9 +71,9 @@ void GB_map_free(GB_symbol_map_t *map) free(map); } -static int hash_name(const char *name) +static unsigned hash_name(const char *name) { - int r = 0; + unsigned r = 0; while (*name) { r <<= 1; if (r & 0x400) { @@ -87,7 +87,7 @@ static int hash_name(const char *name) void GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB_bank_symbol_t *bank_symbol) { - int hash = hash_name(bank_symbol->name); + unsigned hash = hash_name(bank_symbol->name); GB_symbol_t *symbol = malloc(sizeof(*symbol)); symbol->name = bank_symbol->name; symbol->addr = bank_symbol->addr; @@ -98,7 +98,7 @@ void GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB const GB_symbol_t *GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, const char *name) { - int hash = hash_name(name); + unsigned hash = hash_name(name); GB_symbol_t *symbol = map->buckets[hash]; while (symbol) { diff --git a/SDL/gui.c b/SDL/gui.c index 8bc6551..201452a 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -142,8 +142,8 @@ void update_viewport(void) double y_factor = win_height / (double) GB_get_screen_height(&gb); if (configuration.scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) { - x_factor = (int)(x_factor); - y_factor = (int)(y_factor); + x_factor = (unsigned)(x_factor); + y_factor = (unsigned)(y_factor); } if (configuration.scaling_mode != GB_SDL_SCALING_ENTIRE_WINDOW) { @@ -1265,7 +1265,7 @@ void run_gui(bool is_running) } if (item->value_getter && !item->backwards_handler) { char line[25]; - snprintf(line, sizeof(line), "%s%*s", item->string, 24 - (int)strlen(item->string), item->value_getter(i)); + snprintf(line, sizeof(line), "%s%*s", item->string, 24 - (unsigned)strlen(item->string), item->value_getter(i)); draw_text_centered(pixels, width, height, y + y_offset, line, gui_palette_native[3], gui_palette_native[0], i == current_selection ? DECORATION_SELECTION : DECORATION_NONE); y += 12; diff --git a/SDL/shader.c b/SDL/shader.c index 250046b..de2ba56 100644 --- a/SDL/shader.c +++ b/SDL/shader.c @@ -75,7 +75,7 @@ bool init_shader_with_name(shader_t *shader, const char *name) static char master_shader_code[0x801] = {0,}; static char shader_code[0x10001] = {0,}; static char final_shader_code[0x10801] = {0,}; - static signed long filter_token_location = 0; + static ssize_t filter_token_location = 0; if (!master_shader_code[0]) { FILE *master_shader_f = fopen(resource_path("Shaders/MasterShader.fsh"), "r"); From 4a21dd323226272b2c66ae2cc4236e13f98e2514 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 15:29:49 +0300 Subject: [PATCH 202/341] The Cocoa sidebar is now resizeable and collapseable --- Cocoa/Document.h | 5 +- Cocoa/Document.m | 38 ++++++ Cocoa/Document.xib | 304 ++++++++++++++++++++++++-------------------- Cocoa/GBSplitView.h | 15 +++ Cocoa/GBSplitView.m | 28 ++++ 5 files changed, 251 insertions(+), 139 deletions(-) create mode 100644 Cocoa/GBSplitView.h create mode 100644 Cocoa/GBSplitView.m diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 412e6ff..0a7f7e8 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -1,8 +1,9 @@ #import #include "GBView.h" #include "GBImageView.h" +#include "GBSplitView.h" -@interface Document : NSDocument +@interface Document : NSDocument @property (strong) IBOutlet GBView *view; @property (strong) IBOutlet NSTextView *consoleOutput; @property (strong) IBOutlet NSPanel *consoleWindow; @@ -30,6 +31,8 @@ @property (strong) IBOutlet NSButton *feedSaveButton; @property (strong) IBOutlet NSTextView *debuggerSideViewInput; @property (strong) IBOutlet NSTextView *debuggerSideView; +@property (strong) IBOutlet GBSplitView *debuggerSplitView; +@property (strong) IBOutlet NSBox *debuggerVerticalLine; -(uint8_t) readMemory:(uint16_t) addr; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 92d5f97..b7b56cf 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -505,6 +505,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self.feedSaveButton removeFromSuperview]; self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console – %@", [[self.fileURL path] lastPathComponent]]; + self.debuggerSplitView.dividerColor = [NSColor clearColor]; /* contentView.superview.subviews.lastObject is the titlebar view */ NSView *titleView = self.printerFeedWindow.contentView.superview.subviews.lastObject; @@ -1661,4 +1662,41 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } +- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview; +{ + if ([[splitView arrangedSubviews] lastObject] == subview) { + return YES; + } + return NO; +} + +- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex +{ + return 600; +} + +- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex { + return splitView.frame.size.width - 321; +} + +- (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view { + if ([[splitView arrangedSubviews] lastObject] == view) { + return NO; + } + return YES; +} + +- (void)splitViewDidResizeSubviews:(NSNotification *)notification +{ + NSSplitView *splitview = notification.object; + if ([[[splitview arrangedSubviews] firstObject] frame].size.width < 600) { + [splitview setPosition:600 ofDividerAtIndex:0]; + } + /* NSSplitView renders its separator without the proper vibrancy, so we made it transparent and move an + NSBox-based separator that renders properly so it acts like the split view's separator. */ + NSRect rect = self.debuggerVerticalLine.frame; + rect.origin.x = [[[splitview arrangedSubviews] firstObject] frame].size.width - 1; + self.debuggerVerticalLine.frame = rect; +} + @end diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index ae9cf90..c680df5 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -1,8 +1,8 @@ - + - + @@ -14,6 +14,8 @@ + + @@ -39,7 +41,7 @@ - + @@ -50,11 +52,11 @@ - + - + @@ -67,7 +69,7 @@ - + @@ -78,38 +80,7 @@ - - - - - - - - - - - - - - - - - NSAllRomanInputSourcesLocaleIdentifier - - - - - - - - - - - - + @@ -124,83 +95,140 @@ - + - - - - - - - - - - - - - - - - NSAllRomanInputSourcesLocaleIdentifier - - - - - - - - - - - - - - - - - - - - - - - - - - - NSAllRomanInputSourcesLocaleIdentifier - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + + + + + + + - + @@ -208,7 +236,7 @@ - + @@ -248,7 +276,7 @@ - + @@ -265,7 +293,7 @@ - + @@ -284,7 +312,7 @@ - + @@ -292,13 +320,13 @@ - + - + - + @@ -307,17 +335,17 @@ - + - + - + @@ -326,7 +354,7 @@ - + @@ -358,7 +386,7 @@ - - + @@ -397,7 +425,7 @@ - + @@ -430,7 +458,7 @@ - + @@ -448,7 +476,7 @@ - + @@ -474,10 +502,10 @@ - + - + @@ -597,11 +625,11 @@ - - + @@ -765,7 +793,7 @@ - + diff --git a/Cocoa/GBSplitView.h b/Cocoa/GBSplitView.h new file mode 100644 index 0000000..143b8f6 --- /dev/null +++ b/Cocoa/GBSplitView.h @@ -0,0 +1,15 @@ +// +// GBSplitView.h +// SameBoySDL +// +// Created by Lior Halphon on 9/4/20. +// Copyright © 2020 Lior Halphon. All rights reserved. +// + +#import + +@interface GBSplitView : NSSplitView + +-(void) setDividerColor:(NSColor *)color; + +@end diff --git a/Cocoa/GBSplitView.m b/Cocoa/GBSplitView.m new file mode 100644 index 0000000..425d965 --- /dev/null +++ b/Cocoa/GBSplitView.m @@ -0,0 +1,28 @@ +// +// GBSplitView.m +// SameBoySDL +// +// Created by Lior Halphon on 9/4/20. +// Copyright © 2020 Lior Halphon. All rights reserved. +// + +#import "GBSplitView.h" + +@implementation GBSplitView +{ + NSColor *_dividerColor; +} + +- (void)setDividerColor:(NSColor *)color { + _dividerColor = color; + [self setNeedsDisplay:YES]; +} + +- (NSColor *)dividerColor { + if (_dividerColor) { + return _dividerColor; + } + return [super dividerColor]; +} + +@end From 1d80c185d85314c7d23b3e07d821ab1c3f88ea3b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 17:25:14 +0300 Subject: [PATCH 203/341] Remove IDE comment --- Cocoa/GBSplitView.h | 8 -------- Cocoa/GBSplitView.m | 8 -------- 2 files changed, 16 deletions(-) diff --git a/Cocoa/GBSplitView.h b/Cocoa/GBSplitView.h index 143b8f6..7b2faa2 100644 --- a/Cocoa/GBSplitView.h +++ b/Cocoa/GBSplitView.h @@ -1,11 +1,3 @@ -// -// GBSplitView.h -// SameBoySDL -// -// Created by Lior Halphon on 9/4/20. -// Copyright © 2020 Lior Halphon. All rights reserved. -// - #import @interface GBSplitView : NSSplitView diff --git a/Cocoa/GBSplitView.m b/Cocoa/GBSplitView.m index 425d965..0a30fe0 100644 --- a/Cocoa/GBSplitView.m +++ b/Cocoa/GBSplitView.m @@ -1,11 +1,3 @@ -// -// GBSplitView.m -// SameBoySDL -// -// Created by Lior Halphon on 9/4/20. -// Copyright © 2020 Lior Halphon. All rights reserved. -// - #import "GBSplitView.h" @implementation GBSplitView From 337e74352d5666c20ebe4a5982fa3424a5c8406b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 9 Apr 2020 20:11:55 +0300 Subject: [PATCH 204/341] Add cheats API, with GameShark and GameGenie import --- Core/cheats.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++ Core/cheats.h | 33 ++++++++++ Core/gb.c | 3 + Core/gb.h | 6 ++ Core/memory.c | 6 +- 5 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 Core/cheats.c create mode 100644 Core/cheats.h diff --git a/Core/cheats.c b/Core/cheats.c new file mode 100644 index 0000000..36b4afb --- /dev/null +++ b/Core/cheats.c @@ -0,0 +1,173 @@ +#include "gb.h" +#include "cheats.h" +#include + +static inline uint8_t hash_addr(uint16_t addr) +{ + return addr; +} + +static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr) +{ + if (addr < 0x4000) { + return gb->mbc_rom0_bank; + } + + if (addr < 0x8000) { + return gb->mbc_rom_bank; + } + + if (addr < 0xD000) { + return 0; + } + + if (addr < 0xE000) { + return gb->cgb_ram_bank; + } + + return 0; +} + +void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value) +{ + const GB_cheat_hash_t *hash = gb->cheat_hash[hash_addr(address)]; + if (hash) { + for (unsigned i = 0; i < hash->size; i++) { + GB_cheat_t *cheat = hash->cheats[i]; + if (cheat->address == address && cheat->enabled && (!cheat->use_old_value || cheat->old_value == *value)) { + if (cheat->bank == GB_CHEAT_ANY_BANK || cheat->bank == bank_for_addr(gb, address)) { + *value = cheat->value; + break; + } + } + } + } +} + +void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled) +{ + GB_cheat_t *cheat = malloc(sizeof(*cheat)); + cheat->address = address; + cheat->bank = bank; + cheat->value = value; + cheat->old_value = old_value; + cheat->use_old_value = use_old_value; + cheat->enabled = enabled; + strncpy(cheat->description, description, sizeof(cheat->description)); + cheat->description[sizeof(cheat->description) - 1] = 0; + gb->cheats = realloc(gb->cheats, (++gb->cheat_count) * sizeof(*cheat)); + gb->cheats[gb->cheat_count - 1] = cheat; + + GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(address)]; + if (!*hash) { + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat)); + (*hash)->size = 1; + (*hash)->cheats[0] = cheat; + } + else { + (*hash)->size++; + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); + (*hash)->cheats[(*hash)->size - 1] = cheat; + } +} + +const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size) +{ + *size = gb->cheat_count; + return (void *)gb->cheats; +} +void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat) +{ + for (unsigned i = 0; i < gb->cheat_count; i++) { + if (gb->cheats[i] == cheat) { + gb->cheats[i] = gb->cheats[--gb->cheat_count]; + if (gb->cheat_count == 0) { + free(gb->cheats); + gb->cheats = NULL; + } + else { + gb->cheats = realloc(gb->cheats, gb->cheat_count * sizeof(*cheat)); + } + break; + } + } + + GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)]; + for (unsigned i = 0; i < (*hash)->size; i++) { + if ((*hash)->cheats[i] == cheat) { + (*hash)->cheats[i] = (*hash)->cheats[(*hash)->size--]; + if ((*hash)->size == 0) { + free(*hash); + *hash = NULL; + } + else { + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); + } + break; + } + } + + free((void *)cheat); +} + +bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *description, bool enabled) +{ + uint8_t dummy; + /* GameShark */ + { + uint8_t bank; + uint8_t value; + uint16_t address; + if (sscanf(cheat, "%02hhx%02hhx%04hx%c", &bank, &value, &address, &dummy) == 3) { + if (address > 0x7FFF) { + return false; + } + if (bank >= 0x80) { + bank &= 0xF; + } + GB_add_cheat(gb, description, address, bank, value, 0, false, enabled); + return true; + } + } + + /* GameGnie */ + { + char stripped_cheat[10] = {0,}; + for (unsigned i = 0; i < 9 && *cheat; i++) { + stripped_cheat[i] = *(cheat++); + while (*cheat == '-') { + cheat++; + } + } + + // Delete the 7th character; + stripped_cheat[7] = stripped_cheat[8]; + stripped_cheat[8] = 0; + + uint8_t old_value; + uint8_t value; + uint16_t address; + if (sscanf(stripped_cheat, "%02hhx%04hx%02hhx%c", &value, &address, &old_value, &dummy) == 3) { + address = (uint16_t)(address >> 4) | (uint16_t)(address << 12); + address ^= 0xF000; + if (address > 0x7FFF) { + return false; + } + old_value = (uint8_t)(old_value >> 2) | (uint8_t)(old_value << 6); + old_value ^= 0xBA; + GB_add_cheat(gb, description, address, GB_CHEAT_ANY_BANK, value, old_value, true, enabled); + return true; + } + + if (sscanf(stripped_cheat, "%02hhx%04hx%c", &value, &address, &dummy) == 2) { + address = (uint16_t)(address >> 4) | (uint16_t)(address << 12); + address ^= 0xF000; + if (address > 0x7FFF) { + return false; + } + GB_add_cheat(gb, description, address, GB_CHEAT_ANY_BANK, value, false, true, enabled); + return true; + } + } + return false; +} diff --git a/Core/cheats.h b/Core/cheats.h new file mode 100644 index 0000000..c461f22 --- /dev/null +++ b/Core/cheats.h @@ -0,0 +1,33 @@ +#ifndef cheats_h +#define cheats_h +#include "gb_struct_def.h" + +#define GB_CHEAT_ANY_BANK 0xFFFF + +typedef struct GB_cheat_s GB_cheat_t; + +void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled); +bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *description, bool enabled); +const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size); +void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat); + +#ifdef GB_INTERNAL +void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value); +#endif + +typedef struct { + size_t size; + GB_cheat_t *cheats[]; +} GB_cheat_hash_t; + +struct GB_cheat_s { + uint16_t address; + uint16_t bank; + uint8_t value; + uint8_t old_value; + bool use_old_value; + bool enabled; + char description[128]; +}; + +#endif diff --git a/Core/gb.c b/Core/gb.c index ba3f20a..75538e1 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -197,6 +197,9 @@ void GB_free(GB_gameboy_t *gb) GB_debugger_clear_symbols(gb); #endif GB_rewind_free(gb); + while (gb->cheats) { + GB_remove_cheat(gb, gb->cheats[0]); + } memset(gb, 0, sizeof(*gb)); } diff --git a/Core/gb.h b/Core/gb.h index 1f3bacf..8a60770 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -21,6 +21,7 @@ #include "sm83_cpu.h" #include "symbol_hash.h" #include "sgb.h" +#include "cheats.h" #define GB_STRUCT_VERSION 13 @@ -644,6 +645,11 @@ struct GB_gameboy_internal_s { double sgb_intro_jingle_phases[7]; double sgb_intro_sweep_phase; double sgb_intro_sweep_previous_sample; + + /* Cheats */ + size_t cheat_count; + GB_cheat_t **cheats; + GB_cheat_hash_t *cheat_hash[256]; /* Misc */ bool turbo; diff --git a/Core/memory.c b/Core/memory.c index 27f58e1..58b96bb 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -435,12 +435,12 @@ uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr) if (is_addr_in_dma_use(gb, addr)) { addr = gb->dma_current_src; } + uint8_t data = read_map[addr >> 12](gb, addr); + GB_apply_cheat(gb, addr, &data); if (gb->read_memory_callback) { - uint8_t data = read_map[addr >> 12](gb, addr); data = gb->read_memory_callback(gb, addr, data); - return data; } - return read_map[addr >> 12](gb, addr); + return data; } static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) From 852a6997ed869d14a5d92d97e62165dd3532cee5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 18:03:10 +0300 Subject: [PATCH 205/341] Add cheats UI to Cocoa --- Cocoa/Document.h | 1 + Cocoa/Document.m | 13 ++ Cocoa/Document.xib | 293 +++++++++++++++++++++++++++-- Cocoa/GBCheatTextFieldCell.h | 5 + Cocoa/GBCheatTextFieldCell.m | 121 ++++++++++++ Cocoa/GBCheatWindowController.h | 17 ++ Cocoa/GBCheatWindowController.m | 234 +++++++++++++++++++++++ Cocoa/GBImageCell.m | 2 +- Cocoa/GBImageView.m | 2 +- Cocoa/GBOptionalVisualEffectView.h | 6 + Cocoa/GBOptionalVisualEffectView.m | 18 ++ Cocoa/MainMenu.xib | 18 ++ Core/cheats.c | 69 ++++++- Core/cheats.h | 3 + Core/gb.h | 1 + Makefile | 2 +- 16 files changed, 782 insertions(+), 23 deletions(-) create mode 100644 Cocoa/GBCheatTextFieldCell.h create mode 100644 Cocoa/GBCheatTextFieldCell.m create mode 100644 Cocoa/GBCheatWindowController.h create mode 100644 Cocoa/GBCheatWindowController.m create mode 100644 Cocoa/GBOptionalVisualEffectView.h create mode 100644 Cocoa/GBOptionalVisualEffectView.m diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 0a7f7e8..1117c26 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -33,6 +33,7 @@ @property (strong) IBOutlet NSTextView *debuggerSideView; @property (strong) IBOutlet GBSplitView *debuggerSplitView; @property (strong) IBOutlet NSBox *debuggerVerticalLine; +@property (strong) IBOutlet NSPanel *cheatsWindow; -(uint8_t) readMemory:(uint16_t) addr; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; diff --git a/Cocoa/Document.m b/Cocoa/Document.m index b7b56cf..71436d3 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -706,6 +706,9 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) else if ([anItem action] == @selector(connectPrinter:)) { [(NSMenuItem*)anItem setState:accessory == GBAccessoryPrinter]; } + else if ([anItem action] == @selector(toggleCheats:)) { + [(NSMenuItem*)anItem setState:GB_cheats_enabled(&gb)]; + } return [super validateUserInterfaceItem:anItem]; } @@ -1699,4 +1702,14 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.debuggerVerticalLine.frame = rect; } +- (IBAction)showCheats:(id)sender +{ + [self.cheatsWindow makeKeyAndOrderFront:nil]; +} + +- (IBAction)toggleCheats:(id)sender +{ + GB_set_cheats_enabled(&gb, !GB_cheats_enabled(&gb)); +} + @end diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index c680df5..338650b 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -9,6 +9,7 @@ + @@ -44,8 +45,7 @@ - - + @@ -69,11 +69,10 @@ - + - - + @@ -103,7 +102,7 @@ - + @@ -228,11 +227,10 @@ - - + + - - + @@ -312,11 +310,10 @@ - - + + - - + @@ -784,10 +781,9 @@ - - - - + + + @@ -803,7 +799,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + NSAllRomanInputSourcesLocaleIdentifier + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Cocoa/GBCheatTextFieldCell.h b/Cocoa/GBCheatTextFieldCell.h new file mode 100644 index 0000000..473e0f3 --- /dev/null +++ b/Cocoa/GBCheatTextFieldCell.h @@ -0,0 +1,5 @@ +#import + +@interface GBCheatTextFieldCell : NSTextFieldCell +@property bool usesAddressFormat; +@end diff --git a/Cocoa/GBCheatTextFieldCell.m b/Cocoa/GBCheatTextFieldCell.m new file mode 100644 index 0000000..611cade --- /dev/null +++ b/Cocoa/GBCheatTextFieldCell.m @@ -0,0 +1,121 @@ +#import "GBCheatTextFieldCell.h" + +@interface GBCheatTextView : NSTextView +@property bool usesAddressFormat; +@end + +@implementation GBCheatTextView + +- (bool)_insertText:(NSString *)string replacementRange:(NSRange)range +{ + if (range.location == NSNotFound) { + range = self.selectedRange; + } + + NSString *new = [self.string stringByReplacingCharactersInRange:range withString:string]; + if (!self.usesAddressFormat) { + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(\\$[0-9A-Fa-f]{1,2}|[0-9]{1,3})$" options:0 error:NULL]; + if ([regex numberOfMatchesInString:new options:0 range:NSMakeRange(0, new.length)]) { + [super insertText:string replacementRange:range]; + return true; + } + if ([regex numberOfMatchesInString:[@"$" stringByAppendingString:new] options:0 range:NSMakeRange(0, new.length + 1)]) { + [super insertText:string replacementRange:range]; + [super insertText:@"$" replacementRange:NSMakeRange(0, 0)]; + return true; + } + if ([new isEqualToString:@"$"] || [string length] == 0) { + self.string = @"$00"; + self.selectedRange = NSMakeRange(1, 2); + return true; + } + } + else { + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(\\$[0-9A-Fa-f]{1,3}:)?\\$[0-9a-fA-F]{1,4}$" options:0 error:NULL]; + if ([regex numberOfMatchesInString:new options:0 range:NSMakeRange(0, new.length)]) { + [super insertText:string replacementRange:range]; + return true; + } + if ([string length] == 0) { + NSUInteger index = [new rangeOfString:@":"].location; + if (index != NSNotFound) { + if (range.location > index) { + self.string = [[new componentsSeparatedByString:@":"] firstObject]; + self.selectedRange = NSMakeRange(self.string.length, 0); + return true; + } + self.string = [[new componentsSeparatedByString:@":"] lastObject]; + self.selectedRange = NSMakeRange(0, 0); + return true; + } + else if ([[self.string substringWithRange:range] isEqualToString:@":"]) { + self.string = [[self.string componentsSeparatedByString:@":"] lastObject]; + self.selectedRange = NSMakeRange(0, 0); + return true; + } + } + if ([new isEqualToString:@"$"] || [string length] == 0) { + self.string = @"$0000"; + self.selectedRange = NSMakeRange(1, 4); + return true; + } + if (([string isEqualToString:@"$"] || [string isEqualToString:@":"]) && range.length == 0 && range.location == 0) { + if ([self _insertText:@"$00:" replacementRange:range]) { + self.selectedRange = NSMakeRange(1, 2); + return true; + } + } + if ([string isEqualToString:@":"] && range.length + range.location == self.string.length) { + if ([self _insertText:@":$0" replacementRange:range]) { + self.selectedRange = NSMakeRange(self.string.length - 2, 2); + return true; + } + } + if ([string isEqualToString:@"$"]) { + if ([self _insertText:@"$0" replacementRange:range]) { + self.selectedRange = NSMakeRange(range.location + 1, 1); + return true; + } + } + } + return false; +} + +- (NSUndoManager *)undoManager +{ + return nil; +} + +- (void)insertText:(id)string replacementRange:(NSRange)replacementRange +{ + if (![self _insertText:string replacementRange:replacementRange]) { + NSBeep(); + } +} + +/* Private API, don't tell the police! */ +- (void)_userReplaceRange:(NSRange)range withString:(NSString *)string +{ + [self insertText:string replacementRange:range]; +} + +@end + +@implementation GBCheatTextFieldCell +{ + bool _drawing, _editing; + GBCheatTextView *_fieldEditor; +} + +- (NSTextView *)fieldEditorForView:(NSView *)controlView +{ + if (_fieldEditor) { + _fieldEditor.usesAddressFormat = self.usesAddressFormat; + return _fieldEditor; + } + _fieldEditor = [[GBCheatTextView alloc] initWithFrame:controlView.frame]; + _fieldEditor.fieldEditor = YES; + _fieldEditor.usesAddressFormat = self.usesAddressFormat; + return _fieldEditor; +} +@end diff --git a/Cocoa/GBCheatWindowController.h b/Cocoa/GBCheatWindowController.h new file mode 100644 index 0000000..adb0bf8 --- /dev/null +++ b/Cocoa/GBCheatWindowController.h @@ -0,0 +1,17 @@ +#import +#import +#import "Document.h" + +@interface GBCheatWindowController : NSObject +@property (weak) IBOutlet NSTableView *cheatsTable; +@property (weak) IBOutlet NSTextField *addressField; +@property (weak) IBOutlet NSTextField *valueField; +@property (weak) IBOutlet NSTextField *oldValueField; +@property (weak) IBOutlet NSButton *oldValueCheckbox; +@property (weak) IBOutlet NSTextField *descriptionField; +@property (weak) IBOutlet NSTextField *importCodeField; +@property (weak) IBOutlet NSTextField *importDescriptionField; +@property (weak) IBOutlet Document *document; + +@end + diff --git a/Cocoa/GBCheatWindowController.m b/Cocoa/GBCheatWindowController.m new file mode 100644 index 0000000..994d5e1 --- /dev/null +++ b/Cocoa/GBCheatWindowController.m @@ -0,0 +1,234 @@ +#import "GBCheatWindowController.h" +#import "GBWarningPopover.h" +#import "GBCheatTextFieldCell.h" + +@implementation GBCheatWindowController + ++ (NSString *)addressStringFromCheat:(const GB_cheat_t *)cheat +{ + if (cheat->bank != GB_CHEAT_ANY_BANK) { + return [NSString stringWithFormat:@"$%x:$%04x", cheat->bank, cheat->address]; + } + return [NSString stringWithFormat:@"$%04x", cheat->address]; +} + ++ (NSString *)actionDescriptionForCheat:(const GB_cheat_t *)cheat +{ + if (cheat->use_old_value) { + return [NSString stringWithFormat:@"[%@]($%02x) = $%02x", [self addressStringFromCheat:cheat], cheat->old_value, cheat->value]; + } + return [NSString stringWithFormat:@"[%@] = $%02x", [self addressStringFromCheat:cheat], cheat->value]; +} + +- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return 0; + size_t cheatCount; + GB_get_cheats(gb, &cheatCount); + return cheatCount + 1; +} + +- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return nil; + size_t cheatCount; + GB_get_cheats(gb, &cheatCount); + NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn]; + if (row >= cheatCount && columnIndex == 0) { + return [[NSCell alloc] init]; + } + return nil; +} + +- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row +{ + size_t cheatCount; + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return nil; + const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount); + NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn]; + if (row >= cheatCount) { + switch (columnIndex) { + case 0: + return @(YES); + + case 1: + return @NO; + + case 2: + return @"Add Cheat..."; + + case 3: + return @""; + } + } + + switch (columnIndex) { + case 0: + return @(NO); + + case 1: + return @(cheats[row]->enabled); + + case 2: + return @(cheats[row]->description); + + case 3: + return [GBCheatWindowController actionDescriptionForCheat:cheats[row]]; + } + + return nil; +} + +- (IBAction)importCheat:(id)sender +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return; + + [self.document performAtomicBlock:^{ + if (GB_import_cheat(gb, + self.importCodeField.stringValue.UTF8String, + self.importDescriptionField.stringValue.UTF8String, + true)) { + self.importCodeField.stringValue = @""; + self.importDescriptionField.stringValue = @""; + [self.cheatsTable reloadData]; + [self tableViewSelectionDidChange:nil]; + } + else { + NSBeep(); + [GBWarningPopover popoverWithContents:@"This code is not a valid GameShark or GameGenie code" onView:self.importCodeField]; + } + }]; +} + +- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return; + size_t cheatCount; + const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount); + NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn]; + [self.document performAtomicBlock:^{ + if (columnIndex == 1) { + if (row >= cheatCount) { + GB_add_cheat(gb, "New Cheat", 0, 0, 0, 0, false, true); + } + else { + GB_update_cheat(gb, cheats[row], cheats[row]->description, cheats[row]->address, cheats[row]->bank, cheats[row]->value, cheats[row]->old_value, cheats[row]->use_old_value, !cheats[row]->enabled); + } + } + else if (row < cheatCount) { + GB_remove_cheat(gb, cheats[row]); + } + }]; + [self.cheatsTable reloadData]; + [self tableViewSelectionDidChange:nil]; +} + +- (void)tableViewSelectionDidChange:(NSNotification *)notification +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return; + + size_t cheatCount; + const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount); + unsigned row = self.cheatsTable.selectedRow; + const GB_cheat_t *cheat = NULL; + if (row >= cheatCount) { + static const GB_cheat_t template = { + .address = 0, + .bank = 0, + .value = 0, + .old_value = 0, + .use_old_value = false, + .enabled = false, + .description = "New Cheat", + }; + cheat = &template; + } + else { + cheat = cheats[row]; + } + + self.addressField.stringValue = [GBCheatWindowController addressStringFromCheat:cheat]; + self.valueField.stringValue = [NSString stringWithFormat:@"$%02x", cheat->value]; + self.oldValueField.stringValue = [NSString stringWithFormat:@"$%02x", cheat->old_value]; + self.oldValueCheckbox.state = cheat->use_old_value; + self.descriptionField.stringValue = @(cheat->description); +} + +- (void)awakeFromNib +{ + [self tableViewSelectionDidChange:nil]; + ((GBCheatTextFieldCell *)self.addressField.cell).usesAddressFormat = true; +} + +- (void)controlTextDidChange:(NSNotification *)obj +{ + [self updateCheat:nil]; +} + +- (IBAction)updateCheat:(id)sender +{ + GB_gameboy_t *gb = self.document.gameboy; + if (!gb) return; + + uint16_t address = 0; + uint16_t bank = GB_CHEAT_ANY_BANK; + if ([self.addressField.stringValue rangeOfString:@":"].location != NSNotFound) { + sscanf(self.addressField.stringValue.UTF8String, "$%hx:$%hx", &bank, &address); + } + else { + sscanf(self.addressField.stringValue.UTF8String, "$%hx", &address); + } + + uint8_t value = 0; + if ([self.valueField.stringValue characterAtIndex:0] == '$') { + sscanf(self.valueField.stringValue.UTF8String, "$%02hhx", &value); + } + else { + sscanf(self.valueField.stringValue.UTF8String, "%hhd", &value); + } + + uint8_t oldValue = 0; + if ([self.oldValueField.stringValue characterAtIndex:0] == '$') { + sscanf(self.oldValueField.stringValue.UTF8String, "$%02hhx", &oldValue); + } + else { + sscanf(self.oldValueField.stringValue.UTF8String, "%hhd", &oldValue); + } + + size_t cheatCount; + const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount); + unsigned row = self.cheatsTable.selectedRow; + + [self.document performAtomicBlock:^{ + if (row >= cheatCount) { + GB_add_cheat(gb, + self.descriptionField.stringValue.UTF8String, + address, + bank, + value, + oldValue, + self.oldValueCheckbox.state, + false); + } + else { + GB_update_cheat(gb, + cheats[row], + self.descriptionField.stringValue.UTF8String, + address, + bank, + value, + oldValue, + self.oldValueCheckbox.state, + cheats[row]->enabled); + } + }]; + [self.cheatsTable reloadData]; +} + +@end diff --git a/Cocoa/GBImageCell.m b/Cocoa/GBImageCell.m index 6f54ec8..de75e0e 100644 --- a/Cocoa/GBImageCell.m +++ b/Cocoa/GBImageCell.m @@ -3,7 +3,7 @@ @implementation GBImageCell - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { - CGContextRef context = [[NSGraphicsContext currentContext] CGContext]; + CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort]; CGContextSetInterpolationQuality(context, kCGInterpolationNone); [super drawWithFrame:cellFrame inView:controlView]; } diff --git a/Cocoa/GBImageView.m b/Cocoa/GBImageView.m index 973625e..47efa00 100644 --- a/Cocoa/GBImageView.m +++ b/Cocoa/GBImageView.m @@ -16,7 +16,7 @@ } - (void)drawRect:(NSRect)dirtyRect { - CGContextRef context = [[NSGraphicsContext currentContext] CGContext]; + CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort]; CGContextSetInterpolationQuality(context, kCGInterpolationNone); [super drawRect:dirtyRect]; CGFloat y_ratio = self.frame.size.height / self.image.size.height; diff --git a/Cocoa/GBOptionalVisualEffectView.h b/Cocoa/GBOptionalVisualEffectView.h new file mode 100644 index 0000000..1355071 --- /dev/null +++ b/Cocoa/GBOptionalVisualEffectView.h @@ -0,0 +1,6 @@ +#import + +/* Fake interface so the compiler assumes it conforms to NSVisualEffectView */ +@interface GBOptionalVisualEffectView : NSVisualEffectView + +@end diff --git a/Cocoa/GBOptionalVisualEffectView.m b/Cocoa/GBOptionalVisualEffectView.m new file mode 100644 index 0000000..c28eb59 --- /dev/null +++ b/Cocoa/GBOptionalVisualEffectView.m @@ -0,0 +1,18 @@ +#import + +@interface GBOptionalVisualEffectView : NSView + +@end + +@implementation GBOptionalVisualEffectView + ++ (instancetype)allocWithZone:(struct _NSZone *)zone +{ + Class NSVisualEffectView = NSClassFromString(@"NSVisualEffectView"); + if (NSVisualEffectView) { + return (id)[NSVisualEffectView alloc]; + } + return [super allocWithZone:zone]; +} + +@end diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index ee989ee..e56b4a0 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -345,6 +345,24 @@
+ + + + + + + + + + + + + + + + + + diff --git a/Core/cheats.c b/Core/cheats.c index 36b4afb..fa9b6a7 100644 --- a/Core/cheats.c +++ b/Core/cheats.c @@ -1,6 +1,7 @@ #include "gb.h" #include "cheats.h" #include +#include static inline uint8_t hash_addr(uint16_t addr) { @@ -30,6 +31,8 @@ static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr) void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value) { + if (!gb->cheat_enabled) return; + if (!gb->boot_rom_finished) return; const GB_cheat_hash_t *hash = gb->cheat_hash[hash_addr(address)]; if (hash) { for (unsigned i = 0; i < hash->size; i++) { @@ -44,6 +47,16 @@ void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value) } } +bool GB_cheats_enabled(GB_gameboy_t *gb) +{ + return gb->cheat_enabled; +} + +void GB_set_cheats_enabled(GB_gameboy_t *gb, bool enabled) +{ + gb->cheat_enabled = enabled; +} + void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled) { GB_cheat_t *cheat = malloc(sizeof(*cheat)); @@ -66,7 +79,7 @@ void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, u } else { (*hash)->size++; - *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); + *hash = realloc(*hash, sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); (*hash)->cheats[(*hash)->size - 1] = cheat; } } @@ -171,3 +184,57 @@ bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *descriptio } return false; } + +void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled) +{ + GB_cheat_t *cheat = NULL; + for (unsigned i = 0; i < gb->cheat_count; i++) { + if (gb->cheats[i] == _cheat) { + cheat = gb->cheats[i]; + break; + } + } + + assert(cheat); + + if (cheat->address != address) { + /* Remove from old bucket */ + GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(cheat->address)]; + for (unsigned i = 0; i < (*hash)->size; i++) { + if ((*hash)->cheats[i] == cheat) { + (*hash)->cheats[i] = (*hash)->cheats[(*hash)->size--]; + if ((*hash)->size == 0) { + free(*hash); + *hash = NULL; + } + else { + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); + } + break; + } + } + cheat->address = address; + + /* Add to new bucket */ + hash = &gb->cheat_hash[hash_addr(address)]; + if (!*hash) { + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat)); + (*hash)->size = 1; + (*hash)->cheats[0] = cheat; + } + else { + (*hash)->size++; + *hash = malloc(sizeof(GB_cheat_hash_t) + sizeof(cheat) * (*hash)->size); + (*hash)->cheats[(*hash)->size - 1] = cheat; + } + } + cheat->bank = bank; + cheat->value = value; + cheat->old_value = old_value; + cheat->use_old_value = use_old_value; + cheat->enabled = enabled; + if (description != cheat->description) { + strncpy(cheat->description, description, sizeof(cheat->description)); + cheat->description[sizeof(cheat->description) - 1] = 0; + } +} diff --git a/Core/cheats.h b/Core/cheats.h index c461f22..be5b39a 100644 --- a/Core/cheats.h +++ b/Core/cheats.h @@ -7,9 +7,12 @@ typedef struct GB_cheat_s GB_cheat_t; void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled); +void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat, const char *description, uint16_t address, uint16_t bank, uint8_t value, uint8_t old_value, bool use_old_value, bool enabled); bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *description, bool enabled); const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size); void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat); +bool GB_cheats_enabled(GB_gameboy_t *gb); +void GB_set_cheats_enabled(GB_gameboy_t *gb, bool enabled); #ifdef GB_INTERNAL void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value); diff --git a/Core/gb.h b/Core/gb.h index 8a60770..03abfa1 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -647,6 +647,7 @@ struct GB_gameboy_internal_s { double sgb_intro_sweep_previous_sample; /* Cheats */ + bool cheat_enabled; size_t cheat_count; GB_cheat_t **cheats; GB_cheat_hash_t *cheat_hash[256]; diff --git a/Makefile b/Makefile index 7c22375..5923b2e 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,7 @@ OPEN_DIALOG = OpenDialog/cocoa.m endif -CFLAGS += -Werror -Wall -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES +CFLAGS += -Werror -Wall -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) SDL_CFLAGS := $(shell sdl2-config --cflags) SDL_LDFLAGS := $(shell sdl2-config --libs) From 2bc75caf9efe0c4f44bd2e6f9bcc154664a7402b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 18:03:21 +0300 Subject: [PATCH 206/341] Fix CRT shader on OpenGL --- Shaders/CRT.fsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shaders/CRT.fsh b/Shaders/CRT.fsh index c1ae7ef..0bc4c65 100644 --- a/Shaders/CRT.fsh +++ b/Shaders/CRT.fsh @@ -159,7 +159,7 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou } // Gamma correction - ret = pow(ret, 0.72); + ret = pow(ret, vec4(0.72)); return ret; } From 0c3db932b22e3ba9e033ce4391c9d8dd5b99f0d6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 18:19:15 +0300 Subject: [PATCH 207/341] Fix Mavericks compatibility --- Cocoa/Document.m | 14 ++++++-------- Cocoa/GBSplitView.h | 2 +- Cocoa/GBSplitView.m | 10 ++++++++++ Cocoa/GBViewMetal.m | 2 ++ Cocoa/NSObject+MavericksCompat.m | 7 +++++++ Makefile | 2 +- 6 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 Cocoa/NSObject+MavericksCompat.m diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 71436d3..0114658 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -817,9 +817,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } if (![console_output_timer isValid]) { - console_output_timer = [NSTimer timerWithTimeInterval:(NSTimeInterval)0.05 repeats:NO block:^(NSTimer * _Nonnull timer) { - [self appendPendingOutput]; - }]; + console_output_timer = [NSTimer timerWithTimeInterval:(NSTimeInterval)0.05 target:self selector:@selector(appendPendingOutput) userInfo:nil repeats:NO]; [[NSRunLoop mainRunLoop] addTimer:console_output_timer forMode:NSDefaultRunLoopMode]; } @@ -1665,7 +1663,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } -- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview; +- (BOOL)splitView:(GBSplitView *)splitView canCollapseSubview:(NSView *)subview; { if ([[splitView arrangedSubviews] lastObject] == subview) { return YES; @@ -1673,16 +1671,16 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) return NO; } -- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex +- (CGFloat)splitView:(GBSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex { return 600; } -- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex { +- (CGFloat)splitView:(GBSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex { return splitView.frame.size.width - 321; } -- (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view { +- (BOOL)splitView:(GBSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view { if ([[splitView arrangedSubviews] lastObject] == view) { return NO; } @@ -1691,7 +1689,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) - (void)splitViewDidResizeSubviews:(NSNotification *)notification { - NSSplitView *splitview = notification.object; + GBSplitView *splitview = notification.object; if ([[[splitview arrangedSubviews] firstObject] frame].size.width < 600) { [splitview setPosition:600 ofDividerAtIndex:0]; } diff --git a/Cocoa/GBSplitView.h b/Cocoa/GBSplitView.h index 7b2faa2..6ab97cf 100644 --- a/Cocoa/GBSplitView.h +++ b/Cocoa/GBSplitView.h @@ -3,5 +3,5 @@ @interface GBSplitView : NSSplitView -(void) setDividerColor:(NSColor *)color; - +- (NSArray *)arrangedSubviews; @end diff --git a/Cocoa/GBSplitView.m b/Cocoa/GBSplitView.m index 0a30fe0..0fb3bc4 100644 --- a/Cocoa/GBSplitView.m +++ b/Cocoa/GBSplitView.m @@ -17,4 +17,14 @@ return [super dividerColor]; } +/* Mavericks comaptibility */ +- (NSArray *)arrangedSubviews +{ + if (@available(macOS 10.11, *)) { + return [super arrangedSubviews]; + } else { + return [self subviews]; + } +} + @end diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 4c8a5d5..62deadc 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -1,4 +1,6 @@ #import "GBViewMetal.h" +#pragma clang diagnostic ignored "-Wpartial-availability" + static const vector_float2 rect[] = { diff --git a/Cocoa/NSObject+MavericksCompat.m b/Cocoa/NSObject+MavericksCompat.m new file mode 100644 index 0000000..6c06514 --- /dev/null +++ b/Cocoa/NSObject+MavericksCompat.m @@ -0,0 +1,7 @@ +#import +@implementation NSObject (MavericksCompat) +- (instancetype)initWithCoder:(NSCoder *)coder +{ + return [self init]; +} +@end diff --git a/Makefile b/Makefile index 5923b2e..36a893d 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,7 @@ OPEN_DIALOG = OpenDialog/cocoa.m endif -CFLAGS += -Werror -Wall -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES +CFLAGS += -Werror -Wall -Wpartial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) SDL_CFLAGS := $(shell sdl2-config --cflags) SDL_LDFLAGS := $(shell sdl2-config --libs) From 5df45417fad1c9467d75dff40b3de7b4369af490 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 18:27:31 +0300 Subject: [PATCH 208/341] Console quirks --- Cocoa/Document.m | 3 ++- Cocoa/Document.xib | 36 ++++++++++++++++++------------------ Core/debugger.c | 5 +++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 0114658..3c83fab 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -470,7 +470,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) - (void)windowControllerDidLoadNib:(NSWindowController *)aController { [super windowControllerDidLoadNib:aController]; - + // Interface Builder bug? + [self.consoleWindow setContentSize:self.consoleWindow.minSize]; /* Close Open Panels, if any */ for (NSWindow *window in [[NSApplication sharedApplication] windows]) { if ([window isKindOfClass:[NSOpenPanel class]]) { diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index 338650b..f096bb3 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -107,22 +107,22 @@ - + - + - + - + - + @@ -137,29 +137,29 @@ - + - + - + - + - + - + @@ -173,27 +173,27 @@ - + - + - + - + - + - + @@ -208,7 +208,7 @@ - + diff --git a/Core/debugger.c b/Core/debugger.c index 4e3bd63..94fae80 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1766,10 +1766,11 @@ static const debugger_command_t commands[] = { {"next", 1, next, "Run the next instruction, skipping over function calls"}, {"step", 1, step, "Run the next instruction, stepping into function calls"}, {"finish", 1, finish, "Run until the current function returns"}, - {"backtrace", 2, backtrace, "Display the current call stack"}, + {"backtrace", 2, backtrace, "Displays the current call stack"}, {"bt", 2, }, /* Alias */ {"sld", 3, stack_leak_detection, "Like finish, but stops if a stack leak is detected (Experimental)"}, - {"ticks", 2, ticks, "Display the number of CPU ticks since the last time 'ticks' was used"}, + {"ticks", 2, ticks, "Displays the number of CPU ticks since the last time 'ticks' was" HELP_NEWLINE + "used"}, {"registers", 1, registers, "Print values of processor registers and other important registers"}, {"cartridge", 2, mbc, "Displays information about the MBC and cartridge"}, {"mbc", 3, }, /* Alias */ From 0abd3b2c469dc65eb09120c4a6811af1da83b678 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 19:15:40 +0300 Subject: [PATCH 209/341] Dump and load cheats --- Cocoa/Document.h | 3 ++ Cocoa/Document.m | 4 +++ Cocoa/Document.xib | 4 ++- Core/cheats.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ Core/cheats.h | 2 ++ 5 files changed, 88 insertions(+), 1 deletion(-) diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 1117c26..9353788 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -3,6 +3,8 @@ #include "GBImageView.h" #include "GBSplitView.h" +@class GBCheatWindowController; + @interface Document : NSDocument @property (strong) IBOutlet GBView *view; @property (strong) IBOutlet NSTextView *consoleOutput; @@ -34,6 +36,7 @@ @property (strong) IBOutlet GBSplitView *debuggerSplitView; @property (strong) IBOutlet NSBox *debuggerVerticalLine; @property (strong) IBOutlet NSPanel *cheatsWindow; +@property (strong) IBOutlet GBCheatWindowController *cheatWindowController; -(uint8_t) readMemory:(uint16_t) addr; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 3c83fab..823204c 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -7,6 +7,7 @@ #include "HexFiend/HexFiend.h" #include "GBMemoryByteArray.h" #include "GBWarningPopover.h" +#include "GBCheatWindowController.h" /* Todo: The general Objective-C coding style conflicts with SameBoy's. This file needs a cleanup. */ /* Todo: Split into category files! This is so messy!!! */ @@ -359,6 +360,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.audioClient = nil; self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); stopping = false; } @@ -643,6 +645,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSString *rom_warnings = [self captureOutputForBlock:^{ GB_load_rom(&gb, [self.fileName UTF8String]); GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + GB_load_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); + [self.cheatWindowController.cheatsTable reloadData]; GB_debugger_clear_symbols(&gb); GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index f096bb3..f1f2f5a 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -9,6 +9,8 @@ + + @@ -1056,7 +1058,7 @@ - + diff --git a/Core/cheats.c b/Core/cheats.c index fa9b6a7..0525816 100644 --- a/Core/cheats.c +++ b/Core/cheats.c @@ -2,6 +2,7 @@ #include "cheats.h" #include #include +#include static inline uint8_t hash_addr(uint16_t addr) { @@ -238,3 +239,78 @@ void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *des cheat->description[sizeof(cheat->description) - 1] = 0; } } + +#define CHEAT_MAGIC 'SBCh' + +void GB_load_cheats(GB_gameboy_t *gb, const char *path) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + return; + } + + uint32_t magic = 0; + uint32_t struct_size = 0; + fread(&magic, sizeof(magic), 1, f); + fread(&struct_size, sizeof(struct_size), 1, f); + if (magic != CHEAT_MAGIC && magic != __builtin_bswap32(CHEAT_MAGIC)) { + GB_log(gb, "The file is not a SameBoy cheat database"); + return; + } + + if (struct_size != sizeof(GB_cheat_t)) { + GB_log(gb, "This cheat database is not compatible with this version of SameBoy"); + return; + } + + // Remove all cheats first + while (gb->cheats) { + GB_remove_cheat(gb, gb->cheats[0]); + } + + GB_cheat_t cheat; + while (fread(&cheat, sizeof(cheat), 1, f)) { + if (magic == __builtin_bswap32(CHEAT_MAGIC)) { + cheat.address = __builtin_bswap16(cheat.address); + cheat.bank = __builtin_bswap16(cheat.bank); + } + cheat.description[sizeof(cheat.description) - 1] = 0; + GB_add_cheat(gb, cheat.description, cheat.address, cheat.bank, cheat.value, cheat.old_value, cheat.use_old_value, cheat.enabled); + } + + return; +} + +int GB_save_cheats(GB_gameboy_t *gb, const char *path) +{ + if (!gb->cheat_count) return 0; // Nothing to save. + FILE *f = fopen(path, "wb"); + if (!f) { + GB_log(gb, "Could not dump cheat database: %s.\n", strerror(errno)); + return errno; + } + + uint32_t magic = CHEAT_MAGIC; + uint32_t struct_size = sizeof(GB_cheat_t); + + if (fwrite(&magic, sizeof(magic), 1, f) != 1) { + fclose(f); + return EIO; + } + + if (fwrite(&struct_size, sizeof(struct_size), 1, f) != 1) { + fclose(f); + return EIO; + } + + for (size_t i = 0; i cheat_count; i++) { + if (fwrite(gb->cheats[i], sizeof(*gb->cheats[i]), 1, f) != 1) { + fclose(f); + return EIO; + } + } + + errno = 0; + fclose(f); + return errno; +} diff --git a/Core/cheats.h b/Core/cheats.h index be5b39a..13b7d7b 100644 --- a/Core/cheats.h +++ b/Core/cheats.h @@ -13,6 +13,8 @@ const GB_cheat_t *const *GB_get_cheats(GB_gameboy_t *gb, size_t *size); void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat); bool GB_cheats_enabled(GB_gameboy_t *gb); void GB_set_cheats_enabled(GB_gameboy_t *gb, bool enabled); +void GB_load_cheats(GB_gameboy_t *gb, const char *path); +int GB_save_cheats(GB_gameboy_t *gb, const char *path); #ifdef GB_INTERNAL void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value); From 695c6ee943d661a40db8fbd3914605e7d2b29a46 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 19:21:00 +0300 Subject: [PATCH 210/341] Don't crash if a naughty frontend runs the boot ROM without a ROM --- Core/display.c | 2 +- Core/gb.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 356e742..67a9fc4 100644 --- a/Core/display.c +++ b/Core/display.c @@ -165,7 +165,7 @@ static void display_vblank(GB_gameboy_t *gb) 0x30DA, 0x69AD, 0x2B57, 0x2B5D, 0x632C, 0x1050, 0x3C84, 0x0E07, 0x0E18, 0x2964, }; - unsigned index = gb->rom[0x14e] % 5; + unsigned index = gb->rom? gb->rom[0x14e] % 5 : 0; gb->borrowed_border.palette[0] = colors[index]; gb->borrowed_border.palette[10] = colors[5 + index]; gb->borrowed_border.palette[14] = colors[10 + index]; diff --git a/Core/gb.c b/Core/gb.c index 75538e1..3b834dd 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -230,7 +230,7 @@ void GB_borrow_sgb_border(GB_gameboy_t *gb) if (gb->border_mode != GB_BORDER_ALWAYS) return; if (gb->tried_loading_sgb_border) return; gb->tried_loading_sgb_border = true; - if (gb->rom[0x146] != 3) return; // Not an SGB game, nothing to borrow + if (gb->rom && gb->rom[0x146] != 3) return; // Not an SGB game, nothing to borrow if (!gb->boot_rom_load_callback) return; // Can't borrow a border without this callback GB_gameboy_t sgb; GB_init(&sgb, GB_MODEL_SGB); From 32a0dc0e435a54bc70d2403d85a2996ced757a52 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 20:44:15 +0300 Subject: [PATCH 211/341] Rename the "Developer" menu to "Develop", like first party Mac apps --- Cocoa/MainMenu.xib | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index e56b4a0..71add1c 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -382,9 +382,9 @@ - + - + From db9410caa5f4a518984ba298fcb6688d5e6cef68 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 11 Apr 2020 21:48:47 +0300 Subject: [PATCH 212/341] Minor UI fix --- Cocoa/Document.m | 2 +- Cocoa/GBCheatWindowController.h | 2 +- Cocoa/GBCheatWindowController.m | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 823204c..10ba510 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -646,7 +646,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_load_rom(&gb, [self.fileName UTF8String]); GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_load_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); - [self.cheatWindowController.cheatsTable reloadData]; + [self.cheatWindowController cheatsUpdated]; GB_debugger_clear_symbols(&gb); GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); diff --git a/Cocoa/GBCheatWindowController.h b/Cocoa/GBCheatWindowController.h index adb0bf8..f70553e 100644 --- a/Cocoa/GBCheatWindowController.h +++ b/Cocoa/GBCheatWindowController.h @@ -12,6 +12,6 @@ @property (weak) IBOutlet NSTextField *importCodeField; @property (weak) IBOutlet NSTextField *importDescriptionField; @property (weak) IBOutlet Document *document; - +- (void)cheatsUpdated; @end diff --git a/Cocoa/GBCheatWindowController.m b/Cocoa/GBCheatWindowController.m index 994d5e1..c10e2a9 100644 --- a/Cocoa/GBCheatWindowController.m +++ b/Cocoa/GBCheatWindowController.m @@ -231,4 +231,10 @@ [self.cheatsTable reloadData]; } +- (void)cheatsUpdated +{ + [self.cheatsTable reloadData]; + [self tableViewSelectionDidChange:nil]; +} + @end From d38fd41b0eaa2a598fd88f6d5d37fcb52f257598 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 20:18:56 +0300 Subject: [PATCH 213/341] Reorder flags so -Wpartial-availablility is affected by -Wno-unknown-warning -Wno-unknown-warning-option, fixes #249, fixes #251 --- Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 36a893d..ba568ff 100644 --- a/Makefile +++ b/Makefile @@ -87,8 +87,13 @@ ifeq ($(PLATFORM),Darwin) OPEN_DIALOG = OpenDialog/cocoa.m endif +# These myst come before the -Wno- flags +CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option + +CFLAGS += -Wpartial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context + +CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES -CFLAGS += -Werror -Wall -Wpartial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-unknown-warning -Wno-unknown-warning-option -Wno-multichar -Wno-int-in-bool-context -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) SDL_CFLAGS := $(shell sdl2-config --cflags) SDL_LDFLAGS := $(shell sdl2-config --libs) From 0cf168f32beb6c98dcfb8d6cd17659f1bbf53c45 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 20:37:57 +0300 Subject: [PATCH 214/341] Fixing inconsistent style --- BootROMs/pb8.c | 33 ++++-- Cocoa/Document.m | 27 +++-- Cocoa/GBAudioClient.m | 6 +- Cocoa/GBGLShader.m | 2 +- Cocoa/GBImageView.m | 2 +- Cocoa/GBOpenGLView.m | 3 +- Cocoa/GBSplitView.m | 9 +- Cocoa/GBTerminalTextFieldCell.m | 3 +- Cocoa/GBView.m | 6 +- Cocoa/GBViewMetal.m | 3 +- Cocoa/joypad.m | 12 ++- Cocoa/main.m | 3 +- Core/apu.c | 4 +- Core/debugger.c | 22 ++-- Core/display.c | 2 +- Core/gb.c | 6 +- Core/gb.h | 6 +- Core/memory.c | 2 +- Core/save_state.c | 2 +- Core/sgb.c | 4 +- Core/sm83_cpu.c | 10 +- Core/sm83_disassembler.c | 3 +- Core/timing.c | 15 +-- OpenDialog/gtk.c | 3 +- OpenDialog/windows.c | 3 +- QuickLook/generator.m | 2 +- QuickLook/main.c | 30 +++--- SDL/gui.c | 8 +- SDL/main.c | 5 +- SDL/opengl_compat.h | 2 +- Tester/main.c | 4 +- Windows/stdio.h | 3 +- libretro/libretro.c | 175 ++++++++++++-------------------- 33 files changed, 197 insertions(+), 223 deletions(-) diff --git a/BootROMs/pb8.c b/BootROMs/pb8.c index 03a196e..4ee4524 100644 --- a/BootROMs/pb8.c +++ b/BootROMs/pb8.c @@ -97,7 +97,8 @@ LoadTileset: * @param blocklength size of an independent input block in bytes * @return 0 for reaching infp end of file, or EOF for error */ -int pb8(FILE *infp, FILE *outfp, size_t blocklength) { +int pb8(FILE *infp, FILE *outfp, size_t blocklength) +{ blocklength >>= 3; // convert bytes to blocks assert(blocklength > 0); while (1) { @@ -113,7 +114,8 @@ int pb8(FILE *infp, FILE *outfp, size_t blocklength) { control_byte <<= 1; if (c == last_byte) { control_byte |= 0x01; - } else { + } + else { literals[nliterals++] = last_byte = c; } } @@ -143,7 +145,8 @@ int pb8(FILE *infp, FILE *outfp, size_t blocklength) { * @param outfp output stream * @return 0 for reaching infp end of file, or EOF for error */ -int unpb8(FILE *infp, FILE *outfp) { +int unpb8(FILE *infp, FILE *outfp) +{ int last_byte = 0; while (1) { int control_byte = fgetc(infp); @@ -165,7 +168,8 @@ int unpb8(FILE *infp, FILE *outfp) { /* CLI frontend ****************************************************/ -static inline void set_fd_binary(unsigned int fd) { +static inline void set_fd_binary(unsigned int fd) +{ #ifdef _WIN32 _setmode(fd, _O_BINARY); #else @@ -197,7 +201,8 @@ static const char *version_msg = static const char *toomanyfilenames_msg = "pb8: too many filenames; try pb8 --help\n"; -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ const char *infilename = NULL; const char *outfilename = NULL; bool decompress = false; @@ -248,11 +253,14 @@ int main(int argc, char **argv) { fprintf(stderr, "pb8: unknown option -%c\n", argtype); return EXIT_FAILURE; } - } else if (!infilename) { + } + else if (!infilename) { infilename = argv[i]; - } else if (!outfilename) { + } + else if (!outfilename) { outfilename = argv[i]; - } else { + } + else { fputs(toomanyfilenames_msg, stderr); return EXIT_FAILURE; } @@ -282,7 +290,8 @@ int main(int argc, char **argv) { perror("for reading"); return EXIT_FAILURE; } - } else { + } + else { infp = stdin; set_fd_binary(0); } @@ -296,7 +305,8 @@ int main(int argc, char **argv) { fclose(infp); return EXIT_FAILURE; } - } else { + } + else { outfp = stdout; set_fd_binary(1); } @@ -305,7 +315,8 @@ int main(int argc, char **argv) { int has_ferror = 0; if (decompress) { compfailed = unpb8(infp, outfp); - } else { + } + else { compfailed = pb8(infp, outfp, blocklength); } fflush(outfp); diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 10ba510..ed3eaaf 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -150,7 +150,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSMutableArray *debugger_input_queue; } -- (instancetype)init { +- (instancetype)init +{ self = [super init]; if (self) { has_debugger_input = [[NSConditionLock alloc] initWithCondition:0]; @@ -470,7 +471,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) } } -- (void)windowControllerDidLoadNib:(NSWindowController *)aController { +- (void)windowControllerDidLoadNib:(NSWindowController *)aController +{ [super windowControllerDidLoadNib:aController]; // Interface Builder bug? [self.consoleWindow setContentSize:self.consoleWindow.minSize]; @@ -625,11 +627,13 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) self.memoryBankItem.enabled = false; } -+ (BOOL)autosavesInPlace { ++ (BOOL)autosavesInPlace +{ return YES; } -- (NSString *)windowNibName { +- (NSString *)windowNibName +{ // Override returning the nib file name of the document // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. return @"Document"; @@ -690,7 +694,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) - (BOOL)validateUserInterfaceItem:(id)anItem { - if([anItem action] == @selector(mute:)) { + if ([anItem action] == @selector(mute:)) { [(NSMenuItem*)anItem setState:!self.audioClient.isPlaying]; } else if ([anItem action] == @selector(togglePause:)) { @@ -837,7 +841,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self.consoleWindow orderBack:nil]; } -- (IBAction)consoleInput:(NSTextField *)sender { +- (IBAction)consoleInput:(NSTextField *)sender +{ NSString *line = [sender stringValue]; if ([line isEqualToString:@""] && lastConsoleInput) { line = lastConsoleInput; @@ -1475,7 +1480,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn]; if (tableView == self.paletteTableView) { if (columnIndex == 0) { - return [NSString stringWithFormat:@"%s %u", row >=8 ? "Object" : "Background", (unsigned)(row & 7)]; + return [NSString stringWithFormat:@"%s %u", row >= 8 ? "Object" : "Background", (unsigned)(row & 7)]; } uint8_t *palette_data = GB_get_direct_access(&gb, row >= 8? GB_DIRECT_ACCESS_OBP : GB_DIRECT_ACCESS_BGP, NULL, NULL); @@ -1572,7 +1577,7 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) [self stop]; NSSavePanel * savePanel = [NSSavePanel savePanel]; [savePanel setAllowedFileTypes:@[@"png"]]; - [savePanel beginSheetModalForWindow:self.printerFeedWindow completionHandler:^(NSInteger result){ + [savePanel beginSheetModalForWindow:self.printerFeedWindow completionHandler:^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { [savePanel orderOut:self]; CGImageRef cgRef = [self.feedImageView.image CGImageForProposedRect:NULL @@ -1681,11 +1686,13 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) return 600; } -- (CGFloat)splitView:(GBSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex { +- (CGFloat)splitView:(GBSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex +{ return splitView.frame.size.width - 321; } -- (BOOL)splitView:(GBSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view { +- (BOOL)splitView:(GBSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)view +{ if ([[splitView arrangedSubviews] lastObject] == view) { return NO; } diff --git a/Cocoa/GBAudioClient.m b/Cocoa/GBAudioClient.m index 81ddec4..7f2115d 100644 --- a/Cocoa/GBAudioClient.m +++ b/Cocoa/GBAudioClient.m @@ -26,8 +26,7 @@ static OSStatus render( -(id) initWithRendererBlock:(void (^)(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer)) block andSampleRate:(UInt32) rate { - if(!(self = [super init])) - { + if (!(self = [super init])) { return nil; } @@ -102,7 +101,8 @@ static OSStatus render( _playing = NO; } --(void) dealloc { +-(void) dealloc +{ [self stop]; AudioUnitUninitialize(audioUnit); AudioComponentInstanceDispose(audioUnit); diff --git a/Cocoa/GBGLShader.m b/Cocoa/GBGLShader.m index d57f43d..920226b 100644 --- a/Cocoa/GBGLShader.m +++ b/Cocoa/GBGLShader.m @@ -169,7 +169,7 @@ void main(void) {\n\ + (GLuint)shaderWithContents:(NSString*)contents type:(GLenum)type { - const GLchar* source = [contents UTF8String]; + const GLchar *source = [contents UTF8String]; // Create the shader object GLuint shader = glCreateShader(type); // Load the shader source diff --git a/Cocoa/GBImageView.m b/Cocoa/GBImageView.m index 47efa00..3525e72 100644 --- a/Cocoa/GBImageView.m +++ b/Cocoa/GBImageView.m @@ -93,7 +93,7 @@ - (void)updateTrackingAreas { - if(trackingArea != nil) { + if (trackingArea != nil) { [self removeTrackingArea:trackingArea]; } diff --git a/Cocoa/GBOpenGLView.m b/Cocoa/GBOpenGLView.m index 8831b62..90ebf8d 100644 --- a/Cocoa/GBOpenGLView.m +++ b/Cocoa/GBOpenGLView.m @@ -4,7 +4,8 @@ @implementation GBOpenGLView -- (void)drawRect:(NSRect)dirtyRect { +- (void)drawRect:(NSRect)dirtyRect +{ if (!self.shader) { self.shader = [[GBGLShader alloc] initWithName:[[NSUserDefaults standardUserDefaults] objectForKey:@"GBFilter"]]; } diff --git a/Cocoa/GBSplitView.m b/Cocoa/GBSplitView.m index 0fb3bc4..a56c24e 100644 --- a/Cocoa/GBSplitView.m +++ b/Cocoa/GBSplitView.m @@ -5,12 +5,14 @@ NSColor *_dividerColor; } -- (void)setDividerColor:(NSColor *)color { +- (void)setDividerColor:(NSColor *)color +{ _dividerColor = color; [self setNeedsDisplay:YES]; } -- (NSColor *)dividerColor { +- (NSColor *)dividerColor +{ if (_dividerColor) { return _dividerColor; } @@ -22,7 +24,8 @@ { if (@available(macOS 10.11, *)) { return [super arrangedSubviews]; - } else { + } + else { return [self subviews]; } } diff --git a/Cocoa/GBTerminalTextFieldCell.m b/Cocoa/GBTerminalTextFieldCell.m index 47a3a35..e95e785 100644 --- a/Cocoa/GBTerminalTextFieldCell.m +++ b/Cocoa/GBTerminalTextFieldCell.m @@ -173,7 +173,8 @@ [super setSelectedRanges:ranges affinity:affinity stillSelecting:stillSelectingFlag]; } -- (BOOL)resignFirstResponder { +- (BOOL)resignFirstResponder +{ reverse_search_mode = false; return [super resignFirstResponder]; } diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 0e52811..6f26a03 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -114,8 +114,7 @@ } - (instancetype)initWithCoder:(NSCoder *)coder { - if (!(self = [super initWithCoder:coder])) - { + if (!(self = [super initWithCoder:coder])) { return self; } [self _init]; @@ -124,8 +123,7 @@ - (instancetype)initWithFrame:(NSRect)frameRect { - if (!(self = [super initWithFrame:frameRect])) - { + if (!(self = [super initWithFrame:frameRect])) { return self; } [self _init]; diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 62deadc..9a1c78b 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -159,8 +159,7 @@ static const vector_float2 rect[] = MTLRenderPassDescriptor *render_pass_descriptor = view.currentRenderPassDescriptor; id command_buffer = [command_queue commandBuffer]; - if (render_pass_descriptor != nil) - { + if (render_pass_descriptor != nil) { *(GB_frame_blending_mode_t *)[frame_blending_mode_buffer contents] = [self frameBlendingMode]; *(vector_float2 *)[output_resolution_buffer contents] = output_resolution; diff --git a/Cocoa/joypad.m b/Cocoa/joypad.m index 2ffe56b..fd9fe70 100755 --- a/Cocoa/joypad.m +++ b/Cocoa/joypad.m @@ -295,7 +295,8 @@ AddHIDElements(CFArrayRef array, recDevice *pDevice) } static bool -ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) { +ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) +{ while (listitem) { if (listitem->cookie == cookie) { return true; @@ -431,7 +432,8 @@ AddHIDElement(const void *value, void *parameter) } if (elementPrevious) { elementPrevious->pNext = element; - } else { + } + else { *headElement = element; } @@ -519,7 +521,8 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice) *guid16++ = 0; *guid16++ = version; *guid16++ = 0; - } else { + } + else { *guid16++ = BUS_BLUETOOTH; *guid16++ = 0; strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4); @@ -582,7 +585,8 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) value = GetHIDElementState(device, element) - element->min; if (range == 4) { /* 4 position hatswitch - scale up value */ value *= 2; - } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */ + } + else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */ value = -1; } if ((unsigned)value >= 8) { diff --git a/Cocoa/main.m b/Cocoa/main.m index 8a6799b..662469a 100644 --- a/Cocoa/main.m +++ b/Cocoa/main.m @@ -1,5 +1,6 @@ #import -int main(int argc, const char * argv[]) { +int main(int argc, const char * argv[]) +{ return NSApplicationMain(argc, argv); } diff --git a/Core/apu.c b/Core/apu.c index feda3c8..e63d89f 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -139,7 +139,7 @@ static double smooth(double x) static void render(GB_gameboy_t *gb) { - GB_sample_t output = {0,0}; + GB_sample_t output = {0, 0}; UNROLL for (unsigned i = 0; i < GB_N_CHANNELS; i++) { @@ -907,7 +907,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.is_active[GB_NOISE] = false; update_sample(gb, GB_NOISE, 0, 0); } - else if (gb->apu.is_active[GB_NOISE]){ + else if (gb->apu.is_active[GB_NOISE]) { nrx2_glitch(&gb->apu.noise_channel.current_volume, value, gb->io_registers[reg]); update_sample(gb, GB_NOISE, gb->apu.current_lfsr_sample ? diff --git a/Core/debugger.c b/Core/debugger.c index 94fae80..ee27e88 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -298,13 +298,15 @@ static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value) static value_t add(value_t a, value_t b) {return FIX_BANK(a.value + b.value);} static value_t sub(value_t a, value_t b) {return FIX_BANK(a.value - b.value);} static value_t mul(value_t a, value_t b) {return FIX_BANK(a.value * b.value);} -static value_t _div(value_t a, value_t b) { +static value_t _div(value_t a, value_t b) +{ if (b.value == 0) { return FIX_BANK(0); } return FIX_BANK(a.value / b.value); }; -static value_t mod(value_t a, value_t b) { +static value_t mod(value_t a, value_t b) +{ if (b.value == 0) { return FIX_BANK(0); } @@ -380,8 +382,7 @@ static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string, while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) { length--; } - if (length == 0) - { + if (length == 0) { GB_log(gb, "Expected expression.\n"); *error = true; return (lvalue_t){0,}; @@ -487,8 +488,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) { length--; } - if (length == 0) - { + if (length == 0) { GB_log(gb, "Expected expression.\n"); *error = true; goto exit; @@ -1167,7 +1167,7 @@ static bool unwatch(GB_gameboy_t *gb, char *arguments, char *modifiers, const de memmove(&gb->watchpoints[index], &gb->watchpoints[index + 1], (gb->n_watchpoints - index - 1) * sizeof(gb->watchpoints[0])); gb->n_watchpoints--; - gb->watchpoints = realloc(gb->watchpoints, gb->n_watchpoints* sizeof(gb->watchpoints[0])); + gb->watchpoints = realloc(gb->watchpoints, gb->n_watchpoints *sizeof(gb->watchpoints[0])); GB_log(gb, "Watchpoint removed from %s\n", debugger_value_to_string(gb, result, true)); return true; @@ -1216,7 +1216,7 @@ static bool list(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug gb->watchpoints[i].condition); } else { - GB_log(gb, " %d. %s (%c%c)\n", i + 1, debugger_value_to_string(gb,addr, addr.has_bank), + GB_log(gb, " %d. %s (%c%c)\n", i + 1, debugger_value_to_string(gb, addr, addr.has_bank), (gb->watchpoints[i].flags & GB_WATCHPOINT_R)? 'r' : '-', (gb->watchpoints[i].flags & GB_WATCHPOINT_W)? 'w' : '-'); } @@ -1581,7 +1581,7 @@ static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg } GB_log(gb, "LY: %d\n", gb->io_registers[GB_IO_LY]); GB_log(gb, "LYC: %d\n", gb->io_registers[GB_IO_LYC]); - GB_log(gb, "Window position: %d, %d\n", (signed) gb->io_registers[GB_IO_WX] - 7 , gb->io_registers[GB_IO_WY]); + GB_log(gb, "Window position: %d, %d\n", (signed) gb->io_registers[GB_IO_WX] - 7, gb->io_registers[GB_IO_WY]); GB_log(gb, "Interrupt line: %s\n", gb->stat_interrupt_line? "On" : "Off"); return true; @@ -1730,7 +1730,7 @@ static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug uint8_t shift_amount = 1, mask; if (modifiers) { - switch(modifiers[0]) { + switch (modifiers[0]) { case 'c': shift_amount = 2; break; @@ -1826,7 +1826,7 @@ static const debugger_command_t *find_command(const char *string) static void print_command_shortcut(GB_gameboy_t *gb, const debugger_command_t *command) { GB_attributed_log(gb, GB_LOG_BOLD | GB_LOG_UNDERLINE, "%.*s", command->min_length, command->command); - GB_attributed_log(gb, GB_LOG_BOLD , "%s", command->command + command->min_length); + GB_attributed_log(gb, GB_LOG_BOLD, "%s", command->command + command->min_length); } static void print_command_description(GB_gameboy_t *gb, const debugger_command_t *command) diff --git a/Core/display.c b/Core/display.c index 67a9fc4..b5c0a0a 100644 --- a/Core/display.c +++ b/Core/display.c @@ -530,7 +530,7 @@ static void render_pixel_if_possible(GB_gameboy_t *gb) *dest = gb->sprite_palettes_rgb[oam_fifo_item->palette * 4 + pixel]; } } - + if (gb->model & GB_MODEL_NO_SFC_BIT) { if (gb->icd_pixel_callback) { gb->icd_pixel_callback(gb, icd_pixel); diff --git a/Core/gb.c b/Core/gb.c index 3b834dd..f6174b9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -119,7 +119,7 @@ static void load_default_border(GB_gameboy_t *gb) }\ }\ }\ - } while(false); + } while (false); if (gb->model == GB_MODEL_AGB) { #include "graphics/agb_border.inc" @@ -658,8 +658,8 @@ void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) #endif } -const GB_palette_t GB_PALETTE_GREY = {{{0x00, 0x00, 0x00}, {0x55, 0x55, 0x55}, {0xaa, 0xaa, 0xaa}, {0xff ,0xff, 0xff}, {0xff ,0xff, 0xff}}}; -const GB_palette_t GB_PALETTE_DMG = {{{0x08, 0x18, 0x10}, {0x39, 0x61, 0x39}, {0x84, 0xa5, 0x63}, {0xc6, 0xde, 0x8c}, {0xd2 ,0xe6 ,0xa6}}}; +const GB_palette_t GB_PALETTE_GREY = {{{0x00, 0x00, 0x00}, {0x55, 0x55, 0x55}, {0xaa, 0xaa, 0xaa}, {0xff, 0xff, 0xff}, {0xff, 0xff, 0xff}}}; +const GB_palette_t GB_PALETTE_DMG = {{{0x08, 0x18, 0x10}, {0x39, 0x61, 0x39}, {0x84, 0xa5, 0x63}, {0xc6, 0xde, 0x8c}, {0xd2, 0xe6, 0xa6}}}; const GB_palette_t GB_PALETTE_MGB = {{{0x07, 0x10, 0x0e}, {0x3a, 0x4c, 0x3a}, {0x81, 0x8d, 0x66}, {0xc2, 0xce, 0x93}, {0xcf, 0xda, 0xac}}}; const GB_palette_t GB_PALETTE_GBL = {{{0x0a, 0x1c, 0x15}, {0x35, 0x78, 0x62}, {0x56, 0xb4, 0x95}, {0x7f, 0xe2, 0xc3}, {0x91, 0xea, 0xd0}}}; diff --git a/Core/gb.h b/Core/gb.h index 03abfa1..01b79e8 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -53,7 +53,7 @@ typedef struct { struct { - uint8_t r,g,b; + uint8_t r, g, b; } colors[5]; } GB_palette_t; @@ -254,11 +254,11 @@ typedef enum { #define INTERNAL_DIV_CYCLES (0x40000) #if !defined(MIN) -#define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; }) +#define MIN(A, B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; }) #endif #if !defined(MAX) -#define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; }) +#define MAX(A, B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; }) #endif #endif diff --git a/Core/memory.c b/Core/memory.c index 58b96bb..ca9c179 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -277,7 +277,7 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_MODEL_SGB_PAL_NO_SFC: case GB_MODEL_SGB2: case GB_MODEL_SGB2_NO_SFC: - ; + break; } } diff --git a/Core/save_state.c b/Core/save_state.c index a53ccba..11024df 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -361,7 +361,7 @@ int GB_load_state_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t le return -1; } - if (buffer_read(gb->vram,gb->vram_size, &buffer, &length) != gb->vram_size) { + if (buffer_read(gb->vram, gb->vram_size, &buffer, &length) != gb->vram_size) { return -1; } diff --git a/Core/sgb.c b/Core/sgb.c index 271b681..c77b0db 100644 --- a/Core/sgb.c +++ b/Core/sgb.c @@ -151,7 +151,7 @@ static void command_ready(GB_gameboy_t *gb) 0xE content bytes. The last command, FB, is padded with zeros, so information past the header is not sent. */ if ((gb->sgb->command[0] & 0xF1) == 0xF1) { - if(gb->boot_rom_finished) return; + if (gb->boot_rom_finished) return; uint8_t checksum = 0; for (unsigned i = 2; i < 0x10; i++) { @@ -247,7 +247,7 @@ static void command_ready(GB_gameboy_t *gb) gb->sgb->attribute_map[x + 20 * y] = inside_palette; } } - else if(middle) { + else if (middle) { gb->sgb->attribute_map[x + 20 * y] = middle_palette; } } diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 2b7b1dd..13f05df 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -452,7 +452,7 @@ static void ld_da16_sp(GB_gameboy_t *gb, uint8_t opcode) addr = cycle_read_inc_oam_bug(gb, gb->pc++); addr |= cycle_read_inc_oam_bug(gb, gb->pc++) << 8; cycle_write(gb, addr, gb->registers[GB_REGISTER_SP] & 0xFF); - cycle_write(gb, addr+1, gb->registers[GB_REGISTER_SP] >> 8); + cycle_write(gb, addr + 1, gb->registers[GB_REGISTER_SP] >> 8); } static void add_hl_rr(GB_gameboy_t *gb, uint8_t opcode) @@ -1222,7 +1222,7 @@ static void ld_a_da16(GB_gameboy_t *gb, uint8_t opcode) uint16_t addr; gb->registers[GB_REGISTER_AF] &= 0xFF; addr = cycle_read_inc_oam_bug(gb, gb->pc++); - addr |= cycle_read_inc_oam_bug(gb, gb->pc++) << 8 ; + addr |= cycle_read_inc_oam_bug(gb, gb->pc++) << 8; gb->registers[GB_REGISTER_AF] |= cycle_read(gb, addr) << 8; } @@ -1410,10 +1410,10 @@ static void bit_r(GB_gameboy_t *gb, uint8_t opcode) } } else if ((opcode & 0xC0) == 0x80) { /* res */ - set_src_value(gb, opcode, value & ~bit) ; + set_src_value(gb, opcode, value & ~bit); } else if ((opcode & 0xC0) == 0xC0) { /* set */ - set_src_value(gb, opcode, value | bit) ; + set_src_value(gb, opcode, value | bit); } } @@ -1567,7 +1567,7 @@ void GB_cpu_run(GB_gameboy_t *gb) GB_debugger_call_hook(gb, call_addr); } /* Run mode */ - else if(!gb->halted) { + else if (!gb->halted) { gb->last_opcode_read = cycle_read_inc_oam_bug(gb, gb->pc++); if (gb->halt_bug) { gb->pc--; diff --git a/Core/sm83_disassembler.c b/Core/sm83_disassembler.c index 96aec00..7dacd9e 100644 --- a/Core/sm83_disassembler.c +++ b/Core/sm83_disassembler.c @@ -97,7 +97,8 @@ static void rla(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc) GB_log(gb, "RLA\n"); } -static void ld_da16_sp(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc){ +static void ld_da16_sp(GB_gameboy_t *gb, uint8_t opcode, uint16_t *pc) +{ uint16_t addr; (*pc)++; addr = GB_read_memory(gb, (*pc)++); diff --git a/Core/timing.c b/Core/timing.c index 1f3f654..9eb9c91 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -274,19 +274,14 @@ void GB_rtc_run(GB_gameboy_t *gb) time_t current_time = time(NULL); while (gb->last_rtc_second < current_time) { gb->last_rtc_second++; - if (++gb->rtc_real.seconds == 60) - { + if (++gb->rtc_real.seconds == 60) { gb->rtc_real.seconds = 0; - if (++gb->rtc_real.minutes == 60) - { + if (++gb->rtc_real.minutes == 60) { gb->rtc_real.minutes = 0; - if (++gb->rtc_real.hours == 24) - { + if (++gb->rtc_real.hours == 24) { gb->rtc_real.hours = 0; - if (++gb->rtc_real.days == 0) - { - if (gb->rtc_real.high & 1) /* Bit 8 of days*/ - { + if (++gb->rtc_real.days == 0) { + if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ gb->rtc_real.high |= 0x80; /* Overflow bit */ } gb->rtc_real.high ^= 1; diff --git a/OpenDialog/gtk.c b/OpenDialog/gtk.c index 7947ea2..d9163fc 100644 --- a/OpenDialog/gtk.c +++ b/OpenDialog/gtk.c @@ -88,8 +88,7 @@ char *do_open_rom_dialog(void) int res = gtk_dialog_run (dialog); char *ret = NULL; - if (res == GTK_RESPONSE_ACCEPT) - { + if (res == GTK_RESPONSE_ACCEPT) { char *filename; filename = gtk_file_chooser_get_filename(dialog); ret = strdup(filename); diff --git a/OpenDialog/windows.c b/OpenDialog/windows.c index 75fe767..6bf9b89 100644 --- a/OpenDialog/windows.c +++ b/OpenDialog/windows.c @@ -17,8 +17,7 @@ char *do_open_rom_dialog(void) dialog.lpstrInitialDir = NULL; dialog.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; - if (GetOpenFileNameW(&dialog) == TRUE) - { + if (GetOpenFileNameW(&dialog) == TRUE) { char *ret = malloc(MAX_PATH * 4); WideCharToMultiByte(CP_UTF8, 0, filename, sizeof(filename), ret, MAX_PATH * 4, NULL, NULL); return ret; diff --git a/QuickLook/generator.m b/QuickLook/generator.m index 1aa0087..c3c13dc 100644 --- a/QuickLook/generator.m +++ b/QuickLook/generator.m @@ -79,7 +79,7 @@ static OSStatus render(CGContextRef cgContext, CFURLRef url, bool showBorder) } /* Mask it with the template (The middle part of the template image is transparent) */ - [effectiveTemplate drawInRect:(NSRect){{0,0},template.size}]; + [effectiveTemplate drawInRect:(NSRect){{0, 0}, template.size}]; } CGColorSpaceRelease(colorSpaceRef); diff --git a/QuickLook/main.c b/QuickLook/main.c index 8566e32..1d1676a 100644 --- a/QuickLook/main.c +++ b/QuickLook/main.c @@ -43,8 +43,8 @@ typedef struct __QuickLookGeneratorPluginType QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID); void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance); -HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv); -void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID); +HRESULT QuickLookGeneratorQueryInterface(void *thisInstance, REFIID iid, LPVOID *ppv); +void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID); ULONG QuickLookGeneratorPluginAddRef(void *thisInstance); ULONG QuickLookGeneratorPluginRelease(void *thisInstance); @@ -77,11 +77,11 @@ QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFact QuickLookGeneratorPluginType *theNewInstance; theNewInstance = (QuickLookGeneratorPluginType *)malloc(sizeof(QuickLookGeneratorPluginType)); - memset(theNewInstance,0,sizeof(QuickLookGeneratorPluginType)); + memset(theNewInstance, 0, sizeof(QuickLookGeneratorPluginType)); /* Point to the function table Malloc enough to store the stuff and copy the filler from myInterfaceFtbl over */ theNewInstance->conduitInterface = malloc(sizeof(QLGeneratorInterfaceStruct)); - memcpy(theNewInstance->conduitInterface,&myInterfaceFtbl,sizeof(QLGeneratorInterfaceStruct)); + memcpy(theNewInstance->conduitInterface,&myInterfaceFtbl, sizeof(QLGeneratorInterfaceStruct)); /* Retain and keep an open instance refcount for each factory. */ theNewInstance->factoryID = CFRetain(inFactoryID); @@ -110,7 +110,7 @@ void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInsta /* Free the instance structure */ free(thisInstance); - if (theFactoryID){ + if (theFactoryID) { CFPlugInRemoveInstanceForFactory(theFactoryID); CFRelease(theFactoryID); } @@ -121,13 +121,13 @@ void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInsta // ----------------------------------------------------------------------------- // Implementation of the IUnknown QueryInterface function. // -HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv) +HRESULT QuickLookGeneratorQueryInterface(void *thisInstance, REFIID iid, LPVOID *ppv) { CFUUIDRef interfaceID; - interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid); + interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, iid); - if (CFEqual(interfaceID,kQLGeneratorCallbacksInterfaceID)){ + if (CFEqual(interfaceID, kQLGeneratorCallbacksInterfaceID)) { /* If the Right interface was requested, bump the ref count, * set the ppv parameter equal to the instance, and * return good status. @@ -138,7 +138,8 @@ HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *p *ppv = thisInstance; CFRelease(interfaceID); return S_OK; - }else{ + } + else { /* Requested interface unknown, bail with error. */ *ppv = NULL; CFRelease(interfaceID); @@ -168,10 +169,11 @@ ULONG QuickLookGeneratorPluginAddRef(void *thisInstance) ULONG QuickLookGeneratorPluginRelease(void *thisInstance) { ((QuickLookGeneratorPluginType*)thisInstance)->refCount -= 1; - if (((QuickLookGeneratorPluginType*)thisInstance)->refCount == 0){ + if (((QuickLookGeneratorPluginType*)thisInstance)->refCount == 0) { DeallocQuickLookGeneratorPluginType((QuickLookGeneratorPluginType*)thisInstance ); return 0; - }else{ + } + else { return ((QuickLookGeneratorPluginType*) thisInstance )->refCount; } } @@ -179,7 +181,7 @@ ULONG QuickLookGeneratorPluginRelease(void *thisInstance) // ----------------------------------------------------------------------------- // QuickLookGeneratorPluginFactory // ----------------------------------------------------------------------------- -void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID) +void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID) { QuickLookGeneratorPluginType *result; CFUUIDRef uuid; @@ -187,8 +189,8 @@ void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID) /* If correct type is being requested, allocate an * instance of kQLGeneratorTypeID and return the IUnknown interface. */ - if (CFEqual(typeID,kQLGeneratorTypeID)){ - uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID)); + if (CFEqual(typeID, kQLGeneratorTypeID)) { + uuid = CFUUIDCreateFromString(kCFAllocatorDefault, CFSTR(PLUGIN_ID)); result = AllocQuickLookGeneratorPluginType(uuid); CFRelease(uuid); return result; diff --git a/SDL/gui.c b/SDL/gui.c index 201452a..5650d9b 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -338,7 +338,7 @@ static void cycle_model_backwards(unsigned index) const char *current_model_string(unsigned index) { - return (const char *[]){"Game Boy", "Game Boy Color", "Game Boy Advance" , "Super Game Boy"} + return (const char *[]){"Game Boy", "Game Boy Color", "Game Boy Advance", "Super Game Boy"} [configuration.model]; } @@ -800,7 +800,7 @@ static void cycle_joypads(unsigned index) SDL_JoystickClose(joystick); joystick = NULL; } - if ((controller = SDL_GameControllerOpen(joypad_index))){ + if ((controller = SDL_GameControllerOpen(joypad_index))) { joystick = SDL_GameControllerGetJoystick(controller); } else { @@ -822,7 +822,7 @@ static void cycle_joypads_backwards(unsigned index) SDL_JoystickClose(joystick); joystick = NULL; } - if ((controller = SDL_GameControllerOpen(joypad_index))){ + if ((controller = SDL_GameControllerOpen(joypad_index))) { joystick = SDL_GameControllerGetJoystick(controller); } else { @@ -886,7 +886,7 @@ void connect_joypad(void) } } else if (!joystick && SDL_NumJoysticks()) { - if ((controller = SDL_GameControllerOpen(0))){ + if ((controller = SDL_GameControllerOpen(0))) { joystick = SDL_GameControllerGetJoystick(controller); } else { diff --git a/SDL/main.c b/SDL/main.c index 4b6afea..4ce2e59 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -149,8 +149,7 @@ static void open_menu(void) static void handle_events(GB_gameboy_t *gb) { SDL_Event event; - while (SDL_PollEvent(&event)) - { + while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: pending_command = GB_SDL_QUIT_COMMAND; @@ -603,7 +602,7 @@ static bool get_arg_flag(const char *flag, int *argc, char **argv) int main(int argc, char **argv) { #ifdef _WIN32 - SetProcessDPIAware(); + SetProcessDPIAware(); #endif #define str(x) #x #define xstr(x) str(x) diff --git a/SDL/opengl_compat.h b/SDL/opengl_compat.h index 15b2a17..4b79b0c 100644 --- a/SDL/opengl_compat.h +++ b/SDL/opengl_compat.h @@ -10,7 +10,7 @@ #define GL_COMPAT_WRAPPER(func) \ ({ extern typeof(func) *GL_COMPAT_NAME(func); \ -if(!GL_COMPAT_NAME(func)) GL_COMPAT_NAME(func) = SDL_GL_GetProcAddress(#func); \ +if (!GL_COMPAT_NAME(func)) GL_COMPAT_NAME(func) = SDL_GL_GetProcAddress(#func); \ GL_COMPAT_NAME(func); \ }) diff --git a/Tester/main.c b/Tester/main.c index e3b662c..27250a6 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -300,7 +300,7 @@ int main(int argc, char **argv) if (max_forks > 1) { while (current_forks >= max_forks) { int wait_out; - while(wait(&wait_out) == -1); + while (wait(&wait_out) == -1); current_forks--; } @@ -433,7 +433,7 @@ int main(int argc, char **argv) } #ifndef _WIN32 int wait_out; - while(wait(&wait_out) != -1); + while (wait(&wait_out) != -1); #endif return 0; } diff --git a/Windows/stdio.h b/Windows/stdio.h index 0595b01..ef21ea4 100755 --- a/Windows/stdio.h +++ b/Windows/stdio.h @@ -24,7 +24,8 @@ static inline int vasprintf(char **str, const char *fmt, va_list args) #endif /* This code is public domain -- Will Hartung 4/9/09 */ -static inline size_t getline(char **lineptr, size_t *n, FILE *stream) { +static inline size_t getline(char **lineptr, size_t *n, FILE *stream) +{ char *bufptr = NULL; char *p = bufptr; size_t size; diff --git a/libretro/libretro.c b/libretro/libretro.c index 8cd1324..2cb3ef7 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -195,7 +195,7 @@ static bool serial_end2(GB_gameboy_t *gb) static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) { - return r<<16|g<<8|b; + return r <<16 | g <<8 | b; } static retro_environment_t environ_cb; @@ -323,15 +323,13 @@ static struct retro_input_descriptor descriptors_4p[] = { static void set_link_cable_state(bool state) { - if (state && emulated_devices == 2) - { + if (state && emulated_devices == 2) { GB_set_serial_transfer_bit_start_callback(&gameboy[0], serial_start1); GB_set_serial_transfer_bit_end_callback(&gameboy[0], serial_end1); GB_set_serial_transfer_bit_start_callback(&gameboy[1], serial_start2); GB_set_serial_transfer_bit_end_callback(&gameboy[1], serial_end2); } - else if (!state) - { + else if (!state) { GB_set_serial_transfer_bit_start_callback(&gameboy[0], NULL); GB_set_serial_transfer_bit_end_callback(&gameboy[0], NULL); GB_set_serial_transfer_bit_start_callback(&gameboy[1], NULL); @@ -375,8 +373,7 @@ static void init_for_current_model(unsigned id) /* todo: attempt to make these more generic */ GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); - if (emulated_devices == 2) - { + if (emulated_devices == 2) { GB_set_vblank_callback(&gameboy[1], (GB_vblank_callback_t) vblank2); if (link_cable_emulation) set_link_cable_state(true); @@ -428,17 +425,17 @@ static void init_for_current_model(unsigned id) descs[8].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_OAM, &size, &bank); descs[8].start = 0xFE00; descs[8].len = 0x00A0; - descs[8].select= 0xFFFFFF00; + descs[8].select = 0xFFFFFF00; descs[9].ptr = descs[2].ptr + 0x2000; /* GBC RAM bank 2 */ descs[9].start = 0x10000; descs[9].len = GB_is_cgb(&gameboy[i]) ? 0x6000 : 0; /* 0x1000 per bank (2-7), unmapped on GB */ - descs[9].select= 0xFFFF0000; + descs[9].select = 0xFFFF0000; descs[10].ptr = GB_get_direct_access(&gameboy[i], GB_DIRECT_ACCESS_IO, &size, &bank); descs[10].start = 0xFF00; descs[10].len = 0x0080; - descs[10].select= 0xFFFFFF00; + descs[10].select = 0xFFFFFF00; struct retro_memory_map mmaps; mmaps.descriptors = descs; @@ -446,8 +443,7 @@ static void init_for_current_model(unsigned id) environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); /* Let's be extremely nitpicky about how devices and descriptors are set */ - if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) - { + if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) { static const struct retro_controller_info ports[] = { { controllers_sgb, 1 }, { controllers_sgb, 1 }, @@ -458,8 +454,7 @@ static void init_for_current_model(unsigned id) environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, descriptors_4p); } - else if (emulated_devices == 1) - { + else if (emulated_devices == 1) { static const struct retro_controller_info ports[] = { { controllers, 1 }, { NULL, 0 }, @@ -467,8 +462,7 @@ static void init_for_current_model(unsigned id) environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, descriptors_1p); } - else - { + else { static const struct retro_controller_info ports[] = { { controllers, 1 }, { controllers, 1 }, @@ -483,12 +477,10 @@ static void init_for_current_model(unsigned id) static void check_variables() { struct retro_variable var = {0}; - if (emulated_devices == 1) - { + if (emulated_devices == 1) { var.key = "sameboy_color_correction_mode"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); else if (strcmp(var.value, "correct curves") == 0) @@ -503,8 +495,7 @@ static void check_variables() var.key = "sameboy_high_pass_filter_mode"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF); else if (strcmp(var.value, "accurate") == 0) @@ -515,8 +506,7 @@ static void check_variables() var.key = "sameboy_model"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[0]; if (strcmp(var.value, "Game Boy") == 0) new_model = MODEL_DMG; @@ -531,8 +521,7 @@ static void check_variables() else new_model = MODEL_AUTO; - if (new_model != model[0]) - { + if (new_model != model[0]) { geometry_updated = true; model[0] = new_model; init_for_current_model(0); @@ -541,20 +530,17 @@ static void check_variables() var.key = "sameboy_border"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "enabled") == 0) sgb_border = 1; else if (strcmp(var.value, "disabled") == 0) sgb_border = 0; } } - else - { + else { var.key = "sameboy_color_correction_mode_1"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); else if (strcmp(var.value, "correct curves") == 0) @@ -569,8 +555,7 @@ static void check_variables() var.key = "sameboy_color_correction_mode_2"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_DISABLED); else if (strcmp(var.value, "correct curves") == 0) @@ -586,8 +571,7 @@ static void check_variables() var.key = "sameboy_high_pass_filter_mode_1"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF); else if (strcmp(var.value, "accurate") == 0) @@ -598,8 +582,7 @@ static void check_variables() var.key = "sameboy_high_pass_filter_mode_2"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_OFF); else if (strcmp(var.value, "accurate") == 0) @@ -610,8 +593,7 @@ static void check_variables() var.key = "sameboy_model_1"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[0]; if (strcmp(var.value, "Game Boy") == 0) new_model = MODEL_DMG; @@ -626,8 +608,7 @@ static void check_variables() else new_model = MODEL_AUTO; - if (model[0] != new_model) - { + if (model[0] != new_model) { model[0] = new_model; init_for_current_model(0); } @@ -635,8 +616,7 @@ static void check_variables() var.key = "sameboy_model_2"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[1]; if (strcmp(var.value, "Game Boy") == 0) new_model = MODEL_DMG; @@ -651,8 +631,7 @@ static void check_variables() else new_model = MODEL_AUTO; - if (model[1] != new_model) - { + if (model[1] != new_model) { model[1] = new_model; init_for_current_model(1); } @@ -660,8 +639,7 @@ static void check_variables() var.key = "sameboy_screen_layout"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "top-down") == 0) screen_layout = LAYOUT_TOP_DOWN; else @@ -672,8 +650,7 @@ static void check_variables() var.key = "sameboy_link"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { bool tmp = link_cable_emulation; if (strcmp(var.value, "enabled") == 0) link_cable_emulation = true; @@ -687,8 +664,7 @@ static void check_variables() var.key = "sameboy_audio_output"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "Game Boy #1") == 0) audio_out = GB_1; else @@ -753,28 +729,25 @@ void retro_get_system_av_info(struct retro_system_av_info *info) struct retro_game_geometry geom; struct retro_system_timing timing = { GB_get_usual_frame_rate(&gameboy[0]), AUDIO_FREQUENCY }; - if (emulated_devices == 2) - { + if (emulated_devices == 2) { if (screen_layout == LAYOUT_TOP_DOWN) { geom.base_width = VIDEO_WIDTH; geom.base_height = VIDEO_HEIGHT * emulated_devices; geom.aspect_ratio = (double)VIDEO_WIDTH / (emulated_devices * VIDEO_HEIGHT); - }else if (screen_layout == LAYOUT_LEFT_RIGHT) { + } + else if (screen_layout == LAYOUT_LEFT_RIGHT) { geom.base_width = VIDEO_WIDTH * emulated_devices; geom.base_height = VIDEO_HEIGHT; geom.aspect_ratio = ((double)VIDEO_WIDTH * emulated_devices) / VIDEO_HEIGHT; } } - else - { - if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) - { + else { + if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { geom.base_width = SGB_VIDEO_WIDTH; geom.base_height = SGB_VIDEO_HEIGHT; geom.aspect_ratio = (double)SGB_VIDEO_WIDTH / SGB_VIDEO_HEIGHT; } - else - { + else { geom.base_width = VIDEO_WIDTH; geom.base_height = VIDEO_HEIGHT; geom.aspect_ratio = (double)VIDEO_WIDTH / VIDEO_HEIGHT; @@ -848,13 +821,11 @@ void retro_run(void) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) check_variables(); - if (emulated_devices == 2) - { + if (emulated_devices == 2) { GB_update_keys_status(&gameboy[0], 0); GB_update_keys_status(&gameboy[1], 1); } - else if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) - { + else if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) { for (unsigned i = 0; i < 4; i++) GB_update_keys_status(&gameboy[0], i); } @@ -863,8 +834,7 @@ void retro_run(void) vblank1_occurred = vblank2_occurred = false; signed delta = 0; - if (emulated_devices == 2) - { + if (emulated_devices == 2) { while (!vblank1_occurred || !vblank2_occurred) { if (delta >= 0) { delta -= GB_run(&gameboy[0]); @@ -874,16 +844,15 @@ void retro_run(void) } } } - else - { + else { GB_run_frame(&gameboy[0]); } - if (emulated_devices == 2) - { + if (emulated_devices == 2) { if (screen_layout == LAYOUT_TOP_DOWN) { video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT * emulated_devices, VIDEO_WIDTH * sizeof(uint32_t)); - }else if (screen_layout == LAYOUT_LEFT_RIGHT) { + } + else if (screen_layout == LAYOUT_LEFT_RIGHT) { /* use slow memcpy method for now */ for (int index = 0; index < emulated_devices; index++) { for (int y = 0; y < VIDEO_HEIGHT; y++) { @@ -896,8 +865,7 @@ void retro_run(void) video_cb(frame_buf_copy, VIDEO_WIDTH * emulated_devices, VIDEO_HEIGHT, VIDEO_WIDTH * emulated_devices * sizeof(uint32_t)); } } - else - { + else { if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { if (sgb_border == 1) video_cb(frame_buf, SGB_VIDEO_WIDTH, SGB_VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); @@ -920,12 +888,11 @@ bool retro_load_game(const struct retro_game_info *info) environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_single); check_variables(); - frame_buf = (uint32_t*)malloc(SGB_VIDEO_PIXELS* emulated_devices * sizeof(uint32_t)); + frame_buf = (uint32_t*)malloc(SGB_VIDEO_PIXELS *emulated_devices * sizeof(uint32_t)); memset(frame_buf, 0, SGB_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t)); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; - if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) - { + if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported\n"); return false; } @@ -933,11 +900,9 @@ bool retro_load_game(const struct retro_game_info *info) auto_model = (info->path[strlen(info->path) - 1] & ~0x20) == 'C' ? MODEL_CGB : MODEL_DMG; snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); - for (int i = 0; i < emulated_devices; i++) - { + for (int i = 0; i < emulated_devices; i++) { init_for_current_model(i); - if (GB_load_rom(&gameboy[i],info->path)) - { + if (GB_load_rom(&gameboy[i], info->path)) { log_cb(RETRO_LOG_INFO, "Failed to load ROM at %s\n", info->path); return false; } @@ -984,8 +949,7 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, memset(frame_buf_copy, 0, emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; - if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) - { + if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported\n"); return false; } @@ -993,11 +957,9 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, auto_model = (info->path[strlen(info->path) - 1] & ~0x20) == 'C' ? MODEL_CGB : MODEL_DMG; snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); - for (int i = 0; i < emulated_devices; i++) - { + for (int i = 0; i < emulated_devices; i++) { init_for_current_model(i); - if (GB_load_rom(&gameboy[i], info[i].path)) - { + if (GB_load_rom(&gameboy[i], info[i].path)) { log_cb(RETRO_LOG_INFO, "Failed to load ROM\n"); return false; } @@ -1063,8 +1025,7 @@ bool retro_serialize(void *data, size_t size) bool retro_unserialize(const void *data, size_t size) { - for (int i = 0; i < emulated_devices; i++) - { + for (int i = 0; i < emulated_devices; i++) { size_t state_size = GB_get_save_state_size(&gameboy[i]); if (state_size > size) { return false; @@ -1085,10 +1046,8 @@ bool retro_unserialize(const void *data, size_t size) void *retro_get_memory_data(unsigned type) { void *data = NULL; - if (emulated_devices == 1) - { - switch(type) - { + if (emulated_devices == 1) { + switch (type) { case RETRO_MEMORY_SYSTEM_RAM: data = gameboy[0].ram; break; @@ -1102,7 +1061,7 @@ void *retro_get_memory_data(unsigned type) data = gameboy[0].vram; break; case RETRO_MEMORY_RTC: - if(gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) data = GB_GET_SECTION(&gameboy[0], rtc); else data = NULL; @@ -1111,10 +1070,8 @@ void *retro_get_memory_data(unsigned type) break; } } - else - { - switch (type) - { + else { + switch (type) { case RETRO_MEMORY_GAMEBOY_1_SRAM: if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) data = gameboy[0].mbc_ram; @@ -1128,13 +1085,13 @@ void *retro_get_memory_data(unsigned type) data = NULL; break; case RETRO_MEMORY_GAMEBOY_1_RTC: - if(gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) data = GB_GET_SECTION(&gameboy[0], rtc); else data = NULL; break; case RETRO_MEMORY_GAMEBOY_2_RTC: - if(gameboy[1].cartridge_type->has_battery) + if (gameboy[1].cartridge_type->has_battery) data = GB_GET_SECTION(&gameboy[1], rtc); else data = NULL; @@ -1150,10 +1107,8 @@ void *retro_get_memory_data(unsigned type) size_t retro_get_memory_size(unsigned type) { size_t size = 0; - if (emulated_devices == 1) - { - switch(type) - { + if (emulated_devices == 1) { + switch (type) { case RETRO_MEMORY_SYSTEM_RAM: size = gameboy[0].ram_size; break; @@ -1167,7 +1122,7 @@ size_t retro_get_memory_size(unsigned type) size = gameboy[0].vram_size; break; case RETRO_MEMORY_RTC: - if(gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) size = GB_SECTION_SIZE(rtc); else size = 0; @@ -1176,10 +1131,8 @@ size_t retro_get_memory_size(unsigned type) break; } } - else - { - switch (type) - { + else { + switch (type) { case RETRO_MEMORY_GAMEBOY_1_SRAM: if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) size = gameboy[0].mbc_ram_size; @@ -1193,11 +1146,11 @@ size_t retro_get_memory_size(unsigned type) size = 0; break; case RETRO_MEMORY_GAMEBOY_1_RTC: - if(gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) size = GB_SECTION_SIZE(rtc); break; case RETRO_MEMORY_GAMEBOY_2_RTC: - if(gameboy[1].cartridge_type->has_battery) + if (gameboy[1].cartridge_type->has_battery) size = GB_SECTION_SIZE(rtc); break; default: From 634dcefd01f67577830755c5344cc118ee6708b9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 20:44:25 +0300 Subject: [PATCH 215/341] Typo --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ba568ff..5a2df1d 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ ifeq ($(PLATFORM),Darwin) OPEN_DIALOG = OpenDialog/cocoa.m endif -# These myst come before the -Wno- flags +# These must come before the -Wno- flags CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option CFLAGS += -Wpartial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context From 198942b2734eaa8463fadab376fa19b55e025be3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 21:00:30 +0300 Subject: [PATCH 216/341] Truly fix #249, fix #251 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5a2df1d..8792f78 100644 --- a/Makefile +++ b/Makefile @@ -90,7 +90,7 @@ endif # These must come before the -Wno- flags CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option -CFLAGS += -Wpartial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context +CFLAGS += -Werror=partial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES From 8ac029d3feab5923fed75e8897f30065f6ad7806 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 21:06:44 +0300 Subject: [PATCH 217/341] Truly truly fix #249, fix #251 --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8792f78..e30b3f6 100644 --- a/Makefile +++ b/Makefile @@ -87,11 +87,11 @@ ifeq ($(PLATFORM),Darwin) OPEN_DIALOG = OpenDialog/cocoa.m endif +# This must come first because GCC is special +CFLAGS += -Werror=partial-availability # These must come before the -Wno- flags CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option - -CFLAGS += -Werror=partial-availability -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context - +CFLAGS += -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) From ddad913e06fb51e80957177d404fef3d1d3b5d6c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Apr 2020 21:59:51 +0300 Subject: [PATCH 218/341] OK this time it will work. --- Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e30b3f6..33b8990 100644 --- a/Makefile +++ b/Makefile @@ -87,11 +87,15 @@ ifeq ($(PLATFORM),Darwin) OPEN_DIALOG = OpenDialog/cocoa.m endif -# This must come first because GCC is special -CFLAGS += -Werror=partial-availability # These must come before the -Wno- flags CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option CFLAGS += -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context + +# Only add this flag if the compiler supports it +ifeq ($(shell $(CC) -x c -c $(NULL) -o $(NULL) -Werror -Wpartial-availability 2> $(NULL); echo $$?),0) +CFLAGS += -Wpartial-availability +endif + CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) From 2df6d266bd6fc993f540f230c4c0ffa7b04ee364 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 13:50:35 +0300 Subject: [PATCH 219/341] Add a GitHub action to avoid breaking builds --- .github/actions/install_deps.sh | 23 +++++++++++++++++++++++ .github/workflows/buildability.yml | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 .github/actions/install_deps.sh create mode 100644 .github/workflows/buildability.yml diff --git a/.github/actions/install_deps.sh b/.github/actions/install_deps.sh new file mode 100644 index 0000000..1c9749e --- /dev/null +++ b/.github/actions/install_deps.sh @@ -0,0 +1,23 @@ +case `echo $1 | cut -d '-' -f 1` in + ubuntu) + sudo apt-get -qq update + sudo apt-get install -yq bison libpng-dev pkg-config libsdl2-dev + ( + cd `mktemp -d` + curl -L https://github.com/rednex/rgbds/archive/v0.4.0.zip > rgbds.zip + unzip rgbds.zip + cd rgbds-* + make -sj + sudo make install + cd .. + rm -rf * + ) + ;; + macos) + brew install rgbds sdl2 + ;; + *) + echo "Unsupported OS" + exit 1 + ;; +esac \ No newline at end of file diff --git a/.github/workflows/buildability.yml b/.github/workflows/buildability.yml new file mode 100644 index 0000000..e5f4a30 --- /dev/null +++ b/.github/workflows/buildability.yml @@ -0,0 +1,28 @@ +name: "Bulidability" +on: push + +jobs: + buildability: + strategy: + matrix: + os: [ubuntu-latest, ubuntu-16.04, macos-latest] + cc: [gcc, clang] + include: + - os: macos-latest + cc: clang + extra_target: cocoa + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - name: Install deps + shell: bash + run: | + ./.github/actions/install_deps.sh ${{ matrix.os }} + - name: Build + run: | + make sdl ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + - name: Upload binaries + uses: actions/upload-artifact@v1 + with: + name: sameboy-canary-${{ matrix.os }}-${{ matrix.cc }} + path: build/bin \ No newline at end of file From 385cd1b8c722a89c43fdf332aa48d64a412d1e6e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 13:52:18 +0300 Subject: [PATCH 220/341] Fix chmod --- .github/actions/install_deps.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 .github/actions/install_deps.sh diff --git a/.github/actions/install_deps.sh b/.github/actions/install_deps.sh old mode 100644 new mode 100755 From 17c97c3c2b140554a50844eeb8b3e5d268608832 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 13:59:31 +0300 Subject: [PATCH 221/341] Use brew's SDL2 on macOS --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 33b8990..d8a1a3c 100644 --- a/Makefile +++ b/Makefile @@ -125,7 +125,6 @@ SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> $(NULL)) CFLAGS += -F/Library/Frameworks -mmacosx-version-min=10.9 OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 -SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 GL_LDFLAGS := -framework OpenGL endif CFLAGS += -Wno-deprecated-declarations From 7e908fef0ee5c3cb1c410a178744d74caa55d458 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:04:51 +0300 Subject: [PATCH 222/341] The macOS environment doesn't come with GCC, it'll just test Clang again --- .github/workflows/buildability.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/buildability.yml b/.github/workflows/buildability.yml index e5f4a30..78df953 100644 --- a/.github/workflows/buildability.yml +++ b/.github/workflows/buildability.yml @@ -11,6 +11,9 @@ jobs: - os: macos-latest cc: clang extra_target: cocoa + exclude: + - os: macos-latest + cc: gcc runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 From 097705456c95e107a906fbbc0ffc5f61dacf5a38 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:05:35 +0300 Subject: [PATCH 223/341] Show compiler version --- .github/workflows/buildability.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/buildability.yml b/.github/workflows/buildability.yml index 78df953..639a250 100644 --- a/.github/workflows/buildability.yml +++ b/.github/workflows/buildability.yml @@ -23,7 +23,7 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - make sdl ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; make sdl ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} - name: Upload binaries uses: actions/upload-artifact@v1 with: From c2a395006ea2421ee574ba1d63ec21f48411a35e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:45:52 +0300 Subject: [PATCH 224/341] Update docs --- README.md | 2 +- build-faq.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91e0bf6..d626bbe 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ SameBoy requires the following tools and libraries to build: * clang * make * Cocoa port: OS X SDK and Xcode command line tools - * SDL port: SDL2.framework (OS X) or libsdl2 (Other platforms) + * SDL port: libsdl2 * [rgbds](https://github.com/bentley/rgbds/releases/), for boot ROM compilation On Windows, SameBoy also requires: diff --git a/build-faq.md b/build-faq.md index e2192f5..2b056dd 100644 --- a/build-faq.md +++ b/build-faq.md @@ -4,4 +4,4 @@ When building on macOS, the build system will make a native Cocoa app by default # Attempting to build the SDL frontend on macOS fails on linking -SameBoy on macOS expects you to have the SDL2 framework installed as a framework. You can find the SDL2 binaries on the [SDL homepage](https://www.libsdl.org/download-2.0.php). Mount the DMG and copy SDL2.framework to `/Library/Frameworks/`. +SameBoy on macOS expects you to have SDL2 installed via Brew, and not as a framework. Older versions expected it to be installed as a framework, but this is no longer the case. \ No newline at end of file From 1e7737a23943a2076ee4ab50268055696a28ba86 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:46:01 +0300 Subject: [PATCH 225/341] Limit unroll to GCC 8 --- Core/gb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/gb.h b/Core/gb.h index 01b79e8..703a489 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -35,7 +35,7 @@ #ifdef GB_INTERNAL #if __clang__ #define UNROLL _Pragma("unroll") -#elif __GNUC__ +#elif __GNUC__ >= 8 #define UNROLL _Pragma("GCC unroll 8") #else #define UNROLL From c62704e26b3d34144cf86f2f0eddd7d3404b5655 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:51:17 +0300 Subject: [PATCH 226/341] Minor fix for GCC's LTO --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index d8a1a3c..1ec1fd9 100644 --- a/Makefile +++ b/Makefile @@ -145,6 +145,7 @@ endif ifneq ($(PLATFORM),windows32) LDFLAGS += -flto CFLAGS += -flto +LDFLAGS += -DGB_INTERNAL # For GCC's LTO endif else From 66112f493038f87cc6b96bb419cf7891ab96b562 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 14:55:51 +0300 Subject: [PATCH 227/341] That wasn't enough to fix it, I'll just disable this warning --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1ec1fd9..8f8ea81 100644 --- a/Makefile +++ b/Makefile @@ -145,7 +145,7 @@ endif ifneq ($(PLATFORM),windows32) LDFLAGS += -flto CFLAGS += -flto -LDFLAGS += -DGB_INTERNAL # For GCC's LTO +LDFLAGS += -Wno-lto-type-mismatch # For GCC's LTO endif else From bb5c9f7fc64bb816f213f0e73a9c6e3ba51ae647 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 15:12:10 +0300 Subject: [PATCH 228/341] Fix libretro build --- Core/cheats.h | 4 ++++ Core/debugger.h | 4 ++-- Core/gb.c | 14 ++++++++------ Core/timing.c | 2 +- libretro/Makefile | 4 ++-- libretro/Makefile.common | 2 +- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Core/cheats.h b/Core/cheats.h index 13b7d7b..cf8aa20 100644 --- a/Core/cheats.h +++ b/Core/cheats.h @@ -17,8 +17,12 @@ void GB_load_cheats(GB_gameboy_t *gb, const char *path); int GB_save_cheats(GB_gameboy_t *gb, const char *path); #ifdef GB_INTERNAL +#ifdef GB_DISABLE_CHEATS +#define GB_apply_cheat(...) +#else void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value); #endif +#endif typedef struct { size_t size; diff --git a/Core/debugger.h b/Core/debugger.h index 2906ad9..4868df3 100644 --- a/Core/debugger.h +++ b/Core/debugger.h @@ -7,7 +7,7 @@ #ifdef GB_INTERNAL -#ifdef DISABLE_DEBUGGER +#ifdef GB_DISABLE_DEBUGGER #define GB_debugger_run(gb) (void)0 #define GB_debugger_handle_async_commands(gb) (void)0 #define GB_debugger_ret_hook(gb) (void)0 @@ -22,7 +22,7 @@ void GB_debugger_ret_hook(GB_gameboy_t *gb); void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value); void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr); const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr); -#endif /* DISABLE_DEBUGGER */ +#endif /* GB_DISABLE_DEBUGGER */ #endif #ifdef GB_INTERNAL diff --git a/Core/gb.c b/Core/gb.c index f6174b9..2b22f9d 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -13,7 +13,7 @@ #include "gb.h" -#ifdef DISABLE_REWIND +#ifdef GB_DISABLE_REWIND #define GB_rewind_free(...) #define GB_rewind_push(...) #endif @@ -57,7 +57,7 @@ void GB_log(GB_gameboy_t *gb, const char *fmt, ...) va_end(args); } -#ifndef DISABLE_DEBUGGER +#ifndef GB_DISABLE_DEBUGGER static char *default_input_callback(GB_gameboy_t *gb) { char *expression = NULL; @@ -148,7 +148,7 @@ void GB_init(GB_gameboy_t *gb, GB_model_t model) gb->vram = malloc(gb->vram_size = 0x2000); } -#ifndef DISABLE_DEBUGGER +#ifndef GB_DISABLE_DEBUGGER gb->input_callback = default_input_callback; gb->async_input_callback = default_async_input_callback; #endif @@ -193,13 +193,15 @@ void GB_free(GB_gameboy_t *gb) if (gb->nontrivial_jump_state) { free(gb->nontrivial_jump_state); } -#ifndef DISABLE_DEBUGGER +#ifndef GB_DISABLE_DEBUGGER GB_debugger_clear_symbols(gb); #endif GB_rewind_free(gb); +#ifndef GB_DISABLE_CHEATS while (gb->cheats) { GB_remove_cheat(gb, gb->cheats[0]); } +#endif memset(gb, 0, sizeof(*gb)); } @@ -643,7 +645,7 @@ void GB_set_log_callback(GB_gameboy_t *gb, GB_log_callback_t callback) void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) { -#ifndef DISABLE_DEBUGGER +#ifndef GB_DISABLE_DEBUGGER if (gb->input_callback == default_input_callback) { gb->async_input_callback = NULL; } @@ -653,7 +655,7 @@ void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) { -#ifndef DISABLE_DEBUGGER +#ifndef GB_DISABLE_DEBUGGER gb->async_input_callback = callback; #endif } diff --git a/Core/timing.c b/Core/timing.c index 9eb9c91..8fee590 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -10,7 +10,7 @@ static const unsigned GB_TAC_TRIGGER_BITS[] = {512, 8, 32, 128}; -#ifndef DISABLE_TIMEKEEPING +#ifndef GB_DISABLE_TIMEKEEPING static int64_t get_nanoseconds(void) { #ifndef _WIN32 diff --git a/libretro/Makefile b/libretro/Makefile index 75ddfc6..7e19893 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -260,7 +260,7 @@ endif include Makefile.common -OBJECTS := $(patsubst %.c,$(CORE_DIR)/build/obj/%_libretro.c.o,$(SOURCES_C)) +OBJECTS := $(patsubst $(CORE_DIR)/%.c,$(CORE_DIR)/build/obj/%_libretro.c.o,$(SOURCES_C)) OBJOUT = -o LINKOUT = -o @@ -306,7 +306,7 @@ else $(LD) $(fpic) $(SHARED) $(INCFLAGS) $(LINKOUT)$@ $(OBJECTS) $(LDFLAGS) endif -$(CORE_DIR)/build/obj/%_libretro.c.o: %.c +$(CORE_DIR)/build/obj/%_libretro.c.o: $(CORE_DIR)/%.c -@$(MKDIR) -p $(dir $@) $(CC) -c $(OBJOUT)$@ $< $(CFLAGS) $(fpic) -DGB_INTERNAL diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 3bf1783..947b14c 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -20,7 +20,7 @@ SOURCES_C := $(CORE_DIR)/Core/gb.c \ $(CORE_DIR)/libretro/sgb2_boot.c \ $(CORE_DIR)/libretro/libretro.c -CFLAGS += -DDISABLE_TIMEKEEPING -DDISABLE_REWIND -DDISABLE_DEBUGGER +CFLAGS += -DGB_DISABLE_TIMEKEEPING -DGB_DISABLE_REWIND -DGB_DISABLE_DEBUGGER -DGB_DISABLE_CHEATS SOURCES_CXX := From 8e702f14526cdc392c3ac6f720b3880b35d04747 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 15:13:04 +0300 Subject: [PATCH 229/341] Also test libretro's buildability --- .github/workflows/buildability.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/buildability.yml b/.github/workflows/buildability.yml index 639a250..7cd5298 100644 --- a/.github/workflows/buildability.yml +++ b/.github/workflows/buildability.yml @@ -23,7 +23,7 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - ${{ matrix.cc }} -v; make sdl ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; make sdl libretro ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} - name: Upload binaries uses: actions/upload-artifact@v1 with: From bf678113923194303320be9297872737a038fb6d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 16:59:47 +0300 Subject: [PATCH 230/341] Sanity test against a few test ROMs --- .github/actions/LICENSE | 25 +++++++++++++++ .github/actions/cgb-acid2.gbc | Bin 0 -> 32768 bytes .github/actions/cgb_sound.gb | Bin 0 -> 65536 bytes .github/actions/dmg-acid2.gb | Bin 0 -> 32768 bytes .github/actions/dmg_sound-2.gb | Bin 0 -> 65536 bytes .github/actions/oam_bug-2.gb | Bin 0 -> 65536 bytes .github/actions/sanity_tests.sh | 29 ++++++++++++++++++ .../{buildability.yml => sanity.yml} | 8 +++-- 8 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .github/actions/LICENSE create mode 100644 .github/actions/cgb-acid2.gbc create mode 100644 .github/actions/cgb_sound.gb create mode 100644 .github/actions/dmg-acid2.gb create mode 100755 .github/actions/dmg_sound-2.gb create mode 100755 .github/actions/oam_bug-2.gb create mode 100755 .github/actions/sanity_tests.sh rename .github/workflows/{buildability.yml => sanity.yml} (73%) diff --git a/.github/actions/LICENSE b/.github/actions/LICENSE new file mode 100644 index 0000000..8c295a2 --- /dev/null +++ b/.github/actions/LICENSE @@ -0,0 +1,25 @@ +Blargg's Test ROMs by Shay Green + +Acid2 tests by Matt Currie under MIT: + +MIT License + +Copyright (c) 2020 Matt Currie + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/.github/actions/cgb-acid2.gbc b/.github/actions/cgb-acid2.gbc new file mode 100644 index 0000000000000000000000000000000000000000..5f71bd36060b46eefcf7785f81ce16a5a6c5fc67 GIT binary patch literal 32768 zcmeI4e`s6R702(B6iIIULvq*Rfw3ebr>2mSWRz%~_VVn=jpBvQHdA8u&xD&fT4z$% zrkOUxlcJ2yHpOuhmfGMZ#(%8TEA{qA3nLKHn%NPK?f*u@njw-bW^7%x1yW}b@9x}t z-_w&Nx!$1J#yI!!{OVbFZHNdXJDVr~T>63G<(ivd|^1e%jxEy@u?x4wEya zn$!@d+cPpsYCpMt^6t%>H>NIMKJnto+P_@C`N`C!%fI=WeDF~F?zV%6L;FOvR|`e| z`NF901Q`c+ zJ;?`h3T})F`yZBnA)f|0Ddc<0_vN!7-xl)X@?CiX(c8rR#w0s~f;0%!PTtE=~z)fI(nY{=>wgnRMhR#!@b!FCc;>`E*Q zXBV8k4kZ?aGY03NgYr~3rBO@Pw{LVY3wn1B4 zGqnp^FlcJ4m3f#tl~NS9+tjSLcdx0PPC_OpP0f1Q>}FlASPq-boJV)J+uhxL_N*k; z)-vxdm?TepOM_Sue(mX;@;IB`Pg{{Bd$zdr;`=v})a zk)uZ=k(L&rhll;Vf6{oH0>NeG~uPHbQxBg zpQ;KkcCwxEHk^Nia~(9LQ}jIC&A^qydyWZh5%h22%p~DOM>D>N7YrtjCyvC!i6e>Q zEMGj%dwzZwUQ2}hbK0VohI3AX8rXQAlkLyKYk_(c{WV5aRohu@W{bz;VK~6v)Fkgy z`?SUU>l>4~93e2D1^NaZg=!brm_#BGf=bxl=kay>J)RTh!}S#Xo8hnXNo#*^s&8uI z^`^Cp^bCDK&#-z`Rr`o*JwEU`O&`=%@uRK~Z^4L{Owtdb|6%gS-~o1>7ylKq`5M~b z~OQI^>Z_4%Bqmmnu! zAGKd?r@x2seEl>H<}xKtF(2SH1>X96bo&(ZVRoMVl9mP!UV)*ti@6EGTl)xXZ-76~ z2X;1=W#jqy+VuzhtO~xr*i2xp^LO$;&F78%rM(9GiTmer5El+3-{+sxw_r7Y&JQ5K zv;<*WcBg*+zwAxmi4pG^m(Br@%ZnJ?RfpS#QbsnVXgm1*FTX^pHB{ZzM33XpHCzT z&x6AAN|xd2wz@H?t-_9w34#5Zq378zvvSLYva`UTUcQofy8iK<@n3s4 zGVsNi1}l6j#zz#1IN@tCnPbgh`HA;Irn$zO&(yxuV5yDWPu;41`qmEb!_0~UK2g(_ zJqVwXxt8sK56Rrg9)i!i#O8e$!geQo35R|3X5I(ip&{OtXV~X#iiD47wKFUG!*X|4 z$rN6buUs_VMFL0w2_OL^fCP{L5 z8~?lh_IfqR{NF^tM8HJAM8HJAM8HJAM8HJAM8HJAM8HJAM8HJAM8HJAM8HJAM8HJA zM8HJAM8HJAM8HJAM8HJAMBvXEflZ}CU+sBi;`?)Fi|=;HLZ2i=t0Gcwv`U(YR!W`G zib!l&Du`YveHfi5^+hi#ubM#4JSqDh#1DtYBFtNkd`duGOq#>GE2RDG&WbVI*W)<2 zMHIF#TsSGeFP^_nIC!lnEL^xwfGDXnS^`by;qLRLUD3ItRc~a8XCew}G{4wdd!p#z zT{Z9ZN0cmU$%&#o@qIci5%tq8`NC+`cWUNnbvQ&ia>cj0`XdLzTG|mxyIdTLD8tRt zoZ{Y!VBW#3O7T5+)kMB(>am z&?COHwri+A!h!ifZ9Fv+H)V@wir3Gs8P@JZp;MNBJtGXhQ4${AJmcqg*3Z^V(6&3RQ#cX!v z=J^hpn}>TXeWRT5_A)V;7t3 zJ{0gSLmIP{Ge2>v5q}7-XdtS@}X~?*vYUic93{%Wpj+yy!lY^!uXeE29CPKCU3t{MhSWIW`FN-3oiS!s+E#?tUyXod* zOSa*|w$zMKxod0aX8l%qS@qF8>jw+1!lA66hXp={eyt9lclZTwSA0OlwzJNP9h4tQ zeHlb70=lv{x-)ujbXO#%ygFRIYvP)^>*_vN_s>-Y2iG0E=-~W?){@)1-u|ym-ETj) zW!u}kZoT_$u~Pi^s)*=W z<<-~@%PTRZ3%7OmTS77A%FkL(#gvVkEpM^@irB%m?7qseH|C5NuFDS22wLR_t7D3# zD5fA`R&mqwN1mxHE@aPC2Cb+JYMsA4_Yvy{JGXSp43I-DmC{ zZ0mGqdV0kr$`wV>M z;4^_wmGQmFjxJ%Fs@Zp{*(>}pYiz#bcCWj`)l^7N&9fCL4+xvRm^T>8V(fQSY&izz zu_ZEF5<|1b#>iYUHIlVYdS)bhpJW@!*@u@(?mnq^MA#P@j-A;B zly?s{FJ6PEb{BeW_u!JT&Y=ivuaiOMG~L`_iTxjvYPw`CBI<)`QKZ#2y*)K@TQNkuT@KrMAzev ztE7LAZj%3h4NoeKt{;nh-;yo$zR`=S(8E=8@o=ns?_llt9&zT_xL8xjx@ux$&(>Y) z>cp25m0~}tL3Qyzq$tB@9vci6paqgx{>{PsxhwO;52U?l8@?8_n{mg{2clPwMP8+w z-BneWTvEDs@Z$N4?uL%jL$Ac&6&CsvEJAB*D}Q~oj<@2UAXub^`uyz{!O?25HU#ez z%GQ*vb2~mQ6crWm`-;m@uB=#SUx(wmc0q8|Pt-e*WwG=?p!#REHsGX3yAp(k1|d7Y z2m;p&^$yoohofGwP>7RDA3Z%tqp}Imn{^6WTs(R2~=)cp|APE)-=y5oN?G6Y1LwZ9)1LStK zIvkGHF6eLA-q3)0@neG^H8i-OQa~<>IfQzuzNKEsr$3ZKnRb*#Q|Yfq2);djG#CsD zj;%dCC`k3iUp?1{8~F9s);0VZDoKAm$pMJ;M|$wb^*QS6Edpd69e*409Q9On%BL!* zxIo_mUTlG|o?4%uEp+kgE}>*yYcSpqf-v6KVAU?`^CigV@S^Bajk+@!5J9=f8c98({Q?j;x}C-<2S80nebC8#{1jU(Rw~EU1DAG-r3lr+eGR9c zP=_wg1pzM@dTUz7Bk@OprC$!~iv*q>V5B`HZ)ED$6d#E?c@K9eWXY^1bzb~FYO&)6KI7B*(9%`f= zp|s!6Lv8K^3;% z<_Rq7pDUi>B}#K}^U9=r>YmZ6@pk{Q0DL%fI*L<%cSY{??oPYM>1q}?Tz`w`ZE<(B zHo2;OqRrv3H~P3fIH=aLC;LcBxpks;d@Ty^7+){O$hXF&)a4~faXTEEB()l%RrI+< zc&$Z`z1d6tF7|ycjs!5~S7qei+G2N!D7#cI>}+4oo%WS};x992fV zF3V=5$bn{erwCbX@VV(SaAIrH1fdR8UjAMnD_brvnJD}znwYb6$9R?ayNH^E^O_lj zKk!fvbXVdtpYB*M4o7;g5GG!lp#Scf_%_Za1mPt?XrIvkCchr!50o^nY(CC(SGIBF z!Igcbyo1JJ8k_w8pey?!nOTCWD_cDl^6McsEOS@3B5SVfq5xYFV4n`K4FNVN!?(66 z0LLOPgH!IxzBn+oD|=HQ#g+Y#Os?!30!CN1C7A5WzB>R{_T2%vvK2YWm95AbT-kVr zxGVb}IfA+lv#4xvW$#y#T-i}M?#f1v(Um2dMRR4Jq_=8jSN6OhyD-Qu2(riVAV+2EiaLE@d$~7zr%c}L8)Q5q z7)y*s5Bq#SyxIE|?#=cqC_Kh43d-|?F|Yt?Y|QGZrddA zwMF`Xj=1hS5gEO2(`ePcfn|{h%kfiaf4l_G@FN2xC2XnAzSiz)_O*z2*c;t#WOT6V zT)(l^Zu5vA2~qMk7%W`fqR-ie zNMsaNYxvHInDlUpkWc4s@cHa*?LN{EqXLN#Th)4XI#IcJop=sUYO{4Y+dA3|vV{|) z_g~Il?bEAPo_Djy+1zYL8z$RORM}di4A^Ms#iWHPb*#-#mL~dOt?;2{T0L!E(O9LJ zbVXc*-YLn|+b}~H$lKH8^xE#A=>?iRwa#Lr+qKp1G3Wt2Q|kc?ycxaP5rZQMmSqJS zxhldsNf+6eJoY-fi|QdZw%A-Qd#i>~qA{$@ugVur;FOPnWivBK#Ncta>q;71-Hl&{ zQoYA8P6dpHgk)9hdZeWev+BYd`M{z|CwlYutt}pn@px^Q&Qfa&sI@lg1K#U%!XU=$ zsZ~0q^{69+olaBlzO?nOFZu{&iSDH_uXgU#$_cbA_KzW$5%5g2#9N>b9euRi@Bplw zo#>a18?Mru*`$uNcopUvfKI-L!%najFtrs8Vs8`7e{(_pK=6t-S z6w-!pUCHO%tsQOj{<1Z;kbeD@L)s@HCJd=ql#))IVnn>9cM2c6yi@QRipVz%q%kY< zuP@!KX#7HhS#fV@2leRmniXFUv9)VfKH*?3-b> zJPfnq6JeMYc^RB?v!X9NwOR3raEe*+^$?jA|1oSdE4B?Jn-vd-VOBgGhFP&JoMcuk z3uiDZ;>(De6_17@sOvCW8#b5~^9Pd5ifhAhvm$bgX2s=UfPnG|vts^0idhj&r{?p| z=ffGyii07xB_c12)Uww@>CB272Q;(d#(^o#ieDaJtpoUt!G6mdWnEc^x3<{m^Khf$ zV2F&0NpAzysF**%jfxji(J|&4klh0@v`aTCt_>TFiux5dD&hwY`g)R4acww_Q89nO zG%EhtlLfN@CITh`A1egp&!08_zm53+=FI#b`F-*HY2yFTafoyn`9IQ*P};fU|J%as z%}^5mU!wB=5|#g}`9}WF^Dkt`C;qSI8~H!-iT@*?_`jNOUIr8QHF!;avi!-3n zyWSI+>0H2o%OK&h3Ax+?(6wpwKKA3j=lmzbVEW$)TQ+3Je_K0Uq8D@@!3!<6RtFsX zI!9|q+=08vz_3!tkg9wfKX+%4&NoS?s&f4aDVx3D&vjj%W7Oqvdc3|VRi%=yNqQbm z(vzj@S(>}S)$QZI*YB`Q$LN&JLESFFf~Oihlhm7*THRpJ3o zYLrXdBdPAmGLz~&xed6!UvI;zxT!(2Ds;H&93H!Un_aJkuLVqvb04h=>06Pcu~#1i zeBkMWfNUrFU<4nH4~caJy19eviq}Bvu~fqcnF~ax@As%kAGdJ4-ydgV2Hdr$flMIU z*t~Y8RuLK`ye=H#c*o*tSfNv8b@=-_osD)L>yt1&sPLt!oL(8;(Mt;TMkh+TeJyrR zr_&3GYCw-V5DcP-;q#}<0BkRHr=f{L{PE}A9B;Zlqh3eAJ9v1_}%{x?ESiu4oH2cd~dLU)AE`ZfxxEh@BK`q&-N8 zpi*CSx_gnrHIqd(+&IsF>)^!z?Dm*l`)qM1rh@)c0xL$_)ly*cIc z(h+$6{i73jl!$*Lfxv$O1RnX*OW>ajv1J;8SC57KdWd}(;spM9NF(qSVfL9YyD7~6 zD$GVhAn>;nf#+p#$_f0XA3@-6PbKhAasuy6BJj&55%@=mz&}a^{&*;fz#k7~AnC}AwxhNBX-y32j z5&2}emOYu4z~|`%K5uFQzjT1r4X`Bx?1y;!pAK19)oCU%PT==)0`DYq9!3(Qv7-|B zP=pis!3c_uvE>7@6$E}Xq!aklAtQm;uQ-9nF9`JYBm#drl!m~EIDa>zf<9Ki{>{di z2$%?bk`a*eLTBs$pQ(WVp9y62|0BOIoA#5a zLl-f?SyQ%e_DBJ%><*6EH+QU?-aC^QCoFMtMfKEFa<#DXm7G{7e2_*T^)8;V|P-cZMxAx ziwV$LiN)|Fg_gogfT#0UCBj+OGP#wZ6DTuTzE=*A^tYf)hg(N9twmx1HtyB_aixdRNHQ&V}*8LcN@> z-L*4y@siabp1LWmczyVA@h6kS>rgH!)J0yNNu&;|TIB+*;~`V6W(FwAW+enCMkOB# z6!2SL^zRFcTrgGvUnh8GAYYa5ja>N?DxQVe9xL+mTD z9^q3;oU9?oNY=ik00fjzkTp*zg{-0J)O`L4DjB@3*C^~KLHY4uExSQU=WTr^qXl;f9N(NGL`JGOwS$(W zIh(m-y4~a43hM$w#@oBerypnGDVBlrK&5h_wHMqD+UZv5aDqWEe&c4$x(JS7QNv_+d5qs%WPVA@1FnOhED3 z&F~CUOOll&m?fb%S%ZbpORw=S@{={HEy6?v)eYKd>yBGi(pm==qETtoac0WXdH?XC zK&+qGeZ1jwQmd6zO~|IuZgd5&JMUzvn-aIgh3K7LIpd~|JA57+7KGqU=3X^gEZ_&wB#H688X@Zue` zfcqtC7m>pKnHA~-VeQOY$aoZ=c91l9v9$ts4y*P7(dk8`f;!7Y)!~DVd|k({6OBb$ zOnNzSO*S<&91zNn1?5xej0OD7q8keULi}9=W5H?|3y?p(#)2M&6~iG(#sc+N$d5zp zm})FIpc@Ou6}C9Y>Vj-lkPVQr;4(56@G>~%#)3J)sf`7fr5X!*xUt}>Bx6BwIN4ax zM#h3RG8P<2HWnPnWGui-iW>{=g0Wx##)88}W5IWmjRl7j#scIRjRgY=KtTC~vEaL@ z#sV~*n$JJK&15X_Dr_VuM_?@INoy<^){OfwADQV!c!~7U(;F zaHD~j8x8PGPb3~HZZ!C=YBYE*1Oa1gPFOAp$Iv?6XmB{mXrNzlqruI*$0r#L4yQ93 zeAhG@Oz|_x%rX%$5ik)j5ik+>SRo)EmO)6)YX1Mxe(-+;?q%Ac4*7lY{AuF<&~b=# z82LZaj!@b;;r}$ve_qzMXi3(@{oT>?yTBz%{Cvl`yjcg^Yu-Be9FzTX4}PKMm+Ve| z*84L{1K(!B7a(zzZpp5%;YWC`H2w=@RK9`I|!NS z%u^$EGX9F>`QD-S0ztOd+UvZZ5JKpb;Sc=D=#1TF-$d}sBu^v_&fk?7GGNSpsDO05)Rju zeWj0#vqG6pK0Cqk>4bdD=$S$48A{~2lN0%!$fcoa%){Lri@N;ymIYc}Ja(@gaT6<6 zt`q;)HjxM7A`DvDLE3 zB}@=g6v6K3Tx7}T1=HAF_=wZ(F2*qkyUVrM2>|)iYj^p&%$8|80jNjJ_{H@QJE`sj z@Up%Wz&ySUz|{fv*#J91I|1AffZc_cp_Tk;Q~Tpv zu!nX6*h4!3yqvrfz{{C-0>FdA{lZ_tP5>t`2u6%M0fdrw0vJi`1b`f)-Q|P~5Kum0 zcL}BL1c0Vf^Z92M3|MJ5wg0NjvK4s?M?uBLCx-x7sQCkY>scb zz=&*J%a`m@$FKH+pa+&)UcSnA1^6QE3V?CNXe6;Vvb==UT>%Cawgmp{(zS zcLf+p+7&>*;+B_I-t&_zFC*!81qhkD0{n5mnN4jb0ww|`0ww|`0ww|`0@H(lT*JxF z*{uJ+)DQlDu|K2#ANhUp{AuF<&~b=#82LZaj!@b;;s2nD7b5!mB2F0*?M=(};d%kC z%W+&xtMf|&?WD(|qV!_)Q_@M$&R<365#2@22dU=VuZm6fK9BYEbJ%5m{P0H>qb%C- zjv~PITjTn&v63HF`2$fUty%m^*PW~7@fu(yz#sRq-#|;^lT|2}es(L%3doPh^2u~j z1sqSE-5L=FyRG%_psGzTyY>0mEClOPRDpUdL=}XMQ3X#XM-_wSb=`U$?aeAj-Nzs!|B2bo;1S>rswO$lxiYiB48q5 zB48q5B48q5BJd}Mfc)ZF$N%?H{Qv&U@&Cy0i|0=h|A&r4q{GPnk#>aA&JF+PyUfQ} zNB>wbYy4_4mUU{d^tR8jzm?@mIkE+}Fe)cUb(qBCm z^6MdHQ#tttos&P{2PI|){Ol)wb~ABunIipp8Ju!XzV{;{{pHk1{{qg*SyH6`wS^E6KWet!gY9cH%~Ir*AoPJUa0lOxB-$#3=p1e8yBoY$ms zax|Tq&p$gdaq%gKE@C-+Uw$sY?~*Ln7L0cHUwzs+yGtgh3xmG5ST zaF7-!_VaVQI3xd%Tb&pM`eRTr8b;h8C9hE#`6iNrovi}$w-M-nbDWXimc+>QE6&J& z#k+kHBfl*jBVS`O@;|X}3bQ&U0ww|`0ww|`0ww|`0ww|<2L$AwpEdtKLi``gJ7t>x zkNm!P{xtD_=r}|=jQk&IM=0%2g#W+XQ>r|~z9-9vwlB2 z=x49^sd+JWEHV54m}(P%8$A;1vJA(%Ekm(wmgBLzEw9D4TV9Rru)Gpex=NP zZB5t&(#-xx)6p*V^GGI}K&hWSAj{3L36!O^3H-sc|Choh(C%MhRc!+L4gyJLfl}_M zA0o2=#ucNHq?!fN&i`+Tn+4jF%mVrqHw&ENJwM4T(4NjLV9x*lIDF}tZ7>lq5ik)j z5ik)j5ik)j5jbZE$UWpOJgfQt_fq`7H*@?y^84cX)5QOw;}GdE@_(cqp|o?t|0%qG zg2MZ0`uN)j@gI$zNAda(`=z}MHEX7tQ4s$y3&|ESJ3gP??Vx=C-5sq>*w5W=^N82o zxI&#vkGtpcJp(SlRADNVGi9Mwd;&h-9gt=Nps=R@3ewL=KS>+VrR7c~ZS@K+jV33c z@F1_eHj{y-CZJU(jZzCBl>!;`d!eYne?dV04_V%zq?t3%dr7xepisQuV6EuItODdu zueD;p&%UJ1Do~Gw{CWuU2l%Xl9DU9_V#?hz>z3I)GMh(p=J(L70$v8EJc8kiA2F+7 zPwK3K0dB2$IB8bFmnO|Bc%G~k&y%$xCwb0%PNq5Y7$bbn{7+!5m z+V+0hl3s3)*xq9EQq(n;t>fF~CD!xPCIwuK}Z7xvInC z!M<(c9o^Up4Vwlw+3rB-w#|o4;IRi9MRePmny~hrQCe}mLByiG>)f6;TdQbmY_xl^ znqZ6F#kbMZx6f-zE{wL!Ct;|EAn5fd%D>g)L*L*499LqpxW&!CN_LO@Khwe(cH#q$cT!Pb5qc2{FJwnYS5TWzh* zNmWFF1Vq&{H`|;pA3oSlk%QgtaYLiNMch_*t9ZF+jjJs~HTf(INI4N#TU$7_6fZV7 zw7WGyH0J{dO%^2QDd(R$Q1kwHDFC1aAy+B1Aml0~b$-W9=~fivV_3J27)Ld%BTryO zLF7-bb>vMLJ+u`C)ng&Q9%35;+&WSo&~|cY39$biVBZK}N(WmVfOX{WX+=R^2B+LQ z()|%D3jTfSih^(Q6$PJ8T2atFX+^`c0l}wf0YMBSMgvE+h!h0* z)(sa?xiQurl<&d0g7eW_=`?&0t%!xH!%%-rc$P zzGumDvi;MI(Vg>ndiQ(JdFP({xli}M_XzoN?9X3Ln*V;5>2AxyU&GQ0^_6`#!=Uc7PR`uLSAhh92d@t3(9pN&sk`Q0wL{kgq6TG~3>cNSJV@vCnx ze}i6|-xrv#33%Trb`jk&O6Io&yvg&jRN=1omwVQpx?nWCn$?vYvk)r_(CjX9&Z6*O zPmxO*Guojd7fD(q3yXoVcDSNb5dArsne^2|iSD~{j>}P@fEV3W{vQ{aVXdPaUwYYTcD5=h}-dAM_x^`Hv zGchy!H@OL ziM&iZ^K9KON$br#x6|yjfSK*hev8ElW=3rCb1#qJDGL4Iwq3AV&Geh^%8|H}WsMa{ck zR15{=_2oC$i#8gx&OtY_Y&avoNv8~zG z*c{m;GELbUT1joJ&#~fItek81(F{a`WwV)OZArT`_4(B2-Q9MnyVu^-+imae?X^dJ z%EeJdx%jCs1lQ2TPo?bvA~k)VNWOj|DR2gUM5NvWL<+$*GC-s#^u-Pm2{zS;uZ%>L zG9?m#vm4IdawQUiGXiI{ob-IlNqYRHq^F;qPU3r}h$zn#6W`$?k8-%!6Z>vu-sDTb zUU?<21DbpHZq$Y9)~#GmNeLliV>!KLi`$*aa+HJ$@ktH8cKqR2)Fx-FWm0sWuXY^>~uOiJI|dnHKXQs z-@9jOao^OGsXMsEVy2e3WipbaX{^s>nCZ9M{r>iLzu(l1+L<#izG!N8RTQ&2LYRMe z_{b5{|HKp9!LD94b!TS_SX-N^>+Ah~k0)2Z*>7G&eWj&Nr`>L|v3;(k#bPNfy?fVg zudWs~o3VIB0!RP}AOR$R1dsp{Kmter2_OL^fCP{L5*^Akz0x=`l1kmYDfH7%cXTv1Zryt5kkH|9Fc=QEg9*KD zTQInPe=ykCDD>c9LqmPN*K59Q$=;o0Z8Z8JFPv6P!z+DIKS0p*IazcwIRj*JnU~3}-r#AS9Pxfn=_#mon%xtR$J?P2_U!ZghJTrl3G?B4oc`U6H~6IVyR}ud zwY=Vxex9D7_vr~%ud3=Fac$%WK11|=rIR1<`mm92+RT?o&<~;gVd5A>fZyQ7|AlP6 zrZ)N1)g_M5PqOFeCy66D=@WwJ~JwK>mPyd zG{p1qz|Zbwqw(?h+J*fMtP1{ov6;YHXK!bJoXwigm;Nd|Pdq-8fxK{-CeUee0?S$##moIKe1Qx=f#-MdVWJs z!)##p z>a+3q`hKv!U&%7;ZVRhp`T{%=G9mE1rs-Ms&wQ|D+VxS-Kho^WF6z~J5}5rU{bbda zM`G`KR@3n1nQEJhjCzS;Atmrdne>5r@ErHtPuG`wvgwK+R9h<|E6JN>Pu?u@tfl82 zfy-Kp)&`%LxvF)*M`mtodoO$8^EJdX|5N~4dc}qL-2wSKnv$l8-|YY4jtAF-y+{BF zAOR$R1dsp{Kmter2_OL^fCP{L5Mi$oN1F!&__JA51%hP9B8x}_%i(cQ8n zV=DsN{F0f3@E(a@7~;HvyvaN=guJYnKdq|%O1XCM7!d1WXqk~$q7abCG&TmLx6iFl zSF5{4vK+-BQ(A7<=hi*vo_%js_de&`y3^Bt*+1d0?N7;Haq#y}=dTtB^Rqm{ZlOR} zAmkMfc>bqNWg^9`^RWH=qaFBFli|_OfMh=9vb4TdhW#VKcFxDb1 zEF35g<_u?4h#z)VPUi~4+p9Cza}kwO*+c)h91xU9A4fk2yOyFwpF?Z1#Z&9+*9+Ou zBYv=!LF-}&EarG%KjYC)vN#?eO2Tljm{B(vQMxPFP(mE)`0q6nWDMR#MIaD_D8CQnG?AsF>Ju zp!!N|$JjX)*L8P@&PG?8qgnJiJYG+}FiNLg9nG%ouIBE>wzfXe+3D6!w|BZ7Vza}G z3Z9E_jy0AsXBp5cmL}16LUd7Kj&yHyaZJi$_moT1G$0rOzd-!W#Gdhi5#{}glftfX z;rX588PC5`mic_fBSNt-!ivi?a)si7Y3z&^7AapXyYZCpeCMf*=UYD95i91GmFbt2 z@ypmRmrq7Y7A>FO`;>Sx0{huR73`?}lo+Sie+RU-fE^{o`eJ;b6wE!vY^eZ>huQ1Af5mG6qy^C+jKSMdcAA z$Hv%+*aURtK=iKY-O=5VSm2ehvfa~H)oiKxTFt*z<_*^lpEq2x%wBYB@4LU>-1qL& zTX(#>`^G!p6)VL5sGMHzUUkQ+uF;nVCnK?`eE(oM`)(C>B~jbv^X(`rsrYTomc>q$ zuRI`rh`VxQ1#?!gt_rrj0=*oGv7Hrq9qdj9VuS3uior;%*ESaGvyH}f*p9~Tw7nkN zX?rEM%l2|C(2L9Z25q5O;EP|ey&nr~y2kbn8!V3vZ(#RSOdel2m0z0~oENk!->r%T zYz46ZPRuWCe&)!}Dhl)2&nkj;GzP8CU7h`){o}i~_TsQFGCCGpv0^0#!LISUMs|+h zJu->Tda=TmIgK9rMR`OUkzZ}}cpS~bs3a)At2{L@QdgaGS@qkth15fvIy)SC$z3Cz zr|uqU=|50?7HR?n{Ey40ro~Jt165-a1FK^bcj0q4KD$?w>BeV$#bjh+;5qT^iMyT^ z7fjs!jF>mE`)M&t`DHn)sZpNG#ve5UcKG(SJtn-{XJRqQ8K>}7ttHMh6i>gnw6XwIj*=a=P4Hws&@ zf*TH{G4^pK+kgT3)`iL|7sg=T(m! z;)nc-FLihw?vBPb(cyM?x&h%d5R&r81wqOl-#sGjAKx<~y*Zw-U;5d2=6coufsc^-lOrC*qjReyKnJeb8YzCII=3N^n^&^vC(D;Z z&R`Cn_%ZhVD(S&!J+8Py`j6;l~3zhwEH&~b9~W#gG)qd&nWw6(SIr$^gV8~zD` zO=_si-Dwk?Z8m#D@E)ObeQ9l{^9w>jK>@$6uoU%53x%#)Y-_s&!C5z5=R}ds)(?T| zpWWVooqp{|5E>eU%-jM9tQYE>9owDGI>AQuZT&c=f_|aDz*dlp^97}KZT-;I->@^6 zj_Vp4cGC6zbeY|b>z(wEwo-0EE$O9y+SV?R7Q2nw3ibGheX6PIfxXawdq;yL*dUV#bSLp{{# zLS2|jfBi!6-KkfD!Jy#W-rtXk)L#76aecUeA8&74&yS&!^w*IbfJlF&2Y+0jv#!o2 zK-Q~M??Rrlj+##8)C3h5=v&IGEfwm?`rJ&Rmml{EMYU}~qaOre%GY4m4(oCy$mj6F zeu0o~AbTXP7st+i9GBX60(Po{^HMG_0bI~;6KbIW{Rs5&*9Gv}$o^V>ScmK1Apo#d zuP1qAzk0m0fy;wHIwm}DOal-7NC*_-nAAw>7WNB3;MJWpRyhFbd9(+e%*Rim&1R>1 zTs?5pS zD4(MUHFO*#9p(r%&K;q1e_VvxKKN&iP>-?u{k9dETdrGUui0|lC2PdSj%Kl^al1pj zZsRp#kK5&StadvZ5tLgZu&BIPJjH7iXW`VydOJl#twpz^#Y1r}_9Pca0vMB38M!yMIyywuU8PrcwRBKuo15w( z5vcC+AZA7~&>B^aD#K{YMe~y6Kyzo02wCljwdpdj)7Y#DLK~>Q@)LhXrczcko&O?C z%v!Z;s#5&Vhv9FC_A8N zq3i-bTjOV6@UxA6HliS=w%Lz>BCms89?Cx7KYJ*9vp*@6{Vjz;+1L8bp=>dj7|OoW zk5KlVeuT0EN~AO$v~`R{6;mktnLt7)JE|C=Y?PQo*&_--K>c_q z`y4e`TUdNPZP?%OJOJd%4`0W>|4rgq3ja@EtGwNp04Rb*-L|LS&*F{WRKv6 zjw<#wHTnSe@M!j33PrQ8RdBChOfeco?506Pv!4m@X!cM5l_yzQP+1;~!9qQn9aYTH zZ2gEwvv1?QJ|mhPRZ>K=p9w_hPZ2IR?geYdLcl`6Lcl`6Lcl`6Lcl`cvyOoBy$F%9 zKP5wbCjb9I2>kzBp|t!Tm!ez%Cn5}Whl=x*Dpi4B1G6) zUrd_E0K*8uFtIn7y;Z6|hHb`H>0{dB*mo>4anI(7%KbwZMItOqrqq6;1}~6N0CEzx zRb^i7=xFh_inlqMI@`(VVAr{RQ=6mFEqc6--foa`_WuTB(rQBtTL2#c>X&C}^_vt+TNiS@Ml=SakG>UROIZkx^M~;RnZJ(zla@d?9q?*2azwN1KLG+!$6WtMY|o z*yUs3qO;OS#Ng@d(v>u|bvAt+O7$MYI2AA&;*wRd^*EP2%&PLQ;{%Ht9q-LQwzar5 zri|9EnySQh znsKSNi;r@&tLPYiK+V!}K3>f&VD;^HRt#~*Lw_<5H!L3*tPUBX@ml1a>z8Z?4tz&FM*yL6$97=F2ZU`H0MUKRmvbRICxfQP-(%gzy56$XU^b9fY z5Poa0KXBvhYiscI7Mr|o?o=EJkyA0@sh~O)3x~Kuz(U|}0|Dix)8_xT5dXg>J^x3!&nTZG z{tq1oNr##LDTDpz%k5p&SSu3gmBr6Qtkoh-8^v*d)D`~{9YJL|3_il z#!Tb4wW~w)fbJuCp|!Eii2%RO(Yla#(AjKaSV?3^RX&BEyYoorZ%C)Avi_Kq$vzt7 zx-QEy>vFo>9`CHGl1bMLJ>O2ylcDQbmA$c}&&z+W-{$bRnjIdMc$jFS7r)=R6*>Vi z`AeoW3_+1OYMkV7UJ6s*Q%zZu^ChR&y!OVgLxIMbcR1XgMp}fKAn1C)3Z~=oRjPR*X{;#foNm%>a(-P$;Sd`gi@RZsPL95!H=}73uygm9&uGiC#Ax|&&k-H=d z?h?&a`W}A+iZOI6_tTS8DM>}(`S*`b;8DZ)MgoDq83Z2Xb4%bK3$gVYfmgTr{CJdo z65<5@XhKY1pa6!fxsUPr6KTmFmeL_JrMYb&=@-zG7%6s9J?6Fx1`~sc8FPNRcUp&Mv8Di^(*e~!ca57|H zSEIScIDy~C3A~HkdKgiR#*j+j4@EeEk48{+l5H4LYC+&9LOOvz88Q=i{fHBI{E|SQ z&miz8Ln#RSA76KLm|Ai4yazdvY|351L{(s7!HvW%tpHV(X{2w|F zk`6Qf$GIbP?u_t%w#$cr|6=Ja^__DOdfD92BS8wUqg^A^E;=PY5(MSm62w#i@H|he z%juQutHh?pwx;ej#MwMzXNUfa7(aB8`C!HP zwB}BShx0>SYLd{RxCnC#Su2Pw8YgB$oJ#mp%2Qh=n1y7pB?0_Cfvt>e)g@h=v(bqu z0AhDnb0ZJogB;z_?dWLgOE9`ocRFY}0h*O~8J-lNSK-A$)CE@&Pu5{`b#_SQ8?L$v zrmF!=j~>BPTbhZMwm?>pW5`WhJuk6--DR2p4LdV!S7Y5;Uu@Icl*ZzfdGW5`#8AbN zdRhBTrv*TXNI|CF6$;t8%or@x%c<;Md6q6-vYW(HH>DJ>4<9c6y#(<(lo#gfA}>oP zQioM-a-P=lkf}B^4HTvGai4jj@s}M(GGDfl!KQ zD}Pw%WX=5e09l(C*hK?*ZpqrU0X7OkNbe(5xA}ZK%KkOL$=U+}jjX*KU>^jS5M;SQ z_D=zjwSu5t2fLiC9Sh7JZ7m2UMO&{8P_%VP&>U?Y#VZTV?<1@af~?gCLDn7!B#^ZS z0%^z^9w3~oZ3;xt)-m>Yz(m$|5c`V1kMQw;LDo=WCTsr`00^ibCu=)GNn{PClllA; z45W#+UKL<31eHgEE7`Sy*`lpahqP$x)ASXYeiFx9Ar=j>H$$v3NXFSOuJQCZ99^8M zT@|2MYvQADH%;K!5#m(sn^bX<9SbRlbi+EGsy!YsQ#JjFQ?)AI<1?t*qBf z$RR*!(8e(*iW%&R09Z0kGE&8<9ki^6HtD33nIg0NurWCvn2FpYOoP{sWpB+H<%5hg0A zVbGq&KEtz;(mSvWMx{{4nJGWd`-cw&V*SMK4Z{~EH!Go;kWH!G=nCF;-pNuQo!n{& z(L23j-u2zLdEJe8Ap|#}+XJ_fx2LlTUXjlBt~N(IvamTy6iYUmP>SaN42bQW(I-T% zxf6z~e~&68jInije}p=*y1NS;Uc8N7+WHkba{}epw2Q;b$Fp8SJ&}Y+*q88Nr&Rcj0u}-m0u}-m0)Hb2D2Ejg zlGB?1|LP$4KN9!Stx$(@pHV(X{2w|Fk`6Qf$GIbP?u_t%n&!VGV@I?oWBT5{=%QY5 zi6WV=IF~bjxU2e|;is7*FCMf|=ZMou8Po06#f0xQmdPTYj=sX3ryFO8i9K=JV|+>yddd^F~<z)xP6^*Y$){Nzb_c7F1@oD|GlDpN4?ZP^^meBPhPPZlWPCkqttlN;rP zVCIc-nqcPV6qTP8%MrA7jJah~FmszfA(-iwjbJ89%>3kb86cp3oS$s-Cj~QMI+@Qu z56NkQnTurhfTFmRm29azTQKt>zZT4Vh+cr0KA8EepFQVi&-mFzc-@{`wy&@0#7hO7 z^a{p!CJJXMi)3;Jy*&t)g3-ljJdvA9P`3GbXwykzCfN&qFa1}g+kX3gvd`t9*$0=m2SdIQ&N-Q+cF6~f^89g&XJwqpX>CBV-N|e&j zG?(FOjzvwbv1EbP7PrIWK;FcfwYB1Z+adA|7D z>vwrrVe7S(0MspJ{Ni|&y{E1Oa7bSXUn~w!|%fD(3|{eQTwV- zSqb3Uu0P4u$NW>*h?z`97_;(`TR2<4y=@m+J94FnE_=KD*-$_>q-C% zf|}oDK@cM+z2D{HAiIPI=zolr0LB&j2L8$}bsTFC3c7K*=jEGxRe-;zRRJ)*7>y{_ zPM()T>Z$^hM)@4^f9N1-+eL5@;no78RxEqo0%B1MPe>x`gO1T0TN8 z=kZi5vUd~Sr=P_xmGOfgMU0AQ#XHIX*Dp2nW#UbKc*`G%DrwE&N4oB8t&Fz-ZvuSQ z$9@Yf@lVD=R=mm1GW^PeigF@VRsqLTXSZgA!ERT|yQpb%%Wl0gTZCj?$|_K|`FuNy z`~jX-@KZgj0Kw$*6t+TP7bwg}Sp};Vy$*IcyUkH%&nj50BxMzNc~-%tiaD!bQ6Mp^ zpoy{ynkcK_r-@kwKTVfaaEF#v(1NT2AF>L<=B$Dz60-`z@vH)rnAxq5CsFuJSp`od zXBEJ7GM|6Y@u}IZOU7z+%1g*9@Xnf5Fs^46jL)7`P#j=q2Ut;n?E!TU%l2CRh16nm zH($Q8i`KE^mzdegql|Hf**1w+2Vk5P9O%b^t6I-LRx`-#L4ZK`2d(f zZo{e43ZAgi3jW+*FqS+E0Sf^O0Sf^O0Sf^O0Ske@Dg>10PdoqLNBRFxrO*FIxz8w{ zBmNH^2T6yS|Kr>dI(KIHKVM}&#<~Y5gBep-im{CDcZ=VOu>YYb6-sd5=CVfz*?;$O zuYeFO?Twx<-3#ZyRP=m`PovQjcD7572*u0SZQADSxo_XUKKHAm?@lZCr{d(?MxB$J ztp+E55t;rdpIc6zC$q=3On-Ho&yPo0qsqxQ>YV&O8I+hEkl70|yMZ{lLYe-&4t6;w z-}fn*{z`JDe;()LEFshX@fn%^CuDH)6EZmY#zaoOF&!t*(Kz`>gAufKjNM}9Gx*owIZDfA+J- z{Oo`EnGKx$7TJDjO;6)?zN#70LRwu|*RQjKGxATk_lZ%WKL!`0;lv$Nilj2~tt4fV z{n)SkZzTHPU@-Dq5*WFD#2NXUy!&S`@>^0da>-)ke^uWXR%0v#ECehBECehBECehB zECfE=2q?ckZT^3R_&;9nly3e%%6&%p9PxkXI7m9o{2%9z(7C@7{x3gK{PXX!pD4 zdTgidmDn!Z%dtQ&Y9z(!OSw;AG{Gk@bN2syiqp@hIQ_OnpTM?sK7sQzpTI)I>099w z=ra2RX3YNYiu(j+%>HkcSvT1l_X(t!{SVV&m-=}yoll@xX7?#d3w#2lv-$-7cC6N02v`VM2v`VM2v`VM2v`W5Aq14WDN1-+^Z)Om{QsWx`Tr>Q8Rc`t z|Dod`=`izuoI672&Itdf^!{l|@2BbG?;^#2B6>FE>pvt*`xsi*LM@{t{xLSJ*u+d@ zK6|H=)&cD7ZfnMR?v6&cxaGPv>RftUy_l~Va2}=zQ>Cm~E3M-b@cHV1G#dbwHT{>9 zen$F9+K?u#JUfLnS~XBxdVI@u7q8(GX@UX-jBo?A*$kTGg=WclkW9%8`n6D&C!gn6 zey1qc1X8@OkoT1Cu|OrG@8Gf6gP8>=pIeW`L7BOq6 zYs&l?`gXI_S_oJOSO{1MSO{1MSO{1M%tSzW<+S7fuT%VgD1H1N zE(u_%MyVHrQnw=spxc}Ta8u&MfbMt_07}f1x?Khcs2``)HziLDfazpD|2&*731ErL z{y|aNkOWXRE2Vx|pBV7)>=Oep!~R7-mTF*YK&iXsHEY!*fF8FCi|!?)04(9G`Z%Qk zV01AWPb#avNu3$sA~BQ9=T`)t3JW|e+H}WPL%?1lQlD7=EK}N awh*uoun@2iun@2iun@2iun_nZ1pYrO(txG_ literal 0 HcmV?d00001 diff --git a/.github/actions/oam_bug-2.gb b/.github/actions/oam_bug-2.gb new file mode 100755 index 0000000000000000000000000000000000000000..a3f55af1049fff765424216fed8c6553c2262ecd GIT binary patch literal 65536 zcmeI54|EjOy~k(sX9-IZ5&<0p%7kQ-6^I0=Az(W4CjzA)AO+8>N7+EK7+iNTe@M_I z1_-HbPy6V3wgs(}@{Z5b_s*-Rt(J#RGjn&NYrIwzdFxYro7(U+PdHiaD2cfHe)rDK z{z*2`!+{>``R!pcb7$`F{`tmbmI!6C+%>MYD$5h zE4;`5wrXN+r~oQ}3ZMe004jhApaQ4>Du4>00;m8gfC``jr~oQ}3ZMe004jhApaQ4> zDu4>00;m8ga5)rMkt=k~ylR^C#pEpM^JXG+$wHvWC$|TR_+Jfyo0!XH3$N&kHTe%PyVy=ggk_ZG(!>Ybwq8 zb7s2q1v|`6SLb3yVHA2 zR%y^(6gCSzJ8datyojPu+N(dE0TSfN7XnkEU)Mv6rb27dq@l9KWkMR%BYn1@xmWRV zVJ_&MXPtyhDbi5(;w)QVBqtbsBEB^w_|N3LGO%Xc8=@t8?EiEitUVP72PVp;0YNqe z*5nAmK-lM{`}3jJ$xvV}*8`4ChrUeZdJM`!PqSpIP<*7ZXfC^5{T*;a`uBwg_EiM- z_1w~POGUpR4eaY#aHjOL#dlmU+Ov$ z$x&77tf`h7YU&&6EkZ9lUF)i@-C0}RSmku?k{s^3$m#psbv06TO#>Lz-w5YuRX%m( zgIc+A5bm82n3SC^{~$0~lT&GDp&Vudf->Q=qz?x=`rG~F6YF`QwO@FlrQh_z+xaOk zn0_VX3VxbfXfg}A?O{0R&CVg~^6wZDUT_bYUf4GHfR@X%0(0}?*R{C!?A8Y+0q4#+yn;p6Dl_Y2;oY$b&QaaPAgJnKF>jBv4Mq7~C-EL!< zjjZHb#i!CsFHA2LUN^n*ionOv|LDW#GoIl6iVi5Pg*Fwovg5o#7Z4jy2DC$ zTH!AHG}>Y{Y~V2H)f9TWRq<)fVxP84?A0C+Piqf~?`SRJ+ghu5Li095wq1(&vgTd- zUGWpmTe(L3m@0)@&jQ+M4W5}8vXrNEkLxZa`-?TNn5lW;#DwhXV<(SVvn}+fwYwA= z1FbgCN&8vph23kLA-T)f+ou&2%!EPE+P~Z1(!a+agq!uERZI!P9r}HtFES$6Rn^zm zR13YbKt3uOYWG*z(!Xu{K%B_#P^H^dV_ezocMt9HZ)M)}U{o{kU}1!0!K$vIh1XlQIVO{!vOL&lS>=B9d7&6Z*@B z*5|%Y=L&%pXZmd^W}&%X@SEDk<+uB3ak1$kfsMrCVi@-0V4DbA7`CGL?@#*PEV`zc zK3Pmp@W;(vb>HSKjoU2j*?F#T#A8CU3V64nN{tQ`(VJj+o}NYCnx#SKf+3S%AJxnG z0}EyF(XOYY0x0sA{Dx1+Op}EfS<;1cX^`jmj;o=j&Q;}S6e5NjogJ_X~%8ecI4o(7MCFt)L8^-Mw({I{dSO zO}#$4vzXjv^O~;zBDMKxn|Yuzv|rK`A6;qV-*cXiiecQnTbwWK$rh+&%NKr>9frL; zx&NRaDtOc{pYMOnPk(9?zn?E0FsC)mmM#>g!{ZDR-eb;z2d7qk2sR=4mM)!!FV3~H z0Chl~wZ7c9Y-YRHoRK%_w=-ryIZ8Tw-)r<2#q!Sriy_A{`7ePLfylBpzm##*WlYeDSFt><;!zq^9yE}zxWz)*tj+=$d%uJpydm7kH%NBN& z!@hinAUG<*6%M#0ifvG!{wXcB!$DglB?xxAkYdh+0v8Jv4%beHqe2jwz1Rk6cA-sZ z%M>%sa6WTdg|iLnYO}YPS-Qe*Z(;e{Shmtq$nRkP*j_egma}@KGy8xY*z5pX=qmfP3Ek&Hr@Fhl1;@^|HZWxEg-->q4>IudPG=cU zLzS%l3RVt?$Udwd`0)B16&0cYWt|G0gYp~|tm*7JYl5x|)OS5MyIxq#dT&k**FdFAoZ2X{HCH*}#MQ$P~ziG^<9| z_t1X<6gbtw#wr&;KM(DJo6N^gwkVb|J6=8LvO^Rzp;?g6{>dSI=)$oez!wbr&J;sF z-{trpE|!T3paQ4>Du4>00;m8gfC``jr~oQ}3ZMe004jhAd~*~a|5xzMY3yZaDVaqE z7GLuQpJy2I$-4uKeKfTT{D+G*c|Dz@$Q~-ew3D3KmoJ2xDrWbz+6H^>vuV9>3eLRP z#ZR4wQ-6I*Kc%&vnJ8KQU`wS$X{8@4LG49bM%$@OI20%J_0b-sqy#1qdxPBa1XxnY z36m-fsyI94dyRFjTGuvd^|DII?UL#ns_Gi*r3SZKa#l6eNVRZkYn97c%O^2wf0VRpsTBq>f%rO;sICz$PzfeWV~06ht0T$yt(Q))MOawq(p&f=f^o0PF;EdwMnYZ)N$)QmoBInF~fJk;!=KY$_Hqs%RFZmr&2zst3iPh2_` zHgQ>@!2CXpD9XkV{UJPt#XKV#yuM~R=fG*W!xat*)d!zM0! zlq3_EM~IJoh;Ykg_=3SAQ2|r{6+i`00aO4LKm||%RN#tHfHZo>;Qw7J@c(x8BKbdy zw9+yX<^M9m`F{!-$^Tizm(dZ1&{ZN{owB-8s=Re};qqHcOQl89cWzq-Nr3$&YOWBP zMDGH8V%bs?vsK<2pKWS1TUs>R^5w%y3&)j~4%wD2OQ>||GG3`k2&G0#GefqOs}r)V zT+OqAFQg!vZ5(9Vur49nhIKsKTp=`npu;_|Z`)urcZQL>k~eYP+I7$rJ$Lv^WmxWO z#v8eHdDd;*urium2ziHP&$A@97*b1S+g7fgb%W9BnZqq6B)2GuS}clLkoO5VVQ6v0 z4`ZU8s(clNQF9F+HSF4=u2(ZXK5~;ci6;d{R?L$ERuKQq&TvmklM3O zq$nzn|0eOCl=a?3Ps&lk;=c)t|5g(`DMRWgo)maG^!RTT;=c*Rc#}9!O1~$;lQM-w zJt=S{-jk9=Km@Rlc~bg4BRnb4b=G~p!IPfclk%KOOFSgg1M%PLSe_J!de-B=ZR{15 z(LE_^y>y+I-sYtzc<1JoY^`dnuc_Aqz*QaquCn(eRQ8?(xOuoArQgH-C|`KMFi3Cr zk_}!604IhYWeSP+qZlbY0G!|PVSbb;B#9rT--CXXE9Ms)HUt$w1yBK002M$5PytjR zsRHC5#^V3G82{gPvHTwdgv*QZf1b|&gX&2B52`xV~R$5y$+Z5&o80Mm29txH#G%Z?K)C*9#{8n&1M7iFo&JD%5KGKoAs24!5 zT8Hor>t@a61%uzSjGe>-D*7#Y{_k)bobIzW8nx@3$GqbJ7rV0DsNMHv517$9tP z8Dt;ZUqZ%zLZu5MjIZx4Jl#t}Drfxn4aT<;x{A>65&C^XVL)os&KRHDz#(V+vaeu# z`$)!rf;0a11jb)DjPV~~jQ`~$%FL3k7f)i}m~Gm-HJ)hOe` zm3YPvsvrW`#~A<3k&F*rXWi!;Jk7}&|526ZddL8oNuL;#@pBBu&l#KX7kFv8mzH_y zAAs=()%hhZw`+48=Re9hA4X!BjYP^ukYV?92aJPsk(b;IoF7yT&L33cIp0We z&VP{K^jFe`gZ@uU6TjN<_4#=wvQ!Z>(>Sw$#8fX*H6gwhj_o-F0=1I~r=; zE&$?xR)Dni*nbl}tpJZgOY>MuC((a};BhEwT56=Is)ny!!c@J4-|HnzO^cP_tom9_ zC`l-wAU(b}RSsA&3f^M)S_R!`^o86Mp>JOVw6~JT(W`Joe(p&!tqwB8kQ;;(mG z1BV>BfBO|LD7-cD1qG2K_vwTe6rLXTg2Fh4+~XK>uTMno^`jv-j46)X69BoZ0lA&= z$h|5Nxt%fOhAZ*NU9Ex$U>`&7RU?rby3V@KH+X=OBX^ccI|$hd$SsbE+z)#q)9Md< z#++7v%0rKO=ua{UUDfdm4|Kxo z+8dcLM&-8jk!3_G=ft1TVokn__+-na^ipND~R7alK3s0`2UnZ{D+1S{~%kEV2~|Iuq=`ImyJgJFsL~3e+f$x zYy#rHJD&I#BohDKG2(|S@x;GL1rfkLM*Is#56!z~rPyti`6+i`00aO4LKn1>$3Xt>a z81MgI=mP#f+jWui|NDVFkHyyiJEpJycOc37fBS*3c#uDRvP)-Ox~9wY-HzOC+4O`; zs#svXG2K6&(J?gaYSyqKi+TSfdZRK`lW%5e`0GSw3;f}(`AT-?q^zmZ7wOWc@RwDr zlHW&OO179E*u=;gN@1>9Hi}|%0 zi}`sJ`jJ9ERp_8XK`)Jl6~hJ&IXS`XZ+rqdze^HdL7&gac|IfO zRu(!C`x9iXqb=sQB(j*_0=TdH!98;zelfp>#Krs$#1`{|EAiyKUjY%oK1R+BBNy|7 zuCwm*4F+3sa$cy=qbm7FSj?|-%*FhkF&6WCX6(iM-XZkA3H8I<0^k-npp+Tp>~crp zc_D{q7_4D7SScGw`b>EP;cuevWA;J%9wDcR2Awn_)DI-A=x3xjJm;zG#t(z%1IbqO zYrqx#zLDR?*l1J$6+i`00aO4LKm||%RN(R|K+cTC|NV^r-@RD=Zv*~+GRFT;>iplC zg#X*%PrnNqJ<=8IHia4_&D6Okckc<9BPMIriWT07Z@?4r4Jfuu`jSH16mSy2PySa6 znxUDRypT1PO_=-0zbB)8xEiB<=-f$`KSKK-0`0S|8jK&?UqafSr%+#n_VvAmr+ev- zb=tqzp#5hQ`dZk`3l-UJCgS2aoYb}iC=KeH;nf48SUpY z+P^oE_U|2y_UA-sAN~M3w6zanGU8rF-;zlCoiW;nEAg}sxAVuWuQA%+GLrV8>#X~H zgQ1n2_KOvINF}X6`}4-6{lfh+Q8%Di#hFo z-UYM|!-}$jq|^Quo%ZXQeUQFNNDt6{Ta@-Y6KLN^aoW#N*^M7Y`<=;Xe+$z7<^7Vx z(oq3a02M$5Pyti`6+i`0fxi<4u9*0LzWzT%TwlkQ0GPwp@vGY*?Wk&im*!m(tOVFp z=XPy_fd4)0)&EKK5Zg_Ko$1q@+@HTge+ePXV0~>WTV`)G%ee!g=z98K`FMTqo?OE+ zFO4syKZa>^ZGABP#p>K#Vwg^6%LeMp>dz^;A$gn8ogvN_s$o=e$?3i zYkADR9t&YJGe-x+k?&D_q~l7B|34gw|JU~x%JyG3{-4`i;`o1vN%xAGnm1%ApO9UB z?Bpem|6e$2{6AcY_iK!d|6h2SKp1d$GJ5| zeFI>j{szD*W*(%!@sJ~z{tbYI_y)k=$uAk~1}cCGpaQ4>Du4>00;m8gfC``jr~oQ} z3ZMe004jhApaQ4>Du4>00;m8gfC``jr~oQ}3ZMe004jhApaQ4>Du4>00;m8gfC``j zr~oQ}3ZMe004jhApaQ4>Du4>00;m8gfC``jr~oQ}3ZMe004jhApaQ4>Du4>00;oV# Gf&T}g$kes~ literal 0 HcmV?d00001 diff --git a/.github/actions/sanity_tests.sh b/.github/actions/sanity_tests.sh new file mode 100755 index 0000000..26fb398 --- /dev/null +++ b/.github/actions/sanity_tests.sh @@ -0,0 +1,29 @@ +./build/bin/tester/sameboy_tester --jobs 5 \ + --length 40 .github/actions/cgb_sound.gb \ + --length 10 .github/actions/cgb-acid2.gbc \ + --length 10 .github/actions/dmg-acid2.gb \ +--dmg --length 40 .github/actions/dmg_sound-2.gb \ +--dmg --length 20 .github/actions/oam_bug-2.gb + +mv .github/actions/dmg{,-mode}-acid2.bmp + +./build/bin/tester/sameboy_tester \ +--dmg --length 10 .github/actions/dmg-acid2.gb + +FAILED_TESTS=` +shasum .github/actions/*.bmp | grep -E -v \(\ +44ce0c7d49254df0637849c9155080ac7dc3ef3d\ \ .github/actions/cgb-acid2.bmp\|\ +dbcc438dcea13b5d1b80c5cd06bda2592cc5d9e0\ \ .github/actions/cgb_sound.bmp\|\ +0caadf9634e40247ae9c15ff71992e8f77bbf89e\ \ .github/actions/dmg-acid2.bmp\|\ +c50daed36c57a8170ff362042694786676350997\ \ .github/actions/dmg-mode-acid2.bmp\|\ +c9e944b7e01078bdeba1819bc2fa9372b111f52d\ \ .github/actions/dmg_sound-2.bmp\|\ +f0172cc91867d3343fbd113a2bb98100074be0de\ \ .github/actions/oam_bug-2.bmp\ +\)` + +if [ -n "$FAILED_TESTS" ] ; then + echo "Failed the following tests:" + echo $FAILED_TESTS | tr " " "\n" | grep -o -E "[^/]+\.bmp" | sed s/.bmp// | sort + exit 1 +fi + +echo Passed all tests \ No newline at end of file diff --git a/.github/workflows/buildability.yml b/.github/workflows/sanity.yml similarity index 73% rename from .github/workflows/buildability.yml rename to .github/workflows/sanity.yml index 7cd5298..e0ac132 100644 --- a/.github/workflows/buildability.yml +++ b/.github/workflows/sanity.yml @@ -1,4 +1,4 @@ -name: "Bulidability" +name: "Bulidability and Sanity" on: push jobs: @@ -23,7 +23,11 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - ${{ matrix.cc }} -v; make sdl libretro ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; make sdl tester libretro ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + - name: Sanity tests + shell: bash + run: | + ./.github/actions/sanity_tests.sh - name: Upload binaries uses: actions/upload-artifact@v1 with: From e819b91a97834e8af12a7ce378cb9db1e0d441e6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:03:45 +0300 Subject: [PATCH 231/341] Rename job, temporarily disable -j --- .github/workflows/sanity.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index e0ac132..41404a0 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -2,7 +2,7 @@ name: "Bulidability and Sanity" on: push jobs: - buildability: + sanity: strategy: matrix: os: [ubuntu-latest, ubuntu-16.04, macos-latest] @@ -23,7 +23,7 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - ${{ matrix.cc }} -v; make sdl tester libretro ${{ matrix.extra_target }} -j CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; make sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }} - name: Sanity tests shell: bash run: | From a35164dc0a0ccb4b9dad372fa704abac430ab03b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:06:24 +0300 Subject: [PATCH 232/341] Fixed unused variable on Linux --- Tester/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tester/main.c b/Tester/main.c index 27250a6..f399f3f 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -178,8 +178,7 @@ static const char *executable_folder(void) _NSGetExecutablePath(&path[0], &length); #else #ifdef __linux__ - ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1); - assert (length != -1); + assert (readlink("/proc/self/exe", &path[0], sizeof(path) - 1) != -1); #else #ifdef _WIN32 HMODULE hModule = GetModuleHandle(NULL); From 7760e11544627da2970b0bfe84e6304a5bd89736 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:12:53 +0300 Subject: [PATCH 233/341] Better error handling --- .github/actions/sanity_tests.sh | 2 ++ Tester/main.c | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/actions/sanity_tests.sh b/.github/actions/sanity_tests.sh index 26fb398..8d37b68 100755 --- a/.github/actions/sanity_tests.sh +++ b/.github/actions/sanity_tests.sh @@ -1,3 +1,5 @@ +set -e + ./build/bin/tester/sameboy_tester --jobs 5 \ --length 40 .github/actions/cgb_sound.gb \ --length 10 .github/actions/cgb-acid2.gbc \ diff --git a/Tester/main.c b/Tester/main.c index f399f3f..e2e1aa8 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -322,15 +322,15 @@ int main(int argc, char **argv) if (dmg) { GB_init(&gb, GB_MODEL_DMG_B); - if (GB_load_boot_rom(&gb, boot_rom_path? boot_rom_path : executable_relative_path("dmg_boot.bin"))) { - perror("Failed to load boot ROM"); + if (GB_load_boot_rom(&gb, boot_rom_path ?: executable_relative_path("dmg_boot.bin"))) { + fprintf(stderr, "Failed to load boot ROM from '%s'\n", boot_rom_path ?: executable_relative_path("dmg_boot.bin")); exit(1); } } else { GB_init(&gb, GB_MODEL_CGB_E); - if (GB_load_boot_rom(&gb, boot_rom_path? boot_rom_path : executable_relative_path("cgb_boot.bin"))) { - perror("Failed to load boot ROM"); + if (GB_load_boot_rom(&gb, boot_rom_path ?: executable_relative_path("cgb_boot.bin"))) { + fprintf(stderr, "Failed to load boot ROM from '%s'\n", boot_rom_path ?: executable_relative_path("cgb_boot.bin")); exit(1); } } From aa9ccc724fb9de220b87a96888bb3db02438aec9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:20:06 +0300 Subject: [PATCH 234/341] Fixing a duh --- Tester/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tester/main.c b/Tester/main.c index e2e1aa8..6c175c6 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -174,11 +174,12 @@ static const char *executable_folder(void) } /* Ugly unportable code! :( */ #ifdef __APPLE__ - unsigned int length = sizeof(path) - 1; + size_t length = sizeof(path) - 1; _NSGetExecutablePath(&path[0], &length); #else #ifdef __linux__ - assert (readlink("/proc/self/exe", &path[0], sizeof(path) - 1) != -1); + size_t __attribute__((unused)) length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1); + assert(length != -1); #else #ifdef _WIN32 HMODULE hModule = GetModuleHandle(NULL); From 09e706865835b85bd34b2462225214e1308aa532 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:22:43 +0300 Subject: [PATCH 235/341] Fixing another duh --- Tester/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tester/main.c b/Tester/main.c index 6c175c6..16dbf7b 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -174,7 +174,7 @@ static const char *executable_folder(void) } /* Ugly unportable code! :( */ #ifdef __APPLE__ - size_t length = sizeof(path) - 1; + uint32_t length = sizeof(path) - 1; _NSGetExecutablePath(&path[0], &length); #else #ifdef __linux__ From 65fb6afd60bc2fe5f7b2190ab2bcd8975f2f322a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 17:57:19 +0300 Subject: [PATCH 236/341] Make fixes --- Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 8f8ea81..49e24b5 100644 --- a/Makefile +++ b/Makefile @@ -342,7 +342,11 @@ $(BIN)/tester/sameboy_tester.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) -Wl,/subsystem:console -$(BIN)/SDL/%.bin $(BIN)/tester/%.bin: $(BOOTROMS_DIR)/%.bin +$(BIN)/SDL/%.bin: $(BOOTROMS_DIR)/%.bin + -@$(MKDIR) -p $(dir $@) + cp -f $^ $@ + +$(BIN)/tester/%.bin: $(BOOTROMS_DIR)/%.bin -@$(MKDIR) -p $(dir $@) cp -f $^ $@ @@ -397,4 +401,4 @@ libretro: clean: rm -rf build -.PHONY: libretro +.PHONY: libretro tester From 9fbafab67f9d38726763d5a29bf446aaca00fc87 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 18:04:27 +0300 Subject: [PATCH 237/341] Use grep -q, put macOS first, restore -j --- .github/actions/sanity_tests.sh | 4 ++-- .github/workflows/sanity.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/actions/sanity_tests.sh b/.github/actions/sanity_tests.sh index 8d37b68..38302d7 100755 --- a/.github/actions/sanity_tests.sh +++ b/.github/actions/sanity_tests.sh @@ -13,7 +13,7 @@ mv .github/actions/dmg{,-mode}-acid2.bmp --dmg --length 10 .github/actions/dmg-acid2.gb FAILED_TESTS=` -shasum .github/actions/*.bmp | grep -E -v \(\ +shasum .github/actions/*.bmp | grep -q -E -v \(\ 44ce0c7d49254df0637849c9155080ac7dc3ef3d\ \ .github/actions/cgb-acid2.bmp\|\ dbcc438dcea13b5d1b80c5cd06bda2592cc5d9e0\ \ .github/actions/cgb_sound.bmp\|\ 0caadf9634e40247ae9c15ff71992e8f77bbf89e\ \ .github/actions/dmg-acid2.bmp\|\ @@ -24,7 +24,7 @@ f0172cc91867d3343fbd113a2bb98100074be0de\ \ .github/actions/oam_bug-2.bmp\ if [ -n "$FAILED_TESTS" ] ; then echo "Failed the following tests:" - echo $FAILED_TESTS | tr " " "\n" | grep -o -E "[^/]+\.bmp" | sed s/.bmp// | sort + echo $FAILED_TESTS | tr " " "\n" | grep -q -o -E "[^/]+\.bmp" | sed s/.bmp// | sort exit 1 fi diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index 41404a0..ade68d0 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -5,7 +5,7 @@ jobs: sanity: strategy: matrix: - os: [ubuntu-latest, ubuntu-16.04, macos-latest] + os: [macos-latest, ubuntu-latest, ubuntu-16.04] cc: [gcc, clang] include: - os: macos-latest From f65dc736321cb51232ee69d3d0be51917221c59e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 18:09:04 +0300 Subject: [PATCH 238/341] -q was not enough --- .github/workflows/sanity.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index ade68d0..653e05d 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -23,7 +23,7 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - ${{ matrix.cc }} -v; make sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; make -j sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }} - name: Sanity tests shell: bash run: | From 36aa3f31b947ca00d2682df7f8a01aa66ccde562 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 18:11:01 +0300 Subject: [PATCH 239/341] -q was not enough --- .github/actions/sanity_tests.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/actions/sanity_tests.sh b/.github/actions/sanity_tests.sh index 38302d7..8984b26 100755 --- a/.github/actions/sanity_tests.sh +++ b/.github/actions/sanity_tests.sh @@ -12,6 +12,8 @@ mv .github/actions/dmg{,-mode}-acid2.bmp ./build/bin/tester/sameboy_tester \ --dmg --length 10 .github/actions/dmg-acid2.gb +set +e + FAILED_TESTS=` shasum .github/actions/*.bmp | grep -q -E -v \(\ 44ce0c7d49254df0637849c9155080ac7dc3ef3d\ \ .github/actions/cgb-acid2.bmp\|\ From 152924e13fe851c0824579989b8f59d4dbd5358f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 22:48:48 +0300 Subject: [PATCH 240/341] Add support to the ISX format, including symbols --- Cocoa/Document.m | 9 +- Cocoa/Info.plist | 47 +++++++- Core/debugger.c | 22 ++-- Core/debugger.h | 3 + Core/gb.c | 213 ++++++++++++++++++++++++++++++++++ Core/gb.h | 3 +- OpenDialog/cocoa.m | 2 +- OpenDialog/gtk.c | 1 + OpenDialog/windows.c | 2 +- QuickLook/Info.plist | 1 + QuickLook/get_image_for_rom.c | 18 ++- SDL/main.c | 18 ++- 12 files changed, 320 insertions(+), 19 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ed3eaaf..ed52efb 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -647,11 +647,16 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) - (void) loadROM { NSString *rom_warnings = [self captureOutputForBlock:^{ - GB_load_rom(&gb, [self.fileName UTF8String]); + GB_debugger_clear_symbols(&gb); + if ([[self.fileType pathExtension] isEqualToString:@"isx"]) { + GB_load_isx(&gb, [self.fileName UTF8String]); + } + else { + GB_load_rom(&gb, [self.fileName UTF8String]); + } GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_load_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); [self.cheatWindowController cheatsUpdated]; - GB_debugger_clear_symbols(&gb); GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); }]; diff --git a/Cocoa/Info.plist b/Cocoa/Info.plist index 1c7bdb5..44a21f0 100644 --- a/Cocoa/Info.plist +++ b/Cocoa/Info.plist @@ -16,7 +16,7 @@ CFBundleTypeIconFile Cartridge CFBundleTypeName - GameBoy Game + Game Boy Game CFBundleTypeRole Viewer LSItemContentTypes @@ -36,7 +36,7 @@ CFBundleTypeIconFile ColorCartridge CFBundleTypeName - GameBoy Color Game + Game Boy Color Game CFBundleTypeRole Viewer LSItemContentTypes @@ -48,6 +48,26 @@ NSDocumentClass Document + + CFBundleTypeExtensions + + gbc + + CFBundleTypeIconFile + ColorCartridge + CFBundleTypeName + Game Boy ISX File + CFBundleTypeRole + Viewer + LSItemContentTypes + + com.github.liji32.sameboy.isx + + LSTypeIsPackage + 0 + NSDocumentClass + Document + CFBundleExecutable SameBoy @@ -85,7 +105,7 @@ public.data UTTypeDescription - GameBoy Game + Game Boy Game UTTypeIconFile Cartridge UTTypeIdentifier @@ -104,7 +124,7 @@ public.data UTTypeDescription - GameBoy Color Game + Game Boy Color Game UTTypeIconFile ColorCartridge UTTypeIdentifier @@ -117,6 +137,25 @@ + + UTTypeConformsTo + + public.data + + UTTypeDescription + Game Boy ISX File + UTTypeIconFile + ColorCartridge + UTTypeIdentifier + com.github.liji32.sameboy.isx + UTTypeTagSpecification + + public.filename-extension + + isx + + + NSCameraUsageDescription SameBoy needs to access your camera to emulate the Game Boy Camera diff --git a/Core/debugger.c b/Core/debugger.c index ee27e88..caf0af1 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -2160,6 +2160,19 @@ void GB_debugger_handle_async_commands(GB_gameboy_t *gb) } } +void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol) +{ + bank &= 0x1FF; + + if (!gb->bank_symbols[bank]) { + gb->bank_symbols[bank] = GB_map_alloc(); + } + GB_bank_symbol_t *allocated_symbol = GB_map_add_symbol(gb->bank_symbols[bank], address, symbol); + if (allocated_symbol) { + GB_reversed_map_add_symbol(&gb->reversed_symbol_map, bank, allocated_symbol); + } +} + void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path) { FILE *f = fopen(path, "r"); @@ -2182,14 +2195,7 @@ void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path) char symbol[length]; if (sscanf(line, "%x:%x %s", &bank, &address, symbol) == 3) { - bank &= 0x1FF; - if (!gb->bank_symbols[bank]) { - gb->bank_symbols[bank] = GB_map_alloc(); - } - GB_bank_symbol_t *allocated_symbol = GB_map_add_symbol(gb->bank_symbols[bank], address, symbol); - if (allocated_symbol) { - GB_reversed_map_add_symbol(&gb->reversed_symbol_map, bank, allocated_symbol); - } + GB_debugger_add_symbol(gb, bank, address, symbol); } } free(line); diff --git a/Core/debugger.h b/Core/debugger.h index 4868df3..b6a12d9 100644 --- a/Core/debugger.h +++ b/Core/debugger.h @@ -14,6 +14,8 @@ #define GB_debugger_call_hook(gb, addr) (void)addr #define GB_debugger_test_write_watchpoint(gb, addr, value) ((void)addr, (void)value) #define GB_debugger_test_read_watchpoint(gb, addr) (void)addr +#define GB_debugger_add_symbol(gb, bank, address, symbol) ((void)bank, (void)address, (void)symbol) + #else void GB_debugger_run(GB_gameboy_t *gb); void GB_debugger_handle_async_commands(GB_gameboy_t *gb); @@ -22,6 +24,7 @@ void GB_debugger_ret_hook(GB_gameboy_t *gb); void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value); void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr); const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr); +void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol); #endif /* GB_DISABLE_DEBUGGER */ #endif diff --git a/Core/gb.c b/Core/gb.c index 2b22f9d..f1eae90 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -296,6 +296,219 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) return 0; } +int GB_load_isx(GB_gameboy_t *gb, const char *path) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + GB_log(gb, "Could not open ISX file: %s.\n", strerror(errno)); + return errno; + } + char magic[4]; +#define READ(x) if (fread(&x, sizeof(x), 1, f) != 1) goto error + fread(magic, 1, sizeof(magic), f); + + bool extended = *(uint32_t *)&magic == htonl('ISX '); + + fseek(f, extended? 0x20 : 0, SEEK_SET); + + + uint8_t *old_rom = gb->rom; + uint32_t old_size = gb->rom_size; + gb->rom = NULL; + gb->rom_size = 0; + + while (true) { + uint8_t record_type = 0; + if (fread(&record_type, sizeof(record_type), 1, f) != 1) break; + switch (record_type) { + case 0x01: { // Binary + uint16_t bank; + uint16_t address; + uint16_t length; + uint8_t byte; + READ(byte); + bank = byte; + if (byte >= 0x80) { + READ(byte); + bank |= byte << 8; + } + + READ(address); +#ifdef GB_BIG_ENDIAN + address = __builtin_bswap16(address); +#endif + address &= 0x3FFF; + + READ(length); +#ifdef GB_BIG_ENDIAN + length = __builtin_bswap16(length); +#endif + + size_t needed_size = bank * 0x4000 + address + length; + if (needed_size > 1024 * 1024 * 32) + goto error; + + if (gb->rom_size < needed_size) { + gb->rom = realloc(gb->rom, needed_size); + memset(gb->rom + gb->rom_size, 0, needed_size - gb->rom_size); + gb->rom_size = needed_size; + } + + if (fread(gb->rom + (bank * 0x4000 + address), length, 1, f) != 1) + goto error; + + break; + } + + case 0x11: { // Extended Binary + uint32_t address; + uint32_t length; + + READ(address); +#ifdef GB_BIG_ENDIAN + address = __builtin_bswap32(address); +#endif + + READ(length); +#ifdef GB_BIG_ENDIAN + length = __builtin_bswap32(length); +#endif + size_t needed_size = address + length; + if (needed_size > 1024 * 1024 * 32) + goto error; + if (gb->rom_size < needed_size) { + gb->rom = realloc(gb->rom, needed_size); + memset(gb->rom + gb->rom_size, 0, needed_size - gb->rom_size); + gb->rom_size = needed_size; + } + + if (fread(gb->rom + address, length, 1, f) != 1) + goto error; + + break; + } + + case 0x04: { // Symbol + uint16_t count; + uint8_t length; + char name[257]; + uint8_t flag; + uint16_t bank; + uint16_t address; + uint8_t byte; + READ(count); +#ifdef GB_BIG_ENDIAN + count = __builtin_bswap16(count); +#endif + while (count--) { + READ(length); + if (fread(name, length, 1, f) != 1) + goto error; + name[length] = 0; + READ(flag); // unused + + READ(byte); + bank = byte; + if (byte >= 0x80) { + READ(byte); + bank |= byte << 8; + } + + READ(address); +#ifdef GB_BIG_ENDIAN + address = __builtin_bswap16(address); +#endif + GB_debugger_add_symbol(gb, bank, address, name); + } + break; + } + + case 0x14: { // Extended Binary + uint16_t count; + uint8_t length; + char name[257]; + uint8_t flag; + uint32_t address; + READ(count); +#ifdef GB_BIG_ENDIAN + count = __builtin_bswap16(count); +#endif + while (count--) { + READ(length); + if (fread(name, length + 1, 1, f) != 1) + goto error; + name[length] = 0; + READ(flag); // unused + + READ(address); +#ifdef GB_BIG_ENDIAN + address = __builtin_bswap32(address); +#endif + // TODO: How to convert 32-bit addresses to Bank:Address? Needs to tell RAM and ROM apart + } + break; + } + + default: + goto done; + } + } +done:; +#undef READ + if (gb->rom_size == 0) goto error; + + size_t needed_size = (gb->rom_size + 0x3FFF) & ~0x3FFF; /* Round to bank */ + + /* And then round to a power of two */ + while (needed_size & (needed_size - 1)) { + /* I promise this works. */ + needed_size |= needed_size >> 1; + needed_size++; + } + + if (needed_size < 0x8000) { + needed_size = 0x8000; + } + + if (gb->rom_size < needed_size) { + gb->rom = realloc(gb->rom, needed_size); + memset(gb->rom + gb->rom_size, 0, needed_size - gb->rom_size); + gb->rom_size = needed_size; + } + + GB_configure_cart(gb); + + // Fix a common wrong MBC error + if (gb->rom[0x147] == 3) { // MBC1 + RAM + Battery + if (gb->rom_size == 64 * 0x4000) { + for (unsigned i = 63 * 0x4000; i < 64 * 0x4000; i++) { + if (gb->rom[i]) { + gb->rom[0x147] = 0x10; // MBC3 + RTC + RAM + Battery + GB_configure_cart(gb); + gb->rom[0x147] = 0x3; + GB_log(gb, "ROM uses MBC1 but appears to use all 64 banks, assuming MBC3\n"); + break; + } + } + } + } + + if (old_rom) { + free(old_rom); + } + + return 0; +error: + GB_log(gb, "Invalid or unsupported ISX file.\n"); + if (gb->rom) { + free(gb->rom); + gb->rom = old_rom; + gb->rom_size = old_size; + } + fclose(f); + return -1; +} + void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size) { gb->rom_size = (size + 0x3fff) & ~0x3fff; diff --git a/Core/gb.h b/Core/gb.h index 703a489..a83bb09 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -721,7 +721,8 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path); void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size); int GB_load_rom(GB_gameboy_t *gb, const char *path); void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size); - +int GB_load_isx(GB_gameboy_t *gb, const char *path); + int GB_save_battery_size(GB_gameboy_t *gb); int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size); int GB_save_battery(GB_gameboy_t *gb, const char *path); diff --git a/OpenDialog/cocoa.m b/OpenDialog/cocoa.m index 29a722c..76b9606 100644 --- a/OpenDialog/cocoa.m +++ b/OpenDialog/cocoa.m @@ -8,7 +8,7 @@ char *do_open_rom_dialog(void) NSWindow *key = [NSApp keyWindow]; NSOpenPanel *dialog = [NSOpenPanel openPanel]; dialog.title = @"Open ROM"; - dialog.allowedFileTypes = @[@"gb", @"gbc", @"sgb"]; + dialog.allowedFileTypes = @[@"gb", @"gbc", @"sgb", @"isx"]; [dialog runModal]; [key makeKeyAndOrderFront:nil]; NSString *ret = [[[dialog URLs] firstObject] path]; diff --git a/OpenDialog/gtk.c b/OpenDialog/gtk.c index d9163fc..5b1caa3 100644 --- a/OpenDialog/gtk.c +++ b/OpenDialog/gtk.c @@ -82,6 +82,7 @@ char *do_open_rom_dialog(void) gtk_file_filter_add_pattern(filter, "*.gb"); gtk_file_filter_add_pattern(filter, "*.gbc"); gtk_file_filter_add_pattern(filter, "*.sgb"); + gtk_file_filter_add_pattern(filter, "*.isx"); gtk_file_filter_set_name(filter, "Game Boy ROMs"); gtk_file_chooser_add_filter(dialog, filter); diff --git a/OpenDialog/windows.c b/OpenDialog/windows.c index 6bf9b89..52e281d 100644 --- a/OpenDialog/windows.c +++ b/OpenDialog/windows.c @@ -10,7 +10,7 @@ char *do_open_rom_dialog(void) dialog.lStructSize = sizeof(dialog); dialog.lpstrFile = filename; dialog.nMaxFile = sizeof(filename); - dialog.lpstrFilter = L"Game Boy ROMs\0*.gb;*.gbc;*.sgb\0All files\0*.*\0\0"; + dialog.lpstrFilter = L"Game Boy ROMs\0*.gb;*.gbc;*.sgb;*.isx\0All files\0*.*\0\0"; dialog.nFilterIndex = 1; dialog.lpstrFileTitle = NULL; dialog.nMaxFileTitle = 0; diff --git a/QuickLook/Info.plist b/QuickLook/Info.plist index 2cff196..b01aae1 100644 --- a/QuickLook/Info.plist +++ b/QuickLook/Info.plist @@ -13,6 +13,7 @@ com.github.liji32.sameboy.gb com.github.liji32.sameboy.gbc + com.github.liji32.sameboy.isx diff --git a/QuickLook/get_image_for_rom.c b/QuickLook/get_image_for_rom.c index 3950dac..b9f87ed 100755 --- a/QuickLook/get_image_for_rom.c +++ b/QuickLook/get_image_for_rom.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "get_image_for_rom.h" @@ -60,7 +61,22 @@ int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *out GB_set_log_callback(&gb, log_callback); GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); - if (GB_load_rom(&gb, filename)) { + size_t length = strlen(filename); + char extension[4] = {0,}; + if (length > 4) { + if (filename[length - 4] == '.') { + extension[0] = tolower(filename[length - 3]); + extension[1] = tolower(filename[length - 2]); + extension[2] = tolower(filename[length - 1]); + } + } + if (strcmp(extension, "isx") == 0) { + if (GB_load_isx(&gb, filename)) { + GB_free(&gb); + return 1; + } + } + else if (GB_load_rom(&gb, filename)) { GB_free(&gb); return 1; } diff --git a/SDL/main.c b/SDL/main.c index 4ce2e59..e09630b 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -526,8 +527,23 @@ restart: } bool error = false; + GB_debugger_clear_symbols(&gb); start_capturing_logs(); - error = GB_load_rom(&gb, filename); + size_t length = strlen(filename); + char extension[4] = {0,}; + if (length > 4) { + if (filename[length - 4] == '.') { + extension[0] = tolower(filename[length - 3]); + extension[1] = tolower(filename[length - 2]); + extension[2] = tolower(filename[length - 1]); + } + } + if (strcmp(extension, "isx") == 0) { + error = GB_load_isx(&gb, filename); + } + else { + GB_load_rom(&gb, filename); + } end_capturing_logs(true, error); size_t path_length = strlen(filename); From ca567bee7913bb588cadd5b6f41a250e89b0389c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 22:54:50 +0300 Subject: [PATCH 241/341] Fix Linux build break --- Core/gb.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Core/gb.c b/Core/gb.c index f1eae90..701a48a 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -307,7 +307,11 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) #define READ(x) if (fread(&x, sizeof(x), 1, f) != 1) goto error fread(magic, 1, sizeof(magic), f); - bool extended = *(uint32_t *)&magic == htonl('ISX '); +#ifdef GB_BIG_ENDIAN + bool extended = *(uint32_t *)&magic == 'ISX '; +#else + bool extended = *(uint32_t *)&magic == __builtin_bswap32('ISX '); +#endif fseek(f, extended? 0x20 : 0, SEEK_SET); From 9e99ce434e9903d7c1db0747a344059768505814 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 23:09:08 +0300 Subject: [PATCH 242/341] Allow loading .RAM files --- Cocoa/Document.m | 2 ++ SDL/main.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ed52efb..6eb9c2f 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -650,6 +650,8 @@ static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) GB_debugger_clear_symbols(&gb); if ([[self.fileType pathExtension] isEqualToString:@"isx"]) { GB_load_isx(&gb, [self.fileName UTF8String]); + GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"ram"] UTF8String]); + } else { GB_load_rom(&gb, [self.fileName UTF8String]); diff --git a/SDL/main.c b/SDL/main.c index e09630b..9a358d0 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -540,6 +540,11 @@ restart: } if (strcmp(extension, "isx") == 0) { error = GB_load_isx(&gb, filename); + /* Try loading .ram file if available */ + char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */ + replace_extension(filename, path_length, battery_save_path, ".ram"); + battery_save_path_ptr = battery_save_path; + GB_load_battery(&gb, battery_save_path); } else { GB_load_rom(&gb, filename); From 0534b091a576312f5d5ea2be4ad48ee4b53d01cb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 23:11:29 +0300 Subject: [PATCH 243/341] Fix SDL --- SDL/main.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/SDL/main.c b/SDL/main.c index 9a358d0..e27c0f2 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -529,18 +529,18 @@ restart: bool error = false; GB_debugger_clear_symbols(&gb); start_capturing_logs(); - size_t length = strlen(filename); + size_t path_length = strlen(filename); char extension[4] = {0,}; - if (length > 4) { - if (filename[length - 4] == '.') { - extension[0] = tolower(filename[length - 3]); - extension[1] = tolower(filename[length - 2]); - extension[2] = tolower(filename[length - 1]); + if (path_length > 4) { + if (filename[path_length - 4] == '.') { + extension[0] = tolower(filename[path_length - 3]); + extension[1] = tolower(filename[path_length - 2]); + extension[2] = tolower(filename[path_length - 1]); } } if (strcmp(extension, "isx") == 0) { error = GB_load_isx(&gb, filename); - /* Try loading .ram file if available */ + /* Configure battery */ char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */ replace_extension(filename, path_length, battery_save_path, ".ram"); battery_save_path_ptr = battery_save_path; @@ -551,7 +551,6 @@ restart: } end_capturing_logs(true, error); - size_t path_length = strlen(filename); /* Configure battery */ char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */ From d1e3ad7790879330845b2bbcb63260cbc5505de4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 23:18:03 +0300 Subject: [PATCH 244/341] Better hueristics for wrong MBC type --- Core/gb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 701a48a..966022b 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -484,13 +484,13 @@ done:; // Fix a common wrong MBC error if (gb->rom[0x147] == 3) { // MBC1 + RAM + Battery - if (gb->rom_size == 64 * 0x4000) { - for (unsigned i = 63 * 0x4000; i < 64 * 0x4000; i++) { + if (gb->rom_size >= 33 * 0x4000) { + for (unsigned i = 32 * 0x4000; i < 33 * 0x4000; i++) { if (gb->rom[i]) { gb->rom[0x147] = 0x10; // MBC3 + RTC + RAM + Battery GB_configure_cart(gb); gb->rom[0x147] = 0x3; - GB_log(gb, "ROM uses MBC1 but appears to use all 64 banks, assuming MBC3\n"); + GB_log(gb, "ROM claims to use MBC1 but appears to require MBC3 or 5, assuming MBC3.\n"); break; } } From 110cedeaac3918170263377792e63f5addf436df Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 25 Apr 2020 23:26:17 +0300 Subject: [PATCH 245/341] Even better hueristics --- Core/gb.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 966022b..a17a5a7 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -484,17 +484,37 @@ done:; // Fix a common wrong MBC error if (gb->rom[0x147] == 3) { // MBC1 + RAM + Battery - if (gb->rom_size >= 33 * 0x4000) { - for (unsigned i = 32 * 0x4000; i < 33 * 0x4000; i++) { + bool needs_fix = false; + if (gb->rom_size >= 0x21 * 0x4000) { + for (unsigned i = 0x20 * 0x4000; i < 0x21 * 0x4000; i++) { if (gb->rom[i]) { - gb->rom[0x147] = 0x10; // MBC3 + RTC + RAM + Battery - GB_configure_cart(gb); - gb->rom[0x147] = 0x3; - GB_log(gb, "ROM claims to use MBC1 but appears to require MBC3 or 5, assuming MBC3.\n"); + needs_fix = true; break; } } } + if (!needs_fix && gb->rom_size >= 0x41 * 0x4000) { + for (unsigned i = 0x40 * 0x4000; i < 0x41 * 0x4000; i++) { + if (gb->rom[i]) { + needs_fix = true; + break; + } + } + } + if (!needs_fix && gb->rom_size >= 0x61 * 0x4000) { + for (unsigned i = 0x60 * 0x4000; i < 0x61 * 0x4000; i++) { + if (gb->rom[i]) { + needs_fix = true; + break; + } + } + } + if (needs_fix) { + gb->rom[0x147] = 0x10; // MBC3 + RTC + RAM + Battery + GB_configure_cart(gb); + gb->rom[0x147] = 0x3; + GB_log(gb, "ROM claims to use MBC1 but appears to require MBC3 or 5, assuming MBC3.\n"); + } } if (old_rom) { From 8d016f19d22c87aa11ead516a0c579a1a040544c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 27 Apr 2020 21:12:30 +0300 Subject: [PATCH 246/341] Move the audio code to a different file --- Makefile | 3 +- SDL/audio/audio.h | 16 +++++++++ SDL/audio/sdl.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++ SDL/main.c | 81 ++++++++----------------------------------- 4 files changed, 119 insertions(+), 68 deletions(-) create mode 100644 SDL/audio/audio.h create mode 100644 SDL/audio/sdl.c diff --git a/Makefile b/Makefile index 49e24b5..9df7eb8 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ endif VERSION := 0.12.3 export VERSION CONF ?= debug +SDL_AUDIO_DRIVER ?= sdl BIN := build/bin OBJ := build/obj @@ -172,7 +173,7 @@ all: cocoa sdl tester libretro # Get a list of our source files and their respective object file targets CORE_SOURCES := $(shell ls Core/*.c) -SDL_SOURCES := $(shell ls SDL/*.c) $(OPEN_DIALOG) +SDL_SOURCES := $(shell ls SDL/*.c) $(OPEN_DIALOG) SDL/audio/$(SDL_AUDIO_DRIVER).c TESTER_SOURCES := $(shell ls Tester/*.c) ifeq ($(PLATFORM),Darwin) diff --git a/SDL/audio/audio.h b/SDL/audio/audio.h new file mode 100644 index 0000000..acaa011 --- /dev/null +++ b/SDL/audio/audio.h @@ -0,0 +1,16 @@ +#ifndef sdl_audio_h +#define sdl_audio_h + +#include +#include +#include + +bool GB_audio_is_playing(void); +void GB_audio_set_paused(bool paused); +void GB_audio_clear_queue(void); +unsigned GB_audio_get_frequency(void); +size_t GB_audio_get_queue_length(void); +void GB_audio_queue_sample(GB_sample_t *sample); +void GB_audio_init(void); + +#endif /* sdl_audio_h */ diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c new file mode 100644 index 0000000..1f8a529 --- /dev/null +++ b/SDL/audio/sdl.c @@ -0,0 +1,87 @@ +#include "audio.h" +#include + +#ifndef _WIN32 +#define AUDIO_FREQUENCY 96000 +#include +#else +#include +/* Windows (well, at least my VM) can't handle 96KHz sound well :( */ + +/* felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq. + we can get is 48000. 96000 also works, but always has some faint crackling in + the audio, no matter how high or low I set the buffer length... + Not quite satisfied with that solution, because acc. to SDL2 docs, + 96k + WASAPI *should* work. */ + +#define AUDIO_FREQUENCY 48000 +#endif + +/* Compatibility with older SDL versions */ +#ifndef SDL_AUDIO_ALLOW_SAMPLES_CHANGE +#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0 +#endif + +static SDL_AudioDeviceID device_id; +static SDL_AudioSpec want_aspec, have_aspec; + +bool GB_audio_is_playing(void) +{ + return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; +} + +void GB_audio_set_paused(bool paused) +{ + GB_audio_clear_queue(); + SDL_PauseAudioDevice(device_id, paused); +} + +void GB_audio_clear_queue(void) +{ + SDL_ClearQueuedAudio(device_id); +} + +unsigned GB_audio_get_frequency(void) +{ + return have_aspec.freq; +} + +size_t GB_audio_get_queue_length(void) +{ + return SDL_GetQueuedAudioSize(device_id); +} + +void GB_audio_queue_sample(GB_sample_t *sample) +{ + SDL_QueueAudio(device_id, sample, sizeof(*sample)); +} + +void GB_audio_init(void) +{ + /* Configure Audio */ + memset(&want_aspec, 0, sizeof(want_aspec)); + want_aspec.freq = AUDIO_FREQUENCY; + want_aspec.format = AUDIO_S16SYS; + want_aspec.channels = 2; + want_aspec.samples = 512; + + SDL_version _sdl_version; + SDL_GetVersion(&_sdl_version); + unsigned sdl_version = _sdl_version.major * 1000 + _sdl_version.minor * 100 + _sdl_version.patch; + +#ifndef _WIN32 + /* SDL 2.0.5 on macOS and Linux introduced a bug where certain combinations of buffer lengths and frequencies + fail to produce audio correctly. */ + if (sdl_version >= 2005) { + want_aspec.samples = 2048; + } +#else + if (sdl_version < 2006) { + /* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency + to 44100 because otherwise we would get garbled audio output.*/ + want_aspec.freq = 44100; + } +#endif + + device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE); +} diff --git a/SDL/main.c b/SDL/main.c index e27c0f2..c4a4d0f 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -8,27 +8,12 @@ #include "utils.h" #include "gui.h" #include "shader.h" - +#include "audio/audio.h" #ifndef _WIN32 -#define AUDIO_FREQUENCY 96000 #include #else #include -/* Windows (well, at least my VM) can't handle 96KHz sound well :( */ - -/* felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq. - we can get is 48000. 96000 also works, but always has some faint crackling in - the audio, no matter how high or low I set the buffer length... - Not quite satisfied with that solution, because acc. to SDL2 docs, - 96k + WASAPI *should* work. */ - -#define AUDIO_FREQUENCY 48000 -#endif - -/* Compatibility with older SDL versions */ -#ifndef SDL_AUDIO_ALLOW_SAMPLES_CHANGE -#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0 #endif GB_gameboy_t gb; @@ -42,7 +27,6 @@ static char *filename = NULL; static typeof(free) *free_function = NULL; static char *battery_save_path_ptr; -SDL_AudioDeviceID device_id; void set_filename(const char *new_filename, typeof(free) *new_free_function) { @@ -53,8 +37,6 @@ void set_filename(const char *new_filename, typeof(free) *new_free_function) free_function = new_free_function; } -static SDL_AudioSpec want_aspec, have_aspec; - static char *captured_log = NULL; static void log_capture_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes) @@ -127,16 +109,15 @@ static void screen_size_changed(void) static void open_menu(void) { - bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; + bool audio_playing = GB_audio_is_playing(); if (audio_playing) { - SDL_PauseAudioDevice(device_id, 1); + GB_audio_set_paused(true); } size_t previous_width = GB_get_screen_width(&gb); run_gui(true); SDL_ShowCursor(SDL_DISABLE); if (audio_playing) { - SDL_ClearQueuedAudio(device_id); - SDL_PauseAudioDevice(device_id, 0); + GB_audio_set_paused(false); } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); GB_set_border_mode(&gb, configuration.border_mode); @@ -176,7 +157,7 @@ static void handle_events(GB_gameboy_t *gb) GB_set_key_state(gb, (GB_key_t) button, event.type == SDL_JOYBUTTONDOWN); } else if (button == JOYPAD_BUTTON_TURBO) { - SDL_ClearQueuedAudio(device_id); + GB_audio_clear_queue(); turbo_down = event.type == SDL_JOYBUTTONDOWN; GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down); } @@ -294,14 +275,7 @@ static void handle_events(GB_gameboy_t *gb) break; } #endif - bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; - if (audio_playing) { - SDL_PauseAudioDevice(device_id, 1); - } - else if (!audio_playing) { - SDL_ClearQueuedAudio(device_id); - SDL_PauseAudioDevice(device_id, 0); - } + GB_audio_set_paused(GB_audio_is_playing()); } break; @@ -336,7 +310,7 @@ static void handle_events(GB_gameboy_t *gb) case SDL_KEYUP: // Fallthrough if (event.key.keysym.scancode == configuration.keys[8]) { turbo_down = event.type == SDL_KEYDOWN; - SDL_ClearQueuedAudio(device_id); + GB_audio_clear_queue(); GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down); } else if (event.key.keysym.scancode == configuration.keys_2[0]) { @@ -409,15 +383,15 @@ static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) if (turbo_down) { static unsigned skip = 0; skip++; - if (skip == have_aspec.freq / 8) { + if (skip == GB_audio_get_frequency() / 8) { skip = 0; } - if (skip > have_aspec.freq / 16) { + if (skip > GB_audio_get_frequency() / 16) { return; } } - if (SDL_GetQueuedAudioSize(device_id) / sizeof(*sample) > have_aspec.freq / 4) { + if (GB_audio_get_queue_length() / sizeof(*sample) > GB_audio_get_frequency() / 4) { return; } @@ -426,7 +400,7 @@ static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) sample->right = sample->right * configuration.volume / 100; } - SDL_QueueAudio(device_id, sample, sizeof(*sample)); + GB_audio_queue_sample(sample); } @@ -514,7 +488,7 @@ restart: GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_pixels_output(&gb, active_pixel_buffer); GB_set_rgb_encode_callback(&gb, rgb_encode); - GB_set_sample_rate(&gb, have_aspec.freq); + GB_set_sample_rate(&gb, GB_audio_get_frequency()); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); update_palette(); if ((unsigned)configuration.border_mode <= GB_BORDER_ALWAYS) { @@ -675,34 +649,7 @@ int main(int argc, char **argv) pixel_format = SDL_AllocFormat(SDL_PIXELFORMAT_ABGR8888); } - - /* Configure Audio */ - memset(&want_aspec, 0, sizeof(want_aspec)); - want_aspec.freq = AUDIO_FREQUENCY; - want_aspec.format = AUDIO_S16SYS; - want_aspec.channels = 2; - want_aspec.samples = 512; - - SDL_version _sdl_version; - SDL_GetVersion(&_sdl_version); - unsigned sdl_version = _sdl_version.major * 1000 + _sdl_version.minor * 100 + _sdl_version.patch; - -#ifndef _WIN32 - /* SDL 2.0.5 on macOS and Linux introduced a bug where certain combinations of buffer lengths and frequencies - fail to produce audio correctly. */ - if (sdl_version >= 2005) { - want_aspec.samples = 2048; - } -#else - if (sdl_version < 2006) { - /* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency - to 44100 because otherwise we would get garbled audio output.*/ - want_aspec.freq = 44100; - } -#endif - - device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE); - /* Start Audio */ + GB_audio_init(); SDL_EventState(SDL_DROPFILE, SDL_ENABLE); @@ -735,7 +682,7 @@ int main(int argc, char **argv) else { connect_joypad(); } - SDL_PauseAudioDevice(device_id, 0); + GB_audio_set_paused(false); run(); // Never returns return 0; } From c64d5b58b6a8f09877e8dc115a5ba657fca63895 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 27 Apr 2020 23:29:26 +0300 Subject: [PATCH 247/341] Make failed builds easier to read --- .github/workflows/sanity.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index 653e05d..d25a180 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -23,7 +23,7 @@ jobs: ./.github/actions/install_deps.sh ${{ matrix.os }} - name: Build run: | - ${{ matrix.cc }} -v; make -j sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }} + ${{ matrix.cc }} -v; (make -j sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }} || (echo "==== Build Failed ==="; make sdl tester libretro ${{ matrix.extra_target }} CONF=release CC=${{ matrix.cc }})) - name: Sanity tests shell: bash run: | From 1e54c55c117f1141699d53647e2348460e96440a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 28 Apr 2020 21:44:29 +0300 Subject: [PATCH 248/341] Making libretro compile without warnings with GCC --- Makefile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9df7eb8..94e8a83 100644 --- a/Makefile +++ b/Makefile @@ -89,14 +89,16 @@ OPEN_DIALOG = OpenDialog/cocoa.m endif # These must come before the -Wno- flags -CFLAGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option -CFLAGS += -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context +WARNINGS += -Werror -Wall -Wno-unknown-warning -Wno-unknown-warning-option +WARNINGS += -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context # Only add this flag if the compiler supports it ifeq ($(shell $(CC) -x c -c $(NULL) -o $(NULL) -Werror -Wpartial-availability 2> $(NULL); echo $$?),0) -CFLAGS += -Wpartial-availability +WARNINGS += -Wpartial-availability endif +CFLAGS += $(WARNINGS) + CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES ifeq (,$(PKG_CONFIG)) @@ -396,7 +398,7 @@ $(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.pb8 # Libretro Core (uses its own build system) libretro: - $(MAKE) -C libretro + CFLAGS="$(WARNINGS)" $(MAKE) -C libretro # Clean clean: From 8f6047fdca3f909b0b1809f4d76c5b6684f7b679 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 28 Apr 2020 21:53:37 +0300 Subject: [PATCH 249/341] Prevent -Wall from overriding -Wno flags --- libretro/Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libretro/Makefile b/libretro/Makefile index 7e19893..b327628 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -1,6 +1,8 @@ STATIC_LINKING := 0 AR := ar +CFLAGS := -Wall $(CFLAGS) + GIT_VERSION ?= " $(shell git rev-parse --short HEAD || echo unknown)" ifneq ($(GIT_VERSION)," unknown") CFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\" @@ -93,7 +95,7 @@ else ifeq ($(platform), linux-portable) else ifeq ($(platform), switch) TARGET := $(TARGET_NAME)_libretro_$(platform).a include $(LIBTRANSISTOR_HOME)/libtransistor.mk - CFLAGS += -Wl,-q -Wall -O3 -fno-short-enums -fno-optimize-sibling-calls + CFLAGS += -Wl,-q -O3 -fno-short-enums -fno-optimize-sibling-calls STATIC_LINKING=1 # Nintendo WiiU else ifeq ($(platform), wiiu) @@ -141,7 +143,7 @@ else ifeq ($(platform), vita) TARGET := $(TARGET_NAME)_vita.a CC = arm-vita-eabi-gcc AR = arm-vita-eabi-ar - CFLAGS += -Wl,-q -Wall -O3 -fno-short-enums -fno-optimize-sibling-calls + CFLAGS += -Wl,-q -O3 -fno-short-enums -fno-optimize-sibling-calls STATIC_LINKING = 1 # Windows MSVC 2017 all architectures @@ -278,7 +280,7 @@ else LD = $(CC) endif -CFLAGS += -Wall -D__LIBRETRO__ $(fpic) $(INCFLAGS) -std=gnu11 -D_GNU_SOURCE -D_USE_MATH_DEFINES +CFLAGS += -D__LIBRETRO__ $(fpic) $(INCFLAGS) -std=gnu11 -D_GNU_SOURCE -D_USE_MATH_DEFINES all: $(TARGET) From 151d58eb60cc4abffd08a8e4ca0276850f98b8b8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:05:31 +0300 Subject: [PATCH 250/341] setRumble should be double --- Cocoa/GBView.h | 2 +- Cocoa/GBView.m | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index c19d340..80721cd 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -22,5 +22,5 @@ typedef enum { - (uint32_t *)currentBuffer; - (uint32_t *)previousBuffer; - (void)screenSizeChanged; -- (void)setRumble: (bool)on; +- (void)setRumble: (double)amp; @end diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 1272c93..e733731 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -270,9 +270,9 @@ } } -- (void)setRumble:(bool)on +- (void)setRumble:(double)amp { - [lastController setRumbleAmplitude:(double)on]; + [lastController setRumbleAmplitude:amp]; } - (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis From 6448a692e297e71fc86b2f07fd142c0515fb6406 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:06:11 +0300 Subject: [PATCH 251/341] Add smart rumble to games without a rumblepak --- Core/apu.c | 1 - Core/display.c | 8 +------- Core/gb.h | 1 + Core/rumble.c | 42 ++++++++++++++++++++++++++++++++++++++++++ Core/rumble.h | 8 ++++++++ 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 Core/rumble.c create mode 100644 Core/rumble.h diff --git a/Core/apu.c b/Core/apu.c index e63d89f..afb970c 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -493,7 +493,6 @@ void GB_apu_run(GB_gameboy_t *gb) /* Step LFSR */ unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; - /* Todo: is this formula is different on a GBA? */ bool new_high_bit = (gb->apu.noise_channel.lfsr ^ (gb->apu.noise_channel.lfsr >> 1) ^ 1) & 1; gb->apu.noise_channel.lfsr >>= 1; diff --git a/Core/display.c b/Core/display.c index b3522f1..be2e108 100644 --- a/Core/display.c +++ b/Core/display.c @@ -200,14 +200,8 @@ static void display_vblank(GB_gameboy_t *gb) } } } + GB_handle_rumble(gb); - if (gb->rumble_callback) { - if (gb->rumble_on_cycles + gb->rumble_off_cycles) { - gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles)); - gb->rumble_on_cycles = gb->rumble_off_cycles = 0; - } - } - if (gb->vblank_callback) { gb->vblank_callback(gb); } diff --git a/Core/gb.h b/Core/gb.h index d63d400..7967b6f 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -22,6 +22,7 @@ #include "symbol_hash.h" #include "sgb.h" #include "cheats.h" +#include "rumble.h" #define GB_STRUCT_VERSION 13 diff --git a/Core/rumble.c b/Core/rumble.c new file mode 100644 index 0000000..5ac3d0d --- /dev/null +++ b/Core/rumble.c @@ -0,0 +1,42 @@ +#include "rumble.h" +#include "gb.h" + +void GB_handle_rumble(GB_gameboy_t *gb) +{ + if (gb->rumble_callback) { + if (gb->cartridge_type->has_rumble) { + if (gb->rumble_on_cycles + gb->rumble_off_cycles) { + gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles)); + gb->rumble_on_cycles = gb->rumble_off_cycles = 0; + } + } + else { + unsigned volume = (gb->io_registers[GB_IO_NR50] & 7) + 1 + ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1; + unsigned ch4_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 8) + !!(gb->io_registers[GB_IO_NR51] & 0x80)); + unsigned ch1_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 1) + !!(gb->io_registers[GB_IO_NR51] & 0x10)); + + double ch4_rumble = (MIN(gb->apu.noise_channel.sample_length * (gb->apu.noise_channel.narrow? 8 : 1) , 4096) * ((signed) gb->apu.noise_channel.current_volume * gb->apu.noise_channel.current_volume * ch4_volume / 32.0 - 50) - 2048) / 2048.0; + + ch4_rumble = MIN(ch4_rumble, 1.0); + ch4_rumble = MAX(ch4_rumble, 0.0); + + double ch1_rumble = 0; + if (gb->apu.sweep_enabled && ((gb->io_registers[GB_IO_NR10] >> 4) & 7)) { + double sweep_speed = (gb->io_registers[GB_IO_NR10] & 7) / (double)((gb->io_registers[GB_IO_NR10] >> 4) & 7); + ch1_rumble = gb->apu.square_channels[GB_SQUARE_1].current_volume * ch1_volume / 32.0 * sweep_speed / 8.0 - 0.5; + ch1_rumble = MIN(ch1_rumble, 1.0); + ch1_rumble = MAX(ch1_rumble, 0.0); + } + + if (!gb->apu.is_active[GB_NOISE]) { + ch4_rumble = 0; + } + + if (!gb->apu.is_active[GB_SQUARE_1]) { + ch1_rumble = 0; + } + + gb->rumble_callback(gb, MIN(MAX(ch1_rumble / 2 + ch4_rumble, 0.0), 1.0)); + } + } +} diff --git a/Core/rumble.h b/Core/rumble.h new file mode 100644 index 0000000..a378f2d --- /dev/null +++ b/Core/rumble.h @@ -0,0 +1,8 @@ +#ifndef rumble_h +#define rumble_h + +#include "gb_struct_def.h" + +void GB_handle_rumble(GB_gameboy_t *gb); + +#endif /* rumble_h */ From 4c443d51ce52431e52dbd9e9b9313746a6248825 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:06:38 +0300 Subject: [PATCH 252/341] Minor JoyKit improvements --- JoyKit/JOYAxes2D.m | 27 ++++++++++++++++++++------- JoyKit/JOYController.m | 3 +++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m index 28ce16c..624ccef 100644 --- a/JoyKit/JOYAxes2D.m +++ b/JoyKit/JOYAxes2D.m @@ -125,23 +125,21 @@ double old1 = _state1, old2 = _state2; { - double min = [self effectiveMinX]; - double max = [self effectiveMaxX]; - if (min == max) return false; int32_t value = x; - + if (initialX != 0) { minX = MIN(value, minX); maxX = MAX(value, maxX); } + double min = [self effectiveMinX]; + double max = [self effectiveMaxX]; + if (min == max) return false; + _state1 = (value - min) / (max - min) * 2 - 1; } { - double min = [self effectiveMinY]; - double max = [self effectiveMaxY]; - if (min == max) return false; int32_t value = y; if (initialY != 0) { @@ -149,8 +147,23 @@ maxY = MAX(value, maxY); } + double min = [self effectiveMinY]; + double max = [self effectiveMaxY]; + if (min == max) return false; + _state2 = (value - min) / (max - min) * 2 - 1; } + + if (_state1 < -1 || _state1 > 1 || + _state2 < -1 || _state2 > 1) { + // Makes no sense, recalibrate + _state1 = _state2 = 0; + initialX = initialY = 0; + minX = _element1.max; + minY = _element2.max; + maxX = _element1.min; + maxY = _element2.min; + } return old1 != _state1 || old2 != _state2; } diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index d5e1acb..2b21861 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -382,11 +382,13 @@ typedef struct __attribute__((packed)) { - (NSString *)deviceName { + if (!_device) return nil; return IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductKey)); } - (NSString *)uniqueID { + if (!_device) return nil; NSString *serial = (__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDSerialNumberKey)); if (!serial || [(__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDTransportKey)) isEqualToString:@"USB"]) { serial = [NSString stringWithFormat:@"%04x%04x%08x", @@ -581,6 +583,7 @@ typedef struct __attribute__((packed)) { - (void)sendReport:(NSData *)report { if (!report.length) return; + if (!_device) return; IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length); } From 4b2417855373761a126b30749c3ae5191a5c8d5d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:50:31 +0300 Subject: [PATCH 253/341] Rumble mode selection --- Cocoa/AppDelegate.m | 1 + Cocoa/Document.m | 11 +++++++++ Cocoa/GBPreferencesWindow.h | 1 + Cocoa/GBPreferencesWindow.m | 20 ++++++++++++++++ Cocoa/Preferences.xib | 48 +++++++++++++++++++++++++++++-------- Core/gb.h | 1 + Core/rumble.c | 13 +++++++++- Core/rumble.h | 9 +++++++ 8 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index be476cf..3404620 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -43,6 +43,7 @@ @"GBDMGModel": @(GB_MODEL_DMG_B), @"GBCGBModel": @(GB_MODEL_CGB_E), @"GBSGBModel": @(GB_MODEL_SGB2), + @"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY), }]; [JOYController startOnRunLoop:[NSRunLoop currentRunLoop] withOptions:@{ diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 824c816..90a1dc8 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -228,6 +228,11 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) borderModeChanged = true; } +- (void) updateRumbleMode +{ + GB_set_rumble_mode(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRumbleMode"]); +} + - (void) initCommon { GB_init(&gb, [self internalModel]); @@ -247,6 +252,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]); GB_apu_set_sample_callback(&gb, audioCallback); GB_set_rumble_callback(&gb, rumbleCallback); + [self updateRumbleMode]; } - (void) updateMinSize @@ -556,6 +562,11 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) name:@"GBBorderModeChanged" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateRumbleMode) + name:@"GBRumbleModeChanged" + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRewindLength) name:@"GBRewindLengthChanged" diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index 7f6bf06..ee697a8 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -16,6 +16,7 @@ @property (strong) IBOutlet NSButton *skipButton; @property (strong) IBOutlet NSMenuItem *bootROMsFolderItem; @property (strong) IBOutlet NSPopUpButtonCell *bootROMsButton; +@property (strong) IBOutlet NSPopUpButton *rumbleModePopupButton; @property (weak) IBOutlet NSPopUpButton *dmgPopupButton; @property (weak) IBOutlet NSPopUpButton *sgbPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 4d0848c..71183e1 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -24,6 +24,7 @@ NSPopUpButton *_dmgPopupButton, *_sgbPopupButton, *_cgbPopupButton; NSPopUpButton *_preferredJoypadButton; + NSPopUpButton *_rumbleModePopupButton; } + (NSArray *)filterList @@ -125,6 +126,18 @@ return _displayBorderPopupButton; } +- (void)setRumbleModePopupButton:(NSPopUpButton *)rumbleModePopupButton +{ + _rumbleModePopupButton = rumbleModePopupButton; + NSInteger mode = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRumbleMode"]; + [_rumbleModePopupButton selectItemWithTag:mode]; +} + +- (NSPopUpButton *)rumbleModePopupButton +{ + return _rumbleModePopupButton; +} + - (void)setRewindPopupButton:(NSPopUpButton *)rewindPopupButton { _rewindPopupButton = rewindPopupButton; @@ -267,6 +280,13 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBBorderModeChanged" object:nil]; } +- (IBAction)rumbleModeChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender selectedItem].tag) + forKey:@"GBRumbleMode"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBRumbleModeChanged" object:nil]; +} + - (IBAction)rewindLengthChanged:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@([sender selectedTag]) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 1107e7d..149f71e 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -76,6 +76,7 @@ + @@ -463,11 +464,11 @@ - + - + @@ -475,8 +476,17 @@ + + + + + + + + + - + @@ -534,7 +544,7 @@ - + @@ -543,7 +553,7 @@ - + @@ -559,11 +569,11 @@ - + - + @@ -572,7 +582,7 @@ - + @@ -590,8 +600,26 @@ + + + + + + + + + + + + + + + + + + - + diff --git a/Core/gb.h b/Core/gb.h index 7967b6f..97a8069 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -661,6 +661,7 @@ struct GB_gameboy_internal_s { bool vblank_just_occured; // For slow operations involving syscalls; these should only run once per vblank uint8_t cycles_since_run; // How many cycles have passed since the last call to GB_run(), in 8MHz units double clock_multiplier; + GB_rumble_mode_t rumble_mode; uint32_t rumble_on_cycles; uint32_t rumble_off_cycles; diff --git a/Core/rumble.c b/Core/rumble.c index 5ac3d0d..8cbe20d 100644 --- a/Core/rumble.c +++ b/Core/rumble.c @@ -1,16 +1,27 @@ #include "rumble.h" #include "gb.h" +void GB_set_rumble_mode(GB_gameboy_t *gb, GB_rumble_mode_t mode) +{ + gb->rumble_mode = mode; + if (gb->rumble_callback) { + gb->rumble_callback(gb, 0); + } +} + void GB_handle_rumble(GB_gameboy_t *gb) { if (gb->rumble_callback) { + if (gb->rumble_mode == GB_RUMBLE_DISABLED) { + return; + } if (gb->cartridge_type->has_rumble) { if (gb->rumble_on_cycles + gb->rumble_off_cycles) { gb->rumble_callback(gb, gb->rumble_on_cycles / (double)(gb->rumble_on_cycles + gb->rumble_off_cycles)); gb->rumble_on_cycles = gb->rumble_off_cycles = 0; } } - else { + else if (gb->rumble_mode == GB_RUMBLE_ALL_GAMES) { unsigned volume = (gb->io_registers[GB_IO_NR50] & 7) + 1 + ((gb->io_registers[GB_IO_NR50] >> 4) & 7) + 1; unsigned ch4_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 8) + !!(gb->io_registers[GB_IO_NR51] & 0x80)); unsigned ch1_volume = volume * (!!(gb->io_registers[GB_IO_NR51] & 1) + !!(gb->io_registers[GB_IO_NR51] & 0x10)); diff --git a/Core/rumble.h b/Core/rumble.h index a378f2d..eae9f37 100644 --- a/Core/rumble.h +++ b/Core/rumble.h @@ -3,6 +3,15 @@ #include "gb_struct_def.h" +typedef enum { + GB_RUMBLE_DISABLED, + GB_RUMBLE_CARTRIDGE_ONLY, + GB_RUMBLE_ALL_GAMES +} GB_rumble_mode_t; + +#ifdef GB_INTERNAL void GB_handle_rumble(GB_gameboy_t *gb); +#endif +void GB_set_rumble_mode(GB_gameboy_t *gb, GB_rumble_mode_t mode); #endif /* rumble_h */ From 0c91502859496c21e174095eb8a56d74a236c58a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:52:32 +0300 Subject: [PATCH 254/341] Remove log --- Cocoa/GBPreferencesWindow.m | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 71183e1..31eebde 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -332,9 +332,7 @@ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ joypad_wait = false; }); - - NSLog(@"%@", button); - + if (!button.isPressed) return; if (joystick_configuration_state == -1) return; if (joystick_configuration_state == GBButtonCount) return; From 05cf3656b80df7ebf91baabf13aec9322269d733 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:58:38 +0300 Subject: [PATCH 255/341] Fix libretro --- .github/workflows/sanity.yml | 1 + libretro/Makefile.common | 1 + libretro/libretro.c | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index d25a180..6c795fb 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -3,6 +3,7 @@ on: push jobs: sanity: + fail-fast: false strategy: matrix: os: [macos-latest, ubuntu-latest, ubuntu-16.04] diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 947b14c..7f7688a 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -13,6 +13,7 @@ SOURCES_C := $(CORE_DIR)/Core/gb.c \ $(CORE_DIR)/Core/joypad.c \ $(CORE_DIR)/Core/save_state.c \ $(CORE_DIR)/Core/random.c \ + $(CORE_DIR)/Core/rumble.c \ $(CORE_DIR)/libretro/agb_boot.c \ $(CORE_DIR)/libretro/cgb_boot.c \ $(CORE_DIR)/libretro/dmg_boot.c \ diff --git a/libretro/libretro.c b/libretro/libretro.c index 2ea1cb6..4831065 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -376,6 +376,7 @@ static void init_for_current_model(unsigned id) GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); GB_apu_set_sample_callback(&gameboy[i], audio_callback); GB_set_rumble_callback(&gameboy[i], rumble_callback); + GB_set_rumble_mode(&gameboy[i], GB_RUMBLE_CARTRIDGE_ONLY); /* todo: attempt to make these more generic */ From 5c9d50e25ff3fe1b1e4c0aa273dd07b4cce4b70c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 17:02:20 +0300 Subject: [PATCH 256/341] Fix job --- .github/workflows/sanity.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sanity.yml b/.github/workflows/sanity.yml index 6c795fb..f460931 100644 --- a/.github/workflows/sanity.yml +++ b/.github/workflows/sanity.yml @@ -3,8 +3,8 @@ on: push jobs: sanity: - fail-fast: false strategy: + fail-fast: false matrix: os: [macos-latest, ubuntu-latest, ubuntu-16.04] cc: [gcc, clang] From 66112af37e330f322be3a6ff16bb768e16f0e0f3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 19:53:47 +0300 Subject: [PATCH 257/341] Fix PWM performence issue --- JoyKit/JOYController.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 2b21861..c18fbdb 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -115,6 +115,7 @@ typedef struct __attribute__((packed)) { volatile double _rumblePWMRatio; bool _physicallyConnected; bool _logicallyConnected; + bool _rumblePWMThreadRunning; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device @@ -609,7 +610,7 @@ typedef struct __attribute__((packed)) { [NSThread sleepForTimeInterval:(1 - _rumblePWMRatio) / 10]; } [_rumblePWMThreadLock lock]; - [_rumblePWMThreadLock signal]; + _rumblePWMThreadRunning = false; [_rumblePWMThreadLock unlock]; } @@ -657,23 +658,23 @@ typedef struct __attribute__((packed)) { } else { if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { - if (_rumblePWMRatio == 0) { // PWM thread not running, start it. + [_rumblePWMThreadLock lock]; + if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. if (amp != 0) { _rumblePWMRatio = amp; + _rumblePWMThreadRunning = true; [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; } } else { if (amp == 0) { // Thread is running, signal it to stop - [_rumblePWMThreadLock lock]; _rumblePWMRatio = 0; - [_rumblePWMThreadLock wait]; - [_rumblePWMThreadLock unlock]; } else { _rumblePWMRatio = amp; } } + [_rumblePWMThreadLock unlock]; } else { [_rumbleElement setValue:amp * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; From 9f876e380c55251d16842d710b17c9e379ab65ba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 20:08:00 +0300 Subject: [PATCH 258/341] Offical WUP-028s require an activation sequence --- JoyKit/ControllerConfiguration.inc | 2 ++ JoyKit/JOYController.m | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index cdbdce3..cf327f9 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -120,6 +120,8 @@ hacksByName = @{ JOYConnectedUsage: @2, JOYConnectedUsagePage: @0xFF00, + + JOYActivationReport: [NSData dataWithBytes:(uint8_t[]){0x13} length:1], JOYSubElementStructs: @{ diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index c18fbdb..b4810ab 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -19,7 +19,7 @@ static NSString const *JOYConnectedUsagePage = @"JOYConnectedUsagePage"; static NSString const *JOYRumbleMin = @"JOYRumbleMin"; static NSString const *JOYRumbleMax = @"JOYRumbleMax"; static NSString const *JOYSwapZRz = @"JOYSwapZRz"; - +static NSString const *JOYActivationReport = @"JOYActivationReport"; static NSMutableDictionary *controllers; // Physical controllers static NSMutableArray *exposedControllers; // Logical controllers @@ -691,15 +691,25 @@ typedef struct __attribute__((packed)) { { NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); NSDictionary *hacks = hacksByName[name]; + if (!hacks) { + hacks = hacksByManufacturer[(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey))]; + } NSArray *filters = hacks[JOYReportIDFilters]; + JOYController *controller = nil; if (filters) { - JOYController *controller = [[JOYMultiplayerController alloc] initWithDevice:device - reportIDFilters:filters]; - [controllers setObject:controller forKey:[NSValue valueWithPointer:device]]; + controller = [[JOYMultiplayerController alloc] initWithDevice:device + reportIDFilters:filters]; } else { - [controllers setObject:[[JOYController alloc] initWithDevice:device] forKey:[NSValue valueWithPointer:device]]; + controller = [[JOYController alloc] initWithDevice:device]; } + + if (hacks[JOYActivationReport]) { + [controller sendReport:hacks[JOYActivationReport]]; + } + [controllers setObject:controller forKey:[NSValue valueWithPointer:device]]; + + } + (void)controllerRemoved:(IOHIDDeviceRef) device From 03ea6dc708cd6d72800a803bd80556e876afb022 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 20:44:55 +0300 Subject: [PATCH 259/341] Make builds possible without Xcode --- Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Makefile b/Makefile index 5c702a1..9236a11 100644 --- a/Makefile +++ b/Makefile @@ -125,6 +125,13 @@ endif ifeq ($(PLATFORM),Darwin) SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> $(NULL)) +ifeq ($(SYSROOT),) +SYSROOT := $(shell ls /Library/Developer/CommandLineTools/SDKs/ | grep 10 | tail -n 1) +endif +ifeq ($(SYSROOT),) +$(error Could not find a macOS SDK) +endif + CFLAGS += -F/Library/Frameworks -mmacosx-version-min=10.9 OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 From 0f73282e4ee47e9f844499ace3f8538140c868a3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 20:54:28 +0300 Subject: [PATCH 260/341] Actually allow it --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9236a11..03569e8 100644 --- a/Makefile +++ b/Makefile @@ -126,7 +126,7 @@ endif ifeq ($(PLATFORM),Darwin) SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> $(NULL)) ifeq ($(SYSROOT),) -SYSROOT := $(shell ls /Library/Developer/CommandLineTools/SDKs/ | grep 10 | tail -n 1) +SYSROOT := /Library/Developer/CommandLineTools/SDKs/$(shell ls /Library/Developer/CommandLineTools/SDKs/ | grep 10 | tail -n 1) endif ifeq ($(SYSROOT),) $(error Could not find a macOS SDK) From 4c1f073d20f4eb917709da896c4f33e34ac4042d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 20:59:19 +0300 Subject: [PATCH 261/341] Fix error report --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 03569e8..afc8d89 100644 --- a/Makefile +++ b/Makefile @@ -128,7 +128,7 @@ SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> $(NULL)) ifeq ($(SYSROOT),) SYSROOT := /Library/Developer/CommandLineTools/SDKs/$(shell ls /Library/Developer/CommandLineTools/SDKs/ | grep 10 | tail -n 1) endif -ifeq ($(SYSROOT),) +ifeq ($(SYSROOT),/Library/Developer/CommandLineTools/SDKs/) $(error Could not find a macOS SDK) endif From 6bcaffe27d3f1ddd73e2a3e6ef4d19a5c1677798 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 23:47:18 +0300 Subject: [PATCH 262/341] Fix sendReport on JOYMultiplayerControlle --- JoyKit/JOYMultiplayerController.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/JoyKit/JOYMultiplayerController.m b/JoyKit/JOYMultiplayerController.m index 0952c84..50840a1 100644 --- a/JoyKit/JOYMultiplayerController.m +++ b/JoyKit/JOYMultiplayerController.m @@ -4,6 +4,7 @@ - (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix; - (void)elementChanged:(IOHIDElementRef) element toValue:(IOHIDValueRef) value; - (void)disconnected; +- (void)sendReport:(NSData *)report; @end @implementation JOYMultiplayerController @@ -41,4 +42,9 @@ } } +- (void)sendReport:(NSData *)report +{ + [[_children firstObject] sendReport:report]; +} + @end From 60ad3160cf32ea86ea5a9662d67fdc4b8e93cf15 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 23:52:28 +0300 Subject: [PATCH 263/341] Fix an XIB oops --- Cocoa/Document.xib | 1 - 1 file changed, 1 deletion(-) diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index f1f2f5a..e1c5fa0 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -9,7 +9,6 @@ - From 160282c42af29f80b7e271e319015a60d2ad6e30 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 30 Apr 2020 23:56:14 +0300 Subject: [PATCH 264/341] Fix WUP-028 on Catalina, make controller configuration compatible between macOS versions --- JoyKit/ControllerConfiguration.inc | 4 ++-- JoyKit/JOYAxes2D.m | 2 +- JoyKit/JOYAxis.m | 2 +- JoyKit/JOYButton.m | 2 +- JoyKit/JOYController.m | 6 +++++- JoyKit/JOYElement.h | 1 + JoyKit/JOYSubElement.m | 1 + 7 files changed, 12 insertions(+), 6 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index cf327f9..6c81e9f 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -126,14 +126,14 @@ hacksByName = @{ JOYSubElementStructs: @{ // Rumble - @(1364): @[ + @(1357): @[ @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(2), @"size":@1, @"offset":@1, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(3), @"size":@1, @"offset":@2, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(4), @"size":@1, @"offset":@3, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, ], - @(11): @[ + @(4): @[ // Player 1 diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m index 624ccef..a1b91d2 100644 --- a/JoyKit/JOYAxes2D.m +++ b/JoyKit/JOYAxes2D.m @@ -36,7 +36,7 @@ - (uint64_t)uniqueID { - return _element1.uniqueID; + return _element1.persistentUniqueID; } - (NSString *)description diff --git a/JoyKit/JOYAxis.m b/JoyKit/JOYAxis.m index 169eaee..74a4563 100644 --- a/JoyKit/JOYAxis.m +++ b/JoyKit/JOYAxis.m @@ -40,7 +40,7 @@ - (uint64_t)uniqueID { - return _element.uniqueID; + return _element.persistentUniqueID; } - (NSString *)description diff --git a/JoyKit/JOYButton.m b/JoyKit/JOYButton.m index 3e6026d..a904299 100644 --- a/JoyKit/JOYButton.m +++ b/JoyKit/JOYButton.m @@ -50,7 +50,7 @@ - (uint64_t)uniqueID { - return _element.uniqueID; + return _element.persistentUniqueID; } - (NSString *)description diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index b4810ab..10ec6e4 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -173,13 +173,17 @@ typedef struct __attribute__((packed)) { JOYElement *previousAxisElement = nil; id previous = nil; + unsigned persistentUniqueID = 0; for (id _element in array) { if (_element == previous) continue; // Some elements are reported twice for some reason previous = _element; NSArray *elements = nil; JOYElement *element = [[JOYElement alloc] initWithElement:(__bridge IOHIDElementRef)_element]; + /* Cookie is not persistent across macOS versions because Apple added kIOHIDElementTypeInput_NULL + in a backwards incompatible manner. We must maintain our own cookie-like ID. */ + element.persistentUniqueID = persistentUniqueID++; - NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.uniqueID)]; + NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.persistentUniqueID)]; bool isOutput = false; if (subElementDefs && element.uniqueID != element.parentID) { diff --git a/JoyKit/JOYElement.h b/JoyKit/JOYElement.h index 860c247..4a6c311 100644 --- a/JoyKit/JOYElement.h +++ b/JoyKit/JOYElement.h @@ -10,6 +10,7 @@ @property (readonly) uint16_t usage; @property (readonly) uint16_t usagePage; @property (readonly) uint32_t uniqueID; +@property unsigned persistentUniqueID; @property int32_t min; @property int32_t max; @property (readonly) int32_t reportID; diff --git a/JoyKit/JOYSubElement.m b/JoyKit/JOYSubElement.m index 55e289e..01b121a 100644 --- a/JoyKit/JOYSubElement.m +++ b/JoyKit/JOYSubElement.m @@ -34,6 +34,7 @@ _usage = usage; _usagePage = usagePage; _uniqueID = (uint32_t)((_parent.uniqueID << 16) | offset); + self.persistentUniqueID = (uint32_t)((_parent.persistentUniqueID << 16) | offset); _min = min; _max = max; _reportID = _parent.reportID; From 40562b1c54176bc52021ba6bfed56a1dcbd4b94e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 00:25:40 +0300 Subject: [PATCH 265/341] Revert "Fix WUP-028 on Catalina, make controller configuration compatible between macOS versions" This reverts commit 160282c42af29f80b7e271e319015a60d2ad6e30. --- JoyKit/ControllerConfiguration.inc | 4 ++-- JoyKit/JOYAxes2D.m | 2 +- JoyKit/JOYAxis.m | 2 +- JoyKit/JOYButton.m | 2 +- JoyKit/JOYController.m | 6 +----- JoyKit/JOYElement.h | 1 - JoyKit/JOYSubElement.m | 1 - 7 files changed, 6 insertions(+), 12 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index 6c81e9f..cf327f9 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -126,14 +126,14 @@ hacksByName = @{ JOYSubElementStructs: @{ // Rumble - @(1357): @[ + @(1364): @[ @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(2), @"size":@1, @"offset":@1, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(3), @"size":@1, @"offset":@2, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(4), @"size":@1, @"offset":@3, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, ], - @(4): @[ + @(11): @[ // Player 1 diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m index a1b91d2..624ccef 100644 --- a/JoyKit/JOYAxes2D.m +++ b/JoyKit/JOYAxes2D.m @@ -36,7 +36,7 @@ - (uint64_t)uniqueID { - return _element1.persistentUniqueID; + return _element1.uniqueID; } - (NSString *)description diff --git a/JoyKit/JOYAxis.m b/JoyKit/JOYAxis.m index 74a4563..169eaee 100644 --- a/JoyKit/JOYAxis.m +++ b/JoyKit/JOYAxis.m @@ -40,7 +40,7 @@ - (uint64_t)uniqueID { - return _element.persistentUniqueID; + return _element.uniqueID; } - (NSString *)description diff --git a/JoyKit/JOYButton.m b/JoyKit/JOYButton.m index a904299..3e6026d 100644 --- a/JoyKit/JOYButton.m +++ b/JoyKit/JOYButton.m @@ -50,7 +50,7 @@ - (uint64_t)uniqueID { - return _element.persistentUniqueID; + return _element.uniqueID; } - (NSString *)description diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 10ec6e4..b4810ab 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -173,17 +173,13 @@ typedef struct __attribute__((packed)) { JOYElement *previousAxisElement = nil; id previous = nil; - unsigned persistentUniqueID = 0; for (id _element in array) { if (_element == previous) continue; // Some elements are reported twice for some reason previous = _element; NSArray *elements = nil; JOYElement *element = [[JOYElement alloc] initWithElement:(__bridge IOHIDElementRef)_element]; - /* Cookie is not persistent across macOS versions because Apple added kIOHIDElementTypeInput_NULL - in a backwards incompatible manner. We must maintain our own cookie-like ID. */ - element.persistentUniqueID = persistentUniqueID++; - NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.persistentUniqueID)]; + NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.uniqueID)]; bool isOutput = false; if (subElementDefs && element.uniqueID != element.parentID) { diff --git a/JoyKit/JOYElement.h b/JoyKit/JOYElement.h index 4a6c311..860c247 100644 --- a/JoyKit/JOYElement.h +++ b/JoyKit/JOYElement.h @@ -10,7 +10,6 @@ @property (readonly) uint16_t usage; @property (readonly) uint16_t usagePage; @property (readonly) uint32_t uniqueID; -@property unsigned persistentUniqueID; @property int32_t min; @property int32_t max; @property (readonly) int32_t reportID; diff --git a/JoyKit/JOYSubElement.m b/JoyKit/JOYSubElement.m index 01b121a..55e289e 100644 --- a/JoyKit/JOYSubElement.m +++ b/JoyKit/JOYSubElement.m @@ -34,7 +34,6 @@ _usage = usage; _usagePage = usagePage; _uniqueID = (uint32_t)((_parent.uniqueID << 16) | offset); - self.persistentUniqueID = (uint32_t)((_parent.persistentUniqueID << 16) | offset); _min = min; _max = max; _reportID = _parent.reportID; From 5da80062d9de17a4fdcfdb993021da4c27115cc0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 00:34:00 +0300 Subject: [PATCH 266/341] Fix WUP-028 on Catalina, make controller configuration compatible between macOS versions --- JoyKit/JOYElement.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/JoyKit/JOYElement.m b/JoyKit/JOYElement.m index 56fcb36..6539c2e 100644 --- a/JoyKit/JOYElement.m +++ b/JoyKit/JOYElement.m @@ -41,6 +41,24 @@ IOHIDElementRef parent = IOHIDElementGetParent(element); _parentID = parent? (uint32_t)IOHIDElementGetCookie(parent) : -1; _device = IOHIDElementGetDevice(element); + + /* Catalina added a new input type in a way that breaks cookie consistency across macOS versions, + we shall adjust our cookies to to compensate */ + unsigned cookieShift = 0, parentCookieShift = 0; + NSArray *nones = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(IOHIDElementGetDevice(element), + (__bridge CFDictionaryRef)@{@(kIOHIDElementTypeKey): @(kIOHIDElementTypeInput_NULL)}, + 0)); + for (id none in nones) { + if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < _uniqueID) { + cookieShift++; + } + if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < (int32_t)_parentID) { + parentCookieShift++; + } + } + + _uniqueID -= cookieShift; + _parentID -= parentCookieShift; } return self; } From ea18ba9335044c9d6b5d47ccc5adc8df1e2e1a01 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 15:50:22 +0300 Subject: [PATCH 267/341] Add rumble settings to libretro --- libretro/libretro.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 4831065..2baf354 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -211,6 +211,7 @@ static const struct retro_variable vars_single[] = { { "sameboy_high_pass_filter_mode", "High-pass filter; off|accurate|remove dc offset" }, { "sameboy_model", "Emulated model; Auto|Game Boy|Game Boy Color|Game Boy Advance|Super Game Boy|Super Game Boy 2" }, { "sameboy_border", "Super Game Boy border; enabled|disabled" }, + { "sameboy_rumble", "Enable rumble; never|rumble-enabled games|all games" }, { NULL } }; @@ -226,6 +227,8 @@ static const struct retro_variable vars_dual[] = { { "sameboy_color_correction_mode_2", "Color correction for Game Boy #2; off|correct curves|emulate hardware|preserve brightness" }, { "sameboy_high_pass_filter_mode_1", "High-pass filter for Game Boy #1; off|accurate|remove dc offset" }, { "sameboy_high_pass_filter_mode_2", "High-pass filter for Game Boy #2; off|accurate|remove dc offset" }, + { "sameboy_rumble_1", "Enable rumble for Game Boy #1; never|rumble-enabled games|all games" }, + { "sameboy_rumble_2", "Enable rumble for Game Boy #2; never|rumble-enabled games|all games" }, { NULL } }; @@ -497,9 +500,20 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce_contrast") == 0) + else if (strcmp(var.value, "reduce contrast") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); } + + var.key = "sameboy_rumble"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { + if (strcmp(var.value, "never") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED); + else if (strcmp(var.value, "rumble-enabled games") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY); + else if (strcmp(var.value, "all games") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES); + } var.key = "sameboy_high_pass_filter_mode"; var.value = NULL; @@ -557,7 +571,7 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce_contrast") == 0) + else if (strcmp(var.value, "reduce contrast") == 0) GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); } @@ -572,10 +586,32 @@ static void check_variables() GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_EMULATE_HARDWARE); else if (strcmp(var.value, "preserve brightness") == 0) GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce_contrast") == 0) + else if (strcmp(var.value, "reduce contrast") == 0) GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_REDUCE_CONTRAST); } + + var.key = "sameboy_rumble_1"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { + if (strcmp(var.value, "never") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED); + else if (strcmp(var.value, "rumble-enabled games") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY); + else if (strcmp(var.value, "all games") == 0) + GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES); + } + + var.key = "sameboy_rumble_2"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { + if (strcmp(var.value, "never") == 0) + GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_DISABLED); + else if (strcmp(var.value, "rumble-enabled games") == 0) + GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_CARTRIDGE_ONLY); + else if (strcmp(var.value, "all games") == 0) + GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_ALL_GAMES); + } var.key = "sameboy_high_pass_filter_mode_1"; var.value = NULL; From 5a56c3b8825719481f6eb2a779ea96392c37cbcd Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 16:03:26 +0300 Subject: [PATCH 268/341] Style fixes --- Core/gb.c | 19 +-- Shaders/HQ2x.fsh | 42 ++++-- Shaders/OmniScale.fsh | 39 +++-- libretro/libretro.c | 321 ++++++++++++++++++++++++++++-------------- 4 files changed, 274 insertions(+), 147 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index a17a5a7..a1f573c 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -349,8 +349,7 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) #endif size_t needed_size = bank * 0x4000 + address + length; - if (needed_size > 1024 * 1024 * 32) - goto error; + if (needed_size > 1024 * 1024 * 32) goto error; if (gb->rom_size < needed_size) { gb->rom = realloc(gb->rom, needed_size); @@ -358,8 +357,7 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) gb->rom_size = needed_size; } - if (fread(gb->rom + (bank * 0x4000 + address), length, 1, f) != 1) - goto error; + if (fread(gb->rom + (bank * 0x4000 + address), length, 1, f) != 1) goto error; break; } @@ -378,16 +376,15 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) length = __builtin_bswap32(length); #endif size_t needed_size = address + length; - if (needed_size > 1024 * 1024 * 32) - goto error; + if (needed_size > 1024 * 1024 * 32) goto error; + if (gb->rom_size < needed_size) { gb->rom = realloc(gb->rom, needed_size); memset(gb->rom + gb->rom_size, 0, needed_size - gb->rom_size); gb->rom_size = needed_size; } - if (fread(gb->rom + address, length, 1, f) != 1) - goto error; + if (fread(gb->rom + address, length, 1, f) != 1) goto error; break; } @@ -406,8 +403,7 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) #endif while (count--) { READ(length); - if (fread(name, length, 1, f) != 1) - goto error; + if (fread(name, length, 1, f) != 1) goto error; name[length] = 0; READ(flag); // unused @@ -439,8 +435,7 @@ int GB_load_isx(GB_gameboy_t *gb, const char *path) #endif while (count--) { READ(length); - if (fread(name, length + 1, 1, f) != 1) - goto error; + if (fread(name, length + 1, 1, f) != 1) goto error; name[length] = 0; READ(flag); // unused diff --git a/Shaders/HQ2x.fsh b/Shaders/HQ2x.fsh index 3871db9..2e19fa6 100644 --- a/Shaders/HQ2x.fsh +++ b/Shaders/HQ2x.fsh @@ -62,40 +62,54 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou if (is_different(w7, w4)) pattern |= 64; if (is_different(w8, w4)) pattern |= 128; - if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5)) + if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5)) { return interp_2px(w4, 3.0, w3, 1.0); - if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3)) + } + if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3)) { return interp_2px(w4, 3.0, w1, 1.0); - if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1)) + } + if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1)) { return w4; + } if ((P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) || P(0xdf,0x5a) || P(0x9f,0x8a) || P(0xcf,0x8a) || P(0xef,0x4e) || P(0x3f,0x0e) || P(0xfb,0x5a) || P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) || - P(0xeb,0x8a)) && is_different(w3, w1)) + P(0xeb,0x8a)) && is_different(w3, w1)) { return interp_2px(w4, 3.0, w0, 1.0); - if (P(0x0b,0x08)) + } + if (P(0x0b,0x08)) { return interp_3px(w4, 2.0, w0, 1.0, w1, 1.0); - if (P(0x0b,0x02)) + } + if (P(0x0b,0x02)) { return interp_3px(w4, 2.0, w0, 1.0, w3, 1.0); - if (P(0x2f,0x2f)) + } + if (P(0x2f,0x2f)) { return interp_3px(w4, 1.04, w3, 1.0, w1, 1.0); - if (P(0xbf,0x37) || P(0xdb,0x13)) + } + if (P(0xbf,0x37) || P(0xdb,0x13)) { return interp_3px(w4, 5.0, w1, 2.0, w3, 1.0); - if (P(0xdb,0x49) || P(0xef,0x6d)) + } + if (P(0xdb,0x49) || P(0xef,0x6d)) { return interp_3px(w4, 5.0, w3, 2.0, w1, 1.0); - if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43)) + } + if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43)) { return interp_2px(w4, 3.0, w3, 1.0); - if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19)) + } + if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19)) { return interp_2px(w4, 3.0, w1, 1.0); - if (P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7e,0x0e)) + } + if (P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7e,0x0e)) { return interp_3px(w4, 2.0, w3, 3.0, w1, 3.0); + } if (P(0xfb,0x6a) || P(0x6f,0x6e) || P(0x3f,0x3e) || P(0xfb,0xfa) || - P(0xdf,0xde) || P(0xdf,0x1e)) + P(0xdf,0xde) || P(0xdf,0x1e)) { return interp_2px(w4, 3.0, w0, 1.0); + } if (P(0x0a,0x00) || P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) || P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) || - P(0x3b,0x1b)) + P(0x3b,0x1b)) { return interp_3px(w4, 2.0, w3, 1.0, w1, 1.0); + } return interp_3px(w4, 6.0, w3, 1.0, w1, 1.0); } diff --git a/Shaders/OmniScale.fsh b/Shaders/OmniScale.fsh index bb2b7d6..c76f736 100644 --- a/Shaders/OmniScale.fsh +++ b/Shaders/OmniScale.fsh @@ -63,21 +63,27 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou if (is_different(w7, w4)) pattern |= 1 << 6; if (is_different(w8, w4)) pattern |= 1 << 7; - if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5)) + if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5)) { return mix(w4, w3, 0.5 - p.x); - if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3)) + } + if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3)) { return mix(w4, w1, 0.5 - p.y); - if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1)) + } + if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1)) { return w4; + } if ((P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) || P(0xdf,0x5a) || P(0x9f,0x8a) || P(0xcf,0x8a) || P(0xef,0x4e) || P(0x3f,0x0e) || P(0xfb,0x5a) || P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) || - P(0xeb,0x8a)) && is_different(w3, w1)) + P(0xeb,0x8a)) && is_different(w3, w1)) { return mix(w4, mix(w4, w0, 0.5 - p.x), 0.5 - p.y); - if (P(0x0b,0x08)) + } + if (P(0x0b,0x08)) { return mix(mix(w0 * 0.375 + w1 * 0.25 + w4 * 0.375, w4 * 0.5 + w1 * 0.5, p.x * 2.0), w4, p.y * 2.0); - if (P(0x0b,0x02)) + } + if (P(0x0b,0x02)) { return mix(mix(w0 * 0.375 + w3 * 0.25 + w4 * 0.375, w4 * 0.5 + w3 * 0.5, p.y * 2.0), w4, p.x * 2.0); + } if (P(0x2f,0x2f)) { float dist = length(p - vec2(0.5)); float pixel_size = length(1.0 / (output_resolution / input_resolution)); @@ -142,7 +148,6 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou } return mix(r, w4, (dist + pixel_size / 2 - 1.0) / pixel_size); - } if (P(0x7e,0x2a) || P(0xef,0xab)) { @@ -169,15 +174,18 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou return mix(r, w4, (dist + pixel_size / 2 - 1.0) / pixel_size); } - if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43)) + if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43)) { return mix(w4, w3, 0.5 - p.x); + } - if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19)) + if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19)) { return mix(w4, w1, 0.5 - p.y); + } if (P(0xfb,0x6a) || P(0x6f,0x6e) || P(0x3f,0x3e) || P(0xfb,0xfa) || - P(0xdf,0xde) || P(0xdf,0x1e)) + P(0xdf,0xde) || P(0xdf,0x1e)) { return mix(w4, w0, (1.0 - p.x - p.y) / 2.0); + } if (P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) || P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) || @@ -204,17 +212,20 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou return mix(r, w4, (dist + pixel_size / 2 - 0.5) / pixel_size); } - if (P(0x0b,0x01)) + if (P(0x0b,0x01)) { return mix(mix(w4, w3, 0.5 - p.x), mix(w1, (w1 + w3) / 2.0, 0.5 - p.x), 0.5 - p.y); + } - if (P(0x0b,0x00)) + if (P(0x0b,0x00)) { return mix(mix(w4, w3, 0.5 - p.x), mix(w1, w0, 0.5 - p.x), 0.5 - p.y); + } float dist = p.x + p.y; float pixel_size = length(1.0 / (output_resolution / input_resolution)); - if (dist > 0.5 + pixel_size / 2) + if (dist > 0.5 + pixel_size / 2) { return w4; + } /* We need more samples to "solve" this diagonal */ vec4 x0 = texture(image, position + vec2( -o.x * 2.0, -o.y * 2.0)); @@ -239,7 +250,7 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou pattern >>= 1; } - if (diagonal_bias <= 0) { + if (diagonal_bias <= 0) { vec4 r = mix(w1, w3, p.y - p.x + 0.5); if (dist < 0.5 - pixel_size / 2) { return r; diff --git a/libretro/libretro.c b/libretro/libretro.c index 2baf354..fa478f5 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -370,8 +370,9 @@ static void init_for_current_model(unsigned id) snprintf(buf, sizeof(buf), "%s%c%s_boot.bin", retro_system_directory, slash, model_name); log_cb(RETRO_LOG_INFO, "Initializing as model: %s\n", model_name); log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - if (GB_load_boot_rom(&gameboy[i], buf)) + if (GB_load_boot_rom(&gameboy[i], buf)) { GB_load_boot_rom_from_buffer(&gameboy[i], boot_code, boot_length); + } GB_set_user_data(&gameboy[i], (void*)NULL); GB_set_pixels_output(&gameboy[i],(unsigned int*)(frame_buf + i * ((model[i] == MODEL_SGB || model[i] == MODEL_SGB2) ? SGB_VIDEO_PIXELS : VIDEO_PIXELS))); @@ -386,8 +387,9 @@ static void init_for_current_model(unsigned id) GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); if (emulated_devices == 2) { GB_set_vblank_callback(&gameboy[1], (GB_vblank_callback_t) vblank2); - if (link_cable_emulation) + if (link_cable_emulation) { set_link_cable_state(true); + } } struct retro_memory_descriptor descs[11]; @@ -492,56 +494,73 @@ static void check_variables() var.key = "sameboy_color_correction_mode"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); - else if (strcmp(var.value, "correct curves") == 0) + } + else if (strcmp(var.value, "correct curves") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_CORRECT_CURVES); - else if (strcmp(var.value, "emulate hardware") == 0) + } + else if (strcmp(var.value, "emulate hardware") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); - else if (strcmp(var.value, "preserve brightness") == 0) + } + else if (strcmp(var.value, "preserve brightness") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce contrast") == 0) + } + else if (strcmp(var.value, "reduce contrast") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); + } } var.key = "sameboy_rumble"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "never") == 0) + if (strcmp(var.value, "never") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED); - else if (strcmp(var.value, "rumble-enabled games") == 0) + } + else if (strcmp(var.value, "rumble-enabled games") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY); - else if (strcmp(var.value, "all games") == 0) + } + else if (strcmp(var.value, "all games") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES); + } } var.key = "sameboy_high_pass_filter_mode"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF); - else if (strcmp(var.value, "accurate") == 0) + } + else if (strcmp(var.value, "accurate") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_ACCURATE); - else if (strcmp(var.value, "remove dc offset") == 0) + } + else if (strcmp(var.value, "remove dc offset") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_REMOVE_DC_OFFSET); + } } var.key = "sameboy_model"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[0]; - if (strcmp(var.value, "Game Boy") == 0) + if (strcmp(var.value, "Game Boy") == 0) { new_model = MODEL_DMG; - else if (strcmp(var.value, "Game Boy Color") == 0) + } + else if (strcmp(var.value, "Game Boy Color") == 0) { new_model = MODEL_CGB; - else if (strcmp(var.value, "Game Boy Advance") == 0) + } + else if (strcmp(var.value, "Game Boy Advance") == 0) { new_model = MODEL_AGB; - else if (strcmp(var.value, "Super Game Boy") == 0) + } + else if (strcmp(var.value, "Super Game Boy") == 0) { new_model = MODEL_SGB; - else if (strcmp(var.value, "Super Game Boy 2") == 0) + } + else if (strcmp(var.value, "Super Game Boy 2") == 0) { new_model = MODEL_SGB2; - else + } + else { new_model = MODEL_AUTO; + } if (new_model != model[0]) { geometry_updated = true; @@ -553,104 +572,134 @@ static void check_variables() var.key = "sameboy_border"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "enabled") == 0) + if (strcmp(var.value, "enabled") == 0) { sgb_border = 1; - else if (strcmp(var.value, "disabled") == 0) + } + else if (strcmp(var.value, "disabled") == 0) { sgb_border = 0; + } } } else { var.key = "sameboy_color_correction_mode_1"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_DISABLED); - else if (strcmp(var.value, "correct curves") == 0) + } + else if (strcmp(var.value, "correct curves") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_CORRECT_CURVES); - else if (strcmp(var.value, "emulate hardware") == 0) + } + else if (strcmp(var.value, "emulate hardware") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); - else if (strcmp(var.value, "preserve brightness") == 0) + } + else if (strcmp(var.value, "preserve brightness") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce contrast") == 0) + } + else if (strcmp(var.value, "reduce contrast") == 0) { GB_set_color_correction_mode(&gameboy[0], GB_COLOR_CORRECTION_REDUCE_CONTRAST); + } } var.key = "sameboy_color_correction_mode_2"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_DISABLED); - else if (strcmp(var.value, "correct curves") == 0) + } + else if (strcmp(var.value, "correct curves") == 0) { GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_CORRECT_CURVES); - else if (strcmp(var.value, "emulate hardware") == 0) + } + else if (strcmp(var.value, "emulate hardware") == 0) { GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_EMULATE_HARDWARE); - else if (strcmp(var.value, "preserve brightness") == 0) + } + else if (strcmp(var.value, "preserve brightness") == 0) { GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - else if (strcmp(var.value, "reduce contrast") == 0) + } + else if (strcmp(var.value, "reduce contrast") == 0) { GB_set_color_correction_mode(&gameboy[1], GB_COLOR_CORRECTION_REDUCE_CONTRAST); + } } var.key = "sameboy_rumble_1"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "never") == 0) + if (strcmp(var.value, "never") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_DISABLED); - else if (strcmp(var.value, "rumble-enabled games") == 0) + } + else if (strcmp(var.value, "rumble-enabled games") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_CARTRIDGE_ONLY); - else if (strcmp(var.value, "all games") == 0) + } + else if (strcmp(var.value, "all games") == 0) { GB_set_rumble_mode(&gameboy[0], GB_RUMBLE_ALL_GAMES); + } } var.key = "sameboy_rumble_2"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "never") == 0) + if (strcmp(var.value, "never") == 0) { GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_DISABLED); - else if (strcmp(var.value, "rumble-enabled games") == 0) + } + else if (strcmp(var.value, "rumble-enabled games") == 0) { GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_CARTRIDGE_ONLY); - else if (strcmp(var.value, "all games") == 0) + } + else if (strcmp(var.value, "all games") == 0) { GB_set_rumble_mode(&gameboy[1], GB_RUMBLE_ALL_GAMES); + } } var.key = "sameboy_high_pass_filter_mode_1"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_OFF); - else if (strcmp(var.value, "accurate") == 0) + } + else if (strcmp(var.value, "accurate") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_ACCURATE); - else if (strcmp(var.value, "remove dc offset") == 0) + } + else if (strcmp(var.value, "remove dc offset") == 0) { GB_set_highpass_filter_mode(&gameboy[0], GB_HIGHPASS_REMOVE_DC_OFFSET); + } } var.key = "sameboy_high_pass_filter_mode_2"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "off") == 0) + if (strcmp(var.value, "off") == 0) { GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_OFF); - else if (strcmp(var.value, "accurate") == 0) + } + else if (strcmp(var.value, "accurate") == 0) { GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_ACCURATE); - else if (strcmp(var.value, "remove dc offset") == 0) + } + else if (strcmp(var.value, "remove dc offset") == 0) { GB_set_highpass_filter_mode(&gameboy[1], GB_HIGHPASS_REMOVE_DC_OFFSET); + } } var.key = "sameboy_model_1"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[0]; - if (strcmp(var.value, "Game Boy") == 0) + if (strcmp(var.value, "Game Boy") == 0) { new_model = MODEL_DMG; - else if (strcmp(var.value, "Game Boy Color") == 0) + } + else if (strcmp(var.value, "Game Boy Color") == 0) { new_model = MODEL_CGB; - else if (strcmp(var.value, "Game Boy Advance") == 0) + } + else if (strcmp(var.value, "Game Boy Advance") == 0) { new_model = MODEL_AGB; - else if (strcmp(var.value, "Super Game Boy") == 0) + } + else if (strcmp(var.value, "Super Game Boy") == 0) { new_model = MODEL_SGB; - else if (strcmp(var.value, "Super Game Boy 2") == 0) + } + else if (strcmp(var.value, "Super Game Boy 2") == 0) { new_model = MODEL_SGB2; - else + } + else { new_model = MODEL_AUTO; + } if (model[0] != new_model) { model[0] = new_model; @@ -662,18 +711,24 @@ static void check_variables() var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { enum model new_model = model[1]; - if (strcmp(var.value, "Game Boy") == 0) + if (strcmp(var.value, "Game Boy") == 0) { new_model = MODEL_DMG; - else if (strcmp(var.value, "Game Boy Color") == 0) + } + else if (strcmp(var.value, "Game Boy Color") == 0) { new_model = MODEL_CGB; - else if (strcmp(var.value, "Game Boy Advance") == 0) + } + else if (strcmp(var.value, "Game Boy Advance") == 0) { new_model = MODEL_AGB; - else if (strcmp(var.value, "Super Game Boy") == 0) + } + else if (strcmp(var.value, "Super Game Boy") == 0) { new_model = MODEL_SGB; - else if (strcmp(var.value, "Super Game Boy 2") == 0) + } + else if (strcmp(var.value, "Super Game Boy 2") == 0) { new_model = MODEL_SGB; - else + } + else { new_model = MODEL_AUTO; + } if (model[1] != new_model) { model[1] = new_model; @@ -684,10 +739,12 @@ static void check_variables() var.key = "sameboy_screen_layout"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "top-down") == 0) + if (strcmp(var.value, "top-down") == 0) { screen_layout = LAYOUT_TOP_DOWN; - else + } + else { screen_layout = LAYOUT_LEFT_RIGHT; + } geometry_updated = true; } @@ -696,23 +753,29 @@ static void check_variables() var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { bool tmp = link_cable_emulation; - if (strcmp(var.value, "enabled") == 0) + if (strcmp(var.value, "enabled") == 0) { link_cable_emulation = true; - else + } + else { link_cable_emulation = false; - if (link_cable_emulation && link_cable_emulation != tmp) + } + if (link_cable_emulation && link_cable_emulation != tmp) { set_link_cable_state(true); - else if (!link_cable_emulation && link_cable_emulation != tmp) + } + else if (!link_cable_emulation && link_cable_emulation != tmp) { set_link_cable_state(false); + } } var.key = "sameboy_audio_output"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "Game Boy #1") == 0) + if (strcmp(var.value, "Game Boy #1") == 0) { audio_out = GB_1; - else + } + else { audio_out = GB_2; + } } } } @@ -721,20 +784,26 @@ void retro_init(void) { const char *dir = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) + if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) { snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir); - else + } + else { snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", "."); + } - if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) + if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) { snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir); - else + } + else { snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", "."); + } - if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) + if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) { log_cb = logging.log; - else + } + else { log_cb = fallback_log; + } } void retro_deinit(void) @@ -839,8 +908,9 @@ void retro_set_video_refresh(retro_video_refresh_t cb) void retro_reset(void) { - for (int i = 0; i < emulated_devices; i++) + for (int i = 0; i < emulated_devices; i++) { GB_reset(&gameboy[i]); + } } @@ -849,8 +919,9 @@ void retro_run(void) bool updated = false; - if (!initialized) + if (!initialized) { geometry_updated = false; + } if (geometry_updated) { struct retro_system_av_info info; @@ -859,22 +930,26 @@ void retro_run(void) geometry_updated = false; } - if (!frame_buf) + if (!frame_buf) { return; + } - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) { check_variables(); + } if (emulated_devices == 2) { GB_update_keys_status(&gameboy[0], 0); GB_update_keys_status(&gameboy[1], 1); } else if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) { - for (unsigned i = 0; i < 4; i++) + for (unsigned i = 0; i < 4; i++) { GB_update_keys_status(&gameboy[0], i); + } } - else + else { GB_update_keys_status(&gameboy[0], 0); + } vblank1_occurred = vblank2_occurred = false; signed delta = 0; @@ -911,16 +986,18 @@ void retro_run(void) } else { if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { - if (sgb_border == 1) + if (sgb_border == 1) { video_cb(frame_buf, SGB_VIDEO_WIDTH, SGB_VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); + } else { int crop = SGB_VIDEO_WIDTH * ((SGB_VIDEO_HEIGHT - VIDEO_HEIGHT) / 2) + ((SGB_VIDEO_WIDTH - VIDEO_WIDTH) / 2); video_cb(frame_buf + crop, VIDEO_WIDTH, VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); } } - else + else { video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); + } } @@ -955,10 +1032,12 @@ bool retro_load_game(const struct retro_game_info *info) bool achievements = true; environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &achievements); - if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) + if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) { log_cb(RETRO_LOG_INFO, "Rumble environment supported\n"); - else + } + else { log_cb(RETRO_LOG_INFO, "Rumble environment not supported\n"); + } check_variables(); return true; @@ -966,8 +1045,9 @@ bool retro_load_game(const struct retro_game_info *info) void retro_unload_game(void) { - for (int i = 0; i < emulated_devices; i++) + for (int i = 0; i < emulated_devices; i++) { GB_free(&gameboy[i]); + } } unsigned retro_get_region(void) @@ -978,10 +1058,12 @@ unsigned retro_get_region(void) bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num_info) { - if (type == RETRO_GAME_TYPE_GAMEBOY_LINK_2P) + if (type == RETRO_GAME_TYPE_GAMEBOY_LINK_2P) { emulated_devices = 2; - else + } + else { return false; /* all other types are unhandled for now */ + } environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_dual); check_variables(); @@ -1012,10 +1094,12 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, bool achievements = true; environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &achievements); - if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) + if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) { log_cb(RETRO_LOG_INFO, "Rumble environment supported\n"); - else + } + else { log_cb(RETRO_LOG_INFO, "Rumble environment not supported\n"); + } check_variables(); return true; @@ -1048,8 +1132,9 @@ size_t retro_serialize_size(void) bool retro_serialize(void *data, size_t size) { - if (!initialized || !data) + if (!initialized || !data) { return false; + } size_t offset = 0; @@ -1096,19 +1181,23 @@ void *retro_get_memory_data(unsigned type) data = gameboy[0].ram; break; case RETRO_MEMORY_SAVE_RAM: - if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) + if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) { data = gameboy[0].mbc_ram; - else + } + else { data = NULL; + } break; case RETRO_MEMORY_VIDEO_RAM: data = gameboy[0].vram; break; case RETRO_MEMORY_RTC: - if (gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) { data = GB_GET_SECTION(&gameboy[0], rtc); - else + } + else { data = NULL; + } break; default: break; @@ -1117,28 +1206,36 @@ void *retro_get_memory_data(unsigned type) else { switch (type) { case RETRO_MEMORY_GAMEBOY_1_SRAM: - if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) + if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) { data = gameboy[0].mbc_ram; - else + } + else { data = NULL; + } break; case RETRO_MEMORY_GAMEBOY_2_SRAM: - if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) + if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) { data = gameboy[1].mbc_ram; - else + } + else { data = NULL; + } break; case RETRO_MEMORY_GAMEBOY_1_RTC: - if (gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) { data = GB_GET_SECTION(&gameboy[0], rtc); - else + } + else { data = NULL; + } break; case RETRO_MEMORY_GAMEBOY_2_RTC: - if (gameboy[1].cartridge_type->has_battery) + if (gameboy[1].cartridge_type->has_battery) { data = GB_GET_SECTION(&gameboy[1], rtc); - else + } + else { data = NULL; + } break; default: break; @@ -1157,19 +1254,23 @@ size_t retro_get_memory_size(unsigned type) size = gameboy[0].ram_size; break; case RETRO_MEMORY_SAVE_RAM: - if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) + if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) { size = gameboy[0].mbc_ram_size; - else + } + else { size = 0; + } break; case RETRO_MEMORY_VIDEO_RAM: size = gameboy[0].vram_size; break; case RETRO_MEMORY_RTC: - if (gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) { size = GB_SECTION_SIZE(rtc); - else + } + else { size = 0; + } break; default: break; @@ -1178,24 +1279,30 @@ size_t retro_get_memory_size(unsigned type) else { switch (type) { case RETRO_MEMORY_GAMEBOY_1_SRAM: - if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) + if (gameboy[0].cartridge_type->has_battery && gameboy[0].mbc_ram_size != 0) { size = gameboy[0].mbc_ram_size; - else + } + else { size = 0; + } break; case RETRO_MEMORY_GAMEBOY_2_SRAM: - if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) + if (gameboy[1].cartridge_type->has_battery && gameboy[1].mbc_ram_size != 0) { size = gameboy[1].mbc_ram_size; - else + } + else { size = 0; + } break; case RETRO_MEMORY_GAMEBOY_1_RTC: - if (gameboy[0].cartridge_type->has_battery) + if (gameboy[0].cartridge_type->has_battery) { size = GB_SECTION_SIZE(rtc); + } break; case RETRO_MEMORY_GAMEBOY_2_RTC: - if (gameboy[1].cartridge_type->has_battery) + if (gameboy[1].cartridge_type->has_battery) { size = GB_SECTION_SIZE(rtc); + } break; default: break; From 4bf252800e562545dc9457be461b356144036e52 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:08:44 +0300 Subject: [PATCH 269/341] Improve PWM quality, fix a crash --- JoyKit/JOYController.m | 37 +++++++++++++++++++++++-------------- JoyKit/JOYElement.h | 4 ++-- JoyKit/JOYElement.m | 10 ++++++---- JoyKit/JOYSubElement.m | 17 +++++++++-------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index b4810ab..f9f0775 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -5,6 +5,8 @@ #import "JOYEmulatedButton.h" #include +#define PWM_RESOLUTION 16 + static NSString const *JOYAxisGroups = @"JOYAxisGroups"; static NSString const *JOYReportIDFilters = @"JOYReportIDFilters"; static NSString const *JOYButtonUsageMapping = @"JOYButtonUsageMapping"; @@ -130,7 +132,7 @@ typedef struct __attribute__((packed)) { _physicallyConnected = true; _logicallyConnected = true; - _device = device; + _device = (IOHIDDeviceRef)CFRetain(device); _serialSuffix = suffix; IOHIDDeviceRegisterInputValueCallback(device, HIDInput, (void *)self); @@ -603,11 +605,17 @@ typedef struct __attribute__((packed)) { - (void)pwmThread { - while (_rumblePWMRatio != 0) { - [_rumbleElement setValue:1]; - [NSThread sleepForTimeInterval:_rumblePWMRatio / 10]; - [_rumbleElement setValue:0]; - [NSThread sleepForTimeInterval:(1 - _rumblePWMRatio) / 10]; + /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller + uses rumble. */ + unsigned rumbleCounter = 0; + while (self.connected) { + if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { + break; + } + rumbleCounter += round(_rumblePWMRatio * PWM_RESOLUTION); + if (rumbleCounter >= PWM_RESOLUTION) { + rumbleCounter -= PWM_RESOLUTION; + } } [_rumblePWMThreadLock lock]; _rumblePWMThreadRunning = false; @@ -659,6 +667,7 @@ typedef struct __attribute__((packed)) { else { if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { [_rumblePWMThreadLock lock]; + _rumblePWMRatio = amp; if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. if (amp != 0) { _rumblePWMRatio = amp; @@ -666,14 +675,6 @@ typedef struct __attribute__((packed)) { [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; } } - else { - if (amp == 0) { // Thread is running, signal it to stop - _rumblePWMRatio = 0; - } - else { - _rumblePWMRatio = amp; - } - } [_rumblePWMThreadLock unlock]; } else { @@ -771,4 +772,12 @@ typedef struct __attribute__((packed)) { IOHIDManagerRegisterDeviceRemovalCallback(manager, HIDDeviceRemoved, NULL); IOHIDManagerScheduleWithRunLoop(manager, [runloop getCFRunLoop], kCFRunLoopDefaultMode); } + +- (void)dealloc +{ + if (_device) { + CFRelease(_device); + _device = NULL; + } +} @end diff --git a/JoyKit/JOYElement.h b/JoyKit/JOYElement.h index 860c247..0e917dd 100644 --- a/JoyKit/JOYElement.h +++ b/JoyKit/JOYElement.h @@ -5,8 +5,8 @@ - (instancetype)initWithElement:(IOHIDElementRef)element; - (int32_t)value; - (NSData *)dataValue; -- (void)setValue:(uint32_t)value; -- (void)setDataValue:(NSData *)value; +- (IOReturn)setValue:(uint32_t)value; +- (IOReturn)setDataValue:(NSData *)value; @property (readonly) uint16_t usage; @property (readonly) uint16_t usagePage; @property (readonly) uint32_t uniqueID; diff --git a/JoyKit/JOYElement.m b/JoyKit/JOYElement.m index 6539c2e..4050312 100644 --- a/JoyKit/JOYElement.m +++ b/JoyKit/JOYElement.m @@ -81,18 +81,20 @@ return [NSData dataWithBytes:IOHIDValueGetBytePtr(value) length:IOHIDValueGetLength(value)]; } -- (void)setValue:(uint32_t)value +- (IOReturn)setValue:(uint32_t)value { IOHIDValueRef ivalue = IOHIDValueCreateWithIntegerValue(NULL, (__bridge IOHIDElementRef)_element, 0, value); - IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); + IOReturn ret = IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); CFRelease(ivalue); + return ret; } -- (void)setDataValue:(NSData *)value +- (IOReturn)setDataValue:(NSData *)value { IOHIDValueRef ivalue = IOHIDValueCreateWithBytes(NULL, (__bridge IOHIDElementRef)_element, 0, value.bytes, value.length); - IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); + IOReturn ret = IOHIDDeviceSetValue(_device, (__bridge IOHIDElementRef)_element, ivalue); CFRelease(ivalue); + return ret; } /* For use as a dictionary key */ diff --git a/JoyKit/JOYSubElement.m b/JoyKit/JOYSubElement.m index 55e289e..c94badc 100644 --- a/JoyKit/JOYSubElement.m +++ b/JoyKit/JOYSubElement.m @@ -65,15 +65,15 @@ return ret; } -- (void)setValue: (uint32_t) value +- (IOReturn)setValue: (uint32_t) value { NSMutableData *dataValue = [[_parent dataValue] mutableCopy]; - if (!dataValue) return; - if (_size > 32) return; - if (_size + (_offset % 8) > 32) return; + if (!dataValue) return -1; + if (_size > 32) return -1; + if (_size + (_offset % 8) > 32) return -1; size_t parentLength = dataValue.length; - if (_size > parentLength * 8) return; - if (_size + _offset >= parentLength * 8) return; + if (_size > parentLength * 8) return -1; + if (_size + _offset >= parentLength * 8) return -1; uint8_t *bytes = dataValue.mutableBytes; uint8_t temp[4] = {0,}; @@ -81,7 +81,7 @@ (*(uint32_t *)temp) &= ~((1 << (_size - 1)) << (_offset % 8)); (*(uint32_t *)temp) |= (value) << (_offset % 8); memcpy(bytes + _offset / 8, temp, (_offset + _size - 1) / 8 - _offset / 8 + 1); - [_parent setDataValue:dataValue]; + return [_parent setDataValue:dataValue]; } - (NSData *)dataValue @@ -90,9 +90,10 @@ return nil; } -- (void)setDataValue:(NSData *)data +- (IOReturn)setDataValue:(NSData *)data { [self doesNotRecognizeSelector:_cmd]; + return -1; } From 021cdb402dd837c1002c7a296a638c3c2f1e8f61 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:16:33 +0300 Subject: [PATCH 270/341] Various hacks for stopping the PWM thread when needed, important if we have a WUP-028 connected with more than one controller --- Cocoa/Document.m | 2 +- Cocoa/GBView.m | 2 ++ JoyKit/JOYController.h | 1 + JoyKit/JOYController.m | 13 ++++++++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 90a1dc8..ff47cd9 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -381,7 +381,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); - [_view setRumble:false]; + [_view setRumble:0]; stopping = false; } diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index e733731..d342497 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -116,6 +116,7 @@ } [[NSNotificationCenter defaultCenter] removeObserver:self]; [lastController setRumbleAmplitude:0]; + [lastController _forceStopPWMThread]; [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder @@ -302,6 +303,7 @@ if (![self.window isMainWindow]) return; if (controller != lastController) { [lastController setRumbleAmplitude:0]; + [lastController _forceStopPWMThread]; lastController = controller; } diff --git a/JoyKit/JOYController.h b/JoyKit/JOYController.h index 9ed7cf7..9363e36 100644 --- a/JoyKit/JOYController.h +++ b/JoyKit/JOYController.h @@ -35,6 +35,7 @@ static NSString const *JOYHatsEmulateButtonsKey = @"JOYHatsEmulateButtons"; - (NSArray *) hats; - (void)setRumbleAmplitude:(double)amp; - (void)setPlayerLEDs:(uint8_t)mask; +- (void)_forceStopPWMThread; // Hack @property (readonly, getter=isConnected) bool connected; @end diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index f9f0775..0268054 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -118,6 +118,7 @@ typedef struct __attribute__((packed)) { bool _physicallyConnected; bool _logicallyConnected; bool _rumblePWMThreadRunning; + volatile bool _forceStopPWMThread; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device @@ -608,7 +609,7 @@ typedef struct __attribute__((packed)) { /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller uses rumble. */ unsigned rumbleCounter = 0; - while (self.connected) { + while (self.connected && !_forceStopPWMThread) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { break; } @@ -619,6 +620,7 @@ typedef struct __attribute__((packed)) { } [_rumblePWMThreadLock lock]; _rumblePWMThreadRunning = false; + _forceStopPWMThread = false; [_rumblePWMThreadLock unlock]; } @@ -688,6 +690,15 @@ typedef struct __attribute__((packed)) { return _logicallyConnected && _physicallyConnected; } +- (void)_forceStopPWMThread +{ + [_rumblePWMThreadLock lock]; + if (_rumblePWMThreadRunning) { + _forceStopPWMThread = true; + } + [_rumblePWMThreadLock unlock]; +} + + (void)controllerAdded:(IOHIDDeviceRef) device { NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); From 285457852798cd1738a6f98068c4920d96ebdb98 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:26:48 +0300 Subject: [PATCH 271/341] Less ugly hacks --- Cocoa/GBView.m | 2 -- JoyKit/JOYController.h | 1 - JoyKit/JOYController.m | 8 +++++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index d342497..e733731 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -116,7 +116,6 @@ } [[NSNotificationCenter defaultCenter] removeObserver:self]; [lastController setRumbleAmplitude:0]; - [lastController _forceStopPWMThread]; [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder @@ -303,7 +302,6 @@ if (![self.window isMainWindow]) return; if (controller != lastController) { [lastController setRumbleAmplitude:0]; - [lastController _forceStopPWMThread]; lastController = controller; } diff --git a/JoyKit/JOYController.h b/JoyKit/JOYController.h index 9363e36..9ed7cf7 100644 --- a/JoyKit/JOYController.h +++ b/JoyKit/JOYController.h @@ -35,7 +35,6 @@ static NSString const *JOYHatsEmulateButtonsKey = @"JOYHatsEmulateButtons"; - (NSArray *) hats; - (void)setRumbleAmplitude:(double)amp; - (void)setPlayerLEDs:(uint8_t)mask; -- (void)_forceStopPWMThread; // Hack @property (readonly, getter=isConnected) bool connected; @end diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 0268054..92662ad 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -607,7 +607,12 @@ typedef struct __attribute__((packed)) { - (void)pwmThread { /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller - uses rumble. */ + uses rumble. At least make sure any sibling controllers don't have their PWM thread running. */ + for (JOYController *controller in [JOYController allControllers]) { + if (controller != self && controller->_device == _device) { + [controller _forceStopPWMThread]; + } + } unsigned rumbleCounter = 0; while (self.connected && !_forceStopPWMThread) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { @@ -619,6 +624,7 @@ typedef struct __attribute__((packed)) { } } [_rumblePWMThreadLock lock]; + [_rumbleElement setValue:0]; _rumblePWMThreadRunning = false; _forceStopPWMThread = false; [_rumblePWMThreadLock unlock]; From 7e124e169eddb8453a1973b351ab76bd6ea9a515 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:28:06 +0300 Subject: [PATCH 272/341] Avoid races --- JoyKit/JOYController.m | 1 + 1 file changed, 1 insertion(+) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 92662ad..0d88f28 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -703,6 +703,7 @@ typedef struct __attribute__((packed)) { _forceStopPWMThread = true; } [_rumblePWMThreadLock unlock]; + while (_rumblePWMThreadRunning); } + (void)controllerAdded:(IOHIDDeviceRef) device From 69fb2ad0a37dd40b1b7b46951336da1ef30d1621 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 18:46:00 +0300 Subject: [PATCH 273/341] Fix rumble on WUP-028 on ports other than 1 --- JoyKit/ControllerConfiguration.inc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index cf327f9..c8b49cc 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -127,10 +127,10 @@ hacksByName = @{ // Rumble @(1364): @[ - @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, - @{@"reportID": @(2), @"size":@1, @"offset":@1, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, - @{@"reportID": @(3), @"size":@1, @"offset":@2, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, - @{@"reportID": @(4), @"size":@1, @"offset":@3, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(2), @"size":@1, @"offset":@8, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(3), @"size":@1, @"offset":@16, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, + @{@"reportID": @(4), @"size":@1, @"offset":@24, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, ], @(11): @[ From c492022ae69541d80663033611a78a4a3e01583b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 19:31:04 +0300 Subject: [PATCH 274/341] Fix a deadlock --- JoyKit/JOYController.m | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 0d88f28..459896d 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -35,6 +35,8 @@ static bool axesEmulateButtons = false; static bool axes2DEmulateButtons = false; static bool hatsEmulateButtons = false; +static NSLock *globalPWMThreadLock; + @interface JOYController () + (void)controllerAdded:(IOHIDDeviceRef) device; + (void)controllerRemoved:(IOHIDDeviceRef) device; @@ -111,9 +113,9 @@ typedef struct __attribute__((packed)) { JOYElement *_connectedElement; NSMutableDictionary *_iokitToJOY; NSString *_serialSuffix; - bool _isSwitch; // Does thie controller use the Switch protocol? + bool _isSwitch; // Does this controller use the Switch protocol? JOYSwitchPacket _lastSwitchPacket; - NSCondition *_rumblePWMThreadLock; + NSLock *_rumblePWMThreadLock; volatile double _rumblePWMRatio; bool _physicallyConnected; bool _logicallyConnected; @@ -149,7 +151,7 @@ typedef struct __attribute__((packed)) { _hatEmulatedButtons = [NSMutableDictionary dictionary]; _multiElements = [NSMutableDictionary dictionary]; _iokitToJOY = [NSMutableDictionary dictionary]; - _rumblePWMThreadLock = [[NSCondition alloc] init]; + _rumblePWMThreadLock = [[NSLock alloc] init]; //NSMutableArray *axes3d = [NSMutableArray array]; @@ -606,13 +608,6 @@ typedef struct __attribute__((packed)) { - (void)pwmThread { - /* TODO: This does not handle correctly the case of having a multi-port controller where more than one controller - uses rumble. At least make sure any sibling controllers don't have their PWM thread running. */ - for (JOYController *controller in [JOYController allControllers]) { - if (controller != self && controller->_device == _device) { - [controller _forceStopPWMThread]; - } - } unsigned rumbleCounter = 0; while (self.connected && !_forceStopPWMThread) { if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { @@ -678,9 +673,20 @@ typedef struct __attribute__((packed)) { _rumblePWMRatio = amp; if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. if (amp != 0) { + /* TODO: The PWM thread does not handle correctly the case of having a multi-port controller where more + than one controller uses rumble. At least make sure any sibling controllers don't have their + PWM thread running. */ + + [globalPWMThreadLock lock]; + for (JOYController *controller in [JOYController allControllers]) { + if (controller != self && controller->_device == _device) { + [controller _forceStopPWMThread]; + } + } _rumblePWMRatio = amp; _rumblePWMThreadRunning = true; [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; + [globalPWMThreadLock unlock]; } } [_rumblePWMThreadLock unlock]; @@ -765,6 +771,7 @@ typedef struct __attribute__((packed)) { controllers = [NSMutableDictionary dictionary]; exposedControllers = [NSMutableArray array]; + globalPWMThreadLock = [[NSLock alloc] init]; NSArray *array = @[ CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick), CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad), From e5302a9b1e6ce04a245619d971c4ea438fffb9cb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 1 May 2020 23:42:08 +0300 Subject: [PATCH 275/341] Set sane libretro defaults, add border settings (Closes #203), general libretro cleanup --- libretro/libretro.c | 194 +++++++++++++++++++++++++------------------- 1 file changed, 111 insertions(+), 83 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index fa478f5..14bfaa8 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -29,13 +29,10 @@ static const char slash = '\\'; static const char slash = '/'; #endif -#define VIDEO_WIDTH 160 -#define VIDEO_HEIGHT 144 -#define VIDEO_PIXELS (VIDEO_WIDTH * VIDEO_HEIGHT) +#define MAX_VIDEO_WIDTH 256 +#define MAX_VIDEO_HEIGHT 224 +#define MAX_VIDEO_PIXELS (MAX_VIDEO_WIDTH * MAX_VIDEO_HEIGHT) -#define SGB_VIDEO_WIDTH 256 -#define SGB_VIDEO_HEIGHT 224 -#define SGB_VIDEO_PIXELS (SGB_VIDEO_WIDTH * SGB_VIDEO_HEIGHT) #define RETRO_MEMORY_GAMEBOY_1_SRAM ((1 << 8) | RETRO_MEMORY_SAVE_RAM) #define RETRO_MEMORY_GAMEBOY_1_RTC ((2 << 8) | RETRO_MEMORY_RTC) @@ -92,7 +89,6 @@ static unsigned emulated_devices = 1; static bool initialized = false; static unsigned screen_layout = 0; static unsigned audio_out = 0; -static unsigned sgb_border = 1; static bool geometry_updated = false; static bool link_cable_emulation = false; @@ -207,11 +203,11 @@ static retro_environment_t environ_cb; /* variables for single cart mode */ static const struct retro_variable vars_single[] = { - { "sameboy_color_correction_mode", "Color correction; off|correct curves|emulate hardware|preserve brightness|reduce contrast" }, - { "sameboy_high_pass_filter_mode", "High-pass filter; off|accurate|remove dc offset" }, + { "sameboy_color_correction_mode", "Color correction; emulate hardware|preserve brightness|reduce contrast|off|correct curves" }, + { "sameboy_high_pass_filter_mode", "High-pass filter; accurate|remove dc offset|off" }, { "sameboy_model", "Emulated model; Auto|Game Boy|Game Boy Color|Game Boy Advance|Super Game Boy|Super Game Boy 2" }, - { "sameboy_border", "Super Game Boy border; enabled|disabled" }, - { "sameboy_rumble", "Enable rumble; never|rumble-enabled games|all games" }, + { "sameboy_border", "Display border; Super Game Boy only|always|never" }, + { "sameboy_rumble", "Enable rumble; rumble-enabled games|all games|never" }, { NULL } }; @@ -223,12 +219,12 @@ static const struct retro_variable vars_dual[] = { { "sameboy_audio_output", "Audio output; Game Boy #1|Game Boy #2" }, { "sameboy_model_1", "Emulated model for Game Boy #1; Auto|Game Boy|Game Boy Color|Game Boy Advance" }, { "sameboy_model_2", "Emulated model for Game Boy #2; Auto|Game Boy|Game Boy Color|Game Boy Advance" }, - { "sameboy_color_correction_mode_1", "Color correction for Game Boy #1; off|correct curves|emulate hardware|preserve brightness" }, - { "sameboy_color_correction_mode_2", "Color correction for Game Boy #2; off|correct curves|emulate hardware|preserve brightness" }, - { "sameboy_high_pass_filter_mode_1", "High-pass filter for Game Boy #1; off|accurate|remove dc offset" }, - { "sameboy_high_pass_filter_mode_2", "High-pass filter for Game Boy #2; off|accurate|remove dc offset" }, - { "sameboy_rumble_1", "Enable rumble for Game Boy #1; never|rumble-enabled games|all games" }, - { "sameboy_rumble_2", "Enable rumble for Game Boy #2; never|rumble-enabled games|all games" }, + { "sameboy_color_correction_mode_1", "Color correction for Game Boy #1; emulate hardware|preserve brightness|reduce contrast|off|correct curves" }, + { "sameboy_color_correction_mode_2", "Color correction for Game Boy #2; emulate hardware|preserve brightness|reduce contrast|off|correct curves" }, + { "sameboy_high_pass_filter_mode_1", "High-pass filter for Game Boy #1; accurate|remove dc offset|off" }, + { "sameboy_high_pass_filter_mode_2", "High-pass filter for Game Boy #2; accurate|remove dc offset|off" }, + { "sameboy_rumble_1", "Enable rumble for Game Boy #1; rumble-enabled games|all games|never" }, + { "sameboy_rumble_2", "Enable rumble for Game Boy #2; rumble-enabled games|all games|never" }, { NULL } }; @@ -345,6 +341,52 @@ static void set_link_cable_state(bool state) } } +static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) +{ + const char *model_name = (char *[]){ + [GB_BOOT_ROM_DMG0] = "dmg0_boot", + [GB_BOOT_ROM_DMG] = "dmg_boot", + [GB_BOOT_ROM_MGB] = "mgb_boot", + [GB_BOOT_ROM_SGB] = "sgb_boot", + [GB_BOOT_ROM_SGB2] = "sgb2_boot", + [GB_BOOT_ROM_CGB0] = "cgb0_boot", + [GB_BOOT_ROM_CGB] = "cgb_boot", + [GB_BOOT_ROM_AGB] = "agb_boot", + }[type]; + + const uint8_t *boot_code = (const unsigned char *[]) + { + [GB_BOOT_ROM_DMG0] = dmg_boot, // dmg0 not implemented yet + [GB_BOOT_ROM_DMG] = dmg_boot, + [GB_BOOT_ROM_MGB] = dmg_boot, // mgb not implemented yet + [GB_BOOT_ROM_SGB] = sgb_boot, + [GB_BOOT_ROM_SGB2] = sgb2_boot, + [GB_BOOT_ROM_CGB0] = cgb_boot, // cgb0 not implemented yet + [GB_BOOT_ROM_CGB] = cgb_boot, + [GB_BOOT_ROM_AGB] = agb_boot, + }[type]; + + unsigned boot_length = (unsigned []){ + [GB_BOOT_ROM_DMG0] = dmg_boot_length, // dmg0 not implemented yet + [GB_BOOT_ROM_DMG] = dmg_boot_length, + [GB_BOOT_ROM_MGB] = dmg_boot_length, // mgb not implemented yet + [GB_BOOT_ROM_SGB] = sgb_boot_length, + [GB_BOOT_ROM_SGB2] = sgb2_boot_length, + [GB_BOOT_ROM_CGB0] = cgb_boot_length, // cgb0 not implemented yet + [GB_BOOT_ROM_CGB] = cgb_boot_length, + [GB_BOOT_ROM_AGB] = agb_boot_length, + }[type]; + + char buf[256]; + snprintf(buf, sizeof(buf), "%s%c%s_boot.bin", retro_system_directory, slash, model_name); + log_cb(RETRO_LOG_INFO, "Initializing as model: %s\n", model_name); + log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); + + if (GB_load_boot_rom(gb, buf)) { + GB_load_boot_rom_from_buffer(gb, boot_code, boot_length); + } +} + static void init_for_current_model(unsigned id) { unsigned i = id; @@ -362,26 +404,17 @@ static void init_for_current_model(unsigned id) else { GB_init(&gameboy[i], libretro_to_internal_model[effective_model]); } - const char *model_name = (const char *[]){"dmg", "cgb", "agb", "sgb", "sgb2"}[effective_model]; - const unsigned char *boot_code = (const unsigned char *[]){dmg_boot, cgb_boot, agb_boot, sgb_boot, sgb2_boot}[effective_model]; - unsigned boot_length = (unsigned []){dmg_boot_length, cgb_boot_length, agb_boot_length, sgb_boot_length, sgb2_boot_length}[effective_model]; + + GB_set_boot_rom_load_callback(&gameboy[i], boot_rom_load); - char buf[256]; - snprintf(buf, sizeof(buf), "%s%c%s_boot.bin", retro_system_directory, slash, model_name); - log_cb(RETRO_LOG_INFO, "Initializing as model: %s\n", model_name); - log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - if (GB_load_boot_rom(&gameboy[i], buf)) { - GB_load_boot_rom_from_buffer(&gameboy[i], boot_code, boot_length); - } - GB_set_user_data(&gameboy[i], (void*)NULL); + /* When running multiple devices they are assumed to use the same resolution */ - GB_set_pixels_output(&gameboy[i],(unsigned int*)(frame_buf + i * ((model[i] == MODEL_SGB || model[i] == MODEL_SGB2) ? SGB_VIDEO_PIXELS : VIDEO_PIXELS))); + GB_set_pixels_output(&gameboy[i], + (uint32_t *)(frame_buf + GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]) * i)); GB_set_rgb_encode_callback(&gameboy[i], rgb_encode); GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); GB_apu_set_sample_callback(&gameboy[i], audio_callback); GB_set_rumble_callback(&gameboy[i], rumble_callback); - GB_set_rumble_mode(&gameboy[i], GB_RUMBLE_CARTRIDGE_ONLY); - /* todo: attempt to make these more generic */ GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); @@ -571,16 +604,23 @@ static void check_variables() var.key = "sameboy_border"; var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (strcmp(var.value, "enabled") == 0) { - sgb_border = 1; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { + if (strcmp(var.value, "never") == 0) { + GB_set_border_mode(&gameboy[0], GB_BORDER_NEVER); } - else if (strcmp(var.value, "disabled") == 0) { - sgb_border = 0; + else if (strcmp(var.value, "Super Game Boy only") == 0) { + GB_set_border_mode(&gameboy[0], GB_BORDER_SGB); } + else if (strcmp(var.value, "always") == 0) { + GB_set_border_mode(&gameboy[0], GB_BORDER_ALWAYS); + } + + geometry_updated = true; } } - else { + else { + GB_set_border_mode(&gameboy[0], GB_BORDER_NEVER); + GB_set_border_mode(&gameboy[1], GB_BORDER_NEVER); var.key = "sameboy_color_correction_mode_1"; var.value = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { @@ -844,31 +884,24 @@ void retro_get_system_av_info(struct retro_system_av_info *info) if (emulated_devices == 2) { if (screen_layout == LAYOUT_TOP_DOWN) { - geom.base_width = VIDEO_WIDTH; - geom.base_height = VIDEO_HEIGHT * emulated_devices; - geom.aspect_ratio = (double)VIDEO_WIDTH / (emulated_devices * VIDEO_HEIGHT); + geom.base_width = GB_get_screen_width(&gameboy[0]); + geom.base_height = GB_get_screen_height(&gameboy[0]) * emulated_devices; + geom.aspect_ratio = (double)GB_get_screen_width(&gameboy[0]) / (emulated_devices * GB_get_screen_height(&gameboy[0])); } else if (screen_layout == LAYOUT_LEFT_RIGHT) { - geom.base_width = VIDEO_WIDTH * emulated_devices; - geom.base_height = VIDEO_HEIGHT; - geom.aspect_ratio = ((double)VIDEO_WIDTH * emulated_devices) / VIDEO_HEIGHT; + geom.base_width = GB_get_screen_width(&gameboy[0]) * emulated_devices; + geom.base_height = GB_get_screen_height(&gameboy[0]); + geom.aspect_ratio = ((double)GB_get_screen_width(&gameboy[0]) * emulated_devices) / GB_get_screen_height(&gameboy[0]); } } else { - if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { - geom.base_width = SGB_VIDEO_WIDTH; - geom.base_height = SGB_VIDEO_HEIGHT; - geom.aspect_ratio = (double)SGB_VIDEO_WIDTH / SGB_VIDEO_HEIGHT; - } - else { - geom.base_width = VIDEO_WIDTH; - geom.base_height = VIDEO_HEIGHT; - geom.aspect_ratio = (double)VIDEO_WIDTH / VIDEO_HEIGHT; - } + geom.base_width = GB_get_screen_width(&gameboy[0]); + geom.base_height = GB_get_screen_height(&gameboy[0]); + geom.aspect_ratio = (double)GB_get_screen_width(&gameboy[0]) / GB_get_screen_height(&gameboy[0]); } - geom.max_width = SGB_VIDEO_WIDTH * emulated_devices; - geom.max_height = SGB_VIDEO_HEIGHT * emulated_devices; + geom.max_width = MAX_VIDEO_WIDTH * emulated_devices; + geom.max_height = MAX_VIDEO_HEIGHT * emulated_devices; info->geometry = geom; info->timing = timing; @@ -969,35 +1002,30 @@ void retro_run(void) if (emulated_devices == 2) { if (screen_layout == LAYOUT_TOP_DOWN) { - video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT * emulated_devices, VIDEO_WIDTH * sizeof(uint32_t)); + video_cb(frame_buf, + GB_get_screen_width(&gameboy[0]), + GB_get_screen_height(&gameboy[0]) * emulated_devices, + GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t)); } else if (screen_layout == LAYOUT_LEFT_RIGHT) { - /* use slow memcpy method for now */ - for (int index = 0; index < emulated_devices; index++) { - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - frame_buf_copy[VIDEO_WIDTH * emulated_devices * y + (x + VIDEO_WIDTH * index)] = frame_buf[VIDEO_WIDTH * (y + VIDEO_HEIGHT * index) + x]; - } + unsigned pitch = GB_get_screen_width(&gameboy[0]) * emulated_devices; + unsigned pixels_per_device = GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]); + for (int y = 0; y < GB_get_screen_height(&gameboy[0]); y++) { + for (unsigned i = 0; i < emulated_devices; i++) { + memcpy(frame_buf_copy + y * pitch + GB_get_screen_width(&gameboy[0]) * i, + frame_buf + pixels_per_device * i + y * GB_get_screen_width(&gameboy[0]), + GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t)); } } - video_cb(frame_buf_copy, VIDEO_WIDTH * emulated_devices, VIDEO_HEIGHT, VIDEO_WIDTH * emulated_devices * sizeof(uint32_t)); + video_cb(frame_buf_copy, GB_get_screen_width(&gameboy[0]) * emulated_devices, GB_get_screen_height(&gameboy[0]), GB_get_screen_width(&gameboy[0]) * emulated_devices * sizeof(uint32_t)); } } - else { - if (model[0] == MODEL_SGB || model[0] == MODEL_SGB2) { - if (sgb_border == 1) { - video_cb(frame_buf, SGB_VIDEO_WIDTH, SGB_VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); - } - else { - int crop = SGB_VIDEO_WIDTH * ((SGB_VIDEO_HEIGHT - VIDEO_HEIGHT) / 2) + ((SGB_VIDEO_WIDTH - VIDEO_WIDTH) / 2); - - video_cb(frame_buf + crop, VIDEO_WIDTH, VIDEO_HEIGHT, SGB_VIDEO_WIDTH * sizeof(uint32_t)); - } - } - else { - video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); - } + else { + video_cb(frame_buf, + GB_get_screen_width(&gameboy[0]), + GB_get_screen_height(&gameboy[0]), + GB_get_screen_width(&gameboy[0]) * sizeof(uint32_t)); } @@ -1009,8 +1037,8 @@ bool retro_load_game(const struct retro_game_info *info) environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_single); check_variables(); - frame_buf = (uint32_t*)malloc(SGB_VIDEO_PIXELS *emulated_devices * sizeof(uint32_t)); - memset(frame_buf, 0, SGB_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t)); + frame_buf = (uint32_t *)malloc(MAX_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t)); + memset(frame_buf, 0, MAX_VIDEO_PIXELS * emulated_devices * sizeof(uint32_t)); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { @@ -1068,11 +1096,11 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_dual); check_variables(); - frame_buf = (uint32_t*)malloc(emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); - frame_buf_copy = (uint32_t*)malloc(emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); + frame_buf = (uint32_t*)malloc(emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t)); + frame_buf_copy = (uint32_t*)malloc(emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t)); - memset(frame_buf, 0, emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); - memset(frame_buf_copy, 0, emulated_devices * SGB_VIDEO_PIXELS * sizeof(uint32_t)); + memset(frame_buf, 0, emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t)); + memset(frame_buf_copy, 0, emulated_devices * MAX_VIDEO_PIXELS * sizeof(uint32_t)); enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { From 78e2b94cb5b4e16bea2e147dceaa822425fa08ea Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 May 2020 20:55:54 +0300 Subject: [PATCH 276/341] Rewrite the "Sub Elements" design into a more powerful Custom Report design that can overwrite an entire report structure of a sepcific report by its ID --- JoyKit/ControllerConfiguration.inc | 14 +- JoyKit/JOYController.m | 514 ++++++++++++++++------------- JoyKit/JOYFullReportElement.h | 10 + JoyKit/JOYFullReportElement.m | 73 ++++ JoyKit/JOYMultiplayerController.h | 2 +- JoyKit/JOYMultiplayerController.m | 6 +- 6 files changed, 384 insertions(+), 235 deletions(-) create mode 100644 JoyKit/JOYFullReportElement.h create mode 100644 JoyKit/JOYFullReportElement.m diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index c8b49cc..35a6cff 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -123,17 +123,17 @@ hacksByName = @{ JOYActivationReport: [NSData dataWithBytes:(uint8_t[]){0x13} length:1], - JOYSubElementStructs: @{ + JOYCustomReports: @{ // Rumble - @(1364): @[ + @(-17): @[ @{@"reportID": @(1), @"size":@1, @"offset":@0, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(2), @"size":@1, @"offset":@8, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(3), @"size":@1, @"offset":@16, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, @{@"reportID": @(4), @"size":@1, @"offset":@24, @"usagePage":@(0xFF00), @"usage":@1, @"min": @0, @"max": @1}, ], - @(11): @[ + @(33): @[ // Player 1 @@ -141,11 +141,11 @@ hacksByName = @{ @{@"reportID": @(1), @"size":@1, @"offset":@8, @"usagePage":@(kHIDPage_Button), @"usage":@1}, @{@"reportID": @(1), @"size":@1, @"offset":@9, @"usagePage":@(kHIDPage_Button), @"usage":@2}, - @{@"reportID": @(1), @"size":@1, @"offset":@10, @"usagePage":@(kHIDPage_Button), @"usage":@3}, - @{@"reportID": @(1), @"size":@1, @"offset":@11, @"usagePage":@(kHIDPage_Button), @"usage":@4}, + @{@"reportID": @(1), @"size":@1, @"offset":@10,@"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(1), @"size":@1, @"offset":@11,@"usagePage":@(kHIDPage_Button), @"usage":@4}, - @{@"reportID": @(1), @"size":@1, @"offset":@12, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, - @{@"reportID": @(1), @"size":@1, @"offset":@13, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(1), @"size":@1, @"offset":@12, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + @{@"reportID": @(1), @"size":@1, @"offset":@13, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, @{@"reportID": @(1), @"size":@1, @"offset":@14, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, @{@"reportID": @(1), @"size":@1, @"offset":@15, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 459896d..131441c 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -2,6 +2,8 @@ #import "JOYMultiplayerController.h" #import "JOYElement.h" #import "JOYSubElement.h" +#import "JOYFullReportElement.h" + #import "JOYEmulatedButton.h" #include @@ -12,7 +14,7 @@ static NSString const *JOYReportIDFilters = @"JOYReportIDFilters"; static NSString const *JOYButtonUsageMapping = @"JOYButtonUsageMapping"; static NSString const *JOYAxisUsageMapping = @"JOYAxisUsageMapping"; static NSString const *JOYAxes2DUsageMapping = @"JOYAxes2DUsageMapping"; -static NSString const *JOYSubElementStructs = @"JOYSubElementStructs"; +static NSString const *JOYCustomReports = @"JOYCustomReports"; static NSString const *JOYIsSwitch = @"JOYIsSwitch"; static NSString const *JOYRumbleUsage = @"JOYRumbleUsage"; static NSString const *JOYRumbleUsagePage = @"JOYRumbleUsagePage"; @@ -41,6 +43,8 @@ static NSLock *globalPWMThreadLock; + (void)controllerAdded:(IOHIDDeviceRef) device; + (void)controllerRemoved:(IOHIDDeviceRef) device; - (void)elementChanged:(IOHIDElementRef) element; +- (void)gotReport:(NSData *)report; + @end @interface JOYButton () @@ -86,6 +90,11 @@ static void HIDInput(void *context, IOReturn result, void *sender, IOHIDValueRef [(__bridge JOYController *)context elementChanged:IOHIDValueGetElement(value)]; } +static void HIDReport(void *context, IOReturn result, void *sender, IOHIDReportType type, + uint32_t reportID, uint8_t *report, CFIndex reportLength) +{ + [(__bridge JOYController *)context gotReport:[[NSData alloc] initWithBytesNoCopy:report length:reportLength freeWhenDone:NO]]; +} typedef struct __attribute__((packed)) { uint8_t reportID; @@ -102,7 +111,8 @@ typedef struct __attribute__((packed)) { NSMutableDictionary *_axes; NSMutableDictionary *_axes2D; NSMutableDictionary *_hats; - NSMutableDictionary *> *_multiElements; + NSMutableDictionary *_fullReportElements; + NSMutableDictionary *> *_multiElements; // Button emulation NSMutableDictionary *_axisEmulatedButtons; @@ -121,14 +131,182 @@ typedef struct __attribute__((packed)) { bool _logicallyConnected; bool _rumblePWMThreadRunning; volatile bool _forceStopPWMThread; + + NSDictionary *_hacks; + NSMutableData *_lastReport; + + // Used when creating inputs + JOYElement *_previousAxisElement; + } -- (instancetype)initWithDevice:(IOHIDDeviceRef) device +- (instancetype)initWithDevice:(IOHIDDeviceRef) device hacks:(NSDictionary *)hacks { - return [self initWithDevice:device reportIDFilter:nil serialSuffix:nil]; + return [self initWithDevice:device reportIDFilter:nil serialSuffix:nil hacks:hacks]; } -- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix +-(void)createOutputForElement:(JOYElement *)element +{ + uint16_t rumbleUsagePage = (uint16_t)[_hacks[JOYRumbleUsagePage] unsignedIntValue]; + uint16_t rumbleUsage = (uint16_t)[_hacks[JOYRumbleUsage] unsignedIntValue]; + + if (!_rumbleElement && rumbleUsage && rumbleUsagePage && element.usage == rumbleUsage && element.usagePage == rumbleUsagePage) { + if (_hacks[JOYRumbleMin]) { + element.min = [_hacks[JOYRumbleMin] unsignedIntValue]; + } + if (_hacks[JOYRumbleMax]) { + element.max = [_hacks[JOYRumbleMax] unsignedIntValue]; + } + _rumbleElement = element; + } +} + +-(void)createInputForElement:(JOYElement *)element +{ + uint16_t connectedUsagePage = (uint16_t)[_hacks[JOYConnectedUsagePage] unsignedIntValue]; + uint16_t connectedUsage = (uint16_t)[_hacks[JOYConnectedUsage] unsignedIntValue]; + + if (!_connectedElement && connectedUsage && connectedUsagePage && element.usage == connectedUsage && element.usagePage == connectedUsagePage) { + _connectedElement = element; + _logicallyConnected = element.value != element.min; + return; + } + + if (element.usagePage == kHIDPage_Button) { + button: { + JOYButton *button = [[JOYButton alloc] initWithElement: element]; + [_buttons setObject:button forKey:element]; + NSNumber *replacementUsage = _hacks[JOYButtonUsageMapping][@(button.usage)]; + if (replacementUsage) { + button.usage = [replacementUsage unsignedIntValue]; + } + return; + } + } + else if (element.usagePage == kHIDPage_GenericDesktop) { + NSDictionary *axisGroups = @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(2), + @(kHIDUsage_GD_Rz): @(1), + }; + + axisGroups = _hacks[JOYAxisGroups] ?: axisGroups; + + switch (element.usage) { + case kHIDUsage_GD_X: + case kHIDUsage_GD_Y: + case kHIDUsage_GD_Z: + case kHIDUsage_GD_Rx: + case kHIDUsage_GD_Ry: + case kHIDUsage_GD_Rz: { + + JOYElement *other = _previousAxisElement; + _previousAxisElement = element; + if (!other) goto single; + if (other.usage >= element.usage) goto single; + if (other.reportID != element.reportID) goto single; + if (![axisGroups[@(other.usage)] isEqualTo: axisGroups[@(element.usage)]]) goto single; + if (other.parentID != element.parentID) goto single; + + JOYAxes2D *axes = nil; + if (other.usage == kHIDUsage_GD_Z && element.usage == kHIDUsage_GD_Rz && [_hacks[JOYSwapZRz] boolValue]) { + axes = [[JOYAxes2D alloc] initWithFirstElement:element secondElement:other]; + } + else { + axes = [[JOYAxes2D alloc] initWithFirstElement:other secondElement:element]; + } + NSNumber *replacementUsage = _hacks[JOYAxes2DUsageMapping][@(axes.usage)]; + if (replacementUsage) { + axes.usage = [replacementUsage unsignedIntValue]; + } + + [_axisEmulatedButtons removeObjectForKey:@(_axes[other].uniqueID)]; + [_axes removeObjectForKey:other]; + _previousAxisElement = nil; + _axes2D[other] = axes; + _axes2D[element] = axes; + + if (axes2DEmulateButtons) { + _axes2DEmulatedButtons[@(axes.uniqueID)] = @[ + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:axes.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:axes.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:axes.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:axes.uniqueID | 0x400000000L], + ]; + } + + /* + for (NSArray *group in axes2d) { + break; + IOHIDElementRef first = (__bridge IOHIDElementRef)group[0]; + IOHIDElementRef second = (__bridge IOHIDElementRef)group[1]; + if (IOHIDElementGetUsage(first) > element.usage) continue; + if (IOHIDElementGetUsage(second) > element.usage) continue; + if (IOHIDElementGetReportID(first) != IOHIDElementGetReportID(element)) continue; + if ((IOHIDElementGetUsage(first) - kHIDUsage_GD_X) / 3 != (element.usage - kHIDUsage_GD_X) / 3) continue; + if (IOHIDElementGetParent(first) != IOHIDElementGetParent(element)) continue; + + [axes2d removeObject:group]; + [axes3d addObject:@[(__bridge id)first, (__bridge id)second, _element]]; + found = true; + break; + }*/ + break; + } + single: + case kHIDUsage_GD_Slider: + case kHIDUsage_GD_Dial: + case kHIDUsage_GD_Wheel: { + JOYAxis *axis = [[JOYAxis alloc] initWithElement: element]; + [_axes setObject:axis forKey:element]; + + NSNumber *replacementUsage = _hacks[JOYAxisUsageMapping][@(axis.usage)]; + if (replacementUsage) { + axis.usage = [replacementUsage unsignedIntValue]; + } + + if (axesEmulateButtons && axis.usage >= JOYAxisUsageL1 && axis.usage <= JOYAxisUsageR3) { + _axisEmulatedButtons[@(axis.uniqueID)] = + [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageL1 + JOYButtonUsageL1 uniqueID:axis.uniqueID]; + } + + if (axesEmulateButtons && axis.usage >= JOYAxisUsageGeneric0) { + _axisEmulatedButtons[@(axis.uniqueID)] = + [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageGeneric0 + JOYButtonUsageGeneric0 uniqueID:axis.uniqueID]; + } + + break; + } + case kHIDUsage_GD_DPadUp: + case kHIDUsage_GD_DPadDown: + case kHIDUsage_GD_DPadRight: + case kHIDUsage_GD_DPadLeft: + case kHIDUsage_GD_Start: + case kHIDUsage_GD_Select: + case kHIDUsage_GD_SystemMainMenu: + goto button; + + case kHIDUsage_GD_Hatswitch: { + JOYHat *hat = [[JOYHat alloc] initWithElement: element]; + [_hats setObject:hat forKey:element]; + if (hatsEmulateButtons) { + _hatEmulatedButtons[@(hat.uniqueID)] = @[ + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:hat.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:hat.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:hat.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:hat.uniqueID | 0x400000000L], + ]; + } + break; + } + } + } +} + +- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix hacks:(NSDictionary *)hacks { self = [super init]; if (!self) return self; @@ -149,229 +327,109 @@ typedef struct __attribute__((packed)) { _axisEmulatedButtons = [NSMutableDictionary dictionary]; _axes2DEmulatedButtons = [NSMutableDictionary dictionary]; _hatEmulatedButtons = [NSMutableDictionary dictionary]; - _multiElements = [NSMutableDictionary dictionary]; _iokitToJOY = [NSMutableDictionary dictionary]; _rumblePWMThreadLock = [[NSLock alloc] init]; //NSMutableArray *axes3d = [NSMutableArray array]; - NSDictionary *axisGroups = @{ - @(kHIDUsage_GD_X): @(0), - @(kHIDUsage_GD_Y): @(0), - @(kHIDUsage_GD_Z): @(1), - @(kHIDUsage_GD_Rx): @(2), - @(kHIDUsage_GD_Ry): @(2), - @(kHIDUsage_GD_Rz): @(1), - }; - NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); - NSDictionary *hacks = hacksByName[name]; - if (!hacks) { - hacks = hacksByManufacturer[(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey))]; + _hacks = hacks; + _isSwitch = [_hacks[JOYIsSwitch] boolValue]; + NSDictionary *customReports = hacks[JOYCustomReports]; + + if (hacks[JOYCustomReports]) { + _multiElements = [NSMutableDictionary dictionary]; + _fullReportElements = [NSMutableDictionary dictionary]; + _lastReport = [NSMutableData dataWithLength:MAX( + MAX( + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxInputReportSizeKey)) unsignedIntValue], + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxOutputReportSizeKey)) unsignedIntValue] + ), + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxFeatureReportSizeKey)) unsignedIntValue] + )]; + IOHIDDeviceRegisterInputReportCallback(device, _lastReport.mutableBytes, _lastReport.length, HIDReport, (void *)self); + + for (NSNumber *_reportID in customReports) { + signed reportID = [_reportID intValue]; + bool isOutput = false; + if (reportID < 0) { + isOutput = true; + reportID = -reportID; + } + + JOYFullReportElement *element = [[JOYFullReportElement alloc] initWithDevice:device reportID:reportID]; + NSMutableArray *elements = [NSMutableArray array]; + for (NSDictionary *subElementDef in customReports[_reportID]) { + if (filter && subElementDef[@"reportID"] && ![filter containsObject:subElementDef[@"reportID"]]) continue; + JOYSubElement *subElement = [[JOYSubElement alloc] initWithRealElement:element + size:subElementDef[@"size"].unsignedLongValue + offset:subElementDef[@"offset"].unsignedLongValue + 8 // Compensate for the reportID + usagePage:subElementDef[@"usagePage"].unsignedLongValue + usage:subElementDef[@"usage"].unsignedLongValue + min:subElementDef[@"min"].unsignedIntValue + max:subElementDef[@"max"].unsignedIntValue]; + [elements addObject:subElement]; + if (isOutput) { + [self createOutputForElement:subElement]; + } + else { + [self createInputForElement:subElement]; + } + } + _multiElements[element] = elements; + if (!isOutput) { + _fullReportElements[@(reportID)] = element; + } + } } - axisGroups = hacks[JOYAxisGroups] ?: axisGroups; - _isSwitch = [hacks[JOYIsSwitch] boolValue]; - uint16_t rumbleUsagePage = (uint16_t)[hacks[JOYRumbleUsagePage] unsignedIntValue]; - uint16_t rumbleUsage = (uint16_t)[hacks[JOYRumbleUsage] unsignedIntValue]; - uint16_t connectedUsagePage = (uint16_t)[hacks[JOYConnectedUsagePage] unsignedIntValue]; - uint16_t connectedUsage = (uint16_t)[hacks[JOYConnectedUsage] unsignedIntValue]; - - JOYElement *previousAxisElement = nil; + id previous = nil; for (id _element in array) { if (_element == previous) continue; // Some elements are reported twice for some reason previous = _element; - NSArray *elements = nil; JOYElement *element = [[JOYElement alloc] initWithElement:(__bridge IOHIDElementRef)_element]; - - NSArray*> *subElementDefs = hacks[JOYSubElementStructs][@(element.uniqueID)]; bool isOutput = false; - if (subElementDefs && element.uniqueID != element.parentID) { - elements = [NSMutableArray array]; - for (NSDictionary *virtualInput in subElementDefs) { - if (filter && virtualInput[@"reportID"] && ![filter containsObject:virtualInput[@"reportID"]]) continue; - [(NSMutableArray *)elements addObject:[[JOYSubElement alloc] initWithRealElement:element - size:virtualInput[@"size"].unsignedLongValue - offset:virtualInput[@"offset"].unsignedLongValue - usagePage:virtualInput[@"usagePage"].unsignedLongValue - usage:virtualInput[@"usage"].unsignedLongValue - min:virtualInput[@"min"].unsignedIntValue - max:virtualInput[@"max"].unsignedIntValue]]; - } - isOutput = IOHIDElementGetType((__bridge IOHIDElementRef)_element) == kIOHIDElementTypeOutput; - [_multiElements setObject:elements forKey:element]; + if (filter && ![filter containsObject:@(element.reportID)]) continue; + + switch (IOHIDElementGetType((__bridge IOHIDElementRef)_element)) { + /* Handled */ + case kIOHIDElementTypeInput_Misc: + case kIOHIDElementTypeInput_Button: + case kIOHIDElementTypeInput_Axis: + break; + case kIOHIDElementTypeOutput: + isOutput = true; + break; + /* Ignored */ + default: + case kIOHIDElementTypeInput_ScanCodes: + case kIOHIDElementTypeInput_NULL: + case kIOHIDElementTypeFeature: + case kIOHIDElementTypeCollection: + continue; + } + if ((!isOutput && customReports[@(element.reportID)]) || + (isOutput && customReports[@(-element.reportID)])) continue; + + + if (IOHIDElementIsArray((__bridge IOHIDElementRef)_element)) continue; + + if (isOutput) { + [self createOutputForElement:element]; } else { - if (filter && ![filter containsObject:@(element.reportID)]) continue; - - switch (IOHIDElementGetType((__bridge IOHIDElementRef)_element)) { - /* Handled */ - case kIOHIDElementTypeInput_Misc: - case kIOHIDElementTypeInput_Button: - case kIOHIDElementTypeInput_Axis: - break; - case kIOHIDElementTypeOutput: - isOutput = true; - break; - /* Ignored */ - default: - case kIOHIDElementTypeInput_ScanCodes: - case kIOHIDElementTypeInput_NULL: - case kIOHIDElementTypeFeature: - case kIOHIDElementTypeCollection: - continue; - } - if (IOHIDElementIsArray((__bridge IOHIDElementRef)_element)) continue; - - elements = @[element]; + [self createInputForElement:element]; } _iokitToJOY[@(IOHIDElementGetCookie((__bridge IOHIDElementRef)_element))] = element; - for (JOYElement *element in elements) { - if (isOutput) { - if (!_rumbleElement && rumbleUsage && rumbleUsagePage && element.usage == rumbleUsage && element.usagePage == rumbleUsagePage) { - if (hacks[JOYRumbleMin]) { - element.min = [hacks[JOYRumbleMin] unsignedIntValue]; - } - if (hacks[JOYRumbleMax]) { - element.max = [hacks[JOYRumbleMax] unsignedIntValue]; - } - _rumbleElement = element; - } - continue; - } - else { - if (!_connectedElement && connectedUsage && connectedUsagePage && element.usage == connectedUsage && element.usagePage == connectedUsagePage) { - _connectedElement = element; - _logicallyConnected = element.value != element.min; - continue; - } - } - - if (element.usagePage == kHIDPage_Button) { - button: { - JOYButton *button = [[JOYButton alloc] initWithElement: element]; - [_buttons setObject:button forKey:element]; - NSNumber *replacementUsage = hacks[JOYButtonUsageMapping][@(button.usage)]; - if (replacementUsage) { - button.usage = [replacementUsage unsignedIntValue]; - } - continue; - } - } - else if (element.usagePage == kHIDPage_GenericDesktop) { - switch (element.usage) { - case kHIDUsage_GD_X: - case kHIDUsage_GD_Y: - case kHIDUsage_GD_Z: - case kHIDUsage_GD_Rx: - case kHIDUsage_GD_Ry: - case kHIDUsage_GD_Rz: { - - JOYElement *other = previousAxisElement; - previousAxisElement = element; - if (!other) goto single; - if (other.usage >= element.usage) goto single; - if (other.reportID != element.reportID) goto single; - if (![axisGroups[@(other.usage)] isEqualTo: axisGroups[@(element.usage)]]) goto single; - if (other.parentID != element.parentID) goto single; - - JOYAxes2D *axes = nil; - if (other.usage == kHIDUsage_GD_Z && element.usage == kHIDUsage_GD_Rz && [hacks[JOYSwapZRz] boolValue]) { - axes = [[JOYAxes2D alloc] initWithFirstElement:element secondElement:other]; - } - else { - axes = [[JOYAxes2D alloc] initWithFirstElement:other secondElement:element]; - } - NSNumber *replacementUsage = hacks[JOYAxes2DUsageMapping][@(axes.usage)]; - if (replacementUsage) { - axes.usage = [replacementUsage unsignedIntValue]; - } - - [_axisEmulatedButtons removeObjectForKey:@(_axes[other].uniqueID)]; - [_axes removeObjectForKey:other]; - previousAxisElement = nil; - _axes2D[other] = axes; - _axes2D[element] = axes; - - if (axes2DEmulateButtons) { - _axes2DEmulatedButtons[@(axes.uniqueID)] = @[ - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:axes.uniqueID | 0x100000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:axes.uniqueID | 0x200000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:axes.uniqueID | 0x300000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:axes.uniqueID | 0x400000000L], - ]; - } - - /* - for (NSArray *group in axes2d) { - break; - IOHIDElementRef first = (__bridge IOHIDElementRef)group[0]; - IOHIDElementRef second = (__bridge IOHIDElementRef)group[1]; - if (IOHIDElementGetUsage(first) > element.usage) continue; - if (IOHIDElementGetUsage(second) > element.usage) continue; - if (IOHIDElementGetReportID(first) != IOHIDElementGetReportID(element)) continue; - if ((IOHIDElementGetUsage(first) - kHIDUsage_GD_X) / 3 != (element.usage - kHIDUsage_GD_X) / 3) continue; - if (IOHIDElementGetParent(first) != IOHIDElementGetParent(element)) continue; - - [axes2d removeObject:group]; - [axes3d addObject:@[(__bridge id)first, (__bridge id)second, _element]]; - found = true; - break; - }*/ - break; - } - single: - case kHIDUsage_GD_Slider: - case kHIDUsage_GD_Dial: - case kHIDUsage_GD_Wheel: { - JOYAxis *axis = [[JOYAxis alloc] initWithElement: element]; - [_axes setObject:axis forKey:element]; - - NSNumber *replacementUsage = hacks[JOYAxisUsageMapping][@(axis.usage)]; - if (replacementUsage) { - axis.usage = [replacementUsage unsignedIntValue]; - } - - if (axesEmulateButtons && axis.usage >= JOYAxisUsageL1 && axis.usage <= JOYAxisUsageR3) { - _axisEmulatedButtons[@(axis.uniqueID)] = - [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageL1 + JOYButtonUsageL1 uniqueID:axis.uniqueID]; - } - - if (axesEmulateButtons && axis.usage >= JOYAxisUsageGeneric0) { - _axisEmulatedButtons[@(axis.uniqueID)] = - [[JOYEmulatedButton alloc] initWithUsage:axis.usage - JOYAxisUsageGeneric0 + JOYButtonUsageGeneric0 uniqueID:axis.uniqueID]; - } - - break; - } - case kHIDUsage_GD_DPadUp: - case kHIDUsage_GD_DPadDown: - case kHIDUsage_GD_DPadRight: - case kHIDUsage_GD_DPadLeft: - case kHIDUsage_GD_Start: - case kHIDUsage_GD_Select: - case kHIDUsage_GD_SystemMainMenu: - goto button; - - case kHIDUsage_GD_Hatswitch: { - JOYHat *hat = [[JOYHat alloc] initWithElement: element]; - [_hats setObject:hat forKey:element]; - if (hatsEmulateButtons) { - _hatEmulatedButtons[@(hat.uniqueID)] = @[ - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:hat.uniqueID | 0x100000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:hat.uniqueID | 0x200000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:hat.uniqueID | 0x300000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:hat.uniqueID | 0x400000000L], - ]; - } - break; - } - } - } + if (_isSwitch && element.reportID == 0x30) { + /* This report does not match its report descriptor (The descriptor ignores several fields) so + we can't use the elements in it directly.*/ + continue; } + } [exposedControllers addObject:self]; @@ -383,6 +441,11 @@ typedef struct __attribute__((packed)) { } } + if (_hacks[JOYActivationReport]) { + [self sendReport:hacks[JOYActivationReport]]; + } + + return self; } @@ -441,6 +504,21 @@ typedef struct __attribute__((packed)) { return [_hats allValues]; } +- (void)gotReport:(NSData *)report +{ + JOYFullReportElement *element = _fullReportElements[@(*(uint8_t *)report.bytes)]; + if (!element) return; + [element updateValue:report]; + + NSArray *subElements = _multiElements[element]; + if (subElements) { + for (JOYElement *subElement in subElements) { + [self _elementChanged:subElement]; + } + return; + } +} + - (void)elementChanged:(IOHIDElementRef)element { JOYElement *_element = _iokitToJOY[@(IOHIDElementGetCookie(element))]; @@ -473,16 +551,6 @@ typedef struct __attribute__((packed)) { } } - { - NSArray *subElements = _multiElements[element]; - if (subElements) { - for (JOYElement *subElement in subElements) { - [self _elementChanged:subElement]; - } - return; - } - } - if (!self.connected) return; { JOYButton *button = _buttons[element]; @@ -602,7 +670,7 @@ typedef struct __attribute__((packed)) { _lastSwitchPacket.sequence &= 0xF; _lastSwitchPacket.command = 0x30; // LED _lastSwitchPacket.commandData[0] = mask; - [self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; + //[self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; } } @@ -723,15 +791,13 @@ typedef struct __attribute__((packed)) { JOYController *controller = nil; if (filters) { controller = [[JOYMultiplayerController alloc] initWithDevice:device - reportIDFilters:filters]; + reportIDFilters:filters + hacks:hacks]; } else { - controller = [[JOYController alloc] initWithDevice:device]; - } - - if (hacks[JOYActivationReport]) { - [controller sendReport:hacks[JOYActivationReport]]; + controller = [[JOYController alloc] initWithDevice:device hacks:hacks]; } + [controllers setObject:controller forKey:[NSValue valueWithPointer:device]]; diff --git a/JoyKit/JOYFullReportElement.h b/JoyKit/JOYFullReportElement.h new file mode 100644 index 0000000..808644e --- /dev/null +++ b/JoyKit/JOYFullReportElement.h @@ -0,0 +1,10 @@ +#import +#include +#include "JOYElement.h" + +@interface JOYFullReportElement : JOYElement +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportID:(unsigned)reportID; +- (void)updateValue:(NSData *)value; +@end + + diff --git a/JoyKit/JOYFullReportElement.m b/JoyKit/JOYFullReportElement.m new file mode 100644 index 0000000..c8efb27 --- /dev/null +++ b/JoyKit/JOYFullReportElement.m @@ -0,0 +1,73 @@ +#import "JOYFullReportElement.h" +#include + +@implementation JOYFullReportElement +{ + IOHIDDeviceRef _device; + NSData *_data; + unsigned _reportID; + size_t _capacity; +} + +- (uint32_t)uniqueID +{ + return _reportID ^ 0xFFFF; +} + +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportID:(unsigned)reportID +{ + if ((self = [super init])) { + _data = [[NSMutableData alloc] initWithLength:[(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxOutputReportSizeKey)) unsignedIntValue]]; + *(uint8_t *)(((NSMutableData *)_data).mutableBytes) = reportID; + _reportID = reportID; + _device = device; + } + return self; +} + +- (int32_t)value +{ + [self doesNotRecognizeSelector:_cmd]; + return 0; +} + +- (NSData *)dataValue +{ + return _data; +} + +- (IOReturn)setValue:(uint32_t)value +{ + [self doesNotRecognizeSelector:_cmd]; + return -1; +} + +- (IOReturn)setDataValue:(NSData *)value +{ + + [self updateValue:value]; + return IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, _reportID, [_data bytes], [_data length]);; +} + +- (void)updateValue:(NSData *)value +{ + _data = [value copy]; +} + +/* For use as a dictionary key */ + +- (NSUInteger)hash +{ + return self.uniqueID; +} + +- (BOOL)isEqual:(id)object +{ + return self.uniqueID == self.uniqueID; +} + +- (id)copyWithZone:(nullable NSZone *)zone; +{ + return self; +} +@end diff --git a/JoyKit/JOYMultiplayerController.h b/JoyKit/JOYMultiplayerController.h index 24004f5..44d7421 100644 --- a/JoyKit/JOYMultiplayerController.h +++ b/JoyKit/JOYMultiplayerController.h @@ -2,7 +2,7 @@ #include @interface JOYMultiplayerController : JOYController -- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters; +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters hacks:hacks; @end diff --git a/JoyKit/JOYMultiplayerController.m b/JoyKit/JOYMultiplayerController.m index 50840a1..a31ae92 100644 --- a/JoyKit/JOYMultiplayerController.m +++ b/JoyKit/JOYMultiplayerController.m @@ -1,7 +1,7 @@ #import "JOYMultiplayerController.h" @interface JOYController () -- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix; +- (instancetype)initWithDevice:(IOHIDDeviceRef)device reportIDFilter:(NSArray *) filter serialSuffix:(NSString *)suffix hacks:(NSDictionary *)hacks; - (void)elementChanged:(IOHIDElementRef) element toValue:(IOHIDValueRef) value; - (void)disconnected; - (void)sendReport:(NSData *)report; @@ -12,7 +12,7 @@ NSMutableArray *_children; } -- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters +- (instancetype)initWithDevice:(IOHIDDeviceRef) device reportIDFilters:(NSArray *>*) reportIDFilters hacks:(NSDictionary *)hacks; { self = [super init]; if (!self) return self; @@ -21,7 +21,7 @@ unsigned index = 1; for (NSArray *filter in reportIDFilters) { - JOYController *controller = [[JOYController alloc] initWithDevice:device reportIDFilter:filter serialSuffix:[NSString stringWithFormat:@"%d", index]]; + JOYController *controller = [[JOYController alloc] initWithDevice:device reportIDFilter:filter serialSuffix:[NSString stringWithFormat:@"%d", index] hacks:hacks]; [_children addObject:controller]; index++; } From 9413d68976c6f076c19acdd54c505ce5ccddf452 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 May 2020 22:14:53 +0300 Subject: [PATCH 277/341] Add support for wired Switch Pro Controller --- JoyKit/ControllerConfiguration.inc | 40 ++++++++++++++++++++++++++++++ JoyKit/JOYAxes2D.m | 4 +-- JoyKit/JOYController.m | 4 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index 35a6cff..b7dbfe8 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -367,5 +367,45 @@ hacksByName = @{ AXES2D(1): @(JOYAxes2DUsageLeftStick), AXES2D(4): @(JOYAxes2DUsageRightStick), }, + + JOYCustomReports: @{ + @(0x30): @[ + + // For USB mode, which uses the wrong report descriptor + + @{@"reportID": @(1), @"size":@1, @"offset":@16, @"usagePage":@(kHIDPage_Button), @"usage":@3}, + @{@"reportID": @(1), @"size":@1, @"offset":@17, @"usagePage":@(kHIDPage_Button), @"usage":@4}, + @{@"reportID": @(1), @"size":@1, @"offset":@18, @"usagePage":@(kHIDPage_Button), @"usage":@1}, + @{@"reportID": @(1), @"size":@1, @"offset":@19, @"usagePage":@(kHIDPage_Button), @"usage":@2}, + + // SR and SL not used on the Pro Controller + @{@"reportID": @(1), @"size":@1, @"offset":@22, @"usagePage":@(kHIDPage_Button), @"usage":@6}, + @{@"reportID": @(1), @"size":@1, @"offset":@23, @"usagePage":@(kHIDPage_Button), @"usage":@8}, + + @{@"reportID": @(1), @"size":@1, @"offset":@24, @"usagePage":@(kHIDPage_Button), @"usage":@9}, + @{@"reportID": @(1), @"size":@1, @"offset":@25, @"usagePage":@(kHIDPage_Button), @"usage":@10}, + @{@"reportID": @(1), @"size":@1, @"offset":@26, @"usagePage":@(kHIDPage_Button), @"usage":@12}, + @{@"reportID": @(1), @"size":@1, @"offset":@27, @"usagePage":@(kHIDPage_Button), @"usage":@11}, + + @{@"reportID": @(1), @"size":@1, @"offset":@28, @"usagePage":@(kHIDPage_Button), @"usage":@13}, + @{@"reportID": @(1), @"size":@1, @"offset":@29, @"usagePage":@(kHIDPage_Button), @"usage":@14}, + + @{@"reportID": @(1), @"size":@1, @"offset":@32, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadDown)}, + @{@"reportID": @(1), @"size":@1, @"offset":@33, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadUp)}, + @{@"reportID": @(1), @"size":@1, @"offset":@34, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadRight)}, + @{@"reportID": @(1), @"size":@1, @"offset":@35, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_DPadLeft)}, + + // SR and SL not used on the Pro Controller + @{@"reportID": @(1), @"size":@1, @"offset":@38, @"usagePage":@(kHIDPage_Button), @"usage":@5}, + @{@"reportID": @(1), @"size":@1, @"offset":@39, @"usagePage":@(kHIDPage_Button), @"usage":@7}, + + /* Sticks */ + @{@"reportID": @(1), @"size":@12, @"offset":@40, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_X), @"min": @0, @"max": @0xFFF}, + @{@"reportID": @(1), @"size":@12, @"offset":@52, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Y), @"min": @0xFFF, @"max": @0}, + + @{@"reportID": @(1), @"size":@12, @"offset":@64, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @0xFFF}, + @{@"reportID": @(1), @"size":@12, @"offset":@76, @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @0xFFF, @"max": @0}, + ], + }, }, }; diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m index 624ccef..272d34f 100644 --- a/JoyKit/JOYAxes2D.m +++ b/JoyKit/JOYAxes2D.m @@ -57,8 +57,8 @@ uint16_t usage = element1.usage; _usage = JOYAxes2DUsageGeneric0 + usage - kHIDUsage_GD_X + 1; } - initialX = [_element1 value]; - initialY = [_element2 value]; + initialX = 0; + initialY = 0; minX = element1.max; minY = element2.max; maxX = element1.min; diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 131441c..beb61a3 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -445,6 +445,10 @@ typedef struct __attribute__((packed)) { [self sendReport:hacks[JOYActivationReport]]; } + if (_isSwitch) { + [self sendReport:[NSData dataWithBytes:(uint8_t[]){0x80, 0x04} length:2]]; + [self sendReport:[NSData dataWithBytes:(uint8_t[]){0x80, 0x02} length:2]]; + } return self; } From bb37f8d2f0a7dbfa5814e2d9faa897b52fdfd230 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 May 2020 23:04:12 +0300 Subject: [PATCH 278/341] Optimize Joypad initialization --- JoyKit/JOYElement.m | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/JoyKit/JOYElement.m b/JoyKit/JOYElement.m index 4050312..2432002 100644 --- a/JoyKit/JOYElement.m +++ b/JoyKit/JOYElement.m @@ -1,5 +1,6 @@ #import "JOYElement.h" #include +#include @implementation JOYElement { @@ -28,6 +29,24 @@ _max = max; } +/* Ugly hack because IOHIDDeviceCopyMatchingElements is slow */ ++ (NSArray *) cookiesToSkipForDevice:(IOHIDDeviceRef)device +{ + id _device = (__bridge id)device; + NSMutableArray *ret = objc_getAssociatedObject(_device, _cmd); + if (ret) return ret; + + ret = [NSMutableArray array]; + NSArray *nones = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(device, + (__bridge CFDictionaryRef)@{@(kIOHIDElementTypeKey): @(kIOHIDElementTypeInput_NULL)}, + 0)); + for (id none in nones) { + [ret addObject:@(IOHIDElementGetCookie((__bridge IOHIDElementRef)none))]; + } + objc_setAssociatedObject(_device, _cmd, ret, OBJC_ASSOCIATION_RETAIN); + return ret; +} + - (instancetype)initWithElement:(IOHIDElementRef)element { if ((self = [super init])) { @@ -45,14 +64,12 @@ /* Catalina added a new input type in a way that breaks cookie consistency across macOS versions, we shall adjust our cookies to to compensate */ unsigned cookieShift = 0, parentCookieShift = 0; - NSArray *nones = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(IOHIDElementGetDevice(element), - (__bridge CFDictionaryRef)@{@(kIOHIDElementTypeKey): @(kIOHIDElementTypeInput_NULL)}, - 0)); - for (id none in nones) { - if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < _uniqueID) { + + for (NSNumber *none in [JOYElement cookiesToSkipForDevice:_device]) { + if (none.unsignedIntValue < _uniqueID) { cookieShift++; } - if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < (int32_t)_parentID) { + if (none.unsignedIntValue < (int32_t)_parentID) { parentCookieShift++; } } From 6910c3d24bb973a18973fc496c564df86293dbbc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 3 May 2020 20:23:37 +0300 Subject: [PATCH 279/341] Complete DualShock 3 support --- JoyKit/ControllerConfiguration.inc | 66 +++++++++++++++++++ JoyKit/JOYController.m | 102 ++++++++++++++++++++++------- 2 files changed, 143 insertions(+), 25 deletions(-) diff --git a/JoyKit/ControllerConfiguration.inc b/JoyKit/ControllerConfiguration.inc index b7dbfe8..ea3ba9a 100644 --- a/JoyKit/ControllerConfiguration.inc +++ b/JoyKit/ControllerConfiguration.inc @@ -408,4 +408,70 @@ hacksByName = @{ ], }, }, + + JOYIgnoredReports: @(0x30), // Ignore the real 0x30 report as it's broken + + @"PLAYSTATION(R)3 Controller": @{ // DualShock 3 + JOYAxisGroups: @{ + @(kHIDUsage_GD_X): @(0), + @(kHIDUsage_GD_Y): @(0), + @(kHIDUsage_GD_Z): @(1), + @(kHIDUsage_GD_Rx): @(2), + @(kHIDUsage_GD_Ry): @(3), + @(kHIDUsage_GD_Rz): @(1), + }, + + JOYButtonUsageMapping: @{ + BUTTON(1): @(JOYButtonUsageSelect), + BUTTON(2): @(JOYButtonUsageL3), + BUTTON(3): @(JOYButtonUsageR3), + BUTTON(4): @(JOYButtonUsageStart), + BUTTON(5): @(JOYButtonUsageDPadUp), + BUTTON(6): @(JOYButtonUsageDPadRight), + BUTTON(7): @(JOYButtonUsageDPadDown), + BUTTON(8): @(JOYButtonUsageDPadLeft), + BUTTON(9): @(JOYButtonUsageL2), + BUTTON(10): @(JOYButtonUsageR2), + BUTTON(11): @(JOYButtonUsageL1), + BUTTON(12): @(JOYButtonUsageR1), + BUTTON(13): @(JOYButtonUsageX), + BUTTON(14): @(JOYButtonUsageA), + BUTTON(15): @(JOYButtonUsageB), + BUTTON(16): @(JOYButtonUsageY), + BUTTON(17): @(JOYButtonUsageHome), + }, + + JOYAxisUsageMapping: @{ + AXIS(4): @(JOYAxisUsageL1), + AXIS(5): @(JOYAxisUsageR1), + AXIS(8): @(JOYAxisUsageL2), + AXIS(9): @(JOYAxisUsageR2), + }, + + JOYAxes2DUsageMapping: @{ + AXES2D(1): @(JOYAxes2DUsageLeftStick), + AXES2D(3): @(JOYAxes2DUsageRightStick), + }, + + JOYCustomReports: @{ + @(0x01): @[ + /* Pressure sensitive inputs */ + @{@"reportID": @(1), @"size":@8, @"offset":@(13 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(14 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(15 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(16 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(17 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Dial), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(18 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Wheel), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(19 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Rx), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(20 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Ry), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(21 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(22 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(23 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + @{@"reportID": @(1), @"size":@8, @"offset":@(24 * 8), @"usagePage":@(kHIDPage_GenericDesktop), @"usage":@(kHIDUsage_GD_Slider), @"min": @0, @"max": @255}, + ] + }, + + JOYIsDualShock3: @YES, + }, + }; diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index beb61a3..015737e 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -24,6 +24,8 @@ static NSString const *JOYRumbleMin = @"JOYRumbleMin"; static NSString const *JOYRumbleMax = @"JOYRumbleMax"; static NSString const *JOYSwapZRz = @"JOYSwapZRz"; static NSString const *JOYActivationReport = @"JOYActivationReport"; +static NSString const *JOYIgnoredReports = @"JOYIgnoredReports"; +static NSString const *JOYIsDualShock3 = @"JOYIsDualShock3"; static NSMutableDictionary *controllers; // Physical controllers static NSMutableArray *exposedControllers; // Logical controllers @@ -104,6 +106,30 @@ typedef struct __attribute__((packed)) { uint8_t commandData[26]; } JOYSwitchPacket; +typedef struct __attribute__((packed)) { + uint8_t reportID; + uint8_t padding; + uint8_t rumbleRightDuration; + uint8_t rumbleRightStrength; + uint8_t rumbleLeftDuration; + uint8_t rumbleLeftStrength; + uint32_t padding2; + uint8_t ledsEnabled; + struct { + uint8_t timeEnabled; + uint8_t dutyLength; + uint8_t enabled; + uint8_t dutyOff; + uint8_t dutyOnn; + } __attribute__((packed)) led[5]; + uint8_t padding3[13]; +} JOYDualShock3Output; + +typedef union { + JOYSwitchPacket switchPacket; + JOYDualShock3Output ds3Output; +} JOYVendorSpecificOutput; + @implementation JOYController { IOHIDDeviceRef _device; @@ -124,7 +150,8 @@ typedef struct __attribute__((packed)) { NSMutableDictionary *_iokitToJOY; NSString *_serialSuffix; bool _isSwitch; // Does this controller use the Switch protocol? - JOYSwitchPacket _lastSwitchPacket; + bool _isDualShock3; // Does this controller use DS3 outputs? + JOYVendorSpecificOutput _lastVendorSpecificOutput; NSLock *_rumblePWMThreadLock; volatile double _rumblePWMRatio; bool _physicallyConnected; @@ -335,6 +362,8 @@ typedef struct __attribute__((packed)) { _hacks = hacks; _isSwitch = [_hacks[JOYIsSwitch] boolValue]; + _isDualShock3 = [_hacks[JOYIsDualShock3] boolValue]; + NSDictionary *customReports = hacks[JOYCustomReports]; if (hacks[JOYCustomReports]) { @@ -384,6 +413,11 @@ typedef struct __attribute__((packed)) { } id previous = nil; + NSSet *ignoredReports = nil; + if (hacks[ignoredReports]) { + ignoredReports = [NSSet setWithArray:hacks[ignoredReports]]; + } + for (id _element in array) { if (_element == previous) continue; // Some elements are reported twice for some reason previous = _element; @@ -409,8 +443,8 @@ typedef struct __attribute__((packed)) { case kIOHIDElementTypeCollection: continue; } - if ((!isOutput && customReports[@(element.reportID)]) || - (isOutput && customReports[@(-element.reportID)])) continue; + if ((!isOutput && [ignoredReports containsObject:@(element.reportID)]) || + (isOutput && [ignoredReports containsObject:@(-element.reportID)])) continue; if (IOHIDElementIsArray((__bridge IOHIDElementRef)_element)) continue; @@ -423,13 +457,6 @@ typedef struct __attribute__((packed)) { } _iokitToJOY[@(IOHIDElementGetCookie((__bridge IOHIDElementRef)_element))] = element; - - if (_isSwitch && element.reportID == 0x30) { - /* This report does not match its report descriptor (The descriptor ignores several fields) so - we can't use the elements in it directly.*/ - continue; - } - } [exposedControllers addObject:self]; @@ -450,6 +477,20 @@ typedef struct __attribute__((packed)) { [self sendReport:[NSData dataWithBytes:(uint8_t[]){0x80, 0x02} length:2]]; } + if (_isDualShock3) { + _lastVendorSpecificOutput.ds3Output = (JOYDualShock3Output){ + .reportID = 1, + .led = { + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, + {.timeEnabled = 0, .dutyLength = 0, .enabled = 0, .dutyOff = 0, .dutyOnn = 0}, + } + }; + + } + return self; } @@ -653,7 +694,7 @@ typedef struct __attribute__((packed)) { } } _physicallyConnected = false; - [self setRumbleAmplitude:0]; // Stop the rumble thread. + [self _forceStopPWMThread]; // Stop the rumble thread. [exposedControllers removeObject:self]; _device = nil; } @@ -669,13 +710,18 @@ typedef struct __attribute__((packed)) { { mask &= 0xF; if (_isSwitch) { - _lastSwitchPacket.reportID = 0x1; // Rumble and LEDs - _lastSwitchPacket.sequence++; - _lastSwitchPacket.sequence &= 0xF; - _lastSwitchPacket.command = 0x30; // LED - _lastSwitchPacket.commandData[0] = mask; + _lastVendorSpecificOutput.switchPacket.reportID = 0x1; // Rumble and LEDs + _lastVendorSpecificOutput.switchPacket.sequence++; + _lastVendorSpecificOutput.switchPacket.sequence &= 0xF; + _lastVendorSpecificOutput.switchPacket.command = 0x30; // LED + _lastVendorSpecificOutput.switchPacket.commandData[0] = mask; //[self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; } + else if (_isDualShock3) { + _lastVendorSpecificOutput.ds3Output.reportID = 1; + _lastVendorSpecificOutput.ds3Output.ledsEnabled = mask << 1; + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.ds3Output length:sizeof(_lastVendorSpecificOutput.ds3Output)]]; + } } - (void)pwmThread @@ -727,17 +773,23 @@ typedef struct __attribute__((packed)) { lowAmp = 0; } - _lastSwitchPacket.rumbleData[0] = _lastSwitchPacket.rumbleData[4] = highFreq & 0xFF; - _lastSwitchPacket.rumbleData[1] = _lastSwitchPacket.rumbleData[5] = (highAmp << 1) + ((highFreq >> 8) & 0x1); - _lastSwitchPacket.rumbleData[2] = _lastSwitchPacket.rumbleData[6] = lowFreq; - _lastSwitchPacket.rumbleData[3] = _lastSwitchPacket.rumbleData[7] = lowAmp; + _lastVendorSpecificOutput.switchPacket.rumbleData[0] = _lastVendorSpecificOutput.switchPacket.rumbleData[4] = highFreq & 0xFF; + _lastVendorSpecificOutput.switchPacket.rumbleData[1] = _lastVendorSpecificOutput.switchPacket.rumbleData[5] = (highAmp << 1) + ((highFreq >> 8) & 0x1); + _lastVendorSpecificOutput.switchPacket.rumbleData[2] = _lastVendorSpecificOutput.switchPacket.rumbleData[6] = lowFreq; + _lastVendorSpecificOutput.switchPacket.rumbleData[3] = _lastVendorSpecificOutput.switchPacket.rumbleData[7] = lowAmp; - _lastSwitchPacket.reportID = 0x10; // Rumble only - _lastSwitchPacket.sequence++; - _lastSwitchPacket.sequence &= 0xF; - _lastSwitchPacket.command = 0; // LED - [self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; + _lastVendorSpecificOutput.switchPacket.reportID = 0x10; // Rumble only + _lastVendorSpecificOutput.switchPacket.sequence++; + _lastVendorSpecificOutput.switchPacket.sequence &= 0xF; + _lastVendorSpecificOutput.switchPacket.command = 0; // LED + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.switchPacket length:sizeof(_lastVendorSpecificOutput.switchPacket)]]; + } + else if (_isDualShock3) { + _lastVendorSpecificOutput.ds3Output.reportID = 1; + _lastVendorSpecificOutput.ds3Output.rumbleLeftDuration = _lastVendorSpecificOutput.ds3Output.rumbleRightDuration = amp? 0xff : 0; + _lastVendorSpecificOutput.ds3Output.rumbleLeftStrength = _lastVendorSpecificOutput.ds3Output.rumbleRightStrength = amp * 0xff; + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.ds3Output length:sizeof(_lastVendorSpecificOutput.ds3Output)]]; } else { if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { From 19126df7f4b69ca8001b6d2ca6b2c703b7625195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 3 May 2020 22:41:56 +0200 Subject: [PATCH 280/341] Save 8 bytes in the CGB boot ROM --- BootROMs/cgb_boot.asm | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 6ae869b..618e11a 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -984,14 +984,13 @@ BrightenColor: and $1F cp $1F jr nz, .blueNotMaxed - res 0, c + dec c .blueNotMaxed ; Is green maxed? ld a, e - and $E0 cp $E0 - jr nz, .greenNotMaxed + jr c, .greenNotMaxed ld a, d and $3 cp $3 @@ -1007,18 +1006,13 @@ BrightenColor: res 2, b .redNotMaxed - ; Add de to bc - push hl - ld h, d - ld l, e - add hl, bc - ld d, h - ld e, l - pop hl - + ; add de, bc + ; ld [hli], de ld a, e + add c ld [hli], a ld a, d + adc b ld [hli], a ret @@ -1155,7 +1149,7 @@ ReplaceColorInAllPalettes: dec c jr nz, .loop ret - + LoadDMGTilemap: push af call WaitFrame From cb738190be6abca03d1cd3265440908107168a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 3 May 2020 22:44:13 +0200 Subject: [PATCH 281/341] Add a 2bpp CGB boot ROM logo, pending palettes --- BootROMs/SameBoyLogo.png | Bin 14763 -> 479 bytes BootROMs/cgb_boot.asm | 116 ++++++++++++++++++++++++++------------- BootROMs/pb12.py | 68 +++++++++++++++++++++++ Makefile | 13 ++--- 4 files changed, 152 insertions(+), 45 deletions(-) create mode 100644 BootROMs/pb12.py diff --git a/BootROMs/SameBoyLogo.png b/BootROMs/SameBoyLogo.png index 4bc9706080a1d5e161d506f2d2be6d9c8e1446d4..c7cfc087dbf7a26a2969fc463d116b9e28eb58ac 100644 GIT binary patch delta 465 zcmV;?0WSWlbKe7y7=Ho-0000_WgT_^000b7OjJex|Nnn~fBygg000170)~4400E9k zL_t(2&yA8XZo@DPMHzU|sHMA(LKiteI>gABqv&c1I&`dpB6F{x05ZBr`2CFIphJcl zW-MEeXKQ3lQsjN*%!4hpQ&S3sG} z^6VxHz^3k}8BhsS(Jj@7f)Dc4Cz~FVo7>Rf2rx_^Q}w%=4I_(c^4B(0xOv*U`OvMd zuWE)kbmkB!&3|3Hv*;eF4m9Dn!u~ zO32V{^YirP=K~A~#fBD}H?nKUF%Iq4y9kbaYQ6(34paiPDNr^e8o<%(0$d4jOWOm$ zj=5L>FZ$J|Bc1pQc=KLvfL`!fZ&qn_HC(2BxEwLy5=#35u>6jP-U$>M#%2~9leL~x z*UVWtYJLF4^3nnX&}Ra14CW{k86!XU`$=!6=YQ&Hl>c47@!eI<`B+HV00000NkvXX Hu0mjf?upU~ literal 14763 zcmeI3Z)_7~9LEp;L>R#cCPQJ-vp}L?dw1P+TaUJ6w1ZCQoTJDl5V+kv-7ahIj=S61 zO_L2wTtG2om>^;#z5o=>e1nl_l!QP+{1Y%lvWyUvCyV*4SD>(o!B#*4lw5lhSP$6yrGkYelFckP%>l6tF}(lS-*eTLzlZbrYHPhO(_WV28J1(XYB%Q>cwS&xI`?sf zDqs{a#dBt|E+X}i|_CWbh?!HobejwG%v4a$ujHol5hh@r{WGU~_yN#qX zHCbXn5~P4?nb60T^+BYuSl(K8xKygF)3h3T;2q?Mly#c%UL7zkz|^`82{iP;nJg+d zdA(D!G_zBi=DzdY)84(MBnpdDG!?sNS{ukH)7b2utj#Hf>xFfR zB*`jEa>QWu%c3RTPnIHWX&y~h62148rJzJ|$dZuIjFf1Fl$6*37+viMFoi}$>6D$& zsG63jN}w?kf(;%;kp(Fs*Cu!%x@&ljck@1$b&C?uyA$m-{yMP+NIt2~!4Zq0_9>|& zn$&GqelE5AfT>p3Ou#MB%-lr|DZ#ZNi$`rsA9re>qTl1R-$t*tpnSNTPHh` zigUi`qG1Ai`vqN-`!9bW<TSb#(f(3z!Ma{mtdKW1TuC$Hvd`Zvj&<-+0fcINzG`29DQzxwG%Fd9Y>g;z=bLst zb6$8@IHuB`vd*@G)61hqq$6j{t}*go-6?@=D|}{vk1I_6d8N2I@_XnZ2ZcL&$cfQ( zWDt}FHYT{Rh)@B+h0?&r1Q!+&Dj>K}8rYcN!XiQi1Q$vJ8xveuM5ut^LTO-Qf(wfX z6%bq~4QxzsVG*GMf(xaAjR`I+B2++dp){~D!G%SH3J5Ng1~w+Ru!v9r!G+Sm#sn7@ z5h@_KP#V~n;KCw81q2sL0~-@uSVX9R;6iC&V}c8d2o(@qC=F~(aA6Ul0)h*rfsF|+ zEFx4uaG^A?F~NmJgbD~Qlm<2?xUh&&0l|gRz{Ug@77;2SxKJ9{nBc-9LIngDN&_3; z6IVsy4M3p6*Z$J*{k}Iw7w&=Y2GUZrB}P$w@Lj@zw<&7kUwFMnQQa&>UEfMk!iN+! zUmMzTdKJ{(X^hmzGozc%p4k0#ebwokXYXF{E~GCF9sKjqqe};_ZU10k-ppA~y?4qN zd1Bepi)fZq`s#8JI9X?ogR5z*!0Mf_cwl)ur4faY3mu=I6C;s&nthpddy#4 zIeYNp`MYzMpW6M$@R9Ycx0jE6KKtg~TjJwu+?Q8h*{7WJ4d1%W&;9Gn#NR)yT<}X| q|J65BRq~ZvM=n-=HTda`G&Q(G{(bH7&EG-_RAWO^WO(J)9sdD_A1D9- diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 618e11a..6837615 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -87,20 +87,24 @@ ELSE .tilemapRowLoop + call .write_with_palette + + ; Repeat the 3 tiles common between E and B. This saves 27 bytes after + ; compression, with a cost of 17 bytes of code. push af - ; Switch to second VRAM Bank - ld a, 1 - ldh [$4F], a - ld [hl], 8 - ; Switch to back first VRAM Bank - xor a - ldh [$4F], a + sub $20 + sub $3 + jr nc, .notspecial + add $20 + call .write_with_palette + dec c +.notspecial pop af - ldi [hl], a - add d + + add d ; d = 3 for SameBoy logo, d = 1 for Nintendo logo dec c jr nz, .tilemapRowLoop - sub 47 + sub 44 push de ld de, $10 add hl, de @@ -116,6 +120,19 @@ ELSE ld l, $a7 ld bc, $0107 jr .tilemapRowLoop + +.write_with_palette + push af + ; Switch to second VRAM Bank + ld a, 1 + ldh [$4F], a + ld [hl], 8 + ; Switch to back first VRAM Bank + xor a + ldh [$4F], a + pop af + ldi [hl], a + ret .endTilemap ENDC @@ -532,7 +549,7 @@ TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c SameBoyLogo: - incbin "SameBoyLogo.pb8" + incbin "SameBoyLogo.pb12" AnimationColors: dw $7FFF ; White @@ -634,30 +651,28 @@ ReadCGBLogoHalfTile: ld a, e ret -; LoadTileset using PB8 codec, 2019 Damian Yerrick -; -; The logo is compressed using PB8, a form of RLE with unary-coded -; run lengths. Each block representing 8 bytes consists of a control -; byte, where each bit (MSB to LSB) is 0 for literal or 1 for repeat -; previous, followed by the literals in that block. +; LoadTileset using PB12 codec, 2020 Jakub Kądziołka +; (based on PB8 codec, 2019 Damian Yerrick) SameBoyLogo_dst = $8080 SameBoyLogo_length = (128 * 24) / 64 LoadTileset: ld hl, SameBoyLogo - ld de, SameBoyLogo_dst + ld de, SameBoyLogo_dst - 1 ld c, SameBoyLogo_length -.pb8BlockLoop: - ; Register map for PB8 decompression +.refill + ; Register map for PB12 decompression ; HL: source address in boot ROM ; DE: destination address in VRAM ; A: Current literal value ; B: Repeat bits, terminated by 1000... - ; C: Number of 8-byte blocks left in this block ; Source address in HL lets the repeat bits go straight to B, ; bypassing A and avoiding spilling registers to the stack. ld b, [hl] + dec b + jr z, .sameboyLogoEnd + inc b inc hl ; Shift a 1 into lower bit of shift value. Once this bit @@ -665,26 +680,53 @@ LoadTileset: scf rl b -.pb8BitLoop: +.loop ; If not a repeat, load a literal byte - jr c,.pb8Repeat - ld a, [hli] -.pb8Repeat: - ; Decompressed data uses colors 0 and 1, so write once, inc twice - ld [de], a - inc de - inc de + jr c, .simple_repeat sla b - jr nz, .pb8BitLoop - - dec c - jr nz, .pb8BlockLoop - -; End PB8 decoding. The rest uses HL as the destination - ld h, d - ld l, e + jr c, .shifty_repeat + ld a, [hli] + jr .got_byte +.shifty_repeat + sla b + jr nz, .no_refill_during_shift + ld b, [hl] ; see above. Also, no, factoring it out into a callable + inc hl ; routine doesn't save bytes, even with conditional calls + scf + rl b +.no_refill_during_shift + ld c, a + jr nc, .shift_left + srl a + db $fe ; eat the add a with cp d8 +.shift_left + add a + sla b + jr c, .go_and + or c + db $fe ; eat the and c with cp d8 +.go_and + and c + jr .got_byte +.simple_repeat + sla b + jr c, .got_byte + ; far repeat + dec de + ld a, [de] + inc de +.got_byte + inc de + ld [de], a + sla b + jr nz, .loop + jr .refill +; End PB12 decoding. The rest uses HL as the destination .sameboyLogoEnd + ld h, d + ld l, $80 + ; Copy (unresized) ROM logo ld de, $104 .CGBROMLogoLoop diff --git a/BootROMs/pb12.py b/BootROMs/pb12.py new file mode 100644 index 0000000..0c06538 --- /dev/null +++ b/BootROMs/pb12.py @@ -0,0 +1,68 @@ +import sys + +def opts(byte): + # top bit: 0 = left, 1 = right + # bottom bit: 0 = or, 1 = and + if byte is None: return [] + return [ + byte | (byte << 1) & 0xff, + byte & (byte << 1), + byte | (byte >> 1) & 0xff, + byte & (byte >> 1), + ] + +def pb12(data): + data = iter(data) + + literals = bytearray() + bits = 0 + control = 0 + prev = [None, None] + gotta_end = False + + chunk = bytearray() + while True: + try: + byte = next(data) + except StopIteration: + if bits == 0: break + byte = 0 + chunk.append(byte) + + if byte in prev: + bits += 2 + control <<= 1 + control |= 1 + control <<= 1 + if prev[1] == byte: + control |= 1 # 10 = out[-2], 11 = out[-1] + else: + bits += 2 + control <<= 2 + options = opts(prev[1]) + if byte in options: + # 01 = modify + control |= 1 + + bits += 2 + control <<= 2 + control |= options.index(byte) + else: + # 00 = literal + literals.append(byte) + prev = [prev[1], byte] + if bits >= 8: + outctl = control >> (bits - 8) + assert outctl != 1 # that's the end byte + yield bytes([outctl]) + literals + bits -= 8 + control &= (1 << bits) - 1 + literals = bytearray() + chunk = bytearray() + yield b'\x01' + +_, infile, outfile = sys.argv +with open(infile, 'rb') as f: + data = f.read() +with open(outfile, 'wb') as f: + f.writelines(pb12(data)) diff --git a/Makefile b/Makefile index afc8d89..1f81862 100644 --- a/Makefile +++ b/Makefile @@ -382,21 +382,18 @@ $(BIN)/SDL/Shaders: Shaders # Boot ROMs -$(OBJ)/%.1bpp: %.png +$(OBJ)/%.2bpp: %.png -@$(MKDIR) -p $(dir $@) - rgbgfx -d 1 -h -o $@ $< + rgbgfx -h -u -o $@ $< -$(OBJ)/BootROMs/SameBoyLogo.pb8: $(OBJ)/BootROMs/SameBoyLogo.1bpp $(PB8_COMPRESS) - $(realpath $(PB8_COMPRESS)) -l 384 $< $@ - -$(PB8_COMPRESS): BootROMs/pb8.c - $(CC) $< -o $@ +$(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp BootROMs/pb12.py + python3 BootROMs/pb12.py $< $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/sgb2_boot: BootROMs/sgb_boot.asm -$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.pb8 +$(BIN)/BootROMs/%.bin: BootROMs/%.asm $(OBJ)/BootROMs/SameBoyLogo.pb12 -@$(MKDIR) -p $(dir $@) rgbasm -i $(OBJ)/BootROMs/ -i BootROMs/ -o $@.tmp $< rgblink -o $@.tmp2 $@.tmp From b057e0d10af8394671e2af8d463c2f948e860f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Sun, 3 May 2020 23:07:53 +0200 Subject: [PATCH 282/341] Save 4 more bytes in the CGB boot ROM --- BootROMs/cgb_boot.asm | 52 ++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 6837615..1d9cc6a 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -21,7 +21,7 @@ Start: ldh [InputPalette], a ; Clear title checksum ldh [TitleChecksum], a - + ; Clear OAM ld h, $fe ld c, $a0 @@ -753,29 +753,6 @@ ReadTrademarkSymbol: jr nz, .loadTrademarkSymbolLoop ret -LoadObjPalettes: - ld c, $6A - jr LoadPalettes - -LoadBGPalettes64: - ld d, 64 - -LoadBGPalettes: - ld e, 0 - ld c, $68 - -LoadPalettes: - ld a, $80 - or e - ld [c], a - inc c -.loop - ld a, [hli] - ld [c], a - dec d - jr nz, .loop - ret - DoIntroAnimation: ; Animate the intro ld a, 1 @@ -902,8 +879,7 @@ EmulateDMG: call LoadPalettesFromIndex ld a, 4 ; Set the final values for DMG mode - ld d, 0 - ld e, $8 + ld de, 8 ld l, $7c ret @@ -997,7 +973,8 @@ LoadPalettesFromIndex: ; a = index of combination ld c, a add hl, bc ld d, 8 - call LoadObjPalettes + ld c, $6A + call LoadPalettes pop hl bit 3, e jr nz, .loadBGPalette @@ -1011,7 +988,26 @@ LoadPalettesFromIndex: ; a = index of combination ld c, a add hl, bc ld d, 8 - jp LoadBGPalettes + jr LoadBGPalettes + +LoadBGPalettes64: + ld d, 64 + +LoadBGPalettes: + ld e, 0 + ld c, $68 + +LoadPalettes: + ld a, $80 + or e + ld [c], a + inc c +.loop + ld a, [hli] + ld [c], a + dec d + jr nz, .loop + ret BrightenColor: ld a, [hli] From 2225fd114c795a4a607fa54267a2a695c80a83d6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 4 May 2020 02:07:19 +0300 Subject: [PATCH 283/341] Handle 2bpp palettes --- BootROMs/cgb_boot.asm | 74 +++++++++++++++++++------------------------ BootROMs/pb12.py | 2 +- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 1d9cc6a..9408788 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -138,41 +138,24 @@ ENDC ; Expand Palettes ld de, AnimationColors - ld c, 8 + ld c, 16 ld hl, BgPalettes xor a .expandPalettesLoop: -IF !DEF(FAST) cpl -ENDC - ; One white + ; One white or black ldi [hl], a ldi [hl], a - -IF DEF(FAST) - ; 3 more whites - ldi [hl], a - ldi [hl], a - ldi [hl], a - ldi [hl], a - ldi [hl], a - ldi [hl], a -ELSE - ; The actual color + + ; One color + push af ld a, [de] inc de ldi [hl], a ld a, [de] inc de ldi [hl], a - - xor a - ; Two blacks - ldi [hl], a - ldi [hl], a - ldi [hl], a - ldi [hl], a -ENDC + pop af dec c jr nz, .expandPalettesLoop @@ -551,19 +534,20 @@ TrademarkSymbol: SameBoyLogo: incbin "SameBoyLogo.pb12" -AnimationColors: - dw $7FFF ; White - dw $774F ; Cyan - dw $22C7 ; Green - dw $039F ; Yellow - dw $017D ; Orange - dw $241D ; Red - dw $6D38 ; Purple - dw $7102 ; Blue -AnimationColorsEnd: +animation_color: MACRO + dw ((\1) >> 1) | $4210, (\1) +ENDM -DMGPalettes: - dw $7FFF, $32BF, $00D0, $0000 +AnimationColors: + animation_color $7FFF, ($7FFF >> 1) ; White + animation_color $774F, ($774F >> 1) ; Cyan + animation_color $22C7, ($22C7 >> 1) ; Green + animation_color $039F, ($039F >> 1) ; Yellow + animation_color $017D, ($017D >> 1) ; Orange + animation_color $241D, ($241D >> 1) ; Red + animation_color $6D38, ($6D38 >> 1) ; Purple + animation_color $7102, ($7102 >> 1) ; Blue +AnimationColorsEnd: ; Helper Functions DoubleBitsAndWriteRowTwice: @@ -1140,27 +1124,33 @@ ChangeAnimationPalette: .isWhite push af ld a, [hli] + push hl - ld hl, BgPalettes ; First color, all palette + ld hl, BgPalettes ; First color, all palettes + call ReplaceColorInAllPalettes + ld l, LOW(BgPalettes + 2) ; Second color, all palettes call ReplaceColorInAllPalettes pop hl - ldh [BgPalettes + 2], a ; Second color, first palette + ldh [BgPalettes + 6], a ; Fourth color, first palette ld a, [hli] push hl - ld hl, BgPalettes + 1 ; First color, all palette + ld hl, BgPalettes + 1 ; First color, all palettes + call ReplaceColorInAllPalettes + ld l, LOW(BgPalettes + 3) ; Second color, all palettes call ReplaceColorInAllPalettes pop hl - ldh [BgPalettes + 3], a ; Second color, first palette + ldh [BgPalettes + 7], a ; Fourth color, first palette + pop af jr z, .isNotWhite inc hl inc hl .isNotWhite ld a, [hli] - ldh [BgPalettes + 7 * 8 + 2], a ; Second color, 7th palette + ldh [BgPalettes + 7 * 8 + 6], a ; Fourth color, 7th palette ld a, [hli] - ldh [BgPalettes + 7 * 8 + 3], a ; Second color, 7th palette + ldh [BgPalettes + 7 * 8 + 7], a ; Fourth color, 7th palette ld a, [hli] ldh [BgPalettes + 4], a ; Third color, first palette ld a, [hl] @@ -1180,7 +1170,7 @@ ChangeAnimationPalette: ReplaceColorInAllPalettes: ld de, 8 - ld c, 8 + ld c, e .loop ld [hl], a add hl, de diff --git a/BootROMs/pb12.py b/BootROMs/pb12.py index 0c06538..dc53d6b 100644 --- a/BootROMs/pb12.py +++ b/BootROMs/pb12.py @@ -63,6 +63,6 @@ def pb12(data): _, infile, outfile = sys.argv with open(infile, 'rb') as f: - data = f.read() + data = f.read().rstrip(b'\x00') with open(outfile, 'wb') as f: f.writelines(pb12(data)) From 72a90ba91c8b8ee07859fc25f6b536a5f5afaaad Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 4 May 2020 02:17:03 +0300 Subject: [PATCH 284/341] Hacky color blending --- BootROMs/cgb_boot.asm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 9408788..cbbcf96 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -1093,12 +1093,11 @@ GetInputPaletteIndex: ; Slide into change Animation Palette ChangeAnimationPalette: - push af push hl push bc push de ld hl, KeyCombinationPalettes - 1 ; Input palettes are 1-based, 0 means nothing down - ld c ,a + ld c, a ld b, 0 add hl, bc ld a, [hl] @@ -1149,6 +1148,7 @@ ChangeAnimationPalette: .isNotWhite ld a, [hli] ldh [BgPalettes + 7 * 8 + 6], a ; Fourth color, 7th palette + ldh [BgPalettes + 7 * 8 + 2], a ; Second color, half, 7th palette; rough color mixing ld a, [hli] ldh [BgPalettes + 7 * 8 + 7], a ; Fourth color, 7th palette ld a, [hli] @@ -1165,7 +1165,6 @@ ChangeAnimationPalette: pop de pop bc pop hl - pop af ret ReplaceColorInAllPalettes: From 260f61f33a5df32f1bd1542e18d645591a189758 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 4 May 2020 22:48:00 +0300 Subject: [PATCH 285/341] =?UTF-8?q?This=20window=20shouldn=E2=80=99t=20be?= =?UTF-8?q?=20resizeable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cocoa/Document.xib | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index e1c5fa0..81ce018 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -116,7 +116,7 @@ - + @@ -153,7 +153,7 @@ - + @@ -187,7 +187,7 @@ - + @@ -312,7 +312,7 @@ - + @@ -970,7 +970,7 @@ - + From f46f138e9fbda089a1a9f71b88d608b38b932315 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 4 May 2020 23:54:43 +0300 Subject: [PATCH 286/341] Clear VRAM correctly --- BootROMs/cgb_boot.asm | 155 ++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 81 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index cbbcf96..33e18e5 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -16,6 +16,15 @@ Start: xor a call ClearMemoryPage ld [c], a + + ld hl, $FF30 +; Init waveform + ld c, $10 +.waveformLoop + ldi [hl], a + cpl + dec c + jr nz, .waveformLoop ; Clear chosen input palette ldh [InputPalette], a @@ -40,8 +49,6 @@ Start: ld a, $77 ldh [$24], a - call InitWaveform - ; Init BG palette ld a, $fc ldh [$47], a @@ -776,25 +783,68 @@ IF !DEF(FAST) ld hl, BgPalettes .frameLoop push bc - call BrightenColor + + ; Brighten Color + ld a, [hli] + ld e, a + ld a, [hld] + ld d, a + ; RGB(1,1,1) + ld bc, $421 + + ; Is blue maxed? + ld a, e + and $1F + cp $1F + jr nz, .blueNotMaxed + dec c +.blueNotMaxed + + ; Is green maxed? + ld a, e + cp $E0 + jr c, .greenNotMaxed + ld a, d + and $3 + cp $3 + jr nz, .greenNotMaxed + res 5, c +.greenNotMaxed + + ; Is red maxed? + ld a, d + and $7C + cp $7C + jr nz, .redNotMaxed + res 2, b +.redNotMaxed + + ; add de, bc + ; ld [hli], de + ld a, e + add c + ld [hli], a + ld a, d + adc b + ld [hli], a pop bc + dec c jr nz, .frameLoop - call WaitFrame call WaitFrame ld hl, BgPalettes call LoadBGPalettes64 + call WaitFrame dec b jr nz, .fadeLoop ENDC + ld a, 1 call ClearVRAMViaHDMA - ; Select the first bank - xor a - ldh [$4F], a - cpl + call _ClearVRAMViaHDMA + call ClearVRAMViaHDMA ; A = $40, so it's bank 0 + ; A should be $FF ldh [$00], a - call ClearVRAMViaHDMA ; Final values for CGB mode ld de, $ff56 @@ -993,70 +1043,24 @@ LoadPalettes: jr nz, .loop ret -BrightenColor: - ld a, [hli] - ld e, a - ld a, [hld] - ld d, a - ; RGB(1,1,1) - ld bc, $421 - - ; Is blue maxed? - ld a, e - and $1F - cp $1F - jr nz, .blueNotMaxed - dec c -.blueNotMaxed - - ; Is green maxed? - ld a, e - cp $E0 - jr c, .greenNotMaxed - ld a, d - and $3 - cp $3 - jr nz, .greenNotMaxed - res 5, c -.greenNotMaxed - - ; Is red maxed? - ld a, d - and $7C - cp $7C - jr nz, .redNotMaxed - res 2, b -.redNotMaxed - - ; add de, bc - ; ld [hli], de - ld a, e - add c - ld [hli], a - ld a, d - adc b - ld [hli], a - ret - ClearVRAMViaHDMA: - ld hl, $FF51 - - ; Src - ld a, $88 - ld [hli], a - xor a - ld [hli], a - - ; Dest - ld a, $98 - ld [hli], a - ld a, $A0 - ld [hli], a - - ; Do it - ld [hl], $12 + ldh [$4F], a + ld hl, HDMAData +_ClearVRAMViaHDMA: + ld c, $51 + ld b, 5 +.loop + ld a, [hli] + ldh [c], a + inc c + dec b + jr nz, .loop ret +HDMAData: + db $88, $00, $98, $A0, $12 + db $88, $00, $80, $00, $40 + GetInputPaletteIndex: ld a, $20 ; Select directions ldh [$00], a @@ -1196,17 +1200,6 @@ LoadDMGTilemap: pop af ret -InitWaveform: - ld hl, $FF30 -; Init waveform - xor a - ld c, $10 -.waveformLoop - ldi [hl], a - cpl - dec c - jr nz, .waveformLoop - ret SECTION "ROMMax", ROM0[$900] ; Prevent us from overflowing From a3f261184d2a732b32eb30d6b3538b04fba31988 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 5 May 2020 01:44:48 +0300 Subject: [PATCH 287/341] Optimize more --- BootROMs/cgb_boot.asm | 65 +++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 33e18e5..a83c27a 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -6,8 +6,7 @@ Start: ld sp, $fffe ; Clear memory VRAM - ld hl, $8000 - call ClearMemoryPage + call ClearMemoryPage8000 ld a, 2 ld c, $70 ld [c], a @@ -74,9 +73,7 @@ Start: ; Clear the second VRAM bank ld a, 1 ldh [$4F], a - xor a - ld hl, $8000 - call ClearMemoryPage + call ClearMemoryPage8000 call LoadTileset ld b, 3 @@ -177,8 +174,10 @@ ENDC IF !DEF(FAST) call DoIntroAnimation + ld a, 45 + ldh [WaitLoopCounter], a ; Wait ~0.75 seconds - ld b, 45 + ld b, a call WaitBFrames ; Play first sound @@ -189,10 +188,6 @@ IF !DEF(FAST) ; Play second sound ld a, $c1 call PlaySound - -; Wait ~0.5 seconds - ld a, 30 - ldh [WaitLoopCounter], a .waitLoop call GetInputPaletteIndex @@ -602,8 +597,11 @@ PlaySound: ldh [$14], a ret +ClearMemoryPage8000: + ld hl, $8000 ; Clear from HL to HL | 0x2000 ClearMemoryPage: + xor a ldi [hl], a bit 5, h jr z, ClearMemoryPage @@ -781,6 +779,7 @@ IF !DEF(FAST) .fadeLoop ld c, 32 ; 32 colors to fade ld hl, BgPalettes + push hl .frameLoop push bc @@ -833,7 +832,7 @@ IF !DEF(FAST) jr nz, .frameLoop call WaitFrame - ld hl, BgPalettes + pop hl call LoadBGPalettes64 call WaitFrame dec b @@ -888,6 +887,14 @@ ENDC ldh [$4C], a ld a, $1 ret + +GetKeyComboPalette: + ld hl, KeyCombinationPalettes - 1 ; Return value is 1-based, 0 means nothing down + ld c ,a + ld b, 0 + add hl, bc + ld a, [hl] + ret EmulateDMG: ld a, 1 @@ -900,11 +907,7 @@ EmulateDMG: ldh a, [InputPalette] and a jr z, .nothingDown - ld hl, KeyCombinationPalettes - 1 ; Return value is 1-based, 0 means nothing down - ld c ,a - ld b, 0 - add hl, bc - ld a, [hl] + call GetKeyComboPalette jr .paletteFromKeys .nothingDown ld a, b @@ -985,17 +988,21 @@ GetPaletteIndex: .notNintendo xor a ret - -LoadPalettesFromIndex: ; a = index of combination + +GetPaletteCombo: ld b, a ; Multiply by 3 add b add b - + ld hl, PaletteCombinations ld b, 0 ld c, a add hl, bc + ret + +LoadPalettesFromIndex: ; a = index of combination + call GetPaletteCombo ; Obj Palettes ld e, 0 @@ -1100,20 +1107,10 @@ ChangeAnimationPalette: push hl push bc push de - ld hl, KeyCombinationPalettes - 1 ; Input palettes are 1-based, 0 means nothing down - ld c, a - ld b, 0 - add hl, bc - ld a, [hl] - ld b, a - ; Multiply by 3 - add b - add b - - ld hl, PaletteCombinations + 2; Background Palette - ld b, 0 - ld c, a - add hl, bc + call GetKeyComboPalette + call GetPaletteCombo + inc l + inc l ld a, [hl] ld hl, Palettes + 1 ld b, 0 @@ -1164,7 +1161,7 @@ ChangeAnimationPalette: ld hl, BgPalettes call LoadBGPalettes64 ; Delay the wait loop while the user is selecting a palette - ld a, 30 + ld a, 45 ldh [WaitLoopCounter], a pop de pop bc From 730567dc609a1213d6395ca92d4be00c072a2817 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 6 May 2020 01:06:22 +0300 Subject: [PATCH 288/341] Proper color mixing --- BootROMs/cgb_boot.asm | 198 ++++++++++++++++++++++++------------------ 1 file changed, 115 insertions(+), 83 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index a83c27a..332279c 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -10,35 +10,16 @@ Start: ld a, 2 ld c, $70 ld [c], a -; Clear RAM Bank 2 (Like the original boot ROM +; Clear RAM Bank 2 (Like the original boot ROM) ld h, $D0 - xor a call ClearMemoryPage ld [c], a - ld hl, $FF30 -; Init waveform - ld c, $10 -.waveformLoop - ldi [hl], a - cpl - dec c - jr nz, .waveformLoop - ; Clear chosen input palette ldh [InputPalette], a ; Clear title checksum ldh [TitleChecksum], a - -; Clear OAM - ld h, $fe - ld c, $a0 -.clearOAMLoop - ldi [hl], a - dec c - jr nz, .clearOAMLoop - -; Init Audio + ld a, $80 ldh [$26], a ldh [$11], a @@ -47,6 +28,23 @@ Start: ldh [$25], a ld a, $77 ldh [$24], a + ld hl, $FF30 +; Init waveform + ld c, $10 +.waveformLoop + ldi [hl], a + cpl + dec c + jr nz, .waveformLoop + + +; Clear OAM + ld h, $fe + ld c, $a0 +.clearOAMLoop + ldi [hl], a + dec c + jr nz, .clearOAMLoop ; Init BG palette ld a, $fc @@ -142,25 +140,44 @@ ENDC ; Expand Palettes ld de, AnimationColors - ld c, 16 + ld c, 8 ld hl, BgPalettes xor a .expandPalettesLoop: cpl - ; One white or black - ldi [hl], a - ldi [hl], a + ; One white + ld [hli], a + ld [hli], a + + ; Mixed with white + ld a, [de] + inc e + or $20 + ld b, a + + ld a, [de] + dec e + or $84 + rra + rr b + ld [hl], b + inc l + ld [hli], a + + ; One black + xor a + ld [hli], a + ld [hli], a ; One color - push af ld a, [de] - inc de - ldi [hl], a + inc e + ld [hli], a ld a, [de] - inc de - ldi [hl], a - pop af - + inc e + ld [hli], a + + xor a dec c jr nz, .expandPalettesLoop @@ -200,6 +217,9 @@ ELSE call PlaySound ENDC call Preboot +IF DEF(AGB) + ld b, 1 +ENDC ; Will be filled with NOPs @@ -208,7 +228,6 @@ BootGame: ldh [$50], a SECTION "MoreStuff", ROM0[$200] - ; Game Palettes Data TitleChecksums: db $00 ; Default @@ -512,43 +531,40 @@ Palettes: dw $4778, $3290, $1D87, $0861 ; DMG LCD KeyCombinationPalettes - db 1 ; Right - db 48 ; Left - db 5 ; Up - db 8 ; Down - db 0 ; Right + A - db 40 ; Left + A - db 43 ; Up + A - db 3 ; Down + A - db 6 ; Right + B - db 7 ; Left + B - db 28 ; Up + B - db 49 ; Down + B + db 1 * 3 ; Right + db 48 * 3 ; Left + db 5 * 3 ; Up + db 8 * 3 ; Down + db 0 * 3 ; Right + A + db 40 * 3 ; Left + A + db 43 * 3 ; Up + A + db 3 * 3 ; Down + A + db 6 * 3 ; Right + B + db 7 * 3 ; Left + B + db 28 * 3 ; Up + B + db 49 * 3 ; Down + B ; SameBoy "Exclusives" - db 51 ; Right + A + B - db 52 ; Left + A + B - db 53 ; Up + A + B - db 54 ; Down + A + B - + db 51 * 3 ; Right + A + B + db 52 * 3 ; Left + A + B + db 53 * 3 ; Up + A + B + db 54 * 3 ; Down + A + B + TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c SameBoyLogo: incbin "SameBoyLogo.pb12" -animation_color: MACRO - dw ((\1) >> 1) | $4210, (\1) -ENDM AnimationColors: - animation_color $7FFF, ($7FFF >> 1) ; White - animation_color $774F, ($774F >> 1) ; Cyan - animation_color $22C7, ($22C7 >> 1) ; Green - animation_color $039F, ($039F >> 1) ; Yellow - animation_color $017D, ($017D >> 1) ; Orange - animation_color $241D, ($241D >> 1) ; Red - animation_color $6D38, ($6D38 >> 1) ; Purple - animation_color $7102, ($7102 >> 1) ; Blue + dw $7FFF ; White + dw $774F ; Cyan + dw $22C7 ; Green + dw $039F ; Yellow + dw $017D ; Orange + dw $241D ; Red + dw $6D38 ; Purple + dw $7102 ; Blue AnimationColorsEnd: ; Helper Functions @@ -842,11 +858,13 @@ ENDC call ClearVRAMViaHDMA call _ClearVRAMViaHDMA call ClearVRAMViaHDMA ; A = $40, so it's bank 0 + cpl ; A should be $FF ldh [$00], a ; Final values for CGB mode - ld de, $ff56 + ld d, a + ld e, c ld l, $0d ld a, [$143] @@ -870,7 +888,7 @@ IF DEF(AGB) ld c, a add a, $11 ld h, c - ld b, 1 + ; B is set to 1 after ret ELSE ; Set registers to match the original CGB boot ; AF = $1180, C = 0 @@ -990,11 +1008,6 @@ GetPaletteIndex: ret GetPaletteCombo: - ld b, a - ; Multiply by 3 - add b - add b - ld hl, PaletteCombinations ld b, 0 ld c, a @@ -1064,10 +1077,6 @@ _ClearVRAMViaHDMA: jr nz, .loop ret -HDMAData: - db $88, $00, $98, $A0, $12 - db $88, $00, $80, $00, $40 - GetInputPaletteIndex: ld a, $20 ; Select directions ldh [$00], a @@ -1104,7 +1113,6 @@ GetInputPaletteIndex: ; Slide into change Animation Palette ChangeAnimationPalette: - push hl push bc push de call GetKeyComboPalette @@ -1147,16 +1155,37 @@ ChangeAnimationPalette: inc hl inc hl .isNotWhite + ; Mixing code by ISSOtm + ldh a, [BgPalettes + 7 * 8 + 2] + and ~$21 + ld b, a + ld a, [hli] + and ~$21 + add a, b + ld b, a + ld a, [BgPalettes + 7 * 8 + 3] + res 2, a ; and ~$04, but not touching carry + ld c, [hl] + res 2, c ; and ~$04, but not touching carry + adc a, c + rra ; Carry sort of "extends" the accumulator, we're bringing that bit back home + ld [BgPalettes + 7 * 8 + 3], a + ld a, b + rra + ld [BgPalettes + 7 * 8 + 2], a + dec l + ld a, [hli] ldh [BgPalettes + 7 * 8 + 6], a ; Fourth color, 7th palette - ldh [BgPalettes + 7 * 8 + 2], a ; Second color, half, 7th palette; rough color mixing ld a, [hli] ldh [BgPalettes + 7 * 8 + 7], a ; Fourth color, 7th palette + ld a, [hli] ldh [BgPalettes + 4], a ; Third color, first palette - ld a, [hl] + ld a, [hli] ldh [BgPalettes + 5], a ; Third color, first palette + call WaitFrame ld hl, BgPalettes call LoadBGPalettes64 @@ -1165,7 +1194,6 @@ ChangeAnimationPalette: ldh [WaitLoopCounter], a pop de pop bc - pop hl ret ReplaceColorInAllPalettes: @@ -1181,7 +1209,7 @@ ReplaceColorInAllPalettes: LoadDMGTilemap: push af call WaitFrame - ld a,$19 ; Trademark symbol + ld a, $19 ; Trademark symbol ld [$9910], a ; ... put in the superscript position ld hl,$992f ; Bottom right corner of the logo ld c,$c ; Tiles in a logo row @@ -1191,16 +1219,20 @@ LoadDMGTilemap: ldd [hl], a dec c jr nz, .tilemapLoop - ld l,$0f ; Jump to top row + ld l, $0f ; Jump to top row jr .tilemapLoop .tilemapDone pop af ret - - -SECTION "ROMMax", ROM0[$900] - ; Prevent us from overflowing - ds 1 + +HDMAData: + db $88, $00, $98, $A0, $12 + db $88, $00, $80, $00, $40 + +BootEnd: +IF BootEnd > $900 + FAIL "BootROM overflowed: {BootEnd}" +ENDC SECTION "HRAM", HRAM[$FF80] TitleChecksum: From 184743637eebf34f5fc673057dbca954037f3599 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 6 May 2020 01:10:46 +0300 Subject: [PATCH 289/341] Fix silly regression --- BootROMs/cgb_boot.asm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 332279c..f07fbcf 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -858,8 +858,7 @@ ENDC call ClearVRAMViaHDMA call _ClearVRAMViaHDMA call ClearVRAMViaHDMA ; A = $40, so it's bank 0 - cpl - ; A should be $FF + ld a, $ff ldh [$00], a ; Final values for CGB mode From 7cff35368d26e87a0638e9be2d56642665af0ac0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 6 May 2020 23:30:01 +0300 Subject: [PATCH 290/341] Port to C to remove the Python dep, remove leftovers --- BootROMs/pb12.c | 95 +++++++++++++ BootROMs/pb12.py | 68 ---------- BootROMs/pb8.c | 341 ----------------------------------------------- Makefile | 9 +- 4 files changed, 101 insertions(+), 412 deletions(-) create mode 100644 BootROMs/pb12.c delete mode 100644 BootROMs/pb12.py delete mode 100644 BootROMs/pb8.c diff --git a/BootROMs/pb12.c b/BootROMs/pb12.c new file mode 100644 index 0000000..878dd0d --- /dev/null +++ b/BootROMs/pb12.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include + +void opts(uint8_t byte, uint8_t *options) +{ + *(options++) = byte | ((byte << 1) & 0xff); + *(options++) = byte & (byte << 1); + *(options++) = byte | ((byte >> 1) & 0xff); + *(options++) = byte & (byte >> 1); +} + +int main() +{ + static uint8_t source[0x4000]; + size_t size = read(STDIN_FILENO, &source, sizeof(source)); + unsigned pos = 0; + assert(size <= 0x4000); + while (size && source[size - 1] == 0) { + size--; + } + + uint8_t *literals = NULL; + size_t literals_size = 0; + unsigned bits = 0; + unsigned control = 0; + unsigned prev[2] = {-1, -1}; // Unsigned to allow "not set" values + + while (true) { + + uint8_t byte = 0; + if (pos == size){ + if (bits == 0) break; + } + else { + byte = source[pos++]; + } + + if (byte == prev[0] || byte == prev[1]) { + bits += 2; + control <<= 1; + control |= 1; + control <<= 1; + if (byte == prev[1]) { + control |= 1; + } + } + else { + bits += 2; + control <<= 2; + uint8_t options[4]; + opts(prev[1], options); + bool found = false; + for (unsigned i = 0; i < 4; i++) { + if (options[i] == byte) { + // 01 = modify + control |= 1; + + bits += 2; + control <<= 2; + control |= i; + found = true; + break; + } + } + if (!found) { + literals = realloc(literals, literals_size++); + literals[literals_size - 1] = byte; + } + } + + prev[0] = prev[1]; + prev[1] = byte; + if (bits >= 8) { + uint8_t outctl = control >> (bits - 8); + assert(outctl != 1); + write(STDOUT_FILENO, &outctl, 1); + write(STDOUT_FILENO, literals, literals_size); + bits -= 8; + control &= (1 << bits) - 1; + literals_size = 0; + } + } + uint8_t end_byte = 1; + write(STDOUT_FILENO, &end_byte, 1); + + if (literals) { + free(literals); + } + + return 0; +} diff --git a/BootROMs/pb12.py b/BootROMs/pb12.py deleted file mode 100644 index dc53d6b..0000000 --- a/BootROMs/pb12.py +++ /dev/null @@ -1,68 +0,0 @@ -import sys - -def opts(byte): - # top bit: 0 = left, 1 = right - # bottom bit: 0 = or, 1 = and - if byte is None: return [] - return [ - byte | (byte << 1) & 0xff, - byte & (byte << 1), - byte | (byte >> 1) & 0xff, - byte & (byte >> 1), - ] - -def pb12(data): - data = iter(data) - - literals = bytearray() - bits = 0 - control = 0 - prev = [None, None] - gotta_end = False - - chunk = bytearray() - while True: - try: - byte = next(data) - except StopIteration: - if bits == 0: break - byte = 0 - chunk.append(byte) - - if byte in prev: - bits += 2 - control <<= 1 - control |= 1 - control <<= 1 - if prev[1] == byte: - control |= 1 # 10 = out[-2], 11 = out[-1] - else: - bits += 2 - control <<= 2 - options = opts(prev[1]) - if byte in options: - # 01 = modify - control |= 1 - - bits += 2 - control <<= 2 - control |= options.index(byte) - else: - # 00 = literal - literals.append(byte) - prev = [prev[1], byte] - if bits >= 8: - outctl = control >> (bits - 8) - assert outctl != 1 # that's the end byte - yield bytes([outctl]) + literals - bits -= 8 - control &= (1 << bits) - 1 - literals = bytearray() - chunk = bytearray() - yield b'\x01' - -_, infile, outfile = sys.argv -with open(infile, 'rb') as f: - data = f.read().rstrip(b'\x00') -with open(outfile, 'wb') as f: - f.writelines(pb12(data)) diff --git a/BootROMs/pb8.c b/BootROMs/pb8.c deleted file mode 100644 index 4ee4524..0000000 --- a/BootROMs/pb8.c +++ /dev/null @@ -1,341 +0,0 @@ -/* - -PB8 compressor and decompressor - -Copyright 2019 Damian Yerrick - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. -2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. -3. This notice may not be removed or altered from any source distribution. - -*/ - -#include -#include -#include -#include -#include -#include - -// For setting stdin/stdout to binary mode -#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) -#include -#define fd_isatty isatty -#elif defined (_WIN32) -#include -#include -#define fd_isatty _isatty -#endif - -/* - -; The logo is compressed using PB8, a form of RLE with unary-coded -; run lengths. Each block representing 8 bytes consists of a control -; byte, where each bit (MSB to LSB) is 0 for literal or 1 for repeat -; previous, followed by the literals in that block. - -SameBoyLogo_dst = $8080 -SameBoyLogo_length = (128 * 24) / 64 - -LoadTileset: - ld hl, SameBoyLogo - ld de, SameBoyLogo_dst - ld c, SameBoyLogo_length -.pb8BlockLoop: - ; Register map for PB8 decompression - ; HL: source address in boot ROM - ; DE: destination address in VRAM - ; A: Current literal value - ; B: Repeat bits, terminated by 1000... - ; C: Number of 8-byte blocks left in this block - ; Source address in HL lets the repeat bits go straight to B, - ; bypassing A and avoiding spilling registers to the stack. - ld b, [hl] - inc hl - - ; Shift a 1 into lower bit of shift value. Once this bit - ; reaches the carry, B becomes 0 and the byte is over - scf - rl b - -.pb8BitLoop: - ; If not a repeat, load a literal byte - jr c,.pb8Repeat - ld a, [hli] -.pb8Repeat: - ; Decompressed data uses colors 0 and 1, so write once, inc twice - ld [de], a - inc de - inc de - sla b - jr nz, .pb8BitLoop - - dec c - jr nz, .pb8BlockLoop - ret - -*/ - -/* Compressor and decompressor *************************************/ - -/** - * Compresses an input stream to PB8 data on an output stream. - * @param infp input stream - * @param outfp output stream - * @param blocklength size of an independent input block in bytes - * @return 0 for reaching infp end of file, or EOF for error - */ -int pb8(FILE *infp, FILE *outfp, size_t blocklength) -{ - blocklength >>= 3; // convert bytes to blocks - assert(blocklength > 0); - while (1) { - int last_byte = EOF; // value that never occurs in a file - for (size_t blkleft = blocklength; blkleft > 0; --blkleft) { - unsigned int control_byte = 0x0001; - unsigned char literals[8]; - size_t nliterals = 0; - while (control_byte < 0x100) { - int c = fgetc(infp); - if (c == EOF) break; - - control_byte <<= 1; - if (c == last_byte) { - control_byte |= 0x01; - } - else { - literals[nliterals++] = last_byte = c; - } - } - if (control_byte > 1) { - // Fill partial block with repeats - while (control_byte < 0x100) { - control_byte = (control_byte << 1) | 1; - } - - // Write control byte and check for write failure - int ok = fputc(control_byte & 0xFF, outfp); - if (ok == EOF) return EOF; - size_t ok2 = fwrite(literals, 1, nliterals, outfp); - if (ok2 < nliterals) return EOF; - } - - // If finished, return success or failure - if (ferror(infp) || ferror(outfp)) return EOF; - if (feof(infp)) return 0; - } // End 8-byte block - } // End packet, resetting last_byte -} - -/** - * Decompresses PB8 data on an input stream to an output stream. - * @param infp input stream - * @param outfp output stream - * @return 0 for reaching infp end of file, or EOF for error - */ -int unpb8(FILE *infp, FILE *outfp) -{ - int last_byte = 0; - while (1) { - int control_byte = fgetc(infp); - if (control_byte == EOF) { - return feof(infp) ? 0 : EOF; - } - control_byte &= 0xFF; - for (size_t bytesleft = 8; bytesleft > 0; --bytesleft) { - if (!(control_byte & 0x80)) { - last_byte = fgetc(infp); - if (last_byte == EOF) return EOF; // read error - } - control_byte <<= 1; - int ok = fputc(last_byte, outfp); - if (ok == EOF) return EOF; - } - } -} - -/* CLI frontend ****************************************************/ - -static inline void set_fd_binary(unsigned int fd) -{ -#ifdef _WIN32 - _setmode(fd, _O_BINARY); -#else - (void) fd; -#endif -} - -static const char *usage_msg = -"usage: pb8 [-d] [-l blocklength] [infile [outfile]]\n" -"Compresses a file using RLE with unary run and literal lengths.\n" -"\n" -"options:\n" -" -d decompress\n" -" -l blocklength allow RLE packets to span up to blocklength\n" -" input bytes (multiple of 8; default 8)\n" -" -h, -?, --help show this usage page\n" -" --version show copyright info\n" -"\n" -"If infile is - or missing, it is standard input.\n" -"If outfile is - or missing, it is standard output.\n" -"You cannot compress to or decompress from a terminal.\n" -; -static const char *version_msg = -"PB8 compressor (C version) v0.01\n" -"Copyright 2019 Damian Yerrick \n" -"This software is provided 'as-is', without any express or implied\n" -"warranty.\n" -; -static const char *toomanyfilenames_msg = -"pb8: too many filenames; try pb8 --help\n"; - -int main(int argc, char **argv) -{ - const char *infilename = NULL; - const char *outfilename = NULL; - bool decompress = false; - size_t blocklength = 8; - - for (int i = 1; i < argc; ++i) { - if (argv[i][0] == '-' && argv[i][1] != 0) { - if (!strcmp(argv[i], "--help")) { - fputs(usage_msg, stdout); - return 0; - } - if (!strcmp(argv[i], "--version")) { - fputs(version_msg, stdout); - return 0; - } - - // -t1 or -t 1 - int argtype = argv[i][1]; - switch (argtype) { - case 'h': - case '?': - fputs(usage_msg, stdout); - return 0; - - case 'd': - decompress = true; - break; - - case 'l': { - const char *argvalue = argv[i][2] ? argv[i] + 2 : argv[++i]; - const char *endptr = NULL; - - unsigned long tvalue = strtoul(argvalue, (char **)&endptr, 10); - if (endptr == argvalue || tvalue == 0 || tvalue > SIZE_MAX) { - fprintf(stderr, "pb8: block length %s not a positive integer\n", - argvalue); - return EXIT_FAILURE; - } - if (tvalue % 8 != 0) { - fprintf(stderr, "pb8: block length %s not a multiple of 8\n", - argvalue); - return EXIT_FAILURE; - } - blocklength = tvalue; - } break; - - default: - fprintf(stderr, "pb8: unknown option -%c\n", argtype); - return EXIT_FAILURE; - } - } - else if (!infilename) { - infilename = argv[i]; - } - else if (!outfilename) { - outfilename = argv[i]; - } - else { - fputs(toomanyfilenames_msg, stderr); - return EXIT_FAILURE; - } - } - if (infilename && !strcmp(infilename, "-")) { - infilename = NULL; - } - if (!infilename && decompress && fd_isatty(0)) { - fputs("pb8: cannot decompress from terminal; try redirecting stdin\n", - stderr); - return EXIT_FAILURE; - } - if (outfilename && !strcmp(outfilename, "-")) { - outfilename = NULL; - } - if (!outfilename && !decompress && fd_isatty(1)) { - fputs("pb8: cannot compress to terminal; try redirecting stdout or pb8 --help\n", - stderr); - return EXIT_FAILURE; - } - - FILE *infp = NULL; - if (infilename) { - infp = fopen(infilename, "rb"); - if (!infp) { - fprintf(stderr, "pb8: error opening %s ", infilename); - perror("for reading"); - return EXIT_FAILURE; - } - } - else { - infp = stdin; - set_fd_binary(0); - } - - FILE *outfp = NULL; - if (outfilename) { - outfp = fopen(outfilename, "wb"); - if (!outfp) { - fprintf(stderr, "pb8: error opening %s ", outfilename); - perror("for writing"); - fclose(infp); - return EXIT_FAILURE; - } - } - else { - outfp = stdout; - set_fd_binary(1); - } - - int compfailed = 0; - int has_ferror = 0; - if (decompress) { - compfailed = unpb8(infp, outfp); - } - else { - compfailed = pb8(infp, outfp, blocklength); - } - fflush(outfp); - if (ferror(infp)) { - fprintf(stderr, "pb8: error reading %s\n", - infilename ? infilename : ""); - has_ferror = EOF; - } - fclose(infp); - if (ferror(outfp)) { - fprintf(stderr, "pb8: error writing %s\n", - outfilename ? outfilename : ""); - has_ferror = EOF; - } - fclose(outfp); - - if (compfailed && !has_ferror) { - fputs("pb8: unknown compression failure\n", stderr); - } - - return (compfailed || has_ferror) ? EXIT_FAILURE : EXIT_SUCCESS; -} diff --git a/Makefile b/Makefile index 1f81862..78978e4 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ else EXESUFFIX:= endif -PB8_COMPRESS := build/pb8$(EXESUFFIX) +PB12_COMPRESS := build/pb12$(EXESUFFIX) ifeq ($(PLATFORM),Darwin) DEFAULT := cocoa @@ -386,8 +386,11 @@ $(OBJ)/%.2bpp: %.png -@$(MKDIR) -p $(dir $@) rgbgfx -h -u -o $@ $< -$(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp BootROMs/pb12.py - python3 BootROMs/pb12.py $< $@ +$(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRESS) + $(PB12_COMPRESS) < $< > $@ + +$(PB12_COMPRESS): BootROMs/pb12.c + $(CC) -Wall -Werror $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm From 99ec31dfdca5603b61d3836f62b80e5a3bc591a1 Mon Sep 17 00:00:00 2001 From: Fredrik Ljungdahl Date: Thu, 7 May 2020 00:12:35 +0200 Subject: [PATCH 291/341] Allow more than 1 symbol per debug address --- Core/symbol_hash.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Core/symbol_hash.c b/Core/symbol_hash.c index 33e3399..75a7837 100644 --- a/Core/symbol_hash.c +++ b/Core/symbol_hash.c @@ -28,8 +28,6 @@ GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const c { size_t index = GB_map_find_symbol_index(map, addr); - if (index < map->n_symbols && map->symbols[index].addr == addr) return NULL; - map->symbols = realloc(map->symbols, (map->n_symbols + 1) * sizeof(map->symbols[0])); memmove(&map->symbols[index + 1], &map->symbols[index], (map->n_symbols - index) * sizeof(map->symbols[0])); map->symbols[index].addr = addr; From 8625b23c0d0571b127b6a45ca88577979d11a465 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 7 May 2020 01:32:03 +0300 Subject: [PATCH 292/341] Whoops --- BootROMs/pb12.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BootROMs/pb12.c b/BootROMs/pb12.c index 878dd0d..9ab38fc 100644 --- a/BootROMs/pb12.c +++ b/BootROMs/pb12.c @@ -67,7 +67,7 @@ int main() } } if (!found) { - literals = realloc(literals, literals_size++); + literals = realloc(literals, ++literals_size); literals[literals_size - 1] = byte; } } From e0636718163c77bbde211b66a6de85a006d2fd14 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 7 May 2020 22:46:06 +0300 Subject: [PATCH 293/341] No need to use malloc here, the buffer never gets large --- BootROMs/pb12.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/BootROMs/pb12.c b/BootROMs/pb12.c index 9ab38fc..3f6d5f8 100644 --- a/BootROMs/pb12.c +++ b/BootROMs/pb12.c @@ -23,7 +23,7 @@ int main() size--; } - uint8_t *literals = NULL; + uint8_t literals[8]; size_t literals_size = 0; unsigned bits = 0; unsigned control = 0; @@ -67,8 +67,7 @@ int main() } } if (!found) { - literals = realloc(literals, ++literals_size); - literals[literals_size - 1] = byte; + literals[literals_size++] = byte; } } @@ -87,9 +86,5 @@ int main() uint8_t end_byte = 1; write(STDOUT_FILENO, &end_byte, 1); - if (literals) { - free(literals); - } - return 0; } From 620ee3cf514e8d6219d96a1dd7c9d5da808a8c4f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 7 May 2020 23:43:49 +0300 Subject: [PATCH 294/341] Make the libretro frontend not crash on rumble-less frontends --- libretro/libretro.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libretro/libretro.c b/libretro/libretro.c index 14bfaa8..9e0f9a3 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -142,6 +142,8 @@ static void GB_update_keys_status(GB_gameboy_t *gb, unsigned port) static void rumble_callback(GB_gameboy_t *gb, double amplitude) { + if (!rumble.set_rumble_state) return; + if (gb == &gameboy[0]) { rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535 * amplitude); } From 24220defd649fc8984b0e9e959b7ab70694f480a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Fri, 8 May 2020 13:48:31 +0200 Subject: [PATCH 295/341] Save 16 bytes in the CGB boot ROM --- BootROMs/cgb_boot.asm | 99 ++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index f07fbcf..8770b8a 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -14,12 +14,12 @@ Start: ld h, $D0 call ClearMemoryPage ld [c], a - + ; Clear chosen input palette ldh [InputPalette], a ; Clear title checksum ldh [TitleChecksum], a - + ld a, $80 ldh [$26], a ldh [$11], a @@ -36,7 +36,7 @@ Start: cpl dec c jr nz, .waveformLoop - + ; Clear OAM ld h, $fe @@ -45,7 +45,7 @@ Start: ldi [hl], a dec c jr nz, .clearOAMLoop - + ; Init BG palette ld a, $fc ldh [$47], a @@ -148,13 +148,13 @@ ENDC ; One white ld [hli], a ld [hli], a - + ; Mixed with white ld a, [de] inc e or $20 ld b, a - + ld a, [de] dec e or $84 @@ -163,26 +163,25 @@ ENDC ld [hl], b inc l ld [hli], a - + ; One black xor a ld [hli], a ld [hli], a - + ; One color ld a, [de] inc e ld [hli], a ld a, [de] inc e - ld [hli], a - + ld [hli], a + xor a dec c jr nz, .expandPalettesLoop - ld hl, BgPalettes - call LoadBGPalettes64 + call LoadPalettesFromHRAM ; Turn on LCD ld a, $91 @@ -205,7 +204,7 @@ IF !DEF(FAST) ; Play second sound ld a, $c1 call PlaySound - + .waitLoop call GetInputPaletteIndex call WaitFrame @@ -530,7 +529,7 @@ Palettes: dw $7FFF, $7FEA, $7D5F, $0000 ; CGA 1 dw $4778, $3290, $1D87, $0861 ; DMG LCD -KeyCombinationPalettes +KeyCombinationPalettes: db 1 * 3 ; Right db 48 * 3 ; Left db 5 * 3 ; Up @@ -548,7 +547,7 @@ KeyCombinationPalettes db 52 * 3 ; Left + A + B db 53 * 3 ; Up + A + B db 54 * 3 ; Down + A + B - + TrademarkSymbol: db $3c,$42,$b9,$a5,$b9,$a5,$42,$3c @@ -795,10 +794,9 @@ IF !DEF(FAST) .fadeLoop ld c, 32 ; 32 colors to fade ld hl, BgPalettes - push hl .frameLoop push bc - + ; Brighten Color ld a, [hli] ld e, a @@ -843,13 +841,12 @@ IF !DEF(FAST) adc b ld [hli], a pop bc - + dec c jr nz, .frameLoop call WaitFrame - pop hl - call LoadBGPalettes64 + call LoadPalettesFromHRAM call WaitFrame dec b jr nz, .fadeLoop @@ -860,21 +857,21 @@ ENDC call ClearVRAMViaHDMA ; A = $40, so it's bank 0 ld a, $ff ldh [$00], a - + ; Final values for CGB mode ld d, a ld e, c ld l, $0d - + ld a, [$143] bit 7, a call z, EmulateDMG bit 7, a - + ldh [$4C], a ldh a, [TitleChecksum] ld b, a - + jr z, .skipDMGForCGBCheck ldh a, [InputPalette] and a @@ -904,10 +901,10 @@ ENDC ldh [$4C], a ld a, $1 ret - + GetKeyComboPalette: ld hl, KeyCombinationPalettes - 1 ; Return value is 1-based, 0 means nothing down - ld c ,a + ld c, a ld b, 0 add hl, bc ld a, [hl] @@ -1005,7 +1002,8 @@ GetPaletteIndex: .notNintendo xor a ret - + +; optimizations in callers rely on this returning with b = 0 GetPaletteCombo: ld hl, PaletteCombinations ld b, 0 @@ -1022,7 +1020,7 @@ LoadPalettesFromIndex: ; a = index of combination ld a, [hli] push hl ld hl, Palettes - ld b, 0 + ; b is already 0 ld c, a add hl, bc ld d, 8 @@ -1035,15 +1033,15 @@ LoadPalettesFromIndex: ; a = index of combination jr .loadObjPalette .loadBGPalette ;BG Palette - ld a, [hli] + ld c, [hl] + ; b is already 0 ld hl, Palettes - ld b, 0 - ld c, a add hl, bc ld d, 8 jr LoadBGPalettes -LoadBGPalettes64: +LoadPalettesFromHRAM: + ld hl, BgPalettes ld d, 64 LoadBGPalettes: @@ -1076,6 +1074,7 @@ _ClearVRAMViaHDMA: jr nz, .loop ret +; clobbers AF and HL GetInputPaletteIndex: ld a, $20 ; Select directions ldh [$00], a @@ -1083,11 +1082,10 @@ GetInputPaletteIndex: cpl and $F ret z ; No direction keys pressed, no palette - push bc - ld c, 0 + ld l, 0 .directionLoop - inc c + inc l rra jr nc, .directionLoop @@ -1100,15 +1098,13 @@ GetInputPaletteIndex: rla rla and $C - add c - ld b, a + add l + ld l, a ldh a, [InputPalette] - ld c, a - ld a, b - ldh [InputPalette], a - cp c - pop bc + cp l ret z ; No change, don't load + ld a, l + ldh [InputPalette], a ; Slide into change Animation Palette ChangeAnimationPalette: @@ -1118,10 +1114,8 @@ ChangeAnimationPalette: call GetPaletteCombo inc l inc l - ld a, [hl] + ld c, [hl] ld hl, Palettes + 1 - ld b, 0 - ld c, a add hl, bc ld a, [hld] cp $7F ; Is white color? @@ -1131,7 +1125,7 @@ ChangeAnimationPalette: .isWhite push af ld a, [hli] - + push hl ld hl, BgPalettes ; First color, all palettes call ReplaceColorInAllPalettes @@ -1148,7 +1142,7 @@ ChangeAnimationPalette: call ReplaceColorInAllPalettes pop hl ldh [BgPalettes + 7], a ; Fourth color, first palette - + pop af jr z, .isNotWhite inc hl @@ -1173,12 +1167,12 @@ ChangeAnimationPalette: rra ld [BgPalettes + 7 * 8 + 2], a dec l - + ld a, [hli] ldh [BgPalettes + 7 * 8 + 6], a ; Fourth color, 7th palette ld a, [hli] ldh [BgPalettes + 7 * 8 + 7], a ; Fourth color, 7th palette - + ld a, [hli] ldh [BgPalettes + 4], a ; Third color, first palette ld a, [hli] @@ -1186,8 +1180,7 @@ ChangeAnimationPalette: call WaitFrame - ld hl, BgPalettes - call LoadBGPalettes64 + call LoadPalettesFromHRAM ; Delay the wait loop while the user is selecting a palette ld a, 45 ldh [WaitLoopCounter], a @@ -1223,11 +1216,11 @@ LoadDMGTilemap: .tilemapDone pop af ret - + HDMAData: db $88, $00, $98, $A0, $12 db $88, $00, $80, $00, $40 - + BootEnd: IF BootEnd > $900 FAIL "BootROM overflowed: {BootEnd}" From 5f2c7b966feb03b4a8d87b140c39f48a3ac7c953 Mon Sep 17 00:00:00 2001 From: Rupert Carmichael <5050061-carmiker@users.noreply.gitlab.com> Date: Sat, 9 May 2020 11:49:20 -0400 Subject: [PATCH 296/341] Pre-buffer audio samples before passing to SDL's queue --- SDL/audio/sdl.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c index 1f8a529..d8e7e37 100644 --- a/SDL/audio/sdl.c +++ b/SDL/audio/sdl.c @@ -25,6 +25,10 @@ static SDL_AudioDeviceID device_id; static SDL_AudioSpec want_aspec, have_aspec; +#define BUFFERSIZE 1024 +static int bufferpos = 0; +static int16_t audiobuffer[BUFFERSIZE]; + bool GB_audio_is_playing(void) { return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; @@ -53,7 +57,14 @@ size_t GB_audio_get_queue_length(void) void GB_audio_queue_sample(GB_sample_t *sample) { - SDL_QueueAudio(device_id, sample, sizeof(*sample)); + audiobuffer[bufferpos++] = sample->left; + audiobuffer[bufferpos++] = sample->right; + + if (bufferpos == BUFFERSIZE) + { + bufferpos = 0; + SDL_QueueAudio(device_id, (const void*)audiobuffer, BUFFERSIZE * sizeof(int16_t)); + } } void GB_audio_init(void) @@ -63,7 +74,7 @@ void GB_audio_init(void) want_aspec.freq = AUDIO_FREQUENCY; want_aspec.format = AUDIO_S16SYS; want_aspec.channels = 2; - want_aspec.samples = 512; + want_aspec.samples = 1024; SDL_version _sdl_version; SDL_GetVersion(&_sdl_version); From 3cba3e8e27e568e7daefbb7f4ff2eb73c4f18ffe Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 10 May 2020 00:37:52 +0300 Subject: [PATCH 297/341] Emulate CGB-C PCM read glitch, fix a potential noise volume envelope bug --- Core/apu.c | 23 +++++++++++++++++++++-- Core/apu.h | 1 + Core/memory.c | 8 ++++---- Core/timing.c | 3 ++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index afb970c..7e7ab31 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -283,6 +283,15 @@ static void tick_square_envelope(GB_gameboy_t *gb, enum GB_CHANNELS index) if (gb->apu.square_channels[index].volume_countdown || (nrx2 & 7)) { if (!gb->apu.square_channels[index].volume_countdown || !--gb->apu.square_channels[index].volume_countdown) { + if (gb->cgb_double_speed) { + if (index == GB_SQUARE_1) { + gb->apu.pcm_mask[0] &= gb->apu.square_channels[GB_SQUARE_1].current_volume | 0xF1; + } + else { + gb->apu.pcm_mask[0] &= (gb->apu.square_channels[GB_SQUARE_2].current_volume << 2) | 0x1F; + } + } + if ((nrx2 & 8) && gb->apu.square_channels[index].current_volume < 0xF) { gb->apu.square_channels[index].current_volume++; } @@ -305,7 +314,10 @@ static void tick_noise_envelope(GB_gameboy_t *gb) uint8_t nr42 = gb->io_registers[GB_IO_NR42]; if (gb->apu.noise_channel.volume_countdown || (nr42 & 7)) { - if (!--gb->apu.noise_channel.volume_countdown) { + if (!gb->apu.noise_channel.volume_countdown || !--gb->apu.noise_channel.volume_countdown) { + if (gb->cgb_double_speed) { + gb->apu.pcm_mask[0] &= (gb->apu.noise_channel.current_volume << 2) | 0x1F; + } if ((nr42 & 8) && gb->apu.noise_channel.current_volume < 0xF) { gb->apu.noise_channel.current_volume++; } @@ -423,7 +435,7 @@ void GB_apu_run(GB_gameboy_t *gb) uint8_t cycles = gb->apu.apu_cycles >> 2; gb->apu.apu_cycles = 0; if (!cycles) return; - + if (likely(!gb->stopped || GB_is_cgb(gb))) { /* To align the square signal to 1MHz */ gb->apu.lf_div ^= cycles & 1; @@ -455,6 +467,9 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.square_channels[i].sample_countdown = (gb->apu.square_channels[i].sample_length ^ 0x7FF) * 2 + 1; gb->apu.square_channels[i].current_sample_index++; gb->apu.square_channels[i].current_sample_index &= 0x7; + if (cycles_left == 0 && gb->apu.samples[i] == 0) { + gb->apu.pcm_mask[0] &= i == GB_SQUARE_1? 0xF0 : 0x0F; + } update_square_sample(gb, i); } @@ -506,6 +521,10 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.current_lfsr_sample = gb->apu.noise_channel.lfsr & 1; + if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0) { + gb->apu.pcm_mask[1] &= 0x0F; + } + update_sample(gb, GB_NOISE, gb->apu.current_lfsr_sample ? gb->apu.noise_channel.current_volume : 0, diff --git a/Core/apu.h b/Core/apu.h index 398b903..a3a36a6 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -119,6 +119,7 @@ typedef struct #define GB_SKIP_DIV_EVENT_SKIP 2 uint8_t skip_div_event; bool current_lfsr_sample; + uint8_t pcm_mask[2]; // For CGB-0 to CGB-C PCM read glitch } GB_apu_t; typedef enum { diff --git a/Core/memory.c b/Core/memory.c index 49296dd..9f52af5 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -303,12 +303,12 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_IO_PCM_12: if (!GB_is_cgb(gb)) return 0xFF; - return (gb->apu.is_active[GB_SQUARE_2] ? (gb->apu.samples[GB_SQUARE_2] << 4) : 0) | - (gb->apu.is_active[GB_SQUARE_1] ? (gb->apu.samples[GB_SQUARE_1]) : 0); + return ((gb->apu.is_active[GB_SQUARE_2] ? (gb->apu.samples[GB_SQUARE_2] << 4) : 0) | + (gb->apu.is_active[GB_SQUARE_1] ? (gb->apu.samples[GB_SQUARE_1]) : 0)) & (gb->model <= GB_MODEL_CGB_C? gb->apu.pcm_mask[0] : 0xFF); case GB_IO_PCM_34: if (!GB_is_cgb(gb)) return 0xFF; - return (gb->apu.is_active[GB_NOISE] ? (gb->apu.samples[GB_NOISE] << 4) : 0) | - (gb->apu.is_active[GB_WAVE] ? (gb->apu.samples[GB_WAVE]) : 0); + return ((gb->apu.is_active[GB_NOISE] ? (gb->apu.samples[GB_NOISE] << 4) : 0) | + (gb->apu.is_active[GB_WAVE] ? (gb->apu.samples[GB_WAVE]) : 0)) & (gb->model <= GB_MODEL_CGB_C? gb->apu.pcm_mask[1] : 0xFF); case GB_IO_JOYP: GB_timing_sync(gb); case GB_IO_TMA: diff --git a/Core/timing.c b/Core/timing.c index f79727c..f734caf 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -214,7 +214,8 @@ static void advance_serial(GB_gameboy_t *gb, uint8_t cycles) } void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) -{ +{ + gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF; // Sort of hacky, but too many cross-component interactions to do it right // Affected by speed boost gb->dma_cycles += cycles; From 0200596391983291320a7aab1a6e849a5984d0e8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 10 May 2020 22:05:47 +0300 Subject: [PATCH 298/341] Fix #256 --- libretro/libretro.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 9e0f9a3..a7d6432 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -346,14 +346,14 @@ static void set_link_cable_state(bool state) static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) { const char *model_name = (char *[]){ - [GB_BOOT_ROM_DMG0] = "dmg0_boot", - [GB_BOOT_ROM_DMG] = "dmg_boot", - [GB_BOOT_ROM_MGB] = "mgb_boot", - [GB_BOOT_ROM_SGB] = "sgb_boot", - [GB_BOOT_ROM_SGB2] = "sgb2_boot", - [GB_BOOT_ROM_CGB0] = "cgb0_boot", - [GB_BOOT_ROM_CGB] = "cgb_boot", - [GB_BOOT_ROM_AGB] = "agb_boot", + [GB_BOOT_ROM_DMG0] = "dmg0", + [GB_BOOT_ROM_DMG] = "dmg", + [GB_BOOT_ROM_MGB] = "mgb", + [GB_BOOT_ROM_SGB] = "sgb", + [GB_BOOT_ROM_SGB2] = "sgb2", + [GB_BOOT_ROM_CGB0] = "cgb0", + [GB_BOOT_ROM_CGB] = "cgb", + [GB_BOOT_ROM_AGB] = "agb", }[type]; const uint8_t *boot_code = (const unsigned char *[]) From 1b7c3c4c7c29cff93f89b5a9836e5620a9ea6459 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 10 May 2020 22:16:49 +0300 Subject: [PATCH 299/341] Minor fixes, style update --- SDL/audio/sdl.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c index d8e7e37..12ee69a 100644 --- a/SDL/audio/sdl.c +++ b/SDL/audio/sdl.c @@ -25,9 +25,9 @@ static SDL_AudioDeviceID device_id; static SDL_AudioSpec want_aspec, have_aspec; -#define BUFFERSIZE 1024 -static int bufferpos = 0; -static int16_t audiobuffer[BUFFERSIZE]; +#define AUDIO_BUFFER_SIZE 512 +static unsigned buffer_pos = 0; +static GB_sample_t audio_buffer[AUDIO_BUFFER_SIZE]; bool GB_audio_is_playing(void) { @@ -57,13 +57,11 @@ size_t GB_audio_get_queue_length(void) void GB_audio_queue_sample(GB_sample_t *sample) { - audiobuffer[bufferpos++] = sample->left; - audiobuffer[bufferpos++] = sample->right; + audio_buffer[buffer_pos++] = *sample; - if (bufferpos == BUFFERSIZE) - { - bufferpos = 0; - SDL_QueueAudio(device_id, (const void*)audiobuffer, BUFFERSIZE * sizeof(int16_t)); + if (buffer_pos == AUDIO_BUFFER_SIZE) { + buffer_pos = 0; + SDL_QueueAudio(device_id, (const void *)audio_buffer, sizeof(audio_buffer)); } } @@ -74,7 +72,7 @@ void GB_audio_init(void) want_aspec.freq = AUDIO_FREQUENCY; want_aspec.format = AUDIO_S16SYS; want_aspec.channels = 2; - want_aspec.samples = 1024; + want_aspec.samples = 512; SDL_version _sdl_version; SDL_GetVersion(&_sdl_version); From 2cc980755e4dc5acdf18b844f8ac0ab6be694fe6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 13 May 2020 22:21:31 +0300 Subject: [PATCH 300/341] HuC1 IR support --- Core/debugger.c | 4 +++- Core/gb.h | 1 + Core/mbc.h | 2 +- Core/memory.c | 20 ++++++++++++++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index a46de86..20884c6 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1439,7 +1439,9 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg GB_log(gb, "Current mapped ROM bank: %x\n", gb->mbc_rom_bank); if (cartridge->has_ram) { GB_log(gb, "Current mapped RAM bank: %x\n", gb->mbc_ram_bank); - GB_log(gb, "RAM is curently %s\n", gb->mbc_ram_enable? "enabled" : "disabled"); + if (gb->cartridge_type->mbc_type != GB_HUC1) { + GB_log(gb, "RAM is curently %s\n", gb->mbc_ram_enable? "enabled" : "disabled"); + } } if (cartridge->mbc_type == GB_MBC1 && gb->mbc1_wiring == GB_STANDARD_MBC1_WIRING) { GB_log(gb, "MBC1 banking mode is %s\n", gb->mbc1.mode == 1 ? "RAM" : "ROM"); diff --git a/Core/gb.h b/Core/gb.h index 97a8069..29d394f 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -430,6 +430,7 @@ struct GB_gameboy_internal_s { bool camera_registers_mapped; uint8_t camera_registers[0x36]; bool rumble_state; + bool cart_ir; ); diff --git a/Core/mbc.h b/Core/mbc.h index 7e9b47f..6a23300 100644 --- a/Core/mbc.h +++ b/Core/mbc.h @@ -10,7 +10,7 @@ typedef struct { GB_MBC2, GB_MBC3, GB_MBC5, - GB_HUC1, /* Todo: HUC1 features are not emulated. Should be unified with the CGB IR sensor API. */ + GB_HUC1, GB_HUC3, } mbc_type; enum { diff --git a/Core/memory.c b/Core/memory.c index 9f52af5..6a65a98 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -150,6 +150,10 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) gb->cartridge_type->mbc_subtype != GB_CAMERA && gb->cartridge_type->mbc_type != GB_HUC1) return 0xFF; + if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { + return 0xc0 | gb->cart_ir | gb->infrared_input | (gb->io_registers[GB_IO_RP] & 1); + } + if (gb->cartridge_type->has_rtc && gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { /* RTC read */ gb->rtc_latched.high |= ~0xC1; /* Not all bytes in RTC high are used. */ @@ -379,7 +383,7 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_IO_RP: { if (!gb->cgb_mode) return 0xFF; /* You will read your own IR LED if it's on. */ - bool read_value = gb->infrared_input || (gb->io_registers[GB_IO_RP] & 1); + bool read_value = gb->infrared_input || (gb->io_registers[GB_IO_RP] & 1) || gb->cart_ir; uint8_t ret = (gb->io_registers[GB_IO_RP] & 0xC1) | 0x3C; if ((gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 && read_value) { ret |= 2; @@ -493,10 +497,9 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) break; case GB_HUC1: switch (addr & 0xF000) { - case 0x0000: case 0x1000: gb->mbc_ram_enable = (value & 0xF) == 0xA; break; + case 0x0000: case 0x1000: gb->huc1.mode = (value & 0xF) == 0xE; break; case 0x2000: case 0x3000: gb->huc1.bank_low = value; break; case 0x4000: case 0x5000: gb->huc1.bank_high = value; break; - case 0x6000: case 0x7000: gb->huc1.mode = value; break; } break; case GB_HUC3: @@ -526,7 +529,16 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; } - if (!gb->mbc_ram_enable || !gb->mbc_ram_size) return; + if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && gb->cartridge_type->mbc_type != GB_HUC1) return; + + if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { + if (gb->cart_ir != (value & 1) && gb->infrared_callback) { + gb->infrared_callback(gb, value & 1, gb->cycles_since_ir_change); + gb->cycles_since_ir_change = 0; + } + gb->cart_ir = value & 1; + return; + } if (gb->cartridge_type->has_rtc && gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { gb->rtc_latched.data[gb->mbc_ram_bank - 8] = gb->rtc_real.data[gb->mbc_ram_bank - 8] = value; From a9023d08c69f0c1fbbd685d30a2e002a61f941ab Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 16 May 2020 23:27:17 +0300 Subject: [PATCH 301/341] =?UTF-8?q?Emulate=20HuC-3=E2=80=99s=20IR=20and=20?= =?UTF-8?q?RTC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/debugger.c | 4 +- Core/gb.c | 98 ++++++++++++++++++++++++++++++++++++++- Core/gb.h | 12 ++++- Core/mbc.c | 4 +- Core/memory.c | 121 ++++++++++++++++++++++++++++++++++++++++++------ Core/sm83_cpu.c | 5 +- Core/timing.c | 12 +++++ Core/timing.h | 5 +- 8 files changed, 230 insertions(+), 31 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index 20884c6..0f08fb1 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1431,8 +1431,8 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg [GB_MBC2] = "MBC2", [GB_MBC3] = "MBC3", [GB_MBC5] = "MBC5", - [GB_HUC1] = "HUC1", - [GB_HUC3] = "HUC3", + [GB_HUC1] = "HUC-1", + [GB_HUC3] = "HUC-3", }; GB_log(gb, "%s\n", mapper_names[cartridge->mbc_type]); } diff --git a/Core/gb.c b/Core/gb.c index a1f573c..d0632dd 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -560,6 +560,12 @@ typedef struct { uint8_t padding5[3]; } GB_vba_rtc_time_t; +typedef struct __attribute__((packed)) { + uint64_t last_rtc_second; + uint16_t minutes; + uint16_t days; +} GB_huc3_rtc_time_t; + typedef union { struct __attribute__((packed)) { GB_rtc_time_t rtc_real; @@ -582,6 +588,9 @@ int GB_save_battery_size(GB_gameboy_t *gb) if (!gb->cartridge_type->has_battery) return 0; // Nothing to save. if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */ + if (gb->cartridge_type->mbc_type == GB_HUC3) { + return gb->mbc_ram_size + sizeof(GB_huc3_rtc_time_t); + } GB_rtc_save_t rtc_save_size; return gb->mbc_ram_size + (gb->cartridge_type->has_rtc ? sizeof(rtc_save_size.vba64) : 0); } @@ -595,7 +604,25 @@ int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size) memcpy(buffer, gb->mbc_ram, gb->mbc_ram_size); - if (gb->cartridge_type->has_rtc) { + if (gb->cartridge_type->mbc_type == GB_HUC3) { + buffer += gb->mbc_ram_size; + +#ifdef GB_BIG_ENDIAN + GB_huc3_rtc_time_t rtc_save = { + __builtin_bswap64(gb->last_rtc_second), + __builtin_bswap16(gb->huc3_minutes), + __builtin_bswap16(gb->huc3_days), + }; +#else + GB_huc3_rtc_time_t rtc_save = { + gb->last_rtc_second, + gb->huc3_minutes, + gb->huc3_days, + }; +#endif + memcpy(buffer, &rtc_save, sizeof(rtc_save)); + } + else if (gb->cartridge_type->has_rtc) { GB_rtc_save_t rtc_save = {{{{0,}},},}; rtc_save.vba64.rtc_real.seconds = gb->rtc_real.seconds; rtc_save.vba64.rtc_real.minutes = gb->rtc_real.minutes; @@ -633,7 +660,27 @@ int GB_save_battery(GB_gameboy_t *gb, const char *path) fclose(f); return EIO; } - if (gb->cartridge_type->has_rtc) { + if (gb->cartridge_type->mbc_type == GB_HUC3) { +#ifdef GB_BIG_ENDIAN + GB_huc3_rtc_time_t rtc_save = { + __builtin_bswap64(gb->last_rtc_second), + __builtin_bswap16(gb->huc3_minutes), + __builtin_bswap16(gb->huc3_days), + }; +#else + GB_huc3_rtc_time_t rtc_save = { + gb->last_rtc_second, + gb->huc3_minutes, + gb->huc3_days, + }; +#endif + + if (fwrite(&rtc_save, sizeof(rtc_save), 1, f) != 1) { + fclose(f); + return EIO; + } + } + else if (gb->cartridge_type->has_rtc) { GB_rtc_save_t rtc_save = {{{{0,}},},}; rtc_save.vba64.rtc_real.seconds = gb->rtc_real.seconds; rtc_save.vba64.rtc_real.minutes = gb->rtc_real.minutes; @@ -668,6 +715,28 @@ void GB_load_battery_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t if (size <= gb->mbc_ram_size) { goto reset_rtc; } + + if (gb->cartridge_type->mbc_type == GB_HUC3) { + GB_huc3_rtc_time_t rtc_save; + if (size - gb->mbc_ram_size < sizeof(rtc_save)) { + goto reset_rtc; + } + memcpy(&rtc_save, buffer + gb->mbc_ram_size, sizeof(rtc_save)); +#ifdef GB_BIG_ENDIAN + gb->last_rtc_second = __builtin_bswap64(rtc_save.last_rtc_second); + gb->huc3_minutes = __builtin_bswap16(rtc_save.minutes); + gb->huc3_days = __builtin_bswap16(rtc_save.days); +#else + gb->last_rtc_second = rtc_save.last_rtc_second; + gb->huc3_minutes = rtc_save.minutes; + gb->huc3_days = rtc_save.days; +#endif + if (gb->last_rtc_second > time(NULL)) { + /* We must reset RTC here, or it will not advance. */ + goto reset_rtc; + } + return; + } GB_rtc_save_t rtc_save; memcpy(&rtc_save, buffer + gb->mbc_ram_size, MIN(sizeof(rtc_save), size)); @@ -731,6 +800,8 @@ void GB_load_battery_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t reset_rtc: gb->last_rtc_second = time(NULL); gb->rtc_real.high |= 0x80; /* This gives the game a hint that the clock should be reset. */ + gb->huc3_days = 0xFFFF; + gb->huc3_minutes = 0xFFF; exit: return; } @@ -746,6 +817,27 @@ void GB_load_battery(GB_gameboy_t *gb, const char *path) if (fread(gb->mbc_ram, 1, gb->mbc_ram_size, f) != gb->mbc_ram_size) { goto reset_rtc; } + + if (gb->cartridge_type->mbc_type == GB_HUC3) { + GB_huc3_rtc_time_t rtc_save; + if (fread(&rtc_save, sizeof(rtc_save), 1, f) != 1) { + goto reset_rtc; + } +#ifdef GB_BIG_ENDIAN + gb->last_rtc_second = __builtin_bswap64(rtc_save.last_rtc_second); + gb->huc3_minutes = __builtin_bswap16(rtc_save.minutes); + gb->huc3_days = __builtin_bswap16(rtc_save.days); +#else + gb->last_rtc_second = rtc_save.last_rtc_second; + gb->huc3_minutes = rtc_save.minutes; + gb->huc3_days = rtc_save.days; +#endif + if (gb->last_rtc_second > time(NULL)) { + /* We must reset RTC here, or it will not advance. */ + goto reset_rtc; + } + return; + } GB_rtc_save_t rtc_save; switch (fread(&rtc_save, 1, sizeof(rtc_save), f)) { @@ -808,6 +900,8 @@ void GB_load_battery(GB_gameboy_t *gb, const char *path) reset_rtc: gb->last_rtc_second = time(NULL); gb->rtc_real.high |= 0x80; /* This gives the game a hint that the clock should be reset. */ + gb->huc3_days = 0xFFFF; + gb->huc3_minutes = 0xFFF; exit: fclose(f); return; diff --git a/Core/gb.h b/Core/gb.h index 29d394f..f5f7df5 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -422,8 +422,9 @@ struct GB_gameboy_internal_s { } huc1; struct { - uint8_t rom_bank; - uint8_t ram_bank; + uint8_t rom_bank:7; + uint8_t padding:1; + uint8_t ram_bank:4; } huc3; }; uint16_t mbc_rom0_bank; /* For some MBC1 wirings. */ @@ -431,6 +432,13 @@ struct GB_gameboy_internal_s { uint8_t camera_registers[0x36]; bool rumble_state; bool cart_ir; + + // TODO: move to huc3 struct when breaking save compat + uint8_t huc3_mode; + uint8_t huc3_access_index; + uint16_t huc3_minutes, huc3_days; + uint8_t huc3_read; + uint8_t huc3_access_flags; ); diff --git a/Core/mbc.c b/Core/mbc.c index 2ee53e8..72073f6 100644 --- a/Core/mbc.c +++ b/Core/mbc.c @@ -37,8 +37,8 @@ const GB_cartridge_t GB_cart_defs[256] = { [0xFC] = { GB_MBC5 , GB_CAMERA , true , true , false, false}, // FCh POCKET CAMERA { GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // FDh BANDAI TAMA5 (Todo: Not supported) - { GB_HUC3 , GB_STANDARD_MBC, true , true , false, false}, // FEh HuC3 (Todo: Mapper support only) - { GB_HUC1 , GB_STANDARD_MBC, true , true , false, false}, // FFh HuC1+RAM+BATTERY (Todo: No IR bindings) + { GB_HUC3 , GB_STANDARD_MBC, true , true , true, false}, // FEh HuC3 + { GB_HUC1 , GB_STANDARD_MBC, true , true , false, false}, // FFh HuC1+RAM+BATTERY }; void GB_update_mbc_mappings(GB_gameboy_t *gb) diff --git a/Core/memory.c b/Core/memory.c index 6a65a98..fbf1318 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -113,6 +113,11 @@ static bool is_addr_in_dma_use(GB_gameboy_t *gb, uint16_t addr) return bus_for_addr(gb, addr) == bus_for_addr(gb, gb->dma_current_src); } +static bool effective_ir_input(GB_gameboy_t *gb) +{ + return gb->infrared_input || (gb->io_registers[GB_IO_RP] & 1) || gb->cart_ir; +} + static uint8_t read_rom(GB_gameboy_t *gb, uint16_t addr) { if (addr < 0x100 && !gb->boot_rom_finished) { @@ -146,12 +151,33 @@ static uint8_t read_vram(GB_gameboy_t *gb, uint16_t addr) static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) { + if (gb->cartridge_type->mbc_type == GB_HUC3) { + switch (gb->huc3_mode) { + case 0xC: // RTC read + if (gb->huc3_access_flags == 0x2) { + return 1; + } + return gb->huc3_read; + case 0xD: // RTC status + return 1; + case 0xE: // IR mode + return effective_ir_input(gb); // TODO: What are the other bits? + default: + GB_log(gb, "Unsupported HuC-3 mode %x read: %04x\n", gb->huc3_mode, addr); + return 1; // TODO: What happens in this case? + case 0: // TODO: R/O RAM? (or is it disabled?) + case 0xA: // RAM + break; + } + } + if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && gb->cartridge_type->mbc_subtype != GB_CAMERA && - gb->cartridge_type->mbc_type != GB_HUC1) return 0xFF; + gb->cartridge_type->mbc_type != GB_HUC1 && + gb->cartridge_type->mbc_type != GB_HUC3) return 0xFF; if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { - return 0xc0 | gb->cart_ir | gb->infrared_input | (gb->io_registers[GB_IO_RP] & 1); + return 0xc0 | effective_ir_input(gb); } if (gb->cartridge_type->has_rtc && gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { @@ -383,9 +409,8 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) case GB_IO_RP: { if (!gb->cgb_mode) return 0xFF; /* You will read your own IR LED if it's on. */ - bool read_value = gb->infrared_input || (gb->io_registers[GB_IO_RP] & 1) || gb->cart_ir; uint8_t ret = (gb->io_registers[GB_IO_RP] & 0xC1) | 0x3C; - if ((gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 && read_value) { + if ((gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 && effective_ir_input(gb)) { ret |= 2; } return ret; @@ -504,7 +529,10 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) break; case GB_HUC3: switch (addr & 0xF000) { - case 0x0000: case 0x1000: gb->mbc_ram_enable = (value & 0xF) == 0xA; break; + case 0x0000: case 0x1000: + gb->huc3_mode = value & 0xF; + gb->mbc_ram_enable = gb->huc3_mode == 0xA; + break; case 0x2000: case 0x3000: gb->huc3.rom_bank = value; break; case 0x4000: case 0x5000: gb->huc3.ram_bank = value; break; } @@ -524,19 +552,82 @@ static void write_vram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) { + if (gb->cartridge_type->mbc_type == GB_HUC3) { + switch (gb->huc3_mode) { + case 0xB: // RTC Write + switch (value >> 4) { + case 1: + if (gb->huc3_access_index < 3) { + gb->huc3_read = (gb->huc3_minutes >> (gb->huc3_access_index * 4)) & 0xF; + } + else if (gb->huc3_access_index < 7) { + gb->huc3_read = (gb->huc3_days >> ((gb->huc3_access_index - 3) * 4)) & 0xF; + } + else { + GB_log(gb, "Attempting to read from unsupported HuC-3 register: %03x\n", gb->huc3_access_index); + } + gb->huc3_access_index++; + return; + case 3: + if (gb->huc3_access_index < 3) { + gb->huc3_minutes &= ~(0xF << (gb->huc3_access_index * 4)); + gb->huc3_minutes |= ((value & 0xF) << (gb->huc3_access_index * 4)); + } + else if (gb->huc3_access_index < 7) { + gb->huc3_days &= ~(0xF << ((gb->huc3_access_index - 3) * 4)); + gb->huc3_days |= ((value & 0xF) << ((gb->huc3_access_index - 3) * 4)); + } + gb->huc3_access_index++; + return; + case 4: + gb->huc3_access_index &= 0xF0; + gb->huc3_access_index |= value & 0xF; + return; + case 5: + gb->huc3_access_index &= 0x0F; + gb->huc3_access_index |= (value & 0xF) << 4; + return; + case 6: + gb->huc3_access_flags = (value & 0xF); + return; + + default: + GB_log(gb, "HuC-3 RTC Write %02x\n", value); + break; + } + + return; + case 0xD: // RTC status + // Not sure what writes here mean, they're always 0xFE + return; + case 0xE: // IR mode + gb->cart_ir = value & 1; + return; + default: + GB_log(gb, "Unsupported HuC-3 mode %x write: [%04x] = %02x\n", gb->huc3_mode, addr, value); + return; + case 0: // Disabled + case 0xA: // RAM + break; + } + } + if (gb->camera_registers_mapped) { GB_camera_write_register(gb, addr, value); return; } - if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && gb->cartridge_type->mbc_type != GB_HUC1) return; + if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) + && gb->cartridge_type->mbc_type != GB_HUC1) return; if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { - if (gb->cart_ir != (value & 1) && gb->infrared_callback) { - gb->infrared_callback(gb, value & 1, gb->cycles_since_ir_change); + bool old_input = effective_ir_input(gb); + gb->cart_ir = value & 1; + bool new_input = effective_ir_input(gb); + if (new_input != old_input) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); gb->cycles_since_ir_change = 0; } - gb->cart_ir = value & 1; return; } @@ -943,13 +1034,13 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (!GB_is_cgb(gb)) { return; } - if ((value & 1) != (gb->io_registers[GB_IO_RP] & 1)) { - if (gb->infrared_callback) { - gb->infrared_callback(gb, value & 1, gb->cycles_since_ir_change); - gb->cycles_since_ir_change = 0; - } - } + bool old_input = effective_ir_input(gb); gb->io_registers[GB_IO_RP] = value; + bool new_input = effective_ir_input(gb); + if (new_input != old_input) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + gb->cycles_since_ir_change = 0; + } return; } diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 13f05df..d0b8ec4 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -917,10 +917,7 @@ static void halt(GB_gameboy_t *gb, uint8_t opcode) { assert(gb->pending_cycles == 4); gb->pending_cycles = 0; - GB_advance_cycles(gb, 1); - GB_advance_cycles(gb, 1); - GB_advance_cycles(gb, 1); - GB_advance_cycles(gb, 1); + GB_advance_cycles(gb, 4); gb->halted = true; /* Despite what some online documentations say, the HALT bug also happens on a CGB, in both CGB and DMG modes. */ diff --git a/Core/timing.c b/Core/timing.c index f734caf..17983bc 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -279,6 +279,18 @@ void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac) void GB_rtc_run(GB_gameboy_t *gb) { + if (gb->cartridge_type->mbc_type == GB_HUC3) { + time_t current_time = time(NULL); + while (gb->last_rtc_second / 60 < current_time / 60) { + gb->last_rtc_second += 60; + gb->huc3_minutes++; + if (gb->huc3_minutes == 60 * 24) { + gb->huc3_days++; + gb->huc3_minutes = 0; + } + } + return; + } if ((gb->rtc_real.high & 0x40) == 0) { /* is timer running? */ time_t current_time = time(NULL); while (gb->last_rtc_second < current_time) { diff --git a/Core/timing.h b/Core/timing.h index 02ca54c..d4fa07f 100644 --- a/Core/timing.h +++ b/Core/timing.h @@ -15,7 +15,6 @@ enum { GB_TIMA_RELOADED = 2 }; -#define GB_HALT_VALUE (0xFFFF) #define GB_SLEEP(gb, unit, state, cycles) do {\ (gb)->unit##_cycles -= (cycles) * __state_machine_divisor; \ @@ -26,12 +25,10 @@ enum { }\ } while (0) -#define GB_HALT(gb, unit) (gb)->unit##_cycles = GB_HALT_VALUE - #define GB_STATE_MACHINE(gb, unit, cycles, divisor) \ static const int __state_machine_divisor = divisor;\ (gb)->unit##_cycles += cycles; \ -if ((gb)->unit##_cycles <= 0 || (gb)->unit##_cycles == GB_HALT_VALUE) {\ +if ((gb)->unit##_cycles <= 0) {\ return;\ }\ switch ((gb)->unit##_state) From a588993f28257641ffde349af265a29a6e20c1f7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 17 May 2020 00:10:43 +0300 Subject: [PATCH 302/341] Add an HuC command required by Pocket Family 2 --- Core/memory.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Core/memory.c b/Core/memory.c index fbf1318..5075c6f 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -568,6 +568,7 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } gb->huc3_access_index++; return; + case 2: case 3: if (gb->huc3_access_index < 3) { gb->huc3_minutes &= ~(0xF << (gb->huc3_access_index * 4)); @@ -577,7 +578,9 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->huc3_days &= ~(0xF << ((gb->huc3_access_index - 3) * 4)); gb->huc3_days |= ((value & 0xF) << ((gb->huc3_access_index - 3) * 4)); } - gb->huc3_access_index++; + if ((value >> 4) == 3) { + gb->huc3_access_index++; + } return; case 4: gb->huc3_access_index &= 0xF0; From 157123e118589e48273efe3f90eb93afb49b3eab Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 17 May 2020 19:24:11 +0300 Subject: [PATCH 303/341] Fix clearing OAM and initializeing wave RAM --- BootROMs/cgb_boot.asm | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 8770b8a..dc3544f 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -15,6 +15,22 @@ Start: call ClearMemoryPage ld [c], a +; Clear OAM + ld h, $fe + ld c, $a0 +.clearOAMLoop + ldi [hl], a + dec c + jr nz, .clearOAMLoop + +; Init waveform + ld c, $10 +.waveformLoop + ldi [hl], a + cpl + dec c + jr nz, .waveformLoop + ; Clear chosen input palette ldh [InputPalette], a ; Clear title checksum @@ -29,22 +45,6 @@ Start: ld a, $77 ldh [$24], a ld hl, $FF30 -; Init waveform - ld c, $10 -.waveformLoop - ldi [hl], a - cpl - dec c - jr nz, .waveformLoop - - -; Clear OAM - ld h, $fe - ld c, $a0 -.clearOAMLoop - ldi [hl], a - dec c - jr nz, .clearOAMLoop ; Init BG palette ld a, $fc From 933b6228862a871006052ab32bec77fc5515fc46 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 19 May 2020 01:24:02 +0300 Subject: [PATCH 304/341] Allow more GameShark cheats --- Core/cheats.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Core/cheats.c b/Core/cheats.c index 0525816..14875e0 100644 --- a/Core/cheats.c +++ b/Core/cheats.c @@ -133,9 +133,6 @@ bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *descriptio uint8_t value; uint16_t address; if (sscanf(cheat, "%02hhx%02hhx%04hx%c", &bank, &value, &address, &dummy) == 3) { - if (address > 0x7FFF) { - return false; - } if (bank >= 0x80) { bank &= 0xF; } @@ -144,7 +141,7 @@ bool GB_import_cheat(GB_gameboy_t *gb, const char *cheat, const char *descriptio } } - /* GameGnie */ + /* GameGenie */ { char stripped_cheat[10] = {0,}; for (unsigned i = 0; i < 9 && *cheat; i++) { From ce9114ed556f71afcca17ad31b7d9858810a75d4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 19 May 2020 01:24:09 +0300 Subject: [PATCH 305/341] Fix IR bugs --- Core/memory.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index 5075c6f..474eda6 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -595,7 +595,6 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; default: - GB_log(gb, "HuC-3 RTC Write %02x\n", value); break; } @@ -603,9 +602,18 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) case 0xD: // RTC status // Not sure what writes here mean, they're always 0xFE return; - case 0xE: // IR mode + case 0xE: { // IR mode + bool old_input = effective_ir_input(gb); gb->cart_ir = value & 1; + bool new_input = effective_ir_input(gb); + if (new_input != old_input) { + if (gb->infrared_callback) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + } + gb->cycles_since_ir_change = 0; + } return; + } default: GB_log(gb, "Unsupported HuC-3 mode %x write: [%04x] = %02x\n", gb->huc3_mode, addr, value); return; @@ -628,7 +636,9 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->cart_ir = value & 1; bool new_input = effective_ir_input(gb); if (new_input != old_input) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + if (gb->infrared_callback) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + } gb->cycles_since_ir_change = 0; } return; @@ -1041,7 +1051,9 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->io_registers[GB_IO_RP] = value; bool new_input = effective_ir_input(gb); if (new_input != old_input) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + if (gb->infrared_callback) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + } gb->cycles_since_ir_change = 0; } return; From 08ca56eec7f53f6f8849f667c2137666162ca585 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 23 May 2020 00:05:43 +0300 Subject: [PATCH 306/341] Cleanup --- Core/gb.h | 2 +- Core/memory.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index f5f7df5..426a3ef 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -418,7 +418,7 @@ struct GB_gameboy_internal_s { struct { uint8_t bank_low:6; uint8_t bank_high:3; - uint8_t mode:1; + bool mode:1; } huc1; struct { diff --git a/Core/memory.c b/Core/memory.c index 474eda6..61a397a 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -174,13 +174,16 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && gb->cartridge_type->mbc_subtype != GB_CAMERA && gb->cartridge_type->mbc_type != GB_HUC1 && - gb->cartridge_type->mbc_type != GB_HUC3) return 0xFF; + gb->cartridge_type->mbc_type != GB_HUC3) { + return 0xFF; + } if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { return 0xc0 | effective_ir_input(gb); } - if (gb->cartridge_type->has_rtc && gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { + if (gb->cartridge_type->has_rtc && gb->cartridge_type->mbc_type != GB_HUC3 && + gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { /* RTC read */ gb->rtc_latched.high |= ~0xC1; /* Not all bytes in RTC high are used. */ return gb->rtc_latched.data[gb->mbc_ram_bank - 8]; From 369410f3705b155117273548898389d649d52a20 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 23 May 2020 00:09:30 +0300 Subject: [PATCH 307/341] Fix HuC-1 regression --- Core/gb.h | 1 + Core/memory.c | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index 426a3ef..4e6cb3e 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -419,6 +419,7 @@ struct GB_gameboy_internal_s { uint8_t bank_low:6; uint8_t bank_high:3; bool mode:1; + bool ir_mode:1; } huc1; struct { diff --git a/Core/memory.c b/Core/memory.c index 61a397a..85ee099 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -178,7 +178,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) return 0xFF; } - if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { + if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) { return 0xc0 | effective_ir_input(gb); } @@ -525,9 +525,10 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) break; case GB_HUC1: switch (addr & 0xF000) { - case 0x0000: case 0x1000: gb->huc1.mode = (value & 0xF) == 0xE; break; + case 0x0000: case 0x1000: gb->huc1.ir_mode = (value & 0xF) == 0xE; break; case 0x2000: case 0x3000: gb->huc1.bank_low = value; break; case 0x4000: case 0x5000: gb->huc1.bank_high = value; break; + case 0x6000: case 0x7000: gb->huc1.mode = value; break; } break; case GB_HUC3: @@ -634,7 +635,7 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && gb->cartridge_type->mbc_type != GB_HUC1) return; - if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.mode) { + if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) { bool old_input = effective_ir_input(gb); gb->cart_ir = value & 1; bool new_input = effective_ir_input(gb); From 7af66387def986861992d977aab82502e1fe6b6b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 23 May 2020 14:50:54 +0300 Subject: [PATCH 308/341] HuC-3 alarm clock emulation --- Cocoa/AppDelegate.h | 2 +- Cocoa/AppDelegate.m | 6 ++ Cocoa/Document.m | 33 ++++++++++ Core/gb.c | 39 +++++++++++ Core/gb.h | 5 ++ Core/memory.c | 156 +++++++++++++++++++++++++------------------- 6 files changed, 172 insertions(+), 69 deletions(-) diff --git a/Cocoa/AppDelegate.h b/Cocoa/AppDelegate.h index 608a50c..22e0c36 100644 --- a/Cocoa/AppDelegate.h +++ b/Cocoa/AppDelegate.h @@ -1,6 +1,6 @@ #import -@interface AppDelegate : NSObject +@interface AppDelegate : NSObject @property IBOutlet NSWindow *preferencesWindow; @property (strong) IBOutlet NSView *graphicsTab; diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index 3404620..e54012f 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -50,6 +50,8 @@ JOYAxes2DEmulateButtonsKey: @YES, JOYHatsEmulateButtonsKey: @YES, }]; + + [NSUserNotificationCenter defaultUserNotificationCenter].delegate = self; } - (IBAction)toggleDeveloperMode:(id)sender @@ -101,4 +103,8 @@ return YES; } +- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification +{ + [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:notification.identifier display:YES]; +} @end diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ff47cd9..ff77f8e 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -358,6 +358,23 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) } NSTimer *hex_timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(reloadMemoryView) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:hex_timer forMode:NSDefaultRunLoopMode]; + + /* Clear pending alarms, don't play alarms while playing*/ + NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter]; + for (NSUserNotification *notification in [center scheduledNotifications]) { + if ([notification.identifier isEqualToString:self.fileName]) { + [center removeScheduledNotification:notification]; + break; + } + } + + for (NSUserNotification *notification in [center deliveredNotifications]) { + if ([notification.identifier isEqualToString:self.fileName]) { + [center removeDeliveredNotification:notification]; + break; + } + } + while (running) { if (rewind) { rewind = false; @@ -381,6 +398,22 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); GB_save_cheats(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"cht"] UTF8String]); + unsigned time_to_alarm = GB_time_to_alarm(&gb); + + if (time_to_alarm) { + NSUserNotification *notification = [[NSUserNotification alloc] init]; + NSString *friendlyName = [[self.fileName lastPathComponent] stringByDeletingPathExtension]; + NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^)]+\\)|\\[[^\\]]+\\]" options:0 error:nil]; + friendlyName = [regex stringByReplacingMatchesInString:friendlyName options:0 range:NSMakeRange(0, [friendlyName length]) withTemplate:@""]; + friendlyName = [friendlyName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + notification.title = [NSString stringWithFormat:@"%@ Played an Alarm", friendlyName]; + notification.informativeText = [NSString stringWithFormat:@"%@ requested your attention by playing a scheduled alarm", friendlyName]; + notification.identifier = self.fileName; + notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:time_to_alarm]; + notification.soundName = NSUserNotificationDefaultSoundName; + [center scheduleNotification:notification]; + } [_view setRumble:0]; stopping = false; } diff --git a/Core/gb.c b/Core/gb.c index d0632dd..ce9b9af 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -564,6 +564,8 @@ typedef struct __attribute__((packed)) { uint64_t last_rtc_second; uint16_t minutes; uint16_t days; + uint16_t alarm_minutes, alarm_days; + uint8_t alarm_enabled; } GB_huc3_rtc_time_t; typedef union { @@ -612,12 +614,18 @@ int GB_save_battery_to_buffer(GB_gameboy_t *gb, uint8_t *buffer, size_t size) __builtin_bswap64(gb->last_rtc_second), __builtin_bswap16(gb->huc3_minutes), __builtin_bswap16(gb->huc3_days), + __builtin_bswap16(gb->huc3_alarm_minutes), + __builtin_bswap16(gb->huc3_alarm_days), + gb->huc3_alarm_enabled, }; #else GB_huc3_rtc_time_t rtc_save = { gb->last_rtc_second, gb->huc3_minutes, gb->huc3_days, + gb->huc3_alarm_minutes, + gb->huc3_alarm_days, + gb->huc3_alarm_enabled, }; #endif memcpy(buffer, &rtc_save, sizeof(rtc_save)); @@ -666,12 +674,18 @@ int GB_save_battery(GB_gameboy_t *gb, const char *path) __builtin_bswap64(gb->last_rtc_second), __builtin_bswap16(gb->huc3_minutes), __builtin_bswap16(gb->huc3_days), + __builtin_bswap16(gb->huc3_alarm_minutes), + __builtin_bswap16(gb->huc3_alarm_days), + gb->huc3_alarm_enabled, }; #else GB_huc3_rtc_time_t rtc_save = { gb->last_rtc_second, gb->huc3_minutes, gb->huc3_days, + gb->huc3_alarm_minutes, + gb->huc3_alarm_days, + gb->huc3_alarm_enabled, }; #endif @@ -726,10 +740,16 @@ void GB_load_battery_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t gb->last_rtc_second = __builtin_bswap64(rtc_save.last_rtc_second); gb->huc3_minutes = __builtin_bswap16(rtc_save.minutes); gb->huc3_days = __builtin_bswap16(rtc_save.days); + gb->huc3_alarm_minutes = __builtin_bswap16(rtc_save.alarm_minutes); + gb->huc3_alarm_days = __builtin_bswap16(rtc_save.alarm_days); + gb->huc3_alarm_enabled = rtc_save.alarm_enabled; #else gb->last_rtc_second = rtc_save.last_rtc_second; gb->huc3_minutes = rtc_save.minutes; gb->huc3_days = rtc_save.days; + gb->huc3_alarm_minutes = rtc_save.alarm_minutes; + gb->huc3_alarm_days = rtc_save.alarm_days; + gb->huc3_alarm_enabled = rtc_save.alarm_enabled; #endif if (gb->last_rtc_second > time(NULL)) { /* We must reset RTC here, or it will not advance. */ @@ -802,6 +822,7 @@ reset_rtc: gb->rtc_real.high |= 0x80; /* This gives the game a hint that the clock should be reset. */ gb->huc3_days = 0xFFFF; gb->huc3_minutes = 0xFFF; + gb->huc3_alarm_enabled = false; exit: return; } @@ -827,10 +848,16 @@ void GB_load_battery(GB_gameboy_t *gb, const char *path) gb->last_rtc_second = __builtin_bswap64(rtc_save.last_rtc_second); gb->huc3_minutes = __builtin_bswap16(rtc_save.minutes); gb->huc3_days = __builtin_bswap16(rtc_save.days); + gb->huc3_alarm_minutes = __builtin_bswap16(rtc_save.alarm_minutes); + gb->huc3_alarm_days = __builtin_bswap16(rtc_save.alarm_days); + gb->huc3_alarm_enabled = rtc_save.alarm_enabled; #else gb->last_rtc_second = rtc_save.last_rtc_second; gb->huc3_minutes = rtc_save.minutes; gb->huc3_days = rtc_save.days; + gb->huc3_alarm_minutes = rtc_save.alarm_minutes; + gb->huc3_alarm_days = rtc_save.alarm_days; + gb->huc3_alarm_enabled = rtc_save.alarm_enabled; #endif if (gb->last_rtc_second > time(NULL)) { /* We must reset RTC here, or it will not advance. */ @@ -902,6 +929,7 @@ reset_rtc: gb->rtc_real.high |= 0x80; /* This gives the game a hint that the clock should be reset. */ gb->huc3_days = 0xFFFF; gb->huc3_minutes = 0xFFF; + gb->huc3_alarm_enabled = false; exit: fclose(f); return; @@ -1568,3 +1596,14 @@ void GB_set_boot_rom_load_callback(GB_gameboy_t *gb, GB_boot_rom_load_callback_t gb->boot_rom_load_callback = callback; request_boot_rom(gb); } + +unsigned GB_time_to_alarm(GB_gameboy_t *gb) +{ + if (gb->cartridge_type->mbc_type != GB_HUC3) return 0; + if (!gb->huc3_alarm_enabled) return 0; + if (!(gb->huc3_alarm_days & 0x2000)) return 0; + unsigned current_time = (gb->huc3_days & 0x1FFF) * 24 * 60 * 60 + gb->huc3_minutes * 60 + (time(NULL) % 60); + unsigned alarm_time = (gb->huc3_alarm_days & 0x1FFF) * 24 * 60 * 60 + gb->huc3_alarm_minutes * 60; + if (current_time > alarm_time) return 0; + return alarm_time - current_time; +} diff --git a/Core/gb.h b/Core/gb.h index 4e6cb3e..27b95b3 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -438,6 +438,8 @@ struct GB_gameboy_internal_s { uint8_t huc3_mode; uint8_t huc3_access_index; uint16_t huc3_minutes, huc3_days; + uint16_t huc3_alarm_minutes, huc3_alarm_days; + bool huc3_alarm_enabled; uint8_t huc3_read; uint8_t huc3_access_flags; ); @@ -779,6 +781,9 @@ void GB_serial_set_data_bit(GB_gameboy_t *gb, bool data); void GB_disconnect_serial(GB_gameboy_t *gb); +/* For cartridges with an alarm clock */ +unsigned GB_time_to_alarm(GB_gameboy_t *gb); // 0 if no alarm + /* 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 index 85ee099..7f7686a 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -554,77 +554,97 @@ static void write_vram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->vram[(addr & 0x1FFF) + (uint16_t) gb->cgb_vram_bank * 0x2000] = value; } +static bool huc3_write(GB_gameboy_t *gb, uint8_t value) +{ + switch (gb->huc3_mode) { + case 0xB: // RTC Write + switch (value >> 4) { + case 1: + if (gb->huc3_access_index < 3) { + gb->huc3_read = (gb->huc3_minutes >> (gb->huc3_access_index * 4)) & 0xF; + } + else if (gb->huc3_access_index < 7) { + gb->huc3_read = (gb->huc3_days >> ((gb->huc3_access_index - 3) * 4)) & 0xF; + } + else { + // GB_log(gb, "Attempting to read from unsupported HuC-3 register: %03x\n", gb->huc3_access_index); + } + gb->huc3_access_index++; + break; + case 2: + case 3: + if (gb->huc3_access_index < 3) { + gb->huc3_minutes &= ~(0xF << (gb->huc3_access_index * 4)); + gb->huc3_minutes |= ((value & 0xF) << (gb->huc3_access_index * 4)); + } + else if (gb->huc3_access_index < 7) { + gb->huc3_days &= ~(0xF << ((gb->huc3_access_index - 3) * 4)); + gb->huc3_days |= ((value & 0xF) << ((gb->huc3_access_index - 3) * 4)); + } + else if (gb->huc3_access_index >= 0x58 && gb->huc3_access_index <= 0x5a) { + gb->huc3_alarm_minutes &= ~(0xF << ((gb->huc3_access_index - 0x58) * 4)); + gb->huc3_alarm_minutes |= ((value & 0xF) << ((gb->huc3_access_index - 0x58) * 4)); + } + else if (gb->huc3_access_index >= 0x5b && gb->huc3_access_index <= 0x5e) { + gb->huc3_alarm_days &= ~(0xF << ((gb->huc3_access_index - 0x5b) * 4)); + gb->huc3_alarm_days |= ((value & 0xF) << ((gb->huc3_access_index - 0x5b) * 4)); + } + else if (gb->huc3_access_index == 0x5f) { + gb->huc3_alarm_enabled = value & 1; + } + else { + // GB_log(gb, "Attempting to write %x to unsupported HuC-3 register: %03x\n", value & 0xF, gb->huc3_access_index); + } + if ((value >> 4) == 3) { + gb->huc3_access_index++; + } + break; + case 4: + gb->huc3_access_index &= 0xF0; + gb->huc3_access_index |= value & 0xF; + break; + case 5: + gb->huc3_access_index &= 0x0F; + gb->huc3_access_index |= (value & 0xF) << 4; + break; + case 6: + gb->huc3_access_flags = (value & 0xF); + break; + + default: + break; + } + + return true; + case 0xD: // RTC status + // Not sure what writes here mean, they're always 0xFE + return true; + case 0xE: { // IR mode + bool old_input = effective_ir_input(gb); + gb->cart_ir = value & 1; + bool new_input = effective_ir_input(gb); + if (new_input != old_input) { + if (gb->infrared_callback) { + gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + } + gb->cycles_since_ir_change = 0; + } + return true; + } + case 0xC: + return true; + default: + return false; + case 0: // Disabled + case 0xA: // RAM + return false; + } +} + static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) { if (gb->cartridge_type->mbc_type == GB_HUC3) { - switch (gb->huc3_mode) { - case 0xB: // RTC Write - switch (value >> 4) { - case 1: - if (gb->huc3_access_index < 3) { - gb->huc3_read = (gb->huc3_minutes >> (gb->huc3_access_index * 4)) & 0xF; - } - else if (gb->huc3_access_index < 7) { - gb->huc3_read = (gb->huc3_days >> ((gb->huc3_access_index - 3) * 4)) & 0xF; - } - else { - GB_log(gb, "Attempting to read from unsupported HuC-3 register: %03x\n", gb->huc3_access_index); - } - gb->huc3_access_index++; - return; - case 2: - case 3: - if (gb->huc3_access_index < 3) { - gb->huc3_minutes &= ~(0xF << (gb->huc3_access_index * 4)); - gb->huc3_minutes |= ((value & 0xF) << (gb->huc3_access_index * 4)); - } - else if (gb->huc3_access_index < 7) { - gb->huc3_days &= ~(0xF << ((gb->huc3_access_index - 3) * 4)); - gb->huc3_days |= ((value & 0xF) << ((gb->huc3_access_index - 3) * 4)); - } - if ((value >> 4) == 3) { - gb->huc3_access_index++; - } - return; - case 4: - gb->huc3_access_index &= 0xF0; - gb->huc3_access_index |= value & 0xF; - return; - case 5: - gb->huc3_access_index &= 0x0F; - gb->huc3_access_index |= (value & 0xF) << 4; - return; - case 6: - gb->huc3_access_flags = (value & 0xF); - return; - - default: - break; - } - - return; - case 0xD: // RTC status - // Not sure what writes here mean, they're always 0xFE - return; - case 0xE: { // IR mode - bool old_input = effective_ir_input(gb); - gb->cart_ir = value & 1; - bool new_input = effective_ir_input(gb); - if (new_input != old_input) { - if (gb->infrared_callback) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); - } - gb->cycles_since_ir_change = 0; - } - return; - } - default: - GB_log(gb, "Unsupported HuC-3 mode %x write: [%04x] = %02x\n", gb->huc3_mode, addr, value); - return; - case 0: // Disabled - case 0xA: // RAM - break; - } + if (huc3_write(gb, value)) return; } if (gb->camera_registers_mapped) { From f1442b0ea6b86712a1b93c8e1375b08ddb985fcf Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 24 May 2020 23:04:36 +0300 Subject: [PATCH 309/341] Attempt to add rumble support to SDL. Who knows it might work. --- Cocoa/Preferences.xib | 6 ++--- SDL/gui.c | 56 +++++++++++++++++++++++++++++++++++++++---- SDL/gui.h | 2 ++ SDL/main.c | 7 ++++++ 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 149f71e..aa4a87d 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -603,13 +603,13 @@ - + - - + + diff --git a/SDL/gui.c b/SDL/gui.c index 5650d9b..81a9e42 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -108,10 +108,11 @@ configuration_t configuration = .rewind_length = 60 * 2, .model = MODEL_CGB, .volume = 100, + .rumble_mode = GB_RUMBLE_ALL_GAMES, }; -static const char *help[] ={ +static const char *help[] = { "Drop a ROM to play.\n" "\n" "Keyboard Shortcuts:\n" @@ -763,6 +764,7 @@ static void enter_controls_menu(unsigned index) static unsigned joypad_index = 0; static SDL_Joystick *joystick = NULL; static SDL_GameController *controller = NULL; +SDL_Haptic *haptic = NULL; const char *current_joypad_name(unsigned index) { @@ -792,6 +794,12 @@ static void cycle_joypads(unsigned index) if (joypad_index >= SDL_NumJoysticks()) { joypad_index = 0; } + + if (haptic) { + SDL_HapticClose(haptic); + haptic = NULL; + } + if (controller) { SDL_GameControllerClose(controller); controller = NULL; @@ -806,14 +814,22 @@ static void cycle_joypads(unsigned index) else { joystick = SDL_JoystickOpen(joypad_index); } -} + if (joystick) { + haptic = SDL_HapticOpenFromJoystick(joystick); + }} static void cycle_joypads_backwards(unsigned index) { - joypad_index++; + joypad_index--; if (joypad_index >= SDL_NumJoysticks()) { joypad_index = SDL_NumJoysticks() - 1; } + + if (haptic) { + SDL_HapticClose(haptic); + haptic = NULL; + } + if (controller) { SDL_GameControllerClose(controller); controller = NULL; @@ -828,7 +844,9 @@ static void cycle_joypads_backwards(unsigned index) else { joystick = SDL_JoystickOpen(joypad_index); } -} + if (joystick) { + haptic = SDL_HapticOpenFromJoystick(joystick); + }} static void detect_joypad_layout(unsigned index) { @@ -837,9 +855,36 @@ static void detect_joypad_layout(unsigned index) joypad_axis_temp = -1; } +static void cycle_rumble_mode(unsigned index) +{ + if (configuration.rumble_mode == GB_RUMBLE_ALL_GAMES) { + configuration.rumble_mode = GB_RUMBLE_DISABLED; + } + else { + configuration.rumble_mode++; + } +} + +static void cycle_rumble_mode_backwards(unsigned index) +{ + if (configuration.rumble_mode == GB_RUMBLE_DISABLED) { + configuration.rumble_mode = GB_RUMBLE_ALL_GAMES; + } + else { + configuration.rumble_mode--; + } +} + +const char *current_rumble_mode(unsigned index) +{ + return (const char *[]){"Disabled", "Rumble Game Paks Only", "All Games"} + [configuration.rumble_mode]; +} + static const struct menu_item joypad_menu[] = { {"Joypad:", cycle_joypads, current_joypad_name, cycle_joypads_backwards}, {"Configure layout", detect_joypad_layout}, + {"Rumble Mode:", cycle_rumble_mode, current_rumble_mode, cycle_rumble_mode_backwards}, {"Back", return_to_root_menu}, {NULL,} }; @@ -893,6 +938,9 @@ void connect_joypad(void) joystick = SDL_JoystickOpen(0); } } + if (joystick) { + haptic = SDL_HapticOpenFromJoystick(joystick); + } } void run_gui(bool is_running) diff --git a/SDL/gui.h b/SDL/gui.h index 4a3b55f..af7543b 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -21,6 +21,7 @@ extern SDL_Window *window; extern SDL_Renderer *renderer; extern SDL_Texture *texture; extern SDL_PixelFormat *pixel_format; +extern SDL_Haptic *haptic; extern shader_t shader; enum scaling_mode { @@ -105,6 +106,7 @@ typedef struct { uint8_t dmg_palette; GB_border_mode_t border_mode; uint8_t volume; + GB_rumble_mode_t rumble_mode; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index c4a4d0f..f75e3aa 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -367,6 +367,11 @@ static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) return SDL_MapRGB(pixel_format, r, g, b); } +static void rumble(GB_gameboy_t *gb, double amp) +{ + SDL_HapticRumblePlay(haptic, amp, 250); +} + static void debugger_interrupt(int ignore) { if (!GB_is_inited(&gb)) return; @@ -488,6 +493,8 @@ restart: GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_pixels_output(&gb, active_pixel_buffer); GB_set_rgb_encode_callback(&gb, rgb_encode); + GB_set_rumble_callback(&gb, rumble); + GB_set_rumble_mode(&gb, configuration.rumble_mode); GB_set_sample_rate(&gb, GB_audio_get_frequency()); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); update_palette(); From 17dfe0dd6a4e9ec2c6433059a5c94be56ffa1f4c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 29 May 2020 16:30:40 +0300 Subject: [PATCH 310/341] Fix minor CGB-C regression --- Core/display.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index be2e108..4bfae7a 100644 --- a/Core/display.c +++ b/Core/display.c @@ -851,8 +851,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_SLEEP(gb, display, 37, 2); gb->cgb_palettes_blocked = true; - gb->cycles_for_line += 3; - GB_SLEEP(gb, display, 38, 3); + gb->cycles_for_line += (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_C)? 2 : 3; + GB_SLEEP(gb, display, 38, (GB_is_cgb(gb) && gb->model <= GB_MODEL_CGB_C)? 2 : 3); gb->vram_read_blocked = true; gb->vram_write_blocked = true; From 29b64d7545a4464a294d9096f6bbf7318913a6d4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 29 May 2020 16:51:20 +0300 Subject: [PATCH 311/341] Slightly reduce the scanline-ish LCD effect --- Shaders/MasterShader.metal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shaders/MasterShader.metal b/Shaders/MasterShader.metal index ee8dec9..a0b6393 100644 --- a/Shaders/MasterShader.metal +++ b/Shaders/MasterShader.metal @@ -42,7 +42,7 @@ static inline float4 texture(texture2d texture, float2 pos) #line 1 {filter} -#define BLEND_BIAS (1.0/3.0) +#define BLEND_BIAS (2.0/5.0) enum frame_blending_mode { DISABLED, From ffa569deeb1656c2a473e95a782132608152bf8a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 29 May 2020 23:10:23 +0300 Subject: [PATCH 312/341] Partial emulation of reading VRAM right after mode 3 --- Core/display.c | 10 ++++++---- Core/gb.h | 2 ++ Core/memory.c | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Core/display.c b/Core/display.c index 4bfae7a..94d7f77 100644 --- a/Core/display.c +++ b/Core/display.c @@ -603,14 +603,15 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) /* This value is cached on the CGB-D and newer, so it cannot be used to mix tiles together */ gb->fetcher_y = y; } - gb->current_tile = gb->vram[map + x + y / 8 * 32]; + gb->last_tile_index_address = map + x + y / 8 * 32; + gb->current_tile = gb->vram[gb->last_tile_index_address]; if (gb->vram_ppu_blocked) { gb->current_tile = 0xFF; } if (GB_is_cgb(gb)) { /* The CGB actually accesses both the tile index AND the attributes in the same T-cycle. - This probably means the CGB has a 16-bit data bus for the VRAM. */ - gb->current_tile_attributes = gb->vram[map + x + y / 8 * 32 + 0x2000]; + This probably means the CGB has a 16-bit data bus for the VRAM. */ + gb->current_tile_attributes = gb->vram[gb->last_tile_index_address + 0x2000]; if (gb->vram_ppu_blocked) { gb->current_tile_attributes = 0xFF; } @@ -667,8 +668,9 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) if (gb->current_tile_attributes & 0x40) { y_flip = 0x7; } + gb->last_tile_data_address = tile_address + ((y & 7) ^ y_flip) * 2 + 1; gb->current_tile_data[1] = - gb->vram[tile_address + ((y & 7) ^ y_flip) * 2 + 1]; + gb->vram[gb->last_tile_data_address]; if (gb->vram_ppu_blocked) { gb->current_tile_data[1] = 0xFF; } diff --git a/Core/gb.h b/Core/gb.h index 27b95b3..68c4ea9 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -538,6 +538,8 @@ struct GB_gameboy_internal_s { uint8_t window_tile_x; uint8_t lcd_x; // The LCD can go out of sync since the push signal is skipped in some cases. bool is_odd_frame; + uint16_t last_tile_data_address; + uint16_t last_tile_index_address; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ diff --git a/Core/memory.c b/Core/memory.c index 7f7686a..3f924bc 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -146,6 +146,18 @@ static uint8_t read_vram(GB_gameboy_t *gb, uint16_t addr) if (gb->vram_read_blocked) { return 0xFF; } + if (gb->display_state == 22 && GB_is_cgb(gb) && !gb->cgb_double_speed) { + if (addr & 0x1000) { + addr = gb->last_tile_index_address; + } + else if (gb->last_tile_data_address & 0x1000) { + /* TODO: This is case is more complicated then the rest and differ between revisions + It's probably affected by how VRAM is layed out, might be easier after a decap is done*/ + } + else { + addr = gb->last_tile_data_address; + } + } return gb->vram[(addr & 0x1FFF) + (uint16_t) gb->cgb_vram_bank * 0x2000]; } @@ -551,6 +563,19 @@ static void write_vram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) //GB_log(gb, "Wrote %02x to %04x (VRAM) during mode 3\n", value, addr); return; } + /* TODO: not verified */ + if (gb->display_state == 22 && GB_is_cgb(gb) && !gb->cgb_double_speed) { + if (addr & 0x1000) { + addr = gb->last_tile_index_address; + } + else if (gb->last_tile_data_address & 0x1000) { + /* TODO: This is case is more complicated then the rest and differ between revisions + It's probably affected by how VRAM is layed out, might be easier after a decap is done */ + } + else { + addr = gb->last_tile_data_address; + } + } gb->vram[(addr & 0x1FFF) + (uint16_t) gb->cgb_vram_bank * 0x2000] = value; } From fa7232944f277e4abdadd6edcd3f2bc487aca41f Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 01:25:21 +0300 Subject: [PATCH 313/341] =?UTF-8?q?Better=20emulation=20of=20CGB=E2=80=99s?= =?UTF-8?q?=20first=20frame=20behavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/display.c | 11 +++++++++-- Core/gb.h | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 94d7f77..2eb8c42 100644 --- a/Core/display.c +++ b/Core/display.c @@ -128,7 +128,7 @@ static void display_vblank(GB_gameboy_t *gb) bool is_ppu_stopped = !GB_is_cgb(gb) && gb->stopped && gb->io_registers[GB_IO_LCDC] & 0x80; - if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || is_ppu_stopped) || gb->frame_skip_state == GB_FRAMESKIP_LCD_TURNED_ON)) { + if (!gb->disable_rendering && ((!(gb->io_registers[GB_IO_LCDC] & 0x80) || is_ppu_stopped) || gb->cgb_repeated_a_frame)) { /* LCD is off, set screen to white or black (if LCD is on in stop mode) */ if (!GB_is_sgb(gb)) { uint32_t color = 0; @@ -796,6 +796,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) while (true) { GB_SLEEP(gb, display, 1, LCDC_PERIOD); display_vblank(gb); + gb->cgb_repeated_a_frame = true; } return; } @@ -1240,11 +1241,17 @@ abort_fetching_object: } } else { - gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; if (!GB_is_sgb(gb) || gb->current_lcd_line < LINES) { gb->is_odd_frame ^= true; display_vblank(gb); } + if (gb->frame_skip_state == GB_FRAMESKIP_FIRST_FRAME_SKIPPED) { + gb->cgb_repeated_a_frame = true; + gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; + } + else { + gb->cgb_repeated_a_frame = false; + } } } diff --git a/Core/gb.h b/Core/gb.h index 68c4ea9..1376a12 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -540,6 +540,7 @@ struct GB_gameboy_internal_s { bool is_odd_frame; uint16_t last_tile_data_address; uint16_t last_tile_index_address; + bool cgb_repeated_a_frame; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ From 4d91081046f75efc701e0fda7787daebe982c72c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 17:58:02 +0300 Subject: [PATCH 314/341] Do not send LED updates if nothing changed --- JoyKit/JOYController.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 015737e..12f0d39 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -164,6 +164,8 @@ typedef union { // Used when creating inputs JOYElement *_previousAxisElement; + + uint8_t _playerLEDs; } @@ -342,6 +344,7 @@ typedef union { _logicallyConnected = true; _device = (IOHIDDeviceRef)CFRetain(device); _serialSuffix = suffix; + _playerLEDs = -1; IOHIDDeviceRegisterInputValueCallback(device, HIDInput, (void *)self); IOHIDDeviceScheduleWithRunLoop(device, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); @@ -709,6 +712,10 @@ typedef union { - (void)setPlayerLEDs:(uint8_t)mask { mask &= 0xF; + if (mask == _playerLEDs) { + return; + } + _playerLEDs = mask; if (_isSwitch) { _lastVendorSpecificOutput.switchPacket.reportID = 0x1; // Rumble and LEDs _lastVendorSpecificOutput.switchPacket.sequence++; From 59b94b92ca0469f8b207081dd609fd2846fd7937 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 18:43:09 +0300 Subject: [PATCH 315/341] Make sure reports are only sent from one thread --- Cocoa/GBView.m | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index e733731..726259d 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -115,7 +115,7 @@ [NSCursor unhide]; } [[NSNotificationCenter defaultCenter] removeObserver:self]; - [lastController setRumbleAmplitude:0]; + [self setRumble:0]; [JOYController unregisterListener:self]; } - (instancetype)initWithCoder:(NSCoder *)coder @@ -272,7 +272,9 @@ - (void)setRumble:(double)amp { - [lastController setRumbleAmplitude:amp]; + dispatch_async(dispatch_get_main_queue(), ^{ + [lastController setRumbleAmplitude:amp]; + }); } - (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis @@ -301,7 +303,7 @@ { if (![self.window isMainWindow]) return; if (controller != lastController) { - [lastController setRumbleAmplitude:0]; + [self setRumble:0]; lastController = controller; } @@ -319,7 +321,9 @@ ![preferred_joypad isEqualToString:controller.uniqueID]) { continue; } - [controller setPlayerLEDs:1 << player]; + dispatch_async(dispatch_get_main_queue(), ^{ + [controller setPlayerLEDs:1 << player]; + }); NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID]; if (!mapping) { mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName]; From e678b50101271ede54d2f0371af2cbc3775ed550 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 20:15:13 +0300 Subject: [PATCH 316/341] Force all controllers to use a rumble thread --- JoyKit/JOYController.m | 96 +++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 12f0d39..968e2e9 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -39,7 +39,7 @@ static bool axesEmulateButtons = false; static bool axes2DEmulateButtons = false; static bool hatsEmulateButtons = false; -static NSLock *globalPWMThreadLock; +static NSLock *globalRumbleThreadLock; @interface JOYController () + (void)controllerAdded:(IOHIDDeviceRef) device; @@ -152,12 +152,12 @@ typedef union { bool _isSwitch; // Does this controller use the Switch protocol? bool _isDualShock3; // Does this controller use DS3 outputs? JOYVendorSpecificOutput _lastVendorSpecificOutput; - NSLock *_rumblePWMThreadLock; + NSLock *_rumbleThreadLock; volatile double _rumblePWMRatio; bool _physicallyConnected; bool _logicallyConnected; - bool _rumblePWMThreadRunning; - volatile bool _forceStopPWMThread; + bool _rumbleThreadRunning; + volatile bool _forceStopRumbleThread; NSDictionary *_hacks; NSMutableData *_lastReport; @@ -358,7 +358,7 @@ typedef union { _axes2DEmulatedButtons = [NSMutableDictionary dictionary]; _hatEmulatedButtons = [NSMutableDictionary dictionary]; _iokitToJOY = [NSMutableDictionary dictionary]; - _rumblePWMThreadLock = [[NSLock alloc] init]; + _rumbleThreadLock = [[NSLock alloc] init]; //NSMutableArray *axes3d = [NSMutableArray array]; @@ -697,7 +697,7 @@ typedef union { } } _physicallyConnected = false; - [self _forceStopPWMThread]; // Stop the rumble thread. + [self _forceStopRumbleThread]; // Stop the rumble thread. [exposedControllers removeObject:self]; _device = nil; } @@ -731,23 +731,30 @@ typedef union { } } -- (void)pwmThread +- (void)rumbleThread { unsigned rumbleCounter = 0; - while (self.connected && !_forceStopPWMThread) { - if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { - break; - } - rumbleCounter += round(_rumblePWMRatio * PWM_RESOLUTION); - if (rumbleCounter >= PWM_RESOLUTION) { - rumbleCounter -= PWM_RESOLUTION; + if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { + while (self.connected && !_forceStopRumbleThread) { + if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { + break; + } + rumbleCounter += round(_rumblePWMRatio * PWM_RESOLUTION); + if (rumbleCounter >= PWM_RESOLUTION) { + rumbleCounter -= PWM_RESOLUTION; + } } } - [_rumblePWMThreadLock lock]; + else { + while (self.connected && !_forceStopRumbleThread) { + [_rumbleElement setValue:_rumblePWMRatio * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; + } + } + [_rumbleThreadLock lock]; [_rumbleElement setValue:0]; - _rumblePWMThreadRunning = false; - _forceStopPWMThread = false; - [_rumblePWMThreadLock unlock]; + _rumbleThreadRunning = false; + _forceStopRumbleThread = false; + [_rumbleThreadLock unlock]; } - (void)setRumbleAmplitude:(double)amp /* andFrequency: (double)frequency */ @@ -799,32 +806,27 @@ typedef union { [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.ds3Output length:sizeof(_lastVendorSpecificOutput.ds3Output)]]; } else { - if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { - [_rumblePWMThreadLock lock]; - _rumblePWMRatio = amp; - if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. - if (amp != 0) { - /* TODO: The PWM thread does not handle correctly the case of having a multi-port controller where more - than one controller uses rumble. At least make sure any sibling controllers don't have their - PWM thread running. */ - - [globalPWMThreadLock lock]; - for (JOYController *controller in [JOYController allControllers]) { - if (controller != self && controller->_device == _device) { - [controller _forceStopPWMThread]; - } + [_rumbleThreadLock lock]; + _rumblePWMRatio = amp; + if (!_rumbleThreadRunning) { // PWM thread not running, start it. + if (amp != 0) { + /* TODO: The PWM thread does not handle correctly the case of having a multi-port controller where more + than one controller uses rumble. At least make sure any sibling controllers don't have their + PWM thread running. */ + + [globalRumbleThreadLock lock]; + for (JOYController *controller in [JOYController allControllers]) { + if (controller != self && controller->_device == _device) { + [controller _forceStopRumbleThread]; } - _rumblePWMRatio = amp; - _rumblePWMThreadRunning = true; - [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; - [globalPWMThreadLock unlock]; } + _rumblePWMRatio = amp; + _rumbleThreadRunning = true; + [self performSelectorInBackground:@selector(rumbleThread) withObject:nil]; + [globalRumbleThreadLock unlock]; } - [_rumblePWMThreadLock unlock]; - } - else { - [_rumbleElement setValue:amp * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; } + [_rumbleThreadLock unlock]; } } @@ -833,14 +835,14 @@ typedef union { return _logicallyConnected && _physicallyConnected; } -- (void)_forceStopPWMThread +- (void)_forceStopRumbleThread { - [_rumblePWMThreadLock lock]; - if (_rumblePWMThreadRunning) { - _forceStopPWMThread = true; + [_rumbleThreadLock lock]; + if (_rumbleThreadRunning) { + _forceStopRumbleThread = true; } - [_rumblePWMThreadLock unlock]; - while (_rumblePWMThreadRunning); + [_rumbleThreadLock unlock]; + while (_rumbleThreadRunning); } + (void)controllerAdded:(IOHIDDeviceRef) device @@ -900,7 +902,7 @@ typedef union { controllers = [NSMutableDictionary dictionary]; exposedControllers = [NSMutableArray array]; - globalPWMThreadLock = [[NSLock alloc] init]; + globalRumbleThreadLock = [[NSLock alloc] init]; NSArray *array = @[ CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick), CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad), From af5cb72edc7b819969a3079ca11f83ae5f6c64fa Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 21:31:00 +0300 Subject: [PATCH 317/341] Restore Switch LED support --- JoyKit/JOYController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 968e2e9..886dea4 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -722,7 +722,7 @@ typedef union { _lastVendorSpecificOutput.switchPacket.sequence &= 0xF; _lastVendorSpecificOutput.switchPacket.command = 0x30; // LED _lastVendorSpecificOutput.switchPacket.commandData[0] = mask; - //[self sendReport:[NSData dataWithBytes:&_lastSwitchPacket length:sizeof(_lastSwitchPacket)]]; + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.switchPacket length:sizeof(_lastVendorSpecificOutput.switchPacket)]]; } else if (_isDualShock3) { _lastVendorSpecificOutput.ds3Output.reportID = 1; From c9b401135fc35119cda01bebdbd5f3e4e83a7288 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 22:18:32 +0300 Subject: [PATCH 318/341] =?UTF-8?q?Actually,=20don=E2=80=99t=20use=20rumbl?= =?UTF-8?q?e=20threads=20at=20all,=20because=20IOHIDDeviceSetReport=20seem?= =?UTF-8?q?s=20to=20queue=20stuff=20despite=20being=20blocking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cocoa/GBView.m | 4 +- JoyKit/JOYController.m | 206 +++++++++++++++++------------------------ 2 files changed, 85 insertions(+), 125 deletions(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 726259d..e5cb7c8 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -272,9 +272,7 @@ - (void)setRumble:(double)amp { - dispatch_async(dispatch_get_main_queue(), ^{ - [lastController setRumbleAmplitude:amp]; - }); + [lastController setRumbleAmplitude:amp]; } - (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 886dea4..96de291 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -39,8 +39,6 @@ static bool axesEmulateButtons = false; static bool axes2DEmulateButtons = false; static bool hatsEmulateButtons = false; -static NSLock *globalRumbleThreadLock; - @interface JOYController () + (void)controllerAdded:(IOHIDDeviceRef) device; + (void)controllerRemoved:(IOHIDDeviceRef) device; @@ -95,7 +93,9 @@ static void HIDInput(void *context, IOReturn result, void *sender, IOHIDValueRef static void HIDReport(void *context, IOReturn result, void *sender, IOHIDReportType type, uint32_t reportID, uint8_t *report, CFIndex reportLength) { - [(__bridge JOYController *)context gotReport:[[NSData alloc] initWithBytesNoCopy:report length:reportLength freeWhenDone:NO]]; + if (reportLength) { + [(__bridge JOYController *)context gotReport:[[NSData alloc] initWithBytesNoCopy:report length:reportLength freeWhenDone:NO]]; + } } typedef struct __attribute__((packed)) { @@ -152,12 +152,9 @@ typedef union { bool _isSwitch; // Does this controller use the Switch protocol? bool _isDualShock3; // Does this controller use DS3 outputs? JOYVendorSpecificOutput _lastVendorSpecificOutput; - NSLock *_rumbleThreadLock; - volatile double _rumblePWMRatio; + volatile double _rumbleAmplitude; bool _physicallyConnected; bool _logicallyConnected; - bool _rumbleThreadRunning; - volatile bool _forceStopRumbleThread; NSDictionary *_hacks; NSMutableData *_lastReport; @@ -166,7 +163,8 @@ typedef union { JOYElement *_previousAxisElement; uint8_t _playerLEDs; - + double _sentRumbleAmp; + unsigned _rumbleCounter; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device hacks:(NSDictionary *)hacks @@ -358,7 +356,6 @@ typedef union { _axes2DEmulatedButtons = [NSMutableDictionary dictionary]; _hatEmulatedButtons = [NSMutableDictionary dictionary]; _iokitToJOY = [NSMutableDictionary dictionary]; - _rumbleThreadLock = [[NSLock alloc] init]; //NSMutableArray *axes3d = [NSMutableArray array]; @@ -368,18 +365,19 @@ typedef union { _isDualShock3 = [_hacks[JOYIsDualShock3] boolValue]; NSDictionary *customReports = hacks[JOYCustomReports]; - + _lastReport = [NSMutableData dataWithLength:MAX( + MAX( + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxInputReportSizeKey)) unsignedIntValue], + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxOutputReportSizeKey)) unsignedIntValue] + ), + [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxFeatureReportSizeKey)) unsignedIntValue] + )]; + IOHIDDeviceRegisterInputReportCallback(device, _lastReport.mutableBytes, _lastReport.length, HIDReport, (void *)self); + if (hacks[JOYCustomReports]) { _multiElements = [NSMutableDictionary dictionary]; _fullReportElements = [NSMutableDictionary dictionary]; - _lastReport = [NSMutableData dataWithLength:MAX( - MAX( - [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxInputReportSizeKey)) unsignedIntValue], - [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxOutputReportSizeKey)) unsignedIntValue] - ), - [(__bridge NSNumber *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDMaxFeatureReportSizeKey)) unsignedIntValue] - )]; - IOHIDDeviceRegisterInputReportCallback(device, _lastReport.mutableBytes, _lastReport.length, HIDReport, (void *)self); + for (NSNumber *_reportID in customReports) { signed reportID = [_reportID intValue]; @@ -555,16 +553,17 @@ typedef union { - (void)gotReport:(NSData *)report { JOYFullReportElement *element = _fullReportElements[@(*(uint8_t *)report.bytes)]; - if (!element) return; - [element updateValue:report]; - - NSArray *subElements = _multiElements[element]; - if (subElements) { - for (JOYElement *subElement in subElements) { - [self _elementChanged:subElement]; + if (element) { + [element updateValue:report]; + + NSArray *subElements = _multiElements[element]; + if (subElements) { + for (JOYElement *subElement in subElements) { + [self _elementChanged:subElement]; + } } - return; } + [self updateRumble]; } - (void)elementChanged:(IOHIDElementRef)element @@ -697,7 +696,6 @@ typedef union { } } _physicallyConnected = false; - [self _forceStopRumbleThread]; // Stop the rumble thread. [exposedControllers removeObject:self]; _device = nil; } @@ -731,103 +729,78 @@ typedef union { } } -- (void)rumbleThread +- (void)updateRumble { - unsigned rumbleCounter = 0; + if (!self.connected) { + return; + } if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { - while (self.connected && !_forceStopRumbleThread) { - if ([_rumbleElement setValue:rumbleCounter < round(_rumblePWMRatio * PWM_RESOLUTION)]) { - break; - } - rumbleCounter += round(_rumblePWMRatio * PWM_RESOLUTION); - if (rumbleCounter >= PWM_RESOLUTION) { - rumbleCounter -= PWM_RESOLUTION; - } + double ampToSend = _rumbleCounter < round(_rumbleAmplitude * PWM_RESOLUTION); + if (ampToSend != _sentRumbleAmp) { + [_rumbleElement setValue:ampToSend]; + _sentRumbleAmp = ampToSend; + } + _rumbleCounter += round(_rumbleAmplitude * PWM_RESOLUTION); + if (_rumbleCounter >= PWM_RESOLUTION) { + _rumbleCounter -= PWM_RESOLUTION; } } else { - while (self.connected && !_forceStopRumbleThread) { - [_rumbleElement setValue:_rumblePWMRatio * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; + if (_rumbleAmplitude == _sentRumbleAmp) { + return; + } + _sentRumbleAmp = _rumbleAmplitude; + if (_isSwitch) { + double frequency = 144; + double amp = _rumbleAmplitude; + + uint8_t highAmp = amp * 0x64; + uint8_t lowAmp = amp * 0x32 + 0x40; + if (frequency < 0) frequency = 0; + if (frequency > 1252) frequency = 1252; + uint8_t encodedFrequency = (uint8_t)round(log2(frequency / 10.0) * 32.0); + + uint16_t highFreq = (encodedFrequency - 0x60) * 4; + uint8_t lowFreq = encodedFrequency - 0x40; + + //if (frequency < 82 || frequency > 312) { + if (amp) { + highAmp = 0; + } + + if (frequency < 40 || frequency > 626) { + lowAmp = 0; + } + + _lastVendorSpecificOutput.switchPacket.rumbleData[0] = _lastVendorSpecificOutput.switchPacket.rumbleData[4] = highFreq & 0xFF; + _lastVendorSpecificOutput.switchPacket.rumbleData[1] = _lastVendorSpecificOutput.switchPacket.rumbleData[5] = (highAmp << 1) + ((highFreq >> 8) & 0x1); + _lastVendorSpecificOutput.switchPacket.rumbleData[2] = _lastVendorSpecificOutput.switchPacket.rumbleData[6] = lowFreq; + _lastVendorSpecificOutput.switchPacket.rumbleData[3] = _lastVendorSpecificOutput.switchPacket.rumbleData[7] = lowAmp; + + + _lastVendorSpecificOutput.switchPacket.reportID = 0x10; // Rumble only + _lastVendorSpecificOutput.switchPacket.sequence++; + _lastVendorSpecificOutput.switchPacket.sequence &= 0xF; + _lastVendorSpecificOutput.switchPacket.command = 0; // LED + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.switchPacket length:sizeof(_lastVendorSpecificOutput.switchPacket)]]; + } + else if (_isDualShock3) { + _lastVendorSpecificOutput.ds3Output.reportID = 1; + _lastVendorSpecificOutput.ds3Output.rumbleLeftDuration = _lastVendorSpecificOutput.ds3Output.rumbleRightDuration = _rumbleAmplitude? 0xff : 0; + _lastVendorSpecificOutput.ds3Output.rumbleLeftStrength = _lastVendorSpecificOutput.ds3Output.rumbleRightStrength = round(_rumbleAmplitude * 0xff); + [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.ds3Output length:sizeof(_lastVendorSpecificOutput.ds3Output)]]; + } + else { + [_rumbleElement setValue:_rumbleAmplitude * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min]; } } - [_rumbleThreadLock lock]; - [_rumbleElement setValue:0]; - _rumbleThreadRunning = false; - _forceStopRumbleThread = false; - [_rumbleThreadLock unlock]; } - (void)setRumbleAmplitude:(double)amp /* andFrequency: (double)frequency */ { - double frequency = 144; // I have no idea what I'm doing. - if (amp < 0) amp = 0; if (amp > 1) amp = 1; - if (_isSwitch) { - if (amp == 0) { - amp = 1; - frequency = 0; - } - - uint8_t highAmp = amp * 0x64; - uint8_t lowAmp = amp * 0x32 + 0x40; - if (frequency < 0) frequency = 0; - if (frequency > 1252) frequency = 1252; - uint8_t encodedFrequency = (uint8_t)round(log2(frequency / 10.0) * 32.0); - - uint16_t highFreq = (encodedFrequency - 0x60) * 4; - uint8_t lowFreq = encodedFrequency - 0x40; - - //if (frequency < 82 || frequency > 312) { - if (amp) { - highAmp = 0; - } - - if (frequency < 40 || frequency > 626) { - lowAmp = 0; - } - - _lastVendorSpecificOutput.switchPacket.rumbleData[0] = _lastVendorSpecificOutput.switchPacket.rumbleData[4] = highFreq & 0xFF; - _lastVendorSpecificOutput.switchPacket.rumbleData[1] = _lastVendorSpecificOutput.switchPacket.rumbleData[5] = (highAmp << 1) + ((highFreq >> 8) & 0x1); - _lastVendorSpecificOutput.switchPacket.rumbleData[2] = _lastVendorSpecificOutput.switchPacket.rumbleData[6] = lowFreq; - _lastVendorSpecificOutput.switchPacket.rumbleData[3] = _lastVendorSpecificOutput.switchPacket.rumbleData[7] = lowAmp; - - - _lastVendorSpecificOutput.switchPacket.reportID = 0x10; // Rumble only - _lastVendorSpecificOutput.switchPacket.sequence++; - _lastVendorSpecificOutput.switchPacket.sequence &= 0xF; - _lastVendorSpecificOutput.switchPacket.command = 0; // LED - [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.switchPacket length:sizeof(_lastVendorSpecificOutput.switchPacket)]]; - } - else if (_isDualShock3) { - _lastVendorSpecificOutput.ds3Output.reportID = 1; - _lastVendorSpecificOutput.ds3Output.rumbleLeftDuration = _lastVendorSpecificOutput.ds3Output.rumbleRightDuration = amp? 0xff : 0; - _lastVendorSpecificOutput.ds3Output.rumbleLeftStrength = _lastVendorSpecificOutput.ds3Output.rumbleRightStrength = amp * 0xff; - [self sendReport:[NSData dataWithBytes:&_lastVendorSpecificOutput.ds3Output length:sizeof(_lastVendorSpecificOutput.ds3Output)]]; - } - else { - [_rumbleThreadLock lock]; - _rumblePWMRatio = amp; - if (!_rumbleThreadRunning) { // PWM thread not running, start it. - if (amp != 0) { - /* TODO: The PWM thread does not handle correctly the case of having a multi-port controller where more - than one controller uses rumble. At least make sure any sibling controllers don't have their - PWM thread running. */ - - [globalRumbleThreadLock lock]; - for (JOYController *controller in [JOYController allControllers]) { - if (controller != self && controller->_device == _device) { - [controller _forceStopRumbleThread]; - } - } - _rumblePWMRatio = amp; - _rumbleThreadRunning = true; - [self performSelectorInBackground:@selector(rumbleThread) withObject:nil]; - [globalRumbleThreadLock unlock]; - } - } - [_rumbleThreadLock unlock]; - } + _rumbleAmplitude = amp; } - (bool)isConnected @@ -835,16 +808,6 @@ typedef union { return _logicallyConnected && _physicallyConnected; } -- (void)_forceStopRumbleThread -{ - [_rumbleThreadLock lock]; - if (_rumbleThreadRunning) { - _forceStopRumbleThread = true; - } - [_rumbleThreadLock unlock]; - while (_rumbleThreadRunning); -} - + (void)controllerAdded:(IOHIDDeviceRef) device { NSString *name = (__bridge NSString *)IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)); @@ -902,7 +865,6 @@ typedef union { controllers = [NSMutableDictionary dictionary]; exposedControllers = [NSMutableArray array]; - globalRumbleThreadLock = [[NSLock alloc] init]; NSArray *array = @[ CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick), CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad), From c665fcb2ed2705d017bd6dcaef7f405a836c9ce5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 22:20:45 +0300 Subject: [PATCH 319/341] Minor fixes --- JoyKit/JOYController.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 96de291..b815ddb 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -697,6 +697,8 @@ typedef union { } _physicallyConnected = false; [exposedControllers removeObject:self]; + [self setRumbleAmplitude:0]; + [self updateRumble]; _device = nil; } @@ -734,6 +736,9 @@ typedef union { if (!self.connected) { return; } + if (!_rumbleElement && !_isSwitch && !_isDualShock3) { + return; + } if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { double ampToSend = _rumbleCounter < round(_rumbleAmplitude * PWM_RESOLUTION); if (ampToSend != _sentRumbleAmp) { From 83b959c12625ad8840fdb3634a0a7b9ea8be6f8c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 30 May 2020 22:46:06 +0300 Subject: [PATCH 320/341] Delay requests to show notifications --- Cocoa/Document.m | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ff77f8e..035c0e2 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -359,19 +359,21 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) NSTimer *hex_timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(reloadMemoryView) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:hex_timer forMode:NSDefaultRunLoopMode]; - /* Clear pending alarms, don't play alarms while playing*/ - NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter]; - for (NSUserNotification *notification in [center scheduledNotifications]) { - if ([notification.identifier isEqualToString:self.fileName]) { - [center removeScheduledNotification:notification]; - break; + /* Clear pending alarms, don't play alarms while playing */ + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBNotificationsUsed"]) { + NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter]; + for (NSUserNotification *notification in [center scheduledNotifications]) { + if ([notification.identifier isEqualToString:self.fileName]) { + [center removeScheduledNotification:notification]; + break; + } } - } - - for (NSUserNotification *notification in [center deliveredNotifications]) { - if ([notification.identifier isEqualToString:self.fileName]) { - [center removeDeliveredNotification:notification]; - break; + + for (NSUserNotification *notification in [center deliveredNotifications]) { + if ([notification.identifier isEqualToString:self.fileName]) { + [center removeDeliveredNotification:notification]; + break; + } } } @@ -412,7 +414,8 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) notification.identifier = self.fileName; notification.deliveryDate = [NSDate dateWithTimeIntervalSinceNow:time_to_alarm]; notification.soundName = NSUserNotificationDefaultSoundName; - [center scheduleNotification:notification]; + [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification]; + [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"GBNotificationsUsed"]; } [_view setRumble:0]; stopping = false; From f105f2801791f9cafe309adaa407e3c22b2ac3c7 Mon Sep 17 00:00:00 2001 From: James Larrowe Date: Sat, 30 May 2020 15:54:51 -0400 Subject: [PATCH 321/341] Add ld b,b breakpoint Signed-off-by: James Larrowe --- Core/sm83_cpu.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 13f05df..9dbc90f 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -1,6 +1,7 @@ #include #include #include +#include "debugger.h" #include "gb.h" @@ -789,6 +790,11 @@ LD_X_Y(l,b) LD_X_Y(l,c) LD_X_Y(l,d) LD_X_Y(l,e) LD_X_Y(l,h) LD_X_DHL LD_DHL_Y(b) LD_DHL_Y(c) LD_DHL_Y(d) LD_DHL_Y(e) LD_DHL_Y(h) LD_DHL_Y(l) LD_DHL_Y(a) LD_X_Y(a,b) LD_X_Y(a,c) LD_X_Y(a,d) LD_X_Y(a,e) LD_X_Y(a,h) LD_X_Y(a,l) LD_X_DHL(a) +// simply fire the debugger +static void ld_b_b(GB_gameboy_t *gb, uint8_t opcode) +{ + GB_debugger_break(gb); +} static void add_a_r(GB_gameboy_t *gb, uint8_t opcode) { @@ -1462,7 +1468,7 @@ static GB_opcode_t *opcodes[256] = { jr_cc_r8, add_hl_rr, ld_a_dhli, dec_rr, inc_lr, dec_lr, ld_lr_d8, cpl, jr_cc_r8, ld_rr_d16, ld_dhld_a, inc_rr, inc_dhl, dec_dhl, ld_dhl_d8, scf, /* 3X */ jr_cc_r8, add_hl_rr, ld_a_dhld, dec_rr, inc_hr, dec_hr, ld_hr_d8, ccf, - nop, ld_b_c, ld_b_d, ld_b_e, ld_b_h, ld_b_l, ld_b_dhl, ld_b_a, /* 4X */ + ld_b_b, ld_b_c, ld_b_d, ld_b_e, ld_b_h, ld_b_l, ld_b_dhl, ld_b_a, /* 4X */ ld_c_b, nop, ld_c_d, ld_c_e, ld_c_h, ld_c_l, ld_c_dhl, ld_c_a, ld_d_b, ld_d_c, nop, ld_d_e, ld_d_h, ld_d_l, ld_d_dhl, ld_d_a, /* 5X */ ld_e_b, ld_e_c, ld_e_d, nop, ld_e_h, ld_e_l, ld_e_dhl, ld_e_a, From abdece7737c272dc3f1e771fd18256b4d6da8bb9 Mon Sep 17 00:00:00 2001 From: James Larrowe Date: Sat, 30 May 2020 16:35:07 -0400 Subject: [PATCH 322/341] add debugger command to enable and disable --- Core/debugger.c | 18 ++++++++++++++++++ Core/gb.h | 2 +- Core/sm83_cpu.c | 6 ++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index a46de86..ed498c9 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -832,6 +832,23 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const return true; } +/* Enable or disable software breakpoints */ +static bool softbreak(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) +{ + NO_MODIFIERS + if (strcmp(lstrip(arguments), "on") == 0) { + gb->has_software_breakpoints = true; + } + else if(strcmp(lstrip(arguments), "off") == 0) { + gb->has_software_breakpoints = false; + } + else { + print_usage(gb, command); + } + + return true; +} + /* Find the index of the closest breakpoint equal or greater to addr */ static uint16_t find_breakpoint(GB_gameboy_t *gb, value_t addr) { @@ -1780,6 +1797,7 @@ static const debugger_command_t commands[] = { "a more (c)ompact one, or a one-(l)iner", "", "(f|c|l)"}, {"lcd", 3, lcd, "Displays information about the current state of the LCD controller"}, {"palettes", 3, palettes, "Displays the current CGB palettes"}, + {"softbreak", 2, softbreak, "Enables or disables software breakpoints", "(on|off)"}, {"breakpoint", 1, breakpoint, "Add a new breakpoint at the specified address/expression" HELP_NEWLINE "Can also modify the condition of existing breakpoints." HELP_NEWLINE "If the j modifier is used, the breakpoint will occur just before" HELP_NEWLINE diff --git a/Core/gb.h b/Core/gb.h index 97a8069..1445a68 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -601,7 +601,7 @@ struct GB_gameboy_internal_s { /* Breakpoints */ uint16_t n_breakpoints; struct GB_breakpoint_s *breakpoints; - bool has_jump_to_breakpoints; + bool has_jump_to_breakpoints, has_software_breakpoints; void *nontrivial_jump_state; bool non_trivial_jump_breakpoint_occured; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 9dbc90f..b337b36 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -790,10 +790,12 @@ LD_X_Y(l,b) LD_X_Y(l,c) LD_X_Y(l,d) LD_X_Y(l,e) LD_X_Y(l,h) LD_X_DHL LD_DHL_Y(b) LD_DHL_Y(c) LD_DHL_Y(d) LD_DHL_Y(e) LD_DHL_Y(h) LD_DHL_Y(l) LD_DHL_Y(a) LD_X_Y(a,b) LD_X_Y(a,c) LD_X_Y(a,d) LD_X_Y(a,e) LD_X_Y(a,h) LD_X_Y(a,l) LD_X_DHL(a) -// simply fire the debugger +// fire the debugger if software breakpoints are enabled static void ld_b_b(GB_gameboy_t *gb, uint8_t opcode) { - GB_debugger_break(gb); + if(gb->has_software_breakpoints) { + GB_debugger_break(gb); + } } static void add_a_r(GB_gameboy_t *gb, uint8_t opcode) From 6fcf77c7f6e83b3f6e0a207f6a80ea2a2ba3e4af Mon Sep 17 00:00:00 2001 From: James Larrowe Date: Sat, 30 May 2020 16:46:17 -0400 Subject: [PATCH 323/341] Make no argument for softbreak be equivalent to "on" --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index ed498c9..09f78c8 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -836,7 +836,7 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const static bool softbreak(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { NO_MODIFIERS - if (strcmp(lstrip(arguments), "on") == 0) { + if (strcmp(lstrip(arguments), "on") == 0 || !strlen(lstrip(arguments))) { gb->has_software_breakpoints = true; } else if(strcmp(lstrip(arguments), "off") == 0) { From fd97e1191914df4d1ea78024e1f7255dfe7f4fb9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 00:54:13 +0300 Subject: [PATCH 324/341] Spacing --- Core/sm83_cpu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index b337b36..f6a69fb 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -793,7 +793,7 @@ LD_X_Y(a,b) LD_X_Y(a,c) LD_X_Y(a,d) LD_X_Y(a,e) LD_X_Y(a,h) LD_X_Y(a,l) LD_X_DHL // fire the debugger if software breakpoints are enabled static void ld_b_b(GB_gameboy_t *gb, uint8_t opcode) { - if(gb->has_software_breakpoints) { + if (gb->has_software_breakpoints) { GB_debugger_break(gb); } } From f1ea39f1c660ef2e377473a3e3264bed8ed05332 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 00:54:49 +0300 Subject: [PATCH 325/341] Spacing --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 09f78c8..87b0914 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -839,7 +839,7 @@ static bool softbreak(GB_gameboy_t *gb, char *arguments, char *modifiers, const if (strcmp(lstrip(arguments), "on") == 0 || !strlen(lstrip(arguments))) { gb->has_software_breakpoints = true; } - else if(strcmp(lstrip(arguments), "off") == 0) { + else if (strcmp(lstrip(arguments), "off") == 0) { gb->has_software_breakpoints = false; } else { From 97e844a0b743529d17309026e43072fc290e32e5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 01:01:06 +0300 Subject: [PATCH 326/341] GB_debugger_break is for external APIs, not available on libretro builds --- Core/sm83_cpu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 84105ba..3b3eceb 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -1,7 +1,6 @@ #include #include #include -#include "debugger.h" #include "gb.h" @@ -794,7 +793,7 @@ LD_X_Y(a,b) LD_X_Y(a,c) LD_X_Y(a,d) LD_X_Y(a,e) LD_X_Y(a,h) LD_X_Y(a,l) LD_X_DHL static void ld_b_b(GB_gameboy_t *gb, uint8_t opcode) { if (gb->has_software_breakpoints) { - GB_debugger_break(gb); + gb->debug_stopped = true; } } From 0c0ca8e862ebf5aa049b68d067d5db8a7a82b83b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 01:41:27 +0300 Subject: [PATCH 327/341] =?UTF-8?q?Last=20resort=20for=20Macs=20that=20can?= =?UTF-8?q?=E2=80=99t=20send=20reports=20to=20certain=20devices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JoyKit/JOYController.m | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index b815ddb..ca2d1b1 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -120,7 +120,7 @@ typedef struct __attribute__((packed)) { uint8_t dutyLength; uint8_t enabled; uint8_t dutyOff; - uint8_t dutyOnn; + uint8_t dutyOn; } __attribute__((packed)) led[5]; uint8_t padding3[13]; } JOYDualShock3Output; @@ -165,6 +165,7 @@ typedef union { uint8_t _playerLEDs; double _sentRumbleAmp; unsigned _rumbleCounter; + bool _deviceCantSendReports; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device hacks:(NSDictionary *)hacks @@ -482,11 +483,11 @@ typedef union { _lastVendorSpecificOutput.ds3Output = (JOYDualShock3Output){ .reportID = 1, .led = { - {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, - {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, - {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, - {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOnn = 0x32}, - {.timeEnabled = 0, .dutyLength = 0, .enabled = 0, .dutyOff = 0, .dutyOnn = 0}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOn = 0x32}, + {.timeEnabled = 0xff, .dutyLength = 0x27, .enabled = 0x10, .dutyOff = 0, .dutyOn = 0x32}, + {.timeEnabled = 0, .dutyLength = 0, .enabled = 0, .dutyOff = 0, .dutyOn = 0}, } }; @@ -706,7 +707,13 @@ typedef union { { if (!report.length) return; if (!_device) return; - IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length); + if (_deviceCantSendReports) return; + /* Some Macs fail to send reports to some devices, specifically the DS3, returning the bogus(?) error code 1 after + freezing for 5 seconds. Stop sending reports if that's the case. */ + if (IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length) == 1) { + _deviceCantSendReports = true; + NSLog(@"This Mac appears to be incapable of sending output reports to %@", self); + } } - (void)setPlayerLEDs:(uint8_t)mask From 08efb46d41bc06c193cba85edfe00522bb81626d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 20:32:00 +0300 Subject: [PATCH 328/341] =?UTF-8?q?Made=20the=20command=20line=20debugger?= =?UTF-8?q?=20output=20=E2=80=9C>=E2=80=9D=20before=20inputs,=20added=20sp?= =?UTF-8?q?ecial=20magic=20sequence=20to=20break=20the=20debugger=20from?= =?UTF-8?q?=20stdin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/gb.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Core/gb.c b/Core/gb.c index ce9b9af..1bc2235 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -62,6 +62,9 @@ static char *default_input_callback(GB_gameboy_t *gb) { char *expression = NULL; size_t size = 0; + if (gb->debug_stopped) { + printf(">"); + } if (getline(&expression, &size, stdin) == -1) { /* The user doesn't have STDIN or used ^D. We make sure the program keeps running. */ @@ -77,6 +80,12 @@ static char *default_input_callback(GB_gameboy_t *gb) if (expression[length - 1] == '\n') { expression[length - 1] = 0; } + + if (expression[0] == '\x03') { + gb->debug_stopped = true; + free(expression); + return strdup(""); + } return expression; } From 9521729e4e7ae90133d8cdc23b5e82fe837f865d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 21:54:54 +0300 Subject: [PATCH 329/341] Fixed Windows build --- Makefile | 4 ++-- Windows/unistd.h | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 Windows/unistd.h diff --git a/Makefile b/Makefile index 78978e4..8f96bb6 100644 --- a/Makefile +++ b/Makefile @@ -387,10 +387,10 @@ $(OBJ)/%.2bpp: %.png rgbgfx -h -u -o $@ $< $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRESS) - $(PB12_COMPRESS) < $< > $@ + $(realpath $(PB12_COMPRESS)) < $< > $@ $(PB12_COMPRESS): BootROMs/pb12.c - $(CC) -Wall -Werror $< -o $@ + $(CC) $(LDFLAGS) $(CFLAGS) -Wall -Werror $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm diff --git a/Windows/unistd.h b/Windows/unistd.h new file mode 100644 index 0000000..b7aabf2 --- /dev/null +++ b/Windows/unistd.h @@ -0,0 +1,7 @@ +#include +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +#define read(...) _read(__VA_ARGS__) +#define write(...) _write(__VA_ARGS__) From 9e8b4345c03aac473dfc1471b3e22fc540ed961d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 31 May 2020 21:55:04 +0300 Subject: [PATCH 330/341] Update version to 0.13 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8f96bb6..1c6b01a 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.12.3 +VERSION := 0.13 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From 6a3cd371d01e5d253b0ab999da53d87ac84ab7cc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 3 Jun 2020 20:54:06 +0300 Subject: [PATCH 331/341] Fix potential memory corruption when execution malformed ROMs --- Core/mbc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Core/mbc.c b/Core/mbc.c index 72073f6..ba5055f 100644 --- a/Core/mbc.c +++ b/Core/mbc.c @@ -135,7 +135,10 @@ void GB_configure_cart(GB_gameboy_t *gb) static const unsigned ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000}; gb->mbc_ram_size = ram_sizes[gb->rom[0x149]]; } - gb->mbc_ram = malloc(gb->mbc_ram_size); + + if (gb->mbc_ram_size) { + gb->mbc_ram = malloc(gb->mbc_ram_size); + } /* Todo: Some games assume unintialized MBC RAM is 0xFF. It this true for all cartridges types? */ memset(gb->mbc_ram, 0xFF, gb->mbc_ram_size); From b7a9039e50426d80f136403c95e85f23aad11ea3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 3 Jun 2020 21:06:47 +0300 Subject: [PATCH 332/341] Sanitize SDL preferences for cross-version stability --- SDL/main.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SDL/main.c b/SDL/main.c index f75e3aa..3df369f 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -671,7 +671,19 @@ int main(int argc, char **argv) if (prefs_file) { fread(&configuration, 1, sizeof(configuration), prefs_file); fclose(prefs_file); + + /* Sanitize for stability */ + configuration.color_correction_mode %= GB_COLOR_CORRECTION_REDUCE_CONTRAST +1; + configuration.scaling_mode %= GB_SDL_SCALING_MAX; + configuration.blending_mode %= GB_FRAME_BLENDING_MODE_ACCURATE + 1; + configuration.highpass_mode %= GB_HIGHPASS_MAX; + configuration.model %= MODEL_MAX; + configuration.sgb_revision %= SGB_MAX; + configuration.dmg_palette %= 3; + configuration.border_mode %= GB_BORDER_ALWAYS + 1; + configuration.rumble_mode %= GB_RUMBLE_ALL_GAMES + 1; } + if (configuration.model >= MODEL_MAX) { configuration.model = MODEL_CGB; } From ef203cf0e5b6dd1e9ee5da47b0827b0ef132fa02 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 3 Jun 2020 21:18:09 +0300 Subject: [PATCH 333/341] Update version to 0.13.1 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1c6b01a..4b9c291 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13 +VERSION := 0.13.1 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From c07588e3bd7db8f2bfe9e483046ba6856d23fd1e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 5 Jun 2020 02:10:05 +0300 Subject: [PATCH 334/341] Console auto complete --- Cocoa/Document.m | 5 + Cocoa/GBTerminalTextFieldCell.h | 3 +- Cocoa/GBTerminalTextFieldCell.m | 40 ++++++- Core/debugger.c | 194 ++++++++++++++++++++++++++++++-- Core/debugger.h | 2 +- 5 files changed, 231 insertions(+), 13 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 035c0e2..095cfe3 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -8,6 +8,7 @@ #include "GBMemoryByteArray.h" #include "GBWarningPopover.h" #include "GBCheatWindowController.h" +#include "GBTerminalTextFieldCell.h" /* Todo: The general Objective-C coding style conflicts with SameBoy's. This file needs a cleanup. */ /* Todo: Split into category files! This is so messy!!! */ @@ -546,6 +547,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) self.debuggerSideViewInput.textColor = [NSColor whiteColor]; self.debuggerSideViewInput.defaultParagraphStyle = paragraph_style; [self.debuggerSideViewInput setString:@"registers\nbacktrace\n"]; + ((GBTerminalTextFieldCell *)self.consoleInput.cell).gb = &gb; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSideView) name:NSTextDidChangeNotification @@ -1008,6 +1010,9 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [debugger_input_queue removeObjectAtIndex:0]; } [has_debugger_input unlockWithCondition:[debugger_input_queue count] != 0]; + if ((id)input == [NSNull null]) { + return NULL; + } return input? strdup([input UTF8String]): NULL; } diff --git a/Cocoa/GBTerminalTextFieldCell.h b/Cocoa/GBTerminalTextFieldCell.h index eae02e5..484e0c3 100644 --- a/Cocoa/GBTerminalTextFieldCell.h +++ b/Cocoa/GBTerminalTextFieldCell.h @@ -1,5 +1,6 @@ #import +#include @interface GBTerminalTextFieldCell : NSTextFieldCell - +@property GB_gameboy_t *gb; @end diff --git a/Cocoa/GBTerminalTextFieldCell.m b/Cocoa/GBTerminalTextFieldCell.m index e95e785..c1ed203 100644 --- a/Cocoa/GBTerminalTextFieldCell.m +++ b/Cocoa/GBTerminalTextFieldCell.m @@ -2,6 +2,7 @@ #import "GBTerminalTextFieldCell.h" @interface GBTerminalTextView : NSTextView +@property GB_gameboy_t *gb; @end @implementation GBTerminalTextFieldCell @@ -12,10 +13,12 @@ - (NSTextView *)fieldEditorForView:(NSView *)controlView { if (field_editor) { + field_editor.gb = self.gb; return field_editor; } field_editor = [[GBTerminalTextView alloc] init]; [field_editor setFieldEditor:YES]; + field_editor.gb = self.gb; return field_editor; } @@ -26,6 +29,8 @@ NSMutableOrderedSet *lines; NSUInteger current_line; bool reverse_search_mode; + NSRange auto_complete_range; + uintptr_t auto_complete_context; } - (instancetype)init @@ -170,6 +175,7 @@ -(void)setSelectedRanges:(NSArray *)ranges affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag { reverse_search_mode = false; + auto_complete_context = 0; [super setSelectedRanges:ranges affinity:affinity stillSelecting:stillSelectingFlag]; } @@ -188,6 +194,38 @@ [attributes setObject:color forKey:NSForegroundColorAttributeName]; [[[NSAttributedString alloc] initWithString:@"Reverse search..." attributes:attributes] drawAtPoint:NSMakePoint(2, 0)]; } - } + +/* Todo: lazy design, make it use a delegate instead of having a gb reference*/ + +- (void)insertTab:(id)sender +{ + if (auto_complete_context == 0) { + NSRange selection = self.selectedRange; + if (selection.length) { + [self delete:nil]; + } + auto_complete_range = NSMakeRange(selection.location, 0); + } + char *substring = strdup([self.string substringToIndex:auto_complete_range.location].UTF8String); + uintptr_t context = auto_complete_context; + char *completion = GB_debugger_complete_substring(self.gb, substring, &context); + free(substring); + if (completion) { + NSString *ns_completion = @(completion); + free(completion); + if (!ns_completion) { + goto error; + } + self.selectedRange = auto_complete_range; + auto_complete_range.length = ns_completion.length; + [self replaceCharactersInRange:self.selectedRange withString:ns_completion]; + auto_complete_context = context; + return; + } +error: + auto_complete_context = context; + NSBeep(); +} + @end diff --git a/Core/debugger.c b/Core/debugger.c index 34144df..038f76f 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -689,6 +689,7 @@ exit: struct debugger_command_s; typedef bool debugger_command_imp_t(GB_gameboy_t *gb, char *arguments, char *modifiers, const struct debugger_command_s *command); +typedef char *debugger_completer_imp_t(GB_gameboy_t *gb, const char *string, uintptr_t *context); typedef struct debugger_command_s { const char *command; @@ -697,6 +698,8 @@ typedef struct debugger_command_s { const char *help_string; // Null if should not appear in help const char *arguments_format; // For usage message const char *modifiers_format; // For usage message + debugger_completer_imp_t *argument_completer; + debugger_completer_imp_t *modifiers_completer; } debugger_command_t; static const char *lstrip(const char *str) @@ -832,6 +835,19 @@ static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const return true; } +static char *on_off_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context) +{ + size_t length = strlen(string); + const char *suggestions[] = {"on", "off"}; + while (*context < sizeof(suggestions) / sizeof(suggestions[0])) { + if (memcmp(string, suggestions[*context], length) == 0) { + return strdup(suggestions[(*context)++] + length); + } + (*context)++; + } + return NULL; +} + /* Enable or disable software breakpoints */ static bool softbreak(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { @@ -873,6 +889,65 @@ static uint16_t find_breakpoint(GB_gameboy_t *gb, value_t addr) return (uint16_t) min; } +static inline bool is_legal_symbol_char(char c) +{ + if (c >= '0' && c <= '9') return true; + if (c >= 'A' && c <= 'Z') return true; + if (c >= 'a' && c <= 'z') return true; + if (c == '_') return true; + if (c == '.') return true; + return false; +} + +static char *symbol_completer(GB_gameboy_t *gb, const char *string, uintptr_t *_context) +{ + const char *symbol_prefix = string; + while (*string) { + if (!is_legal_symbol_char(*string)) { + symbol_prefix = string + 1; + } + string++; + } + + if (*symbol_prefix == '$') { + return NULL; + } + + struct { + uint16_t bank; + uint32_t symbol; + } *context = (void *)_context; + + + size_t length = strlen(symbol_prefix); + while (context->bank < 0x200) { + if (gb->bank_symbols[context->bank] == NULL || + context->symbol >= gb->bank_symbols[context->bank]->n_symbols) { + context->bank++; + context->symbol = 0; + continue; + } + const char *candidate = gb->bank_symbols[context->bank]->symbols[context->symbol++].name; + if (memcmp(symbol_prefix, candidate, length) == 0) { + return strdup(candidate + length); + } + } + return NULL; +} + +static char *j_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context) +{ + size_t length = strlen(string); + const char *suggestions[] = {"j"}; + while (*context < sizeof(suggestions) / sizeof(suggestions[0])) { + if (memcmp(string, suggestions[*context], length) == 0) { + return strdup(suggestions[(*context)++] + length); + } + (*context)++; + } + return NULL; +} + static bool breakpoint(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { bool is_jump_to = true; @@ -1040,6 +1115,19 @@ static uint16_t find_watchpoint(GB_gameboy_t *gb, value_t addr) return (uint16_t) min; } +static char *rw_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context) +{ + size_t length = strlen(string); + const char *suggestions[] = {"r", "rw", "w"}; + while (*context < sizeof(suggestions) / sizeof(suggestions[0])) { + if (memcmp(string, suggestions[*context], length) == 0) { + return strdup(suggestions[(*context)++] + length); + } + (*context)++; + } + return NULL; +} + static bool watch(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { if (strlen(lstrip(arguments)) == 0) { @@ -1277,6 +1365,19 @@ static bool should_break(GB_gameboy_t *gb, uint16_t addr, bool jump_to) return _should_break(gb, full_addr, jump_to); } +static char *format_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context) +{ + size_t length = strlen(string); + const char *suggestions[] = {"a", "b", "d", "o", "x"}; + while (*context < sizeof(suggestions) / sizeof(suggestions[0])) { + if (memcmp(string, suggestions[*context], length) == 0) { + return strdup(suggestions[(*context)++] + length); + } + (*context)++; + } + return NULL; +} + static bool print(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { if (strlen(lstrip(arguments)) == 0) { @@ -1740,6 +1841,19 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg return true; } +static char *wave_completer(GB_gameboy_t *gb, const char *string, uintptr_t *context) +{ + size_t length = strlen(string); + const char *suggestions[] = {"c", "f", "l"}; + while (*context < sizeof(suggestions) / sizeof(suggestions[0])) { + if (memcmp(string, suggestions[*context], length) == 0) { + return strdup(suggestions[(*context)++] + length); + } + (*context)++; + } + return NULL; +} + static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command) { if (strlen(lstrip(arguments)) || (modifiers && !strchr("fcl", modifiers[0]))) { @@ -1787,7 +1901,7 @@ static const debugger_command_t commands[] = { {"finish", 1, finish, "Run until the current function returns"}, {"backtrace", 2, backtrace, "Displays the current call stack"}, {"bt", 2, }, /* Alias */ - {"sld", 3, stack_leak_detection, "Like finish, but stops if a stack leak is detected (Experimental)"}, + {"sld", 3, stack_leak_detection, "Like finish, but stops if a stack leak is detected"}, {"ticks", 2, ticks, "Displays the number of CPU ticks since the last time 'ticks' was" HELP_NEWLINE "used"}, {"registers", 1, registers, "Print values of processor registers and other important registers"}, @@ -1796,30 +1910,33 @@ static const debugger_command_t commands[] = { {"apu", 3, apu, "Displays information about the current state of the audio chip"}, {"wave", 3, wave, "Prints a visual representation of the wave RAM." HELP_NEWLINE "Modifiers can be used for a (f)ull print (the default)," HELP_NEWLINE - "a more (c)ompact one, or a one-(l)iner", "", "(f|c|l)"}, + "a more (c)ompact one, or a one-(l)iner", "", "(f|c|l)", .modifiers_completer = wave_completer}, {"lcd", 3, lcd, "Displays information about the current state of the LCD controller"}, {"palettes", 3, palettes, "Displays the current CGB palettes"}, - {"softbreak", 2, softbreak, "Enables or disables software breakpoints", "(on|off)"}, + {"softbreak", 2, softbreak, "Enables or disables software breakpoints", "(on|off)", .argument_completer = on_off_completer}, {"breakpoint", 1, breakpoint, "Add a new breakpoint at the specified address/expression" HELP_NEWLINE "Can also modify the condition of existing breakpoints." HELP_NEWLINE "If the j modifier is used, the breakpoint will occur just before" HELP_NEWLINE "jumping to the target.", - "[ if ]", "j"}, - {"delete", 2, delete, "Delete a breakpoint by its address, or all breakpoints", "[]"}, + "[ if ]", "j", + .argument_completer = symbol_completer, .modifiers_completer = j_completer}, + {"delete", 2, delete, "Delete a breakpoint by its address, or all breakpoints", "[]", .argument_completer = symbol_completer}, {"watch", 1, watch, "Add a new watchpoint at the specified address/expression." HELP_NEWLINE "Can also modify the condition and type of existing watchpoints." HELP_NEWLINE "Default watchpoint type is write-only.", - "[ if ]", "(r|w|rw)"}, - {"unwatch", 3, unwatch, "Delete a watchpoint by its address, or all watchpoints", "[]"}, + "[ if ]", "(r|w|rw)", + .argument_completer = symbol_completer, .modifiers_completer = rw_completer + }, + {"unwatch", 3, unwatch, "Delete a watchpoint by its address, or all watchpoints", "[]", .argument_completer = symbol_completer}, {"list", 1, list, "List all set breakpoints and watchpoints"}, {"print", 1, print, "Evaluate and print an expression" HELP_NEWLINE "Use modifier to format as an address (a, default) or as a number in" HELP_NEWLINE "decimal (d), hexadecimal (x), octal (o) or binary (b).", - "", "format"}, + "", "format", .argument_completer = symbol_completer, .modifiers_completer = format_completer}, {"eval", 2, }, /* Alias */ - {"examine", 2, examine, "Examine values at address", "", "count"}, + {"examine", 2, examine, "Examine values at address", "", "count", .argument_completer = symbol_completer}, {"x", 1, }, /* Alias */ - {"disassemble", 1, disassemble, "Disassemble instructions at address", "", "count"}, + {"disassemble", 1, disassemble, "Disassemble instructions at address", "", "count", .argument_completer = symbol_completer}, {"help", 1, help, "List available commands or show help for the specified command", "[]"}, @@ -2075,6 +2192,63 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input) } } +/* Returns true if debugger waits for more commands */ +char *GB_debugger_complete_substring(GB_gameboy_t *gb, char *input, uintptr_t *context) +{ + char *command_string = input; + char *arguments = strchr(input, ' '); + if (arguments) { + /* Actually "split" the string. */ + arguments[0] = 0; + arguments++; + } + + char *modifiers = strchr(command_string, '/'); + if (modifiers) { + /* Actually "split" the string. */ + modifiers[0] = 0; + modifiers++; + } + + const debugger_command_t *command = find_command(command_string); + if (command && command->implementation == help && arguments) { + command_string = arguments; + arguments = NULL; + } + + /* No commands and no modifiers, complete the command */ + if (!arguments && !modifiers) { + size_t length = strlen(command_string); + if (*context >= sizeof(commands) / sizeof(commands[0])) { + return NULL; + } + for (const debugger_command_t *command = &commands[*context]; command->command; command++) { + (*context)++; + if (memcmp(command->command, command_string, length) == 0) { /* Is a substring? */ + return strdup(command->command + length); + } + } + return NULL; + } + + if (command) { + if (arguments) { + if (command->argument_completer) { + return command->argument_completer(gb, arguments, context); + } + return NULL; + } + + if (modifiers) { + if (command->modifiers_completer) { + return command->modifiers_completer(gb, modifiers, context); + } + return NULL; + } + } + return NULL; +} + typedef enum { JUMP_TO_NONE, JUMP_TO_BREAK, diff --git a/Core/debugger.h b/Core/debugger.h index b6a12d9..0678b30 100644 --- a/Core/debugger.h +++ b/Core/debugger.h @@ -34,7 +34,7 @@ bool /* Returns true if debugger waits for more commands. Not relevant for non-G void #endif GB_debugger_execute_command(GB_gameboy_t *gb, char *input); /* Destroys input. */ - +char *GB_debugger_complete_substring(GB_gameboy_t *gb, char *input, uintptr_t *context); /* Destroys input, result requires free */ void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path); const char *GB_debugger_name_for_address(GB_gameboy_t *gb, uint16_t addr); From 4a51f5c95698ad3df8bae22acd01e4e60a74de80 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 9 Jun 2020 20:09:50 +0300 Subject: [PATCH 335/341] Cherry-picking libretro memory map bugfix (Closes #227, #205). Fixing libretro build with modern macOS SDKs. --- Makefile | 2 +- libretro/libretro.c | 81 ++++++++++++++++++++++++--------------------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 4b9c291..587f2f8 100644 --- a/Makefile +++ b/Makefile @@ -390,7 +390,7 @@ $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRE $(realpath $(PB12_COMPRESS)) < $< > $@ $(PB12_COMPRESS): BootROMs/pb12.c - $(CC) $(LDFLAGS) $(CFLAGS) -Wall -Werror $< -o $@ + cc -Wall -Werror $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm diff --git a/libretro/libretro.c b/libretro/libretro.c index a7d6432..24514d4 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -389,47 +389,12 @@ static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) } } -static void init_for_current_model(unsigned id) +static void retro_set_memory_maps(void) { - unsigned i = id; - enum model effective_model; - - effective_model = model[i]; - if (effective_model == MODEL_AUTO) { - effective_model = auto_model; - } - - - if (GB_is_inited(&gameboy[i])) { - GB_switch_model_and_reset(&gameboy[i], libretro_to_internal_model[effective_model]); - } - else { - GB_init(&gameboy[i], libretro_to_internal_model[effective_model]); - } - - GB_set_boot_rom_load_callback(&gameboy[i], boot_rom_load); - - /* When running multiple devices they are assumed to use the same resolution */ - - GB_set_pixels_output(&gameboy[i], - (uint32_t *)(frame_buf + GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]) * i)); - GB_set_rgb_encode_callback(&gameboy[i], rgb_encode); - GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); - GB_apu_set_sample_callback(&gameboy[i], audio_callback); - GB_set_rumble_callback(&gameboy[i], rumble_callback); - - /* todo: attempt to make these more generic */ - GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); - if (emulated_devices == 2) { - GB_set_vblank_callback(&gameboy[1], (GB_vblank_callback_t) vblank2); - if (link_cable_emulation) { - set_link_cable_state(true); - } - } - struct retro_memory_descriptor descs[11]; size_t size; uint16_t bank; + unsigned i; /* todo: add netplay awareness for this so achievements can be granted on the respective client */ @@ -489,6 +454,45 @@ static void init_for_current_model(unsigned id) mmaps.descriptors = descs; mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]); environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); +} + +static void init_for_current_model(unsigned id) +{ + unsigned i = id; + enum model effective_model; + + effective_model = model[i]; + if (effective_model == MODEL_AUTO) { + effective_model = auto_model; + } + + + if (GB_is_inited(&gameboy[i])) { + GB_switch_model_and_reset(&gameboy[i], libretro_to_internal_model[effective_model]); + } + else { + GB_init(&gameboy[i], libretro_to_internal_model[effective_model]); + } + + GB_set_boot_rom_load_callback(&gameboy[i], boot_rom_load); + + /* When running multiple devices they are assumed to use the same resolution */ + + GB_set_pixels_output(&gameboy[i], + (uint32_t *)(frame_buf + GB_get_screen_width(&gameboy[0]) * GB_get_screen_height(&gameboy[0]) * i)); + GB_set_rgb_encode_callback(&gameboy[i], rgb_encode); + GB_set_sample_rate(&gameboy[i], AUDIO_FREQUENCY); + GB_apu_set_sample_callback(&gameboy[i], audio_callback); + GB_set_rumble_callback(&gameboy[i], rumble_callback); + + /* todo: attempt to make these more generic */ + GB_set_vblank_callback(&gameboy[0], (GB_vblank_callback_t) vblank1); + if (emulated_devices == 2) { + GB_set_vblank_callback(&gameboy[1], (GB_vblank_callback_t) vblank2); + if (link_cable_emulation) { + set_link_cable_state(true); + } + } /* Let's be extremely nitpicky about how devices and descriptors are set */ if (emulated_devices == 1 && (model[0] == MODEL_SGB || model[0] == MODEL_SGB2)) { @@ -1070,6 +1074,9 @@ bool retro_load_game(const struct retro_game_info *info) } check_variables(); + + retro_set_memory_maps(); + return true; } From edf77624087f09decd3656adf71bc0400a17af06 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 10 Jun 2020 01:10:11 +0300 Subject: [PATCH 336/341] Improved Dark Mode support, improved Hex Fiend's general system-native appearance --- Cocoa/Document.m | 1 - Cocoa/Document.xib | 22 +++++------ Cocoa/GBColorCell.m | 8 +++- Cocoa/GBPreferencesWindow.m | 20 ++++++++++ Cocoa/Joypad~dark.png | Bin 0 -> 6244 bytes Cocoa/Joypad~dark@2x.png | Bin 0 -> 7175 bytes Cocoa/NSImageNamedDarkSupport.m | 42 ++++++++++++++++++++ Cocoa/Preferences.xib | 10 ++--- Cocoa/Speaker~dark.png | Bin 0 -> 4562 bytes Cocoa/Speaker~dark@2x.png | Bin 0 -> 5992 bytes HexFiend/HFFunctions.h | 2 +- HexFiend/HFLineCountingRepresenter.m | 10 ++--- HexFiend/HFLineCountingView.m | 56 ++++++++++----------------- HexFiend/HFRepresenterTextView.m | 18 ++++++--- HexFiend/HFStatusBarRepresenter.m | 44 +++++---------------- HexFiend/HFTextRepresenter.m | 12 ++++-- 16 files changed, 141 insertions(+), 104 deletions(-) create mode 100644 Cocoa/Joypad~dark.png create mode 100644 Cocoa/Joypad~dark@2x.png create mode 100644 Cocoa/NSImageNamedDarkSupport.m create mode 100644 Cocoa/Speaker~dark.png create mode 100644 Cocoa/Speaker~dark@2x.png diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 095cfe3..fdb7d97 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -647,7 +647,6 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) { hex_controller = [[HFController alloc] init]; [hex_controller setBytesPerColumn:1]; - [hex_controller setFont:[NSFont userFixedPitchFontOfSize:12]]; [hex_controller setEditMode:HFOverwriteMode]; [hex_controller setByteArray:[[GBMemoryByteArray alloc] initWithDocument:self]]; diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index 81ce018..e2b0ca6 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -116,7 +116,7 @@ - + @@ -153,7 +153,7 @@ - + @@ -187,7 +187,7 @@ - + @@ -339,7 +339,7 @@ - + @@ -505,9 +505,9 @@ - + - + @@ -640,7 +640,7 @@ - + @@ -649,7 +649,7 @@ - + @@ -800,7 +800,7 @@ @@ -970,7 +977,7 @@ - + @@ -1069,6 +1076,7 @@ + From 4f42f4f718ec17c38c729b1fa9d9906effa88e36 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 11 Jun 2020 00:38:53 +0300 Subject: [PATCH 340/341] Minor layout fixes --- Cocoa/Document.xib | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index c13c9dd..1197c0f 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -243,9 +243,9 @@ - + - + @@ -903,7 +903,7 @@