]> git.sur5r.net Git - cc65/blob - doc/atari.sgml
add example of a memory configuration with holes; add cpg@aladdin.de
[cc65] / doc / atari.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
5 <title>Atari specific information for cc65
6 <author>Shawn Jefferson, <htmlurl url="mailto:shawnjefferson@24fightingchickens.com" name="shawnjefferson@24fightingchickens.com">
7 Christian Groessler, <htmlurl url="mailto:cpg@aladdin.de" name="cpg@aladdin.de">
8 <date>14-Sep-2004
9
10 <abstract>
11 An overview over the Atari runtime system as it is implemented for the cc65 C
12 compiler.
13 </abstract>
14
15 <!-- Table of contents -->
16 <toc>
17
18 <!-- Begin the document -->
19
20 <sect>Overview<p>
21
22 This file contains an overview of the Atari runtime system as it comes
23 with the cc65 C compiler. It describes the memory layout, Atari specific
24 header files, available drivers, and any pitfalls specific to that
25 platform.
26
27 Please note that Atari specific functions are just mentioned here, they are
28 described in detail in the separate <htmlurl url="funcref.html" name="function
29 reference">. Even functions marked as "platform dependent" may be available on
30 more than one platform. Please see the function reference for more
31 information.
32
33
34 <sect>Binary format<p>
35
36 The standard binary output format generated by the linker for the
37 Atari target is a machine language program with a standard executable
38 header (FF FF &lt;2 byte start address&gt; &lt;2 bytes end address&gt;
39 &lsqb;program bytes&rsqb;). These values are calculated in the crt0.s
40 file from the __CODE_LOAD__ and __BSS_LOAD__ values, so keep this in
41 mind if you create a custom linker config file and start moving
42 segments around (see section <ref name="Reserving a memory area inside the program" id="memhole">).  You can
43 override this behaviour by creating your own crt0.s file and linking
44 it into your program.  A run vector is added to the end of the file
45 (&dollar;02E0 &lt;run vector&gt;) and is calculated using
46 __CODE_LOAD__ in crt0.s.
47
48
49 <sect>Memory layout<p>
50
51 The default linker script assumes that the BASIC ROM is disabled (or
52 the BASIC cartridge unplugged). This gives a usable memory range from
53 &dollar;2E00 - &dollar;BC1F. The library startup code examines the
54 current memory configuration, which depends on the size of the
55 installed memory and cartridges present, by inspecting the value in
56 the MEMTOP (&dollar;2E5) variable. Then the initial stack pointer,
57 which indicates the upper bound of memory used, is adjusted. The load
58 address of &dollar;2E00 was chosen to accommodate having a DOS loaded
59 and a driver that resides in low memory such as the 850 R: handler.
60 You can override this behaviour by creating a custom linker config
61 file.
62
63 Special locations:
64
65 <descrip>
66   <tag/Text screen/
67   The text screen depends on the installed memory size and cartridges
68   and can be obtained from the SAVMSC variable (&dollar;58).
69
70   <tag/Stack/
71   The C runtime stack is located at MEMTOP and grows downwards,
72   regardless of how your linker config file is setup.  This
73   accomodates the different memory configurations of the Atari
74   machines, as well as having a cartridge installed.  You can override
75   this behaviour by writing your own crt0.s file and linking it to
76   your program (see also <ref name="Final note"
77   id="memhole_final_note">).
78
79   <tag/Heap/
80   The C heap is located at the end of the program and grows towards the C
81   runtime stack.
82
83 </descrip><p>
84
85
86
87 <sect>Platform specific header files<p>
88
89 Programs containing Atari specific code may use the <tt/atari.h/
90 header file.
91
92
93 <sect1>Atari specific functions<p>
94
95 The functions listed below are special for the Atari. See the <htmlurl
96 url="funcref.html" name="function reference"> for declaration and usage.
97
98 <itemize>
99 <item>get_ostype
100 <item>get_tv
101 <item>_gtia_mkcolor
102 <item>_getcolor
103 <item>_getdefdev
104 <item>_graphics
105 <item>_rest_vecs
106 <item>_save_vecs
107 <item>_scroll
108 <item>_setcolor
109 <item>_setcolor_low
110 </itemize>
111
112
113 <sect1>Hardware access<p>
114
115 The following pseudo variables declared in the <tt/atari.h/ header
116 file do allow access to hardware located in the address space. Some
117 variables are structures, accessing the struct fields will access the
118 chip registers.
119
120 <descrip>
121
122   <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
123   The <tt/GTIA_READ/ structure allows read access to the GTIA. The
124   <tt/GTIA_WRITE/ structure allows write access to the GTIA.
125   See the <tt/_gtia.h/ header file located in the include directory
126   for the declaration of the structure.
127
128   <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
129   The <tt/POKEY_READ/ structure allows read access to the POKEY. The
130   <tt/POKEY_WRITE/ structure allows write access to the POKEY.
131   See the <tt/_pokey.h/ header file located in the include directory
132   for the declaration of the structure.
133
134   <tag><tt/ANTIC/</tag>
135   The <tt/ANTIC/ structure allows read access to the ANTIC.
136   See the <tt/_antic.h/ header file located in the include directory
137   for the declaration of the structure.
138
139   <tag><tt/PIA/</tag>
140   The <tt/PIA/ structure allows read access to the PIA 6520.
141   See the <tt/_pia.h/ header file located in the include directory
142   for the declaration of the structure.
143
144 </descrip><p>
145
146
147
148 <sect>Loadable drivers<p>
149
150 <sect1>Graphics drivers<p>
151
152 Currently there are no graphics drivers available for the Atari platform.
153
154 <sect1>Extended memory drivers<p>
155
156 Currently there are no extended memory drivers available for the Atari
157 platform.
158
159 <sect1>Joystick drivers<p>
160
161 <descrip>
162
163   <tag><tt/atari-stdjoy.joy/</tag>
164   Supports up to four standard joysticks connected to the joystick ports of
165   the Atari.
166
167 </descrip><p>
168
169
170
171 <sect1>Mouse drivers<p>
172
173 Currently no drivers available (in fact, the API for loadable mouse drivers
174 does not exist). There is a static driver you can use.
175
176
177 <sect1>RS232 device drivers<p>
178
179 Currently there are no RS232 loadable drivers available for the Atari
180 platform. There is a static driver you can use.
181
182
183 <sect>Limitations<p>
184
185
186
187 <sect>Other hints<p>
188
189 <sect1>Function keys<p>
190
191 These are defined to be Atari + number key.
192
193 <sect1>Reserving a memory area inside the program<label id="memhole"><p>
194
195 The Atari 130XE maps its additional memory into CPU memory in 16K
196 chunks at address &dollar;4000 to &dollar;7FFF. One might want to
197 prevent this memory area from being used by cc65. Other reasons to
198 prevent the use of some memory area could be the buffers for display
199 lists and screen memory.
200 <p>
201 The Atari executable format allows holes inside a program, e.g. one
202 part loads into &dollar;2E00 to &dollar;3FFF, going below the reserved
203 memory area (assuming a reserved area from &dollar;4000 to
204 &dollar;7FFF), and another part loads into &dollar;8000 to
205 &dollar;BC1F.
206 <p>
207 Each load chunk of the executable starts with a 4 byte header which
208 defines its load address and size.
209 <p>
210 <sect2>Low code and high data example<p>
211 Goal: Create an executable with 2 load chunks which doesn't use the
212 memory area from &dollar;4000 to &dollar;7FFF. The CODE segment of
213 the program should go below &dollar;4000 and the DATA and RODATA
214 segments should go above &dollar;7FFF.
215 <p>
216 The main problem is that the EXE header generated by the cc65 runtine
217 lib is wrong. It defines a single load chunk with the sizes/addresses
218 of the CODE, RODATA, and DATA segments (the whole user program).
219 <p>
220 The contents of the EXE header come from the EXEHDR segment, which is
221 defined in crt0.s. This cannot be changed w/o modifiying and
222 recompiling the cc65 atari runtime lib. Therefore the original EXE
223 header must be discarded. It will be replaced by a user created
224 one.
225 <p>
226 The user needs to create a customized linker config file which adds
227 new memory areas and segments to hold the new EXE header and added
228 load chunk header data. Also an assembly source file needs to be
229 created which defines the contents of the new EXE header and the
230 second load chunk header.
231 <p>
232 <p>
233 This is a modified cc65 Atari linker configuration file (split.cfg):
234 <tscreen><verb>
235 MEMORY {
236     ZP: start = $82, size = $7E, type = rw, define = yes;
237
238     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
239     RAMLO: start = $2E00, size = $1200, file = %O;
240
241     BANK: start = $4000, size = $4000, file = "";
242
243     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
244     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
245 }
246 SEGMENTS {
247     EXEHDR: load = BANK, type = wprot;
248
249     NEXEHDR: load = HEADER, type = wprot;               # first load chunk
250     CODE: load = RAMLO, type = wprot, define = yes;
251
252     CHKHDR: load = SECHDR, type = wprot;                # second load chunk
253     RODATA: load = RAM, type = wprot, define = yes;
254     DATA: load = RAM, type = rw, define = yes;
255     BSS: load = RAM, type = bss, define = yes;
256
257     ZEROPAGE: load = ZP, type = zp;
258     AUTOSTRT: load = RAM, type = wprot;                 # defines program entry point
259 }
260 FEATURES {
261     CONDES: segment = RODATA,
262             type = constructor,
263             label = __CONSTRUCTOR_TABLE__,
264             count = __CONSTRUCTOR_COUNT__;
265     CONDES: segment = RODATA,
266             type = destructor,
267             label = __DESTRUCTOR_TABLE__,
268             count = __DESTRUCTOR_COUNT__;
269 }
270 SYMBOLS {
271     __STACKSIZE__ = $800;       # 2K stack
272 }
273 </verb></tscreen>
274 <p>
275
276 A new memory area BANK was added which describes the reserved area.
277 It gets loaded with the contents of the old EXEHDR segment. But the
278 memory area isn't written to the output file. This way the contents of
279 the EXEHDR segment get discarded.
280 <p>
281 The added NEXEHDR segment defines the correct EXE header. It puts only
282 the CODE segment into load chunk #1 (RAMLO memory area).
283 <p>
284 The header for the second load chunk comes from the new CHKHDR
285 segment. It puts the RODATA and DATA segments into load chunk #2 (RAM
286 memory area).
287 <p>
288 <p>
289 The contents of the new NEXEHDR and CHKHDR segments come from this
290 file (split.s):
291 <tscreen><verb>
292         .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
293         .import __DATA_LOAD__, __RODATA_LOAD__
294
295         .segment "NEXEHDR"
296         .word    $FFFF          ; EXE file magic number
297         ; 1st load chunk
298         .word    __CODE_LOAD__
299         .word    __CODE_LOAD__ + __CODE_SIZE__ - 1
300
301         .segment "CHKHDR"
302         ; 2nd load chunk (contains with AUTOSTRT in fact a 3rd load chunk)
303         .word    __RODATA_LOAD__
304         .word    __BSS_LOAD__ - 1
305 </verb></tscreen>
306 <p>
307 Compile with
308 <tscreen><verb>
309 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
310 </verb></tscreen>
311
312 <sect2>Low data and high code example<p>
313
314
315 Goal: Put RODATA and DATA into low memory and CODE with BSS into high
316 memory (split2.cfg):
317
318 <tscreen><verb>
319 MEMORY {
320     ZP: start = $82, size = $7E, type = rw, define = yes;
321
322     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
323     RAMLO: start = $2E00, size = $1200, file = %O;
324
325     BANK: start = $4000, size = $4000, file = "";
326
327     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
328     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
329 }
330 SEGMENTS {
331     EXEHDR: load = BANK, type = wprot;                  # discarded old EXE header
332
333     NEXEHDR: load = HEADER, type = wprot;               # first load chunk
334     RODATA: load = RAMLO, type = wprot, define = yes;
335     DATA: load = RAMLO, type = rw, define = yes;
336
337     CHKHDR: load = SECHDR, type = wprot;                # second load chunk
338     CODE: load = RAM, type = wprot, define = yes;
339     BSS: load = RAM, type = bss, define = yes;
340
341     ZEROPAGE: load = ZP, type = zp;
342     AUTOSTRT: load = RAM, type = wprot;                 # defines program entry point
343 }
344 FEATURES {
345     CONDES: segment = RODATA,
346             type = constructor,
347             label = __CONSTRUCTOR_TABLE__,
348             count = __CONSTRUCTOR_COUNT__;
349     CONDES: segment = RODATA,
350             type = destructor,
351             label = __DESTRUCTOR_TABLE__,
352             count = __DESTRUCTOR_COUNT__;
353 }
354 SYMBOLS {
355     __STACKSIZE__ = $800;       # 2K stack
356 }
357 </verb></tscreen>
358
359 New contents for NEXEHDR and CHKHDR are needed (split2.s):
360 <tscreen><verb>
361         .import __CODE_LOAD__, __BSS_LOAD__, __DATA_SIZE__
362         .import __DATA_LOAD__, __RODATA_LOAD__
363
364         .segment "NEXEHDR"
365         .word    $FFFF
366         .word    __RODATA_LOAD__
367         .word    __DATA_LOAD__ + __DATA_SIZE__ - 1
368
369         .segment "CHKHDR"
370         .word    __CODE_LOAD__
371         .word    __BSS_LOAD__ - 1
372 </verb></tscreen>
373
374 Compile with
375 <tscreen><verb>
376 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
377 </verb></tscreen>
378
379 <sect2>Final note<label id="memhole_final_note"><p>
380
381 There are two other memory areas which don't appear directly in the
382 linker script. They are the stack and the heap.
383
384 The cc65 runtime lib places the stack location at the end of available
385 memory. This is dynamically set from the MEMTOP system variable at
386 startup. The heap is located in the area between the end of the BSS
387 segment and the top of the stack as defined by __STACKSIZE__.
388
389 If BSS and/or the stack shouldn't stay at the end of the program,
390 some parts of the cc65 runtime lib need to be replaced/modified.
391
392 common/_heap.s defines the location of the heap and atari/crt0.s
393 defines the location of the stack by initializing sp.
394
395
396 <sect>Bugs/Feedback<p>
397
398 If you have problems using the library, if you find any bugs, or if you're
399 doing something interesting with it, I would be glad to hear from you. Feel
400 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
401 name="uz@cc65.org"> or <htmlurl url="mailto:cpg@aladdin.de"
402 name="cpg@aladdin.de">).
403
404
405
406 <sect>License<p>
407
408 This software is provided 'as-is', without any expressed or implied
409 warranty.  In no event will the authors be held liable for any damages
410 arising from the use of this software.
411
412 Permission is granted to anyone to use this software for any purpose,
413 including commercial applications, and to alter it and redistribute it
414 freely, subject to the following restrictions:
415
416 <enum>
417 <item>  The origin of this software must not be misrepresented; you must not
418         claim that you wrote the original software. If you use this software
419         in a product, an acknowledgment in the product documentation would be
420         appreciated but is not required.
421 <item>  Altered source versions must be plainly marked as such, and must not
422         be misrepresented as being the original software.
423 <item>  This notice may not be removed or altered from any source
424         distribution.
425 </enum>
426
427 </article>