`timescale 1ns / 1ps

module DecimFilterWrapperTb	();

//================================================================================
//	PARAMETERS
//================================================================================

parameter	N	=	4;
parameter	M	=	3;
parameter	InDataWidth	=	14;

parameter	K = N*20*($log10(M));
parameter	BitGworth = $ceil(2**(K/20));
parameter	MaxWidth = InDataWidth+BitGworth+1;

parameter	[31:0]	Nco1PhaseInc	=	32'h40000000;
parameter	[31:0]	Nco2PhaseInc	=	32'h0F5C28F6;



//================================================================================
//	REG/WIRE
//================================================================================


reg		Clk50;
reg		Rst;

reg		[2:0]	decimFactor;

reg		[31:0]	tbCnt;

wire	oscWind	=	(tbCnt>=100&tbCnt<=199)?	1'b1:1'b0;

wire	resultVal;
wire	respVal;

wire	signed	[InDataWidth-1:0]	ncoSin1;
wire	signed	[InDataWidth-1:0]	ncoSin2;
wire	signed	[InDataWidth-1:0]	ncoSin3;

wire	signed	[MaxWidth-1:0]	filteredDataOut;
wire	signed	[MaxWidth-1:0]	impResponse;

wire	signed	[InDataWidth*2-1:0]	adcDataMixed	=	(ncoSin1*ncoSin2);
wire	signed	[InDataWidth-1:0]	adcDataMixedCut	=	adcDataMixed[26-:14];

wire	signed	[InDataWidth-1:0]	sinAdd	=	(ncoSin1>>>1)+(ncoSin2>>>1);

wire	signed	[InDataWidth-1:0]	singlePulse	=	(tbCnt==100)?	14'h1fff:14'h0;

integer inSignal,filteredData, impResp;

//================================================================================
//	CLK GEN
//================================================================================

always	#10 Clk50	=	~Clk50;

//================================================================================
//	CODING
//================================================================================
	
initial begin
	Clk50		=	1'b1;
	Rst			=	1'b1;
#200;
	Rst			=	1'b0;
end	

always	@(posedge	Clk50)	begin
	if	(!Rst)	begin
		tbCnt	<=	tbCnt+32'd1;
	end	else	begin
		tbCnt	<=	32'd0;
	end
end
	
always	@(posedge	Clk50)	begin
	if	(!Rst)	begin
		if (tbCnt == 100) begin
			decimFactor	<= 2;
		end
		// end else if (tbCnt == 5400) begin
			// decimFactor	<= 1;
		// end else if (tbCnt == 6400) begin
			// decimFactor	<= 2;
		// end else if (tbCnt == 7400) begin
			// decimFactor	<= 3;
		// end else if (tbCnt == 8400) begin
			// decimFactor	<= 4;
		// end else if (tbCnt == 9400) begin
			// decimFactor	<= 5;
		// end else if (tbCnt == 10400) begin
			// decimFactor	<= 6;
		// end else if (tbCnt == 11400) begin
			// decimFactor	<= 7;
		// end
	end else begin
	 decimFactor <= 2;
	end 
end

CordicNco		
#(	
	.ODatWidth	(14),
	.PhIncWidth	(32),
	.IterNum	(10),
	.EnSinN		(1)
)
ncoInst1
(
	.Clk_i		(Clk50),
	.Rst_i		(Rst),
	.Val_i		(1'b1),
	.PhaseInc_i	(Nco1PhaseInc),
	.WindVal_i	(1'b1),
	.WinType_i	(),
	.Wind_o		(),
	.Sin_o		(ncoSin1),
	.Cos_o		(),
	.Val_o		()
);

CordicNco		
#(	
	.ODatWidth	(14),
	.PhIncWidth	(32),
	.IterNum	(10),
	.EnSinN		(1)
)
ncoInst2
(
	.Clk_i		(Clk50),
	.Rst_i		(Rst),
	.Val_i		(1'b1),
	.PhaseInc_i	(Nco2PhaseInc),
	.WindVal_i	(1'b1),
	.WinType_i	(),
	.Wind_o		(),
	.Sin_o		(ncoSin2),
	.Cos_o		(),
	.Val_o		()
);

DecimFilterWrapperTest	
#(	
	.InDataWidth	(InDataWidth),
	.N	(N),
	.M	(M),
	.OutDataWidth	(MaxWidth)
)
DecimFilter
(
	.Clk_i			(Clk50),
	.DecimFactor_i	(decimFactor),
	.Rst_i			(Rst),
	.DataVal_i		(oscWind),

	.Data_i		(sinAdd),
	
	.Data_o	(filteredDataOut),
	.DataVal_o	(resultVal)
);

DecimFilterWrapperTest	
#(	
	.InDataWidth	(InDataWidth),
	.N	(N),
	.M	(M),
	.OutDataWidth	(MaxWidth)
)
ImpulseResponseFilter
(
	.Clk_i			(Clk50),
	.DecimFactor_i	(decimFactor),
	.Rst_i			(Rst),
	.DataVal_i		(oscWind),

	.Data_i		(singlePulse),
	
	.Data_o	(impResponse),
	.DataVal_o	()
);
	
always	@(posedge	Clk50)	begin
	if	(tbCnt==32'd1)	begin
		inSignal = $fopen("C:/S5243_FFT_REPO/src/src/Sim/InputSignal.txt","w");
	end	else	begin
		if	(oscWind)	begin
			// $display("AdcData is %d", sinAdd);
			// $fwrite(inSignal,"%d\n",   sinAddExt18);
			// $fwrite(inSignal,"%d\n",   adcDataMixedCut);
			$fwrite(inSignal,"%d\n",   sinAdd);
		end	
	end	
end

always	@(posedge	Clk50)	begin
	if	(tbCnt==32'd1)	begin
		filteredData = $fopen("C:/S5243_FFT_REPO/src/src/Sim/FilteredData.txt","w");
	end	else	begin
		if	(resultVal)	begin
			$fwrite(filteredData,"%d\n",   filteredDataOut);
			// $fwrite(filteredData,"%d\n",   firDataOut);
		end	
	end	
end
 

always	@(posedge	Clk50)	begin
	if	(tbCnt==32'd1)	begin
		impResp = $fopen("C:/S5243_FFT_REPO/src/src/Sim/ImpResp.txt","w");
	end	else	begin
		if	(resultVal)	begin
			// $fwrite(impResp,"%d\n",   firRespout);
			$fwrite(impResp,"%d\n",   impResponse);
		end	
	end
end 

endmodule



















