-
Notifications
You must be signed in to change notification settings - Fork 4
/
Hub-Mega2560.ino
2691 lines (2382 loc) · 80.6 KB
/
Hub-Mega2560.ino
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// System libraries
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <SPI.h>
#include <SD.h>
// Debugging
//#define __DEBUG_JOY__
//#define __DEBUG_JPG__
//#define __DEBUG_MOUSE__
//#define __DEBUG_CMD__
//#define __DEBUG_COM__
//#define __DEBUG_IO__
//#define __DEBUG_PCK__
//#define __DEBUG_TCP__
//#define __DEBUG_UDP__
//#define __DEBUG_URL__
//#define __DEBUG_WEB__
// Firmware Version
char espVersion[5] = "?";
char espUpdate[5] = "?";
char megaVersion[5] = "v0.6";
char megaUpdate[5] = "?";
char urlEsp[] = "http://8bit-unity.com/Hub-ESP8266.bin";
char urlMega[] = "http://8bit-unity.com/Hub-Mega2560.bin";
char urlVer[] = "http://8bit-unity.com/Hub-Version.txt";
// HUB Commands
#define HUB_SYS_ERROR 0
#define HUB_SYS_RESET 1
#define HUB_SYS_NOTIF 2
#define HUB_SYS_SCAN 3
#define HUB_SYS_CONNECT 4
#define HUB_SYS_IP 5
#define HUB_SYS_MOUSE 6
#define HUB_SYS_VERSION 7
#define HUB_SYS_UPDATE 8
#define HUB_SYS_STATE 9 // COM
#define HUB_SYS_RESEND 9 // ESP
#define HUB_DIR_LS 10 // Todo: Implement for root directory /microSD
#define HUB_DIR_MK 11
#define HUB_DIR_RM 12
#define HUB_DIR_CD 13
#define HUB_FILE_OPEN 21
#define HUB_FILE_SEEK 22
#define HUB_FILE_READ 23
#define HUB_FILE_WRITE 24
#define HUB_FILE_CLOSE 25
#define HUB_UDP_OPEN 30
#define HUB_UDP_RECV 31
#define HUB_UDP_SEND 32
#define HUB_UDP_CLOSE 33
#define HUB_UDP_SLOT 34
#define HUB_TCP_OPEN 40
#define HUB_TCP_RECV 41
#define HUB_TCP_SEND 42
#define HUB_TCP_CLOSE 43
#define HUB_TCP_SLOT 44
#define HUB_WEB_OPEN 50
#define HUB_WEB_RECV 51
#define HUB_WEB_HEADER 52
#define HUB_WEB_BODY 53
#define HUB_WEB_SEND 54
#define HUB_WEB_CLOSE 55
#define HUB_URL_GET 60
#define HUB_URL_READ 61
// String definitions
#ifdef __DEBUG_CMD__
const char* cmdString[] =
{"SYS_ERROR","SYS_RESET","SYS_NOTIF", "SYS_SCAN", "SYS_CONNECT","SYS_IP", "SYS_MOUSE","SYS_VERSION","SYS_UPDATE","",
"DIR_LS", "DIR_MK", "DIR_RM", "DIR_CD", "", "", "", "", "", "",
"FILE_OPEN","FILE_SEEK","FILE_READ", "FILE_WRITE","FILE_CLOSE", "", "", "", "", "",
"UDP_OPEN", "UDP_RECV", "UDP_SEND", "UDP_CLOSE", "UDP_SLOT", "", "", "", "", "",
"TCP_OPEN", "TCP_RECV", "TCP_SEND", "TCP_CLOSE", "TCP_SLOT", "", "", "", "", "",
"WEB_OPEN", "WEB_RECV", "WEB_HEADER","WEB_BODY", "WEB_SEND", "WEB_CLOSE","", "", "", "",
"URL_GET", "URL_READ", "", "", "", "", "", "", "", ""};
#endif
const char* blank = " ";
const char* fail = "Update failed! ";
// Useful macros
#define MIN(a,b) (a>b ? b : a)
#define MAX(a,b) (a>b ? a : b)
// HUB Modes
#define MODE_NONE 0
#define MODE_APPLE 1
#define MODE_ATARI 2
#define MODE_BBC 3
#define MODE_C64 4
#define MODE_NES 5
#define MODE_ORIC 6
#define MODE_LYNX 7
#define HUB_MODES 8
const char* modeString[HUB_MODES] = {"Please setup", "Apple //", "Atari 8bit", "BBC Micro", "C64/C128", "NES", "Oric", "Lynx"};
byte hubMode = MODE_NONE;
// COMM Params
#define FILES 8 // Number of file handles
#define SLOTS 8 // Number of connection handles
#define PACKET 256 // Max. packet length (bytes)
#define TIMEOUT 1000 // Packet timout (milliseconds)
// HUB Files
File hubFile[FILES];
// IP Params
char IP[17] = "Not connected...";
// Buffers for data exchange with ESP8266
char serBuffer[PACKET];
unsigned char serLen;
////////////////////////////////
// DEBUGGING functions //
////////////////////////////////
/*void udpDebug() {
// Setup request
unsigned char cmd[] = {1};
while (true) {
writeCMD(HUB_UDP_SLOT);
writeChar(0);
writeCMD(HUB_UDP_OPEN);
writeChar(199); writeChar(47);
writeChar(196); writeChar(106);
writeInt(5000); writeInt(5000);
writeCMD(HUB_UDP_SEND);
writeBuffer(cmd, 1);
// Wait for answer
uint32_t timeout = millis()+3000;
while (millis() < timeout) {
if (Serial3.find("CMD") && readChar() == HUB_UDP_RECV) {
readChar();
if (readBuffer())
Serial.print("Received: ");
Serial.println(serLen);
break;
}
}
if (millis() >= timeout) {
Serial.println("Timeout");
lcd.print("Timeout");
}
writeCMD(HUB_UDP_CLOSE);
delay(500);
}
}*/
////////////////////////////////
// PACKET functions //
////////////////////////////////
// Define packet structure
typedef struct packet {
unsigned char ID;
unsigned char cmd;
unsigned char slot;
unsigned char len;
unsigned char* data;
uint32_t timeout;
struct packet* next;
} packet_t;
packet_t* packetHead = NULL;
unsigned char packetID = 0;
void pushPacket(unsigned char cmd, signed char slot) {
// Create new packet
packet_t* packet = malloc(sizeof(packet_t));
packet->next = NULL;
// Assign ID & Timeout
if (++packetID>254) { packetID = 1; }
packet->ID = packetID;
packet->timeout = millis() + TIMEOUT;
// Copy data to packet
packet->len = serLen;
packet->cmd = cmd;
packet->slot = slot;
packet->data = (unsigned char*)malloc(serLen);
memcpy(packet->data, serBuffer, serLen);
// Append packet at tail of linked list
if (!packetHead) {
packetHead = packet;
} else {
packet_t *packetTail = packetHead;
while (packetTail->next != NULL) {
packetTail = packetTail->next;
}
packetTail->next = packet;
}
#ifdef __DEBUG_PCK__
Serial.print("PUSH: "); Serial.println((byte)packet->ID);
#endif
}
packet_t *getPacket(unsigned char cmd, signed char slot) {
// Find packet with matching cmd/slot
packet_t *packet = packetHead;
while (packet) {
if (packet->cmd == cmd && packet->slot == slot)
return packet;
packet = packet->next;
}
return NULL;
}
void popPacket(unsigned char ID) {
// Pop packet with matching ID
packet_t *prev = NULL, *next = NULL, *packet = packetHead;
while (packet) {
next = packet->next;
if (packet->ID == ID) {
if (prev)
prev->next = next;
else
packetHead = next;
#ifdef __DEBUG_PCK__
Serial.print("POP: "); Serial.println((byte)packet->ID);
#endif
free(packet->data);
free(packet);
return;
}
prev = packet;
packet = next;
}
}
void packetTimeout() {
// Remove packets that have exceeded timeout
while (packetHead && millis() > packetHead->timeout) {
popPacket(packetHead->ID);
}
}
//////////////////////////
// List functions //
//////////////////////////
// Define list structure
typedef struct list {
char* data;
struct list* next;
} list_t;
list_t* listHead;
void pushList(char* data) {
// Create new packet
list_t* elt = malloc(sizeof(list_t));
elt->data = data;
elt->next = NULL;
// Append packet at tail of linked list
if (!listHead) {
listHead = elt;
} else {
list_t *tail = listHead;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = elt;
}
}
unsigned char* getList(unsigned char index) {
byte i=0;
list_t* elt = listHead;
while (elt && i<index) {
elt = elt->next;
i++;
}
if (elt) {
return elt->data;
} else {
return NULL;
}
}
unsigned char lenList() {
byte i=0;
list_t* elt = listHead;
while (elt) {
elt = elt->next;
i++;
}
return i;
}
void clearList() {
list_t* elt;
while (listHead) {
elt = listHead;
listHead = elt->next;
free(elt);
}
}
////////////////////////////////
// PERIPHERAL functions //
////////////////////////////////
// Joystick states
#define JOY_UP 1
#define JOY_DOWN 2
#define JOY_LEFT 4
#define JOY_RIGHT 8
#define JOY_FIRE1 16
#define JOY_FIRE2 32
// Joystick modes
#define JOY_STD 0
#define JOY_PASE 1
// Joystick params
unsigned char joyMode = JOY_STD;
unsigned char joyState[4] = { 255, 255, 255, 255 };
unsigned char joyPins[4][8] = { { 23, 25, 27, 29, 22, 24, 28, 26 }, // U, D, L, R, F1, F2, Alt F2, GND
{ 31, 33, 35, 37, 30, 32, 36, 34 },
{ 39, 41, 43, 45, 38, 40, 44, 42 },
{ 0, 0, 0, 0, 0, 0, 0, 0 } }; // Joy #4 unassigned in current version
unsigned char joyStnd[6] = { 1, 2, 4, 8, 16, 32 };
unsigned char joyPase[6] = { 16, 8, 1, 2, 4, 0 };
// Mouse params
unsigned char mouseState[2] = { 255, 255 };
unsigned char mouseSpeed = 1;
unsigned char mouseFPS = 1;
void setupJOY() {
// Set I/O pins
for (byte i=0; i<3; i++) {
for (byte j=0; j<7; j++) {
pinMode(joyPins[i][j], INPUT_PULLUP); // Inputs
}
pinMode(joyPins[i][7], OUTPUT); // Ground
digitalWrite(joyPins[i][7], LOW);
}
// Be careful with Atari 7800 joypads (Pin 9 causes short-circuit)
for (byte i=0; i<3; i++) {
if (!digitalRead(joyPins[i][6])) joyPins[i][6] = joyPins[i][5];
}
}
void readJOY() {
// Read digital pins
switch (joyMode) {
case JOY_STD:
for (byte i=0; i<3; i++) {
joyState[i] |= 63;
for (byte j=0; j<6; j++) {
if (!digitalRead(joyPins[i][j])) { joyState[i] &= ~joyStnd[j]; }
}
if (!digitalRead(joyPins[i][6])) { joyState[i] &= ~joyStnd[5]; }
}
break;
case JOY_PASE:
for (byte i=0; i<2; i++) {
joyState[i] = 32;
for (byte j=0; j<6; j++) {
if (digitalRead(joyPins[i][j])) { joyState[i] += joyPase[j]; }
}
if (!digitalRead(joyPins[i][6])) { joyState[i] &= ~joyPase[5]; }
}
break;
}
#ifdef __DEBUG_JOY__
Serial.print("Joystick: ");
for (byte i=0; i<4; i++) { Serial.println(joyState[i]); Serial.print(","); }
#endif
}
void readMouse() {
unsigned char s = readChar();
signed char x = readChar();
signed char y = readChar();
signed char w = readChar();
// Adjust inputs
switch (mouseSpeed) {
case 0:
x /= 6; y /= -4;
break;
case 1:
x /= 4; y /= -3;
break;
default:
x /= 2; y /= -2;
break;
}
if (w == -128) w = 0;
// Register inputs
joyState[0] |= 192; joyState[1] |= 192; joyState[2] |= 192;
if (s&1) { joyState[0] &= ~64; } // L Button
if (s&2) { joyState[0] &= ~128; } // R Button
if (s&4) { joyState[1] &= ~64; } // M Button
if (w<0) { joyState[2] &= ~64; } // W Up
if (w>0) { joyState[2] &= ~128; } // W Down
if ((x!=0 || y!=0) && (mouseState[0]==255 || mouseState[1] ==255)) {
mouseState[0] = 80; mouseState[1] = 100;
}
if (x<0) { mouseState[0] -= MIN(mouseState[0], -x); }
if (x>0) { mouseState[0] += MIN(159-mouseState[0], x); }
if (y<0) { mouseState[1] -= MIN(mouseState[1], -y); }
if (y>0) { mouseState[1] += MIN(199-mouseState[1], y); }
#ifdef __DEBUG_MOUSE__
Serial.print("M:"); Serial.print((s&1)>0); // L but
Serial.print(","); Serial.print((s&4)>2); // M but
Serial.print(","); Serial.print((s&2)>1); // R but
Serial.print(","); Serial.print(mouseState[0]);
Serial.print(","); Serial.print(mouseState[1]);
Serial.print(","); Serial.println(w);
#endif
}
////////////////////////////////
// SD functions //
////////////////////////////////
unsigned char sdInserted = 0;
void printDir(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDir(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void setupSD() {
// Try to init SD card
Serial.println("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("Notif: No Micro-SD card inserted");
sdInserted = 0;
return;
}
// Open root folder
File root = SD.open("/");
sdInserted = 1;
// Debugging
//decodeJPG("banner.jpg");
//printDir(root, 1);
}
////////////////////////////////
// LCD functions //
////////////////////////////////
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
void setupLCD() {
// Setup LCD light
lcd.begin(20,4);
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
displayHeader();
}
void displayHeader() {
// Show header
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" < 8bit-Hub >");
}
void displayMode() {
lcd.setCursor(0,1);
lcd.print("Mode: ");
lcd.setCursor(5,1);
lcd.print(modeString[hubMode]);
Serial.print("Hub Mode: ");
Serial.println(modeString[hubMode]);
}
void displaySD() {
lcd.setCursor(0,2);
lcd.print("SD:");
if (sdInserted) {
lcd.print("Inserted");
} else {
lcd.print("Not inserted");
}
}
void displayIP() {
lcd.setCursor(0,3);
lcd.print("IP: ");
lcd.setCursor(3,3);
lcd.print(IP);
}
//////////////////////
// COM Packets //
//////////////////////
#define COM_ERR_OK 0
#define COM_ERR_NODATA 1
#define COM_ERR_HEADER 2
#define COM_ERR_TRUNCAT 3
#define COM_ERR_CORRUPT 4
#define COM_ERR_RESENT 5
// Packet information
volatile unsigned char checksum;
volatile unsigned char comOutID = 0, comOutLen, comOutBuffer[PACKET], *comOutData, comOutInd = 0;
volatile unsigned char comInHeader, comInCMD, comInID, comInBuffer[PACKET], comInLen = 0;
volatile byte comCode = COM_ERR_NODATA;
// Variables used by Atari/C64/NES/Oric interrupts
volatile unsigned char hasHeader, hasCMD, hasLen, rcvLen;
volatile unsigned char inByte = 0, outByte = 0;
volatile unsigned char interruptMode = 1;
volatile unsigned char interruptOffset = 0;
volatile unsigned long interruptTimer = 0;
// Variables used for Debugging I/O
#ifdef __DEBUG_IO__
volatile unsigned int inRec = 0, outRec = 0, inErr = 0, inSta = 0, outSta = 0;
unsigned long timerIO = millis();
#endif
void setupCOM() {
// Setup COM Connection
switch (hubMode) {
case MODE_ATARI:
pinMode(3, INPUT_PULLUP); // STROBE (PC)
pinMode(4, INPUT_PULLUP); // R/W DATA BIT
pinMode(5, INPUT_PULLUP); // R/W STATE
pinMode(6, OUTPUT); // READY (HUB)
PORTH |= _BV(PH3); // Pin 6 HIGH == READY
attachInterrupt(digitalPinToInterrupt(3), atariInterrupt, FALLING);
break;
case MODE_C64:
pinMode(18, INPUT_PULLUP); // STROBE (PC)
pinMode(17, INPUT_PULLUP); // R/W STATE
pinMode(16, OUTPUT); // READY (HUB)
pinMode(6, INPUT_PULLUP); // R/W DATA BIT 1
pinMode(5, INPUT_PULLUP); // R/W DATA BIT 2
pinMode(4, INPUT_PULLUP); // R/W DATA BIT 3
pinMode(3, INPUT_PULLUP); // R/W DATA BIT 4
PORTH &= ~_BV(PH1); // Pin 16 LOW == READY
attachInterrupt(digitalPinToInterrupt(18), c64Interrupt, FALLING);
break;
case MODE_LYNX:
// Setup Serial 2 on pins 16/17
Serial2.begin(62500, SERIAL_8N2); // Lynx comm (Bauds 62500, 41666, 9600)
while (!Serial2) { }
Serial2.setTimeout(30);
Serial2.flush();
Serial2.readString();
break;
case MODE_NES:
pinMode(18, INPUT_PULLUP); // STROBE (PC)
pinMode(16, INPUT_PULLUP); // R/W STATE
pinMode(17, INPUT_PULLUP); // READY (PC)
pinMode(6, OUTPUT); // READY (HUB)
pinMode(7, INPUT_PULLUP); // DATA IN 1
pinMode(4, OUTPUT); // DATA OUT 1
pinMode(5, OUTPUT); // DATA OUT 2
PORTH &= ~_BV(PH3); // Pin 6 HIGH (inverted) == READY
attachInterrupt(digitalPinToInterrupt(18), nesInterrupt, RISING);
break;
case MODE_ORIC:
pinMode(3, INPUT_PULLUP); // STROBE (PC)
pinMode(17, INPUT_PULLUP); // R/W STATE
pinMode(16, INPUT_PULLUP); // READY (PC)
pinMode(2, OUTPUT); // READY (HUB)
pinMode(4, INPUT_PULLUP); // R/W DATA BIT 1
pinMode(5, INPUT_PULLUP); // R/W DATA BIT 2
pinMode(6, INPUT_PULLUP); // R/W DATA BIT 3
pinMode(7, INPUT_PULLUP); // R/W DATA BIT 4
PORTE |= _BV(PE4); // Pin 2 HIGH
attachInterrupt(digitalPinToInterrupt(3), oricInterrupt, FALLING);
break;
}
}
void preparePacket() {
// Prepare data
comOutInd = comOutLen = 0;
comOutBuffer[comOutLen++] = outByte = 170;
if (comInCMD == HUB_SYS_STATE) {
// Package hub state (joy/mouse)
comOutBuffer[comOutLen++] = checksum = comOutID;
comOutBuffer[comOutLen++] = 6;
for (unsigned char i=0; i<4; i++) { comOutBuffer[comOutLen++] = joyState[i]; checksum += joyState[i]; }
for (unsigned char i=0; i<2; i++) { comOutBuffer[comOutLen++] = mouseState[i]; checksum += mouseState[i]; }
} else {
// Do we have packet ready to send?
signed char slot = -1;
if (comInCMD == HUB_UDP_RECV) slot = 0;
if (comInCMD == HUB_TCP_RECV) slot = 0;
packet_t *packet = getPacket(comInCMD, slot);
// Retrieve data (if any)
if (packet) {
comOutBuffer[comOutLen++] = checksum = comOutID = packet->ID;
comOutBuffer[comOutLen++] = packet->len;
for (unsigned char i=0; i<packet->len; i++) { comOutBuffer[comOutLen++] = packet->data[i]; checksum += packet->data[i]; }
} else {
comOutBuffer[comOutLen++] = checksum = comOutID;
comOutBuffer[comOutLen++] = 0;
}
}
comOutBuffer[comOutLen++] = checksum;
}
boolean checkPacket() {
// Compute checksum
checksum = comInCMD;
for (byte i=0; i<comInLen; i++)
checksum += comInBuffer[i];
// Verify checksum
if (comInBuffer[comInLen] != checksum) {
#ifdef __DEBUG_IO__
inErr++;
#endif
comCode = COM_ERR_CORRUPT;
return false;
} else {
comCode = COM_ERR_OK;
return true;
}
}
// Function used by Atari/C64/NES/Oric interrupts
void processByte() {
// Check header
if (!hasHeader) {
if (inByte == 85 || inByte == 170) {
comInHeader = inByte;
hasHeader = 1;
} else {
#ifdef __DEBUG_IO__
inErr++;
#endif
}
return;
}
// Check CMD
if (!hasCMD) {
comInCMD = inByte;
hasCMD = 1;
return;
}
// If this a recv request?
if (comInHeader == 85) {
// Process byte
comInID = inByte;
// Process packets
popPacket(comInID);
preparePacket();
} else {
// Check for length
if (!hasLen) {
comInLen = inByte;
hasLen = 1;
rcvLen = 0;
return;
}
// Add data to buffer
comInBuffer[rcvLen++] = inByte;
// Check if packet was fully received (including extra byte for checksum)
if (rcvLen < comInLen+1) return;
// Check packet and reset state
comOutInd = 0; comOutLen = 1;
if (checkPacket()) {
if (comInLen)
comProcessCMD();
outByte = 85;
} else {
outByte = 0;
}
}
// Reset incoming state
hasHeader = 0;
hasCMD = 0;
hasLen = 0;
}
/////////////////////////////////
// ATARI Communication //
/////////////////////////////////
void atariRead() {
// Setup pin for input
DDRG &= ~_BV(PG5); PORTG |= _BV(PG5);
delayMicroseconds(8);
// Read 1 bit from computer
inByte |= ((PING & _BV(PG5))>0) << (7-interruptOffset++);
}
void atariWrite() {
// Setup pin for output
DDRG |= _BV(PG5);
// Write 1 bit to computer
if (outByte & (0b00000001 << interruptOffset++)) { PORTG |= _BV(PG5); } else { PORTG &= ~_BV(PG5); }
}
void atariInterrupt() {
// Lock ready pin
PORTH &= ~_BV(PH3); // Pin 6 LOW
// Check R/W pin
if (!(PINE & _BV(PE3))) {
atariRead();
interruptMode = 1;
} else {
atariWrite();
interruptMode = 0;
}
// If byte complete?
if (interruptOffset < 8) {
// Update timer
interruptTimer = millis();
} else {
// Reset offset
interruptOffset = 0;
// Process data
if (interruptMode) {
processByte();
inByte = 0;
#ifdef __DEBUG_IO__
inRec++;
#endif
} else {
outByte = comOutBuffer[++comOutInd];
#ifdef __DEBUG_IO__
outRec++;
#endif
}
// Unlock ready pin
PORTH |= _BV(PH3); // Pin 4 HIGH == READY
}
}
////////////////////////////////
// C64 Communication //
////////////////////////////////
void c64Read() {
// Setup pins for input
DDRH &= ~_BV(PH3); PORTH |= _BV(PH3); // pin 6
DDRE &= ~_BV(PE3); PORTE |= _BV(PE3); // pin 5
DDRG &= ~_BV(PG5); PORTG |= _BV(PG5); // pin 4
DDRE &= ~_BV(PE5); PORTE |= _BV(PE5); // pin 3
// Read 4 bits from computer
inByte |= ((PINH & _BV(PH3))>0) << (interruptOffset++);
inByte |= ((PINE & _BV(PE3))>0) << (interruptOffset++);
inByte |= ((PING & _BV(PG5))>0) << (interruptOffset++);
inByte |= ((PINE & _BV(PE5))>0) << (interruptOffset++);
}
void c64Write() {
// Setup pins for output
DDRH |= _BV(PH3); // pin 6
DDRE |= _BV(PE3); // pin 5
DDRG |= _BV(PG5); // pin 4
DDRE |= _BV(PE5); // pin 3
// Write 4 bits to computer
if (outByte & (1 << interruptOffset++)) { PORTH |= _BV(PH3); } else { PORTH &= ~_BV(PH3); }
if (outByte & (1 << interruptOffset++)) { PORTE |= _BV(PE3); } else { PORTE &= ~_BV(PE3); }
if (outByte & (1 << interruptOffset++)) { PORTG |= _BV(PG5); } else { PORTG &= ~_BV(PG5); }
if (outByte & (1 << interruptOffset++)) { PORTE |= _BV(PE5); } else { PORTE &= ~_BV(PE5); }
}
void c64Interrupt() {
// Lock ready pin
PORTH |= _BV(PH1); // HIGH
// Check R/W pin
if (!(PINH & _BV(PH0))) {
c64Read();
interruptMode = 1;
} else {
c64Write();
interruptMode = 0;
}
// If byte complete?
if (interruptOffset < 8) {
// Reset time
interruptTimer = millis();
} else {
// Reset offset
interruptOffset = 0;
// Process data
if (interruptMode) {
processByte();
inByte = 0;
#ifdef __DEBUG_IO__
inRec++;
#endif
} else {
outByte = comOutBuffer[++comOutInd];
#ifdef __DEBUG_IO__
outRec++;
#endif
}
// Unlock ready pin
PORTH &= ~_BV(PH1); // LOW
}
}
////////////////////////////////
// LYNX Communication //
////////////////////////////////
unsigned char lynxBitPeriod = 16; // Bauds: 62500=16 / 41666=24 / 9600=104
unsigned long lynxTimer;
void lynxOutputMode(void) {
// Switch pin to output
PORTD |= B00000100;
DDRD |= B00000100;
lynxTimer = micros();
while (micros()-lynxTimer < lynxBitPeriod);
}
void lynxInputMode(void) {
// Switch pin to input
DDRD &= B11111011;
}
void lynxWrite(char value) {
unsigned char i, parity = 0, mask = 1;
// Start Bit
PORTD &= B11111011;
lynxTimer += lynxBitPeriod; while (micros()-lynxTimer < lynxBitPeriod) ;
// Value Bits
for (i=0; i<8; i++) {
if (value & mask) {
PORTD |= B00000100;
parity++;
} else {
PORTD &= B11111011;
}
mask = mask << 1;
lynxTimer += lynxBitPeriod; while (micros()-lynxTimer < lynxBitPeriod) ;
}
// Parity Bit
if (parity % 2) {
PORTD &= B11111011;
} else {
PORTD |= B00000100;
}
lynxTimer += lynxBitPeriod; while (micros()-lynxTimer < lynxBitPeriod) ;
// Stop Bit
PORTD |= B00000100;
for (i=0; i<6; i++) { // Bauds: 41666,62500 = i<6 / 9600 = i<1
lynxTimer += lynxBitPeriod; while (micros()-lynxTimer < lynxBitPeriod) ;
}
}
void lynxProcessCOM() {
// Have we got data?
if (!Serial2.available()) { comCode = COM_ERR_NODATA; return; }
// Get Header
if (!Serial2.readBytes((unsigned char*)&comInHeader, 1)) { comCode = COM_ERR_HEADER; return; }
#ifdef __DEBUG_IO__
inRec++;
#endif
// Check Header
if (comInHeader != 170 && comInHeader != 85) { comCode = COM_ERR_HEADER; return; }
// Get Command
if (!Serial2.readBytes((unsigned char*)&comInCMD, 1)) { comCode = COM_ERR_TRUNCAT; return; }
#ifdef __DEBUG_IO__
inRec++;
#endif
// Send or Receive?
if (comInHeader == 85) {
// Get RecvID
if (!Serial2.readBytes((unsigned char*)&comInID, 1)) { comCode = COM_ERR_TRUNCAT; return; }
#ifdef __DEBUG_IO__
inRec++;
#endif
// Process packets
popPacket(comInID);
preparePacket();
// Send DATA
//delayMicroseconds(6*lynxBitPeriod); // Bauds: 62500=6* / 41666=8* / 9600=2*
lynxOutputMode();
for (unsigned char i=0; i<comOutLen; i++) {
lynxWrite(comOutBuffer[i]);
#ifdef __DEBUG_IO__
outRec++;
#endif
}
} else {
// Get Length
if (!Serial2.readBytes((unsigned char*)&comInLen, 1)) { comCode = COM_ERR_TRUNCAT; return; }
#ifdef __DEBUG_IO__
inRec++;
#endif
// Get Buffer+Checksum
if (!Serial2.readBytes((unsigned char*)comInBuffer, comInLen+1)) { comCode = COM_ERR_TRUNCAT; return; }
#ifdef __DEBUG_IO__
inRec += comInLen+1;
#endif
// Check Data Integrity
if (checkPacket()) {
// Send ACKNOW and process CMD
lynxOutputMode();
lynxWrite(85);
comProcessCMD();
} else {
// Send NG
lynxOutputMode();
lynxWrite(0);
}
#ifdef __DEBUG_IO__
outRec++;
#endif
}
// Return pin to input and clear data sent to self
lynxInputMode();
while (Serial2.available())
Serial2.read();
}
/////////////////////////////////
// NES Communication //
/////////////////////////////////
void nesRead() {
// Read 1 bit from computer
inByte |= ((PINH & _BV(PH4))>0) << (interruptOffset++); // Pin 7
}
void nesWrite() {
// Write 2 bits to computer
if (outByte & (1 << interruptOffset++)) { PORTG &= ~_BV(PG5); } else { PORTG |= _BV(PG5); }
if (outByte & (1 << interruptOffset++)) { PORTE &= ~_BV(PE3); } else { PORTE |= _BV(PE3); }
}
void nesInterrupt() {
// Check R/W pin
if (!(PINH & _BV(PH0))) return;