Shop OBEX P1 Docs P2 Docs Learn Events
summary program calls - reso;lving SERIN conflict — Parallax Forums

summary program calls - reso;lving SERIN conflict

ArchiverArchiver Posts: 46,084
edited 2001-05-10 14:49 in General Discussion
Hi,

The SERIN command allows to read in data directly to the scratchpad.
The program stack was placed at the start of the scratchpad so there
is a conflict. Moving the stack to the end of the scratchpad to resolve
the conflict.


This summary elaborates on the program calling scheme.
The scheme is shown for programs 0 and 1. Tracking the code
For those programs it is easy to add code for programs 2-7.

Features:
7 nested program calls with returnvalues

Limitations:
Program calls are only possible at the main level outside for-next loops.
(if a program call inside a for-next loop at the main level is imperative,
workaround using a counter and an if-then statement).
No check on nesting level (i.e stackpointer value)

Resources:
Uses 14 Scratchpad locations for program stack
2 global word variables (global to all programs that is)


Greetings, peter
Any more suggestions are welcome.


'Method for program gosub using scratchpad as program stack

'scratchpad usage (TOS stands for Top Of Stack)
'TOS+ 0 = not used
'TOS+ 1 = parent ID 1
'TOS+ 2 = return ID 1
'TOS+ 3 = parent ID 2
'TOS+ 4 = return ID 2
'TOS+ 5 = parent ID 3
'TOS+ 6 = return ID 3
'TOS+ 7 = parent ID 4
'TOS+ 8 = return ID 4
'TOS+ 9 = parent ID 5
'TOS+10 = return ID 5
'TOS+11 = parent ID 6
'TOS+12 = return ID 6
'TOS+13 = parent ID 7
'TOS+14 = return ID 7


'program 0

TopOfStackBS2 con 63-15 'top of stack fot BS2
TopOfStackBSX con 127-15 'top of stack for SX, E, P

TOS con TopOfStackBSX 'pick the right top of stack

'global variables
pcvar1 var word 'program call variable #1
pcvar2 var word 'program call variable #2
returnvalue var pcvar1
ParentID var pcvar1.highbyte 'parent program to return to, returnflags
FunctionID var pcvar1.lowbyte 'function to call, returnvalue
ReturnID var pcvar2.lowbyte 'parent program entry to return to
StackPointer var pcvar2.nib2 'stackpointer
RunDirect var pcvar2.bit12 '0 for program call, 1 for direct run

ProgramID con 0

Begin0:
if RunDirect then main0 'check for run direct
if StackPointer=0 then main0 'SP clear so no program call/return
get TOS+StackPointer-1,ParentID 'get ParentID
if ParentID.bit7 then bg0 'if > 127 then return address
branch FunctionID,[noparse][[/noparse]function000,function001,...,function0FF]
bg0:
get TOS+StackPointer,ReturnID
StackPointer=StackPointer-2 'as all are retrieved, adjust stack here
ParentID.bit7=0 'clear returnidflag, bits 6-0 hold returnflags
branch ReturnID,[noparse][[/noparse]return000,return001,...,return0FF]

main0:
'init
main0loop:

' ...

put TOS+StackPointer+1,ProgramID
put TOS+StackPointer+2,0
FunctionID=14
StackPointer=StackPointer+2
Rundirect=0 'only required if directrun is used somewhere
run 4 'execute function 14 in program 4, then return to return000
return000:
if returnvalue<>0 then ... 'check 15-bit return value

' ...

put TOS+StackPointer+1,ProgramID
put TOS+StackPointer+2,1
FunctionID=167
StackPointer=StackPointer+2
Rundirect=0 'only required if directrun is used somewhere
run 7 'execute function 167 in program 7, then return to return001
return001:
if parentID<>0 then ... 'check return flags
if functionID<>0 then ... 'check 8-bit return value

' ...

runDirect=1
run 3 'run program 3

main0end:
goto main0loop

'function entries for program calls

function000:
gosub function_000
goto ReturnFromProgramCall_0

function001:
gosub function_001
goto ReturnFromProgramCall_0

' ...

ReturnFromProgramCall_0:
get TOS+StackPointer-1,ParentID 'get ParentID
put TOS+StackPointer-1,128+0 'enable return + add flags
FunctionID=0 'return a value
run ParentID 'return to parent program

'subroutines

function_000:
return

function_001:
return

end

'Program 1

TopOfStackBS2 con 63-15 'top of stack fot BS2
TopOfStackBSX con 127-15 'top of stack for SX

TOS con TopOfStackBSX 'pick the right top of stack

'global variables
pcvar1 var word 'program call variable #1
pcvar2 var word 'program call variable #2
returnvalue var pcvar1
ParentID var pcvar1.highbyte 'parent program to return to, returnflags
FunctionID var pcvar1.lowbyte 'function to call, returnvalue
ReturnID var pcvar2.lowbyte 'parent program entry to return to
StackPointer var pcvar2.nib2 'stackpointer
RunDirect var pcvar2.bit12 '0 for program call, 1 for direct run

ProgramID con 1

Begin1:
if RunDirect then main1 'check for run direct
if StackPointer=0 then main1 'SP clear so no program call/return
get TOS+StackPointer-1,ParentID 'get ParentID
if ParentID.bit7 then bg1 'if > 127 then return address
branch FunctionID,[noparse][[/noparse]function100,function101,...,function1FF]
bg1:
get TOS+StackPointer,ReturnID
StackPointer=StackPointer-2 'as all are retrieved, adjust stack here
ParentID.bit7=0 'clear returnidflag, bits 6-0 hold returnflags
branch ReturnID,[noparse][[/noparse]return100,return101,...,return1FF]

main1:
'init
main1loop:

' ...

put TOS+StackPointer+1,ProgramID
put TOS+StackPointer+2,0
FunctionID=26
StackPointer=StackPointer+2
Rundirect=0 'only required if directrun is used somewhere
run 5 'execute function 26 in program 5, then return to return100
return100:
if returnvalue<>0 then ... 'check 15-bit return value

' ...

put TOS+StackPointer+1,ProgramID
put TOS+StackPointer+2,1
FunctionID=231
StackPointer=StackPointer+2
Rundirect=0 'only required if directrun is used somewhere
run 0 'execute function 231 in program 0, then return to return101
return101:
if parentID<>0 then ... 'check return flags
if functionID<>0 then ... 'check 8-bit return value

' ...

runDirect=1
run 6 'run program 6

main1end:
goto main1loop

'function entries for program calls

function100:
gosub function_100
goto ReturnFromProgramCall_1

function101:
gosub function_101
goto ReturnFromProgramCall_1

' ...

ReturnFromProgramCall_1:
get TOS+StackPointer-1,ParentID 'get ParentID
put TOS+StackPointer-1,128+0 'enable return + add flags
FunctionID=0 'return a value
run ParentID 'return to parent program

'subroutines

function_100:
return

function_101:
return

end

'program 2

TopOfStackBS2 con 63-15 'top of stack fot BS2
TopOfStackBSX con 127-15 'top of stack for SX

TOS con TopOfStackBSX 'pick the right top of stack

'global variables
pcvar1 var word 'program call variable #1
pcvar2 var word 'program call variable #2
returnvalue var pcvar1
ParentID var pcvar1.highbyte 'parent program to return to, returnflags
FunctionID var pcvar1.lowbyte 'function to call, returnvalue
ReturnID var pcvar2.lowbyte 'parent program entry to return to
StackPointer var pcvar2.nib2 'stackpointer
RunDirect var pcvar2.bit12 '0 for program call, 1 for direct run

ProgramID con 2

Begin2:
'etc.
Sign In or Register to comment.