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

Subversion Repositories wbuart32

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

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
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
15
//
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
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// 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
                perror("Could not allocate socket: ");
61
                exit(-1);
62
        }
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
                        perror("SockOpt Err:");
70
                        exit(-1);
71
                }
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
                perror("BIND FAILED:");
83
                exit(-1);
84
        }
85
 
86
        if (listen(m_skt, 1) != 0) {
87
                perror("Listen failed:");
88
                exit(-1);
89
        }
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
        // Close any active connection
108
        if (m_conrd >= 0)                                close(m_conrd);
109
        if ((m_conwr >= 0)&&(m_conwr != m_conrd))        close(m_conwr);
110
        if (m_skt >= 0) close(m_skt);
111
 
112
        m_conrd = m_conwr = m_skt = -1;
113
}
114
 
115
void    UARTSIM::setup(unsigned isetup) {
116
        if (isetup != m_setup) {
117
                m_setup = isetup;
118
                m_baud_counts = (isetup & 0x0ffffff);
119
                m_nbits   = 8-((isetup >> 28)&0x03);
120
                m_nstop   =((isetup >> 27)&1)+1;
121
                m_nparity = (isetup >> 26)&1;
122
                m_fixdp   = (isetup >> 25)&1;
123
                m_evenp   = (isetup >> 24)&1;
124
        }
125
}
126
 
127
int     UARTSIM::nettick(int i_tx) {
128
        int     o_rx = 1;
129
 
130
        if ((m_conrd < 0)&&(m_conwr<0)&&(m_skt>=0)) {
131
                // Can we accept a connection?
132
                struct  pollfd  pb;
133
 
134
                pb.fd = m_skt;
135
                pb.events = POLLIN;
136
                poll(&pb, 1, 0);
137
 
138
                if (pb.revents & POLLIN) {
139
                        m_conrd = accept(m_skt, 0, 0);
140
                        m_conwr = m_conrd;
141
 
142
                        if (m_conrd < 0)
143
                                perror("Accept failed:");
144
                }
145
        }
146
 
147
        if ((!i_tx)&&(m_last_tx))
148
                m_rx_changectr = 0;
149
        else    m_rx_changectr++;
150
        m_last_tx = i_tx;
151
 
152
        if (m_rx_state == RXIDLE) {
153
                if (!i_tx) {
154
                        m_rx_state = RXDATA;
155 3 dgisselq
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
156 2 dgisselq
                        m_rx_baudcounter -= m_rx_changectr;
157
                        m_rx_busy    = 0;
158
                        m_rx_data    = 0;
159
                }
160
        } else if (m_rx_baudcounter <= 0) {
161
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
162
                        m_rx_state = RXIDLE;
163
                        if (m_conwr >= 0) {
164
                                char    buf[1];
165
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
166
                                if (1 != send(m_conwr, buf, 1, 0)) {
167
                                        close(m_conwr);
168
                                        m_conrd = m_conwr = -1;
169
                                }
170
                        }
171
                } else {
172
                        m_rx_busy = (m_rx_busy << 1)|1;
173
                        // Low order bit is transmitted first, in this
174
                        // order:
175
                        //      Start bit (1'b1)
176
                        //      bit 0
177
                        //      bit 1
178
                        //      bit 2
179
                        //      ...
180
                        //      bit N-1
181
                        //      (possible parity bit)
182
                        //      stop bit
183
                        //      (possible secondary stop bit)
184
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
185 3 dgisselq
                } m_rx_baudcounter = m_baud_counts-1;
186 2 dgisselq
        } else
187
                m_rx_baudcounter--;
188
 
189
        if (m_tx_state == TXIDLE) {
190
                struct  pollfd  pb;
191
                pb.fd = m_conrd;
192
                pb.events = POLLIN;
193
                if (poll(&pb, 1, 0) < 0)
194
                        perror("Polling error:");
195
                if (pb.revents & POLLIN) {
196
                        char    buf[1];
197
                        if (1 == recv(m_conrd, buf, 1, MSG_DONTWAIT)) {
198
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
199
                                        // << nstart_bits
200
                                        |((buf[0]<<1)&0x01fe);
201
                                if (m_nparity) {
202
                                        int     p;
203
 
204
                                        // If m_nparity is set, we need to then
205
                                        // create the parity bit.
206
                                        if (m_fixdp)
207
                                                p = m_evenp;
208
                                        else {
209
                                                p = (m_tx_data >> 1)&0x0ff;
210
                                                p = p ^ (p>>4);
211
                                                p = p ^ (p>>2);
212
                                                p = p ^ (p>>1);
213
                                                p &= 1;
214
                                                p ^= m_evenp;
215
                                        }
216
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
217
                                }
218
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
219
                                m_tx_state = TXDATA;
220
                                o_rx = 0;
221 3 dgisselq
                                m_tx_baudcounter = m_baud_counts-1;
222 2 dgisselq
                        }
223
                }
224 3 dgisselq
        } else if (m_tx_baudcounter <= 0) {
225 2 dgisselq
                m_tx_data >>= 1;
226
                m_tx_busy >>= 1;
227
                if (!m_tx_busy)
228
                        m_tx_state = TXIDLE;
229
                else
230 3 dgisselq
                        m_tx_baudcounter = m_baud_counts-1;
231 2 dgisselq
                o_rx = m_tx_data&1;
232
        } else {
233
                m_tx_baudcounter--;
234
                o_rx = m_tx_data&1;
235
        }
236
 
237
        return o_rx;
238
}
239
 
240
int     UARTSIM::fdtick(int i_tx) {
241
        int     o_rx = 1;
242
 
243
        if ((!i_tx)&&(m_last_tx))
244
                m_rx_changectr = 0;
245
        else    m_rx_changectr++;
246
        m_last_tx = i_tx;
247
 
248
        if (m_rx_state == RXIDLE) {
249
                if (!i_tx) {
250
                        m_rx_state = RXDATA;
251 3 dgisselq
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2-1;
252 2 dgisselq
                        m_rx_baudcounter -= m_rx_changectr;
253
                        m_rx_busy    = 0;
254
                        m_rx_data    = 0;
255
                }
256
        } else if (m_rx_baudcounter <= 0) {
257
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
258
                        m_rx_state = RXIDLE;
259
                        if (m_conwr >= 0) {
260
                                char    buf[1];
261
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
262
                                if (1 != write(m_conwr, buf, 1)) {
263
                                        fprintf(stderr, "ERR while attempting to write out--closing output port\n");
264
                                        perror("UARTSIM::write() ");
265
                                        m_conrd = m_conwr = -1;
266
                                }
267
                        }
268
                } else {
269
                        m_rx_busy = (m_rx_busy << 1)|1;
270
                        // Low order bit is transmitted first, in this
271
                        // order:
272
                        //      Start bit (1'b1)
273
                        //      bit 0
274
                        //      bit 1
275
                        //      bit 2
276
                        //      ...
277
                        //      bit N-1
278
                        //      (possible parity bit)
279
                        //      stop bit
280
                        //      (possible secondary stop bit)
281
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
282 3 dgisselq
                } m_rx_baudcounter = m_baud_counts-1;
283 2 dgisselq
        } else
284
                m_rx_baudcounter--;
285
 
286
        if ((m_tx_state == TXIDLE)&&(m_conrd >= 0)) {
287
                struct  pollfd  pb;
288
                pb.fd = m_conrd;
289
                pb.events = POLLIN;
290
                if (poll(&pb, 1, 0) < 0)
291
                        perror("Polling error:");
292
                if (pb.revents & POLLIN) {
293
                        char    buf[1];
294
                        int     nr;
295
                        if (1==(nr = read(m_conrd, buf, 1))) {
296
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
297
                                        // << nstart_bits
298
                                        |((buf[0]<<1)&0x01fe);
299
                                if (m_nparity) {
300
                                        int     p;
301
 
302
                                        // If m_nparity is set, we need to then
303
                                        // create the parity bit.
304
                                        if (m_fixdp)
305
                                                p = m_evenp;
306
                                        else {
307
                                                p = (m_tx_data >> 1)&0x0ff;
308
                                                p = p ^ (p>>4);
309
                                                p = p ^ (p>>2);
310
                                                p = p ^ (p>>1);
311
                                                p &= 1;
312
                                                p ^= m_evenp;
313
                                        }
314
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
315
                                }
316
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
317
                                m_tx_state = TXDATA;
318
                                o_rx = 0;
319 3 dgisselq
                                m_tx_baudcounter = m_baud_counts-1;
320 2 dgisselq
                        } else if (nr < 0) {
321
                                fprintf(stderr, "ERR while attempting to read in--closing input port\n");
322
                                perror("UARTSIM::read() ");
323
                                m_conrd = -1;
324
                        } // and we really don't care if nr == 0 except that
325
                        // the poll above is supposed to keep it from happening
326
                }
327
        } else if (m_tx_baudcounter == 0) {
328
                m_tx_data >>= 1;
329
                m_tx_busy >>= 1;
330
                if (!m_tx_busy)
331
                        m_tx_state = TXIDLE;
332
                else
333 3 dgisselq
                        m_tx_baudcounter = m_baud_counts-1;
334 2 dgisselq
                o_rx = m_tx_data&1;
335
        } else {
336
                m_tx_baudcounter--;
337
                o_rx = m_tx_data&1;
338
        }
339
 
340
        return o_rx;
341
}
342
 

powered by: WebSVN 2.1.0

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