Click Here To Go To The Your Computer Archive



Written By Jeremy Hill


Cover Art
Click Here To Enlarge Loading Screen

Loading Screen
Click Here To Enlarge Opening Screen

Opening Screen
Click Here To Enlarge Screenshot

Game Screenshot

Multitasking

Here's how to hare a dozen pairs of hands. Jeremy Hill presents the art of multi¬tasking for the delectation of BBC users

Multi-tasking is the running of several programs at the same time on the same computer. This program allows multi-tasking on the standard BBC computer in Basic, but will probably not work with the second-processor.

Actually on most computers, certainly on microcomputers, true multi-tasking - programs really running at the same time - is impossible.

The method we use to get around this is to run each program for a short period of time - this is called a time-splice - and then go on to the next pro¬gram until the last program is reached, then the first program is continued from when it left off, and so on.

To give the impression of all the programs running at the same time, the time each program is run for before it goes on to the next is usually quite short.

Because BBC Basic was not designed for multi-tasking there are certain restrictions imposed on the programmer. For in¬stance this program cannot be switched within procedures without causing some rather undesirable consequences. Also you cannot switch tasks within GOSUBs, FOR...NEXT and REPEAT...UNTIL loops.

Using This System

To use this system is very simple, you simply type the listing - ignoring comments in the form REM ... , or: ... if you wish to - and save it. When you wish to use it within a set of programs to run together you load in this program and type in before it the set of programs together with a 'description line' before the actual program - see later. Note that Break must be pressed before PROCassem is used again.

Another restriction of this system is that. all the programs have to be written one after each other, in the same program area, although this means that you can use "global variables" in order to allow all the programs to communicate with each other. You must note, however, that all the variables are global and using the same variable in more than one program unintentionally will cause dire consequences. You have been warned! An example of how to use this system:

 10 PROCassem
 20 !&72 = &FFFFFFFF (time splice in hundredths of a second)
 30 ?&76=&FF
 40 GOTO 70 (first program line number)
 50
 60 REM Program 1
 70 CALL define:GOTO 120 (next program line number)
 80 REM Any program
 90 CALL chk:GOTO 80
100
110 REM Program 2
120 CALL define:CALL start (to signal last program)
130 REM Another program
140 CALL chk:GOTO 130

Lines 20 to 40, 70, 120 are description lines. These setup the tasks by telling the system where the tasks are within the program.

When an individual task is being performed the system has to be told when it can change the task being run. This is done by putting: CALL chk at points in the tasks where switching can take place, eg FNs or Procedures. If the time-splice is over the program will be suspended until its time-splice comes around again otherwise the execution will continue after the Call.

It is important to remember that the task being run will not change until the time-splice is over and Callchk has been executed, so the program can use extra time if it needs to by not calling chk. If, on the other hand, the task being executed needs no more time for the moment it can force a switch by executing:

CALLnxt

This program works by making use of the interval timer built into the BBC.

This timer counts from the number you specify to 10991162703 by one every hundredth of a second. When the counter reaches its maxi¬mum number it resets back to zero (giving you a range of a hundredth of a second to nearly three and a half centuries long time-splice!). When this hap¬pens the computer is informed of 'Interval timer crossing zero'.

This is when my multi-tasking routine comes in. When the computer detects this condition my routine takes over and sets a signal in the computer - a flag - to show that the time-splice is over.

When your program executes Callchk another routine in my program checks this flag to see whether the time-splice is over. If the time-splice is not yet over then the task continues as normal. Otherwise it stores where the program left off and restores the next program's position. This is unless it has reached the last program, in' which case it resets to the first program again. Finally bytes &4F to &80 in zero-page are used by this pro-gram, and when you run the program after you have typed it it (save it firstl) you should then see a demonstration of multi¬tasking. If you see this you can then adapt the program for your own multi-tasking application.

The program listed below is an example of the multi-tasking on the BBC with five programs running at the same time. These are:

  1. A program printing text in the top, left hand corner of the screen;
  2. A program plotting individual random points on one area of the screen;
  3. Another program drawing lines in random positions in another area of the screen;
  4. Yet another program check¬ing the keyboard and accept¬ing Mos commands ('.' corn-mands), executing the line when Return is pressed. The line will be printed just below the text and Delete will work as usual;
  5. Lastly there is a program telling the time (not very accurately, but it serves as a demonstration) in hours and minutes at the top of the screen in the middle of the line.

The last part of the program is simply the m/c assembly of the multi-tasking code. This program creates quite an interesting visual effect, and is worth referring to in order to see how the multi-tasking utility is used.