1 /* main.c -- top level of ARMulator: 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /**********************************************************************/
19 /* Forks the ARMulator and hangs on a socket passing on RDP messages */
20 /* down a pipe to the ARMulator which translates them into RDI calls. */
21 /**********************************************************************/
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
36 #define MAXHOSTNAMELENGTH 64
38 /* Read and write routines down sockets and pipes */
40 void MYread_chars(int sock, void *p, int n);
41 unsigned char MYread_char(int sock);
42 ARMword MYread_word(int sock);
43 void MYread_FPword(int sock, char *putinhere);
45 void MYwrite_word(int sock, ARMword i);
46 void MYwrite_string(int sock, char *s);
47 void MYwrite_FPword(int sock, char *fromhere);
48 void MYwrite_char(int sock, unsigned char c);
50 void passon(int source, int dest, int n);
53 /* Mother and child processes */
57 /* The child process id. */
60 /* The socket to the debugger */
63 /* The pipes between the two processes */
67 /* A pipe for handling SWI return values that goes straight from the */
68 /* parent to the ARMulator host interface, bypassing the childs RDP */
69 /* to RDI interpreter */
72 /* The maximum number of file descriptors */
75 /* The socket handle */
78 /* The machine name */
79 char localhost[MAXHOSTNAMELENGTH + 1];
81 /* The socket number */
82 unsigned int socketnumber;
84 /**************************************************************/
85 /* Takes one argument: the socket number. */
86 /* Opens a socket to the debugger, and once opened spawns the */
87 /* ARMulator and sets up a couple of pipes. */
88 /**************************************************************/
89 int main(int argc, char *argv[]) {
91 struct sockaddr_in devil, isa;
96 fprintf(stderr, "No socket number\n");
100 sscanf(argv[1], "%d", &socketnumber);
101 if (!socketnumber || socketnumber > 0xffff) {
102 fprintf(stderr, "Invalid socket number: %d\n", socketnumber);
106 gethostname(localhost, MAXHOSTNAMELENGTH);
107 hp = gethostbyname(localhost);
109 fprintf(stderr, "Cannot get local host info\n");
114 sockethandle = socket(hp->h_addrtype, SOCK_STREAM, 0);
115 if (sockethandle < 0) {
120 devil.sin_family = hp->h_addrtype;
121 devil.sin_port = htons(socketnumber);
122 devil.sin_addr.s_addr = 0;
123 for(i = 0; i < sizeof(devil.sin_zero); i++) devil.sin_zero[i] = '\000';
124 memcpy(&devil.sin_addr, hp->h_addr_list[0], hp->h_length);
126 if (bind(sockethandle, &devil, sizeof(devil)) < 0) {
131 /* May only accept one debugger at once */
133 if (listen(sockethandle, 0)) {
138 fprintf(stderr, "Waiting for connection from debugger...");
140 debugsock = accept(sockethandle, &isa, &i);
146 fprintf(stderr, " done.\nConnection Established.\n");
148 nfds = getdtablesize();
159 if (pipe(DebuggerARMul)) {
165 fprintf(stderr, "Created pipes ok\n");
171 fprintf(stderr, "fork() ok\n");
174 if (child == 0) kid ();
175 if (child != -1) parent ();