Shop OBEX P1 Docs P2 Docs Learn Events
variable declaration in multi-slot BS2pe program — Parallax Forums

variable declaration in multi-slot BS2pe program

davejamesdavejames Posts: 4,047
edited 2014-05-19 13:53 in BASIC Stamp
Hi All,

It seems common (even recommended) to declare variables in all 8 slots of a BS2pe program even if they are not specifically used in a particular slot.

Meaning, if variable XYZ is declared and only used in slot 1, it must be declared in slots 2 through 8 also.

Does anybody know why this is the case?

Thanks.

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-05-14 11:57
    Yes, this is because if you change anything as you move from slot to slot the data in the existing variables could be corrupted. By declaring everything the same in each slot you guarantee that your variables will remain intact when switching from slot to slot.

    Here's an example: Say you have two word variables and a byte variable but in the next slot you only declare the byte. It will now occupy part of the original word variable and corrupt that for when you return.
  • davejamesdavejames Posts: 4,047
    edited 2014-05-14 12:11
    Here's an example: Say you have two word variables and a byte variable but in the next slot you only declare the byte. It will now occupy part of the original word variable and corrupt that for when you return.

    ...ahhh - got it!

    Thanks.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-05-14 17:16
    Note that you can shuttle values back and forth between the main RAM and the BS2pe scratchpad RAM. That allows strategic recycling of the precious RAM variables.

    Essential variables that really need to be used often in multiple banks can be allocated first in every slot. Memory is always allotted to words first in the order they are defined in the program, then all the bytes, then the nibs, then the bits.

    If there are bytes, nibs and bits that need to be conserved between slots, give them an alias name as part of one of the initial words. E.g.,
    myBestByte VAR myGoodWord.byte0
    myBestNib VAR myGoodWord.nib3
    myBestBit VAR myGoodWord.bit12
  • davejamesdavejames Posts: 4,047
    edited 2014-05-15 10:13
    Note that you can shuttle values back and forth between the main RAM and the BS2pe scratchpad RAM. That allows strategic recycling of the precious RAM variables.

    Essential variables that really need to be used often in multiple banks can be allocated first in every slot. Memory is always allotted to words first in the order they are defined in the program, then all the bytes, then the nibs, then the bits.

    If there are bytes, nibs and bits that need to be conserved between slots, give them an alias name as part of one of the initial words. E.g.,
    myBestByte VAR myGoodWord.byte0
    myBestNib VAR myGoodWord.nib3
    myBestBit VAR myGoodWord.bit12

    Hi Mr. Allen - yes, I just discovered this morning how variables are allocated (word, then byte, then nib, then bit); thank you.

    The tip for "shuttling" variable values will probably come in handy as I re-think the construction of an 8-slot program. Up to this point, I've been lucky that cross-slot variables haven't clashed.

    DJ
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-05-16 08:24
    I read an article in Nuts and Volts years ago about preserving variables via the SPRAM, however I found it much easier, simpler and straight forward (for me) to just keep the declarations the same and if I needed a different variable I would use one of the existing ones that I could afford to "destroy". But for me I am generally using most of the same variables across banks.

    The trick in the way you do things is to keep track of any variables that may no longer be properly initialized after you've switched banks. I've seen many people get caught by that when declaring things differently.
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 08:22
    Through all this discussion, I've made a map of variables used and it appears there is no clashing.

    Out of the 208 bits (13 X 16) available, I'm using 197 in combinations of words, bytes, nibs, nib arrays, and a single bit. Doing this even clarifies "shadowing" nib arrays (0) to other nibs.

    And it adds to the documentation that I should have done at the beginning...shame on me :frown:
  • tomcrawfordtomcrawford Posts: 1,129
    edited 2014-05-19 10:39
    I have really really most sincerely wished that the compiler included an "Include" function so you could have the variable declarations (at least the common ones) in one place and didn't have to change them in eight files.
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 11:04
    I have really really most sincerely wished that the compiler included an "Include" function so you could have the variable declarations (at least the common ones) in one place and didn't have to change them in eight files.

    Hi Tom - how would you envision that looking and operating?
  • tomcrawfordtomcrawford Posts: 1,129
    edited 2014-05-19 12:31
    Here is the beginning of an eight-module project I wrote a few years ago.
    
    ' {$STAMP BS2px, Fonts.bpx, Adjust.bpx, Pithy.bpx, Pithy2.bpx, Pithy3.bpx, Pithy4.bpx, Days.bpx}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    
    Days    DATA "SunMonTueWedThuFriSat"
    Months  DATA "JanFebMarAprMayJunJulAugSepOctNovDec"
    
    '++++++++++++++++define the program modules++++++++++
    Main     CON 0
    Fonts    CON 1
    Adjust   CON 2
    Pith     CON 3
    
    
    
    
    '+++++++++++++RAM+++++++++++++++++
    RCols   CON   0               'first 64 bytes are the pattern buffer
    
    
    
    FlagWord  VAR Word                   'must be the first in every module
    FlagByte  VAR FlagWord.LOWBYTE
    FlagNib   VAR FlagByte.LOWNIB
    Started   VAR Flagnib.BIT0          '1 says we been through initialization
    Pithy     VAR FlagNib.BIT1          'get font to display a pithy saying
    NoBD      VAR FlagNib.BIT2          'no birthday boy
    DST       VAR FlagNib.BIT3          'daylight saving time in effect
    Lite      VAR FlagByte.HIGHNIB     'current brightness variable
    HB        VAR Flagword.HIGHBYTE    'high byte
    PPnt      VAR HB.HIGHNIB           'which module contains the saying
    Quick     VAR HB.LOWNIB            'throttle
    TWord0    VAR Word
    Secs      VAR TWord0.HIGHBYTE      'BCD Seconds
    Mins      VAR TWord0.LOWBYTE       'BCD Minutes
    TWord1    VAR Word
    Hrs       VAR TWord1.HIGHBYTE      'BCD Hours
    Day       VAR TWord1.LOWBYTE       'BCD Day of week
    TWord2    VAR Word
    Date      VAR TWord2.HIGHBYTE      'BCD Date of month
    Month     VAR TWord2.LOWBYTE       'BCD Month
    Saying    VAR TWord2               'contains the address of the current saying.  Reuses Date/Month
    TWord3    VAR Word
    Year      VAR TWord3.HIGHBYTE      'BCD Year
    Fahr      VAR TWord3.LOWBYTE       'Bin temperature
    Point     VAR Word
    IPnt      VAR Point.HIGHBYTE       'ram input pointer
    OPnt      VAR Point.LOWBYTE        'ram output pointer
    RNum      VAR Word                 'random saying selector
    'from here down is local to each module
    WVar      VAR Word               'generally useful word varialbe
    BVar      VAR Byte
    Dat       VAR Byte               'data to/from clock
    i         VAR Byte
    j         VAR Byte
    Device    VAR Byte
    Column    VAR Byte
    Col16     VAR    Column.LOWNIB                'low order bits of column
    Color     VAR Byte
    Adrs      VAR Byte
    Pattern   VAR Byte
    
    
    begin:
    IF (Started = 0) THEN GOTO StartUp              'need to go initialize the world
    

    Note that the first half-dozen or words of the variable area needs to be identical for all modules. It would be nice to move that to a separate file, say IDECLARE.bpx which is called by each module (including this one). Then to make a change, one would merely make it in IDECLARE. Just saves a little bookkeeping. But I think that Parallax would have done this about ten years ago if it was gonna happen.
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 12:55
    Then to make a change, one would merely make it in IDECLARE. Just saves a little bookkeeping. But I think that Parallax would have done this about ten years ago if it was gonna happen.

    OK - I understand what you're saying. Any, yeah, it would have been done initially if done at all.

    Later,

    DJ
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 13:06
    Hey Mr. Savage,

    More questions concerning variables - this time the order in which they are placed in the variable space.

    Given:
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    j VAR Byte
    k VAR Byte
    
    n_ary VAR Nib(3)
    
    bt1 VAR Bit
    bt2 VAR Bit
    bt3 VAR Bit
    
    END
    

    Are the variables inserted into the variable register(s) as shown below?

    variablesChart_b.png


    Thanks much.
    452 x 412 - 16K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-05-19 13:11
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 13:44
    Yes

    Yup - I was aware of the register display in the IDE, just not sure if the example "j" and "k" were inserted in the order specified by the code ("j" declared first, then "k").

    I could tell from the IDE display the order in which word/byte/nib/bit variables were allocated but didn't know if arrays were inserted starting with index zero.

    Thanks again.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-05-19 13:46
    It's always low-byte then high-byte allocations.
  • davejamesdavejames Posts: 4,047
    edited 2014-05-19 13:53
    It's always low-byte then high-byte allocations.

    ...roger that.
Sign In or Register to comment.