aboutsummaryrefslogtreecommitdiffhomepage
path: root/Cart_Reader/WSV.ino
blob: ebe3c7b4d60cd9a71ca2f09921172bbbcdae45ca (plain)
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
//******************************************
// 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;

// 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() {
  createFolder("WSV", "ROM", romName, "sv");

  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
//******************************************

#if (defined(ENABLE_OLED) || defined(ENABLE_LCD))
void printRomSize_WSV(int index) {
    display_Clear();
    print_Msg(F("ROM Size: "));
    println_Msg(pgm_read_word(&(WSV[index])));
}
#endif

void setROMSize_WSV() {
  byte newwsvsize;
#if (defined(ENABLE_OLED) || defined(ENABLE_LCD))
  display_Clear();
  if (wsvlo == wsvhi)
    newwsvsize = wsvlo;
  else {
    newwsvsize = navigateMenu(wsvlo, wsvhi, &printRomSize_WSV);

    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(FS(FSTRING_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
//******************************************
struct database_entry_WSV {
  char crc_search[9];
  byte gameSize;
};

void readDataLine_WSV(FsFile& database, void* entry) {
  struct database_entry_WSV* castEntry = (database_entry_WSV*)entry;
  // Read CRC32 checksum
  for (byte i = 0; i < 8; i++) {
    checksumStr[i] = char(database.read());
  }

  // Skip over semicolon
  database.seekCur(1);

  // Read CRC32 of first 512 bytes
  for (byte i = 0; i < 8; i++) {
    castEntry->crc_search[i] = char(database.read());
  }

  // Skip over semicolon
  database.seekCur(1);

  // Read rom size
  // Read the next ascii character and subtract 48 to convert to decimal
  castEntry->gameSize = ((database.read() - 48) * 10) + (database.read() - 48);

  // Skip rest of line
  database.seekCur(2);
}

void printDataLine_WSV(void* entry) {
  struct database_entry_WSV* castEntry = (database_entry_WSV*)entry;
  print_Msg(F("Size: "));
  if (castEntry->gameSize == 51)
    print_Msg(F("512"));
  else
    print_Msg(castEntry->gameSize);
  println_Msg(F("KB"));
}

void setCart_WSV() {
  //go to root
  sd.chdir();

  struct database_entry_WSV entry;

  // Select starting letter
  byte myLetter = starting_letter();

  // Open database
  if (myFile.open("wsv.txt", O_READ)) {
    seek_first_letter_in_database(myFile, myLetter);

    if(checkCartSelection(myFile, &readDataLine_WSV, &entry, &printDataLine_WSV)) {
      //word WSV[] = {32,64,512};
      switch (entry.gameSize) {
        case 32:
          wsvsize = 0;
          break;

        case 64:
          wsvsize = 1;
          break;

        case 51:
          wsvsize = 2;
          break;
        }
        EEPROM_writeAnything(8, wsvsize);
    }
  } else {
    print_FatalError(FS(FSTRING_DATABASE_FILE_NOT_FOUND));
  }
}
#endif
//******************************************
// End of File
//******************************************