/*1this file contains stuff which isn't realistic emulation but is used for loading and bootstrapping things2*/34#include <stdio.h>5#include <string.h>6#include <assert.h>78#include "types.h"9#include "loader.h"10#include "asm.h"1112void Load_BIOS(PSX& psx, const char* path)13{14FILE* inf = fopen(path,"rb");15fread(psx.bios,1,BIOS_SIZE,inf);16fclose(inf);17}1819bool Load_EXE_Check(const char* fname)20{21//check for the PSX EXE signature22FILE* inf = fopen(fname, "rb");23char tmp[8] = {0};24fread(tmp,1,8,inf);25fclose(inf);26return !memcmp(tmp, "PS-X EXE", 8);27}2829//TODO - we could load other format EXEs as well, not just PSX-EXE (psxjin appears to do this? check PSXGetFileType and Load() in misc.cpp)30void Load_EXE(PSX& psx, const wchar_t* fname)31{32FILE* inf = _wfopen(fname, L"rb");33PSX_EXE_Header header;34fread(&header,sizeof(PSX_EXE_Header),1,inf);3536//load the text section to main memory37u32 text_destination = header.text_load_addr & RAM_MASK; //convert from virtual address to physical38fseek(inf, header.text_exe_offset + 0x800, SEEK_SET); //image addresses are relative to the image section of the file (past the 0x800 header)39fread(psx.ram+text_destination,1,header.text_size,inf);4041//now, mednafen patches the bios to run its own routine loaded to PIO which loads the program from fake memory.42//i have a better idea. lets patch it with a special escape code which will run the bootstrapping code in C43psx.patch(0xBFC06990, ASM_BREAK(PSX::eFakeBreakOp_BootEXE));44psx.exeBootHeader = header;4546//patch the kernel image section of the bios with traps for our bios hacks47psx.patch(0xBFC10000, ASM_BREAK(PSX::eFakeBreakOp_BiosHack)); //im not sure why we have to include these two. they must get chosen for some other reason to get patched into the kernel48psx.patch(0xBFC10010, ASM_BREAK(PSX::eFakeBreakOp_BiosHack)); //..49psx.patch(0xBFC10020, ASM_BREAK(PSX::eFakeBreakOp_BiosHack)); //this should correspond to 0xA0 in kernel area50psx.patch(0xBFC10030, ASM_BREAK(PSX::eFakeBreakOp_BiosHack)); //this should correspond to 0xB0 in kernel area5152}5354