-
Notifications
You must be signed in to change notification settings - Fork 3
/
vga.c
244 lines (215 loc) · 5.36 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
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
#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;
word vga_x_pan = 0;
byte vga_x_pel_pan = 0;
word vga_y_pan = 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, 0, 0x8000 ); /* 0x10000 / 2 = 0x8000 */
memset( VGA + 0x8000, 0, 0x8000 ); /* 0x10000 / 2 = 0x8000 */
}
void set_virtual_640()
{
vga_width = 640;
update_page_offsets();
outportb( CRTC_INDEX, LINE_OFFSET );
outportb( CRTC_DATA, vga_width / 8 );
}
void set_pan_x( int x )
{
vga_x_pan = x / 4;
vga_x_pel_pan = x % 4;
}
void set_pan_y( int y )
{
vga_y_pan = y;
}
void update_pan()
{
byte ac;
word o,high_address,low_address;
o = vga_y_pan * (vga_width >> 2) + vga_x_pan;
high_address = HIGH_ADDRESS | (o & 0xFF00);
low_address = LOW_ADDRESS | (o << 8);
outport( CRTC_INDEX, high_address );
outport( CRTC_INDEX, low_address );
disable();
inp( INPUT_STATUS );
ac = inp( AC_WRITE );
outportb( AC_WRITE, PEL_PANNING );
outportb( AC_WRITE, vga_x_pel_pan );
outportb( AC_WRITE, ac );
while( inp( INPUT_STATUS ) & VRETRACE );
while( !(inp( INPUT_STATUS ) & VRETRACE ) );
enable();
}
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 + vga_x_pan) & 0xFF00);
low_address = LOW_ADDRESS | ((*page1 + vga_x_pan) << 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();
#if 0
ac = inp( AC_WRITE );
outportb( AC_WRITE, PEL_PANNING );
outportb( AC_WRITE, vga_x_pel_pan );
outportb( AC_WRITE, ac );
#endif
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 blit2mem( byte far *d, int x, int y, int w, int h )
{
int i;
byte far *src = (byte far *)VGA + x + y * vga_width;
byte far *dst = d;
for( i = y; i < y + h; ++i ) {
movedata( FP_SEG( src ), FP_OFF( src ),
FP_SEG( dst ), FP_OFF( dst ), w );
src += vga_width;
dst += w;
}
}
void blit2vga( byte far *s, int x, int y, int w, int h )
{
int i;
byte far *src = s;
byte far *dst = (byte far *)VGA + x + y * vga_width;
for( i = y; i < y + h; ++i ) {
movedata( FP_SEG( src ), FP_OFF( src ),
FP_SEG( dst ), FP_OFF( dst ), w );
src += w;
dst += vga_width;
}
}
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 = p * w / 4 * h;
screen_offset = ((dword)y * vga_width + x + p) >> 2;
for(j=0; j<h; j++)
{
memcpy(
VGA + page + screen_offset,
s + bitmap_offset,
w >> 2
);
bitmap_offset += w >> 2;
screen_offset += vga_width >> 2;
}
}
}
/* Warning: no clipping! */
void draw_rectangle( int x, int y, int w, int h, byte c )
{
int i, j;
for( j = y; j < y + h; ++j ) {
for( i = x; i < x + w; ++i ) {
byte far *dst = (byte far *)VGA + i + j * vga_width;
*dst = c;
}
}
}