]> git.sur5r.net Git - cc65/blob - libsrc/vic20/crt0.s
Fixed a bug
[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         __RAM_START__, __RAM_SIZE__     ; Linker generated
12
13         .include        "zeropage.inc"
14         .include        "vic20.inc"
15         .include        "../cbm/cbm.inc"
16
17 ; ------------------------------------------------------------------------
18 ; BASIC header with a SYS call
19
20 .segment        "EXEHDR"
21         .word   Head            ; Load address
22 Head:   .word   @Next
23         .word   1000            ; Line number
24         .byte   $9E             ; SYS token
25         .byte   <(((@Start / 1000) .mod 10) + $30)
26         .byte   <(((@Start /  100) .mod 10) + $30)
27         .byte   <(((@Start /   10) .mod 10) + $30)
28         .byte   <(((@Start /    1) .mod 10) + $30)
29         .byte   $00             ; End of BASIC line
30 @Next:  .word   0               ; BASIC end marker
31 @Start:
32
33 ; ------------------------------------------------------------------------
34 ; Actual code
35
36 .code
37
38         ldx     #zpspace-1
39 L1:     lda     sp,x
40         sta     zpsave,x        ; Save the zero page locations we need
41         dex
42         bpl     L1
43
44 ; Close open files
45
46         jsr     CLRCH
47
48 ; Switch to second charset
49
50         lda     #14
51         jsr     BSOUT
52
53 ; Clear the BSS data
54
55         jsr     zerobss
56
57 ; Save system stuff and setup the stack
58
59         tsx
60         stx     spsave          ; Save the system stack ptr
61
62         lda     #<(__RAM_START__ + __RAM_SIZE__)
63         sta     sp
64         lda     #>(__RAM_START__ + __RAM_SIZE__)
65         sta     sp+1            ; Set argument stack ptr
66
67 ; Call module constructors
68
69         jsr     initlib
70
71 ; Pass an empty command line
72
73         jsr     push0           ; argc
74         jsr     push0           ; argv
75
76         ldy     #4              ; Argument size
77         jsr     _main           ; call the users code
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                     ; Restore stack pointer
87
88 ; Copy back the zero page stuff
89
90         ldx     #zpspace-1
91 L2:     lda     zpsave,x
92         sta     sp,x
93         dex
94         bpl     L2
95
96 ; Reset changed vectors, back to basic
97
98         jmp     RESTOR
99
100
101 .data
102
103 zpsave: .res    zpspace
104
105 .bss
106
107 spsave: .res    1