haseeb_heaven

GameGenieDecoder.c

Feb 21st, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.18 KB | None | 0 0
  1. //Game Genie Decoder . Decodes 6 or 8 GameGenie code to its equivalent Address and value.
  2. //Written by HaseeB Mir (haseebmir.hm@gmail.com)
  3. //Dated : 22/02/2017
  4. //Refrence by The Mighty Mike Master : http://tuxnes.sourceforge.net/gamegenie.html
  5. //Thanks to Mighty Mike for his amazing refrence.
  6.  
  7. //Including Standard Libraries.
  8. #include<stdio.h>
  9. #include<stdbool.h>
  10. #include<ctype.h>
  11.  
  12. //Defining Length Constants.
  13. #define MAX_GAME_GENIE_LEN 8 //Defining Max Length of GameGenie.
  14. #define GAME_GENIE_TABLE_LEN 16 //MAX GameGenie Table Length.
  15.  
  16. //GameGenieCode subroutines.
  17. void setGameGenieTable();
  18. int checkGameGenieCode(char *);
  19. int decodeGameGenieCode(char *);
  20. char* readGameGenieCode();
  21. int getGameGenieLen();
  22.  
  23. //subroutines for Address,value and Compare.
  24. int decodeAddress();
  25. int decodeValue();
  26. int decodeCompare();
  27.  
  28. //Checking Invalid Input.
  29. int isInvalidInput();
  30.  
  31. //Converting Input to UpperCase if needed.
  32. _Bool isInputLower(char *str);
  33. void toUpperCase(char*);
  34.  
  35. //GameGenie Code Table.
  36. char GameGenieTable[GAME_GENIE_TABLE_LEN] = {
  37.                         'A','P','Z','L','G','I','T','Y',
  38.                         'E','O','X','U','K','S','V','N',
  39.                         }; //Master GameGenie Table.
  40.  
  41. short n0,n1,n2,n3,n4,n5,n6,n7; //Contains HEX of GameGenieCode.  
  42. _Bool is_8_Char_GameGenie = false; //Checking for 8-Char GameGenie Code.
  43.  
  44. char *GameGenieCode; //Contains 6 or 8 Character GameGenieCode.
  45.  
  46. int main(void){
  47.    
  48.  printf("Enter Game Genie Code\n");
  49.  GameGenieCode = readGameGenieCode(); //Reads 6 or 8 Character GameGenieCode..
  50.  
  51.  //If Input is in lower Case convert it to Upper case.
  52.  if(isInputLower(GameGenieCode))
  53.   toUpperCase(GameGenieCode);
  54.  
  55.  //Checking for 8-Character GameGenieCode.
  56.     is_8_Char_GameGenie = (getGameGenieLen() == MAX_GAME_GENIE_LEN) ? true : false;
  57.      
  58.  if(decodeGameGenieCode(GameGenieCode)){
  59.      printf("Address = 0x%x\n",decodeAddress()); //Prints Address in Hex.
  60.      printf("value = %x\n",decodeValue()); //Prints Value in Hex.
  61.  
  62.    if(is_8_Char_GameGenie) //If its 8-Character GameGenie then decode Compare value also.
  63.      printf("Compare = %x\n",decodeCompare());//Prints Compare Value in Hex.   
  64.    
  65.     }
  66.  
  67.  else
  68.  printf("Not a Valid Game Genie Code\n\n");
  69.  
  70.  free(GameGenieCode);//Free the Reserved Memory.
  71. return 0;
  72. }
  73.  
  74. //Reads GameGenieCode from Input limiting to only 8-char Input.
  75. char *readGameGenieCode() {
  76.     char *buf, *tmp_p;
  77.         int i, c;
  78.  
  79.         i = 0;
  80.         buf = (char*)malloc(sizeof(char));
  81.         if (buf == NULL)
  82.                 return NULL;
  83.  
  84.         c = fgetc(stdin);
  85.                            
  86.                             int char_count = 1; //Counter to check if Input is greater than MAX_GAME_GENIE_LEN.
  87.                            
  88.                             //Read input one by one untill Newline or EOF Occurs.
  89.                             //Dont read input after 8 char_count.
  90.                            
  91.         while ( char_count  > 0 && char_count   <= MAX_GAME_GENIE_LEN && c != '\n' && c != EOF) {
  92.                            
  93.                 buf[i++] = c;
  94.                 tmp_p = buf;
  95.                 buf = (char*)realloc(buf, (i + 1) * sizeof(char));
  96.                 if (buf == NULL) {
  97.                         free(tmp_p);
  98.                         return NULL;
  99.                 }
  100.                 c = fgetc(stdin);
  101.                 char_count++; //Count Every Char untill value will become equal to MAX_GAME_GENIE_LEN.
  102.         }
  103.  
  104.         if (i == 0) {
  105.                 free(buf);
  106.                 return NULL;
  107.         }
  108.  
  109.         buf[i] = '\0';
  110.         return buf;
  111. }
  112.  
  113. _Bool isInputLower(char *str){
  114.    
  115.     int index = 0;
  116.    
  117.     for(index = 0; index < strlen(str); index++){
  118.         if(islower(str[index]))
  119.          return true;
  120.     }
  121.        
  122.         return false;
  123. }
  124.  
  125. //Converts the Input to Capital case.
  126. void toUpperCase(char *str)
  127. {
  128.  
  129.     int index = 0;
  130.     char smallCase; //Hold small cases to compare
  131.  
  132.     while(str[index])
  133.     {
  134.         //Check for All small characters.
  135.         for(smallCase = 'a'; smallCase <= 'z'; smallCase++)
  136.  
  137.             if(str[index] == smallCase)
  138.                 str[index] -= 32; // 32 is difference between Capital and Small character's ASCII.
  139.  
  140.         index++;
  141.     }
  142.  
  143. }
  144.  
  145. //Get Length of GameGenie Code.
  146. int getGameGenieLen(){
  147.     return strlen(GameGenieCode);
  148. }
  149.  
  150. //Checking Invalid Input.
  151. int isInvalidInput(){
  152.    
  153.     if(getGameGenieLen() < 6) //Dont check for > 8 because we has already set Input taking limit to 8 in readGameGenieCode() function.
  154.       return 1; //Return True if its invalid Input.
  155.      
  156.  return 0;   //Otherwise return false.
  157. }
  158.  
  159. //Decodes Value from GameGenie Code.
  160. int decodeAddress(){
  161.    
  162.  int address = 0x8000 +
  163.               ((n3 & 7) << 12)
  164.             | ((n5 & 7) << 8) | ((n4 & 8) << 8)
  165.             | ((n2 & 7) << 4) | ((n1 & 8) << 4)
  166.             |  (n4 & 7)       |  (n3 & 8);
  167.            
  168.  return address;
  169.  
  170. }
  171.  
  172. //Decodes Value from 6 or 8-Char GameGenie Code.
  173. int decodeValue(){
  174.    
  175.  int value;
  176.    
  177.     //Checking if it's 8-Char_GameGenie Code.
  178.     (is_8_Char_GameGenie == true)
  179.    
  180.  //Value of 8-Char GameGenie.
  181.     ? (value =
  182.              ((n1 & 7) << 4) | ((n0 & 8) << 4)
  183.             | (n0 & 7)       |  (n7 & 8))
  184.    
  185.         //Value of 6-Char GameGenie.
  186.     : (value =
  187.              ((n1 & 7) << 4) | ((n0 & 8) << 4)
  188.             | (n0 & 7)       |  (n5 & 8) );
  189.  
  190.  
  191.  return value;
  192. }
  193.  
  194.  
  195. //Decodes Compare Value for 8-Char GameGenie Code.
  196. int decodeCompare(){
  197.    
  198.           int  compare =
  199.              ((n7 & 7) << 4) | ((n6 & 8) << 4)
  200.             | (n6 & 7)       |  (n5 & 8);
  201.       return compare;      
  202.    
  203. }
  204.  
  205. //Decodes GameGenie chars to its equivalent Hex.
  206. int decodeGameGenieCode(char *GameGenieCode){
  207.  
  208.  //Check for Invalid Input.
  209.    if(isInvalidInput())
  210.                 return 0; //Return False for Invalid Input.
  211.                
  212. int i = 0,j = 0,found = 0;
  213. int GameGenieLen = getGameGenieLen();
  214.  
  215.  for(i = 0; i <     GameGenieLen    ; i++){
  216.      for(j = 0; j < GAME_GENIE_TABLE_LEN; j++)
  217.      if(GameGenieCode[i] == GameGenieTable[j]){
  218.      found ++;
  219.      
  220.      //Twirling Ternary operators to convert Game Genie to its equivalent Hex.
  221.         (found == 1) ? n0 = j : (found == 2) ? n1 = j :
  222.         (found == 3) ? n2 = j : (found == 4) ? n3 = j :
  223.         (found == 5) ? n4 = j : (found == 6) ? n5 = j : j;
  224.        
  225.        
  226.         (is_8_Char_GameGenie) ? (found == 7 ? n6 = j : found == 8 ? n7 = j : j) : j;     
  227.      }
  228.  }
  229.  
  230.  //Checking for Invalid Genie Codes.
  231.  return (found == GameGenieLen) ? 1 : 0;
  232. }
Add Comment
Please, Sign In to add comment