1 <!doctype linuxdoc system>
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">
11 An overview over the Atari runtime system as it is implemented for the cc65 C
15 <!-- Table of contents -->
18 <!-- Begin the document -->
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
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
34 <sect>Binary format<p>
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 <2 byte start address> <2 bytes end address>
39 [program bytes]). 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 ($02E0 <run vector>) and is calculated using
46 __CODE_LOAD__ in crt0.s.
49 <sect>Memory layout<p>
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 $2E00 - $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 ($2E5) variable. Then the initial stack pointer,
57 which indicates the upper bound of memory used, is adjusted. The load
58 address of $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
67 The text screen depends on the installed memory size and cartridges
68 and can be obtained from the SAVMSC variable ($58).
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">).
80 The C heap is located at the end of the program and grows towards the C
87 <sect>Platform specific header files<p>
89 Programs containing Atari specific code may use the <tt/atari.h/
93 <sect1>Atari specific functions<p>
95 The functions listed below are special for the Atari. See the <htmlurl
96 url="funcref.html" name="function reference"> for declaration and usage.
113 <sect1>Hardware access<p>
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
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.
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.
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.
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.
148 <sect>Loadable drivers<p>
150 <sect1>Graphics drivers<p>
152 Currently there are no graphics drivers available for the Atari platform.
154 <sect1>Extended memory drivers<p>
156 Currently there are no extended memory drivers available for the Atari
159 <sect1>Joystick drivers<p>
163 <tag><tt/atari-stdjoy.joy/</tag>
164 Supports up to four standard joysticks connected to the joystick ports of
171 <sect1>Mouse drivers<p>
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.
177 <sect1>RS232 device drivers<p>
179 Currently there are no RS232 loadable drivers available for the Atari
180 platform. There is a static driver you can use.
189 <sect1>Function keys<p>
191 These are defined to be Atari + number key.
193 <sect1>Reserving a memory area inside the program<label id="memhole"><p>
195 The Atari 130XE maps its additional memory into CPU memory in 16K
196 chunks at address $4000 to $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.
201 The Atari executable format allows holes inside a program, e.g. one
202 part loads into $2E00 to $3FFF, going below the reserved
203 memory area (assuming a reserved area from $4000 to
204 $7FFF), and another part loads into $8000 to
207 Each load chunk of the executable starts with a 4 byte header which
208 defines its load address and size.
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 $4000 to $7FFF. The CODE segment of
213 the program should go below $4000 and the DATA and RODATA
214 segments should go above $7FFF.
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).
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
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.
233 This is a modified cc65 Atari linker configuration file (split.cfg):
236 ZP: start = $82, size = $7E, type = rw, define = yes;
238 HEADER: start = $0000, size = $6, file = %O; # first load chunk
239 RAMLO: start = $2E00, size = $1200, file = %O;
241 BANK: start = $4000, size = $4000, file = "";
243 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
244 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
247 EXEHDR: load = BANK, type = wprot;
249 NEXEHDR: load = HEADER, type = wprot; # first load chunk
250 CODE: load = RAMLO, type = wprot, define = yes;
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;
257 ZEROPAGE: load = ZP, type = zp;
258 AUTOSTRT: load = RAM, type = wprot; # defines program entry point
261 CONDES: segment = RODATA,
263 label = __CONSTRUCTOR_TABLE__,
264 count = __CONSTRUCTOR_COUNT__;
265 CONDES: segment = RODATA,
267 label = __DESTRUCTOR_TABLE__,
268 count = __DESTRUCTOR_COUNT__;
271 __STACKSIZE__ = $800; # 2K stack
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.
281 The added NEXEHDR segment defines the correct EXE header. It puts only
282 the CODE segment into load chunk #1 (RAMLO memory area).
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
289 The contents of the new NEXEHDR and CHKHDR segments come from this
292 .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
293 .import __DATA_LOAD__, __RODATA_LOAD__
296 .word $FFFF ; EXE file magic number
299 .word __CODE_LOAD__ + __CODE_SIZE__ - 1
302 ; 2nd load chunk (contains with AUTOSTRT in fact a 3rd load chunk)
303 .word __RODATA_LOAD__
304 .word __BSS_LOAD__ - 1
309 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
312 <sect2>Low data and high code example<p>
315 Goal: Put RODATA and DATA into low memory and CODE with BSS into high
320 ZP: start = $82, size = $7E, type = rw, define = yes;
322 HEADER: start = $0000, size = $6, file = %O; # first load chunk
323 RAMLO: start = $2E00, size = $1200, file = %O;
325 BANK: start = $4000, size = $4000, file = "";
327 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
328 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
331 EXEHDR: load = BANK, type = wprot; # discarded old EXE header
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;
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;
341 ZEROPAGE: load = ZP, type = zp;
342 AUTOSTRT: load = RAM, type = wprot; # defines program entry point
345 CONDES: segment = RODATA,
347 label = __CONSTRUCTOR_TABLE__,
348 count = __CONSTRUCTOR_COUNT__;
349 CONDES: segment = RODATA,
351 label = __DESTRUCTOR_TABLE__,
352 count = __DESTRUCTOR_COUNT__;
355 __STACKSIZE__ = $800; # 2K stack
359 New contents for NEXEHDR and CHKHDR are needed (split2.s):
361 .import __CODE_LOAD__, __BSS_LOAD__, __DATA_SIZE__
362 .import __DATA_LOAD__, __RODATA_LOAD__
366 .word __RODATA_LOAD__
367 .word __DATA_LOAD__ + __DATA_SIZE__ - 1
371 .word __BSS_LOAD__ - 1
376 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
379 <sect2>Final note<label id="memhole_final_note"><p>
381 There are two other memory areas which don't appear directly in the
382 linker script. They are the stack and the heap.
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__.
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.
392 common/_heap.s defines the location of the heap and atari/crt0.s
393 defines the location of the stack by initializing sp.
396 <sect>Bugs/Feedback<p>
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">).
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.
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:
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