Compare commits

...

2 Commits

Author SHA1 Message Date
Yourtion
9e1c17b4d1 设定多个定时器 2016-04-18 14:30:57 +08:00
Yourtion
b50c16d8e0 超时功能 2016-04-18 14:06:26 +08:00
3 changed files with 100 additions and 4 deletions

View File

@@ -8,7 +8,9 @@ void make_window8(unsigned char *buf, int xsize, int ysize, char *title);
void HariMain(void)
{
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
char s[40], keybuf[32], mousebuf[128];
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;
@@ -20,13 +22,25 @@ 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) */
io_out8(PIC1_IMR, 0xef); /* 开放鼠标中断(11101111) */
fifo8_init(&timerfifo, 8, timerbuf);
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);
memtotal = memtest(0x00400000, 0xbfffffff);
@@ -68,7 +82,7 @@ void HariMain(void)
sheet_refresh(sht_win, 40, 28, 120, 44); /* 到这里结束 */
io_cli();
if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) == 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) {
@@ -117,6 +131,28 @@ void HariMain(void)
sheet_refresh(sht_back, 0, 0, 80, 16); /* 刷新文字 */
sheet_slide(sht_mouse, mx, my); /* 包含sheet_refresh含sheet_refresh */
}
} else if (fifo8_status(&timerfifo) != 0) {
i = fifo8_get(&timerfifo); /* 首先读入(为了设定起始点) */
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);
}
}
}

View File

@@ -169,10 +169,22 @@ void sheet_slide(struct SHEET *sht, int vx0, int vy0);
void sheet_free(struct SHEET *sht);
/* timer.c */
#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);

View File

@@ -4,21 +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)
{
io_out8(PIT_CTRL, 0x43);
int i;
io_out8(PIT_CTRL, 0x34);
io_out8(PIT_CNT0, 0x9c);
io_out8(PIT_CNT0, 0x2e);
timerctl.count = 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++;
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;
}