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