-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.v
992 lines (814 loc) · 35.9 KB
/
uart.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
//////////////////////////////////////////////////////////////////
// //
// UART //
// //
// This file is part of the Amber project //
// http://www.opencores.org/project,amber //
// //
// Description //
// This is a synchronous UART meaning it uses the system //
// clock rather than having its own clock. This means the //
// standard UART Baud rates are approximated and not exact. //
// However the UART tandard provides for a 10% margin on //
// baud rates and this module is much more accurate than that. //
// //
// The Baud rate must be set before synthesis and is not //
// programmable. This keeps the UART small. //
// //
// The UART uses 8 data bits, 1 stop bit and no parity bits. //
// //
// The UART has a 16-byte transmit and a 16-byte receive FIFO //
// These FIFOs are implemented in flipflops - FPGAs have lots //
// of flipflops! //
// //
// Author(s): //
// - Conor Santifort, [email protected] //
// //
//////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2010 Authors and OPENCORES.ORG //
// //
// This source file may be used and distributed without //
// restriction provided that this copyright statement is not //
// removed from the file and that any derivative work contains //
// the original copyright notice and the associated disclaimer. //
// //
// This source file is free software; you can redistribute it //
// and/or modify it under the terms of the GNU Lesser General //
// Public License as published by the Free Software Foundation; //
// either version 2.1 of the License, or (at your option) any //
// later version. //
// //
// This source is distributed in the hope that it will be //
// useful, but WITHOUT ANY WARRANTY; without even the implied //
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU Lesser General Public License for more //
// details. //
// //
// You should have received a copy of the GNU Lesser General //
// Public License along with this source; if not, download it //
// from http://www.opencores.org/lgpl.shtml //
// //
//////////////////////////////////////////////////////////////////
`include "system_config_defines.v"
`include "global_defines.v"
// Normally AMBER_UART_BAUD is defined in the system_config_defines.v file.
`ifndef AMBER_UART_BAUD
`define AMBER_UART_BAUD 230400
`endif
module uart #(
parameter WB_DWIDTH = 32,
parameter WB_SWIDTH = 4
)(
input i_clk,
input [31:0] i_wb_adr,
input [WB_SWIDTH-1:0] i_wb_sel,
input i_wb_we,
output [WB_DWIDTH-1:0] o_wb_dat,
input [WB_DWIDTH-1:0] i_wb_dat,
input i_wb_cyc,
input i_wb_stb,
output o_wb_ack,
output o_wb_err,
output o_uart_int,
input i_uart_cts_n, // Clear To Send
output o_uart_txd, // Transmit data
output o_uart_rts_n, // Request to Send
input i_uart_rxd // Receive data
);
`include "register_addresses.v"
localparam [3:0] TXD_IDLE = 4'd0,
TXD_START = 4'd1,
TXD_DATA0 = 4'd2,
TXD_DATA1 = 4'd3,
TXD_DATA2 = 4'd4,
TXD_DATA3 = 4'd5,
TXD_DATA4 = 4'd6,
TXD_DATA5 = 4'd7,
TXD_DATA6 = 4'd8,
TXD_DATA7 = 4'd9,
TXD_STOP1 = 4'd10,
TXD_STOP2 = 4'd11,
TXD_STOP3 = 4'd12;
localparam [3:0] RXD_IDLE = 4'd0,
RXD_START = 4'd1,
RXD_START_MID = 4'd2,
RXD_START_MID1 = 4'd3,
RXD_DATA0 = 4'd4,
RXD_DATA1 = 4'd5,
RXD_DATA2 = 4'd6,
RXD_DATA3 = 4'd7,
RXD_DATA4 = 4'd8,
RXD_DATA5 = 4'd9,
RXD_DATA6 = 4'd10,
RXD_DATA7 = 4'd11,
RXD_STOP = 4'd12;
localparam RX_INTERRUPT_COUNT = 24'h3fffff;
// -------------------------------------------------------------------------
// Baud Rate Configuration
// -------------------------------------------------------------------------
`ifndef Veritak
localparam real UART_BAUD = `AMBER_UART_BAUD; // Hz
`ifdef XILINX_VIRTEX6_FPGA
localparam real CLK_FREQ = 1200.0 / `AMBER_CLK_DIVIDER ; // MHz
`else
localparam real CLK_FREQ = 800.0 / `AMBER_CLK_DIVIDER ; // MHz
//localparam real CLK_FREQ = 50.0 / `AMBER_CLK_DIVIDER ; // MHz
`endif
localparam real UART_BIT_PERIOD = 1000000000 / UART_BAUD; // nS
//localparam real UART_BIT_PERIOD = 50000000 / UART_BAUD; // nS
localparam real UART_WORD_PERIOD = ( UART_BIT_PERIOD * 12 ); // nS
localparam real CLK_PERIOD = 1000 / CLK_FREQ; // nS
localparam real CLKS_PER_WORD = UART_WORD_PERIOD / CLK_PERIOD;
localparam real CLKS_PER_BIT = CLKS_PER_WORD / 12;
// These are rounded to the nearest whole number
// i.e. 29.485960 -> 29
// 29.566303 -> 30
localparam [9:0] TX_BITPULSE_COUNT = CLKS_PER_BIT;
localparam [9:0] TX_CLKS_PER_WORD = CLKS_PER_WORD;
`else
localparam [9:0] TX_BITPULSE_COUNT = 30;
localparam [9:0] TX_CLKS_PER_WORD = 360;
`endif
localparam [9:0] TX_BITADJUST_COUNT = TX_CLKS_PER_WORD - 11*TX_BITPULSE_COUNT;
localparam [9:0] RX_BITPULSE_COUNT = TX_BITPULSE_COUNT-2;
localparam [9:0] RX_HALFPULSE_COUNT = TX_BITPULSE_COUNT/2 - 4;
// -------------------------------------------------------------------------
reg tx_interrupt = 'd0;
reg rx_interrupt = 'd0;
reg [23:0] rx_int_timer = 'd0;
wire fifo_enable;
reg [7:0] tx_fifo [0:15];
wire tx_fifo_full;
wire tx_fifo_empty;
wire tx_fifo_half_or_less_full;
wire tx_fifo_push;
wire tx_fifo_push_not_full;
wire tx_fifo_pop_not_empty;
reg [4:0] tx_fifo_wp = 'd0;
reg [4:0] tx_fifo_rp = 'd0;
reg [4:0] tx_fifo_count = 'd0; // number of entries in the fifo
reg tx_fifo_full_flag = 'd0;
reg [7:0] rx_fifo [0:15];
reg rx_fifo_empty = 1'd1;
reg rx_fifo_full = 1'd0;
wire rx_fifo_half_or_more; // true when half full or greater
reg [4:0] rx_fifo_count = 'd0; // number of entries in the fifo
wire rx_fifo_push;
wire rx_fifo_push_not_full;
wire rx_fifo_pop;
wire rx_fifo_pop_not_empty;
reg [4:0] rx_fifo_wp = 'd0;
reg [4:0] rx_fifo_rp = 'd0;
wire [7:0] tx_byte;
reg [3:0] txd_state = TXD_IDLE;
reg txd = 1'd1;
reg tx_bit_pulse = 'd0;
reg [9:0] tx_bit_pulse_count = 'd0;
reg [7:0] rx_byte = 'd0;
reg [3:0] rxd_state = RXD_IDLE;
wire rx_start;
reg rxen = 'd0;
reg [9:0] rx_bit_pulse_count = 'd0;
reg restart_rx_bit_count = 'd0;
reg [4:0] rxd_d = 5'h1f;
reg [3:0] uart0_cts_n_d = 4'hf;
// Wishbone registers
reg [7:0] uart_rsr_reg = 'd0; // Receive status, (Write) Error Clear
reg [7:0] uart_lcrh_reg = 'd0; // Line Control High Byte
reg [7:0] uart_lcrm_reg = 'd0; // Line Control Middle Byte
reg [7:0] uart_lcrl_reg = 'd0; // Line Control Low Byte
reg [7:0] uart_cr_reg = 'd0; // Control Register
// Wishbone interface
reg [31:0] wb_rdata32 = 'd0;
wire wb_start_write;
wire wb_start_read;
reg wb_start_read_d1 = 'd0;
wire [31:0] wb_wdata32;
integer i;
// ======================================================
// Wishbone Interface
// ======================================================
// Can't start a write while a read is completing. The ack for the read cycle
// needs to be sent first
assign wb_start_write = i_wb_stb && i_wb_we && !wb_start_read_d1;
assign wb_start_read = i_wb_stb && !i_wb_we && !o_wb_ack;
always @( posedge i_clk )
wb_start_read_d1 <= wb_start_read;
assign o_wb_err = 1'd0;
assign o_wb_ack = i_wb_stb && ( wb_start_write || wb_start_read_d1 );
generate
if (WB_DWIDTH == 128)
begin : wb128
assign wb_wdata32 = i_wb_adr[3:2] == 2'd3 ? i_wb_dat[127:96] :
i_wb_adr[3:2] == 2'd2 ? i_wb_dat[ 95:64] :
i_wb_adr[3:2] == 2'd1 ? i_wb_dat[ 63:32] :
i_wb_dat[ 31: 0] ;
assign o_wb_dat = {4{wb_rdata32}};
end
else
begin : wb32
assign wb_wdata32 = i_wb_dat;
assign o_wb_dat = wb_rdata32;
end
endgenerate
// ======================================================
// UART 0 Receive FIFO
// ======================================================
assign rx_fifo_pop = wb_start_read && i_wb_adr[15:0] == AMBER_UART_DR;
assign rx_fifo_push_not_full = rx_fifo_push && !rx_fifo_full;
assign rx_fifo_pop_not_empty = rx_fifo_pop && !rx_fifo_empty;
assign rx_fifo_half_or_more = rx_fifo_count >= 5'd8;
always @ ( posedge i_clk )
begin
if ( fifo_enable )
begin
// RX FIFO Push
if ( rx_fifo_push_not_full )
begin
rx_fifo[rx_fifo_wp[3:0]] <= rx_byte;
rx_fifo_wp <= rx_fifo_wp + 1'd1;
end
if ( rx_fifo_pop_not_empty )
begin
rx_fifo_rp <= rx_fifo_rp + 1'd1;
end
if ( rx_fifo_push_not_full && !rx_fifo_pop_not_empty )
rx_fifo_count <= rx_fifo_count + 1'd1;
else if ( rx_fifo_pop_not_empty && !rx_fifo_push_not_full )
rx_fifo_count <= rx_fifo_count - 1'd1;
rx_fifo_full <= rx_fifo_wp == {~rx_fifo_rp[4], rx_fifo_rp[3:0]};
rx_fifo_empty <= rx_fifo_wp == rx_fifo_rp;
if ( rx_fifo_empty || rx_fifo_pop )
rx_int_timer <= 'd0;
else if ( rx_int_timer != RX_INTERRUPT_COUNT )
rx_int_timer <= rx_int_timer + 1'd1;
end
else // No FIFO
begin
rx_int_timer <= 'd0;
if ( rx_fifo_push )
begin
rx_fifo[0] <= rx_byte;
rx_fifo_empty <= 1'd0;
rx_fifo_full <= 1'd1;
end
else if ( rx_fifo_pop )
begin
rx_fifo_empty <= 1'd1;
rx_fifo_full <= 1'd0;
end
end
end
// ======================================================
// Transmit Interrupts
// ======================================================
// UART 0 Transmit Interrupt
always @ ( posedge i_clk )
begin
// Clear the interrupt
if ( wb_start_write && i_wb_adr[15:0] == AMBER_UART_ICR )
tx_interrupt <= 1'd0;
// Set the interrupt
else if ( fifo_enable )
// This interrupt clears automatically as bytes are pushed into the tx fifo
// cr bit 5 is Transmit Interrupt Enable
tx_interrupt <= tx_fifo_half_or_less_full && uart_cr_reg[5];
else
// This interrupt clears automatically when a byte is written to tx
// cr bit 5 is Transmit Interrupt Enable
tx_interrupt <= tx_fifo_empty && uart_cr_reg[5];
end
// ======================================================
// Receive Interrupts
// ======================================================
always @ ( posedge i_clk )
if (fifo_enable)
rx_interrupt <= rx_fifo_half_or_more || rx_int_timer == RX_INTERRUPT_COUNT;
else
rx_interrupt <= rx_fifo_full;
assign o_uart_int = ( tx_interrupt & uart_cr_reg[5] ) | // UART transmit interrupt w/ enable
( rx_interrupt & uart_cr_reg[4] ) ; // UART receive interrupt w/ enable
assign fifo_enable = uart_lcrh_reg[4];
// ========================================================
// UART Transmit
// ========================================================
assign o_uart_txd = txd;
assign tx_fifo_full = fifo_enable ? tx_fifo_count >= 5'd16 : tx_fifo_full_flag;
assign tx_fifo_empty = fifo_enable ? tx_fifo_count == 5'd00 : !tx_fifo_full_flag;
assign tx_fifo_half_or_less_full = tx_fifo_count <= 5'd8;
assign tx_byte = fifo_enable ? tx_fifo[tx_fifo_rp[3:0]] : tx_fifo[0] ;
assign tx_fifo_push = wb_start_write && i_wb_adr[15:0] == AMBER_UART_DR;
assign tx_fifo_push_not_full = tx_fifo_push && !tx_fifo_full;
assign tx_fifo_pop_not_empty = txd_state == TXD_STOP3 && tx_bit_pulse == 1'd1 && !tx_fifo_empty;
// Transmit FIFO
always @( posedge i_clk )
begin
// Use 8-entry FIFO
if ( fifo_enable )
begin
// Push
if ( tx_fifo_push_not_full )
begin
tx_fifo[tx_fifo_wp[3:0]] <= wb_wdata32[7:0];
tx_fifo_wp <= tx_fifo_wp + 1'd1;
end
// Pop
if ( tx_fifo_pop_not_empty )
tx_fifo_rp <= tx_fifo_rp + 1'd1;
// Count up
if (tx_fifo_push_not_full && !tx_fifo_pop_not_empty)
tx_fifo_count <= tx_fifo_count + 1'd1;
// Count down
else if (tx_fifo_pop_not_empty && !tx_fifo_push_not_full)
tx_fifo_count <= tx_fifo_count - 1'd1;
end
// Do not use 8-entry FIFO, single entry register instead
else
begin
// Clear FIFO values
tx_fifo_wp <= 'd0;
tx_fifo_rp <= 'd0;
tx_fifo_count <= 'd0;
// Push
if ( tx_fifo_push_not_full )
begin
tx_fifo[0] <= wb_wdata32[7:0];
tx_fifo_full_flag <= 1'd1;
end
// Pop
else if ( tx_fifo_pop_not_empty )
tx_fifo_full_flag <= 1'd0;
end
end
// ========================================================
// Register Clear to Send Input
// ========================================================
always @( posedge i_clk )
uart0_cts_n_d <= {uart0_cts_n_d[2:0], i_uart_cts_n};
// ========================================================
// Transmit Pulse generater - matches baud rate
// ========================================================
always @( posedge i_clk )
if (( tx_bit_pulse_count == (TX_BITADJUST_COUNT-1) && txd_state == TXD_STOP2 ) ||
( tx_bit_pulse_count == (TX_BITPULSE_COUNT-1) && txd_state != TXD_STOP2 ) )
begin
tx_bit_pulse_count <= 'd0;
tx_bit_pulse <= 1'd1;
end
else
begin
tx_bit_pulse_count <= tx_bit_pulse_count + 1'd1;
tx_bit_pulse <= 1'd0;
end
// ========================================================
// Byte Transmitted
// ========================================================
// Idle state, txd = 1
// start bit, txd = 0
// Data x 8, lsb first
// stop bit, txd = 1
// X = 0x58 = 01011000
always @( posedge i_clk )
if ( tx_bit_pulse )
case ( txd_state )
TXD_IDLE :
begin
txd <= 1'd1;
if ( uart0_cts_n_d[3:1] == 3'b000 && !tx_fifo_empty )
txd_state <= TXD_START;
end
TXD_START :
begin
txd <= 1'd0;
txd_state <= TXD_DATA0;
end
TXD_DATA0 :
begin
txd <= tx_byte[0];
txd_state <= TXD_DATA1;
end
TXD_DATA1 :
begin
txd <= tx_byte[1];
txd_state <= TXD_DATA2;
end
TXD_DATA2 :
begin
txd <= tx_byte[2];
txd_state <= TXD_DATA3;
end
TXD_DATA3 :
begin
txd <= tx_byte[3];
txd_state <= TXD_DATA4;
end
TXD_DATA4 :
begin
txd <= tx_byte[4];
txd_state <= TXD_DATA5;
end
TXD_DATA5 :
begin
txd <= tx_byte[5];
txd_state <= TXD_DATA6;
end
TXD_DATA6 :
begin
txd <= tx_byte[6];
txd_state <= TXD_DATA7;
end
TXD_DATA7 :
begin
txd <= tx_byte[7];
txd_state <= TXD_STOP1;
end
TXD_STOP1 :
begin
txd <= 1'd1;
txd_state <= TXD_STOP2;
end
TXD_STOP2 :
begin
txd <= 1'd1;
txd_state <= TXD_STOP3;
end
TXD_STOP3 :
begin
txd <= 1'd1;
txd_state <= TXD_IDLE;
end
default :
begin
txd <= 1'd1;
end
endcase
// ========================================================
// UART Receive
// ========================================================
assign o_uart_rts_n = ~rxen;
assign rx_fifo_push = rxd_state == RXD_STOP && rx_bit_pulse_count == 10'd0;
// ========================================================
// Receive bit pulse
// ========================================================
// Pulse generater - matches baud rate
always @( posedge i_clk )
if ( restart_rx_bit_count )
rx_bit_pulse_count <= 'd0;
else
rx_bit_pulse_count <= rx_bit_pulse_count + 1'd1;
// ========================================================
// Detect 1->0 transition. Filter out glitches and jaggedy transitions
// ========================================================
always @( posedge i_clk )
rxd_d[4:0] <= {rxd_d[3:0], i_uart_rxd};
assign rx_start = rxd_d[4:3] == 2'b11 && rxd_d[1:0] == 2'b00;
// ========================================================
// Receive state machine
// ========================================================
always @( posedge i_clk )
case ( rxd_state )
RXD_IDLE :
if ( rx_fifo_full )
rxen <= 1'd0;
else
begin
rxd_state <= RXD_START;
rxen <= 1'd1;
restart_rx_bit_count <= 1'd1;
rx_byte <= 'd0;
end
RXD_START :
// Filter out glitches and jaggedy transitions
if ( rx_start )
begin
rxd_state <= RXD_START_MID1;
restart_rx_bit_count <= 1'd1;
end
else
restart_rx_bit_count <= 1'd0;
// This state just delays the check on the
// rx_bit_pulse_count value by 1 clock cycle to
// give it time to reset
RXD_START_MID1 :
rxd_state <= RXD_START_MID;
RXD_START_MID :
if ( rx_bit_pulse_count == RX_HALFPULSE_COUNT )
begin
rxd_state <= RXD_DATA0;
restart_rx_bit_count <= 1'd1;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA0 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA1;
restart_rx_bit_count <= 1'd1;
rx_byte[0] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA1 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA2;
restart_rx_bit_count <= 1'd1;
rx_byte[1] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA2 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA3;
restart_rx_bit_count <= 1'd1;
rx_byte[2] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA3 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA4;
restart_rx_bit_count <= 1'd1;
rx_byte[3] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA4 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA5;
restart_rx_bit_count <= 1'd1;
rx_byte[4] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA5 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA6;
restart_rx_bit_count <= 1'd1;
rx_byte[5] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA6 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_DATA7;
restart_rx_bit_count <= 1'd1;
rx_byte[6] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_DATA7 :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT )
begin
rxd_state <= RXD_STOP;
restart_rx_bit_count <= 1'd1;
rx_byte[7] <= i_uart_rxd;
end
else
restart_rx_bit_count <= 1'd0;
RXD_STOP :
if ( rx_bit_pulse_count == RX_BITPULSE_COUNT ) // half way through stop bit
begin
rxd_state <= RXD_IDLE;
restart_rx_bit_count <= 1'd1;
end
else
restart_rx_bit_count <= 1'd0;
default :
begin
rxd_state <= RXD_IDLE;
end
endcase
// ========================================================
// Register Writes
// ========================================================
always @( posedge i_clk )
if ( wb_start_write )
case ( i_wb_adr[15:0] )
// Receive status, (Write) Error Clear
AMBER_UART_RSR: uart_rsr_reg <= wb_wdata32[7:0];
// Line Control High Byte
AMBER_UART_LCRH: uart_lcrh_reg <= wb_wdata32[7:0];
// Line Control Middle Byte
AMBER_UART_LCRM: uart_lcrm_reg <= wb_wdata32[7:0];
// Line Control Low Byte
AMBER_UART_LCRL: uart_lcrl_reg <= wb_wdata32[7:0];
// Control Register
AMBER_UART_CR: uart_cr_reg <= wb_wdata32[7:0];
endcase
// ========================================================
// Register Reads
// ========================================================
always @( posedge i_clk )
if ( wb_start_read )
case ( i_wb_adr[15:0] )
AMBER_UART_CID0: wb_rdata32 <= 32'h0d;
AMBER_UART_CID1: wb_rdata32 <= 32'hf0;
AMBER_UART_CID2: wb_rdata32 <= 32'h05;
AMBER_UART_CID3: wb_rdata32 <= 32'hb1;
AMBER_UART_PID0: wb_rdata32 <= 32'h10;
AMBER_UART_PID1: wb_rdata32 <= 32'h10;
AMBER_UART_PID2: wb_rdata32 <= 32'h04;
AMBER_UART_PID3: wb_rdata32 <= 32'h00;
AMBER_UART_DR: // Rx data
if ( fifo_enable )
wb_rdata32 <= {24'd0, rx_fifo[rx_fifo_rp[3:0]]};
else
wb_rdata32 <= {24'd0, rx_fifo[0]};
AMBER_UART_RSR: wb_rdata32 <= uart_rsr_reg; // Receive status, (Write) Error Clear
AMBER_UART_LCRH: wb_rdata32 <= uart_lcrh_reg; // Line Control High Byte
AMBER_UART_LCRM: wb_rdata32 <= uart_lcrm_reg; // Line Control Middle Byte
AMBER_UART_LCRL: wb_rdata32 <= uart_lcrl_reg; // Line Control Low Byte
AMBER_UART_CR: wb_rdata32 <= uart_cr_reg; // Control Register
// UART Tx/Rx Status
AMBER_UART_FR: wb_rdata32 <= {tx_fifo_empty, // tx fifo empty
rx_fifo_full, // rx fifo full
tx_fifo_full, // tx fifo full
rx_fifo_empty, // rx fifo empty
!tx_fifo_empty, // uart busy
1'd1, // !Data Carrier Detect
1'd1, // !Data Set Ready
!uart0_cts_n_d[3] // !Clear to Send
}; // Flag Register
// Interrupt Status
AMBER_UART_IIR: wb_rdata32 <= {5'd0,
1'd0, // RTIS - receive timeout interrupt
tx_interrupt, // TIS - transmit interrupt status
rx_interrupt, // RIS - receive interrupt status
1'd0 // Modem interrupt status
}; // (Write) Clear Int
default: wb_rdata32 <= 32'h00c0ffee;
endcase
// =======================================================================================
// =======================================================================================
// =======================================================================================
// Non-synthesizable debug code
// =======================================================================================
//synopsys translate_off
// ========================================================
// Report UART Register accesses
// ========================================================
`ifndef Veritak
// Check in case UART approximate period
// is too far off
initial
begin
if ((( TX_BITPULSE_COUNT * CLK_PERIOD ) > (UART_BIT_PERIOD * 1.03) ) ||
(( TX_BITPULSE_COUNT * CLK_PERIOD ) < (UART_BIT_PERIOD * 0.97) ) )
begin
`TB_ERROR_MESSAGE
$display("UART TX bit period, %.1f, is too big. UART will not work!", TX_BITPULSE_COUNT * CLK_PERIOD);
$display("Baud rate is %f, and baud bit period is %.1f", UART_BAUD, UART_BIT_PERIOD);
$display("Either reduce the baud rate, or increase the system clock frequency");
$display("------");
end
end
`endif
`ifdef AMBER_UART_DEBUG
wire wb_read_ack;
reg uart0_rx_int_d1 = 'd0;
assign wb_read_ack = i_wb_stb && !i_wb_we && o_wb_ack;
`ifndef Veritak
initial
begin
$display("%m UART period = %f nS, want %f nS, %d, %d",
(TX_BITPULSE_COUNT*11 + TX_BITADJUST_COUNT) * CLK_PERIOD,
UART_WORD_PERIOD,
TX_BITPULSE_COUNT, TX_BITADJUST_COUNT);
end
`endif
// Transmit Interrupts
always @ ( posedge i_clk )
if ( wb_start_write && i_wb_adr[15:0] == AMBER_UART_ICR )
;
else if ( fifo_enable )
begin
if (tx_interrupt == 1'd0 && tx_fifo_half_or_less_full && uart_cr_reg[5])
$display("%m: tx_interrupt Interrupt Set with FIFO enabled");
if (tx_interrupt == 1'd1 && !(tx_fifo_half_or_less_full && uart_cr_reg[5]))
$display("%m: tx_interrupt Interrupt Cleared with FIFO enabled");
end
else
begin
if (tx_interrupt == 1'd0 && tx_fifo_empty && uart_cr_reg[5])
$display("%m: tx_interrupt Interrupt Set with FIFO disabled");
if (tx_interrupt == 1'd1 && !(tx_fifo_empty && uart_cr_reg[5]))
$display("%m: tx_interrupt Interrupt Cleared with FIFO disabled");
end
// Receive Interrupts
always @ ( posedge i_clk )
begin
uart0_rx_int_d1 <= rx_interrupt;
if ( rx_interrupt && !uart0_rx_int_d1 )
begin
`TB_DEBUG_MESSAGE
$display("rx_interrupt Interrupt fifo_enable %d, rx_fifo_full %d",
fifo_enable, rx_fifo_full);
$display("rx_fifo_half_or_more %d, rx_int_timer 0x%08h, rx_fifo_count %d",
rx_fifo_half_or_more, rx_int_timer, rx_fifo_count);
end
if ( !rx_interrupt && uart0_rx_int_d1 )
begin
`TB_DEBUG_MESSAGE
$display("rx_interrupt Interrupt Cleared fifo_enable %d, rx_fifo_full %d",
fifo_enable, rx_fifo_full);
$display(" rx_fifo_half_or_more %d, rx_int_timer 0x%08h, rx_fifo_count %d",
rx_fifo_half_or_more, rx_int_timer, rx_fifo_count);
end
end
always @( posedge i_clk )
if ( wb_read_ack || wb_start_write )
begin
`TB_DEBUG_MESSAGE
if ( wb_start_write )
$write("Write 0x%08x to ", wb_wdata32);
else
$write("Read 0x%08x from ", o_wb_dat);
case ( i_wb_adr[15:0] )
AMBER_UART_PID0: $write("UART PID0 register");
AMBER_UART_PID1: $write("UART PID1 register");
AMBER_UART_PID2: $write("UART PID2 register");
AMBER_UART_PID3: $write("UART PID3 register");
AMBER_UART_CID0: $write("UART CID0 register");
AMBER_UART_CID1: $write("UART CID1 register");
AMBER_UART_CID2: $write("UART CID2 register");
AMBER_UART_CID3: $write("UART CID3 register");
AMBER_UART_DR: $write("UART Tx/Rx char %c", wb_start_write ? wb_wdata32[7:0] : o_wb_dat[7:0] );
AMBER_UART_RSR: $write("UART (Read) Receive status, (Write) Error Clear");
AMBER_UART_LCRH: $write("UART Line Control High Byte");
AMBER_UART_LCRM: $write("UART Line Control Middle Byte");
AMBER_UART_LCRL: $write("UART Line Control Low Byte");
AMBER_UART_CR: $write("UART Control Register");
AMBER_UART_FR: $write("UART Flag Register");
AMBER_UART_IIR: $write("UART (Read) Interrupt Identification Register");
default:
begin
`TB_ERROR_MESSAGE
$write("Unknown UART Register region");
end
endcase
$write(", Address 0x%08h\n", i_wb_adr);
end
`endif
// ========================================================
// Assertions
// ========================================================
always @ ( posedge i_clk )
begin
if ( rx_fifo_pop && !rx_fifo_push && rx_fifo_empty )
begin
`TB_WARNING_MESSAGE
$write("UART rx FIFO underflow\n");
end
if ( !rx_fifo_pop && rx_fifo_push && rx_fifo_full )
begin
`TB_WARNING_MESSAGE
$write("UART rx FIFO overflow\n");
end
if ( tx_fifo_push && tx_fifo_full )
begin
`TB_WARNING_MESSAGE
$display("UART tx FIFO overflow - char = %c", wb_wdata32[7:0]);
end
end
// ========================================================
// Debug State Machines
// ========================================================
wire [(10*8)-1:0] xTXD_STATE;
wire [(14*8)-1:0] xRXD_STATE;
assign xTXD_STATE = txd_state == TXD_IDLE ? "TXD_IDLE" :
txd_state == TXD_START ? "TXD_START" :
txd_state == TXD_DATA0 ? "TXD_DATA0" :
txd_state == TXD_DATA1 ? "TXD_DATA1" :
txd_state == TXD_DATA2 ? "TXD_DATA2" :
txd_state == TXD_DATA3 ? "TXD_DATA3" :
txd_state == TXD_DATA4 ? "TXD_DATA4" :
txd_state == TXD_DATA5 ? "TXD_DATA5" :
txd_state == TXD_DATA6 ? "TXD_DATA6" :
txd_state == TXD_DATA7 ? "TXD_DATA7" :
txd_state == TXD_STOP1 ? "TXD_STOP1" :
txd_state == TXD_STOP2 ? "TXD_STOP2" :
txd_state == TXD_STOP3 ? "TXD_STOP3" :
"UNKNOWN" ;
assign xRXD_STATE = rxd_state == RXD_IDLE ? "RXD_IDLE" :
rxd_state == RXD_START ? "RXD_START" :
rxd_state == RXD_START_MID1 ? "RXD_START_MID1":
rxd_state == RXD_START_MID ? "RXD_START_MID" :
rxd_state == RXD_DATA0 ? "RXD_DATA0" :
rxd_state == RXD_DATA1 ? "RXD_DATA1" :
rxd_state == RXD_DATA2 ? "RXD_DATA2" :
rxd_state == RXD_DATA3 ? "RXD_DATA3" :
rxd_state == RXD_DATA4 ? "RXD_DATA4" :
rxd_state == RXD_DATA5 ? "RXD_DATA5" :
rxd_state == RXD_DATA6 ? "RXD_DATA6" :
rxd_state == RXD_DATA7 ? "RXD_DATA7" :
rxd_state == RXD_STOP ? "RXD_STOP" :
"UNKNOWN" ;
//synopsys translate_on
endmodule