www.pudn.com > GameCubeJig_LastCodeFile.zip > GCJIG.$$$
/******************************************************/
/* FileName: GameCubeJig.c */
/* for PIC's SoftWare SPI By manual */
/* 16c74b 3.3v 4Mhz */
/* WriteBy : John He */
/* Date : 02/01/8--18 (Last code) */
/* Ver : 2 (Working stats is dis by MemTypeLed */
/* RC4---SDI */
/* RC5---SDO */
/* RC3---SCK */
/******************************************************/
/*****************************************************************/
/* GetMemType(void)= 0x04--4M --> 0~1023 Sector */
/* 0X08--8M --> 0~2407 Sector */
/* 0X10--16M --> 0~4095 Sector */
/* Read Unit: Sector == 512 byte */
/* Write Unit: Page == 128 byte */
/* Erase Unit: 16 Sector */
/* Note: 1.Writeing the MemCard must to see it's status; */
/* and Read it must not to see it's status. */
/* 2. Level1-4_Command process must use the */
/* last 4 bytes data getting by Txd_command_WithRxd()*/
/* and the ReadArry process is so on */
/* 3. The MemCard's BUSY signal must be checking when */
/* sending the every command */
/*****************************************************************/
// #include "pic.h"
#include "stdio.h"
#include "pic1674.h"
#define Mem_CS RB1
#define Mem_Busy RB2
#define Card_Dec RA5
#define Start_Key RA0
#define SDI RC4
#define SDO RC5
#define SCK RC3
#define Led1_Ok RA1
#define Led2_Fail RA2
#define Led3_Read RA3
#define Led4_Write RA4
bank1 unsigned char temp_txd_data;
bank1 unsigned char verify_error_flag1,verify_error_flag2;
unsigned char level1_flg,level2_flg,level3_flg,level4_flg; // !!!!!!!!
unsigned char rxd_data_buf;
unsigned char rxd_data[9];
unsigned char temp_data[8];
bit Rxd_Bit;
bank1 unsigned char Level1_Command[] = {0x52,0x03,0xff,0x01,0x48,0xff,0xff,0xff,0xff};
bank1 unsigned char Level2_Command[] = {0x52,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff};
bank1 unsigned char Level3_Command[] = {0x52,0x9c,0x0f,0x00,0x00,0xff,0xff,0xff,0xff};
bank1 unsigned char Level4_Command[] = {0x52,0x94,0xa1,0x00,0x00,0xff,0xff,0xff,0xff};
//unsigned char Temp_Arry[] = {0x00,0x00,0x00,0x00};
bank1 unsigned char GetMem_Type[] = {0x00,0x00,0xff,0x80,0x00,0x00};//{0xaa,0x55,0xaa,0x55,0xaa,0x55};
bank1 unsigned char StatusClear[] = {0x89};
bank1 unsigned char ReadStutas[] = {0x83,0X00,0Xff} ;
//bank1 unsigned int Sector_Addres[] =
// {0,2,4,8,16,(32+1),64,128,256,512,(1024-16),(2048-16),(4096-16)};
bank1 unsigned char Sector_Addres[] ={0x00,0x00, 0x00, 0x01, 0x00,0x02, 0x00,0x04, 0x00,0x08, 0x00,0x10, 0x00,0x21, 0x00,0x40,
0x00,0x80, 0x01,0x00, 0x02,0x00, 0x04,0x00, 0x08,0x00, 0x10,0x00 };
const unsigned char ChechData_Lev1[]={0x00,0x01,0x37,0xff, 0x36,0x03,0x5a,0x6e, 0xa4,0x69,0xe8,0x07, 0x31,0xf2,0xeb,0xd9,
0x5c,0x74,0xd9,0xbc, 0x56,0x25,0x83,0x77, 0x82,0xc2,0xce,0x9e, 0x68,0x42,0xd6,0x3f,
0x84,0x17,0xff,0xec, 0xf3,0x30,0x2f,0xca, 0xcc,0xb6,0x10,0x00, 0xc0,0x43,0x5c,0x21,
0x41,0x45,0x62,0xdb, 0x81,0x4d,0x2d,0xa9, 0x34,0xd5,0xec,0xa0, 0x0f,0x17,0x9e,0x39,
0x51,0x26,0x8d,0x77, 0x81,0xc9,0xda,0x82, 0x6d,0x59,0xfc,0x33, 0xb3,0x30,0xb7,0xac,
0xb2,0x37,0xb9,0x9a, 0x8f,0x3c,0xa5,0xdc, 0xa4,0x5b,0x97,0x2e, 0x55,0xa4,0x0e,0x5c,
0xa2,0x47,0x5a,0xf9}; //ok
const unsigned char ChechData_Lev2[]={0x90,0xea,0x47,0xe9, 0x9a,0x1f,0x1c,0xb5, 0x7d,0x15,0x1b,0xe6, 0xed,0xc5,0x06,0x1c,
0xfb,0x94,0x80,0x29, 0x35,0x4a,0xa9,0x7b, 0x0d,0xb5,0x6e,0xdf, 0xe0,0xc4,0xd9,0xbd,
0x9e,0x8c,0xe3,0x75, 0xd8,0xa8,0x3c,0x58, 0x82,0x49,0x14,0x91, 0x37,0xdf,0xaf,0x9a,
0x7a,0x0f,0x4f,0xf1, 0xba,0xe5,0xce,0x8d, 0x2c,0x5b,0x98,0x0a, 0x6e,0xb4,0x1f,0x2e,
0xa7,0x01,0x49,0x4d, 0xb4,0x4d,0xd9,0x44, 0x47,0xbe,0xf0,0x7e, 0x40,0x4c,0x6d,0x62};
const unsigned char ChechData_Lev3[]={0xf1,0x18,0xc7,0xbc, 0x7e,0xcb,0x79,0x4b, 0xfb,0xa0,0x1d,0x2e, 0x9b,0x17,0x67,0x49,
0xf0,0x0f,0x87,0x14, 0x83,0xf0,0x96,0x33, 0xad,0x16,0x5b,0x78, 0x90,0x60,0xc1,0x3f,
0x41,0x81,0x82,0xc3, 0xbc,0x01,0x84,0x41, 0x43,0x7a,0x8f,0xcb, 0x47,0x09,0x65,0x42,
0x5f,0x95,0xbd,0x0c, 0x64,0x01,0xec,0x6e, 0xb4,0xca,0x37,0x6b, 0x00,0x37,0xcd,0xd3,
0xd6,0x58,0x39,0xef, 0xdd,0x44,0xfa,0x43, 0xb8,0x77,0x87,0x30, 0x4f,0xe8,0x66,0x5e,
0xb0,0xa7,0x7a,0x2f, 0x6c,0x88,0xc0,0x85, 0xeb,0x40,0x50,0x0f};
const unsigned char ChechData_Lev4[]={0x5c,0xe6,0xd4,0x5b, 0x2f,0x92,0xb1,0x45,
0xc6,0xe8,0xf6,0xac, 0xc0,0xb4,0xca,0x18, 0x18,0x5c,0x37,0xbc, 0x18,0xd4,0xe0,0xab,
0x51,0x4c,0x88,0x3c, 0xa5,0x77,0x01,0x55, 0x96,0xd3,0xed,0xfc, 0x10,0x59,0x91,0xdf,
0xc8,0xca,0x01,0x43, 0xe6,0xcf,0x97,0xc6, 0xac,0x9d,0x27,0x64, 0x78,0xfe,0x53,0xe2,
0x41,0xf3,0x08,0x81, 0x06,0x96,0xff,0x92, 0x09,0xb6,0x2d,0x49, 0x3e,0xc9,0x1b,0x81,
0xd1,0x26,0xbe,0xb5, 0x70,0xc9,0x8e,0xa2, 0xfb,0xbb,0x00,0xfa, 0x49,0x3a,0x77,0x0f,
0x2f,0xdc,0xed,0xff};
/* Below array must be modified Sector addres or Page address when useing it */
bank1 unsigned char EraseSector[] = {0xf1,0x00,0x00};
bank1 unsigned char ReadArry[] = {0x52,0x00,0x00, 0x00, 0x00,0xff,0xff,0xff,0xff};
bank1 unsigned char Page_Program[] = {0xf2,0x00,0x00, 0x00, 0x00};
/* SA2 SA1 PN */
/**********************/
void CS0_Card(void)
{
Mem_CS=0;
}
/**********************/
void CS1_Card(void)
{
Mem_CS=1;
}
/***********************/
void delay(unsigned int cnt)
{
unsigned int j;
for(j=0;j> 1;
if(((temp_txd_data) & 0x80)==0x80) {SDO=1;};
if(((temp_txd_data) & 0x80)==0x00) {SDO=0;};
temp_txd_data=(temp_txd_data) << 1;
SCK=1;
}
SCK=0;
}
/*************************************************/
void Txd_command_WithRxd ( unsigned char command_array[],unsigned char command_number)
{ unsigned char i;
while(Mem_Busy==1) {}
for(i=0;i<(command_number);i++)
{
ssp_One_Byte(command_array[i]);
rxd_data[i]=rxd_data_buf;
}
}
/*********************************/
/********************************/
unsigned char GetMemType(void)
{
unsigned char mem_type;
CS0_Card();
delay(20);
Txd_command_WithRxd(GetMem_Type,6);
mem_type=rxd_data[5];
CS1_Card();
return (mem_type);
}
/********************/
void ClearStatus(void)
{
CS0_Card();
delay(20);
//Txd_command_WithRxd(StatusClear,1);
ssp_One_Byte(0x89);
delay(5);
CS1_Card();
}
/****************************************/
/* FunctionName: ReadStatus(void) */
/* Function: to read the card's status */
/* until the card is not busy */
/* and this function is come end */
/****************************************/
/*
unsigned char ReadStatus(void)
{
lp1:
CS0_Card();
Txd_command_WithRxd(ReadStutas,3);
CS1_Card();
if(rxd_data[2] & 0x80 !=0X00) { ClearStatus();goto lp1 ;}
if(rxd_data[2] & 0x80 ==0x00) { return (1);}
return(0);
}
*/
unsigned char ReadStatus(void)
{
CS0_Card();
Txd_command_WithRxd(ReadStutas,3);
CS1_Card();
if(rxd_data[2] && 0x80 ==0x00) { return (1);}
else {return (0);}
}
/***********************************
Function: To erase 16 sector data
Return : 1--ok
********************/
unsigned char Erase16Sector(unsigned int data_sa2,unsigned int data_sa1)
{
CS0_Card();
EraseSector[1]= data_sa2; /* SA2 */
EraseSector[2]= data_sa1; /* SA1 */
//eras_lp:
Txd_command_WithRxd(EraseSector,3);
// The ASIC return the stats is always 0xff now !!!!
// if(ReadStatus() !=1){goto eras_lp;}
CS1_Card();
return (1);
}
/*******************/
void Get_TempRxdData_9(void)
{
temp_data[0]=rxd_data[5];
temp_data[1]=rxd_data[6];
temp_data[2]=rxd_data[7];
temp_data[3]=rxd_data[8];
}
/************************/
void Get_TRxdData_4(void)
{
temp_data[0]=rxd_data[0];
temp_data[1]=rxd_data[1];
temp_data[2]=rxd_data[2];
temp_data[3]=rxd_data[3];
}
/**********************/
void main()
{
unsigned char MemCard_Type;
unsigned char i,j,k, led_light_data;
unsigned char verify_times;
unsigned char loop_number;
unsigned char data_togle;
lp_t:
ini_mcu();
while(Card_Dec==1) {}
MemCard_Type = GetMemType();
if(MemCard_Type==0x04){led_light_data=0Xf7;};
if(MemCard_Type==0x08){led_light_data=0Xef;};
if(MemCard_Type==0x10){led_light_data=0Xe7;};
/*++++++++++++++++++++++*/
//if(MemCard_Type==0x04) {Led3_Read=0; Led4_Write=1;Led2_Fail=1;Led1_Ok=1;}
//if(MemCard_Type==0x08) {Led4_Write=1;Led3_Read=1;Led2_Fail=1;Led1_Ok=1;}
//if(MemCard_Type==0x10) {Led4_Write=0;Led3_Read=0;Led2_Fail=1;Led1_Ok=1;}
if(MemCard_Type==0x04) {PORTA=0X16;}
if(MemCard_Type==0x08) {PORTA=0X0e;}
if(MemCard_Type==0x10) {PORTA=0X06;}
lp1:
if(RB7 !=0 ) { goto lp1;}
else {PORTA=0Xff;} //{ Led4_Write=1;Led3_Read=1;Led2_Fail=1;Led1_Ok=1;}
ClearStatus();
ReadStatus();
/************* Level1 ok **************/
CS0_Card();
Txd_command_WithRxd(Level1_Command,9);
Get_TempRxdData_9();
for(i=0;i<25;i++)
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if((temp_data[0])!=(ChechData_Lev1[4*i])) {level1_flg=1;}
if((temp_data[1])!=(ChechData_Lev1[4*i+1])) {level1_flg=1;}
if((temp_data[2])!=(ChechData_Lev1[4*i+2])) {level1_flg=1;}
if((temp_data[3])!=(ChechData_Lev1[4*i+3])) {level1_flg=1;}
}
CS1_Card();
/* for contral light the Led1 */
PORTA=led_light_data;
/************* Level2 ok **************/
CS0_Card();
Txd_command_WithRxd(Level2_Command,9);
Get_TempRxdData_9();
for(i=0;i<5;i++) // These 20 bytes must not be check
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
}
for(i=0;i<20;i++)
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if((temp_data[0])!=(ChechData_Lev2[4*i])) {level2_flg=1;}
if((temp_data[1])!=(ChechData_Lev2[4*i+1])) {level2_flg=1;}
if((temp_data[2])!=(ChechData_Lev2[4*i+2])) {level2_flg=1;}
if((temp_data[3])!=(ChechData_Lev2[4*i+3])) {level2_flg=1;}
}
CS1_Card();
/************* Level3 ok **************/
CS0_Card();
Txd_command_WithRxd(Level3_Command,9);
if( rxd_data[1]!=0x78 && rxd_data[2]!=0xa8 && rxd_data[3]!=0x5e && rxd_data[4]!=0xf9 ) {level3_flg=1;}
if( rxd_data[5]!=0x01 && rxd_data[6]!=0x09 && rxd_data[7]!=0xb3 && rxd_data[8]!=0xb6 ) {level3_flg=1;}
Get_TempRxdData_9();
for(i=0;i<23;i++)
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if((temp_data[0])!=(ChechData_Lev3[4*i])) {level3_flg=1;}
if((temp_data[1])!=(ChechData_Lev3[4*i+1])) {level3_flg=1;}
if((temp_data[2])!=(ChechData_Lev3[4*i+2])) {level3_flg=1;}
if((temp_data[3])!=(ChechData_Lev3[4*i+3])) {level3_flg=1;}
}
CS1_Card();
/* for contral light the Led1 */
PORTA=0xff;
/************* Level4 ok **************/
CS0_Card();
Txd_command_WithRxd(Level4_Command,9);
if( rxd_data[1]!=0x6c && rxd_data[2]!=0x2c && rxd_data[3]!=0xe1 && rxd_data[4]!=0x62) {level4_flg=1;}
if( rxd_data[5]!=0x88 && rxd_data[6]!=0xad && rxd_data[7]!=0x7a && rxd_data[8]!=0x65) {level4_flg=1;}
Get_TempRxdData_9();
for(i=0;i<23;i++)
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if((temp_data[0])!=(ChechData_Lev4[4*i])) {level4_flg=1;}
if((temp_data[1])!=(ChechData_Lev4[4*i+1])) {level4_flg=1;}
if((temp_data[2])!=(ChechData_Lev4[4*i+2])) {level4_flg=1;}
if((temp_data[3])!=(ChechData_Lev4[4*i+3])) {level4_flg=1;}
}
CS1_Card();
/*************************/
ClearStatus();
ReadStatus();
if( level1_flg==1 || level2_flg==1 || level3_flg==1 || level4_flg==1 )
{
verify_error_flag1=1;
}
//goto lp_end;
/*@@@@@@@@@@@@@@ process sector 0 @@@@@@@@@@@@@*/
/****** erase sector 0 ****************/
Erase16Sector(Sector_Addres[0],Sector_Addres[1]);
ReadStatus();
ClearStatus();
/******* write sector 0 ***********/
Page_Program[1]= Sector_Addres[0]; //SA2
Page_Program[2]= Sector_Addres[1]; //SA1
for(k=0;k<4;k++)
{
/* for contral light the Led1 */
if((k==0) || (k==2) ){PORTA=led_light_data;}
if((k==1) || (k==3) ){PORTA=0xff;}
Page_Program[3]=k; // page 0/1/2/3
CS0_Card();
Txd_command_WithRxd(Page_Program,5);
switch(k) {
case 0: {
for(j=0;j<128;j++) {ssp_One_Byte(j);} // write: 0--127
} break;
case 1: {
for(j=0;j<128;j++) {ssp_One_Byte(j+128);} // write: 128--255
} break;
case 2: {
for(j=0;j<128;j++) {ssp_One_Byte(255-j);} // write: 255--128
} break;
case 3: {
for(j=0;j<128;j++) {ssp_One_Byte(127-j);} // write: 127--0
} break;
default:
break;
}
CS1_Card();
ReadStatus();
ClearStatus();
}
/* for contral light the Led1 */
PORTA=0Xff;
/****** checking sector 0 **********/
verify_error_flag1=0;
ReadArry[1]=Sector_Addres[0]; //SA2
ReadArry[2]=Sector_Addres[1]; //SA1
s0_read_lp1:
CS0_Card();
Txd_command_WithRxd(ReadArry,9);
Get_TempRxdData_9();
verify_times=verify_times+1;
for(k=0;k<32;k++) // checking page0 data: 0--127
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if (rxd_data[0]!=4*k) {verify_error_flag1=1;}
if (rxd_data[1]!=(4*k+1)) {verify_error_flag1=1;}
if (rxd_data[2]!=(4*k+2)) {verify_error_flag1=1;}
if (rxd_data[3]!=(4*k+3)) {verify_error_flag1=1;}
}
/* for contral light the Led1 */
PORTA=led_light_data;
for(k=0;k<32;k++) // checking page1 data: 128--255
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if(rxd_data[0]!=(4*k+128)) {verify_error_flag1=1;}
if(rxd_data[1]!=(4*k+1+128)) {verify_error_flag1=1;}
if(rxd_data[2]!=(4*k+2+128)) {verify_error_flag1=1;}
if(rxd_data[3]!=(4*k+3+128)) {verify_error_flag1=1;}
}
/* for contral light the Led1 */
//PORTA=0xff;
for(k=0;k<32;k++) // checking page2 data: 255--128
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if(rxd_data[0]!=(255-4*k)) {verify_error_flag1=1;}
if(rxd_data[1]!=(255-(4*k+1))) {verify_error_flag1=1;}
if(rxd_data[2]!=(255-(4*k+2))) {verify_error_flag1=1;}
if(rxd_data[3]!=(255-(4*k+3))) {verify_error_flag1=1;}
}
/* for contral light the Led1 */
//PORTA=led_light_data;
for(k=0;k<32;k++) // checking page3 data: 127--0
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if(rxd_data[0]!=(127-4*k)) {verify_error_flag1=1;}
if(rxd_data[1]!=(127-(4*k+1))) {verify_error_flag1=1;}
if(rxd_data[2]!=(127-(4*k+2))) {verify_error_flag1=1;}
if(rxd_data[3]!=(127-(4*k+3))) {verify_error_flag1=1;}
}
CS1_Card();
/* for contral light the Led1 */
PORTA=0Xff;
if( (verify_times<2) && (verify_error_flag1==1) )
{
verify_error_flag1=0;
goto s0_read_lp1;
}
if(verify_error_flag1 == 1) //added by louis
{
goto lp_end;
}
//goto lp_end;
/*************************************************/
if(MemCard_Type==0x04) {loop_number=11;}
if(MemCard_Type==0x08) {loop_number=12;}
if(MemCard_Type==0x10) {loop_number=13;}
/*@@@@@@@@@@@@@@@ Erase the sectors for checking @@@@@@@@@@@@@@@@@@@@@@*/
for(i=0;i<(loop_number+1);i++)
{
/* for contral light the Led1 */
// if( (i==0) || (i==2)||(i==4)||(i==6)||(i==8)||(i==10) ){PORTA=led_light_data;}
// if( (i==1) || (i==3)||(i==5)||(i==7)||(i==9)||(i==11) ){PORTA=0xff;}
/*+++++++++ Erase 16 sectors ++++++++++++++++++*/
if(i<=3)
{ Erase16Sector(Sector_Addres[0],Sector_Addres[1]); }
else
{ Erase16Sector(Sector_Addres[2*i],Sector_Addres[2*i+1]); }
ReadStatus();
ClearStatus();
}
/* for contral light the Led1 */
PORTA=led_light_data;
/*@@@@@@@@@@@@@@@@@ to writting "i" to the sector 's page 0 @@@@@@@@@@@@@@@@@@@@@@*/
for(i=0;i<(loop_number);i++)
{
/* for contral light the Led1 */
// if( (i==0) || (i==2)||(i==4)||(i==6)||(i==8)||(i==10) ){PORTA=led_light_data;}
// if( (i==1) || (i==3)||(i==5)||(i==7)||(i==9)||(i==11) ){PORTA=0xff;}
/*+++++++++ Writeing the ONE sector with "i" ++++*/
Page_Program[1]= Sector_Addres[2*i]; //SA2
Page_Program[2]= Sector_Addres[2*i+1]; //SA1
Page_Program[3]=0X00; /*Page 0 */
CS0_Card();
Txd_command_WithRxd(Page_Program,5);
for(j=0;j<128;j++) {ssp_One_Byte(i+1);}
CS1_Card();
ReadStatus();
ClearStatus();
}
/* for contral light the Led1 */
PORTA=0xff;
/*@@@@@@@@@@@@@@@@ Checking the sectors @@@@@@@@@@@@@@@@@@@@@@@*/
for(i=0;i<(loop_number);i++)
{
verify_error_flag1=0;
ReadArry[1]=Sector_Addres[2*i]; //SA2
ReadArry[2]=Sector_Addres[2*i+1]; //SA1
read_lp1:
CS0_Card();
Txd_command_WithRxd(ReadArry,9);
Get_TempRxdData_9();
verify_times=verify_times+1;
for(j=0;j<32;j++) // for checking page 0 of every sector
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
if( (rxd_data[0]!=(i+1))||(rxd_data[1]!=(i+1))||(rxd_data[2]!=(i+1))||(rxd_data[3]!=(i+1)) )
{
verify_error_flag1=1;
goto lp_end;
}
}
/* for contral light the Led1 */
PORTA=led_light_data;
for(k=0;k<96;k++) // only read the page 1--3 of every sector
{
Txd_command_WithRxd(temp_data,4);
Get_TRxdData_4();
}
CS1_Card();
if( (verify_times<2) && (verify_error_flag1==1) )
{
verify_error_flag1 =0;
goto read_lp1;
}
/* for contral light the Led1 */
PORTA=led_light_data;
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/* for contral light the Led1 */
PORTA=0xff;
lp_end:
if( level1_flg==1 || level2_flg==1 || level3_flg==1 || level4_flg==1 ) {verify_error_flag1=1;}
if( verify_error_flag1==1) {PORTA=0Xfb; goto lp_end;}
else{PORTA=0Xfd;goto lp_end;}
}