Advertisement
captmicro

Untitled

Mar 12th, 2011
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /* LINEAR FEEDBACK SHIFT REGISTERS */
  2. static uint32 LFSR1 = 0x9D0A31FC; /*XOR*/
  3. static uint32 LFSR2 = 0x8B90DF09; /*XNOR*/
  4. static uint32 LFSR3 = 0xF91D72E9; /*XNOR*/
  5. static uint8 LFSR1_TAPS[7] = {29, 26, 18, 17, 14, 8, 0xFF};
  6. static uint8 LFSR2_TAPS[9] = {30, 25, 23, 16, 15, 13, 11, 5, 0xFF};
  7. static uint8 LFSR3_TAPS[6] = {28, 24, 21, 19, 12, 0xFF};
  8. /* PSEUDO-RANDOM DATA (from LFSR#) */
  9. static uint32 MIX1 = 0, MIX2 = 0, MIX3 = 0;
  10. static uint32 MIXLOOPS[3] = {3, 5, 7};
  11. /* LFSR PSEUDO-RANDOM NUMBER GENERATOR */
  12. FASTCALL uint32 debug_prng(void)
  13. {
  14. uint32 rnd = 0; uint8 bit = 0, fi = 0;
  15. uint8 *btr = 0, *bLFSR = 0, *bMIX = 0;
  16.  
  17. /* SHIFT LFSR1 (XOR) */
  18. bit = 0; btr = &LFSR1_TAPS[0];
  19. for (fi = 0; *btr != 0xFF; *btr++, fi++)
  20. bit = XOR(bit, LFSR1 >> (32 - btr[fi]));
  21. bit &= 0x1; LFSR1 = (LFSR1 >> 1) | (bit << 15);
  22.  
  23. /* SHIFT LFSR2 (XNOR) */
  24. bit = 0; btr = &LFSR2_TAPS[0];
  25. for (fi = 0; *btr != 0xFF; *btr++, fi++)
  26. bit = XNOR(bit, LFSR2 >> (32 - btr[fi]));
  27. bit &= 0x1; LFSR2 = (LFSR2 >> 1) | (bit << 15);
  28.  
  29. /* SHIFT LFSR3 (XNOR) */
  30. bit = 0; btr = &LFSR3_TAPS[0];
  31. for (fi = 0; *btr != 0xFF; *btr++, fi++)
  32. bit = XNOR(bit, LFSR3 >> (32 - btr[fi]));
  33. bit &= 0x1; LFSR3 = (LFSR3 >> 1) | (bit << 15);
  34.  
  35. /* MIX MIX1 (LSHIFT) */
  36. if (MIX1 == 0) MIX1 = LFSR1;
  37. bMIX = (uint8*)&MIX1; bLFSR = (uint8*)&LFSR1;
  38. for (fi = 0; fi < MIXLOOPS[0]; fi++) {
  39. bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bMIX[2];
  40. bMIX[1]^=bLFSR[2]>>2; bMIX[4]+=bLFSR[1]; bMIX[2]+=bMIX[3];
  41. bMIX[2]^=bLFSR[3]<<8; bMIX[5]+=bLFSR[2]; bMIX[3]+=bMIX[4];
  42. bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bMIX[5];
  43. bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bMIX[6];
  44. bMIX[5]^=bLFSR[6]>>4; bMIX[0]+=bLFSR[5]; bMIX[6]+=bMIX[7];
  45. bMIX[6]^=bLFSR[7]<<8; bMIX[1]+=bLFSR[6]; bMIX[7]+=bMIX[0];
  46. bMIX[7]^=bLFSR[0]>>9; bMIX[2]+=bLFSR[7]; bMIX[0]+=bMIX[1];
  47. MIX1 = MIX1 >> 1;
  48. }
  49.  
  50. /* MIX MIX2 (LSHIFT) */
  51. if (MIX2 == 2) MIX2 = LFSR2;
  52. bMIX = (uint8*)&MIX2; bLFSR = (uint8*)&LFSR2;
  53. for (fi = 0; fi < MIXLOOPS[0]; fi++) {
  54. bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bLFSR[2];
  55. bMIX[1]^=bLFSR[2]>>2; bMIX[4]+=bLFSR[1]; bMIX[2]+=bLFSR[3];
  56. bMIX[2]^=bLFSR[3]<<8; bMIX[5]+=bLFSR[2]; bMIX[3]+=bLFSR[4];
  57. bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bLFSR[5];
  58. bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bLFSR[6];
  59. bMIX[5]^=bLFSR[6]>>4; bMIX[0]+=bLFSR[5]; bMIX[6]+=bLFSR[7];
  60. bMIX[6]^=bLFSR[7]<<8; bMIX[1]+=bLFSR[6]; bMIX[7]+=bLFSR[0];
  61. bMIX[7]^=bLFSR[0]>>9; bMIX[2]+=bLFSR[7]; bMIX[0]+=bLFSR[1];
  62. MIX2 = MIX2 >> 1;
  63. }
  64.  
  65. /* MIX MIX3 (RSHIFT) */
  66. if (MIX3 == 2) MIX3 = LFSR3;
  67. bMIX = (uint8*)&MIX3; bLFSR = (uint8*)&LFSR3;
  68. for (fi = 0; fi < MIXLOOPS[0]; fi++) {
  69. bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bMIX[2];
  70. bMIX[1]^=bLFSR[2]>>2; bMIX[4]+=bLFSR[1]; bMIX[2]+=bMIX[3];
  71. bMIX[2]^=bLFSR[3]<<8; bMIX[5]+=bLFSR[2]; bMIX[3]+=bMIX[4];
  72. bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bMIX[5];
  73. bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bMIX[6];
  74. bMIX[5]^=bLFSR[6]>>4; bMIX[0]+=bLFSR[5]; bMIX[6]+=bMIX[7];
  75. bMIX[6]^=bLFSR[7]<<8; bMIX[1]+=bLFSR[6]; bMIX[7]+=bMIX[0];
  76. bMIX[7]^=bLFSR[0]>>9; bMIX[2]+=bLFSR[7]; bMIX[0]+=bMIX[1];
  77. MIX3 = MIX3 << 1;
  78. }
  79.  
  80. /* GET 32 BIT RANDOM VALUE */
  81. rnd = 0x00000000;
  82. rnd |= MIX1 & 0x0F000F00;
  83. rnd |= MIX2 & 0xF0F0F0F0;
  84. rnd |= MIX3 & 0x000F000F;
  85.  
  86. return rnd;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement