home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / atari / atari800-0.8.6 / atari.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-11  |  45.1 KB  |  1,475 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <signal.h>
  6. #include <sys/time.h>
  7. #include <unistd.h>
  8.  
  9. #ifdef VMS
  10. #include <unixio.h>
  11. #include <file.h>
  12. #else
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. #ifdef DJGPP
  17. #include "djgpp.h"
  18. #endif
  19.  
  20. #ifdef AMIGA_PPC
  21. #include <time.h>
  22. #include <clib/powerpc_protos.h>
  23. #endif
  24.  
  25. static char *rcsid = "$Id: atari.c,v 1.48 1998/02/21 15:22:04 david Exp $";
  26.  
  27. #define FALSE   0
  28. #define TRUE    1
  29.  
  30. #include "atari.h"
  31. #include "cpu.h"
  32. #include "antic.h"
  33. #include "gtia.h"
  34. #include "pia.h"
  35. #include "pokey.h"
  36. #include "supercart.h"
  37. #include "devices.h"
  38. #include "sio.h"
  39. #include "monitor.h"
  40. #include "platform.h"
  41. #include "prompts.h"
  42. #include "rt-config.h"
  43.  
  44. TVmode tv_mode = PAL;
  45. Machine machine = Atari;
  46. int verbose = FALSE;
  47. double fps;
  48. int nframes;
  49. ULONG lastspeed;                                /* measure time between two Antic runs */
  50.  
  51. static int os = 2;
  52. static int pil_on = FALSE;
  53.  
  54. long firsttime;
  55.  
  56. extern UBYTE NMIEN;
  57. extern UBYTE NMIST;
  58. extern UBYTE PORTA;
  59. extern UBYTE PORTB;
  60.  
  61. extern int xe_bank;
  62. extern int rom_inserted;
  63. extern UBYTE atari_basic[8192];
  64. extern UBYTE atarixl_os[16384];
  65.  
  66. UBYTE *cart_image = NULL;               /* For cartridge memory */
  67. int cart_type = NO_CART;
  68. int DELAYED_SERIN_IRQ;
  69. int DELAYED_SEROUT_IRQ;
  70. int DELAYED_XMTDONE_IRQ;
  71. int countdown_rate = 4000;
  72. double deltatime;
  73.  
  74. #ifdef BACKUP_MSG
  75. char backup_msg_buf[512];
  76. #endif
  77.  
  78. void add_esc(UWORD address, UBYTE esc_code);
  79. int Atari800_Exit(int run_monitor);
  80. void Atari800_Hardware(void);
  81.  
  82. int load_cart(char *filename, int type);
  83.  
  84. static char *rom_filename = NULL;
  85.  
  86. void sigint_handler(int num)
  87. {
  88.         int restart;
  89.         int diskno;
  90.  
  91.         restart = Atari800_Exit(TRUE);
  92.         if (restart) {
  93.                 signal(SIGINT, sigint_handler);
  94.                 return;
  95.         }
  96.         for (diskno = 1; diskno < 8; diskno++)
  97.                 SIO_Dismount(diskno);
  98.  
  99.         exit(0);
  100. }
  101.  
  102.  
  103. int load_image(char *filename, int addr, int nbytes)
  104. {
  105.         int status = FALSE;
  106.         int fd;
  107.  
  108.         fd = open(filename, O_RDONLY, 0777);
  109.         if (fd != -1) {
  110.                 status = read(fd, &memory[addr], nbytes);
  111.                 if (status != nbytes) {
  112. #ifdef BACKUP_MSG
  113.                         sprintf(backup_msg_buf, "Error reading %s\n", filename);
  114. #else
  115.                         printf("Error reading %s\n", filename);
  116. #endif
  117.                         Atari800_Exit(FALSE);
  118.                         exit(1);
  119.                 }
  120.                 close(fd);
  121.  
  122.                 status = TRUE;
  123.         }
  124.         else if (verbose) {
  125.                 perror(filename);
  126.         }
  127.         return status;
  128. }
  129.  
  130. void EnablePILL(void)
  131. {
  132.         if (os != 3) {                          /* Disable PIL when running Montezumma's Revenge */
  133.                 SetROM(0x8000, 0xbfff);
  134.                 pil_on = TRUE;
  135.         }
  136. }
  137.  
  138. void DisablePILL(void)
  139. {
  140.         if (os != 3) {                          /* Disable PIL when running Montezumma's Revenge */
  141.                 SetRAM(0x8000, 0xbfff);
  142.                 pil_on = FALSE;
  143.         }
  144. }
  145.  
  146. /*
  147.  * Load a standard 8K ROM from the specified file
  148.  */
  149.  
  150. int Insert_8K_ROM(char *filename)
  151. {
  152.         int status = FALSE;
  153.         int fd;
  154.  
  155.         fd = open(filename, O_RDONLY, 0777);
  156.         if (fd != -1) {
  157.                 read(fd, &memory[0xa000], 0x2000);
  158.                 close(fd);
  159.                 SetRAM(0x8000, 0x9fff);
  160.                 SetROM(0xa000, 0xbfff);
  161.                 cart_type = NORMAL8_CART;
  162.                 rom_inserted = TRUE;
  163.                 status = TRUE;
  164.         }
  165.         return status;
  166. }
  167.  
  168. /*
  169.  * Load a standard 16K ROM from the specified file
  170.  */
  171.  
  172. int Insert_16K_ROM(char *filename)
  173. {
  174.         int status = FALSE;
  175.         int fd;
  176.  
  177.         fd = open(filename, O_RDONLY, 0777);
  178.         if (fd != -1) {
  179.                 read(fd, &memory[0x8000], 0x4000);
  180.                 close(fd);
  181.                 SetROM(0x8000, 0xbfff);
  182.                 cart_type = NORMAL16_CART;
  183.                 rom_inserted = TRUE;
  184.                 status = TRUE;
  185.         }
  186.         return status;
  187. }
  188.  
  189. /*
  190.  * Load an OSS Supercartridge from the specified file
  191.  * The OSS cartridge is a 16K bank switched cartridge
  192.  * that occupies 8K of address space between $a000
  193.  * and $bfff
  194.  */
  195.  
  196. int Insert_OSS_ROM(char *filename)
  197. {
  198.         int status = FALSE;
  199.         int fd;
  200.  
  201.         fd = open(filename, O_RDONLY, 0777);
  202.         if (fd != -1) {
  203.                 cart_image = (UBYTE *) malloc(0x4000);
  204.                 if (cart_image) {
  205.                         read(fd, cart_image, 0x4000);
  206.                         memcpy(&memory[0xa000], cart_image, 0x1000);
  207.                         memcpy(&memory[0xb000], cart_image + 0x3000, 0x1000);
  208.                         SetRAM(0x8000, 0x9fff);
  209.                         SetROM(0xa000, 0xbfff);
  210.                         cart_type = OSS_SUPERCART;
  211.                         rom_inserted = TRUE;
  212.                         status = TRUE;
  213.                 }
  214.                 close(fd);
  215.         }
  216.         return status;
  217. }
  218.  
  219. /*
  220.  * Load a DB Supercartridge from the specified file
  221.  * The DB cartridge is a 32K bank switched cartridge
  222.  * that occupies 16K of address space between $8000
  223.  * and $bfff
  224.  */
  225.  
  226. int Insert_DB_ROM(char *filename)
  227. {
  228.         int status = FALSE;
  229.         int fd;
  230.  
  231.         fd = open(filename, O_RDONLY, 0777);
  232.         if (fd != -1) {
  233.                 cart_image = (UBYTE *) malloc(0x8000);
  234.                 if (cart_image) {
  235.                         read(fd, cart_image, 0x8000);
  236.                         memcpy(&memory[0x8000], cart_image, 0x2000);
  237.                         memcpy(&memory[0xa000], cart_image + 0x6000, 0x2000);
  238.                         SetROM(0x8000, 0xbfff);
  239.                         cart_type = DB_SUPERCART;
  240.                         rom_inserted = TRUE;
  241.                         status = TRUE;
  242.                 }
  243.                 close(fd);
  244.         }
  245.         return status;
  246. }
  247.  
  248. /*
  249.  * Load a 32K 5200 ROM from the specified file
  250.  */
  251.  
  252. int Insert_32K_5200ROM(char *filename)
  253. {
  254.         int status = FALSE;
  255.         int fd;
  256.  
  257.         fd = open(filename, O_RDONLY, 0777);
  258.         if (fd != -1) {
  259.                 read(fd, &memory[0x4000], 0x8000);
  260.                 close(fd);
  261.                 SetROM(0x4000, 0xbfff);
  262.                 cart_type = AGS32_CART;
  263.                 rom_inserted = TRUE;
  264.                 status = TRUE;
  265.         }
  266.         return status;
  267. }
  268.  
  269. /*
  270.  * This removes any loaded cartridge ROM files from the emulator
  271.  * It doesn't remove either the OS, FFP or character set ROMS.
  272.  */
  273.  
  274. int Remove_ROM(void)
  275. {
  276.         if (cart_image) {                       /* Release memory allocated for Super Cartridges */
  277.                 free(cart_image);
  278.                 cart_image = NULL;
  279.         }
  280.         SetRAM(0x8000, 0xbfff);         /* Ensure cartridge area is RAM */
  281.         cart_type = NO_CART;
  282.         rom_inserted = FALSE;
  283.  
  284.         return TRUE;
  285. }
  286.  
  287. int Insert_Cartridge(char *filename)
  288. {
  289.         int status = FALSE;
  290.         int fd;
  291.  
  292.         fd = open(filename, O_RDONLY, 0777);
  293.         if (fd != -1) {
  294.                 UBYTE header[16];
  295.  
  296.                 read(fd, header, sizeof(header));
  297.                 if ((header[0] == 'C') &&
  298.                         (header[1] == 'A') &&
  299.                         (header[2] == 'R') &&
  300.                         (header[3] == 'T')) {
  301.                         int type;
  302.                         int checksum;
  303.  
  304.                         type = (header[4] << 24) |
  305.                                 (header[5] << 16) |
  306.                                 (header[6] << 8) |
  307.                                 header[7];
  308.  
  309.                         checksum = (header[4] << 24) |
  310.                                 (header[5] << 16) |
  311.                                 (header[6] << 8) |
  312.                                 header[7];
  313.  
  314.                         switch (type) {
  315. #define STD_8K 1
  316. #define STD_16K 2
  317. #define OSS 3
  318. #define AGS 4
  319.                         case STD_8K:
  320.                                 read(fd, &memory[0xa000], 0x2000);
  321.                                 SetRAM(0x8000, 0x9fff);
  322.                                 SetROM(0xa000, 0xbfff);
  323.                                 cart_type = NORMAL8_CART;
  324.                                 rom_inserted = TRUE;
  325.                                 status = TRUE;
  326.                                 break;
  327.                         case STD_16K:
  328.                                 read(fd, &memory[0x8000], 0x4000);
  329.                                 SetROM(0x8000, 0xbfff);
  330.                                 cart_type = NORMAL16_CART;
  331.                                 rom_inserted = TRUE;
  332.                                 status = TRUE;
  333.                                 break;
  334.                         case OSS:
  335.                                 cart_image = (UBYTE *) malloc(0x4000);
  336.                                 if (cart_image) {
  337.                                         read(fd, cart_image, 0x4000);
  338.                                         memcpy(&memory[0xa000], cart_image, 0x1000);
  339.                                         memcpy(&memory[0xb000], cart_image + 0x3000, 0x1000);
  340.                                         SetRAM(0x8000, 0x9fff);
  341.                                         SetROM(0xa000, 0xbfff);
  342.                                         cart_type = OSS_SUPERCART;
  343.                                         rom_inserted = TRUE;
  344.                                         status = TRUE;
  345.                                 }
  346.                                 break;
  347.                         case AGS:
  348.                                 read(fd, &memory[0x4000], 0x8000);
  349.                                 close(fd);
  350.                                 SetROM(0x4000, 0xbfff);
  351.                                 cart_type = AGS32_CART;
  352.                                 rom_inserted = TRUE;
  353.                                 status = TRUE;
  354.                                 break;
  355.                         default:
  356.                                 printf("%s is in unsupported cartridge format %d\n",
  357.                                            filename, type);
  358.                                 break;
  359.                         }
  360.                 }
  361.                 else {
  362.                         printf("%s is not a cartridge\n", filename);
  363.                 }
  364.                 close(fd);
  365.         }
  366.         return status;
  367. }
  368.  
  369. void PatchOS(void)
  370. {
  371.         const unsigned short o_open = 0;
  372.         const unsigned short o_close = 2;
  373.         const unsigned short o_read = 4;
  374.         const unsigned short o_write = 6;
  375.         const unsigned short o_status = 8;
  376.         /* const unsigned short   o_special = 10; */
  377.         const unsigned short o_init = 12;
  378.  
  379.         unsigned short addr;
  380.         unsigned short entry;
  381.         unsigned short devtab;
  382.         int i;
  383. /*
  384.    =====================
  385.    Disable Checksum Test
  386.    =====================
  387.  */
  388.         if (enable_sio_patch)
  389.                 add_esc(0xe459, ESC_SIOV);
  390.  
  391.         switch (machine) {
  392.         case Atari:
  393.                 break;
  394.         case AtariXL:
  395.         case AtariXE:
  396.                 memory[0xc314] = 0x8e;
  397.                 memory[0xc315] = 0xff;
  398.                 memory[0xc319] = 0x8e;
  399.                 memory[0xc31a] = 0xff;
  400.                 break;
  401.         default:
  402.                 printf("Fatal Error in atari.c: PatchOS(): Unknown machine\n");
  403.                 Atari800_Exit(FALSE);
  404.                 exit(1);
  405.                 break;
  406.         }
  407. /*
  408.    ==========================================
  409.    Patch O.S. - Modify Handler Table (HATABS)
  410.    ==========================================
  411.  */
  412.         switch (machine) {
  413.         case Atari:
  414.                 addr = 0xf0e3;
  415.                 break;
  416.         case AtariXL:
  417.         case AtariXE:
  418.                 addr = 0xc42e;
  419.                 break;
  420.         default:
  421.                 printf("Fatal Error in atari.c: PatchOS(): Unknown machine\n");
  422.                 Atari800_Exit(FALSE);
  423.                 exit(1);
  424.                 break;
  425.         }
  426.  
  427.         for (i = 0; i < 5; i++) {
  428.                 devtab = (memory[addr + 2] << 8) | memory[addr + 1];
  429.  
  430.                 switch (memory[addr]) {
  431.                 case 'P':
  432.                         entry = (memory[devtab + o_open + 1] << 8) | memory[devtab + o_open];
  433.                         add_esc(entry + 1, ESC_PHOPEN);
  434.                         entry = (memory[devtab + o_close + 1] << 8) | memory[devtab + o_close];
  435.                         add_esc(entry + 1, ESC_PHCLOS);
  436. /*
  437.    entry = (memory[devtab+o_read+1] << 8) | memory[devtab+o_read];
  438.    add_esc (entry+1, ESC_PHREAD);
  439.  */
  440.                         entry = (memory[devtab + o_write + 1] << 8) | memory[devtab + o_write];
  441.                         add_esc(entry + 1, ESC_PHWRIT);
  442.                         entry = (memory[devtab + o_status + 1] << 8) | memory[devtab + o_status];
  443.                         add_esc(entry + 1, ESC_PHSTAT);
  444. /*
  445.    entry = (memory[devtab+o_special+1] << 8) | memory[devtab+o_special];
  446.    add_esc (entry+1, ESC_PHSPEC);
  447.  */
  448.                         memory[devtab + o_init] = 0xd2;
  449.                         memory[devtab + o_init + 1] = ESC_PHINIT;
  450.                         break;
  451.                 case 'C':
  452.                         memory[addr] = 'H';
  453.                         entry = (memory[devtab + o_open + 1] << 8) | memory[devtab + o_open];
  454.                         add_esc(entry + 1, ESC_HHOPEN);
  455.                         entry = (memory[devtab + o_close + 1] << 8) | memory[devtab + o_close];
  456.                         add_esc(entry + 1, ESC_HHCLOS);
  457.                         entry = (memory[devtab + o_read + 1] << 8) | memory[devtab + o_read];
  458.                         add_esc(entry + 1, ESC_HHREAD);
  459.                         entry = (memory[devtab + o_write + 1] << 8) | memory[devtab + o_write];
  460.                         add_esc(entry + 1, ESC_HHWRIT);
  461.                         entry = (memory[devtab + o_status + 1] << 8) | memory[devtab + o_status];
  462.                         add_esc(entry + 1, ESC_HHSTAT);
  463.                         break;
  464.                 case 'E':
  465. #ifdef BASIC
  466.                         printf("Editor Device\n");
  467.                         entry = (memory[devtab + o_open + 1] << 8) | memory[devtab + o_open];
  468.                         add_esc(entry + 1, ESC_E_OPEN);
  469.                         entry = (memory[devtab + o_read + 1] << 8) | memory[devtab + o_read];
  470.                         add_esc(entry + 1, ESC_E_READ);
  471.                         entry = (memory[devtab + o_write + 1] << 8) | memory[devtab + o_write];
  472.                         add_esc(entry + 1, ESC_E_WRITE);
  473. #endif
  474.                         break;
  475.                 case 'S':
  476.                         break;
  477.                 case 'K':
  478. #ifdef BASIC
  479.                         printf("Keyboard Device\n");
  480.                         entry = (memory[devtab + o_read + 1] << 8) | memory[devtab + o_read];
  481.                         add_esc(entry + 1, ESC_K_READ);
  482. #endif
  483.                         break;
  484.                 default:
  485.                         break;
  486.                 }
  487.  
  488.                 addr += 3;                              /* Next Device in HATABS */
  489.         }
  490. }
  491.  
  492. static int Reset_Disabled = FALSE;
  493.  
  494. void Coldstart(void)
  495. {
  496.         if (os == 3) {
  497.                 os = 2;                                 /* Fiddle OS to prevent infinite recursion */
  498.                 Initialise_Monty();
  499.                 os = 3;
  500.         }
  501.         NMIEN = 0x00;
  502.         NMIST = 0x00;
  503.         PORTA = 0xff;
  504.         if (machine == AtariXL || machine == AtariXE)
  505.                 PIA_PutByte(_PORTB, 0xff);      /* turn on operating system */
  506.         else
  507.                 PORTB = 0xff;
  508.         memory[0x244] = 1;
  509.         CPU_Reset();
  510.  
  511.         if (hold_option)
  512.                 next_console_value = 0x03;      /* Hold Option During Reboot */
  513. }
  514.  
  515. void Warmstart(void)
  516. {
  517.         if (os == 3)
  518.                 Initialise_Monty();
  519.  
  520.         NMIEN = 0x00;
  521.         NMIST = 0x00;
  522.         PORTA = 0xff;
  523.         PORTB = 0xff;
  524.         CPU_Reset();
  525. }
  526.  
  527. int Initialise_AtariOSA(void)
  528. {
  529.         int status;
  530.  
  531.         status = load_image(atari_osa_filename, 0xd800, 0x2800);
  532.         if (status) {
  533.                 machine = Atari;
  534.                 PatchOS();
  535.                 SetRAM(0x0000, 0xbfff);
  536.                 if (enable_c000_ram)
  537.                         SetRAM(0xc000, 0xcfff);
  538.                 else
  539.                         SetROM(0xc000, 0xcfff);
  540.                 SetROM(0xd800, 0xffff);
  541.                 SetHARDWARE(0xd000, 0xd7ff);
  542.                 Coldstart();
  543.         }
  544.         return status;
  545. }
  546.  
  547. int Initialise_AtariOSB(void)
  548. {
  549.         int status;
  550.  
  551.         status = load_image(atari_osb_filename, 0xd800, 0x2800);
  552.         if (status) {
  553.                 machine = Atari;
  554.                 PatchOS();
  555.                 SetRAM(0x0000, 0xbfff);
  556.                 if (enable_c000_ram)
  557.                         SetRAM(0xc000, 0xcfff);
  558.                 else
  559.                         SetROM(0xc000, 0xcfff);
  560.                 SetROM(0xd800, 0xffff);
  561.                 SetHARDWARE(0xd000, 0xd7ff);
  562.                 Coldstart();
  563.         }
  564.         return status;
  565. }
  566.  
  567. int Initialise_AtariXL(void)
  568. {
  569.         int status;
  570.  
  571.         status = load_image(atari_xlxe_filename, 0xc000, 0x4000);
  572.         if (status) {
  573.                 machine = AtariXL;
  574.                 PatchOS();
  575.                 memcpy(atarixl_os, memory + 0xc000, 0x4000);
  576.  
  577.                 if (cart_type == NO_CART) {
  578.                         status = Insert_8K_ROM(atari_basic_filename);
  579.                         if (status) {
  580.                                 memcpy(atari_basic, memory + 0xa000, 0x2000);
  581.                                 SetRAM(0x0000, 0x9fff);
  582.                                 SetROM(0xc000, 0xffff);
  583.                                 SetHARDWARE(0xd000, 0xd7ff);
  584.                                 rom_inserted = FALSE;
  585.                                 Coldstart();
  586.                         }
  587.                         else {
  588. #ifdef BACKUP_MSG
  589.                                 sprintf(backup_msg_buf, "Unable to load %s\n", atari_basic_filename);
  590. #else
  591.                                 printf("Unable to load %s\n", atari_basic_filename);
  592. #endif
  593.                                 Atari800_Exit(FALSE);
  594.                                 exit(1);
  595.                         }
  596.                 }
  597.                 else {
  598.                         SetRAM(0x0000, 0xbfff);
  599.                         SetROM(0xc000, 0xffff);
  600.                         SetHARDWARE(0xd000, 0xd7ff);
  601.                         rom_inserted = FALSE;
  602.                         Coldstart();
  603.                 }
  604.         }
  605.         return status;
  606. }
  607.  
  608. int Initialise_AtariXE(void)
  609. {
  610.         int status;
  611.  
  612.         status = Initialise_AtariXL();
  613.         machine = AtariXE;
  614.         xe_bank = -1;
  615.  
  616.         return status;
  617. }
  618.  
  619. int Initialise_Atari5200(void)
  620. {
  621.         int status;
  622.  
  623.         int i;
  624.  
  625.         for (i = 0; i < 0xf800; i++)
  626.                 memory[i] = 0x00;
  627.  
  628.         status = load_image(atari_5200_filename, 0xf800, 0x800);
  629.         if (status) {
  630.                 machine = Atari5200;
  631.                 SetRAM(0x0000, 0x3fff);
  632.                 SetROM(0xf800, 0xffff);
  633.                 SetROM(0x4000, 0xffff);
  634.                 SetHARDWARE(0xc000, 0xc0ff);    /* 5200 GTIA Chip */
  635.                 SetHARDWARE(0xd400, 0xd4ff);    /* 5200 ANTIC Chip */
  636.                 SetHARDWARE(0xe800, 0xe8ff);    /* 5200 POKEY Chip */
  637.                 SetHARDWARE(0xeb00, 0xebff);    /* 5200 POKEY Chip */
  638.                 Coldstart();
  639.         }
  640.         return status;
  641. }
  642.  
  643. /*
  644.  * Initialise System with Montezuma's Revenge
  645.  * image. Standard Atari OS is not required.
  646.  */
  647.  
  648. #include "emuos.h"
  649.  
  650. #ifdef __BUILT_IN_MONTY__
  651. #include "monty.h"
  652. #endif
  653.  
  654. int Initialise_Monty(void)
  655. {
  656.         int status;
  657.  
  658.         status = load_image("monty-emuos.img", 0x0000, 0x10000);
  659. #ifdef __BUILT_IN_MONTY__
  660.         if (!status) {
  661.                 memcpy(&memory[0x0000], monty_h, 0xc000);
  662.                 memcpy(&memory[0xc000], emuos_h, 0x4000);
  663.                 status = TRUE;
  664.         }
  665. #endif
  666.  
  667.         if (status) {
  668.                 machine = Atari;
  669. //      PatchOS ();
  670.                 SetRAM(0x0000, 0xbfff);
  671.                 if (enable_c000_ram)
  672.                         SetRAM(0xc000, 0xcfff);
  673.                 else
  674.                         SetROM(0xc000, 0xcfff);
  675.                 SetROM(0xd800, 0xffff);
  676.                 SetHARDWARE(0xd000, 0xd7ff);
  677.                 Coldstart();
  678.         }
  679.         return status;
  680. }
  681.  
  682. /*
  683.  * Initialise System with an replacement OS. It has just
  684.  * enough functionality to run Defender and Star Raider.
  685.  */
  686.  
  687. int Initialise_EmuOS(void)
  688. {
  689.         int status;
  690.  
  691.         status = load_image("emuos.img", 0xc000, 0x4000);
  692.         if (!status)
  693.                 memcpy(&memory[0xc000], emuos_h, 0x4000);
  694.         else
  695.                 printf("EmuOS: Using external emulated OS\n");
  696.  
  697.         machine = Atari;
  698.         PatchOS();
  699.         SetRAM(0x0000, 0xbfff);
  700.         if (enable_c000_ram)
  701.                 SetRAM(0xc000, 0xcfff);
  702.         else
  703.                 SetROM(0xc000, 0xcfff);
  704.         SetROM(0xd800, 0xffff);
  705.         SetHARDWARE(0xd000, 0xd7ff);
  706.         Coldstart();
  707.  
  708.         status = TRUE;
  709.  
  710.         return status;
  711. }
  712.  
  713. int main(int argc, char **argv)
  714. {
  715.         FILE *fp;
  716.  
  717.         int status;
  718.         int error;
  719.         int diskno = 1;
  720.         int i;
  721.         int j;
  722.  
  723.         char *rtconfig_filename = NULL;
  724.         int config = FALSE;
  725.  
  726.         error = FALSE;
  727.  
  728.         for (i = j = 1; i < argc; i++) {
  729.                 if (strcmp(argv[i], "-configure") == 0)
  730.                         config = TRUE;
  731.                 else if (strcmp(argv[i], "-config") == 0)
  732.                         rtconfig_filename = argv[++i];
  733.                 else if (strcmp(argv[i], "-v") == 0) {
  734.                         printf("%s\n", ATARI_TITLE);
  735.                         exit(1);
  736.                 }
  737.                 else if (strcmp(argv[i], "-verbose") == 0)
  738.                         verbose = TRUE;
  739.                 else
  740.                         argv[j++] = argv[i];
  741.         }
  742.  
  743.         argc = j;
  744.  
  745.         if (!RtConfigLoad(rtconfig_filename))
  746.                 config = TRUE;
  747.  
  748.         if (config) {
  749.                 RtConfigUpdate();
  750.                 RtConfigSave();
  751.         }
  752.         switch (default_system) {
  753.         case 1:
  754.                 machine = Atari;
  755.                 os = 1;
  756.                 break;
  757.         case 2:
  758.                 machine = Atari;
  759.                 os = 2;
  760.                 break;
  761.         case 3:
  762.                 machine = AtariXL;
  763.                 break;
  764.         case 4:
  765.                 machine = AtariXE;
  766.                 break;
  767.         case 5:
  768.                 machine = Atari5200;
  769.                 break;
  770.         default:
  771.                 machine = AtariXL;
  772.                 break;
  773.         }
  774.  
  775.         switch (default_tv_mode) {
  776.         case 1:
  777.                 tv_mode = PAL;
  778.                 break;
  779.         case 2:
  780.                 tv_mode = NTSC;
  781.                 break;
  782.         default:
  783.                 tv_mode = PAL;
  784.                 break;
  785.         }
  786.  
  787.         for (i = j = 1; i < argc; i++) {
  788.                 if (strcmp(argv[i], "-atari") == 0)
  789.                         machine = Atari;
  790.                 else if (strcmp(argv[i], "-xl") == 0)
  791.                         machine = AtariXL;
  792.                 else if (strcmp(argv[i], "-xe") == 0)
  793.                         machine = AtariXE;
  794.                 else if (strcmp(argv[i], "-5200") == 0)
  795.                         machine = Atari5200;
  796.                 else if (strcmp(argv[i], "-nobasic") == 0)
  797.                         hold_option = TRUE;
  798.                 else if (strcmp(argv[i], "-nopatch") == 0)
  799.                         enable_sio_patch = FALSE;
  800.                 else if (strcmp(argv[i], "-pal") == 0)
  801.                         tv_mode = PAL;
  802.                 else if (strcmp(argv[i], "-ntsc") == 0)
  803.                         tv_mode = NTSC;
  804.                 else if (strcmp(argv[i], "-osa_rom") == 0)
  805.                         strcpy(atari_osa_filename, argv[++i]);
  806.                 else if (strcmp(argv[i], "-osb_rom") == 0)
  807.                         strcpy(atari_osb_filename, argv[++i]);
  808.                 else if (strcmp(argv[i], "-xlxe_rom") == 0)
  809.                         strcpy(atari_xlxe_filename, argv[++i]);
  810.                 else if (strcmp(argv[i], "-5200_rom") == 0)
  811.                         strcpy(atari_5200_filename, argv[++i]);
  812.                 else if (strcmp(argv[i], "-basic_rom") == 0)
  813.                         strcpy(atari_basic_filename, argv[++i]);
  814.                 else if (strcmp(argv[i], "-cart") == 0) {
  815.                         rom_filename = argv[++i];
  816.                         cart_type = CARTRIDGE;
  817.                 }
  818.                 else if (strcmp(argv[i], "-rom") == 0) {
  819.                         rom_filename = argv[++i];
  820.                         cart_type = NORMAL8_CART;
  821.                 }
  822.                 else if (strcmp(argv[i], "-rom16") == 0) {
  823.                         rom_filename = argv[++i];
  824.                         cart_type = NORMAL16_CART;
  825.                 }
  826.                 else if (strcmp(argv[i], "-ags32") == 0) {
  827.                         rom_filename = argv[++i];
  828.                         cart_type = AGS32_CART;
  829.                 }
  830.                 else if (strcmp(argv[i], "-oss") == 0) {
  831.                         rom_filename = argv[++i];
  832.                         cart_type = OSS_SUPERCART;
  833.                 }
  834.                 else if (strcmp(argv[i], "-db") == 0) {         /* db 16/32 superduper cart */
  835.                         rom_filename = argv[++i];
  836.                         cart_type = DB_SUPERCART;
  837.                 }
  838.                 else if (strcmp(argv[i], "-refresh") == 0) {
  839.                         sscanf(argv[++i], "%d", &refresh_rate);
  840.                         if (refresh_rate < 1)
  841.                                 refresh_rate = 1;
  842.                 }
  843.                 else if (strcmp(argv[i], "-help") == 0) {
  844.                         printf("\t-configure    Update Configuration File\n");
  845.                         printf("\t-config fnm   Specify Alternate Configuration File\n");
  846.                         printf("\t-atari        Standard Atari 800 mode\n");
  847.                         printf("\t-xl           Atari XL mode\n");
  848.                         printf("\t-xe           Atari XE mode (Currently same as -xl)\n");
  849.                         printf("\t-5200         Atari 5200 Games System\n");
  850.                         printf("\t-pal          Enable PAL TV mode\n");
  851.                         printf("\t-ntsc         Enable NTSC TV mode\n");
  852.                         printf("\t-rom fnm      Install standard 8K Cartridge\n");
  853.                         printf("\t-rom16 fnm    Install standard 16K Cartridge\n");
  854.                         printf("\t-oss fnm      Install OSS Super Cartridge\n");
  855.                         printf("\t-db fnm       Install DB's 16/32K Cartridge (not for normal use)\n");
  856.                         printf("\t-refresh num  Specify screen refresh rate\n");
  857.                         printf("\t-nopatch      Don't patch SIO routine in OS\n");
  858.                         printf("\t-a            Use A OS\n");
  859.                         printf("\t-b            Use B OS\n");
  860.                         printf("\t-c            Enable RAM between 0xc000 and 0xd000\n");
  861.                         printf("\t-v            Show version/release number\n");
  862.                         argv[j++] = argv[i];
  863.                 }
  864.                 else if (strcmp(argv[i], "-a") == 0)
  865.                         os = 1;
  866.                 else if (strcmp(argv[i], "-b") == 0)
  867.                         os = 2;
  868.                 else if (strcmp(argv[i], "-monty") == 0) {
  869.                         machine = Atari;
  870.                         os = 3;
  871.                 }
  872.                 else if (strcmp(argv[i], "-emuos") == 0) {
  873.                         machine = Atari;
  874.                         os = 4;
  875.                 }
  876.                 else if (strcmp(argv[i], "-c") == 0)
  877.                         enable_c000_ram = TRUE;
  878.                 else
  879.                         argv[j++] = argv[i];
  880.         }
  881.  
  882.         argc = j;
  883.  
  884.         if (tv_mode == PAL)
  885.                 deltatime = (1.0 / 50.0);
  886.         else
  887.                 deltatime = (1.0 / 60.0);
  888.  
  889.         Device_Initialise(&argc, argv);
  890.         SIO_Initialise(&argc, argv);
  891.  
  892.         Atari_Initialise(&argc, argv);  /* Platform Specific Initialisation */
  893.  
  894.         if (!atari_screen) {
  895.                 atari_screen = (ULONG *) malloc((ATARI_HEIGHT + 16) * ATARI_WIDTH);
  896.                 for (i = 0; i < 256; i++)
  897.                         colour_translation_table[i] = i;
  898.         }
  899.         /*
  900.          * Initialise basic 64K memory to zero.
  901.          */
  902.  
  903.         for (i = 0; i < 65536; i++)
  904.                 memory[i] = 0;
  905.  
  906.         /*
  907.          * Initialise Custom Chips
  908.          */
  909.  
  910.         ANTIC_Initialise(&argc, argv);
  911.         GTIA_Initialise(&argc, argv);
  912.         PIA_Initialise(&argc, argv);
  913.  
  914.         /*
  915.          * Initialise Serial Port Interrupts
  916.          */
  917.  
  918.         DELAYED_SERIN_IRQ = 0;
  919.         DELAYED_SEROUT_IRQ = 0;
  920.         DELAYED_XMTDONE_IRQ = 0;
  921.  
  922.         /*
  923.          * Any parameters left on the command line must be disk images.
  924.          */
  925.  
  926.         for (i = 1; i < argc; i++) {
  927.                 if (!SIO_Mount(diskno++, argv[i])) {
  928.                         printf("Disk File %s not found\n", argv[i]);
  929.                         error = TRUE;
  930.                 }
  931.         }
  932.  
  933.         if (error) {
  934. #ifdef BACKUP_MSG
  935.                 sprintf(backup_msg_buf, "Usage: %s [-rom filename] [-oss filename] [diskfile1...diskfile8]\n", argv[0]);
  936.                 sprintf(backup_msg_buf + strlen(backup_msg_buf), "\t-help         Extended Help\n");
  937. #else
  938.                 printf("Usage: %s [-rom filename] [-oss filename] [diskfile1...diskfile8]\n", argv[0]);
  939.                 printf("\t-help         Extended Help\n");
  940. #endif
  941.                 Atari800_Exit(FALSE);
  942.                 exit(1);
  943.         }
  944.         /*
  945.          * Install CTRL-C Handler
  946.          */
  947.  
  948.         signal(SIGINT, sigint_handler);
  949.  
  950.         /*
  951.          * Configure Atari System
  952.          */
  953.  
  954.         switch (machine) {
  955.         case Atari:
  956.                 if (os == 1)
  957.                         status = Initialise_AtariOSA();
  958.                 else if (os == 2)
  959.                         status = Initialise_AtariOSB();
  960.                 else if (os == 3)
  961.                         status = Initialise_Monty();
  962.                 else
  963.                         status = Initialise_EmuOS();
  964.                 break;
  965.         case AtariXL:
  966.                 status = Initialise_AtariXL();
  967.                 break;
  968.         case AtariXE:
  969.                 status = Initialise_AtariXE();
  970.                 break;
  971.         case Atari5200:
  972.                 status = Initialise_Atari5200();
  973.                 break;
  974.         default:
  975.                 printf("Fatal Error in atari.c\n");
  976.                 Atari800_Exit(FALSE);
  977.                 exit(1);
  978.         }
  979.  
  980.         if (!status) {
  981.                 printf("Operating System not available - using Emulated OS\n");
  982.                 status = Initialise_EmuOS();
  983.         }
  984. /*
  985.  * ================================
  986.  * Install requested ROM cartridges
  987.  * ================================
  988.  */
  989.         if (rom_filename) {
  990.                 switch (cart_type) {
  991.                 case CARTRIDGE:
  992.                         status = Insert_Cartridge(rom_filename);
  993.                         break;
  994.                 case OSS_SUPERCART:
  995.                         status = Insert_OSS_ROM(rom_filename);
  996.                         break;
  997.                 case DB_SUPERCART:
  998.                         status = Insert_DB_ROM(rom_filename);
  999.                         break;
  1000.                 case NORMAL8_CART:
  1001.                         status = Insert_8K_ROM(rom_filename);
  1002.                         break;
  1003.                 case NORMAL16_CART:
  1004.                         status = Insert_16K_ROM(rom_filename);
  1005.                         break;
  1006.                 case AGS32_CART:
  1007.                         status = Insert_32K_5200ROM(rom_filename);
  1008.                         break;
  1009.                 }
  1010.  
  1011.                 if (status) {
  1012.                         rom_inserted = TRUE;
  1013.                 }
  1014.                 else {
  1015.                         rom_inserted = FALSE;
  1016.                 }
  1017.         }
  1018.         else {
  1019.                 rom_inserted = FALSE;
  1020.         }
  1021. /*
  1022.  * ======================================
  1023.  * Reset CPU and start hardware emulation
  1024.  * ======================================
  1025.  */
  1026.         Atari800_Hardware();
  1027.         printf("Fatal error: Atari800_Hardware() returned\n");
  1028.         Atari800_Exit(FALSE);
  1029.         exit(1);
  1030.         return 1;
  1031. }
  1032.  
  1033. void add_esc(UWORD address, UBYTE esc_code)
  1034. {
  1035.         memory[address++] = 0xf2;       /* ESC */
  1036.         memory[address++] = esc_code;   /* ESC CODE */
  1037.         memory[address] = 0x60;         /* RTS */
  1038. }
  1039.  
  1040. /*
  1041.    ================================
  1042.    N = 0 : I/O Successful and Y = 1
  1043.    N = 1 : I/O Error and Y = error#
  1044.    ================================
  1045.  */
  1046.  
  1047. void K_Device(UBYTE esc_code)
  1048. {
  1049.         char ch;
  1050.  
  1051.         switch (esc_code) {
  1052.         case ESC_K_OPEN:
  1053.         case ESC_K_CLOSE:
  1054.                 regY = 1;
  1055.                 ClrN;
  1056.                 break;
  1057.         case ESC_K_WRITE:
  1058.         case ESC_K_STATUS:
  1059.         case ESC_K_SPECIAL:
  1060.                 regY = 146;
  1061.                 SetN;
  1062.                 break;
  1063.         case ESC_K_READ:
  1064.                 ch = getchar();
  1065.                 switch (ch) {
  1066.                 case '\n':
  1067.                         ch = 0x9b;
  1068.                         break;
  1069.                 default:
  1070.                         break;
  1071.                 }
  1072.                 regA = ch;
  1073.                 regY = 1;
  1074.                 ClrN;
  1075.                 break;
  1076.         }
  1077. }
  1078.  
  1079. void E_Device(UBYTE esc_code)
  1080. {
  1081.         UBYTE ch;
  1082.  
  1083.         switch (esc_code) {
  1084.         case ESC_E_OPEN:
  1085.                 printf("Editor Open\n");
  1086.                 regY = 1;
  1087.                 ClrN;
  1088.                 break;
  1089.         case ESC_E_READ:
  1090.                 ch = getchar();
  1091.                 switch (ch) {
  1092.                 case '\n':
  1093.                         ch = 0x9b;
  1094.                         break;
  1095.                 default:
  1096.                         break;
  1097.                 }
  1098.                 regA = ch;
  1099.                 regY = 1;
  1100.                 ClrN;
  1101.                 break;
  1102.         case ESC_E_WRITE:
  1103.                 ch = regA;
  1104.                 switch (ch) {
  1105.                 case 0x7d:
  1106.                         putchar('*');
  1107.                         break;
  1108.                 case 0x9b:
  1109.                         putchar('\n');
  1110.                         break;
  1111.                 default:
  1112.                         if ((ch >= 0x20) && (ch <= 0x7e))       /* for DJGPP */
  1113.                                 putchar(ch & 0x7f);
  1114.                         break;
  1115.                 }
  1116.                 regY = 1;
  1117.                 ClrN;
  1118.                 break;
  1119.         }
  1120. }
  1121.  
  1122. #define CDTMV1 0x0218
  1123.  
  1124. void Escape(UBYTE esc_code)
  1125. {
  1126.         int addr;
  1127.  
  1128.         switch (esc_code) {
  1129. #ifdef MONITOR_BREAK
  1130.         case ESC_BREAK:
  1131.                 Atari800_Exit(TRUE);
  1132.                 break;
  1133. #endif
  1134.         case ESC_BREAK:
  1135.         case ESC_SIOV:
  1136.                 SIO();
  1137.                 break;
  1138.         case ESC_K_OPEN:
  1139.         case ESC_K_CLOSE:
  1140.         case ESC_K_READ:
  1141.         case ESC_K_WRITE:
  1142.         case ESC_K_STATUS:
  1143.         case ESC_K_SPECIAL:
  1144.                 K_Device(esc_code);
  1145.                 break;
  1146.         case ESC_E_OPEN:
  1147.         case ESC_E_READ:
  1148.         case ESC_E_WRITE:
  1149.                 E_Device(esc_code);
  1150.                 break;
  1151.         case ESC_PHOPEN:
  1152.                 Device_PHOPEN();
  1153.                 break;
  1154.         case ESC_PHCLOS:
  1155.                 Device_PHCLOS();
  1156.                 break;
  1157.         case ESC_PHREAD:
  1158.                 Device_PHREAD();
  1159.                 break;
  1160.         case ESC_PHWRIT:
  1161.                 Device_PHWRIT();
  1162.                 break;
  1163.         case ESC_PHSTAT:
  1164.                 Device_PHSTAT();
  1165.                 break;
  1166.         case ESC_PHSPEC:
  1167.                 Device_PHSPEC();
  1168.                 break;
  1169.         case ESC_PHINIT:
  1170.                 Device_PHINIT();
  1171.                 break;
  1172.         case ESC_HHOPEN:
  1173.                 Device_HHOPEN();
  1174.                 break;
  1175.         case ESC_HHCLOS:
  1176.                 Device_HHCLOS();
  1177.                 break;
  1178.         case ESC_HHREAD:
  1179.                 Device_HHREAD();
  1180.                 break;
  1181.         case ESC_HHWRIT:
  1182.                 Device_HHWRIT();
  1183.                 break;
  1184.         case ESC_HHSTAT:
  1185.                 Device_HHSTAT();
  1186.                 break;
  1187.         case ESC_HHSPEC:
  1188.                 Device_HHSPEC();
  1189.                 break;
  1190.         case ESC_HHINIT:
  1191.                 Device_HHINIT();
  1192.                 break;
  1193.         default:
  1194.                 Atari800_Exit(FALSE);
  1195.                 printf("Invalid ESC Code %x at Address %x\n",
  1196.                            esc_code, regPC - 2);
  1197.                 monitor();
  1198.                 exit(1);
  1199.         }
  1200. }
  1201.  
  1202. int Atari800_Exit(int run_monitor)
  1203. {
  1204.         if (verbose) {
  1205.                 printf("Current Frames per Secound = %f\n", (double)((double)nframes/(double)firsttime));
  1206.         }
  1207.         return Atari_Exit(run_monitor);
  1208. }
  1209.  
  1210. UBYTE Atari800_GetByte(UWORD addr)
  1211. {
  1212.         UBYTE byte;
  1213. /*
  1214.    ============================================================
  1215.    GTIA, POKEY, PIA and ANTIC do not fully decode their address
  1216.    ------------------------------------------------------------
  1217.    PIA (At least) is fully decoded when emulating the XL/XE
  1218.    ============================================================
  1219.  */
  1220.         switch (addr & 0xff00) {
  1221.         case 0xd000:                            /* GTIA */
  1222.                 byte = GTIA_GetByte(addr - 0xd000);
  1223.                 break;
  1224.         case 0xd200:                            /* POKEY */
  1225.                 byte = POKEY_GetByte(addr - 0xd200);
  1226.                 break;
  1227.         case 0xd300:                            /* PIA */
  1228.                 byte = PIA_GetByte(addr - 0xd300);
  1229.                 break;
  1230.         case 0xd400:                            /* ANTIC */
  1231.                 byte = ANTIC_GetByte(addr - 0xd400);
  1232.                 break;
  1233.         case 0xc000:                            /* GTIA - 5200 */
  1234.                 byte = GTIA_GetByte(addr - 0xc000);
  1235.                 break;
  1236.         case 0xe800:                            /* POKEY - 5200 */
  1237.                 byte = POKEY_GetByte(addr - 0xe800);
  1238.                 break;
  1239.         case 0xeb00:                            /* POKEY - 5200 */
  1240.                 byte = POKEY_GetByte(addr - 0xeb00);
  1241.                 break;
  1242.         default:
  1243.                 break;
  1244.         }
  1245.  
  1246.         return byte;
  1247. }
  1248.  
  1249. int Atari800_PutByte(UWORD addr, UBYTE byte)
  1250. {
  1251.         int abort = FALSE;
  1252. /*
  1253.    ============================================================
  1254.    GTIA, POKEY, PIA and ANTIC do not fully decode their address
  1255.    ------------------------------------------------------------
  1256.    PIA (At least) is fully decoded when emulating the XL/XE
  1257.    ============================================================
  1258.  */
  1259.         switch (addr & 0xff00) {
  1260.         case 0xd000:                            /* GTIA */
  1261.                 abort = GTIA_PutByte(addr - 0xd000, byte);
  1262.                 break;
  1263.         case 0xd200:                            /* POKEY */
  1264.                 abort = POKEY_PutByte(addr - 0xd200, byte);
  1265.                 break;
  1266.         case 0xd300:                            /* PIA */
  1267.                 abort = PIA_PutByte(addr - 0xd300, byte);
  1268.                 break;
  1269.         case 0xd400:                            /* ANTIC */
  1270.                 abort = ANTIC_PutByte(addr - 0xd400, byte);
  1271.                 break;
  1272.         case 0xd500:                            /* Super Cartridges */
  1273.                 abort = SuperCart_PutByte(addr, byte);
  1274.                 break;
  1275.         case 0xc000:                            /* GTIA - 5200 */
  1276.                 abort = GTIA_PutByte(addr - 0xc000, byte);
  1277.                 break;
  1278.         case 0xeb00:                            /* POKEY - 5200 */
  1279.                 abort = POKEY_PutByte(addr - 0xeb00, byte);
  1280.                 break;
  1281.         default:
  1282.                 break;
  1283.         }
  1284.  
  1285.         return abort;
  1286. }
  1287.  
  1288. #ifdef AMIGA_PPC
  1289. #define CLK_TCK  50
  1290. #endif
  1291.  
  1292. void ShowRealSpeed(ULONG * atari_screen, int refresh_rate)
  1293. {
  1294.         UWORD *ptr, *ptr2;
  1295.         double j;
  1296.         int i = (clock() - lastspeed) * (tv_mode == PAL ? 50 : 60) / CLK_TCK / refresh_rate;
  1297.         j=(double)((double)nframes/(double)firsttime);
  1298.         i=((int)j)*2;
  1299.         lastspeed = clock();
  1300.         if (i > ATARI_WIDTH / 4)
  1301.                 return;
  1302.  
  1303.         ptr = (UWORD *) atari_screen;
  1304.         ptr += (ATARI_WIDTH / 2) * (ATARI_HEIGHT - 2) + ATARI_WIDTH / 4;        /* begin in middle of line */
  1305.         ptr2 = ptr + ATARI_WIDTH / 2;
  1306.  
  1307.         while (--i > 0) {                       /* number of blocks = times slower */
  1308.                 int j = i << 1;
  1309.                 ptr[j] = ptr2[j] = 0x0707;
  1310.                 j++;
  1311.                 ptr[j] = ptr2[j] = 0;
  1312.         }
  1313. }
  1314.  
  1315. void Atari800_Hardware(void)
  1316. {
  1317.         static struct timeval tp;
  1318.         static struct timezone tzp;
  1319.         static double lasttime;
  1320. #ifdef USE_CLOCK
  1321.         static ULONG nextclock = 0;     /* put here a non-zero value to enable speed regulator */
  1322. #endif
  1323.         static Key_Held = FALSE;
  1324.  
  1325.         nframes = 0;
  1326. #ifndef AMIGA_PPC
  1327.         gettimeofday(&tp, &tzp);
  1328.         lasttime = tp.tv_sec + (tp.tv_usec / 1000000.0);
  1329. #else
  1330.         //lasttime=clock()/CLOCKS_PER_SEC;
  1331.         GetSysTimePPC(&tp);
  1332.         lasttime=tp.mtv_sec.s_sec+(tp.mtv_usec.s_usec/1000000);
  1333.         firsttime=0;
  1334. #endif
  1335.         fps = 0.0;
  1336.  
  1337.         while (TRUE) {
  1338.                 static int test_val = 0;
  1339.                 int keycode;
  1340.  
  1341. #ifndef BASIC
  1342. /*
  1343.    colour_lookup[8] = colour_translation_table[COLBK];
  1344.  */
  1345.  
  1346.                 keycode = Atari_Keyboard();
  1347.  
  1348.                 switch (keycode) {
  1349.                 case AKEY_COLDSTART:
  1350.                         Coldstart();
  1351.                         break;
  1352.                 case AKEY_WARMSTART:
  1353.                         Warmstart();
  1354.                         break;
  1355.                 case AKEY_EXIT:
  1356.                         Atari800_Exit(FALSE);
  1357.                         exit(1);
  1358.                 case AKEY_BREAK:
  1359.                         if (IRQEN & 0x80) {
  1360.                                 IRQST &= ~0x80;
  1361.                                 IRQ = 1;
  1362.                         }
  1363.                         break;
  1364.                 case AKEY_UI:
  1365.                         ui(atari_screen);
  1366.                         break;
  1367.                 case AKEY_PIL:
  1368.                         if (pil_on)
  1369.                                 DisablePILL();
  1370.                         else
  1371.                                 EnablePILL();
  1372.                         break;
  1373.                 case AKEY_NONE:
  1374.                         Key_Held = FALSE;
  1375.                         break;
  1376.                 default:
  1377.                         if (Key_Held)
  1378.                                 break;
  1379.                         KBCODE = keycode;
  1380.                         Key_Held = TRUE;
  1381.                         if (IRQEN & 0x40) {
  1382.                                 IRQST &= ~0x40;
  1383.                                 IRQ = 1;
  1384.                         }
  1385.                         break;
  1386.                 }
  1387. #endif
  1388.  
  1389.                 /*
  1390.                  * Generate Screen
  1391.                  */
  1392.  
  1393. #ifndef BASIC
  1394.                 if (++test_val == refresh_rate) {
  1395.                         ANTIC_RunDisplayList();
  1396.                         ShowRealSpeed(atari_screen, refresh_rate);
  1397.                         Atari_DisplayScreen((UBYTE *) atari_screen);
  1398.                         test_val = 0;
  1399.                 }
  1400.                 else {
  1401.                         for (ypos = 0; ypos < ATARI_HEIGHT; ypos++) {
  1402.                                 GO(114);
  1403.                         }
  1404.                 }
  1405. #else
  1406.                 for (ypos = 0; ypos < ATARI_HEIGHT; ypos++) {
  1407.                         GO(114);
  1408.                 }
  1409. #endif
  1410.  
  1411.                 nframes++;
  1412.  
  1413. /* if too fast host computer, slow the emulation down to 100% of original speed */
  1414.  
  1415. #ifdef USE_CLOCK
  1416.                 /* on Atari Falcon CLK_TCK = 200 (i.e. 5 ms granularity) */
  1417.                 /* on DOS (DJGPP) CLK_TCK = 91 */
  1418.                 if (nextclock) {
  1419.                         ULONG curclock;
  1420.                         do {
  1421.                                 curclock = clock();
  1422.                         } while (curclock < nextclock);
  1423.  
  1424.                         nextclock = curclock + (CLK_TCK / (tv_mode == PAL ? 50 : 60));
  1425.                 }
  1426. #else
  1427. #ifndef DJGPP
  1428.                 if (deltatime > 0.0) {
  1429.                         double curtime;
  1430.  
  1431.                         do {
  1432. #ifndef AMIGA_PPC
  1433.                                 gettimeofday(&tp, &tzp);
  1434.                                 curtime = tp.tv_sec + (tp.tv_usec / 1000000.0);
  1435. #else
  1436.                                 GetSysTimePPC(&tp);
  1437.                                 curtime=tp.mtv_sec.s_sec+(tp.mtv_usec.s_usec/1000000);
  1438.                                 //curtime=clock()/CLOCKS_PER_SEC;
  1439. #endif
  1440.                         } while (0);//(curtime < (lasttime + deltatime));
  1441.  
  1442.                         firsttime=firsttime+(curtime-lasttime);
  1443.                         fps = 1.0 / (curtime - lasttime);
  1444.                         lasttime = curtime;
  1445.                 }
  1446. #else                                                   // for dos, count ticks and use the ticks_per_second global variable
  1447.                 // Use the high speed clock tick function uclock()
  1448.                 if (deltatime > 0.0) {
  1449.                         static uclock_t lasttime = 0;
  1450.                         uclock_t curtime, uclockd;
  1451.                         unsigned long uclockd_hi, uclockd_lo;
  1452.                         unsigned long uclocks_per_int;
  1453.  
  1454.                         uclocks_per_int = (deltatime * (double) UCLOCKS_PER_SEC) + 0.5;
  1455.                         // printf( "ticks_per_int %d, %.8X\n", uclocks_per_int, (unsigned long) lasttime );
  1456.  
  1457.                         do {
  1458.                                 curtime = uclock();
  1459.                                 if (curtime > lasttime) {
  1460.                                         uclockd = curtime - lasttime;
  1461.                                 }
  1462.                                 else {
  1463.                                         uclockd = ((uclock_t) ~ 0 - lasttime) + curtime + (uclock_t) 1;
  1464.                                 }
  1465.                                 uclockd_lo = (unsigned long) uclockd;
  1466.                                 uclockd_hi = (unsigned long) (uclockd >> 32);
  1467.                         } while ((uclockd_hi == 0) && (uclocks_per_int > uclockd_lo));
  1468.  
  1469.                         lasttime = curtime;
  1470.                 }
  1471. #endif                                                  /* DJGPP */
  1472. #endif                                                  /* FALCON */
  1473.         }
  1474. }
  1475.