mrburnette
Senior Member
There has been several posts of recent about the named variables limitations: that is, B0-B27 in the case of an 08M2. But, as has been noted there is additional RAM in the 08M2, but it is not "named" and therefore PEEK/POKE are the access methods.
What if we consider the 08M2 RAM as 4 banks with only the lower BANK0 having variable names: B0-B27 / W0-W13... so the scenario will apply to other chips in the PICAXE line, but they will simply contain more banks since the 08M2 has the least amount of RAM of the currently shipping chips.
Problem to solve: Have more simple-to-use variables without having to keep track of PEEK/POKE addresses
A solution: Consider extended RAM, above that directly addressable with variable names, as banks of RAM
Implementation:
View attachment 9565
Explanation and possible usage:
Run the code snippet in the Simulator and watch the Memory map (see JPG example above) fill as the Simulator walks the DEMO code.
The POKE to starting locations 28, 56, and 84 represent the beginning of a memory block. IF we call lower memory represented by B0-B27 as BANK0, then locations 28/56/84 would represent BANK 1/2/3 respectively.
To use this concept, write modular code. Before each subroutine call, swap main memory, BANK0 with a holding location BANK1, BANK2, or BANK3. Save your subroutine state if necessary and restore BANK0 before the RETURN statement.
If you need a few PUBLIC variables to communicate across routines, etc. then just modify the PEEK/POKE statements accordingly along with the BANK indexes so that the entire BANK0 is not saved/restored.
Same concept could be applied to chip models with scratchpad memory to form an even deeper stack.
- Ray
What if we consider the 08M2 RAM as 4 banks with only the lower BANK0 having variable names: B0-B27 / W0-W13... so the scenario will apply to other chips in the PICAXE line, but they will simply contain more banks since the 08M2 has the least amount of RAM of the currently shipping chips.
Problem to solve: Have more simple-to-use variables without having to keep track of PEEK/POKE addresses
A solution: Consider extended RAM, above that directly addressable with variable names, as banks of RAM
Implementation:
Code:
' DEMO of saving named variables and restoring... 3 levels deep
' M. Ray Burnette, 09 Nov 2011
' Refer to Manual 2, page 11 and other parts as appropriate
'
' 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 a higher partition
' Select one of the following RAM OFFSET INDEXES: 28, 56, or 84
'
bptr = 0
' BANK 1 _____________________________________>
Poke 28, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
bptr = 0
' BANK 2 _____________________________________>
Poke 56, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
bptr = 0
' BANK 3 _____________________________________>
Poke 84, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
'
'
' For this DEMO, let's clear Bank 0 before each restore
for bptr = 0 to 27 : @bptr = 0 : next
'
' Now restore Bank 1, 2, 3 into Bank 0 (named variables)
bptr = 0
' BANK 1 _____________________________________>
Peek 28, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
'
for bptr = 0 to 27 : @bptr = 0 : next
'
bptr = 0
' BANK 2 _____________________________________>
Peek 56, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
'
for bptr = 0 to 27 : @bptr = 0 : next
'
bptr = 0
' BANK 3 _____________________________________>
Peek 84, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc
END
Explanation and possible usage:
Run the code snippet in the Simulator and watch the Memory map (see JPG example above) fill as the Simulator walks the DEMO code.
The POKE to starting locations 28, 56, and 84 represent the beginning of a memory block. IF we call lower memory represented by B0-B27 as BANK0, then locations 28/56/84 would represent BANK 1/2/3 respectively.
To use this concept, write modular code. Before each subroutine call, swap main memory, BANK0 with a holding location BANK1, BANK2, or BANK3. Save your subroutine state if necessary and restore BANK0 before the RETURN statement.
If you need a few PUBLIC variables to communicate across routines, etc. then just modify the PEEK/POKE statements accordingly along with the BANK indexes so that the entire BANK0 is not saved/restored.
Same concept could be applied to chip models with scratchpad memory to form an even deeper stack.
- Ray