spi_tb.v 3.3 KB

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