forked from semerad/gt3b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
98 lines (78 loc) · 2.09 KB
/
task.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
task - cooperative multitasking
Copyright (C) 2011 Pavel Semerad
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "task.h"
// main OPER task
TCB OPER;
// pointer to current task
TCB *ptid;
// initialise tasks
void task_init(void) {
// initialize current task id
ptid = &OPER;
// OPER is running -> running task has ASLEEP state
sleep(OPER);
// initialize LINK in OPER task
OPER.link = &OPER;
}
// activate task - set pointers and AWAKE
void _do_activate(TCB *task, u8 *stack, u16 stack_size,
void (*function)(void)) {
u8 *stack_ret = stack + stack_size - 2;
*(u16 *)stack_ret = (u16)function;
task->hwstack = stack_ret - 1; // SP points below last value
task->status = _AWAKE;
}
// build - link in task
void _do_build(TCB *task) {
task->status = _ASLEEP; // do not run this yet
task->link = OPER.link;
OPER.link = task;
}
// pause current task and try to run another one
void pause(void) {
#asm
; set _AWAKE
ldw X, _ptid
ld A, #255
ld (X), A
_pause_stop:
; save HW stack pointer
ldw Y, SP
ldw (3, X), Y
; find next _AWAKE task - if none, loop
__xpause:
ldw X, (1, X)
ld A, (X)
jreq __xpause ; skip _ASLEEP task
; _AWAKE task found, restore HW stack pointer
ldw Y, X
ldw X, (3, X)
sim
ldw SP, X
rim
; set _ASLEEP - current task has this state
clr A
ld (Y), A
; set ptid to this task
ldw _ptid, Y
#endasm
}
// stop current task and try to run another one
void stop(void) {
#asm
ldw X, _ptid
jra _pause_stop
#endasm
}