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

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <stdlib.h>                             /* for free() */
  6.  
  7. #ifdef AMIGA_PPC
  8. #include <clib/powerpc_protos.h>
  9. #endif
  10.  
  11. #include "rt-config.h"
  12. #include "atari.h"
  13. #include "cpu.h"
  14. #include "gtia.h"
  15. #include "sio.h"
  16. #include "list.h"
  17.  
  18. #define FALSE 0
  19. #define TRUE 1
  20.  
  21. static char charset[8192];
  22. extern UBYTE atarixl_os[16384];
  23. extern unsigned char ascii_to_screen[128];
  24.  
  25. unsigned char key_to_ascii[256] =
  26. {
  27.         0x6C, 0x6A, 0x3B, 0x00, 0x00, 0x6B, 0x2B, 0x2A, 0x6F, 0x00, 0x70, 0x75, 0x9B, 0x69, 0x2D, 0x3D,
  28.         0x76, 0x00, 0x63, 0x00, 0x00, 0x62, 0x78, 0x7A, 0x34, 0x00, 0x33, 0x36, 0x1B, 0x35, 0x32, 0x31,
  29.         0x2C, 0x20, 0x2E, 0x6E, 0x00, 0x6D, 0x2F, 0x00, 0x72, 0x00, 0x65, 0x79, 0x7F, 0x74, 0x77, 0x71,
  30.         0x39, 0x00, 0x30, 0x37, 0x7E, 0x38, 0x3C, 0x3E, 0x66, 0x68, 0x64, 0x00, 0x00, 0x67, 0x73, 0x61,
  31.  
  32.         0x4C, 0x4A, 0x3A, 0x00, 0x00, 0x4B, 0x5C, 0x5E, 0x4F, 0x00, 0x50, 0x55, 0x9B, 0x49, 0x5F, 0x7C,
  33.         0x56, 0x00, 0x43, 0x00, 0x00, 0x42, 0x58, 0x5A, 0x24, 0x00, 0x23, 0x26, 0x1B, 0x25, 0x22, 0x21,
  34.         0x5B, 0x20, 0x5D, 0x4E, 0x00, 0x4D, 0x3F, 0x00, 0x52, 0x00, 0x45, 0x59, 0x9F, 0x54, 0x57, 0x51,
  35.         0x28, 0x00, 0x29, 0x27, 0x9C, 0x40, 0x7D, 0x9D, 0x46, 0x48, 0x44, 0x00, 0x00, 0x47, 0x53, 0x41,
  36.  
  37.         0x0C, 0x0A, 0x7B, 0x00, 0x00, 0x0B, 0x1E, 0x1F, 0x0F, 0x00, 0x10, 0x15, 0x9B, 0x09, 0x1C, 0x1D,
  38.         0x16, 0x00, 0x03, 0x00, 0x00, 0x02, 0x18, 0x1A, 0x00, 0x00, 0x9B, 0x00, 0x1B, 0x00, 0xFD, 0x00,
  39.         0x00, 0x20, 0x60, 0x0E, 0x00, 0x0D, 0x00, 0x00, 0x12, 0x00, 0x05, 0x19, 0x9E, 0x14, 0x17, 0x11,
  40.         0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x7D, 0xFF, 0x06, 0x08, 0x04, 0x00, 0x00, 0x07, 0x13, 0x01,
  41.  
  42.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  46. };
  47.  
  48. unsigned char ascii_to_screen[128] =
  49. {
  50.         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  51.         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  52.         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  53.         0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  54.         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  55.         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  56.         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  57.         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  58.         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  59.         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  60.         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  61.         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  62.         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  63.         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  64.         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  65.         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  66. };
  67.  
  68. int GetKeyPress(UBYTE * screen)
  69. {
  70.         int keycode;
  71. #ifndef AMIGA_PPC
  72.         usleep(100000UL);
  73. #endif
  74.         do {
  75. #ifndef BASIC
  76.                 Atari_DisplayScreen(screen);
  77. #endif
  78.                 keycode = Atari_Keyboard();
  79.         } while (keycode == AKEY_NONE);
  80.  
  81.         return key_to_ascii[keycode];
  82. }
  83.  
  84. void Plot(UBYTE * screen, int fg, int bg, int ch, int x, int y)
  85. {
  86.         int offset = ascii_to_screen[(ch & 0x07f)] * 8;
  87.         int i;
  88.         int j;
  89.  
  90.         char *ptr;
  91.  
  92.         ptr = &screen[24 * ATARI_WIDTH + 32];
  93.  
  94.         for (i = 0; i < 8; i++) {
  95.                 UBYTE data;
  96.  
  97.                 data = charset[offset++];
  98.  
  99.                 for (j = 0; j < 8; j++) {
  100.                         int pixel;
  101.  
  102.                         if (data & 0x80)
  103.                                 pixel = colour_translation_table[fg];
  104.                         else
  105.                                 pixel = colour_translation_table[bg];
  106.  
  107.                         ptr[(y * 8 + i) * ATARI_WIDTH + (x * 8 + j)] = pixel;
  108.  
  109.                         data = data << 1;
  110.                 }
  111.         }
  112. }
  113.  
  114. void Print(UBYTE * screen, int fg, int bg, char *string, int x, int y)
  115. {
  116.         while (*string) {
  117.                 Plot(screen, fg, bg, *string++, x, y);
  118.                 x++;
  119.         }
  120. }
  121.  
  122. void CenterPrint(UBYTE * screen, int fg, int bg, char *string, int y)
  123. {
  124.         Print(screen, fg, bg, string, (40 - strlen(string)) / 2, y);
  125. }
  126.  
  127. void EditString(UBYTE * screen, int fg, int bg,
  128.                                 int len, char *string,
  129.                                 int x, int y)
  130. {
  131.         int offset = 0;
  132.         int done = FALSE;
  133.  
  134.         Print(screen, fg, bg, string, x, y);
  135.  
  136.         while (!done) {
  137.                 int ascii;
  138.  
  139.                 Plot(screen, bg, fg, string[offset], x + offset, y);
  140.  
  141.                 ascii = GetKeyPress(screen);
  142.                 switch (ascii) {
  143.                 case 0x1e:                              /* Cursor Left */
  144.                         Plot(screen, fg, bg, string[offset], x + offset, y);
  145.                         if (offset > 0)
  146.                                 offset--;
  147.                         break;
  148.                 case 0x1f:                              /* Cursor Right */
  149.                         Plot(screen, fg, bg, string[offset], x + offset, y);
  150.                         if ((offset + 1) < len)
  151.                                 offset++;
  152.                         break;
  153.                 case 0x7e:                              /* Backspace */
  154.                         Plot(screen, fg, bg, string[offset], x + offset, y);
  155.                         if (offset > 0) {
  156.                                 offset--;
  157.                                 string[offset] = ' ';
  158.                         }
  159.                         break;
  160.                 case 0x9b:                              /* Return */
  161.                         done = TRUE;
  162.                         break;
  163.                 default:
  164.                         string[offset] = (char) ascii;
  165.                         Plot(screen, fg, bg, string[offset], x + offset, y);
  166.                         if ((offset + 1) < len)
  167.                                 offset++;
  168.                         break;
  169.                 }
  170.         }
  171. }
  172.  
  173. void Box(UBYTE * screen, int fg, int bg, int x1, int y1, int x2, int y2)
  174. {
  175.         int x;
  176.         int y;
  177.  
  178.         for (x = x1 + 1; x < x2; x++) {
  179.                 Plot(screen, fg, bg, 18, x, y1);
  180.                 Plot(screen, fg, bg, 18, x, y2);
  181.         }
  182.  
  183.         for (y = y1 + 1; y < y2; y++) {
  184.                 Plot(screen, fg, bg, 124, x1, y);
  185.                 Plot(screen, fg, bg, 124, x2, y);
  186.         }
  187.  
  188.         Plot(screen, fg, bg, 17, x1, y1);
  189.         Plot(screen, fg, bg, 5, x2, y1);
  190.         Plot(screen, fg, bg, 3, x2, y2);
  191.         Plot(screen, fg, bg, 26, x1, y2);
  192. }
  193.  
  194. void ClearScreen(UBYTE * screen)
  195. {
  196.         int x;
  197.         int y;
  198.  
  199.         for (y = 0; y < ATARI_HEIGHT; y++)
  200.                 for (x = 0; x < ATARI_WIDTH; x++)
  201.                         screen[y * ATARI_WIDTH + x] = colour_translation_table[0];
  202.  
  203.         for (x = 0; x < 40; x++)
  204.                 for (y = 0; y < 24; y++)
  205.                         Plot(screen, 0x9a, 0x94, ' ', x, y);
  206. }
  207.  
  208. void TitleScreen(UBYTE * screen, char *title)
  209. {
  210.         Box(screen, 0x9a, 0x94, 0, 0, 39, 2);
  211.         CenterPrint(screen, 0x9a, 0x94, title, 1);
  212. }
  213.  
  214. void SelectItem(UBYTE * screen,
  215.                                 int fg, int bg,
  216.                                 int index, char *items[],
  217.                                 int nrows, int ncolumns,
  218.                                 int xoffset, int yoffset)
  219. {
  220.         int x;
  221.         int y;
  222.  
  223.         x = index / nrows;
  224.         y = index - (x * nrows);
  225.  
  226.         x = x * (40 / ncolumns);
  227.  
  228.         x += xoffset;
  229.         y += yoffset;
  230.  
  231.         Print(screen, fg, bg, items[index], x, y);
  232. }
  233.  
  234. int Select(UBYTE * screen,
  235.                    int default_item,
  236.                    int nitems, char *items[],
  237.                    int nrows, int ncolumns,
  238.                    int xoffset, int yoffset,
  239.                    int scrollable,
  240.                    int *ascii)
  241. {
  242.         int index = 0;
  243.  
  244.         for (index = 0; index < nitems; index++)
  245.                 SelectItem(screen, 0x9a, 0x94, index, items, nrows, ncolumns, xoffset, yoffset);
  246.  
  247.         index = default_item;
  248.         SelectItem(screen, 0x94, 0x9a, index, items, nrows, ncolumns, xoffset, yoffset);
  249.  
  250.         for (;;) {
  251.                 int row;
  252.                 int column;
  253.                 int new_index;
  254.  
  255.                 column = index / nrows;
  256.                 row = index - (column * nrows);
  257.  
  258.                 *ascii = GetKeyPress(screen);
  259.                 switch (*ascii) {
  260.                 case 0x1c:                              /* Up */
  261.                         if (row > 0)
  262.                                 row--;
  263.                         break;
  264.                 case 0x1d:                              /* Down */
  265.                         if (row < (nrows - 1))
  266.                                 row++;
  267.                         break;
  268.                 case 0x1e:                              /* Left */
  269.                         if (column > 0)
  270.                                 column--;
  271.                         else if (scrollable)
  272.                                 return index + nitems;
  273.                         break;
  274.                 case 0x1f:                              /* Right */
  275.                         if (column < (ncolumns - 1))
  276.                                 column++;
  277.                         else if (scrollable)
  278.                                 return index + nitems * 2;
  279.                         break;
  280.                 case 0x20:                              /* Space */
  281.                 case 0x7e:                              /* Backspace */
  282.                 case 0x7f:                              /* Tab */
  283.                 case 0x9b:                              /* Select */
  284.                         return index;
  285.                 case 0x1b:                              /* Cancel */
  286.                         return -1;
  287.                 default:
  288.                         break;
  289.                 }
  290.  
  291.                 new_index = (column * nrows) + row;
  292.                 if ((new_index >= 0) && (new_index < nitems)) {
  293.                         SelectItem(screen, 0x9a, 0x94, index, items, nrows, ncolumns, xoffset, yoffset);
  294.  
  295.                         index = new_index;
  296.                         SelectItem(screen, 0x94, 0x9a, index, items, nrows, ncolumns, xoffset, yoffset);
  297.                 }
  298.         }
  299. }
  300.  
  301. void SelectSystem(UBYTE * screen)
  302. {
  303.         int system;
  304.         int ascii;
  305.  
  306.         char *menu[5] =
  307.         {
  308.                 "Atari OS/A",
  309.                 "Atari OS/B",
  310.                 "Atari 800XL",
  311.                 "Atari 130XE",
  312.                 "Atari 5200"
  313.         };
  314.  
  315.         ClearScreen(screen);
  316.         TitleScreen(screen, "Select System");
  317.         Box(screen, 0x9a, 0x94, 0, 3, 39, 23);
  318.  
  319.         system = Select(screen, 0, 5, menu, 5, 1, 1, 4, FALSE, &ascii);
  320.  
  321.         switch (system) {
  322.         case 0:
  323.                 Initialise_AtariOSA();
  324.                 break;
  325.         case 1:
  326.                 Initialise_AtariOSB();
  327.                 break;
  328.         case 2:
  329.                 Initialise_AtariXL();
  330.                 break;
  331.         case 3:
  332.                 Initialise_AtariXE();
  333.                 break;
  334.         case 4:
  335.                 Initialise_Atari5200();
  336.                 break;
  337.         default:
  338.                 break;
  339.         }
  340. }
  341.  
  342. int FilenameSort(char *filename1, char *filename2)
  343. {
  344.         return strcmp(filename1, filename2);
  345. }
  346.  
  347. List *GetDirectory(char *directory)
  348. {
  349.         DIR *dp = NULL;
  350.         List *list = NULL;
  351.  
  352.         dp = opendir(directory);
  353.         if (dp) {
  354.                 struct dirent *entry;
  355.  
  356.                 list = ListCreate();
  357.                 if (!list) {
  358.                         printf("ListCreate(): Failed\n");
  359.                         return NULL;
  360.                 }
  361.                 while ((entry = readdir(dp))) {
  362.                         char *filename;
  363.  
  364. #ifdef AMIGA_PPC
  365.                         filename = (void *)strdup(entry->d_name);
  366. #else
  367.                         filename = strdup(entry->d_name);
  368. #endif
  369.                         if (!filename) {
  370.                                 perror("strdup");
  371.                                 return NULL;
  372.                         }
  373.                         ListAddTail(list, filename);
  374.                 }
  375.  
  376.                 closedir(dp);
  377.  
  378. #ifdef AMIGA_PPC
  379.                 ListSort(list, (void *)FilenameSort);
  380. #else
  381.                 ListSort(list, FilenameSort);
  382. #endif
  383.         }
  384.         return list;
  385. }
  386.  
  387. int FileSelector(UBYTE * screen, char *directory, char *full_filename)
  388. {
  389.         List *list;
  390.         int flag = FALSE;
  391.  
  392.         list = GetDirectory(directory);
  393.         if (list) {
  394.                 char *filename;
  395.                 int nitems = 0;
  396.                 int item = 0;
  397.                 int done = FALSE;
  398.                 int offset = 0;
  399.                 int nfiles = 0;
  400.  
  401. #define NROWS 19
  402. #define NCOLUMNS 2
  403. #define MAX_FILES (NROWS * NCOLUMNS)
  404.  
  405.                 char *files[MAX_FILES];
  406.  
  407.                 ListReset(list);
  408.                 while (ListTraverse(list, (void *) &filename))
  409.                         nfiles++;
  410.  
  411.                 while (!done) {
  412.                         int ascii;
  413.  
  414.                         ListReset(list);
  415.                         for (nitems = 0; nitems < offset; nitems++)
  416.                                 ListTraverse(list, (void *) &filename);
  417.  
  418.                         for (nitems = 0; nitems < MAX_FILES; nitems++) {
  419.                                 if (ListTraverse(list, (void *) &filename)) {
  420.                                         files[nitems] = filename;
  421.                                 }
  422.                                 else
  423.                                         break;
  424.                         }
  425.  
  426.                         ClearScreen(screen);
  427.                         TitleScreen(screen, "Select File");
  428.                         Box(screen, 0x9a, 0x94, 0, 3, 39, 23);
  429.  
  430.                         item = Select(screen, item, nitems, files, NROWS, NCOLUMNS, 1, 4, TRUE, &ascii);
  431.                         if (item >= (nitems * 2 + NROWS)) {             /* Scroll Right */
  432.                                 if ((offset + NROWS + NROWS) < nfiles)
  433.                                         offset += NROWS;
  434.                                 item = item % nitems;
  435.                         }
  436.                         else if (item >= nitems) {      /* Scroll Left */
  437.                                 if ((offset - NROWS) >= 0)
  438.                                         offset -= NROWS;
  439.                                 item = item % nitems;
  440.                         }
  441.                         else if (item != -1) {
  442. #ifndef BACK_SLASH
  443.                                 sprintf(full_filename, "%s/%s", directory, files[item]);
  444. #else                                                   /* DOS, TOS fs */
  445.                                 sprintf(full_filename, "%s\\%s", directory, files[item]);
  446. #endif
  447.                                 flag = TRUE;
  448.                                 break;
  449.                         }
  450.                         else
  451.                                 break;
  452.                 }
  453.  
  454. #ifdef AMIGA_PPC
  455.                 ListFree(list,(void *)free);
  456. #else
  457.                 ListFree(list, free);
  458. #endif
  459.         }
  460.         return flag;
  461. }
  462.  
  463. void DiskManagement(UBYTE * screen)
  464. {
  465.         char *menu[8] =
  466.         {
  467.                 NULL,                                   /* D1 */
  468.                 NULL,                                   /* D2 */
  469.                 NULL,                                   /* D3 */
  470.                 NULL,                                   /* D4 */
  471.                 NULL,                                   /* D5 */
  472.                 NULL,                                   /* D6 */
  473.                 NULL,                                   /* D7 */
  474.                 NULL,                                   /* D8 */
  475.         };
  476.  
  477.         int done = FALSE;
  478.         int dsknum = 0;
  479.  
  480.         menu[0] = sio_filename[0];
  481.         menu[1] = sio_filename[1];
  482.         menu[2] = sio_filename[2];
  483.         menu[3] = sio_filename[3];
  484.         menu[4] = sio_filename[4];
  485.         menu[5] = sio_filename[5];
  486.         menu[6] = sio_filename[6];
  487.         menu[7] = sio_filename[7];
  488.  
  489.         while (!done) {
  490.                 char filename[256];
  491.                 int ascii;
  492.  
  493.                 ClearScreen(screen);
  494.                 TitleScreen(screen, "Disk Management");
  495.                 Box(screen, 0x9a, 0x94, 0, 3, 39, 23);
  496.  
  497.                 Print(screen, 0x9a, 0x94, "D1:", 1, 4);
  498.                 Print(screen, 0x9a, 0x94, "D2:", 1, 5);
  499.                 Print(screen, 0x9a, 0x94, "D3:", 1, 6);
  500.                 Print(screen, 0x9a, 0x94, "D4:", 1, 7);
  501.                 Print(screen, 0x9a, 0x94, "D5:", 1, 8);
  502.                 Print(screen, 0x9a, 0x94, "D6:", 1, 9);
  503.                 Print(screen, 0x9a, 0x94, "D7:", 1, 10);
  504.                 Print(screen, 0x9a, 0x94, "D8:", 1, 11);
  505.  
  506.                 dsknum = Select(screen, dsknum, 8, menu, 8, 1, 4, 4, FALSE, &ascii);
  507.                 if (dsknum != -1) {
  508.                         if (ascii == 0x9b) {
  509.                                 if (FileSelector(screen, atari_disk_dir, filename)) {
  510.                                         SIO_Dismount(dsknum + 1);
  511.                                         SIO_Mount(dsknum + 1, filename);
  512.                                 }
  513.                         }
  514.                         else {
  515.                                 if (strcmp(sio_filename[dsknum], "Empty") == 0)
  516.                                         SIO_DisableDrive(dsknum + 1);
  517.                                 else
  518.                                         SIO_Dismount(dsknum + 1);
  519.                         }
  520.                 }
  521.                 else
  522.                         done = TRUE;
  523.         }
  524. }
  525.  
  526. void CartManagement(UBYTE * screen)
  527. {
  528.         typedef struct {
  529.                 UBYTE id[4];
  530.                 UBYTE type[4];
  531.                 UBYTE checksum[4];
  532.                 UBYTE gash[4];
  533.         } Header;
  534.  
  535.         const int CART_UNKNOWN = 0;
  536.         const int CART_STD_8K = 1;
  537.         const int CART_STD_16K = 2;
  538.         const int CART_OSS = 3;
  539.         const int CART_AGS = 4;
  540.  
  541.         const int nitems = 5;
  542.  
  543.         static char *menu[5] =
  544.         {
  545.                 "Create Cartridge from ROM image",
  546.                 "Extract ROM image from Cartridge",
  547.                 "Insert Cartridge",
  548.                 "Remove Cartridge",
  549.                 "Enable PILL Mode"
  550.         };
  551.  
  552.         int done = FALSE;
  553.         int option = 2;
  554.  
  555.         while (!done) {
  556.                 char filename[256];
  557.                 int ascii;
  558.  
  559.                 ClearScreen(screen);
  560.                 TitleScreen(screen, "Cartridge Management");
  561.                 Box(screen, 0x9a, 0x94, 0, 3, 39, 23);
  562.  
  563.                 option = Select(screen, option, nitems, menu, nitems, 1, 1, 4, FALSE, &ascii);
  564.                 switch (option) {
  565.                 case 0:
  566.                         if (FileSelector(screen, atari_rom_dir, filename)) {
  567.                                 UBYTE image[32769];
  568.                                 int type = CART_UNKNOWN;
  569.                                 int nbytes;
  570.                                 int fd;
  571.  
  572.                                 fd = open(filename, O_RDONLY, 0777);
  573.                                 if (fd == -1) {
  574.                                         perror(filename);
  575.                                         exit(1);
  576.                                 }
  577.                                 nbytes = read(fd, image, sizeof(image));
  578.                                 switch (nbytes) {
  579.                                 case 8192:
  580.                                         type = CART_STD_8K;
  581.                                         break;
  582.                                 case 16384:
  583.                                         {
  584.                                                 const int nitems = 2;
  585.                                                 static char *menu[2] =
  586.                                                 {
  587.                                                         "Standard 16K Cartridge",
  588.                                                         "OSS Super Cartridge"
  589.                                                 };
  590.  
  591.                                                 int option = 0;
  592.  
  593.                                                 Box(screen, 0x9a, 0x94, 8, 10, 31, 13);
  594.  
  595.                                                 option = Select(screen, option,
  596.                                                                                 nitems, menu,
  597.                                                                                 nitems, 1,
  598.                                                                                 9, 11, FALSE, &ascii);
  599.                                                 switch (option) {
  600.                                                 case 0:
  601.                                                         type = CART_STD_16K;
  602.                                                         break;
  603.                                                 case 1:
  604.                                                         type = CART_OSS;
  605.                                                         break;
  606.                                                 default:
  607.                                                         continue;
  608.                                                 }
  609.                                         }
  610.                                         break;
  611.                                 case 32768:
  612.                                         type = CART_AGS;
  613.                                 }
  614.  
  615.                                 close(fd);
  616.  
  617.                                 if (type != CART_UNKNOWN) {
  618.                                         Header header;
  619.  
  620.                                         int checksum = 0;
  621.                                         int i;
  622.  
  623.                                         char fname[33];
  624. #ifdef AMIGA_PPC
  625.                                         CopyMemPPC("                                ", fname,32);
  626. #else
  627.                                         memcpy(fname, "                                ", 32);
  628. #endif
  629.                                         Box(screen, 0x9a, 0x94, 3, 9, 36, 11);
  630.                                         Print(screen, 0x94, 0x9a, "Filename", 4, 9);
  631.                                         EditString(screen, 0x9a, 0x94, 32, fname, 4, 10);
  632.                                         fname[32] = '\0';
  633.                                         RemoveSpaces(fname);
  634.  
  635.                                         for (i = 0; i < nbytes; i++)
  636.                                                 checksum += image[i];
  637.  
  638.                                         header.id[0] = 'C';
  639.                                         header.id[1] = 'A';
  640.                                         header.id[2] = 'R';
  641.                                         header.id[3] = 'T';
  642.                                         header.type[0] = (type >> 24) & 0xff;
  643.                                         header.type[1] = (type >> 16) & 0xff;
  644.                                         header.type[2] = (type >> 8) & 0xff;
  645.                                         header.type[3] = type & 0xff;
  646.                                         header.checksum[0] = (checksum >> 24) & 0xff;
  647.                                         header.checksum[1] = (checksum >> 16) & 0xff;
  648.                                         header.checksum[2] = (checksum >> 8) & 0xff;
  649.                                         header.checksum[3] = checksum & 0xff;
  650.                                         header.gash[0] = '\0';
  651.                                         header.gash[1] = '\0';
  652.                                         header.gash[2] = '\0';
  653.                                         header.gash[3] = '\0';
  654.  
  655.                                         sprintf(filename, "%s/%s", atari_rom_dir, fname);
  656.                                         fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0777);
  657.                                         if (fd != -1) {
  658.                                                 write(fd, &header, sizeof(header));
  659.                                                 write(fd, image, nbytes);
  660.                                                 close(fd);
  661.                                         }
  662.                                 }
  663.                         }
  664.                         break;
  665.                 case 1:
  666.                         if (FileSelector(screen, atari_rom_dir, filename)) {
  667.                                 int fd;
  668.  
  669.                                 fd = open(filename, O_RDONLY, 0777);
  670.                                 if (fd != -1) {
  671.                                         Header header;
  672.                                         UBYTE image[32769];
  673.                                         char fname[33];
  674.                                         int nbytes;
  675.  
  676.                                         read(fd, &header, sizeof(header));
  677.                                         nbytes = read(fd, image, sizeof(image));
  678.  
  679.                                         close(fd);
  680.  
  681. #ifdef AMIGA_PPC
  682.                                         CopyMemPPC("                                ", fname,32);
  683. #else
  684.                                         memcpy(fname, "                                ", 32);
  685. #endif
  686.                                         Box(screen, 0x9a, 0x94, 3, 9, 36, 11);
  687.                                         Print(screen, 0x94, 0x9a, "Filename", 4, 9);
  688.                                         EditString(screen, 0x9a, 0x94, 32, fname, 4, 10);
  689.                                         fname[32] = '\0';
  690.                                         RemoveSpaces(fname);
  691.  
  692.                                         sprintf(filename, "%s/%s", atari_rom_dir, fname);
  693.  
  694.                                         fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0777);
  695.                                         if (fd != -1) {
  696.                                                 write(fd, image, nbytes);
  697.                                                 close(fd);
  698.                                         }
  699.                                 }
  700.                         }
  701.                         break;
  702.                 case 2:
  703.                         if (FileSelector(screen, atari_rom_dir, filename)) {
  704.                                 if (!Insert_Cartridge(filename)) {
  705.                                         const int nitems = 4;
  706.                                         static char *menu[4] =
  707.                                         {
  708.                                                 "Standard 8K Cartridge",
  709.                                                 "Standard 16K Cartridge",
  710.                                                 "OSS Super Cartridge",
  711.                                                 "Atari 5200 Cartridge"
  712.                                         };
  713.  
  714.                                         int option = 0;
  715.  
  716.                                         Box(screen, 0x9a, 0x94, 8, 10, 31, 15);
  717.  
  718.                                         option = Select(screen, option,
  719.                                                                         nitems, menu,
  720.                                                                         nitems, 1,
  721.                                                                         9, 11, FALSE, &ascii);
  722.                                         switch (option) {
  723.                                         case 0:
  724.                                                 Insert_8K_ROM(filename);
  725.                                                 break;
  726.                                         case 1:
  727.                                                 Insert_16K_ROM(filename);
  728.                                                 break;
  729.                                         case 2:
  730.                                                 Insert_OSS_ROM(filename);
  731.                                                 break;
  732.                                         case 3:
  733.                                                 Insert_32K_5200ROM(filename);
  734.                                                 break;
  735.                                         }
  736.                                 }
  737.                                 Coldstart();
  738.                         }
  739.                         break;
  740.                 case 3:
  741.                         Remove_ROM();
  742.                         Coldstart();
  743.                         break;
  744.                 case 4:
  745.                         EnablePILL();
  746.                         Coldstart();
  747.                         break;
  748.                 default:
  749.                         done = TRUE;
  750.                         break;
  751.                 }
  752.         }
  753. }
  754.  
  755. void AboutEmulator(UBYTE * screen)
  756. {
  757.         ClearScreen(screen);
  758.  
  759.         Box(screen, 0x9a, 0x94, 0, 0, 39, 8);
  760.         CenterPrint(screen, 0x9a, 0x94, ATARI_TITLE, 1);
  761.         CenterPrint(screen, 0x9a, 0x94, "Copyright (c) 1995-1998 David Firth", 2);
  762.         CenterPrint(screen, 0x9a, 0x94, "E-Mail: david@signus.demon.co.uk", 3);
  763.         CenterPrint(screen, 0x9a, 0x94, "http://www.signus.demon.co.uk/", 4);
  764.         CenterPrint(screen, 0x9a, 0x94, "Atari PokeySound 2.3", 6);
  765.         CenterPrint(screen, 0x9a, 0x94, "Copyright (c) 1996-1998 Ron Fries", 7);
  766.  
  767.         Box(screen, 0x9a, 0x94, 0, 9, 39, 23);
  768.         CenterPrint(screen, 0x9a, 0x94, "This program is free software; you can", 10);
  769.         CenterPrint(screen, 0x9a, 0x94, "redistribute it and/or modify it under", 11);
  770.         CenterPrint(screen, 0x9a, 0x94, "the terms of the GNU General Public", 12);
  771.         CenterPrint(screen, 0x9a, 0x94, "License as published by the Free", 13);
  772.         CenterPrint(screen, 0x9a, 0x94, "Software Foundation; either version 1,", 14);
  773.         CenterPrint(screen, 0x9a, 0x94, "or (at your option) any later version.", 15);
  774.  
  775.         CenterPrint(screen, 0x94, 0x9a, "Press any Key to Continue", 22);
  776.         GetKeyPress(screen);
  777. }
  778.  
  779. void ui(UBYTE * screen)
  780. {
  781.         static int initialised = FALSE;
  782.         int option = 0;
  783.         char filename[256];
  784.         int done = FALSE;
  785.  
  786.         const int nitems = 7;
  787.         char *menu[7] =
  788.         {
  789.                 "About the Emulator",
  790.                 "Select System",
  791.                 "Disk Management",
  792.                 "Cartridge Management",
  793.                 "Power On Reset (Warm Start)",
  794.                 "Power Off Reset (Cold Start)",
  795.                 "Exit Emulator"
  796.         };
  797.  
  798.         if (!initialised) {
  799.                 if (machine == AtariXL || machine == AtariXE)
  800. #ifdef AMIGA_PPC
  801.                         CopyMemPPC(atarixl_os+0x2000,charset,8192);
  802. #else
  803.                         memcpy(charset, atarixl_os + 0x2000, 8192);
  804. #endif
  805.                 else
  806. #ifdef AMIGA_PPC
  807.                         CopyMemPPC(memory+0xe000,charset,8192);
  808. #else
  809.                         memcpy(charset, memory + 0xe000, 8192);
  810. #endif
  811.                 initialised = TRUE;
  812.         }
  813.         while (!done) {
  814.                 int ascii;
  815.  
  816.                 ClearScreen(screen);
  817.                 TitleScreen(screen, ATARI_TITLE);
  818.                 Box(screen, 0x9a, 0x94, 0, 3, 39, 23);
  819.  
  820.                 option = Select(screen, option, nitems, menu,
  821.                                                 nitems, 1, 1, 4, FALSE, &ascii);
  822.  
  823.                 switch (option) {
  824.                 case -1:
  825.                         done = TRUE;
  826.                         break;
  827.                 case 0:
  828.                         AboutEmulator(screen);
  829.                         break;
  830.                 case 1:
  831.                         SelectSystem(screen);
  832.                         break;
  833.                 case 2:
  834.                         DiskManagement(screen);
  835.                         break;
  836.                 case 3:
  837.                         CartManagement(screen);
  838.                         break;
  839.                 case 4:
  840.                         Warmstart();
  841.                         break;
  842.                 case 5:
  843.                         Coldstart();
  844.                         break;
  845.                 case 6:
  846.                         Atari800_Exit(0);
  847.                         exit(0);
  848.                 }
  849.         }
  850. }
  851.