ice40_spram_gen.v 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ice40_spram_gen.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. module ice40_spram_gen #(
  35. parameter integer ADDR_WIDTH = 15,
  36. parameter integer DATA_WIDTH = 32,
  37. // auto
  38. parameter integer MASK_WIDTH = (DATA_WIDTH + 3) / 4,
  39. parameter integer AL = ADDR_WIDTH - 1,
  40. parameter integer DL = DATA_WIDTH - 1,
  41. parameter integer ML = MASK_WIDTH - 1
  42. )(
  43. input wire [AL:0] addr,
  44. output reg [DL:0] rd_data,
  45. input wire rd_ena, // Write WILL corrupt read data
  46. input wire [DL:0] wr_data,
  47. input wire [ML:0] wr_mask,
  48. input wire wr_ena,
  49. input wire clk
  50. );
  51. genvar x, y;
  52. // Constants
  53. // ---------
  54. localparam integer ND = 1 << (ADDR_WIDTH - 14);
  55. localparam integer NW = (DATA_WIDTH + 15) / 16;
  56. localparam integer MSW = (ADDR_WIDTH > 14) ? (ADDR_WIDTH - 14) : 0;
  57. localparam integer MDW = NW * 16;
  58. localparam integer MMW = NW * 4;
  59. initial
  60. $display("ice40_spram_gen: (%dx%d) -> %dx%d array of SPRAM\n", (1<<ADDR_WIDTH), DATA_WIDTH, ND, NW);
  61. // Signals
  62. // -------
  63. reg [AL:0] addr_r;
  64. wire [13:0] mem_addr;
  65. wire [ND-1:0] mem_sel;
  66. wire [ND-1:0] mem_ce;
  67. wire [ND-1:0] mem_wren;
  68. wire [MDW-1:0] mem_do_m[0:ND-1];
  69. wire [MDW-1:0] mem_do_w;
  70. reg [MDW-1:0] mem_di_w;
  71. reg [MMW-1:0] mem_dm_w;
  72. wire [15:0] mem_do[0:ND*NW-1];
  73. wire [15:0] mem_di[0:ND*NW-1];
  74. wire [ 3:0] mem_dm[0:ND*NW-1];
  75. // Main logic
  76. // ----------
  77. // Register address for read-muxing
  78. always @(posedge clk)
  79. if (rd_ena)
  80. addr_r <= addr;
  81. // Address mapping
  82. assign mem_addr = addr[13:0];
  83. // Map input data to/from 16*NW bit words
  84. always @(*)
  85. begin : map
  86. integer n, x, o;
  87. // Map actual bits
  88. for (n=0; n<MASK_WIDTH; n=n+1)
  89. begin
  90. // Determine position
  91. x = n % NW; // Which SPRAM
  92. o = n / NW; // Which nibble inside that SPRAM
  93. // Map IO
  94. mem_di_w[(16*x)+(4*o)+:4] = wr_data[4*n+:4];
  95. mem_dm_w[( 4*x)+ o ] = ~wr_mask[n];
  96. rd_data[4*n+:4] = mem_do_w[(16*x)+(4*o)+:4];
  97. end
  98. end
  99. // Generate memory array
  100. generate
  101. // Per-depth loop
  102. for (y=0; y<ND; y=y+1)
  103. begin
  104. // Per-width loop
  105. for (x=0; x<NW; x=x+1)
  106. begin
  107. // IO mapping for word
  108. assign mem_di[y*NW+x] = mem_di_w[x*16+:16];
  109. assign mem_dm[y*NW+x] = mem_dm_w[x* 4+: 4];
  110. assign mem_do_m[y][x*16+:16] = mem_do[y*NW+x];
  111. // Memory element
  112. SB_SPRAM256KA ram_I (
  113. .ADDRESS (mem_addr),
  114. .DATAIN (mem_di[y*NW+x]),
  115. .MASKWREN (mem_dm[y*NW+x]),
  116. .WREN (mem_wren[y]),
  117. .CHIPSELECT(mem_ce[y]),
  118. .CLOCK (clk),
  119. .STANDBY (1'b0),
  120. .SLEEP (1'b0),
  121. .POWEROFF (1'b1),
  122. .DATAOUT (mem_do[y*NW+x])
  123. );
  124. end
  125. // Enables
  126. if (MSW == 0)
  127. assign mem_sel[y] = 1'b1;
  128. else
  129. assign mem_sel[y] = (addr[AL:AL-MSW+1] == y);
  130. assign mem_wren[y] = mem_sel[y] & wr_ena;
  131. assign mem_ce[y] = mem_sel[y] & (wr_ena | rd_ena);
  132. // Muxing
  133. if (MSW == 0)
  134. // Trivial case
  135. assign mem_do_w = mem_do_m[0];
  136. else
  137. // Read side mux
  138. assign mem_do_w = mem_do_m[addr_r[AL:AL-MSW+1]];
  139. end
  140. endgenerate
  141. endmodule