aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/ramsave.cpp
blob: ea4bff9cc36d4bd415adfad14af66ca7cbc049dd (plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */
 // Save in order 1,2,3,4,larger 2,5
#include "stdafx.h"
#include "system.h"
#include "ramsave.h"
#include "nds.h"
#include "compressor/lz.h"

#define CART_RAM ((vu8 *) (0x0A000000))
#define SRAM_SAVE_MAX (65533)

DSSaveFile::DSSaveFile() {
	ptr = 0;
	saveCompressed = false;
	save.isValid = false;
	ownsData = false;
	isOpenFlag = true;
	isTempFile = false;
}

DSSaveFile::DSSaveFile(SCUMMSave* s, bool compressed, u8* data) {
	save = *s;
	saveData = data;
	ptr = 0;
	saveCompressed = compressed;
	isOpenFlag = true;

	if (saveCompressed) {
		u8* uncompressed = new unsigned char[save.size];
		if (!uncompressed) consolePrintf("Out of memory allocating %d!\n", save.size);
		LZ_Uncompress(saveData, uncompressed, save.compressedSize);
		saveData = uncompressed;
		ownsData = true;
		saveCompressed = false;
//		consolePrintf("Decompressed. name=%s size=%d (%d)", save.name, save.size, save.compressedSize);
		
	} else {
		ownsData = false;
		origHeader = s;
	}
	
	if (save.magic == (int) 0xBEEFCAFE) {
		save.isValid = true;
	} else {
		save.isValid = false;
	}
	
	isTempFile = false;
}

DSSaveFile::~DSSaveFile() {
	if (!ownsData) {
		*origHeader = save;
		DSSaveFileManager::instance()->flushToSaveRAM();
	}
	if (ownsData) {
		delete saveData;
	}
}

bool DSSaveFile::loadFromSaveRAM(vu8* address) {
	
	SCUMMSave newSave;

	for (int t = 0; t < (int) sizeof(newSave); t++) {
		((char *) (&newSave))[t] = *(address + t);
	}

	if (newSave.magic == 0xBEEFCAFE) {
		newSave.isValid = true;

		*((u16 *) (0x4000204)) |= 0x3;
		
		saveData = new unsigned char[newSave.compressedSize];
		
		for (int t = 0; t < (int) newSave.compressedSize; t++) {
			((char *) (saveData))[t] = *(address + t + sizeof(newSave));
		}
		
		if (ownsData) delete this->saveData;
		save = newSave;
		saveCompressed = true;
		this->saveData = saveData;
		ownsData = true;
		ptr = 0;

		return true;
	}
	
	return false;
}

void DSSaveFile::compress() {
	if (!saveCompressed) {
		unsigned char* compBuffer = new unsigned char[(save.size * 110) / 100];
		int compSize = LZ_Compress((u8 *) saveData, compBuffer, save.size);
		save.compressedSize = compSize;
		
		
		
		delete saveData;
		
		// Make the save smaller
		saveData = (u8 *) realloc(compBuffer, save.compressedSize);
		saveCompressed = true;
	}
}

int DSSaveFile::saveToSaveRAM(vu8* address) {

	unsigned char* compBuffer;
	bool failed;
	

	int compSize;
	
	compress();
	
	compSize = save.compressedSize;
	compBuffer = saveData;
	
	if (DSSaveFileManager::instance()->getBytesFree() >= getRamUsage()) {

		DSSaveFileManager::instance()->addBytesFree(-getRamUsage());
	
		// Write header
		for (int t = 0; t < sizeof(save); t++) {
			while (*(address + t) != ((char *) (&save))[t]) {
				*(address + t) = ((char *) (&save))[t];
			}
		}
	
		// Write compressed buffer
		for (int t = sizeof(save); t < (int) sizeof(save) + compSize; t++) {
			while (*(address + t) != compBuffer[t - sizeof(save)]) {
				*(address + t) = compBuffer[t - sizeof(save)];
			}
		}
		
		failed = false;
	} else {
		failed = true;
	}

		
	return failed? 0: compSize + sizeof(save);

}

void DSSaveFile::reset() {
	ptr = 0;
}

uint32 DSSaveFile::read(void *buf, uint32 size) {
	if (ptr + size > save.size) {
		size = save.size - ptr;
		if (size < 0) size = 0;
	}
	memcpy(buf, saveData + ptr, size);
//	consolePrintf("byte: %d ", ((u8 *) (buf))[0]);
	
	ptr += size;
	return size;
}

uint32 DSSaveFile::pos() const {
	return ptr;
}

uint32 DSSaveFile::size() const {
	return save.size;
}

void DSSaveFile::seek(int32 pos, int whence) {
	switch (whence) {
		case SEEK_SET: {
			ptr = pos;
			break;
		}
		case SEEK_CUR: {
			ptr += pos;
			break;
		}
		case SEEK_END: {
			ptr = save.size - pos;
			break;
		}
	}
}

bool DSSaveFile::eos() const {
	return ptr >= (int) save.size;
}

void DSSaveFile::skip(uint32 bytes) {
	ptr = ptr + bytes;
	if (ptr > (int) save.size) ptr = save.size;
}

uint32 DSSaveFile::write(const void *buf, uint32 size) {

	if (ptr + size > DS_MAX_SAVE_SIZE) {
		size = DS_MAX_SAVE_SIZE - ptr;
	}

	memcpy(saveData + ptr, buf, size);
	ptr += size;
	save.size += size;
	return size;
}

bool DSSaveFile::matches(char* prefix, int num) {
	char str[16];
	if (isValid()) {
		sprintf(str, "%s%02d", prefix, num);
		if (!strcmp(str, save.name)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

bool DSSaveFile::matches(char* filename) {
	if (isValid()) {
		return !strcmp(save.name, filename);
	} else {
		return false;
	}
}
	
void DSSaveFile::setName(char *name) {
	save.isValid = true;
	save.magic = 0xBEEFCAFE;
	ownsData = true;
	save.size = 0;
	save.compressedSize = 0;
	saveData = new unsigned char[DS_MAX_SAVE_SIZE];
	strcpy(save.name, name);
	
	if ((strstr(name, ".s99")) || (strstr(name, ".c"))) {
		isTempFile = true;
	} else {
		isTempFile = false;
	}
}

void DSSaveFile::clearData() {
	save.size = 0;

	if (saveCompressed) {
		if (ownsData) {
			delete saveData;
			DSSaveFileManager::instance()->addBytesFree(getRamUsage());
		}
		saveData = new unsigned char[DS_MAX_SAVE_SIZE];
		saveCompressed = false;
		ownsData = true;
	}

}

void DSSaveFile::deleteFile() {
	if (isValid()) {
		if (ownsData) {
			DSSaveFileManager::instance()->addBytesFree(getRamUsage());
			delete saveData;
			saveData = NULL;
		}
		ptr = 0;
		saveCompressed = false;
		save.isValid = false;
		ownsData = false;
		isOpenFlag = true;
	}
}

DSSaveFileManager::DSSaveFileManager() {
	instancePtr = this;
	
	*((u16 *) (0x4000204)) |= 0x3;
	swiWaitForVBlank();
	
	loadAllFromSRAM();
}

DSSaveFileManager::~DSSaveFileManager() {
	instancePtr = NULL;
}

void DSSaveFileManager::loadAllFromSRAM() {
	int addr = 1;
	
	for (int r = 0; r < 8; r++) {
		gbaSave[r].deleteFile();
	}

	sramBytesFree = SRAM_SAVE_MAX;

	// Try to find saves in save RAM
	for (int r = 0; r < 8; r++) {
		if (gbaSave[r].loadFromSaveRAM(CART_RAM + addr)) {
			addr += gbaSave[r].getRamUsage();
			sramBytesFree -= gbaSave[r].getRamUsage();
		}
	}

}

void DSSaveFileManager::formatSram() {
	for (int r = 0; r < SRAM_SAVE_MAX; r++) {
		*(CART_RAM + r) = 0;
	}
	
	loadAllFromSRAM();
}

void DSSaveFileManager::listFiles() {
	for (int r = 0; r < 8; r++) {
		if (gbaSave[r].isValid()) {
			consolePrintf("'%s': %d bytes\n", gbaSave[r].getName(), gbaSave[r].getRamUsage());
		}
	}
	consolePrintf("SRAM free: %d bytes\n", getBytesFree());
}

DSSaveFileManager* DSSaveFileManager::instancePtr = NULL;

DSSaveFile *DSSaveFileManager::openSavefile(const char* filename, bool saveOrLoad) {
	for (int r = 0; r < 8; r++) {
		if (gbaSave[r].isValid() && (gbaSave[r].matches((char *) filename))) {
//			consolePrintf("Matched save %d (%d)\n", r, gbaSave[r].getSize());
			gbaSave[r].reset();
			//consolePrintf("reset ");
			if (saveOrLoad) gbaSave[r].clearData();
//			consolePrintf("cleared ");
			return gbaSave[r].clone();
		}
	}
	
	if (saveOrLoad) {
		return makeSaveFile(filename, saveOrLoad);
	} else {
		return NULL;
	}
}



DSSaveFile* DSSaveFile::clone() {
//	consolePrintf("Clone %s %d\n", save.name, save.size);
	return new DSSaveFile(&save, saveCompressed, saveData);
}

void DSSaveFileManager::deleteFile(char* name) {
//	consolePrintf("Deleting %s", name);
	for (int r = 0; r < 8; r++) {
		if (gbaSave[r].isValid() && (gbaSave[r].matches((char *) name))) {
			gbaSave[r].deleteFile();
		}
	}
	flushToSaveRAM();
}

void DSSaveFileManager::listSavefiles(const char *prefix, bool *marks, int num) {
	memset(marks, true, num * sizeof(bool));
	return;

	memset(marks, false, num*sizeof(bool));

	for (int saveNum = 0; saveNum < num; saveNum++) {
		for (int r = 0; r < 8; r++) {
			if (gbaSave[r].isValid() && (gbaSave[r].matches((char *) prefix, saveNum))) {
				marks[saveNum] = true;
			}
		}
	}
	
}

DSSaveFile *DSSaveFileManager::makeSaveFile(const char *filename, bool saveOrLoad) {
	
	// Find a free save slot
	int r = 0;
	
	while ((r < 8) && (gbaSave[r].isValid())) {
		r++;
	}
	
	if ((r == 8) && (gbaSave[r].isValid())) {
		// No more saves
		return NULL;
	} else {
		// Allocate this save
//		consolePrintf("Allocated save %d\n", r);
		gbaSave[r].setName((char *) filename);
		gbaSave[r].reset();
		return gbaSave[r].clone();
	}
}

void DSSaveFileManager::flushToSaveRAM() {
	int cartAddr = 1;
	int s;
	int extraData = DSSaveFileManager::getExtraData();
	
	*((u16 *) (0x4000204)) |= 0x3;
	
	swiWaitForVBlank();

	int size = 0;
	for (int r = 0; (r < 8); r++) {
		if (gbaSave[r].isValid()) {
			gbaSave[r].compress();
			if (!gbaSave[r].isTemp()) size += gbaSave[r].getRamUsage();
		}
	}
	
	if (size <= SRAM_SAVE_MAX) {

		for (int r = 0; r < SRAM_SAVE_MAX; r++) {
			*(CART_RAM + r) = 0;
		}
		
		sramBytesFree = SRAM_SAVE_MAX;
		
		for (int r = 0; (r < 8); r++) {
			if (gbaSave[r].isValid() && (!gbaSave[r].isTemp())) {
				
				cartAddr += s = gbaSave[r].saveToSaveRAM(CART_RAM + cartAddr);
				
	/*			if (s == 0) {
					consolePrintf("WARNING: Save didn't fit in cart RAM and has been lost!!  Delete files and save again.", gbaSave[r].getName());
					failed = true;
				}*/
			}
		}
	} else {

		consolePrintf("WARNING: Save didn't fit in cart RAM and has been lost!!  Delete files and save again.");
		loadAllFromSRAM();
		
	}

	DSSaveFileManager::setExtraData(extraData);
//	consolePrintf("SRAM free: %d bytes\n", getBytesFree());
}

void DSSaveFileManager::setExtraData(int data) {
	// Offset of extra data is 31.  This overlaps the padding and reserved bytes of the first save entry.
	// which have not been used up until now.  So it should be safe.

	vu8* sram = CART_RAM + 31;

	*(sram + 0) = 0xF0;		// This is an identifier to check
	*(sram + 1) = 0x0D;		// that extra data is present.

	*(sram + 2) = (data & 0xFF000000) >> 24;		// Now write the actual data
	*(sram + 3) = (data & 0x00FF0000) >> 16;		// taking care to use single
	*(sram + 4) = (data & 0x0000FF00) >> 8;			// byte writes (it's an 8-bit bus)
	*(sram + 5) = (data & 0x000000FF);
}

bool DSSaveFileManager::isExtraDataPresent() {
	vu8* sram = CART_RAM + 31;

	// Check for the identifier
	return ((*(sram + 0) == 0xF0) && (*(sram + 1) == 0x0D));
}

int DSSaveFileManager::getExtraData() {
	vu8* sram = CART_RAM + 31;

	if (isExtraDataPresent()) {
		int value = (*(sram + 2) << 24) | (*(sram + 3) << 16) | (*(sram + 4) << 8) | (*(sram + 5));
		return value;
	} else {
		return 0;
	}
}