1 /* Target-dependent code for Atmel AVR, for GDB.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Contributed by Theodore A. Roth, troth@openavr.org */
24 /* Portions of this file were taken from the original gdb-4.18 patch developed
25 by Denis Chertykov, denisc@overta.ru */
32 #include "arch-utils.h"
34 #include "gdb_string.h"
38 (AVR micros are pure Harvard Architecture processors.)
40 The AVR family of microcontrollers have three distinctly different memory
41 spaces: flash, sram and eeprom. The flash is 16 bits wide and is used for
42 the most part to store program instructions. The sram is 8 bits wide and is
43 used for the stack and the heap. Some devices lack sram and some can have
44 an additional external sram added on as a peripheral.
46 The eeprom is 8 bits wide and is used to store data when the device is
47 powered down. Eeprom is not directly accessible, it can only be accessed
48 via io-registers using a special algorithm. Accessing eeprom via gdb's
49 remote serial protocol ('m' or 'M' packets) looks difficult to do and is
50 not included at this time.
52 [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or
53 written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''. For this to
54 work, the remote target must be able to handle eeprom accesses and perform
55 the address translation.]
57 All three memory spaces have physical addresses beginning at 0x0. In
58 addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit
59 bytes instead of the 16 bit wide words used by the real device for the
62 In order for remote targets to work correctly, extra bits must be added to
63 addresses before they are send to the target or received from the target
64 via the remote serial protocol. The extra bits are the MSBs and are used to
65 decode which memory space the address is referring to. */
68 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
71 #define EXTRACT_INSN(addr) extract_unsigned_integer(addr,2)
73 /* Constants: prefixed with AVR_ to avoid name space clashes */
87 AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
88 AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
90 AVR_PC_REG_INDEX = 35, /* index into array of registers */
92 AVR_MAX_PROLOGUE_SIZE = 56, /* bytes */
94 /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */
97 /* Number of the last pushed register. r17 for current avr-gcc */
98 AVR_LAST_PUSHED_REGNUM = 17,
100 /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8
101 bits? Do these have to match the bfd vma values?. It sure would make
102 things easier in the future if they didn't need to match.
104 Note: I chose these values so as to be consistent with bfd vma
107 TRoth/2002-04-08: There is already a conflict with very large programs
108 in the mega128. The mega128 has 128K instruction bytes (64K words),
109 thus the Most Significant Bit is 0x10000 which gets masked off my
112 The problem manifests itself when trying to set a breakpoint in a
113 function which resides in the upper half of the instruction space and
114 thus requires a 17-bit address.
116 For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK
117 from 0x00ff0000 to 0x00f00000. Eeprom is not accessible from gdb yet,
118 but could be for some remote targets by just adding the correct offset
119 to the address and letting the remote target handle the low-level
120 details of actually accessing the eeprom. */
122 AVR_IMEM_START = 0x00000000, /* INSN memory */
123 AVR_SMEM_START = 0x00800000, /* SRAM memory */
125 /* No eeprom mask defined */
126 AVR_MEM_MASK = 0x00f00000, /* mask to determine memory space */
128 AVR_EMEM_START = 0x00810000, /* EEPROM memory */
129 AVR_MEM_MASK = 0x00ff0000, /* mask to determine memory space */
133 /* Any function with a frame looks like this
134 ....... <-SP POINTS HERE
135 LOCALS1 <-FP POINTS HERE
144 struct frame_extra_info
147 CORE_ADDR args_pointer;
156 /* FIXME: TRoth: is there anything to put here? */
160 /* Lookup the name of a register given it's number. */
163 avr_register_name (int regnum)
165 static char *register_names[] = {
166 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
167 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
168 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
169 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
174 if (regnum >= (sizeof (register_names) / sizeof (*register_names)))
176 return register_names[regnum];
179 /* Return the GDB type object for the "standard" data type
180 of data in register N. */
183 avr_register_type (struct gdbarch *gdbarch, int reg_nr)
185 if (reg_nr == AVR_PC_REGNUM)
186 return builtin_type_uint32;
188 if (reg_nr == AVR_SP_REGNUM)
189 return builtin_type_void_data_ptr;
191 return builtin_type_uint8;
194 /* Instruction address checks and convertions. */
197 avr_make_iaddr (CORE_ADDR x)
199 return ((x) | AVR_IMEM_START);
203 avr_iaddr_p (CORE_ADDR x)
205 return (((x) & AVR_MEM_MASK) == AVR_IMEM_START);
208 /* FIXME: TRoth: Really need to use a larger mask for instructions. Some
209 devices are already up to 128KBytes of flash space.
211 TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */
214 avr_convert_iaddr_to_raw (CORE_ADDR x)
216 return ((x) & 0xffffffff);
219 /* SRAM address checks and convertions. */
222 avr_make_saddr (CORE_ADDR x)
224 return ((x) | AVR_SMEM_START);
228 avr_saddr_p (CORE_ADDR x)
230 return (((x) & AVR_MEM_MASK) == AVR_SMEM_START);
234 avr_convert_saddr_to_raw (CORE_ADDR x)
236 return ((x) & 0xffffffff);
239 /* EEPROM address checks and convertions. I don't know if these will ever
240 actually be used, but I've added them just the same. TRoth */
242 /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
243 programs in the mega128. */
245 /* static CORE_ADDR */
246 /* avr_make_eaddr (CORE_ADDR x) */
248 /* return ((x) | AVR_EMEM_START); */
252 /* avr_eaddr_p (CORE_ADDR x) */
254 /* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
257 /* static CORE_ADDR */
258 /* avr_convert_eaddr_to_raw (CORE_ADDR x) */
260 /* return ((x) & 0xffffffff); */
263 /* Convert from address to pointer and vice-versa. */
266 avr_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
268 /* Is it a code address? */
269 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
270 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
272 store_unsigned_integer (buf, TYPE_LENGTH (type),
273 avr_convert_iaddr_to_raw (addr >> 1));
277 /* Strip off any upper segment bits. */
278 store_unsigned_integer (buf, TYPE_LENGTH (type),
279 avr_convert_saddr_to_raw (addr));
284 avr_pointer_to_address (struct type *type, const void *buf)
286 CORE_ADDR addr = extract_unsigned_integer (buf, TYPE_LENGTH (type));
288 /* Is it a code address? */
289 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
290 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
291 || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
292 return avr_make_iaddr (addr << 1);
294 return avr_make_saddr (addr);
298 avr_read_pc (ptid_t ptid)
304 save_ptid = inferior_ptid;
305 inferior_ptid = ptid;
306 pc = (int) read_register (AVR_PC_REGNUM);
307 inferior_ptid = save_ptid;
308 retval = avr_make_iaddr (pc);
313 avr_write_pc (CORE_ADDR val, ptid_t ptid)
317 save_ptid = inferior_ptid;
318 inferior_ptid = ptid;
319 write_register (AVR_PC_REGNUM, avr_convert_iaddr_to_raw (val));
320 inferior_ptid = save_ptid;
326 return (avr_make_saddr (read_register (AVR_SP_REGNUM)));
330 avr_write_sp (CORE_ADDR val)
332 write_register (AVR_SP_REGNUM, avr_convert_saddr_to_raw (val));
340 fp = read_register (AVR_FP_REGNUM);
341 fp += (read_register (AVR_FP_REGNUM+1) << 8);
343 return (avr_make_saddr (fp));
346 /* avr_scan_prologue is also used as the
347 deprecated_frame_init_saved_regs().
349 Put here the code to store, into fi->saved_regs, the addresses of
350 the saved registers of frame described by FRAME_INFO. This
351 includes special registers such as pc and fp saved in special ways
352 in the stack frame. sp is even more special: the address we return
353 for it IS the sp for the next frame. */
355 /* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info)
356 This function decodes a AVR function prologue to determine:
357 1) the size of the stack frame
358 2) which registers are saved on it
359 3) the offsets of saved regs
360 This information is stored in the "extra_info" field of the frame_info.
362 A typical AVR function prologue might look like this:
368 sbiw r28,<LOCALS_SIZE>
369 in __tmp_reg__,__SREG__
372 out __SREG__,__tmp_reg__
375 A `-mcall-prologues' prologue look like this:
376 ldi r26,<LOCALS_SIZE>
377 ldi r27,<LOCALS_SIZE>/265
378 ldi r30,pm_lo8(.L_foo_body)
379 ldi r31,pm_hi8(.L_foo_body)
380 rjmp __prologue_saves__+RRR
384 avr_scan_prologue (struct frame_info *fi)
386 CORE_ADDR prologue_start;
387 CORE_ADDR prologue_end;
393 struct minimal_symbol *msymbol;
395 unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
398 get_frame_extra_info (fi)->framereg = AVR_SP_REGNUM;
400 if (find_pc_partial_function
401 (get_frame_pc (fi), &name, &prologue_start, &prologue_end))
403 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
405 if (sal.line == 0) /* no line info, use current PC */
406 prologue_end = get_frame_pc (fi);
407 else if (sal.end < prologue_end) /* next line begins after fn end */
408 prologue_end = sal.end; /* (probably means no prologue) */
411 /* We're in the boondocks: allow for */
412 /* 19 pushes, an add, and "mv fp,sp" */
413 prologue_end = prologue_start + AVR_MAX_PROLOGUE_SIZE;
415 prologue_end = min (prologue_end, get_frame_pc (fi));
417 /* Search the prologue looking for instructions that set up the
418 frame pointer, adjust the stack pointer, and save registers. */
420 get_frame_extra_info (fi)->framesize = 0;
421 prologue_len = min (prologue_end - prologue_start, AVR_MAX_PROLOGUE_SIZE);
422 read_memory (prologue_start, prologue, prologue_len);
424 /* Scanning main()'s prologue
425 ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
426 ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
430 if (name && strcmp ("main", name) == 0 && prologue_len == 8)
433 unsigned char img[] = {
434 0xde, 0xbf, /* out __SP_H__,r29 */
435 0xcd, 0xbf /* out __SP_L__,r28 */
438 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
439 insn = EXTRACT_INSN (&prologue[vpc]);
440 /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
441 if ((insn & 0xf0f0) == 0xe0c0)
443 locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
444 insn = EXTRACT_INSN (&prologue[vpc + 2]);
445 /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
446 if ((insn & 0xf0f0) == 0xe0d0)
448 locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
449 if (memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
451 deprecated_update_frame_base_hack (fi, locals);
453 get_frame_extra_info (fi)->is_main = 1;
460 /* Scanning `-mcall-prologues' prologue
461 FIXME: mega prologue have a 12 bytes long */
463 while (prologue_len <= 12) /* I'm use while to avoit many goto's */
469 insn = EXTRACT_INSN (&prologue[vpc]);
470 /* ldi r26,<LOCALS_SIZE> */
471 if ((insn & 0xf0f0) != 0xe0a0)
473 loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
475 insn = EXTRACT_INSN (&prologue[vpc + 2]);
476 /* ldi r27,<LOCALS_SIZE> / 256 */
477 if ((insn & 0xf0f0) != 0xe0b0)
479 loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
481 insn = EXTRACT_INSN (&prologue[vpc + 4]);
482 /* ldi r30,pm_lo8(.L_foo_body) */
483 if ((insn & 0xf0f0) != 0xe0e0)
485 body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
487 insn = EXTRACT_INSN (&prologue[vpc + 6]);
488 /* ldi r31,pm_hi8(.L_foo_body) */
489 if ((insn & 0xf0f0) != 0xe0f0)
491 body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
493 if (body_addr != (prologue_start + 10) / 2)
496 msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
500 /* FIXME: prologue for mega have a JMP instead of RJMP */
501 insn = EXTRACT_INSN (&prologue[vpc + 8]);
502 /* rjmp __prologue_saves__+RRR */
503 if ((insn & 0xf000) != 0xc000)
506 /* Extract PC relative offset from RJMP */
507 i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
508 /* Convert offset to byte addressable mode */
510 /* Destination address */
511 i += vpc + prologue_start + 10;
512 /* Resovle offset (in words) from __prologue_saves__ symbol.
513 Which is a pushes count in `-mcall-prologues' mode */
514 num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2;
516 if (num_pushes > AVR_MAX_PUSHES)
522 get_frame_saved_regs (fi)[AVR_FP_REGNUM + 1] = num_pushes;
524 get_frame_saved_regs (fi)[AVR_FP_REGNUM] = num_pushes - 1;
526 for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
527 from <= AVR_LAST_PUSHED_REGNUM; ++from)
528 get_frame_saved_regs (fi)[from] = ++i;
530 get_frame_extra_info (fi)->locals_size = loc_size;
531 get_frame_extra_info (fi)->framesize = loc_size + num_pushes;
532 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
536 /* Scan interrupt or signal function */
538 if (prologue_len >= 12)
540 unsigned char img[] = {
541 0x78, 0x94, /* sei */
542 0x1f, 0x92, /* push r1 */
543 0x0f, 0x92, /* push r0 */
544 0x0f, 0xb6, /* in r0,0x3f SREG */
545 0x0f, 0x92, /* push r0 */
546 0x11, 0x24 /* clr r1 */
548 if (memcmp (prologue, img, sizeof (img)) == 0)
551 get_frame_saved_regs (fi)[0] = 2;
552 get_frame_saved_regs (fi)[1] = 1;
553 get_frame_extra_info (fi)->framesize += 3;
555 else if (memcmp (img + 1, prologue, sizeof (img) - 1) == 0)
557 vpc += sizeof (img) - 1;
558 get_frame_saved_regs (fi)[0] = 2;
559 get_frame_saved_regs (fi)[1] = 1;
560 get_frame_extra_info (fi)->framesize += 3;
564 /* First stage of the prologue scanning.
567 for (; vpc <= prologue_len; vpc += 2)
569 insn = EXTRACT_INSN (&prologue[vpc]);
570 if ((insn & 0xfe0f) == 0x920f) /* push rXX */
572 /* Bits 4-9 contain a mask for registers R0-R32. */
573 regno = (insn & 0x1f0) >> 4;
574 ++get_frame_extra_info (fi)->framesize;
575 get_frame_saved_regs (fi)[regno] = get_frame_extra_info (fi)->framesize;
582 /* Second stage of the prologue scanning.
587 if (scan_stage == 1 && vpc + 4 <= prologue_len)
589 unsigned char img[] = {
590 0xcd, 0xb7, /* in r28,__SP_L__ */
591 0xde, 0xb7 /* in r29,__SP_H__ */
593 unsigned short insn1;
595 if (memcmp (prologue + vpc, img, sizeof (img)) == 0)
598 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
603 /* Third stage of the prologue scanning. (Really two stages)
605 sbiw r28,XX or subi r28,lo8(XX)
607 in __tmp_reg__,__SREG__
610 out __SREG__,__tmp_reg__
613 if (scan_stage == 2 && vpc + 12 <= prologue_len)
616 unsigned char img[] = {
617 0x0f, 0xb6, /* in r0,0x3f */
618 0xf8, 0x94, /* cli */
619 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
620 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
621 0xde, 0xbf /* out 0x3e,r29 ; SPH */
623 unsigned char img_sig[] = {
624 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
625 0xde, 0xbf /* out 0x3e,r29 ; SPH */
627 unsigned char img_int[] = {
628 0xf8, 0x94, /* cli */
629 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
630 0x78, 0x94, /* sei */
631 0xde, 0xbf /* out 0x3e,r29 ; SPH */
634 insn = EXTRACT_INSN (&prologue[vpc]);
636 if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */
637 locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
638 else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
640 locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
641 insn = EXTRACT_INSN (&prologue[vpc]);
643 locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4) << 8);
647 get_frame_extra_info (fi)->locals_size = locals_size;
648 get_frame_extra_info (fi)->framesize += locals_size;
652 /* This function actually figures out the frame address for a given pc and
653 sp. This is tricky because we sometimes don't use an explicit
654 frame pointer, and the previous stack pointer isn't necessarily recorded
655 on the stack. The only reliable way to get this info is to
656 examine the prologue. */
659 avr_init_extra_frame_info (int fromleaf, struct frame_info *fi)
663 if (get_next_frame (fi))
664 deprecated_update_frame_pc_hack (fi, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi)));
666 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
667 frame_saved_regs_zalloc (fi);
669 get_frame_extra_info (fi)->return_pc = 0;
670 get_frame_extra_info (fi)->args_pointer = 0;
671 get_frame_extra_info (fi)->locals_size = 0;
672 get_frame_extra_info (fi)->framereg = 0;
673 get_frame_extra_info (fi)->framesize = 0;
674 get_frame_extra_info (fi)->is_main = 0;
676 avr_scan_prologue (fi);
678 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
679 get_frame_base (fi)))
681 /* We need to setup fi->frame here because call_function_by_hand
682 gets it wrong by assuming it's always FP. */
683 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), get_frame_base (fi),
686 else if (!get_next_frame (fi))
687 /* this is the innermost frame? */
688 deprecated_update_frame_base_hack (fi, read_register (get_frame_extra_info (fi)->framereg));
689 else if (get_frame_extra_info (fi)->is_main != 1)
690 /* not the innermost frame, not `main' */
691 /* If we have an next frame, the callee saved it. */
693 struct frame_info *next_fi = get_next_frame (fi);
694 if (get_frame_extra_info (fi)->framereg == AVR_SP_REGNUM)
695 deprecated_update_frame_base_hack (fi, (get_frame_base (next_fi)
697 + get_frame_extra_info (next_fi)->framesize));
698 /* FIXME: I don't analyse va_args functions */
703 unsigned int fp_low, fp_high;
705 /* Scan all frames */
706 for (; next_fi; next_fi = get_next_frame (next_fi))
708 /* look for saved AVR_FP_REGNUM */
709 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM] && !fp)
710 fp = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM];
711 /* look for saved AVR_FP_REGNUM + 1 */
712 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1] && !fp1)
713 fp1 = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1];
715 fp_low = (fp ? read_memory_unsigned_integer (avr_make_saddr (fp), 1)
716 : read_register (AVR_FP_REGNUM)) & 0xff;
718 (fp1 ? read_memory_unsigned_integer (avr_make_saddr (fp1), 1) :
719 read_register (AVR_FP_REGNUM + 1)) & 0xff;
720 deprecated_update_frame_base_hack (fi, fp_low | (fp_high << 8));
724 /* TRoth: Do we want to do this if we are in main? I don't think we should
725 since return_pc makes no sense when we are in main. */
727 if ((get_frame_pc (fi)) && (get_frame_extra_info (fi)->is_main == 0))
728 /* We are not in CALL_DUMMY */
733 addr = get_frame_base (fi) + get_frame_extra_info (fi)->framesize + 1;
735 /* Return address in stack in different endianness */
737 get_frame_extra_info (fi)->return_pc =
738 read_memory_unsigned_integer (avr_make_saddr (addr), 1) << 8;
739 get_frame_extra_info (fi)->return_pc |=
740 read_memory_unsigned_integer (avr_make_saddr (addr + 1), 1);
742 /* This return address in words,
743 must be converted to the bytes address */
744 get_frame_extra_info (fi)->return_pc *= 2;
746 /* Resolve a pushed registers addresses */
747 for (i = 0; i < NUM_REGS; i++)
749 if (get_frame_saved_regs (fi)[i])
750 get_frame_saved_regs (fi)[i] = addr - get_frame_saved_regs (fi)[i];
755 /* Restore the machine to the state it had before the current frame was
756 created. Usually used either by the "RETURN" command, or by
757 call_function_by_hand after the dummy_frame is finished. */
764 struct frame_info *frame = get_current_frame ();
766 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
767 get_frame_base (frame),
768 get_frame_base (frame)))
770 generic_pop_dummy_frame ();
774 /* TRoth: Why only loop over 8 registers? */
776 for (regnum = 0; regnum < 8; regnum++)
778 /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the
779 actual value we want, not the address of the value we want. */
780 if (get_frame_saved_regs (frame)[regnum] && regnum != AVR_SP_REGNUM)
782 saddr = avr_make_saddr (get_frame_saved_regs (frame)[regnum]);
783 write_register (regnum,
784 read_memory_unsigned_integer (saddr, 1));
786 else if (get_frame_saved_regs (frame)[regnum] && regnum == AVR_SP_REGNUM)
787 write_register (regnum, get_frame_base (frame) + 2);
790 /* Don't forget the update the PC too! */
791 write_pc (get_frame_extra_info (frame)->return_pc);
793 flush_cached_frames ();
796 /* Return the saved PC from this frame. */
799 avr_frame_saved_pc (struct frame_info *frame)
801 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
802 get_frame_base (frame),
803 get_frame_base (frame)))
804 return deprecated_read_register_dummy (get_frame_pc (frame),
805 get_frame_base (frame),
808 return get_frame_extra_info (frame)->return_pc;
812 avr_saved_pc_after_call (struct frame_info *frame)
814 unsigned char m1, m2;
815 unsigned int sp = read_register (AVR_SP_REGNUM);
816 m1 = read_memory_unsigned_integer (avr_make_saddr (sp + 1), 1);
817 m2 = read_memory_unsigned_integer (avr_make_saddr (sp + 2), 1);
818 return (m2 | (m1 << 8)) * 2;
821 /* Returns the return address for a dummy. */
824 avr_call_dummy_address (void)
826 return entry_point_address ();
829 /* Setup the return address for a dummy frame, as called by
830 call_function_by_hand. Only necessary when you are using an empty
834 avr_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
836 unsigned char buf[2];
839 struct minimal_symbol *msymbol;
846 write_memory (sp + 1, buf, 2);
849 /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a
850 left-over from Denis' original patch which used avr-mon for the target
851 instead of the generic remote target. */
852 if ((strcmp (target_shortname, "avr-mon") == 0)
853 && (msymbol = lookup_minimal_symbol ("gdb_break", NULL, NULL)))
855 mon_brk = SYMBOL_VALUE_ADDRESS (msymbol);
856 store_unsigned_integer (buf, wordsize, mon_brk / 2);
858 write_memory (sp + 1, buf + 1, 1);
859 write_memory (sp + 2, buf, 1);
866 avr_skip_prologue (CORE_ADDR pc)
868 CORE_ADDR func_addr, func_end;
869 struct symtab_and_line sal;
871 /* See what the symbol table says */
873 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
875 sal = find_pc_line (func_addr, 0);
877 /* troth/2002-08-05: For some very simple functions, gcc doesn't
878 generate a prologue and the sal.end ends up being the 2-byte ``ret''
879 instruction at the end of the function, but func_end ends up being
880 the address of the first instruction of the _next_ function. By
881 adjusting func_end by 2 bytes, we can catch these functions and not
882 return sal.end if it is the ``ret'' instruction. */
884 if (sal.line != 0 && sal.end < (func_end-2))
888 /* Either we didn't find the start of this function (nothing we can do),
889 or there's no line info, or the line after the prologue is after
890 the end of the function (there probably isn't a prologue). */
896 avr_frame_address (struct frame_info *fi)
898 return avr_make_saddr (get_frame_base (fi));
901 /* Given a GDB frame, determine the address of the calling function's
902 frame. This will be used to create a new GDB frame struct, and
903 then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
904 will be called for the new frame.
906 For us, the frame address is its stack pointer value, so we look up
907 the function prologue to determine the caller's sp value, and return it. */
910 avr_frame_chain (struct frame_info *frame)
912 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
913 get_frame_base (frame),
914 get_frame_base (frame)))
916 /* initialize the return_pc now */
917 get_frame_extra_info (frame)->return_pc
918 = deprecated_read_register_dummy (get_frame_pc (frame),
919 get_frame_base (frame),
921 return get_frame_base (frame);
923 return (get_frame_extra_info (frame)->is_main ? 0
924 : get_frame_base (frame) + get_frame_extra_info (frame)->framesize + 2 /* ret addr */ );
927 /* Store the address of the place in which to copy the structure the
928 subroutine will return. This is called from call_function.
930 We store structs through a pointer passed in the first Argument
934 avr_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
936 write_register (0, addr);
939 /* Setup the function arguments for calling a function in the inferior.
941 On the AVR architecture, there are 18 registers (R25 to R8) which are
942 dedicated for passing function arguments. Up to the first 18 arguments
943 (depending on size) may go into these registers. The rest go on the stack.
945 Arguments that are larger than WORDSIZE bytes will be split between two or
946 more registers as available, but will NOT be split between a register and
949 An exceptional case exists for struct arguments (and possibly other
950 aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
951 not a multiple of WORDSIZE bytes. In this case the argument is never split
952 between the registers and the stack, but instead is copied in its entirety
953 onto the stack, AND also copied into as many registers as there is room
954 for. In other words, space in registers permitting, two copies of the same
955 argument are passed in. As far as I can tell, only the one on the stack is
956 used, although that may be a function of the level of compiler
957 optimization. I suspect this is a compiler bug. Arguments of these odd
958 sizes are left-justified within the word (as opposed to arguments smaller
959 than WORDSIZE bytes, which are right-justified).
961 If the function is to return an aggregate type such as a struct, the caller
962 must allocate space into which the callee will copy the return value. In
963 this case, a pointer to the return value location is passed into the callee
964 in register R0, which displaces one of the other arguments passed in via
965 registers R0 to R2. */
968 avr_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
969 int struct_return, CORE_ADDR struct_addr)
971 int stack_alloc, stack_offset;
983 /* Now make sure there's space on the stack */
984 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
985 stack_alloc += TYPE_LENGTH (VALUE_TYPE (args[argnum]));
986 sp -= stack_alloc; /* make room on stack for args */
987 /* we may over-allocate a little here, but that won't hurt anything */
990 if (struct_return) /* "struct return" pointer takes up one argreg */
992 write_register (--argreg, struct_addr);
995 /* Now load as many as possible of the first arguments into registers, and
996 push the rest onto the stack. There are 3N bytes in three registers
997 available. Loop thru args from first to last. */
999 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
1001 type = VALUE_TYPE (args[argnum]);
1002 len = TYPE_LENGTH (type);
1003 val = (char *) VALUE_CONTENTS (args[argnum]);
1005 /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because
1006 some *&^%$ things get passed on the stack AND in the registers! */
1008 { /* there's room in registers */
1010 regval = extract_unsigned_integer (val + len, wordsize);
1011 write_register (argreg--, regval);
1017 /* Not all avr devices support the BREAK insn. Those that don't should treat
1018 it as a NOP. Thus, it should be ok. Since the avr is currently a remote
1019 only target, this shouldn't be a problem (I hope). TRoth/2003-05-14 */
1021 static const unsigned char *
1022 avr_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
1024 static unsigned char avr_break_insn [] = { 0x98, 0x95 };
1025 *lenptr = sizeof (avr_break_insn);
1026 return avr_break_insn;
1029 /* Initialize the gdbarch structure for the AVR's. */
1031 static struct gdbarch *
1032 avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1034 struct gdbarch *gdbarch;
1035 struct gdbarch_tdep *tdep;
1037 /* Find a candidate among the list of pre-declared architectures. */
1038 arches = gdbarch_list_lookup_by_info (arches, &info);
1040 return arches->gdbarch;
1042 /* None found, create a new architecture from the information provided. */
1043 tdep = XMALLOC (struct gdbarch_tdep);
1044 gdbarch = gdbarch_alloc (&info, tdep);
1046 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1047 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1048 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1050 /* If we ever need to differentiate the device types, do it here. */
1051 switch (info.bfd_arch_info->mach)
1061 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1062 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1063 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1064 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1065 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1066 set_gdbarch_addr_bit (gdbarch, 32);
1067 set_gdbarch_bfd_vma_bit (gdbarch, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */
1069 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1070 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1071 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1073 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
1074 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little);
1075 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_single_little);
1077 set_gdbarch_read_pc (gdbarch, avr_read_pc);
1078 set_gdbarch_write_pc (gdbarch, avr_write_pc);
1079 set_gdbarch_deprecated_target_read_fp (gdbarch, avr_read_fp);
1080 set_gdbarch_read_sp (gdbarch, avr_read_sp);
1081 set_gdbarch_deprecated_dummy_write_sp (gdbarch, avr_write_sp);
1083 set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
1085 set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
1086 set_gdbarch_deprecated_fp_regnum (gdbarch, AVR_FP_REGNUM);
1087 set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
1089 set_gdbarch_register_name (gdbarch, avr_register_name);
1090 set_gdbarch_register_type (gdbarch, avr_register_type);
1092 set_gdbarch_print_insn (gdbarch, print_insn_avr);
1094 set_gdbarch_call_dummy_address (gdbarch, avr_call_dummy_address);
1096 set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
1097 set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
1098 set_gdbarch_deprecated_push_arguments (gdbarch, avr_push_arguments);
1099 set_gdbarch_deprecated_push_return_address (gdbarch, avr_push_return_address);
1100 set_gdbarch_deprecated_pop_frame (gdbarch, avr_pop_frame);
1102 set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
1103 set_gdbarch_deprecated_store_struct_return (gdbarch, avr_store_struct_return);
1105 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, avr_scan_prologue);
1106 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, avr_init_extra_frame_info);
1107 set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
1108 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1110 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1111 set_gdbarch_breakpoint_from_pc (gdbarch, avr_breakpoint_from_pc);
1113 set_gdbarch_function_start_offset (gdbarch, 0);
1115 set_gdbarch_frame_args_skip (gdbarch, 0);
1116 set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue); /* ??? */
1117 set_gdbarch_deprecated_frame_chain (gdbarch, avr_frame_chain);
1118 set_gdbarch_deprecated_frame_saved_pc (gdbarch, avr_frame_saved_pc);
1119 set_gdbarch_frame_args_address (gdbarch, avr_frame_address);
1120 set_gdbarch_frame_locals_address (gdbarch, avr_frame_address);
1121 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, avr_saved_pc_after_call);
1126 /* Send a query request to the avr remote target asking for values of the io
1127 registers. If args parameter is not NULL, then the user has requested info
1128 on a specific io register [This still needs implemented and is ignored for
1129 now]. The query string should be one of these forms:
1131 "Ravr.io_reg" -> reply is "NN" number of io registers
1133 "Ravr.io_reg:addr,len" where addr is first register and len is number of
1134 registers to be read. The reply should be "<NAME>,VV;" for each io register
1135 where, <NAME> is a string, and VV is the hex value of the register.
1137 All io registers are 8-bit. */
1140 avr_io_reg_read_command (char *args, int from_tty)
1146 unsigned int nreg = 0;
1150 if (!current_target.to_query)
1152 fprintf_unfiltered (gdb_stderr,
1153 "ERR: info io_registers NOT supported by current "
1158 /* Just get the maximum buffer size. */
1159 target_query ((int) 'R', 0, 0, &bufsiz);
1160 if (bufsiz > sizeof (buf))
1161 bufsiz = sizeof (buf);
1163 /* Find out how many io registers the target has. */
1164 strcpy (query, "avr.io_reg");
1165 target_query ((int) 'R', query, buf, &bufsiz);
1167 if (strncmp (buf, "", bufsiz) == 0)
1169 fprintf_unfiltered (gdb_stderr,
1170 "info io_registers NOT supported by target\n");
1174 if (sscanf (buf, "%x", &nreg) != 1)
1176 fprintf_unfiltered (gdb_stderr,
1177 "Error fetching number of io registers\n");
1181 reinitialize_more_filter ();
1183 printf_unfiltered ("Target has %u io registers:\n\n", nreg);
1185 /* only fetch up to 8 registers at a time to keep the buffer small */
1188 for (i = 0; i < nreg; i += step)
1190 /* how many registers this round? */
1193 j = nreg - i; /* last block is less than 8 registers */
1195 snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
1196 target_query ((int) 'R', query, buf, &bufsiz);
1199 for (k = i; k < (i + j); k++)
1201 if (sscanf (p, "%[^,],%x;", query, &val) == 2)
1203 printf_filtered ("[%02x] %-15s : %02x\n", k, query, val);
1204 while ((*p != ';') && (*p != '\0'))
1206 p++; /* skip over ';' */
1214 extern initialize_file_ftype _initialize_avr_tdep; /* -Wmissing-prototypes */
1217 _initialize_avr_tdep (void)
1219 register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
1221 /* Add a new command to allow the user to query the avr remote target for
1222 the values of the io space registers in a saner way than just using
1225 /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
1226 io_registers' to signify it is not available on other platforms. */
1228 add_cmd ("io_registers", class_info, avr_io_reg_read_command,
1229 "query remote avr target for io space register values", &infolist);