; Chapter 5 9S12C32 assembly language programs ; Jonathan W. Valvano, 2/07/07 ; This software accompanies the book, ; Embedded Microcomputer Systems: Real Time Interfacing, Second Edition ; published by Thomson Engineering, 2006 ; ; Program 5.1. Assembly code that statically allocates three threads. TCB1 fdb TCB2 fdb IS1 fcb 1 rmb 49 IS1 fcb $40 fdb 0,0,0 fdb ProgA TCB2 fdb TCB3 fdb IS2 fcb 2 rmb 49 IS2 fcb $40 fdb 0,0,0 fdb ProgA TCB3 fdb TCB1 link fdb IS3 SP fcb 4 Id rmb 49 IS3 fcb $40 CCR fdb 0,0,0 DXY fdb ProgB PC ; Program 5.2. Assembly code that implements the preemptive thread switcher. Next equ 0 ;pointer to next TCB SP equ 2 ;Stack pointer for this thread Id equ 4 ;Used to visualize which thread is current running RunPt rmb 2 ;pointer to thread that is currently running Main ldaa #$FF staa DDRT ;PTT displays which thread is executing staa DDRM ;PTM displays which program is executing ldx #TCB1 ;First thread to run jmp Start ; Suspend thread which is currently running OC5Han ldx RunPt sts SP,x ;save Stack Pointer in TCB ; launch next thread ldx Next,x Start stx RunPt ldaa Id,x staa PTT ;visualizes running thread lds SP,x ;set SP for this new thread ldd TC5 addd #20000 ;interrupts every 10 ms std TC5 ldaa #$20 staa TFLG1 ;acknowledge OC5 rti ; Program 5.3. Assembly code for the two main programs and shared subroutine. ProgA pshx tsx ldd #5 std 0,x LoopA ldaa #2 staa PTM ldd 0,x jsr sub std 0,x bra LoopA ProgB pshx tsx ldd #5 std 0,x LoopB ldaa #4 staa PTM ldd 0,x jsr sub std 0,x bra LoopB Sub pshx tsx std 0,x ldaa #1 staa PTM ldd 0,x addd #1 pulx rts ;assembly implementation S fcb 1 ;semaphore, initialized to 1 ; spin if zero, otherwise decrement wait sei ;read-modify-write atomic ldaa S ;value of semaphore bhi OK ;available if >0 cli bra wait ;**interrupts occur here** OK deca staa S ;S=S-1 cli rts ;increment semaphore signal inc S ;S=S+1, this is atomic rts ;Program 5.8. A spinlock counting semaphore. ; Program 5.9. 6812 assembly code for a spinlock binary semaphore. S fcb 1 ;semaphore flag initialized to 1 bWait clra ;new value loop minm S ;in either case S is now 0, carry set if S used to 1 bcc loop ;loop until S=1 rts bSignal ldaa #1 staa S ;S=1 rts ; Program 5.12. Assembly code to initialize a blocking semaphore. S rmb 1 ;semaphore counter BlockPt rmb 2 ;Pointer to threads blocked on S Init tpa psha ;Save old value of I sei ;Make atomic ldaa #1 staa S ;Init semaphore value ldx #Null stx BlockPt ;empty list pula tap ;Restore old value of I rts ; Program 5.13. Assembly code helper function to block a thread, used to implement a blocking semaphore. ; To block a thread on semaphore S, execute SWI SWIhan ldx RunPt ;running process "to be blocked" sts SP,x ;save Stack Pointer in its TCB ; Unlink "to be blocked" thread from RunPt list ldy Next,x ;find previous thread sty RunPt ;next one to run look cpx Next,y ;search to find previous beq found ldy Next,y bra look found ldd RunPt ;one after blocked std Next,y ;link previous to next to run ; Put "to be blocked" thread on block list ldy BlockPt sty Next,x ;link "to be blocked" stx BlockPt ; Launch next thread ldx RunPt lds SP,x ;set SP for this new thread ldd TCNT ;Next thread gets a full 10ms time slice addd #20000 ;interrupt after 10 ms std TC5 ldaa #$20 staa TFLG1 ;clear C5F rti