Browse Source

Implement log formatter

master
Forest Belton 1 year ago
parent
commit
9f7778bfd7
8 changed files with 84 additions and 40 deletions
  1. +21
    -0
      include/log.h
  2. +3
    -4
      include/util.h
  3. +19
    -12
      src/boot.c
  4. +5
    -2
      src/cpu.c
  5. +4
    -4
      src/insn.c
  6. +28
    -0
      src/log.c
  7. +3
    -6
      src/main.c
  8. +1
    -12
      src/util.c

+ 21
- 0
include/log.h View File

@ -0,0 +1,21 @@
#ifndef PSXC_LOG_H_
#define PSXC_LOG_H_
#include <stdlib.h>
#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

+ 3
- 4
include/util.h View File

@ -4,13 +4,12 @@
#include <stddef.h>
#include <stdint.h>
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

+ 19
- 12
src/boot.c View File

@ -1,6 +1,7 @@
#include "boot.h"
#include "cpu.h"
#include "insn.h"
#include "log.h"
#include "psx.h"
#include <assert.h>
@ -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);

+ 5
- 2
src/cpu.c View File

@ -1,4 +1,7 @@
#include "cpu.h"
#include "log.h"
#include <stdio.h>
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];
}

+ 4
- 4
src/insn.c View File

@ -1,6 +1,6 @@
#include "insn.h"
#include "log.h"
#include "cpu.h"
#include "util.h"
#include <stddef.h>
#include <stdio.h>
@ -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);
}

+ 28
- 0
src/log.c View File

@ -0,0 +1,28 @@
#include "log.h"
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
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");
}

+ 3
- 6
src/main.c View File

@ -1,20 +1,17 @@
#include "boot.h"
#include "insn.h"
#include "log.h"
#include "util.h"
#include <stdio.h>
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);

+ 1
- 12
src/util.c View File

@ -4,18 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
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;

Loading…
Cancel
Save