2 ; Startup code for cc65 (PCEngine version)
4 ; by Groepaz/Hitmen <groepaz@gmx.net>
5 ; based on code by Ullrich von Bassewitz <uz@cc65.org>
7 ; This must be the *first* file on the linker command line
11 .export __STARTUP__ : absolute = 1 ; Mark as startup
13 .import initlib, donelib
14 .import push0, _main, zerobss
16 .import tmp1,tmp2,tmp3
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__
34 ; ------------------------------------------------------------------------
35 ; Place the startup code in a special segment.
41 ; setup the CPU and System-IRQ
47 csh ; set high speed CPU mode
52 ; Setup stack and memory mapping
53 ldx #$FF ; Stack top ($21FF)
56 ; at startup all MPRs are set to 0, so init them
58 tam #%00000001 ; 0000-1FFF = Hardware page
60 tam #%00000010 ; 2000-3FFF = Work RAM
62 ; FIXME: setup a larger block of memory to use with C-code
64 ;tam #%00000100 ; 4000-5FFF = Save RAM
66 ;tam #%00001000 ; 6000-7FFF Page 2
68 ;tam #%00010000 ; 8000-9FFF Page 3
70 ;tam #%00100000 ; A000-BFFF Page 4
72 ;tam #%01000000 ; C000-DFFF Page 5
74 ;tam #%10000000 ; e000-fFFF hucard/syscard bank 0
76 ; Clear work RAM (2000-3FFF)
78 tii $2000, $2001, $1FFF
81 stz TIMER_CTRL ; Timer off
83 sta IRQ_MASK ; Interrupts off
84 stz IRQ_STATUS ; Acknowledge timer
86 ; FIXME; i dont know why the heck this one doesnt work when called from a constructor :/
90 ; Turn on background and VD interrupt/IRQ1
92 sta IRQ_MASK ; IRQ1=on
97 ; Copy the .data segment to RAM
100 lda #>(__DATA_LOAD__)
107 ldx #>(__DATA_SIZE__)
109 beq @s1 ; no more full pages
125 ; copy remaining bytes
133 cpy #<(__DATA_SIZE__)
137 lda #<(__RAM_START__+__RAM_SIZE__)
139 lda #>(__RAM_START__+__RAM_SIZE__)
142 ; Call module constructors
145 cli ; allow IRQ only after constructors have run
147 ; Pass an empty command line
151 ldy #4 ; Argument size
152 jsr _main ; call the users code
154 ; Call module destructors. This is also the _exit entry.
156 jsr donelib ; Run module destructors
158 ; reset the PCEngine (start over)
161 ; ------------------------------------------------------------------------
162 ; System V-Blank Interupt
163 ; FIXME: hooks should be provided so the user can abuse the IRQ
164 ; ------------------------------------------------------------------------
171 ; increment the system tick counter
180 ; Acknowlege interrupt
199 ; ------------------------------------------------------------------------
201 ; ------------------------------------------------------------------------
204 .word _irq2 ; $fff6 IRQ2 (External IRQ, BRK)
205 .word _irq1 ; $fff8 IRQ1 (VDC)
206 .word _timer ; $fffa Timer
207 .word _nmi ; $fffc NMI
208 .word start ; $fffe reset