home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / v2600 / source.lha / source / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  4.8 KB  |  238 lines

  1.  
  2.  
  3. /*****************************************************************************
  4.  
  5.    This file is part of x2600, the Atari 2600 Emulator
  6.    ===================================================
  7.    
  8.    Copyright 1996 Alex Hornby. For contributions see the file CREDITS.
  9.  
  10.    This software is distributed under the terms of the GNU General Public
  11.    License. This is free software with ABSOLUTELY NO WARRANTY.
  12.    
  13.    See the file COPYING for details.
  14.    
  15.    $Id: files.c,v 1.17 1996/11/24 16:55:40 ahornby Exp $
  16.  
  17.    Tweaked by Matthew Stroup for Amiga v2600, January 27, 1997.
  18.  
  19. ******************************************************************************/
  20.  
  21. /*
  22.    Used to load cartridge images into memory.
  23.  */
  24.  
  25. #include "config.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include "options.h"
  31. #include "types.h"
  32. #include "vmachine.h"
  33. #include "c26def.h"
  34.  
  35. /* Return the size of the file pointed to by fp */
  36. /* Avoids need for UNIX type stat, leaves file */
  37. /* pointer untouched. */
  38. long
  39. filesize (FILE * fp)
  40. {
  41.   long curpos, length;
  42.  
  43.   curpos = ftell (fp);
  44.   fseek (fp, 0L, SEEK_END);
  45.   length = ftell (fp);
  46.   fseek (fp, curpos, SEEK_SET);
  47.   return length;
  48. }
  49.  
  50. /* Load from the common 2600 format */
  51. /* Uses the tag structure from c26def.h */
  52. int
  53. loadc26 (FILE * fp, long flen)
  54. {
  55.   char databuf[65535];
  56.   struct c26_tag tag;
  57.  
  58.   while (fread (&tag, sizeof (struct c26_tag), 1, fp))
  59.     {
  60.       /* If positive then it has a data block */
  61.       if (tag.type >= 0)
  62.     {
  63.       if (fread (databuf, sizeof (UBYTE), tag.len, fp))
  64.         {
  65.           perror ("x2600 loadc26:");
  66.           fprintf (stderr, "Error reading data.\n");
  67.         }
  68.     }
  69.  
  70.       switch (tag.type)
  71.     {
  72.  
  73.     case VERSION:
  74.       if (Verbose)
  75.         printf ("File version %d\n", tag.len);
  76.       break;
  77.  
  78.     case WRITER:
  79.       if (Verbose)
  80.         printf ("Written by: %s\n", databuf);
  81.       break;
  82.  
  83.     case TVTYPE:
  84.       base_opts.tvtype = tag.len;
  85.       break;
  86.  
  87.     case CONTROLLERS:
  88.       /* Higher byte */
  89.       base_opts.lcon = (tag.len & 0xff00)>>8;
  90.       /* Low byte */
  91.       base_opts.rcon = (tag.len) & 0xff;
  92.  
  93.     case BANKING:
  94.       base_opts.bank = tag.len;
  95.       break;
  96.  
  97.     case DATA:
  98.       if (tag.len <= 16384)
  99.         memcpy (&theCart[0], databuf, tag.len);
  100.       else
  101.         {
  102.           fprintf (stderr, "Data larger that 16k!\n");
  103.           exit (-1);
  104.         }
  105.       rom_size = tag.len;
  106.       if (tag.len == 2048)
  107.         {
  108.           memcpy (&theCart[0], databuf, tag.len);
  109.           memcpy (&theCart[2048], databuf, tag.len);
  110.         }
  111.       break;
  112.  
  113.     default:
  114.       fprintf (stderr, "Unknown tag %d\n. Ignoring.", tag.type);
  115.       break;
  116.     }
  117.     }
  118.     return 1;
  119. }
  120.  
  121. /* Load a raw binary image  */
  122. /* fp: file stream to load */
  123. /* stat_data: unix stat structure for file pointed to by fp */
  124. /* returns: size of file loaded */
  125. int
  126. loadRaw (FILE * fp, long flen)
  127. {
  128.   int size = flen;
  129.   if (Verbose)
  130.     printf ("Raw loading %d bytes\n", size);
  131.  
  132.   if (size > 16384)
  133.     size = 16384;
  134.   fread (theCart, sizeof (UBYTE), size, fp);
  135.  
  136.   rom_size = size;
  137.   if (size == 2048)
  138.     {
  139.       memcpy (&theCart[2048], &theCart[0], 2048);
  140.       rom_size = 4096;
  141.     }
  142.   else if (size < 2048)
  143.     {
  144.       theCart[0x0ffc] = 0x00;
  145.       theCart[0x0ffd] = 0xf0;
  146.       rom_size = 4096;
  147.     }
  148.   
  149.   return size;
  150. }
  151.  
  152. /* Load a commodore format .prg file  */
  153. /* fp: file stream to load */
  154. /* stat_data: unix stat structure for file pointed to by fp */
  155. /* returns: size of file loaded */
  156. int
  157. loadPrg (FILE * fp, long flen)
  158. {
  159.   int start;
  160.   int rompos;
  161.   int size;
  162.   UBYTE buf1, buf2;
  163.  
  164.   /* Get the load address */
  165.   fread (&buf1, sizeof (UBYTE), 1, fp);
  166.   fread (&buf2, sizeof (UBYTE), 1, fp);
  167.   start = buf2 * 256 + buf1;
  168.  
  169.   /* Translate the load address to a ROM address */
  170.   rompos = start & 0xfff;
  171.  
  172.   /* Get length of data part */
  173.   size = flen - 2;
  174.  
  175.   if (Verbose)
  176.     printf ("Loading .prg at %x\n", start);
  177.  
  178.   /* Load the data */
  179.   fread (&theCart[rompos], sizeof (UBYTE), size, fp);
  180.  
  181.   /* Put the load address into the reset vector */
  182.   theCart[0x0ffc] = buf1;
  183.   theCart[0x0ffd] = buf2;
  184.   return size;
  185. }
  186.  
  187. /* Load a cartridge image */
  188. /* name: filename to load */
  189. /* returns: -1 on error 0 otherwise  */
  190. int
  191. loadCart (void)
  192. {
  193.   FILE *fp;
  194.   char *ext;
  195.   int flen;
  196.   char *name = base_opts.filename;
  197.  
  198.   if (name == NULL)
  199.     {
  200.       fprintf (stderr, "filename is NULL!\n");
  201.       return -1;
  202.     }
  203.  
  204.   fp = fopen (name, "rb");
  205.   if (!fp)
  206.     {
  207.       fprintf (stderr, "Can't find %s\n", name);
  208.       return -1;
  209.     }
  210.  
  211.   if (Verbose)
  212.     printf ("Loading cart: %s\n", name);
  213.   flen = filesize (fp);
  214.  
  215.   ext = strrchr (name, '.');
  216.   if (strcmp (".pal", ext) == 0 || strcmp (".vcs", ext) == 0 ||
  217.       strcmp (".raw", ext) == 0 || strcmp (".bin", ext) == 0 ||
  218.       strcmp (".BIN", ext) == 0)
  219.     {
  220.       loadRaw (fp, flen);
  221.     }
  222.   else if (strcmp (".prg", ext) == 0)
  223.     {
  224.       loadPrg (fp, flen);
  225.     }
  226.   else if (strcmp (".c26", ext) == 0)
  227.     {
  228.       loadc26 (fp, flen);
  229.     }
  230.   else
  231.     {
  232.       fprintf (stderr, "Unknown file format %s\n", ext);
  233.       return -1;
  234.     }
  235.   fclose (fp);
  236.   return 0;
  237. }
  238.