/*-------------------------------------------------------------------
	DREAMCAST INITIALIZATION ROUTINES

	Thomas Szirtes / Sega Europe 1998
	modified from sbinit.c version 0.52

   Copyright (C) 1998 SEGA Enterprises Co.,Ltd
  --------------------------------------------------------------------*/


/*
 *		COMPILER SWITCHES
 */
 
/* Do you wish to use Ninja Library? (0 - no, 1 - yes) */
#define	USE_NINJA	0	

/* Do you wish to use GDFS (GDROM) system? (0 - no, 1 - yes) */
#define USE_GDFS 	1       

/* Place heap at end of memory (ie. don't care about size)? (0-no,1-yes) */
#define USE_B_END 	1

/* 
 *		INCLUDE HEADER FILES
 */

#include	<shinobi.h>
#include	<sg_syhw.h>
#define		_STRICT_UNION_
#define		_CLX2_
#include	<kamui2.h>


/*
 *		DEFINTIONS
 */

#define P1AREA   0x80000000

extern Uint8* _BSG_END;   

#define WORK_END (((Uint32)_BSG_END) & 0xe0000000 | 0x0d000000)

/* if using gdfs - specify maximum number of open files, and buffers */
#if	USE_GDFS
 #define FILES 8
 #define BUFFERS 1024
#endif

/* if not using b_end then specify size of heap */
#if !USE_B_END
 #define HEAP_SIZE 0x00400000
 #define HEAP_AREA ((void*)((Uint32)WORK_END - (Uint32)HEAP_SIZE))
#else
 #define HEAP_AREA ((void*)((((Uint32)_BSG_END | P1AREA) & 0xffffffe0) + 0x20))
 #define HEAP_SIZE (WORK_END - (Uint32)HEAP_AREA)
#endif


/* 
 *		SYSTEM DATA
 */

/* for maple bus peripheral data */
Uint8 gMapleRecvBuf[1024 * 24 * 2 + 32];
Uint8 gMapleSendBuf[1024 * 24 * 2 + 32];

#if USE_GDFS
 Uint8 gdfswork[GDFS_WORK_SIZE(FILES) + 32];
 Uint8 gdfscurdir[GDFS_DIRREC_SIZE(BUFFERS) + 32];
#endif



/*
 *		sbInitSystem();
 *
 *		Initialize hardware and shinobi libraries, also initialize Kamui/Ninja as specified
 *
 *		INPUTS :
 *			mode		-		screen resolution and display mode
 *			frame		-		frame buffer format
 *			count		-		vsync count
 */

void sbInitSystem(Int mode, Int frame, Int count)
{
	/* mask interrupts */
	set_imask(15);

	/* first part of hardware initialization */
	syHwInit();

	/* initialize heap for malloc */
	syMallocInit(HEAP_AREA, HEAP_SIZE);

#if	USE_NINJA
	njInitSystem( mode, frame, count );
#else
	kmInitDevice( (mode&(KM_PAL|KM_NTSC|KM_VGA)) );
	kmSetDisplayMode( mode, frame, TRUE, TRUE);
	kmSetWaitVsyncCount( count );
#endif

	/* second part of hardware initialization */
	syHwInit2();

	/* Initilize peripheral input */
	pdInitPeripheral(PDD_PLOGIC_ACTIVE, gMapleRecvBuf, gMapleSendBuf);

    syRtcInit();

	/* release interrupts */
	set_imask(0);

	/* initialize file system */
#if USE_GDFS
	{
		Uint8* wk;
		Uint8* dir;
        Sint32 err;

		wk  = (Uint8*)(((Uint32)gdfswork & 0xffffffe0) + 0x20);
		dir = (Uint8*)(((Uint32)gdfscurdir & 0xffffffe0) + 0x20);

        do {
            err = gdFsInit(FILES, wk, BUFFERS, dir);
        } while (err != GDD_ERR_OK);
	}
#endif


}



void sbExitSystem(void)
{

#if USE_GDFS
	gdFsFinish();
#endif
    syRtcFinish();


	pdExitPeripheral();

	njExitSystem();

	syMallocFinish();

	syHwFinish();

	set_imask(15);
}


/******************************* end of file *******************************/

