From b2059ca1ad5c95fb60b5502d84656aee0b1ce01e Mon Sep 17 00:00:00 2001 From: ProjectRevoTPP Date: Tue, 21 Apr 2020 11:46:50 -0400 Subject: decompile script.c --- src/script.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 src/script.c (limited to 'src/script.c') diff --git a/src/script.c b/src/script.c new file mode 100644 index 00000000..e354ec9c --- /dev/null +++ b/src/script.c @@ -0,0 +1,229 @@ +#include "nitro.h" +#include "script.h" + +void InitScriptContext(struct ScriptContext *ctx, void *cmdTable, void *cmdTableEnd) +{ + u32 i; + + ctx->mode = 0; + ctx->scriptPtr = 0; + ctx->stackDepth = 0; + ctx->nativePtr = 0; + ctx->cmdTable = cmdTable; + ctx->cmdTableEnd = cmdTableEnd; + + for (i = 0; i < 4; i++) + ctx->data[i] = 0; + + for (i = 0; i < 20; i++) + ctx->stack[i] = 0; + + ctx->unk74 = 0; +} + +u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr) +{ + ctx->scriptPtr = ptr; + ctx->mode = 1; + return 1; +} + +void SetupNativeScript(struct ScriptContext *ctx, u8 (*ptr)(void)) +{ + ctx->mode = 2; + ctx->nativePtr = ptr; +} + +void StopScript(struct ScriptContext *ctx) +{ + ctx->mode = 0; + ctx->scriptPtr = 0; +} + +void FUN_02038B6C(struct ScriptContext *ctx, int r1) +{ + ctx->unk74 = r1; +} + +extern void ErrorHandling(void); + +#ifdef NONMATCHING +u8 RunScriptCommand(struct ScriptContext *ctx) +{ + struct ScriptContext *localCtx = ctx; + + if (localCtx->mode == 0) + return FALSE; + + switch (localCtx->mode) + { + case 0: + return FALSE; + case 2: + if (localCtx->nativePtr) + { + if (localCtx->nativePtr() == TRUE) + localCtx->mode = 1; + return TRUE; + } + localCtx->mode = 1; + case 1: + while (1) + { + u16 cmdCode; + ScrCmdFunc *func; + + if (!localCtx->scriptPtr) + { + localCtx->mode = 0; + return FALSE; + } + + cmdCode = ScriptReadHalfword(localCtx); + if ((u32)localCtx->cmdTableEnd >= cmdCode) + { + ErrorHandling(); + localCtx->mode = 0; + return FALSE; + } + + func = &localCtx->cmdTable[cmdCode]; + + if ((*func)(localCtx) == 1) + break; + } + } + + return TRUE; +} +#else +u8 RunScriptCommand(struct ScriptContext *ctx) +{ + __asm { + add r4, r0, #0x0 + ldrb r1, [r4, #0x1] + cmp r1, #0x0 + bne _02038B7E + mov r0, #0x0 + pop {r4, pc} + _02038B7E: + beq _02038B8A + cmp r1, #0x1 + beq _02038BA6 + cmp r1, #0x2 + beq _02038B8E + b _02038BD8 + _02038B8A: + mov r0, #0x0 + pop {r4, pc} + _02038B8E: + ldr r1, [r4, #0x4] + cmp r1, #0x0 + beq _02038BA2 + blx r1 + cmp r0, #0x1 + bne _02038B9E + mov r0, #0x1 + strb r0, [r4, #0x1] + _02038B9E: + mov r0, #0x1 + pop {r4, pc} + _02038BA2: + mov r0, #0x1 + strb r0, [r4, #0x1] + // for some reason it adds a b _02038BA6 here + _02038BA6: + ldr r0, [r4, #0x8] + cmp r0, #0x0 + bne _02038BB2 + mov r0, #0x0 + strb r0, [r4, #0x1] + pop {r4, pc} + _02038BB2: + add r0, r4, #0x0 + bl ScriptReadHalfword + add r1, r0, #0x0 + ldr r0, [r4, #0x60] + cmp r1, r0 + blo _02038BCA + bl ErrorHandling + mov r0, #0x0 + strb r0, [r4, #0x1] + pop {r4, pc} + _02038BCA: + ldr r2, [r4, #0x5c] + lsl r1, r1, #0x2 + ldr r1, [r2, r1] + add r0, r4, #0x0 + blx r1 + cmp r0, #0x1 + bne _02038BA6 + _02038BD8: + mov r0, #0x1 + } +} +#endif + +u8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr) +{ + if (ctx->stackDepth + 1 >= 20) + { + return 1; + } + else + { + ctx->stack[ctx->stackDepth] = ptr; + ctx->stackDepth++; + return 0; + } +} + +const u8 *ScriptPop(struct ScriptContext *ctx) +{ + if (ctx->stackDepth == 0) + return NULL; + + ctx->stackDepth--; + return ctx->stack[ctx->stackDepth]; +} + +void ScriptJump(struct ScriptContext *ctx, const u8 *ptr) +{ + ctx->scriptPtr = ptr; +} + +void ScriptCall(struct ScriptContext *ctx, const u8 *ptr) +{ + ScriptPush(ctx, ctx->scriptPtr); + ctx->scriptPtr = ptr; +} + +void ScriptReturn(struct ScriptContext *ctx) +{ + ctx->scriptPtr = ScriptPop(ctx); +} + +u16 ScriptReadHalfword(struct ScriptContext *ctx) +{ + u16 value = *(ctx->scriptPtr++); + value += *(ctx->scriptPtr++) << 8; + return value; +} + +u32 ScriptReadWord(struct ScriptContext *ctx) +{ + u32 value0 = *(ctx->scriptPtr++); + u32 value1 = *(ctx->scriptPtr++); + u32 value2 = *(ctx->scriptPtr++); + u32 value3 = *(ctx->scriptPtr++); + u32 retVal = 0; + + retVal += value3; + retVal <<= 8; + retVal += value2; + retVal <<= 8; + retVal += value1; + retVal <<= 8; + retVal += value0; + return retVal; +} -- cgit v1.2.3 From fb6be2fb9463b1542e4cc839627b2d2d1bb0afbe Mon Sep 17 00:00:00 2001 From: ProjectRevoTPP Date: Tue, 21 Apr 2020 12:03:53 -0400 Subject: fix various issues and NONMATCHING. Thanks Demki! --- src/script.c | 84 ++++++++---------------------------------------------------- 1 file changed, 10 insertions(+), 74 deletions(-) (limited to 'src/script.c') diff --git a/src/script.c b/src/script.c index e354ec9c..e8b8ee60 100644 --- a/src/script.c +++ b/src/script.c @@ -1,6 +1,8 @@ #include "nitro.h" #include "script.h" +u16 ScriptReadHalfword(struct ScriptContext *ctx); + void InitScriptContext(struct ScriptContext *ctx, void *cmdTable, void *cmdTableEnd) { u32 i; @@ -28,7 +30,7 @@ u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr) return 1; } -void SetupNativeScript(struct ScriptContext *ctx, u8 (*ptr)(void)) +void SetupNativeScript(struct ScriptContext *ctx, u8 (*ptr)(struct ScriptContext *)) { ctx->mode = 2; ctx->nativePtr = ptr; @@ -47,7 +49,6 @@ void FUN_02038B6C(struct ScriptContext *ctx, int r1) extern void ErrorHandling(void); -#ifdef NONMATCHING u8 RunScriptCommand(struct ScriptContext *ctx) { struct ScriptContext *localCtx = ctx; @@ -62,7 +63,7 @@ u8 RunScriptCommand(struct ScriptContext *ctx) case 2: if (localCtx->nativePtr) { - if (localCtx->nativePtr() == TRUE) + if (localCtx->nativePtr(ctx) == TRUE) localCtx->mode = 1; return TRUE; } @@ -80,10 +81,11 @@ u8 RunScriptCommand(struct ScriptContext *ctx) } cmdCode = ScriptReadHalfword(localCtx); - if ((u32)localCtx->cmdTableEnd >= cmdCode) + u32 cmdTableEnd = (u32)localCtx->cmdTableEnd; + if (cmdCode >= cmdTableEnd) { ErrorHandling(); - localCtx->mode = 0; + localCtx->mode = 0; return FALSE; } @@ -96,73 +98,6 @@ u8 RunScriptCommand(struct ScriptContext *ctx) return TRUE; } -#else -u8 RunScriptCommand(struct ScriptContext *ctx) -{ - __asm { - add r4, r0, #0x0 - ldrb r1, [r4, #0x1] - cmp r1, #0x0 - bne _02038B7E - mov r0, #0x0 - pop {r4, pc} - _02038B7E: - beq _02038B8A - cmp r1, #0x1 - beq _02038BA6 - cmp r1, #0x2 - beq _02038B8E - b _02038BD8 - _02038B8A: - mov r0, #0x0 - pop {r4, pc} - _02038B8E: - ldr r1, [r4, #0x4] - cmp r1, #0x0 - beq _02038BA2 - blx r1 - cmp r0, #0x1 - bne _02038B9E - mov r0, #0x1 - strb r0, [r4, #0x1] - _02038B9E: - mov r0, #0x1 - pop {r4, pc} - _02038BA2: - mov r0, #0x1 - strb r0, [r4, #0x1] - // for some reason it adds a b _02038BA6 here - _02038BA6: - ldr r0, [r4, #0x8] - cmp r0, #0x0 - bne _02038BB2 - mov r0, #0x0 - strb r0, [r4, #0x1] - pop {r4, pc} - _02038BB2: - add r0, r4, #0x0 - bl ScriptReadHalfword - add r1, r0, #0x0 - ldr r0, [r4, #0x60] - cmp r1, r0 - blo _02038BCA - bl ErrorHandling - mov r0, #0x0 - strb r0, [r4, #0x1] - pop {r4, pc} - _02038BCA: - ldr r2, [r4, #0x5c] - lsl r1, r1, #0x2 - ldr r1, [r2, r1] - add r0, r4, #0x0 - blx r1 - cmp r0, #0x1 - bne _02038BA6 - _02038BD8: - mov r0, #0x1 - } -} -#endif u8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr) { @@ -192,10 +127,11 @@ void ScriptJump(struct ScriptContext *ctx, const u8 *ptr) ctx->scriptPtr = ptr; } -void ScriptCall(struct ScriptContext *ctx, const u8 *ptr) +u8 ScriptCall(struct ScriptContext *ctx, const u8 *ptr) { - ScriptPush(ctx, ctx->scriptPtr); + u8 ret = ScriptPush(ctx, ctx->scriptPtr); ctx->scriptPtr = ptr; + return ret; } void ScriptReturn(struct ScriptContext *ctx) -- cgit v1.2.3 From 6ceaabc28c712f82c6100f8288af66774bfdbb82 Mon Sep 17 00:00:00 2001 From: ProjectRevoTPP Date: Tue, 21 Apr 2020 12:15:56 -0400 Subject: make consistent with gen 3's version again --- src/script.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/script.c') diff --git a/src/script.c b/src/script.c index e8b8ee60..0a9a85cf 100644 --- a/src/script.c +++ b/src/script.c @@ -51,47 +51,45 @@ extern void ErrorHandling(void); u8 RunScriptCommand(struct ScriptContext *ctx) { - struct ScriptContext *localCtx = ctx; - - if (localCtx->mode == 0) + if (ctx->mode == 0) return FALSE; - switch (localCtx->mode) + switch (ctx->mode) { case 0: return FALSE; case 2: - if (localCtx->nativePtr) + if (ctx->nativePtr) { - if (localCtx->nativePtr(ctx) == TRUE) - localCtx->mode = 1; + if (ctx->nativePtr(ctx) == TRUE) + ctx->mode = 1; return TRUE; } - localCtx->mode = 1; + ctx->mode = 1; case 1: while (1) { u16 cmdCode; ScrCmdFunc *func; - if (!localCtx->scriptPtr) + if (!ctx->scriptPtr) { - localCtx->mode = 0; + ctx->mode = 0; return FALSE; } - cmdCode = ScriptReadHalfword(localCtx); - u32 cmdTableEnd = (u32)localCtx->cmdTableEnd; + cmdCode = ScriptReadHalfword(ctx); + u32 cmdTableEnd = (u32)ctx->cmdTableEnd; if (cmdCode >= cmdTableEnd) { ErrorHandling(); - localCtx->mode = 0; + ctx->mode = 0; return FALSE; } - func = &localCtx->cmdTable[cmdCode]; + func = &ctx->cmdTable[cmdCode]; - if ((*func)(localCtx) == 1) + if ((*func)(ctx) == 1) break; } } -- cgit v1.2.3