Advertisement
TerusTheBird

N64 data loader configuration

Jan 27th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. // This loads 10 bytes per frame, with 10 signalling bits
  2. // we only do bitwise operations at the beginning, and there's not too many
  3. // (I also intentionally wrote them to be (hopefully) easier to convert to assembly)
  4.  
  5. // let the following be an array that magically contains the bytes for 3 controllers
  6. uint8_t pad_bytes[ 12 ];
  7.  
  8. uint16_t command = 0;
  9. uint16_t command_last = 1;
  10.  
  11. read_pad( ) {
  12.     uint16_t temp;
  13.     uint8_t out_bytes[ 10 ];
  14.    
  15.     // for all of the controller bytes that are full bytes, load them directly
  16.     out_bytes[ 0 ] = pad_bytes[  0 ];
  17.     out_bytes[ 1 ] = pad_bytes[  2 ];
  18.     out_bytes[ 2 ] = pad_bytes[  3 ];
  19.     out_bytes[ 3 ] = pad_bytes[  4 ];
  20.     out_bytes[ 4 ] = pad_bytes[  6 ];
  21.     out_bytes[ 5 ] = pad_bytes[  7 ];
  22.     out_bytes[ 6 ] = pad_bytes[  8 ];
  23.     out_bytes[ 7 ] = pad_bytes[ 10 ];
  24.     out_bytes[ 8 ] = pad_bytes[ 11 ];
  25.    
  26.     // pad_bytes[ 1 ], pad_bytes[ 5 ], pad_bytes[ 9 ] only have 6 bits of useful data
  27.     // we'll have to do some bitwise operations to compose them into bytes
  28.    
  29.     // mask off garbage bits, can skip if this isn't needed
  30.     pad_bytes[ 1 ] &= 63;
  31.     pad_bytes[ 5 ] &= 63;
  32.     pad_bytes[ 9 ] &= 63;
  33.    
  34.    
  35.     // --- Read extra byte ---
  36.    
  37.     // step by step bit twiddling
  38.     temp = pad_bytes[ 5 ];  // read pad byte 5 into a register
  39.     temp &= 3;              // mask off everything except the first two bits
  40.     temp <<= 6;             // shift it up 6 bits, so that the first two bits are at the top of the byte
  41.     temp |= pad_bytes[ 1 ]; // merge it with the entirety of pad byte 1
  42.    
  43.     // write it out
  44.     out_bytes[ 9 ] = temp;
  45.    
  46.     // out_bytes now has 10 new bytes loaded into it
  47.    
  48.    
  49.     // --- Read command ---
  50.    
  51.     command_last = command; // so we can detect when a command has changed (ie, started)
  52.    
  53.     // more bit level voodoo
  54.     command = pad_bytes[ 5 ];  // store pad byte 5 in a register
  55.     command &= 60;             // mask off the lowest two bits (which were data)
  56.     command <<= 4;             // shift what's left up 4 bits
  57.     command |= pad_bytes[ 9 ]; // merge all of the bits of pad byte 9 into it
  58.    
  59.     // "command" is now a 10 bit number
  60.    
  61.    
  62.     // --- Execute command ---
  63.    
  64.     switch( command ) {
  65.        
  66.         default: break;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement