Advertisement
Chdata

ASM --> C++

Apr 4th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. /*
  2. ;-------------------------------; Random Number Generator
  3. GetRand:                        ; By: 1024
  4.     PHY                         ; Comments By: Fakescaper
  5.     LDY #$01                    ;
  6.     JSR .Maths                  ;
  7.     DEY                         ;
  8.     JSR .Maths                  ;
  9.     PLY                         ;
  10.     RTL                         ;
  11. .Maths  LDA $148B               ;
  12.     ASL #2                      ;
  13.     SEC                         ;
  14.     ADC $148B                   ;
  15.     STA $148B                   ;
  16.     ASL $148C                   ;
  17.     LDA #$20                    ;
  18.     BIT $148C                   ;
  19.     BCC .Label1                 ; If any of the ADC or ASL cleared carry,
  20.     BEQ .Label2                 ; reverse the results of the BIT test
  21.     BNE .Label3                 ;
  22. .Label1  BNE .Label2            ;
  23. .Label3                         ;
  24.     INC $148C                   ; when determining whether or not we INC
  25. .Label2                         ;
  26.     LDA $148C                   ;
  27.     EOR $148B                   ;
  28.     STA $148D,Y                 ; Output gets stored in this RAM Address, 16 bit result.
  29.     RTS                         ; You might want to add an ADC $13 or SBC $94 before this STA for extra "random"
  30. ;-------------------------------; (or you can just do it after calling this routine)
  31. */
  32.  
  33. /*
  34. Below is a rewritten version that outputs the same numbers pretty much
  35. A random routine that lets you specify how many bytes of entropy to produce.
  36.  
  37. */
  38. #include <iostream>
  39. #include <iomanip>
  40. #include <ctime>        // For the time function
  41. using namespace std;
  42.  
  43. // Process setting/clearing the carry flag
  44. void Process_C(int &A, bool &PF_C)
  45. {
  46.     PF_C = false;
  47.     while(A < 0 || A > 255){PF_C = true; A &= 0xFF;}
  48. }
  49.  
  50. //[0, ((256^bytes)-1)]
  51. unsigned long int GetRand(int bytes, int &ADR_148B, int &ADR_148C, bool &PF_C)
  52. {
  53.     unsigned long int out = 0;
  54.  
  55.     do
  56.     {
  57.         int temp = ADR_148B;
  58.         temp = temp << 2;
  59.         temp += ADR_148B + 1;
  60.         temp &= 0xFF;
  61.         ADR_148B = temp;
  62.  
  63.         ADR_148C = ADR_148C << 1;
  64.         Process_C(ADR_148C, PF_C);
  65.  
  66.         if ( (!PF_C) ^ (0x20 & ADR_148C) )
  67.         {
  68.             if (++ADR_148C > 255)
  69.             {
  70.                 ADR_148C = 0;
  71.             }
  72.         }
  73.  
  74.         temp = ADR_148C^ADR_148B;
  75.  
  76.         out |= temp;
  77.  
  78.         while(bytes > 1 && (out & 0xFF))
  79.         {
  80.             out = out << 8;
  81.         }
  82.  
  83.         bytes--;
  84.     }
  85.     while(bytes > 0);
  86.  
  87.     return out;
  88. }
  89.  
  90. // How many digits the decimal values of bytes will be
  91. int wid[] = {
  92.     0, 3, 5, 8, 10, 13, 15, 17
  93. };
  94.  
  95. int main()
  96. {
  97.     // The Carry Processor Flag
  98.     bool PF_C;
  99.  
  100.     int ADR_148B;
  101.     int ADR_148C;
  102.  
  103.     ADR_148B = (ADR_148B + time(0)) & 0xFF;
  104.     ADR_148C = (ADR_148C + time(0)) & 0xFF;
  105.  
  106.     unsigned long int temp;
  107.  
  108.     for(int i = 1; i <= 5; i++)
  109.     {
  110.         for(int i = 1; i <= 4; i++)
  111.         {
  112.             temp = GetRand(i, ADR_148B, ADR_148C, PF_C); // i bytes of entropy from 1-4 (8-bit, 32-bit, 512-bit, 4096-bit)
  113.             cout << "0x" << setfill('0') << setw(i*2) << uppercase << hex << temp << " (" << setfill(' ') << setw(wid[i]) << dec << temp << ") - ";
  114.         }
  115.  
  116.         cout << "\n";
  117.  
  118.         //temp = GetRand(2, ADR_148B, ADR_148C, PF_C); 2 bytes of entropy
  119.         //cout << "\n" << "0x" << uppercase << hex << temp << " (" << dec << temp << ")\n\n";
  120.     }
  121.    
  122.     cout << "\n";
  123.  
  124.     long double avg = 0;
  125.     int cnt = 0;
  126.  
  127.     for(int i = 1; i <= 14; i++)
  128.     {
  129.         for(int i = 1; i <= 6; i++)
  130.         {
  131.             temp = GetRand(1, ADR_148B, ADR_148C, PF_C);
  132.             avg += temp;
  133.             cnt++;
  134.             cout << "0x" << setfill('0') << setw(2) << uppercase << hex << temp << " (" << setfill(' ') << setw(3) << dec << temp << ") - ";
  135.         }
  136.         cout << "\n";
  137.     }
  138.  
  139.     cout << "The average of " << cnt << " random values was: " << avg/cnt << " for 1 byte of entropy (0-255).";
  140.     cout << "\nWhich is a difference of " << (avg/cnt)-127.5 << "\nFrom the expected average of 127.5";
  141.  
  142.     /*
  143.     2 bytes: 32767.5
  144.     3 bytes: 8388607.5
  145.     4 bytes: 2147483647.5
  146.     */
  147.  
  148.     cout << "\n\nPress Enter to Exit. ";
  149.  
  150.     cin.ignore();
  151.     //cin.get();
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement