-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGA.C
182 lines (160 loc) · 3.95 KB
/
VGA.C
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
#include <dos.h>
#include <mem.h>
#include "vga.h"
#define SET_MODE 0x00
#define VIDEO_INT 0x10
#define VGA_256_COLOR_MODE 0x13
#define TEXT_MODE 0x03
byte far * const VGA=(byte far *)0xA0000000L;
/* dimensions of each page and offset */
word vga_width = 320;
word vga_height = 200;
word vga_page[4];
word vga_current_page = 0;
void set_graphics_mode()
{
set_mode(VGA_256_COLOR_MODE);
}
void set_text_mode()
{
set_mode(TEXT_MODE);
}
void set_mode(byte mode)
{
union REGS regs;
regs.h.ah = SET_MODE;
regs.h.al = mode;
int86( VIDEO_INT, ®s, ®s );
}
void update_page_offsets()
{
vga_page[0] = 0;
vga_page[1] = ((dword)vga_width*vga_height) / 4;
vga_page[2] = vga_page[1] * 2;
vga_page[3] = vga_page[1] * 3;
}
void set_mode_y()
{
set_mode( VGA_256_COLOR_MODE );
update_page_offsets();
/* disable chain 4 */
outportb( SC_INDEX, MEMORY_MODE );
outportb( SC_DATA, 0x06 );
/* disable doubleword mode */
outportb( CRTC_INDEX, UNDERLINE_LOCATION );
outportb( CRTC_DATA, 0x00 );
/* disable word mode */
outportb( CRTC_INDEX, MODE_CONTROL );
outportb( CRTC_DATA, 0xE3 );
/* clear all VGA mem */
outportb( SC_INDEX, MAP_MASK );
outportb( SC_DATA, 0xFF );
/* write 2^16 nulls */
memset( VGA + 0x0000, 0x0, 0x8000 ); /* 0x10000 / 2 = 0x8000 */
memset( VGA + 0x8000, 0x0, 0x8000 ); /* 0x10000 / 2 = 0x8000 */
update_page_offsets();
}
void setpix( word page, int x, int y, byte c )
{
outportb( SC_INDEX, MAP_MASK );
outportb( SC_DATA, 1 << (x & 3) );
VGA[ page + ((dword)vga_width * y >> 2) + (x >> 2) ] = c;
/* x/4 is equal to x>>2 */
}
void page_flip( word *page1, word *page2 )
{
byte ac;
word temp;
word high_address, low_address;
temp = *page1;
*page1 = *page2;
*page2 = temp;
high_address = HIGH_ADDRESS | ((*page1) & 0xFF00);
low_address = LOW_ADDRESS | ((*page1) << 8);
/*
instead of:
outportb( CRTC_INDEX, HIGH_ADDRESS );
outportb( CRTC_DATA, (*page1 & 0xFF00) >> 8 );
do this:
high_address = HIGH_ADDRESS | (*page1 & 0xFF00);
outport( CRTC_INDEX, high_address );
*/
outport( CRTC_INDEX, high_address );
outport( CRTC_INDEX, low_address );
disable();
while( inp( INPUT_STATUS ) & VRETRACE );
while( !(inp( INPUT_STATUS ) & VRETRACE ) );
enable();
vga_current_page = *page1;
}
void copy2page( byte far *s, word page, int x0, int y0, int w, int h )
{
int x,y;
byte c;
for( y = 0; y < h; y++ ) {
for( x = 0; x < w; ++x ) {
c = *(s + (dword)y * w + x);
setpix( page, x0 + x, y0 + y, c);
}
}
}
void wait_for_retrace()
{
while( inp( INPUT_STATUS ) & VRETRACE );
while( ! (inp( INPUT_STATUS ) & VRETRACE) );
}
void set_palette(byte *palette)
{
int i;
outp( PALETTE_INDEX, 0 );
for( i = 0; i < NUM_COLORS * 3; ++i ) {
outp( PALETTE_DATA, palette[ i ] );
}
}
void blit2page( byte far *s[], word page, int x, int y, int w, int h )
{
int j;
byte p;
dword screen_offset;
dword bitmap_offset;
for( p = 0; p < 4; p++ ) {
outportb( SC_INDEX, MAP_MASK );
outportb( SC_DATA, 1 << ((p + x) & 3) );
bitmap_offset = 0;
screen_offset = ((dword)y * vga_width + x + p) >> 2;
for(j=0; j<h; j++)
{
memcpy(
VGA + page + screen_offset,
s[p] + bitmap_offset ,
w >> 2
);
bitmap_offset += vga_width >> 2;
screen_offset += vga_width >> 2;
}
}
}
void blit4( byte far *s, int x, int y, int w, int h )
{
int j,i;
byte p;
dword screen_offset;
dword bitmap_offset;
outportb( SC_INDEX, MAP_MASK );
outportb( SC_DATA, 0x0F ); /* Write to all four planes at once */
bitmap_offset = 0;
screen_offset = ((dword)y * vga_width + x) >> 2;
for(j=0; j<h; j++)
{
for(i = 0; i < 4; i++) {
memcpy(
VGA + screen_offset,
s + bitmap_offset ,
w
);
screen_offset += vga_width >> 2;
}
bitmap_offset += vga_width;
}
}