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