Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.         dZ80_LoadZ80File.
  3.  
  4.         Allocates the Z80's address space and loads in the source file.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include "dissz80p.h"
  12.  
  13. int dZ80_LoadZ80File(DISZ80 *d, DWORD *pBytesLoaded)
  14. {
  15.         FILE    *f;
  16.         char    buf[_MAX_PATH + 64];
  17.  
  18.         *pBytesLoaded = 0;                      /* We've not loaded anything yet */
  19.  
  20.         if (d->mem0Start == NULL)
  21.                 d->mem0Start = (BYTE *)malloc(Z80MEMSIZE);
  22.  
  23.         if (d->mem0Start == NULL)
  24.                 {
  25.                 dZ80_Error(d, "dZ80 couldn't allocate memory for the Z80 program.");
  26.                 return DERR_OUTOFMEM;
  27.                 }
  28.  
  29.         memset(d->mem0Start, 0, Z80MEMSIZE);
  30.  
  31.         f = fopen(d->srcFileName, "rb");
  32.         if (f == NULL)
  33.                 {
  34.                 sprintf(buf, "Couldn't open the source file \"%s\"", d->srcFileName);
  35.                 dZ80_Error(d, buf);
  36.                 return(DERR_COULDNTOPENFILE);
  37.                 }
  38.  
  39. /* Get the file size */
  40.         if (fseek(f, 0, SEEK_END))
  41.                 {
  42.                 sprintf(buf, "Couldn't determine the size of the file \"%s\"", d->srcFileName);
  43.                 dZ80_Error(d, buf);
  44.                 }
  45.         else
  46.                 {
  47.                 if (ftell(f) > 65536L )
  48.                         {
  49.                         sprintf(buf, "Warning: The file \"%s\" is over 65,536 bytes in length.", d->srcFileName);
  50.                         dZ80_Error(d, buf);
  51.                         }
  52.                 }
  53.  
  54. /* Put the file's position back in the correct place */
  55.         fseek(f, d->fileHeaderSize, SEEK_SET);
  56.  
  57. /* Read the whole file in one gulp (should be only a small file) */
  58.         *pBytesLoaded = fread(d->mem0Start + d->fileStartAddr, 1, Z80MEMSIZE - d->fileStartAddr, f);
  59.  
  60.         fclose(f);     
  61.        
  62.         return DERR_NONE;       /* All's well */
  63. }
  64.  
  65.