Daniel,
Yes, your warning is important. Array or wider than 1 byte variable allocated in single G-segment may get allocated in two R-pages. RPAGE accesses may fail in this case. Also, if using PAGED_RAM variables from ISR, one should save/restore GPAGE or RPAGE or even both paging registers.
But your words about more efficient RPAGE accesses dragged my attention. Yes, I agree, Gxxx instructions cost extra byte, extra CPU cycle, also not all instructions (like BSET) are available for global addressing. But 64k big G-page in contrast to 4k R-page should allow to reduce page register switching, isn’t it? Single G-page covers all available internal S12X RAM. In case paged flash and external bus accesses are not necessary, it is physically possible to setup GPAGE register once and not touch it any more. Global accesses could be more effective in this case, right? But I wondered how CW compiler deals with paging registers and found something bad.
I tried to put 3 variables into PAGED_RAM and initialized them from main(). PRM file is project wizard default and not touched. I disassembled C file and noticed that RPAGE register is set up once. That’s weird. How does compiler recognize that paged variables are linked to the same R-segment? Maybe compiler/linker force all paged data from same *.c file to single R-segment? I tried to move data definitions to another *.c module and declaring data in main.c between DATA_SEG pragmas. It didn’t change anything. I also tried to allocate more than 4k in DATA_SEG. Disassemble didn’t changed, also I didn't get single warning.
#pragma DATA_SEG __RPAGE_SEG PAGED_RAM
char a[4096-4]; // code works only if all three a, b and c fit single 4k R-segment.
short b,c;
#pragma DATA_SEG DEFAULT
void main(void) {
/* put your own code here */
a[100] = 123;
b = 456;
c = 789;
Disassemble:
28: a[100] = 123;
0000 c600 [1] LDAB #PAGE(a)
0002 5b16 [2] STAB /*RPAGE*/22
0004 c67b [1] LDAB #123
0006 7b0000 [3] STAB a:100
29: b = 456;
0009 cc01c8 [2] LDD #456
000c 7c0000 [3] STD b
30: c = 789;
000f ce0315 [2] LDX #789
0012 7e0000 [3] STX c
Of course this fails if I define a as char a[4096];. a and b are placed in different R-segments, but RPAGE is not switched when accessing a and b.
Am I missing something? I guess I should make service request for this.