1 /* Target-dependent code for Hitachi H8/500, for GDB.
2 Copyright 1993, 1994, 1995, 2001 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
37 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
39 static int code_size
= 2;
41 static int data_size
= 2;
43 /* Shape of an H8/500 frame :
49 return address <2 or 4 bytes>
58 /* an easy to debug H8 stack frame looks like:
62 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
67 #define IS_PUSH(x) (((x) & 0xff00)==0x6d00)
68 #define IS_LINK_8(x) ((x) == 0x17)
69 #define IS_LINK_16(x) ((x) == 0x1f)
70 #define IS_MOVE_FP(x) ((x) == 0x0d76)
71 #define IS_MOV_SP_FP(x) ((x) == 0x0d76)
72 #define IS_SUB2_SP(x) ((x) == 0x1b87)
73 #define IS_MOVK_R5(x) ((x) == 0x7905)
74 #define IS_SUB_R5SP(x) ((x) == 0x1957)
82 h8500_skip_prologue (CORE_ADDR start_pc
)
86 w
= read_memory_integer (start_pc
, 1);
90 w
= read_memory_integer (start_pc
, 1);
96 w
= read_memory_integer (start_pc
, 2);
103 h8500_addr_bits_remove (CORE_ADDR addr
)
105 return ((addr
) & 0xffffff);
108 /* Given a GDB frame, determine the address of the calling function's frame.
109 This will be used to create a new GDB frame struct, and then
110 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
112 For us, the frame address is its stack pointer value, so we look up
113 the function prologue to determine the caller's sp value, and return it. */
116 h8500_frame_chain (struct frame_info
*thisframe
)
118 if (!inside_entry_file (thisframe
->pc
))
119 return (read_memory_integer (FRAME_FP (thisframe
), PTR_SIZE
));
124 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
125 is not the address of a valid instruction, the address of the next
126 instruction beyond ADDR otherwise. *PWORD1 receives the first word
127 of the instruction. */
130 NEXT_PROLOGUE_INSN (CORE_ADDR addr
, CORE_ADDR lim
, char *pword1
)
134 read_memory (addr
, pword1
, 1);
135 read_memory (addr
, pword1
+ 1, 1);
141 /* Examine the prologue of a function. `ip' points to the first
142 instruction. `limit' is the limit of the prologue (e.g. the addr
143 of the first linenumber, or perhaps the program counter if we're
144 stepping through). `frame_sp' is the stack pointer value in use in
145 this frame. `fsr' is a pointer to a frame_saved_regs structure
146 into which we put info about the registers saved by this frame.
147 `fi' is a struct frame_info pointer; we fill in various fields in
148 it to reflect the offsets of the arg pointer and the locals
151 /* Return the saved PC from this frame. */
154 frame_saved_pc (struct frame_info
*frame
)
156 return read_memory_integer (FRAME_FP (frame
) + 2, PTR_SIZE
);
160 h8500_pop_frame (void)
163 struct frame_saved_regs fsr
;
164 struct frame_info
*frame
= get_current_frame ();
166 get_frame_saved_regs (frame
, &fsr
);
168 for (regnum
= 0; regnum
< 8; regnum
++)
170 if (fsr
.regs
[regnum
])
171 write_register (regnum
, read_memory_short (fsr
.regs
[regnum
]));
173 flush_cached_frames ();
178 print_register_hook (int regno
)
180 if (regno
== CCR_REGNUM
)
188 read_relative_register_raw_bytes (regno
, b
);
190 printf_unfiltered ("\t");
191 printf_unfiltered ("I-%d - ", (l
& 0x80) != 0);
196 printf_unfiltered ("N-%d ", N
);
197 printf_unfiltered ("Z-%d ", Z
);
198 printf_unfiltered ("V-%d ", V
);
199 printf_unfiltered ("C-%d ", C
);
201 printf_unfiltered ("u> ");
203 printf_unfiltered ("u<= ");
205 printf_unfiltered ("u>= ");
207 printf_unfiltered ("u< ");
209 printf_unfiltered ("!= ");
211 printf_unfiltered ("== ");
213 printf_unfiltered (">= ");
215 printf_unfiltered ("< ");
216 if ((Z
| (N
^ V
)) == 0)
217 printf_unfiltered ("> ");
218 if ((Z
| (N
^ V
)) == 1)
219 printf_unfiltered ("<= ");
224 h8500_register_size (int regno
)
255 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
260 h8500_register_virtual_type (int regno
)
268 return builtin_type_unsigned_char
;
278 return builtin_type_unsigned_short
;
288 return builtin_type_unsigned_long
;
290 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
294 /* Put here the code to store, into a struct frame_saved_regs,
295 the addresses of the saved registers of frame described by FRAME_INFO.
296 This includes special registers such as pc and fp saved in special
297 ways in the stack frame. sp is even more special:
298 the address we return for it IS the sp for the next frame. */
301 frame_find_saved_regs (struct frame_info
*frame_info
,
302 struct frame_saved_regs
*frame_saved_regs
)
305 register int regmask
;
306 register CORE_ADDR next_addr
;
307 register CORE_ADDR pc
;
308 unsigned char thebyte
;
310 memset (frame_saved_regs
, '\0', sizeof *frame_saved_regs
);
312 if ((frame_info
)->pc
>= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4
313 && (frame_info
)->pc
<= (frame_info
)->frame
)
315 next_addr
= (frame_info
)->frame
;
316 pc
= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4;
320 pc
= get_pc_function_start ((frame_info
)->pc
);
321 /* Verify we have a link a6 instruction next;
322 if not we lose. If we win, find the address above the saved
323 regs using the amount of storage from the link instruction.
326 thebyte
= read_memory_integer (pc
, 1);
328 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 2), pc
+= 2;
329 else if (0x17 == thebyte
)
330 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 1), pc
+= 1;
335 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
336 if ((0x0c0177777 & read_memory_integer (pc
, 2)) == 0157774)
337 next_addr
+= read_memory_integer (pc
+= 2, 4), pc
+= 4;
341 thebyte
= read_memory_integer (pc
, 1);
346 regmask
= read_memory_integer (pc
, 1);
348 for (regnum
= 0; regnum
< 8; regnum
++, regmask
>>= 1)
352 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
355 thebyte
= read_memory_integer (pc
, 1);
357 /* Maybe got a load of pushes */
358 while (thebyte
== 0xbf)
361 regnum
= read_memory_integer (pc
, 1) & 0x7;
363 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
364 thebyte
= read_memory_integer (pc
, 1);
369 /* Remember the address of the frame pointer */
370 (frame_saved_regs
)->regs
[FP_REGNUM
] = (frame_info
)->frame
;
372 /* This is where the old sp is hidden */
373 (frame_saved_regs
)->regs
[SP_REGNUM
] = (frame_info
)->frame
;
375 /* And the PC - remember the pushed FP is always two bytes long */
376 (frame_saved_regs
)->regs
[PC_REGNUM
] = (frame_info
)->frame
+ 2;
380 saved_pc_after_call (void)
383 int a
= read_register (SP_REGNUM
);
385 x
= read_memory_integer (a
, code_size
);
388 /* Stick current code segement onto top */
390 x
|= read_register (SEG_C_REGNUM
) << 16;
397 h8500_set_pointer_size (int newsize
)
399 static int oldsize
= 0;
401 if (oldsize
!= newsize
)
403 printf_unfiltered ("pointer size set to %d bits\n", newsize
);
413 _initialize_gdbtypes ();
418 big_command (char *arg
, int from_tty
)
420 h8500_set_pointer_size (32);
426 medium_command (char *arg
, int from_tty
)
428 h8500_set_pointer_size (32);
434 compact_command (char *arg
, int from_tty
)
436 h8500_set_pointer_size (32);
442 small_command (char *arg
, int from_tty
)
444 h8500_set_pointer_size (16);
449 static struct cmd_list_element
*setmemorylist
;
452 set_memory (char *args
, int from_tty
)
454 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
455 help_list (setmemorylist
, "set memory ", -1, gdb_stdout
);
458 /* See if variable name is ppc or pr[0-7] */
461 h8500_is_trapped_internalvar (char *name
)
466 if (strcmp (name
+ 1, "pc") == 0)
472 && name
[3] == '\000')
479 h8500_value_of_trapped_internalvar (struct internalvar
*var
)
482 unsigned char regbuf
[4];
483 int page_regnum
, regnum
;
485 regnum
= var
->name
[2] == 'c' ? PC_REGNUM
: var
->name
[2] - '0';
487 switch (var
->name
[2])
490 page_regnum
= SEG_C_REGNUM
;
496 page_regnum
= SEG_D_REGNUM
;
500 page_regnum
= SEG_E_REGNUM
;
504 page_regnum
= SEG_T_REGNUM
;
508 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, page_regnum
, NULL
);
509 regval
= regbuf
[0] << 16;
511 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, regnum
, NULL
);
512 regval
|= regbuf
[0] << 8 | regbuf
[1]; /* XXX host/target byte order */
514 xfree (var
->value
); /* Free up old value */
516 var
->value
= value_from_longest (builtin_type_unsigned_long
, regval
);
517 release_value (var
->value
); /* Unchain new value */
519 VALUE_LVAL (var
->value
) = lval_internalvar
;
520 VALUE_INTERNALVAR (var
->value
) = var
;
525 h8500_set_trapped_internalvar (struct internalvar
*var
, value_ptr newval
,
526 int bitpos
, int bitsize
, int offset
)
528 char *page_regnum
, *regnum
;
529 char expression
[100];
532 enum type_code newval_type_code
;
534 type
= check_typedef (VALUE_TYPE (newval
));
535 newval_type_code
= TYPE_CODE (type
);
537 if ((newval_type_code
!= TYPE_CODE_INT
538 && newval_type_code
!= TYPE_CODE_PTR
)
539 || TYPE_LENGTH (type
) != sizeof (new_regval
))
540 error ("Illegal type (%s) for assignment to $%s\n",
541 TYPE_NAME (VALUE_TYPE (newval
)), var
->name
);
543 new_regval
= *(long *) VALUE_CONTENTS_RAW (newval
);
545 regnum
= var
->name
+ 1;
547 switch (var
->name
[2])
568 sprintf (expression
, "$%s=%d", page_regnum
, new_regval
>> 16);
569 parse_and_eval (expression
);
571 sprintf (expression
, "$%s=%d", regnum
, new_regval
& 0xffff);
572 parse_and_eval (expression
);
578 return read_register (PR7_REGNUM
);
582 h8500_write_sp (CORE_ADDR v
)
584 write_register (PR7_REGNUM
, v
);
588 h8500_read_pc (int pid
)
590 return read_register (PC_REGNUM
);
594 h8500_write_pc (CORE_ADDR v
, int pid
)
596 write_register (PC_REGNUM
, v
);
602 return read_register (PR6_REGNUM
);
606 h8500_write_fp (CORE_ADDR v
)
608 write_register (PR6_REGNUM
, v
);
612 _initialize_h8500_tdep (void)
614 tm_print_insn
= print_insn_h8500
;
616 add_prefix_cmd ("memory", no_class
, set_memory
,
617 "set the memory model", &setmemorylist
, "set memory ", 0,
620 add_cmd ("small", class_support
, small_command
,
621 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist
);
623 add_cmd ("big", class_support
, big_command
,
624 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist
);
626 add_cmd ("medium", class_support
, medium_command
,
627 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist
);
629 add_cmd ("compact", class_support
, compact_command
,
630 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist
);