data/full_repos/permissive/152455180/Star_Extract_IP_V2.v
[Errno 2] No such file or directory: 'preprocess.output'
1: b"%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:384: syntax error, unexpected ','\n  pix00<=0,pix01<=0,pix02<=0,pix03<=0,pix04<=0,pix05<=0,pix06<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:385: syntax error, unexpected ','\n  pix10<=0,pix11<=0,pix12<=0,pix13<=0,pix14<=0,pix15<=0,pix16<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:386: syntax error, unexpected ','\n  pix20<=0,pix21<=0,pix22<=0,pix23<=0,pix24<=0,pix25<=0,pix26<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:387: syntax error, unexpected ','\n  pix30<=0,pix31<=0,pix32<=0,pix33<=0,pix34<=0,pix35<=0,pix36<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:388: syntax error, unexpected ','\n  pix40<=0,pix41<=0,pix42<=0,pix43<=0,pix44<=0,pix45<=0,pix46<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:389: syntax error, unexpected ','\n  pix50<=0,pix51<=0,pix52<=0,pix53<=0,pix54<=0,pix55<=0,pix56<=0;\n          ^\n%Error: data/full_repos/permissive/152455180/Star_Extract_IP_V2.v:390: syntax error, unexpected ','\n  pix60<=0,pix61<=0,pix62<=0,pix63<=0,pix64<=0,pix65<=0,pix66<=0;\n          ^\n%Error: Exiting due to 7 error(s)\n"
module Star_Extract_IP#(parameter FRAME_WIDTH  = 1280,parameter FRAME_HEIGHT = 1024)
(

    input rst,


	input IMG_CLK_OUT    ,


	input [9:0] IMG_DOUT ,
	input IMG_FRAME_VAL  ,


	input IMG_LINE_VAL  ,

	

	output reg   we  ,


	reg    [15:0]din , 
	output [17:0]addr,
	output   sample_clk,


	
	output [0:0]LED 					,
	output reg finish_star_extract_IP	,
	output reg [2:0] IMG_TRIG,
	input [1:0] IMG_MON 
	
    
); 
(* keep_hierarchy = "yes" *)

wire  clk;
wire  clk_shift_270;
wire locked;
reg running = 0;
clk_wiz_0 Clk_Management_Unit1
(

	.clk_out1(clk),
	.clk_out2(clk_shift_270),
	.clk_out3(sample_clk),

	.clk_in1(IMG_CLK_OUT),

	.locked(locked) 
	
);  
always @(posedge clk && locked)
begin 
	running <= 1;



end 

reg   [31:0]frame_addr=0, start_addr=0;
wire  [31:0]end_addr;



reg   frame_val_temp, frame_val_temp2;
reg   done=0, trigger_run=0;
 


parameter    streamPieces    = 1018;
parameter   IMG_STREAM_PIECE = 8960;
parameter FRAMEWIDTH_2 =2560;
parameter FRAMEWIDTH_3 =3840,FRAMEWIDTH_4 =5120  ; 
parameter FRAMEWIDTH_5 =6400,FRAMEWIDTH_6 =7680   ; 

(* ram_style = "distributed" *)  reg   [9 :0] memory0[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory1[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory2[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory3[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory4[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory5[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory6[FRAME_WIDTH-1:0];
reg [2:0]  memNum = 0;
reg [2:0]  Ind    = 0;
reg pix00,pix01,pix02,pix03,pix04,pix05,pix06;
reg pix10,pix11,pix12,pix13,pix14,pix15,pix16;
reg pix20,pix21,pix22,pix23,pix24,pix25,pix26;
reg pix30,pix31,pix32,pix33,pix34,pix35,pix36;
reg pix40,pix41,pix42,pix43,pix44,pix45,pix46;
reg pix50,pix51,pix52,pix53,pix54,pix55,pix56;
reg pix60,pix61,pix62,pix63,pix64,pix65,pix66;


reg   [11:0]addr_LM   = 0;
reg   [14:0]addr_DM   = 0;
reg   [20:0]addr_Img  = 0;
reg   [14:0]rowNumber = 0;
reg   [14:0]colNumber = 0;

reg   firstPartOfImage   = 1;

wire   transferFromCamera   ;
assign transferFromCamera   =  running&IMG_FRAME_VAL & IMG_LINE_VAL ;


reg [14:0]FRAME_WIDTH_1  ,FRAME_WIDTH_2  ,FRAME_WIDTH_3  ;
reg [14:0]FRAME_WIDTH_4  ,FRAME_WIDTH_5  ,FRAME_WIDTH_6  ; 


always @(posedge clk_shift_270 && locked )
begin 
	if (rst)
	  begin	
		memNum        <=3'b0;
		Ind           <=3'b0;
		FRAME_WIDTH_1 <=1280;
		FRAME_WIDTH_2 <=2560;
		FRAME_WIDTH_3 <=3840;
		FRAME_WIDTH_4 <=5120;
		FRAME_WIDTH_5 <=6400;
		FRAME_WIDTH_6 <=7680; 
		addr_LM <= 0;
	    addr_DM <= 0;
		addr_Img<= 0;
		firstPartOfImage   <= 1;
		rowNumber  <= 0; colNumber   <= 0;
	 end
	else 
	begin
	if (transferFromCamera)
		begin
		    if(addr_Img == (FRAME_HEIGHT*FRAME_WIDTH)-1)
			    	  firstPartOfImage <=1;
	        else 
			   begin 
			   
			     if(addr_LM==FRAME_WIDTH-1)  
					addr_LM<=0;
				 else 
					addr_LM <= addr_LM+1;
					
			     if (addr_DM == IMG_STREAM_PIECE)
			        begin 
						addr_DM             <= 1;
					    firstPartOfImage    <= 0;
				 	   
				    end
				 else 
					addr_DM <= addr_DM  + 1;
				     
				if(memNum==0)memory0[addr_LM]   <= IMG_DOUT;
				if(memNum==1)memory1[addr_LM]   <= IMG_DOUT;
				if(memNum==2)memory2[addr_LM]   <= IMG_DOUT;
				if(memNum==3)memory3[addr_LM]   <= IMG_DOUT;
				if(memNum==4)memory4[addr_LM]   <= IMG_DOUT;
				if(memNum==5)memory5[addr_LM]   <= IMG_DOUT;
				if(memNum==6)memory6[addr_LM]   <= IMG_DOUT;
						
				
					
				addr_Img <= addr_Img + 1;					 
                
                if(colNumber == FRAME_WIDTH-1)
					 colNumber=0;
				else 
                     colNumber<= colNumber + 1;
				if(Ind == 6)
					Ind=0;
				else 
					Ind<=Ind+1;



				    if(addr_DM==IMG_STREAM_PIECE)begin  rowNumber<= rowNumber+1;memNum<=0;   end	
					if(addr_DM==FRAME_WIDTH)	 begin  rowNumber<= rowNumber+1;memNum<=1;   end		
					if(addr_DM==FRAMEWIDTH_2)	 begin 	rowNumber<= rowNumber+1;memNum<=2;   end
					if(addr_DM==FRAMEWIDTH_3)	 begin 	rowNumber<= rowNumber+1;memNum<=3;   end
					if(addr_DM==FRAMEWIDTH_4)	 begin 	rowNumber<= rowNumber+1;memNum<=4;   end 
					if(addr_DM==FRAMEWIDTH_5)	 begin 	rowNumber<= rowNumber+1;memNum<=5;   end
					if(addr_DM==FRAMEWIDTH_6)	 begin 	rowNumber<= rowNumber+1;memNum<=6;   end
                     	



				if(Ind==0) 
				begin 	pix66<=memory0[addr_LM];pix56<=memory1[addr_LM];pix46<=memory2[addr_LM];
						pix36<=memory3[addr_LM];pix26<=memory4[addr_LM];pix16<=memory5[addr_LM];
						pix06<=memory6[addr_LM];	
				end 
				if(Ind==1) 
				begin 	pix65<=memory0[addr_LM];pix55<=memory1[addr_LM];pix45<=memory2[addr_LM];
						pix35<=memory3[addr_LM];pix25<=memory4[addr_LM];pix15<=memory5[addr_LM];
						pix05<=memory6[addr_LM];	
				end 
				if(Ind==2) 
				begin 	pix64<=memory0[addr_LM];pix54<=memory1[addr_LM];pix44<=memory2[addr_LM];
						pix34<=memory3[addr_LM];pix24<=memory4[addr_LM];pix14<=memory5[addr_LM];
						pix04<=memory6[addr_LM];	
				end 
				if(Ind==3) 
				begin 	pix63<=memory0[addr_LM];pix53<=memory1[addr_LM];pix43<=memory2[addr_LM];
						pix33<=memory3[addr_LM];pix23<=memory4[addr_LM];pix13<=memory5[addr_LM];
						pix03<=memory6[addr_LM];	
				end 
				if(Ind==4) 
				begin 	pix62<=memory0[addr_LM];pix52<=memory1[addr_LM];pix42<=memory2[addr_LM];
						pix32<=memory3[addr_LM];pix22<=memory4[addr_LM];pix12<=memory5[addr_LM];
						pix02<=memory6[addr_LM];	
				end 
				if(Ind==5) 
				begin 	pix61<=memory0[addr_LM];pix51<=memory1[addr_LM];pix41<=memory2[addr_LM];
						pix31<=memory3[addr_LM];pix21<=memory4[addr_LM];pix11<=memory5[addr_LM];
						pix01<=memory6[addr_LM];	
				end 
				if(Ind==6) 
				begin 	pix60<=memory0[addr_LM];pix50<=memory1[addr_LM];pix40<=memory2[addr_LM];
						pix30<=memory3[addr_LM];pix20<=memory4[addr_LM];pix10<=memory5[addr_LM];
						pix00<=memory6[addr_LM];	
				end 
				
				
				
				if (rowNumber>=6 & colNumber>=6)
				begin 					 		

					if(addr_DM<FRAMEWIDTH_3) 
								FRAME_WIDTH_3<=FRAMEWIDTH_3-IMG_STREAM_PIECE;
					else 
									FRAME_WIDTH_3 <=3840;		

					if(addr_DM<FRAMEWIDTH_4) 
							 		FRAME_WIDTH_4<=FRAMEWIDTH_4-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_4 <=5120;

					if(addr_DM<FRAMEWIDTH_2) 
									FRAME_WIDTH_2<=FRAMEWIDTH_2-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_2 <=2560;

					if(addr_DM<FRAMEWIDTH_5) 
									FRAME_WIDTH_5<=FRAMEWIDTH_5-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_5 <=6400;
					
					if(addr_DM<FRAMEWIDTH_6) 
									FRAME_WIDTH_6<=FRAMEWIDTH_6-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_6 <=7680;
					
					if(addr_DM<FRAME_WIDTH) 
									FRAME_WIDTH_1<=FRAME_WIDTH-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_1 <=1280;
				end 
				
			end
		end	

		
	end		
end
 



reg  [9:0]cutoff_Brightness = 100 ;
reg  [9:0]brightCounts[9:0];
reg [9:0] num_bright_areas = 0;

reg [15:0] background_mean_21,background_mean_63 ;
reg [15:0]y_weighted_sum_REG,x_weighted_sum_REG,centroid_sum_REG;

reg getaddrPhase=0;


reg [17:0] addr_write_to_Stars_blkRAM = 0;
reg  update_stars;
reg [2:0]index;



assign   addr          = addr_write_to_Stars_blkRAM;
                         
                        
                        
wire  [14:0]crnt_Star_addr   ;
assign  crnt_Star_addr = addr_DM;

reg  [9:0] center,above,below,right,left;

reg  [14:0]aboveAddr,belowAddr,centerAddr,leftAddr,rightAddr;




reg [13:0]background_sum ;

reg [14:0]bk02 ,bk03 ,bk04 ;
reg [14:0]bk11 ,bk15 ;
reg [14:0]bk20 ,bk26 ;
reg [14:0]bk30 ,bk36 ;
reg [14:0]bk40 ,bk46 ;
reg [14:0]bk51 ,bk55 ;
reg [14:0]bk62 ,bk63 ,bk64 ;


reg [14:0]centroid_sum   ;
reg [14:0]cen12,cen13,cen14;
reg [14:0]cen21,cen22,cen23,cen24,cen25;
reg [14:0]cen31,cen32,cen33,cen34,cen35;
reg [14:0]cen41,cen42,cen43,cen44,cen45;
reg [14:0]cen52,cen53,cen54;



reg [15:0]x_weighted_sum ;
reg [15:0]y_weighted_sum ;


										


reg [2:0] clockCount ;
																			
always @(posedge sample_clk && locked)
begin
	if(rst)
	begin 

		brightCounts[0] <= 0;brightCounts[1] <= 0;brightCounts[2] <= 0;
		brightCounts[3] <= 0;brightCounts[4] <= 0;brightCounts[5] <= 0;
		brightCounts[6] <= 0;brightCounts[7] <= 0;brightCounts[8] <= 0;
		brightCounts[9] <= 0;
        num_bright_areas <= 0;		
		addr_write_to_Stars_blkRAM <= -1;
 		getaddrPhase               <= 1;
		update_stars               <= 0;
		index                      <= 0;
		clockCount    <=   0; 
		din           <=   0;
		background_mean_21<=0;
		background_mean_63<=0;
		y_weighted_sum_REG<=0;
		x_weighted_sum_REG<=0;
		centroid_sum_REG  <=0;
		pix00<=0,pix01<=0,pix02<=0,pix03<=0,pix04<=0,pix05<=0,pix06<=0;
		pix10<=0,pix11<=0,pix12<=0,pix13<=0,pix14<=0,pix15<=0,pix16<=0;
		pix20<=0,pix21<=0,pix22<=0,pix23<=0,pix24<=0,pix25<=0,pix26<=0;
		pix30<=0,pix31<=0,pix32<=0,pix33<=0,pix34<=0,pix35<=0,pix36<=0;
		pix40<=0,pix41<=0,pix42<=0,pix43<=0,pix44<=0,pix45<=0,pix46<=0;
		pix50<=0,pix51<=0,pix52<=0,pix53<=0,pix54<=0,pix55<=0,pix56<=0;
		pix60<=0,pix61<=0,pix62<=0,pix63<=0,pix64<=0,pix65<=0,pix66<=0;
	end	
	else
	begin
	
    if (rowNumber>=6 & colNumber>=6)
	    begin

				if(clockCount==0)
				 begin
				   
							 we           <= 0;
							 
							 if(memory[crnt_Star_addr ] > 1)
							   brightCounts[0]<=brightCounts[0]+1;
							 if (memory[crnt_Star_addr]>2)
							   brightCounts[1]<=brightCounts[1]+1;
							 if (memory[crnt_Star_addr]>4)
							   brightCounts[2]<=brightCounts[2]+1;
							 if (memory[crnt_Star_addr]>8)
							   brightCounts[3]<=brightCounts[3]+1;
							 if (memory[crnt_Star_addr]>16)
							   brightCounts[4]<=brightCounts[4]+1;
							 if (memory[crnt_Star_addr]>32)
							   brightCounts[5]<=brightCounts[5]+1;
							 if (memory[crnt_Star_addr]>64)
							   brightCounts[6]<=brightCounts[6]+1;
							 if (memory[crnt_Star_addr]>128)
							   brightCounts[7]<=brightCounts[7]+1;
							 if (memory[crnt_Star_addr]>256)
							   brightCounts[8]<=brightCounts[8]+1;
							 if (memory[crnt_Star_addr]>512)
							   brightCounts[9]<=brightCounts[9]+1;
							  



							aboveAddr <= crnt_Star_addr-FRAME_WIDTH_4-3;
							belowAddr <= crnt_Star_addr-FRAME_WIDTH_2-3;
							centerAddr<= crnt_Star_addr-FRAME_WIDTH_3-3;
							leftAddr  <= crnt_Star_addr-FRAME_WIDTH_3-4;
							rightAddr <= crnt_Star_addr-FRAME_WIDTH_3-2;

							


							center <= memory[crnt_Star_addr-FRAME_WIDTH_3-3];
							above  <= memory[crnt_Star_addr-FRAME_WIDTH_4-3];
							below  <= memory[crnt_Star_addr-FRAME_WIDTH_2-3];
							left   <= memory[crnt_Star_addr-FRAME_WIDTH_3-4];
							right  <= memory[crnt_Star_addr-FRAME_WIDTH_3-2];

							


							 bk02<=crnt_Star_addr-2              ;bk03<=crnt_Star_addr-3              ;bk04<=crnt_Star_addr-4 ;
							 bk11<=crnt_Star_addr-FRAME_WIDTH_1-1;bk15<=crnt_Star_addr-FRAME_WIDTH_1-5;
							 bk20<=crnt_Star_addr-FRAME_WIDTH_2  ;bk26<=crnt_Star_addr-FRAME_WIDTH_2-6;
							 bk30<=crnt_Star_addr-FRAME_WIDTH_3  ;bk36<=crnt_Star_addr-FRAME_WIDTH_3-6;
							 bk40<=crnt_Star_addr-FRAME_WIDTH_4  ;bk46<=crnt_Star_addr-FRAME_WIDTH_4-6;
							 bk51<=crnt_Star_addr-FRAME_WIDTH_5-1;bk55<=crnt_Star_addr-FRAME_WIDTH_5-5;
							 bk62<=crnt_Star_addr-FRAME_WIDTH_6-2;bk63<=crnt_Star_addr-FRAME_WIDTH_6-3;bk64<=crnt_Star_addr-FRAME_WIDTH_6-4;

							cen12<=crnt_Star_addr-FRAME_WIDTH_1-2;cen13<=crnt_Star_addr-FRAME_WIDTH_1-3;cen14<=crnt_Star_addr-FRAME_WIDTH_1-4;
							cen21<=crnt_Star_addr-FRAME_WIDTH_2-1;cen22<=crnt_Star_addr-FRAME_WIDTH_2-2;cen23<=crnt_Star_addr-FRAME_WIDTH_2-3;cen24<=crnt_Star_addr-FRAME_WIDTH_2-4;cen25<=crnt_Star_addr-FRAME_WIDTH_2-5;
							cen31<=crnt_Star_addr-FRAME_WIDTH_3-1;cen32<=crnt_Star_addr-FRAME_WIDTH_3-2;cen33<=crnt_Star_addr-FRAME_WIDTH_3-3;cen34<=crnt_Star_addr-FRAME_WIDTH_3-4;cen35<=crnt_Star_addr-FRAME_WIDTH_3-5;
							cen41<=crnt_Star_addr-FRAME_WIDTH_4-1;cen42<=crnt_Star_addr-FRAME_WIDTH_4-2;cen43<=crnt_Star_addr-FRAME_WIDTH_4-3;cen44<=crnt_Star_addr-FRAME_WIDTH_4-4;cen45<=crnt_Star_addr-FRAME_WIDTH_4-5;
							cen52<=crnt_Star_addr-FRAME_WIDTH_5-2;cen53<=crnt_Star_addr-FRAME_WIDTH_5-3;cen54<=crnt_Star_addr-FRAME_WIDTH_5-4;
							clockCount <= clockCount+1;
				  end
				 else
                 begin 				 
				   if(( ( center > cutoff_Brightness) && ( above >cutoff_Brightness | below >cutoff_Brightness | right > cutoff_Brightness | left > cutoff_Brightness) && (center >= left & center >= above & center > below & center > right ) ))
				    begin 
					  if(clockCount == 1)
						begin 

							background_sum <= memory[bk02]+ memory[bk03]+memory[bk04]
							+ memory[bk11]+memory[bk15]
							+ memory[bk20]+memory[bk26]
							+ memory[bk30]+memory[bk36]
							+ memory[bk40]+memory[bk46]
							+ memory[bk51]+memory[bk55]
							+ memory[bk62]+ memory[bk63]+ memory[bk64];

														

							centroid_sum  <= memory[cen12]+ memory[cen13]+ memory[cen14]
							+memory[cen21]+memory[cen22]+ memory[cen23]+ memory[cen24]+memory[cen25]
							+memory[cen31]+memory[cen32]+ memory[cen33]+ memory[cen34]+memory[cen35]
							+memory[cen41]+memory[cen42]+ memory[cen43]+ memory[cen44]+memory[cen45]
							+memory[cen52]+ memory[cen53]+ memory[cen54];




							x_weighted_sum  <= (memory[cen14] << 1)+ ( memory[cen13]+(memory[cen13] << 1))+ (memory[cen12]<<2)
							+memory[cen25]+(memory[cen24]<<1)+ ( memory[cen23]+(memory[cen23]<<1))+ (memory[cen22]<<2)+(memory[cen21]+(memory[cen21]<<2))
							+memory[cen35]+(memory[cen34]<<1)+ ( memory[cen33]+(memory[cen33]<<1))+ (memory[cen32]<<2)+(memory[cen31]+(memory[cen31]<<2))
							+memory[cen45]+(memory[cen44]<<1)+ ( memory[cen43]+(memory[cen43]<<1))+ (memory[cen42]<<2)+(memory[cen41]+(memory[cen41]<<2))
							+(memory[cen54]<<1)+ (memory[cen53]+(memory[cen53]<<1))+ (memory[cen52]<<2);


							y_weighted_sum  <= (memory[cen12]+(memory[cen12] << 2))+ ( memory[cen13]+(memory[cen13] << 2))+ (memory[cen14]+(memory[cen14]<<2))
							+(memory[cen21]<<2)+(memory[cen22]<<2)+ ( memory[cen23]<<2)+ (memory[cen24]<<2)+(memory[cen25]<<2)
							+(memory[cen31]+(memory[cen31]<<1))+(memory[cen32]+(memory[cen32]<<1))+ ( memory[cen33]+(memory[cen33]<<1))+ (memory[cen34]+(memory[cen34]<<1))+(memory[cen35]+(memory[cen35]<<1))
							+(memory[cen41]<<1)+(memory[cen42]<<1)+ (memory[cen43]<<1)+ (memory[cen44]<<1)+(memory[cen45]<<1)
							+(memory[cen52]   )+(memory[cen53]   )+ (memory[cen54]   );

							clockCount <= clockCount+1;
							
				  end	
                      else 
						begin 
						if(clockCount == 2)
							begin 
						




								
										num_bright_areas       <= num_bright_areas+1;													 									    									    




										

										background_mean_21 <=( background_sum)+( background_sum >>2)  +( background_sum >>4);
										

										background_mean_63 <=( background_sum <<2)- ( background_sum>> 4);
										

										 clockCount <= clockCount + 1;
									   
								      

							  
							end		
					 	else 
							if(clockCount == 3)
							  begin 


								if(getaddrPhase)
								begin
									we           <= 1;getaddrPhase <= 0;
								end 
								else 
								begin 
									 if(index== 0)
									   begin 
										 din <= rowNumber -6 ;
										 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										 index <= 1 ;
									   end 
									 else if(index == 1)
									   begin 
										 din <= colNumber -6 ;
										 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										 index <= 2 ;
									   end
									  else if(index == 2) 
										 begin 

											if(y_weighted_sum_REG > background_mean_63)
													din <= y_weighted_sum_REG - background_mean_63;
											else 
													din <= 0;
											 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
											 index <= 3 ;
										 end 
										 
									  else if(index == 3)
										 begin

												if(x_weighted_sum_REG > background_mean_63)
													din <= x_weighted_sum_REG - background_mean_63;
												else 
													din <= 0;
												addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
												index <= 4 ;
										 end  
									  else if(index == 4)
										 begin 
											
											if(centroid_sum_REG > background_mean_21)
													din <= centroid_sum_REG - background_mean_21;
											else 
													din <= 0;
											 
										   addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										   index <= 0 ;
										   update_stars <= 0;
										   clockCount <= 0;
										 end  
							   
								end 
					          end
		 
                         end  
                     end
					 else 
						clockCount<=0;
  		
					end 
	 
            end
	end
end
endmodule
module Star_Extract_IP#(parameter FRAME_WIDTH  = 1280,parameter FRAME_HEIGHT = 1024)
(

    input rst,


	input IMG_CLK_OUT    ,


	input [9:0] IMG_DOUT ,
	input IMG_FRAME_VAL  ,


	input IMG_LINE_VAL  ,

	

	output reg   we  ,


	reg    [15:0]din , 
	output [17:0]addr,
	output   sample_clk,


	
	output [0:0]LED 					,
	output reg finish_star_extract_IP	,
	output reg [2:0] IMG_TRIG,
	input [1:0] IMG_MON 
	
    
);
 
(* keep_hierarchy = "yes" *)

wire  clk;
wire  clk_shift_270;
wire locked;
reg running = 0;
clk_wiz_0 Clk_Management_Unit1
(

	.clk_out1(clk),
	.clk_out2(clk_shift_270),
	.clk_out3(sample_clk),

	.clk_in1(IMG_CLK_OUT),

	.locked(locked) 
	
);  
always @(posedge clk && locked)
begin 
	running <= 1;



end 

reg   [31:0]frame_addr=0, start_addr=0;
wire  [31:0]end_addr;



reg   frame_val_temp, frame_val_temp2;
reg   done=0, trigger_run=0;
 


parameter    streamPieces    = 1018;
parameter   IMG_STREAM_PIECE = 8960;
parameter FRAMEWIDTH_2 =2560;
parameter FRAMEWIDTH_3 =3840,FRAMEWIDTH_4 =5120  ; 
parameter FRAMEWIDTH_5 =6400,FRAMEWIDTH_6 =7680   ; 

(* ram_style = "distributed" *)  reg   [9 :0] memory0[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory1[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory2[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory3[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory4[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory5[FRAME_WIDTH-1:0];
(* ram_style = "distributed" *)  reg   [9 :0] memory6[FRAME_WIDTH-1:0];
reg [2:0]  memNum = 0;
reg [2:0]  Ind    = 0;
reg pix00,pix01,pix02,pix03,pix04,pix05,pix06;
reg pix10,pix11,pix12,pix13,pix14,pix15,pix16;
reg pix20,pix21,pix22,pix23,pix24,pix25,pix26;
reg pix30,pix31,pix32,pix33,pix34,pix35,pix36;
reg pix40,pix41,pix42,pix43,pix44,pix45,pix46;
reg pix50,pix51,pix52,pix53,pix54,pix55,pix56;
reg pix60,pix61,pix62,pix63,pix64,pix65,pix66;


reg   [11:0]addr_LM   = 0;
reg   [14:0]addr_DM   = 0;
reg   [20:0]addr_Img  = 0;
reg   [14:0]rowNumber = 0;
reg   [14:0]colNumber = 0;

reg   firstPartOfImage   = 1;

wire   transferFromCamera   ;
assign transferFromCamera   =  running&IMG_FRAME_VAL & IMG_LINE_VAL ;


reg [14:0]FRAME_WIDTH_1  ,FRAME_WIDTH_2  ,FRAME_WIDTH_3  ;
reg [14:0]FRAME_WIDTH_4  ,FRAME_WIDTH_5  ,FRAME_WIDTH_6  ; 


always @(posedge clk_shift_270 && locked )
begin 
	if (rst)
	  begin	
		memNum        <=3'b0;
		Ind           <=3'b0;
		FRAME_WIDTH_1 <=1280;
		FRAME_WIDTH_2 <=2560;
		FRAME_WIDTH_3 <=3840;
		FRAME_WIDTH_4 <=5120;
		FRAME_WIDTH_5 <=6400;
		FRAME_WIDTH_6 <=7680; 
		addr_LM <= 0;
	    addr_DM <= 0;
		addr_Img<= 0;
		firstPartOfImage   <= 1;
		rowNumber  <= 0; colNumber   <= 0;
	 end
	else 
	begin
	if (transferFromCamera)
		begin
		    if(addr_Img == (FRAME_HEIGHT*FRAME_WIDTH)-1)
			    	  firstPartOfImage <=1;
	        else 
			   begin 
			   
			     if(addr_LM==FRAME_WIDTH-1)  
					addr_LM<=0;
				 else 
					addr_LM <= addr_LM+1;
					
			     if (addr_DM == IMG_STREAM_PIECE)
			        begin 
						addr_DM             <= 1;
					    firstPartOfImage    <= 0;
				 	   
				    end
				 else 
					addr_DM <= addr_DM  + 1;
				     
				if(memNum==0)memory0[addr_LM]   <= IMG_DOUT;
				if(memNum==1)memory1[addr_LM]   <= IMG_DOUT;
				if(memNum==2)memory2[addr_LM]   <= IMG_DOUT;
				if(memNum==3)memory3[addr_LM]   <= IMG_DOUT;
				if(memNum==4)memory4[addr_LM]   <= IMG_DOUT;
				if(memNum==5)memory5[addr_LM]   <= IMG_DOUT;
				if(memNum==6)memory6[addr_LM]   <= IMG_DOUT;
						
				
					
				addr_Img <= addr_Img + 1;					 
                
                if(colNumber == FRAME_WIDTH-1)
					 colNumber=0;
				else 
                     colNumber<= colNumber + 1;
				if(Ind == 6)
					Ind=0;
				else 
					Ind<=Ind+1;



				    if(addr_DM==IMG_STREAM_PIECE)begin  rowNumber<= rowNumber+1;memNum<=0;   end	
					if(addr_DM==FRAME_WIDTH)	 begin  rowNumber<= rowNumber+1;memNum<=1;   end		
					if(addr_DM==FRAMEWIDTH_2)	 begin 	rowNumber<= rowNumber+1;memNum<=2;   end
					if(addr_DM==FRAMEWIDTH_3)	 begin 	rowNumber<= rowNumber+1;memNum<=3;   end
					if(addr_DM==FRAMEWIDTH_4)	 begin 	rowNumber<= rowNumber+1;memNum<=4;   end 
					if(addr_DM==FRAMEWIDTH_5)	 begin 	rowNumber<= rowNumber+1;memNum<=5;   end
					if(addr_DM==FRAMEWIDTH_6)	 begin 	rowNumber<= rowNumber+1;memNum<=6;   end
                     	



				if(Ind==0) 
				begin 	pix66<=memory0[addr_LM];pix56<=memory1[addr_LM];pix46<=memory2[addr_LM];
						pix36<=memory3[addr_LM];pix26<=memory4[addr_LM];pix16<=memory5[addr_LM];
						pix06<=memory6[addr_LM];	
				end 
				if(Ind==1) 
				begin 	pix65<=memory0[addr_LM];pix55<=memory1[addr_LM];pix45<=memory2[addr_LM];
						pix35<=memory3[addr_LM];pix25<=memory4[addr_LM];pix15<=memory5[addr_LM];
						pix05<=memory6[addr_LM];	
				end 
				if(Ind==2) 
				begin 	pix64<=memory0[addr_LM];pix54<=memory1[addr_LM];pix44<=memory2[addr_LM];
						pix34<=memory3[addr_LM];pix24<=memory4[addr_LM];pix14<=memory5[addr_LM];
						pix04<=memory6[addr_LM];	
				end 
				if(Ind==3) 
				begin 	pix63<=memory0[addr_LM];pix53<=memory1[addr_LM];pix43<=memory2[addr_LM];
						pix33<=memory3[addr_LM];pix23<=memory4[addr_LM];pix13<=memory5[addr_LM];
						pix03<=memory6[addr_LM];	
				end 
				if(Ind==4) 
				begin 	pix62<=memory0[addr_LM];pix52<=memory1[addr_LM];pix42<=memory2[addr_LM];
						pix32<=memory3[addr_LM];pix22<=memory4[addr_LM];pix12<=memory5[addr_LM];
						pix02<=memory6[addr_LM];	
				end 
				if(Ind==5) 
				begin 	pix61<=memory0[addr_LM];pix51<=memory1[addr_LM];pix41<=memory2[addr_LM];
						pix31<=memory3[addr_LM];pix21<=memory4[addr_LM];pix11<=memory5[addr_LM];
						pix01<=memory6[addr_LM];	
				end 
				if(Ind==6) 
				begin 	pix60<=memory0[addr_LM];pix50<=memory1[addr_LM];pix40<=memory2[addr_LM];
						pix30<=memory3[addr_LM];pix20<=memory4[addr_LM];pix10<=memory5[addr_LM];
						pix00<=memory6[addr_LM];	
				end 
				
				
				
				if (rowNumber>=6 & colNumber>=6)
				begin 					 		

					if(addr_DM<FRAMEWIDTH_3) 
								FRAME_WIDTH_3<=FRAMEWIDTH_3-IMG_STREAM_PIECE;
					else 
									FRAME_WIDTH_3 <=3840;		

					if(addr_DM<FRAMEWIDTH_4) 
							 		FRAME_WIDTH_4<=FRAMEWIDTH_4-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_4 <=5120;

					if(addr_DM<FRAMEWIDTH_2) 
									FRAME_WIDTH_2<=FRAMEWIDTH_2-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_2 <=2560;

					if(addr_DM<FRAMEWIDTH_5) 
									FRAME_WIDTH_5<=FRAMEWIDTH_5-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_5 <=6400;
					
					if(addr_DM<FRAMEWIDTH_6) 
									FRAME_WIDTH_6<=FRAMEWIDTH_6-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_6 <=7680;
					
					if(addr_DM<FRAME_WIDTH) 
									FRAME_WIDTH_1<=FRAME_WIDTH-IMG_STREAM_PIECE;	
					else 
									FRAME_WIDTH_1 <=1280;
				end 
				
			end
		end	

		
	end		
end
 



reg  [9:0]cutoff_Brightness = 100 ;
reg  [9:0]brightCounts[9:0];
reg [9:0] num_bright_areas = 0;

reg [15:0] background_mean_21,background_mean_63 ;
reg [15:0]y_weighted_sum_REG,x_weighted_sum_REG,centroid_sum_REG;

reg getaddrPhase=0;


reg [17:0] addr_write_to_Stars_blkRAM = 0;
reg  update_stars;
reg [2:0]index;



assign   addr          = addr_write_to_Stars_blkRAM;
                         
                        
                        
wire  [14:0]crnt_Star_addr   ;
assign  crnt_Star_addr = addr_DM;

reg  [9:0] center,above,below,right,left;

reg  [14:0]aboveAddr,belowAddr,centerAddr,leftAddr,rightAddr;




reg [13:0]background_sum ;

reg [14:0]bk02 ,bk03 ,bk04 ;
reg [14:0]bk11 ,bk15 ;
reg [14:0]bk20 ,bk26 ;
reg [14:0]bk30 ,bk36 ;
reg [14:0]bk40 ,bk46 ;
reg [14:0]bk51 ,bk55 ;
reg [14:0]bk62 ,bk63 ,bk64 ;


reg [14:0]centroid_sum   ;
reg [14:0]cen12,cen13,cen14;
reg [14:0]cen21,cen22,cen23,cen24,cen25;
reg [14:0]cen31,cen32,cen33,cen34,cen35;
reg [14:0]cen41,cen42,cen43,cen44,cen45;
reg [14:0]cen52,cen53,cen54;



reg [15:0]x_weighted_sum ;
reg [15:0]y_weighted_sum ;


										


reg [2:0] clockCount ;
																			
always @(posedge sample_clk && locked)
begin
	if(rst)
	begin 

		brightCounts[0] <= 0;brightCounts[1] <= 0;brightCounts[2] <= 0;
		brightCounts[3] <= 0;brightCounts[4] <= 0;brightCounts[5] <= 0;
		brightCounts[6] <= 0;brightCounts[7] <= 0;brightCounts[8] <= 0;
		brightCounts[9] <= 0;
        num_bright_areas <= 0;		
		addr_write_to_Stars_blkRAM <= -1;
 		getaddrPhase               <= 1;
		update_stars               <= 0;
		index                      <= 0;
		clockCount    <=   0; 
		din           <=   0;
		background_mean_21<=0;
		background_mean_63<=0;
		y_weighted_sum_REG<=0;
		x_weighted_sum_REG<=0;
		centroid_sum_REG  <=0;
		pix00<=0,pix01<=0,pix02<=0,pix03<=0,pix04<=0,pix05<=0,pix06<=0;
		pix10<=0,pix11<=0,pix12<=0,pix13<=0,pix14<=0,pix15<=0,pix16<=0;
		pix20<=0,pix21<=0,pix22<=0,pix23<=0,pix24<=0,pix25<=0,pix26<=0;
		pix30<=0,pix31<=0,pix32<=0,pix33<=0,pix34<=0,pix35<=0,pix36<=0;
		pix40<=0,pix41<=0,pix42<=0,pix43<=0,pix44<=0,pix45<=0,pix46<=0;
		pix50<=0,pix51<=0,pix52<=0,pix53<=0,pix54<=0,pix55<=0,pix56<=0;
		pix60<=0,pix61<=0,pix62<=0,pix63<=0,pix64<=0,pix65<=0,pix66<=0;
	end	
	else
	begin
	
    if (rowNumber>=6 & colNumber>=6)
	    begin

				if(clockCount==0)
				 begin
				   
							 we           <= 0;
							 
							 if(memory[crnt_Star_addr ] > 1)
							   brightCounts[0]<=brightCounts[0]+1;
							 if (memory[crnt_Star_addr]>2)
							   brightCounts[1]<=brightCounts[1]+1;
							 if (memory[crnt_Star_addr]>4)
							   brightCounts[2]<=brightCounts[2]+1;
							 if (memory[crnt_Star_addr]>8)
							   brightCounts[3]<=brightCounts[3]+1;
							 if (memory[crnt_Star_addr]>16)
							   brightCounts[4]<=brightCounts[4]+1;
							 if (memory[crnt_Star_addr]>32)
							   brightCounts[5]<=brightCounts[5]+1;
							 if (memory[crnt_Star_addr]>64)
							   brightCounts[6]<=brightCounts[6]+1;
							 if (memory[crnt_Star_addr]>128)
							   brightCounts[7]<=brightCounts[7]+1;
							 if (memory[crnt_Star_addr]>256)
							   brightCounts[8]<=brightCounts[8]+1;
							 if (memory[crnt_Star_addr]>512)
							   brightCounts[9]<=brightCounts[9]+1;
							  



							aboveAddr <= crnt_Star_addr-FRAME_WIDTH_4-3;
							belowAddr <= crnt_Star_addr-FRAME_WIDTH_2-3;
							centerAddr<= crnt_Star_addr-FRAME_WIDTH_3-3;
							leftAddr  <= crnt_Star_addr-FRAME_WIDTH_3-4;
							rightAddr <= crnt_Star_addr-FRAME_WIDTH_3-2;

							


							center <= memory[crnt_Star_addr-FRAME_WIDTH_3-3];
							above  <= memory[crnt_Star_addr-FRAME_WIDTH_4-3];
							below  <= memory[crnt_Star_addr-FRAME_WIDTH_2-3];
							left   <= memory[crnt_Star_addr-FRAME_WIDTH_3-4];
							right  <= memory[crnt_Star_addr-FRAME_WIDTH_3-2];

							


							 bk02<=crnt_Star_addr-2              ;bk03<=crnt_Star_addr-3              ;bk04<=crnt_Star_addr-4 ;
							 bk11<=crnt_Star_addr-FRAME_WIDTH_1-1;bk15<=crnt_Star_addr-FRAME_WIDTH_1-5;
							 bk20<=crnt_Star_addr-FRAME_WIDTH_2  ;bk26<=crnt_Star_addr-FRAME_WIDTH_2-6;
							 bk30<=crnt_Star_addr-FRAME_WIDTH_3  ;bk36<=crnt_Star_addr-FRAME_WIDTH_3-6;
							 bk40<=crnt_Star_addr-FRAME_WIDTH_4  ;bk46<=crnt_Star_addr-FRAME_WIDTH_4-6;
							 bk51<=crnt_Star_addr-FRAME_WIDTH_5-1;bk55<=crnt_Star_addr-FRAME_WIDTH_5-5;
							 bk62<=crnt_Star_addr-FRAME_WIDTH_6-2;bk63<=crnt_Star_addr-FRAME_WIDTH_6-3;bk64<=crnt_Star_addr-FRAME_WIDTH_6-4;

							cen12<=crnt_Star_addr-FRAME_WIDTH_1-2;cen13<=crnt_Star_addr-FRAME_WIDTH_1-3;cen14<=crnt_Star_addr-FRAME_WIDTH_1-4;
							cen21<=crnt_Star_addr-FRAME_WIDTH_2-1;cen22<=crnt_Star_addr-FRAME_WIDTH_2-2;cen23<=crnt_Star_addr-FRAME_WIDTH_2-3;cen24<=crnt_Star_addr-FRAME_WIDTH_2-4;cen25<=crnt_Star_addr-FRAME_WIDTH_2-5;
							cen31<=crnt_Star_addr-FRAME_WIDTH_3-1;cen32<=crnt_Star_addr-FRAME_WIDTH_3-2;cen33<=crnt_Star_addr-FRAME_WIDTH_3-3;cen34<=crnt_Star_addr-FRAME_WIDTH_3-4;cen35<=crnt_Star_addr-FRAME_WIDTH_3-5;
							cen41<=crnt_Star_addr-FRAME_WIDTH_4-1;cen42<=crnt_Star_addr-FRAME_WIDTH_4-2;cen43<=crnt_Star_addr-FRAME_WIDTH_4-3;cen44<=crnt_Star_addr-FRAME_WIDTH_4-4;cen45<=crnt_Star_addr-FRAME_WIDTH_4-5;
							cen52<=crnt_Star_addr-FRAME_WIDTH_5-2;cen53<=crnt_Star_addr-FRAME_WIDTH_5-3;cen54<=crnt_Star_addr-FRAME_WIDTH_5-4;
							clockCount <= clockCount+1;
				  end
				 else
                 begin 				 
				   if(( ( center > cutoff_Brightness) && ( above >cutoff_Brightness | below >cutoff_Brightness | right > cutoff_Brightness | left > cutoff_Brightness) && (center >= left & center >= above & center > below & center > right ) ))
				    begin 
					  if(clockCount == 1)
						begin 

							background_sum <= memory[bk02]+ memory[bk03]+memory[bk04]
							+ memory[bk11]+memory[bk15]
							+ memory[bk20]+memory[bk26]
							+ memory[bk30]+memory[bk36]
							+ memory[bk40]+memory[bk46]
							+ memory[bk51]+memory[bk55]
							+ memory[bk62]+ memory[bk63]+ memory[bk64];

														

							centroid_sum  <= memory[cen12]+ memory[cen13]+ memory[cen14]
							+memory[cen21]+memory[cen22]+ memory[cen23]+ memory[cen24]+memory[cen25]
							+memory[cen31]+memory[cen32]+ memory[cen33]+ memory[cen34]+memory[cen35]
							+memory[cen41]+memory[cen42]+ memory[cen43]+ memory[cen44]+memory[cen45]
							+memory[cen52]+ memory[cen53]+ memory[cen54];




							x_weighted_sum  <= (memory[cen14] << 1)+ ( memory[cen13]+(memory[cen13] << 1))+ (memory[cen12]<<2)
							+memory[cen25]+(memory[cen24]<<1)+ ( memory[cen23]+(memory[cen23]<<1))+ (memory[cen22]<<2)+(memory[cen21]+(memory[cen21]<<2))
							+memory[cen35]+(memory[cen34]<<1)+ ( memory[cen33]+(memory[cen33]<<1))+ (memory[cen32]<<2)+(memory[cen31]+(memory[cen31]<<2))
							+memory[cen45]+(memory[cen44]<<1)+ ( memory[cen43]+(memory[cen43]<<1))+ (memory[cen42]<<2)+(memory[cen41]+(memory[cen41]<<2))
							+(memory[cen54]<<1)+ (memory[cen53]+(memory[cen53]<<1))+ (memory[cen52]<<2);


							y_weighted_sum  <= (memory[cen12]+(memory[cen12] << 2))+ ( memory[cen13]+(memory[cen13] << 2))+ (memory[cen14]+(memory[cen14]<<2))
							+(memory[cen21]<<2)+(memory[cen22]<<2)+ ( memory[cen23]<<2)+ (memory[cen24]<<2)+(memory[cen25]<<2)
							+(memory[cen31]+(memory[cen31]<<1))+(memory[cen32]+(memory[cen32]<<1))+ ( memory[cen33]+(memory[cen33]<<1))+ (memory[cen34]+(memory[cen34]<<1))+(memory[cen35]+(memory[cen35]<<1))
							+(memory[cen41]<<1)+(memory[cen42]<<1)+ (memory[cen43]<<1)+ (memory[cen44]<<1)+(memory[cen45]<<1)
							+(memory[cen52]   )+(memory[cen53]   )+ (memory[cen54]   );

							clockCount <= clockCount+1;
							
				  end	
                      else 
						begin 
						if(clockCount == 2)
							begin 
						




								
										num_bright_areas       <= num_bright_areas+1;													 									    									    




										

										background_mean_21 <=( background_sum)+( background_sum >>2)  +( background_sum >>4);
										

										background_mean_63 <=( background_sum <<2)- ( background_sum>> 4);
										

										 clockCount <= clockCount + 1;
									   
								      

							  
							end		
					 	else 
							if(clockCount == 3)
							  begin 


								if(getaddrPhase)
								begin
									we           <= 1;getaddrPhase <= 0;
								end 
								else 
								begin 
									 if(index== 0)
									   begin 
										 din <= rowNumber -6 ;
										 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										 index <= 1 ;
									   end 
									 else if(index == 1)
									   begin 
										 din <= colNumber -6 ;
										 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										 index <= 2 ;
									   end
									  else if(index == 2) 
										 begin 

											if(y_weighted_sum_REG > background_mean_63)
													din <= y_weighted_sum_REG - background_mean_63;
											else 
													din <= 0;
											 addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
											 index <= 3 ;
										 end 
										 
									  else if(index == 3)
										 begin

												if(x_weighted_sum_REG > background_mean_63)
													din <= x_weighted_sum_REG - background_mean_63;
												else 
													din <= 0;
												addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
												index <= 4 ;
										 end  
									  else if(index == 4)
										 begin 
											
											if(centroid_sum_REG > background_mean_21)
													din <= centroid_sum_REG - background_mean_21;
											else 
													din <= 0;
											 
										   addr_write_to_Stars_blkRAM <= addr_write_to_Stars_blkRAM + 1;
										   index <= 0 ;
										   update_stars <= 0;
										   clockCount <= 0;
										 end  
							   
								end 
					          end
		 
                         end  
                     end
					 else 
						clockCount<=0;
  		
					end 
	 
            end
	end
end
endmodule