haseeb_heaven

GameGenieEncoder.c

Feb 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. //GameGenieEncoder. It Encodes Adress and value to its Equivalent GameGenie Code.
  2. //Written by Haseeb Mir (haseebmir.hm@gmail.com)
  3. //Dated : 26/02/2017
  4.  
  5. //Thanks to Emu Works for their GameGenie Encoder/Decoder Software under GNU GENERAL PUBLIC LICENSE.
  6. //Software could be found here : http://games.technoplaza.net/ggencoder/qt/
  7.  
  8. //Including Standard Libraries.
  9. #include<stdio.h>
  10. #include<stdbool.h>
  11. #include<string.h>
  12.  
  13. #define byte unsigned char //We need only one byte for Value and Compare
  14. #define word unsigned short //We need only two bytes for address.
  15.  
  16. //GameGenieTable list of GameGenie Characters.
  17. static const char GameGenieTable[] = {
  18.     'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y',
  19.     'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N'
  20. };
  21.  
  22. //Variable for Storing Inputs.
  23. word address = 0x0; //2 bytes for range (8000-FFFF)  
  24. byte value = 0x0; //1 byte for range (00-FF)
  25. byte compare = 0x0; //1 byte for range (00-FF)
  26. _Bool _compareBool = false; //Bool To check if input has Compare present or not.
  27.  
  28. //Prototype for Encoding Input to GameGenieCode.
  29. word getAddress();
  30. byte getValue();
  31. char* encode_input(word,byte,byte);
  32.  
  33. //Prototype For Compare values.
  34. _Bool hasCompare();
  35. byte getCompare();
  36.  
  37. //For Clearing Input buffer when there is Buffer OverFlow.
  38. void clearInputBuffer();
  39.  
  40. int main(){
  41.  
  42.  //Reading Input.
  43.     printf("Enter Adress : (Range : 8000-FFFF).\n");
  44.     scanf("%x",&address);
  45.  
  46.  //Clears Input buffer for Next Input. Only True In case of Buffer OverFlow.
  47.   clearInputBuffer();
  48.  
  49.     printf("\nEnter value : (Range : 00-FF).\n");
  50.     scanf("%x",&value);
  51.  
  52.  //Reading Optional Compare input for 8-char GameGenie Code.
  53.     printf("\nEnter Compare value : (Range : 00-FF)  (Optional) if you want to Skip Write \\n\n");
  54.  
  55.  //Clears Input buffer for Next Input. Only True In case of Buffer OverFlow.
  56.   clearInputBuffer();
  57.  
  58.     //If we have read atleast One Input be it Zero even.
  59.     if(scanf("%x",&compare) > 0)
  60.       _compareBool = true; //set Bool to true.
  61.      
  62.  if(compare == '\n') //If user Skipped Compare.
  63.     _compareBool = false; //Then Set _compareBool to False.
  64.    
  65.  //Prints Equivalent GameGenie Code.
  66.     printf("\nGame Genie Code is : %s\n",strrev(encode_input(getAddress(),getValue(),getCompare())));
  67.  
  68. return 0;
  69. }
  70.  
  71. //Clears Input buffer for Next Input.
  72. void clearInputBuffer(){
  73.      //Same as what fflush(stdin) does, but atleast it doesnt have undefined behaiour. :p
  74.  char ch;
  75.    
  76.     while((ch = getchar()) != '\n' && ch != EOF);//Dont override the input buffer with previos overflow value .
  77.  //Simply Wait till EOF or '\n' occurs.
  78.  
  79. }
  80.  
  81. //Checking if input has Compare value.
  82. _Bool hasCompare(){
  83.     return (_compareBool != false);
  84.     }  
  85.  
  86. //Get Compare Value within 1 Byte Range. (0xFF).
  87. byte getCompare(){
  88.  compare =  (compare & 0xFF); //Masking Invalid Compare value Range to its Range.
  89.  return compare;
  90.     }
  91.  
  92. //Get Adress within ROM Adress Range (0x8000 - 0xFFFF).
  93. word getAddress(){
  94.  address = (address & 0xFFFF); //Masking Invalid Address to its Range.
  95.  return address;
  96.     }
  97.  
  98. //Get Value within 1 Byte Range. (0xFF).
  99. byte getValue(){
  100.  value =  (value & 0xFF); //Masking Invalid value to its Range.
  101.  return value;
  102. }
  103.  
  104.  
  105. //Encoding Input to GameGenieCode.        
  106. char* encode_input(word  inputAddress,byte inputValue,byte inputCompare) {
  107.     int genie;
  108.     int temp = 0;
  109.    
  110.     byte value = inputValue;
  111.     word address = inputAddress;
  112.    
  113.     // position 1678
  114.     genie = ((value & 0x80) >> 4) | (value & 0x7);
  115.    
  116.     // position H234
  117.     temp = ((address & 0x80) >> 4) | ((value & 0x70) >> 4);
  118.     genie <<= 4;
  119.     genie |= temp;
  120.    
  121.     // position -IJK
  122.     temp = (address & 0x70) >> 4;
  123.    
  124.     if (hasCompare()) {
  125.         temp |= 0x8;
  126.     }
  127.    
  128.     genie <<= 4;
  129.     genie |= temp;
  130.    
  131.     // position LABC
  132.     temp = (address & 0x8) | ((address & 0x7000) >> 12);
  133.     genie <<= 4;
  134.     genie |= temp;
  135.    
  136.     // position DMNO
  137.     temp = ((address & 0x800) >> 8) | (address & 0x7);
  138.     genie <<= 4;
  139.     genie |= temp;
  140.    
  141.     if (hasCompare()) {
  142.         byte compare = inputCompare;
  143.        
  144.         // position eEFG
  145.         temp = (compare & 0x8) | ((address & 0x700) >> 8);
  146.         genie <<= 4;
  147.         genie |= temp;
  148.        
  149.         // position afgh
  150.         temp = ((compare & 0x80) >> 4) | (compare & 0x7);
  151.         genie <<= 4;
  152.         genie |= temp;
  153.        
  154.         // position 5bcd
  155.         temp = (value & 0x8) | ((compare & 0x70) >> 4);
  156.         genie <<= 4;
  157.         genie |= temp;
  158.     } else {
  159.         // position 5EFG
  160.         temp = (value & 0x8) | ((address & 0x700) >> 8);
  161.         genie <<= 4;
  162.         genie |= temp;
  163.     }
  164.    
  165.    
  166.                     //Contains Encoded GameGenie Code.
  167.                     static char GameGenieCode[8] = {0x0}; //We are returning this so it has to be static.
  168.              
  169.              //Generates Equivalent GameGenieCode from Input.
  170.                 int index;
  171.                 for (index = 0; index <  (hasCompare() ? 8 : 6); index++)
  172.         GameGenieCode[index] = GameGenieTable[(genie >> (index * 4)) & 0xF];
  173.    
  174.                 //Return Encoded GameGenie Code.      
  175.     return GameGenieCode;
  176. }
Add Comment
Please, Sign In to add comment