blob: 9f4dc8faf0b44335f143c3a20fe02cb68c852992 (
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <arch.h>
#include <print.h>
#include "drivers/vga.h"
#include "term.h"
uint16_t buffer[TERM_W * TERM_H * sizeof(uint16_t)];
uint8_t x, y;
uint8_t color;
const uint16_t blank = (uint16_t) 0 | VGA_BLACK << 12 | VGA_WHITE << 8;
static void term_clear_line(int y) {
if (y < 0 || y >= TERM_H)
return;
for (uint8_t x = 0; x < TERM_W; x++) {
const size_t index = y * TERM_W + x;
buffer[index] = blank;
}
}
void term_init (void) {
x = 0;
y = 0;
term_setfg(VGA_WHITE);
term_setbg(VGA_BLACK);
term_clear();
}
void term_setpos(uint8_t xp, uint8_t yp) {
x = xp;
y = yp;
vgatext_cur_mov(x, y);
}
void term_scroll (int lines) {
arch_disable_int();
y -= lines;
if (!lines) return;
if(lines >= TERM_H || lines <= -TERM_H) {
term_clear();
} else if(lines > 0) {
memmove(buffer, buffer + lines * TERM_W, 2 * (TERM_H - lines) * TERM_W);
term_clear_line(TERM_H - lines);
} else {
memmove(buffer + lines * TERM_W, buffer + lines, (TERM_H + lines) * TERM_W);
}
arch_enable_int();
}
void term_setfg(enum vga_color c) {
color = (color & 0xF0) | c;
}
void term_setbg(enum vga_color c) {
color = (color & 0x0F) | c << 4;
}
void term_clear (void) {
for (uint8_t y = 0; y < TERM_H; y++)
term_clear_line(y);
}
uint32_t term_save(void) {
uint32_t state = 0;
state |= (uint32_t) x << 16;
state |= (uint32_t) y << 8;
state |= (uint32_t) color << 0;
return state;
}
void term_load(uint32_t state) {
x = (uint8_t) (state >> 16);
y = (uint8_t) (state >> 8);
color = (uint8_t) (state >> 0);
vgatext_cur_mov(x, y);
}
uint16_t term_save_col(void) {
return color;
}
void term_load_col(uint16_t c) {
color = c;
}
void putchar(int c) {
switch (c) {
case '\n':
x = 0;
y++;
break;
case '\t':
x += 4;
break;
case '\v':
case '\f':
y++;
break;
case '\r':
x = 0;
break;
default: {
const size_t index = y * TERM_W + x;
buffer[index] = c | (uint16_t) color << 8;
x++;
}
}
if (x >= TERM_W) {
x = 0;
y++;
}
if (y >= TERM_H) {
term_scroll(y - (TERM_H - 1));
y = TERM_H - 1;
}
vgatext_cur_mov(x, y);
}
bool term_newline(void) {
return x == 0;
}
void term_flush(void) {
arch_disable_int();
vgatext_write_buf(buffer);
arch_enable_int();
}
|