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