`timescale 1ns/ 1ps

module Type_Distgsh_single #(
    parameter       sub_threshold   = 18'd40   ,
    parameter       apple_std_value = 18'd289  ,
    parameter       pear_std_value  = 18'd325  ,
    parameter       banan_std_value = 18'd524  ,
    parameter       mango_std_value = 18'd499  ,
    parameter       orang_std_value = 18'd253  ,
    parameter       grape_std_value = 18'd453  ,
    parameter       pitay_std_value = 18'd422  ,
    parameter       kiwi_std_value  = 18'd424  
)(
       input          pixelclk          ,
       input          reset_n           ,
       input          i_hs              ,
       input          i_vs              ,
       input          i_de              ,        
       input  [ 2:0]  s_frame_cnt       ,
       input  [17:0]  scircul_1         ,  //single fruit recongnation
       input          sc_done_1         ,
       input  [3:0]   total_num_1       ,

	output         o_hs              ,
	output         o_vs              ,
	output         o_de              ,
       output [3:0]   fruit_num         ,
       output         apple_type_en     ,
       output         pear_type_en      ,
       output         banan_type_en     ,
       output         mango_type_en     ,
       output         orang_type_en     ,
       output         grape_type_en     ,
       output         pitay_type_en     ,
       output         kiwi_type_en          
);

reg     [2:0]       r_frame_cnt       ;
reg     [3:0]       r_fruit_num       ;
reg                 r_apple_type_en   ; 
reg                 r_pear_type_en    ;   
reg                 r_banan_type_en    ;
reg                 r_mango_type_en   ; 
reg                 r_orang_type_en   ; 
reg                 r_grape_type_en   ; 
reg                 r_pitay_type_en   ; 
reg                 r_kiwi_type_en    ;  
reg     [17:0]      app_sub_thrd_value;
reg     [17:0]      pea_sub_thrd_value;
reg     [17:0]      ban_sub_thrd_value;
reg     [17:0]      man_sub_thrd_value;
reg     [17:0]      ora_sub_thrd_value;
reg     [17:0]      gra_sub_thrd_value;
reg     [17:0]      pit_sub_thrd_value;
reg     [17:0]      kiw_sub_thrd_value;  
reg                 r_sc_done_1       ;


reg                  i_vs_d1    ;
reg                  i_vs_d2    ;
reg                  i_hs_d1    ;
reg                  i_hs_d2    ;
reg                  i_de_d1    ;
reg                  i_de_d2    ;

wire     [17:0]     s_app_sub_thrd_value;
wire     [17:0]     s_pea_sub_thrd_value;
wire     [17:0]     s_ban_sub_thrd_value;
wire     [17:0]     s_man_sub_thrd_value;
wire     [17:0]     s_ora_sub_thrd_value;
wire     [17:0]     s_gra_sub_thrd_value;
wire     [17:0]     s_pit_sub_thrd_value;
wire     [17:0]     s_kiw_sub_thrd_value;

assign     o_hs  = i_hs_d2 ;
assign     o_vs  = i_vs_d2 ;
assign     o_de  = i_de_d2 ;

assign     s_app_sub_thrd_value = (app_sub_thrd_value[17]==1'b1)?(~app_sub_thrd_value+18'd1):app_sub_thrd_value;
assign     s_pea_sub_thrd_value = (pea_sub_thrd_value[17]==1'b1)?(~pea_sub_thrd_value+18'd1):pea_sub_thrd_value;
assign     s_ban_sub_thrd_value = (ban_sub_thrd_value[17]==1'b1)?(~ban_sub_thrd_value+18'd1):ban_sub_thrd_value;
assign     s_man_sub_thrd_value = (man_sub_thrd_value[17]==1'b1)?(~man_sub_thrd_value+18'd1):man_sub_thrd_value;
assign     s_ora_sub_thrd_value = (ora_sub_thrd_value[17]==1'b1)?(~ora_sub_thrd_value+18'd1):ora_sub_thrd_value;
assign     s_gra_sub_thrd_value = (gra_sub_thrd_value[17]==1'b1)?(~gra_sub_thrd_value+18'd1):gra_sub_thrd_value;
assign     s_pit_sub_thrd_value = (pit_sub_thrd_value[17]==1'b1)?(~pit_sub_thrd_value+18'd1):pit_sub_thrd_value;
assign     s_kiw_sub_thrd_value = (kiw_sub_thrd_value[17]==1'b1)?(~kiw_sub_thrd_value+18'd1):kiw_sub_thrd_value;

wire                 TFT_VS_fall;

assign   fruit_num       =  r_fruit_num       ;
assign   TFT_VS_fall     =  (!i_vs_d1)&i_vs_d2;
assign   apple_type_en   =  r_apple_type_en   ;
assign   pear_type_en    =  r_pear_type_en    ;
assign   banan_type_en  =   r_banan_type_en   ;
assign   mango_type_en   =  r_mango_type_en   ;
assign   orang_type_en   =  r_orang_type_en   ;
assign   grape_type_en   =  r_grape_type_en   ;
assign   pitay_type_en   =  r_pitay_type_en   ;
assign   kiwi_type_en    =  r_kiwi_type_en    ;

//latch
always @(posedge pixelclk or negedge reset_n) begin
    if (!reset_n)
       r_frame_cnt  <=  3'd0         ;
    else 
       r_frame_cnt  <=  s_frame_cnt  ;
end

always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n) begin
      i_vs_d1     <= 1'b0   ;
      i_vs_d2     <= 1'b0   ;
    end
    else begin
      i_vs_d1     <= i_vs   ;
      i_vs_d2     <= i_vs_d1;       
    end 
end

always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n) begin
      i_hs_d1     <= 1'b0   ;
      i_hs_d2     <= 1'b0   ;
    end
    else begin
      i_hs_d1     <= i_hs   ;
      i_hs_d2     <= i_hs_d1;        
    end 
end

always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n) begin
      i_de_d1     <= 1'b0   ;
      i_de_d2     <= 1'b0   ;
    end
    else begin
      i_de_d1     <= i_de   ;
      i_de_d2     <= i_de_d1;        
    end 
end

always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n) begin
        r_sc_done_1   <=  1'b0    ;
    end
    else begin
        r_sc_done_1   <=  sc_done_1  ;     
    end 
end

//step1 sub normal value
always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n) begin
        app_sub_thrd_value   <=  18'd0  ;
        pea_sub_thrd_value   <=  18'd0  ;
        ban_sub_thrd_value   <=  18'd0  ;
        man_sub_thrd_value   <=  18'd0  ;
        ora_sub_thrd_value   <=  18'd0  ;
        gra_sub_thrd_value   <=  18'd0  ;
        pit_sub_thrd_value   <=  18'd0  ;
        kiw_sub_thrd_value   <=  18'd0  ;
    end
    else if (r_frame_cnt == 3'd1 && sc_done_1 == 1'b1) begin
           app_sub_thrd_value  <=  scircul_1 - apple_std_value; 
           pea_sub_thrd_value  <=  scircul_1 - pear_std_value ;
           ban_sub_thrd_value  <=  scircul_1 - banan_std_value;
           man_sub_thrd_value  <=  scircul_1 - mango_std_value;
           ora_sub_thrd_value  <=  scircul_1 - orang_std_value;
           gra_sub_thrd_value  <=  scircul_1 - grape_std_value;
           pit_sub_thrd_value  <=  scircul_1 - pitay_std_value;
           kiw_sub_thrd_value  <=  scircul_1 - kiwi_std_value;
    end 
    else begin
        app_sub_thrd_value   <=  18'd0  ;
        pea_sub_thrd_value   <=  18'd0  ;
        ban_sub_thrd_value   <=  18'd0  ;
        man_sub_thrd_value   <=  18'd0  ;
        ora_sub_thrd_value   <=  18'd0  ;
        gra_sub_thrd_value   <=  18'd0  ;
        pit_sub_thrd_value   <=  18'd0  ;
        kiw_sub_thrd_value   <=  18'd0  ;        
    end
end

//step2 compare with the threold value
always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_apple_type_en   <=  1'b0;     
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_app_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_apple_type_en <= 1'b1   ;
    end
    else begin
        r_apple_type_en   <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_pear_type_en    <=  1'b0;        
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_pea_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_pear_type_en  <= 1'b1   ;
    end
    else begin
        r_pear_type_en    <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_banan_type_en    <=  1'b0;       
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_ban_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_banan_type_en  <= 1'b1   ;
    end
    else begin
        r_banan_type_en    <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_mango_type_en   <=  1'b0;       
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_man_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_mango_type_en <= 1'b1   ;
    end
    else begin
        r_mango_type_en   <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_orang_type_en   <=  1'b0;       
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_ora_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_orang_type_en <= 1'b1   ;
    end
    else begin
        r_orang_type_en   <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_grape_type_en   <=  1'b0;      
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_gra_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_grape_type_en <= 1'b1   ;
    end
    else begin
        r_grape_type_en   <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_pitay_type_en   <=  1'b0;        
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_pit_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_pitay_type_en <= 1'b1   ;
    end
    else begin
        r_pitay_type_en   <=  1'b0;
    end

always @(posedge pixelclk or negedge reset_n)
    if(!reset_n) begin
        r_kiwi_type_en    <=  1'b0;        
    end
    else if (r_frame_cnt == 3'd1) begin
        if ((s_kiw_sub_thrd_value < sub_threshold) && (r_sc_done_1 == 1'b1))
           r_kiwi_type_en  <= 1'b1   ;
    end
    else begin
        r_kiwi_type_en    <=  1'b0;
    end

//calculate the finally fruit number
always @(posedge pixelclk or negedge reset_n) begin
    if(!reset_n)
       r_fruit_num   <=  4'd0   ;
    else
       r_fruit_num   <=  total_num_1; 
end
endmodule