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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [verilog/] [linetest.v] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    linetest.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     To test that the txuart and rxuart modules work properly, by
8
//              buffering one line's worth of input, and then piping that line
9
//      to the transmitter while (possibly) receiving a new line.
10
//
11 5 dgisselq
//      With some modifications (discussed below), this RTL should be able to
12
//      run as a top-level testing file, requiring only the transmit and receive
13
//      UART pins and the clock to work.
14
//
15 2 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
16
//              Gisselquist Technology, LLC
17
//
18
////////////////////////////////////////////////////////////////////////////////
19
//
20
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
21
//
22
// 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 13 dgisselq
// One issue with the design is how to set the values of the setup register.
45
// (*This is a comment, not a verilator attribute ... )  Verilator needs to
46
// know/set those values in order to work.  However, this design can also be
47
// used as a stand-alone top level configuration file.  In this latter case,
48
// the setup register needs to be set internal to the file.  Here, we use
49
// OPT_STANDALONE to distinguish between the two.  If set, the file runs under
50
// (* Another comment still ...) Verilator and we need to get i_setup from the
51
// external environment.  If not, it must be set internally.
52 5 dgisselq
//
53 13 dgisselq
`ifndef VERILATOR
54
`define OPT_STANDALONE
55
`endif
56 5 dgisselq
//
57 15 dgisselq
//
58
// Two versions of the UART can be found in the rtl directory: a full featured
59
// UART, and a LITE UART that only handles 8N1 -- no break sending, break
60
// detection, parity error detection, etc.  If we set USE_LITE_UART here, those
61
// simplified UART modules will be used.
62
//
63
// `define      USE_LITE_UART
64
//
65
//
66 5 dgisselq
module  linetest(i_clk,
67
`ifndef OPT_STANDALONE
68
                        i_setup,
69
`endif
70
                        i_uart_rx, o_uart_tx);
71 2 dgisselq
        input           i_clk;
72 5 dgisselq
`ifndef OPT_STANDALONE
73 10 dgisselq
        input   [30:0]   i_setup;
74 5 dgisselq
`endif
75
        input           i_uart_rx;
76
        output  wire    o_uart_tx;
77 2 dgisselq
 
78 5 dgisselq
        // If i_setup isnt set up as an input parameter, it needs to be set.
79
        // We do so here, to a setting appropriate to create a 115200 Baud
80
        // comms system from a 100MHz clock.  This also sets us to an 8-bit
81
        // data word, 1-stop bit, and no parity.
82
`ifdef  OPT_STANDALONE
83 13 dgisselq
        wire    [30:0]   i_setup;
84 10 dgisselq
        assign          i_setup = 31'd868;      // 115200 Baud, if clk @ 100MHz
85 5 dgisselq
`endif
86
 
87 2 dgisselq
        reg     [7:0]    buffer  [0:255];
88
        reg     [7:0]    head, tail;
89
 
90 5 dgisselq
        // Create a reset line that will always be true on a power on reset
91 2 dgisselq
        reg     pwr_reset;
92
        initial pwr_reset = 1'b1;
93
        always @(posedge i_clk)
94 18 dgisselq
                pwr_reset <= 1'b0;
95 2 dgisselq
 
96 5 dgisselq
 
97
 
98
        // The UART Receiver
99
        //
100
        // This is where everything begins, by reading data from the UART.
101
        //
102
        // Data (rx_data) is present when rx_stb is true.  Any parity or
103
        // frame errors will also be valid at that time.  Finally, we'll ignore
104
        // errors, and even the clocked uart input distributed from here.
105 18 dgisselq
        wire    rx_stb, rx_break, rx_perr, rx_ferr;
106
        /* verilator lint_off UNUSED */
107
        wire    rx_ignored;
108
        /* verilator lint_on UNUSED */
109 2 dgisselq
        wire    [7:0]    rx_data;
110
 
111 15 dgisselq
`ifdef  USE_LITE_UART
112
        rxuartlite #(24'd868)
113
                receiver(i_clk, i_uart_rx, rx_stb, rx_data);
114
`else
115 5 dgisselq
        rxuart  receiver(i_clk, pwr_reset, i_setup, i_uart_rx, rx_stb, rx_data,
116 2 dgisselq
                        rx_break, rx_perr, rx_ferr, rx_ignored);
117 15 dgisselq
`endif
118 2 dgisselq
 
119
 
120 5 dgisselq
        // The next step in this process is to dump everything we read into a 
121
        // FIFO.  First step: writing into the FIFO.  Always write into FIFO
122
        // memory.  (The next step will step the memory address if rx_stb was
123
        // true ...)
124 2 dgisselq
        wire    [7:0]    nxt_head;
125
        assign  nxt_head = head + 8'h01;
126
        always @(posedge i_clk)
127
                buffer[head] <= rx_data;
128 5 dgisselq
 
129
        // Select where in our FIFO memory to write.  On reset, we clear the 
130
        // memory.  In all other cases/respects, we step the memory forward.
131
        //
132
        // However ... we won't step it forward IF ...
133
        //      rx_break        - we are in a BREAK condition on the line
134
        //              (i.e. ... it's disconnected)
135
        //      rx_perr         - We've seen a parity error
136
        //      rx_ferr         - Same thing for a frame error
137
        //      nxt_head != tail - If the FIFO is already full, we'll just drop
138
        //              this new value, rather than dumping random garbage
139
        //              from the FIFO until we go round again ...  i.e., we
140
        //              don't write on potential overflow.
141
        //
142
        // Adjusting this address will make certain that the next write to the
143
        // FIFO goes to the next address--since we've already written the FIFO
144
        // memory at this address.
145 2 dgisselq
        initial head= 8'h00;
146
        always @(posedge i_clk)
147
                if (pwr_reset)
148
                        head <= 8'h00;
149
                else if ((rx_stb)&&(!rx_break)&&(!rx_perr)&&(!rx_ferr)&&(nxt_head != tail))
150
                        head <= nxt_head;
151
 
152
        wire    [7:0]    nused;
153
        reg     [7:0]    lineend;
154
        reg             run_tx;
155
 
156 5 dgisselq
        // How much of the FIFO is in use?  head - tail.  What if they wrap
157
        // around?  Still: head-tail, but this time truncated to the number of
158
        // bits of interest.  It can never be negative ... so ... we're good,
159
        // this just measures that number.
160 2 dgisselq
        assign  nused = head-tail;
161
 
162 5 dgisselq
        // Here's the guts of the algorithm--setting run_tx.  Once set, the
163
        // buffer will flush.  Here, we set it on one of two conditions: 1)
164
        // a newline is received, or 2) the line is now longer than 80
165
        // characters.
166
        //
167
        // Once the line has ben transmitted (separate from emptying the buffer)
168
        // we stop transmitting.
169 2 dgisselq
        initial run_tx = 0;
170
        initial lineend = 0;
171
        always @(posedge i_clk)
172
                if (pwr_reset)
173
                begin
174
                        run_tx <= 1'b0;
175
                        lineend <= 8'h00;
176 5 dgisselq
                end else if(((rx_data == 8'h0a)||(rx_data == 8'hd))&&(rx_stb))
177 2 dgisselq
                begin
178 5 dgisselq
                        // Start transmitting once we get to either a newline
179
                        // or a carriage return character
180 2 dgisselq
                        lineend <= head+8'h1;
181
                        run_tx <= 1'b1;
182 5 dgisselq
                end else if ((!run_tx)&&(nused>8'd80))
183 2 dgisselq
                begin
184 5 dgisselq
                        // Start transmitting once we get to 80 chars
185 2 dgisselq
                        lineend <= head;
186
                        run_tx <= 1'b1;
187
                end else if (tail == lineend)
188 5 dgisselq
                        // Line buffer has been emptied
189 2 dgisselq
                        run_tx <= 1'b0;
190
 
191 5 dgisselq
        // Now ... let's deal with the transmitter
192 2 dgisselq
        wire    tx_break, tx_busy;
193
        assign  tx_break = 1'b0;
194
        reg     [7:0]    tx_data;
195
        reg             tx_stb;
196
 
197 5 dgisselq
        // When do we wish to transmit?
198
        //
199
        // Any time run_tx is true--but we'll give it an extra clock.
200 2 dgisselq
        initial tx_stb = 1'b0;
201
        always @(posedge i_clk)
202
                tx_stb <= run_tx;
203 5 dgisselq
 
204
        // We'll transmit the data from our FIFO from ... wherever our tail
205
        // is pointed.
206
        always @(posedge i_clk)
207
                tx_data <= buffer[tail];
208
 
209
        // We increment the pointer to where we read from any time 1) we are
210
        // requesting to transmit a character, and 2) the transmitter was not
211
        // busy and thus accepted our request.  At that time, increment the
212
        // pointer, and we'll be ready for another round.
213 2 dgisselq
        initial tail = 8'h00;
214
        always @(posedge i_clk)
215
                if(pwr_reset)
216
                        tail <= 8'h00;
217
                else if ((tx_stb)&&(!tx_busy))
218
                        tail <= tail + 8'h01;
219 10 dgisselq
 
220
        // Bypass any hardwaare flow control
221 15 dgisselq
        wire    cts_n;
222
        assign  cts_n = 1'b0;
223 10 dgisselq
 
224 15 dgisselq
`ifdef  USE_LITE_UART
225
        txuartlite #(24'd868)
226
                transmitter(i_clk, tx_stb, tx_data, o_uart_tx, tx_busy);
227
`else
228 2 dgisselq
        txuart  transmitter(i_clk, pwr_reset, i_setup, tx_break,
229 15 dgisselq
                        tx_stb, tx_data, cts_n, o_uart_tx, tx_busy);
230
`endif
231 2 dgisselq
 
232
endmodule

powered by: WebSVN 2.1.0

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