From 6e8a478ee93157781f453b1b0b72ee9d1ba74b4c Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Thu, 14 Dec 2017 11:50:50 +0300 Subject: [PATCH] lua/js sync impl --- src/console.c | 2 +- src/jsapi.c | 9 +++++++-- src/luaapi.c | 21 +++++++++++++++++++-- src/tic.c | 27 +++++++++++++++------------ src/tic.h | 12 ------------ src/ticapi.h | 2 +- 6 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/console.c b/src/console.c index 1e07b33..eca851e 100644 --- a/src/console.c +++ b/src/console.c @@ -1978,7 +1978,7 @@ static void onConsoleResumeCommand(Console* console, const char* param) commandDone(console); console->tic->api.resume(console->tic); - console->tic->api.sync(console->tic, tic_bank_all, 0, false); + console->tic->api.sync(console->tic, NULL, 0, false); setStudioMode(TIC_RUN_MODE); } diff --git a/src/jsapi.c b/src/jsapi.c index c21485f..03133f3 100644 --- a/src/jsapi.c +++ b/src/jsapi.c @@ -704,8 +704,13 @@ static duk_ret_t duk_sync(duk_context* duk) tic_mem* memory = (tic_mem*)getDukMachine(duk); bool toCart = duk_is_null_or_undefined(duk, 0) ? true : duk_to_boolean(duk, 0); + const char* section = duk_is_null_or_undefined(duk, 1) ? NULL : duk_to_string(duk, 1); + s32 bank = duk_is_null_or_undefined(duk, 2) ? 0 : duk_to_int(duk, 2); - memory->api.sync(memory, tic_bank_all, 0, toCart); + if(bank >= 0 && bank < TIC_BANKS) + memory->api.sync(memory, section, bank, toCart); + else + duk_error(duk, DUK_ERR_ERROR, "sync() error, invalid bank"); return 0; } @@ -747,7 +752,7 @@ static const struct{duk_c_function func; s32 params;} ApiFunc[] = {duk_textri,14}, {duk_clip, 4}, {duk_music, 4}, - {duk_sync, 1}, + {duk_sync, 3}, }; static void initDuktape(tic_machine* machine) diff --git a/src/luaapi.c b/src/luaapi.c index 83510eb..4c3cab9 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -748,11 +748,28 @@ static s32 lua_sync(lua_State* lua) tic_mem* memory = (tic_mem*)getLuaMachine(lua); bool toCart = true; - + const char* section = NULL; + s32 bank = 0; + if(lua_gettop(lua) >= 1) + { toCart = lua_toboolean(lua, 1); - memory->api.sync(memory, tic_bank_all, 0, toCart); + if(lua_gettop(lua) >= 2) + { + section = lua_tostring(lua, 2); + + if(lua_gettop(lua) >= 3) + { + bank = getLuaNumber(lua, 3); + } + } + } + + if(bank >= 0 && bank < TIC_BANKS) + memory->api.sync(memory, section, bank, toCart); + else + luaL_error(lua, "sync() error, invalid bank"); return 0; } diff --git a/src/tic.c b/src/tic.c index b2facdb..c4369e2 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1323,31 +1323,34 @@ static void initCover(tic_mem* tic) } } -static void api_sync(tic_mem* tic, tic_bank_section section, s32 bank, bool toCart) +static void api_sync(tic_mem* tic, const char* section, s32 bank, bool toCart) { - static const struct {s32 cart; s32 ram; s32 size;} Offsets[TIC_BANK_SECTIONS] = + static const struct {const char* name; s32 cart; s32 ram; s32 size;} Sections[] = { - {offsetof(tic_cartridge, bank.tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles)}, - {offsetof(tic_cartridge, bank.sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles)}, - {offsetof(tic_cartridge, bank.map), offsetof(tic_ram, map), sizeof(tic_map)}, - {offsetof(tic_cartridge, bank.sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx)}, - {offsetof(tic_cartridge, bank.palette), offsetof(tic_ram, vram.palette), sizeof(tic_palette)}, + {"tiles", offsetof(tic_cartridge, bank.tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles)}, + {"sprites", offsetof(tic_cartridge, bank.sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles)}, + {"map", offsetof(tic_cartridge, bank.map), offsetof(tic_ram, map), sizeof(tic_map)}, + {"sfx", offsetof(tic_cartridge, bank.sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx)}, + {"music", offsetof(tic_cartridge, bank.music), offsetof(tic_ram, music), sizeof(tic_music)}, + {"pal", offsetof(tic_cartridge, bank.palette), offsetof(tic_ram, vram.palette), sizeof(tic_palette)}, }; assert(bank >= 0 && bank < TIC_BANKS); s32 bankOffset = bank * sizeof(tic_bank); - for(s32 i = 0; i < TIC_BANK_SECTIONS; i++) - if(section & (1 << i)) + for(s32 i = 0; i < COUNT_OF(Sections); i++) + { + if(section == NULL || (section && strcmp(section, Sections[i].name) == 0)) toCart - ? memcpy((u8*)&tic->cart + Offsets[i].cart + bankOffset, (u8*)&tic->ram + Offsets[i].ram, Offsets[i].size) - : memcpy((u8*)&tic->ram + Offsets[i].ram, (u8*)&tic->cart + Offsets[i].cart + bankOffset, Offsets[i].size); + ? memcpy((u8*)&tic->cart + Sections[i].cart + bankOffset, (u8*)&tic->ram + Sections[i].ram, Sections[i].size) + : memcpy((u8*)&tic->ram + Sections[i].ram, (u8*)&tic->cart + Sections[i].cart + bankOffset, Sections[i].size); + } } static void cart2ram(tic_mem* memory) { - api_sync(memory, tic_bank_all, 0, false); + api_sync(memory, NULL, 0, false); initCover(memory); } diff --git a/src/tic.h b/src/tic.h index b1263fe..777cd7f 100644 --- a/src/tic.h +++ b/src/tic.h @@ -108,7 +108,6 @@ #define TIC_CODE_SIZE (0x10000) #define TIC_BANKS 8 -#define TIC_BANK_SECTIONS 5 #define SFX_NOTES {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"} @@ -327,17 +326,6 @@ typedef struct tic_tile data[TIC_BANK_SPRITES]; } tic_tiles; -typedef enum -{ - tic_bank_tiles = 1 << 0, - tic_bank_sprites = 1 << 1, - tic_bank_map = 1 << 2, - tic_bank_sfx = 1 << 3, - tic_bank_music = 1 << 4, - tic_bank_palette = 1 << 5, - tic_bank_all = (1 << (TIC_BANK_SECTIONS+1)) - 1, -} tic_bank_section; - typedef struct { tic_tiles tiles; diff --git a/src/ticapi.h b/src/ticapi.h index 13572fd..cc08966 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, tic_bank_section section, s32 bank, bool toCart); + void (*sync) (tic_mem* memory, const char* section, s32 bank, bool toCart); u32 (*btnp) (tic_mem* memory, s32 id, s32 hold, s32 period); void (*load) (tic_cartridge* rom, s32 cartSize, const u8* buffer, s32 size, bool palette);