gbpakreadwrite.c 2.33 KB
/*---------------------------------------------------------------------
        Copyright (C) 1997, Nintendo.
        
        File            gbpakreadwrite.c
        Coded    by     Koji Mitsunari. Oct  2, 1997.
        Modified by     Koji Mitsunari. Apr 16, 1998.
        Comments        Read/Write data of Gameboy cartridge
   
        $Id: 
   ---------------------------------------------------------------------*/

/**************************************************************************
 *
 *  $Revision: 1.1.1.2 $
 *  $Date: 2002/10/29 08:06:44 $
 *  $Source: 
 *
 **************************************************************************/

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

s32
osGbpakReadWrite(OSPfs *pfs, u16 flag, u16 address, u8 *buffer, u16 size)
{
  s32	i, ret;
  u8	bank  = (u8)(address >> 14);

#ifdef	ROM_REG0_ERR_VERSION

  u8	temp[BLOCKSIZE];

  if ( (pfs->label[4] != 0) && (bank != 2) ) {
    pfs->label[4] = 0;
    if ( (ret = __osGbpakSetBank(pfs, 0)) != 0 ) return(ret);
    for ( i = 0 ; i < BLOCKSIZE-1 ; temp[i++] = 11);
    if ( (ret = __osGbpakWrite32(pfs, (0xc000/BLOCKSIZE), temp)) != 0 ) {
      return(ret);
    }
  } else if  ( (pfs->label[4] != 1) && (bank == 2) ) {
    pfs->label[4] = 1;
    if ( (ret = __osGbpakSetBank(pfs, 0)) != 0 ) return(ret);
    for ( i = 0 ; i < BLOCKSIZE-1 ; temp[i++] = 0 );
    temp[BLOCKSIZE-1] = 0x0a;
    if ( (ret = __osGbpakWrite32(pfs, (0xc000/BLOCKSIZE), temp)) != 0 ) {
      return(ret);
    }
  }
#endif
  if ( bank != pfs->banks ) {
    if ( (ret = __osGbpakSetBank(pfs, bank)) != 0 ) {
      return(ret);
    }
  }

  if (size == 0)
    return 0;

  size = (size >> 5);
  address = ((address | 0xc000)>>5);

  if ( flag == OS_WRITE ) {
    for ( i = 0 ; i < size ; i++ , buffer += 32 ) {
      if ((ret =  __osGbpakWrite32(pfs, address, buffer)) != 0 ) {
	break;
      }
      if ( ++address >= 0x800 && i < size-1 ) {
	if (( ret = __osGbpakSetBank(pfs, ++bank)) != 0) {
	  break;
	}
	address = 0x600;
      }
    }
  } else {
    for ( i = 0 ; i < size ; i++ , buffer += 32 ) {
      if ((ret =  __osGbpakRead32(pfs, address, buffer)) != 0 ) {
	break;
      }
      if ( ++address >= 0x800 &&  i < size-1 ) {
	if (( ret = __osGbpakSetBank(pfs, ++bank)) != 0) {
	  break;
	}
	address = 0x600;
      }
    }
  }
  if (ret == PFS_ERR_NEW_PACK) {
    ret = PFS_ERR_CONTRFAIL;
  }
  return(ret);
}