Add support to count the number of instructions issued.
[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 <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
37
38 /* Structures used by the simulator, for gdb just have static structures */
39
40 static psim *simulator;
41 static int nr_cpus;
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 *buf = (char *)alloca (strlen (args) + 1);
55     char *p;
56     strcpy (buf, args);
57
58     p = strtok (args, " \t");
59     while (p != (char *)0) {
60       if (*p != '-')
61         error ("Argument is not an option '%s'", p);
62
63       else {
64         /* check arguments -- note, main.c also contains argument processing
65            code for the standalone emulator.  */
66         while (*++p != '\0') {
67           switch (*p) {
68           default:
69             error ("Usage: target sim [ -a -p -c -C -s -i -I -t ]\n");
70             break;
71           case 'a':
72             for (i = 0; i < nr_trace; i++)
73               trace[i] = 1;
74             break;
75           case 'p':
76             trace[trace_cpu] = trace[trace_semantics] = 1;
77             break;
78           case 'c':
79             trace[trace_core] = 1;
80             break;
81           case 'C':
82             trace[trace_console_device] = 1;
83             break;
84           case 's':
85             trace[trace_create_stack] = 1;
86             break;
87           case 'i':
88             trace[trace_icu_device] = 1;
89             break;
90           case 'I':
91             print_info = 1;
92             break;
93           case 't':
94             trace[trace_device_tree] = 1;
95             break;
96           }
97         }
98       }
99
100       p = strtok ((char *)0, " \t");
101     }
102   }
103
104   /* do something */
105   TRACE(trace_tbd, ("sim_open() - TBD - should parse the arguments\n"));
106   TRACE(trace_tbd, ("sim_open() - TBD - can not create simulator here as do not have description of it\n"));
107 }
108
109
110 void
111 sim_close (int quitting)
112 {
113   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
114   if (print_info)
115     psim_print_info (simulator, 1);
116
117   /* nothing to do */
118 }
119
120
121 int
122 sim_load (char *prog, int from_tty)
123 {
124   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
125                     prog, from_tty));
126
127   /* sanity check */
128   if (prog == NULL) {
129     error ("sim_load() - TBD - read stan shebs e-mail about how to find the program name?\n");
130     return -1;
131   }
132   TRACE(trace_tbd, ("sim_load() - TBD - parse that prog stripping things like quotes\n"));
133
134   /* create the simulator */
135   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
136   nr_cpus = (WITH_SMP ? WITH_SMP : 1);
137   simulator = psim_create(prog, nr_cpus);
138
139   /* bring in all the data section */
140   psim_load(simulator);
141
142   return 0;
143 }
144
145
146 void
147 sim_kill (void)
148 {
149   TRACE(trace_gdb, ("sim_kill(void) called\n"));
150   /* do nothing, nothing to do */
151 }
152
153
154 int
155 sim_read (SIM_ADDR mem, unsigned char *buf, int length)
156 {
157   return psim_read_memory(simulator, nr_cpus, buf, mem, length,
158                           raw_transfer);
159 }
160
161
162 int
163 sim_write (SIM_ADDR mem, unsigned char *buf, int length)
164 {
165   return psim_write_memory(simulator, nr_cpus, buf, mem, length,
166                            raw_transfer, 1/*violate_ro*/);
167 }
168
169
170 void
171 sim_fetch_register (int regno, unsigned char *buf)
172 {
173   if (simulator == NULL) {
174     return;
175   }
176
177   psim_read_register(simulator, nr_cpus, buf, register_names[regno],
178                      raw_transfer);
179 }
180
181
182 void
183 sim_store_register (int regno, unsigned char *buf)
184 {
185   if (simulator == NULL)
186     return;
187
188   psim_write_register(simulator, nr_cpus, buf, register_names[regno],
189                       raw_transfer);
190 }
191
192
193 void
194 sim_info (int verbose)
195 {
196   TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
197   psim_print_info (simulator, verbose);
198 }
199
200
201 void
202 sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
203 {
204   unsigned_word entry_point = start_address;
205
206   TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
207                     start_address));
208
209   psim_load(simulator);
210   psim_stack(simulator, argv, envp);
211
212   psim_write_register(simulator, -1 /* all start at same PC */,
213                       &entry_point, "pc", cooked_transfer);
214 }
215
216
217 static volatile int sim_should_run;
218
219 void
220 sim_stop_reason (enum sim_stop *reason, int *sigrc)
221 {
222   psim_status status = psim_get_status(simulator);
223
224   switch (CURRENT_ENVIRONMENT) {
225
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
261
262
263 /* Run (or resume) the program.  */
264 static void
265 sim_ctrl_c()
266 {
267   sim_should_run = 0;
268 }
269
270 void
271 sim_resume (int step, int siggnal)
272 {
273   void (*prev) ();
274   unsigned_word program_counter;
275
276   prev = signal(SIGINT, sim_ctrl_c);
277   sim_should_run = 1;
278
279   if (step)
280     psim_step(simulator);
281   else
282     psim_run_until_stop(simulator, &sim_should_run);
283
284   signal(SIGINT, prev);
285 }
286
287 void
288 sim_do_command(char *cmd)
289 {
290   TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", cmd));
291 }
292
293 /****/
294
295 void *
296 zalloc(long size)
297 {
298   void *memory = (void*)xmalloc(size);
299   if (memory == NULL)
300     error("xmalloc failed\n");
301   bzero(memory, size);
302   return memory;
303 }
304
305 void zfree(void *data)
306 {
307   mfree(NULL, data);
308 }