ice40_ebr_tb.v 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * ice40_ebr_tb.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2020 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 ice40_ebr_tb #(
  36. parameter integer READ_MODE = 2, /* 0 = 256x16, 1 = 512x8 */
  37. parameter integer WRITE_MODE = 1, /* 2 = 1024x4, 3 = 2048x2 */
  38. parameter integer MASK_WORKAROUND = 1
  39. );
  40. // Config
  41. // ------
  42. localparam integer WAW = 8 + WRITE_MODE;
  43. localparam integer WDW = 16 / (1 << WRITE_MODE);
  44. localparam integer RAW = 8 + READ_MODE;
  45. localparam integer RDW = 16 / (1 << READ_MODE);
  46. // Signals
  47. // -------
  48. reg [WAW-1:0] wr_addr;
  49. reg [WDW-1:0] wr_data;
  50. reg [WDW-1:0] wr_mask;
  51. reg wr_ena;
  52. reg [RAW-1:0] rd_addr;
  53. wire [RDW-1:0] rd_data;
  54. reg rd_ena;
  55. reg clk = 0;
  56. reg rst = 1;
  57. // Test bench
  58. // ----------
  59. // Setup recording
  60. initial begin
  61. $dumpfile("ice40_ebr_tb.vcd");
  62. $dumpvars(0,ice40_ebr_tb);
  63. end
  64. // Reset pulse
  65. initial begin
  66. # 200 rst = 0;
  67. # 1000000 $finish;
  68. end
  69. // Clocks
  70. always #10 clk = !clk;
  71. // DUT
  72. // ---
  73. ice40_ebr #(
  74. .READ_MODE(READ_MODE),
  75. .WRITE_MODE(WRITE_MODE),
  76. .MASK_WORKAROUND(MASK_WORKAROUND)
  77. ) dut_I (
  78. .wr_addr(wr_addr),
  79. .wr_data(wr_data),
  80. .wr_mask(wr_mask),
  81. .wr_ena(wr_ena),
  82. .wr_clk(clk),
  83. .rd_addr(rd_addr),
  84. .rd_data(rd_data),
  85. .rd_ena(rd_ena),
  86. .rd_clk(clk)
  87. );
  88. reg mode;
  89. always @(posedge clk)
  90. begin
  91. // Writes
  92. if (!mode) begin
  93. wr_addr <= wr_addr + wr_ena;
  94. wr_data <= $random;
  95. wr_mask <= 8'hf0;
  96. wr_ena <= ~&wr_addr;
  97. mode <= mode ^ &wr_addr;
  98. end
  99. // Reads
  100. if (mode) begin
  101. rd_addr <= rd_addr + rd_ena;
  102. rd_ena <= ~&rd_addr;
  103. mode <= mode ^ &rd_addr;
  104. end
  105. // Reset
  106. if (rst) begin
  107. wr_addr <= 0;
  108. wr_ena <= 1'b0;
  109. rd_addr <= 0;
  110. rd_ena <= 1'b0;
  111. mode <= 1'b0;
  112. end
  113. end
  114. endmodule