; Mark Keates
; Freddy Offenga
; Christian Groessler
-;
-; This must be the *first* file on the linker command line
+; Stefan Haubenthal
;
.export _exit
- .constructor initsp,26
+ .export __STARTUP__ : absolute = 1 ; Mark as startup
- .import getargs, argc, argv
.import initlib, donelib
- .import zerobss, pushax
- .import _main,__filetab,getfd
- .import __CODE_LOAD__, __BSS_LOAD__
+ .import callmain, zerobss, callirq
+ .import __INTERRUPTOR_COUNT__
+ .import __STARTUP_LOAD__, __ZPSAVE_LOAD__
+ .import __RESERVED_MEMORY__
+ .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
-
-
-.zeropage
-
-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
-
-zpspace = * - zpstart ; Zero page space allocated
-
-.code
-
; ------------------------------------------------------------------------
; EXE header
.segment "EXEHDR"
.word $FFFF
- .word __CODE_LOAD__
- .word __BSS_LOAD__ - 1
- .code
- .reloc
+ .word __STARTUP_LOAD__
+ .word __ZPSAVE_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).
; Save the zero page locations we need
- ldx #zpspace-1
+ ldx #zpspace-1
L1: lda sp,x
sta zpsave,x
dex
jsr zerobss
-; setup the stack
+; Setup the stack
tsx
stx spsave
-; report memory usage
+; Report memory usage
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
- ldx MEMTOP+1
- stx APPMHI+1
+ sta sp ; setup runtime stack part 1
+ lda MEMTOP+1
+ sbc #>__RESERVED_MEMORY__
+ sta APPMHI+1
+ sta sp+1 ; setup runtime stack part 2
+
+; If we have IRQ functions, chain our stub into the IRQ vector
+
+ lda #<__INTERRUPTOR_COUNT__
+ beq NoIRQ1
+ lda VVBLKI
+ ldx VVBLKI+1
+ sta IRQInd+1
+ stx IRQInd+2
+ lda #6
+ ldy #<IRQStub
+ ldx #>IRQStub
+ jsr SETVBV
; Call module constructors
- jsr initlib
+NoIRQ1: jsr initlib
-; set left margin to 0
+; Set left margin to 0
lda LMARGN
sta old_lmargin
- lda #0
- sta LMARGN
+ ldy #0
+ sty LMARGN
-; set keyb to upper/lowercase mode
+; Set keyb to upper/lowercase mode
ldx SHFLOK
stx old_shflok
- sta SHFLOK
+ sty SHFLOK
; Initialize conio stuff
- lda #$FF
- sta CH
-
-; set stdio stream handles
+ dey ; Set X to $FF
+ sty CH
- 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
+; Push arguments and call main
-; Pass command line if present
-
- jsr getargs
-
- lda argc
- ldx argc+1
- jsr pushax ; argc
- lda #<argv
- ldx #>argv
- jsr pushax ; argv
-
- ldy #4 ; Argument size
- jsr _main ; call the users code
+ jsr callmain
; Call module destructors. This is also the _exit entry.
_exit: jsr donelib ; Run module destructors
+; Reset the IRQ vector if we chained it.
+
+ pha ; Save the return code on stack
+ lda #<__INTERRUPTOR_COUNT__
+ beq NoIRQ2
+ lda #6
+ ldy IRQInd+1
+ ldx IRQInd+2
+ jsr SETVBV
+
; Restore system stuff
- ldx spsave
- txs ; Restore stack pointer
+NoIRQ2: ldx spsave
+ txs ; Restore stack pointer
-; restore left margin
+; Restore left margin
lda old_lmargin
sta LMARGN
-; restore kb mode
+; Restore kb mode
lda old_shflok
sta SHFLOK
-; restore APPMHI
+; Restore APPMHI
lda appmsav
sta APPMHI
dex
bpl L2
+; Turn on cursor
+
+ inx
+ stx CRSINH
+
; Back to DOS
rts
-; *** end of main startup code
+; ------------------------------------------------------------------------
+; The IRQ vector jumps here, if condes routines are defined with type 2.
-; setup sp
+IRQStub:
+ cld ; Just to be sure
+ jsr callirq ; Call the functions
+ jmp IRQInd ; Jump to the saved IRQ vector
-initsp:
- lda APPMHI
- sta sp
- lda APPMHI+1
- sta sp+1
- rts
+; ------------------------------------------------------------------------
+; Data
+
+.data
+
+IRQInd: jmp $0000
+
+; *** end of main startup code
- .data
+.segment "ZPSAVE"
-zpsave: .res zpspace
+zpsave: .res zpspace
.bss
old_lmargin: .res 1
.segment "AUTOSTRT"
- .word $02E0
- .word $02E1
- .word __CODE_LOAD__ + 1
+ .word RUNAD ; defined in atari.h
+ .word RUNAD+1
+ .word __STARTUP_LOAD__ + 1