/*---------------------------------------------------------------------------*
  Project:  NitroDWC - ectest
  File:     main.c

  Copyright 2003-2006 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.

  $NoKeywords: $
 *---------------------------------------------------------------------------*/


#include <nitro.h>
#include <nitro/card.h>
#include <nitro/fs.h>
#include <string.h>

/* Routine for screen rendering*/
#include "draw.h"
#include "ectest.h"


/*---------------------------------------------------------------------------*
  Name:         VBlankIntr

  Description:  V-blank callback

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
static void
VBlankIntr(void)
{
    OS_SetIrqCheckFlag(OS_IE_V_BLANK);
}


/*---------------------------------------------------------------------------*
  Name:         InitApp

  Description:  OS etc., application base part initialization

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
static void
InitApp(void)
{
    void   *tmp;
    OSHeapHandle hh;

    /* OS initialization*/
    OS_Init();
    OS_InitThread();
    OS_InitTick();

    /* Arena initialization*/
    tmp = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
    OS_SetArenaLo(OS_ARENA_MAIN, tmp);
    hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
    if (hh < 0)
        OS_Panic("ARM9: Fail to create heap...\n");
    hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);

    /* Enable interrupts*/
    OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
    (void)OS_EnableIrqMask(OS_IE_V_BLANK);
    (void)OS_EnableIrq();
}

static void 
drawScreen(void)
{
    ClearFrame(0);
    DrawBegin();
    SetTextColor(RGB555_WHITE);

    DrawText((32-sizeof("EC TEST"))/2*10, 0, "EC TEST");
    DrawText(10, 100, "Press A to start test");

    DrawEnd();
}


/*---------------------------------------------------------------------------*
  Name:         NitroMain

  Description:  main entry point

  Arguments:    None.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NitroMain(void)
{
    u32 last, r;
    ECError rv = EC_ERROR_OK;

    /* Sample application initialization*/
    InitApp();
    InitDraw();
    SetClearColor(RGB555_CLEAR);

    CTRDG_Init();
    CTRDG_Enable(TRUE);

    drawScreen();

    /* Screen transition*/
    last = PAD_Read();
    for (;;) {
        r = PAD_Read();
        if ((r & PAD_BUTTON_A) && !(last & PAD_BUTTON_A)) {
            rv = ectest();
        }
        last = r;
    }
}
