Home | History | Annotate | Line # | Download | only in gdb
      1      1.1  christos /* Target-dependent code for the Z80.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "arch-utils.h"
     21      1.1  christos #include "dis-asm.h"
     22  1.1.1.2  christos #include "extract-store-integer.h"
     23      1.1  christos #include "frame.h"
     24      1.1  christos #include "frame-unwind.h"
     25      1.1  christos #include "frame-base.h"
     26      1.1  christos #include "trad-frame.h"
     27  1.1.1.2  christos #include "cli/cli-cmds.h"
     28      1.1  christos #include "gdbcore.h"
     29      1.1  christos #include "gdbtypes.h"
     30      1.1  christos #include "inferior.h"
     31      1.1  christos #include "objfiles.h"
     32      1.1  christos #include "symfile.h"
     33      1.1  christos #include "gdbarch.h"
     34      1.1  christos 
     35      1.1  christos #include "z80-tdep.h"
     36      1.1  christos #include "features/z80.c"
     37      1.1  christos 
     38      1.1  christos /* You need to define __gdb_break_handler symbol pointing to the breakpoint
     39      1.1  christos    handler.  The value of the symbol will be used to determine the instruction
     40      1.1  christos    for software breakpoint.  If __gdb_break_handler points to one of standard
     41      1.1  christos    RST addresses (0x00, 0x08, 0x10,... 0x38) then RST __gdb_break_handler
     42      1.1  christos    instruction will be used, else CALL __gdb_break_handler
     43      1.1  christos 
     44      1.1  christos ;breakpoint handler
     45      1.1  christos 	.globl	__gdb_break_handler
     46      1.1  christos 	.org	8
     47      1.1  christos __gdb_break_handler:
     48      1.1  christos 	jp	_debug_swbreak
     49      1.1  christos 
     50      1.1  christos */
     51      1.1  christos 
     52      1.1  christos /* Meaning of terms "previous" and "next":
     53      1.1  christos      previous frame - frame of callee, which is called by current function
     54      1.1  christos      current frame - frame of current function which has called callee
     55      1.1  christos      next frame - frame of caller, which has called current function
     56      1.1  christos */
     57      1.1  christos 
     58      1.1  christos struct z80_gdbarch_tdep : gdbarch_tdep_base
     59      1.1  christos {
     60      1.1  christos   /* Number of bytes used for address:
     61      1.1  christos       2 bytes for all Z80 family
     62      1.1  christos       3 bytes for eZ80 CPUs operating in ADL mode */
     63      1.1  christos   int addr_length = 0;
     64      1.1  christos 
     65      1.1  christos   /* Type for void.  */
     66      1.1  christos   struct type *void_type = nullptr;
     67      1.1  christos 
     68      1.1  christos   /* Type for a function returning void.  */
     69      1.1  christos   struct type *func_void_type = nullptr;
     70      1.1  christos 
     71      1.1  christos   /* Type for a pointer to a function.  Used for the type of PC.  */
     72      1.1  christos   struct type *pc_type = nullptr;
     73      1.1  christos };
     74      1.1  christos 
     75      1.1  christos /* At any time stack frame contains following parts:
     76      1.1  christos    [<current PC>]
     77      1.1  christos    [<temporaries, y bytes>]
     78      1.1  christos    [<local variables, x bytes>
     79      1.1  christos    <next frame FP>]
     80      1.1  christos    [<saved state (critical or interrupt functions), 2 or 10 bytes>]
     81      1.1  christos    In simplest case <next PC> is pointer to the call instruction
     82      1.1  christos    (or call __call_hl). There are more difficult cases: interrupt handler or
     83      1.1  christos    push/ret and jp; but they are untrackable.
     84      1.1  christos */
     85      1.1  christos 
     86      1.1  christos struct z80_unwind_cache
     87      1.1  christos {
     88      1.1  christos   /* The previous frame's inner most stack address (SP after call executed),
     89      1.1  christos      it is current frame's frame_id.  */
     90      1.1  christos   CORE_ADDR prev_sp;
     91      1.1  christos 
     92      1.1  christos   /* Size of the frame, prev_sp + size = next_frame.prev_sp */
     93      1.1  christos   ULONGEST size;
     94      1.1  christos 
     95      1.1  christos   /* size of saved state (including frame pointer and return address),
     96      1.1  christos      assume: prev_sp + size = IX + state_size */
     97      1.1  christos   ULONGEST state_size;
     98      1.1  christos 
     99      1.1  christos   struct
    100      1.1  christos   {
    101      1.1  christos     unsigned int called : 1;    /* there is return address on stack */
    102      1.1  christos     unsigned int load_args : 1; /* prologues loads args using POPs */
    103      1.1  christos     unsigned int fp_sdcc : 1;   /* prologue saves and adjusts frame pointer IX */
    104      1.1  christos     unsigned int interrupt : 1; /* __interrupt handler */
    105      1.1  christos     unsigned int critical : 1;  /* __critical function */
    106      1.1  christos   } prologue_type;
    107      1.1  christos 
    108      1.1  christos   /* Table indicating the location of each and every register.  */
    109      1.1  christos   struct trad_frame_saved_reg *saved_regs;
    110      1.1  christos };
    111      1.1  christos 
    112      1.1  christos enum z80_instruction_type
    113      1.1  christos {
    114      1.1  christos   insn_default,
    115      1.1  christos   insn_z80,
    116      1.1  christos   insn_adl,
    117      1.1  christos   insn_z80_ed,
    118      1.1  christos   insn_adl_ed,
    119      1.1  christos   insn_z80_ddfd,
    120      1.1  christos   insn_adl_ddfd,
    121      1.1  christos   insn_djnz_d,
    122      1.1  christos   insn_jr_d,
    123      1.1  christos   insn_jr_cc_d,
    124      1.1  christos   insn_jp_nn,
    125      1.1  christos   insn_jp_rr,
    126      1.1  christos   insn_jp_cc_nn,
    127      1.1  christos   insn_call_nn,
    128      1.1  christos   insn_call_cc_nn,
    129      1.1  christos   insn_rst_n,
    130      1.1  christos   insn_ret,
    131      1.1  christos   insn_ret_cc,
    132      1.1  christos   insn_push_rr,
    133      1.1  christos   insn_pop_rr,
    134      1.1  christos   insn_dec_sp,
    135      1.1  christos   insn_inc_sp,
    136      1.1  christos   insn_ld_sp_nn,
    137      1.1  christos   insn_ld_sp_6nn9, /* ld sp, (nn) */
    138      1.1  christos   insn_ld_sp_rr,
    139      1.1  christos   insn_force_nop /* invalid opcode prefix */
    140      1.1  christos };
    141      1.1  christos 
    142      1.1  christos struct z80_insn_info
    143      1.1  christos {
    144      1.1  christos   gdb_byte code;
    145      1.1  christos   gdb_byte mask;
    146      1.1  christos   gdb_byte size; /* without prefix(es) */
    147      1.1  christos   enum z80_instruction_type type;
    148      1.1  christos };
    149      1.1  christos 
    150      1.1  christos /* Constants */
    151      1.1  christos 
    152      1.1  christos static const struct z80_insn_info *
    153      1.1  christos z80_get_insn_info (struct gdbarch *gdbarch, const gdb_byte *buf, int *size);
    154      1.1  christos 
    155      1.1  christos static const char *z80_reg_names[] =
    156      1.1  christos {
    157      1.1  christos   /* 24 bit on eZ80, else 16 bit */
    158      1.1  christos   "af", "bc", "de", "hl",
    159      1.1  christos   "sp", "pc", "ix", "iy",
    160      1.1  christos   "af'", "bc'", "de'", "hl'",
    161      1.1  christos   "ir",
    162      1.1  christos   /* eZ80 only */
    163      1.1  christos   "sps"
    164      1.1  christos };
    165      1.1  christos 
    166      1.1  christos /* Return the name of register REGNUM.  */
    167      1.1  christos static const char *
    168      1.1  christos z80_register_name (struct gdbarch *gdbarch, int regnum)
    169      1.1  christos {
    170      1.1  christos   if (regnum < ARRAY_SIZE (z80_reg_names))
    171      1.1  christos     return z80_reg_names[regnum];
    172      1.1  christos 
    173      1.1  christos   return "";
    174      1.1  christos }
    175      1.1  christos 
    176      1.1  christos /* Return the type of a register specified by the architecture.  Only
    177      1.1  christos    the register cache should call this function directly; others should
    178      1.1  christos    use "register_type".  */
    179      1.1  christos static struct type *
    180      1.1  christos z80_register_type (struct gdbarch *gdbarch, int reg_nr)
    181      1.1  christos {
    182      1.1  christos   return builtin_type (gdbarch)->builtin_data_ptr;
    183      1.1  christos }
    184      1.1  christos 
    185      1.1  christos /* The next 2 functions check BUF for instruction.  If it is pop/push rr, then
    186      1.1  christos    it returns register number OR'ed with 0x100 */
    187      1.1  christos static int
    188      1.1  christos z80_is_pop_rr (const gdb_byte buf[], int *size)
    189      1.1  christos {
    190      1.1  christos   switch (buf[0])
    191      1.1  christos     {
    192      1.1  christos     case 0xc1:
    193      1.1  christos       *size = 1;
    194      1.1  christos       return Z80_BC_REGNUM | 0x100;
    195      1.1  christos     case 0xd1:
    196      1.1  christos       *size = 1;
    197      1.1  christos       return Z80_DE_REGNUM | 0x100;
    198      1.1  christos     case 0xe1:
    199      1.1  christos       *size = 1;
    200      1.1  christos       return Z80_HL_REGNUM | 0x100;
    201      1.1  christos     case 0xf1:
    202      1.1  christos       *size = 1;
    203      1.1  christos       return Z80_AF_REGNUM | 0x100;
    204      1.1  christos     case 0xdd:
    205      1.1  christos       *size = 2;
    206      1.1  christos       return (buf[1] == 0xe1) ? (Z80_IX_REGNUM | 0x100) : 0;
    207      1.1  christos     case 0xfd:
    208      1.1  christos       *size = 2;
    209      1.1  christos       return (buf[1] == 0xe1) ? (Z80_IY_REGNUM | 0x100) : 0;
    210      1.1  christos     }
    211      1.1  christos   *size = 0;
    212      1.1  christos   return 0;
    213      1.1  christos }
    214      1.1  christos 
    215      1.1  christos static int
    216      1.1  christos z80_is_push_rr (const gdb_byte buf[], int *size)
    217      1.1  christos {
    218      1.1  christos   switch (buf[0])
    219      1.1  christos     {
    220      1.1  christos     case 0xc5:
    221      1.1  christos       *size = 1;
    222      1.1  christos       return Z80_BC_REGNUM | 0x100;
    223      1.1  christos     case 0xd5:
    224      1.1  christos       *size = 1;
    225      1.1  christos       return Z80_DE_REGNUM | 0x100;
    226      1.1  christos     case 0xe5:
    227      1.1  christos       *size = 1;
    228      1.1  christos       return Z80_HL_REGNUM | 0x100;
    229      1.1  christos     case 0xf5:
    230      1.1  christos       *size = 1;
    231      1.1  christos       return Z80_AF_REGNUM | 0x100;
    232      1.1  christos     case 0xdd:
    233      1.1  christos       *size = 2;
    234      1.1  christos       return (buf[1] == 0xe5) ? (Z80_IX_REGNUM | 0x100) : 0;
    235      1.1  christos     case 0xfd:
    236      1.1  christos       *size = 2;
    237      1.1  christos       return (buf[1] == 0xe5) ? (Z80_IY_REGNUM | 0x100) : 0;
    238      1.1  christos     }
    239      1.1  christos   *size = 0;
    240      1.1  christos   return 0;
    241      1.1  christos }
    242      1.1  christos 
    243      1.1  christos /* Function: z80_scan_prologue
    244      1.1  christos 
    245      1.1  christos    This function decodes a function prologue to determine:
    246      1.1  christos      1) the size of the stack frame
    247      1.1  christos      2) which registers are saved on it
    248      1.1  christos      3) the offsets of saved regs
    249      1.1  christos    This information is stored in the z80_unwind_cache structure.
    250      1.1  christos    Small SDCC functions may just load args using POP instructions in prologue:
    251      1.1  christos 	pop	af
    252      1.1  christos 	pop	de
    253      1.1  christos 	pop	hl
    254      1.1  christos 	pop	bc
    255      1.1  christos 	push	bc
    256      1.1  christos 	push	hl
    257      1.1  christos 	push	de
    258      1.1  christos 	push	af
    259      1.1  christos    SDCC function prologue may have up to 3 sections (all are optional):
    260      1.1  christos      1) save state
    261      1.1  christos        a) __critical functions:
    262      1.1  christos 	ld	a,i
    263      1.1  christos 	di
    264      1.1  christos 	push	af
    265      1.1  christos        b) __interrupt (both int and nmi) functions:
    266      1.1  christos 	push	af
    267      1.1  christos 	push	bc
    268      1.1  christos 	push	de
    269      1.1  christos 	push	hl
    270      1.1  christos 	push	iy
    271      1.1  christos      2) save and adjust frame pointer
    272      1.1  christos        a) call to special function (size optimization)
    273      1.1  christos 	call	___sdcc_enter_ix
    274      1.1  christos        b) inline (speed optimization)
    275      1.1  christos 	push	ix
    276      1.1  christos 	ld	ix, #0
    277      1.1  christos 	add	ix, sp
    278      1.1  christos        c) without FP, but saving it (IX is optimized out)
    279      1.1  christos 	push	ix
    280      1.1  christos      3) allocate local variables
    281      1.1  christos        a) via series of PUSH AF and optional DEC SP (size optimization)
    282      1.1  christos 	push	af
    283      1.1  christos 	...
    284      1.1  christos 	push	af
    285      1.1  christos 	dec	sp	;optional, if allocated odd numbers of bytes
    286      1.1  christos        b) via SP decrements
    287      1.1  christos 	dec	sp
    288      1.1  christos 	...
    289      1.1  christos 	dec	sp
    290      1.1  christos        c) via addition (for large frames: 5+ for speed and 9+ for size opt.)
    291      1.1  christos 	ld	hl, #xxxx	;size of stack frame
    292      1.1  christos 	add	hl, sp
    293      1.1  christos 	ld	sp, hl
    294      1.1  christos        d) same, but using register IY (arrays or for __z88dk_fastcall functions)
    295      1.1  christos 	ld	iy, #xxxx	;size of stack frame
    296      1.1  christos 	add	iy, sp
    297      1.1  christos 	ld	sp, iy
    298      1.1  christos        e) same as c, but for eZ80
    299      1.1  christos 	lea	hl, ix - #nn
    300      1.1  christos 	ld	sp, hl
    301      1.1  christos        f) same as d, but for eZ80
    302      1.1  christos 	lea	iy, ix - #nn
    303      1.1  christos 	ld	sp, iy
    304      1.1  christos */
    305      1.1  christos 
    306      1.1  christos static int
    307      1.1  christos z80_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR pc_beg, CORE_ADDR pc_end,
    308      1.1  christos 		   struct z80_unwind_cache *info)
    309      1.1  christos {
    310      1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    311      1.1  christos   z80_gdbarch_tdep *tdep = gdbarch_tdep<z80_gdbarch_tdep> (gdbarch);
    312      1.1  christos   int addr_len = tdep->addr_length;
    313      1.1  christos   gdb_byte prologue[32]; /* max prologue is 24 bytes: __interrupt with local array */
    314      1.1  christos   int pos = 0;
    315      1.1  christos   int len;
    316      1.1  christos   int reg;
    317      1.1  christos   CORE_ADDR value;
    318      1.1  christos 
    319      1.1  christos   len = pc_end - pc_beg;
    320      1.1  christos   if (len > (int)sizeof (prologue))
    321      1.1  christos     len = sizeof (prologue);
    322      1.1  christos 
    323      1.1  christos   read_memory (pc_beg, prologue, len);
    324      1.1  christos 
    325      1.1  christos   /* stage0: check for series of POPs and then PUSHs */
    326      1.1  christos   if ((reg = z80_is_pop_rr(prologue, &pos)))
    327      1.1  christos     {
    328      1.1  christos       int i;
    329      1.1  christos       int size = pos;
    330      1.1  christos       gdb_byte regs[8]; /* Z80 have only 6 register pairs */
    331      1.1  christos       regs[0] = reg & 0xff;
    332      1.1  christos       for (i = 1; i < 8 && (regs[i] = z80_is_pop_rr (&prologue[pos], &size));
    333      1.1  christos 	   ++i, pos += size);
    334      1.1  christos       /* now we expect series of PUSHs in reverse order */
    335      1.1  christos       for (--i; i >= 0 && regs[i] == z80_is_push_rr (&prologue[pos], &size);
    336      1.1  christos 	   --i, pos += size);
    337      1.1  christos       if (i == -1 && pos > 0)
    338      1.1  christos 	info->prologue_type.load_args = 1;
    339      1.1  christos       else
    340      1.1  christos 	pos = 0;
    341      1.1  christos     }
    342      1.1  christos   /* stage1: check for __interrupt handlers and __critical functions */
    343      1.1  christos   else if (!memcmp (&prologue[pos], "\355\127\363\365", 4))
    344      1.1  christos     { /* ld a, i; di; push af */
    345      1.1  christos       info->prologue_type.critical = 1;
    346      1.1  christos       pos += 4;
    347      1.1  christos       info->state_size += addr_len;
    348      1.1  christos     }
    349      1.1  christos   else if (!memcmp (&prologue[pos], "\365\305\325\345\375\345", 6))
    350      1.1  christos     { /* push af; push bc; push de; push hl; push iy */
    351      1.1  christos       info->prologue_type.interrupt = 1;
    352      1.1  christos       pos += 6;
    353      1.1  christos       info->state_size += addr_len * 5;
    354      1.1  christos     }
    355      1.1  christos 
    356      1.1  christos   /* stage2: check for FP saving scheme */
    357      1.1  christos   if (prologue[pos] == 0xcd) /* call nn */
    358      1.1  christos     {
    359  1.1.1.3  christos       bound_minimal_symbol msymbol
    360  1.1.1.3  christos 	= lookup_minimal_symbol (current_program_space, "__sdcc_enter_ix");
    361      1.1  christos       if (msymbol.minsym)
    362      1.1  christos 	{
    363      1.1  christos 	  value = msymbol.value_address ();
    364      1.1  christos 	  if (value == extract_unsigned_integer (&prologue[pos+1], addr_len, byte_order))
    365      1.1  christos 	    {
    366      1.1  christos 	      pos += 1 + addr_len;
    367      1.1  christos 	      info->prologue_type.fp_sdcc = 1;
    368      1.1  christos 	    }
    369      1.1  christos 	}
    370      1.1  christos     }
    371      1.1  christos   else if (!memcmp (&prologue[pos], "\335\345\335\041\000\000", 4+addr_len) &&
    372      1.1  christos 	   !memcmp (&prologue[pos+4+addr_len], "\335\071\335\371", 4))
    373      1.1  christos     { /* push ix; ld ix, #0; add ix, sp; ld sp, ix */
    374      1.1  christos       pos += 4 + addr_len + 4;
    375      1.1  christos       info->prologue_type.fp_sdcc = 1;
    376      1.1  christos     }
    377      1.1  christos   else if (!memcmp (&prologue[pos], "\335\345", 2))
    378      1.1  christos     { /* push ix */
    379      1.1  christos       pos += 2;
    380      1.1  christos       info->prologue_type.fp_sdcc = 1;
    381      1.1  christos     }
    382      1.1  christos 
    383      1.1  christos   /* stage3: check for local variables allocation */
    384      1.1  christos   switch (prologue[pos])
    385      1.1  christos     {
    386      1.1  christos       case 0xf5: /* push af */
    387      1.1  christos 	info->size = 0;
    388      1.1  christos 	while (prologue[pos] == 0xf5)
    389      1.1  christos 	  {
    390      1.1  christos 	    info->size += addr_len;
    391      1.1  christos 	    pos++;
    392      1.1  christos 	  }
    393      1.1  christos 	if (prologue[pos] == 0x3b) /* dec sp */
    394      1.1  christos 	  {
    395      1.1  christos 	    info->size++;
    396      1.1  christos 	    pos++;
    397      1.1  christos 	  }
    398      1.1  christos 	break;
    399      1.1  christos       case 0x3b: /* dec sp */
    400      1.1  christos 	info->size = 0;
    401      1.1  christos 	while (prologue[pos] == 0x3b)
    402      1.1  christos 	  {
    403      1.1  christos 	    info->size++;
    404      1.1  christos 	    pos++;
    405      1.1  christos 	  }
    406      1.1  christos 	break;
    407      1.1  christos       case 0x21: /*ld hl, -nn */
    408      1.1  christos 	if (prologue[pos+addr_len] == 0x39 && prologue[pos+addr_len] >= 0x80 &&
    409      1.1  christos 	    prologue[pos+addr_len+1] == 0xf9)
    410      1.1  christos 	  { /* add hl, sp; ld sp, hl */
    411      1.1  christos 	    info->size = -extract_signed_integer(&prologue[pos+1], addr_len, byte_order);
    412      1.1  christos 	    pos += 1 + addr_len + 2;
    413      1.1  christos 	  }
    414      1.1  christos 	break;
    415      1.1  christos       case 0xfd: /* ld iy, -nn */
    416      1.1  christos 	if (prologue[pos+1] == 0x21 && prologue[pos+1+addr_len] >= 0x80 &&
    417      1.1  christos 	    !memcmp (&prologue[pos+2+addr_len], "\375\071\375\371", 4))
    418      1.1  christos 	  {
    419      1.1  christos 	    info->size = -extract_signed_integer(&prologue[pos+2], addr_len, byte_order);
    420      1.1  christos 	    pos += 2 + addr_len + 4;
    421      1.1  christos 	  }
    422      1.1  christos 	break;
    423      1.1  christos       case 0xed: /* check for lea xx, ix - n */
    424      1.1  christos 	switch (prologue[pos+1])
    425      1.1  christos 	  {
    426      1.1  christos 	  case 0x22: /* lea hl, ix - n */
    427      1.1  christos 	    if (prologue[pos+2] >= 0x80 && prologue[pos+3] == 0xf9)
    428      1.1  christos 	      { /* ld sp, hl */
    429      1.1  christos 		info->size = -extract_signed_integer(&prologue[pos+2], 1, byte_order);
    430      1.1  christos 		pos += 4;
    431      1.1  christos 	      }
    432      1.1  christos 	    break;
    433      1.1  christos 	  case 0x55: /* lea iy, ix - n */
    434      1.1  christos 	    if (prologue[pos+2] >= 0x80 && prologue[pos+3] == 0xfd &&
    435      1.1  christos 		prologue[pos+4] == 0xf9)
    436      1.1  christos 	      { /* ld sp, iy */
    437      1.1  christos 		info->size = -extract_signed_integer(&prologue[pos+2], 1, byte_order);
    438      1.1  christos 		pos += 5;
    439      1.1  christos 	      }
    440      1.1  christos 	    break;
    441      1.1  christos 	  }
    442      1.1  christos 	  break;
    443      1.1  christos     }
    444      1.1  christos   len = 0;
    445      1.1  christos 
    446      1.1  christos   if (info->prologue_type.interrupt)
    447      1.1  christos     {
    448      1.1  christos       info->saved_regs[Z80_AF_REGNUM].set_addr (len++);
    449      1.1  christos       info->saved_regs[Z80_BC_REGNUM].set_addr (len++);
    450      1.1  christos       info->saved_regs[Z80_DE_REGNUM].set_addr (len++);
    451      1.1  christos       info->saved_regs[Z80_HL_REGNUM].set_addr (len++);
    452      1.1  christos       info->saved_regs[Z80_IY_REGNUM].set_addr (len++);
    453      1.1  christos     }
    454      1.1  christos 
    455      1.1  christos   if (info->prologue_type.critical)
    456      1.1  christos     len++; /* just skip IFF2 saved state */
    457      1.1  christos 
    458      1.1  christos   if (info->prologue_type.fp_sdcc)
    459      1.1  christos     info->saved_regs[Z80_IX_REGNUM].set_addr (len++);
    460      1.1  christos 
    461      1.1  christos   info->state_size += len * addr_len;
    462      1.1  christos 
    463      1.1  christos   return pc_beg + pos;
    464      1.1  christos }
    465      1.1  christos 
    466      1.1  christos static CORE_ADDR
    467      1.1  christos z80_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
    468      1.1  christos {
    469      1.1  christos   CORE_ADDR func_addr, func_end;
    470      1.1  christos   CORE_ADDR prologue_end;
    471      1.1  christos 
    472      1.1  christos   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
    473      1.1  christos     return pc;
    474      1.1  christos 
    475      1.1  christos   prologue_end = skip_prologue_using_sal (gdbarch, func_addr);
    476      1.1  christos   if (prologue_end != 0)
    477      1.1  christos     return std::max (pc, prologue_end);
    478      1.1  christos 
    479      1.1  christos   {
    480      1.1  christos     struct z80_unwind_cache info = {0};
    481      1.1  christos     struct trad_frame_saved_reg saved_regs[Z80_NUM_REGS];
    482      1.1  christos 
    483      1.1  christos     info.saved_regs = saved_regs;
    484      1.1  christos 
    485      1.1  christos     /* Need to run the prologue scanner to figure out if the function has a
    486      1.1  christos        prologue.  */
    487      1.1  christos 
    488      1.1  christos     prologue_end = z80_scan_prologue (gdbarch, func_addr, func_end, &info);
    489      1.1  christos 
    490      1.1  christos     if (info.prologue_type.fp_sdcc || info.prologue_type.interrupt ||
    491      1.1  christos 	info.prologue_type.critical)
    492      1.1  christos       return std::max (pc, prologue_end);
    493      1.1  christos   }
    494      1.1  christos 
    495      1.1  christos   if (prologue_end != 0)
    496      1.1  christos     {
    497      1.1  christos       struct symtab_and_line prologue_sal = find_pc_line (func_addr, 0);
    498      1.1  christos       struct compunit_symtab *compunit = prologue_sal.symtab->compunit ();
    499      1.1  christos       const char *debug_format = compunit->debugformat ();
    500      1.1  christos 
    501      1.1  christos       if (debug_format != NULL &&
    502      1.1  christos 	  !strncasecmp ("dwarf", debug_format, strlen("dwarf")))
    503      1.1  christos 	return std::max (pc, prologue_end);
    504      1.1  christos     }
    505      1.1  christos 
    506      1.1  christos   return pc;
    507      1.1  christos }
    508      1.1  christos 
    509      1.1  christos /* Return the return-value convention that will be used by FUNCTION
    510      1.1  christos    to return a value of type VALTYPE.  FUNCTION may be NULL in which
    511      1.1  christos    case the return convention is computed based only on VALTYPE.
    512      1.1  christos 
    513      1.1  christos    If READBUF is not NULL, extract the return value and save it in this buffer.
    514      1.1  christos 
    515      1.1  christos    If WRITEBUF is not NULL, it contains a return value which will be
    516      1.1  christos    stored into the appropriate register.  This can be used when we want
    517      1.1  christos    to force the value returned by a function (see the "return" command
    518      1.1  christos    for instance).  */
    519      1.1  christos static enum return_value_convention
    520      1.1  christos z80_return_value (struct gdbarch *gdbarch, struct value *function,
    521      1.1  christos 		  struct type *valtype, struct regcache *regcache,
    522      1.1  christos 		  gdb_byte *readbuf, const gdb_byte *writebuf)
    523      1.1  christos {
    524      1.1  christos   /* Byte are returned in L, word in HL, dword in DEHL.  */
    525      1.1  christos   int len = valtype->length ();
    526      1.1  christos 
    527      1.1  christos   if ((valtype->code () == TYPE_CODE_STRUCT
    528      1.1  christos        || valtype->code () == TYPE_CODE_UNION
    529      1.1  christos        || valtype->code () == TYPE_CODE_ARRAY)
    530      1.1  christos       && len > 4)
    531      1.1  christos     return RETURN_VALUE_STRUCT_CONVENTION;
    532      1.1  christos 
    533      1.1  christos   if (writebuf != NULL)
    534      1.1  christos     {
    535      1.1  christos       if (len > 2)
    536      1.1  christos 	{
    537      1.1  christos 	  regcache->cooked_write_part (Z80_DE_REGNUM, 0, len - 2, writebuf+2);
    538      1.1  christos 	  len = 2;
    539      1.1  christos 	}
    540      1.1  christos       regcache->cooked_write_part (Z80_HL_REGNUM, 0, len, writebuf);
    541      1.1  christos     }
    542      1.1  christos 
    543      1.1  christos   if (readbuf != NULL)
    544      1.1  christos     {
    545      1.1  christos       if (len > 2)
    546      1.1  christos 	{
    547      1.1  christos 	  regcache->cooked_read_part (Z80_DE_REGNUM, 0, len - 2, readbuf+2);
    548      1.1  christos 	  len = 2;
    549      1.1  christos 	}
    550      1.1  christos       regcache->cooked_read_part (Z80_HL_REGNUM, 0, len, readbuf);
    551      1.1  christos     }
    552      1.1  christos 
    553      1.1  christos   return RETURN_VALUE_REGISTER_CONVENTION;
    554      1.1  christos }
    555      1.1  christos 
    556      1.1  christos /* function unwinds current stack frame and returns next one */
    557      1.1  christos static struct z80_unwind_cache *
    558  1.1.1.2  christos z80_frame_unwind_cache (const frame_info_ptr &this_frame,
    559      1.1  christos 			void **this_prologue_cache)
    560      1.1  christos {
    561      1.1  christos   CORE_ADDR start_pc, current_pc;
    562      1.1  christos   ULONGEST this_base;
    563      1.1  christos   int i;
    564      1.1  christos   gdb_byte buf[sizeof(void*)];
    565      1.1  christos   struct z80_unwind_cache *info;
    566      1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
    567      1.1  christos   z80_gdbarch_tdep *tdep = gdbarch_tdep<z80_gdbarch_tdep> (gdbarch);
    568      1.1  christos   int addr_len = tdep->addr_length;
    569      1.1  christos 
    570      1.1  christos   if (*this_prologue_cache)
    571      1.1  christos     return (struct z80_unwind_cache *) *this_prologue_cache;
    572      1.1  christos 
    573      1.1  christos   info = FRAME_OBSTACK_ZALLOC (struct z80_unwind_cache);
    574      1.1  christos   memset (info, 0, sizeof (*info));
    575      1.1  christos   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
    576      1.1  christos   *this_prologue_cache = info;
    577      1.1  christos 
    578      1.1  christos   start_pc = get_frame_func (this_frame);
    579      1.1  christos   current_pc = get_frame_pc (this_frame);
    580      1.1  christos   if ((start_pc > 0) && (start_pc <= current_pc))
    581      1.1  christos     z80_scan_prologue (get_frame_arch (this_frame),
    582      1.1  christos 		       start_pc, current_pc, info);
    583      1.1  christos 
    584      1.1  christos   if (info->prologue_type.fp_sdcc)
    585      1.1  christos     {
    586      1.1  christos       /*  With SDCC standard prologue, IX points to the end of current frame
    587      1.1  christos 	  (where previous frame pointer and state are saved).  */
    588      1.1  christos       this_base = get_frame_register_unsigned (this_frame, Z80_IX_REGNUM);
    589      1.1  christos       info->prev_sp = this_base + info->size;
    590      1.1  christos     }
    591      1.1  christos   else
    592      1.1  christos     {
    593      1.1  christos       CORE_ADDR addr;
    594      1.1  christos       CORE_ADDR sp;
    595      1.1  christos       CORE_ADDR sp_mask = (1 << gdbarch_ptr_bit(gdbarch)) - 1;
    596      1.1  christos       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    597      1.1  christos       /* Assume that the FP is this frame's SP but with that pushed
    598      1.1  christos 	 stack space added back.  */
    599      1.1  christos       this_base = get_frame_register_unsigned (this_frame, Z80_SP_REGNUM);
    600      1.1  christos       sp = this_base + info->size;
    601      1.1  christos       for (;; ++sp)
    602      1.1  christos 	{
    603      1.1  christos 	  sp &= sp_mask;
    604      1.1  christos 	  if (sp < this_base)
    605      1.1  christos 	    { /* overflow, looks like end of stack */
    606      1.1  christos 	      sp = this_base + info->size;
    607      1.1  christos 	      break;
    608      1.1  christos 	    }
    609      1.1  christos 	  /* find return address */
    610      1.1  christos 	  read_memory (sp, buf, addr_len);
    611      1.1  christos 	  addr = extract_unsigned_integer(buf, addr_len, byte_order);
    612      1.1  christos 	  read_memory (addr-addr_len-1, buf, addr_len+1);
    613      1.1  christos 	  if (buf[0] == 0xcd || (buf[0] & 0307) == 0304) /* Is it CALL */
    614      1.1  christos 	    { /* CALL nn or CALL cc,nn */
    615      1.1  christos 	      static const char *names[] =
    616      1.1  christos 		{
    617      1.1  christos 		  "__sdcc_call_ix", "__sdcc_call_iy", "__sdcc_call_hl"
    618      1.1  christos 		};
    619      1.1  christos 	      addr = extract_unsigned_integer(buf+1, addr_len, byte_order);
    620      1.1  christos 	      if (addr == start_pc)
    621      1.1  christos 		break; /* found */
    622      1.1  christos 	      for (i = sizeof(names)/sizeof(*names)-1; i >= 0; --i)
    623      1.1  christos 		{
    624  1.1.1.3  christos 		  bound_minimal_symbol msymbol
    625  1.1.1.3  christos 		    = lookup_minimal_symbol (current_program_space, names[i]);
    626      1.1  christos 		  if (!msymbol.minsym)
    627      1.1  christos 		    continue;
    628      1.1  christos 		  if (addr == msymbol.value_address ())
    629      1.1  christos 		    break;
    630      1.1  christos 		}
    631      1.1  christos 	      if (i >= 0)
    632      1.1  christos 		break;
    633      1.1  christos 	      continue;
    634      1.1  christos 	    }
    635      1.1  christos 	  else
    636      1.1  christos 	    continue; /* it is not call_nn, call_cc_nn */
    637      1.1  christos 	}
    638      1.1  christos       info->prev_sp = sp;
    639      1.1  christos     }
    640      1.1  christos 
    641      1.1  christos   /* Adjust all the saved registers so that they contain addresses and not
    642      1.1  christos      offsets.  */
    643      1.1  christos   for (i = 0; i < gdbarch_num_regs (gdbarch) - 1; i++)
    644      1.1  christos     if (info->saved_regs[i].addr () > 0)
    645      1.1  christos       info->saved_regs[i].set_addr
    646      1.1  christos 	(info->prev_sp - info->saved_regs[i].addr () * addr_len);
    647      1.1  christos 
    648      1.1  christos   /* Except for the startup code, the return PC is always saved on
    649      1.1  christos      the stack and is at the base of the frame.  */
    650      1.1  christos   info->saved_regs[Z80_PC_REGNUM].set_addr (info->prev_sp);
    651      1.1  christos 
    652      1.1  christos   /* The previous frame's SP needed to be computed.  Save the computed
    653      1.1  christos      value.  */
    654      1.1  christos   info->saved_regs[Z80_SP_REGNUM].set_value (info->prev_sp + addr_len);
    655      1.1  christos   return info;
    656      1.1  christos }
    657      1.1  christos 
    658      1.1  christos /* Given a GDB frame, determine the address of the calling function's
    659      1.1  christos    frame.  This will be used to create a new GDB frame struct.  */
    660      1.1  christos static void
    661  1.1.1.2  christos z80_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
    662      1.1  christos 		   struct frame_id *this_id)
    663      1.1  christos {
    664      1.1  christos   struct frame_id id;
    665      1.1  christos   struct z80_unwind_cache *info;
    666      1.1  christos   CORE_ADDR base;
    667      1.1  christos   CORE_ADDR func;
    668      1.1  christos 
    669      1.1  christos   /* The FUNC is easy.  */
    670      1.1  christos   func = get_frame_func (this_frame);
    671      1.1  christos 
    672      1.1  christos   info = z80_frame_unwind_cache (this_frame, this_cache);
    673      1.1  christos   /* Hopefully the prologue analysis either correctly determined the
    674      1.1  christos      frame's base (which is the SP from the previous frame), or set
    675      1.1  christos      that base to "NULL".  */
    676      1.1  christos   base = info->prev_sp;
    677      1.1  christos   if (base == 0)
    678      1.1  christos     return;
    679      1.1  christos 
    680      1.1  christos   id = frame_id_build (base, func);
    681      1.1  christos   *this_id = id;
    682      1.1  christos }
    683      1.1  christos 
    684      1.1  christos static struct value *
    685  1.1.1.2  christos z80_frame_prev_register (const frame_info_ptr &this_frame,
    686      1.1  christos 			 void **this_prologue_cache, int regnum)
    687      1.1  christos {
    688      1.1  christos   struct z80_unwind_cache *info
    689      1.1  christos     = z80_frame_unwind_cache (this_frame, this_prologue_cache);
    690      1.1  christos 
    691      1.1  christos   if (regnum == Z80_PC_REGNUM)
    692      1.1  christos     {
    693      1.1  christos       if (info->saved_regs[Z80_PC_REGNUM].is_addr ())
    694      1.1  christos 	{
    695      1.1  christos 	  /* Reading the return PC from the PC register is slightly
    696      1.1  christos 	     abnormal.  */
    697      1.1  christos 	  ULONGEST pc;
    698      1.1  christos 	  gdb_byte buf[3];
    699      1.1  christos 	  struct gdbarch *gdbarch = get_frame_arch (this_frame);
    700      1.1  christos 	  z80_gdbarch_tdep *tdep = gdbarch_tdep<z80_gdbarch_tdep> (gdbarch);
    701      1.1  christos 	  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    702      1.1  christos 
    703      1.1  christos 	  read_memory (info->saved_regs[Z80_PC_REGNUM].addr (),
    704      1.1  christos 		       buf, tdep->addr_length);
    705      1.1  christos 	  pc = extract_unsigned_integer (buf, tdep->addr_length, byte_order);
    706      1.1  christos 	  return frame_unwind_got_constant (this_frame, regnum, pc);
    707      1.1  christos 	}
    708      1.1  christos 
    709      1.1  christos       return frame_unwind_got_optimized (this_frame, regnum);
    710      1.1  christos     }
    711      1.1  christos 
    712      1.1  christos   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
    713      1.1  christos }
    714      1.1  christos 
    715      1.1  christos /* Return the breakpoint kind for this target based on *PCPTR.  */
    716      1.1  christos static int
    717      1.1  christos z80_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
    718      1.1  christos {
    719      1.1  christos   static int addr = -1;
    720      1.1  christos   if (addr == -1)
    721      1.1  christos     {
    722  1.1.1.3  christos       bound_minimal_symbol bh
    723  1.1.1.3  christos 	= lookup_minimal_symbol (current_program_space, "_break_handler");
    724      1.1  christos       if (bh.minsym)
    725      1.1  christos 	addr = bh.value_address ();
    726      1.1  christos       else
    727      1.1  christos 	{
    728      1.1  christos 	  warning(_("Unable to determine inferior's software breakpoint type: "
    729      1.1  christos 		    "couldn't find `_break_handler' function in inferior. Will "
    730      1.1  christos 		    "be used default software breakpoint instruction RST 0x08."));
    731      1.1  christos 	  addr = 0x0008;
    732      1.1  christos 	}
    733      1.1  christos     }
    734      1.1  christos   return addr;
    735      1.1  christos }
    736      1.1  christos 
    737      1.1  christos /* Return the software breakpoint from KIND. KIND is just address of breakpoint
    738      1.1  christos    handler.  If address is on of standard RSTs, then RST n instruction is used
    739      1.1  christos    as breakpoint.
    740      1.1  christos    SIZE is set to the software breakpoint's length in memory.  */
    741      1.1  christos static const gdb_byte *
    742      1.1  christos z80_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
    743      1.1  christos {
    744      1.1  christos   static gdb_byte break_insn[8];
    745      1.1  christos 
    746      1.1  christos   if ((kind & 070) == kind)
    747      1.1  christos     {
    748      1.1  christos       break_insn[0] = kind | 0307;
    749      1.1  christos       *size = 1;
    750      1.1  christos     }
    751  1.1.1.2  christos   else /* kind is non-RST address, use CALL instead, but it is dangerous */
    752      1.1  christos     {
    753      1.1  christos       z80_gdbarch_tdep *tdep = gdbarch_tdep<z80_gdbarch_tdep> (gdbarch);
    754      1.1  christos       gdb_byte *p = break_insn;
    755      1.1  christos       *p++ = 0xcd;
    756      1.1  christos       *p++ = (kind >> 0) & 0xff;
    757      1.1  christos       *p++ = (kind >> 8) & 0xff;
    758      1.1  christos       if (tdep->addr_length > 2)
    759      1.1  christos 	*p++ = (kind >> 16) & 0xff;
    760      1.1  christos       *size = p - break_insn;
    761      1.1  christos     }
    762      1.1  christos   return break_insn;
    763      1.1  christos }
    764      1.1  christos 
    765      1.1  christos /* Return a vector of addresses on which the software single step
    766      1.1  christos    breakpoints should be inserted.  NULL means software single step is
    767      1.1  christos    not used.
    768      1.1  christos    Only one breakpoint address will be returned: conditional branches
    769      1.1  christos    will be always evaluated. */
    770      1.1  christos static std::vector<CORE_ADDR>
    771      1.1  christos z80_software_single_step (struct regcache *regcache)
    772      1.1  christos {
    773      1.1  christos   static const int flag_mask[] = {1 << 6, 1 << 0, 1 << 2, 1 << 7};
    774      1.1  christos   gdb_byte buf[8];
    775      1.1  christos   ULONGEST t;
    776      1.1  christos   ULONGEST addr;
    777      1.1  christos   int opcode;
    778      1.1  christos   int size;
    779      1.1  christos   const struct z80_insn_info *info;
    780      1.1  christos   std::vector<CORE_ADDR> ret (1);
    781  1.1.1.2  christos   gdbarch *gdbarch = current_inferior ()->arch ();
    782      1.1  christos 
    783      1.1  christos   regcache->cooked_read (Z80_PC_REGNUM, &addr);
    784      1.1  christos   read_memory (addr, buf, sizeof(buf));
    785      1.1  christos   info = z80_get_insn_info (gdbarch, buf, &size);
    786      1.1  christos   ret[0] = addr + size;
    787      1.1  christos   if (info == NULL) /* possible in case of double prefix */
    788      1.1  christos     { /* forced NOP, TODO: replace by NOP */
    789      1.1  christos       return ret;
    790      1.1  christos     }
    791      1.1  christos   opcode = buf[size - info->size]; /* take opcode instead of prefix */
    792      1.1  christos   /* stage 1: check for conditions */
    793      1.1  christos   switch (info->type)
    794      1.1  christos     {
    795      1.1  christos     case insn_djnz_d:
    796      1.1  christos       regcache->cooked_read (Z80_BC_REGNUM, &t);
    797      1.1  christos       if ((t & 0xff00) != 0x100)
    798      1.1  christos 	return ret;
    799      1.1  christos       break;
    800      1.1  christos     case insn_jr_cc_d:
    801      1.1  christos       opcode &= 030; /* JR NZ,d has cc equal to 040, but others 000 */
    802  1.1.1.2  christos       [[fallthrough]];
    803      1.1  christos     case insn_jp_cc_nn:
    804      1.1  christos     case insn_call_cc_nn:
    805      1.1  christos     case insn_ret_cc:
    806      1.1  christos       regcache->cooked_read (Z80_AF_REGNUM, &t);
    807      1.1  christos       /* lower bit of condition inverts match, so invert flags if set */
    808      1.1  christos       if ((opcode & 010) != 0)
    809      1.1  christos 	t = ~t;
    810      1.1  christos       /* two higher bits of condition field defines flag, so use them only
    811      1.1  christos 	 to check condition of "not execute" */
    812      1.1  christos       if (t & flag_mask[(opcode >> 4) & 3])
    813      1.1  christos 	return ret;
    814      1.1  christos       break;
    815      1.1  christos     }
    816      1.1  christos   /* stage 2: compute address */
    817      1.1  christos   /* TODO: implement eZ80 MADL support */
    818      1.1  christos   switch (info->type)
    819      1.1  christos     {
    820      1.1  christos     default:
    821      1.1  christos       return ret;
    822      1.1  christos     case insn_djnz_d:
    823      1.1  christos     case insn_jr_d:
    824      1.1  christos     case insn_jr_cc_d:
    825      1.1  christos       addr += size;
    826      1.1  christos       addr += (signed char)buf[size-1];
    827      1.1  christos       break;
    828      1.1  christos     case insn_jp_rr:
    829      1.1  christos       if (size == 1)
    830      1.1  christos 	opcode = Z80_HL_REGNUM;
    831      1.1  christos       else
    832      1.1  christos 	opcode = (buf[size-2] & 0x20) ? Z80_IY_REGNUM : Z80_IX_REGNUM;
    833      1.1  christos       regcache->cooked_read (opcode, &addr);
    834      1.1  christos       break;
    835      1.1  christos     case insn_jp_nn:
    836      1.1  christos     case insn_jp_cc_nn:
    837      1.1  christos     case insn_call_nn:
    838      1.1  christos     case insn_call_cc_nn:
    839      1.1  christos       addr = buf[size-1] * 0x100 + buf[size-2];
    840      1.1  christos       if (info->size > 3) /* long instruction mode */
    841      1.1  christos 	addr = addr * 0x100 + buf[size-3];
    842      1.1  christos       break;
    843      1.1  christos     case insn_rst_n:
    844      1.1  christos       addr = opcode & 070;
    845      1.1  christos       break;
    846      1.1  christos     case insn_ret:
    847      1.1  christos     case insn_ret_cc:
    848      1.1  christos       regcache->cooked_read (Z80_SP_REGNUM, &addr);
    849      1.1  christos       read_memory (addr, buf, 3);
    850      1.1  christos       addr = buf[1] * 0x100 + buf[0];
    851      1.1  christos       if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_ez80_adl)
    852      1.1  christos 	addr = addr * 0x100 + buf[2];
    853      1.1  christos       break;
    854      1.1  christos     }
    855      1.1  christos   ret[0] = addr;
    856      1.1  christos   return ret;
    857      1.1  christos }
    858      1.1  christos 
    859      1.1  christos /* Cached, dynamically allocated copies of the target data structures: */
    860      1.1  christos static unsigned (*cache_ovly_region_table)[3] = 0;
    861      1.1  christos static unsigned cache_novly_regions;
    862      1.1  christos static CORE_ADDR cache_ovly_region_table_base = 0;
    863      1.1  christos enum z80_ovly_index
    864      1.1  christos   {
    865      1.1  christos     Z80_VMA, Z80_OSIZE, Z80_MAPPED_TO_LMA
    866      1.1  christos   };
    867      1.1  christos 
    868      1.1  christos static void
    869      1.1  christos z80_free_overlay_region_table (void)
    870      1.1  christos {
    871      1.1  christos   if (cache_ovly_region_table)
    872      1.1  christos     xfree (cache_ovly_region_table);
    873      1.1  christos   cache_novly_regions = 0;
    874      1.1  christos   cache_ovly_region_table = NULL;
    875      1.1  christos   cache_ovly_region_table_base = 0;
    876      1.1  christos }
    877      1.1  christos 
    878      1.1  christos /* Read an array of ints of size SIZE from the target into a local buffer.
    879      1.1  christos    Convert to host order.  LEN is number of ints.  */
    880      1.1  christos 
    881      1.1  christos static void
    882      1.1  christos read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
    883      1.1  christos 			int len, int size, enum bfd_endian byte_order)
    884      1.1  christos {
    885      1.1  christos   /* alloca is safe here, because regions array is very small. */
    886      1.1  christos   gdb_byte *buf = (gdb_byte *) alloca (len * size);
    887      1.1  christos   int i;
    888      1.1  christos 
    889      1.1  christos   read_memory (memaddr, buf, len * size);
    890      1.1  christos   for (i = 0; i < len; i++)
    891      1.1  christos     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
    892      1.1  christos }
    893      1.1  christos 
    894      1.1  christos static int
    895      1.1  christos z80_read_overlay_region_table ()
    896      1.1  christos {
    897      1.1  christos   struct gdbarch *gdbarch;
    898      1.1  christos   int word_size;
    899      1.1  christos   enum bfd_endian byte_order;
    900      1.1  christos 
    901      1.1  christos   z80_free_overlay_region_table ();
    902  1.1.1.3  christos   bound_minimal_symbol novly_regions_msym
    903  1.1.1.3  christos     = lookup_minimal_symbol (current_program_space, "_novly_regions");
    904      1.1  christos   if (! novly_regions_msym.minsym)
    905      1.1  christos     {
    906      1.1  christos       error (_("Error reading inferior's overlay table: "
    907      1.1  christos 	       "couldn't find `_novly_regions'\n"
    908      1.1  christos 	       "variable in inferior.  Use `overlay manual' mode."));
    909      1.1  christos       return 0;
    910      1.1  christos     }
    911      1.1  christos 
    912  1.1.1.3  christos   bound_minimal_symbol ovly_region_table_msym
    913  1.1.1.3  christos     = lookup_minimal_symbol (current_program_space, "_ovly_region_table");
    914      1.1  christos   if (! ovly_region_table_msym.minsym)
    915      1.1  christos     {
    916      1.1  christos       error (_("Error reading inferior's overlay table: couldn't find "
    917      1.1  christos 	       "`_ovly_region_table'\n"
    918      1.1  christos 	       "array in inferior.  Use `overlay manual' mode."));
    919      1.1  christos       return 0;
    920      1.1  christos     }
    921      1.1  christos 
    922      1.1  christos   const enum overlay_debugging_state save_ovly_dbg = overlay_debugging;
    923      1.1  christos   /* prevent infinite recurse */
    924      1.1  christos   overlay_debugging = ovly_off;
    925      1.1  christos 
    926      1.1  christos   gdbarch = ovly_region_table_msym.objfile->arch ();
    927      1.1  christos   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
    928      1.1  christos   byte_order = gdbarch_byte_order (gdbarch);
    929      1.1  christos 
    930      1.1  christos   cache_novly_regions = read_memory_integer (novly_regions_msym.value_address (),
    931  1.1.1.2  christos 					     4, byte_order);
    932      1.1  christos   cache_ovly_region_table
    933      1.1  christos     = (unsigned int (*)[3]) xmalloc (cache_novly_regions *
    934      1.1  christos 					sizeof (*cache_ovly_region_table));
    935      1.1  christos   cache_ovly_region_table_base
    936      1.1  christos     = ovly_region_table_msym.value_address ();
    937      1.1  christos   read_target_long_array (cache_ovly_region_table_base,
    938      1.1  christos 			  (unsigned int *) cache_ovly_region_table,
    939      1.1  christos 			  cache_novly_regions * 3, word_size, byte_order);
    940      1.1  christos 
    941      1.1  christos   overlay_debugging = save_ovly_dbg;
    942      1.1  christos   return 1;                     /* SUCCESS */
    943      1.1  christos }
    944      1.1  christos 
    945      1.1  christos static int
    946      1.1  christos z80_overlay_update_1 (struct obj_section *osect)
    947      1.1  christos {
    948      1.1  christos   int i;
    949      1.1  christos   asection *bsect = osect->the_bfd_section;
    950      1.1  christos   unsigned lma;
    951      1.1  christos   unsigned vma = bfd_section_vma (bsect);
    952      1.1  christos 
    953      1.1  christos   /* find region corresponding to the section VMA */
    954      1.1  christos   for (i = 0; i < cache_novly_regions; i++)
    955      1.1  christos     if (cache_ovly_region_table[i][Z80_VMA] == vma)
    956      1.1  christos 	break;
    957      1.1  christos   if (i == cache_novly_regions)
    958      1.1  christos     return 0; /* no such region */
    959      1.1  christos 
    960      1.1  christos   lma = cache_ovly_region_table[i][Z80_MAPPED_TO_LMA];
    961      1.1  christos   i = 0;
    962      1.1  christos 
    963      1.1  christos   /* we have interest for sections with same VMA */
    964      1.1  christos   for (objfile *objfile : current_program_space->objfiles ())
    965  1.1.1.2  christos     for (obj_section *sect : objfile->sections ())
    966  1.1.1.2  christos       if (section_is_overlay (sect))
    967      1.1  christos 	{
    968  1.1.1.2  christos 	  sect->ovly_mapped = (lma == bfd_section_lma (sect->the_bfd_section));
    969  1.1.1.2  christos 	  i |= sect->ovly_mapped; /* true, if at least one section is mapped */
    970      1.1  christos 	}
    971      1.1  christos   return i;
    972      1.1  christos }
    973      1.1  christos 
    974      1.1  christos /* Refresh overlay mapped state for section OSECT.  */
    975      1.1  christos static void
    976      1.1  christos z80_overlay_update (struct obj_section *osect)
    977      1.1  christos {
    978      1.1  christos   /* Always need to read the entire table anew.  */
    979      1.1  christos   if (!z80_read_overlay_region_table ())
    980      1.1  christos     return;
    981      1.1  christos 
    982      1.1  christos   /* Were we given an osect to look up?  NULL means do all of them.  */
    983      1.1  christos   if (osect != nullptr && z80_overlay_update_1 (osect))
    984      1.1  christos     return;
    985      1.1  christos 
    986      1.1  christos   /* Update all sections, even if only one was requested.  */
    987      1.1  christos   for (objfile *objfile : current_program_space->objfiles ())
    988  1.1.1.2  christos     for (obj_section *sect : objfile->sections ())
    989      1.1  christos       {
    990  1.1.1.2  christos 	if (!section_is_overlay (sect))
    991      1.1  christos 	  continue;
    992      1.1  christos 
    993  1.1.1.2  christos 	asection *bsect = sect->the_bfd_section;
    994      1.1  christos 	bfd_vma lma = bfd_section_lma (bsect);
    995      1.1  christos 	bfd_vma vma = bfd_section_vma (bsect);
    996      1.1  christos 
    997      1.1  christos 	for (int i = 0; i < cache_novly_regions; ++i)
    998      1.1  christos 	  if (cache_ovly_region_table[i][Z80_VMA] == vma)
    999  1.1.1.2  christos 	    sect->ovly_mapped =
   1000      1.1  christos 	      (cache_ovly_region_table[i][Z80_MAPPED_TO_LMA] == lma);
   1001      1.1  christos       }
   1002      1.1  christos }
   1003      1.1  christos 
   1004      1.1  christos /* Return non-zero if the instruction at ADDR is a call; zero otherwise.  */
   1005      1.1  christos static int
   1006      1.1  christos z80_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
   1007      1.1  christos {
   1008      1.1  christos   gdb_byte buf[8];
   1009      1.1  christos   int size;
   1010      1.1  christos   const struct z80_insn_info *info;
   1011      1.1  christos   read_memory (addr, buf, sizeof(buf));
   1012      1.1  christos   info = z80_get_insn_info (gdbarch, buf, &size);
   1013      1.1  christos   if (info)
   1014      1.1  christos     switch (info->type)
   1015      1.1  christos       {
   1016      1.1  christos       case insn_call_nn:
   1017      1.1  christos       case insn_call_cc_nn:
   1018      1.1  christos       case insn_rst_n:
   1019      1.1  christos 	return 1;
   1020      1.1  christos       }
   1021      1.1  christos   return 0;
   1022      1.1  christos }
   1023      1.1  christos 
   1024      1.1  christos /* Return non-zero if the instruction at ADDR is a return; zero otherwise. */
   1025      1.1  christos static int
   1026      1.1  christos z80_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
   1027      1.1  christos {
   1028      1.1  christos   gdb_byte buf[8];
   1029      1.1  christos   int size;
   1030      1.1  christos   const struct z80_insn_info *info;
   1031      1.1  christos   read_memory (addr, buf, sizeof(buf));
   1032      1.1  christos   info = z80_get_insn_info (gdbarch, buf, &size);
   1033      1.1  christos   if (info)
   1034      1.1  christos     switch (info->type)
   1035      1.1  christos       {
   1036      1.1  christos       case insn_ret:
   1037      1.1  christos       case insn_ret_cc:
   1038      1.1  christos 	return 1;
   1039      1.1  christos       }
   1040      1.1  christos   return 0;
   1041      1.1  christos }
   1042      1.1  christos 
   1043      1.1  christos /* Return non-zero if the instruction at ADDR is a jump; zero otherwise.  */
   1044      1.1  christos static int
   1045      1.1  christos z80_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
   1046      1.1  christos {
   1047      1.1  christos   gdb_byte buf[8];
   1048      1.1  christos   int size;
   1049      1.1  christos   const struct z80_insn_info *info;
   1050      1.1  christos   read_memory (addr, buf, sizeof(buf));
   1051      1.1  christos   info = z80_get_insn_info (gdbarch, buf, &size);
   1052      1.1  christos   if (info)
   1053      1.1  christos     switch (info->type)
   1054      1.1  christos       {
   1055      1.1  christos       case insn_jp_nn:
   1056      1.1  christos       case insn_jp_cc_nn:
   1057      1.1  christos       case insn_jp_rr:
   1058      1.1  christos       case insn_jr_d:
   1059      1.1  christos       case insn_jr_cc_d:
   1060      1.1  christos       case insn_djnz_d:
   1061      1.1  christos 	return 1;
   1062      1.1  christos       }
   1063      1.1  christos   return 0;
   1064      1.1  christos }
   1065      1.1  christos 
   1066      1.1  christos static const struct frame_unwind
   1067      1.1  christos z80_frame_unwind =
   1068      1.1  christos {
   1069      1.1  christos   "z80",
   1070      1.1  christos   NORMAL_FRAME,
   1071      1.1  christos   default_frame_unwind_stop_reason,
   1072      1.1  christos   z80_frame_this_id,
   1073      1.1  christos   z80_frame_prev_register,
   1074      1.1  christos   NULL, /*unwind_data*/
   1075      1.1  christos   default_frame_sniffer
   1076      1.1  christos   /*dealloc_cache*/
   1077      1.1  christos   /*prev_arch*/
   1078      1.1  christos };
   1079      1.1  christos 
   1080      1.1  christos /* Initialize the gdbarch struct for the Z80 arch */
   1081      1.1  christos static struct gdbarch *
   1082      1.1  christos z80_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   1083      1.1  christos {
   1084      1.1  christos   struct gdbarch_list *best_arch;
   1085      1.1  christos   tdesc_arch_data_up tdesc_data;
   1086      1.1  christos   unsigned long mach = info.bfd_arch_info->mach;
   1087      1.1  christos   const struct target_desc *tdesc = info.target_desc;
   1088      1.1  christos 
   1089      1.1  christos   if (!tdesc_has_registers (tdesc))
   1090      1.1  christos     /* Pick a default target description.  */
   1091      1.1  christos     tdesc = tdesc_z80;
   1092      1.1  christos 
   1093      1.1  christos   /* Check any target description for validity.  */
   1094      1.1  christos   if (tdesc_has_registers (tdesc))
   1095      1.1  christos     {
   1096      1.1  christos       const struct tdesc_feature *feature;
   1097      1.1  christos       int valid_p;
   1098      1.1  christos 
   1099      1.1  christos       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.z80.cpu");
   1100      1.1  christos       if (feature == NULL)
   1101      1.1  christos 	return NULL;
   1102      1.1  christos 
   1103      1.1  christos       tdesc_data = tdesc_data_alloc ();
   1104      1.1  christos 
   1105      1.1  christos       valid_p = 1;
   1106      1.1  christos 
   1107      1.1  christos       for (unsigned i = 0; i < Z80_NUM_REGS; i++)
   1108      1.1  christos 	valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
   1109      1.1  christos 					    z80_reg_names[i]);
   1110      1.1  christos 
   1111      1.1  christos       if (!valid_p)
   1112      1.1  christos 	return NULL;
   1113      1.1  christos     }
   1114      1.1  christos 
   1115      1.1  christos   /* If there is already a candidate, use it.  */
   1116      1.1  christos   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
   1117      1.1  christos        best_arch != NULL;
   1118      1.1  christos        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
   1119      1.1  christos     {
   1120      1.1  christos       if (mach == gdbarch_bfd_arch_info (best_arch->gdbarch)->mach)
   1121      1.1  christos 	return best_arch->gdbarch;
   1122      1.1  christos     }
   1123      1.1  christos 
   1124      1.1  christos   /* None found, create a new architecture from the information provided.  */
   1125  1.1.1.2  christos   gdbarch *gdbarch
   1126  1.1.1.2  christos     = gdbarch_alloc (&info, gdbarch_tdep_up (new z80_gdbarch_tdep));
   1127  1.1.1.2  christos   z80_gdbarch_tdep *tdep = gdbarch_tdep<z80_gdbarch_tdep> (gdbarch);
   1128      1.1  christos 
   1129      1.1  christos   if (mach == bfd_mach_ez80_adl)
   1130      1.1  christos     {
   1131      1.1  christos       tdep->addr_length = 3;
   1132      1.1  christos       set_gdbarch_max_insn_length (gdbarch, 6);
   1133      1.1  christos     }
   1134      1.1  christos   else
   1135      1.1  christos     {
   1136      1.1  christos       tdep->addr_length = 2;
   1137      1.1  christos       set_gdbarch_max_insn_length (gdbarch, 4);
   1138      1.1  christos     }
   1139      1.1  christos 
   1140      1.1  christos   /* Create a type for PC.  We can't use builtin types here, as they may not
   1141      1.1  christos      be defined.  */
   1142  1.1.1.2  christos   type_allocator alloc (gdbarch);
   1143  1.1.1.2  christos   tdep->void_type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT,
   1144  1.1.1.2  christos 				    "void");
   1145      1.1  christos   tdep->func_void_type = make_function_type (tdep->void_type, NULL);
   1146  1.1.1.2  christos   tdep->pc_type = init_pointer_type (alloc,
   1147      1.1  christos 				     tdep->addr_length * TARGET_CHAR_BIT,
   1148      1.1  christos 				     NULL, tdep->func_void_type);
   1149      1.1  christos 
   1150      1.1  christos   set_gdbarch_short_bit (gdbarch, TARGET_CHAR_BIT);
   1151      1.1  christos   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
   1152      1.1  christos   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   1153      1.1  christos   set_gdbarch_ptr_bit (gdbarch, tdep->addr_length * TARGET_CHAR_BIT);
   1154      1.1  christos   set_gdbarch_addr_bit (gdbarch, tdep->addr_length * TARGET_CHAR_BIT);
   1155      1.1  christos 
   1156      1.1  christos   set_gdbarch_num_regs (gdbarch, (mach == bfd_mach_ez80_adl) ? EZ80_NUM_REGS
   1157      1.1  christos 							     : Z80_NUM_REGS);
   1158      1.1  christos   set_gdbarch_sp_regnum (gdbarch, Z80_SP_REGNUM);
   1159      1.1  christos   set_gdbarch_pc_regnum (gdbarch, Z80_PC_REGNUM);
   1160      1.1  christos 
   1161      1.1  christos   set_gdbarch_register_name (gdbarch, z80_register_name);
   1162      1.1  christos   set_gdbarch_register_type (gdbarch, z80_register_type);
   1163      1.1  christos 
   1164      1.1  christos   /* TODO: get FP type from binary (extra flags required) */
   1165      1.1  christos   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   1166      1.1  christos   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   1167      1.1  christos   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   1168      1.1  christos   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
   1169      1.1  christos   set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
   1170      1.1  christos   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
   1171      1.1  christos 
   1172      1.1  christos   set_gdbarch_return_value (gdbarch, z80_return_value);
   1173      1.1  christos 
   1174      1.1  christos   set_gdbarch_skip_prologue (gdbarch, z80_skip_prologue);
   1175      1.1  christos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan); // falling stack
   1176      1.1  christos 
   1177      1.1  christos   set_gdbarch_software_single_step (gdbarch, z80_software_single_step);
   1178      1.1  christos   set_gdbarch_breakpoint_kind_from_pc (gdbarch, z80_breakpoint_kind_from_pc);
   1179      1.1  christos   set_gdbarch_sw_breakpoint_from_kind (gdbarch, z80_sw_breakpoint_from_kind);
   1180      1.1  christos   set_gdbarch_insn_is_call (gdbarch, z80_insn_is_call);
   1181      1.1  christos   set_gdbarch_insn_is_jump (gdbarch, z80_insn_is_jump);
   1182      1.1  christos   set_gdbarch_insn_is_ret (gdbarch, z80_insn_is_ret);
   1183      1.1  christos 
   1184      1.1  christos   set_gdbarch_overlay_update (gdbarch, z80_overlay_update);
   1185      1.1  christos 
   1186      1.1  christos   frame_unwind_append_unwinder (gdbarch, &z80_frame_unwind);
   1187      1.1  christos   if (tdesc_data)
   1188      1.1  christos     tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
   1189      1.1  christos 
   1190      1.1  christos   return gdbarch;
   1191      1.1  christos }
   1192      1.1  christos 
   1193      1.1  christos /* Table to disassemble machine codes without prefix.  */
   1194      1.1  christos static const struct z80_insn_info
   1195      1.1  christos ez80_main_insn_table[] =
   1196      1.1  christos { /* table with double prefix check */
   1197      1.1  christos   { 0100, 0377, 0, insn_force_nop}, //double prefix
   1198      1.1  christos   { 0111, 0377, 0, insn_force_nop}, //double prefix
   1199      1.1  christos   { 0122, 0377, 0, insn_force_nop}, //double prefix
   1200      1.1  christos   { 0133, 0377, 0, insn_force_nop}, //double prefix
   1201      1.1  christos   /* initial table for eZ80_z80 */
   1202      1.1  christos   { 0100, 0377, 1, insn_z80      }, //eZ80 mode prefix
   1203      1.1  christos   { 0111, 0377, 1, insn_z80      }, //eZ80 mode prefix
   1204      1.1  christos   { 0122, 0377, 1, insn_adl      }, //eZ80 mode prefix
   1205      1.1  christos   { 0133, 0377, 1, insn_adl      }, //eZ80 mode prefix
   1206      1.1  christos   /* here common Z80/Z180/eZ80 opcodes */
   1207      1.1  christos   { 0000, 0367, 1, insn_default  }, //"nop", "ex af,af'"
   1208      1.1  christos   { 0061, 0377, 3, insn_ld_sp_nn }, //"ld sp,nn"
   1209      1.1  christos   { 0001, 0317, 3, insn_default  }, //"ld rr,nn"
   1210      1.1  christos   { 0002, 0347, 1, insn_default  }, //"ld (rr),a", "ld a,(rr)"
   1211      1.1  christos   { 0042, 0347, 3, insn_default  }, //"ld (nn),hl/a", "ld hl/a,(nn)"
   1212      1.1  christos   { 0063, 0377, 1, insn_inc_sp   }, //"inc sp"
   1213      1.1  christos   { 0073, 0377, 1, insn_dec_sp   }, //"dec sp"
   1214      1.1  christos   { 0003, 0303, 1, insn_default  }, //"inc rr", "dec rr", ...
   1215      1.1  christos   { 0004, 0307, 1, insn_default  }, //"inc/dec r/(hl)"
   1216      1.1  christos   { 0006, 0307, 2, insn_default  }, //"ld r,n", "ld (hl),n"
   1217      1.1  christos   { 0020, 0377, 2, insn_djnz_d   }, //"djnz dis"
   1218      1.1  christos   { 0030, 0377, 2, insn_jr_d     }, //"jr dis"
   1219      1.1  christos   { 0040, 0347, 2, insn_jr_cc_d  }, //"jr cc,dis"
   1220      1.1  christos   { 0100, 0300, 1, insn_default  }, //"ld r,r", "halt"
   1221      1.1  christos   { 0200, 0300, 1, insn_default  }, //"alu_op a,r"
   1222      1.1  christos   { 0300, 0307, 1, insn_ret_cc   }, //"ret cc"
   1223      1.1  christos   { 0301, 0317, 1, insn_pop_rr   }, //"pop rr"
   1224      1.1  christos   { 0302, 0307, 3, insn_jp_cc_nn }, //"jp cc,nn"
   1225      1.1  christos   { 0303, 0377, 3, insn_jp_nn    }, //"jp nn"
   1226      1.1  christos   { 0304, 0307, 3, insn_call_cc_nn}, //"call cc,nn"
   1227      1.1  christos   { 0305, 0317, 1, insn_push_rr  }, //"push rr"
   1228      1.1  christos   { 0306, 0307, 2, insn_default  }, //"alu_op a,n"
   1229      1.1  christos   { 0307, 0307, 1, insn_rst_n    }, //"rst n"
   1230      1.1  christos   { 0311, 0377, 1, insn_ret      }, //"ret"
   1231      1.1  christos   { 0313, 0377, 2, insn_default  }, //CB prefix
   1232      1.1  christos   { 0315, 0377, 3, insn_call_nn  }, //"call nn"
   1233      1.1  christos   { 0323, 0367, 2, insn_default  }, //"out (n),a", "in a,(n)"
   1234      1.1  christos   { 0335, 0337, 1, insn_z80_ddfd }, //DD/FD prefix
   1235      1.1  christos   { 0351, 0377, 1, insn_jp_rr    }, //"jp (hl)"
   1236      1.1  christos   { 0355, 0377, 1, insn_z80_ed   }, //ED prefix
   1237      1.1  christos   { 0371, 0377, 1, insn_ld_sp_rr }, //"ld sp,hl"
   1238      1.1  christos   { 0000, 0000, 1, insn_default  }  //others
   1239      1.1  christos } ;
   1240      1.1  christos 
   1241      1.1  christos static const struct z80_insn_info
   1242      1.1  christos ez80_adl_main_insn_table[] =
   1243      1.1  christos { /* table with double prefix check */
   1244      1.1  christos   { 0100, 0377, 0, insn_force_nop}, //double prefix
   1245      1.1  christos   { 0111, 0377, 0, insn_force_nop}, //double prefix
   1246      1.1  christos   { 0122, 0377, 0, insn_force_nop}, //double prefix
   1247      1.1  christos   { 0133, 0377, 0, insn_force_nop}, //double prefix
   1248      1.1  christos   /* initial table for eZ80_adl */
   1249      1.1  christos   { 0000, 0367, 1, insn_default  }, //"nop", "ex af,af'"
   1250      1.1  christos   { 0061, 0377, 4, insn_ld_sp_nn }, //"ld sp,Mmn"
   1251      1.1  christos   { 0001, 0317, 4, insn_default  }, //"ld rr,Mmn"
   1252      1.1  christos   { 0002, 0347, 1, insn_default  }, //"ld (rr),a", "ld a,(rr)"
   1253      1.1  christos   { 0042, 0347, 4, insn_default  }, //"ld (Mmn),hl/a", "ld hl/a,(Mmn)"
   1254      1.1  christos   { 0063, 0377, 1, insn_inc_sp   }, //"inc sp"
   1255      1.1  christos   { 0073, 0377, 1, insn_dec_sp   }, //"dec sp"
   1256      1.1  christos   { 0003, 0303, 1, insn_default  }, //"inc rr", "dec rr", ...
   1257      1.1  christos   { 0004, 0307, 1, insn_default  }, //"inc/dec r/(hl)"
   1258      1.1  christos   { 0006, 0307, 2, insn_default  }, //"ld r,n", "ld (hl),n"
   1259      1.1  christos   { 0020, 0377, 2, insn_djnz_d   }, //"djnz dis"
   1260      1.1  christos   { 0030, 0377, 2, insn_jr_d     }, //"jr dis"
   1261      1.1  christos   { 0040, 0347, 2, insn_jr_cc_d  }, //"jr cc,dis"
   1262      1.1  christos   { 0100, 0377, 1, insn_z80      }, //eZ80 mode prefix (short instruction)
   1263      1.1  christos   { 0111, 0377, 1, insn_z80      }, //eZ80 mode prefix (short instruction)
   1264      1.1  christos   { 0122, 0377, 1, insn_adl      }, //eZ80 mode prefix (long instruction)
   1265      1.1  christos   { 0133, 0377, 1, insn_adl      }, //eZ80 mode prefix (long instruction)
   1266      1.1  christos   { 0100, 0300, 1, insn_default  }, //"ld r,r", "halt"
   1267      1.1  christos   { 0200, 0300, 1, insn_default  }, //"alu_op a,r"
   1268      1.1  christos   { 0300, 0307, 1, insn_ret_cc   }, //"ret cc"
   1269      1.1  christos   { 0301, 0317, 1, insn_pop_rr   }, //"pop rr"
   1270      1.1  christos   { 0302, 0307, 4, insn_jp_cc_nn }, //"jp cc,nn"
   1271      1.1  christos   { 0303, 0377, 4, insn_jp_nn    }, //"jp nn"
   1272      1.1  christos   { 0304, 0307, 4, insn_call_cc_nn}, //"call cc,Mmn"
   1273      1.1  christos   { 0305, 0317, 1, insn_push_rr  }, //"push rr"
   1274      1.1  christos   { 0306, 0307, 2, insn_default  }, //"alu_op a,n"
   1275      1.1  christos   { 0307, 0307, 1, insn_rst_n    }, //"rst n"
   1276      1.1  christos   { 0311, 0377, 1, insn_ret      }, //"ret"
   1277      1.1  christos   { 0313, 0377, 2, insn_default  }, //CB prefix
   1278      1.1  christos   { 0315, 0377, 4, insn_call_nn  }, //"call Mmn"
   1279      1.1  christos   { 0323, 0367, 2, insn_default  }, //"out (n),a", "in a,(n)"
   1280      1.1  christos   { 0335, 0337, 1, insn_adl_ddfd }, //DD/FD prefix
   1281      1.1  christos   { 0351, 0377, 1, insn_jp_rr    }, //"jp (hl)"
   1282      1.1  christos   { 0355, 0377, 1, insn_adl_ed   }, //ED prefix
   1283      1.1  christos   { 0371, 0377, 1, insn_ld_sp_rr }, //"ld sp,hl"
   1284      1.1  christos   { 0000, 0000, 1, insn_default  }  //others
   1285      1.1  christos };
   1286      1.1  christos 
   1287      1.1  christos /* ED prefix opcodes table.
   1288      1.1  christos    Note the instruction length does include the ED prefix (+ 1 byte)
   1289      1.1  christos */
   1290      1.1  christos static const struct z80_insn_info
   1291      1.1  christos ez80_ed_insn_table[] =
   1292      1.1  christos {
   1293      1.1  christos   /* eZ80 only instructions */
   1294      1.1  christos   { 0002, 0366, 2, insn_default    }, //"lea rr,ii+d"
   1295      1.1  christos   { 0124, 0376, 2, insn_default    }, //"lea ix,iy+d", "lea iy,ix+d"
   1296      1.1  christos   { 0145, 0377, 2, insn_default    }, //"pea ix+d"
   1297      1.1  christos   { 0146, 0377, 2, insn_default    }, //"pea iy+d"
   1298      1.1  christos   { 0164, 0377, 2, insn_default    }, //"tstio n"
   1299      1.1  christos   /* Z180/eZ80 only instructions */
   1300      1.1  christos   { 0060, 0376, 1, insn_default    }, //not an instruction
   1301      1.1  christos   { 0000, 0306, 2, insn_default    }, //"in0 r,(n)", "out0 (n),r"
   1302      1.1  christos   { 0144, 0377, 2, insn_default    }, //"tst a, n"
   1303      1.1  christos   /* common instructions */
   1304      1.1  christos   { 0173, 0377, 3, insn_ld_sp_6nn9 }, //"ld sp,(nn)"
   1305      1.1  christos   { 0103, 0307, 3, insn_default    }, //"ld (nn),rr", "ld rr,(nn)"
   1306      1.1  christos   { 0105, 0317, 1, insn_ret        }, //"retn", "reti"
   1307      1.1  christos   { 0000, 0000, 1, insn_default    }
   1308      1.1  christos };
   1309      1.1  christos 
   1310      1.1  christos static const struct z80_insn_info
   1311      1.1  christos ez80_adl_ed_insn_table[] =
   1312      1.1  christos {
   1313      1.1  christos   { 0002, 0366, 2, insn_default }, //"lea rr,ii+d"
   1314      1.1  christos   { 0124, 0376, 2, insn_default }, //"lea ix,iy+d", "lea iy,ix+d"
   1315      1.1  christos   { 0145, 0377, 2, insn_default }, //"pea ix+d"
   1316      1.1  christos   { 0146, 0377, 2, insn_default }, //"pea iy+d"
   1317      1.1  christos   { 0164, 0377, 2, insn_default }, //"tstio n"
   1318      1.1  christos   { 0060, 0376, 1, insn_default }, //not an instruction
   1319      1.1  christos   { 0000, 0306, 2, insn_default }, //"in0 r,(n)", "out0 (n),r"
   1320      1.1  christos   { 0144, 0377, 2, insn_default }, //"tst a, n"
   1321      1.1  christos   { 0173, 0377, 4, insn_ld_sp_6nn9 }, //"ld sp,(nn)"
   1322      1.1  christos   { 0103, 0307, 4, insn_default }, //"ld (nn),rr", "ld rr,(nn)"
   1323      1.1  christos   { 0105, 0317, 1, insn_ret     }, //"retn", "reti"
   1324      1.1  christos   { 0000, 0000, 1, insn_default }
   1325      1.1  christos };
   1326      1.1  christos 
   1327      1.1  christos /* table for FD and DD prefixed instructions */
   1328      1.1  christos static const struct z80_insn_info
   1329      1.1  christos ez80_ddfd_insn_table[] =
   1330      1.1  christos {
   1331      1.1  christos   /* ez80 only instructions */
   1332      1.1  christos   { 0007, 0307, 2, insn_default }, //"ld rr,(ii+d)"
   1333      1.1  christos   { 0061, 0377, 2, insn_default }, //"ld ii,(ii+d)"
   1334      1.1  christos   /* common instructions */
   1335      1.1  christos   { 0011, 0367, 2, insn_default }, //"add ii,rr"
   1336      1.1  christos   { 0041, 0377, 3, insn_default }, //"ld ii,nn"
   1337      1.1  christos   { 0042, 0367, 3, insn_default }, //"ld (nn),ii", "ld ii,(nn)"
   1338      1.1  christos   { 0043, 0367, 1, insn_default }, //"inc ii", "dec ii"
   1339      1.1  christos   { 0044, 0366, 1, insn_default }, //"inc/dec iih/iil"
   1340      1.1  christos   { 0046, 0367, 2, insn_default }, //"ld iih,n", "ld iil,n"
   1341      1.1  christos   { 0064, 0376, 2, insn_default }, //"inc (ii+d)", "dec (ii+d)"
   1342      1.1  christos   { 0066, 0377, 2, insn_default }, //"ld (ii+d),n"
   1343      1.1  christos   { 0166, 0377, 0, insn_default }, //not an instruction
   1344      1.1  christos   { 0160, 0370, 2, insn_default }, //"ld (ii+d),r"
   1345      1.1  christos   { 0104, 0306, 1, insn_default }, //"ld r,iih", "ld r,iil"
   1346      1.1  christos   { 0106, 0307, 2, insn_default }, //"ld r,(ii+d)"
   1347      1.1  christos   { 0140, 0360, 1, insn_default }, //"ld iih,r", "ld iil,r"
   1348      1.1  christos   { 0204, 0306, 1, insn_default }, //"alu_op a,iih", "alu_op a,iil"
   1349      1.1  christos   { 0206, 0307, 2, insn_default }, //"alu_op a,(ii+d)"
   1350      1.1  christos   { 0313, 0377, 3, insn_default }, //DD/FD CB dd oo instructions
   1351      1.1  christos   { 0335, 0337, 0, insn_force_nop}, //double DD/FD prefix, exec DD/FD as NOP
   1352      1.1  christos   { 0341, 0373, 1, insn_default }, //"pop ii", "push ii"
   1353      1.1  christos   { 0343, 0377, 1, insn_default }, //"ex (sp),ii"
   1354      1.1  christos   { 0351, 0377, 1, insn_jp_rr   }, //"jp (ii)"
   1355      1.1  christos   { 0371, 0377, 1, insn_ld_sp_rr}, //"ld sp,ii"
   1356      1.1  christos   { 0000, 0000, 0, insn_default }  //not an instruction, exec DD/FD as NOP
   1357      1.1  christos };
   1358      1.1  christos 
   1359      1.1  christos static const struct z80_insn_info
   1360      1.1  christos ez80_adl_ddfd_insn_table[] =
   1361      1.1  christos {
   1362      1.1  christos   { 0007, 0307, 2, insn_default }, //"ld rr,(ii+d)"
   1363      1.1  christos   { 0061, 0377, 2, insn_default }, //"ld ii,(ii+d)"
   1364      1.1  christos   { 0011, 0367, 1, insn_default }, //"add ii,rr"
   1365      1.1  christos   { 0041, 0377, 4, insn_default }, //"ld ii,nn"
   1366      1.1  christos   { 0042, 0367, 4, insn_default }, //"ld (nn),ii", "ld ii,(nn)"
   1367      1.1  christos   { 0043, 0367, 1, insn_default }, //"inc ii", "dec ii"
   1368      1.1  christos   { 0044, 0366, 1, insn_default }, //"inc/dec iih/iil"
   1369      1.1  christos   { 0046, 0367, 2, insn_default }, //"ld iih,n", "ld iil,n"
   1370      1.1  christos   { 0064, 0376, 2, insn_default }, //"inc (ii+d)", "dec (ii+d)"
   1371      1.1  christos   { 0066, 0377, 3, insn_default }, //"ld (ii+d),n"
   1372      1.1  christos   { 0166, 0377, 0, insn_default }, //not an instruction
   1373      1.1  christos   { 0160, 0370, 2, insn_default }, //"ld (ii+d),r"
   1374      1.1  christos   { 0104, 0306, 1, insn_default }, //"ld r,iih", "ld r,iil"
   1375      1.1  christos   { 0106, 0307, 2, insn_default }, //"ld r,(ii+d)"
   1376      1.1  christos   { 0140, 0360, 1, insn_default }, //"ld iih,r", "ld iil,r"
   1377      1.1  christos   { 0204, 0306, 1, insn_default }, //"alu_op a,iih", "alu_op a,iil"
   1378      1.1  christos   { 0206, 0307, 2, insn_default }, //"alu_op a,(ii+d)"
   1379      1.1  christos   { 0313, 0377, 3, insn_default }, //DD/FD CB dd oo instructions
   1380      1.1  christos   { 0335, 0337, 0, insn_force_nop}, //double DD/FD prefix, exec DD/FD as NOP
   1381      1.1  christos   { 0341, 0373, 1, insn_default }, //"pop ii", "push ii"
   1382      1.1  christos   { 0343, 0377, 1, insn_default }, //"ex (sp),ii"
   1383      1.1  christos   { 0351, 0377, 1, insn_jp_rr   }, //"jp (ii)"
   1384      1.1  christos   { 0371, 0377, 1, insn_ld_sp_rr}, //"ld sp,ii"
   1385      1.1  christos   { 0000, 0000, 0, insn_default }  //not an instruction, exec DD/FD as NOP
   1386      1.1  christos };
   1387      1.1  christos 
   1388      1.1  christos /* Return pointer to instruction information structure corresponded to opcode
   1389      1.1  christos    in buf.  */
   1390      1.1  christos static const struct z80_insn_info *
   1391      1.1  christos z80_get_insn_info (struct gdbarch *gdbarch, const gdb_byte *buf, int *size)
   1392      1.1  christos {
   1393      1.1  christos   int code;
   1394      1.1  christos   const struct z80_insn_info *info;
   1395      1.1  christos   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
   1396      1.1  christos   *size = 0;
   1397      1.1  christos   switch (mach)
   1398      1.1  christos     {
   1399      1.1  christos     case bfd_mach_ez80_z80:
   1400      1.1  christos       info = &ez80_main_insn_table[4]; /* skip force_nops */
   1401      1.1  christos       break;
   1402      1.1  christos     case bfd_mach_ez80_adl:
   1403      1.1  christos       info = &ez80_adl_main_insn_table[4]; /* skip force_nops */
   1404      1.1  christos       break;
   1405      1.1  christos     default:
   1406  1.1.1.2  christos       info = &ez80_main_insn_table[8]; /* skip eZ80 prefixes and force_nops */
   1407      1.1  christos       break;
   1408      1.1  christos     }
   1409      1.1  christos   do
   1410      1.1  christos     {
   1411      1.1  christos       for (; ((code = buf[*size]) & info->mask) != info->code; ++info)
   1412      1.1  christos 	;
   1413      1.1  christos       *size += info->size;
   1414      1.1  christos       /* process instruction type */
   1415      1.1  christos       switch (info->type)
   1416      1.1  christos 	{
   1417      1.1  christos 	case insn_z80:
   1418      1.1  christos 	  if (mach == bfd_mach_ez80_z80 || mach == bfd_mach_ez80_adl)
   1419      1.1  christos 	    info = &ez80_main_insn_table[0];
   1420      1.1  christos 	  else
   1421      1.1  christos 	    info = &ez80_main_insn_table[8];
   1422      1.1  christos 	  break;
   1423      1.1  christos 	case insn_adl:
   1424      1.1  christos 	  info = &ez80_adl_main_insn_table[0];
   1425      1.1  christos 	  break;
   1426      1.1  christos 	/*  These two (for GameBoy Z80 & Z80 Next CPUs) haven't been tested.
   1427      1.1  christos 
   1428      1.1  christos 	case bfd_mach_gbz80:
   1429      1.1  christos 	  info = &gbz80_main_insn_table[0];
   1430      1.1  christos 	  break;
   1431      1.1  christos 	case bfd_mach_z80n:
   1432      1.1  christos 	  info = &z80n_main_insn_table[0];
   1433      1.1  christos 	  break;
   1434      1.1  christos 	*/
   1435      1.1  christos 	case insn_z80_ddfd:
   1436      1.1  christos 	  if (mach == bfd_mach_ez80_z80 || mach == bfd_mach_ez80_adl)
   1437      1.1  christos 	    info = &ez80_ddfd_insn_table[0];
   1438      1.1  christos 	  else
   1439      1.1  christos 	    info = &ez80_ddfd_insn_table[2];
   1440      1.1  christos 	  break;
   1441      1.1  christos 	case insn_adl_ddfd:
   1442      1.1  christos 	  info = &ez80_adl_ddfd_insn_table[0];
   1443      1.1  christos 	  break;
   1444      1.1  christos 	case insn_z80_ed:
   1445      1.1  christos 	  info = &ez80_ed_insn_table[0];
   1446      1.1  christos 	  break;
   1447      1.1  christos 	case insn_adl_ed:
   1448      1.1  christos 	  info = &ez80_adl_ed_insn_table[0];
   1449      1.1  christos 	  break;
   1450      1.1  christos 	case insn_force_nop:
   1451      1.1  christos 	  return NULL;
   1452      1.1  christos 	default:
   1453      1.1  christos 	  return info;
   1454      1.1  christos 	}
   1455      1.1  christos     }
   1456      1.1  christos   while (1);
   1457      1.1  christos }
   1458      1.1  christos 
   1459      1.1  christos extern initialize_file_ftype _initialize_z80_tdep;
   1460      1.1  christos 
   1461      1.1  christos void
   1462      1.1  christos _initialize_z80_tdep ()
   1463      1.1  christos {
   1464      1.1  christos   gdbarch_register (bfd_arch_z80, z80_gdbarch_init);
   1465      1.1  christos   initialize_tdesc_z80 ();
   1466      1.1  christos }
   1467