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
|
//******************************************
// WSV MODULE
//******************************************
#ifdef ENABLE_WSV
// Watara Supervision
// Cartridge Pinout
// 40P 2.5mm pitch connector
//
// BACK LABEL
// SIDE SIDE
// +-------+
// /RD -| 1 40 |- +5V
// A0 -| 2 39 |- nc
// A1 -| 3 38 |- nc
// A2 -| 4 37 |- nc
// A3 -| 5 36 |- nc
// A4 -| 6 35 |- /WR
// A5 -| 7 34 |- D0
// A6 -| 8 33 |- D1
// A7 -| 9 32 |- D2
// A8 -| 10 31 |- D3
// A9 -| 11 30 |- D4
// A10 -| 12 29 |- D5
// A11 -| 13 28 |- D6
// A12 -| 14 27 |- D7
// A13 -| 15 26 |- nc
// A14 -| 16 25 |- nc
// A15 -| 17 24 |- L1
// A16 -| 18 23 |- L2
// L3 -| 19 22 |- GND
// L0 -| 20 21 |- PWR GND
// +-------+
//
// L3 - L0 are the Link Port's I/O - only the 'MAGNUM' variant
// routed these to the cartridge slot as additional banking bits.
//
// CONTROL PINS:
// /WR - (PH5)
// /RD - (PH6)
const word WSV[] PROGMEM = { 32, 64, 512 };
byte wsvlo = 0; // Lowest Entry
byte wsvhi = 2; // Highest Entry
byte wsvsize;
byte newwsvsize;
// EEPROM MAPPING
// 08 ROM SIZE
//******************************************
// SETUP
//******************************************
void setup_WSV() {
// Request 3.3V
setVoltage(VOLTS_SET_3V3);
// Set Address Pins to Output
//A0-A7
DDRF = 0xFF;
//A8-A15
DDRK = 0xFF;
//BA0-BA7 (BA5-BA7 UNUSED)
DDRL = 0xFF;
//PA0-PA7 (UNUSED)
DDRA = 0xFF;
// Set Data Pins (D0-D7) to Input
// D0 - D7
DDRC = 0x00;
// Set Control Pins to Output
// WR(PH5) RD(PH6)
// DDRH |= (1 << 5) | (1 << 6);
// ---(PH0) ---(PH1) ---(PH3) ---(PH4) /WR(PH5) /RD(PH6)
DDRH |= (1 << 0) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6);
// Switch WR(PH5) to HIGH
// PORTH |= (1 << 5);
// Switch RD(PH6) to LOW
// PORTH &= ~(1 << 6);
// Setting Control Pins to HIGH
// ---(PH0) ---(PH1) ---(PH3) ---(PH4) /WR(PH5)
PORTH |= (1 << 0) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 5);
// Switch RD(PH6) to LOW
PORTH &= ~(1 << 6);
// Set Unused Pins HIGH
PORTL = 0xE0;
PORTA = 0xFF;
PORTJ |= (1 << 0); // TIME(PJ0)
checkStatus_WSV();
strcpy(romName, "SUPERVISION");
mode = CORE_WSV;
}
//******************************************
// MENU
//******************************************
// Base Menu
static const char* const menuOptionsSV[] PROGMEM = { FSTRING_SELECT_CART, FSTRING_READ_ROM, FSTRING_SET_SIZE, FSTRING_RESET };
void wsvMenu() {
convertPgm(menuOptionsSV, 4);
uint8_t mainMenu = question_box(F("SUPERVISION MENU"), menuOptions, 4, 0);
switch (mainMenu) {
case 0:
// Select Cart
setCart_WSV();
setup_WSV();
break;
case 1:
// Read Rom
sd.chdir("/");
readROM_WSV();
sd.chdir("/");
break;
case 2:
setROMSize_WSV();
break;
case 3:
// reset
resetArduino();
break;
}
}
//******************************************
// LOW LEVEL FUNCTIONS
//******************************************
// WRITE
void controlOut_WSV() {
// Switch RD(PH6) to HIGH
PORTH |= (1 << 6);
// Switch WR(PH5) to LOW
PORTH &= ~(1 << 5);
}
// READ
void controlIn_WSV() {
// Switch WR(PH5) to HIGH
PORTH |= (1 << 5);
// Switch RD(PH6) to LOW
PORTH &= ~(1 << 6);
}
void dataIn_WSV() {
DDRC = 0x00;
}
void dataOut_WSV() {
DDRC = 0xFF;
}
uint8_t readByte_WSV(uint32_t addr) {
PORTF = addr & 0xFF;
PORTK = (addr >> 8) & 0xFF;
PORTL = (addr >> 16) & 0xFF;
// Wait for data bus
// 6 x 62.5ns = 375ns
NOP;
NOP;
NOP;
NOP;
NOP;
NOP;
uint8_t ret = PINC;
NOP;
return ret;
}
//******************************************
// READ CODE
//******************************************
void readROM_WSV() {
strcpy(fileName, romName);
strcat(fileName, ".sv");
// create a new folder for storing rom file
EEPROM_readAnything(0, foldern);
//sprintf(folder, "WSV/ROM/%s/%d", romName, foldern);
sprintf(folder, "WSV/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);
// start reading rom
dataIn_WSV();
controlIn_WSV();
romSize = pgm_read_word(&(WSV[wsvsize]));
uint32_t romStart = 0;
if (romSize < 64)
romStart = 0x8000;
uint32_t romEnd = (uint32_t)romSize * 0x400;
for (uint32_t addr = 0; addr < romEnd; addr += 512) {
for (uint16_t w = 0; w < 512; w++)
sdBuffer[w] = readByte_WSV(romStart + addr + w);
myFile.write(sdBuffer, 512);
}
myFile.close();
// Compare CRC32 to database and rename ROM if found
// Arguments: database name, precalculated crc string or 0 to calculate, rename rom or not, starting offset
compareCRC("wsv.txt", 0, 1, 0);
println_Msg(FS(FSTRING_EMPTY));
// 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_WSV() {
#if (defined(ENABLE_OLED) || defined(ENABLE_LCD))
display_Clear();
if (wsvlo == wsvhi)
newwsvsize = wsvlo;
else {
uint8_t b = 0;
int i = wsvlo;
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_word(&(WSV[i])));
println_Msg(FS(FSTRING_EMPTY));
#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 == wsvlo)
i = wsvhi;
else
i--;
// Only update display after input because of slow LCD library
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_word(&(WSV[i])));
println_Msg(FS(FSTRING_EMPTY));
#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 == wsvhi)
i = wsvlo;
else
i++;
// Only update display after input because of slow LCD library
display_Clear();
print_Msg(F("ROM Size: "));
println_Msg(pgm_read_word(&(WSV[i])));
println_Msg(FS(FSTRING_EMPTY));
#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)
newwsvsize = i;
break;
}
}
display.setCursor(0, 56); // Display selection at bottom
}
print_Msg(F("ROM SIZE "));
print_Msg(pgm_read_word(&(WSV[newwsvsize])));
println_Msg(F("K"));
display_Update();
delay(1000);
#else
if (wsvlo == wsvhi)
newwsvsize = wsvlo;
else {
setrom:
String sizeROM;
for (int i = 0; i < (wsvhi - wsvlo + 1); i++) {
Serial.print(F("Select ROM Size: "));
Serial.print(i);
Serial.print(F(" = "));
Serial.print(pgm_read_word(&(WSV[i + wsvlo])));
Serial.println(F("K"));
}
Serial.print(F("Enter ROM Size: "));
while (Serial.available() == 0) {}
sizeROM = Serial.readStringUntil('\n');
Serial.println(sizeROM);
newwsvsize = sizeROM.toInt() + wsvlo;
if (newwsvsize > wsvhi) {
Serial.println(F("SIZE NOT SUPPORTED"));
Serial.println(FS(FSTRING_EMPTY));
goto setrom;
}
}
Serial.print(F("ROM Size = "));
Serial.print(pgm_read_word(&(WSV[newwsvsize])));
Serial.println(F("K"));
#endif
EEPROM_writeAnything(8, newwsvsize);
wsvsize = newwsvsize;
}
void checkStatus_WSV() {
EEPROM_readAnything(8, wsvsize);
if (wsvsize > 2) {
wsvsize = 1; // default 64K
EEPROM_writeAnything(8, wsvsize);
}
#if (defined(ENABLE_OLED) || defined(ENABLE_LCD))
display_Clear();
println_Msg(F("WATARA SUPERVISION"));
println_Msg(F("CURRENT SETTINGS"));
println_Msg(FS(FSTRING_EMPTY));
print_Msg(F("ROM SIZE: "));
print_Msg(pgm_read_word(&(WSV[wsvsize])));
println_Msg(F("K"));
display_Update();
wait();
#else
Serial.print(F("CURRENT ROM SIZE: "));
Serial.print(pgm_read_word(&(WSV[wsvsize])));
Serial.println(F("K"));
Serial.println(FS(FSTRING_EMPTY));
#endif
}
//******************************************
// CART SELECT CODE
//******************************************
void setCart_WSV() {
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("wsv.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(FS(FSTRING_EMPTY));
println_Msg(gamename);
print_Msg(F("Size: "));
if (cartSize == 51)
print_Msg(F("512"));
else
print_Msg(cartSize);
println_Msg(F("KB"));
println_Msg(FS(FSTRING_EMPTY));
#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();
uint8_t 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) {
//word WSV[] = {32,64,512};
switch (cartSize) {
case 32:
wsvsize = 0;
break;
case 64:
wsvsize = 1;
break;
case 51:
wsvsize = 2;
break;
}
EEPROM_writeAnything(8, wsvsize);
myFile.close();
break;
}
}
}
} else {
print_FatalError(F("Database file not found"));
}
}
#endif
//******************************************
// End of File
//******************************************
|