From e88a48e0a13f92c565dcc62ff6bd9ce4a8774725 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jun 2020 23:18:38 +0300 Subject: [PATCH 01/98] Use gamma-corrected mixing in shaders --- Shaders/CRT.fsh | 3 --- Shaders/HQ2x.fsh | 4 ++-- Shaders/MasterShader.fsh | 14 +++++++++++--- Shaders/MasterShader.metal | 7 ++++--- Shaders/OmniScale.fsh | 2 +- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Shaders/CRT.fsh b/Shaders/CRT.fsh index 0bc4c65..4cbab72 100644 --- a/Shaders/CRT.fsh +++ b/Shaders/CRT.fsh @@ -158,8 +158,5 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou ret *= output_resolution.y - pixel_position.y; } - // Gamma correction - ret = pow(ret, vec4(0.72)); - return ret; } diff --git a/Shaders/HQ2x.fsh b/Shaders/HQ2x.fsh index 2e19fa6..7ae8063 100644 --- a/Shaders/HQ2x.fsh +++ b/Shaders/HQ2x.fsh @@ -12,7 +12,7 @@ STATIC vec3 rgb_to_hq_colospace(vec4 rgb) STATIC bool is_different(vec4 a, vec4 b) { vec3 diff = abs(rgb_to_hq_colospace(a) - rgb_to_hq_colospace(b)); - return diff.x > 0.188 || diff.y > 0.027 || diff.z > 0.031; + return diff.x > 0.018 || diff.y > 0.002 || diff.z > 0.005; } #define P(m, r) ((pattern & (m)) == (r)) @@ -84,7 +84,7 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou return interp_3px(w4, 2.0, w0, 1.0, w3, 1.0); } if (P(0x2f,0x2f)) { - return interp_3px(w4, 1.04, w3, 1.0, w1, 1.0); + return interp_3px(w4, 4.0, w3, 1.0, w1, 1.0); } if (P(0xbf,0x37) || P(0xdb,0x13)) { return interp_3px(w4, 5.0, w1, 2.0, w3, 1.0); diff --git a/Shaders/MasterShader.fsh b/Shaders/MasterShader.fsh index 729ab5f..a7c33db 100644 --- a/Shaders/MasterShader.fsh +++ b/Shaders/MasterShader.fsh @@ -9,14 +9,22 @@ uniform vec2 origin; #define equal(x, y) ((x) == (y)) #define inequal(x, y) ((x) != (y)) #define STATIC +#define GAMMA (2.2) out vec4 frag_color; +vec4 _texture(sampler2D t, vec2 pos) +{ + return pow(texture(t, pos), vec4(GAMMA)); +} + +#define texture _texture + #line 1 {filter} -#define BLEND_BIAS (1.0/3.0) +#define BLEND_BIAS (2.0/5.0) #define DISABLED 0 #define SIMPLE 1 @@ -58,7 +66,7 @@ void main() break; } - frag_color = mix(scale(image, position, input_resolution, output_resolution), - scale(previous_image, position, input_resolution, output_resolution), ratio); + frag_color = pow(mix(scale(image, position, input_resolution, output_resolution), + scale(previous_image, position, input_resolution, output_resolution), ratio), vec4(1.0 / GAMMA)); } diff --git a/Shaders/MasterShader.metal b/Shaders/MasterShader.metal index a0b6393..b900176 100644 --- a/Shaders/MasterShader.metal +++ b/Shaders/MasterShader.metal @@ -12,6 +12,7 @@ typedef texture2d sampler2D; #define equal(x, y) all((x) == (y)) #define inequal(x, y) any((x) != (y)) #define STATIC static +#define GAMMA (2.2) typedef struct { float4 position [[position]]; @@ -36,7 +37,7 @@ vertex rasterizer_data vertex_shader(uint index [[ vertex_id ]], static inline float4 texture(texture2d texture, float2 pos) { constexpr sampler texture_sampler; - return float4(texture.sample(texture_sampler, pos)); + return pow(float4(texture.sample(texture_sampler, pos)), GAMMA); } #line 1 @@ -87,7 +88,7 @@ fragment float4 fragment_shader(rasterizer_data in [[stage_in]], break; } - return mix(scale(image, in.texcoords, input_resolution, *output_resolution), - scale(previous_image, in.texcoords, input_resolution, *output_resolution), ratio); + return pow(mix(scale(image, in.texcoords, input_resolution, *output_resolution), + scale(previous_image, in.texcoords, input_resolution, *output_resolution), ratio), 1 / GAMMA); } diff --git a/Shaders/OmniScale.fsh b/Shaders/OmniScale.fsh index c76f736..eab27ae 100644 --- a/Shaders/OmniScale.fsh +++ b/Shaders/OmniScale.fsh @@ -19,7 +19,7 @@ STATIC vec3 rgb_to_hq_colospace(vec4 rgb) STATIC bool is_different(vec4 a, vec4 b) { vec3 diff = abs(rgb_to_hq_colospace(a) - rgb_to_hq_colospace(b)); - return diff.x > 0.125 || diff.y > 0.027 || diff.z > 0.031; + return diff.x > 0.018 || diff.y > 0.002 || diff.z > 0.005; } #define P(m, r) ((pattern & (m)) == (r)) From 87d25c089663615e2b7608117be01fad432b15b0 Mon Sep 17 00:00:00 2001 From: James Larrowe Date: Mon, 22 Jun 2020 20:14:32 +0000 Subject: [PATCH 02/98] Compatibility hacks for old compilers GCC versions below 4.8.1 didn't have __builtin_bswap16, so provide a suitable replacement. --- Core/gb.h | 4 ++++ Makefile | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Core/gb.h b/Core/gb.h index b773ebb..409e4cd 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -52,6 +52,10 @@ #error Unable to detect endianess #endif +#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) +#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; }) +#endif + typedef struct { struct { uint8_t r, g, b; diff --git a/Makefile b/Makefile index d598f97..f8e9935 100644 --- a/Makefile +++ b/Makefile @@ -392,7 +392,7 @@ $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRE $(realpath $(PB12_COMPRESS)) < $< > $@ $(PB12_COMPRESS): BootROMs/pb12.c - $(NATIVE_CC) -Wall -Werror $< -o $@ + $(NATIVE_CC) -std=c99 -Wall -Werror $< -o $@ $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm From cfcdce81ba38904e7b1f8fe8b553a2dbd7810b69 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 24 Jun 2020 19:15:20 +0300 Subject: [PATCH 03/98] Fix color in OpenGL when frame blending is disabled --- Shaders/MasterShader.fsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shaders/MasterShader.fsh b/Shaders/MasterShader.fsh index a7c33db..3f891d5 100644 --- a/Shaders/MasterShader.fsh +++ b/Shaders/MasterShader.fsh @@ -43,7 +43,7 @@ void main() switch (frame_blending_mode) { default: case DISABLED: - frag_color = scale(image, position, input_resolution, output_resolution); + frag_color = pow(scale(image, position, input_resolution, output_resolution), vec4(1.0 / GAMMA)); return; case SIMPLE: ratio = 0.5; From a2e656a7c27f3b68cb397c0a590267920297a4b8 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 24 Jun 2020 20:34:52 +0300 Subject: [PATCH 04/98] Fixed boot ROM regression; CGB games were given the wrong palettes --- BootROMs/cgb_boot.asm | 192 +++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 95 deletions(-) diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index dc3544f..1345915 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -329,101 +329,103 @@ FirstChecksumWithDuplicate: ChecksumsEnd: PalettePerChecksum: -; | $80 means game requires DMG boot tilemap - db 0 ; Default Palette - db 4 ; ALLEY WAY - db 5 ; YAKUMAN - db 35 ; BASEBALL, (Game and Watch 2) - db 34 ; TENNIS - db 3 ; TETRIS - db 31 ; QIX - db 15 ; DR.MARIO - db 10 ; RADARMISSION - db 5 ; F1RACE - db 19 ; YOSSY NO TAMAGO - db 36 ; - db 7 | $80 ; X - db 37 ; MARIOLAND2 - db 30 ; YOSSY NO COOKIE - db 44 ; ZELDA - db 21 ; - db 32 ; - db 31 ; TETRIS FLASH - db 20 ; DONKEY KONG - db 5 ; MARIO'S PICROSS - db 33 ; - db 13 ; POKEMON RED, (GAMEBOYCAMERA G) - db 14 ; POKEMON GREEN - db 5 ; PICROSS 2 - db 29 ; YOSSY NO PANEPON - db 5 ; KIRAKIRA KIDS - db 18 ; GAMEBOY GALLERY - db 9 ; POCKETCAMERA - db 3 ; - db 2 ; BALLOON KID - db 26 ; KINGOFTHEZOO - db 25 ; DMG FOOTBALL - db 25 ; WORLD CUP - db 41 ; OTHELLO - db 42 ; SUPER RC PRO-AM - db 26 ; DYNABLASTER - db 45 ; BOY AND BLOB GB2 - db 42 ; MEGAMAN - db 45 ; STAR WARS-NOA - db 36 ; - db 38 ; WAVERACE - db 26 ; - db 42 ; LOLO2 - db 30 ; YOSHI'S COOKIE - db 41 ; MYSTIC QUEST - db 34 ; - db 34 ; TOPRANKINGTENNIS - db 5 ; MANSELL - db 42 ; MEGAMAN3 - db 6 ; SPACE INVADERS - db 5 ; GAME&WATCH - db 33 ; DONKEYKONGLAND95 - db 25 ; ASTEROIDS/MISCMD - db 42 ; STREET FIGHTER 2 - db 42 ; DEFENDER/JOUST - db 40 ; KILLERINSTINCT95 - db 2 ; TETRIS BLAST - db 16 ; PINOCCHIO - db 25 ; - db 42 ; BA.TOSHINDEN - db 42 ; NETTOU KOF 95 - db 5 ; - db 0 ; TETRIS PLUS - db 39 ; DONKEYKONGLAND 3 - db 36 ; - db 22 ; SUPER MARIOLAND - db 25 ; GOLF - db 6 ; SOLARSTRIKER - db 32 ; GBWARS - db 12 ; KAERUNOTAMENI - db 36 ; - db 11 ; POKEMON BLUE - db 39 ; DONKEYKONGLAND - db 18 ; GAMEBOY GALLERY2 - db 39 ; DONKEYKONGLAND 2 - db 24 ; KID ICARUS - db 31 ; TETRIS2 - db 50 ; - db 17 ; MOGURANYA - db 46 ; - db 6 ; GALAGA&GALAXIAN - db 27 ; BT2RAGNAROKWORLD - db 0 ; KEN GRIFFEY JR - db 47 ; - db 41 ; MAGNETIC SOCCER - db 41 ; VEGAS STAKES - db 0 ; - db 0 ; MILLI/CENTI/PEDE - db 19 ; MARIO & YOSHI - db 34 ; SOCCER - db 23 ; POKEBOM - db 18 ; G&W GALLERY - db 29 ; TETRIS ATTACK +palette_index: MACRO ; palette, flags + db ((\1) * 3) | (\2) ; | $80 means game requires DMG boot tilemap +ENDM + palette_index 0, 0 ; Default Palette + palette_index 4, 0 ; ALLEY WAY + palette_index 5, 0 ; YAKUMAN + palette_index 35, 0 ; BASEBALL, (Game and Watch 2) + palette_index 34, 0 ; TENNIS + palette_index 3, 0 ; TETRIS + palette_index 31, 0 ; QIX + palette_index 15, 0 ; DR.MARIO + palette_index 10, 0 ; RADARMISSION + palette_index 5, 0 ; F1RACE + palette_index 19, 0 ; YOSSY NO TAMAGO + palette_index 36, 0 ; + palette_index 7, $80 ; X + palette_index 37, 0 ; MARIOLAND2 + palette_index 30, 0 ; YOSSY NO COOKIE + palette_index 44, 0 ; ZELDA + palette_index 21, 0 ; + palette_index 32, 0 ; + palette_index 31, 0 ; TETRIS FLASH + palette_index 20, 0 ; DONKEY KONG + palette_index 5, 0 ; MARIO'S PICROSS + palette_index 33, 0 ; + palette_index 13, 0 ; POKEMON RED, (GAMEBOYCAMERA G) + palette_index 14, 0 ; POKEMON GREEN + palette_index 5, 0 ; PICROSS 2 + palette_index 29, 0 ; YOSSY NO PANEPON + palette_index 5, 0 ; KIRAKIRA KIDS + palette_index 18, 0 ; GAMEBOY GALLERY + palette_index 9, 0 ; POCKETCAMERA + palette_index 3, 0 ; + palette_index 2, 0 ; BALLOON KID + palette_index 26, 0 ; KINGOFTHEZOO + palette_index 25, 0 ; DMG FOOTBALL + palette_index 25, 0 ; WORLD CUP + palette_index 41, 0 ; OTHELLO + palette_index 42, 0 ; SUPER RC PRO-AM + palette_index 26, 0 ; DYNABLASTER + palette_index 45, 0 ; BOY AND BLOB GB2 + palette_index 42, 0 ; MEGAMAN + palette_index 45, 0 ; STAR WARS-NOA + palette_index 36, 0 ; + palette_index 38, 0 ; WAVERACE + palette_index 26, 0 ; + palette_index 42, 0 ; LOLO2 + palette_index 30, 0 ; YOSHI'S COOKIE + palette_index 41, 0 ; MYSTIC QUEST + palette_index 34, 0 ; + palette_index 34, 0 ; TOPRANKINGTENNIS + palette_index 5, 0 ; MANSELL + palette_index 42, 0 ; MEGAMAN3 + palette_index 6, 0 ; SPACE INVADERS + palette_index 5, 0 ; GAME&WATCH + palette_index 33, 0 ; DONKEYKONGLAND95 + palette_index 25, 0 ; ASTEROIDS/MISCMD + palette_index 42, 0 ; STREET FIGHTER 2 + palette_index 42, 0 ; DEFENDER/JOUST + palette_index 40, 0 ; KILLERINSTINCT95 + palette_index 2, 0 ; TETRIS BLAST + palette_index 16, 0 ; PINOCCHIO + palette_index 25, 0 ; + palette_index 42, 0 ; BA.TOSHINDEN + palette_index 42, 0 ; NETTOU KOF 95 + palette_index 5, 0 ; + palette_index 0, 0 ; TETRIS PLUS + palette_index 39, 0 ; DONKEYKONGLAND 3 + palette_index 36, 0 ; + palette_index 22, 0 ; SUPER MARIOLAND + palette_index 25, 0 ; GOLF + palette_index 6, 0 ; SOLARSTRIKER + palette_index 32, 0 ; GBWARS + palette_index 12, 0 ; KAERUNOTAMENI + palette_index 36, 0 ; + palette_index 11, 0 ; POKEMON BLUE + palette_index 39, 0 ; DONKEYKONGLAND + palette_index 18, 0 ; GAMEBOY GALLERY2 + palette_index 39, 0 ; DONKEYKONGLAND 2 + palette_index 24, 0 ; KID ICARUS + palette_index 31, 0 ; TETRIS2 + palette_index 50, 0 ; + palette_index 17, 0 ; MOGURANYA + palette_index 46, 0 ; + palette_index 6, 0 ; GALAGA&GALAXIAN + palette_index 27, 0 ; BT2RAGNAROKWORLD + palette_index 0, 0 ; KEN GRIFFEY JR + palette_index 47, 0 ; + palette_index 41, 0 ; MAGNETIC SOCCER + palette_index 41, 0 ; VEGAS STAKES + palette_index 0, 0 ; + palette_index 0, 0 ; MILLI/CENTI/PEDE + palette_index 19, 0 ; MARIO & YOSHI + palette_index 34, 0 ; SOCCER + palette_index 23, 0 ; POKEBOM + palette_index 18, 0 ; G&W GALLERY + palette_index 29, 0 ; TETRIS ATTACK Dups4thLetterArray: db "BEFAARBEKEK R-URAR INAILICE R" From 64f381fa2382f972090315788e5a95c4594e2fa1 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 24 Jun 2020 23:34:33 +0300 Subject: [PATCH 05/98] Update version to 0.13.3 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f8e9935..1f4715e 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13.2 +VERSION := 0.13.3 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From a7942d6a1f08a88cfc0c77aa6100a0b1b091bd7b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 25 Jun 2020 19:51:58 +0300 Subject: [PATCH 06/98] Allow building fat x86-64 and ARM64 macOS binaries --- Makefile | 27 +++++++++++++++++---------- README.md | 4 ++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 1f4715e..19652dd 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,13 @@ LDFLAGS += -march=native -mtune=native CFLAGS += -march=native -mtune=native endif +ifeq ($(CONF),fat_release) +override CONF := release +FAT_FLAGS += -arch x86_64 -arch arm64 +endif + + + # Set compilation and linkage flags based on target, platform and configuration OPEN_DIALOG = OpenDialog/gtk.c @@ -134,9 +141,9 @@ ifeq ($(SYSROOT),/Library/Developer/CommandLineTools/SDKs/) $(error Could not find a macOS SDK) endif -CFLAGS += -F/Library/Frameworks -mmacosx-version-min=10.9 +CFLAGS += -F/Library/Frameworks -mmacosx-version-min=10.9 -isysroot $(SYSROOT) 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 +LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore -weak_framework Metal -weak_framework MetalKit -mmacosx-version-min=10.9 -isysroot $(SYSROOT) GL_LDFLAGS := -framework OpenGL endif CFLAGS += -Wno-deprecated-declarations @@ -229,24 +236,24 @@ $(OBJ)/%.dep: % $(OBJ)/Core/%.c.o: Core/%.c -@$(MKDIR) -p $(dir $@) - $(CC) $(CFLAGS) -DGB_INTERNAL -c $< -o $@ + $(CC) $(CFLAGS) $(FAT_FLAGS) -DGB_INTERNAL -c $< -o $@ $(OBJ)/SDL/%.c.o: SDL/%.c -@$(MKDIR) -p $(dir $@) - $(CC) $(CFLAGS) $(SDL_CFLAGS) $(GL_CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) $(FAT_FLAGS) $(SDL_CFLAGS) $(GL_CFLAGS) -c $< -o $@ $(OBJ)/%.c.o: %.c -@$(MKDIR) -p $(dir $@) - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) $(FAT_FLAGS) -c $< -o $@ # HexFiend requires more flags $(OBJ)/HexFiend/%.m.o: HexFiend/%.m -@$(MKDIR) -p $(dir $@) - $(CC) $(CFLAGS) $(OCFLAGS) -c $< -o $@ -fno-objc-arc -include HexFiend/HexFiend_2_Framework_Prefix.pch + $(CC) $(CFLAGS) $(FAT_FLAGS) $(OCFLAGS) -c $< -o $@ -fno-objc-arc -include HexFiend/HexFiend_2_Framework_Prefix.pch $(OBJ)/%.m.o: %.m -@$(MKDIR) -p $(dir $@) - $(CC) $(CFLAGS) $(OCFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) $(FAT_FLAGS) $(OCFLAGS) -c $< -o $@ # Cocoa Port @@ -274,7 +281,7 @@ $(BIN)/SameBoy.app: $(BIN)/SameBoy.app/Contents/MacOS/SameBoy \ $(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 + $(CC) $^ -o $@ $(LDFLAGS) $(FAT_FLAGS) -framework OpenGL -framework AudioUnit -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework IOKit ifeq ($(CONF), release) $(STRIP) $@ endif @@ -296,7 +303,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) -Wl,-exported_symbols_list,QuickLook/exports.sym -bundle -framework Cocoa -framework Quicklook + $(CC) $^ -o $@ $(LDFLAGS) $(FAT_FLAGS) -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. @@ -309,7 +316,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) $(GL_LDFLAGS) + $(CC) $^ -o $@ $(LDFLAGS) $(FAT_FLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) ifeq ($(CONF), release) $(STRIP) $@ endif diff --git a/README.md b/README.md index d626bbe..88b8caa 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ On Windows, SameBoy also requires: * [GnuWin](http://gnuwin32.sourceforge.net/) * Running vcvars32 before running make. Make sure all required tools and libraries are in %PATH% and %lib%, respectively. -To compile, simply run `make`. The targets are `cocoa` (Default for macOS), `sdl` (Default for everything else), `libretro`, `bootroms` and `tester`. You may also specify `CONF=debug` (default), `CONF=release` or `CONF=native_release` to control optimization and symbols. `native_release` is faster than `release`, but is optimized to the host's CPU and therefore is not portable. You may set `BOOTROMS_DIR=...` to a directory containing precompiled `dmg_boot.bin` and `cgb_boot.bin` files, otherwise the build system will compile and use SameBoy's own boot ROMs. +To compile, simply run `make`. The targets are `cocoa` (Default for macOS), `sdl` (Default for everything else), `libretro`, `bootroms` and `tester`. You may also specify `CONF=debug` (default), `CONF=release`, `CONF=native_release` or `CONF=fat_release` to control optimization, symbols and multi-architectures. `native_release` is faster than `release`, but is optimized to the host's CPU and therefore is not portable. `fat_release` is exclusive to macOS and builds x86-64 and ARM64 fat binaries; this requires using a recent enough `clang` and macOS SDK using `xcode-select`, or setting them explicitly with `CC=` and `SYSROOT=`, respectively. All other configurations will build to your host architecture. You may set `BOOTROMS_DIR=...` to a directory containing precompiled boot ROM files, otherwise the build system will compile and use SameBoy's own boot ROMs. By default, the SDL port will look for resource files with a path relative to executable. If you are packaging SameBoy, you may wish to override this by setting the `DATA_DIR` variable during compilation to the target path of the directory containing all files (apart from the executable, that's not necessary) from the `build/bin/SDL` directory in the source tree. Make sure the variable ends with a `/` character. -SameBoy was compiled and tested on macOS, Ubuntu and 32-bit Windows 7. +SameBoy was compiled and tested on macOS, Ubuntu and 64-bit Windows 7. From 44ff0563c08ab182adf1ca13921ff4147b6e3d4e Mon Sep 17 00:00:00 2001 From: Leopoldo Pla Date: Thu, 25 Jun 2020 19:50:24 +0200 Subject: [PATCH 07/98] Fixes #259 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 19652dd..aaa47b0 100644 --- a/Makefile +++ b/Makefile @@ -287,7 +287,7 @@ ifeq ($(CONF), release) endif $(BIN)/SameBoy.app/Contents/Resources/Base.lproj/%.nib: Cocoa/%.xib - ibtool --compile $@ $^ + ibtool --compile $@ $^ 2>&1 | cat - # Quick Look generator From ac3b09966cb849e07ce4f1278e83fcd4133c373e Mon Sep 17 00:00:00 2001 From: Renato Oliveira Date: Sat, 1 Aug 2020 12:55:53 -0300 Subject: [PATCH 08/98] Fix "Cartrdige" to "Cartridge" --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index 038f76f..e013076 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1533,7 +1533,7 @@ static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg const GB_cartridge_t *cartridge = gb->cartridge_type; if (cartridge->has_ram) { - GB_log(gb, "Cartrdige includes%s RAM: $%x bytes\n", cartridge->has_battery? " battery-backed": "", gb->mbc_ram_size); + GB_log(gb, "Cartridge includes%s RAM: $%x bytes\n", cartridge->has_battery? " battery-backed": "", gb->mbc_ram_size); } else { GB_log(gb, "No cartridge RAM\n"); From 445aa74b145b360cec1a62b423737d6898198d7b Mon Sep 17 00:00:00 2001 From: Renato Oliveira Date: Sat, 1 Aug 2020 12:57:10 -0300 Subject: [PATCH 09/98] Fix "cartrdige" to "cartridge" in comments --- QuickLook/generator.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/QuickLook/generator.m b/QuickLook/generator.m index c3c13dc..1b2de66 100644 --- a/QuickLook/generator.m +++ b/QuickLook/generator.m @@ -61,9 +61,9 @@ static OSStatus render(CGContextRef cgContext, CFURLRef url, bool showBorder) } if (showBorder) { - /* Use the CGB flag to determine the cartrdige "look": + /* Use the CGB flag to determine the cartridge "look": - DMG cartridges are grey - - CGB cartrdiges are transparent + - CGB cartridges are transparent - CGB cartridges that support DMG systems are black */ NSImage *effectiveTemplate = nil; @@ -115,4 +115,4 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum CGContextRelease(cgContext); return -1; } -} \ No newline at end of file +} From b0d118f2465005924155e9eaada2e5afa05c1f73 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 4 Aug 2020 21:32:27 +0300 Subject: [PATCH 10/98] Fix broken and regressed MBC3 RTC emulation. Fixes #273, fixes #276, fixes #280 --- Core/gb.h | 3 ++- Core/memory.c | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Core/gb.h b/Core/gb.h index 409e4cd..f085eac 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -438,7 +438,7 @@ struct GB_gameboy_internal_s { bool rumble_state; bool cart_ir; - // TODO: move to huc3 struct when breaking save compat + // TODO: move to huc3/mbc3 struct when breaking save compat uint8_t huc3_mode; uint8_t huc3_access_index; uint16_t huc3_minutes, huc3_days; @@ -446,6 +446,7 @@ struct GB_gameboy_internal_s { bool huc3_alarm_enabled; uint8_t huc3_read; uint8_t huc3_access_flags; + bool mbc3_rtc_mapped; ); diff --git a/Core/memory.c b/Core/memory.c index 3f924bc..c44c4ec 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -195,10 +195,10 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) } if (gb->cartridge_type->has_rtc && gb->cartridge_type->mbc_type != GB_HUC3 && - gb->mbc_ram_bank >= 8 && gb->mbc_ram_bank <= 0xC) { + gb->mbc3_rtc_mapped && gb->mbc_ram_bank <= 4) { /* 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]; + return gb->rtc_latched.data[gb->mbc_ram_bank]; } if (gb->camera_registers_mapped) { @@ -509,7 +509,10 @@ static void write_mbc(GB_gameboy_t *gb, uint16_t addr, uint8_t value) switch (addr & 0xF000) { case 0x0000: case 0x1000: gb->mbc_ram_enable = (value & 0xF) == 0xA; break; case 0x2000: case 0x3000: gb->mbc3.rom_bank = value; break; - case 0x4000: case 0x5000: gb->mbc3.ram_bank = value; break; + case 0x4000: case 0x5000: + gb->mbc3.ram_bank = value; + gb->mbc3_rtc_mapped = value & 8; + break; case 0x6000: case 0x7000: if (!gb->rtc_latch && (value & 1)) { /* Todo: verify condition is correct */ memcpy(&gb->rtc_latched, &gb->rtc_real, sizeof(gb->rtc_real)); @@ -693,8 +696,8 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) 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; + if (gb->cartridge_type->has_rtc && gb->mbc3_rtc_mapped && gb->mbc_ram_bank <= 4) { + gb->rtc_latched.data[gb->mbc_ram_bank] = gb->rtc_real.data[gb->mbc_ram_bank] = value; return; } From 289853445f815d2e8eae448dc6a69e73ba494ef7 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 4 Aug 2020 21:32:33 +0300 Subject: [PATCH 11/98] Fix long loading times when loading save states with bad local RTC times --- Core/timing.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Core/timing.c b/Core/timing.c index 17983bc..1633dc8 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -291,8 +291,20 @@ void GB_rtc_run(GB_gameboy_t *gb) } return; } + if ((gb->rtc_real.high & 0x40) == 0) { /* is timer running? */ time_t current_time = time(NULL); + + while (gb->last_rtc_second + 60 * 60 * 24 < current_time) { + gb->last_rtc_second += 60 * 60 + 24; + 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; + } + } + while (gb->last_rtc_second < current_time) { gb->last_rtc_second++; if (++gb->rtc_real.seconds == 60) { From d3664d5da0eaab1f6f564466351fe4c8a5d7fa8e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 5 Aug 2020 01:39:19 +0300 Subject: [PATCH 12/98] Fix more RTC regressions --- Core/mbc.c | 1 - Core/memory.c | 10 +++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Core/mbc.c b/Core/mbc.c index ba5055f..2259681 100644 --- a/Core/mbc.c +++ b/Core/mbc.c @@ -88,7 +88,6 @@ void GB_update_mbc_mappings(GB_gameboy_t *gb) 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; diff --git a/Core/memory.c b/Core/memory.c index c44c4ec..a75c382 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -183,7 +183,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) } } - if ((!gb->mbc_ram_enable || !gb->mbc_ram_size) && + if ((!gb->mbc_ram_enable) && gb->cartridge_type->mbc_subtype != GB_CAMERA && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) { @@ -205,7 +205,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) return GB_camera_read_register(gb, addr); } - if (!gb->mbc_ram) { + if (!gb->mbc_ram || gb->mbc_ram_size) { return 0xFF; } @@ -213,7 +213,11 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) return GB_camera_read_image(gb, addr - 0xa100); } - uint8_t ret = gb->mbc_ram[((addr & 0x1FFF) + gb->mbc_ram_bank * 0x2000) & (gb->mbc_ram_size - 1)]; + uint8_t effective_bank = gb->mbc_ram_bank; + if (gb->is_mbc30) { + effective_bank &= 0x3; + } + uint8_t ret = gb->mbc_ram[((addr & 0x1FFF) + effective_bank * 0x2000) & (gb->mbc_ram_size - 1)]; if (gb->cartridge_type->mbc_type == GB_MBC2) { ret |= 0xF0; } From 3f97b8eaa8da22711e291b1c5100e7469aaebb9e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 5 Aug 2020 02:10:21 +0300 Subject: [PATCH 13/98] Even more regressions --- Core/memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index a75c382..e1ebedd 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -684,7 +684,7 @@ 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) + if ((!gb->mbc_ram_enable) && gb->cartridge_type->mbc_type != GB_HUC1) return; if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) { @@ -705,7 +705,7 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) return; } - if (!gb->mbc_ram) { + if (!gb->mbc_ram || !gb->mbc_ram_size) { return; } From 5b2eec214ba7018a50482b95434395868fc5649e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 5 Aug 2020 21:17:22 +0300 Subject: [PATCH 14/98] Update version to 0.13.4 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index aaa47b0..b3f23cd 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13.3 +VERSION := 0.13.4 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From 012b9a2ba0414791f6bef962b748f7a019eb1338 Mon Sep 17 00:00:00 2001 From: Nadia Holmquist Pedersen Date: Thu, 6 Aug 2020 03:08:19 +0200 Subject: [PATCH 15/98] SDL: Make default window scale configurable --- SDL/gui.c | 35 ++++++++++++++++++++++++++++ SDL/gui.h | 3 +++ SDL/main.c | 68 +++++++++++++++++++++++++++++------------------------- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 81a9e42..57e232d 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -170,6 +170,10 @@ void update_viewport(void) } } +void rescale_window() { + SDL_SetWindowSize(window, GB_get_screen_width(&gb) * configuration.default_scale, GB_get_screen_height(&gb) * configuration.default_scale); +} + /* Does NOT check for bounds! */ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color) { @@ -435,6 +439,12 @@ const char *current_scaling_mode(unsigned index) [configuration.scaling_mode]; } +const char *current_default_scale(unsigned index) +{ + return (const char *[]){"1x", "2x", "3x", "4x", "5x", "6x", "7x", "8x"} + [configuration.default_scale - 1]; +} + const char *current_color_correction_mode(unsigned index) { return (const char *[]){"Disabled", "Correct Color Curves", "Emulate Hardware", "Preserve Brightness", "Reduce Contrast"} @@ -475,6 +485,30 @@ void cycle_scaling_backwards(unsigned index) render_texture(NULL, NULL); } +void cycle_default_scale(unsigned index) +{ + if (configuration.default_scale == GB_SDL_DEFAULT_SCALE_MAX) { + configuration.default_scale = 1; + } else { + configuration.default_scale++; + } + + rescale_window(); + update_viewport(); +} + +void cycle_default_scale_backwards(unsigned index) +{ + if (configuration.default_scale == 1) { + configuration.default_scale = GB_SDL_DEFAULT_SCALE_MAX; + } else { + configuration.default_scale--; + } + + rescale_window(); + update_viewport(); +} + static void cycle_color_correction(unsigned index) { if (configuration.color_correction_mode == GB_COLOR_CORRECTION_REDUCE_CONTRAST) { @@ -643,6 +677,7 @@ const char *blending_mode_string(unsigned index) static const struct menu_item graphics_menu[] = { {"Scaling Mode:", cycle_scaling, current_scaling_mode, cycle_scaling_backwards}, + {"Default Window Scale:", cycle_default_scale, current_default_scale, cycle_default_scale_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}, diff --git a/SDL/gui.h b/SDL/gui.h index af7543b..c950907 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -41,6 +41,7 @@ enum pending_command { GB_SDL_QUIT_COMMAND, }; +#define GB_SDL_DEFAULT_SCALE_MAX 8 extern enum pending_command pending_command; extern unsigned command_parameter; @@ -107,6 +108,8 @@ typedef struct { GB_border_mode_t border_mode; uint8_t volume; GB_rumble_mode_t rumble_mode; + + uint8_t default_scale; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 3df369f..f17867e 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -624,12 +624,48 @@ int main(int argc, char **argv) SDL_Init(SDL_INIT_EVERYTHING); + 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) { + 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.default_scale %= GB_SDL_DEFAULT_SCALE_MAX + 1; + 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; + } + + if (configuration.default_scale == 0) { + configuration.default_scale = 2; + } + + atexit(save_configuration); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + printf("scale %d\n", configuration.default_scale); window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 160 * 2, 144 * 2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); + 160 * configuration.default_scale, 144 * configuration.default_scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); SDL_SetWindowMinimumSize(window, 160, 144); if (fullscreen) { @@ -660,36 +696,6 @@ int main(int argc, char **argv) SDL_EventState(SDL_DROPFILE, SDL_ENABLE); - 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) { - 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; - } - - atexit(save_configuration); - if (!init_shader_with_name(&shader, configuration.filter)) { init_shader_with_name(&shader, "NearestNeighbor"); } From bce4bfba61f169b6b320b5b33aef838ef284badc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 6 Aug 2020 19:33:33 +0300 Subject: [PATCH 16/98] Fix major battery save regressions introduced by the last release, fixes #282 --- Core/memory.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index e1ebedd..f73209e 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -205,7 +205,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) return GB_camera_read_register(gb, addr); } - if (!gb->mbc_ram || gb->mbc_ram_size) { + if (!gb->mbc_ram || !gb->mbc_ram_size) { return 0xFF; } @@ -214,7 +214,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) } uint8_t effective_bank = gb->mbc_ram_bank; - if (gb->is_mbc30) { + if (gb->cartridge_type->mbc_type == GB_MBC3 && !gb->is_mbc30) { effective_bank &= 0x3; } uint8_t ret = gb->mbc_ram[((addr & 0x1FFF) + effective_bank * 0x2000) & (gb->mbc_ram_size - 1)]; @@ -708,8 +708,13 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (!gb->mbc_ram || !gb->mbc_ram_size) { return; } + + uint8_t effective_bank = gb->mbc_ram_bank; + if (gb->cartridge_type->mbc_type == GB_MBC3 && !gb->is_mbc30) { + effective_bank &= 0x3; + } - gb->mbc_ram[((addr & 0x1FFF) + gb->mbc_ram_bank * 0x2000) & (gb->mbc_ram_size - 1)] = value; + gb->mbc_ram[((addr & 0x1FFF) + effective_bank * 0x2000) & (gb->mbc_ram_size - 1)] = value; } static void write_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) From 766529d7bec1108815563527b81d1c355bfd2757 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 6 Aug 2020 21:01:55 +0300 Subject: [PATCH 17/98] Update version to 0.13.5 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b3f23cd..2acfe23 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13.4 +VERSION := 0.13.5 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From cc37632714a9ff7fe718a60e8a189e6a84a3b5b9 Mon Sep 17 00:00:00 2001 From: Nadia Holmquist Pedersen Date: Wed, 19 Aug 2020 06:15:36 +0200 Subject: [PATCH 18/98] Remove printf --- SDL/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/SDL/main.c b/SDL/main.c index f17867e..e79d0b3 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -663,7 +663,6 @@ int main(int argc, char **argv) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - printf("scale %d\n", configuration.default_scale); window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 160 * configuration.default_scale, 144 * configuration.default_scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); SDL_SetWindowMinimumSize(window, 160, 144); From 5cffdbcd278b0839b5f1369092c606462ff3de75 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 00:56:12 +0300 Subject: [PATCH 19/98] Prevent asking for notification permissions until used by an HuC-3 game --- Cocoa/AppDelegate.m | 4 +++- Cocoa/Document.m | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index e54012f..133fab7 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -51,7 +51,9 @@ JOYHatsEmulateButtonsKey: @YES, }]; - [NSUserNotificationCenter defaultUserNotificationCenter].delegate = self; + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBNotificationsUsed"]) { + [NSUserNotificationCenter defaultUserNotificationCenter].delegate = self; + } } - (IBAction)toggleDeveloperMode:(id)sender diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ece8092..c7c1e35 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -404,6 +404,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) unsigned time_to_alarm = GB_time_to_alarm(&gb); if (time_to_alarm) { + [NSUserNotificationCenter defaultUserNotificationCenter].delegate = (id)[NSApp delegate]; NSUserNotification *notification = [[NSUserNotification alloc] init]; NSString *friendlyName = [[self.fileName lastPathComponent] stringByDeletingPathExtension]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([^)]+\\)|\\[[^\\]]+\\]" options:0 error:nil]; From c2410a4ffc41388fd71aee6d6e308ff269ce9829 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 03:27:57 +0300 Subject: [PATCH 20/98] Update UI for Big Sur --- Cocoa/BigSurToolbar.h | 30 ++++++++++++++++++++++++++++++ Cocoa/Document.m | 12 ++++++++++++ Cocoa/Document.xib | 10 ++++------ Cocoa/GBPreferencesWindow.m | 6 ++++++ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 Cocoa/BigSurToolbar.h diff --git a/Cocoa/BigSurToolbar.h b/Cocoa/BigSurToolbar.h new file mode 100644 index 0000000..ea8b370 --- /dev/null +++ b/Cocoa/BigSurToolbar.h @@ -0,0 +1,30 @@ +#import +#ifndef BigSurToolbar_h +#define BigSurToolbar_h + +/* Backport the toolbarStyle property to allow compilation with older SDKs*/ +#ifndef __MAC_10_16 +typedef NS_ENUM(NSInteger, NSWindowToolbarStyle) { + // The default value. The style will be determined by the window's given configuration + NSWindowToolbarStyleAutomatic, + // The toolbar will appear below the window title + NSWindowToolbarStyleExpanded, + // The toolbar will appear below the window title and the items in the toolbar will attempt to have equal widths when possible + NSWindowToolbarStylePreference, + // The window title will appear inline with the toolbar when visible + NSWindowToolbarStyleUnified, + // Same as NSWindowToolbarStyleUnified, but with reduced margins in the toolbar allowing more focus to be on the contents of the window + NSWindowToolbarStyleUnifiedCompact +} API_AVAILABLE(macos(11.0)); + +@interface NSWindow (toolbarStyle) +@property NSWindowToolbarStyle toolbarStyle API_AVAILABLE(macos(11.0)); +@end + +@interface NSImage (SFSymbols) ++ (instancetype)imageWithSystemSymbolName:(NSString *)symbolName accessibilityDescription:(NSString *)description API_AVAILABLE(macos(11.0)); +@end + +#endif + +#endif diff --git a/Cocoa/Document.m b/Cocoa/Document.m index c7c1e35..2c11833 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -9,6 +9,7 @@ #include "GBWarningPopover.h" #include "GBCheatWindowController.h" #include "GBTerminalTextFieldCell.h" +#include "BigSurToolbar.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!!! */ @@ -569,6 +570,17 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) self.consoleWindow.title = [NSString stringWithFormat:@"Debug Console – %@", [[self.fileURL path] lastPathComponent]]; self.debuggerSplitView.dividerColor = [NSColor clearColor]; + if (@available(macOS 11.0, *)) { + self.memoryWindow.toolbarStyle = NSWindowToolbarStyleExpanded; + self.printerFeedWindow.toolbarStyle = NSWindowToolbarStyleUnifiedCompact; + [self.printerFeedWindow.toolbar removeItemAtIndex:1]; + self.printerFeedWindow.toolbar.items.firstObject.image = + [NSImage imageWithSystemSymbolName:@"square.and.arrow.down" + accessibilityDescription:@"Save"]; + self.printerFeedWindow.toolbar.items.lastObject.image = + [NSImage imageWithSystemSymbolName:@"printer" + accessibilityDescription:@"Print"]; + } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateHighpassFilter) diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index 1197c0f..d02f5bd 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -115,7 +115,7 @@ - + @@ -152,7 +152,7 @@ - + @@ -186,7 +186,7 @@ - + @@ -800,13 +800,11 @@ - - - + diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index aa219d8..491f0c0 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -1,6 +1,7 @@ #import "GBPreferencesWindow.h" #import "NSString+StringForKey.h" #import "GBButtons.h" +#import "BigSurToolbar.h" #import @implementation GBPreferencesWindow @@ -52,6 +53,11 @@ return filters; } +- (NSWindowToolbarStyle)toolbarStyle +{ + return NSWindowToolbarStylePreference; +} + - (void)close { joystick_configuration_state = -1; From 832dc127a4c81ce1fe79bc0c44415317ea9ae295 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 14:02:41 +0300 Subject: [PATCH 21/98] Fix Quick Look preview on Big Sur --- QuickLook/generator.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/QuickLook/generator.m b/QuickLook/generator.m index 1b2de66..92bb6ac 100644 --- a/QuickLook/generator.m +++ b/QuickLook/generator.m @@ -105,8 +105,9 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) { + extern NSString *kQLThumbnailPropertyIconFlavorKey; @autoreleasepool { - CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, ((NSSize){1024, 1024}), true, (__bridge CFDictionaryRef)(@{@"IconFlavor" : @(0)})); + CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, ((NSSize){1024, 1024}), true, (__bridge CFDictionaryRef)(@{kQLThumbnailPropertyIconFlavorKey : @(0)})); if (render(cgContext, url, true) == noErr) { QLThumbnailRequestFlushContext(thumbnail, cgContext); CGContextRelease(cgContext); From dab1c1bcfa0794b1db874ceb7f55ef605b51f0f2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 14:10:02 +0300 Subject: [PATCH 22/98] Fix RTC drift --- Core/timing.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/timing.c b/Core/timing.c index 1633dc8..965ba27 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -296,7 +296,7 @@ void GB_rtc_run(GB_gameboy_t *gb) time_t current_time = time(NULL); while (gb->last_rtc_second + 60 * 60 * 24 < current_time) { - gb->last_rtc_second += 60 * 60 + 24; + gb->last_rtc_second += 60 * 60 * 24; if (++gb->rtc_real.days == 0) { if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ gb->rtc_real.high |= 0x80; /* Overflow bit */ From e307de80648606abb3b91b3ebcff284976935c46 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 14:16:07 +0300 Subject: [PATCH 23/98] Style fixes --- SDL/gui.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 57e232d..e158816 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -170,7 +170,8 @@ void update_viewport(void) } } -void rescale_window() { +static void rescale_window(void) +{ SDL_SetWindowSize(window, GB_get_screen_width(&gb) * configuration.default_scale, GB_get_screen_height(&gb) * configuration.default_scale); } @@ -489,7 +490,8 @@ void cycle_default_scale(unsigned index) { if (configuration.default_scale == GB_SDL_DEFAULT_SCALE_MAX) { configuration.default_scale = 1; - } else { + } + else { configuration.default_scale++; } @@ -501,7 +503,8 @@ void cycle_default_scale_backwards(unsigned index) { if (configuration.default_scale == 1) { configuration.default_scale = GB_SDL_DEFAULT_SCALE_MAX; - } else { + } + else { configuration.default_scale--; } From abce93640ce48be5596c7c78f6b69ba298974602 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 14:20:44 +0300 Subject: [PATCH 24/98] Set a default value in the struct (instead of just during sanitation) --- SDL/gui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL/gui.c b/SDL/gui.c index e158816..62656e8 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -109,6 +109,7 @@ configuration_t configuration = .model = MODEL_CGB, .volume = 100, .rumble_mode = GB_RUMBLE_ALL_GAMES, + .default_scale = 2, }; From 2e4a6380773b40180758ebffcf2fc6859164aad4 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 22 Aug 2020 14:21:54 +0300 Subject: [PATCH 25/98] Update version to 0.13.6 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2acfe23..ca8ac76 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13.5 +VERSION := 0.13.6 export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl From 9c50a992af6f1152bedd8b12be27c6ebe03cb425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20K=C4=85dzio=C5=82ka?= Date: Thu, 17 Sep 2020 19:56:31 +0200 Subject: [PATCH 26/98] pb12: check the return value of write --- BootROMs/pb12.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/BootROMs/pb12.c b/BootROMs/pb12.c index 3f6d5f8..3a72fab 100644 --- a/BootROMs/pb12.c +++ b/BootROMs/pb12.c @@ -4,6 +4,7 @@ #include #include #include +#include void opts(uint8_t byte, uint8_t *options) { @@ -13,6 +14,17 @@ void opts(uint8_t byte, uint8_t *options) *(options++) = byte & (byte >> 1); } +void write_all(int fd, const void *buf, size_t count) { + while (count) { + ssize_t written = write(fd, buf, count); + if (written < 0) { + err(1, "write"); + } + count -= written; + buf += written; + } +} + int main() { static uint8_t source[0x4000]; @@ -76,15 +88,15 @@ int main() if (bits >= 8) { uint8_t outctl = control >> (bits - 8); assert(outctl != 1); - write(STDOUT_FILENO, &outctl, 1); - write(STDOUT_FILENO, literals, literals_size); + write_all(STDOUT_FILENO, &outctl, 1); + write_all(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); + write_all(STDOUT_FILENO, &end_byte, 1); return 0; } From abea3888dbc527ece51ade3ced2485be8c30840e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 17 Sep 2020 23:18:16 +0300 Subject: [PATCH 27/98] Fix compilation under GCC 9 --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index ca8ac76..df0ba43 100644 --- a/Makefile +++ b/Makefile @@ -106,6 +106,11 @@ ifeq ($(shell $(CC) -x c -c $(NULL) -o $(NULL) -Werror -Wpartial-availability 2> WARNINGS += -Wpartial-availability endif +# GCC's implementation of this warning has false positives, so we skip it +ifneq ($(shell $(CC) --version 2>&1 | grep "gcc"), ) +WARNINGS += -no-maybe-uninitialized +endif + CFLAGS += $(WARNINGS) CFLAGS += -std=gnu11 -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES From faf91508e22494c49217943c90b13b30fcf699c0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 17 Sep 2020 23:25:56 +0300 Subject: [PATCH 28/98] Yes, I *do* mean -Wno-maybe-uninitialized! --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index df0ba43..be52287 100644 --- a/Makefile +++ b/Makefile @@ -108,7 +108,7 @@ endif # GCC's implementation of this warning has false positives, so we skip it ifneq ($(shell $(CC) --version 2>&1 | grep "gcc"), ) -WARNINGS += -no-maybe-uninitialized +WARNINGS += -Wno-maybe-uninitialized endif CFLAGS += $(WARNINGS) From e35c22d4059a70c2d79409adc0b3224d121b66bf Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 17 Sep 2020 23:47:35 +0300 Subject: [PATCH 29/98] Fix a potential single byte overflow --- Core/debugger.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/debugger.c b/Core/debugger.c index e013076..002d455 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -132,7 +132,7 @@ static const char *value_to_string(GB_gameboy_t *gb, uint16_t value, bool prefer } /* Avoid overflow */ - if (symbol && strlen(symbol->name) > 240) { + if (symbol && strlen(symbol->name) >= 240) { symbol = NULL; } @@ -172,7 +172,7 @@ static const char *debugger_value_to_string(GB_gameboy_t *gb, value_t value, boo } /* Avoid overflow */ - if (symbol && strlen(symbol->name) > 240) { + if (symbol && strlen(symbol->name) >= 240) { symbol = NULL; } From 7ff3556bc3af05bdc5ca3e469fc479da2ff21bba Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 19 Sep 2020 19:31:24 +0300 Subject: [PATCH 30/98] Workboy emulation (Cocoa only) --- Cocoa/Document.m | 23 +++++- Cocoa/GBView.m | 115 ++++++++++++++++++++++++++++++ Cocoa/MainMenu.xib | 6 ++ Core/gb.c | 3 +- Core/gb.h | 5 ++ Core/printer.c | 14 ++-- Core/printer.h | 4 +- Core/workboy.c | 169 +++++++++++++++++++++++++++++++++++++++++++++ Core/workboy.h | 118 +++++++++++++++++++++++++++++++ 9 files changed, 446 insertions(+), 11 deletions(-) create mode 100644 Core/workboy.c create mode 100644 Core/workboy.h diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 2c11833..653179d 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -47,7 +47,7 @@ enum model { bool oamUpdating; NSMutableData *currentPrinterImageData; - enum {GBAccessoryNone, GBAccessoryPrinter} accessory; + enum {GBAccessoryNone, GBAccessoryPrinter, GBAccessoryWorkboy} accessory; bool rom_warning_issued; @@ -138,6 +138,16 @@ 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 setWorkboyTime(GB_gameboy_t *gb, time_t t) +{ + [[NSUserDefaults standardUserDefaults] setInteger:time(NULL) - t forKey:@"GBWorkboyTimeOffset"]; +} + +static time_t getWorkboyTime(GB_gameboy_t *gb) +{ + return time(NULL) - [[NSUserDefaults standardUserDefaults] integerForKey:@"GBWorkboyTimeOffset"]; +} + static void audioCallback(GB_gameboy_t *gb, GB_sample_t *sample) { Document *self = (__bridge Document *)GB_get_user_data(gb); @@ -791,6 +801,9 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) else if ([anItem action] == @selector(connectPrinter:)) { [(NSMenuItem*)anItem setState:accessory == GBAccessoryPrinter]; } + else if ([anItem action] == @selector(connectWorkboy:)) { + [(NSMenuItem*)anItem setState:accessory == GBAccessoryWorkboy]; + } else if ([anItem action] == @selector(toggleCheats:)) { [(NSMenuItem*)anItem setState:GB_cheats_enabled(&gb)]; } @@ -1701,6 +1714,14 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) }]; } +- (IBAction)connectWorkboy:(id)sender +{ + [self performAtomicBlock:^{ + accessory = GBAccessoryWorkboy; + GB_connect_workboy(&gb, setWorkboyTime, getWorkboyTime); + }]; +} + - (void) updateHighpassFilter { if (GB_is_inited(&gb)) { diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index e5cb7c8..6854ba7 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -1,4 +1,5 @@ #import +#import #import "GBView.h" #import "GBViewGL.h" #import "GBViewMetal.h" @@ -8,6 +9,98 @@ #define JOYSTICK_HIGH 0x4000 #define JOYSTICK_LOW 0x3800 +static const uint8_t workboy_ascii_to_key[] = { + ['0'] = GB_WORKBOY_0, + ['`'] = GB_WORKBOY_UMLAUT, + ['1'] = GB_WORKBOY_1, + ['2'] = GB_WORKBOY_2, + ['3'] = GB_WORKBOY_3, + ['4'] = GB_WORKBOY_4, + ['5'] = GB_WORKBOY_5, + ['6'] = GB_WORKBOY_6, + ['7'] = GB_WORKBOY_7, + ['8'] = GB_WORKBOY_8, + ['9'] = GB_WORKBOY_9, + + ['\r'] = GB_WORKBOY_ENTER, + [3] = GB_WORKBOY_ENTER, + + ['!'] = GB_WORKBOY_EXCLAMATION_MARK, + ['$'] = GB_WORKBOY_DOLLAR, + ['#'] = GB_WORKBOY_HASH, + ['~'] = GB_WORKBOY_TILDE, + ['*'] = GB_WORKBOY_ASTERISK, + ['+'] = GB_WORKBOY_PLUS, + ['-'] = GB_WORKBOY_MINUS, + ['('] = GB_WORKBOY_LEFT_PARENTHESIS, + [')'] = GB_WORKBOY_RIGHT_PARENTHESIS, + [';'] = GB_WORKBOY_SEMICOLON, + [':'] = GB_WORKBOY_COLON, + ['%'] = GB_WORKBOY_PERCENT, + ['='] = GB_WORKBOY_EQUAL, + [','] = GB_WORKBOY_COMMA, + ['<'] = GB_WORKBOY_LT, + ['.'] = GB_WORKBOY_DOT, + ['>'] = GB_WORKBOY_GT, + ['/'] = GB_WORKBOY_SLASH, + ['?'] = GB_WORKBOY_QUESTION_MARK, + [' '] = GB_WORKBOY_SPACE, + ['\''] = GB_WORKBOY_QUOTE, + ['@'] = GB_WORKBOY_AT, + + ['q'] = GB_WORKBOY_Q, + ['w'] = GB_WORKBOY_W, + ['e'] = GB_WORKBOY_E, + ['r'] = GB_WORKBOY_R, + ['t'] = GB_WORKBOY_T, + ['y'] = GB_WORKBOY_Y, + ['u'] = GB_WORKBOY_U, + ['i'] = GB_WORKBOY_I, + ['o'] = GB_WORKBOY_O, + ['p'] = GB_WORKBOY_P, + ['a'] = GB_WORKBOY_A, + ['s'] = GB_WORKBOY_S, + ['d'] = GB_WORKBOY_D, + ['f'] = GB_WORKBOY_F, + ['g'] = GB_WORKBOY_G, + ['h'] = GB_WORKBOY_H, + ['j'] = GB_WORKBOY_J, + ['k'] = GB_WORKBOY_K, + ['l'] = GB_WORKBOY_L, + ['z'] = GB_WORKBOY_Z, + ['x'] = GB_WORKBOY_X, + ['c'] = GB_WORKBOY_C, + ['v'] = GB_WORKBOY_V, + ['b'] = GB_WORKBOY_B, + ['n'] = GB_WORKBOY_N, + ['m'] = GB_WORKBOY_M, +}; + +static const uint8_t workboy_vk_to_key[] = { + [kVK_F1] = GB_WORKBOY_CLOCK, + [kVK_F2] = GB_WORKBOY_TEMPERATURE, + [kVK_F3] = GB_WORKBOY_MONEY, + [kVK_F4] = GB_WORKBOY_CALCULATOR, + [kVK_F5] = GB_WORKBOY_DATE, + [kVK_F6] = GB_WORKBOY_CONVERSION, + [kVK_F7] = GB_WORKBOY_RECORD, + [kVK_F8] = GB_WORKBOY_WORLD, + [kVK_F9] = GB_WORKBOY_PHONE, + [kVK_F10] = GB_WORKBOY_UNKNOWN, + [kVK_Delete] = GB_WORKBOY_BACKSPACE, + [kVK_Shift] = GB_WORKBOY_SHIFT_DOWN, + [kVK_RightShift] = GB_WORKBOY_SHIFT_DOWN, + [kVK_UpArrow] = GB_WORKBOY_UP, + [kVK_DownArrow] = GB_WORKBOY_DOWN, + [kVK_LeftArrow] = GB_WORKBOY_LEFT, + [kVK_RightArrow] = GB_WORKBOY_RIGHT, + [kVK_Escape] = GB_WORKBOY_ESCAPE, + [kVK_ANSI_KeypadDecimal] = GB_WORKBOY_DECIMAL_POINT, + [kVK_ANSI_KeypadClear] = GB_WORKBOY_M, + [kVK_ANSI_KeypadMultiply] = GB_WORKBOY_H, + [kVK_ANSI_KeypadDivide] = GB_WORKBOY_J, +}; + @implementation GBView { uint32_t *image_buffers[3]; @@ -188,7 +281,20 @@ -(void)keyDown:(NSEvent *)theEvent { + if ([theEvent type] != NSEventTypeFlagsChanged && theEvent.isARepeat) return; unsigned short keyCode = theEvent.keyCode; + if (GB_workboy_is_enabled(_gb)) { + if (theEvent.keyCode < sizeof(workboy_vk_to_key) && workboy_vk_to_key[theEvent.keyCode]) { + GB_workboy_set_key(_gb, workboy_vk_to_key[theEvent.keyCode]); + return; + } + unichar c = [theEvent type] != NSEventTypeFlagsChanged? [theEvent.charactersIgnoringModifiers.lowercaseString characterAtIndex:0] : 0; + if (c < sizeof(workboy_ascii_to_key) && workboy_ascii_to_key[c]) { + GB_workboy_set_key(_gb, workboy_ascii_to_key[c]); + return; + } + } + bool handled = false; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; @@ -232,6 +338,15 @@ -(void)keyUp:(NSEvent *)theEvent { unsigned short keyCode = theEvent.keyCode; + if (GB_workboy_is_enabled(_gb)) { + if (keyCode == kVK_Shift || keyCode == kVK_RightShift) { + GB_workboy_set_key(_gb, GB_WORKBOY_SHIFT_UP); + } + else { + GB_workboy_set_key(_gb, GB_WORKBOY_NONE); + } + + } bool handled = false; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index 71add1c..586d872 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -379,6 +379,12 @@ + + + + + + diff --git a/Core/gb.c b/Core/gb.c index 1bc2235..7325d79 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -1131,8 +1131,9 @@ void GB_disconnect_serial(GB_gameboy_t *gb) gb->serial_transfer_bit_start_callback = NULL; gb->serial_transfer_bit_end_callback = NULL; - /* Reset any internally-emulated device. Currently, only the printer. */ + /* Reset any internally-emulated device. */ memset(&gb->printer, 0, sizeof(gb->printer)); + memset(&gb->workboy, 0, sizeof(gb->workboy)); } bool GB_is_inited(GB_gameboy_t *gb) diff --git a/Core/gb.h b/Core/gb.h index f085eac..9043936 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -23,6 +23,7 @@ #include "sgb.h" #include "cheats.h" #include "rumble.h" +#include "workboy.h" #define GB_STRUCT_VERSION 13 @@ -372,6 +373,7 @@ struct GB_gameboy_internal_s { GB_printer_t printer; uint8_t extra_oam[0xff00 - 0xfea0]; uint32_t ram_size; // Different between CGB and DMG + GB_workboy_t workboy; ); /* DMA and HDMA */ @@ -608,6 +610,9 @@ struct GB_gameboy_internal_s { GB_read_memory_callback_t read_memory_callback; GB_boot_rom_load_callback_t boot_rom_load_callback; GB_print_image_callback_t printer_callback; + GB_workboy_set_time_callback workboy_set_time_callback; + GB_workboy_get_time_callback workboy_get_time_callback; + /* IR */ uint64_t cycles_since_ir_change; // In 8MHz units uint64_t cycles_since_input_ir_change; // In 8MHz units diff --git a/Core/printer.c b/Core/printer.c index 7b47ace..f04e54d 100644 --- a/Core/printer.c +++ b/Core/printer.c @@ -189,13 +189,13 @@ static void byte_reieve_completed(GB_gameboy_t *gb, uint8_t byte_received) static void serial_start(GB_gameboy_t *gb, bool bit_received) { - gb->printer.byte_being_recieved <<= 1; - gb->printer.byte_being_recieved |= bit_received; - gb->printer.bits_recieved++; - if (gb->printer.bits_recieved == 8) { - byte_reieve_completed(gb, gb->printer.byte_being_recieved); - gb->printer.bits_recieved = 0; - gb->printer.byte_being_recieved = 0; + gb->printer.byte_being_received <<= 1; + gb->printer.byte_being_received |= bit_received; + gb->printer.bits_received++; + if (gb->printer.bits_received == 8) { + byte_reieve_completed(gb, gb->printer.byte_being_received); + gb->printer.bits_received = 0; + gb->printer.byte_being_received = 0; } } diff --git a/Core/printer.h b/Core/printer.h index 71f919a..b29650f 100644 --- a/Core/printer.h +++ b/Core/printer.h @@ -54,8 +54,8 @@ typedef struct uint8_t compression_run_lenth; bool compression_run_is_compressed; - uint8_t bits_recieved; - uint8_t byte_being_recieved; + uint8_t bits_received; + uint8_t byte_being_received; bool bit_to_send; } GB_printer_t; diff --git a/Core/workboy.c b/Core/workboy.c new file mode 100644 index 0000000..3b10379 --- /dev/null +++ b/Core/workboy.c @@ -0,0 +1,169 @@ +#include "gb.h" +#include + +static inline uint8_t int_to_bcd(uint8_t i) +{ + return (i % 10) + ((i / 10) << 4); +} + +static inline uint8_t bcd_to_int(uint8_t i) +{ + return (i & 0xF) + (i >> 4) * 10; +} + +/* + Note: This peripheral was never released. This is a hacky software reimplementation of it that allows + reaccessing all of the features present in Workboy's ROM. Some of the implementation details are + obviously wrong, but without access to the actual hardware, this is the best I can do. +*/ + +static void serial_start(GB_gameboy_t *gb, bool bit_received) +{ + gb->workboy.byte_being_received <<= 1; + gb->workboy.byte_being_received |= bit_received; + gb->workboy.bits_received++; + if (gb->workboy.bits_received == 8) { + gb->workboy.byte_to_send = 0; + if (gb->workboy.mode != 'W' && gb->workboy.byte_being_received == 'R') { + gb->workboy.byte_to_send = 'D'; + gb->workboy.key = GB_WORKBOY_NONE; + gb->workboy.mode = gb->workboy.byte_being_received; + gb->workboy.buffer_index = 1; + + time_t time = gb->workboy_get_time_callback(gb); + struct tm tm; + tm = *localtime(&time); + memset(gb->workboy.buffer, 0, sizeof(gb->workboy.buffer)); + + gb->workboy.buffer[0] = 4; // Unknown, unused, but appears to be expected to be 4 + gb->workboy.buffer[2] = int_to_bcd(tm.tm_sec); // Seconds, BCD + gb->workboy.buffer[3] = int_to_bcd(tm.tm_min); // Minutes, BCD + gb->workboy.buffer[4] = int_to_bcd(tm.tm_hour); // Hours, BCD + gb->workboy.buffer[5] = int_to_bcd(tm.tm_mday); // Days, BCD. Upper most 2 bits are added to Year for some reason + gb->workboy.buffer[6] = int_to_bcd(tm.tm_mon + 1); // Months, BCD + gb->workboy.buffer[0xF] = tm.tm_year; // Years, plain number, since 1900 + + } + else if (gb->workboy.mode != 'W' && gb->workboy.byte_being_received == 'W') { + gb->workboy.byte_to_send = 'D'; // It is actually unknown what this value should be + gb->workboy.key = GB_WORKBOY_NONE; + gb->workboy.mode = gb->workboy.byte_being_received; + gb->workboy.buffer_index = 0; + } + else if (gb->workboy.mode != 'W' && (gb->workboy.byte_being_received == 'O' || gb->workboy.mode == 'O')) { + gb->workboy.mode = 'O'; + gb->workboy.byte_to_send = gb->workboy.key; + if (gb->workboy.key != GB_WORKBOY_NONE) { + if (gb->workboy.key & GB_WORKBOY_REQUIRE_SHIFT) { + gb->workboy.key &= ~GB_WORKBOY_REQUIRE_SHIFT; + if (gb->workboy.shift_down) { + gb->workboy.byte_to_send = gb->workboy.key; + gb->workboy.key = GB_WORKBOY_NONE; + } + else { + gb->workboy.byte_to_send = GB_WORKBOY_SHIFT_DOWN; + gb->workboy.shift_down = true; + } + } + else if (gb->workboy.key & GB_WORKBOY_FORBID_SHIFT) { + gb->workboy.key &= ~GB_WORKBOY_FORBID_SHIFT; + if (!gb->workboy.shift_down) { + gb->workboy.byte_to_send = gb->workboy.key; + gb->workboy.key = GB_WORKBOY_NONE; + } + else { + gb->workboy.byte_to_send = GB_WORKBOY_SHIFT_UP; + gb->workboy.shift_down = false; + } + } + else { + if (gb->workboy.key == GB_WORKBOY_SHIFT_DOWN) { + gb->workboy.shift_down = true; + gb->workboy.user_shift_down = true; + } + else if (gb->workboy.key == GB_WORKBOY_SHIFT_UP) { + gb->workboy.shift_down = false; + gb->workboy.user_shift_down = false; + } + gb->workboy.byte_to_send = gb->workboy.key; + gb->workboy.key = GB_WORKBOY_NONE; + } + } + } + else if (gb->workboy.mode == 'R') { + if (gb->workboy.buffer_index / 2 >= sizeof(gb->workboy.buffer)) { + gb->workboy.byte_to_send = 0; + } + else { + if (gb->workboy.buffer_index & 1) { + gb->workboy.byte_to_send = "0123456789ABCDEF"[gb->workboy.buffer[gb->workboy.buffer_index / 2] & 0xF]; + } + else { + gb->workboy.byte_to_send = "0123456789ABCDEF"[gb->workboy.buffer[gb->workboy.buffer_index / 2] >> 4]; + } + gb->workboy.buffer_index++; + } + } + else if (gb->workboy.mode == 'W') { + gb->workboy.byte_to_send = 'D'; + if (gb->workboy.buffer_index < 2) { + gb->workboy.buffer_index++; + } + else if ((gb->workboy.buffer_index - 2) < sizeof(gb->workboy.buffer)) { + gb->workboy.buffer[gb->workboy.buffer_index - 2] = gb->workboy.byte_being_received; + gb->workboy.buffer_index++; + if (gb->workboy.buffer_index - 2 == sizeof(gb->workboy.buffer)) { + struct tm tm = {0,}; + tm.tm_sec = bcd_to_int(gb->workboy.buffer[7]); + tm.tm_min = bcd_to_int(gb->workboy.buffer[8]); + tm.tm_hour = bcd_to_int(gb->workboy.buffer[9]); + tm.tm_mday = bcd_to_int(gb->workboy.buffer[0xA]); + tm.tm_mon = bcd_to_int(gb->workboy.buffer[0xB] & 0x3F) - 1; + tm.tm_year = (uint8_t)(gb->workboy.buffer[0x14] + (gb->workboy.buffer[0xA] >> 6)); // What were they thinking? + gb->workboy_set_time_callback(gb, mktime(&tm)); + gb->workboy.mode = 'O'; + } + } + } + gb->workboy.bits_received = 0; + gb->workboy.byte_being_received = 0; + } +} + +static bool serial_end(GB_gameboy_t *gb) +{ + bool ret = gb->workboy.bit_to_send; + gb->workboy.bit_to_send = gb->workboy.byte_to_send & 0x80; + gb->workboy.byte_to_send <<= 1; + return ret; +} + +void GB_connect_workboy(GB_gameboy_t *gb, + GB_workboy_set_time_callback set_time_callback, + GB_workboy_get_time_callback get_time_callback) +{ + memset(&gb->workboy, 0, sizeof(gb->workboy)); + GB_set_serial_transfer_bit_start_callback(gb, serial_start); + GB_set_serial_transfer_bit_end_callback(gb, serial_end); + gb->workboy_set_time_callback = set_time_callback; + gb->workboy_get_time_callback = get_time_callback; +} + +bool GB_workboy_is_enabled(GB_gameboy_t *gb) +{ + return gb->workboy.mode; +} + +void GB_workboy_set_key(GB_gameboy_t *gb, uint8_t key) +{ + if (gb->workboy.user_shift_down != gb->workboy.shift_down && + (key & (GB_WORKBOY_REQUIRE_SHIFT | GB_WORKBOY_FORBID_SHIFT)) == 0) { + if (gb->workboy.user_shift_down) { + key |= GB_WORKBOY_REQUIRE_SHIFT; + } + else { + key |= GB_WORKBOY_FORBID_SHIFT; + } + } + gb->workboy.key = key; +} diff --git a/Core/workboy.h b/Core/workboy.h new file mode 100644 index 0000000..d21f273 --- /dev/null +++ b/Core/workboy.h @@ -0,0 +1,118 @@ +#ifndef workboy_h +#define workboy_h +#include +#include +#include +#include "gb_struct_def.h" + + +typedef struct { + uint8_t byte_to_send; + bool bit_to_send; + uint8_t byte_being_received; + uint8_t bits_received; + uint8_t mode; + uint8_t key; + bool shift_down; + bool user_shift_down; + uint8_t buffer[0x15]; + uint8_t buffer_index; // In nibbles during read, in bytes during write +} GB_workboy_t; + +typedef void (*GB_workboy_set_time_callback)(GB_gameboy_t *gb, time_t time); +typedef time_t (*GB_workboy_get_time_callback)(GB_gameboy_t *gb); + +enum { + GB_WORKBOY_NONE = 0xFF, + GB_WORKBOY_REQUIRE_SHIFT = 0x40, + GB_WORKBOY_FORBID_SHIFT = 0x80, + + GB_WORKBOY_CLOCK = 1, + GB_WORKBOY_TEMPERATURE = 2, + GB_WORKBOY_MONEY = 3, + GB_WORKBOY_CALCULATOR = 4, + GB_WORKBOY_DATE = 5, + GB_WORKBOY_CONVERSION = 6, + GB_WORKBOY_RECORD = 7, + GB_WORKBOY_WORLD = 8, + GB_WORKBOY_PHONE = 9, + GB_WORKBOY_ESCAPE = 10, + GB_WORKBOY_BACKSPACE = 11, + GB_WORKBOY_UNKNOWN = 12, + GB_WORKBOY_LEFT = 13, + GB_WORKBOY_Q = 17, + GB_WORKBOY_1 = 17 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_W = 18, + GB_WORKBOY_2 = 18 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_E = 19, + GB_WORKBOY_3 = 19 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_R = 20, + GB_WORKBOY_T = 21, + GB_WORKBOY_Y = 22 , + GB_WORKBOY_U = 23 , + GB_WORKBOY_I = 24, + GB_WORKBOY_EXCLAMATION_MARK = 24 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_O = 25, + GB_WORKBOY_TILDE = 25 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_P = 26, + GB_WORKBOY_ASTERISK = 26 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_DOLLAR = 27 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_HASH = 27 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_A = 28, + GB_WORKBOY_4 = 28 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_S = 29, + GB_WORKBOY_5 = 29 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_D = 30, + GB_WORKBOY_6 = 30 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_F = 31, + GB_WORKBOY_PLUS = 31 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_G = 32, + GB_WORKBOY_MINUS = 32 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_H = 33, + GB_WORKBOY_J = 34, + GB_WORKBOY_K = 35, + GB_WORKBOY_LEFT_PARENTHESIS = 35 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_L = 36, + GB_WORKBOY_RIGHT_PARENTHESIS = 36 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_SEMICOLON = 37 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_COLON = 37, + GB_WORKBOY_ENTER = 38, + GB_WORKBOY_SHIFT_DOWN = 39, + GB_WORKBOY_Z = 40, + GB_WORKBOY_7 = 40 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_X = 41, + GB_WORKBOY_8 = 41 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_C = 42, + GB_WORKBOY_9 = 42 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_V = 43, + GB_WORKBOY_DECIMAL_POINT = 43 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_B = 44, + GB_WORKBOY_PERCENT = 44 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_N = 45, + GB_WORKBOY_EQUAL = 45 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_M = 46, + GB_WORKBOY_COMMA = 47 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_LT = 47 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_DOT = 48 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_GT = 48 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_SLASH = 49 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_QUESTION_MARK = 49 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_SHIFT_UP = 50, + GB_WORKBOY_0 = 51 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_UMLAUT = 51, + GB_WORKBOY_SPACE = 52, + GB_WORKBOY_QUOTE = 53 | GB_WORKBOY_FORBID_SHIFT, + GB_WORKBOY_AT = 53 | GB_WORKBOY_REQUIRE_SHIFT, + GB_WORKBOY_UP = 54, + GB_WORKBOY_DOWN = 55, + GB_WORKBOY_RIGHT = 56, +}; + + +void GB_connect_workboy(GB_gameboy_t *gb, + GB_workboy_set_time_callback set_time_callback, + GB_workboy_get_time_callback get_time_callback); +bool GB_workboy_is_enabled(GB_gameboy_t *gb); +void GB_workboy_set_key(GB_gameboy_t *gb, uint8_t key); + +#endif From 1e9e961e9ce793963c94583a48bc66401e5a7434 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 29 Sep 2020 20:43:47 +0300 Subject: [PATCH 31/98] Create CONTRIBUTING.md --- CONTRIBUTING.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..94627d1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,79 @@ +# SameBoy Coding and Contribution Guidelines + +## Issues + +GitHub Issues are the most effective way to report a bug or request a feature in SameBoy. When reporting a bug, make sure you use the latest stable release, and make sure you mention the SameBoy frontend (Cocoa, SDL, Libretro) and operating system you're using. If you're using Linux/BSD/etc, or you build your own copy of SameBoy for another reason, give as much details as possible on your environment. + +If your bug involves a crash, please attach a crash log or a core dump. If you're using Linux/BSD/etc, or if you're using the Libretro core, please attach the `sameboy` binary (or `libretro_sameboy` library) in that case. + +If your bug is a regression, it'd be extremely helpful if you can report the the first affected version. You get extra credits if you use `git bisect` to point the exact breaking commit. + +If your bug is an emulation bug (Such as a failing test ROM), and you have access to a Game Boy you can test on, please confirm SameBoy is indeed behaving differently from hardware, and report both the emulated model and revision in SameBoy, and the hardware revision you're testing on. + +If your issue is a feature request, demonstrating use cases can help me better prioritize it. + +## Pull Requests + +To allow quicker integration into SameBoy's master branch, contributors are asked to follow SameBoy's style and coding guidelines. Keep in mind that despite the seemingly strict guidelines, all pull requests are welcome – not following the guidelines does not mean your pull request will not be accepted, but it will require manual tweaks from my side for integrating. + +### Languages and Compilers + +SameBoy's core, SDL frontend, Libretro frontend, and automatic tester (Folders `Core`, `SDL` & `OpenDialog`, `libretro`, and `Tester`; respectively) are all written in C11. The Cocoa frontend, SameBoy's fork of Hex Fiend, JoyKit and the Quick Look previewer (Folders `Cocoa`, `HexFiend`, `JoyKit` and `QuickLook`; respectively) are all written in ARC-enabled Objective-C. The SameBoot ROMs (Under `BootROMs`) are written in rgbds-flavor SM83 assembly, with build tools in C11. The shaders (inside `Shaders`) are written in a polyglot GLSL and Metal style, with a few GLSL- and Metal-specific sources. The build system uses standalone Make, in the GNU flavor. Avoid adding new languages (C++, Swift, Python, CMake...) to any of the existing sub-projects. + +SameBoy's main target compiler is Clang, but GCC is also supported when targeting Linux and Libretro. Other compilers (e.g. MSVC) are not supported, and unless there's a good reason, there's no need to go out of your way to add specific support for them. Extensions that are supported by both compilers (Such as `typeof`) may be used if it makes sense. It's OK if you can't test one of these compilers yourself; once you push a commit, the CI bot will let you know if you broke something. + +### Third Party Libraries and Tools + +Avoid adding new required dependencies; run-time and compile-time dependencies alike. Most importantly, avoid linking against GPL licensed libraries (LGPL libraries are fine), so SameBoy can retain its MIT license. + +### Spacing, Indentation and Formatting + +In all files and languages (Other than Makefiles when required), 4 spaces are used for indentation. Unix line endings (`\n`) are used exclusively, even in Windows-specific source files. (`\r` and `\t` shouldn't appear in any source file). Opening braces belong on the same line as their control flow directive, and on their own line when following a function prototype. The `else` keyword always starts on its own line. The `case` keyword is indented relative to its `switch` block, and the code inside a `case` is indented relative to its label. A control flow keyword should have a space between it and the following `(`, commas should follow a space, and operator (except `.` and `->`) should be surrounded by spaces. + +Control flow statements must use `{}`, with the exception of `if` statements that only contain a single `break`, `continue`, or trivial `return` statements. If `{}`s are omitted, the statement must be on the same line as the `if` condition. Functions that do not have any argument must be specified as `(void)`, as mandated by the C standard. The `sizeof` and `typeof` operators should be used as if they're functions (With `()`). `*`, when used to declare pointer types (including functions that return pointers), and when used to dereference a pointer, is attached to the right side (The variable name) – not to the left, and not with spaces on both sides. + +No strict limitations on a line's maximum width, but use your best judgement if you think a statement would benefit from an additional line break. + +Well formatted code example: + +``` +static void my_function(void) +{ + GB_something_t *thing = GB_function(&gb, GB_FLAG_ONE | GB_FLAG_TWO, sizeof(thing)); + if (GB_is_thing(thing)) return; + + switch (*thing) { + case GB_QUACK: + // Something + case GB_DUCK: + // Something else + } +} +``` + +Badly formatted code example: +``` +static void my_function(){ + GB_something_t* thing=GB_function(&gb , GB_FLAG_ONE|GB_FLAG_TWO , sizeof thing); + if( GB_is_thing ( thing ) ) + return; + + switch(* thing) + { + case GB_QUACK: + // Something + case GB_DUCK: + // Something else + } +} +``` + +### Other Coding Conventions + +The primitive types to be used in SameBoy are `unsigned` and `signed` (Without the `int` keyword), the `(u)int*_t` types, `char *` for UTF-8 strings, `double` for non-integer numbers, and `bool` for booleans (Including in Objective-C code, avoid `BOOL`). As long as it's not mandated by a 3rd-party API (e.g. `int` when using file descriptors), avoid using other primitive types. Use `const` whenever possible. + +Most C names should be `lower_case_snake_case`. Constants and macros use `UPPER_CASE_SNAKE_CASE`. Type definitions use a `_t` suffix. Type definitions, as well as non-static (exported) core symbols, should be prefixed with `GB_` (SameBoy's core is intended to be used as a library, so it shouldn't contaminate the global namespace without prefixes). Exported symbols that are only meant to be used by other parts of the core should still get the `GB_` prefix, but their header definition should be inside `#ifdef GB_INTERNAL`. + +For Objective-C naming conventions, use Apple's conventions (Some old Objective-C code mixes these with the C naming convention; new code should use Apple's convention exclusively). The name prefix for SameBoy classes and constants is `GB`. JoyKit's prefix is `JOY`, and Hex Fiend's prefix is `HF`. + +In all languages, prefer long, unambiguous names over short ambiguous ones. From 2a5aed626da3cfe6d8e2adac9116e82a280e93c5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 29 Sep 2020 20:50:14 +0300 Subject: [PATCH 32/98] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 88b8caa..dcffabe 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ Features currently supported only with the Cocoa version: ## Compatibility SameBoy passes all of [blargg's test ROMs](http://gbdev.gg8.se/wiki/articles/Test_ROMs#Blargg.27s_tests), all of [mooneye-gb's](https://github.com/Gekkio/mooneye-gb) tests (Some tests require the original boot ROMs), and all of [Wilbert Pol's tests](https://github.com/wilbertpol/mooneye-gb/tree/master/tests/acceptance). SameBoy should work with most games and demos, please [report](https://github.com/LIJI32/SameBoy/issues/new) any broken ROM. The latest results for SameBoy's automatic tester are available [here](https://sameboy.github.io/automation/). +## Contributing +SameBoy is an open-source project licensed under the MIT license, and you're welcome to contribute by creating issues, implementing new features, improving emulation accuracy and fixing existing open issues. You can read the [contribution guidelines](CONTRIBUTING.md) to make sure your contributions are as effective as possible. + ## Compilation SameBoy requires the following tools and libraries to build: * clang From 04e5f1b8cf78313e68aeac07a46f75c8db0cdd09 Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 5 Oct 2020 14:33:36 -0700 Subject: [PATCH 33/98] Updated for Windows clang and SDL2 changes --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index be52287..78c28b6 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ endif ifeq ($(PLATFORM),windows32) _ := $(shell chcp 65001) EXESUFFIX:=.exe -NATIVE_CC = clang -IWindows -Wno-deprecated-declarations +NATIVE_CC = clang -IWindows -Wno-deprecated-declarations --target=i386-pc-windows else EXESUFFIX:= NATIVE_CC := cc @@ -129,8 +129,8 @@ 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 -luser32 -lSDL2main -Wl,/MANIFESTFILE:NUL +CFLAGS += -IWindows -Drandom=rand --target=i386-pc-windows +LDFLAGS += -lmsvcrt -lcomdlg32 -luser32 -lshell32 -lSDL2main -Wl,/MANIFESTFILE:NUL --target=i386-pc-windows SDL_LDFLAGS := -lSDL2 GL_LDFLAGS := -lopengl32 else @@ -404,7 +404,7 @@ $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRE $(realpath $(PB12_COMPRESS)) < $< > $@ $(PB12_COMPRESS): BootROMs/pb12.c - $(NATIVE_CC) -std=c99 -Wall -Werror $< -o $@ + $(NATIVE_CC) -std=c99 -Wall -Werror $< -o $@ --target=i386-pc-windows $(BIN)/BootROMs/agb_boot.bin: BootROMs/cgb_boot.asm $(BIN)/BootROMs/cgb_boot_fast.bin: BootROMs/cgb_boot.asm From 28234da2d29a35f381c3015e0a5eab38ccdf4623 Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 5 Oct 2020 14:34:00 -0700 Subject: [PATCH 34/98] Updated instructions for Windows building --- README.md | 4 ++-- build-faq.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dcffabe..5df1d5c 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,14 @@ SameBoy is an open-source project licensed under the MIT license, and you're wel SameBoy requires the following tools and libraries to build: * clang * make - * Cocoa port: OS X SDK and Xcode command line tools + * Cocoa port: OS X SDK and Xcode command line tools [OSX Only] * SDL port: libsdl2 * [rgbds](https://github.com/bentley/rgbds/releases/), for boot ROM compilation On Windows, SameBoy also requires: * Visual Studio (For headers, etc.) * [GnuWin](http://gnuwin32.sourceforge.net/) - * Running vcvars32 before running make. Make sure all required tools and libraries are in %PATH% and %lib%, respectively. + * Running vcvars32 before running make. Make sure all required tools and libraries are in %PATH% and %lib%, respectively. (see [Build FAQ] (https://github.com/LIJI32/SameBoy/blob/master/build-faq.md)) To compile, simply run `make`. The targets are `cocoa` (Default for macOS), `sdl` (Default for everything else), `libretro`, `bootroms` and `tester`. You may also specify `CONF=debug` (default), `CONF=release`, `CONF=native_release` or `CONF=fat_release` to control optimization, symbols and multi-architectures. `native_release` is faster than `release`, but is optimized to the host's CPU and therefore is not portable. `fat_release` is exclusive to macOS and builds x86-64 and ARM64 fat binaries; this requires using a recent enough `clang` and macOS SDK using `xcode-select`, or setting them explicitly with `CC=` and `SYSROOT=`, respectively. All other configurations will build to your host architecture. You may set `BOOTROMS_DIR=...` to a directory containing precompiled boot ROM files, otherwise the build system will compile and use SameBoy's own boot ROMs. diff --git a/build-faq.md b/build-faq.md index 2b056dd..481d1b9 100644 --- a/build-faq.md +++ b/build-faq.md @@ -4,4 +4,58 @@ 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 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 +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. + +# Windows build process + +For the various tools and libraries, follow the below guide to ensure easy, proper configuration for the build environment: + +#### clang + +This may be installed via a Visual Studio installer packages instead of built from source. + +#### SDL Port + +[libsdl2](https://libsdl.org/download-2.0.php) has two separate files that must be downloaded + 1. The `-x86` Runtime Binary (e.g., `SDL2-2.0.12-win32-x86.zip` (as of writing)) + 2. The Visual C++ Development Library (e.g., `SDL2-devel-2.0.12-VC.zip` (as of writing)) + +For the Runtime Binary, place the extracted `SDL2.dll` into a known folder for later. + +- `C:\SDL2\bin\SDL2.dll` will be used as an example + +For the Visual C++ Development Library, place the extracted files within a known folder for later. + +The following examples will be referenced later: + +- `C:\SDL2\lib\x86\*` +- `C:\SDL2\include\*` + +#### Gnuwin + +Ensure that this is in %PATH%. + +If errors arise (i.e., particularly with the `CREATE_PROCESS('usr/bin/mkdir')` calls, also verify that Git for Windows has not been installed with full Linux support. If it has, remove `C:\Program Files\Git\usr\bin` from the SYSTEM %PATH% until after compilation. + +### Building + +Within a command prompt in the project directory: + +``` +vcvars32 +set path=%path%;C:\SDL2\bin +set lib=%lib%;C:\SDL2\lib\x86 +set include=%include%;C:\SDL2\include +make +``` +Please note that these directories (`C:\SDL2\*`) are the examples given within the "SDL Port" section above. Ensure that your `path`, `lib`, and `include` paths are updated appropriately with the SDL2 downloads. + +#### Error -1073741819 + +If encountering an error that appears as follows: + +> make: *** [build/bin/BootROMs/dmg_boot.bin] Error -1073741819 + +Simply run `make` again, and the process will continue. This appears to happen occasionally with `build/bin/BootROMs/dmg_boot.bin` and `build/bin/BootROMs/sgb2_boot.bin`. It does not affect the compiled output. + + From 0b5853070aa71da85482c60a49100b309cc7a57d Mon Sep 17 00:00:00 2001 From: yo Date: Mon, 5 Oct 2020 14:37:49 -0700 Subject: [PATCH 35/98] Updated instructions for Windows building --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5df1d5c..1218fc1 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ SameBoy requires the following tools and libraries to build: On Windows, SameBoy also requires: * Visual Studio (For headers, etc.) * [GnuWin](http://gnuwin32.sourceforge.net/) - * Running vcvars32 before running make. Make sure all required tools and libraries are in %PATH% and %lib%, respectively. (see [Build FAQ] (https://github.com/LIJI32/SameBoy/blob/master/build-faq.md)) + * Running vcvars32 before running make. Make sure all required tools and libraries are in %PATH% and %lib%, respectively. (see [Build FAQ](https://github.com/LIJI32/SameBoy/blob/master/build-faq.md) for more details on Windows compilation) To compile, simply run `make`. The targets are `cocoa` (Default for macOS), `sdl` (Default for everything else), `libretro`, `bootroms` and `tester`. You may also specify `CONF=debug` (default), `CONF=release`, `CONF=native_release` or `CONF=fat_release` to control optimization, symbols and multi-architectures. `native_release` is faster than `release`, but is optimized to the host's CPU and therefore is not portable. `fat_release` is exclusive to macOS and builds x86-64 and ARM64 fat binaries; this requires using a recent enough `clang` and macOS SDK using `xcode-select`, or setting them explicitly with `CC=` and `SYSROOT=`, respectively. All other configurations will build to your host architecture. You may set `BOOTROMS_DIR=...` to a directory containing precompiled boot ROM files, otherwise the build system will compile and use SameBoy's own boot ROMs. From 38afb187cf7891724624f2f5199d163761e1b3a1 Mon Sep 17 00:00:00 2001 From: yo Date: Tue, 6 Oct 2020 23:03:39 -0700 Subject: [PATCH 36/98] Resolving some comments and clarifying some language --- Makefile | 2 +- build-faq.md | 27 +++++++++------------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 78c28b6..0881fe9 100644 --- a/Makefile +++ b/Makefile @@ -404,7 +404,7 @@ $(OBJ)/BootROMs/SameBoyLogo.pb12: $(OBJ)/BootROMs/SameBoyLogo.2bpp $(PB12_COMPRE $(realpath $(PB12_COMPRESS)) < $< > $@ $(PB12_COMPRESS): BootROMs/pb12.c - $(NATIVE_CC) -std=c99 -Wall -Werror $< -o $@ --target=i386-pc-windows + $(NATIVE_CC) -std=c99 -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/build-faq.md b/build-faq.md index 481d1b9..56c59ae 100644 --- a/build-faq.md +++ b/build-faq.md @@ -10,30 +10,22 @@ SameBoy on macOS expects you to have SDL2 installed via Brew, and not as a frame For the various tools and libraries, follow the below guide to ensure easy, proper configuration for the build environment: -#### clang - -This may be installed via a Visual Studio installer packages instead of built from source. - #### SDL Port -[libsdl2](https://libsdl.org/download-2.0.php) has two separate files that must be downloaded - 1. The `-x86` Runtime Binary (e.g., `SDL2-2.0.12-win32-x86.zip` (as of writing)) - 2. The Visual C++ Development Library (e.g., `SDL2-devel-2.0.12-VC.zip` (as of writing)) +For [libSDL2](https://libsdl.org/download-2.0.php), download the Visual C++ Development Library pack. Place the extracted files within a known folder for later. Both the `\x86\` and `\include\` paths will be needed. -For the Runtime Binary, place the extracted `SDL2.dll` into a known folder for later. - -- `C:\SDL2\bin\SDL2.dll` will be used as an example - -For the Visual C++ Development Library, place the extracted files within a known folder for later. - -The following examples will be referenced later: +The following examples will be referenced later: - `C:\SDL2\lib\x86\*` - `C:\SDL2\include\*` -#### Gnuwin +#### rgbds -Ensure that this is in %PATH%. +After downloading [rgbds](https://github.com/bentley/rgbds/releases/), ensure that it is added to the `%PATH%`. This may be done by adding it to the user's or SYSTEM's Environment Variables, or may be added to the command line at compilation time via `set path=%path%;C:\path\to\rgbds`. + +#### GnuWin + +Ensure that the `gnuwin32\bin\` directory is included in `%PATH%`. Like rgbds above, this may instead be manually included on the command line before installation: `set path=%path%;C:\path\to\gnuwin32\bin`. If errors arise (i.e., particularly with the `CREATE_PROCESS('usr/bin/mkdir')` calls, also verify that Git for Windows has not been installed with full Linux support. If it has, remove `C:\Program Files\Git\usr\bin` from the SYSTEM %PATH% until after compilation. @@ -43,12 +35,11 @@ Within a command prompt in the project directory: ``` vcvars32 -set path=%path%;C:\SDL2\bin set lib=%lib%;C:\SDL2\lib\x86 set include=%include%;C:\SDL2\include make ``` -Please note that these directories (`C:\SDL2\*`) are the examples given within the "SDL Port" section above. Ensure that your `path`, `lib`, and `include` paths are updated appropriately with the SDL2 downloads. +Please note that these directories (`C:\SDL2\*`) are the examples given within the "SDL Port" section above. Ensure that your `%PATH%` properly includes `rgbds` and `gnuwin32\bin`, and that the `lib` and `include` paths include the appropriate SDL2 directories. #### Error -1073741819 From 64963e1746f0a548d3412e115fd7fba934193226 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 9 Oct 2020 15:57:23 +0300 Subject: [PATCH 37/98] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1218fc1..97cd2e4 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ SameBoy is an open-source project licensed under the MIT license, and you're wel SameBoy requires the following tools and libraries to build: * clang * make - * Cocoa port: OS X SDK and Xcode command line tools [OSX Only] + * macOS Cocoa port: macOS SDK and Xcode (For command line tools and ibtool) * SDL port: libsdl2 * [rgbds](https://github.com/bentley/rgbds/releases/), for boot ROM compilation From 99ec5b32fc09700b20bade5e976242830770c4e3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 9 Oct 2020 16:03:32 +0300 Subject: [PATCH 38/98] Update build-faq.md --- build-faq.md | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/build-faq.md b/build-faq.md index 56c59ae..9def134 100644 --- a/build-faq.md +++ b/build-faq.md @@ -1,16 +1,19 @@ -# Attempting to build the Cocoa frontend fails with NSInternalInconsistencyException +# macOS Specific Issues +## Attempting to build the Cocoa frontend fails with NSInternalInconsistencyException When building on macOS, the build system will make a native Cocoa app by default. In this case, the build system uses the Xcode `ibtool` command to build user interface files. If this command fails, you can fix this issue by starting Xcode and letting it install components. After this is done, you should be able to close Xcode and build successfully. -# Attempting to build the SDL frontend on macOS fails on linking +## Attempting to build the SDL frontend on macOS fails on linking 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. -# Windows build process +# Windows Build Process + +## Tools and Libraries Installation For the various tools and libraries, follow the below guide to ensure easy, proper configuration for the build environment: -#### SDL Port +### SDL2 For [libSDL2](https://libsdl.org/download-2.0.php), download the Visual C++ Development Library pack. Place the extracted files within a known folder for later. Both the `\x86\` and `\include\` paths will be needed. @@ -19,17 +22,15 @@ The following examples will be referenced later: - `C:\SDL2\lib\x86\*` - `C:\SDL2\include\*` -#### rgbds +### rgbds After downloading [rgbds](https://github.com/bentley/rgbds/releases/), ensure that it is added to the `%PATH%`. This may be done by adding it to the user's or SYSTEM's Environment Variables, or may be added to the command line at compilation time via `set path=%path%;C:\path\to\rgbds`. -#### GnuWin +### GnuWin Ensure that the `gnuwin32\bin\` directory is included in `%PATH%`. Like rgbds above, this may instead be manually included on the command line before installation: `set path=%path%;C:\path\to\gnuwin32\bin`. -If errors arise (i.e., particularly with the `CREATE_PROCESS('usr/bin/mkdir')` calls, also verify that Git for Windows has not been installed with full Linux support. If it has, remove `C:\Program Files\Git\usr\bin` from the SYSTEM %PATH% until after compilation. - -### Building +## Building Within a command prompt in the project directory: @@ -41,12 +42,16 @@ make ``` Please note that these directories (`C:\SDL2\*`) are the examples given within the "SDL Port" section above. Ensure that your `%PATH%` properly includes `rgbds` and `gnuwin32\bin`, and that the `lib` and `include` paths include the appropriate SDL2 directories. -#### Error -1073741819 +## Common Errors + +### Error -1073741819 If encountering an error that appears as follows: -> make: *** [build/bin/BootROMs/dmg_boot.bin] Error -1073741819 +``` make: *** [build/bin/BootROMs/dmg_boot.bin] Error -1073741819``` -Simply run `make` again, and the process will continue. This appears to happen occasionally with `build/bin/BootROMs/dmg_boot.bin` and `build/bin/BootROMs/sgb2_boot.bin`. It does not affect the compiled output. +Simply run `make` again, and the process will continue. This appears to happen occasionally with `build/bin/BootROMs/dmg_boot.bin` and `build/bin/BootROMs/sgb2_boot.bin`. It does not affect the compiled output. This appears to be an issue with GnuWin. +### The system cannot find the file specified (`usr/bin/mkdir`) +If errors arise (i.e., particularly with the `CREATE_PROCESS('usr/bin/mkdir')` calls, also verify that Git for Windows has not been installed with full Linux support. If it has, remove `C:\Program Files\Git\usr\bin` from the SYSTEM %PATH% until after compilation. This happens because the Git for Windows version of `which` is used instead of the GnuWin one, and it returns a Unix-style path instead of a Windows one. From c35fe8b5179cf8022454714d1848abe6cbe2644d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 9 Oct 2020 16:39:23 +0300 Subject: [PATCH 39/98] Make `gb.h` compatible with C++ again for bsnes integration. Fixed #300 --- Core/save_state.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Core/save_state.h b/Core/save_state.h index fcb9135..8e5fc4e 100644 --- a/Core/save_state.h +++ b/Core/save_state.h @@ -5,10 +5,16 @@ #define GB_PADDING(type, old_usage) type old_usage##__do_not_use +#ifdef __cplusplus +/* For bsnes integration. C++ code does not need section information, and throws a fit over certain types such + as anonymous enums inside unions */ +#define GB_SECTION(name, ...) __attribute__ ((aligned (8))) __VA_ARGS__ +#else #define GB_SECTION(name, ...) __attribute__ ((aligned (8))) union {uint8_t name##_section_start; struct {__VA_ARGS__};}; uint8_t name##_section_end[0] #define GB_SECTION_OFFSET(name) (offsetof(GB_gameboy_t, name##_section_start)) #define GB_SECTION_SIZE(name) (offsetof(GB_gameboy_t, name##_section_end) - offsetof(GB_gameboy_t, name##_section_start)) #define GB_GET_SECTION(gb, name) ((void*)&((gb)->name##_section_start)) +#endif #define GB_aligned_double __attribute__ ((aligned (8))) double From faeb1d2e184a5bd4c9cdd023baaf09298eb57b68 Mon Sep 17 00:00:00 2001 From: slash0042 <57612744+slash0042@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:21:20 +0000 Subject: [PATCH 40/98] Add libnx port --- libretro/Makefile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libretro/Makefile b/libretro/Makefile index b327628..00b28cd 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -97,6 +97,15 @@ else ifeq ($(platform), switch) include $(LIBTRANSISTOR_HOME)/libtransistor.mk CFLAGS += -Wl,-q -O3 -fno-short-enums -fno-optimize-sibling-calls STATIC_LINKING=1 +# Nintendo Switch (libnx) +else ifeq ($(platform), libnx) + include $(DEVKITPRO)/libnx/switch_rules + TARGET := $(TARGET_NAME)_libretro_$(platform).a + DEFINES += -DSWITCH=1 -D__SWITCH__ -DARM + CFLAGS += $(DEFINES) -fPIE -I$(LIBNX)/include/ -ffunction-sections -fdata-sections -ftls-model=local-exec + CFLAGS += -march=armv8-a -mtune=cortex-a57 -mtp=soft -mcpu=cortex-a57+crc+fp+simd -ffast-math + CXXFLAGS := $(ASFLAGS) $(CFLAGS) + STATIC_LINKING = 1 # Nintendo WiiU else ifeq ($(platform), wiiu) TARGET := $(TARGET_NAME)_libretro_$(platform).a From efe8d6b643c0829739bdb9892e80cbcbd4c27464 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 10 Oct 2020 01:21:13 +0000 Subject: [PATCH 41/98] Update Makefile --- libretro/Makefile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libretro/Makefile b/libretro/Makefile index 00b28cd..366ec17 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -109,8 +109,8 @@ else ifeq ($(platform), libnx) # Nintendo WiiU else ifeq ($(platform), wiiu) TARGET := $(TARGET_NAME)_libretro_$(platform).a - CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT) - AR = $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT) + CC ?= $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT) + AR ?= $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT) CFLAGS += -DGEKKO -DHW_RVL -DWIIU -mwup -mcpu=750 -meabi -mhard-float -D__ppc__ -DMSB_FIRST -I$(DEVKITPRO)/libogc/include CFLAGS += -U__INT32_TYPE__ -U __UINT32_TYPE__ -D__INT32_TYPE__=int STATIC_LINKING = 1 @@ -149,7 +149,7 @@ else ifeq ($(platform), emscripten) fpic := -fPIC SHARED := -shared -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined else ifeq ($(platform), vita) - TARGET := $(TARGET_NAME)_vita.a + TARGET := $(TARGET_NAME)_libretro_vita.a CC = arm-vita-eabi-gcc AR = arm-vita-eabi-ar CFLAGS += -Wl,-q -O3 -fno-short-enums -fno-optimize-sibling-calls @@ -181,14 +181,14 @@ else ifneq (,$(findstring windows_msvc2017,$(platform))) TargetArchMoniker = $(subst $(WinPartition)_,,$(PlatformSuffix)) - CC = cl.exe - CXX = cl.exe - LD = link.exe + CC ?= cl.exe + CXX ?= cl.exe + LD ?= link.exe reg_query = $(call filter_out2,$(subst $2,,$(shell reg query "$2" -v "$1" 2>nul))) fix_path = $(subst $(SPACE),\ ,$(subst \,/,$1)) - ProgramFiles86w := $(shell cmd /c "echo %PROGRAMFILES(x86)%") + ProgramFiles86w := $(shell cmd //c "echo %PROGRAMFILES(x86)%") ProgramFiles86 := $(shell cygpath "$(ProgramFiles86w)") WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0) @@ -251,7 +251,7 @@ else ifneq (,$(findstring windows_msvc2017,$(platform))) LDFLAGS += -DLL else - CC = gcc + 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 endif From 8dc60d0b87d77db1effa7ba1c6003775affc16db Mon Sep 17 00:00:00 2001 From: radius Date: Sat, 10 Oct 2020 03:52:22 +0000 Subject: [PATCH 42/98] update makefile --- libretro/Makefile | 35 +++++++++++++++++++++++++++++++++-- libretro/Makefile.common | 2 ++ libretro/jni/Android.mk | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/libretro/Makefile b/libretro/Makefile index 366ec17..c72b1f7 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -51,7 +51,7 @@ ifeq ($(platform), win) INCFLAGS += -I Windows endif -CORE_DIR += .. +CORE_DIR = ../ TARGET_NAME = sameboy LIBM = -lm @@ -90,7 +90,38 @@ else ifeq ($(platform), linux-portable) TARGET := $(TARGET_NAME)_libretro.$(EXT) fpic := -fPIC -nostdlib SHARED := -shared -Wl,--version-script=$(CORE_DIR)/libretro/link.T - LIBM := + LIBM := +# (armv7 a7, hard point, neon based) ### +# NESC, SNESC, C64 mini +else ifeq ($(platform), classic_armv7_a7) + TARGET := $(TARGET_NAME)_libretro.so + fpic := -fPIC + SHARED := -shared -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined + CFLAGS += -Ofast \ + -flto=4 -fwhole-program -fuse-linker-plugin \ + -fdata-sections -ffunction-sections -Wl,--gc-sections \ + -fno-stack-protector -fno-ident -fomit-frame-pointer \ + -falign-functions=1 -falign-jumps=1 -falign-loops=1 \ + -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-unroll-loops \ + -fmerge-all-constants -fno-math-errno \ + -marm -mtune=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard + CXXFLAGS += $(CFLAGS) + CPPFLAGS += $(CFLAGS) + ASFLAGS += $(CFLAGS) + HAVE_NEON = 1 + ARCH = arm + BUILTIN_GPU = neon + USE_DYNAREC = 1 + ifeq ($(shell echo `$(CC) -dumpversion` "< 4.9" | bc -l), 1) + CFLAGS += -march=armv7-a + else + CFLAGS += -march=armv7ve + # If gcc is 5.0 or later + ifeq ($(shell echo `$(CC) -dumpversion` ">= 5" | bc -l), 1) + LDFLAGS += -static-libgcc -static-libstdc++ + endif + endif +####################################### # Nintendo Switch (libtransistor) else ifeq ($(platform), switch) TARGET := $(TARGET_NAME)_libretro_$(platform).a diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 7f7688a..430c03d 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -1,3 +1,5 @@ +VERSION := 0.13.6 + INCFLAGS := -I$(CORE_DIR) SOURCES_C := $(CORE_DIR)/Core/gb.c \ diff --git a/libretro/jni/Android.mk b/libretro/jni/Android.mk index 3b2d74b..e0646b9 100644 --- a/libretro/jni/Android.mk +++ b/libretro/jni/Android.mk @@ -8,7 +8,7 @@ include $(CORE_DIR)/libretro/Makefile.common GENERATED_SOURCES := $(filter %_boot.c,$(SOURCES_C)) -COREFLAGS := -DINLINE=inline -D__LIBRETRO__ -DGB_INTERNAL $(INCFLAGS) -DSAMEBOY_CORE_VERSION=\"$(VERSION)\" -Wno-multichar +COREFLAGS := -DINLINE=inline -D__LIBRETRO__ -DGB_INTERNAL $(INCFLAGS) -DSAMEBOY_CORE_VERSION=\"$(VERSION)\" -Wno-multichar -DANDROID GIT_VERSION := " $(shell git rev-parse --short HEAD || echo unknown)" ifneq ($(GIT_VERSION)," unknown") From cd526d960e9eb8c5074478e1251aabb5d6158b4f Mon Sep 17 00:00:00 2001 From: SimpleTease <31772993+SimpleTease@users.noreply.github.com> Date: Wed, 7 Oct 2020 21:59:29 -0500 Subject: [PATCH 43/98] libretro: changing model requires manual game restart --- libretro/libretro.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 24514d4..3f70611 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -207,7 +207,7 @@ static retro_environment_t environ_cb; static const struct retro_variable vars_single[] = { { "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_model", "Emulated model (Restart game); Auto|Game Boy|Game Boy Color|Game Boy Advance|Super Game Boy|Super Game Boy 2" }, { "sameboy_border", "Display border; Super Game Boy only|always|never" }, { "sameboy_rumble", "Enable rumble; rumble-enabled games|all games|never" }, { NULL } @@ -219,8 +219,8 @@ static const struct retro_variable vars_dual[] = { /*{ "sameboy_ir", "Infrared Sensor Emulation; disabled|enabled" },*/ { "sameboy_screen_layout", "Screen layout; top-down|left-right" }, { "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_model_1", "Emulated model for Game Boy #1 (Restart game); Auto|Game Boy|Game Boy Color|Game Boy Advance" }, + { "sameboy_model_2", "Emulated model for Game Boy #2 (Restart game); Auto|Game Boy|Game Boy Color|Game Boy Advance" }, { "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" }, @@ -601,11 +601,7 @@ static void check_variables() new_model = MODEL_AUTO; } - if (new_model != model[0]) { - geometry_updated = true; - model[0] = new_model; - init_for_current_model(0); - } + model[0] = new_model; } var.key = "sameboy_border"; @@ -747,10 +743,7 @@ static void check_variables() new_model = MODEL_AUTO; } - if (model[0] != new_model) { - model[0] = new_model; - init_for_current_model(0); - } + model[0] = new_model; } var.key = "sameboy_model_2"; @@ -776,10 +769,7 @@ static void check_variables() new_model = MODEL_AUTO; } - if (model[1] != new_model) { - model[1] = new_model; - init_for_current_model(1); - } + model[1] = new_model; } var.key = "sameboy_screen_layout"; @@ -947,10 +937,14 @@ void retro_set_video_refresh(retro_video_refresh_t cb) void retro_reset(void) { + check_variables(); + for (int i = 0; i < emulated_devices; i++) { + init_for_current_model(i); GB_reset(&gameboy[i]); } + geometry_updated = true; } void retro_run(void) From 2bfca48e0f5f1c4eaa982bd2e4d50a2735c13ae2 Mon Sep 17 00:00:00 2001 From: SimpleTease <31772993+SimpleTease@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:01:42 -0500 Subject: [PATCH 44/98] libretro: fix core version --- libretro/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libretro/Makefile b/libretro/Makefile index c72b1f7..2ed87b8 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -17,8 +17,6 @@ filter_out2 = $(call filter_out1,$(call filter_out1,$1)) unixpath = $(subst \,/,$1) unixcygpath = /$(subst :,,$(call unixpath,$1)) -CFLAGS += -DSAMEBOY_CORE_VERSION=\"$(VERSION)\" - ifeq ($(platform),) platform = unix ifeq ($(shell uname -a),) @@ -302,6 +300,8 @@ endif include Makefile.common +CFLAGS += -DSAMEBOY_CORE_VERSION=\"$(VERSION)\" + OBJECTS := $(patsubst $(CORE_DIR)/%.c,$(CORE_DIR)/build/obj/%_libretro.c.o,$(SOURCES_C)) OBJOUT = -o From 526c2e029a8475806e40f2b027cc5cae42f96b52 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 10 Oct 2020 14:50:11 +0300 Subject: [PATCH 45/98] Fix #296 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97cd2e4..1107fdc 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ SameBoy is an open-source project licensed under the MIT license, and you're wel ## Compilation SameBoy requires the following tools and libraries to build: - * clang + * clang (Recommended; required for macOS) or GCC * make * macOS Cocoa port: macOS SDK and Xcode (For command line tools and ibtool) * SDL port: libsdl2 From 714227883fbc196a73905bb38332aede65bfeed8 Mon Sep 17 00:00:00 2001 From: SimpleTease <31772993+SimpleTease@users.noreply.github.com> Date: Sat, 10 Oct 2020 08:45:59 -0500 Subject: [PATCH 46/98] cross-compile friendly --- BootROMs/pb12.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BootROMs/pb12.c b/BootROMs/pb12.c index 3a72fab..cfedf6b 100644 --- a/BootROMs/pb12.c +++ b/BootROMs/pb12.c @@ -4,7 +4,6 @@ #include #include #include -#include void opts(uint8_t byte, uint8_t *options) { @@ -18,7 +17,8 @@ void write_all(int fd, const void *buf, size_t count) { while (count) { ssize_t written = write(fd, buf, count); if (written < 0) { - err(1, "write"); + fprintf(stderr, "write"); + exit(1); } count -= written; buf += written; From 696bebc673bf1d5ab7356e142c4a84f1fbbef184 Mon Sep 17 00:00:00 2001 From: SimpleTease <31772993+SimpleTease@users.noreply.github.com> Date: Sat, 10 Oct 2020 17:14:10 +0000 Subject: [PATCH 47/98] libretro: joypad bitmasks --- libretro/libretro.c | 39 +- libretro/libretro.h | 1393 +++++++++++++++++++++++++++++++------------ 2 files changed, 1039 insertions(+), 393 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 3f70611..9e27f03 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -85,6 +85,8 @@ static retro_audio_sample_t audio_sample_cb; static retro_input_poll_t input_poll_cb; static retro_input_state_t input_state_cb; +static bool libretro_supports_bitmasks = false; + static unsigned emulated_devices = 1; static bool initialized = false; static unsigned screen_layout = 0; @@ -119,24 +121,39 @@ static struct retro_rumble_interface rumble; static void GB_update_keys_status(GB_gameboy_t *gb, unsigned port) { + uint16_t joypad_bits = 0; + input_poll_cb(); + if (libretro_supports_bitmasks) { + joypad_bits = input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK); + } + else { + unsigned j; + + for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3+1); j++) { + if (input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, j)) { + joypad_bits |= (1 << j); + } + } + } + GB_set_key_state_for_player(gb, GB_KEY_RIGHT, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)); GB_set_key_state_for_player(gb, GB_KEY_LEFT, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)); GB_set_key_state_for_player(gb, GB_KEY_UP, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_UP)); GB_set_key_state_for_player(gb, GB_KEY_DOWN, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)); GB_set_key_state_for_player(gb, GB_KEY_A, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_A)); GB_set_key_state_for_player(gb, GB_KEY_B, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_B)); GB_set_key_state_for_player(gb, GB_KEY_SELECT, emulated_devices == 1 ? port : 0, - input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT)); 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)); + joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_START)); } @@ -840,6 +857,10 @@ void retro_init(void) else { log_cb = fallback_log; } + + if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL)) { + libretro_supports_bitmasks = true; + } } void retro_deinit(void) @@ -848,6 +869,8 @@ void retro_deinit(void) free(frame_buf_copy); frame_buf = NULL; frame_buf_copy = NULL; + + libretro_supports_bitmasks = false; } unsigned retro_api_version(void) diff --git a/libretro/libretro.h b/libretro/libretro.h index a4df6be..1fd2f5b 100644 --- a/libretro/libretro.h +++ b/libretro/libretro.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2016 The RetroArch team +/* Copyright (C) 2010-2018 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this libretro API header (libretro.h). @@ -32,7 +32,7 @@ extern "C" { #endif #ifndef __cplusplus -#if defined(_MSC_VER) && !defined(SN_TARGET_PS3) +#if defined(_MSC_VER) && _MSC_VER < 1800 && !defined(SN_TARGET_PS3) /* Hack applied for MSVC when compiling in C89 mode * as it isn't C99-compliant. */ #define bool unsigned char @@ -77,7 +77,7 @@ extern "C" { # endif #endif -/* Used for checking API/ABI mismatches that can break libretro +/* Used for checking API/ABI mismatches that can break libretro * implementations. * It is not incremented for compatible changes to the API. */ @@ -87,13 +87,13 @@ extern "C" { * Libretro's fundamental device abstractions. * * Libretro's input system consists of some standardized device types, - * such as a joypad (with/without analog), mouse, keyboard, lightgun + * such as a joypad (with/without analog), mouse, keyboard, lightgun * and a pointer. * - * The functionality of these devices are fixed, and individual cores + * The functionality of these devices are fixed, and individual cores * map their own concept of a controller to libretro's abstractions. - * This makes it possible for frontends to map the abstract types to a - * real input device, and not having to worry about binding input + * This makes it possible for frontends to map the abstract types to a + * real input device, and not having to worry about binding input * correctly to arbitrary controller layouts. */ @@ -104,43 +104,52 @@ extern "C" { /* Input disabled. */ #define RETRO_DEVICE_NONE 0 -/* The JOYPAD is called RetroPad. It is essentially a Super Nintendo - * controller, but with additional L2/R2/L3/R3 buttons, similar to a +/* The JOYPAD is called RetroPad. It is essentially a Super Nintendo + * controller, but with additional L2/R2/L3/R3 buttons, similar to a * PS1 DualShock. */ #define RETRO_DEVICE_JOYPAD 1 /* The mouse is a simple mouse, similar to Super Nintendo's mouse. * X and Y coordinates are reported relatively to last poll (poll callback). - * It is up to the libretro implementation to keep track of where the mouse + * It is up to the libretro implementation to keep track of where the mouse * pointer is supposed to be on the screen. - * The frontend must make sure not to interfere with its own hardware + * The frontend must make sure not to interfere with its own hardware * mouse pointer. */ #define RETRO_DEVICE_MOUSE 2 /* KEYBOARD device lets one poll for raw key pressed. - * It is poll based, so input callback will return with the current + * It is poll based, so input callback will return with the current * pressed state. * For event/text based keyboard input, see * RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK. */ #define RETRO_DEVICE_KEYBOARD 3 -/* Lightgun X/Y coordinates are reported relatively to last poll, - * similar to mouse. */ +/* LIGHTGUN device is similar to Guncon-2 for PlayStation 2. + * It reports X/Y coordinates in screen space (similar to the pointer) + * in the range [-0x8000, 0x7fff] in both axes, with zero being center and + * -0x8000 being out of bounds. + * As well as reporting on/off screen state. It features a trigger, + * start/select buttons, auxiliary action buttons and a + * directional pad. A forced off-screen shot can be requested for + * auto-reloading function in some games. + */ #define RETRO_DEVICE_LIGHTGUN 4 /* The ANALOG device is an extension to JOYPAD (RetroPad). - * Similar to DualShock it adds two analog sticks. - * This is treated as a separate device type as it returns values in the - * full analog range of [-0x8000, 0x7fff]. Positive X axis is right. - * Positive Y axis is down. - * Only use ANALOG type when polling for analog values of the axes. + * Similar to DualShock2 it adds two analog sticks and all buttons can + * be analog. This is treated as a separate device type as it returns + * axis values in the full analog range of [-0x7fff, 0x7fff], + * although some devices may return -0x8000. + * Positive X axis is right. Positive Y axis is down. + * Buttons are returned in the range [0, 0x7fff]. + * Only use ANALOG type when polling for analog values. */ #define RETRO_DEVICE_ANALOG 5 /* Abstracts the concept of a pointing mechanism, e.g. touch. - * This allows libretro to query in absolute coordinates where on the + * This allows libretro to query in absolute coordinates where on the * screen a mouse (or something similar) is being placed. * For a touch centric device, coordinates reported are the coordinates * of the press. @@ -148,33 +157,34 @@ extern "C" { * Coordinates in X and Y are reported as: * [-0x7fff, 0x7fff]: -0x7fff corresponds to the far left/top of the screen, * and 0x7fff corresponds to the far right/bottom of the screen. - * The "screen" is here defined as area that is passed to the frontend and + * The "screen" is here defined as area that is passed to the frontend and * later displayed on the monitor. * * The frontend is free to scale/resize this screen as it sees fit, however, - * (X, Y) = (-0x7fff, -0x7fff) will correspond to the top-left pixel of the + * (X, Y) = (-0x7fff, -0x7fff) will correspond to the top-left pixel of the * game image, etc. * - * To check if the pointer coordinates are valid (e.g. a touch display + * To check if the pointer coordinates are valid (e.g. a touch display * actually being touched), PRESSED returns 1 or 0. * - * If using a mouse on a desktop, PRESSED will usually correspond to the + * If using a mouse on a desktop, PRESSED will usually correspond to the * left mouse button, but this is a frontend decision. * PRESSED will only return 1 if the pointer is inside the game screen. * - * For multi-touch, the index variable can be used to successively query + * For multi-touch, the index variable can be used to successively query * more presses. * If index = 0 returns true for _PRESSED, coordinates can be extracted - * with _X, _Y for index = 0. One can then query _PRESSED, _X, _Y with + * with _X, _Y for index = 0. One can then query _PRESSED, _X, _Y with * index = 1, and so on. - * Eventually _PRESSED will return false for an index. No further presses + * Eventually _PRESSED will return false for an index. No further presses * are registered at this point. */ #define RETRO_DEVICE_POINTER 6 /* Buttons for the RetroPad (JOYPAD). - * The placement of these is equivalent to placements on the + * The placement of these is equivalent to placements on the * Super Nintendo controller. - * L2/R2/L3/R3 buttons correspond to the PS1 DualShock. */ + * L2/R2/L3/R3 buttons correspond to the PS1 DualShock. + * Also used as id values for RETRO_DEVICE_INDEX_ANALOG_BUTTON */ #define RETRO_DEVICE_ID_JOYPAD_B 0 #define RETRO_DEVICE_ID_JOYPAD_Y 1 #define RETRO_DEVICE_ID_JOYPAD_SELECT 2 @@ -192,11 +202,14 @@ extern "C" { #define RETRO_DEVICE_ID_JOYPAD_L3 14 #define RETRO_DEVICE_ID_JOYPAD_R3 15 +#define RETRO_DEVICE_ID_JOYPAD_MASK 256 + /* Index / Id values for ANALOG device. */ -#define RETRO_DEVICE_INDEX_ANALOG_LEFT 0 -#define RETRO_DEVICE_INDEX_ANALOG_RIGHT 1 -#define RETRO_DEVICE_ID_ANALOG_X 0 -#define RETRO_DEVICE_ID_ANALOG_Y 1 +#define RETRO_DEVICE_INDEX_ANALOG_LEFT 0 +#define RETRO_DEVICE_INDEX_ANALOG_RIGHT 1 +#define RETRO_DEVICE_INDEX_ANALOG_BUTTON 2 +#define RETRO_DEVICE_ID_ANALOG_X 0 +#define RETRO_DEVICE_ID_ANALOG_Y 1 /* Id values for MOUSE. */ #define RETRO_DEVICE_ID_MOUSE_X 0 @@ -208,20 +221,36 @@ extern "C" { #define RETRO_DEVICE_ID_MOUSE_MIDDLE 6 #define RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP 7 #define RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN 8 +#define RETRO_DEVICE_ID_MOUSE_BUTTON_4 9 +#define RETRO_DEVICE_ID_MOUSE_BUTTON_5 10 -/* Id values for LIGHTGUN types. */ -#define RETRO_DEVICE_ID_LIGHTGUN_X 0 -#define RETRO_DEVICE_ID_LIGHTGUN_Y 1 -#define RETRO_DEVICE_ID_LIGHTGUN_TRIGGER 2 -#define RETRO_DEVICE_ID_LIGHTGUN_CURSOR 3 -#define RETRO_DEVICE_ID_LIGHTGUN_TURBO 4 -#define RETRO_DEVICE_ID_LIGHTGUN_PAUSE 5 -#define RETRO_DEVICE_ID_LIGHTGUN_START 6 +/* Id values for LIGHTGUN. */ +#define RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X 13 /*Absolute Position*/ +#define RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y 14 /*Absolute*/ +#define RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN 15 /*Status Check*/ +#define RETRO_DEVICE_ID_LIGHTGUN_TRIGGER 2 +#define RETRO_DEVICE_ID_LIGHTGUN_RELOAD 16 /*Forced off-screen shot*/ +#define RETRO_DEVICE_ID_LIGHTGUN_AUX_A 3 +#define RETRO_DEVICE_ID_LIGHTGUN_AUX_B 4 +#define RETRO_DEVICE_ID_LIGHTGUN_START 6 +#define RETRO_DEVICE_ID_LIGHTGUN_SELECT 7 +#define RETRO_DEVICE_ID_LIGHTGUN_AUX_C 8 +#define RETRO_DEVICE_ID_LIGHTGUN_DPAD_UP 9 +#define RETRO_DEVICE_ID_LIGHTGUN_DPAD_DOWN 10 +#define RETRO_DEVICE_ID_LIGHTGUN_DPAD_LEFT 11 +#define RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT 12 +/* deprecated */ +#define RETRO_DEVICE_ID_LIGHTGUN_X 0 /*Relative Position*/ +#define RETRO_DEVICE_ID_LIGHTGUN_Y 1 /*Relative*/ +#define RETRO_DEVICE_ID_LIGHTGUN_CURSOR 3 /*Use Aux:A*/ +#define RETRO_DEVICE_ID_LIGHTGUN_TURBO 4 /*Use Aux:B*/ +#define RETRO_DEVICE_ID_LIGHTGUN_PAUSE 5 /*Use Start*/ /* Id values for POINTER. */ #define RETRO_DEVICE_ID_POINTER_X 0 #define RETRO_DEVICE_ID_POINTER_Y 1 #define RETRO_DEVICE_ID_POINTER_PRESSED 2 +#define RETRO_DEVICE_ID_POINTER_COUNT 3 /* Returned from retro_get_region(). */ #define RETRO_REGION_NTSC 0 @@ -230,28 +259,33 @@ extern "C" { /* Id values for LANGUAGE */ enum retro_language { - RETRO_LANGUAGE_ENGLISH = 0, - RETRO_LANGUAGE_JAPANESE = 1, - RETRO_LANGUAGE_FRENCH = 2, - RETRO_LANGUAGE_SPANISH = 3, - RETRO_LANGUAGE_GERMAN = 4, - RETRO_LANGUAGE_ITALIAN = 5, - RETRO_LANGUAGE_DUTCH = 6, - RETRO_LANGUAGE_PORTUGUESE = 7, - RETRO_LANGUAGE_RUSSIAN = 8, - RETRO_LANGUAGE_KOREAN = 9, - RETRO_LANGUAGE_CHINESE_TRADITIONAL = 10, - RETRO_LANGUAGE_CHINESE_SIMPLIFIED = 11, - RETRO_LANGUAGE_ESPERANTO = 12, - RETRO_LANGUAGE_POLISH = 13, + RETRO_LANGUAGE_ENGLISH = 0, + RETRO_LANGUAGE_JAPANESE = 1, + RETRO_LANGUAGE_FRENCH = 2, + RETRO_LANGUAGE_SPANISH = 3, + RETRO_LANGUAGE_GERMAN = 4, + RETRO_LANGUAGE_ITALIAN = 5, + RETRO_LANGUAGE_DUTCH = 6, + RETRO_LANGUAGE_PORTUGUESE_BRAZIL = 7, + RETRO_LANGUAGE_PORTUGUESE_PORTUGAL = 8, + RETRO_LANGUAGE_RUSSIAN = 9, + RETRO_LANGUAGE_KOREAN = 10, + RETRO_LANGUAGE_CHINESE_TRADITIONAL = 11, + RETRO_LANGUAGE_CHINESE_SIMPLIFIED = 12, + RETRO_LANGUAGE_ESPERANTO = 13, + RETRO_LANGUAGE_POLISH = 14, + RETRO_LANGUAGE_VIETNAMESE = 15, + RETRO_LANGUAGE_ARABIC = 16, + RETRO_LANGUAGE_GREEK = 17, + RETRO_LANGUAGE_TURKISH = 18, RETRO_LANGUAGE_LAST, /* Ensure sizeof(enum) == sizeof(int) */ - RETRO_LANGUAGE_DUMMY = INT_MAX + RETRO_LANGUAGE_DUMMY = INT_MAX }; /* Passed to retro_get_memory_data/size(). - * If the memory type doesn't apply to the + * If the memory type doesn't apply to the * implementation NULL/0 can be returned. */ #define RETRO_MEMORY_MASK 0xff @@ -349,6 +383,10 @@ enum retro_key RETROK_x = 120, RETROK_y = 121, RETROK_z = 122, + RETROK_LEFTBRACE = 123, + RETROK_BAR = 124, + RETROK_RIGHTBRACE = 125, + RETROK_TILDE = 126, RETROK_DELETE = 127, RETROK_KP0 = 256, @@ -419,6 +457,7 @@ enum retro_key RETROK_POWER = 320, RETROK_EURO = 321, RETROK_UNDO = 322, + RETROK_OEM_102 = 323, RETROK_LAST, @@ -441,7 +480,7 @@ enum retro_mod RETROKMOD_DUMMY = INT_MAX /* Ensure sizeof(enum) == sizeof(int) */ }; -/* If set, this call is not part of the public libretro API yet. It can +/* If set, this call is not part of the public libretro API yet. It can * change or be removed at any time. */ #define RETRO_ENVIRONMENT_EXPERIMENTAL 0x10000 /* Environment callback to be used internally in frontend. */ @@ -450,12 +489,14 @@ enum retro_mod /* Environment commands. */ #define RETRO_ENVIRONMENT_SET_ROTATION 1 /* const unsigned * -- * Sets screen rotation of graphics. - * Is only implemented if rotation can be accelerated by hardware. - * Valid values are 0, 1, 2, 3, which rotates screen by 0, 90, 180, + * Valid values are 0, 1, 2, 3, which rotates screen by 0, 90, 180, * 270 degrees counter-clockwise respectively. */ #define RETRO_ENVIRONMENT_GET_OVERSCAN 2 /* bool * -- - * Boolean value whether or not the implementation should use overscan, + * NOTE: As of 2019 this callback is considered deprecated in favor of + * using core options to manage overscan in a more nuanced, core-specific way. + * + * Boolean value whether or not the implementation should use overscan, * or crop away overscan. */ #define RETRO_ENVIRONMENT_GET_CAN_DUPE 3 /* bool * -- @@ -463,15 +504,15 @@ enum retro_mod * passing NULL to video frame callback. */ - /* Environ 4, 5 are no longer supported (GET_VARIABLE / SET_VARIABLES), + /* Environ 4, 5 are no longer supported (GET_VARIABLE / SET_VARIABLES), * and reserved to avoid possible ABI clash. */ #define RETRO_ENVIRONMENT_SET_MESSAGE 6 /* const struct retro_message * -- - * Sets a message to be displayed in implementation-specific manner + * Sets a message to be displayed in implementation-specific manner * for a certain amount of 'frames'. - * Should not be used for trivial messages, which should simply be - * logged via RETRO_ENVIRONMENT_GET_LOG_INTERFACE (or as a + * Should not be used for trivial messages, which should simply be + * logged via RETRO_ENVIRONMENT_GET_LOG_INTERFACE (or as a * fallback, stderr). */ #define RETRO_ENVIRONMENT_SHUTDOWN 7 /* N/A (NULL) -- @@ -499,15 +540,15 @@ enum retro_mod #define RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY 9 /* const char ** -- * Returns the "system" directory of the frontend. - * This directory can be used to store system specific + * This directory can be used to store system specific * content such as BIOSes, configuration data, etc. * The returned value can be NULL. * If so, no such directory is defined, * and it's up to the implementation to find a suitable directory. * - * NOTE: Some cores used this folder also for "save" data such as + * NOTE: Some cores used this folder also for "save" data such as * memory cards, etc, for lack of a better place to put it. - * This is now discouraged, and if possible, cores should try to + * This is now discouraged, and if possible, cores should try to * use the new GET_SAVE_DIRECTORY. */ #define RETRO_ENVIRONMENT_SET_PIXEL_FORMAT 10 @@ -515,19 +556,19 @@ enum retro_mod * Sets the internal pixel format used by the implementation. * The default pixel format is RETRO_PIXEL_FORMAT_0RGB1555. * This pixel format however, is deprecated (see enum retro_pixel_format). - * If the call returns false, the frontend does not support this pixel + * If the call returns false, the frontend does not support this pixel * format. * - * This function should be called inside retro_load_game() or + * This function should be called inside retro_load_game() or * retro_get_system_av_info(). */ #define RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS 11 /* const struct retro_input_descriptor * -- * Sets an array of retro_input_descriptors. * It is up to the frontend to present this in a usable way. - * The array is terminated by retro_input_descriptor::description + * The array is terminated by retro_input_descriptor::description * being set to NULL. - * This function can be called at any time, but it is recommended + * This function can be called at any time, but it is recommended * to call it as early as possible. */ #define RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK 12 @@ -536,52 +577,55 @@ enum retro_mod */ #define RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE 13 /* const struct retro_disk_control_callback * -- - * Sets an interface which frontend can use to eject and insert + * Sets an interface which frontend can use to eject and insert * disk images. - * This is used for games which consist of multiple images and + * This is used for games which consist of multiple images and * must be manually swapped out by the user (e.g. PSX). */ #define RETRO_ENVIRONMENT_SET_HW_RENDER 14 /* struct retro_hw_render_callback * -- - * Sets an interface to let a libretro core render with + * Sets an interface to let a libretro core render with * hardware acceleration. * Should be called in retro_load_game(). - * If successful, libretro cores will be able to render to a + * If successful, libretro cores will be able to render to a * frontend-provided framebuffer. - * The size of this framebuffer will be at least as large as + * The size of this framebuffer will be at least as large as * max_width/max_height provided in get_av_info(). - * If HW rendering is used, pass only RETRO_HW_FRAME_BUFFER_VALID or + * If HW rendering is used, pass only RETRO_HW_FRAME_BUFFER_VALID or * NULL to retro_video_refresh_t. */ #define RETRO_ENVIRONMENT_GET_VARIABLE 15 /* struct retro_variable * -- * Interface to acquire user-defined information from environment * that cannot feasibly be supported in a multi-system way. - * 'key' should be set to a key which has already been set by + * 'key' should be set to a key which has already been set by * SET_VARIABLES. * 'data' will be set to a value or NULL. */ #define RETRO_ENVIRONMENT_SET_VARIABLES 16 /* const struct retro_variable * -- * Allows an implementation to signal the environment - * which variables it might want to check for later using + * which variables it might want to check for later using * GET_VARIABLE. - * This allows the frontend to present these variables to + * This allows the frontend to present these variables to * a user dynamically. - * This should be called as early as possible (ideally in - * retro_set_environment). + * This should be called the first time as early as + * possible (ideally in retro_set_environment). + * Afterward it may be called again for the core to communicate + * updated options to the frontend, but the number of core + * options must not change from the number in the initial call. * - * 'data' points to an array of retro_variable structs + * 'data' points to an array of retro_variable structs * terminated by a { NULL, NULL } element. - * retro_variable::key should be namespaced to not collide - * with other implementations' keys. E.g. A core called + * retro_variable::key should be namespaced to not collide + * with other implementations' keys. E.g. A core called * 'foo' should use keys named as 'foo_option'. - * retro_variable::value should contain a human readable - * description of the key as well as a '|' delimited list + * retro_variable::value should contain a human readable + * description of the key as well as a '|' delimited list * of expected values. * - * The number of possible options should be very limited, - * i.e. it should be feasible to cycle through options + * The number of possible options should be very limited, + * i.e. it should be feasible to cycle through options * without a keyboard. * * First entry should be treated as a default. @@ -589,11 +633,11 @@ enum retro_mod * Example entry: * { "foo_option", "Speed hack coprocessor X; false|true" } * - * Text before first ';' is description. This ';' must be - * followed by a space, and followed by a list of possible + * Text before first ';' is description. This ';' must be + * followed by a space, and followed by a list of possible * values split up with '|'. * - * Only strings are operated on. The possible values will + * Only strings are operated on. The possible values will * generally be displayed and stored as-is by the frontend. */ #define RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE 17 @@ -604,72 +648,72 @@ enum retro_mod */ #define RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME 18 /* const bool * -- - * If true, the libretro implementation supports calls to + * If true, the libretro implementation supports calls to * retro_load_game() with NULL as argument. * Used by cores which can run without particular game data. * This should be called within retro_set_environment() only. */ #define RETRO_ENVIRONMENT_GET_LIBRETRO_PATH 19 /* const char ** -- - * Retrieves the absolute path from where this libretro + * Retrieves the absolute path from where this libretro * implementation was loaded. - * NULL is returned if the libretro was loaded statically - * (i.e. linked statically to frontend), or if the path cannot be + * NULL is returned if the libretro was loaded statically + * (i.e. linked statically to frontend), or if the path cannot be * determined. - * Mostly useful in cooperation with SET_SUPPORT_NO_GAME as assets can + * Mostly useful in cooperation with SET_SUPPORT_NO_GAME as assets can * be loaded without ugly hacks. */ - - /* Environment 20 was an obsolete version of SET_AUDIO_CALLBACK. + + /* Environment 20 was an obsolete version of SET_AUDIO_CALLBACK. * It was not used by any known core at the time, * and was removed from the API. */ +#define RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK 21 + /* const struct retro_frame_time_callback * -- + * Lets the core know how much time has passed since last + * invocation of retro_run(). + * The frontend can tamper with the timing to fake fast-forward, + * slow-motion, frame stepping, etc. + * In this case the delta time will use the reference value + * in frame_time_callback.. + */ #define RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK 22 /* const struct retro_audio_callback * -- - * Sets an interface which is used to notify a libretro core about audio + * Sets an interface which is used to notify a libretro core about audio * being available for writing. - * The callback can be called from any thread, so a core using this must + * The callback can be called from any thread, so a core using this must * have a thread safe audio implementation. - * It is intended for games where audio and video are completely + * It is intended for games where audio and video are completely * asynchronous and audio can be generated on the fly. - * This interface is not recommended for use with emulators which have + * This interface is not recommended for use with emulators which have * highly synchronous audio. * - * The callback only notifies about writability; the libretro core still + * The callback only notifies about writability; the libretro core still * has to call the normal audio callbacks - * to write audio. The audio callbacks must be called from within the + * to write audio. The audio callbacks must be called from within the * notification callback. * The amount of audio data to write is up to the implementation. * Generally, the audio callback will be called continously in a loop. * - * Due to thread safety guarantees and lack of sync between audio and - * video, a frontend can selectively disallow this interface based on - * internal configuration. A core using this interface must also + * Due to thread safety guarantees and lack of sync between audio and + * video, a frontend can selectively disallow this interface based on + * internal configuration. A core using this interface must also * implement the "normal" audio interface. * - * A libretro core using SET_AUDIO_CALLBACK should also make use of + * A libretro core using SET_AUDIO_CALLBACK should also make use of * SET_FRAME_TIME_CALLBACK. */ -#define RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK 21 - /* const struct retro_frame_time_callback * -- - * Lets the core know how much time has passed since last - * invocation of retro_run(). - * The frontend can tamper with the timing to fake fast-forward, - * slow-motion, frame stepping, etc. - * In this case the delta time will use the reference value - * in frame_time_callback.. - */ #define RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE 23 /* struct retro_rumble_interface * -- - * Gets an interface which is used by a libretro core to set + * Gets an interface which is used by a libretro core to set * state of rumble motors in controllers. - * A strong and weak motor is supported, and they can be + * A strong and weak motor is supported, and they can be * controlled indepedently. */ #define RETRO_ENVIRONMENT_GET_INPUT_DEVICE_CAPABILITIES 24 /* uint64_t * -- - * Gets a bitmask telling which device type are expected to be + * Gets a bitmask telling which device type are expected to be * handled properly in a call to retro_input_state_t. - * Devices which are not handled or recognized always return + * Devices which are not handled or recognized always return * 0 in retro_input_state_t. * Example bitmask: caps = (1 << RETRO_DEVICE_JOYPAD) | (1 << RETRO_DEVICE_ANALOG). * Should only be called in retro_run(). @@ -678,56 +722,56 @@ enum retro_mod /* struct retro_sensor_interface * -- * Gets access to the sensor interface. * The purpose of this interface is to allow - * setting state related to sensors such as polling rate, + * setting state related to sensors such as polling rate, * enabling/disable it entirely, etc. - * Reading sensor state is done via the normal + * Reading sensor state is done via the normal * input_state_callback API. */ #define RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE (26 | RETRO_ENVIRONMENT_EXPERIMENTAL) /* struct retro_camera_callback * -- * Gets an interface to a video camera driver. - * A libretro core can use this interface to get access to a + * A libretro core can use this interface to get access to a * video camera. - * New video frames are delivered in a callback in same + * New video frames are delivered in a callback in same * thread as retro_run(). * * GET_CAMERA_INTERFACE should be called in retro_load_game(). * - * Depending on the camera implementation used, camera frames + * Depending on the camera implementation used, camera frames * will be delivered as a raw framebuffer, * or as an OpenGL texture directly. * - * The core has to tell the frontend here which types of + * The core has to tell the frontend here which types of * buffers can be handled properly. - * An OpenGL texture can only be handled when using a + * An OpenGL texture can only be handled when using a * libretro GL core (SET_HW_RENDER). - * It is recommended to use a libretro GL core when + * It is recommended to use a libretro GL core when * using camera interface. * - * The camera is not started automatically. The retrieved start/stop + * The camera is not started automatically. The retrieved start/stop * functions must be used to explicitly * start and stop the camera driver. */ #define RETRO_ENVIRONMENT_GET_LOG_INTERFACE 27 /* struct retro_log_callback * -- - * Gets an interface for logging. This is useful for + * Gets an interface for logging. This is useful for * logging in a cross-platform way - * as certain platforms cannot use use stderr for logging. + * as certain platforms cannot use stderr for logging. * It also allows the frontend to * show logging information in a more suitable way. - * If this interface is not used, libretro cores should + * If this interface is not used, libretro cores should * log to stderr as desired. */ #define RETRO_ENVIRONMENT_GET_PERF_INTERFACE 28 /* struct retro_perf_callback * -- - * Gets an interface for performance counters. This is useful - * for performance logging in a cross-platform way and for detecting + * Gets an interface for performance counters. This is useful + * for performance logging in a cross-platform way and for detecting * architecture-specific features, such as SIMD support. */ #define RETRO_ENVIRONMENT_GET_LOCATION_INTERFACE 29 /* struct retro_location_callback * -- * Gets access to the location interface. - * The purpose of this interface is to be able to retrieve + * The purpose of this interface is to be able to retrieve * location-based information from the host device, * such as current latitude / longitude. */ @@ -735,7 +779,7 @@ enum retro_mod #define RETRO_ENVIRONMENT_GET_CORE_ASSETS_DIRECTORY 30 /* const char ** -- * Returns the "core assets" directory of the frontend. - * This directory can be used to store specific assets that the + * This directory can be used to store specific assets that the * core relies upon, such as art assets, * input data, etc etc. * The returned value can be NULL. @@ -744,76 +788,77 @@ enum retro_mod */ #define RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY 31 /* const char ** -- - * Returns the "save" directory of the frontend. - * This directory can be used to store SRAM, memory cards, - * high scores, etc, if the libretro core + * Returns the "save" directory of the frontend, unless there is no + * save directory available. The save directory should be used to + * store SRAM, memory cards, high scores, etc, if the libretro core * cannot use the regular memory interface (retro_get_memory_data()). * - * NOTE: libretro cores used to check GET_SYSTEM_DIRECTORY for - * similar things before. - * They should still check GET_SYSTEM_DIRECTORY if they want to - * be backwards compatible. - * The path here can be NULL. It should only be non-NULL if the - * frontend user has set a specific save path. + * If the frontend cannot designate a save directory, it will return + * NULL to indicate that the core should attempt to operate without a + * save directory set. + * + * NOTE: early libretro cores used the system directory for save + * files. Cores that need to be backwards-compatible can still check + * GET_SYSTEM_DIRECTORY. */ #define RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO 32 /* const struct retro_system_av_info * -- - * Sets a new av_info structure. This can only be called from + * Sets a new av_info structure. This can only be called from * within retro_run(). - * This should *only* be used if the core is completely altering the + * This should *only* be used if the core is completely altering the * internal resolutions, aspect ratios, timings, sampling rate, etc. - * Calling this can require a full reinitialization of video/audio + * Calling this can require a full reinitialization of video/audio * drivers in the frontend, * - * so it is important to call it very sparingly, and usually only with + * so it is important to call it very sparingly, and usually only with * the users explicit consent. - * An eventual driver reinitialize will happen so that video and + * An eventual driver reinitialize will happen so that video and * audio callbacks - * happening after this call within the same retro_run() call will + * happening after this call within the same retro_run() call will * target the newly initialized driver. * - * This callback makes it possible to support configurable resolutions + * This callback makes it possible to support configurable resolutions * in games, which can be useful to * avoid setting the "worst case" in max_width/max_height. * - * ***HIGHLY RECOMMENDED*** Do not call this callback every time + * ***HIGHLY RECOMMENDED*** Do not call this callback every time * resolution changes in an emulator core if it's - * expected to be a temporary change, for the reasons of possible + * expected to be a temporary change, for the reasons of possible * driver reinitialization. - * This call is not a free pass for not trying to provide - * correct values in retro_get_system_av_info(). If you need to change - * things like aspect ratio or nominal width/height, - * use RETRO_ENVIRONMENT_SET_GEOMETRY, which is a softer variant + * This call is not a free pass for not trying to provide + * correct values in retro_get_system_av_info(). If you need to change + * things like aspect ratio or nominal width/height, + * use RETRO_ENVIRONMENT_SET_GEOMETRY, which is a softer variant * of SET_SYSTEM_AV_INFO. * - * If this returns false, the frontend does not acknowledge a + * If this returns false, the frontend does not acknowledge a * changed av_info struct. */ #define RETRO_ENVIRONMENT_SET_PROC_ADDRESS_CALLBACK 33 /* const struct retro_get_proc_address_interface * -- - * Allows a libretro core to announce support for the + * Allows a libretro core to announce support for the * get_proc_address() interface. - * This interface allows for a standard way to extend libretro where + * This interface allows for a standard way to extend libretro where * use of environment calls are too indirect, * e.g. for cases where the frontend wants to call directly into the core. * - * If a core wants to expose this interface, SET_PROC_ADDRESS_CALLBACK + * If a core wants to expose this interface, SET_PROC_ADDRESS_CALLBACK * **MUST** be called from within retro_set_environment(). */ #define RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO 34 /* const struct retro_subsystem_info * -- * This environment call introduces the concept of libretro "subsystems". - * A subsystem is a variant of a libretro core which supports + * A subsystem is a variant of a libretro core which supports * different kinds of games. - * The purpose of this is to support e.g. emulators which might + * The purpose of this is to support e.g. emulators which might * have special needs, e.g. Super Nintendo's Super GameBoy, Sufami Turbo. - * It can also be used to pick among subsystems in an explicit way + * It can also be used to pick among subsystems in an explicit way * if the libretro implementation is a multi-system emulator itself. * * Loading a game via a subsystem is done with retro_load_game_special(), - * and this environment call allows a libretro core to expose which + * and this environment call allows a libretro core to expose which * subsystems are supported for use with retro_load_game_special(). - * A core passes an array of retro_game_special_info which is terminated + * A core passes an array of retro_game_special_info which is terminated * with a zeroed out retro_game_special_info struct. * * If a core wants to use this functionality, SET_SUBSYSTEM_INFO @@ -821,68 +866,81 @@ enum retro_mod */ #define RETRO_ENVIRONMENT_SET_CONTROLLER_INFO 35 /* const struct retro_controller_info * -- - * This environment call lets a libretro core tell the frontend - * which controller types are recognized in calls to + * This environment call lets a libretro core tell the frontend + * which controller subclasses are recognized in calls to * retro_set_controller_port_device(). * - * Some emulators such as Super Nintendo - * support multiple lightgun types which must be specifically - * selected from. - * It is therefore sometimes necessary for a frontend to be able - * to tell the core about a special kind of input device which is - * not covered by the libretro input API. + * Some emulators such as Super Nintendo support multiple lightgun + * types which must be specifically selected from. It is therefore + * sometimes necessary for a frontend to be able to tell the core + * about a special kind of input device which is not specifcally + * provided by the Libretro API. * - * In order for a frontend to understand the workings of an input device, - * it must be a specialized type - * of the generic device types already defined in the libretro API. + * In order for a frontend to understand the workings of those devices, + * they must be defined as a specialized subclass of the generic device + * types already defined in the libretro API. * - * Which devices are supported can vary per input port. - * The core must pass an array of const struct retro_controller_info which - * is terminated with a blanked out struct. Each element of the struct - * corresponds to an ascending port index to - * retro_set_controller_port_device(). - * Even if special device types are set in the libretro core, + * The core must pass an array of const struct retro_controller_info which + * is terminated with a blanked out struct. Each element of the + * retro_controller_info struct corresponds to the ascending port index + * that is passed to retro_set_controller_port_device() when that function + * is called to indicate to the core that the frontend has changed the + * active device subclass. SEE ALSO: retro_set_controller_port_device() + * + * The ascending input port indexes provided by the core in the struct + * are generally presented by frontends as ascending User # or Player #, + * such as Player 1, Player 2, Player 3, etc. Which device subclasses are + * supported can vary per input port. + * + * The first inner element of each entry in the retro_controller_info array + * is a retro_controller_description struct that specifies the names and + * codes of all device subclasses that are available for the corresponding + * User or Player, beginning with the generic Libretro device that the + * subclasses are derived from. The second inner element of each entry is the + * total number of subclasses that are listed in the retro_controller_description. + * + * NOTE: Even if special device types are set in the libretro core, * libretro should only poll input based on the base input device types. */ #define RETRO_ENVIRONMENT_SET_MEMORY_MAPS (36 | RETRO_ENVIRONMENT_EXPERIMENTAL) /* const struct retro_memory_map * -- - * This environment call lets a libretro core tell the frontend + * This environment call lets a libretro core tell the frontend * about the memory maps this core emulates. * This can be used to implement, for example, cheats in a core-agnostic way. * - * Should only be used by emulators; it doesn't make much sense for + * Should only be used by emulators; it doesn't make much sense for * anything else. - * It is recommended to expose all relevant pointers through + * It is recommended to expose all relevant pointers through * retro_get_memory_* as well. * * Can be called from retro_init and retro_load_game. */ #define RETRO_ENVIRONMENT_SET_GEOMETRY 37 /* const struct retro_game_geometry * -- - * This environment call is similar to SET_SYSTEM_AV_INFO for changing - * video parameters, but provides a guarantee that drivers will not be + * This environment call is similar to SET_SYSTEM_AV_INFO for changing + * video parameters, but provides a guarantee that drivers will not be * reinitialized. * This can only be called from within retro_run(). * - * The purpose of this call is to allow a core to alter nominal - * width/heights as well as aspect ratios on-the-fly, which can be + * The purpose of this call is to allow a core to alter nominal + * width/heights as well as aspect ratios on-the-fly, which can be * useful for some emulators to change in run-time. * * max_width/max_height arguments are ignored and cannot be changed - * with this call as this could potentially require a reinitialization or a + * with this call as this could potentially require a reinitialization or a * non-constant time operation. * If max_width/max_height are to be changed, SET_SYSTEM_AV_INFO is required. * - * A frontend must guarantee that this environment call completes in + * A frontend must guarantee that this environment call completes in * constant time. */ -#define RETRO_ENVIRONMENT_GET_USERNAME 38 +#define RETRO_ENVIRONMENT_GET_USERNAME 38 /* const char ** * Returns the specified username of the frontend, if specified by the user. - * This username can be used as a nickname for a core that has online facilities + * This username can be used as a nickname for a core that has online facilities * or any other mode where personalization of the user is desirable. * The returned value can be NULL. - * If this environ callback is used by a core that requires a valid username, + * If this environ callback is used by a core that requires a valid username, * a default username should be specified by the core. */ #define RETRO_ENVIRONMENT_GET_LANGUAGE 39 @@ -920,20 +978,6 @@ enum retro_mod * A frontend must make sure that the pointer obtained from this function is * writeable (and readable). */ - -enum retro_hw_render_interface_type -{ - RETRO_HW_RENDER_INTERFACE_VULKAN = 0, - RETRO_HW_RENDER_INTERFACE_DUMMY = INT_MAX -}; - -/* Base struct. All retro_hw_render_interface_* types - * contain at least these fields. */ -struct retro_hw_render_interface -{ - enum retro_hw_render_interface_type interface_type; - unsigned interface_version; -}; #define RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE (41 | RETRO_ENVIRONMENT_EXPERIMENTAL) /* const struct retro_hw_render_interface ** -- * Returns an API specific rendering interface for accessing API specific data. @@ -945,7 +989,6 @@ struct retro_hw_render_interface * Similarly, after context_destroyed callback returns, * the contents of the HW_RENDER_INTERFACE are invalidated. */ - #define RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS (42 | RETRO_ENVIRONMENT_EXPERIMENTAL) /* const bool * -- * If true, the libretro implementation supports achievements @@ -954,6 +997,483 @@ struct retro_hw_render_interface * * This must be called before the first call to retro_run. */ +#define RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE (43 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* const struct retro_hw_render_context_negotiation_interface * -- + * Sets an interface which lets the libretro core negotiate with frontend how a context is created. + * The semantics of this interface depends on which API is used in SET_HW_RENDER earlier. + * This interface will be used when the frontend is trying to create a HW rendering context, + * so it will be used after SET_HW_RENDER, but before the context_reset callback. + */ +#define RETRO_ENVIRONMENT_SET_SERIALIZATION_QUIRKS 44 + /* uint64_t * -- + * Sets quirk flags associated with serialization. The frontend will zero any flags it doesn't + * recognize or support. Should be set in either retro_init or retro_load_game, but not both. + */ +#define RETRO_ENVIRONMENT_SET_HW_SHARED_CONTEXT (44 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* N/A (null) * -- + * The frontend will try to use a 'shared' hardware context (mostly applicable + * to OpenGL) when a hardware context is being set up. + * + * Returns true if the frontend supports shared hardware contexts and false + * if the frontend does not support shared hardware contexts. + * + * This will do nothing on its own until SET_HW_RENDER env callbacks are + * being used. + */ +#define RETRO_ENVIRONMENT_GET_VFS_INTERFACE (45 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* struct retro_vfs_interface_info * -- + * Gets access to the VFS interface. + * VFS presence needs to be queried prior to load_game or any + * get_system/save/other_directory being called to let front end know + * core supports VFS before it starts handing out paths. + * It is recomended to do so in retro_set_environment + */ +#define RETRO_ENVIRONMENT_GET_LED_INTERFACE (46 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* struct retro_led_interface * -- + * Gets an interface which is used by a libretro core to set + * state of LEDs. + */ +#define RETRO_ENVIRONMENT_GET_AUDIO_VIDEO_ENABLE (47 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* int * -- + * Tells the core if the frontend wants audio or video. + * If disabled, the frontend will discard the audio or video, + * so the core may decide to skip generating a frame or generating audio. + * This is mainly used for increasing performance. + * Bit 0 (value 1): Enable Video + * Bit 1 (value 2): Enable Audio + * Bit 2 (value 4): Use Fast Savestates. + * Bit 3 (value 8): Hard Disable Audio + * Other bits are reserved for future use and will default to zero. + * If video is disabled: + * * The frontend wants the core to not generate any video, + * including presenting frames via hardware acceleration. + * * The frontend's video frame callback will do nothing. + * * After running the frame, the video output of the next frame should be + * no different than if video was enabled, and saving and loading state + * should have no issues. + * If audio is disabled: + * * The frontend wants the core to not generate any audio. + * * The frontend's audio callbacks will do nothing. + * * After running the frame, the audio output of the next frame should be + * no different than if audio was enabled, and saving and loading state + * should have no issues. + * Fast Savestates: + * * Guaranteed to be created by the same binary that will load them. + * * Will not be written to or read from the disk. + * * Suggest that the core assumes loading state will succeed. + * * Suggest that the core updates its memory buffers in-place if possible. + * * Suggest that the core skips clearing memory. + * * Suggest that the core skips resetting the system. + * * Suggest that the core may skip validation steps. + * Hard Disable Audio: + * * Used for a secondary core when running ahead. + * * Indicates that the frontend will never need audio from the core. + * * Suggests that the core may stop synthesizing audio, but this should not + * compromise emulation accuracy. + * * Audio output for the next frame does not matter, and the frontend will + * never need an accurate audio state in the future. + * * State will never be saved when using Hard Disable Audio. + */ +#define RETRO_ENVIRONMENT_GET_MIDI_INTERFACE (48 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* struct retro_midi_interface ** -- + * Returns a MIDI interface that can be used for raw data I/O. + */ + +#define RETRO_ENVIRONMENT_GET_FASTFORWARDING (49 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* bool * -- + * Boolean value that indicates whether or not the frontend is in + * fastforwarding mode. + */ + +#define RETRO_ENVIRONMENT_GET_TARGET_REFRESH_RATE (50 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* float * -- + * Float value that lets us know what target refresh rate + * is curently in use by the frontend. + * + * The core can use the returned value to set an ideal + * refresh rate/framerate. + */ + +#define RETRO_ENVIRONMENT_GET_INPUT_BITMASKS (51 | RETRO_ENVIRONMENT_EXPERIMENTAL) + /* bool * -- + * Boolean value that indicates whether or not the frontend supports + * input bitmasks being returned by retro_input_state_t. The advantage + * of this is that retro_input_state_t has to be only called once to + * grab all button states instead of multiple times. + * + * If it returns true, you can pass RETRO_DEVICE_ID_JOYPAD_MASK as 'id' + * to retro_input_state_t (make sure 'device' is set to RETRO_DEVICE_JOYPAD). + * It will return a bitmask of all the digital buttons. + */ + +#define RETRO_ENVIRONMENT_GET_CORE_OPTIONS_VERSION 52 + /* unsigned * -- + * Unsigned value is the API version number of the core options + * interface supported by the frontend. If callback return false, + * API version is assumed to be 0. + * + * In legacy code, core options are set by passing an array of + * retro_variable structs to RETRO_ENVIRONMENT_SET_VARIABLES. + * This may be still be done regardless of the core options + * interface version. + * + * If version is 1 however, core options may instead be set by + * passing an array of retro_core_option_definition structs to + * RETRO_ENVIRONMENT_SET_CORE_OPTIONS, or a 2D array of + * retro_core_option_definition structs to RETRO_ENVIRONMENT_SET_CORE_OPTIONS_INTL. + * This allows the core to additionally set option sublabel information + * and/or provide localisation support. + */ + +#define RETRO_ENVIRONMENT_SET_CORE_OPTIONS 53 + /* const struct retro_core_option_definition ** -- + * Allows an implementation to signal the environment + * which variables it might want to check for later using + * GET_VARIABLE. + * This allows the frontend to present these variables to + * a user dynamically. + * This should only be called if RETRO_ENVIRONMENT_GET_ENHANCED_CORE_OPTIONS + * returns an API version of 1. + * This should be called instead of RETRO_ENVIRONMENT_SET_VARIABLES. + * This should be called the first time as early as + * possible (ideally in retro_set_environment). + * Afterwards it may be called again for the core to communicate + * updated options to the frontend, but the number of core + * options must not change from the number in the initial call. + * + * 'data' points to an array of retro_core_option_definition structs + * terminated by a { NULL, NULL, NULL, {{0}}, NULL } element. + * retro_core_option_definition::key should be namespaced to not collide + * with other implementations' keys. e.g. A core called + * 'foo' should use keys named as 'foo_option'. + * retro_core_option_definition::desc should contain a human readable + * description of the key. + * retro_core_option_definition::info should contain any additional human + * readable information text that a typical user may need to + * understand the functionality of the option. + * retro_core_option_definition::values is an array of retro_core_option_value + * structs terminated by a { NULL, NULL } element. + * > retro_core_option_definition::values[index].value is an expected option + * value. + * > retro_core_option_definition::values[index].label is a human readable + * label used when displaying the value on screen. If NULL, + * the value itself is used. + * retro_core_option_definition::default_value is the default core option + * setting. It must match one of the expected option values in the + * retro_core_option_definition::values array. If it does not, or the + * default value is NULL, the first entry in the + * retro_core_option_definition::values array is treated as the default. + * + * The number of possible options should be very limited, + * and must be less than RETRO_NUM_CORE_OPTION_VALUES_MAX. + * i.e. it should be feasible to cycle through options + * without a keyboard. + * + * First entry should be treated as a default. + * + * Example entry: + * { + * "foo_option", + * "Speed hack coprocessor X", + * "Provides increased performance at the expense of reduced accuracy", + * { + * { "false", NULL }, + * { "true", NULL }, + * { "unstable", "Turbo (Unstable)" }, + * { NULL, NULL }, + * }, + * "false" + * } + * + * Only strings are operated on. The possible values will + * generally be displayed and stored as-is by the frontend. + */ + +#define RETRO_ENVIRONMENT_SET_CORE_OPTIONS_INTL 54 + /* const struct retro_core_options_intl * -- + * Allows an implementation to signal the environment + * which variables it might want to check for later using + * GET_VARIABLE. + * This allows the frontend to present these variables to + * a user dynamically. + * This should only be called if RETRO_ENVIRONMENT_GET_ENHANCED_CORE_OPTIONS + * returns an API version of 1. + * This should be called instead of RETRO_ENVIRONMENT_SET_VARIABLES. + * This should be called the first time as early as + * possible (ideally in retro_set_environment). + * Afterwards it may be called again for the core to communicate + * updated options to the frontend, but the number of core + * options must not change from the number in the initial call. + * + * This is fundamentally the same as RETRO_ENVIRONMENT_SET_CORE_OPTIONS, + * with the addition of localisation support. The description of the + * RETRO_ENVIRONMENT_SET_CORE_OPTIONS callback should be consulted + * for further details. + * + * 'data' points to a retro_core_options_intl struct. + * + * retro_core_options_intl::us is a pointer to an array of + * retro_core_option_definition structs defining the US English + * core options implementation. It must point to a valid array. + * + * retro_core_options_intl::local is a pointer to an array of + * retro_core_option_definition structs defining core options for + * the current frontend language. It may be NULL (in which case + * retro_core_options_intl::us is used by the frontend). Any items + * missing from this array will be read from retro_core_options_intl::us + * instead. + * + * NOTE: Default core option values are always taken from the + * retro_core_options_intl::us array. Any default values in + * retro_core_options_intl::local array will be ignored. + */ + +#define RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY 55 + /* struct retro_core_option_display * -- + * + * Allows an implementation to signal the environment to show + * or hide a variable when displaying core options. This is + * considered a *suggestion*. The frontend is free to ignore + * this callback, and its implementation not considered mandatory. + * + * 'data' points to a retro_core_option_display struct + * + * retro_core_option_display::key is a variable identifier + * which has already been set by SET_VARIABLES/SET_CORE_OPTIONS. + * + * retro_core_option_display::visible is a boolean, specifying + * whether variable should be displayed + * + * Note that all core option variables will be set visible by + * default when calling SET_VARIABLES/SET_CORE_OPTIONS. + */ + +/* VFS functionality */ + +/* File paths: + * File paths passed as parameters when using this API shall be well formed UNIX-style, + * using "/" (unquoted forward slash) as directory separator regardless of the platform's native separator. + * Paths shall also include at least one forward slash ("game.bin" is an invalid path, use "./game.bin" instead). + * Other than the directory separator, cores shall not make assumptions about path format: + * "C:/path/game.bin", "http://example.com/game.bin", "#game/game.bin", "./game.bin" (without quotes) are all valid paths. + * Cores may replace the basename or remove path components from the end, and/or add new components; + * however, cores shall not append "./", "../" or multiple consecutive forward slashes ("//") to paths they request to front end. + * The frontend is encouraged to make such paths work as well as it can, but is allowed to give up if the core alters paths too much. + * Frontends are encouraged, but not required, to support native file system paths (modulo replacing the directory separator, if applicable). + * Cores are allowed to try using them, but must remain functional if the front rejects such requests. + * Cores are encouraged to use the libretro-common filestream functions for file I/O, + * as they seamlessly integrate with VFS, deal with directory separator replacement as appropriate + * and provide platform-specific fallbacks in cases where front ends do not support VFS. */ + +/* Opaque file handle + * Introduced in VFS API v1 */ +struct retro_vfs_file_handle; + +/* Opaque directory handle + * Introduced in VFS API v3 */ +struct retro_vfs_dir_handle; + +/* File open flags + * Introduced in VFS API v1 */ +#define RETRO_VFS_FILE_ACCESS_READ (1 << 0) /* Read only mode */ +#define RETRO_VFS_FILE_ACCESS_WRITE (1 << 1) /* Write only mode, discard contents and overwrites existing file unless RETRO_VFS_FILE_ACCESS_UPDATE is also specified */ +#define RETRO_VFS_FILE_ACCESS_READ_WRITE (RETRO_VFS_FILE_ACCESS_READ | RETRO_VFS_FILE_ACCESS_WRITE) /* Read-write mode, discard contents and overwrites existing file unless RETRO_VFS_FILE_ACCESS_UPDATE is also specified*/ +#define RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING (1 << 2) /* Prevents discarding content of existing files opened for writing */ + +/* These are only hints. The frontend may choose to ignore them. Other than RAM/CPU/etc use, + and how they react to unlikely external interference (for example someone else writing to that file, + or the file's server going down), behavior will not change. */ +#define RETRO_VFS_FILE_ACCESS_HINT_NONE (0) +/* Indicate that the file will be accessed many times. The frontend should aggressively cache everything. */ +#define RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS (1 << 0) + +/* Seek positions */ +#define RETRO_VFS_SEEK_POSITION_START 0 +#define RETRO_VFS_SEEK_POSITION_CURRENT 1 +#define RETRO_VFS_SEEK_POSITION_END 2 + +/* stat() result flags + * Introduced in VFS API v3 */ +#define RETRO_VFS_STAT_IS_VALID (1 << 0) +#define RETRO_VFS_STAT_IS_DIRECTORY (1 << 1) +#define RETRO_VFS_STAT_IS_CHARACTER_SPECIAL (1 << 2) + +/* Get path from opaque handle. Returns the exact same path passed to file_open when getting the handle + * Introduced in VFS API v1 */ +typedef const char *(RETRO_CALLCONV *retro_vfs_get_path_t)(struct retro_vfs_file_handle *stream); + +/* Open a file for reading or writing. If path points to a directory, this will + * fail. Returns the opaque file handle, or NULL for error. + * Introduced in VFS API v1 */ +typedef struct retro_vfs_file_handle *(RETRO_CALLCONV *retro_vfs_open_t)(const char *path, unsigned mode, unsigned hints); + +/* Close the file and release its resources. Must be called if open_file returns non-NULL. Returns 0 on success, -1 on failure. + * Whether the call succeeds ot not, the handle passed as parameter becomes invalid and should no longer be used. + * Introduced in VFS API v1 */ +typedef int (RETRO_CALLCONV *retro_vfs_close_t)(struct retro_vfs_file_handle *stream); + +/* Return the size of the file in bytes, or -1 for error. + * Introduced in VFS API v1 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_size_t)(struct retro_vfs_file_handle *stream); + +/* Truncate file to specified size. Returns 0 on success or -1 on error + * Introduced in VFS API v2 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_truncate_t)(struct retro_vfs_file_handle *stream, int64_t length); + +/* Get the current read / write position for the file. Returns -1 for error. + * Introduced in VFS API v1 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_tell_t)(struct retro_vfs_file_handle *stream); + +/* Set the current read/write position for the file. Returns the new position, -1 for error. + * Introduced in VFS API v1 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_seek_t)(struct retro_vfs_file_handle *stream, int64_t offset, int seek_position); + +/* Read data from a file. Returns the number of bytes read, or -1 for error. + * Introduced in VFS API v1 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_read_t)(struct retro_vfs_file_handle *stream, void *s, uint64_t len); + +/* Write data to a file. Returns the number of bytes written, or -1 for error. + * Introduced in VFS API v1 */ +typedef int64_t (RETRO_CALLCONV *retro_vfs_write_t)(struct retro_vfs_file_handle *stream, const void *s, uint64_t len); + +/* Flush pending writes to file, if using buffered IO. Returns 0 on sucess, or -1 on failure. + * Introduced in VFS API v1 */ +typedef int (RETRO_CALLCONV *retro_vfs_flush_t)(struct retro_vfs_file_handle *stream); + +/* Delete the specified file. Returns 0 on success, -1 on failure + * Introduced in VFS API v1 */ +typedef int (RETRO_CALLCONV *retro_vfs_remove_t)(const char *path); + +/* Rename the specified file. Returns 0 on success, -1 on failure + * Introduced in VFS API v1 */ +typedef int (RETRO_CALLCONV *retro_vfs_rename_t)(const char *old_path, const char *new_path); + +/* Stat the specified file. Retruns a bitmask of RETRO_VFS_STAT_* flags, none are set if path was not valid. + * Additionally stores file size in given variable, unless NULL is given. + * Introduced in VFS API v3 */ +typedef int (RETRO_CALLCONV *retro_vfs_stat_t)(const char *path, int32_t *size); + +/* Create the specified directory. Returns 0 on success, -1 on unknown failure, -2 if already exists. + * Introduced in VFS API v3 */ +typedef int (RETRO_CALLCONV *retro_vfs_mkdir_t)(const char *dir); + +/* Open the specified directory for listing. Returns the opaque dir handle, or NULL for error. + * Support for the include_hidden argument may vary depending on the platform. + * Introduced in VFS API v3 */ +typedef struct retro_vfs_dir_handle *(RETRO_CALLCONV *retro_vfs_opendir_t)(const char *dir, bool include_hidden); + +/* Read the directory entry at the current position, and move the read pointer to the next position. + * Returns true on success, false if already on the last entry. + * Introduced in VFS API v3 */ +typedef bool (RETRO_CALLCONV *retro_vfs_readdir_t)(struct retro_vfs_dir_handle *dirstream); + +/* Get the name of the last entry read. Returns a string on success, or NULL for error. + * The returned string pointer is valid until the next call to readdir or closedir. + * Introduced in VFS API v3 */ +typedef const char *(RETRO_CALLCONV *retro_vfs_dirent_get_name_t)(struct retro_vfs_dir_handle *dirstream); + +/* Check if the last entry read was a directory. Returns true if it was, false otherwise (or on error). + * Introduced in VFS API v3 */ +typedef bool (RETRO_CALLCONV *retro_vfs_dirent_is_dir_t)(struct retro_vfs_dir_handle *dirstream); + +/* Close the directory and release its resources. Must be called if opendir returns non-NULL. Returns 0 on success, -1 on failure. + * Whether the call succeeds ot not, the handle passed as parameter becomes invalid and should no longer be used. + * Introduced in VFS API v3 */ +typedef int (RETRO_CALLCONV *retro_vfs_closedir_t)(struct retro_vfs_dir_handle *dirstream); + +struct retro_vfs_interface +{ + /* VFS API v1 */ + retro_vfs_get_path_t get_path; + retro_vfs_open_t open; + retro_vfs_close_t close; + retro_vfs_size_t size; + retro_vfs_tell_t tell; + retro_vfs_seek_t seek; + retro_vfs_read_t read; + retro_vfs_write_t write; + retro_vfs_flush_t flush; + retro_vfs_remove_t remove; + retro_vfs_rename_t rename; + /* VFS API v2 */ + retro_vfs_truncate_t truncate; + /* VFS API v3 */ + retro_vfs_stat_t stat; + retro_vfs_mkdir_t mkdir; + retro_vfs_opendir_t opendir; + retro_vfs_readdir_t readdir; + retro_vfs_dirent_get_name_t dirent_get_name; + retro_vfs_dirent_is_dir_t dirent_is_dir; + retro_vfs_closedir_t closedir; +}; + +struct retro_vfs_interface_info +{ + /* Set by core: should this be higher than the version the front end supports, + * front end will return false in the RETRO_ENVIRONMENT_GET_VFS_INTERFACE call + * Introduced in VFS API v1 */ + uint32_t required_interface_version; + + /* Frontend writes interface pointer here. The frontend also sets the actual + * version, must be at least required_interface_version. + * Introduced in VFS API v1 */ + struct retro_vfs_interface *iface; +}; + +enum retro_hw_render_interface_type +{ + RETRO_HW_RENDER_INTERFACE_VULKAN = 0, + RETRO_HW_RENDER_INTERFACE_D3D9 = 1, + RETRO_HW_RENDER_INTERFACE_D3D10 = 2, + RETRO_HW_RENDER_INTERFACE_D3D11 = 3, + RETRO_HW_RENDER_INTERFACE_D3D12 = 4, + RETRO_HW_RENDER_INTERFACE_GSKIT_PS2 = 5, + RETRO_HW_RENDER_INTERFACE_DUMMY = INT_MAX +}; + +/* Base struct. All retro_hw_render_interface_* types + * contain at least these fields. */ +struct retro_hw_render_interface +{ + enum retro_hw_render_interface_type interface_type; + unsigned interface_version; +}; + +typedef void (RETRO_CALLCONV *retro_set_led_state_t)(int led, int state); +struct retro_led_interface +{ + retro_set_led_state_t set_led_state; +}; + +/* Retrieves the current state of the MIDI input. + * Returns true if it's enabled, false otherwise. */ +typedef bool (RETRO_CALLCONV *retro_midi_input_enabled_t)(void); + +/* Retrieves the current state of the MIDI output. + * Returns true if it's enabled, false otherwise */ +typedef bool (RETRO_CALLCONV *retro_midi_output_enabled_t)(void); + +/* Reads next byte from the input stream. + * Returns true if byte is read, false otherwise. */ +typedef bool (RETRO_CALLCONV *retro_midi_read_t)(uint8_t *byte); + +/* Writes byte to the output stream. + * 'delta_time' is in microseconds and represent time elapsed since previous write. + * Returns true if byte is written, false otherwise. */ +typedef bool (RETRO_CALLCONV *retro_midi_write_t)(uint8_t byte, uint32_t delta_time); + +/* Flushes previously written data. + * Returns true if successful, false otherwise. */ +typedef bool (RETRO_CALLCONV *retro_midi_flush_t)(void); + +struct retro_midi_interface +{ + retro_midi_input_enabled_t input_enabled; + retro_midi_output_enabled_t output_enabled; + retro_midi_read_t read; + retro_midi_write_t write; + retro_midi_flush_t flush; +}; enum retro_hw_render_context_negotiation_interface_type { @@ -968,77 +1488,97 @@ struct retro_hw_render_context_negotiation_interface enum retro_hw_render_context_negotiation_interface_type interface_type; unsigned interface_version; }; -#define RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE (43 | RETRO_ENVIRONMENT_EXPERIMENTAL) - /* const struct retro_hw_render_context_negotiation_interface * -- - * Sets an interface which lets the libretro core negotiate with frontend how a context is created. - * The semantics of this interface depends on which API is used in SET_HW_RENDER earlier. - * This interface will be used when the frontend is trying to create a HW rendering context, - * so it will be used after SET_HW_RENDER, but before the context_reset callback. - */ -#define RETRO_MEMDESC_CONST (1 << 0) /* The frontend will never change this memory area once retro_load_game has returned. */ -#define RETRO_MEMDESC_BIGENDIAN (1 << 1) /* The memory area contains big endian data. Default is little endian. */ -#define RETRO_MEMDESC_ALIGN_2 (1 << 16) /* All memory access in this area is aligned to their own size, or 2, whichever is smaller. */ -#define RETRO_MEMDESC_ALIGN_4 (2 << 16) -#define RETRO_MEMDESC_ALIGN_8 (3 << 16) -#define RETRO_MEMDESC_MINSIZE_2 (1 << 24) /* All memory in this region is accessed at least 2 bytes at the time. */ -#define RETRO_MEMDESC_MINSIZE_4 (2 << 24) -#define RETRO_MEMDESC_MINSIZE_8 (3 << 24) +/* Serialized state is incomplete in some way. Set if serialization is + * usable in typical end-user cases but should not be relied upon to + * implement frame-sensitive frontend features such as netplay or + * rerecording. */ +#define RETRO_SERIALIZATION_QUIRK_INCOMPLETE (1 << 0) +/* The core must spend some time initializing before serialization is + * supported. retro_serialize() will initially fail; retro_unserialize() + * and retro_serialize_size() may or may not work correctly either. */ +#define RETRO_SERIALIZATION_QUIRK_MUST_INITIALIZE (1 << 1) +/* Serialization size may change within a session. */ +#define RETRO_SERIALIZATION_QUIRK_CORE_VARIABLE_SIZE (1 << 2) +/* Set by the frontend to acknowledge that it supports variable-sized + * states. */ +#define RETRO_SERIALIZATION_QUIRK_FRONT_VARIABLE_SIZE (1 << 3) +/* Serialized state can only be loaded during the same session. */ +#define RETRO_SERIALIZATION_QUIRK_SINGLE_SESSION (1 << 4) +/* Serialized state cannot be loaded on an architecture with a different + * endianness from the one it was saved on. */ +#define RETRO_SERIALIZATION_QUIRK_ENDIAN_DEPENDENT (1 << 5) +/* Serialized state cannot be loaded on a different platform from the one it + * was saved on for reasons other than endianness, such as word size + * dependence */ +#define RETRO_SERIALIZATION_QUIRK_PLATFORM_DEPENDENT (1 << 6) + +#define RETRO_MEMDESC_CONST (1 << 0) /* The frontend will never change this memory area once retro_load_game has returned. */ +#define RETRO_MEMDESC_BIGENDIAN (1 << 1) /* The memory area contains big endian data. Default is little endian. */ +#define RETRO_MEMDESC_SYSTEM_RAM (1 << 2) /* The memory area is system RAM. This is main RAM of the gaming system. */ +#define RETRO_MEMDESC_SAVE_RAM (1 << 3) /* The memory area is save RAM. This RAM is usually found on a game cartridge, backed up by a battery. */ +#define RETRO_MEMDESC_VIDEO_RAM (1 << 4) /* The memory area is video RAM (VRAM) */ +#define RETRO_MEMDESC_ALIGN_2 (1 << 16) /* All memory access in this area is aligned to their own size, or 2, whichever is smaller. */ +#define RETRO_MEMDESC_ALIGN_4 (2 << 16) +#define RETRO_MEMDESC_ALIGN_8 (3 << 16) +#define RETRO_MEMDESC_MINSIZE_2 (1 << 24) /* All memory in this region is accessed at least 2 bytes at the time. */ +#define RETRO_MEMDESC_MINSIZE_4 (2 << 24) +#define RETRO_MEMDESC_MINSIZE_8 (3 << 24) struct retro_memory_descriptor { uint64_t flags; /* Pointer to the start of the relevant ROM or RAM chip. - * It's strongly recommended to use 'offset' if possible, rather than + * It's strongly recommended to use 'offset' if possible, rather than * doing math on the pointer. * - * If the same byte is mapped my multiple descriptors, their descriptors + * If the same byte is mapped my multiple descriptors, their descriptors * must have the same pointer. - * If 'start' does not point to the first byte in the pointer, put the + * If 'start' does not point to the first byte in the pointer, put the * difference in 'offset' instead. * - * May be NULL if there's nothing usable here (e.g. hardware registers and + * May be NULL if there's nothing usable here (e.g. hardware registers and * open bus). No flags should be set if the pointer is NULL. * It's recommended to minimize the number of descriptors if possible, * but not mandatory. */ void *ptr; size_t offset; - /* This is the location in the emulated address space + /* This is the location in the emulated address space * where the mapping starts. */ size_t start; /* Which bits must be same as in 'start' for this mapping to apply. - * The first memory descriptor to claim a certain byte is the one + * The first memory descriptor to claim a certain byte is the one * that applies. * A bit which is set in 'start' must also be set in this. - * Can be zero, in which case each byte is assumed mapped exactly once. + * Can be zero, in which case each byte is assumed mapped exactly once. * In this case, 'len' must be a power of two. */ size_t select; - /* If this is nonzero, the set bits are assumed not connected to the + /* If this is nonzero, the set bits are assumed not connected to the * memory chip's address pins. */ size_t disconnect; /* This one tells the size of the current memory area. - * If, after start+disconnect are applied, the address is higher than + * If, after start+disconnect are applied, the address is higher than * this, the highest bit of the address is cleared. * * If the address is still too high, the next highest bit is cleared. - * Can be zero, in which case it's assumed to be infinite (as limited + * Can be zero, in which case it's assumed to be infinite (as limited * by 'select' and 'disconnect'). */ size_t len; - /* To go from emulated address to physical address, the following + /* To go from emulated address to physical address, the following * order applies: * Subtract 'start', pick off 'disconnect', apply 'len', add 'offset'. */ - /* The address space name must consist of only a-zA-Z0-9_-, + /* The address space name must consist of only a-zA-Z0-9_-, * should be as short as feasible (maximum length is 8 plus the NUL), - * and may not be any other address space plus one or more 0-9A-F + * and may not be any other address space plus one or more 0-9A-F * at the end. - * However, multiple memory descriptors for the same address space is - * allowed, and the address space name can be empty. NULL is treated + * However, multiple memory descriptors for the same address space is + * allowed, and the address space name can be empty. NULL is treated * as empty. * * Address space names are case sensitive, but avoid lowercase if possible. @@ -1052,11 +1592,11 @@ struct retro_memory_descriptor * 'a'+blank - valid ('a' is not in 0-9A-F) * 'a'+'A' - valid (neither is a prefix of each other) * 'AR'+blank - valid ('R' is not in 0-9A-F) - * 'ARB'+blank - valid (the B can't be part of the address either, because + * 'ARB'+blank - valid (the B can't be part of the address either, because * there is no namespace 'AR') - * blank+'B' - not valid, because it's ambigous which address space B1234 + * blank+'B' - not valid, because it's ambigous which address space B1234 * would refer to. - * The length can't be used for that purpose; the frontend may want + * The length can't be used for that purpose; the frontend may want * to append arbitrary data to an address, without a separator. */ const char *addrspace; @@ -1078,32 +1618,32 @@ struct retro_memory_descriptor * the most recent addition and continue on the next bit. * TODO: Can the above be optimized? Is "remove the lowest bit set in both * pointer and 'len'" equivalent? */ - + /* TODO: Some emulators (MAME?) emulate big endian systems by only accessing * the emulated memory in 32-bit chunks, native endian. But that's nothing * compared to Darek Mihocka * (section Emulation 103 - Nearly Free Byte Reversal) - he flips the ENTIRE * RAM backwards! I'll want to represent both of those, via some flags. - * + * * I suspect MAME either didn't think of that idea, or don't want the #ifdef. * Not sure which, nor do I really care. */ - + /* TODO: Some of those flags are unused and/or don't really make sense. Clean * them up. */ }; -/* The frontend may use the largest value of 'start'+'select' in a +/* The frontend may use the largest value of 'start'+'select' in a * certain namespace to infer the size of the address space. * - * If the address space is larger than that, a mapping with .ptr=NULL - * should be at the end of the array, with .select set to all ones for + * If the address space is larger than that, a mapping with .ptr=NULL + * should be at the end of the array, with .select set to all ones for * as long as the address space is big. * * Sample descriptors (minus .ptr, and RETRO_MEMFLAG_ on the flags): * SNES WRAM: * .start=0x7E0000, .len=0x20000 - * (Note that this must be mapped before the ROM in most cases; some of the - * ROM mappers + * (Note that this must be mapped before the ROM in most cases; some of the + * ROM mappers * try to claim $7E0000, or at least $7E8000.) * SNES SPC700 RAM: * .addrspace="S", .len=0x10000 @@ -1112,7 +1652,7 @@ struct retro_memory_descriptor * .flags=MIRROR, .start=0x800000, .select=0xC0E000, .len=0x2000 * SNES WRAM mirrors, alternate equivalent descriptor: * .flags=MIRROR, .select=0x40E000, .disconnect=~0x1FFF - * (Various similar constructions can be created by combining parts of + * (Various similar constructions can be created by combining parts of * the above two.) * SNES LoROM (512KB, mirrored a couple of times): * .flags=CONST, .start=0x008000, .select=0x408000, .disconnect=0x8000, .len=512*1024 @@ -1138,13 +1678,13 @@ struct retro_memory_map struct retro_controller_description { - /* Human-readable description of the controller. Even if using a generic - * input device type, this can be set to the particular device type the + /* Human-readable description of the controller. Even if using a generic + * input device type, this can be set to the particular device type the * core uses. */ const char *desc; - /* Device type passed to retro_set_controller_port_device(). If the device - * type is a sub-class of a generic input device type, use the + /* Device type passed to retro_set_controller_port_device(). If the device + * type is a sub-class of a generic input device type, use the * RETRO_DEVICE_SUBCLASS macro to create an ID. * * E.g. RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 1). */ @@ -1162,8 +1702,8 @@ struct retro_subsystem_memory_info /* The extension associated with a memory type, e.g. "psram". */ const char *extension; - /* The memory type for retro_get_memory(). This should be at - * least 0x100 to avoid conflict with standardized + /* The memory type for retro_get_memory(). This should be at + * least 0x100 to avoid conflict with standardized * libretro memory types. */ unsigned type; }; @@ -1182,11 +1722,11 @@ struct retro_subsystem_rom_info /* Same definition as retro_get_system_info(). */ bool block_extract; - /* This is set if the content is required to load a game. + /* This is set if the content is required to load a game. * If this is set to false, a zeroed-out retro_game_info can be passed. */ bool required; - /* Content can have multiple associated persistent + /* Content can have multiple associated persistent * memory types (retro_get_memory()). */ const struct retro_subsystem_memory_info *memory; unsigned num_memory; @@ -1204,17 +1744,17 @@ struct retro_subsystem_info */ const char *ident; - /* Infos for each content file. The first entry is assumed to be the + /* Infos for each content file. The first entry is assumed to be the * "most significant" content for frontend purposes. - * E.g. with Super GameBoy, the first content should be the GameBoy ROM, + * E.g. with Super GameBoy, the first content should be the GameBoy ROM, * as it is the most "significant" content to a user. - * If a frontend creates new file paths based on the content used + * If a frontend creates new file paths based on the content used * (e.g. savestates), it should use the path for the first ROM to do so. */ const struct retro_subsystem_rom_info *roms; /* Number of content files associated with a subsystem. */ unsigned num_roms; - + /* The type passed to retro_load_game_special(). */ unsigned id; }; @@ -1225,13 +1765,13 @@ typedef void (RETRO_CALLCONV *retro_proc_address_t)(void); * (None here so far). * * Get a symbol from a libretro core. - * Cores should only return symbols which are actual + * Cores should only return symbols which are actual * extensions to the libretro API. * - * Frontends should not use this to obtain symbols to standard + * Frontends should not use this to obtain symbols to standard * libretro entry points (static linking or dlsym). * - * The symbol name must be equal to the function name, + * The symbol name must be equal to the function name, * e.g. if void retro_foo(void); exists, the symbol must be called "retro_foo". * The returned function pointer must be cast to the corresponding type. */ @@ -1285,6 +1825,7 @@ struct retro_log_callback #define RETRO_SIMD_POPCNT (1 << 18) #define RETRO_SIMD_MOVBE (1 << 19) #define RETRO_SIMD_CMOV (1 << 20) +#define RETRO_SIMD_ASIMD (1 << 21) typedef uint64_t retro_perf_tick_t; typedef int64_t retro_time_t; @@ -1305,7 +1846,7 @@ struct retro_perf_counter typedef retro_time_t (RETRO_CALLCONV *retro_perf_get_time_usec_t)(void); /* A simple counter. Usually nanoseconds, but can also be CPU cycles. - * Can be used directly if desired (when creating a more sophisticated + * Can be used directly if desired (when creating a more sophisticated * performance counter system). * */ typedef retro_perf_tick_t (RETRO_CALLCONV *retro_perf_get_counter_t)(void); @@ -1319,9 +1860,9 @@ typedef uint64_t (RETRO_CALLCONV *retro_get_cpu_features_t)(void); typedef void (RETRO_CALLCONV *retro_perf_log_t)(void); /* Register a performance counter. - * ident field must be set with a discrete value and other values in + * ident field must be set with a discrete value and other values in * retro_perf_counter must be 0. - * Registering can be called multiple times. To avoid calling to + * Registering can be called multiple times. To avoid calling to * frontend redundantly, you can check registered field first. */ typedef void (RETRO_CALLCONV *retro_perf_register_t)(struct retro_perf_counter *counter); @@ -1392,7 +1933,7 @@ enum retro_sensor_action #define RETRO_SENSOR_ACCELEROMETER_Y 1 #define RETRO_SENSOR_ACCELEROMETER_Z 2 -typedef bool (RETRO_CALLCONV *retro_set_sensor_state_t)(unsigned port, +typedef bool (RETRO_CALLCONV *retro_set_sensor_state_t)(unsigned port, enum retro_sensor_action action, unsigned rate); typedef float (RETRO_CALLCONV *retro_sensor_get_input_t)(unsigned port, unsigned id); @@ -1417,7 +1958,7 @@ typedef bool (RETRO_CALLCONV *retro_camera_start_t)(void); /* Stops the camera driver. Can only be called in retro_run(). */ typedef void (RETRO_CALLCONV *retro_camera_stop_t)(void); -/* Callback which signals when the camera driver is initialized +/* Callback which signals when the camera driver is initialized * and/or deinitialized. * retro_camera_start_t can be called in initialized callback. */ @@ -1427,36 +1968,36 @@ typedef void (RETRO_CALLCONV *retro_camera_lifetime_status_t)(void); * Width, height and pitch are similar to retro_video_refresh_t. * First pixel is top-left origin. */ -typedef void (RETRO_CALLCONV *retro_camera_frame_raw_framebuffer_t)(const uint32_t *buffer, +typedef void (RETRO_CALLCONV *retro_camera_frame_raw_framebuffer_t)(const uint32_t *buffer, unsigned width, unsigned height, size_t pitch); /* A callback for when OpenGL textures are used. * * texture_id is a texture owned by camera driver. - * Its state or content should be considered immutable, except for things like + * Its state or content should be considered immutable, except for things like * texture filtering and clamping. * * texture_target is the texture target for the GL texture. - * These can include e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE, and possibly + * These can include e.g. GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE, and possibly * more depending on extensions. * - * affine points to a packed 3x3 column-major matrix used to apply an affine + * affine points to a packed 3x3 column-major matrix used to apply an affine * transform to texture coordinates. (affine_matrix * vec3(coord_x, coord_y, 1.0)) - * After transform, normalized texture coord (0, 0) should be bottom-left + * After transform, normalized texture coord (0, 0) should be bottom-left * and (1, 1) should be top-right (or (width, height) for RECTANGLE). * - * GL-specific typedefs are avoided here to avoid relying on gl.h in + * GL-specific typedefs are avoided here to avoid relying on gl.h in * the API definition. */ -typedef void (RETRO_CALLCONV *retro_camera_frame_opengl_texture_t)(unsigned texture_id, +typedef void (RETRO_CALLCONV *retro_camera_frame_opengl_texture_t)(unsigned texture_id, unsigned texture_target, const float *affine); struct retro_camera_callback { - /* Set by libretro core. + /* Set by libretro core. * Example bitmask: caps = (1 << RETRO_CAMERA_BUFFER_OPENGL_TEXTURE) | (1 << RETRO_CAMERA_BUFFER_RAW_FRAMEBUFFER). */ - uint64_t caps; + uint64_t caps; /* Desired resolution for camera. Is only used as a hint. */ unsigned width; @@ -1470,22 +2011,22 @@ struct retro_camera_callback retro_camera_frame_raw_framebuffer_t frame_raw_framebuffer; /* Set by libretro core if OpenGL texture callbacks will be used. */ - retro_camera_frame_opengl_texture_t frame_opengl_texture; + retro_camera_frame_opengl_texture_t frame_opengl_texture; - /* Set by libretro core. Called after camera driver is initialized and + /* Set by libretro core. Called after camera driver is initialized and * ready to be started. * Can be NULL, in which this callback is not called. */ retro_camera_lifetime_status_t initialized; - /* Set by libretro core. Called right before camera driver is + /* Set by libretro core. Called right before camera driver is * deinitialized. * Can be NULL, in which this callback is not called. */ retro_camera_lifetime_status_t deinitialized; }; -/* Sets the interval of time and/or distance at which to update/poll +/* Sets the interval of time and/or distance at which to update/poll * location-based data. * * To ensure compatibility with all location-based implementations, @@ -1498,20 +2039,20 @@ typedef void (RETRO_CALLCONV *retro_location_set_interval_t)(unsigned interval_m unsigned interval_distance); /* Start location services. The device will start listening for changes to the - * current location at regular intervals (which are defined with + * current location at regular intervals (which are defined with * retro_location_set_interval_t). */ typedef bool (RETRO_CALLCONV *retro_location_start_t)(void); -/* Stop location services. The device will stop listening for changes +/* Stop location services. The device will stop listening for changes * to the current location. */ typedef void (RETRO_CALLCONV *retro_location_stop_t)(void); -/* Get the position of the current location. Will set parameters to +/* Get the position of the current location. Will set parameters to * 0 if no new location update has happened since the last time. */ typedef bool (RETRO_CALLCONV *retro_location_get_position_t)(double *lat, double *lon, double *horiz_accuracy, double *vert_accuracy); -/* Callback which signals when the location driver is initialized +/* Callback which signals when the location driver is initialized * and/or deinitialized. * retro_location_start_t can be called in initialized callback. */ @@ -1536,14 +2077,14 @@ enum retro_rumble_effect RETRO_RUMBLE_DUMMY = INT_MAX }; -/* Sets rumble state for joypad plugged in port 'port'. +/* Sets rumble state for joypad plugged in port 'port'. * Rumble effects are controlled independently, * and setting e.g. strong rumble does not override weak rumble. * Strength has a range of [0, 0xffff]. * - * Returns true if rumble state request was honored. + * Returns true if rumble state request was honored. * Calling this before first retro_run() is likely to return false. */ -typedef bool (RETRO_CALLCONV *retro_set_rumble_state_t)(unsigned port, +typedef bool (RETRO_CALLCONV *retro_set_rumble_state_t)(unsigned port, enum retro_rumble_effect effect, uint16_t strength); struct retro_rumble_interface @@ -1554,10 +2095,10 @@ struct retro_rumble_interface /* Notifies libretro that audio data should be written. */ typedef void (RETRO_CALLCONV *retro_audio_callback_t)(void); -/* True: Audio driver in frontend is active, and callback is +/* True: Audio driver in frontend is active, and callback is * expected to be called regularily. - * False: Audio driver in frontend is paused or inactive. - * Audio callback will not be called until set_state has been + * False: Audio driver in frontend is paused or inactive. + * Audio callback will not be called until set_state has been * called with true. * Initial state is false (inactive). */ @@ -1569,11 +2110,11 @@ struct retro_audio_callback retro_audio_set_state_callback_t set_state; }; -/* Notifies a libretro core of time spent since last invocation +/* Notifies a libretro core of time spent since last invocation * of retro_run() in microseconds. * * It will be called right before retro_run() every frame. - * The frontend can tamper with timing to support cases like + * The frontend can tamper with timing to support cases like * fast-forward, slow-motion and framestepping. * * In those scenarios the reference frame time value will be used. */ @@ -1582,8 +2123,8 @@ typedef void (RETRO_CALLCONV *retro_frame_time_callback_t)(retro_usec_t usec); struct retro_frame_time_callback { retro_frame_time_callback_t callback; - /* Represents the time of one frame. It is computed as - * 1000000 / fps, but the implementation will resolve the + /* Represents the time of one frame. It is computed as + * 1000000 / fps, but the implementation will resolve the * rounding to ensure that framestepping, etc is exact. */ retro_usec_t reference; }; @@ -1599,7 +2140,7 @@ struct retro_frame_time_callback * it should implement context_destroy callback. * If called, all GPU resources must be reinitialized. * Usually called when frontend reinits video driver. - * Also called first time video driver is initialized, + * Also called first time video driver is initialized, * allowing libretro core to initialize resources. */ typedef void (RETRO_CALLCONV *retro_hw_context_reset_t)(void); @@ -1616,7 +2157,7 @@ enum retro_hw_context_type { RETRO_HW_CONTEXT_NONE = 0, /* OpenGL 2.x. Driver can choose to use latest compatibility context. */ - RETRO_HW_CONTEXT_OPENGL = 1, + RETRO_HW_CONTEXT_OPENGL = 1, /* OpenGL ES 2.0. */ RETRO_HW_CONTEXT_OPENGLES2 = 2, /* Modern desktop core GL context. Use version_major/ @@ -1631,6 +2172,10 @@ enum retro_hw_context_type /* Vulkan, see RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE. */ RETRO_HW_CONTEXT_VULKAN = 6, + /* Direct3D, set version_major to select the type of interface + * returned by RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE */ + RETRO_HW_CONTEXT_DIRECT3D = 7, + RETRO_HW_CONTEXT_DUMMY = INT_MAX }; @@ -1642,10 +2187,10 @@ struct retro_hw_render_callback /* Called when a context has been created or when it has been reset. * An OpenGL context is only valid after context_reset() has been called. * - * When context_reset is called, OpenGL resources in the libretro + * When context_reset is called, OpenGL resources in the libretro * implementation are guaranteed to be invalid. * - * It is possible that context_reset is called multiple times during an + * It is possible that context_reset is called multiple times during an * application lifecycle. * If context_reset is called without any notification (context_destroy), * the OpenGL context was lost and resources should just be recreated @@ -1658,7 +2203,8 @@ struct retro_hw_render_callback * be providing preallocated framebuffers. */ retro_hw_get_current_framebuffer_t get_current_framebuffer; - /* Set by frontend. */ + /* Set by frontend. + * Can return all relevant functions, including glClear on Windows. */ retro_hw_get_proc_address_t get_proc_address; /* Set if render buffers should have depth component attached. @@ -1669,48 +2215,48 @@ struct retro_hw_render_callback * TODO: Obsolete. */ bool stencil; - /* If depth and stencil are true, a packed 24/8 buffer will be added. + /* If depth and stencil are true, a packed 24/8 buffer will be added. * Only attaching stencil is invalid and will be ignored. */ - /* Use conventional bottom-left origin convention. If false, + /* Use conventional bottom-left origin convention. If false, * standard libretro top-left origin semantics are used. * TODO: Move to GL specific interface. */ bool bottom_left_origin; - + /* Major version number for core GL context or GLES 3.1+. */ unsigned version_major; /* Minor version number for core GL context or GLES 3.1+. */ unsigned version_minor; - /* If this is true, the frontend will go very far to avoid + /* If this is true, the frontend will go very far to avoid * resetting context in scenarios like toggling fullscreen, etc. * TODO: Obsolete? Maybe frontend should just always assume this ... */ bool cache_context; - /* The reset callback might still be called in extreme situations + /* The reset callback might still be called in extreme situations * such as if the context is lost beyond recovery. * - * For optimal stability, set this to false, and allow context to be + * For optimal stability, set this to false, and allow context to be * reset at any time. */ - - /* A callback to be called before the context is destroyed in a + + /* A callback to be called before the context is destroyed in a * controlled way by the frontend. */ retro_hw_context_reset_t context_destroy; /* OpenGL resources can be deinitialized cleanly at this step. - * context_destroy can be set to NULL, in which resources will + * context_destroy can be set to NULL, in which resources will * just be destroyed without any notification. * - * Even when context_destroy is non-NULL, it is possible that + * Even when context_destroy is non-NULL, it is possible that * context_reset is called without any destroy notification. - * This happens if context is lost by external factors (such as + * This happens if context is lost by external factors (such as * notified by GL_ARB_robustness). * * In this case, the context is assumed to be already dead, - * and the libretro implementation must not try to free any OpenGL + * and the libretro implementation must not try to free any OpenGL * resources in the subsequent context_reset. */ @@ -1718,7 +2264,7 @@ struct retro_hw_render_callback bool debug_context; }; -/* Callback type passed in RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK. +/* Callback type passed in RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK. * Called by the frontend in response to keyboard events. * down is set if the key is being pressed, or false if it is being released. * keycode is the RETROK value of the char. @@ -1726,16 +2272,16 @@ struct retro_hw_render_callback * key_modifiers is a set of RETROKMOD values or'ed together. * * The pressed/keycode state can be indepedent of the character. - * It is also possible that multiple characters are generated from a + * It is also possible that multiple characters are generated from a * single keypress. * Keycode events should be treated separately from character events. * However, when possible, the frontend should try to synchronize these. * If only a character is posted, keycode should be RETROK_UNKNOWN. * - * Similarily if only a keycode event is generated with no corresponding + * Similarily if only a keycode event is generated with no corresponding * character, character should be 0. */ -typedef void (RETRO_CALLCONV *retro_keyboard_event_t)(bool down, unsigned keycode, +typedef void (RETRO_CALLCONV *retro_keyboard_event_t)(bool down, unsigned keycode, uint32_t character, uint16_t key_modifiers); struct retro_keyboard_callback @@ -1744,15 +2290,15 @@ struct retro_keyboard_callback }; /* Callbacks for RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE. - * Should be set for implementations which can swap out multiple disk + * Should be set for implementations which can swap out multiple disk * images in runtime. * * If the implementation can do this automatically, it should strive to do so. * However, there are cases where the user must manually do so. * - * Overview: To swap a disk image, eject the disk image with + * Overview: To swap a disk image, eject the disk image with * set_eject_state(true). - * Set the disk index with set_image_index(index). Insert the disk again + * Set the disk index with set_image_index(index). Insert the disk again * with set_eject_state(false). */ @@ -1770,7 +2316,7 @@ typedef bool (RETRO_CALLCONV *retro_get_eject_state_t)(void); typedef unsigned (RETRO_CALLCONV *retro_get_image_index_t)(void); /* Sets image index. Can only be called when disk is ejected. - * The implementation supports setting "no disk" by using an + * The implementation supports setting "no disk" by using an * index >= get_num_images(). */ typedef bool (RETRO_CALLCONV *retro_set_image_index_t)(unsigned index); @@ -1784,11 +2330,11 @@ struct retro_game_info; * Arguments to pass in info have same requirements as retro_load_game(). * Virtual disk tray must be ejected when calling this. * - * Replacing a disk image with info = NULL will remove the disk image + * Replacing a disk image with info = NULL will remove the disk image * from the internal list. * As a result, calls to get_image_index() can change. * - * E.g. replace_image_index(1, NULL), and previous get_image_index() + * E.g. replace_image_index(1, NULL), and previous get_image_index() * returned 4 before. * Index 1 will be removed, and the new index is 3. */ @@ -1797,7 +2343,7 @@ typedef bool (RETRO_CALLCONV *retro_replace_image_index_t)(unsigned index, /* Adds a new valid index (get_num_images()) to the internal disk list. * This will increment subsequent return values from get_num_images() by 1. - * This image index cannot be used until a disk image has been set + * This image index cannot be used until a disk image has been set * with replace_image_index. */ typedef bool (RETRO_CALLCONV *retro_add_image_index_t)(void); @@ -1828,7 +2374,7 @@ enum retro_pixel_format /* RGB565, native endian. * This pixel format is the recommended format to use if a 15/16-bit - * format is desired as it is the pixel format that is typically + * format is desired as it is the pixel format that is typically * available on a wide range of low-power devices. * * It is also natively supported in APIs like OpenGL ES. */ @@ -1858,43 +2404,52 @@ struct retro_input_descriptor /* Human readable description for parameters. * The pointer must remain valid until * retro_unload_game() is called. */ - const char *description; + const char *description; }; struct retro_system_info { - /* All pointers are owned by libretro implementation, and pointers must + /* All pointers are owned by libretro implementation, and pointers must * remain valid until retro_deinit() is called. */ - const char *library_name; /* Descriptive name of library. Should not + const char *library_name; /* Descriptive name of library. Should not * contain any version numbers, etc. */ const char *library_version; /* Descriptive version of core. */ - const char *valid_extensions; /* A string listing probably content - * extensions the core will be able to + const char *valid_extensions; /* A string listing probably content + * extensions the core will be able to * load, separated with pipe. * I.e. "bin|rom|iso". - * Typically used for a GUI to filter + * Typically used for a GUI to filter * out extensions. */ - /* If true, retro_load_game() is guaranteed to provide a valid pathname - * in retro_game_info::path. - * ::data and ::size are both invalid. + /* Libretro cores that need to have direct access to their content + * files, including cores which use the path of the content files to + * determine the paths of other files, should set need_fullpath to true. * - * If false, ::data and ::size are guaranteed to be valid, but ::path - * might not be valid. + * Cores should strive for setting need_fullpath to false, + * as it allows the frontend to perform patching, etc. * - * This is typically set to true for libretro implementations that must - * load from file. - * Implementations should strive for setting this to false, as it allows - * the frontend to perform patching, etc. */ - bool need_fullpath; + * If need_fullpath is true and retro_load_game() is called: + * - retro_game_info::path is guaranteed to have a valid path + * - retro_game_info::data and retro_game_info::size are invalid + * + * If need_fullpath is false and retro_load_game() is called: + * - retro_game_info::path may be NULL + * - retro_game_info::data and retro_game_info::size are guaranteed + * to be valid + * + * See also: + * - RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY + * - RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY + */ + bool need_fullpath; - /* If true, the frontend is not allowed to extract any archives before + /* If true, the frontend is not allowed to extract any archives before * loading the real content. - * Necessary for certain libretro implementations that load games + * Necessary for certain libretro implementations that load games * from zipped archives. */ - bool block_extract; + bool block_extract; }; struct retro_game_geometry @@ -1926,27 +2481,87 @@ struct retro_system_av_info struct retro_variable { /* Variable to query in RETRO_ENVIRONMENT_GET_VARIABLE. - * If NULL, obtains the complete environment string if more + * If NULL, obtains the complete environment string if more * complex parsing is necessary. - * The environment string is formatted as key-value pairs + * The environment string is formatted as key-value pairs * delimited by semicolons as so: * "key1=value1;key2=value2;..." */ const char *key; - + /* Value to be obtained. If key does not exist, it is set to NULL. */ const char *value; }; +struct retro_core_option_display +{ + /* Variable to configure in RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY */ + const char *key; + + /* Specifies whether variable should be displayed + * when presenting core options to the user */ + bool visible; +}; + +/* Maximum number of values permitted for a core option + * NOTE: This may be increased on a core-by-core basis + * if required (doing so has no effect on the frontend) */ +#define RETRO_NUM_CORE_OPTION_VALUES_MAX 128 + +struct retro_core_option_value +{ + /* Expected option value */ + const char *value; + + /* Human-readable value label. If NULL, value itself + * will be displayed by the frontend */ + const char *label; +}; + +struct retro_core_option_definition +{ + /* Variable to query in RETRO_ENVIRONMENT_GET_VARIABLE. */ + const char *key; + + /* Human-readable core option description (used as menu label) */ + const char *desc; + + /* Human-readable core option information (used as menu sublabel) */ + const char *info; + + /* Array of retro_core_option_value structs, terminated by NULL */ + struct retro_core_option_value values[RETRO_NUM_CORE_OPTION_VALUES_MAX]; + + /* Default core option value. Must match one of the values + * in the retro_core_option_value array, otherwise will be + * ignored */ + const char *default_value; +}; + +struct retro_core_options_intl +{ + /* Pointer to an array of retro_core_option_definition structs + * - US English implementation + * - Must point to a valid array */ + struct retro_core_option_definition *us; + + /* Pointer to an array of retro_core_option_definition structs + * - Implementation for current frontend language + * - May be NULL */ + struct retro_core_option_definition *local; +}; + struct retro_game_info { const char *path; /* Path to game, UTF-8 encoded. - * Usually used as a reference. - * May be NULL if rom was loaded from stdin - * or similar. - * retro_system_info::need_fullpath guaranteed + * Sometimes used as a reference for building other paths. + * May be NULL if game was loaded from stdin or similar, + * but in this case some cores will be unable to load `data`. + * So, it is preferable to fabricate something here instead + * of passing NULL, which will help more cores to succeed. + * retro_system_info::need_fullpath requires * that this path is valid. */ - const void *data; /* Memory buffer of loaded game. Will be NULL + const void *data; /* Memory buffer of loaded game. Will be NULL * if need_fullpath was set. */ size_t size; /* Size of memory buffer. */ const char *meta; /* String of implementation specific meta-data. */ @@ -1984,25 +2599,25 @@ struct retro_framebuffer /* Callbacks */ -/* Environment callback. Gives implementations a way of performing +/* Environment callback. Gives implementations a way of performing * uncommon tasks. Extensible. */ typedef bool (RETRO_CALLCONV *retro_environment_t)(unsigned cmd, void *data); -/* Render a frame. Pixel format is 15-bit 0RGB1555 native endian +/* Render a frame. Pixel format is 15-bit 0RGB1555 native endian * unless changed (see RETRO_ENVIRONMENT_SET_PIXEL_FORMAT). * * Width and height specify dimensions of buffer. * Pitch specifices length in bytes between two lines in buffer. * - * For performance reasons, it is highly recommended to have a frame + * For performance reasons, it is highly recommended to have a frame * that is packed in memory, i.e. pitch == width * byte_per_pixel. - * Certain graphic APIs, such as OpenGL ES, do not like textures + * Certain graphic APIs, such as OpenGL ES, do not like textures * that are not packed in memory. */ typedef void (RETRO_CALLCONV *retro_video_refresh_t)(const void *data, unsigned width, unsigned height, size_t pitch); -/* Renders a single audio frame. Should only be used if implementation +/* Renders a single audio frame. Should only be used if implementation * generates a single sample at a time. * Format is signed 16-bit native endian. */ @@ -2020,20 +2635,20 @@ typedef size_t (RETRO_CALLCONV *retro_audio_sample_batch_t)(const int16_t *data, /* Polls input. */ typedef void (RETRO_CALLCONV *retro_input_poll_t)(void); -/* Queries for input for player 'port'. device will be masked with +/* Queries for input for player 'port'. device will be masked with * RETRO_DEVICE_MASK. * - * Specialization of devices such as RETRO_DEVICE_JOYPAD_MULTITAP that + * Specialization of devices such as RETRO_DEVICE_JOYPAD_MULTITAP that * have been set with retro_set_controller_port_device() * will still use the higher level RETRO_DEVICE_JOYPAD to request input. */ -typedef int16_t (RETRO_CALLCONV *retro_input_state_t)(unsigned port, unsigned device, +typedef int16_t (RETRO_CALLCONV *retro_input_state_t)(unsigned port, unsigned device, unsigned index, unsigned id); -/* Sets callbacks. retro_set_environment() is guaranteed to be called +/* Sets callbacks. retro_set_environment() is guaranteed to be called * before retro_init(). * - * The rest of the set_* functions are guaranteed to have been called + * The rest of the set_* functions are guaranteed to have been called * before the first call to retro_run() is made. */ RETRO_API void retro_set_environment(retro_environment_t); RETRO_API void retro_set_video_refresh(retro_video_refresh_t); @@ -2050,27 +2665,33 @@ RETRO_API void retro_deinit(void); * when the API is revised. */ RETRO_API unsigned retro_api_version(void); -/* Gets statically known system info. Pointers provided in *info +/* Gets statically known system info. Pointers provided in *info * must be statically allocated. * Can be called at any time, even before retro_init(). */ RETRO_API void retro_get_system_info(struct retro_system_info *info); /* Gets information about system audio/video timings and geometry. * Can be called only after retro_load_game() has successfully completed. - * NOTE: The implementation of this function might not initialize every + * NOTE: The implementation of this function might not initialize every * variable if needed. - * E.g. geom.aspect_ratio might not be initialized if core doesn't + * E.g. geom.aspect_ratio might not be initialized if core doesn't * desire a particular aspect ratio. */ RETRO_API void retro_get_system_av_info(struct retro_system_av_info *info); /* Sets device to be used for player 'port'. - * By default, RETRO_DEVICE_JOYPAD is assumed to be plugged into all + * By default, RETRO_DEVICE_JOYPAD is assumed to be plugged into all * available ports. - * Setting a particular device type is not a guarantee that libretro cores - * will only poll input based on that particular device type. It is only a - * hint to the libretro core when a core cannot automatically detect the - * appropriate input device type on its own. It is also relevant when a - * core can change its behavior depending on device type. */ + * Setting a particular device type is not a guarantee that libretro cores + * will only poll input based on that particular device type. It is only a + * hint to the libretro core when a core cannot automatically detect the + * appropriate input device type on its own. It is also relevant when a + * core can change its behavior depending on device type. + * + * As part of the core's implementation of retro_set_controller_port_device, + * the core should call RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS to notify the + * frontend if the descriptions for any controls have changed as a + * result of changing the device type. + */ RETRO_API void retro_set_controller_port_device(unsigned port, unsigned device); /* Resets the current game. */ @@ -2078,18 +2699,18 @@ RETRO_API void retro_reset(void); /* Runs the game for one video frame. * During retro_run(), input_poll callback must be called at least once. - * + * * If a frame is not rendered for reasons where a game "dropped" a frame, - * this still counts as a frame, and retro_run() should explicitly dupe + * this still counts as a frame, and retro_run() should explicitly dupe * a frame if GET_CAN_DUPE returns true. * In this case, the video callback can take a NULL argument for data. */ RETRO_API void retro_run(void); -/* Returns the amount of data the implementation requires to serialize +/* Returns the amount of data the implementation requires to serialize * internal state (save states). - * Between calls to retro_load_game() and retro_unload_game(), the - * returned size is never allowed to be larger than a previous returned + * Between calls to retro_load_game() and retro_unload_game(), the + * returned size is never allowed to be larger than a previous returned * value, to ensure that the frontend can allocate a save state buffer once. */ RETRO_API size_t retro_serialize_size(void); @@ -2102,7 +2723,9 @@ RETRO_API bool retro_unserialize(const void *data, size_t size); RETRO_API void retro_cheat_reset(void); RETRO_API void retro_cheat_set(unsigned index, bool enabled, const char *code); -/* Loads a game. */ +/* Loads a game. + * Return true to indicate successful loading and false to indicate load failure. + */ RETRO_API bool retro_load_game(const struct retro_game_info *game); /* Loads a "special" kind of game. Should not be used, @@ -2112,7 +2735,7 @@ RETRO_API bool retro_load_game_special( const struct retro_game_info *info, size_t num_info ); -/* Unloads a currently loaded game. */ +/* Unloads the currently loaded game. Called before retro_deinit(void). */ RETRO_API void retro_unload_game(void); /* Gets region of game. */ From eb295de2184e9654c7de83348a7e490fd40ac49e Mon Sep 17 00:00:00 2001 From: SimpleTease <31772993+SimpleTease@users.noreply.github.com> Date: Sat, 10 Oct 2020 23:33:10 +0000 Subject: [PATCH 48/98] shared version.mk --- Makefile | 2 +- libretro/Makefile.common | 2 +- version.mk | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 version.mk diff --git a/Makefile b/Makefile index 0881fe9..c3d030a 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ ifeq ($(MAKECMDGOALS),) MAKECMDGOALS := $(DEFAULT) endif -VERSION := 0.13.6 +include version.mk export VERSION CONF ?= debug SDL_AUDIO_DRIVER ?= sdl diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 430c03d..fabe3ad 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -1,4 +1,4 @@ -VERSION := 0.13.6 +include $(CORE_DIR)/version.mk INCFLAGS := -I$(CORE_DIR) diff --git a/version.mk b/version.mk new file mode 100644 index 0000000..35ae0ad --- /dev/null +++ b/version.mk @@ -0,0 +1 @@ +VERSION := 0.13.6 From 6e0c09f78c1aa8da47bde45c27b9bee09686d7fe Mon Sep 17 00:00:00 2001 From: James Larrowe Date: Tue, 13 Oct 2020 18:19:29 -0400 Subject: [PATCH 49/98] Update RGBDS links in README and build-faq The repo's owner has changed twice since this link was used; once from bentley to the neutral rednex organization, and then from rednex to gbdev --- README.md | 2 +- build-faq.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1107fdc..5ed0268 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ SameBoy requires the following tools and libraries to build: * make * macOS Cocoa port: macOS SDK and Xcode (For command line tools and ibtool) * SDL port: libsdl2 - * [rgbds](https://github.com/bentley/rgbds/releases/), for boot ROM compilation + * [rgbds](https://github.com/gbdev/rgbds/releases/), for boot ROM compilation On Windows, SameBoy also requires: * Visual Studio (For headers, etc.) diff --git a/build-faq.md b/build-faq.md index 9def134..0921436 100644 --- a/build-faq.md +++ b/build-faq.md @@ -24,7 +24,7 @@ The following examples will be referenced later: ### rgbds -After downloading [rgbds](https://github.com/bentley/rgbds/releases/), ensure that it is added to the `%PATH%`. This may be done by adding it to the user's or SYSTEM's Environment Variables, or may be added to the command line at compilation time via `set path=%path%;C:\path\to\rgbds`. +After downloading [rgbds](https://github.com/gbdev/rgbds/releases/), ensure that it is added to the `%PATH%`. This may be done by adding it to the user's or SYSTEM's Environment Variables, or may be added to the command line at compilation time via `set path=%path%;C:\path\to\rgbds`. ### GnuWin From 03cbab2f85cfd62aada063ec0371a9c02db9bf5b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 15 Oct 2020 19:21:37 +0300 Subject: [PATCH 50/98] Windows is no longer officially supported in the standalone builds --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ed0268..9721be9 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,4 @@ To compile, simply run `make`. The targets are `cocoa` (Default for macOS), `sdl By default, the SDL port will look for resource files with a path relative to executable. If you are packaging SameBoy, you may wish to override this by setting the `DATA_DIR` variable during compilation to the target path of the directory containing all files (apart from the executable, that's not necessary) from the `build/bin/SDL` directory in the source tree. Make sure the variable ends with a `/` character. -SameBoy was compiled and tested on macOS, Ubuntu and 64-bit Windows 7. +SameBoy is compiled and tested on macOS and Ubuntu. From 88198e64f4398755d0892b63a4176dfabe6f690e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 13 Nov 2020 16:28:48 +0200 Subject: [PATCH 51/98] Minor bug fixes --- Core/cheats.c | 6 +++--- Core/debugger.c | 2 +- Core/save_state.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/cheats.c b/Core/cheats.c index 14875e0..451dddb 100644 --- a/Core/cheats.c +++ b/Core/cheats.c @@ -69,7 +69,7 @@ void GB_add_cheat(GB_gameboy_t *gb, const char *description, uint16_t address, u 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 = realloc(gb->cheats, (++gb->cheat_count) * sizeof(gb->cheats[0])); gb->cheats[gb->cheat_count - 1] = cheat; GB_cheat_hash_t **hash = &gb->cheat_hash[hash_addr(address)]; @@ -100,7 +100,7 @@ void GB_remove_cheat(GB_gameboy_t *gb, const GB_cheat_t *cheat) gb->cheats = NULL; } else { - gb->cheats = realloc(gb->cheats, gb->cheat_count * sizeof(*cheat)); + gb->cheats = realloc(gb->cheats, gb->cheat_count * sizeof(gb->cheats[0])); } break; } @@ -222,7 +222,7 @@ void GB_update_cheat(GB_gameboy_t *gb, const GB_cheat_t *_cheat, const char *des } 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; } } diff --git a/Core/debugger.c b/Core/debugger.c index 002d455..c27acd3 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -473,7 +473,7 @@ value_t debugger_evaluate(GB_gameboy_t *gb, const char *string, size_t length, bool *error, uint16_t *watchpoint_address, uint8_t *watchpoint_new_value) { - /* Disable watchpoints while evaulating expressions */ + /* Disable watchpoints while evaluating expressions */ uint16_t n_watchpoints = gb->n_watchpoints; gb->n_watchpoints = 0; diff --git a/Core/save_state.c b/Core/save_state.c index 9ef6ae3..827cf57 100644 --- a/Core/save_state.c +++ b/Core/save_state.c @@ -150,7 +150,7 @@ static bool verify_and_update_state_compatibility(GB_gameboy_t *gb, GB_gameboy_t { if (save->ram_size == 0 && (&save->ram_size)[-1] == gb->ram_size) { /* This is a save state with a bad printer struct from a 32-bit OS */ - memcpy(save->extra_oam + 4, save->extra_oam, (uintptr_t)&save->ram_size - (uintptr_t)&save->extra_oam); + memmove(save->extra_oam + 4, save->extra_oam, (uintptr_t)&save->ram_size - (uintptr_t)&save->extra_oam); } if (save->ram_size == 0) { /* Save doesn't have ram size specified, it's a pre 0.12 save state with potentially From 60b89787622840b2f5cf1490d401a82fa0257a5d Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 13 Nov 2020 23:07:35 +0200 Subject: [PATCH 52/98] Local link cable and infrared emulation in the Cocoa port --- Cocoa/AppDelegate.h | 3 +- Cocoa/AppDelegate.m | 25 ++++- Cocoa/Document.h | 5 +- Cocoa/Document.m | 220 +++++++++++++++++++++++++++++++++++++++----- Cocoa/Document.xib | 11 ++- Cocoa/GBView.h | 2 + Cocoa/GBView.m | 118 +++++++++++++++++++----- Cocoa/MainMenu.xib | 17 +++- Core/gb.h | 2 - 9 files changed, 351 insertions(+), 52 deletions(-) diff --git a/Cocoa/AppDelegate.h b/Cocoa/AppDelegate.h index 22e0c36..8f91565 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; @@ -10,6 +10,7 @@ - (IBAction)showPreferences: (id) sender; - (IBAction)toggleDeveloperMode:(id)sender; - (IBAction)switchPreferencesTab:(id)sender; +@property (weak) IBOutlet NSMenuItem *linkCableMenuItem; @end diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index 133fab7..282105e 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -81,10 +81,29 @@ if ([anItem action] == @selector(toggleDeveloperMode:)) { [(NSMenuItem *)anItem setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"DeveloperMode"]]; } - + + if (anItem == self.linkCableMenuItem) { + return [[NSDocumentController sharedDocumentController] documents].count > 1; + } return true; } +- (void)menuNeedsUpdate:(NSMenu *)menu +{ + NSMutableArray *items = [NSMutableArray array]; + NSDocument *currentDocument = [[NSDocumentController sharedDocumentController] currentDocument]; + + for (NSDocument *document in [[NSDocumentController sharedDocumentController] documents]) { + if (document == currentDocument) continue; + NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:document.displayName action:@selector(connectLinkCable:) keyEquivalent:@""]; + item.representedObject = document; + item.image = [[NSWorkspace sharedWorkspace] iconForFile:document.fileURL.path]; + [item.image setSize:NSMakeSize(16, 16)]; + [items addObject:item]; + } + menu.itemArray = items; +} + - (IBAction) showPreferences: (id) sender { NSArray *objects; @@ -109,4 +128,8 @@ { [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfFile:notification.identifier display:YES]; } + +- (IBAction)nop:(id)sender +{ +} @end diff --git a/Cocoa/Document.h b/Cocoa/Document.h index 660d7bc..bf5d9c0 100644 --- a/Cocoa/Document.h +++ b/Cocoa/Document.h @@ -6,6 +6,7 @@ @class GBCheatWindowController; @interface Document : NSDocument +@property (readonly) GB_gameboy_t *gb; @property (strong) IBOutlet GBView *view; @property (strong) IBOutlet NSTextView *consoleOutput; @property (strong) IBOutlet NSPanel *consoleWindow; @@ -36,10 +37,12 @@ @property (strong) IBOutlet NSBox *debuggerVerticalLine; @property (strong) IBOutlet NSPanel *cheatsWindow; @property (strong) IBOutlet GBCheatWindowController *cheatWindowController; +@property (readonly) Document *partner; +@property (readonly) bool isSlave; -(uint8_t) readMemory:(uint16_t) addr; -(void) writeMemory:(uint16_t) addr value:(uint8_t)value; -(void) performAtomicBlock: (void (^)())block; - +-(void) connectLinkCable:(NSMenuItem *)sender; @end diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 653179d..84e181f 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -28,6 +28,7 @@ enum model { NSMutableAttributedString *pending_console_output; NSRecursiveLock *console_output_lock; NSTimer *console_output_timer; + NSTimer *hex_timer; bool fullScreen; bool in_sync_input; @@ -47,7 +48,7 @@ enum model { bool oamUpdating; NSMutableData *currentPrinterImageData; - enum {GBAccessoryNone, GBAccessoryPrinter, GBAccessoryWorkboy} accessory; + enum {GBAccessoryNone, GBAccessoryPrinter, GBAccessoryWorkboy, GBAccessoryLinkCable} accessory; bool rom_warning_issued; @@ -66,6 +67,12 @@ enum model { size_t audioBufferNeeded; bool borderModeChanged; + + /* Link cable*/ + Document *master; + Document *slave; + signed linkOffset; + bool linkCableBit; } @property GBAudioClient *audioClient; @@ -81,6 +88,10 @@ enum model { - (void) gotNewSample:(GB_sample_t *)sample; - (void) rumbleChanged:(double)amp; - (void) loadBootROM:(GB_boot_rom_t)type; +- (void)linkCableBitStart:(bool)bit; +- (bool)linkCableBitEnd; +- (void)infraredStateChanged:(bool)state; + @end static void boot_rom_load(GB_gameboy_t *gb, GB_boot_rom_t type) @@ -160,6 +171,26 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [self rumbleChanged:amp]; } + +static void linkCableBitStart(GB_gameboy_t *gb, bool bit_to_send) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + [self linkCableBitStart:bit_to_send]; +} + +static bool linkCableBitEnd(GB_gameboy_t *gb) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + return [self linkCableBitEnd]; +} + +static void infraredStateChanged(GB_gameboy_t *gb, bool on, uint64_t cycles_since_last_update) +{ + Document *self = (__bridge Document *)GB_get_user_data(gb); + [self infraredStateChanged:on]; +} + + @implementation Document { GB_gameboy_t gb; @@ -264,6 +295,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); + GB_set_infrared_callback(&gb, infraredStateChanged); [self updateRumbleMode]; } @@ -335,9 +367,8 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [_view setRumble:amp]; } -- (void) run +- (void) preRun { - 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) { @@ -368,7 +399,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Mute"]) { [self.audioClient start]; } - NSTimer *hex_timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(reloadMemoryView) userInfo:nil repeats:YES]; + 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 */ @@ -388,19 +419,58 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) } } } - - while (running) { - if (rewind) { - rewind = false; - GB_rewind_pop(&gb); - if (!GB_rewind_pop(&gb)) { - rewind = self.view.isRewinding; +} + +static unsigned *multiplication_table_for_frequency(unsigned frequency) +{ + unsigned *ret = malloc(sizeof(*ret) * 0x100); + for (unsigned i = 0; i < 0x100; i++) { + ret[i] = i * frequency; + } + return ret; +} + +- (void) run +{ + assert(!master); + running = true; + [self preRun]; + if (slave) { + [slave preRun]; + unsigned *masterTable = multiplication_table_for_frequency(GB_get_clock_rate(&gb)); + unsigned *slaveTable = multiplication_table_for_frequency(GB_get_clock_rate(&slave->gb)); + while (running) { + if (linkOffset <= 0) { + linkOffset += slaveTable[GB_run(&gb)]; + } + else { + linkOffset -= masterTable[GB_run(&slave->gb)]; } } - else { - GB_run(&gb); + free(masterTable); + free(slaveTable); + [slave postRun]; + } + else { + while (running) { + if (rewind) { + rewind = false; + GB_rewind_pop(&gb); + if (!GB_rewind_pop(&gb)) { + rewind = self.view.isRewinding; + } + } + else { + GB_run(&gb); + } } } + [self postRun]; + stopping = false; +} + +- (void)postRun +{ [hex_timer invalidate]; [audioLock lock]; memset(audioBuffer, 0, (audioBufferSize - audioBufferPosition) * sizeof(*audioBuffer)); @@ -421,7 +491,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) 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; @@ -431,18 +501,31 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"GBNotificationsUsed"]; } [_view setRumble:0]; - stopping = false; } - (void) start { - if (running) return; self.view.mouseHidingEnabled = (self.mainWindow.styleMask & NSFullScreenWindowMask) != 0; + if (master) { + [master start]; + return; + } + if (running) return; [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] start]; } - (void) stop { + if (master) { + if (!master->running) return; + GB_debugger_set_disabled(&gb, true); + if (GB_debugger_is_stopped(&gb)) { + [self interruptDebugInputRead]; + } + [master stop]; + GB_debugger_set_disabled(&gb, false); + return; + } if (!running) return; GB_debugger_set_disabled(&gb, true); if (GB_debugger_is_stopped(&gb)) { @@ -519,6 +602,10 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (IBAction)togglePause:(id)sender { + if (master) { + [master togglePause:sender]; + return; + } if (running) { [self stop]; } @@ -749,6 +836,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (void)close { + [self disconnectLinkCable]; [[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.width forKey:@"LastWindowWidth"]; [[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.height forKey:@"LastWindowHeight"]; [self stop]; @@ -760,9 +848,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) { [self log:"^C\n"]; GB_debugger_break(&gb); - if (!running) { - [self start]; - } + [self start]; [self.consoleWindow makeKeyAndOrderFront:nil]; [self.consoleInput becomeFirstResponder]; } @@ -781,10 +867,13 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (BOOL)validateUserInterfaceItem:(id)anItem { if ([anItem action] == @selector(mute:)) { - [(NSMenuItem*)anItem setState:!self.audioClient.isPlaying]; + [(NSMenuItem *)anItem setState:!self.audioClient.isPlaying]; } else if ([anItem action] == @selector(togglePause:)) { - [(NSMenuItem*)anItem setState:(!running) || (GB_debugger_is_stopped(&gb))]; + if (master) { + [(NSMenuItem *)anItem setState:(!master->running) || (GB_debugger_is_stopped(&gb)) || (GB_debugger_is_stopped(&gb))]; + } + [(NSMenuItem *)anItem setState:(!running) || (GB_debugger_is_stopped(&gb))]; return !GB_debugger_is_stopped(&gb); } else if ([anItem action] == @selector(reset:) && anItem.tag != MODEL_NONE) { @@ -804,6 +893,10 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) else if ([anItem action] == @selector(connectWorkboy:)) { [(NSMenuItem*)anItem setState:accessory == GBAccessoryWorkboy]; } + else if ([anItem action] == @selector(connectLinkCable:)) { + [(NSMenuItem*)anItem setState:[(NSMenuItem *)anItem representedObject] == master || + [(NSMenuItem *)anItem representedObject] == slave]; + } else if ([anItem action] == @selector(toggleCheats:)) { [(NSMenuItem*)anItem setState:GB_cheats_enabled(&gb)]; } @@ -1090,6 +1183,9 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) { while (!GB_is_inited(&gb)); bool was_running = running && !GB_debugger_is_stopped(&gb); + if (master) { + was_running |= master->running; + } if (was_running) { [self stop]; } @@ -1700,6 +1796,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (IBAction)disconnectAllAccessories:(id)sender { + [self disconnectLinkCable]; [self performAtomicBlock:^{ accessory = GBAccessoryNone; GB_disconnect_serial(&gb); @@ -1708,6 +1805,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (IBAction)connectPrinter:(id)sender { + [self disconnectLinkCable]; [self performAtomicBlock:^{ accessory = GBAccessoryPrinter; GB_connect_printer(&gb, printImage); @@ -1716,6 +1814,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (IBAction)connectWorkboy:(id)sender { + [self disconnectLinkCable]; [self performAtomicBlock:^{ accessory = GBAccessoryWorkboy; GB_connect_workboy(&gb, setWorkboyTime, getWorkboyTime); @@ -1832,4 +1931,83 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) { GB_set_cheats_enabled(&gb, !GB_cheats_enabled(&gb)); } + +- (void)disconnectLinkCable +{ + bool wasRunning = self->running; + Document *partner = master ?: slave; + if (partner) { + [self stop]; + partner->master = nil; + partner->slave = nil; + master = nil; + slave = nil; + if (wasRunning) { + [partner start]; + [self start]; + } + GB_set_turbo_mode(&gb, false, false); + GB_set_turbo_mode(&partner->gb, false, false); + partner->accessory = GBAccessoryNone; + accessory = GBAccessoryNone; + } +} + +- (void)connectLinkCable:(NSMenuItem *)sender +{ + [self disconnectAllAccessories:sender]; + Document *partner = [sender representedObject]; + [partner disconnectAllAccessories:sender]; + + bool wasRunning = self->running; + [self stop]; + [partner stop]; + GB_set_turbo_mode(&partner->gb, true, true); + slave = partner; + partner->master = self; + linkOffset = 0; + partner->accessory = GBAccessoryLinkCable; + accessory = GBAccessoryLinkCable; + GB_set_serial_transfer_bit_start_callback(&gb, linkCableBitStart); + GB_set_serial_transfer_bit_start_callback(&partner->gb, linkCableBitStart); + GB_set_serial_transfer_bit_end_callback(&gb, linkCableBitEnd); + GB_set_serial_transfer_bit_end_callback(&partner->gb, linkCableBitEnd); + if (wasRunning) { + [self start]; + } +} + +- (void)linkCableBitStart:(bool)bit +{ + linkCableBit = bit; +} + +-(bool)linkCableBitEnd +{ + bool ret = GB_serial_get_data_bit(&self.partner->gb); + GB_serial_set_data_bit(&self.partner->gb, linkCableBit); + return ret; +} + +- (void)infraredStateChanged:(bool)state +{ + if (self.partner) { + GB_set_infrared_input(&self.partner->gb, state); + } +} + +-(Document *)partner +{ + return slave ?: master; +} + +- (bool)isSlave +{ + return master; +} + +- (GB_gameboy_t *)gb +{ + return &gb; +} @end diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib index d02f5bd..a2cf5ee 100644 --- a/Cocoa/Document.xib +++ b/Cocoa/Document.xib @@ -59,6 +59,9 @@ + + + @@ -115,7 +118,7 @@ - + @@ -152,7 +155,7 @@ - + @@ -186,7 +189,7 @@ - + @@ -975,7 +978,7 @@ - + diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index 80721cd..26fce14 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -1,6 +1,7 @@ #import #include #import +@class Document; typedef enum { GB_FRAME_BLENDING_MODE_DISABLED, @@ -13,6 +14,7 @@ typedef enum { @interface GBView : NSView - (void) flip; - (uint32_t *) pixels; +@property (weak) IBOutlet Document *document; @property GB_gameboy_t *gb; @property (nonatomic) GB_frame_blending_mode_t frameBlendingMode; @property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled; diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 6854ba7..0d834c0 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -5,6 +5,7 @@ #import "GBViewMetal.h" #import "GBButtons.h" #import "NSString+StringForKey.h" +#import "Document.h" #define JOYSTICK_HIGH 0x4000 #define JOYSTICK_LOW 0x3800 @@ -257,6 +258,9 @@ static const uint8_t workboy_vk_to_key[] = { { if (analogClockMultiplierValid && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]) { GB_set_clock_multiplier(_gb, analogClockMultiplier); + if (self.document.partner) { + GB_set_clock_multiplier(self.document.partner.gb, analogClockMultiplier); + } if (analogClockMultiplier == 1.0) { analogClockMultiplierValid = false; } @@ -265,10 +269,16 @@ static const uint8_t workboy_vk_to_key[] = { if (underclockKeyDown && clockMultiplier > 0.5) { clockMultiplier -= 1.0/16; GB_set_clock_multiplier(_gb, clockMultiplier); + if (self.document.partner) { + GB_set_clock_multiplier(self.document.partner.gb, clockMultiplier); + } } if (!underclockKeyDown && clockMultiplier < 1.0) { clockMultiplier += 1.0/16; GB_set_clock_multiplier(_gb, clockMultiplier); + if (self.document.partner) { + GB_set_clock_multiplier(self.document.partner.gb, clockMultiplier); + } } } current_buffer = (current_buffer + 1) % self.numberOfBuffers; @@ -299,6 +309,9 @@ static const uint8_t workboy_vk_to_key[] = { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; unsigned player_count = GB_get_player_count(_gb); + if (self.document.partner) { + player_count = 2; + } for (unsigned player = 0; player < player_count; player++) { for (GBButton button = 0; button < GBButtonCount; button++) { NSNumber *key = [defaults valueForKey:button_to_preference_name(button, player)]; @@ -308,13 +321,20 @@ static const uint8_t workboy_vk_to_key[] = { handled = true; switch (button) { case GBTurbo: - GB_set_turbo_mode(_gb, true, self.isRewinding); + if (self.document.isSlave) { + GB_set_turbo_mode(self.document.partner.gb, true, false); + } + else { + GB_set_turbo_mode(_gb, true, self.isRewinding); + } analogClockMultiplierValid = false; break; case GBRewind: - self.isRewinding = true; - GB_set_turbo_mode(_gb, false, false); + if (!self.document.partner) { + self.isRewinding = true; + GB_set_turbo_mode(_gb, false, false); + } break; case GBUnderclock: @@ -323,7 +343,17 @@ static const uint8_t workboy_vk_to_key[] = { break; default: - GB_set_key_state_for_player(_gb, (GB_key_t)button, player, true); + if (self.document.partner) { + if (player == 0) { + GB_set_key_state_for_player(_gb, (GB_key_t)button, 0, true); + } + else { + GB_set_key_state_for_player(self.document.partner.gb, (GB_key_t)button, 0, true); + } + } + else { + GB_set_key_state_for_player(_gb, (GB_key_t)button, player, true); + } break; } } @@ -351,6 +381,9 @@ static const uint8_t workboy_vk_to_key[] = { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; unsigned player_count = GB_get_player_count(_gb); + if (self.document.partner) { + player_count = 2; + } for (unsigned player = 0; player < player_count; player++) { for (GBButton button = 0; button < GBButtonCount; button++) { NSNumber *key = [defaults valueForKey:button_to_preference_name(button, player)]; @@ -360,7 +393,12 @@ static const uint8_t workboy_vk_to_key[] = { handled = true; switch (button) { case GBTurbo: - GB_set_turbo_mode(_gb, false, false); + if (self.document.isSlave) { + GB_set_turbo_mode(self.document.partner.gb, false, false); + } + else { + GB_set_turbo_mode(_gb, false, false); + } analogClockMultiplierValid = false; break; @@ -374,7 +412,17 @@ static const uint8_t workboy_vk_to_key[] = { break; default: - GB_set_key_state_for_player(_gb, (GB_key_t)button, player, false); + if (self.document.partner) { + if (player == 0) { + GB_set_key_state_for_player(_gb, (GB_key_t)button, 0, false); + } + else { + GB_set_key_state_for_player(self.document.partner.gb, (GB_key_t)button, 0, false); + } + } + else { + GB_set_key_state_for_player(_gb, (GB_key_t)button, player, false); + } break; } } @@ -415,13 +463,11 @@ static const uint8_t workboy_vk_to_key[] = { - (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button { if (![self.window isMainWindow]) return; - if (controller != lastController) { - [self setRumble:0]; - lastController = controller; - } - unsigned player_count = GB_get_player_count(_gb); + if (self.document.partner) { + player_count = 2; + } IOPMAssertionID assertionID; IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID); @@ -447,33 +493,63 @@ static const uint8_t workboy_vk_to_key[] = { usage = (const JOYButtonUsage[]){JOYButtonUsageY, JOYButtonUsageA, JOYButtonUsageB, JOYButtonUsageX}[(usage - JOYButtonUsageGeneric0) & 3]; } + GB_gameboy_t *effectiveGB = _gb; + unsigned effectivePlayer = player; + + if (player && self.document.partner) { + effectiveGB = self.document.partner.gb; + effectivePlayer = 0; + if (controller != self.document.partner.view->lastController) { + [self setRumble:0]; + self.document.partner.view->lastController = controller; + } + } + else { + if (controller != lastController) { + [self setRumble:0]; + lastController = controller; + } + } + 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 JOYButtonUsageA: GB_set_key_state_for_player(effectiveGB, GB_KEY_A, effectivePlayer, button.isPressed); break; + case JOYButtonUsageB: GB_set_key_state_for_player(effectiveGB, GB_KEY_B, effectivePlayer, 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 JOYButtonUsageX: GB_set_key_state_for_player(effectiveGB, GB_KEY_START, effectivePlayer, button.isPressed); break; case JOYButtonUsageSelect: - case JOYButtonUsageY: GB_set_key_state_for_player(_gb, GB_KEY_SELECT, player, button.isPressed); break; + case JOYButtonUsageY: GB_set_key_state_for_player(effectiveGB, GB_KEY_SELECT, effectivePlayer, button.isPressed); break; case JOYButtonUsageR2: case JOYButtonUsageL2: case JOYButtonUsageZ: { self.isRewinding = button.isPressed; if (button.isPressed) { - GB_set_turbo_mode(_gb, false, false); + if (self.document.isSlave) { + GB_set_turbo_mode(self.document.partner.gb, false, false); + } + else { + GB_set_turbo_mode(_gb, false, false); + } } break; } - case JOYButtonUsageL1: GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding); break; + case JOYButtonUsageL1: { + if (self.document.isSlave) { + GB_set_turbo_mode(self.document.partner.gb, button.isPressed, false); break; + } + else { + 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; + case JOYButtonUsageDPadLeft: GB_set_key_state_for_player(effectiveGB, GB_KEY_LEFT, effectivePlayer, button.isPressed); break; + case JOYButtonUsageDPadRight: GB_set_key_state_for_player(effectiveGB, GB_KEY_RIGHT, effectivePlayer, button.isPressed); break; + case JOYButtonUsageDPadUp: GB_set_key_state_for_player(effectiveGB, GB_KEY_UP, effectivePlayer, button.isPressed); break; + case JOYButtonUsageDPadDown: GB_set_key_state_for_player(effectiveGB, GB_KEY_DOWN, effectivePlayer, button.isPressed); break; default: break; diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index 586d872..04bcf8f 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -12,7 +12,11 @@ - + + + + + @@ -373,6 +377,17 @@ + + + + + + + + + + + diff --git a/Core/gb.h b/Core/gb.h index 9043936..c42af4b 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -803,9 +803,7 @@ void GB_set_icd_pixel_callback(GB_gameboy_t *gb, GB_icd_pixel_callback_t callbac 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 uint32_t GB_get_clock_rate(GB_gameboy_t *gb); -#endif void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier); unsigned GB_get_screen_width(GB_gameboy_t *gb); From c36bdc22f66fbc58c82d4e3812e0110e50ece208 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 14 Nov 2020 13:55:39 +0200 Subject: [PATCH 53/98] More accurate interrupt emulation --- Core/sm83_cpu.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 3b3eceb..ffc5997 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -261,7 +261,20 @@ static void cycle_oam_bug(GB_gameboy_t *gb, uint8_t register_id) } GB_trigger_oam_bug(gb, gb->registers[register_id]); /* Todo: test T-cycle timing */ gb->pending_cycles = 4; +} +static void cycle_oam_bug_pc(GB_gameboy_t *gb) +{ + if (GB_is_cgb(gb)) { + /* Slight optimization */ + gb->pending_cycles += 4; + return; + } + if (gb->pending_cycles) { + GB_advance_cycles(gb, gb->pending_cycles); + } + GB_trigger_oam_bug(gb, gb->pc); /* Todo: test T-cycle timing */ + gb->pending_cycles = 4; } static void flush_pending_cycles(GB_gameboy_t *gb) @@ -1538,8 +1551,9 @@ void GB_cpu_run(GB_gameboy_t *gb) gb->halted = false; uint16_t call_addr = gb->pc; - cycle_no_access(gb); - cycle_no_access(gb); + gb->last_opcode_read = cycle_read_inc_oam_bug(gb, gb->pc++); + cycle_oam_bug_pc(gb); + gb->pc--; GB_trigger_oam_bug(gb, gb->registers[GB_REGISTER_SP]); /* Todo: test T-cycle timing */ cycle_no_access(gb); From 7fdc58a07ef48b155c28d97f4004f81de7a82c36 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 20 Nov 2020 16:24:16 +0200 Subject: [PATCH 54/98] Implement CGB-mode TILE_SEL mixing, fixes cgb-acid-hell and m3_lcdc_tile_sel_change2, closes #308 --- Core/display.c | 76 +++++++++++++++++++++++++++++++++++++++++-------- Core/gb.h | 2 ++ Core/sm83_cpu.c | 30 +++++++++++++++++++ 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/Core/display.c b/Core/display.c index 2eb8c42..b57317b 100644 --- a/Core/display.c +++ b/Core/display.c @@ -430,6 +430,23 @@ static void add_object_from_index(GB_gameboy_t *gb, unsigned index) } } +static uint8_t data_for_tile_sel_glitch(GB_gameboy_t *gb, bool *should_use) +{ + /* + Based on Matt Currie's research here: + https://github.com/mattcurrie/mealybug-tearoom-tests/blob/master/the-comprehensive-game-boy-ppu-documentation.md#tile_sel-bit-4 + */ + + *should_use = true; + if (gb->io_registers[GB_IO_LCDC] & 0x10) { + *should_use = !(gb->current_tile & 0x80); + /* if (gb->model != GB_MODEL_CGB_D) */ return gb->current_tile; + // TODO: CGB D behaves differently + } + return gb->data_for_sel_glitch; +} + + static void render_pixel_if_possible(GB_gameboy_t *gb) { GB_fifo_item_t *fifo_item = NULL; @@ -621,6 +638,10 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) break; case GB_FETCHER_GET_TILE_DATA_LOWER: { + bool use_glitched = false; + if (gb->tile_sel_glitch) { + gb->current_tile_data[0] = data_for_tile_sel_glitch(gb, &use_glitched); + } uint8_t y_flip = 0; uint16_t tile_address = 0; uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb); @@ -638,20 +659,32 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) if (gb->current_tile_attributes & 0x40) { y_flip = 0x7; } - gb->current_tile_data[0] = + if (!use_glitched) { + gb->current_tile_data[0] = + gb->vram[tile_address + ((y & 7) ^ y_flip) * 2]; + if (gb->vram_ppu_blocked) { + gb->current_tile_data[0] = 0xFF; + } + } + else { + gb->data_for_sel_glitch = gb->vram[tile_address + ((y & 7) ^ y_flip) * 2]; - if (gb->vram_ppu_blocked) { - gb->current_tile_data[0] = 0xFF; + if (gb->vram_ppu_blocked) { + gb->data_for_sel_glitch = 0xFF; + } } } gb->fetcher_state++; break; case GB_FETCHER_GET_TILE_DATA_HIGH: { - /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. - Additionally, on CGB-D and newer mixing two tiles by changing the tileset - bit mid-fetching causes a glitched mixing of the two, in comparison to the - more logical DMG version. */ + /* Todo: Verified for DMG (Tested: SGB2), CGB timing is wrong. */ + + bool use_glitched = false; + if (gb->tile_sel_glitch) { + gb->current_tile_data[1] = data_for_tile_sel_glitch(gb, &use_glitched); + } + uint16_t tile_address = 0; uint8_t y = gb->model > GB_MODEL_CGB_C ? gb->fetcher_y : fetcher_y(gb); @@ -669,10 +702,20 @@ static void advance_fetcher_state_machine(GB_gameboy_t *gb) y_flip = 0x7; } gb->last_tile_data_address = tile_address + ((y & 7) ^ y_flip) * 2 + 1; - gb->current_tile_data[1] = - gb->vram[gb->last_tile_data_address]; - if (gb->vram_ppu_blocked) { - gb->current_tile_data[1] = 0xFF; + if (!use_glitched) { + gb->current_tile_data[1] = + gb->vram[gb->last_tile_data_address]; + if (gb->vram_ppu_blocked) { + gb->current_tile_data[1] = 0xFF; + } + } + else { + if ((gb->io_registers[GB_IO_LCDC] & 0x10) && gb->tile_sel_glitch) { + gb->data_for_sel_glitch = gb->vram[gb->last_tile_data_address]; + if (gb->vram_ppu_blocked) { + gb->data_for_sel_glitch = 0xFF; + } + } } } if (gb->wx_triggered) { @@ -1112,7 +1155,8 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) object->flags & 0x80, gb->object_priority == GB_OBJECT_PRIORITY_INDEX? gb->visible_objs[gb->n_visible_objs - 1] : 0, object->flags & 0x20); - + + gb->data_for_sel_glitch = gb->vram_ppu_blocked? 0xFF : gb->vram[line_address + 1]; gb->n_visible_objs--; } @@ -1128,6 +1172,14 @@ abort_fetching_object: GB_SLEEP(gb, display, 21, 1); } + /* TODO: Verify */ + if (gb->fetcher_state == 4 || gb->fetcher_state == 5) { + gb->data_for_sel_glitch = gb->current_tile_data[0]; + } + else { + gb->data_for_sel_glitch = gb->current_tile_data[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; diff --git a/Core/gb.h b/Core/gb.h index c42af4b..0d0aaa9 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -548,6 +548,7 @@ struct GB_gameboy_internal_s { uint16_t last_tile_data_address; uint16_t last_tile_index_address; bool cgb_repeated_a_frame; + uint8_t data_for_sel_glitch; ); /* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */ @@ -692,6 +693,7 @@ struct GB_gameboy_internal_s { /* Temporary state */ bool wx_just_changed; + bool tile_sel_glitch; ); }; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index ffc5997..7107ed1 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -21,10 +21,12 @@ typedef enum { GB_CONFLICT_DMG_LCDC, GB_CONFLICT_SGB_LCDC, GB_CONFLICT_WX, + GB_CONFLICT_CGB_LCDC, } GB_conflict_t; /* Todo: How does double speed mode affect these? */ static const GB_conflict_t cgb_conflict_map[0x80] = { + [GB_IO_LCDC] = GB_CONFLICT_CGB_LCDC, [GB_IO_IF] = GB_CONFLICT_WRITE_CPU, [GB_IO_LYC] = GB_CONFLICT_WRITE_CPU, [GB_IO_STAT] = GB_CONFLICT_STAT_CGB, @@ -241,6 +243,34 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) gb->wx_just_changed = false; gb->pending_cycles = 3; return; + + case GB_CONFLICT_CGB_LCDC: + if ((value ^ gb->io_registers[GB_IO_LCDC]) & 0x10) { + // Todo: This is difference is because my timing is off in one of the models + if (gb->model > GB_MODEL_CGB_C) { + GB_advance_cycles(gb, gb->pending_cycles); + gb->tile_sel_glitch = true; + GB_advance_cycles(gb, 1); + gb->tile_sel_glitch = false; + GB_write_memory(gb, addr, value); + gb->pending_cycles = 3; + } + else { + GB_advance_cycles(gb, gb->pending_cycles - 1); + gb->tile_sel_glitch = true; + GB_advance_cycles(gb, 1); + gb->tile_sel_glitch = false; + GB_write_memory(gb, addr, value); + gb->pending_cycles = 4; + } + } + else { + GB_advance_cycles(gb, gb->pending_cycles); + GB_write_memory(gb, addr, value); + gb->pending_cycles = 4; + } + return; + } } From cd2310f0a749499130cd148cafa2386c32491832 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 20 Nov 2020 19:39:54 +0200 Subject: [PATCH 55/98] Wave RAM reads 0xFF while active on AGBs --- Core/apu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/apu.c b/Core/apu.c index 7e7ab31..2b5dc3b 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -596,6 +596,9 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg) if (!GB_is_cgb(gb) && !gb->apu.wave_channel.wave_form_just_read) { return 0xFF; } + if (gb->model == GB_MODEL_AGB) { + return 0xFF; + } reg = GB_IO_WAV_START + gb->apu.wave_channel.current_sample_index / 2; } From b7f34547631dba70f84ec670ec701af0e3f9ff56 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 20 Nov 2020 22:12:15 +0200 Subject: [PATCH 56/98] More accurate emulation of the IR port --- Core/memory.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index f73209e..0d33e6c 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -115,7 +115,7 @@ static bool is_addr_in_dma_use(GB_gameboy_t *gb, uint16_t addr) static bool effective_ir_input(GB_gameboy_t *gb) { - return gb->infrared_input || (gb->io_registers[GB_IO_RP] & 1) || gb->cart_ir; + return gb->infrared_input || gb->cart_ir; } static uint8_t read_rom(GB_gameboy_t *gb, uint16_t addr) @@ -428,9 +428,12 @@ 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. */ - uint8_t ret = (gb->io_registers[GB_IO_RP] & 0xC1) | 0x3C; - if ((gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 && effective_ir_input(gb)) { - ret |= 2; + uint8_t ret = (gb->io_registers[GB_IO_RP] & 0xC1) | 0x2E; + if (gb->model != GB_MODEL_CGB_E) { + ret |= 0x10; + } + if (((gb->io_registers[GB_IO_RP] & 0xC1) == 0xC0 && effective_ir_input(gb)) && gb->model != GB_MODEL_AGB) { + ret &= ~2; } return ret; } From 1d9ac5ccc3f54a5d527e6f7dab554015107ad5d5 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 21 Nov 2020 00:52:54 +0200 Subject: [PATCH 57/98] More accurate IR emulation, simplify API --- Cocoa/Document.m | 2 +- Core/gb.c | 11 ----------- Core/gb.h | 19 ++++--------------- Core/memory.c | 39 +++++++++++++-------------------------- Core/timing.c | 35 +++++++++++++++++++++++++---------- 5 files changed, 43 insertions(+), 63 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 84e181f..ea7ef49 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -184,7 +184,7 @@ static bool linkCableBitEnd(GB_gameboy_t *gb) return [self linkCableBitEnd]; } -static void infraredStateChanged(GB_gameboy_t *gb, bool on, uint64_t cycles_since_last_update) +static void infraredStateChanged(GB_gameboy_t *gb, bool on) { Document *self = (__bridge Document *)GB_get_user_data(gb); [self infraredStateChanged:on]; diff --git a/Core/gb.c b/Core/gb.c index 7325d79..57c3788 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -1073,17 +1073,6 @@ void GB_set_infrared_callback(GB_gameboy_t *gb, GB_infrared_callback_t callback) void GB_set_infrared_input(GB_gameboy_t *gb, bool state) { gb->infrared_input = state; - gb->cycles_since_input_ir_change = 0; - gb->ir_queue_length = 0; -} - -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"); - return; - } - gb->ir_queue[gb->ir_queue_length++] = (GB_ir_queue_item_t){state, cycles_after_previous_change}; } void GB_set_rumble_callback(GB_gameboy_t *gb, GB_rumble_callback_t callback) diff --git a/Core/gb.h b/Core/gb.h index 0d0aaa9..7610fca 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -125,8 +125,6 @@ typedef enum { GB_BORDER_ALWAYS, } GB_border_mode_t; -#define GB_MAX_IR_QUEUE 256 - enum { /* Joypad and Serial */ GB_IO_JOYP = 0x00, // Joypad (R/W) @@ -272,7 +270,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, uint64_t cycles_since_last_update); +typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool 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); @@ -283,11 +281,6 @@ typedef void (*GB_icd_hreset_callback_t)(GB_gameboy_t *gb); typedef void (*GB_icd_vreset_callback_t)(GB_gameboy_t *gb); typedef void (*GB_boot_rom_load_callback_t)(GB_gameboy_t *gb, GB_boot_rom_t type); -typedef struct { - bool state; - uint64_t delay; -} GB_ir_queue_item_t; - struct GB_breakpoint_s; struct GB_watchpoint_s; @@ -374,6 +367,9 @@ struct GB_gameboy_internal_s { uint8_t extra_oam[0xff00 - 0xfea0]; uint32_t ram_size; // Different between CGB and DMG GB_workboy_t workboy; + + int32_t ir_sensor; + bool effective_ir_input; ); /* DMA and HDMA */ @@ -613,12 +609,6 @@ struct GB_gameboy_internal_s { GB_print_image_callback_t printer_callback; GB_workboy_set_time_callback workboy_set_time_callback; GB_workboy_get_time_callback workboy_get_time_callback; - - /* IR */ - 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; /*** Debugger ***/ volatile bool debug_stopped, debug_disable; @@ -771,7 +761,6 @@ 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, 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/memory.c b/Core/memory.c index 0d33e6c..40af0e5 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -113,11 +113,6 @@ 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->cart_ir; -} - static uint8_t read_rom(GB_gameboy_t *gb, uint16_t addr) { if (addr < 0x100 && !gb->boot_rom_finished) { @@ -173,7 +168,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) case 0xD: // RTC status return 1; case 0xE: // IR mode - return effective_ir_input(gb); // TODO: What are the other bits? + return gb->effective_ir_input; // 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? @@ -191,7 +186,7 @@ static uint8_t read_mbc_ram(GB_gameboy_t *gb, uint16_t addr) } if (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) { - return 0xc0 | effective_ir_input(gb); + return 0xc0 | gb->effective_ir_input; } if (gb->cartridge_type->has_rtc && gb->cartridge_type->mbc_type != GB_HUC3 && @@ -432,7 +427,7 @@ static uint8_t read_high_memory(GB_gameboy_t *gb, uint16_t addr) if (gb->model != GB_MODEL_CGB_E) { ret |= 0x10; } - if (((gb->io_registers[GB_IO_RP] & 0xC1) == 0xC0 && effective_ir_input(gb)) && gb->model != GB_MODEL_AGB) { + if (((gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 && gb->effective_ir_input) && gb->model != GB_MODEL_AGB) { ret &= ~2; } return ret; @@ -655,14 +650,11 @@ static bool huc3_write(GB_gameboy_t *gb, uint8_t value) // 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->cart_ir != (value & 1)) { + gb->cart_ir = value & 1; if (gb->infrared_callback) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + gb->infrared_callback(gb, value & 1); } - gb->cycles_since_ir_change = 0; } return true; } @@ -691,14 +683,11 @@ static void write_mbc_ram(GB_gameboy_t *gb, uint16_t addr, uint8_t value) && gb->cartridge_type->mbc_type != GB_HUC1) return; 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); - if (new_input != old_input) { + if (gb->cart_ir != (value & 1)) { + gb->cart_ir = value & 1; if (gb->infrared_callback) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + gb->infrared_callback(gb, value & 1); } - gb->cycles_since_ir_change = 0; } return; } @@ -1111,15 +1100,13 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if (!GB_is_cgb(gb)) { return; } - 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) { + if ((gb->io_registers[GB_IO_RP] ^ value) & 1) { if (gb->infrared_callback) { - gb->infrared_callback(gb, new_input, gb->cycles_since_ir_change); + gb->infrared_callback(gb, value & 1); } - gb->cycles_since_ir_change = 0; } + gb->io_registers[GB_IO_RP] = value; + return; } diff --git a/Core/timing.c b/Core/timing.c index 965ba27..44ff8f7 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -89,15 +89,32 @@ void GB_timing_sync(GB_gameboy_t *gb) } #endif -static void GB_ir_run(GB_gameboy_t *gb) + +#define IR_DECAY 31500 +#define IR_THRESHOLD 19900 +#define IR_MAX IR_THRESHOLD * 2 + IR_DECAY + +static void GB_ir_run(GB_gameboy_t *gb, uint32_t cycles) { - if (gb->ir_queue_length == 0) return; - if (gb->cycles_since_input_ir_change >= gb->ir_queue[0].delay) { - gb->cycles_since_input_ir_change -= gb->ir_queue[0].delay; - gb->infrared_input = gb->ir_queue[0].state; - gb->ir_queue_length--; - memmove(&gb->ir_queue[0], &gb->ir_queue[1], sizeof(gb->ir_queue[0]) * (gb->ir_queue_length)); + if (gb->model == GB_MODEL_AGB) return; + if (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1)) { + gb->ir_sensor += cycles; + if (gb->ir_sensor > IR_MAX) { + gb->ir_sensor = IR_MAX; + } + + gb->effective_ir_input = gb->ir_sensor >= IR_THRESHOLD && gb->ir_sensor <= IR_THRESHOLD + IR_DECAY; } + else { + if (gb->ir_sensor <= cycles) { + gb->ir_sensor = 0; + } + else { + gb->ir_sensor -= cycles; + } + gb->effective_ir_input = false; + } + } static void advance_tima_state_machine(GB_gameboy_t *gb) @@ -234,8 +251,6 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) gb->double_speed_alignment += cycles; gb->hdma_cycles += cycles; gb->apu_output.sample_cycles += cycles; - gb->cycles_since_ir_change += cycles; - gb->cycles_since_input_ir_change += cycles; gb->cycles_since_last_sync += cycles; gb->cycles_since_run += cycles; @@ -252,7 +267,7 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) } GB_apu_run(gb); GB_display_run(gb, cycles); - GB_ir_run(gb); + GB_ir_run(gb, cycles); } /* From bdd27ce50d4b27898e11e8a34c9371e210f10846 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 21 Nov 2020 15:36:21 +0200 Subject: [PATCH 58/98] IR support in the libretro port --- libretro/libretro.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libretro/libretro.c b/libretro/libretro.c index 9e27f03..0fb8dc5 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -213,6 +213,16 @@ static bool serial_end2(GB_gameboy_t *gb) return ret; } +static void infrared_callback1(GB_gameboy_t *gb, bool output) +{ + GB_set_infrared_input(&gameboy[1], output); +} + +static void infrared_callback2(GB_gameboy_t *gb, bool output) +{ + GB_set_infrared_input(&gameboy[0], output); +} + static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) { return r <<16 | g <<8 | b; @@ -351,12 +361,16 @@ static void set_link_cable_state(bool state) 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); + GB_set_infrared_callback(&gameboy[0], infrared_callback1); + GB_set_infrared_callback(&gameboy[1], infrared_callback2); } 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); GB_set_serial_transfer_bit_end_callback(&gameboy[1], NULL); + GB_set_infrared_callback(&gameboy[0], NULL); + GB_set_infrared_callback(&gameboy[1], NULL); } } From 027cecde2493f022e6e2881e6c8494183a3f39e2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 21 Nov 2020 16:19:58 +0200 Subject: [PATCH 59/98] Added debugger "undo" command. Closes #156 --- Core/debugger.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- Core/gb.c | 7 +++++++ Core/gb.h | 4 ++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index c27acd3..3e1b66c 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1889,6 +1889,29 @@ static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debug return true; } +static bool undo(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; + } + + if (!gb->undo_label) { + GB_log(gb, "No undo state available\n"); + return true; + } + uint16_t pc = gb->pc; + GB_load_state_from_buffer(gb, gb->undo_state, GB_get_save_state_size(gb)); + GB_log(gb, "Reverted a \"%s\" command.\n", gb->undo_label); + if (pc != gb->pc) { + GB_cpu_disassemble(gb, gb->pc, 5); + } + gb->undo_label = NULL; + + return true; +} + static bool help(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command); #define HELP_NEWLINE "\n " @@ -1899,6 +1922,7 @@ 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"}, + {"undo", 1, undo, "Reverts the last command"}, {"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"}, @@ -2184,7 +2208,30 @@ bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input) const debugger_command_t *command = find_command(command_string); if (command) { - return command->implementation(gb, arguments, modifiers, command); + uint8_t *old_state = malloc(GB_get_save_state_size(gb)); + GB_save_state_to_buffer(gb, old_state); + bool ret = command->implementation(gb, arguments, modifiers, command); + if (!ret) { // Command continues, save state in any case + free(gb->undo_state); + gb->undo_state = old_state; + gb->undo_label = command->command; + } + else { + uint8_t *new_state = malloc(GB_get_save_state_size(gb)); + GB_save_state_to_buffer(gb, new_state); + if (memcmp(new_state, old_state, GB_get_save_state_size(gb)) != 0) { + // State changed, save the old state as the new undo state + free(gb->undo_state); + gb->undo_state = old_state; + gb->undo_label = command->command; + } + else { + // Nothing changed, just free the old state + free(old_state); + } + free(new_state); + } + return ret; } else { GB_log(gb, "%s: no such command.\n", command_string); @@ -2260,6 +2307,11 @@ 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; + + if (!gb->undo_state) { + gb->undo_state = malloc(GB_get_save_state_size(gb)); + GB_save_state_to_buffer(gb, gb->undo_state); + } char *input = NULL; if (gb->debug_next_command && gb->debug_call_depth <= 0 && !gb->halted) { diff --git a/Core/gb.c b/Core/gb.c index 57c3788..4788ad9 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -202,6 +202,9 @@ void GB_free(GB_gameboy_t *gb) if (gb->nontrivial_jump_state) { free(gb->nontrivial_jump_state); } + if (gb->undo_state) { + free(gb->undo_state); + } #ifndef GB_DISABLE_DEBUGGER GB_debugger_clear_symbols(gb); #endif @@ -1434,6 +1437,10 @@ void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model) gb->ram = realloc(gb->ram, gb->ram_size = 0x2000); gb->vram = realloc(gb->vram, gb->vram_size = 0x2000); } + if (gb->undo_state) { + free(gb->undo_state); + gb->undo_state = NULL; + } GB_rewind_free(gb); GB_reset(gb); load_default_border(gb); diff --git a/Core/gb.h b/Core/gb.h index 7610fca..ed736e0 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -646,6 +646,10 @@ struct GB_gameboy_internal_s { /* Ticks command */ uint64_t debugger_ticks; + /* Undo */ + uint8_t *undo_state; + const char *undo_label; + /* Rewind */ #define GB_REWIND_FRAMES_PER_KEY 255 size_t rewind_buffer_length; From bbf609f46b919ab603c9b28424fb724dd92a16fc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 21 Nov 2020 21:05:03 +0200 Subject: [PATCH 60/98] Add TGA output option to the tester, closes #310 --- Tester/main.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/Tester/main.c b/Tester/main.c index 16dbf7b..6faab3b 100755 --- a/Tester/main.c +++ b/Tester/main.c @@ -31,16 +31,23 @@ static unsigned int test_length = 60 * 40; GB_gameboy_t gb; static unsigned int frames = 0; -const char bmp_header[] = { -0x42, 0x4D, 0x48, 0x68, 0x01, 0x00, 0x00, 0x00, -0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x38, 0x00, -0x00, 0x00, 0xA0, 0x00, 0x00, 0x00, 0x70, 0xFF, -0xFF, 0xFF, 0x01, 0x00, 0x20, 0x00, 0x03, 0x00, -0x00, 0x00, 0x02, 0x68, 0x01, 0x00, 0x12, 0x0B, -0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, -0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +static bool use_tga = false; +static const uint8_t bmp_header[] = { + 0x42, 0x4D, 0x48, 0x68, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x38, 0x00, + 0x00, 0x00, 0xA0, 0x00, 0x00, 0x00, 0x70, 0xFF, + 0xFF, 0xFF, 0x01, 0x00, 0x20, 0x00, 0x03, 0x00, + 0x00, 0x00, 0x02, 0x68, 0x01, 0x00, 0x12, 0x0B, + 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static const uint8_t tga_header[] = { + 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00, 0x90, 0x00, + 0x20, 0x28, }; uint32_t bitmap[160*144]; @@ -139,7 +146,12 @@ static void vblank(GB_gameboy_t *gb) /* Let the test run for extra four seconds if the screen is off/disabled */ if (!is_screen_blank || frames >= test_length + 60 * 4) { FILE *f = fopen(bmp_filename, "wb"); - fwrite(&bmp_header, 1, sizeof(bmp_header), f); + if (use_tga) { + fwrite(&tga_header, 1, sizeof(tga_header), f); + } + else { + fwrite(&bmp_header, 1, sizeof(bmp_header), f); + } fwrite(&bitmap, 1, sizeof(bitmap), f); fclose(f); if (!gb->boot_rom_finished) { @@ -215,6 +227,9 @@ static char *executable_relative_path(const char *filename) static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) { + if (use_tga) { + return (r << 16) | (g << 8) | (b); + } return (r << 24) | (g << 16) | (b << 8); } @@ -268,6 +283,12 @@ int main(int argc, char **argv) dmg = true; continue; } + + if (strcmp(argv[i], "--tga") == 0) { + fprintf(stderr, "Using TGA output\n"); + use_tga = true; + continue; + } if (strcmp(argv[i], "--start") == 0) { fprintf(stderr, "Pushing Start and A\n"); @@ -312,7 +333,7 @@ int main(int argc, char **argv) size_t path_length = strlen(filename); char bitmap_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .bmp + NULL */ - replace_extension(filename, path_length, bitmap_path, ".bmp"); + replace_extension(filename, path_length, bitmap_path, use_tga? ".tga" : ".bmp"); bmp_filename = &bitmap_path[0]; char log_path[path_length + 5]; From 67c0e03f3b88532a726fa33ca202ce72a8e76ef2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 22 Nov 2020 00:21:19 +0200 Subject: [PATCH 61/98] Fix a window bug in CGB mode, fixes #123 --- Core/display.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/display.c b/Core/display.c index b57317b..c876599 100644 --- a/Core/display.c +++ b/Core/display.c @@ -1021,7 +1021,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) 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]) { + if (gb->position_in_line == scx_to_wx0_comparisons[gb->io_registers[GB_IO_SCX] & 7] && !GB_is_cgb(gb)) { should_activate_window = true; } } From 0485124076aef3e66c9b1095ec0b30ee2329ab51 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 28 Nov 2020 19:31:25 +0200 Subject: [PATCH 62/98] Redo channel 1 sweep based on DMG schematics; emulates two newly discovered behaviors and also fixes #309 --- Core/apu.c | 51 ++++++++++++++++++++----------------------------- Core/apu.h | 4 ++-- Core/debugger.c | 2 +- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 2b5dc3b..9275f81 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -230,15 +230,6 @@ static void render(GB_gameboy_t *gb) gb->apu_output.sample_callback(gb, &filtered_output); } -static uint16_t new_sweep_sample_length(GB_gameboy_t *gb) -{ - 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_length - delta; - } - return gb->apu.shadow_sweep_sample_length + delta; -} - static void update_square_sample(GB_gameboy_t *gb, unsigned index) { if (gb->apu.square_channels[index].current_sample_index & 0x80) return; @@ -405,21 +396,22 @@ void GB_apu_div_event(GB_gameboy_t *gb) } if ((gb->apu.div_divider & 3) == 3) { - if (!gb->apu.sweep_enabled) { - return; - } if (gb->apu.square_sweep_countdown) { - if (!--gb->apu.square_sweep_countdown) { + if (!--gb->apu.square_sweep_countdown && gb->apu.sweep_enabled) { 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_length = - gb->apu.new_sweep_sample_length; + gb->apu.sweep_length_addend + gb->apu.shadow_sweep_sample_length + !!(gb->io_registers[GB_IO_NR10] & 0x8); + gb->apu.square_channels[GB_SQUARE_1].sample_length &= 0x7FF; } + gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; + gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); 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_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; } + + gb->apu.sweep_enabled = gb->io_registers[GB_IO_NR10] & 0x70; 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; @@ -447,13 +439,14 @@ 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_length = new_sweep_sample_length(gb); - if (gb->apu.new_sweep_sample_length > 0x7ff) { + gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length; + if (gb->io_registers[GB_IO_NR10] & 8) { + gb->apu.sweep_length_addend ^= 0x7FF; + } + if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) { 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; } } @@ -672,10 +665,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) /* Square channels */ case GB_IO_NR10: - if (gb->apu.sweep_decreasing && !(value & 8)) { + if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) { gb->apu.is_active[GB_SQUARE_1] = false; update_sample(gb, GB_SQUARE_1, 0, 0); - gb->apu.sweep_enabled = false; gb->apu.square_sweep_calculate_countdown = 0; } if ((value & 0x70) == 0) { @@ -744,12 +736,12 @@ 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_length = - gb->apu.new_sweep_sample_length = - gb->apu.square_channels[0].sample_length; - } if (value & 0x80) { + if (index == GB_SQUARE_1) { + gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; + gb->apu.shadow_sweep_sample_length = 0; + gb->apu.sweep_enabled = gb->io_registers[GB_IO_NR10] & 0x70; + } /* Current sample index remains unchanged when restarting channels 1 or 2. It is only reset by turning the APU off. */ if (!gb->apu.is_active[index]) { @@ -782,15 +774,14 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } if (index == GB_SQUARE_1) { - gb->apu.sweep_decreasing = false; if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ - gb->apu.square_sweep_calculate_countdown = 0x13 - gb->apu.lf_div; + gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; + gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); } else { gb->apu.square_sweep_calculate_countdown = 0; } - gb->apu.sweep_enabled = gb->io_registers[GB_IO_NR10] & 0x77; 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; } diff --git a/Core/apu.h b/Core/apu.h index a3a36a6..0512723 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -64,10 +64,10 @@ typedef struct uint8_t square_sweep_countdown; // In 128Hz uint8_t square_sweep_calculate_countdown; // In 2 MHz - uint16_t new_sweep_sample_length; + uint16_t sweep_length_addend; uint16_t shadow_sweep_sample_length; bool sweep_enabled; - bool sweep_decreasing; + GB_PADDING(bool, sweep_decreasing); struct { uint16_t pulse_length; // Reloaded from NRX1 (xorred), in 256Hz DIV ticks diff --git a/Core/debugger.c b/Core/debugger.c index 3e1b66c..28db01c 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1780,7 +1780,7 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg 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->io_registers[GB_IO_NR10] & 0x8) ? "decreasing" : "increasing", gb->apu.square_sweep_calculate_countdown); } From 74cf452a48d7ad6676b2e5f5580bf8e47b2178de Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 1 Dec 2020 14:17:35 +0200 Subject: [PATCH 63/98] Further accuracy improvements to sweep; passes Blargg's APU tests again, this time for real --- Core/apu.c | 73 +++++++++++++++++++++++-------------------------- Core/apu.h | 2 +- Core/debugger.c | 15 +++++++--- Core/rumble.c | 2 +- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 9275f81..c568d08 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -329,6 +329,24 @@ static void tick_noise_envelope(GB_gameboy_t *gb) } } +static void trigger_sweep_calculation(GB_gameboy_t *gb) +{ + if ((gb->io_registers[GB_IO_NR10] & 0x70) && gb->apu.square_sweep_countdown == 7) { + if (gb->io_registers[GB_IO_NR10] & 0x07) { + gb->apu.square_channels[GB_SQUARE_1].sample_length = + gb->apu.sweep_length_addend + gb->apu.shadow_sweep_sample_length + !!(gb->io_registers[GB_IO_NR10] & 0x8); + gb->apu.square_channels[GB_SQUARE_1].sample_length &= 0x7FF; + } + gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; + gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); + + /* Recalculation and overflow check only occurs after a delay */ + gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; + + gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; + } +} + void GB_apu_div_event(GB_gameboy_t *gb) { if (!gb->apu.global_enable) return; @@ -396,27 +414,9 @@ void GB_apu_div_event(GB_gameboy_t *gb) } if ((gb->apu.div_divider & 3) == 3) { - if (gb->apu.square_sweep_countdown) { - if (!--gb->apu.square_sweep_countdown && gb->apu.sweep_enabled) { - 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.sweep_length_addend + gb->apu.shadow_sweep_sample_length + !!(gb->io_registers[GB_IO_NR10] & 0x8); - gb->apu.square_channels[GB_SQUARE_1].sample_length &= 0x7FF; - } - gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; - gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); - - if (gb->io_registers[GB_IO_NR10] & 0x70) { - /* Recalculation and overflow check only occurs after a delay */ - gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; - } - - gb->apu.sweep_enabled = gb->io_registers[GB_IO_NR10] & 0x70; - - 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; - } - } + gb->apu.square_sweep_countdown++; + gb->apu.square_sweep_countdown &= 7; + trigger_sweep_calculation(gb); } } @@ -433,7 +433,8 @@ void GB_apu_run(GB_gameboy_t *gb) 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 && + ((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.square_sweep_calculate_countdown <= 7)) { // Calculation is paused if the lower bits if (gb->apu.square_sweep_calculate_countdown > cycles) { gb->apu.square_sweep_calculate_countdown -= cycles; } @@ -664,18 +665,16 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) break; /* Square channels */ - case GB_IO_NR10: - if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend > 0x7FF && !(gb->io_registers[GB_IO_NR10] & 8)) { + case GB_IO_NR10:{ + bool old_negate = gb->io_registers[GB_IO_NR10] & 8; + gb->io_registers[GB_IO_NR10] = value; + if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend + old_negate > 0x7FF && !(value & 8)) { gb->apu.is_active[GB_SQUARE_1] = false; update_sample(gb, GB_SQUARE_1, 0, 0); - gb->apu.square_sweep_calculate_countdown = 0; - } - if ((value & 0x70) == 0) { - /* Todo: what happens if we set period to 0 while a calculate event is scheduled, and then - re-set it to non-zero? */ - gb->apu.square_sweep_calculate_countdown = 0; } + trigger_sweep_calculation(gb); break; + } case GB_IO_NR11: case GB_IO_NR21: { @@ -737,11 +736,6 @@ 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 (value & 0x80) { - if (index == GB_SQUARE_1) { - gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; - gb->apu.shadow_sweep_sample_length = 0; - gb->apu.sweep_enabled = gb->io_registers[GB_IO_NR10] & 0x70; - } /* Current sample index remains unchanged when restarting channels 1 or 2. It is only reset by turning the APU off. */ if (!gb->apu.is_active[index]) { @@ -776,16 +770,17 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) if (index == GB_SQUARE_1) { if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ + gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length; gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; + gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); } else { - gb->apu.square_sweep_calculate_countdown = 0; + gb->apu.shadow_sweep_sample_length = 0; + gb->apu.sweep_length_addend = 0; } - 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; + gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } - } /* 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 0512723..a8b5697 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -66,7 +66,7 @@ typedef struct uint8_t square_sweep_calculate_countdown; // In 2 MHz uint16_t sweep_length_addend; uint16_t shadow_sweep_sample_length; - bool sweep_enabled; + GB_PADDING(bool, sweep_enabled); GB_PADDING(bool, sweep_decreasing); struct { diff --git a/Core/debugger.c b/Core/debugger.c index 28db01c..9825d95 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1778,10 +1778,17 @@ static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugg 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->io_registers[GB_IO_NR10] & 0x8) ? "decreasing" : "increasing", - gb->apu.square_sweep_calculate_countdown); + GB_log(gb, " Frequency sweep %s and %s\n", + ((gb->io_registers[GB_IO_NR10] & 0x7) && (gb->io_registers[GB_IO_NR10] & 0x70))? "active" : "inactive", + (gb->io_registers[GB_IO_NR10] & 0x8) ? "decreasing" : "increasing"); + if (gb->apu.square_sweep_calculate_countdown) { + GB_log(gb, " On going frequency calculation will be ready in %u APU ticks\n", + gb->apu.square_sweep_calculate_countdown); + } + else { + GB_log(gb, " Shadow frequency register: 0x%03x\n", gb->apu.shadow_sweep_sample_length); + GB_log(gb, " Sweep addend register: 0x%03x\n", gb->apu.sweep_length_addend); + } } if (gb->apu.square_channels[channel].length_enabled) { diff --git a/Core/rumble.c b/Core/rumble.c index 8cbe20d..22321fb 100644 --- a/Core/rumble.c +++ b/Core/rumble.c @@ -32,7 +32,7 @@ void GB_handle_rumble(GB_gameboy_t *gb) ch4_rumble = MAX(ch4_rumble, 0.0); double ch1_rumble = 0; - if (gb->apu.sweep_enabled && ((gb->io_registers[GB_IO_NR10] >> 4) & 7)) { + if ((gb->io_registers[GB_IO_NR10] & 0x7) && (gb->io_registers[GB_IO_NR10] & 0x70)) { 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); From 13bc8679f9b4cc14290bf12644224c08154b18aa Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 1 Dec 2020 14:18:19 +0200 Subject: [PATCH 64/98] Correct preservation of NRx1's state on pre-CGB models --- Core/apu.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index c568d08..22808da 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -633,11 +633,11 @@ 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], - gb->io_registers[GB_IO_NR31], - gb->io_registers[GB_IO_NR41] + uint8_t old_pulse_lengths[] = { + gb->apu.square_channels[0].pulse_length, + gb->apu.square_channels[1].pulse_length, + gb->apu.wave_channel.pulse_length, + gb->apu.noise_channel.pulse_length }; if ((value & 0x80) && !gb->apu.global_enable) { GB_apu_init(gb); @@ -649,17 +649,14 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } memset(&gb->apu, 0, sizeof(gb->apu)); 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]); - GB_apu_write(gb, GB_IO_NR31, old_nrx1[2]); - GB_apu_write(gb, GB_IO_NR41, old_nrx1[3]); + gb->apu.square_channels[0].pulse_length = old_pulse_lengths[0]; + gb->apu.square_channels[1].pulse_length = old_pulse_lengths[1]; + gb->apu.wave_channel.pulse_length = old_pulse_lengths[2]; + gb->apu.noise_channel.pulse_length = old_pulse_lengths[3]; } } break; From 1baa0446a94c9cc1e2400c31303bbf00a3fd3821 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 1 Dec 2020 22:37:13 +0200 Subject: [PATCH 65/98] More sweep improvements --- Core/apu.c | 29 ++++++++++++++++++++++------- Core/apu.h | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 22808da..4366fb2 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -337,11 +337,13 @@ static void trigger_sweep_calculation(GB_gameboy_t *gb) gb->apu.sweep_length_addend + gb->apu.shadow_sweep_sample_length + !!(gb->io_registers[GB_IO_NR10] & 0x8); gb->apu.square_channels[GB_SQUARE_1].sample_length &= 0x7FF; } - gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; - gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); + if (gb->apu.channel_1_restart_hold == 0) { + gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; + gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); + } /* Recalculation and overflow check only occurs after a delay */ - gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; + gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } @@ -440,7 +442,9 @@ void GB_apu_run(GB_gameboy_t *gb) } else { /* APU bug: sweep frequency is checked after adding the sweep delta twice */ - gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length; + if (gb->apu.channel_1_restart_hold == 0) { + gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length; + } if (gb->io_registers[GB_IO_NR10] & 8) { gb->apu.sweep_length_addend ^= 0x7FF; } @@ -451,6 +455,15 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.square_sweep_calculate_countdown = 0; } } + + if (gb->apu.channel_1_restart_hold) { + if (gb->apu.channel_1_restart_hold > cycles) { + gb->apu.channel_1_restart_hold -= cycles; + } + else { + gb->apu.channel_1_restart_hold = 0; + } + } UNROLL for (unsigned i = GB_SQUARE_1; i <= GB_SQUARE_2; i++) { @@ -665,7 +678,9 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR10:{ bool old_negate = gb->io_registers[GB_IO_NR10] & 8; gb->io_registers[GB_IO_NR10] = value; - if (gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend + old_negate > 0x7FF && !(value & 8)) { + if (gb->apu.square_sweep_calculate_countdown == 0 && + gb->apu.shadow_sweep_sample_length + gb->apu.sweep_length_addend + old_negate > 0x7FF && + !(value & 8)) { gb->apu.is_active[GB_SQUARE_1] = false; update_sample(gb, GB_SQUARE_1, 0, 0); } @@ -765,17 +780,17 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } if (index == GB_SQUARE_1) { + gb->apu.shadow_sweep_sample_length = 0; if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ - gb->apu.shadow_sweep_sample_length = gb->apu.square_channels[GB_SQUARE_1].sample_length; gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); } else { - gb->apu.shadow_sweep_sample_length = 0; gb->apu.sweep_length_addend = 0; } + gb->apu.channel_1_restart_hold = 4 - gb->apu.lf_div; gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } } diff --git a/Core/apu.h b/Core/apu.h index a8b5697..07182c9 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -120,6 +120,7 @@ typedef struct uint8_t skip_div_event; bool current_lfsr_sample; uint8_t pcm_mask[2]; // For CGB-0 to CGB-C PCM read glitch + uint8_t channel_1_restart_hold; } GB_apu_t; typedef enum { From 7de6194e28771d1231d4ca1618f82c5977ab1514 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 16:02:25 +0200 Subject: [PATCH 66/98] Redo channel 4's timing accurately, emulate NR43 write quirks --- Core/apu.c | 127 ++++++++++++++++++++++++++++++------------------ Core/apu.h | 8 ++- Core/debugger.c | 6 +-- Core/rumble.c | 7 ++- 4 files changed, 93 insertions(+), 55 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 4366fb2..d689515 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -422,6 +422,28 @@ void GB_apu_div_event(GB_gameboy_t *gb) } } +static void step_lfsr(GB_gameboy_t *gb, unsigned cycles_offset) +{ + unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; + 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->apu.is_active[GB_NOISE]) { + update_sample(gb, GB_NOISE, + gb->apu.current_lfsr_sample ? + gb->apu.noise_channel.current_volume : 0, + cycles_offset); + } +} void GB_apu_run(GB_gameboy_t *gb) { @@ -506,39 +528,38 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.wave_channel.wave_form_just_read = false; } } - - if (gb->apu.is_active[GB_NOISE]) { + + // The noise channel can step even if inactive on the DMG + if (gb->apu.is_active[GB_NOISE] || !GB_is_cgb(gb)) { 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; + unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; + if (!divisor) divisor = 2; + if (gb->apu.noise_channel.counter_countdown == 0) { + gb->apu.noise_channel.counter_countdown = divisor; + } + while (unlikely(cycles_left >= gb->apu.noise_channel.counter_countdown)) { + cycles_left -= gb->apu.noise_channel.counter_countdown; + gb->apu.noise_channel.counter_countdown = divisor + gb->apu.channel_4_delta; + gb->apu.channel_4_delta = 0; + bool old_bit = (gb->apu.noise_channel.counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; + gb->apu.noise_channel.counter++; + gb->apu.noise_channel.counter &= 0x3FFF; + bool new_bit = (gb->apu.noise_channel.counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; /* Step LFSR */ - unsigned high_bit_mask = gb->apu.noise_channel.narrow ? 0x4040 : 0x4000; - 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 (new_bit && !old_bit) { + step_lfsr(gb, cycles - cycles_left); + if (cycles_left == 0 && gb->apu.samples[GB_NOISE] == 0) { + gb->apu.pcm_mask[1] &= 0x0F; + } } - 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 (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, - 0); } if (cycles_left) { - gb->apu.noise_channel.sample_countdown -= cycles_left; + gb->apu.noise_channel.counter_countdown -= cycles_left; + gb->apu.channel_4_countdown_reloaded = false; + } + else { + gb->apu.channel_4_countdown_reloaded = true; } } } @@ -938,33 +959,43 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) 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. */ + bool old_bit = (gb->apu.noise_channel.counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; + gb->io_registers[GB_IO_NR43] = value; + bool new_bit = (gb->apu.noise_channel.counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; + if (gb->apu.channel_4_countdown_reloaded) { + unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; + if (!divisor) divisor = 2; + gb->apu.noise_channel.counter_countdown = + divisor + (divisor == 2? 0 : (uint8_t[]){2, 1, 0, 3}[(gb->apu.noise_channel.alignment) & 3]); + gb->apu.channel_4_delta = 0; + } + /* Step LFSR */ + if (new_bit && !old_bit) { + step_lfsr(gb, 0); + } 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) { - if ((gb->io_registers[GB_IO_NR43] & 7) == 1) { - gb->apu.noise_channel.sample_countdown += 2; - } - else { - gb->apu.noise_channel.sample_countdown -= 2; - } + unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; + if (!divisor) divisor = 2; + gb->apu.channel_4_delta = 0; + gb->apu.noise_channel.counter_countdown = divisor + 4; + if (divisor == 2) { + gb->apu.noise_channel.counter_countdown += 1 - gb->apu.lf_div; } - if (gb->apu.is_active[GB_NOISE]) { - gb->apu.noise_channel.sample_countdown += 2; + else { + gb->apu.noise_channel.counter_countdown += (uint8_t[]){2, 1, 0, 3}[gb->apu.noise_channel.alignment & 3]; + if (((gb->apu.noise_channel.alignment + 1) & 3) < 2) { + if ((gb->io_registers[GB_IO_NR43] & 0x07) == 1) { + gb->apu.noise_channel.counter_countdown -= 2; + gb->apu.channel_4_delta = 2; + } + else { + gb->apu.noise_channel.counter_countdown -= 4; + } + } } gb->apu.noise_channel.current_volume = gb->io_registers[GB_IO_NR42] >> 4; diff --git a/Core/apu.h b/Core/apu.h index 07182c9..df1be40 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -105,8 +105,9 @@ typedef struct 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 + uint8_t counter_countdown; // Counts from 0-7 to 0 to tick counter (Scaled from 512KHz to 2MHz) + uint8_t __padding; + uint16_t counter; // A bit from this 14-bit register ticks LFSR bool length_enabled; // NR44 uint8_t alignment; // If (NR43 & 7) != 0, samples are aligned to 512KHz clock instead of @@ -121,6 +122,9 @@ typedef struct bool current_lfsr_sample; uint8_t pcm_mask[2]; // For CGB-0 to CGB-C PCM read glitch uint8_t channel_1_restart_hold; + int8_t channel_4_delta; + bool channel_4_countdown_reloaded; + } GB_apu_t; typedef enum { diff --git a/Core/debugger.c b/Core/debugger.c index 9825d95..e62fd51 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1821,10 +1821,10 @@ 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_log(gb, " Current volume: %u, current internal counter: 0x%x (next increase in %u ticks)\n", gb->apu.noise_channel.current_volume, - gb->apu.noise_channel.sample_length * 4 + 3, - gb->apu.noise_channel.sample_countdown); + gb->apu.noise_channel.counter, + gb->apu.noise_channel.counter_countdown); GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n", gb->apu.noise_channel.volume_countdown, diff --git a/Core/rumble.c b/Core/rumble.c index 22321fb..87eb870 100644 --- a/Core/rumble.c +++ b/Core/rumble.c @@ -25,8 +25,11 @@ void GB_handle_rumble(GB_gameboy_t *gb) 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; + unsigned ch4_divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 1; + if (!ch4_divisor) ch4_divisor = 1; + unsigned ch4_sample_length = (ch4_divisor << (gb->io_registers[GB_IO_NR43] >> 4)) - 1; + + double ch4_rumble = (MIN(ch4_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); From 6b30de5fb1c9c7c177ea544db9fe82e7d40a2171 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 16:02:46 +0200 Subject: [PATCH 67/98] Fixed dark colors on Metal without frame blending --- Shaders/MasterShader.metal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shaders/MasterShader.metal b/Shaders/MasterShader.metal index b900176..2f3113e 100644 --- a/Shaders/MasterShader.metal +++ b/Shaders/MasterShader.metal @@ -66,7 +66,7 @@ fragment float4 fragment_shader(rasterizer_data in [[stage_in]], switch (*frame_blending_mode) { default: case DISABLED: - return scale(image, in.texcoords, input_resolution, *output_resolution); + return pow(scale(image, in.texcoords, input_resolution, *output_resolution), 1 / GAMMA); case SIMPLE: ratio = 0.5; break; From dffc12331b0dc1ac8bd1df705f9858706ebc8713 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 18:11:35 +0200 Subject: [PATCH 68/98] Emulate the delayed NR44 write on the DMG --- Core/apu.c | 94 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index d689515..fdd21db 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -451,6 +451,22 @@ void GB_apu_run(GB_gameboy_t *gb) uint8_t cycles = gb->apu.apu_cycles >> 2; gb->apu.apu_cycles = 0; if (!cycles) return; + bool start_ch4 = false; + if (gb->apu.channel_4_dmg_delayed_start) { + if (gb->apu.channel_4_dmg_delayed_start == cycles) { + gb->apu.channel_4_dmg_delayed_start = 0; + start_ch4 = true; + } + else if (gb->apu.channel_4_dmg_delayed_start > cycles) { + gb->apu.channel_4_dmg_delayed_start -= cycles; + } + else { + /* Split it into two */ + cycles -= gb->apu.channel_4_dmg_delayed_start; + gb->apu.apu_cycles = gb->apu.channel_4_dmg_delayed_start * 2; + GB_apu_run(gb); + } + } if (likely(!gb->stopped || GB_is_cgb(gb))) { /* To align the square signal to 1MHz */ @@ -572,6 +588,9 @@ void GB_apu_run(GB_gameboy_t *gb) render(gb); } } + if (start_ch4) { + GB_apu_write(gb, GB_IO_NR44, gb->io_registers[GB_IO_NR44] | 0x80); + } } void GB_apu_init(GB_gameboy_t *gb) { @@ -978,49 +997,54 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR44: { if (value & 0x80) { - unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; - if (!divisor) divisor = 2; - gb->apu.channel_4_delta = 0; - gb->apu.noise_channel.counter_countdown = divisor + 4; - if (divisor == 2) { - gb->apu.noise_channel.counter_countdown += 1 - gb->apu.lf_div; + if (!GB_is_cgb(gb) && (gb->apu.noise_channel.alignment & 3) != 0) { + gb->apu.channel_4_dmg_delayed_start = 6; } else { - gb->apu.noise_channel.counter_countdown += (uint8_t[]){2, 1, 0, 3}[gb->apu.noise_channel.alignment & 3]; - if (((gb->apu.noise_channel.alignment + 1) & 3) < 2) { - if ((gb->io_registers[GB_IO_NR43] & 0x07) == 1) { - gb->apu.noise_channel.counter_countdown -= 2; - gb->apu.channel_4_delta = 2; - } - else { - gb->apu.noise_channel.counter_countdown -= 4; + unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; + if (!divisor) divisor = 2; + gb->apu.channel_4_delta = 0; + gb->apu.noise_channel.counter_countdown = divisor + 4; + if (divisor == 2) { + gb->apu.noise_channel.counter_countdown += 1 - gb->apu.lf_div; + } + else { + gb->apu.noise_channel.counter_countdown += (uint8_t[]){2, 1, 0, 3}[gb->apu.noise_channel.alignment & 3]; + if (((gb->apu.noise_channel.alignment + 1) & 3) < 2) { + if ((gb->io_registers[GB_IO_NR43] & 0x07) == 1) { + gb->apu.noise_channel.counter_countdown -= 2; + gb->apu.channel_4_delta = 2; + } + else { + gb->apu.noise_channel.counter_countdown -= 4; + } } } - } - gb->apu.noise_channel.current_volume = gb->io_registers[GB_IO_NR42] >> 4; + 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. */ - if (gb->apu.is_active[GB_NOISE]) { - update_sample(gb, GB_NOISE, - gb->apu.current_lfsr_sample ? - gb->apu.noise_channel.current_volume : 0, - 0); - } - 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; + /* 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[GB_NOISE]) { + update_sample(gb, GB_NOISE, + gb->apu.current_lfsr_sample ? + gb->apu.noise_channel.current_volume : 0, + 0); + } + 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.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; + if (gb->apu.noise_channel.pulse_length == 0) { + gb->apu.noise_channel.pulse_length = 0x40; + gb->apu.noise_channel.length_enabled = false; + } } } From 4f408eae7cc2d390ff941a8669d0f513d61fd915 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 18:13:55 +0200 Subject: [PATCH 69/98] Whoops --- Core/apu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/apu.h b/Core/apu.h index df1be40..9d5fc80 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -124,7 +124,7 @@ typedef struct uint8_t channel_1_restart_hold; int8_t channel_4_delta; bool channel_4_countdown_reloaded; - + uint8_t channel_4_dmg_delayed_start; } GB_apu_t; typedef enum { From 770885440f5fe656d6dc04acdcfa77a4ef711a85 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 19:09:53 +0200 Subject: [PATCH 70/98] Minor changes to debugger output --- Core/debugger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/debugger.c b/Core/debugger.c index e62fd51..f6b4e4f 100644 --- a/Core/debugger.c +++ b/Core/debugger.c @@ -1821,7 +1821,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 internal counter: 0x%x (next increase in %u ticks)\n", + GB_log(gb, " Current volume: %u, current internal counter: 0x%04x (next increase in %u ticks)\n", gb->apu.noise_channel.current_volume, gb->apu.noise_channel.counter, gb->apu.noise_channel.counter_countdown); From 555835549a6239fda6051873a5363a20ee9b93dc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 20:35:18 +0200 Subject: [PATCH 71/98] More accurate pausing behavior, including revision differences --- Core/apu.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index fdd21db..e37b265 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -474,7 +474,8 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.noise_channel.alignment += cycles; if (gb->apu.square_sweep_calculate_countdown && - ((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.square_sweep_calculate_countdown <= 7)) { // Calculation is paused if the lower bits + ((gb->io_registers[GB_IO_NR10] & 7) || + gb->apu.square_sweep_calculate_countdown <= (gb->model > GB_MODEL_CGB_C? 3 : 1))) { // Calculation is paused if the lower bits if (gb->apu.square_sweep_calculate_countdown > cycles) { gb->apu.square_sweep_calculate_countdown -= cycles; } @@ -823,7 +824,10 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.shadow_sweep_sample_length = 0; if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ - gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 7 - gb->apu.lf_div; + gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; + if (gb->model > GB_MODEL_CGB_C) { + gb->apu.square_sweep_calculate_countdown += 2; + } gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; gb->apu.sweep_length_addend >>= (gb->io_registers[GB_IO_NR10] & 7); } From db483ce95f31655fec74ddc53495d78f33903c77 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 20:40:35 +0200 Subject: [PATCH 72/98] Warn about potential odd-mode triggers --- Core/sm83_cpu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 7107ed1..5631853 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -351,6 +351,9 @@ static void leave_stop_mode(GB_gameboy_t *gb) static void stop(GB_gameboy_t *gb, uint8_t opcode) { if (gb->io_registers[GB_IO_KEY1] & 0x1) { + if (gb->cgb_double_speed && gb->io_registers[GB_IO_LCDC] & 0x80) { + GB_log(gb, "Returning from double speed mode while the PPU is on may trigger odd-mode\n"); + } flush_pending_cycles(gb); bool needs_alignment = false; From 7a3ebb708c528f3e1c069f03bc1a185b1b02744b Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 12 Dec 2020 22:55:14 +0200 Subject: [PATCH 73/98] LCDC write timing regression fix --- Core/sm83_cpu.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index 5631853..c93de07 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -249,6 +249,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) // Todo: This is difference is because my timing is off in one of the models if (gb->model > GB_MODEL_CGB_C) { GB_advance_cycles(gb, gb->pending_cycles); + GB_write_memory(gb, addr, value ^ 0x10); // Write with the old TILE_SET first gb->tile_sel_glitch = true; GB_advance_cycles(gb, 1); gb->tile_sel_glitch = false; @@ -257,6 +258,7 @@ static void cycle_write(GB_gameboy_t *gb, uint16_t addr, uint8_t value) } else { GB_advance_cycles(gb, gb->pending_cycles - 1); + GB_write_memory(gb, addr, value ^ 0x10); // Write with the old TILE_SET first gb->tile_sel_glitch = true; GB_advance_cycles(gb, 1); gb->tile_sel_glitch = false; From 8a13b7be24cf412b0556875d7a8607c7e21462b1 Mon Sep 17 00:00:00 2001 From: Dalton Messmer <33463986+messmerd@users.noreply.github.com> Date: Sat, 19 Dec 2020 00:58:19 -0500 Subject: [PATCH 74/98] Add .gitattributes line ending settings Always use LF line endings for shaders --- .gitattributes | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitattributes b/.gitattributes index 427cb28..2149ea1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,7 @@ +# Always use LF line endings for shaders +*.fsh text eol=lf +*.metal text eol=lf + HexFiend/* linguist-vendored *.inc linguist-language=C Core/*.h linguist-language=C From 8f64f49c3be597f57377e4926ca2a61f2848c8c9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 23 Dec 2020 23:49:57 +0200 Subject: [PATCH 75/98] More accurate emulation of window timing, actual correct fix of #123 --- Core/display.c | 44 +++++++++++++++++++++----------------------- Core/sm83_cpu.c | 5 ++--- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Core/display.c b/Core/display.c index c876599..5627678 100644 --- a/Core/display.c +++ b/Core/display.c @@ -821,7 +821,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) GB_STATE(gb, display, 28); GB_STATE(gb, display, 29); GB_STATE(gb, display, 30); - // GB_STATE(gb, display, 31); + GB_STATE(gb, display, 31); GB_STATE(gb, display, 32); GB_STATE(gb, display, 33); GB_STATE(gb, display, 34); @@ -853,13 +853,7 @@ 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->wy_triggered = false; gb->ly_for_comparison = 0; gb->io_registers[GB_IO_STAT] &= ~3; @@ -910,11 +904,6 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) /* Lines 0 - 143 */ gb->window_y = -1; for (; gb->current_line < LINES; gb->current_line++) { - /* 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->oam_write_blocked = GB_is_cgb(gb) && !gb->cgb_double_speed; gb->accessed_oam_row = 0; @@ -994,6 +983,12 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_for_line += 2; GB_SLEEP(gb, display, 32, 2); mode_3_start: + /* Todo: verify timings */ + if ((gb->io_registers[GB_IO_LCDC] & 0x20) && + (gb->io_registers[GB_IO_WY] == 0) && + gb->current_line == 0) { + gb->wy_triggered = true; + } fifo_clear(&gb->bg_fifo); fifo_clear(&gb->oam_fifo); @@ -1021,7 +1016,7 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) 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] && !GB_is_cgb(gb)) { + if (gb->position_in_line == scx_to_wx0_comparisons[gb->io_registers[GB_IO_SCX] & 7]) { should_activate_window = true; } } @@ -1243,7 +1238,17 @@ abort_fetching_object: if (gb->hdma_on_hblank) { gb->hdma_starting = true; } - GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line); + GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line - 2); + /* + TODO: Verify double speed timing + TODO: Line 0 behaves differently when enabling the window during the transition between mode 2 to 3 + */ + if ((gb->io_registers[GB_IO_LCDC] & 0x20) && + (gb->io_registers[GB_IO_WY] == gb->current_line || + gb->io_registers[GB_IO_WY] == gb->current_line + 1)) { + gb->wy_triggered = true; + } + GB_SLEEP(gb, display, 31, 2); gb->mode_for_interrupt = 2; // Todo: unverified timing @@ -1337,14 +1342,7 @@ 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; - } + gb->wy_triggered = false; // TODO: not the correct timing gb->current_lcd_line = 0; diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index c93de07..e423fba 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -380,9 +380,8 @@ 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. */ - gb->interrupt_enable = 0; + /* TODO: HW Bug? When STOP is executed while a button is down, the CPU enters halt + mode instead. Fine details not confirmed yet. */ gb->halted = true; } else { From aa2bdf2a1c2e1e004703b6e8bb61cb1cd7e257fc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 23 Dec 2020 23:50:19 +0200 Subject: [PATCH 76/98] Better support for non-QWERTY Latin layouts --- SDL/gui.c | 4 ++-- SDL/gui.h | 9 +++++++++ SDL/main.c | 3 +-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 62656e8..3848d15 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1206,7 +1206,7 @@ void run_gui(bool is_running) } case SDL_KEYDOWN: - if (event.key.keysym.scancode == SDL_SCANCODE_F && event.key.keysym.mod & MODIFIER) { + if (event_hotkey_code(&event) == SDL_SCANCODE_F && event.key.keysym.mod & MODIFIER) { if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) { SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); } @@ -1215,7 +1215,7 @@ void run_gui(bool is_running) } update_viewport(); } - if (event.key.keysym.scancode == SDL_SCANCODE_O) { + if (event_hotkey_code(&event) == SDL_SCANCODE_O) { if (event.key.keysym.mod & MODIFIER) { char *filename = do_open_rom_dialog(); if (filename) { diff --git a/SDL/gui.h b/SDL/gui.h index c950907..f55464d 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -122,4 +122,13 @@ void connect_joypad(void); joypad_button_t get_joypad_button(uint8_t physical_button); joypad_axis_t get_joypad_axis(uint8_t physical_axis); +static SDL_Scancode event_hotkey_code(SDL_Event *event) +{ + if (event->key.keysym.sym >= SDLK_a && event->key.keysym.sym < SDLK_z) { + return SDL_SCANCODE_A + event->key.keysym.sym - SDLK_a; + } + + return event->key.keysym.scancode; +} + #endif diff --git a/SDL/main.c b/SDL/main.c index e79d0b3..45d016d 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -233,7 +233,7 @@ static void handle_events(GB_gameboy_t *gb) }; case SDL_KEYDOWN: - switch (event.key.keysym.scancode) { + switch (event_hotkey_code(&event)) { case SDL_SCANCODE_ESCAPE: { open_menu(); break; @@ -241,7 +241,6 @@ static void handle_events(GB_gameboy_t *gb) case SDL_SCANCODE_C: if (event.type == SDL_KEYDOWN && (event.key.keysym.mod & KMOD_CTRL)) { GB_debugger_break(gb); - } break; From 66f62d696c9664121887de43c33d234c17f17e63 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 24 Dec 2020 20:50:47 +0200 Subject: [PATCH 77/98] More window fixes --- Core/display.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Core/display.c b/Core/display.c index 5627678..031b23d 100644 --- a/Core/display.c +++ b/Core/display.c @@ -983,10 +983,9 @@ void GB_display_run(GB_gameboy_t *gb, uint8_t cycles) gb->cycles_for_line += 2; GB_SLEEP(gb, display, 32, 2); mode_3_start: - /* Todo: verify timings */ + /* TODO: Timing seems incorrect, might need an access conflict handling. */ if ((gb->io_registers[GB_IO_LCDC] & 0x20) && - (gb->io_registers[GB_IO_WY] == 0) && - gb->current_line == 0) { + gb->io_registers[GB_IO_WY] == gb->current_line) { gb->wy_triggered = true; } @@ -1241,11 +1240,10 @@ abort_fetching_object: GB_SLEEP(gb, display, 11, LINE_LENGTH - gb->cycles_for_line - 2); /* TODO: Verify double speed timing - TODO: Line 0 behaves differently when enabling the window during the transition between mode 2 to 3 + TODO: Timing differs on a DMG */ if ((gb->io_registers[GB_IO_LCDC] & 0x20) && - (gb->io_registers[GB_IO_WY] == gb->current_line || - gb->io_registers[GB_IO_WY] == gb->current_line + 1)) { + (gb->io_registers[GB_IO_WY] == gb->current_line)) { gb->wy_triggered = true; } GB_SLEEP(gb, display, 31, 2); From b5a611c5db46d6a0649d04d24d8d6339200f9ca1 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 24 Dec 2020 23:17:20 +0200 Subject: [PATCH 78/98] More accurate color correction curves --- Core/display.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/display.c b/Core/display.c index 031b23d..f5e81a8 100644 --- a/Core/display.c +++ b/Core/display.c @@ -215,12 +215,12 @@ static inline uint8_t scale_channel(uint8_t x) static inline uint8_t scale_channel_with_curve(uint8_t 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]; + return (uint8_t[]){0,6,12,20,28,36,45,56,66,76,88,100,113,125,137,149,161,172,182,192,202,210,218,225,232,238,243,247,250,252,254,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]; + return (uint8_t[]){0,3,8,14,20,26,33,40,47,54,62,70,78,86,94,103,112,120,129,138,147,157,166,176,185,195,205,215,225,235,245,255}[x]; } static inline uint8_t scale_channel_with_curve_sgb(uint8_t x) From 159d9d0348bbc14a71a59180f3c5199eb278dd07 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 25 Dec 2020 14:14:17 +0200 Subject: [PATCH 79/98] Color temperature control --- Cocoa/Document.m | 13 ++++++++++ Cocoa/GBPreferencesWindow.h | 2 +- Cocoa/GBPreferencesWindow.m | 20 +++++++++++++++ Cocoa/Preferences.xib | 5 ++-- Core/display.c | 51 ++++++++++++++++++++++++++++++++----- Core/display.h | 1 + Core/gb.h | 1 + SDL/gui.c | 43 +++++++++++++++++++++++++++++++ SDL/gui.h | 4 +++ SDL/main.c | 3 +++ 10 files changed, 134 insertions(+), 9 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ea7ef49..a354e03 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -689,6 +689,11 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) name:@"GBColorCorrectionChanged" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateLightTemperature) + name:@"GBLightTemperatureChanged" + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFrameBlendingMode) name:@"GBFrameBlendingModeChanged" @@ -1835,6 +1840,14 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) } } +- (void) updateLightTemperature +{ + if (GB_is_inited(&gb)) { + GB_set_light_temperature(&gb, [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBLightTemperature"]); + } +} + + - (void) updateFrameBlendingMode { self.view.frameBlendingMode = (GB_frame_blending_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBFrameBlendingMode"]; diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index ee697a8..f0b7506 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -17,7 +17,7 @@ @property (strong) IBOutlet NSMenuItem *bootROMsFolderItem; @property (strong) IBOutlet NSPopUpButtonCell *bootROMsButton; @property (strong) IBOutlet NSPopUpButton *rumbleModePopupButton; - +@property (weak) IBOutlet NSSlider *temperatureSlider; @property (weak) IBOutlet NSPopUpButton *dmgPopupButton; @property (weak) IBOutlet NSPopUpButton *sgbPopupButton; @property (weak) IBOutlet NSPopUpButton *cgbPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 491f0c0..dd13ca1 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -26,6 +26,7 @@ NSPopUpButton *_dmgPopupButton, *_sgbPopupButton, *_cgbPopupButton; NSPopUpButton *_preferredJoypadButton; NSPopUpButton *_rumbleModePopupButton; + NSSlider *_temperatureSlider; } + (NSArray *)filterList @@ -91,11 +92,23 @@ [_colorCorrectionPopupButton selectItemAtIndex:mode]; } + - (NSPopUpButton *)colorCorrectionPopupButton { return _colorCorrectionPopupButton; } +- (void)setTemperatureSlider:(NSSlider *)temperatureSlider +{ + _temperatureSlider = temperatureSlider; + [temperatureSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBLightTemperature"] * 256]; + temperatureSlider.continuous = YES; +} + +- (NSSlider *)temperatureSlider +{ + return _temperatureSlider; +} - (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton { _frameBlendingModePopupButton = frameBlendingModePopupButton; @@ -284,6 +297,13 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBColorCorrectionChanged" object:nil]; } +- (IBAction)lightTemperatureChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0) + forKey:@"GBLightTemperature"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil]; +} + - (IBAction)franeBlendingModeChanged:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 99c6543..73eb0ab 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -79,15 +79,16 @@ + - + - + diff --git a/Core/display.c b/Core/display.c index f5e81a8..6ac6be8 100644 --- a/Core/display.c +++ b/Core/display.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "gb.h" /* FIFO functions */ @@ -208,6 +209,26 @@ static void display_vblank(GB_gameboy_t *gb) GB_timing_sync(gb); } +static inline void temperature_tint(double temperature, double *r, double *g, double *b) +{ + if (temperature >= 0) { + *r = 1; + *g = pow(1 - temperature, 0.375); + if (temperature >= 0.75) { + *b = 0; + } + else { + *b = sqrt(0.75 - temperature); + } + } + else { + *b = 1; + double squared = pow(temperature, 2); + *g = 0.125 * squared + 0.3 * temperature + 1.0; + *r = 0.21875 * squared + 0.5 * temperature + 1.0; + } +} + static inline uint8_t scale_channel(uint8_t x) { return (x << 3) | (x >> 2); @@ -240,13 +261,12 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border) g = scale_channel(g); b = scale_channel(b); } + else if (GB_is_sgb(gb) || for_border) { + r = scale_channel_with_curve_sgb(r); + g = scale_channel_with_curve_sgb(g); + b = scale_channel_with_curve_sgb(b); + } else { - 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), - 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); @@ -301,6 +321,14 @@ uint32_t GB_convert_rgb15(GB_gameboy_t *gb, uint16_t color, bool for_border) } } + if (gb->light_temperature) { + double light_r, light_g, light_b; + temperature_tint(gb->light_temperature, &light_r, &light_g, &light_b); + r = round(light_r * r); + g = round(light_g * g); + b = round(light_b * b); + } + return gb->rgb_encode_callback(gb, r, g, b); } @@ -324,6 +352,17 @@ void GB_set_color_correction_mode(GB_gameboy_t *gb, GB_color_correction_mode_t m } } +void GB_set_light_temperature(GB_gameboy_t *gb, double temperature) +{ + gb->light_temperature = temperature; + if (GB_is_cgb(gb)) { + for (unsigned i = 0; i < 32; i++) { + GB_palette_changed(gb, false, i * 2); + GB_palette_changed(gb, true, i * 2); + } + } +} + /* STAT interrupt is implemented based on this finding: http://board.byuu.org/phpbb3/viewtopic.php?p=25527#p25531 diff --git a/Core/display.h b/Core/display.h index 5bdeba8..fdaf172 100644 --- a/Core/display.h +++ b/Core/display.h @@ -58,5 +58,6 @@ 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); +void GB_set_light_temperature(GB_gameboy_t *gb, double temperature); bool GB_is_odd_frame(GB_gameboy_t *gb); #endif /* display_h */ diff --git a/Core/gb.h b/Core/gb.h index ed736e0..c2e96db 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -573,6 +573,7 @@ struct GB_gameboy_internal_s { uint32_t sprite_palettes_rgb[0x20]; const GB_palette_t *dmg_palette; GB_color_correction_mode_t color_correction_mode; + double light_temperature; bool keys[4][GB_KEY_MAX]; GB_border_mode_t border_mode; GB_sgb_border_t borrowed_border; diff --git a/SDL/gui.c b/SDL/gui.c index 3848d15..63e42d8 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -110,6 +110,7 @@ configuration_t configuration = .volume = 100, .rumble_mode = GB_RUMBLE_ALL_GAMES, .default_scale = 2, + .color_temperature = 10, }; @@ -453,6 +454,33 @@ const char *current_color_correction_mode(unsigned index) [configuration.color_correction_mode]; } +const char *current_color_temperature(unsigned index) +{ + return (const char *[]){"12000K", + "11450K", + "10900K", + "10350K", + "9800K", + "9250K", + "8700K", + "8150K", + "7600K", + "7050K", + "6500K (White)", + "5950K", + "5400K", + "4850K", + "4300K", + "3750K", + "3200K", + "2650K", + "2100K", + "1550K", + "1000K"} + [configuration.color_temperature]; +} + + const char *current_palette(unsigned index) { return (const char *[]){"Greyscale", "Lime (Game Boy)", "Olive (Pocket)", "Teal (Light)"} @@ -533,6 +561,20 @@ static void cycle_color_correction_backwards(unsigned index) } } +static void decrease_color_temperature(unsigned index) +{ + if (configuration.color_temperature < 20) { + configuration.color_temperature++; + } +} + +static void increase_color_temperature(unsigned index) +{ + if (configuration.color_temperature > 0) { + configuration.color_temperature--; + } +} + static void cycle_palette(unsigned index) { if (configuration.dmg_palette == 3) { @@ -684,6 +726,7 @@ static const struct menu_item graphics_menu[] = { {"Default Window Scale:", cycle_default_scale, current_default_scale, cycle_default_scale_backwards}, {"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards}, {"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards}, + {"Ambient Light:", decrease_color_temperature, current_color_temperature, increase_color_temperature}, {"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}, diff --git a/SDL/gui.h b/SDL/gui.h index f55464d..84930e0 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -110,6 +110,10 @@ typedef struct { GB_rumble_mode_t rumble_mode; uint8_t default_scale; + + /* v0.14 */ + unsigned padding; + uint8_t color_temperature; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 45d016d..a20d644 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -120,6 +120,7 @@ static void open_menu(void) GB_audio_set_paused(false); } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); + GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0); GB_set_border_mode(&gb, configuration.border_mode); update_palette(); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); @@ -496,6 +497,7 @@ restart: 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); + GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0); update_palette(); if ((unsigned)configuration.border_mode <= GB_BORDER_ALWAYS) { GB_set_border_mode(&gb, configuration.border_mode); @@ -646,6 +648,7 @@ int main(int argc, char **argv) configuration.dmg_palette %= 3; configuration.border_mode %= GB_BORDER_ALWAYS + 1; configuration.rumble_mode %= GB_RUMBLE_ALL_GAMES + 1; + configuration.color_temperature %= 21; } if (configuration.model >= MODEL_MAX) { From 4bbd27735fb6d12ba95deb10437214b2172aeec3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 25 Dec 2020 20:40:39 +0200 Subject: [PATCH 80/98] Fix a regression in speed switch timing, reset DIV on speed switch, better odd-mode detection and avoidance --- Core/memory.c | 1 + Core/sm83_cpu.c | 16 ++++++++-------- Core/timing.c | 8 ++++++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Core/memory.c b/Core/memory.c index 40af0e5..cff31b6 100644 --- a/Core/memory.c +++ b/Core/memory.c @@ -895,6 +895,7 @@ static void write_high_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value) if ((value & 0x80) && !(gb->io_registers[GB_IO_LCDC] & 0x80)) { gb->display_cycles = 0; gb->display_state = 0; + gb->double_speed_alignment = 0; if (GB_is_sgb(gb)) { gb->frame_skip_state = GB_FRAMESKIP_SECOND_FRAME_RENDERED; } diff --git a/Core/sm83_cpu.c b/Core/sm83_cpu.c index e423fba..e56040b 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -332,6 +332,7 @@ static void nop(GB_gameboy_t *gb, uint8_t opcode) static void enter_stop_mode(GB_gameboy_t *gb) { + GB_write_memory(gb, 0xFF00 + GB_IO_DIV, 0); gb->stopped = true; gb->oam_ppu_blocked = !gb->oam_read_blocked; gb->vram_ppu_blocked = !gb->vram_read_blocked; @@ -340,30 +341,30 @@ static void enter_stop_mode(GB_gameboy_t *gb) 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; + /* The CPU takes more time to wake up then the other components */ + for (unsigned i = 0x2000; i--;) { + GB_advance_cycles(gb, 0x10); + } + GB_write_memory(gb, 0xFF00 + GB_IO_DIV, 0); } static void stop(GB_gameboy_t *gb, uint8_t opcode) { if (gb->io_registers[GB_IO_KEY1] & 0x1) { - if (gb->cgb_double_speed && gb->io_registers[GB_IO_LCDC] & 0x80) { - GB_log(gb, "Returning from double speed mode while the PPU is on may trigger odd-mode\n"); - } flush_pending_cycles(gb); bool needs_alignment = false; GB_advance_cycles(gb, 0x4); /* Make sure we keep the CPU ticks aligned correctly when returning from double speed mode */ + if (gb->double_speed_alignment & 7) { GB_advance_cycles(gb, 0x4); needs_alignment = true; + GB_log(gb, "ROM triggered PPU odd mode, which is currently not supported. Reverting to even-mode.\n"); } gb->cgb_double_speed ^= true; @@ -388,7 +389,6 @@ static void stop(GB_gameboy_t *gb, uint8_t opcode) enter_stop_mode(gb); } } - /* Todo: is PC being actually read? */ gb->pc++; } diff --git a/Core/timing.c b/Core/timing.c index 44ff8f7..7009d7b 100644 --- a/Core/timing.c +++ b/Core/timing.c @@ -157,7 +157,9 @@ 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; + if (GB_is_cgb(gb)) { + gb->apu.apu_cycles += 4 << !gb->cgb_double_speed; + } return; } @@ -248,7 +250,9 @@ void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) } // Not affected by speed boost - gb->double_speed_alignment += cycles; + if (gb->io_registers[GB_IO_LCDC] & 0x80) { + gb->double_speed_alignment += cycles; + } gb->hdma_cycles += cycles; gb->apu_output.sample_cycles += cycles; gb->cycles_since_last_sync += cycles; From 544d39f19d3707af33c889afb02c9a59dbabdf8c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 00:19:48 +0200 Subject: [PATCH 81/98] Further improvements to STOP timing --- 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 e56040b..cf73b31 100644 --- a/Core/sm83_cpu.c +++ b/Core/sm83_cpu.c @@ -346,9 +346,10 @@ static void leave_stop_mode(GB_gameboy_t *gb) gb->vram_ppu_blocked = false; gb->cgb_palettes_ppu_blocked = false; /* The CPU takes more time to wake up then the other components */ - for (unsigned i = 0x2000; i--;) { + for (unsigned i = 0x1FFF; i--;) { GB_advance_cycles(gb, 0x10); } + GB_advance_cycles(gb, gb->cgb_double_speed? 0x10 : 0xF); GB_write_memory(gb, 0xFF00 + GB_IO_DIV, 0); } From 6d5ce6c54d0e6311de3eb1ef5c961e0d80d79488 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 01:45:03 +0200 Subject: [PATCH 82/98] Better scrolling a spacing in the SDL UI --- SDL/gui.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 63e42d8..84c13e9 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -211,7 +211,7 @@ static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned heig continue; } - if (x > width - GLYPH_WIDTH || y == 0 || y - y_offset > 144 - GLYPH_HEIGHT) { + if (x > width - GLYPH_WIDTH || y == 0 || y - y_offset > 144 - GLYPH_HEIGHT || y <= y_offset) { break; } @@ -1384,9 +1384,17 @@ void run_gui(bool is_running) 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 == 0) { + if (y < scroll) { + scroll = (y - 4) / 12 * 12; + goto rerender; + } + } + else { + if (y < scroll + 24) { + scroll = (y - 24) / 12 * 12; + goto rerender; + } } } if (i == current_selection && i == 0 && scroll != 0) { @@ -1406,14 +1414,20 @@ void run_gui(bool is_running) i == current_selection && !item->value_getter ? DECORATION_SELECTION : DECORATION_NONE); y += 12; if (item->value_getter) { - draw_text_centered(pixels, width, height, y + y_offset, item->value_getter(i), gui_palette_native[3], gui_palette_native[0], + draw_text_centered(pixels, width, height, y + y_offset - 1, item->value_getter(i), gui_palette_native[3], gui_palette_native[0], i == current_selection ? DECORATION_ARROWS : DECORATION_NONE); y += 12; } } if (i == current_selection) { - if (y > scroll + 144) { - scroll = y - 144; + if (item[1].string) { + if (y > scroll + 120) { + scroll = (y - 120) / 12 * 12; + goto rerender; + } + } + else if (y > scroll + 144) { + scroll = (y - 144) / 12 * 12; goto rerender; } } From c471696fbb4efbb31caef6d599293ec5377349e6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 03:20:53 +0200 Subject: [PATCH 83/98] Scrollbar and mouse wheel support --- SDL/gui.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 84c13e9..900907c 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -197,7 +197,7 @@ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigne } } -static unsigned scroll = 0; +static signed 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; @@ -262,6 +262,10 @@ struct menu_item { }; static const struct menu_item *current_menu = NULL; static const struct menu_item *root_menu = NULL; +static unsigned menu_height; +static unsigned scrollbar_size; +static bool mouse_scroling = false; + static unsigned current_selection = 0; static enum { @@ -303,6 +307,23 @@ static void open_rom(unsigned index) } } +static void recalculate_menu_height(void) +{ + menu_height = 24; + scrollbar_size = 0; + if (gui_state == SHOWING_MENU) { + for (const struct menu_item *item = current_menu; item->string; item++) { + menu_height += 12; + if (item->backwards_handler) { + menu_height += 12; + } + } + } + if (menu_height > 144) { + scrollbar_size = 144 * 140 / menu_height; + } +} + static const struct menu_item paused_menu[] = { {"Resume", NULL}, {"Open ROM", open_rom}, @@ -323,6 +344,7 @@ static void return_to_root_menu(unsigned index) current_menu = root_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } static void cycle_model(unsigned index) @@ -434,6 +456,7 @@ static void enter_emulation_menu(unsigned index) current_menu = emulation_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } const char *current_scaling_mode(unsigned index) @@ -739,6 +762,7 @@ static void enter_graphics_menu(unsigned index) current_menu = graphics_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } const char *highpass_filter_string(unsigned index) @@ -800,6 +824,7 @@ static void enter_audio_menu(unsigned index) current_menu = audio_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } static void modify_key(unsigned index) @@ -841,6 +866,7 @@ static void enter_controls_menu(unsigned index) current_menu = controls_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } static unsigned joypad_index = 0; @@ -976,6 +1002,7 @@ static void enter_joypad_menu(unsigned index) current_menu = joypad_menu; current_selection = 0; scroll = 0; + recalculate_menu_height(); } joypad_button_t get_joypad_button(uint8_t physical_button) @@ -1060,6 +1087,7 @@ void run_gui(bool is_running) gui_state = is_running? SHOWING_MENU : SHOWING_DROP_MESSAGE; bool should_render = true; current_menu = root_menu = is_running? paused_menu : nonpaused_menu; + recalculate_menu_height(); current_selection = 0; scroll = 0; do { @@ -1247,6 +1275,23 @@ void run_gui(bool is_running) } break; } + + case SDL_MOUSEWHEEL: { + if (menu_height > 144) { + scroll -= event.wheel.y; + if (scroll < 0) { + scroll = 0; + } + if (scroll >= menu_height - 144) { + scroll = menu_height - 144; + } + + mouse_scroling = true; + should_render = true; + } + break; + } + case SDL_KEYDOWN: if (event_hotkey_code(&event) == SDL_SCANCODE_F && event.key.keysym.mod & MODIFIER) { @@ -1295,18 +1340,22 @@ void run_gui(bool is_running) gui_state = SHOWING_DROP_MESSAGE; } current_selection = 0; + mouse_scroling = false; scroll = 0; current_menu = root_menu; + recalculate_menu_height(); should_render = true; } } else if (gui_state == SHOWING_MENU) { if (event.key.keysym.scancode == SDL_SCANCODE_DOWN && current_menu[current_selection + 1].string) { current_selection++; + mouse_scroling = false; should_render = true; } else if (event.key.keysym.scancode == SDL_SCANCODE_UP && current_selection) { current_selection--; + mouse_scroling = false; should_render = true; } else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && !current_menu[current_selection].backwards_handler) { @@ -1383,7 +1432,7 @@ 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 (i == current_selection && !mouse_scroling) { if (i == 0) { if (y < scroll) { scroll = (y - 4) / 12 * 12; @@ -1397,7 +1446,7 @@ void run_gui(bool is_running) } } } - if (i == current_selection && i == 0 && scroll != 0) { + if (i == current_selection && i == 0 && scroll != 0 && !mouse_scroling) { scroll = 0; goto rerender; } @@ -1433,6 +1482,22 @@ void run_gui(bool is_running) } } + if (scrollbar_size) { + unsigned scrollbar_offset = (140 - scrollbar_size) * scroll / (menu_height - 144); + if (scrollbar_offset + scrollbar_size > 140) { + scrollbar_offset = 140 - scrollbar_size; + } + for (unsigned y = 0; y < 140; y++) { + uint32_t *pixel = pixels + x_offset + 156 + width * (y + y_offset + 2); + if (y >= scrollbar_offset && y < scrollbar_offset + scrollbar_size) { + pixel[0] = pixel[1]= gui_palette_native[2]; + } + else { + pixel[0] = pixel[1]= gui_palette_native[1]; + } + + } + } break; case SHOWING_HELP: draw_text(pixels, width, height, 2 + x_offset, 2 + y_offset, help[current_help_page], gui_palette_native[3], gui_palette_native[0]); From e1f797c21251c86bf66b1570047eb9a9926d5464 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 13:13:43 +0200 Subject: [PATCH 84/98] Improved scrolling --- SDL/gui.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 900907c..5701495 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -177,8 +177,7 @@ static void rescale_window(void) SDL_SetWindowSize(window, GB_get_screen_width(&gb) * configuration.default_scale, GB_get_screen_height(&gb) * configuration.default_scale); } -/* Does NOT check for bounds! */ -static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color) +static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color, uint32_t *mask_top, uint32_t *mask_bottom) { if (ch < ' ' || ch > font_max) { ch = '?'; @@ -188,7 +187,7 @@ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigne for (unsigned y = GLYPH_HEIGHT; y--;) { for (unsigned x = GLYPH_WIDTH; x--;) { - if (*(data++)) { + if (*(data++) && buffer >= mask_top && buffer < mask_bottom) { (*buffer) = color; } buffer++; @@ -198,7 +197,7 @@ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigne } static signed scroll = 0; -static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, unsigned y, const char *string, uint32_t color) +static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, signed y, const char *string, uint32_t color) { y -= scroll; unsigned orig_x = x; @@ -211,17 +210,17 @@ static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned heig continue; } - if (x > width - GLYPH_WIDTH || y == 0 || y - y_offset > 144 - GLYPH_HEIGHT || y <= y_offset) { + if (x > width - GLYPH_WIDTH) { break; } - draw_char(&buffer[x + width * y], width, height, *string, color); + draw_char(&buffer[(signed)(x + width * y)], width, height, *string, color, &buffer[width * y_offset], &buffer[width * (y_offset + 144)]); x += GLYPH_WIDTH; string++; } } -static void draw_text(uint32_t *buffer, unsigned width, unsigned height, 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, signed y, const char *string, uint32_t color, uint32_t border) { draw_unbordered_text(buffer, width, height, x - 1, y, string, border); draw_unbordered_text(buffer, width, height, x + 1, y, string, border); @@ -1468,15 +1467,12 @@ void run_gui(bool is_running) y += 12; } } - if (i == current_selection) { - if (item[1].string) { - if (y > scroll + 120) { - scroll = (y - 120) / 12 * 12; - goto rerender; - } - } - else if (y > scroll + 144) { + if (i == current_selection && !mouse_scroling) { + if (y > scroll + 144) { scroll = (y - 144) / 12 * 12; + if (scroll > menu_height - 144) { + scroll = menu_height - 144; + } goto rerender; } } From 7fc59b5cf4e27c05c239ce1d5c1ac083bf58e832 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 15:10:11 +0200 Subject: [PATCH 85/98] Let the SDL port choose a boot ROMs folder --- Makefile | 2 +- OpenDialog/cocoa.m | 18 +++++++++++ OpenDialog/gtk.c | 69 ++++++++++++++++++++++++++++++++++++++++ OpenDialog/open_dialog.h | 2 +- OpenDialog/windows.c | 32 ++++++++++++++++++- SDL/font.c | 20 ++++++++++++ SDL/font.h | 2 ++ SDL/gui.c | 43 ++++++++++++++++++++++++- SDL/gui.h | 1 + SDL/main.c | 16 +++++++--- 10 files changed, 197 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c3d030a..9d3ca7c 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,7 @@ GL_LDFLAGS := $(shell $(PKG_CONFIG) --libs gl || echo -lGL) endif ifeq ($(PLATFORM),windows32) CFLAGS += -IWindows -Drandom=rand --target=i386-pc-windows -LDFLAGS += -lmsvcrt -lcomdlg32 -luser32 -lshell32 -lSDL2main -Wl,/MANIFESTFILE:NUL --target=i386-pc-windows +LDFLAGS += -lmsvcrt -lcomdlg32 -luser32 -lshell32 -lole32 -lSDL2main -Wl,/MANIFESTFILE:NUL --target=i386-pc-windows SDL_LDFLAGS := -lSDL2 GL_LDFLAGS := -lopengl32 else diff --git a/OpenDialog/cocoa.m b/OpenDialog/cocoa.m index 76b9606..aeeb98a 100644 --- a/OpenDialog/cocoa.m +++ b/OpenDialog/cocoa.m @@ -18,3 +18,21 @@ char *do_open_rom_dialog(void) return NULL; } } + +char *do_open_folder_dialog(void) +{ + @autoreleasepool { + NSWindow *key = [NSApp keyWindow]; + NSOpenPanel *dialog = [NSOpenPanel openPanel]; + dialog.title = @"Select Boot ROMs Folder"; + dialog.canChooseDirectories = true; + dialog.canChooseFiles = false; + [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 index 5b1caa3..378dcb4 100644 --- a/OpenDialog/gtk.c +++ b/OpenDialog/gtk.c @@ -6,6 +6,7 @@ #include #define GTK_FILE_CHOOSER_ACTION_OPEN 0 +#define GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER 2 #define GTK_RESPONSE_ACCEPT -3 #define GTK_RESPONSE_CANCEL -6 @@ -111,3 +112,71 @@ lazy_error: fprintf(stderr, "Failed to display GTK dialog\n"); return NULL; } + +char *do_open_folder_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(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); + + gtk_init_check(0, 0); + + + void *dialog = gtk_file_chooser_dialog_new("Select Boot ROMs Folder", + 0, + GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, + "_Cancel", GTK_RESPONSE_CANCEL, + "_Open", GTK_RESPONSE_ACCEPT, + NULL ); + + + int res = gtk_dialog_run (dialog); + char *ret = NULL; + + if (res == GTK_RESPONSE_ACCEPT) { + char *filename; + filename = gtk_file_chooser_get_filename(dialog); + ret = strdup(filename); + g_free(filename); + } + + while (gtk_events_pending()) { + gtk_main_iteration(); + } + + gtk_widget_destroy(dialog); + + while (gtk_events_pending()) { + gtk_main_iteration(); + } + 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 index 85e5721..6d7fb5b 100644 --- a/OpenDialog/open_dialog.h +++ b/OpenDialog/open_dialog.h @@ -2,5 +2,5 @@ #define open_rom_h char *do_open_rom_dialog(void); - +char *do_open_folder_dialog(void); #endif /* open_rom_h */ diff --git a/OpenDialog/windows.c b/OpenDialog/windows.c index 52e281d..e711032 100644 --- a/OpenDialog/windows.c +++ b/OpenDialog/windows.c @@ -1,10 +1,11 @@ #include +#include #include "open_dialog.h" char *do_open_rom_dialog(void) { OPENFILENAMEW dialog; - wchar_t filename[MAX_PATH] = {0}; + static wchar_t filename[MAX_PATH] = {0}; memset(&dialog, 0, sizeof(dialog)); dialog.lStructSize = sizeof(dialog); @@ -25,3 +26,32 @@ char *do_open_rom_dialog(void) return NULL; } + +char *do_open_folder_dialog(void) +{ + + BROWSEINFOW dialog; + memset(&dialog, 0, sizeof(dialog)); + + dialog.ulFlags = BIF_USENEWUI; + dialog.lpszTitle = L"Select Boot ROMs Folder"; + + OleInitialize(NULL); + + LPITEMIDLIST list = SHBrowseForFolderW(&dialog); + static wchar_t filename[MAX_PATH] = {0}; + + if (list) { + if (!SHGetPathFromIDListW(list, filename)) { + OleUninitialize(); + return NULL; + } + char *ret = malloc(MAX_PATH * 4); + WideCharToMultiByte(CP_UTF8, 0, filename, sizeof(filename), ret, MAX_PATH * 4, NULL, NULL); + CoTaskMemFree(list); + OleUninitialize(); + return ret; + } + OleUninitialize(); + return NULL; +} diff --git a/SDL/font.c b/SDL/font.c index 93f3fa9..eb6243e 100644 --- a/SDL/font.c +++ b/SDL/font.c @@ -1033,6 +1033,26 @@ uint8_t font[] = { _, _, _, X, X, _, _, _, _, _, X, _, _, _, _, _, _, _, + + /* Elipsis */ + _, _, _, _, _, _, + _, _, _, _, _, _, + _, _, _, _, _, _, + _, _, _, _, _, _, + _, _, _, _, _, _, + _, _, _, _, _, _, + X, _, X, _, X, _, + _, _, _, _, _, _, + + /* Mojibake */ + X, X, X, X, X, _, + X, _, _, _, X, _, + X, _, _, _, X, _, + X, _, _, _, X, _, + X, _, _, _, X, _, + X, _, _, _, X, _, + X, _, _, _, X, _, + X, X, X, X, X, _, }; const uint8_t font_max = sizeof(font) / GLYPH_HEIGHT / GLYPH_WIDTH + ' '; diff --git a/SDL/font.h b/SDL/font.h index 21753a8..06d7adf 100644 --- a/SDL/font.h +++ b/SDL/font.h @@ -12,5 +12,7 @@ extern const uint8_t font_max; #define CTRL_STRING "\x80\x81\x82" #define SHIFT_STRING "\x83" #define CMD_STRING "\x84\x85" +#define ELLIPSIS_STRING "\x87" +#define MOJIBAKE_STRING "\x88" #endif /* font_h */ diff --git a/SDL/gui.c b/SDL/gui.c index 5701495..c36dff4 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -442,9 +442,50 @@ const char *current_rewind_string(unsigned index) return "Custom"; } +const char *current_bootrom_string(unsigned index) +{ + if (!configuration.bootrom_path[0]) { + return "Built-in Boot ROMs"; + } + size_t length = strlen(configuration.bootrom_path); + static char ret[24] = {0,}; + if (length <= 23) { + strcpy(ret, configuration.bootrom_path); + } + else { + memcpy(ret, configuration.bootrom_path, 11); + memcpy(ret + 12, configuration.bootrom_path + length - 11, 11); + } + for (unsigned i = 0; i < 24; i++) { + if (ret[i] < 0) { + ret[i] = MOJIBAKE_STRING[0]; + } + } + if (length > 23) { + ret[11] = ELLIPSIS_STRING[0]; + } + return ret; +} + +static void toggle_bootrom(unsigned index) +{ + if (configuration.bootrom_path[0]) { + configuration.bootrom_path[0] = 0; + } + else { + char *folder = do_open_folder_dialog(); + if (!folder) return; + if (strlen(folder) < sizeof(configuration.bootrom_path) - 1) { + strcpy(configuration.bootrom_path, folder); + } + free(folder); + } +} + 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}, + {"Boot ROMs Folder:", toggle_bootrom, current_bootrom_string, toggle_bootrom}, {"Rewind Length:", cycle_rewind, current_rewind_string, cycle_rewind_backwards}, {"Back", return_to_root_menu}, {NULL,} @@ -883,7 +924,7 @@ const char *current_joypad_name(unsigned index) // SDL returns a name with repeated and trailing spaces while (*orig_name && i < sizeof(name) - 2) { if (orig_name[0] != ' ' || orig_name[1] != ' ') { - name[i++] = *orig_name; + name[i++] = *orig_name > 0? *orig_name : MOJIBAKE_STRING[0]; } orig_name++; } diff --git a/SDL/gui.h b/SDL/gui.h index 84930e0..b507237 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -114,6 +114,7 @@ typedef struct { /* v0.14 */ unsigned padding; uint8_t color_temperature; + char bootrom_path[4096]; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index a20d644..06cde73 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -448,8 +448,6 @@ static bool handle_pending_command(void) static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type) { - bool error = false; - start_capturing_logs(); static const char *const names[] = { [GB_BOOT_ROM_DMG0] = "dmg0_boot.bin", [GB_BOOT_ROM_DMG] = "dmg_boot.bin", @@ -460,8 +458,17 @@ static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type) [GB_BOOT_ROM_CGB] = "cgb_boot.bin", [GB_BOOT_ROM_AGB] = "agb_boot.bin", }; - GB_load_boot_rom(gb, resource_path(names[type])); - end_capturing_logs(true, error); + bool use_built_in = true; + if (configuration.bootrom_path[0]) { + static char path[4096]; + snprintf(path, sizeof(path), "%s/%s", configuration.bootrom_path, names[type]); + use_built_in = GB_load_boot_rom(gb, path); + } + if (use_built_in) { + start_capturing_logs(); + GB_load_boot_rom(gb, resource_path(names[type])); + end_capturing_logs(true, false); + } } static void run(void) @@ -649,6 +656,7 @@ int main(int argc, char **argv) configuration.border_mode %= GB_BORDER_ALWAYS + 1; configuration.rumble_mode %= GB_RUMBLE_ALL_GAMES + 1; configuration.color_temperature %= 21; + configuration.bootrom_path[sizeof(configuration.bootrom_path) - 1] = 0; } if (configuration.model >= MODEL_MAX) { From 3dbd2eac91daed6a89f9a9b54c023c161eb594eb Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 23:33:01 +0200 Subject: [PATCH 86/98] Something went wrong with the color temperature commit somehow --- Cocoa/Document.m | 1 + Cocoa/GBPreferencesWindow.m | 1 - Cocoa/Preferences.xib | 29 +++++++++++++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index a354e03..ad443b6 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -286,6 +286,7 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on) 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"]); + GB_set_light_temperature(&gb, [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBLightTemperature"]); GB_set_border_mode(&gb, (GB_border_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBBorderMode"]); [self updatePalette]; GB_set_rgb_encode_callback(&gb, rgbEncode); diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index dd13ca1..6edc54e 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -102,7 +102,6 @@ { _temperatureSlider = temperatureSlider; [temperatureSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBLightTemperature"] * 256]; - temperatureSlider.continuous = YES; } - (NSSlider *)temperatureSlider diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 73eb0ab..e5494fb 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -79,7 +79,7 @@ - + @@ -97,7 +97,7 @@ - + @@ -134,7 +134,7 @@ - + @@ -143,7 +143,7 @@ - + @@ -263,8 +263,25 @@ + + + + + + + + + + + + + + + + + - + @@ -491,7 +508,7 @@ - + From 47ebc317331037fcf9b98ee10d66fa17cb953e66 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 23:52:41 +0200 Subject: [PATCH 87/98] Fixed a bug where the SDL and libretro frontend would not update the border when loading a new ROM --- Core/gb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/gb.c b/Core/gb.c index 4788ad9..83cf23b 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -305,6 +305,7 @@ 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); + gb->tried_loading_sgb_border = false; return 0; } @@ -537,6 +538,7 @@ error: gb->rom_size = old_size; } fclose(f); + gb->tried_loading_sgb_border = false; return -1; } @@ -557,6 +559,7 @@ void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t siz memset(gb->rom, 0xff, gb->rom_size); memcpy(gb->rom, buffer, size); GB_configure_cart(gb); + gb->tried_loading_sgb_border = false; } typedef struct { From 1d34637bdaf7848aa5f99ea2c89087f45420973a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 26 Dec 2020 23:56:26 +0200 Subject: [PATCH 88/98] Fix it harder --- Core/gb.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Core/gb.c b/Core/gb.c index 83cf23b..77ea144 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -306,6 +306,8 @@ int GB_load_rom(GB_gameboy_t *gb, const char *path) fclose(f); GB_configure_cart(gb); gb->tried_loading_sgb_border = false; + gb->has_sgb_border = false; + load_default_border(gb); return 0; } @@ -539,6 +541,8 @@ error: } fclose(f); gb->tried_loading_sgb_border = false; + gb->has_sgb_border = false; + load_default_border(gb); return -1; } @@ -560,6 +564,8 @@ void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t siz memcpy(gb->rom, buffer, size); GB_configure_cart(gb); gb->tried_loading_sgb_border = false; + gb->has_sgb_border = false; + load_default_border(gb); } typedef struct { From 9e808b255cbc07aea196067ab41c28f0d0854959 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 27 Dec 2020 00:03:40 +0200 Subject: [PATCH 89/98] Escape now returns to the previous menu if used from a submenu in the SDL port --- SDL/gui.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SDL/gui.c b/SDL/gui.c index c36dff4..a21526d 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1369,7 +1369,11 @@ void run_gui(bool is_running) } } else if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { - if (is_running) { + if (gui_state == SHOWING_MENU && current_menu != root_menu) { + return_to_root_menu(0); + should_render = true; + } + else if (is_running) { return; } else { From e535d97e8430ab353b3b6de87d3f67afd9adad5e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 27 Dec 2020 00:23:16 +0200 Subject: [PATCH 90/98] Fix GCC9 build break --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9d3ca7c..a03f417 100644 --- a/Makefile +++ b/Makefile @@ -99,7 +99,7 @@ endif # These must come before the -Wno- flags 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 +WARNINGS += -Wno-nonnull -Wno-unused-result -Wno-strict-aliasing -Wno-multichar -Wno-int-in-bool-context -Wno-format-truncation # 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) From 8e858c1bf146f7f62f7048f7089b9572bc3b2b30 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 27 Dec 2020 01:02:50 +0200 Subject: [PATCH 91/98] Capitalization --- Cocoa/Preferences.xib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index e5494fb..7ca5f28 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -274,7 +274,7 @@ - + From 5c854dbdca808c0258cb34a3cb11b1f9d2dd67ca Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 31 Dec 2020 00:06:36 +0200 Subject: [PATCH 92/98] Interference emulation --- Cocoa/Document.m | 12 ++++++++ Cocoa/GBPreferencesWindow.h | 1 + Cocoa/GBPreferencesWindow.m | 21 ++++++++++++++ Cocoa/Preferences.xib | 30 ++++++++++++++++---- Core/apu.c | 55 +++++++++++++++++++++++++++++++++++++ Core/apu.h | 4 +++ SDL/gui.c | 26 +++++++++++++++++- SDL/gui.h | 1 + SDL/main.c | 2 ++ 9 files changed, 145 insertions(+), 7 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index ad443b6..0beceab 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -287,6 +287,7 @@ static void infraredStateChanged(GB_gameboy_t *gb, bool on) 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"]); GB_set_light_temperature(&gb, [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBLightTemperature"]); + GB_set_interference_volume(&gb, [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBInterferenceVolume"]); GB_set_border_mode(&gb, (GB_border_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBBorderMode"]); [self updatePalette]; GB_set_rgb_encode_callback(&gb, rgbEncode); @@ -695,6 +696,11 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) name:@"GBLightTemperatureChanged" object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateInterferenceVolume) + name:@"GBInterferenceVolumeChanged" + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFrameBlendingMode) name:@"GBFrameBlendingModeChanged" @@ -1848,6 +1854,12 @@ static unsigned *multiplication_table_for_frequency(unsigned frequency) } } +- (void) updateInterferenceVolume +{ + if (GB_is_inited(&gb)) { + GB_set_interference_volume(&gb, [[NSUserDefaults standardUserDefaults] doubleForKey:@"GBInterferenceVolume"]); + } +} - (void) updateFrameBlendingMode { diff --git a/Cocoa/GBPreferencesWindow.h b/Cocoa/GBPreferencesWindow.h index f0b7506..43a8f1d 100644 --- a/Cocoa/GBPreferencesWindow.h +++ b/Cocoa/GBPreferencesWindow.h @@ -18,6 +18,7 @@ @property (strong) IBOutlet NSPopUpButtonCell *bootROMsButton; @property (strong) IBOutlet NSPopUpButton *rumbleModePopupButton; @property (weak) IBOutlet NSSlider *temperatureSlider; +@property (weak) IBOutlet NSSlider *interferenceSlider; @property (weak) IBOutlet NSPopUpButton *dmgPopupButton; @property (weak) IBOutlet NSPopUpButton *sgbPopupButton; @property (weak) IBOutlet NSPopUpButton *cgbPopupButton; diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index 6edc54e..bd3a4a8 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -27,6 +27,7 @@ NSPopUpButton *_preferredJoypadButton; NSPopUpButton *_rumbleModePopupButton; NSSlider *_temperatureSlider; + NSSlider *_interferenceSlider; } + (NSArray *)filterList @@ -108,6 +109,18 @@ { return _temperatureSlider; } + +- (void)setInterferenceSlider:(NSSlider *)interferenceSlider +{ + _interferenceSlider = interferenceSlider; + [interferenceSlider setDoubleValue:[[NSUserDefaults standardUserDefaults] doubleForKey:@"GBInterferenceVolume"] * 256]; +} + +- (NSSlider *)interferenceSlider +{ + return _interferenceSlider; +} + - (void)setFrameBlendingModePopupButton:(NSPopUpButton *)frameBlendingModePopupButton { _frameBlendingModePopupButton = frameBlendingModePopupButton; @@ -303,6 +316,14 @@ [[NSNotificationCenter defaultCenter] postNotificationName:@"GBLightTemperatureChanged" object:nil]; } +- (IBAction)volumeTemperatureChanged:(id)sender +{ + [[NSUserDefaults standardUserDefaults] setObject:@([sender doubleValue] / 256.0) + forKey:@"GBInterferenceVolume"]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBInterferenceVolumeChanged" object:nil]; + +} + - (IBAction)franeBlendingModeChanged:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@([sender indexOfSelectedItem]) diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 7ca5f28..248cfce 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -73,6 +73,7 @@ + @@ -174,7 +175,7 @@ - + @@ -264,7 +265,7 @@ - + @@ -446,11 +447,11 @@ - + - + @@ -470,7 +471,7 @@ - + @@ -478,8 +479,25 @@ + + + + + + + + + + + + + + + + + - + diff --git a/Core/apu.c b/Core/apu.c index e37b265..3abca7d 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -137,6 +137,45 @@ static double smooth(double x) return 3*x*x - 2*x*x*x; } +static signed interference(GB_gameboy_t *gb) +{ + /* These aren't scientifically measured, but based on ear based on several recordings */ + signed ret = 0; + if (gb->halted) { + if (gb->model != GB_MODEL_AGB) { + ret -= MAX_CH_AMP / 5; + } + else { + ret -= MAX_CH_AMP / 12; + } + } + if (gb->io_registers[GB_IO_LCDC] & 0x80) { + ret += MAX_CH_AMP / 7; + if ((gb->io_registers[GB_IO_STAT] & 3) == 3 && gb->model != GB_MODEL_AGB) { + ret += MAX_CH_AMP / 14; + } + else if ((gb->io_registers[GB_IO_STAT] & 3) == 1) { + ret -= MAX_CH_AMP / 7; + } + } + + if (gb->apu.global_enable) { + ret += MAX_CH_AMP / 10; + } + + if (GB_is_cgb(gb) && gb->model < GB_MODEL_AGB && (gb->io_registers[GB_IO_RP] & 1)) { + ret += MAX_CH_AMP / 10; + } + + if (!GB_is_cgb(gb)) { + ret /= 4; + } + + ret += rand() % (MAX_CH_AMP / 12); + + return ret; +} + static void render(GB_gameboy_t *gb) { GB_sample_t output = {0, 0}; @@ -226,6 +265,17 @@ static void render(GB_gameboy_t *gb) } + + if (gb->apu_output.interference_volume) { + signed interference_bias = interference(gb); + int16_t interference_sample = (interference_bias - gb->apu_output.interference_highpass); + gb->apu_output.interference_highpass = gb->apu_output.interference_highpass * gb->apu_output.highpass_rate + + (1 - gb->apu_output.highpass_rate) * interference_sample; + interference_bias *= gb->apu_output.interference_volume; + + filtered_output.left = MAX(MIN(filtered_output.left + interference_bias, 0x7FFF), -0x8000); + filtered_output.right = MAX(MIN(filtered_output.right + interference_bias, 0x7FFF), -0x8000); + } assert(gb->apu_output.sample_callback); gb->apu_output.sample_callback(gb, &filtered_output); } @@ -1122,3 +1172,8 @@ void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb) gb->apu_output.cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */ } } + +void GB_set_interference_volume(GB_gameboy_t *gb, double volume) +{ + gb->apu_output.interference_volume = volume; +} diff --git a/Core/apu.h b/Core/apu.h index 9d5fc80..69cea16 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -154,12 +154,16 @@ typedef struct { GB_sample_callback_t sample_callback; bool rate_set_in_clocks; + double interference_volume; + double interference_highpass; } 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, 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_set_interference_volume(GB_gameboy_t *gb, double volume); 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/SDL/gui.c b/SDL/gui.c index a21526d..0846664 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -789,7 +789,7 @@ static const struct menu_item graphics_menu[] = { {"Default Window Scale:", cycle_default_scale, current_default_scale, cycle_default_scale_backwards}, {"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards}, {"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards}, - {"Ambient Light:", decrease_color_temperature, current_color_temperature, increase_color_temperature}, + {"Ambient Light Temp.:", decrease_color_temperature, current_color_temperature, increase_color_temperature}, {"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}, @@ -852,9 +852,33 @@ void decrease_volume(unsigned index) } } +const char *interference_volume_string(unsigned index) +{ + static char ret[5]; + sprintf(ret, "%d%%", configuration.interference_volume); + return ret; +} + +void increase_interference_volume(unsigned index) +{ + configuration.interference_volume += 5; + if (configuration.interference_volume > 100) { + configuration.interference_volume = 100; + } +} + +void decrease_interference_volume(unsigned index) +{ + configuration.interference_volume -= 5; + if (configuration.interference_volume > 100) { + configuration.interference_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}, + {"Interference Volume:", increase_interference_volume, interference_volume_string, decrease_interference_volume}, {"Back", return_to_root_menu}, {NULL,} }; diff --git a/SDL/gui.h b/SDL/gui.h index b507237..8d69ec3 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -115,6 +115,7 @@ typedef struct { unsigned padding; uint8_t color_temperature; char bootrom_path[4096]; + uint8_t interference_volume; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 06cde73..63a61c9 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -121,6 +121,7 @@ static void open_menu(void) } GB_set_color_correction_mode(&gb, configuration.color_correction_mode); GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0); + GB_set_interference_volume(&gb, configuration.interference_volume / 100.0); GB_set_border_mode(&gb, configuration.border_mode); update_palette(); GB_set_highpass_filter_mode(&gb, configuration.highpass_mode); @@ -505,6 +506,7 @@ restart: GB_set_sample_rate(&gb, GB_audio_get_frequency()); GB_set_color_correction_mode(&gb, configuration.color_correction_mode); GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0); + GB_set_interference_volume(&gb, configuration.interference_volume / 100.0); update_palette(); if ((unsigned)configuration.border_mode <= GB_BORDER_ALWAYS) { GB_set_border_mode(&gb, configuration.border_mode); From b54a72d9b9a72dc243cd37216b3adb4c99511a44 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 Jan 2021 14:56:45 +0200 Subject: [PATCH 93/98] Fixing a bug where where zero-shift sweep wouldn't tick --- Core/apu.c | 7 ++++--- Core/apu.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 3abca7d..4d66e24 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -394,7 +394,7 @@ static void trigger_sweep_calculation(GB_gameboy_t *gb) /* Recalculation and overflow check only occurs after a delay */ gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; - + gb->apu.unshifted_sweep = !(gb->io_registers[GB_IO_NR10] & 0x7); gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } } @@ -524,7 +524,7 @@ void GB_apu_run(GB_gameboy_t *gb) gb->apu.noise_channel.alignment += cycles; if (gb->apu.square_sweep_calculate_countdown && - ((gb->io_registers[GB_IO_NR10] & 7) || + (((gb->io_registers[GB_IO_NR10] & 7) || gb->apu.unshifted_sweep) || gb->apu.square_sweep_calculate_countdown <= (gb->model > GB_MODEL_CGB_C? 3 : 1))) { // Calculation is paused if the lower bits if (gb->apu.square_sweep_calculate_countdown > cycles) { gb->apu.square_sweep_calculate_countdown -= cycles; @@ -875,6 +875,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) if (gb->io_registers[GB_IO_NR10] & 7) { /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; + gb->apu.unshifted_sweep = false; if (gb->model > GB_MODEL_CGB_C) { gb->apu.square_sweep_calculate_countdown += 2; } @@ -884,7 +885,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) else { gb->apu.sweep_length_addend = 0; } - gb->apu.channel_1_restart_hold = 4 - gb->apu.lf_div; + gb->apu.channel_1_restart_hold = 2 - gb->apu.lf_div + GB_is_cgb(gb) * 2; gb->apu.square_sweep_countdown = ((gb->io_registers[GB_IO_NR10] >> 4) & 7) ^ 7; } } diff --git a/Core/apu.h b/Core/apu.h index 69cea16..eadae1b 100644 --- a/Core/apu.h +++ b/Core/apu.h @@ -66,7 +66,7 @@ typedef struct uint8_t square_sweep_calculate_countdown; // In 2 MHz uint16_t sweep_length_addend; uint16_t shadow_sweep_sample_length; - GB_PADDING(bool, sweep_enabled); + bool unshifted_sweep; GB_PADDING(bool, sweep_decreasing); struct { From a9c337264e783f234252929da3aeae089a1df8e2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 Jan 2021 16:23:34 +0200 Subject: [PATCH 94/98] Fix the last remaining APU test --- Core/apu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 4d66e24..890ffe3 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -821,7 +821,7 @@ 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; - + bool was_active = gb->apu.is_active[index]; /* 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. */ @@ -876,7 +876,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) /* APU bug: if shift is nonzero, overflow check also occurs on trigger */ gb->apu.square_sweep_calculate_countdown = (gb->io_registers[GB_IO_NR10] & 0x7) * 2 + 5 - gb->apu.lf_div; gb->apu.unshifted_sweep = false; - if (gb->model > GB_MODEL_CGB_C) { + if (gb->model > GB_MODEL_CGB_C && !was_active) { gb->apu.square_sweep_calculate_countdown += 2; } gb->apu.sweep_length_addend = gb->apu.square_channels[GB_SQUARE_1].sample_length; From ecace40fb06fa929cd27a22f5853fc2775e18f93 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 Jan 2021 18:27:21 +0200 Subject: [PATCH 95/98] Minor APU bug fix --- Core/apu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Core/apu.c b/Core/apu.c index 890ffe3..8ddb52b 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -846,6 +846,10 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } else { /* Timing quirk: if already active, sound starts 2 (2MHz) ticks earlier.*/ + if (gb->apu.square_channels[index].sample_countdown <= 1) { + gb->apu.square_channels[index].current_sample_index++; + gb->apu.square_channels[index].current_sample_index &= 0x7; + } 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; From f9b13c66b162766216d1c9b19f99ea38fb5625f3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 3 Jan 2021 13:49:36 +0200 Subject: [PATCH 96/98] Emulation of a newly discovered revision specific APU quirk --- Core/apu.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 8ddb52b..6c397a6 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -836,6 +836,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } } + uint16_t old_sample_length = gb->apu.square_channels[index].sample_length; gb->apu.square_channels[index].sample_length &= 0xFF; gb->apu.square_channels[index].sample_length |= (value & 7) << 8; if (value & 0x80) { @@ -845,12 +846,21 @@ 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 + 6 - gb->apu.lf_div; } else { - /* Timing quirk: if already active, sound starts 2 (2MHz) ticks earlier.*/ - if (gb->apu.square_channels[index].sample_countdown <= 1) { - gb->apu.square_channels[index].current_sample_index++; - gb->apu.square_channels[index].current_sample_index &= 0x7; + unsigned extra_delay = 0; + if (gb->model == GB_MODEL_CGB_E /* || gb->model == GB_MODEL_CGB_D */) { + if ((!(value & 4) && ((gb->io_registers[reg] & 4) || old_sample_length == 0x3FF)) || + (old_sample_length == 0x7FF && gb->apu.square_channels[index].sample_length != 0x7FF)) { + gb->apu.square_channels[index].current_sample_index++; + gb->apu.square_channels[index].current_sample_index &= 0x7; + } + else if (gb->apu.square_channels[index].sample_length == 0x7FF && + old_sample_length != 0x7FF && + (gb->apu.square_channels[index].current_sample_index & 0x80)) { + extra_delay += 2; + } } - gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 4 - gb->apu.lf_div; + /* Timing quirk: if already active, sound starts 2 (2MHz) ticks earlier.*/ + gb->apu.square_channels[index].sample_countdown = (gb->apu.square_channels[index].sample_length ^ 0x7FF) * 2 + 4 - gb->apu.lf_div + extra_delay; } gb->apu.square_channels[index].current_volume = gb->io_registers[index == GB_SQUARE_1 ? GB_IO_NR12 : GB_IO_NR22] >> 4; From 29a3b18186c181399f4b99b9111ca9d8b5726886 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 3 Jan 2021 16:52:18 +0200 Subject: [PATCH 97/98] Better camera noise on frontends without camera support --- Core/camera.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Core/camera.c b/Core/camera.c index bef8489..7751f18 100644 --- a/Core/camera.c +++ b/Core/camera.c @@ -1,26 +1,26 @@ #include "gb.h" -static signed noise_seed = 0; +static uint32_t noise_seed = 0; -/* This is not a complete emulation of the camera chip. Only the features used by the GameBoy Camera ROMs are supported. +/* This is not a complete emulation of the camera chip. Only the features used by the Game Boy 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) { - signed value = (x + y * 128 + noise_seed); - uint8_t *data = (uint8_t *) &value; - unsigned hash = 0; + uint32_t value = (x * 151 + y * 149) ^ noise_seed; + uint32_t hash = 0; - while ((signed *) data != &value + 1) { - hash ^= (*data << 8); - if (hash & 0x8000) { - hash ^= 0x8a00; - hash ^= *data; - } - data++; + while (value) { hash <<= 1; + if (hash & 0x100) { + hash ^= 0x101; + } + if (value & 0x80000000) { + hash ^= 0xA1; + } + value <<= 1; } - return (hash >> 8); + return hash; } static long get_processed_color(GB_gameboy_t *gb, uint8_t x, uint8_t y) From e4c7333a1a0816a682acc7c3ae1898c87c138768 Mon Sep 17 00:00:00 2001 From: "C.W. Betts" Date: Mon, 4 Jan 2021 01:08:31 -0700 Subject: [PATCH 98/98] Fix visibility of a few functions in the QuickLook plug-in. --- QuickLook/exports.sym | 2 -- QuickLook/main.c | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/QuickLook/exports.sym b/QuickLook/exports.sym index f979687..2e7fdde 100644 --- a/QuickLook/exports.sym +++ b/QuickLook/exports.sym @@ -1,3 +1 @@ -_DeallocQuickLookGeneratorPluginType -_QuickLookGeneratorQueryInterface _QuickLookGeneratorPluginFactory diff --git a/QuickLook/main.c b/QuickLook/main.c index 1d1676a..4e45313 100644 --- a/QuickLook/main.c +++ b/QuickLook/main.c @@ -41,12 +41,12 @@ typedef struct __QuickLookGeneratorPluginType // Forward declaration for the IUnknown implementation. // -QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID); -void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance); -HRESULT QuickLookGeneratorQueryInterface(void *thisInstance, REFIID iid, LPVOID *ppv); -void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID); -ULONG QuickLookGeneratorPluginAddRef(void *thisInstance); -ULONG QuickLookGeneratorPluginRelease(void *thisInstance); +static QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID); +static void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance); +static HRESULT QuickLookGeneratorQueryInterface(void *thisInstance, REFIID iid, LPVOID *ppv); +extern void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID); +static ULONG QuickLookGeneratorPluginAddRef(void *thisInstance); +static ULONG QuickLookGeneratorPluginRelease(void *thisInstance); // ----------------------------------------------------------------------------- // myInterfaceFtbl definition