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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sim/] [verilated/] [uartsim.cpp] - Blame information for rev 58

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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