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