   digitalWrite(LATCH, LOW);
 }
 
-bool getRomName() {
-  File config = SD.open("config.txt");
-  if (config) {
-    for (byte i = 0; config.peek() != '\n' && i <= FILENAME_SIZE; ++i) {
-      romName[i] = config.read();
-    }
-    config.close();
-    return true;
-  } else {
-    // File config.txt was not found or could not be opened.
-    return false;
-  }
+byte getKey(int pin) {
+  int v = analogRead(pin);
+  delay(50);
+  v = analogRead(pin);
+  if (v < 600 && v > 300)
+    return DOWNKEY;
+  if (v < 300 && v > 100)
+    return SELECTKEY;
+  if (v < 100)
+    return UPKEY;
+  return NOKEY;
 }

#include <SD.h>   // Both includes are needed
#include <SPI.h>  // for the Arduino SD library.

// Defines for Arduino port numbers
#define CLOCK          5     // SRCLK (A/D-Bus Shift Registers)
#define LATCH          3     // RCLK  (A/D-Bus Shift Registers)
#define DATA           6     // SER   (A/D-Bus Shift Registers)
#define OESHIFT        2     // _G    (A/D-Bus Shift Registers)
#define WESRAM         10
#define OEVCBUS        7     // OE for Bus Transceivers
#define SD_CS          4     // Chip Select for SD card
#define LED            13

// Miscellaneous other defines
#define FILENAME_SIZE  12    // 8.3 convention, see SD library doc.
#define MAX_ROM_SIZE   6144

// Global variables
char romName[FILENAME_SIZE + 1];
File romFile;
unsigned blinkTimeHigh = 500;
unsigned blinkTimeLow = 500;

void setup() {
  pinMode(SD_CS, OUTPUT);

  pinMode(OEVCBUS, OUTPUT);
  pinMode(LED, OUTPUT);

  // Isolate MultiROM from console bus
  disableVCBus();

  // Enable 7-segment display
  display.setBrightness(0x0f);

  // Wait for user selecting a ROM number
  byte key = getKey(A7);
  do {
    if (key == UPKEY && lastkey != UPKEY && romNumber < MAX_ROMS)
      ++romNumber;
    else if (key == DOWNKEY && lastkey != DOWNKEY && romNumber > 1)
      --romNumber;
    display.showNumberDec(romNumber, false, 2, 2);
    lastkey = key;
    key = getKey(A7);
  } while (key != SELECTKEY);

  // Construct ROM file name
  itoa(romNumber, romName, 10);
  byte len = strlen(romName);
  romName[len]   = '.';
  romName[len+1] = 'b';
  romName[len+2] = 'i';
  romName[len+3] = 'n';

  // Initialize SD card and open ROM file
  if (SD.begin(SD_CS)) {
    romFile = SD.open(romName);

    // Write ROM file to SRAM
    if (romFile) {
      unsigned romSize = static_cast<unsigned>(romFile.size());
      if (romSize <= MAX_ROM_SIZE) {
        for (unsigned i = 0; i < romSize; i++) {
          write2RAM(i, romFile.read());
        }
      } else {
        // ROM size too big.
        blinkTimeHigh =   50;
        blinkTimeLow  = 1000;
      }
      romFile.close();
      enableVCBus();
    } else {
      // ROM file could not be opened.
      blinkTimeHigh =  50;
      blinkTimeLow  = 200;
    }
  } else {
    // SD card not found or otherwise failing initialization
    blinkTimeHigh = 50;
    blinkTimeLow  = 50;
  }
  SPI.end();  // Otherwise on-board LED does not work!
}
