added mask parameter for sync()
This commit is contained in:
BADIM-PC\Vadim 2017-12-17 22:11:01 +03:00
parent b35513b692
commit d2fe2adddb
5 changed files with 27 additions and 23 deletions

View File

@ -703,12 +703,12 @@ static duk_ret_t duk_sync(duk_context* duk)
{ {
tic_mem* memory = (tic_mem*)getDukMachine(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); 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); bool toCart = duk_is_null_or_undefined(duk, 2) ? false : duk_to_boolean(duk, 2);
if(bank >= 0 && bank < TIC_BANKS) if(bank >= 0 && bank < TIC_BANKS)
memory->api.sync(memory, section, bank, toCart); memory->api.sync(memory, mask, bank, toCart);
else else
duk_error(duk, DUK_ERR_ERROR, "sync() error, invalid bank"); duk_error(duk, DUK_ERR_ERROR, "sync() error, invalid bank");

View File

@ -748,12 +748,12 @@ static s32 lua_sync(lua_State* lua)
tic_mem* memory = (tic_mem*)getLuaMachine(lua); tic_mem* memory = (tic_mem*)getLuaMachine(lua);
bool toCart = false; bool toCart = false;
const char* section = NULL; u32 mask = 0;
s32 bank = 0; s32 bank = 0;
if(lua_gettop(lua) >= 1) if(lua_gettop(lua) >= 1)
{ {
section = lua_tostring(lua, 1); mask = getLuaNumber(lua, 1);
if(lua_gettop(lua) >= 2) if(lua_gettop(lua) >= 2)
{ {
@ -767,7 +767,7 @@ static s32 lua_sync(lua_State* lua)
} }
if(bank >= 0 && bank < TIC_BANKS) if(bank >= 0 && bank < TIC_BANKS)
memory->api.sync(memory, section, bank, toCart); memory->api.sync(memory, mask, bank, toCart);
else else
luaL_error(lua, "sync() error, invalid bank"); luaL_error(lua, "sync() error, invalid bank");

View File

@ -94,7 +94,8 @@ typedef struct
u8 (*getpix)(tic_mem* memory, s32 x, s32 y); u8 (*getpix)(tic_mem* memory, s32 x, s32 y);
void (*drawhline)(tic_mem* memory, s32 xl, s32 xr, s32 y, u8 color); void (*drawhline)(tic_mem* memory, s32 xl, s32 xr, s32 y, u8 color);
bool synced; u32 synced;
bool initialized; bool initialized;
} MachineState; } MachineState;

View File

@ -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.setpix = setPixelDma;
machine->state.getpix = getPixelDma; machine->state.getpix = getPixelDma;
machine->state.drawhline = drawHLineDma; machine->state.drawhline = drawHLineDma;
machine->state.synced = false; machine->state.synced = 0;
} }
static void api_tick_end(tic_mem* memory) 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; tic_machine* machine = (tic_machine*)tic;
if(machine->state.synced) static const struct {s32 bank; s32 ram; s32 size;} Sections[] =
return;
static const struct {const char* name; s32 bank; s32 ram; s32 size;} Sections[] =
{ {
{"tiles", offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles)}, {offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles) },
{"sprites", offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles)}, {offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles) },
{"map", offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map)}, {offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map) },
{"sfx", offsetof(tic_bank, sfx), offsetof(tic_ram, sfx), sizeof(tic_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, 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); 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 toCart
? memcpy((u8*)&tic->cart.banks[bank] + Sections[i].bank, (u8*)&tic->ram + Sections[i].ram, Sections[i].size) ? 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); : 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 toCart
? memcpy(&tic->cart.palette, &tic->ram.vram.palette, sizeof(tic_palette)) ? memcpy(&tic->cart.palette, &tic->ram.vram.palette, sizeof(tic_palette))
: memcpy(&tic->ram.vram.palette, &tic->cart.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) static void cart2ram(tic_mem* memory)
{ {
api_sync(memory, NULL, 0, false); api_sync(memory, 0, 0, false);
initCover(memory); initCover(memory);
} }

View File

@ -103,7 +103,7 @@ typedef struct
void (*reset) (tic_mem* memory); void (*reset) (tic_mem* memory);
void (*pause) (tic_mem* memory); void (*pause) (tic_mem* memory);
void (*resume) (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); u32 (*btnp) (tic_mem* memory, s32 id, s32 hold, s32 period);
void (*load) (tic_cartridge* rom, const u8* buffer, s32 size, bool palette); void (*load) (tic_cartridge* rom, const u8* buffer, s32 size, bool palette);