]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Made the code that logs indirect-goto referals be a little more efficient.
[cc65] / libsrc / c16 / crt0.s
1 ;
2 ; Startup code for cc65 (C16 version)
3 ;
4 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So, many
5 ; things are similar here; and, we even use the plus4.inc include file.
6 ;
7
8         .export         _exit
9         .export         __STARTUP__ : absolute = 1      ; Mark as startup
10         .import         initlib, donelib
11         .import         callmain, zerobss
12         .import         MEMTOP, RESTOR, BSOUT, CLRCH
13         .importzp       ST
14
15         .include        "zeropage.inc"
16         .include        "plus4.inc"
17
18 ; ------------------------------------------------------------------------
19 ; Startup code
20
21 .segment        "STARTUP"
22
23 Start:
24
25 ; Save the zero-page locations that we need.
26
27         ldx     #zpspace-1
28 L1:     lda     sp,x
29         sta     zpsave,x
30         dex
31         bpl     L1
32
33 ; Switch to the second charset.
34
35         lda     #14
36         jsr     BSOUT
37
38 ; Clear the BSS data.
39
40         jsr     zerobss
41
42 ; Save some system stuff; and, set up the stack.
43
44         tsx
45         stx     spsave          ; save system stk ptr
46
47         sec
48         jsr     MEMTOP          ; Get top memory
49         cpy     #$80            ; We can only use the low 32K :-(
50         bcc     MemOk
51         ldy     #$80
52         ldx     #$00
53 MemOk:  stx     sp
54         sty     sp+1            ; set argument stack ptr
55
56 ; Call the module constructors.
57
58         jsr     initlib
59
60 ; Push the command-line arguments; and, call main().
61
62         jsr     callmain
63
64 ; Call the module destructors. This is also the exit() entry.
65
66 _exit:  pha                     ; Save the return code on stack
67         jsr     donelib         ; Run module destructors
68
69 ; Copy back the zero-page stuff.
70
71         ldx     #zpspace-1
72 L2:     lda     zpsave,x
73         sta     sp,x
74         dex
75         bpl     L2
76
77 ; Store the return code into BASIC's status variable.
78
79         pla
80         sta     ST
81
82 ; Restore the stack pointer.
83
84         ldx     spsave
85         txs
86
87 ; Back to BASIC.
88
89         rts
90
91 ; ------------------------------------------------------------------------
92
93 .segment        "INIT"
94
95 zpsave: .res    zpspace
96
97 ; ------------------------------------------------------------------------
98
99 .bss
100
101 spsave: .res    1