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

Subversion Repositories wbuart32

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

Go to most recent revision | 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
                pwr_reset = 1'b0;
95
 
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 2 dgisselq
        wire    rx_stb, rx_break, rx_perr, rx_ferr, rx_ignored;
106
        wire    [7:0]    rx_data;
107
 
108 15 dgisselq
`ifdef  USE_LITE_UART
109
        rxuartlite #(24'd868)
110
                receiver(i_clk, i_uart_rx, rx_stb, rx_data);
111
`else
112 5 dgisselq
        rxuart  receiver(i_clk, pwr_reset, i_setup, i_uart_rx, rx_stb, rx_data,
113 2 dgisselq
                        rx_break, rx_perr, rx_ferr, rx_ignored);
114 15 dgisselq
`endif
115 2 dgisselq
 
116
 
117 5 dgisselq
        // The next step in this process is to dump everything we read into a 
118
        // FIFO.  First step: writing into the FIFO.  Always write into FIFO
119
        // memory.  (The next step will step the memory address if rx_stb was
120
        // true ...)
121 2 dgisselq
        wire    [7:0]    nxt_head;
122
        assign  nxt_head = head + 8'h01;
123
        always @(posedge i_clk)
124
                buffer[head] <= rx_data;
125 5 dgisselq
 
126
        // Select where in our FIFO memory to write.  On reset, we clear the 
127
        // memory.  In all other cases/respects, we step the memory forward.
128
        //
129
        // However ... we won't step it forward IF ...
130
        //      rx_break        - we are in a BREAK condition on the line
131
        //              (i.e. ... it's disconnected)
132
        //      rx_perr         - We've seen a parity error
133
        //      rx_ferr         - Same thing for a frame error
134
        //      nxt_head != tail - If the FIFO is already full, we'll just drop
135
        //              this new value, rather than dumping random garbage
136
        //              from the FIFO until we go round again ...  i.e., we
137
        //              don't write on potential overflow.
138
        //
139
        // Adjusting this address will make certain that the next write to the
140
        // FIFO goes to the next address--since we've already written the FIFO
141
        // memory at this address.
142 2 dgisselq
        initial head= 8'h00;
143
        always @(posedge i_clk)
144
                if (pwr_reset)
145
                        head <= 8'h00;
146
                else if ((rx_stb)&&(!rx_break)&&(!rx_perr)&&(!rx_ferr)&&(nxt_head != tail))
147
                        head <= nxt_head;
148
 
149
        wire    [7:0]    nused;
150
        reg     [7:0]    lineend;
151
        reg             run_tx;
152
 
153 5 dgisselq
        // How much of the FIFO is in use?  head - tail.  What if they wrap
154
        // around?  Still: head-tail, but this time truncated to the number of
155
        // bits of interest.  It can never be negative ... so ... we're good,
156
        // this just measures that number.
157 2 dgisselq
        assign  nused = head-tail;
158
 
159 5 dgisselq
        // Here's the guts of the algorithm--setting run_tx.  Once set, the
160
        // buffer will flush.  Here, we set it on one of two conditions: 1)
161
        // a newline is received, or 2) the line is now longer than 80
162
        // characters.
163
        //
164
        // Once the line has ben transmitted (separate from emptying the buffer)
165
        // we stop transmitting.
166 2 dgisselq
        initial run_tx = 0;
167
        initial lineend = 0;
168
        always @(posedge i_clk)
169
                if (pwr_reset)
170
                begin
171
                        run_tx <= 1'b0;
172
                        lineend <= 8'h00;
173 5 dgisselq
                end else if(((rx_data == 8'h0a)||(rx_data == 8'hd))&&(rx_stb))
174 2 dgisselq
                begin
175 5 dgisselq
                        // Start transmitting once we get to either a newline
176
                        // or a carriage return character
177 2 dgisselq
                        lineend <= head+8'h1;
178
                        run_tx <= 1'b1;
179 5 dgisselq
                end else if ((!run_tx)&&(nused>8'd80))
180 2 dgisselq
                begin
181 5 dgisselq
                        // Start transmitting once we get to 80 chars
182 2 dgisselq
                        lineend <= head;
183
                        run_tx <= 1'b1;
184
                end else if (tail == lineend)
185 5 dgisselq
                        // Line buffer has been emptied
186 2 dgisselq
                        run_tx <= 1'b0;
187
 
188 5 dgisselq
        // Now ... let's deal with the transmitter
189 2 dgisselq
        wire    tx_break, tx_busy;
190
        assign  tx_break = 1'b0;
191
        reg     [7:0]    tx_data;
192
        reg             tx_stb;
193
 
194 5 dgisselq
        // When do we wish to transmit?
195
        //
196
        // Any time run_tx is true--but we'll give it an extra clock.
197 2 dgisselq
        initial tx_stb = 1'b0;
198
        always @(posedge i_clk)
199
                tx_stb <= run_tx;
200 5 dgisselq
 
201
        // We'll transmit the data from our FIFO from ... wherever our tail
202
        // is pointed.
203
        always @(posedge i_clk)
204
                tx_data <= buffer[tail];
205
 
206
        // We increment the pointer to where we read from any time 1) we are
207
        // requesting to transmit a character, and 2) the transmitter was not
208
        // busy and thus accepted our request.  At that time, increment the
209
        // pointer, and we'll be ready for another round.
210 2 dgisselq
        initial tail = 8'h00;
211
        always @(posedge i_clk)
212
                if(pwr_reset)
213
                        tail <= 8'h00;
214
                else if ((tx_stb)&&(!tx_busy))
215
                        tail <= tail + 8'h01;
216 10 dgisselq
 
217
        // Bypass any hardwaare flow control
218 15 dgisselq
        wire    cts_n;
219
        assign  cts_n = 1'b0;
220 10 dgisselq
 
221 15 dgisselq
`ifdef  USE_LITE_UART
222
        txuartlite #(24'd868)
223
                transmitter(i_clk, tx_stb, tx_data, o_uart_tx, tx_busy);
224
`else
225 2 dgisselq
        txuart  transmitter(i_clk, pwr_reset, i_setup, tx_break,
226 15 dgisselq
                        tx_stb, tx_data, cts_n, o_uart_tx, tx_busy);
227
`endif
228 2 dgisselq
 
229
endmodule

powered by: WebSVN 2.1.0

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