]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
Moved IRQ hooking / unhooking from startup code to constructor / destructor to avoid...
[cc65] / libsrc / c128 / crt0.s
1 ;
2 ; Startup code for cc65 (C128 version)
3 ;
4
5         .export         _exit
6         .export         __STARTUP__ : absolute = 1      ; Mark as startup
7         .import         initlib, donelib
8         .import         zerobss
9         .import         push0, callmain
10         .import         RESTOR, BSOUT, CLRCH
11         .import         __RAM_START__, __RAM_SIZE__, __STACKSIZE__
12         .importzp       ST
13
14         .include        "zeropage.inc"
15         .include        "c128.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Startup code
20
21 .segment        "STARTUP"
22
23 Start:
24
25 ; Switch to the second charset
26
27         lda     #14
28         jsr     BSOUT
29
30 ; Before doing anything else, we have to setup our banking configuration.
31 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
32 ; to the underlying RAM works, but it is bad style.
33
34         lda     MMU_CR          ; Get current memory configuration...
35         pha                     ; ...and save it for later
36         lda     #MMU_CFG_CC65   ; Bank0 with kernal ROM
37         sta     MMU_CR
38
39 ; Save the zero page locations we need
40
41         ldx     #zpspace-1
42 L1:     lda     sp,x
43         sta     zpsave,x
44         dex
45         bpl     L1
46
47 ; Clear the BSS data
48
49         jsr     zerobss
50
51 ; Save system stuff and setup the stack
52
53         pla                     ; Get MMU setting
54         sta     mmusave
55
56         tsx
57         stx     spsave          ; Save the system stack pointer
58
59         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
60         sta     sp
61         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
62         sta     sp+1            ; Set argument stack ptr
63
64 ; Call module constructors
65
66         jsr     initlib
67
68 ; Set the bank for the file name to our execution bank. We must do this,
69 ; *after* calling constructors, because some of them may depend on the
70 ; original value of this register.
71
72         lda     #0
73         sta     FNAM_BANK
74
75 ; Push arguments and call main()
76
77         jsr     callmain
78
79 ; Back from main (this is also the _exit entry). Run module destructors
80
81 _exit:  pha                     ; Save the return code on stack
82         jsr     donelib
83
84 ; Copy back the zero page stuff
85
86         ldx     #zpspace-1
87 L2:     lda     zpsave,x
88         sta     sp,x
89         dex
90         bpl     L2
91
92 ; Place the program return code into ST
93
94         pla
95         sta     ST
96
97 ; Reset the stack and the memory configuration
98
99         ldx     spsave
100         txs
101         ldx     mmusave
102         stx     MMU_CR
103
104 ; Done, return to BASIC
105
106         rts
107
108 ; ------------------------------------------------------------------------
109 ; Data
110
111 .segment        "ZPSAVE"
112
113 zpsave: .res    zpspace
114
115 ; ------------------------------------------------------------------------
116
117 .bss
118
119 spsave: .res    1
120 mmusave:.res    1