; Startup code for cc65 (ATARI version)
;
; Contributing authors:
-; Mark Keates
-; Freddy Offenga
-; Christian Groessler
+; Mark Keates
+; Freddy Offenga
+; Christian Groessler
+; Stefan Haubenthal
;
-; This must be the *first* file on the linker command line
-;
-
- .export _exit
- .constructor initsp,26
- .import getargs, argc, argv
- .import initlib, donelib
- .import zerobss, pushax
- .import _main,__filetab,getfd
- .import __CODE_LOAD__, __BSS_LOAD__
-
- .include "atari.inc"
+ .export __STARTUP__ : absolute = 1 ; Mark as startup
+ .export _exit, start
+
+ .import initlib, donelib
+ .import callmain, zerobss
+ .import __RESERVED_MEMORY__
+ .import __MAIN_START__, __MAIN_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 "zeropage.inc"
+ .include "atari.inc"
; ------------------------------------------------------------------------
-; Define and export the ZP variables for the runtime
- .exportzp sp, sreg, regsave
- .exportzp ptr1, ptr2, ptr3, ptr4
- .exportzp tmp1, tmp2, tmp3, tmp4
- .exportzp fntemp, regbank, zpspace
+.segment "STARTUP"
+ 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.
-.zeropage
+; Real entry point:
-zpstart = *
-sp: .res 2 ; Stack pointer
-sreg: .res 2 ; Secondary register/high 16 bit for longs
-regsave: .res 2 ; slot to save/restore (E)AX into
-ptr1: .res 2
-ptr2: .res 2
-ptr3: .res 2
-ptr4: .res 2
-tmp1: .res 1
-tmp2: .res 1
-tmp3: .res 1
-tmp4: .res 1
-fntemp: .res 2 ; Pointer to file name
-regbank: .res 6 ; 6 byte register bank
+start:
-zpspace = * - zpstart ; Zero page space allocated
+.ifdef __ATARIXL__
+ jsr sram_init
+.endif
-.code
+; Clear the BSS data.
-; ------------------------------------------------------------------------
-; EXE header
+ jsr zerobss
- .segment "EXEHDR"
- .word $FFFF
- .word __CODE_LOAD__
- .word __BSS_LOAD__ - 1
- .code
- .reloc
+; Set up the stack.
-; ------------------------------------------------------------------------
-; Actual code
+ tsx
+ stx SP_save
- rts ; fix for SpartaDOS / OS/A+
- ; they first call the entry point from AUTOSTRT and
- ; then the load addess (this rts here).
- ; We point AUTOSTRT directly after the rts.
+.ifdef __ATARIXL__
-; Real entry point:
+ lda #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+ ldx #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+ sta sp
+ stx sp+1
-; Save the zero page locations we need
+.else
- ldx #zpspace-1
-L1: lda sp,x
- sta zpsave,x
- dex
- bpl L1
+; Report the memory usage.
-; Clear the BSS data
+ lda APPMHI
+ sta APPMHI_save ; remember old APPMHI value
+ lda APPMHI+1
+ sta APPMHI_save+1
- jsr zerobss
+ sec
+ lda MEMTOP
+ sbc #<__RESERVED_MEMORY__
+ sta APPMHI ; initialize our APPMHI value
+ sta sp ; set up runtime stack part 1
+ lda MEMTOP+1
+ sbc #>__RESERVED_MEMORY__
+ sta APPMHI+1
+ sta sp+1 ; set up runtime stack part 2
-; setup the stack
+.endif
- tsx
- stx spsave
+; Call the module constructors.
-; report memory usage
+ jsr initlib
- lda APPMHI
- sta appmsav ; remember old APPMHI value
- lda APPMHI+1
- sta appmsav+1
+; Set the left margin to 0.
- lda MEMTOP
- sta APPMHI ; initialize our APPMHI value
- ldx MEMTOP+1
- stx APPMHI+1
+ lda LMARGN
+ sta LMARGN_save
+ ldy #0
+ sty LMARGN
-; Call module constructors
+; Set the keyboard to upper-/lower-case mode.
- jsr initlib
+ ldx SHFLOK
+ stx SHFLOK_save
+ sty SHFLOK
-; set left margin to 0
+; Initialize the conio stuff.
- lda LMARGN
- sta old_lmargin
- lda #0
- sta LMARGN
+ dey ; Set Y to $FF
+ sty CH ; remove keypress which might be in the input buffer
-; set keyb to upper/lowercase mode
+; Push the command-line arguments; and, call main().
- ldx SHFLOK
- stx old_shflok
- sta SHFLOK
+ jsr callmain
-; Initialize conio stuff
+; Call the module destructors. This is also the exit() entry.
- lda #$FF
- sta CH
+_exit: jsr donelib ; Run module destructors
-; set stdio stream handles
+; Restore the system stuff.
- lda #0
- jsr getfd
- sta __filetab ; setup stdin
- lda #0
- jsr getfd
- sta __filetab + 2 ; setup stdout
- lda #0
- jsr getfd
- sta __filetab + 4 ; setup stderr
+ ldx SP_save
+ txs ; Restore stack pointer
-; Pass command line if present
+; Restore the left margin.
- jsr getargs
+ lda LMARGN_save
+ sta LMARGN
- lda argc
- ldx argc+1
- jsr pushax ; argc
- lda #<argv
- ldx #>argv
- jsr pushax ; argv
+; Restore the kb mode.
- ldy #4 ; Argument size
- jsr _main ; call the users code
+ lda SHFLOK_save
+ sta SHFLOK
-; Call module destructors. This is also the _exit entry.
+; Restore APPMHI.
-_exit: jsr donelib ; Run module destructors
+ lda APPMHI_save
+ sta APPMHI
+ lda APPMHI_save+1
+ sta APPMHI+1
-; Restore system stuff
+.ifdef __ATARIXL__
- ldx spsave
- txs ; Restore stack pointer
+; Atari XL target stuff...
-; restore left margin
+ lda PORTB_save
+ sta PORTB
+ lda RAMTOP_save
+ sta RAMTOP
+ lda MEMTOP_save
+ sta MEMTOP
+ lda MEMTOP_save+1
+ sta MEMTOP+1
- lda old_lmargin
- sta LMARGN
-; restore kb mode
+; 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.
- lda old_shflok
- sta SHFLOK
+ jsr findfreeiocb
-; restore APPMHI
+ ; Reopen it in Graphics 0
+ lda #OPEN
+ sta ICCOM,x
+ lda #OPNIN | OPNOT
+ sta ICAX1,x
+ lda #0
+ sta ICAX2,x
+ lda #<scrdev
+ sta ICBAL,x
+ lda #>scrdev
+ sta ICBAH,x
+ lda #3
+ sta ICBLL,x
+ lda #0
+ sta ICBLH,x
+ jsr CIOV_org
+; No error checking here, shouldn't happen(TM); and, no way to
+; recover anyway.
- lda appmsav
- sta APPMHI
- lda appmsav+1
- sta APPMHI+1
+ lda #CLOSE
+ sta ICCOM,x
+ jsr CIOV_org
-; Copy back the zero page stuff
+.endif
- ldx #zpspace-1
-L2: lda zpsave,x
- sta sp,x
- dex
- bpl L2
+; Turn on the cursor.
-; Back to DOS
+ ldx #0
+ stx CRSINH
- rts
+; Back to DOS.
-; *** end of main startup code
-
-; setup sp
-
-initsp:
- lda APPMHI
- sta sp
- lda APPMHI+1
- sta sp+1
- rts
+ rts
- .data
-
-zpsave: .res zpspace
+; *** end of main startup code
- .bss
+; ------------------------------------------------------------------------
-spsave: .res 1
-appmsav: .res 1
-old_shflok: .res 1
-old_lmargin: .res 1
+.bss
- .segment "AUTOSTRT"
- .word $02E0
- .word $02E1
- .word __CODE_LOAD__ + 1
+SP_save: .res 1
+SHFLOK_save: .res 1
+LMARGN_save: .res 1
+.ifndef __ATARIXL__
+APPMHI_save: .res 2
+.endif