From 2a7dfdc74a5e0f0c2facca16778a7131e4c1d2fd Mon Sep 17 00:00:00 2001 From: Sergio Martin Date: Sun, 28 Jan 2024 19:57:24 +0100 Subject: [PATCH 1/2] More simplfiication --- source/nesInstanceBase.hpp | 9 ++-- source/playbackInstance.hpp | 10 +++++ source/quickNES/nesInstance.hpp | 68 ++++++++++++------------------- source/quickerNES/nesInstance.hpp | 54 ++++++++++-------------- source/utils.hpp | 2 +- 5 files changed, 62 insertions(+), 81 deletions(-) diff --git a/source/nesInstanceBase.hpp b/source/nesInstanceBase.hpp index b071089..5daad89 100644 --- a/source/nesInstanceBase.hpp +++ b/source/nesInstanceBase.hpp @@ -95,11 +95,8 @@ class NESInstanceBase // Virtual functions - virtual uint8_t *getLowMem() const = 0; - virtual uint8_t *getNametableMem() const = 0; - virtual uint8_t *getHighMem() const = 0; - virtual const uint8_t *getChrMem() const = 0; - virtual size_t getChrMemSize() const = 0; + virtual uint8_t *getLowMem() = 0; + virtual void serializeState(uint8_t *state) const = 0; virtual void deserializeState(const uint8_t *state) = 0; virtual size_t getStateSize() const = 0; @@ -107,7 +104,7 @@ class NESInstanceBase virtual void doSoftReset() = 0; virtual void doHardReset() = 0; virtual std::string getCoreName() const = 0; - virtual void *getInternalEmulatorPointer() const = 0; + virtual void *getInternalEmulatorPointer() = 0; protected: diff --git a/source/playbackInstance.hpp b/source/playbackInstance.hpp index b7f1ec8..d0e5a67 100644 --- a/source/playbackInstance.hpp +++ b/source/playbackInstance.hpp @@ -39,6 +39,12 @@ class PlaybackInstance // Initializes the playback module instance PlaybackInstance(NESInstance *emu, const std::vector &sequence, const std::string &overlayPath = "") : _emu(emu) { + // Allocating video buffer + _video_buffer = (uint8_t *)malloc(image_width * image_height); + + // Setting video buffer + ((emulator_t*)_emu->getInternalEmulatorPointer())->set_pixels(_video_buffer, image_width + 8); + // Enabling emulation rendering _emu->enableRendering(); @@ -224,6 +230,7 @@ class PlaybackInstance } private: + // Internal sequence information std::vector _stepSequence; @@ -239,6 +246,9 @@ class PlaybackInstance // Flag to store whether to use the button overlay bool _useOverlay = false; + // Video buffer + uint8_t *_video_buffer; + // Overlay info std::string _overlayPath; SDL_Surface *_overlayBaseSurface = NULL; diff --git a/source/quickNES/nesInstance.hpp b/source/quickNES/nesInstance.hpp index d729dc5..de0ca6e 100644 --- a/source/quickNES/nesInstance.hpp +++ b/source/quickNES/nesInstance.hpp @@ -12,56 +12,31 @@ extern void register_misc_mappers(); extern void register_extra_mappers(); extern void register_mapper_70(); -class NESInstance : public NESInstanceBase +class NESInstance final : public NESInstanceBase { public: NESInstance() : NESInstanceBase() { - // Creating new emulator - _nes = new Nes_Emu; - - // Allocating video buffer - video_buffer = (uint8_t *)malloc(image_width * image_height); - - // Setting video buffer - _nes->set_pixels(video_buffer, image_width + 8); - // If running the original QuickNES, register extra mappers now register_misc_mappers(); register_extra_mappers(); register_mapper_70(); } - virtual bool loadROMImpl(const uint8_t* romData, const size_t romSize) override - { - // Loading rom data - Mem_File_Reader romReader(romData, (int)romSize); - Auto_File_Reader romFile(romReader); - auto result = _nes->load_ines(romFile); - return result == 0; - } - - uint8_t *getLowMem() const override { return _nes->low_mem(); }; - uint8_t *getNametableMem() const override { return _nes->nametable_mem(); }; - uint8_t *getHighMem() const override { return _nes->high_mem(); }; - const uint8_t *getChrMem() const override { return _nes->chr_mem(); }; - size_t getChrMemSize() const override { return _nes->chr_size(); }; - - void enableStateBlockImpl(const std::string& block) override {}; - void disableStateBlockImpl(const std::string& block) override {}; + uint8_t *getLowMem() override { return _nes.low_mem(); }; void serializeState(uint8_t *state) const override { Mem_Writer w(state, _stateSize, 0); Auto_File_Writer a(w); - _nes->save_state(a); + _nes.save_state(a); } void deserializeState(const uint8_t *state) override { Mem_File_Reader r(state, _stateSize); Auto_File_Reader a(r); - _nes->load_state(a); + _nes.load_state(a); } inline size_t getStateSize() const override @@ -69,28 +44,39 @@ class NESInstance : public NESInstanceBase uint8_t *data = (uint8_t *)malloc(_DUMMY_SIZE); Mem_Writer w(data, _DUMMY_SIZE); Auto_File_Writer a(w); - _nes->save_state(a); + _nes.save_state(a); free(data); return w.size(); } - void advanceStateImpl(const Controller::port_t controller1, const Controller::port_t controller2) override + std::string getCoreName() const override { return "QuickNES"; } + void doSoftReset() override { _nes.reset(false); } + void doHardReset() override { _nes.reset(true); } + + void *getInternalEmulatorPointer() override { return &_nes; } + + protected: + + bool loadROMImpl(const uint8_t* romData, const size_t romSize) override { - if (_doRendering == true) _nes->emulate_frame(controller1, controller2); - if (_doRendering == false) _nes->emulate_skip_frame(controller1, controller2); + // Loading rom data + Mem_File_Reader romReader(romData, (int)romSize); + Auto_File_Reader romFile(romReader); + auto result = _nes.load_ines(romFile); + return result == 0; } - std::string getCoreName() const override { return "QuickNES"; } - void doSoftReset() override { _nes->reset(false); } - void doHardReset() override { _nes->reset(true); } + void enableStateBlockImpl(const std::string& block) override {}; + void disableStateBlockImpl(const std::string& block) override {}; - void *getInternalEmulatorPointer() const override { return _nes; } + void advanceStateImpl(const Controller::port_t controller1, const Controller::port_t controller2) override + { + if (_doRendering == true) _nes.emulate_frame(controller1, controller2); + if (_doRendering == false) _nes.emulate_skip_frame(controller1, controller2); + } private: - // Video buffer - uint8_t *video_buffer; - // Emulator instance - Nes_Emu *_nes; + emulator_t _nes; }; diff --git a/source/quickerNES/nesInstance.hpp b/source/quickerNES/nesInstance.hpp index 7da944a..96c3f92 100644 --- a/source/quickerNES/nesInstance.hpp +++ b/source/quickerNES/nesInstance.hpp @@ -5,57 +5,45 @@ typedef quickerNES::Emu emulator_t; -class NESInstance : public NESInstanceBase +class NESInstance final : public NESInstanceBase { public: - NESInstance() : NESInstanceBase() - { - // Creating new emulator - _nes = new emulator_t; - // Allocating video buffer - video_buffer = (uint8_t *)malloc(image_width * image_height); + uint8_t *getLowMem() override { return _nes.low_mem(); }; - // Setting video buffer - _nes->set_pixels(video_buffer, image_width + 8); - } + void serializeState(uint8_t *state) const override { _nes.serializeState(state); } + void deserializeState(const uint8_t *state) override { _nes.deserializeState(state); } + size_t getStateSize() const override { return _nes.getStateSize(); } - ~NESInstance() = default; + std::string getCoreName() const override { return "QuickerNES"; } + + void doSoftReset() override { _nes.reset(false); } + void doHardReset() override { _nes.reset(true); } + + void *getInternalEmulatorPointer() override { return &_nes; } + + protected: - virtual bool loadROMImpl(const uint8_t* romData, const size_t romSize) override + bool loadROMImpl(const uint8_t* romData, const size_t romSize) override { // Loading rom data - _nes->load_ines(romData); + _nes.load_ines(romData); return true; } - uint8_t *getLowMem() const override { return _nes->low_mem(); }; - uint8_t *getNametableMem() const override { return _nes->nametable_mem(); }; - uint8_t *getHighMem() const override { return _nes->high_mem(); }; - const uint8_t *getChrMem() const override { return _nes->chr_mem(); }; - size_t getChrMemSize() const override { return _nes->chr_size(); }; - - void serializeState(uint8_t *state) const override { _nes->serializeState(state); } - void deserializeState(const uint8_t *state) override { _nes->deserializeState(state); } - size_t getStateSize() const override { return _nes->getStateSize(); } - void enableStateBlockImpl(const std::string& block) override { _nes->enableStateBlock(block); }; - void disableStateBlockImpl(const std::string& block) override { _nes->disableStateBlock(block); }; - + void enableStateBlockImpl(const std::string& block) override { _nes.enableStateBlock(block); }; + void disableStateBlockImpl(const std::string& block) override { _nes.disableStateBlock(block); }; void advanceStateImpl(const Controller::port_t controller1, const Controller::port_t controller2) override { - if (_doRendering == true) _nes->emulate_frame(controller1, controller2); - if (_doRendering == false) _nes->emulate_skip_frame(controller1, controller2); + if (_doRendering == true) _nes.emulate_frame(controller1, controller2); + if (_doRendering == false) _nes.emulate_skip_frame(controller1, controller2); } - std::string getCoreName() const override { return "QuickerNES"; } - void doSoftReset() override { _nes->reset(false); } - void doHardReset() override { _nes->reset(true); } - - void *getInternalEmulatorPointer() const override { return _nes; } + private: // Video buffer uint8_t *video_buffer; // Emulator instance - emulator_t *_nes; + emulator_t _nes; }; diff --git a/source/utils.hpp b/source/utils.hpp index 64e3976..c9f3319 100644 --- a/source/utils.hpp +++ b/source/utils.hpp @@ -167,7 +167,7 @@ inline hash_t calculateMetroHash(uint8_t *data, size_t size) return result; } -inline hash_t calculateStateHash(const NESInstance* nes) +inline hash_t calculateStateHash(NESInstance* nes) { return calculateMetroHash(nes->getLowMem(), _LOW_MEM_SIZE); } \ No newline at end of file From 280655e53c1c85c2b0eeca2eb327535635bfe64c Mon Sep 17 00:00:00 2001 From: Sergio Martin Date: Sun, 28 Jan 2024 19:57:38 +0100 Subject: [PATCH 2/2] More simplfiication --- source/quickerNES/nesInstance.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/quickerNES/nesInstance.hpp b/source/quickerNES/nesInstance.hpp index 96c3f92..36a0efa 100644 --- a/source/quickerNES/nesInstance.hpp +++ b/source/quickerNES/nesInstance.hpp @@ -41,9 +41,6 @@ class NESInstance final : public NESInstanceBase private: - // Video buffer - uint8_t *video_buffer; - // Emulator instance emulator_t _nes; };