2024-04-21 12:37:45 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2026-08-01 15:45:00 +02:00
|
|
|
#include "exe.h"
|
|
|
|
|
#include "log.h"
|
|
|
|
|
#include "frontend/platform_file.h"
|
2024-04-21 12:37:45 -03:00
|
|
|
|
2024-09-03 12:31:29 -03:00
|
|
|
int psx_exe_load(psx_cpu_t* cpu, const char* path) {
|
|
|
|
|
if (!path)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2026-08-01 15:45:00 +02:00
|
|
|
FILE* file = psxe_platform_fopen(path, "rb");
|
2024-04-21 12:37:45 -03:00
|
|
|
|
2024-09-03 12:31:29 -03:00
|
|
|
if (!file)
|
|
|
|
|
return 1;
|
2024-04-21 12:37:45 -03:00
|
|
|
|
|
|
|
|
// Read header
|
|
|
|
|
psx_exe_hdr_t hdr;
|
|
|
|
|
|
2024-09-03 12:31:29 -03:00
|
|
|
if (!fread((char*)&hdr, 1, sizeof(psx_exe_hdr_t), file))
|
|
|
|
|
return 2;
|
2024-04-21 12:37:45 -03:00
|
|
|
|
|
|
|
|
// Seek to program start
|
|
|
|
|
fseek(file, 0x800, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
// Read to RAM directly
|
|
|
|
|
uint32_t offset = hdr.ramdest & 0x7fffffff;
|
|
|
|
|
|
2026-07-27 21:23:15 +02:00
|
|
|
if (!fread(cpu->bus->ram->buf + offset, 1, hdr.filesz, file))
|
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
|
|
psx_cpu_invalidate_range(cpu, offset, hdr.filesz);
|
|
|
|
|
|
2024-04-21 12:37:45 -03:00
|
|
|
// Load initial register values
|
|
|
|
|
cpu->pc = hdr.ipc;
|
|
|
|
|
cpu->next_pc = cpu->pc + 4;
|
|
|
|
|
cpu->r[28] = hdr.igp;
|
|
|
|
|
|
|
|
|
|
if (hdr.ispb) {
|
|
|
|
|
cpu->r[29] = hdr.ispb + hdr.ispoff;
|
|
|
|
|
cpu->r[30] = cpu->r[29];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info("Loaded PS-X EXE file \"%s\"", path);
|
|
|
|
|
log_fatal("PC=%08x SP=%08x (%08x) GP=%08x", cpu->pc, cpu->r[29], hdr.ispb, cpu->r[28]);
|
|
|
|
|
|
|
|
|
|
fclose(file);
|
2024-09-03 12:31:29 -03:00
|
|
|
|
|
|
|
|
return 0;
|
2026-07-27 21:23:15 +02:00
|
|
|
}
|