Commit | Line | Data |
---|---|---|
d45a4bef JB |
1 | /* main.c --- main function for stand-alone M32C simulator. |
2 | ||
9b254dd1 | 3 | Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc. |
d45a4bef JB |
4 | Contributed by Red Hat, Inc. |
5 | ||
6 | This file is part of the GNU simulators. | |
7 | ||
4744ac1b JB |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
d45a4bef | 12 | |
4744ac1b JB |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
d45a4bef JB |
17 | |
18 | You should have received a copy of the GNU General Public License | |
4744ac1b | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d45a4bef JB |
20 | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <assert.h> | |
27 | #include <setjmp.h> | |
28 | #include <signal.h> | |
29 | ||
3877a145 DD |
30 | #include <sys/types.h> |
31 | #include <sys/socket.h> | |
32 | #include <netinet/in.h> | |
33 | #include <netinet/tcp.h> | |
34 | ||
35 | ||
d45a4bef JB |
36 | #include "bfd.h" |
37 | ||
38 | #include "cpu.h" | |
39 | #include "mem.h" | |
40 | #include "misc.h" | |
41 | #include "load.h" | |
42 | #include "trace.h" | |
3877a145 DD |
43 | #ifdef TIMER_A |
44 | #include "int.h" | |
45 | #include "timer_a.h" | |
46 | #endif | |
d45a4bef | 47 | |
3877a145 DD |
48 | extern int m32c_console_ofd; |
49 | extern int m32c_console_ifd; | |
50 | ||
51 | int m32c_disassemble = 0; | |
d45a4bef JB |
52 | static unsigned int cycles = 0; |
53 | ||
54 | static void | |
55 | done (int exit_code) | |
56 | { | |
57 | if (verbose) | |
58 | { | |
59 | stack_heap_stats (); | |
60 | mem_usage_stats (); | |
61 | printf ("insns: %14s\n", comma (cycles)); | |
62 | } | |
63 | exit (exit_code); | |
64 | } | |
65 | ||
3877a145 DD |
66 | static void |
67 | setup_tcp_console (char *portname) | |
68 | { | |
69 | int port = atoi (portname); | |
70 | struct sockaddr_in address; | |
71 | int isocket; | |
72 | socklen_t as; | |
73 | unsigned char *a; | |
74 | ||
75 | if (port < 1024) | |
76 | { | |
77 | printf ("invalid port number %d\n", port); | |
78 | exit (1); | |
79 | } | |
80 | printf ("waiting for tcp console on port %d\n", port); | |
81 | ||
82 | memset (&address, 0, sizeof (address)); | |
83 | address.sin_family = AF_INET; | |
84 | address.sin_port = htons (port); | |
85 | ||
86 | isocket = socket (AF_INET, SOCK_STREAM, 0); | |
87 | if (isocket < 0) | |
88 | { | |
89 | perror ("socket"); | |
90 | exit (1); | |
91 | } | |
92 | ||
93 | if (bind (isocket, (struct sockaddr *) &address, sizeof (address))) | |
94 | { | |
95 | perror ("bind"); | |
96 | exit (1); | |
97 | } | |
98 | listen (isocket, 2); | |
99 | ||
100 | printf ("waiting for connection...\n"); | |
101 | as = sizeof (address); | |
102 | m32c_console_ifd = accept (isocket, (struct sockaddr *) &address, &as); | |
103 | if (m32c_console_ifd == -1) | |
104 | { | |
105 | perror ("accept"); | |
106 | exit (1); | |
107 | } | |
108 | a = (unsigned char *) (&address.sin_addr.s_addr); | |
109 | printf ("connection from %d.%d.%d.%d\n", a[0], a[1], a[2], a[3]); | |
110 | m32c_console_ofd = m32c_console_ifd; | |
111 | } | |
112 | ||
d45a4bef JB |
113 | int |
114 | main (int argc, char **argv) | |
115 | { | |
116 | int o; | |
117 | int save_trace; | |
118 | bfd *prog; | |
3877a145 DD |
119 | char *console_port_s = 0; |
120 | ||
121 | setbuf (stdout, 0); | |
122 | ||
123 | in_gdb = 0; | |
d45a4bef | 124 | |
3877a145 | 125 | while ((o = getopt (argc, argv, "tc:vdm:")) != -1) |
d45a4bef JB |
126 | switch (o) |
127 | { | |
128 | case 't': | |
129 | trace++; | |
130 | break; | |
3877a145 DD |
131 | case 'c': |
132 | console_port_s = optarg; | |
133 | break; | |
d45a4bef JB |
134 | case 'v': |
135 | verbose++; | |
136 | break; | |
137 | case 'd': | |
3877a145 | 138 | m32c_disassemble++; |
d45a4bef JB |
139 | break; |
140 | case 'm': | |
141 | if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0) | |
142 | default_machine = bfd_mach_m16c; | |
143 | else if (strcmp (optarg, "m32cm") == 0 | |
144 | || strcmp (optarg, "m32c") == 0) | |
145 | default_machine = bfd_mach_m32c; | |
146 | else | |
147 | { | |
148 | fprintf (stderr, "Invalid machine: %s\n", optarg); | |
149 | exit (1); | |
150 | } | |
151 | break; | |
152 | case '?': | |
153 | fprintf (stderr, | |
3877a145 DD |
154 | "usage: run [-v] [-t] [-d] [-m r8c|m16c|m32cm|m32c]" |
155 | " program\n"); | |
d45a4bef JB |
156 | exit (1); |
157 | } | |
158 | ||
159 | prog = bfd_openr (argv[optind], 0); | |
160 | if (!prog) | |
161 | { | |
162 | fprintf (stderr, "Can't read %s\n", argv[optind]); | |
163 | exit (1); | |
164 | } | |
165 | ||
166 | if (!bfd_check_format (prog, bfd_object)) | |
167 | { | |
168 | fprintf (stderr, "%s not a m32c program\n", argv[optind]); | |
169 | exit (1); | |
170 | } | |
171 | ||
172 | save_trace = trace; | |
173 | trace = 0; | |
174 | m32c_load (prog); | |
175 | trace = save_trace; | |
176 | ||
3877a145 DD |
177 | if (console_port_s) |
178 | setup_tcp_console (console_port_s); | |
179 | ||
180 | sim_disasm_init (prog); | |
d45a4bef JB |
181 | |
182 | while (1) | |
183 | { | |
184 | int rc; | |
185 | ||
186 | if (trace) | |
187 | printf ("\n"); | |
188 | ||
3877a145 | 189 | if (m32c_disassemble) |
d45a4bef JB |
190 | sim_disasm_one (); |
191 | ||
192 | enable_counting = verbose; | |
193 | cycles++; | |
194 | rc = decode_opcode (); | |
195 | enable_counting = 0; | |
196 | ||
197 | if (M32C_HIT_BREAK (rc)) | |
198 | done (1); | |
199 | else if (M32C_EXITED (rc)) | |
200 | done (M32C_EXIT_STATUS (rc)); | |
201 | else | |
202 | assert (M32C_STEPPED (rc)); | |
203 | ||
204 | trace_register_changes (); | |
3877a145 DD |
205 | |
206 | #ifdef TIMER_A | |
207 | update_timer_a (); | |
208 | #endif | |
d45a4bef JB |
209 | } |
210 | } |