WKern
Loading...
Searching...
No Matches
printer.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <global.h>
20#include <io/kio.h>
21#include <types/nums.h>
22#include <types/vargs.h>
28void Kcfp() {
29 for (int i = 0; i < 80 * 25; i++) {
30 vmem[i * 2] = ' ';
31 vmem[(i * 2) + 1] = 0x0F;
32 }
33 col = 0;
34 row = 0;
35}
36
46void Kputchar(char c) {
47 if (c == '\n') {
48 col = 0;
49 row++;
50 } else {
51 int index = (row * 80 + col) * 2;
52 vmem[index] = c;
53 vmem[index + 1] = 0x07;
54 col++;
55 if (col >= 80) {
56 col = 0;
57 row++;
58 }
59 }
60
61 if (row >= 25) {
62 Kcfp();
63 row = 0;
64 }
65}
66
72 if (col > 0) {
73 col--;
74 } else if (row > 0) {
75 row--;
76 col = 79;
77 }
78
79 int offset = (row * 80) + col;
80 vmem[offset * 2] = ' '; // clear the character
81 vmem[(offset * 2) + 1] = 0x07; // normal attribute
82}
83
92void KprintHex(u32 num) {
93 char buf[11] = "0x00000000";
94 const char *hex = "0123456789ABCDEF";
95 for (int i = 0; i < 8; i++) {
96 buf[9 - i] = hex[(num >> (i * 4)) & 0xF];
97 }
98 Kprintf(buf);
99}
100
108void KprintDec(int num) {
109 char buf[12]; // enough for -2^31
110 int i = 0;
111 if (num == 0) {
112 Kputchar('0');
113 return;
114 }
115 if (num < 0) {
116 Kputchar('-');
117 num = -num;
118 }
119 while (num > 0) {
120 buf[i++] = '0' + (num % 10);
121 num /= 10;
122 }
123 while (i--) {
124 Kputchar(buf[i]);
125 }
126}
127
133void KprintStr(const char *str) {
134 while (*str) {
135 Kputchar(*str++);
136 }
137}
138
152void Kprintf(const char *fmt, ...) {
153 va_list args;
154 va_start(args, fmt);
155
156 while (*fmt) {
157 if (*fmt == '%') {
158 fmt++;
159 switch (*fmt) {
160 case 'c': {
161 char pchar = (char)va_arg(args, int);
162 Kputchar(pchar);
163 break;
164 }
165 case 's': {
166 const char *pstr = va_arg(args, const char *);
167 KprintStr(pstr);
168 break;
169 }
170 case 'd': {
171 int num = va_arg(args, int);
172 KprintDec(num);
173 break;
174 }
175 case 'x': {
176 u32 hex = va_arg(args, u32);
177 KprintHex(hex);
178 break;
179 }
180 case '%': {
181 Kputchar('%');
182 break;
183 }
184 default:
185 Kputchar('%');
186 Kputchar(*fmt);
187 break;
188 }
189 } else {
190 Kputchar(*fmt);
191 }
192 fmt++;
193 }
194
195 va_end(args);
196}
int row
Definition main.c:32
int col
Definition main.c:33
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
void KprintDec(int num)
Prints a signed integer in decimal format.
Definition printer.c:108
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
void KprintStr(const char *str)
Prints a null-terminated string to the screen.
Definition printer.c:133
void KputcharBackspace()
Handles backspace keypress by moving the cursor back and clearing the character.
Definition printer.c:71
void Kcfp()
Clears the screen by filling the video memory with spaces.
Definition printer.c:28
void KprintHex(u32 num)
Prints a 32-bit unsigned integer in hexadecimal format prefixed with "0x".
Definition printer.c:92
void Kputchar(char c)
Outputs a single character to the screen at the current cursor position.
Definition printer.c:46
#define va_start(ap, last_arg)
Definition vargs.h:22
#define va_arg(ap, type)
Definition vargs.h:23
#define va_end(ap)
Definition vargs.h:24
__builtin_va_list va_list
Definition vargs.h:20