z80-stub.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /* Debug stub for Z80.
  2. Copyright (C) 2021-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. /* Usage:
  15. 1. Copy this file to project directory
  16. 2. Configure it commenting/uncommenting macros below or define DBG_CONFIGURED
  17. and all required macros and then include this file to one of your C-source
  18. files.
  19. 3. Implement getDebugChar() and putDebugChar(), functions must not return
  20. until data received or sent.
  21. 4. Implement all optional functions used to toggle breakpoints/watchpoints,
  22. if supported. Do not write fuctions to toggle software breakpoints if
  23. you unsure (GDB will do itself).
  24. 5. Implement serial port initialization routine called at program start.
  25. 6. Add necessary debugger entry points to your program, for example:
  26. .org 0x08 ;RST 8 handler
  27. jp _debug_swbreak
  28. ...
  29. .org 0x66 ;NMI handler
  30. jp _debug_nmi
  31. ...
  32. main_loop:
  33. halt
  34. call isDbgInterrupt
  35. jr z,101$
  36. ld hl, 2 ;EX_SIGINT
  37. push hl
  38. call _debug_exception
  39. 101$:
  40. ...
  41. 7. Compile file using SDCC (supported ports are: z80, z180, z80n, gbz80 and
  42. ez80_z80), do not use --peep-asm option. For example:
  43. $ sdcc -mz80 --opt-code-size --max-allocs-per-node 50000 z80-stub.c
  44. */
  45. /******************************************************************************\
  46. Configuration
  47. \******************************************************************************/
  48. #ifndef DBG_CONFIGURED
  49. /* Uncomment this line, if stub size is critical for you */
  50. //#define DBG_MIN_SIZE
  51. /* Comment this line out if software breakpoints are unsupported.
  52. If you have special function to toggle software breakpoints, then provide
  53. here name of these function. Expected prototype:
  54. int toggle_swbreak(int set, void *addr);
  55. function must return 0 on success. */
  56. //#define DBG_SWBREAK toggle_swbreak
  57. #define DBG_SWBREAK
  58. /* Define if one of standard RST handlers is used as software
  59. breakpoint entry point */
  60. //#define DBG_SWBREAK_RST 0x08
  61. /* if platform supports hardware breakpoints then define following macro
  62. by name of function. Fuction must have next prototype:
  63. int toggle_hwbreak(int set, void *addr);
  64. function must return 0 on success. */
  65. //#define DBG_HWBREAK toggle_hwbreak
  66. /* if platform supports hardware watchpoints then define all or some of
  67. following macros by names of functions. Fuctions prototypes:
  68. int toggle_watch(int set, void *addr, size_t size); // memory write watch
  69. int toggle_rwatch(int set, void *addr, size_t size); // memory read watch
  70. int toggle_awatch(int set, void *addr, size_t size); // memory access watch
  71. function must return 0 on success. */
  72. //#define DBG_WWATCH toggle_watch
  73. //#define DBG_RWATCH toggle_rwatch
  74. //#define DBG_AWATCH toggle_awatch
  75. /* Size of hardware breakpoint. Required to correct PC. */
  76. #define DBG_HWBREAK_SIZE 0
  77. /* Define following macro if you need custom memory read/write routine.
  78. Function should return non-zero on success, and zero on failure
  79. (for example, write to ROM area).
  80. Useful with overlays (bank switching).
  81. Do not forget to define:
  82. _ovly_table - overlay table
  83. _novlys - number of items in _ovly_table
  84. or
  85. _ovly_region_table - overlay regions table
  86. _novly_regions - number of items in _ovly_region_table
  87. _ovly_debug_prepare - function is called before overlay mapping
  88. _ovly_debug_event - function is called after overlay mapping
  89. */
  90. //#define DBG_MEMCPY memcpy
  91. /* define dedicated stack size if required */
  92. //#define DBG_STACK_SIZE 256
  93. /* max GDB packet size
  94. should be much less that DBG_STACK_SIZE because it will be allocated on stack
  95. */
  96. #define DBG_PACKET_SIZE 150
  97. /* Uncomment if required to use trampoline when resuming operation.
  98. Useful with dedicated stack when stack pointer do not point to the stack or
  99. stack is not writable */
  100. //#define DBG_USE_TRAMPOLINE
  101. /* Uncomment following macro to enable debug printing to debugger console */
  102. //#define DBG_PRINT
  103. #define DBG_NMI_EX EX_HWBREAK
  104. #define DBG_INT_EX EX_SIGINT
  105. /* Define following macro to statement, which will be exectuted after entering to
  106. stub_main function. Statement should include semicolon. */
  107. //#define DBG_ENTER debug_enter();
  108. /* Define following macro to instruction(s), which will be execute before return
  109. control to the program. It is useful when gdb-stub is placed in one of overlays.
  110. This procedure must not change any register. On top of stack before invocation
  111. will be return address of the program. */
  112. //#define DBG_RESUME jp _restore_bank
  113. /* Define following macro to the string containing memory map definition XML.
  114. GDB will use it to select proper breakpoint type (HW or SW). */
  115. /*#define DBG_MEMORY_MAP "\
  116. <memory-map>\
  117. <memory type=\"rom\" start=\"0x0000\" length=\"0x4000\"/>\
  118. <!-- <memory type=\"flash\" start=\"0x4000\" length=\"0x4000\">\
  119. <property name=\"blocksize\">128</property>\
  120. </memory> -->\
  121. <memory type=\"ram\" start=\"0x8000\" length=\"0x8000\"/>\
  122. </memory-map>\
  123. "
  124. */
  125. #endif /* DBG_CONFIGURED */
  126. /******************************************************************************\
  127. Public Interface
  128. \******************************************************************************/
  129. /* Enter to debug mode from software or hardware breakpoint.
  130. Assume address of next instruction after breakpoint call is on top of stack.
  131. Do JP _debug_swbreak or JP _debug_hwbreak from RST handler, for example.
  132. */
  133. void debug_swbreak (void);
  134. void debug_hwbreak (void);
  135. /* Jump to this function from NMI handler. Just replace RETN instruction by
  136. JP _debug_nmi
  137. Use if NMI detects request to enter to debug mode.
  138. */
  139. void debug_nmi (void);
  140. /* Jump to this function from INT handler. Just replace EI+RETI instructions by
  141. JP _debug_int
  142. Use if INT detects request to enter to debug mode.
  143. */
  144. void debug_int (void);
  145. #define EX_SWBREAK 0 /* sw breakpoint */
  146. #define EX_HWBREAK -1 /* hw breakpoint */
  147. #define EX_WWATCH -2 /* memory write watch */
  148. #define EX_RWATCH -3 /* memory read watch */
  149. #define EX_AWATCH -4 /* memory access watch */
  150. #define EX_SIGINT 2
  151. #define EX_SIGTRAP 5
  152. #define EX_SIGABRT 6
  153. #define EX_SIGBUS 10
  154. #define EX_SIGSEGV 11
  155. /* or any standard *nix signal value */
  156. /* Enter to debug mode (after receiving BREAK from GDB, for example)
  157. * Assume:
  158. * program PC in (SP+0)
  159. * caught signal in (SP+2)
  160. * program SP is SP+4
  161. */
  162. void debug_exception (int ex);
  163. /* Prints to debugger console. */
  164. void debug_print(const char *str);
  165. /******************************************************************************\
  166. Required functions
  167. \******************************************************************************/
  168. extern int getDebugChar (void);
  169. extern void putDebugChar (int ch);
  170. #ifdef DBG_SWBREAK
  171. #define DO_EXPAND(VAL) VAL ## 123456
  172. #define EXPAND(VAL) DO_EXPAND(VAL)
  173. #if EXPAND(DBG_SWBREAK) != 123456
  174. #define DBG_SWBREAK_PROC DBG_SWBREAK
  175. extern int DBG_SWBREAK(int set, void *addr);
  176. #endif
  177. #undef EXPAND
  178. #undef DO_EXPAND
  179. #endif /* DBG_SWBREAK */
  180. #ifdef DBG_HWBREAK
  181. extern int DBG_HWBREAK(int set, void *addr);
  182. #endif
  183. #ifdef DBG_MEMCPY
  184. extern void* DBG_MEMCPY (void *dest, const void *src, unsigned n);
  185. #endif
  186. #ifdef DBG_WWATCH
  187. extern int DBG_WWATCH(int set, void *addr, unsigned size);
  188. #endif
  189. #ifdef DBG_RWATCH
  190. extern int DBG_RWATCH(int set, void *addr, unsigned size);
  191. #endif
  192. #ifdef DBG_AWATCH
  193. extern int DBG_AWATCH(int set, void *addr, unsigned size);
  194. #endif
  195. /******************************************************************************\
  196. IMPLEMENTATION
  197. \******************************************************************************/
  198. #include <string.h>
  199. #ifndef NULL
  200. # define NULL (void*)0
  201. #endif
  202. typedef unsigned char byte;
  203. typedef unsigned short word;
  204. /* CPU state */
  205. #ifdef __SDCC_ez80_adl
  206. # define REG_SIZE 3
  207. #else
  208. # define REG_SIZE 2
  209. #endif /* __SDCC_ez80_adl */
  210. #define R_AF (0*REG_SIZE)
  211. #define R_BC (1*REG_SIZE)
  212. #define R_DE (2*REG_SIZE)
  213. #define R_HL (3*REG_SIZE)
  214. #define R_SP (4*REG_SIZE)
  215. #define R_PC (5*REG_SIZE)
  216. #ifndef __SDCC_gbz80
  217. #define R_IX (6*REG_SIZE)
  218. #define R_IY (7*REG_SIZE)
  219. #define R_AF_ (8*REG_SIZE)
  220. #define R_BC_ (9*REG_SIZE)
  221. #define R_DE_ (10*REG_SIZE)
  222. #define R_HL_ (11*REG_SIZE)
  223. #define R_IR (12*REG_SIZE)
  224. #ifdef __SDCC_ez80_adl
  225. #define R_SPS (13*REG_SIZE)
  226. #define NUMREGBYTES (14*REG_SIZE)
  227. #else
  228. #define NUMREGBYTES (13*REG_SIZE)
  229. #endif /* __SDCC_ez80_adl */
  230. #else
  231. #define NUMREGBYTES (6*REG_SIZE)
  232. #define FASTCALL
  233. #endif /*__SDCC_gbz80 */
  234. static byte state[NUMREGBYTES];
  235. #if DBG_PACKET_SIZE < (NUMREGBYTES*2+5)
  236. #error "Too small DBG_PACKET_SIZE"
  237. #endif
  238. #ifndef FASTCALL
  239. #define FASTCALL __z88dk_fastcall
  240. #endif
  241. /* dedicated stack */
  242. #ifdef DBG_STACK_SIZE
  243. #define LOAD_SP ld sp, #_stack + DBG_STACK_SIZE
  244. static char stack[DBG_STACK_SIZE];
  245. #else
  246. #undef DBG_USE_TRAMPOLINE
  247. #define LOAD_SP
  248. #endif
  249. #ifndef DBG_ENTER
  250. #define DBG_ENTER
  251. #endif
  252. #ifndef DBG_RESUME
  253. #define DBG_RESUME ret
  254. #endif
  255. static signed char sigval;
  256. static void stub_main (int sigval, int pc_adj);
  257. static char high_hex (byte v) FASTCALL;
  258. static char low_hex (byte v) FASTCALL;
  259. static char put_packet_info (const char *buffer) FASTCALL;
  260. static void save_cpu_state (void);
  261. static void rest_cpu_state (void);
  262. /******************************************************************************/
  263. #ifdef DBG_SWBREAK
  264. #ifdef DBG_SWBREAK_RST
  265. #define DBG_SWBREAK_SIZE 1
  266. #else
  267. #define DBG_SWBREAK_SIZE 3
  268. #endif
  269. void
  270. debug_swbreak (void) __naked
  271. {
  272. __asm
  273. ld (#_state + R_SP), sp
  274. LOAD_SP
  275. call _save_cpu_state
  276. ld hl, #-DBG_SWBREAK_SIZE
  277. push hl
  278. ld hl, #EX_SWBREAK
  279. push hl
  280. call _stub_main
  281. .globl _break_handler
  282. #ifdef DBG_SWBREAK_RST
  283. _break_handler = DBG_SWBREAK_RST
  284. #else
  285. _break_handler = _debug_swbreak
  286. #endif
  287. __endasm;
  288. }
  289. #endif /* DBG_SWBREAK */
  290. /******************************************************************************/
  291. #ifdef DBG_HWBREAK
  292. #ifndef DBG_HWBREAK_SIZE
  293. #define DBG_HWBREAK_SIZE 0
  294. #endif /* DBG_HWBREAK_SIZE */
  295. void
  296. debug_hwbreak (void) __naked
  297. {
  298. __asm
  299. ld (#_state + R_SP), sp
  300. LOAD_SP
  301. call _save_cpu_state
  302. ld hl, #-DBG_HWBREAK_SIZE
  303. push hl
  304. ld hl, #EX_HWBREAK
  305. push hl
  306. call _stub_main
  307. __endasm;
  308. }
  309. #endif /* DBG_HWBREAK_SET */
  310. /******************************************************************************/
  311. void
  312. debug_exception (int ex) __naked
  313. {
  314. __asm
  315. ld (#_state + R_SP), sp
  316. LOAD_SP
  317. call _save_cpu_state
  318. ld hl, #0
  319. push hl
  320. #ifdef __SDCC_gbz80
  321. ld hl, #_state + R_SP
  322. ld a, (hl+)
  323. ld h, (hl)
  324. ld l, a
  325. #else
  326. ld hl, (#_state + R_SP)
  327. #endif
  328. inc hl
  329. inc hl
  330. ld e, (hl)
  331. inc hl
  332. ld d, (hl)
  333. push de
  334. call _stub_main
  335. __endasm;
  336. (void)ex;
  337. }
  338. /******************************************************************************/
  339. #ifndef __SDCC_gbz80
  340. void
  341. debug_nmi(void) __naked
  342. {
  343. __asm
  344. ld (#_state + R_SP), sp
  345. LOAD_SP
  346. call _save_cpu_state
  347. ld hl, #0 ;pc_adj
  348. push hl
  349. ld hl, #DBG_NMI_EX
  350. push hl
  351. ld hl, #_stub_main
  352. push hl
  353. push hl
  354. retn
  355. __endasm;
  356. }
  357. #endif
  358. /******************************************************************************/
  359. void
  360. debug_int(void) __naked
  361. {
  362. __asm
  363. ld (#_state + R_SP), sp
  364. LOAD_SP
  365. call _save_cpu_state
  366. ld hl, #0 ;pc_adj
  367. push hl
  368. ld hl, #DBG_INT_EX
  369. push hl
  370. ld hl, #_stub_main
  371. push hl
  372. push hl
  373. ei
  374. reti
  375. __endasm;
  376. }
  377. /******************************************************************************/
  378. #ifdef DBG_PRINT
  379. void
  380. debug_print(const char *str)
  381. {
  382. putDebugChar ('$');
  383. putDebugChar ('O');
  384. char csum = 'O';
  385. for (; *str != '\0'; )
  386. {
  387. char c = high_hex (*str);
  388. csum += c;
  389. putDebugChar (c);
  390. c = low_hex (*str++);
  391. csum += c;
  392. putDebugChar (c);
  393. }
  394. putDebugChar ('#');
  395. putDebugChar (high_hex (csum));
  396. putDebugChar (low_hex (csum));
  397. }
  398. #endif /* DBG_PRINT */
  399. /******************************************************************************/
  400. static void store_pc_sp (int pc_adj) FASTCALL;
  401. #define get_reg_value(mem) (*(void* const*)(mem))
  402. #define set_reg_value(mem,val) do { (*(void**)(mem) = (val)); } while (0)
  403. static char* byte2hex(char *buf, byte val);
  404. static int hex2int (const char **buf) FASTCALL;
  405. static char* int2hex (char *buf, int v);
  406. static void get_packet (char *buffer);
  407. static void put_packet (const char *buffer);
  408. static char process (char *buffer) FASTCALL;
  409. static void rest_cpu_state (void);
  410. static void
  411. stub_main (int ex, int pc_adj)
  412. {
  413. char buffer[DBG_PACKET_SIZE+1];
  414. sigval = (signed char)ex;
  415. store_pc_sp (pc_adj);
  416. DBG_ENTER
  417. /* after starting gdb_stub must always return stop reason */
  418. *buffer = '?';
  419. for (; process (buffer);)
  420. {
  421. put_packet (buffer);
  422. get_packet (buffer);
  423. }
  424. put_packet (buffer);
  425. rest_cpu_state ();
  426. }
  427. static void
  428. get_packet (char *buffer)
  429. {
  430. byte csum;
  431. char ch;
  432. char *p;
  433. byte esc;
  434. #if DBG_PACKET_SIZE <= 256
  435. byte count; /* it is OK to use up to 256 here */
  436. #else
  437. unsigned count;
  438. #endif
  439. for (;; putDebugChar ('-'))
  440. {
  441. /* wait for packet start character */
  442. while (getDebugChar () != '$');
  443. retry:
  444. csum = 0;
  445. esc = 0;
  446. p = buffer;
  447. count = DBG_PACKET_SIZE;
  448. do
  449. {
  450. ch = getDebugChar ();
  451. switch (ch)
  452. {
  453. case '$':
  454. goto retry;
  455. case '#':
  456. goto finish;
  457. case '}':
  458. esc = 0x20;
  459. break;
  460. default:
  461. *p++ = ch ^ esc;
  462. esc = 0;
  463. --count;
  464. }
  465. csum += ch;
  466. }
  467. while (count != 0);
  468. finish:
  469. *p = '\0';
  470. if (ch != '#') /* packet is too large */
  471. continue;
  472. ch = getDebugChar ();
  473. if (ch != high_hex (csum))
  474. continue;
  475. ch = getDebugChar ();
  476. if (ch != low_hex (csum))
  477. continue;
  478. break;
  479. }
  480. putDebugChar ('+');
  481. }
  482. static void
  483. put_packet (const char *buffer)
  484. {
  485. /* $<packet info>#<checksum>. */
  486. for (;;)
  487. {
  488. putDebugChar ('$');
  489. char checksum = put_packet_info (buffer);
  490. putDebugChar ('#');
  491. putDebugChar (high_hex(checksum));
  492. putDebugChar (low_hex(checksum));
  493. for (;;)
  494. {
  495. char c = getDebugChar ();
  496. switch (c)
  497. {
  498. case '+': return;
  499. case '-': break;
  500. default:
  501. putDebugChar (c);
  502. continue;
  503. }
  504. break;
  505. }
  506. }
  507. }
  508. static char
  509. put_packet_info (const char *src) FASTCALL
  510. {
  511. char ch;
  512. char checksum = 0;
  513. for (;;)
  514. {
  515. ch = *src++;
  516. if (ch == '\0')
  517. break;
  518. if (ch == '}' || ch == '*' || ch == '#' || ch == '$')
  519. {
  520. /* escape special characters */
  521. putDebugChar ('}');
  522. checksum += '}';
  523. ch ^= 0x20;
  524. }
  525. putDebugChar (ch);
  526. checksum += ch;
  527. }
  528. return checksum;
  529. }
  530. static void
  531. store_pc_sp (int pc_adj) FASTCALL
  532. {
  533. byte *sp = get_reg_value (&state[R_SP]);
  534. byte *pc = get_reg_value (sp);
  535. pc += pc_adj;
  536. set_reg_value (&state[R_PC], pc);
  537. set_reg_value (&state[R_SP], sp + REG_SIZE);
  538. }
  539. static char *mem2hex (char *buf, const byte *mem, unsigned bytes);
  540. static char *hex2mem (byte *mem, const char *buf, unsigned bytes);
  541. /* Command processors. Takes pointer to buffer (begins from command symbol),
  542. modifies buffer, returns: -1 - empty response (ignore), 0 - success,
  543. positive: error code. */
  544. #ifdef DBG_MIN_SIZE
  545. static signed char
  546. process_question (char *p) FASTCALL
  547. {
  548. signed char sig;
  549. *p++ = 'S';
  550. sig = sigval;
  551. if (sig <= 0)
  552. sig = EX_SIGTRAP;
  553. p = byte2hex (p, (byte)sig);
  554. *p = '\0';
  555. return 0;
  556. }
  557. #else /* DBG_MIN_SIZE */
  558. static char *format_reg_value (char *p, unsigned reg_num, const byte *value);
  559. static signed char
  560. process_question (char *p) FASTCALL
  561. {
  562. signed char sig;
  563. *p++ = 'T';
  564. sig = sigval;
  565. if (sig <= 0)
  566. sig = EX_SIGTRAP;
  567. p = byte2hex (p, (byte)sig);
  568. p = format_reg_value(p, R_AF/REG_SIZE, &state[R_AF]);
  569. p = format_reg_value(p, R_SP/REG_SIZE, &state[R_SP]);
  570. p = format_reg_value(p, R_PC/REG_SIZE, &state[R_PC]);
  571. #if defined(DBG_SWBREAK_PROC) || defined(DBG_HWBREAK) || defined(DBG_WWATCH) || defined(DBG_RWATCH) || defined(DBG_AWATCH)
  572. const char *reason;
  573. unsigned addr = 0;
  574. switch (sigval)
  575. {
  576. #ifdef DBG_SWBREAK_PROC
  577. case EX_SWBREAK:
  578. reason = "swbreak";
  579. break;
  580. #endif
  581. #ifdef DBG_HWBREAK
  582. case EX_HWBREAK:
  583. reason = "hwbreak";
  584. break;
  585. #endif
  586. #ifdef DBG_WWATCH
  587. case EX_WWATCH:
  588. reason = "watch";
  589. addr = 1;
  590. break;
  591. #endif
  592. #ifdef DBG_RWATCH
  593. case EX_RWATCH:
  594. reason = "rwatch";
  595. addr = 1;
  596. break;
  597. #endif
  598. #ifdef DBG_AWATCH
  599. case EX_AWATCH:
  600. reason = "awatch";
  601. addr = 1;
  602. break;
  603. #endif
  604. default:
  605. goto finish;
  606. }
  607. while ((*p++ = *reason++))
  608. ;
  609. --p;
  610. *p++ = ':';
  611. if (addr != 0)
  612. p = int2hex(p, addr);
  613. *p++ = ';';
  614. finish:
  615. #endif /* DBG_HWBREAK, DBG_WWATCH, DBG_RWATCH, DBG_AWATCH */
  616. *p++ = '\0';
  617. return 0;
  618. }
  619. #endif /* DBG_MINSIZE */
  620. #define STRING2(x) #x
  621. #define STRING1(x) STRING2(x)
  622. #define STRING(x) STRING1(x)
  623. #ifdef DBG_MEMORY_MAP
  624. static void read_memory_map (char *buffer, unsigned offset, unsigned length);
  625. #endif
  626. static signed char
  627. process_q (char *buffer) FASTCALL
  628. {
  629. char *p;
  630. if (memcmp (buffer + 1, "Supported", 9) == 0)
  631. {
  632. memcpy (buffer, "PacketSize=", 11);
  633. p = int2hex (&buffer[11], DBG_PACKET_SIZE);
  634. #ifndef DBG_MIN_SIZE
  635. #ifdef DBG_SWBREAK_PROC
  636. memcpy (p, ";swbreak+", 9);
  637. p += 9;
  638. #endif
  639. #ifdef DBG_HWBREAK
  640. memcpy (p, ";hwbreak+", 9);
  641. p += 9;
  642. #endif
  643. #endif /* DBG_MIN_SIZE */
  644. #ifdef DBG_MEMORY_MAP
  645. memcpy (p, ";qXfer:memory-map:read+", 23);
  646. p += 23;
  647. #endif
  648. *p = '\0';
  649. return 0;
  650. }
  651. #ifdef DBG_MEMORY_MAP
  652. if (memcmp (buffer + 1, "Xfer:memory-map:read:", 21) == 0)
  653. {
  654. p = strchr (buffer + 1 + 21, ':');
  655. if (p == NULL)
  656. return 1;
  657. ++p;
  658. unsigned offset = hex2int (&p);
  659. if (*p++ != ',')
  660. return 2;
  661. unsigned length = hex2int (&p);
  662. if (length == 0)
  663. return 3;
  664. if (length > DBG_PACKET_SIZE)
  665. return 4;
  666. read_memory_map (buffer, offset, length);
  667. return 0;
  668. }
  669. #endif
  670. #ifndef DBG_MIN_SIZE
  671. if (memcmp (&buffer[1], "Attached", 9) == 0)
  672. {
  673. /* Just report that GDB attached to existing process
  674. if it is not applicable for you, then send patches */
  675. memcpy(buffer, "1", 2);
  676. return 0;
  677. }
  678. #endif /* DBG_MIN_SIZE */
  679. *buffer = '\0';
  680. return -1;
  681. }
  682. static signed char
  683. process_g (char *buffer) FASTCALL
  684. {
  685. mem2hex (buffer, state, NUMREGBYTES);
  686. return 0;
  687. }
  688. static signed char
  689. process_G (char *buffer) FASTCALL
  690. {
  691. hex2mem (state, &buffer[1], NUMREGBYTES);
  692. /* OK response */
  693. *buffer = '\0';
  694. return 0;
  695. }
  696. static signed char
  697. process_m (char *buffer) FASTCALL
  698. {/* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  699. char *p = &buffer[1];
  700. byte *addr = (void*)hex2int(&p);
  701. if (*p++ != ',')
  702. return 1;
  703. unsigned len = (unsigned)hex2int(&p);
  704. if (len == 0)
  705. return 2;
  706. if (len > DBG_PACKET_SIZE/2)
  707. return 3;
  708. p = buffer;
  709. #ifdef DBG_MEMCPY
  710. do
  711. {
  712. byte tmp[16];
  713. unsigned tlen = sizeof(tmp);
  714. if (tlen > len)
  715. tlen = len;
  716. if (!DBG_MEMCPY(tmp, addr, tlen))
  717. return 4;
  718. p = mem2hex (p, tmp, tlen);
  719. addr += tlen;
  720. len -= tlen;
  721. }
  722. while (len);
  723. #else
  724. p = mem2hex (p, addr, len);
  725. #endif
  726. return 0;
  727. }
  728. static signed char
  729. process_M (char *buffer) FASTCALL
  730. {/* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  731. char *p = &buffer[1];
  732. byte *addr = (void*)hex2int(&p);
  733. if (*p != ',')
  734. return 1;
  735. ++p;
  736. unsigned len = (unsigned)hex2int(&p);
  737. if (*p++ != ':')
  738. return 2;
  739. if (len == 0)
  740. goto end;
  741. if (len*2 + (p - buffer) > DBG_PACKET_SIZE)
  742. return 3;
  743. #ifdef DBG_MEMCPY
  744. do
  745. {
  746. byte tmp[16];
  747. unsigned tlen = sizeof(tmp);
  748. if (tlen > len)
  749. tlen = len;
  750. p = hex2mem (tmp, p, tlen);
  751. if (!DBG_MEMCPY(addr, tmp, tlen))
  752. return 4;
  753. addr += tlen;
  754. len -= tlen;
  755. }
  756. while (len);
  757. #else
  758. hex2mem (addr, p, len);
  759. #endif
  760. end:
  761. /* OK response */
  762. *buffer = '\0';
  763. return 0;
  764. }
  765. #ifndef DBG_MIN_SIZE
  766. static signed char
  767. process_X (char *buffer) FASTCALL
  768. {/* XAA..AA,LLLL: Write LLLL binary bytes at address AA.AA return OK */
  769. char *p = &buffer[1];
  770. byte *addr = (void*)hex2int(&p);
  771. if (*p != ',')
  772. return 1;
  773. ++p;
  774. unsigned len = (unsigned)hex2int(&p);
  775. if (*p++ != ':')
  776. return 2;
  777. if (len == 0)
  778. goto end;
  779. if (len + (p - buffer) > DBG_PACKET_SIZE)
  780. return 3;
  781. #ifdef DBG_MEMCPY
  782. if (!DBG_MEMCPY(addr, p, len))
  783. return 4;
  784. #else
  785. memcpy (addr, p, len);
  786. #endif
  787. end:
  788. /* OK response */
  789. *buffer = '\0';
  790. return 0;
  791. }
  792. #else /* DBG_MIN_SIZE */
  793. static signed char
  794. process_X (char *buffer) FASTCALL
  795. {
  796. (void)buffer;
  797. return -1;
  798. }
  799. #endif /* DBG_MIN_SIZE */
  800. static signed char
  801. process_c (char *buffer) FASTCALL
  802. {/* 'cAAAA' - Continue at address AAAA(optional) */
  803. const char *p = &buffer[1];
  804. if (*p != '\0')
  805. {
  806. void *addr = (void*)hex2int(&p);
  807. set_reg_value (&state[R_PC], addr);
  808. }
  809. rest_cpu_state ();
  810. return 0;
  811. }
  812. static signed char
  813. process_D (char *buffer) FASTCALL
  814. {/* 'D' - detach the program: continue execution */
  815. *buffer = '\0';
  816. return -2;
  817. }
  818. static signed char
  819. process_k (char *buffer) FASTCALL
  820. {/* 'k' - Kill the program */
  821. set_reg_value (&state[R_PC], 0);
  822. rest_cpu_state ();
  823. (void)buffer;
  824. return 0;
  825. }
  826. static signed char
  827. process_v (char *buffer) FASTCALL
  828. {
  829. #ifndef DBG_MIN_SIZE
  830. if (memcmp (&buffer[1], "Cont", 4) == 0)
  831. {
  832. if (buffer[5] == '?')
  833. {
  834. /* result response will be "vCont;c;C"; C action must be
  835. supported too, because GDB reguires at lease both of them */
  836. memcpy (&buffer[5], ";c;C", 5);
  837. return 0;
  838. }
  839. buffer[0] = '\0';
  840. if (buffer[5] == ';' && (buffer[6] == 'c' || buffer[6] == 'C'))
  841. return -2; /* resume execution */
  842. return 1;
  843. }
  844. #endif /* DBG_MIN_SIZE */
  845. return -1;
  846. }
  847. static signed char
  848. process_zZ (char *buffer) FASTCALL
  849. { /* insert/remove breakpoint */
  850. #if defined(DBG_SWBREAK_PROC) || defined(DBG_HWBREAK) || \
  851. defined(DBG_WWATCH) || defined(DBG_RWATCH) || defined(DBG_AWATCH)
  852. const byte set = (*buffer == 'Z');
  853. const char *p = &buffer[3];
  854. void *addr = (void*)hex2int(&p);
  855. if (*p != ',')
  856. return 1;
  857. p++;
  858. int kind = hex2int(&p);
  859. *buffer = '\0';
  860. switch (buffer[1])
  861. {
  862. #ifdef DBG_SWBREAK_PROC
  863. case '0': /* sw break */
  864. return DBG_SWBREAK_PROC(set, addr);
  865. #endif
  866. #ifdef DBG_HWBREAK
  867. case '1': /* hw break */
  868. return DBG_HWBREAK(set, addr);
  869. #endif
  870. #ifdef DBG_WWATCH
  871. case '2': /* write watch */
  872. return DBG_WWATCH(set, addr, kind);
  873. #endif
  874. #ifdef DBG_RWATCH
  875. case '3': /* read watch */
  876. return DBG_RWATCH(set, addr, kind);
  877. #endif
  878. #ifdef DBG_AWATCH
  879. case '4': /* access watch */
  880. return DBG_AWATCH(set, addr, kind);
  881. #endif
  882. default:; /* not supported */
  883. }
  884. #endif
  885. (void)buffer;
  886. return -1;
  887. }
  888. static signed char
  889. do_process (char *buffer) FASTCALL
  890. {
  891. switch (*buffer)
  892. {
  893. case '?': return process_question (buffer);
  894. case 'G': return process_G (buffer);
  895. case 'k': return process_k (buffer);
  896. case 'M': return process_M (buffer);
  897. case 'X': return process_X (buffer);
  898. case 'Z': return process_zZ (buffer);
  899. case 'c': return process_c (buffer);
  900. case 'D': return process_D (buffer);
  901. case 'g': return process_g (buffer);
  902. case 'm': return process_m (buffer);
  903. case 'q': return process_q (buffer);
  904. case 'v': return process_v (buffer);
  905. case 'z': return process_zZ (buffer);
  906. default: return -1; /* empty response */
  907. }
  908. }
  909. static char
  910. process (char *buffer) FASTCALL
  911. {
  912. signed char err = do_process (buffer);
  913. char *p = buffer;
  914. char ret = 1;
  915. if (err == -2)
  916. {
  917. ret = 0;
  918. err = 0;
  919. }
  920. if (err > 0)
  921. {
  922. *p++ = 'E';
  923. p = byte2hex (p, err);
  924. *p = '\0';
  925. }
  926. else if (err < 0)
  927. {
  928. *p = '\0';
  929. }
  930. else if (*p == '\0')
  931. memcpy(p, "OK", 3);
  932. return ret;
  933. }
  934. static char *
  935. byte2hex (char *p, byte v)
  936. {
  937. *p++ = high_hex (v);
  938. *p++ = low_hex (v);
  939. return p;
  940. }
  941. static signed char
  942. hex2val (unsigned char hex) FASTCALL
  943. {
  944. if (hex <= '9')
  945. return hex - '0';
  946. hex &= 0xdf; /* make uppercase */
  947. hex -= 'A' - 10;
  948. return (hex >= 10 && hex < 16) ? hex : -1;
  949. }
  950. static int
  951. hex2byte (const char *p) FASTCALL
  952. {
  953. signed char h = hex2val (p[0]);
  954. signed char l = hex2val (p[1]);
  955. if (h < 0 || l < 0)
  956. return -1;
  957. return (byte)((byte)h << 4) | (byte)l;
  958. }
  959. static int
  960. hex2int (const char **buf) FASTCALL
  961. {
  962. word r = 0;
  963. for (;; (*buf)++)
  964. {
  965. signed char a = hex2val(**buf);
  966. if (a < 0)
  967. break;
  968. r <<= 4;
  969. r += (byte)a;
  970. }
  971. return (int)r;
  972. }
  973. static char *
  974. int2hex (char *buf, int v)
  975. {
  976. buf = byte2hex(buf, (word)v >> 8);
  977. return byte2hex(buf, (byte)v);
  978. }
  979. static char
  980. high_hex (byte v) FASTCALL
  981. {
  982. return low_hex(v >> 4);
  983. }
  984. static char
  985. low_hex (byte v) FASTCALL
  986. {
  987. /*
  988. __asm
  989. ld a, l
  990. and a, #0x0f
  991. add a, #0x90
  992. daa
  993. adc a, #0x40
  994. daa
  995. ld l, a
  996. __endasm;
  997. (void)v;
  998. */
  999. v &= 0x0f;
  1000. v += '0';
  1001. if (v < '9'+1)
  1002. return v;
  1003. return v + 'a' - '0' - 10;
  1004. }
  1005. /* convert the memory, pointed to by mem into hex, placing result in buf */
  1006. /* return a pointer to the last char put in buf (null) */
  1007. static char *
  1008. mem2hex (char *buf, const byte *mem, unsigned bytes)
  1009. {
  1010. char *d = buf;
  1011. if (bytes != 0)
  1012. {
  1013. do
  1014. {
  1015. d = byte2hex (d, *mem++);
  1016. }
  1017. while (--bytes);
  1018. }
  1019. *d = 0;
  1020. return d;
  1021. }
  1022. /* convert the hex array pointed to by buf into binary, to be placed in mem
  1023. return a pointer to the character after the last byte written */
  1024. static const char *
  1025. hex2mem (byte *mem, const char *buf, unsigned bytes)
  1026. {
  1027. if (bytes != 0)
  1028. {
  1029. do
  1030. {
  1031. *mem++ = hex2byte (buf);
  1032. buf += 2;
  1033. }
  1034. while (--bytes);
  1035. }
  1036. return buf;
  1037. }
  1038. #ifdef DBG_MEMORY_MAP
  1039. static void
  1040. read_memory_map (char *buffer, unsigned offset, unsigned length)
  1041. {
  1042. const char *map = DBG_MEMORY_MAP;
  1043. const unsigned map_sz = strlen(map);
  1044. if (offset >= map_sz)
  1045. {
  1046. buffer[0] = 'l';
  1047. buffer[1] = '\0';
  1048. return;
  1049. }
  1050. if (offset + length > map_sz)
  1051. length = map_sz - offset;
  1052. buffer[0] = 'm';
  1053. memcpy (&buffer[1], &map[offset], length);
  1054. buffer[1+length] = '\0';
  1055. }
  1056. #endif
  1057. /* write string like " nn:0123" and return pointer after it */
  1058. #ifndef DBG_MIN_SIZE
  1059. static char *
  1060. format_reg_value (char *p, unsigned reg_num, const byte *value)
  1061. {
  1062. char *d = p;
  1063. unsigned char i;
  1064. d = byte2hex(d, reg_num);
  1065. *d++ = ':';
  1066. value += REG_SIZE;
  1067. i = REG_SIZE;
  1068. do
  1069. {
  1070. d = byte2hex(d, *--value);
  1071. }
  1072. while (--i != 0);
  1073. *d++ = ';';
  1074. return d;
  1075. }
  1076. #endif /* DBG_MIN_SIZE */
  1077. #ifdef __SDCC_gbz80
  1078. /* saves all state.except PC and SP */
  1079. static void
  1080. save_cpu_state() __naked
  1081. {
  1082. __asm
  1083. push af
  1084. ld a, l
  1085. ld (#_state + R_HL + 0), a
  1086. ld a, h
  1087. ld (#_state + R_HL + 1), a
  1088. ld hl, #_state + R_HL - 1
  1089. ld (hl), d
  1090. dec hl
  1091. ld (hl), e
  1092. dec hl
  1093. ld (hl), b
  1094. dec hl
  1095. ld (hl), c
  1096. dec hl
  1097. pop bc
  1098. ld (hl), b
  1099. dec hl
  1100. ld (hl), c
  1101. ret
  1102. __endasm;
  1103. }
  1104. /* restore CPU state and continue execution */
  1105. static void
  1106. rest_cpu_state() __naked
  1107. {
  1108. __asm
  1109. ;restore SP
  1110. ld a, (#_state + R_SP + 0)
  1111. ld l,a
  1112. ld a, (#_state + R_SP + 1)
  1113. ld h,a
  1114. ld sp, hl
  1115. ;push PC value as return address
  1116. ld a, (#_state + R_PC + 0)
  1117. ld l, a
  1118. ld a, (#_state + R_PC + 1)
  1119. ld h, a
  1120. push hl
  1121. ;restore registers
  1122. ld hl, #_state + R_AF
  1123. ld c, (hl)
  1124. inc hl
  1125. ld b, (hl)
  1126. inc hl
  1127. push bc
  1128. ld c, (hl)
  1129. inc hl
  1130. ld b, (hl)
  1131. inc hl
  1132. ld e, (hl)
  1133. inc hl
  1134. ld d, (hl)
  1135. inc hl
  1136. ld a, (hl)
  1137. inc hl
  1138. ld h, (hl)
  1139. ld l, a
  1140. pop af
  1141. ret
  1142. __endasm;
  1143. }
  1144. #else
  1145. /* saves all state.except PC and SP */
  1146. static void
  1147. save_cpu_state() __naked
  1148. {
  1149. __asm
  1150. ld (#_state + R_HL), hl
  1151. ld (#_state + R_DE), de
  1152. ld (#_state + R_BC), bc
  1153. push af
  1154. pop hl
  1155. ld (#_state + R_AF), hl
  1156. ld a, r ;R is increased by 7 or by 8 if called via RST
  1157. ld l, a
  1158. sub a, #7
  1159. xor a, l
  1160. and a, #0x7f
  1161. xor a, l
  1162. #ifdef __SDCC_ez80_adl
  1163. ld hl, i
  1164. ex de, hl
  1165. ld hl, #_state + R_IR
  1166. ld (hl), a
  1167. inc hl
  1168. ld (hl), e
  1169. inc hl
  1170. ld (hl), d
  1171. ld a, MB
  1172. ld (#_state + R_AF+2), a
  1173. #else
  1174. ld l, a
  1175. ld a, i
  1176. ld h, a
  1177. ld (#_state + R_IR), hl
  1178. #endif /* __SDCC_ez80_adl */
  1179. ld (#_state + R_IX), ix
  1180. ld (#_state + R_IY), iy
  1181. ex af, af' ;'
  1182. exx
  1183. ld (#_state + R_HL_), hl
  1184. ld (#_state + R_DE_), de
  1185. ld (#_state + R_BC_), bc
  1186. push af
  1187. pop hl
  1188. ld (#_state + R_AF_), hl
  1189. ret
  1190. __endasm;
  1191. }
  1192. /* restore CPU state and continue execution */
  1193. static void
  1194. rest_cpu_state() __naked
  1195. {
  1196. __asm
  1197. #ifdef DBG_USE_TRAMPOLINE
  1198. ld sp, _stack + DBG_STACK_SIZE
  1199. ld hl, (#_state + R_PC)
  1200. push hl /* resume address */
  1201. #ifdef __SDCC_ez80_adl
  1202. ld hl, 0xc30000 ; use 0xc34000 for jp.s
  1203. #else
  1204. ld hl, 0xc300
  1205. #endif
  1206. push hl /* JP opcode */
  1207. #endif /* DBG_USE_TRAMPOLINE */
  1208. ld hl, (#_state + R_AF_)
  1209. push hl
  1210. pop af
  1211. ld bc, (#_state + R_BC_)
  1212. ld de, (#_state + R_DE_)
  1213. ld hl, (#_state + R_HL_)
  1214. exx
  1215. ex af, af' ;'
  1216. ld iy, (#_state + R_IY)
  1217. ld ix, (#_state + R_IX)
  1218. #ifdef __SDCC_ez80_adl
  1219. ld a, (#_state + R_AF + 2)
  1220. ld MB, a
  1221. ld hl, (#_state + R_IR + 1) ;I register
  1222. ld i, hl
  1223. ld a, (#_state + R_IR + 0) ; R register
  1224. ld l, a
  1225. #else
  1226. ld hl, (#_state + R_IR)
  1227. ld a, h
  1228. ld i, a
  1229. ld a, l
  1230. #endif /* __SDCC_ez80_adl */
  1231. sub a, #10 ;number of M1 cycles after ld r,a
  1232. xor a, l
  1233. and a, #0x7f
  1234. xor a, l
  1235. ld r, a
  1236. ld de, (#_state + R_DE)
  1237. ld bc, (#_state + R_BC)
  1238. ld hl, (#_state + R_AF)
  1239. push hl
  1240. pop af
  1241. ld sp, (#_state + R_SP)
  1242. #ifndef DBG_USE_TRAMPOLINE
  1243. ld hl, (#_state + R_PC)
  1244. push hl
  1245. ld hl, (#_state + R_HL)
  1246. DBG_RESUME
  1247. #else
  1248. ld hl, (#_state + R_HL)
  1249. #ifdef __SDCC_ez80_adl
  1250. jp #_stack + DBG_STACK_SIZE - 4
  1251. #else
  1252. jp #_stack + DBG_STACK_SIZE - 3
  1253. #endif
  1254. #endif /* DBG_USE_TRAMPOLINE */
  1255. __endasm;
  1256. }
  1257. #endif /* __SDCC_gbz80 */