aboutsummaryrefslogtreecommitdiffhomepage
path: root/Cart_Reader/LYNX.ino
blob: 001ed27948ac700b053dd1dba3aeeb4f46f945cf (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
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
//******************************************
// ATARI LYNX MODULE
//******************************************
//
// For use with SNES-Lynx adapter
// +----+
// |  1 |- GND
// |  2 |- D3
// |  3 |- D2
// |  4 |- D4
// |  5 |- D1
// |  6 |- D5
// |  7 |- D0
// |  8 |- D6
// |  9 |- D7
// | 10 |- /OE
// | 11 |- A1
// | 12 |- A2
// | 13 |- A3
// | 14 |- A6
// | 15 |- A4
// | 16 |- A5
// | 17 |- A0
// | 18 |- A7
// | 19 |- A16
// | 20 |- A17
// | 21 |- A18
// | 22 |- A19
// | 23 |- A15
// | 24 |- A14
// | 25 |- A13
// | 26 |- A12
// | 27 |- /WE
// | 28 |- A8
// | 29 |- A9
// | 30 |- A10
// | 31 |- VCC
// | 32 |- AUDIN
// | 33 |- VCC
// | 34 |- SWVCC
// +----+
//
// Version 1.2
// By @partlyhuman
// This implementation would not be possible without the invaluable
// documentation on
// https://atarilynxvault.com/
// by Igor (@theatarigamer) of K-Retro Gaming / Atari Lynx Vault
// and the reference implementation of the Lynx Cart Programmer Pi-Hat
// https://bitbucket.org/atarilynx/lynx/src/master/
// by Karri Kaksonen (whitelynx.fi) and Igor as well as countless contributions
// by the Atari Lynx community
//
#ifdef ENABLE_LYNX

#pragma region DEFS

#define LYNX_HEADER_SIZE 64
#define LYNX_WE 8
#define LYNX_OE 9
#define LYNX_AUDIN_ON 0x80000
#define LYNX_BLOCKADDR 2048UL
#define LYNX_BLOCKCOUNT 256
// Includes \0
static const char LYNX[5] = "LYNX";

// Cart information
static bool lynxUseAudin;
static uint16_t lynxBlockSize;

#pragma region LOWLEVEL

void setup_LYNX() {
  setVoltage(VOLTS_SET_5V);

  // Address pins output
  // A0-7, A8-A16 (A11 doesn't exist)
  DDRF = 0xff;
  DDRK = 0xff;
  DDRL = 0xff;

  // Data pins input
  DDRC = 0x00;

  // Control pins output
  // CE is tied low, not accessible
  pinMode(LYNX_WE, OUTPUT);
  pinMode(LYNX_OE, OUTPUT);
  digitalWrite(LYNX_WE, HIGH);
  digitalWrite(LYNX_OE, HIGH);

  strcpy(romName, LYNX);
  mode = CORE_LYNX;
}

static void dataDir_LYNX(byte direction) {
  DDRC = (direction == OUTPUT) ? 0xff : 0x00;
}

static void setAddr_LYNX(uint32_t addr) {
  PORTF = addr & 0xff;
  PORTK = (addr >> 8) & 0xff;
  // AUDIN connected to L3
  PORTL = ((addr >> 16) & 0b1111);
}

static uint8_t readByte_LYNX(uint32_t addr) {
  digitalWrite(LYNX_OE, HIGH);
  setAddr_LYNX(addr);
  digitalWrite(LYNX_OE, LOW);
  delayMicroseconds(20);
  uint8_t data = PINC;
  digitalWrite(LYNX_OE, HIGH);
  return data;
}

static bool waitPressButton_LYNX(bool ret = false) {
  print_STR(press_button_STR, true);
  display_Update();
  wait();
  return ret;
}

#pragma region HIGHLEVEL

static void compareStride_LYNX(uint8_t b, int i, int stride) {
  uint8_t other = readByte_LYNX(i + stride);
  if (other == 0xff || other == 0x00) {
    // If this is NOR flash, these in-between spaces should be formatted to all 1's
    // in which case, we DON'T report this as an unmirrored area
    // Additionally, we have encountered commercial carts where the maskrom is larger than it needs to be
    // and in this case, the maskrom will be initialized to 0s, not 1s.
    return;
  }
  if (b != other) {
    // if these bytes differ, they're likely in the same block, which means the block size contains both addresses (next POT beyond the stride)
    lynxBlockSize = max(lynxBlockSize, stride << 1);
  }
}

static bool detectCart_LYNX() {
  // Could omit logging to save a few bytes
  display_Clear();
  println_Msg(F("Identifying..."));

  lynxUseAudin = false;
  lynxBlockSize = 0;

  // Somewhat arbitrary, however many bytes would be unlikely to be
  // coincidentally mirrored
  const size_t DETECT_BYTES = 128;
  for (int i = 0; i < DETECT_BYTES && lynxBlockSize < LYNX_BLOCKADDR; i++) {
    uint8_t b = readByte_LYNX(i);
    // If any differences are detected when AUDIN=1, AUDIN is used to bankswitch
    // meaning we also use the maximum block size
    // (1024kb cart / 256 blocks = 4kb block bank switched between lower/upper 2kb blocks)
    if (b != readByte_LYNX(i + LYNX_AUDIN_ON)) {
      lynxUseAudin = true;
      lynxBlockSize = 2048;
      break;
    }
    // Identify mirroring of largest stride
    // Valid cart sizes of 128kb, 256kb, 512kb / 256 blocks = block sizes of 512b, 1024b, 2048b
    compareStride_LYNX(b, i, 256);
    compareStride_LYNX(b, i, 512);
    compareStride_LYNX(b, i, 1024);
  }

  if (lynxBlockSize == 0) {
    print_STR(error_STR, false);
    display_Update();
    wait();
    resetArduino();
  }
  print_Msg(F("AUDIN="));
  print_Msg(lynxUseAudin);
  print_Msg(F(" BLOCK="));
  println_Msg(lynxBlockSize);
  display_Update();
}

static void writeHeader_LYNX() {
  char header[LYNX_HEADER_SIZE] = {};
  // Magic number
  strcpy(header, LYNX);
  // Cart name (dummy)
  strcpy(header + 10, LYNX);
  // Manufacturer (dummy)
  strcpy(header + 42, LYNX);
  // Version
  header[8] = 1;
  // Bank 0 page size
  header[4] = lynxBlockSize & 0xff;
  // Bank 1 page size
  header[5] = (lynxBlockSize >> 8) & 0xff;
  // AUDIN used
  header[59] = lynxUseAudin;
  // TODO detect EEPROM?
  // header[60] = lynxUseEeprom;
  myFile.write(header, LYNX_HEADER_SIZE);
}

static void readROM_LYNX() {
  dataDir_LYNX(INPUT);

  // The upper part of the address is used as a block address
  // There are always 256 blocks, but the size of the block can vary
  // So outer loop always steps through block addresses
  const uint32_t upto = LYNX_BLOCKCOUNT * LYNX_BLOCKADDR;
  for (uint32_t blockAddr = 0; blockAddr < upto; blockAddr += LYNX_BLOCKADDR) {
    draw_progressbar(blockAddr, upto);
    blinkLED();

    for (uint32_t i = 0; i < lynxBlockSize; i++) {
      uint32_t addr = blockAddr + i;
      if (lynxUseAudin && i >= lynxBlockSize / 2) {
        addr += LYNX_AUDIN_ON - lynxBlockSize / 2;
      }
      uint8_t byte = readByte_LYNX(addr);
      sdBuffer[i % 512] = byte;
      if ((i + 1) % 512 == 0) {
        myFile.write(sdBuffer, 512);
      }
    }
  }
  draw_progressbar(upto, upto);
}

#pragma region FLASH

#ifdef ENABLE_FLASH

static void writeByte_LYNX(uint32_t addr, uint8_t data) {
  digitalWrite(LYNX_OE, HIGH);
  digitalWrite(LYNX_WE, HIGH);
  setAddr_LYNX(addr);
  PORTC = data;
  digitalWrite(LYNX_WE, LOW);
  delayMicroseconds(20);
  digitalWrite(LYNX_WE, HIGH);
}

// Implements data complement status checking
// We only look at D7, or the highest bit of expected
void waitForDataComplement_LYNX(uint8_t expected) {
  dataDir_LYNX(INPUT);
  uint8_t status;
  do {
    digitalWrite(LYNX_OE, LOW);
    // one nop = 62.5ns
    // tOE = 30-50ns depending on flash
    NOP;
    status = PINC;
    digitalWrite(LYNX_OE, HIGH);
    // test highest bit
  } while ((status ^ expected) >> 7);

  dataDir_LYNX(OUTPUT);
}

static bool readHeader_LYNX() {
  uint32_t romSize = fileSize;

  print_Msg(F("Checking ROM..."));
  display_Update();

  char header[LYNX_HEADER_SIZE];
  myFile.read(header, LYNX_HEADER_SIZE);

  // Check for header to start with LYNX, assume valid .LNX header
  if (strncmp(header, LYNX, 4) == 0) {
    // Pull values from header
    lynxBlockSize = (header[5] << 8) | header[4];
    lynxUseAudin = header[59];
    romSize = fileSize - LYNX_HEADER_SIZE;
    println_Msg(FS(FSTRING_EMPTY));
  } else {
    // Header not valid, assume unheadered, rewind so we don't skip valid data
    println_Msg(F("[NO HEADER]"));
    myFile.seek(0);

    // Get block size from file size
    lynxBlockSize = fileSize / LYNX_BLOCKCOUNT;
    lynxUseAudin = lynxBlockSize >= 2048;
    romSize = fileSize;
  }

  print_Msg(F("AUDIN="));
  print_Msg(lynxUseAudin);
  print_Msg(F(" BLOCK="));
  println_Msg(lynxBlockSize);

  print_Msg(FS(FSTRING_ROM_SIZE));
  print_Msg(romSize / 1024);
  println_Msg(F("KB"));

  // Ensure valid block size, file size, and file fits in flash
  uint32_t expectedSize = (uint32_t)LYNX_BLOCKCOUNT * lynxBlockSize;
  if (lynxBlockSize % 256 != 0 || lynxBlockSize > LYNX_BLOCKADDR || lynxBlockSize <= 0) {
    println_Msg(FS(FSTRING_EMPTY));
    print_STR(error_STR, false);
    println_Msg(F("Invalid block size"));
    return waitPressButton_LYNX();
  }
  if (romSize != expectedSize) {
    println_Msg(FS(FSTRING_EMPTY));
    print_STR(error_STR, false);
    println_Msg(F("Invalid file size"));
    return waitPressButton_LYNX();
  }
  if (expectedSize > flashSize) {
    println_Msg(FS(FSTRING_EMPTY));
    print_STR(error_STR, false);
    print_STR(file_too_big_STR, true);
    return waitPressButton_LYNX();
  }

  println_Msg(FS(FSTRING_OK));
  display_Update();
  return true;
}

static bool detectFlash_LYNX() {
  print_Msg(F("Detecting flash..."));

  // SOFTWARE ID PROGRAM
  dataDir_LYNX(OUTPUT);
  writeByte_LYNX(0x5555, 0xAA);
  writeByte_LYNX(0x2AAA, 0x55);
  writeByte_LYNX(0x5555, 0x90);
  dataDir_LYNX(INPUT);
  // tIDA = 150ns
  NOP;
  NOP;
  NOP;
  // MFG,DEVICE
  uint16_t deviceId = (readByte_LYNX(0x0) << 8) | readByte_LYNX(0x1);

  // EXIT SOFTWARE ID PROGRAM
  dataDir_LYNX(OUTPUT);
  writeByte_LYNX(0x5555, 0xAA);
  writeByte_LYNX(0x2AAA, 0x55);
  writeByte_LYNX(0x5555, 0xF0);

  flashSize = 0;
  switch (deviceId) {
    case 0xBFB5:
      // SST39SF010 = 1Mbit
      flashSize = 131072UL;
      break;
    case 0xBFB6:
      // SST39SF020 = 2Mbit
      flashSize = 262144UL;
      break;
    case 0xBFB7:
      // SST39SF040 = 4Mbit
      flashSize = 524288UL;
      break;
      // case 0xC2A4:
      //   // MX29F040 = 4Mbit
      //   flashSize = 524288UL;
      //   break;
      // case 0xC2D5:
      //   // MX29F080 = 8Mbit
      //   flashSize = 1048576UL;
      //   break;
  }

  if (flashSize <= 0) {
    println_Msg(FS(FSTRING_EMPTY));
    print_STR(error_STR, false);
    println_Msg(F("Not recognized"));
    return waitPressButton_LYNX();
  }

  print_Msg(flashSize / 1024);
  println_Msg(F("KB"));
  display_Update();
  return true;
}

static void eraseFlash_LYNX() {
  print_Msg(F("Erasing..."));
  display_Update();

  // CHIP ERASE PROGRAM
  dataDir_LYNX(OUTPUT);
  writeByte_LYNX(0x5555, 0xAA);
  writeByte_LYNX(0x2AAA, 0x55);
  writeByte_LYNX(0x5555, 0x80);
  writeByte_LYNX(0x5555, 0xAA);
  writeByte_LYNX(0x2AAA, 0x55);
  writeByte_LYNX(0x5555, 0x10);
  waitForDataComplement_LYNX(0xFF);

  println_Msg(FS(FSTRING_OK));
  display_Update();
}

static void writeROM_LYNX() {
  filePath[0] = '\0';
  sd.chdir("/");
  fileBrowser(FS(FSTRING_SELECT_FILE));
  display_Clear();

  // HACK: openFlashFile() checks fileSize against flashSize for you, but this disregards header size:
  // if size(rom) == size(flash) but size(rom + header) > size(flash) we'd get a false negative
  // Pretend we have infinite flash size, allow openFlashFile() to pass this test, then do our own test in readHeader_LYNX()
  flashSize = ULONG_MAX;

  if (!openFlashFile()) return;

  if (!detectFlash_LYNX()) return;

  if (!readHeader_LYNX()) return;

  // Pause to read debug info
  // wait();
  // or alternately auto-advance
  delay(2000);
  display_Clear();

  eraseFlash_LYNX();

  print_STR(flashing_file_STR, true);
  display_Update();

  dataDir_LYNX(OUTPUT);
  uint8_t block[lynxBlockSize];
  const uint32_t upto = LYNX_BLOCKCOUNT * LYNX_BLOCKADDR;
  for (uint32_t blockAddr = 0; blockAddr < upto; blockAddr += LYNX_BLOCKADDR) {
    draw_progressbar(blockAddr, upto);
    blinkLED();

    myFile.read(block, lynxBlockSize);
    for (uint32_t i = 0; i < lynxBlockSize; i++) {
      uint32_t addr = blockAddr + i;
      if (lynxUseAudin && i >= lynxBlockSize / 2) {
        addr += LYNX_AUDIN_ON - lynxBlockSize / 2;
      }

      // BYTE PROGRAM
      uint8_t b = block[i];
      writeByte_LYNX(0x5555, 0xAA);
      writeByte_LYNX(0x2AAA, 0x55);
      writeByte_LYNX(0x5555, 0xA0);
      writeByte_LYNX(addr, b);

      waitForDataComplement_LYNX(b);
    }
  }
  draw_progressbar(upto, upto);

  myFile.close();
  dataDir_LYNX(INPUT);
  print_STR(done_STR, true);
  waitPressButton_LYNX();
}

#endif

#pragma region MENU

static const char PROGMEM LYNX_MENU_FLASH[] = "Program Flashcart";
static const char* const menuOptionsLYNX[] PROGMEM = { FSTRING_READ_ROM, LYNX_MENU_FLASH, FSTRING_RESET };

void lynxMenu() {
  size_t menuCount = sizeof(menuOptionsLYNX) / sizeof(menuOptionsLYNX[0]);
  convertPgm(menuOptionsLYNX, menuCount);
  uint8_t mainMenu = question_box(F("LYNX MENU"), menuOptions, menuCount, 0);
  display_Clear();
  display_Update();

  switch (mainMenu) {
    case 0:
      sd.chdir("/");
      createFolderAndOpenFile(LYNX, "ROM", romName, "lnx");
      detectCart_LYNX();
      writeHeader_LYNX();
      readROM_LYNX();
      myFile.close();
      sd.chdir("/");
      compareCRC("lynx.txt", 0, true, LYNX_HEADER_SIZE);
      print_STR(done_STR, true);
      waitPressButton_LYNX();
      break;

#ifdef ENABLE_FLASH
    case 1:
      writeROM_LYNX();
      break;
#endif

    case 2:
      resetArduino();
      break;

    default:
      print_MissingModule();
      break;
  }
}

#endif