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

C51: How calling library in fixed ROM from code in XRAM

I have a 8051 project which includes ROM and XRAM, both are used as code memory
The memory address and range is as follows

CODE Space
Main CODE at RAM C:0x0000 ~ C:0x2FFF
Lib  CODE at ROM C:0x3000 ~ C:0x3FFF

XDATA Space
    XDATA RAM  at X:0x0000 ~ X:0x0FFF

I want to build a library code by Keil C51, and put the library code in fixed ROM memory,
so the code in RAM is able to call the library routine in the existing ROM.
For example:

main.c in CODE RAM (C:0x0000 ~ C:0x2FFF)

main()
{
    int x, y, z;
    x = 1;
    y = 2;
    z = Add(x, y);
}

lib.c in CODE ROM (C:0x3000 ~ C:0x3FFF)

int Add(int x, int y)
{
    int z;
    z = x + y;
    return z;
}

int Sub(int x, int y)
{
    int z;
    z = x - y;
    return z;
}

My question is:
1. Is there easy way to tell Keil C51 where to call library code at (C:0x3000 ~ C:0x3FFF) ? If a lot of routines in library.

2. Is it possible to call library function with arguments ?

3. Can library routine use local variables ?

Thanks for your response

Parents
  • A call table (or function table, vtable, dispatch table, et al.) is a struct of function pointers:

    struct call_table {
       int (*add) (int x, int y);
       int (*sub) (int x, int y);
       //...
    };
    
    struct call_table MY_CALL_TABLE __at(0x3000) = {
       Add,
       Sub,
    };
    

    Then just call the function via the table:

    sum = MY_CALL_TABLE.add(2, 3);
    

Reply
  • A call table (or function table, vtable, dispatch table, et al.) is a struct of function pointers:

    struct call_table {
       int (*add) (int x, int y);
       int (*sub) (int x, int y);
       //...
    };
    
    struct call_table MY_CALL_TABLE __at(0x3000) = {
       Add,
       Sub,
    };
    

    Then just call the function via the table:

    sum = MY_CALL_TABLE.add(2, 3);
    

Children
  • Note that "just call the function via the table" isn't so easy with C51 and interfacing two separate programs.

    C51 will place the parameters in global memory cells. But the calling program will not know what global cells the receiving side have allocated for the parameters. Not only that, but the two programs will have to have two completely separated RAM regions, with the exception of some shared RAM for data passing.

    8051 is not a good processor for trying some more advanced programming concepts.