Guest User

UART_RX_8N1.v

a guest
Oct 22nd, 2013
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module UART_Receiver_8N1_v1 (
  2.     input wire clk_in,
  3.     input wire rx_in,
  4.     input wire reset_in,
  5.     output wire [7:0] data,
  6.     output wire data_ready
  7. );
  8.  
  9. parameter clkFreq = 49152000;
  10. parameter baudRate = 115200;
  11. localparam BCONST = clkFreq/baudRate;
  12. localparam baudDividerWidth = $clog2(BCONST+1);
  13.  
  14. // SHIFT REGISTER
  15. wire shift_in;
  16. wire shift_enable;
  17. wire shift_clk;
  18. reg [7:0] shift_reg;
  19.  
  20. always @(posedge shift_clk)
  21. begin
  22.     if (shift_enable)
  23.         shift_reg <= {shift_in, shift_reg[7:1]};
  24. end
  25.  
  26. // BITS COUNTER
  27. wire bits_count_clk;
  28. wire bits_count_enable;
  29. wire bits_count_sclr;
  30. wire bits_count_cout;
  31. reg [3:0] bits_count;
  32.  
  33. always @(posedge bits_count_clk)
  34. begin
  35.     if (bits_count_sclr)
  36.         bits_count <= 4'd0;
  37.     else if (bits_count_enable)
  38.     begin
  39.         if (bits_count==4'd9)
  40.             bits_count <= 4'd0;
  41.         else
  42.             bits_count <= bits_count + 1'd1;
  43.     end
  44. end
  45. assign bits_count_cout = (bits_count==4'd9);
  46.  
  47. // BAUD COUNTER
  48. wire baud_count_clk;
  49. wire baud_count_sload;
  50. wire baud_count_cout;
  51. wire [baudDividerWidth-1:0] baud_count_data;
  52. reg [baudDividerWidth-1:0] baud_count;
  53.  
  54. always @(posedge baud_count_clk)
  55. begin
  56.     if (baud_count_sload)
  57.         baud_count <= baud_count_data;
  58.     else if (baud_count==BCONST)
  59.         baud_count <= 0;
  60.     else
  61.         baud_count <= baud_count + 1'd1;
  62. end
  63. assign baud_count_cout = (baud_count==BCONST);
  64.  
  65. // SRFF
  66. wire SRFF_s;
  67. wire SRFF_r;
  68. wire SRFF_clk;
  69. reg SRFF_q;
  70. always @(posedge SRFF_clk)
  71. begin
  72.     if (SRFF_s)
  73.         SRFF_q = 1'b1;
  74.     if (SRFF_r)
  75.         SRFF_q = 1'b0;
  76. end
  77.  
  78. // RX SYNCRONIZER
  79. wire rx_sync_in;
  80. wire rx_sync_clk;
  81. reg [1:0] rx_sync;
  82. always @(posedge rx_sync_clk)
  83. begin
  84.     rx_sync <= {rx_sync[0],rx_sync_in};
  85. end
  86.  
  87. // CONNECTION & LOGIC
  88. // rx syn
  89. assign rx_sync_clk = clk_in;
  90. assign rx_sync_in = rx_in;
  91. // shift reg
  92. assign shift_in = rx_sync[0];
  93. assign shift_clk = clk_in;
  94. assign shift_enable = baud_count_cout;
  95. // srff
  96. assign SRFF_clk = clk_in;
  97. assign SRFF_s = rx_sync[1] & ~rx_sync[0];
  98. assign SRFF_r = baud_count_cout & bits_count_cout;
  99. // baud counter
  100. assign baud_count_clk = clk_in;
  101. assign baud_count_data = BCONST/2;
  102. assign baud_count_sload = SRFF_s & ~SRFF_q;
  103. // bits counter
  104. assign bits_count_clk = clk_in;
  105. assign bits_count_sclr = baud_count_sload;
  106. assign bits_count_enable = baud_count_cout;
  107. // module
  108. assign data_ready = SRFF_r & SRFF_q;
  109. assign data = shift_reg;
  110.  
  111. endmodule
Advertisement
Add Comment
Please, Sign In to add comment