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

Subversion Repositories wbuart32

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

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

powered by: WebSVN 2.1.0

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