gbpakreadwrite.c
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*---------------------------------------------------------------------
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);
}