1 /*****************************************************************************
2 ** $Source: /cygdrive/d/Private/_SVNROOT/bluemsx/blueMSX/Src/Memory/sramLoader.c,v $
3 **
4 ** $Revision: 1.7 $
5 **
6 ** $Date: 2008-03-30 18:38:44 $
7 **
8 ** More info: http://www.bluemsx.com
9 **
10 ** Copyright (C) 2003-2006 Daniel Vik
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** This program is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ** GNU General Public License for more details.
21 **
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 **
26 ******************************************************************************
27 */
28 #include "sramLoader.h"
29 #include "Board.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 
34 
35 
sramCreateFilenameWithSuffix(const char * romFilename,char * suffix,char * ext)36 const char* sramCreateFilenameWithSuffix(const char* romFilename, char* suffix, char* ext)
37 {
38     static char SRAMfileName[512];
39     char fileName[512];
40     char* dst = fileName + 512;
41     const char* src;
42 
43     *--dst = '\0';
44     if (ext == NULL) {
45         *--dst = 'm';
46         *--dst = 'a';
47         *--dst = 'r';
48         *--dst = 's';
49         *--dst = '.';
50     }
51     else {
52         char* p = ext + strlen(ext);
53         do {
54             *--dst = *--p;
55         } while (p != ext);
56     }
57 
58     dst -= strlen(suffix);
59     memcpy(dst, suffix, strlen(suffix));
60 
61     src = romFilename + strlen(romFilename);
62 
63     while (*src != '.' && src > romFilename) {
64         src--;
65     }
66     src--;
67 
68     while (*src != '/' && *src != '\\' && src >= romFilename) {
69         *--dst = *src--;
70     }
71 
72     sprintf(SRAMfileName, "%s" DIR_SEPARATOR "%s", boardGetBaseDirectory(), dst);
73 
74     return SRAMfileName;
75 }
76 
77 
sramCreateFilename(const char * romFilename)78 const char* sramCreateFilename(const char* romFilename) {
79     return sramCreateFilenameWithSuffix(romFilename, "", NULL);
80 }
sramLoad(const char * filename,UInt8 * sram,int length,void * header,int headerLength)81 void sramLoad(const char* filename, UInt8* sram, int length, void* header, int headerLength) {
82     FILE* file;
83 
84     file = fopen(filename, "rb");
85     if (file != NULL) {
86         if (headerLength > 0) {
87             char* readHeader[256];
88 
89             fread(readHeader, 1, headerLength, file);
90             if (memcmp(readHeader, header, headerLength)) {
91                 fclose(file);
92                 return;
93             }
94         }
95         fread(sram, 1, length, file);
96         fclose(file);
97     }
98 }
99 
sramSave(const char * filename,UInt8 * sram,int length,void * header,int headerLength)100 void sramSave(const char* filename, UInt8* sram, int length, void* header, int headerLength) {
101     FILE* file;
102 
103     file = fopen(filename, "wb");
104     if (file != NULL) {
105         if (headerLength > 0) {
106             fwrite(header, 1, headerLength, file);
107         }
108         fwrite(sram, 1, length, file);
109         fclose(file);
110     }
111 }
112 
113