/*---------------------------------------------------------------------------*
  Project:  NitroSystem - build - demos - gfd - demolib
  File:     system.c

  Copyright 2004,2005 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: system.c,v $
  Revision 1.4  06/29/2005 04:56:34  hatamoto_minoru
  Corrected the year number given in the copyright.

  Revision 1.3  06/29/2005 04:43:58  hatamoto_minoru
  Fixed typos: NitroSyetem -> NitroSystem

  Revision 1.2  09/22/2004 08:23:33  kitani_toshikazu
  GfDDemo_Free() added

  Revision 1.1  09/21/2004 10:30:58  kitani_toshikazu
  gfd demo added

  $NoKeywords: $
 *---------------------------------------------------------------------------*/
#include "gfd_demolib/system.h"


#define SYSTEM_HEAP_SIZE        64*1024

#define GFDDEMO_ROUND_UP(value, alignment) \
    (((u32)(value) + (alignment-1)) & ~(alignment-1))

#define GFDDEMO_ROUND_DOWN(value, alignment) \
    ((u32)(value) & ~(alignment-1))

static NNSFndHeapHandle gfdDemoAppHeap;     // Application heap (use for expanded heap)


/*---------------------------------------------------------------------------*
    Initialization related.
 *---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*
  Name:         VBlankIntr_

  Description:  V blank callback routine.

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
static void
VBlankIntr_(void)
{
    OS_SetIrqCheckFlag(OS_IE_V_BLANK);
}

/*---------------------------------------------------------------------------*
  Name:         GFDDemo_CommonInit

  Description:  Initializes the OS, graphics engine, heap, and file system.
                 

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void GFDDemo_CommonInit(void)
{
    OS_Init();
    FX_Init();
    GX_SetPower(GX_POWER_ALL);
    GX_Init();
    OS_InitTick();

    GX_DispOff();
    GXS_DispOff();

    OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr_ );

    (void)OS_EnableIrqMask(OS_IE_V_BLANK);
    (void)OS_EnableIrqMask(OS_IE_FIFO_RECV);
    (void)OS_EnableIrq();

    (void)GX_VBlankIntr(TRUE);         // to generate VBlank interrupt request
    
    {
        void* sysHeapMemory = OS_AllocFromMainArenaLo(SYSTEM_HEAP_SIZE, 16);
        u32   arenaLow      = GFDDEMO_ROUND_UP  (OS_GetMainArenaLo(), 16);
        u32   arenaHigh     = GFDDEMO_ROUND_DOWN(OS_GetMainArenaHi(), 16);
        u32   appHeapSize   = arenaHigh - arenaLow;
        void* appHeapMemory = OS_AllocFromMainArenaLo(appHeapSize, 16);
        gfdDemoAppHeap      = NNS_FndCreateExpHeap(appHeapMemory, appHeapSize);
    }
}


void* GfDDemo_Allock( u32 size )
{
    return NNS_FndAllocFromExpHeapEx( gfdDemoAppHeap, size, 16);
}

void GfDDemo_Free( void* pBlk )
{
    NNS_FndFreeToExpHeap( gfdDemoAppHeap, pBlk );
}


