From 28d9ddf0e2aff8fe6937949f54285cae9ee478a7 Mon Sep 17 00:00:00 2001 From: 1138-4EB <1138-4EB@users.noreply.github.com> Date: Thu, 2 Mar 2017 00:24:57 +0100 Subject: =?UTF-8?q?Add=20raw=20sources=20of=20tutorial=20'How=20to=20simul?= =?UTF-8?q?ate=20an=20UART=20VHDL=20code=20with=20ghdl}'=20by=20'Ren=C3=A9?= =?UTF-8?q?=20Do=C3=9F'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/using/UART_srcs/vhpi/tty.c | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 doc/using/UART_srcs/vhpi/tty.c (limited to 'doc/using/UART_srcs/vhpi/tty.c') diff --git a/doc/using/UART_srcs/vhpi/tty.c b/doc/using/UART_srcs/vhpi/tty.c new file mode 100644 index 000000000..c04f8f359 --- /dev/null +++ b/doc/using/UART_srcs/vhpi/tty.c @@ -0,0 +1,83 @@ +/* + VPI code allowing you to connect terminal emulator or other program to pty "connected" + to the UART-like port in IP core simulated in GHDL. + + This code is written by Wojciech M. Zabolotny (wz...@ise.pw.edu.pl) on 2nd June 2011 + and is published as PUBLIC DOMAIN + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *ptsname(int fd); + +int ptyf = -1; + + + +int tty_open(int a) +{ + ptyf = open("/dev/ptmx",O_RDWR); + if(ptyf<0) { + perror("I can't open pseudoterminal\n"); + return -1; + } + if(unlockpt(ptyf)<0) { + perror("I can't unlock pseudoterminal\n"); + return -1; + } + if(grantpt(ptyf)<0) { + perror("I can't grant pseudoterminal\n"); + return -1; + } + printf("Pseudoterminal: %s\n",ptsname(ptyf)); + // sleep(10); + return 0; +} + + +int read_enable(void) +{ + //In the masks below you may omit POLLHUP in this case + //disconnection of the terminal emulator from pty will not + //stop simulation, and you'll be able to reconnect + //the same or different program to pty and running simulation + struct pollfd pfd[1]={{ptyf,POLLIN | POLLERR | POLLHUP,0}}; + int res; + res=poll(pfd,1,0); + if(res==0) return 0; + if(res<0) return 0; //error + //If you removed POLLHUP from the mask above, you should remove it below too + if(pfd[0].revents & (POLLERR|POLLHUP)) return 0; //disconnected or error? + if(pfd[0].revents & POLLIN) return 1; + return 0; +} + + +int read_data(void) +{ + unsigned char c; + read(ptyf,&c,1); + return c; +} + +int write_data(int byte) +{ + unsigned char c = byte; + write(ptyf,&c,1); + + // Debug: printf("Writing %x to pty\n", c); + return 0; +} + -- cgit v1.2.3