-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathinitialize.c
365 lines (326 loc) · 9.77 KB
/
initialize.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "initialize.h"
#include "aobj.h"
#include "class.h"
#include "debug.h"
#include "displayfunc.h"
#include "fobj.h"
#include "id.h"
#include "list.h"
#include "lobj.h"
#include "mtx.h"
#include "objalloc.h"
#include "random.h"
#include "robj.h"
#include "shadow.h"
#include "state.h"
#include "tev.h"
#include "video.h"
#include <stdarg.h>
#include <dolphin/gx/GXEnum.h>
#include <dolphin/gx/GXLight.h>
#include <dolphin/gx/GXPixel.h>
#include <dolphin/gx/types.h>
#include <dolphin/os.h>
#include <dolphin/os/OSArena.h>
#include <dolphin/os/OSMemory.h>
#include <dolphin/vi/vi.h>
extern OSHeapHandle HSD_Synth_804D6018;
extern GXRenderModeObj GXNtsc480IntDf;
static void* FrameBuffer[HSD_VI_XFB_MAX];
static HSD_MemReport memReport;
static void* hsd_heap_next_arena_lo;
static void* hsd_heap_next_arena_hi;
static HSD_RenderPass current_render_pass;
static GXFifoObj* DefaultFifoObj;
static int current_pix_fmt;
static s32 init_done;
static s32 shown;
static volatile OSHeapHandle current_heap = -1;
static GXRenderModeObj* rmode = &GXNtsc480IntDf;
static int current_z_fmt = GX_ZC_MID;
static u32 iparam_fifo_size = HSD_DEFAULT_FIFO_SIZE;
static int iparam_xfb_max_num = HSD_DEFAULT_XFB_MAX_NUM;
static int iparam_heap_max_num = 4;
static u32 iparam_audio_heap_size = HSD_DEFAULT_AUDIO_SIZE;
static GXColor HSD_Init_804D5E1C = { 0 };
void HSD_InitComponent(void)
{
HSD_OSInit();
{
HSD_VIStatus vi_status;
GXColor black = { 0, 0, 0, 0 };
vi_status.rmode = *rmode;
vi_status.black = GX_TRUE;
vi_status.vf = GX_TRUE;
vi_status.gamma = GX_GM_1_0;
vi_status.clear_clr = black;
vi_status.clear_z = GX_MAX_Z24;
vi_status.update_clr = GX_ENABLE;
vi_status.update_alpha = GX_ENABLE;
vi_status.update_z = GX_ENABLE;
HSD_VIInit(&vi_status, FrameBuffer[0], FrameBuffer[1], FrameBuffer[2]);
}
HSD_GXInit();
HSD_DVDInit();
HSD_IDSetup();
VIWaitForRetrace();
HSD_ObjInit();
HSD_LogInit();
init_done = true;
}
void HSD_GXSetFifoObj(GXFifoObj* fifo)
{
memReport.gxfifo = iparam_fifo_size;
DefaultFifoObj = fifo;
}
static void HSD_DVDInit(void) {}
void** HSD_AllocateXFB(s32 nbuffer, GXRenderModeObj* rm)
{
u32 fb_size;
u32 arena_lo;
u32 arena_hi;
s32 i;
if (rm == NULL) {
return NULL;
}
fb_size = ((rm->fbWidth + 0xF) & 0xFFF0) * rm->xfbHeight * 2;
arena_lo = OSRoundUp32B(OSGetArenaLo());
arena_hi = OSRoundDown32B(OSGetArenaHi());
memReport.xfb = fb_size * nbuffer;
if (arena_lo == arena_hi) {
for (i = 0; i < nbuffer; i++) {
FrameBuffer[i] = OSAllocFromHeap(__OSCurrHeap, fb_size);
}
} else {
for (i = 0; i < nbuffer; i++) {
FrameBuffer[i] = (void*) arena_lo;
arena_lo = OSRoundUp32B(arena_lo + fb_size);
}
if (arena_lo > arena_hi) {
HSD_Panic(__FILE__, 265, "No memory space remains for XFB.\n");
}
OSSetArenaLo((void*) arena_lo);
}
for (i = nbuffer; i < HSD_VI_XFB_MAX; i++) {
FrameBuffer[i] = NULL;
}
return FrameBuffer;
}
GXFifoObj* HSD_AllocateFifo(u32 size)
{
GXFifoObj* fifo;
u32 arena_lo;
u32 arena_hi;
arena_lo = OSRoundUp32B(OSGetArenaLo());
arena_hi = OSRoundDown32B(OSGetArenaHi());
if (arena_lo == 0 && arena_hi == 0) {
fifo = OSAllocFromHeap(__OSCurrHeap, size);
if (fifo == NULL) {
HSD_Panic(__FILE__, 295, "cannot allocate memory for gx fifo.\n");
}
} else {
fifo = (void*) arena_lo;
arena_lo += size;
if (arena_lo > (u32) OSGetArenaHi()) {
HSD_Panic(__FILE__, 302, "no space remains for gx fifo.\n");
}
OSSetArenaLo((void*) arena_lo);
}
return fifo;
}
static void HSD_GXInit(void)
{
GXLightObj lightobj;
int i;
GXInitLightPos(&lightobj, 1, 0, 0);
GXInitLightDir(&lightobj, 1, 0, 0);
GXInitLightAttn(&lightobj, 1, 0, 0, 1, 0, 0);
GXInitLightColor(&lightobj, HSD_Init_804D5E1C);
for (i = 0; i < 8; i++) {
GXLoadLightObjImm(&lightobj, HSD_Index2LightID(i));
}
HSD_StateInvalidate(-1);
}
static void HSD_OSInit(void)
{
u32 new_arena_lo;
u32 new_arena_hi;
u32 old_arena_lo = (u32) OSGetArenaLo();
u32 old_arena_hi = (u32) OSGetArenaHi();
memReport.total = OSGetPhysicalMemSize();
memReport.system = memReport.total - (u32) OSGetArenaHi() +
(u32) OSGetArenaLo() - memReport.xfb - memReport.gxfifo;
old_arena_lo = (u32) OSInitAlloc(
(void*) old_arena_lo, (void*) old_arena_hi, iparam_heap_max_num);
OSSetArenaLo((void*) old_arena_lo);
new_arena_lo = OSRoundUp32B(old_arena_lo);
new_arena_hi = OSRoundDown32B(old_arena_hi);
HSD_Synth_804D6018 = OSCreateHeap(
(void*) new_arena_lo, (void*) (new_arena_lo + iparam_audio_heap_size));
new_arena_lo += iparam_audio_heap_size;
hsd_heap_next_arena_lo = (void*) new_arena_lo;
hsd_heap_next_arena_hi = (void*) new_arena_hi;
current_heap = OSCreateHeap((void*) new_arena_lo, (void*) new_arena_hi);
OSSetCurrentHeap(current_heap);
memReport.heap = new_arena_hi - new_arena_lo;
HSD_ObjSetHeap(new_arena_hi - new_arena_lo, NULL);
OSSetArenaLo((void*) new_arena_hi);
}
OSHeapHandle HSD_GetHeap(void)
{
return current_heap;
}
void HSD_SetHeap(OSHeapHandle handle)
{
current_heap = handle;
}
void HSD_GetNextArena(void** lo, void** hi)
{
*lo = hsd_heap_next_arena_lo;
*hi = hsd_heap_next_arena_hi;
}
OSHeapHandle HSD_CreateMainHeap(void* lo, void* hi)
{
int i;
void (*cb_table[])(void* lo, void* hi) = {
_HSD_AObjForgetMemory,
_HSD_DispForgetMemory,
_HSD_IDForgetMemory,
_HSD_ObjAllocForgetMemory,
_HSD_RandForgetMemory,
_HSD_RObjForgetMemory,
NULL,
};
hsdForgetClassLibrary("sysdolphin_base_library");
HSD_ObjInit();
for (i = 0; cb_table[i] != NULL; i++) {
cb_table[i](hsd_heap_next_arena_lo, hsd_heap_next_arena_hi);
}
if (lo != NULL) {
hsd_heap_next_arena_lo = lo;
}
if (hi != NULL) {
hsd_heap_next_arena_hi = hi;
}
OSDestroyHeap(current_heap);
current_heap =
OSCreateHeap(hsd_heap_next_arena_lo, hsd_heap_next_arena_hi);
OSSetCurrentHeap(current_heap);
HSD_ObjSetHeap((u32) hsd_heap_next_arena_hi - (u32) hsd_heap_next_arena_lo,
NULL);
return current_heap;
}
HSD_RenderPass HSD_GetCurrentRenderPass(void)
{
return current_render_pass;
}
void HSD_StartRender(HSD_RenderPass pass)
{
GXRenderModeObj* rmode = HSD_VIGetRenderMode();
current_render_pass = pass;
if (rmode->aa) {
GXSetPixelFmt(GX_PF_RGB565_Z16, current_z_fmt);
} else {
GXSetPixelFmt(current_pix_fmt, GX_ZC_LINEAR);
}
GXSetFieldMode(rmode->field_rendering, rmode->xfbHeight < rmode->viHeight);
}
void HSD_Init_803755A8(void)
{
// Does nothing, but need to force a comparison to make this match
if (current_render_pass == HSD_RP_OFFSCREEN) {
current_render_pass == 0;
}
}
static void HSD_ObjInit(void)
{
HSD_ListInitAllocData();
HSD_AObjInitAllocData();
HSD_FObjInitAllocData();
HSD_IDInitAllocData();
HSD_VecInitAllocData();
HSD_MtxInitAllocData();
HSD_RObjInitAllocData();
HSD_RenderInitAllocData();
HSD_ShadowInitAllocData();
HSD_ZListInitAllocData();
}
#ifndef BUGFIX
static char str_pix_fmt_neq_gx_pf_rgb565_z16[] = "pix_fmt != GX_PF_RGB565_Z16";
#endif
void HSD_ObjDumpStat(void)
{
struct {
HSD_ObjAllocData* (*func)(void);
char* label;
} types[] = { { HSD_AObjGetAllocData, "aobj" },
{ HSD_FObjGetAllocData, "fobj" },
{ HSD_IDGetAllocData, "id" },
{ HSD_SListGetAllocData, "slist" },
{ HSD_DListGetAllocData, "dlist" },
{ HSD_VecGetAllocData, "vec" },
{ HSD_MtxGetAllocData, "mtx" },
{ HSD_RObjGetAllocData, "robj" },
{ HSD_RvalueObjGetAllocData, "rval" },
{ HSD_ShadowGetAllocData, "shadow" },
{ HSD_RenderGetAllocData, "render" },
{ HSD_ChanGetAllocData, "chan" },
{ HSD_TevRegGetAllocData, "tevreg" },
{ NULL, NULL } };
int i;
for (i = 0; types[i].label; i++) {
OSReport("objalloc: %s\tusing %d\tfreed %d\tpeak %d\n", types[i].label,
HSD_ObjAllocGetUsing(types[i].func()),
HSD_ObjAllocGetFreed(types[i].func()),
HSD_ObjAllocGetPeak(types[i].func()));
}
}
bool HSD_SetInitParameter(HSD_InitParam param, ...)
{
va_list ap;
bool ok = false;
if (init_done) {
if (!shown) {
OSReport(
"init parameter should be set before invoking HSD_Init().\n");
shown = true;
}
return false;
}
va_start(ap, param);
switch (param) {
case HSD_INIT_FIFO_SIZE: {
u32 fifo_size = va_arg(ap, u32);
if (fifo_size > 0) {
iparam_fifo_size = fifo_size;
ok = true;
}
} break;
case HSD_INIT_XFB_MAX_NUM: {
u32 xfb_max_num = va_arg(ap, u32);
if (xfb_max_num > 0) {
iparam_xfb_max_num = xfb_max_num;
ok = true;
}
} break;
case HSD_INIT_HEAP_MAX_NUM: {
u32 heap_size = va_arg(ap, u32);
if (heap_size > 0) {
iparam_heap_max_num = heap_size;
ok = true;
}
} break;
case HSD_INIT_AUDIO_HEAP_SIZE: {
u32 heap_size = va_arg(ap, u32);
if (heap_size > 0) {
iparam_audio_heap_size = heap_size;
ok = true;
}
} break;
case HSD_INIT_RENDER_MODE_OBJ:
rmode = va_arg(ap, GXRenderModeObj*);
break;
}
va_end(ap);
return ok;
}