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

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

/* The video buffer, should be replaced by symbol in ldscript.ld */
static char *vidmem;

int vga_line, vga_col;

int vga_inited = 0; // it will be changed in pci_rom.c

static int vga_console_inited = 0;

#define VIDBUFFER 0xB8000;

static void memsetw(void *s, int c, unsigned int n)
{
	int i;
	 u16 *ss = (u16 *) s;

	for (i = 0; i < n; i++) {
		ss[i] = ( u16 ) c;
	}
}

static void vga_init(void)
{
	// these are globals
	vga_line = 0;
	vga_col = 0;
	vidmem = (char *) VIDBUFFER;
	
	// mainboard or chip specific init routines
	// also loads font
	vga_hardware_fixup();
	
	// set attributes, char for entire screen
	// font should be previously loaded in 
	// device specific code (vga_hardware_fixup)
	 memsetw(vidmem, VGA_ATTR_CLR_WHT, 2*1024); //
}

static void vga_scroll(void)
{
	int i;

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

static void vga_tx_byte(unsigned char byte)
{
	if (!vga_inited) {
		return;
	}
 
	if(!vga_console_inited) {
		vga_init();
		vga_console_inited = 1;
	}

	if (byte == '\n') {
		vga_line++;
		vga_col = 0;

	} else if (byte == '\r') {
		vga_col = 0;

	} else if (byte == '\b') {
		vga_col--;

	} else if (byte == '\t') {
		vga_col += 4;

	} else if (byte == '\a') {
		//beep
//		beep(500);
		;
	} else {
		vidmem[((vga_col + (vga_line *COLS)) * 2)] = byte;
		vidmem[((vga_col + (vga_line *COLS)) * 2) +1] = VGA_ATTR_CLR_WHT;
		vga_col++;
	}
	if (vga_col < 0) {
		vga_col = 0;
	}
	if (vga_col >= COLS) {
		vga_line++;
		vga_col = 0;
	}
	if (vga_line >= LINES) {
		vga_scroll();
		vga_line--;
	}
	// move the cursor
	write_crtc((vga_col + (vga_line *COLS)) >> 8, CRTC_CURSOR_HI);
	write_crtc((vga_col + (vga_line *COLS)) & 0x0ff, CRTC_CURSOR_LO);
}

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