vga text output and libc expansion
[3sys] / sys1 / kern / src / vga.c
1 #include <string.h>
2 #include "vga.h"
3 #include "asmutil.h"
4
5 #define CRTC_ADDR_PORT  0x3d4
6 #define CRTC_DATA_PORT  0x3d5
7
8 /* CRTC registers */
9 #define CRTC_START_H    0x0c
10 #define CRTC_START_L    0x0d
11 #define CRTC_CURPOS_H   0x0e
12 #define CRTC_CURPOS_L   0x0f
13
14 static void crtc_write(int reg, unsigned char val);
15
16 static uint16_t attr = 0x0700;
17 static int yoffs;
18
19 void vga_setcolor(int fg, int bg)
20 {
21         attr = ((bg & 7) << 12) | ((fg & 0xf) << 8);
22 }
23
24 void vga_setcursor(int x, int y)
25 {
26         int loc = (y + yoffs) * 80 + x;
27         crtc_write(CRTC_CURPOS_H, loc >> 8);
28         crtc_write(CRTC_CURPOS_L, loc);
29 }
30
31 void vga_setstart(int start)
32 {
33         yoffs = start;
34         crtc_write(CRTC_START_H, start >> 8);
35         crtc_write(CRTC_START_L, start);
36 }
37
38 void vga_reset(void)
39 {
40         vga_setcolor(VGA_WHITE, VGA_BLACK);
41         vga_setstart(0);
42         vga_setcursor(0, 0);
43         memset((void*)0xb8000, 0, 80 * 25 * 2);
44 }
45
46 void vga_drawchar(int x, int y, int c)
47 {
48         uint16_t *ptr = (uint16_t*)0xb8000 + (y + yoffs) * 80 + x;
49         *ptr = (c & 0xff) | attr;
50 }
51
52 static void crtc_write(int reg, unsigned char val)
53 {
54         outp(CRTC_ADDR_PORT, reg);
55         outp(CRTC_DATA_PORT, val);
56 }