Copy function to RAM buffer on HCS12?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy function to RAM buffer on HCS12?

Jump to solution
2,574 Views
RichTestardi
Senior Contributor II

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

Labels (1)
0 Kudos
1 Solution
424 Views
CompilerGuru
NXP Employee
NXP Employee

The code almost worked Smiley Happy

 

The only real issue I saw was that the function call to buf was done with a __far function pointer, and that does not match the __near calling convention of f0.

 

I also added a few casts to avoid the compiler warnings I saw.

 

Daniel

 

 

#include <stdio.h>#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){    for (;;) {            /* copy PIC functions */      (void)memmove(buf, (char*)(int)f0, (int)end-(int)f0);      /* start f0 */      ((void(*__near)(void))(int)buf) (); /* cast buf to fnct pointer and call it */    }}

 

 

View solution in original post

0 Kudos
2 Replies
425 Views
CompilerGuru
NXP Employee
NXP Employee

The code almost worked Smiley Happy

 

The only real issue I saw was that the function call to buf was done with a __far function pointer, and that does not match the __near calling convention of f0.

 

I also added a few casts to avoid the compiler warnings I saw.

 

Daniel

 

 

#include <stdio.h>#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){    for (;;) {            /* copy PIC functions */      (void)memmove(buf, (char*)(int)f0, (int)end-(int)f0);      /* start f0 */      ((void(*__near)(void))(int)buf) (); /* cast buf to fnct pointer and call it */    }}

 

 

0 Kudos
424 Views
RichTestardi
Senior Contributor II

Thank you, thank you, thank you!!!

 

And even the compiler warning!!!

 

-- Rich

 

0 Kudos