what is the difference between data and program memory? help me please..
Data memory = where you place your variables. You can read and write values.
Program memory = where the application is stored. Some chips allows parts of the program memory to be modified in blocks (segments), but you can't store variables in the program memory. It is normally possible to store constants - i.e. initialized variables that you do not change - in the program memory.
Your PC also has data memory and program memory. But the program memory is very small in the PC - it is just for storage of the BIOS - the boot messages you see when the PC boots, and (often, but not always) the configiguration pages where you define if you have a floppy installed, if the computer should support a USB keyboard etc.
Some chips allows parts of the program memory to be modified in blocks (segments), but you can't store variables in the program memory. To be honest I am a bit surprised. I thought that program memory was off limits to the user for anything, constants or variables in general.
If you do not store and access constants in the program memory, then you need the startup code to copy the constants from the program memory into the RAM on program startup. But requiring two copies of the variable is obviously a waste of space.
The program memory is non-volatile and keeps its contents after power-off. But newer memory architectures, such as the flash memory in just about every new processor, can be erased and reprogrammed in the system (ISP = in-system-programming) and even by the running application (IAP = In Application Programming).
This allows field upgrades of the processor. But it may also be used to store configuration data in the program memory.
But since it is intended for program memory and for field upgrades of the application, you can normally not write individual bytes. And each write normally takes quite long time and may require a full block to be erased. Because of this, it will not work for normal variables. You can't just do a memory write like you can with the RAM memory.
A number of processors - such as the 8051 - separates program and data memory. This is called a Harward architecture. The processor has one set of instructions to access data memory and another set of instructions to access program memory. This allows the compiler to select suitable instructions when accessing a constant stored in the program memory. But there will not exist any instructions for writing into the program memory.
The solution to the above problem - how to write into the program memory for processors with separated program and data address spaces - is normally to allow special function registers indirectly access and write new data, or to create a double-mapped memory area where you can access a mapped memory block using either data operations or program operations.
Most newer processors unifies the address space for program and data, allowing the same processor instructions to access data or program memory. This is called a von Neumann architecture. It is just the address of the memory access that specifies if the access is within the range of a data memory or a program memory.
But in the end - yes, program memory is off limits. But only for "normal" write instructions. The compiler can produce code to pick up constants from the program memory. And some processors have invented methods to allow blocks of the program memory to be erased and rewritten with new information.
Right. Allowing users to write into the program memory blocks would invariably lead to problems, if the user is using a variable that could become corrupt. On the other hand unused program memory would continue to be unused
Unused trunk space or knee space is left unused in a car also.
You often have to design for worst-case requirements and end up with mostly unused extra capacity.
If you do use IAP methods to store information in the program space (for the chpis that supports this), you should make use of checksums to give the program the ability to decide if a valid configuration has been stored, or if it should run with default values.
Harward architecture
Correctly spelled as: Harvard architecture