`timescale 1ns / 1ps
(* keep_hierarchy = "yes" *)	
module FftDataFormer
#(	
	parameter	AdcDataWidth		=	16,	
	parameter	ExtAdcDataWidth		=	AdcDataWidth+2,	
	parameter	ChNum				=	1,
	parameter	OutDataWidth		=	256,
	parameter	DataValCycles		=	OutDataWidth/(AdcDataWidth*2)
)	
(
	input	Clk_i, 
	input	Rst_i,	
	input	OscWind_i,
	input	[31:0]	MeasNum_i,
	
	input	[AdcDataWidth*2-1:0]	AdcData_i,
	input	AdcDataVal_i,

	
	output	[OutDataWidth-1:0]	OscDataBus_o,
	output	OscDataBusVal_o
);
//================================================================================
//  REG/WIRE
//================================================================================

	reg	[OutDataWidth-1:0]	oscDataBusReg;
	reg	[OutDataWidth-1:0]	oscDataBusRegReg;
	
	reg	oscDataBusValReg;
	reg	oscDataBusValRegReg;
	
	reg	[DataValCycles-1:0]	cycleCnt;
	
	reg	[31:0]	wrDataCnt;
	wire	wrDone	=	OscWind_i?	(wrDataCnt	==	MeasNum_i):1'b0;
	
//================================================================================
//  ASSIGNMENTS
//================================================================================

	assign	OscDataBus_o	=	oscDataBusRegReg;
	assign	OscDataBusVal_o	=	oscDataBusValRegReg;
	
//================================================================================
//  CODING
//================================================================================	

	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(OscWind_i)	begin
				if	(!wrDone)	begin
					oscDataBusValRegReg	<=	oscDataBusValReg;
				end	else	begin
					oscDataBusValRegReg	<=	0;
				end
			end	else	begin
				oscDataBusValRegReg	<=	0;
			end
		end	else	begin
			oscDataBusValRegReg	<=	0;
		end
	end
	
	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(oscDataBusValReg)	begin
				// oscDataBusRegReg	<=	oscDataBusReg;
				oscDataBusRegReg	<=	{oscDataBusReg[127:0], oscDataBusReg[OutDataWidth-1:128]};
			end	
		end	else	begin
			oscDataBusRegReg	<=	0;
		end
	end
	
	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(OscWind_i)	begin
				if	(AdcDataVal_i)	begin
					if	(cycleCnt	!=	DataValCycles-1)	begin
						cycleCnt	<=	cycleCnt+4'd1;
					end	else	begin
						cycleCnt	<=	4'd0;
					end
				end	
			end	else	begin
				cycleCnt	<=	0;
			end
		end	else	begin
			cycleCnt	<=	4'd0;
		end
	end
	
	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(OscWind_i)	begin
				if	(oscDataBusValRegReg)	begin
					if	(wrDataCnt	!=	MeasNum_i)	begin
						wrDataCnt	<=	wrDataCnt+1;
					end
				end
			end	else	begin
				wrDataCnt	<=	0;
			end
		end	else	begin
			wrDataCnt	<=	0;
		end
	end

	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(OscWind_i)	begin
				if	(AdcDataVal_i)	begin
					oscDataBusReg	<=	{oscDataBusReg[OutDataWidth-AdcDataWidth-1:0],AdcData_i};	//first points
				end
			end	else	begin
				oscDataBusReg	<=	0;
			end
		end	else	begin
			oscDataBusReg	<=	0;
		end
	end
	
	always	@(posedge	Clk_i)	begin
		if	(!Rst_i)	begin
			if	(cycleCnt	==	DataValCycles-1	&	AdcDataVal_i)	begin
				oscDataBusValReg	<=	1'b1;
			end	else	begin
				oscDataBusValReg	<=	1'b0;
			end
		end	else	begin
			oscDataBusValReg	<=	1'b0;
		end
	end

endmodule
