#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

  char code[8];
  int num[8];
  int value;
  int address;
  int compare;
  int length=6;

int xlate(char in)                      //Translates code letters into numbers.
{                                       //Yes, I know this sucks!!!
   if ((in=='A')||(in=='a')) return 0;
   if ((in=='P')||(in=='p')) return 1;
   if ((in=='Z')||(in=='z')) return 2;
   if ((in=='L')||(in=='l')) return 3;
   if ((in=='G')||(in=='g')) return 4;
   if ((in=='I')||(in=='i')) return 5;
   if ((in=='T')||(in=='t')) return 6;
   if ((in=='Y')||(in=='y')) return 7;
   if ((in=='E')||(in=='e')) return 8;
   if ((in=='O')||(in=='o')) return 9;
   if ((in=='X')||(in=='x')) return 10;
   if ((in=='U')||(in=='u')) return 11;
   if ((in=='K')||(in=='k')) return 12;
   if ((in=='S')||(in=='s')) return 13;
   if ((in=='V')||(in=='v')) return 14;
   if ((in=='N')||(in=='n')) return 15;
   cout << " Invalid code!!\n";
   exit(20);
}

char xlate2(int in)                     //Translates numbers back into code letters.
{                                       //And I know this sucks worse!!!
    if (in==0) return 'A';
    if (in==1) return 'P';
    if (in==2) return 'Z';
    if (in==3) return 'L';
    if (in==4) return 'G';
    if (in==5) return 'I';
    if (in==6) return 'T';
    if (in==7) return 'Y';
    if (in==8) return 'E';
    if (in==9) return 'O';
    if (in==10) return 'X';
    if (in==11) return 'U';
    if (in==12) return 'K';
    if (in==13) return 'S';
    if (in==14) return 'V';
    if (in==15) return 'N';
    cout << " Invalid code!!\n";
    exit(20);
}

void main()
{
   
   cout << "Game Genie code convertor v1.1\nŠ1998 Chris Covell\n(ccovell@direct.ca)\n\n(D)ecode or (E)ncode a Game Genie code?: ";
   cin >> code[0];
   if ((code[0]=='D')||(code[0]=='d'))
  { 
   cout << "Enter Game Genie code: ";

   for (int i=0; i<6; i++)
   {
      cin >> code[i];                   //Read first six letters of code...
      num[i]=xlate(code[i]);            //And see if they're valid.
   }
   
   if (num[2]>>3)                       //Is it a 6- or 8-letter code?
   {
      cin >> code[6] >> code[7];        //If so, read some more...
      num[6]=xlate(code[6]);
      num[7]=xlate(code[7]);
      length=8;
   }

   
//Decoding of Game Genie code section starts here   
//-----------------------------------------------


   value = ((num[0]&8)<<4)+((num[1]&7)<<4)+(num[0]&7);          //MM Hmmm...
   
   address = ((num[3]&7)<<12)+((num[4]&8)<<8)+((num[5]&7)<<8)+((num[1]&8)<<4)+((num[2]&7)<<4)+(num[3]&8)+(num[4]&7);

   if (length==8)
   {
    value+=(num[7]&8);                                                  //Oh yeah...
    compare = ((num[6]&8)<<4)+((num[7]&7)<<4)+(num[5]&8)+(num[6]&7);    //You don't say...
    cout << "The compared value is: " << setbase(16) << compare << endl;
   }
   else value+=(num[5]&8);             

   cout << "The code value is:     " << setbase(16) << value << "\nThe ROM address is:    " << address << "\nThe CPU address is:    " << address+32768 << endl;
  }


//Any of that make sense?  Here's the Encoding part:
//--------------------------------------------------

  
  else if ((code[0]=='E')||(code[0]=='e'))
 {
  cout << "Do you want a compare value? (Y/N): ";
  cin >> code[0];
  if ((code[0]=='Y')||(code[0]=='y'))
  {
    length=8;
    cout << "Enter compare value (HEX): ";
    cin >> setbase(16) >> compare;
  }  
  cout << "Enter code value (HEX):    ";
  cin >> setbase(16) >> value;
  cout << "Enter ROM address (HEX):   ";
  cin >> setbase(16) >> address;
  
  num[0]=(value&7)+((value>>4)&8);              //I'm proudest of this little section.
  num[1]=((value>>4)&7)+((address>>4)&8);
  num[2]=((address>>4)&7);
  num[3]=(address>>12)+(address&8);
  num[4]=(address&7)+((address>>8)&8);
  num[5]=((address>>8)&7);
  
  if (length==6)                                //If it's six,
    num[5]+=value&8;                            //Num[5]+=value&8, get it?
  else
  {
    num[2]+=8;
    num[5]+=compare&8;
    num[6]=(compare&7)+((compare>>4)&8);
    num[7]=((compare>>4)&7)+(value&8);
  }  
  
  for(int i=0;i<length;i++)
  code[i]=xlate2(num[i]);
  
  cout << "Your code is:              ";
  
  for(i=0;i<length;i++)
  cout << code[i];
  cout << endl;         //This is the most important line in the entire program.
 }
 
  else cout << "Invalid choice.  Sorry.\n"; 
}                                               //And this is the runner-up.
