/*---------------------------------------------------------------------------*
  Project:  NitroSDK - CARD - include
  File:     common.h

  Copyright 2004-2004 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: common.h,v $
  Revision 1.9  2004/10/08 14:31:52  yosizaki
  add CARD_GetRomHeader().

  Revision 1.8  2004/09/27 13:40:49  yada
  add some definitions

  Revision 1.7  2004/09/09 07:11:21  yosizaki
  delete CARD_BACKUP_TYPE_EEPROM_ 512k, 1M.

  Revision 1.6  2004/08/18 13:40:49  yosizaki
  add FLASH 2Mbit.

  Revision 1.5  2004/08/18 08:19:19  yosizaki
  change CARDBackupType format.

  Revision 1.4  2004/07/28 13:18:29  yosizaki
  asynchronous mode support.

  Revision 1.3  2004/07/23 08:31:58  yosizaki
  add new function.

  Revision 1.2  2004/07/19 13:10:34  yosizaki
  add task thread.

  Revision 1.1  2004/07/05 08:10:48  yosizaki
  (none)

  $NoKeywords: $
 *---------------------------------------------------------------------------*/


#if	!defined(NITRO_CARD_COMMON_H_)
#define NITRO_CARD_COMMON_H_


#if	defined(__cplusplus)
extern "C" {
#endif


#include <nitro/misc.h>
#include <nitro/types.h>
#include <nitro/memorymap.h>
#include <nitro/mi/dma.h>
#include <nitro/os.h>


/********************************************************************
 * constants
 ********************************************************************/

/* API result code */
typedef	enum {
	CARD_RESULT_SUCCESS = 0,
	CARD_RESULT_FAILURE,
	CARD_RESULT_INVALID_PARAM,
	CARD_RESULT_UNSUPPORTED,
	CARD_RESULT_TIMEOUT,
	CARD_RESULT_ERROR
} CARDResult;

/* for CARD_IdentifyBackup() */
#define	CARD_BACKUP_TYPE_DEVICE_SHIFT	0
#define	CARD_BACKUP_TYPE_DEVICE_MASK	0xFF
#define	CARD_BACKUP_TYPE_DEVICE_EEPROM	1
#define	CARD_BACKUP_TYPE_DEVICE_FLASH	2
#define	CARD_BACKUP_TYPE_SIZEBIT_SHIFT	8
#define	CARD_BACKUP_TYPE_SIZEBIT_MASK	0xFF
#define	CARD_BACKUP_TYPE_VENDER_SHIFT	16
#define	CARD_BACKUP_TYPE_VENDER_MASK	0xFF
#define	CARD_BACKUP_TYPE_DEFINE(type, size, vender)	\
	(((CARD_BACKUP_TYPE_DEVICE_ ## type) << CARD_BACKUP_TYPE_DEVICE_SHIFT) |	\
	((size) << CARD_BACKUP_TYPE_SIZEBIT_SHIFT) |	\
	((vender) << CARD_BACKUP_TYPE_VENDER_SHIFT))

typedef	enum {
	CARD_BACKUP_TYPE_EEPROM_4KBITS   = CARD_BACKUP_TYPE_DEFINE(EEPROM,  9, 0),
	CARD_BACKUP_TYPE_EEPROM_64KBITS  = CARD_BACKUP_TYPE_DEFINE(EEPROM, 13, 0),
	CARD_BACKUP_TYPE_FLASH_2MBITS    = CARD_BACKUP_TYPE_DEFINE(FLASH, 18, 0),
	CARD_BACKUP_TYPE_NOT_USE = 0
} CARDBackupType;


#define	CARD_THREAD_PRIORITY_DEFAULT	4


/********************************************************************
 * functions
 ********************************************************************/

/* card-system settings *********************************************/

/*
 * initialization
 * You need to call at least once before all of CARD functions.
 */
void	CARD_Init(void);

/*
 * determine if initialization done
 */
BOOL	CARD_IsAvailable(void);

/* Obtain priority of thread used in asynchronized functions of card. */
u32	CARD_GetThreadPriority(void);

/* Set priority of thread used in asynchronized functions of card. */
u32	CARD_SetThreadPriority(u32 prior);

/* Get function error code which is called lastly */
CARDResult	CARD_GetResultCode(void);


/*---------------------------------------------------------------------------*
  Name:         CARD_GetRomHeader

  Description:  get data buffer which contains own ROM header information.
                if wireless-downloaded program call this function,
                the data is not equal to HW_ROM_HEADER_BUF's.

  Arguments:    None.

  Returns:      pointer of CARD-ROM header information buffer.
 *---------------------------------------------------------------------------*/
const u8*	CARD_GetRomHeader(void);


/* backup settings **************************************************/

/* Specification of CARD backup device. */
void	CARD_IdentifyBackup(CARDBackupType type);

/* Obtain total size of backup device. */
u32	CARD_GetBackupTotalSize(void);

/* Obtain write sector size (BYTE) of backup device. */
u32	CARD_GetBackupSectorSize(void);

/* Obtain write page size (BYTE) of backup device. */
u32	CARD_GetBackupPageSize(void);

/* Obtain currently setup backup device type. */
CARDBackupType	CARD_GetCurrentBackupType(void);


// PXI protocol definition
#define CARD_PXI_COMMAND_MASK              0x0000003f  // command part
#define CARD_PXI_COMMAND_SHIFT             0
#define CARD_PXI_COMMAND_PARAM_MASK        0x01ffffc0  // parameter part
#define CARD_PXI_COMMAND_PARAM_SHIFT       6

//---- PXI command
#define CARD_PXI_COMMAND_TERMINATE         0x0001      // arm9->arm7 terminate command
#define CARD_PXI_COMMAND_PULLED_OUT        0x0011      // arm7->arm9 pulled out message


#if	defined(__cplusplus)
}	/* extern "C" */
#endif


/* NITRO_CARD_COMMON_H_ */
#endif
