1 <!doctype linuxdoc system>
5 <title>Atari specific information for cc65
6 <author>Shawn Jefferson, <htmlurl
7 url="mailto:shawnjefferson@24fightingchickens.com"
8 name="shawnjefferson@24fightingchickens.com"> and
9 Christian Groessler, <htmlurl url="mailto:cpg@aladdin.de" name="cpg@aladdin.de">
13 An overview over the Atari runtime system as it is implemented for the cc65 C
17 <!-- Table of contents -->
20 <!-- Begin the document -->
24 This file contains an overview of the Atari runtime system as it comes
25 with the cc65 C compiler. It describes the memory layout, Atari specific
26 header files, available drivers, and any pitfalls specific to that
29 Please note that Atari specific functions are just mentioned here, they are
30 described in detail in the separate <htmlurl url="funcref.html" name="function
31 reference">. Even functions marked as "platform dependent" may be available on
32 more than one platform. Please see the function reference for more
36 <sect>Binary format<p>
38 The standard binary output format generated by the linker for the
39 Atari target is a machine language program with a standard executable
40 header (FF FF <2 byte start address> <2 bytes end address>
41 [program bytes]). These values are calculated in the crt0.s
42 file from the __CODE_LOAD__ and __BSS_LOAD__ values, so keep this in
43 mind if you create a custom linker config file and start moving
44 segments around (see section <ref name="Reserving a memory area inside the program" id="memhole">). You can
45 override this behaviour by creating your own crt0.s file and linking
46 it into your program. A run vector is added to the end of the file
47 ($02E0 <run vector>) and is calculated using
48 __CODE_LOAD__ in crt0.s.
51 <sect>Memory layout<p>
53 The default linker script assumes that the BASIC ROM is disabled (or
54 the BASIC cartridge unplugged). This gives a usable memory range from
55 $2E00 - $BC1F. The library startup code examines the
56 current memory configuration, which depends on the size of the
57 installed memory and cartridges present, by inspecting the value in
58 the MEMTOP ($2E5) variable. Then the initial stack pointer,
59 which indicates the upper bound of memory used, is adjusted. The load
60 address of $2E00 was chosen to accommodate having a DOS loaded
61 and a driver that resides in low memory such as the 850 R: handler.
62 You can override this behaviour by creating a custom linker config
69 The text screen depends on the installed memory size and cartridges
70 and can be obtained from the SAVMSC variable ($58).
73 The C runtime stack is located at MEMTOP and grows downwards,
74 regardless of how your linker config file is setup. This
75 accommodates the different memory configurations of the Atari
76 machines, as well as having a cartridge installed. You can override
77 this behaviour by writing your own crt0.s file and linking it to
78 your program (see also <ref name="Final note"
79 id="memhole_final_note">).
82 The C heap is located at the end of the program and grows towards the C
89 <sect>Platform specific header files<p>
91 Programs containing Atari specific code may use the <tt/atari.h/
95 <sect1>Atari specific functions<p>
97 The functions listed below are special for the Atari. See the <htmlurl
98 url="funcref.html" name="function reference"> for declaration and usage.
115 <sect1>Hardware access<p>
117 The following pseudo variables declared in the <tt/atari.h/ header
118 file do allow access to hardware located in the address space. Some
119 variables are structures, accessing the struct fields will access the
124 <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
125 The <tt/GTIA_READ/ structure allows read access to the GTIA. The
126 <tt/GTIA_WRITE/ structure allows write access to the GTIA.
127 See the <tt/_gtia.h/ header file located in the include directory
128 for the declaration of the structure.
130 <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
131 The <tt/POKEY_READ/ structure allows read access to the POKEY. The
132 <tt/POKEY_WRITE/ structure allows write access to the POKEY.
133 See the <tt/_pokey.h/ header file located in the include directory
134 for the declaration of the structure.
136 <tag><tt/ANTIC/</tag>
137 The <tt/ANTIC/ structure allows read access to the ANTIC.
138 See the <tt/_antic.h/ header file located in the include directory
139 for the declaration of the structure.
142 The <tt/PIA/ structure allows read access to the PIA 6520.
143 See the <tt/_pia.h/ header file located in the include directory
144 for the declaration of the structure.
150 <sect>Loadable drivers<p>
152 <sect1>Graphics drivers<p>
154 Currently there are no graphics drivers available for the Atari platform.
155 However, the runtime library provides a function named _graphics, with
156 a mode parameter just like the BASIC GRAPHICS command. This function will
157 switch to the requested graphics mode.
158 There are currently no functions available to access the graphics
159 memory. The access must be implemented manually.
161 Many graphics modes require more memory than the text screen which is
162 in effect when the program starts up. Therefore the programmer has to
163 tell the program beforehand the memory requirements of the graphics
164 modes the program intends to use.
165 This can be done by using the __RESERVED_MEMORY__ linker config
166 variable. The number specified there describes the number of bytes to
167 subtract from the top of available memory as seen from the runtime
168 library. This memory is then used by the screen buffer.
170 The numbers for the different graphics modes presented below should
171 only be seen as a rule of thumb. Since the screen buffer memory needs
172 to start at specific boundaries, the numbers depend on the current top
174 The following numbers were determined by a BASIC program.
178 graphics mode|reserved memory@<hline>
212 <caption>reserved memory required for different graphics modes
215 The values of "1" are needed because the graphics command crashes if
216 it doesn't have at least one byte available. This seems to be a bug of
219 <sect1>Extended memory drivers<p>
221 Currently there are no extended memory drivers available for the Atari
224 <sect1>Joystick drivers<p>
228 <tag><tt/ataristd.joy/</tag>
229 Supports up to four standard joysticks connected to the joystick ports of
232 <tag><tt/atarim8.joy/</tag>
233 Supports up to eight standard joysticks connected to a MultiJoy adapter.
239 <sect1>Mouse drivers<p>
241 Currently no drivers available (in fact, the API for loadable mouse drivers
242 does not exist). There is a static driver you can use.
245 <sect1>RS232 device drivers<p>
247 Currently there are no RS232 loadable drivers available for the Atari
248 platform. There is a static driver you can use.
254 <sect>DIO implementation<label id="dio"><p>
256 The Atari supports disk drives with either 128 or 256 byte sectors.
257 The first three sectors of any disk are always 128 bytes long though. This is
258 because the system can only boot from 128 bytes sectors.
260 Therefore the DIO read and write functions transfer only 128 bytes
261 for sectors 1 to 3, regardless of the type of diskette.
264 <sect>CONIO implementation<label id="conio"><p>
266 The console I/O is speed optimized therefore support for XEP80 hardware
267 or f80.com software is missing. Of course you may use stdio.h functions.
272 <sect1>Function keys<p>
274 These are defined to be Atari + number key.
276 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
278 The Atari 130XE maps its additional memory into CPU memory in 16K
279 chunks at address $4000 to $7FFF. One might want to
280 prevent this memory area from being used by cc65. Other reasons to
281 prevent the use of some memory area could be the buffers for display
282 lists and screen memory.
284 The Atari executable format allows holes inside a program, e.g. one
285 part loads into $2E00 to $3FFF, going below the reserved
286 memory area (assuming a reserved area from $4000 to
287 $7FFF), and another part loads into $8000 to
290 Each load chunk of the executable starts with a 4 byte header which
291 defines its load address and size.
293 <sect2>Low code and high data example<p>
294 Goal: Create an executable with 2 load chunks which doesn't use the
295 memory area from $4000 to $7FFF. The CODE segment of
296 the program should go below $4000 and the DATA and RODATA
297 segments should go above $7FFF.
299 The main problem is that the EXE header generated by the cc65 runtime
300 lib is wrong. It defines a single load chunk with the sizes/addresses
301 of the LOWCODE, INIT, CODE, RODATA, and DATA segments (the whole user
304 The contents of the EXE header come from the EXEHDR segment, which is
305 defined in crt0.s. This cannot be changed w/o modifying and
306 recompiling the cc65 atari runtime lib. Therefore the original EXE
307 header must be discarded. It will be replaced by a user created
310 The user needs to create a customized linker config file which adds
311 new memory areas and segments to hold the new EXE header and the
312 header data for the second load chunk. Also an assembly source file
313 needs to be created which defines the contents of the new EXE header
314 and the second load chunk header.
317 This is a modified cc65 Atari linker configuration file (split.cfg):
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
329 TRAILER: start = $0000, size = $0006, file = %O;
332 EXEHDR: load = BANK, type = ro;
334 NEXEHDR: load = HEADER, type = ro; # first load chunk
335 LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
336 INIT: load = RAMLO, type = ro, optional = yes;
337 CODE: load = RAMLO, type = ro, define = yes;
339 CHKHDR: load = SECHDR, type = ro; # second load chunk
340 RODATA: load = RAM, type = ro, define = yes;
341 DATA: load = RAM, type = rw, define = yes;
342 BSS: load = RAM, type = bss, define = yes;
344 ZEROPAGE: load = ZP, type = zp;
345 AUTOSTRT: load = TRAILER, type = ro; # defines program entry point
348 CONDES: segment = RODATA,
350 label = __CONSTRUCTOR_TABLE__,
351 count = __CONSTRUCTOR_COUNT__;
352 CONDES: segment = RODATA,
354 label = __DESTRUCTOR_TABLE__,
355 count = __DESTRUCTOR_COUNT__;
358 __STACKSIZE__ = $800; # 2K stack
359 __RESERVED_MEMORY__: value = $0, weak = yes;
364 A new memory area BANK was added which describes the reserved area.
365 It gets loaded with the contents of the old EXEHDR segment. But the
366 memory area isn't written to the output file. This way the contents of
367 the EXEHDR segment get discarded.
369 The added NEXEHDR segment defines the correct EXE header. It puts only
370 the CODE segment into load chunk #1 (RAMLO memory area).
372 The header for the second load chunk comes from the new CHKHDR
373 segment. It puts the RODATA and DATA segments into load chunk #2 (RAM
377 The contents of the new NEXEHDR and CHKHDR segments come from this
380 .import __LOWCODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
381 .import __CODE_LOAD__, __DATA_LOAD__, __RODATA_LOAD__
384 .word $FFFF ; EXE file magic number
386 .word __LOWCODE_LOAD__
387 .word __CODE_LOAD__ + __CODE_SIZE__ - 1
390 ; 2nd load chunk (contains with AUTOSTRT in fact a 3rd load chunk)
391 .word __RODATA_LOAD__
392 .word __BSS_LOAD__ - 1
397 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
400 <sect2>Low data and high code example<p>
403 Goal: Put RODATA and DATA into low memory and LOWCODE, INIT, CODE, BSS
404 into high memory (split2.cfg):
408 ZP: start = $82, size = $7E, type = rw, define = yes;
410 HEADER: start = $0000, size = $6, file = %O; # first load chunk
411 RAMLO: start = $2E00, size = $1200, file = %O;
413 BANK: start = $4000, size = $4000, file = "";
415 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
416 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
417 TRAILER: start = $0000, size = $0006, file = %O;
420 EXEHDR: load = BANK, type = ro; # discarded old EXE header
422 NEXEHDR: load = HEADER, type = ro; # first load chunk
423 RODATA: load = RAMLO, type = ro, define = yes;
424 DATA: load = RAMLO, type = rw, define = yes;
426 CHKHDR: load = SECHDR, type = ro; # second load chunk
427 LOWCODE: load = RAM, type = ro, define = yes, optional = yes;
428 INIT: load = RAM, type = ro, optional = yes;
429 CODE: load = RAM, type = ro, define = yes;
430 BSS: load = RAM, type = bss, define = yes;
432 ZEROPAGE: load = ZP, type = zp;
433 AUTOSTRT: load = TRAILER, type = ro; # defines program entry point
436 CONDES: segment = RODATA,
438 label = __CONSTRUCTOR_TABLE__,
439 count = __CONSTRUCTOR_COUNT__;
440 CONDES: segment = RODATA,
442 label = __DESTRUCTOR_TABLE__,
443 count = __DESTRUCTOR_COUNT__;
446 __STACKSIZE__ = $800; # 2K stack
447 __RESERVED_MEMORY__: value = $0, weak = yes;
451 New contents for NEXEHDR and CHKHDR are needed (split2.s):
453 .import __LOWCODE_LOAD__, __BSS_LOAD__, __DATA_SIZE__
454 .import __DATA_LOAD__, __RODATA_LOAD__
458 .word __RODATA_LOAD__
459 .word __DATA_LOAD__ + __DATA_SIZE__ - 1
462 .word __LOWCODE_LOAD__
463 .word __BSS_LOAD__ - 1
468 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
471 <sect2>Final note<label id="memhole_final_note"><p>
473 There are two other memory areas which don't appear directly in the
474 linker script. They are the stack and the heap.
476 The cc65 runtime lib places the stack location at the end of available
477 memory. This is dynamically set from the MEMTOP system variable at
478 startup. The heap is located in the area between the end of the BSS
479 segment and the top of the stack as defined by __STACKSIZE__.
481 If BSS and/or the stack shouldn't stay at the end of the program,
482 some parts of the cc65 runtime lib need to be replaced/modified.
484 common/_heap.s defines the location of the heap and atari/crt0.s
485 defines the location of the stack by initializing sp.
488 <sect>Bugs/Feedback<p>
490 If you have problems using the library, if you find any bugs, or if you're
491 doing something interesting with it, I would be glad to hear from you. Feel
492 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
493 name="uz@cc65.org"> or <htmlurl url="mailto:cpg@aladdin.de"
494 name="cpg@aladdin.de">).
500 This software is provided 'as-is', without any expressed or implied
501 warranty. In no event will the authors be held liable for any damages
502 arising from the use of this software.
504 Permission is granted to anyone to use this software for any purpose,
505 including commercial applications, and to alter it and redistribute it
506 freely, subject to the following restrictions:
509 <item> The origin of this software must not be misrepresented; you must not
510 claim that you wrote the original software. If you use this software
511 in a product, an acknowledgment in the product documentation would be
512 appreciated but is not required.
513 <item> Altered source versions must be plainly marked as such, and must not
514 be misrepresented as being the original software.
515 <item> This notice may not be removed or altered from any source