1 |
5 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: helloworld.v
|
4 |
|
|
//
|
5 |
|
|
// Project: wbuart32, a full featured UART with simulator
|
6 |
|
|
//
|
7 |
|
|
// Purpose: To create a *very* simple UART test program, which can be used
|
8 |
|
|
// as the top level design file of any FPGA program.
|
9 |
|
|
//
|
10 |
|
|
// With some modifications (discussed below), this RTL should be able to
|
11 |
|
|
// run as a top-level testing file, requiring only the UART and clock pin
|
12 |
|
|
// to work.
|
13 |
|
|
//
|
14 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
15 |
|
|
// Gisselquist Technology, LLC
|
16 |
|
|
//
|
17 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
18 |
|
|
//
|
19 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
20 |
|
|
//
|
21 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
22 |
|
|
// modify it under the terms of the GNU General Public License as published
|
23 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
24 |
|
|
// your option) any later version.
|
25 |
|
|
//
|
26 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
27 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
28 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
29 |
|
|
// for more details.
|
30 |
|
|
//
|
31 |
|
|
// You should have received a copy of the GNU General Public License along
|
32 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
33 |
|
|
// target there if the PDF file isn't present.) If not, see
|
34 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
35 |
|
|
//
|
36 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
37 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
38 |
|
|
//
|
39 |
|
|
//
|
40 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
//
|
42 |
|
|
//
|
43 |
13 |
dgisselq |
// One issue with the design is how to set the values of the setup register.
|
44 |
|
|
// (*This is a comment, not a verilator attribute ... ) Verilator needs to
|
45 |
|
|
// know/set those values in order to work. However, this design can also be
|
46 |
|
|
// used as a stand-alone top level configuration file. In this latter case,
|
47 |
|
|
// the setup register needs to be set internal to the file. Here, we use
|
48 |
|
|
// OPT_STANDALONE to distinguish between the two. If set, the file runs under
|
49 |
|
|
// (* Another comment still ...) Verilator and we need to get i_setup from the
|
50 |
|
|
// external environment. If not, it must be set internally.
|
51 |
5 |
dgisselq |
//
|
52 |
13 |
dgisselq |
`ifndef VERILATOR
|
53 |
|
|
`define OPT_STANDALONE
|
54 |
|
|
`endif
|
55 |
5 |
dgisselq |
//
|
56 |
15 |
dgisselq |
//
|
57 |
|
|
// Two versions of the UART can be found in the rtl directory: a full featured
|
58 |
|
|
// UART, and a LITE UART that only handles 8N1 -- no break sending, break
|
59 |
|
|
// detection, parity error detection, etc. If we set USE_LITE_UART here, those
|
60 |
|
|
// simplified UART modules will be used.
|
61 |
|
|
//
|
62 |
|
|
// `define USE_LITE_UART
|
63 |
|
|
//
|
64 |
|
|
//
|
65 |
5 |
dgisselq |
module helloworld(i_clk,
|
66 |
|
|
`ifndef OPT_STANDALONE
|
67 |
|
|
i_setup,
|
68 |
|
|
`endif
|
69 |
|
|
o_uart_tx);
|
70 |
|
|
input i_clk;
|
71 |
|
|
output wire o_uart_tx;
|
72 |
|
|
|
73 |
13 |
dgisselq |
// Here we set i_setup to something appropriate to create a 115200 Baud
|
74 |
|
|
// UART system from a 100MHz clock. This also sets us to an 8-bit data
|
75 |
|
|
// word, 1-stop bit, and no parity. This will be overwritten by
|
76 |
|
|
// i_setup, but at least it gives us something to start with/from.
|
77 |
|
|
parameter INITIAL_UART_SETUP = 31'd868;
|
78 |
|
|
|
79 |
|
|
// The i_setup wires are input when run under Verilator, but need to
|
80 |
|
|
// be set internally if this is going to run as a standalone top level
|
81 |
|
|
// test configuration.
|
82 |
5 |
dgisselq |
`ifdef OPT_STANDALONE
|
83 |
10 |
dgisselq |
wire [30:0] i_setup;
|
84 |
13 |
dgisselq |
assign i_setup = INITIAL_UART_SETUP;
|
85 |
|
|
`else
|
86 |
|
|
input [30:0] i_setup;
|
87 |
5 |
dgisselq |
`endif
|
88 |
|
|
|
89 |
|
|
reg pwr_reset;
|
90 |
|
|
initial pwr_reset = 1'b1;
|
91 |
|
|
always @(posedge i_clk)
|
92 |
|
|
pwr_reset <= 1'b0;
|
93 |
|
|
|
94 |
|
|
reg [7:0] message [0:15];
|
95 |
|
|
|
96 |
|
|
initial begin
|
97 |
|
|
message[ 0] = "H";
|
98 |
|
|
message[ 1] = "e";
|
99 |
|
|
message[ 2] = "l";
|
100 |
|
|
message[ 3] = "l";
|
101 |
|
|
message[ 4] = "o";
|
102 |
|
|
message[ 5] = ",";
|
103 |
|
|
message[ 6] = " ";
|
104 |
|
|
message[ 7] = "W";
|
105 |
|
|
message[ 8] = "o";
|
106 |
|
|
message[ 9] = "r";
|
107 |
|
|
message[10] = "l";
|
108 |
|
|
message[11] = "d";
|
109 |
|
|
message[12] = "!";
|
110 |
|
|
message[13] = " ";
|
111 |
|
|
message[14] = "\r";
|
112 |
|
|
message[15] = "\n";
|
113 |
|
|
end
|
114 |
|
|
|
115 |
|
|
reg [27:0] counter;
|
116 |
|
|
initial counter = 28'hffffff0;
|
117 |
|
|
always @(posedge i_clk)
|
118 |
|
|
counter <= counter + 1'b1;
|
119 |
|
|
|
120 |
|
|
wire tx_break, tx_busy;
|
121 |
|
|
reg tx_stb;
|
122 |
|
|
reg [3:0] tx_index;
|
123 |
|
|
reg [7:0] tx_data;
|
124 |
|
|
|
125 |
|
|
assign tx_break = 1'b0;
|
126 |
|
|
|
127 |
|
|
initial tx_index = 4'h0;
|
128 |
|
|
always @(posedge i_clk)
|
129 |
|
|
if ((tx_stb)&&(!tx_busy))
|
130 |
|
|
tx_index <= tx_index + 1'b1;
|
131 |
|
|
always @(posedge i_clk)
|
132 |
|
|
tx_data <= message[tx_index];
|
133 |
|
|
|
134 |
|
|
initial tx_stb = 1'b0;
|
135 |
|
|
always @(posedge i_clk)
|
136 |
|
|
if (&counter)
|
137 |
|
|
tx_stb <= 1'b1;
|
138 |
|
|
else if ((tx_stb)&&(!tx_busy)&&(tx_index==4'hf))
|
139 |
|
|
tx_stb <= 1'b0;
|
140 |
|
|
|
141 |
10 |
dgisselq |
// Bypass any hardware flow control
|
142 |
15 |
dgisselq |
wire cts_n;
|
143 |
|
|
assign cts_n = 1'b0;
|
144 |
10 |
dgisselq |
|
145 |
15 |
dgisselq |
`ifdef USE_LITE_UART
|
146 |
|
|
txuartlite
|
147 |
|
|
#(24'd868)
|
148 |
|
|
transmitter(i_clk, tx_stb, tx_data, o_uart_tx, tx_busy);
|
149 |
|
|
`else
|
150 |
5 |
dgisselq |
txuart transmitter(i_clk, pwr_reset, i_setup, tx_break,
|
151 |
15 |
dgisselq |
tx_stb, tx_data, cts_n, o_uart_tx, tx_busy);
|
152 |
|
|
`endif
|
153 |
5 |
dgisselq |
|
154 |
|
|
endmodule
|