]> git.sur5r.net Git - cc65/blobdiff - libsrc/apple2/crt0.s
Fix
[cc65] / libsrc / apple2 / crt0.s
index f061b212be71f58ab4d0712924103ccf37a691e4..60a8516d1f9188f4a1d0c3b956817106f5e9014d 100644 (file)
@@ -6,16 +6,17 @@
 
         .export         _exit, done, return
         .export         __STARTUP__ : absolute = 1      ; Mark as startup
-        .import         zerobss
+
         .import         initlib, donelib
-        .import         callmain
+        .import         zerobss, callmain
+        .import         __ONCE_LOAD__, __ONCE_SIZE__    ; Linker generated
         .import         __LC_START__, __LC_LAST__       ; Linker generated
-        .import         __INIT_RUN__, __INIT_SIZE__     ; Linker generated
-        .import         __INITBSS_RUN__                 ; Linker generated
 
         .include        "zeropage.inc"
         .include        "apple2.inc"
 
+; ------------------------------------------------------------------------
+
         .segment        "STARTUP"
 
         ; ProDOS TechRefMan, chapter 5.2.1:
         ldx     #$FF
         txs                     ; Init stack pointer
 
-        ; Switch in LC bank 2 for W/O.
-        bit     $C081
-        bit     $C081
-
-        ; Set the source start address.
-        lda     #<(__INITBSS_RUN__ + __INIT_SIZE__)
-        ldy     #>(__INITBSS_RUN__ + __INIT_SIZE__)
-        sta     $9B
-        sty     $9C
-
-        ; Set the source last address.
-        lda     #<(__INITBSS_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
-        ldy     #>(__INITBSS_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
-        sta     $96
-        sty     $97
-
-        ; Set the destination last address.
-        lda     #<__LC_LAST__
-        ldy     #>__LC_LAST__
-        sta     $94
-        sty     $95
-
-        ; Call into Applesoft Block Transfer Up -- which handles zero-
-        ; sized blocks well -- to move the content of the LC memory area.
-        jsr     $D39A           ; BLTU2
-
-        ; Set the source start address.
-        lda     #<__INITBSS_RUN__
-        ldy     #>__INITBSS_RUN__
-        sta     $9B
-        sty     $9C
-
-        ; Set the source last address.
-        lda     #<(__INITBSS_RUN__ + __INIT_SIZE__)
-        ldy     #>(__INITBSS_RUN__ + __INIT_SIZE__)
-        sta     $96
-        sty     $97
-
-        ; Set the destination last address.
-        lda     #<(__INIT_RUN__ + __INIT_SIZE__)
-        ldy     #>(__INIT_RUN__ + __INIT_SIZE__)
-        sta     $94
-        sty     $95
+        ; Save space by putting some of the start-up code in the ONCE segment,
+        ; which can be re-used by the BSS segment, the heap and the C stack.
+        jsr     init
 
-        ; Call into Applesoft Block Transfer Up -- which handles moving
-        ; overlapping blocks upwards well -- to move the INIT segment.
-        jsr     $D39A           ; BLTU2
+        ; Clear the BSS data.
+        jsr     zerobss
 
-        ; Delegate all further processing, to keep the STARTUP segment small.
-        jsr     init
+        ; Push the command-line arguments; and, call main().
+        jsr     callmain
 
         ; Avoid a re-entrance of donelib. This is also the exit() entry.
 _exit:  ldx     #<exit
@@ -109,7 +69,9 @@ exit:   ldx     #$02
         ; We're done
         jmp     done
 
-        .segment        "INIT"
+; ------------------------------------------------------------------------
+
+        .segment        "ONCE"
 
         ; Save the zero-page locations that we need.
 init:   ldx     #zpspace-1
@@ -118,9 +80,6 @@ init:   ldx     #zpspace-1
         dex
         bpl     :-
 
-        ; Clear the BSS data.
-        jsr     zerobss
-
         ; Save the original RESET vector.
         ldx     #$02
 :       lda     SOFTEV,x
@@ -128,13 +87,6 @@ init:   ldx     #zpspace-1
         dex
         bpl     :-
 
-        ; ProDOS TechRefMan, chapter 5.3.5:
-        ; "Your system program should place in the RESET vector the
-        ;  address of a routine that ... closes the files."
-        ldx     #<_exit
-        lda     #>_exit
-        jsr     reset           ; Setup RESET vector
-
         ; Check for ProDOS.
         ldy     $BF00           ; MLI call entry point
         cpy     #$4C            ; Is MLI present? (JMP opcode)
@@ -164,14 +116,50 @@ basic:  lda     HIMEM
 :       sta     sp
         stx     sp+1
 
+        ; ProDOS TechRefMan, chapter 5.3.5:
+        ; "Your system program should place in the RESET vector the
+        ;  address of a routine that ... closes the files."
+        ldx     #<_exit
+        lda     #>_exit
+        jsr     reset           ; Setup RESET vector
+
         ; Call the module constructors.
         jsr     initlib
 
-        ; Switch in LC bank 2 for R/O.
+        ; Switch in LC bank 2 for W/O.
+        bit     $C081
+        bit     $C081
+
+        ; Set the source start address.
+        ; Aka __LC_LOAD__ iff segment LC exists.
+        lda     #<(__ONCE_LOAD__ + __ONCE_SIZE__)
+        ldy     #>(__ONCE_LOAD__ + __ONCE_SIZE__)
+        sta     $9B
+        sty     $9C
+
+        ; Set the source last address.
+        ; Aka __LC_LOAD__ + __LC_SIZE__ iff segment LC exists.
+        lda     #<((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
+        ldy     #>((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
+        sta     $96
+        sty     $97
+
+        ; Set the destination last address.
+        ; Aka __LC_RUN__ + __LC_SIZE__ iff segment LC exists.
+        lda     #<__LC_LAST__
+        ldy     #>__LC_LAST__
+        sta     $94
+        sty     $95
+
+        ; Call into Applesoft Block Transfer Up -- which handles zero-
+        ; sized blocks well -- to move the content of the LC memory area.
+        jsr     $D39A           ; BLTU2
+
+        ; Switch in LC bank 2 for R/O and return.
         bit     $C080
+        rts
 
-        ; Push the command-line arguments; and, call main().
-        jmp     callmain
+; ------------------------------------------------------------------------
 
         .code
 
@@ -187,6 +175,8 @@ quit:   jsr     $BF00           ; MLI call entry point
         .byte   $65             ; Quit
         .word   q_param
 
+; ------------------------------------------------------------------------
+
         .rodata
 
         ; MLI parameter list for quit
@@ -196,15 +186,16 @@ q_param:.byte   $04             ; param_count
         .byte   $00             ; reserved
         .word   $0000           ; reserved
 
+; ------------------------------------------------------------------------
+
         .data
 
         ; Final jump when we're done
 done:   jmp     DOSWARM         ; Potentially patched at runtime
 
-        .segment        "INITBSS"
-
-zpsave: .res    zpspace
+; ------------------------------------------------------------------------
 
-        .bss
+        .segment        "INIT"
 
+zpsave: .res    zpspace
 rvsave: .res    3