diff --git a/source/quickerNES/core/cart.hpp b/source/quickerNES/core/cart.hpp index abf1a42..75d025c 100644 --- a/source/quickerNES/core/cart.hpp +++ b/source/quickerNES/core/cart.hpp @@ -34,12 +34,12 @@ 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); // Load iNES file - void load_ines(const uint8_t *buffer) + const char *load_ines(const uint8_t *buffer) { ines_header_t h; @@ -49,7 +49,10 @@ class Cart memcpy(&h, &buffer[bufferPos], copySize); bufferPos += copySize; } - if (h.zero[7]) h.flags2 = 0; + + 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; } @@ -114,4 +119,4 @@ class Cart unsigned mapper; }; -} // namespace quickerNES \ No newline at end of file +} // namespace quickerNES diff --git a/source/quickerNES/core/core.hpp b/source/quickerNES/core/core.hpp index afc13fb..95c9813 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 { @@ -138,7 +142,7 @@ class Core : private Cpu return 0; } - void open(Cart const *new_cart) + const char *open(Cart const *new_cart) { close(); init(); @@ -153,7 +157,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 +169,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 @@ -844,6 +850,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; 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; diff --git a/source/quickerNES/core/emu.cpp b/source/quickerNES/core/emu.cpp index ad513c4..751b12a 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,12 @@ 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); + const char *error = private_cart.load_ines(buffer); + if (error) return error; + + return set_cart(&private_cart); } void Emu::write_chr(void const *p, long count, long offset) @@ -839,4 +844,4 @@ void Emu::RestoreAudioBufferState() sound_buf->RestoreAudioBufferState(); } -} // namespace quickerNES \ No newline at end of file +} // namespace quickerNES 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; }