From 9e1c17b4d16a56cdc9e12b48e8a5849e86cd8b5c Mon Sep 17 00:00:00 2001 From: Yourtion Date: Mon, 18 Apr 2016 14:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=AE=9A=E5=A4=9A=E4=B8=AA=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 12_day/bootpack.c | 37 ++++++++++++++++++++++++---- 12_day/bootpack.h | 16 +++++++++--- 12_day/timer.c | 63 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 90 insertions(+), 26 deletions(-) diff --git a/12_day/bootpack.c b/12_day/bootpack.c index 1894d26..6d8385c 100644 --- a/12_day/bootpack.c +++ b/12_day/bootpack.c @@ -8,8 +8,9 @@ void make_window8(unsigned char *buf, int xsize, int ysize, char *title); void HariMain(void) { struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO; - struct FIFO8 timerfifo; - char s[40], keybuf[32], mousebuf[128], timerbuf[8]; + struct FIFO8 timerfifo, timerfifo2, timerfifo3; + char s[40], keybuf[32], mousebuf[128], timerbuf[8], timerbuf2[8], timerbuf3[8]; + struct TIMER *timer, *timer2, *timer3; int mx, my, i; unsigned int memtotal; struct MOUSE_DEC mdec; @@ -28,8 +29,17 @@ void HariMain(void) io_out8(PIC1_IMR, 0xef); /* 开放鼠标中断(11101111) */ fifo8_init(&timerfifo, 8, timerbuf); - settimer(1000, &timerfifo, 1); - + timer = timer_alloc(); + timer_init(timer, &timerfifo, 1); + timer_settime(timer, 1000); + fifo8_init(&timerfifo2, 8, timerbuf2); + timer2 = timer_alloc(); + timer_init(timer2, &timerfifo2, 1); + timer_settime(timer2, 300); + fifo8_init(&timerfifo3, 8, timerbuf3); + timer3 = timer_alloc(); + timer_init(timer3, &timerfifo3, 1); + timer_settime(timer3, 50); init_keyboard(); enable_mouse(&mdec); @@ -72,7 +82,7 @@ void HariMain(void) sheet_refresh(sht_win, 40, 28, 120, 44); /* 到这里结束 */ io_cli(); - if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) + fifo8_status(&timerfifo) == 0) { + if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) + fifo8_status(&timerfifo) + fifo8_status(&timerfifo2) + fifo8_status(&timerfifo3)== 0) { io_sti(); /* 不做HLT */ } else { if (fifo8_status(&keyfifo) != 0) { @@ -126,6 +136,23 @@ void HariMain(void) io_sti(); putfonts8_asc(buf_back, binfo->scrnx, 0, 64, COL8_FFFFFF, "10[sec]"); sheet_refresh(sht_back, 0, 64, 56, 80); + } else if (fifo8_status(&timerfifo2) != 0) { + i = fifo8_get(&timerfifo2); /* 首先读入(为了设定起始点) */ + io_sti(); + putfonts8_asc(buf_back, binfo->scrnx, 0, 80, COL8_FFFFFF, "3[sec]"); + sheet_refresh(sht_back, 0, 80, 48, 96); + } else if (fifo8_status(&timerfifo3) != 0) { + i = fifo8_get(&timerfifo3); /* 首先读入(为了设定起始点) */ + io_sti(); + if (i != 0) { + timer_init(timer3, &timerfifo3, 0); /* 然后设置0 */ + boxfill8(buf_back, binfo->scrnx, COL8_FFFFFF, 8, 96, 15, 111); + } else { + timer_init(timer3, &timerfifo3, 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/12_day/bootpack.h b/12_day/bootpack.h index d6c2c6f..f7a1a01 100644 --- a/12_day/bootpack.h +++ b/12_day/bootpack.h @@ -169,14 +169,22 @@ void sheet_slide(struct SHEET *sht, int vx0, int vy0); void sheet_free(struct SHEET *sht); /* timer.c */ -struct TIMERCTL { - unsigned int count; - unsigned int timeout; +#define MAX_TIMER 500 +struct TIMER { + unsigned int timeout, flags; struct FIFO8 *fifo; unsigned char data; }; +struct TIMERCTL { + unsigned int count; + struct TIMER timer[MAX_TIMER]; +}; 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_settime(struct TIMER *timer, unsigned int timeout); void inthandler20(int *esp); -void settimer(unsigned int timeout, struct FIFO8 *fifo, unsigned char data); + diff --git a/12_day/timer.c b/12_day/timer.c index e014a96..0f384d6 100644 --- a/12_day/timer.c +++ b/12_day/timer.c @@ -4,40 +4,69 @@ #define PIT_CTRL 0x0043 #define PIT_CNT0 0x0040 +#define TIMER_FLAGS_ALLOC 1 /* 已配置状态 */ +#define TIMER_FLAGS_USING 2 /* 定时器运行中 */ struct TIMERCTL timerctl; void init_pit(void) { + int i; io_out8(PIT_CTRL, 0x34); io_out8(PIT_CNT0, 0x9c); io_out8(PIT_CNT0, 0x2e); timerctl.count = 0; - timerctl.timeout = 0; + for (i = 0; i < MAX_TIMER; i++) { + timerctl.timer[i].flags = 0; /* 未使用 */ + } + return; +} + +struct TIMER *timer_alloc(void) +{ + int i; + for (i = 0; i < MAX_TIMER; i++) { + if (timerctl.timer[i].flags == 0) { + timerctl.timer[i].flags = TIMER_FLAGS_ALLOC; + return &timerctl.timer[i]; + } + } + return 0; /* 没找到 */ +} + +void timer_free(struct TIMER *timer) +{ + timer->flags = 0; /* 未使用 */ + return; +} + +void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data) +{ + timer->fifo = fifo; + timer->data = data; + return; +} + +void timer_settime(struct TIMER *timer, unsigned int timeout) +{ + timer->timeout = timeout; + timer->flags = TIMER_FLAGS_USING; return; } void inthandler20(int *esp) { + int i; io_out8(PIC0_OCW2, 0x60); /* 把IRQ-00信号接收完了的信息通知给PIC */ timerctl.count++; - if (timerctl.timeout > 0) { /* 如果已经设定了超时 */ - timerctl.timeout--; - if (timerctl.timeout == 0) { - fifo8_put(timerctl.fifo, timerctl.data); + for (i = 0; i < MAX_TIMER; i++) { + if (timerctl.timer[i].flags == TIMER_FLAGS_USING) { + timerctl.timer[i].timeout--; + if (timerctl.timer[i].timeout == 0) { + timerctl.timer[i].flags = TIMER_FLAGS_ALLOC; + fifo8_put(timerctl.timer[i].fifo, timerctl.timer[i].data); + } } } return; } - -void settimer(unsigned int timeout, struct FIFO8 *fifo, unsigned char data) -{ - int eflags; - eflags = io_load_eflags(); - io_cli(); - timerctl.timeout = timeout; - timerctl.fifo = fifo; - timerctl.data = data; - io_store_eflags(eflags); - return; -}