]> git.sur5r.net Git - cc65/blob - libsrc/vic20/crt0.s
Use the assembler version as BASIC line number, so the version of the
[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         callmain
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 ; Place the startup code in a special segment.
20
21 .segment        "STARTUP"
22
23 ; BASIC header with a SYS call
24
25         .word   Head            ; Load address
26 Head:   .word   @Next
27         .word   .version        ; Line number
28         .byte   $9E             ; SYS token
29         .byte   <(((@Start / 1000) .mod 10) + $30)
30         .byte   <(((@Start /  100) .mod 10) + $30)
31         .byte   <(((@Start /   10) .mod 10) + $30)
32         .byte   <(((@Start /    1) .mod 10) + $30)
33         .byte   $00             ; End of BASIC line
34 @Next:  .word   0               ; BASIC end marker
35 @Start:
36
37 ; ------------------------------------------------------------------------
38 ; Actual code
39
40         ldx     #zpspace-1
41 L1:     lda     sp,x
42         sta     zpsave,x        ; Save the zero page locations we need
43         dex
44         bpl     L1
45
46 ; Close open files
47
48         jsr     CLRCH
49
50 ; Switch to second charset
51
52         lda     #14
53         jsr     BSOUT
54
55 ; Clear the BSS data
56
57         jsr     zerobss
58
59 ; Save system stuff and setup the stack
60
61         tsx
62         stx     spsave          ; Save the system stack ptr
63
64         lda     #<(__RAM_START__ + __RAM_SIZE__)
65         sta     sp
66         lda     #>(__RAM_START__ + __RAM_SIZE__)
67         sta     sp+1            ; Set argument stack ptr
68
69 ; Call module constructors
70
71         jsr     initlib
72
73 ; Push arguments and call main()
74
75         jsr     callmain
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                     ; Restore stack pointer
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, back to basic
95
96         jmp     RESTOR
97
98
99 ; ------------------------------------------------------------------------
100 ; Data
101
102 .data
103
104 zpsave: .res    zpspace
105
106 .bss
107
108 spsave: .res    1