removed USTACK
[pyfrprog.git] / pkernel / start91460.asm
1 #set    OFF             0
2 #set    ON              1
3 #set    DEFAULT         2
4 ;=========================================================================================
5 ; 4.1  Controller Device
6 ;=========================================================================================
7 #set    MB91464A        2                       ; MB91460 series
8 ;
9 #set    MB91467B       10                       ; MB91460 series
10 ;
11 #set    MB91467C       11                       ; MB91460 series
12 ;
13 #set    MB91467D        4                       ; MB91460 series
14 ;
15 #set    MB91469G        6                       ; MB91460 series
16 ;
17 #set    MB91465K        3                       ; MB91460 series
18 ;
19 #set    MB91463N        8                       ; MB91460 series
20 ;
21 #set    MB91461R        1                       ; MB91460 series
22 #set    MB91467R        5                       ; MB91460 series
23 ;
24 #set    MB91465X        9                       ; MB91460 series
25 ;
26 #set    others          7                       ; MB91460 series
27 ;
28 ;
29 #set    DEVICE          MB91465X                ; <<< select device
30 ;=========================================================================================
31 ; 4.3  Stack Type and Stack Size
32 ;=========================================================================================
33 ;
34 #set    USRSTACK        0                       ; user stack:   for main program
35 #set    SYSSTACK        1                       ; system stack: for main program and 
36 ;                                               ;               interrupts
37 ;
38 #set    STACKUSE        SYSSTACK                ; <<< set active stack
39 ;
40 #set    STACK_RESERVE   ON                      ; <<< reserve stack area in 
41 ;                                               ;     this module
42 #set    STACK_SYS_SIZE  0x400-4                 ; <<< byte size of System stack
43 #set    STACK_USR_SIZE  0x2                     ; <<< byte size of User stack 
44 ;
45 ; - If the active stack is set to SYSSTACK,  it is used for main program and interrupts. 
46 ;   In this case,  the user stack  could be set to a dummy size.  If the active stack is 
47 ;   set  to  user  stack,  it is  used  for the  main  program  but the  system stack is 
48 ;   automatically activated,  if an interrupt is serviced.  Both stack areas must have a 
49 ;   reasonable size.
50 ; - If STACK_RESERVE is ON,  the sections USTACK and SSTACK are reserved in this module. 
51 ;   Otherwise, they have to be reserved in other modules.  If STACK_RESERVE is OFF,  the 
52 ;   size definitions STACK_SYS_SIZE and STACK_USR_SIZE have no meaning.
53 ; - Even if  they  are reverved  in other modules,  they are  still initialised  in this 
54 ;   start-up file.
55 ;
56 ; Note: Several library functions require quite a big stack (due to ANSI). 
57 ;       Check the stack information files (*.stk) in the LIB\911 directory.
58 ;
59 ;=========================================================================================
60 ; 4.4  Copy code from Flash to I-RAM
61 ;=========================================================================================
62 ;
63 #set    I_RAM           ON                      ; <<< select  if  code  in  section IRAM
64 ;                                                     should be copied
65 ; If this option is activated code located in the  section IRAM is copied during startup 
66 ; from ROM to the instruction-RAM. The code is linked for the instruction-RAM.
67 ;
68 ;=========================================================================================
69 ; 4.7  Clock Configuration
70 ;=========================================================================================
71 ;=========================================================================================
72 ; 4.7.1  Clock Selection
73 ;=========================================================================================
74 ;
75 ; No clock settings
76 #set    NO_CLOCK                                               0x01
77 ;
78 ; Sub-oscillation input: 32 kHz 
79 #set    SUB_32KHZ_CPU__32KHZ_PER_32KHZ_EXT_32KHZ_CAN__2MHZ     0x11
80 ;
81 ; Oscillation input: 4 MHz 
82 #set    PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ     0x23
83 ;
84 ; MB91461R only: Oscillation input: 10 MHz
85 #set    PLL_10MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x41
86 ;
87 ; MB91461R only: Oscillation input: 20 MHz
88 #set    PLL_20MHZ_CPU__60MHZ_PER_20MHZ_EXT_30MHZ_CAN_20MHZ     0x51
89 ;
90 ; User settings
91 #set    CLOCK_USER                                             0x61
92 ;
93 ;
94 ;
95 #set    CLOCKSPEED      PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ
96 ;                                               ; <<< Select clock configuration 
97 ;
98 ;=========================================================================================
99 ; 4.7.2  Select Clock Modulator  
100 ;=========================================================================================
101 ;
102 #set    CLOMO           OFF                     ; <<< Enable /disable clock modulator      
103 ;
104 #set    CMPR            0x026F                  ; <<< Ref. to the data sheet, CMPR
105
106 ; Please  refer  to the data sheet  of the device  if you  enable clock modulation.  The 
107 ; register CMPR dependant on the PLL-Clock.
108 ;
109 ; Note: If the CLKCAN source is set either to main oscillator or to PLL  output then the
110 ;       clock  for  the CAN  is not influenced by  the clock  modulation.  If the CLKCAN 
111 ;       source is set CPU clock (CLKB) then the clock for the CAN is also modulated  (if 
112 ;       the clock modulator is enabled).
113 ;
114 ; Note: If the clock modulator is enabled,  the wait states  of the  internal flash wait 
115 ;       states  must  be  adapted  to  maximum frequency.  Please check the  wait states 
116 ;       settings.
117 ;
118 ; Note: This feature  is not supported by every device,  e.g. MB91461.  Please check the 
119 ;       data sheet.
120 ;
121 ;=========================================================================================
122 ; 4.8  External Bus Interface
123 ;
124 ;      The rest of the configuration is only applicable for devices with an external bus 
125 ;      interface.
126 ;
127 ;      If the device does not offer an external bus interface,  the configuration can be 
128 ;      stoped at this point.
129 ;
130 ;=========================================================================================
131 ; 5  Definition of Configurations
132 ;=========================================================================================
133 ;
134 #set    NOCLOCK         0                       ; do not touch CKSCR register
135 #set    MAINCLOCK       1                       ; select main clock 
136 ;                                               ; MB91461R : 1/4 of oscillation input
137 ;                                               ; Others:    1/2 of oscillation input
138 #set    MAINPLLCLOCK    2                       ; select main clock with PLL
139 #set    SUBCLOCK        3                       ; select subclock (if available)
140 ;
141 #set    PSCLOCK_CLKB    0x00                    ; select core clock (initial)
142 #set    PSCLOCK_PLL     0x10                    ; select PLL output (x)
143 #set    PSCLOCK_MAIN    0x30                    ; select Main Oscillation
144 ;
145 ;=========================================================================================
146 ; 5.2  CLOCKSPEED == NO_CLOCK
147 ;=========================================================================================
148 ;
149 #if (CLOCKSPEED == NO_CLOCK )
150     #set CLOCKSOURCE       NOCLOCK 
151 #endif      
152 ;=========================================================================================
153 ; 5.5  CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ 
154 ;=========================================================================================
155 ;
156 #if (CLOCKSPEED == PLL_4MHZ__CPU__64MHZ_PER_16MHZ_EXT_32MHZ_CAN_16MHZ )
157 ;
158 ; Start restriction; Maximum frequency
159   #if (DEVICE == MB91461R) 
160      #error: Frequency is not supported by this device.
161   #endif 
162 ; End restriction
163 ;
164   #set  CLOCKSOURCE     MAINPLLCLOCK            ; Clocksource
165   #set  ENABLE_SUBCLOCK OFF                     ; Subclock: ON/OFF
166   #set  PLLSPEED        0x010F                  ; 0x48Ch, 0x48Dh: PLLDIVM/N    ;  64 MHz
167   #set  DIV_G           0x0F                    ; 0x48Eh: PLLDIVG; 
168   #set  MUL_G           0x0F                    ; 0x48Fh: PLLMULG;     
169   ; Clock Divider
170   #set  CPUCLOCK        0x00                    ; 0x486h: DIV0R_B;    => /1    ;  64 MHz       
171   #set  PERCLOCK        0x03                    ; 0x486h: DIV0R_P;    => /4    ;  16 MHz 
172   #set  EXTBUSCLOCK     0x01                    ; 0x487h: DIV1R_T;    => /2    ;  32 MHz 
173   ; CAN Clock
174   #set  PSCLOCKSOURCE   PSCLOCK_PLL             ; 0x4C0h: CANPRE;     => PLLx  ; 128 MHz
175   #set  PSDVC           0x07                    ; 0x4C0h: CANPRE_DVC; => /8    ;  16 MHz
176   #set  CANCLOCK        0x00                    ; 0x4C1h: CANCKD; all CAN Clocks enabled
177   ; Voltage Regulator 
178   #set  REGULATORSEL    0x06                    ; 0x4CEh: REGSEL;
179   #set  REGULATORCTRL   0x00                    ; 0x4CFh: REGCTR;
180   ; Memory Controller
181   #set  FLASHCONTROL    0x032                   ; 0x7002h: FCHCR;
182   #set  FLASHREADT      0xC413                  ; 0x7004h: FMWT;
183   #set  FLASHMWT2       0x10                    ; 0x7006h: FMWT2;
184 #endif  
185 ;
186 ;=========================================================================================
187 ; 6  Section and Data Declaration
188 ;=========================================================================================
189         .export __start             
190         .import _main
191         .import _RAM_INIT
192         .import _ROM_INIT
193 ;=========================================================================================
194 ; 6.1  Define Stack Size
195 ;=========================================================================================
196  .SECTION  SSTACK, STACK, ALIGN=4
197 #if STACK_RESERVE == ON
198         .EXPORT         __systemstack, __systemstack_top
199  __systemstack:
200         .RES.B          STACK_SYS_SIZE
201  __systemstack_top: 
202 #endif
203  
204 ;=========================================================================================
205 ; 6.2  Define Sections
206 ;=========================================================================================
207         .section        DATA,  data,  align=4
208         .section        INIT,  data,  align=4
209         .section        IRAM,  code,  align=4
210         
211 #if I_RAM 
212         .import _RAM_IRAM
213         .import _ROM_IRAM
214 #endif
215                     
216 ;-----------------------------------------------------------------------------------------
217 ; MACRO Clear RC Watchdog
218 ;-----------------------------------------------------------------------------------------
219 #macro  ClearRCwatchdog
220         LDI             #0x4C7,R7               ; clear RC watchdog
221         BANDL           #0x7,@R7
222 #endm
223         .section        CODE, code, align=4
224         .section        CODE_START, code, align=4
225 #pragma section CODE=IRAM,attr=CODE
226
227 ;=========================================================================================
228 ; 7.  S T A R T 
229 ;=========================================================================================
230 __start:                                        ; start point   
231 startnop: 
232         NOP       
233 ;   
234         ANDCCR          #0xEF                   ; disable interrupts   
235         STILM           #31                         ; set interrupt level to low prior
236         ClearRCwatchdog                         ; clear harware watchdog
237
238 ;=========================================================================================
239 ; 7.1  Initialise Stack Pointer
240 ;=========================================================================================
241 #if STACKUSE == SYSSTACK       
242         ANDCCR          #0xDF
243         LDI             #__systemstack_top, SP  ; initialize SP
244 #endif
245
246 #if (CLOCKSOURCE != NOCLOCK)                                          
247 ;=========================================================================================
248 ; 7.2  Check for CSV reset and set CSV
249 ;=========================================================================================
250 ; Start restriction; No clock supervisor (CSV)
251 #if (DEVICE != MB91461R) && (DEVICE != MB91467R) && (DEVICE != MB91463N)
252 ; End restriction
253         LDI:20          #0x04AD, R0             ; CSVCR
254         BORL            #0x8, @R0               ; Enable Main Osc CSV
255         BTSTH           #0x4, @R0               ; Check for Main Osc missing
256         BEQ             NoMAINCSVreset          ; Main osc available -> branch 
257                                                 ;   to NoCSVreset
258         BANDL           #0x7, @R0               ; Disable Main Osc CSV
259         
260         LDI             #noClockStartup, R0     ; Main Clock missing -> no
261         JMP             @R0                     ; clock startup
262                                                 
263 NoMAINCSVreset: 
264
265
266         BORL            #0x4, @R0               ; Enable Sub Osc CSV
267         BTSTH           #0x2, @R0               ; Check for Sub Osc missing
268         BEQ             NoSUBCSVreset           ; Sub osc available -> branch 
269                                                 ;   to NoCSVreset
270         BANDL           #0xB, @R0               ; Disable Sub Osc SCSV
271 #if (CLOCKSOURCE == SUBCLOCK)
272         LDI             #noClockStartup, R0     ; Sub Clock missing -> no
273         JMP             @R0                     ; clock startup
274 #endif                                                
275 NoSUBCSVreset:       
276 #endif        
277 ;=========================================================================================
278 ; 7.3  Check Clock Condition
279 ;=========================================================================================
280         LDI             #0x484, R0              ; Check for Default Values
281         LDI             #0x0F, R1               
282         ANDB            R1, @R0
283         BEQ             clock_startup 
284
285 ;=========================================================================================
286 ; 7.4  Restore Default Settings after Reset
287 ;=========================================================================================
288 ;=========================================================================================
289 ; 7.4.1  Disable Clock Modulator
290 ;=========================================================================================
291         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
292         BANDL           #0xD, @R0               ; Disable Frequency modulation
293 FMODwait:        
294         BTSTL           #8, @R0                 ; Wait until Frequency modulation
295         BNE             FMODwait                ; is disabled
296         
297         BANDL           #0xE, @R0               ; Power down clock modulator
298
299 ;=========================================================================================
300 ; 7.4.2  Check if running on Sub Clock, change to Main Clock
301 ;=========================================================================================
302         LDI:20          #0x0484,R12             ; Check if running on sub clock
303         LDUB            @R12,R0
304         LDI:8           #0x3,R1
305         AND             R1,R0
306         CMP             #0x3,R0
307         BNE             notOnSubClock
308         
309         LDI:20          #0x04CC,R12             ; Check if Main Clock is stopped
310         BTSTL           #1, @R12
311         BEQ             mainNotStopped
312
313         BANDL           #0xE, @R12              ; Start Main Oscillation
314                         
315         LDI             #0x4C8, R0              ; Main Stabilisation Wait Time
316         LDI             #0x04, R1               ; 32.7 ms
317         AND             R1, @R0  
318         BORH            #0x02, @R0      
319         
320         mainStabTime:                           ; Wait for stabilisation time
321         ClearRCwatchdog                         ; clear harware watchdog
322         BTSTH           #8, @R0
323         BEQ             mainStabTime
324         LDI             #0x0, R1
325         STB             R1, @R0
326
327 mainNotStopped:        
328         LDI:20          #0x0484, R12            ; disable sub clock as source
329         BANDL           #0xD, @R12              ; Clock source = 0x01 (Main/2)  
330                                                        
331 notOnSubClock:
332 ;=========================================================================================
333 ; 7.4.3  Disable Sub Clock
334 ;=========================================================================================
335 #if ENABLE_SUBCLOCK != ON
336         LDI             #0x0484, R0             ; Clock source control reg CLKR
337         BANDL           #0x7, @R0               ; Disable PLL
338 #endif       
339
340 ;=========================================================================================
341 ; 7.4.4  Check if running on PLL, Gear Down PLL
342 ;=========================================================================================
343         LDI:20          #0x0484,R12             ; Check if running on PLL
344         LDUB            @R12,R0
345         LDI:8           #0x3,R1
346         AND             R1,R0
347         CMP             #0x2,R0
348         BNE             notOnPll
349                     
350         LDI:20          #0x0490, R11            ; clear flags  
351         LDI:8           #0x0,R1        
352         STB             R1, @R11
353         LDI             #0x04,R1
354         STB             R1, @R11                ; Set Flag for Simulator; no Effekt on
355                                                 ; Emulator      
356
357         BANDL           #0xC, @R12              ; disable PLL as clock source  
358                                                 ; Clock Source = 0x00 (Main/2)
359                                                     
360         LDI:20          #0x048E,R12             ; check if DivG != 0
361         LDUB            @R12, R0
362         LDI:8           #0xFF,R1
363         AND             R1,R0
364         BEQ             notOnPll
365                                                                                           
366 gearDownLoop:    
367         ClearRCwatchdog                         ; clear harware watchdog
368         BTSTL           #4, @R11                ; Gear Down
369         BEQ             gearDownLoop            ; 
370  
371         LDI             #0x00,R1                ; Clear Flags
372         STB             R1, @R11                ;       
373         
374 notOnPll:
375 ;=========================================================================================
376 ; 7.4.5  Disable PLL
377 ;=========================================================================================
378         LDI             #0x0484, R0             ; Clock source control reg CLKR
379         BANDL           #0xB, @R0               ; Disable PLL
380         
381 ;=========================================================================================
382 ; 7.4.6  Set to Main Clock
383 ;=========================================================================================
384         LDI:20          #0x0484,R12             ; Check if running on PLL
385         BANDL           #0xC, @R12              ; disable PLL as clock source  
386                                                 ; Clock Source = 0x00 (Main/2)
387
388 clock_startup:
389 ;=========================================================================================
390 ; 7.5  Set Memory Controller
391 ;=========================================================================================
392 ; Start restriction; No embedded flash
393 #if DEVICE != MB91461R
394 ; End restriction
395         LDI             #0x7002, R1             ; FLASH Controller Reg.
396         LDI             #FLASHCONTROL, R2       ; Flash Controller Settings
397         STH             R2, @R1                 ; set register
398         LDI             #0x7004, R1             ; FLASH Memory Wait Timing Reg.
399         LDI             #FLASHREADT, R2         ; wait settings
400         STH             R2, @R1                 ; set register
401         LDI             #0x7006, R1             ; FLASH Memory Wait Timing Reg.
402         LDI             #FLASHMWT2, R2          ; wait settings
403         STB             R2, @R1                 ; set register               
404 #endif                
405         ClearRCwatchdog   
406                                                        
407 ;=========================================================================================
408 ; 7.6  Clock startup
409 ;=========================================================================================
410 ;=========================================================================================
411 ; 7.6.1  Set Voltage Regulator Settings
412 ;=========================================================================================
413 ; Start restriction; No regulator settings
414 #if DEVICE != MB91461R
415 ; End restriction
416         LDI             #0x04CF, R0             ; REGCTR
417         LDI             #REGULATORCTRL, R1
418         STB             R1, @R0
419
420         LDI             #0x04CE, R0             ; REGSEL
421         LDI             #REGULATORSEL, R1
422         STB             R1, @R0
423 #endif
424
425 ;=========================================================================================
426 ; 7.6.2  Power on Clock Modulator - Clock Modulator Part I
427 ;=========================================================================================
428 #if CLOMO == ON 
429         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
430         LDI             #0x11, R1               ; Load value to Power on CM
431         ORB             R1, @R0                 ; Power on clock modulaor
432 #endif
433
434 ;=========================================================================================
435 ; 7.6.3  Set CLKR Register w/o Clock Mode
436 ;=========================================================================================
437 ; Set Clock source (Base Clock) for the three clock tree selections
438 ; This select Base clock is used to select afterwards the 3
439 ; Clocks for the diffenrent internal trees.
440 ; When PLL is used, first pll multiplication ratio is set and PLL is
441 ; enabled. After waiting the PLL stabilisation time via timebase
442 ; timer, PLL clock is selected as clock source. 
443         LDI             #0x048C, R0             ; PLL Cntl Reg. PLLDIVM/N
444         LDI:20          #PLLSPEED, R1
445         STH             R1, @R0
446
447         LDI             #0x048E, R0             ; PLL Cntl Reg. PLLDIVG
448         LDI             #DIV_G, R1
449         STB             R1, @R0
450
451         LDI             #0x048F, R0             ; PLL Cntl Reg. PLLMULG
452         LDI             #MUL_G, R1
453         STB             R1, @R0
454
455 ;=========================================================================================
456 ; 7.6.4  Start PLL 
457 ;=========================================================================================
458 #if ( ( CLOCKSOURCE == MAINPLLCLOCK ) || ( PSCLOCKSOURCE == PSCLOCK_PLL ) )
459         LDI             #0x0484, R0             ; Clock source control reg CLKR
460         LDI             #0x04, R1               ; Use PLL x1, enable PLL 
461         ORB             R1, @R0                 ; store data to CLKR register
462 #endif
463        
464 ;=========================================================================================
465 ; 7.6.5  Wait for PLL oscillation stabilisation
466 ;=========================================================================================
467 #if ((CLOCKSOURCE==MAINPLLCLOCK)||(PSCLOCKSOURCE==PSCLOCK_PLL))
468         LDI             #0x0482, R12            ; TimeBaseTimer TBCR
469         LDI             #0x00, R1               ; set 1024 us @ 2 MHz 
470         STB             R1, @R12
471
472         BANDH           #7, @R12                ; clear interrupt flag
473         
474         LDI             #0x0483, R0             ; clearTimeBaseTimer CTBR
475         LDI             #0xA5, R1                 
476         STB             R1, @R0
477         LDI             #0x5A, R1                 
478         STB             R1, @R0
479         
480         BANDH           #7, @R12                ; clear interrupt flag
481         BORH            #8, @R12                ; set interrupt flag for simulator
482
483 PLLwait:        
484         ClearRCwatchdog                         ; clear harware watchdog
485         BTSTH           #8, @R12
486         BEQ             PLLwait
487 #endif
488
489 ;=========================================================================================
490 ; 7.6.6  Set clocks 
491 ;=========================================================================================
492 ;=========================================================================================
493 ; 7.6.6.1  Set CPU and peripheral clock 
494 ;=========================================================================================
495 ; CPU and peripheral clock are set in one register
496         LDI             #0x0486, R2             ; Set DIVR0 (CPU-clock (CLKB)  
497         LDI             #((CPUCLOCK << 4) + PERCLOCK), R3 ; Load CPU clock setting
498         STB             R3, @R2               
499 ;=========================================================================================
500 ; 7.6.6.2  Set External Bus interface clock
501 ;=========================================================================================
502 ; set External Bus clock
503 ; Be aware to do smooth clock setting, to avoid wrong clock setting
504 ; Take care, always write 0 to the lower 4 bits of DIVR1 register
505         LDI             #0x0487, R2             ; Set DIVR1  
506         LDI             #(EXTBUSCLOCK << 4), R3 ; Load Peripheral clock setting
507         STB             R3, @R2 
508         
509 ;=========================================================================================
510 ; 7.6.6.3  Set CAN clock prescaler
511 ;=========================================================================================
512 ; Set CAN Prescaler, only clock relevant parameter 
513         LDI             #0x04C0, R0             ; Set CAN ClockParameter Register
514         LDI             #(PSCLOCKSOURCE + PSDVC), R1     ; Load Divider
515         STB             R1, @R0                          ; Set Divider
516 ; enable CAN clocks
517         LDI             #0x04c1, R0             ; Set CAN Clock enable Register
518         LDI             #CANCLOCK, R1           ; Load CANCLOCK
519         STB             R1, @R0                 ; set CANCLOCK
520
521 ;=========================================================================================
522 ; 7.6.7  Switch to PLL Mode
523 ;=========================================================================================
524 #if ( (CLOCKSOURCE == MAINPLLCLOCK) )
525
526 #if (DIV_G != 0x00)
527         LDI             #0x0490, R0             ; PLL Ctrl Register   
528         LDI             #0x00,R1
529         STB             R1, @R0                 ; Clear Flag
530         LDI             #0x01,R1
531         STB             R1, @R0                 ; Set Flag for Simulator; no Effekt on
532 #endif                                                ; Emulator
533  
534         LDI             #0x0484, R3             ; Clock source control reg CLKR
535         BORL            #0x2, @R3               ; enable PLL as clock source                                               
536                                                 
537 #if (DIV_G != 0x00)                                                
538 gearUpLoop:    
539         ClearRCwatchdog                         ; clear harware watchdog
540         LDUB            @R0, R2                 ; LOAD PLLCTR to R2
541         AND             R1, R2                  ; GRUP, counter reach 0
542         BEQ             gearUpLoop
543
544         LDI             #0x00,R1
545         STB             R1, @R0                 ; Clear Gear-Up Flag
546 #endif         
547      
548 #endif
549
550 ;=========================================================================================
551 ; 7.6.8  Enable Frequncy Modulation - Clock Modulator Part II
552 ;=========================================================================================
553 #if CLOMO == ON                                 ; Only applicable if Modulator is on
554         LDI             #0x04B8, R0             ; Clock Modulation Parameter Reg
555         LDI             #CMPR, R1               ; Load CMP value
556         STH             R1, @R0                 ; Store CMP value in CMPR
557
558         LDI             #0x04BB, R0             ; Clock Modulator Control Reg
559         LDI             #0x13, R1               ; Load value to FM on CM
560         ORB             R1, @R0                 ; FM on 
561 #endif
562
563 #endif
564 noClockStartup:
565         ClearRCwatchdog
566
567 ;=========================================================================================
568 ; 7.8  Copy code from Flash to I-RAM 
569 ;=========================================================================================
570 #if I_RAM == ON
571         LDI             #_RAM_IRAM, R0
572         LDI             #_ROM_IRAM, R1
573         LDI             #sizeof(IRAM), R13
574         CMP             #0, R13
575         BEQ             copy_iram_end
576 copy_iram1: 
577         ADD             #-1, R13
578         LDUB            @(R13, R1), R12
579         BNE:D           copy_iram1
580         STB             R12, @(R13, R0)
581 copy_iram_end: 
582         ClearRCwatchdog
583 #endif
584
585 ;=========================================================================================
586 ; Standard C startup
587 ;=========================================================================================
588 ;=========================================================================================
589 ; 7.10  Clear data 
590 ;=========================================================================================
591 ; clear DATA section
592 ; According to ANSI, the DATA section must be cleared during start-up
593         LDI:8           #0, R0
594         LDI             #sizeof DATA &~0x3, R1
595         LDI             #DATA, R13
596         CMP             #0, R1
597         BEQ             data_clr1
598 data_clr0:
599         ADD2            #-4, R1
600         BNE:D           data_clr0
601         ST              R0, @(R13, R1)
602 data_clr1:
603         LDI:8           #sizeof DATA & 0x3, R1
604         LDI             #DATA + (sizeof DATA & ~0x3), R13
605
606         CMP             #0, R1
607         BEQ             data_clr_end
608 data_clr2:
609         ADD2            #-1, R1
610         BNE:D           data_clr2
611         STB             R0, @(R13, R1)
612 data_clr_end:
613         ClearRCwatchdog
614         
615 ;=========================================================================================
616 ; 7.11  Copy Init section from ROM to RAM
617 ;=========================================================================================
618 ; copy rom
619 ; All initialised data's (e.g. int i=1) must be stored in ROM/FLASH area. 
620 ; (start value)
621 ; The Application must copy the Section (Init) into the RAM area.
622         LDI             #_RAM_INIT, R0
623         LDI             #_ROM_INIT, R1
624         LDI             #sizeof(INIT), R2
625         CMP             #0, R2
626         BEQ:D           copy_rom_end
627         LDI             #3, R12
628         AND             R2, R12
629         BEQ:D           copy_rom2
630         MOV             R2, R13
631         MOV             R2, R3
632         SUB             R12, R3
633 copy_rom1:
634         ADD             #-1, R13
635         LDUB            @(R13, R1), R12
636         CMP             R3, R13
637         BHI:D           copy_rom1
638         STB             R12, @(R13, R0)
639         CMP             #0, R3
640         BEQ:D           copy_rom_end
641 copy_rom2:
642         ADD             #-4, R13
643         LD              @(R13, R1), R12
644         BGT:D           copy_rom2
645         ST              R12, @(R13, R0)
646 copy_rom_end:
647         ClearRCwatchdog
648
649 start_main:
650 ;=========================================================================================
651 ; 7.14  call main routine
652 ;=========================================================================================
653        ClearRCwatchdog                            ; clear harware watchdog
654        LDI:8            #0, r4                    ; Set the 1st parameter for main to 0.
655        CALL32:d         _main, r12
656        LDI:8            #0, r5                    ; Set the 2nd parameter for main to 0.
657 ;=========================================================================================
658 ; 7.15  Return from main function
659 ;=========================================================================================
660 end: 
661         BRA            end  
662         .end            __start