home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / graphics / povsrc20 / ibm.c < prev    next >
Text File  |  1993-09-28  |  120KB  |  3,002 lines

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