|
|
@@ -0,0 +1,213 @@
|
|
|
+/* 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 <modules/tty.h>
|
|
|
+#include <kernel/vsprintf.h>
|
|
|
+#include <kernel/schk.h>
|
|
|
+
|
|
|
+#include <lib/string.h>
|
|
|
+#include <arch/io.h>
|
|
|
+
|
|
|
+#include <stdarg.h>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+static char buffer[256];
|
|
|
+
|
|
|
+
|
|
|
+static int syslog_index = 0;
|
|
|
+static __uint16_t *fb = FB_ADDR;
|
|
|
+static __uint8_t x, y;
|
|
|
+
|
|
|
+
|
|
|
+errno_t tty_init(void) {
|
|
|
+ x = bootarg->x, y = bootarg->y;
|
|
|
+
|
|
|
+ /* setup the frame buffer address fb */
|
|
|
+ /* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
|
|
|
+ fb += y * VGA_X + x;
|
|
|
+
|
|
|
+ /* well, this is the end */
|
|
|
+
|
|
|
+ return 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(__uint8_t color, 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(color, buffer);
|
|
|
+
|
|
|
+ memset(buffer, 0, 256);
|
|
|
+
|
|
|
+ va_end(args);
|
|
|
+
|
|
|
+ schk_leave();
|
|
|
+
|
|
|
+ return i;
|
|
|
+}
|
|
|
+
|
|
|
+void print(__uint8_t color, char *s) {
|
|
|
+ while (*s)
|
|
|
+ putc(*s++, color);
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void putc(char c, __uint8_t color) {
|
|
|
+ if (c == '\n') {
|
|
|
+ /* we do it like \r\n */
|
|
|
+ x = 0;
|
|
|
+
|
|
|
+ if (++y == VGA_Y) {
|
|
|
+ roll_screen();
|
|
|
+ y = 24;
|
|
|
+ }
|
|
|
+
|
|
|
+ update_cursor();
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (c == '\r') {
|
|
|
+ x = 0;
|
|
|
+ update_cursor();
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ else if (c == '\t') {
|
|
|
+ x = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
|
|
|
+ update_cursor();
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ __asm__ __volatile__ (
|
|
|
+ /* rdi is the frame buffer, and rax is the */
|
|
|
+ "cld\n\t"
|
|
|
+ "movb %%cl, %%ah\n\t"
|
|
|
+ "stosw\n\t"
|
|
|
+
|
|
|
+ : "+D" (fb)
|
|
|
+ : "a" (c), "c" (color)
|
|
|
+ );
|
|
|
+
|
|
|
+ if (++x == VGA_X) {
|
|
|
+ x = 0;
|
|
|
+
|
|
|
+ if (++y == VGA_Y) {
|
|
|
+ roll_screen();
|
|
|
+ y = 24;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ update_cursor();
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+__uint16_t read_cursor(void) {
|
|
|
+ __uint16_t pos = 0;
|
|
|
+
|
|
|
+ outb(CMD_POS_LO, CURSOR_INDEX);
|
|
|
+ pos |= inb(CURSOR_DATA);
|
|
|
+
|
|
|
+ outb(CMD_POS_HI, CURSOR_INDEX);
|
|
|
+ pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
|
|
|
+
|
|
|
+ return pos;
|
|
|
+}
|
|
|
+
|
|
|
+void update_cursor(void) {
|
|
|
+ __uint16_t pos = y * bootarg->vga_x + x;
|
|
|
+ fb = FB_ADDR + pos;
|
|
|
+
|
|
|
+ outb(CMD_POS_LO, CURSOR_INDEX);
|
|
|
+ outb(pos & 0xff, CURSOR_DATA);
|
|
|
+
|
|
|
+ outb(CMD_POS_HI, CURSOR_INDEX);
|
|
|
+ outb((pos >> 8) & 0xff, CURSOR_DATA);
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void roll_screen(void) {
|
|
|
+ /* roll down the screen */
|
|
|
+
|
|
|
+ __asm__ __volatile__ (
|
|
|
+ "cld\n\t"
|
|
|
+ "movq $960, %%rcx\n\t"
|
|
|
+ "movq $0xb80a0, %%rsi\n\t"
|
|
|
+ "movq $0xb8000, %%rdi\n\t"
|
|
|
+ "rep\n\t"
|
|
|
+ "movsq\n\t"
|
|
|
+
|
|
|
+ "movq $0xb8f00, %%rdi\n\t"
|
|
|
+ "movq $0x0720, %%rax\n\t"
|
|
|
+ "movq $80, %%rcx\n\t"
|
|
|
+ "rep\n\t"
|
|
|
+ "stosw\n\t"
|
|
|
+ :::
|
|
|
+ );
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|