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

Subversion Repositories wbuart32

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

Go to most recent revision | 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 11 dgisselq
// Copyright (C) 2015-2017, 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
 
81
        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
int     UARTSIM::nettick(int i_tx) {
156
        int     o_rx = 1, nr = 0;
157
 
158
        check_for_new_connections();
159
 
160 2 dgisselq
        if ((!i_tx)&&(m_last_tx))
161
                m_rx_changectr = 0;
162
        else    m_rx_changectr++;
163
        m_last_tx = i_tx;
164
 
165
        if (m_rx_state == RXIDLE) {
166
                if (!i_tx) {
167
                        m_rx_state = RXDATA;
168 3 dgisselq
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
169 2 dgisselq
                        m_rx_baudcounter -= m_rx_changectr;
170
                        m_rx_busy    = 0;
171
                        m_rx_data    = 0;
172
                }
173
        } else if (m_rx_baudcounter <= 0) {
174
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
175
                        m_rx_state = RXIDLE;
176
                        if (m_conwr >= 0) {
177
                                char    buf[1];
178
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
179
                                if (1 != send(m_conwr, buf, 1, 0)) {
180
                                        close(m_conwr);
181
                                        m_conrd = m_conwr = -1;
182 18 dgisselq
                                        fprintf(stderr, "Failed write, connection closed\n");
183 2 dgisselq
                                }
184
                        }
185
                } else {
186
                        m_rx_busy = (m_rx_busy << 1)|1;
187
                        // Low order bit is transmitted first, in this
188
                        // order:
189
                        //      Start bit (1'b1)
190
                        //      bit 0
191
                        //      bit 1
192
                        //      bit 2
193
                        //      ...
194
                        //      bit N-1
195
                        //      (possible parity bit)
196
                        //      stop bit
197
                        //      (possible secondary stop bit)
198
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
199 3 dgisselq
                } m_rx_baudcounter = m_baud_counts-1;
200 2 dgisselq
        } else
201
                m_rx_baudcounter--;
202
 
203
        if (m_tx_state == TXIDLE) {
204
                struct  pollfd  pb;
205
                pb.fd = m_conrd;
206
                pb.events = POLLIN;
207
                if (poll(&pb, 1, 0) < 0)
208
                        perror("Polling error:");
209
                if (pb.revents & POLLIN) {
210
                        char    buf[1];
211 18 dgisselq
                        if (1 == (nr = recv(m_conrd, buf, 1, MSG_DONTWAIT))) {
212 2 dgisselq
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
213
                                        // << nstart_bits
214
                                        |((buf[0]<<1)&0x01fe);
215
                                if (m_nparity) {
216
                                        int     p;
217
 
218
                                        // If m_nparity is set, we need to then
219
                                        // create the parity bit.
220
                                        if (m_fixdp)
221
                                                p = m_evenp;
222
                                        else {
223
                                                p = (m_tx_data >> 1)&0x0ff;
224
                                                p = p ^ (p>>4);
225
                                                p = p ^ (p>>2);
226
                                                p = p ^ (p>>1);
227
                                                p &= 1;
228
                                                p ^= m_evenp;
229
                                        }
230
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
231
                                }
232
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
233
                                m_tx_state = TXDATA;
234
                                o_rx = 0;
235 3 dgisselq
                                m_tx_baudcounter = m_baud_counts-1;
236 18 dgisselq
                        } else if (nr == 0) {
237
                                close(m_conrd);
238
                                m_conrd = m_conwr = -1;
239
                                // printf("Closing network connection\n");
240
                        } else if (nr < 0) {
241
                                perror("O/S Read err:");
242
                                close(m_conrd);
243
                                m_conrd = m_conwr = -1;
244 2 dgisselq
                        }
245
                }
246 3 dgisselq
        } else if (m_tx_baudcounter <= 0) {
247 2 dgisselq
                m_tx_data >>= 1;
248
                m_tx_busy >>= 1;
249
                if (!m_tx_busy)
250
                        m_tx_state = TXIDLE;
251
                else
252 3 dgisselq
                        m_tx_baudcounter = m_baud_counts-1;
253 2 dgisselq
                o_rx = m_tx_data&1;
254
        } else {
255
                m_tx_baudcounter--;
256
                o_rx = m_tx_data&1;
257
        }
258
 
259
        return o_rx;
260
}
261
 
262
int     UARTSIM::fdtick(int i_tx) {
263
        int     o_rx = 1;
264
 
265
        if ((!i_tx)&&(m_last_tx))
266
                m_rx_changectr = 0;
267
        else    m_rx_changectr++;
268
        m_last_tx = i_tx;
269
 
270
        if (m_rx_state == RXIDLE) {
271
                if (!i_tx) {
272
                        m_rx_state = RXDATA;
273 3 dgisselq
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
274 2 dgisselq
                        m_rx_baudcounter -= m_rx_changectr;
275
                        m_rx_busy    = 0;
276
                        m_rx_data    = 0;
277
                }
278
        } else if (m_rx_baudcounter <= 0) {
279
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
280
                        m_rx_state = RXIDLE;
281
                        if (m_conwr >= 0) {
282
                                char    buf[1];
283
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
284
                                if (1 != write(m_conwr, buf, 1)) {
285
                                        fprintf(stderr, "ERR while attempting to write out--closing output port\n");
286
                                        perror("UARTSIM::write() ");
287
                                        m_conrd = m_conwr = -1;
288
                                }
289
                        }
290
                } else {
291
                        m_rx_busy = (m_rx_busy << 1)|1;
292
                        // Low order bit is transmitted first, in this
293
                        // order:
294
                        //      Start bit (1'b1)
295
                        //      bit 0
296
                        //      bit 1
297
                        //      bit 2
298
                        //      ...
299
                        //      bit N-1
300
                        //      (possible parity bit)
301
                        //      stop bit
302
                        //      (possible secondary stop bit)
303
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
304 3 dgisselq
                } m_rx_baudcounter = m_baud_counts-1;
305 2 dgisselq
        } else
306
                m_rx_baudcounter--;
307
 
308
        if ((m_tx_state == TXIDLE)&&(m_conrd >= 0)) {
309
                struct  pollfd  pb;
310
                pb.fd = m_conrd;
311
                pb.events = POLLIN;
312
                if (poll(&pb, 1, 0) < 0)
313
                        perror("Polling error:");
314
                if (pb.revents & POLLIN) {
315
                        char    buf[1];
316
                        int     nr;
317
                        if (1==(nr = read(m_conrd, buf, 1))) {
318
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
319
                                        // << nstart_bits
320
                                        |((buf[0]<<1)&0x01fe);
321
                                if (m_nparity) {
322
                                        int     p;
323
 
324
                                        // If m_nparity is set, we need to then
325
                                        // create the parity bit.
326
                                        if (m_fixdp)
327
                                                p = m_evenp;
328
                                        else {
329
                                                p = (m_tx_data >> 1)&0x0ff;
330
                                                p = p ^ (p>>4);
331
                                                p = p ^ (p>>2);
332
                                                p = p ^ (p>>1);
333
                                                p &= 1;
334
                                                p ^= m_evenp;
335
                                        }
336
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
337
                                }
338
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
339
                                m_tx_state = TXDATA;
340
                                o_rx = 0;
341 3 dgisselq
                                m_tx_baudcounter = m_baud_counts-1;
342 2 dgisselq
                        } else if (nr < 0) {
343
                                fprintf(stderr, "ERR while attempting to read in--closing input port\n");
344
                                perror("UARTSIM::read() ");
345
                                m_conrd = -1;
346
                        } // and we really don't care if nr == 0 except that
347
                        // the poll above is supposed to keep it from happening
348
                }
349
        } else if (m_tx_baudcounter == 0) {
350
                m_tx_data >>= 1;
351
                m_tx_busy >>= 1;
352
                if (!m_tx_busy)
353
                        m_tx_state = TXIDLE;
354
                else
355 3 dgisselq
                        m_tx_baudcounter = m_baud_counts-1;
356 2 dgisselq
                o_rx = m_tx_data&1;
357
        } else {
358
                m_tx_baudcounter--;
359
                o_rx = m_tx_data&1;
360
        }
361
 
362
        return o_rx;
363
}
364
 

powered by: WebSVN 2.1.0

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