11#include " iSystem.h"
22
3+ #include " dolphin/dvd/dvd.h"
4+ #include " dolphin/gx/GXFrameBuffer.h"
5+ #include " dolphin/gx/GXManage.h"
6+ #include " dolphin/gx/GXStruct.h"
7+ #include " dolphin/hio.h"
8+ #include " dolphin/os/OSError.h"
9+ #include " dolphin/vi.h"
10+ #include " rpcollis.h"
11+ #include " rphanim.h"
12+ #include " rpmatfx.h"
13+ #include " rpptank.h"
14+ #include " rpskin.h"
15+ #include " rpusrdat.h"
16+ #include " rpworld.h"
17+ #include " rwplcore.h"
18+ #include " xBase.h"
19+ #include " xFX.h"
20+ #include " xShadow.h"
21+ #include " xstransvc.h"
22+ #include < stdio.h>
323#include < types.h>
424
525#include < rwcore.h>
1030#include " xPad.h"
1131#include " xMemMgr.h"
1232
33+ #include " iMemMgr.h"
1334#include " iSystem.h"
1435#include " iFile.h"
36+ #include " iFMV.h"
1537#include " iTime.h"
1638#include " iTRC.h"
1739
18- extern U32 mem_base_alloc;
19- extern U32 add;
20- extern U32 size;
21- extern S32 gEmergencyMemLevel ;
22- extern OSHeapHandle the_heap;
23- extern void * bad_val;
24- extern void * MemoryFunctions[4 ];
25- extern U16 last_error;
26- extern OSContext* last_context;
27-
28- // Taken from iSystem.s
29- // Defining these here makes the stringBase0 offsets match in the later functions.
30- static char * str1 = " Level %d, Warning $03d: %s\n " ;
31- static char * str2 = " FPE: Invalid operation: " ;
32- static char * str3 = " SNaN\n " ;
33- static char * str4 = " Infinity - Infinity\n " ;
34- static char * str5 = " Infinity / Infinity\n " ;
35- static char * str6 = " 0 / 0\n " ;
36- static char * str7 = " Infinity * 0\n " ;
37- static char * str8 = " Invalid compare\n " ;
38- static char * str9 = " Software request\n " ;
39- static char * str10 = " Invalid square root\n " ;
40- static char * str11 = " Invalid integer convert\n " ;
41- static char * str12 = " FPE: Overflow\n " ;
42- static char * str13 = " FPE: Underflow\n " ;
43- static char * str14 = " FPE: Zero division\n " ;
44- static char * str15 = " FPE: Inexact result\n " ;
45- static char * str16 = " iSystem.cpp" ;
46- static char * str17 = " Unable to initialize memory system.\n " ;
47- static char * str18 = " (With apologies to Jim Morrison) This the end, my only friend, The End." ;
48- static char * str19 = " %s.rw3" ;
49-
50- void ** psGetMemoryFunctions ()
40+ struct
5141{
52- return MemoryFunctions;
42+ GXRenderModeObj* renderMode;
43+ U32 unk_4;
44+ U32 unk_8;
45+ } deviceConfig;
46+ RwVideoMode sVideoMode ;
47+
48+ GXDrawSyncCallback old_dsc;
49+ U16 last_error;
50+ OSContext* last_context;
51+ U32 size;
52+ U32 add;
53+ S32 gEmergencyMemLevel ;
54+ void * bad_val = (void *)0x81abcaa0 ;
55+ U32 test_alloc_val = 0x210A ;
56+
57+ static RwMemoryFunctions MemoryFunctions;
58+
59+ static const char * __deadstripped ()
60+ {
61+ return " Level %d, Warning $03d: %s\n " ;
62+ }
63+
64+ RwMemoryFunctions* psGetMemoryFunctions ()
65+ {
66+ return &MemoryFunctions;
5367}
5468
5569void iVSync ()
5670{
5771 VIWaitForRetrace ();
5872}
5973
60- U16 my_dsc (U16 dsc)
74+ void my_dsc (u16 dsc)
6175{
62- return dsc;
6376}
6477
65- void FloatingPointErrorHandler (U16 last, OSContext* ctxt, U64 unk1, U64 unk2)
78+ static void FloatingPointErrorHandler (U16 last, OSContext* ctxt, u32 unk1, u32 unk2)
6679{
67- U32 uVar2;
68- uVar2 = (ctxt-> fpscr ) & 0xf8 << 0x16 | 0x1f80700 ;
80+ U32 uVar2 = ctxt-> fpscr ;
81+ uVar2 & = ((uVar2 & 0xf8 ) << 0x16 | 0x1f80700 ) ;
6982 if ((uVar2 & 0x20000000 ) != 0 )
7083 {
7184 OSReport (" FPE: Invalid operation: " );
@@ -126,7 +139,7 @@ void FloatingPointErrorHandler(U16 last, OSContext* ctxt, U64 unk1, U64 unk2)
126139 ctxt->srr0 = ctxt->srr0 + 4 ;
127140}
128141
129- void MemoryProtectionErrorHandler (U16 last, OSContext* ctx, U64 unk1, U64 unk2)
142+ static void MemoryProtectionErrorHandler (u16 last, OSContext* ctx, u32 unk1, u32 unk2)
130143{
131144 last_error = last;
132145 last_context = ctx;
@@ -136,24 +149,54 @@ void MemoryProtectionErrorHandler(U16 last, OSContext* ctx, U64 unk1, U64 unk2)
136149 }
137150}
138151
139- // FIXME: Define a bunch of functions :)
140- void TRCInit ()
152+ static void TRCInit ()
141153{
142154 iTRCDisk::Init ();
143- // iTRCDisk::SetPadStopRumblingFunction(iPadStopRumble);
144- // iTRCDisk::SetSndSuspendFunction(iSndSuspend);
145- // iTRCDisk::SetSndResumeFunction(iSndResume);
146- // iTRCDisk::SetSndKillFunction(iSndDIEDIEDIE);
147- // iTRCDisk::SetMovieSuspendFunction(iFMV::Suspend);
148- // iTRCDisk::SetMovieResumeFunction(iFMV::Resume);
149- // ResetButton::SetSndKillFunction(iSndDIEDIEDIE);
155+ iTRCDisk::SetPadStopRumblingFunction (iPadStopRumble);
156+ iTRCDisk::SetSndSuspendFunction (iSndSuspend);
157+ iTRCDisk::SetSndResumeFunction (iSndResume);
158+ iTRCDisk::SetSndKillFunction (iSndDIEDIEDIE);
159+ iTRCDisk::SetMovieSuspendFunction (iFMV::Suspend);
160+ iTRCDisk::SetMovieResumeFunction (iFMV::Resume);
161+ ResetButton::SetSndKillFunction (iSndDIEDIEDIE);
150162}
151163
152- S32 RenderWareExit ()
164+ static S32 RenderWareExit ();
165+ S32 DolphinInitMemorySystem (RwMemoryFunctions*);
166+ static S32 RenderWareInit ();
167+
168+ // FIXME: This should be a dolphin header somewhere
169+ extern " C" GXDrawSyncCallback GXSetDrawSyncCallback (GXDrawSyncCallback cb);
170+ void iSystemInit (U32 options)
153171{
154- RwEngineStop ();
155- RwEngineClose ();
156- return RwEngineTerm ();
172+ deviceConfig.renderMode = &GXNtsc480IntDf;
173+ deviceConfig.unk_4 = 0 ;
174+ deviceConfig.unk_8 = 0x40000 ;
175+
176+ GXSetMisc (GX_MT_XF_FLUSH, 8 );
177+ OSInit ();
178+ DVDInit ();
179+ VIInit ();
180+
181+ __OSFpscrEnableBits = FPSCR_ZE;
182+ OSSetErrorHandler (0x10 , (OSErrorHandler)FloatingPointErrorHandler);
183+ OSSetErrorHandler (0xf , (OSErrorHandler)MemoryProtectionErrorHandler);
184+
185+ if (!DolphinInitMemorySystem (&MemoryFunctions))
186+ {
187+ OSPanic (__FILE__, 0x1cc , " Unable to initialize memory system.\n " );
188+ }
189+ xDebugInit ();
190+ xMemInit ();
191+ iFileInit ();
192+ iTimeInit ();
193+ xPadInit ();
194+ xSndInit ();
195+ TRCInit ();
196+ RenderWareInit ();
197+ xMathInit ();
198+ xMath3Init ();
199+ old_dsc = GXSetDrawSyncCallback (my_dsc);
157200}
158201
159202void iSystemExit ()
@@ -166,10 +209,127 @@ void iSystemExit()
166209 iFileExit ();
167210 iTimeExit ();
168211 xMemExit ();
169- OSPanic (" iSystem.cpp " , 0x21d ,
212+ OSPanic (__FILE__ , 0x21d ,
170213 " (With apologies to Jim Morrison) This the end, my only friend, The End." );
171214}
172215
216+ static U32 RWAttachPlugins ()
217+ {
218+ if (!RpWorldPluginAttach ())
219+ {
220+ return true ;
221+ }
222+ if (!RpCollisionPluginAttach ())
223+ {
224+ return true ;
225+ }
226+ if (!RpSkinPluginAttach ())
227+ {
228+ return true ;
229+ }
230+ if (!RpHAnimPluginAttach ())
231+ {
232+ return true ;
233+ }
234+ if (!RpMatFXPluginAttach ())
235+ {
236+ return true ;
237+ }
238+ if (!RpUserDataPluginAttach ())
239+ {
240+ return true ;
241+ }
242+ if (!RpPTankPluginAttach ())
243+ {
244+ return true ;
245+ }
246+ return false ;
247+ }
248+
249+ static S32 DolphinInstallFileSystem ();
250+ static RwTexture* TextureRead (const RwChar* name, const RwChar* maskName);
251+
252+ static S32 RenderWareInit ()
253+ {
254+ RwMemoryFunctions* memoryFns = psGetMemoryFunctions ();
255+ if (!RwEngineInit (memoryFns, 0 , 0x60000 ))
256+ {
257+ return TRUE ;
258+ }
259+ RwResourcesSetArenaSize (0x60000 );
260+ if (!DolphinInstallFileSystem ())
261+ {
262+ return TRUE ;
263+ }
264+ if (RWAttachPlugins ())
265+ {
266+ return TRUE ;
267+ }
268+ RwEngineOpenParams params;
269+ params.displayID = &deviceConfig;
270+ if (!RwEngineOpen (¶ms))
271+ {
272+ RwEngineTerm ();
273+ return TRUE ;
274+ }
275+ RwEngineGetVideoModeInfo (&sVideoMode , RwEngineGetCurrentVideoMode ());
276+ if (!RwEngineStart ())
277+ {
278+ RwEngineClose ();
279+ RwEngineTerm ();
280+ return TRUE ;
281+ }
282+ RwTextureSetReadCallBack (TextureRead);
283+ RwRenderStateSet ((RwRenderState)0x14 , (void *)0x2 ); // RwRenderState 0x14 isn't defined??
284+ xShadowInit ();
285+ xFXInit ();
286+ RwTextureSetMipmapping (TRUE );
287+ RwTextureSetAutoMipmapping (TRUE );
288+ return FALSE ;
289+ }
290+
291+ static S32 RenderWareExit ()
292+ {
293+ RwEngineStop ();
294+ RwEngineClose ();
295+ return RwEngineTerm ();
296+ }
297+
298+ extern U32 _RwGameCubeRasterExtOffset;
299+ struct RwGameCubeRasterExt
300+ {
301+ U8 pad[0xc ];
302+ U32 unk_c;
303+ };
304+
305+ static RwTexture* TextureRead (const RwChar* name, const RwChar* maskName)
306+ {
307+ char buf[0x100 ];
308+ sprintf (buf, " %s.rw3" , name);
309+
310+ U32 assetSize;
311+ RwTexture* asset = (RwTexture*)xSTFindAsset (xStrHash (buf), &assetSize);
312+ if (asset != NULL )
313+ {
314+ if (asset->raster != NULL && asset->raster ->depth < 8 )
315+ {
316+ RwGameCubeRasterExt* ext =
317+ (RwGameCubeRasterExt*)((U32)asset->raster + _RwGameCubeRasterExtOffset);
318+ if (ext == NULL || ext->unk_c != 14 )
319+ {
320+ asset = NULL ;
321+ }
322+ }
323+ }
324+
325+ if (asset != NULL )
326+ {
327+ strcpy (asset->name , name);
328+ strcpy (asset->mask , maskName);
329+ }
330+ return asset;
331+ }
332+
173333void null_func ()
174334{
175335 mem_base_alloc += 4 ;
@@ -232,6 +392,39 @@ void _rwDolphinHeapFree(void* __ptr)
232392 }
233393}
234394
395+ void * _rwDolphinHeapAlloc (u32 size)
396+ {
397+ static u32 alloc_num = 0 ;
398+ U32 alloc = (U32)malloc (size + 0x20 );
399+ if (alloc != NULL )
400+ {
401+ *(U32*)((U8*)alloc + 0x0 ) = size;
402+ *(U32*)((U8*)alloc + 0x4 ) = alloc_num;
403+ u32 this_alloc = alloc_num;
404+ alloc_num++;
405+ if (this_alloc >= test_alloc_val - 0xa )
406+ {
407+ null_func ();
408+ *(U32*)((U8*)alloc + 0x8 ) = 0x1EE7D00D ;
409+ alloc = alloc + 0xc ;
410+ }
411+ else
412+ {
413+ *(U32*)((U8*)alloc + 0x8 ) = 0xDEADBEEF ;
414+ alloc = alloc + 0xc ;
415+ }
416+
417+ *(U32*)((U8*)alloc + 0x0 ) = 0xDEADBEEF ;
418+ *(U32*)((U8*)alloc + 0x4 ) = 0xDEADBEEF ;
419+ *(U32*)((U8*)alloc + 0x8 ) = 0xDEADBEEF ;
420+ *(U32*)((U8*)alloc + 0xc ) = 0xDEADBEEF ;
421+ *(U32*)((U8*)alloc + 0x10 ) = 0xDEADBEEF ;
422+ alloc = alloc + 0x14 ;
423+ }
424+
425+ return (void *)alloc;
426+ }
427+
235428S32 iGetMinute ()
236429{
237430 OSTime ticks = OSGetTime ();
0 commit comments