Page 1 of 1
Need Multitasking - one more time
Posted: Tue Apr 30, 2002 10:31 am
by Peter Sejfried
Hi to all!!!
I downloaded simple multitasking demo code written by Alexei A. Frounze. This is Turbo Pascal example but works perfectly. Is there somebody who could help me and who will try to convert this code to 32-bit protected mode? I'm not a master of 32-bit PMode and I need help. The firs part of this code is written in assembler. The second in Pascal. Please tell me if you could help me and if you can please try to convert it. I tried translation and multitasking doesn't work. The code is in next message.
Best Regards
Peter
Re:Need Multitasking - one more time
Posted: Tue Apr 30, 2002 10:32 am
by Peter Sejfried
Here is Pascal part of multitasking code:
USES CRT, DOS;
CONST
STACK_SIZE = 4096; { TASK STACK SIZE }
MAX_TASKS = 8; { MAX TASKS }
FASTER = 8; { SPEED UP "PIT" BY "FASTER" }
VAR
OLD_TIMER : POINTER;
SWITCHING : BOOLEAN; { ENABLE/DISABLE SWITCHING }
BLOCKS, { STACK BUFFERS' ADDRESSES }
STACK_LIST : ARRAY [0..MAX_TASKS-1] OF POINTER; { STACK POINTERS FOR TASKS }
CUR_TASK : WORD; { CURRENT TASK NO. }
TASK_CNT : WORD; { TASKS TOTAL }
ACTIVE_TIME, { ACTIVITY IN TICKS }
AVAIL_TIME : ARRAY [0..MAX_TASKS-1] OF WORD; { QUANT OF TIME IN TICS }
{$L MULTIT}
PROCEDURE SHEDULER; FAR; EXTERNAL;
PROCEDURE ADD_TASK (IP, CS, DS, AVAILTIME : WORD); FAR; EXTERNAL;
PROCEDURE INIT;
VAR I : WORD;
BEGIN
SWITCHING := FALSE;
TASK_CNT := 1;
CUR_TASK := 0;
FOR I := 0 TO MAX_TASKS-1 DO
GETMEM (BLOCKS
, STACK_SIZE);
AVAIL_TIME[0] := 7; { 70% OF CPU TIME FOR MAIN PROGRAM }
ACTIVE_TIME[0] := 0;
GETINTVEC (8, OLD_TIMER);
SETINTVEC (8, @SHEDULER);
{ REPROGRAMMING "PIT" FOR MORE SMOOTH THREAD EXECUTION }
{ SPEEDING UP FOR "FASTER" TIMES }
PORT[$43] := $34;
PORT[$40] := ($10000 DIV FASTER) AND $FF;
PORT[$40] := (($10000 DIV FASTER) SHR AND $FF;
END;
PROCEDURE DONE;
VAR I : WORD;
BEGIN
SWITCHING := FALSE;
FOR I := 0 TO MAX_TASKS-1 DO
FREEMEM (BLOCKS, STACK_SIZE);
SETINTVEC (8, OLD_TIMER);
{ REPROGRAMMING "PIT" BACK }
{ SETTING STANDARD 18.2 HZ FREQUENCY }
PORT[$43] := $34;
PORT[$40] := $00;
PORT[$40] := $00;
END;
(* TASKS' CODE *)
{ We use this subroutine for screen output because CRT is a global unit }
{ and it wouldn't work properly, if every thread/task used it. }
{ In other words, DOS and some BP functions/procedures are not reenterable. }
procedure WriteStrXY (x, y : word; s : string);
var
l, i : integer;
addr : word;
begin
i := 1;
l := length(s);
addr := (y*80+x) shl 1;
while l > 0 do begin
Mem[$B800:addr] := byte(s);
inc (addr, 2);
inc (i);
dec (l)
end
end;
procedure task1;
var
counter : longint;
s : string;
begin
counter := 0;
repeat
str (counter, s);
WriteStrXY (0, 0, 'Task 1 counter: '+s);
inc (counter)
until false
end;
procedure task2;
var
counter : longint;
s : string;
begin
counter := 0;
repeat
str (counter, s);
WriteStrXY (0, 1, 'Task 2 counter: '+s);
inc (counter)
until false
end;
(* MAIN PROGRAM *)
BEGIN
CLRSCR;
INIT; { 70% OF CPU TIME FOR MAIN PROGRAM }
ADD_TASK (OFS(TASK1), CSEG, DSEG, 1); { 10% OF CPU TIME FOR TASK 1 }
ADD_TASK (OFS(TASK2), CSEG, DSEG, 2); { 20% OF CPU TIME FOR TASK 2 }
GOTOXY (1, 4); WRITELN ('HIT A KEY TO START SWITCHING...'); READKEY;
SWITCHING := TRUE;
WRITELN ('HIT A KEY TO SHUTDOWN...'#13#10); READKEY;
DONE;
WRITELN ('??????????????????????????????????????????ż');
WRITELN ('ł Real Mode ł');
WRITELN ('ł Multitasking/multithreading ł');
WRITELN ('ł Demo ł');
WRITELN ('ł ł');
WRITELN ('ł by Alexei A. Frounze (c) 2000 ł');
WRITELN ('ł ł');
WRITELN ('łE-mail: [email protected] ł');
WRITELN ('łHomepage: http://alexfru.chat.ru ł');
WRITELN ('łMirror: http://members.xoom.com/alexfruł');
WRITELN ('Ŕ?????????????????????????????????????????Ů');
END.
Re:Need Multitasking - one more time
Posted: Tue Apr 30, 2002 10:33 am
by Peter Sejfried
And here is assembler part of multitasking demo:
IDEAL
MODEL LARGE
P386
STACK_SIZE EQU 4096
MAX_TASKS EQU 8
FASTER EQU 8
EXTRN OLD_TIMER : DWORD
EXTRN SWITCHING : BYTE
EXTRN BLOCKS : DWORD
EXTRN STACK_LIST : DWORD
EXTRN TASK_CNT : WORD
EXTRN CUR_TASK : WORD
EXTRN AVAIL_TIME : DWORD
EXTRN ACTIVE_TIME : DWORD
PUBLIC SHEDULER, ADD_TASK
CODESEG
PROC SHEDULER FAR
PUSH DS
PUSH ES
PUSH FS
PUSH GS
PUSH EAX
PUSH EBX
PUSH ECX
PUSH EDX
PUSH ESI
PUSH EDI
PUSH EBP ; SAVE TASK REGISTERS ON THE STACK
MOV AX, DGROUP
MOV DS, AX ; LOAD DS WITH VALID DATA SEGMENT
INC [TICK]
CMP [TICK], FASTER
JE @@CALL
MOV AL, 20H
OUT 20H, AL
JMP @@SKIP
@@CALL:
MOV [TICK], 0
PUSHF
CALL [OLD_TIMER] ; HANDLE IRQ0 AT STANDARD FRQUENCY
@@SKIP:
CMP [SWITCHING], 0
JE @@EXIT ; SWITCHING IS DISABLED
CMP [TASK_CNT], 2
JC @@EXIT ; NOTHING TO SWITCH
MOV AX, [CUR_TASK]
MOV BX, AX
SHL BX, 2
LEA SI, [AVAIL_TIME]
MOV CX, [DS:BX+SI] ; CX = AVAILABLE TIME
LEA SI, [ACTIVE_TIME]
INC [WORD PTR DS:BX+SI]
CMP [DS:BX+SI], CX
JC @@EXIT ; SWITCHING WILL BE A BIT LATER
MOV [WORD PTR DS:BX+SI], 0
LEA SI, [STACK_LIST] ; DS:SI = ^TASK LIST (STACK LIST)
MOV [DS:BX+SI], SP
MOV [DS:BX+SI+2], SS ; SAVE CURRENT STACK STATE
@@SEARCH:
INC AX ; NEXT TASK NUMBER
CMP AX, [TASK_CNT]
JC @@NEXT
XOR AX, AX ; FIRST TASK
@@NEXT:
MOV [CUR_TASK], AX ; NEXT TASK NUMBER
MOV BX, AX
SHL BX, 2
LEA SI, [AVAIL_TIME]
CMP [WORD PTR DS:BX+SI], 0
JE @@SEARCH ; KEEP SEARCHING, IF FROZEN TASK FOUND
LEA SI, [STACK_LIST] ; DS:SI = ^TASK LIST (STACK LIST)
MOV SP, [DS:BX+SI]
MOV SS, [DS:BX+SI+2] ; RESTORE STACK STATE
@@EXIT:
POP EBP
POP EDI
POP ESI
POP EDX
POP ECX
POP EBX
POP EAX
POP GS
POP FS
POP ES
POP DS ; RESTORE TASK REGISTERS FROM THE STACK
IRET ; CONTINUE THE TASK
ENDP
PROC ADD_TASK PASCAL FAR _IP:WORD, _CS:WORD, _DS:WORD, _AT:WORD
CMP [TASK_CNT], MAX_TASKS
JAE @@EXIT
LEA SI, [BLOCKS] ; DS:SI = ^BLOCKS
MOV BX, [TASK_CNT]
SHL BX, 2
MOV AX, [DS:BX+SI]
MOV DX, [DS:BX+SI+2] ; DX:AX = ^BLOCK
ADD AX, STACK_SIZE-42 ; CORRECT FOR SP
LEA SI, [STACK_LIST] ; DS:SI = ^STACK PTR
MOV [DS:BX+SI], AX
MOV [DS:BX+SI+2], DX ; SS:SP IS DONE FOR THE TASK
PUSH ES
MOV ES, DX
ADD AX, 42
MOV DI, AX ; ES:DI = ^STACK
MOV [WORD PTR ES:DI-2], 202H; FLAGS
MOV AX, [_CS]
MOV [ES:DI- 4], AX ; CS
MOV AX, [_IP]
MOV [ES:DI- 6], AX ; IP
MOV AX, [_DS]
MOV [ES:DI- 8], AX ; DS
MOV [ES:DI-10], AX ; ES
XOR EAX, EAX
MOV [ES:DI-12], AX ; FS
MOV [ES:DI-14], AX ; GS
MOV [ES:DI-18], EAX ; EAX
MOV [ES:DI-22], EAX ; EBX
MOV [ES:DI-26], EAX ; ECX
MOV [ES:DI-30], EAX ; EDX
MOV [ES:DI-34], EAX ; ESI
MOV [ES:DI-38], EAX ; EDI
MOV [ES:DI-42], EAX ; EBP
POP ES
LEA SI, [AVAIL_TIME]
MOV AX, [_AT]
MOV [DS:BX+SI], AX
LEA SI, [ACTIVE_TIME]
MOV [WORD PTR DS:BX+SI], 0
INC [TASK_CNT]
@@EXIT:
RET
ENDP
DATASEG
TICK DW 0
END
Re:Need Multitasking - one more time
Posted: Tue Apr 30, 2002 10:37 am
by Peter Sejfried
In the first part of code is line like this:
PORT[$40] := (($10000 DIV FASTER) SHR
AND $FF;
it should be:
PORT[$40] := (($10000 DIV FASTER) SHR 8 ) AND $FF;
The 8 number and the bracket is interpreted as
Re:Need Multitasking - one more time
Posted: Wed May 01, 2002 5:43 am
by Peter Sejfried
Hi!!!
Could anyone help me. I'm waiting for your reply about attached codes. Is it possible to convert it to 32 bit Protected Mode? If yes - how could I do it? What I must change in codes to properly run it in 32 bit PMode.
Re:Need Multitasking - one more time
Posted: Wed May 01, 2002 10:16 am
by ASHLEY4
That man Alexei A. Frounze is a genius, like a lot of east european and russian's they seem to have a nack for low level programing.
As he uses dos and crt which call's dos ,Your OS has to be dos compatible.
There is a pascal called vpascal ,that is 32bit and more compatible with turbo pascal than freepascal.(also made by (east european's).
I have that code i will try to compile it with vpascal and get back to you.
(Just in case your wondering i'm English, in case you thought i was biassed)
.
\\\///
(@@)
ASHLEY4.
Re:Need Multitasking - one more time
Posted: Wed May 01, 2002 1:15 pm
by ASHLEY4
No it does not work in free or vpascal,But in Alexei A. Frounze "PMTUT.ZIP"avalable from is web site,theres a tut (tut07) that tell's how to multitask in PM,But this is in asm/c.
This may help?.
\\\///
(@@)
ASHLEY4.
Re:Need Multitasking - one more time
Posted: Wed May 01, 2002 2:16 pm
by Peter Sejfried
OK!!!
Thanks ASHLEY!
I'll check it. If you have free time and you know how to translate this demo code to 32 bit PMode, please try to convert it for me.
Regards
Peter
Re:Need Multitasking - one more time
Posted: Thu May 02, 2002 12:39 am
by K.J.
ASHLEY4 wrote:
That man Alexei A. Frounze is a genius, like a lot of east european and russian's they seem to have a nack for low level programing.
As he uses dos and crt which call's dos ,Your OS has to be dos compatible...
Not all of Alexei's code examples need DOS. He seems to use DOS in some of his code so that there is no need for a bootsector.
K.J.