]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
info about c1541 in docs, lowered highest available address to $6000 due to
[cc65] / libsrc / c128 / crt0.s
1 ;
2 ; Startup code for cc65 (C128 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         initconio, doneconio, zerobss
10         .import         push0, _main
11
12         .include        "c128.inc"
13         .include        "../cbm/cbm.inc"
14
15 ; ------------------------------------------------------------------------
16 ; Define and export the ZP variables for the C64 runtime
17
18         .exportzp       sp, sreg, regsave
19         .exportzp       ptr1, ptr2, ptr3, ptr4
20         .exportzp       tmp1, tmp2, tmp3, tmp4
21         .exportzp       regbank, zpspace
22
23 .zeropage
24
25 zpstart = *
26 sp:             .res    2       ; Stack pointer
27 sreg:           .res    2       ; Secondary register/high 16 bit for longs
28 regsave:        .res    2       ; slot to save/restore (E)AX into
29 ptr1:           .res    2
30 ptr2:           .res    2
31 ptr3:           .res    2
32 ptr4:           .res    2
33 tmp1:           .res    1
34 tmp2:           .res    1
35 tmp3:           .res    1
36 tmp4:           .res    1
37 regbank:        .res    6       ; 6 byte register bank
38
39 zpspace = * - zpstart           ; Zero page space allocated
40
41 .code
42
43 ; ------------------------------------------------------------------------
44 ; BASIC header with a SYS call
45
46         .org    $1BFF
47         .word   Head            ; Load address
48 Head:   .word   @Next
49         .word   1000            ; Line number
50         .byte   $9E,"7181"      ; SYS 7181
51         .byte   $00             ; End of BASIC line
52 @Next:  .word   0               ; BASIC end marker
53         .reloc
54
55 ; ------------------------------------------------------------------------
56 ; Actual code
57
58         ldx     #zpspace-1
59 L1:     lda     sp,x
60         sta     zpsave,x        ; save the zero page locations we need
61         dex
62         bpl     L1
63
64 ; Close open files
65
66         jsr     CLRCH
67
68 ; Switch to second charset
69
70         lda     #14
71         jsr     BSOUT
72
73 ; Get the current MMU setting and save it. Set new memory config.
74
75         lda     MMU_CR          ; Get current memory configuration...
76         pha                     ; ...and save it for later
77         lda     #$0E            ; Bank0 with kernal ROM
78         sta     MMU_CR
79
80 ; Clear the BSS data
81
82         jsr     zerobss
83
84 ; Save system stuff and setup the stack
85
86         pla                     ; Get MMU setting
87         sta     mmusave
88
89         tsx
90         stx     spsave          ; save system stk ptr
91
92         lda     #<$C000
93         sta     sp
94         lda     #>$C000
95         sta     sp+1
96
97 ; Call module constructors
98
99         jsr     initlib
100
101 ; Initialize conio stuff
102
103         jsr     initconio
104
105 ; Pass an empty command line
106
107         jsr     push0           ; argc
108         jsr     push0           ; argv
109
110         ldy     #4              ; Argument size
111         jsr     _main           ; call the users code
112
113 ; Call module destructors. This is also the _exit entry.
114
115 _exit:  jsr     donelib         ; Run module destructors
116
117 ; Reset the conio stuff
118
119         jsr     doneconio
120
121 ; Reset stack and the MMU
122
123         ldx     spsave          ; Patched at runtime
124         txs
125         lda     mmusave         ; Patched at runtime
126         sta     MMU_CR
127
128 ; Copy back the zero page stuff
129
130         ldx     #zpspace-1
131 L2:     lda     zpsave,x
132         sta     sp,x
133         dex
134         bpl     L2
135
136 ; Done
137
138         jmp     RESTOR
139
140 .data
141 zpsave: .res    zpspace
142
143 .bss
144 spsave: .res    1
145 mmusave:.res    1
146
147
148