-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrdevice.c
1455 lines (1335 loc) · 36.8 KB
/
rdevice.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
/*
* rdevice.c - Atari850 emulation
*
* Copyright (c) ???? Tom Hunt, Chris Martin
* Copyright (c) 2003-2005,2007-2008 Atari800 development team (see DOC/CREDITS)
*
* This file is part of the Atari800 emulator project which emulates
* the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
*
* Atari800 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Atari800 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Atari800; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Originally From Closer To Home (Tom Hunt)
*
* 2003.04.03 [email protected] - Added connecting Host name lookup and port
* changing as well as CONNECT_STRING changing...
* 2003.04.03 [email protected] - Added setsockopt SO_REUSEADDR for socket to
* allow reconnections
* 2003.04.05 [email protected] - Added sighandler for catching SIGPIPE error...
* can get this and cause the emu to crash if
* the other end disconnects prematurely...
* 2003.04.06 [email protected] - Added translation on/off and line feeds
* - Fixed IP address lookup (print ip address if
* lookup fails...)
* 2003.04.07 [email protected] - Added Local echo when not connected.
* 2003.04.17 [email protected] - Added Telnet escape sequence parsing for
* connecting to telnet
* 2003.09.02 [email protected] - Cleaned up Serial port (/dev/ttyS*) Handling
* 2003.09.05 [email protected] - Included minor bug fixes submitted from
* Christian Groessler (thanks Chris!)
* - Removed erroneous return in RREAD().
* - Fixed typo for 9600 baud in xio_36()
* - Modified serial port dev_name generation in
* open_connection_serial()
* - Now we only modify the serial port options if
* we can open a port (in open_connection_serial())
* - Removed Telnet IAC escape sequence handling in
* RWRIT() and now we only
* write to the port if we are connected.
* 2003.09.08 [email protected] - Added missing baud rates and data word
* lengths (5,6,7,8)
* 2003.09.11 [email protected] - Fixed Address lookup in open_connection()
* - Reformatted and cleaned whole file.
* - Removed all memory writes to addresses
* 746 - 749 except within the RSTAT function.
*
* TODO:
* create a new socket and a buffer for each port number....
* non-concurrent mode error reporting to 747
* error bits in location 746
* implement xio 32?
* error reporting to CPU_regY
* heavy ASCII translation
* controlling DTR, RTS, & XMT - xio 34
*
* Dialing out via telnet:
* Type "ATDL" to toggle Linefeeds on/off (normally off)
* Type "ATDI <hostname> <port>" example: "ATDI localhost 23"
* if the port is omitted, then 23 is assumed.
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef DEBUG
#define DBG_APRINT(x) Log_print(x)
#else
#define DBG_APRINT(x)
#endif
#if 0 /* debug stuff */
#ifdef R_NETWORK
#warning R_NETWORK defined
#endif
#ifdef R_SERIAL
#warning R_SERIAL defined
#endif
#endif
#ifdef WIN32
#ifdef R_SERIAL
#error The Windows version does not support the serial R: device
#endif
#ifndef R_NETWORK
#error R_NETWORK must be defined to use the Windows R: device
#endif
#include <winsock2.h>
#define F_SETFL 0
#define O_NONBLOCK 0
#define WM_SOCKET (WM_USER+20)
#define inet_pton(fam,ip,addr) ((addr)->s_addr = inet_addr(ip), 0)
static u_long ioctlsocket_non_block = 1; /* for ioctlsocket */
#define perror(a) printf("%s:WSA error code:%d\n",a,WSAGetLastError())
#define close(a) closesocket(a)
typedef char *caddr_t;
static void catch_disconnect(int sig);
static int rdevice_win32_read(SOCKET s, char *buf, int len) {
int r;
r = recv(s, buf, len, 0);
if (r <= 0 ) {
switch (WSAGetLastError()) {
case WSAECONNRESET:
case WSAECONNABORTED:
case WSAESHUTDOWN:
catch_disconnect(0);
break;
}
}
return r;
}
static int rdevice_win32_write(SOCKET s, char *buf, int len) {
int r;
r = send(s, buf, len, 0);
if (r <= 0 ) {
switch (WSAGetLastError()) {
case WSAECONNRESET:
case WSAECONNABORTED:
case WSAESHUTDOWN:
catch_disconnect(0);
break;
}
}
return r;
}
#define read(a,b,c) rdevice_win32_read(a,b,c)
#define write(a,b,c) rdevice_win32_write(a,b,c)
#elif defined(DREAMCAST) /* above WIN32, below DREAMCAST */
/* these functions reside in dc/atari_dc.c */
extern void dc_init_serial(void);
extern int dc_write_serial(unsigned char byte);
extern int dc_read_serial(unsigned char *byte);
extern void dc_set_baud(int baud);
#else /* above DREAMCAST, below not WIN32 and not DREAMCAST */
#if !defined(R_SERIAL) && !defined(R_NETWORK)
#error R: device needs serial or network or both
#endif
#ifdef R_NETWORK
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#endif /* R_NETWORK */
#include <sys/types.h>
#include <fcntl.h>
#endif /* not WIN32 and not DREAMCAST */
#if defined(R_SERIAL) && !defined(DREAMCAST)
#include <termios.h>
#endif /* defined(R_SERIAL) && !defined(DREAMCAST) */
#include "atari.h"
#include "rdevice.h"
#include "cpu.h"
#include "devices.h"
#include "log.h"
#include "memory.h"
#include "util.h"
#define Peek(a) MEMORY_dGetByte(a)
#define DPeek(a) MEMORY_dGetWord(a)
#define Poke(x,y) MEMORY_dPutByte(x, y)
/*---------------------------------------------------------------------------
Global Variables
---------------------------------------------------------------------------*/
static int connected;
static int do_once;
static int rdev_fd;
#ifdef R_NETWORK
static struct sockaddr_in in;
static struct sockaddr_in peer_in;
static int sock;
static int portnum = 9000;
static char inetaddress[256];
static char CONNECT_STRING[40] = "\r\n_CONNECT 2400\r\n";
static int retval;
#endif /* R_NETWORK */
static char MESSAGE[256];
static char command_buf[256];
static char bufout[256];
static int concurrent;
static int command_end = 0;
static int translation = 1;
static int trans_cr = 0;
static int linefeeds = 1;
static int bufend = 0;
#ifndef R_NETWORK
int RDevice_serial_enabled = 1;
#else
int RDevice_serial_enabled = 0; /* Default to network, if enabled. Use parameter to -rdevice command line switch to enable serial mode. */
#endif
char RDevice_serial_device[FILENAME_MAX];
/*---------------------------------------------------------------------------
Host Support Function - If Disconnect signal is found, then close socket
and clean up.
---------------------------------------------------------------------------*/
#ifdef R_NETWORK
static void catch_disconnect(int sig)
{
DBG_APRINT("R*: Disconnected....");
close(rdev_fd);
connected = 0;
do_once = 0;
bufout[0] = 0;
strcat(bufout, "\r\nNO CARRIER\r\n");
bufend = 13;
#if 0
Poke(747,bufend);
Poke(748,0);
#endif
}
#endif /* R_NETWORK */
/*---------------------------------------------------------------------------
Host Support Function - XIO 34 - Called from RDevice_SPEC
Controls handshake lines DTR, RTS, SD
---------------------------------------------------------------------------*/
static void xio_34(void)
{
#ifndef DREAMCAST /* Dreamcast port doesn't currently support handshake lines */
int temp;
/*int fid;*/
#ifdef R_SERIAL
struct termios options;
#endif /* R_SERIAL */
/*int status;*/
/*fid = MEMORY_dGetByte(0x2e) >> 4;*/
temp = MEMORY_dGetByte(Devices_ICAX1Z);
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
tcgetattr(rdev_fd, &options);
/*ioctl(rdev_fd, TIOCMGET, &status);*/
}
#endif /* R_SERIAL */
if(temp & 0x80)
{
if(temp & 0x40)
{
/* turn DTR on */
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
/*status |= TIOCM_DTR;*/
}
#endif /* R_SERIAL */
}
else
{
/*Drop DTR*/
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
cfsetispeed(&options, B0);
cfsetospeed(&options, B0);
}
#endif /* R_SERIAL */
if(connected != 0)
{
close ( rdev_fd );
connected = 0;
do_once = 0;
/*bufend = 0;*/
}
}
}
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
/* RTS Set/Clear*/
if(temp & 0x20)
{
if(temp & 0x10)
{
/*status |= TIOCM_RTS;*/
}
else
{
/*status &= ~TIOCM_RTS;*/
}
}
if(temp & 0x02)
{
if(temp & 0x01)
{
/*status |= TIOCM_RTS;*/
}
else
{
/*status &= ~TIOCM_RTS;*/
}
}
}
#endif /* R_SERIAL */
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
tcsetattr(rdev_fd, TCSANOW, &options);
/*ioctl(rdev_fd, TIOCMSET, &status);*/
}
#endif /* R_SERIAL */
#endif /* not defined DREAMCAST */
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
}
/*---------------------------------------------------------------------------
Host Support Function - XIO 36 - Called from RDevice_SPEC
Sets baud, stop bits, and ready monitoring.
---------------------------------------------------------------------------*/
static void xio_36(void)
{
int aux1, aux2;
#if defined(R_SERIAL) && !defined(DREAMCAST)
struct termios options;
#endif /* defined(R_SERIAL) && !defined(DREAMCAST) */
aux1 = MEMORY_dGetByte(Devices_ICAX1Z);
aux2 = MEMORY_dGetByte(Devices_ICAX2Z);
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
#ifndef DREAMCAST
tcgetattr(rdev_fd, &options);
/*Set Stop bits*/
if(aux1 & 0x80)
{ /*2 Stop bits*/
options.c_cflag |= CSTOPB;
}
else
{ /*1 Stop bit*/
options.c_cflag &= ~CSTOPB;
}
/*Set word size*/
if((aux1 & 0x30) == 0)
{ /*8 bit word size*/
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
}
else if((aux1 & 0x30) == 0x10)
{ /*7 bit word size*/
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
}
else if((aux1 & 0x30) == 0x20)
{ /*6 bit word size*/
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS6;
}
else if((aux1 & 0x30) == 0x30)
{ /*5 bit word size*/
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS5;
}
else
{ /*8 bit word size*/
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
}
/*Set Baud Rate*/
if((aux1 & 0x0f) == 0)
{ /*300 Baud*/
cfsetispeed(&options, B300);
cfsetospeed(&options, B300);
}
else if((aux1 & 0x0f) == 1)
{ /* 45.5 Baud (unsupported) - now 57600 */
#ifdef B57600
cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);
#else
cfsetispeed(&options, B50);
cfsetospeed(&options, B50);
#endif
}
else if ((aux1 & 0x0f) == 2)
{ /* 50 Baud */
cfsetispeed(&options, B50);
cfsetospeed(&options, B50);
}
else if ((aux1 & 0x0f) == 3)
{ /* 56.875 Baud (unsupported) - now 115200 */
#ifdef B115200
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
#else
cfsetispeed(&options, B50);
cfsetospeed(&options, B50);
#endif
}
else if((aux1 & 0x0f) == 4)
{ /* 75 Baud */
cfsetispeed(&options, B75);
cfsetospeed(&options, B75);
}
else if((aux1 & 0x0f) == 5)
{ /* 110 Baud */
cfsetispeed(&options, B110);
cfsetospeed(&options, B110);
}
else if((aux1 & 0x0f) == 6)
{ /* 134.5 Baud */
cfsetispeed(&options, B134);
cfsetospeed(&options, B134);
}
else if((aux1 & 0x0f) == 7)
{ /* 150 Baud */
cfsetispeed(&options, B150);
cfsetospeed(&options, B150);
}
else if((aux1 & 0x0f) == 8)
{ /* 300 Baud */
cfsetispeed(&options, B300);
cfsetospeed(&options, B300);
}
else if((aux1 & 0x0f) == 9)
{ /* 600 Baud */
cfsetispeed(&options, B600);
cfsetospeed(&options, B600);
}
else if((aux1 & 0x0f) == 10)
{ /* 1200 Baud */
cfsetispeed(&options, B1200);
cfsetospeed(&options, B1200);
}
else if((aux1 & 0x0f) == 12)
{ /* 2400 Baud */
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);
}
else if((aux1 & 0x0f) == 13)
{ /* 4800 Baud */
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
}
else if((aux1 & 0x0f) == 14)
{ /* 9600 Baud */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
}
else if((aux1 & 0x0f) == 15)
{ /* 19200 Baud */
#ifdef B19200
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
#else
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
#endif
}
else
{ /* 115200 Baud (can add 38400, 76800 if wanted) */
#ifdef B115200
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
#else
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
#endif
}
if(aux1 & 0x40)
{ /* 230400 Baud */
#ifdef B230400
cfsetispeed(&options, B230400);
cfsetospeed(&options, B230400);
#endif
}
tcsetattr(rdev_fd, TCSANOW, &options);
#else /* below DREAMCAST, above not; both R_SERIAL */
/* Set Stop bits */
if(aux1 & 0x80)
{ /* 2 Stop bits */
/* currently not supported on DC */
}
else
{ /* 1 Stop bit */
/* currently not supported on DC */
}
/* Set word size */
if((aux1 & 0x30) == 0)
{ /* 8 bit word size */
/* currently not supported on DC */
}
else if((aux1 & 0x30) == 0x10)
{ /* 7 bit word size */
/* currently not supported on DC */
}
else if((aux1 & 0x30) == 0x20)
{ /* 6 bit word size */
/* currently not supported on DC */
}
else if((aux1 & 0x30) == 0x30)
{ /* 5 bit word size */
/* currently not supported on DC */
}
else
{ /* 8 bit word size */
/* currently not supported on DC */
}
/* Set Baud Rate */
if((aux1 & 0x0f) == 0)
{ /* 300 Baud */
dc_set_baud(300);
}
else if((aux1 & 0x0f) == 1)
{ /* 45.5 Baud (unsupported) - now 57600 */
dc_set_baud(57600);
}
else if ((aux1 & 0x0f) == 2)
{ /* 50 Baud */
dc_set_baud(50);
}
else if ((aux1 & 0x0f) == 3)
{ /* 56.875 Baud (unsupported) - now 115200 */
dc_set_baud(115200);
}
else if((aux1 & 0x0f) == 4)
{ /* 75 Baud */
dc_set_baud(75);
}
else if((aux1 & 0x0f) == 5)
{ /* 110 Baud */
dc_set_baud(110);
}
else if((aux1 & 0x0f) == 6)
{ /* 134.5 Baud */
dc_set_baud(134);
}
else if((aux1 & 0x0f) == 7)
{ /* 150 Baud */
dc_set_baud(150);
}
else if((aux1 & 0x0f) == 8)
{ /* 300 Baud */
dc_set_baud(300);
}
else if((aux1 & 0x0f) == 9)
{ /* 600 Baud */
dc_set_baud(600);
}
else if((aux1 & 0x0f) == 10)
{ /* 1200 Baud */
dc_set_baud(1200);
}
else if((aux1 & 0x0f) == 12)
{ /* 2400 Baud */
dc_set_baud(2400);
}
else if((aux1 & 0x0f) == 13)
{ /* 4800 Baud */
dc_set_baud(4800);
}
else if((aux1 & 0x0f) == 14)
{ /* 9600 Baud */
dc_set_baud(9600);
}
else if((aux1 & 0x0f) == 15)
{ /* 19200 Baud */
dc_set_baud(19200);
}
else
{ /* 115200 Baud (can add 38400, 76800 if wanted) */
dc_set_baud(115200);
}
if(aux1 & 0x40)
{ /* 230400 Baud */
dc_set_baud(230400);
}
#endif /* DREAMCAST */
}
#endif /* R_SERIAL */
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
}
/*---------------------------------------------------------------------------
Host Support Function - XIO 38 - Called from RDevice_SPEC
Sets Translation and parity
---------------------------------------------------------------------------*/
static void xio_38(void)
{
int aux1;
#if defined(R_SERIAL) && !defined(DREAMCAST)
struct termios options;
#endif /* defined(R_SERIAL) && !defined(DREAMCAST) */
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
aux1 = Peek(Devices_ICAX1Z);
#if defined(R_SERIAL) && !defined(DREAMCAST)
if(RDevice_serial_enabled)
{
if(aux1 & 0x04)
{ /*Odd Parity*/
tcgetattr(rdev_fd, &options);
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
tcsetattr(rdev_fd, TCSANOW, &options);
}
else if(aux1 & 0x08)
{ /*Even Parity*/
tcgetattr(rdev_fd, &options);
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
tcsetattr(rdev_fd, TCSANOW, &options);
}
else
{ /*No Parity*/
tcgetattr(rdev_fd, &options);
options.c_cflag &= ~PARENB;
tcsetattr(rdev_fd, TCSANOW, &options);
}
}
#endif /* defined(R_SERIAL) && !defined(DREAMCAST) */
if(aux1 & 0x20)
{ /* No Translation */
DBG_APRINT("R*: No ATASCII/ASCII TRANSLATION");
translation = 0;
}
else
{
translation = 1;
}
if(aux1 & 0x40)
{ /* Append line feed */
DBG_APRINT("R*: Append Line Feeds");
linefeeds = 1;
}
else
{
linefeeds = 0;
}
}
/*---------------------------------------------------------------------------
Host Support Function - XIO 40 - Called from RDevice_SPEC
Sets concurrent mode. Also checks for dropped carrier.
---------------------------------------------------------------------------*/
static void xio_40(void)
{
int aux1;
/*
if(connected == 0)
Poke(747,0);
*/
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
aux1 = Peek(Devices_ICAX1Z);
if(aux1 >= 12)
{
concurrent = 1;
DBG_APRINT("R*: Entering concurrent IO mode...");
}
else
{
concurrent = 0;
sprintf(MESSAGE, "R*: XIO 40, %d", aux1);
DBG_APRINT(MESSAGE);
}
}
/*---------------------------------------------------------------------------
Host Support Function - Internet Socket Open Connection
---------------------------------------------------------------------------*/
#ifdef R_NETWORK
static void open_connection(char * address, int port)
{
struct hostent *host;
#ifdef WIN32
static int winsock_started;
WSADATA wdata;
WORD ver = MAKEWORD(1,1);
int ret;
if (!winsock_started) {
ret = WSAStartup(ver, &wdata);
if (ret) {
printf("Error in WSAStartup,return code:%d\n", ret);
}
winsock_started = 1;
}
#endif /* WIN32 */
if((address != NULL) && (strlen(address) > 0))
{
close(rdev_fd);
close(sock);
do_once = 1;
connected = 1;
memset ( &peer_in, 0, sizeof ( struct sockaddr_in ) );
/*rdev_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);*/
rdev_fd = socket(AF_INET, SOCK_STREAM, 0);
#ifdef WIN32
ioctlsocket(rdev_fd, FIONBIO, &ioctlsocket_non_block);
#else
fcntl(rdev_fd, F_SETFL, O_NONBLOCK);
#endif
peer_in.sin_family = AF_INET;
if(inet_pton(AF_INET, address, &peer_in.sin_addr) == 0)
{ /* invalid address if zero */
if((peer_in.sin_addr.s_addr == -1) || (peer_in.sin_addr.s_addr == 0))
{
host = gethostbyname(address);
if(host != NULL)
{
sprintf(MESSAGE, "R*: Host = '%s'.", host->h_name);
DBG_APRINT(MESSAGE);
memcpy((caddr_t)&peer_in.sin_addr, host->h_addr_list[0], host->h_length);
}
else
{
perror("gethostbyname");
}
}
}
if(port > 0)
{
peer_in.sin_port = htons (port);
}
else
{ /* telnet port */
peer_in.sin_port = htons (23);
}
if(connect(rdev_fd, (struct sockaddr *)&peer_in, sizeof(peer_in)) < 0)
{
#ifdef DEBUG
sprintf(MESSAGE, "R*: connect: '%s'", strerror(errno));
DBG_APRINT(MESSAGE);
#endif
}
#ifndef WIN32
signal(SIGPIPE, catch_disconnect); /*Need to see if the other end disconnects...*/
signal(SIGHUP, catch_disconnect); /*Need to see if the other end disconnects...*/
#endif /* WIN32 */
sprintf(MESSAGE, "R*: Connecting to %s", address);
DBG_APRINT(MESSAGE);
#ifdef WIN32
ioctlsocket(rdev_fd, FIONBIO, &ioctlsocket_non_block);
#else
fcntl(rdev_fd, F_SETFL, O_NONBLOCK);
#endif /* WIN32 */
/* Telnet negotiation */
sprintf(MESSAGE, "%c%c%c%c%c%c%c%c%c", 0xff, 0xfb, 0x01, 0xff, 0xfb, 0x03, 0xff, 0xfd, 0x0f3);
write(rdev_fd, MESSAGE, 9);
DBG_APRINT("R*: Negotiating Terminal Options...");
}
}
#endif /* R_NETWORK */
#if defined(R_SERIAL) && !defined(DREAMCAST)
#ifdef __linux__
#define TTY_DEV_NAME "/dev/ttyS0" /* Linux */
#elif defined (__NetBSD__) && defined(__i386__)
#define TTY_DEV_NAME "/dev/tty00" /* NetBSD/x86 */
#elif defined (__FreeBSD__) && defined(__i386__)
#define TTY_DEV_NAME "/dev/ttyd1" /* FreeBSD/x86 */
#elif defined (__sun__)
#define TTY_DEV_NAME "/dev/ttya" /* Solaris */
#else
#error tty name unknown!
#endif
#endif /* defined(R_SERIAL) && !defined(DREAMCAST) */
/*---------------------------------------------------------------------------
Host Support Function - Serial Open Connection
---------------------------------------------------------------------------*/
#ifdef R_SERIAL
static void open_connection_serial(int port)
{
#ifdef DREAMCAST
dc_init_serial();
connected = 1;
#else /* above DREAMCAST, below not */
char dev_name[FILENAME_MAX] = TTY_DEV_NAME; /* reinitialize each time */
struct termios options;
if(connected)
close(rdev_fd);
do_once = 1;
if (*RDevice_serial_device) /* got a device name from command line */
{
strcpy(dev_name, RDevice_serial_device);
}
dev_name[strlen(dev_name) - 1] += port - 1;
sprintf(MESSAGE, "R*: using serial device %s", dev_name);
DBG_APRINT(MESSAGE);
rdev_fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
if(rdev_fd == -1)
{
connected = 0;
perror("R*: open_port: Unable to open serial Port - ");
}
else
{
connected = 1;
#if 0
fcntl(rdev_fd, F_SETFL, O_NONBLOCK);
#endif
/*Set 8N1 by default on open*/
/*Set Baud to 115200 by default;*/
tcgetattr(rdev_fd, &options);
options.c_lflag = 0;
options.c_iflag = 0;
options.c_oflag = 0;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
options.c_cflag = CS8 | CREAD | CLOCAL;
#if 0
cfmakeraw(&options);
/*#else*/
/* from glibc 2.3.2 */
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
options.c_cflag &= ~(CSIZE|PARENB);
options.c_cflag |= CS8;
options.c_cc[VMIN] = 1; /* read returns when one char is available. */
options.c_cc[VTIME] = 0;
#endif
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(rdev_fd, TCSANOW, &options);
}
#endif /* not DREAMCAST */
}
#endif /* R_SERIAL */
/*---------------------------------------------------------------------------
Host Support Function - Retrieve address from open command:
From Basic: OPEN #1,8,23,"R:JYBOLAC.HOMELINUX.COM"
Returns: "jybolac.homelinux.com"
---------------------------------------------------------------------------*/
#ifdef R_NETWORK
static void RDevice_GetInetAddress(void)
{
UWORD bufadr = Devices_SkipDeviceName();
char *p;
if (bufadr == 0) {
/* XXX: signal error */
inetaddress[0] = '\0';
return;
}
p = inetaddress;
while (p < inetaddress + sizeof(inetaddress) - 1) {
char c = (char) MEMORY_dGetByte(bufadr);
if (c < '!' || c > '\x7e')
break;
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
*p++ = c;
bufadr++;
}
*p = '\0';
}
#endif /* R_NETWORK */
/*---------------------------------------------------------------------------
R Device OPEN vector - called from Atari OS Device Handler Address Table
---------------------------------------------------------------------------*/
void RDevice_OPEN(void)
{
int port;
int direction;
int devnum;
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
bufend = 0;
port = Peek(Devices_ICAX2Z);
direction = Peek(Devices_ICAX1Z);
devnum = MEMORY_dGetByte(Devices_ICDNOZ);
if(direction & 0x04)
{
DBG_APRINT("R*: Open for Reading...");
}
if(direction & 0x08)
{
DBG_APRINT("R*: Open for Writing...");
#ifdef R_SERIAL
if(RDevice_serial_enabled)
{
DBG_APRINT("R*: serial mode.");
open_connection_serial(devnum);
}
#endif /* R_SERIAL */
#if defined(R_SERIAL) && defined(R_NETWORK)
else
#endif
#ifdef R_NETWORK
{
DBG_APRINT("R*: Socket mode.");
RDevice_GetInetAddress();
open_connection(inetaddress, port);
}
#endif /* R_NETWORK */
}
if(direction & 0x01)
{
/* Open for concurrent mode */
}
}
/*---------------------------------------------------------------------------
R Device CLOSE vector - called from Atari OS Device Handler Address Table
---------------------------------------------------------------------------*/
void RDevice_CLOS(void)
{
CPU_regA = 1;
CPU_regY = 1;
CPU_ClrN;
concurrent = 0;
bufend = 0;
close(rdev_fd);
}
/*---------------------------------------------------------------------------
R Device READ vector - called from Atari OS Device Handler Address Table
---------------------------------------------------------------------------*/
void RDevice_READ(void)
{
int j;