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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [rtl/] [rxuartlite.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:    rxuartlite.v
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     Receive and decode inputs from a single UART line.
8
//
9
//
10
//      To interface with this module, connect it to your system clock,
11
//      and a UART input.  Set the parameter to the number of clocks per
12
//      baud.  When data becomes available, the o_wr line will be asserted
13
//      for one clock cycle.
14
//
15
//      This interface only handles 8N1 serial port communications.  It does
16
//      not handle the break, parity, or frame error conditions.
17
//
18
//
19
// Creator:     Dan Gisselquist, Ph.D.
20
//              Gisselquist Technology, LLC
21
//
22
////////////////////////////////////////////////////////////////////////////////
23
//
24 21 dgisselq
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
25 15 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 17 dgisselq
`default_nettype        none
49
//
50
`define RXUL_BIT_ZERO           4'h0
51
`define RXUL_BIT_ONE            4'h1
52
`define RXUL_BIT_TWO            4'h2
53
`define RXUL_BIT_THREE          4'h3
54
`define RXUL_BIT_FOUR           4'h4
55
`define RXUL_BIT_FIVE           4'h5
56
`define RXUL_BIT_SIX            4'h6
57
`define RXUL_BIT_SEVEN          4'h7
58
`define RXUL_STOP               4'h8
59 21 dgisselq
`define RXUL_WAIT               4'h9
60 17 dgisselq
`define RXUL_IDLE               4'hf
61 15 dgisselq
 
62
module rxuartlite(i_clk, i_uart_rx, o_wr, o_data);
63 21 dgisselq
        parameter                       TIMER_BITS = 10;
64
        parameter  [(TIMER_BITS-1):0]    CLOCKS_PER_BAUD = 868;  // 115200 MBaud at 100MHz
65
        localparam                      TB = TIMER_BITS;
66 17 dgisselq
        input   wire            i_clk;
67
        input   wire            i_uart_rx;
68 15 dgisselq
        output  reg             o_wr;
69
        output  reg     [7:0]    o_data;
70
 
71
 
72 21 dgisselq
        wire    [(TB-1):0]       half_baud;
73
        reg     [3:0]            state;
74 15 dgisselq
 
75 21 dgisselq
        assign  half_baud = { 1'b0, CLOCKS_PER_BAUD[(TB-1):1] };
76
        reg     [(TB-1):0]       baud_counter;
77
        reg                     zero_baud_counter;
78 15 dgisselq
 
79
 
80
        // Since this is an asynchronous receiver, we need to register our
81
        // input a couple of clocks over to avoid any problems with 
82
        // metastability.  We do that here, and then ignore all but the
83
        // ck_uart wire.
84
        reg     q_uart, qq_uart, ck_uart;
85 21 dgisselq
        initial q_uart  = 1'b1;
86
        initial qq_uart = 1'b1;
87
        initial ck_uart = 1'b1;
88 15 dgisselq
        always @(posedge i_clk)
89 21 dgisselq
                { ck_uart, qq_uart, q_uart } <= { qq_uart, q_uart, i_uart_rx };
90 15 dgisselq
 
91
        // Keep track of the number of clocks since the last change.
92
        //
93
        // This is used to determine if we are in either a break or an idle
94
        // condition, as discussed further below.
95 21 dgisselq
        reg     [(TB-1):0]       chg_counter;
96
        initial chg_counter = {(TB){1'b1}};
97 15 dgisselq
        always @(posedge i_clk)
98
                if (qq_uart != ck_uart)
99 21 dgisselq
                        chg_counter <= 0;
100
                else if (chg_counter != { (TB){1'b1} })
101 15 dgisselq
                        chg_counter <= chg_counter + 1;
102
 
103
        // Are we in the middle of a baud iterval?  Specifically, are we
104
        // in the middle of a start bit?  Set this to high if so.  We'll use
105
        // this within our state machine to transition out of the IDLE
106
        // state.
107
        reg     half_baud_time;
108
        initial half_baud_time = 0;
109
        always @(posedge i_clk)
110 21 dgisselq
                half_baud_time <= (!ck_uart)&&(chg_counter >= half_baud-1'b1-1'b1);
111 15 dgisselq
 
112
 
113 17 dgisselq
        initial state = `RXUL_IDLE;
114 15 dgisselq
        always @(posedge i_clk)
115
        begin
116 17 dgisselq
                if (state == `RXUL_IDLE)
117 15 dgisselq
                begin // Idle state, independent of baud counter
118
                        // By default, just stay in the IDLE state
119 17 dgisselq
                        state <= `RXUL_IDLE;
120 21 dgisselq
                        if ((!ck_uart)&&(half_baud_time))
121 15 dgisselq
                                // UNLESS: We are in the center of a valid
122
                                // start bit
123 17 dgisselq
                                state <= `RXUL_BIT_ZERO;
124 21 dgisselq
                end else if ((state >= `RXUL_WAIT)&&(ck_uart))
125
                        state <= `RXUL_IDLE;
126
                else if (zero_baud_counter)
127 15 dgisselq
                begin
128 21 dgisselq
                        if (state <= `RXUL_STOP)
129 15 dgisselq
                                // Data arrives least significant bit first.
130
                                // By the time this is clocked in, it's what
131
                                // you'll have.
132
                                state <= state + 1;
133
                end
134
        end
135
 
136
        // Data bit capture logic.
137
        //
138
        // This is drastically simplified from the state machine above, based
139
        // upon: 1) it doesn't matter what it is until the end of a captured
140
        // byte, and 2) the data register will flush itself of any invalid
141
        // data in all other cases.  Hence, let's keep it real simple.
142
        reg     [7:0]    data_reg;
143
        always @(posedge i_clk)
144 21 dgisselq
                if ((zero_baud_counter)&&(state != `RXUL_STOP))
145
                        data_reg <= { qq_uart, data_reg[7:1] };
146 15 dgisselq
 
147
        // Our data bit logic doesn't need nearly the complexity of all that
148
        // work above.  Indeed, we only need to know if we are at the end of
149
        // a stop bit, in which case we copy the data_reg into our output
150
        // data register, o_data, and tell others (for one clock) that data is
151
        // available.
152
        //
153 21 dgisselq
        initial o_wr = 1'b0;
154 15 dgisselq
        initial o_data = 8'h00;
155
        always @(posedge i_clk)
156 21 dgisselq
                if ((zero_baud_counter)&&(state == `RXUL_STOP)&&(ck_uart))
157 15 dgisselq
                begin
158
                        o_wr   <= 1'b1;
159
                        o_data <= data_reg;
160
                end else
161
                        o_wr   <= 1'b0;
162
 
163
        // The baud counter
164
        //
165
        // This is used as a "clock divider" if you will, but the clock needs
166
        // to be reset before any byte can be decoded.  In all other respects,
167 18 dgisselq
        // we set ourselves up for CLOCKS_PER_BAUD counts between baud
168 15 dgisselq
        // intervals.
169 21 dgisselq
        initial baud_counter = 0;
170 15 dgisselq
        always @(posedge i_clk)
171 21 dgisselq
                if (((state==`RXUL_IDLE))&&(!ck_uart)&&(half_baud_time))
172 15 dgisselq
                        baud_counter <= CLOCKS_PER_BAUD-1'b1;
173 21 dgisselq
                else if (state == `RXUL_WAIT)
174
                        baud_counter <= 0;
175
                else if ((zero_baud_counter)&&(state < `RXUL_STOP))
176
                        baud_counter <= CLOCKS_PER_BAUD-1'b1;
177
                else if (!zero_baud_counter)
178 15 dgisselq
                        baud_counter <= baud_counter-1'b1;
179
 
180
        // zero_baud_counter
181
        //
182
        // Rather than testing whether or not (baud_counter == 0) within our
183
        // (already too complicated) state transition tables, we use
184
        // zero_baud_counter to pre-charge that test on the clock
185
        // before--cleaning up some otherwise difficult timing dependencies.
186 21 dgisselq
        initial zero_baud_counter = 1'b1;
187 15 dgisselq
        always @(posedge i_clk)
188 21 dgisselq
                if ((state == `RXUL_IDLE)&&(!ck_uart)&&(half_baud_time))
189 15 dgisselq
                        zero_baud_counter <= 1'b0;
190 21 dgisselq
                else if (state == `RXUL_WAIT)
191
                        zero_baud_counter <= 1'b1;
192
                else if ((zero_baud_counter)&&(state < `RXUL_STOP))
193
                        zero_baud_counter <= 1'b0;
194
                else if (baud_counter == 1)
195
                        zero_baud_counter <= 1'b1;
196
 
197
`ifdef  FORMAL
198
`define FORMAL_VERILATOR
199
`else
200
`ifdef  VERILATOR
201
`define FORMAL_VERILATOR
202
`endif
203
`endif
204
 
205
`ifdef  FORMAL
206
 
207
// `define PHASE_TWO    // SymbiYosys controls this definition
208
`define PHASE_ONE_ASSERT        assert
209
`define PHASE_TWO_ASSERT        assert
210
 
211
`ifdef  PHASE_TWO
212
`undef  PHASE_ONE_ASSERT
213
`define PHASE_ONE_ASSERT        assume
214
`endif
215
 
216
        localparam      F_CKRES = 10;
217
 
218
        wire                    f_tx_start, f_tx_busy;
219
        wire    [(F_CKRES-1):0]  f_tx_step;
220
        reg                     f_tx_zclk;
221
        reg     [(TB-1):0]       f_tx_timer;
222
        wire    [7:0]            f_rx_newdata;
223
        reg     [(TB-1):0]       f_tx_baud;
224
        wire                    f_tx_zbaud;
225
 
226
        wire    [(TB-1):0]       f_max_baud_difference;
227
        reg     [(TB-1):0]       f_baud_difference;
228
        reg     [(TB+3):0]       f_tx_count, f_rx_count;
229
        wire    [7:0]            f_tx_data;
230
 
231
 
232
 
233
        wire                    f_txclk;
234
        reg     [1:0]            f_rx_clock;
235
        reg     [(F_CKRES-1):0]  f_tx_clock;
236
        reg                     f_past_valid, f_past_valid_tx;
237
 
238
        initial f_past_valid = 1'b0;
239
        always @(posedge i_clk)
240
                f_past_valid <= 1'b1;
241
 
242
        initial f_rx_clock = 3'h0;
243
        always @($global_clock)
244
                f_rx_clock <= f_rx_clock + 1'b1;
245
 
246
        always @(*)
247
                assume(i_clk == f_rx_clock[1]);
248
 
249
 
250
 
251
        ///////////////////////////////////////////////////////////
252
        //
253
        //
254
        // Generate a transmitted signal
255
        //
256
        //
257
        ///////////////////////////////////////////////////////////
258
 
259
 
260
        // First, calculate the transmit clock
261
        localparam [(F_CKRES-1):0] F_MIDSTEP = { 2'b01, {(F_CKRES-2){1'b0}} };
262
        //
263
        // Need to allow us to slip by half a baud clock over 10 baud intervals
264
        //
265
        // (F_STEP / (2^F_CKRES)) * (CLOCKS_PER_BAUD)*10 < CLOCKS_PER_BAUD/2
266
        // F_STEP * 2 * 10 < 2^F_CKRES
267
        localparam [(F_CKRES-1):0] F_HALFSTEP= F_MIDSTEP/32;
268
        localparam [(F_CKRES-1):0] F_MINSTEP = F_MIDSTEP - F_HALFSTEP + 1;
269
        localparam [(F_CKRES-1):0] F_MAXSTEP = F_MIDSTEP + F_HALFSTEP - 1;
270
 
271
        initial assert(F_MINSTEP <= F_MIDSTEP);
272
        initial assert(F_MIDSTEP <= F_MAXSTEP);
273
 
274
        assign  f_tx_step = $anyconst;
275
        //      assume((f_tx_step >= F_MINSTEP)&&(f_tx_step <= F_MAXSTEP));
276
        //
277
        //
278
        always @(*) assume((f_tx_step == F_MINSTEP)
279
                        ||(f_tx_step == F_MIDSTEP)
280
                        ||(f_tx_step == F_MAXSTEP));
281
 
282
        // initial      rx_clock = $anyseq;
283
        always @($global_clock)
284
                f_tx_clock <= f_tx_clock + f_tx_step;
285
 
286
        assign  f_txclk = f_tx_clock[F_CKRES-1];
287
 
288
        // 
289
        initial f_past_valid_tx = 1'b0;
290
        always @(posedge f_txclk)
291
                f_past_valid_tx <= 1'b1;
292
 
293
        initial assume(i_uart_rx);
294
 
295
 
296
        //////////////////////////////////////////////
297
        //
298
        //
299
        // Build a simulated transmitter
300
        //
301
        //
302
        //////////////////////////////////////////////
303
        //
304
        // First, the simulated timing generator
305
 
306
        // parameter    TIMER_BITS = 10;
307
        // parameter [(TIMER_BITS-1):0] CLOCKS_PER_BAUD = 868;
308
        // localparam   TB = TIMER_BITS;
309
 
310
        assign  f_tx_start = $anyseq;
311
        always @(*)
312
        if (f_tx_busy)
313
                assume(!f_tx_start);
314
 
315
        initial f_tx_baud = 0;
316
        always @(posedge f_txclk)
317
        if ((f_tx_zbaud)&&((f_tx_busy)||(f_tx_start)))
318
                f_tx_baud <= CLOCKS_PER_BAUD-1'b1;
319
        else if (!f_tx_zbaud)
320
                f_tx_baud <= f_tx_baud - 1'b1;
321
 
322
        always @(*)
323
                `PHASE_ONE_ASSERT(f_tx_baud < CLOCKS_PER_BAUD);
324
 
325
        always @(*)
326
        if (!f_tx_busy)
327
                `PHASE_ONE_ASSERT(f_tx_baud == 0);
328
 
329
        assign  f_tx_zbaud = (f_tx_baud == 0);
330
 
331
        // Pick some data to transmit
332
        assign  f_tx_data = $anyseq;
333
 
334
        // But only if we aren't busy
335
        initial assume(f_tx_data == 0);
336
        always @(posedge f_txclk)
337
        if ((!f_tx_zbaud)||(f_tx_busy)||(!f_tx_start))
338
                assume(f_tx_data == $past(f_tx_data));
339
 
340
        // Force the data to change on a clock only
341
        always @($global_clock)
342
        if ((f_past_valid)&&(!$rose(f_txclk)))
343
                assume($stable(f_tx_data));
344
        else if (f_tx_busy)
345
                assume($stable(f_tx_data));
346
 
347
        //
348
        always @($global_clock)
349
        if ((!f_past_valid)||(!$rose(f_txclk)))
350
        begin
351
                assume($stable(f_tx_start));
352
                assume($stable(f_tx_data));
353
        end
354
 
355
        //
356
        //
357
        //
358
 
359
        reg     [9:0]    f_tx_reg;
360
        reg             f_tx_busy;
361
 
362
        // Here's the transmitter itself (roughly)
363
        initial f_tx_busy   = 1'b0;
364
        initial f_tx_reg    = 0;
365
        always @(posedge f_txclk)
366
        if (!f_tx_zbaud)
367
        begin
368
                `PHASE_ONE_ASSERT(f_tx_busy);
369
        end else begin
370
                f_tx_reg  <= { 1'b0, f_tx_reg[9:1] };
371
                if (f_tx_start)
372
                        f_tx_reg <= { 1'b1, f_tx_data, 1'b0 };
373
        end
374
 
375
        // Create a busy flag that we'll use
376
        always @(*)
377
        if (!f_tx_zbaud)
378
                f_tx_busy <= 1'b1;
379
        else if (|f_tx_reg)
380
                f_tx_busy <= 1'b1;
381
        else
382
                f_tx_busy <= 1'b0;
383
 
384
        //
385
        // Tie the TX register to the TX data
386
        always @(posedge f_txclk)
387
        if (f_tx_reg[9])
388
                `PHASE_ONE_ASSERT(f_tx_reg[8:0] == { f_tx_data, 1'b0 });
389
        else if (f_tx_reg[8])
390
                `PHASE_ONE_ASSERT(f_tx_reg[7:0] == f_tx_data[7:0] );
391
        else if (f_tx_reg[7])
392
                `PHASE_ONE_ASSERT(f_tx_reg[6:0] == f_tx_data[7:1] );
393
        else if (f_tx_reg[6])
394
                `PHASE_ONE_ASSERT(f_tx_reg[5:0] == f_tx_data[7:2] );
395
        else if (f_tx_reg[5])
396
                `PHASE_ONE_ASSERT(f_tx_reg[4:0] == f_tx_data[7:3] );
397
        else if (f_tx_reg[4])
398
                `PHASE_ONE_ASSERT(f_tx_reg[3:0] == f_tx_data[7:4] );
399
        else if (f_tx_reg[3])
400
                `PHASE_ONE_ASSERT(f_tx_reg[2:0] == f_tx_data[7:5] );
401
        else if (f_tx_reg[2])
402
                `PHASE_ONE_ASSERT(f_tx_reg[1:0] == f_tx_data[7:6] );
403
        else if (f_tx_reg[1])
404
                `PHASE_ONE_ASSERT(f_tx_reg[0] == f_tx_data[7]);
405
 
406
        // Our counter since we start
407
        initial f_tx_count = 0;
408
        always @(posedge f_txclk)
409
        if (!f_tx_busy)
410
                f_tx_count <= 0;
411
        else
412
                f_tx_count <= f_tx_count + 1'b1;
413
 
414
        always @(*)
415
        if (f_tx_reg == 10'h0)
416
                assume(i_uart_rx);
417
        else
418
                assume(i_uart_rx == f_tx_reg[0]);
419
 
420
        //
421
        // Make sure the absolute transmit clock timer matches our state
422
        //
423
        always @(posedge f_txclk)
424
        if (!f_tx_busy)
425
        begin
426
                if ((!f_past_valid_tx)||(!$past(f_tx_busy)))
427
                        `PHASE_ONE_ASSERT(f_tx_count == 0);
428
        end else if (f_tx_reg[9])
429
                `PHASE_ONE_ASSERT(f_tx_count ==
430
                                    CLOCKS_PER_BAUD -1 -f_tx_baud);
431
        else if (f_tx_reg[8])
432
                `PHASE_ONE_ASSERT(f_tx_count ==
433
                                2 * CLOCKS_PER_BAUD -1 -f_tx_baud);
434
        else if (f_tx_reg[7])
435
                `PHASE_ONE_ASSERT(f_tx_count ==
436
                                3 * CLOCKS_PER_BAUD -1 -f_tx_baud);
437
        else if (f_tx_reg[6])
438
                `PHASE_ONE_ASSERT(f_tx_count ==
439
                                4 * CLOCKS_PER_BAUD -1 -f_tx_baud);
440
        else if (f_tx_reg[5])
441
                `PHASE_ONE_ASSERT(f_tx_count ==
442
                                5 * CLOCKS_PER_BAUD -1 -f_tx_baud);
443
        else if (f_tx_reg[4])
444
                `PHASE_ONE_ASSERT(f_tx_count ==
445
                                6 * CLOCKS_PER_BAUD -1 -f_tx_baud);
446
        else if (f_tx_reg[3])
447
                `PHASE_ONE_ASSERT(f_tx_count ==
448
                                7 * CLOCKS_PER_BAUD -1 -f_tx_baud);
449
        else if (f_tx_reg[2])
450
                `PHASE_ONE_ASSERT(f_tx_count ==
451
                                8 * CLOCKS_PER_BAUD -1 -f_tx_baud);
452
        else if (f_tx_reg[1])
453
                `PHASE_ONE_ASSERT(f_tx_count ==
454
                                9 * CLOCKS_PER_BAUD -1 -f_tx_baud);
455
        else if (f_tx_reg[0])
456
                `PHASE_ONE_ASSERT(f_tx_count ==
457
                                10 * CLOCKS_PER_BAUD -1 -f_tx_baud);
458
        else
459
                `PHASE_ONE_ASSERT(f_tx_count ==
460
                                11 * CLOCKS_PER_BAUD -1 -f_tx_baud);
461
 
462
 
463
        ///////////////////////////////////////
464
        //
465
        // Receiver
466
        //
467
        ///////////////////////////////////////
468
        //
469
        // Count RX clocks since the start of the first stop bit, measured in
470
        // rx clocks
471
        initial f_rx_count = 0;
472
        always @(posedge i_clk)
473
        if (state == `RXUL_IDLE)
474
                f_rx_count = (!ck_uart) ? (chg_counter+2) : 0;
475
        else
476
                f_rx_count <= f_rx_count + 1'b1;
477
        always @(posedge i_clk)
478
        if (state == 0)
479
                `PHASE_ONE_ASSERT(f_rx_count
480
                                == half_baud + (CLOCKS_PER_BAUD-baud_counter));
481
        else if (state == 1)
482
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 2 * CLOCKS_PER_BAUD
483
                                        - baud_counter);
484
        else if (state == 2)
485
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 3 * CLOCKS_PER_BAUD
486
                                        - baud_counter);
487
        else if (state == 3)
488
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 4 * CLOCKS_PER_BAUD
489
                                        - baud_counter);
490
        else if (state == 4)
491
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 5 * CLOCKS_PER_BAUD
492
                                        - baud_counter);
493
        else if (state == 5)
494
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 6 * CLOCKS_PER_BAUD
495
                                        - baud_counter);
496
        else if (state == 6)
497
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 7 * CLOCKS_PER_BAUD
498
                                        - baud_counter);
499
        else if (state == 7)
500
                `PHASE_ONE_ASSERT(f_rx_count == half_baud + 8 * CLOCKS_PER_BAUD
501
                                        - baud_counter);
502
        else if (state == 8)
503
                `PHASE_ONE_ASSERT((f_rx_count == half_baud + 9 * CLOCKS_PER_BAUD
504
                                        - baud_counter)
505
                        ||(f_rx_count == half_baud + 10 * CLOCKS_PER_BAUD
506
                                        - baud_counter));
507
 
508
        always @(*)
509
                `PHASE_ONE_ASSERT( ((!zero_baud_counter)
510
                                &&(state == `RXUL_IDLE)
511
                                &&(baud_counter == 0))
512
                        ||((zero_baud_counter)&&(baud_counter == 0))
513
                        ||((!zero_baud_counter)&&(baud_counter != 0)));
514
 
515
        always @(posedge i_clk)
516
        if (!f_past_valid)
517
                `PHASE_ONE_ASSERT((state == `RXUL_IDLE)&&(baud_counter == 0)
518
                        &&(zero_baud_counter));
519
 
520
        always @(*)
521
        begin
522
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'h2);
523
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'h4);
524
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'h5);
525
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'h6);
526
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'h9);
527
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'ha);
528
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'hb);
529
                `PHASE_ONE_ASSERT({ ck_uart,qq_uart,q_uart,i_uart_rx } != 4'hd);
530
        end
531
 
532
        always @(posedge i_clk)
533
        if ((f_past_valid)&&($past(state) >= `RXUL_WAIT)&&($past(ck_uart)))
534
                `PHASE_ONE_ASSERT(state == `RXUL_IDLE);
535
 
536
        always @(posedge i_clk)
537
        if ((f_past_valid)&&($past(state) >= `RXUL_WAIT)
538
                        &&(($past(state) != `RXUL_IDLE)||(state == `RXUL_IDLE)))
539
                `PHASE_ONE_ASSERT(zero_baud_counter);
540
 
541
        // Calculate an absolute value of the difference between the two baud
542
        // clocks
543
`ifdef  PHASE_TWO
544
        always @(posedge i_clk)
545
        if ((f_past_valid)&&($past(state)==`RXUL_IDLE)&&(state == `RXUL_IDLE))
546
        begin
547
                `PHASE_TWO_ASSERT(($past(ck_uart))
548
                        ||(chg_counter <=
549
                                { 1'b0, CLOCKS_PER_BAUD[(TB-1):1] }));
550
        end
551
 
552
        always @(posedge f_txclk)
553
        if (!f_past_valid_tx)
554
                `PHASE_TWO_ASSERT((state == `RXUL_IDLE)&&(baud_counter == 0)
555
                        &&(zero_baud_counter)&&(!f_tx_busy));
556
 
557
        wire    [(TB+3):0]       f_tx_count_two_clocks_ago;
558
        assign  f_tx_count_two_clocks_ago = f_tx_count - 2;
559
        always @(*)
560
        if (f_tx_count >= f_rx_count + 2)
561
                f_baud_difference = f_tx_count_two_clocks_ago - f_rx_count;
562
        else
563
                f_baud_difference = f_rx_count - f_tx_count_two_clocks_ago;
564
 
565
        localparam      F_SYNC_DLY = 8;
566
 
567
        wire    [(TB+4+F_CKRES-1):0]     f_sub_baud_difference;
568
        reg     [F_CKRES-1:0]    ck_tx_clock;
569
        reg     [((F_SYNC_DLY-1)*F_CKRES)-1:0]   q_tx_clock;
570
        reg     [TB+3:0] ck_tx_count;
571
        reg     [(F_SYNC_DLY-1)*(TB+4)-1:0]      q_tx_count;
572
        initial q_tx_count = 0;
573
        initial ck_tx_count = 0;
574
        initial q_tx_clock = 0;
575
        initial ck_tx_clock = 0;
576
        always @($global_clock)
577
                { ck_tx_clock, q_tx_clock } <= { q_tx_clock, f_tx_clock };
578
        always @($global_clock)
579
                { ck_tx_count, q_tx_count } <= { q_tx_count, f_tx_count };
580
 
581
 
582
        wire    [TB+4+F_CKRES-1:0]       f_ck_tx_time, f_rx_time;
583
        always @(*)
584
                f_ck_tx_time = { ck_tx_count, !ck_tx_clock[F_CKRES-1],
585
                                                ck_tx_clock[F_CKRES-2:0] };
586
        always @(*)
587
                f_rx_time = { f_rx_count, !f_rx_clock[1], f_rx_clock[0],
588
                                                {(F_CKRES-2){1'b0}} };
589
 
590
        wire    [TB+4+F_CKRES-1:0]       f_signed_difference;
591
        always @(*)
592
                f_signed_difference = f_ck_tx_time - f_rx_time;
593
 
594
        always @(*)
595
        if (f_signed_difference[TB+4+F_CKRES-1])
596
                f_sub_baud_difference = -f_signed_difference;
597
        else
598
                f_sub_baud_difference =  f_signed_difference;
599
 
600
        always @($global_clock)
601
        if (state == `RXUL_WAIT)
602
                `PHASE_TWO_ASSERT((!f_tx_busy)||(f_tx_reg[9:1] == 0));
603
 
604
        always @($global_clock)
605
        if (state == `RXUL_IDLE)
606
        begin
607
                `PHASE_TWO_ASSERT((!f_tx_busy)||(f_tx_reg[9])||(f_tx_reg[9:1]==0));
608
                if (!ck_uart)
609
                        ;//`PHASE_TWO_ASSERT((f_rx_count < 4)||(f_sub_baud_difference <= ((CLOCKS_PER_BAUD<<F_CKRES)/20)));
610 15 dgisselq
                else
611 21 dgisselq
                        `PHASE_TWO_ASSERT((f_tx_reg[9:1]==0)||(f_tx_count < (3 + CLOCKS_PER_BAUD/2)));
612
        end else if (state == 0)
613
                `PHASE_TWO_ASSERT(f_sub_baud_difference
614
                                <=  2 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
615
        else if (state == 1)
616
                `PHASE_TWO_ASSERT(f_sub_baud_difference
617
                                <=  3 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
618
        else if (state == 2)
619
                `PHASE_TWO_ASSERT(f_sub_baud_difference
620
                                <=  4 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
621
        else if (state == 3)
622
                `PHASE_TWO_ASSERT(f_sub_baud_difference
623
                                <=  5 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
624
        else if (state == 4)
625
                `PHASE_TWO_ASSERT(f_sub_baud_difference
626
                                <=  6 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
627
        else if (state == 5)
628
                `PHASE_TWO_ASSERT(f_sub_baud_difference
629
                                <=  7 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
630
        else if (state == 6)
631
                `PHASE_TWO_ASSERT(f_sub_baud_difference
632
                                <=  8 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
633
        else if (state == 7)
634
                `PHASE_TWO_ASSERT(f_sub_baud_difference
635
                                <=  9 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
636
        else if (state == 8)
637
                `PHASE_TWO_ASSERT(f_sub_baud_difference
638
                                <= 10 * ((CLOCKS_PER_BAUD<<F_CKRES)/20));
639 15 dgisselq
 
640 21 dgisselq
        always @(posedge i_clk)
641
        if (o_wr)
642
                `PHASE_TWO_ASSERT(o_data == $past(f_tx_data,4));
643 15 dgisselq
 
644 21 dgisselq
        // always @(posedge i_clk)
645
        // if ((zero_baud_counter)&&(state != 4'hf)&&(CLOCKS_PER_BAUD > 6))
646
                // assert(i_uart_rx == ck_uart);
647
 
648
        // Make sure the data register matches
649
        always @(posedge i_clk)
650
        // if ((f_past_valid)&&(state != $past(state)))
651
        begin
652
                if (state == 4'h0)
653
                        `PHASE_TWO_ASSERT(!data_reg[7]);
654
 
655
                if (state == 4'h1)
656
                        `PHASE_TWO_ASSERT((data_reg[7]
657
                                == $past(f_tx_data[0]))&&(!data_reg[6]));
658
 
659
                if (state == 4'h2)
660
                        `PHASE_TWO_ASSERT(data_reg[7:6]
661
                                        == $past(f_tx_data[1:0]));
662
 
663
                if (state == 4'h3)
664
                        `PHASE_TWO_ASSERT(data_reg[7:5] == $past(f_tx_data[2:0]));
665
 
666
                if (state == 4'h4)
667
                        `PHASE_TWO_ASSERT(data_reg[7:4] == $past(f_tx_data[3:0]));
668
 
669
                if (state == 4'h5)
670
                        `PHASE_TWO_ASSERT(data_reg[7:3] == $past(f_tx_data[4:0]));
671
 
672
                if (state == 4'h6)
673
                        `PHASE_TWO_ASSERT(data_reg[7:2] == $past(f_tx_data[5:0]));
674
 
675
                if (state == 4'h7)
676
                        `PHASE_TWO_ASSERT(data_reg[7:1] == $past(f_tx_data[6:0]));
677
 
678
                if (state == 4'h8)
679
                        `PHASE_TWO_ASSERT(data_reg[7:0] == $past(f_tx_data[7:0]));
680
        end
681
 
682
        always @(posedge i_clk)
683
                cover(o_wr);
684
`endif
685
`endif
686
`ifdef  FORMAL_VERILATOR
687
        // FORMAL properties which can be tested via Verilator as well as
688
        // Yosys FORMAL
689
        always @(*)
690
                assert((state == 4'hf)||(state <= `RXUL_WAIT));
691
        always @(*)
692
                assert(zero_baud_counter == (baud_counter == 0)? 1'b1:1'b0);
693
        always @(*)
694
                assert(baud_counter <= CLOCKS_PER_BAUD-1'b1);
695
 
696
`endif
697
 
698 15 dgisselq
endmodule
699
 
700
 

powered by: WebSVN 2.1.0

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