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