`timescale 1ns / 1ps

//=============================================================================================================

//	Тестовая конфигурация:
//
//	Режим измерения "Точка в импульсе".
//	Количество измерений = 1.
//	Выбраный фильтр = 2МГц.
//
//	PG1	->	Reference Sequense Generator.	|	Шаблон 1 имп.
//	PG2	->	модулятор.						|	Шаблон 1 имп.
//	PG3	->	Sample Strobe Generator.		|	Шаблон 1 имп.
//	PG4	->	Gating Generator.				|	Шаблон 1 имп.
//	
//	Настройки мультиплексоров генераторов:
//	PG1MUX_OUT	->	INT_TRIG.
//	PG2MUX_OUT	->	PG1. Для всех генераторов кроме PG1 сигналом начала работы является выход PG1.
//	PG3MUX_OUT	->	PG1.
//	PG4MUX_OUT	->	PG1.
//	PG5MUX_OUT	->	PG1.
//	PG6MUX_OUT	->	PG1.
//	PG7MUX_OUT	->	PG1.
//	
//	Настройки остальных мультиплексоров:
//	MODMUX_OUT			->	PG2.
//	GATINGMUX_OUT		->	PG4.
//	SAMPLSTROBEMUX_OUT	->	PG3.
//	EXTSTARTMUX			->	DSPSTART.

//=============================================================================================================
module S5443TopPointInPulseTb;
	
	localparam	[3:0]	EP1MUXCMD	=	4'd1;
	localparam	[3:0]	EP2MUXCMD	=	4'd1;
	localparam	[3:0]	EP3MUXCMD	=	4'd1;
	localparam	[3:0]	EP4MUXCMD	=	4'd1;
	localparam	[3:0]	EP5MUXCMD	=	4'd1;
	localparam	[3:0]	EP6MUXCMD	=	4'd1;
	
	localparam	[3:0]	PG1MUXCMD	=	4'd13;
	localparam	[3:0]	PG2MUXCMD	=	4'd0;
	localparam	[3:0]	PG3MUXCMD	=	4'd0;
	localparam	[3:0]	PG4MUXCMD	=	4'd0;
	localparam	[3:0]	PG5MUXCMD	=	4'd0;
	localparam	[3:0]	PG6MUXCMD	=	4'd0;
	localparam	[3:0]	PG7MUXCMD	=	4'd0;
	
	localparam	[2:0]	PG1MODE	=	3'd1;
	localparam	[2:0]	PG2MODE	=	3'd1;
	localparam	[2:0]	PG3MODE	=	3'd1;
	localparam	[2:0]	PG4MODE	=	3'd1;
	localparam	[2:0]	PG5MODE	=	3'd0;
	localparam	[2:0]	PG6MODE	=	3'd0;
	localparam	[2:0]	PG7MODE	=	3'd0;
	
	localparam	[3:0]	EXTTRIGMUXCMD	=	4'd15;
	localparam	[3:0]	MODMUXCMD		=	4'd1;
	localparam	[3:0]	GATINGMUXCMD	=	4'd2;
	localparam	[3:0]	SMPLSTRBMUXCMD	=	4'd3;
	
	//COMMANDS	FOR REG_MAP
	parameter	[31:0]	MeasCmd =	{8'h11,8'h0,8'h64,8'h10};
	parameter	[31:0]	MeasCmd2 =	{8'h11,8'h0,8'h62,8'h20};
	parameter	[31:0]	MeasCmd3 =	{8'h11,8'h0,8'h62,8'h40};
	parameter	[31:0]	IfFtwH 	=	{8'h15,16'h0,8'h23};
	parameter	[31:0]	IfFtwL 	=	{8'h16,24'h51eb85};
	
	parameter	[31:0]	IfFtwHR 	=	{8'h15,16'h0,8'h23};
	parameter	[31:0]	IfFtwLR 	=	{8'h16,24'h51eb86};

	reg	[31:0]	IfFtwHCurr;
	reg	[31:0]	IfFtwLCurr;
	
	parameter	[31:0]	FilterCorrCmdH 		=	{8'h17,24'hD70A3D};
	parameter	[31:0]	FilterCorrCmdL 		=	{8'h18,24'hD70A3D};
	
	//PG7 Cmd
	parameter	[31:0]	PG7P1DelayRegCmd	=	{8'h20,24'd0};
	parameter	[31:0]	PG7P2DelayRegCmd	=	{8'h21,24'd0};
	parameter	[31:0]	PG7P3DelayRegCmd	=	{8'h22,24'd0};
	parameter	[31:0]	PG7P123DelayRegCmd	=	{8'h23,24'd0};
	parameter	[31:0]	PG7P1WidthRegCmd	=	{8'h24,24'd1};
	parameter	[31:0]	PG7P2WidthRegCmd	=	{8'h25,24'd0};
	parameter	[31:0]	PG7P3WidthRegCmd	=	{8'h26,24'd0};
	parameter	[31:0]	PG7P123WidthRegCmd	=	{8'h27,24'd0};

	//PG1 Cmd
	parameter	[31:0]	PG1P1DelayRegCmd	=	{8'h28,24'd0};
	parameter	[31:0]	PG1P2DelayRegCmd	=	{8'h29,24'd0};
	parameter	[31:0]	PG1P3DelayRegCmd	=	{8'h2a,24'd0};
	parameter	[31:0]	PG1P123DelayRegCmd	=	{8'h2b,24'd0};
	parameter	[31:0]	PG1P1WidthRegCmd	=	{8'h2c,24'd1};
	parameter	[31:0]	PG1P2WidthRegCmd	=	{8'h2d,24'd0};
	parameter	[31:0]	PG1P3WidthRegCmd	=	{8'h2e,24'd0};
	parameter	[31:0]	PG1P123WidthRegCmd	=	{8'h2f,24'd0};
	
	//PG2 Cmd
	parameter	[31:0]	PG2P1DelayRegCmd	=	{8'h30,24'd9};
	parameter	[31:0]	PG2P2DelayRegCmd	=	{8'h31,24'd0};
	parameter	[31:0]	PG2P3DelayRegCmd	=	{8'h32,24'd0};
	parameter	[31:0]	PG2P123DelayRegCmd	=	{8'h33,24'd0};
	parameter	[31:0]	PG2P1WidthRegCmd	=	{8'h34,24'd40};
	parameter	[31:0]	PG2P2WidthRegCmd	=	{8'h35,24'd0};
	parameter	[31:0]	PG2P3WidthRegCmd	=	{8'h36,24'd0};
	parameter	[31:0]	PG2P123WidthRegCmd	=	{8'h37,24'd0};
	
	//PG3 Cmd
	parameter	[31:0]	PG3P1DelayRegCmd	=	{8'h38,24'd10};
	parameter	[31:0]	PG3P2DelayRegCmd	=	{8'h39,24'd0};
	parameter	[31:0]	PG3P3DelayRegCmd	=	{8'h3a,24'd0};
	parameter	[31:0]	PG3P123DelayRegCmd	=	{8'h3b,24'd0};
	parameter	[31:0]	PG3P1WidthRegCmd	=	{8'h3c,24'd31};
	parameter	[31:0]	PG3P2WidthRegCmd	=	{8'h3d,24'd0};
	parameter	[31:0]	PG3P3WidthRegCmd	=	{8'h3e,24'd0};
	parameter	[31:0]	PG3P123WidthRegCmd	=	{8'h3f,24'd0};
	
	//PG4 Cmd
	parameter	[31:0]	PG4P1DelayRegCmd	=	{8'h40,24'd9};
	parameter	[31:0]	PG4P2DelayRegCmd	=	{8'h41,24'd0};
	parameter	[31:0]	PG4P3DelayRegCmd	=	{8'h42,24'd0};
	parameter	[31:0]	PG4P123DelayRegCmd	=	{8'h43,24'd0};
	parameter	[31:0]	PG4P1WidthRegCmd	=	{8'h44,24'd1};
	parameter	[31:0]	PG4P2WidthRegCmd	=	{8'h45,24'd6};
	parameter	[31:0]	PG4P3WidthRegCmd	=	{8'h46,24'd7};
	parameter	[31:0]	PG4P123WidthRegCmd	=	{8'h47,24'd0};
	
	//PG5 Cmd
	parameter	[31:0]	PG5P1DelayRegCmd	=	{8'h48,24'd5};
	parameter	[31:0]	PG5P2DelayRegCmd	=	{8'h49,24'd15};
	parameter	[31:0]	PG5P3DelayRegCmd	=	{8'h4a,24'd30};
	parameter	[31:0]	PG5P123DelayRegCmd	=	{8'h4b,24'd0};
	parameter	[31:0]	PG5P1WidthRegCmd	=	{8'h4c,24'd5};
	parameter	[31:0]	PG5P2WidthRegCmd	=	{8'h4d,24'd6};
	parameter	[31:0]	PG5P3WidthRegCmd	=	{8'h4e,24'd7};
	parameter	[31:0]	PG5P123WidthRegCmd	=	{8'h4f,24'd0};
	
	//PG6 Cmd
	parameter	[31:0]	PG6P1DelayRegCmd	=	{8'h50,24'd5};
	parameter	[31:0]	PG6P2DelayRegCmd	=	{8'h51,24'd15};
	parameter	[31:0]	PG6P3DelayRegCmd	=	{8'h52,24'd30};
	parameter	[31:0]	PG6P123DelayRegCmd	=	{8'h53,24'd0};
	parameter	[31:0]	PG6P1WidthRegCmd	=	{8'h54,24'd5};
	parameter	[31:0]	PG6P2WidthRegCmd	=	{8'h55,24'd6};
	parameter	[31:0]	PG6P3WidthRegCmd	=	{8'h56,24'd7};
	parameter	[31:0]	PG6P123WidthRegCmd	=	{8'h57,24'd0};
	
	parameter	[31:0]	MeasNum0RegCmd		=	{8'h58,24'd1};
	parameter	[31:0]	MeasNum1RegCmd		=	{8'h59,24'd0};
	parameter	[31:0]	PGMode0RegCmd		=	{8'h0b,3'b0,PG7MODE,PG6MODE,PG5MODE,PG4MODE,PG3MODE,PG2MODE,PG1MODE};
	parameter	[31:0]	PGMode1RegCmd		=	{8'h1b,7'b0000000,7'b00000010,10'b0};
	
	parameter	[31:0]	MuxCtrl1RegCmd	=	{8'h1c,PG6MUXCMD,PG5MUXCMD,PG4MUXCMD,PG3MUXCMD,PG2MUXCMD,PG1MUXCMD};
	parameter	[31:0]	MuxCtrl2RegCmd	=	{8'h1d,4'b0,SMPLSTRBMUXCMD,GATINGMUXCMD,MODMUXCMD,EXTTRIGMUXCMD,PG7MUXCMD};
	parameter	[31:0]	MuxCtrl3RegCmd	=	{8'h1e,EP6MUXCMD,EP5MUXCMD,EP4MUXCMD,EP3MUXCMD,EP2MUXCMD,EP1MUXCMD};
	
	//=================================================================================================================================================================================================================
	
	reg		Clk41;
	reg		Clk50;
	reg		Clk70;
	
	reg	[3:0]	testCnt;
	reg	[31:0]	tb_cnt=4'd0;
	reg	rst;
	reg	mosi_i	=	1'b0;
	reg	Miso_i	=	1'b0;
	reg	ss_i;
	reg	clk_i	=	1'b0;
	
	
	reg	[31:0]	DspSpiData;
	reg		startCalcCmdReg;
						
	wire	[13:0]	cos_value;	
	wire	[17:0]	sin_value;				

	wire	ExtDspTrigPos0	=	(tb_cnt	>=	180	&&	tb_cnt	<=	181)?	1'b1:1'b0;
	wire	ExtDspTrigNeg0	=	(tb_cnt	>=	180	&&	tb_cnt	<=	181)?	1'b0:1'b1;
	
	wire	ExtTrigger0		=	ExtDspTrigNeg0;
	
	wire	TrigFromDsp		=	(tb_cnt	>=	1100	&&	tb_cnt	<=	1101)?	1'b1:1'b0;
	wire	endMeas;
	reg	[31:0]	cmdCnt;
	reg	[31:0]	cmdCntR;
	
	reg	trig0;
	reg	trig1;
	
	wire	trig0R;
    wire	trig1R;
	
	assign	trig0R	=	trig0;
    assign	trig1R	=	trig1;
	
//==========================================================================================
//clocks gen
	always	#10 Clk50	=	~Clk50;
	always	#(14.285714285714/2) Clk70	=	~Clk70;
	always	#10 clk_i	=	~clk_i;
	always	#(24.390243902439/2)	Clk41	=	~Clk41;
	
	wire	sck_i;	
//==========================================================================================
initial begin
	Clk50	=	1'b1;
	Clk70	=	1'b1;
	rst		=	1'b1;
	Clk41	=	1'b0;
	trig0	=	1'b0;
	trig1	=	1'b0;
#100;
	rst		=	1'b0;
#400;
	Clk41	=	1'b0;
end		
	
wire	cmdCntFlag	=	(cmdCntR	==	70	&	cmdCnt==0);
reg	[1:0]	cycleCnt;

always	@(posedge	Clk41)	begin
	if	(!rst)	begin
		cmdCntR	<=	cmdCnt;
	end	else	begin
		cmdCntR	<=	0;
	end
end

always	@(posedge	Clk41)	begin
	if	(!rst)	begin
		if	(cmdCntFlag)	begin
			if	(cycleCnt	!= 2)	begin
				cycleCnt	<=	cycleCnt+1;
			end
		end	
	end	else	begin
		cycleCnt	<=	0;
	end
end

always	@(*)	begin
	if	(tb_cnt	<	3060)	begin
		IfFtwHCurr	<=	IfFtwH;
		IfFtwLCurr	<=	IfFtwL;
	end	else	begin
		IfFtwHCurr	<=	IfFtwHR;
		IfFtwLCurr	<=	IfFtwLR;
	end
end

always	@(*)	begin
	if	(!rst)	begin
		if	(tb_cnt	==	3510	|	tb_cnt	==	3540)	begin
			trig0	=	1'b1;
			trig1	=	1'b1;
		end	else	begin
			trig0	=	1'b0;
			trig1	=	1'b0;
		end
	end	else	begin
		trig0	=	1'b0;
	    trig1	=	1'b0;
	end
end

reg	endMeasReg;
reg	endMeasReg1;
reg	endMeasReg2;
always	@(posedge	Clk41)	begin
	endMeasReg	<=	endMeas;
	endMeasReg1	<=	endMeasReg;
	endMeasReg2	<=	endMeasReg1;
end

wire	endMeasNeg	=	!endMeas&endMeasReg;

always	@(posedge	Clk70)	begin
	if	(!rst)	begin
		if	(!endMeasReg2)	begin
			if	(tb_cnt	==	3501	|	tb_cnt	==	6400)	begin
				startCalcCmdReg	<=	1'b1;
			end	
		end	else	begin
			startCalcCmdReg	<=	1'b0;
		end
	end	else	begin
		startCalcCmdReg	<=	1'b0;
	end
end

always	@(negedge	Clk41)	begin
	if	(!rst)		begin
		tb_cnt	<=	tb_cnt+1;
	end	else	begin
		tb_cnt	<=	0;
	end
end

always	@(negedge	Clk41)	begin
	if	(!rst)		begin
		testCnt	<=	testCnt+1;
	end	else	begin
		testCnt	<=	0;
	end
end

wire	Adc1DataDa0P;
wire	Adc1DataDa1P;

wire	[31:0]	test	=	32'h2351eb85;
wire	Sign1to4	=	(tb_cnt	==	40)?	4'b0001:4'bzzzz;


CordicNco		
#(	.ODatWidth	(18),
	.PhIncWidth	(32),
	.IterNum	(10),
	.EnSinN		(0))
ncoInst
(
	.Clk_i				(Clk50),
	.Rst_i				(rst),
	.Val_i				(1'b1),
	.PhaseInc_i			(test>>1),
	.WindVal_i			(1'b1),
	.WinType_i			(),
	.Wind_o				(),
	.Sin_o				(sin_value),
	.Cos_o				(cos_value),
	.Val_o				()
);


S5443Top MasterFpga 
(
	.Clk_i				(Clk50),
	.Led_o				(),
//------------------------------------------	
    .Adc1FclkP_i		(),		
    .Adc1FclkN_i		(),		

    .Adc1DataDa0P_i		(Adc1DataDa0P),
	.Adc1DataDa0N_i		(~Adc1DataDa0P),		
    .Adc1DataDa1P_i		(Adc1DataDa1P),
    .Adc1DataDa1N_i		(~Adc1DataDa1P),

	.Adc1DataDb0P_i		(Adc1DataDa0P),
    .Adc1DataDb0N_i		(~Adc1DataDa0P),		
    .Adc1DataDb1P_i		(Adc1DataDa1P),
    .Adc1DataDb1N_i		(~Adc1DataDa1P),
//------------------------------------------	
    .Adc2FclkP_i		(),		
    .Adc2FclkN_i		(),		

    .Adc2DataDa0P_i		(1'b1),
    .Adc2DataDa0N_i		(1'b0),		
    .Adc2DataDa1P_i		(1'b1),
    .Adc2DataDa1N_i		(1'b0),
  
	.Adc2DataDb0P_i		(1'b1),
    .Adc2DataDb0N_i		(1'b0),		
    .Adc2DataDb1P_i		(1'b1),
    .Adc2DataDb1N_i		(1'b0),
//------------------------------------------
	.AdcInitMosi_o		(),
	.AdcInitClk_o		(),			
	.Adc1InitCs_o		(),
	.Adc2InitCs_o		(),
	.AdcInitRst_o		(),
//------------------------------------------	
	
	.Mosi_i				(mosi_i),
	.Sck_i				(~sck_i),
	.Ss_i				(ss_i),

	.LpOutClk_o			(),
	.LpOutFs_o			(),			
	.LpOutData_o		(),
	
	//fpga-dsp signals
	.StartMeas_i		(startCalcCmdReg),
	.StartMeas_o		(startMeasS),
	.EndMeas_o			(endMeas),
	.TimersClk_o		(),
	
	.Trig6to1_io		(),	
	.Trig6to1Dir_o		(),	
	
	.DspTrigOut_i		(dspTrigOut),				//Trig from DSP
	.DspTrigIn_o		(),				//Trig To DSP
	
	.OverloadS_i		(1'b0),
	.Overload_o			(),
	
	.PortSel_o			(),
	.PortSelDir_o		(),
	
	//mod out line
	
	.Mod_o				(),	
	
	//gain lines
	.SensEnM_io			(sensEn),
	.AmpEn_o			(),	//	0-adc1ChA 1-adc1ChB 2-adc2ChA 3-adc2ChB
	.AdcData_i			(sin_value[17-:14])
);

parameter	IDLE	=	2'h0;
parameter	CMD		=	2'h1;
parameter	TX		=	2'h2;
parameter	PAUSE	=	2'h3;

reg	[1:0]	txCurrState;
reg	[1:0]	txNextState;

wire	txWork	=	tb_cnt	>=	23;
wire	txStop	=	cmdCnt	==	70;


reg	[6:0]	txCnt;
reg	[3:0]	pauseCnt;

always	@(posedge	Clk41)	begin
	if	(!rst)	begin
		if	(txCurrState	==	CMD)	begin
			if	(!txStop)	begin
				cmdCnt	<=	cmdCnt+1;
			end	else	begin
				cmdCnt	<=	0;
			end
		end
	end	else	begin
		cmdCnt	<=	0;
	end
end

always	@(posedge	Clk41)	begin
	if	(!rst)	begin
		if	(txCurrState	==	TX)	begin
			txCnt	<=	txCnt+1;
		end	else	begin
			txCnt	<=	0;
		end
	end	else	begin
		txCnt	<=	0;
	end
end

always	@(posedge	Clk41)	begin
	if	(!rst)	begin
		if	(txCurrState	==	PAUSE)	begin
			pauseCnt	<=	pauseCnt+1;
		end	else	begin
			pauseCnt	<=	0;
		end
	end	else	begin
		pauseCnt	<=	0;
	end
end
	

always	@(posedge	Clk41)	begin
	if	(txCurrState	==	CMD)	begin
		if	(cmdCnt	==	0)	begin
			DspSpiData		<=	MeasCmd;
			// DspSpiData		<=	MeasCmd2;
			// DspSpiData		<=	MeasCmd3;
		end	else	if	(cmdCnt	==	1)	begin
			DspSpiData		<=	IfFtwHCurr;
		end	else	if	(cmdCnt	==	2)	begin
			DspSpiData		<=	IfFtwLCurr;
		end	else	if	(cmdCnt	==	3)	begin
			DspSpiData		<=	FilterCorrCmdH;
		end	else	if	(cmdCnt	==	4)	begin
			DspSpiData		<=	FilterCorrCmdL;
		end	else	if	(cmdCnt	==	5)	begin
			DspSpiData		<=	PG1P1DelayRegCmd;
		end	else	if	(cmdCnt	==	6)	begin
			DspSpiData		<=	PG1P2DelayRegCmd;
		end	else	if	(cmdCnt	==	7)	begin
			DspSpiData		<=	PG1P3DelayRegCmd;
		end	else	if	(cmdCnt	==	8)	begin
			DspSpiData		<=	PG1P123DelayRegCmd;
		end	else	if	(cmdCnt	==	9)	begin
			DspSpiData		<=	PG1P1WidthRegCmd;
		end	else	if	(cmdCnt	==	10)	begin
			DspSpiData		<=	PG1P2WidthRegCmd;
		end	else	if	(cmdCnt	==	11)	begin
			DspSpiData		<=	PG1P3WidthRegCmd;
		end	else	if	(cmdCnt	==	12)	begin
			DspSpiData		<=	PG1P123WidthRegCmd;
		end	else	if	(cmdCnt	==	13)	begin
			DspSpiData		<=	PG2P1DelayRegCmd;
		end	else	if	(cmdCnt	==	14)	begin
			DspSpiData		<=	PG2P2DelayRegCmd;
		end	else	if	(cmdCnt	==	15)	begin
			DspSpiData		<=	PG2P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	16)	begin
			DspSpiData		<=	PG2P123DelayRegCmd;
		end	else	if	(cmdCnt	==	17)	begin
			DspSpiData		<=	PG2P1WidthRegCmd;
		end	else	if	(cmdCnt	==	18)	begin
			DspSpiData		<=	PG2P2WidthRegCmd;
		end	else	if	(cmdCnt	==	19)	begin
			DspSpiData		<=	PG2P3WidthRegCmd;
		end	else	if	(cmdCnt	==	20)	begin
			DspSpiData		<=	PG2P123WidthRegCmd;
		end	else	if	(cmdCnt	==	21)	begin
			DspSpiData		<=	PG3P1DelayRegCmd;
		end	else	if	(cmdCnt	==	22)	begin
			DspSpiData		<=	PG3P2DelayRegCmd;
		end	else	if	(cmdCnt	==	23)	begin
			DspSpiData		<=	PG3P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	24)	begin
			DspSpiData		<=	PG3P123DelayRegCmd;
		end	else	if	(cmdCnt	==	25)	begin
			DspSpiData		<=	PG3P1WidthRegCmd;
		end	else	if	(cmdCnt	==	26)	begin
			DspSpiData		<=	PG3P2WidthRegCmd;
		end	else	if	(cmdCnt	==	27)	begin
			DspSpiData		<=	PG3P3WidthRegCmd;
		end	else	if	(cmdCnt	==	28)	begin
			DspSpiData		<=	PG3P123WidthRegCmd;
		end	else	if	(cmdCnt	==	29)	begin
			DspSpiData		<=	PG4P1DelayRegCmd;
		end	else	if	(cmdCnt	==	30)	begin
			DspSpiData		<=	PG4P2DelayRegCmd;
		end	else	if	(cmdCnt	==	31)	begin
			DspSpiData		<=	PG4P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	32)	begin
			DspSpiData		<=	PG4P123DelayRegCmd;
		end	else	if	(cmdCnt	==	33)	begin
			DspSpiData		<=	PG4P1WidthRegCmd;
		end	else	if	(cmdCnt	==	34)	begin
			DspSpiData		<=	PG4P2WidthRegCmd;
		end	else	if	(cmdCnt	==	35)	begin
			DspSpiData		<=	PG4P3WidthRegCmd;
		end	else	if	(cmdCnt	==	36)	begin
			DspSpiData		<=	PG4P123WidthRegCmd;
		end	else	if	(cmdCnt	==	37)	begin
			DspSpiData		<=	PG5P1DelayRegCmd;
		end	else	if	(cmdCnt	==	38)	begin
			DspSpiData		<=	PG5P2DelayRegCmd;
		end	else	if	(cmdCnt	==	39)	begin
			DspSpiData		<=	PG5P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	40)	begin
			DspSpiData		<=	PG5P123DelayRegCmd;
		end	else	if	(cmdCnt	==	41)	begin
			DspSpiData		<=	PG5P1WidthRegCmd;
		end	else	if	(cmdCnt	==	42)	begin
			DspSpiData		<=	PG5P2WidthRegCmd;
		end	else	if	(cmdCnt	==	43)	begin
			DspSpiData		<=	PG5P3WidthRegCmd;
		end	else	if	(cmdCnt	==	44)	begin
			DspSpiData		<=	PG5P123WidthRegCmd;
		end	else	if	(cmdCnt	==	45)	begin
			DspSpiData		<=	PG6P1DelayRegCmd;
		end	else	if	(cmdCnt	==	46)	begin
			DspSpiData		<=	PG6P2DelayRegCmd;
		end	else	if	(cmdCnt	==	47)	begin
			DspSpiData		<=	PG6P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	48)	begin
			DspSpiData		<=	PG6P123DelayRegCmd;
		end	else	if	(cmdCnt	==	49)	begin
			DspSpiData		<=	PG6P1WidthRegCmd;
		end	else	if	(cmdCnt	==	50)	begin
			DspSpiData		<=	PG6P2WidthRegCmd;
		end	else	if	(cmdCnt	==	51)	begin
			DspSpiData		<=	PG6P3WidthRegCmd;
		end	else	if	(cmdCnt	==	52)	begin
			DspSpiData		<=	PG6P123WidthRegCmd;
		end	else	if	(cmdCnt	==	53)	begin
			DspSpiData		<=	PG7P1DelayRegCmd;
		end	else	if	(cmdCnt	==	54)	begin
			DspSpiData		<=	PG7P2DelayRegCmd;
		end	else	if	(cmdCnt	==	55)	begin
			DspSpiData		<=	PG7P3DelayRegCmd;	
		end	else	if	(cmdCnt	==	56)	begin
			DspSpiData		<=	PG7P123DelayRegCmd;
		end	else	if	(cmdCnt	==	57)	begin
			DspSpiData		<=	PG7P1WidthRegCmd;
		end	else	if	(cmdCnt	==	58)	begin
			DspSpiData		<=	PG7P2WidthRegCmd;
		end	else	if	(cmdCnt	==	59)	begin
			DspSpiData		<=	PG7P3WidthRegCmd;
		end	else	if	(cmdCnt	==	60)	begin
			DspSpiData		<=	PG7P123WidthRegCmd;
		end	else	if	(cmdCnt	==	61)	begin
			DspSpiData		<=	MeasNum0RegCmd;
		end else	if	(cmdCnt	==	62)	begin
			DspSpiData		<=	MeasNum1RegCmd;
		end else	if	(cmdCnt	==	63)	begin
			DspSpiData		<=	PGMode0RegCmd;
		end	else	if	(cmdCnt	==	64)	begin
			DspSpiData		<=	PGMode1RegCmd;
		end	else	if	(cmdCnt	==	65)	begin
			DspSpiData		<=	MuxCtrl1RegCmd;
		end	else	if	(cmdCnt	==	66)	begin
			DspSpiData		<=	MuxCtrl2RegCmd;
		end	else	if	(cmdCnt	==	67)	begin
			DspSpiData		<=	MuxCtrl3RegCmd;
		end	
	end	else	if	(txCurrState	==	TX)	begin
		DspSpiData	<=	DspSpiData<<1;
	end
end

always	@(posedge Clk41)	begin
	if	(txCurrState	==	TX)	begin
		if	(txCnt	>=	7'd0)	begin
			mosi_i	<=	DspSpiData[31];
		end	else	begin
			mosi_i	<=	1'b1;
		end
	end	else	begin
		mosi_i	<=	1'b1;
	end
end

always	@(posedge	Clk41)	begin
	if	(txCurrState	==	TX)	begin
		ss_i	<=	1'b0;
	end	else	begin
		ss_i	<=	1'b1;
	end
end

assign	sck_i	=	Clk41;

always	@(posedge	Clk41)	begin
	if	(rst)	begin
		txCurrState	<=	IDLE;
	end	else	begin
		txCurrState	<=	txNextState;
	end
end


always @(*) begin
	txNextState	=	IDLE;
	case(txCurrState)
	IDLE	:	begin
					if (txWork	&	cycleCnt<1)	begin
						txNextState = CMD;
					end	else begin
						txNextState = IDLE;
					end
				end
				
	CMD	:		begin
					if (!txStop)	begin
						txNextState = TX;
					end	else begin
						txNextState = IDLE;
					end
				end

	TX		:	begin
					if (txCnt==6'd31) begin
						txNextState  = PAUSE;
					end	else begin
						txNextState  = TX;
					end
				end
        
	PAUSE	:	begin
					if (pauseCnt==4'd10) begin
						txNextState  = CMD;
					end	else begin
						txNextState  = PAUSE;
					end
				end
	endcase
end























endmodule






























