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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [bench/] [cpp/] [uartsim.cpp] - Blame information for rev 117

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 112 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    uartsim.cpp
4
//
5
// Project:     XuLA2-LX25 SoC based upon the ZipCPU
6
//
7
// Purpose:     
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
 
48
#include "uartsim.h"
49
 
50
void    UARTSIM::setup_listener(const int port) {
51
        struct  sockaddr_in     my_addr;
52
 
53
        signal(SIGPIPE, SIG_IGN);
54
 
55
        printf("Listening on port %d\n", port);
56
 
57
        m_skt = socket(AF_INET, SOCK_STREAM, 0);
58
        if (m_skt < 0) {
59
                perror("Could not allocate socket: ");
60
                exit(-1);
61
        }
62
 
63
        // Set the reuse address option
64
        {
65
                int optv = 1, er;
66
                er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
67
                if (er != 0) {
68
                        perror("SockOpt Err:");
69
                        exit(-1);
70
                }
71
        }
72
 
73
        memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
74
        my_addr.sin_family = AF_INET;
75
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
76
        my_addr.sin_port = htons(port);
77
 
78
        if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
79
                perror("BIND FAILED:");
80
                exit(-1);
81
        }
82
 
83
        if (listen(m_skt, 1) != 0) {
84
                perror("Listen failed:");
85
                exit(-1);
86
        }
87
}
88
 
89
UARTSIM::UARTSIM(const int port) {
90
        m_con = m_skt = -1;
91
        setup_listener(port);
92
        setup(25);
93
        m_rx_baudcounter = 0;
94
        m_tx_baudcounter = 0;
95
        m_rx_state = RXIDLE;
96
        m_tx_state = TXIDLE;
97
}
98
 
99
void    UARTSIM::kill(void) {
100
        // Close any active connection
101
        if (m_con >= 0)  close(m_con);
102
        if (m_skt >= 0) close(m_skt);
103
}
104
 
105
void    UARTSIM::setup(unsigned isetup) {
106
        if (isetup != m_setup) {
107
                m_setup = isetup;
108
                m_baud_counts = (isetup & 0x0ffffff);
109
                m_nbits   = 8-((isetup >> 28)&0x03);
110
                m_nstop   =((isetup >> 27)&1)+1;
111
                m_nparity = (isetup >> 26)&1;
112
                m_fixdp   = (isetup >> 25)&1;
113
                m_evenp   = (isetup >> 24)&1;
114
        }
115
}
116
 
117
int     UARTSIM::tick(int i_tx) {
118
        int     o_rx = 1;
119
 
120
        if (m_con < 0) {
121
                // Can we accept a connection?
122
                struct  pollfd  pb;
123
 
124
                pb.fd = m_skt;
125
                pb.events = POLLIN;
126
                poll(&pb, 1, 0);
127
 
128
                if (pb.revents & POLLIN) {
129
                        m_con = accept(m_skt, 0, 0);
130
 
131
                        if (m_con < 0)
132
                                perror("Accept failed:");
133
                }
134
        }
135
 
136
        if ((!i_tx)&&(m_last_tx))
137
                m_rx_changectr = 0;
138
        else    m_rx_changectr++;
139
        m_last_tx = i_tx;
140
 
141
        if (m_rx_state == RXIDLE) {
142
                if (!i_tx) {
143
                        m_rx_state = RXDATA;
144
                        m_rx_baudcounter =m_baud_counts+m_baud_counts/2;
145
                        m_rx_baudcounter -= m_rx_changectr;
146
                        m_rx_busy    = 0;
147
                        m_rx_data    = 0;
148
                }
149
        } else if (m_rx_baudcounter <= 0) {
150
                if (m_rx_busy >= (1<<(m_nbits+m_nparity+m_nstop-1))) {
151
                        m_rx_state = RXIDLE;
152
                        if (m_con > 0) {
153
                                char    buf[1];
154
                                buf[0] = (m_rx_data >> (32-m_nbits-m_nstop-m_nparity))&0x0ff;
155
                                if (1 != send(m_con, buf, 1, 0)) {
156
                                        close(m_con);
157
                                        m_con = -1;
158
                                }
159
                        }
160
                } else {
161
                        m_rx_busy = (m_rx_busy << 1)|1;
162
                        // Low order bit is transmitted first, in this
163
                        // order:
164
                        //      Start bit (1'b1)
165
                        //      bit 0
166
                        //      bit 1
167
                        //      bit 2
168
                        //      ...
169
                        //      bit N-1
170
                        //      (possible parity bit)
171
                        //      stop bit
172
                        //      (possible secondary stop bit)
173
                        m_rx_data = ((i_tx&1)<<31) | (m_rx_data>>1);
174
                } m_rx_baudcounter = m_baud_counts;
175
        } else
176
                m_rx_baudcounter--;
177
 
178
        if (m_tx_state == TXIDLE) {
179
                struct  pollfd  pb;
180
                pb.fd = m_con;
181
                pb.events = POLLIN;
182
                if (poll(&pb, 1, 0) < 0)
183
                        perror("Polling error:");
184
                if (pb.revents & POLLIN) {
185
                        char    buf[1];
186
                        if (1 == recv(m_con, buf, 1, MSG_DONTWAIT)) {
187
                                m_tx_data = (-1<<(m_nbits+m_nparity+1))
188
                                        // << nstart_bits
189
                                        |((buf[0]<<1)&0x01fe);
190
                                if (m_nparity) {
191
                                        int     p;
192
                                        if (m_fixdp)
193
                                                p = m_evenp;
194
                                        else {
195
                                                int p = (m_tx_data >> 1)&0x0ff;
196
                                                p = p ^ (p>>4);
197
                                                p = p ^ (p>>2);
198
                                                p = p ^ (p>>1);
199
                                                p &= 1;
200
                                                p ^= m_evenp;
201
                                        }
202
                                        m_tx_data |= (p<<(m_nbits+m_nparity));
203
                                }
204
                                m_tx_busy = (1<<(m_nbits+m_nparity+m_nstop+1))-1;
205
                                m_tx_state = TXDATA;
206
                                o_rx = 0;
207
                                m_tx_baudcounter = m_baud_counts;
208
                        }
209
                }
210
        } else if (m_tx_baudcounter == 0) {
211
                m_tx_data >>= 1;
212
                m_tx_busy >>= 1;
213
                if (!m_tx_busy)
214
                        m_tx_state = TXIDLE;
215
                else
216
                        m_tx_baudcounter = m_baud_counts;
217
                o_rx = m_tx_data&1;
218
        } else {
219
                m_tx_baudcounter--;
220
                o_rx = m_tx_data&1;
221
        }
222
 
223
        return o_rx;
224
}
225
 
226
/*
227
        0x00    -> c0   = 1100 0000???
228
        0x01    -> c0   = 1100 0000??
229
        0x02    -> c0   = 1100 0000?
230
        0x04    -> c1   = 1100 0001
231
        0x20    -> c5   = 1100 0101
232
        0x40    -> ca   = 1100 1010
233
        0x80    -> d4   = 1101 0100
234
 */

powered by: WebSVN 2.1.0

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