/* 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 .
*/
#include
#include
#include
#include
#include
#include
#include
#include
static char buffer[256];
static int syslog_index = 0;
void syslog(const char *s) {
/* we can't use schk in this function, */
/* because this will make a die-loop (schk_init calls this function) */
int i;
for (i = 0; s[i]; i++) {
if (syslog_index + i == 32768)
syslog_index = 0;
*(syslog_index + SYSLOG_BUFFER) = s[i];
syslog_index++;
}
return;
}
int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
schk_init();
va_list args;
int i;
/* I think 256 is enough in kernel */
/* too long, return it. */
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);
memset(buffer, 0, 256);
va_end(args);
schk_leave();
return i;
}
static int x = 0, y = 0;
void putc(char c, __uint32_t fg, __uint32_t bg) {
schk_init();
if (c == '\n') {
/* it's '\n', but we use '\r\n' */
if (y + CHAR_HEIGHT > VGA_Y)
y = 0;
else
y += 16;
x = 0;
/* and clear next line */
return;
}
if (c == '\r') {
x = 0;
return;
}
if (c == '\t') {
/* align to next tab point */
int spaces = ((x + TAB_SIZE - 1) & (~(TAB_SIZE - 1))) - x;
/* this is not good: maybe uses too many stack */
for (; spaces; spaces--)
putc(' ', fg, bg);
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;
/* need next line? */
if (x >= VGA_X) {
x = 0;
y += CHAR_HEIGHT;
}
/* no enough space for this character */
/* VGA_Y % CHAR_HEIGHT != 0, so we cannot use y == VGA_Y */
if (y + CHAR_HEIGHT > VGA_Y)
y = 0;
/* 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;
schk_leave();
return;
}
void print(__uint32_t fg, __uint32_t bg, char *s) {
schk_init();
for (; *s; s++) {
if (*s == '\n')
putc(' ', fg, bg);
putc(*s, fg, bg);
}
schk_leave();
return;
}