-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cart.cs
262 lines (198 loc) · 7.31 KB
/
Cart.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace flashkit_md
{
class Cart
{
public const int MAP_2M = 1;
public const int MAP_3M = 2;
public const int MAP_SSF = 3;
static string getRomRegion(byte[] rom_hdr)
{
byte val = rom_hdr[0x1f0];
if (val != rom_hdr[0x1f1] && rom_hdr[0x1f1] != 0x20 && rom_hdr[0x1f1] != 0) return "W";
switch (val)
{
case (byte)'F':
case (byte)'C':
return "W";
case (byte)'U':
case (byte)'W':
case (byte)'4':
case 4:
return "U";
case (byte)'J':
case (byte)'B':
case (byte)'1':
case 1:
return "J";
case (byte)'E':
case (byte)'A':
case (byte)'8':
case 8:
return "E";
}
return "X";
}
public static string getRomName()
{
string name = null;
byte[] rom_hdr = new byte[512];
Device.setAddr(0);
Device.read(rom_hdr, 0, 512);
name = getRomName(0x120, rom_hdr);
if (name == null) name = getRomName(0x150, rom_hdr);
if (name == null) name = "Unknown";
name += " (" + getRomRegion(rom_hdr)+")";
return name;
}
static string getRomName(int offset, byte []buff)
{
string name = "";
int name_empty = 1;
for (int i = offset + 47; i >= offset; i--)
{
if (buff[i] != 0 & buff[i] != 0x20) break;
if (buff[i] == 0x20) buff[i] = 0;
}
for (int i = offset; i < offset + 48; i++)
{
if (buff[i] == 0) break;
if (buff[i] == '/' || buff[i] == ':') buff[i] = (byte)'-';
try
{
name += (char)buff[i];
if (buff[i] != 0x20) name_empty = 0;
if (buff[i] == ' ' || buff[i] == '!' || buff[i] == '(' || buff[i] == ')' || buff[i] == '_' || buff[i] == '-') continue;
if (buff[i] == '.' || buff[i] == '[' || buff[i] == ']' || buff[i] == '|') continue;
if (buff[i] == '&' || buff[i] == 0x27 || buff[i] == 0x60) continue;
if (buff[i] >= '0' && buff[i] <= '9') continue;
if (buff[i] >= 'A' && buff[i] <= 'Z') continue;
if (buff[i] >= 'a' && buff[i] <= 'z') continue;
throw new Exception("name error");
}
catch (Exception)
{
return null;
}
}
if (name_empty != 0) return null;
return name;
}
static int checkRomSize(int base_addr, int max_len)
{
int eq;
int base_len = 0x8000;
int len = 0x8000;
byte[] sector0 = new byte[256];
byte[] sector = new byte[256];
Device.writeWord(0xA13000, 0x0000);
Device.setAddr(base_addr);
Device.read(sector0, 0, sector.Length);
for (; ; )
{
Device.setAddr(base_addr + len);
Device.read(sector, 0, sector.Length);
eq = 1;
for (int i = 0; i < sector.Length; i++) if (sector0[i] != sector[i]) eq = 0;
if (eq == 1) break;
len *= 2;
if (len >= max_len) break;
}
if (len == base_len) return 0;
return len;
}
public static int getRomSize()
{
byte[] sector0 = new byte[512];
byte[] sector = new byte[512];
int ram = 0;
int extra_rom = 0;
if (ramAvailable())
{
ram = 1;
extra_rom = 1;
Device.writeWord(0xA13000, 0x0000);
Device.setAddr(0x200000);
Device.read(sector0, 0, 512);
Device.setAddr(0x200000);
Device.read(sector, 0, 512);
for (int i = 0; i < sector.Length; i++) if (sector[i] != sector0[i]) extra_rom = 0;
if (extra_rom != 0)
{
extra_rom = 0;
Device.setAddr(0x200000 + 0x10000);
Device.read(sector, 0, 512);
Device.writeWord(0xA13000, 0xffff);
Device.setAddr(0x200000);
Device.read(sector, 0, 512);
for (int i = 0; i < sector.Length; i++) if (sector[i] != sector0[i]) extra_rom = 1;
}
}
int max_rom_size = ram != 0 && extra_rom == 0 ? 0x200000 : 0x400000;
int len = checkRomSize(0, max_rom_size);
if (len == 0x400000)
{
len = 0x200000;
int len2 = checkRomSize(0x200000, 0x200000);
if (len2 == 0x200000)
{
len2 = checkRomSize(0x300000, 0x100000);
len2 = len2 >= 0x80000 ? 0x200000 : 0x100000;
}
if (len2 >= 0x80000) len += len2;
}
return len;
}
static bool ramAvailable()
{
int first_word;
UInt16 tmp;
Device.writeWord(0xA13000, 0xffff);
first_word = Device.readWord(0x200000);
Device.writeWord(0x200000, (UInt16)(first_word ^ 0xffff));
tmp = Device.readWord(0x200000);
Device.writeWord(0x200000, (UInt16)first_word);
tmp ^= 0xffff;
if ((first_word & 0x00ff) != (tmp & 0x00ff)) return false;
return true;
}
public static int getRamSize()
{
int ram_szie = 256;
int first_word;
int first_word_tmp;
UInt16 tmp;
UInt16 tmp2;
int ram_type = 0x00ff;
//Device.writeWord(0xA13000, 0x0001);
if (!ramAvailable()) return 0;
first_word = Device.readWord(0x200000);
while (ram_szie < 0x100000)
{
tmp = Device.readWord(0x200000 + ram_szie);
Device.writeWord(0x200000 + ram_szie, (UInt16)(tmp ^ 0xffff));
tmp2 = Device.readWord(0x200000 + ram_szie);
first_word_tmp = Device.readWord(0x200000);
Device.writeWord(0x200000 + ram_szie, tmp);
tmp2 ^= 0xffff;
if ((tmp & 0xff) != (tmp2 & 0xff)) break;
if ((first_word & ram_type) != (first_word_tmp & ram_type)) break;
ram_szie *= 2;
}
return ram_szie / 2;
}
public static byte[] getRam()
{
byte[] buff = null;
int ram_size = 0;
ram_size = Cart.getRamSize() * 2;
buff = new byte[ram_size];
Device.writeWord(0xA13000, 0xffff);
Device.setAddr(0x200000);
Device.read(buff, 0, buff.Length);
return buff;
}
}
}