//****************************************************************************** /// _read - read a file descriptor. int _read(int file, char *buf, int len) { // stdin reads from the UART by default if (file == STDIN_FILENO) { int i; int seen_newline = 0; // read data for(i = 0; i < len; i++) { int s, c; // return if nothing is available and we already have a full line __PATMOS_UART_STATUS(s); if ((s & (__PATMOS_UART_DAV | __PATMOS_UART_PAE)) == 0 && seen_newline) { break; } // wait for data to be available from the UART while ((s & (__PATMOS_UART_DAV | __PATMOS_UART_PAE)) == 0) { __PATMOS_UART_STATUS(s); } // simulator signals EOF via parity error if ((s & __PATMOS_UART_PAE) != 0) { break; } // read the data from the UART. __PATMOS_UART_RD_DATA(c); // copy data into the given buffer. *buf++ = c & 0xff; // stdin is usually line buffered, so stop reading on newline if (c == '\n') { seen_newline = 1; } } // keep track of offset for lseek __patmos_stdin_offset += i; // clear error code and return errno = 0; return i; } // TODO: implement for simulator target errno = EBADF; return -1; }
//****************************************************************************** /// _lseek - seek position for a file descriptor. int _lseek(int fd, off_t offset, int whenceq) { // For stdin and (positive) seek relative to current pos, we just consume bytes if (fd == STDIN_FILENO) { int curoff; switch (whenceq) { case SEEK_SET: curoff = offset - __patmos_stdin_offset; break; case SEEK_CUR: curoff = offset; break; default: // We do not support SEEK_END for now errno = EINVAL; return -1; } if (curoff < 0) { // We do not support seeking backwards errno = EINVAL; return -1; } int i; // consume data from UART for(i = 0; i < curoff; i++) { int s, c; // wait for data to be available from the UART do { __PATMOS_UART_STATUS(s); } while((s & (__PATMOS_UART_DAV | __PATMOS_UART_PAE)) == 0); // reached EOF? if ((s & __PATMOS_UART_PAE) == 0) { // read the data from the UART. __PATMOS_UART_RD_DATA(c); } else { // signal EOF errno = EINVAL; return -1; } } // successful, update offset __patmos_stdin_offset += curoff; return __patmos_stdin_offset; } // TODO: implement for simulator target errno = EBADF; return -1; }
/* * console_inbyte_nonblocking * * This routine polls for a character. */ int console_inbyte_nonblocking( int port ) { int s, c; /* wait for data to be available from the UART */ do { __PATMOS_UART_STATUS(s); } while((s & (__PATMOS_UART_DAV | __PATMOS_UART_PAE)) == 0); /* read the data from the UART */ __PATMOS_UART_RD_DATA(c); return c; }
/* * console_outbyte_polled * * This routine transmits a character using polling. */ void console_outbyte_polled( int port, unsigned char ch ) { int s; /* * Wait for the UART to be ready for transmission */ do { __PATMOS_UART_STATUS(s); } while((s & __PATMOS_UART_TRE) == 0); /* * Write data to the UART */ __PATMOS_UART_WR_DATA(ch); }