From 8b47178add1e78da5ff96b335ecac73f34f4a8e9 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Wed, 8 Nov 2023 16:21:21 -0500 Subject: Move GBACart-related global state into objects (#1870) - RAII will now do the heavy lifting - Mark some methods as const or noexcept - Once the `NDS` object is finalized, most of these `assert`s can go away --- src/GBACart.h | 98 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'src/GBACart.h') diff --git a/src/GBACart.h b/src/GBACart.h index 32ff6d5..7447f6f 100644 --- a/src/GBACart.h +++ b/src/GBACart.h @@ -53,7 +53,7 @@ public: virtual int SetInput(int num, bool pressed); - virtual u16 ROMRead(u32 addr); + virtual u16 ROMRead(u32 addr) const; virtual void ROMWrite(u32 addr, u16 val); virtual u8 SRAMRead(u32 addr); @@ -83,7 +83,7 @@ public: virtual void SetupSave(u32 type) override; virtual void LoadSave(const u8* savedata, u32 savelen) override; - virtual u16 ROMRead(u32 addr) override; + virtual u16 ROMRead(u32 addr) const override; virtual void ROMWrite(u32 addr, u16 val) override; virtual u8 SRAMRead(u32 addr) override; @@ -180,7 +180,7 @@ public: void DoSavestate(Savestate* file) override; - u16 ROMRead(u32 addr) override; + u16 ROMRead(u32 addr) const override; void ROMWrite(u32 addr, u16 val) override; private: @@ -195,13 +195,52 @@ enum Input_SolarSensorUp, }; -extern std::unique_ptr Cart; - -bool Init(); -void DeInit(); -void Reset(); - -void DoSavestate(Savestate* file); +class GBACartSlot +{ +public: + GBACartSlot() noexcept = default; + ~GBACartSlot() noexcept = default; + void Reset() noexcept; + void DoSavestate(Savestate* file) noexcept; + /// Applies the GBACartData to the emulator state and unloads an existing ROM if any. + /// Upon successful insertion, \c cart will be nullptr and the global GBACart state + /// (\c CartROM, CartInserted, etc.) will be updated. + bool InsertROM(std::unique_ptr&& cart) noexcept; + bool LoadROM(const u8* romdata, u32 romlen) noexcept; + void LoadSave(const u8* savedata, u32 savelen) noexcept; + + void LoadAddon(int type) noexcept; + + void EjectCart() noexcept; + + // TODO: make more flexible, support nonbinary inputs + int SetInput(int num, bool pressed) noexcept; + + void SetOpenBusDecay(u16 val) noexcept { OpenBusDecay = val; } + + u16 ROMRead(u32 addr) const noexcept; + void ROMWrite(u32 addr, u16 val) noexcept; + + u8 SRAMRead(u32 addr) noexcept; + void SRAMWrite(u32 addr, u8 val) noexcept; + + /// This function is intended to allow frontends to save and load SRAM + /// without using melonDS APIs. + /// Modifying the emulated SRAM for any other reason is strongly discouraged. + /// The returned pointer may be invalidated if the emulator is reset, + /// or when a new game is loaded. + /// Consequently, don't store the returned pointer for any longer than necessary. + /// @returns Pointer to this cart's SRAM if a cart is loaded and supports SRAM, otherwise \c nullptr. + [[nodiscard]] u8* GetSaveMemory() noexcept { return Cart ? Cart->GetSaveMemory() : nullptr; } + [[nodiscard]] const u8* GetSaveMemory() const noexcept { return Cart ? Cart->GetSaveMemory() : nullptr; } + + /// @returns The length of the buffer returned by ::GetSaveMemory() + /// if a cart is loaded and supports SRAM, otherwise zero. + [[nodiscard]] u32 GetSaveMemoryLength() const noexcept { return Cart ? Cart->GetSaveMemoryLength() : 0; } +private: + std::unique_ptr Cart = nullptr; + u16 OpenBusDecay = 0; +}; /// Parses the given ROM data and constructs a \c GBACart::CartCommon subclass /// that can be inserted into the emulator or used to extract information about the cart beforehand. @@ -213,45 +252,6 @@ void DoSavestate(Savestate* file); /// or \c nullptr if the ROM data couldn't be parsed. std::unique_ptr ParseROM(const u8* romdata, u32 romlen); -/// Applies the GBACartData to the emulator state and unloads an existing ROM if any. -/// Upon successful insertion, \c cart will be nullptr and the global GBACart state -/// (\c CartROM, CartInserted, etc.) will be updated. -bool InsertROM(std::unique_ptr&& cart); -bool LoadROM(const u8* romdata, u32 romlen); -void LoadSave(const u8* savedata, u32 savelen); - -void LoadAddon(int type); - -void EjectCart(); - -//bool LoadROM(const char* path, const char* sram); -//bool LoadROM(const u8* romdata, u32 filelength, const char *sram); -//void RelocateSave(const char* path, bool write); - -// TODO: make more flexible, support nonbinary inputs -int SetInput(int num, bool pressed); - -void SetOpenBusDecay(u16 val); - -u16 ROMRead(u32 addr); -void ROMWrite(u32 addr, u16 val); - -u8 SRAMRead(u32 addr); -void SRAMWrite(u32 addr, u8 val); - -/// This function is intended to allow frontends to save and load SRAM -/// without using melonDS APIs. -/// Modifying the emulated SRAM for any other reason is strongly discouraged. -/// The returned pointer may be invalidated if the emulator is reset, -/// or when a new game is loaded. -/// Consequently, don't store the returned pointer for any longer than necessary. -/// @returns Pointer to this cart's SRAM if a cart is loaded and supports SRAM, otherwise \c nullptr. -u8* GetSaveMemory(); - -/// @returns The length of the buffer returned by ::GetSaveMemory() -/// if a cart is loaded and supports SRAM, otherwise zero. -u32 GetSaveMemoryLength(); - } #endif // GBACART_H -- cgit v1.2.3