diff --git a/.vscode/settings.json b/.vscode/settings.json index cdb613b..5c050d5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,4 @@ { - "files.associations": { - "test.h": "c" - } + "C_Cpp.default.intelliSenseMode": "clang-x64", + "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 80}" } \ No newline at end of file diff --git a/include/boot.h b/include/boot.h index f80ffd4..d90af68 100644 --- a/include/boot.h +++ b/include/boot.h @@ -3,8 +3,7 @@ #include "util.h" -typedef struct __attribute__((packed)) -{ +typedef struct __attribute__((packed)) { uint32_t pc; uint32_t gp; uint32_t addr_dest; diff --git a/include/cpu.h b/include/cpu.h index 8f0933f..564dc5c 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -3,21 +3,20 @@ #include -#define REG_GP 28 // Global pointer -#define REG_SP 29 // Stack pointer -#define REG_FP 30 // Frame pointer +#define REG_GP 28 // Global pointer +#define REG_SP 29 // Stack pointer +#define REG_FP 30 // Frame pointer #define MAIN_RAM_SIZE 0x200000 #define SCRATCHPAD_BASE 0x1F800000 #define SCRATCHPAD_END 0x1F8003FF #define SCRATCHPAD_SIZE (SCRATCHPAD_END - SCRATCHPAD_BASE + 1) -typedef struct -{ - uint32_t regs[32]; - uint32_t pc; - uint32_t main_ram[MAIN_RAM_SIZE]; - uint32_t sratchpad_ram[SCRATCHPAD_SIZE]; +typedef struct { + uint32_t regs[32]; + uint32_t pc; + uint32_t main_ram[MAIN_RAM_SIZE]; + uint32_t sratchpad_ram[SCRATCHPAD_SIZE]; } cpu_t; void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x); diff --git a/include/insn.h b/include/insn.h index 1f0037b..4475c63 100644 --- a/include/insn.h +++ b/include/insn.h @@ -1,84 +1,82 @@ #ifndef PSXC_INSN_H_ #define PSXC_INSN_H_ -#include "cpu.h" - #include -typedef enum -{ - SPECIAL = 0x00, - BCONDZ = 0x01, - J = 0x02, - JAL = 0x03, - BEQ = 0x04, - BNE = 0x05, - BLEZ = 0x06, - BGTZ = 0x07, - ADDI = 0x08, - ADDIU = 0x09, - SLTI = 0x0a, - SLTIU = 0x0b, - ANDI = 0x0c, - ORI = 0x0d, - XORI = 0x0e, - LUI = 0x0f, - COP0 = 0x10, - COP1 = 0x11, - COP2 = 0x12, - COP3 = 0x13, - LB = 0x20, - LH = 0x21, - LWL = 0x22, - LW = 0x23, - LBU = 0x24, - LHU = 0x25, - LWR = 0x26, - SB = 0x28, - SH = 0x29, - SWL = 0x2a, - SW = 0x2b, - SWR = 0x2e, - LWC0 = 0x30, - LWC1 = 0x31, - LWC2 = 0x32, - LWC3 = 0x33, - SWC0 = 0x38, - SWC1 = 0x39, - SWC2 = 0x3a, - SWC3 = 0x3b, +#include "cpu.h" + +typedef enum { + SPECIAL = 0x00, + BCONDZ = 0x01, + J = 0x02, + JAL = 0x03, + BEQ = 0x04, + BNE = 0x05, + BLEZ = 0x06, + BGTZ = 0x07, + ADDI = 0x08, + ADDIU = 0x09, + SLTI = 0x0a, + SLTIU = 0x0b, + ANDI = 0x0c, + ORI = 0x0d, + XORI = 0x0e, + LUI = 0x0f, + COP0 = 0x10, + COP1 = 0x11, + COP2 = 0x12, + COP3 = 0x13, + LB = 0x20, + LH = 0x21, + LWL = 0x22, + LW = 0x23, + LBU = 0x24, + LHU = 0x25, + LWR = 0x26, + SB = 0x28, + SH = 0x29, + SWL = 0x2a, + SW = 0x2b, + SWR = 0x2e, + LWC0 = 0x30, + LWC1 = 0x31, + LWC2 = 0x32, + LWC3 = 0x33, + SWC0 = 0x38, + SWC1 = 0x39, + SWC2 = 0x3a, + SWC3 = 0x3b, } op_primary_t; -typedef enum -{ - SLL = 0x00, - SRL = 0x02, - SRA = 0x03, - SLLV = 0x04, - SRLV = 0x06, - SRAV = 0x07, - JR = 0x08, - JALR = 0x09, - SYSCALL = 0x0c, - BREAK = 0x0d, - MFHI = 0x10, - MTHI = 0x11, - MFLO = 0x12, - MTLO = 0x13, - MULT = 0x18, - MULTU = 0x19, - DIV = 0x1a, - DIVU = 0x1b, - ADD = 0x20, - ADDU = 0x21, - SUB = 0x22, - SUBU = 0x23, - AND = 0x24, - OR = 0x25, - XOR = 0x26, - NOR = 0x27, - SLT = 0x2a, - SLTU = 0x2b, +typedef enum { + SLL = 0x00, + SRL = 0x02, + SRA = 0x03, + SLLV = 0x04, + SRLV = 0x06, + SRAV = 0x07, + JR = 0x08, + JALR = 0x09, + SYSCALL = 0x0c, + BREAK = 0x0d, + MFHI = 0x10, + MTHI = 0x11, + MFLO = 0x12, + MTLO = 0x13, + MULT = 0x18, + MULTU = 0x19, + DIV = 0x1a, + DIVU = 0x1b, + ADD = 0x20, + ADDU = 0x21, + SUB = 0x22, + SUBU = 0x23, + AND = 0x24, + OR = 0x25, + XOR = 0x26, + NOR = 0x27, + SLT = 0x2a, + SLTU = 0x2b, } op_secondary_t; typedef void (*cpu_insn_handler)(cpu_t *, uint32_t); diff --git a/include/log.h b/include/log.h index 8f89258..2793bab 100644 --- a/include/log.h +++ b/include/log.h @@ -3,18 +3,19 @@ #include -#define log(level, fmt, ...) _log(__FILE__, __func__, __LINE__, (level), fmt, ##__VA_ARGS__) +#define log(level, fmt, ...) \ + _log(__FILE__, __func__, __LINE__, (level), fmt, ##__VA_ARGS__) #define debug(fmt, ...) log("DEBUG", fmt, ##__VA_ARGS__) #define fatal(fmt, ...) \ - do \ - { \ + 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 void _log(const char *filename, const char *funcname, int line_num, + const char *lvl, const char *fmt, ...); extern const char *REG_NAMES[32]; diff --git a/include/psx.h b/include/psx.h index 5c63032..30df555 100644 --- a/include/psx.h +++ b/include/psx.h @@ -4,8 +4,7 @@ #include "boot.h" #include "cpu.h" -typedef struct -{ +typedef struct { psx_header_t header; cpu_t *cpu; } psx_t; diff --git a/include/util.h b/include/util.h index ed7b6a1..36f746a 100644 --- a/include/util.h +++ b/include/util.h @@ -4,10 +4,9 @@ #include #include -typedef struct -{ - size_t size; - uint8_t data[]; +typedef struct { + size_t size; + uint8_t data[]; } byte_arr_t; extern byte_arr_t *read_file(const char *filepath); diff --git a/src/boot.c b/src/boot.c index 7f8bcff..3001c70 100644 --- a/src/boot.c +++ b/src/boot.c @@ -1,19 +1,19 @@ #include "boot.h" -#include "cpu.h" -#include "insn.h" -#include "log.h" -#include "psx.h" #include -#include #include +#include #include +#include "cpu.h" +#include "insn.h" +#include "log.h" +#include "psx.h" + static const uint8_t MAGIC[0x10] = "PS-X EXE"; static const uint8_t RESERVED[0x10] = {0}; -psx_header_t parse_psx_header(byte_arr_t *prgm) -{ +psx_header_t parse_psx_header(byte_arr_t *prgm) { psx_header_t header; // TODO: Convert asserts into recoverable error @@ -37,29 +37,25 @@ psx_header_t parse_psx_header(byte_arr_t *prgm) return header; } -uint32_t translate_addr(uint32_t addr) -{ - if (addr >= 0xA0000000) - { +uint32_t translate_addr(uint32_t addr) { + if (addr >= 0xA0000000) { addr = addr - 0xA0000000; - } - else if (addr >= 0x80000000) - { + } else if (addr >= 0x80000000) { addr = addr - 0x80000000; } return addr; } -void boot_psx_prgm(byte_arr_t *prgm) -{ +void boot_psx_prgm(byte_arr_t *prgm) { debug("Parsing program header"); psx_header_t header = parse_psx_header(prgm); 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("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); @@ -77,8 +73,7 @@ void boot_psx_prgm(byte_arr_t *prgm) psx_t psx = {header, cpu}; - while (1) - { + while (1) { debug("Executing instruction at %08x", psx.cpu->pc); const uint32_t insn = cpu_read32(cpu, psx.cpu->pc); diff --git a/src/cpu.c b/src/cpu.c index 3076219..87d2baa 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -1,31 +1,26 @@ #include "cpu.h" -#include "log.h" #include -static uint32_t translate_addr(uint32_t addr) -{ - if (addr >= 0xA0000000) - { +#include "log.h" + +static uint32_t translate_addr(uint32_t addr) { + if (addr >= 0xA0000000) { addr = addr - 0xA0000000; - } - else if (addr >= 0x80000000) - { + } else if (addr >= 0x80000000) { addr = addr - 0x80000000; } return addr; } -void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x) -{ +void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x) { 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) -{ +uint32_t cpu_read32(cpu_t *cpu, uint32_t 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 876dde4..51b9707 100644 --- a/src/insn.c +++ b/src/insn.c @@ -1,10 +1,11 @@ #include "insn.h" -#include "log.h" -#include "cpu.h" #include #include +#include "cpu.h" +#include "log.h" + #define TABLE_SIZE 0x34 void insn_lw(cpu_t *cpu, uint32_t insn); @@ -26,37 +27,32 @@ static cpu_insn_handler secondary_insn_handler[TABLE_SIZE] = {NULL}; #define OP2(insn) BITS(insn, 0, 6) #define IMM(insn) BITS(insn, 0, 16) -void insn_execute(cpu_t *cpu, uint32_t insn) -{ - const op_primary_t op = OP(insn); +void insn_execute(cpu_t *cpu, uint32_t insn) { + const op_primary_t op = OP(insn); - if (op == SPECIAL) - { - const op_secondary_t op2 = OP2(insn); + if (op == SPECIAL) { + const op_secondary_t op2 = OP2(insn); - if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) - { - fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn, - op, op2); - } + if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) { + fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn, + op, op2); + } - secondary_insn_handler[op2](cpu, insn); - } + secondary_insn_handler[op2](cpu, insn); + } - if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) - { - fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op); - } + if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) { + fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op); + } - primary_insn_handler[op](cpu, insn); + primary_insn_handler[op](cpu, insn); } -void insn_lw(cpu_t *cpu, uint32_t insn) -{ - const uint8_t rs = RS(insn); - const uint8_t rt = RT(insn); - const uint16_t imm = IMM(insn); +void insn_lw(cpu_t *cpu, uint32_t insn) { + const uint8_t rs = RS(insn); + const uint8_t rt = RT(insn); + const uint16_t imm = IMM(insn); - debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm); - cpu->regs[rt] = cpu_read32(cpu, cpu->regs[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 index 3552735..ae91257 100644 --- a/src/log.c +++ b/src/log.c @@ -5,12 +5,12 @@ #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"}; + "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, ...) -{ +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"]; diff --git a/src/main.c b/src/main.c index 07914ec..df95552 100644 --- a/src/main.c +++ b/src/main.c @@ -1,20 +1,18 @@ +#include + #include "boot.h" #include "insn.h" #include "log.h" #include "util.h" -#include - -int main() -{ - info("Loading program"); - byte_arr_t *prgm = read_file("boot.rom"); - if (prgm == NULL) - { - fatal("Couldn't load program"); - } +int main() { + info("Loading program"); + byte_arr_t *prgm = read_file("boot.rom"); + if (prgm == NULL) { + fatal("Couldn't load program"); + } - boot_psx_prgm(prgm); + boot_psx_prgm(prgm); - return 0; + return 0; } diff --git a/src/util.c b/src/util.c index 3ba8379..f5707d0 100644 --- a/src/util.c +++ b/src/util.c @@ -4,41 +4,36 @@ #include #include -byte_arr_t *read_file(const char *filepath) -{ - FILE *fp = fopen(filepath, "rb"); - byte_arr_t *arr = NULL; - - if (fp == NULL) - { - // TODO: Log correctly - fprintf(stderr, "%s: ", filepath); - perror("could not open"); - goto end; - } - - fseek(fp, 0, SEEK_END); - const long size = ftell(fp); - if (size < 0) - { - goto end; - } - - arr = malloc(sizeof *arr + size); - if (arr == NULL) - { - goto end; - } - - arr->size = size; - - rewind(fp); - fread(arr->data, 1, size, fp); +byte_arr_t *read_file(const char *filepath) { + FILE *fp = fopen(filepath, "rb"); + byte_arr_t *arr = NULL; + + if (fp == NULL) { + // TODO: Log correctly + fprintf(stderr, "%s: ", filepath); + perror("could not open"); + goto end; + } + + fseek(fp, 0, SEEK_END); + const long size = ftell(fp); + if (size < 0) { + goto end; + } + + arr = malloc(sizeof *arr + size); + if (arr == NULL) { + goto end; + } + + arr->size = size; + + rewind(fp); + fread(arr->data, 1, size, fp); end: - if (fp != NULL) - { - fclose(fp); - } - return arr; + if (fp != NULL) { + fclose(fp); + } + return arr; } diff --git a/test/runner.c b/test/runner.c index 58df0c9..de058f0 100644 --- a/test/runner.c +++ b/test/runner.c @@ -1,13 +1,13 @@ -#include "test.h" - #include +#include "test.h" + int main(int argc, char *argv[]) { - printf("%s:\n", argv[0]); - for (size_t i = 0; i < NUM_TEST_CASES; ++i) { - printf(" * %s: ", TEST_CASES[i].name); - TEST_CASES[i].test(); - printf("OK!\n"); - } - return 0; + printf("%s:\n", argv[0]); + for (size_t i = 0; i < NUM_TEST_CASES; ++i) { + printf(" * %s: ", TEST_CASES[i].name); + TEST_CASES[i].test(); + printf("OK!\n"); + } + return 0; } diff --git a/test/test.h b/test/test.h index 86ea6ff..b51fb6e 100644 --- a/test/test.h +++ b/test/test.h @@ -4,8 +4,8 @@ #include typedef struct { - const char *name; - void (*test)(void); + const char *name; + void (*test)(void); } test_case_t; extern const test_case_t TEST_CASES[]; diff --git a/test/test_util.c b/test/test_util.c index d298dbd..155ebc5 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -1,32 +1,32 @@ -#include "test.h" -#include "util.h" - #include #include #include #include +#include "test.h" +#include "util.h" + #define ARR_SIZE 50 void test_read_file(void) { - const char *filepath = tmpnam(NULL); + const char *filepath = tmpnam(NULL); - uint8_t arr[ARR_SIZE] = {0}; - for (size_t i = 0; i < sizeof arr; ++i) { - arr[i] = rand() % 256; - } + uint8_t arr[ARR_SIZE] = {0}; + for (size_t i = 0; i < sizeof arr; ++i) { + arr[i] = rand() % 256; + } - FILE *fp = fopen(filepath, "wb"); - assert(fp != NULL); + FILE *fp = fopen(filepath, "wb"); + assert(fp != NULL); - fwrite(&arr[0], 1, ARR_SIZE, fp); - fclose(fp); + fwrite(&arr[0], 1, ARR_SIZE, fp); + fclose(fp); - byte_arr_t *barr = read_file(filepath); - assert(barr != NULL); - assert(barr->size == ARR_SIZE); + byte_arr_t *barr = read_file(filepath); + assert(barr != NULL); + assert(barr->size == ARR_SIZE); - assert(memcmp(&arr[0], barr->data, ARR_SIZE) == 0); + assert(memcmp(&arr[0], barr->data, ARR_SIZE) == 0); } const test_case_t TEST_CASES[] = {