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 char InputHex (char* Prompt, unsigned* Val)
526 /* Prompt for a hexadecimal value. Return 0 on failure. */
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 void ErrorPrompt (char* Msg)
567 /* Display an error message and wait for a key */
569 /* Save the current prompt */
570 char* OldPrompt = ActivePrompt;
572 /* Display the new one */
575 /* Wait for a key and discard it */
578 /* Restore the old prompt */
579 DisplayPrompt (OldPrompt);
584 static char InputGoto (unsigned* Addr)
585 /* Prompt "Goto" and read an address. Print an error and return 0 on failure. */
588 Ok = InputHex ("Goto: ", Addr);
590 ErrorPrompt ("Invalid input - press a key");
597 static void BreakInRomError (void)
598 /* Print an error message if we cannot set a breakpoint */
600 ErrorPrompt ("Cannot set breakpoint - press a key");
605 /*****************************************************************************/
606 /* Breakpoint handling */
607 /*****************************************************************************/
611 static void DbgSetTmpBreak (unsigned Addr)
612 /* Set a breakpoint */
614 BreakPoint* B = DbgGetBreakSlot ();
621 static void DbgToggleUserBreak (unsigned Addr)
622 /* Set a breakpoint */
624 BreakPoint* B = DbgIsBreak (Addr);
627 /* We have a breakpoint, remove it */
628 B->bk_use = BRK_EMPTY;
631 /* We don't have a breakpoint, set one */
632 if (DbgBreakCount >= MAX_USERBREAKS) {
633 ErrorPrompt ("Too many breakpoints - press a key");
635 /* Test if we can set a breakpoint at that address */
636 if (!DbgIsRAM (Addr)) {
639 /* Set the breakpoint */
640 B = DbgGetBreakSlot ();
642 B->bk_use = BRK_USER;
651 static void DbgResetTmpBreaks (void)
652 /* Reset all temporary breakpoints */
655 BreakPoint* B = DbgBreaks;
657 for (i = 0; i < MAX_USERBREAKS; ++i) {
658 if (B->bk_use == BRK_TMP) {
659 B->bk_use = BRK_EMPTY;
667 static unsigned char DbgTmpBreaksOk (void)
668 /* Check if the temporary breakpoints can be set, if so, return 1, if not,
669 * reset them all and return 0.
673 BreakPoint* B = DbgBreaks;
674 for (i = 0; i < MAX_USERBREAKS; ++i) {
675 if (B->bk_use == BRK_TMP && !DbgIsRAM (B->bk_addr)) {
677 DbgResetTmpBreaks ();
687 /*****************************************************************************/
688 /* Assembler window stuff */
689 /*****************************************************************************/
693 static unsigned AsmBack (unsigned mem, unsigned char lines)
694 /* Go back in the assembler window the given number of lines (calculate
695 * new start address).
705 cur = mem - (lines * 3) - offs;
707 cur += DbgDisAsmLen (cur);
709 in = (in + 1) & 0x1F;
711 if (cur == mem || offs == 12) {
713 return adr [(in - lines - 1) & 0x1F];
715 /* The requested address is inside an instruction, go back
716 * one more byte and try again.
728 static unsigned UpdateAsm (void)
729 /* Update the assembler window starting at the given address */
734 unsigned char width = AsmFrame.fd_width;
735 unsigned char x = AsmFrame.fd_x1 + 1;
736 unsigned m = AsmBack (AsmAddr, 2);
738 for (y = AsmFrame.fd_y1+1; y < AsmFrame.fd_y2; ++y) {
739 len = DbgDisAsm (m, buf, width);
744 if (DbgIsBreak (m)) {
761 static unsigned AsmArg16 (void)
762 /* Return a 16 bit argument */
764 return *(unsigned*)(AsmAddr+1);
769 static void AsmFollow (void)
770 /* Follow the current instruction */
772 switch (*(unsigned char*) AsmAddr) {
776 AsmAddr = AsmArg16 ();
780 AsmAddr = *(unsigned*)AsmArg16 ();
791 AsmAddr = AsmAddr + 2 + *(signed char*)(AsmAddr+1);
795 AsmAddr = (*(unsigned*) (DbgSP + 0x101) + 1);
799 AsmAddr = *(unsigned*) (DbgSP + 0x102);
807 static void AsmHome (void)
808 /* Set the cursor to home position */
815 static void InitAsm (void)
816 /* Initialize the asm window */
824 static char AsmHandler (void)
825 /* Get characters and handle them */
832 /* Update the window contents */
835 /* Read and handle input */
836 switch (c = GetKeyUpdate ()) {
843 AsmAddr = AsmBack (AsmAddr, AsmFrame.fd_height);
850 DbgToggleUserBreak (AsmAddr);
858 InputGoto (&AsmAddr);
870 AsmAddr = AsmBack (AsmAddr, 1);
874 AsmAddr += DbgDisAsmLen (AsmAddr);
886 /*****************************************************************************/
887 /* Register window stuff */
888 /*****************************************************************************/
892 static unsigned UpdateReg (void)
893 /* Update the register window */
895 unsigned char x1 = RegFrame.fd_x1 + 5;
896 unsigned char x2 = x1 + 2;
897 unsigned char y = RegFrame.fd_y1;
899 /* Print the register contents */
900 gotoxy (x1, ++y); cputhex16 (brk_pc);
901 gotoxy (x2, ++y); cputhex8 (brk_sr);
902 gotoxy (x2, ++y); cputhex8 (brk_a);
903 gotoxy (x2, ++y); cputhex8 (brk_x);
904 gotoxy (x2, ++y); cputhex8 (brk_y);
905 gotoxy (x2, ++y); cputhex8 (DbgSP);
906 gotoxy (x1, ++y); cputhex16 (DbgCS);
907 gotoxy (x1, ++y); cputhex16 (DbgHI);
915 static void InitReg (void)
916 /* Initialize the register window */
923 static char RegHandler (void)
924 /* Get characters and handle them */
926 return GetKeyUpdate ();
931 /*****************************************************************************/
932 /* Stack window stuff */
933 /*****************************************************************************/
937 static unsigned UpdateStack (void)
938 /* Update the stack window */
940 unsigned char mem = StackAddr;
941 unsigned char x1 = StackFrame.fd_x1 + 1;
942 unsigned char x2 = x1 + 6;
945 for (y = StackFrame.fd_y2-1; y > StackFrame.fd_y1; --y) {
949 cputhex8 (* (unsigned char*) (mem + 0x100));
957 static void StackHome (void)
958 /* Set the cursor to home position */
960 StackAddr = DbgSP + 1;
965 static void InitStack (void)
966 /* Initialize the stack window */
974 static char StackHandler (void)
975 /* Get characters and handle them */
978 unsigned char BytesPerPage = StackFrame.fd_height;
982 /* Read and handle input */
983 switch (c = GetKeyUpdate ()) {
986 StackAddr += BytesPerPage;
990 StackAddr -= BytesPerPage;
1010 /* Update the window contents */
1017 /*****************************************************************************/
1018 /* C Stack window stuff */
1019 /*****************************************************************************/
1023 static unsigned UpdateCStack (void)
1024 /* Update the C stack window */
1026 unsigned mem = CStackAddr;
1027 unsigned char x = CStackFrame.fd_x1 + 5;
1030 for (y = CStackFrame.fd_y2-1; y > CStackFrame.fd_y1; --y) {
1032 cputhex16 (* (unsigned*)mem);
1035 cputsxy (CStackFrame.fd_x1+1, CStackFrame.fd_y2-1, "->");
1041 static void CStackHome (void)
1042 /* Set the cursor to home position */
1049 static void InitCStack (void)
1050 /* Initialize the C stack window */
1058 static char CStackHandler (void)
1059 /* Get characters and handle them */
1062 unsigned char BytesPerPage = CStackFrame.fd_height * 2;
1066 /* Read and handle input */
1067 switch (c = GetKeyUpdate ()) {
1070 CStackAddr += BytesPerPage;
1074 CStackAddr -= BytesPerPage;
1094 /* Update the window contents */
1101 /*****************************************************************************/
1102 /* Dump window stuff */
1103 /*****************************************************************************/
1107 static unsigned UpdateDump (void)
1108 /* Update the dump window */
1112 unsigned mem = DumpAddr;
1113 unsigned char x = DumpFrame.fd_x1 + 1;
1114 unsigned char* p = (unsigned char*) mem;
1116 for (y = DumpFrame.fd_y1+1; y < DumpFrame.fd_y2; ++y) {
1117 cputsxy (x, y, DbgMemDump (mem, Buf, DUMP_BYTES));
1125 static void DumpHome (void)
1126 /* Set the cursor to home position */
1133 static char DumpHandler (void)
1134 /* Get characters and handle them */
1137 unsigned BytesPerPage = DumpFrame.fd_height * 8;
1141 /* Read and handle input */
1142 switch (c = GetKeyUpdate ()) {
1145 DumpAddr += BytesPerPage;
1149 DumpAddr -= BytesPerPage;
1153 InputGoto (&DumpAddr);
1173 /* Update the window contents */
1180 /*****************************************************************************/
1181 /* Help window stuff */
1182 /*****************************************************************************/
1186 static char HelpHandler (void)
1187 /* Get characters and handle them */
1189 /* Activate the frame */
1190 int OldActive = ActiveFrame;
1191 ActivateFrame (WIN_HELP, 1);
1193 /* Say that we're waiting for a key */
1196 /* Get a character and discard it */
1199 /* Redraw the old stuff */
1202 /* Done, return no char */
1208 /*****************************************************************************/
1210 /*****************************************************************************/
1214 static unsigned GetArg16 (void)
1215 /* Read an argument */
1217 return *(unsigned*)(brk_pc+1);
1222 static unsigned GetStack16 (unsigned char Offs)
1223 /* Fetch a 16 bit value from stack top */
1225 return *(unsigned*)(DbgSP+Offs+0x101);
1230 static void SetRTSBreak (void)
1231 /* Set a breakpoint at the return target */
1233 DbgSetTmpBreak (GetStack16 (0) + 1);
1238 static void SingleStep (char StepInto)
1242 switch (*(unsigned char*) brk_pc) {
1245 /* Set breakpoint at target */
1246 DbgSetTmpBreak (GetArg16 ());
1250 /* Indirect jump, ignore CPU error when crossing page */
1251 DbgSetTmpBreak (*(unsigned*)GetArg16 ());
1262 /* Be sure not to set the breakpoint twice if this is a jump to
1263 * the following instruction.
1265 Offs = *(signed char*)(brk_pc+1);
1267 DbgSetTmpBreak (brk_pc + Offs + 2);
1272 /* Set a breakpoint at the return target */
1277 /* Set a breakpoint at the return target */
1278 DbgSetTmpBreak (GetStack16 (1));
1283 /* Set breakpoint at target */
1284 DbgSetTmpBreak (GetArg16 ());
1290 /* Place a breakpoint behind the instruction */
1291 DbgSetTmpBreak (brk_pc + DbgDisAsmLen (brk_pc));
1296 /*****************************************************************************/
1297 /* High level window handling */
1298 /*****************************************************************************/
1302 static void RedrawStatic (char Frame)
1303 /* Redraw static display stuff */
1305 /* Reset the active frame */
1308 /* Clear the screen hide the cursor */
1309 bordercolor (COLOR_BORDER);
1310 bgcolor (COLOR_BACKGROUND);
1314 /* Build the frame layout of the screen */
1315 textcolor (COLOR_FRAMELOW);
1318 /* Draw the prompt line */
1321 /* Activate the active frame */
1322 ActivateFrame (Frame, 0);
1327 static void Redraw (char Frame)
1328 /* Redraw the display in case it's garbled */
1330 /* Redraw the static stuff */
1331 RedrawStatic (Frame);
1333 /* Init the window contents */
1343 static char GetKeyUpdate (void)
1344 /* Wait for a key updating the windows in the background */
1346 static unsigned char Win;
1348 /* While there are no keys... */
1370 Win = (Win + 1) & 0x03;
1374 /* We have a key - return it */
1380 /*****************************************************************************/
1381 /* Externally visible functions */
1382 /*****************************************************************************/
1386 void DbgEntry (void)
1387 /* Start up the debugger */
1389 static unsigned char FirstTime = 1;
1393 /* If this is the first call, setup the display */
1397 /* Draw the window, default active frame is ASM frame */
1398 RedrawStatic (WIN_ASM);
1406 /* Only initialize variables here, don't do a display update. The actual
1407 * display update will be done while waiting for user input.
1410 UpdateReg (); /* Must update this (static later) */
1414 /* Wait for user input */
1417 c = Frames [ActiveFrame]->fd_func ();
1425 ActivateFrame (c - '1', 0);
1439 /* Go until return */
1448 /* Go to cursor, only possible if cursor not at current PC */
1449 if (AsmAddr != brk_pc) {
1450 DbgSetTmpBreak (AsmAddr);
1460 if (DbgTmpBreaksOk ()) {
1461 /* Could set breakpoints */
1471 if (DbgTmpBreaksOk ()) {
1472 /* Could set breakpoints */
1483 /* Skip instruction */
1484 brk_pc += DbgDisAsmLen (brk_pc);
1490 Redraw (ActiveFrame);