`timescale 1ns/1ps // Shadow writes become visible to the PWM path only on commit. rd_data is // registered, so the target read contract is one system-clock cycle. module output_target_ram #( parameter integer DEPTH = 12, parameter integer PTC_COUNT = 8, parameter integer ADDR_WIDTH = (DEPTH <= 1) ? 1 : $clog2(DEPTH), parameter [7:0] BOOT_PTC_TARGET = 8'd0, parameter [7:0] BOOT_FAN_TARGET = 8'd10, parameter [7:0] WATCHDOG_PTC_TARGET = 8'd0, parameter [7:0] WATCHDOG_FAN_TARGET = 8'd20 ) ( input wire clk, input wire rst_n, input wire wr_en, input wire [ADDR_WIDTH-1:0] wr_addr, input wire [7:0] wr_data, input wire commit, input wire watchdog_active, input wire [ADDR_WIDTH-1:0] rd_addr, output reg [7:0] rd_data ); (* ram_style = "block" *) reg [7:0] shadow_memory [0:DEPTH-1]; (* ram_style = "block" *) reg [7:0] active_memory [0:DEPTH-1]; integer index; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin rd_data <= BOOT_PTC_TARGET; for (index = 0; index < DEPTH; index = index + 1) begin shadow_memory[index] <= (index < PTC_COUNT) ? BOOT_PTC_TARGET : BOOT_FAN_TARGET; active_memory[index] <= (index < PTC_COUNT) ? BOOT_PTC_TARGET : BOOT_FAN_TARGET; end end else begin if (wr_en && (wr_addr < DEPTH)) shadow_memory[wr_addr] <= wr_data; if (commit) begin for (index = 0; index < DEPTH; index = index + 1) active_memory[index] <= shadow_memory[index]; end if (rd_addr < DEPTH) begin if (watchdog_active) rd_data <= (rd_addr < PTC_COUNT) ? WATCHDOG_PTC_TARGET : WATCHDOG_FAN_TARGET; else rd_data <= active_memory[rd_addr]; end else begin rd_data <= 8'd0; end end end endmodule