File: PcsxrMemCardArray.m

package info (click to toggle)
pcsxr 1.9.94-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,500 kB
  • ctags: 16,110
  • sloc: ansic: 117,276; objc: 8,159; cpp: 1,140; makefile: 316; asm: 145; pascal: 30; sh: 20
file content (346 lines) | stat: -rw-r--r-- 8,739 bytes parent folder | download | duplicates (4)
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
//
//  PcsxrMemCardArray.m
//  Pcsxr
//
//  Created by C.W. Betts on 7/6/13.
//
//

#import "PcsxrMemCardArray.h"
#import "ConfigurationController.h"
#include "sio.h"

#define MAX_MEMCARD_BLOCKS 15

static inline void CopyMemcardData(char *from, char *to, int srci, int dsti, char *str)
{
	// header
	memmove(to + (dsti + 1) * 128, from + (srci + 1) * 128, 128);
	SaveMcd(str, to, (dsti + 1) * 128, 128);
	
	// data
	memmove(to + (dsti + 1) * 1024 * 8, from + (srci+1) * 1024 * 8, 1024 * 8);
	SaveMcd(str, to, (dsti + 1) * 1024 * 8, 1024 * 8);
}

static inline char* BlankHeader()
{
	struct PSXMemHeader {
		unsigned int allocState;
		unsigned int fileSize;
		unsigned short nextBlock;
		char fileName[21];
		unsigned char garbage[96];
		unsigned char checksum;
	};
	
	static struct PSXMemHeader *toReturn = NULL;
	if (!toReturn) {
		toReturn = calloc(sizeof(struct PSXMemHeader), 1);
		
		//FIXME: Which value is right?
		toReturn->allocState = 0x000000a0;
		//toReturn->allocState = 0xa0000000;
		toReturn->nextBlock = 0xFFFF;
		unsigned char *bytePtr = (unsigned char*)toReturn;
		for (int i = 0; i < sizeof(struct PSXMemHeader) - sizeof(unsigned char); i++) {
			toReturn->checksum = toReturn->checksum ^ bytePtr[i];
		}
	}
	
	return (char*)toReturn;
}

static inline void ClearMemcardData(char *to, int dsti, char *str)
{
	// header
	char *header = BlankHeader();
	memcpy(to + (dsti + 1) * 128, header, 128);
	SaveMcd(str, to, (dsti + 1) * 128, 128);
	
	// data
	memset(to + (dsti + 1) * 1024 * 8, 0, 1024 * 8);
	SaveMcd(str, to, (dsti + 1) * 1024 * 8, 1024 * 8);
}

@interface PcsxrMemCardArray ()
@property (strong) NSArray *rawArray;
@property (readonly) char* memDataPtr;
@property int cardNumber;
@end

@implementation PcsxrMemCardArray
@synthesize rawArray;
@synthesize cardNumber;

- (char*)memDataPtr
{
	if (cardNumber == 1) {
		return Mcd1Data;
	} else {
		return Mcd2Data;
	}
}

- (const char *)memCardCPath
{
	if (cardNumber == 1) {
		return Config.Mcd1;
	} else {
		return Config.Mcd2;
	}
}

- (id)initWithMemoryCardNumber:(int)carNum
{
	NSParameterAssert(carNum == 1 || carNum == 2);
	if (self = [super init]) {
		NSMutableArray *tmpMemArray = [[NSMutableArray alloc] initWithCapacity:MAX_MEMCARD_BLOCKS];
		cardNumber = carNum;
		int i, x;
		i = 0;
		while (i < MAX_MEMCARD_BLOCKS) {
			x = 1;
			McdBlock memBlock;
			GetMcdBlockInfo(carNum, i + 1, &memBlock);
			
			if ([PcsxrMemoryObject memFlagsFromBlockFlags:memBlock.Flags] == memFlagFree) {
				//Free space: ignore
				i++;
				continue;
			}
			do {
				McdBlock tmpBlock;
				GetMcdBlockInfo(carNum, i + x + 1, &tmpBlock);
				if ((tmpBlock.Flags & 0x3) == 0x3) {
					x++;
					break;
				} else if ((tmpBlock.Flags & 0x2) == 0x2) {
					x++;
				} else {
					break;
				}
			} while (i + x - 1 < MAX_MEMCARD_BLOCKS);
			@autoreleasepool {
				PcsxrMemoryObject *obj = [[PcsxrMemoryObject alloc] initWithMcdBlock:&memBlock startingIndex:i size:x];
				[tmpMemArray addObject:obj];
			}
			i += x;
		}
		self.rawArray = [NSArray arrayWithArray:tmpMemArray];
	}
	return self;
}

- (int)indexOfFreeBlocksWithSize:(int)asize
{
	int foundcount = 0, i = 0;
	
	McdBlock obj;
	// search for empty (formatted) blocks first
	while (i < MAX_MEMCARD_BLOCKS && foundcount < asize) {
		GetMcdBlockInfo(cardNumber, 1 + i++, &obj);
		//&Blocks[target_card][++i];
		if ((obj.Flags & 0xFF) == 0xA0) { // if A0 but not A1
			foundcount++;
		} else if (foundcount >= 1) { // need to find n count consecutive blocks
			foundcount = 0;
		} else {
			//i++;
		}
		//printf("formatstatus=%x\n", Info->Flags);
 	}
	
	if (foundcount == asize)
		return (i-foundcount);
	
	// no free formatted slots, try to find a deleted one
	foundcount = i = 0;
	while (i < MAX_MEMCARD_BLOCKS && foundcount < asize) {
		GetMcdBlockInfo(cardNumber, 1 + i++, &obj);
		if ((obj.Flags & 0xF0) == 0xA0) { // A2 or A6 f.e.
			foundcount++;
		} else if (foundcount >= 1) { // need to find n count consecutive blocks
			foundcount = 0;
		} else {
			//i++;
		}
		//printf("delstatus=%x\n", Info->Flags);
 	}
	
	if (foundcount == asize)
		return (i-foundcount);
	
 	return -1;
}

- (BOOL)moveBlockAtIndex:(int)idx toMemoryCard:(PcsxrMemCardArray*)otherCard
{
	if (idx == [rawArray count]) {
#ifdef DEBUG
		NSLog(@"Trying to get an object one more than the length of the raw array. Perhaps you were trying to \"move\" the free blocks. We don't want to do this.");
#endif
		return NO;
	}
	PcsxrMemoryObject *tmpObj = rawArray[idx];

	int memSize = tmpObj.blockSize;
	
	if ([otherCard availableBlocks] < memSize) {
		NSLog(@"Failing because the other card does not have enough space!");
		return NO;
	}
	
	int toCopy = [otherCard indexOfFreeBlocksWithSize:memSize];
	if (toCopy == -1) {
		NSLog(@"Not enough consecutive blocks. Compacting the other card.");
		[otherCard compactMemory];
		//Since we're accessing the mem card data directly (instead of via PcsxrMemoryObject objects) using the following calls, we don't need to reload the data.
		toCopy = [otherCard indexOfFreeBlocksWithSize:memSize];
		NSAssert(toCopy != -1, @"Compacting the card should have made space!");
	}
	
	int memIdx = tmpObj.startingIndex;
	for (int i = 0; i < memSize; i++) {
		CopyMemcardData([self memDataPtr], [otherCard memDataPtr], (memIdx+i), (toCopy+i), (char*)otherCard.memCardCPath);
	}
	
	return YES;
}

- (int)freeBlocks
{
	int memSize = 15;
	for (PcsxrMemoryObject *memObj in rawArray) {
		memSize -= memObj.blockSize;
	}
	return memSize;
}

- (int)availableBlocks
{
	int memSize = MAX_MEMCARD_BLOCKS;
	for (PcsxrMemoryObject *memObj in rawArray) {
		if (memObj.flagNameIndex != memFlagDeleted) {
			memSize -= memObj.blockSize;
		}
	}
	return memSize;
}

- (NSArray*)memoryArray
{
	int freeSize = [self freeBlocks];
	
	if (freeSize) {
		McdBlock theBlock;
		//Create a blank "block" that will be used for
		theBlock.Flags = 0xA0;
		theBlock.IconCount = 0;
		PcsxrMemoryObject *freeObj = [[PcsxrMemoryObject alloc] initWithMcdBlock:&theBlock startingIndex:MAX_MEMCARD_BLOCKS - 1 - freeSize size:freeSize];
		return [rawArray arrayByAddingObject:freeObj];
	} else
		return rawArray;
}

- (NSURL*)memCardURL
{
	if (cardNumber == 1) {
		return [[NSUserDefaults standardUserDefaults] URLForKey:@"Mcd1"];
	} else {
		return [[NSUserDefaults standardUserDefaults] URLForKey:@"Mcd2"];
	}
}

- (int)memorySizeAtIndex:(int)idx
{
	if (idx == [rawArray count]) {
#ifdef DEBUG
		NSLog(@"Trying to get an object one more than the length of the raw array. Perhaps you were trying to \"count\" the free blocks.");
#endif
		return [self freeBlocks];
	}

	return [rawArray[idx] blockSize];
}

- (void)compactMemory
{
	int i = 0, x = 1;
	while (i < MAX_MEMCARD_BLOCKS && x < MAX_MEMCARD_BLOCKS) {
		x = i;
		McdBlock baseBlock;
		GetMcdBlockInfo(cardNumber, i+1, &baseBlock);
		PCSXRMemFlags theFlags = [PcsxrMemoryObject memFlagsFromBlockFlags:baseBlock.Flags];
		
		if (theFlags == memFlagDeleted || theFlags == memFlagFree) {
			PCSXRMemFlags up1Flags = theFlags;
			while ((up1Flags == memFlagDeleted || up1Flags == memFlagFree) && x < MAX_MEMCARD_BLOCKS){
				x++;
				McdBlock up1Block;
				GetMcdBlockInfo(cardNumber, x+1, &up1Block);
				up1Flags = [PcsxrMemoryObject memFlagsFromBlockFlags:up1Block.Flags];
			}
			if (x >= MAX_MEMCARD_BLOCKS) {
				
				break;
			}
			CopyMemcardData(self.memDataPtr, self.memDataPtr, x, i, (char*)[[self.memCardURL path] fileSystemRepresentation]);
			ClearMemcardData(self.memDataPtr, x, (char*)self.memCardCPath );
		}
		i++;
	}
	
	while (i < MAX_MEMCARD_BLOCKS) {
		ClearMemcardData(self.memDataPtr, i, (char*)self.memCardCPath);
		i++;
	}
	
	LoadMcd(cardNumber, (char*)self.memCardCPath);
}

- (void)deleteMemoryBlocksAtIndex:(int)slotnum
{
	int xor = 0, i, j;
	char *data, *ptr, *filename;
	if (cardNumber == 1) {
		filename = Config.Mcd1;
		data = Mcd1Data;
	} else {
		filename = Config.Mcd2;
		data = Mcd2Data;
	}
	
	if (slotnum == [rawArray count]) {
#ifdef DEBUG
		NSLog(@"Trying to get an object one more than the length of the raw array. Perhaps you were trying to \"delete\" the free blocks.");
#endif
		return;
	}
	
	PcsxrMemoryObject *theObj = rawArray[slotnum];
	
	McdBlock flagBlock;
	
	for(i = theObj.startingIndex +1; i < (theObj.startingIndex + theObj.blockSize +1); i++)
	{
		GetMcdBlockInfo(cardNumber, i, &flagBlock);
		ptr = data + i * 128;
		
		if ((flagBlock.Flags & 0xF0) == 0xA0) {
			if ((flagBlock.Flags & 0xF) >= 1 &&
				(flagBlock.Flags & 0xF) <= 3) { // deleted
				*ptr = 0x50 | (flagBlock.Flags & 0xF);
			} else return;
		} else if ((flagBlock.Flags & 0xF0) == 0x50) { // used
			*ptr = 0xA0 | (flagBlock.Flags & 0xF);
		} else { continue; }
		
		for (j = 0; j < 127; j++) xor ^= *ptr++;
		*ptr = xor;
		
		SaveMcd(filename, data, i * 128, 128);
	}
}

@end