/*
** StereoTemplate.vfs - generic stereo format for #including
**
** you can #define the following before including this file:
**
** SER - serration duration (defaults to 1.0H - HorizontalSync)
** USE_SER - use serrations
** note: if block sync formats are generated (USE_SER undefined), HSync
** will be replaced with a serrated version of CSync for use with genlock.
**
** FPS - fields per second
**
** VSYNC - vertical sync duration in lines
** VBP - vertical back porch
** EXTRA_VBP - if defined, the amount to bloat vbp by
** VACT - number of lines of active video (required)
** VFP - vertical front porch
**
** HSYNC - horizontal sync duration
** HBP - horizontal sync duration
** HACT - pixels per line (required)
** HFP - horizontal sync duration
**
** FORMAT_NAME - optional name for the format (defaults to HACTxVACT_FPSs)
**
** The following rules are used to generate missing parameters:
** ------------------------------------------------------------
** Vtotal = 1.05 * VACT
** VFP    = 0.004 * Vtotal + 1
** VSYNC  = 0.004 * Vtotal + 1
** VBP    = Vtotal - VFP - VSYNC - VACT
** 
** Htotal = 1.25 * HACT
** HFP    = 0.0125 * Htotal
** HSYNC  = 0.1 * Htotal
** HBP    = Htotal - HFP - HSYNC - HACT
*/

#if !defined(FPS) || !defined(VACT) || !defined(HACT)
    error "FPS, VACT and HACT must all be defined!";
#endif

#if !defined(SER)
#define SER (1.0H - HorizontalSync)
#endif

/* number of lines of extra vertical back porch needed for cheap xtal eyes */
#if !defined(EXTRA_VBP)
#define EXTRA_VBP 0
#endif

#define mks(x) #x
#define MAKE_FMT_NAME(w,h,f)	mks(w) + "x" + mks(h) + "_" + mks(f) + "s"

#if !defined(FORMAT_NAME)
#define FORMAT_NAME MAKE_FMT_NAME( HACT, VACT, FPS )
#endif

#if !defined(VFP)
#define VFP ((0.0042 * VACT) + 1)
#endif

#if !defined(VSYNC)
#define VSYNC ((0.0042 * VACT) + 1)
#endif

#if !defined(VFUDGE)
#define VFUDGE 0
#endif
#if !defined(VNFUDGE)
#define VNFUDGE 0
#endif

#if !defined(VBP)
#define VBP ((0.05 * VACT) - VFP - VSYNC + EXTRA_VBP)
#endif

#if !defined(HFP)
#define HFP (0.015625 * HACT)
#endif

#if !defined(HSYNC)
#define HSYNC (0.125 * HACT)
#endif

#if !defined(HBP)
#define HBP ((0.25 * HACT) - HFP - HSYNC)
#endif

General
{
    int i;
    ActivePixelsPerLine = HACT;
    ActiveLinesPerFrame = VACT;
    FramesPerSecond = FPS / 2;
    FieldsPerFrame = 2;
    TotalLinesPerFrame = 2 * (VSYNC + VBP + VACT + VFP) + VFUDGE - VNFUDGE;
    i = (HSYNC + HBP + HACT + HFP + 4-1) / 4;
    TotalPixelsPerLine = i*4;
    FormatName = FORMAT_NAME;
    SerratedCSyncOnHSync = true;
}

#define CVT_PIXEL_CLOCK(p) ((p * VideoClockRatioNumerator) / VideoClockRatioDenominator)
#define CVT_CLOCK_PIXEL(c) ((c * VideoClockRatioDenominator) / VideoClockRatioNumerator)

Active Line
{
    int   iHFPc, iHFPp;
    int   iHSc, iHSp;
    int   iHBPp;

    iHFPc = CVT_PIXEL_CLOCK( HFP );
    iHFPp = CVT_CLOCK_PIXEL( iHFPc );
    iHSc = CVT_PIXEL_CLOCK( HSYNC );
    iHSp = CVT_CLOCK_PIXEL( iHSc );
    iHBPp = TotalPixelsPerLine - iHFPp - iHSp - ActivePixelsPerLine;

#if defined(DEBUG)
	info "tot " + TotalPixelsPerLine + " fp " + iHFPp + " s " + iHSp + " bp " + iHBPp + " act " + ActivePixelsPerLine;
#endif

    HorizontalFrontPorch = pixels( iHFPp );
    HorizontalSync = pixels( iHSp );
    HorizontalBackPorch = pixels( iHBPp );
}

Field
{
    eye = { Left };
    swap = true;

#if defined(USE_SER)
	Vertical Sync =
	{
	    repeat VSYNC
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = SER;
	    }
	}

	Vertical Back Porch =
	{
	    repeat VBP
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = HorizontalSync;
	    }
	}
#else
	Vertical Sync =
	{
	    {
		Length = 1.0H;
		Low = 0 usec;
	    }
	    repeat VSYNC - 1
	    {
		Length = 1.0H;
	    }
	}

	Vertical Back Porch =
	{
	    {
		Length = 1.0H;
		High = HorizontalSync;
	    }
	    repeat VBP - 1
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = HorizontalSync;
	    }
	}
#endif

    Active =
    {
	repeat ActiveLinesPerFrame
	{
	    Length = 1.0H;
	    Low = 0 usec;
	    High = HorizontalSync;
	}
    }

    Vertical Front Porch =
    {
	repeat VFP
	{
	    Length = 1.0H;
	    Low = 0 usec;
	    High = HorizontalSync;
	}
    }
}

Field
{
    eye = { Right };
    swap = false;

#if defined(USE_SER)
	Vertical Sync =
	{
	    repeat (VSYNC / 2)
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = SER;
	    }
	}

	Vertical Back Porch =
	{
	    repeat (VSYNC - (VSYNC / 2)) + VBP
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = HorizontalSync;
	    }
	}
#else
	Vertical Sync =
	{
	    {
		Length = 1.0H;
		Low = 0 usec;
	    }
	    repeat (VSYNC / 2) - 1
	    {
		Length = 1.0H;
	    }
	}

	Vertical Back Porch =
	{
	    {
		Length = 1.0H;
		High = HorizontalSync;
	    }
	    repeat (VSYNC - (VSYNC / 2)) - 1 + VBP
	    {
		Length = 1.0H;
		Low = 0 usec;
		High = HorizontalSync;
	    }
	}
#endif

    Active =
    {
	repeat ActiveLinesPerFrame
	{
	    Length = 1.0H;
	    Low = 0 usec;
	    High = HorizontalSync;
	}
    }

    Vertical Front Porch =
    {
	repeat VFP
	{
	    Length = 1.0H;
	    Low = 0 usec;
	    High = HorizontalSync;
	}
    }
}
