Line 6... |
Line 6... |
//
|
//
|
// Purpose: To test that the txuart and rxuart modules work properly, by
|
// Purpose: To test that the txuart and rxuart modules work properly, by
|
// buffering one line's worth of input, and then piping that line
|
// buffering one line's worth of input, and then piping that line
|
// to the transmitter while (possibly) receiving a new line.
|
// to the transmitter while (possibly) receiving a new line.
|
//
|
//
|
|
// With some modifications (discussed below), this RTL should be able to
|
|
// run as a top-level testing file, requiring only the transmit and receive
|
|
// UART pins and the clock to work.
|
|
//
|
// Creator: Dan Gisselquist, Ph.D.
|
// Creator: Dan Gisselquist, Ph.D.
|
// Gisselquist Technology, LLC
|
// Gisselquist Technology, LLC
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
Line 35... |
Line 39... |
//
|
//
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
//
|
//
|
module linetest(i_clk, i_setup, i_uart, o_uart);
|
// Uncomment the next line if you want this program to work as a standalone
|
|
// (not verilated) RTL "program" to test your UART. You'll also need to set
|
|
// your setup condition properly, though. I recommend setting it to the
|
|
// ratio of your onboard clock to your desired baud rate. For more information
|
|
// about how to set this, please see the specification.
|
|
//
|
|
// `define OPT_STANDALONE
|
|
//
|
|
module linetest(i_clk,
|
|
`ifndef OPT_STANDALONE
|
|
i_setup,
|
|
`endif
|
|
i_uart_rx, o_uart_tx);
|
input i_clk;
|
input i_clk;
|
|
`ifndef OPT_STANDALONE
|
input [29:0] i_setup;
|
input [29:0] i_setup;
|
input i_uart;
|
`endif
|
output wire o_uart;
|
input i_uart_rx;
|
|
output wire o_uart_tx;
|
|
|
|
// If i_setup isnt set up as an input parameter, it needs to be set.
|
|
// We do so here, to a setting appropriate to create a 115200 Baud
|
|
// comms system from a 100MHz clock. This also sets us to an 8-bit
|
|
// data word, 1-stop bit, and no parity.
|
|
`ifdef OPT_STANDALONE
|
|
wire [29:0] i_setup;
|
|
assign i_setup = 30'd868; // 115200 Baud, if clk @ 100MHz
|
|
`endif
|
|
|
reg [7:0] buffer [0:255];
|
reg [7:0] buffer [0:255];
|
reg [7:0] head, tail;
|
reg [7:0] head, tail;
|
|
|
|
// Create a reset line that will always be true on a power on reset
|
reg pwr_reset;
|
reg pwr_reset;
|
initial pwr_reset = 1'b1;
|
initial pwr_reset = 1'b1;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
pwr_reset = 1'b0;
|
pwr_reset = 1'b0;
|
|
|
|
|
|
|
|
// The UART Receiver
|
|
//
|
|
// This is where everything begins, by reading data from the UART.
|
|
//
|
|
// Data (rx_data) is present when rx_stb is true. Any parity or
|
|
// frame errors will also be valid at that time. Finally, we'll ignore
|
|
// errors, and even the clocked uart input distributed from here.
|
wire rx_stb, rx_break, rx_perr, rx_ferr, rx_ignored;
|
wire rx_stb, rx_break, rx_perr, rx_ferr, rx_ignored;
|
wire [7:0] rx_data;
|
wire [7:0] rx_data;
|
|
|
rxuart receiver(i_clk, pwr_reset, i_setup, i_uart, rx_stb, rx_data,
|
rxuart receiver(i_clk, pwr_reset, i_setup, i_uart_rx, rx_stb, rx_data,
|
rx_break, rx_perr, rx_ferr, rx_ignored);
|
rx_break, rx_perr, rx_ferr, rx_ignored);
|
|
|
|
|
|
// The next step in this process is to dump everything we read into a
|
|
// FIFO. First step: writing into the FIFO. Always write into FIFO
|
|
// memory. (The next step will step the memory address if rx_stb was
|
|
// true ...)
|
wire [7:0] nxt_head;
|
wire [7:0] nxt_head;
|
assign nxt_head = head + 8'h01;
|
assign nxt_head = head + 8'h01;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
buffer[head] <= rx_data;
|
buffer[head] <= rx_data;
|
|
|
|
// Select where in our FIFO memory to write. On reset, we clear the
|
|
// memory. In all other cases/respects, we step the memory forward.
|
|
//
|
|
// However ... we won't step it forward IF ...
|
|
// rx_break - we are in a BREAK condition on the line
|
|
// (i.e. ... it's disconnected)
|
|
// rx_perr - We've seen a parity error
|
|
// rx_ferr - Same thing for a frame error
|
|
// nxt_head != tail - If the FIFO is already full, we'll just drop
|
|
// this new value, rather than dumping random garbage
|
|
// from the FIFO until we go round again ... i.e., we
|
|
// don't write on potential overflow.
|
|
//
|
|
// Adjusting this address will make certain that the next write to the
|
|
// FIFO goes to the next address--since we've already written the FIFO
|
|
// memory at this address.
|
initial head= 8'h00;
|
initial head= 8'h00;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
if (pwr_reset)
|
if (pwr_reset)
|
head <= 8'h00;
|
head <= 8'h00;
|
else if ((rx_stb)&&(!rx_break)&&(!rx_perr)&&(!rx_ferr)&&(nxt_head != tail))
|
else if ((rx_stb)&&(!rx_break)&&(!rx_perr)&&(!rx_ferr)&&(nxt_head != tail))
|
Line 71... |
Line 129... |
|
|
wire [7:0] nused;
|
wire [7:0] nused;
|
reg [7:0] lineend;
|
reg [7:0] lineend;
|
reg run_tx;
|
reg run_tx;
|
|
|
|
// How much of the FIFO is in use? head - tail. What if they wrap
|
|
// around? Still: head-tail, but this time truncated to the number of
|
|
// bits of interest. It can never be negative ... so ... we're good,
|
|
// this just measures that number.
|
assign nused = head-tail;
|
assign nused = head-tail;
|
|
|
|
// Here's the guts of the algorithm--setting run_tx. Once set, the
|
|
// buffer will flush. Here, we set it on one of two conditions: 1)
|
|
// a newline is received, or 2) the line is now longer than 80
|
|
// characters.
|
|
//
|
|
// Once the line has ben transmitted (separate from emptying the buffer)
|
|
// we stop transmitting.
|
initial run_tx = 0;
|
initial run_tx = 0;
|
initial lineend = 0;
|
initial lineend = 0;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
if (pwr_reset)
|
if (pwr_reset)
|
begin
|
begin
|
run_tx <= 1'b0;
|
run_tx <= 1'b0;
|
lineend <= 8'h00;
|
lineend <= 8'h00;
|
end else if ((rx_data == 8'h0a)&&(rx_stb))
|
end else if(((rx_data == 8'h0a)||(rx_data == 8'hd))&&(rx_stb))
|
begin
|
begin
|
|
// Start transmitting once we get to either a newline
|
|
// or a carriage return character
|
lineend <= head+8'h1;
|
lineend <= head+8'h1;
|
run_tx <= 1'b1;
|
run_tx <= 1'b1;
|
end else if ((!run_tx)&&(nused>8'd80)&&(head != tail))
|
end else if ((!run_tx)&&(nused>8'd80))
|
begin
|
begin
|
|
// Start transmitting once we get to 80 chars
|
lineend <= head;
|
lineend <= head;
|
run_tx <= 1'b1;
|
run_tx <= 1'b1;
|
end else if (tail == lineend)
|
end else if (tail == lineend)
|
|
// Line buffer has been emptied
|
run_tx <= 1'b0;
|
run_tx <= 1'b0;
|
|
|
|
// Now ... let's deal with the transmitter
|
wire tx_break, tx_busy;
|
wire tx_break, tx_busy;
|
assign tx_break = 1'b0;
|
assign tx_break = 1'b0;
|
reg [7:0] tx_data;
|
reg [7:0] tx_data;
|
reg tx_stb;
|
reg tx_stb;
|
|
|
always @(posedge i_clk)
|
// When do we wish to transmit?
|
tx_data <= buffer[tail];
|
//
|
|
// Any time run_tx is true--but we'll give it an extra clock.
|
initial tx_stb = 1'b0;
|
initial tx_stb = 1'b0;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
tx_stb <= run_tx;
|
tx_stb <= run_tx;
|
|
|
|
// We'll transmit the data from our FIFO from ... wherever our tail
|
|
// is pointed.
|
|
always @(posedge i_clk)
|
|
tx_data <= buffer[tail];
|
|
|
|
// We increment the pointer to where we read from any time 1) we are
|
|
// requesting to transmit a character, and 2) the transmitter was not
|
|
// busy and thus accepted our request. At that time, increment the
|
|
// pointer, and we'll be ready for another round.
|
initial tail = 8'h00;
|
initial tail = 8'h00;
|
always @(posedge i_clk)
|
always @(posedge i_clk)
|
if(pwr_reset)
|
if(pwr_reset)
|
tail <= 8'h00;
|
tail <= 8'h00;
|
else if ((tx_stb)&&(!tx_busy))
|
else if ((tx_stb)&&(!tx_busy))
|
tail <= tail + 8'h01;
|
tail <= tail + 8'h01;
|
|
|
txuart transmitter(i_clk, pwr_reset, i_setup, tx_break,
|
txuart transmitter(i_clk, pwr_reset, i_setup, tx_break,
|
tx_stb, tx_data, o_uart, tx_busy);
|
tx_stb, tx_data, o_uart_tx, tx_busy);
|
|
|
endmodule
|
endmodule
|
|
|
No newline at end of file
|
No newline at end of file
|