summaryrefslogtreecommitdiff
path: root/src/vga.c
blob: 767d45f1812f7e9984f89ee612022dd82c6d00c6 (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
#include "vga.h"

#include "string.h"

static int calculate_offset( vga_t *vga );
static uint8_t calculate_color_cell( vga_t *vga );

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

	vga->res_x = VGA_DEFAULT_RES_X;
	vga->res_y = VGA_DEFAULT_RES_Y;
	vga->color = VGA_COLOR_DARK_GREY;
	vga->background_color = VGA_COLOR_BLACK;
	
	// TODO: get current position from VGA hardware
	//~ vga_set_cursor( vga, 0, 0 );
};

void vga_clear_screen( vga_t *vga )
{
	volatile uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;

	for( int i = 0; i < 2 * ( vga->res_x * vga->res_y ); i += 2 ) {
		*(VIDEO_MEMORY+i) = ' ';
		*(VIDEO_MEMORY+i+1) = calculate_color_cell( vga );
	}
	
	//~ vga_set_cursor( vga, 0, 0 );
}

void vga_set_cursor( vga_t *vga, const int x, const int y )
{
	vga->cursor_x = x;
	vga->cursor_y = y;
}

void vga_set_color( vga_t *vga, const vga_color_t color )
{
	vga->color = color;
}

void vga_set_background_color( vga_t *vga, const vga_color_t color )
{
	vga->background_color = color;
}

static int calculate_offset( vga_t *vga )
{
	int offset = ( vga->res_x * vga->cursor_y + vga->cursor_x ) * 2;
	
	return offset;
}

static uint8_t calculate_color_cell( vga_t *vga )
{
	uint8_t cell;
	
	cell = ( vga->background_color << 4 ) | vga->color;
	
	return cell;
}

void vga_put_char( vga_t *vga, const int x, const int y, const uint8_t c )
{
	// TODO: PANIC? OUT OF BOUND ACCESS
	if( x > vga->res_x || y > vga->res_y ) return;
	
	vga_set_cursor( vga, x, y );
	
	int offset = calculate_offset( vga );

	volatile uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;
	*(VIDEO_MEMORY+offset) = c;
	*(VIDEO_MEMORY+offset+1) = calculate_color_cell( vga );
}