From 90ad25656d8e937e99bbae666ce46084f3a7322a Mon Sep 17 00:00:00 2001 From: Yourtion Date: Wed, 20 Apr 2016 13:57:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=B0=83=E6=95=B4FIFO?= =?UTF-8?q?=E7=BC=93=E5=86=B2=E5=8C=BA=EF=BC=882=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 13_day/bootpack.c | 86 +++++++++++++++++++++-------------------------- 13_day/bootpack.h | 24 ++++++------- 13_day/fifo.c | 26 +++++++------- 13_day/keyboard.c | 25 ++++++++------ 13_day/mouse.c | 22 +++++++----- 13_day/timer.c | 4 +-- 6 files changed, 93 insertions(+), 94 deletions(-) diff --git a/13_day/bootpack.c b/13_day/bootpack.c index 837bffb..758c4c6 100644 --- a/13_day/bootpack.c +++ b/13_day/bootpack.c @@ -9,8 +9,9 @@ void putfonts8_asc_sht(struct SHEET *sht, int x, int y, int c, int b, char *s, i void HariMain(void) { struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; - struct FIFO8 timerfifo, timerfifo2, timerfifo3; - char s[40], keybuf[32], mousebuf[128], timerbuf[8], timerbuf2[8], timerbuf3[8]; + struct FIFO32 fifo; + char s[40]; + int fifobuf[128]; struct TIMER *timer, *timer2, *timer3; int mx, my, i; unsigned int memtotal; @@ -23,25 +24,23 @@ void HariMain(void) init_gdtidt(); init_pic(); io_sti(); /* IDT/PIC的初始化已经完成,于是开放CPU的中断 */ - fifo8_init(&keyfifo, 32, keybuf); - fifo8_init(&mousefifo, 128, mousebuf); - init_pit(); - io_out8(PIC0_IMR, 0xf8); /* PIT和PIC1和键盘设置为许可(11111000) */ + fifo32_init(&fifo, 128, fifobuf); + init_keyboard(&fifo, 256); + enable_mouse(&fifo, 512, &mdec); + io_out8(PIC0_IMR, 0xf8); /* 设定PIT和PIC1以及键盘为许可(11111000) */ io_out8(PIC1_IMR, 0xef); /* 开放鼠标中断(11101111) */ + init_pit(); - fifo8_init(&timerfifo, 8, timerbuf); - timer = timer_alloc(); - timer_init(timer, &timerfifo, 10); - timer_settime(timer, 1000); - timer2 = timer_alloc(); - timer_init(timer2, &timerfifo, 3); - timer_settime(timer2, 300); - timer3 = timer_alloc(); - timer_init(timer3, &timerfifo, 1); - timer_settime(timer3, 50); + timer = timer_alloc(); + timer_init(timer, &fifo, 10); + timer_settime(timer, 1000); + timer2 = timer_alloc(); + timer_init(timer2, &fifo, 3); + timer_settime(timer2, 300); + timer3 = timer_alloc(); + timer_init(timer3, &fifo, 1); + timer_settime(timer3, 50); - init_keyboard(); - enable_mouse(&mdec); memtotal = memtest(0x00400000, 0xbfffffff); memman_init(memman); memman_free(memman, 0x00001000, 0x0009e000); /* 0x00001000 - 0x0009efff */ @@ -79,19 +78,17 @@ void HariMain(void) putfonts8_asc_sht(sht_win, 40, 28, COL8_000000, COL8_C6C6C6, s, 10); io_cli(); - if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) + fifo8_status(&timerfifo) == 0) { + if (fifo32_status(&fifo) == 0) { io_sti(); } else { - if (fifo8_status(&keyfifo) != 0) { - i = fifo8_get(&keyfifo); - io_sti(); - sprintf(s, "%02X", i); + i = fifo32_get(&fifo); + io_sti(); + if (256 <= i && i <= 511) { /* 键盘数据*/ + sprintf(s, "%02X", i - 256); putfonts8_asc_sht(sht_back, 0, 16, COL8_FFFFFF, COL8_008484, s, 2); - } else if (fifo8_status(&mousefifo) != 0) { - i = fifo8_get(&mousefifo); - io_sti(); - if (mouse_decode(&mdec, i) != 0) { - /* 3字节都凑齐了,所以把它们显示出来*/ + } else if (512 <= i && i <= 767) { /* 鼠标数据*/ + if (mouse_decode(&mdec, i - 512) != 0) { + /* 已经收集了3字节的数据,所以显示出来 */ sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y); if ((mdec.btn & 0x01) != 0) { s[1] = 'L'; @@ -122,25 +119,20 @@ void HariMain(void) putfonts8_asc_sht(sht_back, 0, 0, COL8_FFFFFF, COL8_008484, s, 10); sheet_slide(sht_mouse, mx, my);/* 包含sheet_refresh含sheet_refresh */ } - } else if (fifo8_status(&timerfifo) != 0) { - i = fifo8_get(&timerfifo); /*超时的是哪个呢? */ - io_sti(); - if (i == 10) { - putfonts8_asc_sht(sht_back, 0, 64, COL8_FFFFFF, COL8_008484, "10[sec]", 7); - } else if (i == 3) { - putfonts8_asc_sht(sht_back, 0, 80, COL8_FFFFFF, COL8_008484, "3[sec]", 6); - } else { - /* 0还是1 */ - if (i != 0) { - timer_init(timer3, &timerfifo, 0); /*下面是设定为0 */ - boxfill8(buf_back, binfo->scrnx, COL8_FFFFFF, 8, 96, 15, 111); - } else { - timer_init(timer3, &timerfifo, 1); /*下面是设定为1*/ - boxfill8(buf_back, binfo->scrnx, COL8_008484, 8, 96, 15, 111); - } - timer_settime(timer3, 50); - sheet_refresh(sht_back, 8, 96, 16, 112); - } + } else if (i == 10) { /* 10秒定时器 */ + putfonts8_asc_sht(sht_back, 0, 64, COL8_FFFFFF, COL8_008484, "10[sec]", 7); + } else if (i == 3) { /* 3秒定时器 */ + putfonts8_asc_sht(sht_back, 0, 80, COL8_FFFFFF, COL8_008484, "3[sec]", 6); + } else if (i == 1) { /* 光标用定时器*/ + timer_init(timer3, &fifo, 0); /* 下面是设定0 */ + boxfill8(buf_back, binfo->scrnx, COL8_FFFFFF, 8, 96, 15, 111); + timer_settime(timer3, 50); + sheet_refresh(sht_back, 8, 96, 16, 112); + } else if (i == 0) { /* 光标用定时器 */ + timer_init(timer3, &fifo, 1); /* 下面是设定1 */ + boxfill8(buf_back, binfo->scrnx, COL8_008484, 8, 96, 15, 111); + timer_settime(timer3, 50); + sheet_refresh(sht_back, 8, 96, 16, 112); } } } diff --git a/13_day/bootpack.h b/13_day/bootpack.h index fdd47ed..0d3a105 100644 --- a/13_day/bootpack.h +++ b/13_day/bootpack.h @@ -29,14 +29,14 @@ void asm_inthandler2c(void); unsigned int memtest_sub(unsigned int start, unsigned int end); /* fifo.c */ -struct FIFO8 { - unsigned char *buf; +struct FIFO32 { + int *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); +void fifo32_init(struct FIFO32 *fifo, int size, int *buf); +int fifo32_put(struct FIFO32 *fifo, int data); +int fifo32_get(struct FIFO32 *fifo); +int fifo32_status(struct FIFO32 *fifo); /* graphic.c */ void init_palette(void); @@ -108,8 +108,7 @@ void inthandler27(int *esp); /* keyboard.c */ void inthandler21(int *esp); void wait_KBC_sendready(void); -void init_keyboard(void); -extern struct FIFO8 keyfifo; +void init_keyboard(struct FIFO32 *fifo, int data0); #define PORT_KEYDAT 0x0060 #define PORT_KEYCMD 0x0064 @@ -119,9 +118,8 @@ struct MOUSE_DEC { int x, y, btn; }; void inthandler2c(int *esp); -void enable_mouse(struct MOUSE_DEC *mdec); +void enable_mouse(struct FIFO32 *fifo, int data0, struct MOUSE_DEC *mdec); int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat); -extern struct FIFO8 mousefifo; /* memory.c */ #define MEMMAN_FREES 4090 /* 大约是32KB*/ @@ -172,8 +170,8 @@ void sheet_free(struct SHEET *sht); #define MAX_TIMER 500 struct TIMER { unsigned int timeout, flags; - struct FIFO8 *fifo; - unsigned char data; + struct FIFO32 *fifo; + int data; }; struct TIMERCTL { unsigned int count, next, using; @@ -185,7 +183,7 @@ extern struct TIMERCTL timerctl; void init_pit(void); struct TIMER *timer_alloc(void); void timer_free(struct TIMER *timer); -void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data); +void timer_init(struct TIMER *timer, struct FIFO32 *fifo, unsigned char data); void timer_settime(struct TIMER *timer, unsigned int timeout); void inthandler20(int *esp); diff --git a/13_day/fifo.c b/13_day/fifo.c index f582a49..e9d69cd 100644 --- a/13_day/fifo.c +++ b/13_day/fifo.c @@ -4,23 +4,23 @@ #define FLAGS_OVERRUN 0x0001 -void fifo8_init(struct FIFO8 *fifo, int size, unsigned char *buf) -/* 初始化FIFO缓冲区 */ +void fifo32_init(struct FIFO32 *fifo, int size, int *buf) +/* FIFO缓冲区的初始化*/ { fifo->size = size; fifo->buf = buf; - fifo->free = size; /* 缓冲区大小 */ + fifo->free = size; /*空*/ fifo->flags = 0; - fifo->p = 0; /* 下一个数据写入位置 */ - fifo->q = 0; /* 下一个数据读出位置 */ + fifo->p = 0; /*写入位置*/ + fifo->q = 0; /*读取位置*/ return; } -int fifo8_put(struct FIFO8 *fifo, unsigned char data) -/* 向FIFO传送数据并保存 */ +int fifo32_put(struct FIFO32 *fifo, int data) +/*给FIFO发送数据并储存在FIFO中*/ { if (fifo->free == 0) { - /* 没有空间了,溢出 */ + /*没有空余空间,溢出*/ fifo->flags |= FLAGS_OVERRUN; return -1; } @@ -33,12 +33,12 @@ int fifo8_put(struct FIFO8 *fifo, unsigned char data) return 0; } -int fifo8_get(struct FIFO8 *fifo) -/* 从FIFO取得一个数据 */ +int fifo32_get(struct FIFO32 *fifo) +/*从FIFO取得一个数据*/ { int data; if (fifo->free == fifo->size) { - /* 如果缓冲区为空则返回-1 */ + /*当缓冲区为空的情况下返回-1*/ return -1; } data = fifo->buf[fifo->q]; @@ -50,8 +50,8 @@ int fifo8_get(struct FIFO8 *fifo) return data; } -int fifo8_status(struct FIFO8 *fifo) -/* 报告一下积攒是数据量 */ +int fifo32_status(struct FIFO32 *fifo) +/*报告已经存储了多少数据*/ { return fifo->size - fifo->free; } diff --git a/13_day/keyboard.c b/13_day/keyboard.c index 3d0b190..eb5140a 100644 --- a/13_day/keyboard.c +++ b/13_day/keyboard.c @@ -2,15 +2,15 @@ #include "bootpack.h" -struct FIFO8 keyfifo; +struct FIFO32 *keyfifo; +int keydata0; -void inthandler21(int *esp) { - /* 来自PS/2键盘的中断 */ - struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; - unsigned char data, s[4]; - io_out8(PIC0_OCW2, 0x61); /* 通知PIC IRQ-01 已经受理完毕 */ +void inthandler21(int *esp) +{ + int data; + io_out8(PIC0_OCW2, 0x61); /* 把IRQ-01接收信号结束的信息通知给PIC */ data = io_in8(PORT_KEYDAT); - fifo8_put(&keyfifo, data); + fifo32_put(keyfifo, data + keydata0); return; } @@ -19,7 +19,8 @@ void inthandler21(int *esp) { #define KEYCMD_WRITE_MODE 0x60 #define KBC_MODE 0x47 -void wait_KBC_sendready(void) { +void wait_KBC_sendready(void) +{ /* 等待键盘控制电路准备完毕 */ for (;;) { if ((io_in8(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY) == 0) { @@ -29,8 +30,12 @@ void wait_KBC_sendready(void) { return; } -void init_keyboard(void) { - /* 初始化键盘控制电路 */ +void init_keyboard(struct FIFO32 *fifo, int data0) +{ + /* 将FIFO缓冲区的信息保存到全局变量里 */ + keyfifo = fifo; + keydata0 = data0; + /* 键盘控制器的初始化 */ wait_KBC_sendready(); io_out8(PORT_KEYCMD, KEYCMD_WRITE_MODE); wait_KBC_sendready(); diff --git a/13_day/mouse.c b/13_day/mouse.c index 294bde4..625a899 100644 --- a/13_day/mouse.c +++ b/13_day/mouse.c @@ -2,32 +2,36 @@ #include "bootpack.h" -struct FIFO8 mousefifo; +struct FIFO32 *mousefifo; +int mousedata0; void inthandler2c(int *esp) { /* 来自PS/2鼠标的中断 */ - unsigned char data; - io_out8(PIC1_OCW2, 0x64); /* 通知PIC IRQ-12 已经受理完毕 */ - io_out8(PIC0_OCW2, 0x62); /* 通知PIC IRQ-02 已经受理完毕 */ + int data; + io_out8(PIC1_OCW2, 0x64); /* 把IRQ-12接收信号结束的信息通知给PIC1 */ + io_out8(PIC0_OCW2, 0x62); /* 把IRQ-02接收信号结束的信息通知给PIC0 */ data = io_in8(PORT_KEYDAT); - fifo8_put(&mousefifo, data); + fifo32_put(mousefifo, data + mousedata0); return; } #define KEYCMD_SENDTO_MOUSE 0xd4 #define MOUSECMD_ENABLE 0xf4 -void enable_mouse(struct MOUSE_DEC *mdec) +void enable_mouse(struct FIFO32 *fifo, int data0, struct MOUSE_DEC *mdec) { + /* 将FIFO缓冲区的信息保存到全局变量里 */ + mousefifo = fifo; + mousedata0 = data0; /* 鼠标有效 */ wait_KBC_sendready(); io_out8(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); wait_KBC_sendready(); io_out8(PORT_KEYDAT, MOUSECMD_ENABLE); - /* 顺利的话,ACK(0xfa)会被送过来 */ - mdec->phase = 0; /* 等待0xfa的阶段 */ - return; + /* 顺利的话,ACK(0xfa)会被发送*/ + mdec->phase = 0; /* 等待鼠标的0xfa的阶段*/ +return; } int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat) diff --git a/13_day/timer.c b/13_day/timer.c index 5c46b22..a8977d4 100644 --- a/13_day/timer.c +++ b/13_day/timer.c @@ -43,7 +43,7 @@ void timer_free(struct TIMER *timer) return; } -void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data) +void timer_init(struct TIMER *timer, struct FIFO32 *fifo, unsigned char data) { timer->fifo = fifo; timer->data = data; @@ -91,7 +91,7 @@ void inthandler20(int *esp) } /* 超时*/ timerctl.timers[i]->flags = TIMER_FLAGS_ALLOC; - fifo8_put(timerctl.timers[i]->fifo, timerctl.timers[i]->data); + fifo32_put(timerctl.timers[i]->fifo, timerctl.timers[i]->data); } /* 正好有i个定时器超时了。其余的进行移位。 */ timerctl.using -= i;