output_target_ram.v 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. `timescale 1ns/1ps
  2. // Shadow writes become visible to the PWM path only on commit. rd_data is
  3. // registered, so the target read contract is one system-clock cycle.
  4. module output_target_ram #(
  5. parameter integer DEPTH = 12,
  6. parameter integer PTC_COUNT = 8,
  7. parameter integer ADDR_WIDTH = (DEPTH <= 1) ? 1 : $clog2(DEPTH),
  8. parameter [7:0] BOOT_PTC_TARGET = 8'd0,
  9. parameter [7:0] BOOT_FAN_TARGET = 8'd10,
  10. parameter [7:0] WATCHDOG_PTC_TARGET = 8'd0,
  11. parameter [7:0] WATCHDOG_FAN_TARGET = 8'd20
  12. ) (
  13. input wire clk,
  14. input wire rst_n,
  15. input wire wr_en,
  16. input wire [ADDR_WIDTH-1:0] wr_addr,
  17. input wire [7:0] wr_data,
  18. input wire commit,
  19. input wire watchdog_active,
  20. input wire [ADDR_WIDTH-1:0] rd_addr,
  21. output reg [7:0] rd_data
  22. );
  23. (* ram_style = "block" *) reg [7:0] shadow_memory [0:DEPTH-1];
  24. (* ram_style = "block" *) reg [7:0] active_memory [0:DEPTH-1];
  25. integer index;
  26. always @(posedge clk or negedge rst_n) begin
  27. if (!rst_n) begin
  28. rd_data <= BOOT_PTC_TARGET;
  29. for (index = 0; index < DEPTH; index = index + 1) begin
  30. shadow_memory[index] <= (index < PTC_COUNT) ? BOOT_PTC_TARGET : BOOT_FAN_TARGET;
  31. active_memory[index] <= (index < PTC_COUNT) ? BOOT_PTC_TARGET : BOOT_FAN_TARGET;
  32. end
  33. end else begin
  34. if (wr_en && (wr_addr < DEPTH)) shadow_memory[wr_addr] <= wr_data;
  35. if (commit) begin
  36. for (index = 0; index < DEPTH; index = index + 1)
  37. active_memory[index] <= shadow_memory[index];
  38. end
  39. if (rd_addr < DEPTH) begin
  40. if (watchdog_active)
  41. rd_data <= (rd_addr < PTC_COUNT) ? WATCHDOG_PTC_TARGET : WATCHDOG_FAN_TARGET;
  42. else
  43. rd_data <= active_memory[rd_addr];
  44. end else begin
  45. rd_data <= 8'd0;
  46. end
  47. end
  48. end
  49. endmodule