Memcpy

Hello,

I am new to embedded programming, particularly C, and I am trying to write bytes to code memory, within my already executing code. The purpose is to see if I can write a program to program memory within a program. I believe that is sometimes called a boot loader.

Here is what I have: (It's a simple approach)

#include "REG51XC2.h"
#include <string.h>


unsigned char code code_buffer [16] _at_ 0x0B00;
unsigned char byte = 0x11;
unsigned char * bytep = &byte;

void main(void)
{
        memcpy(code_buffer, bytep, sizeof(code_buffer));
        while(1);
}

What I am trying to do is assign some space in program memory (location 0x0B00) and write a byte there using the method "memcpy".

When I run my emulation, I can see in the disassembler that no where does it use the command "MOVC" which I know to be the assembly way of writing to code memory.

     9: void main(void)
    10: {
    11:         memcpy(code_buffer, bytep, sizeof(code_buffer));
C:0x0982    7800     MOV      R0,#0x00
C:0x0984    7C0B     MOV      R4,#byte(0x0B)
C:0x0986    7DFF     MOV      R5,#0xFF
C:0x0988    AB08     MOV      R3,bytep(0x08)
C:0x098A    AA09     MOV      R2,0x09
C:0x098C    A90A     MOV      R1,0x0A
C:0x098E    7E00     MOV      R6,#0x00
C:0x0990    7F10     MOV      R7,#0x10
C:0x0992    1208D0   LCALL    C?COPY(C:08D0)
    12:         while(1);

The one good thing, though, is that the assignment of "code_buffer" was successful in putting placing it at 0x0B00.

C:0x0AFF    00       NOP
                 code_buffer:
C:0x0B00    00       NOP
C:0x0B01    00       NOP
C:0x0B02    00       NOP

So what my understanding is, is memcpy will allow me to copy values to data memory (internal or external) but not program memory.

With this in mind, does that mean I will be writing a version of the "memcpy" function in assembly to get the data into program memory, or is there another function in C I could use? If so, is there any material I can reference or some code I could use to implement the function above?

I appreciate any help,

Max

Parents
  • A normal 8051 processor is physically unable to write to code memory. There is zero instructions that can do that, so there is zero way you can write a program that can manage this.

    You may be able to find specific 8051 chips that can perform in-application-programming. But not by writing to code memory. They may have methods where specific XDATA memory might overlay code memory. Or they may have methods to write to magic processor registers that will work as a magic window into the code flash. There could exist 8051 cores that have some extra instructions added to the instruction set - but then assemblers/compilers will not be able to produce such code since they would not know about this extension.

    But no normal memcopy() etc can do what you want, since the processor instruction set was never intended to manage what you want. And only the specific documentation for your specific 8051 variant might tell if there are some way to perform in-application-programming - and how that is then implemented.

Reply
  • A normal 8051 processor is physically unable to write to code memory. There is zero instructions that can do that, so there is zero way you can write a program that can manage this.

    You may be able to find specific 8051 chips that can perform in-application-programming. But not by writing to code memory. They may have methods where specific XDATA memory might overlay code memory. Or they may have methods to write to magic processor registers that will work as a magic window into the code flash. There could exist 8051 cores that have some extra instructions added to the instruction set - but then assemblers/compilers will not be able to produce such code since they would not know about this extension.

    But no normal memcopy() etc can do what you want, since the processor instruction set was never intended to manage what you want. And only the specific documentation for your specific 8051 variant might tell if there are some way to perform in-application-programming - and how that is then implemented.

Children
More questions in this forum