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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [rtl/] [txuartlite.v] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 15 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    txuartlite.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     Transmit outputs over a single UART line.  This particular UART
8
//              implementation has been extremely simplified: it does not handle
9
//      generating break conditions, nor does it handle anything other than the
10
//      8N1 (8 data bits, no parity, 1 stop bit) UART sub-protocol.
11
//
12
//      To interface with this module, connect it to your system clock, and
13
//      pass it the byte of data you wish to transmit.  Strobe the i_wr line
14
//      high for one cycle, and your data will be off.  Wait until the 'o_busy'
15
//      line is low before strobing the i_wr line again--this implementation
16
//      has NO BUFFER, so strobing i_wr while the core is busy will just
17 18 dgisselq
//      get ignored.  The output will be placed on the o_txuart output line.
18 15 dgisselq
//
19
//      (I often set both data and strobe on the same clock, and then just leave
20
//      them set until the busy line is low.  Then I move on to the next piece
21
//      of data.)
22
//
23
// Creator:     Dan Gisselquist, Ph.D.
24
//              Gisselquist Technology, LLC
25
//
26
////////////////////////////////////////////////////////////////////////////////
27
//
28
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
29
//
30
// This program is free software (firmware): you can redistribute it and/or
31
// modify it under the terms of  the GNU General Public License as published
32
// by the Free Software Foundation, either version 3 of the License, or (at
33
// your option) any later version.
34
//
35
// This program is distributed in the hope that it will be useful, but WITHOUT
36
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
37
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
38
// for more details.
39
//
40
// You should have received a copy of the GNU General Public License along
41
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
42
// target there if the PDF file isn't present.)  If not, see
43
// <http://www.gnu.org/licenses/> for a copy.
44
//
45
// License:     GPL, v3, as defined and found on www.gnu.org,
46
//              http://www.gnu.org/licenses/gpl.html
47
//
48
//
49
////////////////////////////////////////////////////////////////////////////////
50
//
51
//
52 17 dgisselq
`default_nettype        none
53 15 dgisselq
//
54 17 dgisselq
`define TXUL_BIT_ZERO   4'h0
55
`define TXUL_BIT_ONE    4'h1
56
`define TXUL_BIT_TWO    4'h2
57
`define TXUL_BIT_THREE  4'h3
58
`define TXUL_BIT_FOUR   4'h4
59
`define TXUL_BIT_FIVE   4'h5
60
`define TXUL_BIT_SIX    4'h6
61
`define TXUL_BIT_SEVEN  4'h7
62
`define TXUL_STOP       4'h8
63
`define TXUL_IDLE       4'hf
64 15 dgisselq
//
65 17 dgisselq
//
66 15 dgisselq
module txuartlite(i_clk, i_wr, i_data, o_uart_tx, o_busy);
67 18 dgisselq
        parameter       [23:0]   CLOCKS_PER_BAUD = 24'd8; // 24'd868;
68 17 dgisselq
        input   wire            i_clk;
69
        input   wire            i_wr;
70
        input   wire    [7:0]    i_data;
71 15 dgisselq
        // And the UART input line itself
72
        output  reg             o_uart_tx;
73
        // A line to tell others when we are ready to accept data.  If
74
        // (i_wr)&&(!o_busy) is ever true, then the core has accepted a byte
75
        // for transmission.
76
        output  wire            o_busy;
77
 
78
        reg     [23:0]   baud_counter;
79
        reg     [3:0]    state;
80
        reg     [7:0]    lcl_data;
81
        reg             r_busy, zero_baud_counter;
82
 
83
        initial r_busy = 1'b1;
84 17 dgisselq
        initial state  = `TXUL_IDLE;
85 15 dgisselq
        always @(posedge i_clk)
86
        begin
87
                if (!zero_baud_counter)
88
                        // r_busy needs to be set coming into here
89
                        r_busy <= 1'b1;
90 17 dgisselq
                else if (state == `TXUL_IDLE)   // STATE_IDLE
91 15 dgisselq
                begin
92
                        r_busy <= 1'b0;
93
                        if ((i_wr)&&(!r_busy))
94
                        begin   // Immediately start us off with a start bit
95
                                r_busy <= 1'b1;
96 17 dgisselq
                                state <= `TXUL_BIT_ZERO;
97 15 dgisselq
                        end
98
                end else begin
99
                        // One clock tick in each of these states ...
100
                        r_busy <= 1'b1;
101 17 dgisselq
                        if (state <=`TXUL_STOP) // start bit, 8-d bits, stop-b
102 15 dgisselq
                                state <= state + 1;
103
                        else
104 17 dgisselq
                                state <= `TXUL_IDLE;
105 18 dgisselq
                end
106 15 dgisselq
        end
107
 
108
        // o_busy
109
        //
110
        // This is a wire, designed to be true is we are ever busy above.
111
        // originally, this was going to be true if we were ever not in the
112
        // idle state.  The logic has since become more complex, hence we have
113
        // a register dedicated to this and just copy out that registers value.
114
        assign  o_busy = (r_busy);
115
 
116
 
117
        // lcl_data
118
        //
119
        // This is our working copy of the i_data register which we use
120
        // when transmitting.  It is only of interest during transmit, and is
121
        // allowed to be whatever at any other time.  Hence, if r_busy isn't
122
        // true, we can always set it.  On the one clock where r_busy isn't
123
        // true and i_wr is, we set it and r_busy is true thereafter.
124
        // Then, on any zero_baud_counter (i.e. change between baud intervals)
125
        // we simple logically shift the register right to grab the next bit.
126
        initial lcl_data = 8'hff;
127
        always @(posedge i_clk)
128
                if ((i_wr)&&(!r_busy))
129
                        lcl_data <= i_data;
130
                else if (zero_baud_counter)
131
                        lcl_data <= { 1'b1, lcl_data[7:1] };
132
 
133
        // o_uart_tx
134
        //
135
        // This is the final result/output desired of this core.  It's all
136
        // centered about o_uart_tx.  This is what finally needs to follow
137
        // the UART protocol.
138
        //
139
        initial o_uart_tx = 1'b1;
140
        always @(posedge i_clk)
141
                if ((i_wr)&&(!r_busy))
142
                        o_uart_tx <= 1'b0;      // Set the start bit on writes
143
                else if (zero_baud_counter)     // Set the data bit.
144
                        o_uart_tx <= lcl_data[0];
145
 
146
 
147
        // All of the above logic is driven by the baud counter.  Bits must last
148
        // CLOCKS_PER_BAUD in length, and this baud counter is what we use to
149
        // make certain of that.
150
        //
151
        // The basic logic is this: at the beginning of a bit interval, start
152
        // the baud counter and set it to count CLOCKS_PER_BAUD.  When it gets
153
        // to zero, restart it.
154
        //
155
        // However, comparing a 28'bit number to zero can be rather complex--
156
        // especially if we wish to do anything else on that same clock.  For
157
        // that reason, we create "zero_baud_counter".  zero_baud_counter is
158
        // nothing more than a flag that is true anytime baud_counter is zero.
159
        // It's true when the logic (above) needs to step to the next bit.
160
        // Simple enough?
161
        //
162
        // I wish we could stop there, but there are some other (ugly)
163
        // conditions to deal with that offer exceptions to this basic logic.
164
        //
165
        // 1. When the user has commanded a BREAK across the line, we need to
166
        // wait several baud intervals following the break before we start
167
        // transmitting, to give any receiver a chance to recognize that we are
168
        // out of the break condition, and to know that the next bit will be
169
        // a stop bit.
170
        //
171
        // 2. A reset is similar to a break condition--on both we wait several
172
        // baud intervals before allowing a start bit.
173
        //
174
        // 3. In the idle state, we stop our counter--so that upon a request
175
        // to transmit when idle we can start transmitting immediately, rather
176
        // than waiting for the end of the next (fictitious and arbitrary) baud
177
        // interval.
178
        //
179 17 dgisselq
        // When (i_wr)&&(!r_busy)&&(state == `TXUL_IDLE) then we're not only in
180 15 dgisselq
        // the idle state, but we also just accepted a command to start writing
181
        // the next word.  At this point, the baud counter needs to be reset
182
        // to the number of CLOCKS_PER_BAUD, and zero_baud_counter set to zero.
183
        //
184
        // The logic is a bit twisted here, in that it will only check for the
185
        // above condition when zero_baud_counter is false--so as to make
186
        // certain the STOP bit is complete.
187
        initial zero_baud_counter = 1'b0;
188
        initial baud_counter = 24'h05;
189
        always @(posedge i_clk)
190
        begin
191
                zero_baud_counter <= (baud_counter == 24'h01);
192 17 dgisselq
                if (state == `TXUL_IDLE)
193 15 dgisselq
                begin
194
                        baud_counter <= 24'h0;
195
                        zero_baud_counter <= 1'b1;
196
                        if ((i_wr)&&(!r_busy))
197
                        begin
198
                                baud_counter <= CLOCKS_PER_BAUD - 24'h01;
199
                                zero_baud_counter <= 1'b0;
200
                        end
201
                end else if (!zero_baud_counter)
202
                        baud_counter <= baud_counter - 24'h01;
203
                else
204
                        baud_counter <= CLOCKS_PER_BAUD - 24'h01;
205
        end
206 18 dgisselq
 
207
//
208
//
209
// FORMAL METHODS
210
//
211
//
212
//
213
`ifdef  FORMAL
214
 
215
`ifdef  TXUARTLITE
216
`define ASSUME  assume
217
`else
218
`define ASSUME  assert
219
`endif
220
 
221
        // Setup
222
 
223
        reg     f_past_valid, f_last_clk;
224
 
225
        always @($global_clock)
226
        begin
227
                restrict(i_clk == !f_last_clk);
228
                f_last_clk <= i_clk;
229
                if (!$rose(i_clk))
230
                begin
231
                        `ASSUME($stable(i_wr));
232
                        `ASSUME($stable(i_data));
233
                end
234
        end
235
 
236
        initial f_past_valid = 1'b0;
237
        always @(posedge i_clk)
238
                f_past_valid <= 1'b1;
239
 
240
        always @(posedge i_clk)
241
                if ((f_past_valid)&&($past(i_wr))&&($past(o_busy)))
242
                begin
243
                        `ASSUME(i_wr   == $past(i_wr));
244
                        `ASSUME(i_data == $past(i_data));
245
                end
246
 
247
        // Check the baud counter
248
        always @(posedge i_clk)
249
                if (zero_baud_counter)
250
                        assert(baud_counter == 0);
251
 
252
        always @(posedge i_clk)
253
                if ((f_past_valid)&&($past(baud_counter != 0))&&($past(state != `TXUL_IDLE)))
254
                        assert(baud_counter == $past(baud_counter - 1'b1));
255
 
256
        always @(posedge i_clk)
257
                if ((f_past_valid)&&(!$past(zero_baud_counter))&&($past(state != `TXUL_IDLE)))
258
                        assert($stable(o_uart_tx));
259
 
260
        reg     [23:0]   f_baud_count;
261
        initial f_baud_count = 1'b0;
262
        always @(posedge i_clk)
263
                if (zero_baud_counter)
264
                        f_baud_count <= 0;
265
                else
266
                        f_baud_count <= f_baud_count + 1'b1;
267
 
268
        always @(posedge i_clk)
269
                assert(f_baud_count < CLOCKS_PER_BAUD);
270
 
271
        always @(posedge i_clk)
272
                if (baud_counter != 0)
273
                        assert(o_busy);
274
 
275
        reg     [9:0]    f_txbits;
276
        initial f_txbits = 0;
277
        always @(posedge i_clk)
278
                if (zero_baud_counter)
279
                        f_txbits <= { o_uart_tx, f_txbits[9:1] };
280
 
281
        reg     [3:0]    f_bitcount;
282
        initial f_bitcount = 0;
283
        always @(posedge i_clk)
284
                //if (baud_counter == CLOCKS_PER_BAUD - 24'h01)
285
                        //f_bitcount <= f_bitcount + 1'b1;
286
                if ((!f_past_valid)||(!$past(f_past_valid)))
287
                        f_bitcount <= 0;
288
                else if ((state == `TXUL_IDLE)&&(zero_baud_counter))
289
                        f_bitcount <= 0;
290
                else if (zero_baud_counter)
291
                        f_bitcount <= f_bitcount + 1'b1;
292
 
293
        always @(posedge i_clk)
294
                assert(f_bitcount <= 4'ha);
295
 
296
        reg     [7:0]    f_request_tx_data;
297
        always @(posedge i_clk)
298
                if ((i_wr)&&(!o_busy))
299
                        f_request_tx_data <= i_data;
300
 
301
        wire    [3:0]    subcount;
302
        assign  subcount = 10-f_bitcount;
303
        always @(posedge i_clk)
304
                if (f_bitcount > 0)
305
                        assert(!f_txbits[subcount]);
306
/*
307
 
308
        always @(posedge i_clk)
309
                if ((f_bitcount > 2)&&(f_bitcount <= 10))
310
                        assert(f_txbits[f_bitcount-2:0]
311
                                == f_request_tx_data[7:(9-f_bitcount)]);
312
*/
313
 
314
        always @(posedge i_clk)
315
                if (f_bitcount == 4'ha)
316
                begin
317
                        assert(f_txbits[8:1] == f_request_tx_data);
318
                        assert( f_txbits[9]);
319
                end
320
 
321
        always @(posedge i_clk)
322
                assert((state <= `TXUL_STOP + 1'b1)||(state == `TXUL_IDLE));
323
//
324
//
325
 
326
`endif  // FORMAL
327 15 dgisselq
endmodule
328
 

powered by: WebSVN 2.1.0

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