; 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
-;
-
-RESERVE_MOUSE_MEMORY = 1 ; for P/M
-.ifdef RESERVE_MOUSE_MEMORY
- .export mouse_pm0
+ .export __STARTUP__ : absolute = 1 ; Mark as startup
+ .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
- .export _exit
- .import getargs, argc, argv
- .import __hinit, initconio, zerobss, pushax, doatexit
- .import _main,__filetab,getfd
- .import __CODE_LOAD__, __BSS_LOAD__
- .import __graphmode_used
- .include "atari.inc"
+ .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 addess (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
+; Setup 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 #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+ sta sp
+ lda #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+ sta sp+1
-; Save the zero page locations we need
+.else
- ldx #zpspace-1
-L1: lda sp,x
- sta zpsave,x
- dex
- bpl L1
+; Report 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 ; setup runtime stack part 1
+ lda MEMTOP+1
+ sbc #>__RESERVED_MEMORY__
+ sta APPMHI+1
+ sta sp+1 ; setup runtime stack part 2
-; setup the stack
+.endif
- tsx
- stx spsave
+; Call module constructors
-; report memory usage and initialize stack pointer
+ jsr initlib
- lda APPMHI
- sta appmsav
- lda APPMHI+1
- sta appmsav+1
+; Set left margin to 0
- jsr getmemtop ; adjust for graphics mode to use
+ lda LMARGN
+ sta LMARGN_save
+ ldy #0
+ sty LMARGN
- sta sp
- sta APPMHI
- stx sp+1 ; Set argument stack ptr
- stx APPMHI+1
+; Set keyb to upper/lowercase mode
-; set left margin to 0
+ ldx SHFLOK
+ stx SHFLOK_save
+ sty SHFLOK
- lda LMARGN
- sta old_lmargin
- lda #0
- sta LMARGN
+; Initialize conio stuff
-; set keyb to upper/lowercase mode
+ dey ; Set Y to $FF
+ sty CH ; remove keypress which might be in the input buffer
- ldx SHFLOK
- stx old_shflok
- sta SHFLOK
+; Push arguments and call main
-; Initialize the heap
+ jsr callmain
- jsr __hinit
+; Call module destructors. This is also the _exit entry.
-; Initialize conio stuff
+_exit: jsr donelib ; Run module destructors
- jsr initconio
+; Restore system stuff
- lda #$FF
- sta CH
+ ldx SP_save
+ txs ; Restore stack pointer
-; set stdio stream handles
+; Restore left margin
- 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
+ lda LMARGN_save
+ sta LMARGN
-; Pass command line if present
+; Restore kb mode
- jsr getargs
+ lda SHFLOK_save
+ sta SHFLOK
- lda argc
- ldx argc+1
- jsr pushax ; argc
- lda #<argv
- ldx #>argv
- jsr pushax ; argv
+; Restore APPMHI
- ldy #4 ; Argument size
- jsr _main ; call the users code
+ lda APPMHI_save
+ sta APPMHI
+ lda APPMHI_save+1
+ sta APPMHI+1
-; fall thru to exit...
+.ifdef __ATARIXL__
-_exit: jsr doatexit ; call exit functions
+; Atari XL target stuff...
- ldx spsave
- txs ; Restore stack pointer
+ lda PORTB_save
+ sta PORTB
+ lda RAMTOP_save
+ sta RAMTOP
+ lda MEMTOP_save
+ sta MEMTOP
+ lda MEMTOP_save+1
+ sta MEMTOP+1
-; restore left margin
- lda old_lmargin
- sta LMARGN
+; 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.
-; restore kb mode
+ jsr findfreeiocb
- lda old_shflok
- sta SHFLOK
+ ; 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.
-; restore APPMHI
+ lda #CLOSE
+ sta ICCOM,x
+ jsr CIOV_org
- lda appmsav
- sta APPMHI
- lda appmsav+1
- sta APPMHI+1
+.endif
-; Copy back the zero page stuff
+; Turn on cursor
- ldx #zpspace-1
-L2: lda zpsave,x
- sta sp,x
- dex
- bpl L2
+ ldx #0
+ stx CRSINH
; Back to DOS
- rts
+ rts
; *** end of main startup code
+; ------------------------------------------------------------------------
-; calc. upper memory limit to use
-
-.proc getmemtop
-
- ldy __graphmode_used
- beq ignore ; mode 0 doesn't need adjustment
- cpy #32
- bcs ignore ; invalid value
-
- tya
- asl a
- tay
- lda MEMTOP
- sec
- sbc grmemusage,y
- pha
- lda MEMTOP+1
- sbc grmemusage+1,y
- tax
- pla
-.ifdef RESERVE_MOUSE_MEMORY
-
-adj_mouse:
- txa ; get upper byte of address
- and #%11111000 ; make 2k aligned
- sec
- sbc #%00001000 ; reserve 2k
- tax
- adc #3 ; add 4 (C = 1)
- sta mouse_pm0
- lda #0
-.endif
- rts
-
-ignore: lda MEMTOP
- ldx MEMTOP+1
-.ifdef RESERVE_MOUSE_MEMORY
- bne adj_mouse
-.else
- rts
-.endif
+.bss
-.endproc
-
- .data
-
-zpsave: .res zpspace
-
- .rodata
-
-; memory usage of the different graphics modes (0-31)
-; values < 0 of "bytes needed" are mappped to 0
-; bytes needed ; mode ; val. of MEMTOP
-grmemusage:
- .word 0 ; 0 ; 39967
- .word 0 ;-318 ; 1 ; 40285
- .word 0 ;-568 ; 2 ; 40535
- .word 0 ;-558 ; 3 ; 40525
- .word 0 ;-298 ; 4 ; 40265
- .word 182 ; 5 ; 39785
- .word 1182 ; 6 ; 38785
- .word 3198 ; 7 ; 36769
- .word 7120 ; 8 ; 32847
- .word 7146 ; 9 ; 32821
- .word 7146 ; 10 ; 32821
- .word 7146 ; 11 ; 32821
- .word 162 ; 12 ; 39805
- .word 0 ;-328 ; 13 ; 40295
- .word 3278 ; 14 ; 36689
- .word 7120 ; 15 ; 32847
- .word 0 ; 16 ; 39967
- .word 0 ;-320 ; 17 ; 40287
- .word 0 ;-572 ; 18 ; 40539
- .word 0 ;-560 ; 19 ; 40527
- .word 0 ;-296 ; 20 ; 40263
- .word 184 ; 21 ; 39783
- .word 1192 ; 22 ; 38775
- .word 3208 ; 23 ; 36759
- .word 7146 ; 24 ; 32821
- .word 7146 ; 25 ; 32821
- .word 7146 ; 26 ; 32821
- .word 7146 ; 27 ; 32821
- .word 160 ; 28 ; 39807
- .word 0 ;-332 ; 29 ; 40299
- .word 3304 ; 30 ; 36663
- .word 7146 ; 31 ; 32821
-
-; the program used to get these values (Atari BASIC):
-; 100 FILE=0
-; 110 IF FILE=1 THEN OPEN #1,8,0,"D:FREEMEM.OUT"
-; 120 IF FILE<>1 THEN OPEN #1,8,0,"E:"
-; 200 DIM G(32)
-; 210 FOR I=0 TO 32:GRAPHICS I:GOSUB 1000:G(I)=VAL:NEXT I
-; 220 GRAPHICS 0
-; 230 FOR I=0 TO 31:PRINT #1;I;":",G(I);" - ";G(0)-G(I):NEXT I
-; 240 CLOSE #1
-; 999 END
-; 1000 VAL=PEEK(741)+256*PEEK(742)
-; 1010 RETURN
-
- .bss
-
-spsave: .res 1
-appmsav: .res 1
-old_shflok: .res 1
-old_lmargin: .res 1
-.ifdef RESERVE_MOUSE_MEMORY
-mouse_pm0: .res 1
+SP_save: .res 1
+SHFLOK_save: .res 1
+LMARGN_save: .res 1
+.ifndef __ATARIXL__
+APPMHI_save: .res 2
.endif
-
- .segment "AUTOSTRT"
- .word $02E0
- .word $02E1
- .word __CODE_LOAD__ + 1