From 62a64515efa4640aa942c66174776b9ba0eb38f6 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Thu, 14 Dec 2017 10:54:23 +0300 Subject: [PATCH] added sections/banks to SYNC api --- src/console.c | 2 +- src/jsapi.c | 2 +- src/luaapi.c | 2 +- src/tic.c | 37 ++++++++++++++++++++----------------- src/tic.h | 12 ++++++++++++ src/ticapi.h | 2 +- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/console.c b/src/console.c index 7e0e940..1e07b33 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, false); + console->tic->api.sync(console->tic, tic_bank_all, 0, false); setStudioMode(TIC_RUN_MODE); } diff --git a/src/jsapi.c b/src/jsapi.c index 8889bc3..c21485f 100644 --- a/src/jsapi.c +++ b/src/jsapi.c @@ -705,7 +705,7 @@ static duk_ret_t duk_sync(duk_context* duk) bool toCart = duk_is_null_or_undefined(duk, 0) ? true : duk_to_boolean(duk, 0); - memory->api.sync(memory, toCart); + memory->api.sync(memory, tic_bank_all, 0, toCart); return 0; } diff --git a/src/luaapi.c b/src/luaapi.c index 13453e5..83510eb 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -752,7 +752,7 @@ static s32 lua_sync(lua_State* lua) if(lua_gettop(lua) >= 1) toCart = lua_toboolean(lua, 1); - memory->api.sync(memory, toCart); + memory->api.sync(memory, tic_bank_all, 0, toCart); return 0; } diff --git a/src/tic.c b/src/tic.c index 8245d9d..b2facdb 100644 --- a/src/tic.c +++ b/src/tic.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "ticapi.h" #include "tools.h" @@ -1322,29 +1323,31 @@ static void initCover(tic_mem* tic) } } -static void api_sync(tic_mem* tic, bool toCart) +static void api_sync(tic_mem* tic, tic_bank_section section, s32 bank, bool toCart) { - if(toCart) + static const struct {s32 cart; s32 ram; s32 size;} Offsets[TIC_BANK_SECTIONS] = { - memcpy(&tic->cart.bank.tiles, &tic->ram.tiles, sizeof(tic_tiles)); - memcpy(&tic->cart.bank.sprites, &tic->ram.sprites, sizeof(tic_tiles)); - memcpy(&tic->cart.bank.map, &tic->ram.map, sizeof(tic_map)); - memcpy(&tic->cart.bank.sfx, &tic->ram.sfx, sizeof(tic_sfx)); - memcpy(&tic->cart.bank.music, &tic->ram.music, sizeof(tic_music)); - } - else - { - memcpy(&tic->ram.tiles, &tic->cart.bank.tiles, sizeof(tic_tiles)); - memcpy(&tic->ram.sprites, &tic->cart.bank.sprites, sizeof(tic_tiles)); - memcpy(&tic->ram.map, &tic->cart.bank.map, sizeof(tic_map)); - memcpy(&tic->ram.sfx, &tic->cart.bank.sfx, sizeof(tic_sfx)); - memcpy(&tic->ram.music, &tic->cart.bank.music, sizeof(tic_music)); - } + {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)}, + }; + + 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)) + 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); } static void cart2ram(tic_mem* memory) { - api_sync(memory, false); + api_sync(memory, tic_bank_all, 0, false); initCover(memory); } diff --git a/src/tic.h b/src/tic.h index 777cd7f..b1263fe 100644 --- a/src/tic.h +++ b/src/tic.h @@ -108,6 +108,7 @@ #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-"} @@ -326,6 +327,17 @@ 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 75d9e3a..13572fd 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, bool toCart); + void (*sync) (tic_mem* memory, tic_bank_section 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);