forked from arjun024/mkernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.c
114 lines (89 loc) · 2.1 KB
/
tty.c
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
/*
* Copyright (C) 2014 Ender Zheng
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
*/
#include "tty.h"
_START static void tty_copy_row(int src, int dst);
_START static void tty_clear_row(int row);
_STARTDATA static char *vidptr = (char*)0xb8000;
_START void tty_init(void)
{
int i = 0;
int j = 0;
for (i = 0; i < TTY_MAX_ROW; i++){
for (j = 0; j < TTY_MAX_COL; j++){
tty_setcolor(i, j, clWhite, clBlack);
tty_putchar(i, j, ' ');
}
}
}
_START void tty_setcolor(int x, int y, TTY_COLOR front, TTY_COLOR back)
{
int color_field = (back << 4) | (front);
int cur = ROW_COL_TO_CUR(x,y);
if (x < 0 || x >= TTY_MAX_ROW ||
y < 0 || y >= TTY_MAX_COL)
return;
vidptr[cur*2+1] = color_field;
}
_START TTY_COLOR tty_get_frontcolor(int x, int y)
{
int cur = ROW_COL_TO_CUR(x,y);
int color_field = 0;
if (x < 0 || x >= TTY_MAX_ROW ||
y < 0 || y >= TTY_MAX_COL)
return clBlack;
color_field = vidptr[cur*2+1];
return (color_field % 8);
}
_START TTY_COLOR tty_get_backcolor(int x, int y)
{
int cur = ROW_COL_TO_CUR(x,y);
int color_field = 0;
if (x < 0 || x >= TTY_MAX_ROW ||
y < 0 || y >= TTY_MAX_COL)
return clBlack;
color_field = vidptr[cur*2+1];
return (color_field >> 4);
}
_START void tty_putchar(int x, int y, char c)
{
int cur = ROW_COL_TO_CUR(x,y);
if (x < 0 || x >= TTY_MAX_ROW ||
y < 0 || y >= TTY_MAX_COL)
return;
vidptr[cur*2] = c;
}
_START char tty_getchar(int x, int y)
{
int cur = ROW_COL_TO_CUR(x,y);
if (x < 0 || x >= TTY_MAX_ROW ||
y < 0 || y >= TTY_MAX_COL)
return ' ';
return vidptr[cur*2];
}
_START void tty_roll_one_line()
{
int row = 0;
for (row = 0; row < (TTY_MAX_ROW-1); row++)
tty_copy_row(row+1, row);
tty_clear_row(TTY_MAX_ROW-1);
}
_START static void tty_copy_row(int src, int dst)
{
int col = 0;
for (col = 0; col < TTY_MAX_COL; col++)
tty_putchar(dst, col, tty_getchar(src, col));
}
_START static void tty_clear_row(int row)
{
int col = 0;
for (col = 0; col < TTY_MAX_COL; col++)
tty_putchar(row, col, ' ');
}
_START void tty_clear()
{
int row = 0;
for (row = 0; row < TTY_MAX_ROW; row++)
tty_clear_row(row);
}