gbpakreadid.c
3.03 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*---------------------------------------------------------------------
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.1.1.2 $
* $Date: 2002/10/29 08:06:44 $
* $Source:
*
**************************************************************************/
#include "osint.h"
#include "controller.h"
s32
osGbpakReadId(OSPfs *pfs, OSGbpakId *id, u8 *status)
{
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);
}