////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Filename: uartsim.cpp
|
// Filename: uartsim.cpp
|
//
|
//
|
// Project: wbuart32, a full featured UART with simulator
|
// Project: wbuart32, a full featured UART with simulator
|
//
|
//
|
// Purpose: To forward a Verilator simulated UART link over a TCP/IP pipe.
|
// Purpose: To forward a Verilator simulated UART link over a TCP/IP pipe.
|
//
|
//
|
// Creator: Dan Gisselquist, Ph.D.
|
// Creator: Dan Gisselquist, Ph.D.
|
// Gisselquist Technology, LLC
|
// Gisselquist Technology, LLC
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
//
|
//
|
// This program is free software (firmware): you can redistribute it and/or
|
// This program is free software (firmware): you can redistribute it and/or
|
// modify it under the terms of the GNU General Public License as published
|
// modify it under the terms of the GNU General Public License as published
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
// your option) any later version.
|
// your option) any later version.
|
//
|
//
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
// target there if the PDF file isn't present.) If not, see
|
// target there if the PDF file isn't present.) If not, see
|
// <http://www.gnu.org/licenses/> for a copy.
|
// <http://www.gnu.org/licenses/> for a copy.
|
//
|
//
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// http://www.gnu.org/licenses/gpl.html
|
// http://www.gnu.org/licenses/gpl.html
|
//
|
//
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
//
|
//
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <sys/socket.h>
|
#include <sys/socket.h>
|
#include <poll.h>
|
#include <poll.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
#include <signal.h>
|
#include <signal.h>
|
#include <ctype.h>
|
#include <ctype.h>
|
|
|
#include "uartsim.h"
|
#include "uartsim.h"
|
|
|
void UARTSIM::setup_listener(const int port) {
|
void UARTSIM::setup_listener(const int port) {
|
struct sockaddr_in my_addr;
|
struct sockaddr_in my_addr;
|
|
|
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
|
|
printf("Listening on port %d\n", port);
|
printf("Listening on port %d\n", port);
|
|
|
m_skt = socket(AF_INET, SOCK_STREAM, 0);
|
m_skt = socket(AF_INET, SOCK_STREAM, 0);
|
if (m_skt < 0) {
|
if (m_skt < 0) {
|
perror("Could not allocate socket: ");
|
perror("Could not allocate socket: ");
|
exit(-1);
|
exit(-1);
|
}
|
}
|
|
|
// Set the reuse address option
|
// Set the reuse address option
|
{
|
{
|
int optv = 1, er;
|
int optv = 1, er;
|
er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
|
er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
|
if (er != 0) {
|
if (er != 0) {
|
perror("SockOpt Err:");
|
perror("SockOpt Err:");
|
exit(-1);
|
exit(-1);
|
}
|
}
|
}
|
}
|
|
|
memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
|
memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
|
my_addr.sin_family = AF_INET;
|
my_addr.sin_family = AF_INET;
|
// Use *all* internet ports to this computer, allowing connections from
|
// Use *all* internet ports to this computer, allowing connections from
|
// any/every one of them.
|
// any/every one of them.
|
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
my_addr.sin_port = htons(port);
|
my_addr.sin_port = htons(port);
|
|
|
if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
|
if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
|
perror("BIND FAILED:");
|
perror("BIND FAILED:");
|
exit(-1);
|
exit(-1);
|
}
|
}
|
|
|
if (listen(m_skt, 1) != 0) {
|
if (listen(m_skt, 1) != 0) {
|
perror("Listen failed:");
|
perror("Listen failed:");
|
exit(-1);
|
exit(-1);
|
}
|
}
|
}
|
}
|
|
|
UARTSIM::UARTSIM(const int port) {
|
UARTSIM::UARTSIM(const int port) {
|
m_conrd = m_conwr = m_skt = -1;
|
m_conrd = m_conwr = m_skt = -1;
|
if (port == 0) {
|
if (port == 0) {
|
m_conrd = STDIN_FILENO;
|
m_conrd = STDIN_FILENO;
|
m_conwr = STDOUT_FILENO;
|
m_conwr = STDOUT_FILENO;
|
} else
|
} else
|
setup_listener(port);
|
setup_listener(port);
|
setup(25); // Set us up for (default) 8N1 w/ a baud rate of CLK/25
|
setup(25); // Set us up for (default) 8N1 w/ a baud rate of CLK/25
|
m_rx_baudcounter = 0;
|
m_rx_baudcounter = 0;
|
m_tx_baudcounter = 0;
|
m_tx_baudcounter = 0;
|
m_rx_state = RXIDLE;
|
m_rx_state = RXIDLE;
|
m_tx_state = TXIDLE;
|
m_tx_state = TXIDLE;
|
}
|
}
|
|
|
void UARTSIM::kill(void) {
|
void UARTSIM::kill(void) {
|
|
fflush(stdout);
|
|
|
|
// Quickly double check that we aren't about to close stdin/stdout
|
|
if (m_conrd == STDIN_FILENO)
|
|
m_conwr = -1;
|
|
if (m_conwr == STDOUT_FILENO)
|
|
m_conwr = -1;
|
// Close any active connection
|
// Close any active connection
|
if (m_conrd >= 0) close(m_conrd);
|
if (m_conrd >= 0) close(m_conrd);
|
if ((m_conwr >= 0)&&(m_conwr != m_conrd)) close(m_conwr);
|
if ((m_conwr >= 0)&&(m_conwr != m_conrd)) close(m_conwr);
|
if (m_skt >= 0) close(m_skt);
|
if (m_skt >= 0) close(m_skt);
|
|
|
m_conrd = m_conwr = m_skt = -1;
|
m_conrd = m_conwr = m_skt = -1;
|
}
|
}
|
|
|
void UARTSIM::setup(unsigned isetup) {
|
void UARTSIM::setup(unsigned isetup) {
|
if (isetup != m_setup) {
|
if (isetup != m_setup) {
|
m_setup = isetup;
|
m_setup = isetup;
|
m_baud_counts = (isetup & 0x0ffffff);
|
m_baud_counts = (isetup & 0x0ffffff);
|
m_nbits = 8-((isetup >> 28)&0x03);
|
m_nbits = 8-((isetup >> 28)&0x03);
|
m_nstop =((isetup >> 27)&1)+1;
|
m_nstop =((isetup >> 27)&1)+1;
|
m_nparity = (isetup >> 26)&1;
|
m_nparity = (isetup >> 26)&1;
|
m_fixdp = (isetup >> 25)&1;
|
m_fixdp = (isetup >> 25)&1;
|
m_evenp = (isetup >> 24)&1;
|
m_evenp = (isetup >> 24)&1;
|
}
|
}
|
}
|
}
|
|
|
int UARTSIM::nettick(int i_tx) {
|
int UARTSIM::nettick(int i_tx) {
|
int o_rx = 1;
|
int o_rx = 1;
|
|
|
if ((m_conrd < 0)&&(m_conwr<0)&&(m_skt>=0)) {
|
if ((m_conrd < 0)&&(m_conwr<0)&&(m_skt>=0)) {
|
// Can we accept a connection?
|
// Can we accept a connection?
|
struct pollfd pb;
|
struct pollfd pb;
|
|
|
pb.fd = m_skt;
|
pb.fd = m_skt;
|
pb.events = POLLIN;
|
pb.events = POLLIN;
|
poll(&pb, 1, 0);
|
poll(&pb, 1, 0);
|
|
|
if (pb.revents & POLLIN) {
|
if (pb.revents & POLLIN) {
|
m_conrd = accept(m_skt, 0, 0);
|
m_conrd = accept(m_skt, 0, 0);
|
m_conwr = m_conrd;
|
m_conwr = m_conrd;
|
|
|
if (m_conrd < 0)
|
if (m_conrd < 0)
|
perror("Accept failed:");
|
perror("Accept failed:");
|
}
|
}
|
}
|
}
|
|
|
if ((!i_tx)&&(m_last_tx))
|
if ((!i_tx)&&(m_last_tx))
|
m_rx_changectr = 0;
|
m_rx_changectr = 0;
|
else m_rx_changectr++;
|
else m_rx_changectr++;
|
m_last_tx = i_tx;
|
m_last_tx = i_tx;
|
|
|
if (m_rx_state == RXIDLE) {
|
if (m_rx_state == RXIDLE) {
|
if (!i_tx) {
|
if (!i_tx) {
|
m_rx_state = RXDATA;
|
m_rx_state = RXDATA;
|
m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
|
m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
|
m_rx_baudcounter -= m_rx_changectr;
|
m_rx_baudcounter -= m_rx_changectr;
|
m_rx_busy = 0;
|
m_rx_busy = 0;
|
m_rx_data = 0;
|
m_rx_data = 0;
|
}
|
}
|
} else if (m_rx_baudcounter <= 0) {
|
} else if (m_rx_baudcounter <= 0) {
|
if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
|
if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
|
m_rx_state = RXIDLE;
|
m_rx_state = RXIDLE;
|
if (m_conwr >= 0) {
|
if (m_conwr >= 0) {
|
char buf[1];
|
char buf[1];
|
buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
|
buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
|
if (1 != send(m_conwr, buf, 1, 0)) {
|
if (1 != send(m_conwr, buf, 1, 0)) {
|
close(m_conwr);
|
close(m_conwr);
|
m_conrd = m_conwr = -1;
|
m_conrd = m_conwr = -1;
|
}
|
}
|
}
|
}
|
} else {
|
} else {
|
m_rx_busy = (m_rx_busy << 1)|1;
|
m_rx_busy = (m_rx_busy << 1)|1;
|
// Low order bit is transmitted first, in this
|
// Low order bit is transmitted first, in this
|
// order:
|
// order:
|
// Start bit (1'b1)
|
// Start bit (1'b1)
|
// bit 0
|
// bit 0
|
// bit 1
|
// bit 1
|
// bit 2
|
// bit 2
|
// ...
|
// ...
|
// bit N-1
|
// bit N-1
|
// (possible parity bit)
|
// (possible parity bit)
|
// stop bit
|
// stop bit
|
// (possible secondary stop bit)
|
// (possible secondary stop bit)
|
m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
|
m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
|
} m_rx_baudcounter = m_baud_counts-1;
|
} m_rx_baudcounter = m_baud_counts-1;
|
} else
|
} else
|
m_rx_baudcounter--;
|
m_rx_baudcounter--;
|
|
|
if (m_tx_state == TXIDLE) {
|
if (m_tx_state == TXIDLE) {
|
struct pollfd pb;
|
struct pollfd pb;
|
pb.fd = m_conrd;
|
pb.fd = m_conrd;
|
pb.events = POLLIN;
|
pb.events = POLLIN;
|
if (poll(&pb, 1, 0) < 0)
|
if (poll(&pb, 1, 0) < 0)
|
perror("Polling error:");
|
perror("Polling error:");
|
if (pb.revents & POLLIN) {
|
if (pb.revents & POLLIN) {
|
char buf[1];
|
char buf[1];
|
if (1 == recv(m_conrd, buf, 1, MSG_DONTWAIT)) {
|
if (1 == recv(m_conrd, buf, 1, MSG_DONTWAIT)) {
|
m_tx_data = (-1<<(m_nbits+m_nparity+1))
|
m_tx_data = (-1<<(m_nbits+m_nparity+1))
|
// << nstart_bits
|
// << nstart_bits
|
|((buf[0]<<1)&0x01fe);
|
|((buf[0]<<1)&0x01fe);
|
if (m_nparity) {
|
if (m_nparity) {
|
int p;
|
int p;
|
|
|
// If m_nparity is set, we need to then
|
// If m_nparity is set, we need to then
|
// create the parity bit.
|
// create the parity bit.
|
if (m_fixdp)
|
if (m_fixdp)
|
p = m_evenp;
|
p = m_evenp;
|
else {
|
else {
|
p = (m_tx_data >> 1)&0x0ff;
|
p = (m_tx_data >> 1)&0x0ff;
|
p = p ^ (p>>4);
|
p = p ^ (p>>4);
|
p = p ^ (p>>2);
|
p = p ^ (p>>2);
|
p = p ^ (p>>1);
|
p = p ^ (p>>1);
|
p &= 1;
|
p &= 1;
|
p ^= m_evenp;
|
p ^= m_evenp;
|
}
|
}
|
m_tx_data |= (p<<(m_nbits+m_nparity));
|
m_tx_data |= (p<<(m_nbits+m_nparity));
|
}
|
}
|
m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
|
m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
|
m_tx_state = TXDATA;
|
m_tx_state = TXDATA;
|
o_rx = 0;
|
o_rx = 0;
|
m_tx_baudcounter = m_baud_counts-1;
|
m_tx_baudcounter = m_baud_counts-1;
|
}
|
}
|
}
|
}
|
} else if (m_tx_baudcounter <= 0) {
|
} else if (m_tx_baudcounter <= 0) {
|
m_tx_data >>= 1;
|
m_tx_data >>= 1;
|
m_tx_busy >>= 1;
|
m_tx_busy >>= 1;
|
if (!m_tx_busy)
|
if (!m_tx_busy)
|
m_tx_state = TXIDLE;
|
m_tx_state = TXIDLE;
|
else
|
else
|
m_tx_baudcounter = m_baud_counts-1;
|
m_tx_baudcounter = m_baud_counts-1;
|
o_rx = m_tx_data&1;
|
o_rx = m_tx_data&1;
|
} else {
|
} else {
|
m_tx_baudcounter--;
|
m_tx_baudcounter--;
|
o_rx = m_tx_data&1;
|
o_rx = m_tx_data&1;
|
}
|
}
|
|
|
return o_rx;
|
return o_rx;
|
}
|
}
|
|
|
int UARTSIM::fdtick(int i_tx) {
|
int UARTSIM::fdtick(int i_tx) {
|
int o_rx = 1;
|
int o_rx = 1;
|
|
|
if ((!i_tx)&&(m_last_tx))
|
if ((!i_tx)&&(m_last_tx))
|
m_rx_changectr = 0;
|
m_rx_changectr = 0;
|
else m_rx_changectr++;
|
else m_rx_changectr++;
|
m_last_tx = i_tx;
|
m_last_tx = i_tx;
|
|
|
if (m_rx_state == RXIDLE) {
|
if (m_rx_state == RXIDLE) {
|
if (!i_tx) {
|
if (!i_tx) {
|
m_rx_state = RXDATA;
|
m_rx_state = RXDATA;
|
m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
|
m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
|
m_rx_baudcounter -= m_rx_changectr;
|
m_rx_baudcounter -= m_rx_changectr;
|
m_rx_busy = 0;
|
m_rx_busy = 0;
|
m_rx_data = 0;
|
m_rx_data = 0;
|
}
|
}
|
} else if (m_rx_baudcounter <= 0) {
|
} else if (m_rx_baudcounter <= 0) {
|
if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
|
if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
|
m_rx_state = RXIDLE;
|
m_rx_state = RXIDLE;
|
if (m_conwr >= 0) {
|
if (m_conwr >= 0) {
|
char buf[1];
|
char buf[1];
|
buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
|
buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
|
if (1 != write(m_conwr, buf, 1)) {
|
if (1 != write(m_conwr, buf, 1)) {
|
fprintf(stderr, "ERR while attempting to write out--closing output port\n");
|
fprintf(stderr, "ERR while attempting to write out--closing output port\n");
|
perror("UARTSIM::write() ");
|
perror("UARTSIM::write() ");
|
m_conrd = m_conwr = -1;
|
m_conrd = m_conwr = -1;
|
}
|
}
|
}
|
}
|
} else {
|
} else {
|
m_rx_busy = (m_rx_busy << 1)|1;
|
m_rx_busy = (m_rx_busy << 1)|1;
|
// Low order bit is transmitted first, in this
|
// Low order bit is transmitted first, in this
|
// order:
|
// order:
|
// Start bit (1'b1)
|
// Start bit (1'b1)
|
// bit 0
|
// bit 0
|
// bit 1
|
// bit 1
|
// bit 2
|
// bit 2
|
// ...
|
// ...
|
// bit N-1
|
// bit N-1
|
// (possible parity bit)
|
// (possible parity bit)
|
// stop bit
|
// stop bit
|
// (possible secondary stop bit)
|
// (possible secondary stop bit)
|
m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
|
m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
|
} m_rx_baudcounter = m_baud_counts-1;
|
} m_rx_baudcounter = m_baud_counts-1;
|
} else
|
} else
|
m_rx_baudcounter--;
|
m_rx_baudcounter--;
|
|
|
if ((m_tx_state == TXIDLE)&&(m_conrd >= 0)) {
|
if ((m_tx_state == TXIDLE)&&(m_conrd >= 0)) {
|
struct pollfd pb;
|
struct pollfd pb;
|
pb.fd = m_conrd;
|
pb.fd = m_conrd;
|
pb.events = POLLIN;
|
pb.events = POLLIN;
|
if (poll(&pb, 1, 0) < 0)
|
if (poll(&pb, 1, 0) < 0)
|
perror("Polling error:");
|
perror("Polling error:");
|
if (pb.revents & POLLIN) {
|
if (pb.revents & POLLIN) {
|
char buf[1];
|
char buf[1];
|
int nr;
|
int nr;
|
if (1==(nr = read(m_conrd, buf, 1))) {
|
if (1==(nr = read(m_conrd, buf, 1))) {
|
m_tx_data = (-1<<(m_nbits+m_nparity+1))
|
m_tx_data = (-1<<(m_nbits+m_nparity+1))
|
// << nstart_bits
|
// << nstart_bits
|
|((buf[0]<<1)&0x01fe);
|
|((buf[0]<<1)&0x01fe);
|
if (m_nparity) {
|
if (m_nparity) {
|
int p;
|
int p;
|
|
|
// If m_nparity is set, we need to then
|
// If m_nparity is set, we need to then
|
// create the parity bit.
|
// create the parity bit.
|
if (m_fixdp)
|
if (m_fixdp)
|
p = m_evenp;
|
p = m_evenp;
|
else {
|
else {
|
p = (m_tx_data >> 1)&0x0ff;
|
p = (m_tx_data >> 1)&0x0ff;
|
p = p ^ (p>>4);
|
p = p ^ (p>>4);
|
p = p ^ (p>>2);
|
p = p ^ (p>>2);
|
p = p ^ (p>>1);
|
p = p ^ (p>>1);
|
p &= 1;
|
p &= 1;
|
p ^= m_evenp;
|
p ^= m_evenp;
|
}
|
}
|
m_tx_data |= (p<<(m_nbits+m_nparity));
|
m_tx_data |= (p<<(m_nbits+m_nparity));
|
}
|
}
|
m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
|
m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
|
m_tx_state = TXDATA;
|
m_tx_state = TXDATA;
|
o_rx = 0;
|
o_rx = 0;
|
m_tx_baudcounter = m_baud_counts-1;
|
m_tx_baudcounter = m_baud_counts-1;
|
} else if (nr < 0) {
|
} else if (nr < 0) {
|
fprintf(stderr, "ERR while attempting to read in--closing input port\n");
|
fprintf(stderr, "ERR while attempting to read in--closing input port\n");
|
perror("UARTSIM::read() ");
|
perror("UARTSIM::read() ");
|
m_conrd = -1;
|
m_conrd = -1;
|
} // and we really don't care if nr == 0 except that
|
} // and we really don't care if nr == 0 except that
|
// the poll above is supposed to keep it from happening
|
// the poll above is supposed to keep it from happening
|
}
|
}
|
} else if (m_tx_baudcounter == 0) {
|
} else if (m_tx_baudcounter == 0) {
|
m_tx_data >>= 1;
|
m_tx_data >>= 1;
|
m_tx_busy >>= 1;
|
m_tx_busy >>= 1;
|
if (!m_tx_busy)
|
if (!m_tx_busy)
|
m_tx_state = TXIDLE;
|
m_tx_state = TXIDLE;
|
else
|
else
|
m_tx_baudcounter = m_baud_counts-1;
|
m_tx_baudcounter = m_baud_counts-1;
|
o_rx = m_tx_data&1;
|
o_rx = m_tx_data&1;
|
} else {
|
} else {
|
m_tx_baudcounter--;
|
m_tx_baudcounter--;
|
o_rx = m_tx_data&1;
|
o_rx = m_tx_data&1;
|
}
|
}
|
|
|
return o_rx;
|
return o_rx;
|
}
|
}
|
|
|
|
|