]> git.sur5r.net Git - cc65/blob - doc/atari.sgml
Fixed several build warnings.
[cc65] / doc / atari.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
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:chris@groessler.org" name="chris@groessler.org">
10 <date>03-Jan-2006
11
12 <abstract>
13 An overview over the Atari runtime system as it is implemented for the cc65 C
14 compiler.
15 </abstract>
16
17 <!-- Table of contents -->
18 <toc>
19
20 <!-- Begin the document -->
21
22 <sect>Overview<p>
23
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
27 platform.
28
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
33 information.
34
35
36 <sect>Binary format<p>
37
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 &lt;2 byte start address&gt; &lt;2 bytes end address&gt;
41 &lsqb;program bytes&rsqb;). These values are calculated in the crt0.s
42 file from the __STARTUP_LOAD__ and __ZPSAVE_LOAD__ values, so keep
43 this in mind if you create a custom linker config file and start
44 moving segments around (see section
45 <ref name="Reserving a memory area inside the program" id="memhole">).
46 You can override this behaviour by creating your own crt0.s file and
47 linking it into your program.  A run vector is added to the end of the
48 file (&dollar;02E0 &lt;run vector&gt;) and is calculated using
49 __STARTUP_LOAD__ in crt0.s.
50
51
52 <sect>Memory layout<p>
53
54 The default linker script assumes that the BASIC ROM is disabled (or
55 the BASIC cartridge unplugged). This gives a usable memory range from
56 &dollar;2E00 - &dollar;BC1F. The library startup code examines the
57 current memory configuration, which depends on the size of the
58 installed memory and cartridges present, by inspecting the value in
59 the MEMTOP (&dollar;2E5) variable. Then the initial stack pointer,
60 which indicates the upper bound of memory used, is adjusted. The
61 default load address of &dollar;2E00 was chosen to accommodate having
62 a DOS loaded and a driver that resides in low memory such as the 850
63 R: handler. You can override this behaviour by creating a custom
64 linker config file or by using the "--start-addr" cl65 command line
65 argument or the "--start-addr" or "-S" ld65 command line arguments.
66
67 Special locations:
68
69 <descrip>
70   <tag/Text screen/
71   The text screen depends on the installed memory size and cartridges
72   and can be obtained from the SAVMSC variable (&dollar;58).
73
74   <tag/Stack/
75   The C runtime stack is located at MEMTOP and grows downwards,
76   regardless of how your linker config file is setup.  This
77   accommodates the different memory configurations of the Atari
78   machines, as well as having a cartridge installed.  You can override
79   this behaviour by writing your own crt0.s file and linking it to
80   your program (see also <ref name="Final note"
81   id="memhole_final_note">).
82
83   <tag/Heap/
84   The C heap is located at the end of the program and grows towards the C
85   runtime stack.
86
87 </descrip><p>
88
89
90
91 <sect>Platform specific header files<p>
92
93 Programs containing Atari specific code may use the <tt/atari.h/
94 header file.
95
96
97 <sect1>Atari specific functions<p>
98
99 The functions listed below are special for the Atari. See the <htmlurl
100 url="funcref.html" name="function reference"> for declaration and usage.
101
102 <itemize>
103 <item>get_ostype
104 <item>get_tv
105 <item>_gtia_mkcolor
106 <item>_getcolor
107 <item>_getdefdev
108 <item>_graphics
109 <item>_rest_vecs
110 <item>_save_vecs
111 <item>_scroll
112 <item>_setcolor
113 <item>_setcolor_low
114 </itemize>
115
116
117 <sect1>Hardware access<p>
118
119 The following pseudo variables declared in the <tt/atari.h/ header
120 file do allow access to hardware located in the address space. Some
121 variables are structures, accessing the struct fields will access the
122 chip registers.
123
124 <descrip>
125
126   <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
127   The <tt/GTIA_READ/ structure allows read access to the GTIA. The
128   <tt/GTIA_WRITE/ structure allows write access to the GTIA.
129   See the <tt/_gtia.h/ header file located in the include directory
130   for the declaration of the structure.
131
132   <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
133   The <tt/POKEY_READ/ structure allows read access to the POKEY. The
134   <tt/POKEY_WRITE/ structure allows write access to the POKEY.
135   See the <tt/_pokey.h/ header file located in the include directory
136   for the declaration of the structure.
137
138   <tag><tt/ANTIC/</tag>
139   The <tt/ANTIC/ structure allows read access to the ANTIC.
140   See the <tt/_antic.h/ header file located in the include directory
141   for the declaration of the structure.
142
143   <tag><tt/PIA/</tag>
144   The <tt/PIA/ structure allows read access to the PIA 6520.
145   See the <tt/_pia.h/ header file located in the include directory
146   for the declaration of the structure.
147
148 </descrip><p>
149
150
151
152 <sect>Loadable drivers<p>
153
154 <sect1>Graphics drivers<p>
155
156 Currently there are no graphics drivers available for the Atari platform.
157 However, the runtime library provides a function named _graphics, with
158 a mode parameter just like the BASIC GRAPHICS command. This function will
159 switch to the requested graphics mode.
160 There are currently no functions available to access the graphics
161 memory. The access must be implemented manually.
162
163 Many graphics modes require more memory than the text screen which is
164 in effect when the program starts up. Therefore the programmer has to
165 tell the program beforehand the memory requirements of the graphics
166 modes the program intends to use.
167 This can be done by using the __RESERVED_MEMORY__ linker config
168 variable. The number specified there describes the number of bytes to
169 subtract from the top of available memory as seen from the runtime
170 library. This memory is then used by the screen buffer.
171
172 The numbers for the different graphics modes presented below should
173 only be seen as a rule of thumb. Since the screen buffer memory needs
174 to start at specific boundaries, the numbers depend on the current top
175 of available memory.
176 The following numbers were determined by a BASIC program.
177
178 <table>
179 <tabular ca="rr">
180 graphics mode|reserved memory@<hline>
181 0|1@
182 1|1@
183 2|1@
184 3|1@
185 4|1@
186 5|182@
187 6|1182@
188 7|3198@
189 8|7120@
190 9|7146@
191 10|7146@
192 11|7146@
193 12|162@
194 13|1@
195 14|3278@
196 15|7120@
197 16|1@
198 17|1@
199 18|1@
200 19|1@
201 20|1@
202 21|184@
203 22|1192@
204 23|3208@
205 24|7146@
206 25|7146@
207 26|7146@
208 27|7146@
209 28|162@
210 29|1@
211 30|3304@
212 31|7146
213 </tabular>
214 <caption>reserved memory required for different graphics modes
215 </table>
216
217 The values of "1" are needed because the graphics command crashes if
218 it doesn't have at least one byte available. This seems to be a bug of
219 the Atari ROM code.
220
221 <sect1>Extended memory drivers<p>
222
223 Currently there are no extended memory drivers available for the Atari
224 platform.
225
226 <sect1>Joystick drivers<p>
227
228 <descrip>
229
230   <tag><tt/ataristd.joy/</tag>
231   Supports up to four standard joysticks connected to the joystick ports of
232   the Atari.
233
234   <tag><tt/atarim8.joy/</tag>
235   Supports up to eight standard joysticks connected to a MultiJoy adapter.
236
237 </descrip><p>
238
239
240
241 <sect1>Mouse drivers<p>
242
243 Currently no drivers available (in fact, the API for loadable mouse drivers
244 does not exist). There is a static driver you can use.
245
246
247 <sect1>RS232 device drivers<p>
248
249 Currently there are no RS232 loadable drivers available for the Atari
250 platform. There is a static driver you can use.
251
252
253 <sect>Limitations<p>
254
255
256 <sect>DIO implementation<label id="dio"><p>
257
258 The Atari supports disk drives with either 128 or 256 byte sectors.
259 The first three sectors of any disk are always 128 bytes long though. This is
260 because the system can only boot from 128 bytes sectors.
261
262 Therefore the DIO read and write functions transfer only 128 bytes
263 for sectors 1 to 3, regardless of the type of diskette.
264
265
266 <sect>CONIO implementation<label id="conio"><p>
267
268 The console I/O is speed optimized therefore support for XEP80 hardware
269 or f80.com software is missing. Of course you may use stdio.h functions.
270
271
272 <sect>Other hints<p>
273
274
275 <sect1>Function keys<p>
276
277 Function keys are mapped to Atari + number key.
278
279
280 <sect1>Passing arguments to the program<p>
281
282 Command line arguments can be passed to <tt/main()/ when DOS supports it.
283
284 <enum>
285 <item>Arguments are separated by spaces.
286 <item>Leading and trailing spaces around an argument are ignored.
287 <item>The first argument passed to <tt/main/ is the program name.
288 <item>A maximum number of 16 arguments (including the program name) are
289       supported.                                                       
290 </enum>
291
292
293 <sect1>Interrupts<p>
294
295 The runtime for the Atari uses routines marked as <tt/.INTERRUPTOR/ for
296 interrupt handlers. Such routines must be written as simple machine language
297 subroutines and will be called automatically by the VBI handler code
298 when they are linked into a program. See the discussion of the <tt/.CONDES/
299 feature in the <htmlurl url="ca65.html" name="assembler manual">.
300
301
302 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
303
304 The Atari 130XE maps its additional memory into CPU memory in 16K
305 chunks at address &dollar;4000 to &dollar;7FFF. One might want to
306 prevent this memory area from being used by cc65. Other reasons to
307 prevent the use of some memory area could be to reserve space for the
308 buffers for display lists and screen memory.
309 <p>
310 The Atari executable format allows holes inside a program, e.g. one
311 part loads into &dollar;2E00 to &dollar;3FFF, going below the reserved
312 memory area (assuming a reserved area from &dollar;4000 to
313 &dollar;7FFF), and another part loads into &dollar;8000 to
314 &dollar;BC1F.
315 <p>
316 Each load chunk of the executable starts with a 4 byte header which
317 defines its load address and size. In the following linker scripts
318 these headers are named HEADER and SECHDR (for the MEMORY layout), and
319 accordingly NEXEHDR and CHKHDR (for the SEGMENTS layout).
320 <p>
321 <sect2>Low code and high data example<p>
322 Goal: Create an executable with 2 load chunks which doesn't use the
323 memory area from &dollar;4000 to &dollar;7FFF. The CODE segment of
324 the program should go below &dollar;4000 and the DATA and RODATA
325 segments should go above &dollar;7FFF.
326 <p>
327 The main problem is that the EXE header generated by the cc65 runtime
328 lib is wrong. It defines a single load chunk with the sizes/addresses
329 of the STARTUP, LOWCODE, INIT, CODE, RODATA, and DATA segments (the whole user
330 program).
331 <p>
332 The contents of the EXE header come from the EXEHDR segment, which is
333 defined in crt0.s. This cannot be changed without modifying and
334 recompiling the cc65 atari runtime lib. Therefore the original EXE
335 header must be discarded. It will be replaced by a user created
336 one. The discarding is done by assigning the EXEHDR segment to the
337 BANK memory area. The BANK memory area is discarded in the new linker
338 script (written to file "").
339 <p>
340 The user needs to create a customized linker config file which adds
341 new memory areas and segments to hold the new EXE header and the
342 header data for the second load chunk. Also an assembly source file
343 needs to be created which defines the contents of the new EXE header
344 and the second load chunk header.
345 <p>
346 <p>
347 This is an example of a modified cc65 Atari linker configuration file
348 (split.cfg):
349 <tscreen><verb>
350 SYMBOLS {
351     __STACKSIZE__ = $800;                               # 2K stack
352     __RESERVED_MEMORY__: value = $0000, weak = yes;
353 }
354 FEATURES {
355     STARTADDRESS: default = $2E00;
356 }
357 MEMORY {
358     ZP: start = $82, size = $7E, type = rw, define = yes;
359
360     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
361     RAMLO: start = %S, size = $4000 - %S, file = %O;
362
363     BANK: start = $4000, size = $4000, file = "";
364
365     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
366     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
367 }
368 SEGMENTS {
369     EXEHDR: load = BANK, type = ro;
370
371     NEXEHDR: load = HEADER, type = ro;                  # first load chunk
372     STARTUP: load = RAMLO, type = ro, define = yes;
373     LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
374     INIT: load = RAMLO, type = ro, optional = yes;
375     CODE: load = RAMLO, type = ro, define = yes;
376
377     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
378     RODATA: load = RAM, type = ro, define = yes;
379     DATA: load = RAM, type = rw, define = yes;
380     BSS: load = RAM, type = bss, define = yes;
381     ZPSAVE: load = RAM, type = bss, define = yes;
382
383     ZEROPAGE: load = ZP, type = zp;
384     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
385 }
386 FEATURES {
387     CONDES: segment = RODATA,
388             type = constructor,
389             label = __CONSTRUCTOR_TABLE__,
390             count = __CONSTRUCTOR_COUNT__;
391     CONDES: segment = RODATA,
392             type = destructor,
393             label = __DESTRUCTOR_TABLE__,
394             count = __DESTRUCTOR_COUNT__;
395 }
396 </verb></tscreen>
397 <p>
398
399 A new memory area BANK was added which describes the reserved area.
400 It gets loaded with the contents of the old EXEHDR segment. But the
401 memory area isn't written to the output file. This way the contents of
402 the EXEHDR segment get discarded.
403 <p>
404 The newly added NEXEHDR segment defines the correct EXE header. It
405 puts the STARTUP, LOWCODE, INIT, and CODE segments, which are the
406 segments containing only code, into load chunk #1 (RAMLO memory area).
407 <p>
408 The header for the second load chunk comes from the new CHKHDR
409 segment. It puts the RODATA, DATA, BSS, and ZPSAVE segments into load
410 chunk #2 (RAM memory area).
411 <p>
412 <p>
413 The contents of the new NEXEHDR and CHKHDR segments come from this
414 file (split.s):
415 <tscreen><verb>
416         .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
417         .import __DATA_LOAD__, __RODATA_LOAD__, __STARTUP_LOAD__
418
419         .segment "NEXEHDR"
420         .word    $FFFF
421         .word    __STARTUP_LOAD__
422         .word    __CODE_LOAD__ + __CODE_SIZE__ - 1
423
424         .segment "CHKHDR"
425         .word    __RODATA_LOAD__
426         .word    __BSS_LOAD__ - 1
427 </verb></tscreen>
428 <p>
429 Compile with
430 <tscreen><verb>
431 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
432 </verb></tscreen>
433
434 <sect2>Low data and high code example<p>
435
436
437 Goal: Put RODATA and DATA into low memory and STARTUP, LOWCODE, INIT,
438 CODE, BSS, ZPSAVE into high memory (split2.cfg):
439
440 <tscreen><verb>
441 SYMBOLS {
442     __STACKSIZE__ = $800;       # 2K stack
443     __RESERVED_MEMORY__: value = $0000, weak = yes;
444 }
445 FEATURES {
446     STARTADDRESS: default = $2E00;
447 }
448 MEMORY {
449     ZP: start = $82, size = $7E, type = rw, define = yes;
450
451     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
452     RAMLO: start = %S, size = $4000 - %S, file = %O;
453
454     BANK: start = $4000, size = $4000, file = "";
455
456     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
457     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
458 }
459 SEGMENTS {
460     EXEHDR: load = BANK, type = ro;                     # discarded old EXE header
461
462     NEXEHDR: load = HEADER, type = ro;                  # first load chunk
463     RODATA: load = RAMLO, type = ro, define = yes;
464     DATA: load = RAMLO, type = rw, define = yes;
465
466     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
467     STARTUP: load = RAM, type = ro, define = yes;
468     INIT: load = RAM, type = ro, optional = yes;
469     CODE: load = RAM, type = ro, define = yes;
470     ZPSAVE: load = RAM, type = bss, define = yes;
471     BSS: load = RAM, type = bss, define = yes;
472
473     ZEROPAGE: load = ZP, type = zp;
474     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
475 }
476 FEATURES {
477     CONDES: segment = RODATA,
478             type = constructor,
479             label = __CONSTRUCTOR_TABLE__,
480             count = __CONSTRUCTOR_COUNT__;
481     CONDES: segment = RODATA,
482             type = destructor,
483             label = __DESTRUCTOR_TABLE__,
484             count = __DESTRUCTOR_COUNT__;
485 }
486 </verb></tscreen>
487
488 New contents for NEXEHDR and CHKHDR are needed (split2.s):
489 <tscreen><verb>
490         .import __STARTUP_LOAD__, __ZPSAVE_LOAD__, __DATA_SIZE__
491         .import __DATA_LOAD__, __RODATA_LOAD__
492
493         .segment "NEXEHDR"
494         .word    $FFFF
495         .word    __RODATA_LOAD__
496         .word    __DATA_LOAD__ + __DATA_SIZE__ - 1
497
498         .segment "CHKHDR"
499         .word    __STARTUP_LOAD__
500         .word    __ZPSAVE_LOAD__ - 1
501 </verb></tscreen>
502
503 Compile with
504 <tscreen><verb>
505 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
506 </verb></tscreen>
507
508 <sect2>Final note<label id="memhole_final_note"><p>
509
510 There are two other memory areas which don't appear directly in the
511 linker script. They are the stack and the heap.
512
513 The cc65 runtime lib places the stack location at the end of available
514 memory. This is dynamically set from the MEMTOP system variable at
515 startup. The heap is located in the area between the end of the BSS
516 segment and the top of the stack as defined by __STACKSIZE__.
517
518 If BSS and/or the stack shouldn't stay at the end of the program,
519 some parts of the cc65 runtime lib need to be replaced/modified.
520
521 common/_heap.s defines the location of the heap and atari/crt0.s
522 defines the location of the stack by initializing sp.
523
524
525 <sect>Bugs/Feedback<p>
526
527 If you have problems using the library, if you find any bugs, or if you're
528 doing something interesting with it, I would be glad to hear from you. Feel
529 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
530 name="uz@cc65.org"> or <htmlurl url="mailto:chris@groessler.org"
531 name="chris@groessler.org"> ).
532
533
534
535 <sect>License<p>
536
537 This software is provided 'as-is', without any expressed or implied
538 warranty.  In no event will the authors be held liable for any damages
539 arising from the use of this software.
540
541 Permission is granted to anyone to use this software for any purpose,
542 including commercial applications, and to alter it and redistribute it
543 freely, subject to the following restrictions:
544
545 <enum>
546 <item>  The origin of this software must not be misrepresented; you must not
547         claim that you wrote the original software. If you use this software
548         in a product, an acknowledgment in the product documentation would be
549         appreciated but is not required.
550 <item>  Altered source versions must be plainly marked as such, and must not
551         be misrepresented as being the original software.
552 <item>  This notice may not be removed or altered from any source
553         distribution.
554 </enum>
555
556 </article>