From 3feca3e420db7ce6d57b09ba8da69037ea41245c Mon Sep 17 00:00:00 2001 From: pavelyakov Date: Fri, 29 May 2020 09:37:24 +0000 Subject: [PATCH] Addition new functions Timers git-svn-id: svn://kolibrios.org@7997 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/lib/timers.c | 100 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 programs/cmm/lib/timers.c diff --git a/programs/cmm/lib/timers.c b/programs/cmm/lib/timers.c new file mode 100644 index 0000000000..9baaf56623 --- /dev/null +++ b/programs/cmm/lib/timers.c @@ -0,0 +1,100 @@ +/* TIMERS PaulCodeman */ + +#define offsetSizeTimers 4*3+1 +#define defaultMaxTimers 1000 +:struct Timers +{ + dword time; + dword alloc; + dword count; + dword size; + void watch(void); + void getTime(); + dword set(dword,dword,byte); + dword clear(dword); +}; +void Timers::getTime(void) +{ + EAX = 26; + EBX = 9; + $int 0x40 + time = EAX; +} +void Timers::watch(void) +{ + dword position = 0; + dword i = 0; + IF (!alloc) RETURN; + getTime(); + i = count; + position = alloc; + WHILE(i) + { + IF (DSDWORD[position]) + { + IF (DSDWORD[position+4]<=time) + { + $call DSDWORD[position]; + IF (DSBYTE[position+12]) DSDWORD[position+4] = time+DSDWORD[position+8]; + ELSE + { + DSDWORD[position] = 0; + count--; + } + } + i--; + } + position+=offsetSizeTimers; + } +} +dword Timers::set(dword function, newTime, byte repeat) +{ + dword position = 0; + dword i = 0; + IF (!alloc) + { + size = defaultMaxTimers*offsetSizeTimers; + alloc = malloc(size); + } + i = count; + position = alloc; + WHILE(i) + { + IF (!DSDWORD[position]) BREAK; + position+=offsetSizeTimers; + i--; + } + count++; + getTime(); + DSDWORD[position] = function; + DSDWORD[position+4] = time+newTime; + DSBYTE[position+8] = newTime; + DSBYTE[position+12] = repeat; + RETURN position; +} +dword Timers::clear(dword id) +{ + IF (!alloc) || (!DSDWORD[id]) RETURN 0; + count--; + DSDWORD[id] = 0; + RETURN id; +} + +// Analogs JS Functions +:Timers Time = {0}; +inline dword setTimeout(dword function, time) +{ + RETURN Time.set(function, time, 0); +} +inline dword setInterval(dword function, time) +{ + RETURN Time.set(function, time, 1); +} +inline dword clearTimeout(dword id) +{ + RETURN Time.clear(id); +} +inline dword clearInterval(dword id) +{ + RETURN Time.clear(id); +}