kern

view src/vid.c @ 2:86781ef20689

added hardware scrolling, memset16 and temporary keyboard input for testing
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 04 Dec 2010 08:04:43 +0200
parents ebe5e0e44a9d
children 0489a34ab348
line source
1 #if 0
3 #include <string.h>
4 #include "vid.h"
5 #include "asmops.h"
7 /* height of our virtual console text buffer */
8 #define VIRT_HEIGHT 1024
10 /* CRTC ports */
11 #define CRTC_ADDR 0x3d4
12 #define CRTC_DATA 0x3d5
14 /* CRTC registers */
15 #define CRTC_START_HIGH 0xc
16 #define CRTC_START_LOW 0xd
17 #define CRTC_CURSOR_HIGH 0xe
18 #define CRTC_CURSOR_LOW 0xf
20 /* construct a character with its attributes */
21 #define VMEM_CHAR(c, fg, bg) \
22 ((uint16_t)(c) | (((uint16_t)(fg) & 0xf) << 8) | (((uint16_t)(bg) & 0xf) << 12))
24 static void set_start_line(int line);
26 static uint16_t *vmem = (uint16_t*)0xb8000;
27 static int start_line;
30 void clear_scr(void)
31 {
32 memset16(vmem, VMEM_CHAR(' ', LTGRAY, BLACK), WIDTH * HEIGHT);
33 start_line = 0;
34 set_start_line(0);
35 set_cursor(0, 0);
36 }
38 void set_char(char c, int x, int y, int fg, int bg)
39 {
40 vmem[(y + start_line) * WIDTH + x] = VMEM_CHAR(c, fg, bg);
41 }
43 void set_cursor(int x, int y)
44 {
45 int loc;
47 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
48 loc = 0xffff;
49 } else {
50 loc = (y + start_line) * WIDTH + x;
51 }
53 outb(CRTC_CURSOR_LOW, CRTC_ADDR);
54 outb(loc, CRTC_DATA);
55 outb(CRTC_CURSOR_HIGH, CRTC_ADDR);
56 outb(loc >> 8, CRTC_DATA);
57 }
59 void scroll_scr(void)
60 {
61 if(++start_line > VIRT_HEIGHT - HEIGHT) {
62 /* The bottom of the visible range reached the end of our text buffer.
63 * Copy the rest of the lines to the top and reset start_line.
64 */
65 memcpy(vmem, vmem + start_line * WIDTH, (HEIGHT - 1) * WIDTH);
66 start_line = 0;
67 }
69 /* clear the next line that will be revealed by scrolling */
70 int new_line = start_line + HEIGHT - 1;
71 memset16(vmem + new_line * WIDTH, VMEM_CHAR(' ', LTGRAY, BLACK), WIDTH);
73 set_start_line(start_line);
74 }
76 static void set_start_line(int line)
77 {
78 int start_addr = line * WIDTH;
80 outb(CRTC_START_LOW, CRTC_ADDR);
81 outb(start_addr & 0xff, CRTC_DATA);
82 outb(CRTC_START_HIGH, CRTC_ADDR);
83 outb((start_addr >> 8) & 0xff, CRTC_DATA);
84 }
85 #endif /* 0 */
87 #include <string.h>
88 #include "vid.h"
89 #include "asmops.h"
91 #define WIDTH 80
92 #define HEIGHT 25
94 /* CRTC ports */
95 #define CRTC_ADDR 0x3d4
96 #define CRTC_DATA 0x3d5
98 /* CRTC registers */
99 #define CRTC_CURSOR_HIGH 0xe
100 #define CRTC_CURSOR_LOW 0xf
102 /* construct a character with its attributes */
103 #define VMEM_CHAR(c, fg, bg) \
104 ((uint16_t)(c) | (((uint16_t)(fg) & 0xf) << 8) | \
105 (((uint16_t)(bg) & 0xf) << 12))
107 #define CLEAR_CHAR VMEM_CHAR(' ', LTGRAY, BLACK)
109 /* pointer to the text mode video memory */
110 static uint16_t *vmem = (uint16_t*)0xb8000;
112 void clear_scr(void)
113 {
114 memset16(vmem, CLEAR_CHAR, WIDTH * HEIGHT);
115 }
117 void set_char(char c, int x, int y, int fg, int bg)
118 {
119 vmem[y * WIDTH + x] = VMEM_CHAR(c, fg, bg);
120 }
122 void set_cursor(int x, int y)
123 {
124 int loc;
125 if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
126 loc = 0xffff;
127 } else {
128 loc = y * WIDTH + x;
129 }
131 /* tell the vga where we want the cursor by writing
132 * to the "cursor address" register of the CRTC */
133 outb(CRTC_CURSOR_LOW, CRTC_ADDR);
134 outb(loc, CRTC_DATA);
135 outb(CRTC_CURSOR_HIGH, CRTC_ADDR);
136 outb(loc >> 8, CRTC_DATA);
137 }
139 void scroll_scr(void)
140 {
141 /* simple scrolling by manually copying memory */
142 memmove(vmem, vmem + WIDTH, WIDTH * (HEIGHT - 1) * 2);
143 memset16(vmem + WIDTH * (HEIGHT - 1), CLEAR_CHAR, WIDTH);
144 }