AnsweredAssumed Answered

Copy function to RAM buffer on HCS12?

Question asked by Rich Testardi on Mar 10, 2009
Latest reply on Mar 10, 2009 by Rich Testardi

Hi all,


I'm new to HCS12, using CW4.7 on a MC9S12DT256.


I want to copy a ROM function to a buffer in RAM and run it exactly as in "Listing 10.13 Taking the address of a function" of the HC(S)12 Build Tools Reference Manual, but I cannot get it to work.  Even the example just runs off into the weeds, stopping at a bgnd at some low address.


Part of the problem may be I am using a banked memory model.


Basically, what I want to do is copy a ROM function to a buffer in RAM (the buffer is used for other things at other times -- I was hoping  not to have to permanently leave this function in private RAM, though I may have to give up here) and then call it thru a function pointer.


I understand that far function pointers and data pointers are not exchangeable because the page indicator is stored in a different place, but I was hoping to get by with just near pointers here.


The strange thing is that this all "just worked" on the HCS08.


My simplified version of the sample code from listing 10.13 is below.  If anyone can tell me what is wrong or how pointers might behave in a non-obvious way, I would appreciate it very much!


#pragma CODE_SEG __PIC_SEG __NEAR_SEG PIC_CODE/* declarations of PIC functions */void f0(void);int g;/* implementation of PIC functions */void f0(void){    g=1234;}/* dummy function to calculate the end of */void end(void){}/* the PIC_CODE segment *//* implementation of main module. Copies and starts the PIC code */#pragma CODE_SEG DEFAULTchar buf[100]; /* RAM area into which to copy the PIC functions */void main(void){    /* copy PIC functions */    memmove(buf, (char*)f0,(char*)end-(char*)f0);    /* start f0 */    ((void(*)(void))buf) (); /* cast buf to fnct pointer and call it */}


Thank you for any help!!!


-- Rich