|
@ -1,4 +1,7 @@ |
|
|
#include "boot.h" |
|
|
#include "boot.h" |
|
|
|
|
|
#include "cpu.h" |
|
|
|
|
|
#include "insn.h" |
|
|
|
|
|
#include "psx.h" |
|
|
|
|
|
|
|
|
#include <assert.h> |
|
|
#include <assert.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdlib.h> |
|
@ -8,23 +11,12 @@ |
|
|
static const uint8_t MAGIC[0x10] = "PS-X EXE"; |
|
|
static const uint8_t MAGIC[0x10] = "PS-X EXE"; |
|
|
static const uint8_t RESERVED[0x10] = {0}; |
|
|
static const uint8_t RESERVED[0x10] = {0}; |
|
|
|
|
|
|
|
|
typedef struct __attribute__((packed)) |
|
|
|
|
|
{ |
|
|
|
|
|
uint32_t pc; |
|
|
|
|
|
uint32_t gp; |
|
|
|
|
|
uint32_t addr_dest; |
|
|
|
|
|
uint32_t size_file; |
|
|
|
|
|
uint32_t addr_data; |
|
|
|
|
|
uint32_t size_data; |
|
|
|
|
|
uint32_t addr_bss; |
|
|
|
|
|
uint32_t size_bss; |
|
|
|
|
|
} psx_header_t; |
|
|
|
|
|
|
|
|
|
|
|
psx_header_t parse_psx_header(byte_arr_t *prgm) |
|
|
psx_header_t parse_psx_header(byte_arr_t *prgm) |
|
|
{ |
|
|
{ |
|
|
psx_header_t header; |
|
|
psx_header_t header; |
|
|
|
|
|
|
|
|
// TODO: Convert asserts into recoverable error |
|
|
// TODO: Convert asserts into recoverable error |
|
|
|
|
|
assert(sizeof header == 0x38 - 0x10); |
|
|
assert(prgm->size >= 0x800); |
|
|
assert(prgm->size >= 0x800); |
|
|
assert(memcmp(MAGIC, prgm->data, sizeof MAGIC) == 0); |
|
|
assert(memcmp(MAGIC, prgm->data, sizeof MAGIC) == 0); |
|
|
memcpy(&header, &prgm->data[sizeof MAGIC], sizeof header); |
|
|
memcpy(&header, &prgm->data[sizeof MAGIC], sizeof header); |
|
@ -35,14 +27,56 @@ psx_header_t parse_psx_header(byte_arr_t *prgm) |
|
|
return header; |
|
|
return header; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint32_t translate_addr(uint32_t addr) |
|
|
|
|
|
{ |
|
|
|
|
|
if (addr >= 0xA0000000) |
|
|
|
|
|
{ |
|
|
|
|
|
addr = addr - 0xA0000000; |
|
|
|
|
|
} |
|
|
|
|
|
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) |
|
|
{ |
|
|
{ |
|
|
|
|
|
printf("parsing program header\n"); |
|
|
|
|
|
|
|
|
psx_header_t header = parse_psx_header(prgm); |
|
|
psx_header_t header = parse_psx_header(prgm); |
|
|
|
|
|
|
|
|
printf("pc = %08x\n", header.pc); |
|
|
|
|
|
printf("gp = %08x\n", header.gp); |
|
|
|
|
|
|
|
|
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("data = %08x (%u bytes)\n", header.addr_data, header.size_data); |
|
|
printf("bss = %08x (%u bytes)\n", header.addr_bss, header.size_bss); |
|
|
printf("bss = %08x (%u bytes)\n", header.addr_bss, header.size_bss); |
|
|
printf("dest = %08x\n", header.addr_dest); |
|
|
printf("dest = %08x\n", header.addr_dest); |
|
|
printf("size = %08x (%u bytes)\n", header.size_file, header.size_file); |
|
|
printf("size = %08x (%u bytes)\n", header.size_file, header.size_file); |
|
|
|
|
|
|
|
|
|
|
|
cpu_t *cpu = malloc(sizeof *cpu); |
|
|
|
|
|
|
|
|
|
|
|
uint32_t start = translate_addr(header.addr_dest); |
|
|
|
|
|
assert(start + header.size_file < MAIN_RAM_SIZE); |
|
|
|
|
|
memcpy(&cpu->main_ram[start], &prgm->data[0x800], header.size_file); |
|
|
|
|
|
|
|
|
|
|
|
cpu->pc = header.pc; |
|
|
|
|
|
cpu->regs[REG_GP] = header.gp; |
|
|
|
|
|
cpu->regs[REG_SP] = header.sp; |
|
|
|
|
|
cpu->regs[REG_FP] = header.fp; |
|
|
|
|
|
|
|
|
|
|
|
psx_t psx = {header, cpu}; |
|
|
|
|
|
const int max_insns = 1; |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < max_insns; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("=== insn ===\n"); |
|
|
|
|
|
printf("pc = %08x\n", psx.cpu->pc); |
|
|
|
|
|
|
|
|
|
|
|
const uint32_t insn = cpu_read32(cpu, psx.cpu->pc); |
|
|
|
|
|
insn_execute(cpu, insn); |
|
|
|
|
|
|
|
|
|
|
|
psx.cpu->pc += 1; |
|
|
|
|
|
} |
|
|
} |
|
|
} |