]> git.sur5r.net Git - cc65/blobdiff - libsrc/atari/crt0.s
Fixed _textcolor definition.
[cc65] / libsrc / atari / crt0.s
index 9bf5771d46accb386ea5ed97e2ed5b755eaa449c..97b7d7e95df3f344e66f5047770ecda27f1a0750 100644 (file)
@@ -8,19 +8,20 @@
 ;       Stefan Haubenthal
 ;
 
-        .export         _exit
         .export         __STARTUP__ : absolute = 1      ; Mark as startup
+        .export         _exit, start, excexit, SP_save
 
         .import         initlib, donelib
         .import         callmain, zerobss
-        .import         __STARTUP_LOAD__, __BSS_LOAD__
         .import         __RESERVED_MEMORY__
-        .import         __RAM_START__, __RAM_SIZE__
-.if .defined(__ATARIXL__)
+        .import         __MAIN_START__, __MAIN_SIZE__
+        .import         __LOWCODE_RUN__, __LOWCODE_SIZE__
+.ifdef __ATARIXL__
         .import         __STACKSIZE__
         .import         sram_init
         .import         scrdev
         .import         findfreeiocb
+        .forceimport    sramprep                        ; force inclusion of the "shadow RAM preparation" load chunk
         .include        "save_area.inc"
 .endif
 
         .include        "atari.inc"
 
 ; ------------------------------------------------------------------------
-; EXE header
-
-.segment        "EXEHDR"
-
-        .word   $FFFF
-
-.if .defined(__ATARIXL__)
-.segment        "MAINHDR"
-.endif
-
-        .word   __STARTUP_LOAD__
-        .word   __BSS_LOAD__ - 1
-
-; ------------------------------------------------------------------------
-; Actual code
 
 .segment        "STARTUP"
 
         rts     ; fix for SpartaDOS / OS/A+
-                ; they first call the entry point from AUTOSTRT and
-                ; then the load addess (this rts here).
+                ; 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:
 
-.if .defined(__ATARIXL__)
+.ifdef __ATARIXL__
         jsr     sram_init
 .endif
 
-; Clear the BSS data
+; Clear the BSS data.
 
         jsr     zerobss
 
-; Setup the stack
+; Set up the stack.
 
         tsx
-        stx     spsave
+        stx     SP_save
+
+.ifdef __ATARIXL__
+
+        lda     #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+        ldx     #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+        sta     sp
+        stx     sp+1
 
-.if .not .defined(__ATARIXL__)
+.else
 
-; Report memory usage
+; Report the memory usage.
 
         lda     APPMHI
+        ldx     APPMHI+1
         sta     APPMHI_save             ; remember old APPMHI value
-        lda     APPMHI+1
-        sta     APPMHI_save+1
+        stx     APPMHI_save+1
 
         sec
         lda     MEMTOP
         sbc     #<__RESERVED_MEMORY__
         sta     APPMHI                  ; initialize our APPMHI value
-        sta     sp                      ; setup runtime stack part 1
+        sta     sp                      ; set up runtime stack part 1
         lda     MEMTOP+1
         sbc     #>__RESERVED_MEMORY__
         sta     APPMHI+1
-        sta     sp+1                    ; setup runtime stack part 2
-
-.else
-
-        lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
-        sta     sp
-        lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
-        sta     sp+1
+        sta     sp+1                    ; set up runtime stack part 2
 
 .endif
 
-; Call module constructors
+; Call the module constructors.
 
         jsr     initlib
 
-.if 0
-.if .defined(__ATARIXL__)
-        .import __heapadd
-        .import pushax
-        .import __RAM_BELOW_ROM_START__
-        .import __RAM_BELOW_ROM_SIZE__
-        .import __RAM_BELOW_ROM_LAST__
-
-        lda     #<__RAM_BELOW_ROM_LAST__
-        ldx     #>__RAM_BELOW_ROM_LAST__
-        jsr     pushax
-        lda     #<(__RAM_BELOW_ROM_SIZE__ - (__RAM_BELOW_ROM_LAST__ - __RAM_BELOW_ROM_START__))
-        ldx     #>(__RAM_BELOW_ROM_SIZE__ - (__RAM_BELOW_ROM_LAST__ - __RAM_BELOW_ROM_START__))
-        jsr     __heapadd
-.endif
-.endif
-
-; Set left margin to 0
+; Set the left margin to 0.
 
         lda     LMARGN
-        sta     old_lmargin
+        sta     LMARGN_save
         ldy     #0
         sty     LMARGN
 
-; Set keyb to upper/lowercase mode
+; Set the keyboard to upper-/lower-case mode.
 
         ldx     SHFLOK
-        stx     old_shflok
+        stx     SHFLOK_save
         sty     SHFLOK
 
-; Initialize conio stuff
+; Initialize the conio stuff.
 
-        dey                             ; Set X to $FF
-        sty     CH
+        dey                     ; Set Y to $FF
+        sty     CH              ; remove keypress which might be in the input buffer
 
-; Push arguments and call main
+; Push the command-line arguments; and, call main().
 
         jsr     callmain
 
-; Call module destructors. This is also the _exit entry.
+; Call the module destructors. This is also the exit() entry.
 
-_exit:  jsr     donelib         ; Run module destructors
+_exit:  ldx     SP_save
+        txs                     ; Restore stack pointer
 
-; Restore system stuff
+; Restore the system stuff.
 
-        ldx     spsave
-        txs                     ; Restore stack pointer
+excexit:jsr     donelib         ; Run module destructors; 'excexit' is called from the exec routine
 
-; Restore left margin
+; Restore the left margin.
 
-        lda     old_lmargin
+        lda     LMARGN_save
         sta     LMARGN
 
-; Restore kb mode
+; Restore the kb mode.
 
-        lda     old_shflok
+        lda     SHFLOK_save
         sta     SHFLOK
 
-; Restore APPMHI
+; Restore APPMHI.
 
         lda     APPMHI_save
+        ldx     APPMHI_save+1
         sta     APPMHI
-        lda     APPMHI_save+1
-        sta     APPMHI+1
+        stx     APPMHI+1
 
-.if .defined(__ATARIXL__)
+.ifdef __ATARIXL__
 
 ; Atari XL target stuff...
 
@@ -174,12 +143,14 @@ _exit:  jsr     donelib         ; Run module destructors
         lda     RAMTOP_save
         sta     RAMTOP
         lda     MEMTOP_save
+        ldx     MEMTOP_save+1
         sta     MEMTOP
-        lda     MEMTOP_save+1
-        sta     MEMTOP+1
+        stx     MEMTOP+1
 
 
-; ... issue a GRAPHICS 0 call (copied'n'pasted from TGI drivers)
+; Issue a GRAPHICS 0 call (copied'n'pasted from the TGI drivers), in
+; order to restore screen memory to its default location just
+; before the ROM.
 
         jsr     findfreeiocb
 
@@ -199,19 +170,21 @@ _exit:  jsr     donelib         ; Run module destructors
         lda     #0
         sta     ICBLH,x
         jsr     CIOV_org
-; add error checking here...
+; No error checking here, shouldn't happen(TM); and, no way to
+; recover anyway.
+
         lda     #CLOSE
         sta     ICCOM,x
         jsr     CIOV_org
 
 .endif
 
-; Turn on cursor
+; Turn on the cursor.
 
         ldx     #0
         stx     CRSINH
 
-; Back to DOS
+; Back to DOS.
 
         rts
 
@@ -221,15 +194,17 @@ _exit:  jsr     donelib         ; Run module destructors
 
 .bss
 
-spsave:         .res    1
-old_shflok:     .res    1
-old_lmargin:    .res    1
-.if .not .defined(__ATARIXL__)
+SP_save:        .res    1
+SHFLOK_save:    .res    1
+LMARGN_save:    .res    1
+.ifndef __ATARIXL__
 APPMHI_save:    .res    2
 .endif
 
+; ------------------------------------------------------------------------
 
-.segment "AUTOSTRT"
-        .word   RUNAD                   ; defined in atari.inc
-        .word   RUNAD+1
-        .word   start
+.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