summaryrefslogtreecommitdiff
path: root/libpsx/src/runexe/runexe.c
blob: 5fe22b0ee6610d7fefa744beb1e96013681edee1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * PSXSDK
 *
 * RunEXE functionality
 */

#include <psx.h>
#include <stdio.h>
#include <string.h>
#include <runexe.h>

// Include runexe stage2 position independent code

extern void PSX_RunExeStage2();
extern void PSX_RunExeStage2_END();

static void *runExeBuffer;

int PSX_RunExe(void *exeBuffer)
{
	//unsigned char *exe = exeBuffer;
	unsigned int *exe = exeBuffer;
// Size of position indipendent code	
	int sz = PSX_RunExeStage2_END -
			PSX_RunExeStage2;
	
	if(!(PSX_GetInitFlags() & PSX_INIT_SAVESTATE))
	{
		printf("RunExe error: State was not saved!\n");
		return 0;
	}
	
	if(((unsigned int)exeBuffer & 3))
	{
		printf("RunExe: Alignment violation.\n");
		printf("Specified buffer must have an address which is a multiplier of 4.\n");
		return 0;
	}
	
	// Check magic string
	if(strcmp(exeBuffer, "PS-X EXE") != 0)
		return 0; // Return failure if magic string couldn't be found
	
	runExeBuffer = exeBuffer;
	
	printf("PSXSDK RunExe Debug Information\n");
	printf("t_addr = %08x\n", exe[6]);
	printf("t_size = %08x\n", exe[7]);
	printf("d_addr = %08x\n", exe[8]);
	printf("d_size = %08x\n", exe[9]);
	printf("b_addr = %08x\n", exe[10]);
	printf("b_size = %08x\n", exe[11]);
	printf("s_addr = %08x\n", exe[12]);
	printf("s_size = %08x\n", exe[13]);
	
	printf("OK..! Farewell.\n");

// Deinitialize the PSXSDK library	
	PSX_DeInit();
	
// Copy the position independent stage2 code in high memory
	memcpy((void*)0x801fef00, PSX_RunExeStage2, sz);
	
// Stack is unusable now! Do not do anything else except calling the stage2 code.
	
// Jump to the stage2 code without using the stack..
	
	__asm__("la $a0, runExeBuffer");
	__asm__("nop");
	__asm__("lw $a0, 0($a0)");
	__asm__("j 0x801fef00");
	__asm__("nop");
	
// This is never reached but keep the compiler happy	
	return 0;
}