diff --git a/src/main.c b/src/main.c index cd762dc..ec09d5e 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,10 @@ #include "util.h" #include "spc_player.h" +#ifdef __SWITCH__ +#include "switch_impl.h" +#endif + static void playAudio(Snes *snes, SDL_AudioDeviceID device, int16_t *audioBuffer); static void renderScreen(Snes *snes, SDL_Renderer *renderer, SDL_Texture *texture); static void SDLCALL AudioCallback(void *userdata, Uint8 *stream, int len); @@ -309,6 +313,9 @@ static const struct RendererFuncs kSdlRendererFuncs = { #undef main int main(int argc, char** argv) { +#ifdef __SWITCH__ + SwitchImpl_Init(); +#endif argc--, argv++; const char *config_file = NULL; if (argc >= 2 && strcmp(argv[0], "--config") == 0) { @@ -367,6 +374,20 @@ int main(int argc, char** argv) { g_renderer_funcs = kSdlRendererFuncs; } + // init snes, load rom + const char* filename = argv[0] ? argv[0] : "sm.smc"; + Snes *snes = SnesInit(filename); + + if(snes == NULL) { + #ifdef __SWITCH__ + ThrowMissingROM(); + #else + char buf[256]; + snprintf(buf, sizeof(buf), "unable to load rom: %s", filename); + Die(buf); + #endif + return 1; + } SDL_Window *window = SDL_CreateWindow(kWindowTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, g_win_flags); if(window == NULL) { @@ -403,9 +424,6 @@ int main(int argc, char** argv) { g_audiobuffer = (uint8 *)malloc(g_frames_per_block * have.channels * sizeof(int16)); } - // init snes, load rom - Snes *snes = SnesInit(argv[0] ? argv[0] : "sm.smc"); - PpuBeginDrawing(snes->snes_ppu, g_pixels, 256 * 4, 0); PpuBeginDrawing(snes->my_ppu, g_my_pixels, 256 * 4, 0); @@ -420,7 +438,7 @@ int main(int argc, char** argv) { for (int i = 0; i < SDL_NumJoysticks(); i++) OpenOneGamepad(i); - + bool running = true; uint32 lastTick = SDL_GetTicks(); uint32 curTick = 0; @@ -539,6 +557,10 @@ int main(int argc, char** argv) { g_renderer_funcs.Destroy(); +#ifdef __SWITCH__ + SwitchImpl_Exit(); +#endif + SDL_DestroyWindow(window); SDL_Quit(); return 0; @@ -653,7 +675,7 @@ static void HandleCommand(uint32 j, bool pressed) { case kKeys_WindowBigger: ChangeWindowScale(1); break; case kKeys_WindowSmaller: ChangeWindowScale(-1); break; case kKeys_DisplayPerf: g_display_perf ^= 1; break; - case kKeys_ToggleRenderer: + case kKeys_ToggleRenderer: g_ppu_render_flags ^= kPpuRenderFlags_NewRenderer; g_new_ppu = (g_ppu_render_flags & kPpuRenderFlags_NewRenderer) != 0; break; diff --git a/src/platform/switch/.gitignore b/src/platform/switch/.gitignore new file mode 100644 index 0000000..24c39e2 --- /dev/null +++ b/src/platform/switch/.gitignore @@ -0,0 +1,5 @@ +/bin +/*.nacp +/*.nro +/*.elf +/*.smc \ No newline at end of file diff --git a/src/platform/switch/Makefile b/src/platform/switch/Makefile new file mode 100644 index 0000000..d624f72 --- /dev/null +++ b/src/platform/switch/Makefile @@ -0,0 +1,224 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITPRO)/libnx/switch_rules + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# DATA is a list of directories containing data files +# INCLUDES is a list of directories containing header files +# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) +# +# NO_ICON: if set to anything, do not use icon. +# NO_NACP: if set to anything, no .nacp file is generated. +# APP_TITLE is the name of the app stored in the .nacp file (Optional) +# APP_AUTHOR is the author of the app stored in the .nacp file (Optional) +# APP_VERSION is the version of the app stored in the .nacp file (Optional) +# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) +# ICON is the filename of the icon (.jpg), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .jpg +# - icon.jpg +# - /default_icon.jpg +# +# CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .json +# - config.json +# If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead +# of a homebrew executable (.nro). This is intended to be used for sysmodules. +# NACP building is skipped as well. +#--------------------------------------------------------------------------------- +SRC_DIR := ../../ +TARGET := sm +BUILD := bin +SOURCES := $(SRC_DIR) $(SRC_DIR)/snes $(SRC_DIR)/platform/switch/src $(SRC_DIR)/../third_party/gl_core + +CFILES := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/snes/*.c $(SRC_DIR)/platform/switch/src/*.c) $(SRC_DIR)/../third_party/gl_core/gl_core_3_1.c + +INCLUDES := include $(SRC_DIR)/../ ./src/ +APP_TITLE := Super Metroid +APP_AUTHOR := snesrev & Lywx +APP_VERSION := $(shell git rev-parse --short HEAD) $(shell git rev-parse --abbrev-ref HEAD) + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE + +CFLAGS := -g -Wall -O2 -ffunction-sections -Wno-parentheses \ + $(ARCH) $(DEFINES) + +CFLAGS += -D__SWITCH__ $(INCLUDE) -DSTBI_NO_THREAD_LOCALS `sdl2-config --cflags` + +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions +CFLAGS += -std=gnu11 + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := `$(PREFIX)pkg-config --libs sdl2` -lnx -lm + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(LIBNX) + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) +export TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ + + +export DEPSDIR := $(CURDIR)/$(BUILD) + +#CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +#CFILES := $(wildcard ../../*.c ../../snes/*.c) ../../third_party/gl_core/gl_core_3_1.c ../../third_party/opus-1.3.1-stripped/opus_decoder_amalgam.c +#CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +#SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +export LD := $(CXX) +#--------------------------------------------------------------------------------- + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SRC := $(CPPFILES:.cpp=.o) $(notdir $(CFILES:.c=.o)) $(SFILES:.s=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) + +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +$(info $(OFILES)) + +ifeq ($(strip $(CONFIG_JSON)),) + jsons := $(wildcard *.json) + ifneq (,$(findstring $(TARGET).json,$(jsons))) + export APP_JSON := $(TOPDIR)/$(TARGET).json + else + ifneq (,$(findstring config.json,$(jsons))) + export APP_JSON := $(TOPDIR)/config.json + endif + endif +else + export APP_JSON := $(TOPDIR)/$(CONFIG_JSON) +endif + +ifeq ($(strip $(ICON)),) + icons := $(wildcard *.jpg) + ifneq (,$(findstring $(TARGET).jpg,$(icons))) + export APP_ICON := $(TOPDIR)/$(TARGET).jpg + else + ifneq (,$(findstring icon.jpg,$(icons))) + export APP_ICON := $(TOPDIR)/icon.jpg + endif + endif +else + export APP_ICON := $(TOPDIR)/$(ICON) +endif + +ifeq ($(strip $(NO_ICON)),) + export NROFLAGS += --icon=$(APP_ICON) +endif + +ifeq ($(strip $(NO_NACP)),) + export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp +endif + +ifneq ($(APP_TITLEID),) + export NACPFLAGS += --titleid=$(APP_TITLEID) +endif + +ifneq ($(ROMFS),) + export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) +endif + +.PHONY: $(BUILD) clean all + +#--------------------------------------------------------------------------------- +all: $(BUILD) + +$(BUILD): + @echo $(CFILES) ... + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... +ifeq ($(strip $(APP_JSON)),) + @rm -fr $(BUILD) $(TARGET).nro $(TARGET).nacp $(TARGET).elf +else + @rm -fr $(BUILD) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf +endif + + +#--------------------------------------------------------------------------------- +else +.PHONY: all + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +ifeq ($(strip $(APP_JSON)),) + +all : $(OUTPUT).nro + +ifeq ($(strip $(NO_NACP)),) +$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp +else +$(OUTPUT).nro : $(OUTPUT).elf +endif + +else + +all : $(OUTPUT).nsp + +$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm + +$(OUTPUT).nso : $(OUTPUT).elf + +endif + +$(OUTPUT).elf : $(OFILES) + +$(OFILES_SRC) : $(HFILES_BIN) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o %_bin.h : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/src/platform/switch/icon.jpg b/src/platform/switch/icon.jpg new file mode 100644 index 0000000..71414da Binary files /dev/null and b/src/platform/switch/icon.jpg differ diff --git a/src/platform/switch/sm.ini b/src/platform/switch/sm.ini new file mode 100644 index 0000000..bcba937 --- /dev/null +++ b/src/platform/switch/sm.ini @@ -0,0 +1,92 @@ +[General] + +# Disable the SDL_Delay that happens each frame (Gives slightly better perf if your +# display is set to exactly 60hz) +DisableFrameDelay = 1 + +[Graphics] +# Window size ( Auto or WidthxHeight ) +WindowSize = 1920x1080 + +# Fullscreen mode (0=windowed, 1=desktop fullscreen, 2=fullscreen w/mode change) +Fullscreen = 0 + +# Window scale (1=100%, 2=200%, 3=300%, etc.) +WindowScale = 1 + +# Use an optimized (but potentially more buggy) SNES PPU implementation +NewRenderer = 1 + +# Display the world map with higher resolution +EnhancedMode7 = 1 + +# Don't keep the aspect ratio +IgnoreAspectRatio = 1 + +# Enable this option to remove the sprite limits per scan line +NoSpriteLimits = 1 + +# Use either SDL, SDL-Software, or OpenGL as the output method +# SDL-Software rendering might give better performance on Raspberry pi. +OutputMethod = SDL + +# Set to true to use linear filtering. Gives less crisp pixels. Works with SDL and OpenGL. +LinearFiltering = 0 + +# Set a glsl shader. Only supported with the OpenGL output method +# This can be the path to a .glsl or .glslp file +# Get them with: git clone https://github.com/snesrev/glsl-shaders +#Shader = glsl-shaders/hqx/hq4x.glslp + +[Sound] +EnableAudio = 1 + +# DSP frequency in samples per second (e.g. 48000, 44100, 32000, 22050, 11025) +AudioFreq = 44100 + +# number of separate sound channels (1=mono, 2=stereo) +AudioChannels = 2 + +# Audio buffer size in samples (power of 2; e.g., 4096, 2048, 1024) [try 1024 if sound is crackly]. The higher the more lag before you hear sounds. +AudioSamples = 1024 + +[KeyMap] +# Change what keyboard keys map to the joypad +# Order: Up, Down, Left, Right, Select, Start, A, B, X, Y, L, R + +# This default is suitable for QWERTY keyboards. +Controls = Up, Down, Left, Right, Right Shift, Return, x, z, s, a, c, v + +# This default is suitable for QWERTZ keyboards. +#Controls = Up, Down, Left, Right, Right Shift, Return, x, y, s, a, c, v + +# This one is suitable for AZERTY keyboards. +#Controls = Up, Down, Left, Right, Right Shift, Return, x, w, s, q, c, v + +CheatLife = w +CheatJump = Ctrl+q +ClearKeyLog = k +StopReplay = l +Fullscreen = Alt+Return +Reset = Ctrl+r +Pause = Shift+p +PauseDimmed = p +Turbo = Tab +ReplayTurbo = t +WindowBigger = Ctrl+Up +WindowSmaller = Ctrl+Down + +VolumeUp = Shift+= +VolumeDown = Shift+- + +Load = F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 +Save = Shift+F1,Shift+F2,Shift+F3,Shift+F4,Shift+F5,Shift+F6,Shift+F7,Shift+F8,Shift+F9,Shift+F10 +Replay= Ctrl+F1,Ctrl+F2,Ctrl+F3,Ctrl+F4,Ctrl+F5,Ctrl+F6,Ctrl+F7,Ctrl+F8,Ctrl+F9,Ctrl+F10 + +LoadRef = 1,2,3,4,5,6,7,8,9,0,-,=,Backspace +ReplayRef = Ctrl+1,Ctrl+2,Ctrl+3,Ctrl+4,Ctrl+5,Ctrl+6,Ctrl+7,Ctrl+8,Ctrl+9,Ctrl+0,Ctrl+-,Ctrl+=,Ctrl+Backspace + +[GamepadMap] +# Any keys used in KeyMap can be used also in this section. +# The shoulder button is called L1/Lb and L2, and the thumbstick button is called L3 +Controls = DpadUp, DpadDown, DpadLeft, DpadRight, Back, Start, B, A, Y, X, Lb, Rb diff --git a/src/platform/switch/src/switch_impl.c b/src/platform/switch/src/switch_impl.c new file mode 100644 index 0000000..0a5eba4 --- /dev/null +++ b/src/platform/switch/src/switch_impl.c @@ -0,0 +1,45 @@ +#include "switch_impl.h" + +#include +#include +#include + +void SwitchImpl_Init() { + appletInitializeGamePlayRecording(); + appletSetGamePlayRecordingState(true); + appletSetFocusHandlingMode(AppletFocusHandlingMode_NoSuspend); + socketInitializeDefault(); +#ifdef DEBUG + nxlinkStdio(); +#endif +} + +void SwitchImpl_Exit() { + appletSetGamePlayRecordingState(false); + socketExit(); +} + +void PrintErrorMessageToScreen(const char* str, ...) { + consoleInit(NULL); + + va_list args; + va_start(args, str); + vprintf(str, args); + va_end(args); + + while (appletMainLoop()) { + consoleUpdate(NULL); + } + + consoleExit(NULL); +} + +// Error messages + +void ThrowMissingROM() { + PrintErrorMessageToScreen( + "\x1b[2;2HYou've launched Super Metroid without the rom file." + "\x1b[4;2HPlease relaunch making sure sm.smc exists." + "\x1b[44;2HMade with <3 by snesrev and Lywx" + ); +} \ No newline at end of file diff --git a/src/platform/switch/src/switch_impl.h b/src/platform/switch/src/switch_impl.h new file mode 100644 index 0000000..9962dab --- /dev/null +++ b/src/platform/switch/src/switch_impl.h @@ -0,0 +1,8 @@ +#pragma once + +void SwitchImpl_Init(); +void SwitchImpl_Exit(); + +// Error messages + +void ThrowMissingROM(); \ No newline at end of file diff --git a/src/sm_cpu_infra.c b/src/sm_cpu_infra.c index 338fff8..c9e8415 100644 --- a/src/sm_cpu_infra.c +++ b/src/sm_cpu_infra.c @@ -388,7 +388,6 @@ static void VerifySnapshotsEq(Snapshot *b, Snapshot *a, Snapshot *prev) { memcpy(&a->ram[0x611], &b->ram[0x611], 6); // coroutine_state (copy from mine to theirs) memcpy(&a->ram[0x77e], &b->ram[0x77e], 5); // my counter memcpy(&a->ram[0xe20], &b->ram[0xe20], 2); // enemy_population_ptr - if (memcmp(b->ram, a->ram, 0x20000)) { fprintf(stderr, "@%d: Memory compare failed (mine != theirs, prev):\n", snes_frame_counter); @@ -502,7 +501,7 @@ int RunAsmCode(uint32 pc, uint16 a, uint16 x, uint16 y, int flags) { bool dc = g_snes->debug_cycles; g_cpu->db = pc >> 16; - + g_cpu->a = a; g_cpu->x = x; g_cpu->y = y; @@ -572,14 +571,12 @@ void RtlUpdateSnesPatchForBugfix() { Snes *SnesInit(const char *filename) { g_snes = snes_init(g_ram); - + g_cpu = g_snes->cpu; - + bool loaded = loadRom(filename, g_snes); if (!loaded) { - char buf[256]; - snprintf(buf, sizeof(buf), "unable to load rom: %s", filename); - Die(buf); + return NULL; } g_sram = g_snes->cart->ram; @@ -601,7 +598,7 @@ Snes *SnesInit(const char *filename) { { uint8 t[2] = { 0x0a, 0x0a }; PatchBytes(0x8584B2, t, 2); } // HandleMessageBoxInteraction has a loop // LoadRoomPlmGfx passes bad value - { uint8 t[] = { 0xc0, 0x00, 0x00, 0xf0, 0x03, 0x20, 0x64, 0x87, 0x60}; PatchBytes(0x84efd3, t, sizeof(t)); } + { uint8 t[] = { 0xc0, 0x00, 0x00, 0xf0, 0x03, 0x20, 0x64, 0x87, 0x60}; PatchBytes(0x84efd3, t, sizeof(t)); } { uint8 t[] = { 0xd3, 0xef }; PatchBytes(0x848243, t, sizeof(t)); } // EprojColl_8676 doesn't initialize Y @@ -629,12 +626,12 @@ Snes *SnesInit(const char *filename) { { uint8 t[] = { 0x18, 0x18, 0x18 }; PatchBytes(0xA98C12, t, sizeof(t)); } { uint8 t[] = { 0x60 }; PatchBytes(0x8085F6, t, sizeof(t)); } - + // Remove 4 frames of delay in reset routine { uint8 t[] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x80843C, t, sizeof(t)); } { uint8 t[] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x808475, t, sizeof(t)); } { uint8 t[] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x808525, t, sizeof(t)); } - + // Remove WaitUntilEndOfVblank in WaitUntilEndOfVblankAndClearHdma - We run frame by frame. { uint8 t[] = { 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x8882A1, t, sizeof(t)); } @@ -677,7 +674,7 @@ Snes *SnesInit(const char *filename) { // Patch ClearMessageBoxBg3Tilemap { uint8 t[] = { 0x18, 0x18, 0x18 }; PatchBytes(0x858203, t, sizeof(t)); } // WaitForLagFrame { uint8 t[] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x858236, t, sizeof(t)); } // HandleMusicQueue etc - + // Patch WriteMessageTilemap { uint8 t[] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }; PatchBytes(0x8582B8, t, sizeof(t)); } @@ -834,7 +831,7 @@ again_theirs: again_mine: g_snes->ppu = g_snes->my_ppu; RestoreSnapshot(&g_snapshot_before); - + g_snes->runningWhichVersion = 2; RunOneFrameOfGame(); DrawFrameToPpu(); @@ -903,4 +900,4 @@ void RtlRunFrameCompare(uint16 input, int run_what) { g_use_my_apu_code = true; RunOneFrameOfGame_Both(); } -} \ No newline at end of file +} \ No newline at end of file