<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>S12 / MagniV Microcontrollers中的主题 Copy function to RAM buffer on HCS12?</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160403#M5018</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm new to HCS12, using CW4.7 on a MC9S12DT256.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; Even the example just runs off into the weeds, stopping at a bgnd at some low address.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Part of the problem may be I am using a banked memory model.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The strange thing is that this all "just worked" on the HCS08.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My simplified version of the sample code from listing 10.13 is below.&amp;nbsp; If anyone can tell me what is wrong or how pointers might behave in a non-obvious way, I would appreciate it very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
#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){&amp;nbsp;&amp;nbsp;&amp;nbsp; 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){&amp;nbsp;&amp;nbsp;&amp;nbsp; /* copy PIC functions */&amp;nbsp;&amp;nbsp;&amp;nbsp; memmove(buf, (char*)f0,(char*)end-(char*)f0);&amp;nbsp;&amp;nbsp;&amp;nbsp; /* start f0 */&amp;nbsp;&amp;nbsp;&amp;nbsp; ((void(*)(void))buf) (); /* cast buf to fnct pointer and call it */}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any help!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- Rich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 09:05:42 GMT</pubDate>
    <dc:creator>RichTestardi</dc:creator>
    <dc:date>2020-10-29T09:05:42Z</dc:date>
    <item>
      <title>Copy function to RAM buffer on HCS12?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160403#M5018</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm new to HCS12, using CW4.7 on a MC9S12DT256.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; Even the example just runs off into the weeds, stopping at a bgnd at some low address.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Part of the problem may be I am using a banked memory model.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The strange thing is that this all "just worked" on the HCS08.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My simplified version of the sample code from listing 10.13 is below.&amp;nbsp; If anyone can tell me what is wrong or how pointers might behave in a non-obvious way, I would appreciate it very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
#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){&amp;nbsp;&amp;nbsp;&amp;nbsp; 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){&amp;nbsp;&amp;nbsp;&amp;nbsp; /* copy PIC functions */&amp;nbsp;&amp;nbsp;&amp;nbsp; memmove(buf, (char*)f0,(char*)end-(char*)f0);&amp;nbsp;&amp;nbsp;&amp;nbsp; /* start f0 */&amp;nbsp;&amp;nbsp;&amp;nbsp; ((void(*)(void))buf) (); /* cast buf to fnct pointer and call it */}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any help!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- Rich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:05:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160403#M5018</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2020-10-29T09:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Copy function to RAM buffer on HCS12?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160404#M5019</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The code almost worked &lt;IMG alt="Smiley Happy" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="https://community.nxp.com/i/smilies/16x16_smiley-happy.png" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also added a few casts to avoid the compiler warnings I saw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
#include &amp;lt;stdio.h&amp;gt;#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 */    }}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:05:44 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160404#M5019</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2020-10-29T09:05:44Z</dc:date>
    </item>
    <item>
      <title>Re: Copy function to RAM buffer on HCS12?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160405#M5020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, thank you, thank you!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And even the compiler warning!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-- Rich&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Mar 2009 20:50:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/Copy-function-to-RAM-buffer-on-HCS12/m-p/160405#M5020</guid>
      <dc:creator>RichTestardi</dc:creator>
      <dc:date>2009-03-10T20:50:38Z</dc:date>
    </item>
  </channel>
</rss>

