-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGA.C
245 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
245
#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
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 200
#define ABS(a) ((a)>0?(a):-(a))
#define SETPIX(x,y,c) *(BUF + (dword)SCREEN_WIDTH * (y) + (x)) = c
byte far * const VGA=(byte far *)0xA0000000L;
byte far * BUF=(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 cycle_palette(byte *palette, int j)
{
int i;
outp( PALETTE_INDEX, 0 );
for( i = 0; i < NUM_COLORS * 3; ++i ) {
outp( PALETTE_DATA, palette[ (i + j * 3) % (NUM_COLORS * 3) ] );
}
}
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 = x >> 2;
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;
}
}
void memcpy_rect(
byte far *dest, byte far *src,
word width, word height,
word x0_src, word y0_src,
word x0_dst, word y0_dst,
word copy_width, word copy_height
)
{
word i;
byte far *s = src + x0_src + y0_src * width;
byte far *d = dest + x0_dst + y0_dst * height;
for( i = 0; i < copy_height; ++i, s += width, d += width ) {
memcpy( d, s, copy_width );
}
}
void
draw_line(int x0, int y0, int x1, int y1, char col)
{
int dx,dy,sx,sy,error,e2;
dx = ABS(x1 - x0);
sx = x0 < x1 ? 1 : -1;
dy = ABS(y1 - y0);
sy = y0 < y1 ? 1 : -1;
error = dx - dy;
while(1)
{
if(x0 >= 0 && y0 >= 0 && x0 < SCREEN_WIDTH && y0 < SCREEN_HEIGHT)
SETPIX(x0, y0, col);
if( x0 == x1 && y0 == y1 ) break;
e2 = 2 * error;
if( e2 >= -dy ) {
if( x0 == x1 ) break;
error -= dy;
x0 += sx;
}
if( e2 <= dx ) {
if( y0 == y1 ) break;
error += dx;
y0 += sy;
}
}
}