Setting Origin using C using HC(S)08 on the MC9S08GB60A

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

Setting Origin using C using HC(S)08 on the MC9S08GB60A

1,670 Views
MrStackPointer
Contributor I

We have a program which downloads data via serial comms in to RAM. Which is then executed. For easy coding we are using C to code the block of code which is uploaded to the micro's RAM. This block of code changes depending on what is sent down the serial.

 

In asm instructions we would do the following

 

ASM: 

;[use serial to download set of opcodes into $0100]

;then once download is complete

ORG $0100

;[code stored in memory now runs.]

 

 

How would one do this in C? We are having trouble figuring out how to define an 'origin' in C so that this new code that is downloaded into RAM can be run. 

 

We are using a MC9S08GB60A.

 

Any help will be awesome! 

 

 

Labels (1)
0 Kudos
Reply
5 Replies

958 Views
MrStackPointer
Contributor I

Thanks for the help guys.

 

Although the address locations need to be modified so that all the REL jumps that the code does, take place in the RAM space you intend to place the code. If this is not done, then the C code will not execute correctly in RAM

 

 

0 Kudos
Reply

958 Views
bigmac
Specialist III

Hello,

 

As Lundin has already indicated, the code being downloaded to the target will need to have been previously compiled and linked so that a binary image of the assembly/machine code is what is sent.  Or alternatively, perhaps use S19 format for an ASCII format file, which is easily converted to a binary image, and provides rudimentary error detection.

 

The linking process will need to be controlled so that the execution location for the code is from the specific block of RAM memory, so that absolute jumps are to the correct address.  Relative jumps within the new code are not a problem.

 

Regards,

Mac

0 Kudos
Reply

958 Views
Lundin
Senior Contributor IV
For the code download itself, it must be OP-codes. There is no way around that.
To call the code at a specific address from C, you must do like this:


typedef void(* FuncPtr_t)(void);

...

const uint16_t CODE_ADDRESS = 0x0100;
const FuncPtr_t func_ptr = (const FuncPtr_t)CODE_ADDRESS;
func_ptr();


The behavior of that code is well-defined by the C standard. If you want to pass parameters to the code, it becomes a bit more tricky on the function-side. On the caller-side, simply change the function pointer to one with parameters.
0 Kudos
Reply

958 Views
ipa
Contributor III

C language supports asm instructions; try:

 asm {bsr 0x100;} /* branch to subroutine at 0x100 */

Take care of right syntax - the exmple above is for 9S12.

ipa

0 Kudos
Reply

958 Views
CrasyCat
Specialist III

Hello

 

Well according to my understanding you are looking for a way to execute code stored at a specific address in RAM. Am I right?

 

This can be done defining a constant function pointer and calling the function through the function pointer.

 

For example:

 define the constant function pointer as

 

   void (*const fktPtr)(void) = (void(*)(void))0x100;

 

  in your code add a JSR to the code located at address 0x100:

  

   fktPtr();

  

CrasyCat
0 Kudos
Reply