summaryrefslogtreecommitdiff
path: root/src/kernel/vgatext.c
blob: 13a089e1d1f9a07c69c7a3043cc1cd1b690a55bb (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
#include "vgatext.h"

#include <stdbool.h>

#include "string.h"
#include "stdlib.h"
#include "kernel.h"

static bool params_ok( vga_text_t *vga_text, const int x, const int y );
static int calculate_offset( vga_text_t *vga_text, const int x, const int y );
static uint8_t calculate_color_cell( vga_text_t *vga_text );
static void scroll_screen( vga_text_t *vga_text );

void vga_text_init( vga_text_t *vga_text )
{
	memset( vga_text, 0, sizeof( vga_text_t ) );

	vga_text->res_x = VGA_TEXT_DEFAULT_RES_X;
	vga_text->res_y = VGA_TEXT_DEFAULT_RES_Y;
	vga_text->color = VGA_TEXT_COLOR_DARK_GREY;
	vga_text->background_color = VGA_TEXT_COLOR_BLACK;
	vga_text->show_mouse_cursor = false;
	vga_text->mouse_cursor_x = -1;
	vga_text->mouse_cursor_y = -1;

	// make sure we use the 0x3dx VGA_TEXT ports, is done
	// in assembly in stage 2 too
	port8_init( &vga_text->crtc_misc_port, 0x3d2 );
	port8_write( &vga_text->crtc_misc_port, 1 );

	// set up VGA_TEXT ports
	port8_init( &vga_text->crtc_index_port, 0x3d4 );
	port8_init( &vga_text->crtc_data_port, 0x3d5 );
	
	vga_text_set_cursor_from_hardware( vga_text );	
};

void vga_text_clear_screen( vga_text_t *vga_text )
{
	uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;

	for( int i = 0; i < 2 * ( vga_text->res_x * vga_text->res_y ); i += 2 ) {
		*(VIDEO_MEMORY+i) = ' ';
		*(VIDEO_MEMORY+i+1) = calculate_color_cell( vga_text );
	}
	
	vga_text_set_cursor( vga_text, 0, 0 );
}

void vga_text_set_cursor( vga_text_t *vga_text, const int x, const int y )
{
	if( !params_ok( vga_text, x, y ) ) {
		kernel_panic( "Cursor parameters out of bounds: (%d, %d), resolution is only (%d, %d)",
			x, y, vga_text->res_x, vga_text->res_y );
	}

	vga_text->cursor_x = x;
	vga_text->cursor_y = y;
	
	// TODO: have a silent mode where we don't update the cursor
	vga_text_set_cursor_on_hardware( vga_text );
}

void vga_text_set_cursor_from_hardware( vga_text_t *vga_text )
{
	uint16_t hw_cursor_pos;
	
	port8_write( &vga_text->crtc_index_port, 14 );
	uint8_t data = port8_read( &(vga_text->crtc_data_port ) );
	hw_cursor_pos = data << 8;
 
	port8_write( &vga_text->crtc_index_port, 15 );
	data = port8_read( &(vga_text->crtc_data_port ) );

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

void vga_text_set_cursor_on_hardware( vga_text_t *vga_text )
{
    uint16_t hw_cursor_pos = vga_text->cursor_x + vga_text->cursor_y * vga_text->res_x;

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

int vga_text_get_cursor_x( vga_text_t *vga_text )
{
	return vga_text->cursor_x;
}

int vga_text_get_cursor_y( vga_text_t *vga_text )
{
	return vga_text->cursor_y;
}

void vga_text_set_color( vga_text_t *vga_text, const vga_text_color_t color )
{
	vga_text->color = color;
}

void vga_text_set_background_color( vga_text_t *vga_text, const vga_text_color_t color )
{
	vga_text->background_color = color;
}

static int calculate_offset( vga_text_t *vga_text, const int x, const int y )
{
	int offset = ( vga_text->res_x * y + x ) * 2;
	
	return offset;
}

static void scroll_screen( vga_text_t *vga_text )
{
	uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;
	
	memmove( (void *)VIDEO_MEMORY, (const void *)VIDEO_MEMORY + 2 * vga_text->res_x, 2 * ( vga_text->res_y - 1 ) * vga_text->res_x );

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

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

static bool params_ok( vga_text_t *vga_text, const int x, const int y )
{
	if( x < 0 || x > vga_text->res_x || y < 0 || y > vga_text->res_y ) {
		return false;
	}

	return true;
}

void vga_text_put_char_at( vga_text_t *vga_text, const int x, const int y, const char c )
{
	vga_text_set_cursor( vga_text, x, y );
	vga_text_put_char( vga_text, c );
}


void vga_text_put_string_at( vga_text_t *vga_text, const int x, const int y, const char *s )
{
	vga_text_set_cursor( vga_text, x, y );
	vga_text_put_string( vga_text, s );	
}

void vga_text_put_char( vga_text_t *vga_text, const char c )
{		
	int offset = calculate_offset( vga_text, vga_text->cursor_x, vga_text->cursor_y );

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

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

	vga_text_set_cursor_on_hardware( vga_text );
}

void vga_text_put_string( vga_text_t *vga_text, const char *s )
{
	for( size_t i = 0; i < strlen( s ); i++ ) {
		vga_text_put_char( vga_text, s[i] );
	}
}

void vga_text_put_newline( vga_text_t *vga_text )
{
	vga_text->cursor_x = 0;
	vga_text->cursor_y++;
	if( vga_text->cursor_y >= vga_text->res_y ) {
		scroll_screen( vga_text );
	}

	vga_text_set_cursor_on_hardware( vga_text );
}

void vga_text_set_background_color_at( vga_text_t *vga_text, const int x, const int y, vga_text_color_t color )
{
	int offset = calculate_offset( vga_text, x, y );

	uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;
	*(VIDEO_MEMORY+offset+1) = ( *(VIDEO_MEMORY+offset+1) & 0x0f ) | ( color << 4 );
}

vga_text_color_t vga_text_get_background_color_at( vga_text_t *vga_text, const int x, const int y )
{
	int offset = calculate_offset( vga_text, x, y );

	uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;
	uint8_t cell = *(VIDEO_MEMORY+offset+1);

	return cell & 0xf0;
}

void vga_text_inverse_colors_at( vga_text_t *vga_text, const int x, const int y )
{
	int offset = calculate_offset( vga_text, x, y );

	uint8_t *VIDEO_MEMORY = (uint8_t *)0xb8000;
	*(VIDEO_MEMORY+offset+1) = ( ( *(VIDEO_MEMORY+offset+1) & 0x0f ) << 4 )
		| ( ( *(VIDEO_MEMORY+offset+1) & 0xf0 ) >> 4 );
}

void vga_text_show_mouse_cursor( vga_text_t *vga_text )
{
	if( !vga_text->show_mouse_cursor ) {
		vga_text->show_mouse_cursor = true;
		vga_text_inverse_colors_at( vga_text, vga_text->mouse_cursor_x, vga_text->mouse_cursor_y );
	}
}

void vga_text_move_mouse_cursor( vga_text_t *vga_text, const int x, const int y )
{
	if( vga_text->show_mouse_cursor ) {
		vga_text_inverse_colors_at( vga_text, vga_text->mouse_cursor_x, vga_text->mouse_cursor_y );
	}
	
	vga_text->mouse_cursor_x = x;
	vga_text->mouse_cursor_y = y;
	
	if( vga_text->show_mouse_cursor ) {
		vga_text_inverse_colors_at( vga_text, vga_text->mouse_cursor_x, vga_text->mouse_cursor_y );
	}
}

void vga_text_hide_mouse_cursor( vga_text_t *vga_text )
{
	if( vga_text->show_mouse_cursor ) {
		vga_text_inverse_colors_at( vga_text, vga_text->mouse_cursor_x, vga_text->mouse_cursor_y );
		vga_text->show_mouse_cursor = false;
	}
}

void vga_text_save( vga_text_t *vga_text )
{
	uint8_t *VIDEO_MEMORY = (uint8_t *)0xA0000;

	memcpy( vga_text->buf, (const uint8_t *)VIDEO_MEMORY, 2*65535 );
}

void vga_text_restore( vga_text_t *vga_text )
{
	uint8_t *VIDEO_MEMORY = (uint8_t *)0xA0000;

	memcpy( (void *)VIDEO_MEMORY, (const uint8_t *)vga_text->buf, 2*65535 );
}