From 9f7778bfd740418fb04ceec7d99550a6cbeeadec Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Sat, 26 Jun 2021 18:32:22 -0400 Subject: [PATCH] Implement log formatter --- include/log.h | 21 +++++++++++++++++++++ include/util.h | 7 +++---- src/boot.c | 31 +++++++++++++++++++------------ src/cpu.c | 7 +++++-- src/insn.c | 8 ++++---- src/log.c | 28 ++++++++++++++++++++++++++++ src/main.c | 9 +++------ src/util.c | 13 +------------ 8 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 include/log.h create mode 100644 src/log.c diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..8f89258 --- /dev/null +++ b/include/log.h @@ -0,0 +1,21 @@ +#ifndef PSXC_LOG_H_ +#define PSXC_LOG_H_ + +#include + +#define log(level, fmt, ...) _log(__FILE__, __func__, __LINE__, (level), fmt, ##__VA_ARGS__) + +#define debug(fmt, ...) log("DEBUG", fmt, ##__VA_ARGS__) +#define fatal(fmt, ...) \ + do \ + { \ + log("FATAL", fmt, ##__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } while (0) +#define info(fmt, ...) log("INFO", fmt, ##__VA_ARGS__) + +extern void _log(const char *filename, const char *funcname, int line_num, const char *lvl, const char *fmt, ...); + +extern const char *REG_NAMES[32]; + +#endif diff --git a/include/util.h b/include/util.h index a6f2a7b..ed7b6a1 100644 --- a/include/util.h +++ b/include/util.h @@ -4,13 +4,12 @@ #include #include -typedef struct { +typedef struct +{ size_t size; uint8_t data[]; } byte_arr_t; -void fatal(const char *fmt, ...); - -byte_arr_t *read_file(const char *filepath); +extern byte_arr_t *read_file(const char *filepath); #endif diff --git a/src/boot.c b/src/boot.c index ad2b356..7f8bcff 100644 --- a/src/boot.c +++ b/src/boot.c @@ -1,6 +1,7 @@ #include "boot.h" #include "cpu.h" #include "insn.h" +#include "log.h" #include "psx.h" #include @@ -16,12 +17,21 @@ psx_header_t parse_psx_header(byte_arr_t *prgm) psx_header_t header; // TODO: Convert asserts into recoverable error + debug("Checking header struct size"); assert(sizeof header == 0x38 - 0x10); + + debug("Checking program size is greater than header"); assert(prgm->size >= 0x800); + + debug("Checking magic number in header"); assert(memcmp(MAGIC, prgm->data, sizeof MAGIC) == 0); + memcpy(&header, &prgm->data[sizeof MAGIC], sizeof header); + debug("Checking file size in header matches program size"); assert(header.size_file == prgm->size - 0x800); + + debug("Checking A34h region is zero"); assert(memcmp(RESERVED, &prgm->data[0x38], sizeof RESERVED) == 0); return header; @@ -43,17 +53,16 @@ uint32_t translate_addr(uint32_t addr) void boot_psx_prgm(byte_arr_t *prgm) { - printf("parsing program header\n"); + debug("Parsing program header"); psx_header_t header = parse_psx_header(prgm); - printf("=== header ===\n"); - printf("pc = %08x, gp = %08x\n", header.pc, header.gp); - printf("sp = %08x, fp = %08x\n", header.sp, header.fp); - printf("data = %08x (%u bytes)\n", header.addr_data, header.size_data); - printf("bss = %08x (%u bytes)\n", header.addr_bss, header.size_bss); - printf("dest = %08x\n", header.addr_dest); - printf("size = %08x (%u bytes)\n", header.size_file, header.size_file); + debug("Program header"); + debug("size=%u bytes", header.size_file); + debug("pc=%08x, gp=%08x, sp=%08x, fp=%08x", header.pc, header.gp, header.sp, header.fp); + debug("data=%08x (%u bytes)", header.addr_data, header.size_data); + debug("bss=%08x (%u bytes)", header.addr_bss, header.size_bss); + debug("dest=%08x", header.addr_dest); cpu_t *cpu = malloc(sizeof *cpu); @@ -67,12 +76,10 @@ void boot_psx_prgm(byte_arr_t *prgm) cpu->regs[REG_FP] = header.fp; psx_t psx = {header, cpu}; - const int max_insns = 1; - for (int i = 0; i < max_insns; i++) + while (1) { - printf("=== insn ===\n"); - printf("pc = %08x\n", psx.cpu->pc); + debug("Executing instruction at %08x", psx.cpu->pc); const uint32_t insn = cpu_read32(cpu, psx.cpu->pc); insn_execute(cpu, insn); diff --git a/src/cpu.c b/src/cpu.c index a097ddc..3076219 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -1,4 +1,7 @@ #include "cpu.h" +#include "log.h" + +#include static uint32_t translate_addr(uint32_t addr) { @@ -16,14 +19,14 @@ static uint32_t translate_addr(uint32_t addr) void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x) { - printf("WRITE: %08x to %08x\n", x, addr); + debug("Write [%08x] = %08x", addr, x); addr = translate_addr(addr); cpu->main_ram[addr] = x; } uint32_t cpu_read32(cpu_t *cpu, uint32_t addr) { - printf("READ: %08x\n", addr); + debug("Read [%08x]", addr); addr = translate_addr(addr); return cpu->main_ram[addr]; } diff --git a/src/insn.c b/src/insn.c index cfd9d67..876dde4 100644 --- a/src/insn.c +++ b/src/insn.c @@ -1,6 +1,6 @@ #include "insn.h" +#include "log.h" #include "cpu.h" -#include "util.h" #include #include @@ -36,7 +36,7 @@ void insn_execute(cpu_t *cpu, uint32_t insn) if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) { - fatal("unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn, + fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn, op, op2); } @@ -45,7 +45,7 @@ void insn_execute(cpu_t *cpu, uint32_t insn) if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) { - fatal("unsupported instruction: insn=%08x, op=%02x", insn, op); + fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op); } primary_insn_handler[op](cpu, insn); @@ -57,6 +57,6 @@ void insn_lw(cpu_t *cpu, uint32_t insn) const uint8_t rt = RT(insn); const uint16_t imm = IMM(insn); - printf("LW R%u, [R%u + %x]\n", rt, rs, imm); + debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm); cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm); } diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..3552735 --- /dev/null +++ b/src/log.c @@ -0,0 +1,28 @@ +#include "log.h" + +#include +#include +#include + +const char *REG_NAMES[32] = { + "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", + "R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20", "R21", "R22", "R23", + "R24", "R25", "R26", "R27", "GP", "SP", "FP", "RA"}; + +void _log(const char *filename, const char *funcname, int line_num, const char *level, const char *fmt, ...) +{ + time_t now; + char fmt_time[sizeof "2011-10-08T07:07:09Z"]; + + time(&now); + strftime(&fmt_time[0], sizeof fmt_time, "%Y-%m-%dT%H:%M:%S", gmtime(&now)); + + printf("%s,%s,%s,%d,%s,\"", fmt_time, level, filename, line_num, funcname); + + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + printf("\"\n"); +} diff --git a/src/main.c b/src/main.c index 4eb2085..07914ec 100644 --- a/src/main.c +++ b/src/main.c @@ -1,20 +1,17 @@ #include "boot.h" #include "insn.h" +#include "log.h" #include "util.h" #include int main() { - // uint32_t x = 0xffcdefab; - // const insn_t insn = *(insn_t *)&x; - // printf("%u", insn.insn_reg.op); - - printf("loading boot rom\n"); + info("Loading program"); byte_arr_t *prgm = read_file("boot.rom"); if (prgm == NULL) { - fatal("couldn't load boot.rom"); + fatal("Couldn't load program"); } boot_psx_prgm(prgm); diff --git a/src/util.c b/src/util.c index ddd9559..3ba8379 100644 --- a/src/util.c +++ b/src/util.c @@ -4,18 +4,6 @@ #include #include -void fatal(const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); - va_end(args); - - exit(EXIT_FAILURE); -} - byte_arr_t *read_file(const char *filepath) { FILE *fp = fopen(filepath, "rb"); @@ -23,6 +11,7 @@ byte_arr_t *read_file(const char *filepath) if (fp == NULL) { + // TODO: Log correctly fprintf(stderr, "%s: ", filepath); perror("could not open"); goto end;