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