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