{{ header | safe }}

`default_nettype none

module reporting_logic #(
    parameter COUNTER_WIDTH = {{ config.counter_width | default(32) }},
    {%- set metrics = config.metrics_to_monitor | default([]) %}
    parameter NUM_METRICS = {{ (metrics|length) if metrics else 1 }}
) (
    input  wire  clk,
    input  wire  rst_n,
    
    // Data inputs
    input  wire  [COUNTER_WIDTH-1:0] sample_data [NUM_METRICS-1:0],
    input  wire  sample_valid,
    input  wire  [COUNTER_WIDTH-1:0] transaction_count,
    input  wire  [COUNTER_WIDTH-1:0] threshold,
    input  wire  report_ack,
    input  wire  [3:0] report_select,
    
    // Report outputs
    output logic report_ready,
    output logic [COUNTER_WIDTH-1:0] report_data
);

// Performance Reporting Logic
logic [COUNTER_WIDTH-1:0] report_buffer [NUM_METRICS-1:0];
logic report_pending;
logic [3:0] report_state;

// Report state machine
localparam REPORT_IDLE     = 4'h0;
localparam REPORT_PREPARE  = 4'h1;
localparam REPORT_READY    = 4'h2;
localparam REPORT_TRANSMIT = 4'h3;

always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        report_state <= REPORT_IDLE;
        report_pending <= 1'b0;
        report_ready <= 1'b0;
    {%- if metrics -%}
    {%- for i in range(metrics|length) -%}
    report_buffer[{{ i }}] <= COUNTER_WIDTH'h0;
    {%- endfor -%}
    {%- else -%}
    report_buffer[0] <= COUNTER_WIDTH'h0;
    {%- endif -%}
    end else begin
        case (report_state)
            REPORT_IDLE: begin
                report_ready <= 1'b0;
                if (sample_valid || (transaction_count > threshold)) begin
                    report_state <= REPORT_PREPARE;
                    report_pending <= 1'b1;
                end
            end
            
            REPORT_PREPARE: begin
                // Copy current metrics to report buffer
                {%- if metrics -%}
                {%- for i in range(metrics|length) -%}
                report_buffer[{{ i }}] <= sample_data[{{ i }}];
                {%- endfor -%}
                {%- else -%}
                report_buffer[0] <= sample_data[0];
                {%- endif -%}
                report_state <= REPORT_READY;
            end
            
            REPORT_READY: begin
                report_ready <= 1'b1;
                if (report_ack) begin
                    report_state <= REPORT_TRANSMIT;
                end
            end
            
            REPORT_TRANSMIT: begin
                report_ready <= 1'b0;
                report_pending <= 1'b0;
                report_state <= REPORT_IDLE;
            end
            
            default: begin
                report_state <= REPORT_IDLE;
            end
        endcase
    end
end

// Report data multiplexer
always_comb begin
    case (report_select)
    {%- if metrics -%}
    {%- for i in range(metrics|length) -%}
    4'h{{ "%x" % i }}: report_data = report_buffer[{{ i }}];
    {%- endfor -%}
    {%- else -%}
    4'h0: report_data = report_buffer[0];
    {%- endif -%}
        default: report_data = COUNTER_WIDTH'h0;
    endcase
end

endmodule

`default_nettype wire
