/
cstartup.asm
280 lines (237 loc) · 9.08 KB
/
cstartup.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
;==============================================================================
; Cyan Technology Ltd
;
; FILE
; cstartup.asm - Assembler startup for C programs.
;
; DESCRIPTION
; Defines C segments. Contains initial code called before C is started
;==============================================================================
MODULE cstartup
.ALL
;
; C reserves DATA address 0 for the NULL pointer. The value H'DEAD is put in
; here so that it is easier to spot the effect of a NULL pointer during
; debugging. User memory for constants grows upwards from H'0001. The first
; address is given the equate symbol $??LO_ADDR.
;
.SEG C_RESERVED1
ORG 0
dc H'DEAD
$??LO_ADDR EQU $
;
; DATA addresses H'EFE0-H'EFFF are used for scratchpad RAM in interrupt mode.
; DATA addresses H'EFC0-H'EFDF are used for scratchpad RAM in user mode.
; DATA addresses H'EFB8-H'EFBF are used for register storage in interrupt mode.
; Then follows the interrupt stack, user stack and user heap.
; User memory for variables grows downwards from the end of the user stack.
; This version of cstartup only contains one area of scratchpad RAM
; which constrains users not to write re-entrant re-interruptable
; code.
; The interrupt stack must start at IY-38 to be compatible with the C compiler.
;
.SEG C_RESERVED2
ORG H'EFB8
$??HI_ADDR DEQU $
ds 8 ; Interrupt register storage
ds 32 ; User Scratchpad
IY_SCRATCH DEQU $
$?irq_scratchpad? DEQU $
ds 32 ; Interrupt Scratchpad
;
; The registers that control the functional blocks of the eCOG1 are located
; at addresses H'FEA0 to H'FFCF. The C header file <ecog1.h> declares an
; external structure that descibes the registers. This variable is defined
; below.
;
.SEG REGISTERS
ORG H'FEA0
$fd:
$rg:
;
; C requires the following segments:
; CONST - Constants in ROM. For example:
; const char c = 'c' ;
; printf( "Hello World!" ) ;
; VAR - Variables in RAM. These are set to zero by the cstartup code.
; For example:
; int i ; (in file scope)
; static int i ; (in function scope)
; INIT - Initialisd variables in RAM. For example:
; int i = 9 ; (in file scope)
; static int i = 9 ; (in function scope)
; INITC - Initialisation data for the INIT segment
; HEAP - The heap. Required if malloc() etc. are used.
; STACK - The stack. Always required.
;
; The memory allocated to each segment is defined by the value of
; $??<segment_name>_SIZE as set below. These sizes can be set manually or, if
; the appropriate line is tagged with !PACK and the -pack option is specified
; to ECOGCL, ECOGCL will write in the size actually required for the segment.
; The sizes of the STACK and HEAP segments must be set by the user.
;
$??ISTACK_SIZE = H'0040
$??STACK_SIZE = H'0100
$??HEAP_SIZE = H'0080
; -- Locate DATA segments in memory --
;
; Segments are allocated sequentially by the ??ALLOCATE macro. They may be
; set at fixed addresses by setting ADDR prior to calling ??ALLOCATE.
;
??ALLOCATE MACRO seg
.SEG &seg
ORG ADDR
$??&seg!_LO = ADDR
ADDR = ADDR + $??&seg!_SIZE
$??&seg!_HI = ADDR-1
ENDMAC
; Allocate DATA ROM
ADDR = $??LO_ADDR
??ALLOCATE INITC
??ALLOCATE CONST
; Allocate DATA RAM
ADDR = $??HI_ADDR
IFDEF $??VAR_SIZE
ADDR = ADDR - $??VAR_SIZE - $??INIT_SIZE
ENDIF
ADDR = ADDR - $??ISTACK_SIZE - $??STACK_SIZE - $??HEAP_SIZE
??ALLOCATE INIT
??ALLOCATE VAR
??ALLOCATE HEAP
??ALLOCATE STACK
??ALLOCATE ISTACK
; -- Memory initialisation macros --
;
; Segments may be initialised by filling with a constant value using the
; ??SEGFILL macro. Two symbols are passed, the segment name and the value to
; fill with. A third symbol (the size) is assumed.
;
??SEGFILL MACRO seg, value
LOCAL fill_loop
IF $??&seg!_SIZE
ld x, #$??&seg
ld al, #$??&seg!_SIZE
ld ah, &value
&fill_loop: st ah, @(0,x)
add x, #1
sub al, #1
bne &fill_loop
ENDIF
ENDMAC
;
; Segments may be initialised by copying an initialisation segment with
; the ??SEGCOPY macro. Two symbols are passed, the source and destination
; segment names.
;
??SEGCOPY MACRO src, dest
IF $??&src!_SIZE
ld x, #$??&src
ld y, #$??&dest
ld al, #$??&src!_SIZE
bc
ENDIF
ENDMAC
;
; Fills a block of memory with a value. Three values are passed, the start
; address for the block, the number of addresses to write to and the value
; to be written.
;
??MEMFILL MACRO start, length, value
LOCAL fill_loop
ld x, &start
ld al, &length
ld ah, &value
&fill_loop: st ah, @(0,x)
add x, #1
sub al, #1
bne &fill_loop
ENDMAC
;
; Input argument for main().
;
.SEG CONST
argv dc 0,0 ; NULL as two-word byte address
;
; Start of Code.
;
.CODE
ORG H'40
$?cstart_code:
bra $ecog1ConfigMMU ; configure MMU and Cache Banks
$ecog1ConfigContinue:
;
; Initialise segments. The HEAP and STACK are filled with H'9999 and H'aaaa
; respectively so that their maximum runtime extents can be checked. The
; INIT segment is set from the ROM initialisers in the INITC segment. The non
; initialised RAM segment VAR is set to zero (compiler puts 0 initialised
; variables in these segments as well as uninitialised ones,x).
;
??SEGFILL HEAP, #h'9999
??SEGFILL STACK, #h'AAAA
??SEGFILL ISTACK, #h'BBBB
??SEGCOPY INITC, INIT
??SEGFILL VAR, #h'0
; Set interrupt stack pointer.
ld y, #IY_SCRATCH
; Set user mode flag to allow interupts.
st flags, @(-1,y)
ld al, @(-1,y)
or al, #h'10
st al, @(-1,y)
ld flags, @(-1,y)
; Set usermode stack pointer
ld y, #$??STACK_HI
; Call ecog1Config to setup eCOG1 peripherals
; Defined in module produced by configuration compiler
bsr $ecog1Config
; Call main, setting argc and argv[0] to 0.
ld ah, #argv
ld al, #0
bsr $main
;
; Main may exit by returning or by explicitly calling $exit. In either case
; exit code will be in AL.
;
$exit:
brk ; Alert the user if in debug mode
bra 0 ; Restart
;
; This is the minimal interrupt routine. The contents of FLAGS is restored
; as the program counter is restored using rti.
;
$minimal_handler:
st flags,@(-33,y) ; Store Flags
st al, @(-34,y) ; Store AL
ld al, @(-33,y) ; Put Flags into AL
or al, #h'0010 ; Set usermode
st al, @(-33,y) ; Store the value to be restored to Flags
brk ; Alert the user if in debug mode
ld al, @(-34,y) ; Restore AL
rti @(-33,y) ; Restore PC and Flags
;
; The address exception can happen often during development. A handler
; is put here to catch the exception.
;
$address_error:
st flags,@(-33,y) ; Store Flags
st al, @(-34,y) ; Store AL
ld al, @(-33,y) ; Put Flags into AL
or al, #h'0010 ; Set usermode
st al, @(-33,y) ; Store the value to be restored to Flags
brk ; Alert the user if in debug mode
ld al, #h'a
st al, @h'ff69 ; Clear status in mmu.address_exception
ld al, #h'200
st al, @h'ff7a ; Clear status in emi.ctrl_sts
ld al, @(-34,y) ; Restore AL
rti @(-33,y) ; Restore PC and Flags
; End of startup code
$??CSTARTUP_END EQU $
; Reserve storage for stacks
.SEG HEAP
DS $??HEAP_SIZE
.SEG STACK
DS $??STACK_SIZE
.SEG ISTACK
DS $??ISTACK_SIZE
ENDMOD