1 |
2 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: uartsim.h
|
4 |
|
|
//
|
5 |
|
|
// Project: wbuart32, a full featured UART with simulator
|
6 |
|
|
//
|
7 |
|
|
// Purpose: To forward a Verilator simulated UART link over a TCP/IP pipe.
|
8 |
|
|
//
|
9 |
|
|
// This file provides the description of the interface between the UARTSIM
|
10 |
|
|
// and the rest of the world. See below for more detailed descriptions.
|
11 |
|
|
//
|
12 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
13 |
|
|
// Gisselquist Technology, LLC
|
14 |
|
|
//
|
15 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
16 |
|
|
//
|
17 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
18 |
|
|
//
|
19 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
20 |
|
|
// modify it under the terms of the GNU General Public License as published
|
21 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
22 |
|
|
// your option) any later version.
|
23 |
|
|
//
|
24 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
25 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
26 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
27 |
|
|
// for more details.
|
28 |
|
|
//
|
29 |
|
|
// You should have received a copy of the GNU General Public License along
|
30 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
31 |
|
|
// target there if the PDF file isn't present.) If not, see
|
32 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
33 |
|
|
//
|
34 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
35 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
//
|
40 |
|
|
//
|
41 |
|
|
#ifndef UARTSIM_H
|
42 |
|
|
#define UARTSIM_H
|
43 |
|
|
|
44 |
|
|
#include <stdio.h>
|
45 |
|
|
#include <stdlib.h>
|
46 |
|
|
#include <string.h>
|
47 |
|
|
#include <sys/types.h>
|
48 |
|
|
#include <sys/socket.h>
|
49 |
|
|
#include <poll.h>
|
50 |
|
|
#include <unistd.h>
|
51 |
|
|
#include <arpa/inet.h>
|
52 |
|
|
#include <signal.h>
|
53 |
|
|
|
54 |
|
|
#define TXIDLE 0
|
55 |
|
|
#define TXDATA 1
|
56 |
|
|
#define RXIDLE 0
|
57 |
|
|
#define RXDATA 1
|
58 |
|
|
|
59 |
|
|
class UARTSIM {
|
60 |
|
|
// The file descriptors:
|
61 |
|
|
// m_skt is the socket/port we are listening on
|
62 |
|
|
// m_conrd is the file descriptor to read from
|
63 |
|
|
// m_conwr is the file descriptor to write to
|
64 |
|
|
int m_skt, m_conrd, m_conwr;
|
65 |
|
|
//
|
66 |
|
|
// The m_setup register is the 29'bit control register used within
|
67 |
|
|
// the core.
|
68 |
|
|
unsigned m_setup;
|
69 |
|
|
// And the pieces of the setup register broken out.
|
70 |
|
|
int m_nparity, m_fixdp, m_evenp, m_nbits, m_nstop, m_baud_counts;
|
71 |
|
|
|
72 |
|
|
// UART state
|
73 |
|
|
int m_rx_baudcounter, m_rx_state, m_rx_busy,
|
74 |
|
|
m_rx_changectr, m_last_tx;
|
75 |
|
|
int m_tx_baudcounter, m_tx_state, m_tx_busy;
|
76 |
|
|
unsigned m_rx_data, m_tx_data;
|
77 |
|
|
|
78 |
|
|
// setup_listener is an attempt to encapsulate all of the network
|
79 |
|
|
// related setup stuff.
|
80 |
|
|
void setup_listener(const int port);
|
81 |
|
|
|
82 |
|
|
// nettick() gets called if we are connected to a network, and
|
83 |
|
|
int nettick(const int i_tx);
|
84 |
|
|
// fdtick() if we are not.
|
85 |
|
|
int fdtick(const int i_tx);
|
86 |
|
|
|
87 |
|
|
// We'll use the file descriptor for the listener socket to determine
|
88 |
|
|
// whether we are connected to the network or not. If not connected
|
89 |
|
|
// to the network, then we assume m_conrd and m_conwr refer to
|
90 |
|
|
// your more traditional file descriptors, and use them as such.
|
91 |
|
|
int tick(const int i_tx) {
|
92 |
|
|
if (m_skt >= 0)
|
93 |
|
|
return nettick(i_tx);
|
94 |
|
|
else
|
95 |
|
|
return fdtick(i_tx);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
public:
|
99 |
|
|
//
|
100 |
|
|
// The UARTSIM constructor takes one argument: the port on the
|
101 |
|
|
// localhost to listen in on. Once started, connections may be made
|
102 |
|
|
// to this port to get the output from the port.
|
103 |
|
|
UARTSIM(const int port);
|
104 |
|
|
|
105 |
|
|
// kill() closes any active connection and the socket. Once killed,
|
106 |
|
|
// no further output will be sent to the port.
|
107 |
|
|
void kill(void);
|
108 |
|
|
|
109 |
|
|
// setup() busts out the bits from isetup to the various internal
|
110 |
|
|
// parameters. It is ideally only called between bits at appropriate
|
111 |
|
|
// transition intervals.
|
112 |
|
|
void setup(unsigned isetup);
|
113 |
|
|
|
114 |
|
|
// The operator() function is called on every tick. The input is the
|
115 |
|
|
// the output txuart transmit wire from the device. The output is to
|
116 |
|
|
// be connected to the the rxuart receive wire into the device. This
|
117 |
|
|
// makes hookup and operation very simple.
|
118 |
|
|
//
|
119 |
|
|
// This is the most appropriate simulation entry function if the
|
120 |
|
|
// setup register will never change.
|
121 |
|
|
//
|
122 |
|
|
int operator()(int i_tx) {
|
123 |
|
|
return tick(i_tx); }
|
124 |
|
|
|
125 |
|
|
// If there is a possibility that the core might change the UART setup,
|
126 |
|
|
// then it makes sense to include that current setup when calling the
|
127 |
|
|
// tick operator.
|
128 |
|
|
int operator()(int i_tx, unsigned isetup) {
|
129 |
|
|
setup(isetup); return tick(i_tx); }
|
130 |
|
|
};
|
131 |
|
|
|
132 |
|
|
#endif
|