Created
October 27, 2020 02:03
-
-
Save ITotalJustice/f238e62c8e720f4ec52cc572ab8e9d3b to your computer and use it in GitHub Desktop.
parses logs from https://github.com/wheremyfoodat/Gameboy-logs/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DOWNLOAD PARSED HEADERS FROM: https://files.catbox.moe/imeaak.zip | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#define CHUNK_SIZE 10000 | |
typedef uint8_t u8; | |
typedef uint16_t u16; | |
typedef uint32_t u32; | |
typedef uint64_t u64; | |
typedef struct { | |
u8 *A, *F, *B, *C, *D, *E, *H, *L; | |
u16 *SP; | |
u16 *PC; | |
u8 *MEM0, *MEM1, *MEM2, *MEM3; | |
char *data; | |
u64 data_size; | |
u64 data_pos; | |
u64 entry_count; | |
u64 alloc_count; | |
} context_t; | |
static void ctx_realloc(context_t *ctx) { | |
u64 new_size = CHUNK_SIZE + (ctx->alloc_count * CHUNK_SIZE); | |
ctx->A = realloc(ctx->A, new_size); assert(ctx->A); | |
ctx->F = realloc(ctx->F, new_size); assert(ctx->F); | |
ctx->B = realloc(ctx->B, new_size); assert(ctx->B); | |
ctx->C = realloc(ctx->C, new_size); assert(ctx->C); | |
ctx->D = realloc(ctx->D, new_size); assert(ctx->D); | |
ctx->E = realloc(ctx->E, new_size); assert(ctx->E); | |
ctx->H = realloc(ctx->H, new_size); assert(ctx->H); | |
ctx->L = realloc(ctx->L, new_size); assert(ctx->L); | |
ctx->SP = realloc(ctx->SP, new_size * sizeof(u16)); assert(ctx->SP); | |
ctx->PC = realloc(ctx->PC, new_size * sizeof(u16)); assert(ctx->PC); | |
ctx->MEM0 = realloc(ctx->MEM0, new_size); assert(ctx->MEM0); | |
ctx->MEM1 = realloc(ctx->MEM1, new_size); assert(ctx->MEM1); | |
ctx->MEM2 = realloc(ctx->MEM2, new_size); assert(ctx->MEM2); | |
ctx->MEM3 = realloc(ctx->MEM3, new_size); assert(ctx->MEM3); | |
ctx->alloc_count++; | |
} | |
static u8 ctx_fetch_byte(context_t *ctx, int pre) { | |
ctx->data_pos += pre; | |
char buf[5] = "0x"; | |
strncat(buf, ctx->data + ctx->data_pos, 2); | |
ctx->data_pos += 3; | |
return strtoul(buf, NULL, 0x10); | |
} | |
static u16 ctx_fetch_word(context_t *ctx, int pre) { | |
ctx->data_pos += pre; | |
char buf[7] = "0x"; | |
strncat(buf, ctx->data + ctx->data_pos, 4); | |
ctx->data_pos += 5; | |
return strtoul(buf, NULL, 0x10); | |
} | |
static void ctx_dump_byte(FILE *fout, context_t *ctx, u8 *mbyte, const char *name) { | |
fprintf(fout, "\nstatic const unsigned char %s[] = {", name); | |
for (u64 i = 0; i < ctx->entry_count; i++) { | |
fprintf(fout, "0x%02X,", mbyte[i]); | |
} | |
fprintf(fout, "};"); | |
} | |
static void ctx_dump_word(FILE *fout, const context_t *ctx, const u16 *word, const char *name) { | |
fprintf(fout, "\nstatic const unsigned short %s[] = {", name); | |
for (u64 i = 0; i < ctx->entry_count; i++) { | |
fprintf(fout, "0x%04X,", word[i]); | |
} | |
fprintf(fout, "};"); | |
} | |
int main(int argc, char **argv) { | |
context_t ctx = {0}; | |
ctx.SP = NULL; | |
if (argc < 3) { | |
fprintf(stderr, "usage: %s path_to_log.txt output.h\n", argv[0]); | |
exit(-1); | |
} | |
FILE *fp = fopen(argv[1], "rb"); | |
if (!fp) { | |
fprintf(stderr, "failed to open file\n"); | |
exit(-1); | |
} | |
FILE *fout = fopen(argv[2], "wb"); | |
if (!fout) { | |
fprintf(stderr, "failed to open out file\n"); | |
fclose(fp); | |
exit(-1); | |
} | |
fseek(fp, 0, SEEK_END); | |
ctx.data_size = ftell(fp); | |
rewind(fp); | |
ctx.data = malloc(ctx.data_size + 1); | |
if (!fread(ctx.data, ctx.data_size, 1, fp)) { | |
fprintf(stderr, "failed to read file\n"); | |
exit(-1); | |
} | |
fclose(fp); | |
ctx_realloc(&ctx); | |
while (ctx.data_pos < ctx.data_size) { | |
if ((ctx.entry_count % (CHUNK_SIZE - 1)) == 0) { | |
ctx_realloc(&ctx); | |
} | |
ctx.A[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.F[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.B[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.C[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.D[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.E[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.H[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.L[ctx.entry_count] = ctx_fetch_byte(&ctx, 3); | |
ctx.SP[ctx.entry_count] = ctx_fetch_word(&ctx, 4); | |
ctx.PC[ctx.entry_count] = ctx_fetch_word(&ctx, 7); | |
ctx.MEM0[ctx.entry_count] = ctx_fetch_byte(&ctx, 1); | |
ctx.MEM1[ctx.entry_count] = ctx_fetch_byte(&ctx, 0); | |
ctx.MEM2[ctx.entry_count] = ctx_fetch_byte(&ctx, 0); | |
ctx.MEM3[ctx.entry_count] = ctx_fetch_byte(&ctx, 0); | |
// skip to new line | |
while (ctx.data_pos < ctx.data_size) { | |
if (ctx.data[ctx.data_pos++] != '\n') { | |
continue; | |
} | |
break; | |
} | |
ctx.entry_count++; | |
} | |
fprintf(fout, "#pragma once\n\n"); | |
ctx_dump_byte(fout, &ctx, ctx.A, "ARR_A"); | |
ctx_dump_byte(fout, &ctx, ctx.F, "ARR_F"); | |
ctx_dump_byte(fout, &ctx, ctx.B, "ARR_B"); | |
ctx_dump_byte(fout, &ctx, ctx.C, "ARR_C"); | |
ctx_dump_byte(fout, &ctx, ctx.D, "ARR_D"); | |
ctx_dump_byte(fout, &ctx, ctx.E, "ARR_E"); | |
ctx_dump_byte(fout, &ctx, ctx.H, "ARR_H"); | |
ctx_dump_byte(fout, &ctx, ctx.L, "ARR_L"); | |
ctx_dump_word(fout, &ctx, ctx.SP, "ARR_SP"); | |
ctx_dump_word(fout, &ctx, ctx.PC, "ARR_PC"); | |
ctx_dump_byte(fout, &ctx, ctx.MEM0, "ARR_MEM0"); | |
ctx_dump_byte(fout, &ctx, ctx.MEM1, "ARR_MEM1"); | |
ctx_dump_byte(fout, &ctx, ctx.MEM2, "ARR_MEM2"); | |
ctx_dump_byte(fout, &ctx, ctx.MEM3, "ARR_MEM3"); | |
printf("done!\n"); | |
fclose(fout); | |
free(ctx.A); | |
free(ctx.F); | |
free(ctx.B); | |
free(ctx.C); | |
free(ctx.D); | |
free(ctx.E); | |
free(ctx.H); | |
free(ctx.L); | |
free(ctx.SP); | |
free(ctx.PC); | |
free(ctx.MEM0); | |
free(ctx.MEM1); | |
free(ctx.MEM2); | |
free(ctx.MEM3); | |
free(ctx.data); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment