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

Subversion Repositories xulalx25soc

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

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
                printf("Listening on port %d\n", port);
75
 
76
                m_skt = socket(AF_INET, SOCK_STREAM, 0);
77
                if (m_skt < 0) {
78
                        perror("Could not allocate socket: ");
79
                        exit(-1);
80
                }
81
 
82
                // Set the reuse address option
83
                {
84
                        int optv = 1, er;
85
                        er = setsockopt(m_skt, SOL_SOCKET, SO_REUSEADDR, &optv, sizeof(optv));
86
                        if (er != 0) {
87
                                perror("SockOpt Err:");
88
                                exit(-1);
89
                        }
90
                }
91
 
92
                memset(&my_addr, 0, sizeof(struct sockaddr_in)); // clear structure
93
                my_addr.sin_family = AF_INET;
94
                my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
95
                my_addr.sin_port = htons(port);
96
 
97
                if (bind(m_skt, (struct sockaddr *)&my_addr, sizeof(my_addr))!=0) {
98
                        perror("BIND FAILED:");
99
                        exit(-1);
100
                }
101
 
102
                if (listen(m_skt, 1) != 0) {
103
                        perror("Listen failed:");
104
                        exit(-1);
105
                }
106
        }
107
 
108
public:
109
        int     m_skt, m_con;
110
        char    m_txbuf[PIPEBUFLEN], m_rxbuf[PIPEBUFLEN];
111 47 dgisselq
        int     m_ilen, m_rxpos, m_txpos, m_uart_wait, m_tx_busy;
112 4 dgisselq
        bool    m_started_flag;
113
 
114
        PIPECMDR(const int port) : TESTB<VA>() {
115
                m_con = m_skt = -1;
116
                setup_listener(port);
117
                m_rxpos = m_txpos = m_ilen = 0;
118
                m_started_flag = false;
119 47 dgisselq
                m_uart_wait = 0; // Flow control into the FPGA
120
                m_tx_busy   = 0; // Flow control out of the FPGA
121 4 dgisselq
        }
122
 
123
        virtual void    kill(void) {
124
                // Close any active connection
125
                if (m_con >= 0)  close(m_con);
126
                if (m_skt >= 0) close(m_skt);
127
        }
128
 
129
        virtual void    tick(void) {
130
                if (m_con < 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_con = accept(m_skt, 0, 0);
140
 
141
                                if (m_con < 0)
142
                                        perror("Accept failed:");
143
                        }
144
                }
145
 
146
                TESTB<VA>::m_core->i_rx_stb = 0;
147
 
148
                if (m_uart_wait == 0) {
149
                        if (m_ilen > 0) {
150
                                // Is there a byte in our buffer somewhere?
151
                                TESTB<VA>::m_core->i_rx_stb = 1;
152
                                TESTB<VA>::m_core->i_rx_data = m_rxbuf[m_rxpos++];
153
                                m_ilen--;
154
                        } else if (m_con > 0) {
155
                                // Is there a byte to be read here?
156
                                struct  pollfd  pb;
157
                                pb.fd = m_con;
158
                                pb.events = POLLIN;
159
                                if (poll(&pb, 1, 0) < 0)
160
                                        perror("Polling error:");
161
                                if (pb.revents & POLLIN) {
162
                                        if ((m_ilen =recv(m_con, m_rxbuf, sizeof(m_rxbuf), MSG_DONTWAIT)) > 0) {
163
                                                m_rxbuf[m_ilen] = '\0';
164
                                                if (m_rxbuf[m_ilen-1] == '\n') {
165
                                                        m_rxbuf[m_ilen-1] = '\0';
166
                                                        printf("< \'%s\'\n", m_rxbuf);
167
                                                        m_rxbuf[m_ilen-1] = '\n';
168
                                                } else printf("< \'%s\'\n", m_rxbuf);
169
                                                TESTB<VA>::m_core->i_rx_stb = 1;
170
                                                TESTB<VA>::m_core->i_rx_data = m_rxbuf[0];
171
                                                m_rxpos = 1; m_ilen--;
172
                                                m_started_flag = true;
173
                                        } else if (m_ilen < 0) {
174
                                                // An error occurred, close the connection
175 37 dgisselq
                                                // This could also be the
176
                                                // indication of a simple
177
                                                // connection close, so we deal
178
                                                // with this quietly.
179
                                                // perror("Read error: ");
180
                                                // fprintf(stderr, "Closing connection\n");
181 4 dgisselq
                                                close(m_con);
182
                                                m_con = -1;
183
                                        } else { // the connection closed on us
184
                                                close(m_con);
185
                                                m_con = -1;
186
                                        }
187
                                }
188
                        } m_uart_wait = (TESTB<VA>::m_core->i_rx_stb)?UARTLEN:0;
189
                } else {
190
                        // Still working on transmitting a character
191
                        m_uart_wait = m_uart_wait - 1;
192
                }
193
 
194
                /*
195
                if (TESTB<VA>::m_core->i_rx_stb) {
196
                        putchar(TESTB<VA>::m_core->i_rx_data);
197
                        fflush(stdout);
198
                }
199
                */
200
                TESTB<VA>::tick();
201
 
202 47 dgisselq
                bool tx_accepted = false;
203
                if (m_tx_busy == 0) {
204
                        if ((TESTB<VA>::m_core->o_tx_stb)&&(m_con > 0)) {
205
                                m_txbuf[m_txpos++] = TESTB<VA>::m_core->o_tx_data;
206
                                tx_accepted = true;
207
                                if ((TESTB<VA>::m_core->o_tx_data == '\n')||(m_txpos >= sizeof(m_txbuf))) {
208
                                        int     snt = 0;
209
                                        snt = send(m_con, m_txbuf, m_txpos, 0);
210 75 dgisselq
                                        if (snt < 0) {
211
                                                close(m_con);
212
                                                m_con = -1;
213
                                                snt = 0;
214
                                        }
215 47 dgisselq
                                        m_txbuf[m_txpos] = '\0';
216
                                        printf("> %s", m_txbuf);
217
                                        if (snt < m_txpos) {
218 75 dgisselq
                                                fprintf(stderr, "Only sent %d bytes of %d!\n",
219
                                                        snt, m_txpos);
220 47 dgisselq
                                        }
221
                                        m_txpos = 0;
222 4 dgisselq
                                }
223
                        }
224 47 dgisselq
                } else
225
                        m_tx_busy--;
226
 
227
                if ((TESTB<VA>::m_core->o_tx_stb)&&(TESTB<VA>::m_core->i_tx_busy==0))
228
                        m_tx_busy = UARTLEN;
229
                TESTB<VA>::m_core->i_tx_busy = (m_tx_busy != 0);
230
 
231
                if (0) {
232
                        if ((m_tx_busy!=0)||(TESTB<VA>::m_core->i_tx_busy)
233
                                ||(TESTB<VA>::m_core->o_tx_stb)
234
                                ||(tx_accepted))
235
                                printf("%4d %d %d %02x %s\n",
236
                                        m_tx_busy,
237
                                        TESTB<VA>::m_core->i_tx_busy,
238
                                        TESTB<VA>::m_core->o_tx_stb,
239
                                        TESTB<VA>::m_core->o_tx_data,
240
                                        (tx_accepted)?"READ!":"");
241 4 dgisselq
                }
242
 
243 47 dgisselq
 
244 4 dgisselq
                /*
245
                if((TESTB<VA>::m_core->o_wb_cyc)||(TESTB<VA>::m_core->o_wb_stb)){
246
                        printf("BUS: %d,%d,%d %8x %8x\n",
247
                                TESTB<VA>::m_core->o_wb_cyc,
248
                                TESTB<VA>::m_core->o_wb_stb,
249
                                TESTB<VA>::m_core->o_wb_we,
250
                                TESTB<VA>::m_core->o_wb_addr,
251
                                TESTB<VA>::m_core->o_wb_data);
252
                } else if (m_started_flag) {
253
                        printf("%02x,%c,%d,%d,%02x -> %d,%d,%d, %2x,%2x,%2x\n",
254
                                TESTB<VA>::m_core->i_rx_data,
255
                                (TESTB<VA>::m_core->i_rx_stb)?(TESTB<VA>::m_core->i_rx_data):' ',
256
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__r_valid,
257
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__rx_eol,
258
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__rx_six_bits,
259
                                //
260
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_strobe,
261
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_hold,
262
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__o_rq_we,
263
                                //
264
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__state,
265
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__nreg,
266
                                TESTB<VA>::m_core->v__DOT__decodewb__DOT__szreg);
267
                }
268
                */
269
        }
270
};
271
 
272
#endif

powered by: WebSVN 2.1.0

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