uart_tb.v 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * uart_tb.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. `timescale 1ns / 100ps
  35. module uart_tb;
  36. // Signals
  37. reg rst = 1'b1;
  38. reg clk_rx = 1'b0;
  39. reg clk_tx = 1'b0;
  40. wire serial;
  41. reg [7:0] tx_data;
  42. wire tx_valid;
  43. wire tx_ack;
  44. wire [7:0] rx_data;
  45. wire rx_stb;
  46. // Setup recording
  47. initial begin
  48. $dumpfile("uart_tb.vcd");
  49. $dumpvars(0,uart_tb);
  50. end
  51. // Reset pulse
  52. initial begin
  53. # 200 rst = 0;
  54. # 1000000 $finish;
  55. end
  56. // Clocks
  57. always #10.4 clk_rx = !clk_rx;
  58. always #10.0 clk_tx = !clk_tx;
  59. // DUT
  60. uart_tx #(
  61. .DIV_WIDTH(4)
  62. ) dut_tx_I (
  63. .tx(serial),
  64. .data(tx_data),
  65. .valid(tx_valid),
  66. .ack(tx_ack),
  67. .div(4'h3),
  68. .clk(clk_tx),
  69. .rst(rst)
  70. );
  71. uart_rx #(
  72. .DIV_WIDTH(4),
  73. .GLITCH_FILTER(2)
  74. ) dut_rx_I (
  75. .rx(serial),
  76. .data(rx_data),
  77. .stb(rx_stb),
  78. .div(4'h3),
  79. .clk(clk_rx),
  80. .rst(rst)
  81. );
  82. always @(posedge clk_tx)
  83. if (rst)
  84. tx_data <= 8'h00;
  85. else if (tx_ack)
  86. tx_data <= tx_data + 1;
  87. assign tx_valid = ~rst;
  88. endmodule // uart_tb