shithub: psxe

ref: d4772ba78d6d6b0fa7fb14da7c81e6793313c1dd
dir: /psx/exe.c/

View raw version
#include <stdio.h>

#include "exe.h"
#include "log.h"

void psx_exe_load(psx_cpu_t* cpu, const char* path) {
    FILE* file = fopen(path, "rb");

    if (!file) {
        log_error("Couldn't open PS-X EXE file \"%s\"", path);

        exit(1);
    }

    // Read header
    psx_exe_hdr_t hdr;
    
    fread((char*)&hdr, 1, sizeof(psx_exe_hdr_t), file);

    // Seek to program start 
    fseek(file, 0x800, SEEK_SET);

    // Read to RAM directly
    uint32_t offset = hdr.ramdest & 0x7fffffff;

    fread(cpu->bus->ram->buf + offset, 1, hdr.filesz, file);

    // Load initial register values
    cpu->pc = hdr.ipc;
    cpu->r[28] = hdr.igp;
    cpu->r[29] = hdr.ispb + hdr.ispoff;
    cpu->r[30] = cpu->r[29];

    psx_cpu_fetch(cpu);

    log_info("Loaded PS-X EXE file \"%s\"", path);

    fclose(file);
}