diff --git a/src/jsapi.c b/src/jsapi.c index 3289671..58f7457 100644 --- a/src/jsapi.c +++ b/src/jsapi.c @@ -703,12 +703,12 @@ static duk_ret_t duk_sync(duk_context* duk) { tic_mem* memory = (tic_mem*)getDukMachine(duk); - const char* section = duk_is_null_or_undefined(duk, 0) ? NULL : duk_to_string(duk, 0); + u32 mask = duk_is_null_or_undefined(duk, 0) ? 0 : duk_to_int(duk, 0); s32 bank = duk_is_null_or_undefined(duk, 1) ? 0 : duk_to_int(duk, 1); bool toCart = duk_is_null_or_undefined(duk, 2) ? false : duk_to_boolean(duk, 2); if(bank >= 0 && bank < TIC_BANKS) - memory->api.sync(memory, section, bank, toCart); + memory->api.sync(memory, mask, bank, toCart); else duk_error(duk, DUK_ERR_ERROR, "sync() error, invalid bank"); diff --git a/src/luaapi.c b/src/luaapi.c index e6d7359..9334aae 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -748,12 +748,12 @@ static s32 lua_sync(lua_State* lua) tic_mem* memory = (tic_mem*)getLuaMachine(lua); bool toCart = false; - const char* section = NULL; + u32 mask = 0; s32 bank = 0; if(lua_gettop(lua) >= 1) { - section = lua_tostring(lua, 1); + mask = getLuaNumber(lua, 1); if(lua_gettop(lua) >= 2) { @@ -767,7 +767,7 @@ static s32 lua_sync(lua_State* lua) } if(bank >= 0 && bank < TIC_BANKS) - memory->api.sync(memory, section, bank, toCart); + memory->api.sync(memory, mask, bank, toCart); else luaL_error(lua, "sync() error, invalid bank"); diff --git a/src/machine.h b/src/machine.h index f04f489..27cf229 100644 --- a/src/machine.h +++ b/src/machine.h @@ -94,7 +94,8 @@ typedef struct u8 (*getpix)(tic_mem* memory, s32 x, s32 y); void (*drawhline)(tic_mem* memory, s32 xl, s32 xr, s32 y, u8 color); - bool synced; + u32 synced; + bool initialized; } MachineState; diff --git a/src/tic.c b/src/tic.c index 1203570..dfcce3a 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1268,7 +1268,7 @@ static void api_tick_start(tic_mem* memory, const tic_sfx* sfxsrc, const tic_mus machine->state.setpix = setPixelDma; machine->state.getpix = getPixelDma; machine->state.drawhline = drawHLineDma; - machine->state.synced = false; + machine->state.synced = 0; } static void api_tick_end(tic_mem* memory) @@ -1363,43 +1363,46 @@ static void initCover(tic_mem* tic) } } -static void api_sync(tic_mem* tic, const char* section, s32 bank, bool toCart) +static void api_sync(tic_mem* tic, u32 mask, s32 bank, bool toCart) { tic_machine* machine = (tic_machine*)tic; - - if(machine->state.synced) - return; - static const struct {const char* name; s32 bank; s32 ram; s32 size;} Sections[] = + static const struct {s32 bank; s32 ram; s32 size;} Sections[] = { - {"tiles", offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles)}, - {"sprites", offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles)}, - {"map", offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map)}, - {"sfx", offsetof(tic_bank, sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx)}, - {"music", offsetof(tic_bank, music), offsetof(tic_ram, music), sizeof(tic_music)}, + {offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles) }, + {offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles) }, + {offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map) }, + {offsetof(tic_bank, sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx) }, + {offsetof(tic_bank, music), offsetof(tic_ram, music), sizeof(tic_music) }, }; + enum{Count = COUNT_OF(Sections), Mask = (1 << (Count+1)) - 1}; + + if(mask == 0) mask = Mask; + + mask &= ~machine->state.synced & Mask; + assert(bank >= 0 && bank < TIC_BANKS); - for(s32 i = 0; i < COUNT_OF(Sections); i++) + for(s32 i = 0; i < Count; i++) { - if(section == NULL || (section && strcmp(section, Sections[i].name) == 0)) + if(mask & (1 << i)) toCart ? memcpy((u8*)&tic->cart.banks[bank] + Sections[i].bank, (u8*)&tic->ram + Sections[i].ram, Sections[i].size) : memcpy((u8*)&tic->ram + Sections[i].ram, (u8*)&tic->cart.banks[bank] + Sections[i].bank, Sections[i].size); } - if(section == NULL || (section && strcmp(section, "pal") == 0)) + if(mask & (1 << Count)) toCart ? memcpy(&tic->cart.palette, &tic->ram.vram.palette, sizeof(tic_palette)) : memcpy(&tic->ram.vram.palette, &tic->cart.palette, sizeof(tic_palette)); - machine->state.synced = true; + machine->state.synced |= mask; } static void cart2ram(tic_mem* memory) { - api_sync(memory, NULL, 0, false); + api_sync(memory, 0, 0, false); initCover(memory); } diff --git a/src/ticapi.h b/src/ticapi.h index 53188c9..5693c16 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -103,7 +103,7 @@ typedef struct void (*reset) (tic_mem* memory); void (*pause) (tic_mem* memory); void (*resume) (tic_mem* memory); - void (*sync) (tic_mem* memory, const char* section, s32 bank, bool toCart); + void (*sync) (tic_mem* memory, u32 mask, s32 bank, bool toCart); u32 (*btnp) (tic_mem* memory, s32 id, s32 hold, s32 period); void (*load) (tic_cartridge* rom, const u8* buffer, s32 size, bool palette);