From 7e0288f514908737e719fee59cd4b3f2855c96b9 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Wed, 15 Jan 2025 14:57:58 +0100 Subject: [PATCH 1/7] fix crash/exit instead of returning error for unsupported mappers --- source/quickerNES/core/core.hpp | 6 ++++-- source/quickerNES/core/emu.cpp | 11 +++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/source/quickerNES/core/core.hpp b/source/quickerNES/core/core.hpp index afc13fb..d14792a 100644 --- a/source/quickerNES/core/core.hpp +++ b/source/quickerNES/core/core.hpp @@ -138,7 +138,7 @@ class Core : private Cpu return 0; } - void open(Cart const *new_cart) + const char *open(Cart const *new_cart) { close(); init(); @@ -153,7 +153,7 @@ class Core : private Cpu if (mapper == nullptr) { fprintf(stderr, "Could not find mapper for code: %u\n", mapperCode); - exit(-1); + return "Unsupported mapper"; } // Assigning backwards pointers to cartdrige and emulator now @@ -165,6 +165,8 @@ class Core : private Cpu cart = new_cart; memset(impl->unmapped_page, unmapped_fill, sizeof impl->unmapped_page); reset(true, true); + + return nullptr; } inline void serializeState(jaffarCommon::serializer::Base &serializer) const diff --git a/source/quickerNES/core/emu.cpp b/source/quickerNES/core/emu.cpp index ad513c4..a706be2 100644 --- a/source/quickerNES/core/emu.cpp +++ b/source/quickerNES/core/emu.cpp @@ -78,16 +78,19 @@ inline void Emu::clear_sound_buf() sound_buf->clear(); } -void Emu::set_cart(Cart const *new_cart) +const char *Emu::set_cart(Cart const *new_cart) { auto_init(); - emu.open(new_cart); + const char *error = emu.open(new_cart); + if (error) return error; channel_count_ = Apu::osc_count + emu.mapper->channel_count(); sound_buf->set_channel_count(channel_count()); set_equalizer(equalizer_); enable_sound(true); reset(); + + return nullptr; } void Emu::reset(bool full_reset, bool erase_battery_ram) @@ -167,10 +170,10 @@ const char *Emu::emulate_frame(uint32_t joypad1, uint32_t joypad2, uint32_t arka // Extras -void Emu::load_ines(const uint8_t *buffer) +const char *Emu::load_ines(const uint8_t *buffer) { private_cart.load_ines(buffer); - set_cart(&private_cart); + return set_cart(&private_cart); } void Emu::write_chr(void const *p, long count, long offset) From 7fa7b0979d6413bf6a457339b5d4243f0cbeb9b2 Mon Sep 17 00:00:00 2001 From: Sergio Martin Date: Tue, 28 Jan 2025 18:16:44 +0100 Subject: [PATCH 2/7] Adding input callback --- source/quickerNES/core/core.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/quickerNES/core/core.hpp b/source/quickerNES/core/core.hpp index afc13fb..cf2c8bd 100644 --- a/source/quickerNES/core/core.hpp +++ b/source/quickerNES/core/core.hpp @@ -26,6 +26,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include +#ifdef QUICKERNES_ENABLE_INPUT_CALLBACK +extern void (*input_callback_cb)(void); +#endif + namespace quickerNES { @@ -844,6 +848,10 @@ class Core : private Cpu input_state.arkanoid_latch = current_arkanoid_latch; input_state.arkanoid_fire = current_arkanoid_fire; #endif + + #ifdef QUICKERNES_ENABLE_INPUT_CALLBACK + input_callback_cb(); + #endif } input_state.w4016 = data; return; From be584d7ee341212a01dd2cb52ea5457d36d3d04b Mon Sep 17 00:00:00 2001 From: Sergio Martin Date: Tue, 28 Jan 2025 18:48:36 +0100 Subject: [PATCH 3/7] Fixed wrong ifdef --- source/quickerNES/core/core.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/quickerNES/core/core.hpp b/source/quickerNES/core/core.hpp index cf2c8bd..c987117 100644 --- a/source/quickerNES/core/core.hpp +++ b/source/quickerNES/core/core.hpp @@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include -#ifdef QUICKERNES_ENABLE_INPUT_CALLBACK +#ifdef _QUICKERNES_ENABLE_INPUT_CALLBACK extern void (*input_callback_cb)(void); #endif @@ -849,7 +849,7 @@ class Core : private Cpu input_state.arkanoid_fire = current_arkanoid_fire; #endif - #ifdef QUICKERNES_ENABLE_INPUT_CALLBACK + #ifdef _QUICKERNES_ENABLE_INPUT_CALLBACK input_callback_cb(); #endif } From 6376c091cf1b5f320a4166171780055328043199 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Tue, 4 Feb 2025 10:21:35 -0800 Subject: [PATCH 4/7] Don't use zero[7] in ines header Looks like this byte was used as some indicator a ripper put their name within the header, and as a response make the ROM unusable within quickNES. This byte has no defined meaning in iNES, but it does for NES 2.0 (although that can safely be ignored here) --- source/quickerNES/core/cart.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/quickerNES/core/cart.hpp b/source/quickerNES/core/cart.hpp index abf1a42..ccd61f4 100644 --- a/source/quickerNES/core/cart.hpp +++ b/source/quickerNES/core/cart.hpp @@ -34,7 +34,7 @@ class Cart uint8_t chr_count; // number of 8K CHR banks uint8_t flags; // MMMM FTBV Mapper low, Four-screen, Trainer, Battery, V mirror uint8_t flags2; // MMMM --XX Mapper high 4 bits - uint8_t zero[8]; // if zero [7] is non-zero, treat flags2 as zero + uint8_t zero[8]; }; static_assert(sizeof(ines_header_t) == 16); @@ -49,7 +49,7 @@ class Cart memcpy(&h, &buffer[bufferPos], copySize); bufferPos += copySize; } - if (h.zero[7]) h.flags2 = 0; + set_mapper(h.flags, h.flags2); // skip trainer @@ -114,4 +114,4 @@ class Cart unsigned mapper; }; -} // namespace quickerNES \ No newline at end of file +} // namespace quickerNES From ac1fde2036c3e4a2544a3f56196c33a17b60c90d Mon Sep 17 00:00:00 2001 From: SergioMartin86 Date: Thu, 6 Feb 2025 06:01:57 +0100 Subject: [PATCH 5/7] Adjusting signatures --- source/quickerNES/core/emu.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/quickerNES/core/emu.hpp b/source/quickerNES/core/emu.hpp index bdb7c81..b8e164a 100644 --- a/source/quickerNES/core/emu.hpp +++ b/source/quickerNES/core/emu.hpp @@ -22,7 +22,7 @@ class Emu // Basic setup // Load iNES file into emulator and clear recording - void load_ines(const uint8_t *buffer); + const char* load_ines(const uint8_t *buffer); // Set sample rate for sound generation const char *set_sample_rate(long); @@ -98,7 +98,7 @@ class Emu // Use already-loaded cartridge. Retains pointer, so it must be kept around until // closed. A cartridge can be shared among multiple emulators. After opening, // cartridge's CHR data shouldn't be modified since a copy is cached internally. - void set_cart(Cart const *); + const char *set_cart(Cart const *new_cart); // Pointer to current cartridge, or NULL if none is loaded Cart const *cart() const { return emu.cart; } From 4df2b49643b8e026aa701005bcc9185e793f3fcd Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Thu, 27 Feb 2025 21:54:06 +0100 Subject: [PATCH 6/7] add error check in ines_load --- source/quickerNES/core/cart.hpp | 7 ++++++- source/quickerNES/core/emu.cpp | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/source/quickerNES/core/cart.hpp b/source/quickerNES/core/cart.hpp index ccd61f4..75d025c 100644 --- a/source/quickerNES/core/cart.hpp +++ b/source/quickerNES/core/cart.hpp @@ -39,7 +39,7 @@ class Cart static_assert(sizeof(ines_header_t) == 16); // Load iNES file - void load_ines(const uint8_t *buffer) + const char *load_ines(const uint8_t *buffer) { ines_header_t h; @@ -50,6 +50,9 @@ class Cart bufferPos += copySize; } + if (memcmp(h.signature, "NES\x1A", 4) != 0) + return "Not an iNES file"; + set_mapper(h.flags, h.flags2); // skip trainer @@ -73,6 +76,8 @@ class Cart memcpy(chr(), &buffer[bufferPos], copySize); bufferPos += copySize; } + + return nullptr; } inline bool has_battery_ram() const { return mapper & 0x02; } diff --git a/source/quickerNES/core/emu.cpp b/source/quickerNES/core/emu.cpp index a706be2..751b12a 100644 --- a/source/quickerNES/core/emu.cpp +++ b/source/quickerNES/core/emu.cpp @@ -172,7 +172,9 @@ const char *Emu::emulate_frame(uint32_t joypad1, uint32_t joypad2, uint32_t arka const char *Emu::load_ines(const uint8_t *buffer) { - private_cart.load_ines(buffer); + const char *error = private_cart.load_ines(buffer); + if (error) return error; + return set_cart(&private_cart); } @@ -842,4 +844,4 @@ void Emu::RestoreAudioBufferState() sound_buf->RestoreAudioBufferState(); } -} // namespace quickerNES \ No newline at end of file +} // namespace quickerNES From 258203c776359a8cc032b5b3b729602a223ca91f Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:23:19 -0700 Subject: [PATCH 7/7] Fix building on Linux clang --- source/quickerNES/core/cpu.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/quickerNES/core/cpu.cpp b/source/quickerNES/core/cpu.cpp index 171f9da..e34d830 100644 --- a/source/quickerNES/core/cpu.cpp +++ b/source/quickerNES/core/cpu.cpp @@ -477,7 +477,7 @@ uint8_t clock_table[256] = { // This optimization is only possible with the GNU compiler -- MSVC does not allow function alignment -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(__clang__) __attribute__((optimize("align-functions=1024"))) #endif Cpu::result_t @@ -1480,8 +1480,7 @@ end: CALC_STATUS(temp); r.status = temp; } - - this->clock_count = clock_count; + r.pc = pc; r.sp = GET_SP(); r.a = a;