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

Subversion Repositories wbuart32

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

Go to most recent revision | 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
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
25
//
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 5 dgisselq
        // The i_setup wires are input when run under Verilator, but need to
76
        // be set internally if this is going to run as a standalone top level
77
        // test configuration.
78
`ifdef  OPT_STANDALONE
79 10 dgisselq
        wire    [30:0]   i_setup;
80 6 dgisselq
        assign  i_setup = INITIAL_UART_SETUP;
81 5 dgisselq
`else
82 10 dgisselq
        input   [30:0]   i_setup;
83 5 dgisselq
`endif
84
 
85 6 dgisselq
        reg             restart;
86 5 dgisselq
        reg             wb_stb;
87
        reg     [1:0]    wb_addr;
88
        reg     [31:0]   wb_data;
89
 
90
        wire            uart_stall, uart_ack;
91
        wire    [31:0]   uart_data;
92
 
93
        wire            tx_int, txfifo_int;
94
 
95
        // The next four lines create a strobe signal that is true on the first
96
        // clock, but never after.  This makes for a decent power-on reset
97
        // signal.
98
        reg     pwr_reset;
99
        initial pwr_reset = 1'b1;
100
        always @(posedge i_clk)
101
                pwr_reset <= 1'b0;
102
 
103
        // The message we wish to transmit is kept in "message".  It needs to be
104
        // set initially.  Do so here.
105
        //
106
        // Since the message has fewer than 2048 elements in it, we preset every
107
        // element to a space so that if (for some reason) we broadcast past the
108
        // end of our message, we'll at least be sending something useful.
109
        integer i;
110
        reg     [7:0]    message [0:2047];
111
        initial begin
112 10 dgisselq
                // xx Verilator needs this file to be in the directory the file
113
                // is run from.  For that reason, the project builds, makes,
114
                // and keeps speech.hex in bench/cpp.  
115
                //
116
                // Vivado, however, wants speech.hex to be in a project file
117
                // directory, such as bench/verilog.  For that reason, the
118
                // build function in bench/cpp also copies speech.hex to the
119
                // bench/verilog directory.  You may need to make certain the
120
                // file is both built, and copied into a directory where your
121
                // synthesis tool can find it.
122
                //
123
                $readmemh("speech.hex", message);
124 6 dgisselq
                for(i=1481; i<2048; i=i+1)
125 5 dgisselq
                        message[i] = 8'h20;
126 10 dgisselq
                //
127
                // The problem with the above approach is Xilinx's ISE program.
128
                // It's broken.  It can't handle HEX files well (at all?) and
129
                // has more problems with HEX's defining ROM's.  For that
130
                // reason, the mkspeech program can be tuned to create an
131
                // include file, speech.inc.  We include that program here.
132
                // It is rather ugly, though, and not a very elegant solution,
133
                // since it walks through every value in our speech, byte by
134
                // byte, with an initial line for each byte declaring what it
135
                // is to be.
136
                //
137
                // If you (need to) use this route, comment out both the 
138
                // readmemh, the for loop, and the message[i] = 8'h20 lines
139
                // above and uncomment the include line below.
140
                //
141
                // `include "speech.inc"
142 5 dgisselq
        end
143
 
144
        // Let's keep track of time, and send our message over and over again.
145
        // To do this, we'll keep track of a restart counter.  When this counter
146
        // rolls over, we restart our message.
147
        reg     [30:0]   restart_counter;
148
        // Since we want to start our message just a couple clocks after power
149
        // up, we'll set the reset counter just a couple clocks shy of a roll
150
        // over.
151
        initial restart_counter = -31'd16;
152
        always @(posedge i_clk)
153
                restart_counter <= restart_counter+1'b1;
154
 
155
        // Ok, now that we have a counter that tells us when to start over,
156
        // let's build a set of signals that we can use to get things started
157
        // again.  This will be the restart signal.  On this signal, we just
158
        // restart everything.
159
        initial restart = 0;
160
        always @(posedge i_clk)
161
                restart <= (restart_counter == 0);
162
 
163
        // Our message index.  This is the address of the character we wish to
164
        // transmit next.  Note, there's a clock delay between setting this 
165
        // index and when the wb_data is valid.  Hence, we set the index on
166
        // restart[0] to zero.
167
        reg     [10:0]   msg_index;
168
        initial msg_index = 11'd2040;
169
        always @(posedge i_clk)
170
        begin
171
                if (restart)
172
                        msg_index <= 0;
173
                else if ((wb_stb)&&(!uart_stall))
174
                        // We only advance the index if a port operation on the
175
                        // wbuart has taken place.  That's what the
176
                        // (wb_stb)&&(!uart_stall) is about.  (wb_stb) is the
177
                        // request for a transaction on the bus, uart_stall
178
                        // tells us to wait 'cause the peripheral isn't ready. 
179
                        // In our case, it's always ready, uart_stall == 0, but
180
                        // we keep/maintain this logic for good form.
181
                        //
182
                        // Note also, we only advance when restart[0] is zero.
183
                        // This keeps us from advancing prior to the setup
184
                        // word.
185
                        msg_index <= msg_index + 1'b1;
186
        end
187
 
188
        // What data will we be sending to the port?
189
        always @(posedge i_clk)
190
                if (restart)
191
                        // The first thing we do is set the baud rate, and
192
                        // serial port configuration parameters.  Ideally,
193
                        // we'd only set this once.  But rather than complicate
194
                        // the logic, we set it everytime we start over.
195 10 dgisselq
                        wb_data <= { 1'b0, i_setup };
196 5 dgisselq
                else if ((wb_stb)&&(!uart_stall))
197
                        // Then, if the last thing was received over the bus,
198
                        // we move to the next data item.
199
                        wb_data <= { 24'h00, message[msg_index] };
200
 
201
        // We send our first value to the SETUP address (all zeros), all other
202
        // values we send to the transmitters address.  We should really be
203
        // double checking that stall remains low, but its not required here.
204
        always @(posedge i_clk)
205
                if (restart)
206
                        wb_addr <= 2'b00;
207
                else // if (!uart_stall)??
208
                        wb_addr <= 2'b11;
209
 
210 6 dgisselq
        // Knowing when to stop sending the speech is important, but depends
211
        // upon an 11 bit comparison.  Since FPGA logic is best measured by the
212
        // number of inputs to an always block, we pull those 11-bits out of
213
        // the always block for wb_stb, and place them here on the clock prior.
214
        // If end_of_message is true, then we need to stop transmitting, and
215
        // wait for the next (restart) to get us started again.  We set that
216
        // flag hee.
217
        reg     end_of_message;
218
        initial end_of_message = 1'b1;
219
        always @(posedge i_clk)
220
                if (restart)
221
                        end_of_message <= 1'b0;
222
                else
223
                        end_of_message <= (msg_index >= 1481);
224
 
225 5 dgisselq
        // The wb_stb signal indicates that we wish to write, using the wishbone
226
        // to our peripheral.  We have two separate types of writes.  First,
227
        // we wish to write our setup.  Then we want to drop STB and write
228
        // our data.  Once we've filled half of the FIFO, we wait for the FIFO
229
        // to empty before issuing a STB again and then fill up half the FIFO
230
        // again.
231
        initial wb_stb = 1'b0;
232
        always @(posedge i_clk)
233
                if (restart)
234 6 dgisselq
                        // Start sending to the UART on a reset.  The first
235
                        // thing we'll send will be the configuration, but
236
                        // that's done elsewhere.  This just starts up the
237
                        // writes to the peripheral wbuart.
238 5 dgisselq
                        wb_stb <= 1'b1;
239 6 dgisselq
                else if (end_of_message)
240
                        // Stop transmitting when we get to the end of our
241
                        // message.
242 5 dgisselq
                        wb_stb <= 1'b0;
243 10 dgisselq
                else if (txfifo_int)
244
                        // If the FIFO is less than half full, then write to
245
                        // it.
246 5 dgisselq
                        wb_stb <= 1'b1;
247
                else
248 6 dgisselq
                        // But once the FIFO gets to half full, stop.
249 5 dgisselq
                        wb_stb <= 1'b0;
250
 
251
        // We aren't using the receive interrupts, so we'll just mark them
252
        // here as ignored.
253
        wire    ignored_rx_int, ignored_rxfifo_int;
254
 
255 10 dgisselq
        // The WBUART can handle hardware flow control signals.  This test,
256
        // however, cannot.  The reason?  Simply just to keep things simple.
257
        // If you want to add hardware flow control to your design, simply
258
        // make rts an input to this module.
259
        //
260
        // Since this is an output only module demonstrator, what would be the
261
        // cts output is unused.
262
        wire    rts, cts;
263
        assign  rts = 1'b1;
264
 
265 5 dgisselq
        // Finally--the unit under test--now that we've set up all the wires
266
        // to run/test it.
267 6 dgisselq
        wbuart  #(INITIAL_UART_SETUP)
268 5 dgisselq
                wbuarti(i_clk, pwr_reset,
269
                        wb_stb, wb_stb, 1'b1, wb_addr, wb_data,
270 10 dgisselq
                        uart_ack, uart_stall, uart_data,
271
                        1'b1, o_uart_tx, rts, cts,
272 5 dgisselq
                        ignored_rx_int, tx_int,
273
                        ignored_rxfifo_int, txfifo_int);
274
 
275
endmodule

powered by: WebSVN 2.1.0

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