]> git.dujemihanovic.xyz Git - nameless-os.git/blob - kernel/arch/x86/tty/tty.c
Implement input part of stdio
[nameless-os.git] / kernel / arch / x86 / tty / tty.c
1 #include <io.h>
2 #include <tty.h>
3 #include <stdint.h>
4 #include <input/ps2.h>
5
6 #define VGA_WIDTH 80
7 #define VGA_HEIGHT 25
8
9 volatile char *video_memory = (char *) 0xB8000; /* VGA VRAM starts at 0xB8000 */
10
11 static int cursor_x = 0; /* keep track of where cursor is */
12 static int cursor_y = 0;
13
14 char *hex_chars = "0123456789ABCDEF";
15 char kgets_buffer[100];
16
17 void screen_clear(void)
18 {
19 int x, y;
20 for ( y = 0; y < VGA_HEIGHT; y++ ) {
21 for ( x = 0; x < VGA_WIDTH; x++ ) {
22 video_memory[(y * VGA_WIDTH + x) * 2 + 1] = VGA_COLOR_LIGHT_GRAY;
23 video_memory[(y * VGA_WIDTH + x) * 2] = ' ';
24 }
25 }
26
27 cursor_x = 0;
28 cursor_y = 0;
29 }
30
31 void scroll_up(void)
32 {
33 int x, y;
34 for ( y = 1; y < VGA_HEIGHT; y++ ) {
35 for ( x = 0; x < VGA_WIDTH; x++ ) {
36 video_memory[((y - 1) * VGA_WIDTH + x) * 2] = video_memory[(y * VGA_WIDTH + x) * 2];
37 }
38 }
39 for ( x = 0; x < VGA_WIDTH; x++ ) {
40 video_memory[((VGA_HEIGHT - 1) * VGA_WIDTH + x) * 2] = ' ';
41 }
42 cursor_y = VGA_HEIGHT - 1;
43 }
44
45 void kprint(const char *string, uint8_t color)
46 {
47 char next_char;
48 uint8_t vga_misc_output;
49 uint16_t crtc_port;
50 next_char = *string;
51
52 while ( next_char != 0 ) {
53 if ( next_char == '\n') { cursor_x = 0; cursor_y++; }
54 else { video_memory[(cursor_y * VGA_WIDTH + cursor_x) * 2] = next_char; video_memory[((cursor_y * VGA_WIDTH + cursor_x++) * 2)+ 1] = color != 0 ? color : VGA_COLOR_LIGHT_GRAY; }
55 if ( cursor_x >= VGA_WIDTH ) { cursor_x = 0; cursor_y++; }
56 if ( cursor_y >= VGA_HEIGHT ) { scroll_up(); }
57 next_char = *++string;
58 }
59
60 vga_misc_output = inb(0x3CC);
61 if (vga_misc_output & 0x1 == 0) {
62 crtc_port = 0x3B4;
63 } else {
64 crtc_port = 0x3D4;
65 }
66
67 outb(crtc_port, 0xE);
68 outb(crtc_port + 1, (cursor_y * VGA_WIDTH + cursor_x) >> 8);
69 outb(crtc_port, 0xF);
70 outb(crtc_port + 1, (cursor_y * VGA_WIDTH + cursor_x) & 0xFF);
71 }
72
73 void kprintc(const char character, uint8_t color)
74 {
75 uint8_t vga_misc_output;
76 uint16_t crtc_port;
77
78 if ( character == '\n') { cursor_x = 0; cursor_y++; }
79 if (character == 0x7f) {
80 cursor_x--;
81 if (cursor_x < 0) {
82 if (cursor_y > 0) {
83 cursor_x = VGA_WIDTH - 1;
84 cursor_y--;
85 } else cursor_x = 0;
86 }
87 video_memory[(cursor_y * VGA_WIDTH + cursor_x) * 2] = ' ';
88 }
89 else { video_memory[(cursor_y * VGA_WIDTH + cursor_x) * 2] = character; video_memory[((cursor_y * VGA_WIDTH + cursor_x++) * 2)+ 1] = color != 0 ? color : VGA_COLOR_LIGHT_GRAY; }
90 if ( cursor_x >= VGA_WIDTH ) { cursor_x = 0; cursor_y++; }
91 if ( cursor_y >= VGA_HEIGHT ) { scroll_up(); }
92
93 vga_misc_output = inb(0x3CC);
94 if (vga_misc_output & 0x1 == 0) {
95 crtc_port = 0x3B4;
96 } else {
97 crtc_port = 0x3D4;
98 }
99
100 outb(crtc_port, 0xE);
101 outb(crtc_port + 1, (cursor_y * VGA_WIDTH + cursor_x) >> 8);
102 outb(crtc_port, 0xF);
103 outb(crtc_port + 1, (cursor_y * VGA_WIDTH + cursor_x) & 0xFF);
104
105 }
106
107 void kprintb(uint8_t byte)
108 {
109 uint8_t temp;
110 temp = byte >> 4;
111 kprint("0x", VGA_COLOR_LIGHT_GRAY);
112 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
113 temp = byte & 0xF;
114 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
115 }
116
117 void kprintw(uint16_t word)
118 {
119 uint8_t temp;
120 temp = word >> 12;
121 kprint("0x", VGA_COLOR_LIGHT_GRAY);
122 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
123 temp = (word >> 8) & 0xF;
124 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
125 temp = (word >> 4) & 0xF;
126 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
127 temp = word & 0xF;
128 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
129 }
130
131 void kprintd(uint32_t dword)
132 {
133 uint8_t temp;
134 temp = dword >> 28;
135 kprint("0x", VGA_COLOR_LIGHT_GRAY);
136 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
137 temp = (dword >> 24) & 0xF;
138 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
139 temp = (dword >> 20) & 0xF;
140 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
141 temp = (dword >> 16) & 0xF;
142 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
143 temp = (dword >> 12) & 0xF;
144 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
145 temp = (dword >> 8) & 0xF;
146 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
147 temp = (dword >> 4) & 0xF;
148 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
149 temp = dword & 0xF;
150 kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY);
151 }
152
153 char *kgets()
154 {
155 for (int i=0; i<100; i++) {
156 kgets_buffer[i] = '\0';
157 }
158
159 int i=0;
160 while (i<100) {
161 char temp = ps2_get_keystroke();
162 if (temp == '\n') break;
163 if (temp == 0x7f) {
164 if (i>0) {
165 i-=1;
166 kgets_buffer[i] = '\0';
167 kprintc(temp, 0);
168 }
169 continue;
170 }
171 kgets_buffer[i] = temp;
172 kprintc(temp, 0);
173 i++;
174 }
175
176 return kgets_buffer;
177 }