Shop OBEX P1 Docs P2 Docs Learn Events
global constant ? — Parallax Forums

global constant ?

Chris MicroChris Micro Posts: 160
edited 2009-05-18 22:39 in Propeller 1
Hi there,

is it possible in SPIN to create global constants which are available in every object in a project?
In "C" there are the header files: you can define gobal constants there and include the header file in every code file.

Is the same thing possible in SPIN?

thanx,
chris

Comments

  • RossHRossH Posts: 5,511
    edited 2009-05-18 07:32
    Hi Chris,

    You can do something similar by declaring constants in one SPIN file, then referring to them in another file using the "#" syntax. For example:

    In file "MY_CONSTANTS.SPIN":

    CON
    CONST_1 = 1
    CONST_2 = 2

    PUB Dummy

    Then, to use these constants in another file:

    OBJ
    MY_CONSTANTS : "MY_CONSTANTS.SPIN"

    CON
    X = MY_CONSTANTS#CONST_1

    PUB
    ...


    This approach has a few drawbacks - for instance, each constant file you have actually consumes some memory. But it may be useful to you.


    Ross.
  • Chris MicroChris Micro Posts: 160
    edited 2009-05-18 08:34
    Hmm, I'm not shure if that helps realiy.

    What I want to have is a file with the hardware ports definitions e.g.

    VGA_PINS = 16
    ...

    Of course the hardware connections are for every object the same and therefore it should be possible to have a global pin definition file.
  • RossHRossH Posts: 5,511
    edited 2009-05-18 08:50
    Hi Chris,

    There have been various attempts to come up with a solution to this exact problem. I don't think any of them are ideal. The issue (as you have discovered) is that basic support for this needs to be built into the language in the first place. Various people have tried "bolt on" solutions to overcome the shortcomings of SPIN - but (again) I'm not aware of any having achieved resounding success. They tend to be either non-standard extensions to the SPIN language, or require the use of a SPIN pre-processor.

    Try searching the forum for various language tools - you could start with bstc, Homespun and SCUM.

    Or you could just use C - see http://forums.parallax.com/showthread.php?p=795326

    Ross.
  • AleAle Posts: 2,363
    edited 2009-05-18 08:56
    My programs are just one big file, so all my constants are global smile.gif. No "objects", almost no SPIN, just asm. Pure, fast, nice, spaghetti-shaped pasm wink.gif

    You should look at the compiled code, I do not think you consume that much memory, the problem is you need at least one PUB per spin file, but that one can be just one abort, break or repeat command, one byte (2 really)and few pointers. It will use 10 bytes. With BST you can remove unused methods smile.gif, and save juicy 2 bytes !!!

    test2.spin
    VAR
    
      byte a, b, c
    
    OBJ
      consts : "consts"
    
    
    PUB start
    
      a := b + consts#const1 * consts#const2
                                   
    
    



    consts.spin
    CON
      const1 = 500
      const2 = 600
    
    PUB nada
      abort    
    
    



    |===========================================================================|
    Objects : -
    test2
      |
      +--consts
    
    Object Address : 0010 : Object Name : test2
    Object Address : 002C : Object Name : consts
    
    Binary Image Information :
    PBASE : 0010
    VBASE : 0038
    DBASE : 0044
    PCURR : 001C
    DCURR : 0048
    |===========================================================================|
    |===========================================================================|
    Object test2
    Object Base is 0010
    |===========================================================================|
    Object Constants
    |===========================================================================|
    |===========================================================================|
    |===========================================================================|
    VBASE Global Variables
    |===========================================================================|
    VBASE : 0000 BYTE Size 0001 Variable a
    VBASE : 0001 BYTE Size 0001 Variable b
    VBASE : 0002 BYTE Size 0001 Variable c
    |===========================================================================|
    |===========================================================================|
    Spin Block start with 0 Parameters and 0 Extra Stack Longs. Method 1
    PUB start
    
    Local Parameter DBASE:0000 - Result
    |===========================================================================|
    11                        a := b + consts#const1 * consts#const2
    Addr : 001C:          88 01  : Memory Op Byte VBASE + READ Address = 0001
    Addr : 001E:       39 01 F4  : Constant 2 Bytes - 01 F4 
    Addr : 0021:       39 02 58  : Constant 2 Bytes - 02 58 
    Addr : 0024:             F4  : Math Op *     
    Addr : 0025:             EC  : Math Op +     
    Addr : 0026:          89 00  : Memory Op Byte VBASE + WRITE Address = 0000
    Addr : 0028:             32  : Return        
    |===========================================================================|
    Object consts
    Object Base is 002C
    |===========================================================================|
    Object Constants
    |===========================================================================|
    |===========================================================================|
    Spin Block nada with 0 Parameters and 0 Extra Stack Longs. Method 1
    PUB nada
    
    Local Parameter DBASE:0000 - Result
    |===========================================================================|
    6                        abort
    Addr : 0034:             30  : Abort         
    Addr : 0035: Data : 32                       2
    
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
  • Chris MicroChris Micro Posts: 160
    edited 2009-05-18 12:26
    Hi Ale,

    I know, assembler is best smile.gif But as you know, I like to learn also SPIN, even it's sometime a little bit odd ....

    Hi RossH,

    thanx for your help smile.gif Very pitty that it is not possible with spin, so I have to copy the global constant block in every object .-(
  • AleAle Posts: 2,363
    edited 2009-05-18 15:03
    Chris: As I demonstrated before, you can use RossH idea losing only 10 bytes in the process.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
  • jazzedjazzed Posts: 11,803
    edited 2009-05-18 18:20
    Chris Micro said...

    thanx for your help smile.gif Very pitty that it is not possible with spin, so I have to copy the global constant block in every object .-(
    The C preprocessor is including that global const block for you.
    Don't Homespun and BSTC both have a preprocessor?
    No preprocessor is of course a glaring missed opportunity in the Propeller tool [noparse]:)[/noparse]
    Chris Micro said...

    In "C" there are the header files: you can define gobal constants there and include the header file in every code file.
    There is not much difference in '#include <const.h>' to get #defines/enum and 'OBJ con : "const.spin" '
    except of course that you add con# to each symbol ... which has many advantages over #defines, etc....

    Enjoy!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • BradCBradC Posts: 2,601
    edited 2009-05-18 22:39
    jazzed said...

    Don't Homespun and BSTC both have a preprocessor?

    Not that can include files.. only conditional compilation.
    jazzed said...

    No preprocessor is of course a glaring missed opportunity in the Propeller tool [noparse]:)[/noparse]

    You just can't be all things to all people.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
Sign In or Register to comment.