// Dinkybug.c - Debugger for DinkyOS // By Ben Greenblatt // 10/28/99-12/11/99 // Modified from terminal poll code by Craig Peacock #include #include #include #define NUMREGS 32 #define HEXVAL 253 #define REGDUMP 252 #define TIMEOUT 251 #define PCDUMP 250 #define NOTASKS 249 #define BADCODE 248 #define PORT1 0x2F8 #define NUMPORTS 5 // Prototype for ascii to hex converter int atox(char *); // We use an object of type serBack to hold the original state of the // serial port. When the program exits, for any reason, the destructor // is called, which restores the data to the port. class serBack { private: char backup[NUMPORTS]; int portAddr; public: serBack(int port); ~serBack(); }; /* Defines Serial Ports Base Address */ /* COM1 0x3F8 */ /* COM2 0x2F8 */ /* COM3 0x3E8 */ /* COM4 0x2E8 */ void main(int argc, char *argv[]) { int c; // UART Status unsigned int ch; // Character from the UART int userChar; // Character input by the user char hexFlag; // Indicates that next character is a hex value to convert char regFlag; // Indicates that a register dump is in progress char PCFlag; // Indicates that a PC dump is in progress char BadFlag; // Indicates that a bad flag message is being sent int regIndex; // Register we are receiving int PCIndex; // PC word we are receiving int BadIndex; // Bad return code word count int portAddress; // Address of the com port char backup[4]; // Backups of the original IO port values // Check the input from the command line if (argc!=2) { printf("Problem: Please specify port address\n"); return; } portAddress=atox(argv[1]); serBack holder(portAddress); outportb(portAddress + 1 , 0); /* Turn off interrupts - Port1 */ /* PORT 1 - Communication Settings */ outportb(portAddress + 3 , 0x80); /* SET DLAB ON */ outportb(portAddress + 0 , 0x0c); /* Set Baud rate - Divisor Latch Low Byte */ /* Default 0x03 = 38,400 BPS */ /* 0x01 = 115,200 BPS */ /* 0x02 = 56,700 BPS */ /* 0x06 = 19,200 BPS */ /* 0x0C = 9,600 BPS */ /* 0x18 = 4,800 BPS */ /* 0x30 = 2,400 BPS */ outportb(portAddress + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */ outportb(portAddress + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */ outportb(portAddress + 2 , 0xC7); /* FIFO Control Register */ outportb(portAddress + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */ printf("\nCOMATOS Debugger\nPress ESC to quit \n"); userChar=0; regFlag=0; PCFlag=0; BadFlag=0; do { c = inportb(portAddress + 5); /* Check to see if char has been */ /* received. */ if (c & 1) { ch = inportb(portAddress); // If so, then get Char // If we are in the middle of a register dump, store the next character if (regFlag==1) { // Print the register value printf("R%02d: 0x%02x\t",regIndex,ch); // Every four registers, start new line if ((regIndex%4)==3) printf("\n"); // If this was the last register if (regIndex==(NUMREGS-1)) regFlag=0; regIndex++; } // If we are in the middle of a PC dump, just print the character // with no flag checking else if (PCFlag==1) { printf("%x",ch); if (PCIndex==1) PCFlag=0; PCIndex++; } // If we are in the middle of a bad return code, print process number // and the code else if (BadFlag==1) { BadIndex++; if (BadIndex==1) printf("0x%02x", ch); else if (BadIndex==2) { printf("\nCode: 0x%02x\n", ch); BadIndex=0; BadFlag=0; } } // This flag indicates a crash else if (ch==TIMEOUT) { printf("\nTask has timed out.\n"); printf("Task Number: "); } // This flag indicates a register dump else if (ch==REGDUMP) { printf("\nRegister Dump\n"); regFlag=1; regIndex=0; } // This flag indicates a PC value dump else if (ch==PCDUMP) { printf("\nProgram Counter: 0x"); PCFlag=1; PCIndex=0; } // This flag indicates that no tasks are running else if (ch==NOTASKS) printf("\nERROR: No tasks are running\n"); else if (ch==BADCODE) { printf("\nERROR: Bad return code from\nprocess: "); BadFlag=1; BadIndex=0; } // Print Char to Screen else printf("%c",ch); } // If key pressed, get Char if (kbhit()) { if ((userChar = getch())!=27) outportb(portAddress+0,userChar); } } while (userChar !=27); /* Quit when ESC (ASC 27) is pressed */ getch(); } serBack::serBack(int port) { portAddr=port; for (int i=0;i='0' && str[i]<='9') result|=(str[i]-'0'); else if (str[i]>='a' && str[i]<='f') result|=(str[i]+10-'a'); else if (str[i]>='A' && str[i]<='F') result|=(str[i]+10-'A'); i++; } return result; }