]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/sim_calls.c
New simulator changes from Andrew
[thirdparty/binutils-gdb.git] / sim / ppc / sim_calls.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <signal.h> /* FIXME - should be machine dependant version */
23 #include <stdarg.h>
24 #include <ctype.h>
25
26 #include "psim.h"
27 #include "options.h"
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #else
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39 #endif
40
41 #include "../../gdb/defs.h"
42
43 #include "../../gdb/remote-sim.h"
44 #include "../../gdb/callback.h"
45
46
47 /* Structures used by the simulator, for gdb just have static structures */
48
49 static psim *simulator;
50 static device *root_device;
51 static const char *register_names[] = REGISTER_NAMES;
52
53 void
54 sim_open (char *args)
55 {
56 /* Note: The simulation is not created by sim_open() because
57 complete information is not yet available */
58 /* trace the call */
59 TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
60
61 if (root_device != NULL)
62 printf_filtered("Warning - re-open of simulator leaks memory\n");
63 root_device = psim_tree();
64 simulator = NULL;
65
66 if (args) {
67 char **argv = buildargv(args);
68 psim_options(root_device, argv);
69 freeargv(argv);
70 }
71
72 if (ppc_trace[trace_opts])
73 print_options ();
74 }
75
76
77 void
78 sim_close (int quitting)
79 {
80 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
81 if (ppc_trace[trace_print_info] && simulator != NULL)
82 psim_print_info (simulator, ppc_trace[trace_print_info]);
83 }
84
85
86 int
87 sim_load (char *prog, int from_tty)
88 {
89 char **argv;
90 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
91 prog, from_tty));
92 ASSERT(prog != NULL);
93
94 /* parse the arguments, assume that the file is argument 0 */
95 argv = buildargv(prog);
96 ASSERT(argv != NULL && argv[0] != NULL);
97
98 /* create the simulator */
99 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
100 simulator = psim_create(argv[0], root_device);
101
102 /* bring in all the data section */
103 psim_init(simulator);
104
105 /* release the arguments */
106 freeargv(argv);
107
108 /* `I did it my way' */
109 return 0;
110 }
111
112
113 void
114 sim_kill (void)
115 {
116 TRACE(trace_gdb, ("sim_kill(void) called\n"));
117 /* do nothing, nothing to do */
118 }
119
120
121 int
122 sim_read (SIM_ADDR mem, unsigned char *buf, int length)
123 {
124 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
125 buf, mem, length);
126 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
127 (long)mem, (long)buf, length, result));
128 return result;
129 }
130
131
132 int
133 sim_write (SIM_ADDR mem, unsigned char *buf, int length)
134 {
135 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
136 buf, mem, length,
137 1/*violate_ro*/);
138 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
139 (long)mem, (long)buf, length, result));
140 return result;
141 }
142
143
144 void
145 sim_fetch_register (int regno, unsigned char *buf)
146 {
147 if (simulator == NULL) {
148 return;
149 }
150 TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
151 regno, register_names[regno], (long)buf));
152 psim_read_register(simulator, MAX_NR_PROCESSORS,
153 buf, register_names[regno],
154 raw_transfer);
155 }
156
157
158 void
159 sim_store_register (int regno, unsigned char *buf)
160 {
161 if (simulator == NULL)
162 return;
163 TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
164 regno, register_names[regno], (long)buf));
165 psim_write_register(simulator, MAX_NR_PROCESSORS,
166 buf, register_names[regno],
167 raw_transfer);
168 }
169
170
171 void
172 sim_info (int verbose)
173 {
174 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
175 psim_print_info (simulator, verbose);
176 }
177
178
179 void
180 sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
181 {
182 unsigned_word entry_point = start_address;
183
184 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
185 start_address));
186
187 psim_init(simulator);
188 psim_stack(simulator, argv, envp);
189
190 psim_write_register(simulator, -1 /* all start at same PC */,
191 &entry_point, "pc", cooked_transfer);
192 }
193
194
195 static volatile int sim_should_run;
196
197 void
198 sim_stop_reason (enum sim_stop *reason, int *sigrc)
199 {
200 psim_status status = psim_get_status(simulator);
201
202 switch (CURRENT_ENVIRONMENT) {
203
204 case USER_ENVIRONMENT:
205 case VIRTUAL_ENVIRONMENT:
206 switch (status.reason) {
207 case was_continuing:
208 *reason = sim_stopped;
209 *sigrc = SIGTRAP;
210 if (sim_should_run) {
211 error("sim_stop_reason() unknown reason for halt\n");
212 }
213 break;
214 case was_trap:
215 *reason = sim_stopped;
216 *sigrc = SIGTRAP;
217 break;
218 case was_exited:
219 *reason = sim_exited;
220 *sigrc = 0;
221 break;
222 case was_signalled:
223 *reason = sim_signalled;
224 *sigrc = status.signal;
225 break;
226 }
227 break;
228
229 case OPERATING_ENVIRONMENT:
230 *reason = sim_stopped;
231 *sigrc = SIGTRAP;
232 break;
233
234 default:
235 error("sim_stop_reason() - unknown environment\n");
236
237 }
238
239 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
240 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
241 }
242
243
244
245 /* Run (or resume) the program. */
246 static void
247 sim_ctrl_c()
248 {
249 sim_should_run = 0;
250 }
251
252 void
253 sim_resume (int step, int siggnal)
254 {
255 void (*prev) ();
256
257 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
258 step, siggnal));
259
260 prev = signal(SIGINT, sim_ctrl_c);
261 sim_should_run = 1;
262
263 if (step)
264 psim_step(simulator);
265 else
266 psim_run_until_stop(simulator, &sim_should_run);
267
268 signal(SIGINT, prev);
269 }
270
271 void
272 sim_do_command (char *cmd)
273 {
274 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
275 cmd ? cmd : "(null)"));
276 if (cmd) {
277 char **argv = buildargv(cmd);
278 psim_options(root_device, argv);
279 freeargv(argv);
280 }
281 }
282
283 void
284 sim_set_callbacks (host_callback *callback)
285 {
286 TRACE(trace_gdb, ("sim_set_callbacks called\n"));
287 }
288
289 /****/
290
291 void *
292 zalloc(long size)
293 {
294 void *memory = (void*)xmalloc(size);
295 if (memory == NULL)
296 error("xmalloc failed\n");
297 memset(memory, 0, size);
298 return memory;
299 }
300
301 void zfree(void *data)
302 {
303 mfree(NULL, data);
304 }
305
306 void flush_stdoutput(void)
307 {
308 gdb_flush (gdb_stdout);
309 }