Extending M2 named variables

Armp

Senior Member
Inspired by Rays Blog http://www.picaxeforum.co.uk/entry.php?36-Extending-PICAXE-named-variables-by-300-or-more! I have written a couple of small subroutines to facilitate Bank Switching on M2s. They use no 'normal' variables at all....

They are demonstrated in the attached variation of Rays test case.

Code:
' From Manual 2, page 13
'The M2 parts have 8 word variables which are reserved 
'for system hardware use.'
'However if that piece of system hardware is not used within 
'a program they may be used as general purpose variables.

Symbol X0 = s_w1 '- Ptr0 - Bank Start Address
Symbol X1 = s_w2 '- Ptr1 - Not used here
Symbol X2 = s_w3 '- Ptr2 - Not used here
Symbol X3 = s_w4 '- Ptr3 - Not used here
Symbol X4 = s_w5 '- Temp Variable - Used for Loop Counter
Symbol X5 = s_w6 '- Temp Variable - Used for bptr



'***** Demo Code Starts *******

' Fill lower RAM addresses: B0 - B27 (W0 - W13) ... For this DEMO this will be RAM Bank 0
for bptr = 0 to 27 
 @bptr = bptr 
next

' Copy Bank 0, named variables B0 thru B27 (main memory) 
' into Banks 1, 2 and 3 at RAM OFFSET INDEXES: 28, 56, or 84

For X4 = 1 to 3
X0 = X4*28
gosub PutBank
next X4

'w2= x5 ' Check if bptr is still 28?  It is!!

for bptr = 0 to 27 'Now Clear Named Variables
 @bptr = 0 
next 

X0 = 3*28            ' And Reload them from Bank 3
gosub GetBank

stop                ' All Done...



'These two subs allow the user to save the active variables b0-b27
'Into multiple banks of RAM, and subsequently restore them.
'Together they use 29 bytes of code space, and NO 'normal' variables
'Bptr is used, but optionally can be retored on exit

GetBank:
'X0 contains the Bank Start Address
'X5 Is used to save/restore bptr - OPTIONAL

'X5= bptr              ' Save Users bptr
bptr = 0
do                 ' Could do 2, 7 or 14 per loop - This is smallest
 Peek X0, @bptrinc   
 Inc X0
loop until bptr>27
'bptr=X5            ' Restore Users bptr
Return

PutBank:
'X0 contains the Bank Start Address
'X5 Is used to save/restore bptr - OPTIONAL

'X5= bptr              ' Save Users bptr
bptr = 0
Do                       ' Could do 2, 7 or 14 per loop - This is smallest
 Poke X0, @bptrinc        
 Inc X0
loop until bptr>27
'bptr=X5             ' Restore Users bptr
Return
The concept is (I hope) very flexible, and the loop in particular can be modified in a number ways to trade speed for size.

EDIT: This seems to be fixed in 5.4.3
As an aside I did note that the compiler does not always recognize the s_wx variables as words:
Poke 80, word s_w1 gives syntax error "Error: Word variable required!", but Poke 80, s_w1 is accepted.
Not sure if its a compiler issue?
 
Last edited:

Armp

Senior Member
Thanks Ray...

The code can easily be changed to switch only a section of variables (my default is w10-w13). Faster and gives me 4 local variables.

Code:
PushHiWords:
'X5= bptr 
bptr = 20  'w10 to w13
Do
   Poke X0, @bptrinc
   Inc X0
loop until bptr>27
'bptr=X5
Return
I still prefer to PSH/POP to a stack, but you do then have to be careful with your 'housekeeping' or you get chaos. Keeping stack pointers in these reserved variables almost ensures no user code will accidentally corrupt them..
 
Last edited:

Armp

Senior Member
And if you want to keep it really simple and just have 6 extra named variables - then the compiler is happy with:

Code:
Symbol w14 = s_w1 '
Symbol w15 = s_w2 '
Symbol w16 = s_w3 '
Symbol w17 = s_w4 '
Symbol w18 = s_w5 '
Symbol w19 = s_w6 '

'Then use them as normal variables
w16=3
w17= 9*w16
w3=w17  'w3 = 27 as expected
Must confess I haven't tried it on hardware yet
 
Top