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

Subversion Repositories wbuart32

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

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
module  linetest(i_clk,
58
`ifndef OPT_STANDALONE
59
                        i_setup,
60
`endif
61
                        i_uart_rx, o_uart_tx);
62 2 dgisselq
        input           i_clk;
63 5 dgisselq
`ifndef OPT_STANDALONE
64 10 dgisselq
        input   [30:0]   i_setup;
65 5 dgisselq
`endif
66
        input           i_uart_rx;
67
        output  wire    o_uart_tx;
68 2 dgisselq
 
69 5 dgisselq
        // If i_setup isnt set up as an input parameter, it needs to be set.
70
        // We do so here, to a setting appropriate to create a 115200 Baud
71
        // comms system from a 100MHz clock.  This also sets us to an 8-bit
72
        // data word, 1-stop bit, and no parity.
73
`ifdef  OPT_STANDALONE
74 13 dgisselq
        wire    [30:0]   i_setup;
75 10 dgisselq
        assign          i_setup = 31'd868;      // 115200 Baud, if clk @ 100MHz
76 5 dgisselq
`endif
77
 
78 2 dgisselq
        reg     [7:0]    buffer  [0:255];
79
        reg     [7:0]    head, tail;
80
 
81 5 dgisselq
        // Create a reset line that will always be true on a power on reset
82 2 dgisselq
        reg     pwr_reset;
83
        initial pwr_reset = 1'b1;
84
        always @(posedge i_clk)
85
                pwr_reset = 1'b0;
86
 
87 5 dgisselq
 
88
 
89
        // The UART Receiver
90
        //
91
        // This is where everything begins, by reading data from the UART.
92
        //
93
        // Data (rx_data) is present when rx_stb is true.  Any parity or
94
        // frame errors will also be valid at that time.  Finally, we'll ignore
95
        // errors, and even the clocked uart input distributed from here.
96 2 dgisselq
        wire    rx_stb, rx_break, rx_perr, rx_ferr, rx_ignored;
97
        wire    [7:0]    rx_data;
98
 
99 5 dgisselq
        rxuart  receiver(i_clk, pwr_reset, i_setup, i_uart_rx, rx_stb, rx_data,
100 2 dgisselq
                        rx_break, rx_perr, rx_ferr, rx_ignored);
101
 
102
 
103 5 dgisselq
        // The next step in this process is to dump everything we read into a 
104
        // FIFO.  First step: writing into the FIFO.  Always write into FIFO
105
        // memory.  (The next step will step the memory address if rx_stb was
106
        // true ...)
107 2 dgisselq
        wire    [7:0]    nxt_head;
108
        assign  nxt_head = head + 8'h01;
109
        always @(posedge i_clk)
110
                buffer[head] <= rx_data;
111 5 dgisselq
 
112
        // Select where in our FIFO memory to write.  On reset, we clear the 
113
        // memory.  In all other cases/respects, we step the memory forward.
114
        //
115
        // However ... we won't step it forward IF ...
116
        //      rx_break        - we are in a BREAK condition on the line
117
        //              (i.e. ... it's disconnected)
118
        //      rx_perr         - We've seen a parity error
119
        //      rx_ferr         - Same thing for a frame error
120
        //      nxt_head != tail - If the FIFO is already full, we'll just drop
121
        //              this new value, rather than dumping random garbage
122
        //              from the FIFO until we go round again ...  i.e., we
123
        //              don't write on potential overflow.
124
        //
125
        // Adjusting this address will make certain that the next write to the
126
        // FIFO goes to the next address--since we've already written the FIFO
127
        // memory at this address.
128 2 dgisselq
        initial head= 8'h00;
129
        always @(posedge i_clk)
130
                if (pwr_reset)
131
                        head <= 8'h00;
132
                else if ((rx_stb)&&(!rx_break)&&(!rx_perr)&&(!rx_ferr)&&(nxt_head != tail))
133
                        head <= nxt_head;
134
 
135
        wire    [7:0]    nused;
136
        reg     [7:0]    lineend;
137
        reg             run_tx;
138
 
139 5 dgisselq
        // How much of the FIFO is in use?  head - tail.  What if they wrap
140
        // around?  Still: head-tail, but this time truncated to the number of
141
        // bits of interest.  It can never be negative ... so ... we're good,
142
        // this just measures that number.
143 2 dgisselq
        assign  nused = head-tail;
144
 
145 5 dgisselq
        // Here's the guts of the algorithm--setting run_tx.  Once set, the
146
        // buffer will flush.  Here, we set it on one of two conditions: 1)
147
        // a newline is received, or 2) the line is now longer than 80
148
        // characters.
149
        //
150
        // Once the line has ben transmitted (separate from emptying the buffer)
151
        // we stop transmitting.
152 2 dgisselq
        initial run_tx = 0;
153
        initial lineend = 0;
154
        always @(posedge i_clk)
155
                if (pwr_reset)
156
                begin
157
                        run_tx <= 1'b0;
158
                        lineend <= 8'h00;
159 5 dgisselq
                end else if(((rx_data == 8'h0a)||(rx_data == 8'hd))&&(rx_stb))
160 2 dgisselq
                begin
161 5 dgisselq
                        // Start transmitting once we get to either a newline
162
                        // or a carriage return character
163 2 dgisselq
                        lineend <= head+8'h1;
164
                        run_tx <= 1'b1;
165 5 dgisselq
                end else if ((!run_tx)&&(nused>8'd80))
166 2 dgisselq
                begin
167 5 dgisselq
                        // Start transmitting once we get to 80 chars
168 2 dgisselq
                        lineend <= head;
169
                        run_tx <= 1'b1;
170
                end else if (tail == lineend)
171 5 dgisselq
                        // Line buffer has been emptied
172 2 dgisselq
                        run_tx <= 1'b0;
173
 
174 5 dgisselq
        // Now ... let's deal with the transmitter
175 2 dgisselq
        wire    tx_break, tx_busy;
176
        assign  tx_break = 1'b0;
177
        reg     [7:0]    tx_data;
178
        reg             tx_stb;
179
 
180 5 dgisselq
        // When do we wish to transmit?
181
        //
182
        // Any time run_tx is true--but we'll give it an extra clock.
183 2 dgisselq
        initial tx_stb = 1'b0;
184
        always @(posedge i_clk)
185
                tx_stb <= run_tx;
186 5 dgisselq
 
187
        // We'll transmit the data from our FIFO from ... wherever our tail
188
        // is pointed.
189
        always @(posedge i_clk)
190
                tx_data <= buffer[tail];
191
 
192
        // We increment the pointer to where we read from any time 1) we are
193
        // requesting to transmit a character, and 2) the transmitter was not
194
        // busy and thus accepted our request.  At that time, increment the
195
        // pointer, and we'll be ready for another round.
196 2 dgisselq
        initial tail = 8'h00;
197
        always @(posedge i_clk)
198
                if(pwr_reset)
199
                        tail <= 8'h00;
200
                else if ((tx_stb)&&(!tx_busy))
201
                        tail <= tail + 8'h01;
202 10 dgisselq
 
203
        // Bypass any hardwaare flow control
204
        wire    rts;
205
        assign  rts = 1'b1;
206
 
207 2 dgisselq
        txuart  transmitter(i_clk, pwr_reset, i_setup, tx_break,
208 10 dgisselq
                        tx_stb, tx_data, rts, o_uart_tx, tx_busy);
209 2 dgisselq
 
210
endmodule

powered by: WebSVN 2.1.0

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