Multi-tasking

Multi-tasking is a function that until recently was not so generally known. The Forth community have been using it for many years, indeed Charles Moore's first Forth's used it. Once used you will wonder how you did without it. The version in the TLM, there are many, is the simplest and called a Round Robin tasker. Basically each task is a separate program that is often self contained. The tasks run until they reach a point where they have to wait for something, at this point a task PAUSEs and the next runs. If the task does not have a natural wait then a PAUSE must be added at some convenient point, or the task STOPped. These tasks are said to be co-operative, that is they must relinquish control of the processor to allow others to have a go. Each task has it's own stack space and an area called USER. Here resides the links to the tasks memory plus variables that may be used only by that task. One example of these is BASE used by the numbers, described elsewhere. A task is given a name and the sizes of the Data stack, Return stack and User area these are then allocated to it by TASK:. The stacks work down from their tops and the user area goes up, as shown below.:

USER Area

The USER area starts with the task start code called MLINK. The other bytes defined by IRTC are show below:

 MLINK UP+0 Address of the next Task area with the MSB set if stopped else cleared when running.

 DP0 UP+2 The address of the bottom of the Data stack.

 PTR UP+4 The pointer for number to ASCII conversion.

 BASE UP+6 The radix for number conversion.

 HANDLER UP+8 The exception return address.

 PAD UP+20 General area used by number conversion, PTR>PAD number to ASCII, PAD> ASCII to number.

Next available USER address is UP+30

Running Tasks

The Task areas allocated by TASK: which is preceded by three values to set the task data, return and user stack areas. The USER area is a described above. The Return stack area is used by the Forth words >R R> R@ and the DO..LOOPs. The Data stack holds the parameters passed between Forth words and if multi-tasking is used must also be able to hold a copy of the PIC stack, which could be 31 items deep.

40 20 30 TASK: <task-name>

Then we must chain the tasks together at runtime. To do this we LINK the tasks.

<task-name> LINK

The LINK process adds a task to the chain but sets the task as RESTing. To make a task run the application we wish, the USER area and the task's stacks must be set to run whatever word we select. The task must be ACTIVATEd.

<task-name> ACTIVATE <word>

This must be inside a : definition. <word> must be an endless loop or terminate with STOP.

 

Contents