]> git.sur5r.net Git - cc65/blob - libsrc/pce/crt0.s
use tii instead of copy loop to copy data section to ram
[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         IRQStub
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         .include        "extzp.inc"
30
31         .importzp       sp
32         .importzp       ptr1,ptr2
33         .importzp       tmp1,tmp2,tmp3
34
35 ; ------------------------------------------------------------------------
36 ; Place the startup code in a special segment.
37
38         .segment "STARTUP"
39
40 start:
41
42         ; setup the CPU and System-IRQ
43
44         ; Initialize CPU
45
46         sei
47         nop
48         csh                     ; set high speed CPU mode
49         nop
50         cld
51         nop
52
53         ; Setup stack and memory mapping
54         ldx     #$FF            ; Stack top ($21FF)
55         txs
56
57         ; at startup all MPRs are set to 0, so init them
58         lda     #$ff
59         tam     #%00000001      ; 0000-1FFF = Hardware page
60         lda     #$F8
61         tam     #%00000010      ; 2000-3FFF = Work RAM
62
63         ; FIXME: setup a larger block of memory to use with C-code
64         ;lda     #$F7
65         ;tam     #%00000100      ; 4000-5FFF = Save RAM
66         ;lda     #1
67         ;tam     #%00001000      ; 6000-7FFF  Page 2
68         ;lda     #2
69         ;tam     #%00010000      ; 8000-9FFF  Page 3
70         ;lda     #3
71         ;tam     #%00100000      ; A000-BFFF  Page 4
72         ;lda     #4
73         ;tam     #%01000000      ; C000-DFFF  Page 5
74         ;lda     #0
75         ;tam     #%10000000      ; e000-fFFF  hucard/syscard bank 0
76
77         ; Clear work RAM (2000-3FFF)
78         stz     <$00
79         tii     $2000, $2001, $1FFF
80
81         ; Initialize hardware
82         stz     TIMER_CTRL      ; Timer off
83         lda     #$07
84         sta     IRQ_MASK        ; Interrupts off
85         stz     IRQ_STATUS      ; Acknowledge timer
86
87         ; FIXME; i dont know why the heck this one doesnt work when called from a constructor :/
88         .import vdc_init
89         jsr     vdc_init
90
91         ; Turn on background and VD interrupt/IRQ1
92         lda     #$05
93         sta     IRQ_MASK        ; IRQ1=on
94
95         ; Clear the BSS data
96         jsr     zerobss
97
98         ; Copy the .data segment to RAM
99         tii     __DATA_LOAD__, __DATA_RUN__, __DATA_SIZE__
100
101         ; setup the stack
102         lda     #<(__RAM_START__+__RAM_SIZE__)
103         sta     sp
104         lda     #>(__RAM_START__+__RAM_SIZE__)
105         sta     sp + 1
106
107         ; Call module constructors
108         jsr     initlib
109
110         cli     ; allow IRQ only after constructors have run
111
112         ; Pass an empty command line
113         jsr     push0           ; argc
114         jsr     push0           ; argv
115
116         ldy     #4              ; Argument size
117         jsr     _main           ; call the users code
118
119         ; Call module destructors. This is also the _exit entry.
120 _exit:
121         jsr     donelib         ; Run module destructors
122
123         ; reset the PCEngine (start over)
124         jmp     start
125
126 _nmi:
127         rti
128
129         .export initmainargs
130 initmainargs:
131         rts
132
133 ; ------------------------------------------------------------------------
134 ; hardware vectors
135 ; ------------------------------------------------------------------------
136         .segment "VECTORS"
137
138         .word   IRQStub         ; $fff6 IRQ2 (External IRQ, BRK)
139         .word   IRQStub         ; $fff8 IRQ1 (VDC)
140         .word   IRQStub         ; $fffa Timer
141         .word   _nmi            ; $fffc NMI
142         .word   start           ; $fffe reset