]> git.sur5r.net Git - cc65/blob - libsrc/plus4/crt0.s
701b172587627b4135a2393af06f6e2a7608b2ea
[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        "zeropage.inc"
12         .include        "plus4.inc"
13         .include        "../cbm/cbm.inc"
14
15 ; ------------------------------------------------------------------------
16 ; BASIC header with a SYS call
17
18 .code
19
20         .org    $0FFF
21         .word   Head            ; Load address
22 Head:   .word   @Next
23         .word   1000            ; Line number
24         .byte   $9E,"4109"      ; SYS 4109
25         .byte   $00             ; End of BASIC line
26 @Next:  .word   0               ; BASIC end marker
27         .reloc
28
29 ; ------------------------------------------------------------------------
30 ; Actual code
31
32         ldx     #zpspace-1
33 L1:     lda     sp,x
34         sta     zpsave,x        ; save the zero page locations we need
35         dex
36         bpl     L1
37
38 ; Close open files
39
40         jsr     CLRCH
41
42 ; Switch to second charset
43
44         lda     #14
45         jsr     BSOUT
46
47 ; Clear the BSS data
48
49         jsr     zerobss
50
51 ; Save system stuff and setup the stack
52
53         tsx
54         stx     spsave          ; save system stk ptr
55
56         sec
57         jsr     MEMTOP          ; Get top memory
58         cpy     #$80            ; We can only use the low 32K :-(
59         bcc     MemOk
60         ldy     #$80
61         ldx     #$00
62 MemOk:  stx     sp
63         sty     sp+1            ; set argument stack ptr
64
65 ; Call module constructors
66
67         jsr     initlib
68
69 ; Pass an empty command line
70
71         jsr     push0           ; argc
72         jsr     push0           ; argv
73
74         ldy     #4              ; Argument size
75         jsr     _main           ; call the users code
76
77 ; Call module destructors. This is also the _exit entry.
78
79 _exit:  jsr     donelib         ; Run module destructors
80
81 ; Restore system stuff
82
83         ldx     spsave
84         txs
85
86 ; Copy back the zero page stuff
87
88         ldx     #zpspace-1
89 L2:     lda     zpsave,x
90         sta     sp,x
91         dex
92         bpl     L2
93
94 ; Reset changed vectors
95
96         jmp     RESTOR
97
98
99 .data
100 zpsave: .res    zpspace
101
102 .bss
103 spsave: .res    1
104
105