mailbox_wb.v 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * mailbox_wb.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2025 Krzysztof Skrzynecki, Jakub Duchniewicz <j.duchniewicz@gmail.com>
  7. * SPDX-License-Identifier: TODO:
  8. */
  9. `default_nettype none
  10. module mailbox_wb #(
  11. parameter AW = 4, // Address width for 16 registers (4 bits)
  12. parameter DW = 32 // Data width for the Wishbone interface (32 bits)
  13. )(
  14. input wire clk,
  15. input wire rst,
  16. // Wishbone Interface
  17. input wire [AW-1:0] wb_addr,
  18. input wire [DW-1:0] wb_wdata,
  19. output reg [DW-1:0] wb_rdata,
  20. input wire wb_we,
  21. input wire wb_cyc,
  22. output reg wb_ack,
  23. // Flattened custom hardware side (RTL)
  24. output wire [16*16-1:0] registers_flat // Flattened register array (16 registers of 16 bits each)
  25. );
  26. // Internal registers (16 registers, each 16 bits wide)
  27. reg [15:0] registers_array[15:0];
  28. // Always reset the registers on reset signal
  29. integer i;
  30. always @(posedge clk or posedge rst) begin
  31. if (rst) begin
  32. wb_ack <= 1'b0;
  33. for (i = 0; i < 16; i = i + 1) begin
  34. registers_array[i] <= 16'h0; // Reset all registers to 0
  35. end
  36. end else begin
  37. // Default no ack
  38. wb_ack <= 1'b0;
  39. if (wb_cyc) begin
  40. // Write operation (if write enable is active) // wb_stb would
  41. // help here
  42. if (wb_we) begin
  43. case (wb_addr)
  44. 4'b0000: registers_array[0] <= wb_wdata[15:0]; // Only use lower 16 bits
  45. 4'b0001: registers_array[1] <= wb_wdata[15:0];
  46. 4'b0010: registers_array[2] <= wb_wdata[15:0];
  47. 4'b0011: registers_array[3] <= wb_wdata[15:0];
  48. 4'b0100: registers_array[4] <= wb_wdata[15:0];
  49. 4'b0101: registers_array[5] <= wb_wdata[15:0];
  50. 4'b0110: registers_array[6] <= wb_wdata[15:0];
  51. 4'b0111: registers_array[7] <= wb_wdata[15:0];
  52. 4'b1000: registers_array[8] <= wb_wdata[15:0];
  53. 4'b1001: registers_array[9] <= wb_wdata[15:0];
  54. 4'b1010: registers_array[10] <= wb_wdata[15:0];
  55. 4'b1011: registers_array[11] <= wb_wdata[15:0];
  56. 4'b1100: registers_array[12] <= wb_wdata[15:0];
  57. 4'b1101: registers_array[13] <= wb_wdata[15:0];
  58. 4'b1110: registers_array[14] <= wb_wdata[15:0];
  59. 4'b1111: registers_array[15] <= wb_wdata[15:0];
  60. endcase
  61. end
  62. // Read operation (read the correct register based on address)
  63. case (wb_addr)
  64. 4'b0000: wb_rdata <= {16'h0, registers_array[0]}; // Place 16-bit value in lower half of 32-bit bus
  65. 4'b0001: wb_rdata <= {16'h0, registers_array[1]};
  66. 4'b0010: wb_rdata <= {16'h0, registers_array[2]};
  67. 4'b0011: wb_rdata <= {16'h0, registers_array[3]};
  68. 4'b0100: wb_rdata <= {16'h0, registers_array[4]};
  69. 4'b0101: wb_rdata <= {16'h0, registers_array[5]};
  70. 4'b0110: wb_rdata <= {16'h0, registers_array[6]};
  71. 4'b0111: wb_rdata <= {16'h0, registers_array[7]};
  72. 4'b1000: wb_rdata <= {16'h0, registers_array[8]};
  73. 4'b1001: wb_rdata <= {16'h0, registers_array[9]};
  74. 4'b1010: wb_rdata <= {16'h0, registers_array[10]};
  75. 4'b1011: wb_rdata <= {16'h0, registers_array[11]};
  76. 4'b1100: wb_rdata <= {16'h0, registers_array[12]};
  77. 4'b1101: wb_rdata <= {16'h0, registers_array[13]};
  78. 4'b1110: wb_rdata <= {16'h0, registers_array[14]};
  79. 4'b1111: wb_rdata <= {16'h0, registers_array[15]};
  80. default: wb_rdata <= 32'hDEAD_BEEF; // Default error value
  81. endcase
  82. // Acknowledge for exactly 1 cycle
  83. wb_ack <= 1'b1;
  84. end
  85. end
  86. end
  87. // Flatten the registers array to the output port (registers_flat)
  88. generate
  89. genvar j;
  90. for (j = 0; j < 16; j = j + 1) begin : flatten
  91. assign registers_flat[16*(j+1)-1:16*j] = registers_array[j];
  92. end
  93. endgenerate
  94. endmodule