| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /* Copyright (C) 2021 Rain */
- /* This file is part of XNIX. */
- /*
- XNIX is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) and later version.
- */
- /*
- XNIX is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- */
- /*
- You should have received a copy of the GNU General Public License
- along with XNIX. If not, see <https://www.gnu.org/licenses/>.
- */
- #include <kernel/init.h>
- #include <kernel/types.h>
- #include <kernel/errno.h>
- #include <kernel/graphic.h>
- #include <kernel/vsprintf.h>
- #include <stdarg.h>
- int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
- va_list args;
- int i;
- /* I think 256 is enough in kernel */
- /* too long, return it. */
- if (s[255])
- return 0;
- static char buffer[256] = {0};
- va_start(args, s);
- /* it maybe overflow, but that's in .data section and we did check. */
- i = vsprintf(buffer, s, args);
- print(fg, bg, buffer);
- va_end(args);
- return i;
- }
- static int x = 0, y = 0;
- void putc(char c, __uint32_t fg, __uint32_t bg) {
- if (c == '\n') {
- /* it's '\n', but we use '\r\n' */
- x = 0;
- y += 16;
- return;
- }
- if (c == '\r') {
- x = 0;
- return;
- }
- if (c == ' ') {
- x += 8;
- return;
- }
- struct psf_header *psf = (struct psf_header *) &font_start;
- /*
- * yeah, it's very ugly. but it just get address of this glyph, and check
- * 'c' is really in this font.
- * */
- __uint8_t *glyph = (__uint8_t *) &font_start + psf->header_size +
- (c > 0 && c < psf->glyphs_count ? c : 0) * psf->glyph_size;
- /* okay, now we put it on the screen */
- int i;
- int mask;
- char d;
- for (i = 0; i < 16; i++) {
- __uint32_t *addr;
- int j;
- addr = FB_ADDR + VGA_X * (y + i) + x;
- mask = 0x0100;
- d = glyph[i];
- for (j = 0; j < 8; j++) {
- mask >>= 1;
- if (d & mask)
- addr[j] = fg;
-
- else
- addr[j] = bg;
- }
- }
- x += 8;
- if (x >= VGA_X) {
- x = 0;
- y += 16;
- /* but y will > Y_LINES */
- }
- return;
- }
- void print(__uint32_t fg, __uint32_t bg, char *s) {
- for (; *s; s++)
- putc(*s, fg, bg);
- return;
- }
|