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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [verilog/] [speechfifo.v] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    speechfifo.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     To test/demonstrate/prove the wishbone access to the FIFO'd
8
//              UART via sending more information than the FIFO can hold,
9
//      and then verifying that this was the value received.
10
//
11
//      To do this, we "borrow" a copy of Abraham Lincolns Gettysburg address,
12
//      make that the FIFO isn't large enough to hold it, and then try
13
//      to send this address every couple of minutes.
14
//
15
//      With some minor modifications (discussed below), this RTL should be
16
//      able to be run as a top-level testing file, requiring only that the
17
//      clock and the transmit UART pins be working.
18
//
19
// Creator:     Dan Gisselquist, Ph.D.
20
//              Gisselquist Technology, LLC
21
//
22
////////////////////////////////////////////////////////////////////////////////
23
//
24 18 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
25 5 dgisselq
//
26
// This program is free software (firmware): you can redistribute it and/or
27
// modify it under the terms of  the GNU General Public License as published
28
// by the Free Software Foundation, either version 3 of the License, or (at
29
// your option) any later version.
30
//
31
// This program is distributed in the hope that it will be useful, but WITHOUT
32
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
33
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34
// for more details.
35
//
36
// You should have received a copy of the GNU General Public License along
37
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
38
// target there if the PDF file isn't present.)  If not, see
39
// <http://www.gnu.org/licenses/> for a copy.
40
//
41
// License:     GPL, v3, as defined and found on www.gnu.org,
42
//              http://www.gnu.org/licenses/gpl.html
43
//
44
//
45
////////////////////////////////////////////////////////////////////////////////
46
//
47
//
48 10 dgisselq
// One issue with the design is how to set the values of the setup register.
49
// (*This is a comment, not a verilator attribute ... )  Verilator needs to
50
// know/set those values in order to work.  However, this design can also be
51
// used as a stand-alone top level configuration file.  In this latter case,
52
// the setup register needs to be set internal to the file.  Here, we use
53
// OPT_STANDALONE to distinguish between the two.  If set, the file runs under
54
// (* Another comment still ...) Verilator and we need to get i_setup from the
55
// external environment.  If not, it must be set internally.
56 5 dgisselq
//
57 10 dgisselq
`ifndef VERILATOR
58
`define OPT_STANDALONE
59
`endif
60 5 dgisselq
//
61
module  speechfifo(i_clk,
62
`ifndef OPT_STANDALONE
63
                        i_setup,
64
`endif
65
                        o_uart_tx);
66
        input           i_clk;
67
        output  wire    o_uart_tx;
68
 
69 6 dgisselq
        // Here we set i_setup to something appropriate to create a 115200 Baud
70
        // UART system from a 100MHz clock.  This also sets us to an 8-bit data
71
        // word, 1-stop bit, and no parity.  This will be overwritten by
72
        // i_setup, but at least it gives us something to start with/from.
73 10 dgisselq
        parameter       INITIAL_UART_SETUP = 31'd868;
74 6 dgisselq
 
75 18 dgisselq
        // Let's set our message length, in case we ever wish to change it in
76
        // the future
77
        localparam      MSGLEN=2203;
78
 
79 5 dgisselq
        // 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
`ifdef  OPT_STANDALONE
83 10 dgisselq
        wire    [30:0]   i_setup;
84 6 dgisselq
        assign  i_setup = INITIAL_UART_SETUP;
85 5 dgisselq
`else
86 10 dgisselq
        input   [30:0]   i_setup;
87 5 dgisselq
`endif
88
 
89 6 dgisselq
        reg             restart;
90 5 dgisselq
        reg             wb_stb;
91
        reg     [1:0]    wb_addr;
92
        reg     [31:0]   wb_data;
93
 
94 18 dgisselq
        wire            uart_stall;
95
 
96
        // We aren't using the receive interrupts, or the received data, or the
97
        // ready to send line, so we'll just mark them all here as ignored.
98
 
99
        /* verilator lint_off UNUSED */
100
        wire            uart_ack, tx_int;
101 5 dgisselq
        wire    [31:0]   uart_data;
102 18 dgisselq
        wire            ignored_rx_int, ignored_rxfifo_int;
103
        wire            rts_n_ignored;
104
        /* verilator lint_on UNUSED */
105 5 dgisselq
 
106 18 dgisselq
        /* verilator lint_on UNUSED */
107 5 dgisselq
 
108 18 dgisselq
        wire            txfifo_int;
109
 
110 5 dgisselq
        // The next four lines create a strobe signal that is true on the first
111
        // clock, but never after.  This makes for a decent power-on reset
112
        // signal.
113
        reg     pwr_reset;
114
        initial pwr_reset = 1'b1;
115
        always @(posedge i_clk)
116
                pwr_reset <= 1'b0;
117
 
118
        // The message we wish to transmit is kept in "message".  It needs to be
119
        // set initially.  Do so here.
120
        //
121
        // Since the message has fewer than 2048 elements in it, we preset every
122
        // element to a space so that if (for some reason) we broadcast past the
123
        // end of our message, we'll at least be sending something useful.
124
        integer i;
125 18 dgisselq
        reg     [7:0]    message [0:4095];
126 5 dgisselq
        initial begin
127 10 dgisselq
                // xx Verilator needs this file to be in the directory the file
128
                // is run from.  For that reason, the project builds, makes,
129
                // and keeps speech.hex in bench/cpp.  
130
                //
131
                // Vivado, however, wants speech.hex to be in a project file
132
                // directory, such as bench/verilog.  For that reason, the
133
                // build function in bench/cpp also copies speech.hex to the
134
                // bench/verilog directory.  You may need to make certain the
135
                // file is both built, and copied into a directory where your
136
                // synthesis tool can find it.
137
                //
138
                $readmemh("speech.hex", message);
139 18 dgisselq
                for(i=MSGLEN; i<4095; i=i+1)
140 5 dgisselq
                        message[i] = 8'h20;
141 18 dgisselq
 
142 10 dgisselq
                //
143
                // The problem with the above approach is Xilinx's ISE program.
144
                // It's broken.  It can't handle HEX files well (at all?) and
145
                // has more problems with HEX's defining ROM's.  For that
146
                // reason, the mkspeech program can be tuned to create an
147
                // include file, speech.inc.  We include that program here.
148
                // It is rather ugly, though, and not a very elegant solution,
149
                // since it walks through every value in our speech, byte by
150
                // byte, with an initial line for each byte declaring what it
151
                // is to be.
152
                //
153
                // If you (need to) use this route, comment out both the 
154
                // readmemh, the for loop, and the message[i] = 8'h20 lines
155
                // above and uncomment the include line below.
156
                //
157
                // `include "speech.inc"
158 5 dgisselq
        end
159
 
160
        // Let's keep track of time, and send our message over and over again.
161
        // To do this, we'll keep track of a restart counter.  When this counter
162
        // rolls over, we restart our message.
163
        reg     [30:0]   restart_counter;
164
        // Since we want to start our message just a couple clocks after power
165
        // up, we'll set the reset counter just a couple clocks shy of a roll
166
        // over.
167
        initial restart_counter = -31'd16;
168
        always @(posedge i_clk)
169
                restart_counter <= restart_counter+1'b1;
170
 
171
        // Ok, now that we have a counter that tells us when to start over,
172
        // let's build a set of signals that we can use to get things started
173
        // again.  This will be the restart signal.  On this signal, we just
174
        // restart everything.
175
        initial restart = 0;
176
        always @(posedge i_clk)
177
                restart <= (restart_counter == 0);
178
 
179
        // Our message index.  This is the address of the character we wish to
180
        // transmit next.  Note, there's a clock delay between setting this 
181
        // index and when the wb_data is valid.  Hence, we set the index on
182
        // restart[0] to zero.
183 18 dgisselq
        reg     [11:0]   msg_index;
184
        initial msg_index = 12'h000 - 12'h8;
185 5 dgisselq
        always @(posedge i_clk)
186
        begin
187
                if (restart)
188
                        msg_index <= 0;
189
                else if ((wb_stb)&&(!uart_stall))
190
                        // We only advance the index if a port operation on the
191
                        // wbuart has taken place.  That's what the
192
                        // (wb_stb)&&(!uart_stall) is about.  (wb_stb) is the
193
                        // request for a transaction on the bus, uart_stall
194
                        // tells us to wait 'cause the peripheral isn't ready. 
195
                        // In our case, it's always ready, uart_stall == 0, but
196
                        // we keep/maintain this logic for good form.
197
                        //
198
                        // Note also, we only advance when restart[0] is zero.
199
                        // This keeps us from advancing prior to the setup
200
                        // word.
201
                        msg_index <= msg_index + 1'b1;
202
        end
203
 
204
        // What data will we be sending to the port?
205
        always @(posedge i_clk)
206
                if (restart)
207
                        // The first thing we do is set the baud rate, and
208
                        // serial port configuration parameters.  Ideally,
209
                        // we'd only set this once.  But rather than complicate
210
                        // the logic, we set it everytime we start over.
211 10 dgisselq
                        wb_data <= { 1'b0, i_setup };
212 5 dgisselq
                else if ((wb_stb)&&(!uart_stall))
213
                        // Then, if the last thing was received over the bus,
214
                        // we move to the next data item.
215
                        wb_data <= { 24'h00, message[msg_index] };
216
 
217
        // We send our first value to the SETUP address (all zeros), all other
218
        // values we send to the transmitters address.  We should really be
219
        // double checking that stall remains low, but its not required here.
220
        always @(posedge i_clk)
221
                if (restart)
222
                        wb_addr <= 2'b00;
223
                else // if (!uart_stall)??
224
                        wb_addr <= 2'b11;
225
 
226 6 dgisselq
        // Knowing when to stop sending the speech is important, but depends
227
        // upon an 11 bit comparison.  Since FPGA logic is best measured by the
228
        // number of inputs to an always block, we pull those 11-bits out of
229
        // the always block for wb_stb, and place them here on the clock prior.
230
        // If end_of_message is true, then we need to stop transmitting, and
231
        // wait for the next (restart) to get us started again.  We set that
232
        // flag hee.
233
        reg     end_of_message;
234
        initial end_of_message = 1'b1;
235
        always @(posedge i_clk)
236
                if (restart)
237
                        end_of_message <= 1'b0;
238
                else
239 18 dgisselq
                        end_of_message <= (msg_index >= MSGLEN);
240 6 dgisselq
 
241 5 dgisselq
        // The wb_stb signal indicates that we wish to write, using the wishbone
242
        // to our peripheral.  We have two separate types of writes.  First,
243
        // we wish to write our setup.  Then we want to drop STB and write
244
        // our data.  Once we've filled half of the FIFO, we wait for the FIFO
245
        // to empty before issuing a STB again and then fill up half the FIFO
246
        // again.
247
        initial wb_stb = 1'b0;
248
        always @(posedge i_clk)
249
                if (restart)
250 6 dgisselq
                        // Start sending to the UART on a reset.  The first
251
                        // thing we'll send will be the configuration, but
252
                        // that's done elsewhere.  This just starts up the
253
                        // writes to the peripheral wbuart.
254 5 dgisselq
                        wb_stb <= 1'b1;
255 6 dgisselq
                else if (end_of_message)
256
                        // Stop transmitting when we get to the end of our
257
                        // message.
258 5 dgisselq
                        wb_stb <= 1'b0;
259 10 dgisselq
                else if (txfifo_int)
260
                        // If the FIFO is less than half full, then write to
261
                        // it.
262 5 dgisselq
                        wb_stb <= 1'b1;
263
                else
264 6 dgisselq
                        // But once the FIFO gets to half full, stop.
265 5 dgisselq
                        wb_stb <= 1'b0;
266
 
267 10 dgisselq
        // The WBUART can handle hardware flow control signals.  This test,
268
        // however, cannot.  The reason?  Simply just to keep things simple.
269
        // If you want to add hardware flow control to your design, simply
270
        // make rts an input to this module.
271
        //
272
        // Since this is an output only module demonstrator, what would be the
273
        // cts output is unused.
274 18 dgisselq
        wire    cts_n;
275 15 dgisselq
        assign  cts_n = 1'b0;
276 10 dgisselq
 
277 5 dgisselq
        // Finally--the unit under test--now that we've set up all the wires
278
        // to run/test it.
279 6 dgisselq
        wbuart  #(INITIAL_UART_SETUP)
280 5 dgisselq
                wbuarti(i_clk, pwr_reset,
281
                        wb_stb, wb_stb, 1'b1, wb_addr, wb_data,
282 10 dgisselq
                        uart_ack, uart_stall, uart_data,
283 15 dgisselq
                        1'b1, o_uart_tx, cts_n, rts_n_ignored,
284 5 dgisselq
                        ignored_rx_int, tx_int,
285
                        ignored_rxfifo_int, txfifo_int);
286
 
287
endmodule

powered by: WebSVN 2.1.0

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