summaryrefslogtreecommitdiff
path: root/src/console/vga_console.c
blob: 0a749f653cce47f61926090b48febd919d1a806f (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
/*
 *
 * modified from original freebios code
 * by Steve M. Gehlbach <steve@kesa.com>
 *
 */

/*
 * TODO:
 * * make vga_console_init take FB location, columns, lines and starting
 *   column/line.
 * * track a word offset, and not columns/lines. The offset is needed more
 *   often than columns/lines and the latter two can be calculated easily.
 * * then implement real vga scrolling, instead of memcpying stuff around.
 *
 * -- libv.
 */

#include <arch/io.h>
#include <string.h>
#include <pc80/vga_io.h>
#include <pc80/vga.h>
#include <console/console.h>

/* The video buffer, should be replaced by symbol in ldscript.ld */
static char *vidmem;
static int total_lines, total_columns;
static int current_line, current_column;
static int vga_console_inited = 0;

/*
 *
 */
void vga_console_init(void)
{
	vidmem = (char *) VGA_FB;
	total_columns = VGA_COLUMNS;
	total_lines = VGA_LINES;
	current_column = 0;
	current_line = 0;

	vga_console_inited = 1;
}

static void vga_scroll(void)
{
	int i;

	memcpy(vidmem, vidmem + total_columns * 2, (total_lines - 1) * total_columns * 2);
	for (i = (total_lines - 1) * total_columns * 2; i < total_lines * total_columns * 2; i += 2)
		vidmem[i] = ' ';
}

static void
vga_tx_byte(unsigned char byte)
{
	if (!vga_console_inited)
	    return;

	switch (byte) {
	case '\n':
		current_line++;
		current_column = 0;
		break;
	case '\r':
		current_column = 0;
		break;
	case '\b':
		current_column--;
		break;
	case '\t':
		current_column += 4;
		break;
	case '\a': /* beep */
		break;
	default:
		vidmem[((current_column + (current_line * total_columns)) * 2)] = byte;
		vidmem[((current_column + (current_line * total_columns)) * 2) +1] = 0x07;
		current_column++;
		break;
	}

	if (current_column < 0)
		current_column = 0;
	if (current_column >= total_columns) {
		current_line++;
		current_column = 0;
	}
	if (current_line >= total_lines) {
		vga_scroll();
		current_line--;
	}

	/* move the cursor */
	vga_cr_write(0x0E, (current_column + (current_line * total_columns)) >> 8);
	vga_cr_write(0x0F, (current_column + (current_line * total_columns)) & 0x0ff);
}

static const struct console_driver vga_console __console ={
	.init    = 0,
	.tx_byte = vga_tx_byte,
	.rx_byte = 0,
	.tst_byte = 0,
};