Advertisement
TerusTheBird

hyperspeed polling

Feb 18th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. #define POLLS 4
  2. #define _data_len ( 45 * ( (POLLS) / 4 ) )
  3.  
  4. typedef struct {
  5.     u32 init;
  6.     volatile u8* data_pointer;
  7.     u32 crc_state;
  8.     u32 crc_table[ 256 ];
  9.     u32 poll_count;
  10.     u8  temp_data[ 24 ];
  11.     union {
  12.         u8  bytes[ _data_len ];
  13.         u16 _u16[ _data_len / sizeof(u16) ];
  14.         u32 _u32[ _data_len / sizeof(u32) ];
  15.         s16 _s16[ _data_len / sizeof(s16) ];
  16.         int _int[ _data_len / sizeof(int) ];
  17.     } out;
  18. } static_globals;
  19. extern static_globals gvars;
  20.  
  21. typedef struct {
  22.     u8 b1, b2, x, y;
  23.     u16 status;
  24. } __attribute__((packed)) raw_input_t;
  25. // 6 bytes long
  26. // might be bad, probably not needed
  27. // hopefully this works if you pay attention to alignment
  28.  
  29. int pad_poll( void ) {
  30.     u32 bits;
  31.     u8 bits_n, i, written_bytes, padding;
  32.     bits_n = 0;
  33.     bits = 0ULL;
  34.     written_bytes = 0;
  35.     for( i = 0; i < (POLLS); ++i ) {
  36.         raw_input_t* pad_data;
  37.         // try to preserve the original pad input
  38.         // *never write* to padmgr
  39.        
  40.         if( !i ) { // the first time, use pad data from the original game poll
  41.             pad_data = (raw_input_t*)(&padmgr->pads[0]);
  42.         } else { // afterwards, poll the pads ourselves, and discard data for pad 1
  43.             pad_data = (raw_input_t*)(gvars.temp_data);
  44.             osContStartReadData( queue ); // black magic
  45.             osRecvMesg( queue, NULL, OS_MESG_BLOCK );
  46.             osContGetReadData( pad_data ); // actually get the pad data
  47.         }
  48.         // pad_data now has the state of the pads for this poll
  49.        
  50.         if( pad_data[1].status == 0 || pad_data[2].status == 0 || pad_data[3].status == 0 )
  51.             return 0;
  52.        
  53.         gvars.out.bytes[ written_bytes++ ] = pad_data[1].b1;
  54.         gvars.out.bytes[ written_bytes++ ] = pad_data[1].x;
  55.         gvars.out.bytes[ written_bytes++ ] = pad_data[1].y;
  56.         gvars.out.bytes[ written_bytes++ ] = pad_data[2].b1;
  57.         gvars.out.bytes[ written_bytes++ ] = pad_data[2].x;
  58.         gvars.out.bytes[ written_bytes++ ] = pad_data[2].y;
  59.         gvars.out.bytes[ written_bytes++ ] = pad_data[3].b1;
  60.         gvars.out.bytes[ written_bytes++ ] = pad_data[3].x;
  61.         gvars.out.bytes[ written_bytes++ ] = pad_data[3].y;
  62.         bits <<= 6;
  63.         bits  |= pad_data[1].b2;
  64.         bits <<= 6;
  65.         bits  |= pad_data[2].b2;
  66.         bits <<= 6;
  67.         bits  |= pad_data[3].b2;
  68.         bits_n += 18;
  69.         gvars.out.bytes[ written_bytes++ ] = ( bits & 0xFF );
  70.         bits >>= 8;
  71.         gvars.out.bytes[ written_bytes++ ] = ( bits & 0xFF );
  72.         bits >>= 8;
  73.         bits_n -= 16;
  74.         if( bits_n == 8 ) {
  75.             gvars.out.bytes[ written_bytes++ ] = ( bits & 0xFF );
  76.             bits >>= 8;
  77.             bits_n -= 8;
  78.         }
  79.     }
  80.     return 1;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement