; Startup code for cc65 (ATARI version)
;
; Contributing authors:
-; Mark Keates
-; Freddy Offenga
-; Christian Groessler
+; Mark Keates
+; Freddy Offenga
+; Christian Groessler
+; Stefan Haubenthal
;
- .export _exit
.export __STARTUP__ : absolute = 1 ; Mark as startup
- .constructor initsp, 26
-
- .import initlib, donelib, callmain
- .import zerobss, pushax
- .import _main, __filetab, getfd
- .import __STARTUP_LOAD__, __ZPSAVE_LOAD__
- .import __RESERVED_MEMORY__
+ .export _exit, start
+
+ .import initlib, donelib
+ .import callmain, zerobss
+ .import __RESERVED_MEMORY__
+ .import __RAM_START__, __RAM_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"
-
-; ------------------------------------------------------------------------
-; EXE header
-
- .segment "EXEHDR"
- .word $FFFF
- .word __STARTUP_LOAD__
- .word __ZPSAVE_LOAD__ - 1
+ .include "atari.inc"
; ------------------------------------------------------------------------
-; Actual code
- .segment "STARTUP"
+.segment "STARTUP"
- 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.
+ 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.
; Real entry point:
-; Save the zero page locations we need
+start:
- ldx #zpspace-1
-L1: lda sp,x
- sta zpsave,x
- dex
- bpl L1
+.ifdef __ATARIXL__
+ jsr sram_init
+.endif
; Clear the BSS data
- jsr zerobss
+ jsr zerobss
+
+; Setup the stack
+
+ tsx
+ stx SP_save
+
+.ifdef __ATARIXL__
+
+ lda #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+ sta sp
+ lda #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+ sta sp+1
-; setup the stack
+.else
- tsx
- stx spsave
+; Report memory usage
-; report memory usage
+ lda APPMHI
+ sta APPMHI_save ; remember old APPMHI value
+ lda APPMHI+1
+ sta APPMHI_save+1
- lda APPMHI
- sta appmsav ; remember old APPMHI value
- lda APPMHI+1
- sta appmsav+1
+ sec
+ lda MEMTOP
+ sbc #<__RESERVED_MEMORY__
+ sta APPMHI ; initialize our APPMHI value
+ sta sp ; setup runtime stack part 1
+ lda MEMTOP+1
+ sbc #>__RESERVED_MEMORY__
+ sta APPMHI+1
+ sta sp+1 ; setup runtime stack part 2
- sec
- lda MEMTOP
- sbc #<__RESERVED_MEMORY__
- sta APPMHI ; initialize our APPMHI value
- lda MEMTOP+1
- sbc #>__RESERVED_MEMORY__
- sta APPMHI+1
+.endif
; Call module constructors
- jsr initlib
+ jsr initlib
-; set left margin to 0
+; Set left margin to 0
- lda LMARGN
- sta old_lmargin
- ldy #0
- sty LMARGN
+ lda LMARGN
+ sta LMARGN_save
+ ldy #0
+ sty LMARGN
-; set keyb to upper/lowercase mode
+; Set keyb to upper/lowercase mode
- ldx SHFLOK
- stx old_shflok
- sty SHFLOK
+ ldx SHFLOK
+ stx SHFLOK_save
+ sty SHFLOK
; Initialize 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
- jsr callmain
+ jsr callmain
; Call module destructors. This is also the _exit entry.
-_exit: jsr donelib ; Run module destructors
+_exit: jsr donelib ; Run module destructors
; Restore system stuff
- ldx spsave
- txs ; Restore stack pointer
+ ldx SP_save
+ txs ; Restore stack pointer
-; restore left margin
+; Restore left margin
- lda old_lmargin
- sta LMARGN
+ lda LMARGN_save
+ sta LMARGN
-; restore kb mode
+; Restore kb mode
- lda old_shflok
- sta SHFLOK
+ lda SHFLOK_save
+ sta SHFLOK
-; restore APPMHI
+; Restore APPMHI
- lda appmsav
- sta APPMHI
- lda appmsav+1
- sta APPMHI+1
+ lda APPMHI_save
+ sta APPMHI
+ lda APPMHI_save+1
+ sta APPMHI+1
-; Copy back the zero page stuff
+.ifdef __ATARIXL__
- ldx #zpspace-1
-L2: lda zpsave,x
- sta sp,x
- dex
- bpl L2
+; Atari XL target stuff...
-; turn on cursor
+ lda PORTB_save
+ sta PORTB
+ lda RAMTOP_save
+ sta RAMTOP
+ lda MEMTOP_save
+ sta MEMTOP
+ lda MEMTOP_save+1
+ sta MEMTOP+1
- inx
- stx CRSINH
-; Back to DOS
+; Issue a GRAPHICS 0 call (copied'n'pasted from TGI drivers) in
+; order to restore screen memory to its defailt location just
+; before the ROM.
- rts
+ jsr findfreeiocb
-; *** end of main startup code
+ ; 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.
-; setup sp
+ lda #CLOSE
+ sta ICCOM,x
+ jsr CIOV_org
-.segment "INIT"
+.endif
-initsp:
- lda APPMHI
- sta sp
- lda APPMHI+1
- sta sp+1
- rts
+; Turn on cursor
-.segment "ZPSAVE"
+ ldx #0
+ stx CRSINH
+
+; Back to DOS
-zpsave: .res zpspace
+ rts
- .bss
+; *** end of main startup code
+
+; ------------------------------------------------------------------------
-spsave: .res 1
-appmsav: .res 1
-old_shflok: .res 1
-old_lmargin: .res 1
+.bss
- .segment "AUTOSTRT"
- .word RUNAD ; defined in atari.h
- .word RUNAD+1
- .word __STARTUP_LOAD__ + 1
+SP_save: .res 1
+SHFLOK_save: .res 1
+LMARGN_save: .res 1
+.ifndef __ATARIXL__
+APPMHI_save: .res 2
+.endif