data/full_repos/permissive/514467334/project/ConvKing/source/Accelerator/KNN/knn_result.v
1: b'%Warning-WIDTH: data/full_repos/permissive/514467334/project/ConvKing/source/Accelerator/KNN/knn_result.v:111: Bit extraction of array[4:0] requires 3 bit index, not 4 bits.\n                                                                                                             : ... In instance knn_result\n        max <= color_cnt[max_cnt];\n                        ^\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/514467334/project/ConvKing/source/Accelerator/KNN/knn_result.v:115: Bit extraction of array[4:0] requires 3 bit index, not 4 bits.\n                                                                                                             : ... In instance knn_result\n            max <= color_cnt[max_cnt];\n                            ^\n%Warning-WIDTH: data/full_repos/permissive/514467334/project/ConvKing/source/Accelerator/KNN/knn_result.v:114: Bit extraction of array[4:0] requires 3 bit index, not 4 bits.\n                                                                                                             : ... In instance knn_result\n        if (color_cnt[max_cnt] > max) begin\n                     ^\n%Error: Exiting due to 3 warning(s)\n'
module knn_result #(
           parameter knn = 4,
           parameter color_num = 5
       ) (
           input clk_en,
           input reset_n,
           input dic_go,

           input [13: 0] distance,
           input [2: 0] m,

           input dic_end,
           input dic_end_q,
           input knn_fin,

           output [3: 0] knn_resultf,
           output out_flag
       );


reg [13: 0] min;
reg [2: 0] min_p;
always @(posedge clk_en) begin
    if (!reset_n) begin
        min <= 0;
        min_p <= 0;
    end
    else if (dic_go) begin
        if (m == 0) begin
            min <= distance;
            min_p <= m;
        end
        else if (m > 0 & dic_end & dic_end_q) begin
            min <= min;
            min_p <= min_p;
        end
        else if (m > 0 & (!(dic_end & dic_end_q))) begin
            if (min > distance) begin
                min <= distance;
                min_p <= m;
            end
            else begin
                min <= min;
                min_p <= min_p;
            end
        end
        else begin
            min <= min;
            min_p <= min_p;
        end
    end
    else begin
        min <= 0;
        min_p <= 0;
    end
end




reg [5: 0] color_cnt [0: color_num - 1];
always@(posedge clk_en) begin
    if (!reset_n) begin
        color_cnt[0] <= 0;
        color_cnt[1] <= 0;
        color_cnt[2] <= 0;
        color_cnt[3] <= 0;
        color_cnt[4] <= 0;
    end
    else if (dic_end & dic_end_q) begin
        color_cnt[min_p] <= color_cnt[min_p] + 1;
    end
    else begin
        color_cnt[0] <= color_cnt[0];
        color_cnt[1] <= color_cnt[1];
        color_cnt[2] <= color_cnt[2];
        color_cnt[3] <= color_cnt[3];
        color_cnt[4] <= color_cnt[4];
    end
end


reg q0;
reg q1;
wire fin;
always@(posedge clk_en) begin
    if (!reset_n) begin
        q0 <= 0;
    end
    else begin
        q0 <= knn_fin;
        q1 <= q0;
    end
end
assign fin = ~q1 & q0;

reg [3: 0] max_cnt;
reg [5: 0] max;
reg [3: 0] max_p;
wire stop;
assign stop = (max_cnt == color_num) ? 1 : 0;
always@(posedge clk_en) begin
    if (!reset_n) begin
        max <= 0;
        max_p <= 0;
        max_cnt <= 0;
    end
    else if (fin) begin
        max_cnt <= max_cnt + 1;
        max_p <= 0;
        max <= color_cnt[max_cnt];
    end
    else if (q1 & q0 & !stop) begin
        if (color_cnt[max_cnt] > max) begin
            max <= color_cnt[max_cnt];
            max_p <= max_cnt;
        end
        else begin
            max_p <= max_p;
            max <= max;
        end
        max_cnt <= max_cnt + 1;
    end
    else begin
        max_cnt <= max_cnt;
    end
end
assign knn_resultf = stop ? max_p : 0;
assign out_flag = stop ? 1 : 0;

endmodule
module knn_result #(
           parameter knn = 4,
           parameter color_num = 5
       ) (
           input clk_en,
           input reset_n,
           input dic_go,

           input [13: 0] distance,
           input [2: 0] m,

           input dic_end,
           input dic_end_q,
           input knn_fin,

           output [3: 0] knn_resultf,
           output out_flag
       );



reg [13: 0] min;
reg [2: 0] min_p;
always @(posedge clk_en) begin
    if (!reset_n) begin
        min <= 0;
        min_p <= 0;
    end
    else if (dic_go) begin
        if (m == 0) begin
            min <= distance;
            min_p <= m;
        end
        else if (m > 0 & dic_end & dic_end_q) begin
            min <= min;
            min_p <= min_p;
        end
        else if (m > 0 & (!(dic_end & dic_end_q))) begin
            if (min > distance) begin
                min <= distance;
                min_p <= m;
            end
            else begin
                min <= min;
                min_p <= min_p;
            end
        end
        else begin
            min <= min;
            min_p <= min_p;
        end
    end
    else begin
        min <= 0;
        min_p <= 0;
    end
end




reg [5: 0] color_cnt [0: color_num - 1];
always@(posedge clk_en) begin
    if (!reset_n) begin
        color_cnt[0] <= 0;
        color_cnt[1] <= 0;
        color_cnt[2] <= 0;
        color_cnt[3] <= 0;
        color_cnt[4] <= 0;
    end
    else if (dic_end & dic_end_q) begin
        color_cnt[min_p] <= color_cnt[min_p] + 1;
    end
    else begin
        color_cnt[0] <= color_cnt[0];
        color_cnt[1] <= color_cnt[1];
        color_cnt[2] <= color_cnt[2];
        color_cnt[3] <= color_cnt[3];
        color_cnt[4] <= color_cnt[4];
    end
end


reg q0;
reg q1;
wire fin;
always@(posedge clk_en) begin
    if (!reset_n) begin
        q0 <= 0;
    end
    else begin
        q0 <= knn_fin;
        q1 <= q0;
    end
end
assign fin = ~q1 & q0;

reg [3: 0] max_cnt;
reg [5: 0] max;
reg [3: 0] max_p;
wire stop;
assign stop = (max_cnt == color_num) ? 1 : 0;
always@(posedge clk_en) begin
    if (!reset_n) begin
        max <= 0;
        max_p <= 0;
        max_cnt <= 0;
    end
    else if (fin) begin
        max_cnt <= max_cnt + 1;
        max_p <= 0;
        max <= color_cnt[max_cnt];
    end
    else if (q1 & q0 & !stop) begin
        if (color_cnt[max_cnt] > max) begin
            max <= color_cnt[max_cnt];
            max_p <= max_cnt;
        end
        else begin
            max_p <= max_p;
            max <= max;
        end
        max_cnt <= max_cnt + 1;
    end
    else begin
        max_cnt <= max_cnt;
    end
end
assign knn_resultf = stop ? max_p : 0;
assign out_flag = stop ? 1 : 0;

endmodule