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

#include <stdbool.h>

#include "string.h"
#include "stdlib.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;

	// make sure we use the 0x3dx VGA ports, is done
	// in assembly in stage 2 too
	port_init( &vga->crtc_misc_port, PORT_TYPE_8BIT, 0x3d2 );
	port_write8( &vga->crtc_misc_port, 1 );

	// set up VGA ports
	port_init( &vga->crtc_index_port, PORT_TYPE_8BIT, 0x3d4 );
	port_init( &vga->crtc_data_port, PORT_TYPE_8BIT, 0x3d5 );
	
	vga_set_cursor_from_hardware( vga );	
};

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;
	
	// TODO: have a silent mode where we don't update the cursor
	vga_set_cursor_on_hardware( vga );
}

void vga_set_cursor_from_hardware( vga_t *vga )
{
	uint16_t hw_cursor_pos;
	
	port_write8( &vga->crtc_index_port, 14 );
	uint8_t data = port_read8( &(vga->crtc_data_port ) );
	hw_cursor_pos = data << 8;
 
	port_write8( &vga->crtc_index_port, 15 );
	data = port_read8( &(vga->crtc_data_port ) );

	hw_cursor_pos |= data;
	
	vga->cursor_x = hw_cursor_pos % vga->res_x;
	vga->cursor_y = hw_cursor_pos / vga->res_x;
	
	// reset to sane values if we get funny values from the VGA card
	if( vga->cursor_x < 0 || vga->cursor_x > vga->res_x ||
		vga->cursor_y < 0 || vga->cursor_y > vga->res_y ) {
		vga->cursor_x = 0;
		vga->cursor_y = 0;
	}
}

void vga_set_cursor_on_hardware( vga_t *vga )
{
    uint16_t hw_cursor_pos = vga->cursor_x + vga->cursor_y * vga->res_x;

    port_write8( &vga->crtc_index_port, 15 );
    port_write8( &vga->crtc_data_port, hw_cursor_pos & 0xff );
    port_write8( &vga->crtc_index_port, 14 );
    port_write8( &vga->crtc_data_port, hw_cursor_pos >> 8 );
}

int vga_get_cursor_x( vga_t *vga )
{
	return vga->cursor_x;
}

int vga_get_cursor_y( vga_t *vga )
{
	return vga->cursor_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 );
		}
	}

	vga_set_cursor_on_hardware( 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 );
	}

	vga_set_cursor_on_hardware( vga );
}