kernel: +output stack on exception; +stacktrace: gets exception data via COM or 0xE9...
[meinos.git] / kernel2 / vga.c
blob57d90ec97ae7dd9bbd6251692e9168615ba33a71
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <string.h>
20 #include <ctype.h>
21 #include <ioport.h>
22 #include <vga.h>
24 /**
25 * Initializes VGA
27 void vga_init() {
28 com_init();
29 vga_text_video = (vga_text_char_t*)VGA_TEXT_ADDRESS;
30 vga_text_set_hwcursor(0,VGA_TEXT_HEIGHT+1);
31 vga_text_clear();
34 /**
35 * Clears VGA text screen
37 void vga_text_clear() {
38 vga_text_cursor.x = 0;
39 vga_text_cursor.y = 0;
40 vga_text_cursor.color = VGA_TEXT_COLOR_DEFAULT;
41 // trick: just set whole screen with bg color as bg and front color
42 memset(vga_text_video,(vga_text_cursor.color&0xF0)|(vga_text_cursor.color>>4),VGA_TEXT_SIZE);
45 /**
46 * Moves VGA text hardware cursor
47 * @param x New X position
48 * @param y New Y position
50 void vga_text_set_hwcursor(int x,int y) {
51 int off = y*VGA_TEXT_WIDTH+x;
53 outb(0x3D4,14);
54 outb(0x3D5,off>>8);
55 outb(0x3D4,15);
56 outb(0x3D5,off);
59 /**
60 * Set color of next chars
61 * @param color Color
63 void vga_text_setcolor(vga_text_color_t color) {
64 vga_text_cursor.color = color;
67 /**
68 * Prints character to VGA text screen
70 void vga_text_printchar(char chr) {
71 if (chr=='\b') vga_text_cursor.x = vga_text_cursor.x>0?vga_text_cursor.x-1:vga_text_cursor.x;
72 else if (chr=='\r') vga_text_cursor.x = 0;
73 else if (chr=='\n') {
74 vga_text_cursor.y++;
75 vga_text_cursor.x = 0;
77 else if (chr=='\f') vga_text_clear();
78 else if (chr=='\t') vga_text_cursor.x = vga_text_cursor.x = vga_text_cursor.x>0?(vga_text_cursor.x/VGA_TEXT_TABSIZE+1)*VGA_TEXT_TABSIZE:0;
79 else if (isprint(chr)) {
80 int off = vga_text_cursor.x+vga_text_cursor.y*VGA_TEXT_WIDTH;
81 if (off==0x3FD) asm("hlt"::"a"(0xD00FC0DE));
82 vga_text_video[off].chr = chr;
83 vga_text_video[off].attr = vga_text_cursor.color;
84 vga_text_cursor.x++;
87 com_send(chr);
88 bochs_send(chr);
90 if (vga_text_cursor.x>=VGA_TEXT_WIDTH) {
91 vga_text_cursor.x = 0;
92 vga_text_cursor.y++;
94 if (vga_text_cursor.y>=VGA_TEXT_HEIGHT) {
95 memmove(vga_text_video,vga_text_video+VGA_TEXT_WIDTH,VGA_TEXT_WIDTH*(VGA_TEXT_HEIGHT-1)*VGA_TEXT_BPC);
96 memset(vga_text_video+VGA_TEXT_WIDTH*(VGA_TEXT_HEIGHT-1),(vga_text_cursor.color&0xF0)|(vga_text_cursor.color>>4),VGA_TEXT_WIDTH*VGA_TEXT_BPC);
97 vga_text_cursor.y = VGA_TEXT_HEIGHT-1;
101 /// @todo remove
102 #define COM_PORT 0x3f8 /* COM1 */
103 #define BAUDRATE 9600
104 void com_init() {
105 int baud = 115200/BAUDRATE;
106 // Keine Parität
107 outb(COM_PORT+1,0x00); // no interrupts
108 outb(COM_PORT+3,0x80); // dlab
109 outb(COM_PORT+0,baud); // Baudrate (low)
110 outb(COM_PORT+1,baud>>8); // Baudrate (high)
111 outb(COM_PORT+3,0x03); // 8 Bits, 1 stopbit
112 outb(COM_PORT+2,0xC7);
113 outb(COM_PORT+4,0x0B);
115 int is_transmit_empty() {
116 return inb(COM_PORT+5)&0x20;
118 void com_send(char a) {
119 while (!is_transmit_empty());
120 outb(COM_PORT,a);
123 #define BOCHS_PORT 0xE9
124 void bochs_send(char a) {
125 outb(BOCHS_PORT,a);