struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / r2k / crt0.s
blobbeb22f45c458202a4fd4c9c46046cc1ee6a73cca
1 ;--------------------------------------------------------------------------
2 ; crt0.s - Generic crt0.s for a Rabbit 2000
3 ; derived from "Generic crt0.s for a Z80"
5 ; Copyright (C) 2000, Michael Hope
6 ; Modified for Rabbit by Leland Morrison 2011
7 ; Copyright (C) 2020, Philipp Klaus Krause
9 ; This library is free software; you can redistribute it and/or modify it
10 ; under the terms of the GNU General Public License as published by the
11 ; Free Software Foundation; either version 2, or (at your option) any
12 ; later version.
14 ; This library is distributed in the hope that it will be useful,
15 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ; GNU General Public License for more details.
19 ; You should have received a copy of the GNU General Public License
20 ; along with this library; see the file COPYING. If not, write to the
21 ; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
22 ; MA 02110-1301, USA.
24 ; As a special exception, if you link this library with other files,
25 ; some of which are compiled with SDCC, to produce an executable,
26 ; this library does not by itself cause the resulting executable to
27 ; be covered by the GNU General Public License. This exception does
28 ; not however invalidate any other reasons why the executable file
29 ; might be covered by the GNU General Public License.
30 ;--------------------------------------------------------------------------
32 .module crt0
33 .globl _main
34 .globl ___sdcc_external_startup
36 GCSR .equ 0x00 ; Global control / status register
37 MMIDR .equ 0x10
38 STACKSEG .equ 0x11
39 SEGSIZE .equ 0x13
40 MB0CR .equ 0x14 ; Memory Bank 0 Control Register
41 MB1CR .equ 0x15 ; Memory Bank 1 Control Register
42 MB2CR .equ 0x16 ; Memory Bank 2 Control Register
43 MB3CR .equ 0x17 ; Memory Bank 3 Control Register
45 .area _HEADER (ABS)
47 ; Reset vector - assuming smode0 and smode1 input pins are grounded
48 .org 0
50 ; Setup internal interrupts. Upper byte of interrupt vector table address. For compatibility with Rabbit 3000, we choose this even here (Rabbit 2000 allows odd values, so #1 could be used to save space).
51 ld a, #2
52 ld iir, a
54 ; Configure physical address space.
55 ; Leave MB0CR Flash at default slow at /OE0, /CS0
56 ; Assume slow RAM at /CS1, /OE1, /WE1
57 ld a, #0x05
58 ioi
59 ld (MB2CR), a;
61 ; Configure logical address space. 32 KB root segment followed by 8 KB data segment, 16 KB stack segment, 8 KB xpc segment.
62 ; By default, SDCC will use the root segment for code and constant data, stack segment for data (including stack). data segment and xpc segment are then unused.
63 ld a, #0xa8 ; 16 KB stack segment at 0xa000, 8 KB data segment at 0x8000
64 ioi
65 ld (SEGSIZE), a
67 ; Configure mapping to physical address space.
68 ld a, #0x76
69 ioi
70 ld (STACKSEG), a ; stack segment base at 0x76000 + 0xa000 = 0x80000
72 ; Set stack pointer directly above top of stack segment
73 ld sp, #0xe000
75 call ___sdcc_external_startup
77 ; Initialise global variables. Skip if __sdcc_external_startup returned
78 ; non-zero value. Note: calling convention version 1 only.
79 or a, a
80 jr NZ, skip_gsinit
81 call gsinit
82 skip_gsinit:
84 call _main
85 jp _exit
87 ; Periodic Interrupt
88 .org 0x200
89 push af
90 ioi
91 ld a, (GCSR) ; clear interrupt
92 pop af
93 ipres
94 ret
96 ; Secondary Watchdog - Rabbit 3000A only
97 .org 0x210
98 reti
100 ; rst 0x10
101 .org 0x220
104 ; rst 0x18
105 .org 0x230
108 ; rst 0x20
109 .org 0x240
112 ; rst 0x28
113 .org 0x250
116 ; Syscall instruction - Rabbit 3000A only
117 .org 0x260
120 ; rst 0x38
121 .org 0x270
124 ; Slave Port
125 .org 0x280
126 ipres
129 ; Timer A
130 .org 0x2a0
131 ipres
134 ; Timer B
135 .org 0x2b0
136 ipres
139 ; Serial Port A
140 .org 0x2c0
141 reti
143 ; Serial Port B
144 .org 0x2d0
145 ipres
148 ; Serial Port C
149 .org 0x2e0
150 ipres
153 ; Serial Port D
154 .org 0x2f0
155 ipres
158 .org 0x300
160 ;; Ordering of segments for the linker.
161 .area _HOME
162 .area _CODE
163 .area _INITIALIZER
164 .area _GSINIT
165 .area _GSFINAL
167 .area _DATA
168 .area _INITIALIZED
169 .area _BSEG
170 .area _BSS
171 .area _HEAP
173 .area _CODE
174 _exit::
175 ;; Exit - special code to the emulator
176 ld a,#0
177 rst #0x28
179 ;halt ; opcode for halt used for 'altd' on rabbit processors
180 jr 1$
182 .area _GSINIT
183 gsinit::
184 ld bc, #l__DATA
185 ld a, b
186 or a, c
187 jr Z, zeroed_data
188 ld hl, #s__DATA
189 ld (hl), #0x00
190 dec bc
191 ld a, b
192 or a, c
193 jr Z, zeroed_data
194 ld e, l
195 ld d, h
196 inc de
197 zero_loop:
198 ldi ; Work around new ldir wait state bug.
199 jp LO, zero_loop
201 zeroed_data:
203 ld bc, #l__INITIALIZER
204 ld a, b
205 or a, c
206 jr Z, gsinit_next
207 ld de, #s__INITIALIZED
208 ld hl, #s__INITIALIZER
209 copy_loop:
210 ldi ; Work around new ldir wait state bug.
211 jp LO, copy_loop
212 gsinit_next:
214 .area _GSFINAL