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

Subversion Repositories wbuart32

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

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
//      test, returning "SUCCESS" on success, or "FAIL" on failure as a last
12
//      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
#include <signal.h>
54
#include "verilated.h"
55
#include "Vlinetest.h"
56
#include "uartsim.h"
57
 
58
int     main(int argc, char **argv) {
59
        Verilated::commandArgs(argc, argv);
60
        Vlinetest       tb;
61
        UARTSIM         *uart;
62
        bool            run_interactively = false;
63
        int             port = 0;
64
        unsigned        setup = 25;
65
 
66
        for(int argn=1; argn<argc; argn++) {
67
                if (argv[argn][0] == '-') for(int j=1; (j<1000)&&(argv[argn][j]); j++)
68
                switch(argv[argn][j]) {
69
                        case 'i':
70
                                run_interactively = true;
71
                                break;
72
                        case 'p':
73
                                port = atoi(argv[argn++]); j+= 4000;
74
                                run_interactively = true;
75
                                break;
76
                        case 's':
77
                                setup= strtoul(argv[argn++], NULL, 0); j+= 4000;
78
                                break;
79
                        default:
80
                                printf("Undefined option, -%c\n", argv[argn][j]);
81
                                break;
82
                }
83
        }
84
 
85
        tb.i_setup = setup;
86
        tb.i_uart = 1;
87
        if (run_interactively) {
88
                uart = new UARTSIM(port);
89
                uart->setup(tb.i_setup);
90
 
91
                while(1) {
92
 
93
                        tb.i_clk = 1;
94
                        tb.eval();
95
                        tb.i_clk = 0;
96
                        tb.eval();
97
 
98
                        tb.i_uart = (*uart)(tb.o_uart);
99
                }
100
 
101
        } else {
102
                int     childs_stdin[2], childs_stdout[2];
103
 
104
                if ((pipe(childs_stdin)!=0)||(pipe(childs_stdout) != 0)) {
105
                        fprintf(stderr, "ERR setting up child pipes\n");
106
                        perror("O/S ERR");
107
                        printf("TEST FAILURE\n");
108
                        exit(EXIT_FAILURE);
109
                }
110
 
111
                pid_t pid = fork();
112
 
113
                if (pid < 0) {
114
                        fprintf(stderr, "ERR setting up child process\n");
115
                        perror("O/S ERR");
116
                        printf("TEST FAILURE\n");
117
                        exit(EXIT_FAILURE);
118
                }
119
 
120
                if (pid) {
121
                        int     nr=-2, nw;
122
 
123
                        // We are the parent
124
                        close(childs_stdin[ 0]); // Close the read end
125
                        close(childs_stdout[1]); // Close the write end
126
 
127
                        char string[] = "This is a UART testing string\r\n";
128
                        char test[256];
129
 
130
                        nw = write(childs_stdin[1], string, strlen(string));
131
                        if (nw == (int)strlen(string)) {
132
                                int     rpos = 0;
133
                                test[0] = '\0';
134
                                while((rpos<nw)
135
                                        &&(0<(nr=read(childs_stdout[0],
136
                                                &test[rpos], strlen(string)-rpos))))
137
                                        rpos += nr;
138
 
139
                                nr = rpos;
140
                                if (rpos > 0)
141
                                        test[rpos] = '\0';
142
                                printf("Successfully read %d characters: %s\n", nr, test);
143
                        }
144
 
145
                        // We are done, kill our child if not already dead
146
                        kill(pid, SIGTERM);
147
 
148
                        if ((nr == nw)&&(nw == (int)strlen(string))
149
                                        &&(strcmp(test, string) == 0))
150
                                printf("SUCCESS!\n");
151
                        else
152
                                printf("TEST FAILED\n");
153
                } else {
154
                        close(childs_stdin[ 1]);
155
                        close(childs_stdout[0]);
156
                        close(STDIN_FILENO);
157
                        if (dup(childs_stdin[0]) < 0) {
158
                                fprintf(stderr, "ERR setting up child FD\n");
159
                                perror("O/S ERR");
160
                                exit(EXIT_FAILURE);
161
                        }
162
                        close(STDOUT_FILENO);
163
                        if (dup(childs_stdout[1]) < 0) {
164
                                fprintf(stderr, "ERR setting up child FD\n");
165
                                perror("O/S ERR");
166
                                exit(EXIT_FAILURE);
167
                        }
168
 
169
                        // UARTSIM(0) uses stdin and stdout for its FD's
170
                        uart = new UARTSIM(0);
171
                        uart->setup(tb.i_setup);
172
 
173
                        // Make sure we don't run longer than 4 seconds ...
174
                        time_t  start = time(NULL);
175
                        int     iterations_before_check = 2048;
176
                        bool    done = false;
177
 
178
                        for(int i=0; i<200000; i++) {
179
                                // Clear any initial break condition
180
                                tb.i_clk = 1;
181
                                tb.eval();
182
                                tb.i_clk = 0;
183
                                tb.eval();
184
 
185
                                tb.i_uart = 1;
186
                        }
187
 
188
                        while(!done) {
189
                                tb.i_clk = 1;
190
                                tb.eval();
191
                                tb.i_clk = 0;
192
                                tb.eval();
193
 
194
                                tb.i_uart = (*uart)(tb.o_uart);
195
 
196
                                /*
197
                                fprintf(stderr, "%02x:%02x (%02x/%d) %d,%d->%02x [%d/%d/%08x]\n",
198
                                        tb.v__DOT__head,
199
                                        tb.v__DOT__tail,
200
                                        tb.v__DOT__lineend,
201
                                        tb.v__DOT__run_tx,
202
                                        tb.v__DOT__tx_stb,
203
                                        tb.v__DOT__transmitter__DOT__r_busy,
204
                                        tb.v__DOT__tx_data & 0x0ff,
205
                                        tb.v__DOT__transmitter__DOT__state,
206
                                        tb.v__DOT__transmitter__DOT__zero_baud_counter,
207
                                        tb.v__DOT__transmitter__DOT__baud_counter);
208
                                */
209
 
210
                                if (iterations_before_check-- <= 0) {
211
                                        iterations_before_check = 2048;
212
                                        done = ((time(NULL)-start)>60);
213
                                        if (done)
214
                                        fprintf(stderr, "CHILD-TIMEOUT\n");
215
                                }
216
                        }
217
                }
218
        }
219
}

powered by: WebSVN 2.1.0

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