home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / DISKVID.C < prev    next >
C/C++ Source or Header  |  1991-07-07  |  23KB  |  722 lines

  1. /*
  2.    "Disk-Video" (and RAM-Video and Expanded-Memory Video) routines
  3.  
  4.    Reworked with fast caching July '90 by Pieter Branderhorst.
  5.    (I'm proud of this cache handler, had to get my name on it!)
  6.    Caution when modifying any code in here:  bugs are possible which
  7.    slow the cache substantially but don't cause incorrect results.
  8.    Do timing tests for a variety of situations after any change.
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include "fractint.h"
  15.  
  16. extern int sxdots, sydots, colors;
  17. extern int dotmode;      /* video access method, 11 if really disk video */
  18. extern int diskisactive;  /* set by fractint to disable re-init */
  19. extern int debugflag;
  20. extern int diskflag;
  21.  
  22. #define BOXROW     6
  23. #define BOXCOL     11
  24. #define BOXWIDTH 57
  25. #define BOXDEPTH 12
  26.  
  27. int disk16bit=0;       /* storing 16 bit values for continuous potential */
  28.  
  29. extern int Shadowing, AntiAliasing;
  30.  
  31. static int timetodisplay;
  32. static FILE *fp = NULL;
  33. static int disktarga;
  34.  
  35. #define BLOCKLEN 64    /* must be a power of 2, must match next */
  36. #define BLOCKSHIFT 6    /* must match above */
  37. #define CACHEMIN 4    /* minimum cache size in Kbytes */
  38. #define CACHEMAX 64    /* maximum cache size in Kbytes */
  39. #define FREEMEM  20    /* try to leave this much far memory unallocated */
  40. #define HASHSIZE 1024    /* power of 2, near CACHEMAX/(BLOCKLEN+8) */
  41.  
  42. static struct cache {    /* structure of each cache entry */
  43.    long offset;            /* pixel offset in image */
  44.    unsigned char pixel[BLOCKLEN];  /* one pixel per byte (this *is* faster) */
  45.    unsigned int hashlink;       /* ptr to next cache entry with same hash */
  46.    unsigned int dirty : 1;       /* changed since read? */
  47.    unsigned int lru : 1;       /* recently used? */
  48.    } far *cache_end, far *cache_lru, far *cur_cache;
  49.  
  50. static struct cache far *cache_start = NULL;
  51. static long high_offset;       /* highwater mark of writes */
  52. static long seek_offset;       /* what we'll get next if we don't seek */
  53. static long cur_offset;        /* offset of last block referenced */
  54. static int cur_row;
  55. static long cur_row_base;
  56. static unsigned int far *hash_ptr = NULL;
  57. static int pixelshift;
  58. static int headerlength;
  59. static unsigned int rowsize = 0;   /* doubles as a disk video not ok flag */
  60. static unsigned int colsize;       /* sydots, *2 when pot16bit */
  61.  
  62. static char far *charbuf = NULL;   /* currently used only with XMM */
  63.  
  64. /* For expanded memory: */
  65. static unsigned char far *expmemoryvideo;
  66. static int oldexppage,expoffset;
  67. static unsigned int emmhandle = 0;
  68.  
  69. /* For extended memory: */
  70. static unsigned int xmmhandle = 0;
  71. static long extoffset;
  72. static unsigned char far *extbufptr, far *endreadbuf, far *endwritebuf;
  73. struct XMM_Move
  74.   {
  75.     unsigned long   Length;
  76.     unsigned int    SourceHandle;
  77.     unsigned long   SourceOffset;
  78.     unsigned int    DestHandle;
  79.     unsigned long   DestOffset;
  80.   };
  81. #define XMMREADLEN 64    /* amount transferred from extended mem at once */
  82. #define XMMWRITELEN 256 /* max amount transferred to extended mem at once,
  83.                must be a factor of 1024 and >= XMMREADLEN */
  84.  
  85. int startdisk(),pot_startdisk();
  86. void enddisk();
  87. int readdisk(unsigned int,unsigned int);
  88. void writedisk(unsigned int,unsigned int,unsigned int);
  89. int targa_startdisk(FILE *, int);
  90. void targa_readdisk (unsigned int, unsigned int,
  91.              unsigned char *, unsigned char *, unsigned char *);
  92. void targa_writedisk(unsigned int, unsigned int,
  93.              unsigned char, unsigned char, unsigned char);
  94. void dvid_status(int,char *);
  95.  
  96. static int  _fastcall near common_startdisk(int newrowsize, int newcolsize);
  97. static void _fastcall near findload_cache(long);
  98. static struct cache far * _fastcall near find_cache(long);
  99. static void near write_cache_lru(void);
  100. static void (_fastcall near *put_char)(unsigned char);
  101. static void _fastcall near disk_putc(unsigned char);
  102. static void _fastcall near exp_putc(unsigned char);
  103. static void _fastcall near ext_putc(unsigned char);
  104. static unsigned char (near *get_char)();
  105. static unsigned char near disk_getc();
  106. static unsigned char near exp_getc();
  107. static unsigned char near ext_getc();
  108. static void (_fastcall near *doseek)(long);
  109. static void _fastcall near disk_seek(long);
  110. static void _fastcall near exp_seek(long);
  111. static void _fastcall near ext_seek(long);
  112.  
  113. int made_dsktemp = 0;
  114.  
  115. int startdisk()
  116. {
  117.    if (!diskisactive)
  118.       return(0);
  119.    headerlength = disktarga = 0;
  120.    return (common_startdisk(sxdots,sydots));
  121.    }
  122.  
  123. int pot_startdisk()
  124. {
  125.    int i;
  126.    if (dotmode == 11) /* ditch the original disk file */
  127.       enddisk();
  128.    else
  129.       showtempmsg("clearing 16bit pot work area");
  130.    headerlength = disktarga = 0;
  131.    i = common_startdisk(sxdots,sydots<<1);
  132.    cleartempmsg();
  133.    disk16bit = 1;
  134.    return (i);
  135.    }
  136.  
  137. int targa_startdisk(FILE *targafp,int overhead)
  138. {
  139.    int i;
  140.    if (dotmode == 11) { /* ditch the original disk file, make just the targa */
  141.       enddisk();      /* close the 'screen' */
  142.       setnullvideo(); /* set readdot and writedot routines to do nothing */
  143.       }
  144.    headerlength = overhead;
  145.    fp = targafp;
  146.    disktarga = 1;
  147.    i = common_startdisk(sxdots*3,sydots);
  148.    high_offset = 100000000L; /* targa not necessarily init'd to zeros */
  149.    return (i);
  150. }
  151.  
  152. static int _fastcall near common_startdisk(int newrowsize, int newcolsize)
  153. {
  154.    int i,success;
  155.    long memorysize;
  156.    unsigned int far *fwd_link;
  157.    struct cache far *ptr1;
  158.    long longtmp;
  159.    unsigned int cache_size;
  160.    int exppages;
  161.    struct XMM_Move MoveStruct;
  162.    unsigned char far *tempfar;
  163.  
  164.    if (diskflag)
  165.       enddisk();
  166.    if (dotmode == 11) { /* otherwise, real screen also in use, don't hit it */
  167.       char buf[20];
  168.       helptitle();
  169.       setattr(1,0,C_DVID_BKGRD,24*80);    /* init rest to background */
  170.       for (i = 0; i < BOXDEPTH; ++i)
  171.      setattr(BOXROW+i,BOXCOL,C_DVID_LO,BOXWIDTH);  /* init box */
  172.       putstring(BOXROW+2,BOXCOL+4,C_DVID_HI,"'Disk-Video' mode");
  173.       putstring(BOXROW+4,BOXCOL+4,C_DVID_LO,"Screen resolution: ");
  174.       sprintf(buf,"%d x %d",sxdots,sydots);
  175.       putstring(-1,-1,C_DVID_LO,buf);
  176.       if (disktarga)
  177.      putstring(-1,-1,C_DVID_LO,"  24 bit Targa");
  178.       else {
  179.      putstring(-1,-1,C_DVID_LO,"  Colors: ");
  180.      sprintf(buf,"%d",colors);
  181.      putstring(-1,-1,C_DVID_LO,buf);
  182.      }
  183.       putstring(BOXROW+8,BOXCOL+4,C_DVID_LO,"Status:");
  184.       dvid_status(0,"clearing the 'screen'");
  185.       }
  186.    cur_offset = seek_offset = high_offset = -1;
  187.    cur_row    = -1;
  188.    if (disktarga)
  189.       pixelshift = 0;
  190.    else {
  191.       pixelshift = 3;
  192.       i = 2;
  193.       while (i < colors) {
  194.      i *= i;
  195.      --pixelshift;
  196.      }
  197.       }
  198.    timetodisplay = 1000;  /* time-to-display-status counter */
  199.  
  200.    /* allocate cache: try for the max; leave FREEMEMk free if we can get
  201.       that much or more; if we can't get that much leave 1/2 of whatever
  202.       there is free; demand a certain minimum or nogo at all */
  203.    for (cache_size = CACHEMAX; cache_size >= CACHEMIN; --cache_size) {
  204.       longtmp = (cache_size < FREEMEM) ? (long)cache_size << 11
  205.                        : (long)(cache_size+FREEMEM) << 10;
  206.       if ((tempfar = farmemalloc(longtmp))) {
  207.      farmemfree(tempfar);
  208.      break;
  209.      }
  210.       }
  211.    longtmp = (long)cache_size << 10;
  212.    cache_start = (struct cache far *)farmemalloc(longtmp);
  213.    if (cache_size == 64)
  214.       --longtmp; /* safety for next line */
  215.    cache_end = (cache_lru = cache_start) + longtmp / sizeof(*cache_start);
  216.    hash_ptr  = (unsigned int far *)farmemalloc((long)(HASHSIZE<<1));
  217.    if (cache_start == NULL || hash_ptr == NULL) {
  218.       static char far msg[]={"*** insufficient free memory for cache buffers ***"};
  219.       stopmsg(0,msg);
  220.       return(-1);
  221.       }
  222.    if (dotmode == 11) {
  223.       char buf[50];
  224.       sprintf(buf,"Cache size: %dK\n\n",cache_size);
  225.       putstring(BOXROW+6,BOXCOL+4,C_DVID_LO,buf);
  226.       }
  227.  
  228.    /* preset cache to all invalid entries so we don't need free list logic */
  229.    for (i = 0; i < HASHSIZE; ++i)
  230.       hash_ptr[i] = 0xffff; /* 0xffff marks the end of a hash chain */
  231.    longtmp = 100000000L;
  232.    for (ptr1 = cache_start; ptr1 < cache_end; ++ptr1) {
  233.       ptr1->dirty = ptr1->lru = 0;
  234.       fwd_link = hash_ptr
  235.      + (((unsigned short)(longtmp+=BLOCKLEN) >> BLOCKSHIFT) & (HASHSIZE-1));
  236.       ptr1->offset = longtmp;
  237.       ptr1->hashlink = *fwd_link;
  238.       *fwd_link = (char far *)ptr1 - (char far *)cache_start;
  239.       }
  240.  
  241.    memorysize = (long)(newcolsize) * newrowsize;
  242.    if ((i = (short)memorysize & (BLOCKLEN-1)) != 0)
  243.       memorysize += BLOCKLEN - i;
  244.    memorysize >>= pixelshift;
  245.    diskflag = 1;
  246.    rowsize = newrowsize;
  247.    colsize = newcolsize;
  248.  
  249.    if (debugflag != 420 && debugflag != 422 /* 422=xmm test, 420=disk test */
  250.      && disktarga == 0) {
  251.       /* Try Expanded Memory */
  252.       exppages = (memorysize + 16383) >> 14;
  253.       if ((expmemoryvideo = emmquery()) != NULL
  254.     && (emmhandle = emmallocate(exppages)) != 0) {
  255.      if (dotmode == 11)
  256.         putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Expanded Memory");
  257.      for (oldexppage = 0; oldexppage < exppages; oldexppage++)
  258.         emmclearpage(oldexppage, emmhandle); /* clear the "video" */
  259.      put_char = exp_putc;
  260.      get_char = exp_getc;
  261.      doseek  = exp_seek;
  262.      goto common_okend;
  263.      }
  264.       }
  265.  
  266.    if (debugflag != 420 && disktarga == 0) {
  267.       /* Try Extended Memory */
  268.       if ((charbuf = farmemalloc((long)XMMWRITELEN)) != NULL
  269.     && xmmquery() !=0
  270.     && (xmmhandle = xmmallocate(longtmp = (memorysize+1023) >> 10)) != 0) {
  271.      if (dotmode == 11)
  272.         putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Extended Memory");
  273.      for (i = 0; i < XMMWRITELEN; i++)
  274.         charbuf[i] = 0;
  275.      MoveStruct.SourceHandle = 0;     /* Source is in conventional memory */
  276.      MoveStruct.SourceOffset = (unsigned long)charbuf;
  277.      MoveStruct.DestHandle     = xmmhandle;
  278.      MoveStruct.Length     = XMMWRITELEN;
  279.      MoveStruct.DestOffset     = 0;
  280.      longtmp *= (1024/XMMWRITELEN);
  281.      while (--longtmp >= 0) {
  282.         if ((success = xmmmoveextended(&MoveStruct)) == 0) break;
  283.         MoveStruct.DestOffset += XMMWRITELEN;
  284.         }
  285.      if (success) {
  286.         put_char = ext_putc;
  287.         get_char = ext_getc;
  288.         doseek  = ext_seek;
  289.         extbufptr = endreadbuf = charbuf;
  290.         endwritebuf = charbuf + XMMWRITELEN;
  291.         goto common_okend;
  292.         }
  293.      xmmdeallocate(xmmhandle);     /* Clear the memory */
  294.      xmmhandle = 0;          /* Signal same */
  295.      }
  296.       }
  297.  
  298.    if (dotmode == 11)
  299.       putstring(BOXROW+2,BOXCOL+23,C_DVID_LO,"Using your Disk Drive");
  300.    if (disktarga == 0) {
  301.       if ((fp = fopen("FRACTINT.DSK","w+b")) != NULL) {
  302.      made_dsktemp = 1;
  303.      while (--memorysize >= 0) /* "clear the screen" (write to the disk) */
  304.         putc(0,fp);
  305.      if (ferror(fp)) {
  306.         static char far msg[]={"*** insufficient free disk space ***"};
  307.         stopmsg(0,msg);
  308.         fclose(fp);
  309.         fp = NULL;
  310.         rowsize = 0;
  311.         return(-1);
  312.         }
  313.      fclose(fp); /* so clusters aren't lost if we crash while running */
  314.      fp = fopen("FRACTINT.DSK","r+b"); /* reopen */
  315.      }
  316.       if (fp == NULL) {
  317.      static char far msg[]={"*** Couldn't create FRACTINT.DSK ***"};
  318.      stopmsg(0,msg);
  319.      rowsize = 0;
  320.      return(-1);
  321.      }
  322.       }
  323.    put_char = disk_putc;
  324.    get_char = disk_getc;
  325.    doseek  = disk_seek;
  326.  
  327. common_okend:
  328.    if (dotmode == 11)
  329.       dvid_status(0,"");
  330.    return(0);
  331. }
  332.  
  333. void enddisk()
  334. {
  335.    if (fp != NULL) {
  336.       if (disktarga) /* flush the cache */
  337.      for (cache_lru = cache_start; cache_lru < cache_end; ++cache_lru)
  338.         if (cache_lru->dirty)
  339.            write_cache_lru();
  340.       fclose(fp);
  341.       }
  342.    if (charbuf != NULL)
  343.       farmemfree((void far *)charbuf);
  344.    if (hash_ptr != NULL)
  345.       farmemfree((void far *)hash_ptr);
  346.    if (cache_start != NULL)
  347.       farmemfree((void far *)cache_start);
  348.    if (emmhandle != 0)     /* Expanded memory video? */
  349.       emmdeallocate(emmhandle);
  350.    if (xmmhandle != 0)     /* Extended memory video? */
  351.       xmmdeallocate(xmmhandle);
  352.    diskflag = rowsize = emmhandle = xmmhandle = disk16bit = 0;
  353.    hash_ptr    = NULL;
  354.    cache_start = NULL;
  355.    charbuf     = NULL;
  356.    fp           = NULL;
  357. }
  358.  
  359. int readdisk(unsigned int col, unsigned int row)
  360. {
  361.    int col_subscr;
  362.    long offset;
  363.    char buf[41];
  364.    if (--timetodisplay < 0) {  /* time to display status? */
  365.       if (dotmode == 11) {
  366.      sprintf(buf," reading line %4d",
  367.         (row >= sydots) ? row-sydots : row); /* adjust when potfile */
  368.      dvid_status(0,buf);
  369.      }
  370.       timetodisplay = 1000;
  371.       }
  372.    if (row != cur_row)    { /* try to avoid ghastly code generated for multiply */
  373.       if (row >= colsize) /* while we're at it avoid this test if not needed  */
  374.      return(0);
  375.       cur_row_base = (long)(cur_row = row) * rowsize;
  376.       }
  377.    if (col >= rowsize)
  378.       return(0);
  379.    offset = cur_row_base + col;
  380.    col_subscr = (short)offset & (BLOCKLEN-1); /* offset within cache entry */
  381.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  382.       findload_cache(offset & (0L-BLOCKLEN));
  383.    return (cur_cache->pixel[col_subscr]);
  384. }
  385.  
  386. void targa_readdisk(unsigned int col, unsigned int row,
  387.             unsigned char *red, unsigned char *green, unsigned char *blue)
  388. {
  389.    col *= 3;
  390.    *blue  = readdisk(col,row);
  391.    *green = readdisk(++col,row);
  392.    *red   = readdisk(col+1,row);
  393. }
  394.  
  395. void writedisk(unsigned int col, unsigned int row, unsigned int color)
  396. {
  397.    int col_subscr;
  398.    long offset;
  399.    char buf[41];
  400.    if (--timetodisplay < 0) {  /* time to display status? */
  401.       if (dotmode == 11) {
  402.      sprintf(buf," writing line %4d",
  403.         (row >= sydots) ? row-sydots : row); /* adjust when potfile */
  404.      dvid_status(0,buf);
  405.      }
  406.       timetodisplay = 1000;
  407.       }
  408.    if (row != cur_row)    { /* try to avoid ghastly code generated for multiply */
  409.       if (row >= colsize) /* while we're at it avoid this test if not needed  */
  410.      return;
  411.       cur_row_base = (long)(cur_row = row) * rowsize;
  412.       }
  413.    if (col >= rowsize)
  414.       return;
  415.    offset = cur_row_base + col;
  416.    col_subscr = (short)offset & (BLOCKLEN-1);
  417.    if (cur_offset != (offset & (0L-BLOCKLEN))) /* same entry as last ref? */
  418.       findload_cache(offset & (0L-BLOCKLEN));
  419.    if (cur_cache->pixel[col_subscr] != (color & 0xff)) {
  420.       cur_cache->pixel[col_subscr] = color;
  421.       cur_cache->dirty = 1;
  422.       }
  423.    if (Shadowing) {
  424.       unsigned Mask;
  425.       Mask = (1 << AntiAliasing) - 1;
  426.       if(!(col & Mask) && !(row & Mask))
  427.      ShadowPutColor(col, row, color);
  428.       }
  429. }
  430.  
  431. void targa_writedisk(unsigned int col, unsigned int row,
  432.             unsigned char red, unsigned char green, unsigned char blue)
  433. {
  434.    writedisk(col*=3,row,blue);
  435.    writedisk(++col, row,green);
  436.    writedisk(col+1, row,red);
  437. }
  438.  
  439. static void _fastcall near findload_cache(long offset) /* used by read/write */
  440. {
  441.    unsigned int tbloffset;
  442.    int i,j;
  443.    unsigned int far *fwd_link;
  444.    unsigned char far *pixelptr;
  445.    unsigned char tmpchar;
  446.    cur_offset = offset; /* note this for next reference */
  447.    /* check if required entry is in cache - lookup by hash */
  448.    tbloffset = hash_ptr[ ((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1) ];
  449.    while (tbloffset != 0xffff) { /* follow the hash chain */
  450.       (char far *)cur_cache = (char far *)cache_start + tbloffset;
  451.       if (cur_cache->offset == offset) { /* great, it is in the cache */
  452.      cur_cache->lru = 1;
  453.      return;
  454.      }
  455.       tbloffset = cur_cache->hashlink;
  456.       }
  457.    /* must load the cache entry from backing store */
  458.    while (1) { /* look around for something not recently used */
  459.       if (++cache_lru >= cache_end)
  460.      cache_lru = cache_start;
  461.       if (cache_lru->lru == 0)
  462.      break;
  463.       cache_lru->lru = 0;
  464.       }
  465.    if (cache_lru->dirty) /* must write this block before reusing it */
  466.       write_cache_lru();
  467.    /* remove block at cache_lru from its hash chain */
  468.    fwd_link = hash_ptr
  469.         + (((unsigned short)cache_lru->offset >> BLOCKSHIFT) & (HASHSIZE-1));
  470.    tbloffset = (char far *)cache_lru - (char far *)cache_start;
  471.    while (*fwd_link != tbloffset)
  472.       fwd_link = &((struct cache far *)((char far *)cache_start+*fwd_link))->hashlink;
  473.    *fwd_link = cache_lru->hashlink;
  474.    /* load block */
  475.    cache_lru->dirty  = 0;
  476.    cache_lru->lru    = 1;
  477.    cache_lru->offset = offset;
  478.    pixelptr = &cache_lru->pixel[0];
  479.    if (offset > high_offset) { /* never been this high before, just clear it */
  480.       high_offset = offset;
  481.       for (i = 0; i < BLOCKLEN; ++i)
  482.      *(pixelptr++) = 0;
  483.       }
  484.    else {
  485.       if (offset != seek_offset)
  486.      (*doseek)(offset >> pixelshift);
  487.       seek_offset = offset + BLOCKLEN;
  488.       switch (pixelshift) {
  489.      case 0:
  490.         for (i = 0; i < BLOCKLEN; ++i)
  491.            *(pixelptr++) = (*get_char)();
  492.         break;
  493.      case 1:
  494.         for (i = 0; i < BLOCKLEN/2; ++i) {
  495.            tmpchar = (*get_char)();
  496.            *(pixelptr++) = tmpchar >> 4;
  497.            *(pixelptr++) = tmpchar & 15;
  498.            }
  499.         break;
  500.      case 2:
  501.         for (i = 0; i < BLOCKLEN/4; ++i) {
  502.            tmpchar = (*get_char)();
  503.            for (j = 6; j >= 0; j -= 2)
  504.           *(pixelptr++) = (tmpchar >> j) & 3;
  505.            }
  506.         break;
  507.      case 3:
  508.         for (i = 0; i < BLOCKLEN/8; ++i) {
  509.            tmpchar = (*get_char)();
  510.            for (j = 7; j >= 0; --j)
  511.           *(pixelptr++) = (tmpchar >> j) & 1;
  512.            }
  513.         break;
  514.      }
  515.       }
  516.    /* add new block to its hash chain */
  517.    fwd_link = hash_ptr + (((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1));
  518.    cache_lru->hashlink = *fwd_link;
  519.    *fwd_link = (char far *)cache_lru - (char far *)cache_start;
  520.    cur_cache = cache_lru;
  521.    }
  522.  
  523. static struct cache far * _fastcall near find_cache(long offset)
  524. /* lookup for write_cache_lru */
  525. {
  526.    unsigned int tbloffset;
  527.    struct cache far *ptr1;
  528.    tbloffset = hash_ptr[ ((unsigned short)offset >> BLOCKSHIFT) & (HASHSIZE-1) ];
  529.    while (tbloffset != 0xffff) {
  530.       (char far *)ptr1 = (char far *)cache_start + tbloffset;
  531.       if (ptr1->offset == offset)
  532.      return (ptr1);
  533.       tbloffset = ptr1->hashlink;
  534.       }
  535.    return (NULL);
  536. }
  537.  
  538. static void near write_cache_lru()
  539. {
  540.    int i,j;
  541.    unsigned char far *pixelptr;
  542.    long offset;
  543.    unsigned char tmpchar;
  544.    struct cache far *ptr1, far *ptr2;
  545. #define WRITEGAP 4 /* 1 for no gaps */
  546.    /* scan back to also write any preceding dirty blocks, skipping small gaps */
  547.    ptr1 = cache_lru;
  548.    offset = ptr1->offset;
  549.    i = 0;
  550.    while (++i <= WRITEGAP) {
  551.       if ((ptr2 = find_cache(offset -= BLOCKLEN)) != NULL && ptr2->dirty) {
  552.      ptr1 = ptr2;
  553.      i = 0;
  554.      }
  555.       }
  556.    /* write all consecutive dirty blocks (often whole cache in 1pass modes) */
  557.    /* keep going past small gaps */
  558. write_seek:
  559.    (*doseek)(ptr1->offset >> pixelshift);
  560. write_stuff:
  561.    pixelptr = &ptr1->pixel[0];
  562.    switch (pixelshift) {
  563.       case 0:
  564.      for (i = 0; i < BLOCKLEN; ++i)
  565.         (*put_char)(*(pixelptr++));
  566.      break;
  567.       case 1:
  568.      for (i = 0; i < BLOCKLEN/2; ++i) {
  569.         tmpchar = *(pixelptr++) << 4;
  570.         tmpchar += *(pixelptr++);
  571.         (*put_char)(tmpchar);
  572.         }
  573.      break;
  574.       case 2:
  575.      for (i = 0; i < BLOCKLEN/4; ++i) {
  576.         for (j = 6; j >= 0; j -= 2)
  577.            tmpchar = (tmpchar << 2) + *(pixelptr++);
  578.         (*put_char)(tmpchar);
  579.         }
  580.      break;
  581.       case 3:
  582.      for (i = 0; i < BLOCKLEN/8; ++i) {
  583.         (*put_char)((unsigned char)
  584.             ((((((((((((((*pixelptr
  585.                         << 1)
  586.                         | *(pixelptr+1) )
  587.                         << 1)
  588.             | *(pixelptr+2) )
  589.                         << 1)
  590.                         | *(pixelptr+3) )
  591.                         << 1)
  592.             | *(pixelptr+4) )
  593.                         << 1)
  594.                         | *(pixelptr+5) )
  595.                         << 1)
  596.             | *(pixelptr+6) )
  597.                         << 1)
  598.                         | *(pixelptr+7)));
  599.         pixelptr += 8;
  600.         }
  601.      break;
  602.       }
  603.    ptr1->dirty = 0;
  604.    offset = ptr1->offset + BLOCKLEN;
  605.    if ((ptr1 = find_cache(offset)) && ptr1->dirty)
  606.       goto write_stuff;
  607.    i = 1;
  608.    while (++i <= WRITEGAP) {
  609.       if ((ptr1 = find_cache(offset += BLOCKLEN)) && ptr1->dirty)
  610.      goto write_seek;
  611.       }
  612.    seek_offset = -1; /* force a seek before next read */
  613. }
  614.  
  615. /* Seek, get_char, put_char routines follow for expanded, extended, disk.
  616.    Note that the calling logic always separates get_char and put_char
  617.    sequences with a seek between them.    A get_char is never followed by
  618.    a put_char nor v.v. without a seek between them.
  619.    */
  620.  
  621. static void _fastcall near disk_seek(long offset)    /* real disk seek */
  622. {
  623.    fseek(fp,offset+headerlength,SEEK_SET);
  624.    }
  625.  
  626. static unsigned char near disk_getc()            /* real disk get_char */
  627. {
  628.    return(getc(fp));
  629.    }
  630.  
  631. static void _fastcall near disk_putc(unsigned char c)    /* real disk put_char */
  632. {
  633.    putc(c,fp);
  634.    }
  635.  
  636. static void _fastcall near exp_seek(long offset)    /* expanded mem seek */
  637. {
  638.    int page;
  639.    expoffset = (short)offset & 0x3fff;
  640.    page = offset >> 14;
  641.    if (page != oldexppage) { /* time to get a new page? */
  642.       oldexppage = page;
  643.       emmgetpage(page,emmhandle);
  644.       }
  645.    }
  646.  
  647. static unsigned char near exp_getc()            /* expanded get_char */
  648. {
  649.    if (expoffset > 0x3fff) /* wrapped into a new page? */
  650.       exp_seek((long)(oldexppage+1) << 14);
  651.    return(expmemoryvideo[expoffset++]);
  652.    }
  653.  
  654. static void _fastcall near exp_putc(unsigned char c)    /* expanded put_char */
  655. {
  656.    if (expoffset > 0x3fff) /* wrapped into a new page? */
  657.       exp_seek((long)(oldexppage+1) << 14);
  658.    expmemoryvideo[expoffset++] = c;
  659.    }
  660.  
  661. static void near ext_writebuf() /* subrtn for extended mem seek/put_char */
  662. {
  663.    struct XMM_Move MoveStruct;
  664.    MoveStruct.Length = extbufptr - charbuf;
  665.    MoveStruct.SourceHandle = 0; /* Source is conventional memory */
  666.    MoveStruct.SourceOffset = (unsigned long)charbuf;
  667.    MoveStruct.DestHandle = xmmhandle;
  668.    MoveStruct.DestOffset = extoffset;
  669.    xmmmoveextended(&MoveStruct);
  670.    }
  671.  
  672. static void _fastcall near ext_seek(long offset)    /* extended mem seek */
  673. {
  674.    if (extbufptr > endreadbuf) /* only true if there was a put_char sequence */
  675.       ext_writebuf();
  676.    extoffset = offset;
  677.    extbufptr = endreadbuf = charbuf;
  678.    }
  679.  
  680. static unsigned char near ext_getc()            /* extended get_char */
  681. {
  682.    struct XMM_Move MoveStruct;
  683.    if (extbufptr >= endreadbuf) { /* drained the last read buffer we fetched? */
  684.       MoveStruct.Length = XMMREADLEN;
  685.       MoveStruct.SourceHandle = xmmhandle; /* Source is extended memory */
  686.       MoveStruct.SourceOffset = extoffset;
  687.       MoveStruct.DestHandle = 0;
  688.       MoveStruct.DestOffset = (unsigned long)charbuf;
  689.       xmmmoveextended(&MoveStruct);
  690.       extoffset += XMMREADLEN;
  691.       endreadbuf = XMMREADLEN + (extbufptr = charbuf);
  692.       }
  693.    return (*(extbufptr++));
  694.    }
  695.  
  696. static void _fastcall near ext_putc(unsigned char c)    /* extended get_char */
  697. {
  698.    if (extbufptr >= endwritebuf) { /* filled the local write buffer? */
  699.       ext_writebuf();
  700.       extoffset += XMMWRITELEN;
  701.       extbufptr = charbuf;
  702.       }
  703.    *(extbufptr++) = c;
  704.    }
  705.  
  706. void dvid_status(int line,char *msg)
  707. {
  708.    char buf[41];
  709.    int attrib;
  710.    memset(buf,' ',40);
  711.    memcpy(buf,msg,strlen(msg));
  712.    buf[40] = 0;
  713.    attrib = C_DVID_HI;
  714.    if (line >= 100) {
  715.       line -= 100;
  716.       attrib = C_STOP_ERR;
  717.       }
  718.    putstring(BOXROW+8+line,BOXCOL+12,attrib,buf);
  719.    movecursor(25,80);
  720. }
  721.  
  722.