1 /* Target-dependent code for Hitachi H8/500, for GDB.
2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 Contributed by Steve Chamberlain
36 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
38 static int code_size
= 2;
40 static int data_size
= 2;
42 /* Shape of an H8/500 frame :
48 return address <2 or 4 bytes>
57 /* an easy to debug H8 stack frame looks like:
61 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
66 #define IS_PUSH(x) (((x) & 0xff00)==0x6d00)
67 #define IS_LINK_8(x) ((x) == 0x17)
68 #define IS_LINK_16(x) ((x) == 0x1f)
69 #define IS_MOVE_FP(x) ((x) == 0x0d76)
70 #define IS_MOV_SP_FP(x) ((x) == 0x0d76)
71 #define IS_SUB2_SP(x) ((x) == 0x1b87)
72 #define IS_MOVK_R5(x) ((x) == 0x7905)
73 #define IS_SUB_R5SP(x) ((x) == 0x1957)
81 h8500_skip_prologue (CORE_ADDR start_pc
)
85 w
= read_memory_integer (start_pc
, 1);
89 w
= read_memory_integer (start_pc
, 1);
95 w
= read_memory_integer (start_pc
, 2);
102 h8500_addr_bits_remove (CORE_ADDR addr
)
104 return ((addr
) & 0xffffff);
107 /* Given a GDB frame, determine the address of the calling function's frame.
108 This will be used to create a new GDB frame struct, and then
109 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
111 For us, the frame address is its stack pointer value, so we look up
112 the function prologue to determine the caller's sp value, and return it. */
115 h8500_frame_chain (struct frame_info
*thisframe
)
117 if (!inside_entry_file (thisframe
->pc
))
118 return (read_memory_integer (FRAME_FP (thisframe
), PTR_SIZE
));
123 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
124 is not the address of a valid instruction, the address of the next
125 instruction beyond ADDR otherwise. *PWORD1 receives the first word
126 of the instruction. */
129 NEXT_PROLOGUE_INSN (CORE_ADDR addr
, CORE_ADDR lim
, char *pword1
)
133 read_memory (addr
, pword1
, 1);
134 read_memory (addr
, pword1
+ 1, 1);
140 /* Examine the prologue of a function. `ip' points to the first
141 instruction. `limit' is the limit of the prologue (e.g. the addr
142 of the first linenumber, or perhaps the program counter if we're
143 stepping through). `frame_sp' is the stack pointer value in use in
144 this frame. `fsr' is a pointer to a frame_saved_regs structure
145 into which we put info about the registers saved by this frame.
146 `fi' is a struct frame_info pointer; we fill in various fields in
147 it to reflect the offsets of the arg pointer and the locals
150 /* Return the saved PC from this frame. */
153 frame_saved_pc (struct frame_info
*frame
)
155 return read_memory_integer (FRAME_FP (frame
) + 2, PTR_SIZE
);
159 h8500_pop_frame (void)
162 struct frame_saved_regs fsr
;
163 struct frame_info
*frame
= get_current_frame ();
165 get_frame_saved_regs (frame
, &fsr
);
167 for (regnum
= 0; regnum
< 8; regnum
++)
169 if (fsr
.regs
[regnum
])
170 write_register (regnum
, read_memory_short (fsr
.regs
[regnum
]));
172 flush_cached_frames ();
177 print_register_hook (int regno
)
179 if (regno
== CCR_REGNUM
)
187 read_relative_register_raw_bytes (regno
, b
);
189 printf_unfiltered ("\t");
190 printf_unfiltered ("I-%d - ", (l
& 0x80) != 0);
195 printf_unfiltered ("N-%d ", N
);
196 printf_unfiltered ("Z-%d ", Z
);
197 printf_unfiltered ("V-%d ", V
);
198 printf_unfiltered ("C-%d ", C
);
200 printf_unfiltered ("u> ");
202 printf_unfiltered ("u<= ");
204 printf_unfiltered ("u>= ");
206 printf_unfiltered ("u< ");
208 printf_unfiltered ("!= ");
210 printf_unfiltered ("== ");
212 printf_unfiltered (">= ");
214 printf_unfiltered ("< ");
215 if ((Z
| (N
^ V
)) == 0)
216 printf_unfiltered ("> ");
217 if ((Z
| (N
^ V
)) == 1)
218 printf_unfiltered ("<= ");
223 h8500_register_size (int regno
)
259 h8500_register_virtual_type (int regno
)
267 return builtin_type_unsigned_char
;
277 return builtin_type_unsigned_short
;
287 return builtin_type_unsigned_long
;
293 /* Put here the code to store, into a struct frame_saved_regs,
294 the addresses of the saved registers of frame described by FRAME_INFO.
295 This includes special registers such as pc and fp saved in special
296 ways in the stack frame. sp is even more special:
297 the address we return for it IS the sp for the next frame. */
300 frame_find_saved_regs (struct frame_info
*frame_info
,
301 struct frame_saved_regs
*frame_saved_regs
)
304 register int regmask
;
305 register CORE_ADDR next_addr
;
306 register CORE_ADDR pc
;
307 unsigned char thebyte
;
309 memset (frame_saved_regs
, '\0', sizeof *frame_saved_regs
);
311 if ((frame_info
)->pc
>= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4
312 && (frame_info
)->pc
<= (frame_info
)->frame
)
314 next_addr
= (frame_info
)->frame
;
315 pc
= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4;
319 pc
= get_pc_function_start ((frame_info
)->pc
);
320 /* Verify we have a link a6 instruction next;
321 if not we lose. If we win, find the address above the saved
322 regs using the amount of storage from the link instruction.
325 thebyte
= read_memory_integer (pc
, 1);
327 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 2), pc
+= 2;
328 else if (0x17 == thebyte
)
329 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 1), pc
+= 1;
334 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
335 if ((0x0c0177777 & read_memory_integer (pc
, 2)) == 0157774)
336 next_addr
+= read_memory_integer (pc
+= 2, 4), pc
+= 4;
340 thebyte
= read_memory_integer (pc
, 1);
345 regmask
= read_memory_integer (pc
, 1);
347 for (regnum
= 0; regnum
< 8; regnum
++, regmask
>>= 1)
351 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
354 thebyte
= read_memory_integer (pc
, 1);
356 /* Maybe got a load of pushes */
357 while (thebyte
== 0xbf)
360 regnum
= read_memory_integer (pc
, 1) & 0x7;
362 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
363 thebyte
= read_memory_integer (pc
, 1);
368 /* Remember the address of the frame pointer */
369 (frame_saved_regs
)->regs
[FP_REGNUM
] = (frame_info
)->frame
;
371 /* This is where the old sp is hidden */
372 (frame_saved_regs
)->regs
[SP_REGNUM
] = (frame_info
)->frame
;
374 /* And the PC - remember the pushed FP is always two bytes long */
375 (frame_saved_regs
)->regs
[PC_REGNUM
] = (frame_info
)->frame
+ 2;
379 saved_pc_after_call (void)
382 int a
= read_register (SP_REGNUM
);
384 x
= read_memory_integer (a
, code_size
);
387 /* Stick current code segement onto top */
389 x
|= read_register (SEG_C_REGNUM
) << 16;
396 h8500_set_pointer_size (int newsize
)
398 static int oldsize
= 0;
400 if (oldsize
!= newsize
)
402 printf_unfiltered ("pointer size set to %d bits\n", newsize
);
412 _initialize_gdbtypes ();
417 big_command (char *arg
, int from_tty
)
419 h8500_set_pointer_size (32);
425 medium_command (char *arg
, int from_tty
)
427 h8500_set_pointer_size (32);
433 compact_command (char *arg
, int from_tty
)
435 h8500_set_pointer_size (32);
441 small_command (char *arg
, int from_tty
)
443 h8500_set_pointer_size (16);
448 static struct cmd_list_element
*setmemorylist
;
451 set_memory (char *args
, int from_tty
)
453 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
454 help_list (setmemorylist
, "set memory ", -1, gdb_stdout
);
457 /* See if variable name is ppc or pr[0-7] */
460 h8500_is_trapped_internalvar (char *name
)
465 if (strcmp (name
+ 1, "pc") == 0)
471 && name
[3] == '\000')
478 h8500_value_of_trapped_internalvar (struct internalvar
*var
)
481 unsigned char regbuf
[4];
482 int page_regnum
, regnum
;
484 regnum
= var
->name
[2] == 'c' ? PC_REGNUM
: var
->name
[2] - '0';
486 switch (var
->name
[2])
489 page_regnum
= SEG_C_REGNUM
;
495 page_regnum
= SEG_D_REGNUM
;
499 page_regnum
= SEG_E_REGNUM
;
503 page_regnum
= SEG_T_REGNUM
;
507 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, page_regnum
, NULL
);
508 regval
= regbuf
[0] << 16;
510 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, regnum
, NULL
);
511 regval
|= regbuf
[0] << 8 | regbuf
[1]; /* XXX host/target byte order */
513 xfree (var
->value
); /* Free up old value */
515 var
->value
= value_from_longest (builtin_type_unsigned_long
, regval
);
516 release_value (var
->value
); /* Unchain new value */
518 VALUE_LVAL (var
->value
) = lval_internalvar
;
519 VALUE_INTERNALVAR (var
->value
) = var
;
524 h8500_set_trapped_internalvar (struct internalvar
*var
, value_ptr newval
,
525 int bitpos
, int bitsize
, int offset
)
527 char *page_regnum
, *regnum
;
528 char expression
[100];
531 enum type_code newval_type_code
;
533 type
= check_typedef (VALUE_TYPE (newval
));
534 newval_type_code
= TYPE_CODE (type
);
536 if ((newval_type_code
!= TYPE_CODE_INT
537 && newval_type_code
!= TYPE_CODE_PTR
)
538 || TYPE_LENGTH (type
) != sizeof (new_regval
))
539 error ("Illegal type (%s) for assignment to $%s\n",
540 TYPE_NAME (VALUE_TYPE (newval
)), var
->name
);
542 new_regval
= *(long *) VALUE_CONTENTS_RAW (newval
);
544 regnum
= var
->name
+ 1;
546 switch (var
->name
[2])
567 sprintf (expression
, "$%s=%d", page_regnum
, new_regval
>> 16);
568 parse_and_eval (expression
);
570 sprintf (expression
, "$%s=%d", regnum
, new_regval
& 0xffff);
571 parse_and_eval (expression
);
577 return read_register (PR7_REGNUM
);
581 h8500_write_sp (CORE_ADDR v
)
583 write_register (PR7_REGNUM
, v
);
587 h8500_read_pc (int pid
)
589 return read_register (PC_REGNUM
);
593 h8500_write_pc (CORE_ADDR v
, int pid
)
595 write_register (PC_REGNUM
, v
);
601 return read_register (PR6_REGNUM
);
605 h8500_write_fp (CORE_ADDR v
)
607 write_register (PR6_REGNUM
, v
);
611 _initialize_h8500_tdep (void)
613 tm_print_insn
= print_insn_h8500
;
615 add_prefix_cmd ("memory", no_class
, set_memory
,
616 "set the memory model", &setmemorylist
, "set memory ", 0,
619 add_cmd ("small", class_support
, small_command
,
620 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist
);
622 add_cmd ("big", class_support
, big_command
,
623 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist
);
625 add_cmd ("medium", class_support
, medium_command
,
626 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist
);
628 add_cmd ("compact", class_support
, compact_command
,
629 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist
);