;=============================================================================
; (c) Copyright Raisonance S.A.S. 1989 - 2006, All rights reserved.
;=============================================================================
;
; This is the 'C' language "STARTUP" routine for RCST7.
;      File name:  STARTUP.A51
;
;        Purpose:  To provide the initial startup code for ST7 from
;                  power-up to the initial execution of the main() routine.
;
;          Scope:  This file contains all the code and information required
;                  to accomplish "Purpose". It is applicable to all memory
;                  models and RCST7 options.
;                  This file, assembled using the appropriate memory model,
;                  is automatically linked in by the linker when it encounters
;                  a "main" symbol in the object modules to be linked.
;
;                  The routine will be executed each time the controller
;                  experiences a "reset" condition.
;
;                  If you wish to use this startup routine with your assembly
;                  code too, you should add a "main" label in your asm code.
;
; Considerations:  The values selected are intended for a standard ST7
;                  controller. In most cases these will work well for must
;                  other processors under most conditions. The ranges for
;                  initialization, or the initialization values can be changed
;                  or modified as needed.
;
;        Caveats:  It is possible to modify this file and affect the execution
;                  the target processor in many ways. Often settings and
;                  selections have effects on each other. Some may not be
;                  compatible. This startup routine is provided "as is". If
;                  you change it to meet some design objective, you are
;                  responsible of the outcome of your selections, and
;                  their effect upon the target system.
;
;-----------------------------------------------------------------------------
;  NOTE:  YOU SHOULD ALTER THIS FILE ONLY AFTER YOU HAVE BUILT AND TESTED YOUR
;  CODE, AND YOU'RE CONVINCED THAT YOU CAN MAKE IT RUN OR WORK BETTER BY DOING
;  SO.  IF YOU ONLY NEED SOME SPECIFIC INITIALIZATIONS, IT IS RECOMMENDED THAT
;  YOU  DO  THEM  IN  THE FIRST FEW LINES OF YOUR MAIN() ROUTINE.  MODIFY THIS
;  FILE ONLY AS A LAST RESORT.
;=============================================================================
; $Id: startup.st7,v 1.29 2007/11/22 13:10:26 Bruno Exp $
;=============================================================================

    CSTART              SEGMENT CODE INSECTION0

;-----------------------------------------------------------------------------
; External symbols
;-----------------------------------------------------------------------------

    ; The "?C?InitData" routine is responsible for initializing the global
    ; variables. If your application does not use global initialized variables,
    ; it will not be used.
    ; The routine is available from the standard library used with your model,
    ; for instance RCST7.LIB
    EXTRN   CODE (?C?InitData)

    ; The "?C?InitDataWBit" routine is responsible for initializing the global
    ; variables including BIT initilizations. If your application does not use
    ; global initialized BIT variables, prefer the ?C?InitData routine above,
    ; which has a smaller footprint.
    EXTRN   CODE (?C?InitDataWBit)

    ; The "main" routine is the main entry point for the user application
    EXTRN   CODE (main)


    RSG_BTM1_GETFLAG    SEGMENT BIT

;---------------------------------------------------------------------------
; Flag for getchar/ungetchar SCI buffer full bit
; If you do not use the init_sci routine below, you can remove this segment.
;---------------------------------------------------------------------------
    RSEG                RSG_BTM1_GETFLAG

FULLB_SCI:
    DBIT    1     ; Read bit indicates whether the buffer has been read


    RSEG                CSTART

;-----------------------------------------------------------------------------
; Public symbols
; The symbol CST7_START is the reset entry point for the RLST7 linker.
; Several variants of this entry point may be required by your projects,
; depending of your project requirements.
; It is recommended that all these startup variants are defined as public
; symbols here.
;-----------------------------------------------------------------------------

    PUBLIC  CST7_START                 ; Called fo bare startup
    PUBLIC  CST7_START_V               ; Startup with global variables initialization
    PUBLIC  CST7_START_VB              ; Startup with global variables and bit initialization


;=============================================================================
; Note that the _?init_sci? routine found in early versions of the library
; does *not* exist anymore. The putchar function now has the responsability
; for SCI peripheral initialization. Please refer to your putchar()
; documentation (putchar assembly source code is given along with the library).
;=============================================================================
 This routine initializes the SCI for your device.
;
; Note that the constants described herein are for the ST72321/ST72325 family
; of devices. In case your project uses a different controller, you will have
; to rewrite these constants according to your device data sheet.
; The SCI is setup so that the baud rate is 9600 bps at 12 MHz.
;
; The whole routine can be removed from your startup if you do not use SCI
; functions such as putchar, getchar, puts, printf or scanf.
;=============================================================================

    ; Symbols for SCI registers
    SCISR    DATA   050h        ; SCI Status Register
    SCIDR    DATA   051h        ; SCI Data Register
    SCIBRR   DATA   052h        ; SCI Baud Rate Register
    SCICR1   DATA   053h        ; SCI Control Register 1
    SCICR2   DATA   054h        ; SCI Control Register 2
    SCIERPR  DATA   055h        ; SCI Receive Prescaler
    SCIETPR  DATA   057h        ; SCI Transmit Prescaler
    EXT_SCI  EQU    1           ; Indicates SCIERPR and SCIETPR registers are available
    BDRATDIV EQU    000h        ; Baud rate divisor (depends on the CPU frequency)
    EXTBDRAT EQU    027h        ; Extended baud rate divisor

    ; Serial Communications Interface Control Register 1 bit definitions
    R8          EQU     7       ; Receive data bit 8
    T8          EQU     6       ; Transmit data bit 8
    SCID        EQU     5       ; Disabled for low power consumption
    M           EQU     4       ; Word length
    WAKE        EQU     3       ; Wake-Up method
    PCE         EQU     2       ; Parity control enable
    PS          EQU     1       ; Parity selection
    PIE         EQU     0       ; Parity interrupt enable

    ; Serial Communications Interface Control Register 2 bit definitions
    TIE         EQU     7       ; Transmitter interrupt enable.
    TCIE        EQU     6       ; Transmission complete interrupt enable
    RIE         EQU     5       ; Receiver interrupt enable.
    ILIE        EQU     4       ; Idle line interrupt enable.
    TE          EQU     3       ; Transmitter enable.
    RE          EQU     2       ; Receiver enable.
    RWU         EQU     1       ; Receiver wake-up.
    SBK         EQU     0       ; Send break.

    ; SCISR bit definitions
    TDRE        EQU     7       ; Transmit data register empty
    TC          EQU     6       ; Transmission complete
    RDRF        EQU     5       ; Received data ready flag
    IDLE        EQU     4       ; Idle line detect
    OERR        EQU     3       ; Overrun error
    NF          EQU     2       ; Noise flag
    FE          EQU     1       ; Framing error
    PE          EQU     0       ; Parity error


_?init_sci?:
    ; Load Default value into control registers
    CLR     SCICR1
    CLR     SCICR2

    ; Clear all the flags in Status register (8 bits data, no parity, no HW control)
    LD      A, SCISR
    LD      A, SCIDR
    CLR     SCIDR

    ; Set baudrate to 9600 bauds @ 6 MHz (Half REva oscillator, which is 12 MHz)
    LD      A, #BDRATDIV
    LD      SCIBRR, A

$IF(EXT_SCI)
    LD      A, #EXTBDRAT
    LD      SCIERPR, A
    LD      SCIETPR, A
$ENDIF

;---------------------------------------------------------------------------
; Specific SCI settings. Uncomment/modify the lines for appropriate settings.
;   ; Words will be 9 bits long (8+parity bit)
;   BSET    SCICR1, #M
;
;   ; Parity control enabled
;   BSET    SCICR1, #PCE
;---------------------------------------------------------------------------

    ; Enable the SCI
    BSET    SCICR2, #TE
    BSET    SCICR2, #RE

    ; Specifies that no char is in the getchar/ungetchar buffer yet
    BRES    FULLB_SCI

    RET


;=============================================================================
; Reset entry point. This is the first piece of code to get called upon
; RESET conditions
;=============================================================================

CST7_START:
CST7_START_S:
CST7_START_V:
CST7_START_S_V:
CST7_START_VB:
CST7_START_S_VB:

; Remove next line if your project does not require SCI initialization
    CALL    _?init_sci?                ; Initializing SCI

; Remove the following line if your project does not use any C global variables
; Initialization.
; Replace ?C?InitDataWBit by ?C?InitData if you require some global variables
; initialization but no BIT initialization.
    CALL    ?C?InitDataWBit            ; Initialize global variables (with bits)

    JP      main                       ; Call the user entry point (main)


;=============================================================================
; Interrupt vector table.
;=============================================================================

    CSEG AT 0FFFEh

    dw      CST7_START                 ; RESET vector

END
