program calling with BS2SX, BS2E, BS2P plus contextswitch
Hi Tracy,
I ran into the problem of retaining variable values during program
switching.
Thought you might be interested in my solution.
As the stack mechanism does provide a means to retain variable values for
the calling
program (i.e parent), it does not for the called program (i.e. child), as
the ram locations
are restored upon return.
To overcome this I found an elegant way.
I reserve some scratchpad locations above the stack to hold program
variables that
have to be retained during a program call.
Upon entry of a program these values are restored, while when doing a
program call
these values are saved. This reduces overhead to just 2 gosubs and 2 small
routines.
Retain1 var word 'retainable vars (known to program only)
start at ramlocation 0
'
Global1 var word 'globals (known to all programs)
'
Local1 var word 'locals stop at ramlocation 25 (known to
program only)
'program 0
gosub Restore0Global 'first line of code: restore
context upon entry
branch TargetID,[noparse][[/noparse]...]
'
gosub PushLocal
put StackPointer,EN1P0
StackPointer=StackPointer-1
TargetID=EN6P4
goto CallProgram
entry01:
'return point
gosub PopLocal
'
CallProgram: 'single entry used for program calling
gosub Save0Globsl 'save context before calling
run TargetID
'
'
Save0Global:
for X=0 to RETAIN0SIZE-1
put RETAIN0START-X,B0(X) 'implied array
next 'put B0 first, B3 last (for RETAIN0SIZE=4)
return
Restore0Global:
for X=0 to RETAIN0SIZE-1
get RETAIN0START-X,B0(X) 'implied array
next 'get B0 first, B3 last (for RETAIN0SIZE=4)
return
'Program 1
Save1Global:
for X=0 to RETAIN1SIZE-1
put RETAIN1START-X,B0(X) 'implied array
next 'put B0 first, B3 last (for RETAIN1SIZE=4)
return
Restore1Global:
for X=0 to RETAIN1SIZE-1
get RETAIN1START-X,B0(X) 'implied array
next 'get B0 first, B3 last (for RETAIN1SIZE=4)
return
The constants RETAIN?START and RETAIN?SIZE are defined at the program start.
TopOfScratchPad con 126 '126 or 62
retain01 var word 'start at ramlocation 0 (program 0)
retain02 var byte
RETAIN0SIZE con 3
RETAIN0START con TopOfScratchPad '126 or 62
retain11 var byte 'start at ramlocation 0 (program 1)
retain12 var byte
RETAIN1SIZE con 2
RETAIN1START con RETAIN0START-RETAIN0SIZE
etc. until
RETAIN7SIZE con 0 'program 7 without retainable variables
RETAIN7START con RETAIN6START-RETAIN6SIZE
TOS con RETAIN7START-RETAIN7SIZE 'Top Of Stack
For programs with a RETAINSIZE=0 the gosub in the first line must be
removed,
so must the gosub in CallProgram and both routines Save?Global and
Restore?Global.
Any comments on this procedure for a (stripped down) context switch is
welcomed.
Greetings peter.
I ran into the problem of retaining variable values during program
switching.
Thought you might be interested in my solution.
As the stack mechanism does provide a means to retain variable values for
the calling
program (i.e parent), it does not for the called program (i.e. child), as
the ram locations
are restored upon return.
To overcome this I found an elegant way.
I reserve some scratchpad locations above the stack to hold program
variables that
have to be retained during a program call.
Upon entry of a program these values are restored, while when doing a
program call
these values are saved. This reduces overhead to just 2 gosubs and 2 small
routines.
Retain1 var word 'retainable vars (known to program only)
start at ramlocation 0
'
Global1 var word 'globals (known to all programs)
'
Local1 var word 'locals stop at ramlocation 25 (known to
program only)
'program 0
gosub Restore0Global 'first line of code: restore
context upon entry
branch TargetID,[noparse][[/noparse]...]
'
gosub PushLocal
put StackPointer,EN1P0
StackPointer=StackPointer-1
TargetID=EN6P4
goto CallProgram
entry01:
'return point
gosub PopLocal
'
CallProgram: 'single entry used for program calling
gosub Save0Globsl 'save context before calling
run TargetID
'
'
Save0Global:
for X=0 to RETAIN0SIZE-1
put RETAIN0START-X,B0(X) 'implied array
next 'put B0 first, B3 last (for RETAIN0SIZE=4)
return
Restore0Global:
for X=0 to RETAIN0SIZE-1
get RETAIN0START-X,B0(X) 'implied array
next 'get B0 first, B3 last (for RETAIN0SIZE=4)
return
'Program 1
Save1Global:
for X=0 to RETAIN1SIZE-1
put RETAIN1START-X,B0(X) 'implied array
next 'put B0 first, B3 last (for RETAIN1SIZE=4)
return
Restore1Global:
for X=0 to RETAIN1SIZE-1
get RETAIN1START-X,B0(X) 'implied array
next 'get B0 first, B3 last (for RETAIN1SIZE=4)
return
The constants RETAIN?START and RETAIN?SIZE are defined at the program start.
TopOfScratchPad con 126 '126 or 62
retain01 var word 'start at ramlocation 0 (program 0)
retain02 var byte
RETAIN0SIZE con 3
RETAIN0START con TopOfScratchPad '126 or 62
retain11 var byte 'start at ramlocation 0 (program 1)
retain12 var byte
RETAIN1SIZE con 2
RETAIN1START con RETAIN0START-RETAIN0SIZE
etc. until
RETAIN7SIZE con 0 'program 7 without retainable variables
RETAIN7START con RETAIN6START-RETAIN6SIZE
TOS con RETAIN7START-RETAIN7SIZE 'Top Of Stack
For programs with a RETAINSIZE=0 the gosub in the first line must be
removed,
so must the gosub in CallProgram and both routines Save?Global and
Restore?Global.
Any comments on this procedure for a (stripped down) context switch is
welcomed.
Greetings peter.