usb_ep_status.v 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * usb_ep_status.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut
  7. * All rights reserved.
  8. *
  9. * LGPL v3+, see LICENSE.lgpl3
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 3 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. `default_nettype none
  26. module usb_ep_status (
  27. // Priority port
  28. input wire [ 7:0] p_addr_0,
  29. input wire p_read_0,
  30. input wire p_zero_0,
  31. input wire p_write_0,
  32. input wire [15:0] p_din_0,
  33. output reg [15:0] p_dout_3,
  34. // Aux R/W port
  35. input wire [ 7:0] s_addr_0,
  36. input wire s_read_0,
  37. input wire s_zero_0,
  38. input wire s_write_0,
  39. input wire [15:0] s_din_0,
  40. output reg [15:0] s_dout_3,
  41. output wire s_ready_0,
  42. // Clock / Reset
  43. input wire clk,
  44. input wire rst
  45. );
  46. // Signals
  47. wire s_ready_0_i;
  48. reg [ 7:0] addr_1;
  49. reg [15:0] din_1;
  50. reg we_1;
  51. reg p_read_1;
  52. reg p_zero_1;
  53. reg s_read_1;
  54. reg s_zero_1;
  55. wire [15:0] dout_2;
  56. reg p_read_2;
  57. reg p_zero_2;
  58. reg s_read_2;
  59. reg s_zero_2;
  60. // "Arbitration"
  61. assign s_ready_0_i = ~p_read_0 & ~p_write_0;
  62. assign s_ready_0 = s_ready_0_i;
  63. // Stage 1 : Address mux and Write delay
  64. always @(posedge clk)
  65. begin
  66. addr_1 <= (p_read_0 | p_write_0) ? p_addr_0 : s_addr_0;
  67. we_1 <= p_write_0 | (s_write_0 & s_ready_0_i);
  68. din_1 <= p_write_0 ? p_din_0 : s_din_0;
  69. p_read_1 <= p_read_0;
  70. p_zero_1 <= p_zero_0;
  71. s_read_1 <= s_read_0 & s_ready_0_i;
  72. s_zero_1 <= s_zero_0 & s_ready_0_i;
  73. end
  74. // Stage 2 : Delays
  75. always @(posedge clk)
  76. begin
  77. p_read_2 <= p_read_1 | p_zero_1;
  78. p_zero_2 <= p_zero_1;
  79. s_read_2 <= s_read_1 | s_zero_1;
  80. s_zero_2 <= s_zero_1;
  81. end
  82. // Stage 3 : Output registers
  83. always @(posedge clk)
  84. if (p_read_2)
  85. p_dout_3 <= p_zero_2 ? 16'h0000 : dout_2;
  86. always @(posedge clk)
  87. if (s_read_2)
  88. s_dout_3 <= s_zero_2 ? 16'h0000 : dout_2;
  89. // RAM element
  90. SB_RAM40_4K #(
  91. `ifdef SIM
  92. .INIT_FILE("usb_ep_status.hex"),
  93. `endif
  94. .WRITE_MODE(0),
  95. .READ_MODE(0)
  96. ) ebr_I (
  97. .RDATA(dout_2),
  98. .RADDR({3'b000, addr_1}),
  99. .RCLK(clk),
  100. .RCLKE(1'b1),
  101. .RE(1'b1),
  102. .WDATA(din_1),
  103. .WADDR({3'b000, addr_1}),
  104. .MASK(16'h0000),
  105. .WCLK(clk),
  106. .WCLKE(we_1),
  107. .WE(1'b1)
  108. );
  109. endmodule // usb_ep_status