home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / graphics / ftpovstc / ibm.c < prev    next >
Text File  |  1994-05-25  |  120KB  |  3,099 lines

  1. /****************************************************************************
  2. *
  3. *  ATTENTION!!!
  4. *
  5. *  THIS FILE HAS BEEN MODIFIED!!! IT IS NOT PART OF THE OFFICAL
  6. *  POV-RAY 2.2 DISTRIBUTION!!!
  7. *
  8. *  THIS FILE IS PART OF "FASTER THAN POV-RAY" (VERSION 1.1),
  9. *  A SPED-UP VERSION OF POV-RAY 2.2. USE AT YOUR OWN RISK!!!!!!
  10. *
  11. *  New files: addon0.c, addon1.c, addon2.c, addon3.c, addon.h
  12. *
  13. *  The additional modules were written by Dieter Bayer.
  14. *
  15. *  Send comments, suggestions, bugs, ideas ... to:
  16. *
  17. *  dieter@cip.e-technik.uni-erlangen.de
  18. *
  19. *  All changed/added lines are enclosed in #ifdef DB_CODE ... #endif
  20. *
  21. *  The vista projection was taken from:
  22. *
  23. *    A. Hashimoto, T. Akimoto, K. Mase, and Y. Suenaga, 
  24. *    "Vista Ray-Tracing: High Speed Ray Tracing Using Perspective
  25. *    Projection Image", New Advances in Computer Graphics, Proceedings
  26. *    of CG International '89, R. A. Earnshaw, B. Wyvill (Eds.), 
  27. *    Springer, ..., pp. 549-560
  28. *
  29. *  The idea for the light buffer was taken from:
  30. *
  31. *    E. Haines and D. Greenberg, "The Light Buffer: A Shadow-Testing 
  32. *    Accelerator", IEEE CG&A, Vol. 6, No. 9, Sept. 1986, pp. 6-16
  33. *
  34. *****************************************************************************/
  35.  
  36. /****************************************************************************
  37. *
  38. *  ATTENTION!!!
  39. *
  40. *  THIS FILE HAS BEEN MODIFIED!!! IT IS NOT PART OF THE OFFICAL
  41. *  POV-RAY 2.2 DISTRIBUTION!!!
  42. *
  43. *  THIS FILE IS PART OF "FASTER THAN POV-RAY" (VERSION 1.1),
  44. *  A SPED-UP VERSION OF POV-RAY 2.2. USE AT YOUR OWN RISK!!!!!!
  45. *
  46. *  New files: addon0.c, addon1.c, addon2.c, addon3.c, addon.h
  47. *
  48. *  The additional modules were written by Dieter Bayer.
  49. *
  50. *  Send comments, suggestions, bugs, ideas ... to:
  51. *
  52. *  dieter@cip.e-technik.uni-erlangen.de
  53. *
  54. *  All changed/added lines are enclosed in #ifdef DB_CODE ... #endif
  55. *
  56. *  The vista projection was taken from:
  57. *
  58. *    A. Hashimoto, T. Akimoto, K. Mase, and Y. Suenaga, 
  59. *    "Vista Ray-Tracing: High Speed Ray Tracing Using Perspective
  60. *    Projection Image", New Advances in Computer Graphics, Proceedings
  61. *    of CG International '89, R. A. Earnshaw, B. Wyvill (Eds.), 
  62. *    Springer, ..., pp. 549-560
  63. *
  64. *  The idea for the light buffer was taken from:
  65. *
  66. *    E. Haines and D. Greenberg, "The Light Buffer: A Shadow-Testing 
  67. *    Accelerator", IEEE CG&A, Vol. 6, No. 9, Sept. 1986, pp. 6-16
  68. *
  69. *  The new anti-aliasing method was taken from:
  70. *
  71. *    G. Wyvill and P. Sharp, "Fast Antialiasing of Ray Traced Images", 
  72. *    New Advances in Computer Graphics, Proceedings of CG International '89, 
  73. *    R. A. Earnshaw, B. Wyvill (Eds.), Springer, ..., pp. 579-588
  74. *
  75. *****************************************************************************/
  76.  
  77. /****************************************************************************
  78. *                   ibm.c
  79. *
  80. *  This module implements the IBM-PC specific routines for POV-Ray.
  81. *
  82. *  from Persistence of Vision Raytracer
  83. *  Copyright 1993 Persistence of Vision Team
  84. *---------------------------------------------------------------------------
  85. *  NOTICE: This source code file is provided so that users may experiment
  86. *  with enhancements to POV-Ray and to port the software to platforms other 
  87. *  than those supported by the POV-Ray Team.  There are strict rules under
  88. *  which you are permitted to use this file.  The rules are in the file
  89. *  named POVLEGAL.DOC which should be distributed with this file. If 
  90. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  91. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  92. *  Forum.  The latest version of POV-Ray may be found there as well.
  93. *
  94. * This program is based on the popular DKB raytracer version 2.12.
  95. * DKBTrace was originally written by David K. Buck.
  96. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  97. *
  98. *****************************************************************************/
  99.  
  100. /* Original IBM VGA "colour" output routines for MS/DOS by Aaron A. Collins.
  101.  
  102.    This will deliver approximate colorings using HSV values for the selection.
  103.    The palette map is divided into 4 parts - upper and lower half generated
  104.    with full and half "value" (intensity), respectively.  These halves are
  105.    further halved by full and half saturation values of each range (pastels).
  106.    There are three constant colors, black, white, and grey.  They are used
  107.    when the saturation is low enough that the hue becomes undefined, and which
  108.    one is selected is based on a simple range map of "value".  Usage of the
  109.    palette is accomplished by converting the requested color RGB into an HSV
  110.    value.  If the saturation is too low (< .25) then black, white or grey is
  111.    selected.  If there is enough saturation to consider looking at the hue,
  112.    then the hue range of 1-63 is scaled into one of the 4 palette quadrants
  113.    based on its "value" and "saturation" characteristics.
  114.  
  115.    Further SVGA, MVGA mods by Aaron A. Collins:
  116.    SVGA, MVGA assembler routines originally by John Bridges.
  117.    VESA assembler routines from FRACTINT, by The Stone Soup Group
  118.    AT&T VDC600 SVGA mods to DKB Trace 2.01 module IBM.C by John Gooding
  119.  
  120.    This file now represents the collective wisdom of the VGAKIT34 package,
  121.    with support for all of the SVGA types known to mankind.  Seriously,
  122.    VGAKIT34 is an excellent technical reference for IBM programmers wishing
  123.    to do any sort of SVGA video access, and it encompasses nearly all of the
  124.    SVGA adapters in use today.  It was written by John Bridges, a.k.a.
  125.    CIS:73307,606, GENIE:J.BRIDGES.  It was originally coded in IBM 80x86
  126.    assembler, and since DKBTrace is a completely "C"-based package, I have
  127.    converted John's assembler routines all into "C".  These may be a tad bit
  128.    slower, but they should be compatible across a wide variety of 80x86/(S)VGA
  129.    machines.  Note if you have a regular cheapo VGA card like myself, included
  130.    is "MODE13x" or MVGA (modified VGA) mode (some call it "tweaked", but I
  131.    call it "Simulated SVGA"), which gives 360x480 on any reasonably register-
  132.    compatible plain vanilla VGA card.  This mode gives a good simulated 640 by
  133.    480 screen resolution.  I did not implement all the neat hi-res modes of
  134.    all the various SVGA adapters, if you select a trace size bigger than the
  135.    program and/or card can handle (most likely 640x480), it is dynamically
  136.    scaled to fit the available resolution, so you'll be able to see a rough
  137.    approximation of an 800x600 trace even on any el-cheapo VGA card at 320x200
  138.    resolution.  The VESA VGA mode was freely adapted from FRACTINT, whose GIF
  139.    reading routines we are already using in DKBTrace.  I hope my conversion
  140.    of it works properly.
  141.  
  142.    There is still a reported problem with the EVEREX autodetect returning
  143.    TRIDENT.  In fact EVEREX uses a TRIDENT chip set, but apparently there
  144.    is some difference in operation.  There are cryptic diagnostic messages
  145.    such as T0000, etc. printed as a result of the autodetection routines
  146.    to help track down why the error is happening.  If you are experiencing
  147.    problems with EVEREX or TRIDENT, make note of the letter-4 digit code you
  148.    are given.  There is now an autodetect for VDC600 that I hope will work
  149.    universally.  A similar problem as the EVEREX exists, in that the VDC600
  150.    is detected as a PARADISE because it uses the PARADISE chip set.  I am now
  151.    looking for what I believe to be the model number in the BIOS ROM of the
  152.    VDC600 to differentiate between the two.  I hope this works  with all
  153.    VDC600's, as I only had one example to work from.  Please send all bug
  154.    reports to Aaron Collins at the "You Can Call Me RAY" BBS, the number is
  155.    in the authors.txt document.
  156.  
  157.  
  158.    POV-Ray Enhancements:
  159.  
  160.    (S)VGA B&W 64 Greyscale code courtesy of Grant Hess 6/21/91
  161.  
  162.    16/24-bit HGWS/TIGA/TRUECOLOR code courtesy of Jay S. Curtis 11/15/90
  163.  
  164.    VGA "332" palette routines courtesy of Alexander Enzmann 7/20/91
  165.  
  166.    Additional support for VESA and Tseng Labs 256 and 32768 color modes by
  167.    Charles Marslett  (CIS: 75300,1636).  Resolution is selected to be the
  168.    lowest possible and still fit the image entirely on the screen. 4/28/91
  169.  
  170.    ATI VGA Wonder XL Support for 32,768 color mode and all other SVGA modes
  171.    by Randy Antler.  Also image centering, outline box, and forces SVGA mode
  172.    for small images.  Minor bug fixes and cleanups as well. 9/11/91
  173.  
  174.    Combined IBMPRO.C and IBM.C into 1 file.
  175.    B.Pulver 10/20/91
  176.  
  177.    Corrected defects in the centering and high color support for VESA modes
  178.    (particularly necessary for the newer 24-bit color cards and S3 based
  179.    Windows accellerator cards) -- Charles Marslett, 12/8/91 to 4/25/92.
  180.  
  181.    Extended Doug's dithering code to other high color modes and eliminated
  182.    a bad validity check in the 24-bit code high color cards.  Had to add
  183.    an "#include <float.h>" to ibmconf.h for the WATCOM C compilers with the
  184.    patchlevel C include files -- Charles Marslett, 10/4/92.
  185.  
  186.    Added support for Diamond Speedstar 24X. Uses a modified copy of some
  187.    of the Paradise code. There is no AutoDetect functionality for this
  188.    card, though.  Note that a couple of pixels (about 14), namely those
  189.    that straddle bank boundaries will be displayed incorrectly.
  190.                                  -- Lutz Kretzschmar,  10/30/92
  191.  
  192.    Fixed (obvious) bug for displaying images larger than screen
  193.    resolutions. Strange no-one caught it, it didn't work!. The line coord
  194.    cache is set to the max value by the box() function. Added code to
  195.    reset the values inside box().
  196.    Added bank switch checking inside pixels for truecolor modes.
  197.    Added HiColor display in 640x480 and 800x600 resolutions for Diamond
  198.    SpeedStar 24X.          -- Lutz Kretzschmar,  11/05/92
  199.  
  200.    Added TARGA+ display adapter support and reworked find_go32() to make
  201.    the VESA and TARGA routines work properly with DJ's port of IBMGCC
  202.    - Aaron A. Collins, 2/2/93.
  203.  
  204.    Completed Intel Code Builder code for VESA 1.2 support. - B.Pulver 2/15/93
  205.    Added: Set palette to P_332 if Hi/True color VESA init fails.
  206.           Doubled dithering matrixes to support 2048Xxxxx images.
  207.           Added code to disable dithering @ rez over 2048Xxxxx.
  208.           Added message warning user of failure if no_valid_mode is called
  209.           during VESA initialization attempt. - B.Pulver 5/13/93
  210.  
  211.    Found consistency bug in previously fixed find_go32(), so this too
  212.    has been fixed.  Found DJGCC's setvbuf() was blowing up the VESA/TARGA
  213.    "answer" area (in low real mode accessable mem) after a disk write, so
  214.    the setvbuf() function has been dummied up in this file to do nothing.
  215.    Added some DEBUG_VIDEO diagnostics for figuring out problems running a
  216.    TARGA+ display on the Intel Code Builder. - Aaron A. Collins, 8/2/1993
  217.  
  218. */
  219.  
  220. #define DITHERED        /* Defined to test Doug Muir's dithering code */
  221.  
  222. /*#define DEBUG_VIDEO*/        /* Uncomment to display Video initialization info during program startup. */
  223. /*#define SVBOK*/            /* Uncomment to use setvbuf in GCCDOS anyway. */
  224.  
  225. #ifdef DEBUG_VIDEO
  226. int diaged;                /* excessive diagnostic message clamp */
  227. #endif
  228.  
  229. #include <dos.h>          /* MS-DOS specific - for int86() REGS struct, etc. */
  230. #include <stdarg.h>
  231. #include <time.h>
  232. #include "frame.h"
  233. #include "povproto.h"
  234.  
  235. #if !defined(DOS386) && !defined(_INTELC32_) && !defined(__386__) && !defined(GCCDOS)
  236.   #include "tiga.h"       /* HGWS/TIGA function library include files - not supported by 32 bit compilers */
  237.   #include "tigadefs.h"
  238.   #include "extend.h"
  239. #endif
  240.  
  241. #include <io.h>           /* for low-level open() funct, etc. */
  242. #include <fcntl.h>        /* for low-level open() modes O_RDWR, O_BINARY, etc. */
  243.  
  244. /* Compiler specific setups & defines. */
  245. #ifdef __TURBOC__
  246.   extern unsigned _stklen = 12288; /* fairly large stack for HEAVY recursion */
  247.   #define CLD asm cld
  248.   #define _asm asm
  249.   #define _far far
  250. #else
  251. #ifdef __WATCOMC__
  252.   #include <float.h>
  253.   #define CLD clear_direction()
  254.   unsigned short __8087cw = IC_AFFINE | RC_NEAR | PC_64  | 0x007F;
  255.   void clear_direction(void);
  256.   #pragma aux clear_direction = 0xFC parm [] modify nomemory;
  257. #ifdef __386__
  258.   #define ax eax
  259.   #define bx ebx
  260.   #define cx ecx
  261.   #define dx edx
  262.   #define di edi
  263.   #define int86 int386
  264.   #define int86x int386x
  265. #endif
  266. #else
  267.  
  268. #ifdef _INTELC32_
  269.   #pragma interrupt (int10)   /* Need to install user INT10 handler for VESA. */
  270.   /* Use MSC 6.0a compatible mode. NOANSI switch in Makefile */
  271.   #include <stk.h>            /* Stack support for user INT10. */
  272.   #define CLD cld()
  273.  
  274.   void int10();
  275.   void (*prev_int10)();
  276.  
  277.   void cld (void)             /* Clear direction flag. */
  278.   {
  279.     unsigned int flags;
  280.     flags = _getflags();
  281.     flags = flags & (!_FLAG_DIRECTION);
  282.     _setflags (flags);
  283.   }
  284. #else
  285. #ifdef __ZTC__
  286.   #include <math.h>
  287.   #include <int.h>
  288.  
  289.   #ifndef DOS386
  290.   #ifndef DOS16RM
  291.   unsigned _stack = 12288;
  292.   #endif
  293.   #endif
  294.  
  295.   #define CLD
  296.   #ifndef min
  297.   #define min(x,y) ((x) < (y)? (x): (y))
  298.   #endif
  299.  
  300.   #ifdef DOS386
  301.     extern unsigned short _x386_zero_base_selector;
  302.     #define _disable()
  303.     #define _enable()
  304.     #define _cdecl
  305.   #else
  306.   #ifdef DOS16RM
  307.     void *D16SegAbsolute(long absadr, short size);
  308.     void D16SegCancel(void *segptr);
  309.     #define _disable()
  310.     #define _enable()
  311.     unsigned char *display_base;
  312.   #else
  313.     #define _disable() int_off()
  314.     #define _enable()  int_on()
  315.   #endif
  316.   #endif
  317.  
  318. #else
  319. #ifdef GCCDOS
  320.   #include <errno.h>
  321.   #include <pc.h>
  322.   #include <ctype.h>
  323.  
  324.   unsigned char *find_go32(int *go32_offset);
  325.  
  326.   #define _cdecl
  327.   #define _far
  328.   #define CLD
  329. #else
  330.   #define CLD _asm cld
  331. #endif
  332. #endif
  333. #endif
  334. #endif
  335. #endif
  336.  
  337. #ifdef __WATCOMC__
  338.   int matherr(struct exception *);     /* Math error traps. */
  339. #else
  340. #ifdef GCCDOS
  341.   int matherr(struct libm_exception *);
  342. #else
  343.   int _cdecl matherr(struct exception *);
  344. #endif
  345. #endif
  346.  
  347.  
  348. #ifdef DITHERED
  349. signed short r_err[2][2050];          /* Allow up to 2048x??? resolutions. */
  350. signed short g_err[2][2050];
  351. signed short b_err[2][2050];
  352. #endif
  353.  
  354. /* The supported VGA adapter types 1 - 9, A - Z.  0 is for auto-detect. */
  355.  
  356. #define BASIC_VGA       1               /* 1 - Tested: AAC */
  357. #define MODE13x         2               /* 2 - Tested: AAC */
  358. #define TSENG3          3               /* 3 - Tested: William Minus */
  359. #define TSENG4          4               /* 4 - Tested: William Minus */
  360. #define VDC600          5               /* 5 - Tested: John Gooding */
  361. #define OAKTECH         6               /* 6 - Untested */
  362. #define VIDEO7          7               /* 7 - Untested */
  363. #define CIRRUS          8               /* 8 - Tested: AAC */
  364. #define PARADISE        9               /* 9 - Tested: John Degner */
  365. #define AHEADA          17              /* A - Untested */
  366. #define AHEADB          18              /* B - Untested */
  367. #define CHIPSTECH       19              /* C - Untested */
  368. #define ATIVGA          20              /* D - Tested: William Earl */
  369. #define EVEREX          21              /* E - Tested: A+B problem - Larry Minton */
  370. #define TRIDENT         22              /* F - Tested: A problem - Alexander Enzmann */
  371. #define VESA            23              /* G - Tested: Charles Marslett/AAC/BP */
  372. #define ATIXL           24              /* H - Tested: Randy Antler */
  373. #define PARADISE24X     25              /* I - Tested: Lutz Kretzschmar */
  374. /* J - N -- 5 more reserved SVGA adapter types */
  375. #define TRUECOLOR       31              /* >= O isn't an (S)VGA - note this is a "marker" display type only!!! */
  376. #define TIGA_16         31              /* O - Tested: Jay S. Curtis */
  377. #define TIGA_24         32              /* P - Tested: Jay S. Curtis */
  378. #define TRUE_640        33              /* Q - 640 x 480 truecolor Tested: Jay S. Curtis */
  379. #define TRUE_800        34              /* R - 800 x 600 truecolor Tested: Jay S. Curtis */
  380. #define TRUE_1024       35              /* S - 1024 x 768 truecolor Tested: Jay S. Curtis */
  381. #define TARGA           36              /* T - TARGA+ in AOM Tested: Aaron A. Collins */
  382. /* U - Z -- 6 more reserved Non-SVGA (True-Color) adapter types */
  383.  
  384.  
  385. #define MISCOUT         0x3c2           /* VGA chip msic output reg. addr */
  386. #define SEQUENCER       0x3c4           /* VGA chip sequencer register addr */
  387. #define CRTC            0x3d4           /* VGA chip crt controller reg addr */
  388.  
  389.  
  390. char *vga_names[] =
  391.     {                                   /* POV-Ray command line option: */
  392.     "",                                 /* '0' is autodetect */
  393.     "Standard VGA",                     /*  1  */
  394.     "Simulated SVGA",                   /*  2  */
  395.     "Tseng Labs 3000 SVGA",             /*  3  */
  396.     "Tseng Labs 4000 SVGA",             /*  4  */
  397.     "AT&T VDC600 SVGA",                 /*  5  */
  398.     "Oak Technologies SVGA",            /*  6  */
  399.     "Video 7 SVGA",                     /*  7  */
  400.     "Video 7 Vega (Cirrus) VGA",        /*  8  */
  401.     "Paradise SVGA",                    /*  9  */
  402.     "",                                 /* misc ASCII */
  403.     "",
  404.     "",
  405.     "", /* reserved */
  406.     "",
  407.     "",
  408.     "",
  409.     "Ahead Systems Ver. A SVGA",        /*  A  */
  410.     "Ahead Systems Ver. B SVGA",        /*  B  */
  411.     "Chips & Technologies SVGA",        /*  C  */
  412.     "ATI SVGA",                         /*  D  */
  413.     "Everex SVGA",                      /*  E  */
  414.     "Trident SVGA",                     /*  F  */
  415.     "VESA Standard SVGA",               /*  G  */
  416.     "ATI VGA Wonder XL 32K Color SVGA", /*  H  */
  417.     "Diamond SpeedStar 24X SVGA",       /*  I  */
  418.     "",
  419.     "", /* spare SVGAs */
  420.     "",
  421.     "",
  422.     "",
  423.     "Hercules GWS/TIGA 16-bit",          /*  O   Here on is reserved for non-SVGA cards */
  424.     "Hercules GWS/TIGA 24-bit",          /*  P   TIGA is unsupported by the 32 bit compilers */
  425.     "TrueColor 640 x 480",               /*  Q  */
  426.     "TrueColor 800 x 600",               /*  R  */
  427.     "TrueColor 1024 x 768",              /*  S  */
  428.     "Truevision TARGA Plus"              /*  T  */
  429.     };
  430.  
  431.  
  432. unsigned int vptbl[] =          /* CRTC register values for MODE13x */
  433.     {
  434.     0x6b00,     /* horz total */
  435.     0x5901,     /* horz displayed */
  436.     0x5a02,     /* start horz blanking */
  437.     0x8e03U,    /* end horz blanking */
  438.     0x5e04,     /* start h sync */
  439.     0x8a05U,    /* end h sync */
  440.     0x0d06,     /* vertical total */
  441.     0x3e07,     /* overflow */
  442.     0x4009,     /* cell height */
  443.     0xea10U,    /* v sync start */
  444.     0xac11U,    /* v sync end and protect cr0-cr7 */
  445.     0xdf12U,    /* vertical displayed */
  446.     0x2d13,     /* offset */
  447.     0x0014,     /* turn off dword mode */
  448.     0xe715U,    /* v blank start */
  449.     0x0616,     /* v blank end */
  450.     0xe317U     /* turn on byte mode */
  451. };
  452.  
  453. unsigned short screen_height, screen_width;
  454. unsigned short svga_width = 640;           /* width of a scan line on the screen */
  455. unsigned short svga_height = 480;          /* number of scan lines on the screen */
  456. unsigned short svga_yincr = 640;           /* width of a scan line in video memory */
  457. int lastx, lasty, lastline;                /* Pixel / Line Caches */
  458. unsigned int x_off = 0;                    /* Offsets for image centering option */
  459. unsigned int y_off = 0;
  460. int whichvga = BASIC_VGA;                  /* BASIC_VGA mode by default */
  461. int vga_512K = FALSE;                      /* Flag for whether or not >= 512K VGA mem */
  462. int bpp = 8;                               /* Number of bits per pixel */
  463. unsigned char cur_page = 255;              /* SVGA current page (bank) number */
  464. unsigned int map_code = 0;                 /* Default map code is 0 */
  465. unsigned long gran = 65536L;               /* SVGA granule size (64K by default) */
  466. #ifdef _INTELC32_
  467. unsigned char *answer;                     /* Code Builder answer area for VESA BIOS calls */
  468. #else
  469. #ifdef DOS16RM
  470. unsigned char *answer;                     /* ZTC 16 answer area for VESA BIOS calls */
  471. static char save[260];
  472. unsigned long tl;
  473. #else
  474. #ifdef GCCDOS
  475. volatile unsigned char *answer;            /* GCC answer area for VESA BIOS calls */
  476. int di_val, c_val, t_val, ds_val;
  477. #else
  478. #ifdef __WATCOMC__
  479. #ifdef __386__
  480. unsigned int vesabuffer;                   /* Save area for the DOS segment pointer */
  481. unsigned char *answer;                     /* Watcom 386 answer area for VESA BIOS calls */
  482. #else
  483. unsigned char answer[260];                 /* Answer area for VESA BIOS calls */
  484. #endif
  485. #else
  486. unsigned char answer[260];                 /* Answer area for VESA BIOS calls */
  487. #endif
  488. #endif
  489. #endif
  490. #endif
  491. unsigned short vesamode;                   /* Current VESA BIOS supported mode */
  492.  
  493. #if !defined(DOS386) && !defined(_INTELC32_) && !defined(__386__) && !defined(GCCDOS)
  494. int dither_matrix[4][4]=
  495.         {
  496.          {0,8,2,10},                       /* HGWS/TIGA dither matrix */
  497.          {12,4,14,6},                      /* this may not be needed? */
  498.          {3,11,1,9},
  499.          {15,7,13,5}
  500.         };
  501. char hpt[3];                               /* HWGS/TIGA information passing area */
  502. char _far *hptr=hpt;                       /* HWGS/TIGA information passing area ptr */
  503. CONFIG config;
  504. #endif
  505.  
  506. DBL Height_Adjust = 1.0;                   /* Fudge factors for SVGA scaling */
  507. DBL Width_Adjust = 1.0;
  508.  
  509. unsigned short pixel_len;
  510. unsigned short red_ndx, blue_ndx, green_ndx;
  511.  
  512. /* VESA VBE standard mode numbers */
  513.  
  514. struct {
  515.    unsigned short modenum;
  516.    unsigned short width;
  517.    unsigned short height;
  518.    unsigned short bpp;
  519.    unsigned short gsize;
  520.    } modelist[] = {{0x100, 640, 400, 8, 0}, {0x101, 640, 480, 8, 0},
  521.                    {0x103, 800, 600, 8, 0}, {0x105, 1024, 768, 8, 0},
  522.                    {0x107, 1280, 1024, 8, 0}, {0x10D, 320, 200, 16, 5},
  523.                    {0x10E, 320, 200, 16, 6}, {0x10F, 320, 200, 24, 8},
  524.                    {0x110, 640, 480, 16, 5}, {0x111, 640, 480, 16, 6},
  525.                    {0x112, 640, 480, 24, 8}, {0x113, 800, 600, 16, 5},
  526.                    {0x114, 800, 600, 16, 6}, {0x115, 800, 600, 24, 8},
  527.                    {0x116, 1024, 768, 16, 5}, {0x117, 1024, 768, 16, 6},
  528.                    {0x118, 1024, 768, 24, 8}, {0x119, 1280, 1024, 16, 5},
  529.                    {0x11A, 1280, 1024, 16, 6}, {0x11B, 1280, 1024, 24, 8}};
  530.  
  531. unsigned int bitmask[] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F,
  532.       0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF,
  533.       0x7FFF, 0xFFFF};
  534.  
  535. int r_shift, g_shift, b_shift;
  536. unsigned int r_mask, g_mask, b_mask;
  537.  
  538.  
  539. extern unsigned int Options;
  540. extern char DisplayFormat, PaletteOption, Color_Bits;
  541. extern int First_Line, Last_Line;
  542. extern int First_Column, Last_Column;
  543.  
  544.  
  545. int AutodetectVGA(void);
  546. int cirrus(void);
  547. int chkbank(unsigned int, unsigned int);
  548. void palette_init(void);
  549. void newbank(void);
  550. void set_palette_register(unsigned, unsigned, unsigned, unsigned);
  551. void hsv_to_rgb(DBL, DBL, DBL, unsigned *, unsigned *, unsigned *);
  552. void rgb_to_hsv(unsigned, unsigned, unsigned, DBL *, DBL *, DBL *);
  553. void atiplot(int, int, int, int, int);
  554. void box(int, int, int, int);
  555. int _cdecl box_fprintf( FILE *stream, char *format,...);
  556.  
  557. #ifdef DOS386
  558. void get_real_buf(short *real_buf, unsigned char _far **ptr);
  559.  
  560. static short real_buf[2];
  561. static unsigned char _far *real_ptr;
  562. #endif
  563.  
  564. /*
  565.     Note: for non-32-bit compilers, we can have a long in the struct and
  566.     still get word-aligned packing.  For 32-bit compilers, we want to use
  567.     all shorts so we don't force longword alignment of the members, which
  568.     can create gaps in the structure due to the member ordering within.
  569. */
  570.  
  571. typedef struct  /* TARGA+ I/O Command Array */
  572. {
  573.     unsigned short      IOCmd;      /*  Command Type        */
  574.     short               IOxi;       /*  Initial x position  */
  575.     short               IOxf;       /*  Final x position    */
  576.     short               IOyi;       /*  Initial y position  */
  577.     short               IOyf;       /*  Final y position    */
  578.     short               IOxd;       /*  Destination X       */
  579.     short               IOyd;       /*  Destination Y       */
  580. #if !defined(DOS16RM) && !defined(DOS386) && !defined(_INTELC32_) && !defined (__386__) && !defined(GCCDOS)
  581.     unsigned long       IOcolor;    /*  Color value         */
  582. #else
  583.     unsigned short      IOcolorLo;  /*  Color value Low Order */
  584.     unsigned short      IOcolorHi;  /*  Color value High Order */
  585. #endif
  586.     unsigned short      IORegsOff;  /*  Structure Buffer Ptr Offset*/
  587.     unsigned short      IORegsSeg;  /*  Structure Buffer Ptr Segment*/
  588.     unsigned short      IORLstOff;  /*  Register List Offset */
  589.     unsigned short      IORLstSeg;  /*  Register List Segment*/
  590.     unsigned short      IOBoardNum; /*  Active Board Number  */
  591.     unsigned short      IOStrucSiz; /*  Structure Size      */
  592. } TGACommand;
  593.  
  594. typedef TGACommand _far *TGACommandPtr;
  595. int TGAHandle;                  /* global TARGA+ file handle used by open() */
  596.  
  597. #ifdef GCCDOS
  598. volatile unsigned short *TGARegs;       /* global TARGA+ register array Pointer */
  599. volatile TGACommandPtr command;         /* TARGA+ Command Structure Pointer */
  600. #else
  601. unsigned short tregs[130];
  602. TGACommand tcmd;                        /* TARGA+ Command Structure */
  603. unsigned short *TGARegs = tregs;        /* global TARGA+ register array Pointer */
  604. TGACommandPtr command = &tcmd;          /* TARGA+ Command Structure Pointer */
  605. #endif
  606.  
  607. void SetTGARegs(int, int, union REGS*, struct SREGS*);
  608.  
  609. void display_init(width, height) /* Set video to requested or best mode */
  610.     int width, height;
  611.     {
  612.     union REGS inr, outr;
  613.     struct SREGS segs;
  614.     unsigned char _far *fp;
  615. #ifdef DOS386
  616.     unsigned int tmp_word;
  617. #else
  618.     unsigned short _far *mp;
  619. #endif
  620.     unsigned int u, granule, vesamodes[64];
  621.     int i, maxvesamode;
  622.     time_t l, lt;
  623.     int show_display_type = FALSE;
  624.  
  625.     if (DisplayFormat == '?') {
  626.         DisplayFormat = '0';
  627.         show_display_type = TRUE;
  628.     }
  629. #ifdef __WATCOMC__
  630. #ifdef __386__
  631.     inr.x.ax = 0x4800;
  632.     inr.x.bx = 17;
  633.     int86(0x21, &inr, &outr);
  634.     vesabuffer = outr.x.ax;
  635.     answer = MK_FP(outr.x.ax, 0);
  636. #endif
  637. #endif
  638. #ifdef DOS16RM
  639.     display_base = (unsigned char *)D16SegAbsolute(0xA0000, 0);
  640.     answer = (unsigned char *)D16SegAbsolute(0x00580, 0x110);
  641.     for (u=0; u < 257; u++)
  642.         save[u] = answer[u];
  643. #endif
  644. #ifdef GCCDOS
  645.     if (DisplayFormat == '0' || DisplayFormat == VESA + '0' || DisplayFormat >= TRUECOLOR + '0')
  646.          answer = find_go32(&di_val);
  647.     else answer = NULL;
  648.  
  649.     /* from the real mode offset (di_val) and the virtual addr of "answer", determine real DS segment value */
  650.     ds_val = (unsigned int)((((unsigned long)answer & 0x0FFFFFFF) - (unsigned long)di_val) >> 4);
  651.  
  652.     command = (TGACommandPtr)(answer + 0x800);
  653.     TGARegs = (unsigned short *)(answer + 0x880);
  654.     c_val = di_val + 0x800;
  655.     t_val = di_val + 0x880;
  656. #endif
  657. #ifdef _INTELC32_
  658.     prev_int10 = _dos_getvect ( 0x10 );     /* Save previous handler pointer */
  659.     _dos_setvect ( 0x80000010, int10 );     /* Install user Int 10h handler */
  660.     if (_dos_allocmem(17, &answer) != 0) {  /* defined at the end of this file. */
  661.         printf ("Memory allocation failure for VESA detect.\n");
  662.         exit(1);
  663.     }
  664. #endif
  665. #ifdef DOS386
  666.    get_real_buf(real_buf, &real_ptr);
  667. #endif
  668.  
  669.     lastline = -1;               /* make sure we display the 1st line... */
  670.     screen_height = height;      /* requested screen height and width */
  671.     screen_width = width;
  672.  
  673. #ifdef DITHERED
  674. if (screen_width <= 2048) {
  675.     for (i=0; (unsigned short)i < screen_width + 2; i++) {
  676.         r_err[0][i] = 0;
  677.         r_err[1][i] = 0;
  678.         g_err[0][i] = 0;
  679.         g_err[1][i] = 0;
  680.         b_err[0][i] = 0;
  681.         b_err[1][i] = 0;
  682.     }
  683. }
  684. #endif
  685.  
  686.     if (DisplayFormat != '0')                   /* if not 0, some display type specified */
  687.         whichvga = (int)(DisplayFormat - '0');  /* de-ASCII-fy selection */
  688.     else {
  689.         whichvga = AutodetectVGA();
  690.         lt = l = time(&l);
  691.         if (show_display_type) {   /* If display format is ? */
  692.             printf("Display detected: (%c) %s Adapter", whichvga + '0', vga_names[whichvga]);
  693.             if (whichvga < TRUECOLOR)   /*  if not a True-Color graphics card */
  694.                 printf(", with %s 512K RAM\n", vga_512K ? ">=" : "<");
  695.             else
  696.                 printf("\nPausing...\n");
  697.             while (time(&l) < lt + 5);   /* display detected VGA type for 5 secs */
  698.                       }
  699.         if (whichvga < TRUECOLOR && !vga_512K)  /* not enough RAM for 640 x 480? */
  700.             whichvga = MODE13x;         /* then try for next best mode... */
  701.     }
  702.  
  703.     if (whichvga < TRUECOLOR) { /* If an (S)VGA (not True-Color graphics card) */
  704.         if (whichvga == CIRRUS) /* Register Compatible VGA? */
  705.             whichvga = MODE13x; /* MODE13x if > 320x200, else... */
  706.  
  707.         if (screen_height <= 200 && screen_width <= 320 &&
  708.                 PaletteOption != HICOLOR && PaletteOption != FULLCOLOR)
  709.             whichvga = BASIC_VGA;
  710.  
  711.         if (whichvga == TSENG4 && PaletteOption == HICOLOR) {
  712.             inr.x.ax = 0x10F1;          /* check and see if it's true... */
  713. #ifdef DOS386
  714.             int86_real(0x10, &inr, &inr);
  715. #else
  716.             int86(0x10, &inr, &inr);
  717. #endif
  718.             if ((int) inr.h.al != 0x10 || (int)inr.h.bl == 0) {
  719.                 printf("Error - High Color Palette Option Unavailable\n");
  720. #ifdef _INTELC32_
  721.                 _dos_setvect(0x10, prev_int10);   /* Restore previous handler */
  722.                 _dos_freemem((unsigned) answer);  /* Free DOS buffer */
  723. #endif
  724.                 exit(1);
  725.             }
  726.         }
  727.     }
  728.  
  729.     switch (whichvga) {
  730.         case MODE13x:
  731.             inr.x.ax = 0x0013;   /* Setup to VGA 360x480x256 (mode 13X) */
  732.             svga_width = 360;    /* Fake 640 mode actually is 360 */
  733.             svga_yincr = 90;
  734.             break;
  735.         case VDC600:
  736.             inr.x.ax = 0x005E;   /* Setup to VGA 640x400x256 (mode 5EH) */
  737.             svga_height = 400;   /* This is the only SVGA card w/400 Lines */
  738.             break;
  739.         case OAKTECH:
  740.             inr.x.ax = 0x0053;   /* Setup to VGA 640x480x256 most SVGAs */
  741.             break;
  742.         case AHEADA:
  743.         case AHEADB:
  744.             inr.x.ax = 0x0061;
  745.             break;
  746.         case EVEREX:
  747.             inr.x.ax = 0x0070;   /* BIOS Mode 0x16 for EV-678? */
  748.             inr.h.bl = 0x30;
  749.             break;
  750.         case ATIVGA:
  751.             if (screen_width <= 1024 && screen_height <= 768) {
  752.                 inr.x.ax = 0x0064;
  753.                 svga_width = 1024;
  754.                 svga_height = 768;
  755.                 svga_yincr = 1024;
  756.             }
  757.             if (screen_width <= 800 && screen_height <= 600) {
  758.                 inr.x.ax = 0x0063;
  759.                 svga_width = 800;
  760.                 svga_height = 600;
  761.                 svga_yincr = 800;
  762.             }
  763.             if (screen_width <= 640 && screen_height <= 480) {
  764.                 inr.x.ax = 0x0062;
  765.                 svga_width = 640;
  766.                 svga_height = 480;
  767.                 svga_yincr = 640;
  768.             }
  769.             break;
  770.         case ATIXL:
  771.             inr.x.ax = 0x0072;
  772.             svga_width = 640;
  773.             svga_height = 480;
  774.             svga_yincr = 640;
  775.             break;
  776.         case TRIDENT:
  777.             inr.x.ax = 0x005d;
  778.             break;
  779.         case VIDEO7:
  780.             inr.x.ax = 0x6f05;
  781.             inr.h.bl = 0x67;
  782.             break;
  783.         case CHIPSTECH:
  784.             if (screen_height <= 400) {
  785.                 inr.x.ax = 0x0078;
  786.                 svga_height = 400;
  787.             }
  788.             else
  789.                 inr.x.ax = 0x0079;
  790.             break;
  791.         case PARADISE:
  792.             inr.x.ax = 0x005f;
  793.             break;
  794.         case PARADISE24X:
  795.             if (PaletteOption == HICOLOR)
  796.                  {
  797.                  if (screen_height <= 480 && screen_width <= 640) {
  798.                       inr.x.ax = 0x0062;
  799.                       svga_width = 640;
  800.                       svga_height = 480;
  801.                       svga_yincr = 640;
  802.                  }
  803.                  else {                     /* 800 by 600 and beyond */
  804.                       inr.x.ax = 0x0063;
  805.                       svga_width = 800;
  806.                       svga_height = 600;
  807.                       svga_yincr = 800;
  808.                  }
  809.                  r_mask = g_mask = b_mask = 0xF8;
  810.                  r_shift = 7;
  811.                  g_shift = 2;               /* These are a left-shifts */
  812.                  b_shift = 3;               /* This is a right-shift */
  813.                  svga_yincr <<= 1;          /* two bytes per pixel */
  814.             }
  815.             else                            /* Truecolor mode. Force 640x480 */
  816.             {
  817.               inr.x.ax = 0x0072;
  818.               svga_width = 640;
  819.               svga_height = 480;
  820.               svga_yincr = 640*3;           /* 3 bytes per pixel */
  821.               bpp = 24;
  822.               pixel_len = bpp/8;
  823.               red_ndx = 0;                  /* Order in Frame buffer is RGB */
  824.               green_ndx = 1;
  825.               blue_ndx = 2;
  826.             }
  827.             vga_512K = TRUE;                /* Always has 1MB */
  828.             break;
  829.         case TSENG3:
  830.         case TSENG4:
  831.             if (screen_height <= 200 && screen_width <= 320) {
  832.                  inr.x.ax = 0x0013;         /* setup to VGA 320x200 for 32K mode  */
  833.                  svga_width = 320;          /* allow scaling to run at 320x200 */
  834.                  svga_height = 200;
  835.                  svga_yincr = 320;
  836.             }
  837.             else if (screen_height <= 350 && screen_width <= 640) {
  838.                  inr.x.ax = 0x002D;
  839.                  svga_height = 350;
  840.             }
  841.             else if (screen_height <= 400 && screen_width <= 640 && whichvga == TSENG4) {
  842.                  inr.x.ax = 0x0078;
  843.                  svga_height = 400;
  844.             }
  845.             else if (screen_height <= 480 && screen_width <= 640) {
  846.                  inr.x.ax = 0x002E;
  847.             }
  848.             else {                          /* 800 by 600 and beyond */
  849.                  inr.x.ax = 0x0030;
  850.                  svga_width = 800;
  851.                  svga_height = 600;
  852.                  svga_yincr = 800;
  853.             }
  854.             if ((screen_height > 600 || screen_width > 800) && whichvga == TSENG4) {
  855.                 if (PaletteOption == HICOLOR) {
  856.                     inr.x.ax = 0x0030;      /* Limit to 800x600 in HiColor mode */
  857.                     svga_width = 800;
  858.                     svga_height = 600;
  859.                     svga_yincr = 800;
  860.                 }
  861.                 else {
  862.                     inr.x.ax = 0x0038;
  863.                     svga_width = 1024;
  864.                     svga_height = 768;
  865.                     svga_yincr = 1024;
  866.                 }
  867.             }
  868.             break;
  869.         case VESA:
  870.             inr.x.ax = 0x4F00;              /* Call function 0 to get VGA info */
  871. #ifdef _INTELC32_
  872.             for (u=0; u<256; u++)
  873.                 answer[u] = 0;
  874.             inr.w.edi = (unsigned)answer;
  875.             int86(0x10, &inr, &outr);       /* Get thru int 10h inr outr */
  876. #else
  877. #ifdef DOS386
  878.             for (u=0; u<256; u++)
  879.                 real_ptr[u] = 0;
  880.             inr.x.di = real_buf[0];         /* deposit results here */
  881.             segread(&segs);                 /* get our DS, etc. */
  882.             segs.es = real_buf[1];          /* get segment of answer */
  883.             int86x_real(0x10, &inr, &outr, &segs);
  884.             for(u=0; u<sizeof(answer); u++)
  885.                 answer[u] = real_ptr[u];
  886. #else
  887. #ifdef GCCDOS
  888.             for (u=0; u<256; u++)
  889.                 answer[u] = 0;
  890.             inr.x.di = di_val;              /* deposit results here */
  891.             int86x(0x10, &inr, &outr, &segs);
  892. #else
  893. #ifdef DOS16RM
  894.             for (u=0; u<256; u++)
  895.                 answer[u] = 0;
  896.             segread(&segs);                 /* get our DS, etc. */
  897.             inr.x.di = 0x580;               /* deposit results here */
  898.             segs.es = 0;                    /* get segment of answer */
  899.             int86x(0x10, &inr, &outr, &segs);
  900. #else
  901.             for (u=0; u<256; u++)
  902.                 answer[u] = 0;
  903.             inr.x.di = FP_OFF(answer);      /* deposit results here */
  904.             segread(&segs);                 /* get our DS, etc. */
  905.             segs.es = FP_SEG(answer);       /* get segment of answer */
  906.             int86x(0x10, &inr, &outr, &segs);
  907. #endif
  908. #endif
  909. #endif
  910. #endif
  911.             if ( outr.x.ax != 0x004F || answer[0] != 'V' ||
  912.                     answer[1] != 'E' || answer[2] != 'S' ||
  913.                     answer[3] != 'A' ) {          /* if response unsuccessful */
  914.                 printf("Error - VESA BIOS Extensions Not Available: code=%04X<%c%c%c%c>\n",
  915.                         outr.x.ax, answer[0],answer[1],answer[2],answer[3]);
  916. #ifdef DOS16RM
  917.                 for (u=0; u < 257; u++)
  918.                     answer[u] = save[u];
  919. #endif
  920. #ifdef _INTELC32_
  921.                 _dos_setvect(0x10, prev_int10);   /* Restore previous handler */
  922.                 _dos_freemem((unsigned) answer);  /* Free DOS buffer */
  923. #endif
  924.                 exit(1);
  925.                 }
  926. #ifdef _INTELC32_
  927.             mp = (char *)(*(unsigned short *)(answer + 14) +
  928.                          (((long)*(unsigned short *)(answer + 16)) << 4));
  929.             for (i = 0; i < 64 && mp[i] != 0xFFFF; i++)    /* for all modes available */
  930.                 vesamodes[i] = mp[i];                      /* copy to local mode list */
  931. #else
  932. #ifdef DOS386
  933.             fp = MK_FP(_x386_zero_base_selector,
  934.                 *(unsigned short *)(answer+14)  /* get mode tbl. pointer   */
  935.                 + (*(unsigned short *)(answer+16) << 4));
  936.             for (u=i=0; i < 64 && (tmp_word=fp[u]|(fp[u+1]<<8)) != 0xFFFF; i++, u += 2)/* for all modes available */
  937.                 vesamodes[i] = tmp_word;        /* copy to local mode list */
  938. #else
  939. #ifdef DOS16RM
  940.             tl = *(unsigned short *)(answer + 14) +
  941.                   (((long)*(unsigned short *)(answer + 16)) << 4);
  942.             mp = (unsigned short *)D16SegAbsolute(tl, 0);
  943.             for (i = 0; i < 64 && mp[i] != 0xFFFF; i++) /* for all modes available */
  944.                 vesamodes[i] = mp[i];                   /* copy to local mode list */
  945.             D16SegCancel((void *)mp);
  946. #else
  947.             mp = (unsigned short *)MK_FP(answer[16]+(answer[17]<<8),
  948.                        answer[14]+(answer[15]<<8));       /* get mode tbl. pointer */
  949.             for (i = 0; i < 64 && mp[i] != 0xFFFF; i++)   /* for all modes available */
  950.                 vesamodes[i] = mp[i];                     /* copy to local mode list */
  951. #endif
  952. #endif
  953. #endif
  954.  
  955. /* Get VESA modes attributes list. */
  956.             maxvesamode = i;                    /* note end of list */
  957.             bpp = (PaletteOption == HICOLOR)? 16: (PaletteOption == FULLCOLOR)? 32: 8;
  958.  
  959.             svga_width = 0;
  960.             svga_height = 0;
  961.             vesamode = 0;
  962.             for (i = 0; i < maxvesamode; i++) {
  963.                 inr.x.ax = 0x4F01;                  /* VESA BIOS fetch attributes call */
  964.                 inr.x.bx = inr.x.cx = vesamodes[i]; /* Get attrs for mode */
  965. #ifdef _INTELC32_
  966.                 inr.w.edi = (unsigned)answer;       /* Stash results here. */
  967.                 int86(0x10, &inr, &outr);
  968. #else
  969. #ifdef DOS386
  970.                 inr.x.di = real_buf[0];             /* Deposit results here */
  971.                 segread(&segs);                     /* Get our DS, etc. */
  972.                 segs.es = real_buf[1];              /* Get segment of answer */
  973.                 int86x_real(0x10, &inr, &outr, &segs);
  974.                 for(u=0; u<sizeof(answer); u++)
  975.                     answer[u] = real_ptr[u];
  976. #else
  977. #ifdef GCCDOS
  978.                 inr.x.di = di_val;                  /* deposit results here */
  979.                 int86x(0x10, &inr, &outr, &segs);   /* BIOS fetch attrib call */
  980. #else
  981. #ifdef DOS16RM
  982.                 segread(&segs);                     /* get our DS, etc. */
  983.                 inr.x.di = 0x580;                   /* deposit results here */
  984.                 segs.es = 0;
  985.                 int86x(0x10, &inr, &outr, &segs);   /* BIOS fetch attrib call */
  986. #else
  987.                 inr.x.di = FP_OFF(answer);          /* deposit attribs here */
  988.                 segread(&segs);                     /* get our DS, etc. */
  989.                 segs.es = FP_SEG(answer);           /* get segment of answer */
  990.                 int86x(0x10, &inr, &outr, &segs);   /* BIOS fetch attrib call */
  991. #endif
  992. #endif
  993. #endif
  994. #endif
  995.                 if ((answer[0] & 0x11) != 0x11)
  996.                     continue;
  997.                 if ((answer[0] & 0x02) == 0) {
  998. #ifdef DEBUG_VIDEO
  999.                     printf("VESA mode without full information found [%03X]\n",
  1000.                             inr.x.cx);
  1001. #endif
  1002.                     for (u=0; u < sizeof(modelist)/sizeof(modelist[0]); u++)
  1003.                         if (vesamodes[i] == modelist[u].modenum) {
  1004.                             *(unsigned short *)(answer+18) = modelist[u].width;
  1005.                             *(unsigned short *)(answer+20) = modelist[u].height;
  1006.                             answer[25] = (unsigned char)modelist[u].bpp;
  1007.                             answer[36] = 0;
  1008.                             if (answer[25] == 16) {
  1009.                                 answer[31] = 5;
  1010.                                 answer[32] = (unsigned char)(modelist[u].gsize + 5);
  1011.                                 answer[33] = (unsigned char)modelist[u].gsize;
  1012.                                 answer[34] = 5;
  1013.                                 answer[35] = 5;
  1014.                             }
  1015.                             else if (answer[25] == 24 || answer[25] == 32) {
  1016.                                 answer[31] = (unsigned char)modelist[u].gsize;
  1017.                                 answer[32] = (unsigned char)(modelist[u].gsize * 2);
  1018.                                 answer[33] = (unsigned char)modelist[u].gsize;
  1019.                                 answer[34] = (unsigned char)modelist[u].gsize;
  1020.                                 answer[35] = (unsigned char)modelist[u].gsize;
  1021.                             }
  1022.                             answer[0] |= 0x02;
  1023.                             break;
  1024.                         }
  1025.                 }
  1026.                 else if (answer[25] == 16 && answer[31] == 0 && answer[33] == 0 &&
  1027.                         answer[35] == 0) {
  1028. #ifdef DEBUG_VIDEO
  1029.                     printf("VESA 1.1 extended information found for mode %03X\n",
  1030.                             inr.x.cx);
  1031. #endif
  1032.                     answer[31] = 5;     /* VESA 1.1 Programming guideline */
  1033.                     answer[32] = 10;    /* default high color is 5-5-5 */
  1034.                     answer[33] = 5;
  1035.                     answer[34] = 5;
  1036.                     answer[35] = 5;
  1037.                 }
  1038.  
  1039.                 if ((answer[0] & 0x02) && ((int)answer[25] == bpp ||
  1040.                         (bpp == 32 && (int)answer[25] == 24) ||
  1041.                         (bpp == 16 && (int)answer[25] == 15))) {
  1042.                     if ((svga_height < screen_height || svga_width < screen_width) &&
  1043.                             *(unsigned short *)(answer+18) >= svga_width &&
  1044.                             *(unsigned short *)(answer+20) >= svga_height)
  1045.                         vesamode = vesamodes[i];
  1046.                     else if (svga_height >= *(unsigned short *)(answer+18) &&
  1047.                             svga_width >= *(unsigned short *)(answer+20) &&
  1048.                             *(unsigned short *)(answer+18) >= screen_width &&
  1049.                             *(unsigned short *)(answer+20) >= screen_height)
  1050.                         vesamode = vesamodes[i];
  1051.  
  1052.                     if (vesamode == vesamodes[i]) {
  1053. #ifdef DEBUG_VIDEO
  1054.                         printf("VESA mode selected: %03X\n", vesamodes[i]);
  1055. #endif
  1056.                         if ((answer[2] & 0x05) == 0x05)
  1057.                             map_code = 0x0000;                   /* Select A page */
  1058.                         else if ((answer[3] & 0x05) == 0x05)
  1059.                             map_code = 0x0001;                   /* Select B page */
  1060.                         bpp = answer[25];                        /* Handle 24 or 32 bpp */
  1061.                         svga_width = *(unsigned short *)(answer+18);
  1062.                         svga_height = *(unsigned short *)(answer+20);
  1063.                         r_shift = answer[32] + answer[31] - 8;
  1064.                         g_shift = answer[34] + answer[33] - 8;
  1065.                         b_shift = answer[36] + answer[35] - 8;
  1066.                         r_mask = 0xFF - bitmask[8 - answer[31]];
  1067.                         g_mask = 0xFF - bitmask[8 - answer[33]];
  1068.                         b_mask = 0xFF - bitmask[8 - answer[35]];
  1069.                         b_shift = - b_shift;
  1070.                         if (bpp == 16 && (b_shift < 0 || r_shift < 0 || g_shift < 0)) {
  1071.                             printf("Adapter not supported currently in high color modes!\n");
  1072.                             printf("Shifts required are RED:%d, GREEN:%d, BLUE:%d\n",
  1073.                                         r_shift, g_shift, b_shift);
  1074.                             printf("Bit masks: RED:%d, GREEN:%d, BLUE:%d\n",
  1075.                                         r_mask, g_mask, b_mask);
  1076.                             printf("Field positions: RED:%d, GREEN:%d, BLUE:%d\n",
  1077.                                         answer[36], answer[34], answer[32]);
  1078.                             printf("Field widths: RED:%d, GREEN:%d, BLUE:%d\n",
  1079.                                         answer[35], answer[33], answer[31]);
  1080.                             exit(1);
  1081.                         }
  1082.                     }
  1083.                 }
  1084.             }
  1085.  
  1086.             if (vesamode == 0){
  1087.                 lt = time(&l);
  1088.                 printf("\nSelected VESA Mode could not be initialized!!!\n");
  1089.                 printf("If you are trying to use HiColor or TrueColor modes they\n");
  1090.                 printf("may not be supported by your VESA BIOS.\n\n");
  1091.                 printf("Using standard 320x200x256 VGA for this run.\n");
  1092.                 printf("\nPausing 10 seconds...\n");
  1093.                 while (time(&l) < lt + 10);
  1094.                 goto no_valid_mode;
  1095.                 }
  1096.  
  1097.             inr.x.ax = 0x4F01;                  /* VESA BIOS fetch attributes call */
  1098.             inr.x.bx = inr.x.cx = vesamode;     /* Get attrs for mode */
  1099. #ifdef _INTELC32_
  1100.             inr.w.edi = (unsigned)answer;       /* Stash results here. */
  1101.             int86(0x10, &inr, &outr);           /* Get thru INT 10     */
  1102. #else
  1103. #ifdef GCCDOS
  1104.             inr.x.di = di_val;                  /* Deposit results here */
  1105.             int86x(0x10, &inr, &outr, &segs);
  1106. #else
  1107. #ifdef DOS386
  1108.             inr.x.di = real_buf[0];             /* Deposit results here */
  1109.             segread(&segs);                     /* Get our DS, etc. */
  1110.             segs.es = real_buf[1];              /* Get segment of answer */
  1111.             int86x_real(0x10, &inr, &outr, &segs);
  1112.             for(u=0; u<sizeof(answer); u++)
  1113.                 answer[u] = real_ptr[u];
  1114. #else
  1115. #ifdef DOS16RM
  1116.             segread(&segs);                     /* Get our DS, etc. */
  1117.             inr.x.di = 0x580;                   /* Deposit results here */
  1118.             segs.es = 0;
  1119.             int86x(0x10, &inr, &outr, &segs);
  1120. #else
  1121.             inr.x.di = FP_OFF(answer);          /* Deposit attribs here */
  1122.             segread(&segs);                     /* Get our DS, etc. */
  1123.             segs.es = FP_SEG(answer);           /* Get segment of answer */
  1124.             int86x(0x10, &inr, &outr, &segs);   /* BIOS fetch attrib call */
  1125. #endif
  1126. #endif
  1127. #endif
  1128. #endif
  1129.             if (outr.x.ax != 0x004F){
  1130.                lt = time(&l);
  1131.                 printf("\nSelected VESA Mode could not be initialized!!!\n");
  1132.                 printf("If you are trying to use HiColor or TrueColor modes they\n");
  1133.                 printf("may not be supported by your VESA BIOS.\n\n");
  1134.                 printf("Using standard 320x200x256 VGA for this run.\n");
  1135.                 printf("\nPausing 10 seconds...\n");
  1136.                 while (time(&l) < lt + 10);
  1137.                 goto no_valid_mode;
  1138.             }
  1139.             svga_yincr = *(unsigned short *)(answer+16);
  1140.             granule = *(unsigned short *)(answer + 4); /* "granule" size */
  1141.             if (granule < 1)
  1142.                 granule = 1;
  1143.             gran = 1024L * granule;
  1144.  
  1145.             inr.x.ax = 0x4F02;                  /* VESA BIOS initialize video mode call */
  1146.             if (svga_yincr == svga_width && PaletteOption == HICOLOR)
  1147.                 if (svga_width == 640 && svga_height == 200) {
  1148.                     svga_width = 320;
  1149.                     svga_yincr = 1024;
  1150.                 }
  1151.                 else {
  1152.                     svga_yincr = svga_width * 2;
  1153.                 }
  1154.             if (PaletteOption == FULLCOLOR) {
  1155.                 if (r_shift == 0 && g_shift == 0 && b_shift == 0) {
  1156.                     r_shift = 16;
  1157.                     g_shift = 8;
  1158.                     b_shift = 0;
  1159.                 }
  1160.                 if (r_mask != 0xFF || g_mask != 0xFF || b_mask != 0xFF ||
  1161.                         (r_shift & 0x07) || (g_shift & 0x07) || (b_shift & 0x07)) {
  1162.                     printf("Adapter not supported currently in full color modes!\n");
  1163.                     printf("Shifts required are RED:%d, GREEN:%d, BLUE:%d\n",
  1164.                                 r_shift, g_shift, b_shift);
  1165.                     printf("Bit masks: RED:0x%02x, GREEN:0x%02x, BLUE:0x%02x\n",
  1166.                                 r_mask, g_mask, b_mask);
  1167.                     printf("Field positions: RED:%d, GREEN:%d, BLUE:%d\n",
  1168.                                 answer[36], answer[34], answer[32]);
  1169.                     printf("Field widths: RED:%d, GREEN:%d, BLUE:%d\n",
  1170.                                 answer[35], answer[33], answer[31]);
  1171.                     exit(1);
  1172.                 }
  1173.                 red_ndx = r_shift >> 3;
  1174.                 green_ndx = g_shift >> 3;
  1175.                 blue_ndx = (-b_shift) >> 3;
  1176.                 pixel_len = bpp/8;
  1177. #ifdef DEBUG_VIDEO
  1178.                 printf("Byte offsets: R=%d, G=%d, B=%d\n", red_ndx, green_ndx,
  1179.                         blue_ndx);
  1180. #endif
  1181.             }
  1182.             break;
  1183.  
  1184. #if !defined(DOS386) && !defined(_INTELC32_) && !defined(__386__) && !defined(GCCDOS)
  1185.         case TIGA_16:                   /* Set up TIGA modes. Real mode compilers only. */
  1186.         case TIGA_24:
  1187.         case TRUE_640:
  1188.         case TRUE_800:
  1189.         case TRUE_1024:
  1190.             if ((int)(DisplayFormat - '0') == whichvga)    /* not autodetected */
  1191.                 if (tiga_set(CD_OPEN) < 0)                 /* Attempt to open the TIGA CD */
  1192.                     {
  1193.                     printf("Error - TIGA CD Not Installed.");
  1194.                     goto TIGA_error;
  1195.                 }
  1196.             if (!set_videomode(TIGA, INIT_GLOBALS | CLR_SCREEN))
  1197.             {
  1198.                 printf("Error #1 Initializing TIGA.");
  1199.                 goto TIGA_error;
  1200.             }
  1201.             if (install_primitives() < 0)                  /* load if not already loaded */
  1202.             {
  1203.                 if (!set_videomode(TIGA, INIT))            /* reinit if due to full heap */
  1204.                 {
  1205.                     printf("Error #2 Initializing TIGA.");
  1206.                     goto TIGA_error;
  1207.                 }
  1208.                 else if (install_primitives() < 0)
  1209.                 {
  1210.                     printf("Error Installing TIGA Primitives.");
  1211. TIGA_error:
  1212. #ifdef DOS16RM
  1213.                     for (u=0; u < 257; u++)
  1214.                         answer[u] = save[u];
  1215. #endif
  1216.                     exit(1);
  1217.                 }
  1218.                 switch (whichvga)
  1219.                     {
  1220.                     case TIGA_16:       /* initialize demo globals */
  1221.                          get_config(&config);
  1222.                          if ((config.mode.disp_vres == 640) && (config.mode.disp_psize == 16))
  1223.                          {
  1224.                             Color_Bits = 5;
  1225.                             svga_width = 640;
  1226.                             svga_height = 480;
  1227.                             svga_yincr = 640;
  1228.                             set_config(1,0);        /* 16 bit (was 2,0 ???? - AAC) */
  1229.                             break;
  1230.                          }
  1231.                          else whichvga = TIGA_24;
  1232.                     case TIGA_24:
  1233.                         get_config(&config);
  1234.                         if ((config.mode.disp_vres == 480) && (config.mode.disp_hres == 512))
  1235.                         {
  1236.                             svga_width = 512;      /* NTSC standard television resolution */
  1237.                             svga_height = 480;
  1238.                             svga_yincr = 512;
  1239.                             set_config(4,0);       /* 24 bit */
  1240.                             break;
  1241.                         }
  1242.                         else whichvga = TRUE_640;
  1243.                     case TRUE_640:
  1244.                         get_config(&config);
  1245.                         if ((config.mode.disp_hres == 640) && (config.mode.disp_psize == 32))
  1246.                         {
  1247.                             svga_width = 640;
  1248.                             svga_height = 480;
  1249.                             svga_yincr = 640;
  1250.                             set_config(0,0);       /* 24 bit */
  1251.                             break;
  1252.                         }
  1253.                         else whichvga = TRUE_800;
  1254.                    case TRUE_800:
  1255.                         get_config(&config);
  1256.                         if ((config.mode.disp_vres == 600) && (config.mode.disp_hres == 800))
  1257.                         {
  1258.                             svga_width = 800;
  1259.                             svga_height = 600;
  1260.                             svga_yincr = 800;
  1261.                             set_config(2,0);       /* 24 bit */
  1262.                             break;
  1263.                         }
  1264.                         else whichvga = TRUE_1024;
  1265.                    case TRUE_1024:
  1266.                         svga_width = 1024;
  1267.                         svga_height = 768;
  1268.                         svga_yincr = 1024;
  1269.                         set_config(4,0);
  1270.                    }
  1271.                }
  1272.             break;
  1273. #endif
  1274.         case TARGA:             /* TARGA Plus board in Advanced Operating Mode */
  1275.             TGAHandle = open("TARGPLUS", O_RDWR | O_BINARY );   /* equivalent to TARGA+ "GraphInit() call */
  1276.             if (TGAHandle != -1) {                              /* equivalent to TARGA+ "SetRawMode()" call */
  1277.                 inr.x.ax = 0x4400;                              /* get current info on device */
  1278.                 inr.x.bx = TGAHandle;                           /* TARGA+ handle */
  1279. #ifdef DOS386
  1280.                 int86_real(0x21, &inr, &inr);
  1281. #else
  1282.                 int86(0x21, &inr, &inr);
  1283. #endif
  1284.                 inr.h.dh = 0;
  1285.                 inr.h.dl |= 0x20;                               /* or in 'RAW' bit. */
  1286. #ifdef DEBUG_VIDEO
  1287.                 printf("DIAG: inr.h.dl = %02X\n", inr.h.dl & 0x00FF);
  1288. #endif
  1289.                 inr.x.ax = 0x4401;                              /* write it back */
  1290. #ifdef DOS386
  1291.                 int86_real(0x21, &inr, &inr);
  1292. #else
  1293.                 int86(0x21, &inr, &inr);                        /* status of operation is !regs.x.cflag */
  1294. #endif
  1295. #ifdef DEBUG_VIDEO
  1296.                 diaged = FALSE;
  1297.                 printf("DIAG: SetTGARegs(5, 0) -");
  1298. #endif
  1299.                 SetTGARegs( 5, 0, &inr, &segs);                 /* equivalent to TARGA+ "ResetBoard()" call */
  1300. #ifdef DOS386
  1301.                 int86x_real(0x21, &inr, &inr, &segs);
  1302. #else
  1303.                 int86x(0x21, &inr, &inr, &segs);                /* call DOS */
  1304. #endif
  1305. #ifdef DEBUG_VIDEO
  1306.                 diaged = FALSE;
  1307.                 printf("DIAG: SetTGARegs(0,1) -");
  1308. #endif
  1309.                 SetTGARegs( 0, 1, &inr, &segs);                 /* equivalent to TARGA+ "ReadAll()" call */
  1310. #ifdef GCCDOS
  1311.                 command->IORegsOff = (unsigned short)t_val;     /* real mode offset pointer to TGARegs structure */
  1312.                 command->IORegsSeg = (unsigned short)ds_val;
  1313. #else
  1314.                 command->IORegsOff = (unsigned short)FP_OFF(TGARegs);   /* pointer to structure */
  1315.                 command->IORegsSeg = (unsigned short)FP_SEG(TGARegs);
  1316. #endif
  1317. #ifdef DEBUG_VIDEO
  1318.                 printf("DIAG: command = %p, TGARegs = %p, IORegsOff = %04X, IORegsSeg = %04X\n", command, TGARegs, command->IORegsOff, command->IORegsSeg);
  1319. #endif
  1320. #ifdef DOS386
  1321.                 int86x_real(0x21, &inr, &inr, &segs);
  1322. #else
  1323.                 int86x(0x21, &inr, &inr, &segs);               /* call DOS */
  1324. #endif
  1325.                 svga_width = TGARegs[0];                       /* sense frame buffer width and height */
  1326.                 svga_height = TGARegs[1];
  1327.                 if (show_display_type) {
  1328.                     printf("Current Display Mode: %dx%d\n", svga_width, svga_height);
  1329.                     while (time(&l) < lt + 5)                  /* display detected Display Mode for 5 secs */
  1330.                         ;
  1331.                 }
  1332.                 if (!svga_width || !svga_height) {
  1333.                     printf("Error - TARGA Plus Registers Read Invalid Values\n");
  1334. #ifdef _INTELC32_
  1335.                     _dos_setvect(0x10, prev_int10);   /* Restore previous handler */
  1336.                     _dos_freemem((unsigned) answer);  /* Free DOS buffer */
  1337. #endif
  1338.                     exit(1);
  1339.                 }
  1340.             }
  1341.             else {
  1342.                 printf("Error Opening TARGA+ Device - TARGAP.SYS driver not installed?\n");
  1343. #ifdef _INTELC32_
  1344.                 _dos_setvect(0x10, prev_int10);/* Restore previous handler */
  1345.                 _dos_freemem((unsigned) answer);/* Free DOS buffer */
  1346. #endif
  1347.                 exit(1);
  1348.             }
  1349.             break;
  1350.  
  1351. default:                /* BASIC_VGA */
  1352. no_valid_mode:
  1353.             if ((PaletteOption == HICOLOR) || (PaletteOption == FULLCOLOR))
  1354.                PaletteOption=P_332;
  1355.             inr.x.ax = 0x0013;  /* setup to VGA 320x200x256 (mode 13H) */
  1356.             svga_width = 320;   /* allow scaling to run at 320x200 */
  1357.             svga_height = 200;
  1358.             svga_yincr = 320;
  1359.             bpp = 8;
  1360.             break;
  1361.         }
  1362.  
  1363.     if (whichvga >= TRUECOLOR)  /* if True-Color then it has no palette */
  1364.         goto exit_point;
  1365.  
  1366. #ifdef DEBUG_VIDEO
  1367.     lt = l = time(&l);
  1368.     printf("Display detected: (%c) %s Adapter", whichvga + '0', vga_names[whichvga]);
  1369.     if (whichvga < TRUECOLOR)                            /*  if not a True-Color graphics card */
  1370.         printf(", with %s 512K RAM", vga_512K ? ">=" : "<");
  1371.     printf("\n   ax=%04X, bx=%04X, cx=%04X\n", inr.x.ax, inr.x.bx, inr.x.cx);
  1372.     printf("   %dx%d, yincr=%d\n", svga_width, svga_height, svga_yincr);
  1373.     printf("   shifts(R,G,B)=%d,%d,%d\n", r_shift, g_shift, b_shift);
  1374.     printf("   masks(R,G,B) =%d,%d,%d\n", r_mask, g_mask, b_mask);
  1375.     while (time(&l) < lt + 5)                          /* display detected VGA type for 5 secs */
  1376.         ;
  1377. #endif
  1378.  
  1379. #ifdef DOS386
  1380.     int86_real(0x10, &inr, &outr);  /* do the BIOS video mode sel. call */
  1381. #else
  1382.     int86(0x10, &inr, &outr);       /* do the BIOS video mode sel. call */
  1383. #endif
  1384.  
  1385.     if (whichvga == MODE13x) {      /* Tweak VGA registers to get higher res! */
  1386.         outpw(SEQUENCER, 0x0604);   /* disable chain 4 */
  1387.         outpw(SEQUENCER, 0x0F02);   /* allow writes to all planes */
  1388. #ifdef DOS386
  1389.         fp = MK_FP(_x386_zero_base_selector, 0xA0000);
  1390. #else
  1391. #ifdef DOS16RM
  1392.         fp = display_base;
  1393. #else
  1394.         fp = MK_FP(0xA000, 0);
  1395. #endif
  1396. #endif
  1397.         for (u = 0; u < 43200; u++) {              /* clear the whole screen */
  1398.             fp = (unsigned char *)MK_FP(0xA000, u);
  1399.             *fp = 0;                               /* set all bytes to 0 */
  1400.         }
  1401.         outpw(SEQUENCER, 0x0100);   /* synchronous reset */
  1402.         outp(MISCOUT, 0xE7);        /* use 28 mhz dot clock */
  1403.         outpw(SEQUENCER, 0x0300);   /* restart sequencer */
  1404.         outp(CRTC, 0x11);           /* ctrl register 11, please */
  1405.         outp(CRTC+1, inp(CRTC+1) & 0x7f); /* write-prot cr0-7 */
  1406.  
  1407.         for (i = 0; i < 17; i++)    /* write CRTC register array */
  1408.             outpw(CRTC, vptbl[i]);
  1409.         }
  1410.  
  1411.     if (PaletteOption == HICOLOR && whichvga == TSENG4) {
  1412.         if ((int)inr.h.al == 0x78)      /* if it was mode 78 */
  1413.            inr.x.bx = 0x2F;             /* make it 2F... */
  1414.         else
  1415.            inr.x.bx = (unsigned int)inr.h.al;
  1416.         inr.x.ax = 0x10F0;
  1417.         int86(0x10, &inr, &inr);        /* Go from 256 color to 32K color mode */
  1418.         r_mask = g_mask = b_mask = 0xF8;
  1419.         r_shift = 7;
  1420.         g_shift = 2;
  1421.         b_shift = 3;
  1422.         svga_yincr <<= 1;
  1423.         }
  1424.  
  1425.     if (whichvga != ATIXL && PaletteOption != HICOLOR && PaletteOption != FULLCOLOR)
  1426.         palette_init();  /* if we get here it has a normal 256 color palette DAC */
  1427.  
  1428.     if (whichvga == CHIPSTECH) {        /* (not sure why this is necessary) */
  1429.         outpw(0x46E8, 0x001E);          /* put chip in setup mode */
  1430.         outpw(0x103, 0x0080);           /* enable extended registers */
  1431.         outpw(0x46E8, 0x000E);          /* take chip out of setup mode */
  1432.         outp(0x3D6, 0x10);
  1433.         }
  1434.  
  1435. exit_point:                             /* we get here if there is no palette to initialize */
  1436.     if (whichvga == VESA && vesamode == 0x13) {
  1437.         bpp = 8;
  1438.         svga_height = 200;
  1439.         svga_width = 320;
  1440. /*      svga_yincr = 320; */
  1441.         }
  1442.     if (screen_height <= svga_height)
  1443.         y_off = (svga_height - screen_height)/2;
  1444.     else
  1445.         Height_Adjust = (DBL)screen_height / (DBL)svga_height;
  1446.     if (screen_width <= svga_width)
  1447.         x_off = (svga_width - screen_width)/2;
  1448.     else
  1449.         Width_Adjust = (DBL)screen_width / (DBL)svga_width;
  1450.  
  1451.     box(0, 0, width-1, height-1);
  1452.     /* Draw a box around the viewport, if defined */
  1453.     if(First_Column > 1 || Last_Column < width ||
  1454.      First_Line > 1 || Last_Line < height)
  1455.         box (First_Column, First_Line, Last_Column-1, Last_Line-1);
  1456.  
  1457. #if defined(DOS16RM)
  1458.     for (u=0; u < 257; u++)
  1459.         answer[u] = save[u];
  1460. #endif
  1461. #ifdef _INTELC32_
  1462.     _dos_setvect(0x10, prev_int10);       /* Restore previous handler */
  1463.     _dos_freemem((unsigned) answer);      /* Free DOS buffer */
  1464. #endif
  1465.     return;
  1466. }
  1467.  
  1468.  
  1469. int AutodetectVGA()           /* Autodetect (S)VGA Adapter Type */
  1470.     {
  1471.     unsigned char _far *biosptr;
  1472.     unsigned char tmp_byte;
  1473.     unsigned int tmp_word;
  1474.     unsigned int retval;
  1475.     union REGS inr, outr;
  1476.     struct SREGS segs;
  1477.  
  1478. #if !defined(DOS386) && !defined(_INTELC32_) && !defined(__386__) && !defined(GCCDOS)
  1479.     if (tiga_set(CD_OPEN) >= 0) /* Try initializing HGWS/TIGA Adapter */
  1480.         {
  1481.         get_config(&config);
  1482.         vga_512K = TRUE;        /* All True-Color Adapters are > 512K! */
  1483.         if ((config.mode.disp_vres == 640) && (config.mode.disp_psize == 16) == TRUE)
  1484.             return(TIGA_16);
  1485.         if ((config.mode.disp_vres == 480) && (config.mode.disp_hres == 512) == TRUE)
  1486.             return(TIGA_24);
  1487.         if ((config.mode.disp_hres == 640) && (config.mode.disp_psize == 32) == TRUE)
  1488.             return(TRUE_640);
  1489.         if ((config.mode.disp_vres == 600) && (config.mode.disp_hres == 800) == TRUE)
  1490.             return(TRUE_800);
  1491.         return(TRUE_1024);
  1492.         }
  1493. #endif
  1494.  
  1495.     TGAHandle = open("TARGPLUS", O_RDWR | O_BINARY );   /* try equivalent to TARGA+ "GraphInit() call */
  1496.     if (TGAHandle != -1)                                /* if open attempt was successful... */
  1497.     {
  1498.         close(TGAHandle);                               /* close it up again */
  1499.         return(TARGA);                                  /* and say we detected a TARGA+ card */
  1500.     }
  1501.  
  1502.     inr.x.ax = 0x4F00;                  /* Test for VESA Adapter */
  1503. #ifdef _INTELC32_
  1504.     inr.w.edi = (unsigned)answer;
  1505.     int86(0x10, &inr, &outr);
  1506. #else
  1507. #ifdef GCCDOS
  1508.     inr.x.di = di_val;                  /* deposit results here */
  1509.     int86x(0x10, &inr, &outr, &segs);
  1510. #else
  1511. #ifdef DOS386
  1512.     inr.x.di = real_buf[0];             /* deposit results here */
  1513.     segread(&segs);                     /* get our DS, etc. */
  1514.     segs.es = real_buf[1];              /* get segment of answer */
  1515.     int86x_real(0x10, &inr, &outr, &segs);
  1516.     for(tmp_word=0; tmp_word<sizeof(answer); tmp_word++)
  1517.         answer[tmp_word] = real_ptr[tmp_word];
  1518. #else
  1519. #ifdef DOS16RM
  1520.     inr.x.di = 0x580;                   /* deposit results here */
  1521.     segs.es = 0;
  1522.     int86x(0x10, &inr, &outr, &segs);
  1523. #else
  1524.     segread(&segs);                     /* get our DS, etc. */
  1525.     inr.x.di = FP_OFF(answer);          /* deposit results here */
  1526.     segread(&segs);                     /* get our DS, etc. */
  1527.     segs.es = FP_SEG(answer);           /* get segment of answer */
  1528.     int86x(0x10, &inr, &outr, &segs);   /* BIOS adapter identify call */
  1529. #endif
  1530. #endif
  1531. #endif
  1532. #endif
  1533.  
  1534.     if (outr.x.ax == 0x004F && answer[0] == 'V' && answer[1] == 'E' &&
  1535.             answer[2] == 'S' && answer[3] == 'A') {
  1536.         vga_512K = TRUE;                 /* assume all VESA's have >= 512K */
  1537.         return (VESA);
  1538.         }
  1539.  
  1540. #ifdef DOS386
  1541.     biosptr = MK_FP(_x386_zero_base_selector, 0x000C0040);
  1542. #else
  1543. #ifdef DOS16RM
  1544.     biosptr = (unsigned char *)D16SegAbsolute(0xC0040, 16);
  1545. #else
  1546.     biosptr = (unsigned char *)MK_FP(0xC000, 0x0040); /* Test for ATI Wonder */
  1547. #endif
  1548. #endif
  1549.  
  1550.     if (*biosptr == '3' && *(biosptr + 1) == '1')
  1551.         {
  1552.         _disable();                     /* Disable system interrupts */
  1553.         outp(0x1CE, 0xBB);
  1554.         if (inp(0x1CD) & 0x20)
  1555.             vga_512K = TRUE;
  1556.         _enable();                      /* Re-enable system interrupts */
  1557. #ifdef DOS16RM
  1558.         D16SegCancel((void *)biosptr);
  1559. #endif
  1560.         return (ATIVGA);
  1561.         }
  1562.     inr.x.ax = 0x7000;                  /* Test for Everex &| Trident */
  1563.     inr.x.bx = 0;
  1564.  
  1565.     CLD;
  1566.  
  1567.     int86(0x10, &inr, &outr);
  1568.     if (outr.h.al == 0x70)
  1569.         {
  1570.         if (outr.h.ch & 0xC0)
  1571.             vga_512K = TRUE;
  1572.         outr.x.dx &= 0xFFF0;
  1573.         if (outr.x.dx == 0x6780)
  1574.             {
  1575.             printf("\nT6780\n");
  1576.             return (TRIDENT);
  1577.             }
  1578.         if (outr.x.dx == 0x2360)
  1579.             {
  1580.             printf("\nT2360\n");
  1581.             return (TRIDENT);
  1582.             }
  1583.         if (outr.x.dx == 0x6730)        /* EVGA? (No BIOS Page Fn.) */
  1584.             {
  1585.             printf("\nE6730\n");
  1586.             return (EVEREX);
  1587.             }
  1588.         printf("\nE0000\n");
  1589.         return (EVEREX);        /* Newer board with fetchable bankswitch */
  1590.         }
  1591.     outp(0x3C4, 0x0B);                  /* Test for Trident */
  1592.     tmp_byte = (unsigned char) inp(0x3C5);
  1593.     if ((tmp_byte > 1) && (tmp_byte < 0x10))
  1594.         {
  1595.         vga_512K = TRUE;
  1596.         printf("\nT0000\n");
  1597.         return (TRIDENT);
  1598.         }
  1599.     if (cirrus())                       /* Test Video7 Vega VGA (Cirrus) */
  1600.         return (CIRRUS);
  1601.     inr.x.ax = 0x6F00;                  /* Test for Video7 SVGA */
  1602.     inr.x.bx = 0;                       /* note - Vega VGA (Cirrus) will */
  1603.  
  1604.     CLD;
  1605.  
  1606.     int86(0x10, &inr, &outr);           /* pass this test - test Cirrus 1st */
  1607.     if (outr.h.bh == 'V' && outr.h.bl == '7')
  1608.         {
  1609.         inr.x.ax = 0x6F07;
  1610.  
  1611.         CLD;
  1612.  
  1613.         int86(0x10, &inr, &outr);
  1614.         if ((outr.h.ah & 0x7F) > 1)
  1615.             vga_512K = TRUE;
  1616.         return (VIDEO7);
  1617.         }
  1618.     outp(0x3CE, 9);                     /* Test for Paradise */
  1619.     if (!inp(0x3CF))
  1620.         {
  1621.         outpw(0x3CE, 0x050F);           /* Turn off write protect on regs */
  1622.         if (chkbank(0,1))               /* if bank 0 and 1 same not para. */
  1623.             {                           /* FALSE == banks same... (C) */
  1624.             if (chkbank(0, 64))         /* if bank 0 and 64 same only 256K */
  1625.                 vga_512K = TRUE;
  1626.  
  1627.             retval = PARADISE;
  1628. #ifdef DOS16RM
  1629.             biosptr = (unsigned char *)D16SegAbsolute(0xC0039, 16);
  1630.             if ((*biosptr == '1') && (*biosptr+1 == '6')) /* p/n 003116 */
  1631.                 retval = VDC600;        /* a real Paradise is p/n 003145 */
  1632.             D16SegCancel((void *)biosptr);
  1633. #else
  1634.             biosptr = (unsigned char *)MK_FP(0xC000, 0x0039);/* Test for AT&T VDC600 */
  1635.             if ((*biosptr == '1') && (*biosptr+1 == '6')) /* p/n 003116 */
  1636.                 retval = VDC600;        /* a real Paradise is p/n 003145 */
  1637. #endif
  1638.             return (retval);
  1639.             }
  1640.         }
  1641.     inr.x.ax = 0x5F00;                  /* Test for Chips & Tech */
  1642.     inr.x.bx = 0;
  1643.  
  1644.     CLD;
  1645.  
  1646.     int86(0x10, &inr, &outr);
  1647.     if (outr.h.al == 0x5F)
  1648.         {
  1649.         if (outr.h.bh >= 1)
  1650.             vga_512K = TRUE;
  1651.         return (CHIPSTECH);
  1652.         }
  1653.     outp(0x3D4, 0x33);                  /* Test for Tseng 4000 or 3000 Chip */
  1654.     tmp_word = (unsigned int) inp(0x3D5) << 8;
  1655.     outpw(0x3D4, 0x0A33);
  1656.     outp(0x3D4, 0x33);
  1657.     retval = BASIC_VGA;
  1658.     if ((inp(0x3D5) & 0x0F) == 0x0A)
  1659.         {
  1660.         outpw(0x3D4, 0x0533);
  1661.         outp(0x3D4, 0x33);
  1662.         if ((inp(0x3D5) & 0x0F) == 0x05)
  1663.             {
  1664.             retval = TSENG4;
  1665.             outpw(0x3D4, tmp_word | 0x33);
  1666.             outp(0x3D4, 0x37);
  1667.             if ((inp(0x3D5) & 0x0A) == 0x0A)
  1668.                 vga_512K = TRUE;
  1669.             outp(0x3BF, 0x03);          /* Enable access to extended regs */
  1670.             outp(0x3D8, 0xA0);
  1671.             outp(0x3D8, 0x29);          /* Enable mapping register access */
  1672.             outp(0x3D8, 0xA0);
  1673.             }
  1674.         }
  1675.     tmp_byte = (unsigned char) inp(0x3CD);      /* save bank switch reg */
  1676.     outp(0x3CD, 0xAA);                          /* test register w/ 0xAA */
  1677.     if (inp(0x3CD) == 0xAA)
  1678.         {
  1679.         outp(0x3CD, 0x55);                      /* test register w/ 0x55 */
  1680.         if (inp(0x3CD) == 0x55)
  1681.             {
  1682.             outp(0x3CD, tmp_byte);              /* restore bank switch reg */
  1683.             if (retval != TSENG4)               /* yep, it's a Tseng... */
  1684.                 retval = TSENG3;
  1685.             vga_512K = TRUE;
  1686.             return (retval);
  1687.             }
  1688.         }
  1689.     outpw(0x3CE, 0x200F);                       /* Test for Ahead A or B chipsets */
  1690.     tmp_byte = (unsigned char) inp(0x3CF);
  1691.     if (tmp_byte == 0x21)
  1692.         {
  1693.         vga_512K = TRUE;                        /* Assume all Ahead's have 512K... */
  1694.         return (AHEADB);
  1695.         }
  1696.     if (tmp_byte == 0x20)
  1697.         {
  1698.         vga_512K = TRUE;
  1699.         return (AHEADA);
  1700.         }
  1701.     if ((inp(0x3DE) & 0xE0) == 0x60)             /* Test for Oak Tech OTI-067 */
  1702.         {
  1703.         outp(0x3DE, 0x0D);
  1704.         if (inp(0x3DF) & 0x80)
  1705.             vga_512K = TRUE;
  1706.         return(OAKTECH);
  1707.         }
  1708.     return (BASIC_VGA);                          /* Return 1 if Unknown/BASIC_VGA */
  1709.     }
  1710.  
  1711. int cirrus()                                     /* Test for presence of Cirrus VGA Chip */
  1712.     {
  1713.     unsigned char tmp_byte;
  1714.     unsigned int crc_word, tmp_word;
  1715.     int retcode = FALSE;
  1716.  
  1717.     outp(0x3D4, 0x0C);                          /* assume 3Dx addressing, scrn A start addr hi */
  1718.     crc_word = (unsigned int) inp(0x3D5) << 8;  /* save the crc */
  1719.     outp(0x3D5, 0);                             /* clear the crc */
  1720.     outp(0x3D4, 0x1F);                          /* Eagle ID register */
  1721.     tmp_byte = (unsigned char) inp(0x3D5);      /* nybble swap "register" */
  1722.     tmp_word = (((tmp_byte & 0x0F) << 4) | ((tmp_byte & 0xf0) >> 4)) << 8;
  1723.     outpw(0x3C4, tmp_word | 0x06);              /* disable extensions */
  1724.     if (!inp(0x3C5))
  1725.         {
  1726.         tmp_word = (unsigned int) tmp_byte << 8;
  1727.         outpw(0x3C4, tmp_word | 0x06);          /* re-enable extensions */
  1728.         if (inp(0x3C5) == 1)
  1729.             retcode = TRUE;
  1730.         }
  1731.     outpw(0x3D5, crc_word | 0x0c);              /* restore the crc */
  1732.     return (retcode);
  1733.     }
  1734.  
  1735. int chkbank(bank0, bank1)                       /* Paradise SVGA specific stuff */
  1736.    unsigned int bank0, bank1;                   /* returns TRUE if banks are different RAM */
  1737.    {
  1738.    static unsigned int value = 0x1234;
  1739.    unsigned int _far *fp;
  1740.    unsigned int temp;
  1741.    unsigned int oldval0, oldval1;
  1742.  
  1743. #ifdef DOS386                                   /* Point out into display RAM */
  1744.    fp = MK_FP(_x386_zero_base_selector, 0xB8000);
  1745. #else
  1746.    fp = (unsigned int *)MK_FP(0xB800, 0);       /* Point out into display RAM */
  1747. #endif
  1748.  
  1749.    outp(0x3CE, 9);
  1750.    outp(0x3CF, bank0);                          /* save prior video data and write test values */
  1751.    oldval0 = *fp;
  1752.    *fp ^= value;
  1753.    if (*fp != (oldval0 ^ value)) {
  1754.       *fp = oldval0;
  1755.       return FALSE;                             /* No RAM there at all -- can't be 512K */
  1756.       }
  1757.  
  1758.    outp(0x3CE, 9);
  1759.    outp(0x3CF, bank1);                          /* save prior video data and write test values */
  1760.    oldval1 = *fp;
  1761.    *fp ^= value;
  1762.    if (*fp != (oldval1 ^ value)) {
  1763.       *fp = oldval1;
  1764.       outp(0x3CE, 9);
  1765.       outp(0x3CF, bank0);
  1766.       *fp = oldval0;
  1767.       return FALSE;                              /* No RAM there at all -- can't be 512K */
  1768.       }
  1769.    if (*fp != oldval0) {
  1770.       *fp = oldval1;
  1771.       outp(0x3CE, 9);
  1772.       outp(0x3CF, bank0);
  1773.       *fp = oldval0;
  1774.       return TRUE;                                /* pages cannot be the same RAM */
  1775.       }
  1776.  
  1777.    outp(0x3CE, 9);
  1778.    outp(0x3CF, bank0);
  1779.    temp = *fp;
  1780.    outp(0x3CE, 9);
  1781.    outp(0x3CF, bank1);
  1782.    *fp = oldval1;
  1783.    outp(0x3CE, 9);
  1784.    outp(0x3CF, bank0);
  1785.    *fp = oldval0;
  1786.  
  1787.    if (temp == oldval0)
  1788.       return FALSE;                               /* pages are the same RAM */
  1789.    else
  1790.       return TRUE;                                /* independent values, so not same RAM */
  1791.    }
  1792.  
  1793. void atiplot(x, y, r, g, b)                  /* ATI VGA Wonder XL 32K color display plot */
  1794. int x, y, r, g, b;
  1795. {
  1796.    char plane=0;
  1797.    unsigned offset, tmp_word;
  1798.    unsigned long address;
  1799.    unsigned short _far *fp;
  1800.  
  1801.    r/=8;
  1802.    g/=8;
  1803.    b/=8;
  1804.    address = y * 1280L + x * 2L;
  1805.    offset = (unsigned)(address % 65536L);
  1806.    plane = (char)(address / 65536L);
  1807.    _disable();
  1808.  
  1809.    outp(0x1CE, 0xB2);                      /* NOTE: this used to be the atibank() funct. - AAC */
  1810.    tmp_word = (unsigned int)((plane << 1) | (inp(0x1CF) & 0xE1));
  1811.    outpw(0x1CE, (tmp_word << 8) | 0x00B2);
  1812.  
  1813. #ifdef DOS386
  1814.    fp = MK_FP(_x386_zero_base_selector, 0xA0000 + offset);
  1815. #else
  1816. #ifdef DOS16RM
  1817.    fp = (unsigned short *)(display_base + offset);
  1818. #else
  1819.    fp = (unsigned short *)MK_FP(0xA000, offset);
  1820. #endif
  1821. #endif
  1822.  
  1823.    *fp = (unsigned short)(r * 1024 + g * 32 + b);
  1824.    _enable();
  1825.    return;
  1826. }
  1827.  
  1828. void SetTGARegs(cmd, read, r, s)        /* Setup TARGA+ Register Array Subroutine */
  1829. int cmd;                                /* Command number */
  1830. int read;                               /* 1 if read, 0 if write */
  1831. union REGS *r;                          /* accumulator registers */
  1832. struct SREGS *s;                        /* segment registers */
  1833. {
  1834.     if (read)
  1835. #ifdef GCCDOS
  1836.         r->x.ax = 0x440A;               /* GCCFIX will make this an IOCTL Read */
  1837. #else
  1838.         r->x.ax = 0x4402;               /* set up IOCTL Read */
  1839. #endif
  1840.     else
  1841. #ifdef GCCDOS
  1842.         r->x.ax = 0x440B;               /* GCCFIX will make this an IOCTL Write */
  1843. #else
  1844.         r->x.ax = 0x4403;               /* set up IOCTL Write */
  1845. #endif
  1846.     r->x.bx = TGAHandle;                /* TARGA device handle */
  1847.     r->x.cx = sizeof(TGACommand);       /* read/write a whole cmd struct */
  1848. #ifdef GCCDOS
  1849.     r->x.dx = c_val;                    /* real mode offset of Command structure */
  1850. #else
  1851.     r->x.dx = (unsigned short) FP_OFF(command);     /* Address of Command structure */
  1852.     s->ds = (unsigned short) FP_SEG(command);
  1853. #endif
  1854. #ifdef DEBUG_VIDEO
  1855.     if (!diaged)
  1856.     {
  1857.         diaged = TRUE;
  1858.         printf(" Sizeof TGACommand = %d, command = %p, dx = %08lX, ds = %04X\n", r->x.cx, command, r->x.dx, s->ds);
  1859.     }
  1860. #endif
  1861.     command->IOCmd = cmd;               /* set current I/O command */
  1862.     command->IOStrucSiz = 260;          /* size in bytes of sizeof(TGARegs) = unsigned short [130] */
  1863. }
  1864.  
  1865.  
  1866. #if defined( DOS386 )
  1867. void setmany(palbuf, start, count)
  1868. unsigned char palbuf[256][3];
  1869. int start, count;
  1870. {
  1871.    unsigned char _far *fp;
  1872.    union REGS regs;
  1873.    struct SREGS sregs;
  1874.    unsigned i;
  1875.  
  1876.    /* Put the contents of the new palette into real memory, the only
  1877.       place I know that won't impact things is the video RAM. */
  1878.    fp = MK_FP(_x386_zero_base_selector, 0xA0000);
  1879.    for (i=start;i<start+count;i++) {
  1880.       fp[3*(i-start)  ] = palbuf[i][0];
  1881.       fp[3*(i-start)+1] = palbuf[i][1];
  1882.       fp[3*(i-start)+2] = palbuf[i][2];
  1883.       }
  1884.  
  1885.    regs.x.ax = 0x1012;
  1886.    regs.x.bx = start;
  1887.    regs.x.cx = count;
  1888.    regs.x.dx = 0;
  1889.    segread(&sregs);
  1890.    sregs.es  = 0xA000;
  1891.    int86x_real(0x10, ®s, ®s, &sregs);
  1892.    /* Now clear off the values that got dumped into the video RAM */
  1893.    for (i=0;i<3*count; i++)
  1894.       *fp++ = 0;
  1895. }
  1896. #endif
  1897.  
  1898. void palette_init()             /* Fill VGA 256 color palette with colors! */
  1899.     {
  1900.     register unsigned m;
  1901.     unsigned r, g, b;
  1902.     register DBL hue, sat, val;
  1903. #ifdef DOS386
  1904.     unsigned char palbuf[256][3];
  1905. #endif
  1906.  
  1907.     if (PaletteOption == GREY)  /* B/W Video Mod */
  1908.     {
  1909.         for (m = 1; m < 64; m++)     /* for the 1st 64 colors... */
  1910.             set_palette_register (m, m, m, m); /* set m to rgb value */
  1911.         for (m = 64; m < 256; m++)     /* for the remaining, at full value */
  1912.             set_palette_register (m, 63, 63, 63);
  1913.         return;
  1914.     }
  1915.     if (PaletteOption == P_332) /* 332 Video Mod */
  1916.     {
  1917.                 for (r=0;r<8;r++)
  1918.                         for (g=0;g<8;g++)
  1919.                                 for (b=0;b<4;b++) {
  1920.                                         m = (r * 32 + g * 4 + b);
  1921. #ifdef DOS386
  1922.                     palbuf[m][0] = r * 9;
  1923.                     palbuf[m][1] = g * 9;
  1924.                     palbuf[m][2] = b * 21;
  1925. #else
  1926.                     set_palette_register(m, r * 9, g * 9, b * 21);
  1927. #endif
  1928.                                 }
  1929. #ifdef DOS386
  1930. setmany(palbuf, 0, 256);
  1931. #endif
  1932.                 return;
  1933.         }
  1934.  
  1935.     /* otherwise, use the classic HSV palette scheme */
  1936.     
  1937.     set_palette_register(0, 0, 0, 0);   /* make palette register 0 black */
  1938.  
  1939.     set_palette_register(64, 63, 63, 63);   /* make palette register 64 white */
  1940.  
  1941.     set_palette_register(128, 31, 31, 31);  /* make register 128 dark grey */
  1942.  
  1943.     set_palette_register(192, 48, 48, 48);  /* make register 192 lite grey */
  1944.  
  1945.     for (m = 1; m < 64; m++)                /* for the 1st 64 colors... */
  1946.         {
  1947.         sat = 0.5;                          /* start with the saturation and intensity low */
  1948.         val = 0.5;
  1949.         hue = 360.0 * ((DBL)(m)) / 64.0;    /* normalize to 360 */
  1950.         hsv_to_rgb (hue, sat, val, &r, &g, &b);
  1951.         set_palette_register (m, r, g, b);  /* set m to rgb value */
  1952.  
  1953.         sat = 1.0;                          /* high saturation and half intensity (shades) */
  1954.         val = 0.50;
  1955.         hue = 360.0 * ((DBL)(m)) / 64.0;    /* normalize to 360 */
  1956.         hsv_to_rgb (hue, sat, val, &r, &g, &b);
  1957.         set_palette_register (m + 64, r, g, b);  /* set m + 64 */
  1958.  
  1959.         sat = 0.5;                          /* half saturation and high intensity (pastels) */
  1960.         val = 1.0;
  1961.  
  1962.         hue = 360.0 * ((DBL)(m)) / 64.0;    /* normalize to 360 */
  1963.         hsv_to_rgb (hue, sat, val, &r, &g, &b);
  1964.         set_palette_register (m + 128, r, g, b); /* set m + 128 */
  1965.  
  1966.         sat = 1.0;                          /* normal full HSV set at full intensity */
  1967.         val = 1.0;
  1968.  
  1969.         hue = 360.0 * ((DBL)(m)) / 64.0;    /* normalize to 360 */
  1970.         hsv_to_rgb (hue, sat, val, &r, &g, &b);
  1971.         set_palette_register (m + 192, r, g, b); /* set m + 192 */
  1972.         }
  1973.     return;
  1974.     }
  1975.  
  1976.  
  1977. void display_finished ()
  1978.     {
  1979.     if (Options & PROMPTEXIT)
  1980.         {
  1981.         fprintf (stdout,"\007\007");    /* long beep */
  1982.         fflush (stdout);                /* make sure its heard */
  1983.         while(!kbhit())         /* wait for key hit */
  1984.            ;
  1985.         if (!getch())           /* get another if ext. scancode */
  1986.            getch();
  1987.         }
  1988.     }
  1989.  
  1990.  
  1991. void display_close()            /* setup to Text 80x25 (mode 3) */
  1992.     {
  1993.     union REGS regs;
  1994.     struct SREGS segs;
  1995.  
  1996. #ifdef __386__
  1997.     regs.x.ax = 0x0003;
  1998.     int86(0x10, ®s, ®s);
  1999.     regs.x.ax = 0x4900;
  2000.     segs.es = vesabuffer;
  2001.     segs.ds = 0;
  2002.     int86x(0x21, ®s, ®s, &segs);
  2003. #else
  2004.     if (whichvga < TRUECOLOR) {
  2005.         regs.x.ax = 0x0003;
  2006.         int86(0x10, ®s, ®s);
  2007.         }
  2008.     else {        /* must be non-SVGA */
  2009. #if !defined(_INTELC32_) && !defined(GCCDOS) && !defined(DOS16RM) && !defined(DOS386)
  2010.         if (whichvga <= TRUE_1024)
  2011.             set_videomode(PREVIOUS, INIT);
  2012.         else
  2013. #endif
  2014.             if (whichvga <= TARGA) {
  2015. #ifdef DEBUG_VIDEO
  2016.                 diaged = FALSE;
  2017.                 printf("DIAG: SetTGARegs(0,0) -");
  2018. #endif
  2019.                 SetTGARegs(0, 0, ®s, &segs);                    /* equivalent to TARGA+ "WriteAll()" call */
  2020. #ifdef GCCDOS
  2021.                 command->IORegsOff = (unsigned short)t_val;        /* real mode offset pointer to TGARegs structure */
  2022.                 command->IORegsSeg = (unsigned short)ds_val;
  2023. #else
  2024.                 command->IORegsOff = (unsigned short)FP_OFF(TGARegs);   /* pointer to structure */
  2025.                 command->IORegsSeg = (unsigned short)FP_SEG(TGARegs);
  2026. #endif
  2027. #ifdef DEBUG_VIDEO
  2028.                 printf("DIAG: command = %p, TGARegs = %p, IORegsOff = %04X, IORegsSeg = %04X\n", command, TGARegs, command->IORegsOff, command->IORegsSeg);
  2029. #endif
  2030. #ifdef DOS386
  2031.                 int86x_real(0x21, ®s, ®s, &segs);
  2032. #else
  2033.                 int86x(0x21, ®s, ®s, &segs);              /* call DOS - status of operation is !regs.x.cflag */
  2034. #endif
  2035.                 close(TGAHandle);                               /* close file channel to targap.sys driver */
  2036.             }
  2037.     }
  2038. #endif
  2039.  
  2040. #ifdef DOS16RM
  2041.     D16SegCancel(display_base);
  2042. #endif
  2043.     return;
  2044. }
  2045.  
  2046.  
  2047.   /* plot a single RGB pixel */
  2048. void display_plot (x, y, Red, Green, Blue)
  2049.    int x, y;
  2050.    unsigned char Red, Green, Blue;
  2051.    {
  2052.    union REGS inr;
  2053.    struct SREGS segs;
  2054.  
  2055.    register unsigned char color, svga_page;
  2056.    unsigned char _far *fp;
  2057.    unsigned int svga_word;
  2058.    unsigned long svga_loc, nsvga_loc, lcolor;
  2059.    DBL h, s, v, fx, fy;
  2060. #ifdef DITHERED
  2061.    int i, r, g, b, re, ge, be, ri, gi, bi;
  2062. #endif
  2063.  
  2064.    if (x == First_Column)                  /* first pixel on this line? */
  2065.         {
  2066. #ifdef DITHERED
  2067. if (screen_width <= 2048) {
  2068.          for(i=0;(unsigned short)i < screen_width+2;i++)
  2069.             {
  2070.             r_err[(y+1)&1][i+x_off] = 0;
  2071.             g_err[(y+1)&1][i+x_off] = 0;
  2072.             b_err[(y+1)&1][i+x_off] = 0;
  2073.             }
  2074. }
  2075. #endif
  2076.         lastx = -1;             /* reset cache, make sure we do the 1st one */
  2077.         lasty = lastline;       /* set last line do to prior line */
  2078.         }
  2079.  
  2080.     y += y_off;
  2081.     x += x_off;
  2082.  
  2083.    /* Scaling factors precomputed per RHA in Height_Adjust or Width_Adjust */
  2084.  
  2085.    if (screen_height > svga_height)     /* auto-scale Y */
  2086.         {
  2087.         fy = (DBL)y / Height_Adjust;
  2088.         y = (int)fy;            /* scale y to svga_height */
  2089.         if (y <= lasty)         /* discard if repeated line */
  2090.             return;
  2091.         lastline = y;           /* save current working line */
  2092.         }
  2093.  
  2094.    if (screen_width > svga_width)               /* auto-scale X */
  2095.         {
  2096.         fx = (DBL)x / Width_Adjust;
  2097.         x = (int)fx;            /* scale x to svga_width */
  2098.         if (x <= lastx)         /* discard if repeated pixel */
  2099.             return;
  2100.         lastx = x;              /* save most recent pixel done */
  2101.         }
  2102.  
  2103.    if (whichvga == ATIXL)
  2104.         {
  2105.         atiplot(x, y, (int)Red, (int)Green, (int)Blue);
  2106.         return;
  2107.         }
  2108.  
  2109.     if (whichvga == TARGA)
  2110.         {
  2111. #ifdef DEBUG_VIDEO
  2112.         diaged = TRUE;                            /* I have this particular diag turned off for now - AAC */
  2113. /*        printf("DIAG: SetTGARegs(2,0) -");      */
  2114. #endif
  2115.         SetTGARegs(2, 0, &inr, &segs);          /* equivalent to TARGA+ "PutPixel()" call */
  2116.         command->IOxi = x;
  2117.         command->IOyi = (svga_height - 1) - y;  /* the stupid-ass TARGA+ board is upside down!! */
  2118.         switch(TGARegs[4]) {                    /* sense pixel depth */
  2119.         case 1:         /* 8 bits total */
  2120.             lcolor = (unsigned long)((int)(0.299 * (DBL)Red) + (int)(0.587 * (DBL)Green) + (int)(.114 * (DBL)Blue));
  2121.             break;
  2122.         case 2:         /* 16 bits total */
  2123.             lcolor = (unsigned long)(((int)Blue & 0x00f8) >> 3) + (unsigned long)(((int)Green & 0x00f8) << 2) + (unsigned long)(((int)Red & 0x00f8) << 7);
  2124.             break;
  2125.         case 4:         /* 32 (and 24) bits total */
  2126.             lcolor = ((unsigned long)Red << 16) + ((unsigned long)Green << 8) + (unsigned long)Blue;
  2127.             break;
  2128.         }
  2129.  
  2130. #if !defined(DOS16RM) && !defined(DOS386) && !defined(_INTELC32_) && !defined (__386__) && !defined(GCCDOS)
  2131.         command->IOcolor = lcolor;
  2132. #else
  2133.         command->IOcolorLo = (unsigned short)(lcolor);
  2134.         command->IOcolorHi = (unsigned short)((lcolor & 0xFFFF0000L) >> 16);
  2135. #endif
  2136. #ifdef DOS386
  2137.         int86x_real(0x21, &inr, &inr, &segs);
  2138. #else
  2139.         int86x(0x21, &inr, &inr, &segs);                    /* call DOS to perform the pixel write */
  2140. #endif
  2141.         return;                                             /* status of operation is !regs.x.cflag */
  2142.     }
  2143.  
  2144. #if !defined(DOS386) && !defined(_INTELC32_) && !defined(__386__) && !defined(GCCDOS)
  2145.    if (whichvga == TIGA_16)
  2146.         {
  2147.         set_bcolor(0);
  2148.         set_fcolor(((unsigned)Red << 10) + (Green << 5) + Blue);
  2149.         draw_point(x, y);
  2150.         return;
  2151.         }
  2152.  
  2153.    if (whichvga == TIGA_24 || whichvga == TRUE_640 || whichvga == TRUE_640 || whichvga == TRUE_1024)
  2154.         {
  2155.         hpt[0] = Green;
  2156.         hpt[1] = Red;
  2157.         hpt[2] = Blue;
  2158.         host2gsp(hptr, ((whichvga == TIGA_24) ? 0x4000L : 0x8000L) * y + ((long) x << 5), 3, 0);
  2159.         return;
  2160.         }
  2161. #endif
  2162.  
  2163.    if (PaletteOption == FULLCOLOR) {
  2164.         svga_loc = ((unsigned long)svga_yincr) * y + x * pixel_len;
  2165.         svga_page = (unsigned char)(svga_loc/gran);
  2166.         svga_loc %= gran;
  2167.         if (svga_page != cur_page) {
  2168.             cur_page = svga_page;
  2169.             if (whichvga == VESA)
  2170.                 {
  2171.                 inr.x.bx = map_code;            /* map code = 0 or 1 */
  2172.                 inr.x.ax = 0x4F05;
  2173.                 inr.x.dx = (unsigned int)svga_page;
  2174. #ifdef DOS386
  2175.                 int86_real(0x10, &inr, &inr);
  2176. #else
  2177.                 int86(0x10, &inr, &inr);
  2178. #endif
  2179.             }
  2180.             else if (whichvga == PARADISE24X) /* need to do special bank */
  2181.                 {                             /* switching with Paradise */
  2182.                 newbank();                    /* chips. */
  2183.             }
  2184.             else
  2185.                 {
  2186.                 outp(0x3CD, (unsigned char)svga_page);
  2187.             }
  2188.         }
  2189.  
  2190. #ifdef DOS386
  2191.         fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)svga_loc);
  2192. #else
  2193. #ifdef DOS16RM
  2194.         fp = display_base + (unsigned int)svga_loc;
  2195. #else
  2196.         fp = (unsigned char *)MK_FP(0xA000, (unsigned int)svga_loc);
  2197. #endif
  2198. #endif
  2199.         if (svga_loc + 2 < gran) {                /* if pixel doesn't cross */
  2200.             fp[blue_ndx] = (unsigned char)Blue;   /* bank, draw all of it */
  2201.             fp[green_ndx] = (unsigned char)Green;
  2202.             fp[red_ndx] = (unsigned char)Red;
  2203.         }
  2204.         else {                                    /* otherwise do bank    */
  2205.           cur_page++;                             /* switch for next bank */
  2206.           newbank();
  2207.           if (svga_loc+blue_ndx >= gran) {
  2208.               nsvga_loc = (svga_loc+blue_ndx) % gran;
  2209. #ifdef DOS386
  2210.         fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)nsvga_loc);
  2211. #else
  2212. #ifdef DOS16RM
  2213.         fp = display_base + (unsigned int)nsvga_loc;
  2214. #else
  2215.         fp = (unsigned char *)MK_FP(0xA000, (unsigned int)nsvga_loc);
  2216. #endif
  2217. #endif
  2218.               *fp = (unsigned char)Blue;
  2219.           }
  2220.           if (svga_loc+green_ndx >= gran) {
  2221.               nsvga_loc = (svga_loc+green_ndx) % gran;
  2222. #ifdef DOS386
  2223.         fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)nsvga_loc);
  2224. #else
  2225. #ifdef DOS16RM
  2226.         fp = display_base + (unsigned int)nsvga_loc;
  2227. #else
  2228.         fp = (unsigned char *)MK_FP(0xA000, (unsigned int)nsvga_loc);
  2229. #endif
  2230. #endif
  2231.               *fp = (unsigned char)Green;
  2232.           }
  2233.           if (svga_loc+red_ndx >= gran) {
  2234.               nsvga_loc = (svga_loc+red_ndx) % gran;
  2235. #ifdef DOS386
  2236.         fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)nsvga_loc);
  2237. #else
  2238. #ifdef DOS16RM
  2239.         fp = display_base + (unsigned int)nsvga_loc;
  2240. #else
  2241.         fp = (unsigned char *)MK_FP(0xA000, (unsigned int)nsvga_loc);
  2242. #endif
  2243. #endif
  2244.               *fp = (unsigned char)Red;
  2245.           }
  2246.         }
  2247.         return;
  2248.     }
  2249.     if (PaletteOption == HICOLOR) {
  2250.         svga_loc = ((unsigned long)svga_yincr) * y + (x << 1);
  2251.         svga_page = (unsigned char)(svga_loc/gran);
  2252.         svga_loc %= gran;
  2253.         if (svga_page != cur_page) {
  2254.             cur_page = svga_page;
  2255.             if (whichvga == VESA)
  2256.                 {
  2257.                 inr.x.bx = map_code;            /* map code = 0 or 1 */
  2258.                 inr.x.ax = 0x4F05;
  2259.                 inr.x.dx = (unsigned int)svga_page;
  2260. #ifdef DOS386
  2261.                 int86_real(0x10, &inr, &inr);
  2262. #else
  2263.                 int86(0x10, &inr, &inr);
  2264. #endif
  2265.                 }
  2266.             else if (whichvga == PARADISE24X) /* need to do special bank */
  2267.                 {                             /* switching with Paradise */
  2268.                 newbank();                    /* chips. */
  2269.             }
  2270.             else
  2271.                 outp(0x3CD, (unsigned char)svga_page);
  2272.             }
  2273.  
  2274. #ifdef DOS386
  2275.         fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)svga_loc);
  2276. #else
  2277. #ifdef DOS16RM
  2278.         fp = display_base + (unsigned int)svga_loc;
  2279. #else
  2280.         fp = (unsigned char *)MK_FP(0xA000, (unsigned int)svga_loc);
  2281. #endif
  2282. #endif
  2283.  
  2284. #ifdef DITHERED
  2285. if (screen_width <= 2048) {
  2286.         r = ri = (int)Red + (int)r_err[y&1][x+1]/16;
  2287.         g = gi = (int)Green + (int)g_err[y&1][x+1]/16;
  2288.         b = bi = (int)Blue + (int)b_err[y&1][x+1]/16;
  2289.  
  2290.         if(ri > 255) ri = 255;
  2291.         if(gi > 255) gi = 255;
  2292.         if(bi > 255) bi = 255;
  2293.         if(ri < 0) ri = 0;
  2294.         if(gi < 0) gi = 0;
  2295.         if(bi < 0) bi = 0;
  2296.  
  2297.         ri &= r_mask;
  2298.         gi &= g_mask;
  2299.         bi &= b_mask;
  2300.  
  2301.         re = r - ri;
  2302.         ge = g - gi;
  2303.         be = b - bi;
  2304.  
  2305.         r_err[y&1][x+2] += (signed char)(7*re);
  2306.         r_err[y&1][x] += (signed char)re;
  2307.         r_err[(y+1)&1][x+1] += (signed char)(5*re);
  2308.         r_err[(y+1)&1][x+2] += (signed char)(3*re);
  2309.         g_err[y&1][x+2] += (signed char)(7*ge);
  2310.         g_err[y&1][x] += (signed char)ge;
  2311.         g_err[(y+1)&1][x+1] += (signed char)(5*ge);
  2312.         g_err[(y+1)&1][x+2] += (signed char)(3*ge);
  2313.         b_err[y&1][x+2] += (signed char)(7*be);
  2314.         b_err[y&1][x] += (signed char)be;
  2315.         b_err[(y+1)&1][x+1] += (signed char)(5*be);
  2316.         b_err[(y+1)&1][x+2] += (signed char)(3*be);
  2317.  
  2318.         *(unsigned short _far *)fp = (unsigned short)  /* stash 16-bit pixel */
  2319.                 (((unsigned long)ri << r_shift) |
  2320.                 ((unsigned long)gi << g_shift) |((unsigned long)bi >> b_shift));
  2321.         return;
  2322. }
  2323. else
  2324.         *(unsigned short _far *)fp = (unsigned short)  /* stash 16-bit pixel */
  2325.                 (((unsigned long)(Red & r_mask) << r_shift) |
  2326.                 ((unsigned long)(Green & g_mask) << g_shift) |
  2327.                 ((unsigned long)(Blue & b_mask) >> b_shift));
  2328.         return;
  2329.  
  2330. #else
  2331.         *(unsigned short _far *)fp = (unsigned short)  /* stash 16-bit pixel */
  2332.                 (((unsigned long)(Red & r_mask) << r_shift) |
  2333.                 ((unsigned long)(Green & g_mask) << g_shift) |
  2334.                 ((unsigned long)(Blue & b_mask) >> b_shift));
  2335.         return;
  2336. #endif
  2337.     }
  2338.  
  2339.     if (PaletteOption == GREY)                /* RGB are already set the same, so */
  2340.         color = (unsigned char)(Green >> 2);  /* really, any color will do... */
  2341.     else if (PaletteOption == P_332) {
  2342. #ifdef DITHERED
  2343. if (screen_width <= 2048) {
  2344.         r = (int)Red + (int)r_err[y&1][x+1];
  2345.         g = (int)Green + (int)g_err[y&1][x+1];
  2346.         b = (int)Blue + (int)b_err[y&1][x+1];
  2347.  
  2348.         ri = (r+18)/36;
  2349.         gi = (g+18)/36;
  2350.         bi = (b+42)/84;
  2351.  
  2352.         if (ri > 7)
  2353.             ri=7;
  2354.         if (gi > 7)
  2355.             gi=7;
  2356.         if (bi > 3)
  2357.             bi=3;
  2358.         if (ri < 0)
  2359.             ri=0;
  2360.         if (gi < 0)
  2361.             gi=0;
  2362.         if (bi < 0)
  2363.             bi=0;
  2364.  
  2365.         color = (unsigned char)(((ri<<5) + (gi<<2) + bi));
  2366.  
  2367.         re = r - ((255 * ri) / 7);
  2368.         ge = g - ((255 * gi) / 7);
  2369.         be = b - ((255 * bi) / 3);
  2370.  
  2371.         r_err[y&1][x+2] += (signed char)((7*re)/16);
  2372.         r_err[y&1][x] += (signed char)(re/16);
  2373.         r_err[(y+1)&1][x+1] += (signed char)((5*re)/16);
  2374.         r_err[(y+1)&1][x+2] += (signed char)((3*re)/16);
  2375.         g_err[y&1][x+2] += (signed char)((7*ge)/16);
  2376.         g_err[y&1][x] += (signed char)(ge/16);
  2377.         g_err[(y+1)&1][x+1] += (signed char)((5*ge)/16);
  2378.         g_err[(y+1)&1][x+2] += (signed char)((3*ge)/16);
  2379.         b_err[y&1][x+2] += (signed char)((7*be)/16);
  2380.         b_err[y&1][x] += (signed char)(be/16);
  2381.         b_err[(y+1)&1][x+1] += (signed char)((5*be)/16);
  2382.         b_err[(y+1)&1][x+2] += (signed char)((3*be)/16);
  2383. }
  2384. else
  2385.         color = ((Red & 0xE0) | ((Green & 0xE0) >> 3) | ((Blue & 0xC0) >> 6));
  2386. #else
  2387.         color = ((Red & 0xE0) | ((Green & 0xE0) >> 3) | ((Blue & 0xC0) >> 6));
  2388.  
  2389. #endif
  2390.     }
  2391.     else {  /* Translate RGB value to nearest of 256 palette Colors (by HSV) */
  2392.         rgb_to_hsv((unsigned)Red,(unsigned)Green,(unsigned)Blue, &h, &s, &v);
  2393.         if (s < 0.20) {           /* black or white if no saturation of color... */
  2394.             if (v < 0.25)
  2395.                 color = 0;        /* black */
  2396.             else if (v > 0.8)
  2397.                 color = 64;       /* white */
  2398.             else if (v > 0.5)
  2399.                 color = 192;      /* lite grey */
  2400.             else
  2401.                 color = 128;      /* dark grey */
  2402.         }
  2403.         else {
  2404.             color = (unsigned char) (64.0 * ((DBL)(h)) / 360.0);
  2405.  
  2406.             if (!color)
  2407.                 color = 1;        /* avoid black, white or grey */
  2408.  
  2409.             if (color > 63)
  2410.                 color = 63;       /* avoid same */
  2411.  
  2412.             if (v > 0.50)
  2413.                 color |= 0x80;    /* colors 128-255 for high inten. */
  2414.  
  2415.             if (s > 0.50)         /* more than half saturated? */
  2416.                 color |= 0x40;    /* color range 64-128 or 192-255 */
  2417.         }
  2418.     }
  2419.     switch (whichvga)             /* decide on (S)VGA bank switching scheme to use */
  2420.         {
  2421.            case BASIC_VGA:        /* none */
  2422.                 svga_loc = svga_yincr * y + x;
  2423.                 break;
  2424.  
  2425.            case MODE13x:          /* faked */
  2426.                 svga_word = 1 << (x & 3);       /* form bit plane mask */
  2427.                 svga_word = (svga_word << 8) | 2;
  2428.                 outpw(SEQUENCER, svga_word);    /* tweak the sequencer */
  2429.                 svga_loc = svga_yincr * y + (x >> 2);
  2430.                 break;
  2431.  
  2432.            default:               /* actual bank switch for all SVGA cards */
  2433.                 svga_loc=((unsigned long)svga_yincr) * y + x;
  2434.                 svga_page=(unsigned char)(svga_loc/gran);
  2435.                 svga_loc %= gran;
  2436.                 if (cur_page != svga_page)      /* if not in correct bank */
  2437.                 {
  2438.                     cur_page = svga_page;       /* set new working bank */
  2439.                     if (whichvga != VESA)
  2440.                     {
  2441.                         _disable();
  2442.                     newbank();
  2443.                         _enable();
  2444.                 }
  2445.                     else
  2446.                         newbank();
  2447.                 }
  2448.                 break;
  2449.         }
  2450.  
  2451. #ifdef DOS386
  2452.    fp = MK_FP(_x386_zero_base_selector, 0xA0000 + (unsigned int)svga_loc);
  2453. #else
  2454. #ifdef DOS16RM
  2455.    fp = display_base + (unsigned int)svga_loc;
  2456. #else
  2457.    fp = (unsigned char *)MK_FP(0xA000, (unsigned int)svga_loc);
  2458. #endif
  2459. #endif
  2460.    *fp = color;         /* write normalized pixel color val to bitplane */
  2461.  
  2462.    return;
  2463.    }
  2464.  
  2465. void newbank()          /* Perform SVGA bank switch on demand - Voila! */
  2466. {
  2467.     register unsigned char tmp_byte, tmp_byte1;
  2468.     register unsigned int tmp_word;
  2469.     union REGS regs;
  2470.     static unsigned char xlateT3[] = {0x40, 0x49, 0x52, 0x5B, 0x64, 0x6D,
  2471.             0x76, 0x7F};
  2472.     static unsigned char xlateT4[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
  2473.             0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
  2474.  
  2475.  
  2476.     switch (whichvga)
  2477.         {
  2478.         case VDC600:                    /* AT&T VDC 600 */
  2479.             tmp_byte = (unsigned char) (cur_page << 4); /* was >> 12... */
  2480.             outpw(0x03CE,0x050F);
  2481.             outp(0x03CE,0x09);
  2482.             outp(0x03CF, tmp_byte);
  2483.             break;
  2484.         case OAKTECH:                   /* Oak Technology OTI-067 */
  2485.             tmp_byte = (unsigned char)(cur_page & 0x0F);
  2486.             outp(0x3DF, (tmp_byte << 4) | tmp_byte);
  2487.             break;
  2488.         case AHEADA:                    /* Ahead Systems Ver A */
  2489.             outpw(0x3CE, 0x200F);       /* enable extended registers */
  2490.             tmp_byte = (unsigned char)(inp(0x3CC) & 0xDF);  /* bit 0 */
  2491.             if (cur_page & 1)
  2492.                     tmp_byte |= 0x20;
  2493.             outp(0x3C2, tmp_byte);
  2494.             outp(0x3CF, 0);                /* bits 1, 2, 3 */
  2495.             tmp_word = (unsigned int)((cur_page >> 1 )|(inp(0x3D0) & 0xF8));
  2496.             outpw(0x3CF, tmp_word << 8);
  2497.             break;
  2498.         case AHEADB:                    /* Ahead Systems Ver B */
  2499.             outpw(0x3CE, 0x200F);       /* enable extended registers */
  2500.             tmp_word = (unsigned int)((cur_page << 4) | cur_page);
  2501.             outpw(0x3CF, (tmp_word << 8) | 0x000D);
  2502.             break;
  2503.         case EVEREX:                    /* Everex SVGA's */
  2504.             outp(0x3C4, 8);
  2505.             if (cur_page & 1)
  2506.                  tmp_word = (unsigned int)(inp(0x3C5) | 0x80);
  2507.             else tmp_word = (unsigned int)(inp(0x3C5) & 0x7F);
  2508.             outpw(0x3C4, (tmp_word << 8) | 0x0008);
  2509.             tmp_byte = (unsigned char)(inp(0x3CC) & 0xDF);
  2510.             if (!(cur_page & 2))
  2511.                  tmp_byte |= 0x20;
  2512.             outp(0x3C2, tmp_byte);
  2513.             break;
  2514.         case ATIVGA:                    /* ATI VGA Wonder (and the XL) */
  2515.             outp(0x1CE, 0xB2);
  2516.             tmp_word = (unsigned int)((cur_page << 1) | (inp(0x1CF) & 0xE1));
  2517.             outpw(0x1CE, (tmp_word << 8) | 0x00B2);
  2518.             break;
  2519.         case TRIDENT:
  2520.             outp(0x3CE, 6);             /* set page size to 64K */
  2521.             tmp_word = (unsigned int)(inp(0x3CF) | 4) << 8;
  2522.             outpw(0x3CE, tmp_word | 0x0006);
  2523.             outp(0x3C4, 0x0b);          /* switch to BPS mode */
  2524.             inp(0x3C5);                 /* dummy read?? */
  2525.             tmp_word = (unsigned int)(cur_page ^ 2) << 8;
  2526.             outpw(0x3C4, tmp_word | 0x000E);
  2527.             break;
  2528.         case VIDEO7:                    /* Video-7 VRAM, FastRAM SVGA cards */
  2529.             tmp_byte1 = tmp_byte = (unsigned char)(cur_page & 0x0F);
  2530.             outpw(0x3C4, 0xEA06);
  2531.             tmp_word = (unsigned int)(tmp_byte & 1) << 8;
  2532.             outpw(0x3C4, tmp_word | 0x00F9);
  2533.             tmp_byte &= 0x0C;
  2534.             tmp_word = (unsigned int)((tmp_byte >> 2) | tmp_byte) << 8;
  2535.             outpw(0x3C4, tmp_word | 0x00F6);
  2536.             tmp_word |= (inp(0x3C5) & 0xF0) << 8;
  2537.             outpw(0x3C4, tmp_word | 0x00F6);
  2538.             tmp_byte = (unsigned char)((tmp_byte1 << 4) & 0x20);
  2539.             outp(0x3C2, (inp(0x3CC) & 0xDF) | tmp_byte);
  2540.             break;
  2541.         case CHIPSTECH:                 /* Chips & Technology VGA Chip Set */
  2542.             outp(0x3D7, cur_page << 2); /* this is all that's necessary?? */
  2543.             break;
  2544.         case PARADISE24X:               /* Diamond Speedstar 24X  */
  2545.         case PARADISE:                  /* Paradise, Professional, Plus */
  2546.             outpw(0x3CE, 0x050F);       /* turn off VGA reg. write protect */
  2547.             tmp_word = (unsigned int)(cur_page << 4) << 8;
  2548.             outpw(0x3CE, tmp_word | 0x0009);
  2549.             break;
  2550.         case TSENG3:                    /* Tseng 3000 - Orchid, STB, etc. */
  2551.             outp(0x3CD, xlateT3[cur_page & 0x07]);
  2552.             break;
  2553.         case TSENG4:                    /* Tseng 4000 - Orchid PD+, etc. */
  2554.             outp(0x3CD, xlateT4[cur_page & 0x0F]);
  2555.             break;
  2556.         case VESA:                      /* VESA standard mode bank switch */
  2557.             regs.x.ax = 0x4F05;         /* VESA BIOS bank switch call */
  2558.             regs.x.dx = cur_page;
  2559.             regs.x.bx = map_code;       /* Map code 0 or 1? */
  2560. #ifdef DOS386
  2561.             int86_real(0x10, ®s, ®s);
  2562. #else
  2563.             int86(0x10, ®s, ®s);  /* Do the video BIOS interrupt */
  2564. #endif
  2565.         }
  2566.     return;
  2567. }
  2568.  
  2569. void set_palette_register (Val, Red, Green, Blue)
  2570.    unsigned Val;
  2571.    unsigned Red, Green, Blue;
  2572.    {
  2573.    union REGS Regs;
  2574.  
  2575.    Regs.x.ax = 0x1010;              /* Set one palette register function */
  2576.    Regs.x.bx = Val;                 /* the palette register to set (color#)*/
  2577.    Regs.h.dh = (char)(Red & 0x3f);  /* set the gun values (6 bits ea.) */
  2578.    Regs.h.ch = (char)(Green & 0x3f);
  2579.    Regs.h.cl = (char)(Blue & 0x3f);
  2580.    int86(0x10, &Regs, &Regs);       /* Do the video interrupt */
  2581.    }
  2582.  
  2583. /* Conversion from Hue, Saturation, Value to Red, Green, and Blue and back */
  2584. /* From "Computer Graphics", Donald Hearn & M. Pauline Baker, p. 304 */
  2585.  
  2586. void hsv_to_rgb(hue, s, v, r, g, b)
  2587.    DBL hue, s, v;               /* hue (0.0-360.0) s and v from 0.0-1.0) */
  2588.    unsigned *r, *g, *b;         /* values from 0 to 63 */
  2589.    {
  2590.    register DBL i, f, p1, p2, p3;
  2591.    register DBL xh;
  2592.    register DBL nr, ng, nb;     /* rgb values of 0.0 - 1.0 */
  2593.  
  2594.    if (hue == 360.0)
  2595.       hue = 0.0;                /* (THIS LOOKS BACKWARDS BUT OK) */
  2596.  
  2597.    xh = hue / 60.0;             /* convert hue to be in 0,6     */
  2598.    i = floor(xh);               /* i = greatest integer <= h    */
  2599.    f = xh - i;                  /* f = fractional part of h     */
  2600.    p1 = v * (1 - s);
  2601.    p2 = v * (1 - (s * f));
  2602.    p3 = v * (1 - (s * (1 - f)));
  2603.  
  2604.    switch ((int) i)
  2605.       {
  2606.       case 0:
  2607.          nr = v;
  2608.          ng = p3;
  2609.          nb = p1;
  2610.          break;
  2611.       case 1:
  2612.          nr = p2;
  2613.          ng = v;
  2614.          nb = p1;
  2615.          break;
  2616.       case 2:
  2617.          nr = p1;
  2618.          ng = v;
  2619.          nb = p3;
  2620.          break;
  2621.       case 3:
  2622.          nr = p1;
  2623.          ng = p2;
  2624.          nb = v;
  2625.          break;
  2626.       case 4:
  2627.          nr = p3;
  2628.          ng = p1;
  2629.          nb = v;
  2630.          break;
  2631.       case 5:
  2632.          nr = v;
  2633.          ng = p1;
  2634.          nb = p2;
  2635.          break;
  2636.       default:
  2637.          nr = ng = nb = 0;
  2638.         }
  2639.  
  2640.    *r = (unsigned)(nr * 63.0); /* Normalize the values to 63 */
  2641.    *g = (unsigned)(ng * 63.0);
  2642.    *b = (unsigned)(nb * 63.0);
  2643.  
  2644.    return;
  2645.    }
  2646.  
  2647.  
  2648. void rgb_to_hsv(r, g, b, h, s, v)
  2649.    unsigned r, g, b;
  2650.    DBL *h, *s, *v;
  2651.    {
  2652.    register DBL m, r1, g1, b1;
  2653.    register DBL nr, ng, nb;             /* rgb values of 0.0 - 1.0 */
  2654.    register DBL nh = 0.0, ns, nv;       /* hsv local values */
  2655.  
  2656.    nr = (DBL) r / 255.0;
  2657.    ng = (DBL) g / 255.0;
  2658.    nb = (DBL) b / 255.0;
  2659.  
  2660.    nv = max (nr, max (ng, nb));
  2661.    m = min (nr, min (ng, nb));
  2662.  
  2663.    if (nv != 0.0)                /* if no value, it's black! */
  2664.       ns = (nv - m) / nv;
  2665.    else
  2666.       ns = 0.0;                  /* black = no colour saturation */
  2667.  
  2668.    if (ns == 0.0)                /* hue undefined if no saturation */
  2669.    {
  2670.       *h = 0.0;                  /* return black level (?) */
  2671.       *s = 0.0;
  2672.       *v = nv;
  2673.       return;
  2674.    }
  2675.  
  2676.    r1 = (nv - nr) / (nv - m);    /* distance of color from red   */
  2677.    g1 = (nv - ng) / (nv - m);    /* distance of color from green */
  2678.    b1 = (nv - nb) / (nv - m);    /* distance of color from blue  */
  2679.  
  2680.    if (nv == nr)
  2681.    {
  2682.       if (m == ng)
  2683.          nh = 5. + b1;
  2684.       else
  2685.          nh = 1. - g1;
  2686.    }
  2687.  
  2688.    if (nv == ng)
  2689.       {
  2690.       if (m == nb)
  2691.          nh = 1. + r1;
  2692.       else
  2693.          nh = 3. - b1;
  2694.       }
  2695.  
  2696.    if (nv == nb)
  2697.       {
  2698.       if (m == nr)
  2699.          nh = 3. + g1;
  2700.       else
  2701.          nh = 5. - r1;
  2702.       }
  2703.  
  2704.    *h = nh * 60.0;        /* return h converted to degrees */
  2705.    *s = ns;
  2706.    *v = nv;
  2707.    return;
  2708.    }
  2709.  
  2710. void box(x1, y1, x2, y2)  /* RHA's draw a thin white box funct. */
  2711. int x1, y1, x2, y2;
  2712. {
  2713.    int i;
  2714.    for (i = x1+1; i < x2; i++)
  2715.    {
  2716.         lastx = -1;  lasty = -1; lastline = -1;
  2717.         display_plot(i, y1, 255, 255, 255);
  2718.         lastx = -1;  lasty = -1; lastline = -1;
  2719.         display_plot(i, y2, 255, 255, 255);
  2720.    }
  2721.    for (i = y1; i <= y2; i++)
  2722.    {
  2723.         lastx = -1;  lasty = -1; lastline = -1;
  2724.         display_plot(x1, i, 255, 255, 255);
  2725.         lastx = -1;  lasty = -1; lastline = -1;
  2726.         display_plot(x2, i, 255, 255, 255);
  2727.    }
  2728.    lastx = -1;
  2729.    lasty = -1;
  2730.    lastline = -1;
  2731.    return;
  2732. }
  2733.  
  2734. #if !__STDC__
  2735. #if !defined(__TURBOC__) && !defined(__BORLANDC__) && !defined (__ZTC__)
  2736.  /* BCC has srand but doesn't set __STDC__ same as Zortech① */
  2737.   
  2738.  
  2739. /* ANSI Standard psuedo-random number generator */
  2740.  
  2741. int rand(void);
  2742. void srand(int);
  2743.  
  2744. static unsigned long int next = 1;
  2745.  
  2746. int rand()
  2747.    {
  2748.    next = next * 1103515245L + 12345L;
  2749.    return ((int) (next / 0x10000L) & 0x7FFF);
  2750.    }
  2751.  
  2752. void srand(seed)
  2753.    int seed;
  2754.    {
  2755.    next = (unsigned long int)seed;
  2756.    }
  2757.  
  2758. #endif
  2759. #endif
  2760.  
  2761.  
  2762. /* Math Error exception struct format:
  2763.         int type;               - exception type - see below
  2764.         char _far *name;        - name of function where error occured
  2765.         long double arg1;       - first argument to function
  2766.         long double arg2;       - second argument (if any) to function
  2767.         long double retval;     - value to be returned by function
  2768. */
  2769.  
  2770. #ifdef __WATCOMC__
  2771. #define EDOM    7       /* MSC is 33 */
  2772. #define ERANGE  8       /* MSC is 34 */
  2773.  
  2774. int matherr(e)
  2775.    struct exception *e;
  2776. #else
  2777. #ifdef GCCDOS
  2778. int matherr(e)
  2779.    struct libm_exception *e;
  2780. #else
  2781. int _cdecl matherr(e)
  2782.    struct exception *e;
  2783. #endif
  2784. #endif
  2785.    {
  2786.    if (Options & DEBUGGING) {
  2787.       /* Since we are just making pictures, not keeping nuclear power under
  2788.          control - it really isn't important if there is a minor math problem.
  2789.          This routine traps and ignores them.  Note: the most common one is
  2790.          a DOMAIN error coming out of "acos". */
  2791.       switch (e->type) {
  2792.          case DOMAIN   : printf("DOMAIN error in '%s'\n", e->name); break;
  2793.          case SING     : printf("SING   error in '%s'\n", e->name); break;
  2794.          case OVERFLOW : printf("OVERFLOW error in '%s'\n", e->name); break;
  2795.          case UNDERFLOW: printf("UNDERFLOW error in '%s'\n", e->name); break;
  2796.          case TLOSS    : printf("TLOSS error in '%s'\n", e->name); break;
  2797.          case PLOSS    : printf("PLOSS error in '%s'\n", e->name); break;
  2798. #ifdef EDOM
  2799.          case EDOM     : printf("EDOM error in '%s'\n", e->name); break;
  2800. #endif
  2801. #ifdef ERANGE
  2802.          case ERANGE   : printf("ERANGE error in '%s'\n", e->name); break;
  2803. #endif
  2804.          default       : printf("Unknown math error in '%s'\n",e->name);break;
  2805.          }
  2806.       }
  2807.    return (1);  /* Indicate the math error was corrected... */
  2808.    }
  2809.  
  2810. #ifdef GCCDOS
  2811. unsigned char *find_go32(go32_offset)
  2812.    int *go32_offset;
  2813.    {
  2814.    static unsigned char *memptr = (unsigned char *)0xE0000000;
  2815.    static unsigned char buf[0x1000];
  2816.    static unsigned char buffer[256];
  2817.  
  2818.    union REGS reg;
  2819.    unsigned char *p, *filename, *last_p;
  2820.    int occur = 0, indx;
  2821.  
  2822. /*  AAC - this old code used the current directory name as
  2823.     a search string, which proved to be not unique enough!
  2824.  
  2825.    reg.x.si = (int)buf;
  2826.    reg.h.ah = 0x47;
  2827.    int86(0x21, ®, ®);
  2828. */
  2829.    
  2830.    strcpy(buf, "tmXXXXXX"); /* make us up a VERY unique filename/search string! */
  2831.    filename = mktemp(buf);
  2832.  
  2833.    for (p = &memptr[0xA0000-1-strlen((char *)buf)]; p != memptr; p--) /* find it */
  2834.       if (buf[0] == *p && strcmp((char *)buf, (char *)p) == 0)
  2835.         {
  2836.             last_p = p;
  2837.             occur++;
  2838.         }
  2839.  
  2840.    if (!occur) { /* AAC - was "if (p == memptr)" - there should be more than 1 occurrence in mem!! */
  2841.       printf("Unable to locate the GO32 communication buffer, fatal error!\n");
  2842.       exit(1);
  2843.       }
  2844. /*  else printf("Found %d occurrences of string, last one = %p\n", occur, last_p); */
  2845.  
  2846.    reg.h.ah = 0x1A;
  2847.    reg.x.dx = (int)buf;
  2848.    int86(0x21, ®, ®);
  2849.  
  2850.    reg.h.ah = 0x4E;
  2851.    reg.x.dx = (int)"*.*";
  2852.    int86(0x21, ®, ®);
  2853.  
  2854.    getcwd(buffer, sizeof(buffer));        /* somehow this is affecting where we wind up at */
  2855.    indx = strlen(buffer);                /* get our cur. dir name len */
  2856.    if (indx < 2)                        /* bump count if in root (special case?) */
  2857.        indx = 2;
  2858.  
  2859.    *go32_offset = (reg.x.dx & 0xFFFF) - 43;    /* this is always right */
  2860.  
  2861.    return(last_p - 3 - indx);            /* this moves around of it's own will... :-( AAC */
  2862.    }
  2863. #endif
  2864.  
  2865. #ifdef _INTELC32_
  2866.  
  2867. void int10 ( void )         /* User Int 10h Protected-Mode Interrupt Handler */
  2868.    {
  2869.    _XSTACK *ebp;            /* Stack frame passed to real-mode handler */
  2870.    _XSTACK *ebx;            /* Stack frame returned to interrupted process */
  2871.  
  2872.    ebp = (_XSTACK *) _get_stk_frame();      /* Get stack frame address */
  2873.    if ((ebp->eax & 0x0000FFFE) != 0x4F00) { /* Check for function = 4F00/1h */
  2874.       (*prev_int10)();                      /* Call previous handler? */
  2875.       return;
  2876.       }
  2877.  
  2878.    if ((ebp->edi & 0xFFF00000) != 0) {
  2879.       ebx = (_XSTACK *) _get_ebx();/* Get return stack frame address */
  2880.       ebx->eax = 0x0000104F;       /* Return code of 0x10=Extended mem err */
  2881.       ebp->flg |= _FLAG_CARRY;     /* Set carry flag to indicate an error */
  2882.       ebp->opts |= _STK_NOINT;     /* Bypass real-mode handler */
  2883.       return;
  2884.       }
  2885.  
  2886.    /* Put the segment value from the physical address in the ES.  */
  2887.    ebp->es = (short)((ebp->edi & 0x000FFFF0) >> 4);
  2888.    ebp->edi &= 0x0000000F;         /* Offset value from physical address */
  2889.    ebp->opts = 0;                  /* Pass to real-mode handler */
  2890.    return;
  2891.    }
  2892.  
  2893. #endif
  2894.  
  2895. void print_ibm_credits()
  2896.   {
  2897.   fprintf (stderr," \n");
  2898.  
  2899.   fprintf (stderr,"חלללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללל†\n");
  2900.   box_fprintf (stderr,"  Persistence of Vision Raytracer Ver %s%s",POV_RAY_VERSION,COMPILER_VER);
  2901.   box_fprintf (stderr,"    %s",DISTRIBUTION_MESSAGE_1);
  2902.   box_fprintf (stderr,"     %s",DISTRIBUTION_MESSAGE_2);
  2903.   box_fprintf (stderr,"     %s",DISTRIBUTION_MESSAGE_3);
  2904.   fprintf (stderr,"´  Copyright (c) 1993 POV-Team                                           ´\n");
  2905.   fprintf (stderr,"וגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגÀ\n");
  2906.   fprintf (stderr,"´  POV-Ray is based on DKBTrace 2.12 by David K. Buck & Aaron A. Collins.´\n");
  2907.   fprintf (stderr,"´    Contributing Authors: (Alphabetically)                              ´\n");
  2908.   fprintf (stderr,"´      Steve Anger        Steve A. Bennett   David K. Buck               ´\n");
  2909.   fprintf (stderr,"´      Aaron A. Collins   Alexander Enzmann  Dan Farmer                  ´\n");
  2910.   fprintf (stderr,"´      Douglas Muir       Bill Pulver        Robert Skinner              ´\n");
  2911.   fprintf (stderr,"´      Scott Taylor       Drew Wells         Chris Young                 ´\n");
  2912.   fprintf (stderr,"´    Other contributors listed in the documentation.-                    ´\n");
  2913.   fprintf (stderr,"זלללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללל¶");
  2914.   fflush (stderr);
  2915.   }
  2916.  
  2917. /* Stats kept by the ray tracer: */
  2918. extern long Number_Of_Pixels, Number_Of_Rays, Number_Of_Pixels_Supersampled;
  2919. extern long Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded;
  2920. extern long Ray_Box_Tests, Ray_Box_Tests_Succeeded;
  2921. extern long Ray_Blob_Tests, Ray_Blob_Tests_Succeeded;
  2922. extern long Ray_Cone_Tests, Ray_Cone_Tests_Succeeded;
  2923. extern long Ray_Disc_Tests, Ray_Disc_Tests_Succeeded;
  2924. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  2925. extern long Ray_Triangle_Tests, Ray_Triangle_Tests_Succeeded;
  2926. extern long Ray_Quadric_Tests, Ray_Quadric_Tests_Succeeded;
  2927. extern long Ray_Poly_Tests, Ray_Poly_Tests_Succeeded;
  2928. extern long Ray_Bicubic_Tests, Ray_Bicubic_Tests_Succeeded;
  2929. extern long Ray_Ht_Field_Tests, Ray_Ht_Field_Tests_Succeeded;
  2930. extern long Ray_Ht_Field_Box_Tests, Ray_HField_Box_Tests_Succeeded;
  2931. extern long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  2932. extern long Clipping_Region_Tests, Clipping_Region_Tests_Succeeded;
  2933. extern long Calls_To_Noise, Calls_To_DNoise;
  2934. extern long Shadow_Ray_Tests, Shadow_Rays_Succeeded, Shadow_Cache_Hits;
  2935. extern long Reflected_Rays_Traced, Refracted_Rays_Traced;
  2936. extern long Transmitted_Rays_Traced, Istack_overflows;
  2937. extern time_t tstart, tstop;
  2938. extern DBL tused;
  2939. extern FRAME Frame;
  2940.  
  2941. #ifdef DB_CODE
  2942. extern unsigned long nChecked, nEnqueued;
  2943. extern unsigned long Project_Tests, Project_Tests_Succeeded;
  2944. extern unsigned long totalQueues;
  2945. extern unsigned long totalQueueResets;
  2946. #endif
  2947.  
  2948. extern char Input_File_Name[FILE_NAME_LENGTH], Output_File_Name[FILE_NAME_LENGTH], Stat_File_Name[FILE_NAME_LENGTH];
  2949.  
  2950. void print_ibm_stats()
  2951.    {
  2952.    long hours,min;
  2953.    DBL sec;
  2954.    FILE *stat_out;
  2955.    long Pixels_In_Image;
  2956.  
  2957.    if (Options & VERBOSE_FILE)
  2958.       stat_out = fopen(Stat_File_Name,"w+t");
  2959.    else
  2960.     stat_out = stdout;
  2961.  
  2962.    Pixels_In_Image = (long)Frame.Screen_Width * (long)Frame.Screen_Height;
  2963.  
  2964.    fprintf (stat_out,"\n");
  2965.    fprintf (stat_out,"חלללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללל†\n");
  2966.    box_fprintf (stat_out,"  Persistence of Vision Raytracer Ver %s%s",POV_RAY_VERSION, COMPILER_VER);
  2967.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  2968.    box_fprintf (stat_out,"  %s statistics",Input_File_Name);
  2969.    if(Pixels_In_Image > Number_Of_Pixels)
  2970.    box_fprintf (stat_out,"  Partial Image Rendered");
  2971.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  2972.    box_fprintf (stat_out," Image Resolution %d pixels wide x %d pixels high",Frame.Screen_Width, Frame.Screen_Height);
  2973.    box_fprintf (stat_out," # Rays Calculated     :  %10ld",Number_Of_Rays);
  2974.    box_fprintf (stat_out," # Pixels Calculated   :  %10ld", Number_Of_Pixels);
  2975.    box_fprintf (stat_out," # Pixels Supersampled :  %10ld",Number_Of_Pixels_Supersampled);
  2976.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  2977.    box_fprintf (stat_out,"   Ray -» Shape Intersection Tests");
  2978.    box_fprintf (stat_out,"   Type             Tests    Succeeded   Percentage");
  2979.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  2980.    if(Ray_Sphere_Tests)
  2981.    box_fprintf (stat_out,"  Sphere       %10ld  %10ld  %10.2f", Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded, ( ((DBL)Ray_Sphere_Tests_Succeeded/(DBL)Ray_Sphere_Tests) *100.0 ) );
  2982.    if(Ray_Plane_Tests)
  2983.    box_fprintf (stat_out,"  Plane        %10ld  %10ld  %10.2f", Ray_Plane_Tests, Ray_Plane_Tests_Succeeded, ( ((DBL)Ray_Plane_Tests_Succeeded/(DBL)Ray_Plane_Tests) *100.0 ));
  2984.    if(Ray_Triangle_Tests)
  2985.    box_fprintf (stat_out,"  Triangle     %10ld  %10ld  %10.2f", Ray_Triangle_Tests, Ray_Triangle_Tests_Succeeded, ( ((DBL)Ray_Triangle_Tests_Succeeded/(DBL)Ray_Triangle_Tests) *100.0 ));
  2986.    if(Ray_Quadric_Tests)
  2987.    box_fprintf (stat_out,"  Quadric      %10ld  %10ld  %10.2f", Ray_Quadric_Tests, Ray_Quadric_Tests_Succeeded, ( ((DBL)Ray_Quadric_Tests_Succeeded/(DBL)Ray_Quadric_Tests) *100.0 ));
  2988.    if(Ray_Blob_Tests)
  2989.    box_fprintf (stat_out,"  Blob         %10ld  %10ld  %10.2f", Ray_Blob_Tests, Ray_Blob_Tests_Succeeded, ( ((DBL)Ray_Blob_Tests_Succeeded/(DBL)Ray_Blob_Tests) *100.0 ));
  2990.    if(Ray_Box_Tests)
  2991.    box_fprintf (stat_out,"  Box          %10ld  %10ld  %10.2f", Ray_Box_Tests, Ray_Box_Tests_Succeeded, ( ((DBL)Ray_Box_Tests_Succeeded/(DBL)Ray_Box_Tests) *100.0 ));
  2992.    if(Ray_Cone_Tests)
  2993.    box_fprintf (stat_out,"  Cone\\Cyl.    %10ld  %10ld  %10.2f", Ray_Cone_Tests, Ray_Cone_Tests_Succeeded, ( ((DBL)Ray_Cone_Tests_Succeeded/(DBL)Ray_Cone_Tests) *100.0 ));
  2994.    if(Ray_Disc_Tests)
  2995.    box_fprintf (stat_out,"  Disc         %10ld  %10ld  %10.2f", Ray_Disc_Tests, Ray_Disc_Tests_Succeeded, ( ((DBL)Ray_Disc_Tests_Succeeded/(DBL)Ray_Disc_Tests) *100.0 ));
  2996.    if(Ray_Poly_Tests)
  2997.    box_fprintf (stat_out,"  Quartic\\Poly %10ld  %10ld  %10.2f", Ray_Poly_Tests, Ray_Poly_Tests_Succeeded, ( ((DBL)Ray_Poly_Tests_Succeeded/(DBL)Ray_Poly_Tests) *100.0 ));
  2998.    if(Ray_Bicubic_Tests)
  2999.    box_fprintf (stat_out,"  Bezier Patch %10ld  %10ld  %10.2f", Ray_Bicubic_Tests, Ray_Bicubic_Tests_Succeeded, ( ((DBL)Ray_Bicubic_Tests_Succeeded/(DBL)Ray_Bicubic_Tests) *100.0 ));
  3000.    if(Ray_Ht_Field_Tests)
  3001.    box_fprintf (stat_out,"  Height Fld   %10ld  %10ld  %10.2f", Ray_Ht_Field_Tests, Ray_Ht_Field_Tests_Succeeded, ( ((DBL)Ray_Ht_Field_Tests_Succeeded/(DBL)Ray_Ht_Field_Tests) *100.0 ));
  3002.    if(Ray_Ht_Field_Box_Tests)
  3003.    box_fprintf (stat_out,"  Hght Fld Box %10ld  %10ld  %10.2f", Ray_Ht_Field_Box_Tests, Ray_HField_Box_Tests_Succeeded, ( ((DBL)Ray_HField_Box_Tests_Succeeded/(DBL)Ray_Ht_Field_Box_Tests) *100.0 ));
  3004.    if(Bounding_Region_Tests)
  3005.    box_fprintf (stat_out,"  Bounds       %10ld  %10ld  %10.2f", Bounding_Region_Tests, Bounding_Region_Tests_Succeeded, ( ((DBL)Bounding_Region_Tests_Succeeded/(DBL)Bounding_Region_Tests) *100.0 ));
  3006.    if(Clipping_Region_Tests)
  3007.    box_fprintf (stat_out,"  Clips        %10ld  %10ld  %10.2f", Clipping_Region_Tests, Clipping_Region_Tests_Succeeded, ( ((DBL)Clipping_Region_Tests_Succeeded/(DBL)Clipping_Region_Tests) *100.0 ));
  3008.    if(nChecked)
  3009. #ifdef DB_CODE
  3010.    box_fprintf (stat_out,"  Slabs        %10ld  %10ld  %10.2f", nChecked, nEnqueued, ( ((DBL)nEnqueued/(DBL)nChecked) *100.0 ));
  3011.    if(Project_Tests)
  3012.    box_fprintf (stat_out,"  Projections  %10ld  %10ld  %10.2f", Project_Tests, Project_Tests_Succeeded, ( ((DBL)Project_Tests_Succeeded/(DBL)Project_Tests) *100.0 ));
  3013. #endif
  3014.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  3015.    if(Calls_To_Noise)
  3016.    box_fprintf (stat_out,  "  Calls to Noise Routine  :  %10ld", Calls_To_Noise);
  3017.    if(Calls_To_DNoise)
  3018.    box_fprintf (stat_out,  "  Calls to DNoise Routine :  %10ld", Calls_To_DNoise);
  3019.    fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  3020.    if(Shadow_Ray_Tests){
  3021.      box_fprintf (stat_out,"  Shadow Ray Tests        :  %10ld",Shadow_Ray_Tests);
  3022. /* SJA */
  3023.      box_fprintf (stat_out,"  Shadow Cache Hits       :  %10ld",Shadow_Cache_Hits);
  3024. /* SJA */
  3025.      box_fprintf (stat_out,"  Blocking Objects Found  :  %10ld",Shadow_Rays_Succeeded);
  3026.      }
  3027.    if(Reflected_Rays_Traced)
  3028.    box_fprintf (stat_out,  "  Reflected Rays          :  %10ld", Reflected_Rays_Traced);
  3029.    if(Refracted_Rays_Traced)
  3030.    box_fprintf (stat_out,  "  Refracted Rays          :  %10ld", Refracted_Rays_Traced);
  3031.    if(Transmitted_Rays_Traced)
  3032.    box_fprintf (stat_out,  "  Transmitted Rays        :  %10ld", Transmitted_Rays_Traced);
  3033.    if(Istack_overflows)
  3034.    box_fprintf (stat_out,  "  I-Stack overflows       :  %10ld", Istack_overflows);
  3035.  
  3036.    if(tused==0) {
  3037.     STOP_TIME                 /* Get trace done time. */
  3038.     tused = TIME_ELAPSED      /* Calc. elapsed time. Define TIME_ELAPSED as */
  3039.                   /* 0 in your specific CONFIG.H if unsupported */
  3040.     }
  3041.    if (tused != 0){
  3042.      /* Convert seconds to hours, min & sec. CdW */
  3043.      hours = (long) tused/3600;
  3044.      min = (long) (tused - hours*3600)/60;
  3045.      sec = tused - (DBL) (hours*3600 + min*60);
  3046.      fprintf (stat_out,"´גגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגגג´\n");
  3047. #ifdef DB_CODE
  3048.      box_fprintf (stat_out,"  Rendering Time          :   %5ld h %2d min %4.2f sec (%.2f sec)", hours,min,sec,tused);
  3049. #else
  3050.      box_fprintf (stat_out,"  Rendering Time          :   %5ld h %2d min %4.2f sec", hours,min,sec);
  3051. #endif
  3052.      }
  3053.      fprintf (stat_out,"זלללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללללל¶\n");
  3054.  
  3055.     if (Options & VERBOSE_FILE)
  3056.       fclose(stat_out);
  3057.  
  3058. }
  3059.  
  3060.  
  3061. /* Output a string to stream with vfprintf formatted for ibm stat screen. */
  3062. /* String should be <80 chars                 */
  3063. int _cdecl box_fprintf( FILE *stream, char *fmt , ... )
  3064. {
  3065.     va_list marker;
  3066.     char buffer[160];
  3067.     int len,i;
  3068.  
  3069.     buffer[0] = '´';
  3070.     va_start( marker, fmt );
  3071.     vsprintf( buffer+1, fmt, marker );
  3072.     va_end( marker );
  3073.     len = strlen (buffer);
  3074.     for(i=len;i<73;i++)
  3075.        buffer[i] = ' ';
  3076.     strcpy (buffer+73,"´\n");
  3077.     return( fputs(buffer,stream) );
  3078. }
  3079.  
  3080. /*
  3081.  * DJGCC's setvbuf() is totally screwed up.  Don't use it;
  3082.  * at least not until a later version than DJ 1.10/GCC 2.4.1.
  3083.  * Or if you want to live dangerously define SVBOK to say that
  3084.  * setvbuf is ok.
  3085.  */
  3086.  
  3087. #ifdef GCCDOS
  3088. #ifndef SVBOK
  3089. int setvbuf(file, buffer, mode, size)   /* replace theirs with ours */
  3090. FILE *file;
  3091. char *buffer;
  3092. int mode, size;
  3093. {
  3094.     return (0);     /* dummy up general success and return */
  3095. }
  3096. #endif
  3097. #endif
  3098.  
  3099.