gbpakinit.c 2.96 KB
/*---------------------------------------------------------------------
        Copyright (C) 1997, Nintendo.
        
        File            gbpakinit.c
        Coded    by     Koji Mitsunari. Oct  2, 1997.
        Modified by     Koji Mitsunari. Apr 18, 1998.
        Comments        64GB-PAK Initialize
   
        $Id: 
   ---------------------------------------------------------------------*/

/**************************************************************************
 *
 *  $Revision: 1.1.1.1 $
 *  $Date: 2002/05/02 03:28:44 $
 *  $Source: 
 *
 **************************************************************************/

#include "osint.h"
#include "controller.h"

#define	GBPAK_INIT_WAIT		192000	/* micro sec. */

OSTimer		__osGbpakTimer;
OSMesgQueue	__osGbpakTimerQ;
OSMesg		__osGbpakTimerMsg;

s32
osGbpakInit(OSMesgQueue *mq, OSPfs *pfs, int channel)
{
  int	i;
  s32	ret;
  u8	temp[BLOCKSIZE];

  pfs->status = 0;

  /* broken CPAK or not ? */
  for ( i = 0 ; i < BLOCKSIZE ; temp[i++] = OS_PFS_CHECK_ID);

  ret =  __osContRamWrite(mq, channel, PFS_BANK_ADDR, temp, 0);
  if (ret == PFS_ERR_NEW_PACK) {
    ret =  __osContRamWrite(mq, channel, PFS_BANK_ADDR, temp, 0);
  }
  if (ret != 0) {
    return(ret);
  }
  
  ret =  __osContRamRead(mq, channel, PFS_BANK_ADDR, temp);
  if (ret == PFS_ERR_NEW_PACK) {
    ret =  PFS_ERR_CONTRFAIL;
  }
  if (ret != 0) {
    return(ret);
  } else if (temp[0x1f] == OS_PFS_CHECK_ID) {
    return(PFS_ERR_DEVICE);
  }

  /* Check GB-PAK ID */
  for ( i = 0 ; i < BLOCKSIZE ; temp[i++] = OS_GBPAK_ID);
  ret =  __osContRamWrite(mq, channel, PFS_BANK_ADDR, temp, 0);
  if (ret == PFS_ERR_NEW_PACK) {
    ret =  PFS_ERR_CONTRFAIL;
  }
  if (ret != 0) {
    return(ret);
  }
  
  ret =  __osContRamRead(mq, channel, PFS_BANK_ADDR, temp);
  if (ret == PFS_ERR_NEW_PACK) {
    ret =  PFS_ERR_CONTRFAIL;
  }
  if (ret != 0) {
    return(ret);
  } else if ( temp[0x1f] != OS_GBPAK_ID ) {
    return(PFS_ERR_DEVICE);
  }
  if ((ret = __osPfsGetStatus(mq, channel)) != 0){
    return(ret);
  }

  osCreateMesgQueue(&__osGbpakTimerQ, &__osGbpakTimerMsg, 1);
  osSetTimer(&__osGbpakTimer, OS_USEC_TO_CYCLES(GBPAK_INIT_WAIT), 0, 
	     &__osGbpakTimerQ, &__osGbpakTimerMsg);
  (void)osRecvMesg(&__osGbpakTimerQ, NULL, OS_MESG_BLOCK);

  pfs->queue = mq;
  pfs->status = PFS_GBPAK_INITIALIZED;
  pfs->channel = channel;
  pfs->activebank = OS_GBPAK_ID;
  pfs->banks = 0xff;		/* GB BANK */
  pfs->version = 255;		/* Memory Controller version */
  pfs->dir_size = 255;		/* RAM Size */

#if	0
  pfs->label[0] = 1;		/* flag for GBPAK Init */
  pfs->label[1] = 0;		/* flag for SRAM Init */
  pfs->label[2] = 0xff;		/* GB ROM BANK */
  pfs->label[3] = 0xff;		/* GB SRAM BANK */
#endif
#ifdef	ROM_REG0_ERR_VERSION
  pfs->label[4] = 0xff;		/* flag for RAM ENABLE */
#endif

#if 0
#ifdef	ROM_REG0_ERR_VERSION
  for (i = 0 ; i < BLOCKSIZE ; temp[i++] = 0 ) ;
  if ((i = osGbpakReadWrite(pfs, OS_WRITE, 0, temp, 32)) != 0 ) {
    return(i);
  }  /* GB RAM ENABLE flag */
#endif
#endif

  return(0);
}