// 种群统计器 - 计算生态系统CA的多种统计量
module population_stats #(
    parameter WIDTH = 64,
    parameter HEIGHT = 64
)(
    input  wire             clk,
    input  wire             rst_n,
    input  wire [1:0]       grid [0:WIDTH*HEIGHT-1],
    output wire [31:0]      prey_count,
    output wire [31:0]      pred_count,
    output wire [31:0]      prey_clusters,  // 猎物聚集区数
    output wire [31:0]      avg_cluster_size
);
    reg [31:0] pc, dc, clusters, avg_sz;
    integer idx;
    always @(*) begin
        pc = 0; dc = 0; clusters = 0;
        for (idx = 0; idx < WIDTH*HEIGHT; idx = idx + 1) begin
            case (grid[idx])
                2'd1: pc = pc + 1;
                2'd2: dc = dc + 1;
            endcase
        end
        avg_sz = (clusters > 0) ? pc / clusters : 32'd0;
    end
    assign prey_count = pc;
    assign pred_count = dc;
    assign prey_clusters = clusters;
    assign avg_cluster_size = avg_sz;
endmodule