-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm-lpt.c
executable file
·1403 lines (1280 loc) · 34.6 KB
/
comm-lpt.c
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
/*
64NET/2 Commune module
(C)Copyright Paul Gardner-Stephen 1995, 1996, 2003-2005 All Rights Reserved.
This module does all the C64 <--> Server communications
Printer port version
*/
/*
BUGS:
This programme will have to be greatly generalised as time progresses.
At present it will hopefully support the full drive redirection scope
intended. Communication device generalisation will be next on the
list.
*/
#include "config.h"
#include "fs.h"
#include "comm-lpt.h"
#include "client-comm.h"
#include "fs_accel.h"
#include "misc_func.h"
#include "comm-work.h"
#include "datestamp.h"
#include "dosemu.h"
int parallel_iec_commune(int first_char);
extern int piec_delaycount;
#define PADDING 1
/* ports */
#ifdef USE_SERIAL_DRIVER
uchar port[1024] = COMM_DEVICE;
#else
uchar port[1024] = "/dev/parport0"; // LPT_DEVICE;
#endif
/* Current talk & listen logical files */
int talklf=-1;
int listenlf=-1;
int lastlf=-1;
/* ** DRIVE and PARTITION resolution: */
/* Partition base directories for each drive */
uchar *partn_dirs[MAX_NET_DEVS][256];
/* ** CBM DOS emulation stuff: */
/* DOS Statii */
uchar dos_status[MAX_NET_DEVS][256];
/* length of DOS statii */
uchar dos_stat_len[MAX_NET_DEVS];
/* position of char we actually talking */
uchar dos_stat_pos[MAX_NET_DEVS];
/* DOS commands */
uchar dos_command[MAX_NET_DEVS][256];
/* length of DOS command */
uchar dos_comm_len[MAX_NET_DEVS]={0};
/* ** current directories & partitions: */
/* current partition on each drive */
int curr_par[MAX_NET_DEVS];
/* current subdirectory (absolute) for each partition on each
drive */
uchar *curr_dir[MAX_NET_DEVS][256];
/* dir blocks for partitions */
int partn_dirtracks[MAX_NET_DEVS][256];
int partn_dirsectors[MAX_NET_DEVS][256];
int curr_dirtracks[MAX_NET_DEVS][256];
int curr_dirsectors[MAX_NET_DEVS][256];
/* Logical files */
fs64_file logical_files[MAX_NET_DEVS][16];
/* Device number as given in config file and passed to client upon boot */
int devnum=1;
/* client system type */
int client_type=0;
/* Last accessed drive, for the purposes of error channels etc */
int last_drive=-1; /* none */
int last_unit=-1; /* none */
/* Drive currently being accessed */
int file_unit=-1;
/* structures for redirected devices */
int devices[MAX_NET_DEVS]={0};
/* file that points to our communications device */
fs64_file file;
/* partition # that will be searched for programs, whatever this means, not used */
int pathdir;
/* our lpt device filedescriptor */
int lpt_fd;
//#define DEBUG_PIEC
#define FLIPFLOP
#include <fcntl.h>
#include <stdio.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <time.h>
#ifdef LINUX
#include "lptio/lptio_linux.c"
#endif
#ifdef __FreeBSD__
#include "lptio/lptio_freebsd.c"
#endif
#ifdef AMIGA
#include "amiga/my_exec_types.h"
#include <exec/semaphores.h>
#include <clib/exec_protos.h>
#include <clib/misc_protos.h>
#include <hardware/cia.h>
#include <resources/misc.h>
extern volatile struct CIA ciaa;
extern volatile struct CIA ciab;
#endif
#ifdef WINDOWS
#include <windows.h>
typedef void (CALLBACK * outb_t)(int,int);
typedef unsigned char (CALLBACK * inb_t)(int);
HINSTANCE dll_handle;
WSADATA wsa_data;
outb_t outb;
inb_t inb;
#endif
//different states
#define IEC_IDLE 0x0
#define IEC_TALK 0x40
#define IEC_LISTEN 0x20
#define IEC_UNTALK 0x5f
#define IEC_UNLISTEN 0x3f
#define IEC_IDENTIFY 0xff
//special SAs for LOAD/SAVE
#define IEC_LOAD 0x60
#define IEC_SAVE 0x60
#define IEC_OPEN 0xf0
#define IEC_CLOSE 0xe0
#define ERROR_EOI 0x40
#define ERROR_FILE_NOT_FOUND 0x02
#define ERROR_FILE_EXISTS 0x03
#define uchar unsigned char
unsigned char request;
int ATN=0;
int devicenumber=9;
uchar SA;
int state=IEC_IDLE;
int myfilenamelen=0;
unsigned char* myfilename;
/* for measuring transfertimes */
clock_t transfertime;
long transferred_amount=0;
unsigned int iec_listen();
unsigned int iec_talk();
void iec_unlisten();
void iec_untalk();
void iec_idle();
unsigned char change_state(unsigned int);
int send_byte(unsigned char data, int error_code);
int receive_byte(int error_code, int wait);
int start_server();
int send_error(unsigned char err);
void begin_measure();
void end_measure();
void acknowledge();
//todo: maybe we add also the resetpin? If the c64 resets also the 64net/2 can
// reset and get into IDLE mode again.
int parallel_iec_commune(int unused) {
lpt_fd=bind_to_port(port); /*doesn't soemhow work with the port variable form above*/
if (lpt_fd<0) {
fprintf(stderr,"Failed to open parallel port.\n");
perror("open_lptport");
}
last_unit=0; file_unit=0;
set_datalines_input();
set_lpt_control(0); // drop strobe
// while(1) {
// printf("$%x\n",get_status());
// }
request=get_status()&REQUEST_IN; //get initial state of PA2 from C64
while(start_server()==-1);
return 0;
}
int start_server() {
for(;;) {
switch(state) {
case IEC_IDLE:
#ifdef DEBUG_PIEC
printf("changed state: IDLE\n");
#endif
iec_idle();
break;
case IEC_LISTEN:
#ifdef DEBUG_PIEC
printf("changed state: LISTEN\n");
#endif
iec_listen();
break;
case IEC_TALK:
#ifdef DEBUG_PIEC
printf("changed state: TALK\n");
#endif
iec_talk();
break;
case IEC_UNLISTEN:
#ifdef DEBUG_PIEC
printf("changed state: UNLISTEN\n");
#endif
iec_unlisten();
break;
case IEC_UNTALK:
#ifdef DEBUG_PIEC
printf("changed state: UNTALK\n");
#endif
iec_untalk();
break;
case IEC_IDENTIFY:
#ifdef DEBUG_PIEC
printf("changed state: IDENTIFY\n");
#endif
//iec_identify();
break;
}
}
return -1;
}
void iec_idle() {
int temp;
//no need to waste 100% CPU time when being idle, so set a polling interval of 1ms
temp=receive_byte(-1,1000);
if((temp&0x100)!=0) change_state(temp);
return;
}
void iec_identify() {
/* we are asked to idenitfy ourself, so present the deviceumber under
which we listen. */
change_state(IEC_IDLE);
send_error(devicenumber);
return;
}
void iec_untalk() {
/* all we should do si stop talking, and that we do anyway as
* atn came high. So we just fall into idle mode and wait
* for new commands */
change_state(IEC_IDLE);
return;
}
void iec_unlisten() {
change_state(IEC_IDLE);
//now we can clear the pos, as we closed the file anyway and clear the status;
//now we can fall into IDLE state to wait for new challanges.
return;
}
unsigned int iec_listen() {
unsigned int temp;
int maxcount=1024;
change_state(IEC_IDLE);
temp=receive_byte(-1,0);
/* we expect a SA coming under ATN high, if something is suspect we fall back to idle */
if((temp&0x100)==0) return -1;
SA=temp;
#ifdef DEBUG_PIEC
printf("listen SA=$%X\n",(uchar)SA);
#endif
/* Record file number for listen */
listenlf=SA&0xf;
/* the data we'll receive should be saved, so we prepare for that */
if((SA&0xf0)==IEC_SAVE && listenlf!=0xf) {
#ifdef DEBUG_PIEC
printf("SA commands us to save\n");
#endif
/* if the file doesn't exist yet we end up here and try to create it now */
if (logical_files[file_unit][listenlf].open != 1) {
#ifdef DEBUG_PIEC
printf("Trying to open file...\n");
#endif
if (fs64_openfile_g (curr_dir[last_unit][curr_par[last_unit]], myfilename, &logical_files[file_unit][listenlf])) {
#ifdef DEBUG_PIEC
printf("Can't find file, creating it...\n");
#endif
/* file not found */
/* postpend ",W" to the filename and try again */
strcat ((char*)myfilename, ",W");
#ifdef DEBUG_PIEC
printf("Filename now:%s\n",myfilename);
#endif
if (fs64_openfile_g (curr_dir[last_unit][curr_par[last_unit]], myfilename, &logical_files[file_unit][listenlf])) {
/* cannot open file for some reason (missing permissions or such) */
/* simply abort */
temp=receive_byte(ERROR_FILE_EXISTS,0);
//unsure about the next line, but should be there for savety
if((temp&0x100)!=0) { change_state(temp); return -1; }
return -1;
}
}
}
//we managed to open a file and have write permissions so we can start saving into it now
if (logical_files[file_unit][listenlf].open == 1) {
#ifdef DEBUG_PIEC
printf("File is open. Now receiving data to save...\n");
#endif
set_error(0,0,0);
/* pour all data into that file until finished or ATN gets high */
while(1) {
/* receive a byte */
temp=receive_byte(-1,0);
/* check if data received under ATN high */
if((temp&0x100)!=0) { change_state(temp); return -1; }
#ifdef DEBUG_PIEC
printf("Got byte $%x\n",temp);
#endif
fs64_writechar(&logical_files[file_unit][listenlf], temp);
}
}
else {
//XXX missing errorhandling here. TB, could be disk full, or write protection on
}
return 0;
}
if((SA&0xf0)==IEC_OPEN || listenlf==0x0f) { /* OPEN/DOS COMMAND */
begin_measure();
myfilenamelen=0;
myfilename=malloc(maxcount);
#ifdef DEBUG_PIEC
printf("Now receiving data...\n");
#endif
//first receive something (filename/command)
while(1) {
/* receive a byte */
temp=receive_byte(-1,0);
if((temp&0x100)!=0) { change_state(temp); break; }
/* byte not under attention: store in input buffer */
myfilename[myfilenamelen]=(uchar)temp;
myfilenamelen++;
/* XXX we should do error handling on too long filenames here. TB */
if(myfilenamelen>=maxcount) {
maxcount+=1024;
myfilename=realloc(myfilename,maxcount);
}
}
myfilename=realloc(myfilename,myfilenamelen+1);
myfilename[myfilenamelen]=0;
/* we are commanded to open, either a file or a command, lets decide further */
if(listenlf==0x0f) {
myfilename[myfilenamelen]=0;
/* Data received was a command */
#ifdef DEBUG_PIEC
printf("Received command \"%s\"\n", myfilename);
#endif
strcpy(dos_command[last_unit],myfilename);
dos_comm_len[last_unit]=myfilenamelen;
if (dos_comm_len[last_unit]!=0) do_dos_command();
if(myfilenamelen>0) { free(myfilename); myfilenamelen=0; }
}
else {
/* Data received was a filename to open */
debug_msg ("*** Opening [%s] on channel $%02x\n", myfilename, listenlf);
//XXX openfile seems to be a bit buggy, has sometimes file.open==1 though it doesn't exist or is a dir.
if(fs64_openfile_g (curr_dir[last_unit][curr_par[last_unit]], myfilename, &logical_files[file_unit][listenlf])!=0) {
logical_files[file_unit][listenlf].open=0;
}
}
return 0;
}
if((SA&0xf0)==IEC_CLOSE) { /* CLOSE */
end_measure();
if (logical_files[file_unit][listenlf].open == 1) {
fs64_closefile_g (&logical_files[last_unit][listenlf]);
set_error(0,0,0);
}
/* Close file from SA */
#ifdef DEBUG_PIEC
printf("Closing logical file %d\n",listenlf);
#endif
/* do nothing except cleaning up */
if(myfilenamelen>0) { free(myfilename); myfilenamelen=0; }
return 0;
}
//if non of the above SAs applies, interpret as new command.
//TASS does such buggy things for example like sending
//$29, $29, $F0 = listen, listen, SA :-/
change_state(temp);
return 0;
}
void begin_measure() {
transferred_amount=0;
transfertime=clock();
return;
}
void end_measure() {
float time;
float rate;
long timediff;
clock_t endtime=clock();
timediff=endtime-transfertime;
time=((float)timediff/(float)CLOCKS_PER_SEC);
rate=(float)transferred_amount/time;
printf("Transferred bytes:%ld\n", transferred_amount);
printf("Time needed:%f\n", time);
printf("Transferrate:%8.2f bps (%8.3fKB/sec)\n", rate,rate/1024);
return;
}
unsigned int iec_talk() {
unsigned int temp=0;
unsigned char data=0;
unsigned char buffer=0;
int i;
change_state(IEC_IDLE);
temp=receive_byte(-1,0);
/* we expect a SA coming under ATN high, if something is suspect we fall back to idle */
if((temp&0x100)==0) return -1;
SA=temp;
#ifdef DEBUG_PIEC
printf("SA=$%X\n",(uchar)SA);
#endif
#ifdef DEBUG_PIEC
printf("Waiting for ATN going low first...\n");
#endif
/* don't start talking while atn is high, as this is really not good for our CIA
* and will bring us seriously out of sync too :-) */
while((get_status()&ATN_IN)!=ATN_IN) {
#ifdef DEBUG_PIEC
printf(".");
#endif
}
#ifdef DEBUG_PIEC
printf("ATN is low now, so serving requested data...\n");
#endif
talklf=SA&0xf;
if((SA&0xf0)==IEC_LOAD && talklf!=0x0f) {
//we can now clear the filename, hopefully
//not really needed here, as it will be done on close
//if(myfilenamelen>0) { free(myfilename); myfilenamelen=0; }
//do we have an open file?
if (logical_files[file_unit][listenlf].open != 1) {
/* File not found, now signal it with sending a dummy
* char followed by a timeout */
//maybe we just tried to load a dir?
//if so, let's change to that dir!
dos_comm_len[last_unit]=3+myfilenamelen;
dos_command[last_unit][0]='C';
dos_command[last_unit][1]='D';
dos_command[last_unit][2]=':';
for(i=0;i<myfilenamelen;i++) {
dos_command[last_unit][3+i]=myfilename[i];
}
if(do_dos_command()==0) {
printf("changed sucessful\n");
set_error(0,0,0);
if(send_byte(0x01,-1)==-1) return -1;
if(send_byte(0x8,-1)==-1) return -1;
if(send_byte(0,-1)==-1) return -1;
if(send_byte(0,ERROR_EOI)==-1) return -1;
// if(send_byte(0,ERROR_FILE_NOT_FOUND)==-1) return -1;
return 0;
}
else {
set_error(62,0,0);
if(send_byte(0,ERROR_FILE_NOT_FOUND)==-1) return -1;
return 0;
}
}
else {
/* start sending bytes until finished or ATN high */
if(!fs64_readchar(&logical_files[file_unit][talklf],&data)) {
while(1) {
buffer=data;
if(fs64_readchar(&logical_files[file_unit][talklf],&data)) break;
if(send_byte(buffer, -1)==-1) return -1; //send actual byte
}
}
set_error(0,0,0);
if(send_byte(buffer,ERROR_EOI)==-1) return -1; //now send last byte + EOI
return 0;
}
}
if(talklf==0x0f) {
/* the c64 reads out the error channel, so talk the status */
#ifdef DEBUG_PIEC
printf("Sending status123...\n");
//printf("Status: %s", dos_status[last_unit]);
#endif
while(1) {
data=dos_status[last_unit][dos_stat_pos[last_unit]];
if(send_byte(data,-1)==-1) return -1;
if(data!=0x0d) {
dos_stat_pos[last_unit]++;
}
else {
set_error(0,0,0);
return 0;
}
}
}
return 0;
}
int send_byte(unsigned char data, int error_code) {
unsigned char temp;
/* we have to make a decision here out of a single look
* on the status. If we do two checks we read the status
* twice and probably do a check on two different values
* of status! Thus the decision might be faulty and we
* get into big troubles! */
#ifdef FLIPFLOP
if((get_status()&ATN_IN)==ATN_IN) {
set_datalines_output();
write_data((data&0xff));
if(error_code==-1) acknowledge();
else send_error(error_code);
}
#endif
while(1) {
temp=get_status();
if((temp&ATN_IN)!=ATN_IN) {
set_datalines_input();
request=temp&REQUEST_IN;
#ifdef DEBUG_PIEC
printf("ATN changed!\n");
#endif
/* ATN went high, we better stop! */
ATN=1;
return -1;
}
if((temp&REQUEST_IN)!=request) break;
}
ATN=0;
request^=REQUEST_IN;
#ifdef DEBUG_PIEC
printf("Request toggled $%x $%x\n",get_status(),request);
#endif
#ifndef FLIPFLOP
set_datalines_output();
write_data((data&0xff));
if(error_code==-1) acknowledge();
else send_error(error_code);
#endif
/* Record bytes sent for calculating LOAD/SAVE speed */
transferred_amount++;
/* When we send a byte, we do not immediately provide an ACK signal to the
* C64. This is to allow for the calling routine to decide when the data
* has been completely sent, and so if we need to assert EOI instead of
* ACK. */
return 0;
}
/* Added a polling-interval value to avoid unnecessary
* cpu-cycles when being idle */
int receive_byte(int error_code, int wait) {
int data=0;
float time;
#ifdef DEBUG_PIEC
printf("Receiving data...\n");
#endif
//set datalines to inputmode
set_datalines_input();
//let c64 prepare things first and wait (#$00 -> $dd03)
#ifdef DEBUG_PIEC
printf("Waiting for request to toggle...\n");
#endif
//If ATN == 1 we had already noticed a toggle before and also
//acknowledged the byte unfortunatedly. so no need to wait here
//anymore but we grant a timeout and try to save the data that
//is hopefully still valid!
if(ATN==1) {
//better save data before it is too late
data|=(read_data()&0xff);
//in case of ATN went high during send_byte, we only poll request with
//an timeout else we might hang here forever as we might have missed
//the toggle of REQUEST_IN
time=clock();
// while(clock()-time<(float)(CLOCKS_PER_SEC/(float)10)) {
// if((get_status()&REQUEST_IN)!=request) break;
// }
for(;;) {
if( clock()-time < (float)(CLOCKS_PER_SEC/(float)10)) {
if ((get_status()&REQUEST_IN)!=request) break;
}
else {
request^=REQUEST_IN;
printf("timeout!\n");
break;
} //timeout
}
}
else {
while((get_status()&REQUEST_IN)==request) {
if(wait>0) usleep(wait);
}
}
request^=REQUEST_IN;
#ifdef DEBUG_PIEC
printf("Request toggled r\n");
#endif
/* Check if the byte was received under attention.
* This is always in sync as the C64 can't change ATN in that situation
* as it is polling for cnt or flag coming high from our acknowledge.
* If ATN high, OR the read byte with $100, so that we know it was received
* under attention. */
//Also we might miss out ATN being high here, so we test also for
//the flag from last send_byte!
if(ATN==1 || (get_status()&ATN_IN)!=ATN_IN) {
#ifdef DEBUG_PIEC
printf("data received under ATN\n");
#endif
data|=0x100;
ATN=0;
}
if(ATN==0) {
data|=(read_data()&0xff);
}
if(error_code==-1) acknowledge();
else send_error(error_code);
/* Record number of bytes transfered so that we can determine SAVE speed */
transferred_amount++;
return data;
}
void acknowledge() {
set_lpt_control(VALID_OUT);
set_lpt_control(0);
}
int send_error(unsigned char err) {
int status;
int a;
struct timeval time;
time.tv_sec = 0;
time.tv_usec = 1000; //5µs is on teh save side! with 1µs i sometimes fail to send the error! TB
#ifdef DEBUG_PIEC
printf("Triggering CNT...\n");
#endif
/* XXX My C128D seems to need 16 toggles, even though the documentation
* suggests 8 should be enough. */
/* XXX unfortunatedly when toggling 16 times the c64 + 64net hangs (guess some
* double triggered error :-( */
/* all this is hopefully solved now! Actually the pc sometimes toggles things
* too fast here. So i keep every state up at least 1µs, that is more than
* one cycle on c64 side. Since that also serial transfer of the errorcode
* to SP2 works stable. */
for(a=0;a<8;a++) {
status=ERROR_CLOCK;
if((err&0x80)!=0) status|=ERROR_DATA;
set_lpt_control(status);
select(1,NULL,NULL,NULL,&time); //wait 1µs
//usleep(1);
status^=ERROR_CLOCK;
set_lpt_control(status);
select(1,NULL,NULL,NULL,&time); //wait 1µs
//usleep(1);
err=err<<1;
}
return 0;
}
uchar change_state(unsigned int new_state) {
/* Change TALK/LISTEN/UNLISTEN/UNTALK/IDENTIFY state based on character received under attention. */
new_state=(new_state&0xff);
if((new_state&0xf0)==IEC_LISTEN) {
if(devicenumber==(new_state&0x0f)) {
state=IEC_LISTEN;
#ifdef DEBUG_PIEC
printf("Received Listen command\n");
#endif
}
else {
#ifdef DEBUG_PIEC
printf("Received Listen, but for different device.\n");
#endif
}
return 0;
}
else if((new_state&0xf0)==IEC_TALK) {
if(devicenumber==(new_state&0x0f)) {
state=IEC_TALK;
#ifdef DEBUG_PIEC
printf("Received Talk command\n");
#endif
}
else {
#ifdef DEBUG_PIEC
printf("Received Talk, but for different device\n");
#endif
}
return 0;
}
else if((new_state&0xff)==IEC_UNLISTEN) {
state=IEC_UNLISTEN;
#ifdef DEBUG_PIEC
printf("Received Unlisten command\n");
#endif
return 0;
}
else if((new_state&0xff)==IEC_UNTALK) {
state=IEC_UNTALK;
#ifdef DEBUG_PIEC
printf("Received Untalk command\n");
#endif
return 0;
}
else if((new_state&0xff)==IEC_IDLE) {
state=IEC_IDLE;
#ifdef DEBUG_PIEC
printf("Going into IDLE mode...\n");
#endif
return 0;
}
else {
/* Some unknown command has been received, so lets go back to IDLE,
* and wait for the next command char */
#ifdef DEBUG_PIEC
printf("Received unknown char: 0x%X\n", new_state);
#endif
state=IEC_IDLE;
return -1;
}
return -1;
}
/* These configuration reading routines really should be in another module,
but that would entail global variables which i want to avoid.
*/
int
read_config (char *file)
{
/* Read in the 64netrc file */
FILE *cf = 0;
uchar temp[256];
if ((cf = fopen (file, "r")) == NULL)
fatal_error ("Cannot read configuration file.");
while (!feof (cf))
{
fgets ((char*)temp, 256, cf);
if ((temp[0] == '#') || (temp[0] == 0) || (temp[0] == '\n') || (temp[0] == '\r'))
{
/* its a comment or blank line */
}
else
{
/* its a real line of stuff */
#ifdef WITH_OLD_STUFF
if (!strncmp ("ram ", temp, 3))
{
/* create `geoRAM' */
if (georam_size != 0)
{
printf ("INIT: GEOS-RAM already specified - ignoring redeclaration\n");
}
else
{
georam_size = atol (&temp[4]);
if (georam_size > 0)
{
if (!(georam =
(uchar *) malloc (georam_size * 256 + PADDING)))
/* couldnt malloc */
fatal_error ("Cannot allocate memory for GEOS-RAM (try smaller size!).");
printf ("INIT: GEOS-RAM created (%d pages = %dKB)\n", georam_size, georam_size / 4);
}
else
{
fatal_error ("INIT: Bad value for GEOS-RAM size - Barf!\n");
}
}
}
else
#endif
if (!strncmp ("port ", temp, 4))
{
/* its a port line */
#ifdef AMIGA
printf ("INIT: Using internal parallel port\n");
strcpy (port, &temp[5]);
#else
strcpy (port, &temp[5]);
/* chop CR/LF */
while((port[strlen(port)-1]=='\n')||(port[strlen(port)-1]=='\r'))
port[strlen(port)-1]=0;
printf ("INIT: Communication port set to %s\n", port);
#endif
}
else if (!strncmp ("path ", temp, 4))
{
/* path partition */
pathdir = atol (&temp[5]);
printf ("64NET/2 will look in partition %d for programmes\n", pathdir);
}
else if (!strncmp ("devnum ", temp, 6))
{
/* 64net2 emulated device number */
devnum = atol (&temp[7]);
printf ("64NET/2 will be seen from client as device %d\n", devnum);
}
else if (!strncmp ("device ", temp, 6))
{
/* its a device line */
read_device (cf);
}
else if (!strncmp ("ledfile", temp, 6))
{
/* its a device line */
if (temp[strlen (temp) - 1] < ' ')
temp[strlen (temp) - 1] = 0;
if (temp[7] < ' ')
sprintf ((char*)&temp[7], " %s/.64net2.leds", getenv ("HOME"));
client_init (&temp[8]);
client_activity (0);
client_error (0);
}
#ifdef WITH_OLD_STUFF
else if (!strncmp ("timeout ", temp, 7))
{
/* set max_tries */
max_tries = atoi (&temp[8]);
printf ("INIT: Timeout set to %d attempts\n", max_tries);
}
#endif
else if (!strncmp ("debug mode", temp, 9))
{
/* debug mode */
debug_mode = 1;
printf ("INIT: Debug mode messages will be displayed.\n");
}
#ifdef WITH_OLD_STUFF
else if (!strncmp ("fishload", temp, 7))
{
/* enable fishload */
allowFishLoad = 1;
printf("INIT: enabled fishload routine.\n");
#ifdef USE_SERIAL_DRIVER
allowFishLoad = 0;
printf("INIT: fishload disabled on serial driver.\n");
#endif
}
else if (!strncmp ("fishsave", temp, 7))
{
/* enable fishsave */
allowFishSave = 1;
printf("INIT: enabled fishsave routine.\n");
#ifdef USE_SERIAL_DRIVER
allowFishSave = 0;
printf("INIT: fishsave disabled on serial driver.\n");
#endif
}
#endif
else
{
/* Unknown line type */
fprintf(stderr,"Unknown configuration directive:\n%s\n",temp);
fatal_error ("Bad configuration file. Unknown line types.");
}
}
}
fclose (cf);
cf = 0;
/* all done! */
/* Check for required bits */
if (!devices[0])
{
/* no devices defined */
fatal_error ("Configuration file contains no device lines.");
}
if (port[0] == 0)
{
/* no port lines */
fatal_error ("Configuration file contains no port line.");
}
return (0);
}
int
read_device (FILE * cf)
{
/* read a device section */
int dev_num, i, pn;
char temp[256];
for (dev_num = 0; dev_num < MAX_NET_DEVS; dev_num++)
if (!devices[dev_num])
break;
if (dev_num >= MAX_NET_DEVS)
{
/* too many network devices */
fatal_error ("Too many network devices defined.");
}
else
{
/* yup, we can cope */
while (!feof (cf))
{
fgets (temp, 256, cf);
if ((temp[0] == '#') || (temp[0] == 0))
{
/* comment line */
}
else
{
/* real line */
/* Acceptables are:
NUMBER <dev_num> - Sets the device # for this dev
PARTITION <dev_num>,<path> - Define a partition
END DEVICE - End definition of device
PRINTER - Define device to be a printer,
not a drive.
*/
if (!strncmp ("number", temp, 6))
{
/* device number */
devices[dev_num] = (uchar) atol (&temp[6]);
printf ("Networked device $%02x assigned to IEC device $%02x\n",
dev_num, devices[dev_num]);
}
else if (!strncmp ("partition", temp, 9))
{
/* partition for drive */
/* find first comma */
for (i = 9; i < strlen (temp); i++)
if (temp[i] == ',')
break;
if (i >= strlen (temp))
fatal_error ("Bad partition line (no commas).");
pn = atol (&temp[9]);
if ((pn < 1) || (pn > 255))
{
/* bad partition # */
fatal_error ("Bad partition number. Must be between 1 and 255.");
}
else
{
/* okey */
partn_dirs[dev_num][pn] =
(uchar *) malloc (strlen (&temp[i + 1]) + PADDING);
if (!partn_dirs[dev_num][pn])
/* couldnt malloc */
fatal_error ("Cannot allocate memory.");
else
{
/* strip newline */