1 /* communicate.c -- ARMulator RDP comms code: ARM6 Instruction Emulator.
2 Copyright (C) 1994 Advanced RISC Machines Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17 /**************************************************************************/
18 /* Functions to read and write characters or groups of characters */
19 /* down sockets or pipes. Those that return a value return -1 on failure */
20 /* and 0 on success. */
21 /**************************************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
30 /* The socket to the debugger */
33 /* The maximum number of file descriptors */
36 /* The socket handle */
37 extern int sockethandle;
39 /* Read and Write routines down a pipe or socket */
41 /****************************************************************/
42 /* Read an individual character. */
43 /* All other read functions rely on this one. */
44 /* It waits 15 seconds until there is a character available: if */
45 /* no character is available, then it timeouts and returns -1. */
46 /****************************************************************/
48 MYread_char (int sock, unsigned char *c)
52 struct timeval timeout = { 15, 0 };
53 struct sockaddr_in isa;
58 FD_SET (sock, &readfds);
60 i = select (nfds, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
70 fprintf (stderr, "read: Timeout\n");
74 if ((i = read (sock, c, 1)) < 1)
76 if (!i && sock == debugsock)
78 fprintf (stderr, "Connection with debugger severed.\n");
79 /* This shouldn't be necessary for a detached armulator, but
80 the armulator cannot be cold started a second time, so
81 this is probably preferable to locking up. */
83 fprintf (stderr, "Waiting for connection from debugger...");
84 debugsock = accept (sockethandle, &isa, &i);
86 { /* Now we are in serious trouble... */
90 fprintf (stderr, " done.\nConnection Established.\n");
99 if (sock == debugsock)
100 fprintf (stderr, "<%02x ", *c);
106 /****************************************************************/
107 /* Read an individual character. */
108 /* It waits until there is a character available. Returns -1 if */
109 /* an error occurs. */
110 /****************************************************************/
112 MYread_charwait (int sock, unsigned char *c)
116 struct sockaddr_in isa;
121 FD_SET (sock, &readfds);
123 i = select (nfds, &readfds,
124 (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
132 if ((i = read (sock, c, 1)) < 1)
134 if (!i && sock == debugsock)
136 fprintf (stderr, "Connection with debugger severed.\n");
138 fprintf (stderr, "Waiting for connection from debugger...");
139 debugsock = accept (sockethandle, &isa, &i);
141 { /* Now we are in serious trouble... */
145 fprintf (stderr, " done.\nConnection Established.\n");
154 if (sock == debugsock)
155 fprintf (stderr, "<%02x ", *c);
162 MYwrite_char (int sock, unsigned char c)
165 if (write (sock, &c, 1) < 1)
168 if (sock == debugsock)
169 fprintf (stderr, ">%02x ", c);
174 MYread_word (int sock, ARMword * here)
176 unsigned char a, b, c, d;
178 if (MYread_char (sock, &a) < 0)
180 if (MYread_char (sock, &b) < 0)
182 if (MYread_char (sock, &c) < 0)
184 if (MYread_char (sock, &d) < 0)
186 *here = a | b << 8 | c << 16 | d << 24;
191 MYwrite_word (int sock, ARMword i)
193 MYwrite_char (sock, i & 0xff);
194 MYwrite_char (sock, (i & 0xff00) >> 8);
195 MYwrite_char (sock, (i & 0xff0000) >> 16);
196 MYwrite_char (sock, (i & 0xff000000) >> 24);
200 MYwrite_string (int sock, char *s)
203 for (i = 0; MYwrite_char (sock, s[i]), s[i]; i++);
207 MYread_FPword (int sock, char *putinhere)
210 for (i = 0; i < 16; i++)
211 if (MYread_char (sock, &putinhere[i]) < 0)
217 MYwrite_FPword (int sock, char *fromhere)
220 for (i = 0; i < 16; i++)
221 MYwrite_char (sock, fromhere[i]);
224 /* Takes n bytes from source and those n bytes */
227 passon (int source, int dest, int n)
232 p = (char *) malloc (n);
235 perror ("Out of memory\n");
240 for (i = 0; i < n; i++)
241 if (MYread_char (source, &p[i]) < 0)
245 if (dest == debugsock)
246 for (i = 0; i < n; i++)
247 fprintf (stderr, ")%02x ", (unsigned char) p[i]);