]> git.sur5r.net Git - cc65/blob - libsrc/atmos/crt0.s
5feb06d3904865df6785cfb12bd0b3ce30a57598
[cc65] / libsrc / atmos / crt0.s
1 ;
2 ; Startup code for cc65 (Oric version)
3 ;
4 ; By Debrune Jérôme <jede@oric.org> and Ullrich von Bassewitz <uz@cc65.org>
5 ;
6
7         .export         _exit
8         .export         __STARTUP__ : absolute = 1      ; Mark as startup
9         .import         initlib, donelib
10         .import         callmain, zerobss
11         .import         __RAM_START__, __RAM_SIZE__
12         .import         __BSS_LOAD__, __STACKSIZE__
13
14         .include        "zeropage.inc"
15         .include        "atmos.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Oric tape header
20
21 .segment        "TAPEHDR"
22
23         .byte   $16, $16, $16   ; Sync bytes
24         .byte   $24             ; End of header marker
25
26         .byte   $00                             ; $2B0
27         .byte   $00                             ; $2AF
28         .byte   $80                             ; $2AE Machine code flag
29         .byte   $C7                             ; $2AD Autoload flag
30         .dbyt   __BSS_LOAD__                    ; $2AB
31         .dbyt   __RAM_START__                   ; $2A9
32         .byte   $00                             ; $2A8
33         .byte   ((.VERSION >> 8) & $0F) + '0'
34         .byte   ((.VERSION >> 4) & $0F) + '0'
35         .byte   (.VERSION & $0F) + '0'
36         .byte   $00                             ; Zero terminated compiler version
37
38 ; ------------------------------------------------------------------------
39 ; Place the startup code in a special segment.
40
41 .segment        "STARTUP"
42
43 ; Save the zero page area we're about to use
44
45         ldx     #zpspace-1
46 L1:     lda     sp,x
47         sta     zpsave,x        ; Save the zero page locations we need
48         dex
49         bpl     L1
50
51 ; Clear the BSS data
52
53         jsr     zerobss
54
55 ; Unprotect columns 0 and 1
56
57         lda     STATUS
58         sta     stsave
59         and     #%11011111
60         sta     STATUS
61
62 ; Save system stuff and setup the stack
63
64         tsx
65         stx     spsave          ; Save system stk ptr
66
67         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
68         sta     sp
69         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
70         sta     sp+1            ; Set argument stack ptr
71
72 ; Call module constructors
73
74         jsr     initlib
75
76 ; Push arguments and call main()
77
78         jsr     callmain
79
80 ; Call module destructors. This is also the _exit entry.
81
82 _exit:  jsr     donelib         ; Run module destructors
83
84 ; Restore system stuff
85
86         ldx     spsave
87         txs
88         lda     stsave
89         sta     STATUS
90
91 ; Copy back the zero page stuff
92
93         ldx     #zpspace-1
94 L2:     lda     zpsave,x
95         sta     sp,x
96         dex
97         bpl     L2
98
99 ; Back to BASIC
100
101         rts
102
103 ; ------------------------------------------------------------------------
104 ; Data
105
106 .segment        "ZPSAVE"
107
108 zpsave: .res    zpspace
109
110 .bss
111 spsave: .res    1
112 stsave: .res    1