AnsweredAssumed Answered

Switch statement & Table of Funciont Pointers (long)

Question asked by Sebastian Irazabal on Dec 21, 2006
Latest reply on Dec 22, 2006 by Sebastian Irazabal
THIS IS THE LONG DETAILED VERSION

Hello everybody, I'm here with a huge problem that's blowing my mind!!!

First of all, I'm working with HCS12 and I'm programming it in C language

This is what I had in the .PRM file:

DEFUALT_ROM PAGE_3C;

Then, the problem began when I ran out of allocation space at that page, so I changed the .PRM to this:

DEFUALT_ROM PAGE_3C,PAGE_38;

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.

I'll try to explain this clearly:

Before I ran out of space, I've got this

#pragma CODE_SEG PAGE_3C
switch(var1){
case 1: ........
case 2: ........
}

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

When all works:

MODULE: -- rtshc12.c.o (ansibf.lib) --
- PROCEDURES:
_CASE_DIRECT_BYTE 3CB5C1 5 5 1 NON_BANKED
_CASE_CHECKED 3CB5C6 B 11 2 NON_BANKED
_CASE_CHECKED_BYTE 3CB5D1 14

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.

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??

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:

#pragma CODE_SEG PAGE_38
void func1(void){......}
void func2(void){......}
void func3(void){......}

const FuncPtrType switchFUNC[3]={func1,func2,func3};

switchFUNC[x]();

... so I check the .MAP agian and I found this:
- PROCEDURES:
func1 388000 1A 26 2 PAGINA_38
func2 38801A 192 402 28 PAGINA_38
func3 3881AC 1A 26 6 PAGINA_38
- VARIABLES:
switchFUNC 3CB017 F

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

Outcomes