Advertisement
Guest User

Untitled

a guest
Apr 27th, 2019
4,341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <EEPROM.h>
  2.  
  3. // C64 Kernel Switcher and Restore Key Reset/Selector
  4. // version 1.1 -- 26-March-2019
  5. // By Adrian Black
  6.  
  7. // restore key mod: https://www.breadbox64.com/blog/c64-restore-mod/
  8.  
  9.  
  10. const int PowerLED = 4;      // Output Power LED
  11. const int PowerLEDAlt = 13;  // Output Power LED (onboard LED)
  12. const int A13 = 5;           // Output EPROM Address Line 13
  13. const int A14 = 6;           // Output EPROM Address Line 14
  14. const int ResetLine = 7;     // Output to /RESET line
  15. const int EXROMLine = 3;     // Output the /EXROM line
  16. const int RestoreKey = 8;    // Input Restore key
  17.  
  18. int RestoreDelay = 2000;  // 2000ms delay for restore key
  19. const int FlashSpeed = 75; // LED Flash delay
  20.  
  21. const unsigned long repeatdelay = 500; // used for debouncing
  22.  
  23. int CurrentROM; // which rom is select (0-3)
  24. int debouncecounter = 0;       // how many times we have seen new value (for debounce)
  25. int debouncereading;
  26. int debounce_count;
  27. int RestoreHeld;
  28. unsigned long TimeHeld; // amount of time Resotre is held down
  29.  
  30. int buttonDuration = 0; // for keeping track of how long restore is held down
  31. boolean buttonHeld = 0; // for keeping track when you are holding down
  32. boolean Released = 0; // Keeping track when the restore key is released
  33. boolean holdingRestore = 0; // Keeping track if you are holding restore
  34. boolean resetSystem = 0; // keep track wheter to reset
  35.  
  36. int buttonInput; // used to return if restore is held
  37. unsigned long time; //used to keep track of millis output
  38. unsigned long htime; //used to keep track of millis output
  39. unsigned long btime; //used to keep track of bounce millis output
  40.  
  41. void setup() {
  42.   pinMode(PowerLED, OUTPUT);
  43.   pinMode(PowerLEDAlt, OUTPUT);
  44.   pinMode(A13, OUTPUT);
  45.   pinMode(A14, OUTPUT);
  46.   pinMode(ResetLine, INPUT);
  47.   pinMode(EXROMLine, INPUT);
  48.   pinMode(RestoreKey, INPUT);
  49.  
  50.   digitalWrite(PowerLED, LED); // turn on the power LED
  51.  
  52.   digitalWrite(ResetLine, LOW); // keep the system reset
  53.   pinMode(ResetLine, OUTPUT); // switch reset line to OUTPUT so it can hold it low
  54.   digitalWrite(ResetLine, LOW); // keep the system reset
  55.  
  56.   CurrentROM = EEPROM.read(1);
  57.   SetSlot(CurrentROM);
  58.   delay(200);
  59.   pinMode(ResetLine, INPUT); // set the reset pin back to high impedance which releases the INPUT line
  60.   delay(1000); // wait 1000ms
  61.   FlashLED(CurrentROM);  // flash the power LED to show the current state
  62.  
  63.   // all set!
  64. }
  65.  
  66. void loop() {
  67.   buttonInput = readButton(); delay(500);
  68.   time = millis(); // load the number of milliseconds the arduino has been running into variable time
  69.   if (buttonInput == 1) {
  70.     if (!buttonHeld) {
  71.       htime = time; TimeHeld = 0; buttonHeld = 1; } //restore button is pushed
  72.     else {
  73.       TimeHeld = time - htime; } // button is being held down, keep track of total time held.
  74.   }
  75.   if (buttonInput == 0) {
  76.     if (buttonHeld) {
  77.       Released = 1; buttonHeld = 0; htime = millis(); TimeHeld = 0; //restore button not being held anymore
  78.     }
  79.   }
  80.  
  81.   if (TimeHeld > RestoreDelay && !Released) { // do this when the time the button is held is longer than the delay and the button is released
  82.     htime = millis();
  83.     if (holdingRestore == 0) { FlashLED(CurrentROM); holdingRestore = 1; resetSystem = 1; } // first time this is run, so flash the LED with current slot and reset time held. Set the holding restore variable.
  84.     else {
  85.       if (CurrentROM < 3) { CurrentROM++; SaveSlot(CurrentROM); } // or you've already been holding restore, so increment the current ROM slot otherwise reset it to 0
  86.       else { CurrentROM = 0; SaveSlot(CurrentROM); }
  87.       if (TimeHeld > RestoreDelay) { TimeHeld = 0;}  // reset the time held
  88.       FlashLED(CurrentROM); //flash the LED
  89.     }
  90.   }
  91.  
  92.   if (Released) {
  93.     //if time held greater than restore delay, reset the system, set the current rom slot, reselt the time held and holding restore
  94.     if (resetSystem) { // on do this if the reset system has been set above
  95.       htime = millis();
  96.       resetSystem = 0;
  97.       holdingRestore = 0;
  98.       Released = 0;
  99.       digitalWrite(ResetLine, LOW); // keep the system reset
  100.       digitalWrite(EXROMLine, LOW); // keep the EXROM line low
  101.       pinMode(ResetLine, OUTPUT);
  102.       pinMode(EXROMLine, OUTPUT);
  103.       digitalWrite(ResetLine, LOW); // keep the system reset
  104.       digitalWrite(EXROMLine, LOW); // keep the EXROM line low
  105.       delay(50); // wait 50ms
  106.       SetSlot(CurrentROM); // select the appropriate kernal ROM
  107.       delay(200); // wait 200ms before releasing RESET line
  108.       pinMode(ResetLine, INPUT); // set the reset pin back to high impedance so computer boots
  109.       delay(300); // wait 300ms before releasing EXROM line
  110.       pinMode(EXROMLine, INPUT); // set the reset pin back to high impedance so computer boots
  111.     } else { //otherwise do nothing
  112.       htime = millis(); Released = 0; resetSystem = 0; holdingRestore = 0;
  113.     }
  114.   }
  115. // finished with loop  
  116. }
  117.  
  118. int readButton() {
  119.  if (!digitalRead(RestoreKey) && (millis() - btime >= repeatdelay)) {
  120.   for(int i = 0; i < 10; i++)
  121.     {
  122.       debouncereading = !digitalRead(RestoreKey);
  123.  
  124.       if(!debouncereading && debouncecounter > 0)
  125.       {
  126.         debouncecounter--;
  127.       }
  128.       if(debouncereading)
  129.       {
  130.         debouncecounter++;
  131.       }
  132.       // If the Input has shown the same value for long enough let's switch it
  133.       if(debouncecounter >= debounce_count)
  134.       {
  135.         btime = millis();
  136.         debouncecounter = 0;
  137.         RestoreHeld = 1;
  138.       }
  139.     delay (10); // wait 10ms
  140.     }
  141.    } else {
  142.     RestoreHeld = 0;
  143.    }
  144. return RestoreHeld;
  145. }
  146.  
  147.  
  148. void SaveSlot(int CurrentRomSlot) {
  149.   // Save Current ROM selection (0-3) into EPROM
  150.   EEPROM.write(1,CurrentRomSlot);
  151. }
  152.  
  153. void FlashLED(int flashcount) {
  154.     // Flash the LED to represent which ROM slot is selected
  155.     switch (flashcount) {
  156.     case 0:
  157.       digitalWrite(PowerLED, LOW);
  158.       digitalWrite(PowerLEDAlt, LOW);
  159.       delay(FlashSpeed);
  160.       digitalWrite(PowerLED, HIGH);
  161.       digitalWrite(PowerLEDAlt, HIGH);
  162.       break;
  163.     case 1:
  164.       digitalWrite(PowerLED, LOW);
  165.       digitalWrite(PowerLEDAlt, LOW);
  166.       delay(FlashSpeed);
  167.       digitalWrite(PowerLED, HIGH);
  168.       digitalWrite(PowerLEDAlt, HIGH);
  169.       delay(FlashSpeed);
  170.       digitalWrite(PowerLED, LOW);
  171.       digitalWrite(PowerLEDAlt, LOW);
  172.       delay(FlashSpeed);
  173.       digitalWrite(PowerLED, HIGH);
  174.       digitalWrite(PowerLEDAlt, HIGH);
  175.       break;
  176.     case 2:
  177.       digitalWrite(PowerLED, LOW);
  178.       digitalWrite(PowerLEDAlt, LOW);
  179.       delay(FlashSpeed);
  180.       digitalWrite(PowerLED, HIGH);
  181.       digitalWrite(PowerLEDAlt, HIGH);
  182.       delay(FlashSpeed);
  183.       digitalWrite(PowerLED, LOW);
  184.       digitalWrite(PowerLEDAlt, LOW);
  185.       delay(FlashSpeed);
  186.       digitalWrite(PowerLED, HIGH);
  187.       digitalWrite(PowerLEDAlt, HIGH);
  188.       delay(FlashSpeed);
  189.       digitalWrite(PowerLED, LOW);
  190.       digitalWrite(PowerLEDAlt, LOW);
  191.       delay(FlashSpeed);
  192.       digitalWrite(PowerLED, HIGH);
  193.       digitalWrite(PowerLEDAlt, HIGH);
  194.       break;
  195.     case 3:
  196.       digitalWrite(PowerLED, LOW);
  197.       digitalWrite(PowerLEDAlt, LOW);
  198.       delay(FlashSpeed);
  199.       digitalWrite(PowerLED, HIGH);
  200.       digitalWrite(PowerLEDAlt, HIGH);
  201.       delay(FlashSpeed);
  202.       digitalWrite(PowerLED, LOW);
  203.       digitalWrite(PowerLEDAlt, LOW);
  204.       delay(FlashSpeed);
  205.       digitalWrite(PowerLED, HIGH);
  206.       digitalWrite(PowerLEDAlt, HIGH);
  207.       delay(FlashSpeed);
  208.       digitalWrite(PowerLED, LOW);
  209.       digitalWrite(PowerLEDAlt, LOW);
  210.       delay(FlashSpeed);
  211.       digitalWrite(PowerLED, HIGH);
  212.       digitalWrite(PowerLEDAlt, HIGH);
  213.       delay(FlashSpeed);
  214.       digitalWrite(PowerLED, LOW);
  215.       digitalWrite(PowerLEDAlt, LOW);
  216.       delay(FlashSpeed);
  217.       digitalWrite(PowerLED, HIGH);
  218.       digitalWrite(PowerLEDAlt, HIGH);
  219.       break;
  220.     default:
  221.       digitalWrite(PowerLED, LOW);
  222.       digitalWrite(PowerLEDAlt, LOW);
  223.       delay(FlashSpeed);
  224.       digitalWrite(PowerLED, HIGH);
  225.       digitalWrite(PowerLEDAlt, HIGH);
  226.       break;
  227.   }
  228. }
  229.  
  230. void SetSlot(int DesiredRomSlot) {
  231.     // Select the actual ROM slot being used
  232.     switch (DesiredRomSlot) {
  233.     case 0:
  234.       digitalWrite(A13, LOW);
  235.       digitalWrite(A14, LOW);
  236.       break;
  237.     case 1:
  238.       digitalWrite(A13, HIGH);
  239.       digitalWrite(A14, LOW);
  240.       break;
  241.     case 2:
  242.       digitalWrite(A13, LOW);
  243.       digitalWrite(A14, HIGH);
  244.       break;
  245.     case 3:
  246.       digitalWrite(A13, HIGH);
  247.       digitalWrite(A14, HIGH);
  248.       break;
  249.     default:
  250.       digitalWrite(A13, LOW);
  251.       digitalWrite(A14, LOW);
  252.       break;
  253.   }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement