Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/21/07 22:39
Read: times


 
#139613 - I do it in a slightly different way
Responding to: ???'s previous message
Instead of placing the functions at fixed addresses, I initially write the code in the "normal" way, so that the functions are "packed" without holes.

Then, when a function needs a patch, I sacrifice the original function's space, leaving the "bad" one there and place the new to the spare space (often, the old function or part of it still can be used).

I don't deal with the "granularity" of FLASH space - on most modern architectures this can be dealt with using buffering (modern internal flash blocks tend to be small enough, 128-512B).

To accomplish this, I use a jumptable on a fixed address, containing jumps to the real functions. I (the C-hater) of course use asm, so this is easy; for example:
Main:
  [...]
  call Function1
  [...]
  call Function2
  [...]

;the real ("Original") functions are here:
O_Function1:
  [... some code then ret ...]

O_Function2:
  [... some code then ret ...]

;JumpTable begins here
  ORG  SomeSuitableFixedAddress
Function1:  jmp   O_Function1
Function2:  jmp   O_Function2
  [etc...]

When updating, I don't touch the original code, just add AFTER all functions the new function, and update the jumptable:
   [... the same main as above ...]
O_Function1:
   [... the same content of O_Function1 ...]
O_Function2:
   [... the same content of O_Function2 ...]
   [... other O_functions ...]
;-- here ends the original code
N_Function1:
   [... some code, sometimes even calling O_Function1 ...]

;JumpTable begins here
  ORG  SomeSuitableFixedAddress
Function1:  jmp   N_Function1
Function2:  jmp   O_Function2
  [etc...]

Of course, the update consists of update of the jumptable and programming of the new function to the spare space.

Now, in C, there is usually no native support for this (unless you write it yourself in an opensource compiler such as SDCC), so some tweaking is needed. Below, an example in SDCC, already patched (the SDCC gurus might come up with something nicer perhaps):
/////////// the jumptable

void dlltab(void) __naked {
__asm
  .area DLLTAB (ABS)
  .org  0HFF80           ;set location to absolute address
__endasm;
}

void T_function1(void) __naked {
__asm
  ljmp   _N_function1    ;this is patched; was: ljmp  O_function1
__endasm;
}

void T_function2(void) __naked {
__asm
  ljmp   O_function2
__endasm;
}

////////////// the patch area

void N_dummyfunction(void) __naked {
__asm
  .area PATCH (ABS)
  .org  0H1000            // we know from the mapfile that this area is empty
__endasm;
}

bit N_function1(char a) {
//data at 0x0002 char a;  // r2 - this is nasty but works; 
                          //but the passing via 1 parameter 
                          //works too and is nicer
data at 0x000B char b;    // we know the absolute address from the mapfile
  if (a < b) return(1); else return(0);
}



///////////// the original code


void dummyfunction(void) __naked {
__asm
  .area CSEG    (CODE)
__endasm;
}


#include <8051.h>
#include <stdio.h>


char c;

bit function1(char a, char b) {
__asm
  ljmp  _T_function1
O_function1:
__endasm;
  if (a == b) return(1); else return(0);
}


char function2(char a) {
__asm
  ljmp  _T_function2
O_function2:
__endasm;
  return(a*2);
}

void main(void) {
  c = 5;
  printf("%d", function2(function1(c,3)));

}


JW

PS.: Russ, see, this is the 2000 words I felt definitively needs to be written down...

List of 20 messages in thread
TopicAuthorDate
is DLL like modules possible with 8051 code ?            01/01/70 00:00      
   Nothing new...            01/01/70 00:00      
      If you choose the right part            01/01/70 00:00      
         the code size is 128kb with banking ?            01/01/70 00:00      
            is that this 'new math' I keep hearing about?            01/01/70 00:00      
               How embarassing            01/01/70 00:00      
            relative cost            01/01/70 00:00      
               The real problem            01/01/70 00:00      
                  OP's position            01/01/70 00:00      
                     I'm guessing, too            01/01/70 00:00      
                        see my post            01/01/70 00:00      
               security issues            01/01/70 00:00      
                  why not landline? that is waaay better            01/01/70 00:00      
               it may be            01/01/70 00:00      
   hooks            01/01/70 00:00      
   with the right chip it can be done            01/01/70 00:00      
      Details            01/01/70 00:00      
         some, including me, agree . others ..            01/01/70 00:00      
      I do it in a slightly different way            01/01/70 00:00      
         The "bios" version            01/01/70 00:00      

Back to Subject List