Andrew's latest changes & print all instruction counts if -I
[deliverable/binutils-gdb.git] / sim / ppc / sim_calls.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, 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 <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include "basics.h"
29 #include "psim.h"
30
31 #include "../../gdb/defs.h"
32
33 #include "devices.h"
34
35 #include "../../gdb/remote-sim.h"
36 #include "../../gdb/callback.h"
37
38
39 /* Structures used by the simulator, for gdb just have static structures */
40
41 static psim *simulator;
42 static char *register_names[] = REGISTER_NAMES;
43 static int print_info = 0;
44
45 void
46 sim_open (char *args)
47 {
48 int i;
49
50 /* trace the call */
51 TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
52
53 if (args) {
54 char **argv = buildargv(args);
55 int argp = 0;
56 int argc;
57 for (argc = 0; argv[argc]; argc++);
58
59 while (argp < argc) {
60 if (*argv[argp] != '-')
61 error ("Argument is not an option '%s'", argv[argp]);
62
63 else {
64 /* check arguments -- note, main.c also contains argument processing
65 code for the standalone emulator. */
66 char *p = argv[argp] + 1;
67 while (*p != '\0') {
68 switch (*p) {
69 default:
70 printf_filtered("Usage:\n\ttarget sim [ -t <trace-option> ]\n");
71 trace_usage();
72 error ("");
73 break;
74 case 't':
75 argp += 1;
76 if (argv[argp] == NULL)
77 error("Missing <trace> option for -t\n");
78 trace_option(argv[argp]); /* better fail if NULL */
79 break;
80 case 'I':
81 print_info = 1;
82 break;
83 }
84 }
85 }
86 argp += 1;
87 }
88 }
89
90 /* do something */
91 TRACE(trace_tbd, ("sim_open() - TBD - should parse the arguments\n"));
92 TRACE(trace_tbd, ("sim_open() - TBD - can not create simulator here as do not have description of it\n"));
93 }
94
95
96 void
97 sim_close (int quitting)
98 {
99 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
100 if (print_info)
101 psim_print_info (simulator, 1);
102
103 /* nothing to do */
104 }
105
106
107 int
108 sim_load (char *prog, int from_tty)
109 {
110 char **argv;
111 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
112 prog, from_tty));
113 ASSERT(prog != NULL);
114
115 /* parse the arguments, assume that the file is argument 0 */
116 argv = buildargv(prog);
117 ASSERT(argv != NULL && argv[0] != NULL);
118
119 /* create the simulator */
120 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
121 simulator = psim_create(argv[0]);
122
123 /* bring in all the data section */
124 psim_init(simulator);
125
126 /* release the arguments */
127 freeargv(argv);
128
129 /* `I did it my way' */
130 return 0;
131 }
132
133
134 void
135 sim_kill (void)
136 {
137 TRACE(trace_gdb, ("sim_kill(void) called\n"));
138 /* do nothing, nothing to do */
139 }
140
141
142 int
143 sim_read (SIM_ADDR mem, unsigned char *buf, int length)
144 {
145 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
146 buf, mem, length);
147 TRACE(trace_gdb, ("sim_read(mem=0x%x, buf=0x%x, length=%d) = %d\n",
148 mem, buf, length, result));
149 return result;
150 }
151
152
153 int
154 sim_write (SIM_ADDR mem, unsigned char *buf, int length)
155 {
156 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
157 buf, mem, length,
158 1/*violate_ro*/);
159 TRACE(trace_gdb, ("sim_write(mem=0x%x, buf=0x%x, length=%d) = %d\n",
160 mem, buf, length, result));
161 return result;
162 }
163
164
165 void
166 sim_fetch_register (int regno, unsigned char *buf)
167 {
168 if (simulator == NULL) {
169 return;
170 }
171 TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%x)\n",
172 regno, register_names[regno], buf));
173 psim_read_register(simulator, MAX_NR_PROCESSORS,
174 buf, register_names[regno],
175 raw_transfer);
176 }
177
178
179 void
180 sim_store_register (int regno, unsigned char *buf)
181 {
182 if (simulator == NULL)
183 return;
184 TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%x)\n",
185 regno, register_names[regno], buf));
186 psim_write_register(simulator, MAX_NR_PROCESSORS,
187 buf, register_names[regno],
188 raw_transfer);
189 }
190
191
192 void
193 sim_info (int verbose)
194 {
195 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
196 psim_print_info (simulator, verbose);
197 }
198
199
200 void
201 sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
202 {
203 unsigned_word entry_point = start_address;
204
205 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
206 start_address));
207
208 psim_init(simulator);
209 psim_stack(simulator, argv, envp);
210
211 psim_write_register(simulator, -1 /* all start at same PC */,
212 &entry_point, "pc", cooked_transfer);
213 }
214
215
216 static volatile int sim_should_run;
217
218 void
219 sim_stop_reason (enum sim_stop *reason, int *sigrc)
220 {
221 psim_status status = psim_get_status(simulator);
222
223 switch (CURRENT_ENVIRONMENT) {
224
225 case USER_ENVIRONMENT:
226 case VIRTUAL_ENVIRONMENT:
227 switch (status.reason) {
228 case was_continuing:
229 *reason = sim_stopped;
230 *sigrc = SIGTRAP;
231 if (sim_should_run) {
232 error("sim_stop_reason() unknown reason for halt\n");
233 }
234 break;
235 case was_trap:
236 *reason = sim_stopped;
237 *sigrc = SIGTRAP;
238 break;
239 case was_exited:
240 *reason = sim_exited;
241 *sigrc = 0;
242 break;
243 case was_signalled:
244 *reason = sim_signalled;
245 *sigrc = status.signal;
246 break;
247 }
248 break;
249
250 case OPERATING_ENVIRONMENT:
251 *reason = sim_stopped;
252 *sigrc = SIGTRAP;
253 break;
254
255 default:
256 error("sim_stop_reason() - unknown environment\n");
257
258 }
259
260 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%x(%d), sigrc=0x%x(%d))\n",
261 reason, *reason, sigrc, *sigrc));
262 }
263
264
265
266 /* Run (or resume) the program. */
267 static void
268 sim_ctrl_c()
269 {
270 sim_should_run = 0;
271 }
272
273 void
274 sim_resume (int step, int siggnal)
275 {
276 void (*prev) ();
277 unsigned_word program_counter;
278
279 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
280 step, siggnal));
281
282 prev = signal(SIGINT, sim_ctrl_c);
283 sim_should_run = 1;
284
285 if (step)
286 psim_step(simulator);
287 else
288 psim_run_until_stop(simulator, &sim_should_run);
289
290 signal(SIGINT, prev);
291 }
292
293 void
294 sim_do_command(char *cmd)
295 {
296 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", cmd));
297 }
298
299 void
300 sim_set_callbacks (host_callback *callback)
301 {
302 TRACE(trace_gdb, ("sim_set_callbacks called\n"));
303 }
304
305 /****/
306
307 void *
308 zalloc(long size)
309 {
310 void *memory = (void*)xmalloc(size);
311 if (memory == NULL)
312 error("xmalloc failed\n");
313 bzero(memory, size);
314 return memory;
315 }
316
317 void zfree(void *data)
318 {
319 mfree(NULL, data);
320 }
This page took 0.043568 seconds and 4 git commands to generate.