summaryrefslogtreecommitdiff
path: root/macosx/Pcsxr-QL/PSXMemEnumerator.m
blob: 62ab396f197b033e67c5d0b9de7d74f4585c44d8 (plain) (blame)
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
//
//  PSXMemEnumerator.c
//  Pcsxr
//
//  Created by C.W. Betts on 7/20/14.
//
//

#include <stdio.h>
#import "PSXMemEnumerator.h"

#define MAX_MEMCARD_BLOCKS 15

static void GetSoloBlockInfo(unsigned char *data, int block, McdBlock *Info)
{
	unsigned char *ptr = data + block * 8192 + 2;
	unsigned char *str = Info->Title;
	unsigned short clut[16];
	unsigned char *	sstr = Info->sTitle;
	unsigned short c;
	int i, x = 0;
	
	memset(Info, 0, sizeof(McdBlock));
	Info->IconCount = *ptr & 0x3;
	ptr += 2;
	
	for (i = 0; i < 48; i++) {
		c = *(ptr) << 8;
		c |= *(ptr + 1);
		if (!c)
			break;
		
		// Convert ASCII characters to half-width
		if (c >= 0x8281 && c <= 0x829A) {
			c = (c - 0x8281) + 'a';
		} else if (c >= 0x824F && c <= 0x827A) {
			c = (c - 0x824F) + '0';
		} else if (c == 0x8140) {
			c = ' ';
		} else if (c == 0x8143) {
			c = ',';
		} else if (c == 0x8144) {
			c = '.';
		} else if (c == 0x8146) {
			c = ':';
		} else if (c == 0x8147) {
			c = ';';
		} else if (c == 0x8148) {
			c = '?';
		} else if (c == 0x8149) {
			c = '!';
		} else if (c == 0x815E) {
			c = '/';
		} else if (c == 0x8168) {
			c = '"';
		} else if (c == 0x8169) {
			c = '(';
		} else if (c == 0x816A) {
			c = ')';
		} else if (c == 0x816D) {
			c = '[';
		} else if (c == 0x816E) {
			c = ']';
		} else if (c == 0x817C) {
			c = '-';
		} else {
			str[i] = ' ';
			sstr[x++] = *ptr++;
			sstr[x++] = *ptr++;
			continue;
		}
		
		str[i] = sstr[x++] = c;
		ptr += 2;
	}
	
	ptr = data + block * 8192 + 0x60; // icon palette data
	
	for (i = 0; i < 16; i++) {
		clut[i] = *((unsigned short *)ptr);
		ptr += 2;
	}
	
	for (i = 0; i < Info->IconCount; i++) {
		short *icon = &Info->Icon[i * 16 * 16];
		
		ptr = data + block * 8192 + 128 + 128 * i; // icon data
		
		for (x = 0; x < 16 * 16; x++) {
			icon[x++] = clut[*ptr & 0xf];
			icon[x] = clut[*ptr >> 4];
			ptr++;
		}
	}

	ptr = data + block * 128;
	
	Info->Flags = *ptr;
	
	ptr += 0xa;
	strlcpy(Info->ID, ptr, 13);
	ptr += 12;
	strlcpy(Info->Name, ptr, 17);
}

static inline PCSXRMemFlag MemBlockFlag(unsigned char blockFlags)
{
	if ((blockFlags & 0xF0) == 0xA0) {
		if ((blockFlags & 0xF) >= 1 && (blockFlags & 0xF) <= 3)
			return PCSXRMemFlagDeleted;
		else
			return PCSXRMemFlagFree;
	} else if ((blockFlags & 0xF0) == 0x50) {
		if ((blockFlags & 0xF) == 0x1)
			return PCSXRMemFlagUsed;
		else if ((blockFlags & 0xF) == 0x2)
			return PCSXRMemFlagLink;
		else if ((blockFlags & 0xF) == 0x3)
			return PCSXRMemFlagEndLink;
	} else
		return PCSXRMemFlagFree;
	
	//Xcode complains unless we do this...
	//NSLog(@"Unknown flag %x", blockFlags);
	return PCSXRMemFlagFree;
}


NSArray *CreateArrayByEnumeratingMemoryCardAtURL(NSURL *location)
{
	NSMutableArray *memArray = [[NSMutableArray alloc] initWithCapacity:MAX_MEMCARD_BLOCKS];
	if (!location) {
		return nil;
	}
	NSData *fileData = [[NSData alloc] initWithContentsOfURL:location options:NSDataReadingMappedIfSafe error:NULL];
	if (!fileData) {
		return nil;
	}
	
	const unsigned char *memPtr = [fileData bytes];
	if ([fileData length] == MCD_SIZE + 64)
		memPtr += 64;
	else if([fileData length] == MCD_SIZE + 3904)
		memPtr += 3904;
	else if ([fileData length] != MCD_SIZE)
		return nil;
	
	int i = 0, x;
	while (i < MAX_MEMCARD_BLOCKS) {
		x = 1;
		McdBlock memBlock;
		GetSoloBlockInfo((unsigned char *)memPtr, i + 1, &memBlock);
		
		if (MemBlockFlag(memBlock.Flags) == PCSXRMemFlagFree) {
			//Free space: ignore
			i++;
			continue;
		}
		while (i + x < MAX_MEMCARD_BLOCKS)  {
			McdBlock tmpBlock;
			GetSoloBlockInfo((unsigned char *)memPtr, i + x + 1, &tmpBlock);
			if ((tmpBlock.Flags & 0x3) == 0x3) {
				x++;
				break;
			} else if ((tmpBlock.Flags & 0x2) == 0x2) {
				x++;
			} else {
				break;
			}
		};
		PcsxrMemoryObject *obj = [[PcsxrMemoryObject alloc] initWithMcdBlock:&memBlock startingIndex:i size:x];
		i += x;
		if (MemBlockFlag(memBlock.Flags) == PCSXRMemFlagDeleted) {
			continue;
		}
		[memArray addObject:obj];
	}
	
	return [[NSArray alloc] initWithArray:memArray];
}