1 /******************************************************************************/
2 /* Mednafen Sega Saturn Emulation Module                                      */
3 /******************************************************************************/
4 /* backup.cpp - Backup memory(512KiB) cart emulation
5 **  Copyright (C) 2016-2017 Mednafen Team
6 **
7 ** This program is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU General Public License
9 ** as published by the Free Software Foundation; either version 2
10 ** of the License, or (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software Foundation, Inc.,
19 ** 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21 
22 #include "common.h"
23 #include "backup.h"
24 
25 namespace MDFN_IEN_SS
26 {
27 
28 static uint8 ExtBackupRAM[0x80000];
29 static bool ExtBackupRAM_Dirty;
30 
31 // TODO: Check mirroring.
32 template<typename T, bool IsWrite>
ExtBackupRAM_RW_DB(uint32 A,uint16 * DB)33 static MDFN_HOT void ExtBackupRAM_RW_DB(uint32 A, uint16* DB)
34 {
35  uint8* const ptr = ExtBackupRAM + ((A >> 1) & 0x7FFFF);
36 
37  if(IsWrite)
38  {
39   if(A & 1)
40   {
41    ExtBackupRAM_Dirty = true;
42    *ptr = *DB;
43   }
44  }
45  else
46  {
47   *DB = (*ptr << 0) | 0xFF00;
48 
49   if((A & ~1) == 0x04FFFFFE)
50    *DB = 0x21;
51  }
52 }
53 
GetClearNVDirty(void)54 static MDFN_COLD bool GetClearNVDirty(void)
55 {
56  bool ret = ExtBackupRAM_Dirty;
57 
58  ExtBackupRAM_Dirty = false;
59 
60  return ret;
61 }
62 
GetNVInfo(const char ** ext,void ** nv_ptr,bool * nv16,uint64 * nv_size)63 static MDFN_COLD void GetNVInfo(const char** ext, void** nv_ptr, bool* nv16, uint64* nv_size)
64 {
65  *ext = "bcr";
66  *nv_ptr = ExtBackupRAM;
67  *nv16 = false;
68  *nv_size = sizeof(ExtBackupRAM);
69 }
70 
StateAction(StateMem * sm,const unsigned load,const bool data_only)71 static MDFN_COLD void StateAction(StateMem* sm, const unsigned load, const bool data_only)
72 {
73  SFORMAT StateRegs[] =
74  {
75   SFPTR8N(ExtBackupRAM, 0x80000, "ExtBackupRAM"),
76   SFEND
77  };
78 
79  MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_BACKUP");
80 
81  if(load)
82  {
83   ExtBackupRAM_Dirty = true;
84  }
85 }
86 
CART_Backup_Init(CartInfo * c)87 void CART_Backup_Init(CartInfo* c)
88 {
89  static const uint8 init[0x10] = { 0x42, 0x61, 0x63, 0x6B, 0x55, 0x70, 0x52, 0x61, 0x6D, 0x20, 0x46, 0x6F, 0x72, 0x6D, 0x61, 0x74 };
90 
91  memset(ExtBackupRAM, 0x00, sizeof(ExtBackupRAM));
92  for(unsigned i = 0; i < 0x200; i += 0x10)
93   memcpy(ExtBackupRAM + i, init, 0x10);
94 
95  ExtBackupRAM_Dirty = false;
96 
97  c->CS01_SetRW8W16(0x04000000, 0x04FFFFFF,
98 	ExtBackupRAM_RW_DB<uint16, false>,
99 	ExtBackupRAM_RW_DB<uint8, true>,
100 	ExtBackupRAM_RW_DB<uint16, true>);
101 
102  c->GetClearNVDirty = GetClearNVDirty;
103  c->GetNVInfo = GetNVInfo;
104  c->StateAction = StateAction;
105 }
106 
107 }
108