#define STX 2 #define ETX 3 #define EOT 4 #define ACK 0x06 #define ESC 0x1B #include #include /*-- printf(), perror() --*/ #include /*-- exit() --*/ #include /*-- STDIN_FILENO, usleep --*/ #include /*-- tcgetattr(), tcsetattr() --*/ #include /*-- kill() --*/ int ch; struct termios tty, tty_old; FILE *fd_data, *fd_hs; char line[5]; pid_t pid; int status; useconds_t microseconds; int main(void) { printf("%c\n", 'a'); return(0); /*-- Create/Open handshake and data channels, and initialize them. --*/ fd_hs = fopen("kbHS.txt", "w"); /*-- Open HS channel. --*/ if( fd_hs == NULL ) { /*-- if fopen() error: --*/ perror("kb can't open handshake:"); /*-- report, --*/ exit(1); /*-- and EXIT(1). --*/ } fd_data = fopen("kbData.txt", "w"); /*-- Open data channel. --*/ if( fd_data == NULL ) { /*-- if fopen() error: --*/ perror("kb can't open data:"); /*-- report, --*/ exit(1); /*-- and EXIT(1). --*/ } fprintf(fd_hs, "%0.2x\n", ACK); /*-- Write ready hs sig. --*/ fprintf(fd_data, "%0.2x\n", 0x31); /*-- Write dummy data '1'. --*/ fclose( fd_hs ); fclose( fd_data ); /*-- Close HS and data channels. --*/ /*-- Start child for verilog simulaton. -------------------------*/ pid = fork(); if(pid == -1) { perror("kb can't fork: "); exit(1); } if (pid == 0) { status = execlp("vvp", "vvp", "a.out", 0); if (status == -1) { perror("kb child can't exec: "); exit(1); } } /*-- MAIN LOOP: ------------------------------------------------*/ /*-- Get ascii code per key event, translate ascii to string, --*/ /*-- write string to data file, write ETX string to hs file. --*/ /*-- Exit on ESC. --*/ while(1) { usleep(1000); fd_hs = fopen("kbHS.txt", "r+"); /*-- Open HS channel. --*/ if( fd_hs == NULL ) { /*-- if fopen() error: --*/ perror("kb can't open handshake:"); /*-- report, --*/ tcsetattr( 0, TCSAFLUSH, &tty_old ); /*-- restore tty, --*/ if(status < 0) perror("kb can't reset: "); /*-- if can't, report, --*/ kill(pid, SIGTERM); exit(1); /*-- and EXIT(1). --*/ } rewind(fd_hs); fprintf(fd_hs, "%c\n", ETX); /*-- Write handshake signal. --*/ fclose(fd_hs); /*-- Close HS channel. --*/ fd_data = fopen("kbData.txt", "r+"); /*-- Open data channel. --*/ if( fd_data == NULL ) { /*-- if fopen() error: --*/ perror("kb can't open handshake:"); /*-- report, --*/ tcsetattr( 0, TCSAFLUSH, &tty_old ); /*-- restore tty, --*/ if(status < 0) perror("kb can't reset: "); /*-- if can't, report, --*/ kill(pid, SIGTERM); exit(1); /*-- and EXIT(1). --*/ } rewind(fd_data); fprintf(fd_data, "%0.2x\n", ch); /*-- Write char as string. --*/ fclose(fd_data); /*-- Close data channel. --*/ }/*--while(1)--*/ }/*--main--*/ /*----------------------------------- the idea here is 1. kb.c creates: () file kbData.txt to send keyboard chars to LC3:kbDevice () file kbHS1.txt to send STX to LC3:kbdevice. () NAMED pipe kbHS2.txt to get ACK from LC3:kbdevice. () NAMED pipe dispData.txt to get char from LC3:dispDevice. () write( kbHS.txt, ACK ) () write( dispHS.txt, ACK ) () fork() () child: exec( a.out ) LOOP: LOOP-kbDevice: () readmem( kbHS.txt ) () if (read(kbHS.txt) == ACK ) if (read(stdio) == nonNull ) write( kbData.txt, char ) write( kbHS.txt, STX ) () if readmem( kbHS.txt ) == STX readmem( kbData.txt, char ) newData <== char fwrite( kbHS.txt, ACK ) () go LOOP-kbDevice LOOP-dispDevice: () DDR <== char () if readmem( dispHS.txt ) == ACK fwrite( dispData.txt, char) fwrite( dispData.txt, STX) else goto if (ACK) goto LOOP-dispDevice () if (read(dispHS.txt) == STX) char = read(dispData.txt) write( dispHS.txt, ACK) write(stdout, char) goto LOOP-kb.c */