z80-tdep.c 45 KB

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