-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathVGAXUA.cpp
247 lines (230 loc) · 6.68 KB
/
VGAXUA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "VGAXUA.h"
#define MSPIMSCK 4
#if defined(__AVR_ATmega2560__)
//HSYNC pin used by TIMER2
#define HSYNCPIN 9
//VSYNC pin used by TIMER1
#define VSYNCPIN 11
//These two pin cannot be modified without modify the HSYNC assembler code
#define COLORPIN0 30
#define COLORPIN1 31
#define EXTRACOLORPORT PORTC
#else
//HSYNC pin used by TIMER2
#define HSYNCPIN 3
//VSYNC pin used by TIMER1
#define VSYNCPIN 9
//These two pin cannot be modified without modify the HSYNC assembler code
#define COLORPIN0 6
#define COLORPIN1 7
#define EXTRACOLORPORT PORTD
#endif
//Number of VGA lines to be skipped (black lines)
/*These lines includes the vertical sync pulse and back porch.
Minimum value must be 35 (calculate from Nick Gammon)
You can modify this value to center the framebuffer vertically, or not*/
#define SKIPLINES 35
unsigned vtimer;
static byte aline, rlinecnt;
static byte vskip;
byte vgaxfb[VGAX_HEIGHT*VGAX_BWIDTH];
static byte *videoline;
static byte vmask;
//VSYNC interrupt
ISR(TIMER1_OVF_vect) {
aline=-1;
vskip=SKIPLINES;
vtimer++;
rlinecnt=0;
videoline=vgaxfb;
}
//HSYNC interrupt
ISR(TIMER2_OVF_vect) {
/*
NOTE: I prefer to generate the line here, inside the interrupt.
Gammon's code generate the line pixels inside main().
My versin generate the signal using only interrupts, so inside main() function
you can do anything you want. Your code will be interrupted when VGA signal
needs to be generated
*/
//check vertical porch
if (vskip) {
vskip--;
return;
}
if (rlinecnt<VGAX_HEIGHT) {
#define nop asm volatile("nop")
#if defined(__AVR_ATmega2560__)
nop;
nop;
#endif
//interrupt jitter fix (needed to keep signal stable)
//code from https://github.com/cnlohr/avrcraft/tree/master/terminal
//modified from VGAX dejitter version (this is faster)
#define DEJITTER_SYNC -0 //was -3 on ATMEGA2560
asm volatile(
" lds r16, %[timer0] \n\t" //
" subi r16, %[tsync] \n\t" //
" andi r16, 7 \n\t" //
" call TL \n\t" //
"TL: \n\t" //
#if defined(__AVR_ATmega2560__)
" pop r17 \n\t" //ATMEGA2560 has a 22bit PC!
#endif
" pop r31 \n\t" //
" pop r30 \n\t" //
" adiw r30, (LW-TL-5) \n\t" //
" add r30, r16 \n\t" //
" ijmp \n\t" //
"LW: \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
" nop \n\t" //
"LBEND: \n\t" //
:
: [timer0] "i" (&TCNT0),
[tsync] "i" ((uint8_t)DEJITTER_SYNC)
: "r30", "r31", "r16", "r17");
register byte *p=videoline;
register byte c=*p++;;
//first 8px
#define drawF UDR0=c; UCSR0B=bit(TXEN0); c=*p++; nop;
//second 8px will turn on Extended Colors PINS
#define drawS UDR0=c; EXTRACOLORPORT=vmask; nop; nop; nop; nop; nop; nop; nop; c=*p++;
//draw 8px
#define draw8 UDR0=c; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; c=*p++;
#if !defined(__AVR_ATmega2560__)
nop; nop;
#endif
//draw 192px
#if defined(__AVR_ATmega2560__)
drawF draw8 drawS draw8
#else
drawF drawS draw8 draw8
#endif
draw8 draw8 draw8 draw8
draw8 draw8 draw8 draw8
draw8 draw8 draw8 draw8
draw8 draw8 draw8 draw8
draw8 draw8 draw8
#if defined(__AVR_ATmega2560__)
draw8
#endif
//draw last 8px
UDR0=c;
UCSR0B=0;
//wait to disable EXTRACOLORS with the right timing
nop;
#if !defined(__AVR_ATmega2560__)
nop; nop; nop; nop;
nop; nop; nop; nop;
nop; nop; nop; nop;
nop; nop; nop; nop;
#endif
//disable VMASK
EXTRACOLORPORT&=~vmask;
}
//increment framebuffer line counter after 6 VGA lines
#if defined(__AVR_ATmega2560__) && defined(ATMEGA2560_MAXRES)
#define CLONED_LINES (2-1)
#else
#define CLONED_LINES (6-1)
#endif
if (++aline==CLONED_LINES) {
aline=-1;
rlinecnt++;
videoline+=VGAX_BWIDTH;
} else {
}
}
void VGAXUA::begin() {
//Timers setup code, modified version of the Nick Gammon's VGA sketch
cli();
//disable TIMER0 interrupt
TIMSK0=0;
TCCR0A=0;
TCCR0B=(1 << CS00); //enable 16MHz counter (used to fix the HSYNC interrupt jitter)
OCR0A=0;
OCR0B=0;
TCNT0=0;
//TIMER1 - vertical sync pulses
pinMode(VSYNCPIN, OUTPUT);
#if VSYNCPIN==10 //ATMEGA328 PIN 10
TCCR1A=bit(WGM10) | bit(WGM11) | bit(COM1B1);
TCCR1B=bit(WGM12) | bit(WGM13) | bit(CS12) | bit(CS10); //1024 prescaler
OCR1A=259; //16666 / 64 uS=260 (less one)
OCR1B=0; //64 / 64 uS=1 (less one)
TIFR1=bit(TOV1); //clear overflow flag
TIMSK1=bit(TOIE1); //interrupt on overflow on TIMER1
#else //ATMEGA328 PIN 9 or ATMEGA2560 PIN 11
TCCR1A=bit(WGM11) | bit(COM1A1);
TCCR1B=bit(WGM12) | bit(WGM13) | bit(CS12) | bit(CS10); //1024 prescaler
ICR1=259; //16666 / 64 uS=260 (less one)
OCR1A=0; //64 / 64 uS=1 (less one)
TIFR1=bit(TOV1); //clear overflow flag
TIMSK1=bit(TOIE1); //interrupt on overflow on TIMER1
#endif
//TIMER2 - horizontal sync pulses
pinMode(HSYNCPIN, OUTPUT);
TCCR2A=bit(WGM20) | bit(WGM21) | bit(COM2B1); //pin3=COM2B1
TCCR2B=bit(WGM22) | bit(CS21); //8 prescaler
OCR2A=63; //32 / 0.5 uS=64 (less one)
OCR2B=7; //4 / 0.5 uS=8 (less one)
TIFR2=bit(TOV2); //clear overflow flag
TIMSK2=bit(TOIE2); //interrupt on overflow on TIMER2
//Setup USART
UBRR0=0;
pinMode(MSPIMSCK, OUTPUT);
UCSR0B=0;
UCSR0C=bit(UCPOL0) | bit (UMSEL00) | bit (UMSEL01);
UCSR0B=0;
//pins for outputting the colour information
pinMode(COLORPIN0, OUTPUT);
pinMode(COLORPIN1, OUTPUT);
sei();
}
void VGAXUA::end() {
//disable TIMER0
TCCR0A=0;
TCCR0B=0;
//disable TIMER1
TCCR1A=0;
TCCR1B=0;
//disable TIMER2
TCCR2A=0;
TCCR2B=0;
}
void VGAXUA::clear(byte color) {
register byte c=color;
c&=1; //0000 0001
c|=c<<1; //0000 0011
c|=c<<2; //0000 1111
c|=c<<4; //1111 1111
unsigned cnt=VGAX_BSIZE;
byte *o=(byte*)vgaxfb;
while (cnt--)
*o++=c;
}
void VGAXUA::copy(byte *src) {
byte *o=(byte*)vgaxfb;
unsigned cnt=VGAX_BSIZE;
while (cnt--)
*o++=pgm_read_byte(src++);
}
void VGAXUA::setExtendedColorsMask(byte mask) {
vmask=(mask & 3)<<6;
}
void VGAXUA::delay(int msec) {
while (msec--) {
unsigned cnt=16000/32;
while (cnt--)
asm volatile("nop\nnop\nnop\nnop\n");
}
}
static unsigned long rand_next=1;
unsigned VGAXUA::rand() {
rand_next=rand_next * 1103515245UL + 12345;
return rand_next+((unsigned)(rand_next / 65536) % 32768);
}