diff --git a/frontpanel-kc8m.c b/frontpanel-kc8m.c index c3a639c..dcee5e5 100644 --- a/frontpanel-kc8m.c +++ b/frontpanel-kc8m.c @@ -10,7 +10,7 @@ */ -#define _XOPEN_SOURCE 500 // Enable timestruct definitions in C99 +#define _XOPEN_SOURCE 500 // Enable timestruct definitions in C99 #include #include @@ -27,7 +27,6 @@ #include "kk8e.h" #include "disasm.h" - #define control(ch) (ch & 037) /*********************/ @@ -36,146 +35,158 @@ // // 123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_ -// PC=_:____ DF=_ L=_ AC=____ MQ=____ AI=____ ____ ____ ____ ____ ____ ____ ____ +// PC=_:____ DF=_ L=_ AC=____ MQ=____ AI=____ ____ ____ ____ ____ ____ ____ ____ // -char *getAllRegs() { +char *getAllRegs() +{ static char buf[100]; - sprintf(buf,"PC=%d:%04o DF=%d L=%d AC=%04o MQ=%04o AI=%04o %04o %04o %04o %04o %04o %04o %04o", - ifr,pc,dfr,lnk?1:0,ac,mq,memory[010],memory[011],memory[012],memory[013],memory[014],memory[015],memory[016],memory[017] - ); + sprintf(buf, "PC=%d:%04o DF=%d L=%d AC=%04o MQ=%04o AI=%04o %04o %04o %04o %04o %04o %04o %04o", + ifr, pc, dfr, lnk ? 1 : 0, ac, mq, memory[010], memory[011], memory[012], memory[013], memory[014], memory[015], memory[016], memory[017]); return buf; } // // 123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_ -// ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ +// ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ ____=____ // -char *getAllWatched() { +char *getAllWatched() +{ static char buf[100]; static char tmps[100]; - buf[0]=0; - for (int i=0; i=0) { - sprintf(tmps,"%04o=%04o ",watch[i],memory[watch[i]]); - strcat(buf,tmps); + buf[0] = 0; + for (int i = 0; i < MAX_WATCHES; i++) + { + if (watch[i] >= 0) + { + sprintf(tmps, "%04o=%04o ", watch[i], memory[watch[i]]); + strcat(buf, tmps); } } return buf; } - -void ttyoctal(int num, int digits ,char * suffix) +void ttyoctal(int num, int digits, char *suffix) { char buf[32]; int temp = num; int i = digits; - while (i > 0) { + while (i > 0) + { i--; buf[i] = (temp & 07) | '0'; temp >>= 3; } i = digits; - if (suffix != (char *)0) { + if (suffix != (char *)0) + { int j = 0; - while (suffix[j] != '\0') { + while (suffix[j] != '\0') + { buf[i++] = suffix[j++]; } } buf[i] = '\0'; - printf("%s",buf); + printf("%s", buf); } - /**********************************************/ /* Implementation of device control interface */ /**********************************************/ -struct device_rec { - int (* mount)(); /* hook to mount file on device */ - void (* dismount)(); /* hook to dismount mounted file */ - int unit; /* thing to pass as param to mount and dismount */ - char * name; /* device name */ - char * longname; /* descriptive device name */ - char * file; /* file attached to device */ - struct device_rec * next; /* next list element */ +struct device_rec +{ + int (*mount)(); /* hook to mount file on device */ + void (*dismount)(); /* hook to dismount mounted file */ + int unit; /* thing to pass as param to mount and dismount */ + char *name; /* device name */ + char *longname; /* descriptive device name */ + char *file; /* file attached to device */ + struct device_rec *next; /* next list element */ }; -static struct device_rec * devices = NULL; +static struct device_rec *devices = NULL; void register_device( - int (* m)(), /* hook to mount file on device */ - void (* d)(), /* hook to dismount mounted file */ - int u, /* device unit */ - char * n, /* device name */ - char * l, /* descriptive device name */ - char * f /* file attached to device */ + int (*m)(), /* hook to mount file on device */ + void (*d)(), /* hook to dismount mounted file */ + int u, /* device unit */ + char *n, /* device name */ + char *l, /* descriptive device name */ + char *f /* file attached to device */ ) { - struct device_rec * temp; - temp = (struct device_rec *)malloc( sizeof( struct device_rec ) ); - temp -> next = devices; - temp -> mount = m; - temp -> dismount = d; - temp -> unit = u; - temp -> name = n; - temp -> longname = l; - temp -> file = f; + struct device_rec *temp; + temp = (struct device_rec *)malloc(sizeof(struct device_rec)); + temp->next = devices; + temp->mount = m; + temp->dismount = d; + temp->unit = u; + temp->name = n; + temp->longname = l; + temp->file = f; devices = temp; } void close_devices(void) { - struct device_rec * temp = devices; - while (temp != NULL) { - (* (temp -> dismount))( temp -> unit ); - temp = temp -> next; + struct device_rec *temp = devices; + while (temp != NULL) + { + (*(temp->dismount))(temp->unit); + temp = temp->next; } } void dump_devices(FILE *f) { - struct device_rec * temp = devices; - while (temp != NULL) { - if ((temp -> file)[0] != '\0') { /* device is mounted */ - fputs( "m ", f ); - fputs( temp -> name, f ); - fputs( " ", f ); - fputs( temp -> file, f ); - fputs( "\n", f ); + struct device_rec *temp = devices; + while (temp != NULL) + { + if ((temp->file)[0] != '\0') + { /* device is mounted */ + fputs("m ", f); + fputs(temp->name, f); + fputs(" ", f); + fputs(temp->file, f); + fputs("\n", f); } - temp = temp -> next; + temp = temp->next; } } static void list_devices(void) { - struct device_rec * temp = devices; + struct device_rec *temp = devices; printf("\r\n"); - while (temp != NULL) { - printf("%s%s%s\r\n",temp -> name, temp -> longname, temp -> file ); - temp = temp -> next; + while (temp != NULL) + { + printf("%s%s%s\r\n", temp->name, temp->longname, temp->file); + temp = temp->next; } } -static struct device_rec * get_device(char * n) +static struct device_rec *get_device(char *n) { - struct device_rec * temp = devices; - while ((temp != NULL) && (strcmp(temp -> name, n) != 0)) { - temp = temp -> next; + struct device_rec *temp = devices; + while ((temp != NULL) && (strcmp(temp->name, n) != 0)) + { + temp = temp->next; } return temp; } -void mount_device(char * n, char * f) +void mount_device(char *n, char *f) { /* quietly try to mount files on devices, used during startup */ - struct device_rec * d; - if ((d = get_device(n)) != NULL) { - int u = d -> unit; - if ( ((d -> file)[0] == '\0') && (f[0] != '\0') ) { + struct device_rec *d; + if ((d = get_device(n)) != NULL) + { + int u = d->unit; + if (((d->file)[0] == '\0') && (f[0] != '\0')) + { /* so long as a device is not mounted and so long as a nonblank name was provided try to do the mount */ - (void)(*(d -> mount))( u, f ); + (void)(*(d->mount))(u, f); /* ignore any error codes */ } } @@ -187,12 +198,9 @@ void mount_device(char * n, char * f) static struct timer console_delay; -extern void (* ttybreak) (); /* hook to tty for keyboard overrun */ - // D = list all devices // M = mount file on device - static char *help_message = "\ g - Run starting at current PC\r\n\ g - Run instructions starting at current PC.\r\n\ @@ -226,247 +234,308 @@ q - Quit emulator and exit to operating system.\r\n\ \r\n\ "; -static int isOctal(char ch) { - return (ch>='0' && ch<='7'); +static int isOctal(char ch) +{ + return (ch >= '0' && ch <= '7'); } - -static char parse_nums(char *p, int *num1, int *num2) { - *num1=-1; - *num2=-1; - char ch=0; - - while (!isOctal(*p)) { // Skip leading non-octals - if (!(*p)) return ch; // Return at end-of string +static char parse_nums(char *p, int *num1, int *num2) +{ + *num1 = -1; + *num2 = -1; + char ch = 0; + + while (!isOctal(*p)) + { // Skip leading non-octals + if (!(*p)) + return ch; // Return at end-of string p++; } - while(isOctal(*p)) { // Collect num1 while octal numbers - if (*num1==-1) *num1=0; // Zero num at first digit - *num1=(*num1)*8+(*p-'0'); + while (isOctal(*p)) + { // Collect num1 while octal numbers + if (*num1 == -1) + *num1 = 0; // Zero num at first digit + *num1 = (*num1) * 8 + (*p - '0'); p++; } - if (!(*p)) return ch; // Return at end-of string - - while (!isOctal(*p)) { // Skip middle non-octals - if (!(*p)) return ch; // Return of end-of string - if (ch==0 && (*p)!=' ') ch=*p; // Store first non-space non-octal + if (!(*p)) + return ch; // Return at end-of string + + while (!isOctal(*p)) + { // Skip middle non-octals + if (!(*p)) + return ch; // Return of end-of string + if (ch == 0 && (*p) != ' ') + ch = *p; // Store first non-space non-octal p++; } - while(isOctal(*p)) { // Collect num2 while octal numbers - if (*num2==-1) *num2=0; // Zero num at first digit - *num2=(*num2)*8+(*p-'0'); + while (isOctal(*p)) + { // Collect num2 while octal numbers + if (*num2 == -1) + *num2 = 0; // Zero num at first digit + *num2 = (*num2) * 8 + (*p - '0'); p++; } return ch; } -char* toThousandsString(long long val) { - static char result[ 128 ]; - sprintf(result, "%lld", val); - int i = strlen(result) - 1; - int i2 = i + (i / 3); - int c = 0; - result[i2 + 1] = 0; - for( ; i != 0; i-- ) { - result[i2--] = result[i]; - c++; - if( c % 3 == 0 ) - result[i2--] = ' '; - } - return result; +char *toThousandsString(long long val) +{ + static char result[128]; + sprintf(result, "%lld", val); + int i = strlen(result) - 1; + int i2 = i + (i / 3); + int c = 0; + result[i2 + 1] = 0; + for (; i != 0; i--) + { + result[i2--] = result[i]; + c++; + if (c % 3 == 0) + result[i2--] = ' '; + } + return result; } -void console(void) { - struct timespec ts100us = { .tv_sec = 0, .tv_nsec = 1000000 }; +void console(void) +{ + struct timespec ts100us = {.tv_sec = 0, .tv_nsec = 1000000}; char ch; char *p; - int i,cnt; + int i, cnt; char cmd[100]; int num1, num2; // Spend 1/10 of a second here making sure the tty buffer is processed - for (i=0; i<100; i++) { + for (i = 0; i < 100; i++) + { fire_timer(); - nanosleep(&ts100us,NULL); + nanosleep(&ts100us, NULL); } // printf("\r\n %s executed\r\n",toThousandsString(opcnt)); - printf("\r\n%s\r\n",getAllRegs()); - p=getAllWatched(); - if (strlen(p)>0) printf("%s\r\n",p); + printf("\r\n%s\r\n", getAllRegs()); + p = getAllWatched(); + if (strlen(p) > 0) + printf("%s\r\n", p); - while (run <= RUNMODE_STOPPED) { - printf(":"); + while (run <= RUNMODE_STOPPED) + { + printf(":"); fflush(stdout); ttygets(cmd, 100); - ch=parse_nums(cmd, &num1, &num2); - switch (cmd[0]) { - - case '?': // Help - fprintf(stdout,"%s", help_message); + ch = parse_nums(cmd, &num1, &num2); + switch (cmd[0]) + { + + case '?': // Help + fprintf(stdout, "%s", help_message); + break; + + case '#': // comment + case '/': // comment + break; + + case 'q': // Quit + printf("\r\nQuitting\r\n"); + fflush(stdout); + usleep(100000); + powerdown(); + break; + + case 'c': // Clear + printf("\r\nFlags and registers cleared\r\n"); + ifr = 0; + ib = ifr; + dfr = ifr; + cpma = pc | ifr; + mq = 0; + sr = 0; + clearflags(); + break; + + case 's': // Set register value + if (num1 == -1) break; - - case '#': // comment - case '/': // comment + if (ch == 'P' || ch == 'p') + pc = num1; + if (ch == 'L' || ch == 'l') + lnk = 010000 * (num1 & 1); + if (ch == 'A' || ch == 'a') + ac = num1; + if (ch == 'M' || ch == 'm') + mq = num1; + if (ch == 'S' || ch == 's') + sr = num1; + if (ch == 'I' || ch == 'i') + ifr = num1 & 7; + if (ch == 'D' || ch == 'd') + dfr = num1 & 7; + printf("\r\n%s\r\n", getAllRegs()); + if (ch == 'S' || ch == 's') + printf("Switches set to %04o\r\n", sr); + break; + + case 'm': // Set data in memory + if (num1 == -1) break; - - case 'q': // Quit - printf( "\r\nQuitting\r\n" ); - fflush(stdout); - usleep(100000); - powerdown(); - break; - - case 'c': // Clear - printf( "\r\nFlags and registers cleared\r\n" ); - ifr = 0; - ib = ifr; - dfr = ifr; - cpma = pc | ifr; - mq=0; - sr=0; - clearflags(); + if (num2 >= 0) + { + memory[num1] = num2; break; - - case 's': // Set register value - if (num1==-1) break; - if (ch=='P' || ch=='p') pc=num1; - if (ch=='L' || ch=='l') lnk=010000*(num1&1); - if (ch=='A' || ch=='a') ac=num1; - if (ch=='M' || ch=='m') mq=num1; - if (ch=='S' || ch=='s') sr=num1; - if (ch=='I' || ch=='i') ifr=num1&7; - if (ch=='D' || ch=='d') dfr=num1&7; - printf("\r\n%s\r\n",getAllRegs()); - if (ch=='S' || ch=='s') printf("Switches set to %04o\r\n",sr); - break; - - case 'm': // Set data in memory - if (num1==-1) break; - if (num2>=0) { - memory[num1]=num2; - break; - } - for (;;) { - printf("%04o: %04o New:",num1,memory[num1]); - fflush(stdout); - ttygets(cmd, 100); - fflush(stdout); - if (cmd[0]==0) { - num1++; - continue; - } - if (!isOctal(cmd[0])) break; - ch=parse_nums(cmd, &num2, &num2); - memory[num1]=num2; + } + for (;;) + { + printf("%04o: %04o New:", num1, memory[num1]); + fflush(stdout); + ttygets(cmd, 100); + fflush(stdout); + if (cmd[0] == 0) + { num1++; + continue; } + if (!isOctal(cmd[0])) + break; + ch = parse_nums(cmd, &num2, &num2); + memory[num1] = num2; + num1++; + } + break; + + case 'g': // Go/Run + trace = 0; + bpInstCnt = num1; + run = RUNMODE_STARTING; + break; + + case 't': // Trace + trace = 1; + bpInstCnt = num1; + if (bpInstCnt < 1) + bpInstCnt = 1; + run = RUNMODE_STARTING; + break; + + case 'd': // Dump memory + if (num1 == -1) break; - - case 'g': // Go/Run - trace=0; - bpInstCnt=num1; - run=RUNMODE_STARTING; - break; - - case 't': // Trace - trace=1; - bpInstCnt=num1; - if (bpInstCnt<1) bpInstCnt=1; - run=RUNMODE_STARTING; - break; - - case 'd': // Dump memory - if (num1==-1) break; - if (num2==-1) num2=16; - while (num2>0) { - printf("%04o: ",num1-(num1%8)); - for (i=0; i0) { - printf("%04o: %04o - %s\r\n",num1,memory[num1],ops[memory[num1]]); + if (num2 == -1) + num2 = 16; + while (num2 > 0) + { + printf("%04o: ", num1 - (num1 % 8)); + for (i = 0; i < num1 % 8; i++) + printf(".... "); + cnt = 8 - num1 % 8; + for (i = 0; i < cnt; i++) + { + printf("%04o ", memory[num1]); num1++; num2--; + if (num2 == 0) + break; } - break; + printf("\r\n"); + } + break; - case 'b': // Breakpoints - if (num1==-1 && num2==-1) { - for (i=0; i 0) + { + printf("%04o: %04o - %s\r\n", num1, memory[num1], ops[memory[num1]]); + num1++; + num2--; + } + break; + + case 'b': // Breakpoints + if (num1 == -1 && num2 == -1) + { + for (i = 0; i < MAX_BREAKPOINTS; i++) + { + if (bp_type[i] == 'E') + printf("BP at execution at %04o\r\n", bp[i]); + if (bp_type[i] == 'R') + printf("BP at read of %04o\r\n", bp[i]); + if (bp_type[i] == 'W') + printf("BP at write of %04o\r\n", bp[i]); + if (bp_type[i] == 'O') + printf("BP at opcode %04o\r\n", bp[i]); } - if (num1!=-1 && ch==0) { - for (i=0; i0) { - for (i=0; i 0) + { + for (i = 0; i < MAX_BREAKPOINTS; i++) + { + if (bp_type[i] == 0) + { + if (ch == 'E' || ch == 'e') + { + bp_type[i] = 'E'; + bp[i] = num1; + printf("Added BP for execution at address %04o\r\n", num1); + i = MAX_BREAKPOINTS; + } + if (ch == 'R' || ch == 'r') + { + bp_type[i] = 'R'; + bp[i] = num1; + printf("Added BP for read of address %04o\r\n", num1); + i = MAX_BREAKPOINTS; + } + if (ch == 'W' || ch == 'w') + { + bp_type[i] = 'W'; + bp[i] = num1; + printf("Added BP for write of address %04o\r\n", num1); + i = MAX_BREAKPOINTS; + } + if (ch == 'O' || ch == 'o') + { + bp_type[i] = 'O'; + bp[i] = num1; + printf("Added BP for opcode %04o\r\n", num1); + i = MAX_BREAKPOINTS; } } } - break; - case 'l': // List devices - list_devices(); - break; + } + break; + case 'l': // List devices + list_devices(); + break; } } } - // case 'G': // case 'g': /* Go */ // case 'T': @@ -519,22 +588,22 @@ void console(void) { // } // break; - /**********************************************************/ /* Interface between cpu implementation and control panel */ /**********************************************************/ static void hitbreak(void) /* called by keyboard server to get attention */ { - run=RUNMODE_BREAK; + run = RUNMODE_BREAK; } void kc8power(int argc, char **argv) /* power-on initialize */ { init_timer(console_delay); - ttybreak = hitbreak; - for (int i=0; i #include @@ -26,7 +26,8 @@ #include #include "ttyaccess.h" -struct threaddata_struct { +struct threaddata_struct +{ int termpipe; int udp_socket; }; @@ -36,38 +37,55 @@ static int pipes[2]; static struct threaddata_struct td; #define RBLEN 1024 -static volatile int rbhead = 0; // head pointer for ring buffer queue -static volatile int rbtail = 0; // tail pointer for ring buffer queue +static volatile int rbhead = 0; // head pointer for ring buffer queue +static volatile int rbtail = 0; // tail pointer for ring buffer queue static volatile char rbqueue[RBLEN]; -static pthread_mutex_t rblock; // Mutex for locking the ring buffer +static pthread_mutex_t rblock; // Mutex for locking the ring buffer static char breakcnt; +void (*ttybreak_hook)() = NULL; /* set by user, called when 5 consec ^C seen */ +void ttybreak() +{ + if (ttybreak_hook != NULL) + { + ttybreak_hook(); + } +} +void set_ttybreak(void (*new_hook)()) +{ + ttybreak_hook = new_hook; +} // // Store character into ringbuffer // -static void queue_to_rb(char ch) { - int newtail = (rbtail + 1) % RBLEN; - if (newtail != rbhead) { - rbqueue[rbtail] = ch; - rbtail = newtail; - } - if (ch==3) breakcnt++; - else breakcnt=0; +static void queue_to_rb(char ch) +{ + int newtail = (rbtail + 1) % RBLEN; + if (newtail != rbhead) + { + rbqueue[rbtail] = ch; + rbtail = newtail; + } + if (ch == 3) + breakcnt++; + else + breakcnt = 0; } - // // Get a character from ringbuffer // returns -1 if empty, else the character // -int get_from_rb(void) { - int ch=-1; +int get_from_rb(void) +{ + int ch = -1; pthread_mutex_lock(&rblock); - if (rbhead != rbtail) { + if (rbhead != rbtail) + { ch = rbqueue[rbhead]; rbhead = (rbhead + 1) % RBLEN; } @@ -75,73 +93,81 @@ int get_from_rb(void) { return ch; } - // // Return number of characters available in the ring buffer // -int count_rb(void) { +int count_rb(void) +{ int cnt; pthread_mutex_lock(&rblock); - cnt=abs(rbhead-rbtail); + cnt = abs(rbhead - rbtail); pthread_mutex_unlock(&rblock); return cnt; } - // // Set stdin to either "raw" or normal mode // mode=true for raw, false=normal // -static void set_stdin_raw(int mode) { +static void set_stdin_raw(int mode) +{ static struct termios oldtio; - static int lastMode=0; + static int lastMode = 0; struct termios newtio; - if (mode) { + if (mode) + { tcgetattr(STDIN_FILENO, &oldtio); // save current port settings - + bzero(&newtio, sizeof(newtio)); - newtio.c_lflag=0; // set input mode (non-canonical, no echo - newtio.c_cc[VTIME]=0; // inter-character timer unused - newtio.c_cc[VMIN]=1; // blocking read - + newtio.c_lflag = 0; // set input mode (non-canonical, no echo + newtio.c_cc[VTIME] = 0; // inter-character timer unused + newtio.c_cc[VMIN] = 1; // blocking read + tcflush(STDIN_FILENO, TCIFLUSH); tcsetattr(STDIN_FILENO, TCSANOW, &newtio); - } else { - if (lastMode) tcsetattr(STDIN_FILENO, TCSANOW, &oldtio); } - lastMode=mode; + else + { + if (lastMode) + tcsetattr(STDIN_FILENO, TCSANOW, &oldtio); + } + lastMode = mode; } - // // Create and bind to a listening UDP socket // -static int create_udp_socket(int port) { - struct sockaddr_in server_address; +static int create_udp_socket(int port) +{ + struct sockaddr_in server_address; int sock; int r; - memset(&server_address, 0, sizeof(server_address)); - server_address.sin_family = AF_INET; - server_address.sin_port = htons(port); - server_address.sin_addr.s_addr = htonl(INADDR_ANY); + memset(&server_address, 0, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_port = htons(port); + server_address.sin_addr.s_addr = htonl(INADDR_ANY); sock = socket(PF_INET, SOCK_DGRAM, 0); - for (int i=0; i<5; i++) { - r=bind(sock, (struct sockaddr *)&server_address, sizeof(server_address)); - if (r==0) break; + for (int i = 0; i < 5; i++) + { + r = bind(sock, (struct sockaddr *)&server_address, sizeof(server_address)); + if (r == 0) + break; sleep(1); } - if (r < 0) { - printf("Could not bind socket to UDP port %d\n",port); - exit(1); - } - return sock; + if (r < 0) + { + printf("Could not bind socket to UDP port %d\n", port); + exit(1); + } + return sock; } // // Thread that waits for keypresses or udp messages and puts them into the ring buffer // -void *keyin_thread(void *td) { +void *keyin_thread(void *td) +{ char buffer[256]; int len; fd_set rfds; @@ -149,125 +175,146 @@ void *keyin_thread(void *td) { int termpipe = ((struct threaddata_struct *)td)->termpipe; int udp_socket = ((struct threaddata_struct *)td)->udp_socket; - for (;;) { + for (;;) + { FD_ZERO(&rfds); FD_SET(STDIN_FILENO, &rfds); FD_SET(termpipe, &rfds); FD_SET(udp_socket, &rfds); - while (select(udp_socket+1, &rfds, NULL, NULL, NULL) == 0); + while (select(udp_socket + 1, &rfds, NULL, NULL, NULL) == 0) + ; - if (FD_ISSET(termpipe, &rfds)) { + if (FD_ISSET(termpipe, &rfds)) + { close(termpipe); break; } - if (FD_ISSET(STDIN_FILENO, &rfds)) { - len=read(STDIN_FILENO, buffer, sizeof(buffer)); + if (FD_ISSET(STDIN_FILENO, &rfds)) + { + len = read(STDIN_FILENO, buffer, sizeof(buffer)); pthread_mutex_lock(&rblock); - for (int i=0; i4) ttybreak(); - nanosleep(&ts100us,NULL); +int ttygetc(void) +{ + int ch; + struct timespec ts100us = {.tv_sec = 0, .tv_nsec = 1000000}; + do + { + ch = get_from_rb(); + if (ch < 0) + { + if (breakcnt > 4) + ttybreak(); + nanosleep(&ts100us, NULL); } - } while (ch<0); - return ch; + } while (ch < 0); + return ch; } // // poll for a character from the console // -int ttypoll(void) { - if (breakcnt>4) ttybreak(); - return get_from_rb(); +int ttypoll(void) +{ + if (breakcnt > 4) + ttybreak(); + return get_from_rb(); } - // // // -void ttygets(char * buf, int len) { - int i = 0; - int ch; - char c; - do { - ch = ttygetc(); - if (ch == '\b' || ch==127) { - if (i > 0) { - write(STDOUT_FILENO, "\b \b", 3); - i--; - } - } else if (ch >= ' ') { - if (i < (len - 1)) { - c=ch; - write(STDOUT_FILENO, &c, 1); - buf[i] = ch; - i++; - } - } - } while ((ch != '\r') && (ch != '\n')); - write(STDOUT_FILENO, "\r\n", 2); - if (i < len) { - buf[i] = '\0'; - } else { - buf[len - 1] = '\0'; - } +void ttygets(char *buf, int len) +{ + int i = 0; + int ch; + char c; + do + { + ch = ttygetc(); + if (ch == '\b' || ch == 127) + { + if (i > 0) + { + write(STDOUT_FILENO, "\b \b", 3); + i--; + } + } + else if (ch >= ' ') + { + if (i < (len - 1)) + { + c = ch; + write(STDOUT_FILENO, &c, 1); + buf[i] = ch; + i++; + } + } + } while ((ch != '\r') && (ch != '\n')); + write(STDOUT_FILENO, "\r\n", 2); + if (i < len) + { + buf[i] = '\0'; + } + else + { + buf[len - 1] = '\0'; + } } diff --git a/ttyaccess.h b/ttyaccess.h index 4410128..8f21bce 100644 --- a/ttyaccess.h +++ b/ttyaccess.h @@ -1,7 +1,8 @@ -extern void(*ttybreak)(); +void ttybreak(); +void set_ttybreak(void (*new_hook)()); int ttygetc(void); -void ttygets(char *buf,int len); +void ttygets(char *buf, int len); int ttypoll(void); void comms_init(void); void comms_cleanup(void); \ No newline at end of file