wm: teppich

Download patch

ref: df5c5446e62224cd36e435f312d1dae181326d51
parent: 4c1d32b6aac406b43dcb63541117ebddb0d1c42c
author: mkf <mkf@cloud9p.org>
date: Sun Nov 19 03:24:18 EST 2023

add a simple abstraction for console, few fixes in vga and ps/2 functions

pc/ps2.c: make ps2_getc() blocking
pc/vga.c: vga_init() no longer takes colors as input
pc/cons.c: import
include/cons.h: import
include/pccons.h: a console for ps2 and vga

--- /dev/null
+++ b/include/cons.h
@@ -1,0 +1,17 @@
+#pragma once
+typedef struct
+{
+	char *name;
+	
+	void (*init)(void);
+	void (*clear)(char);
+	void (*puts)(char *s);
+	void (*putc)(char c);
+	char (*getc)(void);
+}consdev_t;
+
+void consinit(consdev_t);
+void consclear(consdev_t, char);
+void conswrite(consdev_t cons, char* s);
+void consputc(consdev_t, char c);
+char consread(consdev_t cons);
--- /dev/null
+++ b/include/pccons.h
@@ -1,0 +1,15 @@
+#pragma once
+#include <cons.h>
+#include <vga.h>
+#include <ps2.h>
+
+consdev_t pccons = 
+{
+	.name = "cons",
+	.init = vga_init,
+	.clear = vga_clear,
+	.puts = vga_puts,
+	.putc = vga_putc,
+	.getc = ps2_getc,
+	
+};
--- a/include/vga.h
+++ b/include/vga.h
@@ -1,12 +1,14 @@
 #pragma once
 #include <u.h>
 
-static const size_t VGA_WIDTH = 80; /* x */
-static const size_t VGA_HEIGHT = 25; /* y */
+#define VGA_MEM 0xB8000
 
-size_t vga_row;
-size_t vga_col;
+static const uint8 VGA_WIDTH = 80; /* x */
+static const uint8 VGA_HEIGHT = 25; /* y */
 
+uint8 vga_row;
+uint8 vga_col;
+
 uint8 vga_color;
 uint16* vga_buf;
 
@@ -32,7 +34,7 @@
 static uint8 vga_gencolor(int fg, int bg);
 static uint16 vga_char(unsigned char c, uint8 color);
 void vga_clear(char c);
-void vga_init(int fg, int bg);
+void vga_init(void);
 void vga_writeto(char c, uint8 color, int x, int y);
 void vga_scroll(void);
 void vga_nl(void);
--- /dev/null
+++ b/pc/cons.c
@@ -1,0 +1,31 @@
+#include <cons.h>
+
+void
+consinit(consdev_t cons)
+{
+	cons.init();
+}
+
+void
+consclear(consdev_t cons, char c)
+{
+	cons.clear(c);
+}
+
+void
+consputc(consdev_t cons, char c)
+{
+	cons.putc(c);
+}
+
+void
+conswrite(consdev_t cons, char *s)
+{
+	cons.puts(s);
+}
+
+char
+consread(consdev_t cons)
+{
+	return cons.getc();	
+}
--- a/pc/kern.c
+++ b/pc/kern.c
@@ -1,16 +1,15 @@
 #include <u.h>
-#include <vga.h>
-#include <com.h>
-#include <ps2.h>
-#include <mem.h>
 #include <libc.h>
+#include <mem.h>
+#include <cons.h>
+#include <pccons.h>
 
+
 /* abandon hope, all ye who enter here */
 void
 panic(void)
 {
-	vga_puts("panic: give up. it's over.\n");
-	com_puts("panic: give up. it's over.\n");
+	conswrite(pccons, "panic: give up. it's over.\n");
 }
 
 int
@@ -19,21 +18,21 @@
 	/* replace it with a proper table */
 	if(!strcmp(cmd, "clear"))
 	{
-		vga_clear(' ');
+		consclear(pccons, ' ');
 	}
 	else if(!strcmp(cmd, "hello"))
 	{
-		vga_puts("hi");
+		conswrite(pccons, "hi");
 	}
 	else if(!strcmp(cmd, "reboot"))
 		return 0;
 	else
 	{
-		vga_puts("\nno such command\n");
+		conswrite(pccons, "no such command");
 	}
 
-	vga_putc('\n');
-	vga_puts("> ");
+	conswrite(pccons, "\n");
+	conswrite(pccons, "> ");
 	return 1;
 
 }
@@ -46,15 +45,13 @@
 
 	int i = 0;
 	memset(cmd, 0, 16); 
-	vga_puts("> ");
+	conswrite(pccons, "> ");
 	while(1)
 	{
 
-		c = ps2_getc();
-		if(c == 0)
-			continue;
+		c = consread(pccons);
 
-		vga_putc(c);
+		consputc(pccons, c);
 
 		if(c == '\r' || c == '\n')
 		{
@@ -63,8 +60,7 @@
 
 			/* clear the mess */
 			cmd = malloc(16);
-			i = 0;
-			
+			i = 0;	
 		}
 		else if(c != '\b')
 		{
@@ -87,7 +83,8 @@
 {
 	memset(memap, FREE, MEM_MAX * (1024 / BLOCKSIZE));
 
-	vga_init(BLUE, LIGHT_GREY);
+	consinit(pccons);
+	//conswrite(pccons, "Teppich\n");
 	vga_puts("Teppich\n");
 
 	repl();
--- a/pc/ps2.c
+++ b/pc/ps2.c
@@ -20,12 +20,15 @@
 ps2_getc()
 {
 	char c;
-	if(!(ps2_read_status() & 1))
-		return 0;
+	while(1)
+	{
+		if(!(ps2_read_status() & 1))
+			continue;
 
-	c = inb(KB);
-	if(c & 0x80)
-		return 0;
+		c = inb(KB);
+		if(c & 0x80)
+			continue;
 
-	return keymap[c];
+		return keymap[c];
+	}
 }
--- a/pc/vga.c
+++ b/pc/vga.c
@@ -30,12 +30,12 @@
 }
 
 void
-vga_init(int fg, int bg)
+vga_init(void)
 {
 	vga_row = 0;
 	vga_col = 0;
-	vga_color = vga_gencolor(fg, bg);
-	vga_buf = (uint16*) 0xB8000;
+	vga_color = vga_gencolor(WHITE, BLACK);
+	vga_buf = (uint16*) VGA_MEM;
 	vga_clear(' ');
 }