-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino_c128_u36_switch.txt
460 lines (435 loc) · 14.8 KB
/
arduino_c128_u36_switch.txt
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
#include <EEPROM.h>
// C128 8 in 1 27c020 u36 rom switch
// Based on C64 Kernel Switcher and Restore Key Reset/Selector
// version 1.1 -- 26-March-2019 By Adrian Black
// restore key mod: https://www.breadbox64.com/blog/c64-restore-mod/
// modded by insanity 2-April-2021 to add Basic ROM switch needed for OpenROMS Kernal compatibility
// added additional reset to work around RAM bug during EXROM reset in OpenROMS and Speeddos Kernals
// started c128-u36 conversion 19-Sept-2021
const int PowerLED = 4; // Output Power LED
const int PowerLEDAlt = 13; // Output Power LED (onboard LED)
const int A15 = 5; // Output EPROM Address Line 15
const int A16 = 6; // Output EPROM Address Line 16
const int A17 = 7; // Output EPROM Address Line 17
const int ResetLine = 8; // Output to /RESET line
const int EXROMLine = 3; // Output the /EXROM line
const int RestoreKey = 9; // Input Restore key
int RestoreDelay = 2000; // 2000ms delay for restore key
const int FlashSpeed = 75; // LED Flash delay
const unsigned long repeatdelay = 500; // used for debouncing
int CurrentROM; // which rom is select (0-7)
int debouncecounter = 0; // how many times we have seen new value (for debounce)
int debouncereading;
int debounce_count;
int RestoreHeld;
unsigned long TimeHeld; // amount of time Resotre is held down
int buttonDuration = 0; // for keeping track of how long restore is held down
boolean buttonHeld = 0; // for keeping track when you are holding down
boolean Released = 0; // Keeping track when the restore key is released
boolean holdingRestore = 0; // Keeping track if you are holding restore
boolean resetSystem = 0; // keep track wheter to reset
int buttonInput; // used to return if restore is held
unsigned long time; //used to keep track of millis output
unsigned long htime; //used to keep track of millis output
unsigned long btime; //used to keep track of bounce millis output
void setup() {
pinMode(PowerLED, OUTPUT);
pinMode(PowerLEDAlt, OUTPUT);
pinMode(A15, OUTPUT);
pinMode(A16, OUTPUT);
pinMode(A17, OUTPUT);
pinMode(ResetLine, INPUT);
pinMode(EXROMLine, INPUT);
pinMode(RestoreKey, INPUT);
digitalWrite(PowerLED, PowerLED); // turn on the power LED
digitalWrite(ResetLine, LOW); // keep the system reset
pinMode(ResetLine, OUTPUT); // switch reset line to OUTPUT so it can hold it low
digitalWrite(ResetLine, LOW); // keep the system reset
CurrentROM = EEPROM.read(1);
SetSlot(CurrentROM);
delay(200);
pinMode(ResetLine, INPUT); // set the reset pin back to high impedance which releases the INPUT line
delay(1000); // wait 1000ms
FlashLED(CurrentROM); // flash the power LED to show the current state
// all set!
}
void loop() {
buttonInput = readButton(); delay(500);
time = millis(); // load the number of milliseconds the arduino has been running into variable time
if (buttonInput == 1) {
if (!buttonHeld) {
htime = time; TimeHeld = 0; buttonHeld = 1; } //restore button is pushed
else {
TimeHeld = time - htime; } // button is being held down, keep track of total time held.
}
if (buttonInput == 0) {
if (buttonHeld) {
Released = 1; buttonHeld = 0; htime = millis(); TimeHeld = 0; //restore button not being held anymore
}
}
if (TimeHeld > RestoreDelay && !Released) { // do this when the time the button is held is longer than the delay and the button is released
htime = millis();
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.
else {
if (CurrentROM < 7) { CurrentROM++; SaveSlot(CurrentROM); } // or you've already been holding restore, so increment the current ROM slot otherwise reset it to 0
else { CurrentROM = 0; SaveSlot(CurrentROM); }
if (TimeHeld > RestoreDelay) { TimeHeld = 0;} // reset the time held
FlashLED(CurrentROM); //flash the LED
}
}
if (Released) {
//if time held greater than restore delay, reset the system, set the current rom slot, reselt the time held and holding restore
if (resetSystem) { // on do this if the reset system has been set above
htime = millis();
resetSystem = 0;
holdingRestore = 0;
Released = 0;
digitalWrite(ResetLine, LOW); // keep the system reset
digitalWrite(EXROMLine, LOW); // keep the EXROM line low
pinMode(ResetLine, OUTPUT);
pinMode(EXROMLine, OUTPUT);
digitalWrite(ResetLine, LOW); // keep the system reset
digitalWrite(EXROMLine, LOW); // keep the EXROM line low
delay(50); // wait 50ms
SetSlot(CurrentROM); // select the appropriate kernal ROM
delay(200); // wait 200ms before releasing RESET line
pinMode(ResetLine, INPUT); // set the reset pin back to high impedance so computer boots
delay(300); // wait 300ms before releasing EXROM line
pinMode(EXROMLine, INPUT); // set the reset pin back to high impedance so computer boots
// additional reset added to work around RAM bug when running exrom reset on openroms or speeddos kernal
digitalWrite(ResetLine, LOW); // keep the system reset
pinMode(ResetLine, OUTPUT);
digitalWrite(ResetLine, LOW); // keep the system reset
delay(200); // wait 200ms before releasing RESET line
pinMode(ResetLine, INPUT); // set the reset pin back to high impedance so computer boots
} else { //otherwise do nothing
htime = millis(); Released = 0; resetSystem = 0; holdingRestore = 0;
}
}
// finished with loop
}
int readButton() {
if (!digitalRead(RestoreKey) && (millis() - btime >= repeatdelay)) {
for(int i = 0; i < 10; i++)
{
debouncereading = !digitalRead(RestoreKey);
if(!debouncereading && debouncecounter > 0)
{
debouncecounter--;
}
if(debouncereading)
{
debouncecounter++;
}
// If the Input has shown the same value for long enough let's switch it
if(debouncecounter >= debounce_count)
{
btime = millis();
debouncecounter = 0;
RestoreHeld = 1;
}
delay (10); // wait 10ms
}
} else {
RestoreHeld = 0;
}
return RestoreHeld;
}
void SaveSlot(int CurrentRomSlot) {
// Save Current ROM selection (0-7) into EPROM
EEPROM.write(1,CurrentRomSlot);
}
void FlashLED(int flashcount) {
// Flash the LED to represent which ROM slot is selected
switch (flashcount) {
case 0:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 1:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 2:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 3:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 4:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 5:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 6:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
case 7:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
delay(FlashSpeed);
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
default:
digitalWrite(PowerLED, LOW);
digitalWrite(PowerLEDAlt, LOW);
delay(FlashSpeed);
digitalWrite(PowerLED, HIGH);
digitalWrite(PowerLEDAlt, HIGH);
break;
}
}
void SetSlot(int DesiredRomSlot) {
// Select the actual ROM slot being used
switch (DesiredRomSlot) {
// Servant ROM 4.8.4
case 0:
digitalWrite(A15, LOW);
digitalWrite(A16, LOW);
digitalWrite(A17, LOW);
break;
// GEOS 1581
case 1:
digitalWrite(A15, HIGH);
digitalWrite(A16, LOW);
digitalWrite(A17, LOW);
break;
// BASIC 8
case 2:
digitalWrite(A15, LOW);
digitalWrite(A16, HIGH);
digitalWrite(A17, LOW);
break;
// KeyDOS
case 3:
digitalWrite(A15, HIGH);
digitalWrite(A16, HIGH);
digitalWrite(A17, LOW);
break;
// SuperChip A
case 4:
digitalWrite(A15, LOW);
digitalWrite(A16, LOW);
digitalWrite(A17, HIGH);
break;
// SuperChip B
case 5:
digitalWrite(A15, HIGH);
digitalWrite(A16, LOW);
digitalWrite(A17, HIGH);
break;
// Turbo Assembler
case 6:
digitalWrite(A15, LOW);
digitalWrite(A16, HIGH);
digitalWrite(A17, HIGH);
break;
// Diagnostics
case 7:
digitalWrite(A15, HIGH);
digitalWrite(A16, HIGH);
digitalWrite(A17, HIGH);
break;
default:
digitalWrite(A15, LOW);
digitalWrite(A16, LOW);
digitalWrite(A17, LOW);
break;
}
}