Advertisement
Chdata

65c816 Assembly Subroutine as a Function in C++

Apr 4th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. /*
  2. Below is a rewritten version that outputs the same numbers pretty much
  3. but without relying on ADR_148D[Y]
  4.  
  5. (There's another version written to act more like 65c815 asm)
  6.  
  7. */
  8. #include <iostream>
  9. #include <ctime>        // For the time function
  10. using namespace std;
  11.  
  12. // Process setting/clearing the carry flag
  13. void Process_C(int &A, bool &PF_C)
  14. {
  15.     PF_C = false;
  16.     while(A < 0 || A > 255){PF_C = true; A &= 0xFF;}
  17. }
  18.  
  19. //[0, ((256^bytes)-1)]
  20. unsigned long int GetRand(int bytes, int &ADR_148B, int &ADR_148C, bool &PF_C)
  21. {
  22.     unsigned long int out = 0;
  23.  
  24.     do
  25.     {
  26.         int temp = ADR_148B;
  27.         temp = temp << 2;
  28.         Process_C(temp, PF_C);
  29.         PF_C = true;
  30.         temp += ADR_148B + PF_C; //PF_C = 1; always
  31.         Process_C(temp, PF_C);
  32.         ADR_148B = temp;
  33.  
  34.         ADR_148C = ADR_148C << 1;
  35.         Process_C(ADR_148C, PF_C);
  36.  
  37.         if ( (!PF_C) ^ (0x20 & ADR_148C) )
  38.         {
  39.             if (++ADR_148C > 255)
  40.             {
  41.                 ADR_148C = 0;
  42.             }
  43.         }
  44.  
  45.         temp = ADR_148C^ADR_148B;
  46.  
  47.         out |= temp;
  48.  
  49.         while(bytes > 1 && (out & 0xFF))
  50.         {
  51.             out = out << 8;
  52.         }
  53.  
  54.         bytes--;
  55.     }
  56.     while(bytes > 0);
  57.  
  58.     return out;
  59. }
  60.  
  61. int main()
  62. {
  63.     // The Carry Processor Flag
  64.     bool PF_C;
  65.  
  66.     int ADR_148B;
  67.     int ADR_148C;
  68.  
  69.     ADR_148B = (ADR_148B + time(0)) & 0xFF;
  70.     ADR_148C = (ADR_148C + time(0)) & 0xFF;
  71.  
  72.     unsigned long int temp;
  73.  
  74.     for(int i = 1; i <= 5; i++)
  75.     {
  76.         for(int i = 1; i <= 4; i++)
  77.         {
  78.             temp = GetRand(i, ADR_148B, ADR_148C, PF_C); // i bytes of entropy from 1-4 (8-bit, 32-bit, 512-bit, 4096-bit)
  79.             cout << hex << "0x" << temp << " (" << dec << temp << ") - ";
  80.         }
  81.  
  82.         cout << "\n\n";
  83.  
  84.         //temp = GetRand(2, ADR_148B, ADR_148C, PF_C); 2 bytes of entropy
  85.         //cout << "\n" << hex << "0x" << temp << " (" << dec << temp << ")\n\n";
  86.     }
  87.  
  88.     long double avg = 0;
  89.     int cnt = 0;
  90.  
  91.     for(int i = 1; i <= 500; i++)
  92.     {
  93.         for(int i = 1; i <= 6; i++)
  94.         {
  95.             temp = GetRand(1, ADR_148B, ADR_148C, PF_C);
  96.             avg += temp;
  97.             cnt++;
  98.             // Uncommenting these will output all the hex in a similar manner to the loop above
  99.             //cout << hex << "0x" << temp << " (" << dec << temp << ") - ";
  100.         }
  101.         //cout << "\n";
  102.     }
  103.  
  104.     cout << "The average of " << cnt << " random values was: " << avg/cnt << " for 1 byte of entropy (0-255).";
  105.     cout << "\nWhich is a difference of " << (avg/cnt)-127.5 << "\nFrom the expected average of 127.5";
  106.  
  107.     /*
  108.     2 bytes: 32767.5
  109.     3 bytes: 8388607.5
  110.     4 bytes: 2147483647.5
  111.     */
  112.  
  113.     cout << "\n\nPress Enter to Exit. ";
  114.  
  115.     cin.ignore();
  116.     //cin.get();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement