                              1 ;------------------------------------------------------------------------------
                              2 ;  This file is part of the DAvE SDCC Compiler package
                              3 ;  Startup Code for the Infineon XC8xx devices 
                              4 ;  Copyright (c) .
                              5 ;  Version 1.00
                              6 ;
                              7 ;------------------------------------------------------------------------------
                              8 ;  startupxc886.s:  This code is executed after processor reset.
                              9 ;
                             10 ;------------------------------------------------------------------------------
                             11 ;
                             12 ; XC8xx series runs by default from on-chip osciallator.
                             13 ;------------------------------------------------------------------------------
                             14 
                             15 .area startup_code(code)
                             16 
                             17 .globl __sdcc_external_startup
   00D8                      18 __sdcc_external_startup:
                             19 
                    0001     20 UART = 1        ; set for UART Initialisation
                    0000     21 XTAL = 0   ; set to 0 On-chip oscillator
                             22 
                             23 ; <o> PLL N-Divider
                             24 ; <0=>  N=10
                             25 ; <1=>  N=12
                             26 ; <2=>  N=13
                             27 ; <3=>  N=14
                             28 ; <4=>  N=15
                             29 ; <5=>  N=16
                             30 ; <6=>  N=17
                             31 ; <7=>  N=18
                             32 ; <8=>  N=19
                             33 ; <9=>  N=20 (9.6 MHz On-Chip XTAL)
                             34 ; <10=> N=24 (8 MHz Ext XTAL)
                             35 ; <11=> N=30
                             36 ; <12=> N=32
                             37 ; <13=> N=36
                             38 ; <14=> N=40
                             39 ; <15=> N=48
                             40 
                    0009     41      NDIV = 9   ; default 9
                             42 
                             43 ; XC8xx specific SFR Symbols used in STARTUP code
                    00BF     44      SCU_PAGE = 0x00BF
                    00B7     45      PLL_CON  = 0x00B7
                    00B6     46      OSC_CON  = 0x00B6
                    00BB     47      PASSWD   = 0x00BB
                             48 
                    0000     49 .if XTAL                                         ; switch to external XTAL
                             50      MOV     SCU_PAGE,#1
                             51      ORL     PLL_CON, #0x04  ; OSCDISC = 1   _ _ _ NDIV, VCOBYP, OSCDISC, RESLD, LOCK
                             52      ORL     OSC_CON, #0x04  ; OSCSS = 1     0, 0, 0, OSCPD, XPD, OSCSS, ORDRES, OSCR
                             53      ANL     OSC_CON, #0xF7  ; XPD = 0       power xtal
                             54 
                             55      MOV     R0,#10          ; delay around 1.5 ms
                             56 
                             57 DelayXTAL:
                             58 Delay1: DJNZ R1, Delay1
                             59         DJNZ R0, DelayXTAL
                             60                 
                             61 OSCR_NOTSET:
                             62      ORL     OSC_CON, #0x02  ; ORDRES = 1
                             63 WAIT_ORDRES_0:
                             64      MOV A, OSC_CON
                             65      JB      ACC.1,WAIT_ORDRES_0
                             66      JNB     ACC.0,OSCR_NOTSET
                             67 
                             68      ; VCOBYP to change N-Divider
                             69      ORL     PLL_CON, #0x08  ; VCOBYP = 1
                             70      ANL     PLL_CON, #0xFB  ; OSCDISC = 0
                             71      MOV     PASSWD,  #0x98  ; open access to writing protected bit
                             72      ANL     PLL_CON, #0x0F
                             73      ORL     PLL_CON, #NDIV*16
                             74 
                             75      ORL     PLL_CON, #0x02  ; detect PLL lock
                             76 WAIT_LOCK:
                             77      MOV A, PLL_CON
                             78      JNB     ACC.0, WAIT_LOCK
                             79                     ; reconnect to PLL
                             80      ANL     PLL_CON, #0xF7  ; VCOBYP = 0
                             81      MOV     SCU_PAGE,#0
                             82 .endif
                             83 
                             84 
                    0001     85 .if UART        ;If UART Initialisation set
                             86 
                    00B2     87      PORT_PAGE   = 0x00B2
                    0090     88      P1_ALTSEL0  = 0x0090
                    0091     89      P1_ALTSEL1  = 0x0091
                    0091     90      P1_DIR      = 0x0091
                    00B3     91      MODPISEL    = 0x00B3
                    0098     92      SCON        = 0x0098
                    00BE     93      BG          = 0x00BE
                    00BD     94      BCON        = 0x00BD
                             95 
                             96 ;UART Initialisation Code (Use this if you are not using --uart-init commandline option)
                             97 
   00D8 75 B2 02             98      MOV  PORT_PAGE, #0x02   ;Switch to page 2
   00DB 53 90 FD             99      ANL  P1_ALTSEL0,    #0xFD   ;configure alternate function register 0
   00DE 43 91 02            100      ORL  P1_ALTSEL1,    #0x02   ;configure alternate function register 1
   00E1 75 B2 00            101      MOV PORT_PAGE,  #0x00   ;switch to page 0
   00E4 43 91 02            102      ORL  P1_DIR,        #0x02   ;set output direction
   00E7 53 B3 FE            103      ANL  MODPISEL,  #0xFE   ;configure peripheral input select register
   00EA 75 98 52            104      MOV SCON,       #0x52   ;load serial channel control register
   00ED 75 BE 9C            105      MOV BG,         #0x9C   ;load baudrate timer/reload register
   00F0 43 BD 01            106      ORL  BCON,      #0x01   ;load baudrate control register
                            107 
                            108 ;END of UART Initialisation
                            109 
                            110 .endif
                            111 
   00F3 22                  112 ret         ;End of __sdcc_external_startup
                            113 
