]> git.sur5r.net Git - cc65/blob - libsrc/pet/crt0.s
Added o65 symbol export capability
[cc65] / libsrc / pet / crt0.s
1 ;
2 ; Startup code for cc65 (PET version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib
9         .import         zerobss, push0
10         .import         _main
11
12         .include        "pet.inc"
13         .include        "../cbm/cbm.inc"
14
15 ; ------------------------------------------------------------------------
16 ; Define and export the ZP variables for the C64 runtime
17
18         .exportzp       sp, sreg, regsave
19         .exportzp       ptr1, ptr2, ptr3, ptr4
20         .exportzp       tmp1, tmp2, tmp3, tmp4
21         .exportzp       regbank, zpspace
22
23 .zeropage
24
25 zpstart = *
26 sp:             .res    2       ; Stack pointer
27 sreg:           .res    2       ; Secondary register/high 16 bit for longs
28 regsave:        .res    2       ; slot to save/restore (E)AX into
29 ptr1:           .res    2
30 ptr2:           .res    2
31 ptr3:           .res    2
32 ptr4:           .res    2
33 tmp1:           .res    1
34 tmp2:           .res    1
35 tmp3:           .res    1
36 tmp4:           .res    1
37 regbank:        .res    6       ; 6 byte register bank
38
39 zpspace = * - zpstart           ; Zero page space allocated
40
41 .code
42
43 ; ------------------------------------------------------------------------
44 ; BASIC header with a SYS call
45
46         .org    $3FF
47         .word   Head            ; Load address
48 Head:   .word   @Next
49         .word   1000            ; Line number
50         .byte   $9E,"1037"      ; SYS 1037
51         .byte   $00             ; End of BASIC line
52 @Next:  .word   0               ; BASIC end marker
53         .reloc
54
55 ; ------------------------------------------------------------------------
56 ; Actual code
57
58         ldx     #zpspace-1
59 L1:     lda     sp,x
60         sta     zpsave,x        ; Save the zero page locations we need
61         dex
62         bpl     L1
63
64 ; Close open files
65
66         jsr     CLRCH
67
68 ; Switch to second charset
69
70         lda     #14
71 ;       sta     $E84C           ; See PET FAQ
72         jsr     BSOUT
73
74 ; Clear the BSS data
75
76         jsr     zerobss
77
78 ; Save system stuff and setup the stack
79
80         tsx
81         stx     spsave          ; Save the system stack ptr
82
83         lda     MEMSIZE
84         sta     sp
85         lda     MEMSIZE+1
86         sta     sp+1            ; Set argument stack ptr
87
88 ; Call module constructors
89
90         jsr     initlib
91
92 ; Pass an empty command line
93
94         jsr     push0           ; argc
95         jsr     push0           ; argv
96
97         ldy     #4              ; Argument size
98         jsr     _main           ; call the users code
99
100 ; Call module destructors. This is also the _exit entry.
101
102 _exit:  jsr     donelib         ; Run module destructors
103
104 ; Restore system stuff
105
106         ldx     spsave
107         txs                     ; Restore stack pointer
108
109 ; Copy back the zero page stuff
110
111         ldx     #zpspace-1
112 L2:     lda     zpsave,x
113         sta     sp,x
114         dex
115         bpl     L2
116
117 ; Back to basic
118
119         rts
120
121
122 .data
123
124 zpsave: .res    zpspace
125
126 .bss
127
128 spsave: .res    1
129 mmusave:.res    1
130