uart_tx.v 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * uart_tx.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module uart_tx #(
  35. parameter integer DIV_WIDTH = 8
  36. )(
  37. output wire tx,
  38. input wire [7:0] data,
  39. input wire valid,
  40. output reg ack,
  41. input wire [DIV_WIDTH-1:0] div, // div - 2
  42. input wire clk,
  43. input wire rst
  44. );
  45. // Signals
  46. wire go, done, ce;
  47. reg active;
  48. reg [9:0] shift;
  49. reg [DIV_WIDTH:0] div_cnt;
  50. reg [4:0] bit_cnt;
  51. // Control
  52. assign go = valid & ~active;
  53. assign done = ce & bit_cnt[4];
  54. always @(posedge clk or posedge rst)
  55. if (rst)
  56. active <= 1'b0;
  57. else
  58. active <= (active & ~done) | go;
  59. // Baud rate generator
  60. always @(posedge clk)
  61. if (~active | div_cnt[DIV_WIDTH])
  62. div_cnt <= { 1'b0, div };
  63. else
  64. div_cnt <= div_cnt - 1;
  65. assign ce = div_cnt[DIV_WIDTH];
  66. // Bit counter
  67. always @(posedge clk)
  68. if (~active)
  69. bit_cnt <= 5'h08;
  70. else if (ce)
  71. bit_cnt <= bit_cnt - 1;
  72. // Shift register
  73. always @(posedge clk or posedge rst)
  74. if (rst)
  75. shift <= 10'h3ff;
  76. else if (go)
  77. shift <= { 1'b1, data, 1'b0 };
  78. else if (ce)
  79. shift <= { 1'b1, shift[9:1] };
  80. // Outputs
  81. always @(posedge clk)
  82. ack <= go;
  83. assign tx = shift[0];
  84. endmodule // uart_tx