* Makefile.in (linux-nat.o): Add rule.
[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
de18ac1f 22/* Contributed by Theodore A. Roth, troth@openavr.org */
8818c391
TR
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 313 store_unsigned_integer (buf, TYPE_LENGTH (type),
4ea2465e 314 avr_convert_iaddr_to_raw (addr >> 1));
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 326{
7c0b4a20 327 CORE_ADDR addr = extract_unsigned_integer (buf, TYPE_LENGTH (type));
8818c391 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,
de18ac1f 334 "+++ If you see this, please send me an email <troth@openavr.org>\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)))
4ea2465e 341 return avr_make_iaddr (addr << 1);
8818c391
TR
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
f30ee0bc
AC
390/* avr_scan_prologue is also used as the
391 deprecated_frame_init_saved_regs().
8818c391
TR
392
393 Put here the code to store, into fi->saved_regs, the addresses of
394 the saved registers of frame described by FRAME_INFO. This
395 includes special registers such as pc and fp saved in special ways
396 in the stack frame. sp is even more special: the address we return
397 for it IS the sp for the next frame. */
398
399/* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info)
400 This function decodes a AVR function prologue to determine:
401 1) the size of the stack frame
402 2) which registers are saved on it
403 3) the offsets of saved regs
404 This information is stored in the "extra_info" field of the frame_info.
405
406 A typical AVR function prologue might look like this:
407 push rXX
408 push r28
409 push r29
410 in r28,__SP_L__
411 in r29,__SP_H__
412 sbiw r28,<LOCALS_SIZE>
413 in __tmp_reg__,__SREG__
414 cli
415 out __SP_L__,r28
416 out __SREG__,__tmp_reg__
417 out __SP_H__,r29
418
419 A `-mcall-prologues' prologue look like this:
420 ldi r26,<LOCALS_SIZE>
421 ldi r27,<LOCALS_SIZE>/265
422 ldi r30,pm_lo8(.L_foo_body)
423 ldi r31,pm_hi8(.L_foo_body)
424 rjmp __prologue_saves__+RRR
425 .L_foo_body: */
426
427static void
428avr_scan_prologue (struct frame_info *fi)
429{
430 CORE_ADDR prologue_start;
431 CORE_ADDR prologue_end;
2e5ff58c
TR
432 int i;
433 unsigned short insn;
434 int regno;
435 int scan_stage = 0;
436 char *name;
8818c391 437 struct minimal_symbol *msymbol;
2e5ff58c 438 int prologue_len;
8818c391
TR
439 unsigned char prologue[AVR_MAX_PROLOGUE_SIZE];
440 int vpc = 0;
441
da50a4b7 442 get_frame_extra_info (fi)->framereg = AVR_SP_REGNUM;
2e5ff58c
TR
443
444 if (find_pc_partial_function
50abf9e5 445 (get_frame_pc (fi), &name, &prologue_start, &prologue_end))
8818c391
TR
446 {
447 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
448
2e5ff58c 449 if (sal.line == 0) /* no line info, use current PC */
50abf9e5 450 prologue_end = get_frame_pc (fi);
2e5ff58c
TR
451 else if (sal.end < prologue_end) /* next line begins after fn end */
452 prologue_end = sal.end; /* (probably means no prologue) */
8818c391
TR
453 }
454 else
455 /* We're in the boondocks: allow for */
456 /* 19 pushes, an add, and "mv fp,sp" */
2e5ff58c 457 prologue_end = prologue_start + AVR_MAX_PROLOGUE_SIZE;
8818c391 458
50abf9e5 459 prologue_end = min (prologue_end, get_frame_pc (fi));
8818c391
TR
460
461 /* Search the prologue looking for instructions that set up the
462 frame pointer, adjust the stack pointer, and save registers. */
463
da50a4b7 464 get_frame_extra_info (fi)->framesize = 0;
57bc6122 465 prologue_len = min (prologue_end - prologue_start, AVR_MAX_PROLOGUE_SIZE);
8818c391
TR
466 read_memory (prologue_start, prologue, prologue_len);
467
468 /* Scanning main()'s prologue
469 ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
470 ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
471 out __SP_H__,r29
472 out __SP_L__,r28 */
473
474 if (name && strcmp ("main", name) == 0 && prologue_len == 8)
475 {
476 CORE_ADDR locals;
2e5ff58c
TR
477 unsigned char img[] = {
478 0xde, 0xbf, /* out __SP_H__,r29 */
479 0xcd, 0xbf /* out __SP_L__,r28 */
8818c391
TR
480 };
481
da50a4b7 482 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
8818c391
TR
483 insn = EXTRACT_INSN (&prologue[vpc]);
484 /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
2e5ff58c
TR
485 if ((insn & 0xf0f0) == 0xe0c0)
486 {
487 locals = (insn & 0xf) | ((insn & 0x0f00) >> 4);
488 insn = EXTRACT_INSN (&prologue[vpc + 2]);
489 /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
490 if ((insn & 0xf0f0) == 0xe0d0)
491 {
492 locals |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
493 if (memcmp (prologue + vpc + 4, img, sizeof (img)) == 0)
494 {
8ccd593b 495 deprecated_update_frame_base_hack (fi, locals);
2e5ff58c 496
da50a4b7 497 get_frame_extra_info (fi)->is_main = 1;
2e5ff58c
TR
498 return;
499 }
500 }
501 }
8818c391 502 }
2e5ff58c 503
8818c391
TR
504 /* Scanning `-mcall-prologues' prologue
505 FIXME: mega prologue have a 12 bytes long */
506
2e5ff58c 507 while (prologue_len <= 12) /* I'm use while to avoit many goto's */
8818c391
TR
508 {
509 int loc_size;
510 int body_addr;
511 unsigned num_pushes;
2e5ff58c 512
8818c391
TR
513 insn = EXTRACT_INSN (&prologue[vpc]);
514 /* ldi r26,<LOCALS_SIZE> */
2e5ff58c
TR
515 if ((insn & 0xf0f0) != 0xe0a0)
516 break;
8818c391 517 loc_size = (insn & 0xf) | ((insn & 0x0f00) >> 4);
2e5ff58c 518
8818c391
TR
519 insn = EXTRACT_INSN (&prologue[vpc + 2]);
520 /* ldi r27,<LOCALS_SIZE> / 256 */
521 if ((insn & 0xf0f0) != 0xe0b0)
2e5ff58c 522 break;
8818c391 523 loc_size |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
2e5ff58c 524
8818c391
TR
525 insn = EXTRACT_INSN (&prologue[vpc + 4]);
526 /* ldi r30,pm_lo8(.L_foo_body) */
527 if ((insn & 0xf0f0) != 0xe0e0)
2e5ff58c 528 break;
8818c391
TR
529 body_addr = (insn & 0xf) | ((insn & 0x0f00) >> 4);
530
531 insn = EXTRACT_INSN (&prologue[vpc + 6]);
532 /* ldi r31,pm_hi8(.L_foo_body) */
533 if ((insn & 0xf0f0) != 0xe0f0)
2e5ff58c 534 break;
8818c391
TR
535 body_addr |= ((insn & 0xf) | ((insn & 0x0f00) >> 4)) << 8;
536
537 if (body_addr != (prologue_start + 10) / 2)
2e5ff58c 538 break;
8818c391
TR
539
540 msymbol = lookup_minimal_symbol ("__prologue_saves__", NULL, NULL);
541 if (!msymbol)
2e5ff58c 542 break;
8818c391
TR
543
544 /* FIXME: prologue for mega have a JMP instead of RJMP */
545 insn = EXTRACT_INSN (&prologue[vpc + 8]);
546 /* rjmp __prologue_saves__+RRR */
547 if ((insn & 0xf000) != 0xc000)
2e5ff58c
TR
548 break;
549
8818c391
TR
550 /* Extract PC relative offset from RJMP */
551 i = (insn & 0xfff) | (insn & 0x800 ? (-1 ^ 0xfff) : 0);
552 /* Convert offset to byte addressable mode */
553 i *= 2;
554 /* Destination address */
555 i += vpc + prologue_start + 10;
556 /* Resovle offset (in words) from __prologue_saves__ symbol.
557 Which is a pushes count in `-mcall-prologues' mode */
558 num_pushes = AVR_MAX_PUSHES - (i - SYMBOL_VALUE_ADDRESS (msymbol)) / 2;
559
560 if (num_pushes > AVR_MAX_PUSHES)
2e5ff58c
TR
561 num_pushes = 0;
562
8818c391 563 if (num_pushes)
2e5ff58c
TR
564 {
565 int from;
b2fb4676 566 get_frame_saved_regs (fi)[AVR_FP_REGNUM + 1] = num_pushes;
2e5ff58c 567 if (num_pushes >= 2)
b2fb4676 568 get_frame_saved_regs (fi)[AVR_FP_REGNUM] = num_pushes - 1;
2e5ff58c
TR
569 i = 0;
570 for (from = AVR_LAST_PUSHED_REGNUM + 1 - (num_pushes - 2);
571 from <= AVR_LAST_PUSHED_REGNUM; ++from)
b2fb4676 572 get_frame_saved_regs (fi)[from] = ++i;
2e5ff58c 573 }
da50a4b7
AC
574 get_frame_extra_info (fi)->locals_size = loc_size;
575 get_frame_extra_info (fi)->framesize = loc_size + num_pushes;
576 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
8818c391
TR
577 return;
578 }
579
580 /* Scan interrupt or signal function */
581
582 if (prologue_len >= 12)
583 {
2e5ff58c
TR
584 unsigned char img[] = {
585 0x78, 0x94, /* sei */
586 0x1f, 0x92, /* push r1 */
587 0x0f, 0x92, /* push r0 */
588 0x0f, 0xb6, /* in r0,0x3f SREG */
589 0x0f, 0x92, /* push r0 */
590 0x11, 0x24 /* clr r1 */
8818c391
TR
591 };
592 if (memcmp (prologue, img, sizeof (img)) == 0)
2e5ff58c
TR
593 {
594 vpc += sizeof (img);
b2fb4676
AC
595 get_frame_saved_regs (fi)[0] = 2;
596 get_frame_saved_regs (fi)[1] = 1;
da50a4b7 597 get_frame_extra_info (fi)->framesize += 3;
2e5ff58c 598 }
8818c391 599 else if (memcmp (img + 1, prologue, sizeof (img) - 1) == 0)
2e5ff58c
TR
600 {
601 vpc += sizeof (img) - 1;
b2fb4676
AC
602 get_frame_saved_regs (fi)[0] = 2;
603 get_frame_saved_regs (fi)[1] = 1;
da50a4b7 604 get_frame_extra_info (fi)->framesize += 3;
2e5ff58c 605 }
8818c391
TR
606 }
607
608 /* First stage of the prologue scanning.
609 Scan pushes */
610
611 for (; vpc <= prologue_len; vpc += 2)
612 {
613 insn = EXTRACT_INSN (&prologue[vpc]);
2e5ff58c
TR
614 if ((insn & 0xfe0f) == 0x920f) /* push rXX */
615 {
616 /* Bits 4-9 contain a mask for registers R0-R32. */
617 regno = (insn & 0x1f0) >> 4;
da50a4b7
AC
618 ++get_frame_extra_info (fi)->framesize;
619 get_frame_saved_regs (fi)[regno] = get_frame_extra_info (fi)->framesize;
2e5ff58c
TR
620 scan_stage = 1;
621 }
8818c391 622 else
2e5ff58c 623 break;
8818c391
TR
624 }
625
626 /* Second stage of the prologue scanning.
627 Scan:
628 in r28,__SP_L__
629 in r29,__SP_H__ */
630
631 if (scan_stage == 1 && vpc + 4 <= prologue_len)
632 {
2e5ff58c
TR
633 unsigned char img[] = {
634 0xcd, 0xb7, /* in r28,__SP_L__ */
635 0xde, 0xb7 /* in r29,__SP_H__ */
8818c391
TR
636 };
637 unsigned short insn1;
2e5ff58c 638
8818c391 639 if (memcmp (prologue + vpc, img, sizeof (img)) == 0)
2e5ff58c
TR
640 {
641 vpc += 4;
da50a4b7 642 get_frame_extra_info (fi)->framereg = AVR_FP_REGNUM;
2e5ff58c
TR
643 scan_stage = 2;
644 }
8818c391
TR
645 }
646
647 /* Third stage of the prologue scanning. (Really two stages)
648 Scan for:
649 sbiw r28,XX or subi r28,lo8(XX)
650 sbci r29,hi8(XX)
651 in __tmp_reg__,__SREG__
652 cli
653 out __SP_L__,r28
654 out __SREG__,__tmp_reg__
655 out __SP_H__,r29 */
656
657 if (scan_stage == 2 && vpc + 12 <= prologue_len)
658 {
659 int locals_size = 0;
2e5ff58c
TR
660 unsigned char img[] = {
661 0x0f, 0xb6, /* in r0,0x3f */
662 0xf8, 0x94, /* cli */
663 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
664 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
665 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 666 };
2e5ff58c
TR
667 unsigned char img_sig[] = {
668 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
669 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 670 };
2e5ff58c
TR
671 unsigned char img_int[] = {
672 0xf8, 0x94, /* cli */
673 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
674 0x78, 0x94, /* sei */
675 0xde, 0xbf /* out 0x3e,r29 ; SPH */
8818c391 676 };
2e5ff58c 677
8818c391
TR
678 insn = EXTRACT_INSN (&prologue[vpc]);
679 vpc += 2;
2e5ff58c
TR
680 if ((insn & 0xff30) == 0x9720) /* sbiw r28,XXX */
681 locals_size = (insn & 0xf) | ((insn & 0xc0) >> 2);
682 else if ((insn & 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
683 {
684 locals_size = (insn & 0xf) | ((insn & 0xf00) >> 4);
685 insn = EXTRACT_INSN (&prologue[vpc]);
686 vpc += 2;
687 locals_size += ((insn & 0xf) | ((insn & 0xf00) >> 4) << 8);
688 }
8818c391 689 else
2e5ff58c 690 return;
da50a4b7
AC
691 get_frame_extra_info (fi)->locals_size = locals_size;
692 get_frame_extra_info (fi)->framesize += locals_size;
8818c391
TR
693 }
694}
695
696/* This function actually figures out the frame address for a given pc and
697 sp. This is tricky because we sometimes don't use an explicit
698 frame pointer, and the previous stack pointer isn't necessarily recorded
699 on the stack. The only reliable way to get this info is to
700 examine the prologue. */
701
702static void
703avr_init_extra_frame_info (int fromleaf, struct frame_info *fi)
704{
705 int reg;
706
11c02a10 707 if (get_next_frame (fi))
8bedc050 708 deprecated_update_frame_pc_hack (fi, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi)));
8818c391 709
a00a19e9 710 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
8818c391
TR
711 frame_saved_regs_zalloc (fi);
712
da50a4b7
AC
713 get_frame_extra_info (fi)->return_pc = 0;
714 get_frame_extra_info (fi)->args_pointer = 0;
715 get_frame_extra_info (fi)->locals_size = 0;
716 get_frame_extra_info (fi)->framereg = 0;
717 get_frame_extra_info (fi)->framesize = 0;
718 get_frame_extra_info (fi)->is_main = 0;
2e5ff58c 719
8818c391
TR
720 avr_scan_prologue (fi);
721
1e2330ba
AC
722 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
723 get_frame_base (fi)))
8818c391 724 {
04714b91
AC
725 /* We need to setup fi->frame here because call_function_by_hand
726 gets it wrong by assuming it's always FP. */
1e2330ba 727 deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), get_frame_base (fi),
8ccd593b 728 AVR_PC_REGNUM));
8818c391 729 }
da50a4b7
AC
730 else if (!get_next_frame (fi))
731 /* this is the innermost frame? */
732 deprecated_update_frame_base_hack (fi, read_register (get_frame_extra_info (fi)->framereg));
733 else if (get_frame_extra_info (fi)->is_main != 1)
734 /* not the innermost frame, not `main' */
8818c391
TR
735 /* If we have an next frame, the callee saved it. */
736 {
11c02a10 737 struct frame_info *next_fi = get_next_frame (fi);
da50a4b7
AC
738 if (get_frame_extra_info (fi)->framereg == AVR_SP_REGNUM)
739 deprecated_update_frame_base_hack (fi, (get_frame_base (next_fi)
740 + 2 /* ret addr */
741 + get_frame_extra_info (next_fi)->framesize));
8818c391
TR
742 /* FIXME: I don't analyse va_args functions */
743 else
2e5ff58c
TR
744 {
745 CORE_ADDR fp = 0;
746 CORE_ADDR fp1 = 0;
747 unsigned int fp_low, fp_high;
748
749 /* Scan all frames */
11c02a10 750 for (; next_fi; next_fi = get_next_frame (next_fi))
2e5ff58c
TR
751 {
752 /* look for saved AVR_FP_REGNUM */
b2fb4676
AC
753 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM] && !fp)
754 fp = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM];
2e5ff58c 755 /* look for saved AVR_FP_REGNUM + 1 */
b2fb4676
AC
756 if (get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1] && !fp1)
757 fp1 = get_frame_saved_regs (next_fi)[AVR_FP_REGNUM + 1];
2e5ff58c
TR
758 }
759 fp_low = (fp ? read_memory_unsigned_integer (avr_make_saddr (fp), 1)
760 : read_register (AVR_FP_REGNUM)) & 0xff;
761 fp_high =
762 (fp1 ? read_memory_unsigned_integer (avr_make_saddr (fp1), 1) :
763 read_register (AVR_FP_REGNUM + 1)) & 0xff;
8ccd593b 764 deprecated_update_frame_base_hack (fi, fp_low | (fp_high << 8));
2e5ff58c 765 }
8818c391
TR
766 }
767
768 /* TRoth: Do we want to do this if we are in main? I don't think we should
769 since return_pc makes no sense when we are in main. */
770
da50a4b7
AC
771 if ((get_frame_pc (fi)) && (get_frame_extra_info (fi)->is_main == 0))
772 /* We are not in CALL_DUMMY */
8818c391
TR
773 {
774 CORE_ADDR addr;
775 int i;
2e5ff58c 776
da50a4b7 777 addr = get_frame_base (fi) + get_frame_extra_info (fi)->framesize + 1;
2e5ff58c 778
8818c391
TR
779 /* Return address in stack in different endianness */
780
da50a4b7 781 get_frame_extra_info (fi)->return_pc =
2e5ff58c 782 read_memory_unsigned_integer (avr_make_saddr (addr), 1) << 8;
da50a4b7 783 get_frame_extra_info (fi)->return_pc |=
2e5ff58c
TR
784 read_memory_unsigned_integer (avr_make_saddr (addr + 1), 1);
785
8818c391
TR
786 /* This return address in words,
787 must be converted to the bytes address */
da50a4b7 788 get_frame_extra_info (fi)->return_pc *= 2;
8818c391
TR
789
790 /* Resolve a pushed registers addresses */
791 for (i = 0; i < NUM_REGS; i++)
2e5ff58c 792 {
b2fb4676
AC
793 if (get_frame_saved_regs (fi)[i])
794 get_frame_saved_regs (fi)[i] = addr - get_frame_saved_regs (fi)[i];
2e5ff58c 795 }
8818c391
TR
796 }
797}
798
799/* Restore the machine to the state it had before the current frame was
800 created. Usually used either by the "RETURN" command, or by
801 call_function_by_hand after the dummy_frame is finished. */
802
803static void
804avr_pop_frame (void)
805{
806 unsigned regnum;
807 CORE_ADDR saddr;
808 struct frame_info *frame = get_current_frame ();
809
1e2330ba
AC
810 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
811 get_frame_base (frame),
812 get_frame_base (frame)))
8818c391 813 {
2e5ff58c 814 generic_pop_dummy_frame ();
8818c391
TR
815 }
816 else
817 {
818 /* TRoth: Why only loop over 8 registers? */
819
820 for (regnum = 0; regnum < 8; regnum++)
2e5ff58c
TR
821 {
822 /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the
823 actual value we want, not the address of the value we want. */
b2fb4676 824 if (get_frame_saved_regs (frame)[regnum] && regnum != AVR_SP_REGNUM)
2e5ff58c 825 {
b2fb4676 826 saddr = avr_make_saddr (get_frame_saved_regs (frame)[regnum]);
2e5ff58c
TR
827 write_register (regnum,
828 read_memory_unsigned_integer (saddr, 1));
829 }
b2fb4676 830 else if (get_frame_saved_regs (frame)[regnum] && regnum == AVR_SP_REGNUM)
1e2330ba 831 write_register (regnum, get_frame_base (frame) + 2);
2e5ff58c 832 }
8818c391
TR
833
834 /* Don't forget the update the PC too! */
da50a4b7 835 write_pc (get_frame_extra_info (frame)->return_pc);
8818c391
TR
836 }
837 flush_cached_frames ();
838}
839
840/* Return the saved PC from this frame. */
841
842static CORE_ADDR
843avr_frame_saved_pc (struct frame_info *frame)
844{
1e2330ba
AC
845 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
846 get_frame_base (frame),
847 get_frame_base (frame)))
848 return deprecated_read_register_dummy (get_frame_pc (frame),
849 get_frame_base (frame),
135c175f 850 AVR_PC_REGNUM);
8818c391 851 else
da50a4b7 852 return get_frame_extra_info (frame)->return_pc;
8818c391
TR
853}
854
855static CORE_ADDR
856avr_saved_pc_after_call (struct frame_info *frame)
857{
858 unsigned char m1, m2;
859 unsigned int sp = read_register (AVR_SP_REGNUM);
860 m1 = read_memory_unsigned_integer (avr_make_saddr (sp + 1), 1);
861 m2 = read_memory_unsigned_integer (avr_make_saddr (sp + 2), 1);
862 return (m2 | (m1 << 8)) * 2;
863}
864
8818c391
TR
865/* Returns the return address for a dummy. */
866
867static CORE_ADDR
868avr_call_dummy_address (void)
869{
870 return entry_point_address ();
871}
872
8818c391
TR
873/* Setup the return address for a dummy frame, as called by
874 call_function_by_hand. Only necessary when you are using an empty
875 CALL_DUMMY. */
876
877static CORE_ADDR
878avr_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
879{
880 unsigned char buf[2];
881 int wordsize = 2;
45cf40d1 882#if 0
8818c391
TR
883 struct minimal_symbol *msymbol;
884 CORE_ADDR mon_brk;
45cf40d1 885#endif
8818c391
TR
886
887 buf[0] = 0;
888 buf[1] = 0;
889 sp -= wordsize;
890 write_memory (sp + 1, buf, 2);
891
892#if 0
893 /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a
894 left-over from Denis' original patch which used avr-mon for the target
895 instead of the generic remote target. */
896 if ((strcmp (target_shortname, "avr-mon") == 0)
897 && (msymbol = lookup_minimal_symbol ("gdb_break", NULL, NULL)))
898 {
899 mon_brk = SYMBOL_VALUE_ADDRESS (msymbol);
900 store_unsigned_integer (buf, wordsize, mon_brk / 2);
901 sp -= wordsize;
902 write_memory (sp + 1, buf + 1, 1);
903 write_memory (sp + 2, buf, 1);
904 }
905#endif
906 return sp;
907}
908
909static CORE_ADDR
910avr_skip_prologue (CORE_ADDR pc)
911{
912 CORE_ADDR func_addr, func_end;
913 struct symtab_and_line sal;
914
915 /* See what the symbol table says */
916
917 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
918 {
919 sal = find_pc_line (func_addr, 0);
920
ced15480
TR
921 /* troth/2002-08-05: For some very simple functions, gcc doesn't
922 generate a prologue and the sal.end ends up being the 2-byte ``ret''
923 instruction at the end of the function, but func_end ends up being
924 the address of the first instruction of the _next_ function. By
925 adjusting func_end by 2 bytes, we can catch these functions and not
926 return sal.end if it is the ``ret'' instruction. */
927
928 if (sal.line != 0 && sal.end < (func_end-2))
2e5ff58c 929 return sal.end;
8818c391
TR
930 }
931
932/* Either we didn't find the start of this function (nothing we can do),
933 or there's no line info, or the line after the prologue is after
934 the end of the function (there probably isn't a prologue). */
935
936 return pc;
937}
938
939static CORE_ADDR
940avr_frame_address (struct frame_info *fi)
941{
1e2330ba 942 return avr_make_saddr (get_frame_base (fi));
8818c391
TR
943}
944
a5afb99f
AC
945/* Given a GDB frame, determine the address of the calling function's
946 frame. This will be used to create a new GDB frame struct, and
e9582e71
AC
947 then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
948 will be called for the new frame.
8818c391
TR
949
950 For us, the frame address is its stack pointer value, so we look up
951 the function prologue to determine the caller's sp value, and return it. */
952
953static CORE_ADDR
954avr_frame_chain (struct frame_info *frame)
955{
1e2330ba
AC
956 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
957 get_frame_base (frame),
958 get_frame_base (frame)))
8818c391
TR
959 {
960 /* initialize the return_pc now */
da50a4b7 961 get_frame_extra_info (frame)->return_pc
1e2330ba
AC
962 = deprecated_read_register_dummy (get_frame_pc (frame),
963 get_frame_base (frame),
135c175f 964 AVR_PC_REGNUM);
1e2330ba 965 return get_frame_base (frame);
8818c391 966 }
da50a4b7
AC
967 return (get_frame_extra_info (frame)->is_main ? 0
968 : get_frame_base (frame) + get_frame_extra_info (frame)->framesize + 2 /* ret addr */ );
8818c391
TR
969}
970
971/* Store the address of the place in which to copy the structure the
972 subroutine will return. This is called from call_function.
973
974 We store structs through a pointer passed in the first Argument
975 register. */
976
977static void
978avr_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
979{
980 write_register (0, addr);
981}
982
8818c391
TR
983/* Setup the function arguments for calling a function in the inferior.
984
985 On the AVR architecture, there are 18 registers (R25 to R8) which are
986 dedicated for passing function arguments. Up to the first 18 arguments
987 (depending on size) may go into these registers. The rest go on the stack.
988
989 Arguments that are larger than WORDSIZE bytes will be split between two or
990 more registers as available, but will NOT be split between a register and
991 the stack.
992
993 An exceptional case exists for struct arguments (and possibly other
994 aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
995 not a multiple of WORDSIZE bytes. In this case the argument is never split
996 between the registers and the stack, but instead is copied in its entirety
997 onto the stack, AND also copied into as many registers as there is room
998 for. In other words, space in registers permitting, two copies of the same
999 argument are passed in. As far as I can tell, only the one on the stack is
1000 used, although that may be a function of the level of compiler
1001 optimization. I suspect this is a compiler bug. Arguments of these odd
1002 sizes are left-justified within the word (as opposed to arguments smaller
1003 than WORDSIZE bytes, which are right-justified).
1004
1005 If the function is to return an aggregate type such as a struct, the caller
1006 must allocate space into which the callee will copy the return value. In
1007 this case, a pointer to the return value location is passed into the callee
1008 in register R0, which displaces one of the other arguments passed in via
1009 registers R0 to R2. */
1010
1011static CORE_ADDR
1012avr_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
2e5ff58c 1013 int struct_return, CORE_ADDR struct_addr)
8818c391
TR
1014{
1015 int stack_alloc, stack_offset;
1016 int wordsize;
1017 int argreg;
1018 int argnum;
1019 struct type *type;
1020 CORE_ADDR regval;
1021 char *val;
1022 char valbuf[4];
1023 int len;
1024
2e5ff58c 1025 wordsize = 1;
8818c391
TR
1026#if 0
1027 /* Now make sure there's space on the stack */
2e5ff58c
TR
1028 for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
1029 stack_alloc += TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1030 sp -= stack_alloc; /* make room on stack for args */
8818c391
TR
1031 /* we may over-allocate a little here, but that won't hurt anything */
1032#endif
1033 argreg = 25;
2e5ff58c 1034 if (struct_return) /* "struct return" pointer takes up one argreg */
8818c391
TR
1035 {
1036 write_register (--argreg, struct_addr);
1037 }
1038
1039 /* Now load as many as possible of the first arguments into registers, and
1040 push the rest onto the stack. There are 3N bytes in three registers
1041 available. Loop thru args from first to last. */
1042
1043 for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
1044 {
1045 type = VALUE_TYPE (args[argnum]);
1046 len = TYPE_LENGTH (type);
1047 val = (char *) VALUE_CONTENTS (args[argnum]);
1048
1049 /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because
1050 some *&^%$ things get passed on the stack AND in the registers! */
1051 while (len > 0)
2e5ff58c
TR
1052 { /* there's room in registers */
1053 len -= wordsize;
7c0b4a20 1054 regval = extract_unsigned_integer (val + len, wordsize);
2e5ff58c
TR
1055 write_register (argreg--, regval);
1056 }
8818c391
TR
1057 }
1058 return sp;
1059}
1060
909cd28e
TR
1061/* Not all avr devices support the BREAK insn. Those that don't should treat
1062 it as a NOP. Thus, it should be ok. Since the avr is currently a remote
1063 only target, this shouldn't be a problem (I hope). TRoth/2003-05-14 */
1064
a78f21af 1065static const unsigned char *
909cd28e
TR
1066avr_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
1067{
1068 static unsigned char avr_break_insn [] = { 0x98, 0x95 };
1069 *lenptr = sizeof (avr_break_insn);
1070 return avr_break_insn;
1071}
1072
8818c391
TR
1073/* Initialize the gdbarch structure for the AVR's. */
1074
1075static struct gdbarch *
2e5ff58c
TR
1076avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1077{
2e5ff58c
TR
1078 struct gdbarch *gdbarch;
1079 struct gdbarch_tdep *tdep;
8818c391
TR
1080
1081 /* Find a candidate among the list of pre-declared architectures. */
1082 arches = gdbarch_list_lookup_by_info (arches, &info);
1083 if (arches != NULL)
1084 return arches->gdbarch;
1085
1086 /* None found, create a new architecture from the information provided. */
1087 tdep = XMALLOC (struct gdbarch_tdep);
1088 gdbarch = gdbarch_alloc (&info, tdep);
1089
a5afb99f
AC
1090 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1091 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1092 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1093
8818c391
TR
1094 /* If we ever need to differentiate the device types, do it here. */
1095 switch (info.bfd_arch_info->mach)
1096 {
1097 case bfd_mach_avr1:
1098 case bfd_mach_avr2:
1099 case bfd_mach_avr3:
1100 case bfd_mach_avr4:
1101 case bfd_mach_avr5:
1102 break;
1103 }
1104
1105 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1106 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1107 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1108 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1109 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1110 set_gdbarch_addr_bit (gdbarch, 32);
2e5ff58c 1111 set_gdbarch_bfd_vma_bit (gdbarch, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */
8818c391
TR
1112
1113 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1114 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1115 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1116
1117 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
1118 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little);
1119 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_single_little);
1120
1121 set_gdbarch_read_pc (gdbarch, avr_read_pc);
1122 set_gdbarch_write_pc (gdbarch, avr_write_pc);
0ba6dca9 1123 set_gdbarch_deprecated_target_read_fp (gdbarch, avr_read_fp);
8818c391 1124 set_gdbarch_read_sp (gdbarch, avr_read_sp);
6c0e89ed 1125 set_gdbarch_deprecated_dummy_write_sp (gdbarch, avr_write_sp);
8818c391
TR
1126
1127 set_gdbarch_num_regs (gdbarch, AVR_NUM_REGS);
1128
1129 set_gdbarch_sp_regnum (gdbarch, AVR_SP_REGNUM);
0ba6dca9 1130 set_gdbarch_deprecated_fp_regnum (gdbarch, AVR_FP_REGNUM);
8818c391
TR
1131 set_gdbarch_pc_regnum (gdbarch, AVR_PC_REGNUM);
1132
1133 set_gdbarch_register_name (gdbarch, avr_register_name);
b1e29e33 1134 set_gdbarch_deprecated_register_size (gdbarch, 1);
b8b527c5 1135 set_gdbarch_deprecated_register_bytes (gdbarch, AVR_NUM_REG_BYTES);
9c04cab7
AC
1136 set_gdbarch_deprecated_register_byte (gdbarch, avr_register_byte);
1137 set_gdbarch_deprecated_register_raw_size (gdbarch, avr_register_raw_size);
a0ed5532 1138 set_gdbarch_deprecated_max_register_raw_size (gdbarch, 4);
9c04cab7 1139 set_gdbarch_deprecated_register_virtual_size (gdbarch, avr_register_virtual_size);
a0ed5532 1140 set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 4);
9c04cab7 1141 set_gdbarch_deprecated_register_virtual_type (gdbarch, avr_register_virtual_type);
8818c391 1142
8818c391
TR
1143 set_gdbarch_print_insn (gdbarch, print_insn_avr);
1144
8818c391 1145 set_gdbarch_call_dummy_address (gdbarch, avr_call_dummy_address);
8818c391
TR
1146
1147 set_gdbarch_address_to_pointer (gdbarch, avr_address_to_pointer);
1148 set_gdbarch_pointer_to_address (gdbarch, avr_pointer_to_address);
b81774d8 1149 set_gdbarch_deprecated_push_arguments (gdbarch, avr_push_arguments);
28f617b3 1150 set_gdbarch_deprecated_push_return_address (gdbarch, avr_push_return_address);
749b82f6 1151 set_gdbarch_deprecated_pop_frame (gdbarch, avr_pop_frame);
8818c391 1152
8818c391 1153 set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
4183d812 1154 set_gdbarch_deprecated_store_struct_return (gdbarch, avr_store_struct_return);
8818c391 1155
f30ee0bc 1156 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, avr_scan_prologue);
e9582e71 1157 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, avr_init_extra_frame_info);
8818c391 1158 set_gdbarch_skip_prologue (gdbarch, avr_skip_prologue);
8818c391
TR
1159 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1160
1161 set_gdbarch_decr_pc_after_break (gdbarch, 0);
909cd28e 1162 set_gdbarch_breakpoint_from_pc (gdbarch, avr_breakpoint_from_pc);
8818c391
TR
1163
1164 set_gdbarch_function_start_offset (gdbarch, 0);
98be1e77 1165
8818c391 1166 set_gdbarch_frame_args_skip (gdbarch, 0);
2e5ff58c 1167 set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue); /* ??? */
618ce49f 1168 set_gdbarch_deprecated_frame_chain (gdbarch, avr_frame_chain);
8bedc050 1169 set_gdbarch_deprecated_frame_saved_pc (gdbarch, avr_frame_saved_pc);
8818c391
TR
1170 set_gdbarch_frame_args_address (gdbarch, avr_frame_address);
1171 set_gdbarch_frame_locals_address (gdbarch, avr_frame_address);
6913c89a 1172 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, avr_saved_pc_after_call);
8818c391 1173
8818c391
TR
1174 return gdbarch;
1175}
1176
1177/* Send a query request to the avr remote target asking for values of the io
1178 registers. If args parameter is not NULL, then the user has requested info
1179 on a specific io register [This still needs implemented and is ignored for
1180 now]. The query string should be one of these forms:
1181
1182 "Ravr.io_reg" -> reply is "NN" number of io registers
1183
1184 "Ravr.io_reg:addr,len" where addr is first register and len is number of
1185 registers to be read. The reply should be "<NAME>,VV;" for each io register
1186 where, <NAME> is a string, and VV is the hex value of the register.
1187
1188 All io registers are 8-bit. */
1189
1190static void
1191avr_io_reg_read_command (char *args, int from_tty)
1192{
2e5ff58c
TR
1193 int bufsiz = 0;
1194 char buf[400];
1195 char query[400];
1196 char *p;
1197 unsigned int nreg = 0;
1198 unsigned int val;
1199 int i, j, k, step;
8818c391 1200
2e5ff58c 1201 if (!current_target.to_query)
8818c391 1202 {
2e5ff58c 1203 fprintf_unfiltered (gdb_stderr,
98be1e77
TR
1204 "ERR: info io_registers NOT supported by current "
1205 "target\n");
8818c391
TR
1206 return;
1207 }
1208
1209 /* Just get the maximum buffer size. */
1210 target_query ((int) 'R', 0, 0, &bufsiz);
2e5ff58c
TR
1211 if (bufsiz > sizeof (buf))
1212 bufsiz = sizeof (buf);
8818c391
TR
1213
1214 /* Find out how many io registers the target has. */
1215 strcpy (query, "avr.io_reg");
2e5ff58c 1216 target_query ((int) 'R', query, buf, &bufsiz);
8818c391
TR
1217
1218 if (strncmp (buf, "", bufsiz) == 0)
1219 {
2e5ff58c
TR
1220 fprintf_unfiltered (gdb_stderr,
1221 "info io_registers NOT supported by target\n");
8818c391
TR
1222 return;
1223 }
1224
2e5ff58c 1225 if (sscanf (buf, "%x", &nreg) != 1)
8818c391 1226 {
2e5ff58c
TR
1227 fprintf_unfiltered (gdb_stderr,
1228 "Error fetching number of io registers\n");
8818c391
TR
1229 return;
1230 }
1231
2e5ff58c 1232 reinitialize_more_filter ();
8818c391
TR
1233
1234 printf_unfiltered ("Target has %u io registers:\n\n", nreg);
1235
1236 /* only fetch up to 8 registers at a time to keep the buffer small */
1237 step = 8;
1238
2e5ff58c 1239 for (i = 0; i < nreg; i += step)
8818c391 1240 {
91ccbfc1
TR
1241 /* how many registers this round? */
1242 j = step;
1243 if ((i+j) >= nreg)
1244 j = nreg - i; /* last block is less than 8 registers */
8818c391 1245
2e5ff58c 1246 snprintf (query, sizeof (query) - 1, "avr.io_reg:%x,%x", i, j);
8818c391
TR
1247 target_query ((int) 'R', query, buf, &bufsiz);
1248
1249 p = buf;
2e5ff58c
TR
1250 for (k = i; k < (i + j); k++)
1251 {
1252 if (sscanf (p, "%[^,],%x;", query, &val) == 2)
1253 {
1254 printf_filtered ("[%02x] %-15s : %02x\n", k, query, val);
1255 while ((*p != ';') && (*p != '\0'))
1256 p++;
1257 p++; /* skip over ';' */
1258 if (*p == '\0')
1259 break;
1260 }
1261 }
8818c391
TR
1262 }
1263}
1264
a78f21af
AC
1265extern initialize_file_ftype _initialize_avr_tdep; /* -Wmissing-prototypes */
1266
8818c391
TR
1267void
1268_initialize_avr_tdep (void)
1269{
1270 register_gdbarch_init (bfd_arch_avr, avr_gdbarch_init);
1271
1272 /* Add a new command to allow the user to query the avr remote target for
1273 the values of the io space registers in a saner way than just using
1274 `x/NNNb ADDR`. */
1275
1276 /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
1277 io_registers' to signify it is not available on other platforms. */
1278
1279 add_cmd ("io_registers", class_info, avr_io_reg_read_command,
2e5ff58c 1280 "query remote avr target for io space register values", &infolist);
8818c391 1281}
This page took 0.168021 seconds and 4 git commands to generate.