Clean up script.c

This commit is contained in:
GriffinR 2021-02-19 18:52:24 -05:00
parent 81a7f491b7
commit e52b46ab43
2 changed files with 49 additions and 37 deletions

View File

@ -26,8 +26,6 @@ u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr);
void SetupNativeScript(struct ScriptContext *ctx, bool8 (*ptr)(void)); void SetupNativeScript(struct ScriptContext *ctx, bool8 (*ptr)(void));
void StopScript(struct ScriptContext *ctx); void StopScript(struct ScriptContext *ctx);
bool8 RunScriptCommand(struct ScriptContext *ctx); bool8 RunScriptCommand(struct ScriptContext *ctx);
u8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr);
const u8 *ScriptPop(struct ScriptContext *ctx);
void ScriptJump(struct ScriptContext *ctx, const u8 *ptr); void ScriptJump(struct ScriptContext *ctx, const u8 *ptr);
void ScriptCall(struct ScriptContext *ctx, const u8 *ptr); void ScriptCall(struct ScriptContext *ctx, const u8 *ptr);
void ScriptReturn(struct ScriptContext *ctx); void ScriptReturn(struct ScriptContext *ctx);

View File

@ -7,9 +7,14 @@
#define RAM_SCRIPT_MAGIC 51 #define RAM_SCRIPT_MAGIC 51
enum {
SCRIPT_MODE_STOPPED,
SCRIPT_MODE_BYTECODE,
SCRIPT_MODE_NATIVE,
};
extern const u8* gRamScriptRetAddr; extern const u8* gRamScriptRetAddr;
// ewram bss
static u8 sScriptContext1Status; static u8 sScriptContext1Status;
static struct ScriptContext sScriptContext1; static struct ScriptContext sScriptContext1;
static struct ScriptContext sScriptContext2; static struct ScriptContext sScriptContext2;
@ -23,57 +28,60 @@ void InitScriptContext(struct ScriptContext *ctx, void *cmdTable, void *cmdTable
{ {
s32 i; s32 i;
ctx->mode = 0; ctx->mode = SCRIPT_MODE_STOPPED;
ctx->scriptPtr = 0; ctx->scriptPtr = NULL;
ctx->stackDepth = 0; ctx->stackDepth = 0;
ctx->nativePtr = 0; ctx->nativePtr = NULL;
ctx->cmdTable = cmdTable; ctx->cmdTable = cmdTable;
ctx->cmdTableEnd = cmdTableEnd; ctx->cmdTableEnd = cmdTableEnd;
for (i = 0; i < 4; i++) for (i = 0; i < (int)ARRAY_COUNT(ctx->data); i++)
ctx->data[i] = 0; ctx->data[i] = 0;
for (i = 0; i < 20; i++) for (i = 0; i < (int)ARRAY_COUNT(ctx->stack); i++)
ctx->stack[i] = 0; ctx->stack[i] = NULL;
} }
u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr) u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr)
{ {
ctx->scriptPtr = ptr; ctx->scriptPtr = ptr;
ctx->mode = 1; ctx->mode = SCRIPT_MODE_BYTECODE;
return 1; return 1;
} }
void SetupNativeScript(struct ScriptContext *ctx, bool8 (*ptr)(void)) void SetupNativeScript(struct ScriptContext *ctx, bool8 (*ptr)(void))
{ {
ctx->mode = 2; ctx->mode = SCRIPT_MODE_NATIVE;
ctx->nativePtr = ptr; ctx->nativePtr = ptr;
} }
void StopScript(struct ScriptContext *ctx) void StopScript(struct ScriptContext *ctx)
{ {
ctx->mode = 0; ctx->mode = SCRIPT_MODE_STOPPED;
ctx->scriptPtr = 0; ctx->scriptPtr = NULL;
} }
bool8 RunScriptCommand(struct ScriptContext *ctx) bool8 RunScriptCommand(struct ScriptContext *ctx)
{ {
if (ctx->mode == 0) if (ctx->mode == SCRIPT_MODE_STOPPED)
return FALSE; return FALSE;
switch (ctx->mode) switch (ctx->mode)
{ {
case 0: case SCRIPT_MODE_STOPPED:
return FALSE; return FALSE;
case 2: case SCRIPT_MODE_NATIVE:
// Try to call a function in C
// Continue to bytecode if no function it returns TRUE
if (ctx->nativePtr) if (ctx->nativePtr)
{ {
if (ctx->nativePtr() == TRUE) if (ctx->nativePtr() == TRUE)
ctx->mode = 1; ctx->mode = SCRIPT_MODE_BYTECODE;
return TRUE; return TRUE;
} }
ctx->mode = 1; ctx->mode = SCRIPT_MODE_BYTECODE;
case 1: // fallthrough
case SCRIPT_MODE_BYTECODE:
while (1) while (1)
{ {
u8 cmdCode; u8 cmdCode;
@ -81,7 +89,7 @@ bool8 RunScriptCommand(struct ScriptContext *ctx)
if (!ctx->scriptPtr) if (!ctx->scriptPtr)
{ {
ctx->mode = 0; ctx->mode = SCRIPT_MODE_STOPPED;
return FALSE; return FALSE;
} }
@ -97,11 +105,11 @@ bool8 RunScriptCommand(struct ScriptContext *ctx)
if (func >= ctx->cmdTableEnd) if (func >= ctx->cmdTableEnd)
{ {
ctx->mode = 0; ctx->mode = SCRIPT_MODE_STOPPED;
return FALSE; return FALSE;
} }
if ((*func)(ctx) == 1) if ((*func)(ctx) == TRUE)
return TRUE; return TRUE;
} }
} }
@ -109,21 +117,21 @@ bool8 RunScriptCommand(struct ScriptContext *ctx)
return TRUE; return TRUE;
} }
u8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr) static bool8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr)
{ {
if (ctx->stackDepth + 1 >= 20) if (ctx->stackDepth + 1 >= (int)ARRAY_COUNT(ctx->stack))
{ {
return 1; return TRUE;
} }
else else
{ {
ctx->stack[ctx->stackDepth] = ptr; ctx->stack[ctx->stackDepth] = ptr;
ctx->stackDepth++; ctx->stackDepth++;
return 0; return FALSE;
} }
} }
const u8 *ScriptPop(struct ScriptContext *ctx) static const u8 *ScriptPop(struct ScriptContext *ctx)
{ {
if (ctx->stackDepth == 0) if (ctx->stackDepth == 0)
return NULL; return NULL;
@ -196,10 +204,10 @@ void ScriptContext1_Init(void)
bool8 ScriptContext2_RunScript(void) bool8 ScriptContext2_RunScript(void)
{ {
if (sScriptContext1Status == 2) if (sScriptContext1Status == 2)
return 0; return FALSE;
if (sScriptContext1Status == 1) if (sScriptContext1Status == 1)
return 0; return FALSE;
ScriptContext2_Enable(); ScriptContext2_Enable();
@ -207,10 +215,10 @@ bool8 ScriptContext2_RunScript(void)
{ {
sScriptContext1Status = 2; sScriptContext1Status = 2;
ScriptContext2_Disable(); ScriptContext2_Disable();
return 0; return FALSE;
} }
return 1; return TRUE;
} }
void ScriptContext1_SetupScript(const u8 *ptr) void ScriptContext1_SetupScript(const u8 *ptr)
@ -234,7 +242,7 @@ void EnableBothScriptContexts(void)
void ScriptContext2_RunNewScript(const u8 *ptr) void ScriptContext2_RunNewScript(const u8 *ptr)
{ {
InitScriptContext(&sScriptContext2, &gScriptCmdTable, &gScriptCmdTableEnd); InitScriptContext(&sScriptContext2, gScriptCmdTable, gScriptCmdTableEnd);
SetupBytecodeScript(&sScriptContext2, ptr); SetupBytecodeScript(&sScriptContext2, ptr);
while (RunScriptCommand(&sScriptContext2) == TRUE); while (RunScriptCommand(&sScriptContext2) == TRUE);
} }
@ -253,7 +261,7 @@ u8 *MapHeaderGetScriptTable(u8 tag)
if (*mapScripts == tag) if (*mapScripts == tag)
{ {
mapScripts++; mapScripts++;
return (u8 *)(mapScripts[0] + (mapScripts[1] << 8) + (mapScripts[2] << 16) + (mapScripts[3] << 24)); return T2_READ_PTR(mapScripts);
} }
mapScripts += 5; mapScripts += 5;
} }
@ -277,14 +285,20 @@ u8 *MapHeaderCheckScriptTable(u8 tag)
{ {
u16 varIndex1; u16 varIndex1;
u16 varIndex2; u16 varIndex2;
varIndex1 = ptr[0] | (ptr[1] << 8);
// Read first var (or .2byte terminal value)
varIndex1 = T1_READ_16(ptr);
if (!varIndex1) if (!varIndex1)
return NULL; return NULL; // Reached end of table
ptr += 2; ptr += 2;
varIndex2 = ptr[0] | (ptr[1] << 8);
// Read second var
varIndex2 = T1_READ_16(ptr);
ptr += 2; ptr += 2;
// Run map script if vars are equal
if (VarGet(varIndex1) == VarGet(varIndex2)) if (VarGet(varIndex1) == VarGet(varIndex2))
return (u8 *)(ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24)); return T2_READ_PTR(ptr);
ptr += 4; ptr += 4;
} }
} }