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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Contributed by Steve Chamberlain
35 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
37 static int code_size = 2;
39 static int data_size = 2;
41 /* Shape of an H8/500 frame :
47 return address <2 or 4 bytes>
56 /* an easy to debug H8 stack frame looks like:
60 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
65 #define IS_PUSH(x) (((x) & 0xff00)==0x6d00)
66 #define IS_LINK_8(x) ((x) == 0x17)
67 #define IS_LINK_16(x) ((x) == 0x1f)
68 #define IS_MOVE_FP(x) ((x) == 0x0d76)
69 #define IS_MOV_SP_FP(x) ((x) == 0x0d76)
70 #define IS_SUB2_SP(x) ((x) == 0x1b87)
71 #define IS_MOVK_R5(x) ((x) == 0x7905)
72 #define IS_SUB_R5SP(x) ((x) == 0x1957)
80 h8500_skip_prologue (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);
101 /* Given a GDB frame, determine the address of the calling function's frame.
102 This will be used to create a new GDB frame struct, and then
103 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
105 For us, the frame address is its stack pointer value, so we look up
106 the function prologue to determine the caller's sp value, and return it. */
109 h8500_frame_chain (thisframe)
110 struct frame_info *thisframe;
112 if (!inside_entry_file (thisframe->pc))
113 return (read_memory_integer (FRAME_FP (thisframe), PTR_SIZE));
118 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
119 is not the address of a valid instruction, the address of the next
120 instruction beyond ADDR otherwise. *PWORD1 receives the first word
121 of the instruction.*/
124 NEXT_PROLOGUE_INSN (addr, lim, pword1)
131 read_memory (addr, pword1, 1);
132 read_memory (addr, pword1 + 1, 1);
138 /* Examine the prologue of a function. `ip' points to the first
139 instruction. `limit' is the limit of the prologue (e.g. the addr
140 of the first linenumber, or perhaps the program counter if we're
141 stepping through). `frame_sp' is the stack pointer value in use in
142 this frame. `fsr' is a pointer to a frame_saved_regs structure
143 into which we put info about the registers saved by this frame.
144 `fi' is a struct frame_info pointer; we fill in various fields in
145 it to reflect the offsets of the arg pointer and the locals
148 /* Return the saved PC from this frame. */
151 frame_saved_pc (frame)
152 struct frame_info *frame;
154 return read_memory_integer (FRAME_FP (frame) + 2, PTR_SIZE);
161 struct frame_saved_regs fsr;
162 struct frame_info *frame = get_current_frame ();
164 get_frame_saved_regs (frame, &fsr);
166 for (regnum = 0; regnum < 8; regnum++)
168 if (fsr.regs[regnum])
169 write_register (regnum, read_memory_short (fsr.regs[regnum]));
171 flush_cached_frames ();
176 print_register_hook (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 (regno)
260 h8500_register_virtual_type (regno)
269 return builtin_type_unsigned_char;
279 return builtin_type_unsigned_short;
289 return builtin_type_unsigned_long;
295 /* Put here the code to store, into a struct frame_saved_regs,
296 the addresses of the saved registers of frame described by FRAME_INFO.
297 This includes special registers such as pc and fp saved in special
298 ways in the stack frame. sp is even more special:
299 the address we return for it IS the sp for the next frame. */
302 frame_find_saved_regs (frame_info, frame_saved_regs)
303 struct frame_info *frame_info;
304 struct frame_saved_regs *frame_saved_regs;
307 register int regmask;
308 register CORE_ADDR next_addr;
309 register CORE_ADDR pc;
310 unsigned char thebyte;
312 memset (frame_saved_regs, '\0', sizeof *frame_saved_regs);
314 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
315 && (frame_info)->pc <= (frame_info)->frame)
317 next_addr = (frame_info)->frame;
318 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
322 pc = get_pc_function_start ((frame_info)->pc);
323 /* Verify we have a link a6 instruction next;
324 if not we lose. If we win, find the address above the saved
325 regs using the amount of storage from the link instruction.
328 thebyte = read_memory_integer (pc, 1);
330 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
331 else if (0x17 == thebyte)
332 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
337 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
338 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
339 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
343 thebyte = read_memory_integer (pc, 1);
348 regmask = read_memory_integer (pc, 1);
350 for (regnum = 0; regnum < 8; regnum++, regmask >>= 1)
354 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
357 thebyte = read_memory_integer (pc, 1);
359 /* Maybe got a load of pushes */
360 while (thebyte == 0xbf)
363 regnum = read_memory_integer (pc, 1) & 0x7;
365 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
366 thebyte = read_memory_integer (pc, 1);
371 /* Remember the address of the frame pointer */
372 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
374 /* This is where the old sp is hidden */
375 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
377 /* And the PC - remember the pushed FP is always two bytes long */
378 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
382 saved_pc_after_call ()
385 int a = read_register (SP_REGNUM);
387 x = read_memory_integer (a, code_size);
390 /* Stick current code segement onto top */
392 x |= read_register (SEG_C_REGNUM) << 16;
398 #if 0 /* never called */
399 /* Nonzero if instruction at PC is a return instruction. */
405 int b1 = read_memory_integer (pc, 1);
409 case 0x14: /* rtd #8 */
410 case 0x1c: /* rtd #16 */
416 int b2 = read_memory_integer (pc + 1, 1);
419 case 0x18: /* prts */
420 case 0x14: /* prtd #8 */
421 case 0x16: /* prtd #16 */
431 h8500_set_pointer_size (newsize)
434 static int oldsize = 0;
436 if (oldsize != newsize)
438 printf_unfiltered ("pointer size set to %d bits\n", newsize);
448 _initialize_gdbtypes ();
455 h8500_set_pointer_size (32);
463 h8500_set_pointer_size (32);
471 h8500_set_pointer_size (32);
479 h8500_set_pointer_size (16);
484 static struct cmd_list_element *setmemorylist;
487 set_memory (args, from_tty)
491 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
492 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
495 /* See if variable name is ppc or pr[0-7] */
498 h8500_is_trapped_internalvar (name)
504 if (strcmp (name + 1, "pc") == 0)
510 && name[3] == '\000')
517 h8500_value_of_trapped_internalvar (var)
518 struct internalvar *var;
521 unsigned char regbuf[4];
522 int page_regnum, regnum;
524 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
526 switch (var->name[2])
529 page_regnum = SEG_C_REGNUM;
535 page_regnum = SEG_D_REGNUM;
539 page_regnum = SEG_E_REGNUM;
543 page_regnum = SEG_T_REGNUM;
547 get_saved_register (regbuf, NULL, NULL, selected_frame, page_regnum, NULL);
548 regval = regbuf[0] << 16;
550 get_saved_register (regbuf, NULL, NULL, selected_frame, regnum, NULL);
551 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
553 free (var->value); /* Free up old value */
555 var->value = value_from_longest (builtin_type_unsigned_long, regval);
556 release_value (var->value); /* Unchain new value */
558 VALUE_LVAL (var->value) = lval_internalvar;
559 VALUE_INTERNALVAR (var->value) = var;
564 h8500_set_trapped_internalvar (var, newval, bitpos, bitsize, offset)
565 struct internalvar *var;
566 int offset, bitpos, bitsize;
569 char *page_regnum, *regnum;
570 char expression[100];
573 enum type_code newval_type_code;
575 type = VALUE_TYPE (newval);
576 newval_type_code = TYPE_CODE (type);
578 if ((newval_type_code != TYPE_CODE_INT
579 && newval_type_code != TYPE_CODE_PTR)
580 || TYPE_LENGTH (type) != sizeof (new_regval))
581 error ("Illegal type (%s) for assignment to $%s\n",
582 TYPE_NAME (type), var->name);
584 new_regval = *(long *) VALUE_CONTENTS_RAW (newval);
586 regnum = var->name + 1;
588 switch (var->name[2])
609 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
610 parse_and_eval (expression);
612 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
613 parse_and_eval (expression);
619 return read_register (PR7_REGNUM);
626 write_register (PR7_REGNUM, v);
633 return read_register (PC_REGNUM);
637 h8500_write_pc (v, pid)
641 write_register (PC_REGNUM, v);
647 return read_register (PR6_REGNUM);
654 write_register (PR6_REGNUM, v);
658 _initialize_h8500_tdep ()
660 tm_print_insn = print_insn_h8500;
662 add_prefix_cmd ("memory", no_class, set_memory,
663 "set the memory model", &setmemorylist, "set memory ", 0,
666 add_cmd ("small", class_support, small_command,
667 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist);
669 add_cmd ("big", class_support, big_command,
670 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist);
672 add_cmd ("medium", class_support, medium_command,
673 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist);
675 add_cmd ("compact", class_support, compact_command,
676 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist);