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

📄 lpc.c

📁 Xbox主机破解,烧录晶片专用的程式,可烧录256KB的49lf020晶片,使用LPC port
💻 C
字号:
#include "lpc-h.h"

Uword LPT=0x378;  		 /* Location of LPT data port */
Ubyte AbortLPC;
unsigned int LPC_Delay=0;

static Ubyte Lpt_Data;   /* Byte reflects LPT data port */
static Ubyte SyncError;

static void (*Ldout)(Ubyte Nibble);
static Ubyte (*Ldin)(void);

void DelayLoop(void)
{ unsigned int i;

  for(i=LPC_Delay;i;i--)
    /* */;
 }

void Lclk_L(void)
{ DelayLoop();

  Lpt_Data = (Ubyte)((Lpt_Data & DATAMASK) | LFRAME | LRST);
  Outportb(DATA,Lpt_Data);
 }

void Lclk_H(void)
{ DelayLoop();

  Lpt_Data = (Ubyte)((Lpt_Data & DATAMASK) | LCLK | LFRAME | LRST);
  Outportb(DATA,Lpt_Data);
 }

void Lcf_L(void)
{ DelayLoop();

  Lpt_Data = (Ubyte)((Lpt_Data & DATAMASK) | LRST);
  Outportb(DATA,Lpt_Data);
 }

void Lf_L(void)
{ DelayLoop();

  Lpt_Data = (Ubyte)((Lpt_Data & DATAMASK) | LCLK | LRST);
  Outportb(DATA,Lpt_Data);
 }

/* Rev 1 hardware */

void Rev1_Ldout(Ubyte Nibble)
{ DelayLoop();
  Outportb(CTRL,(Ubyte)((Nibble&0x0f)^0x0b));
 }

Ubyte Rev1_Ldin(void)
{ DelayLoop();
  return ((Ubyte)((Inportb(CTRL)&0x0f)^0x0b));
 }

/* Rev 2 hardware */
#define OE	0x80		/* Output enable bit */

void Rev2_Ldout(Ubyte Nibble)
{ DelayLoop();
  Lpt_Data = (Ubyte)((Lpt_Data & CTRLMASK)|((Nibble &0x0f)<<3));
  Outportb(DATA,Lpt_Data);
 }

Ubyte Rev2_Ldin(void)
{ DelayLoop();
  return ((Ubyte)((Inportb(STATUS)>>3)&0x0f));
 }

int Rev2_Detect(void)
{ Rev2_Ldout(0x0f);
  delay(1);
  if (Rev2_Ldin()!=0x0f)
    return(0);

  Rev2_Ldout(0x00);
  delay(1);

  if (Rev2_Ldin()==0x00)
    return (1);
  else
    return (0);
 }

void WriteNibbles(Ubyte N1, Ubyte N2)
{
  Ldout(N1);
  Lclk_H();
  Lclk_L();
  Ldout(N2);
  Lclk_H();
  Lclk_L();
 }

Ubyte ReadByte(void)
{ Ubyte Byte;

  Lclk_H();
  Byte=Ldin();
  Lclk_L();
  Lclk_H();
  Byte|= (Ubyte)(Ldin() <<4);
  Lclk_L();
  return(Byte);
 }

Ubyte ReadNibble(void)
{ Ubyte Byte;

  Lclk_H();
  Byte=Ldin();
  Lclk_L();

  return(Byte);
 }

void Addr(Uword Bank, Uword Ad, Ubyte Mode)
{ Ubyte Byte;

  Lcf_L();
  Ldout(0x00);
  Lclk_H();
  Lclk_L();
  Ldout(Mode);
  Lclk_H();
  Lclk_L();

  Byte = HI_BYTE(Bank);
  WriteNibbles(HI_NIB(Byte),LO_NIB(Byte));
  Byte = LO_BYTE(Bank);
  WriteNibbles(HI_NIB(Byte),LO_NIB(Byte));
  Byte = HI_BYTE(Ad);
  WriteNibbles(HI_NIB(Byte),LO_NIB(Byte));
  Byte = LO_BYTE(Ad);
  WriteNibbles(HI_NIB(Byte),LO_NIB(Byte));

 }

void FlashWrite(Uword Bank,Uword Ad,Ubyte Byte)
{ Ubyte Nibble;
  char Buffer[256];

  Addr(Bank,Ad,WRITE);
  WriteNibbles(LO_NIB(Byte),HI_NIB(Byte));
  WriteNibbles(0x0f,0x0f);

/* Old code - expects LPC to be ready
   if ((Nibble=Ldin())!=0x00)
    printf("Wr: Expecting Sync <0000>, but found %x\n",(int) Nibble);
 */

  while ((Nibble=Ldin())!=0x00)
  { if ((Nibble==0x05) || (Nibble==0x06))
    { Lclk_H();
      Lclk_L();
      }
    else
    { sprintf(Buffer,"Wr: Expecting Sync <0000>, but found 0x%x",(int) Nibble);
      Puts(Buffer);
      SyncError=1;
      break;
     }
   }

  ReadByte();
 }

Ubyte FlashRead(Uword Bank,Uword Ad)
{ Ubyte result, Nibble;
  char Buffer[256];

  Addr(Bank,Ad,READ);
  WriteNibbles(0x0f,0x0f);

/* Old code - expects LPC to be ready
   if ((Nibble=Ldin())!=0x00)
    printf("Wr: Expecting Sync <0000>, but found %x\n",(int) Nibble);
 */

  while ((Nibble=Ldin())!=0x00)
  { if ((Nibble==0x05) || (Nibble==0x06))
    { Lclk_H();
      Lclk_L();
      }
    else
    { sprintf(Buffer,"Rd: Expecting Sync <0000>, but found 0x%x",(int) Nibble);
      Puts(Buffer);
      SyncError=1;
      break;
     }
   }

  result=ReadByte();
  ReadByte();
  return(result);
 }


void FlashReset(void)
{
  Outportb(DATA,LCLK | LFRAME );
  delay(1);
  Outportb(DATA,(Lpt_Data = LCLK | LFRAME | LRST));
 }

void FlashByteWrite(Uword Bank,Uword Addr, Ubyte Data)
{ Ubyte Status;

  FlashWrite(0x0000,0x5555,0xAA);
  FlashWrite(0x0000,0x2AAA,0x55);
  FlashWrite(0x0000,0x5555,0xA0);
  FlashWrite(Bank,Addr,Data);

//  do {
      Status=FlashRead(Bank,Addr);
//   } while(Status!=FlashRead(Bank,Addr));

 }

int FlashErase(void)
{ Uword i, AddrL, AddrH, Status;

  FlashReset();
  for(i=0;i<16;i++)
  { AddrL = (Uword)(i<<14);
    AddrH = (Uword)(i>>2);

    FlashWrite(0x0000,0x5555,0xAA);
    FlashWrite(0x0000,0x2AAA,0x55);
    FlashWrite(0x0000,0x5555,0x80);
    FlashWrite(0x0000,0x5555,0xAA);
    FlashWrite(0x0000,0x2AAA,0x55);
    FlashWrite(AddrH,AddrL,0x50);

    do
    { Status=FlashRead(AddrH,AddrL);
     } while(Status!=FlashRead(AddrH,AddrL));
   }

  return(1);
 }

int BlankCheck(void)
{ Uword error,i,j;
  Ulong n;

  char Buffer[255];

  error =16;
  SetProgress(64);

  FlashReset();
  for(j=0;j<4;j++)
  { for(n=0,i=0;n<0x10000L;n++,i++)
    { if (FlashRead(j,i)!=0xff)
      { if (error)
        { error--;
          sprintf(Buffer,"Address 0x%01x%04x=%02x != 0xff",j,(Uword)i,FlashRead(j,i));
          Puts(Buffer);
         }
        else
   	    break;
	    }

      if ((i&0x0fff)==0)
        StepProgress();

      if (AbortLPC)
        return(0);
	   }
	 }

   if (error==16)
   { SetProgress(0);
     Puts("Blank check completed");
     return(1);
    }
   else
   { Puts("Blank check Aborted");
     return(0);
    }
 }

Uword ReadFlashID(void)
{ Ubyte Mfg, Device;
  char Buffer[255];

  FlashWrite(0x0000,0x5555,0xAA);
  FlashWrite(0x0000,0x2AAA,0x55);
  FlashWrite(0x0000,0x5555,0x90);

  Mfg=FlashRead(0x0,0x0);
  Device=FlashRead(0x0,0x1);

  FlashWrite(0x0000,0x0000,0xF0);

  sprintf(Buffer,"Mfg=0x%x Device=0x%x",Mfg,Device);
  Puts(Buffer);
  return((Uword)((Mfg<<8)|Device));
 }

void ReadChip(char *file)
{ FILE *outfile;
  Uword i,j;
  Ulong n;

  if (!(outfile=fopen(file,"wb")))
  { Puts("Cannot open file for writing");
    return;
   }

  SetProgress(64);
  FlashReset();

  for(j=0;j<4;j++)
  { for(i=0,n=0;n<0x10000L;n++,i++)
    { fputc(FlashRead((Uword)j,(Uword)i),outfile);

      if ((i&0x0fff)==0)
        StepProgress();
      if (AbortLPC)
      { fclose(outfile);
        return;
       }
	  }
	 }
	fclose(outfile);
   SetProgress(0);
 }

void WriteChip(char *file, int index)
{ FILE *infile;
  Uword i,j;
  Ulong n;

  if (!(infile=fopen(file,"rb")))
  { Puts("Cannot open file for reading");
    return;
   }

  if (!index)
    fseek(infile,0L,SEEK_SET);
  else
    fseek(infile,-0x40000L,SEEK_END);

  SetProgress(64);
  FlashReset();

  for(j=0;j<4;j++)
  { for(i=0,n=0;n<0x10000L;n++,i++)
    { FlashByteWrite(j,i,(Ubyte)fgetc(infile));
      if ((i&0x0fff)==0)
        StepProgress();

      if (AbortLPC)
      { fclose(infile);
        return;
       }

	  }
	 }
	fclose(infile);
   SetProgress(0);

   Puts("Write completed");
 }

int VerifyChip(char *file, int index)
{ FILE *infile;
  Uword i,j,error;
  Ulong n;
  Ubyte rddata,data;
  char Buffer[256];

  if (!(infile=fopen(file,"rb")))
  { Puts("Cannot open file for reading");
    return 0;
   }

  if (!index)
    fseek(infile,0L,SEEK_SET);
  else
    fseek(infile,-0x40000L,SEEK_END);

  SetProgress(64);
  FlashReset();
  error=16;

  for(j=0;j<4;j++)
  { for(i=0,n=0;n<0x10000L;n++,i++)
    { data=(Ubyte)fgetc(infile);
      rddata=FlashRead(j,i);

      if (rddata!=data)
	   { if (error)
	     { error--;
	       sprintf(Buffer,"Address 0x%01x%04x=0x%02x != 0x%02x",j,(Uword)i,rddata,data);
          Puts(Buffer);
	      }
	     else
     	    break;
	    }
      if ((i&0x0fff)==0)
        StepProgress();

      if (AbortLPC)
      { fclose(infile);
        return(0);
       }

	  }
	 }
	fclose(infile);
   SetProgress(0);

   if (error==16)
   { Puts("Verify completed");
     return(1);
    }
   else
   { Puts("Verify Aborted");
     return(0);
    }
 }

void SetDriver(int rev)
{ if (rev)
   { Ldout = Rev2_Ldout;
     Ldin  = Rev2_Ldin;
     Puts("LPC^2 rev.2 detected. Using Rev.2 driver");
    }
   else
   { Ldout = Rev1_Ldout;
     Ldin  = Rev1_Ldin;
     Puts("LPC^2 rev.2 not detected. Using Rev.1 driver");
    }
 }

void InitFlash(void)
{
   FlashReset();
 }

⌨️ 快捷键说明

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