/*************************************************************************
 *
 * boot.c
 *
 *************************************************************************
 *
 * main boot process
 *
 *************************************************************************/

/*
 * includes
 */

#include "ult_64.h"

#include "defs.h"
#include "threads.h"


extern void
	Stack_Setup( void *_stack, s32 size );


/*
 * defines
 */

#define IDLE_STACK_SIZE 128

#ifdef DEBUG
	#define MAIN_STACK_SIZE (32*1024)
#else
	#define MAIN_STACK_SIZE (8*1024)
#endif


/*
 * typedefs
 */


extern void
	MainProc( void *arg );


/*
 * structures
 */

/*
 * globals
 */

static OSThread
	IdleThread,
	MainThread;

/*static*/ u64
	IdleStack[IDLE_STACK_SIZE/sizeof(u64)],
	MainStack[MAIN_STACK_SIZE/sizeof(u64)];

u64 bootStack[272/sizeof(u64)];


/*
 * function prototypes
 */

static void
	IdleProc( void *arg );


/*
 * code
 */

/*************************************************************************
 *
 * void boot(void)
 *
 *************************************************************************
 *
 * Basic initialisation stuff - parse command line, start idle thread
 *
 *************************************************************************/

void boot(void)
{
	/*
	 * basic OS init
	 */

	osInitialize();

	/*
	 * connect to PC debugger and init PC filesystem
	 */

#ifdef DEBUG
	init_debug();
	asm("break 0x407");
	PCinit();
#endif

	/*
	 * create idle thread
	 */

Stack_Setup( IdleStack, IDLE_STACK_SIZE );

	osCreateThread( &IdleThread,
									THREAD_IDLE,
									IdleProc,
									NULL,
									&IdleStack[IDLE_STACK_SIZE/sizeof(u64)],
									THREAD_IDLE+1 );

	osStartThread( &IdleThread );

	/*
	 * hang
	 */

	while(1);
}

/*************************************************************************
 *
 * static void IdleThread(void *arg)
 *
 *************************************************************************
 *
 * Idle
 *
 *************************************************************************/

static void IdleProc( void *arg )
{
	/*
	 * create main thread
	 */

Stack_Setup( MainStack, MAIN_STACK_SIZE );

	osCreateThread( &MainThread,
									THREAD_MAIN,
									MainProc,
									arg,
									&MainStack[MAIN_STACK_SIZE/sizeof(u64)],
									THREAD_MAIN );

	osStartThread( &MainThread );

	/*
	 * become the idle thread
	 */

	osSetThreadPri( NULL, THREAD_IDLE );
	while(1);
}


/*************************************************************************
 *
 *
 *
 *************************************************************************
 *
 *
 *
 *************************************************************************/


/*************************************************************************
 *
 *
 *
 *************************************************************************
 *
 *
 *
 *************************************************************************/

/*************************************************************************
 *
 *
 *
 *************************************************************************
 *
 *
 *
 *************************************************************************/

