1 /* Target-machine dependent code for Hitachi H8/500, for GDB.
2 Copyright (C) 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Contributed by Steve Chamberlain
33 #include "../opcodes/h8500-opc.h"
36 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
40 /* Shape of an H8/500 frame :
47 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)
79 CORE_ADDR examine_prologue ();
81 void frame_find_saved_regs ();
85 h8500_skip_prologue (start_pc)
91 w = read_memory_integer (start_pc, 1);
95 w = read_memory_integer (start_pc, 1);
101 w = read_memory_integer (start_pc, 2);
108 print_insn (memaddr, stream)
112 disassemble_info info;
113 GDB_INIT_DISASSEMBLE_INFO (info, stream);
114 return print_insn_h8500 (memaddr, &info);
117 /* Given a GDB frame, determine the address of the calling function's frame.
118 This will be used to create a new GDB frame struct, and then
119 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
121 For us, the frame address is its stack pointer value, so we look up
122 the function prologue to determine the caller's sp value, and return it. */
125 h8500_frame_chain (thisframe)
128 if (!inside_entry_file (thisframe->pc))
129 return (read_memory_integer (FRAME_FP (thisframe), PTR_SIZE));
135 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
136 is not the address of a valid instruction, the address of the next
137 instruction beyond ADDR otherwise. *PWORD1 receives the first word
138 of the instruction.*/
141 NEXT_PROLOGUE_INSN (addr, lim, pword1)
148 read_memory (addr, pword1, 1);
149 read_memory (addr, pword1 + 1, 1);
155 /* Examine the prologue of a function. `ip' points to the first instruction.
156 `limit' is the limit of the prologue (e.g. the addr of the first
157 linenumber, or perhaps the program counter if we're stepping through).
158 `frame_sp' is the stack pointer value in use in this frame.
159 `fsr' is a pointer to a frame_saved_regs structure into which we put
160 info about the registers saved by this frame.
161 `fi' is a struct frame_info pointer; we fill in various fields in it
162 to reflect the offsets of the arg pointer and the locals pointer. */
165 /* Return the saved PC from this frame. */
168 frame_saved_pc (frame)
171 return read_memory_integer ((frame)->frame + 2, PTR_SIZE);
175 frame_locals_address (fi)
176 struct frame_info *fi;
181 /* Return the address of the argument block for the frame
182 described by FI. Returns 0 if the address is unknown. */
185 frame_args_address (fi)
186 struct frame_info *fi;
195 struct frame_saved_regs fsr;
196 struct frame_info *fi;
198 FRAME frame = get_current_frame ();
200 fi = get_frame_info (frame);
201 get_frame_saved_regs (fi, &fsr);
203 for (regnum = 0; regnum < 8; regnum++)
205 if (fsr.regs[regnum])
207 write_register (regnum, read_memory_short (fsr.regs[regnum]));
210 flush_cached_frames ();
216 print_register_hook (regno)
218 if (regno == CCR_REGNUM)
226 read_relative_register_raw_bytes (regno, b);
228 printf_unfiltered ("\t");
229 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
234 printf_unfiltered ("N-%d ", N);
235 printf_unfiltered ("Z-%d ", Z);
236 printf_unfiltered ("V-%d ", V);
237 printf_unfiltered ("C-%d ", C);
239 printf_unfiltered ("u> ");
241 printf_unfiltered ("u<= ");
243 printf_unfiltered ("u>= ");
245 printf_unfiltered ("u< ");
247 printf_unfiltered ("!= ");
249 printf_unfiltered ("== ");
251 printf_unfiltered (">= ");
253 printf_unfiltered ("< ");
254 if ((Z | (N ^ V)) == 0)
255 printf_unfiltered ("> ");
256 if ((Z | (N ^ V)) == 1)
257 printf_unfiltered ("<= ");
262 h8500_register_size (regno)
296 h8500_register_virtual_type (regno)
305 return builtin_type_unsigned_char;
315 return builtin_type_unsigned_short;
325 return builtin_type_unsigned_long;
331 /* Put here the code to store, into a struct frame_saved_regs,
332 the addresses of the saved registers of frame described by FRAME_INFO.
333 This includes special registers such as pc and fp saved in special
334 ways in the stack frame. sp is even more special:
335 the address we return for it IS the sp for the next frame. */
338 frame_find_saved_regs (frame_info, frame_saved_regs)
339 struct frame_info *frame_info;
340 struct frame_saved_regs *frame_saved_regs;
344 register int regmask;
345 register CORE_ADDR next_addr;
346 register CORE_ADDR pc;
347 unsigned char thebyte;
349 memset (frame_saved_regs, '\0', sizeof *frame_saved_regs);
351 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
352 && (frame_info)->pc <= (frame_info)->frame)
354 next_addr = (frame_info)->frame;
355 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
359 pc = get_pc_function_start ((frame_info)->pc);
360 /* Verify we have a link a6 instruction next;
361 if not we lose. If we win, find the address above the saved
362 regs using the amount of storage from the link instruction.
365 thebyte = read_memory_integer (pc, 1);
367 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
368 else if (0x17 == thebyte)
369 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
374 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
375 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
376 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
380 thebyte = read_memory_integer (pc, 1);
385 regmask = read_memory_integer (pc, 1);
387 for (regnum = 0; regnum < 8; regnum++, regmask >>= 1)
391 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
394 thebyte = read_memory_integer (pc, 1);
396 /* Maybe got a load of pushes */
397 while (thebyte == 0xbf)
400 regnum = read_memory_integer (pc, 1) & 0x7;
402 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
403 thebyte = read_memory_integer (pc, 1);
408 /* Remember the address of the frame pointer */
409 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
411 /* This is where the old sp is hidden */
412 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
414 /* And the PC - remember the pushed FP is always two bytes long */
415 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
418 saved_pc_after_call (frame)
421 int a = read_register (SP_REGNUM);
422 x = read_memory_integer (a, code_size);
425 /* Stick current code segement onto top */
427 x |= read_register (SEG_C_REGNUM) << 16;
434 /* Nonzero if instruction at PC is a return instruction. */
438 int b1 = read_memory_integer (pc, 1);
442 case 0x14: /* rtd #8 */
443 case 0x1c: /* rtd #16 */
449 int b2 = read_memory_integer (pc + 1, 1);
452 case 0x18: /* prts */
453 case 0x14: /* prtd #8 */
454 case 0x16: /* prtd #16 */
464 h8500_set_pointer_size (newsize)
467 static int oldsize = 0;
469 if (oldsize != newsize)
471 printf_unfiltered ("pointer size set to %d bits\n", newsize);
481 _initialize_gdbtypes ();
486 struct cmd_list_element *setmemorylist;
489 #define C(name,a,b,c) name () { h8500_set_pointer_size(a); code_size = b; data_size = c; }
491 C(big_command, 32,4,4);
492 C(medium_command, 32, 4,2);
493 C(compact_command, 32,2,4);
494 C(small_command, 16,2,2);
497 set_memory (args, from_tty)
501 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
502 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
505 /* See if variable name is ppc or pr[0-7] */
508 h8500_is_trapped_internalvar (name)
514 if (strcmp (name + 1, "pc") == 0)
520 && name[3] == '\000')
527 h8500_value_of_trapped_internalvar (var)
528 struct internalvar *var;
531 unsigned char regbuf[4];
532 int page_regnum, regnum;
534 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
536 switch (var->name[2])
539 page_regnum = SEG_C_REGNUM;
545 page_regnum = SEG_D_REGNUM;
549 page_regnum = SEG_E_REGNUM;
553 page_regnum = SEG_T_REGNUM;
557 get_saved_register (regbuf, NULL, NULL, selected_frame, page_regnum, NULL);
558 regval = regbuf[0] << 16;
560 get_saved_register (regbuf, NULL, NULL, selected_frame, regnum, NULL);
561 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
563 free (var->value); /* Free up old value */
565 var->value = value_from_longest (builtin_type_unsigned_long, regval);
566 release_value (var->value); /* Unchain new value */
568 VALUE_LVAL (var->value) = lval_internalvar;
569 VALUE_INTERNALVAR (var->value) = var;
574 h8500_set_trapped_internalvar (var, newval, bitpos, bitsize, offset)
575 struct internalvar *var;
576 int offset, bitpos, bitsize;
579 char *page_regnum, *regnum;
580 char expression[100];
583 enum type_code newval_type_code;
585 type = VALUE_TYPE (newval);
586 newval_type_code = TYPE_CODE (type);
588 if ((newval_type_code != TYPE_CODE_INT
589 && newval_type_code != TYPE_CODE_PTR)
590 || TYPE_LENGTH (type) != sizeof (new_regval))
591 error ("Illegal type (%s) for assignment to $%s\n",
592 TYPE_NAME (type), var->name);
594 new_regval = *(long *) VALUE_CONTENTS_RAW (newval);
596 regnum = var->name + 1;
598 switch (var->name[2])
619 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
620 parse_and_eval (expression);
622 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
623 parse_and_eval (expression);
627 _initialize_h8500_tdep ()
629 add_prefix_cmd ("memory", no_class, set_memory,
630 "set the memory model", &setmemorylist, "set memory ", 0,
633 add_cmd ("small", class_support, small_command,
634 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist);
636 add_cmd ("big", class_support, big_command,
637 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist);
639 add_cmd ("medium", class_support, medium_command,
640 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist);
642 add_cmd ("compact", class_support, compact_command,
643 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist);
650 return read_register (PR7_REGNUM);
657 write_register (PR7_REGNUM, v);
663 return read_register (PC_REGNUM);
670 write_register (PC_REGNUM, v);
676 return read_register (PR6_REGNUM);
683 write_register (PR6_REGNUM, v);