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