Genesis-Plus-GX/source/cart_hw/md_eeprom.c
ekeeke31 62f1204476 ----------------------
Genesis Plus GX 1.6.0 
----------------------

[Core/Sound]
---------------
* added YM2413 emulation in Master System compatibility mode.
* fixed SN76489 noise boost initialization.
* minor YM2612 core optimizations.

[Core/VDP]
---------------
* added accurate emulation of SG-1000, Master System (315-5124, 315-5246) & Game Gear VDP.
* added support for all TMS9918 rendering modes.
* improved Mega Drive VDP timings accuracy in Master System Compatibility mode.
* fixed color palette initialization.
* fixed shifted sprites rendering in Mode 4.
* modified pixel rendering support (pixel depth is now forced at compilation time).

[Core/CPU]
---------------
* optimized 68k core (rewrote 68k interrupt handling, removed multiple CPU types support & unused code) for 5~8% speed improvment

[Core/IO]
---------------
* added accurate emulation of Master System (315-5216, 315-5237, 315-5297) & Game Gear I/O controllers.
* added Terebi Oekaki tablet emulation.
* improved Mouse emulation (fixes mouse support in Cannon Fodder).
* improved Justifier emulation (fixes gun support in Lethal Enforcers 2).
* improved 6-Buttons control pad emulation (fixes Duke Nukem 3D)
* modified lightgun emulation to use common key inputs for all devices.
* 2-buttons controller is now picked by default for Master System games.

[Core/MD]
---------------
* added copy-protection hardware emulation for some new dumped games (Tiny Toon Adventures 3, Mighty Morphin Power Rangers & The Battle of Red Cliffs).
* added Game Toshokan in EEPROM database (verified on real cartridge).
* fixed Micro Machines 2 - Turbo Tournament EEPROM size (verified on real cartridge).
* modified SRAM banswitch hardware emulation to be more compatible with some hacks.

[Core/MS]
---------------
* added Cyborg Z to Korean mapper database.

[Core/GG]
---------------
* added 93C46 EEPROM emulation (Majors Pro Baseball, World Series Baseball & World Series Baseball 95).

[Core/General]
---------------
* added support for .mdx ROM format.
* added Game Gear & SG-1000 ROM support.
* added accurate emulation of SG-1000, Master System (I, II) & Game Gear hardware models for 100% compatibility.
* updated to new Genesis Plus license (see http://cgfm2.emuviews.com/)
* removed DOS port
* various code cleanup.

[Gamecube/Wii]
---------------
* IMPORTANT: cheats, screenshots & save files are now stored in console-specific directories (ex: /snaps/md, /cheats/ms, /saves/gg, ...)
* added 8-bit Action Replay & Game Genie codes support (for Master System & Game Gear games).
* improved audio/video synchronization for PAL games in 50Hz TV modes (now use VSYNC like NTSC games in 60hz modes).
* improved gun cursor positioning accuracy.
* improved horizontal scaling & screenshots rendering in H32 mode.
* fixed a bug with ROM file extension handling that would affect cheats, snapshots, sram & savestate files.
* removed ARAM/injected ROM support (unused).
* removed WPAD_ and PAD_ update from VSYNC callback.
* increased GCC inlining limits for some speed improvment.
* compiled with devkitPPC r24 & libogc 1.8.7.
2011-08-07 17:49:46 +00:00

555 lines
18 KiB
C

/****************************************************************************
* Genesis Plus
* I2C Serial EEPROM (24Cxx) support
*
* Copyright (C) 2007-2011 Eke-Eke (Genesis Plus GX)
*
* Redistribution and use of this code or any derivative works are permitted
* provided that the following conditions are met:
*
* - Redistributions may not be sold, nor may they be used in a commercial
* product or activity.
*
* - Redistributions that are modified from the original source must include the
* complete source code, including the source code for all components used by a
* binary built from the modified sources. However, as a special exception, the
* source code distributed need not include anything that is normally distributed
* (in either source or binary form) with the major components (compiler, kernel,
* and so on) of the operating system on which the executable runs, unless that
* component itself accompanies the executable.
*
* - Redistributions must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************************/
#include "shared.h"
#define GAME_CNT 30
T_EEPROM_24C md_eeprom;
typedef struct
{
char game_id[16];
uint16 chk;
T_EEPROM_TYPE type;
} T_GAME_ENTRY;
static const T_GAME_ENTRY database[GAME_CNT] =
{
/* 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 */
/* 24C04 */
{{"T-81406" }, 0, {8, 0x1FF, 0x1FF, 0x200001, 0x200001, 0x200000, 0, 0, 0}}, /* NBA Jam TE */
/* 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 */
/* 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 */
/* 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 */
{{"00054503-00"}, 0, {7, 0x7F, 0x7F, 0x200001, 0x200001, 0x200001, 0, 0, 1}}, /* Game Toshokan */
/* CODEMASTERS mapper */
/* 24C08 */
{{"T-120106" }, 0, {8, 0x3FF, 0x3FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Brian Lara Cricket */
{{"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 */
{{"T-120096" }, 0, {8, 0x7FF, 0x7FF, 0x300000, 0x380001, 0x300000, 0, 7, 1}}, /* Micro Machines 2 - Turbo Tournament (E) */
{{"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 */
};
static void eeprom_update(void);
static unsigned char eeprom_out(void);
void md_eeprom_init()
{
int i = 0;
/* initialize eeprom */
memset(&md_eeprom, 0, sizeof(T_EEPROM_24C));
md_eeprom.sda = md_eeprom.old_sda = 1;
md_eeprom.scl = md_eeprom.old_scl = 1;
md_eeprom.state = STAND_BY;
/* no eeprom by default */
sram.custom = 0;
/* look into game database */
while (i<GAME_CNT)
{
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(&md_eeprom.type, &database[i].type, sizeof(T_EEPROM_TYPE));
return;
}
}
i++;
}
/* Game not found in database but header seems to indicate it uses EEPROM */
if (sram.detected)
{
if ((sram.end - sram.start) < 2)
{
/* set SEGA mapper as default */
sram.custom = 1;
memcpy(&md_eeprom.type, &database[9].type, sizeof(T_EEPROM_TYPE));
}
}
}
unsigned int md_eeprom_read_byte(unsigned int address)
{
if (address == md_eeprom.type.sda_out_adr)
{
return eeprom_out();
}
return READ_BYTE(cart.rom, address);
}
unsigned int md_eeprom_read_word(unsigned int address)
{
if (address == md_eeprom.type.sda_out_adr)
{
return (eeprom_out() << 8);
}
if (address == (md_eeprom.type.sda_out_adr ^ 1))
{
return eeprom_out();
}
return *(uint16 *)(cart.rom + address);
}
void md_eeprom_write_byte(unsigned int address, unsigned int data)
{
int do_update = 0;
if (address == md_eeprom.type.sda_in_adr)
{
md_eeprom.sda = (data >> md_eeprom.type.sda_in_bit) & 1;
do_update = 1;
}
if (address == md_eeprom.type.scl_adr)
{
md_eeprom.scl = (data >> md_eeprom.type.scl_bit) & 1;
do_update = 1;
}
if (do_update)
{
eeprom_update();
return;
}
m68k_unused_8_w(address, data);
}
void md_eeprom_write_word(unsigned int address, unsigned int data)
{
int do_update = 0;
if (address == md_eeprom.type.sda_in_adr)
{
md_eeprom.sda = (data >> (8 + md_eeprom.type.sda_in_bit)) & 1;
do_update = 1;
}
else if (address == (md_eeprom.type.sda_in_adr ^1))
{
md_eeprom.sda = (data >> md_eeprom.type.sda_in_bit) & 1;
do_update = 1;
}
if (address == md_eeprom.type.scl_adr)
{
md_eeprom.scl = (data >> (8 + md_eeprom.type.scl_bit)) & 1;
do_update = 1;
}
else if (address == (md_eeprom.type.scl_adr ^1))
{
md_eeprom.scl = (data >> md_eeprom.type.scl_bit) & 1;
do_update = 1;
}
if (do_update)
{
eeprom_update();
return;
}
m68k_unused_16_w(address, data);
}
static inline void Detect_START()
{
if (md_eeprom.old_scl && md_eeprom.scl)
{
if (md_eeprom.old_sda && !md_eeprom.sda)
{
md_eeprom.cycles = 0;
md_eeprom.slave_mask = 0;
if (md_eeprom.type.address_bits == 7)
{
md_eeprom.word_address = 0;
md_eeprom.state = GET_WORD_ADR_7BITS;
}
else md_eeprom.state = GET_SLAVE_ADR;
}
}
}
static inline void Detect_STOP()
{
if (md_eeprom.old_scl && md_eeprom.scl)
{
if (!md_eeprom.old_sda && md_eeprom.sda)
{
md_eeprom.state = STAND_BY;
}
}
}
static void eeprom_update(void)
{
/* EEPROM current state */
switch (md_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 (!md_eeprom.old_scl && md_eeprom.scl)
{
if (md_eeprom.cycles == 0) md_eeprom.cycles ++;
}
/* look for SCL HIGH to LOW transition */
if (md_eeprom.old_scl && !md_eeprom.scl && (md_eeprom.cycles > 0))
{
if (md_eeprom.cycles < 8)
{
md_eeprom.word_address |= (md_eeprom.old_sda << (7 - md_eeprom.cycles));
}
else if (md_eeprom.cycles == 8)
{
md_eeprom.rw = md_eeprom.old_sda;
}
else
{ /* ACK CYCLE */
md_eeprom.cycles = 0;
md_eeprom.word_address &= md_eeprom.type.size_mask;
md_eeprom.state = md_eeprom.rw ? READ_DATA : WRITE_DATA;
}
md_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 (!md_eeprom.old_scl && md_eeprom.scl)
{
if (md_eeprom.cycles == 0) md_eeprom.cycles ++;
}
/* look for SCL HIGH to LOW transition */
if (md_eeprom.old_scl && !md_eeprom.scl && (md_eeprom.cycles > 0))
{
if ((md_eeprom.cycles > 4) && (md_eeprom.cycles <8))
{
if ((md_eeprom.type.address_bits == 16) ||
(md_eeprom.type.size_mask < (1 << (15 - md_eeprom.cycles))))
{
/* this is a SLAVE ADDRESS bit */
md_eeprom.slave_mask |= (md_eeprom.old_sda << (7 - md_eeprom.cycles));
}
else
{
/* this is a WORD ADDRESS high bit */
if (md_eeprom.old_sda) md_eeprom.word_address |= (1 << (15 - md_eeprom.cycles));
else md_eeprom.word_address &= ~(1 << (15 - md_eeprom.cycles));
}
}
else if (md_eeprom.cycles == 8) md_eeprom.rw = md_eeprom.old_sda;
else if (md_eeprom.cycles > 8)
{
/* ACK CYCLE */
md_eeprom.cycles = 0;
if (md_eeprom.type.address_bits == 16)
{
/* two ADDRESS bytes */
md_eeprom.state = md_eeprom.rw ? READ_DATA : GET_WORD_ADR_HIGH;
md_eeprom.slave_mask <<= 16;
}
else
{
/* one ADDRESS byte */
md_eeprom.state = md_eeprom.rw ? READ_DATA : GET_WORD_ADR_LOW;
md_eeprom.slave_mask <<= 8;
}
}
md_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 (md_eeprom.old_scl && !md_eeprom.scl)
{
if (md_eeprom.cycles < 9)
{
if ((md_eeprom.type.size_mask + 1) < (1 << (17 - md_eeprom.cycles)))
{
/* ignored bit: slave mask should be right-shifted by one */
md_eeprom.slave_mask >>= 1;
}
else
{
/* this is a WORD ADDRESS high bit */
if (md_eeprom.old_sda) md_eeprom.word_address |= (1 << (16 - md_eeprom.cycles));
else md_eeprom.word_address &= ~(1 << (16 - md_eeprom.cycles));
}
md_eeprom.cycles ++;
}
else
{
/* ACK CYCLE */
md_eeprom.cycles = 1;
md_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 (md_eeprom.old_scl && !md_eeprom.scl)
{
if (md_eeprom.cycles < 9)
{
if ((md_eeprom.type.size_mask + 1) < (1 << (9 - md_eeprom.cycles)))
{
/* ignored bit (X24C01): slave mask should be right-shifted by one */
md_eeprom.slave_mask >>= 1;
}
else
{
/* this is a WORD ADDRESS high bit */
if (md_eeprom.old_sda) md_eeprom.word_address |= (1 << (8 - md_eeprom.cycles));
else md_eeprom.word_address &= ~(1 << (8 - md_eeprom.cycles));
}
md_eeprom.cycles ++;
}
else
{
/* ACK CYCLE */
md_eeprom.cycles = 1;
md_eeprom.word_address &= md_eeprom.type.size_mask;
md_eeprom.state = WRITE_DATA;
}
}
break;
}
/*
* Read Cycle
*/
case READ_DATA:
{
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (md_eeprom.old_scl && !md_eeprom.scl)
{
if (md_eeprom.cycles < 9) md_eeprom.cycles ++;
else
{
md_eeprom.cycles = 1;
/* ACK not received */
if (md_eeprom.old_sda) md_eeprom.state = WAIT_STOP;
}
}
break;
}
/*
* Write Cycle
*/
case WRITE_DATA:
{
Detect_START();
Detect_STOP();
/* look for SCL HIGH to LOW transition */
if (md_eeprom.old_scl && !md_eeprom.scl)
{
if (md_eeprom.cycles < 9)
{
/* Write DATA bits (max 64kBytes) */
uint16 sram_address = (md_eeprom.slave_mask | md_eeprom.word_address) & 0xFFFF;
if (md_eeprom.old_sda) sram.sram[sram_address] |= (1 << (8 - md_eeprom.cycles));
else sram.sram[sram_address] &= ~(1 << (8 - md_eeprom.cycles));
if (md_eeprom.cycles == 8)
{
/* WORD ADDRESS is incremented (roll up at maximum pagesize) */
md_eeprom.word_address = (md_eeprom.word_address & (0xFFFF - md_eeprom.type.pagewrite_mask)) |
((md_eeprom.word_address + 1) & md_eeprom.type.pagewrite_mask);
}
md_eeprom.cycles ++;
}
else md_eeprom.cycles = 1; /* ACK cycle */
}
break;
}
}
md_eeprom.old_scl = md_eeprom.scl;
md_eeprom.old_sda = md_eeprom.sda;
}
static unsigned char eeprom_out(void)
{
uint8 sda_out = md_eeprom.sda;
/* EEPROM state */
switch (md_eeprom.state)
{
case READ_DATA:
{
if (md_eeprom.cycles < 9)
{
/* Return DATA bits (max 64kBytes) */
uint16 sram_address = (md_eeprom.slave_mask | md_eeprom.word_address) & 0xffff;
sda_out = (sram.sram[sram_address] >> (8 - md_eeprom.cycles)) & 1;
if (md_eeprom.cycles == 8)
{
/* WORD ADDRESS is incremented (roll up at maximum array size) */
md_eeprom.word_address ++;
md_eeprom.word_address &= md_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 (md_eeprom.cycles == 9) sda_out = 0;
break;
}
default:
{
break;
}
}
return (sda_out << md_eeprom.type.sda_out_bit);
}