Previous Topic

Next Topic

Book Contents

Book Index

Multitasking: Branch and Target

The Suspend and Resume instructions came into being in SPLat dialect 12. There has been another mechanism available since the very first SPLat ever made. This is the instruction pair Branch and Target. There are also alternatives to Branch called BranchM, which "branches on" memory rather than on X and BranchR, which "branches on" R rather than on X.

The best way to describe how Branch works is with the following code fragment (read the comments!):

        BranchM    StateNumber
        Target     State0   ;Go to the line State0 if StateNumber contains 0
        Target     State1   ;Go to the line State1 if StateNumber contains 1
        Target     State2   ;Go to the line State2 if StateNumber contains 2
        Target     State3   ;Go to the line State3 if StateNumber contains 3
        .... etc .....

So, the BranchM instruction grabs the number in the specified RAM location (here StateNumber) then counts its way down the Target list to find the n'th entry (starting counting at 0, not 1). The target list must follow immediately after the BranchM instruction. If the argument (in this case StateNumber) points past the end of the list, SPLat/PC will raise an error.

If you are familiar with the early Basic languages, you will recognise this as something similar to ON..GOTO. With more modern languages it is like SWITCH or CASE but with the selector values constrained to being consecutive integers.

How do we use this in multitasking? Well, the idea is that StateNumber contains the, ehr, state number, and each target address be for the code that handles that state.

A few screens ago I showed the OnOffSwitch subroutine using Suspend and Resume. Here is the same routine using Branch and Target. This assumes a single byte of RAM is reserved with the name OnOffState.

OnOffSwitch
            BranchM     OnOffState
            Target      State0
            Target      State1
            Target      State2
            Target      State3
OnOffInit
Set0
            SetMem      OnOffState,0
            Return
State0
            GoIfInOn    Switch,Set1
            Return
Set1
            On          Light
            SetMem      OnOffState,1
            Return
State1
            GoIfInOff   Switch,Set2
            Return
Set2
            SetMem      OnOffState,2
            Return
State2
            GoIfInOn    Switch,Set3
            Return
Set3
            Off         Light
            SetMem      OnOffState,3
            Return
State3
            GoIfInOff   Switch,Set0
            Return

As an exercise, copy this code and use it to replace the OnOffSwitch task in the previous complete program, then single step it in the simulator until you understand how it works.

Which is better, Branch/Target or Suspend/Resume?

There is no clear cut answer to that question. I think Suspend/Resume is a conceptually easier to understand and can save you some typing, but Branch/Target is perhaps a bit more versatile. If you are only a very occasional SPLat programmer, use Suspend/Resume but remember Branch/Target can be used for things other than multitasking. If you expect to be doing a lot of SPLat programming, try them both.

After a while you will start to see the pros and cons of each method. For instance, with Branch/Target a task can have multiple entry points for external events (i.e. methods), each with its own Branch/Target table. That means it lends itself to situations where external events are "pushed" into it. The Suspend/Resume method, on the other hand, is better suited to looking out for things to do (pulling).

Special note: This tutorial and the multitasking mechanism it covers have largely been superseded by the newer MultiTrack mechanism. MultiTrack achieves everything and more, with considerably less programming effort. The only time you would use this older multitasking mechanism would be if the controller you are using is unable to handle as many tasks as you need using MultiTrack.

Previous Topic

Next Topic