From 9afb777ca2b07a50e129fa43657f991bb1041c5e Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Sat, 2 Jul 2016 02:10:14 -0400 Subject: [PATCH] Backend work. * Get some bits in place to implement stack instructions in the VM. * Some functions we provide an implementation of are in ctrulib or newlib and therefore uneeded by loader --- external/loader/source/interp.c | 1 + external/loader/source/memory.c | 4 +++- external/loader/source/memory.h | 2 +- external/loader/source/patcher.c | 1 + source/interp.c | 38 ++++++++++++++++++++++++++------ source/main.c | 2 +- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/external/loader/source/interp.c b/external/loader/source/interp.c index b36ef1a..59aa1f3 100644 --- a/external/loader/source/interp.c +++ b/external/loader/source/interp.c @@ -3,6 +3,7 @@ // so it's temporarliy #if'd 0. #include <3ds.h> #include "patcher.h" +#include "exheader.h" #include "fsldr.h" #include "internal.h" #include "memory.h" diff --git a/external/loader/source/memory.c b/external/loader/source/memory.c index d0cb84d..82c07f9 100644 --- a/external/loader/source/memory.c +++ b/external/loader/source/memory.c @@ -1,4 +1,5 @@ #include <3ds.h> +#include #include "patcher.h" #include "fsldr.h" #include "internal.h" @@ -9,7 +10,7 @@ #endif #include "../../../source/config.h" #include "../../../source/patch_format.h" - +/* int memcmp(const void *buf1, const void *buf2, u32 size) { @@ -24,6 +25,7 @@ memcmp(const void *buf1, const void *buf2, u32 size) return 0; } +*/ // Quick Search algorithm, adapted from // http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 diff --git a/external/loader/source/memory.h b/external/loader/source/memory.h index c7b77f3..f0c81e5 100644 --- a/external/loader/source/memory.h +++ b/external/loader/source/memory.h @@ -1,7 +1,7 @@ #ifndef __MEMORY_H #define __MEMORY_H -int memcmp(const void *buf1, const void *buf2, u32 size); +//int memcmp(const void *buf1, const void *buf2, u32 size); u8 *memfind(u8 *startPos, u32 size, const void *pattern, u32 patternSize); u32 patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, int offset, const void *replace, u32 repSize, u32 count); size_t strnlen(const char *string, size_t maxlen); diff --git a/external/loader/source/patcher.c b/external/loader/source/patcher.c index c1a14c0..68a9659 100644 --- a/external/loader/source/patcher.c +++ b/external/loader/source/patcher.c @@ -1,4 +1,5 @@ #include <3ds.h> +#include #include "patcher.h" #include "exheader.h" #include "fsldr.h" diff --git a/source/interp.c b/source/interp.c index d936c42..855d21f 100644 --- a/source/interp.c +++ b/source/interp.c @@ -1,11 +1,15 @@ #include #include -#ifndef LOADER #include "std/unused.h" -#include "std/memory.h" -#include "firm/firm.h" -#include "config.h" -#include "common.h" + +#ifndef LOADER + #include "std/memory.h" + #include "firm/firm.h" + #include "config.h" + #include "common.h" + #include "firm/fcram.h" +#else + #include #endif #define OP_NOP 0x00 @@ -72,8 +76,15 @@ static const char hexDigits[] = "0123456789ABCDEF"; int is_n3ds = 1; // TODO - We don't really need to care, but it should still work from loader #endif +#define STACK_SIZE 4096 +#ifdef LOADER + static uint8_t stack_glob[STACK_SIZE]; +#else + static uint8_t *stack_glob = NULL; +#endif + int -exec_bytecode(uint8_t *bytecode, uint16_t ver, uint32_t len, int debug) +exec_bytecode(uint8_t *bytecode, uint32_t len, uint8_t* stack, uint32_t stack_size, uint16_t ver, int debug) { if (!init_bytecode) { #ifndef LOADER @@ -142,6 +153,10 @@ exec_bytecode(uint8_t *bytecode, uint16_t ver, uint32_t len, int debug) init_bytecode = 1; } + memset(stack, 0, stack_size); // Clear stack. + + _UNUSED size_t top = stack_size - 1; + #ifdef LOADER size_t set_mode = 18; #else @@ -494,6 +509,9 @@ exec_bytecode(uint8_t *bytecode, uint16_t ver, uint32_t len, int debug) } found = gt = lt = eq = 0; + memset(stack, 0, stack_size); // Clear stack. + top = stack_size - 1; + bytecode = code + 1; #ifndef LOADER set_mode = 3; @@ -697,5 +715,11 @@ execb(char *filename, int build_cache) debug = 1; } - return exec_bytecode(patch_mem, ver, patch_len, debug); +#ifndef LOADER + if (stack_glob == NULL) { + stack_glob = static_allocate(STACK_SIZE); + } +#endif + + return exec_bytecode(patch_mem, patch_len, stack_glob, STACK_SIZE, ver, debug); } diff --git a/source/main.c b/source/main.c index a32a418..9a5e5c7 100644 --- a/source/main.c +++ b/source/main.c @@ -24,7 +24,7 @@ main(int argc, char** argv) screen_init(); clear_bg(); load_bg_top(PATH_BITS "/top.bin"); - load_bg_bottom(PATH_BITS "/bottom.bin"); // This is basically a menuhax splash (90deg rotated RGB8 pixel data) + load_bg_bottom(PATH_BITS "/bottom.bin"); // This is basically a menuhax splash (90deg rotated BGR8 pixel data) clear_disp(TOP_SCREEN); clear_disp(BOTTOM_SCREEN); -- 2.39.5