This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

General Question Re: Memory Models and Abstract Pointers

Hi All.

I'm becoming more familiar with C51's various memory models, but I could use some confirmation on one issue.

Here's a code snippet:

char CODE_B = 'B';
char CODE_C = 'C';

...

void TaskShow( void )
{
    OStypeMsgP msgP;
    
    InitPORT();
    PORT = 0x00;
    
    for (;;) {
        OS_WaitMsg(MSG_UPDATE_PORT_P, &msgP, OSNO_TIMEOUT, TaskShow1);
        
        if ( *(char *)msgP == CODE_C ) {
            PORT &= ~0xFE;
            PORT |= ( counter >> 8 ) & 0xFE; 
        }
        else
            PORT ^= 0x01; 
    }
}

where OStypeMsgP is void *.

For the SMALL memory model, this code works fine. For the LARGE memory model (with variables in XDATA), I must change the line that dereferences the pointer to
if ( *(char xdata *)msgP == CODE_C ) {

Makes sense to me ...

1) Could someone just verify that this is the intended behavior of variables that do not have an explicit memory type? I.e. if I do not add an explicitly declared memory type (e.g. data|xdata|idata|pdata|...) to the variable's declaration, then will it automatically be in the selected memory model's default memory area?

2) From what I can see, this means that "abstract pointers" are always of type void *, and one must simply dereference them properly, using the explicitly declared memory type in the dereferencing cast. Correct?

Thanks,

Parents
  • Andrew, I'm afraid I don't have much to add here. I'd have done the message object a bit differently to avoid the 8051 data space specifier conundrum but as I'm sure you're well aware, there's not much you can do with a typedef once you've declared it except use it.

    For the message object I'd simply forgo the pointer version and supply the base type letting user create a pointer from it. E.g.

    typedef enum MsgTypes
    {
        MSG_COMPLEX,
        MSG_A,
        MSG_B,
        MSG_C,
        NUM_MSGS
    } MsgTypes;
    
    typedef struct o_complex
    {
        int  var;
        char arr[5];
    } o_complex;
    
    typedef struct o_msg
    {
        MsgTypes            type;
        union MsgBody
        {
            o_complex       complex;
            unsigned long   apple;
            unsigned int    butter;
            unsigned char   cheese;
        } body;
    } o_msg;
    
    int main(void)
    {
        /* Pointer resides in IDATA but points only to DATA space
        */
        o_msg idata * data pMsg;
    
        pMsg->type        = MSG_A;
        pMsg->body.cheese = 'a';
    
        /* blah, blah, blah...
        */
        for (;;);
    
        return 0;
    }

    If you think I might be of any further assistance, maybe we should take it off-line, :-)

    Regards,

    - Mark

Reply
  • Andrew, I'm afraid I don't have much to add here. I'd have done the message object a bit differently to avoid the 8051 data space specifier conundrum but as I'm sure you're well aware, there's not much you can do with a typedef once you've declared it except use it.

    For the message object I'd simply forgo the pointer version and supply the base type letting user create a pointer from it. E.g.

    typedef enum MsgTypes
    {
        MSG_COMPLEX,
        MSG_A,
        MSG_B,
        MSG_C,
        NUM_MSGS
    } MsgTypes;
    
    typedef struct o_complex
    {
        int  var;
        char arr[5];
    } o_complex;
    
    typedef struct o_msg
    {
        MsgTypes            type;
        union MsgBody
        {
            o_complex       complex;
            unsigned long   apple;
            unsigned int    butter;
            unsigned char   cheese;
        } body;
    } o_msg;
    
    int main(void)
    {
        /* Pointer resides in IDATA but points only to DATA space
        */
        o_msg idata * data pMsg;
    
        pMsg->type        = MSG_A;
        pMsg->body.cheese = 'a';
    
        /* blah, blah, blah...
        */
        for (;;);
    
        return 0;
    }

    If you think I might be of any further assistance, maybe we should take it off-line, :-)

    Regards,

    - Mark

Children