View difference between Paste ID: Crs2FVE1 and DFt5dEA3
SHOW: | | - or go back to the newest paste.
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
// Reassign according to your pinout
18
const int pVsync = 2;
19
const int pClock = 3;
20
const int pData0 = 4;
21
const int pData1 = 5;
22
const int pHsync = 6;
23
24
//**********************************************************************************************
25
//
26
// Create them Variables
27
//
28
//**********************************************************************************************
29
30
int vsync = 0;
31
int clock = 0;
32
unsigned char data0 = 0;
33
unsigned char data1 = 0;
34
int hsync = 0;
35
36
int vsync_prev = 0;
37
int clock_prev = 0;
38-
// int hsync_read = 0; // Never got used it seems
38+
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);
52+
  Serial.begin(1);
53-
  // Pins default to inputs. No need for PinMode 
53+
  memset(frame,0,sizeof(frame));
54-
  memset(frame,0,sizeof(frame)); // for (int i=0; i<5760; frame[i++]=0);
54+
55
56
//**********************************************************************************************
57
//
58
// Main Loop
59
//
60
//**********************************************************************************************
61
62
void loop() {
63
    vsync = digitalRead(pVsync),
64
    clock = digitalRead(pClock),
65
    data0 = digitalRead(pData0), 
66
    data1 = digitalRead(pData1),
67
    hsync = digitalRead(pHsync);    
68
    
69
    readClock();
70
    readHsync();
71
    readVsync();
72
    
73
    clock_prev = clock;
74
    vsync_prev = vsync;
75
    hsync_prev = hsync;
76
}
77
78
//**********************************************************************************************
79
//
80
// Read hsync 
81
//
82
//**********************************************************************************************
83
84
void readHsync(){
85
    // Read HSYNC (rising edge)
86
    if (hsync_prev == LOW && hsync == HIGH){
87
       // Next line
88
       line++;
89
       offset = 0;
90
    }
91
    return;
92
}
93
    
94
//**********************************************************************************************
95
//
96
// Read vsync 
97
//
98
//**********************************************************************************************
99
100
void readVsync(){
101
    if (vsync_prev==LOW && vsync==HIGH){
102
       // Next frame
103
104
       // Frame divider 0x1E001E00
105-
       // Frame divider 0x1E00
105+
106
       Serial.write(0x00);
107
       Serial.write(0x1E);
108
       Serial.write(0x00);
109
110-
       for (int i=0; i<5760; i++){
110+
111-
          Serial.write(frame[i]);
111+
       Serial.write(frame, 5760);
112-
          frame[i] = 0;     
112+
       memset(frame, 0, sizeof(frame));
113-
       }
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
}