Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint8 CHIP8_RAND(void)
- {
- static uint32 LFSR1 = 0; static uint32 MIX1 = 0;
- static uint8 LFSR1_TAPS[8] = {31, 28, 22, 21, 18, 13, 9, 4};
- static uint8 LFSR1_TAPCNT = 8; if (LFSR1 == 0) LFSR1 = 0x9D0A31FC;
- /*PRINT CURRENT LFSR1 VALUE*/
- uint8 j = 0; static uint8 CHIP8_RAND_PY = 50;
- char rstr[45] = "LFSR1 **************** MIX1 ****************";
- for (j = 0; j < 16; j++)
- rstr[6+j] = ((LFSR1 >> (15-j)) & 0x1) + '0';
- for (j = 0; j < 16; j++)
- rstr[28+j] = ((MIX1 >> (15-j)) & 0x1) + '0';
- debug_putstr(0, CHIP8_RAND_PY, (char*)&rstr[0]);
- CHIP8_RAND_PY += DEBUG_CHARH + 1;
- if (CHIP8_RAND_PY >= 140) CHIP8_RAND_PY = 50;
- /*LFSR1 SHIFT*/
- uint8 output = 0, fi = 0;
- for (fi = 0; fi < LFSR1_TAPCNT; fi++)
- output ^= LFSR1 >> (32-LFSR1_TAPS[fi]);
- output &= 0x1; //output ^= 1;
- LFSR1 = (LFSR1 >> 1) | (output << 15);
- /*LFSR1 MIX*/
- if (MIX1 == 0) MIX1 = LFSR1; //set after shift
- uint8 *bMIX1 = (uint8*)&MIX1; uint8 *bLFSR1 = (uint8*)&LFSR1;
- for (fi = 0; fi < 3; fi++) {
- bMIX1[0]^=bLFSR1[1]<<11; bMIX1[3]+=bLFSR1[0]; bMIX1[1]+=bMIX1[2];
- bMIX1[1]^=bLFSR1[2]>>2; bMIX1[4]+=bLFSR1[1]; bMIX1[2]+=bMIX1[3];
- bMIX1[2]^=bLFSR1[3]<<8; bMIX1[5]+=bLFSR1[2]; bMIX1[3]+=bMIX1[4];
- bMIX1[3]^=bLFSR1[4]>>16; bMIX1[6]+=bLFSR1[3]; bMIX1[4]+=bMIX1[5];
- bMIX1[4]^=bLFSR1[5]<<10; bMIX1[7]+=bLFSR1[4]; bMIX1[5]+=bMIX1[6];
- bMIX1[5]^=bLFSR1[6]>>4; bMIX1[0]+=bLFSR1[5]; bMIX1[6]+=bMIX1[7];
- bMIX1[6]^=bLFSR1[7]<<8; bMIX1[1]+=bLFSR1[6]; bMIX1[7]+=bMIX1[0];
- bMIX1[7]^=bLFSR1[0]>>9; bMIX1[2]+=bLFSR1[7]; bMIX1[0]+=bMIX1[1];
- MIX1 = MIX1 >> 1;
- }
- /*GET SINGLE BYTE*/
- uint8 rnd = (MIX1) & 0xFF;
- return rnd;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement