Make RL78 disassembler and simulator respect ISA for mul/div
[deliverable/binutils-gdb.git] / sim / rl78 / gdb-if.c
CommitLineData
9058f767
KB
1/* gdb-if.c -- sim interface to GDB.
2
32d0add0 3Copyright (C) 2011-2015 Free Software Foundation, Inc.
9058f767
KB
4Contributed by Red Hat, Inc.
5
6This file is part of the GNU simulators.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include <stdio.h>
23#include <assert.h>
24#include <signal.h>
25#include <string.h>
26#include <ctype.h>
27#include <stdlib.h>
28
29#include "ansidecl.h"
30#include "gdb/callback.h"
31#include "gdb/remote-sim.h"
32#include "gdb/signals.h"
33#include "gdb/sim-rl78.h"
34
35#include "cpu.h"
36#include "mem.h"
37#include "load.h"
38#include "trace.h"
39
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
42 that ability.
43
44 So we just have one instance, that lives in global variables, and
45 each time we open it, we re-initialize it. */
46
47struct sim_state
48{
49 const char *message;
50};
51
52static struct sim_state the_minisim = {
53 "This is the sole rl78 minisim instance."
54};
55
56static int open;
57
9058f767
KB
58static struct host_callback_struct *host_callbacks;
59
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
62 will be reset. */
63
64SIM_DESC
65sim_open (SIM_OPEN_KIND kind,
66 struct host_callback_struct *callback,
67 struct bfd *abfd, char **argv)
68{
69 if (open)
70 fprintf (stderr, "rl78 minisim: re-opened sim\n");
71
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",
76 kind);
77
78 /* We use this for the load command. Perhaps someday, it'll be used
79 for syscalls too. */
80 host_callbacks = callback;
81
82 /* We don't expect any command-line arguments. */
83
84 init_cpu ();
85 trace = 0;
86
87 sim_disasm_init (abfd);
88 open = 1;
0952813b
DD
89
90 while (argv != NULL && *argv != NULL)
91 {
92 if (strcmp (*argv, "g10") == 0 || strcmp (*argv, "-Mg10") == 0)
93 {
94 fprintf (stderr, "rl78 g10 support enabled.\n");
95 rl78_g10_mode = 1;
96 g13_multiply = 0;
97 g14_multiply = 0;
98 mem_set_mirror (0, 0xf8000, 4096);
99 break;
100 }
101 if (strcmp (*argv, "g13") == 0 || strcmp (*argv, "-Mg13") == 0)
102 {
103 fprintf (stderr, "rl78 g13 support enabled.\n");
104 rl78_g10_mode = 0;
105 g13_multiply = 1;
106 g14_multiply = 0;
107 break;
108 }
109 if (strcmp (*argv, "g14") == 0 || strcmp (*argv, "-Mg14") == 0)
110 {
111 fprintf (stderr, "rl78 g14 support enabled.\n");
112 rl78_g10_mode = 0;
113 g13_multiply = 0;
114 g14_multiply = 1;
115 break;
116 }
117 argv++;
118 }
119
9058f767
KB
120 return &the_minisim;
121}
122
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. */
127
128static void
129check_desc (SIM_DESC sd)
130{
131 if (sd != &the_minisim)
132 fprintf (stderr, "rl78 minisim: desc != &the_minisim\n");
133}
134
135/* Close the sim. */
136
137void
138sim_close (SIM_DESC sd, int quitting)
139{
140 check_desc (sd);
141
142 /* Not much to do. At least free up our memory. */
143 init_mem ();
144
145 open = 0;
146}
147
148/* Open the program to run; print a message if the program cannot
149 be opened. */
150
151static bfd *
152open_objfile (const char *filename)
153{
154 bfd *prog = bfd_openr (filename, 0);
155
156 if (!prog)
157 {
158 fprintf (stderr, "Can't read %s\n", filename);
159 return 0;
160 }
161
162 if (!bfd_check_format (prog, bfd_object))
163 {
164 fprintf (stderr, "%s not a rl78 program\n", filename);
165 return 0;
166 }
167
168 return prog;
169}
170
171/* Load a program. */
172
173SIM_RC
b2b255bd 174sim_load (SIM_DESC sd, const char *prog, struct bfd *abfd, int from_tty)
9058f767
KB
175{
176 check_desc (sd);
177
178 if (!abfd)
179 abfd = open_objfile (prog);
180 if (!abfd)
181 return SIM_RC_FAIL;
182
183 rl78_load (abfd, host_callbacks, "sim");
184
185 return SIM_RC_OK;
186}
187
188/* Create inferior. */
189
190SIM_RC
191sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
192{
193 check_desc (sd);
194
195 if (abfd)
196 rl78_load (abfd, 0, "sim");
197
198 return SIM_RC_OK;
199}
200
201/* Read memory. */
202
203int
204sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
205{
206 check_desc (sd);
207
208 if (mem >= MEM_SIZE)
209 return 0;
210 else if (mem + length > MEM_SIZE)
211 length = MEM_SIZE - mem;
212
213 mem_get_blk (mem, buf, length);
214 return length;
215}
216
217/* Write memory. */
218
219int
220sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
221{
222 check_desc (sd);
223
224 if (mem >= MEM_SIZE)
225 return 0;
226 else if (mem + length > MEM_SIZE)
227 length = MEM_SIZE - mem;
228
229 mem_put_blk (mem, buf, length);
230 return length;
231}
232
233/* Read the LENGTH bytes at BUF as an little-endian value. */
234
235static SI
236get_le (unsigned char *buf, int length)
237{
238 SI acc = 0;
239
240 while (--length >= 0)
241 acc = (acc << 8) + buf[length];
242
243 return acc;
244}
245
246/* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
247
248static void
249put_le (unsigned char *buf, int length, SI val)
250{
251 int i;
252
253 for (i = 0; i < length; i++)
254 {
255 buf[i] = val & 0xff;
256 val >>= 8;
257 }
258}
259
260/* Verify that REGNO is in the proper range. Return 0 if not and
261 something non-zero if so. */
262
263static int
264check_regno (enum sim_rl78_regnum regno)
265{
266 return 0 <= regno && regno < sim_rl78_num_regs;
267}
268
269/* Return the size of the register REGNO. */
270
271static size_t
272reg_size (enum sim_rl78_regnum regno)
273{
274 size_t size;
275
276 if (regno == sim_rl78_pc_regnum)
277 size = 4;
278 else
279 size = 1;
280
281 return size;
282}
283
284/* Return the register address associated with the register specified by
285 REGNO. */
286
287static unsigned long
288reg_addr (enum sim_rl78_regnum regno)
289{
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)
303 return 0xffffa;
304 else if (regno == sim_rl78_es_regnum)
305 return 0xffffd;
306 else if (regno == sim_rl78_cs_regnum)
307 return 0xffffc;
308 /* Note: We can't handle PC here because it's not memory mapped. */
309 else if (regno == sim_rl78_spl_regnum)
310 return 0xffff8;
311 else if (regno == sim_rl78_sph_regnum)
312 return 0xffff9;
313 else if (regno == sim_rl78_pmc_regnum)
314 return 0xffffe;
315 else if (regno == sim_rl78_mem_regnum)
316 return 0xfffff;
317
318 return 0;
319}
320
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. */
324
325int
326sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
327{
328 size_t size;
329 SI val;
330
331 check_desc (sd);
332
333 if (!check_regno (regno))
334 return 0;
335
336 size = reg_size (regno);
337
338 if (length != size)
339 return 0;
340
341 if (regno == sim_rl78_pc_regnum)
342 val = pc;
343 else
344 val = memory[reg_addr (regno)];
345
346 put_le (buf, length, val);
347
348 return size;
349}
350
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. */
353
354int
355sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
356{
357 size_t size;
358 SI val;
359
360 check_desc (sd);
361
362 if (!check_regno (regno))
363 return -1;
364
365 size = reg_size (regno);
366
367 if (length != size)
368 return -1;
369
370 val = get_le (buf, length);
371
372 if (regno == sim_rl78_pc_regnum)
317cc67d
KB
373 {
374 pc = val;
375
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);
381 }
9058f767
KB
382 else
383 memory[reg_addr (regno)] = val;
384 return size;
385}
386
387/* Print out message associated with "info target". */
388
389void
390sim_info (SIM_DESC sd, int verbose)
391{
392 check_desc (sd);
393
394 printf ("The rl78 minisim doesn't collect any statistics.\n");
395}
396
397static volatile int stop;
398static enum sim_stop reason;
399int siggnal;
400
401
402/* Given a signal number used by the rl78 bsp (that is, newlib),
403 return the corresponding signal numbers. */
404
405int
406rl78_signal_to_target (int sig)
407{
408 switch (sig)
409 {
410 case 4:
a493e3e2 411 return GDB_SIGNAL_ILL;
9058f767
KB
412
413 case 5:
a493e3e2 414 return GDB_SIGNAL_TRAP;
9058f767
KB
415
416 case 10:
a493e3e2 417 return GDB_SIGNAL_BUS;
9058f767
KB
418
419 case 11:
a493e3e2 420 return GDB_SIGNAL_SEGV;
9058f767
KB
421
422 case 24:
a493e3e2 423 return GDB_SIGNAL_XCPU;
9058f767
KB
424 break;
425
426 case 2:
a493e3e2 427 return GDB_SIGNAL_INT;
9058f767
KB
428
429 case 8:
a493e3e2 430 return GDB_SIGNAL_FPE;
9058f767
KB
431 break;
432
433 case 6:
a493e3e2 434 return GDB_SIGNAL_ABRT;
9058f767
KB
435 }
436
437 return 0;
438}
439
440
441/* Take a step return code RC and set up the variables consulted by
442 sim_stop_reason appropriately. */
443
444void
445handle_step (int rc)
446{
447 if (RL78_STEPPED (rc) || RL78_HIT_BREAK (rc))
448 {
449 reason = sim_stopped;
a493e3e2 450 siggnal = GDB_SIGNAL_TRAP;
9058f767
KB
451 }
452 else if (RL78_STOPPED (rc))
453 {
454 reason = sim_stopped;
455 siggnal = rl78_signal_to_target (RL78_STOP_SIG (rc));
456 }
457 else
458 {
459 assert (RL78_EXITED (rc));
460 reason = sim_exited;
461 siggnal = RL78_EXIT_STATUS (rc);
462 }
463}
464
465
466/* Resume execution after a stop. */
467
468void
469sim_resume (SIM_DESC sd, int step, int sig_to_deliver)
470{
471 int rc;
472
473 check_desc (sd);
474
475 if (sig_to_deliver != 0)
476 {
477 fprintf (stderr,
478 "Warning: the rl78 minisim does not implement "
479 "signal delivery yet.\n" "Resuming with no signal.\n");
480 }
481
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. */
486 for (;;)
487 {
488 if (stop)
489 {
490 stop = 0;
491 reason = sim_stopped;
a493e3e2 492 siggnal = GDB_SIGNAL_INT;
9058f767
KB
493 break;
494 }
495
9058f767
KB
496 rc = setjmp (decode_jmp_buf);
497 if (rc == 0)
498 rc = decode_opcode ();
499
500 if (!RL78_STEPPED (rc) || step)
501 {
502 handle_step (rc);
503 break;
504 }
505 }
506}
507
508/* Stop the sim. */
509
510int
511sim_stop (SIM_DESC sd)
512{
513 stop = 1;
514
515 return 1;
516}
517
518/* Fetch the stop reason and signal. */
519
520void
521sim_stop_reason (SIM_DESC sd, enum sim_stop *reason_p, int *sigrc_p)
522{
523 check_desc (sd);
524
525 *reason_p = reason;
526 *sigrc_p = siggnal;
527}
528
529/* Execute the sim-specific command associated with GDB's "sim ..."
530 command. */
531
532void
60d847df 533sim_do_command (SIM_DESC sd, const char *cmd)
9058f767 534{
60d847df
MF
535 const char *args;
536 char *p = strdup (cmd);
9058f767
KB
537
538 check_desc (sd);
539
540 if (cmd == NULL)
541 {
542 cmd = "";
543 args = "";
544 }
545 else
546 {
9058f767
KB
547 /* Skip leading whitespace. */
548 while (isspace (*p))
549 p++;
550
551 /* Find the extent of the command word. */
552 for (p = cmd; *p; p++)
553 if (isspace (*p))
554 break;
555
556 /* Null-terminate the command word, and record the start of any
557 further arguments. */
558 if (*p)
559 {
560 *p = '\0';
561 args = p + 1;
562 while (isspace (*args))
563 args++;
564 }
565 else
566 args = p;
567 }
568
569 if (strcmp (cmd, "trace") == 0)
570 {
571 if (strcmp (args, "on") == 0)
572 trace = 1;
573 else if (strcmp (args, "off") == 0)
574 trace = 0;
575 else
576 printf ("The 'sim trace' command expects 'on' or 'off' "
577 "as an argument.\n");
578 }
579 else if (strcmp (cmd, "verbose") == 0)
580 {
581 if (strcmp (args, "on") == 0)
582 verbose = 1;
583 else if (strcmp (args, "noisy") == 0)
584 verbose = 2;
585 else if (strcmp (args, "off") == 0)
586 verbose = 0;
587 else
588 printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
589 " as an argument.\n");
590 }
591 else
592 printf ("The 'sim' command expects either 'trace' or 'verbose'"
593 " as a subcommand.\n");
60d847df
MF
594
595 free (p);
9058f767
KB
596}
597
598/* Stub for command completion. */
599
600char **
3cb2ab1a 601sim_complete_command (SIM_DESC sd, const char *text, const char *word)
9058f767
KB
602{
603 return NULL;
604}
This page took 0.167885 seconds and 4 git commands to generate.