1 /* gdb-if.c -- sim interface to GDB.
3 Copyright (C) 2011-2015 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb/callback.h"
31 #include "gdb/remote-sim.h"
32 #include "gdb/signals.h"
33 #include "gdb/sim-rl78.h"
40 /* Ideally, we'd wrap up all the minisim's data structures in an
41 object and pass that around. However, neither GDB nor run needs
44 So we just have one instance, that lives in global variables, and
45 each time we open it, we re-initialize it. */
52 static struct sim_state the_minisim
= {
53 "This is the sole rl78 minisim instance."
58 static struct host_callback_struct
*host_callbacks
;
60 /* Open an instance of the sim. For this sim, only one instance
61 is permitted. If sim_open() is called multiple times, the sim
65 sim_open (SIM_OPEN_KIND kind
,
66 struct host_callback_struct
*callback
,
67 struct bfd
*abfd
, char **argv
)
70 fprintf (stderr
, "rl78 minisim: re-opened sim\n");
72 /* The 'run' interface doesn't use this function, so we don't care
73 about KIND; it's always SIM_OPEN_DEBUG. */
74 if (kind
!= SIM_OPEN_DEBUG
)
75 fprintf (stderr
, "rl78 minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
78 /* We use this for the load command. Perhaps someday, it'll be used
80 host_callbacks
= callback
;
82 /* We don't expect any command-line arguments. */
87 sim_disasm_init (abfd
);
90 while (argv
!= NULL
&& *argv
!= NULL
)
92 if (strcmp (*argv
, "g10") == 0 || strcmp (*argv
, "-Mg10") == 0)
94 fprintf (stderr
, "rl78 g10 support enabled.\n");
98 mem_set_mirror (0, 0xf8000, 4096);
101 if (strcmp (*argv
, "g13") == 0 || strcmp (*argv
, "-Mg13") == 0)
103 fprintf (stderr
, "rl78 g13 support enabled.\n");
109 if (strcmp (*argv
, "g14") == 0 || strcmp (*argv
, "-Mg14") == 0)
111 fprintf (stderr
, "rl78 g14 support enabled.\n");
123 /* Verify the sim descriptor. Just print a message if the descriptor
124 doesn't match. Nothing bad will happen if the descriptor doesn't
125 match because all of the state is global. But if it doesn't
126 match, that means there's a problem with the caller. */
129 check_desc (SIM_DESC sd
)
131 if (sd
!= &the_minisim
)
132 fprintf (stderr
, "rl78 minisim: desc != &the_minisim\n");
138 sim_close (SIM_DESC sd
, int quitting
)
142 /* Not much to do. At least free up our memory. */
148 /* Open the program to run; print a message if the program cannot
152 open_objfile (const char *filename
)
154 bfd
*prog
= bfd_openr (filename
, 0);
158 fprintf (stderr
, "Can't read %s\n", filename
);
162 if (!bfd_check_format (prog
, bfd_object
))
164 fprintf (stderr
, "%s not a rl78 program\n", filename
);
171 /* Load a program. */
174 sim_load (SIM_DESC sd
, const char *prog
, struct bfd
*abfd
, int from_tty
)
179 abfd
= open_objfile (prog
);
183 rl78_load (abfd
, host_callbacks
, "sim");
188 /* Create inferior. */
191 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
196 rl78_load (abfd
, 0, "sim");
204 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
210 else if (mem
+ length
> MEM_SIZE
)
211 length
= MEM_SIZE
- mem
;
213 mem_get_blk (mem
, buf
, length
);
220 sim_write (SIM_DESC sd
, SIM_ADDR mem
, const unsigned char *buf
, int length
)
226 else if (mem
+ length
> MEM_SIZE
)
227 length
= MEM_SIZE
- mem
;
229 mem_put_blk (mem
, buf
, length
);
233 /* Read the LENGTH bytes at BUF as an little-endian value. */
236 get_le (unsigned char *buf
, int length
)
240 while (--length
>= 0)
241 acc
= (acc
<< 8) + buf
[length
];
246 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
249 put_le (unsigned char *buf
, int length
, SI val
)
253 for (i
= 0; i
< length
; i
++)
260 /* Verify that REGNO is in the proper range. Return 0 if not and
261 something non-zero if so. */
264 check_regno (enum sim_rl78_regnum regno
)
266 return 0 <= regno
&& regno
< sim_rl78_num_regs
;
269 /* Return the size of the register REGNO. */
272 reg_size (enum sim_rl78_regnum regno
)
276 if (regno
== sim_rl78_pc_regnum
)
284 /* Return the register address associated with the register specified by
288 reg_addr (enum sim_rl78_regnum regno
)
290 if (sim_rl78_bank0_r0_regnum
<= regno
291 && regno
<= sim_rl78_bank0_r7_regnum
)
292 return 0xffef8 + (regno
- sim_rl78_bank0_r0_regnum
);
293 else if (sim_rl78_bank1_r0_regnum
<= regno
294 && regno
<= sim_rl78_bank1_r7_regnum
)
295 return 0xffef0 + (regno
- sim_rl78_bank1_r0_regnum
);
296 else if (sim_rl78_bank2_r0_regnum
<= regno
297 && regno
<= sim_rl78_bank2_r7_regnum
)
298 return 0xffee8 + (regno
- sim_rl78_bank2_r0_regnum
);
299 else if (sim_rl78_bank3_r0_regnum
<= regno
300 && regno
<= sim_rl78_bank3_r7_regnum
)
301 return 0xffee0 + (regno
- sim_rl78_bank3_r0_regnum
);
302 else if (regno
== sim_rl78_psw_regnum
)
304 else if (regno
== sim_rl78_es_regnum
)
306 else if (regno
== sim_rl78_cs_regnum
)
308 /* Note: We can't handle PC here because it's not memory mapped. */
309 else if (regno
== sim_rl78_spl_regnum
)
311 else if (regno
== sim_rl78_sph_regnum
)
313 else if (regno
== sim_rl78_pmc_regnum
)
315 else if (regno
== sim_rl78_mem_regnum
)
321 /* Fetch the contents of the register specified by REGNO, placing the
322 contents in BUF. The length LENGTH must match the sim's internal
323 notion of the register's size. */
326 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
333 if (!check_regno (regno
))
336 size
= reg_size (regno
);
341 if (regno
== sim_rl78_pc_regnum
)
344 val
= memory
[reg_addr (regno
)];
346 put_le (buf
, length
, val
);
351 /* Store the value stored in BUF to the register REGNO. The length
352 LENGTH must match the sim's internal notion of the register size. */
355 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
362 if (!check_regno (regno
))
365 size
= reg_size (regno
);
370 val
= get_le (buf
, length
);
372 if (regno
== sim_rl78_pc_regnum
)
376 /* The rl78 program counter is 20 bits wide. Ensure that GDB
377 hasn't picked up any stray bits. This has occurred when performing
378 a GDB "return" command in which the return address is obtained
379 from a 32-bit container on the stack. */
380 assert ((pc
& ~0x0fffff) == 0);
383 memory
[reg_addr (regno
)] = val
;
387 /* Print out message associated with "info target". */
390 sim_info (SIM_DESC sd
, int verbose
)
394 printf ("The rl78 minisim doesn't collect any statistics.\n");
397 static volatile int stop
;
398 static enum sim_stop reason
;
402 /* Given a signal number used by the rl78 bsp (that is, newlib),
403 return the corresponding signal numbers. */
406 rl78_signal_to_target (int sig
)
411 return GDB_SIGNAL_ILL
;
414 return GDB_SIGNAL_TRAP
;
417 return GDB_SIGNAL_BUS
;
420 return GDB_SIGNAL_SEGV
;
423 return GDB_SIGNAL_XCPU
;
427 return GDB_SIGNAL_INT
;
430 return GDB_SIGNAL_FPE
;
434 return GDB_SIGNAL_ABRT
;
441 /* Take a step return code RC and set up the variables consulted by
442 sim_stop_reason appropriately. */
447 if (RL78_STEPPED (rc
) || RL78_HIT_BREAK (rc
))
449 reason
= sim_stopped
;
450 siggnal
= GDB_SIGNAL_TRAP
;
452 else if (RL78_STOPPED (rc
))
454 reason
= sim_stopped
;
455 siggnal
= rl78_signal_to_target (RL78_STOP_SIG (rc
));
459 assert (RL78_EXITED (rc
));
461 siggnal
= RL78_EXIT_STATUS (rc
);
466 /* Resume execution after a stop. */
469 sim_resume (SIM_DESC sd
, int step
, int sig_to_deliver
)
475 if (sig_to_deliver
!= 0)
478 "Warning: the rl78 minisim does not implement "
479 "signal delivery yet.\n" "Resuming with no signal.\n");
482 /* We don't clear 'stop' here, because then we would miss
483 interrupts that arrived on the way here. Instead, we clear
484 the flag in sim_stop_reason, after GDB has disabled the
485 interrupt signal handler. */
491 reason
= sim_stopped
;
492 siggnal
= GDB_SIGNAL_INT
;
496 rc
= setjmp (decode_jmp_buf
);
498 rc
= decode_opcode ();
500 if (!RL78_STEPPED (rc
) || step
)
511 sim_stop (SIM_DESC sd
)
518 /* Fetch the stop reason and signal. */
521 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason_p
, int *sigrc_p
)
529 /* Execute the sim-specific command associated with GDB's "sim ..."
533 sim_do_command (SIM_DESC sd
, const char *cmd
)
536 char *p
= strdup (cmd
);
547 /* Skip leading whitespace. */
551 /* Find the extent of the command word. */
552 for (p
= cmd
; *p
; p
++)
556 /* Null-terminate the command word, and record the start of any
557 further arguments. */
562 while (isspace (*args
))
569 if (strcmp (cmd
, "trace") == 0)
571 if (strcmp (args
, "on") == 0)
573 else if (strcmp (args
, "off") == 0)
576 printf ("The 'sim trace' command expects 'on' or 'off' "
577 "as an argument.\n");
579 else if (strcmp (cmd
, "verbose") == 0)
581 if (strcmp (args
, "on") == 0)
583 else if (strcmp (args
, "noisy") == 0)
585 else if (strcmp (args
, "off") == 0)
588 printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
589 " as an argument.\n");
592 printf ("The 'sim' command expects either 'trace' or 'verbose'"
593 " as a subcommand.\n");
598 /* Stub for command completion. */
601 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
This page took 0.041654 seconds and 4 git commands to generate.