<?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>topic Switch statement &amp; Table of Funciont Pointers (long) in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140893#M2561</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;THIS IS THE LONG DETAILED VERSION&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello everybody, I'm here with a huge problem that's blowing my mind!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First of all, I'm working with HCS12 and I'm programming it in C language&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I had in the .PRM file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;DEFUALT_ROM PAGE_3C;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then, the problem began when I ran out of allocation space at that page, so I changed the .PRM to this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;DEFUALT_ROM PAGE_3C,PAGE_38;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, space is no longer a problem.... But know the problem is with the calling convention and moving from one page to another, using the switch statement or a Table of Function Pointers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll try to explain this clearly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Before I ran out of space, I've got this&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#pragma CODE_SEG PAGE_3C&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;switch(var1){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;case 1: ........&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;case 2: ........&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and it all works fine, but then, when I changed the .PRM file, it stops working... And this is what I found out looking at the .MAP&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When all works:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;MODULE: -- rtshc12.c.o (ansibf.lib) --&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- PROCEDURES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_DIRECT_BYTE 3CB5C1 5 5 1 NON_BANKED&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_CHECKED 3CB5C6 B 11 2 NON_BANKED&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_CHECKED_BYTE 3CB5D1 14&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;so those procedures were located at page 3C (I don't know why. I mean why is it at page 3C and no another one.. it's some kind of default I guess), and so was my code (the one with the switch). And everything works fine because when I debbug it, I use Assembly Steps and when it reaches the switch statement, there was JSR instruction to _CASE_CHECKED. Everything was at the same page so there was so problem with that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then I use #pragma to set the CODE_SEG at page 38.... so now my code was at that page, but _CASE_CHECKED remains at PAGE 3C (I checked the .MAP again)... But when the Assembly Steps reach the switch, there was the JSR instruction again... and that's a problem because they are at different pages.. so it stop working... HOW CAN I SOLVE THAT??&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then I learnt that there was the Function Pointers, therefore a Table of Function Pointers... so I dive into that but I've got the same problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#pragma CODE_SEG PAGE_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func1(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func2(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func3(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;const FuncPtrType switchFUNC[3]={func1,func2,func3};&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;switchFUNC[x]();&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... so I check the .MAP agian and I found this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- PROCEDURES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func1 388000 1A 26 2 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func2 38801A 192 402 28 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func3 3881AC 1A 26 6 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- VARIABLES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;switchFUNC 3CB017 F&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;so I don't know why they are at different pages and now, instead of JSR I found CALL[xxxx,Y] but again, there was no page reference&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 21 Dec 2006 22:58:29 GMT</pubDate>
    <dc:creator>SebaS</dc:creator>
    <dc:date>2006-12-21T22:58:29Z</dc:date>
    <item>
      <title>Switch statement &amp; Table of Funciont Pointers (long)</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140893#M2561</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;THIS IS THE LONG DETAILED VERSION&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello everybody, I'm here with a huge problem that's blowing my mind!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First of all, I'm working with HCS12 and I'm programming it in C language&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what I had in the .PRM file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;DEFUALT_ROM PAGE_3C;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then, the problem began when I ran out of allocation space at that page, so I changed the .PRM to this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;DEFUALT_ROM PAGE_3C,PAGE_38;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, space is no longer a problem.... But know the problem is with the calling convention and moving from one page to another, using the switch statement or a Table of Function Pointers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll try to explain this clearly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Before I ran out of space, I've got this&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#pragma CODE_SEG PAGE_3C&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;switch(var1){&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;case 1: ........&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;case 2: ........&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and it all works fine, but then, when I changed the .PRM file, it stops working... And this is what I found out looking at the .MAP&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When all works:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;MODULE: -- rtshc12.c.o (ansibf.lib) --&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- PROCEDURES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_DIRECT_BYTE 3CB5C1 5 5 1 NON_BANKED&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_CHECKED 3CB5C6 B 11 2 NON_BANKED&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;_CASE_CHECKED_BYTE 3CB5D1 14&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;so those procedures were located at page 3C (I don't know why. I mean why is it at page 3C and no another one.. it's some kind of default I guess), and so was my code (the one with the switch). And everything works fine because when I debbug it, I use Assembly Steps and when it reaches the switch statement, there was JSR instruction to _CASE_CHECKED. Everything was at the same page so there was so problem with that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then I use #pragma to set the CODE_SEG at page 38.... so now my code was at that page, but _CASE_CHECKED remains at PAGE 3C (I checked the .MAP again)... But when the Assembly Steps reach the switch, there was the JSR instruction again... and that's a problem because they are at different pages.. so it stop working... HOW CAN I SOLVE THAT??&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then I learnt that there was the Function Pointers, therefore a Table of Function Pointers... so I dive into that but I've got the same problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#pragma CODE_SEG PAGE_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func1(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func2(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void func3(void){......}&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;const FuncPtrType switchFUNC[3]={func1,func2,func3};&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;switchFUNC[x]();&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... so I check the .MAP agian and I found this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- PROCEDURES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func1 388000 1A 26 2 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func2 38801A 192 402 28 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;func3 3881AC 1A 26 6 PAGINA_38&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- VARIABLES:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;switchFUNC 3CB017 F&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;so I don't know why they are at different pages and now, instead of JSR I found CALL[xxxx,Y] but again, there was no page reference&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Dec 2006 22:58:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140893#M2561</guid>
      <dc:creator>SebaS</dc:creator>
      <dc:date>2006-12-21T22:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: Switch statement &amp; Table of Funciont Pointers (long)</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140894#M2562</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;You need to read Tech notes TN06, TN227, TN226, and TN236, which are Acrobat PDF files in the \Technical Notes\Build Tools directory. These tech notes explain some of the details on how to access code and data in different memory segments.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The&amp;nbsp; tech notes are part of the CodeWarrior installation. However, if you skipped installing the documentation, you need run the installer again and do that.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Dec 2006 23:52:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140894#M2562</guid>
      <dc:creator>J2MEJediMaster</dc:creator>
      <dc:date>2006-12-21T23:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Switch statement &amp; Table of Funciont Pointers (long)</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140895#M2563</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Most runtime routines contained in rtshc12.c are supposed to be allocated non banked,&lt;BR /&gt;that's why they are allocated in the section NON_BANKED&lt;BR /&gt;&lt;BR /&gt;_CASE_DIRECT_BYTE 3CB5C1 5 5 1 &lt;B&gt;NON_BANKED&lt;/B&gt;&lt;BR /&gt;&lt;BR /&gt;So to fix this, explicitely place NON_BANKED to non banked area, in 0x0000..0x7FFF or 0xC000..0xFFFF (in a area your chip has flash, of course).&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Dec 2006 05:48:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140895#M2563</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-12-22T05:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: Switch statement &amp; Table of Funciont Pointers (long)</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140896#M2564</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;
&amp;gt;so I don't know why they are at different pages and now,
&amp;gt;instead of JSR I found CALL[xxxx,Y] but again, there was no page reference
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;The two cases are different. For the switch, the compiler want to call the runtime routine which should be allocated non banked, and therefore the compiler uses JSR. Note that the table entries just contain addresses inside of the calling function, and therefore the table only contain 2 byte addresses (or even 1 byte offsets as optimization).&lt;BR /&gt;For the manually defined function pointer table, the compiler wants to call the functions the pointers refer too. Unqualified function pointers are far functions in the banked memory model, therefore the compiler uses the CALL instruction. The page number is encode as part of the 3 byte function pointers, its not part of the call instruction.&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:41:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140896#M2564</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2020-10-29T08:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: Switch statement &amp; Table of Funciont Pointers (long)</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140897#M2565</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks a lot!!! Now it's working!!! Both the switch ant the Table of Function Pointers&lt;BR /&gt;&lt;BR /&gt;I modified the .PRM file and I add this:&lt;BR /&gt;&lt;BR /&gt;NON_BANKED INTO PAGE_C000&lt;BR /&gt;&lt;BR /&gt;and now it's working...... I remember when I first create this project... this line were ther but I deleted because at that page I put the interrupts and I was afraid that would reduce the amount of code at that page.&lt;BR /&gt;&lt;BR /&gt;THANKS!!! especially because you answer really fast!! THANKS A LOT!!&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Dec 2006 22:04:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Switch-statement-Table-of-Funciont-Pointers-long/m-p/140897#M2565</guid>
      <dc:creator>SebaS</dc:creator>
      <dc:date>2006-12-22T22:04:36Z</dc:date>
    </item>
  </channel>
</rss>

