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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [bench/] [cpp/] [pipecmdr.h] - Blame information for rev 94

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

Line No. Rev Author Line
1 75 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 4 dgisselq
//
3
// Filename:    pipecmdr.h
4
//
5 75 dgisselq
// Project:     XuLA2-LX25 SoC based upon the ZipCPU
6 4 dgisselq
//
7
// Purpose:     This program attaches to a Verilated Verilog IP core.
8
//              It will not work apart from such a core.  Once attached,
9 75 dgisselq
//      it connects the simulated core to a controller via a pipe interface
10
//      designed to act like a UART.  Indeed, it is hoped that the final
11
//      interface would be via UART.  Until that point, however, this is just
12
//      a simple test facility designed to verify that the  IP core works
13
//      prior to such actual hardware implementation.
14 4 dgisselq
//
15 75 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
16
//              Gisselquist Technology, LLC
17 4 dgisselq
//
18 75 dgisselq
////////////////////////////////////////////////////////////////////////////////
19 4 dgisselq
//
20 75 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
21 4 dgisselq
//
22 75 dgisselq
// This program is free software (firmware): you can redistribute it and/or
23
// modify it under the terms of  the GNU General Public License as published
24
// by the Free Software Foundation, either version 3 of the License, or (at
25
// your option) any later version.
26
//
27
// This program is distributed in the hope that it will be useful, but WITHOUT
28
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
// for more details.
31
//
32
// You should have received a copy of the GNU General Public License along
33
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
34
// target there if the PDF file isn't present.)  If not, see
35
// <http://www.gnu.org/licenses/> for a copy.
36
//
37
// License:     GPL, v3, as defined and found on www.gnu.org,
38
//              http://www.gnu.org/licenses/gpl.html
39
//
40
//
41
////////////////////////////////////////////////////////////////////////////////
42
//
43
//
44 4 dgisselq
#ifndef PIPECMDR_H
45
#define PIPECMDR_H
46
 
47
#include <sys/types.h>
48
#include <sys/socket.h>
49
#include <poll.h>
50
#include <unistd.h>
51
#include <arpa/inet.h>
52
 
53
#include "testb.h"
54
 
55
#define PIPEBUFLEN      256
56
 
57
// At 115200 Baud, 8 bits of data, no parity and one stop bit, there will
58
// bit ten bits per character and therefore 8681 clocks per transfer
59
//      8681 ~= 100 MHz / 115200 (bauds / second) * 10 bauds / character
60
//
61
// #define      UARTLEN         8681 // Minimum ticks per character, 115200 Baud
62
//
63
// At 4MBaud, each bit takes 25 clocks.  10 bits would thus take 250 clocks
64
//              
65 47 dgisselq
// #define      UARTLEN         250 // Minimum ticks per character, 4M Baud
66
// #define      UARTLEN         1000 // Minimum ticks per character, 1M Hz
67 4 dgisselq
// #define      UARTLEN         8 // Minimum ticks per character
68 47 dgisselq
#define UARTLEN         4096    //
69 4 dgisselq
 
70
template <class VA>     class   PIPECMDR : public TESTB<VA> {
71
        void    setup_listener(const int port) {
72
                struct  sockaddr_in     my_addr;
73
 
74 94 dgisselq
                signal(SIGPIPE, SIG_IGN);
75
 
76 4 dgisselq
                printf("Listening on port %d\n", port);
77
 
78
                m_skt = socket(AF_INET, SOCK_STREAM, 0);
79
                if (m_skt < 0) {
80
                        perror("Could not allocate socket: ");
81
                        exit(-1);
82
                }
83
 
84
                // Set the reuse address option
85
                {
86
                        int optv = 1, er;
87
                        er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
88
                        if (er != 0) {
89
                                perror("SockOpt Err:");
90
                                exit(-1);
91
                        }
92
                }
93
 
94
                memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
95
                my_addr.sin_family = AF_INET;
96
                my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
97
                my_addr.sin_port = htons(port);
98
 
99
                if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
100
                        perror("BIND FAILED:");
101
                        exit(-1);
102
                }
103
 
104
                if (listen(m_skt, 1) != 0) {
105
                        perror("Listen failed:");
106
                        exit(-1);
107
                }
108
        }
109
 
110
public:
111
        int     m_skt, m_con;
112
        char    m_txbuf[PIPEBUFLEN], m_rxbuf[PIPEBUFLEN];
113 47 dgisselq
        int     m_ilen, m_rxpos, m_txpos, m_uart_wait, m_tx_busy;
114 4 dgisselq
        bool    m_started_flag;
115
 
116
        PIPECMDR(const int port) : TESTB<VA>() {
117
                m_con = m_skt = -1;
118
                setup_listener(port);
119
                m_rxpos = m_txpos = m_ilen = 0;
120
                m_started_flag = false;
121 47 dgisselq
                m_uart_wait = 0; // Flow control into the FPGA
122
                m_tx_busy   = 0; // Flow control out of the FPGA
123 4 dgisselq
        }
124
 
125
        virtual void    kill(void) {
126
                // Close any active connection
127
                if (m_con >= 0)  close(m_con);
128
                if (m_skt >= 0) close(m_skt);
129
        }
130
 
131
        virtual void    tick(void) {
132
                if (m_con < 0) {
133
                        // Can we accept a connection?
134
                        struct  pollfd  pb;
135
 
136
                        pb.fd = m_skt;
137
                        pb.events = POLLIN;
138
                        poll(&pb, 1, 0);
139
 
140
                        if (pb.revents & POLLIN) {
141
                                m_con = accept(m_skt, 0, 0);
142
 
143
                                if (m_con < 0)
144
                                        perror("Accept failed:");
145
                        }
146
                }
147
 
148
                TESTB<VA>::m_core->i_rx_stb = 0;
149
 
150
                if (m_uart_wait == 0) {
151
                        if (m_ilen > 0) {
152
                                // Is there a byte in our buffer somewhere?
153
                                TESTB<VA>::m_core->i_rx_stb = 1;
154
                                TESTB<VA>::m_core->i_rx_data = m_rxbuf[m_rxpos++];
155
                                m_ilen--;
156
                        } else if (m_con > 0) {
157
                                // Is there a byte to be read here?
158
                                struct  pollfd  pb;
159
                                pb.fd = m_con;
160
                                pb.events = POLLIN;
161
                                if (poll(&pb, 1, 0) < 0)
162
                                        perror("Polling error:");
163
                                if (pb.revents & POLLIN) {
164
                                        if ((m_ilen =recv(m_con, m_rxbuf, sizeof(m_rxbuf), MSG_DONTWAIT)) > 0) {
165
                                                m_rxbuf[m_ilen] = '\0';
166
                                                if (m_rxbuf[m_ilen-1] == '\n') {
167
                                                        m_rxbuf[m_ilen-1] = '\0';
168
                                                        printf("< \'%s\'\n", m_rxbuf);
169
                                                        m_rxbuf[m_ilen-1] = '\n';
170
                                                } else printf("< \'%s\'\n", m_rxbuf);
171
                                                TESTB<VA>::m_core->i_rx_stb = 1;
172
                                                TESTB<VA>::m_core->i_rx_data = m_rxbuf[0];
173
                                                m_rxpos = 1; m_ilen--;
174
                                                m_started_flag = true;
175
                                        } else if (m_ilen < 0) {
176
                                                // An error occurred, close the connection
177 37 dgisselq
                                                // This could also be the
178
                                                // indication of a simple
179
                                                // connection close, so we deal
180
                                                // with this quietly.
181
                                                // perror("Read error: ");
182
                                                // fprintf(stderr, "Closing connection\n");
183 4 dgisselq
                                                close(m_con);
184
                                                m_con = -1;
185
                                        } else { // the connection closed on us
186
                                                close(m_con);
187
                                                m_con = -1;
188
                                        }
189
                                }
190
                        } m_uart_wait = (TESTB<VA>::m_core->i_rx_stb)?UARTLEN:0;
191
                } else {
192
                        // Still working on transmitting a character
193
                        m_uart_wait = m_uart_wait - 1;
194
                }
195
 
196
                /*
197
                if (TESTB<VA>::m_core->i_rx_stb) {
198
                        putchar(TESTB<VA>::m_core->i_rx_data);
199
                        fflush(stdout);
200
                }
201
                */
202
                TESTB<VA>::tick();
203
 
204 47 dgisselq
                bool tx_accepted = false;
205
                if (m_tx_busy == 0) {
206
                        if ((TESTB<VA>::m_core->o_tx_stb)&&(m_con > 0)) {
207
                                m_txbuf[m_txpos++] = TESTB<VA>::m_core->o_tx_data;
208
                                tx_accepted = true;
209
                                if ((TESTB<VA>::m_core->o_tx_data == '\n')||(m_txpos >= sizeof(m_txbuf))) {
210
                                        int     snt = 0;
211
                                        snt = send(m_con, m_txbuf, m_txpos, 0);
212 75 dgisselq
                                        if (snt < 0) {
213
                                                close(m_con);
214
                                                m_con = -1;
215
                                                snt = 0;
216
                                        }
217 47 dgisselq
                                        m_txbuf[m_txpos] = '\0';
218
                                        printf("> %s", m_txbuf);
219
                                        if (snt < m_txpos) {
220 75 dgisselq
                                                fprintf(stderr, "Only sent %d bytes of %d!\n",
221
                                                        snt, m_txpos);
222 47 dgisselq
                                        }
223
                                        m_txpos = 0;
224 4 dgisselq
                                }
225
                        }
226 47 dgisselq
                } else
227
                        m_tx_busy--;
228
 
229
                if ((TESTB<VA>::m_core->o_tx_stb)&&(TESTB<VA>::m_core->i_tx_busy==0))
230
                        m_tx_busy = UARTLEN;
231
                TESTB<VA>::m_core->i_tx_busy = (m_tx_busy != 0);
232
 
233
                if (0) {
234
                        if ((m_tx_busy!=0)||(TESTB<VA>::m_core->i_tx_busy)
235
                                ||(TESTB<VA>::m_core->o_tx_stb)
236
                                ||(tx_accepted))
237
                                printf("%4d %d %d %02x %s\n",
238
                                        m_tx_busy,
239
                                        TESTB<VA>::m_core->i_tx_busy,
240
                                        TESTB<VA>::m_core->o_tx_stb,
241
                                        TESTB<VA>::m_core->o_tx_data,
242
                                        (tx_accepted)?"READ!":"");
243 4 dgisselq
                }
244
 
245 47 dgisselq
 
246 4 dgisselq
                /*
247
                if((TESTB<VA>::m_core->o_wb_cyc)||(TESTB<VA>::m_core->o_wb_stb)){
248
                        printf("BUS: %d,%d,%d %8x %8x\n",
249
                                TESTB<VA>::m_core->o_wb_cyc,
250
                                TESTB<VA>::m_core->o_wb_stb,
251
                                TESTB<VA>::m_core->o_wb_we,
252
                                TESTB<VA>::m_core->o_wb_addr,
253
                                TESTB<VA>::m_core->o_wb_data);
254
                } else if (m_started_flag) {
255
                        printf("%02x,%c,%d,%d,%02x -> %d,%d,%d, %2x,%2x,%2x\n",
256
                                TESTB<VA>::m_core->i_rx_data,
257
                                (TESTB<VA>::m_core->i_rx_stb)?(TESTB<VA>::m_core->i_rx_data):' ',
258
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__r_valid,
259
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__rx_eol,
260
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__rx_six_bits,
261
                                //
262
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_strobe,
263
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_hold,
264
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_we,
265
                                //
266
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__state,
267
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__nreg,
268
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__szreg);
269
                }
270
                */
271
        }
272
};
273
 
274
#endif

powered by: WebSVN 2.1.0

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