2003-01-15 David Carlton <carlton@math.stanford.edu>
[deliverable/binutils-gdb.git] / gdb / avr-tdep.c
CommitLineData
8818c391 1/* Target-dependent code for Atmel AVR, for GDB.
51603483 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
8818c391
TR
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22/* Contributed by Theodore A. Roth, troth@verinet.com */
23
24/* Portions of this file were taken from the original gdb-4.18 patch developed
25 by Denis Chertykov, denisc@overta.ru */
26
27#include "defs.h"
28#include "gdbcmd.h"
29#include "gdbcore.h"
30#include "inferior.h"
31#include "symfile.h"
32#include "arch-utils.h"
33#include "regcache.h"
5f8a3188 34#include "gdb_string.h"
8818c391
TR
35
36/* AVR Background:
37
38 (AVR micros are pure Harvard Architecture processors.)
39
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.
45
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.
51
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.]
56
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
60 Program Counter.
61
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. */
66
67#undef XMALLOC
68#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
69
70#undef EXTRACT_INSN
71#define EXTRACT_INSN(addr) extract_unsigned_integer(addr,2)
72
73/* Constants: prefixed with AVR_ to avoid name space clashes */
74
75enum
2e5ff58c
TR
76{
77 AVR_REG_W = 24,
78 AVR_REG_X = 26,
79 AVR_REG_Y = 28,
80 AVR_FP_REGNUM = 28,
81 AVR_REG_Z = 30,
82
83 AVR_SREG_REGNUM = 32,
84 AVR_SP_REGNUM = 33,
85 AVR_PC_REGNUM = 34,
86
87 AVR_NUM_REGS = 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
88 AVR_NUM_REG_BYTES = 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
89
90 AVR_PC_REG_INDEX = 35, /* index into array of registers */
91
92 AVR_MAX_PROLOGUE_SIZE = 56, /* bytes */
93
94 /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */
95 AVR_MAX_PUSHES = 18,
96
97 /* Number of the last pushed register. r17 for current avr-gcc */
98 AVR_LAST_PUSHED_REGNUM = 17,
99
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.
103
104 Note: I chose these values so as to be consistent with bfd vma
105 addresses.
106
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
110 AVR_MEM_MASK.
111
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.
115
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. */
121
122 AVR_IMEM_START = 0x00000000, /* INSN memory */
123 AVR_SMEM_START = 0x00800000, /* SRAM memory */
8818c391 124#if 1
2e5ff58c
TR
125 /* No eeprom mask defined */
126 AVR_MEM_MASK = 0x00f00000, /* mask to determine memory space */
8818c391 127#else
2e5ff58c
TR
128 AVR_EMEM_START = 0x00810000, /* EEPROM memory */
129 AVR_MEM_MASK = 0x00ff0000, /* mask to determine memory space */
8818c391 130#endif
2e5ff58c 131};
8818c391
TR
132
133/* Any function with a frame looks like this
134 ....... <-SP POINTS HERE
135 LOCALS1 <-FP POINTS HERE
136 LOCALS0
137 SAVED FP
138 SAVED R3
139 SAVED R2
140 RET PC
141 FIRST ARG
142 SECOND ARG */
143
144struct frame_extra_info
2e5ff58c
TR
145{
146 CORE_ADDR return_pc;
147 CORE_ADDR args_pointer;
148 int locals_size;
149 int framereg;
150 int framesize;
151 int is_main;
152};
8818c391
TR
153
154struct gdbarch_tdep
2e5ff58c
TR
155{
156 /* FIXME: TRoth: is there anything to put here? */
157 int foo;
158};
8818c391
TR
159
160/* Lookup the name of a register given it's number. */
161
fa88f677 162static const char *
8818c391
TR
163avr_register_name (int regnum)
164{
2e5ff58c
TR
165 static char *register_names[] = {
166 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
167 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
8818c391
TR
168 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
169 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
170 "SREG", "SP", "PC"
171 };
172 if (regnum < 0)
173 return NULL;
174 if (regnum >= (sizeof (register_names) / sizeof (*register_names)))
175 return NULL;
176 return register_names[regnum];
177}
178
179/* Index within `registers' of the first byte of the space for
180 register REGNUM. */
181
182static int
183avr_register_byte (int regnum)
184{
185 if (regnum < AVR_PC_REGNUM)
186 return regnum;
187 else
188 return AVR_PC_REG_INDEX;
189}
190
191/* Number of bytes of storage in the actual machine representation for
192 register REGNUM. */
193
194static int
195avr_register_raw_size (int regnum)
196{
197 switch (regnum)
198 {
199 case AVR_PC_REGNUM:
200 return 4;
201 case AVR_SP_REGNUM:
202 case AVR_FP_REGNUM:
203 return 2;
204 default:
205 return 1;
206 }
207}
208
209/* Number of bytes of storage in the program's representation
210 for register N. */
211
212static int
213avr_register_virtual_size (int regnum)
214{
215 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum));
216}
217
218/* Return the GDB type object for the "standard" data type
219 of data in register N. */
220
221static struct type *
222avr_register_virtual_type (int regnum)
223{
224 switch (regnum)
225 {
226 case AVR_PC_REGNUM:
227 return builtin_type_unsigned_long;
228 case AVR_SP_REGNUM:
229 return builtin_type_unsigned_short;
230 default:
231 return builtin_type_unsigned_char;
232 }
233}
234
235/* Instruction address checks and convertions. */
236
237static CORE_ADDR
238avr_make_iaddr (CORE_ADDR x)
239{
240 return ((x) | AVR_IMEM_START);
241}
242
243static int
244avr_iaddr_p (CORE_ADDR x)
245{
246 return (((x) & AVR_MEM_MASK) == AVR_IMEM_START);
247}
248
249/* FIXME: TRoth: Really need to use a larger mask for instructions. Some
250 devices are already up to 128KBytes of flash space.
251
252 TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */
253
254static CORE_ADDR
255avr_convert_iaddr_to_raw (CORE_ADDR x)
256{
257 return ((x) & 0xffffffff);
258}
259
260/* SRAM address checks and convertions. */
261
262static CORE_ADDR
263avr_make_saddr (CORE_ADDR x)
264{
265 return ((x) | AVR_SMEM_START);
266}
267
268static int
269avr_saddr_p (CORE_ADDR x)
270{
271 return (((x) & AVR_MEM_MASK) == AVR_SMEM_START);
272}
273
274static CORE_ADDR
275avr_convert_saddr_to_raw (CORE_ADDR x)
276{
277 return ((x) & 0xffffffff);
278}
279
280/* EEPROM address checks and convertions. I don't know if these will ever
281 actually be used, but I've added them just the same. TRoth */
282
283/* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
284 programs in the mega128. */
285
286/* static CORE_ADDR */
287/* avr_make_eaddr (CORE_ADDR x) */
288/* { */
289/* return ((x) | AVR_EMEM_START); */
290/* } */
291
292/* static int */
293/* avr_eaddr_p (CORE_ADDR x) */
294/* { */
295/* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
296/* } */
297
298/* static CORE_ADDR */
299/* avr_convert_eaddr_to_raw (CORE_ADDR x) */
300/* { */
301/* return ((x) & 0xffffffff); */
302/* } */
303
304/* Convert from address to pointer and vice-versa. */
305
306static void
307avr_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
308{
309 /* Is it a code address? */
310 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
311 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
312 {
2e5ff58c
TR
313 store_unsigned_integer (buf, TYPE_LENGTH (type),
314 avr_convert_iaddr_to_raw (addr));
8818c391
TR
315 }
316 else
317 {
318 /* Strip off any upper segment bits. */
2e5ff58c
TR
319 store_unsigned_integer (buf, TYPE_LENGTH (type),
320 avr_convert_saddr_to_raw (addr));
8818c391
TR
321 }
322}
323
324static CORE_ADDR
66140c26 325avr_pointer_to_address (struct type *type, const void *buf)
8818c391
TR
326{
327 CORE_ADDR addr = extract_address (buf, TYPE_LENGTH (type));
328
2e5ff58c 329 if (TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
8818c391 330 {
2e5ff58c
TR
331 fprintf_unfiltered (gdb_stderr, "CODE_SPACE ---->> ptr->addr: 0x%lx\n",
332 addr);
333 fprintf_unfiltered (gdb_stderr,
334 "+++ If you see this, please send me an email <troth@verinet.com>\n");
8818c391
TR
335 }
336
337 /* Is it a code address? */
338 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
339 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
2e5ff58c 340 || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
8818c391
TR
341 return avr_make_iaddr (addr);
342 else
343 return avr_make_saddr (addr);
344}
345
346static CORE_ADDR
347avr_read_pc (ptid_t ptid)
348{
349 ptid_t save_ptid;
350 CORE_ADDR pc;
351 CORE_ADDR retval;
352
353 save_ptid = inferior_ptid;
354 inferior_ptid = ptid;
355 pc = (int) read_register (AVR_PC_REGNUM);
356 inferior_ptid = save_ptid;
357 retval = avr_make_iaddr (pc);
358 return retval;
359}
360
361static void
362avr_write_pc (CORE_ADDR val, ptid_t ptid)
363{
364 ptid_t save_ptid;
365
366 save_ptid = inferior_ptid;
367 inferior_ptid = ptid;
368 write_register (AVR_PC_REGNUM, avr_convert_iaddr_to_raw (val));
369 inferior_ptid = save_ptid;
370}
371
372static CORE_ADDR
373avr_read_sp (void)
374{
375 return (avr_make_saddr (read_register (AVR_SP_REGNUM)));
376}
377
378static void
379avr_write_sp (CORE_ADDR val)
380{
381 write_register (AVR_SP_REGNUM, avr_convert_saddr_to_raw (val));
382}
383
384static CORE_ADDR
385avr_read_fp (void)
386{
387 return (avr_make_saddr (read_register (AVR_FP_REGNUM)));
388}
389
390/* Translate a GDB virtual ADDR/LEN into a format the remote target
391 understands. Returns number of bytes that can be transfered
392 starting at TARG_ADDR. Return ZERO if no bytes can be transfered
393 (segmentation fault).
394
395 TRoth/2002-04-08: Could this be used to check for dereferencing an invalid
396 pointer? */
397
398static void
399avr_remote_translate_xfer_address (CORE_ADDR memaddr, int nr_bytes,
2e5ff58c 400 CORE_ADDR *targ_addr, int *targ_len)
8818c391
TR
401{
402 long out_addr;
403 long out_len;
404
405 /* FIXME: TRoth: Do nothing for now. Will need to examine memaddr at this
406 point and see if the high bit are set with the masks that we want. */
407
408 *targ_addr = memaddr;
409 *targ_len = nr_bytes;
410}
411
412/* Function pointers obtained from the target are half of what gdb expects so
413 multiply by 2. */
414
415static CORE_ADDR
416avr_convert_from_func_ptr_addr (CORE_ADDR addr)
417{
418 return addr * 2;
419}
420
421/* avr_scan_prologue is also used as the frame_init_saved_regs().
422
423 Put here the code to store, into fi->saved_regs, the addresses of
424 the saved registers of frame described by FRAME_INFO. This
425 includes special registers such as pc and fp saved in special ways
426 in the stack frame. sp is even more special: the address we return
427 for it IS the sp for the next frame. */
428
429/* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info)
430 This function decodes a AVR function prologue to determine:
431 1) the size of the stack frame
432 2) which registers are saved on it
433 3) the offsets of saved regs
434 This information is stored in the "extra_info" field of the frame_info.
435
436 A typical AVR function prologue might look like this:
437 push rXX
438 push r28
439 push r29
440 in r28,__SP_L__
441 in r29,__SP_H__
442 sbiw r28,<LOCALS_SIZE>
443 in __tmp_reg__,__SREG__
444 cli
445 out __SP_L__,r28
446 out __SREG__,__tmp_reg__
447 out __SP_H__,r29
448
449 A `-mcall-prologues' prologue look like this:
450 ldi r26,<LOCALS_SIZE>
451 ldi r27,<LOCALS_SIZE>/265
452 ldi r30,pm_lo8(.L_foo_body)
453 ldi r31,pm_hi8(.L_foo_body)
454 rjmp __prologue_saves__+RRR
455 .L_foo_body: */
456
457static void
458avr_scan_prologue (struct frame_info *fi)
459{
460 CORE_ADDR prologue_start;
461 CORE_ADDR prologue_end;
2e5ff58c
TR
462 int i;
463 unsigned short insn;
464 int regno;
465 int scan_stage = 0;
466 char *name;
8818c391 467 struct minimal_symbol *msymbol;
2e5ff58c 468 int prologue_len;
8818c391
TR
469 unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
470 int vpc = 0;
471
da50a4b7 472 get_frame_extra_info (fi)->framereg = AVR_SP_REGNUM;
2e5ff58c
TR
473
474 if (find_pc_partial_function
50abf9e5 475 (get_frame_pc (fi), &name, &prologue_start, &prologue_end))
8818c391
TR
476 {
477 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
478
2e5ff58c 479 if (sal.line == 0) /* no line info, use current PC */
50abf9e5 480 prologue_end = get_frame_pc (fi);
2e5ff58c
TR
481 else if (sal.end < prologue_end) /* next line begins after fn end */
482 prologue_end = sal.end; /* (probably means no prologue) */
8818c391
TR
483 }
484 else
485 /* We're in the boondocks: allow for */
486 /* 19 pushes, an add, and "mv fp,sp" */
2e5ff58c 487 prologue_end = prologue_start + AVR_MAX_PROLOGUE_SIZE;
8818c391 488
50abf9e5 489 prologue_end = min (prologue_end, get_frame_pc (fi));
8818c391
TR
490
491 /* Search the prologue looking for instructions that set up the
492 frame pointer, adjust the stack pointer, and save registers. */
493
da50a4b7 494 get_frame_extra_info (fi)->framesize = 0;
8818c391
TR
495 prologue_len = prologue_end - prologue_start;
496 read_memory (prologue_start, prologue, prologue_len);
497
498 /* Scanning main()'s prologue
499 ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
500 ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
501 out __SP_H__,r29
502 out __SP_L__,r28 */
503
504 if (name && strcmp ("main", name) == 0 && prologue_len == 8)
505 {
506 CORE_ADDR locals;
2e5ff58c
TR
507 unsigned char img[] = {
508 0xde, 0xbf, /* out __SP_H__,r29 */
509 0xcd, 0xbf /* out __SP_L__,r28 */
8818c391
TR
510 };
511
da50a4b7 512 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
8818c391
TR
513 insn = EXTRACT_INSN (&prologue[vpc]);
514 /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
2e5ff58c
TR
515 if ((insn & 0xf0f0) == 0xe0c0)
516 {
517 locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
518 insn = EXTRACT_INSN (&prologue[vpc + 2]);
519 /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
520 if ((insn & 0xf0f0) == 0xe0d0)
521 {
522 locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
523 if (memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
524 {
8ccd593b 525 deprecated_update_frame_base_hack (fi, locals);
2e5ff58c 526
da50a4b7 527 get_frame_extra_info (fi)->is_main = 1;
2e5ff58c
TR
528 return;
529 }
530 }
531 }
8818c391 532 }
2e5ff58c 533
8818c391
TR
534 /* Scanning `-mcall-prologues' prologue
535 FIXME: mega prologue have a 12 bytes long */
536
2e5ff58c 537 while (prologue_len <= 12) /* I'm use while to avoit many goto's */
8818c391
TR
538 {
539 int loc_size;
540 int body_addr;
541 unsigned num_pushes;
2e5ff58c 542
8818c391
TR
543 insn = EXTRACT_INSN (&prologue[vpc]);
544 /* ldi r26,<LOCALS_SIZE> */
2e5ff58c
TR
545 if ((insn & 0xf0f0) != 0xe0a0)
546 break;
8818c391 547 loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
2e5ff58c 548
8818c391
TR
549 insn = EXTRACT_INSN (&prologue[vpc + 2]);
550 /* ldi r27,<LOCALS_SIZE> / 256 */
551 if ((insn & 0xf0f0) != 0xe0b0)
2e5ff58c 552 break;
8818c391 553 loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
2e5ff58c 554
8818c391
TR
555 insn = EXTRACT_INSN (&prologue[vpc + 4]);
556 /* ldi r30,pm_lo8(.L_foo_body) */
557 if ((insn & 0xf0f0) != 0xe0e0)
2e5ff58c 558 break;
8818c391
TR
559 body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
560
561 insn = EXTRACT_INSN (&prologue[vpc + 6]);
562 /* ldi r31,pm_hi8(.L_foo_body) */
563 if ((insn & 0xf0f0) != 0xe0f0)
2e5ff58c 564 break;
8818c391
TR
565 body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
566
567 if (body_addr != (prologue_start + 10) / 2)
2e5ff58c 568 break;
8818c391
TR
569
570 msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
571 if (!msymbol)
2e5ff58c 572 break;
8818c391
TR
573
574 /* FIXME: prologue for mega have a JMP instead of RJMP */
575 insn = EXTRACT_INSN (&prologue[vpc + 8]);
576 /* rjmp __prologue_saves__+RRR */
577 if ((insn & 0xf000) != 0xc000)
2e5ff58c
TR
578 break;
579
8818c391
TR
580 /* Extract PC relative offset from RJMP */
581 i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
582 /* Convert offset to byte addressable mode */
583 i *= 2;
584 /* Destination address */
585 i += vpc + prologue_start + 10;
586 /* Resovle offset (in words) from __prologue_saves__ symbol.
587 Which is a pushes count in `-mcall-prologues' mode */
588 num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2;
589
590 if (num_pushes > AVR_MAX_PUSHES)
2e5ff58c
TR
591 num_pushes = 0;
592
8818c391 593 if (num_pushes)
2e5ff58c
TR
594 {
595 int from;
b2fb4676 596 get_frame_saved_regs (fi)[AVR_FP_REGNUM + 1] = num_pushes;
2e5ff58c 597 if (num_pushes >= 2)
b2fb4676 598 get_frame_saved_regs (fi)[AVR_FP_REGNUM] = num_pushes - 1;
2e5ff58c
TR
599 i = 0;
600 for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
601 from <= AVR_LAST_PUSHED_REGNUM; ++from)
b2fb4676 602 get_frame_saved_regs (fi)[from] = ++i;
2e5ff58c 603 }
da50a4b7
AC
604 get_frame_extra_info (fi)->locals_size = loc_size;
605 get_frame_extra_info (fi)->framesize = loc_size + num_pushes;
606 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
8818c391
TR
607 return;
608 }
609
610 /* Scan interrupt or signal function */
611
612 if (prologue_len >= 12)
613 {
2e5ff58c
TR
614 unsigned char img[] = {
615 0x78, 0x94, /* sei */
616 0x1f, 0x92, /* push r1 */
617 0x0f, 0x92, /* push r0 */
618 0x0f, 0xb6, /* in r0,0x3f SREG */
619 0x0f, 0x92, /* push r0 */
620 0x11, 0x24 /* clr r1 */
8818c391
TR
621 };
622 if (memcmp (prologue, img, sizeof (img)) == 0)
2e5ff58c
TR
623 {
624 vpc += sizeof (img);
b2fb4676
AC
625 get_frame_saved_regs (fi)[0] = 2;
626 get_frame_saved_regs (fi)[1] = 1;
da50a4b7 627 get_frame_extra_info (fi)->framesize += 3;
2e5ff58c 628 }
8818c391 629 else if (memcmp (img + 1, prologue, sizeof (img) - 1) == 0)
2e5ff58c
TR
630 {
631 vpc += sizeof (img) - 1;
b2fb4676
AC
632 get_frame_saved_regs (fi)[0] = 2;
633 get_frame_saved_regs (fi)[1] = 1;
da50a4b7 634 get_frame_extra_info (fi)->framesize += 3;
2e5ff58c 635 }
8818c391
TR
636 }
637
638 /* First stage of the prologue scanning.
639 Scan pushes */
640
641 for (; vpc <= prologue_len; vpc += 2)
642 {
643 insn = EXTRACT_INSN (&prologue[vpc]);
2e5ff58c
TR
644 if ((insn & 0xfe0f) == 0x920f) /* push rXX */
645 {
646 /* Bits 4-9 contain a mask for registers R0-R32. */
647 regno = (insn & 0x1f0) >> 4;
da50a4b7
AC
648 ++get_frame_extra_info (fi)->framesize;
649 get_frame_saved_regs (fi)[regno] = get_frame_extra_info (fi)->framesize;
2e5ff58c
TR
650 scan_stage = 1;
651 }
8818c391 652 else
2e5ff58c 653 break;
8818c391
TR
654 }
655
656 /* Second stage of the prologue scanning.
657 Scan:
658 in r28,__SP_L__
659 in r29,__SP_H__ */
660
661 if (scan_stage == 1 && vpc + 4 <= prologue_len)
662 {
2e5ff58c
TR
663 unsigned char img[] = {
664 0xcd, 0xb7, /* in r28,__SP_L__ */
665 0xde, 0xb7 /* in r29,__SP_H__ */
8818c391
TR
666 };
667 unsigned short insn1;
2e5ff58c 668
8818c391 669 if (memcmp (prologue + vpc, img, sizeof (img)) == 0)
2e5ff58c
TR
670 {
671 vpc += 4;
da50a4b7 672 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
2e5ff58c
TR
673 scan_stage = 2;
674 }
8818c391
TR
675 }
676
677 /* Third stage of the prologue scanning. (Really two stages)
678 Scan for:
679 sbiw r28,XX or subi r28,lo8(XX)
680 sbci r29,hi8(XX)
681 in __tmp_reg__,__SREG__
682 cli
683 out __SP_L__,r28
684 out __SREG__,__tmp_reg__
685 out __SP_H__,r29 */
686
687 if (scan_stage == 2 && vpc + 12 <= prologue_len)
688 {
689 int locals_size = 0;
2e5ff58c
TR
690 unsigned char img[] = {
691 0x0f, 0xb6, /* in r0,0x3f */
692 0xf8, 0x94, /* cli */
693 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
694 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
695 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 696 };
2e5ff58c
TR
697 unsigned char img_sig[] = {
698 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
699 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 700 };
2e5ff58c
TR
701 unsigned char img_int[] = {
702 0xf8, 0x94, /* cli */
703 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
704 0x78, 0x94, /* sei */
705 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 706 };
2e5ff58c 707
8818c391
TR
708 insn = EXTRACT_INSN (&prologue[vpc]);
709 vpc += 2;
2e5ff58c
TR
710 if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */
711 locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
712 else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
713 {
714 locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
715 insn = EXTRACT_INSN (&prologue[vpc]);
716 vpc += 2;
717 locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4) << 8);
718 }
8818c391 719 else
2e5ff58c 720 return;
da50a4b7
AC
721 get_frame_extra_info (fi)->locals_size = locals_size;
722 get_frame_extra_info (fi)->framesize += locals_size;
8818c391
TR
723 }
724}
725
726/* This function actually figures out the frame address for a given pc and
727 sp. This is tricky because we sometimes don't use an explicit
728 frame pointer, and the previous stack pointer isn't necessarily recorded
729 on the stack. The only reliable way to get this info is to
730 examine the prologue. */
731
732static void
733avr_init_extra_frame_info (int fromleaf, struct frame_info *fi)
734{
735 int reg;
736
11c02a10
AC
737 if (get_next_frame (fi))
738 deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (get_next_frame (fi)));
8818c391 739
a00a19e9 740 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
8818c391
TR
741 frame_saved_regs_zalloc (fi);
742
da50a4b7
AC
743 get_frame_extra_info (fi)->return_pc = 0;
744 get_frame_extra_info (fi)->args_pointer = 0;
745 get_frame_extra_info (fi)->locals_size = 0;
746 get_frame_extra_info (fi)->framereg = 0;
747 get_frame_extra_info (fi)->framesize = 0;
748 get_frame_extra_info (fi)->is_main = 0;
2e5ff58c 749
8818c391
TR
750 avr_scan_prologue (fi);
751
1e2330ba
AC
752 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
753 get_frame_base (fi)))
8818c391
TR
754 {
755 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
756 by assuming it's always FP. */
1e2330ba 757 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), get_frame_base (fi),
8ccd593b 758 AVR_PC_REGNUM));
8818c391 759 }
da50a4b7
AC
760 else if (!get_next_frame (fi))
761 /* this is the innermost frame? */
762 deprecated_update_frame_base_hack (fi, read_register (get_frame_extra_info (fi)->framereg));
763 else if (get_frame_extra_info (fi)->is_main != 1)
764 /* not the innermost frame, not `main' */
8818c391
TR
765 /* If we have an next frame, the callee saved it. */
766 {
11c02a10 767 struct frame_info *next_fi = get_next_frame (fi);
da50a4b7
AC
768 if (get_frame_extra_info (fi)->framereg == AVR_SP_REGNUM)
769 deprecated_update_frame_base_hack (fi, (get_frame_base (next_fi)
770 + 2 /* ret addr */
771 + get_frame_extra_info (next_fi)->framesize));
8818c391
TR
772 /* FIXME: I don't analyse va_args functions */
773 else
2e5ff58c
TR
774 {
775 CORE_ADDR fp = 0;
776 CORE_ADDR fp1 = 0;
777 unsigned int fp_low, fp_high;
778
779 /* Scan all frames */
11c02a10 780 for (; next_fi; next_fi = get_next_frame (next_fi))
2e5ff58c
TR
781 {
782 /* look for saved AVR_FP_REGNUM */
b2fb4676
AC
783 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM] && !fp)
784 fp = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM];
2e5ff58c 785 /* look for saved AVR_FP_REGNUM + 1 */
b2fb4676
AC
786 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1] && !fp1)
787 fp1 = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1];
2e5ff58c
TR
788 }
789 fp_low = (fp ? read_memory_unsigned_integer (avr_make_saddr (fp), 1)
790 : read_register (AVR_FP_REGNUM)) & 0xff;
791 fp_high =
792 (fp1 ? read_memory_unsigned_integer (avr_make_saddr (fp1), 1) :
793 read_register (AVR_FP_REGNUM + 1)) & 0xff;
8ccd593b 794 deprecated_update_frame_base_hack (fi, fp_low | (fp_high << 8));
2e5ff58c 795 }
8818c391
TR
796 }
797
798 /* TRoth: Do we want to do this if we are in main? I don't think we should
799 since return_pc makes no sense when we are in main. */
800
da50a4b7
AC
801 if ((get_frame_pc (fi)) && (get_frame_extra_info (fi)->is_main == 0))
802 /* We are not in CALL_DUMMY */
8818c391
TR
803 {
804 CORE_ADDR addr;
805 int i;
2e5ff58c 806
da50a4b7 807 addr = get_frame_base (fi) + get_frame_extra_info (fi)->framesize + 1;
2e5ff58c 808
8818c391
TR
809 /* Return address in stack in different endianness */
810
da50a4b7 811 get_frame_extra_info (fi)->return_pc =
2e5ff58c 812 read_memory_unsigned_integer (avr_make_saddr (addr), 1) << 8;
da50a4b7 813 get_frame_extra_info (fi)->return_pc |=
2e5ff58c
TR
814 read_memory_unsigned_integer (avr_make_saddr (addr + 1), 1);
815
8818c391
TR
816 /* This return address in words,
817 must be converted to the bytes address */
da50a4b7 818 get_frame_extra_info (fi)->return_pc *= 2;
8818c391
TR
819
820 /* Resolve a pushed registers addresses */
821 for (i = 0; i < NUM_REGS; i++)
2e5ff58c 822 {
b2fb4676
AC
823 if (get_frame_saved_regs (fi)[i])
824 get_frame_saved_regs (fi)[i] = addr - get_frame_saved_regs (fi)[i];
2e5ff58c 825 }
8818c391
TR
826 }
827}
828
829/* Restore the machine to the state it had before the current frame was
830 created. Usually used either by the "RETURN" command, or by
831 call_function_by_hand after the dummy_frame is finished. */
832
833static void
834avr_pop_frame (void)
835{
836 unsigned regnum;
837 CORE_ADDR saddr;
838 struct frame_info *frame = get_current_frame ();
839
1e2330ba
AC
840 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
841 get_frame_base (frame),
842 get_frame_base (frame)))
8818c391 843 {
2e5ff58c 844 generic_pop_dummy_frame ();
8818c391
TR
845 }
846 else
847 {
848 /* TRoth: Why only loop over 8 registers? */
849
850 for (regnum = 0; regnum < 8; regnum++)
2e5ff58c
TR
851 {
852 /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the
853 actual value we want, not the address of the value we want. */
b2fb4676 854 if (get_frame_saved_regs (frame)[regnum] && regnum != AVR_SP_REGNUM)
2e5ff58c 855 {
b2fb4676 856 saddr = avr_make_saddr (get_frame_saved_regs (frame)[regnum]);
2e5ff58c
TR
857 write_register (regnum,
858 read_memory_unsigned_integer (saddr, 1));
859 }
b2fb4676 860 else if (get_frame_saved_regs (frame)[regnum] && regnum == AVR_SP_REGNUM)
1e2330ba 861 write_register (regnum, get_frame_base (frame) + 2);
2e5ff58c 862 }
8818c391
TR
863
864 /* Don't forget the update the PC too! */
da50a4b7 865 write_pc (get_frame_extra_info (frame)->return_pc);
8818c391
TR
866 }
867 flush_cached_frames ();
868}
869
870/* Return the saved PC from this frame. */
871
872static CORE_ADDR
873avr_frame_saved_pc (struct frame_info *frame)
874{
1e2330ba
AC
875 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
876 get_frame_base (frame),
877 get_frame_base (frame)))
878 return deprecated_read_register_dummy (get_frame_pc (frame),
879 get_frame_base (frame),
135c175f 880 AVR_PC_REGNUM);
8818c391 881 else
da50a4b7 882 return get_frame_extra_info (frame)->return_pc;
8818c391
TR
883}
884
885static CORE_ADDR
886avr_saved_pc_after_call (struct frame_info *frame)
887{
888 unsigned char m1, m2;
889 unsigned int sp = read_register (AVR_SP_REGNUM);
890 m1 = read_memory_unsigned_integer (avr_make_saddr (sp + 1), 1);
891 m2 = read_memory_unsigned_integer (avr_make_saddr (sp + 2), 1);
892 return (m2 | (m1 << 8)) * 2;
893}
894
895/* Figure out where in REGBUF the called function has left its return value.
896 Copy that into VALBUF. */
897
898static void
899avr_extract_return_value (struct type *type, char *regbuf, char *valbuf)
900{
901 int wordsize, len;
902
903 wordsize = 2;
904
2e5ff58c
TR
905 len = TYPE_LENGTH (type);
906
907 switch (len)
908 {
909 case 1: /* (char) */
910 case 2: /* (short), (int) */
911 memcpy (valbuf, regbuf + REGISTER_BYTE (24), 2);
912 break;
913 case 4: /* (long), (float) */
914 memcpy (valbuf, regbuf + REGISTER_BYTE (22), 4);
915 break;
916 case 8: /* (double) (doesn't seem to happen, which is good,
917 because this almost certainly isn't right. */
918 error ("I don't know how a double is returned.");
919 break;
920 }
8818c391
TR
921}
922
923/* Returns the return address for a dummy. */
924
925static CORE_ADDR
926avr_call_dummy_address (void)
927{
928 return entry_point_address ();
929}
930
931/* Place the appropriate value in the appropriate registers.
932 Primarily used by the RETURN command. */
933
934static void
935avr_store_return_value (struct type *type, char *valbuf)
936{
937 int wordsize, len, regval;
2e5ff58c 938
8818c391
TR
939 wordsize = 2;
940
2e5ff58c
TR
941 len = TYPE_LENGTH (type);
942 switch (len)
943 {
944 case 1: /* char */
945 case 2: /* short, int */
946 regval = extract_address (valbuf, len);
947 write_register (0, regval);
948 break;
949 case 4: /* long, float */
950 regval = extract_address (valbuf, len);
951 write_register (0, regval >> 16);
952 write_register (1, regval & 0xffff);
953 break;
954 case 8: /* presumeably double, but doesn't seem to happen */
955 error ("I don't know how to return a double.");
956 break;
957 }
8818c391
TR
958}
959
960/* Setup the return address for a dummy frame, as called by
961 call_function_by_hand. Only necessary when you are using an empty
962 CALL_DUMMY. */
963
964static CORE_ADDR
965avr_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
966{
967 unsigned char buf[2];
968 int wordsize = 2;
45cf40d1 969#if 0
8818c391
TR
970 struct minimal_symbol *msymbol;
971 CORE_ADDR mon_brk;
45cf40d1 972#endif
8818c391
TR
973
974 buf[0] = 0;
975 buf[1] = 0;
976 sp -= wordsize;
977 write_memory (sp + 1, buf, 2);
978
979#if 0
980 /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a
981 left-over from Denis' original patch which used avr-mon for the target
982 instead of the generic remote target. */
983 if ((strcmp (target_shortname, "avr-mon") == 0)
984 && (msymbol = lookup_minimal_symbol ("gdb_break", NULL, NULL)))
985 {
986 mon_brk = SYMBOL_VALUE_ADDRESS (msymbol);
987 store_unsigned_integer (buf, wordsize, mon_brk / 2);
988 sp -= wordsize;
989 write_memory (sp + 1, buf + 1, 1);
990 write_memory (sp + 2, buf, 1);
991 }
992#endif
993 return sp;
994}
995
996static CORE_ADDR
997avr_skip_prologue (CORE_ADDR pc)
998{
999 CORE_ADDR func_addr, func_end;
1000 struct symtab_and_line sal;
1001
1002 /* See what the symbol table says */
1003
1004 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1005 {
1006 sal = find_pc_line (func_addr, 0);
1007
ced15480
TR
1008 /* troth/2002-08-05: For some very simple functions, gcc doesn't
1009 generate a prologue and the sal.end ends up being the 2-byte ``ret''
1010 instruction at the end of the function, but func_end ends up being
1011 the address of the first instruction of the _next_ function. By
1012 adjusting func_end by 2 bytes, we can catch these functions and not
1013 return sal.end if it is the ``ret'' instruction. */
1014
1015 if (sal.line != 0 && sal.end < (func_end-2))
2e5ff58c 1016 return sal.end;
8818c391
TR
1017 }
1018
1019/* Either we didn't find the start of this function (nothing we can do),
1020 or there's no line info, or the line after the prologue is after
1021 the end of the function (there probably isn't a prologue). */
1022
1023 return pc;
1024}
1025
1026static CORE_ADDR
1027avr_frame_address (struct frame_info *fi)
1028{
1e2330ba 1029 return avr_make_saddr (get_frame_base (fi));
8818c391
TR
1030}
1031
a5afb99f
AC
1032/* Given a GDB frame, determine the address of the calling function's
1033 frame. This will be used to create a new GDB frame struct, and
1034 then INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC will be
1035 called for the new frame.
8818c391
TR
1036
1037 For us, the frame address is its stack pointer value, so we look up
1038 the function prologue to determine the caller's sp value, and return it. */
1039
1040static CORE_ADDR
1041avr_frame_chain (struct frame_info *frame)
1042{
1e2330ba
AC
1043 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
1044 get_frame_base (frame),
1045 get_frame_base (frame)))
8818c391
TR
1046 {
1047 /* initialize the return_pc now */
da50a4b7 1048 get_frame_extra_info (frame)->return_pc
1e2330ba
AC
1049 = deprecated_read_register_dummy (get_frame_pc (frame),
1050 get_frame_base (frame),
135c175f 1051 AVR_PC_REGNUM);
1e2330ba 1052 return get_frame_base (frame);
8818c391 1053 }
da50a4b7
AC
1054 return (get_frame_extra_info (frame)->is_main ? 0
1055 : get_frame_base (frame) + get_frame_extra_info (frame)->framesize + 2 /* ret addr */ );
8818c391
TR
1056}
1057
1058/* Store the address of the place in which to copy the structure the
1059 subroutine will return. This is called from call_function.
1060
1061 We store structs through a pointer passed in the first Argument
1062 register. */
1063
1064static void
1065avr_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1066{
1067 write_register (0, addr);
1068}
1069
1070/* Extract from an array REGBUF containing the (raw) register state
1071 the address in which a function should return its structure value,
1072 as a CORE_ADDR (or an expression that can be used as one). */
1073
1074static CORE_ADDR
1075avr_extract_struct_value_address (char *regbuf)
1076{
1077 return (extract_address ((regbuf) + REGISTER_BYTE (0),
2e5ff58c 1078 REGISTER_RAW_SIZE (0)) | AVR_SMEM_START);
8818c391
TR
1079}
1080
1081/* Setup the function arguments for calling a function in the inferior.
1082
1083 On the AVR architecture, there are 18 registers (R25 to R8) which are
1084 dedicated for passing function arguments. Up to the first 18 arguments
1085 (depending on size) may go into these registers. The rest go on the stack.
1086
1087 Arguments that are larger than WORDSIZE bytes will be split between two or
1088 more registers as available, but will NOT be split between a register and
1089 the stack.
1090
1091 An exceptional case exists for struct arguments (and possibly other
1092 aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
1093 not a multiple of WORDSIZE bytes. In this case the argument is never split
1094 between the registers and the stack, but instead is copied in its entirety
1095 onto the stack, AND also copied into as many registers as there is room
1096 for. In other words, space in registers permitting, two copies of the same
1097 argument are passed in. As far as I can tell, only the one on the stack is
1098 used, although that may be a function of the level of compiler
1099 optimization. I suspect this is a compiler bug. Arguments of these odd
1100 sizes are left-justified within the word (as opposed to arguments smaller
1101 than WORDSIZE bytes, which are right-justified).
1102
1103 If the function is to return an aggregate type such as a struct, the caller
1104 must allocate space into which the callee will copy the return value. In
1105 this case, a pointer to the return value location is passed into the callee
1106 in register R0, which displaces one of the other arguments passed in via
1107 registers R0 to R2. */
1108
1109static CORE_ADDR
1110avr_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
2e5ff58c 1111 int struct_return, CORE_ADDR struct_addr)
8818c391
TR
1112{
1113 int stack_alloc, stack_offset;
1114 int wordsize;
1115 int argreg;
1116 int argnum;
1117 struct type *type;
1118 CORE_ADDR regval;
1119 char *val;
1120 char valbuf[4];
1121 int len;
1122
2e5ff58c 1123 wordsize = 1;
8818c391
TR
1124#if 0
1125 /* Now make sure there's space on the stack */
2e5ff58c
TR
1126 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
1127 stack_alloc += TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1128 sp -= stack_alloc; /* make room on stack for args */
8818c391
TR
1129 /* we may over-allocate a little here, but that won't hurt anything */
1130#endif
1131 argreg = 25;
2e5ff58c 1132 if (struct_return) /* "struct return" pointer takes up one argreg */
8818c391
TR
1133 {
1134 write_register (--argreg, struct_addr);
1135 }
1136
1137 /* Now load as many as possible of the first arguments into registers, and
1138 push the rest onto the stack. There are 3N bytes in three registers
1139 available. Loop thru args from first to last. */
1140
1141 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
1142 {
1143 type = VALUE_TYPE (args[argnum]);
1144 len = TYPE_LENGTH (type);
1145 val = (char *) VALUE_CONTENTS (args[argnum]);
1146
1147 /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because
1148 some *&^%$ things get passed on the stack AND in the registers! */
1149 while (len > 0)
2e5ff58c
TR
1150 { /* there's room in registers */
1151 len -= wordsize;
1152 regval = extract_address (val + len, wordsize);
1153 write_register (argreg--, regval);
1154 }
8818c391
TR
1155 }
1156 return sp;
1157}
1158
1159/* Initialize the gdbarch structure for the AVR's. */
1160
1161static struct gdbarch *
2e5ff58c
TR
1162avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1163{
8818c391
TR
1164 /* FIXME: TRoth/2002-02-18: I have no idea if avr_call_dummy_words[] should
1165 be bigger or not. Initial testing seems to show that `call my_func()`
1166 works and backtrace from a breakpoint within the call looks correct.
1167 Admittedly, I haven't tested with more than a very simple program. */
2e5ff58c 1168 static LONGEST avr_call_dummy_words[] = { 0 };
8818c391 1169
2e5ff58c
TR
1170 struct gdbarch *gdbarch;
1171 struct gdbarch_tdep *tdep;
8818c391
TR
1172
1173 /* Find a candidate among the list of pre-declared architectures. */
1174 arches = gdbarch_list_lookup_by_info (arches, &info);
1175 if (arches != NULL)
1176 return arches->gdbarch;
1177
1178 /* None found, create a new architecture from the information provided. */
1179 tdep = XMALLOC (struct gdbarch_tdep);
1180 gdbarch = gdbarch_alloc (&info, tdep);
1181
a5afb99f
AC
1182 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1183 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1184 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1185
8818c391
TR
1186 /* If we ever need to differentiate the device types, do it here. */
1187 switch (info.bfd_arch_info->mach)
1188 {
1189 case bfd_mach_avr1:
1190 case bfd_mach_avr2:
1191 case bfd_mach_avr3:
1192 case bfd_mach_avr4:
1193 case bfd_mach_avr5:
1194 break;
1195 }
1196
1197 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1198 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1199 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1200 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1201 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1202 set_gdbarch_addr_bit (gdbarch, 32);
2e5ff58c 1203 set_gdbarch_bfd_vma_bit (gdbarch, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */
8818c391
TR
1204
1205 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1206 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1207 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1208
1209 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
1210 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little);
1211 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_single_little);
1212
1213 set_gdbarch_read_pc (gdbarch, avr_read_pc);
1214 set_gdbarch_write_pc (gdbarch, avr_write_pc);
1215 set_gdbarch_read_fp (gdbarch, avr_read_fp);
1216 set_gdbarch_read_sp (gdbarch, avr_read_sp);
1217 set_gdbarch_write_sp (gdbarch, avr_write_sp);
1218
1219 set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
1220
1221 set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
1222 set_gdbarch_fp_regnum (gdbarch, AVR_FP_REGNUM);
1223 set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
1224
1225 set_gdbarch_register_name (gdbarch, avr_register_name);
1226 set_gdbarch_register_size (gdbarch, 1);
1227 set_gdbarch_register_bytes (gdbarch, AVR_NUM_REG_BYTES);
1228 set_gdbarch_register_byte (gdbarch, avr_register_byte);
1229 set_gdbarch_register_raw_size (gdbarch, avr_register_raw_size);
1230 set_gdbarch_max_register_raw_size (gdbarch, 4);
1231 set_gdbarch_register_virtual_size (gdbarch, avr_register_virtual_size);
1232 set_gdbarch_max_register_virtual_size (gdbarch, 4);
1233 set_gdbarch_register_virtual_type (gdbarch, avr_register_virtual_type);
1234
8818c391
TR
1235 set_gdbarch_print_insn (gdbarch, print_insn_avr);
1236
8818c391
TR
1237 set_gdbarch_call_dummy_address (gdbarch, avr_call_dummy_address);
1238 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1239 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1240 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1241 set_gdbarch_call_dummy_length (gdbarch, 0);
8818c391
TR
1242 set_gdbarch_call_dummy_p (gdbarch, 1);
1243 set_gdbarch_call_dummy_words (gdbarch, avr_call_dummy_words);
1244 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1245 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1246
1247/* set_gdbarch_believe_pcc_promotion (gdbarch, 1); // TRoth: should this be set? */
1248
1249 set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
1250 set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
26e9b323 1251 set_gdbarch_deprecated_extract_return_value (gdbarch, avr_extract_return_value);
8818c391
TR
1252 set_gdbarch_push_arguments (gdbarch, avr_push_arguments);
1253 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
45cf40d1 1254 set_gdbarch_push_return_address (gdbarch, avr_push_return_address);
8818c391
TR
1255 set_gdbarch_pop_frame (gdbarch, avr_pop_frame);
1256
ebba8386 1257 set_gdbarch_deprecated_store_return_value (gdbarch, avr_store_return_value);
8818c391
TR
1258
1259 set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
1260 set_gdbarch_store_struct_return (gdbarch, avr_store_struct_return);
26e9b323
AC
1261 set_gdbarch_deprecated_extract_struct_value_address
1262 (gdbarch, avr_extract_struct_value_address);
8818c391
TR
1263
1264 set_gdbarch_frame_init_saved_regs (gdbarch, avr_scan_prologue);
1265 set_gdbarch_init_extra_frame_info (gdbarch, avr_init_extra_frame_info);
1266 set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
1267/* set_gdbarch_prologue_frameless_p (gdbarch, avr_prologue_frameless_p); */
1268 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1269
1270 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1271
1272 set_gdbarch_function_start_offset (gdbarch, 0);
2e5ff58c
TR
1273 set_gdbarch_remote_translate_xfer_address (gdbarch,
1274 avr_remote_translate_xfer_address);
8818c391 1275 set_gdbarch_frame_args_skip (gdbarch, 0);
2e5ff58c 1276 set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue); /* ??? */
8818c391 1277 set_gdbarch_frame_chain (gdbarch, avr_frame_chain);
8818c391
TR
1278 set_gdbarch_frame_saved_pc (gdbarch, avr_frame_saved_pc);
1279 set_gdbarch_frame_args_address (gdbarch, avr_frame_address);
1280 set_gdbarch_frame_locals_address (gdbarch, avr_frame_address);
1281 set_gdbarch_saved_pc_after_call (gdbarch, avr_saved_pc_after_call);
1282 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1283
2e5ff58c
TR
1284 set_gdbarch_convert_from_func_ptr_addr (gdbarch,
1285 avr_convert_from_func_ptr_addr);
8818c391
TR
1286
1287 return gdbarch;
1288}
1289
1290/* Send a query request to the avr remote target asking for values of the io
1291 registers. If args parameter is not NULL, then the user has requested info
1292 on a specific io register [This still needs implemented and is ignored for
1293 now]. The query string should be one of these forms:
1294
1295 "Ravr.io_reg" -> reply is "NN" number of io registers
1296
1297 "Ravr.io_reg:addr,len" where addr is first register and len is number of
1298 registers to be read. The reply should be "<NAME>,VV;" for each io register
1299 where, <NAME> is a string, and VV is the hex value of the register.
1300
1301 All io registers are 8-bit. */
1302
1303static void
1304avr_io_reg_read_command (char *args, int from_tty)
1305{
2e5ff58c
TR
1306 int bufsiz = 0;
1307 char buf[400];
1308 char query[400];
1309 char *p;
1310 unsigned int nreg = 0;
1311 unsigned int val;
1312 int i, j, k, step;
8818c391
TR
1313
1314/* fprintf_unfiltered (gdb_stderr, "DEBUG: avr_io_reg_read_command (\"%s\", %d)\n", */
1315/* args, from_tty); */
1316
2e5ff58c 1317 if (!current_target.to_query)
8818c391 1318 {
2e5ff58c
TR
1319 fprintf_unfiltered (gdb_stderr,
1320 "ERR: info io_registers NOT supported by current target\n");
8818c391
TR
1321 return;
1322 }
1323
1324 /* Just get the maximum buffer size. */
1325 target_query ((int) 'R', 0, 0, &bufsiz);
2e5ff58c
TR
1326 if (bufsiz > sizeof (buf))
1327 bufsiz = sizeof (buf);
8818c391
TR
1328
1329 /* Find out how many io registers the target has. */
1330 strcpy (query, "avr.io_reg");
2e5ff58c 1331 target_query ((int) 'R', query, buf, &bufsiz);
8818c391
TR
1332
1333 if (strncmp (buf, "", bufsiz) == 0)
1334 {
2e5ff58c
TR
1335 fprintf_unfiltered (gdb_stderr,
1336 "info io_registers NOT supported by target\n");
8818c391
TR
1337 return;
1338 }
1339
2e5ff58c 1340 if (sscanf (buf, "%x", &nreg) != 1)
8818c391 1341 {
2e5ff58c
TR
1342 fprintf_unfiltered (gdb_stderr,
1343 "Error fetching number of io registers\n");
8818c391
TR
1344 return;
1345 }
1346
2e5ff58c 1347 reinitialize_more_filter ();
8818c391
TR
1348
1349 printf_unfiltered ("Target has %u io registers:\n\n", nreg);
1350
1351 /* only fetch up to 8 registers at a time to keep the buffer small */
1352 step = 8;
1353
2e5ff58c 1354 for (i = 0; i < nreg; i += step)
8818c391 1355 {
2e5ff58c 1356 j = step - (nreg % step); /* how many registers this round? */
8818c391 1357
2e5ff58c 1358 snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
8818c391
TR
1359 target_query ((int) 'R', query, buf, &bufsiz);
1360
1361 p = buf;
2e5ff58c
TR
1362 for (k = i; k < (i + j); k++)
1363 {
1364 if (sscanf (p, "%[^,],%x;", query, &val) == 2)
1365 {
1366 printf_filtered ("[%02x] %-15s : %02x\n", k, query, val);
1367 while ((*p != ';') && (*p != '\0'))
1368 p++;
1369 p++; /* skip over ';' */
1370 if (*p == '\0')
1371 break;
1372 }
1373 }
8818c391
TR
1374 }
1375}
1376
1377void
1378_initialize_avr_tdep (void)
1379{
1380 register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
1381
1382 /* Add a new command to allow the user to query the avr remote target for
1383 the values of the io space registers in a saner way than just using
1384 `x/NNNb ADDR`. */
1385
1386 /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
1387 io_registers' to signify it is not available on other platforms. */
1388
1389 add_cmd ("io_registers", class_info, avr_io_reg_read_command,
2e5ff58c 1390 "query remote avr target for io space register values", &infolist);
8818c391 1391}
This page took 0.137511 seconds and 4 git commands to generate.