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
|
//******************************************
// NGP MODULE
//******************************************
#ifdef enable_NGP
static const char ngpMenuItem1[] PROGMEM = "Read Rom";
static const char ngpMenuItem2[] PROGMEM = "Read chip info";
static const char ngpMenuItemReset[] PROGMEM = "Reset";
static const char* const menuOptionsNGP[] PROGMEM = { ngpMenuItem1, ngpMenuItem2, ngpMenuItemReset };
static const char ngpRomItem1[] PROGMEM = "4 Mbits / 512 KB";
static const char ngpRomItem2[] PROGMEM = "8 Mbits / 1 MB";
static const char ngpRomItem3[] PROGMEM = "16 Mbits / 2 MB";
static const char ngpRomItem4[] PROGMEM = "32 Mbits / 4 MB";
static const char* const ngpRomOptions[] PROGMEM = { ngpRomItem1, ngpRomItem2, ngpRomItem3, ngpRomItem4 };
char ngpRomVersion[3];
uint8_t ngpSystemType;
uint8_t manufacturerID;
uint8_t deviceID;
void setup_NGP() {
// A0 - A7
DDRF = 0xff;
// A8 - A15
DDRK = 0xff;
// A16 - A20
DDRL = 0xff;
// D0 - D7
DDRC = 0x00;
// controls
// /CE0: PH3
// /CE1: PH0
// /OE: PH6
// /WE: PH5
// PWR: PH4
DDRH |= ((1 << 0) | (1 << 3) | (1 << 5) | (1 << 6));
DDRH &= ~(1 << 4);
PORTH |= ((1 << 0) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6));
println_Msg(F("Initializing..."));
display_Update();
if (getCartInfo_NGP())
printCartInfo_NGP();
else
print_Error(F("Cartridge read error"), true);
}
void ngpMenu() {
uint8_t mainMenu;
convertPgm(menuOptionsNGP, 3);
mainMenu = question_box(F("NGP Menu"), menuOptions, 3, 0);
switch (mainMenu) {
case 0:
sd.chdir("/");
readROM_NGP(filePath, FILEPATH_LENGTH);
sd.chdir("/");
break;
case 1:
scanChip_NGP();
break;
case 2:
resetArduino();
break;
}
println_Msg(F(""));
println_Msg(F("Press Button..."));
display_Update();
wait();
}
bool getCartInfo_NGP() {
uint8_t* tmp;
// enter autoselect mode
dataOut();
writeByte_NGP(0x555, 0xaa);
writeByte_NGP(0x2aa, 0x55);
writeByte_NGP(0x555, 0x90);
dataIn();
cartSize = 0;
// get chip manufacturer and device IDs
manufacturerID = readByte_NGP(0);
deviceID = readByte_NGP(1);
tmp = (uint8_t*)&romSize;
*(tmp + 0) = deviceID;
*(tmp + 1) = manufacturerID;
switch (romSize) {
case 0xffff: return false; break; // detection error (no cart inserted or hw problem)
case 0x98ab: cartSize = 524288; break; // 4 Mbits - Toshiba
case 0x204c: cartSize = 524288; break; // 4 Mbits - STMicroelectronics ?
case 0x982c: cartSize = 1048576; break; // 8 Mbits - Toshiba
case 0xec2c: cartSize = 1048576; break; // 8 Mbits - Samsung
case 0x982f: cartSize = 2097152; break; // 16 Mbits - Toshiba
case 0xec2f: cartSize = 2097152; break; // 16 Mbits - Samsung
}
// reset to read mode
dataOut();
writeByte_NGP(0x0, 0xf0);
// confirm NGP cart recognition
dataIn();
for (uint32_t addr = 0; addr < 28; addr++)
sdBuffer[addr] = readByte_NGP(addr);
if (memcmp_P(sdBuffer, PSTR("COPYRIGHT BY SNK CORPORATION"), 28) != 0 && memcmp_P(sdBuffer, PSTR(" LICENSED BY SNK CORPORATION"), 28) != 0)
return false;
// get app ID
snprintf(cartID, 5, "%02X%02X", readByte_NGP(0x21), readByte_NGP(0x20));
// force rom size to 32 Mbits for few titles
if (strcmp(cartID, "0060") == 0 || strcmp(cartID, "0061") == 0 || strcmp(cartID, "0069") == 0)
cartSize = 4194304;
// get app version
snprintf(ngpRomVersion, 3, "%02X", readByte_NGP(0x22));
// get app system compatibility
ngpSystemType = readByte_NGP(0x23);
// get app name
for (uint32_t i = 0; i < 17; i++)
romName[i] = readByte_NGP(0x24 + i);
return true;
}
void printCartInfo_NGP() {
display_Clear();
println_Msg(F("NGP Cart Info"));
print_Msg(F("Name: "));
println_Msg(romName);
print_Msg(F("App ID: "));
println_Msg(cartID);
print_Msg(F("App version: "));
println_Msg(ngpRomVersion);
print_Msg(F("System: "));
if (ngpSystemType == 0x0)
println_Msg(F("NGPMonochrome"));
else if (ngpSystemType == 0x10)
println_Msg(F("NGPColor"));
else
println_Msg(F("Unknown"));
print_Msg(F("ROM Size: "));
if (cartSize == 0) {
println_Msg(F("Unknown"));
} else {
print_Msg((cartSize >> 17));
println_Msg(F(" Mbits"));
}
println_Msg(F("Press Button..."));
display_Update();
wait();
}
void readROM_NGP(char* outPathBuf, size_t bufferSize) {
// Set cartsize manually if chip ID is unknown
if (cartSize == 0) {
unsigned char ngpRomMenu;
// Copy menuOptions out of progmem
convertPgm(ngpRomOptions, 4);
ngpRomMenu = question_box(F("Select ROM size"), menuOptions, 4, 0);
// wait for user choice to come back from the question box menu
switch (ngpRomMenu) {
case 0: cartSize = 524288; break;
case 1: cartSize = 1048576; break;
case 2: cartSize = 2097152; break;
case 3: cartSize = 4194304; break;
}
}
// generate fullname of rom file
snprintf(fileName, FILENAME_LENGTH, "%s.ngp", romName);
// create a new folder for storing rom file
EEPROM_readAnything(0, foldern);
snprintf(folder, sizeof(folder), "NGP/ROM/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
// filling output file path to buffer
if (outPathBuf != NULL && bufferSize > 0)
snprintf(outPathBuf, bufferSize, "%s/%s", folder, fileName);
display_Clear();
print_Msg(F("Saving to "));
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_Error(F("Can't create file on SD"), true);
// write new folder number back to EEPROM
foldern++;
EEPROM_writeAnything(0, foldern);
// back to read mode
dataOut();
writeByte_NGP(0x0, 0xf0);
// read rom
dataIn();
uint32_t progress = 0;
draw_progressbar(0, cartSize);
for (uint32_t addr = 0; addr < cartSize; addr += 512) {
// blink LED
if ((addr & ((1 << 14) - 1)) == 0)
blinkLED();
// read block
for (uint32_t i = 0; i < 512; i++)
sdBuffer[i] = readByte_NGP(addr + i);
myFile.write(sdBuffer, 512);
progress += 512;
draw_progressbar(progress, cartSize);
}
myFile.close();
}
void scanChip_NGP() {
display_Clear();
uint32_t block_addr = 0;
// generate name of report file
snprintf(fileName, FILENAME_LENGTH, "%s.txt", romName);
// create a new folder to save report file
EEPROM_readAnything(0, foldern);
snprintf(folder, sizeof(folder), "NGP/ROM/%s/%d", romName, foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
print_Msg(F("Saving chip report to "));
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_Error(F("Can't create file on SD"), true);
// write new folder number back to EEPROM
foldern++;
EEPROM_writeAnything(0, foldern);
// write software info to report file
myFile.println("Name: " + String(romName));
myFile.println("App ID: " + String(cartID));
myFile.println("App version: " + String(ngpRomVersion));
myFile.println("");
// write chip info to report file
myFile.println("Chip manufacturer ID : 0x" + String(manufacturerID, HEX));
myFile.println("Chip device ID : 0x" + String(deviceID, HEX));
myFile.println("");
if (cartSize == 0)
myFile.println("Cart size unknown");
else {
// enter autoselect mode
dataOut();
writeByte_NGP(0x555, 0xaa);
writeByte_NGP(0x2aa, 0x55);
writeByte_NGP(0x555, 0x90);
dataIn();
uint32_t addrMax;
uint8_t sectorID = 0;
// For 32Mbits carts, skip the 2nd 16Mbits chip
if (cartSize == 4194304) {
myFile.println("Warning: this cart is 32Mbits. Only the first 16Mbits chip will be scanned.");
myFile.println("");
addrMax = 2097152;
} else
addrMax = cartSize;
myFile.println("Sector | Start address | Status");
// browse sectors
for (uint32_t addr = 0; addr < addrMax; addr += 0x1000) {
if ((addr % 0x10000 == 0) || (addr == addrMax - 0x8000) || (addr == addrMax - 0x6000) || (addr == addrMax - 0x4000)) {
myFile.print("#" + String(sectorID) + " | 0x" + String(addr, HEX) + " | ");
// check the protection status
if (readByte_NGP(addr + 0x2) == 0)
myFile.println("unprotected");
else
myFile.println("protected");
sectorID += 1;
}
}
myFile.close();
writeByte_NGP(0x00, 0xf0);
}
}
void writeByte_NGP(uint32_t addr, uint8_t data) {
PORTF = addr & 0xff;
PORTK = (addr >> 8) & 0xff;
PORTL = (addr >> 16) & 0x1f;
PORTC = data;
// which chip to select
// 0x000000 - 0x1fffff -> /CE0
// 0x200000 - 0x3fffff -> /CE1
data = (addr & 0x00200000 ? (1 << 0) : (1 << 3));
PORTH &= ~data;
PORTH &= ~(1 << 5);
NOP;
PORTH |= data;
PORTH |= (1 << 5);
NOP;
NOP;
}
uint8_t readByte_NGP(uint32_t addr) {
uint8_t data;
PORTF = addr & 0xff;
PORTK = (addr >> 8) & 0xff;
PORTL = (addr >> 16) & 0x1f;
// which chip to select
// 0x000000 - 0x1fffff -> /CE0
// 0x200000 - 0x3fffff -> /CE1
data = (addr & 0x00200000 ? (1 << 0) : (1 << 3));
PORTH &= ~data;
PORTH &= ~(1 << 6);
NOP;
NOP;
NOP;
data = PINC;
PORTH |= data;
PORTH |= (1 << 6);
return data;
}
#endif
//******************************************
// End of File
//******************************************
|