blob: fffa3d282e779631fe86a04bd5fb363ddbf70a70 [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
oliverschmidtd86377a2005-03-09 00:51:51 +00008 .import zerobss, initlib, callmain, donelib
9 .import __STARTUP_RUN__, __BOOT_SIZE__ ; Linker generated
oliverschmidta330e332005-01-22 03:00:27 +000010 .import __BSS_RUN__, __BSS_SIZE__ ; Linker generated
oliverschmidtd86377a2005-03-09 00:51:51 +000011 .import __INIT_RUN__, __INIT_SIZE__ ; Linker generated
oliverschmidta330e332005-01-22 03:00:27 +000012 .import __CODE_RUN__, __CODE_SIZE__ ; Linker generated
oliverschmidt76872e82004-12-26 14:13:34 +000013 .importzp sp
oliverschmidt97e39572005-03-29 23:10:40 +000014 .linecont +
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
oliverschmidt97e39572005-03-29 23:10:40 +000021 .word __STARTUP_RUN__ ; Start address
22 .word __BSS_RUN__ + __BOOT_SIZE__ + \
23 __INIT_SIZE__ + __CODE_SIZE__ - \
24 __STARTUP_RUN__ ; Size
oliverschmidt3cf3d302004-06-08 20:28:33 +000025
oliverschmidt76872e82004-12-26 14:13:34 +000026; ------------------------------------------------------------------------
27; Place the startup code in a special segment.
oliverschmidt3cf3d302004-06-08 20:28:33 +000028
oliverschmidt76872e82004-12-26 14:13:34 +000029.segment "STARTUP"
oliverschmidt19032c62004-06-14 22:30:32 +000030
oliverschmidtd86377a2005-03-09 00:51:51 +000031; Forward control to the code in the "BOOT" segment
oliverschmidt19032c62004-06-14 22:30:32 +000032
oliverschmidtd86377a2005-03-09 00:51:51 +000033 jmp __BSS_RUN__
oliverschmidt67fa2ac2005-02-08 00:32:10 +000034
35; Avoid re-entrance of donelib. This is also the _exit entry
36
37_exit: ldx #$02
38: lda rvsave,x
39 sta $03F2,x
40 dex
41 bpl :-
42
43; Switch in LC bank 2 for R/O
44
45 bit $C080
46
47; Call module destructors
48
49 jsr donelib
50
oliverschmidt6b492522005-04-17 15:38:36 +000051; Switch in ROM
oliverschmidt67fa2ac2005-02-08 00:32:10 +000052
oliverschmidt6b492522005-04-17 15:38:36 +000053 bit $C082
oliverschmidt67fa2ac2005-02-08 00:32:10 +000054
55; Back to DOS
56
57 jmp $03D0
58
59; ------------------------------------------------------------------------
60; The linker doesn't calculate where this code runs so it has to be PIC
61
oliverschmidtd86377a2005-03-09 00:51:51 +000062.segment "BOOT"
63
oliverschmidt76872e82004-12-26 14:13:34 +000064; Switch to 80 column mode
oliverschmidt3cf3d302004-06-08 20:28:33 +000065
oliverschmidt76872e82004-12-26 14:13:34 +000066 .ifdef __APPLE2ENH__
67 lda #$0D
oliverschmidt0fc22c52004-07-14 12:03:09 +000068 jsr $C300
oliverschmidt76872e82004-12-26 14:13:34 +000069 .endif
oliverschmidt0fc22c52004-07-14 12:03:09 +000070
oliverschmidt67fa2ac2005-02-08 00:32:10 +000071; Setup the stack at HIMEM
72
73 lda $73
74 ldx $73+1
75 sta sp
76 stx sp+1
77
oliverschmidt76872e82004-12-26 14:13:34 +000078; Save the original RESET vector
oliverschmidt3cf3d302004-06-08 20:28:33 +000079
oliverschmidt76872e82004-12-26 14:13:34 +000080 ldx #$02
81: lda $03F2,x
82 sta rvsave,x
83 dex
84 bpl :-
oliverschmidt3cf3d302004-06-08 20:28:33 +000085
oliverschmidt76872e82004-12-26 14:13:34 +000086; ProDOS TechRefMan, chapter 5.3.5:
87; "Your system program should place in the RESET vector the address of a
88; routine that ... closes the files."
oliverschmidt3cf3d302004-06-08 20:28:33 +000089
oliverschmidt76872e82004-12-26 14:13:34 +000090 lda #<_exit
91 sta $03F2
92 lda #>_exit
93 sta $03F3
94 eor #$A5
95 sta $03F4
96
oliverschmidta330e332005-01-22 03:00:27 +000097; Switch in LC bank 2 for W/O
oliverschmidt3cf3d302004-06-08 20:28:33 +000098
oliverschmidta330e332005-01-22 03:00:27 +000099 bit $C081
100 bit $C081
oliverschmidt76872e82004-12-26 14:13:34 +0000101
oliverschmidta330e332005-01-22 03:00:27 +0000102; Set source start
103
oliverschmidtd86377a2005-03-09 00:51:51 +0000104 lda #<(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__)
105 ldx #>(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__)
oliverschmidta330e332005-01-22 03:00:27 +0000106 sta $3C
107 stx $3D
108
109; Set source end
110
oliverschmidtd86377a2005-03-09 00:51:51 +0000111 lda #<(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__ + __CODE_SIZE__)
112 ldx #>(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__ + __CODE_SIZE__)
oliverschmidta330e332005-01-22 03:00:27 +0000113 sta $3E
114 stx $3F
115
116; Set destination
117
118 lda #<__CODE_RUN__
119 ldx #>__CODE_RUN__
120 sta $42
121 stx $43
122
oliverschmidtd86377a2005-03-09 00:51:51 +0000123; Reset index and call MOVE to relocate the "CODE" segment
oliverschmidta330e332005-01-22 03:00:27 +0000124
125 ldy #$00
126 jsr $FE2C
127
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000128; Set source start
oliverschmidta330e332005-01-22 03:00:27 +0000129
oliverschmidtd86377a2005-03-09 00:51:51 +0000130 lda #<(__BSS_RUN__ + __BOOT_SIZE__)
131 ldx #>(__BSS_RUN__ + __BOOT_SIZE__)
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000132 sta $3C
133 stx $3D
oliverschmidta330e332005-01-22 03:00:27 +0000134
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000135; Set source end
oliverschmidt76872e82004-12-26 14:13:34 +0000136
oliverschmidtd86377a2005-03-09 00:51:51 +0000137 lda #<(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__)
138 ldx #>(__BSS_RUN__ + __BOOT_SIZE__ + __INIT_SIZE__)
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000139 sta $3E
140 stx $3F
oliverschmidt3cf3d302004-06-08 20:28:33 +0000141
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000142; Set destination
oliverschmidt76872e82004-12-26 14:13:34 +0000143
oliverschmidtd86377a2005-03-09 00:51:51 +0000144 lda #<__INIT_RUN__
145 ldx #>__INIT_RUN__
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000146 sta $42
147 stx $43
oliverschmidt76872e82004-12-26 14:13:34 +0000148
oliverschmidtd86377a2005-03-09 00:51:51 +0000149; Reset index and call MOVE to relocate the "INIT" segment
oliverschmidt76872e82004-12-26 14:13:34 +0000150
oliverschmidt67fa2ac2005-02-08 00:32:10 +0000151 ldy #$00
oliverschmidtd86377a2005-03-09 00:51:51 +0000152 jsr $FE2C
153
154; Switch in LC bank 2 for R/O
155
156 bit $C080
157
158; Forward control to code in the "INIT" segment
159
160 jmp init
161
162; ------------------------------------------------------------------------
163; The "INIT" segment will be overwritten by the heap
164
165.segment "INIT"
166
167; Clear the BSS data (and thus overwrite the "BOOT" segment)
168
169init: jsr zerobss
170
171; Call module constructors
172
173 jsr initlib
174
175; Push arguments and call main()
176
177 jmp callmain
oliverschmidt3cf3d302004-06-08 20:28:33 +0000178
oliverschmidt76872e82004-12-26 14:13:34 +0000179; ------------------------------------------------------------------------
180; Data
oliverschmidt3cf3d302004-06-08 20:28:33 +0000181
oliverschmidt76872e82004-12-26 14:13:34 +0000182.data
oliverschmidt3cf3d302004-06-08 20:28:33 +0000183
oliverschmidt76872e82004-12-26 14:13:34 +0000184rvsave: .res 3