Ver código fonte

7.937u-keyboard: can input now

rain 5 anos atrás
pai
commit
0283c40466
11 arquivos alterados com 224 adições e 66 exclusões
  1. 1 0
      LINES
  2. 3 0
      README.md
  3. 21 0
      include/kernel/keyboard.h
  4. 3 3
      include/lib/fifo.h
  5. 4 0
      include/modules/tty.h
  6. 8 2
      init/main.c
  7. 155 1
      kernel/keyboard.c
  8. 10 3
      kernel/rlib/fifo.c
  9. 2 55
      kernel/sched.c
  10. 1 1
      kernel/timer.S
  11. 16 1
      modules/tty.c

+ 1 - 0
LINES

@@ -31,4 +31,5 @@
 2021-8-12 7.660: 7660 lines, 55 files, rain
 2021-8-12 7.689: 7689 lines, 55 files, rain
 2021-8-13 7.783: 7783 lines, 55 files, rain
+2021-8-13 7.937: 7937 lines, 55 files, rain
 

+ 3 - 0
README.md

@@ -131,3 +131,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 7.783u-sched:  
 &nbsp;&nbsp;&nbsp;&nbsp;`schedule()` is now working! 
 
+7.937u-keyboard:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we can input characters, by `put_keycode()`.
+ 

+ 21 - 0
include/kernel/keyboard.h

@@ -38,8 +38,29 @@ struct keyboard_buf {
 	struct keyboard_buf *next;
 };
 
+errno_t kbd_init(void);
+void put_keycode(void);
+
 #define KBD_DATA 0x60
 #define MAX_KEYS 128
 
+#define CTRL 	0x1d
+#define EXT 	0xe0
+#define LSHIFT 	0x2a
+#define RSHIFT 	0x36
+#define ALT 	0x38
+#define CAPSLK 	0x3a
+#define BREAK 	0x80
+
+#define UP 	0x48
+#define LEFT 	0x4b
+#define DOWN 	0x50
+#define RIGHT 	0x4d
+
+#define BACKSP 	0x0e
+#define ENTER 	0x1c
+
+#define TAB 	0x0f
+
 #endif
 

+ 3 - 3
include/lib/fifo.h

@@ -30,12 +30,12 @@
 typedef __uint64_t __fifo_buf_t;
 
 struct fifo {
-	__fifo_buf_t *buf;
+	volatile __fifo_buf_t *buf;
 	int total;
-	int next_wr, next_rd;
+	volatile int next_wr, next_rd;
 
 	/* how many we should (can) read */
-	int need_read;
+	volatile int need_read;
 };
 
 

+ 4 - 0
include/modules/tty.h

@@ -47,6 +47,10 @@ __uint16_t read_cursor(void);
 
 void roll_screen(void);
 
+extern __uint8_t x, y;
+
+#define gotoxy(__x, __y) x = (__x);y = (__y);update_cursor();
+
 #define FB_ADDR ((__uint16_t *) 0xb8000)
 
 /* x and y in this vga mode */

+ 8 - 2
init/main.c

@@ -36,6 +36,8 @@
 #include <kernel/int.h>
 
 #include <kernel/sched.h>
+#include <kernel/keyboard.h>
+
 #include <lib/string.h>
 #include <sys/syscall.h>
 
@@ -64,9 +66,10 @@ int init(void) {
 	int_init();
 	mm_init();
 
-	sti();
-
 	task_init();
+	kbd_init();
+
+	sti();
 
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
@@ -76,6 +79,9 @@ int init(void) {
 	else 
 		printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
 
+	for (;;) 
+		put_keycode();
+
 	return 0;
 }
 

+ 155 - 1
kernel/keyboard.c

@@ -43,6 +43,10 @@
 
 
 struct fifo kbd_fifo;
+__uint8_t ctrl = 0, shift = 0, alt, capslock = 0;
+
+/* the 0xe0 */
+__uint8_t ext = 0;
 
 
 errno_t kbd_init(void) {
@@ -61,7 +65,6 @@ errno_t kbd_init(void) {
 
 void do_keyboard(void) {
 	__uint8_t code = inb(KBD_DATA);
-	printk(white, "%b\n", code);
 
 	/* insert it to the fifo */
 	fifo_put(&kbd_fifo, code);
@@ -69,3 +72,154 @@ void do_keyboard(void) {
 	return;
 }
 
+
+/* only displayable numbers/alphabet */
+static char keycode_alphabet[] = {
+	/* nul, esc */
+	0, 0, '1', '2', '3', '4', '5', '6', 
+	/* backspace, tab */
+	'7', '8', '9', '0', '-', '=', 0, 0, 
+	'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 
+	/* enter, lctrl */
+	'o', 'p', '[', ']', 0, 0, 'a', 's', 
+	'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 
+	/* lshift */
+	'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 
+	/* rshift */
+	'b', 'n', 'm', ',', '.', '/', 0, '*', 
+	/* capslock, f1 - f5 */
+	0, ' ', 0, 0, 0, 0, 0, 0, 
+	/* f6 - f10, numlock, scrlock */
+	0, 0, 0, 0, 0, 0, 0, '7', 
+	'8', '9', '-', '4', '5', '6', '+', '1', 
+	'2', '3', '0', '.'
+};
+
+static char keycode_alphabet_shift[] = {
+	0, 0, '!', '@', '#', '$', '%', '^', 
+	'&', '*', '(', ')', '_', '+', 0, 0, 
+	'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 
+	'O', 'P', '{', '}', 0, 0, 'A', 'S', 
+	'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', 
+	'"', '~', 0, '|', 'Z', 'X', 'C', 'V', 
+	'B', 'N', 'M', '<', '>', '?', 0, '*', 
+	0, ' ', 0, 0, 0, 0, 0, 0, 
+	0, 0, 0, 0, 0, 0, 0, '7', 
+	'8', '9', '-', '4', '5', '6', '+', '1', 
+	'2', '3', '0', '.'
+};
+
+void put_keycode(void) {
+	__fifo_buf_t i;
+	i = fifo_get(&kbd_fifo);
+
+	if (!i) 
+		return;
+
+	if (i == EXT) {
+		ext = 1;
+		return;
+	}
+
+
+	/* the limit of keycode_alphabet */
+	if (!ext && i < 0x54) {
+		char a;
+
+		if (!shift && !capslock) {
+			if ((a = keycode_alphabet[i])) 
+				putc(a, white);
+
+			else 
+				goto undisplayable;
+		}
+
+		else {
+			if ((a = keycode_alphabet_shift[i]))
+				putc(a, white);
+
+			else 
+				goto undisplayable;
+		}
+
+		/* its displayable, so return */
+
+		return;
+	}
+
+undisplayable:
+	if (i == EXT) {
+		ext = 1;
+		return;
+	}
+
+	if (i == CTRL) {
+		if (ext) 
+			/* got next character */
+			ext = 0;
+		
+		ctrl = 1;
+		return;
+	}
+
+	if (i == BREAK + CTRL) {
+		if (ext) 
+			ext = 0;
+
+		ctrl = 0;
+		return;
+	}
+
+	if (i == LSHIFT || i == RSHIFT) {
+		shift = 1;
+		return;
+	}
+
+	if (i == BREAK + LSHIFT || i == BREAK + RSHIFT) {
+		shift = 0;
+		return;
+	}
+
+	if (i == ALT) {
+		if (ext) 
+			ext = 0;
+
+		alt = 1;
+		return;
+	}
+
+	if (i == BREAK + ALT) {
+		if (ext) 
+			ext = 0;
+
+		alt = 0;
+		return;
+	}
+
+	if (i == CAPSLK) {
+		capslock = !capslock;
+		return;
+	}
+
+	if (i == BREAK + CAPSLK) 
+		return;
+
+	if (i == BACKSP) {
+		putc('\b', white);
+		return;
+	}
+
+	if (i == ENTER) {
+		putc('\n', white);
+		return;
+	}
+
+	if (i == TAB) {
+		putc('\t', white);
+		return;
+	}
+
+	ext = 0;
+
+	return;
+}

+ 10 - 3
kernel/rlib/fifo.c

@@ -49,24 +49,31 @@ void fifo_init(struct fifo *fifo, __fifo_buf_t *buf, int total) {
 __fifo_buf_t fifo_get(struct fifo *fifo) {
 	schk_init();
 
+	__fifo_buf_t res;
+
 	if (!fifo->need_read) 
 		return 0;
 
 	fifo->need_read--;
 
+	res = fifo->buf[fifo->next_rd++];
+
+	if (fifo->next_rd == fifo->total) 
+		fifo->next_rd = 0;
+
 	schk_leave();
 
-	return fifo->buf[fifo->next_rd++];
+	return res;
 }
 
 
 void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
 	schk_init();
 
-	fifo->buf[fifo->next_wr] = value;
+	fifo->buf[fifo->next_wr++] = value;
 	fifo->need_read++;
 
-	if (fifo->next_wr++ == fifo->total) 
+	if (fifo->next_wr == fifo->total) 
 		fifo->next_wr = 0;
 
 	schk_leave();

+ 2 - 55
kernel/sched.c

@@ -56,56 +56,14 @@ struct task *current;
 
 struct task *tasks[MAX_TASKS] = {0};
 
+
+
 int do_exit(__uint64_t exitcode) {
 	printk(white, "do_exit: %q\n", exitcode);
 
 	for (;;);
 }
 
-__uint64_t task_a(__uint64_t arg) {
-	register __uint64_t a = 12345678;
-	printk(white, "task a is running: %x\n", arg);
-
-	for (;;) {
-		if (a != 12345678) {
-			printk(white, "a is now %d\n", a);
-			panic("task_a: a changed. ");
-		}
-
-		putc('A', white);
-	}
-}
-
-__uint64_t task_b(__uint64_t arg) {
-	register __uint64_t b = 90123456;
-
-	printk(white, "task b is running: %x\n", arg);
-
-	for (;;) {
-		if (b != 90123456) {
-			printk(white, "b is now %d\n", b);
-			panic("task_b: b changed. ");
-		}
-
-		putc('B', white);
-	}
-}
-
-__uint64_t task_c(__uint64_t arg) {
-	register __uint64_t c = 12345670;
-
-	printk(white, "task b is running: %x\n", arg);
-
-	for (;;) {
-		if (c != 12345670) {
-			printk(white, "c is now %d\n", c);
-			panic("task_c: c changed. ");
-		}
-
-		putc('C', white);
-	}
-}
-
 
 int task_init(void) {
 	/* initial the init-tss */
@@ -117,19 +75,8 @@ int task_init(void) {
 
 	set_tss(&init_tss, 0);
 
-	if (ktask_create(task_a, 0x12345678))  
-		panic("task_init: failed to create task a. \n");
-
-	if (ktask_create(task_b, 0x23456789)) 
-		panic("task_init: failed to create task b. \n");
-
-	if (ktask_create(task_c, 0x34567890)) 
-		panic("task_init: failed to create task c. \n");
-
 	current = &init_task;
 
-	switch_to(tasks[0]);
-
 	return 0;
 }
 

+ 1 - 1
kernel/timer.S

@@ -30,7 +30,7 @@ do_timer:
 
 	incq jiffies(%rip)
 
-	callq do_clock
+//	callq do_clock
 
 	ret
 

+ 16 - 1
modules/tty.c

@@ -43,7 +43,7 @@ static char buffer[256];
 
 static int syslog_index = 0;
 static __uint16_t *fb = FB_ADDR;
-static __uint8_t x, y;
+__uint8_t x, y;
 
 
 errno_t tty_init(void) {
@@ -132,6 +132,16 @@ void putc(char c, __uint8_t color) {
 		return;
 	}
 
+	if (c == '\b') {
+		if (x) {
+			x--;
+			update_cursor();
+			*fb = 0x0720;
+		}
+
+		return;
+	}
+
 	else if (c == '\t') {
 		__uint64_t ax = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
 
@@ -184,6 +194,11 @@ __uint16_t read_cursor(void) {
 }
 
 void update_cursor(void) {
+	if (y > VGA_Y) {
+		roll_screen();
+		y = 24;
+	}
+
 	__uint16_t pos = y * bootarg->vga_x + x;
 	fb = FB_ADDR + pos;