forked from stefanberndtsson/c64iec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c64iec.ino
585 lines (506 loc) · 12.6 KB
/
c64iec.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
#include <EtherCard.h>
#include "tftp.h"
#define SRQIN 2
#define ATN 3
#define CLOCK 4
#define DATA 5
#define AVR_EOI A0
#define AVR_DEBUG A1
#define AVR_TIMEOUT A2
#define IEC_COMMAND 0xf0
#define IEC_DEVICE 0x0f
#define IEC_UNTALK 0x50
#define IEC_UNLISTEN 0x30
#define IEC_LISTEN 0x20
#define IEC_TALK 0x40
#define IEC_MODE 0xf0
#define IEC_ADDRESS 0x0f
#define IEC_OPEN 0xf0
#define IEC_CLOSE 0xe0
#define IEC_FASTLOAD 0xd0
#define IEC_DATA 0x60
#define TIMEOUT_COUNT 2700
#define DEVNO 8
#define C64_FILE_LIMIT 16
#define PC64_SIZE 26
typedef unsigned char uchar;
int iec_mode = 0;
uchar iobuf[TFTP_BLKSIZE+1];
uint16_t sent_to_c64 = 0;
byte device_mode = 0;
byte secondary = 0;
// ethernet interface mac address
static byte mymac[] = { 0x42,0x42,0x42,0x01,0x02,0x03 };
byte Ethernet::buffer[500];
static long timer;
static int dhcp_active = 0;
// Max filename length * 3 for extreme case, and 5 extra for extension + null termination
// Absolute maximum name
// Odd characters are coded as %xx, hence the *3.
static char tftp_filename[C64_FILE_LIMIT*3+5];
unsigned char hexarray[17] = "0123456789abcdef";
void create_tftp_filename(uchar *c64name) {
int out_pos = C64_FILE_LIMIT*3+4;
int out_size = out_pos;
int start_pos = C64_FILE_LIMIT;
int trailing = 1;
if(strlen((const char *)c64name) < start_pos) start_pos = strlen((const char *)c64name);
int i;
for(i=start_pos-1;i>=0;--i) {
if(c64name[i] == 0xa0 || c64name[i] == 0x00) continue;
if(c64name[i] == ' ' && trailing) continue;
trailing = 0;
if((c64name[i] >= '0' && c64name[i] <= '9') ||
(c64name[i] >= 'A' && c64name[i] <= 'Z') ||
(c64name[i] == ' ' || c64name[i] == '-' ||
c64name[i] == '_' || c64name[i] == '.')) {
tftp_filename[--out_pos] = tolower(c64name[i]);
} else {
out_pos -= 3;
tftp_filename[out_pos] = '%';
tftp_filename[out_pos+1] = hexarray[(c64name[i]>>4)&0xf];
tftp_filename[out_pos+2] = hexarray[c64name[i]&0xf];
}
}
memmove(tftp_filename, &tftp_filename[out_pos], out_size-out_pos);
tftp_filename[out_size-out_pos] = '.';
tftp_filename[out_size-out_pos+1] = 'p';
tftp_filename[out_size-out_pos+2] = '0';
tftp_filename[out_size-out_pos+3] = '0';
tftp_filename[out_size-out_pos+4] = '\0';
}
void pc64_make_header(byte *iobuf) {
int len;
len = strlen((const char *)iobuf);
if(len <= C64_FILE_LIMIT) {
for(int i=0;i<(C64_FILE_LIMIT-len);i++) {
iobuf[len+i] = '\0';
}
}
memmove(iobuf+8, iobuf, C64_FILE_LIMIT);
strcpy((char *)iobuf, "C64File");
iobuf[24] = '\0';
iobuf[25] = '\0'; /* REL Record size */
}
void ethernet_probe_for_packet() {
int plen;
plen = ether.packetReceive();
if(!tftp_recv_packet(plen))
ether.packetLoop(plen);
/* Check if this packet is interesting, return if not, or if plen == 0 */
}
static inline void clear_iobuf() {
for(int i=0;i<sizeof(iobuf);i++) {
iobuf[i] = '\0';
}
}
static inline int atn_active() {
if(digitalRead(ATN) == LOW) return 1;
return 0;
}
static inline int data_active() {
if(digitalRead(DATA) == LOW) return 1;
return 0;
}
static inline int clock_active() {
if(digitalRead(CLOCK) == LOW) return 1;
return 0;
}
static inline void pull_data() {
pinMode(DATA, OUTPUT);
digitalWrite(DATA, LOW);
}
static inline void push_data() {
pinMode(DATA, OUTPUT);
digitalWrite(DATA, HIGH);
}
static inline void release_data() {
pinMode(DATA, INPUT);
}
static inline void pull_clock() {
pinMode(CLOCK, OUTPUT);
digitalWrite(CLOCK, LOW);
}
static inline void push_clock() {
pinMode(CLOCK, OUTPUT);
digitalWrite(CLOCK, HIGH);
}
static inline void release_clock() {
pinMode(CLOCK, INPUT);
}
static inline int wait_atn(int state) {
pinMode(ATN, INPUT);
int count = 0;
while(digitalRead(ATN) != state) {
count++;
if(count > TIMEOUT_COUNT) return 0;
}
return 1;
}
static inline int wait_clock(int state) {
pinMode(CLOCK, INPUT);
int count = 0;
digitalWrite(AVR_DEBUG, HIGH);
while(digitalRead(CLOCK) != state) {
count++;
if(count > TIMEOUT_COUNT) {
digitalWrite(AVR_DEBUG, LOW);
digitalWrite(AVR_TIMEOUT, HIGH);
return 0;
}
}
return 1;
}
static inline int wait_clock_eoi(int state) {
pinMode(CLOCK, INPUT);
int count = 0;
digitalWrite(AVR_DEBUG, HIGH);
while(digitalRead(CLOCK) != state) {
count++;
if(count > TIMEOUT_COUNT/100) {
digitalWrite(AVR_DEBUG, LOW);
digitalWrite(AVR_TIMEOUT, HIGH);
return 0;
}
}
return 1;
}
static inline int wait_data(int state) {
pinMode(DATA, INPUT);
int count = 0;
while(digitalRead(DATA) != state) {
count++;
if(count > TIMEOUT_COUNT) {
digitalWrite(AVR_TIMEOUT, HIGH);
return 0;
}
}
return 1;
}
int get_byte(uchar *output, int force_eoi) {
int eoi = 0;
int time_first;
int time_second;
if(!wait_clock(HIGH)) return 0;
release_data();
if(!wait_data(HIGH)) return 0;
digitalWrite(AVR_EOI, LOW);
digitalWrite(AVR_EOI, HIGH);
digitalWrite(AVR_EOI, LOW);
if(!force_eoi) {
if(!wait_clock_eoi(LOW)) {
digitalWrite(AVR_EOI, HIGH);
eoi = 1;
push_data();
delayMicroseconds(80);
pull_data();
delayMicroseconds(80);
release_data();
}
} else {
eoi = 1;
}
digitalWrite(AVR_TIMEOUT, LOW);
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output = digitalRead(DATA);
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 1;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 2;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 3;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 4;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 5;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 6;
if(!wait_clock(LOW)) return 0;
if(!wait_clock(HIGH)) return 0;
*output |= digitalRead(DATA) << 7;
if(!wait_clock(LOW)) return 0;
pull_data();
if(eoi) {
release_clock();
pull_data();
}
delayMicroseconds(100);
return eoi;
}
int put_byte(uchar value, int eoi) {
int tmp;
int flag;
delayMicroseconds(100);
tmp = data_active();
push_clock();
if(tmp) {
if(!wait_data(HIGH)) return 0;
flag = eoi;
} else {
flag = 1;
}
if(flag) {
if(!wait_data(HIGH)) return 0;
if(!wait_data(LOW)) return 0;
}
pull_clock();
if(!wait_data(HIGH)) return 0;
for(int i = 0; i<8; i++) {
delayMicroseconds(70);
if(data_active()) return 1;
if(value & (1<<i)) {
push_data();
} else {
pull_data();
}
push_clock();
delayMicroseconds(70);
pull_clock();
push_data();
}
delayMicroseconds(100);
if(!wait_data(LOW)) return 0;
return flag;
}
void handle_close(uchar sec_addr) {
// Nothing really to do here...
}
void handle_open(uchar sec_addr) {
int a = 0;
int eoi;
do {
if(atn_active()) return;
eoi = get_byte(&iobuf[a], 0);
a++;
} while(!eoi && a < sizeof(iobuf)-1);
iobuf[a] = '\0';
}
int send_file() {
int eoi = 0;
int at_start = 1;
int iobuf_offset = 0;
int unsent_data = 0;
int sent_this_time = 0;
sent_to_c64 = 0;
tftp_get_file(tftp_filename);
while(!eoi && tftp_request_in_progress == TFTP_GET) {
/* Fetch TFTP block repeatedly until we get data or last block. */
while(unsent_data == 0 && tftp_request_in_progress == TFTP_GET) {
ethernet_probe_for_packet();
if(tftp_error) return 0;
unsent_data = tftp_get_block(iobuf+iobuf_offset, (at_start ? PC64_SIZE : 0));
if(unsent_data) {
sent_this_time = 0;
}
if(tftp_request_in_progress != TFTP_GET) {
eoi = 1;
break;
}
}
if(unsent_data) {
for(int i=0;i<unsent_data-1+iobuf_offset;i++) {
put_byte(iobuf[i], 0);
sent_to_c64++;
sent_this_time++;
}
if(eoi) {
if(at_start) {
put_byte(iobuf[unsent_data-1], eoi);
sent_to_c64++;
sent_this_time++;
} else {
put_byte(iobuf[unsent_data], eoi);
sent_to_c64++;
sent_this_time++;
}
}
unsent_data = 0;
} else if(eoi) {
put_byte(iobuf[0], 1);
sent_to_c64++;
sent_this_time++;
}
if(!eoi) {
iobuf[0] = iobuf[TFTP_BLKSIZE-PC64_SIZE*at_start-1+iobuf_offset];
iobuf_offset = 1;
at_start = 0;
}
}
return 1;
}
int recv_file() {
int a;
int eoi = 0;
tftp_put_file(tftp_filename);
pc64_make_header(iobuf);
a=26;
do {
if(atn_active()) return 0;
ethernet_probe_for_packet();
eoi = get_byte(&iobuf[a], 0);
a++;
if(tftp_request_in_progress != TFTP_PUT || tftp_error) return 0;
if(a == 256 || eoi) {
while(!tftp_put_block(iobuf, a))
ethernet_probe_for_packet();
if(a == 256 && eoi) {
while(!tftp_put_block(iobuf, 0))
ethernet_probe_for_packet();
}
a = 0;
}
} while(!eoi);
return 1;
}
void handle_listen(uchar sec_addr) {
Serial.print("Mode/Device: ");
Serial.println(device_mode, HEX);
Serial.print("Secondary: ");
Serial.println(secondary, HEX);
Serial.print("Filename: ");
Serial.println((char *)iobuf);
if(sec_addr != 1) {
Serial.println("TODO: SAVE only for now!");
// Only supporting SAVE
return;
}
create_tftp_filename(iobuf);
if(!recv_file()) {
release_clock();
release_data();
}
}
void handle_talk(uchar sec_addr) {
int eoi = 0;
Serial.print("Mode/Device: ");
Serial.println(device_mode, HEX);
Serial.print("Secondary: ");
Serial.println(secondary, HEX);
Serial.print("Filename: ");
Serial.println((char *)iobuf);
if(strlen((const char *)iobuf) == 1 && iobuf[0] == '$') {
strcpy(tftp_filename, "list.dir");
} else {
create_tftp_filename(iobuf);
}
if(!send_file()) {
/* Should hopefully signal error */
release_clock();
release_data();
}
}
void handle_atn() {
uchar device,sec,sec_addr;
uchar mode,sec_mode;
wait_clock(HIGH);
wait_clock(LOW);
if(!get_byte(&device, 1)) return;
mode = device & IEC_COMMAND;
device_mode = device;
if(mode == IEC_UNLISTEN) {
wait_atn(HIGH);
return;
} else if(mode == IEC_UNTALK) {
wait_atn(HIGH);
return;
} else if(mode == IEC_LISTEN) {
if(DEVNO != (device & IEC_DEVICE)) {
/* Not our device */
release_clock();
release_data();
Serial.println("LISTEN: Not our device");
Serial.print("Mode/Device: ");
Serial.println(device_mode, HEX);
return;
}
} else if(mode == IEC_TALK) {
if(DEVNO != (device & IEC_DEVICE)) {
/* Not our device */
release_clock();
release_data();
Serial.println("TALK: Not our device");
return;
}
}
delayMicroseconds(200);
if(!get_byte(&sec, 1)) return;
secondary = sec;
sec_mode = sec & IEC_MODE;
if(sec_mode == IEC_OPEN) {
wait_atn(HIGH);
sec_addr = sec & IEC_ADDRESS;
handle_open(sec_addr);
} else if(sec_mode == IEC_CLOSE) {
sec_addr = sec & IEC_ADDRESS;
handle_close(sec_addr);
} else if(sec_mode == IEC_DATA) {
sec_addr = sec & IEC_ADDRESS;
if(mode == IEC_LISTEN) {
wait_atn(HIGH);
handle_listen(sec_addr);
} else if(mode == IEC_TALK) {
release_clock();
release_data();
wait_atn(HIGH);
delayMicroseconds(200);
release_data();
pull_clock();
delayMicroseconds(200);
handle_talk(sec_addr);
release_data();
release_clock();
}
} else if(sec_mode == IEC_FASTLOAD) {
sec_addr = sec & IEC_ADDRESS;
// handle_fastload();
}
}
void setup() {
pinMode(SRQIN, OUTPUT);
pinMode(ATN, INPUT);
pinMode(CLOCK, INPUT);
pinMode(DATA, INPUT);
pinMode(AVR_EOI, OUTPUT);
pinMode(AVR_DEBUG, OUTPUT);
pinMode(AVR_TIMEOUT, OUTPUT);
clear_iobuf();
pull_clock();
digitalWrite(CLOCK, HIGH);
release_clock();
pull_data();
Serial.begin(9600);
Serial.println("Initializing Ethernet...");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {
Serial.println( "Failed to access Ethernet controller");
}
Serial.println("Ethernet connected...");
Serial.println("Sending DHCP Request...");
if (!ether.dhcpSetup()) {
Serial.println("DHCP failed");
} else {
dhcp_active = 1;
Serial.println("DHCP Request received...");
Serial.println("Fetching Server MAC...");
ether.sendArpRequest(ether.serverip);
while(!ether.serverMacKnown())
ether.packetLoop(ether.packetReceive());
Serial.println("Server MAC received...");
}
}
void loop() {
ethernet_probe_for_packet();
if(atn_active()) {
release_clock();
pull_data();
handle_atn();
} else {
delayMicroseconds(100);
}
}