diff --git a/07_day/Makefile b/07_day/Makefile index 199cdee..2bda89c 100644 --- a/07_day/Makefile +++ b/07_day/Makefile @@ -1,5 +1,5 @@ OBJS_BOOTPACK = bootpack.obj naskfunc.obj hankaku.obj graphic.obj dsctbl.obj \ - int.obj + int.obj fifo.obj TOOLPATH = ../z_tools/ INCPATH = ../z_tools/haribote/ diff --git a/07_day/bootpack.c b/07_day/bootpack.c index aaec63f..dbe463c 100644 --- a/07_day/bootpack.c +++ b/07_day/bootpack.c @@ -3,18 +3,19 @@ #include "bootpack.h" #include -extern struct KEYBUF keybuf; +extern struct FIFO8 keyfifo; void HariMain(void) { struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; - char s[40], mcursor[256]; - int mx, my, i, j; + char s[40], mcursor[256], keybuf[32]; + int mx, my, i; init_gdtidt(); init_pic(); io_sti(); /* IDT/PICの初期化が終わったのでCPUの割り込み禁止を解除 */ + fifo8_init(&keyfifo, 32, keybuf); io_out8(PIC0_IMR, 0xf9); /* PIC1とキーボードを許可(11111001) */ io_out8(PIC1_IMR, 0xef); /* マウスを許可(11101111) */ @@ -29,15 +30,10 @@ void HariMain(void) for (;;) { io_cli(); - if (keybuf.len == 0) { + if (fifo8_status(&keyfifo) == 0) { io_stihlt(); } else { - i = keybuf.data[keybuf.next_r]; - keybuf.len--; - keybuf.next_r++; - if (keybuf.next_r == 32) { - keybuf.next_r = 0; - } + i = fifo8_get(&keyfifo); io_sti(); sprintf(s, "%02X", i); boxfill8(binfo->vram, binfo->scrnx, COL8_008484, 0, 16, 15, 31); diff --git a/07_day/bootpack.h b/07_day/bootpack.h index 0a9000e..650af9c 100644 --- a/07_day/bootpack.h +++ b/07_day/bootpack.h @@ -22,6 +22,16 @@ void asm_inthandler21(void); void asm_inthandler27(void); void asm_inthandler2c(void); +/* fifo.c */ +struct FIFO8 { + unsigned char *buf; + int p, q, size, free, flags; +}; +void fifo8_init(struct FIFO8 *fifo, int size, unsigned char *buf); +int fifo8_put(struct FIFO8 *fifo, unsigned char data); +int fifo8_get(struct FIFO8 *fifo); +int fifo8_status(struct FIFO8 *fifo); + /* graphic.c */ void init_palette(void); void set_palette(int start, int end, unsigned char *rgb); @@ -74,10 +84,6 @@ void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar); #define AR_INTGATE32 0x008e /* int.c */ -struct KEYBUF { - unsigned char data[32]; - int next_r, next_w, len; -}; void init_pic(void); void inthandler21(int *esp); void inthandler27(int *esp); diff --git a/07_day/fifo.c b/07_day/fifo.c new file mode 100644 index 0000000..f582a49 --- /dev/null +++ b/07_day/fifo.c @@ -0,0 +1,57 @@ +/* FIFO */ + +#include "bootpack.h" + +#define FLAGS_OVERRUN 0x0001 + +void fifo8_init(struct FIFO8 *fifo, int size, unsigned char *buf) +/* 初始化FIFO缓冲区 */ +{ + fifo->size = size; + fifo->buf = buf; + fifo->free = size; /* 缓冲区大小 */ + fifo->flags = 0; + fifo->p = 0; /* 下一个数据写入位置 */ + fifo->q = 0; /* 下一个数据读出位置 */ + return; +} + +int fifo8_put(struct FIFO8 *fifo, unsigned char data) +/* 向FIFO传送数据并保存 */ +{ + if (fifo->free == 0) { + /* 没有空间了,溢出 */ + fifo->flags |= FLAGS_OVERRUN; + return -1; + } + fifo->buf[fifo->p] = data; + fifo->p++; + if (fifo->p == fifo->size) { + fifo->p = 0; + } + fifo->free--; + return 0; +} + +int fifo8_get(struct FIFO8 *fifo) +/* 从FIFO取得一个数据 */ +{ + int data; + if (fifo->free == fifo->size) { + /* 如果缓冲区为空则返回-1 */ + return -1; + } + data = fifo->buf[fifo->q]; + fifo->q++; + if (fifo->q == fifo->size) { + fifo->q = 0; + } + fifo->free++; + return data; +} + +int fifo8_status(struct FIFO8 *fifo) +/* 报告一下积攒是数据量 */ +{ + return fifo->size - fifo->free; +} diff --git a/07_day/int.c b/07_day/int.c index bd4a18c..c8595e5 100644 --- a/07_day/int.c +++ b/07_day/int.c @@ -26,7 +26,7 @@ void init_pic(void) #define PORT_KEYDAT 0x0060 -struct KEYBUF keybuf; +struct FIFO8 keyfifo; void inthandler21(int *esp) /* 来自PS/2键盘的中断 */ @@ -35,14 +35,7 @@ void inthandler21(int *esp) unsigned char data, s[4]; io_out8(PIC0_OCW2, 0x61); /* 通知PIC IRQ-01 已经受理完毕 */ data = io_in8(PORT_KEYDAT); - if (keybuf.len < 32) { - keybuf.data[keybuf.next_w] = data; - keybuf.len++; - keybuf.next_w++; - if (keybuf.next_w == 32) { - keybuf.next_w = 0; - } - } + fifo8_put(&keyfifo, data); return; }