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