home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / tt / vmem11 / vmem_prg / vm_load.c < prev    next >
C/C++ Source or Header  |  1990-10-03  |  2KB  |  88 lines

  1. #include "vmem.h"
  2.  
  3. /*********************************************************/
  4. /* Laden eines Speicherbereiches aus dem virt. Speicher: */
  5. /*********************************************************/
  6.  
  7. long vm_load (source, destination, count)
  8.     V_ADR    source;
  9.     char    *destination;
  10.     long    count;
  11. {
  12.     BYTE    *page_ptr;
  13.     VPAGE    vmempage;
  14.     WORD    start_offset;
  15.     long    entry_size;
  16.  
  17.     vmempage = GET_PAGE (source);
  18.     start_offset = GET_OFFSET (source);
  19.  
  20. #ifdef DEBUG
  21.     printf ("VM_LOAD: page: %x  offset: %x\n", vmempage, start_offset);
  22. #endif
  23.  
  24.     if ((vmempage >= info.count_page) || (flags [vmempage] == FREE))
  25.         return (ILLEGAL_ADDRESS);
  26.  
  27.     if ((count == 0) || (count < -1))
  28.         return (ILLEGAL_COUNT);
  29.  
  30.     entry_size = PAGE_TO_ADDR (MD_COUNT (md_find (vmempage)));
  31.  
  32.     if (count == -1)
  33.         count = entry_size - start_offset;
  34.     else
  35.         entry_size = count = MIN (count, (entry_size - start_offset));
  36.  
  37.     if (start_offset > 0)
  38.     {
  39.         long    size;
  40.         
  41.         size = info.page_size - start_offset;
  42.         page_ptr = load_page (vmempage++, CACHE_READ) + start_offset;
  43.  
  44.         if (count <= size)
  45.         {
  46.             memcpy (destination, page_ptr, count);
  47.             return (count);
  48.         }
  49.         else
  50.         {
  51.             memcpy (destination, page_ptr, size);
  52.             destination += size;
  53.             entry_size -= size;
  54.         }
  55.     }
  56.  
  57.     if (entry_size >= info.page_size)
  58.     {
  59.         WORD    rcount;
  60.         long    size;
  61.  
  62.         start_offset = GET_PAGE (entry_size);
  63.         cache_clr (0, MIN (start_offset, info.cache_count));
  64.  
  65.         while (start_offset > 0)
  66.         {
  67.             rcount = MIN (start_offset, info.cache_count);
  68.             size = PAGE_TO_ADDR (rcount);
  69.  
  70.             read_sequence (0, rcount, vmempage);
  71.             memcopy (destination, cache, size);
  72.  
  73.             destination += size;
  74.             entry_size -= size;
  75.             vmempage += rcount;
  76.             start_offset -= rcount;
  77.         }
  78.     }
  79.  
  80.     if (entry_size > 0)
  81.     {
  82.         page_ptr = load_page (vmempage, CACHE_READ);
  83.         memcpy (destination, page_ptr, entry_size);
  84.     }
  85.  
  86.     return (count);
  87. }
  88.