Skip to content

Commit

Permalink
decode to assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
EmperorPenguin18 committed Jul 16, 2024
1 parent 53153b5 commit 528efe9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: sudo ./build-devkit.sh
working-directory: ./buildscripts-devkitARM_r63
- name: add to path
run: echo "PATH=/opt/devkitpro/tools/bin:$PATH" >> $GITHUB_ENV
run: echo "PATH=/opt/devkitpro/devkitARM/bin:$PATH" >> $GITHUB_ENV
- name: mkdir
run: mkdir build
- name: configure
Expand Down
10 changes: 4 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,17 @@ set(CURRENT_COMPILER "NATIVE")
set(NATIVE_C_COMPILER CMAKE_C_COMPILER)
set(HOST_C_COMPILER "")
set(CROSSCOMPILING ${CMAKE_CROSSCOMPILING})
set(data "data.s")
set(flag "s")
if (NOT HOST_SYSTEM_NAME MATCHES ${CMAKE_SYSTEM_NAME})
set(CROSSCOMPILING true)
if (HOST_SYSTEM_NAME MATCHES Linux)
# Nothing for now
elseif (HOST_SYSTEM_NAME MATCHES Emscripten)
set(HOST_C_COMPILER "emcc")
set(data "data.c")
set(flag "c")
elseif (HOST_SYSTEM_NAME MATCHES Windows)
set(HOST_C_COMPILER "x86_64-w64-mingw32-gcc")
elseif (HOST_SYSTEM_NAME MATCHES GBA)
set(HOST_C_COMPILER "arm-none-eabi-gcc")
set(HOST_C_FLAGS "-mthumb -DGBA")
set(data "data.c")
set(flag "c")
endif()
endif()

Expand All @@ -67,6 +61,7 @@ macro(use_host_compiler)
set(CMAKE_SYSTEM_NAME ${HOST_SYSTEM_NAME})
#set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
set(CMAKE_C_COMPILER ${HOST_C_COMPILER})
set(CMAKE_ASM_COMPILER ${HOST_C_COMPILER})
set(CMAKE_C_FLAGS ${HOST_C_FLAGS})
set(CURRENT_COMPILER "HOST" CACHE STRING "Which compiler we are using." FORCE)
endif()
Expand All @@ -80,6 +75,7 @@ macro(use_native_compiler)
set(CMAKE_SYSTEM_NAME ${CMAKE_HOST_SYSTEM_NAME})
#set(CMAKE_SYSTEM_PROCESSOR ${NATIVE_SYSTEM_PROCESSOR})
set(CMAKE_C_COMPILER ${NATIVE_C_COMPILER})
set(CMAKE_ASM_COMPILER ${NATIVE_C_COMPILER})
set(CMAKE_C_FLAGS ${NATIVE_C_FLAGS})
set(CURRENT_COMPILER "NATIVE" CACHE STRING "Which compiler we are using." FORCE)
endif()
Expand All @@ -99,6 +95,8 @@ if (NOT "${resource_path}" STREQUAL "")
endif()

# Generate data array
set(data "data.s")
set(flag "s")
add_custom_command(
OUTPUT ${data}
COMMAND decoder
Expand Down
177 changes: 58 additions & 119 deletions decoder/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ typedef struct file_table {

typedef enum mode {
C,
ASM,
LLVM
ASM
} mode;

#define B_IN_SZ sizeof(size_t)/sizeof(unsigned char)
Expand Down Expand Up @@ -90,33 +89,64 @@ static int args(const int argc, char** argv, mode* m) {
*m = C;
} else if (argv[i][j] == 's') {
*m = ASM;
} else if (argv[i][j] == 'l') {
*m = LLVM;
} else break;
}
return i;
}

#define SWITCH_MODE(m, c, s) \
switch (m) { \
case C: \
c; \
break; \
case ASM: \
s; \
break; \
default: \
break; \
}

#define WRITE_CHECK(text, out, err) \
err = fputs(text, out); \
CHECK_ERROR(err > 0, "couldn't write to file");

#define LINE_BREAK(lc, out) \
lc++; \
#define PRINT_BYTE(file, byte, m, lc) \
{ \
if (lc % 12 == 0) { \
fprintf(out, "\n "); \
SWITCH_MODE(m, \
fprintf(out, "\n\t0x%02x, ", byte), \
fprintf(out, "\n\t.byte 0x%02x", byte) \
); \
lc = 0; \
} else { \
SWITCH_MODE(m, \
fprintf(file, "0x%02x, ", byte), \
fprintf(file, ", 0x%02x", byte) \
); \
} \
lc++; \
}

#define PRINT_C_BYTE(file, byte) \
fprintf(file, "0x%02x, ", byte);

static int mode_c(file_table* ft) {
FILE* out = fopen("data.c", "w");
#define OPEN_FILE(m, out) \
SWITCH_MODE(m, \
out = fopen("data.c", "w"), \
out = fopen("data.s", "w") \
); \
CHECK_ERROR(out, "output failed to open");

int main(int argc, char** argv) {
mode m = 0;
const int n = args(argc, argv, &m);
file_table* ft = sort(argc-n, argv+n);
CHECK_ERROR(ft, "file table failed to initialize");
FILE* out; OPEN_FILE(m, out);
int err;
WRITE_CHECK("extern const unsigned char data_array[];\n\nconst unsigned char data_array[] = {\n ",
out, err);
SWITCH_MODE(m,
WRITE_CHECK("extern const unsigned char data_array[];\n\nconst unsigned char data_array[] = {",
out, err),
WRITE_CHECK("\t.globl\tdata_array\n\t.data\ndata_array:",
out, err)
);
int lc = 0;
for (int i = 0; ft[i].name; i++) {
size_t size = 0;
Expand All @@ -126,122 +156,31 @@ static int mode_c(file_table* ft) {
assert(size == 0);
continue;
}
for (size_t j = 0; j < size; j++) {
PRINT_C_BYTE(out, buf[j]);
LINE_BREAK(lc, out);
}
for (size_t j = 0; j < size; j++) PRINT_BYTE(out, buf[j], m, lc);
if (size % 4 != 0) { // padding for alignment
for (size_t j = 0; j < 4 - (size % 4); j++) {
PRINT_C_BYTE(out, 0);
LINE_BREAK(lc, out);
}
for (size_t j = 0; j < 4 - (size % 4); j++) PRINT_BYTE(out, 0, m, lc);
}
free(buf);
}
WRITE_CHECK("0x0\n};\n\nextern const unsigned char data_info[];\n\nconst unsigned char data_info[] = {\n ",
out, err);
SWITCH_MODE(m,
WRITE_CHECK("\n};\n\nextern const unsigned char data_info[];\n\nconst unsigned char data_info[] = {",
out, err),
WRITE_CHECK("\n\t.globl\tdata_info\ndata_info:",
out, err)
);
lc = 0;
for (int i = 0; ft[i].name; i++) {
for (int j = 0; j < strlen(ft[i].name)+1; j++) { // +1 for null byte
PRINT_C_BYTE(out, ft[i].name[j]);
LINE_BREAK(lc, out);
}
for (int j = 0; j < B_IN_SZ; j++) {
PRINT_C_BYTE(out, (uint8_t)((ft[i].size >> (j*8)) & 0xff) );
LINE_BREAK(lc, out);
}
}
WRITE_CHECK("0x0\n};\n", out, err);
CHECK_ERROR(!fclose(out), "couldn't close file");
return 0;
}

#define PRINT_S_BYTE(file, byte) \
fprintf(file, "\t.byte\t%d\n", (char)byte);

static int mode_s(file_table* ft) {
FILE* out = fopen("data.s", "w");
CHECK_ERROR(out, "output failed to open");
int err;
WRITE_CHECK("\t.text\n\t.globl\tdata_array\n\t.section\t.rodata\n\t.align 32\n\t.type\tdata_array, @object\n",
//WRITE_CHECK("\t.text\n\t.globl\tdata_array\n\t.section\t.rodata\n\t.align 32\n\t.type\tdata_array, @object\n\t.size\tdata_array, ",
out, err);
size_t total_size = 0;
size_t info_size = 0;
unsigned char** buffers = NULL;
for (int i = 0; ft[i].name; i++) {
size_t size = 0;
REALLOC(buffers, (i+1)*sizeof(unsigned char*));
buffers[i] = extension_handler(ft[i].name, &size);
ft[i].size = size;
if (!buffers[i]) {
assert(size == 0);
continue;
}
total_size += size + (ft[i].size % 4 != 0) ? 4 - (size % 4) : 0;
info_size += strlen(ft[i].name)+1 + B_IN_SZ;
}
//fprintf(out, "%ld\ndata_array:\n", total_size);
fprintf(out, "data_array:\n");
for (int i = 0; ft[i].name; i++) {
for (size_t j = 0; j < ft[i].size; j++) PRINT_S_BYTE(out, buffers[i][j]);
if (ft[i].size % 4 != 0) { // padding for alignment
for (size_t j = 0; j < 4 - (ft[i].size % 4); j++) PRINT_S_BYTE(out, 0);
}
free(buffers[i]);
}
free(buffers);
WRITE_CHECK("\t.globl\tdata_info\n\t.align 32\n\t.type\tdata_info, @object\n",
//WRITE_CHECK("\t.globl\tdata_info\n\t.align 32\n\t.type\tdata_info, @object\n\t.size\tdata_info, ",
out, err);
//fprintf(out, "%ld\ndata_info:\n", info_size);
fprintf(out, "data_info:\n");
for (int i = 0; ft[i].name; i++) {
for (int j = 0; j < strlen(ft[i].name)+1; j++) { // +1 for null byte
PRINT_S_BYTE(out, ft[i].name[j]);
PRINT_BYTE(out, ft[i].name[j], m, lc);
}
for (int j = 0; j < B_IN_SZ; j++)
PRINT_S_BYTE(out, (uint8_t)((ft[i].size >> (j*8)) & 0xff) );
PRINT_BYTE(out, (uint8_t)((ft[i].size >> (j*8)) & 0xff), m, lc);
}
PRINT_S_BYTE(out, 0);
SWITCH_MODE(m,
WRITE_CHECK("0x0\n};\n", out, err),
WRITE_CHECK(", 0\n", out, err)
);
CHECK_ERROR(!fclose(out), "couldn't close file");
return 0;
}

#define PRINT_L_BYTE(file, byte) \
fprintf(file, "%d", byte);

static int mode_l(file_table* ft) {
FILE* out = fopen("data.ll", "w");
CHECK_ERROR(out, "output failed to open");
int err = fputs("", out);
CHECK_ERROR(err > 0, "couldn't write to file");
//
err = fputs("", out);
CHECK_ERROR(err > 0, "couldn't write to file");
//
CHECK_ERROR(!fclose(out), "couldn't close file");
return 0;
}

int main(int argc, char** argv) {
mode m = 0;
const int n = args(argc, argv, &m);
file_table* ft = sort(argc-n, argv+n);
CHECK_ERROR(ft, "file table failed to initialize");
switch (m) {
case C:
if (mode_c(ft) != 0) return EXIT_FAILURE;
break;
case ASM:
if (mode_s(ft) != 0) return EXIT_FAILURE;
break;
case LLVM:
if (mode_l(ft) != 0) return EXIT_FAILURE;
break;
default:
break;
}
free(ft);
return EXIT_SUCCESS;
}
12 changes: 8 additions & 4 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ elseif(CMAKE_SYSTEM_NAME MATCHES Emscripten)
target_link_options(${title} PRIVATE "-sFULL_ES3")
target_link_options(${title} PRIVATE "-sTOTAL_MEMORY=67108864")
#target_link_options(${title} PRIVATE "-sALLOW_MEMORY_GROWTH")
file(READ "${CMAKE_BINARY_DIR}/data.s" TEXT)
string(REPLACE "data_array:" "\t.size\tdata_array, 0\ndata_array:" TEXT "${TEXT}")
string(REPLACE "data_info:" "\t.size\tdata_info, 0\ndata_info:" TEXT "${TEXT}")
file(WRITE "${CMAKE_BINARY_DIR}/data.s" "${TEXT}")
elseif(CMAKE_SYSTEM_NAME MATCHES Windows)
set_target_properties(${title} PROPERTIES SUFFIX ".exe")
target_link_options(${title} PRIVATE "-static")
Expand All @@ -57,11 +61,11 @@ elseif(CMAKE_SYSTEM_NAME MATCHES Darwin)
target_link_libraries(${title} PRIVATE "-framework OpenGL")
target_link_libraries(${title} PRIVATE "-framework AudioToolbox")
elseif(CMAKE_SYSTEM_NAME MATCHES GBA)
add_custom_target(${title}.gba ALL
COMMAND arm-none-eabi-objcopy -O binary ${title} ${title}.gba
DEPENDS ${title}
set_target_properties(${title} PROPERTIES SUFFIX ".gba")
add_custom_command(TARGET ${title} POST_BUILD
COMMAND arm-none-eabi-objcopy -O binary ${title}.gba ${title}.gba
)
add_custom_command(TARGET ${title}.gba POST_BUILD
add_custom_command(TARGET ${title} POST_BUILD
COMMAND gbafix ${title}.gba
)
endif()
Expand Down
2 changes: 2 additions & 0 deletions engine/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <minilua.h>

#ifndef GBA
int tidal_create(lua_State*);
int tidal_set_size(lua_State*);
int tidal_set_sprite(lua_State*);
Expand Down Expand Up @@ -36,5 +37,6 @@ static const struct luaL_Reg actions[] = {
{"quit", tidal_quit},
{NULL, NULL}
};
#endif //GBA

#endif

0 comments on commit 528efe9

Please sign in to comment.