Genesis-Plus-GX/source/cart_hw/eeprom.c

462 lines
15 KiB
C
Raw Normal View History

2008-08-07 14:26:07 +02:00
/****************************************************************************
* Genesis Plus
2010-02-19 13:37:39 +01:00
* I2C EEPROM support
2008-08-07 14:26:07 +02:00
*
* Copyright (C) 2007, 2008, 2009 Eke-Eke (GCN/Wii port)
2008-08-07 14:26:07 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "shared.h"
#define GAME_CNT 29
T_EEPROM eeprom;
2008-08-07 14:26:07 +02:00
typedef struct
{
char game_id[16];
2008-12-11 18:38:29 +01:00
uint16 chk;
T_EEPROM_TYPE type;
2008-08-07 14:26:07 +02:00
} T_GAME_ENTRY;
static const T_GAME_ENTRY database[GAME_CNT] =
2008-08-07 14:26:07 +02:00
{
2008-12-11 18:38:29 +01:00
/* ACCLAIM mappers */
/* 24C02 (old mapper) */
{{"T-081326" }, 0, {8, 0xFF, 0xFF, 0x200001, 0x200001, 0x200001, 0, 1, 1}}, /* NBA Jam (UE) */
{{"T-81033" }, 0, {8, 0xFF, 0xFF, 0x200001, 0x200001, 0x200001, 0, 1, 1}}, /* NBA Jam (J) */
/* 24C02 */
{{"T-081276" }, 0, {8, 0xFF, 0xFF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* NFL Quarterback Club */
[Core/VDP] --------------- * added support for Master System compatibility mode (Z80 ports access mode), incl. Mode 5 rendering. * added Mode 4 rendering for both Genesis & Master System modes. * added alternate BG planes rendering functions (should be faster on PPC architectures). [Core/IO] --------------- * added support for Master System compatibility mode (Z80 ports access mode). * added Master System peripherals emulation (Control Pad, Paddle, Sports Pad & Light Phaser). * added XE-1AP (analog controller) emulation. * added Activator emulation. [Core/Extra] --------------- * added support for all known Master System cartridge mappers. * added copy-protection hardware emulation for a few MD unlicensed games: fixes 777 Casino (crash when talking to bunny girls). (NB: most of those unlicensed games seem to have been already patched by ROM dumpers, main purpose is documenting them) * added support for Top Shooter arcade board controller. (A=Shoot, B=Bet, C/RIGHT=Coins, START=Start, hold UP on startup to enter service mode) * improved King of Fighters 98 mapper emulation (registers address decoding is now 100% accurate) * fixed Game Genie when several codes affect same ROM address. * fixed EEPROM types for Brian Lara Cricket & NBA Jam TE (verified on real cartridges) [Core/General] --------------- * added Master System compatibility mode emulation (automatically enabled when loading ROM file with .sms extension). * improved savestate stability & compatibility (support for old 1.4.x savestates is preserved) * various code cleanup & comments. [Gamecube/Wii] --------------- * fixed cheat codes handling when several codes affect same ROM address. * improved input controller detection on menu exit. * improved key remapping dialog box to match emulated device * changed Menu key for Gamecube controller to allow MODE button mapping * fixed DVD not being unmounted on swap (memory leak) [Wii only] --------------- * added USB mouse support for Sega Mouse emulation * compiled with latest libogc: improves USB compatibility & fixes stability issues with Wiimotes.
2011-04-01 00:11:05 +02:00
/* 24C04 */
{{"T-81406" }, 0, {8, 0x1FF, 0x1FF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* NBA Jam TE */
2008-12-11 18:38:29 +01:00
/* 24C16 */
{{"T-081586" }, 0, {8, 0x7FF, 0x7FF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* NFL Quarterback Club '96 */
/* 24C65 */
{{"T-81576" }, 0, {16, 0x1FFF, 0x1FFF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* College Slam */
{{"T-81476" }, 0, {16, 0x1FFF, 0x1FFF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* Frank Thomas Big Hurt Baseball */
2008-12-11 18:38:29 +01:00
/* EA mapper (24C01 only) */
{{"T-50176" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 7, 7, 6}}, /* Rings of Power */
{{"T-50396" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 7, 7, 6}}, /* NHLPA Hockey 93 */
{{"T-50446" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 7, 7, 6}}, /* John Madden Football 93 */
{{"T-50516" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 7, 7, 6}}, /* John Madden Football 93 (Championship Ed.) */
{{"T-50606" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 7, 7, 6}}, /* Bill Walsh College Football */
2008-12-11 18:38:29 +01:00
/* SEGA mapper (24C01 only) */
{{"T-12046" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Megaman - The Wily Wars */
{{"T-12053" }, 0xEA80, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Rockman Mega World (J) [A] */
{{"MK-1215" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Evander 'Real Deal' Holyfield's Boxing */
{{"MK-1228" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Greatest Heavyweights of the Ring (U) */
{{"G-5538" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Greatest Heavyweights of the Ring (J) */
{{"PR-1993" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Greatest Heavyweights of the Ring (E) */
{{"G-4060" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Wonderboy in Monster World */
{{"00001211-00"}, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Sports Talk Baseball */
{{"00004076-00"}, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Honoo no Toukyuuji Dodge Danpei */
{{"G-4524" }, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Ninja Burai Densetsu */
2008-12-11 18:38:29 +01:00
/* CODEMASTERS mapper */
/* 24C08 */
[Core/VDP] --------------- * added support for Master System compatibility mode (Z80 ports access mode), incl. Mode 5 rendering. * added Mode 4 rendering for both Genesis & Master System modes. * added alternate BG planes rendering functions (should be faster on PPC architectures). [Core/IO] --------------- * added support for Master System compatibility mode (Z80 ports access mode). * added Master System peripherals emulation (Control Pad, Paddle, Sports Pad & Light Phaser). * added XE-1AP (analog controller) emulation. * added Activator emulation. [Core/Extra] --------------- * added support for all known Master System cartridge mappers. * added copy-protection hardware emulation for a few MD unlicensed games: fixes 777 Casino (crash when talking to bunny girls). (NB: most of those unlicensed games seem to have been already patched by ROM dumpers, main purpose is documenting them) * added support for Top Shooter arcade board controller. (A=Shoot, B=Bet, C/RIGHT=Coins, START=Start, hold UP on startup to enter service mode) * improved King of Fighters 98 mapper emulation (registers address decoding is now 100% accurate) * fixed Game Genie when several codes affect same ROM address. * fixed EEPROM types for Brian Lara Cricket & NBA Jam TE (verified on real cartridges) [Core/General] --------------- * added Master System compatibility mode emulation (automatically enabled when loading ROM file with .sms extension). * improved savestate stability & compatibility (support for old 1.4.x savestates is preserved) * various code cleanup & comments. [Gamecube/Wii] --------------- * fixed cheat codes handling when several codes affect same ROM address. * improved input controller detection on menu exit. * improved key remapping dialog box to match emulated device * changed Menu key for Gamecube controller to allow MODE button mapping * fixed DVD not being unmounted on swap (memory leak) [Wii only] --------------- * added USB mouse support for Sega Mouse emulation * compiled with latest libogc: improves USB compatibility & fixes stability issues with Wiimotes.
2011-04-01 00:11:05 +02:00
{{"T-120106" }, 0, {8, 0x3FF, 0x3FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Brian Lara Cricket */
2008-12-11 18:38:29 +01:00
{{"T-120096" }, 0, {8, 0x3FF, 0x3FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines 2 - Turbo Tournament (E) */
{{"00000000-00"}, 0x168B, {8, 0x3FF, 0x3FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines Military */
{{"00000000-00"}, 0xCEE0, {8, 0x3FF, 0x3FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines Military (Bad)*/
/* 24C16 */
{{"00000000-00"}, 0x165E, {8, 0x7FF, 0x7FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines Turbo Tournament 96 */
{{"00000000-00"}, 0x2C41, {8, 0x7FF, 0x7FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines Turbo Tournament 96 (Bad)*/
/* 24C65 */
{{"T-120146-50"}, 0, {16, 0x1FFF, 0x1FFF, 0x300000, 0x380001, 0x300000, 0, 7, 1}} /* Brian Lara Cricket 96, Shane Warne Cricket */
2008-08-07 14:26:07 +02:00
};
void eeprom_init()
{
int i = 0;
2008-12-11 18:38:29 +01:00
/* initialize eeprom */
memset(&eeprom, 0, sizeof(T_EEPROM));
eeprom.sda = eeprom.old_sda = 1;
eeprom.scl = eeprom.old_scl = 1;
eeprom.state = STAND_BY;
/* no eeprom by default */
2008-12-11 18:38:29 +01:00
sram.custom = 0;
/* look into game database */
while (i<GAME_CNT)
2008-12-11 18:38:29 +01:00
{
if (strstr(rominfo.product,database[i].game_id) != NULL)
{
/* additional check (Micro Machines, Rockman Mega World) */
if ((database[i].chk == 0) || (database[i].chk == rominfo.checksum))
{
sram.custom = 1;
sram.on = 1;
memcpy(&eeprom.type, &database[i].type, sizeof(T_EEPROM_TYPE));
return;
2008-12-11 18:38:29 +01:00
}
}
i++;
}
/* Game not found in database but header seems to indicate it uses EEPROM */
if (sram.detected)
2008-12-11 18:38:29 +01:00
{
2008-08-07 14:26:07 +02:00
if ((sram.end - sram.start) < 2)
2008-12-11 18:38:29 +01:00
{
/* set SEGA mapper as default */
sram.custom = 1;
2008-12-11 18:38:29 +01:00
memcpy(&eeprom.type, &database[9].type, sizeof(T_EEPROM_TYPE));
}
}
2008-08-07 14:26:07 +02:00
}
static inline void Detect_START()
{
2008-12-11 18:38:29 +01:00
if (eeprom.old_scl && eeprom.scl)
{
if (eeprom.old_sda && !eeprom.sda)
{
eeprom.cycles = 0;
eeprom.slave_mask = 0;
if (eeprom.type.address_bits == 7)
{
eeprom.word_address = 0;
eeprom.state = GET_WORD_ADR_7BITS;
}
else eeprom.state = GET_SLAVE_ADR;
}
}
2008-08-07 14:26:07 +02:00
}
static inline void Detect_STOP()
{
2008-12-11 18:38:29 +01:00
if (eeprom.old_scl && eeprom.scl)
{
if (!eeprom.old_sda && eeprom.sda)
{
eeprom.state = STAND_BY;
}
}
2008-08-07 14:26:07 +02:00
}
2010-07-03 01:03:05 +02:00
void eeprom_write(unsigned int address, unsigned int data, int word_access)
2008-08-07 14:26:07 +02:00
{
2008-12-11 18:38:29 +01:00
/* decode SCL and SDA value */
2008-08-07 14:26:07 +02:00
if (word_access)
{
/* 16-bits access */
2010-07-03 01:03:05 +02:00
if (eeprom.type.sda_in_adr == address) eeprom.sda = (data >> (8 + eeprom.type.sda_in_bit)) & 1; /* MSB */
else if (eeprom.type.sda_in_adr == (address | 1)) eeprom.sda = (data >> eeprom.type.sda_in_bit) & 1; /* LSB */
2008-08-07 14:26:07 +02:00
else eeprom.sda = eeprom.old_sda;
2010-07-03 01:03:05 +02:00
if (eeprom.type.scl_adr == address) eeprom.scl = (data >> (8 + eeprom.type.scl_bit)) & 1; /* MSB */
else if (eeprom.type.scl_adr == (address | 1)) eeprom.scl = (data >> eeprom.type.scl_bit) & 1; /* LSB */
2008-08-07 14:26:07 +02:00
else eeprom.scl = eeprom.old_scl;
}
else
{
2010-07-03 01:03:05 +02:00
if (eeprom.type.sda_in_adr == address) eeprom.sda = (data >> eeprom.type.sda_in_bit) & 1;
else eeprom.sda = eeprom.old_sda;
2008-08-07 14:26:07 +02:00
2010-07-03 01:03:05 +02:00
if (eeprom.type.scl_adr == address) eeprom.scl = (data >> eeprom.type.scl_bit) & 1;
else eeprom.scl = eeprom.old_scl;
2008-08-07 14:26:07 +02:00
}
2008-12-11 18:38:29 +01:00
/* EEPROM current state */
switch (eeprom.state)
{
/* Standby Mode */
case STAND_BY:
Detect_START();
Detect_STOP();
break;
/* Suspended Mode */
case WAIT_STOP:
Detect_STOP();
break;
/* Get Word Address 7 bits: MODE-1 only (24C01)
* and R/W bit
*/
case GET_WORD_ADR_7BITS:
Detect_START();
Detect_STOP();
/* look for SCL LOW to HIGH transition */
if (!eeprom.old_scl && eeprom.scl)
{
if (eeprom.cycles == 0) eeprom.cycles ++;
}
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl && (eeprom.cycles > 0))
{
if (eeprom.cycles < 8)
2008-08-07 14:26:07 +02:00
{
eeprom.word_address |= (eeprom.old_sda << (7 - eeprom.cycles));
2008-12-11 18:38:29 +01:00
}
2008-08-07 14:26:07 +02:00
else if (eeprom.cycles == 8)
{
eeprom.rw = eeprom.old_sda;
2008-12-11 18:38:29 +01:00
}
else
{ /* ACK CYCLE */
eeprom.cycles = 0;
eeprom.word_address &= eeprom.type.size_mask;
eeprom.state = eeprom.rw ? READ_DATA : WRITE_DATA;
}
eeprom.cycles ++;
}
break;
/* Get Slave Address (3bits) : MODE-2 & MODE-3 only (24C01 - 24C512) (0-3bits, depending on the array size)
* or/and Word Address MSB: MODE-2 only (24C04 - 24C16) (0-3bits, depending on the array size)
* and R/W bit
*/
case GET_SLAVE_ADR:
Detect_START();
Detect_STOP();
/* look for SCL LOW to HIGH transition */
if (!eeprom.old_scl && eeprom.scl)
{
if (eeprom.cycles == 0) eeprom.cycles ++;
}
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl && (eeprom.cycles > 0))
{
if ((eeprom.cycles > 4) && (eeprom.cycles <8))
{
if ((eeprom.type.address_bits == 16) ||
(eeprom.type.size_mask < (1 << (15 - eeprom.cycles))))
{
/* this is a SLAVE ADDRESS bit */
eeprom.slave_mask |= (eeprom.old_sda << (7 - eeprom.cycles));
}
else
{
/* this is a WORD ADDRESS high bit */
if (eeprom.old_sda) eeprom.word_address |= (1 << (15 - eeprom.cycles));
else eeprom.word_address &= ~(1 << (15 - eeprom.cycles));
}
}
else if (eeprom.cycles == 8) eeprom.rw = eeprom.old_sda;
else if (eeprom.cycles > 8)
{
/* ACK CYCLE */
eeprom.cycles = 0;
if (eeprom.type.address_bits == 16)
{
/* two ADDRESS bytes */
eeprom.state = eeprom.rw ? READ_DATA : GET_WORD_ADR_HIGH;
eeprom.slave_mask <<= 16;
}
else
{
/* one ADDRESS byte */
eeprom.state = eeprom.rw ? READ_DATA : GET_WORD_ADR_LOW;
eeprom.slave_mask <<= 8;
}
}
eeprom.cycles ++;
}
break;
/* Get Word Address MSB (4-8bits depending on the array size)
* MODE-3 only (24C32 - 24C512)
*/
case GET_WORD_ADR_HIGH:
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl)
{
if (eeprom.cycles < 9)
{
if ((eeprom.type.size_mask + 1) < (1 << (17 - eeprom.cycles)))
{
/* ignored bit: slave mask should be right-shifted by one */
eeprom.slave_mask >>= 1;
}
else
{
/* this is a WORD ADDRESS high bit */
if (eeprom.old_sda) eeprom.word_address |= (1 << (16 - eeprom.cycles));
else eeprom.word_address &= ~(1 << (16 - eeprom.cycles));
}
eeprom.cycles ++;
}
else
{
/* ACK CYCLE */
eeprom.cycles = 1;
eeprom.state = GET_WORD_ADR_LOW;
}
}
break;
/* Get Word Address LSB: 7bits (24C01) or 8bits (24C02-24C512)
* MODE-2 and MODE-3 only (24C01 - 24C512)
*/
case GET_WORD_ADR_LOW:
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl)
{
if (eeprom.cycles < 9)
{
if ((eeprom.type.size_mask + 1) < (1 << (9 - eeprom.cycles)))
{
/* ignored bit (X24C01): slave mask should be right-shifted by one */
eeprom.slave_mask >>= 1;
}
else
{
/* this is a WORD ADDRESS high bit */
if (eeprom.old_sda) eeprom.word_address |= (1 << (8 - eeprom.cycles));
else eeprom.word_address &= ~(1 << (8 - eeprom.cycles));
}
eeprom.cycles ++;
}
else
{
/* ACK CYCLE */
eeprom.cycles = 1;
eeprom.word_address &= eeprom.type.size_mask;
eeprom.state = WRITE_DATA;
}
}
break;
/*
* Read Cycle
*/
case READ_DATA:
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl)
{
if (eeprom.cycles < 9) eeprom.cycles ++;
else
{
eeprom.cycles = 1;
/* ACK not received */
if (eeprom.old_sda) eeprom.state = WAIT_STOP;
}
}
break;
/*
* Write Cycle
*/
case WRITE_DATA:
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (eeprom.old_scl && !eeprom.scl)
{
if (eeprom.cycles < 9)
{
/* Write DATA bits (max 64kBytes) */
uint16 sram_address = (eeprom.slave_mask | eeprom.word_address) & 0xFFFF;
if (eeprom.old_sda) sram.sram[sram_address] |= (1 << (8 - eeprom.cycles));
else sram.sram[sram_address] &= ~(1 << (8 - eeprom.cycles));
if (eeprom.cycles == 8)
{
/* WORD ADDRESS is incremented (roll up at maximum pagesize) */
eeprom.word_address = (eeprom.word_address & (0xFFFF - eeprom.type.pagewrite_mask)) |
((eeprom.word_address + 1) & eeprom.type.pagewrite_mask);
}
eeprom.cycles ++;
}
else eeprom.cycles = 1; /* ACK cycle */
}
break;
}
eeprom.old_scl = eeprom.scl;
eeprom.old_sda = eeprom.sda;
2008-08-07 14:26:07 +02:00
}
2010-07-03 01:03:05 +02:00
unsigned int eeprom_read(int word_access)
2008-08-07 14:26:07 +02:00
{
2008-12-11 18:38:29 +01:00
uint8 sda_out = eeprom.sda;
/* EEPROM state */
switch (eeprom.state)
{
case READ_DATA:
if (eeprom.cycles < 9)
{
/* Return DATA bits (max 64kBytes) */
uint16 sram_address = (eeprom.slave_mask | eeprom.word_address) & 0xffff;
sda_out = (sram.sram[sram_address] >> (8 - eeprom.cycles)) & 1;
if (eeprom.cycles == 8)
{
/* WORD ADDRESS is incremented (roll up at maximum array size) */
eeprom.word_address ++;
eeprom.word_address &= eeprom.type.size_mask;
}
}
break;
case GET_WORD_ADR_7BITS:
case GET_SLAVE_ADR:
case GET_WORD_ADR_HIGH:
case GET_WORD_ADR_LOW:
case WRITE_DATA:
if (eeprom.cycles == 9) sda_out = 0;
break;
default:
break;
}
2008-08-07 14:26:07 +02:00
/* memory access */
if (word_access)
{
/* 16-bits access */
if (eeprom.type.sda_out_adr & 1) return (sda_out << eeprom.type.sda_out_bit); /* LSB */
else return (sda_out << (eeprom.type.sda_out_bit + 8)); /* MSB */
}
else return (sda_out << eeprom.type.sda_out_bit);
}