3 ms·
Preemption just means the task is stopped when its timeslice elapses, and a new task selected. You can do this on any CPU with a regular timer interrupt. On som
by to3m 11y ago
Preemption just means the task is stopped when its timeslice elapses, and a new task selected. You can do this on any CPU with a regular timer interrupt. On something like the ZX Spectrum you'll need to use the vblank interrupt; on something with a programmable timer, you can make the timing tighter.
(In either case, you'll probably want some mechanism for tasks to relinquish their timeslice early; no reason not to make multitasking run better when tasks cooperate, even if it doesn't absolutely require that.)
To do the actual task switch, do exactly what you do on any system:
- save register state in tasks's register area (the state will be available on entry to the interrupt routine, in the usual fashion. The task pointer will be a system global at a fixed address)
- set timer for one timeslice hence (timers probably count down to zero, so this will just be a question of putting a value somewhere into an I/O register)
- resumes task by jumping to the program counter value from the task's register area
- GOTO 10...
(The complications come on something like the 6502, which has a fixed-size, fixed-address stack - but this it hard task-switching difficult, not the preemption! How I imagine CLIPS gets around this: it requires CMD SuperCPU, which is a 65816. Like the Z80 and 6809, but unlike the 6502, the 65816 has a relocatable stack. So it will probably behave as I describe above.)
- cmrdporcupine 11y ago816 has a relocatable stack and direct(nee zero) page, so that's good. Unfortunately both of those still have to be in the lower 64k bank. So that's a bit frustrating for trying to write OS support for multitasking larger memory systems.