rudimentary console
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 13 Jun 2021 21:35:31 +0000 (00:35 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 13 Jun 2021 21:35:31 +0000 (00:35 +0300)
sys1/kern/src/con.c [new file with mode: 0644]
sys1/kern/src/con.h
sys1/kern/src/libc/stdio.c
sys1/kern/src/main.c

diff --git a/sys1/kern/src/con.c b/sys1/kern/src/con.c
new file mode 100644 (file)
index 0000000..b110272
--- /dev/null
@@ -0,0 +1,90 @@
+#include <string.h>
+#include "con.h"
+#include "vga.h"
+
+#define NCOLS                  80
+#define NROWS                  25
+
+/* must be pow2 */
+#define TEXTBUF_SIZE   256
+
+static char textbuf[TEXTBUF_SIZE][NCOLS];
+static int tbuf_first, tbuf_last;
+
+static int curx, cury, scroll;
+
+
+static void newline(void);
+
+
+void con_init(void)
+{
+       con_reset();
+}
+
+void con_reset(void)
+{
+       vga_reset();
+
+       curx = cury = scroll = 0;
+       tbuf_first = tbuf_last = 0;
+       memset(textbuf[0], 0, sizeof textbuf[0]);
+}
+
+void con_putchar(int c)
+{
+       switch(c) {
+       case '\t':
+               curx = (curx + 8) & 0xfffffff8;
+               if(curx >= NCOLS) {
+                       newline();
+               }
+               break;
+
+       case '\r':
+               curx = 0;
+               break;
+
+       case '\n':
+               newline();
+               break;
+
+       case '\b':
+               if(curx > 0) {
+                       textbuf[tbuf_last][--curx] = 0;
+                       vga_drawchar(curx, cury, 0);
+               }
+               break;
+
+       default:
+               textbuf[tbuf_last][curx] = c;
+               vga_drawchar(curx, cury, c);
+               if(++curx >= NCOLS) {
+                       newline();
+               }
+               break;
+       }
+
+       if(cury >= NROWS) {
+               cury--;
+               vga_scroll(++scroll);
+               vga_clearline(NROWS - 1);
+       }
+
+       vga_setcursor(curx, cury);
+}
+
+static void newline(void)
+{
+       int num;
+
+       curx = 0;
+       cury++;
+
+       num = (tbuf_last + 1) & (TEXTBUF_SIZE - 1);
+
+       if(tbuf_last == tbuf_first) {
+               tbuf_first = num;
+       }
+       tbuf_last = num;
+}
index 9eb776a..5beb167 100644 (file)
@@ -1,8 +1,13 @@
 #ifndef CON_H_
 #define CON_H_
 
+#include "vga.h"
+
 void con_init(void);
 
+void con_reset(void);
+#define con_color(fg, bg)      vga_setcolor(fg, bg)
+
 void con_putchar(int c);
 
 #endif /* CON_H_ */
index 2035cf8..3c2d61e 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include "con.h"
 
 enum {
        OUT_DEF,
@@ -13,7 +14,7 @@ static void bwrite(int out, char *buf, size_t buf_sz, char *str, int sz);
 
 int putchar(int c)
 {
-       /*con_putchar(c);*/
+       con_putchar(c);
        return c;
 }
 
index 1a84caf..e45e929 100644 (file)
@@ -1,47 +1,29 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include "con.h"
 #include "vga.h"
 #include "mem.h"
 #include "intr.h"
 
-void drawtext(int x, int y, const char *s)
-{
-       while(*s) {
-               vga_drawchar(x++, y, *s++);
-       }
-}
-
 void kmain(void)
 {
-       int i, row;
-       int line, scroll;
-       char buf[64];
-       char twirl[] = "-\\|/";
+       int i, line;
 
-       vga_reset();
+       con_init();
 
        mem_init();
        intr_init();
 
        line = 0;
        for(;;) {
-               scroll = line <= 24 ? 0 : line - 24;
-               row = line <= 24 ? line : 24;
-               sprintf(buf, "line %d  (scroll %d [%d])", line, scroll, scroll % 25);
-               vga_scroll(scroll);
-               if(scroll) {
-                       vga_clearline(24);
-               }
-               vga_setcolor(VGA_YELLOW | VGA_BRIGHT, VGA_BLACK);
-               drawtext(line & 7, row, buf);
+               printf("line %d", line++);
 
-               for(i=0; i<65536 * 16; i++) {
-                       buf[0] = twirl[(i >> 15) & 3];
-                       buf[1] = 0;
-                       vga_setcolor(VGA_WHITE, VGA_BLACK);
-                       drawtext(50, row, buf);
+               for(i=0; i<65536 * 128; i++) {
+                       if((i & 0x3ffff) == 0) {
+                               con_putchar('.');
+                       }
                }
-               line++;
+               con_putchar('\n');
        }
 }