Section 4. Program Memory: Highlights
Section 4. Program Memory: Highlights
Program Memory
HIGHLIGHTS
This section of the manual contains the following topics:
Program Memory
Note: If the RETURN instruction is placed at the end of the program memory, the Illegal
Address Error Trap will be generated by the device during the run-time, which is due
to the prefetch operation, which will try to preload the next two instructions from the
memory location, which in this case, do not exist. The solution is to leave 2 extra
instruction words available after the RETURN instruction so that the compiler can
place NOP and RESET instructions at the end of the program memory.
Unimplemented
(Read 0s)
0x7FFFFE
0x800000
Reserved
Configuration Memory Space
0xF7FFFE
Device Configuration 0xF80000
Registers 0xF80017
0xF80018
Reserved
4
0xFEFFFE
Program Memory
0xFF0000
DEVID (2) 0xFFFFFE
Note: The address boundaries for user Flash program memory will depend on the dsPIC33F device
variant selected. For further details, refer to the appropriate device data sheet.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as 0
-n = Value at POR 1 = Bit is set 0 = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as 0
-n = Value at POR 1 = Bit is set 0 = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as 0
-n = Value at POR 1 = Bit is set 0 = Bit is cleared x = Bit is unknown
Program Memory
24-bits
0x000000
24
Instruction
23 User
+1(1) Space
Latch
23
Instruction
Program Counter 0
22 0
0x7FFFFE
Figure 4-3: High and Low Address Regions for Table Operations
PC Address 23 16 8 0
0x000100 00000000
0x000102 00000000
0x000104 00000000
0x000106 00000000 4
Program Memory
HIGH Table Address Range LOW Table Address Range
Program Memory
Phantom Byte
(Read as 0)
7 0 15 0
TBLPAG EA
24-bit EA
PC Address 23 16 8 0
0x000100 00000000
0x000102 00000000
0x000104 00000000
0x000106 00000000
TBLRDL.B (Wn<0> = 0)
TBLRDL.W
Program Memory
Phantom Byte
TBLRDL.B (Wn<0> = 1)
(Read as 0)
TBLRDH.W
PC Address 23 16 8 0
0x000100 00000000
0x000102 00000000
0x000104 00000000
0x000106 00000000
TBLRDH.B (Wn<0> = 0)
Program Memory
Phantom Byte
(Read as 0) TBLRDH.B (Wn<0> = 1)
Note: For more information on the unlocking sequence, refer to Section 5. Flash 4
Programming (DS70228).
Program Memory
_MemReadLatch:
mov W0,TBLPAG
tblrdl [W1],W0
tblrdh [W1],W1
return
;**********************************************************
; _MemCommand:
;
;W0 = NVMCON
;no return values
_WriteCommand:
mov W0,NVMCON
mov #0x55,W0;Unlock sequence
mov W0,NVMKEY
mov #0xAA,W0
mov W0,NVMKEY
bset NVMCON,#WR
nop ;Required
nop
Loop:btsc NVMCON,#WR;Wait for write end
bra Loop
return
Example 4-2 uses the space(prog) attribute to allocate the buffer in program memory. The
MPLAB C30 built-in functions, such as builtin_tblpage and builtin_tbloffset, can
be used to access the buffer.
Example 4-2: Using MPLAB C30 Built-in Functions to Access Program Memory
# include <p24hxxxx.h>
main (){
unsigned int lowWord, highWord;
unsigned int tbloffset;
TBLPAG = __builtin_tblpage(&prog_data[3]);
tbloffset = __builtin_tbloffset(&prog_data[3]);
lowWord=__builtin_tblrd1(tbloffset);
highWord=__builtin_tblrdh(tbloffset);
/* do something */
Program Memory
Program Space
0x000100
Data Space
0x0000
PSVPAG
0x01
0x8000 8 23 15 0
EA<15> = 1 0x008000
15 23
0xFFFF
0x017FFF
Upper 8 bits of Program
Memory Data cannot be
read using Program Space
Visibility. Data Read
4
23 bits
Program Memory
Select
1 Wn
PSVPAG Reg
8 bits 15 bits
23-bit EA
main()
{
// Compiler Managed PSV
sum=vectordot(m,x);
}
for(i=0;i<5;i++)
sum+=*m++ * *x++;
return(sum);
}
Example 4-4 illustrates buffer placement and access in the user managed PSV section. The psv
space is the user managed PSV section.
main()
{
int temp;
PSVPAG = __builtin_psvpage(&m1);
CORCONbits.PSV = 1;
sum1=vectordot(m1,x);
PSVPAG = __builtin_psvpage(&m2);
sum2=vectordot(m2,x);
for(i=0;i<5;i++)
sum+=*m++ * *x++;
4
return(sum);
Program Memory
}
Example 4-5 illustrates constant data placement in program memory and performs accessing of
this data through the PSV data window using an assembly program.
.global __reset
__reset:
; Enable Program Space Visibility
bset.b CORCONL, #PSV
A summary of the registers associated with Program Memory is provided in Table 4-1.
Table 4-1: Program Memory Registers
All
SFR Addr Bit 15 Bit 14 Bit 13 Bit 12 Bit 11 Bit 10 Bit 9 Bit 8 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Resets
TBLPAG 0032 Table Page Address Pointer Register 0000
PSVPAG 0034 Program Memory Visibility Page Address Pointer Register 0000
CORCON 0044 US EDT DL<2:0> SATA SATB SATDW ACCSAT IPL3 PSV RND IF 0000
Legend: = unimplemented, read as 0. Shaded bits are not used in the operation of Program Memory.
Program Memory
PIC24H Family Reference Manual
Note: For additional Application Notes and code examples for the PIC24H device family,
visit the Microchip web site (www.microchip.com).
Program Memory