Shop OBEX P1 Docs P2 Docs Learn Events
array of data in cog memory? — Parallax Forums

array of data in cog memory?

jcookjcook Posts: 8
edited 2013-12-10 18:58 in Propeller 1
Is there a way in C to create an array of variables in cog memory?
I am using SimpleIDE with cog_c_toggle as an example. I tried this line to create the array of data that I want:
static _COGMEM unsigned char pdata[512];
When I build I get this error:
cog_array_fw.cogc:10:30: error: data type of 'pdata' isn't suitable for a register
What is the proper way to declare an array and have it in cog memory?

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-12-09 02:32
    Not sure about doing this purely in C. You might be able to allocate somethinng in PASM to be used within C.

    Are you trying to store 8 bit characters? CogRam is all 32bit and nothing else unless you provide the code to convert data.

    I have no problem doing this in PropForth as there is quite a bit of extra space in each cog. The only problem is the StopCog and Reset dump the data. If you are going to do this, you have to deal with more volatility than in HubRam. And wehn the data is lost, the contents of the CogRam are indeterminate.. they are not all a predicitable default value. So you have to initialize again. I suppose that means a shadow image of the array in HubRam.
  • ersmithersmith Posts: 6,054
    edited 2013-12-09 04:49
    jcook wrote: »
    Is there a way in C to create an array of variables in cog memory?
    No, there is no way to do this in (pure) C. You'd have to use some inline PASM to create the self-modifying code that is necessary for accessing arrays in COG memory.

    What are you trying to accomplish? There may be another way to achieve your goal. An array in HUB memory often performs almost as well as one in COG memory (the cost of HUB memory accesses is often hidden by the compiler, and COG memory arrays are slowed down by the requirement to add 1 cycle after modifying the access instruction).
  • Heater.Heater. Posts: 21,230
    edited 2013-12-09 05:20
    Yep, and as this is a array of bytes here putting in HUB is probably quicker. There is no packing/unpacking from longs to do.
    (You could keep each byte in a long COG register wasting three bytes of space each to save packing/unpacking, but that is, well, a waste).
  • jcookjcook Posts: 8
    edited 2013-12-09 20:40
    ersmith wrote: »
    No, there is no way to do this in (pure) C. You'd have to use some inline PASM to create the self-modifying code that is necessary for accessing arrays in COG memory.

    Could you point me to some information on the self modifying code? I have never seen an example of that and it sounds like an interesting thing to investigate.

    As for what I am trying to accomplish, I stumbled across the propeller in a web search and thought it would be a neat thing to play with so I bought a quickstart board and am converting some code I used for digital pattern generation as a learning experience.
  • ersmithersmith Posts: 6,054
    edited 2013-12-10 04:58
    jcook wrote: »
    Could you point me to some information on the self modifying code? I have never seen an example of that and it sounds like an interesting thing to investigate.
    Chapter 3 of the Propeller manual (the section on Assembly Language) is well worth looking at. The Propeller assembly language (PASM) is pretty straight-forward.

    COG memory is organized as 32 bit registers, which also hold instructions. The memory read operations (like RDBYTE) that the C compiler uses can only access HUB memory, not COG memory, which is why C doesn't allow arrays in COG memory. If you did want an array in COG memory you'd have to do something like:
       '' set element X[i] to Y
        add I, X
        movd ins, I
        nop  '' 1 cycle delay necessary to avoid pipeline conflict
    ins
        mov 0-0, Y  '' the actual destination is set above
    

    The movd and movs instructions are specifically designed for modifying the destination and source fields of instructions.
    As for what I am trying to accomplish, I stumbled across the propeller in a web search and thought it would be a neat thing to play with so I bought a quickstart board and am converting some code I used for digital pattern generation as a learning experience.

    Welcome to the Propeller world! It is a very neat processor. If you're just starting out, I'd recommend keeping things simple and just using HUB memory to start with. Porting code from other environments is pretty straightforward with the C compiler. Once you have it working then you can start thinking about optimizing it with inline assembly or COGMEM declarations.

    Regards,
    Eric
  • jcookjcook Posts: 8
    edited 2013-12-10 18:58
    Thanks for all the advice. I will keep playing with the propeller and learning.
Sign In or Register to comment.