Multi-Tasking Menu >Using a simple timer (e.g., 68HC11/12 s RTI or XMEGA s RTC) Outline the steps in multitasking Multitask with single process Simulate single process example >68HC11/12: RTI and TOF together for multitasking Look into my... See examples on web-site: RTI_Step.asm, RTI_1Process.asm, RTI_Mult_Proc.asm, RTI_Mult_Proc2.asm, RTI_Mult_TOF.asm 1 Multi-Tasking: Saving Context When any single running process is paused, its context must be saved >Context is the entire state of a process; it must contain all of the information necessary to return to the process after the interruption When a process is resumed, the context is restored; thus the only thing that should have changed with respect to the process is that time will have advanced 2 1
Stack Usage by the Interrupt (e.g., RTI) >For the XMEGA, RTC is equivalent, but TC can also be used Since XMEGA interrupts push nothing other than PC onto the stack, you will need to do this yourself Stack Pointer After Interrupt Stack Pointer Before Interrupt CCR ACCB ACCA IX H IX L IY H IY L PC H PC L 3 P1: Context for P1 includes P2: Context for P2 includes PN: 4 2
Assume P1 is running and RTI interrupts P1: Context for P1 includes Inside RTI_ISR the stack contains: CCR, B, A, IX, IY & PC for P1 Let s assume RTI_ISR knows that PID=1 Save SP (for P1) into SP 1, i.e., SP SP 1 Then if RTI_ISR wants to go to P2 Let SP SP 2 (SP for P2) Change PID to correspond to P2 RTI_ISR clears RTIF, executes RTI inst. This restores the stack of P2 5 Now P2 is running and RTI interrupts P2: Context for P2 includes Inside RTI_ISR the stack contains: CCR, B, A, IX, IY & PC for P2 Let s assume RTI_ISR knows that PID=2 Save SP (for P2) into SP 2, i.e., SP SP 2 Then if RTI_ISR wants to go to P i Let SP SP i (SP for P i ) Change PID to correspond to P i RTI_ISR clears RTIF, executes RTI inst. This restores the stack of Pi 6 3
Assume PN is running and RTI interrupts PN: Context for PN includes Inside RTI_ISR the stack contains: CCR, B, A, IX, IY & PC for PN Let s assume RTI_ISR knows that PID=n Save SP (for PN) into SP N, i.e., SP SP N Then if RTI_ISR wants to go back to P1 Let SP SP 1 (SP for P1) Change PID to correspond to P1 RTI_ISR clears RTIF, executes RTI inst. This restores the stack of P1 7 P1: Context for P1 includes Q: How do we get things started? A: In the main program: Setup RTI interrupt vector ($FFF0) or pseudo vector ($00EE) Setup variables & constants Create a dummy stack for each process: CCR, B, A, IX, IY, & PC (entry point for P1) Setup RTI system Setup any global variables Enable interrupts Jump to the first process you want to run 8 4
Multi-Tasking needs to allocate PID (Process ID), Stack, and Stack Pointer for each Process. Assume PID = 1 and PS1 is running with Stack1. PS2 is running with Stack2 until the next RTI. Interrupt by RTI (Save the current status of PS1 into Stack1 [automatic]) Return from Interrupt (Restore the previous status of PS2 from Stack2 [automatic]) Inside ISR Clear RTI Flag Update SP1 because PID = 1 Choose PS2 as the next process Set SP = SP2 Set PID = 2 9 Multi-Tasking Examples > Outline the steps > Do it with single process >Simulate single process RTI_Step.asm RTI_1Process.asm > A multitasking example > Another multitasking example >RTI with TOF (i.e., two interrupts working together): RTI_Mult_Proc.asm RTI_Mult_TOF.asm RTI_Mult_Proc2.asm 10 5
The End! 11 6