Shop OBEX P1 Docs P2 Docs Learn Events
Stacks and Subroutines — Parallax Forums

Stacks and Subroutines

Lee MarshallLee Marshall Posts: 106
edited 2007-10-05 14:19 in Propeller 1
I have a couple questions:

Firstly:
what gets loaded into cog 0 at startup? the Main Routine? the whole program??

Secondly:
Where is the stack held(address) for cog0 at startup?
i assume it needs a stack, as whenever you start a new cog, you need to provide a stack address.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Holy parallel processing, Batman!

Comments

  • hippyhippy Posts: 1,981
    edited 2007-10-04 16:27
    Firstly ... A chunk of Assembler code loaded from internal Rom which can interpret the bytecode your downloaded Spin program consists of. This "Spin Interpreter" then reads the bytecode from the Hub memory and obeys what those bytecode instructions mean. Spin code always stays in Hub memory.

    Secondly ... The stack is placed at the end of all your program code and data when downloaded. A word of Hub memory in a pre-defined location indicates where its start is, and this is set by the Propeller Tool as part of the compilation process.
  • Lee MarshallLee Marshall Posts: 106
    edited 2007-10-04 16:33
    ah, i see.. idea.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Holy parallel processing, Batman!
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-04 16:33
    1) Initially the boot loader gets loaded into cog 0. This is an assembly program that first tries to communicate with a PC using I/O pins 30/31, then tries to communicate with an EEPROM on pins 28/29 if the PC isn't there or isn't responding as expected (like the Propeller Tool's downloader). If there's a PC or EEPROM present, the HUB memory gets loaded with a 32K program and the boot loader transfers control to another assembly program which is the Spin interpreter (which gets loaded into cog 0's memory on top of the boot loader). The Spin interpreter remains in cog 0's memory interpreting the Spin program in main memory. None of the Spin program actually gets loaded into any cog.

    2) Neither the boot loader nor the Spin interpreter use a stack. Assembly programs do not require a stack. The call/return mechanism used in the "real" instruction set doesn't use a stack. When you startup a new cog using Spin, the new cog actually gets loaded with a copy of the Spin interpreter. The Spin program makes use of (requires) a stack which is supplied by the COGNEW/COGINIT call. When starting an assembly program, the "stack" parameter is actually a general purpose 14 bit parameter that can be used in any way, but usually is used to pass an address to the assembly program.
  • Lee MarshallLee Marshall Posts: 106
    edited 2007-10-04 16:51
    but what about cog 0 when you first turn the chip on, what does its spin interpreter use as a stack?
    from what hippy said, it seems that the propeller tool defines a stack address upon compilation. is there some way to tell what address this is?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Holy parallel processing, Batman!
  • hippyhippy Posts: 1,981
    edited 2007-10-04 17:10
    Mike's description of the initial start-up for Cog 0 was a lot more comprehensive than mine was.

    The Spin Interpreter running in a Cog does not itself use a stack held in Hub memory. Spin code doesn't "run", only in a notional sense; a Cog runs the Interpreter which uses what the Spin bytecode is to determine what to do and that includes manipulating the stack used by Spin program.

    The first 16 bytes of all Propeller object code are initialisation used to configure after bootload and reset. The base of the stack used is held as a word in bytes $000A (LSB) and $000B (MSB) of the .eeprom image.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-04 17:12
    Ah!

    The Propeller Tool knows the last address used by the program. The default stack (used by your program when it first starts) begins just beyond the end of your program. Its address is stored by the Propeller Tool in locations $E/$F in the program and the Spin interpreter picks it up from there during its initialization. The address in locations $A/$B is normally the "base" of the stack while that in $E/$F is the actual stack pointer value. There are some special cases where they might be different.

    Post Edited (Mike Green) : 10/4/2007 5:17:41 PM GMT
  • Lee MarshallLee Marshall Posts: 106
    edited 2007-10-05 13:41
    ok, new question:
    is there some way to make the prop, at startup, load code from main ram(asm) into cog 0, rather than load the spin interpreter??
    is it part of the initialization bytes at the top of the memory??

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Holy parallel processing, Batman!
  • hippyhippy Posts: 1,981
    edited 2007-10-05 14:19
    The hardware ( for want of a better term ) of the Propeller Chip forces Cog 0 to be loaded with the Spin Interpreter and that will expect a Spin program which is then interpreted. The best that can be done is to have that Spin program consist of a COGNEW which launches Assembler Code.

    What most third party 'Propeller Assemblers' will do is prefix this bit of Spin Code to the assembled code, so once the Propeller is reset, bootloads from Eeprom, the Assembler code runs as if it itself had been bootloaded.

    Somewhere on the forum there is an example from Chip of how this Spin Code Prefix can be coded to use minimal Propeller resources.
Sign In or Register to comment.