forked from n64decomp/perfect_dark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmain.c
142 lines (92 loc) · 2.36 KB
/
cmain.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <SDL.h>
#include <SDL_syswm.h>
#include <ultratypes.h>
#include "../src/lib/ultra/os/osint.h"
#include <rcp.h>
#include <PR/crt_thread.h>
#include "byteswaprom.h"
#include <gbi.h>
#include "../n64-fast3d-engine/gfx_pc.h"
#include "../n64-fast3d-engine/gfx_sdl.h"
#include "../n64-fast3d-engine/gfx_opengl.h"
extern void bootPhase1(void);
u8 * g_Rom;
u32 g_RomSize;
extern __OSEventState __osEventStateTab[OS_NUM_EVENTS];
u32 g_VIRegBase = 0;
u32 g_SP_DMem = 0;
u32 g_SP_IMem = 0;
u32 g_SP_DPC_RegBase = 0;
u32 g_SPRegBase = 0;
u32 g_SPPCReg = 0;
u32 g_AIRegBase = 0;
u32 g_RDRam = 0;
void loadRom()
{
FILE* f = fopen("C:\\Users\\Zak\\Documents\\GitProjects\\perfect_dark\\pd.ntsc-final.z64", "r");
fseek(f, 0, SEEK_END);
long size = ftell(f);
g_Rom = (u8*)malloc(size);
g_RomSize = size;
fseek(f, 0, SEEK_SET);
fread(g_Rom, 1, size, f);
fclose(f);
}
void openWindow()
{
gfx_init(&gfx_sdl, &gfx_opengl_api, "Perfect Dark", false);
}
void allocateMemoryAreas()
{
g_VIRegBase = (u32)calloc(64, 1);
g_SP_DMem = (u32)calloc(4096, 1);
g_SP_DMem = (u32)calloc(4096, 1);
g_SP_DPC_RegBase = (u32)calloc(32, 1);
g_SPRegBase = (u32)calloc(32, 1);
g_RDRam = (u32)calloc(8 * 1024 * 1024, 1);
g_SPPCReg = (u32)calloc(4, 1);
g_AIRegBase = (u32)calloc(32, 1);
}
int startGameOnThread(void* p)
{
openWindow();
bootPhase1();
return 0;
}
thrd_t startGameThread()
{
thrd_t t;
thrd_create(&t, &startGameOnThread, NULL);
return t;
}
Uint32 viCallback(Uint32 interval, void* param)
{
__OSEventState* es = &__osEventStateTab[OS_EVENT_VI];
if (es && es->messageQueue)
{
osSendMesg(es->messageQueue, es->message, OS_MESG_NOBLOCK);
}
return interval;
}
void startVITimer()
{
SDL_AddTimer(16, &viCallback, NULL);
}
int startGame()
{
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
loadRom();
byteSwapRom();
allocateMemoryAreas();
startVITimer();
thrd_t t = startGameThread();
thrd_join(t, NULL);
return 0;
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
return startGame();
}