program ForLoopTest;

{
****************************************************************************
*                                                                          *
* This example illustrates the difference between local and global         *
* variables when used as loop control varable.                             *
*                                                                          *
* To illustrate this compile without the optimiser and run the simulator.  *
* Show the LCD window, then run the program. The Local Loop variable       *
* version is shown on the top line and the Global version on the bottom    *
* line of the LCD.                                                         *
*                                                                          *
* You will see that both run at about the same speed.                      *
*                                                                          *
* Now repeat with optimisation. You will see that the Local variable       *
* version runs nearly 50% quicker than the global variable version         *
*                                                                          *
****************************************************************************
}

{ $WG}                     {global Warnings off}

Device = mega128, VCC=5;
{ $BOOTRST $0F000}         {Reset Jump to $0F000}

Import SysTick, LCDport;

From System Import Processes;


Define
  ProcClock      = 16000000;       {Hertz}
  SysTick        = 1;             {msec}
  StackSize      = $0064, iData;
  FrameSize      = $0064, iData;
  Scheduler      = iData;
  LCDport        = PortA;
  LCDtype        = 44780;
  LCDrows        = 2;              {rows}
  LCDcolumns     = 20;             {columns per line}

Implementation

{$IDATA}

{--------------------------------------------------------------}
{ Type Declarations }

type


{--------------------------------------------------------------}
{ Const Declarations }

{--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}


{--------------------------------------------------------------}
{ functions }



{--------------------------------------------------------------}
{ Main Program }
{$IDATA}

var
  Lj, Gj : byte;
  
procedure DoLoopLocal;
var
  Li : word;
begin
  for Li := 1 to 5000 do
  end_for;
  inc( Lj );
end;

process LoopLocal(64, 64 : iData, 3);
begin
    DoLoopLocal;
end;

var
  Gi : word;
  
procedure DoLoopGlobal;
begin
  for Gi := 1 to 5000 do
  end_for;
  inc( Gj );
end;

process LoopGlobal(64, 64 : iData, 3);
begin
    DoLoopGlobal;
end;

begin


  Start_Processes;
  loop

  LCDXY( 0,0);
  write( LCDOut, ByteToHex( Lj ));
  LCDXY( 0,1);
  write( LCDOut, ByteToHex( Gj ));

  endloop;
end ForLoopTest.

