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