4 * Ullrich von Bassewitz, 08.08.1998
20 /*****************************************************************************/
21 /* Function forwards */
22 /*****************************************************************************/
26 /* Forwards for handler functions */
27 static char AsmHandler (void);
28 static char RegHandler (void);
29 static char StackHandler (void);
30 static char CStackHandler (void);
31 static char DumpHandler (void);
32 static char HelpHandler (void);
34 /* Forwards for other functions */
35 static void DisplayPrompt (char* s);
36 static void SingleStep (char StepInto);
37 static void RedrawStatic (char Frame);
38 static void Redraw (char Frame);
39 static char GetKeyUpdate (void);
43 /*****************************************************************************/
45 /*****************************************************************************/
47 /* Color definitions */
49 # define COLOR_BORDER (BCOLOR_DARKBLUE | CATTR_LUMA6)
50 # define COLOR_BACKGROUND COLOR_WHITE
51 # define COLOR_TEXTHIGH COLOR_BLACK
52 # define COLOR_TEXTLOW COLOR_GRAY1
53 # define COLOR_FRAMEHIGH COLOR_BLACK
54 # define COLOR_FRAMELOW COLOR_GRAY2
57 # define COLOR_BORDER COLOR_BLACK
58 # define COLOR_BACKGROUND COLOR_BLACK
59 # define COLOR_TEXTHIGH COLOR_WHITE
60 # define COLOR_TEXTLOW COLOR_GRAY3
61 # define COLOR_FRAMEHIGH COLOR_WHITE
62 # define COLOR_FRAMELOW COLOR_GRAY3
65 # define COLOR_BORDER COLOR_BLACK
66 # define COLOR_BACKGROUND COLOR_BLACK
67 # define COLOR_TEXTHIGH COLOR_BLACK
68 # define COLOR_TEXTLOW COLOR_BLACK
69 # define COLOR_FRAMEHIGH COLOR_BLACK
70 # define COLOR_FRAMELOW COLOR_BLACK
72 # define COLOR_BORDER COLOR_BLACK
73 # define COLOR_BACKGROUND COLOR_BLACK
74 # define COLOR_TEXTHIGH COLOR_WHITE
75 # define COLOR_TEXTLOW COLOR_WHITE
76 # define COLOR_FRAMEHIGH COLOR_WHITE
77 # define COLOR_FRAMELOW COLOR_WHITE
82 /* Screen definitions */
83 #if defined(__CBM610__)
87 # define DUMP_BYTES 16
88 #elif defined(__APPLE2__) || defined(__ATARI__)
100 /* Defines for opcodes */
109 #define OPC_JMPIND 0x6C
118 /* Register values that are used also in the assembler stuff */
119 extern unsigned char DbgSP; /* Stack pointer */
120 extern unsigned DbgCS; /* C stack pointer */
121 extern unsigned DbgHI; /* High 16 bit of primary reg */
125 /* Descriptor for one text line */
132 /* Window descriptor */
134 unsigned char fd_tl; /* Top left char */
135 unsigned char fd_tr; /* Top right char */
136 unsigned char fd_bl; /* Bottom left char */
137 unsigned char fd_br; /* Bottom right char */
138 unsigned char fd_x1, fd_y1; /* Upper left corner */
139 unsigned char fd_x2, fd_y2; /* Lower right corner */
140 unsigned char fd_width, fd_height; /* Redundant but faster */
141 unsigned char fd_visible; /* Is the window currently visible? */
142 char (*fd_func) (void); /* Handler function */
143 unsigned char fd_textcount; /* Number of text lines to print */
144 TextDesc* fd_text; /* Static text in the window */
149 /* Texts for the windows */
150 static TextDesc RegText [] = {
160 static TextDesc HelpText [] = {
161 { 1, 0, "F1, ? Help" },
162 { 1, 1, "F2, t Toggle breakpoint" },
163 { 1, 2, "F3, u Run until subroutine returns" },
164 { 1, 3, "F4, h Run to cursor" },
165 { 1, 4, "F7, space Step into" },
166 { 1, 5, "F8, enter Step over" },
167 { 1, 6, "1-5 Select active window" },
168 { 1, 7, "+ Page down" },
169 { 1, 8, "- Page up" },
170 { 1, 9, "Cursor Move up/down" },
171 { 1, 10, "c Continue" },
172 { 1, 11, "f Follow instruction" },
173 { 1, 12, "o Goto origin" },
174 { 1, 13, "p Use as new PC value" },
176 { 1, 15, "r Redraw screen" },
177 { 1, 16, "s Skip next instruction" },
182 static FrameDesc AsmFrame = {
183 CH_ULCORNER, CH_TTEE, CH_LTEE, CH_CROSS,
184 0, 0, MAX_X - 10, 15,
190 static FrameDesc RegFrame = {
191 CH_TTEE, CH_URCORNER, CH_LTEE, CH_RTEE,
192 MAX_X - 10, 0, MAX_X - 1, 9,
196 sizeof (RegText) / sizeof (RegText [0]), RegText
198 static FrameDesc StackFrame = {
199 CH_LTEE, CH_RTEE, CH_CROSS, CH_RTEE,
200 MAX_X - 10, 9, MAX_X - 1, 15,
206 static FrameDesc CStackFrame = {
207 CH_CROSS, CH_RTEE, CH_BTEE, CH_LRCORNER,
208 MAX_X - 10, 15, MAX_X - 1, MAX_Y - 1,
214 static FrameDesc DumpFrame = {
215 CH_LTEE, CH_CROSS, CH_LLCORNER, CH_BTEE,
216 0, 15, MAX_X - 10, MAX_Y-1,
217 MAX_X - 11, MAX_Y - 17,
222 static FrameDesc HelpFrame = {
223 CH_ULCORNER, CH_URCORNER, CH_LLCORNER, CH_LRCORNER,
224 0, 0, MAX_X - 1, MAX_Y-1,
225 MAX_X - 2, MAX_Y - 2,
228 sizeof (HelpText) / sizeof (HelpText [0]), HelpText
230 static FrameDesc* Frames [] = {
239 /* Number of active frame, -1 = none */
240 static int ActiveFrame = -1;
250 /* Other window data */
251 static unsigned AsmAddr; /* Start address of output */
252 static unsigned DumpAddr; /* Start address of output */
253 static unsigned CStackAddr; /* Start address of output */
254 static unsigned char StackAddr; /* Start address of output */
258 /* Prompt line data */
259 static char* ActivePrompt = 0; /* Last prompt line displayed */
260 static char PromptColor; /* Color behind prompt */
261 static char PromptLength; /* Length of current prompt string */
265 /* Values for the bk_use field of struct BreakPoint */
266 #define BRK_EMPTY 0x00
267 #define BRK_USER 0x01
270 /* Structure describing a breakpoint */
272 unsigned bk_addr; /* Address, 0 if unused */
273 unsigned char bk_opc; /* Opcode */
274 unsigned char bk_use; /* 1 if in use, 0 otherwise */
279 /* Temporary breakpoints - also accessed from the assembler source */
280 #define MAX_USERBREAKS 10
281 unsigned char DbgBreakCount = 0;
282 BreakPoint DbgBreaks [MAX_USERBREAKS+2];
286 /*****************************************************************************/
287 /* Forwards for functions in the assembler source */
288 /*****************************************************************************/
292 BreakPoint* DbgGetBreakSlot (void);
293 /* Search for a free breakpoint slot. Return a pointer to the slot or 0 */
295 BreakPoint* DbgIsBreak (unsigned Addr);
296 /* Check if there is a user breakpoint at the given address, if so, return
297 * a pointer to the slot, else return 0.
302 /*****************************************************************************/
303 /* Frame/window drawing code */
304 /*****************************************************************************/
308 static void DrawFrame (FrameDesc* F, char Active)
309 /* Draw one window frame */
313 unsigned char tl, tr, bl, br;
314 unsigned char x1, y1, width;
315 unsigned char OldColor;
317 /* Determine the characters for the corners, set frame color */
319 OldColor = textcolor (COLOR_FRAMEHIGH);
325 OldColor = textcolor (COLOR_FRAMELOW);
332 /* Get the coordinates into locals for faster access */
338 cputcxy (x1, y1, tl);
343 cvlinexy (x1, ++y1, F->fd_height);
351 cvlinexy (F->fd_x2, y1, F->fd_height);
353 /* If the window has static text associated, print the text */
354 textcolor (COLOR_TEXTLOW);
355 Count = F->fd_textcount;
358 cputsxy (x1 + T->x, y1 + T->y, T->text);
362 /* Set the old color */
363 textcolor (OldColor);
368 static void DrawFrames (void)
369 /* Draw all frames */
374 /* Build the frame layout of the screen */
375 for (I = 0; I < sizeof (Frames) / sizeof (Frames [0]); ++I) {
385 static void ActivateFrame (int Num, unsigned char Clear)
386 /* Activate a new frame, deactivate the old one */
391 if (ActiveFrame != Num) {
393 /* Deactivate the old one */
394 if (ActiveFrame >= 0) {
395 DrawFrame (Frames [ActiveFrame], 0);
398 /* Activate the new one */
399 if ((ActiveFrame = Num) >= 0) {
400 F = Frames [ActiveFrame];
401 /* Clear the frame if requested */
403 for (y = F->fd_y1+1; y < F->fd_y2; ++y) {
404 cclearxy (F->fd_x1+1, y, F->fd_width);
410 /* Redraw the current prompt line */
411 DisplayPrompt (ActivePrompt);
418 /*****************************************************************************/
420 /*****************************************************************************/
424 static void DisplayPrompt (char* s)
425 /* Display a prompt */
427 unsigned char OldColor;
429 /* Remember the current color */
430 OldColor = textcolor (COLOR_TEXTHIGH);
432 /* Clear the old prompt if there is one */
434 textcolor (PromptColor);
435 chlinexy ((MAX_X - PromptLength) / 2, MAX_Y-1, PromptLength);
438 /* Get the new prompt data */
440 PromptColor = OldColor;
441 PromptLength = strlen (ActivePrompt);
443 /* Display the new prompt */
444 textcolor (COLOR_TEXTHIGH);
445 cputsxy ((MAX_X - PromptLength) / 2, MAX_Y-1, ActivePrompt);
447 /* Restore the old color */
448 textcolor (PromptColor);
453 static void HelpPrompt (void)
454 /* Display a prompt line mentioning the help key */
456 DisplayPrompt ("Press F1 for help");
461 static void AnyKeyPrompt (void)
463 DisplayPrompt ("Press any key to continue");
468 static char Input (char* Prompt, char* Buf, unsigned char Count)
469 /* Read input from the user, return 1 on success, 0 if aborted */
472 unsigned char OldColor;
473 unsigned char OldCursor;
479 /* Clear the current prompt line */
480 cclearxy (0, MAX_Y-1, MAX_X);
482 /* Display the new prompt */
483 OldColor = textcolor (COLOR_TEXTHIGH);
484 cputsxy (0, MAX_Y-1, Prompt);
485 textcolor (COLOR_TEXTLOW);
487 /* Remember where we are, enable the cursor */
489 OldCursor = cursor (1);
491 /* Get input and handle it */
495 if (isalnum (c) && i < Count) {
497 cputcxy (x1 + i, MAX_Y-1, c);
499 } else if (i > 0 && c == CH_DEL) {
501 cputcxy (x1 + i, MAX_Y-1, ' ');
502 gotoxy (x1 + i, MAX_Y-1);
503 } else if (c == '\n') {
506 } else if (c == CH_ESC) {
512 /* Reset settings, display old prompt line */
514 textcolor (OldColor);
518 ActivateFrame (Frame, 0);
525 static int InputHex (char* Prompt, unsigned* Val)
526 /* Prompt for a hexadecimal value */
533 /* Read input from the user (4 digits max), check input */
534 if (Input (Prompt, Buf, sizeof (Buf)-1) && isxdigit (Buf [0])) {
536 /* Check the characters and convert to hex */
539 while ((C = *P) && isxdigit (C)) {
544 C = toupper (C) - ('A' - 10);
550 /* Assign the value */
566 static int InputGoto (unsigned* Addr)
567 /* Prompt "Goto" and read an address */
569 return InputHex ("Goto: ", Addr);
574 static void ErrorPrompt (char* Msg)
575 /* Display an error message and wait for a key */
577 /* Save the current prompt */
578 char* OldPrompt = ActivePrompt;
580 /* Display the new one */
583 /* Wait for a key and discard it */
586 /* Restore the old prompt */
587 DisplayPrompt (OldPrompt);
592 static void BreakInRomError (void)
593 /* Print an error message if we cannot set a breakpoint */
595 ErrorPrompt ("Cannot set breakpoint - press a key");
600 /*****************************************************************************/
601 /* Breakpoint handling */
602 /*****************************************************************************/
606 static void DbgSetTmpBreak (unsigned Addr)
607 /* Set a breakpoint */
609 BreakPoint* B = DbgGetBreakSlot ();
616 static void DbgToggleUserBreak (unsigned Addr)
617 /* Set a breakpoint */
619 BreakPoint* B = DbgIsBreak (Addr);
622 /* We have a breakpoint, remove it */
623 B->bk_use = BRK_EMPTY;
626 /* We don't have a breakpoint, set one */
627 if (DbgBreakCount >= MAX_USERBREAKS) {
628 ErrorPrompt ("Too many breakpoints - press a key");
630 /* Test if we can set a breakpoint at that address */
631 if (!DbgIsRAM (Addr)) {
634 /* Set the breakpoint */
635 B = DbgGetBreakSlot ();
637 B->bk_use = BRK_USER;
646 static void DbgResetTmpBreaks (void)
647 /* Reset all temporary breakpoints */
650 BreakPoint* B = DbgBreaks;
652 for (i = 0; i < MAX_USERBREAKS; ++i) {
653 if (B->bk_use == BRK_TMP) {
654 B->bk_use = BRK_EMPTY;
662 static unsigned char DbgTmpBreaksOk (void)
663 /* Check if the temporary breakpoints can be set, if so, return 1, if not,
664 * reset them all and return 0.
668 BreakPoint* B = DbgBreaks;
669 for (i = 0; i < MAX_USERBREAKS; ++i) {
670 if (B->bk_use == BRK_TMP && !DbgIsRAM (B->bk_addr)) {
672 DbgResetTmpBreaks ();
682 /*****************************************************************************/
683 /* Assembler window stuff */
684 /*****************************************************************************/
688 static unsigned AsmBack (unsigned mem, unsigned char lines)
689 /* Go back in the assembler window the given number of lines (calculate
690 * new start address).
700 cur = mem - (lines * 3) - offs;
702 cur += DbgDisAsmLen (cur);
704 in = (in + 1) & 0x1F;
706 if (cur == mem || offs == 12) {
708 return adr [(in - lines - 1) & 0x1F];
710 /* The requested address is inside an instruction, go back
711 * one more byte and try again.
723 static unsigned UpdateAsm (void)
724 /* Update the assembler window starting at the given address */
729 unsigned char width = AsmFrame.fd_width;
730 unsigned char x = AsmFrame.fd_x1 + 1;
731 unsigned m = AsmBack (AsmAddr, 2);
733 for (y = AsmFrame.fd_y1+1; y < AsmFrame.fd_y2; ++y) {
734 len = DbgDisAsm (m, buf, width);
739 if (DbgIsBreak (m)) {
756 static unsigned AsmArg16 (void)
757 /* Return a 16 bit argument */
759 return *(unsigned*)(AsmAddr+1);
764 static void AsmFollow (void)
765 /* Follow the current instruction */
767 switch (*(unsigned char*) AsmAddr) {
771 AsmAddr = AsmArg16 ();
775 AsmAddr = *(unsigned*)AsmArg16 ();
786 AsmAddr = AsmAddr + 2 + *(signed char*)(AsmAddr+1);
790 AsmAddr = (*(unsigned*) (DbgSP + 0x101) + 1);
794 AsmAddr = *(unsigned*) (DbgSP + 0x102);
802 static void AsmHome (void)
803 /* Set the cursor to home position */
810 static void InitAsm (void)
811 /* Initialize the asm window */
819 static char AsmHandler (void)
820 /* Get characters and handle them */
827 /* Update the window contents */
830 /* Read and handle input */
831 switch (c = GetKeyUpdate ()) {
838 AsmAddr = AsmBack (AsmAddr, AsmFrame.fd_height);
845 DbgToggleUserBreak (AsmAddr);
853 InputGoto (&AsmAddr);
865 AsmAddr = AsmBack (AsmAddr, 1);
869 AsmAddr += DbgDisAsmLen (AsmAddr);
881 /*****************************************************************************/
882 /* Register window stuff */
883 /*****************************************************************************/
887 static unsigned UpdateReg (void)
888 /* Update the register window */
890 unsigned char x1 = RegFrame.fd_x1 + 5;
891 unsigned char x2 = x1 + 2;
892 unsigned char y = RegFrame.fd_y1;
894 /* Print the register contents */
895 gotoxy (x1, ++y); cputhex16 (brk_pc);
896 gotoxy (x2, ++y); cputhex8 (brk_sr);
897 gotoxy (x2, ++y); cputhex8 (brk_a);
898 gotoxy (x2, ++y); cputhex8 (brk_x);
899 gotoxy (x2, ++y); cputhex8 (brk_y);
900 gotoxy (x2, ++y); cputhex8 (DbgSP);
901 gotoxy (x1, ++y); cputhex16 (DbgCS);
902 gotoxy (x1, ++y); cputhex16 (DbgHI);
910 static void InitReg (void)
911 /* Initialize the register window */
918 static char RegHandler (void)
919 /* Get characters and handle them */
921 return GetKeyUpdate ();
926 /*****************************************************************************/
927 /* Stack window stuff */
928 /*****************************************************************************/
932 static unsigned UpdateStack (void)
933 /* Update the stack window */
935 unsigned char mem = StackAddr;
936 unsigned char x1 = StackFrame.fd_x1 + 1;
937 unsigned char x2 = x1 + 6;
940 for (y = StackFrame.fd_y2-1; y > StackFrame.fd_y1; --y) {
944 cputhex8 (* (unsigned char*) (mem + 0x100));
952 static void StackHome (void)
953 /* Set the cursor to home position */
955 StackAddr = DbgSP + 1;
960 static void InitStack (void)
961 /* Initialize the stack window */
969 static char StackHandler (void)
970 /* Get characters and handle them */
973 unsigned char BytesPerPage = StackFrame.fd_height;
977 /* Read and handle input */
978 switch (c = GetKeyUpdate ()) {
981 StackAddr += BytesPerPage;
985 StackAddr -= BytesPerPage;
1005 /* Update the window contents */
1012 /*****************************************************************************/
1013 /* C Stack window stuff */
1014 /*****************************************************************************/
1018 static unsigned UpdateCStack (void)
1019 /* Update the C stack window */
1021 unsigned mem = CStackAddr;
1022 unsigned char x = CStackFrame.fd_x1 + 5;
1025 for (y = CStackFrame.fd_y2-1; y > CStackFrame.fd_y1; --y) {
1027 cputhex16 (* (unsigned*)mem);
1030 cputsxy (CStackFrame.fd_x1+1, CStackFrame.fd_y2-1, "->");
1036 static void CStackHome (void)
1037 /* Set the cursor to home position */
1044 static void InitCStack (void)
1045 /* Initialize the C stack window */
1053 static char CStackHandler (void)
1054 /* Get characters and handle them */
1057 unsigned char BytesPerPage = CStackFrame.fd_height * 2;
1061 /* Read and handle input */
1062 switch (c = GetKeyUpdate ()) {
1065 CStackAddr += BytesPerPage;
1069 CStackAddr -= BytesPerPage;
1089 /* Update the window contents */
1096 /*****************************************************************************/
1097 /* Dump window stuff */
1098 /*****************************************************************************/
1102 static unsigned UpdateDump (void)
1103 /* Update the dump window */
1107 unsigned mem = DumpAddr;
1108 unsigned char x = DumpFrame.fd_x1 + 1;
1109 unsigned char* p = (unsigned char*) mem;
1111 for (y = DumpFrame.fd_y1+1; y < DumpFrame.fd_y2; ++y) {
1112 cputsxy (x, y, DbgMemDump (mem, Buf, DUMP_BYTES));
1120 static void DumpHome (void)
1121 /* Set the cursor to home position */
1128 static char DumpHandler (void)
1129 /* Get characters and handle them */
1132 unsigned BytesPerPage = DumpFrame.fd_height * 8;
1136 /* Read and handle input */
1137 switch (c = GetKeyUpdate ()) {
1140 DumpAddr += BytesPerPage;
1144 DumpAddr -= BytesPerPage;
1148 InputGoto (&DumpAddr);
1168 /* Update the window contents */
1175 /*****************************************************************************/
1176 /* Help window stuff */
1177 /*****************************************************************************/
1181 static char HelpHandler (void)
1182 /* Get characters and handle them */
1184 /* Activate the frame */
1185 int OldActive = ActiveFrame;
1186 ActivateFrame (WIN_HELP, 1);
1188 /* Say that we're waiting for a key */
1191 /* Get a character and discard it */
1194 /* Redraw the old stuff */
1197 /* Done, return no char */
1203 /*****************************************************************************/
1205 /*****************************************************************************/
1209 static unsigned GetArg16 (void)
1210 /* Read an argument */
1212 return *(unsigned*)(brk_pc+1);
1217 static unsigned GetStack16 (unsigned char Offs)
1218 /* Fetch a 16 bit value from stack top */
1220 return *(unsigned*)(DbgSP+Offs+0x101);
1225 static void SetRTSBreak (void)
1226 /* Set a breakpoint at the return target */
1228 DbgSetTmpBreak (GetStack16 (0) + 1);
1233 static void SingleStep (char StepInto)
1237 switch (*(unsigned char*) brk_pc) {
1240 /* Set breakpoint at target */
1241 DbgSetTmpBreak (GetArg16 ());
1245 /* Indirect jump, ignore CPU error when crossing page */
1246 DbgSetTmpBreak (*(unsigned*)GetArg16 ());
1257 /* Be sure not to set the breakpoint twice if this is a jump to
1258 * the following instruction.
1260 Offs = *(signed char*)(brk_pc+1);
1262 DbgSetTmpBreak (brk_pc + Offs + 2);
1267 /* Set a breakpoint at the return target */
1272 /* Set a breakpoint at the return target */
1273 DbgSetTmpBreak (GetStack16 (1));
1278 /* Set breakpoint at target */
1279 DbgSetTmpBreak (GetArg16 ());
1285 /* Place a breakpoint behind the instruction */
1286 DbgSetTmpBreak (brk_pc + DbgDisAsmLen (brk_pc));
1291 /*****************************************************************************/
1292 /* High level window handling */
1293 /*****************************************************************************/
1297 static void RedrawStatic (char Frame)
1298 /* Redraw static display stuff */
1300 /* Reset the active frame */
1303 /* Clear the screen hide the cursor */
1304 bordercolor (COLOR_BORDER);
1305 bgcolor (COLOR_BACKGROUND);
1309 /* Build the frame layout of the screen */
1310 textcolor (COLOR_FRAMELOW);
1313 /* Draw the prompt line */
1316 /* Activate the active frame */
1317 ActivateFrame (Frame, 0);
1322 static void Redraw (char Frame)
1323 /* Redraw the display in case it's garbled */
1325 /* Redraw the static stuff */
1326 RedrawStatic (Frame);
1328 /* Init the window contents */
1338 static char GetKeyUpdate (void)
1339 /* Wait for a key updating the windows in the background */
1341 static unsigned char Win;
1343 /* While there are no keys... */
1365 Win = (Win + 1) & 0x03;
1369 /* We have a key - return it */
1375 /*****************************************************************************/
1376 /* Externally visible functions */
1377 /*****************************************************************************/
1381 void DbgEntry (void)
1382 /* Start up the debugger */
1384 static unsigned char FirstTime = 1;
1388 /* If this is the first call, setup the display */
1392 /* Draw the window, default active frame is ASM frame */
1393 RedrawStatic (WIN_ASM);
1401 /* Only initialize variables here, don't do a display update. The actual
1402 * display update will be done while waiting for user input.
1405 UpdateReg (); /* Must update this (static later) */
1410 /* Wait for user input */
1413 c = Frames [ActiveFrame]->fd_func ();
1421 ActivateFrame (c - '1', 0);
1435 /* Go until return */
1444 /* Go to cursor, only possible if cursor not at current PC */
1445 if (AsmAddr != brk_pc) {
1446 DbgSetTmpBreak (AsmAddr);
1456 if (DbgTmpBreaksOk ()) {
1457 /* Could set breakpoints */
1467 if (DbgTmpBreaksOk ()) {
1468 /* Could set breakpoints */
1479 /* Skip instruction */
1480 brk_pc += DbgDisAsmLen (brk_pc);
1486 Redraw (ActiveFrame);