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