#include "boot.h"
|
|
#include "cpu.h"
|
|
#include "insn.h"
|
|
#include "psx.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.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 header;
|
|
|
|
// TODO: Convert asserts into recoverable error
|
|
assert(sizeof header == 0x38 - 0x10);
|
|
assert(prgm->size >= 0x800);
|
|
assert(memcmp(MAGIC, prgm->data, sizeof MAGIC) == 0);
|
|
memcpy(&header, &prgm->data[sizeof MAGIC], sizeof header);
|
|
|
|
assert(header.size_file == prgm->size - 0x800);
|
|
assert(memcmp(RESERVED, &prgm->data[0x38], sizeof RESERVED) == 0);
|
|
|
|
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)
|
|
{
|
|
printf("parsing program header\n");
|
|
|
|
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);
|
|
|
|
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;
|
|
}
|
|
}
|