summaryrefslogtreecommitdiff
path: root/src/drivers/video/vga.c
blob: 0d3c49a931957a4259c700eacefdac00ef605aed (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "vga.h"

#include "stdio.h"

#include "kernel.h"

#include "vga_font.h"

#undef DEBUG

static vga_vtable_t vga_vtable = {
	{
	vga_activate,
	vga_deactivate,
	vga_deinit,
	vga_print_info
	}
};

void vga_init( vga_t *vga, void *context )
{
	memset( vga, 0, sizeof( vga_t ) );

	port8_init( &vga->misc_port, 0x3C2 );
	port8_init( &vga->crtc_index_port, 0x3D4 );
	port8_init( &vga->crtc_data_port, 0x3D5 );
	port8_init( &vga->sequencer_index_port, 0x3C4 );
	port8_init( &vga->sequencer_data_port, 0x3C5 );
	port8_init( &vga->graphics_controller_index_port, 0x3CE );
	port8_init( &vga->graphics_controller_data_port, 0x3CF );
	port8_init( &vga->attribute_controller_index_port, 0x3C0 );
	port8_init( &vga->attribute_controller_read_port, 0x3C1 );
	port8_init( &vga->attribute_controller_write_port, 0x3C0 );
	port8_init( &vga->attribute_controller_reset_port, 0x3DA );

	vga->context = context;

	vga->base.vtable = &vga_vtable.base;
}

void vga_activate( void *obj )
{
	puts( "Activating driver for VGA video card.." );
}

void vga_deactivate( void *obj )
{
	puts( "Dectivating driver for VGA video card.." );
}

void vga_deinit( void *obj )
{
	// nothing to do
}

void vga_print_info( void *obj )
{
	puts( "Generic VGA video driver" );
}

static void write_registers( vga_t *vga, uint8_t *regs )
{
	// misc
	port8_write( &vga->misc_port, *( regs++ ) );

	// seq (5)
	for( uint8_t i = 0; i < 5; i++ ) {
		port8_write( &vga->sequencer_index_port, i );
		port8_write( &vga->sequencer_data_port, *( regs++ ) );
	}

	// unlock CRTC register (and make sure mode data doesn't lock it again by accident) 
	port8_write( &vga->crtc_index_port, 0x03 );
	port8_write( &vga->crtc_data_port, port8_read( &vga->crtc_data_port ) | 0x80 );
	port8_write( &vga->crtc_index_port, 0x11 );
	port8_write( &vga->crtc_data_port, port8_read( &vga->crtc_data_port ) & ~0x80 );
	regs[0x3] |= 0x80;
	regs[0x11] &= ~0x80;

	// crtc (25)
	for( uint8_t i = 0; i < 25; i++ ) {
		port8_write( &vga->crtc_index_port, i );
		port8_write( &vga->crtc_data_port, *( regs++ ) );
	}

	// gc (9)
	for( uint8_t i = 0; i < 9; i++ ) {
		port8_write( &vga->graphics_controller_index_port, i );
		port8_write( &vga->graphics_controller_data_port, *( regs++ ) );
	}

	// ac (21)
	for( uint8_t i = 0; i < 21; i++ ) {
		(void)port8_read( &vga->attribute_controller_reset_port );
		port8_write( &vga->attribute_controller_index_port, i );
		port8_write( &vga->attribute_controller_write_port, *( regs++ ) );
	}

	// lock 16-color palette and unblank display
	(void)port8_read( &vga->attribute_controller_reset_port );
	port8_write( &vga->attribute_controller_index_port, 0x20 );
}

vga_mode_t vga_make_mode( const vga_mode_type_t mode_type, const int x, const int y, const int color_depth )
{
	vga_mode_t mode;

	mode.mode_type = mode_type;
	mode.x = x;
	mode.y = y;
	mode.color_depth = color_depth;

	return mode;
}
	
// from http://files.osdev.org/mirrors/geezer/osd/graphics/modes.c
static vga_mode_t modes[] = {
	{ VGA_MODE_TYPE_TEXT, 640, 480, 4,
		{ 
		/* MISC */
			0x67,
		/* SEQ */
			0x03, 0x00, 0x03, 0x00, 0x02,
		/* CRTC */
			0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
			0x00, 0x4F, 0x0D, 0x0E, 0x00, 0x00, 0x00, 0x50,
			0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
			0xFF,
		/* GC */
			0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00,
			0xFF,
		/* AC */
			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
			0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
			0x0C, 0x00, 0x0F, 0x08, 0x00
		},
		NULL
	},
	{ VGA_MODE_TYPE_GRAPHICS, 320, 200, 8,
		{
		/* MISC */
			0x63,
		/* SEQ */
			0x03, 0x01, 0x0F, 0x00, 0x0E,
		/* CRTC */
			0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
			0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3,
			0xFF,
		/* GC */
			0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,
			0xFF,
		/* AC */
			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
			0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
			0x41, 0x00, 0x0F, 0x00, 0x00
		},
		NULL
	},
	{ VGA_MODE_TYPE_GRAPHICS, 640, 480, 4,
		{
		/* MISC */
			0xE3,
		/* SEQ */
			0x03, 0x01, 0x08, 0x00, 0x06,
		/* CRTC */
			0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0x0B, 0x3E,
			0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0xEA, 0x0C, 0xDF, 0x28, 0x00, 0xE7, 0x04, 0xE3,
			0xFF,
		/* GC */
			0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x05, 0x0F,
			0xFF,
		/* AC */
			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
			0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
			0x01, 0x00, 0x0F, 0x00, 0x00
		},
		NULL
	}
};

bool vga_supports_mode( const vga_mode_t mode )
{
	for( int i = 0; modes[i].x != 0; i++ ) {
		if( 	mode.mode_type == modes[i].mode_type &&
			mode.x == modes[i].x &&
			mode.y == modes[i].y &&
			mode.color_depth == modes[i].color_depth ) {
			return true;
		}
	}

	return false;
}

static int get_matching_mode_idx( const vga_mode_t mode )
{
	for( int i = 0; modes[i].x != 0; i++ ) {
		if( 	mode.mode_type == modes[i].mode_type &&
			mode.x == modes[i].x &&
			mode.y == modes[i].y &&
			mode.color_depth == modes[i].color_depth ) {
			return i;
		}
	}

	return -1;
}

static uint8_t *get_frame_buffer_segment( vga_t *vga )
{
	port8_write( &vga->graphics_controller_index_port, 0x06 );
	uint8_t segment_no = ( port8_read( &vga->graphics_controller_data_port ) >> 2 ) & 0x03;
	uint8_t *segment;

	switch( segment_no  ) {
		case 0: // A0000h - BFFFFh, 128k
			segment = (uint8_t *)0xA0000;
			break;

		case 1: // A0000h - AFFFFh, 64k
			segment = (uint8_t *)0xA0000;
			break;

		case 2: // B0000h - B7FFFh, 32k
			segment = (uint8_t *)0xB0000;
			break;

		case 3: // B8000h - BFFFFh, 32k
			segment = (uint8_t *)0xB8000;
			break;
	}

#ifdef DEBUG
	printf( "segment: %d 0x%X\n", segment_no, segment );
#endif

	return segment;
}

bool vga_set_mode( vga_t *vga, const vga_mode_t mode )
{
	// do not allow unknown timings!
	if( !vga_supports_mode( mode ) ) {
		return false;
	}

	vga_mode_t matching_mode = modes[get_matching_mode_idx( mode )];

	write_registers( vga, matching_mode.regs );

	vga->mode = mode;
	vga->mode.segment = get_frame_buffer_segment( vga );

	return true;
}


vga_color_t vga_make_RGB( int R, int G, int B )
{
	vga_color_t c;

	c.R = R;
	c.G = G;
	c.B = B;

	return c;
}

static uint8_t get_color_index( const vga_color_t c )
{
	// TODO: for now white and black, standard VGA palette entries?
	if( c.R == 0x00 && c.G == 0x00 && c.B == 0x00 ) return 0x00;
	if( c.R == 0x00 && c.G == 0x00 && c.B == 0xA8 ) return 0x01;
	if( c.R == 0xFF && c.G == 0xFF && c.B == 0xFF ) return 0x3F;

	return 0x00;
}

static bool params_ok( vga_t *vga, const int x, const int y )
{
	if( x < 0 || x > vga->mode.x || y < 0 || y > vga->mode.y ) {
		return false;
	}

	return true;
}

void vga_set_pixel( vga_t *vga, const int x, const int y, const vga_color_t color )
{
	if( !params_ok( vga, x, y ) ) {
		kernel_panic( "Pixel coordinates are out of bounds: (%d, %d), resolution is only (%d, %d)",
			x, y, vga->mode.x, vga->mode.y );
	}

	uint8_t color_idx = get_color_index( color );

	uint8_t *addr = vga->mode.segment + vga->mode.x * y + x;

	*addr = color_idx;
}	

void vga_draw_rectangle( vga_t *vga, const int x, const int y, const int w, const int h, const vga_color_t color )
{
	for( int yy = y; yy < y + h; yy++ ) {
		for( int xx = x; xx < x + w; xx++ ) {
			vga_set_pixel( vga, xx, yy, color );
		}
	}
}

void vga_clear_screen( vga_t *vga, const vga_color_t color )
{
	memset( vga->mode.segment, get_color_index( color ),
		vga->mode.x * vga->mode.y );
}

void vga_draw_char( vga_t *vga, const unsigned char c, const int x, const int y, const vga_color_t background, const vga_color_t foreground )
{

//const struct bitmap_font font = {

/*        .Width = 8, .Height = 16,
        .Chars = 2899,
        .Widths = __font_widths__,
        .Index = __font_index__,
        .Bitmap = __vga_font_bitmap__,
*/
	// assuming font width is 8
	int mask[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };

	// for now, the 34 entry, character height is 16, assuming A-Z
	const unsigned char *bmap = &vga_font.Bitmap[(34+c-65)*16];

	for( int xx = 0; xx < vga_font.Width; xx++ ) {
		for( int yy = 0; yy < vga_font.Height; yy++ ) {
			if( bmap[yy] & mask[xx] ) {
				vga_set_pixel( vga, x + 10 - xx, y + yy, foreground );
			} else {
				vga_set_pixel( vga, x + 10 - xx, y + yy, background );
			}
		}
	}
}