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