Add ABFD argument to sim_open call. Pass through to sim_config so
[external/binutils.git] / sim / m32r / sim-if.c
1 /* Main simulator entry points for the M32R.
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
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, or (at your option)
8 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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #include "sim-main.h"
20 #include <signal.h>
21 #ifdef HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #include "libiberty.h"
25 #include "bfd.h"
26 #include "sim-core.h"
27 #include "cpu-sim.h"
28
29 /* Global state until sim_open starts creating and returning it
30    [and the other simulator i/f fns take it as an argument].  */
31 struct sim_state sim_global_state;
32
33 /* FIXME: Do we *need* to pass state to the semantic routines?  */
34 STATE current_state;
35
36 /* Create an instance of the simulator.  */
37
38 SIM_DESC
39 sim_open (kind, callback, abfd, argv)
40      SIM_OPEN_KIND kind;
41      host_callback *callback;
42      struct _bfd *abfd;
43      char **argv;
44 {
45   int i;
46   SIM_DESC sd = &sim_global_state;
47
48   /* FIXME: until we alloc one, use the global.  */
49   memset (sd, 0, sizeof (sim_global_state));
50   STATE_OPEN_KIND (sd) = kind;
51   STATE_CALLBACK (sd) = callback;
52
53   if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
54     return 0;
55
56 #if 0 /* FIXME: 'twould be nice if we could do this */
57   /* These options override any module options.
58      Obviously ambiguity should be avoided, however the caller may wish to
59      augment the meaning of an option.  */
60   if (extra_options != NULL)
61     sim_add_option_table (sd, extra_options);
62 #endif
63
64   /* getopt will print the error message so we just have to exit if this fails.
65      FIXME: Hmmm...  in the case of gdb we need getopt to call
66      print_filtered.  */
67   if (sim_parse_args (sd, argv) != SIM_RC_OK)
68     {
69       sim_module_uninstall (sd);
70       return 0;
71     }
72
73   if (sim_post_argv_init (sd) != SIM_RC_OK)
74     {
75       sim_module_uninstall (sd);
76       return 0;
77     }
78
79   /* Initialize various cgen things not done by common framework.  */
80   cgen_init (sd);
81
82   /* FIXME:wip */
83   sim_core_attach (sd, NULL, attach_raw_memory, access_read_write_exec,
84                    0, 0, M32R_DEFAULT_MEM_SIZE, NULL, NULL);
85
86   /* Only needed for profiling, but the structure member is small.  */
87   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
88     memset (& CPU_M32R_PROFILE (STATE_CPU (sd, i)), 0,
89             sizeof (CPU_M32R_PROFILE (STATE_CPU (sd, i))));
90
91   return &sim_global_state;
92 }
93
94 void
95 sim_close (sd, quitting)
96      SIM_DESC sd;
97      int quitting;
98 {
99   sim_module_uninstall (sd);
100 }
101
102 SIM_RC
103 sim_load (sd, prog, abfd, from_tty)
104      SIM_DESC sd;
105      char *prog;
106      bfd *abfd;
107      int from_tty;
108 {
109   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
110   bfd *prog_bfd;
111
112   prog_bfd = sim_load_file (sd, STATE_MY_NAME (sd),
113                             STATE_CALLBACK (sd),
114                             prog,
115                             /* pass NULL for abfd, we always open our own */
116                             NULL,
117                             STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG);
118   if (prog_bfd == NULL)
119     return SIM_RC_FAIL;
120   sim_analyze_program (sd, prog_bfd);
121   STATE_CPU_CPU (sd, 0)->pc = STATE_START_ADDR (sd);
122   return SIM_RC_OK;
123
124
125 SIM_RC
126 sim_create_inferior (sd, argv, envp)
127      SIM_DESC sd;
128      char **argv;
129      char **envp;
130 {
131 #if 0
132   STATE_ARGV (sd) = sim_copy_argv (argv);
133   STATE_ENVP (sd) = sim_copy_argv (envp);
134 #endif
135   return SIM_RC_OK;
136 }
137
138 void
139 sim_kill (sd)
140      SIM_DESC sd;
141 {
142   /* nothing to do */
143 }
144
145 int
146 sim_stop (SIM_DESC sd)
147 {
148   return engine_stop (sd);
149 }
150
151 void
152 sim_resume (sd, step, siggnal)
153      SIM_DESC sd;
154      int step, siggnal;
155 {
156   engine_run (sd, step, siggnal);
157 }
158
159 void
160 sim_stop_reason (sd, reason, sigrc)
161      SIM_DESC sd;
162      enum sim_stop *reason;
163      int *sigrc;
164 {
165   sim_cpu *cpu = STATE_CPU (sd, 0);
166
167   /* Map sim_state to sim_stop.  */
168   switch (CPU_EXEC_STATE (cpu))
169     {
170     case EXEC_STATE_EXITED :
171       *reason = sim_exited;
172       *sigrc = CPU_HALT_SIGRC (cpu);
173       break;
174     case EXEC_STATE_STOPPED :
175       *reason = sim_stopped;
176       *sigrc = sim_signal_to_host (CPU_HALT_SIGRC (cpu));
177       break;
178     case EXEC_STATE_SIGNALLED :
179       *reason = sim_signalled;
180       *sigrc = sim_signal_to_host (CPU_HALT_SIGRC (cpu));
181       break;
182     }
183 }
184
185 /* PROFILE_CPU_CALLBACK */
186
187 static void
188 print_m32r_misc_cpu (SIM_CPU *cpu, int verbose)
189 {
190   SIM_DESC sd = CPU_STATE (cpu);
191   char buf[20];
192
193   if (CPU_PROFILE_FLAGS (cpu) [PROFILE_INSN_IDX])
194     {
195       sim_io_printf (sd, "Miscellaneous Statistics\n\n");
196       sim_io_printf (sd, "  %-*s %s\n\n",
197                      PROFILE_LABEL_WIDTH, "Fill nops:",
198                      sim_add_commas (buf, sizeof (buf),
199                                      CPU_M32R_PROFILE (cpu).fillnop_count));
200     }
201 }
202
203 void
204 sim_info (sd, verbose)
205      SIM_DESC sd;
206      int verbose;
207 {
208   profile_print (sd, STATE_VERBOSE_P (sd), NULL, print_m32r_misc_cpu);
209 }
210
211 /* The contents of BUF are in target byte order.  */
212
213 void
214 sim_fetch_register (sd, rn, buf)
215      SIM_DESC sd;
216      int rn;
217      unsigned char *buf;
218 {
219   if (rn < 16)
220     SETTWI (buf, STATE_CPU_CPU (sd, 0)->h_gr[rn]);
221   else if (rn < 21)
222     SETTWI (buf, STATE_CPU_CPU (sd, 0)->h_cr[rn - 16]);
223   else switch (rn) {
224     case PC_REGNUM:
225       SETTWI (buf, STATE_CPU_CPU (sd, 0)->pc);
226       break;
227     case ACCL_REGNUM:
228       SETTWI (buf, GETLODI (STATE_CPU_CPU (sd, 0)->h_accum));
229       break;
230     case ACCH_REGNUM:
231       SETTWI (buf, GETHIDI (STATE_CPU_CPU (sd, 0)->h_accum));
232       break;
233 #if 0
234     case 23: *reg = STATE_CPU_CPU (sd, 0)->h_cond;              break;
235     case 24: *reg = STATE_CPU_CPU (sd, 0)->h_sm;                break;
236     case 25: *reg = STATE_CPU_CPU (sd, 0)->h_bsm;               break;
237     case 26: *reg = STATE_CPU_CPU (sd, 0)->h_ie;                break;
238     case 27: *reg = STATE_CPU_CPU (sd, 0)->h_bie;               break;
239     case 28: *reg = STATE_CPU_CPU (sd, 0)->h_bcarry;            break; /* rename: bc */
240     case 29: memcpy (buf, &STATE_CPU_CPU (sd, 0)->h_bpc, sizeof(WI));   break; /* duplicate */
241 #endif
242     default: abort ();
243   }
244 }
245  
246 /* The contents of BUF are in target byte order.  */
247
248 void
249 sim_store_register (sd, rn, buf)
250      SIM_DESC sd;
251      int rn;
252      unsigned char *buf;
253 {
254   if (rn < 16)
255     STATE_CPU_CPU (sd, 0)->h_gr[rn] = GETTWI (buf);
256   else if (rn < 21)
257     STATE_CPU_CPU (sd, 0)->h_cr[rn - 16] = GETTWI (buf);
258   else switch (rn) {
259     case PC_REGNUM:
260       STATE_CPU_CPU (sd, 0)->pc = GETTWI (buf);
261       break;
262     case ACCL_REGNUM:
263       SETLODI (STATE_CPU_CPU (sd, 0)->h_accum, GETTWI (buf));
264       break;
265     case ACCH_REGNUM:
266       SETHIDI (STATE_CPU_CPU (sd, 0)->h_accum, GETTWI (buf));
267       break;
268 #if 0
269     case 23: STATE_CPU_CPU (sd, 0)->h_cond   = *reg;                    break;
270     case 24: STATE_CPU_CPU (sd, 0)->h_sm     = *reg;                    break;
271     case 25: STATE_CPU_CPU (sd, 0)->h_bsm    = *reg;                    break;
272     case 26: STATE_CPU_CPU (sd, 0)->h_ie     = *reg;                    break;
273     case 27: STATE_CPU_CPU (sd, 0)->h_bie    = *reg;                    break;
274     case 28: STATE_CPU_CPU (sd, 0)->h_bcarry = *reg;                    break; /* rename: bc */
275     case 29: memcpy (&STATE_CPU_CPU (sd, 0)->h_bpc, buf, sizeof(DI));   break; /* duplicate */
276 #endif
277   }
278 }
279
280 int
281 sim_read (sd, addr, buf, len)
282      SIM_DESC sd;
283      SIM_ADDR addr;
284      unsigned char *buf;
285      int len;
286 {
287 #if 1
288   return sim_core_read_buffer (sd, NULL, sim_core_read_map,
289                                 buf, addr, len);
290 #else
291   return (*STATE_MEM_READ (sd)) (sd, addr, buf, len);
292 #endif
293
294
295 int
296 sim_write (sd, addr, buf, len)
297      SIM_DESC sd;
298      SIM_ADDR addr;
299      unsigned char *buf;
300      int len;
301 {
302 #if 1
303   return sim_core_write_buffer (sd, NULL, sim_core_write_map,
304                                 buf, addr, len);
305 #else
306   return (*STATE_MEM_WRITE (sd)) (sd, addr, buf, len);
307 #endif
308 }
309
310 void
311 sim_do_command (sd, cmd)
312      SIM_DESC sd;
313      char *cmd;
314
315   sim_io_error (sd, "sim_do_command - unimplemented");
316 }