diff --git a/Makefile.common b/Makefile.common index 7ad4aacb..111aaa0e 100644 --- a/Makefile.common +++ b/Makefile.common @@ -30,6 +30,7 @@ SOURCES_C += $(CORE_DIR)/libco/libco.c \ $(GB_DIR)/Core/timing.c \ SOURCES_CXX += \ + $(BSNES_DIR)/target-libretro/program.cpp \ $(BSNES_DIR)/target-libretro/libretro.cpp \ $(BSNES_DIR)/emulator/emulator.cpp \ $(BSNES_DIR)/filter/filter.cpp \ diff --git a/bsnes/target-libretro/libretro.cpp b/bsnes/target-libretro/libretro.cpp index 784ce0b8..8eddc9c0 100644 --- a/bsnes/target-libretro/libretro.cpp +++ b/bsnes/target-libretro/libretro.cpp @@ -1,14 +1,22 @@ #include #include "libretro.h" #include "libretro_core_options.h" +#include +#include +#include "program.h" -static retro_environment_t environ_cb; -static retro_video_refresh_t video_cb; -static retro_audio_sample_t audio_cb; -static retro_audio_sample_batch_t audio_batch_cb; -static retro_input_poll_t input_poll; -static retro_input_state_t input_state; -static retro_log_printf_t libretro_print; +retro_environment_t environ_cb; +retro_video_refresh_t video_cb; +retro_audio_sample_t audio_cb; +retro_audio_sample_batch_t audio_batch_cb; +retro_input_poll_t input_poll; +retro_input_state_t input_state; +retro_log_printf_t libretro_print; + +struct Program *program = nullptr; +bool sgb_border_disabled = false; +bool retro_pointer_enabled = false; +bool retro_pointer_superscope_reverse_buttons = false; #define SAMPLERATE 48000 #define AUDIOBUFSIZE (SAMPLERATE/50) * 2 @@ -18,7 +26,7 @@ static uint16_t audio_buffer_max = AUDIOBUFSIZE; static int run_ahead_frames = 0; -static void audio_queue(int16_t left, int16_t right) +void audio_queue(int16_t left, int16_t right) { audio_buffer[audio_buffer_index++] = left; audio_buffer[audio_buffer_index++] = right; @@ -30,7 +38,7 @@ static void audio_queue(int16_t left, int16_t right) } } -#include "program.cpp" +static unique_pointer emulator; static string sgb_bios; static vector cheatList; @@ -597,12 +605,13 @@ void retro_set_input_state(retro_input_state_t cb) void retro_init() { emulator = new SuperFamicom::Interface; - program = new Program; + program = new Program(emulator.data()); } void retro_deinit() { delete program; + emulator.reset(); } unsigned retro_api_version() diff --git a/bsnes/target-libretro/program.cpp b/bsnes/target-libretro/program.cpp index f4c68ede..c18af22f 100644 --- a/bsnes/target-libretro/program.cpp +++ b/bsnes/target-libretro/program.cpp @@ -17,14 +17,27 @@ using namespace nall; #include #include -#include "resources.hpp" +#include "program.h" #include "libretro.h" +extern retro_environment_t environ_cb; +extern retro_video_refresh_t video_cb; +extern retro_audio_sample_t audio_cb; +extern retro_audio_sample_batch_t audio_batch_cb; +extern retro_input_poll_t input_poll; +extern retro_input_state_t input_state; +extern retro_log_printf_t libretro_print; +extern void audio_queue(int16_t left, int16_t right); + #define RETRO_DEVICE_LIGHTGUN_SUPER_SCOPE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0) static Emulator::Interface *emulator; +struct Program; +static Program *program = nullptr; -static bool sgb_border_disabled = false; +extern bool sgb_border_disabled; +extern bool retro_pointer_enabled; +extern bool retro_pointer_superscope_reverse_buttons; // Touchscreen Lightgun Support static const int POINTER_PRESSED_CYCLES = 4; // For touchscreen sensitivity @@ -44,8 +57,6 @@ struct retro_pointer_state }; static retro_pointer_state retro_pointer = { 0, 0, false, false, false, false }; -static bool retro_pointer_enabled = false; -static bool retro_pointer_superscope_reverse_buttons = false; static void input_update_pointer_lightgun( unsigned port, unsigned gun_device) { int x, y; @@ -166,76 +177,16 @@ static int input_handle_touchscreen_lightgun( unsigned port, unsigned gun_device } } -struct Program : Emulator::Platform -{ - Program(); - ~Program(); - - auto open(uint id, string name, vfs::file::mode mode, bool required) -> shared_pointer override; - auto load(uint id, string name, string type, vector options = {}) -> Emulator::Platform::Load override; - auto videoFrame(const uint16* data, uint pitch, uint width, uint height, uint scale) -> void override; - auto audioFrame(const double* samples, uint channels) -> void override; - auto inputPoll(uint port, uint device, uint input) -> int16 override; - auto inputRumble(uint port, uint device, uint input, bool enable) -> void override; - - auto load() -> void; - auto loadFile(string location) -> vector; - auto loadSuperFamicom(string location) -> bool; - auto loadGameBoy(string location) -> bool; - auto loadBSMemory(string location) -> bool; - - auto save() -> void; - - auto openRomSuperFamicom(string name, vfs::file::mode mode) -> shared_pointer; - auto openRomGameBoy(string name, vfs::file::mode mode) -> shared_pointer; - auto openRomBSMemory(string name, vfs::file::mode mode) -> shared_pointer; - - auto hackPatchMemory(vector& data) -> void; - - string base_name; - - bool overscan = false; - -public: - struct Game { - explicit operator bool() const { return (bool)location; } - - string option; - string location; - string manifest; - Markup::Node document; - boolean patched; - boolean verified; - }; - - struct SuperFamicom : Game { - string title; - string region; - vector program; - vector data; - vector expansion; - vector firmware; - } superFamicom; - - struct GameBoy : Game { - vector program; - } gameBoy; - - struct BSMemory : Game { - vector program; - } bsMemory; -}; - -static Program *program = nullptr; - -Program::Program() + +Program::Program(Emulator::Interface * emu) { + program = this; + emulator = emu; Emulator::platform = this; } Program::~Program() { - delete emulator; } auto Program::save() -> void diff --git a/bsnes/target-libretro/program.h b/bsnes/target-libretro/program.h new file mode 100644 index 00000000..e4ce2c15 --- /dev/null +++ b/bsnes/target-libretro/program.h @@ -0,0 +1,72 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "resources.hpp" + +struct Program : Emulator::Platform +{ + Program(Emulator::Interface * emu); + ~Program(); + + auto open(uint id, string name, vfs::file::mode mode, bool required) -> shared_pointer override; + auto load(uint id, string name, string type, vector options = {}) -> Emulator::Platform::Load override; + auto videoFrame(const uint16* data, uint pitch, uint width, uint height, uint scale) -> void override; + auto audioFrame(const double* samples, uint channels) -> void override; + auto inputPoll(uint port, uint device, uint input) -> int16 override; + auto inputRumble(uint port, uint device, uint input, bool enable) -> void override; + + auto load() -> void; + auto loadFile(string location) -> vector; + auto loadSuperFamicom(string location) -> bool; + auto loadGameBoy(string location) -> bool; + auto loadBSMemory(string location) -> bool; + + auto save() -> void; + + auto openRomSuperFamicom(string name, vfs::file::mode mode) -> shared_pointer; + auto openRomGameBoy(string name, vfs::file::mode mode) -> shared_pointer; + auto openRomBSMemory(string name, vfs::file::mode mode) -> shared_pointer; + + auto hackPatchMemory(vector& data) -> void; + + string base_name; + + bool overscan = false; + +public: + struct Game { + explicit operator bool() const { return (bool)location; } + + string option; + string location; + string manifest; + Markup::Node document; + boolean patched; + boolean verified; + }; + + struct SuperFamicom : Game { + string title; + string region; + vector program; + vector data; + vector expansion; + vector firmware; + } superFamicom; + + struct GameBoy : Game { + vector program; + } gameBoy; + + struct BSMemory : Game { + vector program; + } bsMemory; +};