Browse Source

7.660u-keyboard: add a keyboard buffer (fifo)

rain 5 năm trước cách đây
mục cha
commit
755b7af503
15 tập tin đã thay đổi với 322 bổ sung và 69 xóa
  1. 2 1
      LINES
  2. 4 4
      Makefile
  3. 3 0
      README.md
  4. 0 1
      include/kernel/int.h
  5. 45 0
      include/kernel/keyboard.h
  6. 48 0
      include/lib/fifo.h
  7. 88 0
      include/modules/ide.h
  8. 0 2
      init/main.c
  9. 1 1
      kernel/Makefile
  10. 1 16
      kernel/irq.c
  11. 65 0
      kernel/keyboard.c
  12. 1 1
      kernel/mm.c
  13. 1 1
      kernel/rlib/Makefile
  14. 63 0
      kernel/rlib/fifo.c
  15. 0 42
      kernel/sched.c

+ 2 - 1
LINES

@@ -27,5 +27,6 @@
 2021-8-04 7.110: 7110 lines, 50 files, rain
 2021-8-08 7.301: 7301 lines, 50 files, rain
 2021-8-08 7.274: 7274 lines, 50 files, rain
-2021-8-09 7/411: 7411 lines, 50 files, rain
+2021-8-09 7.411: 7411 lines, 50 files, rain
+2021-8-12 7.660: 7660 lines, 55 files, rain
 

+ 4 - 4
Makefile

@@ -1,12 +1,12 @@
 OBJS = boot/bootsect 
 BOOT_SRCS = boot/boot.asm boot/loader.asm 
 BOOT_OBJS = boot/boot.bin boot/loader.bin 
-RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c
-RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o 
+RLIB_SRCS = kernel/rlib/bitman.c kernel/rlib/bitmap.c kernel/rlib/fifo.c 
+RLIB_OBJS = kernel/rlib/bitman.o kernel/rlib/bitmap.o kernel/rlib/fifo.o 
 MODULE_SRCS = modules/tty.c 
 MODULE_OBJS = modules/tty.o 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o $(RLIB_OBJS) $(MODULE_OBJS)
-KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c $(RLIB_SRCS) $(MODULE_SRCS)
+KOBJS = init/head.o init/main.o kernel/int.o kernel/keyboard.o kernel/trap.o kernel/vsprintf.o kernel/irq.o kernel/panic.o kernel/mm.o kernel/pci.o kernel/timer.o kernel/sched.o kernel/sys.o $(RLIB_OBJS) $(MODULE_OBJS)
+KSRCS = init/head.S init/main.c kernel/int.c kernel/keyboard.c kernel/trap.S kernel/vsprintf.c kernel/irq.c kernel/panic.c kernel/mm.c kernel/pci.c kernel/timer.S kernel/sched.c kernel/sys.c $(RLIB_SRCS) $(MODULE_SRCS)
 
 all: Makefile
 	make -C tools/

+ 3 - 0
README.md

@@ -119,3 +119,6 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 7.411u-sched:  
 &nbsp;&nbsp;&nbsp;&nbsp;`switch_to` is now working. it can switch in A and B. (but it is not a `schedule()`) :-)
 
+7.660u-keyboard:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we use a fifo-buffer, to store key scan codes. (but we don't put it to screen)
+

+ 0 - 1
include/kernel/int.h

@@ -192,6 +192,5 @@ extern struct irq_entry irq_table[MAX_IRQ_NR];
 #define RSP 	184
 #define SS 	192
 
-
 #endif
 

+ 45 - 0
include/kernel/keyboard.h

@@ -0,0 +1,45 @@
+/* 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/>.  
+*/
+
+
+
+
+#ifndef _KBD_H
+#define _KBD_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+
+struct keyboard_buf {
+	char raw;
+	__uint32_t cooked;
+
+	struct keyboard_buf *next;
+};
+
+#define KBD_DATA 0x60
+#define MAX_KEYS 128
+
+#endif
+

+ 48 - 0
include/lib/fifo.h

@@ -0,0 +1,48 @@
+/* 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/>.  
+*/
+
+
+
+
+#ifndef _FIFO_H
+#define _FIFO_H
+
+typedef __uint64_t __fifo_buf_t;
+
+struct fifo {
+	__fifo_buf_t *buf;
+	int total;
+	int next_wr, next_rd;
+
+	/* how many we should (can) read */
+	int need_read;
+};
+
+
+void fifo_init(struct fifo *, __fifo_buf_t *, int);
+__fifo_buf_t fifo_get(struct fifo *);
+void fifo_put(struct fifo *, __fifo_buf_t);
+
+
+#endif
+

+ 88 - 0
include/modules/ide.h

@@ -0,0 +1,88 @@
+/* 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/>.  
+*/
+
+
+
+
+#ifndef _MOD_IDE_H
+#define _MOD_IDE_H
+
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+errno_t ide_init(void);
+extern void do_ide_intr(void);
+void unexcepted_ide_intr(void);
+void ide_stat(void);
+
+
+#define IDE_CMD_ID 	0xec
+#define IDE_CMD_RD 	0x20
+#define IDE_CMD_RD48 	0x24
+#define IDE_CMD_WR 	0x30
+#define IDE_CMD_WR48 	0x34
+
+#define DISK0_DATA 	0x1f0
+#define DISK0_ERR	0x1f1
+#define DISK0_SECTORS 	0x1f2
+#define DISK0_ADDR_LOW	0x1f3
+#define DISK0_ADDR_MID	0x1f4
+#define DISK0_ADDR_HIGH 0x1f5
+#define DISK0_MODE 	0x1f6
+#define DISK0_CMD 	0x1f7
+
+#define DISK0_CTL_STAT	0x3f6
+
+#define DISK1_DATA 	0x170
+#define DISK1_ERR 	0x171
+#define DISK1_SECTORS 	0x172
+#define DISK1_ADDR_LOW 	0x173
+#define DISK1_ADDR_MID 	0x174
+#define DISK1_ADDR_HIGH 0x175
+#define DISK1_MODE	0x176
+#define DISK1_CMD	0x177
+
+#define DISK1_CTL_STAT 	0x376
+
+#define STAT_BUSY 	0x80
+#define STAT_READY 	0x40
+#define STAT_REQ 	0x08
+#define STAT_ERR 	0x01
+
+/* enable the irq...but 0 is enable, 1 is disable */
+#define CTL_IRQ		0x02
+#define CTL_RESET 	0x04
+
+/* irq 14 and 15 */
+#define DISK0_INT_NR 	0x2e
+#define DISK1_INT_NR 	0x2f
+
+#define DISK0_IRQ_NR 	14
+#define DISK1_IRQ_NR 	15
+
+#define MODE_DISK1 	0x10
+#define MODE_LBA 	0x40
+
+#endif
+

+ 0 - 2
init/main.c

@@ -68,8 +68,6 @@ int init(void) {
 
 	sti();
 
-	trace_mm();
-
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
 	if (bootarg->bootdev & 0x80) 

+ 1 - 1
kernel/Makefile

@@ -1,6 +1,6 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
 
-all: int.o trap.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o  
+all: int.o trap.o vsprintf.o irq.o panic.o mm.o pci.o timer.o sched.o sys.o keyboard.o 
 
 
 %.o: %.S

+ 1 - 16
kernel/irq.c

@@ -99,7 +99,7 @@ void do_irq(__uint64_t *rsp, __uint64_t nr) {
 
 	else 
 		printk(red, "unknow irq %x\n", nr - 0x20);
-		
+
 	outb(0x20, 0x20);
 
 	schk_leave();
@@ -108,18 +108,3 @@ void do_irq(__uint64_t *rsp, __uint64_t nr) {
 }
 
 
-#define KEYBOARD_DATA 0x0060
-
-void do_keyboard(void) {
-	schk_init();
-
-	/* get keyboard scan code */
-
-	// char code = inb(KEYBOARD_DATA);
-
-	/* we don't know how to use it now */
-
-	schk_leave();
-
-	return;
-}

+ 65 - 0
kernel/keyboard.c

@@ -0,0 +1,65 @@
+/* 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/errno.h>
+#include <kernel/types.h>
+
+#include <modules/tty.h>
+#include <lib/string.h>
+#include <lib/fifo.h>
+
+#include <kernel/mm.h>
+#include <kernel/keyboard.h>
+#include <kernel/int.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+
+struct fifo kbd_fifo;
+
+
+errno_t kbd_init(void) {
+	__fifo_buf_t *key_buf = kmalloc(&pool, MAX_KEYS * sizeof(__fifo_buf_t));
+	memset(key_buf, 0x00, MAX_KEYS * sizeof(__uint64_t));
+
+	fifo_init(&kbd_fifo, key_buf, MAX_KEYS);
+
+	return 0;
+}
+
+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);
+
+	return;
+}
+

+ 1 - 1
kernel/mm.c

@@ -322,7 +322,7 @@ void trace_mm(void) {
 	int i;
 
 	for (i = 0; i < 512; i++) {
-		if (pt[i]) 
+		if (pt[i] & ~0xff) 
 			printk(yellow, "%q -> %q\n", i << PAGE_SHIFT, pt[i] & ~0xff);
 	}
 		

+ 1 - 1
kernel/rlib/Makefile

@@ -1,6 +1,6 @@
 C_FLAGS = -mcmodel=large -m64 -fno-builtin -nostdlib -I ../../include/ -Wall -Werror -O3 -g 
 
-all: bitmap.c bitman.c bitmap.o bitman.o 
+all: bitmap.c bitman.c bitmap.o bitman.o fifo.o 
 
 %.o: %.c
 	gcc -c $*.c $(C_FLAGS)

+ 63 - 0
kernel/rlib/fifo.c

@@ -0,0 +1,63 @@
+/* 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/errno.h>
+#include <kernel/types.h>
+
+#include <lib/fifo.h>
+
+
+void fifo_init(struct fifo *fifo, __fifo_buf_t *buf, int total) {
+	fifo->buf = buf;
+	fifo->total = total;
+	
+	fifo->next_wr = fifo->next_rd = 0;
+	fifo->need_read = 0;
+
+	return;
+}
+
+
+__fifo_buf_t fifo_get(struct fifo *fifo) {
+	if (!fifo->need_read) 
+		return 0;
+
+	fifo->need_read--;
+
+	return fifo->buf[fifo->next_rd++];
+}
+
+
+void fifo_put(struct fifo *fifo, __fifo_buf_t value) {
+	fifo->buf[fifo->next_wr] = value;
+	fifo->need_read++;
+
+	if (fifo->next_wr++ == fifo->total) 
+		fifo->next_wr = 0;
+
+	return;
+}

+ 0 - 42
kernel/sched.c

@@ -58,39 +58,6 @@ struct task *task_lists[16];
 __uint32_t next_pid = 0;
 
 
-__uint64_t ktask_a(__uint64_t arg) {
-	__uint64_t a = 12345;
-	printk(white, "ktask a is now running. arg: %q\n", arg);
-
-	for (;;) {
-		print(white, "switching to task B...\n");
-
-		if (a != 12345) 
-			panic("ktask_a: a changed. \n");
-
-		switch_to(task_lists[0]->next);
-	}
-
-	return 1;
-}
-
-__uint64_t ktask_b(__uint64_t arg) {
-	__uint64_t a = 67890;
-	printk(white, "ktask b is now running. arg: %q\n", arg);
-
-	for (;;) {
-		print(white, "switching to task A...\n");
-
-		if (a != 67890) 
-			panic("ktask_b: a changed. \n");
-
-		switch_to(task_lists[0]);
-	}
-
-	return 1;
-}
-
-
 int do_exit(__uint64_t exitcode) {
 	printk(white, "do_exit: %q\n", exitcode);
 
@@ -112,15 +79,6 @@ int task_init(void) {
 
 	current = task_lists[0];
 
-	if (ktask_create(ktask_a, 0x12345678) == -ENOMEM) {
-		printk(red, "ktask_create returns -ENOMEM (no enough memory). ");
-		return 1;
-	}
-
-	ktask_create(ktask_b, 0x23456789);
-	
-	switch_to(task_lists[0]);
-
 	return 0;
 }