1 /* gdb.c --- sim interface to GDB.
3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 The GNU simulators are free software; you can redistribute them and/or
9 modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The GNU simulators are distributed in the hope that they will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the GNU simulators; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "gdb/callback.h"
32 #include "gdb/remote-sim.h"
33 #include "gdb/signals.h"
34 #include "gdb/sim-m32c.h"
41 /* I don't want to wrap up all the minisim's data structures in an
42 object and pass that around. That'd be a big change, and neither
43 GDB nor run needs that ability.
45 So we just have one instance, that lives in global variables, and
46 each time we open it, we re-initialize it. */
52 static struct sim_state the_minisim
= {
53 "This is the sole m32c minisim instance. See libsim.a's global variables."
59 sim_open (SIM_OPEN_KIND kind
,
60 struct host_callback_struct
*callback
,
61 struct bfd
*abfd
, char **argv
)
64 fprintf (stderr
, "m32c minisim: re-opened sim\n");
66 /* The 'run' interface doesn't use this function, so we don't care
67 about KIND; it's always SIM_OPEN_DEBUG. */
68 if (kind
!= SIM_OPEN_DEBUG
)
69 fprintf (stderr
, "m32c minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
73 m32c_set_mach (bfd_get_mach (abfd
));
75 /* We can use ABFD, if non-NULL to select the appropriate
76 architecture. But we only support the r8c right now. */
78 set_callbacks (callback
);
80 /* We don't expect any command-line arguments. */
90 check_desc (SIM_DESC sd
)
92 if (sd
!= &the_minisim
)
93 fprintf (stderr
, "m32c minisim: desc != &the_minisim\n");
97 sim_close (SIM_DESC sd
, int quitting
)
101 /* Not much to do. At least free up our memory. */
108 open_objfile (const char *filename
)
110 bfd
*prog
= bfd_openr (filename
, 0);
114 fprintf (stderr
, "Can't read %s\n", filename
);
118 if (!bfd_check_format (prog
, bfd_object
))
120 fprintf (stderr
, "%s not a m32c program\n", filename
);
129 sim_load (SIM_DESC sd
, char *prog
, struct bfd
*abfd
, int from_tty
)
134 abfd
= open_objfile (prog
);
144 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
155 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
162 mem_get_blk ((int) mem
, buf
, length
);
168 sim_write (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
172 mem_put_blk ((int) mem
, buf
, length
);
178 /* Read the LENGTH bytes at BUF as an little-endian value. */
180 get_le (unsigned char *buf
, int length
)
183 while (--length
>= 0)
184 acc
= (acc
<< 8) + buf
[length
];
189 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
191 put_le (unsigned char *buf
, int length
, DI val
)
195 for (i
= 0; i
< length
; i
++)
203 check_regno (enum m32c_sim_reg regno
)
205 return 0 <= regno
&& regno
< m32c_sim_reg_num_regs
;
209 mask_size (int addr_mask
)
220 "m32c minisim: addr_mask_size: unexpected mask 0x%x\n",
222 return sizeof (addr_mask
);
227 reg_size (enum m32c_sim_reg regno
)
231 case m32c_sim_reg_r0_bank0
:
232 case m32c_sim_reg_r1_bank0
:
233 case m32c_sim_reg_r2_bank0
:
234 case m32c_sim_reg_r3_bank0
:
235 case m32c_sim_reg_r0_bank1
:
236 case m32c_sim_reg_r1_bank1
:
237 case m32c_sim_reg_r2_bank1
:
238 case m32c_sim_reg_r3_bank1
:
239 case m32c_sim_reg_flg
:
240 case m32c_sim_reg_svf
:
243 case m32c_sim_reg_a0_bank0
:
244 case m32c_sim_reg_a1_bank0
:
245 case m32c_sim_reg_fb_bank0
:
246 case m32c_sim_reg_sb_bank0
:
247 case m32c_sim_reg_a0_bank1
:
248 case m32c_sim_reg_a1_bank1
:
249 case m32c_sim_reg_fb_bank1
:
250 case m32c_sim_reg_sb_bank1
:
251 case m32c_sim_reg_usp
:
252 case m32c_sim_reg_isp
:
253 return mask_size (addr_mask
);
255 case m32c_sim_reg_pc
:
256 case m32c_sim_reg_intb
:
257 case m32c_sim_reg_svp
:
258 case m32c_sim_reg_vct
:
259 return mask_size (membus_mask
);
261 case m32c_sim_reg_dmd0
:
262 case m32c_sim_reg_dmd1
:
265 case m32c_sim_reg_dct0
:
266 case m32c_sim_reg_dct1
:
267 case m32c_sim_reg_drc0
:
268 case m32c_sim_reg_drc1
:
271 case m32c_sim_reg_dma0
:
272 case m32c_sim_reg_dma1
:
273 case m32c_sim_reg_dsa0
:
274 case m32c_sim_reg_dsa1
:
275 case m32c_sim_reg_dra0
:
276 case m32c_sim_reg_dra1
:
280 fprintf (stderr
, "m32c minisim: unrecognized register number: %d\n",
287 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
293 if (!check_regno (regno
))
296 size
= reg_size (regno
);
303 case m32c_sim_reg_r0_bank0
:
304 val
= regs
.r
[0].r_r0
;
306 case m32c_sim_reg_r1_bank0
:
307 val
= regs
.r
[0].r_r1
;
309 case m32c_sim_reg_r2_bank0
:
310 val
= regs
.r
[0].r_r2
;
312 case m32c_sim_reg_r3_bank0
:
313 val
= regs
.r
[0].r_r3
;
315 case m32c_sim_reg_a0_bank0
:
316 val
= regs
.r
[0].r_a0
;
318 case m32c_sim_reg_a1_bank0
:
319 val
= regs
.r
[0].r_a1
;
321 case m32c_sim_reg_fb_bank0
:
322 val
= regs
.r
[0].r_fb
;
324 case m32c_sim_reg_sb_bank0
:
325 val
= regs
.r
[0].r_sb
;
327 case m32c_sim_reg_r0_bank1
:
328 val
= regs
.r
[1].r_r0
;
330 case m32c_sim_reg_r1_bank1
:
331 val
= regs
.r
[1].r_r1
;
333 case m32c_sim_reg_r2_bank1
:
334 val
= regs
.r
[1].r_r2
;
336 case m32c_sim_reg_r3_bank1
:
337 val
= regs
.r
[1].r_r3
;
339 case m32c_sim_reg_a0_bank1
:
340 val
= regs
.r
[1].r_a0
;
342 case m32c_sim_reg_a1_bank1
:
343 val
= regs
.r
[1].r_a1
;
345 case m32c_sim_reg_fb_bank1
:
346 val
= regs
.r
[1].r_fb
;
348 case m32c_sim_reg_sb_bank1
:
349 val
= regs
.r
[1].r_sb
;
352 case m32c_sim_reg_usp
:
355 case m32c_sim_reg_isp
:
358 case m32c_sim_reg_pc
:
361 case m32c_sim_reg_intb
:
362 val
= regs
.r_intbl
* 65536 + regs
.r_intbl
;
364 case m32c_sim_reg_flg
:
368 /* These registers aren't implemented by the minisim. */
369 case m32c_sim_reg_svf
:
370 case m32c_sim_reg_svp
:
371 case m32c_sim_reg_vct
:
372 case m32c_sim_reg_dmd0
:
373 case m32c_sim_reg_dmd1
:
374 case m32c_sim_reg_dct0
:
375 case m32c_sim_reg_dct1
:
376 case m32c_sim_reg_drc0
:
377 case m32c_sim_reg_drc1
:
378 case m32c_sim_reg_dma0
:
379 case m32c_sim_reg_dma1
:
380 case m32c_sim_reg_dsa0
:
381 case m32c_sim_reg_dsa1
:
382 case m32c_sim_reg_dra0
:
383 case m32c_sim_reg_dra1
:
387 fprintf (stderr
, "m32c minisim: unrecognized register number: %d\n",
392 put_le (buf
, length
, val
);
399 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
405 if (!check_regno (regno
))
408 size
= reg_size (regno
);
412 DI val
= get_le (buf
, length
);
416 case m32c_sim_reg_r0_bank0
:
417 regs
.r
[0].r_r0
= val
& 0xffff;
419 case m32c_sim_reg_r1_bank0
:
420 regs
.r
[0].r_r1
= val
& 0xffff;
422 case m32c_sim_reg_r2_bank0
:
423 regs
.r
[0].r_r2
= val
& 0xffff;
425 case m32c_sim_reg_r3_bank0
:
426 regs
.r
[0].r_r3
= val
& 0xffff;
428 case m32c_sim_reg_a0_bank0
:
429 regs
.r
[0].r_a0
= val
& addr_mask
;
431 case m32c_sim_reg_a1_bank0
:
432 regs
.r
[0].r_a1
= val
& addr_mask
;
434 case m32c_sim_reg_fb_bank0
:
435 regs
.r
[0].r_fb
= val
& addr_mask
;
437 case m32c_sim_reg_sb_bank0
:
438 regs
.r
[0].r_sb
= val
& addr_mask
;
440 case m32c_sim_reg_r0_bank1
:
441 regs
.r
[1].r_r0
= val
& 0xffff;
443 case m32c_sim_reg_r1_bank1
:
444 regs
.r
[1].r_r1
= val
& 0xffff;
446 case m32c_sim_reg_r2_bank1
:
447 regs
.r
[1].r_r2
= val
& 0xffff;
449 case m32c_sim_reg_r3_bank1
:
450 regs
.r
[1].r_r3
= val
& 0xffff;
452 case m32c_sim_reg_a0_bank1
:
453 regs
.r
[1].r_a0
= val
& addr_mask
;
455 case m32c_sim_reg_a1_bank1
:
456 regs
.r
[1].r_a1
= val
& addr_mask
;
458 case m32c_sim_reg_fb_bank1
:
459 regs
.r
[1].r_fb
= val
& addr_mask
;
461 case m32c_sim_reg_sb_bank1
:
462 regs
.r
[1].r_sb
= val
& addr_mask
;
465 case m32c_sim_reg_usp
:
466 regs
.r_usp
= val
& addr_mask
;
468 case m32c_sim_reg_isp
:
469 regs
.r_isp
= val
& addr_mask
;
471 case m32c_sim_reg_pc
:
472 regs
.r_pc
= val
& membus_mask
;
474 case m32c_sim_reg_intb
:
475 regs
.r_intbl
= (val
& membus_mask
) & 0xffff;
476 regs
.r_intbh
= (val
& membus_mask
) >> 16;
478 case m32c_sim_reg_flg
:
479 regs
.r_flags
= val
& 0xffff;
482 /* These registers aren't implemented by the minisim. */
483 case m32c_sim_reg_svf
:
484 case m32c_sim_reg_svp
:
485 case m32c_sim_reg_vct
:
486 case m32c_sim_reg_dmd0
:
487 case m32c_sim_reg_dmd1
:
488 case m32c_sim_reg_dct0
:
489 case m32c_sim_reg_dct1
:
490 case m32c_sim_reg_drc0
:
491 case m32c_sim_reg_drc1
:
492 case m32c_sim_reg_dma0
:
493 case m32c_sim_reg_dma1
:
494 case m32c_sim_reg_dsa0
:
495 case m32c_sim_reg_dsa1
:
496 case m32c_sim_reg_dra0
:
497 case m32c_sim_reg_dra1
:
501 fprintf (stderr
, "m32c minisim: unrecognized register number: %d\n",
511 sim_info (SIM_DESC sd
, int verbose
)
515 printf ("The m32c minisim doesn't collect any statistics.\n");
518 static volatile int stop
;
519 static enum sim_stop reason
;
523 /* Given a signal number used by the M32C bsp (that is, newlib),
524 return a host signal number. (Oddly, the gdb/sim interface uses
525 host signal numbers...) */
527 m32c_signal_to_host (int m32c
)
576 /* Take a step return code RC and set up the variables consulted by
577 sim_stop_reason appropriately. */
581 if (M32C_STEPPED (rc
) || M32C_HIT_BREAK (rc
))
583 reason
= sim_stopped
;
584 siggnal
= TARGET_SIGNAL_TRAP
;
586 else if (M32C_STOPPED (rc
))
588 reason
= sim_stopped
;
589 siggnal
= m32c_signal_to_host (M32C_STOP_SIG (rc
));
593 assert (M32C_EXITED (rc
));
595 siggnal
= M32C_EXIT_STATUS (rc
);
601 sim_resume (SIM_DESC sd
, int step
, int sig_to_deliver
)
605 if (sig_to_deliver
!= 0)
608 "Warning: the m32c minisim does not implement "
609 "signal delivery yet.\n" "Resuming with no signal.\n");
613 handle_step (decode_opcode ());
616 /* We don't clear 'stop' here, because then we would miss
617 interrupts that arrived on the way here. Instead, we clear
618 the flag in sim_stop_reason, after GDB has disabled the
619 interrupt signal handler. */
625 reason
= sim_stopped
;
626 siggnal
= TARGET_SIGNAL_INT
;
630 int rc
= decode_opcode ();
632 if (!M32C_STEPPED (rc
))
642 sim_stop (SIM_DESC sd
)
650 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason_p
, int *sigrc_p
)
659 sim_do_command (SIM_DESC sd
, char *cmd
)
665 /* Skip leading whitespace. */
669 /* Find the extent of the command word. */
670 for (p
= cmd
; *p
; p
++)
674 /* Null-terminate the command word, and record the start of any
675 further arguments. */
681 while (isspace (*args
))
687 if (strcmp (cmd
, "trace") == 0)
689 if (strcmp (args
, "on") == 0)
691 else if (strcmp (args
, "off") == 0)
694 printf ("The 'sim trace' command expects 'on' or 'off' "
695 "as an argument.\n");
697 else if (strcmp (cmd
, "verbose") == 0)
699 if (strcmp (args
, "on") == 0)
701 else if (strcmp (args
, "off") == 0)
704 printf ("The 'sim verbose' command expects 'on' or 'off'"
705 " as an argument.\n");
708 printf ("The 'sim' command expects either 'trace' or 'verbose'"
709 " as a subcommand.\n");