From ad9858a56407764ba96241d978856496f517f285 Mon Sep 17 00:00:00 2001 From: Sergio Martin Date: Sun, 14 Jan 2024 19:28:40 +0100 Subject: [PATCH] Adding no frills memcpy-based ines loader --- meson_options.txt | 1 - source/core/Nes_Cart.cpp | 21 +++++++++++++++++++++ source/core/Nes_Cart.h | 1 + source/core/Nes_Emu.cpp | 7 +++++++ source/core/Nes_Emu.h | 1 + source/emuInstance.hpp | 14 ++++++++++---- 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/meson_options.txt b/meson_options.txt index 88c694e..935082b 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -11,6 +11,5 @@ option('CPUFunctionAlignment', value : 1024, description : '''Indicates the alignment for the main emulator CPU function. This is a large function that may cross page boundaries, which impacts performance. - We recommend setting this value for the architecture own page size. This requires the GNU compiler to work.''' ) \ No newline at end of file diff --git a/source/core/Nes_Cart.cpp b/source/core/Nes_Cart.cpp index 5a1ccde..a205150 100644 --- a/source/core/Nes_Cart.cpp +++ b/source/core/Nes_Cart.cpp @@ -118,3 +118,24 @@ const char * Nes_Cart::load_ines( Auto_File_Reader in ) return 0; } + +const char * Nes_Cart::load_ines( const uint8_t* buffer ) +{ + ines_header_t h; + + size_t bufferPos = 0; + { size_t copySize = sizeof(ines_header_t); memcpy(&h, &buffer[bufferPos], copySize); bufferPos += copySize; } + if ( h.zero [7] ) h.flags2 = 0; + set_mapper( h.flags, h.flags2 ); + + // skip trainer + if ( h.flags & 0x04 ) bufferPos += 512; + + resize_prg( h.prg_count * 16 * 1024L ); + resize_chr( h.chr_count * 8 * 1024L ); + + { size_t copySize = prg_size(); memcpy(prg(), &buffer[bufferPos], copySize); bufferPos += copySize; } + { size_t copySize = chr_size(); memcpy(chr(), &buffer[bufferPos], copySize); bufferPos += copySize; } + + return 0; +} diff --git a/source/core/Nes_Cart.h b/source/core/Nes_Cart.h index 18417ca..7548ab2 100644 --- a/source/core/Nes_Cart.h +++ b/source/core/Nes_Cart.h @@ -16,6 +16,7 @@ public: ~Nes_Cart(); // Load iNES file + const char * load_ines( const uint8_t* buffer ); const char * load_ines( Auto_File_Reader ); static const char not_ines_file []; diff --git a/source/core/Nes_Emu.cpp b/source/core/Nes_Emu.cpp index 9057fea..e806f02 100644 --- a/source/core/Nes_Emu.cpp +++ b/source/core/Nes_Emu.cpp @@ -189,6 +189,13 @@ const char * Nes_Emu::emulate_frame( int joypad1, int joypad2 ) // Extras +const char * Nes_Emu::load_ines( const uint8_t* buffer ) +{ + close(); + private_cart.load_ines( buffer ); + return set_cart( &private_cart ); +} + const char * Nes_Emu::load_ines( Auto_File_Reader in ) { close(); diff --git a/source/core/Nes_Emu.h b/source/core/Nes_Emu.h index 7a2ab46..bf78e6f 100644 --- a/source/core/Nes_Emu.h +++ b/source/core/Nes_Emu.h @@ -21,6 +21,7 @@ public: // Load iNES file into emulator and clear recording const char * load_ines( Auto_File_Reader ); + const char * load_ines( const uint8_t* buffer ); // Set sample rate for sound generation const char * set_sample_rate( long ); diff --git a/source/emuInstance.hpp b/source/emuInstance.hpp index 522c205..ab848e3 100644 --- a/source/emuInstance.hpp +++ b/source/emuInstance.hpp @@ -46,10 +46,16 @@ class EmuInstance _romSHA1String = SHA1::GetHash((uint8_t*)romData.data(), romData.size()); // Loading the rom into the emulator - Mem_File_Reader romReader(romData.data(), (int)romData.size()); - Auto_File_Reader romFile(romReader); - auto result = _nes->load_ines(romFile); - if (result != 0) EXIT_WITH_ERROR("Could not initialize emulator with rom file: %s\n", romFilePath.c_str()); + #ifdef USE_ORIGINAL_QUICKNES + Mem_File_Reader romReader(romData.data(), (int)romData.size()); + Auto_File_Reader romFile(romReader); + auto result = _nes->load_ines(romFile); + if (result != 0) EXIT_WITH_ERROR("Could not initialize emulator with rom file: %s\n", romFilePath.c_str()); + #else + auto result = _nes->load_ines((const uint8_t*) romData.data()); + if (result != 0) EXIT_WITH_ERROR("Could not initialize emulator with rom file: %s\n", romFilePath.c_str()); + #endif + // Getting state size to use _stateSize = getStateSizeImpl();