gbpakreadid.c 3.08 KB
/*---------------------------------------------------------------------
        Copyright (C) 1997, Nintendo.
        
        File            gbpakreadid.c
        Coded    by     Koji Mitsunari. Apr  7, 1998.
        Modified by     Koji Mitsunari. Jun 24, 1999.
        Comments        Get ID of Gameboy cartridge
   
        $Id: 
   ---------------------------------------------------------------------*/

/**************************************************************************
 *
 *  $Revision: 1.2 $
 *  $Date: 2003/03/25 20:42:32 $
 *  $Source: 
 *
 **************************************************************************/

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

s32
osGbpakReadId(OSPfs *pfs, OSGbpakId *id, u8 *status)
{
#ifdef BBPLAYER
    return PFS_ERR_DEVICE;
#else
  s32	i, ret;
  u8	isum;
  u8	buf[0x60];
  u8	temp[BLOCKSIZE];
  
  static u8	nintendo[] = {
    0xce, 0xed, 0x66, 0x66,
    0xcc, 0x0d, 0x0, 0xb, 0x3, 0x73, 0x0, 0x83,
    0x0, 0xc, 0x0, 0xd, 0x0, 0x8, 0x11, 0x1f,
    0x88, 0x89, 0x00, 0x0e, 0xdc, 0xcc, 0x6e, 0xe6,
    0xdd, 0xdd, 0xd9, 0x99, 0xbb, 0xbb, 0x67, 0x63,
    0x6e, 0xe, 0xec, 0xcc, 0xdd, 0xdc, 0x99, 0x9f,
    0xbb, 0xb9, 0x33, 0x3e
  };
  static u8	mmc_type[] = {
    0,1,1,1,255,2,2,255,0,0,255,255,255,255,255,3,3,3,3,3
  };

  ret = osGbpakGetStatus(pfs, status);
  if (ret == PFS_ERR_NEW_GBCART) {
    ret = osGbpakGetStatus(pfs, status);
  }
  if (ret == PFS_ERR_NEW_GBCART) {
    return(PFS_ERR_CONTRFAIL);
  } else if (ret == 0) {
#if	1			/* Power ON  */
    if (!(*status & OS_GBPAK_POWER)) {
      if ((ret = osGbpakPower(pfs, OS_GBPAK_POWER_ON))!= 0) {
	return(ret);
      }
    }
#endif

    /* Read Gameboy cartridge ID area */
    ret = osGbpakReadWrite(pfs, OS_READ, 0x100, buf, 0x60);
    if (ret != 0 ) return(ret);

    /* Confirm if data have been read correctly */
    ret = osGbpakGetStatus(pfs, status);
    if (ret == PFS_ERR_NEW_GBCART) ret = PFS_ERR_CONTRFAIL;
    if (ret != 0) return(ret);

    if (!(*status & OS_GBPAK_RSTB_STATUS)) {
      return(PFS_ERR_CONTRFAIL);
    }

    /* Check Nintendo Character Data Area */
    if (bcmp(nintendo, (buf+4), sizeof(nintendo))) {
      for (i = 0 ; i < BLOCKSIZE ; temp[i++] = 0 );
      if ( (ret = osGbpakReadWrite(pfs, OS_WRITE,
				   0x6000, temp, BLOCKSIZE)) != 0 ) {
	return(ret);
      }
      ret = osGbpakReadWrite(pfs, OS_READ, 0x100, buf, 0x60);
      if ( (ret = osGbpakReadWrite(pfs, OS_READ,
				   0x100, buf, 0x60)) != 0 ) {
	return(ret);
      }
      ret = osGbpakGetStatus(pfs, status);
      if (ret == PFS_ERR_NEW_GBCART) ret = PFS_ERR_CONTRFAIL;
      if (ret != 0) return(ret);

      if (bcmp(nintendo, (buf+4), sizeof(nintendo))) {
	return(PFS_ERR_CONTRFAIL);
      }
    }

#if	1			/* for DEBUG */
    /* Check inverted check sum */
    for (i = 0x34, isum =0  ; i <= 0x4d ; i++){
      isum += buf[i];
    }
    if (((isum + 0x19)&0xff) != 0 ){
      return(PFS_ERR_CONTRFAIL);
    }
#endif
      
    bcopy(buf, id, OS_GBPAK_ROM_ID_SIZE);

    if (id->cart_type <= 0x13 ) {
      pfs->version = (int)(mmc_type[id->cart_type]);
    }
    pfs->dir_size = (int)(id->ram_size);
  }
  return(ret);
#endif
}