/*******************************************************************************/

/*
 *      rmns2ktvset.c -- REALmagic NS2000TV setting application
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    originally written by Jacques Mahe <jacques_mahe@sdesigns.com>
 *                    adapted by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "define.h"
#include "ns2keprm.h"
#include "rmns2ktvset.h"

DWORD get_TvStandard (DWORD *pVal)
{
  DWORD sizeOut, checkPedestal;
  
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  SCANCONVERTER_SET, escTvStandard, 0, pVal, sizeof(*pVal), &sizeOut) != OK)
    return NOTGOOD;
  
  if(sizeOut != sizeof(*pVal))
    {
      return NOTGOOD;
    }
  
  if (*pVal & SET_PAL)
    *pVal = SET_PAL;
  else if (*pVal & SET_480P)
    *pVal = SET_480P;
  else
    {
      *pVal = SET_NTSC;
      
      // Check for NTSC Pedestal
      if (FMPProperty (KSPROPERTY_TYPE_GET,
		       SCANCONVERTER_SET, escNtscPedestal, 0, &checkPedestal, sizeof(checkPedestal), &sizeOut) == OK)
	{
	  if(sizeOut != sizeof(checkPedestal))
	    return NOTGOOD;
	  if (checkPedestal)
	    *pVal = escNtscPedestal;
	}
    }

  G_PRINT("\n\n Get TV standard !! \n\n");
  return OK; 
}

DWORD put_TvStandard(DWORD newVal)
{
  DWORD currentValue, sizeOut, fVal;
  

  if ( (SET_NTSC != newVal)
       && (SET_PAL != newVal) 
       && (escNtscPedestal != newVal) 
       && (SET_480P != newVal) )
    return NOTGOOD;
  
  // Check if value is different then curren
  if ( (SUCCEEDED(get_TvStandard(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
  
  // Set the focus TV output to new standard
  if( newVal != escNtscPedestal)
    fVal = newVal;
  else
    fVal = SET_NTSC;
  
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escTvStandard, 0, &fVal, sizeof(fVal), &sizeOut) == OK)
    { 
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      // Set pedestal
      fVal = (escNtscPedestal == newVal);
      FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escNtscPedestal, 0, &fVal, sizeof(fVal), &sizeOut);
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      return OK;
    }
  return NOTGOOD;
}

DWORD get_TvOutputMode(DWORD *pVal)
{
  DWORD sizeOut;
  
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escOutputFormat, 0, pVal, sizeof(*pVal), &sizeOut ) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(*pVal))
    return NOTGOOD;
  return OK; 
}

DWORD put_TvOutputMode(DWORD newVal)
{
  DWORD currentValue, fVal, sizeOut;
  if ( (COMPOSITE != newVal)
       && (COMPONENT_YUV != newVal)
       && (COMPONENT_RGB != newVal)
       && (OUTPUT_OFF != newVal) )
    return NOTGOOD;
	
  // Check if value is different then current
  
  if ( (SUCCEEDED(get_TvOutputMode(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
  
  // Set the focus TV output to new standard
  fVal = newVal;
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escOutputFormat, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;
}

DWORD get_TvScart(DWORD *pVal)
{
  DWORD sizeOut;
  
  if (pVal == NULL)
    return NOTGOOD;
      
  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   VIDEO_SET, evScartOutput, 0, pVal, sizeof(*pVal), &sizeOut) != OK)
      return NOTGOOD;
  if(sizeOut!=sizeof(*pVal))
    return NOTGOOD;
	 
  return OK; 
}

DWORD put_TvScart(DWORD newVal)
{
  DWORD currentValue, fVal, sizeOut;

  if ( (SCART_RGB != newVal)
       && (SCART_COMPOSITE != newVal) 
       && (SCART_DISABLE != newVal) )
    return NOTGOOD;
  
  // Check if value is different then current
  if ( (SUCCEEDED(get_TvScart(&currentValue)))
       && (currentValue == newVal) )
    return OK;	   
  // Set the focus TV output to new standard
  fVal = newVal;
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   VIDEO_SET, evScartOutput, 0, &fVal, sizeof(DWORD), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  
  return OK;
}

DWORD get_TvVerticalPosition(long *pVal)
{
  DWORD sizeOut, fVal;
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escHwOutputVertOffsShadow, 0, &fVal, sizeof(DWORD), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      
      *pVal = (long) (short) fVal;
    }
  else
    {
      *pVal = HPO_RESET;	// Should never happen!
      return NOTGOOD;
    }
 
  return OK;
}

DWORD put_TvVerticalPosition(long newVal)
{
  long currentValue;
  DWORD fVal, sizeOut;

  // Verify this is a new value  
  if ( (SUCCEEDED(get_TvVerticalPosition(&currentValue)))
       && (currentValue == newVal) )
    return OK;

  fVal = max(HPO_MIN, min(HPO_MAX, newVal));
  
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escHwOutputVertOffsShadow, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK; 
}

DWORD get_TvHorizontalPosition(long *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;
	
  // Get the focus TV output to new standard  
  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escHwOutputHorzOffsShadow, 0, &fVal, sizeof(fVal), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (long) (short) fVal;
    }
  else
    {
      *pVal = HPO_RESET;	// Should never happen!
      return NOTGOOD;
    }
  
  return OK;  
}

DWORD put_TvHorizontalPosition(long newVal)
{
  long currentValue;
  DWORD fVal, sizeOut;

  // Verify this is a new value
  if ( (SUCCEEDED(get_TvHorizontalPosition(&currentValue)))
       && (currentValue == newVal) )
    return OK;

  fVal = max(HPO_MIN, min(HPO_MAX, newVal));

  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escHwOutputHorzOffsShadow, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
  return OK;
}

DWORD get_TvHeight(long *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;
 
  // Get the focus TV output to new standard
  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escTVLines, 0, &fVal, sizeof(fVal), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (long) (short) fVal;
    }
  else
    {
      *pVal = VSS_RESET;	// Should never happen!
      return NOTGOOD;
    }
  return OK;
}

DWORD put_TvHeight(long newVal)
{
  // Set the focus TV output to new standard
  long currentValue;
  DWORD fVal, sizeOut;
  
  // Verify this is a new value
  if ( (SUCCEEDED(get_TvHeight(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(VSS_MIN, min(VSS_MAX, newVal));
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escTVLines, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK; 
}

DWORD get_TvWidth(long *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escTVPixels, 0, &fVal, sizeof(fVal), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (long) (short) fVal;
    }
  else
    {
      *pVal = HSS_RESET;	// Should never happen !
      return NOTGOOD;
    }
	
  return OK; 
}

DWORD put_TvWidth(long newVal)
{
  DWORD fVal, sizeOut;
  long currentValue;

  // Verify this is a new value  
  if ( (SUCCEEDED(get_TvWidth(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
  
  fVal= max(HSS_MIN, min(HSS_MAX, newVal));
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escTVPixels, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK; 
}

DWORD get_TvBrightness(short *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;
  
  // Get the focus TV output to new standard
  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escBrightness, 0, &fVal, sizeof(fVal), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (short) fVal;
    }
  else
    {
      *pVal = BRT_RESET;	// Should never happen!
      return NOTGOOD;
    }
  
  return OK;
}

DWORD put_TvBrightness(short newVal)
{
  DWORD sizeOut, fVal;
  short currentValue;
  // Verify this is a new value
	
  if ( (SUCCEEDED(get_TvBrightness(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(BRT_MIN, min(BRT_MAX, newVal));
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   SCANCONVERTER_SET, escBrightness, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;
}

DWORD get_TvContrast(short *pVal)
{
  DWORD sizeOut, fVal;
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escContrast, 0, &fVal, sizeof(fVal), &sizeOut ) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (short) fVal;
    }
  else
    {
      *pVal = CON_RESET;	// Should never happen!
      return NOTGOOD;
    }
  
  return OK;
}

DWORD put_TvContrast(short newVal)
{
  DWORD sizeOut, fVal;
  short currentValue;

  // Verify this is a new value
  if ( (SUCCEEDED(get_TvContrast(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(CON_MIN, min(CON_MAX, newVal));
  if (FMPProperty(KSPROPERTY_TYPE_SET,
		  SCANCONVERTER_SET, escContrast, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;
}

DWORD get_TvSaturation(short *pVal)
{
  DWORD sizeOut, fVal;
  if (pVal == NULL)
    return NOTGOOD;
  
  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  SCANCONVERTER_SET, escSaturation, 0 ,&fVal , sizeof(fVal), &sizeOut ) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (short) fVal;
    }
  else
    {
      *pVal = CSC_RESET;	// Should never happen!
      return NOTGOOD;
    }
	
  return OK;
}

DWORD put_TvSaturation(short newVal)
{
  DWORD sizeOut, fVal;
  short currentValue;

  // Verify this is a new value
	
  if ( (SUCCEEDED(get_TvSaturation(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(CSC_MIN, min(CSC_MAX, newVal));
  if (FMPProperty(KSPROPERTY_TYPE_SET,
		  SCANCONVERTER_SET, escSaturation, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;
}

DWORD get_TvSharpness(short *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;

  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  SCANCONVERTER_SET, escSharpness, 0, &fVal, sizeof(fVal), &sizeOut ) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (short) fVal;
    }
  else
    {
      *pVal = SHP_RESET;	// Should never happen!
      return NOTGOOD;
    }
  
  return OK;
}

DWORD put_TvSharpness(short newVal)
{
  short currentValue;
  DWORD sizeOut, fVal;
  
  // Verify this is a new value
  
  if ( (SUCCEEDED(get_TvSharpness(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(SHP_MIN, min(SHP_MAX, newVal));
  if (FMPProperty(KSPROPERTY_TYPE_SET,
		  SCANCONVERTER_SET, escSharpness, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;  
}

DWORD get_TvFlicker(short *pVal)
{
  DWORD sizeOut, fVal;
  
  if (pVal == NULL)
    return NOTGOOD;
   
  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  SCANCONVERTER_SET, escFlicker, 0, &fVal, sizeof(fVal), &sizeOut ) == OK)
    {
      if(sizeOut!=sizeof(fVal))
	return NOTGOOD;
      *pVal = (short) fVal;
    }
  else
    {
      *pVal = FLK_RESET;	// Should never happen!
      return NOTGOOD;
    }
	
  return OK;
}

DWORD put_TvFlicker(short newVal)
{
  DWORD sizeOut, fVal;
  short currentValue;
  // Verify this is a new value
  if ( (SUCCEEDED(get_TvFlicker(&currentValue)))
       && (currentValue == newVal) )
    return OK;		
	
  fVal = max(FLICKER_MIN, min(FLICKER_MAX, newVal));
  
  if (FMPProperty(KSPROPERTY_TYPE_SET,
		  SCANCONVERTER_SET, escFlicker, 0, &fVal, sizeof(fVal), &sizeOut) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(fVal))
    return NOTGOOD;
  return OK;
}

DWORD TvReset()
{
  // Read/Write back TV standard which will reset everything
  DWORD sizeOut, fVal;
  BOOL result = TRUE;

  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  SCANCONVERTER_SET, escTvStandard, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
    {
      result = FALSE;
    }
  else
    if (FMPProperty (KSPROPERTY_TYPE_SET,
		     SCANCONVERTER_SET, escTvStandard, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
      {
	result = FALSE;
      }
	
  if (FMPProperty (KSPROPERTY_TYPE_GET,
		   SCANCONVERTER_SET, escOutputFormat, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
    {
      result = FALSE;
    }
  else
    if (FMPProperty (KSPROPERTY_TYPE_SET,
		     SCANCONVERTER_SET, escOutputFormat, 0, &fVal, sizeof(fVal), &sizeOut ) != OK)
      {
	result = FALSE;
      }

  if (fVal == SET_NTSC)
    {
      put_TvVerticalPosition(38);
      put_TvHorizontalPosition(178);
      put_TvWidth(658);
      put_TvHeight(412);
    }
  else
    if (fVal == SET_PAL)
      {
	put_TvVerticalPosition(44);
	put_TvHorizontalPosition(230);
	put_TvWidth(820);
	put_TvHeight(496);
      }
    else
      {
	// assert (escTvStandard == RMTV_480P);
	put_TvVerticalPosition(38);
	put_TvHorizontalPosition(178);
	put_TvWidth(658);
	put_TvHeight(412);
      }
  put_TvBrightness(BRT_RESET);
  put_TvContrast(CON_RESET);
  put_TvSaturation(CSC_RESET);
  put_TvSharpness(SHP_RESET);
  put_TvFlicker(8);

  if(!result)
    return NOTGOOD;
  return OK;	// Always return OK or else can generate problems in HTML
}

/////////////////////////////////////////////////////////////////////////////
// Save initial settings into EEPROM
DWORD ReadEEprom(BYTE bAddress, BYTE *pRetValue)
{
  DWORD sizeOut;
  RM_WRITE PropInfo;

  if (pRetValue == NULL)
    return NOTGOOD;
     
  PropInfo.Addr = bAddress;
  PropInfo.Data = 0;
	
  if (FMPProperty(KSPROPERTY_TYPE_GET,
		  EEPROM_SET, eEepromAccess, 0, &PropInfo, sizeof(PropInfo), &sizeOut) == OK)
    {
      if(sizeOut!=sizeof(PropInfo))
	return NOTGOOD;
      *pRetValue = LOBYTE(PropInfo.Data);
      return OK;
    }
  else
    return NOTGOOD;
}

DWORD WriteEEprom(BYTE bAddress, BYTE bValue)
{
  BYTE bCurrentEEpromValue = 0;
  RM_WRITE PropInfo;
  DWORD sizeOut;
  
  // Verify this is a new value
  if ( (SUCCEEDED(ReadEEprom(bAddress, &bCurrentEEpromValue)))
       && (bCurrentEEpromValue == bValue) )
    return OK;
 
  PropInfo.Addr = bAddress;
  PropInfo.Data = bValue;
  
  if (FMPProperty (KSPROPERTY_TYPE_SET,
		   EEPROM_SET, eEepromAccess, 0, &PropInfo, sizeof(PropInfo), &sizeOut ) != OK)
    return NOTGOOD;
  if(sizeOut!=sizeof(PropInfo))
    return NOTGOOD;
  return OK;
}

/* DWORD TvResetFromEeprom() */
/* { */
/*   BOOL result = TRUE;
 */
/*   // Read current RM_EEPROM_MUX
 */
/*   BYTE bCurrentEEpromValue = 0;
 */
/*   DWORD CurrentTvStandard;
 */
/*   DWORD CurrentTvOutputMode;
 */
/*   DWORD CurrentTvScart;
 */
/*   // Let's construct our EEPROM values
 */
/*   BYTE bNewEEpromMux = 0;
 */
/*   BYTE bNewEEPromStdMode = 0;
 */
  
/*   short currentValue;
 */
/*   long lCurrentValue; */
  
/*   if (FAILED(ReadEEprom(RM_EEPROM_MUX, &bCurrentEEpromValue)))
 */
/*     bCurrentEEpromValue = 0;
 */
/*   // Check if value is different then current
 */
  
/*   if (FAILED(get_TvStandard(&CurrentTvStandard))) */
/*     CurrentTvStandard = SET_NTSC; */
/*   // Check if value is different then current */
/*   if (FAILED(get_TvOutputMode(&CurrentTvOutputMode))) */
/*     CurrentTvOutputMode = COMPOSITE; */

/*   if (FAILED(get_TvScart(&CurrentTvScart))) */
/*     CurrentTvScart = SCART_DISABLE; */





/* } */

DWORD ApplyToStartup()
{
  BOOL result = TRUE;
  // Read current RM_EEPROM_MUX
  BYTE bCurrentEEpromValue = 0;
  DWORD CurrentTvStandard;
  DWORD CurrentTvOutputMode;
  DWORD CurrentTvScart;
  // Let's construct our EEPROM values
  BYTE bNewEEpromMux = 0;
  BYTE bNewEEPromStdMode = 0;
	
  short currentValue;
  long lCurrentValue;
	
  if (FAILED(ReadEEprom(RM_EEPROM_MUX, &bCurrentEEpromValue)))
    bCurrentEEpromValue = 0;

  // Check if value is different then current
	
  if (FAILED(get_TvStandard(&CurrentTvStandard)))
    CurrentTvStandard = SET_NTSC;

  // Check if value is different then current
	
  if (FAILED(get_TvOutputMode(&CurrentTvOutputMode)))
    CurrentTvOutputMode = COMPOSITE;

	
  if (FAILED(get_TvScart(&CurrentTvScart)))
    CurrentTvScart = SCART_DISABLE;

	

  if (CurrentTvOutputMode != COMPOSITE)
    {
      if (CurrentTvOutputMode == OUTPUT_OFF)
	{
	  // Both ENMUX0 and ENMUX1 need to set to 1, I_ENMUX1 is inverted (0)
	  bNewEEpromMux |= RMMASK_ENMUX0;
	  bNewEEPromStdMode |= RM_SET_OUTPUT_MODE(RM_EEPROM_TV_OFF);
	}
      else
	{
	  // RMMASK_YSYNC_EN has to be set when outputting in RGB or YUV
	  bNewEEpromMux |= RMMASK_ENMUX0 | RMMASK_I_ENMUX1 | RMMASK_YSYNC_EN;
	  if (CurrentTvOutputMode == COMPONENT_RGB)
	    bNewEEPromStdMode |= RM_SET_OUTPUT_MODE(RM_EEPROM_RGB);
	  else
	    bNewEEPromStdMode |= RM_SET_OUTPUT_MODE(RM_EEPROM_YUV);
	}
		
      // Enable 480 Progressive when needed
      if (CurrentTvStandard == SET_480P)
	{
	  bNewEEPromStdMode |= RM_SET_EEPROM_STANDARD(RM_EEPROM_480P);
	  bNewEEpromMux |= RMMASK_EN480P;
	}
		
      // Set the SCART RGB if outputting to RGB
      if (CurrentTvOutputMode == COMPONENT_RGB)
	bNewEEpromMux |= RMMASK_SETRGB;
    }
  // Else, all values are 0 for composite

  // Set the SCART
  if (CurrentTvScart != SCART_DISABLE)
    {
      bNewEEpromMux |= RMMASK_SCT_ENB;
      bNewEEPromStdMode |= RM_SET_EEPROM_SCART(RM_EEPROM_SCART_4x3);
    }

  if (CurrentTvStandard == escNtscPedestal) 
    bNewEEPromStdMode |= RM_SET_EEPROM_PEDESTAL;
  else
    if (CurrentTvStandard == SET_PAL)
      bNewEEPromStdMode |= RM_SET_EEPROM_STANDARD(RM_EEPROM_PAL);

  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_MUX, &bCurrentEEpromValue)))
       && (bNewEEpromMux != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_MUX, bNewEEpromMux)))
	result = FALSE ;
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_STD_MODE, &bCurrentEEpromValue)))
       && (bNewEEPromStdMode != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_STD_MODE, bNewEEPromStdMode)))
	result = FALSE ; // assert (FALSE);
    }

  // The easy part
	

  if (SUCCEEDED(get_TvBrightness(&currentValue)))
    {
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_BRIGHTNESS, &bCurrentEEpromValue)))
	   && (LOBYTE(currentValue) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_BRIGHTNESS, LOBYTE(currentValue))))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvContrast(&currentValue)))
    {
      // assert (currentValue >= 0);
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_CONTRAST, &bCurrentEEpromValue)))
	   && (LOBYTE(currentValue - 32) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_CONTRAST, currentValue - 32)))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvSaturation(&currentValue)))
    {
      // assert (currentValue >= 0);
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_SATURATION, &bCurrentEEpromValue)))
	   && (LOBYTE(currentValue - 32) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_SATURATION, currentValue - 32)))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvSharpness(&currentValue)))
    {
      // assert (currentValue >= 0);
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_SHARPNESS, &bCurrentEEpromValue)))
	   && (LOBYTE(currentValue - 8) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_SHARPNESS, currentValue - 8)))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvFlicker(&currentValue)))
    {
      // assert (currentValue >= 0);
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_FLICKER, &bCurrentEEpromValue)))
	   && (LOBYTE(currentValue - 8) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_FLICKER, currentValue - 8)))
	    result = FALSE ;// assert (FALSE);
	}
    }

	
  if (SUCCEEDED(get_TvHorizontalPosition(&lCurrentValue)))
    {
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_OFFSET_LOW, &bCurrentEEpromValue)))
	   && ((LOBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_OFFSET_LOW, LOBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_OFFSET_HIGH, &bCurrentEEpromValue)))
	   && ((HIBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_OFFSET_HIGH, HIBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvVerticalPosition(&lCurrentValue)))
    {
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_OFFSET_LOW, &bCurrentEEpromValue)))
	   && ((LOBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_OFFSET_LOW, LOBYTE(lCurrentValue))))
	    result = FALSE ;	// assert (FALSE);
	}
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_OFFSET_HIGH, &bCurrentEEpromValue)))
	   && ((HIBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_OFFSET_HIGH, HIBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvHeight(&lCurrentValue)))
    {
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_TV_LINES_LOW, &bCurrentEEpromValue)))
	   && ((LOBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_TV_LINES_LOW, LOBYTE(lCurrentValue))))
	    result = FALSE ;	// assert (FALSE);
	}
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_TV_LINES_HIGH, &bCurrentEEpromValue)))
	   && ((HIBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_TV_LINES_HIGH, HIBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
    }
  if (SUCCEEDED(get_TvWidth(&lCurrentValue)))
    {
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_TV_PIXELS_LOW, &bCurrentEEpromValue)))
	   && ((LOBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_TV_PIXELS_LOW, LOBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
      if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_TV_PIXELS_HIGH, &bCurrentEEpromValue)))
	   && ((HIBYTE(lCurrentValue)) != bCurrentEEpromValue) )
	{
	  if (FAILED(WriteEEprom(RM_EEPROM_DELTA_TV_PIXELS_HIGH, HIBYTE(lCurrentValue))))
	    result = FALSE ;// assert (FALSE);
	}
    }

  // Write 0 in old EEPROM values (First 200 NS2000TV)
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_POS_LOW, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_POS_LOW, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_POS_HIGH, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_POS_HIGH, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_POS_LOW, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_POS_LOW, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_POS_HIGH, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_POS_HIGH, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_SIZE_LOW, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_SIZE_LOW, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_HOR_SIZE_HIGH, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_HOR_SIZE_HIGH, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_SIZE_LOW, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_SIZE_LOW, 0)))
	result = FALSE ;// assert (FALSE);
    }
  if ( (SUCCEEDED(ReadEEprom(RM_EEPROM_DELTA_VERT_SIZE_HIGH, &bCurrentEEpromValue)))
       && (0 != bCurrentEEpromValue) )
    {
      if (FAILED(WriteEEprom(RM_EEPROM_DELTA_VERT_SIZE_HIGH, 0)))
	result = FALSE ;// assert (FALSE);
    }

  if(!result)
    return NOTGOOD;
  return OK;
}
