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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
//******************************************
// COLECOVISION MODULE
//******************************************
#ifdef enable_COLV
// Coleco Colecovision
// Cartridge Pinout
// 30P 2.54mm pitch connector
//
// FRONT SIDE BACK SIDE
// (EVEN) (ODD)
// +-------+
// /C000 -| 2 1 |- D2
// D3 -| 4 3 |- D1
// D4 -| 6 5 |- D0
// D5 -| 8 7 |- A0
// D6 -| 10 9 |- A1
// D7 -| 12 11 |- A2
// A11 -| 14 13 |- GND (SHLD)
// A10 -| 16 15 |- A3
// /8000 -| 18 17 |- A4
// A14 -| 20 19 |- A13
// /A000 -| 22 21 |- A5
// A12 -| 24 23 |- A6
// A9 -| 26 25 |- A7
// A8 -| 28 27 |- /E000
// VCC(+5V) -| 30 29 |- GND
// +-------+
// CONTROL PINS:
// CHIP SELECT PINS
// /8000(PH3) - CHIP 0 - SNES /CS
// /A000(PH4) - CHIP 1 - SNES /IRQ
// /C000(PH5) - CHIP 2 - SNES /WR
// /E000(PH6) - CHIP 3 - SNES /RD
const byte COL[] PROGMEM = { 8, 12, 16, 20, 24, 32 };
byte collo = 0; // Lowest Entry
byte colhi = 5; // Highest Entry
byte colsize;
byte newcolsize;
// EEPROM MAPPING
// 08 ROM SIZE
//******************************************
// Menu
//******************************************
// Base Menu
static const char colMenuItem1[] PROGMEM = "Select Cart";
static const char colMenuItem2[] PROGMEM = "Read ROM";
static const char colMenuItem3[] PROGMEM = "Set Size";
//static const char colMenuItem4[] PROGMEM = "Reset"; (stored in common strings array)
static const char* const menuOptionsCOL[] PROGMEM = { colMenuItem1, colMenuItem2, colMenuItem3, string_reset2 };
void setup_COL() {
// Set Address Pins to Output
// Colecovision uses A0-A14 [A15-A23 UNUSED]
//A0-A7
DDRF = 0xFF;
//A8-A15
DDRK = 0xFF;
//A16-A23
DDRL = 0xFF;
// Set Control Pins to Output
// ---(PH0) ---(PH1) /8000(PH3) /A000(PH4) /C000(PH5) /E000(PH6)
DDRH |= (1 << 0) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
// Set TIME(PJ0) to Output (UNUSED)
DDRJ |= (1 << 0);
// Set Pins (D0-D7) to Input
DDRC = 0x00;
// Setting Control Pins to HIGH
// ---(PH0) ---(PH1) /8000(PH3) /A000(PH4) /C000(PH5) /E000(PH6)
PORTH |= (1 << 0) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
// Set Unused Data Pins (PA0-PA7) to Output
DDRA = 0xFF;
// Set Unused Pins HIGH
PORTA = 0xFF;
PORTL = 0xFF; // A16-A23
PORTJ |= (1 << 0); // TIME(PJ0)
checkStatus_COL();
strcpy(romName, "COLECO");
mode = mode_COL;
}
void colMenu() {
vselect(false);
convertPgm(menuOptionsCOL, 4);
uint8_t mainMenu = question_box(F("COLECOVISION MENU"), menuOptions, 4, 0);
switch (mainMenu) {
case 0:
// Select Cart
setCart_COL();
setup_COL();
break;
case 1:
// Read ROM
sd.chdir("/");
readROM_COL();
sd.chdir("/");
break;
case 2:
// Set Size
setROMSize_COL();
break;
case 3:
// reset
resetArduino();
break;
}
}
//******************************************
// READ CODE
//******************************************
// CHIP SELECT CONTROL PINS
// /8000(PH3) - CHIP 0
// /A000(PH4) - CHIP 1
// /C000(PH5) - CHIP 2
// /E000(PH6) - CHIP 3
uint8_t readData_COL(uint32_t addr) {
// SELECT ROM CHIP - PULL /CE LOW
uint8_t chipdecode = ((addr >> 13) & 0x3);
if (chipdecode == 3) // CHIP 3
PORTH &= ~(1 << 6); // /E000 LOW (ENABLE)
else if (chipdecode == 2) // CHIP 2
PORTH &= ~(1 << 5); // /C000 LOW (ENABLE)
else if (chipdecode == 1) // CHIP 1
PORTH &= ~(1 << 4); // /A000 LOW (ENABLE)
else // CHIP 0
PORTH &= ~(1 << 3); // /8000 LOW (ENABLE)
PORTF = addr & 0xFF; // A0-A7
PORTK = (addr >> 8) & 0xFF; // A8-A15
// LATCH ADDRESS - PULL /CE HIGH
PORTH |= (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6); // ALL /CE HIGH (DISABLE)
uint8_t ret = PINC;
return ret;
}
void readSegment_COL(uint32_t startaddr, uint32_t endaddr) {
for (uint32_t addr = startaddr; addr < endaddr; addr += 512) {
for (int w = 0; w < 512; w++) {
uint8_t temp = readData_COL(addr + w);
sdBuffer[w] = temp;
}
myFile.write(sdBuffer, 512);
}
}
void readROM_COL() {
strcpy(fileName, romName);
strcat(fileName, ".col");
// create a new folder for storing rom file
EEPROM_readAnything(0, foldern);
// sprintf(folder, "COL/ROM/%s/%d", romName, foldern);
sprintf(folder, "COL/ROM/%d", foldern);
sd.mkdir(folder, true);
sd.chdir(folder);
display_Clear();
print_STR(saving_to_STR, 0);
print_Msg(folder);
println_Msg(F("/..."));
display_Update();
// open file on sdcard
if (!myFile.open(fileName, O_RDWR | O_CREAT))
print_FatalError(create_file_STR);
// write new folder number back to EEPROM
foldern++;
EEPROM_writeAnything(0, foldern);
// RESET ALL CS PINS HIGH (DISABLE)
PORTH |= (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
readSegment_COL(0x8000, 0xA000); // 8K
if (colsize > 0) {
readSegment_COL(0xA000, 0xB000); // +4K = 12K
if (colsize > 1) {
readSegment_COL(0xB000, 0xC000); // +4K = 16K
if (colsize > 2) {
readSegment_COL(0xC000, 0xD000); // +4K = 20K
if (colsize > 3) {
readSegment_COL(0xD000, 0xE000); // +4K = 24K
if (colsize > 4) {
readSegment_COL(0xE000, 0x10000); // +8K = 32K
}
}
}
}
}
myFile.close();
// RESET ALL CS PINS HIGH (DISABLE)
PORTH |= (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
// Compare CRC32 to database and rename ROM if found
compareCRC("colv.txt", 0, 1, 0);
println_Msg(F(""));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
}
//******************************************
// ROM SIZE
//******************************************
void setROMSize_COL() {
#if (defined(enable_OLED) || defined(enable_LCD))
display_Clear();
if (collo == colhi)
newcolsize = collo;
else {
int b = 0;
int i = collo;
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_byte(&(COL[i])));
println_Msg(F(""));
#if defined(enable_OLED)
print_STR(press_to_change_STR, 1);
print_STR(right_to_select_STR, 1);
#elif defined(enable_LCD)
print_STR(rotate_to_change_STR, 1);
print_STR(press_to_select_STR, 1);
#endif
display_Update();
while (1) {
b = checkButton();
if (b == 2) { // Previous (doubleclick)
if (i == collo)
i = colhi;
else
i--;
// Only update display after input because of slow LCD library
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_byte(&(COL[i])));
println_Msg(F(""));
#if defined(enable_OLED)
print_STR(press_to_change_STR, 1);
print_STR(right_to_select_STR, 1);
#elif defined(enable_LCD)
print_STR(rotate_to_change_STR, 1);
print_STR(press_to_select_STR, 1);
#endif
display_Update();
}
if (b == 1) { // Next (press)
if (i == colhi)
i = collo;
else
i++;
// Only update display after input because of slow LCD library
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_byte(&(COL[i])));
println_Msg(F(""));
#if defined(enable_OLED)
print_STR(press_to_change_STR, 1);
print_STR(right_to_select_STR, 1);
#elif defined(enable_LCD)
print_STR(rotate_to_change_STR, 1);
print_STR(press_to_select_STR, 1);
#endif
display_Update();
}
if (b == 3) { // Long Press - Execute (hold)
newcolsize = i;
break;
}
}
display.setCursor(0, 56); // Display selection at bottom
}
print_Msg(F("ROM SIZE "));
print_Msg(pgm_read_byte(&(COL[newcolsize])));
println_Msg(F("K"));
display_Update();
delay(1000);
#else
if (collo == colhi)
newcolsize = collo;
else {
setrom:
String sizeROM;
for (int i = 0; i < (colhi - collo + 1); i++) {
Serial.print(F("Select ROM Size: "));
Serial.print(i);
Serial.print(F(" = "));
Serial.print(pgm_read_byte(&(COL[i + collo])));
Serial.println(F("K"));
}
Serial.print(F("Enter ROM Size: "));
while (Serial.available() == 0) {}
sizeROM = Serial.readStringUntil('\n');
Serial.println(sizeROM);
newcolsize = sizeROM.toInt() + collo;
if (newcolsize > colhi) {
Serial.println(F("SIZE NOT SUPPORTED"));
Serial.println(F(""));
goto setrom;
}
}
Serial.print(F("ROM Size = "));
Serial.print(pgm_read_byte(&(COL[newcolsize])));
Serial.println(F("K"));
#endif
EEPROM_writeAnything(8, newcolsize);
colsize = newcolsize;
}
void checkStatus_COL() {
EEPROM_readAnything(8, colsize);
if (colsize > 5) {
colsize = 0;
EEPROM_writeAnything(8, colsize);
}
#if (defined(enable_OLED) || defined(enable_LCD))
display_Clear();
println_Msg(F("COLECOVISION READER"));
println_Msg(F("CURRENT SETTINGS"));
println_Msg(F(""));
print_Msg(F("ROM SIZE: "));
print_Msg(pgm_read_byte(&(COL[colsize])));
println_Msg(F("K"));
println_Msg(F(""));
// Prints string out of the common strings array either with or without newline
print_STR(press_button_STR, 1);
display_Update();
wait();
#else
Serial.print(F("CURRENT ROM SIZE: "));
Serial.print(pgm_read_byte(&(COL[colsize])));
Serial.println(F("K"));
Serial.println(F(""));
#endif
}
//******************************************
// CART SELECT CODE
//******************************************
void setCart_COL() {
char gamename[100];
char tempStr2[2];
char crc_search[9];
//go to root
sd.chdir();
// Select starting letter
byte myLetter = starting_letter();
// Open database
if (myFile.open("colv.txt", O_READ)) {
// Skip ahead to selected starting letter
if ((myLetter > 0) && (myLetter <= 26)) {
while (myFile.available()) {
// Read current name
get_line(gamename, &myFile, 96);
// Compare selected letter with first letter of current name until match
while (gamename[0] != 64 + myLetter) {
skip_line(&myFile);
skip_line(&myFile);
get_line(gamename, &myFile, 96);
}
break;
}
// Rewind one line
rewind_line(myFile);
}
// Display database
while (myFile.available()) {
display_Clear();
// Read game name
get_line(gamename, &myFile, 96);
// Read CRC32 checksum
sprintf(checksumStr, "%c", myFile.read());
for (byte i = 0; i < 7; i++) {
sprintf(tempStr2, "%c", myFile.read());
strcat(checksumStr, tempStr2);
}
// Skip over semicolon
myFile.seekCur(1);
// Read CRC32 of first 512 bytes
sprintf(crc_search, "%c", myFile.read());
for (byte i = 0; i < 7; i++) {
sprintf(tempStr2, "%c", myFile.read());
strcat(crc_search, tempStr2);
}
// Skip over semicolon
myFile.seekCur(1);
// Read rom size
// Read the next ascii character and subtract 48 to convert to decimal
cartSize = myFile.read() - 48;
// Remove leading 0 for single digit cart sizes
if (cartSize != 0) {
cartSize = cartSize * 10 + myFile.read() - 48;
} else {
cartSize = myFile.read() - 48;
}
// Skip rest of line
myFile.seekCur(2);
// Skip every 3rd line
skip_line(&myFile);
println_Msg(F("Select your cartridge"));
println_Msg(F(""));
println_Msg(gamename);
print_Msg(F("Size: "));
print_Msg(cartSize);
println_Msg(F("KB"));
println_Msg(F(""));
#if defined(enable_OLED)
print_STR(press_to_change_STR, 1);
print_STR(right_to_select_STR, 1);
#elif defined(enable_LCD)
print_STR(rotate_to_change_STR, 1);
print_STR(press_to_select_STR, 1);
#elif defined(SERIAL_MONITOR)
println_Msg(F("U/D to Change"));
println_Msg(F("Space to Select"));
#endif
display_Update();
int b = 0;
while (1) {
// Check button input
b = checkButton();
// Next
if (b == 1) {
break;
}
// Previous
else if (b == 2) {
rewind_line(myFile, 6);
break;
}
// Selection
else if (b == 3) {
//byte COL[] = {8, 12, 16, 20, 24, 32};
switch (cartSize) {
case 8:
colsize = 0;
break;
case 12:
colsize = 1;
break;
case 16:
colsize = 2;
break;
case 20:
colsize = 3;
break;
case 24:
colsize = 4;
break;
case 32:
colsize = 5;
break;
default:
colsize = 0;
break;
}
EEPROM_writeAnything(8, colsize);
myFile.close();
break;
}
}
}
} else {
print_FatalError(F("Database file not found"));
}
}
#endif
//******************************************
// End of File
//******************************************
|