Shop OBEX P1 Docs P2 Docs Learn Events
Catalina C statement syntax question — Parallax Forums

Catalina C statement syntax question

In spin we can write stuff like this...
   Byte[10] := 65
   Word[10] := 65
   Long[10] := 65

I just used address 10 as a random number that popped in my head but my question relates to any address
And so each of the above statements plugs a value in to a memory location in Main Ram, be it a byte, word or long value.

How do I do the same thing in C?
I don't want to create a variable and assign the value to the variable,
I want to poke the value into a memory address...

I know that in Propeller C, with the aid of SimpleTools you can use stuff like HUBDATA

But what if, I want to use Catalina C? (because of its XMM support),
Because I'm guessing that wont work in Catalina C (unless the simpletools libraries are used? this is a guess because I just started with C...)
And I prefer to use Catalina's own library instead of simpletools,
As I would prefer to use the SImpletools library with Propeller C only.

Do I have to use an include file? and what include file do I use?

Is there keyword list with simple C samples describing syntax and usage?
Like a for dummies book?
Most times I resort to a google search, which although correct for most computer C programs, it doesn't usually work with a micro controller.

Is there a list of the functions supported by each of the include files in the library?
What is the exact folder location of the Library files included with the installation?,

Or is there ONE book that I can buy and it will contain everything?
Like a Propeller Bible or something?

I'm finding I'm spending more time trying to find the right syntax for stuff than actually working on the stuff.

Thanks.






Comments

  • RaymanRayman Posts: 14,639
    In FlexC on P2, I found code that did it like this:
    uint8_t peekMemory(uint16_t address)
    {
        uint8_t* p, c;
        p=0;
        return p[address];
    }
    
    I think that might work here too.
  • Or is there ONE book that I can buy and it will contain everything?
    Not likely, but a great place to start is an updated version of the K&R (I have an original). Also, check Humble Bundle for programming books, especially those from No Starch Press -- you can save a lot of money that way (I tend to buy all of their programming bundles).

    What you want to research is pointers.
    I'm finding I'm spending more time trying to find the right syntax for stuff than actually working on the stuff.
    The only way to be productive with any language is to understand the intricacies of its syntax.

    Based on a bit of Googling, you might try either of these approaches.
      uint8_t  *bpntr=(uint16_t *)0x000A = 65; 
      uint16_t *wpntr=(uint16_t *)0x000A = 65; 
      uint32_t *lpntr=(uint16_t *)0x000A = 65;
    
      uint8_t  *bpntr=(uint16_t *)0x000A; 
      uint16_t *wpntr=(uint16_t *)0x000A; 
      uint32_t *lpntr=(uint16_t *)0x000A; 
    
      *bpntr = 65;
    

  • Dave HeinDave Hein Posts: 6,347
    edited 2020-12-14 20:54
    The literal translation to C would be
        *(unsigned char)10 = 65;
        *(short *)10 = 65;
        *(long *)10 = 65;
    
    Note, on the P1 longs are aligned to 4-byte boundaries, so the address 10 actually maps to location 8. Also, since int is 4 bytes it could be used instead of long.

    EDIT: I think you could also write Word[10] as ((short *)0)[10], so you could do something like this:
    #define Byte ((unsigned char *)0)
    #define Word ((short *)0)
    #define Long ((long *)0)
    
    ....
    
        Byte[10] = 65;
        Word[10] = 65;
        Long[10] = 65;
    
    C is case-sensitive so Long and long are two different symbols.
  • Rayman wrote: »
    In FlexC on P2, I found code that did it like this:
    uint8_t peekMemory(uint16_t address)
    {
        uint8_t* p, c;
        p=0;
        return p[address];
    }
    
    I think that might work here too.


    Thank you Ray,
    I had thought after using simpleide that it was some special technique to write to the ram,
    like a special method to write to main ram and another special method to write to cog ram,
    specifically to address the Propeller hardware.

    So I was simply confusing myself...


  • JonnyMac wrote: »
    Or is there ONE book that I can buy and it will contain everything?
    Not likely, but a great place to start is an updated version of the K&R (I have an original). Also, check Humble Bundle for programming books, especially those from No Starch Press -- you can save a lot of money that way (I tend to buy all of their programming bundles).

    What you want to research is pointers.
    I'm finding I'm spending more time trying to find the right syntax for stuff than actually working on the stuff.
    The only way to be productive with any language is to understand the intricacies of its syntax.

    Based on a bit of Googling, you might try either of these approaches.
      uint8_t  *bpntr=(uint16_t *)0x000A = 65; 
      uint16_t *wpntr=(uint16_t *)0x000A = 65; 
      uint32_t *lpntr=(uint16_t *)0x000A = 65;
    
      uint8_t  *bpntr=(uint16_t *)0x000A; 
      uint16_t *wpntr=(uint16_t *)0x000A; 
      uint32_t *lpntr=(uint16_t *)0x000A; 
    
      *bpntr = 65;
    


    Thank you JonnyMac,

    It was the pointers area of C that confused me most,
    But I did a quick google for the book and think I've found, it...
    I've sent you a PM regarding that...

    I'll just buy that one if its the right one, its a second edition though?
    And pointers.... Gosh how much that has troubled me...
    Not the understanding, because I know what a pointer is, but the syntax formatting of the command,

    Like in your example above, Some variables/names have an asterisk to the left, and in other places to the right,
    but it's no worry now, Once I get the book I should be fine and cheers too!!
  • Dave Hein wrote: »
    The literal translation to C would be
        *(unsigned char)10 = 65;
        *(short *)10 = 65;
        *(long *)10 = 65;
    
    Note, on the P1 longs are aligned to 4-byte boundaries, so the address 10 actually maps to location 8. Also, since int is 4 bytes it could be used instead of long.

    EDIT: I think you could also write Word[10] as ((short *)0)[10], so you could do something like this:
    #define Byte ((unsigned char *)0)
    #define Word ((short *)0)
    #define Long ((long *)0)
    
    ....
    
        Byte[10] = 65;
        Word[10] = 65;
        Long[10] = 65;
    
    C is case-sensitive so Long and long are two different symbols.



    Thank You Dave,

    The literal translation helps, since that conversion (of a spin object to C) effort that i has a go at the other day is my first ever serious attempt at C...

    But wow! Gosh your example with the #define definitions is just superior to everything else, it just makes it look so easy, and so simple to understand...
    That's the method I will use, since a simple definition means I can keep the old syntax of Byte[N] = X and it seems much more friendly to look at 1, 2, 3 years down the line.
    Currently I look at stuff I wrote 15-20 years ago and scratch my head and wonder whats going lol (its commented but not commented enough sadly)
    #define Byte ((unsigned char *)0)
    #define Word ((short *)0)
    #define Long ((long *)0)
    ....
        Byte[10] = 65;
        Word[10] = 65;
        Long[10] = 65;
    


Sign In or Register to comment.