]> git.sur5r.net Git - cc65/blob - doc/atari.sgml
Merge pull request #338 from IrgendwerA8/master
[cc65] / doc / atari.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
5 <title>Atari specific information for cc65
6 <author>
7 <url url="mailto:shawnjefferson@24fightingchickens.com" name="Shawn Jefferson"> and<newline>
8 <url url="mailto:chris@groessler.org" name="Christian Groessler">
9 <date>2014-04-24
10
11 <abstract>
12 An overview over the Atari runtime system as it is implemented for the cc65 C
13 compiler.
14 </abstract>
15
16 <!-- Table of contents -->
17 <toc>
18
19 <!-- Begin the document -->
20
21 <sect>Overview<p>
22
23 This file contains an overview of the Atari runtime system as it comes
24 with the cc65 C compiler. It describes the memory layout, Atari specific
25 header files, available drivers, and any pitfalls specific to that
26 platform.
27
28 The Atari runtime support comes in two flavors: <tt/atari/ and <tt/atarixl/.
29 The <tt/atari/ target supports all Atari 8-bit computers, the <tt/atarixl/ only
30 supports XL type or newer machines (excluding the 600XL).
31
32 The <tt/atarixl/ runtime makes the whole 64K of memory available, with the
33 exception of the I/O area at &dollar;D000 - &dollar;D7FF. Since the
34 <tt/atarixl/ runtime has some <ref name="limitations" id="limitations">, it is
35 recommended to use the <tt/atari/ target unless lack of memory dictates the
36 use of the <tt/atarixl/ target.
37
38 Please note that Atari specific functions are just mentioned here, they are
39 described in detail in the separate <url url="funcref.html" name="function
40 reference">. Even functions marked as "platform dependent" may be available on
41 more than one platform. Please see the function reference for more
42 information.
43
44
45 <sect>Binary format<p>
46
47 The Atari DOS executable file format supports more than one load block (<it/chunk/).
48
49 The default binary output format generated by the linker for the
50 Atari target is a machine language program with a standard executable
51 header (FF FF &lt;load chunk #1&gt; ... &lt;load chunk #n&gt).
52 A load chunk has the format &lsqb;&lt;2 byte start address&gt; &lt;2 bytes end address&gt;
53 &lt;chunk data&gt;&rsqb;.
54 A run vector is added to the end of the
55 file (&dollar;02E0 &dollar;02E1 &lt;run vector&gt;) and is calculated using
56 the <tt/start/ label in crt0.s.  (Technically the run vector is also a load chunk,
57 but is not regarded as such here.)
58
59 An <tt/atari/ program has two load chunks, an <tt/atarixl/ program has three load
60 chunks.  The load chunks are defined in the linker configuration files.  For more
61 detailed information about the load chunks see the chapter
62 <ref name="Technical details" id="techdetail">. For the discussion here it's
63 sufficient to know that the first load chunk(s) do preparation work and the
64 main part of the program is in the last load chunk.
65
66 The values determining the size of the main part of the program (the second load
67 chunk for <tt/atari/, the third load chunk for <tt/atarixl/) are calculated in
68 the crt0.s file from the __STARTUP_LOAD__ and __BSS_LOAD__ values.
69 Be aware of that if you create a custom linker config file and start moving segments around (see section
70 <ref name="Reserving a memory area inside the program" id="memhole">).
71
72
73 <sect>Memory layout<p>
74
75 <sect1><tt/atari/ target<p>
76
77 The default linker config file assumes that the BASIC ROM is disabled (or
78 the BASIC cartridge unplugged). This gives a usable memory range of
79 &lsqb;&dollar;2000-&dollar;BC1F&rsqb;. The library startup code examines the
80 current memory configuration, which depends on the size of the
81 installed memory and cartridges. It does so by using the value in
82 the MEMTOP (&dollar;2E5) variable as highest memory address the program
83 can use. The initial stack pointer, which is the upper bound of
84 memory used by the program, is set to this value, minus an optionally
85 defined __RESERVED_MEMORY__ value.
86
87 The default load address of &dollar;2000 can be changed by creating a custom
88 linker config file or by using the "--start-addr" cl65 command line
89 argument or the "--start-addr" or "-S" ld65 command line arguments.
90
91 Please note that the first load chunk (which checks the available memory)
92 will always be loaded at &dollar;2E00, regardless of the specified start
93 address. This address can only be changed by a custom linker config file.
94
95 Special locations:
96
97 <descrip>
98   <tag/Text screen/
99   The text screen depends on the installed memory size and cartridges
100   and can be obtained from the SAVMSC variable (&dollar;58).
101
102   <tag/Stack/
103   The C runtime stack is located at MEMTOP and grows downwards,
104   regardless of how your linker config file is setup.  This
105   accommodates the different memory configurations of the Atari
106   machines, as well as having a cartridge installed.  You can override
107   this behaviour by writing your own crt0.s file and linking it to
108   your program (see also <ref name="Final note"
109   id="memhole_final_note">).
110
111   <tag/Heap/
112   The C heap is located at the end of the program and grows towards the C
113   runtime stack.
114
115 </descrip><p>
116
117 <sect1><tt/atarixl/ target<p>
118
119 The startup code rearranges the memory as follows:
120
121 <enum>
122 <item>Sceen memory and display list are moved below the program start address.
123 <item>The ROM is disabled, making the memory in the areas &lsqb;&dollar;C000-&dollar;CFFF&rsqb;
124 and &lsqb;&dollar;D800-&dollar;FFF9&rsqb; available.
125 <item>Character generator data is copied from ROM to the CHARGEN location specified in the
126 linker config file.  This is (in the default <tt/atarixl.cfg/ file) at the same address as
127 where it is in ROM (&dollar;E000, it can be changed, see <ref name="atarixl chargen location"
128 id="chargenloc">).  With the character generator at &dollar;E000, there are two upper memory
129 areas available, &lsqb;&dollar;D800-&dollar;DFFF&rsqb; and &lsqb;&dollar;E400-&dollar;FFF9&rsqb;.
130 </enum>
131
132 With the default load address of &dollar;2400 this gives a usable memory range of
133 &lsqb;&dollar;2400-&dollar;CFFF&rsqb;.
134
135 Please note that the first load chunk (which checks the system
136 compatibilty and available memory) will always be loaded at
137 &dollar;2E00, regardless of the specified start address. This address
138 can only be changed by a custom linker config file.
139
140 Special locations:
141
142 <descrip>
143   <tag/Text screen/
144   The text screen depends on the selected load address (&dollar;2400
145   by default), and resides directly before that address, rounded to the next
146   lower page boundary.
147   The screen memory's start address can be obtained from the SAVMSC variable
148   (&dollar;58).
149
150   <tag/Stack/
151   The C runtime stack is located at end of the MAIN memory area (&dollar;CFFF)
152   and grows downwards.
153
154   <tag/Heap/
155   The C heap is located at the end of the program (end of BSS segment) and
156   grows towards the C runtime stack.
157
158 </descrip><p>
159
160 <sect>Linker configurations<p>
161
162 The ld65 linker comes with default config files for the Atari. There
163 are two targets for the Atari, <tt/atari/ and <tt/atarixl/.
164 The default config file for <tt/atari/ is selected with
165 <tt/-t atari/, and the default config file for <tt/atarixl/ is selected with
166 <tt/-t atarixl/.
167 The Atari package comes with additional secondary linker config files which
168 can be used via <tt/-t atari -C &lt;configfile&gt;/ (for <tt/atari/ target) or
169 <tt/-t atarixl -C &lt;configfile&gt;/ (for <tt/atarixl/ target).
170
171 <sect1><tt/atari/ config files<p>
172
173 <sect2>default config file (<tt/atari.cfg/)<p>
174
175 The default configuration is tailored to C programs. It creates files
176 which have a default load address of &dollar;2000.
177
178 The files generated by this config file include the
179 <ref name="&dquot;system check&dquot;" id="syschk"> load chunk. It can
180 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
181
182 <sect2><tt/atari-asm.cfg/<p>
183
184 This config file aims to give the assembler programmer maximum
185 flexibility. All program segments (<tt/CODE/, <tt/DATA/, etc.) are
186 optional.
187
188 By default it creates regular DOS executable files, which have a default
189 load address of &dollar;2E00. It's also possible to generate an image of
190 just the program data without EXE header, load address, or (auto-)start address.
191 To you so, you have to define the symbols <tt/__AUTOSTART__/ and <tt/__EXEHDR__/
192 when linking the program. Therefore, to generate a "plain" binary file, pass the
193 options "<tt/-D__AUTOSTART__=1 -D__EXEHDR__=1/" to the linker.
194 It's also possible to create a non auto-starting program file, by defining
195 only the <tt/__AUTOSTART__/ symbol. Such a program has to be run manually
196 after being loaded by DOS (for example by using the "M" option of DOS 2.5).
197 Defining only the <tt/__EXEHDR__/ symbol will create a (useless) file which
198 doesn't conform to the DOS executable file format (like a "plain" binary file)
199 but still has the "autostart" load chunk appended.
200
201 The sections of the file which the defines refer to (<tt/__AUTOSTART__/ for
202 the autostart trailer, <tt/__EXEHDR__/ for the EXE header and load address)
203 is <it/left out/, keep this in mind.
204
205 The values you assign to the two symbols <tt/__AUTOSTART__/ and <tt/__EXEHDR__/
206 don't matter.
207
208 <sect2><tt/atari-cart.cfg/<p>
209
210 This config file can be used to create 8K or 16K cartridges. It's suited both
211 for C and assembly language programs.
212
213 By default, an 8K cartridge is generated. To create a 16K cartridge, pass the
214 size of the cartridge to the linker, like "<tt/-D__CARTSIZE__=0x4000/".
215 The only valid values for <tt/__CARTSIZE__/ are 0x2000 and 0x4000.
216
217 The option byte of the cartridge can be set with the <tt/__CARTFLAGS__/
218 value, passed to the linker. The default value is &dollar;01, which means
219 that the cartridge doesn't prevent the booting of DOS.
220
221 The option byte will be located at address &dollar;BFFD. For more information
222 about its use, see e.g. "Mapping the Atari".
223
224 <sect2><tt/atari-cassette.cfg/<p>
225
226 This config file can be used to create cassette boot files. It's suited both
227 for C and assembly language programs.
228
229 The size of a cassette boot file is restricted to 32K. Larger programs
230 would need to be split in more parts and the parts to be loaded manually.
231
232 To write the generated file to a cassette, a utility (<tt/w2cas.com/) to run
233 on an Atari is provided in the <tt/util/ directory of <tt/atari/ target dir.
234
235 <sect1><tt/atarixl/ config files<p>
236
237 <sect2>default config file (<tt/atarixl.cfg/)<p>
238
239 The default configuration is tailored to C programs. It creates files
240 which have a default load address of &dollar;2400.
241
242 The files generated by this config file include the
243 <ref name="&dquot;system check&dquot;" id="syschkxl"> load chunk. It can
244 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
245
246 <sect2><tt/atarixl-largehimem.cfg/<p>
247
248 This is the same as the default config file, but it rearranges the
249 high memory beneath the ROM into one large block. In order for this
250 config file to work, the runtime library has to be recompiled with a
251 special define. See the file <tt/libsrc&sol;atari&sol;Makefile.inc/ in the
252 source distribution.
253
254 The files generated by this config file include the
255 <ref name="&dquot;system check&dquot;" id="syschkxl"> load chunk. It can
256 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
257
258
259 <sect>Platform specific header files<p>
260
261 Programs containing Atari specific code may use the <tt/atari.h/
262 header file.
263
264
265 <sect1>Atari specific functions<p>
266
267 The functions and global variable listed below are special for the Atari.
268 See the <url url="funcref.html" name="function reference"> for declaration and usage.
269
270 <itemize>
271 <item>get_ostype
272 <item>get_tv
273 <item>_dos_type
274 <item>_gtia_mkcolor
275 <item>_getcolor
276 <item>_getdefdev
277 <item>_graphics
278 <item>_is_cmdline_dos
279 <item>_rest_vecs
280 <item>_save_vecs
281 <item>_scroll
282 <item>_setcolor
283 <item>_setcolor_low
284 </itemize>
285
286
287 <sect1>Hardware access<p>
288
289 The following pseudo variables declared in the <tt/atari.h/ header
290 file do allow access to hardware located in the address space. Some
291 variables are structures, accessing the struct fields will access the
292 chip registers.
293
294 <descrip>
295
296   <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
297   The <tt/GTIA_READ/ structure allows read access to the GTIA. The
298   <tt/GTIA_WRITE/ structure allows write access to the GTIA.
299   See the <tt/_gtia.h/ header file located in the include directory
300   for the declaration of the structure.
301
302   <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
303   The <tt/POKEY_READ/ structure allows read access to the POKEY. The
304   <tt/POKEY_WRITE/ structure allows write access to the POKEY.
305   See the <tt/_pokey.h/ header file located in the include directory
306   for the declaration of the structure.
307
308   <tag><tt/ANTIC/</tag>
309   The <tt/ANTIC/ structure allows read access to the ANTIC.
310   See the <tt/_antic.h/ header file located in the include directory
311   for the declaration of the structure.
312
313   <tag><tt/PIA/</tag>
314   The <tt/PIA/ structure allows read access to the PIA 6520.
315   See the <tt/_pia.h/ header file located in the include directory
316   for the declaration of the structure.
317
318 </descrip><p>
319
320
321 <sect1>Character mapping<p>
322
323 The Atari has two representations for characters:
324 <enum>
325 <item> ATASCII is character mapping which is similar to ASCII and used
326 by the CIO system of the OS. This is the default mapping of cc65 when
327 producing code for the atari target.
328 <item> The internal/screen mapping represents the real value of the
329 screen ram when showing a character.
330 </enum>
331
332 For direct memory access (simplicity and speed) enabling the internal
333 mapping can be useful. This can be achieved by including the
334 "<tt/atari_screen_charmap.h/" header.
335
336 A word of caution: Since the <tt/0x00/ character has to be mapped in an
337 incompatible way to the C-standard, the usage of string functions in
338 conjunction with internal character mapped strings delivers unexpected
339 results regarding the string length. The end of strings are detected where
340 you may not expect them (to early or (much) to late). Internal mapped
341 strings typically support the "<tt/mem...()/" functions.
342
343 <em>For assembler sources the macro "<tt/scrcode/" from the "<tt/atari.mac/"
344 package delivers the same feature.</em>
345
346 You can switch back to the ATASCII mapping by including
347 "<tt/atari_atascii_charmap.h/".
348
349 A final note: Since cc65 has currently some difficulties with string merging
350 under different mappings, defining remapped strings works only flawlessly
351 with static array initialization:
352
353 <verb>
354 #include &lt;atari\_screen\_charmap.h&gt;
355 char pcScreenMappingString[] = "Hello Atari!";
356
357 #include &lt;atari_atascii_charmap.h&gt;
358 char pcAtasciiMappingString[] = "Hello Atari!";
359 </verb>
360
361 delivers correct results, while
362
363 <verb>
364 #include &lt;atari_screen_charmap.h&gt;
365 char* pcScreenMappingString = "Hello Atari!";
366
367 #include &lt;atari_atascii_charmap.h&gt;
368 char* pcAtasciiMappingString = "Hello Atari!";
369 </verb>
370
371 does not.
372
373
374 <sect>Loadable drivers<p>
375
376 The names in the parentheses denote the symbols to be used for static linking of the drivers.
377
378
379 <sect1>Graphics drivers<p>
380
381 <table><tabular ca="rrrr">
382 <tt/atari/|<tt/atarixl/|screen resolution|display pages@<hline>
383 <tt/atr3.tgi (atr3_tgi)/|<tt/atrx3.tgi (atrx3_tgi)/|40x24x4 (CIO mode 3, ANTIC mode 8)|1@
384 <tt/atr4.tgi (atr4_tgi)/|<tt/atrx4.tgi (atrx4_tgi)/|80x48x2 (CIO mode 4, ANTIC mode 9)|1@
385 <tt/atr5.tgi (atr5_tgi)/|<tt/atrx5.tgi (atrx5_tgi)/|80x48x4 (CIO mode 5, ANTIC mode A)|1@
386 <tt/atr6.tgi (atr6_tgi)/|<tt/atrx6.tgi (atrx6_tgi)/|160x96x2 (CIO mode 6, ANTIC mode B)|1@
387 <tt/atr7.tgi (atr7_tgi)/|<tt/atrx7.tgi (atrx7_tgi)/|160x96x4 (CIO mode 7, ANTIC mode D)|1@
388 <tt/atr8.tgi (atr8_tgi)/|<tt/atrx8.tgi (atrx8_tgi)/|320x192x2 (CIO mode 8, ANTIC mode F)|1@
389 <tt/atr8p2.tgi (atr8p2_tgi)/|<tt/atrx8p2.tgi (atrx8p2_tgi)/|320x192x2 (CIO mode 8, ANTIC mode F)|2@
390 <tt/atr9.tgi (atr9_tgi)/|<tt/atrx9.tgi (atrx9_tgi)/|80x192x16b (CIO mode 9, ANTIC mode F, GTIA mode &dollar;40)|1@
391 <tt/atr9p2.tgi (atr9p2_tgi)/|<tt/atrx9p2.tgi (atrx9p2_tgi)/|80x192x16b (CIO mode 9, ANTIC mode F, GTIA mode &dollar;40)|2@
392 <tt/atr10.tgi (atr10_tgi)/|<tt/atrx10.tgi (atrx10_tgi)/|80x192x9 (CIO mode 10, ANTIC mode F, GTIA mode &dollar;80)|1@
393 <tt/atr10p2.tgi (atr10p2_tgi)/|<tt/atrx10p2.tgi (atrx10p2_tgi)/|80x192x9 (CIO mode 10, ANTIC mode F, GTIA mode &dollar;80)|2@
394 <tt/atr11.tgi (atr11_tgi)/|<tt/atrx11.tgi (atrx11_tgi)/|80x192x16h (CIO mode 11, ANTIC mode F, GTIA mode &dollar;C0)|1@
395 <tt/atr14.tgi (atr14_tgi)/|<tt/atrx14.tgi (atrx14_tgi)/|160x192x2 (CIO mode 14, ANTIC mode C)|1@
396 <tt/atr15.tgi (atr15_tgi)/|<tt/atrx15.tgi (atrx15_tgi)/|160x192x4 (CIO mode 15, ANTIC mode E)|1@
397 <tt/atr15p2.tgi (atr15p2_tgi)/|<tt/atrx15p2.tgi (atrx15p2_tgi)/|160x192x4 (CIO mode 15, ANTIC mode E)|2
398 </tabular>
399 <!-- <caption>bla bla -->
400 </table>
401
402
403 Many graphics modes require more memory than the text screen which is
404 in effect when the program starts up. Therefore the programmer has to
405 tell the program beforehand the memory requirements of the graphics
406 modes the program intends to use.
407
408 On the <tt/atari/ target his can be done by using the __RESERVED_MEMORY__
409 linker config variable. The number specified there describes the number
410 of bytes to subtract from the top of available memory as seen from the
411 runtime library. This memory is then used by the screen buffer.
412
413 On the <tt/atarixl/ target the screen memory resides below the program
414 load address.  In order to reserve memory for a graphics mode, one
415 simply uses a higher program load address.  There are restrictions on
416 selectable load addresses,
417 see <ref name="Selecting a good program load address" id="loadaddr">.
418
419 The numbers for the different graphics modes presented below should
420 only be seen as a rule of thumb. Since the screen buffer memory needs
421 to start at specific boundaries, the numbers depend on the current top
422 of available memory.
423 The following numbers were determined by a BASIC program.
424
425 <table>
426 <tabular ca="rr">
427 graphics mode|reserved memory@<hline>
428 0|1@
429 1|1@
430 2|1@
431 3|1@
432 4|1@
433 5|182@
434 6|1182@
435 7|3198@
436 8|7120@
437 9|7146@
438 10|7146@
439 11|7146@
440 12|162@
441 13|1@
442 14|3278@
443 15|7120@
444 16|1@
445 17|1@
446 18|1@
447 19|1@
448 20|1@
449 21|184@
450 22|1192@
451 23|3208@
452 24|7146@
453 25|7146@
454 26|7146@
455 27|7146@
456 28|162@
457 29|1@
458 30|3304@
459 31|7146
460 </tabular>
461 <caption>reserved memory required for different graphics modes
462 </table>
463
464 The values of "1" are needed because the graphics command crashes if
465 it doesn't have at least one byte available. This seems to be a bug of
466 the Atari ROM code.
467
468 Default drivers: <tt/atr8.tgi (atr8_tgi)/ and <tt/atrx8.tgi (atrx8_tgi)/.
469
470 <sect1>Extended memory drivers<p>
471
472 Currently there is only one extended memory driver.  It manages the second 64K of a 130XE.
473
474 <table>
475 <tabular ca="rr">
476 <tt/atari/|<tt/atarixl/@<hline>
477 <tt/atr130.emd (atr130_emd)/|<tt/atrx130.emd (atrx130_emd)/
478 </tabular>
479 </table>
480
481 <sect1>Joystick drivers<p>
482
483 Currently there are two joystick drivers available:
484
485 <table>
486 <tabular ca="rrr">
487 <tt/atari/|<tt/atarixl/|description@<hline>
488 <tt/atrstd.joy (atrstd_joy)/|<tt/atrxstd.joy (atrxstd_joy)/|Supports up to two/four standard joysticks connected to the joystick ports of the Atari. (Four on the pre-XL systems, two on XL or newer.)@
489 <tt/atrmj8.joy (atrmj8_joy)/|<tt/atrxmj8.joy (atrxmj8_joy)/|Supports up to eight standard joysticks connected to a MultiJoy adapter.
490 </tabular>
491 </table>
492
493 Default drivers: <tt/atrstd.joy (atrstd_joy)/ and <tt/atrxstd.joy (atrxstd_joy)/.
494
495 <sect1>Mouse drivers<p>
496
497 Currently there are five mouse drivers available:
498
499 <table>
500 <tabular ca="rrr">
501 <tt/atari/|<tt/atarixl/|description@<hline>
502 <tt/atrjoy.mou (atrjoy_mou)/|<tt/atrxjoy.mou (atrxjoy_mou)/|Supports a mouse emulated by a standard joystick.@
503 <tt/atrst.mou (atrst_mou)/|<tt/atrxst.mou (atrxst_mou)/|Supports an Atari ST mouse.@
504 <tt/atrami.mou (atrami_mou)/|<tt/atrxami.mou (atrxami_mou)/|Supports an Amiga mouse.@
505 <tt/atrtrk.mou (atrtrk_mou)/|<tt/atrxtrk.mou (atrxtrk_mou)/|Supports an Atari trakball.@
506 <tt/atrtt.mou (atrtt_mou)/|<tt/atrxtt.mou (atrxtt_mou)/|Supports an Atari touch tablet.
507 </tabular>
508 </table>
509
510 All mouse devices connect to joystick port #0.
511
512 Default drivers: <tt/atrst.mou (atrst_mou)/ and <tt/atrxst.mou (atrxst_mou)/.
513
514 <sect2>Mouse callbacks<p>
515
516 There are two mouse callbacks available.
517 <p>
518 The "text mode" callbacks (<tt/mouse_txt_callbacks/) display the mouse cursor as a "diamond" character
519 on the standard "GRAPHICS 0" text mode screen. The mouse cursor character can be changed by an
520 assembly file defining the character by exporting the zeropage symbol <tt/mouse_txt_char/.
521 The default file looks like this:
522 <tscreen><verb>
523         .export mouse_txt_char : zp = 96     ; 'diamond' screen code
524 </verb></tscreen>
525 <p>
526 The "P/M" callbacks (<tt/mouse_pm_callbacks/) use Player-Missile graphics for the mouse cursor.
527 The cursor shape can be changed, too, by an assembly file. Here's the default shape definition:
528 <tscreen><verb>
529         .export mouse_pm_bits
530         .export mouse_pm_height    : zeropage
531         .export mouse_pm_hotspot_x : zeropage
532         .export mouse_pm_hotspot_y : zeropage
533         .rodata
534 mouse_pm_bits:
535         .byte   %11110000
536         .byte   %11000000
537         .byte   %10100000
538         .byte   %10010000
539         .byte   %10001000
540         .byte   %00000100
541         .byte   %00000010
542 mouse_pm_height = * - mouse_pm_bits
543 ; hot spot is upper left corner
544 mouse_pm_hotspot_x = 0
545 mouse_pm_hotspot_y = 0
546 </verb></tscreen>
547 <p>
548 <tt/mouse_pm_bits/ defines the shape of the cursor, <tt/mouse_pm_height/ defines the number of
549 bytes in <tt/mouse_pm_bits/. <tt/mouse_pm_hotspot_x/ and <tt/mouse_pm_hotspot_y/ define the
550 position in the shape where "the mouse points to". When using this callback page #6 (&dollar;600
551  - &dollar;6FF) is used for the P/M graphics data and no P/M graphics can otherwise be used
552 by the program. The height of the shape (<tt/mouse_pm_height/)
553 must not exceed 32 lines since the callback routines cannot handle more than 32 lines.
554 <p>
555 The default callbacks definition (<tt/mouse_def_callbacks/) is an alias for the "P/M" callbacks.
556
557 <sect1>RS232 device drivers<p>
558
559 Currently there is one RS232 driver.  It uses the R: device (therefore
560 an R: driver needs to be installed) and was tested with the 850
561 interface module.
562
563 <table>
564 <tabular ca="rr">
565 <tt/atari/|<tt/atarixl/@<hline>
566 <tt/atrrdev.ser (atrrdev_ser)/|<tt/atrxrdev.ser (atrxrdev_ser)/
567 </tabular>
568 </table>
569
570
571 <sect>Limitations<p>
572
573 <sect1><tt/atarixl/<#if output="info|latex2e"> limitations</#if><label id="limitations"<p>
574
575 <itemize>
576 <item>The display is cleared at program start and at program termination.  This is a side
577 effect of relocating the display memory below the program start address.
578 <item>Not all possible CIO and SIO functions are handled by the runtime stub code which banks
579 the ROM in and out.  All functions used by the runtime library are handled, though.
580 <item>The <tt/_sys()/ function is not supported.
581 <item>It is not compatible with DOSes or other programs using the memory below the ROM.
582 </itemize>
583
584 <sect>DIO implementation<label id="dio"><p>
585
586 The Atari supports disk drives with either 128 or 256 byte sectors.
587 The first three sectors of any disk are always 128 bytes long though. This is
588 because the system can only boot from 128 bytes sectors.
589
590 Therefore the DIO read and write functions transfer only 128 bytes
591 for sectors 1 to 3, regardless of the type of diskette.
592
593
594 <sect>CONIO implementation<label id="conio"><p>
595
596 The console I/O is speed optimized therefore support for XEP80 hardware
597 or f80.com software is missing. Of course you may use stdio.h functions.
598
599
600 <sect>Technical details<label id="techdetail"><p>
601
602 <sect1><tt/atari/<#if output="info|latex2e"> details</#if><p>
603
604 <sect2><#if output="info|latex2e"><tt/atari/ </#if>Load chunks<p>
605
606 An <tt/atari/ program contains two load chunks.
607
608 <enum>
609 <item>"system check"<label id="syschk">&nl;
610 This load chunk is always loaded at address &dollar;2E00, and checks if the system has
611 enough memory to run the program. It also checks if the program start address is not
612 below MEMLO. If any of the checks return false, the loading of the program is aborted.&nl;
613 The contents of this chunk come from the SYSCHKCHNK memory area of the linker config file.
614 <item>main program&nl;
615 This load chunk is loaded at the selected program start address (default &dollar;2000) and
616 contains all of the code and data of the program.&nl;
617 The contents of this chunk come from the MAIN memory area of the linker config file.
618 </enum>
619
620
621 <sect1><tt/atarixl/<#if output="info|latex2e"> details</#if><p>
622
623 <sect2>General operation<p>
624
625 The <tt/atarixl/ target banks out the ROM while the program is running in
626 order to make more memory available to the program.
627
628 The screen memory is by default located at the top of available memory,
629 &dollar;BFFF if BASIC is not enabled, &dollar;9FFF if BASIC is enabled.
630 Therefore, in order to create a largest possible continuous memory area,
631 the screen memory is moved below the program load address.  This gives
632 a memory area from &lt;program load addr&gt; to &dollar;CFFF.
633
634 The startup code installs wrappers for interrupt handlers and ROM routines.
635 When an interrupt or call to a ROM routine happens, the wrappers enable the
636 ROM, call the handler or routine, and disable the ROM again.
637
638 The "wrapping" of the ROM routines is done by changing the ROM entry
639 point symbols in <tt/atari.inc/ to point to the wrapper functions.
640
641 For ROM functions which require input or output buffers, the wrappers
642 copy the data as required to buffers in low memory.
643
644 <sect2><#if output="info|latex2e"><tt/atarixl/ </#if>Load chunks<label id="xlchunks"><p>
645
646 An <tt/atarixl/ program contains three load chunks.
647
648 <enum>
649 <item>"system check"<label id="syschkxl">&nl;
650 This load chunk is always loaded at address &dollar;2E00, and checks if the system is
651 suitable for running the program. It also checks if there is enough room between MEMLO
652 and the program start address to move the text mode screen buffer there. If any of the
653 checks return false, the loading of the program is aborted.&nl;
654 The contents of this chunk come from the SYSCHKCHNK memory area of the linker config file.
655 <item>"shadow RAM prepare"&nl;
656 The second load chunk gets loaded to the selected program load address (default &dollar;2400).
657 It moves the screen memory below the program load address, copies the character generator
658 from ROM to its new place in RAM, and copies the parts of the program which reside in
659 high memory below the ROM to their place. The high memory parts are included in this load chunk.&nl;
660 At the beginning of this load chunk there is a .bss area, which is not part of the
661 EXE file. Therefore the on-disk start address of this load chunk will be higher than the
662 selected start address. This .bss area (segment LOWBSS) contains the buffers for the
663 double buffering of ROM input and output data.  If you add contents to this segment be aware
664 that the contents won't be zero initialized by the startup code.&nl;
665 The contents of this chunk come from the SRPREPCHNK memory area of the linker config file.
666 <item>main program&nl;
667 This load chunk is loaded just above the LOWBSS segment, replacing the code of
668 the previous load chunk. It contains all remaining code and data sections of the program,
669 including the startup code.&nl;
670 The contents of this chunk come from the RAM memory area of the linker config file.
671 </enum>
672
673 <sect2>Moving screen memory below the program start address<p>
674
675 When setting a graphics mode, the ROM looks at the RAMTOP location. RAMTOP
676 describes the amount of installed memory in pages (RAMTOP is only one byte).
677 The screen memory and display list are placed immediately below RAMTOP.
678
679 Now in order to relocate the screen memory to lower memory, the startup code
680 puts a value into RAMTOP which causes the ROM routines to allocate the display
681 memory below the program start address and then it issues a ROM call to setup
682 the regular text mode.
683
684 <sect2>Selecting a good program load address<label id="loadaddr"><p>
685
686 Due to the movement of the screen memory below the program start, there are some
687 load addresses which are sub-optimal because they waste memory or prevent a
688 higher resolution graphics mode from being enabled.
689
690 There are restrictions at which addresses screen memory (display buffer and display
691 list) can be placed. The display buffer cannot cross a 4K boundary and a display
692 list cannot cross a 1K boundary.
693
694 The startup code takes this into account when moving the screen memory down.
695 If the program start address (aligned to the next lower page boundary) minus
696 the screen buffer size would result in a screen buffer which spans a 4K
697 boundary, the startup code lowers RAMTOP to this 4K boundary.&nl;
698 The size of the screen buffer in text mode is 960 (&dollar;3C0) bytes. So, for
699 example, a selected start address of &dollar;2300 would span the 4K boundary
700 at &dollar;2000. The startup code would adjust the RAMTOP value in such way that
701 the screen memory would be located just below this boundary (at &dollar;1C40).
702 This results in the area &lsqb;&dollar;2000-&dollar;22FF&rsqb; being wasted.
703 Additionally, the program might fail to load since the lowest address used
704 by the screen memory could be below MEMLO. (The lowest address used in this
705 example would be at &dollar;1C20, where the display list would allocated.)
706
707 These calculations are performed by the startup code (in the first two
708 load chunks), but the startup code only takes the default 40x24 text mode
709 into account. If the program later wants to load TGI drivers which set
710 a more memory consuming graphics mode, the user has to pick a higher
711 load address.
712 Using higher resolution modes there is a restriction in the ROM that it
713 doesn't expect RAMTOP to be at arbitrary values. The Atari memory modules
714 came only in 8K or 16K sizes, so the ROM expects RAMTOP to only have
715 values in 8K steps. Therefore, when using the highest resolution modes
716 the program start address must be at an 8K boundary.
717
718
719 <sect2>Character generator location<label id="chargenloc"><p>
720
721 The default <tt/atarixl/ linker config file (<tt/atarixl.cfg/) leaves the
722 character generator location at the same address where it is in ROM
723 (&dollar;E000). This has the disadvatage to split the upper memory into
724 two parts (&lsqb;&dollar;D800-&dollar;DFFF&rsqb; and
725 &lsqb;&dollar;E400-&dollar;FFF9&rsqb;). For applications which
726 require a large continuous upper memory area, an alternative linker
727 config file (<tt/atarixl-largehimem.cfg/) is provided. It relocates the
728 character generator to &dollar;D800, providing a single big upper
729 memory area at &lsqb;&dollar;DC00-&dollar;FFF9&rsqb;.
730
731 With the character generator at a different address than in ROM, the routines
732 which enable and disable the ROM also have to update the chargen pointer.
733 This code is not enabled by default. In order to enable it,
734 uncomment the line which sets CHARGEN_RELOC in <tt/libsrc&sol;atari&sol;Makefile.inc/
735 and recompile the <tt/atarixl/ runtime library.
736
737 <sect>Other hints<p>
738
739
740 <sect1>Function keys<p>
741
742 Function keys are mapped to Atari + number key.
743
744
745 <sect1>Passing arguments to the program<p>
746
747 Command line arguments can be passed to <tt/main()/ when the used DOS supports it.
748
749 <enum>
750 <item>Arguments are separated by spaces.
751 <item>Leading and trailing spaces around an argument are ignored.
752 <item>The first argument passed to <tt/main/ is the program name.
753 <item>A maximum number of 16 arguments (including the program name) are
754       supported.
755 </enum>
756
757
758 <sect1>Interrupts<p>
759
760 The runtime for the Atari uses routines marked as <tt/.INTERRUPTOR/ for
761 interrupt handlers. Such routines must be written as simple machine language
762 subroutines and will be called automatically by the VBI handler code
763 when they are linked into a program. See the discussion of the <tt/.CONDES/
764 feature in the <url url="ca65.html" name="assembler manual">.
765
766 Please note that on the Atari targets the <tt/.INTERRUPTOR/s are being
767 run in NMI context. The other targets run them in IRQ context.
768
769 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
770
771 (This section is primarily applicable to the <tt/atari/ target, but the
772 principles apply to <tt/atatixl/ as well.)
773
774 The Atari 130XE maps its additional memory into CPU memory in 16K
775 chunks at address &dollar;4000 to &dollar;7FFF. One might want to
776 prevent this memory area from being used by cc65. Other reasons to
777 prevent the use of some memory area could be to reserve space for the
778 buffers for display lists and screen memory.
779 <p>
780 The Atari executable format allows holes inside a program, e.g. one
781 part loads into &dollar;2E00 to &dollar;3FFF, going below the reserved
782 memory area (assuming a reserved area from &dollar;4000 to
783 &dollar;7FFF), and another part loads into &dollar;8000 to
784 &dollar;BC1F.
785 <p>
786 Each load chunk of the executable starts with a 4 byte header which
787 defines its load address and size. In the following linker config files
788 these headers are named HEADER and SECHDR (for the MEMORY layout), and
789 accordingly NEXEHDR and CHKHDR (for the SEGMENTS layout).
790 <p>
791 <sect2>Low code and high data example<p>
792 Goal: Create an executable with 2 load chunks which doesn't use the
793 memory area from &dollar;4000 to &dollar;7FFF. The CODE segment of
794 the program should go below &dollar;4000 and the DATA and RODATA
795 segments should go above &dollar;7FFF.
796 <p>
797 The main problem is that the EXE header generated by the cc65 runtime
798 lib is wrong. It defines a single load chunk with the sizes/addresses
799 of the STARTUP, LOWCODE, ONCE, CODE, RODATA, and DATA segments, in
800 fact, the whole user program (we're disregarding the "system check"
801 load chunk here).
802 <p>
803 The contents of the EXE header come from the EXEHDR and MAINHDR segments.
804 The EXEHDR segment just contains the &dollar;FFFF value which is required
805 to be the first bytes of the EXE file.&nl;
806 The MAINHDR are defined in in crt0.s. This cannot be changed without
807 modifying and recompiling the cc65 atari runtime library. Therefore
808 the original contents of this segment must be discarded and be
809 replaced by a user created one. This discarding is done by assigning the
810 MAINHDR segment to the (new introduced) DISCARD memory area. The DISCARD memory area is
811 thrown away in the new linker config file (written to file "").
812 We add a new FSTHDR segment for the chunk header of the first chunk.
813 <p>
814 The user needs to create a customized linker config file which adds
815 new memory areas and segments to hold the new header data for the first load
816 chunk and the header data for the second load chunk. Also an assembly source file
817 needs to be created which defines the contents of the new header data
818 for the two load chunks.
819 <p>
820 <p>
821 This is an example of a modified cc65 Atari linker configuration file
822 (split.cfg):
823 <tscreen><verb>
824 SYMBOLS {
825     __STACKSIZE__:       value = $800   type = weak;    # 2K stack
826     __RESERVED_MEMORY__: value = $0000, type = weak;
827 }
828 FEATURES {
829     STARTADDRESS: default = $2E00;
830 }
831 MEMORY {
832     ZP: start = $82, size = $7E, type = rw, define = yes;
833
834     HEADER: start = $0000, size = $2, file = %O;        # first load chunk
835
836     FSTHDR: start = $0000, size = $4, file = %O;        # second load chunk
837     RAMLO: start = %S, size = $4000 - %S, file = %O;
838
839     DISCARD: start = $4000, size = $4000, file = "";
840
841     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
842     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
843 }
844 SEGMENTS {
845     EXEHDR: load = HEADER, type = ro;
846
847     MAINHDR: load = DISCARD, type = ro;
848
849     NEXEHDR: load = FSTHDR, type = ro;                  # first load chunk
850     STARTUP: load = RAMLO, type = ro, define = yes;
851     LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
852     ONCE: load = RAMLO, type = ro, optional = yes;
853     CODE: load = RAMLO, type = ro, define = yes;
854
855     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
856     RODATA: load = RAM, type = ro, define = yes;
857     DATA: load = RAM, type = rw, define = yes;
858     BSS: load = RAM, type = bss, define = yes;
859
860     ZEROPAGE: load = ZP, type = zp;
861     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
862 }
863 FEATURES {
864     CONDES: segment = ONCE,
865             type = constructor,
866             label = __CONSTRUCTOR_TABLE__,
867             count = __CONSTRUCTOR_COUNT__;
868     CONDES: segment = RODATA,
869             type = destructor,
870             label = __DESTRUCTOR_TABLE__,
871             count = __DESTRUCTOR_COUNT__;
872 }
873 </verb></tscreen>
874 <p>
875
876 A new memory area DISCARD was added.
877 It gets loaded with the contents of the (now unused) MAINHDR segment. But the
878 memory area isn't written to the output file. This way the contents of
879 the MAINHDR segment get discarded.
880 <p>
881 The newly added NEXEHDR segment defines the correct chunk header for the
882 first intended load chunk. It
883 puts the STARTUP, LOWCODE, ONCE, and CODE segments, which are the
884 segments containing only code, into load chunk #1 (RAMLO memory area).
885 <p>
886 The header for the second load chunk comes from the new CHKHDR
887 segment. It puts the RODATA, DATA, BSS, and ZPSAVE segments into load
888 chunk #2 (RAM memory area).
889 <p>
890 <p>
891 The contents of the new NEXEHDR and CHKHDR segments come from this
892 file (split.s):
893 <tscreen><verb>
894         .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
895         .import __DATA_LOAD__, __RODATA_LOAD__, __STARTUP_LOAD__
896
897         .segment "NEXEHDR"
898         .word    __STARTUP_LOAD__
899         .word    __CODE_LOAD__ + __CODE_SIZE__ - 1
900
901         .segment "CHKHDR"
902         .word    __RODATA_LOAD__
903         .word    __BSS_LOAD__ - 1
904 </verb></tscreen>
905 <p>
906 Compile with
907 <tscreen><verb>
908 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
909 </verb></tscreen>
910
911 <sect2>Low data and high code example<p>
912
913
914 Goal: Put RODATA and DATA into low memory and STARTUP, LOWCODE, ONCE,
915 CODE, BSS, ZPSAVE into high memory (split2.cfg):
916
917 <tscreen><verb>
918 SYMBOLS {
919     __STACKSIZE__:       value = $800   type = weak;    # 2K stack
920     __RESERVED_MEMORY__: value = $0000, type = weak;
921 }
922 FEATURES {
923     STARTADDRESS: default = $2E00;
924 }
925 MEMORY {
926     ZP: start = $82, size = $7E, type = rw, define = yes;
927
928     HEADER: start = $0000, size = $2, file = %O;        # first load chunk
929
930     FSTHDR: start = $0000, size = $4, file = %O;        # second load chunk
931     RAMLO: start = %S, size = $4000 - %S, file = %O;
932
933     DISCARD: start = $4000, size = $4000, file = "";
934
935     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
936     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
937 }
938 SEGMENTS {
939     EXEHDR: load = HEADER, type = ro;                     # discarded old EXE header
940
941     MAINHDR: load = DISCARD, type = ro;
942
943     NEXEHDR: load = FSTHDR, type = ro;                  # first load chunk
944     RODATA: load = RAMLO, type = ro, define = yes;
945     DATA: load = RAMLO, type = rw, define = yes;
946
947     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
948     STARTUP: load = RAM, type = ro, define = yes;
949     ONCE: load = RAM, type = ro, optional = yes;
950     CODE: load = RAM, type = ro, define = yes;
951     BSS: load = RAM, type = bss, define = yes;
952
953     ZEROPAGE: load = ZP, type = zp;
954     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
955 }
956 FEATURES {
957     CONDES: segment = ONCE,
958             type = constructor,
959             label = __CONSTRUCTOR_TABLE__,
960             count = __CONSTRUCTOR_COUNT__;
961     CONDES: segment = RODATA,
962             type = destructor,
963             label = __DESTRUCTOR_TABLE__,
964             count = __DESTRUCTOR_COUNT__;
965 }
966 </verb></tscreen>
967
968 New contents for NEXEHDR and CHKHDR are needed (split2.s):
969 <tscreen><verb>
970         .import __STARTUP_LOAD__, __BSS_LOAD__, __DATA_SIZE__
971         .import __DATA_LOAD__, __RODATA_LOAD__
972
973         .segment "NEXEHDR"
974         .word    __RODATA_LOAD__
975         .word    __DATA_LOAD__ + __DATA_SIZE__ - 1
976
977         .segment "CHKHDR"
978         .word    __STARTUP_LOAD__
979         .word    __BSS_LOAD__ - 1
980 </verb></tscreen>
981
982 Compile with
983 <tscreen><verb>
984 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
985 </verb></tscreen>
986
987 <sect2>Final note<label id="memhole_final_note"><p>
988
989 There are two other memory areas which don't appear directly in the
990 linker config file. They are the stack and the heap.
991
992 The cc65 runtime lib places the stack location at the end of available
993 memory. This is dynamically set from the MEMTOP system variable at
994 startup. The heap is located in the area between the end of the BSS
995 segment and the top of the stack as defined by __STACKSIZE__.
996
997 If BSS and/or the stack shouldn't stay at the end of the program,
998 some parts of the cc65 runtime lib need to be replaced/modified.
999
1000 common/_heap.s defines the location of the heap and atari/crt0.s
1001 defines the location of the stack by initializing sp.
1002
1003
1004 <sect1>Upgrading from an older cc65 version<p>
1005
1006 If you are using a customized linker config file you might get some errors
1007 regarding the MAINHDR segment. Like this:
1008
1009 <tscreen><verb>
1010 ld65: Error: Missing memory area assignment for segment `MAINHDR'
1011 </verb></tscreen>
1012
1013 The old "HEADER" memory description contained six bytes: &dollar;FFFF
1014 and the first and last memory addess of the program. For the "system
1015 check" load chunk this had to be split into two memory assigments. The
1016 "HEADER" now only contains the &dollar;FFFF. The main program's first
1017 and last memory address were moved to a new segment, called "MAINHDR",
1018 which in the new linker config file goes into its own memory area (also
1019 called "MAINHDR").&nl;&nl;
1020 A simple way to adapt your old linker config file is to add the
1021 following line to the "SEGMENTS" section:
1022
1023 <tscreen><verb>
1024 MAINHDR: load = HEADER, type = ro;
1025 </verb></tscreen>
1026
1027
1028
1029 <sect1>Getting rid of the "system check" load chunk<label id="nosyschk"><p>
1030
1031 If, for some reason, you don't want to include the "system check" load
1032 chunk, you can do so by defining the symbol <tt/__SYSTEM_CHECK__/ when linking the
1033 program. The "system check" chunk doesn't include vital parts of the
1034 program. So if you don't want the system checks, it is save to leave them out.
1035 This is probably mostly interesting for debugging.
1036
1037 When using cl65, you can leave it out with this command line:
1038
1039 <tscreen><verb>
1040 cl65 -Wl -D__SYSTEM_CHECK__=1 <arguments>
1041 </verb></tscreen>
1042
1043 The value you assign to <tt/__SYSTEM_CHECK_/ doesn't matter. If the
1044 <tt/__SYSTEM_CHECK__/ symbol is defined, the load chunk won't be included.
1045
1046
1047 <sect>License<p>
1048
1049 This software is provided 'as-is', without any expressed or implied
1050 warranty.  In no event will the authors be held liable for any damages
1051 arising from the use of this software.
1052
1053 Permission is granted to anyone to use this software for any purpose,
1054 including commercial applications, and to alter it and redistribute it
1055 freely, subject to the following restrictions:
1056
1057 <enum>
1058 <item>  The origin of this software must not be misrepresented; you must not
1059         claim that you wrote the original software. If you use this software
1060         in a product, an acknowledgment in the product documentation would be
1061         appreciated but is not required.
1062 <item>  Altered source versions must be plainly marked as such, and must not
1063         be misrepresented as being the original software.
1064 <item>  This notice may not be removed or altered from any source
1065         distribution.
1066 </enum>
1067
1068 </article>