]> git.sur5r.net Git - cc65/blob - libsrc/vic20/crt0.s
ca3ff2f496bbb28c508aa8ec8e766ce7f5af5695
[cc65] / libsrc / vic20 / crt0.s
1 ;
2 ; Startup code for cc65 (Vic20 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         .import         RESTOR, BSOUT, CLRCH
12         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
13
14         .include        "zeropage.inc"
15         .include        "vic20.inc"
16         .include        "../cbm/cbm.inc"
17
18 ; ------------------------------------------------------------------------
19 ; Create an empty LOWCODE segment to avoid linker warnings
20
21 .segment        "LOWCODE"
22
23 ; ------------------------------------------------------------------------
24 ; Place the startup code in a special segment.
25
26 .segment        "STARTUP"
27
28 ; BASIC header with a SYS call
29
30         .word   Head            ; Load address
31 Head:   .word   @Next
32         .word   1000            ; Line number
33         .byte   $9E             ; SYS token
34         .byte   <(((@Start / 1000) .mod 10) + $30)
35         .byte   <(((@Start /  100) .mod 10) + $30)
36         .byte   <(((@Start /   10) .mod 10) + $30)
37         .byte   <(((@Start /    1) .mod 10) + $30)
38         .byte   $00             ; End of BASIC line
39 @Next:  .word   0               ; BASIC end marker
40 @Start:
41
42 ; ------------------------------------------------------------------------
43 ; Actual code
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 ; Close open files
52
53         jsr     CLRCH
54
55 ; Switch to second charset
56
57         lda     #14
58         jsr     BSOUT
59
60 ; Clear the BSS data
61
62         jsr     zerobss
63
64 ; Save system stuff and setup the stack
65
66         tsx
67         stx     spsave          ; Save the system stack ptr
68
69         lda     #<(__RAM_START__ + __RAM_SIZE__)
70         sta     sp
71         lda     #>(__RAM_START__ + __RAM_SIZE__)
72         sta     sp+1            ; Set argument stack ptr
73
74 ; Call module constructors
75
76         jsr     initlib
77
78 ; Pass an empty command line
79
80         jsr     push0           ; argc
81         jsr     push0           ; argv
82
83         ldy     #4              ; Argument size
84         jsr     _main           ; call the users code
85
86 ; Call module destructors. This is also the _exit entry.
87
88 _exit:  jsr     donelib         ; Run module destructors
89
90 ; Restore system stuff
91
92         ldx     spsave
93         txs                     ; Restore stack pointer
94
95 ; Copy back the zero page stuff
96
97         ldx     #zpspace-1
98 L2:     lda     zpsave,x
99         sta     sp,x
100         dex
101         bpl     L2
102
103 ; Reset changed vectors, back to basic
104
105         jmp     RESTOR
106
107
108 ; ------------------------------------------------------------------------
109 ; Data
110
111 .data
112
113 zpsave: .res    zpspace
114
115 .bss
116
117 spsave: .res    1