| 1 |
ATARI800 AS A LIBRARY |
| 2 |
===================== |
| 3 |
|
| 4 |
Libatari800 is a build target of the atari800 emulator that produces a static |
| 5 |
library suitable for embedding in other programs. See the INSTALL file for |
| 6 |
complete build instructions, but in most cases it can be compiled from the top |
| 7 |
level source directory using: |
| 8 |
|
| 9 |
./configure --target=libatari800 |
| 10 |
make |
| 11 |
|
| 12 |
This target requires no external libraries and is platform independent. It is |
| 13 |
also not useful by itself; instead it is designed for developers to embed the |
| 14 |
emulator into another program. |
| 15 |
|
| 16 |
Two sample programs are also compiled (but not installed) that demonstrate the |
| 17 |
usage of the library: guess_settings and libatari800_test. |
| 18 |
|
| 19 |
Using libatari800 to guess emulator settings |
| 20 |
-------------------------------------------- |
| 21 |
|
| 22 |
The demo program guess_settings (source in util/guess_settings.c) attempts to |
| 23 |
guess the major emulator settings (parameters like machine type, operating |
| 24 |
system type, and PAL/NTSC) by running the emulator as fast as possible. If a |
| 25 |
failure is detected (like hitting a BRK instruction, crashing the computer, or |
| 26 |
entering the memo pad/self-test mode) the failure is noted and the next |
| 27 |
permutation is checked. |
| 28 |
|
| 29 |
If the source image is a cartridge and the cartridge type is unknown, all |
| 30 |
cartridge types that match the source size are checked in turn. This can |
| 31 |
greatly increase the number of permutations. |
| 32 |
|
| 33 |
A success is determined by the absence of failure conditions in a specified |
| 34 |
number of frames (default of 1000). |
| 35 |
|
| 36 |
The program is built automatically (but not installed) when the compile target |
| 37 |
is libatari800. It is built in the src directory and can be run from there |
| 38 |
with: |
| 39 |
|
| 40 |
src/guess_settings [options] [name_of_image] |
| 41 |
|
| 42 |
For example: |
| 43 |
|
| 44 |
$ src/guess_settings test.xex |
| 45 |
test.xex: 400/800 NTSC OS/B status: OK through 1000 frames |
| 46 |
test.xex: 400/800 PAL OS/B status: OK through 1000 frames |
| 47 |
test.xex: 400/800 NTSC Altirra status: FAIL (CPU crash) |
| 48 |
test.xex: 400/800 PAL Altirra status: FAIL (CPU crash) |
| 49 |
test.xex: 64k XL NTSC XL ROM status: FAIL (CPU crash) |
| 50 |
test.xex: 64k XL PAL XL ROM status: FAIL (CPU crash) |
| 51 |
test.xex: 64k XL NTSC Altirra status: FAIL (CPU crash) |
| 52 |
test.xex: 64k XL PAL Altirra status: FAIL (CPU crash) |
| 53 |
test.xex: 128k XE NTSC XL ROM status: FAIL (CPU crash) |
| 54 |
test.xex: 128k XE PAL XL ROM status: FAIL (CPU crash) |
| 55 |
test.xex: 128k XE NTSC Altirra status: FAIL (CPU crash) |
| 56 |
test.xex: 128k XE PAL Altirra status: FAIL (CPU crash) |
| 57 |
test.xex: 5200 NTSC Atari status: FAIL (CPU crash) |
| 58 |
test.xex: 5200 NTSC Altirra status: FAIL (CPU crash) |
| 59 |
|
| 60 |
Here's an example with an 8k cartridge image, using additional command line |
| 61 |
arguments to limit the testing to only NTSC XL machines: |
| 62 |
|
| 63 |
$ src/guess_settings -xl -ntsc test.rom |
| 64 |
test.rom: 64k XL NTSC XL ROM status: OK through 1000 frames (cart=1 'Standard 8 KB') |
| 65 |
test.rom: 64k XL NTSC XL ROM status: FAIL (self test) (cart=21 'Right slot 8 KB') |
| 66 |
test.rom: 64k XL NTSC XL ROM status: OK through 1000 frames (cart=39 'Phoenix 8 KB') |
| 67 |
test.rom: 64k XL NTSC XL ROM status: FAIL (self test) (cart=44 'OSS 8 KB') |
| 68 |
test.rom: 64k XL NTSC XL ROM status: FAIL (BRK instruction) (cart=53 'Low bank 8 KB') |
| 69 |
test.rom: 64k XL NTSC XL ROM status: FAIL (unidentified cartridge) |
| 70 |
test.rom: 64k XL NTSC Altirra status: OK through 1000 frames (cart=1 'Standard 8 KB') |
| 71 |
test.rom: 64k XL NTSC Altirra status: FAIL (self test) (cart=21 'Right slot 8 KB') |
| 72 |
test.rom: 64k XL NTSC Altirra status: OK through 1000 frames (cart=39 'Phoenix 8 KB') |
| 73 |
test.rom: 64k XL NTSC Altirra status: FAIL (self test) (cart=44 'OSS 8 KB') |
| 74 |
test.rom: 64k XL NTSC Altirra status: FAIL (self test) (cart=53 'Low bank 8 KB') |
| 75 |
test.rom: 64k XL NTSC Altirra status: FAIL (unidentified cartridge) |
| 76 |
|
| 77 |
|
| 78 |
Using libatari800 to generate video frames |
| 79 |
------------------------------------------ |
| 80 |
|
| 81 |
The test program libatari800_test (source in util/libatari800_test.c) is also |
| 82 |
built but not installed with the libatari800 compile target. It can be run |
| 83 |
using: |
| 84 |
|
| 85 |
src/libatari800_test |
| 86 |
|
| 87 |
It is a very simple example. It boots the emulator into Memo Pad, and after 100 |
| 88 |
frames of emulation, simulates the 'A' key being pressed and held down to print |
| 89 |
a bunch of "A" characters on the screen. A simple representation of the screen |
| 90 |
is displayed as text output to the terminal. |
| 91 |
|
| 92 |
|
| 93 |
LIBRARY OVERVIEW |
| 94 |
================ |
| 95 |
|
| 96 |
The basic operation of the library is to generate one video frame of emulation, |
| 97 |
which is equivalent to about 30,000 machine cycles on a 60 Hz NTSC system. The |
| 98 |
library provides access to the raw screen buffer and other internals like the |
| 99 |
user memory, but produces no output itself. Any video display, sound output, or |
| 100 |
for that matter any user interface is left up to the calling program. |
| 101 |
|
| 102 |
The library will calculate each video frame as fast as it can, with no delay |
| 103 |
and no synchronization to any 50/60 Hz signal at all. It is up to the user |
| 104 |
program to delay the processing of the next frame if real-time operation is |
| 105 |
desired. |
| 106 |
|
| 107 |
This also means that each frame can be calculated much much faster than the |
| 108 |
real hardware, so for example: simulation tasks may be performed very quickly |
| 109 |
and the results can be displayed only after all the processing has been |
| 110 |
completed. |
| 111 |
|
| 112 |
|
| 113 |
Usage |
| 114 |
----- |
| 115 |
|
| 116 |
The call to libatari800_next_frame is the basic interface to the emulator. It |
| 117 |
accepts a pointer to a structure input_template_t, which describes user input |
| 118 |
like the keyboard and joystick state. It performs one full frame's worth of |
| 119 |
emulation, and returns control to the calling function. |
| 120 |
|
| 121 |
The screen array and audio buffer are available at the end of every frame for |
| 122 |
the calling function to present to the user. |
| 123 |
|
| 124 |
The example program libatari800_test shows very simple usage of the library. |
| 125 |
The basic usage to generate emulator frames is shown in this code snippet:: |
| 126 |
|
| 127 |
input_template_t input; |
| 128 |
char *test_args[] = { |
| 129 |
"-atari", |
| 130 |
NULL, |
| 131 |
}; |
| 132 |
|
| 133 |
libatari800_init(-1, test_args); |
| 134 |
|
| 135 |
libatari800_clear_input_array(&input); |
| 136 |
|
| 137 |
while (libatari800_get_frame_number() < 200) { |
| 138 |
libatari800_next_frame(&input); |
| 139 |
if (libatari800_get_frame_number() > 100) { |
| 140 |
input.keychar = 'A'; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
libatari800_exit(); |
| 145 |
|
| 146 |
Note the usage of the test_args array that mimics command line arguments. In the |
| 147 |
future, libatari800 may provide more direct specification of configuration |
| 148 |
parameters, but this is not yet implemented. |
| 149 |
|
| 150 |
So, currently, libatari800 relies on a valid configuration file to specify the |
| 151 |
location of the ROM files. By default, libatari800 will scan in the same |
| 152 |
directories as the normal atari800 program. To provide an alternate location, |
| 153 |
add the "-config" argument and a path to the file, as in the following:: |
| 154 |
|
| 155 |
char *test_args[] = { |
| 156 |
"-atari", |
| 157 |
"-config", |
| 158 |
"/path/to/.atari800.cfg", |
| 159 |
NULL, |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
Advanced usage |
| 164 |
-------------- |
| 165 |
|
| 166 |
In addition to the screen and audio, the entire internal state of the emulator |
| 167 |
can be examined and even changed between frames. The state save format is used |
| 168 |
to facilitate this, so it is even possible restore the state of the emulator |
| 169 |
from save files generated from another atari800 instance. |
| 170 |
|
| 171 |
State save files are difficult to parse as the size of the file (and indeed |
| 172 |
offsets to elements within the file) can change from frame to frame depending |
| 173 |
on the current state of the emulator or features currently being used. For |
| 174 |
example, changing the number of disk drives in use or even changing the disk |
| 175 |
image to point to a different file on the host file system will alter the size |
| 176 |
of the state save file. |
| 177 |
|
| 178 |
To simplify access and prevent the need for parsing the state save file, some |
| 179 |
offsets into the state save file are created and saved into the statesav_tags_t |
| 180 |
structure. Each entry in the structure points to the first byte in that section |
| 181 |
of the state save buffer. For instance, access to the current program counter |
| 182 |
can be found by: |
| 183 |
|
| 184 |
unsigned char state[STATESAV_MAX_SIZE]; |
| 185 |
statesav_tags_t tags; |
| 186 |
pc_state_t *pc; |
| 187 |
|
| 188 |
libatari800_get_current_state(state, &tags); |
| 189 |
pc = (pc_state_t *)&state.state[state.tags.pc]; |
| 190 |
printf("CPU PC=%04x\n", pc->PC); |
| 191 |
|
| 192 |
|
| 193 |
Overview of source code changes |
| 194 |
------------------------------- |
| 195 |
|
| 196 |
The port-specific code lives in the src/libatari800 directory, but some changes |
| 197 |
were needed in the main source code directory. |
| 198 |
|
| 199 |
The libatari800 port differences include: |
| 200 |
|
| 201 |
* the PNG and LIBZ libraries are deliberately excluded for maximal portability; |
| 202 |
only the system math library is needed |
| 203 |
|
| 204 |
* the system monitor is excluded, so any ability stop the 6502 other than at |
| 205 |
the end of a frame is not available. |
| 206 |
|
| 207 |
* the text-based UI to configure the emulator, select cartridges, debug with |
| 208 |
the monitor, etc. is not included. Everything is left to the calling program. |
| 209 |
|
| 210 |
* the crash menu is excluded |
| 211 |
|
| 212 |
* some instrumentation of the state save file has been added |
| 213 |
|
| 214 |
|
| 215 |
LIBRARY FUNCTIONS |
| 216 |
================= |
| 217 |
|
| 218 |
int libatari800_init (int argc, char ** argv) |
| 219 |
Initialize emulator configuration |
| 220 |
|
| 221 |
Sets emulator configuration using the supplied argument list. The arguments correspond to |
| 222 |
command line arguments for the atari800 program, see its manual page for more information |
| 223 |
on arguments and their functions. |
| 224 |
|
| 225 |
Parameters |
| 226 |
argc number of arguments in argv, or -1 if argv contains a NULL terminated list. |
| 227 |
argv list of arguments. |
| 228 |
|
| 229 |
Return values |
| 230 |
FALSE if error in argument list |
| 231 |
TRUE if successful |
| 232 |
|
| 233 |
|
| 234 |
const char* libatari800_error_message () |
| 235 |
Get text description of latest error message |
| 236 |
|
| 237 |
If the libatari800_next_frame return value indicates an error condition, this function |
| 238 |
will return an error message suitable for display to the user. |
| 239 |
|
| 240 |
Returns |
| 241 |
text description of error |
| 242 |
|
| 243 |
|
| 244 |
void libatari800_clear_input_array (input_template_t * input) |
| 245 |
Clears input array structure |
| 246 |
|
| 247 |
Clears any user input (keystrokes, joysticks, paddles) in the input array structure to |
| 248 |
indicate that no user input is taking place. |
| 249 |
|
| 250 |
This should be called to initialize the input array before the first call to |
| 251 |
libatari800_next_frame. |
| 252 |
|
| 253 |
Parameters |
| 254 |
input pointer to input array structure |
| 255 |
|
| 256 |
|
| 257 |
int libatari800_next_frame (input_template_t * input) |
| 258 |
Perform one video frame's worth of emulation |
| 259 |
|
| 260 |
This is the main driver for libatari800. This function runs the emulator for enough CPU |
| 261 |
cycles to produce one video frame's worth of emulation. Results of the frame can be |
| 262 |
retrieved using the libatari800_get_* functions. |
| 263 |
|
| 264 |
Parameters |
| 265 |
input input template structure defining the user input for the frame |
| 266 |
|
| 267 |
Return values |
| 268 |
0 successfully emulated frame |
| 269 |
1 unidentified cartridge type |
| 270 |
2 CPU crash |
| 271 |
3 BRK instruction encountered |
| 272 |
4 display list error |
| 273 |
5 entered self-test mode |
| 274 |
6 entered Memo Pad |
| 275 |
7 encountered invalid escape opcode |
| 276 |
|
| 277 |
|
| 278 |
int libatari800_mount_disk_image (int diskno, const char * filename, int readonly) |
| 279 |
Use disk image in a disk drive |
| 280 |
|
| 281 |
Insert a virtual floppy image into one of the emulated disk drives. Currently supported |
| 282 |
formats include ATR, ATX, DCM, PRO, XFD. |
| 283 |
|
| 284 |
Parameters |
| 285 |
diskno disk drive number (1 - 8) |
| 286 |
filename path to disk image |
| 287 |
readonly if TRUE will be mounted as read-only |
| 288 |
|
| 289 |
Return values |
| 290 |
FALSE if error |
| 291 |
TRUE if successful |
| 292 |
|
| 293 |
|
| 294 |
int libatari800_reboot_with_file (const char * filename) |
| 295 |
Restart emulation using file |
| 296 |
|
| 297 |
Perform a cold start with a disk image, executable file, cartridge, cassette image, BASIC |
| 298 |
file, or atari800 state save file. |
| 299 |
|
| 300 |
This is currently the only way to load and have the emulator run an executable file or |
| 301 |
BASIC program without a boot disk image. The atari800 emulator includes a built-in |
| 302 |
bootloader for these files but is only available at machine start. |
| 303 |
|
| 304 |
Parameters |
| 305 |
path to file |
| 306 |
|
| 307 |
Returns |
| 308 |
file type, or 0 for error |
| 309 |
|
| 310 |
|
| 311 |
UBYTE* libatari800_get_main_memory_ptr () |
| 312 |
Return pointer to main memory |
| 313 |
|
| 314 |
This is actual array containing the emulator's main bank of 64k of RAM. Changes made here |
| 315 |
will be reflected in the state of the emulator, potentially causing the emulated CPU to |
| 316 |
halt if it encounters an illegal instruction. |
| 317 |
|
| 318 |
Accessing memory through this pointer will not return hardware register information, this |
| 319 |
provides access to the RAM only. |
| 320 |
|
| 321 |
Returns |
| 322 |
pointer to the beginning of the 64k block of main memory |
| 323 |
|
| 324 |
|
| 325 |
UBYTE* libatari800_get_screen_ptr () |
| 326 |
Return pointer to screen data |
| 327 |
|
| 328 |
The emulated screen is an array of 384x240 bytes in scan line order with the top of the |
| 329 |
screen at the beginning of the array. Each byte represents the color index of a pixel in |
| 330 |
the usual Atari palette (high 4 bits are the hue and low 4 bits are the luminance). |
| 331 |
|
| 332 |
The large size of the screen includes the overscan areas not usually displayed. Typical |
| 333 |
margins will be 24 on each the right and left, and 16 each on the top and bottom, leaving |
| 334 |
an 8 pixel margin on all sides for the normal 320x192 addressable pixels. |
| 335 |
|
| 336 |
Note that the screen is output only, and changes to this array will have no effect on the |
| 337 |
emulation. |
| 338 |
|
| 339 |
Returns |
| 340 |
pointer to the beginning of the 92160 bytes of data holding the emulated screen. |
| 341 |
|
| 342 |
|
| 343 |
UBYTE* libatari800_get_sound_buffer () |
| 344 |
Return pointer to sound data |
| 345 |
|
| 346 |
If sound is used, each emulated frame will fill the sound buffer with samples at the |
| 347 |
configured audio sample rate. |
| 348 |
|
| 349 |
Because the emulation runs at a non-integer frame rate (approximately 59.923 frames per |
| 350 |
second in NTSC, 49.861 fps in PAL), the number of samples is not a constant for all |
| 351 |
frames -- it can vary by one sample per frame. For example, in NTSC with a sample rate of |
| 352 |
44.1KHz, most frames will contain 736 samples, but one out of about every 19 frames will |
| 353 |
contain 735 samples. |
| 354 |
|
| 355 |
Use the function libatari800_get_sound_buffer_len to determine usable size of the sound |
| 356 |
buffer. |
| 357 |
|
| 358 |
Returns |
| 359 |
pointer to the beginning of the sound sample buffer |
| 360 |
|
| 361 |
|
| 362 |
int libatari800_get_sound_buffer_len () |
| 363 |
Return the usable size of the sound buffer. |
| 364 |
|
| 365 |
Returns |
| 366 |
number of bytes of valid data in the sound buffer |
| 367 |
|
| 368 |
|
| 369 |
int libatari800_get_sound_buffer_allocated_size () |
| 370 |
Return the maximum size of the sound buffer. |
| 371 |
|
| 372 |
Returns |
| 373 |
number of bytes allocated in sound buffer |
| 374 |
|
| 375 |
|
| 376 |
int libatari800_get_sound_frequency () |
| 377 |
Return the audio sample rate in samples per second |
| 378 |
|
| 379 |
Returns |
| 380 |
the audio sample rate, typically 44100 or 48000 |
| 381 |
|
| 382 |
|
| 383 |
int libatari800_get_num_sound_channels () |
| 384 |
Return the number of audio channels |
| 385 |
|
| 386 |
Return values |
| 387 |
1 mono |
| 388 |
2 stereo |
| 389 |
|
| 390 |
|
| 391 |
int libatari800_get_sound_sample_size () |
| 392 |
Return the sample size in bytes of each audio sample |
| 393 |
|
| 394 |
Return values |
| 395 |
1 8-bit audio |
| 396 |
2 16-bit audio |
| 397 |
|
| 398 |
|
| 399 |
float libatari800_get_fps () |
| 400 |
Return the video frame rate |
| 401 |
|
| 402 |
It is important to note that libatari800 can run as fast as the host computer will allow, |
| 403 |
but simulates operation as if it were running at NTSC or PAL frame rates. It is up to the |
| 404 |
calling program to display frames at the correct rate. |
| 405 |
|
| 406 |
The NTSC frame rate is 59.9227434 frames per second, and PAL is 49.8607597 fps. |
| 407 |
|
| 408 |
Returns |
| 409 |
floating point number representing the frame rate. |
| 410 |
|
| 411 |
|
| 412 |
int libatari800_get_frame_number () |
| 413 |
Return the number of frames of emulation in the current state of the emulator |
| 414 |
|
| 415 |
This is also equivalent to the number of times libatari800_next_frame has been called |
| 416 |
since the initialization of libatarii800. |
| 417 |
|
| 418 |
Calls to libatari800_init, cold starts, and warm starts do not reset this number; it will |
| 419 |
continue to grow after these events. This value can get reset, however, if the emulator |
| 420 |
state is restored from a previously saved state. In this case, the number of frames will |
| 421 |
be restored to the value from the saved state. |
| 422 |
|
| 423 |
Returns |
| 424 |
number of frames that have been generated, or zero if no libatari800_next_frame has |
| 425 |
not been called yet. |
| 426 |
|
| 427 |
|
| 428 |
void libatari800_get_current_state (emulator_state_t * state) |
| 429 |
Save the state of the emulator |
| 430 |
|
| 431 |
Save the state of the emulator into a data structure that can later be used to restore |
| 432 |
the emulator to this state using a call to libatari800_restore_state. |
| 433 |
|
| 434 |
The emulator state structure can be examined by using the structure member tags as an |
| 435 |
offset to locate the subsystem of interest, and casting the resulting offset into its own |
| 436 |
struct. E.g. to find the value of the CPU registers and the current program counter, this |
| 437 |
code: |
| 438 |
|
| 439 |
emulator_state_t state; |
| 440 |
cpu_state_t *cpu; |
| 441 |
pc_state_t *pc; |
| 442 |
|
| 443 |
libatari800_get_current_state(&state); |
| 444 |
cpu = (cpu_state_t *)&state.state[state.tags.cpu]; |
| 445 |
pc = (pc_state_t *)&state.state[state.tags.pc]; |
| 446 |
printf("CPU A=%02x X=%02x Y=%02x PC=%04x0, cpu->A, cpu->X, cpu->Y, pc->PC); |
| 447 |
|
| 448 |
gets the current state of the emulator, locates the cpu_state_t and the |
| 449 |
pc_state_t structures within it, and prints the values of interest. |
| 450 |
|
| 451 |
Parameters |
| 452 |
state pointer to an already allocated emulator_state_t structure |
| 453 |
|
| 454 |
|
| 455 |
void libatari800_restore_state (emulator_state_t * state) |
| 456 |
Restore the state of the emulator |
| 457 |
|
| 458 |
Return the emulator to a previous state as defined by a previous call to |
| 459 |
libatari800_get_current_state. |
| 460 |
|
| 461 |
Minimal error checking is performed on the data in state, so if the data in state has |
| 462 |
been altered it is possible that the emulator will be returned to an invalid state and |
| 463 |
further emulation will fail. |
| 464 |
|
| 465 |
Parameters |
| 466 |
state pointer to an already allocated emulator_state_t structure |
| 467 |
|
| 468 |
|
| 469 |
void libatari800_exit () |
| 470 |
Free resources used by the emulator. |
| 471 |
|
| 472 |
Release any memory or other resources used by the emulator. Further calls to |
| 473 |
libatari800_* functions are not permitted after a call to this function, and attempting |
| 474 |
to do so will have undefined behavior and likely crash the program. |