Advertisement
captmicro

Untitled

Mar 11th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. uint8 CHIP8_RAND(void)
  2. {
  3. static uint32 LFSR1 = 0; static uint32 MIX1 = 0;
  4. static uint8 LFSR1_TAPS[8] = {31, 28, 22, 21, 18, 13, 9, 4};
  5. static uint8 LFSR1_TAPCNT = 8; if (LFSR1 == 0) LFSR1 = 0x9D0A31FC;
  6.  
  7. /*PRINT CURRENT LFSR1 VALUE*/
  8. uint8 j = 0; static uint8 CHIP8_RAND_PY = 50;
  9. char rstr[45] = "LFSR1 **************** MIX1 ****************";
  10. for (j = 0; j < 16; j++)
  11. rstr[6+j] = ((LFSR1 >> (15-j)) & 0x1) + '0';
  12. for (j = 0; j < 16; j++)
  13. rstr[28+j] = ((MIX1 >> (15-j)) & 0x1) + '0';
  14. debug_putstr(0, CHIP8_RAND_PY, (char*)&rstr[0]);
  15. CHIP8_RAND_PY += DEBUG_CHARH + 1;
  16. if (CHIP8_RAND_PY >= 140) CHIP8_RAND_PY = 50;
  17.  
  18. /*LFSR1 SHIFT*/
  19. uint8 output = 0, fi = 0;
  20. for (fi = 0; fi < LFSR1_TAPCNT; fi++)
  21. output ^= LFSR1 >> (32-LFSR1_TAPS[fi]);
  22. output &= 0x1; //output ^= 1;
  23. LFSR1 = (LFSR1 >> 1) | (output << 15);
  24.  
  25. /*LFSR1 MIX*/
  26. if (MIX1 == 0) MIX1 = LFSR1; //set after shift
  27. uint8 *bMIX1 = (uint8*)&MIX1; uint8 *bLFSR1 = (uint8*)&LFSR1;
  28. for (fi = 0; fi < 3; fi++) {
  29. bMIX1[0]^=bLFSR1[1]<<11; bMIX1[3]+=bLFSR1[0]; bMIX1[1]+=bMIX1[2];
  30. bMIX1[1]^=bLFSR1[2]>>2; bMIX1[4]+=bLFSR1[1]; bMIX1[2]+=bMIX1[3];
  31. bMIX1[2]^=bLFSR1[3]<<8; bMIX1[5]+=bLFSR1[2]; bMIX1[3]+=bMIX1[4];
  32. bMIX1[3]^=bLFSR1[4]>>16; bMIX1[6]+=bLFSR1[3]; bMIX1[4]+=bMIX1[5];
  33. bMIX1[4]^=bLFSR1[5]<<10; bMIX1[7]+=bLFSR1[4]; bMIX1[5]+=bMIX1[6];
  34. bMIX1[5]^=bLFSR1[6]>>4; bMIX1[0]+=bLFSR1[5]; bMIX1[6]+=bMIX1[7];
  35. bMIX1[6]^=bLFSR1[7]<<8; bMIX1[1]+=bLFSR1[6]; bMIX1[7]+=bMIX1[0];
  36. bMIX1[7]^=bLFSR1[0]>>9; bMIX1[2]+=bLFSR1[7]; bMIX1[0]+=bMIX1[1];
  37. MIX1 = MIX1 >> 1;
  38. }
  39.  
  40. /*GET SINGLE BYTE*/
  41. uint8 rnd = (MIX1) & 0xFF;
  42.  
  43. return rnd;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement