mailbox_wb.v 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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*DW-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. // Handle Wishbone communication
  38. wb_ack <= wb_cyc;
  39. // Write operation (if write enable is active)
  40. if (wb_we && wb_cyc) begin
  41. case (wb_addr)
  42. 4'b0000: registers_array[0] <= wb_wdata[15:0]; // Only use lower 16 bits
  43. 4'b0001: registers_array[1] <= wb_wdata[15:0];
  44. 4'b0010: registers_array[2] <= wb_wdata[15:0];
  45. 4'b0011: registers_array[3] <= wb_wdata[15:0];
  46. 4'b0100: registers_array[4] <= wb_wdata[15:0];
  47. 4'b0101: registers_array[5] <= wb_wdata[15:0];
  48. 4'b0110: registers_array[6] <= wb_wdata[15:0];
  49. 4'b0111: registers_array[7] <= wb_wdata[15:0];
  50. 4'b1000: registers_array[8] <= wb_wdata[15:0];
  51. 4'b1001: registers_array[9] <= wb_wdata[15:0];
  52. 4'b1010: registers_array[10] <= wb_wdata[15:0];
  53. 4'b1011: registers_array[11] <= wb_wdata[15:0];
  54. 4'b1100: registers_array[12] <= wb_wdata[15:0];
  55. 4'b1101: registers_array[13] <= wb_wdata[15:0];
  56. 4'b1110: registers_array[14] <= wb_wdata[15:0];
  57. 4'b1111: registers_array[15] <= wb_wdata[15:0];
  58. endcase
  59. end
  60. // Read operation (read the correct register based on address)
  61. case (wb_addr)
  62. 4'b0000: wb_rdata <= {16'h0, registers_array[0]}; // Place 16-bit value in lower half of 32-bit bus
  63. 4'b0001: wb_rdata <= {16'h0, registers_array[1]};
  64. 4'b0010: wb_rdata <= {16'h0, registers_array[2]};
  65. 4'b0011: wb_rdata <= {16'h0, registers_array[3]};
  66. 4'b0100: wb_rdata <= {16'h0, registers_array[4]};
  67. 4'b0101: wb_rdata <= {16'h0, registers_array[5]};
  68. 4'b0110: wb_rdata <= {16'h0, registers_array[6]};
  69. 4'b0111: wb_rdata <= {16'h0, registers_array[7]};
  70. 4'b1000: wb_rdata <= {16'h0, registers_array[8]};
  71. 4'b1001: wb_rdata <= {16'h0, registers_array[9]};
  72. 4'b1010: wb_rdata <= {16'h0, registers_array[10]};
  73. 4'b1011: wb_rdata <= {16'h0, registers_array[11]};
  74. 4'b1100: wb_rdata <= {16'h0, registers_array[12]};
  75. 4'b1101: wb_rdata <= {16'h0, registers_array[13]};
  76. 4'b1110: wb_rdata <= {16'h0, registers_array[14]};
  77. 4'b1111: wb_rdata <= {16'h0, registers_array[15]};
  78. default: wb_rdata <= 32'hDEAD_BEEF; // Default error value
  79. endcase
  80. end
  81. end
  82. // Flatten the registers array to the output port (registers_flat)
  83. generate
  84. genvar j;
  85. for (j = 0; j < 16; j = j + 1) begin : flatten
  86. assign registers_flat[16*(j+1)-1:16*j] = registers_array[j];
  87. end
  88. endgenerate
  89. endmodule