spi_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * spi_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. * vim: ts=4 sw=4
  34. */
  35. `default_nettype none
  36. `timescale 1ns / 100ps
  37. module spi_tb;
  38. // Signals
  39. reg rst = 1'b1;
  40. reg clk = 1'b0;
  41. wire spi_mosi;
  42. wire spi_miso;
  43. wire spi_cs_n;
  44. wire spi_clk;
  45. wire [7:0] sb_addr;
  46. wire [7:0] sb_data;
  47. wire sb_first;
  48. wire sb_last;
  49. wire sb_strobe;
  50. wire [15:0] reg_val;
  51. wire reg_stb;
  52. // Setup recording
  53. initial begin
  54. $dumpfile("spi_tb.vcd");
  55. $dumpvars(0,spi_tb);
  56. end
  57. // Reset pulse
  58. initial begin
  59. # 200 rst = 0;
  60. # 1000000 $finish;
  61. end
  62. // Clocks
  63. always #10 clk = !clk;
  64. // DUT
  65. spi_simple spi_I (
  66. .spi_mosi(spi_mosi),
  67. .spi_miso(spi_miso),
  68. .spi_cs_n(spi_cs_n),
  69. .spi_clk(spi_clk),
  70. .addr(sb_addr),
  71. .data(sb_data),
  72. .first(sb_first),
  73. .last(sb_last),
  74. .strobe(sb_strobe),
  75. .out(8'hba),
  76. .clk(clk),
  77. .rst(rst)
  78. );
  79. spi_reg #(
  80. .ADDR(8'hA5),
  81. .BYTES(2)
  82. ) reg_I (
  83. .addr(sb_addr),
  84. .data(sb_data),
  85. .first(sb_first),
  86. .strobe(sb_strobe),
  87. .rst_val(16'hbabe),
  88. .out_val(reg_val),
  89. .out_stb(reg_stb),
  90. .clk(clk),
  91. .rst(rst)
  92. );
  93. // SPI data generation
  94. reg [71:0] spi_csn_data = 72'b11110000000000000000000000000000000000000000000000000001111;
  95. reg [71:0] spi_clk_data = 72'b00000010101010101010101010101010101010101010101010101000000;
  96. reg [71:0] spi_dat_data = 72'b00000110011000011001111110000000000111111000000001100000000;
  97. reg [4:0] div;
  98. always @(posedge clk)
  99. if (rst)
  100. div <= 0;
  101. else
  102. div <= div + 1;
  103. always @(posedge clk)
  104. if (div == 4'hf) begin
  105. spi_csn_data <= { spi_csn_data[70:0], 1'b0 & spi_csn_data[71] };
  106. spi_clk_data <= { spi_clk_data[70:0], spi_clk_data[71] };
  107. spi_dat_data <= { spi_dat_data[70:0], spi_dat_data[71] };
  108. end
  109. assign spi_mosi = spi_dat_data[70];
  110. assign spi_cs_n = spi_csn_data[70];
  111. assign spi_clk = spi_clk_data[70];
  112. endmodule // spi_tb