Advertisement
Guest User

zfunctions

a guest
Dec 23rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. byte[] ROM_DATA;
  2. List<byte> dataBuffer = new List<byte>();
  3. public void Decompress(int apos)
  4. {
  5.     int pos = 0x08B800;
  6.     bool done = false;
  7.    
  8.     while (done == false)
  9.     {
  10.         bool expand = false;
  11.         byte b = ROM_DATA[pos];
  12.        
  13.         Console.WriteLine(b + " : " +((b >> 5) & 0x07));
  14.         if (b == 0xFF)
  15.         {
  16.             done = true;
  17.         }
  18.  
  19.         if (((b >> 5) & 0x07) == 7) //expanded command
  20.         {
  21.             expand = true;
  22.         }
  23.         byte cmd = 0;
  24.         short length = 0;
  25.         if (expand)
  26.         {
  27.             cmd = (byte)((b >> 2) & 0x07);
  28.             length = BitConverter.ToInt16(new byte[] { (ROM_DATA[pos + 1]), (byte)(b & 0x03) }, 0);
  29.             pos += 2;
  30.         }
  31.         else
  32.         {
  33.             cmd = (byte)((b >> 5) & 0x07);
  34.             length = (byte)(b & 0x1F);
  35.             pos += 1;
  36.         }
  37.         length += 1;
  38.         if (cmd == 0)//000 Direct Copy Followed by (L+1) bytes of data
  39.         {
  40.             for(int i = 0;i<length;i++)
  41.             {
  42.                 dataBuffer.Add(ROM_DATA[pos]);
  43.                 pos++;
  44.             }
  45.         }
  46.        
  47.         if (cmd == 1)//001 "Byte Fill" Followed by one byte to be repeated (L + 1) times
  48.         {
  49.             byte copiedByte = ROM_DATA[pos];
  50.             pos++;
  51.             for (int i = 0; i < length; i++)
  52.             {
  53.                 dataBuffer.Add(copiedByte);
  54.             }
  55.             //pos++;
  56.         }
  57.  
  58.         if (cmd == 2)//010    "Word Fill" Followed by two bytes. Output first byte, then second, then first,then second, etc. until (L+1) bytes has been outputted
  59.         {
  60.             byte copiedByte = ROM_DATA[pos];
  61.             byte copiedByte2 = ROM_DATA[pos+1];
  62.             pos += 2;
  63.             int j = 0;
  64.             for (int i = 0; i < length; i++)
  65.             {
  66.                 if (j == 0)
  67.                 {
  68.                     dataBuffer.Add(copiedByte);
  69.                     j = 1;
  70.                 }
  71.                 else
  72.                 {
  73.                     dataBuffer.Add(copiedByte2);
  74.                     j = 0;
  75.                 }
  76.             }
  77.         }
  78.        
  79.         if (cmd == 3)//"Increasing Fill" Followed by one byte to be repeated (L + 1) times, but the byte is increased by 1 after each write
  80.         {
  81.             byte copiedByte = ROM_DATA[pos];
  82.             pos += 1;
  83.             for (int i = 0; i < length ; i++)
  84.             {
  85.                 dataBuffer.Add((byte)(copiedByte +i));
  86.             }
  87.         }
  88.  
  89.         if (cmd == 4)//"Repeat" Followed by two bytes (ABCD byte order) containing address (in the output buffer) to copy (L + 1) bytes from
  90.         {
  91.             byte copiedByte = ROM_DATA[pos];
  92.             byte copiedByte2 = ROM_DATA[pos+1];
  93.             int pos2 = BitConverter.ToInt16(new byte[] { copiedByte, copiedByte2 }, 0);
  94.             pos += 2;
  95.             for (int i = 0; i < length; i++)
  96.             {
  97.                 dataBuffer.Add(dataBuffer[pos2]);
  98.                 pos2++;
  99.             }
  100.         }
  101.     }
  102.     FileStream fs = new FileStream("gfx.bin", FileMode.OpenOrCreate, FileAccess.Write);
  103.     fs.Write(dataBuffer.ToArray(), 0, dataBuffer.Count);
  104.     fs.Close();
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. //Convert Snes Address in PC Address - Mirrored Format
  112. public int snestopc(int addr)
  113. {
  114.     int temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000);
  115.     return (temp);
  116. }
  117.  
  118.  
  119.  
  120. //Convert PC Address to Snes Address
  121. public int pctosnes(int addr)
  122. {
  123.     byte[] b = BitConverter.GetBytes(addr);
  124.     b[2] = (byte)(b[2] * 2);
  125.     if (b[1] >= 0x80)
  126.         b[2] += 1;
  127.     else
  128.         b[1] += 0x80;
  129.  
  130.     return BitConverter.ToInt32(b, 0);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement