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

#include <stdbool.h>

#include "string.h"

static bool params_ok( vga_t *vga, const int x, const int y );
static int calculate_offset( vga_t *vga );
static uint8_t calculate_color_cell( vga_t *vga );
static void scroll_screen( 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 )
{
	// TODO: PANIC? OUT OF BOUND ACCESS
	if( !params_ok( vga, x, y ) ) return;

	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 void scroll_screen( vga_t *vga )
{
	volatile uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;

	memmove( (void *)VIDEO_MEMORY, (const void *)VIDEO_MEMORY + 2 * vga->res_x, 2 * ( vga->res_y - 1 ) * vga->res_x );

	for( int i = 2 * ( vga->res_x * ( vga->res_y - 1 ) ); i < 2 * ( vga->res_x * vga->res_y ); i += 2 ) {
		*(VIDEO_MEMORY+i) = ' ';
		*(VIDEO_MEMORY+i+1) = calculate_color_cell( vga );
	}
	
	vga->cursor_y = vga->res_y - 1;
}

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

static bool params_ok( vga_t *vga, const int x, const int y )
{
	if( x > vga->res_x || y > vga->res_y ) return false;

	return true;
}

void vga_put_char_at( vga_t *vga, const int x, const int y, const char c )
{
	vga_set_cursor( vga, x, y );
	vga_put_char( vga, c );
}


void vga_put_string_at( vga_t *vga, const int x, const int y, const char *s )
{
	vga_set_cursor( vga, x, y );
	vga_put_string( vga, s );	
}

void vga_put_char( vga_t *vga, const char c )
{		
	int offset = calculate_offset( vga );

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

	vga->cursor_x++;
	if( vga->cursor_x >= vga->res_x ) {
		vga->cursor_x = 0;
		vga->cursor_y++;
		if( vga->cursor_y >= vga->res_y ) {
			scroll_screen( vga );
		}
	}
}

void vga_put_string( vga_t *vga, const char *s )
{
	for( size_t i = 0; i < strlen( s ); i++ ) {
		vga_put_char( vga, s[i] );
	}
}

void vga_put_newline( vga_t *vga )
{
	vga->cursor_x = 0;
	vga->cursor_y++;
	if( vga->cursor_y >= vga->res_y ) {
		scroll_screen( vga );
	}
}