1 /* Main simulator entry points for the M32R.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
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)
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.
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. */
23 #include "sim-options.h"
24 #include "libiberty.h"
26 #include "targ-vals.h"
28 static void free_state (SIM_DESC);
29 static void print_m32r_misc_cpu (SIM_CPU *cpu, int verbose);
31 /* Records simulator descriptor so utilities like m32r_dump_regs can be
33 SIM_DESC current_state;
35 /* Cover function of sim_state_free to free the cpu buffers as well. */
38 free_state (SIM_DESC sd)
40 if (STATE_MODULES (sd) != NULL)
41 sim_module_uninstall (sd);
42 sim_cpu_free_all (sd);
46 /* Create an instance of the simulator. */
49 sim_open (kind, callback, abfd, argv)
51 host_callback *callback;
55 SIM_DESC sd = sim_state_alloc (kind, callback);
57 /* The cpu data is kept in a separately allocated chunk of memory. */
58 if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
64 #if 0 /* FIXME: pc is in mach-specific struct */
65 /* FIXME: watchpoints code shouldn't need this */
67 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
68 STATE_WATCHPOINTS (sd)->pc = &(PC);
69 STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (PC);
73 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
79 #if 0 /* FIXME: 'twould be nice if we could do this */
80 /* These options override any module options.
81 Obviously ambiguity should be avoided, however the caller may wish to
82 augment the meaning of an option. */
83 if (extra_options != NULL)
84 sim_add_option_table (sd, extra_options);
87 /* Allocate core managed memory */
88 sim_do_commandf (sd, "memory region 0,0x%lx", M32R_DEFAULT_MEM_SIZE);
90 /* Allocate a handler for the MSPR register. */
91 sim_core_attach (sd, NULL,
95 M32R_DEVICE_ADDR, M32R_DEVICE_LEN /*nr_bytes*/,
100 /* getopt will print the error message so we just have to exit if this fails.
101 FIXME: Hmmm... in the case of gdb we need getopt to call
103 if (sim_parse_args (sd, argv) != SIM_RC_OK)
109 /* check for/establish the reference program image */
110 if (sim_analyze_program (sd,
111 (STATE_PROG_ARGV (sd) != NULL
112 ? *STATE_PROG_ARGV (sd)
120 /* If both cpu model and state architecture are set, ensure they're
121 compatible. If only one is set, set the other. If neither are set,
122 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
123 for the selected "mach" (bfd terminology). */
125 SIM_CPU *cpu = STATE_CPU (sd, 0);
127 if (! STATE_ARCHITECTURE (sd)
128 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
131 /* Set the default model. */
132 const MODEL *model = sim_model_lookup (WITH_DEFAULT_MODEL);
133 sim_model_set (sd, NULL, model);
135 if (STATE_ARCHITECTURE (sd)
138 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
139 MACH_NAME (CPU_MACH (cpu))) != 0)
141 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
142 MODEL_NAME (CPU_MODEL (cpu)),
143 STATE_ARCHITECTURE (sd)->printable_name);
148 else if (STATE_ARCHITECTURE (sd))
150 /* Use the default model for the selected machine.
151 The default model is the first one in the list. */
152 const MACH *mach = sim_mach_lookup (STATE_ARCHITECTURE (sd)->printable_name);
153 sim_model_set (sd, NULL, MACH_MODELS (mach));
157 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_NAME (CPU_MACH (cpu)));
161 /* Establish any remaining configuration options. */
162 if (sim_config (sd) != SIM_RC_OK)
168 if (sim_post_argv_init (sd) != SIM_RC_OK)
174 /* Initialize various cgen things not done by common framework. */
180 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
182 /* Only needed for profiling, but the structure member is small. */
183 memset (& CPU_M32R_MISC_PROFILE (STATE_CPU (sd, c)), 0,
184 sizeof (CPU_M32R_MISC_PROFILE (STATE_CPU (sd, c))));
185 /* Hook in callback for reporting these stats */
186 PROFILE_INFO_CPU_CALLBACK (CPU_PROFILE_DATA (STATE_CPU (sd, c)))
187 = print_m32r_misc_cpu;
191 /* Store in a global so things like sparc32_dump_regs can be invoked
192 from the gdb command line. */
199 sim_close (sd, quitting)
203 sim_module_uninstall (sd);
207 sim_create_inferior (sd, abfd, argv, envp)
213 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
217 addr = bfd_get_start_address (abfd);
220 sim_pc_set (current_cpu, addr);
223 STATE_ARGV (sd) = sim_copy_argv (argv);
224 STATE_ENVP (sd) = sim_copy_argv (envp);
231 sim_stop (SIM_DESC sd)
233 switch (STATE_ARCHITECTURE (sd)->mach)
236 return m32r_engine_stop (sd, NULL, NULL_CIA, sim_stopped, SIM_SIGINT);
237 /* start-sanitize-m32rx */
238 #ifdef HAVE_CPU_M32RX
239 case bfd_mach_m32rx :
240 return m32rx_engine_stop (sd, NULL, NULL_CIA, sim_stopped, SIM_SIGINT);
242 /* end-sanitize-m32rx */
248 /* This isn't part of the official interface.
249 This is just a good place to put this for now. */
252 sim_sync_stop (SIM_DESC sd, SIM_CPU *cpu, PCADDR pc, enum sim_stop reason, int sigrc)
254 switch (STATE_ARCHITECTURE (sd)->mach)
257 (void) m32r_engine_stop (sd, cpu, pc, reason, sigrc);
259 /* start-sanitize-m32rx */
260 #ifdef HAVE_CPU_M32RX
261 case bfd_mach_m32rx :
262 (void) m32rx_engine_stop (sd, cpu, pc, reason, sigrc);
265 /* end-sanitize-m32rx */
272 sim_resume (sd, step, siggnal)
276 sim_module_resume (sd);
278 switch (STATE_ARCHITECTURE (sd)->mach)
281 m32r_engine_run (sd, step, siggnal);
283 /* start-sanitize-m32rx */
284 #ifdef HAVE_CPU_M32RX
285 case bfd_mach_m32rx :
286 m32rx_engine_run (sd, step, siggnal);
289 /* end-sanitize-m32rx */
294 sim_module_suspend (sd);
297 /* PROFILE_CPU_CALLBACK */
300 print_m32r_misc_cpu (SIM_CPU *cpu, int verbose)
302 SIM_DESC sd = CPU_STATE (cpu);
305 if (CPU_PROFILE_FLAGS (cpu) [PROFILE_INSN_IDX])
307 sim_io_printf (sd, "Miscellaneous Statistics\n\n");
308 sim_io_printf (sd, " %-*s %s\n\n",
309 PROFILE_LABEL_WIDTH, "Fill nops:",
310 sim_add_commas (buf, sizeof (buf),
311 CPU_M32R_MISC_PROFILE (cpu).fillnop_count));
315 /* The contents of BUF are in target byte order. */
318 sim_fetch_register (sd, rn, buf, length)
324 SIM_CPU *cpu = STATE_CPU (sd, 0);
326 return (* CPU_REG_FETCH (cpu)) (cpu, rn, buf, length);
329 /* The contents of BUF are in target byte order. */
332 sim_store_register (sd, rn, buf, length)
338 SIM_CPU *cpu = STATE_CPU (sd, 0);
340 return (* CPU_REG_STORE (cpu)) (cpu, rn, buf, length);
344 sim_do_command (sd, cmd)
348 if (sim_args_command (sd, cmd) != SIM_RC_OK)
349 sim_io_eprintf (sd, "Unknown command `%s'\n", cmd);
352 /* The semantic code invokes this for illegal (unrecognized) instructions. */
355 sim_engine_illegal_insn (current_cpu, pc)
356 SIM_CPU *current_cpu;
359 sim_engine_halt (CPU_STATE (current_cpu), current_cpu, NULL, pc,
360 sim_stopped, SIM_SIGILL);
363 /* Utility fns to access registers, without knowing the current mach. */
366 h_gr_get (SIM_CPU *current_cpu, UINT regno)
368 switch (STATE_ARCHITECTURE (CPU_STATE (current_cpu))->mach)
371 return m32r_h_gr_get (current_cpu, regno);
372 /* start-sanitize-m32rx */
373 #ifdef HAVE_CPU_M32RX
374 case bfd_mach_m32rx :
375 return m32rx_h_gr_get (current_cpu, regno);
377 /* end-sanitize-m32rx */
384 h_gr_set (SIM_CPU *current_cpu, UINT regno, SI newval)
386 switch (STATE_ARCHITECTURE (CPU_STATE (current_cpu))->mach)
389 m32r_h_gr_set (current_cpu, regno, newval);
391 /* start-sanitize-m32rx */
392 #ifdef HAVE_CPU_M32RX
393 case bfd_mach_m32rx :
394 m32rx_h_gr_set (current_cpu, regno, newval);
397 /* end-sanitize-m32rx */
403 /* Read/write functions for system call interface. */
406 syscall_read_mem (host_callback *cb, struct cb_syscall *sc,
407 unsigned long taddr, char *buf, int bytes)
409 SIM_DESC sd = (SIM_DESC) sc->p1;
410 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
412 return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes);
416 syscall_write_mem (host_callback *cb, struct cb_syscall *sc,
417 unsigned long taddr, const char *buf, int bytes)
419 SIM_DESC sd = (SIM_DESC) sc->p1;
420 SIM_CPU *cpu = (SIM_CPU *) sc->p2;
422 return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
426 The result is the pc address to continue at. */
429 do_trap (SIM_CPU *current_cpu, int num)
431 SIM_DESC sd = CPU_STATE (current_cpu);
432 host_callback *cb = STATE_CALLBACK (sd);
434 #ifdef SIM_HAVE_BREAKPOINTS
435 /* Check for breakpoints "owned" by the simulator first, regardless
439 /* First try sim-break.c. If it's a breakpoint the simulator "owns"
440 it doesn't return. Otherwise it returns and let's us try. */
441 sim_handle_breakpoint (sd, current_cpu, sim_pc_get (current_cpu));
446 if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)
448 /* The new pc is the trap vector entry.
449 We assume there's a branch there to some handler. */
450 USI new_pc = num * 4;
457 /* Trap 0 is used for system calls. */
461 CB_SYSCALL_INIT (&s);
462 s.func = h_gr_get (current_cpu, 0);
463 s.arg1 = h_gr_get (current_cpu, 1);
464 s.arg2 = h_gr_get (current_cpu, 2);
465 s.arg3 = h_gr_get (current_cpu, 3);
467 if (s.func == TARGET_SYS_exit)
469 sim_engine_halt (sd, current_cpu, NULL, sim_pc_get (current_cpu),
474 s.p2 = (PTR) current_cpu;
475 s.read_mem = syscall_read_mem;
476 s.write_mem = syscall_write_mem;
477 cb_syscall (STATE_CALLBACK (sd), &s);
478 h_gr_set (current_cpu, 2, s.errcode);
479 h_gr_set (current_cpu, 0, s.result);
480 h_gr_set (current_cpu, 1, s.result2);
484 case 1: /* breakpoint trap */
485 sim_engine_halt (sd, current_cpu, NULL, NULL_CIA,
486 sim_stopped, SIM_SIGTRAP);
490 /* Unless in the operating environment, ignore other traps. */
494 /* Fake an "rte" insn. */
495 return (sim_pc_get (current_cpu) & -4) + 4;