-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsavermanage.cpp
393 lines (366 loc) · 8.88 KB
/
savermanage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "stdio.h"
#include "string.h"
#include "savermanage.h"
#include "md5.h"
#include "inram.h"
#include "hard.h"
#include "fat16.h"
#include "bgfunc.h"
#include "keypad.h"
#include "shell.h"
#include "global.h"
#include "lang.h"
//
//******************************************************************************
//**** EZ3内存读写
void ReadSram(u32 address, u8* data , u32 size )
{
register int i ;
u16* pData = (u16*)data;
for(i=0;i<size;i+=2)
{
pData[i>>1]=*(u8*)(address+i)+(*(u8*)(address+i+1)*0x100);
}
}
void WriteSram(u32 address, u8* data , u32 size )
{
register int i ;
for(i=0;i<size;i++)
*(u8*)(address+i)=data[i];
}
void CopybackSaver(u8* pCpbuf,u32 size,u32 startpage)
{
u32 left =0 , page = 0 ;
//---做循环,读取size大小
do
{
SetRampage(startpage+page);
ReadSram(0xE000000,pCpbuf,64*1024);
left += 64*1024 ;
page += 0x10 ;
pCpbuf += 64*1024 ;
}
while(left < size) ;
//-结束
/*
if(size>64*1024)
{
ReadSram(0xE000000,pCpbuf,64*1024);
SetRampage(startpage+16);
left = size - 64*1024 ;
ReadSram(0xE000000,pCpbuf+64*1024,left);
}
else
ReadSram(0xE000000,pCpbuf,size);
*/
SetRampage(0);
}
u8 Checkaladdin()
{
SAVEFILEINFO saverinfo ;
char EZSAVER[]="EZFlash Saver FileSystem-aladdin =!@#$%^&*(){}[]:-<>,.?=OVER+END" ;
SetRampage(0);
//const char EZSAVER[]="EZFlash Saver FileSystem-aladdin" ;
ReadSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
if(memcmp(saverinfo.flag,EZSAVER,64))
{//不同,掉电或者刚刚进入。
return 1 ;
}
else
return 0 ;
}
u16 CheckLanguage()
{
u16 lan ;
ReadSram(0x0E0081F0,(u8*)&lan,2) ;
return lan ;
}
u8 CheckSram()
{//返回数据需要备份RAM到NAND区,数值为第几个游戏。
//返回0,表示什么都没有改变,直接退出。
MD5_CTX context;
SAVEFILEINFO saverinfo ;
char EZSAVER[]="EZFlash Saver FileSystem-aladdin =!@#$%^&*(){}[]:-<>,.?=OVER+END" ;
char buf[36];
int size = 0 , i =0;
keypad keys ;
SetRampage(0);
//const char EZSAVER[]="EZFlash Saver FileSystem-aladdin" ;
ReadSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
if(memcmp(saverinfo.flag,EZSAVER,64))
{//不同,掉电或者刚刚进入。
//写入数据,开始
CreateWindow(gl_warning,gl_firstin,60,40,1);
while(1){
sleep(15); keys.update();
if(keys.release(KEY_A)||keys.release(KEY_B)) break;
}
memset(&saverinfo,0,sizeof(SAVEFILEINFO));
memcpy(saverinfo.flag,EZSAVER,64);
saverinfo.saverforce = 1 ;
WriteSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
return 0;
}
else
{
size = saverinfo.saversize *4096 ;
CopybackSaver((u8*)_ramFatCache,size,16);
MD5Init(&context);
MD5Update(&context, (u8*)_ramFatCache, size);
MD5Final((u8*)buf, &context);
for(i=0;i<16;i++)
buf[16+i] = saverinfo.md5code[i];
if(memcmp(buf,buf+16,16))
{//不相等,需要备份存档数据
return 1 ;
}
else
{//相等,存档不需要备份
return 0 ;
}
}
}
int SaveCurrentSaverToFlash()
{
MD5_CTX context;
keypad keys ;
SAVEFILEINFO saverinfo ;
char buf[256] ;
char savername[512] ;
int get,handle;
SetRampage(0);
ReadSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
int size = saverinfo.saversize * 4096 ;
//读文件名
if(size ==0)
return -1 ;
FormatSaverName(saverinfo.gamename,savername,0);
//将数据备份,同时生成md5码
OpenWrite();
CopybackSaver((u8*)0x8800000,size,16);
CloseWrite();
MD5Init(&context);
MD5Update(&context, (u8*)0x8800000, size);
MD5Final((u8*)buf, &context);
memcpy(saverinfo.md5code,buf,16);
saverinfo.isSaved = 1 ;
//准备将数据写入sram区
get = fat_init(1);
if(get)
{
CreateWindow(gl_warning,gl_faterror,60,40,1);
while(1){
sleep(15); keys.update();
if(keys.press(KEY_A)||keys.release(KEY_B)) break;
}
return -3;
}
get = fat_getfirst("\\Saver",buf);
if(get)
{//创建一个dir
fat_mkdir("\\Saver");
}
memcpy(buf,savername,256);
sprintf(savername,"\\Saver\\%s",buf);
sprintf(buf,"%s%s[%dKB]",gl_writing,savername,size/1024);
CreateWindow(gl_waiting,buf,60,40,1);
handle = fat_creat(savername,ATTR_ARCHIVE);
if(handle<0)
handle =fat_open(savername);
fat_write(handle,(char*)0x8800000,size);
fat_close(handle);
fat_deinit();
//最后写入更改。
WriteSram(_FileInfoSaver,(u8*)&saverinfo ,sizeof(SAVEFILEINFO));
return 0 ;
}
void FormatSaverName(char* inname1 , char* outname,int numb)
{
char *pt ;
char pp[4] ;
strcpy(outname,inname1);
pt = strrchr(outname,'.');
if(numb>0)
{
sprintf(pp,"%02d",numb);
pt[1] = 's';
pt[2] = pp[0];
pt[3] = pp[1];
pt[4] = 0;
}
else
{
pt[1] = 's';
pt[2] = 'a';
pt[3] = 'v';
pt[4] = 0;
}
/*
char inname[54];
int i = 0 ;
memcpy(inname,inname1,54);
char *p = strrchr(inname,'.');
if(p)
p[0] = 0 ;
int len = strlen(inname);
for(i=0;i<len;i++)
{
if((inname[i] == ' ')||(inname[i] == '.')||(inname[i] == '+')||(inname[i] == '&'))
inname[i] = '_' ;
}
if(len>8)
inname[8]=0 ;
sprintf(outname,"%s.sav",inname);
p = strrchr(outname,'.');
if(p)
{
p++ ;
if(numb>0)
{
sprintf(inname,"%02d",numb);
p[1] = inname[0] ;
p[2] = inname[1] ;
}
}*/
}
void WriteSaverInfo(char* inname,int saversize,u8 where)
{
SAVEFILEINFO saverinfo ;
SetRampage(0);
ReadSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
sprintf(saverinfo.gamename,"%s",inname);
saverinfo.isSaved = 0 ;
saverinfo.where = where ;
saverinfo.saversize = saversize ;
WriteSram(_FileInfoSaver,(u8*)&saverinfo,sizeof(SAVEFILEINFO));
}
//读取nand Flash区的存档
int ReadFileToSram(char* name)
{
MD5_CTX context;
_stat filestat ;
char *pRam = (char*)_FileInfoSaver ;
u8 *pRead = (u8*)_ramFatBlockCache;
char buf[64];
char savername[64] ;
int get =0,handle=0,left=0 , vfsize = 0,rpage = 0 ;
keypad keys ;
//准备读出数据
get = fat_init(1);
if(get)
{
CreateWindow(gl_warning,gl_faterror,60,40,1);
while(1){
sleep(15); keys.update();
if(keys.press(KEY_A)||keys.release(KEY_B)) break;
}
return get ;
}
sprintf(savername,"\\Saver\\%s",name);
sprintf(buf,"%s%s",gl_saverfile,savername);
CreateWindow(gl_waiting,buf,60,40,1);
handle =fat_open(savername);
if(handle<0)
{
return handle ;
}
fat_get_stat(savername,&filestat);
MD5Init(&context);
left = 0 ;
rpage = 0 ;
do
{
if((filestat.FileSize - left)>63336)
vfsize = 65536 ;
else
vfsize = (filestat.FileSize - left) ;
fat_read(handle,pRead,vfsize);
MD5Update(&context,(u8*)pRead,vfsize);
SetRampage(16+rpage);
WriteSram(_SRAMSaver,pRead,vfsize);
left += 64*1024 ;
rpage += 0x10 ;
}
while(left< filestat.FileSize) ;
MD5Final((u8*)buf, &context);
fat_close_withouwrit(handle);
SetRampage(0);
//将其中的md5校验数据填入sram区
SAVEFILEINFO saverinfo ;
WriteSram(_FileInfoSaver+64,(u8*)buf,16);
return 0 ;
}
void ClearSaver(int size)
{
int left ;
u8 *pRead = (u8*)_ramFatBlockCache;
DmaClear(3,0,_ramFatBlockCache,0x20000,32);
SetRampage(16);
if(size>64*1024)
{
WriteSram(_SRAMSaver,pRead,64*1024);
SetRampage(32);
left = size - 64*1024 ;
WriteSram(_SRAMSaver,pRead+64*1024,left);
}
else
WriteSram(_SRAMSaver,pRead,size);
SetRampage(0);
}
// 进入存档管理
int EnterSaver(u8 mode,int sel)
{
//u8 =0 表示读取存档 =1 表示存储存档
//返回值为-1,表示取消,返回>=0表示所选择的存档索引
FILE_SAVE_INFO fsi;
char *saverspace[5] ;
int ct =0 ;
saverspace[0]=(char *)_UnusedEram;
saverspace[1]=(char *)_UnusedEram+64;
saverspace[2]=(char *)_UnusedEram+64*2;
saverspace[3]=(char *)_UnusedEram+64*3;
saverspace[4]=(char *)_UnusedEram+64*4;
int func_i ;
//搜索存档并设置名称
if(!mode)
{
load(SAVETXTPOS,(u8*)&fsi,sizeof(FILE_SAVE_INFO));
if(fsi.flg=='eztx')
sprintf(saverspace[ct++],"Auto - %s",fsi.fname);
else
sprintf(saverspace[ct++],"Auto - Unused");
}
//第二个存档
load(_txtSaver1,(u8*)&fsi,sizeof(FILE_SAVE_INFO));
if(fsi.flg=='eztx')
sprintf(saverspace[ct++],"saver1 - %s",fsi.fname);
else
sprintf(saverspace[ct++],"saver1 - Unused");
//第二个存档
load(_txtSaver2,(u8*)&fsi,sizeof(FILE_SAVE_INFO));
if(fsi.flg=='eztx')
sprintf(saverspace[ct++],"saver2 - %s",fsi.fname);
else
sprintf(saverspace[ct++],"saver2 - Unused");
//第二个存档
load(_txtSaver3,(u8*)&fsi,sizeof(FILE_SAVE_INFO));
if(fsi.flg=='eztx')
sprintf(saverspace[ct++],"saver3 - %s",fsi.fname);
else
sprintf(saverspace[ct++],"saver3 - Unused");
//第二个存档
load(_txtSaver4,(u8*)&fsi,sizeof(FILE_SAVE_INFO));
if(fsi.flg=='eztx')
sprintf(saverspace[ct++],"saver4 - %s",fsi.fname);
else
sprintf(saverspace[ct++],"saver4 - Unused");
if(mode)
{
func_i = ShowWin_Menu("ezoption.bmp","","txtSaveSaver.bmp",(char**)saverspace,ct,sel);
}
else
{
func_i = ShowWin_Menu("ezoption.bmp","","TXTLoadSaver.bmp",(char**)saverspace,ct,sel);
}
return func_i ;
}