Compare commits

..

2 Commits

Author SHA1 Message Date
Yourtion
efb6028e00 取消定时器 2016-05-11 15:32:26 +08:00
Yourtion
9475d3d828 定时器API 2016-05-11 15:24:10 +08:00
6 changed files with 152 additions and 3 deletions

View File

@@ -148,10 +148,17 @@ walk.bim : walk.obj a_nask.obj Makefile
walk.hrb : walk.bim Makefile
$(BIM2HRB) walk.bim walk.hrb 48k
noodle.bim : noodle.obj a_nask.obj Makefile
$(OBJ2BIM) @$(RULEFILE) out:noodle.bim stack:1k map:noodle.map \
noodle.obj a_nask.obj
noodle.hrb : noodle.bim Makefile
$(BIM2HRB) noodle.bim noodle.hrb 40k
haribote.img : ipl10.bin haribote.sys Makefile \
hello.hrb hello2.hrb a.hrb hello3.hrb hello4.hrb hello5.hrb \
winhelo.hrb winhelo2.hrb winhelo3.hrb star1.hrb stars.hrb stars2.hrb \
lines.hrb walk.hrb
lines.hrb walk.hrb noodle.hrb
$(EDIMG) imgin:../z_tools/fdimg0at.tek \
wbinimg src:ipl10.bin len:512 from:0 to:0 \
copy from:haribote.sys to:@: \
@@ -171,6 +178,7 @@ haribote.img : ipl10.bin haribote.sys Makefile \
copy from:stars2.hrb to:@: \
copy from:lines.hrb to:@: \
copy from:walk.hrb to:@: \
copy from:noodle.hrb to:@: \
imgout:haribote.img
# 其他指令

View File

@@ -17,6 +17,10 @@
GLOBAL _api_linewin
GLOBAL _api_closewin
GLOBAL _api_getkey
GLOBAL _api_alloctimer
GLOBAL _api_inittimer
GLOBAL _api_settimer
GLOBAL _api_freetimer
[SECTION .text]
@@ -186,3 +190,34 @@ _api_getkey: ; int api_getkey(int mode);
MOV EAX,[ESP+4] ; mode
INT 0x40
RET
_api_alloctimer: ; int api_alloctimer(void);
MOV EDX,16
INT 0x40
RET
_api_inittimer: ; void api_inittimer(int timer, int data);
PUSH EBX
MOV EDX,17
MOV EBX,[ESP+ 8] ; timer
MOV EAX,[ESP+12] ; data
INT 0x40
POP EBX
RET
_api_settimer: ; void api_settimer(int timer, int time);
PUSH EBX
MOV EDX,18
MOV EBX,[ESP+ 8] ; timer
MOV EAX,[ESP+12] ; time
INT 0x40
POP EBX
RET
_api_freetimer: ; void api_freetimer(int timer);
PUSH EBX
MOV EDX,19
MOV EBX,[ESP+ 8] ; timer
INT 0x40
POP EBX
RET

View File

@@ -176,7 +176,8 @@ void sheet_free(struct SHEET *sht);
#define MAX_TIMER 500
struct TIMER {
struct TIMER *next;
unsigned int timeout, flags;
unsigned int timeout;
char flags, flags2;
struct FIFO32 *fifo;
int data;
};
@@ -192,6 +193,8 @@ void timer_free(struct TIMER *timer);
void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data);
void timer_settime(struct TIMER *timer, unsigned int timeout);
void inthandler20(int *esp);
int timer_cancel(struct TIMER *timer);
void timer_cancelall(struct FIFO32 *fifo);
/* mtask.c */
#define MAX_TASKS 1000 /*最大任务数量*/

View File

@@ -308,6 +308,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
sheet_free(sht); /*关闭*/
}
}
timer_cancelall(&task->fifo);
memman_free_4k(memman, (int) q, segsiz);
} else {
cons_putstr0(cons, ".hrb file format error.\n");
@@ -414,11 +415,20 @@ int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int
if (i == 3) { /*光标OFF */
cons->cur_c = -1;
}
if (256 <= i && i <= 511) { /*键盘数据通过任务A*/
if (i >= 256) { /*键盘数据通过任务A*/
reg[7] = i - 256;
return 0;
}
}
} else if (edx == 16) {
reg[7] = (int) timer_alloc();
((struct TIMER *) reg[7])->flags2 = 1; /*允许自动取消*/
} else if (edx == 17) {
timer_init((struct TIMER *) ebx, &task->fifo, eax + 256);
} else if (edx == 18) {
timer_settime((struct TIMER *) ebx, eax);
} else if (edx == 19) {
timer_free((struct TIMER *) ebx);
}
return 0;
}

42
24_day/noodle.c Normal file
View File

@@ -0,0 +1,42 @@
#include <stdio.h>
int api_openwin(char *buf, int xsiz, int ysiz, int col_inv, char *title);
void api_putstrwin(int win, int x, int y, int col, int len, char *str);
void api_boxfilwin(int win, int x0, int y0, int x1, int y1, int col);
void api_initmalloc(void);
char *api_malloc(int size);
int api_getkey(int mode);
int api_alloctimer(void);
void api_inittimer(int timer, int data);
void api_settimer(int timer, int time);
void api_end(void);
void HariMain(void)
{
char *buf, s[12];
int win, timer, sec = 0, min = 0, hou = 0;
api_initmalloc();
buf = api_malloc(150 * 50);
win = api_openwin(buf, 150, 50, -1, "noodle");
timer = api_alloctimer();
api_inittimer(timer, 128);
for (;;) {
sprintf(s, "%5d:%02d:%02d", hou, min, sec);
api_boxfilwin(win, 28, 27, 115, 41, 7);/*白色*/
api_putstrwin(win, 28, 27, 0, 11, s); /*黑色*/
api_settimer(timer, 100); /* 1秒 */
if (api_getkey(1) != 128) {
break;
}
sec++;
if (sec == 60) {
sec = 0;
min++;
if (min == 60) {
min = 0;
hou++;
}
}
}
api_end();
}

View File

@@ -36,6 +36,7 @@ struct TIMER *timer_alloc(void)
for (i = 0; i < MAX_TIMER; i++) {
if (timerctl.timers0[i].flags == 0) {
timerctl.timers0[i].flags = TIMER_FLAGS_ALLOC;
timerctl.timers0[i].flags2 = 0;
return &timerctl.timers0[i];
}
}
@@ -116,3 +117,53 @@ void inthandler20(int *esp)
}
return;
}
int timer_cancel(struct TIMER *timer)
{
int e;
struct TIMER *t;
e = io_load_eflags();
io_cli(); /*在设置过程中禁止改变定时器状态*/
if (timer->flags == TIMER_FLAGS_USING) { /*是否需要取消?*/
if (timer == timerctl.t0) {
/*第一个定时器的取消处理*/
t = timer->next;
timerctl.t0 = t;
timerctl.next = t->timeout;
} else {
/*非第一个定时器的取消处理*/
/*找到timer前一个定时器*/
t = timerctl.t0;
for (;;) {
if (t->next == timer) {
break;
}
t = t->next;
}
t->next = timer->next;
/*将之前“timer的下一个”指向“timer的下一个”*/
}
timer->flags = TIMER_FLAGS_ALLOC;
io_store_eflags(e);
return 1; /*取消处理成功*/
}
io_store_eflags(e);
return 0; /*不需要取消处理*/
}
void timer_cancelall(struct FIFO32 *fifo)
{
int e, i;
struct TIMER *t;
e = io_load_eflags();
io_cli(); /*在设置过程中禁止改变定时器状态*/
for (i = 0; i < MAX_TIMER; i++) {
t = &timerctl.timers0[i];
if (t->flags != 0 && t->flags2 != 0 && t->fifo == fifo) {
timer_cancel(t);
timer_free(t);
}
}
io_store_eflags(e);
return;
}