⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autofunc.c

📁 LCD OSD 程序用於8051 開發lcd
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <math.h>
#include "types.h"
#include "board.h"
#include "global.h"
#include "ms_reg.h"
#include "adjust.h"
#include "ms_rwreg.h"
#include "misc.h"
#include "debug.h"
#include "power.h"
#include "menudef.h"
#include "msosd.h"
#include "mstar.h"

#define UseWorstPhase	1
#define EvenClock	0
#define QuickAuto	0

#define OverFlow	BIT7
#define NoneOverFlow	BIT6
#define UnderFlow	BIT7
#define NoneUnderFlow	BIT6

extern BYTE MenuPageIndex;

//===============================================================
// local declaration
Bool mStar_AutoAdcColor(BYTE vsyncTime);


//===============================================================
//===============================================================
// Code
Bool CheckSyncLoss(void)
{
#if UseINT
  if (InputTimingChangeFlag)
    { Power_TurnOffPanel();
      return TRUE;
    }
  else
    return FALSE;
#else
  Bool result=FALSE;

  if (abs(SrcHPeriod-mStar_ReadWord(HSPRD_H))>10)
    result=TRUE;
  else if (abs(SrcVTotal-mStar_ReadWord(VTOTAL_H))>10)
    result=TRUE;
  if (result)
    { Power_TurnOffPanel();
      Set_InputTimingChangeFlag();
    }
  return result;
#endif
}


void WaitAutoStatusReady(BYTE regAddr, BYTE regFlag)
{ BYTE waits=200;
  while (waits-- && !(mStar_ReadByte(regAddr)&regFlag));
}

WORD GetAutoValue(BYTE regAddr)
{ WaitAutoStatusReady(ATOCTRL, ATOR_B);
  return mStar_ReadWord(regAddr);
}


BYTE GetTranstionPosition(BYTE vsyncTime, BYTE regAddr)
{ BYTE retry=6;
  BYTE adjPhase;
  WORD pos;
  BYTE minPhase, maxPhase;

  maxPhase=0x3F;
  minPhase=0x0;
  mStar_AdjustPhase(0);
  Delay1ms(vsyncTime);
  pos=GetAutoValue(regAddr);
  adjPhase=(maxPhase+minPhase)/2;
  while (retry--)
    { mStar_AdjustPhase(adjPhase);
      Delay1ms(vsyncTime);
      if (GetAutoValue(regAddr)!=pos)
      	maxPhase=adjPhase;
      else
      	minPhase=adjPhase;
      adjPhase=(maxPhase+minPhase)/2;
      if (CheckSyncLoss())
       return FALSE;
    }
//  printData("trans phase %d", adjPhase);
  return (adjPhase);
}

#if !QuickAuto
BYTE AutoSetDataThreshold(BYTE vsyncTime)
{ BYTE thr;
  BYTE adjPhase;
  WORD regWord1, regWord2;

  for (thr=4; thr<0x10; thr++)
    { mStar_WriteByte(AOVDV, thr<<4);
      mStar_AdjustPhase(0);
      Delay1ms(vsyncTime);
      regWord1=GetAutoValue(AOHST_H);
      for (adjPhase=4; adjPhase<0x40; adjPhase+=4)
      	{ mStar_AdjustPhase(adjPhase);
          Delay1ms(vsyncTime);
          regWord2=GetAutoValue(AOHST_H);
          if (abs(regWord1-regWord2)>3)
            break;
          if (CheckSyncLoss())
            return FALSE;
      	}
      if (adjPhase>=0x40)
      	break;
    }
  return thr;
}
#endif

WORD SearchMaxWidth(BYTE vsyncTime)
{ BYTE startPhase;
  WORD hStart, hEnd;

  mStar_AdjustPhase(0);
  Delay1ms(vsyncTime);
  Delay1ms(vsyncTime);
  hStart=GetAutoValue(AOHST_H);
//  printData("hStart %d", hStart);
  startPhase=GetTranstionPosition(vsyncTime, AOHST_H);
  hEnd=GetAutoValue(AOHEND_H);
//  printData("hEnd %d", hEnd);
  hStart=hEnd-hStart+1;

  return hStart;
}

Bool mStar_AutoHTotal(BYTE vsyncTime)
{ WORD tempHTotal;
  WORD autoWidth;
  BYTE result;

  // 1st to coast tune HTotal
  mStar_AdjustPhase(0);
  Delay1ms(vsyncTime);
  autoWidth=GetAutoValue(AOHEND_H)-GetAutoValue(AOHST_H);

  tempHTotal=StandardModeWidth/3;
  if (abs(autoWidth-StandardModeWidth)>tempHTotal)
    return FALSE;

  tempHTotal=UserPrefHTotal;
  //printData("1st htotal %d", tempHTotal);
  //printData("1st max width %d", autoWidth);
  if (abs(autoWidth-StandardModeWidth)>1)
    { tempHTotal=(DWORD)UserPrefHTotal*StandardModeWidth/autoWidth;
      if (abs(tempHTotal-StandardModeHTotal)>ClockAdjRange)
      	goto AutoHTotalFailed;
      mStar_AdjustHTotal(tempHTotal);
      //printData("2nd htotal %d", tempHTotal);
    }

  autoWidth=SearchMaxWidth(vsyncTime);
  //printData("2nd max width %d", autoWidth);
  // autoWidth=SearchMaxWidth(vsyncTime);
  // 2nd fine tune
  if (autoWidth!=StandardModeWidth)
    { tempHTotal=tempHTotal+(StandardModeWidth-autoWidth);
      mStar_AdjustHTotal(tempHTotal);
      autoWidth=SearchMaxWidth(vsyncTime);
      // printData("3rd htotal %d", tempHTotal);
      // printData("max width %d", autoWidth);
      tempHTotal=tempHTotal+(StandardModeWidth-autoWidth);
    }
  else if (tempHTotal&1)
    { WORD autoWidth2;

      //tempHTotal=tempHTotal-1;
      mStar_AdjustHTotal(tempHTotal-1);
      autoWidth=SearchMaxWidth(vsyncTime);
      mStar_AdjustHTotal(tempHTotal+1);
      autoWidth2=SearchMaxWidth(vsyncTime);
      if (autoWidth2==StandardModeWidth)
        tempHTotal=tempHTotal+1;
      else if (autoWidth==StandardModeWidth)
        tempHTotal=tempHTotal-1;
      //printData("4th htotal %d", tempHTotal);
      //printData("4th max width %d", autoWidth);
      //printData("4th max width2 %d", autoWidth2);
    #if 0
      if (autoWidth!=StandardModeWidth)
        tempHTotal=tempHTotal+(StandardModeWidth-autoWidth);
    #endif
    }

#if EvenClock
 if (tempHTotal%4)
   tempHTotal=((tempHTotal+1)/4)*4;
#endif

AutoHTotalFailed:
  result=FALSE;
  if (abs(tempHTotal-StandardModeHTotal)<60)
    { UserPrefHTotal=tempHTotal;
      result=TRUE;
    }
  mStar_AdjustHTotal(UserPrefHTotal);
  mStar_AdjustPhase(UserPrefPhase);
//  printData("final htotal %d", UserPrefHTotal);

  return result;
#undef result
}

Bool mStar_AutoPhase(BYTE vsyncTime)
{ BYTE adjPhase, i;
#if UseWorstPhase    
  BYTE worsePhase, worsePhase2;
  DWORD mincksum, cksum;

  mincksum=0xFFFFFFFFul;
  for (adjPhase=0; adjPhase<0x40; adjPhase+=3)
    { mStar_AdjustPhase(adjPhase);
      Delay1ms(vsyncTime);
      WaitAutoStatusReady(ATPCTRL, ATPR_B);
      cksum=mStar_ReadWord(ATPV4);
      cksum=(cksum<<16)|mStar_ReadWord(ATPV2);
      if (cksum<mincksum)
      	{ mincksum=cksum;
      	  worsePhase=adjPhase;
      	}
      if (CheckSyncLoss())
      	return FALSE;
    }
//  printData("coast phase %d", bestPhase);
  // 2nd Search
  if (worsePhase<3)
    worsePhase=(worsePhase+61)%0x40; // 64-3
  else
    worsePhase-=3;
  mincksum=0xFFFFFFFFul;
  for (adjPhase=worsePhase; adjPhase<worsePhase+5; adjPhase++)
    { i=adjPhase%0x40;
      mStar_AdjustPhase(i);
      Delay1ms(vsyncTime);
      WaitAutoStatusReady(ATPCTRL, ATPR_B);
      cksum=mStar_ReadWord(ATPV4);
      cksum=(cksum<<16)|mStar_ReadWord(ATPV2);
      if (cksum<mincksum)
      	{ mincksum=cksum;
      	  worsePhase2=i;
      	}
      if (CheckSyncLoss())
      	return FALSE;
    }
  UserPrefPhase=(worsePhase2+31)%0x40;
#else
  BYTE bestPhase, bestPhase2;
  DWORD maxcksum, cksum;

  maxcksum=0;
  for (adjPhase=0; adjPhase<0x40; adjPhase+=3)
    { mStar_AdjustPhase(adjPhase);
      Delay1ms(vsyncTime);
      WaitAutoStatusReady(ATPCTRL, ATPR_B);
      cksum=mStar_ReadWord(ATPV4);
      cksum=(cksum<<16)|mStar_ReadWord(ATPV2);
      if (cksum>maxcksum)
      	{ maxcksum=cksum;
      	  bestPhase=adjPhase;
      	}
      if (CheckSyncLoss())
      	return FALSE;
    }
//  printData("coast phase %d", bestPhase);
  // 2nd Search
  if (bestPhase<3)
    bestPhase=(bestPhase+61)%0x40; // 64-3
  else
    bestPhase-=3;
  maxcksum=0;
  for (adjPhase=bestPhase; adjPhase<bestPhase+5; adjPhase++)
    { i=adjPhase%0x40;
      mStar_AdjustPhase(i);
      Delay1ms(vsyncTime);
      WaitAutoStatusReady(ATPCTRL, ATPR_B);
      cksum=mStar_ReadWord(ATPV4);
      cksum=(cksum<<16)|mStar_ReadWord(ATPV2);
      if (cksum>maxcksum)
      	{ maxcksum=cksum;
      	  bestPhase2=i;
      	}
      if (CheckSyncLoss())
      	return FALSE;
    }
  UserPrefPhase=bestPhase2;
#endif

  mStar_AdjustPhase(UserPrefPhase);
  printData("final phase %d", UserPrefPhase);

  return UserPrefPhase;
}


void mStar_AutoPosition(void)
{ 
WORD height, width;
  WORD actHeight;
  WORD hStart, vStart;

  hStart=GetAutoValue(AOHST_H);
  vStart=GetAutoValue(AOVST_H);

  if (hStart>StandardModeWidth || vStart>StandardModeHeight)
    return;

  width=GetAutoValue(AOHEND_H)-hStart+1;
  height=GetAutoValue(AOVEND_H)-vStart;

  actHeight=mStar_ReadWord(SPRVDC_H);

  if (UserModeFlag)
    { if (height<(StandardModeHeight*2/3) ||    width<(StandardModeWidth*2/3))
        return;
    }
  else if (height<(StandardModeHeight-80) ||    width<(StandardModeWidth-120))
    { return;
    }

  if (actHeight>height)
    { height=(actHeight-height)/2;
      if (vStart>height)
        UserPrefVStart=vStart-height;
      else
      	UserPrefVStart=0;
    }

  UserPrefHStart=hStart;
  if (SrcInputType==Input_Digital)
    UserPrefHStart-=0x10;

  mStar_WriteWord(SPRHST_H, UserPrefHStart);
  mStar_WriteWord(SPRVST_H, UserPrefVStart);

}
//=================================================================================
//=================================================================================
// Auto Color Setting

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -