nanoba-wasm/src/wasm/module.cpp
2024-04-07 12:22:50 +08:00

87 lines
1.7 KiB
C++

#include <core.hpp>
#include <nba/config.hpp>
#include <memory>
#include <nba/log.hpp>
using Level = nba::Level;
extern const u8 bios_data[16384];
static u8 *game_data;
static size_t data_size;
static nba::core::Core *vm = nullptr;
void initVM()
{
if( vm == nullptr ){
// emu core
auto config = std::make_shared<nba::Config>();
config->skip_bios = true;
vm = new nba::core::Core( config );
// bios
std::vector<u8> bios( bios_data, bios_data+sizeof(bios_data) );
vm->Attach( bios );
}
}
extern "C" {
u8* getRom( size_t size )
{
if( size == 0 ) return game_data;
if( game_data == nullptr ){
game_data = (u8*)malloc(size);
if( game_data ) data_size = size;
}
else if( data_size != size ){
free(game_data);
game_data = nullptr;
game_data = (u8*)malloc(size);
if( game_data ) data_size = size;
}
return game_data;
}
u8* getIRam()
{
return vm->getBus().memory.iram.data();
}
u8* getWRam()
{
return vm->getBus().memory.wram.data();
}
void run( u32 framecount )
{
// good! lets check the addr in 0x03007ffc
for( u32 i=0; i < framecount; ++i ){
//nba::Log<Level::Debug>( "check the irq handler {:08x}", nirq );
vm->RunForOneFrame();
}
}
void go( u32 framecount )
{
initVM();
// rom
vm->Attach( nba::ROM(std::vector<u8>(game_data, game_data+data_size), nullptr, nullptr, 0xffff'ffff) );
// start
vm->Reset();
run(framecount);
}
}
#if __EMSCRIPTEN__
int main(){
initVM();
return 0;
}
#endif