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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [cpp/] [linetest.cpp] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    linetest.cpp
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     To create a pass-through test of the receiver and transmitter
8
//              which can be exercised/proven via Verilator.
9
//
10
//      If you run this program with no arguments, it will run an automatic
11 5 dgisselq
//      test, returning "PASS" on success, or "FAIL" on failure as a last
12 2 dgisselq
//      output line--hence it should support automated testing.
13
//
14
//      If you run with a '-i' argument, the program will run interactively.
15
//      It will then be up to you to determine if it works (or not).  As
16
//      always, it may be killed with a control C.
17
//
18
// Creator:     Dan Gisselquist, Ph.D.
19
//              Gisselquist Technology, LLC
20
//
21
////////////////////////////////////////////////////////////////////////////////
22
//
23
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
24
//
25
// This program is free software (firmware): you can redistribute it and/or
26
// modify it under the terms of  the GNU General Public License as published
27
// by the Free Software Foundation, either version 3 of the License, or (at
28
// your option) any later version.
29
//
30
// This program is distributed in the hope that it will be useful, but WITHOUT
31
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
32
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
33
// for more details.
34
//
35
// You should have received a copy of the GNU General Public License along
36
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
37
// target there if the PDF file isn't present.)  If not, see
38
// <http://www.gnu.org/licenses/> for a copy.
39
//
40
// License:     GPL, v3, as defined and found on www.gnu.org,
41
//              http://www.gnu.org/licenses/gpl.html
42
//
43
//
44
////////////////////////////////////////////////////////////////////////////////
45
//
46
//
47
#include <stdio.h>
48
#include <fcntl.h>
49
#include <unistd.h>
50
#include <string.h>
51
#include <time.h>
52
#include <sys/types.h>
53 5 dgisselq
#include <sys/wait.h>
54 2 dgisselq
#include <signal.h>
55
#include "verilated.h"
56
#include "Vlinetest.h"
57 5 dgisselq
#include "verilated_vcd_c.h"
58 2 dgisselq
#include "uartsim.h"
59
 
60
int     main(int argc, char **argv) {
61
        Verilated::commandArgs(argc, argv);
62
        Vlinetest       tb;
63
        UARTSIM         *uart;
64
        bool            run_interactively = false;
65
        int             port = 0;
66
        unsigned        setup = 25;
67 5 dgisselq
        char string[] = "This is a UART testing string\r\n";
68 2 dgisselq
 
69
        for(int argn=1; argn<argc; argn++) {
70
                if (argv[argn][0] == '-') for(int j=1; (j<1000)&&(argv[argn][j]); j++)
71
                switch(argv[argn][j]) {
72
                        case 'i':
73
                                run_interactively = true;
74
                                break;
75
                        case 'p':
76
                                port = atoi(argv[argn++]); j+= 4000;
77
                                run_interactively = true;
78
                                break;
79
                        case 's':
80 3 dgisselq
                                setup= strtoul(argv[++argn], NULL, 0); j+= 4000;
81 2 dgisselq
                                break;
82
                        default:
83
                                printf("Undefined option, -%c\n", argv[argn][j]);
84
                                break;
85
                }
86
        }
87
 
88
        tb.i_setup = setup;
89 5 dgisselq
        int baudclocks = setup & 0x0ffffff;
90
        tb.i_uart_rx = 1;
91 2 dgisselq
        if (run_interactively) {
92
                uart = new UARTSIM(port);
93
                uart->setup(tb.i_setup);
94
 
95
                while(1) {
96
 
97
                        tb.i_clk = 1;
98
                        tb.eval();
99
                        tb.i_clk = 0;
100
                        tb.eval();
101
 
102 5 dgisselq
                        tb.i_uart_rx = (*uart)(tb.o_uart_tx);
103 2 dgisselq
                }
104
 
105
        } else {
106
                int     childs_stdin[2], childs_stdout[2];
107
 
108
                if ((pipe(childs_stdin)!=0)||(pipe(childs_stdout) != 0)) {
109
                        fprintf(stderr, "ERR setting up child pipes\n");
110
                        perror("O/S ERR");
111
                        printf("TEST FAILURE\n");
112
                        exit(EXIT_FAILURE);
113
                }
114
 
115 5 dgisselq
                pid_t childs_pid = fork();
116 2 dgisselq
 
117 5 dgisselq
                if (childs_pid < 0) {
118 2 dgisselq
                        fprintf(stderr, "ERR setting up child process\n");
119
                        perror("O/S ERR");
120
                        printf("TEST FAILURE\n");
121
                        exit(EXIT_FAILURE);
122
                }
123
 
124 5 dgisselq
                if (childs_pid) {
125 2 dgisselq
                        int     nr=-2, nw;
126
 
127
                        // We are the parent
128
                        close(childs_stdin[ 0]); // Close the read end
129
                        close(childs_stdout[1]); // Close the write end
130
 
131
                        char test[256];
132
 
133
                        nw = write(childs_stdin[1], string, strlen(string));
134
                        if (nw == (int)strlen(string)) {
135
                                int     rpos = 0;
136
                                test[0] = '\0';
137
                                while((rpos<nw)
138
                                        &&(0<(nr=read(childs_stdout[0],
139
                                                &test[rpos], strlen(string)-rpos))))
140
                                        rpos += nr;
141
 
142
                                nr = rpos;
143
                                if (rpos > 0)
144
                                        test[rpos] = '\0';
145
                                printf("Successfully read %d characters: %s\n", nr, test);
146
                        }
147
 
148 5 dgisselq
                        int     status = 0, rv = -1;
149 2 dgisselq
 
150 5 dgisselq
                        // Give the child the oppoortunity to take another
151
                        // 60 seconds to finish closing itself
152
                        for(int waitcount=0; waitcount < 60; waitcount++) {
153
                                rv = waitpid(-1, &status, WNOHANG);
154
                                if (rv == childs_pid)
155
                                        break;
156
                                else if (rv < 0)
157
                                        break;
158
                                else // rv == 0
159
                                        sleep(1);
160
                        }
161
 
162
                        if (rv != childs_pid) {
163
                                kill(childs_pid, SIGTERM);
164
                                printf("WARNING: Child/simulator did not terminate normally\n");
165
                        }
166
 
167
                        if (WEXITSTATUS(status) != EXIT_SUCCESS) {
168
                                printf("WARNING: Child/simulator exit status does not indicate success\n");
169
                        }
170
 
171 2 dgisselq
                        if ((nr == nw)&&(nw == (int)strlen(string))
172 5 dgisselq
                                        &&(strcmp(test, string) == 0)) {
173
                                printf("PASS!\n");
174
                                exit(EXIT_SUCCESS);
175
                        } else {
176 2 dgisselq
                                printf("TEST FAILED\n");
177 5 dgisselq
                                exit(EXIT_FAILURE);
178
                        }
179 2 dgisselq
                } else {
180
                        close(childs_stdin[ 1]);
181
                        close(childs_stdout[0]);
182
                        close(STDIN_FILENO);
183
                        if (dup(childs_stdin[0]) < 0) {
184
                                fprintf(stderr, "ERR setting up child FD\n");
185
                                perror("O/S ERR");
186
                                exit(EXIT_FAILURE);
187
                        }
188
                        close(STDOUT_FILENO);
189
                        if (dup(childs_stdout[1]) < 0) {
190
                                fprintf(stderr, "ERR setting up child FD\n");
191
                                perror("O/S ERR");
192
                                exit(EXIT_FAILURE);
193
                        }
194
 
195
                        // UARTSIM(0) uses stdin and stdout for its FD's
196
                        uart = new UARTSIM(0);
197
                        uart->setup(tb.i_setup);
198
 
199
                        // Make sure we don't run longer than 4 seconds ...
200
                        time_t  start = time(NULL);
201
                        int     iterations_before_check = 2048;
202 5 dgisselq
                        unsigned        clocks = 0;
203 2 dgisselq
                        bool    done = false;
204
 
205 5 dgisselq
#define VCDTRACE
206
#ifdef  VCDTRACE
207
                        Verilated::traceEverOn(true);
208
                        VerilatedVcdC* tfp = new VerilatedVcdC;
209
                        tb.trace(tfp, 99);
210
                        tfp->open("linetest.vcd");
211
#define TRACE_POSEDGE   tfp->dump(10*clocks)
212
#define TRACE_NEGEDGE   tfp->dump(10*clocks+5)
213
#define TRACE_CLOSE     tfp->close()
214
#else
215
#define TRACE_POSEDGE   while(0)
216
#define TRACE_NEGEDGE   while(0)
217
#define TRACE_CLOSE     while(0)
218
#endif
219
 
220
                        for(int i=0; i<(baudclocks*24); i++) {
221 2 dgisselq
                                // Clear any initial break condition
222
                                tb.i_clk = 1;
223
                                tb.eval();
224
                                tb.i_clk = 0;
225
                                tb.eval();
226
 
227 5 dgisselq
                                tb.i_uart_rx = 1;
228 2 dgisselq
                        }
229
 
230 5 dgisselq
                        while(clocks < 2*(baudclocks*16)*strlen(string)) {
231 2 dgisselq
                                tb.i_clk = 1;
232
                                tb.eval();
233 5 dgisselq
                                TRACE_POSEDGE;
234 2 dgisselq
                                tb.i_clk = 0;
235
                                tb.eval();
236 5 dgisselq
                                TRACE_NEGEDGE;
237
                                clocks++;
238 2 dgisselq
 
239 5 dgisselq
                                tb.i_uart_rx = (*uart)(tb.o_uart_tx);
240 2 dgisselq
 
241 3 dgisselq
                                if (false) {
242 5 dgisselq
                                        /*
243 3 dgisselq
                                        static long counts = 0;
244
                                        static int lasti = 1, lasto = 1;
245
                                        bool    writeout = false;
246 2 dgisselq
 
247 3 dgisselq
                                        counts++;
248
                                        if (lasti != tb.i_uart) {
249
                                                writeout = true;
250
                                                lasti = tb.i_uart;
251
                                        } if (lasto != tb.o_uart) {
252
                                                writeout = true;
253
                                                lasto = tb.o_uart;
254
                                        }
255
 
256
                                        if (writeout) {
257
                                        fprintf(stderr, "%08lx : [%d -> %d] %02x:%02x (%02x/%d) %d,%d->%02x [%2d/%d/%08x]\n",
258
                                                counts, tb.i_uart, tb.o_uart,
259
                                                tb.v__DOT__head,
260
                                                tb.v__DOT__tail,
261
                                                tb.v__DOT__lineend,
262
                                                tb.v__DOT__run_tx,
263
                                                tb.v__DOT__tx_stb,
264
                                                tb.v__DOT__transmitter__DOT__r_busy,
265
                                                tb.v__DOT__tx_data & 0x0ff,
266
                                                tb.v__DOT__transmitter__DOT__state,
267
                                                tb.v__DOT__transmitter__DOT__zero_baud_counter,
268
                                                tb.v__DOT__transmitter__DOT__baud_counter);
269 5 dgisselq
                                        } */
270 3 dgisselq
                                }
271
 
272 2 dgisselq
                                if (iterations_before_check-- <= 0) {
273
                                        iterations_before_check = 2048;
274
                                        done = ((time(NULL)-start)>60);
275
                                        if (done)
276
                                        fprintf(stderr, "CHILD-TIMEOUT\n");
277
                                }
278
                        }
279 5 dgisselq
 
280
                        TRACE_CLOSE;
281
 
282
                        exit(EXIT_SUCCESS);
283
                }
284 2 dgisselq
        }
285
}

powered by: WebSVN 2.1.0

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