some cleanup
[pyfrprog.git] / pkernel / start91460.asm
1 /*      THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU       */
2 /*      MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR       */
3 /*      ELIGIBILITY FOR ANY PURPOSES.                                                   */
4 /*      (C) Fujitsu Microelectronics Europe GmbH                                        */
5 ;=========================================================================================
6 ; 1  Contents
7 ;=========================================================================================
8 ; 1       Contents
9 ; 2       Disclaimer
10 ;
11 ; 3       History
12 ;
13 ; 4       Settings
14 ; 4.1     Controller device
15 ; 4.2     Boot / flash security 
16 ; 4.3     Stack type and stack size
17 ; 4.4     Copy code from flash to I-RAM
18 ; 4.5     C++ start-up 
19 ; 4.6     Low-level library interface
20 ; 4.7     Clock Configuration
21 ; 4.7.1   Clock selection
22 ; 4.7.2   Select Clock Modulator
23 ; 4.8     External bus interface
24 ; 4.8.1   Select chipselect 
25 ; 4.8.2   Set memory addressing for chipselects
26 ; 4.8.3   Configure chipselect area
27 ; 4.8.4   Set wait cycles for chipselects
28 ; 4.8.5   Configure chipselects SDRAM memory only 
29 ; 4.8.6   Referesh control register RCR 
30 ; 4.8.7   Terminal and timing control register
31 ; 4.8.8   Enable / disable I-cache
32 ; 4.8.9   Enable CACHE for chipselect
33 ; 4.8.10  Select external bus mode (data lines)
34 ; 4.8.11  Select external bus mode (address lines)
35 ; 4.8.12  Select external bus mode (control signals)
36 ;
37 ; 5       Definitions of Configurations
38 ;
39 ; 6       Section and data declaration
40 ; 6.1     Define stack size
41 ; 6.2     Define sections
42 ;
43 ; 7.      S T A R T 
44 ; 7.1     Initialise stack pointer and table base register
45 ; 7.2     Check for CSV reset and set CSV
46 ; 7.3     Check clock condition
47 ; 7.4     Restore default settings after reset
48 ; 7.4.1   Disable clock modulator
49 ; 7.4.2   Check if running on sub clock, change to main clock
50 ; 7.4.3   Disable sub clock
51 ; 7.4.4   Check if running on PLL, gear down PLL
52 ; 7.4.5   Disable PLL
53 ; 7.4.6   Set to main clock
54 ; 7.5     Set memory controller
55 ; 7.6     Clock startup
56 ; 7.6.1   Set Voltage Regulator Settings
57 ; 7.6.2   Power on clock modulator - clock modulator part I
58 ; 7.6.3   Set CLKR register w/o clock mode
59 ; 7.6.4   Start PLLs 
60 ; 7.6.5   Wait for PLL oscillation stabilisation
61 ; 7.6.6   Set clocks 
62 ; 7.6.6.1 Set CPU and peripheral clock
63 ; 7.6.6.2 Set external bus interface clock
64 ; 7.6.6.3 Set CAN clock prescaler
65 ; 7.6.6.4 Switch main clock mode
66 ; 7.6.6.5 Switch sub clock mode
67 ; 7.6.6.6 Switch to PLL mode
68 ; 7.6.7   Enable frequncy modulation - clock modulator part II
69 ; 7.7     Set BusInterface
70 ; 7.7.1   Disable all CS
71 ; 7.7.2   Clear TCR register
72 ; 7.7.3   Set CS0 
73 ; 7.7.4   Set CS1 
74 ; 7.7.5   Set CS2  
75 ; 7.7.6   Set CS3
76 ; 7.7.7   Set CS4
77 ; 7.7.8   Set CS5 
78 ; 7.7.9   Set CS6
79 ; 7.7.10  Set CS7  
80 ; 7.7.11  Set special SDRAM config register  
81 ; 7.7.12  set Port function register
82 ; 7.7.13  Set TCR register
83 ; 7.7.14  Enable cache for selected CS
84 ; 7.7.15  Set SDRAM referesh control register
85 ; 7.7.16  Enable used CS
86 ; 7.7.17  I-cache on/off
87 ; 7.7.18  Set port function register to general as I/O-port
88 ; 7.8     Copy code from flash to I-RAM
89 ; 7.9     Fill stacks
90 ; 7.10    Clear data 
91 ; 7.11    Copy Init section from ROM to RAM
92 ; 7.12    C library initialization
93 ; 7.13    Call C++ constructors
94 ; 7.14    Call main routine
95 ; 7.15    Return from main function
96 ;
97 ;=========================================================================================
98 ; 2  Disclaimer
99 ;=========================================================================================
100 ;                    Fujitsu Microelectronics Europe GmbH                       
101 ;                http://emea.fujitsu.com/microelectronics 
102 ;                                                              
103 ;    The  following  software  is for  demonstration  purposes only. It  is not fully  
104 ;    tested, nor  validated  in order to fullfill its task under  all  circumstances.  
105 ;    Therefore,  this software or  any part of it must only  be used in an evaluation 
106 ;    laboratory environment.                        
107 ;    This  software  is  subject to  the  rules of  our  standard DISCLAIMER, that is
108 ;    delivered with our  SW-tools on  the  Fujitsu  Microcontrollers  CD/DVD (V3.4 or 
109 ;    higher "\START.HTM") or on our Internet Pages:                                   
110 ;    http://www.fme.gsdc.de/gsdc.htm
111 ;    http://emea.fujitsu.com/microelectronics 
112 ;
113 ;=========================================================================================
114 ; 3  History
115 ;=========================================================================================
116 ;
117 ;=========================================================================================
118 ;       MB914xx (FR60 CORE ONLY) Series C Compiler's 
119 ;
120 ;       Startup file for memory and basic controller initialisation
121 ;=========================================================================================
122 ;History:
123 ;
124 ; 2005-04-18 V1.0 UMa  Release first version
125 ; 2005-06-17 V1.1 UMa  Added bus interface, modified c++ startup
126 ; 2005-06-28 V1.2 UMa  minor changes
127 ; 2005-07-27 V1.3 UMa  default values changed
128 ; 2005-10-04 V1.4 UMa  changed code 'Call main Routine'
129 ;                      Added secutiy section for MB91F467D  
130 ;                      Added Flash Access Read Timing setting section;
131 ; 2005-10-04 V1.5 UMa  Added Flash Controller Section
132 ; 2005-10-28 V1.6 UMa  Check for CSV reset
133 ; 2005-11.16 V1.7 UMa  Monitor Debugger support added: Copy of intvect Table
134 ;                      Ext. Int 0 as abort function
135 ;                      Changed PLL-Startup, Reset HWWD added
136 ; 2005-11-16 V1.7 UMa  Examples for MUL_G changed
137 ; 2006-02-14 V1.8 UMa  mb91464a added
138 ;                      Settings for Clock Spervisor added
139 ;                      Name of Section SECURITY changed to SECURITY_VECTORS
140 ;                      Example values for gear-up changed
141 ; 2006-03-17 V1.9 UMa  Changed Startup for Monitor Debugger
142 ; 2006-04-24 v2.0 UMa  Added MB91465K and MB91469G
143 ; 2006-05-03 v2.1 UMa  Added MB91461R; removed MB91V460A
144 ;                      Added settings for the external bus-interface
145 ; 2006-07-28 v2.2 UMa  Added I-RAM copy function (ROM -> IRAM)
146 ;                      Added default settings for FLASH Access Read Timing 
147 ;                      Settings 
148 ;                      Changed default settings for FLASH cache configuration 
149 ;                      Register
150 ;                      Changed check for clock startup
151 ; 2006-08-16 v2.3 MVo  Corrected Boot Security Sector Addresses for MB91469G
152 ; 2006-10-06 v2.4 UMa  Added new devices
153 ;                      Corrected typo in I_RAM to flash copy function
154 ;                      Changed default settings for flash cache configuration
155 ;                      Changed comments for SDRAM bus interface configuration
156 ;                      Changed comments and default setting of CAN Prescaler
157 ;                      Added Stack filler
158 ;                      Added Settings for REGSEL Register
159 ; 2007-02-13 v2.5 UMa  Introduction of default configurations
160 ;                      Changed I_RAM to flash copy function                    
161 ;
162 ;
163 ;=========================================================================================
164 ; 4  Settings
165 ;=========================================================================================
166 ;
167 ; CHECK ALL OPTIONS WHETHER THEY FIT TO THE APPLICATION;
168 ;
169 ; Configure this startup file in the "Settings" section. Search for
170 ; comments with leading "; <<<". This points to the items to be set.
171 ;=========================================================================================
172 ;
173 #set    OFF             0
174 #set    ON              1
175 #set    DEFAULT         2
176 #set    LOW_PRIOR       31
177 ;
178 ;=========================================================================================
179 ; 4.1  Controller Device
180 ;=========================================================================================
181 #set    MB91464A        2                       ; MB91460 series
182 ;
183 #set    MB91467B       10                       ; MB91460 series
184 ;
185 #set    MB91467C       11                       ; MB91460 series
186 ;
187 #set    MB91467D        4                       ; MB91460 series
188 ;
189 #set    MB91469G        6                       ; MB91460 series
190 ;
191 #set    MB91465K        3                       ; MB91460 series
192 ;
193 #set    MB91463N        8                       ; MB91460 series
194 ;
195 #set    MB91461R        1                       ; MB91460 series
196 #set    MB91467R        5                       ; MB91460 series
197 ;
198 #set    MB91465X        9                       ; MB91460 series
199 ;
200 #set    others          7                       ; MB91460 series
201 ;
202 ;
203 ;
204 #set    DEVICE          MB91465K                ; <<< select device
205 ;
206 ;=========================================================================================
207 ; 4.2  Boot / Flash Security 
208 ;=========================================================================================
209 ;
210 #set    BOOT_FLASH_SEC  OFF                     ; <<< BOOT and Flash Security Vector    
211 ;
212 ; The flash devices have two flash and two boot security vectors.  It is important to set
213 ; the four vectors correctly.  Otherwise it might be possible,  that the flash device is 
214 ; not accessible any more via the bootrom. Please read carefully the hardware manual.
215
216 ; OFF:  The security feature is switch off. The section SECURITY_VECTORS is reserved and
217 ;       the vectors are set.
218 ; ON:   IMPORTANT! The  security vectors are  not set. But the  section SECURITY_VECTORS 
219 ;       is reserved.  
220 ;
221 ; Note: This feature is not supported by every device. Please check the data sheet. This 
222 ;       feature is not available on MB91461R.
223 ;
224 ;=========================================================================================
225 ; 4.3  Stack Type and Stack Size
226 ;=========================================================================================
227 ;
228 #set    USRSTACK        0                       ; user stack:   for main program
229 #set    SYSSTACK        1                       ; system stack: for main program and 
230 ;                                               ;               interrupts
231 ;
232 ;
233 #set    STACKUSE        SYSSTACK                ; <<< set active stack
234 ;
235 #set    STACK_RESERVE   ON                      ; <<< reserve stack area in 
236 ;                                               ;     this module
237 #set    STACK_SYS_SIZE  0x400-4                 ; <<< byte size of System stack
238 #set    STACK_USR_SIZE  0x2                     ; <<< byte size of User stack 
239 ;
240 ; - If the active stack is set to SYSSTACK,  it is used for main program and interrupts. 
241 ;   In this case,  the user stack  could be set to a dummy size.  If the active stack is 
242 ;   set  to  user  stack,  it is  used  for the  main  program  but the  system stack is 
243 ;   automatically activated,  if an interrupt is serviced.  Both stack areas must have a 
244 ;   reasonable size.
245 ; - If STACK_RESERVE is ON,  the sections USTACK and SSTACK are reserved in this module. 
246 ;   Otherwise, they have to be reserved in other modules.  If STACK_RESERVE is OFF,  the 
247 ;   size definitions STACK_SYS_SIZE and STACK_USR_SIZE have no meaning.
248 ; - Even if  they  are reverved  in other modules,  they are  still initialised  in this 
249 ;   start-up file.
250 ;
251 ; Note: Several library functions require quite a big stack (due to ANSI). 
252 ;       Check the stack information files (*.stk) in the LIB\911 directory.
253 ;
254 ;=========================================================================================
255 ; 4.4  Copy code from Flash to I-RAM
256 ;=========================================================================================
257 ;
258 #set    I_RAM           ON                      ; <<< select  if  code  in  section IRAM
259 ;                                                     should be copied
260 ;
261 ; If this option is activated code located in the  section IRAM is copied during startup 
262 ; from ROM to the instruction-RAM. The code is linked for the instruction-RAM.
263 ;
264 ;=========================================================================================
265 ; 4.7  Clock Configuration
266 ;=========================================================================================
267 ;=========================================================================================
268 ; 4.7.1  Clock Selection
269 ;=========================================================================================
270 ;
271 ; No clock settings
272 #set    NO_CLOCK                                               0x01
273 ;
274 ; Sub-oscillation input: 32 kHz 
275 #set    SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ     0x11
276 ;
277 ; Oscillation input: 4 MHz 
278 #set    MAIN_4MHZ_CPU___2MHZ_PER__1MHZ_EXT__1MHZ_CAN__2MHZ     0x21
279 #set    PLL_4MHZ__CPU__48MHZ_PER_16MHZ_EXT_24MHZ_CAN_16MHZ     0x22
280 #set    PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ     0x23
281 #set    PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_27MHZ_CAN_20MHZ     0x24
282 #set    PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_40MHZ_CAN_20MHZ     0x25
283 #set    PLL_4MHZ__CPU__96MHZ_PER_16MHZ_EXT_48MHZ_CAN_16MHZ     0x26  ;not MB91V460, ...
284 #set    PLL_4MHZ__CPU_100MHZ_PER_20MHZ_EXT_50MHZ_CAN_20MHZ     0x27  ;not MB91V460, ...
285 ;
286 ; MB91461R only: Oscillation input: 10 MHz
287 #set    PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x41
288 ;
289 ; MB91461R only: Oscillation input: 20 MHz
290 #set    PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x51
291 ;
292 ; User settings
293 #set    CLOCK_USER                                             0x61
294 ;
295 ;
296 ;
297 #set    CLOCKSPEED      PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ
298 ;                                               ; <<< Select clock configuration 
299 ;
300 ; There are different default configurations available, where all necessary settings for 
301 ; clocks and the related  registers are made.  Beside this configurations,  there is the
302 ; possibility   to  define  a  user   configuration   in  the  chapter   "Definition  of 
303 ; Configurations"
304
305 ; - NO_CLOCK means: 
306 ;   The clock registers are not set by the start-up file.
307 ;
308 ; - PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ means:
309 ;   Main oszillation        =  4 MHz, PLL is activated
310 ;   CPU clock (CLKB)        = 64 MHZ
311 ;   Peripheral clock (CLKP) = 16 MHZ
312 ;   Ext. bus clock (CLKT)   = 32 MHZ
313 ;   CAN clock (CLKCAN)      = 16 MHz, using PLLx 
314 ;
315 ; - CLOCK_USER: 
316 ;   The user configuration definded in the chapter "Definition of Configurations" is set.
317 ;
318 ; Note: Not all  frequencies  are supported  by every device.  Please see  the  hardware 
319 ;       manual.
320 ;
321 ;=========================================================================================
322 ; 4.7.2  Select Clock Modulator  
323 ;=========================================================================================
324 ;
325 #set    CLOMO           OFF                     ; <<< Enable /disable clock modulator      
326 ;
327 #set    CMPR            0x026F                  ; <<< Ref. to the data sheet, CMPR
328
329 ; Please  refer  to the data sheet  of the device  if you  enable clock modulation.  The 
330 ; register CMPR dependant on the PLL-Clock.
331 ;
332 ; Note: If the CLKCAN source is set either to main oscillator or to PLL  output then the
333 ;       clock  for  the CAN  is not influenced by  the clock  modulation.  If the CLKCAN 
334 ;       source is set CPU clock (CLKB) then the clock for the CAN is also modulated  (if 
335 ;       the clock modulator is enabled).
336 ;
337 ; Note: If the clock modulator is enabled,  the wait states  of the  internal flash wait 
338 ;       states  must  be  adapted  to  maximum frequency.  Please check the  wait states 
339 ;       settings.
340 ;
341 ; Note: This feature  is not supported by every device,  e.g. MB91461.  Please check the 
342 ;       data sheet.
343 ;
344 ;=========================================================================================
345 ; 4.8  External Bus Interface
346 ;
347 ;      The rest of the configuration is only applicable for devices with an external bus 
348 ;      interface.
349 ;
350 ;      If the device does not offer an external bus interface,  the configuration can be 
351 ;      stoped at this point.
352 ;
353 ;=========================================================================================
354 ; 5  Definition of Configurations
355 ;=========================================================================================
356 ;
357 #set    NOCLOCK         0                       ; do not touch CKSCR register
358 #set    MAINCLOCK       1                       ; select main clock 
359 ;                                               ; MB91461R : 1/4 of oscillation input
360 ;                                               ; Others:    1/2 of oscillation input
361 #set    MAINPLLCLOCK    2                       ; select main clock with PLL
362 #set    SUBCLOCK        3                       ; select subclock (if available)
363 ;
364 #set    PSCLOCK_CLKB    0x00                    ; select core clock (initial)
365 #set    PSCLOCK_PLL     0x10                    ; select PLL output (x)
366 #set    PSCLOCK_MAIN    0x30                    ; select Main Oscillation
367 ;
368 ;=========================================================================================
369 ; 5.1  CLOCKSPEED == CLOCK_USER <<<
370 ;=========================================================================================
371 ; Must be configured only in the case of CLOCKSPEED is set to CLOCK_USER. Please see the 
372 ; corresponding application note.
373 ;
374 #if (CLOCKSPEED == CLOCK_USER )
375   #set  CLOCKSOURCE     MAINPLLCLOCK            ; <<< Clocksource
376   #set  ENABLE_SUBCLOCK OFF                     ; <<< Subclock: ON/OFF
377   #set  PLLSPEED        0x010F                  ; <<< 0x48Ch, 0x48Dh: PLLDIVM/N ; 64 MHz
378   #set  DIV_G           0x0F                    ; <<< 0x48Eh: PLLDIVG; 
379   #set  MUL_G           0x0F                    ; <<< 0x48Fh: PLLMULG;     
380   ; Clock Divider
381   #set  CPUCLOCK        0x00                    ; <<< 0x486h: DIV0R_B;   => /1  ; 64 MHz       
382   #set  PERCLOCK        0x03                    ; <<< 0x486h: DIV0R_P;   => /4  ; 16 MHz 
383   #set  EXTBUSCLOCK     0x01                    ; <<< 0x487h: DIV1R_T;   => /2  ; 32 MHz 
384   ; CAN Clock
385   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; <<< 0x4C0h: CANPRE;    => PLLx;128 MHz
386   #set  PSDVC           0x07                    ; <<< 0x4C0h: CANPRE_DVC;=> /8  ; 16 MHz
387   #set  CANCLOCK        0x00                    ; <<< 0x4C1h: CANCKD;    
388   ; Voltage Regulator 
389   #set  REGULATORSEL    0x06                    ; <<< 0x4CEh: REGSEL;
390   #set  REGULATORCTRL   0x00                    ; <<< 0x4CFh: REGCTR;
391   ; Memory Controller
392   #set  FLASHCONTROL    0x032                   ; <<< 0x7002h: FCHCR;
393   #set  FLASHREADT      0xC413                  ; <<< 0x7004h: FMWT;
394   #set  FLASHMWT2       0x10                    ; <<< 0x7006h: FMWT2;
395 #endif  
396 ;
397 ;=========================================================================================
398 ; 5.2  CLOCKSPEED == NO_CLOCK
399 ;=========================================================================================
400 ;
401 #if (CLOCKSPEED == NO_CLOCK )
402     #set CLOCKSOURCE       NOCLOCK 
403 #endif      
404 ;
405 ;=========================================================================================
406 ; 5.2  CLOCKSPEED == SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ 
407 ;=========================================================================================
408 ;
409 #if (CLOCKSPEED == SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ )
410 ;
411 ; Start restriction; Maximum frequency
412   #if (DEVICE == MB91463N) || (DEVICE == MB91461R) 
413      #error: Frequency is not supported by this device.
414   #endif 
415 ; End restriction
416 ;
417   #set  CLOCKSOURCE     SUBCLOCK                ; Clocksource
418   #set  ENABLE_SUBCLOCK ON                      ; Subclock: ON/OFF
419   #set  PLLSPEED        0x010F                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;   n. a.
420   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
421   #set  MUL_G           0x0F                    ; 0x48Fh: PLLMULG;     
422   ; Clock Divider
423   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  32 KHz       
424   #set  PERCLOCK        0x00                    ; 0x486h: DIV0R_P;    => /1    ;  32 KHz  
425   #set  EXTBUSCLOCK     0x00                    ; 0x487h: DIV1R_T;    => /1    ;  32 KHz  
426   ; CAN Clock
427   #set  PSCLOCKSOURCE   PSCLOCK_MAIN            ; 0x4C0h: CANPRE;     => MAIN  ;   4 MHz
428   #set  PSDVC           0x01                    ; 0x4C0h: CANPRE_DVC; => /2    ;   2 MHz
429   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
430   ; Voltage Regulator 
431   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
432   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
433   ; Memory Controller
434   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
435   #set  FLASHREADT      0xC100                  ; 0x7004h: FMWT; 
436   #set  FLASHMWT2       0x00                    ; 0x7006h: FMWT2;
437 #endif 
438 ;
439 ;=========================================================================================
440 ; 5.3  CLOCKSPEED == MAIN__4MHZ_CPU___2MHZ_PER__1MHZ_EXT__1MHZ_CAN__2MHZ 
441 ;=========================================================================================
442 ;
443 #if (CLOCKSPEED == MAIN_4MHZ_CPU___2MHZ_PER__1MHZ_EXT__1MHZ_CAN__2MHZ )
444 ;
445 ; Start restriction; Maximum frequency
446   #if (DEVICE == MB91461R) 
447      #error: Frequency is not supported by this device.
448   #endif 
449 ; End restriction
450 ;
451   #set  CLOCKSOURCE     MAINCLOCK               ; Clocksource
452   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
453   #set  PLLSPEED        0x010F                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;   n. a.
454   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
455   #set  MUL_G           0x0F                    ; 0x48Fh: PLLMULG;     
456   ; Clock Divider
457   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;   2 MHz       
458   #set  PERCLOCK        0x01                    ; 0x486h: DIV0R_P;    => /2    ;   1 MHz 
459   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;   1 MHz 
460   ; CAN Clock
461   #set  PSCLOCKSOURCE   PSCLOCK_MAIN            ; 0x4C0h: CANPRE;     => PLLx  ;   4 MHz
462   #set  PSDVC           0x01                    ; 0x4C0h: CANPRE_DVC; => /2    ;   2 MHz
463   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
464   ; Voltage Regulator 
465   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
466   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
467   ; Memory Controller
468   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
469   #set  FLASHREADT      0xC100                  ; 0x7004h: FMWT;
470   #set  FLASHMWT2       0x00                    ; 0x7006h: FMWT2;  
471 #endif           
472 ;
473 ;=========================================================================================
474 ; 5.4  CLOCKSPEED == PLL_4MHZ__CPU__48MHZ_PER_16MHZ_EXT_24MHZ_CAN_16MHZ 
475 ;=========================================================================================
476 ;
477 #if (CLOCKSPEED == PLL_4MHZ__CPU__48MHZ_PER_16MHZ_EXT_24MHZ_CAN_16MHZ )
478 ;
479 ; Start restriction; Maximum frequency
480   #if (DEVICE == MB91461R) 
481      #error: Frequency is not supported by this device.
482   #endif 
483 ; End restriction
484 ;
485   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
486   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
487   #set  PLLSPEED        0x010B                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  48 MHz
488   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
489   #set  MUL_G           0x0B                    ; 0x48Fh: PLLMULG;     
490   ; Clock Divider
491   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  48 MHz       
492   #set  PERCLOCK        0x02                    ; 0x486h: DIV0R_P;    => /3    ;  16 MHz 
493   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  24 MHz 
494   ; CAN Clock
495   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ;  96 MHz
496   #set  PSDVC           0x05                    ; 0x4C0h: CANPRE_DVC; => /6    ;  16 MHz
497   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
498   ; Voltage Regulator 
499   #if (DEVICE == MB91469G) 
500    #set REGULATORSEL    0x36                    ; 0x4CEh: REGSEL;
501   #else
502    #set REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
503   #endif    
504   #set REGULATORCTRL    0x00                    ; 0x4CFh: REGCTR;    
505    ; Memory Controller
506   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
507   #set  FLASHREADT      0xC201                  ; 0x7004h: FMWT;
508   #set  FLASHMWT2       0x00                    ; 0x7006h: FMWT2;   
509 #endif        
510 ;
511 ;=========================================================================================
512 ; 5.5  CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ 
513 ;=========================================================================================
514 ;
515 #if (CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ )
516 ;
517 ; Start restriction; Maximum frequency
518   #if (DEVICE == MB91461R) 
519      #error: Frequency is not supported by this device.
520   #endif 
521 ; End restriction
522 ;
523   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
524   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
525   #set  PLLSPEED        0x010F                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  64 MHz
526   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
527   #set  MUL_G           0x0F                    ; 0x48Fh: PLLMULG;     
528   ; Clock Divider
529   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  64 MHz       
530   #set  PERCLOCK        0x03                    ; 0x486h: DIV0R_P;    => /4    ;  16 MHz 
531   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  32 MHz 
532   ; CAN Clock
533   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 128 MHz
534   #set  PSDVC           0x07                    ; 0x4C0h: CANPRE_DVC; => /8    ;  16 MHz
535   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
536   ; Voltage Regulator 
537   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
538   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
539   ; Memory Controller
540   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
541   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
542   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
543 #endif  
544 ;
545 ;=========================================================================================
546 ; 5.6  CLOCKSPEED == PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_27MHZ_CAN_20MHZ 
547 ;=========================================================================================
548 ;
549 #if (CLOCKSPEED == PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_27MHZ_CAN_20MHZ )
550 ;
551 ; Start restriction; Maximum frequency
552   #if (DEVICE == MB91461R) 
553      #error: Frequency is not supported by this device.
554   #endif 
555 ; End restriction
556 ;
557   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
558   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
559   #set  PLLSPEED        0x0113                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  80 MHz
560   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
561   #set  MUL_G           0x13                    ; 0x48Fh: PLLMULG;     
562   ; Clock Divider
563   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  80 MHz       
564   #set  PERCLOCK        0x03                    ; 0x486h: DIV0R_P;    => /4    ;  20 MHz 
565   #set  EXTBUSCLOCK     0x02                    ; 0x487h: DIV1R_T;    => /3    ;  27 MHz 
566   ; CAN Clock
567   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 160 MHz
568   #set  PSDVC           0x07                    ; 0x4C0h: CANPRE_DVC; => /8    ;   8 MHz
569   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
570   ; Voltage Regulator 
571   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
572   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
573   ; Memory Controller
574   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
575   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
576   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
577 #endif      
578 ;
579 ;=========================================================================================
580 ; 5.7  CLOCKSPEED == PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_40MHZ_CAN_20MHZ 
581 ;=========================================================================================
582 ;
583 #if (CLOCKSPEED == PLL_4MHZ__CPU__80MHZ_PER_20MHZ_EXT_40MHZ_CAN_20MHZ )
584 ;
585 ; Start restriction; Maximum frequency
586   #if (DEVICE == MB91461R) 
587      #error: Frequency is not supported by this device.
588   #endif 
589 ; End restriction
590 ;
591   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
592   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
593   #set  PLLSPEED        0x0113                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  80 MHz
594   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
595   #set  MUL_G           0x13                    ; 0x48Fh: PLLMULG;     
596   ; Clock Divider
597   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  80 MHz       
598   #set  PERCLOCK        0x03                    ; 0x486h: DIV0R_P;    => /4    ;  20 MHz 
599   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  40 MHz 
600   ; CAN Clock
601   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 160 MHz
602   #set  PSDVC           0x07                    ; 0x4C0h: CANPRE_DVC; => /8    ;   8 MHz
603   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
604   ; Voltage Regulator 
605   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
606   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
607   ; Memory Controller
608   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
609   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
610   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
611 #endif      
612 ;
613 ;=========================================================================================
614 ; 5.8  CLOCKSPEED == PLL_4MHZ__CPU__96MHZ_PER_16MHZ_EXT_48MHZ_CAN_16MHZ 
615 ;=========================================================================================
616 ;
617 #if (CLOCKSPEED == PLL_4MHZ__CPU__96MHZ_PER_16MHZ_EXT_48MHZ_CAN_16MHZ )
618 ;
619 ; Start restriction; Maximum frequency
620   #if (DEVICE == MB91464A) || (DEVICE == MB91465K) || (DEVICE == MB91463N) ||\
621       (DEVICE == MB91461R) || (DEVICE == MB91467R)
622      #error: Frequency is not supported by this device.
623   #endif 
624 ; End restriction
625 ;
626   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
627   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
628   #set  PLLSPEED        0x0117                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  96 MHz
629   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
630   #set  MUL_G           0x17                    ; 0x48Fh: PLLMULG;     
631   ; Clock Divider
632   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  64 MHz       
633   #set  PERCLOCK        0x05                    ; 0x486h: DIV0R_P;    => /6    ;  16 MHz 
634   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  32 MHz 
635   ; CAN Clock
636   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 192 MHz
637   #set  PSDVC           0x0B                    ; 0x4C0h: CANPRE_DVC; => /12   ;  16 MHz
638   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
639   ; Voltage Regulator 
640   #if (DEVICE == MB91469G) 
641    #set REGULATORSEL    0x36                    ; 0x4CEh: REGSEL;
642   #else
643    #set REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
644   #endif    
645   #set REGULATORCTRL    0x00                    ; 0x4CFh: REGCTR;    
646   ; Memory Controller
647   #set FLASHCONTROL     0x032                   ; 0x7002h: FCHCR;
648   #set FLASHREADT       0xC413                  ; 0x7004h: FMWT;
649   #set FLASHMWT2        0x10                    ; 0x7006h: FMWT2;
650 #endif        
651 ;
652 ;=========================================================================================
653 ; 5.9  CLOCKSPEED == PLL_4MHZ__CPU_100MHZ_PER_20MHZ_EXT_50MHZ_CAN_20MHZ 
654 ;=========================================================================================
655 ;
656 #if (CLOCKSPEED == PLL_4MHZ__CPU_100MHZ_PER_20MHZ_EXT_50MHZ_CAN_20MHZ )
657 ;
658 ; Start restriction; Maximum frequency
659   #if (DEVICE == MB91464A) || (DEVICE == MB91465K) || (DEVICE == MB91463N) ||\
660       (DEVICE == MB91461R) || (DEVICE == MB91467R) || (DEVICE == MB91467D)
661      #error: Frequency is not supported by this device.
662   #endif 
663 ; End restriction
664 ;
665   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
666   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
667   #set  PLLSPEED        0x0118                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ; 100 MHz
668   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
669   #set  MUL_G           0x17                    ; 0x48Fh: PLLMULG;     
670   ; Clock Divider
671   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ; 100 MHz       
672   #set  PERCLOCK        0x04                    ; 0x486h: DIV0R_P;    => /5    ;  20 MHz 
673   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  50 MHz 
674   ; CAN Clock
675   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 200 MHz
676   #set  PSDVC           0x09                    ; 0x4C0h: CANPRE_DVC; => /10   ;  20 MHz
677   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
678   ; Voltage Regulator 
679   #if (DEVICE == MB91469G) 
680    #set REGULATORSEL    0x36                    ; 0x4CEh: REGSEL;
681   #else
682    #set REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
683   #endif    
684   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;    
685   ; Memory Controller
686   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
687   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
688   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
689 #endif        
690 ;
691 ;=========================================================================================
692 ; 5.10  CLOCKSPEED == PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ 
693 ;=========================================================================================
694 ;
695 #if (CLOCKSPEED == PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ )
696 ;
697 ; Start restriction; Maximum frequency
698   #if (DEVICE == MB91464A) || (DEVICE == MB91467B) || (DEVICE == MB91467C) ||\
699       (DEVICE == MB91467D) || (DEVICE == MB91469G) || (DEVICE == MB91465K) ||\
700       (DEVICE == MB91463N) || (DEVICE == MB91467R) || (DEVICE == MB91465X) 
701      #error: Frequency is not supported by this device.
702   #endif 
703 ; End restriction
704 ;
705   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
706   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
707   #set  PLLSPEED        0x0105                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  60 MHz
708   #set  DIV_G           0x0B                    ; 0x48Eh: PLLDIVG; 
709   #set  MUL_G           0x1F                    ; 0x48Fh: PLLMULG;     
710   ; Clock Divider
711   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  60 MHz       
712   #set  PERCLOCK        0x02                    ; 0x486h: DIV0R_P;    => /3    ;  20 MHz 
713   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  30 MHz 
714   ; CAN Clock
715   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 120 MHz
716   #set  PSDVC           0x05                    ; 0x4C0h: CANPRE_DVC; => /6    ;  20 MHz
717   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
718   ; Voltage Regulator 
719   ; -
720   ; Memory Controller
721   ; -
722 #endif        
723 ;
724 ;=========================================================================================
725 ; 5.11  CLOCKSPEED == PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ 
726 ;=========================================================================================
727 ;
728 #if (CLOCKSPEED == PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ )
729 ;
730 ; Start restriction; Maximum frequency
731   #if (DEVICE == MB91464A) || (DEVICE == MB91467B) || (DEVICE == MB91467C) ||\
732       (DEVICE == MB91467D) || (DEVICE == MB91469G) || (DEVICE == MB91465K) ||\
733       (DEVICE == MB91463N) || (DEVICE == MB91467R) || (DEVICE == MB91465X) 
734      #error: Frequency is not supported by this device.
735   #endif 
736 ; End restriction
737 ;
738   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
739   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
740   #set  PLLSPEED        0x0102                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ; 60 MHz
741   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
742   #set  MUL_G           0x1F                    ; 0x48Fh: PLLMULG;     
743   ; Clock Divider
744   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  60 MHz       
745   #set  PERCLOCK        0x02                    ; 0x486h: DIV0R_P;    => /3    ;  20 MHz 
746   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  30 MHz 
747   ; CAN Clock
748   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 120 MHz
749   #set  PSDVC           0x05                    ; 0x4C0h: CANPRE_DVC; => /6    ;  20 MHz
750   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
751   ; Voltage Regulator 
752   ; -
753   ; Memory Controller
754   ; -
755 #endif  
756 ;      
757 ;=========================================================================================
758 ; 6  Section and Data Declaration
759 ;=========================================================================================
760
761         .export __start             
762         .import _main
763         .import _RAM_INIT
764         .import _ROM_INIT
765 ;=========================================================================================
766 ; 6.1  Define Stack Size
767 ;=========================================================================================
768  .SECTION  SSTACK, STACK, ALIGN=4
769 #if STACK_RESERVE == ON
770         .EXPORT         __systemstack, __systemstack_top
771  __systemstack:
772         .RES.B          STACK_SYS_SIZE
773  __systemstack_top: 
774 #endif
775  
776         .SECTION  USTACK, STACK, ALIGN=4
777 #if STACK_RESERVE == ON
778          .EXPORT        __userstack, __userstack_top
779  __userstack:
780         .RES.B          STACK_USR_SIZE
781  __userstack_top:
782  
783 #endif
784 ;=========================================================================================
785 ; 6.2  Define Sections
786 ;=========================================================================================
787         .section        DATA,  data,  align=4
788         .section        INIT,  data,  align=4
789         .section        IRAM,  code,  align=4
790         .section        CONST, const, align=4
791         .section        INTVECT, const, align=4 
792         
793 #if I_RAM 
794         .import _RAM_IRAM
795         .import _ROM_IRAM
796 #endif
797                     
798 #if (DEVICE != MB91461R)
799     #if (DEVICE == MB91469G)
800         .section        SECURITY_VECTORS, code, locate = 0x248000
801     #else 
802         .section        SECURITY_VECTORS, code, locate = 0x148000
803     #endif
804     
805     #if (BOOT_FLASH_SEC == OFF)        
806         .data.w 0xFFFFFFFF
807         .data.w 0xFFFFFFFF
808         .data.w 0xFFFFFFFF
809         .data.w 0xFFFFFFFF       
810     #else
811         .res.w          4
812     #endif         
813 #endif     
814    
815 ;-----------------------------------------------------------------------------------------
816 ; MACRO Clear RC Watchdog
817 ;-----------------------------------------------------------------------------------------
818 #macro  ClearRCwatchdog
819         LDI             #0x4C7,R7               ; clear RC watchdog
820         BANDL           #0x7,@R7
821 #endm
822 ;-----------------------------------------------------------------------------------------
823 ; MACRO WAIT_LOOP
824 ;-----------------------------------------------------------------------------------------
825 #macro wait_loop loop_number
826 #local _wait64_loop
827         LDI             #loop_number, R0
828 _wait64_loop:
829         ADD             #-1, R0
830         BNE             _wait64_loop
831 #endm
832         .section        CODE, code, align=4
833         .section        CODE_START, code, align=4
834 #pragma section CODE=IRAM,attr=CODE
835
836
837 ;=========================================================================================
838 ; 7.  S T A R T 
839 ;=========================================================================================
840 __start:                                        ; start point   
841 startnop: 
842         NOP       
843 ;   
844         ANDCCR          #0xEF                   ; disable interrupts   
845         STILM           #LOW_PRIOR              ; set interrupt level to low prior
846         ClearRCwatchdog                         ; clear harware watchdog
847
848 ;=========================================================================================
849 ; 7.1  Initialise Stack Pointer and Table Base Register
850 ;=========================================================================================
851 #if STACKUSE == SYSSTACK       
852         ORCCR           #0x20
853         LDI             #__userstack_top, SP    ; initialize SP
854         ANDCCR          #0xDF
855         LDI             #__systemstack_top, SP  ; initialize SP
856 #endif
857
858 #if STACKUSE == USRSTACK
859         ANDCCR          #0xDF
860         LDI             #__systemstack_top, SP  ; initialize SP
861         ORCCR           #0x20
862         LDI             #__userstack_top, SP    ; initialize SP
863 #endif
864
865         LDI             #INTVECT, R0            ; set Table Base
866 smd_tbr: 
867         MOV             R0, TBR         
868
869 #if (CLOCKSOURCE != NOCLOCK)                                          
870 ;=========================================================================================
871 ; 7.2  Check for CSV reset and set CSV
872 ;=========================================================================================
873 ; Start restriction; No clock supervisor (CSV)
874 #if (DEVICE != MB91461R) && (DEVICE != MB91467R) && (DEVICE != MB91463N)
875 ; End restriction
876         LDI:20          #0x04AD, R0             ; CSVCR
877         BORL            #0x8, @R0               ; Enable Main Osc CSV
878         BTSTH           #0x4, @R0               ; Check for Main Osc missing
879         BEQ             NoMAINCSVreset          ; Main osc available -> branch 
880                                                 ;   to NoCSVreset
881         BANDL           #0x7, @R0               ; Disable Main Osc CSV
882         
883         LDI             #noClockStartup, R0     ; Main Clock missing -> no
884         JMP             @R0                     ; clock startup
885                                                 
886 NoMAINCSVreset: 
887
888
889         BORL            #0x4, @R0               ; Enable Sub Osc CSV
890         BTSTH           #0x2, @R0               ; Check for Sub Osc missing
891         BEQ             NoSUBCSVreset           ; Sub osc available -> branch 
892                                                 ;   to NoCSVreset
893         BANDL           #0xB, @R0               ; Disable Sub Osc SCSV
894 #if (CLOCKSOURCE == SUBCLOCK)
895         LDI             #noClockStartup, R0     ; Sub Clock missing -> no
896         JMP             @R0                     ; clock startup
897 #endif                                                
898 NoSUBCSVreset:       
899 #endif        
900 ;=========================================================================================
901 ; 7.3  Check Clock Condition
902 ;=========================================================================================
903         LDI             #0x484, R0              ; Check for Default Values
904         LDI             #0x0F, R1               
905         ANDB            R1, @R0
906         BEQ             clock_startup 
907
908 ;=========================================================================================
909 ; 7.4  Restore Default Settings after Reset
910 ;=========================================================================================
911 ;=========================================================================================
912 ; 7.4.1  Disable Clock Modulator
913 ;=========================================================================================
914         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
915         BANDL           #0xD, @R0               ; Disable Frequency modulation
916 FMODwait:        
917         BTSTL           #8, @R0                 ; Wait until Frequency modulation
918         BNE             FMODwait                ; is disabled
919         
920         BANDL           #0xE, @R0               ; Power down clock modulator
921
922 ;=========================================================================================
923 ; 7.4.2  Check if running on Sub Clock, change to Main Clock
924 ;=========================================================================================
925         LDI:20          #0x0484,R12             ; Check if running on sub clock
926         LDUB            @R12,R0
927         LDI:8           #0x3,R1
928         AND             R1,R0
929         CMP             #0x3,R0
930         BNE             notOnSubClock
931         
932         LDI:20          #0x04CC,R12             ; Check if Main Clock is stopped
933         BTSTL           #1, @R12
934         BEQ             mainNotStopped
935
936         BANDL           #0xE, @R12              ; Start Main Oscillation
937                         
938         LDI             #0x4C8, R0              ; Main Stabilisation Wait Time
939         LDI             #0x04, R1               ; 32.7 ms
940         AND             R1, @R0  
941         BORH            #0x02, @R0      
942         
943         mainStabTime:                           ; Wait for stabilisation time
944         ClearRCwatchdog                         ; clear harware watchdog
945         BTSTH           #8, @R0
946         BEQ             mainStabTime
947         LDI             #0x0, R1
948         STB             R1, @R0
949
950 mainNotStopped:        
951         LDI:20          #0x0484, R12            ; disable sub clock as source
952         BANDL           #0xD, @R12              ; Clock source = 0x01 (Main/2)  
953                                                        
954 notOnSubClock:
955 ;=========================================================================================
956 ; 7.4.3  Disable Sub Clock
957 ;=========================================================================================
958 #if ENABLE_SUBCLOCK != ON
959         LDI             #0x0484, R0             ; Clock source control reg CLKR
960         BANDL           #0x7, @R0               ; Disable PLL
961 #endif       
962
963 ;=========================================================================================
964 ; 7.4.4  Check if running on PLL, Gear Down PLL
965 ;=========================================================================================
966         LDI:20          #0x0484,R12             ; Check if running on PLL
967         LDUB            @R12,R0
968         LDI:8           #0x3,R1
969         AND             R1,R0
970         CMP             #0x2,R0
971         BNE             notOnPll
972                     
973         LDI:20          #0x0490, R11            ; clear flags  
974         LDI:8           #0x0,R1        
975         STB             R1, @R11
976         LDI             #0x04,R1
977         STB             R1, @R11                ; Set Flag for Simulator; no Effekt on
978                                                 ; Emulator      
979
980         BANDL           #0xC, @R12              ; disable PLL as clock source  
981                                                 ; Clock Source = 0x00 (Main/2)
982                                                     
983         LDI:20          #0x048E,R12             ; check if DivG != 0
984         LDUB            @R12, R0
985         LDI:8           #0xFF,R1
986         AND             R1,R0
987         BEQ             notOnPll
988                                                                                           
989 gearDownLoop:    
990         ClearRCwatchdog                         ; clear harware watchdog
991         BTSTL           #4, @R11                ; Gear Down
992         BEQ             gearDownLoop            ; 
993  
994         LDI             #0x00,R1                ; Clear Flags
995         STB             R1, @R11                ;       
996         
997 notOnPll:
998 ;=========================================================================================
999 ; 7.4.5  Disable PLL
1000 ;=========================================================================================
1001         LDI             #0x0484, R0             ; Clock source control reg CLKR
1002         BANDL           #0xB, @R0               ; Disable PLL
1003         
1004 ;=========================================================================================
1005 ; 7.4.6  Set to Main Clock
1006 ;=========================================================================================
1007         LDI:20          #0x0484,R12             ; Check if running on PLL
1008         BANDL           #0xC, @R12              ; disable PLL as clock source  
1009                                                 ; Clock Source = 0x00 (Main/2)
1010
1011 clock_startup:
1012 ;=========================================================================================
1013 ; 7.5  Set Memory Controller
1014 ;=========================================================================================
1015 ; Start restriction; No embedded flash
1016 #if DEVICE != MB91461R
1017 ; End restriction
1018         LDI             #0x7002, R1             ; FLASH Controller Reg.
1019         LDI             #FLASHCONTROL, R2       ; Flash Controller Settings
1020         STH             R2, @R1                 ; set register
1021         LDI             #0x7004, R1             ; FLASH Memory Wait Timing Reg.
1022         LDI             #FLASHREADT, R2         ; wait settings
1023         STH             R2, @R1                 ; set register
1024         LDI             #0x7006, R1             ; FLASH Memory Wait Timing Reg.
1025         LDI             #FLASHMWT2, R2          ; wait settings
1026         STB             R2, @R1                 ; set register               
1027 #endif                
1028         ClearRCwatchdog   
1029                                                        
1030 ;=========================================================================================
1031 ; 7.6  Clock startup
1032 ;=========================================================================================
1033 ;=========================================================================================
1034 ; 7.6.1  Set Voltage Regulator Settings
1035 ;=========================================================================================
1036 ; Start restriction; No regulator settings
1037 #if DEVICE != MB91461R
1038 ; End restriction
1039         LDI             #0x04CF, R0             ; REGCTR
1040         LDI             #REGULATORCTRL, R1
1041         STB             R1, @R0
1042
1043         LDI             #0x04CE, R0             ; REGSEL
1044         LDI             #REGULATORSEL, R1
1045         STB             R1, @R0
1046 #endif
1047
1048 ;=========================================================================================
1049 ; 7.6.2  Power on Clock Modulator - Clock Modulator Part I
1050 ;=========================================================================================
1051 #if CLOMO == ON 
1052         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
1053         LDI             #0x11, R1               ; Load value to Power on CM
1054         ORB             R1, @R0                 ; Power on clock modulaor
1055 #endif
1056
1057 ;=========================================================================================
1058 ; 7.6.3  Set CLKR Register w/o Clock Mode
1059 ;=========================================================================================
1060 ; Set Clock source (Base Clock) for the three clock tree selections
1061 ; This select Base clock is used to select afterwards the 3
1062 ; Clocks for the diffenrent internal trees.
1063 ; When PLL is used, first pll multiplication ratio is set and PLL is
1064 ; enabled. After waiting the PLL stabilisation time via timebase
1065 ; timer, PLL clock is selected as clock source. 
1066         LDI             #0x048C, R0             ; PLL Cntl Reg. PLLDIVM/N
1067         LDI:20          #PLLSPEED, R1
1068         STH             R1, @R0
1069
1070         LDI             #0x048E, R0             ; PLL Cntl Reg. PLLDIVG
1071         LDI             #DIV_G, R1
1072         STB             R1, @R0
1073
1074         LDI             #0x048F, R0             ; PLL Cntl Reg. PLLMULG
1075         LDI             #MUL_G, R1
1076         STB             R1, @R0
1077
1078 ;=========================================================================================
1079 ; 7.6.4  Start PLL 
1080 ;=========================================================================================
1081 #if ( ( CLOCKSOURCE == MAINPLLCLOCK ) || ( PSCLOCKSOURCE == PSCLOCK_PLL ) )
1082         LDI             #0x0484, R0             ; Clock source control reg CLKR
1083         LDI             #0x04, R1               ; Use PLL x1, enable PLL 
1084         ORB             R1, @R0                 ; store data to CLKR register
1085 #endif
1086        
1087        
1088 #if ENABLE_SUBCLOCK == ON
1089         LDI             #0x0484, R0             ; Clock source control reg CLKR
1090         LDI             #0x08, R1               ; enable subclock operation
1091         ORB             R1, @R0                 ; store data to CLKR register
1092         LDI             #0x4CA, R0              ; Sub Clock oszilation 
1093         LDI             #0x00, R1               ; stabilitsation time = 32 ms
1094         AND             R1, @R0  
1095         BORH            #0x02, @R0      
1096 #endif      
1097       
1098 ;=========================================================================================
1099 ; 7.6.5  Wait for PLL oscillation stabilisation
1100 ;=========================================================================================
1101 #if ((CLOCKSOURCE==MAINPLLCLOCK)||(PSCLOCKSOURCE==PSCLOCK_PLL))
1102         LDI             #0x0482, R12            ; TimeBaseTimer TBCR
1103         LDI             #0x00, R1               ; set 1024 us @ 2 MHz 
1104         STB             R1, @R12
1105
1106         BANDH           #7, @R12                ; clear interrupt flag
1107         
1108         LDI             #0x0483, R0             ; clearTimeBaseTimer CTBR
1109         LDI             #0xA5, R1                 
1110         STB             R1, @R0
1111         LDI             #0x5A, R1                 
1112         STB             R1, @R0
1113         
1114         BANDH           #7, @R12                ; clear interrupt flag
1115         BORH            #8, @R12                ; set interrupt flag for simulator
1116
1117 PLLwait:        
1118         ClearRCwatchdog                         ; clear harware watchdog
1119         BTSTH           #8, @R12
1120         BEQ             PLLwait
1121 #endif
1122
1123 ;=========================================================================================
1124 ; 7.6.6  Set clocks 
1125 ;=========================================================================================
1126 ;=========================================================================================
1127 ; 7.6.6.1  Set CPU and peripheral clock 
1128 ;=========================================================================================
1129 ; CPU and peripheral clock are set in one register
1130         LDI             #0x0486, R2             ; Set DIVR0 (CPU-clock (CLKB)  
1131         LDI             #((CPUCLOCK << 4) + PERCLOCK), R3 ; Load CPU clock setting
1132         STB             R3, @R2               
1133 ;=========================================================================================
1134 ; 7.6.6.2  Set External Bus interface clock
1135 ;=========================================================================================
1136 ; set External Bus clock
1137 ; Be aware to do smooth clock setting, to avoid wrong clock setting
1138 ; Take care, always write 0 to the lower 4 bits of DIVR1 register
1139         LDI             #0x0487, R2             ; Set DIVR1  
1140         LDI             #(EXTBUSCLOCK << 4), R3 ; Load Peripheral clock setting
1141         STB             R3, @R2 
1142         
1143 ;=========================================================================================
1144 ; 7.6.6.3  Set CAN clock prescaler
1145 ;=========================================================================================
1146 ; Set CAN Prescaler, only clock relevant parameter 
1147         LDI             #0x04C0, R0             ; Set CAN ClockParameter Register
1148         LDI             #(PSCLOCKSOURCE + PSDVC), R1     ; Load Divider
1149         STB             R1, @R0                          ; Set Divider
1150 ; enable CAN clocks
1151         LDI             #0x04c1, R0             ; Set CAN Clock enable Register
1152         LDI             #CANCLOCK, R1           ; Load CANCLOCK
1153         STB             R1, @R0                 ; set CANCLOCK
1154
1155 ;=========================================================================================
1156 ; 7.6.6.4  Switch Main Clock Mode
1157 ;=========================================================================================
1158 #if CLOCKSOURCE == MAINCLOCK
1159
1160 ;=========================================================================================
1161 ; 7.6.6.5  Switch Subclock Mode
1162 ;=========================================================================================
1163 #elif ( (CLOCKSOURCE == SUBCLOCK) )
1164     #if ENABLE_SUBCLOCK == ON
1165         LDI             #0x4CA, R12
1166 subStabTime:        
1167         ClearRCwatchdog                         ; clear harware watchdog
1168         BTSTH           #8, @R12                ; wait until sub clock stabilisation
1169         BEQ             subStabTime             ; time is over
1170         LDI             #0x0, R1
1171         STB             R1, @R12
1172
1173         LDI             #0x0484, R0             ; Clock source control reg CLKR
1174         LDI             #0x01, R1               ; load value to select main clock
1175         ORB             R1, @R0                 ; enable main clock (1/2 external)        
1176         LDI             #0x03, R1               ; load value to select subclock
1177         ORB             R1, @R0                 ; enable subclock as clock source       
1178     #else
1179         #error: Wrong setting! The clock source is subclock, but the subclock is disabled.
1180     #endif
1181
1182 ;=========================================================================================
1183 ; 7.6.7  Switch to PLL Mode
1184 ;=========================================================================================
1185 #elif ( (CLOCKSOURCE == MAINPLLCLOCK) )
1186
1187 #if (DIV_G != 0x00)
1188         LDI             #0x0490, R0             ; PLL Ctrl Register   
1189         LDI             #0x00,R1
1190         STB             R1, @R0                 ; Clear Flag
1191         LDI             #0x01,R1
1192         STB             R1, @R0                 ; Set Flag for Simulator; no Effekt on
1193 #endif                                                ; Emulator
1194  
1195         LDI             #0x0484, R3             ; Clock source control reg CLKR
1196         BORL            #0x2, @R3               ; enable PLL as clock source                                               
1197                                                 
1198 #if (DIV_G != 0x00)                                                
1199 gearUpLoop:    
1200         ClearRCwatchdog                         ; clear harware watchdog
1201         LDUB            @R0, R2                 ; LOAD PLLCTR to R2
1202         AND             R1, R2                  ; GRUP, counter reach 0
1203         BEQ             gearUpLoop
1204
1205         LDI             #0x00,R1
1206         STB             R1, @R0                 ; Clear Gear-Up Flag
1207 #endif         
1208      
1209 #endif
1210
1211 ;=========================================================================================
1212 ; 7.6.8  Enable Frequncy Modulation - Clock Modulator Part II
1213 ;=========================================================================================
1214 #if CLOMO == ON                                 ; Only applicable if Modulator is on
1215         LDI             #0x04B8, R0             ; Clock Modulation Parameter Reg
1216         LDI             #CMPR, R1               ; Load CMP value
1217         STH             R1, @R0                 ; Store CMP value in CMPR
1218
1219         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
1220         LDI             #0x13, R1               ; Load value to FM on CM
1221         ORB             R1, @R0                 ; FM on 
1222 #endif
1223
1224 #endif
1225 noClockStartup:
1226
1227 ;=========================================================================================
1228 ; 7.7  Set BusInterface
1229 ;=========================================================================================
1230 ; Start restriction; No ext. bus interface
1231 #if (DEVICE != MB91464A) && (DEVICE != MB91467C) && (DEVICE != MB91465K) &&  \
1232     (DEVICE != MB91463N) && (DEVICE != MB91465X)
1233 ; End restriction
1234         NOP
1235 smd_cs_mb91461r:
1236 emu_sram_cs_mb91461r:
1237 smd_cs:
1238
1239 #endif                                          ; #endif (excl. devices)
1240         ClearRCwatchdog
1241
1242 ;=========================================================================================
1243 ; 7.8  Copy code from Flash to I-RAM 
1244 ;=========================================================================================
1245 #if I_RAM == ON
1246         LDI             #_RAM_IRAM, R0
1247         LDI             #_ROM_IRAM, R1
1248         LDI             #sizeof(IRAM), R13
1249         CMP             #0, R13
1250         BEQ             copy_iram_end
1251 copy_iram1: 
1252         ADD             #-1, R13
1253         LDUB            @(R13, R1), R12
1254         BNE:D           copy_iram1
1255         STB             R12, @(R13, R0)
1256 copy_iram_end: 
1257         ClearRCwatchdog
1258 #endif
1259
1260 ;=========================================================================================
1261 ; Standard C startup
1262 ;=========================================================================================
1263 ;=========================================================================================
1264 ; 7.10  Clear data 
1265 ;=========================================================================================
1266 ; clear DATA section
1267 ; According to ANSI, the DATA section must be cleared during start-up
1268         LDI:8           #0, R0
1269         LDI             #sizeof DATA &~0x3, R1
1270         LDI             #DATA, R13
1271         CMP             #0, R1
1272         BEQ             data_clr1
1273 data_clr0:
1274         ADD2            #-4, R1
1275         BNE:D           data_clr0
1276         ST              R0, @(R13, R1)
1277 data_clr1:
1278         LDI:8           #sizeof DATA & 0x3, R1
1279         LDI             #DATA + (sizeof DATA & ~0x3), R13
1280
1281         CMP             #0, R1
1282         BEQ             data_clr_end
1283 data_clr2:
1284         ADD2            #-1, R1
1285         BNE:D           data_clr2
1286         STB             R0, @(R13, R1)
1287 data_clr_end:
1288         ClearRCwatchdog
1289         
1290 ;=========================================================================================
1291 ; 7.11  Copy Init section from ROM to RAM
1292 ;=========================================================================================
1293 ; copy rom
1294 ; All initialised data's (e.g. int i=1) must be stored in ROM/FLASH area. 
1295 ; (start value)
1296 ; The Application must copy the Section (Init) into the RAM area.
1297         LDI             #_RAM_INIT, R0
1298         LDI             #_ROM_INIT, R1
1299         LDI             #sizeof(INIT), R2
1300         CMP             #0, R2
1301         BEQ:D           copy_rom_end
1302         LDI             #3, R12
1303         AND             R2, R12
1304         BEQ:D           copy_rom2
1305         MOV             R2, R13
1306         MOV             R2, R3
1307         SUB             R12, R3
1308 copy_rom1:
1309         ADD             #-1, R13
1310         LDUB            @(R13, R1), R12
1311         CMP             R3, R13
1312         BHI:D           copy_rom1
1313         STB             R12, @(R13, R0)
1314         CMP             #0, R3
1315         BEQ:D           copy_rom_end
1316 copy_rom2:
1317         ADD             #-4, R13
1318         LD              @(R13, R1), R12
1319         BGT:D           copy_rom2
1320         ST              R12, @(R13, R0)
1321 copy_rom_end:
1322         ClearRCwatchdog
1323
1324 start_main:
1325 ;=========================================================================================
1326 ; 7.14  call main routine
1327 ;=========================================================================================
1328        ClearRCwatchdog                            ; clear harware watchdog
1329        LDI:8            #0, r4                    ; Set the 1st parameter for main to 0.
1330        CALL32:d         _main, r12
1331        LDI:8            #0, r5                    ; Set the 2nd parameter for main to 0.
1332 ;=========================================================================================
1333 ; 7.15  Return from main function
1334 ;=========================================================================================
1335 end: 
1336         BRA            end  
1337         .end            __start