]> git.sur5r.net Git - cc65/blob - libsrc/pce/crt0.s
Merge remote-tracking branch 'upstream/master' into pcenginetarget
[cc65] / libsrc / pce / crt0.s
1 ;
2 ; Startup code for cc65 (PCEngine version)
3 ;
4 ; by Groepaz/Hitmen <groepaz@gmx.net>
5 ; based on code by Ullrich von Bassewitz <uz@cc65.org>
6 ;
7 ; This must be the *first* file on the linker command line
8 ;
9
10         .export _exit
11         .export __STARTUP__ : absolute = 1      ; Mark as startup
12
13         .import initlib, donelib
14         .import push0, _main, zerobss
15         .import initheap
16         .import tmp1,tmp2,tmp3
17
18         ; Linker generated
19         .import __RAM_START__, __RAM_SIZE__
20         .import __ROM0_START__, __ROM0_SIZE__
21         .import __ROM_START__, __ROM_SIZE__
22         .import __STARTUP_LOAD__,__STARTUP_RUN__, __STARTUP_SIZE__
23         .import __CODE_LOAD__,__CODE_RUN__, __CODE_SIZE__
24         .import __RODATA_LOAD__,__RODATA_RUN__, __RODATA_SIZE__
25         .import __DATA_LOAD__,__DATA_RUN__, __DATA_SIZE__
26         .import __BSS_SIZE__
27
28         .include "pce.inc"
29
30         .importzp sp
31         .importzp ptr1,ptr2
32
33 ; ------------------------------------------------------------------------
34 ; Place the startup code in a special segment.
35
36                 .segment "STARTUP"
37
38 start:
39
40                 ; setup the CPU and System-IRQ
41
42                 ; Initialize CPU
43
44                 sei
45                 nop
46                 csh                     ; set high speed CPU mode
47                 nop
48                 cld
49                 nop
50
51                 ; Setup stack and memory mapping
52                 ldx     #$FF            ; Stack top ($21FF)
53                 txs
54
55                 ; at startup all MPRs are set to 0, so init them
56                 lda     #$ff
57                 tam     #%00000001      ; 0000-1FFF = Hardware page
58                 lda     #$F8
59                 tam     #%00000010      ; 2000-3FFF = Work RAM
60                 ;lda     #$F7
61                 ;tam     #%00000100      ; 4000-5FFF = Save RAM
62                 ;lda     #1
63                 ;tam     #%00001000      ; 6000-7FFF  Page 2
64                 ;lda     #2
65                 ;tam     #%00010000      ; 8000-9FFF  Page 3
66                 ;lda     #3
67                 ;tam     #%00100000      ; A000-BFFF  Page 4
68                 ;lda     #4
69                 ;tam     #%01000000      ; C000-DFFF  Page 5
70                 ;lda     #0
71                 ;tam     #%10000000      ; e000-fFFF  hucard/syscard bank 0
72
73                 ; Clear work RAM (2000-3FFF)
74                 stz     <$00
75                 tii     $2000, $2001, $1FFF
76
77                 ; Initialize hardware
78                 stz     TIMER_COUNT   ; Timer off
79                 lda     #$07
80                 sta     IRQ_MASK     ; Interrupts off
81                 stz     IRQ_STATUS   ; Acknowledge timer
82
83                 ;; FIXME; i dont know why the heck this one doesnt work when called from a constructor :/
84                 .import vdc_init
85                 jsr     vdc_init
86
87                 ; Turn on background and VD interrupt/IRQ1
88                 lda     #$05
89                 sta     IRQ_MASK           ; IRQ1=on
90
91                 cli
92
93                 ; Clear the BSS data
94                 jsr     zerobss
95
96                 ; Copy the .data segment to RAM
97                 lda     #<(__DATA_LOAD__)
98                 sta     ptr1
99                 lda     #>(__DATA_LOAD__)
100                 sta     ptr1+1
101                 lda     #<(__DATA_RUN__)
102                 sta     ptr2
103                 lda     #>(__DATA_RUN__)
104                 sta     ptr2+1
105
106                 ldx     #>(__DATA_SIZE__)
107 @l2:
108                 beq     @s1        ; no more full pages
109
110                 ; copy one page
111                 ldy     #0
112 @l1:
113                 lda     (ptr1),y
114                 sta     (ptr2),y
115                 iny
116                 bne     @l1
117
118                 inc     ptr1+1
119                 inc     ptr2+1
120
121                 dex
122                 bne     @l2
123
124                 ; copy remaining bytes
125 @s1:
126                 ; copy one page
127                 ldy     #0
128 @l3:
129                 lda     (ptr1),y
130                 sta     (ptr2),y
131                 iny
132                 cpy     #<(__DATA_SIZE__)
133                 bne     @l3
134
135                 ; setup the stack
136                 lda     #<(__RAM_START__+__RAM_SIZE__)
137                 sta     sp
138                 lda     #>(__RAM_START__+__RAM_SIZE__)
139                 sta     sp+1
140
141                 ; Call module constructors
142                 jsr     initlib
143
144                 ; Pass an empty command line
145                 jsr     push0                ; argc
146                 jsr     push0                ; argv
147
148                 ldy     #4                   ; Argument size
149                 jsr     _main                ; call the users code
150
151                 ; Call module destructors. This is also the _exit entry.
152 _exit:
153                 jsr     donelib                ; Run module destructors
154
155                 ; reset the PCEngine (start over)
156                 jmp start
157
158 ; ------------------------------------------------------------------------
159 ; System V-Blank Interupt
160 ; FIXME: hooks should be provided so the user can abuse the IRQ
161 ; ------------------------------------------------------------------------
162
163 _irq1:
164                 pha
165                 phx
166                 phy
167
168
169                 inc     tickcount
170                 bne     @s1
171                 inc     tickcount+1
172                 bne     @s1
173                 inc     tickcount+2
174                 bne     @s1
175                 inc     tickcount+3
176 @s1:
177                 ; Acknowlege interrupt
178                 lda     a:VDC_CTRL
179
180                 ply
181                 plx
182                 pla
183                 rti
184 _irq2:
185                 rti
186 _nmi:
187                 rti
188 _timer:
189                 stz     IRQ_STATUS
190                 rti
191
192                 .export initmainargs
193 initmainargs:
194                 rts
195
196 ; ------------------------------------------------------------------------
197 ; hardware vectors
198 ; ------------------------------------------------------------------------
199                 .segment "VECTORS"
200
201                 .word   _irq2           ; $fff6 IRQ2 (External IRQ, BRK)
202                 .word   _irq1           ; $fff8 IRQ1 (VDC)
203                 .word   _timer          ; $fffa Timer
204                 .word   _nmi            ; $fffc NMI
205                 .word   start           ; $fffe reset