OpenCores
URL https://opencores.org/ocsvn/wbuart32/wbuart32/trunk

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [cpp/] [uartsim.cpp] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    uartsim.cpp
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
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14 26 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
15 2 dgisselq
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27 11 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
28 2 dgisselq
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <sys/types.h>
42
#include <sys/socket.h>
43
#include <poll.h>
44
#include <unistd.h>
45
#include <arpa/inet.h>
46
#include <signal.h>
47
#include <ctype.h>
48
 
49
#include "uartsim.h"
50
 
51
void    UARTSIM::setup_listener(const int port) {
52
        struct  sockaddr_in     my_addr;
53
 
54
        signal(SIGPIPE, SIG_IGN);
55
 
56
        printf("Listening on port %d\n", port);
57
 
58
        m_skt = socket(AF_INET, SOCK_STREAM, 0);
59
        if (m_skt < 0) {
60 18 dgisselq
                perror("ERR: Could not allocate socket: ");
61
                exit(EXIT_FAILURE);
62 2 dgisselq
        }
63
 
64
        // Set the reuse address option
65
        {
66
                int optv = 1, er;
67
                er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
68
                if (er != 0) {
69 18 dgisselq
                        perror("ERR: SockOpt Err:");
70
                        exit(EXIT_FAILURE);
71 2 dgisselq
                }
72
        }
73
 
74
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
75
        my_addr.sin_family = AF_INET;
76
        // Use *all* internet ports to this computer, allowing connections from
77
        // any/every one of them.
78
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
79
        my_addr.sin_port = htons(port);
80 23 dgisselq
 
81 2 dgisselq
        if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
82 18 dgisselq
                perror("ERR: BIND FAILED:");
83
                exit(EXIT_FAILURE);
84 2 dgisselq
        }
85
 
86
        if (listen(m_skt, 1) != 0) {
87 18 dgisselq
                perror("ERR: Listen failed:");
88
                exit(EXIT_FAILURE);
89 2 dgisselq
        }
90
}
91
 
92
UARTSIM::UARTSIM(const int port) {
93
        m_conrd = m_conwr = m_skt = -1;
94
        if (port == 0) {
95
                m_conrd = STDIN_FILENO;
96
                m_conwr = STDOUT_FILENO;
97
        } else
98
                setup_listener(port);
99
        setup(25);      // Set us up for (default) 8N1 w/ a baud rate of CLK/25
100
        m_rx_baudcounter = 0;
101
        m_tx_baudcounter = 0;
102
        m_rx_state = RXIDLE;
103
        m_tx_state = TXIDLE;
104
}
105
 
106
void    UARTSIM::kill(void) {
107 11 dgisselq
        fflush(stdout);
108
 
109
        // Quickly double check that we aren't about to close stdin/stdout
110
        if (m_conrd == STDIN_FILENO)
111
                m_conwr = -1;
112
        if (m_conwr == STDOUT_FILENO)
113
                m_conwr = -1;
114 2 dgisselq
        // Close any active connection
115
        if (m_conrd >= 0)                                close(m_conrd);
116
        if ((m_conwr >= 0)&&(m_conwr != m_conrd))        close(m_conwr);
117
        if (m_skt >= 0) close(m_skt);
118
 
119
        m_conrd = m_conwr = m_skt = -1;
120
}
121
 
122
void    UARTSIM::setup(unsigned isetup) {
123
        if (isetup != m_setup) {
124
                m_setup = isetup;
125
                m_baud_counts = (isetup & 0x0ffffff);
126
                m_nbits   = 8-((isetup >> 28)&0x03);
127
                m_nstop   =((isetup >> 27)&1)+1;
128
                m_nparity = (isetup >> 26)&1;
129
                m_fixdp   = (isetup >> 25)&1;
130
                m_evenp   = (isetup >> 24)&1;
131
        }
132
}
133
 
134 18 dgisselq
void    UARTSIM::check_for_new_connections(void) {
135 2 dgisselq
        if ((m_conrd < 0)&&(m_conwr<0)&&(m_skt>=0)) {
136
                // Can we accept a connection?
137
                struct  pollfd  pb;
138
 
139
                pb.fd = m_skt;
140
                pb.events = POLLIN;
141
                poll(&pb, 1, 0);
142
 
143
                if (pb.revents & POLLIN) {
144
                        m_conrd = accept(m_skt, 0, 0);
145
                        m_conwr = m_conrd;
146
 
147
                        if (m_conrd < 0)
148
                                perror("Accept failed:");
149 18 dgisselq
                        // else printf("New connection accepted!\n");
150 2 dgisselq
                }
151
        }
152
 
153 18 dgisselq
}
154
 
155 23 dgisselq
int     UARTSIM::rawtick(const int i_tx, const bool network) {
156 18 dgisselq
        int     o_rx = 1, nr = 0;
157
 
158 23 dgisselq
        if (network)
159
                check_for_new_connections();
160 18 dgisselq
 
161 2 dgisselq
        if ((!i_tx)&&(m_last_tx))
162
                m_rx_changectr = 0;
163
        else    m_rx_changectr++;
164
        m_last_tx = i_tx;
165
 
166
        if (m_rx_state == RXIDLE) {
167
                if (!i_tx) {
168
                        m_rx_state = RXDATA;
169 3 dgisselq
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
170 2 dgisselq
                        m_rx_baudcounter -= m_rx_changectr;
171
                        m_rx_busy    = 0;
172
                        m_rx_data    = 0;
173
                }
174
        } else if (m_rx_baudcounter <= 0) {
175
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
176
                        m_rx_state = RXIDLE;
177
                        if (m_conwr >= 0) {
178
                                char    buf[1];
179
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
180 23 dgisselq
                                if ((network)&&(1 != send(m_conwr, buf, 1, 0))) {
181 2 dgisselq
                                        close(m_conwr);
182
                                        m_conrd = m_conwr = -1;
183 18 dgisselq
                                        fprintf(stderr, "Failed write, connection closed\n");
184 23 dgisselq
                                } else if ((!network)&&(1 != write(m_conwr, buf, 1))) {
185
                                        fprintf(stderr, "ERR while attempting to write out--closing output port\n");
186
                                        perror("UARTSIM::write() ");
187
                                        m_conrd = m_conwr = -1;
188 2 dgisselq
                                }
189
                        }
190
                } else {
191
                        m_rx_busy = (m_rx_busy << 1)|1;
192
                        // Low order bit is transmitted first, in this
193
                        // order:
194
                        //      Start bit (1'b1)
195
                        //      bit 0
196
                        //      bit 1
197
                        //      bit 2
198
                        //      ...
199
                        //      bit N-1
200
                        //      (possible parity bit)
201
                        //      stop bit
202
                        //      (possible secondary stop bit)
203
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
204 3 dgisselq
                } m_rx_baudcounter = m_baud_counts-1;
205 2 dgisselq
        } else
206
                m_rx_baudcounter--;
207
 
208 23 dgisselq
        if ((m_tx_state == TXIDLE)&&((network)||(m_conrd >= 0))) {
209 2 dgisselq
                struct  pollfd  pb;
210
                pb.fd = m_conrd;
211
                pb.events = POLLIN;
212
                if (poll(&pb, 1, 0) < 0)
213
                        perror("Polling error:");
214 23 dgisselq
 
215 2 dgisselq
                if (pb.revents & POLLIN) {
216
                        char    buf[1];
217 23 dgisselq
 
218
                        if (network)
219
                                nr = recv(m_conrd, buf, 1, MSG_DONTWAIT);
220
                        else
221
                                nr = read(m_conrd, buf, 1);
222
                        if (1 == nr) {
223 2 dgisselq
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
224
                                        // << nstart_bits
225
                                        |((buf[0]<<1)&0x01fe);
226
                                if (m_nparity) {
227
                                        int     p;
228
 
229
                                        // If m_nparity is set, we need to then
230
                                        // create the parity bit.
231
                                        if (m_fixdp)
232
                                                p = m_evenp;
233
                                        else {
234
                                                p = (m_tx_data >> 1)&0x0ff;
235
                                                p = p ^ (p>>4);
236
                                                p = p ^ (p>>2);
237
                                                p = p ^ (p>>1);
238
                                                p &= 1;
239
                                                p ^= m_evenp;
240
                                        }
241
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
242
                                }
243
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
244
                                m_tx_state = TXDATA;
245
                                o_rx = 0;
246 3 dgisselq
                                m_tx_baudcounter = m_baud_counts-1;
247 23 dgisselq
                        } else if ((network)&&(nr == 0)) {
248 18 dgisselq
                                close(m_conrd);
249
                                m_conrd = m_conwr = -1;
250
                                // printf("Closing network connection\n");
251
                        } else if (nr < 0) {
252 23 dgisselq
                                if (!network) {
253
                                        fprintf(stderr, "ERR while attempting to read in--closing input port\n");
254
                                        perror("UARTSIM::read() ");
255
                                        m_conrd = -1;
256
                                } else {
257
                                        perror("O/S Read err:");
258
                                        close(m_conrd);
259
                                        m_conrd = m_conwr = -1;
260
                                }
261 2 dgisselq
                        }
262
                }
263 3 dgisselq
        } else if (m_tx_baudcounter <= 0) {
264 2 dgisselq
                m_tx_data >>= 1;
265
                m_tx_busy >>= 1;
266
                if (!m_tx_busy)
267
                        m_tx_state = TXIDLE;
268
                else
269 3 dgisselq
                        m_tx_baudcounter = m_baud_counts-1;
270 2 dgisselq
                o_rx = m_tx_data&1;
271
        } else {
272
                m_tx_baudcounter--;
273
                o_rx = m_tx_data&1;
274
        }
275
 
276
        return o_rx;
277
}
278
 
279 23 dgisselq
int     UARTSIM::nettick(const int i_tx) {
280
        return rawtick(i_tx, true);
281
}
282 2 dgisselq
 
283 23 dgisselq
int     UARTSIM::fdtick(const int i_tx) {
284
        return rawtick(i_tx, false);
285 2 dgisselq
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.