Advertisement
Guest User

untitled

a guest
Apr 24th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. //**********************************************************************************************
  2. //
  3. // Title: GB to Video
  4. // Author: FriendofMegaman
  5. //
  6. // Cleaned a bit by Jazzmarazz
  7. //
  8. //**********************************************************************************************
  9.  
  10.  
  11. //**********************************************************************************************
  12. //
  13. // Designate Pins with Names
  14. //
  15. //**********************************************************************************************
  16.  
  17. const int pVsync = 2;
  18. const int pClock = 3;
  19. const int pData0 = 4;
  20. const int pData1 = 5;
  21. const int pHsync = 6;
  22.  
  23. //**********************************************************************************************
  24. //
  25. // Create them Variables
  26. //
  27. //**********************************************************************************************
  28.  
  29. int vsync = 0;
  30. int clock = 0;
  31. unsigned char data0 = 0;
  32. unsigned char data1 = 0;
  33. int hsync = 0;
  34.  
  35. int vsync_prev = 0;
  36. int clock_prev = 0;
  37. int hsync_prev = 0;
  38. // int hsync_read = 0; // Never got used it seems
  39.  
  40. unsigned char pixel = 0;
  41.  
  42. int line = 0;
  43. int offset = 0;
  44.  
  45. int array_offset = 0;
  46. int byte_offset = 0;
  47. int bit_offset;
  48.  
  49. unsigned char frame[5760];
  50.  
  51. void setup(){
  52.   Serial.begin(40000);
  53.   // Pins default to inputs. No need for PinMode
  54.   memset(frame,0,sizeof(frame)); // for (int i=0; i<5760; frame[i++]=0);
  55. }
  56.  
  57. //**********************************************************************************************
  58. //
  59. // Main Loop
  60. //
  61. //**********************************************************************************************
  62.  
  63. void loop() {
  64.     vsync = digitalRead(pVsync),
  65.     clock = digitalRead(pClock),
  66.     data0 = digitalRead(pData0),
  67.     data1 = digitalRead(pData1),
  68.     hsync = digitalRead(pHsync);    
  69.    
  70.     readClock();
  71.     readHsync();
  72.     readVsync();
  73.    
  74.     clock_prev = clock;
  75.     vsync_prev = vsync;
  76.     hsync_prev = hsync;
  77. }
  78.  
  79. //**********************************************************************************************
  80. //
  81. // Read hsync
  82. //
  83. //**********************************************************************************************
  84.  
  85. void readHsync(){
  86.     // Read HSYNC (rising edge)
  87.     if (hsync_prev == LOW && hsync == HIGH){
  88.        // Next line
  89.        line++;
  90.        offset = 0;
  91.     }
  92.     return;
  93. }
  94.    
  95. //**********************************************************************************************
  96. //
  97. // Read vsync
  98. //
  99. //**********************************************************************************************
  100.  
  101. void readVsync(){
  102.     if (vsync_prev==LOW && vsync==HIGH){
  103.        // Next frame
  104.  
  105.        // Frame divider 0x1E00
  106.        Serial.write(0x1E);
  107.        Serial.write(0x00);
  108.  
  109.        // Send and nullify frame data
  110.        for (int i=0; i<5760; i++){
  111.           Serial.write(frame[i]);
  112.           frame[i] = 0;    
  113.        }
  114.        line = 0;
  115.        offset = 0;
  116.       }
  117.       return;
  118. }
  119.  
  120. //**********************************************************************************************
  121. //
  122. // Read clock
  123. //
  124. //**********************************************************************************************
  125.  
  126. void readClock(){
  127.     if (clock_prev == HIGH && clock == LOW){
  128.         // Read pixels
  129.         if (offset<160 && line<144){
  130.          
  131.           pixel = 2*data1+data0;
  132.          
  133.           // ************ IMPORTANT ************
  134.           // This part could be implemented as 2-dimensional array, I'm just saving RAM. I tried to make it 2D and the result      
  135.           // was the same.
  136.           // ****************************************
  137.           // Offset in the frame bit map
  138.           bit_offset = (line*320)+offset*2;
  139.  
  140.           // The actual char in the frame array
  141.           array_offset = bit_offset/8;
  142.  
  143.           // The offset within one byte
  144.           byte_offset = bit_offset%8;
  145.  
  146.          // Put the pixel into frame
  147.           frame[array_offset]+=(pixel<<byte_offset);
  148.         }
  149.         offset++;
  150.     }
  151.     return;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement