This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitask-cooperative.s
356 lines (269 loc) · 10 KB
/
multitask-cooperative.s
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# ------------------------------------------------------------------------------
# Cooperative multitasking essentials
# Matthias Koch, 2023
# ------------------------------------------------------------------------------
.option rvc
# ------------------------------------------------------------------------------
# A few useful macro definitions
# ------------------------------------------------------------------------------
.macro push register
addi x2, x2, -4
sw \register, 0(x2)
.endm
.macro pop register
lw \register, 0(x2)
addi x2, x2, 4
.endm
# ------------------------------------------------------------------------------
.text # Flash memory, execution begins here
# ------------------------------------------------------------------------------
j Reset
# Include a few hardware specific parts.
# Have a look if you like, but these are not necessary to understand the
# essentials of multitasking.
.include "hardware-leds.s"
# ------------------------------------------------------------------------------
delay_cycles: # Waits at least the number of cycles specified in x10.
# ------------------------------------------------------------------------------
push x1
rdcycle x15 # Get start time
1:call pause # Let other tasks run while we wait <-- Important!
rdcycle x14 # Get current time
sub x14, x14, x15 # Elapsed time, handles overflow gracefully
bltu x14, x10, 1b
pop x1
ret
# ------------------------------------------------------------------------------
# Definition of the task area memory
# ------------------------------------------------------------------------------
/*
Internal stucture of task memory:
0: Pointer to next task
4: Task currently active
8: Saved stack pointer
... Add more task specific variables as desired ...
Growing from the end: Stack
*/
.equ taskareasize, 512
.equ offset_nexttask, 0
.equ offset_taskactive, 4
.equ offset_savedstack, 8
# ------------------------------------------------------------------------------
pause: # Switch tasks here.
# ------------------------------------------------------------------------------
# Push all registers less the stack pointer to the stack
addi x2, x2, -30*4
sw x1, 29*4(x2)
sw x3, 28*4(x2)
sw x4, 27*4(x2)
sw x5, 26*4(x2)
sw x6, 25*4(x2)
sw x7, 24*4(x2)
sw x8, 23*4(x2)
sw x9, 22*4(x2)
sw x10, 21*4(x2)
sw x11, 20*4(x2)
sw x12, 19*4(x2)
sw x13, 18*4(x2)
sw x14, 17*4(x2)
sw x15, 16*4(x2)
sw x16, 15*4(x2)
sw x17, 14*4(x2)
sw x18, 13*4(x2)
sw x19, 12*4(x2)
sw x20, 11*4(x2)
sw x21, 10*4(x2)
sw x22, 9*4(x2)
sw x23, 8*4(x2)
sw x24, 7*4(x2)
sw x25, 6*4(x2)
sw x26, 5*4(x2)
sw x27, 4*4(x2)
sw x28, 3*4(x2)
sw x29, 2*4(x2)
sw x30, 1*4(x2)
sw x31, 0*4(x2)
# All registers are saved now, so we can use the registers freely.
la x13, current_task # Variable pointing to the currently active task
lw x14, 0(x13) # Get pointer to the currently active task
sw x2, offset_savedstack(x14) # Save stack pointer into task structure
# All context saved. Now time for selecting another task to run.
# This is a very simple round-robin scheduler:
next_task:
lw x14, offset_nexttask(x14) # Get pointer to next task
lw x15, offset_taskactive(x14) # Check if task is active
beq x15, zero, next_task # If none of the tasks is active, this becomes an endless loop.
# Found next task to execute.
sw x14, 0(x13) # Take a note of the soon-active new task.
# Restore context to activate the new task
lw x2, offset_savedstack(x14) # Get stack pointer from the task structure
# Pop all registers less the stack pointer from the stack
lw x31, 0*4(x2)
lw x30, 1*4(x2)
lw x29, 2*4(x2)
lw x28, 3*4(x2)
lw x27, 4*4(x2)
lw x26, 5*4(x2)
lw x25, 6*4(x2)
lw x24, 7*4(x2)
lw x23, 8*4(x2)
lw x22, 9*4(x2)
lw x21, 10*4(x2)
lw x20, 11*4(x2)
lw x19, 12*4(x2)
lw x18, 13*4(x2)
lw x17, 14*4(x2)
lw x16, 15*4(x2)
lw x15, 16*4(x2)
lw x14, 17*4(x2)
lw x13, 18*4(x2)
lw x12, 19*4(x2)
lw x11, 20*4(x2)
lw x10, 21*4(x2)
lw x9, 22*4(x2)
lw x8, 23*4(x2)
lw x7, 24*4(x2)
lw x6, 25*4(x2)
lw x5, 26*4(x2)
lw x4, 27*4(x2)
lw x3, 28*4(x2)
lw x1, 29*4(x2)
addi x2, x2, 30*4
ret
# ------------------------------------------------------------------------------
preparetask: # x10: Task area x11: Entry address x12: Active flag
# ------------------------------------------------------------------------------
# In order to prepare the task area, we need to do a "context switch"
# in reverse, so that the next task switch will launch the routine given.
# One could politely clear the task area first here,
# but it is not strictly necessary, and could be used for passing parameters.
# Prepare stack pointer to hold x1 and x3 to x31.
add x14, x10, taskareasize - 30*4
sw x14, offset_savedstack(x10)
# The initial stack for the next task will be empty after
# "restoring" its registers.
# Write the entry address to the location in the stack that
# will be popped into return address x1 at the upcoming context switch.
sw x11, taskareasize-4(x10)
# Set the task flag to either "active" or "inactive".
# One could insert a task as "inactive", and activate it by a trigger,
# for example in an interrupt handler later.
sw x12, offset_taskactive(x10)
# Insert the task into the ring of tasks.
la x14, current_task
lw x14, 0(x14)
lw x15, offset_nexttask(x14) # Get the old next task of the currently active task
sw x15, offset_nexttask(x10) # Save that one as the next task of the freshly prepared one
sw x10, offset_nexttask(x14) # Insert the freshly prepared one into the ring to be executed next
# The insertion in the ring structure isn't atomic,
# so do not try to insert (or remove) a task in an interrupt handler.
# Done. The new one will be run on the next context switch.
ret
# ------------------------------------------------------------------------------
wake: # x10: Task area Idle a specific task. IRQ safe.
# ------------------------------------------------------------------------------
li x15, -1
sw x15, offset_taskactive(x10)
ret
# ------------------------------------------------------------------------------
idle: # x10: Task area Wake a specific task. IRQ safe.
# ------------------------------------------------------------------------------
sw zero, offset_taskactive(x10)
ret
# ------------------------------------------------------------------------------
Reset: # First routine to run
# ------------------------------------------------------------------------------
# Prepare stack pointer.
# It can be everywhere, but for keeping the example tidy,
# use a proper task area format for what will become the "boot task" also.
la x2, boot_taskarea + taskareasize
# Before one can enter multitasking, we need to create a task area entry
# for the "boot task". This one is already running, so we cannot use preparetask
# but we need to initialise the ring structure of the tasks:
la x14, boot_taskarea # The area of the boot task
sw x14, offset_nexttask(x14) # With only one task, close the ring by pointing back to itself.
la x15, current_task
sw x14, 0(x15)
# Multitasking is now ready to fly, and you can start to call pause now without
# any nasty side effects. Lets try:
call pause
# But as this one is the only task known at the moment, control is transfered back to here.
# ------------------------------------------------------------------------------
# Lets initialise the hardware and define
# a few tasks to do something interesting:
call init_leds
la x10, blink_red_taskarea
la x11, blink_red
li x12, -1 # Active
call preparetask
la x10, blink_green_taskarea
la x11, blink_green
li x12, -1 # Active
call preparetask
la x10, blink_blue_taskarea
la x11, blink_blue
li x12, 0 # Inactive
call preparetask
# Done! We have four tasks now, including the boot task.
# ------------------------------------------------------------------------------
# Let start blinking now. After 10 seconds, the blue LED joins.
li x10, 10000 * CYCLES_MS
call delay_cycles
la x10, blink_blue_taskarea
call wake
mainloop:
# For this example, the boot task finished its work,
# so it just switches context, and repeats.
call pause
j mainloop
# ------------------------------------------------------------------------------
blink_red: # No parameters, will be executed as separate task.
# ------------------------------------------------------------------------------
li x10, 100 * CYCLES_MS
call delay_cycles
call red_on
li x10, 220 * CYCLES_MS
call delay_cycles
call red_off
j blink_red # Tasks must not return.
# ------------------------------------------------------------------------------
blink_green: # No parameters, will be executed as separate task.
# ------------------------------------------------------------------------------
li x10, 200 * CYCLES_MS
call delay_cycles
call green_on
li x10, 320 * CYCLES_MS
call delay_cycles
call green_off
j blink_green # Tasks must not return.
# ------------------------------------------------------------------------------
blink_blue: # No parameters, will be executed as separate task.
# ------------------------------------------------------------------------------
li x10, 300 * CYCLES_MS
call delay_cycles
call blue_on
li x10, 540 * CYCLES_MS
call delay_cycles
call blue_off
j blink_blue # Tasks must not return.
# ------------------------------------------------------------------------------
.bss # RAM memory layout
# ------------------------------------------------------------------------------
boot_taskarea:
.rept taskareasize
.byte 0x00
.endr
blink_red_taskarea:
.rept taskareasize
.byte 0x00
.endr
blink_green_taskarea:
.rept taskareasize
.byte 0x00
.endr
blink_blue_taskarea:
.rept taskareasize
.byte 0x00
.endr
current_task:
.word 0x00000000