Multi-channel MultiTrack: RAM addressing with J
The following program illustrates how J automatically modifies RAM (Data Memory) addressing. The program has 4 channels. When a channel is activated by an off to on transition on its input, the corresponding output flashes 5 (NumberOfFlashes) times. (I made the flashes short so it would be convenient for you to test in the simulator). The flashes are counted in an array of RAM variables called Counter. Counter is declared in a defBYTE directive. The argument 4 makes the defBYTE set aside 4 contiguous bytes of RAM for 4 counters. When Counter is referenced in the program (in the DecMGoIfNZ instruction) J is used to select one of the 4 bytes.
(Click here for some tips for working around problems with copy and paste out of Internet Explorer and HTML-help (.chm) files)
NumberOfFlashes EQU 5
LaunchTask TaskA,0
LaunchTask TaskA,1
LaunchTask TaskA,2
LaunchTask TaskA,3
RunTasksForever
Counter: defBYTE 4 ;Array of 4 single-byte counters
TaskA:
WaitOnK 0
SetMem Counter,NumberOfFlashes
TaskA1:
On 0
Pause 1
Off 0
Pause 1
DecMGoIfNZ Counter,TaskA1
GoTo TaskA
In the above example the address offset is just J. If there was a floating point instruction, the offset would be 4xJ, because each floating point variable uses 4 bytes of RAM. Similarly, for SuperTimers the offset would be 3xJ, because a SuperTimer uses 3 bytes.