Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes from the TASEmulators fork #31

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
13 changes: 9 additions & 4 deletions source/quickerNES/core/cart.hpp
Original file line number Diff line number Diff line change
@@ -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
} // namespace quickerNES
14 changes: 12 additions & 2 deletions source/quickerNES/core/core.hpp
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdexcept>
#include <string>

#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;
17 changes: 11 additions & 6 deletions source/quickerNES/core/emu.cpp
Original file line number Diff line number Diff line change
@@ -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
} // namespace quickerNES
4 changes: 2 additions & 2 deletions source/quickerNES/core/emu.hpp
Original file line number Diff line number Diff line change
@@ -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; }