1 /*****************************************************
2 start12.c - standard startup code
3 The startup code may be optimized to special user requests
4 ----------------------------------------------------
5 Copyright (c) Metrowerks, Basel, Switzerland
9 Note: ROM libraries are not implemented in this startup code
10 Note: C++ destructors of global objects are NOT yet supported in the HIWARE Object File Format.
11 To use this feature, please build your application with the ELF object file format.
12 *****************************************************/
17 /* Macros to control how the startup code handles the COP: */
18 /* #define _DO_FEED_COP_ : do feed the COP */
19 /* #define _DO_ENABLE_COP_: do enable the COP */
20 /* #define _DO_DISABLE_COP_: disable the COP */
21 /* Without defining any of these, the startup code does NOT handle the COP */
23 #pragma DATA_SEG __NEAR_SEG STARTUP_DATA /* _startupData can be accessed using 16 bit accesses. This is needed because it contains the stack top, and without stack, far data cannot be accessed */
24 struct _tagStartup _startupData; /* read-only: */
25 /* _startupData is allocated in ROM and */
26 /* initialized by the linker */
27 #pragma DATA_SEG DEFAULT
29 #include "non_bank.sgm"
30 /* the init function must be in non banked memory if banked variables are used */
31 /* because _SET_PAGE is called, which may change any page register. */
36 void _SET_PAGE(void); /* the inline assembler needs a prototype */
37 /* this is a runtime routine with a special */
38 /* calling convention, dont use it in c code! */
39 static void Init(void);
40 static void Fini(void);
42 #include "default.sgm"
43 #if defined( __BANKED__) || defined(__LARGE__)
44 static void __far Init(void);
45 static void __far Fini(void);
46 #endif /* defined( __BANKED__) || defined(__LARGE__) */
50 /* define value and bits for Windef Register */
52 #define WINDEF (*(volatile unsigned char*) 0x37)
53 #if defined( __BANKED__) || defined(__LARGE__) || defined(__PPAGE__)
54 #define __ENABLE_PPAGE__ 0x40
56 #define __ENABLE_PPAGE__ 0x0
58 #if defined(__DPAGE__)
59 #define __ENABLE_DPAGE__ 0x80
61 #define __ENABLE_DPAGE__ 0x0
63 #if defined(__EPAGE__)
64 #define __ENABLE_EPAGE__ 0x20
66 #define __ENABLE_EPAGE__ 0x0
70 #ifdef _HCS12_SERIALMON
71 /* for Monitor based software remap the RAM & EEPROM to adhere
72 to EB386. Edit RAM and EEPROM sections in PRM file to match these. */
73 #define ___INITRM (*(volatile unsigned char *) 0x0010)
74 #define ___INITRG (*(volatile unsigned char *) 0x0011)
75 #define ___INITEE (*(volatile unsigned char *) 0x0012)
78 #if defined(_DO_FEED_COP_)
79 #define __FEED_COP_IN_HLI() } __asm movb #0x55, _COP_RST_ADR; __asm movb #0xAA, _COP_RST_ADR; __asm {
81 #define __FEED_COP_IN_HLI() /* do nothing */
84 #if !defined(FAR_DATA) && (defined( __BANKED__) || defined(__LARGE__))
85 static void __far Init(void)
87 static void Init(void)
90 /* purpose: 1) zero out RAM-areas where data is allocated */
91 /* 2) copy initialization data from ROM to RAM */
92 /* 3) call global constructors in C++ */
93 /* called from: _Startup, LibInits */
96 #if defined(__HIWARE_OBJECT_FILE_FORMAT__) && defined(__LARGE__)
97 LDX _startupData.pZeroOut:1 ; in the large memory model in the HIWARE format, pZeroOut is a 24 bit pointer
99 LDX _startupData.pZeroOut ; *pZeroOut
101 LDY _startupData.nofZeroOuts ; nofZeroOuts
102 BEQ CopyDown ; if nothing to zero out
104 NextZeroOut: PSHY ; save nofZeroOuts
106 LDAB 1,X+ ; load page of destination address
107 LDY 2,X+ ; load offset of destination address
108 __PIC_JSR(_SET_PAGE) ; sets the page in the correct page register
110 LDY 2,X+ ; start address and advance *pZeroOut (X = X+4)
111 #endif /* FAR_DATA */
112 LDD 2,X+ ; byte count
113 #ifdef __OPTIMIZE_FOR_SIZE__ /* -os, default */
114 NextWord: CLR 1,Y+ ; clear memory byte
115 __FEED_COP_IN_HLI() ; feed the COP if necessary /*lint !e505 !e522 asm code */
116 DBNE D, NextWord ; dec byte count
118 LSRD ; /2 and save bit 0 in the carry
121 LoopClrW: STX 2,Y+ ; Word-Clear
122 __FEED_COP_IN_HLI() ; feed the COP if necessary /*lint !e505 !e522 asm code */
125 BCC LastClr ; handle last byte
129 PULY ; restore nofZeroOuts
130 DEY ; dec nofZeroOuts
133 #ifdef __ELF_OBJECT_FILE_FORMAT__
134 LDX _startupData.toCopyDownBeg ; load address of copy down desc.
136 LDX _startupData.toCopyDownBeg:2 ; load address of copy down desc.
139 LDD 2,X+ ; size of init-data -> D
140 BEQ funcInits ; end of copy down desc.
143 LDAB 1,X+ ; load destination page
144 LDY 2,X+ ; destination address
145 __PIC_JSR(_SET_PAGE) ; sets the destinations page register
146 PULD ; restore counter
148 LDY 2,X+ ; load destination address
149 #endif /* FAR_DATA */
151 #ifdef __OPTIMIZE_FOR_SIZE__ /* -os, default */
152 Copy: MOVB 1,X+,1,Y+ ; move a byte from ROM to the data area
153 __FEED_COP_IN_HLI() ; feed the COP if necessary /*lint !e505 !e522 asm code */
154 DBNE D,Copy ; copy-byte loop
156 LSRD ; /2 and save bit 0 in the carry
157 Copy: MOVW 2,X+,2,Y+ ; move a word from ROM to the data area
158 __FEED_COP_IN_HLI() ; feed the COP if necessary /*lint !e505 !e522 asm code */
159 DBNE D,Copy ; copy-word loop
160 BCC NextBlock ; handle last byte?
161 MOVB 1,X+,1,Y+ ; copy the last byte
164 funcInits: ; call of global construtors is only in c++ necessary
165 #if defined(__cplusplus)
166 #if defined(__ELF_OBJECT_FILE_FORMAT__)
167 #if defined( __BANKED__) || defined(__LARGE__)
168 LDY _startupData.nofInitBodies; load number of cpp.
169 BEQ done ; if cppcount == 0, goto done
170 LDX _startupData.initBodies ; load address of first module to initialize
172 LEAX 3,X ; increment to next init
173 PSHX ; save address of next function to initialize
174 PSHY ; save cpp counter
175 CALL [-3,X] ; use double indirect call to load the page register also
176 PULY ; restore cpp counter
177 PULX ; restore actual address
178 DEY ; decrement cpp counter
180 #else /* defined( __BANKED__) || defined(__LARGE__) */
182 LDD _startupData.nofInitBodies; load number of cpp.
183 BEQ done ; if cppcount == 0, goto done
184 LDX _startupData.initBodies ; load address of first module to initialize
186 LDY 2,X+ ; load address of first module to initialize
188 PSHX ; save actual address
189 JSR 0,Y ; call initialization function
190 PULX ; restore actual address
191 PULD ; restore cpp counter
193 #endif /* defined( __BANKED__) || defined(__LARGE__) */
194 #else /* __ELF_OBJECT_FILE_FORMAT__ */
195 LDX _startupData.mInits ; load address of first module to initialize
196 #if defined( __BANKED__) || defined(__LARGE__)
197 nextInit: LDY 3,X+ ; load address of initialization function
198 BEQ done ; stop when address == 0
199 ; in common environments the offset of a function is never 0, so this test could be avoided
200 #ifdef __InitFunctionsMayHaveOffset0__
201 BRCLR -1,X, done, 0xff ; stop when address == 0
202 #endif /* __InitFunctionsMayHaveOffset0__ */
203 PSHX ; save address of next function to initialize
204 CALL [-3,X] ; use double indirect call to load the page register also
205 #else /* defined( __BANKED__) || defined(__LARGE__) */
207 LDY 2,X+ ; load address of first module to initialize
208 BEQ done ; stop when address of function == 0
209 PSHX ; save actual address
210 JSR 0,Y ; call initialization function
211 #endif /* defined( __BANKED__) || defined(__LARGE__) */
212 PULX ; restore actual address
214 #endif /* __ELF_OBJECT_FILE_FORMAT__ */
216 #endif /* __cplusplus */
220 #if defined( __ELF_OBJECT_FILE_FORMAT__) && defined(__cplusplus )
222 #if !defined(FAR_DATA) && (defined( __BANKED__) || defined(__LARGE__))
223 static void __far Fini(void)
225 static void Fini(void)
228 /* purpose: 1) call global destructors in C++ */
230 #if defined( __BANKED__) || defined(__LARGE__)
232 LDY _startupData.nofFiniBodies; load number of cpp.
233 BEQ done ; if cppcount == 0, goto done
234 LDX _startupData.finiBodies ; load address of first module to finalize
236 LEAX 3,X ; increment to next init
237 PSHX ; save address of next function to finalize
238 PSHY ; save cpp counter
239 CALL [-3,X] ; use double indirect call to load the page register also
240 PULY ; restore cpp counter
241 PULX ; restore actual address
242 DEY ; decrement cpp counter
244 #else /* defined( __BANKED__) || defined(__LARGE__) */
246 LDD _startupData.nofFiniBodies; load number of cpp.
247 BEQ done ; if cppcount == 0, goto done
248 LDX _startupData.finiBodies ; load address of first module to finalize
250 LDY 2,X+ ; load address of first module to finalize
252 PSHX ; save actual address
253 JSR 0,Y ; call finalize function
254 PULX ; restore actual address
255 PULD ; restore cpp counter
257 #endif /* defined( __BANKED__) || defined(__LARGE__) */
264 #include "non_bank.sgm"
266 #pragma MESSAGE DISABLE C12053 /* Stack-pointer change not in debugging-information */
275 /* The function _Startup must be called in order to initialize global variables and to call main */
276 /* You can adapt this function or call it from your startup code to implement a different startup */
279 /* You should also setup the needed IO registers as WINDEF (HC12A4 only) or the COP registers to run */
282 /* to set the reset vector several ways are possible : */
283 /* 1. define the function with "interrupt 0" as done below in the first case */
284 /* 2. add the following line to your prm file : VECTOR ADDRESS 0xfffe _Startup */
285 /* of course, even more posibilities exists */
286 /* the reset vector must be set so that the application has a defined entry point */
288 #define STARTUP_FLAGS_NOT_INIT_SP (1<<1)
290 #if defined(__SET_RESET_VECTOR__)
291 void __interrupt 0 _Startup(void) {
293 void _Startup(void) {
295 /* purpose: 1) initialize the stack
296 2) initialize the RAM, copy down init data etc (Init)
299 called from: _PRESTART-code generated by the Linker
300 or directly referenced by the reset vector */
301 for(;;) { /* forever: initialize the program; call the root-procedure */
302 if (!(_startupData.flags&STARTUP_FLAGS_NOT_INIT_SP)) {
303 /* initialize the stack pointer */
304 INIT_SP_FROM_STARTUP_DESC(); /*lint !e522 asm code */ /* HLI macro definition in hidef.h */
307 #ifdef _HCS12_SERIALMON
308 /* for Monitor based software remap the RAM & EEPROM to adhere
309 to EB386. Edit RAM and EEPROM sections in PRM file to match these. */
310 ___INITRG = 0x00; /* lock registers block to 0x0000 */
311 ___INITRM = 0x39; /* lock Ram to end at 0x3FFF */
312 ___INITEE = 0x09; /* lock EEPROM block to end at 0x0fff */
315 /* Here user defined code could be inserted, the stack could be used */
316 #if defined(_DO_DISABLE_COP_)
320 /* Example : Set up WinDef Register to allow Paging */
321 #ifdef HC812A4 /* HC12 A4 derivative needs WINDEF to configure which pages are available */
322 #if (__ENABLE_EPAGE__ != 0 || __ENABLE_DPAGE__ != 0 || __ENABLE_PPAGE__ != 0)
323 WINDEF= __ENABLE_EPAGE__ | __ENABLE_DPAGE__ | __ENABLE_PPAGE__;
326 Init(); /* zero out, copy down, call constructors */
327 /* Here user defined code could be inserted, all global variables are initilized */
328 #if defined(_DO_ENABLE_COP_)
333 (*_startupData.main)();
335 /* call destructors. Only done when this file is compiled as C++ and for the ELF object file format */
336 /* the HIWARE object file format does not support this */
337 #if defined( __ELF_OBJECT_FILE_FORMAT__) && defined(__cplusplus )
341 } /* end loop forever */