1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2005 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 extern int cpuDmaCount
;
26 int eepromMode
= EEPROM_IDLE
;
29 int eepromAddress
= 0;
30 u8 eepromData
[0x2000];
32 bool eepromInUse
= false;
35 variable_desc eepromSaveData
[] = {
36 { &eepromMode
, sizeof(int) },
37 { &eepromByte
, sizeof(int) },
38 { &eepromBits
, sizeof(int) },
39 { &eepromAddress
, sizeof(int) },
40 { &eepromInUse
, sizeof(bool) },
41 { &eepromData
[0], 512 },
42 { &eepromBuffer
[0], 16 },
48 memset(eepromData
, 255, sizeof(eepromData
));
53 eepromMode
= EEPROM_IDLE
;
61 void eepromSaveGame(gzFile gzFile
)
63 utilWriteData(gzFile
, eepromSaveData
);
64 utilWriteInt(gzFile
, eepromSize
);
65 utilGzWrite(gzFile
, eepromData
, 0x2000);
68 void eepromReadGame(gzFile gzFile
, int version
)
70 utilReadData(gzFile
, eepromSaveData
);
71 if(version
>= SAVE_GAME_VERSION_3
) {
72 eepromSize
= utilReadInt(gzFile
);
73 utilGzRead(gzFile
, eepromData
, 0x2000);
75 // prior to 0.7.1, only 4K EEPROM was supported
81 int eepromRead(u32 unused
/* address */)
85 case EEPROM_READADDRESS
:
86 case EEPROM_WRITEDATA
:
92 eepromMode
= EEPROM_READDATA2
;
98 case EEPROM_READDATA2
:
101 int address
= eepromAddress
<< 3;
102 int mask
= 1 << (7 - (eepromBits
& 7));
103 data
= (eepromData
[address
+eepromByte
] & mask
) ? 1 : 0;
105 if((eepromBits
& 7) == 0)
107 if(eepromBits
== 0x40)
108 eepromMode
= EEPROM_IDLE
;
117 void eepromWrite(u32 unused
/* address */, u8 value
)
126 eepromBuffer
[eepromByte
] = bit
;
127 eepromMode
= EEPROM_READADDRESS
;
129 case EEPROM_READADDRESS
:
130 eepromBuffer
[eepromByte
] <<= 1;
131 eepromBuffer
[eepromByte
] |= bit
;
133 if((eepromBits
& 7) == 0) {
136 if(cpuDmaCount
== 0x11 || cpuDmaCount
== 0x51) {
137 if(eepromBits
== 0x11) {
140 eepromAddress
= ((eepromBuffer
[0] & 0x3F) << 8) |
141 ((eepromBuffer
[1] & 0xFF));
142 if(!(eepromBuffer
[0] & 0x40)) {
143 eepromBuffer
[0] = bit
;
146 eepromMode
= EEPROM_WRITEDATA
;
148 eepromMode
= EEPROM_READDATA
;
154 if(eepromBits
== 9) {
156 eepromAddress
= (eepromBuffer
[0] & 0x3F);
157 if(!(eepromBuffer
[0] & 0x40)) {
158 eepromBuffer
[0] = bit
;
161 eepromMode
= EEPROM_WRITEDATA
;
163 eepromMode
= EEPROM_READDATA
;
170 case EEPROM_READDATA
:
171 case EEPROM_READDATA2
:
172 // should we reset here?
173 eepromMode
= EEPROM_IDLE
;
175 case EEPROM_WRITEDATA
:
176 eepromBuffer
[eepromByte
] <<= 1;
177 eepromBuffer
[eepromByte
] |= bit
;
179 if((eepromBits
& 7) == 0) {
182 if(eepromBits
== 0x40) {
185 for(int i
= 0; i
< 8; i
++) {
186 eepromData
[(eepromAddress
<< 3) + i
] = eepromBuffer
[i
];
188 systemSaveUpdateCounter
= SYSTEM_SAVE_UPDATED
;
189 } else if(eepromBits
== 0x41) {
190 eepromMode
= EEPROM_IDLE
;