Timer-triggered task switching 
Author Message
 Timer-triggered task switching

Hello,
    What is required in order to implement a timer-triggered task
switcher (under i386 protected mode)? This is meant for a simple demo.
Is it necessary to reprogram the timer or the PIC? I could imagine that
I would have to reprogram the timer for a specific interval though, but
perhaps the value there would work?
    And finally, how should the routine go about task switching? I can't
simply just switch the task because I have to issue the EOI and IRET. I
don't need a scheduling algorithm just yet, I am only planning on
switching 2 fixed tasks. If any information is available I would be
greatful.

Thank you,

Bart Trzynadlowski



Sat, 12 Jan 2002 03:00:00 GMT  
 Timer-triggered task switching

Quote:
>Hello,
>    What is required in order to implement a timer-triggered task
>switcher (under i386 protected mode)?

    To write it ;)

Quote:
>This is meant for a simple demo.
>Is it necessary to reprogram the timer or the PIC? I could imagine that
>I would have to reprogram the timer for a specific interval though, but
>perhaps the value there would work?

    The value there means ~55 ms. If this is not too big for you you don't
need to reprogram.  This will gain better performance (less interrupts) but
worse response times (task's response to an event is delayed up to 55 ms).

Quote:
>    And finally, how should the routine go about task switching? I can't
>simply just switch the task because I have to issue the EOI and IRET.

    Why not?

TimerIntHandler:

; Save registers.

; Send EOI to master PIC.
    mov    al, 20h
    out    20h, al

; Make a desicion of a task switch.
switch_task:
    jmp    NextTaskTSS        ; Will keep a next task's TSS at any moment.

; The task will later resume here.

dont_switch_task:
; Restore registers
    ...
    iret

In data segment:

    NextTaskTSS    LABEL    fword    ; dword for 16 bit code.
        DD    ?    ; DW for 16 bit code
    TssSelector    DW    ?    ; Keeps selector of the next TSS.

    Certainly, TimerIntHandler must have a separate stack in every task's
context.

-- The world is full of kings and queens that blind your eyes and steal your
dreams --
[Black Sabbath]



Sun, 13 Jan 2002 03:00:00 GMT  
 Timer-triggered task switching

Quote:

>Hello,
>    What is required in order to implement a timer-triggered task
>switcher (under i386 protected mode)? This is meant for a simple demo.
>Is it necessary to reprogram the timer or the PIC? I could imagine that
>I would have to reprogram the timer for a specific interval though, but
>perhaps the value there would work?
>    And finally, how should the routine go about task switching? I can't
>simply just switch the task because I have to issue the EOI and IRET. I
>don't need a scheduling algorithm just yet, I am only planning on
>switching 2 fixed tasks. If any information is available I would be
>greatful.

Here's a silly example program I put together for you (Let's just hope that
John's
WE MUST NOT POST LARGE SOURCES here program or whatever he uses
doesn't {*filter*}on this one):

 .386p

CODE SEGMENT USE16

 ASSUME cs:CODE, ds:NOTHING

videoHandler:

 sti

   test VBF, 00000001b
 jnz videoHandler

 cli

 or VBF, 00000001b

 pushf
 call OldInt10

 and VBF, 11111110b

 iret

;
; IRQ0 - Timer Interrupt handler - Task Scheduler
;

timerHandler:

 cli

 push ax
 push ds
 push si

 lds si, CTP

 mov ax, ds:[si].TCB.tcbTimeSlice

 or ax, ax
 je exit

 dec ds:[si].TCB.tcbTimeValue
 jnz exit

 mov ds:[si].TCB.tcbTimeValue, ax

 push bx
 push cx
 push dx
 push di
 push es
 push bp

 mov WORD PTR ds:[si].TCB.tcbStackPtr[0], sp
 mov WORD PTR ds:[si].TCB.tcbStackPtr[2], ss

 lds si, ds:[si].TCB.tcbLink

 mov WORD PTR CTP[0], si
 mov WORD PTR CTP[2], ds

 lss sp, ds:[si].TCB.tcbStackPtr

 pop bp
 pop es
 pop di
 pop dx
 pop cx
 pop bx
exit:
 mov al, 20h
 out 20h, al

 pop si
 pop ds
 pop ax

 iret

programStart:

 push cs
 pop ds

 push cs
 pop es

 ASSUME ds:CODE

 mov ax, 3508h
 int 21h
 mov WORD PTR OldInt08[0], bx
 mov WORD PTR OldInt08[2], es

 mov ax, 2508h
 mov dx, OFFSET timerHandler
 int 21h

 mov ax, 3510h
 int 21h
 mov WORD PTR OldInt10[0], bx
 mov WORD PTR OldInt10[2], es

 mov ax, 2510h
 mov dx, OFFSET videoHandler
 int 21h

 cli

 push cs
 pop es

 mov di, OFFSET task2Loop
 mov si, OFFSET Task2
 call initTask

 mov di, OFFSET task3Loop
 mov si, OFFSET Task3
 call initTask

 mov di, OFFSET task4Loop
 mov si, OFFSET Task4
 call initTask

 mov di, OFFSET task5Loop
 mov si, OFFSET Task5
 call initTask

 mov di, OFFSET task6Loop
 mov si, OFFSET Task6
 call initTask

 mov di, OFFSET task7Loop
 mov si, OFFSET Task7
 call initTask

 mov di, OFFSET task8Loop
 mov si, OFFSET Task8
 call initTask

 lss sp, Task1.tcbStackPtr
 mov Task1.tcbTimeSlice, 1

        mov     cx, 100
 call initTimer

 sti

FirstInstructionOfTask1:

 mov ah, 00h
 int 16h

 call doneTimer

 mov ax, 2508h
 lds dx, OldInt08
 int 21h

 ASSUME ds:NOTHING

 mov ax, 2510h
 lds dx, OldInt10
 int 21h

 mov ax, 4C00h
 int 21h

initTimer PROC NEAR

 mov dx, 0012h
 mov ax, 34DDh
 div cx

 xchg cx, ax

 mov al, 34h
 out 43h, al
 mov al, cl
 out 40h, al
 mov  al, ch
 out 40h, al

 ret

initTimer ENDP

doneTimer PROC NEAR

 mov al, 34h
 out 43h, al
 mov al, 0
 out 40h, al
 mov  al, 0
 out 40h, al

 ret

doneTimer ENDP

initTask PROC NEAR

 mov tempSP,SP
 mov tempSS,SS

 lss sp, ds:[si].TCB.tcbStackPtr

 pushf
 push es
 push di

 sub sp, 18

 mov WORD PTR ds:[si].TCB.tcbStackPtr[0], sp
 mov WORD PTR ds:[si].TCB.tcbStackPtr[2], ss

 lss sp, DWORD PTR tempSP

 ret

initTask ENDP

task2Loop:

 mov ah, 3
 mov bh, 0
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],1FH

 mov ax, 0E00h + 'A'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task2Wait1:
 xor cx, cx
task2Wait2:
 dec cx
 jnz task2Wait2
 dec bx
 jnz task2Wait1

 jmp task2Loop

task3Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],2EH

 mov ax, 0E00h + 'B'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task3Wait1:
 xor cx, cx
task3Wait2:
 dec cx
 jnz task3Wait2
 dec bx
 jnz task3Wait1

 jmp task3Loop

task4Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],3FH

 mov ax, 0E00h + 'C'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task4Wait1:
 xor cx, cx
task4Wait2:
 dec cx
 jnz task4Wait2
 dec bx
 jnz task4Wait1

 jmp task4Loop

task5Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],4EH

 mov ax, 0E00h + 'D'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task5Wait1:
 xor cx, cx
task5Wait2:
 dec cx
 jnz task5Wait2
 dec bx
 jnz task5Wait1

 jmp task5Loop

task6Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],5FH

 mov ax, 0E00h + 'E'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task6Wait1:
 xor cx, cx
task6Wait2:
 dec cx
 jnz task6Wait2
 dec bx
 jnz task6Wait1

 jmp task6Loop

task7Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],70H

 mov ax, 0E00h + 'F'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task7Wait1:
 xor cx, cx
task7Wait2:
 dec cx
 jnz task7Wait2
 dec bx
 jnz task7Wait1

 jmp task7Loop

task8Loop:

 mov ah, 3
 int 10h

        mov     al, 80
 mul dh
 add al, dl
 adc ah, 0
 shl ax, 1
 mov si, ax
 inc si

 push 0B800h
 pop ds

 mov BYTE PTR ds:[si],6BH

 mov ax, 0E00h + 'G'
 mov bx, 7
 int 10h

        mov     bx, 0002h
task8Wait1:
 xor cx, cx
task8Wait2:
 dec cx
 jnz task8Wait2
 dec bx
 jnz task8Wait1

 jmp task8Loop

TCB STRUC

tcbLink  DD ?  ; Link to next task
tcbStackPtr  DD ?  ; This task's stack (SS:SP)
tcbTimeSlice  DW ?  ; Time slice for task
tcbTimeValue  DW ?  ; Time value, counting down from slice

TCB ENDS

TaskStackSize EQU 256  ; Size of each task's own stack

; two tasks defined, Task1 (initial task), has a TimeSlice of -1, which
; will make it permanent, until the value is changed.

Task1 TCB <Task2, Stack1 + TaskStackSize, 0, 1>
Task2 TCB <Task3, Stack2 + TaskStackSize, 8, 8>
Task3 TCB <Task4, Stack3 + TaskStackSize, 2, 2>
Task4 TCB <Task5, Stack4 + TaskStackSize, 2, 2>
Task5 TCB <Task6, Stack5 + TaskStackSize, 2, 2>
Task6 TCB <Task7, Stack6 + TaskStackSize, 2, 2>
Task7 TCB <Task8, Stack7 + TaskStackSize, 2, 2>
Task8 TCB <Task1, Stack8 + TaskStackSize, 16, 16>

CTP  DD Task1  ; Current Task Pointer - CTP
VBF  DB 0

Stack1  DB TaskStackSize DUP (?) ; task stacks
Stack2  DB TaskStackSize DUP (?) ;
Stack3  DB TaskStackSize DUP (?) ;
Stack4  DB TaskStackSize DUP (?) ;
Stack5  DB TaskStackSize DUP (?) ;
Stack6  DB TaskStackSize DUP (?) ;
Stack7  DB TaskStackSize DUP (?) ;
Stack8  DB TaskStackSize DUP (?) ;

tempSP DW ?
tempSS DW ?

OldInt08 DD ?
OldInt10 DD ?

CODE  ENDS

 END programStart

--
Regards,
TK - TBD



Sun, 13 Jan 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Timer-triggered task switching

2. Triggering a Timer's action event

3. Without ENABLE signal for triggering Timer

4. timer triggered interrupts

5. timer triggered interrupts

6. Timer attribute - Background Tasks

7. Fossil Q./task switching

8. CW 2003: Task Switching?

9. Clipper, Win95, Novell network and task switching

10. Analog output stops on task switch (bug?)

11. task-switching

12. Task switching problems!...

 

 
Powered by phpBB® Forum Software