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

Subversion Repositories wbuart32

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

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 26 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
29 15 dgisselq
//
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 21 dgisselq
        parameter       [4:0]    TIMING_BITS = 5'd24;
68
        localparam              TB = TIMING_BITS;
69
        parameter       [(TB-1):0]       CLOCKS_PER_BAUD = 8; // 24'd868;
70 17 dgisselq
        input   wire            i_clk;
71
        input   wire            i_wr;
72
        input   wire    [7:0]    i_data;
73 15 dgisselq
        // And the UART input line itself
74
        output  reg             o_uart_tx;
75
        // A line to tell others when we are ready to accept data.  If
76
        // (i_wr)&&(!o_busy) is ever true, then the core has accepted a byte
77
        // for transmission.
78
        output  wire            o_busy;
79
 
80 21 dgisselq
        reg     [(TB-1):0]       baud_counter;
81 15 dgisselq
        reg     [3:0]    state;
82
        reg     [7:0]    lcl_data;
83
        reg             r_busy, zero_baud_counter;
84
 
85
        initial r_busy = 1'b1;
86 17 dgisselq
        initial state  = `TXUL_IDLE;
87 15 dgisselq
        always @(posedge i_clk)
88
        begin
89
                if (!zero_baud_counter)
90
                        // r_busy needs to be set coming into here
91
                        r_busy <= 1'b1;
92 21 dgisselq
                else if (state > `TXUL_STOP)    // STATE_IDLE
93 15 dgisselq
                begin
94 21 dgisselq
                        state <= `TXUL_IDLE;
95 15 dgisselq
                        r_busy <= 1'b0;
96
                        if ((i_wr)&&(!r_busy))
97
                        begin   // Immediately start us off with a start bit
98
                                r_busy <= 1'b1;
99 17 dgisselq
                                state <= `TXUL_BIT_ZERO;
100 15 dgisselq
                        end
101
                end else begin
102
                        // One clock tick in each of these states ...
103
                        r_busy <= 1'b1;
104 17 dgisselq
                        if (state <=`TXUL_STOP) // start bit, 8-d bits, stop-b
105 21 dgisselq
                                state <= state + 1'b1;
106 15 dgisselq
                        else
107 17 dgisselq
                                state <= `TXUL_IDLE;
108 18 dgisselq
                end
109 15 dgisselq
        end
110
 
111
        // o_busy
112
        //
113
        // This is a wire, designed to be true is we are ever busy above.
114
        // originally, this was going to be true if we were ever not in the
115
        // idle state.  The logic has since become more complex, hence we have
116
        // a register dedicated to this and just copy out that registers value.
117
        assign  o_busy = (r_busy);
118
 
119
 
120
        // lcl_data
121
        //
122
        // This is our working copy of the i_data register which we use
123
        // when transmitting.  It is only of interest during transmit, and is
124
        // allowed to be whatever at any other time.  Hence, if r_busy isn't
125
        // true, we can always set it.  On the one clock where r_busy isn't
126
        // true and i_wr is, we set it and r_busy is true thereafter.
127
        // Then, on any zero_baud_counter (i.e. change between baud intervals)
128
        // we simple logically shift the register right to grab the next bit.
129
        initial lcl_data = 8'hff;
130
        always @(posedge i_clk)
131
                if ((i_wr)&&(!r_busy))
132
                        lcl_data <= i_data;
133
                else if (zero_baud_counter)
134
                        lcl_data <= { 1'b1, lcl_data[7:1] };
135
 
136
        // o_uart_tx
137
        //
138
        // This is the final result/output desired of this core.  It's all
139
        // centered about o_uart_tx.  This is what finally needs to follow
140
        // the UART protocol.
141
        //
142
        initial o_uart_tx = 1'b1;
143
        always @(posedge i_clk)
144
                if ((i_wr)&&(!r_busy))
145
                        o_uart_tx <= 1'b0;      // Set the start bit on writes
146
                else if (zero_baud_counter)     // Set the data bit.
147
                        o_uart_tx <= lcl_data[0];
148
 
149
 
150
        // All of the above logic is driven by the baud counter.  Bits must last
151
        // CLOCKS_PER_BAUD in length, and this baud counter is what we use to
152
        // make certain of that.
153
        //
154
        // The basic logic is this: at the beginning of a bit interval, start
155
        // the baud counter and set it to count CLOCKS_PER_BAUD.  When it gets
156
        // to zero, restart it.
157
        //
158
        // However, comparing a 28'bit number to zero can be rather complex--
159
        // especially if we wish to do anything else on that same clock.  For
160
        // that reason, we create "zero_baud_counter".  zero_baud_counter is
161
        // nothing more than a flag that is true anytime baud_counter is zero.
162
        // It's true when the logic (above) needs to step to the next bit.
163
        // Simple enough?
164
        //
165
        // I wish we could stop there, but there are some other (ugly)
166
        // conditions to deal with that offer exceptions to this basic logic.
167
        //
168
        // 1. When the user has commanded a BREAK across the line, we need to
169
        // wait several baud intervals following the break before we start
170
        // transmitting, to give any receiver a chance to recognize that we are
171
        // out of the break condition, and to know that the next bit will be
172
        // a stop bit.
173
        //
174
        // 2. A reset is similar to a break condition--on both we wait several
175
        // baud intervals before allowing a start bit.
176
        //
177
        // 3. In the idle state, we stop our counter--so that upon a request
178
        // to transmit when idle we can start transmitting immediately, rather
179
        // than waiting for the end of the next (fictitious and arbitrary) baud
180
        // interval.
181
        //
182 17 dgisselq
        // When (i_wr)&&(!r_busy)&&(state == `TXUL_IDLE) then we're not only in
183 15 dgisselq
        // the idle state, but we also just accepted a command to start writing
184
        // the next word.  At this point, the baud counter needs to be reset
185
        // to the number of CLOCKS_PER_BAUD, and zero_baud_counter set to zero.
186
        //
187
        // The logic is a bit twisted here, in that it will only check for the
188
        // above condition when zero_baud_counter is false--so as to make
189
        // certain the STOP bit is complete.
190 21 dgisselq
        initial zero_baud_counter = 1'b1;
191
        initial baud_counter = 0;
192 15 dgisselq
        always @(posedge i_clk)
193
        begin
194 26 dgisselq
                zero_baud_counter <= (baud_counter == 1);
195 17 dgisselq
                if (state == `TXUL_IDLE)
196 15 dgisselq
                begin
197 26 dgisselq
                        baud_counter <= 0;
198 15 dgisselq
                        zero_baud_counter <= 1'b1;
199
                        if ((i_wr)&&(!r_busy))
200
                        begin
201 26 dgisselq
                                baud_counter <= CLOCKS_PER_BAUD - 1'b1;
202 15 dgisselq
                                zero_baud_counter <= 1'b0;
203
                        end
204 21 dgisselq
                end else if ((zero_baud_counter)&&(state == 4'h9))
205
                begin
206
                        baud_counter <= 0;
207
                        zero_baud_counter <= 1'b1;
208 15 dgisselq
                end else if (!zero_baud_counter)
209 26 dgisselq
                        baud_counter <= baud_counter - 1'b1;
210 15 dgisselq
                else
211 26 dgisselq
                        baud_counter <= CLOCKS_PER_BAUD - 1'b1;
212 15 dgisselq
        end
213 18 dgisselq
 
214
//
215
//
216
// FORMAL METHODS
217
//
218
//
219
//
220
`ifdef  FORMAL
221
 
222
`ifdef  TXUARTLITE
223
`define ASSUME  assume
224
`else
225
`define ASSUME  assert
226
`endif
227
 
228
        // Setup
229
 
230
        reg     f_past_valid, f_last_clk;
231
 
232
        initial f_past_valid = 1'b0;
233
        always @(posedge i_clk)
234
                f_past_valid <= 1'b1;
235
 
236 21 dgisselq
        initial `ASSUME(!i_wr);
237 18 dgisselq
        always @(posedge i_clk)
238
                if ((f_past_valid)&&($past(i_wr))&&($past(o_busy)))
239
                begin
240
                        `ASSUME(i_wr   == $past(i_wr));
241
                        `ASSUME(i_data == $past(i_data));
242
                end
243
 
244
        // Check the baud counter
245
        always @(posedge i_clk)
246 21 dgisselq
                assert(zero_baud_counter == (baud_counter == 0));
247 18 dgisselq
 
248
        always @(posedge i_clk)
249
                if ((f_past_valid)&&($past(baud_counter != 0))&&($past(state != `TXUL_IDLE)))
250
                        assert(baud_counter == $past(baud_counter - 1'b1));
251
 
252
        always @(posedge i_clk)
253
                if ((f_past_valid)&&(!$past(zero_baud_counter))&&($past(state != `TXUL_IDLE)))
254
                        assert($stable(o_uart_tx));
255
 
256 21 dgisselq
        reg     [(TB-1):0]       f_baud_count;
257 18 dgisselq
        initial f_baud_count = 1'b0;
258
        always @(posedge i_clk)
259
                if (zero_baud_counter)
260
                        f_baud_count <= 0;
261
                else
262
                        f_baud_count <= f_baud_count + 1'b1;
263
 
264
        always @(posedge i_clk)
265
                assert(f_baud_count < CLOCKS_PER_BAUD);
266
 
267
        always @(posedge i_clk)
268
                if (baud_counter != 0)
269
                        assert(o_busy);
270
 
271
        reg     [9:0]    f_txbits;
272
        initial f_txbits = 0;
273
        always @(posedge i_clk)
274
                if (zero_baud_counter)
275
                        f_txbits <= { o_uart_tx, f_txbits[9:1] };
276
 
277 21 dgisselq
        always @(posedge i_clk)
278
        if ((f_past_valid)&&(!$past(zero_baud_counter))
279
                        &&(!$past(state==`TXUL_IDLE)))
280
                assert(state == $past(state));
281
 
282 18 dgisselq
        reg     [3:0]    f_bitcount;
283
        initial f_bitcount = 0;
284
        always @(posedge i_clk)
285
                if ((!f_past_valid)||(!$past(f_past_valid)))
286
                        f_bitcount <= 0;
287
                else if ((state == `TXUL_IDLE)&&(zero_baud_counter))
288
                        f_bitcount <= 0;
289
                else if (zero_baud_counter)
290
                        f_bitcount <= f_bitcount + 1'b1;
291
 
292
        always @(posedge i_clk)
293
                assert(f_bitcount <= 4'ha);
294
 
295
        reg     [7:0]    f_request_tx_data;
296
        always @(posedge i_clk)
297
                if ((i_wr)&&(!o_busy))
298
                        f_request_tx_data <= i_data;
299
 
300
        wire    [3:0]    subcount;
301
        assign  subcount = 10-f_bitcount;
302
        always @(posedge i_clk)
303
                if (f_bitcount > 0)
304
                        assert(!f_txbits[subcount]);
305
 
306
        always @(posedge i_clk)
307
                if (f_bitcount == 4'ha)
308
                begin
309
                        assert(f_txbits[8:1] == f_request_tx_data);
310
                        assert( f_txbits[9]);
311
                end
312
 
313
        always @(posedge i_clk)
314
                assert((state <= `TXUL_STOP + 1'b1)||(state == `TXUL_IDLE));
315
 
316 21 dgisselq
        always @(posedge i_clk)
317
        if ((f_past_valid)&&($past(f_past_valid))&&($past(o_busy)))
318
                cover(!o_busy);
319
 
320 18 dgisselq
`endif  // FORMAL
321 21 dgisselq
`ifdef  VERIFIC_SVA
322
        reg     [7:0]    fsv_data;
323
 
324
        //
325
        // Grab a copy of the data any time we are sent a new byte to transmit
326
        // We'll use this in a moment to compare the item transmitted against
327
        // what is supposed to be transmitted
328
        //
329
        always @(posedge i_clk)
330
                if ((i_wr)&&(!o_busy))
331
                        fsv_data <= i_data;
332
 
333
        //
334
        // One baud interval
335
        //
336
        // 1. The UART output is constant at DAT
337
        // 2. The internal state remains constant at ST
338
        // 3. CKS = the number of clocks per bit.
339
        //
340
        // Everything stays constant during the CKS clocks with the exception
341
        // of (zero_baud_counter), which is *only* raised on the last clock
342
        // interval
343
        sequence        BAUD_INTERVAL(CKS, DAT, SR, ST);
344
                ((o_uart_tx == DAT)&&(state == ST)
345
                        &&(lcl_data == SR)
346
                        &&(!zero_baud_counter))[*(CKS-1)]
347
                ##1 (o_uart_tx == DAT)&&(state == ST)
348
                        &&(lcl_data == SR)
349
                        &&(zero_baud_counter);
350
        endsequence
351
 
352
        //
353
        // One byte transmitted
354
        //
355
        // DATA = the byte that is sent
356
        // CKS  = the number of clocks per bit
357
        //
358
        sequence        SEND(CKS, DATA);
359
                BAUD_INTERVAL(CKS, 1'b0, DATA, 4'h0)
360
                ##1 BAUD_INTERVAL(CKS, DATA[0], {{(1){1'b1}},DATA[7:1]}, 4'h1)
361
                ##1 BAUD_INTERVAL(CKS, DATA[1], {{(2){1'b1}},DATA[7:2]}, 4'h2)
362
                ##1 BAUD_INTERVAL(CKS, DATA[2], {{(3){1'b1}},DATA[7:3]}, 4'h3)
363
                ##1 BAUD_INTERVAL(CKS, DATA[3], {{(4){1'b1}},DATA[7:4]}, 4'h4)
364
                ##1 BAUD_INTERVAL(CKS, DATA[4], {{(5){1'b1}},DATA[7:5]}, 4'h5)
365
                ##1 BAUD_INTERVAL(CKS, DATA[5], {{(6){1'b1}},DATA[7:6]}, 4'h6)
366
                ##1 BAUD_INTERVAL(CKS, DATA[6], {{(7){1'b1}},DATA[7:7]}, 4'h7)
367
                ##1 BAUD_INTERVAL(CKS, DATA[7], 8'hff, 4'h8)
368
                ##1 BAUD_INTERVAL(CKS, 1'b1, 8'hff, 4'h9);
369
        endsequence
370
 
371
        //
372
        // Transmit one byte
373
        //
374
        // Once the byte is transmitted, make certain we return to
375
        // idle
376
        //
377
        assert property (
378
                @(posedge i_clk)
379
                (i_wr)&&(!o_busy)
380
                |=> ((o_busy) throughout SEND(CLOCKS_PER_BAUD,fsv_data))
381
                ##1 (!o_busy)&&(o_uart_tx)&&(zero_baud_counter));
382
 
383
        assume property (
384
                @(posedge i_clk)
385
                (i_wr)&&(o_busy) |=>
386
                        (i_wr)&&(o_busy)&&($stable(i_data)));
387
 
388
        //
389
        // Make certain that o_busy is true any time zero_baud_counter is
390
        // non-zero
391
        //
392
        always @(*)
393
                assert((o_busy)||(zero_baud_counter) );
394
 
395
        // If and only if zero_baud_counter is true, baud_counter must be zero
396
        // Insist on that relationship here.
397
        always @(*)
398
                assert(zero_baud_counter == (baud_counter == 0));
399
 
400
        // To make certain baud_counter stays below CLOCKS_PER_BAUD
401
        always @(*)
402
                assert(baud_counter < CLOCKS_PER_BAUD);
403
 
404
        //
405
        // Insist that we are only ever in a valid state
406
        always @(*)
407
                assert((state <= `TXUL_STOP+1'b1)||(state == `TXUL_IDLE));
408
 
409
`endif // Verific SVA
410 15 dgisselq
endmodule
411
 

powered by: WebSVN 2.1.0

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