/***************************************************************************************1* Genesis Plus2* Backup RAM support3*4* Copyright (C) 2007-2013 Eke-Eke (Genesis Plus GX)5*6* Redistribution and use of this code or any derivative works are permitted7* provided that the following conditions are met:8*9* - Redistributions may not be sold, nor may they be used in a commercial10* product or activity.11*12* - Redistributions that are modified from the original source must include the13* complete source code, including the source code for all components used by a14* binary built from the modified sources. However, as a special exception, the15* source code distributed need not include anything that is normally distributed16* (in either source or binary form) with the major components (compiler, kernel,17* and so on) of the operating system on which the executable runs, unless that18* component itself accompanies the executable.19*20* - Redistributions must reproduce the above copyright notice, this list of21* conditions and the following disclaimer in the documentation and/or other22* materials provided with the distribution.23*24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"25* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE28* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34* POSSIBILITY OF SUCH DAMAGE.35*36****************************************************************************************/3738#include "shared.h"39#include "eeprom_i2c.h"40#include "eeprom_spi.h"41#include "eeprom_93c.h"4243T_SRAM sram;4445/****************************************************************************46* A quick guide to external RAM on the Genesis47*48* The external RAM definition is held at offset 0x1b0 of the ROM header.49*50* 1B0h: dc.b 'RA', %1x1yz000, %abc0000051* 1B4h: dc.l RAM start address52* 1B8h: dc.l RAM end address53* x 1 for BACKUP (not volatile), 0 for volatile RAM54* yz 10 if even address only55* 11 if odd address only56* 00 if both even and odd address57* 01 others (serial EEPROM, RAM with 4-bit data bus, etc)58* abc 001 if SRAM59* 010 if EEPROM (serial or parallel)60* other values unused61*62* Assuming max. 64k backup RAM throughout63****************************************************************************/64void sram_init()65{66memset(&sram, 0, sizeof (T_SRAM));6768/* backup RAM data is stored above cartridge ROM area, at $800000-$80FFFF (max. 64K) */69if (cart.romsize > 0x800000) return;70sram.sram = malloc(0x10000);7172/* initialize Backup RAM */73memset(sram.sram, 0xFF, 0x10000);74//sram.crc = crc32(0, sram.sram, 0x10000);7576/* retrieve informations from header */77if ((READ_BYTE(cart.rom,0x1b0) == 0x52) && (READ_BYTE(cart.rom,0x1b1) == 0x41))78{79/* backup RAM detected */80sram.detected = 1;8182/* enable backup RAM */83sram.on = 1;8485/* retrieve backup RAM start & end addresses */86sram.start = READ_WORD_LONG(cart.rom, 0x1b4);87sram.end = READ_WORD_LONG(cart.rom, 0x1b8);8889/* autodetect games with wrong header infos */90if (strstr(rominfo.product,"T-26013") != NULL)91{92/* Psy-O-Blade (wrong header) */93sram.start = 0x200001;94sram.end = 0x203fff;95}9697/* fixe games indicating internal RAM as volatile external RAM (Feng Kuang Tao Hua Yuan) */98else if (sram.start == 0xff0000)99{100/* backup RAM should be disabled */101sram.on = 0;102}103104/* fixe other bad header informations */105else if ((sram.start > sram.end) || ((sram.end - sram.start) >= 0x10000))106{107sram.end = sram.start + 0xffff;108}109}110else111{112/* autodetect games with missing header infos */113if (strstr(rominfo.product,"T-50086") != NULL)114{115/* PGA Tour Golf */116sram.on = 1;117sram.start = 0x200001;118sram.end = 0x203fff;119}120else if (strstr(rominfo.product,"ACLD007") != NULL)121{122/* Winter Challenge */123sram.on = 1;124sram.start = 0x200001;125sram.end = 0x200fff;126}127else if (strstr(rominfo.product,"T-50286") != NULL)128{129/* Buck Rogers - Countdown to Doomsday */130sram.on = 1;131sram.start = 0x200001;132sram.end = 0x203fff;133}134else if (((rominfo.realchecksum == 0xaeaa) || (rominfo.realchecksum == 0x8dba)) &&135(rominfo.checksum == 0x8104))136{137/* Xin Qigai Wangzi (use uncommon area) */138sram.on = 1;139sram.start = 0x400001;140sram.end = 0x40ffff;141}142else if ((strstr(rominfo.ROMType,"SF") != NULL) && (strstr(rominfo.product,"001") != NULL))143{144/* SF-001 */145sram.on = 1;146if (rominfo.checksum == 0x3e08)147{148/* last revision (use bankswitching) */149sram.start = 0x3c0001;150sram.end = 0x3cffff;151}152else153{154/* older revisions (use uncommon area) */155sram.start = 0x400001;156sram.end = 0x40ffff;157}158}159else if ((strstr(rominfo.ROMType,"SF") != NULL) && (strstr(rominfo.product,"004") != NULL))160{161/* SF-004 (use bankswitching) */162sram.on = 1;163sram.start = 0x200001;164sram.end = 0x203fff;165}166else if (strstr(rominfo.international,"SONIC & KNUCKLES") != NULL)167{168/* Sonic 3 & Knuckles combined ROM */169if (cart.romsize == 0x400000)170{171/* Sonic & Knuckle does not have backup RAM but can access FRAM from Sonic 3 cartridge */172sram.on = 1;173sram.start = 0x200001;174sram.end = 0x203fff;175}176}177178/* auto-detect games which need disabled backup RAM */179else if (strstr(rominfo.product,"T-113016") != NULL)180{181/* Pugsy (does not have backup RAM but tries writing outside ROM area as copy protection) */182sram.on = 0;183}184else if (strstr(rominfo.international,"SONIC THE HEDGEHOG 2") != NULL)185{186/* Sonic the Hedgehog 2 (does not have backup RAM) */187/* this prevents backup RAM from being mapped in place of mirrored ROM when using S&K LOCK-ON feature */188sram.on = 0;189}190191// by default, enable backup RAM for ROM smaller than 2MB192/*193else if (cart.romsize <= 0x200000)194{195// 64KB static RAM mapped to $200000-$20ffff196sram.start = 0x200000;197sram.end = 0x20ffff;198sram.on = 1;199}200*/201}202}203204unsigned int sram_read_byte(unsigned int address)205{206return sram.sram[address & 0xffff];207}208209unsigned int sram_read_word(unsigned int address)210{211address &= 0xfffe;212return (sram.sram[address + 1] | (sram.sram[address] << 8));213}214215void sram_write_byte(unsigned int address, unsigned int data)216{217sram.sram[address & 0xffff] = data;218}219220void sram_write_word(unsigned int address, unsigned int data)221{222address &= 0xfffe;223sram.sram[address] = data >> 8;224sram.sram[address + 1] = data & 0xff;225}226227// the variables in SRAM_T are all part of "configuration", so we don't have to save those.228// the only thing that needs to be saved is the SRAM itself and the SEEPROM struct (if applicable)229230int sram_get_actual_size()231{232if (!sram.on)233return 0;234switch (sram.custom)235{236case 0: // plain bus access saveram237break;238case 1: // i2c239return eeprom_i2c.config.size_mask + 1;240case 2: // spi241return 0x10000; // it doesn't appear to mask anything internally242case 3: // 93c243return 0x10000; // SMS only and i don't have time to look into it244default:245return 0x10000; // who knows246}247// figure size for plain bus access saverams248{249int startaddr = sram.start / 8192;250int endaddr = sram.end / 8192 + 1;251int size = (endaddr - startaddr) * 8192;252return size;253}254}255256257