`timescale 1ns / 1ps

module DecimFilterWrapperTb	();

reg		Clk50;
reg		Rst;

reg		[2:0]	decimFactor;

parameter	MaxWidth	=	32;

reg		[13:0]	adcData;
real	pi	=	3.14159265358;
real	phase	=	0;
real	phaseInc	=	0.001;
real	signal;

reg		[31:0]	tbCnt;

wire 	[31:0] startValue = 32'd10;
wire	[31:0] pNum = 10;
wire 	[31:0] stopValue = startValue+pNum-1;

// wire	oscWind	=	(tbCnt>=10&tbCnt<=509)?	1'b1:1'b0;
wire	oscWind	=	(tbCnt>=startValue & tbCnt<=stopValue)?	1'b1:1'b0;

wire	signed	[13:0]	ncoSin1;
wire	signed	[13:0]	ncoSin2;
wire	signed	[13:0]	ncoSin3;

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

wire	resultVal;
wire	impRespVal;


// wire	signed	[27:0]	adcDataMixed	=	oscWfind?(ncoSin1*ncoSin2):28'd0;
wire	signed	[27:0]	adcDataMixed	=	(ncoSin1*ncoSin2);
wire	signed	[13:0]	adcDataMixedCut	=	adcDataMixed[26-:14];

// wire	signed	[13:0]	sinAdd	=	(ncoSin1>>>2)+(ncoSin2>>>2)+(ncoSin3>>>2);
wire	signed	[13:0]	sinAdd	=	(ncoSin1>>>1)+(ncoSin2>>>1);
// wire	signed	[13:0]	sinAdd	=	ncoSin1;


wire	signed	[13:0]	singlePulse	=	(tbCnt==10)?	14'h1fff:14'h0;
// wire	signed	[13:0]	singlePulse	=	(tbCnt>=10&tbCnt<=12)?	14'h1fff:14'h0;
//==========================================================================================
//clocks gen
always	#10 Clk50	=	~Clk50;

//==========================================================================================
parameter	N	=	2;
parameter	M	=	1;

always	@(posedge	Clk50)	begin
	if	(!Rst)	begin
		decimFactor <= 2;
	end else begin
		decimFactor <= 0;
	end 
end

parameter	LsbForR1	=	10'd16;
parameter	LsbForR2	=	10'd18;
parameter	LsbForR4	=	10'd18;
	
parameter	[31:0]	Nco1PhaseInc	=	32'h19999999;
parameter	[31:0]	Nco2PhaseInc	=	32'h33333333;

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

reg	[20:0]	oscWindDelay;

always	@(posedge	Clk50)	begin
	if	(!Rst)	begin
		oscWindDelay	<=	{oscWindDelay[20:0],oscWind};
	end	else	begin
		oscWindDelay	<= 22'd0;
	end
end

always @ (posedge Clk50)	begin
	if (oscWind)	begin
			phase = phase + phaseInc;
			phaseInc <= phaseInc + 0.003;
			signal = $sin(2*pi*phase);
			adcData = 2**12 * signal;
	end	else	begin
		adcData = 0;
		phase	=	0;
		phaseInc	=	0.001;
	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	
#(	
	.AdcDataWidth	(14),
	.N	(N),
	.M	(M),
	.FilteredDataWidth	(MaxWidth),
	.FirOutDataWidth	(48),
	.FirOutCutBit		(42),
	.LsbForR1			(LsbForR1),
	.LsbForR2			(LsbForR2),
	.LsbForR4			(LsbForR4)
)
DecimFilter
(
	.Clk_i			(Clk50),
	.DecimFactor_i	(decimFactor),
	.Rst_i			(Rst),
	.DataVal_i		(oscWind),

	.Data_i		(sinAdd),
	// .Data_i		(adcDataMixedCut),
	// .Data_i		(adcData),
	// .Data_i		(ncoSin2),
	
	.Data_o	(filteredDataOut),
	.DataVal_o	(resultVal)
);

DecimFilterWrapperTest	
#(	
	.AdcDataWidth	(14),
	.N	(N),
	.M	(M),
	.FilteredDataWidth	(MaxWidth),
	.FirOutDataWidth	(48),
	.FirOutCutBit		(42),
	.LsbForR1			(LsbForR1),
	.LsbForR2			(LsbForR2),
	.LsbForR4			(LsbForR4)
)
ImpulseResponseFilter
(
	.Clk_i			(Clk50),
	.DecimFactor_i	(decimFactor),
	.Rst_i			(Rst),
	.DataVal_i		(oscWind),

	.Data_i		(singlePulse),
	
	.Data_o	(impResponse),
	.DataVal_o	(impRespVal)
);
	

integer inSignal,filteredData, impResp;
parameter	PNum	=	5000;



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

reg	[31:0]	testCnt;
wire	[10:0]	test = N*2+N*2+decimFactor;
wire	[10:0]	test1 = N*2+N*2+decimFactor-1;
// wire	writeEn	=	(oscWindDelay[N*2+N*2+decimFactor-2]);
wire	writeEn	=	(tbCnt>= (startValue+(N*2*2)+decimFactor-6) & tbCnt <= (stopValue+(N*2*2)+decimFactor-6));

always	@(posedge	Clk50)begin
	if	(!Rst)	begin
		if	(writeEn)	begin
			testCnt	<=	testCnt	+1;
		end
	end	else	begin
		testCnt	<=	0;
	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);
		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	(writeEn)	begin
		// if	(impRespVal)	begin
			$fwrite(impResp,"%d\n",   impResponse);
		end	
	end
end 

endmodule



















