blob: a656b12b7c94ad5e68825b3e453164a40994391f [file] [log] [blame]
oliverschmidt3cf3d302004-06-08 20:28:33 +00001;
oliverschmidt76872e82004-12-26 14:13:34 +00002; Startup code for Contiki (Apple2 version)
oliverschmidt3cf3d302004-06-08 20:28:33 +00003;
oliverschmidt76872e82004-12-26 14:13:34 +00004; This must be the *first* file on the linker command line
5;
oliverschmidt3cf3d302004-06-08 20:28:33 +00006
oliverschmidt46ea6732005-01-07 09:50:52 +00007 .export _exit
oliverschmidt76872e82004-12-26 14:13:34 +00008 .export __heaporg, __heapptr, __heapend
9 .export __heapfirst, __heaplast
10 .import initlib, donelib
11 .import zerobss
12 .import __RAM_START__, __RAM_LAST__ ; Linker generated
13 .import callmain
14 .importzp sp
oliverschmidt3cf3d302004-06-08 20:28:33 +000015
oliverschmidt76872e82004-12-26 14:13:34 +000016; ------------------------------------------------------------------------
17; The executable header
oliverschmidt3cf3d302004-06-08 20:28:33 +000018
oliverschmidt76872e82004-12-26 14:13:34 +000019.segment "EXEHDR"
oliverschmidt3cf3d302004-06-08 20:28:33 +000020
oliverschmidt76872e82004-12-26 14:13:34 +000021 .word __RAM_START__ ; Start address
22 .word __RAM_LAST__ - __RAM_START__ ; Size
oliverschmidt3cf3d302004-06-08 20:28:33 +000023
oliverschmidt76872e82004-12-26 14:13:34 +000024; ------------------------------------------------------------------------
25; Place the startup code in a special segment.
oliverschmidt3cf3d302004-06-08 20:28:33 +000026
oliverschmidt76872e82004-12-26 14:13:34 +000027.segment "STARTUP"
oliverschmidt19032c62004-06-14 22:30:32 +000028
oliverschmidt76872e82004-12-26 14:13:34 +000029; ProDOS TechRefMan, chapter 5.2.1:
30; "For maximum interrupt efficiency, a system program should not use more
31; than the upper 3/4 of the stack."
oliverschmidt19032c62004-06-14 22:30:32 +000032
oliverschmidt3cf3d302004-06-08 20:28:33 +000033 ldx #$FF
oliverschmidt76872e82004-12-26 14:13:34 +000034 txs ; Init stack pointer
oliverschmidt3cf3d302004-06-08 20:28:33 +000035
oliverschmidt76872e82004-12-26 14:13:34 +000036; Switch to 80 column mode
oliverschmidt3cf3d302004-06-08 20:28:33 +000037
oliverschmidt76872e82004-12-26 14:13:34 +000038 .ifdef __APPLE2ENH__
39 lda #$0D
oliverschmidt0fc22c52004-07-14 12:03:09 +000040 jsr $C300
oliverschmidt76872e82004-12-26 14:13:34 +000041 .endif
oliverschmidt0fc22c52004-07-14 12:03:09 +000042
oliverschmidt76872e82004-12-26 14:13:34 +000043; Save the original RESET vector
oliverschmidt3cf3d302004-06-08 20:28:33 +000044
oliverschmidt76872e82004-12-26 14:13:34 +000045 ldx #$02
46: lda $03F2,x
47 sta rvsave,x
48 dex
49 bpl :-
oliverschmidt3cf3d302004-06-08 20:28:33 +000050
oliverschmidt76872e82004-12-26 14:13:34 +000051; ProDOS TechRefMan, chapter 5.3.5:
52; "Your system program should place in the RESET vector the address of a
53; routine that ... closes the files."
oliverschmidt3cf3d302004-06-08 20:28:33 +000054
oliverschmidt76872e82004-12-26 14:13:34 +000055 lda #<_exit
56 sta $03F2
57 lda #>_exit
58 sta $03F3
59 eor #$A5
60 sta $03F4
61
62; Switch in LC bank 2 for R/W
oliverschmidt3cf3d302004-06-08 20:28:33 +000063
oliverschmidt76872e82004-12-26 14:13:34 +000064 bit $C083
65 bit $C083
66
oliverschmidt46ea6732005-01-07 09:50:52 +000067; Clear the BSS data (in LC bank 2)
oliverschmidt76872e82004-12-26 14:13:34 +000068
oliverschmidt3cf3d302004-06-08 20:28:33 +000069 jsr zerobss
70
oliverschmidt76872e82004-12-26 14:13:34 +000071; Setup the stack in LC bank 2
72
73 lda #$00
oliverschmidt46ea6732005-01-07 09:50:52 +000074 ldx #$E0
oliverschmidt76872e82004-12-26 14:13:34 +000075 sta sp
oliverschmidt46ea6732005-01-07 09:50:52 +000076 stx sp+1
oliverschmidt76872e82004-12-26 14:13:34 +000077
oliverschmidt46ea6732005-01-07 09:50:52 +000078; Setup the heap end at HIMEM
oliverschmidt76872e82004-12-26 14:13:34 +000079
oliverschmidt46ea6732005-01-07 09:50:52 +000080 lda $73
81 ldx $73+1
82 sta __heapend
83 stx __heapend+1
oliverschmidt76872e82004-12-26 14:13:34 +000084
85; Call module constructors
86
oliverschmidt3cf3d302004-06-08 20:28:33 +000087 jsr initlib
oliverschmidt76872e82004-12-26 14:13:34 +000088
89; Push arguments and call main()
90
oliverschmidt3cf3d302004-06-08 20:28:33 +000091 jsr callmain
oliverschmidt3cf3d302004-06-08 20:28:33 +000092
oliverschmidt46ea6732005-01-07 09:50:52 +000093; Avoid re-entrance of donelib. This is also the _exit entry
oliverschmidt3cf3d302004-06-08 20:28:33 +000094
oliverschmidt76872e82004-12-26 14:13:34 +000095_exit: ldx #$02
96: lda rvsave,x
97 sta $03F2,x
98 dex
99 bpl :-
oliverschmidt3cf3d302004-06-08 20:28:33 +0000100
oliverschmidt76872e82004-12-26 14:13:34 +0000101; Call module destructors
oliverschmidt3cf3d302004-06-08 20:28:33 +0000102
oliverschmidt76872e82004-12-26 14:13:34 +0000103 jsr donelib
oliverschmidt3cf3d302004-06-08 20:28:33 +0000104
oliverschmidt76872e82004-12-26 14:13:34 +0000105; ProDOS TechRefMan, chapter 5.2.1:
106; "System programs should set the stack pointer to $FF at the warm-start
oliverschmidt46ea6732005-01-07 09:50:52 +0000107; entry point."
oliverschmidt3cf3d302004-06-08 20:28:33 +0000108
oliverschmidt46ea6732005-01-07 09:50:52 +0000109 ldx #$FF
oliverschmidt76872e82004-12-26 14:13:34 +0000110 txs ; Re-init stack pointer
oliverschmidt3cf3d302004-06-08 20:28:33 +0000111
oliverschmidt76872e82004-12-26 14:13:34 +0000112; Back to DOS
oliverschmidt3cf3d302004-06-08 20:28:33 +0000113
oliverschmidt76872e82004-12-26 14:13:34 +0000114 jmp $03D0
oliverschmidt3cf3d302004-06-08 20:28:33 +0000115
oliverschmidt76872e82004-12-26 14:13:34 +0000116; ------------------------------------------------------------------------
117; Data
oliverschmidt3cf3d302004-06-08 20:28:33 +0000118
oliverschmidt76872e82004-12-26 14:13:34 +0000119.data
oliverschmidt3cf3d302004-06-08 20:28:33 +0000120
oliverschmidt76872e82004-12-26 14:13:34 +0000121rvsave: .res 3
oliverschmidt3cf3d302004-06-08 20:28:33 +0000122
oliverschmidt76872e82004-12-26 14:13:34 +0000123__heaporg:
124 .word __RAM_LAST__ ; Linker calculates this symbol
125__heapptr:
126 .word __RAM_LAST__ ; Dito
127__heapend:
128 .word 0
129__heapfirst:
130 .word 0
131__heaplast:
132 .word 0