vid_shared_ram.v 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * vid_shared_ram.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. module vid_shared_ram #(
  35. parameter TYPE = "EBR", // "EBR" / "SPRAM"
  36. parameter integer AW = (TYPE == "EBR") ? 8 : 14
  37. )(
  38. // Priority read port
  39. input wire [AW-1:0] p_addr_0,
  40. input wire p_read_0,
  41. input wire p_zero_0,
  42. output reg [ 15:0] p_dout_3,
  43. // Aux R/W port
  44. input wire [AW-1:0] s_addr_0,
  45. input wire [ 15:0] s_din_0,
  46. input wire s_read_0,
  47. input wire s_zero_0,
  48. input wire s_write_0,
  49. output reg [ 15:0] s_dout_3,
  50. output wire s_ready_0,
  51. // Clock / Reset
  52. input wire clk,
  53. input wire rst
  54. );
  55. // Signals
  56. reg [AW-1:0] addr_1;
  57. reg [15:0] din_1;
  58. reg we_1;
  59. reg p_read_1;
  60. reg p_zero_1;
  61. reg s_read_1;
  62. reg s_zero_1;
  63. wire [15:0] dout_2;
  64. reg p_read_2;
  65. reg p_zero_2;
  66. reg s_read_2;
  67. reg s_zero_2;
  68. // "Arbitration"
  69. assign s_ready_0 = ~p_read_0;
  70. // Stage 1 : Address mux and Write delay
  71. always @(posedge clk)
  72. begin
  73. addr_1 <= p_read_0 ? p_addr_0 : s_addr_0;
  74. we_1 <= s_write_0 & ~p_read_0;
  75. din_1 <= s_din_0;
  76. p_read_1 <= p_read_0;
  77. p_zero_1 <= p_zero_0;
  78. s_read_1 <= s_read_0 & ~p_read_0;
  79. s_zero_1 <= s_zero_0 & ~p_read_0;
  80. end
  81. // Stage 2 : Delays
  82. always @(posedge clk)
  83. begin
  84. p_read_2 <= p_read_1 | p_zero_1;
  85. p_zero_2 <= p_zero_1;
  86. s_read_2 <= s_read_1 | s_zero_1;
  87. s_zero_2 <= s_zero_1;
  88. end
  89. // Stage 3 : Output registers
  90. always @(posedge clk)
  91. if (p_read_2)
  92. p_dout_3 <= p_zero_2 ? 16'h0000 : dout_2;
  93. always @(posedge clk)
  94. if (s_read_2)
  95. s_dout_3 <= s_zero_2 ? 16'h0000 : dout_2;
  96. // RAM element
  97. generate
  98. if (TYPE == "SPRAM")
  99. SB_SPRAM256KA spram_I (
  100. .DATAIN(din_1),
  101. .ADDRESS(addr_1),
  102. .MASKWREN(4'hf),
  103. .WREN(we_1),
  104. .CHIPSELECT(1'b1),
  105. .CLOCK(clk),
  106. .STANDBY(1'b0),
  107. .SLEEP(1'b0),
  108. .POWEROFF(1'b1),
  109. .DATAOUT(dout_2)
  110. );
  111. else if (TYPE == "EBR")
  112. SB_RAM40_4K #(
  113. .WRITE_MODE(0),
  114. .READ_MODE(0)
  115. ) ebr_I (
  116. .RDATA(dout_2),
  117. .RADDR({3'b000, addr_1}),
  118. .RCLK(clk),
  119. .RCLKE(1'b1),
  120. .RE(1'b1),
  121. .WDATA(din_1),
  122. .WADDR({3'b000, addr_1}),
  123. .MASK(16'h0000),
  124. .WCLK(clk),
  125. .WCLKE(we_1),
  126. .WE(1'b1)
  127. );
  128. endgenerate
  129. endmodule // vid_shared_ram