How should I initialize globals and static locals?

eTPU_C

It's possible to use a Host Service Request to perform initialization, but we don't feel it's the best way.

We recommend using the host interface macros to create an initialization routine to run on the host.

Here is one way:

Host Initialization

This example shows a basic initialization process.

0200                                    #pragma write c, (

                                        int pram_location;
                                        int pram_top;
                                        #define init_pram(where, towhere) (pram_location = where, pram_top = towhere)
                                        #define alloc_pram(howmuch) (pram_location += howmuch)
                                        #define exceeds_pram(howmuch) ((pram_location + howmuch ) > pram_top ? 1 : 0)

                                        /* ETPUglobalimage expansion omitted due to size: using
                                           const char etpu_image[]; */

                                        void initialize_eTPU(void)
                                        {
                                          /* initialize SCM */
                                          /* alternative, use ETPUcodeimagesize */
                                          for(int i; i < 4 ; i++)
                                            outb(ETPU_SCM_BASE+i, etpu_image[i]);

                                          /* initialize eTPU globals */
                                        #define __etpu_globals(offset, valtype, value) outb(ETPU_PRAM_BASE+offset, (valtype)value);

                                          ::ETPUglobals ;

                                          /* initialize channel 1 */
                                          /* ::ETPUfunctionnumber(handler) */
                                          CxCFS(1, 1);

                                          init_pram();

                                          if(exceeds_pram(::ETPUfunctionframeram(handler))) error(OF_SOME_KIND);
                                          CxCBPA(1, alloc_pram(::ETPUfunctionframeram(handler)));

                                        #define __etpu_staticinit(offset, value) outb(CBPA_BASE(1)+offset, value);

                                          ::ETPUstaticinit(1);


                                        }
                                        );


The generated host interface file (here edited for length) looks like this:

 int24 pram_location;
 int24 pram_top;
 #define init_pram(where, towhere) (pram_location = where, pram_top = towhere)
 #define alloc_pram(howmuch) (pram_location += howmuch)
 #define exceeds_pram(howmuch) ((pram_location + howmuch ) > pram_top ? 1 : 0)
 const char etpu_image[] = { 0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0x40,0x81,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0xC0,0x85,0xC0,0x85,0xC0,0x85,0xC0,0x85,
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                             0xFF,0xDF,0xCC,0xF9,0x1C,0xBF,0xAF,0xBE,
                             0xCF,0xFF,0xF1,0xFF,0xFF,0xC0,0x10,0x1F,
                             0x6F,0xFF,0xFF,0xFF,0x4F,0xFF,0xFF,0xFF,
                             0x6F,0xFF,0xFF,0xFF,0xFF,0xDF,0xCC,0xF9,
                             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
                              };
 void initialize_eTPU(void)
 {
   for(int24 i; i < 4 ; i++)
     outb(ETPU_SCM_BASE+i, etpu_image[i]);
 #define __etpu_globals(offset, valtype, value) outb(ETPU_PRAM_BASE+offset, (valtype)value);
   __etpu_globals(CC                               ,cc_reg    ,0x0000)
__etpu_globals(global1                          ,sint24    ,0x0001)
__etpu_globals(global2                          ,sint24    ,0x0005)
 ;
   CxCFS(1, 1);
   init_pram();
   if(exceeds_pram(0x0000 )) error(OF_SOME_KIND);
   CxCBPA(1, alloc_pram(0x0000 ));
 #define __etpu_staticinit(offset, value) outb(CBPA_BASE(1)+offset, value);
   ;
 }