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 2 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
18 /**************************************************************************/
19 /* Functions to read and write characters or groups of characters */
20 /* down sockets or pipes. Those that return a value return -1 on failure */
21 /* and 0 on success. */
22 /**************************************************************************/
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
31 /* The socket to the debugger */
34 /* The maximum number of file descriptors */
37 /* The socket handle */
38 extern int sockethandle;
40 /* Read and Write routines down a pipe or socket */
42 /****************************************************************/
43 /* Read an individual character. */
44 /* All other read functions rely on this one. */
45 /* It waits 15 seconds until there is a character available: if */
46 /* no character is available, then it timeouts and returns -1. */
47 /****************************************************************/
49 MYread_char (int sock, unsigned char *c)
53 struct timeval timeout = { 15, 0 };
54 struct sockaddr_in isa;
59 FD_SET (sock, &readfds);
61 i = select (nfds, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
71 fprintf (stderr, "read: Timeout\n");
75 if ((i = read (sock, c, 1)) < 1)
77 if (!i && sock == debugsock)
79 fprintf (stderr, "Connection with debugger severed.\n");
80 /* This shouldn't be necessary for a detached armulator, but
81 the armulator cannot be cold started a second time, so
82 this is probably preferable to locking up. */
84 fprintf (stderr, "Waiting for connection from debugger...");
85 debugsock = accept (sockethandle, &isa, &i);
87 { /* Now we are in serious trouble... */
91 fprintf (stderr, " done.\nConnection Established.\n");
100 if (sock == debugsock)
101 fprintf (stderr, "<%02x ", *c);
107 /****************************************************************/
108 /* Read an individual character. */
109 /* It waits until there is a character available. Returns -1 if */
110 /* an error occurs. */
111 /****************************************************************/
113 MYread_charwait (int sock, unsigned char *c)
117 struct sockaddr_in isa;
122 FD_SET (sock, &readfds);
124 i = select (nfds, &readfds,
125 (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
133 if ((i = read (sock, c, 1)) < 1)
135 if (!i && sock == debugsock)
137 fprintf (stderr, "Connection with debugger severed.\n");
139 fprintf (stderr, "Waiting for connection from debugger...");
140 debugsock = accept (sockethandle, &isa, &i);
142 { /* Now we are in serious trouble... */
146 fprintf (stderr, " done.\nConnection Established.\n");
155 if (sock == debugsock)
156 fprintf (stderr, "<%02x ", *c);
163 MYwrite_char (int sock, unsigned char c)
166 if (write (sock, &c, 1) < 1)
169 if (sock == debugsock)
170 fprintf (stderr, ">%02x ", c);
175 MYread_word (int sock, ARMword * here)
177 unsigned char a, b, c, d;
179 if (MYread_char (sock, &a) < 0)
181 if (MYread_char (sock, &b) < 0)
183 if (MYread_char (sock, &c) < 0)
185 if (MYread_char (sock, &d) < 0)
187 *here = a | b << 8 | c << 16 | d << 24;
192 MYwrite_word (int sock, ARMword i)
194 MYwrite_char (sock, i & 0xff);
195 MYwrite_char (sock, (i & 0xff00) >> 8);
196 MYwrite_char (sock, (i & 0xff0000) >> 16);
197 MYwrite_char (sock, (i & 0xff000000) >> 24);
201 MYwrite_string (int sock, char *s)
204 for (i = 0; MYwrite_char (sock, s[i]), s[i]; i++);
208 MYread_FPword (int sock, char *putinhere)
211 for (i = 0; i < 16; i++)
212 if (MYread_char (sock, &putinhere[i]) < 0)
218 MYwrite_FPword (int sock, char *fromhere)
221 for (i = 0; i < 16; i++)
222 MYwrite_char (sock, fromhere[i]);
225 /* Takes n bytes from source and those n bytes */
228 passon (int source, int dest, int n)
233 p = (char *) malloc (n);
236 perror ("Out of memory\n");
241 for (i = 0; i < n; i++)
242 if (MYread_char (source, &p[i]) < 0)
246 if (dest == debugsock)
247 for (i = 0; i < n; i++)
248 fprintf (stderr, ")%02x ", (unsigned char) p[i]);