;
; Startup code for cc65 (ATARI version)
;
; Contributing authors:
;       Mark Keates
;       Freddy Offenga
;       Christian Groessler
;       Stefan Haubenthal
;

; Hacked up, trimmed down version for fnchat.

        .export         __STARTUP__ : absolute = 1      ; Mark as startup
        .export         _exit, start

        .import         _main;  not any more: , zerobss
        .import         __RESERVED_MEMORY__
        .import         __MAIN_START__, __MAIN_SIZE__
        .import         __LOWCODE_RUN__, __LOWCODE_SIZE__
        .import         __BSS_RUN__, __BSS_SIZE__
        .import         _scr_init, _init_channels
        .importzp       ptr1
        .include        "zeropage.inc"
        .include        "atari.inc"

; ------------------------------------------------------------------------

.segment        "STARTUP"
 _exit = WARMSV ; nothing should be calling exit() anyway.

        rts     ; fix for SpartaDOS / OS/A+
                ; They first call the entry point from AUTOSTRT; and
                ; then, the load address (this rts here).
                ; We point AUTOSTRT directly after the rts.

; Real entry point:

start:

; Clear the BSS data.
; This routine was copied from libsrc/common/zerobss.s.
;zerobss:
        lda     #<__BSS_RUN__
        sta     ptr1
        lda     #>__BSS_RUN__
        sta     ptr1+1
        lda     #0
        tay

; Clear full pages

L1:     ldx     #>__BSS_SIZE__
        beq     L3
L2:     sta     (ptr1),y
        iny
        bne     L2
        inc     ptr1+1
        dex
        bne     L2

; Clear remaining page (y is zero on entry)

L3:     cpy     #<__BSS_SIZE__
        beq     L4
        sta     (ptr1),y
        iny
        bne     L3

L4:
; Done clearing BSS.

; Set up the stack
        lda MEMTOP
        sta sp
        lda MEMTOP+1
        sta sp+1

; Set various OS variables.
        ldy     #$ff
        sty     CH              ; remove keypress which might be in the input buffer
        iny                     ; Set Y to 0
        sty     SHFLOK          ; Turn off Caps Lock
        sty     SOUNDR          ; Turn off SIO beep/click

; These 2 used to be called from main(). Moving the calls here saves 6 bytes.
; Do not call _edbox_clear from here (we're *running* in the edbox area).
       jsr _scr_init
       jsr _init_channels

; Go directly to main(). No support for CLI arguments, no saving
; any machine state for a clean exit, because we don't exit.
       jmp _main

; ------------------------------------------------------------------------

.segment "LOWCODE"       ; have at least one (empty) segment of LOWCODE, so that the next line works even if the program doesn't make use of this segment
.assert (__LOWCODE_RUN__ + __LOWCODE_SIZE__ <= $4000 || __LOWCODE_RUN__ > $7FFF || __LOWCODE_SIZE__ = 0), warning, "'lowcode area' reaches into $4000..$7FFF bank memory window"
; check for LOWBSS_SIZE = 0 not needed since the only file which uses LOWBSS (irq.s) also uses LOWCODE
; check for LOWCODE_RUN > $7FFF is mostly for cartridges, where this segment is loaded high (into cart ROM)
; there is a small chance that if the user loads the program really high, LOWCODE is above $7FFF, but LOWBSS is below -- no warning emitted in this case
