% sub-ckt of dspinfpga  generates the FEB data sequence.        JUNE 12'00 %

SUBDESIGN  FRAME
	(CP,
	INIT_FRAME,
	ONES,
	NS[6..0],         % the number of FEB samples  %
	NG[2..0]          % the number of FEB gains    %
	:INPUT;

	FRAMEO,
	SYNCO,
	ADD[9..0],
	HED1,
	HED2,
	CELL,
	ADC,
	LAST,
	STAT
	:OUTPUT;)

	VARIABLE
	FRAME          :DFF  ;
	FRAME_DEL      :DFFE ;
	CK_CNT[2..0]   :DFF  ;
	SYNC           :DFF  ;
	SEQ[14..0]     :DFF  ;

	STATE[2..1]    :DFFE ;
	QHED2          :DFFE ;
	QCELL          :DFFE ;
	QADC           :DFFE ;
	QLAST          :DFFE ;
	QSTAT          :DFFE ;
	WD_CNT[4..0]   :DFFE ;
	BLK_CNT[6..0]  :DFFE ;
	EOG            :LCELL;

	BEGIN
% RECOGNIZE THE FEM DATA BLOCK: %
	FRAME.CLRN = !INIT_FRAME                             ;
	FRAME.D    = ONES & !FRAME # FRAME & !(QSTAT & SYNC) ;

% ---------------------------------------------------------------------- %
% DEFINE THE TIME BASE: %
	IF     !FRAME
	THEN CK_CNT[].D = 0            ; %RESET ADDED MAY 31'00%
	ELSIF   FRAME
	THEN CK_CNT[].D = CK_CNT[] + 1 ;
	END IF ;

	SYNC.D = (CK_CNT[2..0]==6)     ;
	SYNCO  = SYNC                  ;

	FRAME_DEL.ENA = SYNC          ;
	FRAME_DEL.D   = FRAME         ;
	FRAMEO        = FRAME         ;

	IF    FRAME_DEL
	THEN  SEQ[].D = SEQ[] + 1     ;
	END IF ;

	ADD[9..0] = SEQ[9..0] ;

% ---------------------------------------------------------------------- %
% DEFINE THE HED1,HED2,ADC,LAST,STAT FORMAT:%

	STATE[].CLRN = !QSTAT                    ;
	STATE[].ENA  = SYNC                      ;
	STATE1.D     = FRAME & !STATE1 & !STATE2 ;
	STATE2.D     = STATE1 # STATE2           ;

	QHED2.ENA = SYNC     ;
	QHED2.D   = STATE1   ;

	WD_CNT[].ENA = SYNC                             ;
	IF     EOG
	THEN   WD_CNT[].D = 0                           ;
	ELSIF  STATE2 & !EOG & !(BLK_CNT[]==NS[])
	THEN   WD_CNT[].D = WD_CNT[] + 1                ;
	ELSE   WD_CNT[].D = WD_CNT[]                    ;
	END IF ;

	EOG =	(NG[]==1) & (WD_CNT[]==8)  #
		(NG[]==2) & (WD_CNT[]==16) #
		(NG[]==3) & (WD_CNT[]==24) ;

	QCELL.ENA = SYNC                                     ;
	QCELL.D   = (WD_CNT[]==0) & STATE2 & !(BLK_CNT[]==NS[]) ;

	QADC.ENA = SYNC                    ;
	QADC.D   = (WD_CNT[]!=0) & STATE2  ;

	QLAST.ENA = SYNC                   ;
	QLAST.D   = (WD_CNT[]==8)  #
		    (WD_CNT[]==16) #
		    (WD_CNT[]==24) ;

	BLK_CNT[].ENA = SYNC               ;
	IF    (BLK_CNT[]==NS[])
	THEN   BLK_CNT[].D = 0             ;
	ELSIF  EOG
	THEN   BLK_CNT[].D = BLK_CNT[] + 1 ;
	ELSE   BLK_CNT[].D = BLK_CNT[]     ;
	END IF ;

	QSTAT.ENA = SYNC                   ;
	QSTAT.D   = (BLK_CNT[]==NS[])      ;

	HED1 = STATE1 ;
	HED2 = QHED2  ;
	CELL = QCELL  ;
	ADC  = QADC   ;
	LAST = QLAST  ;
	STAT = QSTAT  ;

% CLEAR ALL STATEs FROM FRAME "OFF": %
	WD_CNT[].CLRN  = FRAME     ;
	BLK_CNT[].CLRN = FRAME     ;
	SEQ[].CLRN     = FRAME     ;
	FRAME_DEL.CLRN = FRAME     ;
	QHED2.CLRN     = FRAME_DEL ;
	QCELL.CLRN     = FRAME_DEL ;
	QADC.CLRN      = FRAME_DEL ;
	QLAST.CLRN     = FRAME_DEL ;
	QSTAT.CLRN     = FRAME_DEL ;

% ---------------------------------------------------------------------- %
%CLOCKS: %
	FRAME.CLK     = CP ;
	FRAME_DEL.CLK = CP ;
	SEQ[].CLK     = CP ;
	SYNC.CLK      = CP ;
	QHED2.CLK     = CP ;
	STATE[].CLK   = CP ;
	QCELL.CLK     = CP ;
	QADC.CLK      = CP ;
	QLAST.CLK     = CP ;
	QSTAT.CLK     = CP ;
	CK_CNT[].CLK  = CP ;
	WD_CNT[].CLK  = CP ;
	BLK_CNT[].CLK = CP ;

	END;