How to read ROM source byte and load it to RAM destination byte

Thread Starter

embedtechnology

Joined Dec 25, 2015
42
hi! I am new in c programing .And I have a doubt . I have arranged a 2 dimensional arry in a 8051 RAM space named buffer[ 8 ][ 6 ] and a lookup arry in ROM space of the same 8051 named lookup[ 100 ] at absolute 0x0600
My code :
unsigned short i=0 , j=0 ;
unsigned int lookup_addr=0x0600;
buffer[j]=lookup[lookup_addr];
Is the last line of code here right for micro c compiler?
 
Last edited:

Papabravo

Joined Feb 24, 2006
18,786
hi! I am new in c programing .And I have a doubt . I have arranged a 2 dimensional arry in a 8051 RAM space named buffer[ 8 ][ 6 ] and a lookup arry in ROM space of the same 8051 named lookup[ 100 ] at absolute 0x0600
My code :
unsigned short i=0 , j=0 ;
unsigned int lookup_addr=0x0600;
buffer[j]=lookup[lookup_addr];
Is the last line of code here right for micro c compiler?
That's a good question. Since you have assigned an absolute address and not declared your data in separate memory blocks, how is the compiler supposed to know the difference between using the MOVC instruction to read data from Program Memory using the PSEN* control signal, or the MOVX instruction to access the Data Memory using the RD* and WR* control signals?
 

ApacheKid

Joined Jan 12, 2015
720
hi! I am new in c programing .And I have a doubt . I have arranged a 2 dimensional arry in a 8051 RAM space named buffer[ 8 ][ 6 ] and a lookup arry in ROM space of the same 8051 named lookup[ 100 ] at absolute 0x0600
My code :
unsigned short i=0 , j=0 ;
unsigned int lookup_addr=0x0600;
buffer[j]=lookup[lookup_addr];
Is the last line of code here right for micro c compiler?
You should ideally post all the code because I can't see how buffer of lookup are actually defined.

Anyway, the last line looks incorrect, the RHS is referencing offset 0x0600 from whatever the address is of lookup.

Try this:

Code:
unsigned short i=0 , j=0 ;

unsigned char * lookup_addr=0x0600; // initialize the pointer to this absolute address.

buffer[j]=lookup_addr[j]; // loop and inctement 'j'
However it would be better to use 'memcpy' take a look at that function, its a standard part of the C library.
 

Papabravo

Joined Feb 24, 2006
18,786
You should ideally post all the code because I can't see how buffer of lookup are actually defined.

Anyway, the last line looks incorrect, the RHS is referencing offset 0x0600 from whatever the address is of lookup.

Try this:

Code:
unsigned short i=0 , j=0 ;

unsigned char * lookup_addr=0x0600; // initialize the pointer to this absolute address.

buffer[j]=lookup_addr[j]; // loop and inctement 'j'
However it would be better to use 'memcpy' take a look at that function, its a standard part of the C library.
You still have not addressed the ambiguity of which of the two memory spaces the offset of 0x600 refers to. Does it refer to Program Memory Space or Data Memory Space. 8051 compilers need some help on this one. The TS, in his original post, referred to ROM and RAM as potential synonyms for Program Memory Space and External Data Memory Space. Now he has to find out how to tell the micro c compiler what he means.
 
Last edited:

Thread Starter

embedtechnology

Joined Dec 25, 2015
42
[QUTE="Papabravo, post: 1739308, member: 5202"]
That's a good question. Since you have assigned an absolute address and not declared your data in separate memory blocks, how is the compiler supposed to know the difference between using the MOVC instruction to read data from Program Memory using the PSEN* control signal, or the MOVX instruction to access the Data Memory using the RD* and WR* control signals?
[/QUOTE]
You still have not addressed the ambiguity of which of the two memory spaces the offset of 0x600 refers to. does it refer to Program Memory Space or Data Memory Space. 8051 compilers need some help on this one. The TS, in his original post, referred to ROM and RAM as potential synonyms for Program Memory Space and External Data Memory Space. Now he has to find out how to tell the micro c compiler what he means.
 

Thread Starter

embedtechnology

Joined Dec 25, 2015
42
Actually my arry declaration is like this .
unsigned short const lookup[ 100 ]={0x78, 0xcc ,0xd0,0x28 ----- 0xcc } absolute 0x0600; code ;
RAM buffer is initiated as

unsigned short dsp_buff[8][6] absolute 0x60 ; idata;

My intention is to load first ram_buff location [0][0]with the data of lookup location 0x0600 .

I tried this :
dsp_buff [j]=lookup[read_code]

where i , j are unsigned short type and read_code is short int type ( 2 bytes)
 

Papabravo

Joined Feb 24, 2006
18,786
It has a good shot at being correct. So, what does the generated code look like? Your compiler does let you look at the generated code – right?
 

ApacheKid

Joined Jan 12, 2015
720
Actually my arry declaration is like this .
unsigned short const lookup[ 100 ]={0x78, 0xcc ,0xd0,0x28 ----- 0xcc } absolute 0x0600; code ;
RAM buffer is initiated as

unsigned short dsp_buff[8][6] absolute 0x60 ; idata;

My intention is to load first ram_buff location [0][0]with the data of lookup location 0x0600 .

I tried this :
dsp_buff [j]=lookup[read_code]

where i , j are unsigned short type and read_code is short int type ( 2 bytes)
What is this "absolute" keyword?
 

Papabravo

Joined Feb 24, 2006
18,786
'absolute' is a keyword you might use if you were not planning on having a "linkage editor" relocate and assign blocks of code and data into their respective locations. Using a "linkage editor" supports code portability and reusability across multiple platforms.
 

ApacheKid

Joined Jan 12, 2015
720
absolute key word determines the location of the arry . in other word , the hex address writen just after the key word is the starting address of the arry elements .
I've never seen it before, its not part of any C language standard is it? I don't see any difference between 0x1000 absolute and 0x1000, they are just literals, constants.

Oh I think I see, its telling the compiler what address the declared item must have; but I've never seen that keyword, I might be rusty and have only dabbled with C outside of minicomputers and PCs.

If I search Google for this I get pretty much nothing other than the "abs" function:

c language "absolute" keyword
 

Papabravo

Joined Feb 24, 2006
18,786
I've never seen it before, its not part of any C language standard is it? I don't see any difference between 0x1000 absolute and 0x1000, they are just literals, constants.

Oh I think I see, its telling the compiler what address the declared item must have; but I've never seen that keyword, I might be rusty and have only dabbled with C outside of minicomputers and PCs.

If I search Google for this I get pretty much nothing other than the "abs" function:

c language "absolute" keyword
Normally, in a program written in the C-language there is no need to place items at absolute addresses. This is not the case with an embedded processor. The C-language became so popular as a means of writing programs that for embedded processors there were compiler specific "extensions" to the C-language for specific processors or classes of processors.

If you look at C-compilers for 8051 processors you will notice several keywords as part of an overall language extension.
 

ApacheKid

Joined Jan 12, 2015
720
Normally, in a program written in the C-language there is no need to place items at absolute addresses. This is not the case with an embedded processor. The C-language became so popular as a means of writing programs that for embedded processors there were compiler specific "extensions" to the C-language for specific processors or classes of processors.

If you look at C-compilers for 8051 processors you will notice several keywords as part of an overall language extension.
Thanks, that's interesting and a neat keyword.
 

click_here

Joined Sep 22, 2020
544
I'm not sure what compiler you are using, but this tutorial suggests that you use the "code" keyword.

It should also be noted that using the word 'const' does not guarantee that the variable will be put into non-volatile memory, but rather that it generates an error when assigned a value after initialisation. Be familiar with the compiler that you are using and how/if a variable is put in to non-volatile memory.

Where the variable is put is controlled by the implementation.
 

Papabravo

Joined Feb 24, 2006
18,786
I'm not sure what compiler you are using, but this tutorial suggests that you use the "code" keyword.

It should also be noted that using the word 'const' does not guarantee that the variable will be put into non-volatile memory, but rather that it generates an error when assigned a value after initialisation. Be familiar with the compiler that you are using and how/if a variable is put in to non-volatile memory.

Where the variable is put is controlled by the implementation.
You need to be very careful with your terms when talking about the 8051 architecture. It has BOTH non-volatile data memory AND non-volatile Code Memory. Different instruction are REQUIRED to access the respective memories.
 

click_here

Joined Sep 22, 2020
544
You need to be very careful with your terms when talking about the 8051 architecture. It has BOTH non-volatile data memory AND non-volatile Code Memory. Different instruction are REQUIRED to access the respective memories.
I wasn't being specific with the 8051 environment, but on the C language.
 

nsaspook

Joined Aug 27, 2009
9,985
SDCC
I wasn't being specific with the 8051 environment, but on the C language.
These types of things are implementation dependent in the C standard. Many modern compilers tend to use something like the GCC attribute.
https://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/

but there are many other ways unfortunately.
https://developer.arm.com/documenta...attribute----at-address----variable-attribute
https://gcc.gnu.org/onlinedocs/gcc/AVR-Variable-Attributes.html#AVR-Variable-Attributes

For pre-defined hardware memory locations a casted pointer is usually used.
https://www.ibm.com/docs/en/i/7.2?topic=program-casting-pointers
 

Papabravo

Joined Feb 24, 2006
18,786
I wasn't being specific with the 8051 environment, but on the C language.
I wasn't talking about the C-language, but about the use of the term non-volatile memory. I was pointing out that there could possibly multiple types of non-volatile memory with different access attributes. One is read-write and the other is read-only. This distinction was never contemplated on the original machine that had a C-compiler (PDP-7).
https://en.wikipedia.org/wiki/PDP-7
 

click_here

Joined Sep 22, 2020
544
I wasn't talking about the C-language, but about the use of the term non-volatile memory
Maybe you should re-read this part of the post...
Be familiar with the compiler that you are using and how/if a variable is put in to non-volatile memory
I feel that you have not understood what I was trying to say...
It should also be noted that using the word 'const' does not guarantee that the variable will be put into non-volatile memory, but rather that it generates an error when assigned a value after initialisation. Be familiar with the compiler that you are using and how/if a variable is put in to non-volatile memory
In particular the part that suggest to "Be familiar with the compiler"...
 
Top