home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVSVGA.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  24KB  |  836 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevsvga.c */
  20. /* SuperVGA display drivers for Ghostscript */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxarith.h"            /* for ...log2 */
  25. #include "gxdevice.h"
  26. #include "gdevpccm.h"
  27. #include "gdevpcfb.h"
  28. #include "gdevsvga.h"
  29.  
  30. /* Define the following symbol to use a fixed color table. */
  31. /*#define FIXED_COLORS*/
  32.  
  33. #ifdef FIXED_COLORS
  34.  
  35. #define first_dc_index 256
  36.  
  37. #else                    /* !FIXED_COLORS */
  38.  
  39. /* The color map for dynamically assignable colors. */
  40. #define first_dc_index 64
  41. private int next_dc_index;
  42. #define dc_hash_size 293    /* prime, >num_dc */
  43. typedef struct { ushort rgb, index; } dc_entry;
  44. private dc_entry dynamic_colors[dc_hash_size + 1];
  45.  
  46. #endif                    /* (!)FIXED_COLORS */
  47.  
  48. /* Macro for casting gx_device argument */
  49. #define fb_dev ((gx_device_svga *)dev)
  50.  
  51. /* Procedure records */
  52. #define svga_procs_with_map(open, map_rgb_color) {\
  53.     open, gx_default_get_initial_matrix,\
  54.     gx_default_sync_output, gx_default_output_page, svga_close,\
  55.     map_rgb_color, svga_map_color_rgb,\
  56.     svga_fill_rectangle, gx_default_tile_rectangle,\
  57.     svga_copy_mono, svga_copy_color, gx_default_draw_line,\
  58.     svga_get_bits, gx_default_get_params, gx_default_put_params\
  59. }
  60. #define svga_procs(open)\
  61.   svga_procs_with_map(open, svga_map_rgb_color)
  62.  
  63. /* Save the controller mode */
  64. private int svga_save_mode = -1;
  65.  
  66. /* ------ Internal routines ------ */
  67.  
  68. #define regen 0xa000
  69.  
  70. /* Construct a pointer for writing a pixel. */
  71. /* Assume 64K pages, 64K granularity. */
  72. /* We know that y is within bounds. */
  73. #define set_pixel_ptr(ptr, fbdev, x, y, wnum)\
  74. {    ulong index = (ulong)(y) * fbdev->raster + (uint)(x);\
  75.     if ( (uint)(index >> 16) != fbdev->current_page )\
  76.        {    (*fbdev->set_page)(fbdev, (fbdev->current_page = index >> 16), wnum);\
  77.        }\
  78.     ptr = (fb_ptr)MK_PTR(regen, (ushort)index);\
  79. }
  80. #define set_pixel_write_ptr(ptr, fbdev, x, y)\
  81.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_write)
  82. #define set_pixel_read_ptr(ptr, fbdev, x, y)\
  83.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_read)
  84.  
  85. /* Find the graphics mode for a desired width and height. */
  86. /* Set the mode in the device structure and return 0, */
  87. /* or return an error code. */
  88. int
  89. svga_find_mode(gx_device *dev, const mode_info _ds *mip)
  90. {    for ( ; ; mip++ )
  91.     {    if ( mip->width >= fb_dev->width &&
  92.              mip->height >= fb_dev->height ||
  93.              mip[1].mode < 0
  94.            )
  95.         {    fb_dev->mode = mip;
  96.             gx_device_adjust_resolution(dev, mip->width, mip->height, 1);
  97.             fb_dev->raster = fb_dev->width;
  98.             return 0;
  99.         }
  100.     }
  101.     return_error(gs_error_rangecheck);
  102. }
  103.  
  104. /* Set the index for writing into the color DAC. */
  105. #define svga_dac_set_write_index(i) outportb(0x3c8, i)
  106.  
  107. /* Write 6-bit R,G,B values into the color DAC. */
  108. #define svga_dac_write(r, g, b)\
  109.   (outportb(0x3c9, r), outportb(0x3c9, g), outportb(0x3c9, b))
  110.  
  111. /* ------ Common procedures ------ */
  112.  
  113. #define cv_bits(v,n) (v >> (gx_color_value_bits - n))
  114.  
  115. #ifdef FIXED_COLORS
  116.  
  117. /* (dummy) Initialize the dynamic color table. */
  118. void
  119. svga_init_colors()
  120. {
  121. }
  122.  
  123. /* Load the color DAC with the predefined colors. */
  124. private void
  125. svga_load_colors()
  126. {    gx_color_index ci;
  127.     gx_color_value rgb[3];
  128.     for ( ci = 0; ci < 256; ci++ )
  129.     {    pc_8bit_map_color_rgb(NULL, ci, rgb);
  130.         svga_dac_set_write_index(i);
  131.         svga_dac_write(cv_bits(rgb[0], 6), cv_bits(rgb[1], 6),
  132.                    cv_bits(rgb[2], 6));
  133.     }
  134. }
  135.  
  136. #else                    /* !FIXED_COLORS */
  137.  
  138. /* Initialize the dynamic color table. */
  139. void
  140. svga_init_colors()
  141. {    memset(dynamic_colors, 0, (dc_hash_size + 1) * sizeof(dc_entry));
  142.     next_dc_index = first_dc_index;
  143. }
  144.  
  145. /* Load the color DAC with the predefined colors. */
  146. private void
  147. svga_load_colors()
  148. {    int c;
  149.     svga_dac_set_write_index(0);
  150.     for ( c = 0; c < 64; c++ )
  151.     {    static const byte c2[10] =
  152.            { 0, 42, 0, 0, 0, 0, 0, 0, 21, 63 };
  153.         svga_dac_write(c2[(c >> 2) & 9], c2[(c >> 1) & 9],
  154.                    c2[c & 9]);
  155.     }
  156. }
  157.  
  158. #endif                    /* (!)FIXED_COLORS */
  159.  
  160. /* Initialize the device structure and the DACs. */
  161. int
  162. svga_open(gx_device *dev)
  163. {    fb_dev->x_pixels_per_inch =
  164.       fb_dev->y_pixels_per_inch =
  165.         fb_dev->height / PAGE_HEIGHT_INCHES;
  166.     /* Set the display mode. */
  167.     if ( svga_save_mode < 0 )
  168.         svga_save_mode = (*fb_dev->get_mode)();
  169.     (*fb_dev->set_mode)(fb_dev->mode->mode);
  170.     svga_load_colors();
  171.     svga_init_colors();
  172.     fb_dev->current_page = -1;
  173.     return 0;
  174. }
  175.  
  176. /* Close the device; reinitialize the display for text mode. */
  177. int
  178. svga_close(gx_device *dev)
  179. {    if ( svga_save_mode >= 0 )
  180.         (*fb_dev->set_mode)(svga_save_mode);
  181.     svga_save_mode = -1;
  182.     return 0;
  183. }
  184.  
  185. #ifdef FIXED_COLORS
  186.  
  187. /* Map a r-g-b color to a palette index. */
  188. /* The first 64 entries of the color map are set */
  189. /* for compatibility with the older display modes: */
  190. /* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
  191. /* This requires some bizarre contortions! */
  192. gx_color_index
  193. svga_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  194.   gx_color_value b)
  195. {    gx_color_index ci = pc_8bit_map_rgb_color(dev, r, g, b);
  196.     /* Here is where we should permute the index to match */
  197.     /* the old color map... but we don't yet. */
  198.     return ci;
  199. }
  200.  
  201. #else                    /* !FIXED_COLORS */
  202.  
  203. /* Map a r-g-b color to a palette index. */
  204. /* The first 64 entries of the color map are set */
  205. /* for compatibility with the older display modes: */
  206. /* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
  207. gx_color_index
  208. svga_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  209.   gx_color_value b)
  210. {    ushort r5 = cv_bits(r, 5), g5 = cv_bits(g, 5), b5 = cv_bits(b, 5);
  211.     static const byte cube_bits[32] =
  212.        {    0, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  213.         8, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  214.         1, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  215.         9
  216.        };
  217.     uint cx = ((uint)cube_bits[r5] << 2) + ((uint)cube_bits[g5] << 1) +
  218.           (uint)cube_bits[b5];
  219.     ushort rgb;
  220.     register dc_entry _ds *pdc;
  221.     /* Check for a color on the cube. */
  222.     if ( cx < 64 ) return (gx_color_index)cx;
  223.     /* Not on the cube, check the dynamic color table. */
  224.     rgb = (r5 << 10) + (g5 << 5) + b5;
  225.     for ( pdc = &dynamic_colors[rgb % dc_hash_size]; pdc->rgb != 0; pdc++ )
  226.     {    if ( pdc->rgb == rgb )
  227.             return (gx_color_index)(pdc->index);
  228.     }
  229.     if ( pdc == &dynamic_colors[dc_hash_size] )
  230.     {    /* Wraparound */
  231.         for ( pdc = &dynamic_colors[0]; pdc->rgb != 0; pdc++ )
  232.         {    if ( pdc->rgb == rgb )
  233.                 return (gx_color_index)(pdc->index);
  234.         }
  235.     }
  236.     if ( next_dc_index == 256 )
  237.     {    /* No space left, report failure. */
  238.         return gx_no_color_index;
  239.     }
  240.     /* Not on the cube, and not in the dynamic table. */
  241.     /* Put in the dynamic table if space available. */
  242.     {    int i = next_dc_index++;
  243.         pdc->rgb = rgb;
  244.         pdc->index = i;
  245.         svga_dac_set_write_index(i);
  246.         svga_dac_write(cv_bits(r, 6), cv_bits(g, 6), cv_bits(b, 6));
  247.         return (gx_color_index)i;
  248.     }
  249. }
  250.  
  251. #endif                    /* (!)FIXED_COLORS */
  252.  
  253. /* Map a color code to r-g-b. */
  254. /* This routine must invert the transformation of the one above. */
  255. /* Since this is practically never used, we just read the DAC. */
  256. int
  257. svga_map_color_rgb(gx_device *dev, gx_color_index color,
  258.   gx_color_value prgb[3])
  259. {    uint cval;
  260.     outportb(0x3c7, (byte)color);
  261. #define dacin() (cval = inportb(0x3c9) >> 1,\
  262.   ((cval << 11) + (cval << 6) + (cval << 1) + (cval >> 4)) >>\
  263.    (16 - gx_color_value_bits))
  264.     prgb[0] = dacin();
  265.     prgb[1] = dacin();
  266.     prgb[2] = dacin();
  267. #undef dacin
  268.     return 0;
  269. }
  270.  
  271. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  272. /* Color = gx_no_color_index means transparent (no effect on the image). */
  273. int
  274. svga_copy_mono(gx_device *dev,
  275.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  276.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
  277. {    uint raster = fb_dev->raster;
  278.     ushort limit;
  279.     register int wi;
  280.     uint skip;
  281.     int yi;
  282.     register fb_ptr ptr = (fb_ptr)0;
  283.     const byte *srow;
  284.     uint invert;
  285.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  286.     limit = (ushort)-w;
  287.     skip = raster - w + 1;
  288.     srow = base + (sourcex >> 3);
  289. #define izero (int)czero
  290. #define ione (int)cone
  291.     if ( ione == no_color )
  292.     {    gx_color_index temp;
  293.         if ( izero == no_color ) return 0;    /* no-op */
  294.         temp = czero;
  295.         czero = cone;
  296.         cone = temp;
  297.         invert = ~0;
  298.     }
  299.     else
  300.         invert = 0;
  301.     /* Pre-filling saves us a test in the loop, */
  302.     /* and since tiling is uncommon, we come out ahead. */
  303.     if ( izero != no_color )
  304.         svga_fill_rectangle(dev, x, y, w, h, czero);
  305.     for ( yi = 0; yi < h; yi++ )
  306.        {    const byte *sptr = srow;
  307.         uint bits;
  308.         int bitno = sourcex & 7;
  309.         wi = w;
  310.         if ( PTR_OFF(ptr) <= skip )
  311.         {    set_pixel_write_ptr(ptr, fb_dev, x, y + yi);
  312.         }
  313.         else if ( PTR_OFF(ptr) > limit )
  314.         {    /* We're crossing a page boundary. */
  315.             /* This is extremely rare, so it doesn't matter */
  316.             /* how slow it is. */
  317.             int xi = (ushort)-PTR_OFF(ptr);
  318.             svga_copy_mono(dev, srow, sourcex & 7, sraster,
  319.                 gx_no_bitmap_id, x, y + yi, xi, 1,
  320.                 gx_no_color_index, cone);
  321.             set_pixel_write_ptr(ptr, fb_dev, x + xi, y + yi);
  322.             sptr = srow - (sourcex >> 3) + ((sourcex + xi) >> 3);
  323.             bitno = (sourcex + xi) & 7;
  324.             wi -= xi;
  325.         }
  326.         bits = *sptr ^ invert;
  327.         switch ( bitno )
  328.         {
  329. #define ifbit(msk)\
  330.   if ( bits & msk ) *ptr = (byte)ione;\
  331.   if ( !--wi ) break; ptr++
  332.         case 0:
  333. bit0:            ifbit(0x80);
  334.         case 1:
  335.             ifbit(0x40);
  336.         case 2:
  337.             ifbit(0x20);
  338.         case 3:
  339.             ifbit(0x10);
  340.         case 4:
  341.             ifbit(0x08);
  342.         case 5:
  343.             ifbit(0x04);
  344.         case 6:
  345.             ifbit(0x02);
  346.         case 7:
  347.             ifbit(0x01);
  348. #undef ifbit
  349.             bits = *++sptr ^ invert;
  350.             goto bit0;
  351.         }
  352.         ptr += skip;
  353.         srow += sraster;
  354.        }
  355. #undef izero
  356. #undef ione
  357.     return 0;
  358. }
  359.  
  360. /* Copy a color pixelmap.  This is just like a bitmap, */
  361. /* except that each pixel takes 8 bits instead of 1. */
  362. int
  363. svga_copy_color(gx_device *dev,
  364.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  365.   int x, int y, int w, int h)
  366. {    int xi, yi;
  367.     int skip;
  368.     const byte *sptr;
  369.     fb_ptr ptr;
  370.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  371.     skip = sraster - w;
  372.     sptr = base + sourcex;
  373.     for ( yi = y; yi - y < h; yi++ )
  374.        {    ptr = 0;
  375.         for ( xi = x; xi - x < w; xi++ )
  376.            {    if ( PTR_OFF(ptr) == 0 )
  377.                 set_pixel_write_ptr(ptr, fb_dev, xi, yi);
  378.             *ptr++ = *sptr++;
  379.            }
  380.         sptr += skip;
  381.        }
  382.     return 0;
  383. }
  384.  
  385. /* Fill a rectangle. */
  386. int
  387. svga_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  388.   gx_color_index color)
  389. {    uint raster = fb_dev->raster;
  390.     ushort limit = (ushort)-raster;
  391.     int yi;
  392.     fb_ptr ptr;
  393.     fit_fill(dev, x, y, w, h);
  394.     set_pixel_write_ptr(ptr, fb_dev, x, y);
  395.     /* Most fills are very small and don't cross a page boundary. */
  396.     yi = h;
  397.     switch ( w )
  398.        {
  399.     case 0: return 0;        /* no-op */
  400.     case 1:
  401.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  402.             ptr[0] = (byte)color,
  403.             ptr += raster;
  404.         if ( !++yi ) return 0;
  405.         break;
  406.     case 2:
  407.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  408.             ptr[0] = ptr[1] = (byte)color,
  409.             ptr += raster;
  410.         if ( !++yi ) return 0;
  411.         break;
  412.     case 3:
  413.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  414.             ptr[0] = ptr[1] = ptr[2] = (byte)color,
  415.             ptr += raster;
  416.         if ( !++yi ) return 0;
  417.         break;
  418.     case 4:
  419.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  420.             ptr[0] = ptr[1] = ptr[2] = ptr[3] = (byte)color,
  421.             ptr += raster;
  422.         if ( !++yi ) return 0;
  423.         break;
  424.     default:
  425.         if ( w < 0 ) return 0;
  426.         /* Check for erasepage. */
  427.         if ( w == dev->width && h == dev->height &&
  428.              color < first_dc_index
  429.            )
  430.             svga_init_colors();
  431.        }
  432.     while ( --yi >= 0 )
  433.        {    if ( PTR_OFF(ptr) < limit )
  434.            {    memset(ptr, (byte)color, w);
  435.             ptr += raster;
  436.            }
  437.         else if ( PTR_OFF(ptr) <= (ushort)(-w) )
  438.            {    memset(ptr, (byte)color, w);
  439.             if ( yi > 0 )
  440.                 set_pixel_write_ptr(ptr, fb_dev, x, y + h - yi);
  441.            }
  442.         else
  443.            {    uint left = (uint)0x10000 - PTR_OFF(ptr);
  444.             memset(ptr, (byte)color, left);
  445.             set_pixel_write_ptr(ptr, fb_dev, x + left, y + h - 1 - yi);
  446.             memset(ptr, (byte)color, w - left);
  447.             ptr += raster - left;
  448.            }
  449.        }
  450.     return 0;
  451. }
  452.  
  453. /* Read scan lines back from the frame buffer. */
  454. int
  455. svga_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  456. {    uint bytes_per_row = dev->width;
  457.     byte *dest = data;
  458.     ushort limit = (ushort)-bytes_per_row;
  459.     fb_ptr src;
  460.     if ( y < 0 || y >= dev->height )
  461.         return gs_error_rangecheck;
  462.     set_pixel_read_ptr(src, fb_dev, 0, y);
  463.     /* The logic here is similar to fill_rectangle. */
  464.     if ( PTR_OFF(src) <= limit )
  465.         memcpy(data, src, bytes_per_row);
  466.     else
  467.        {    uint left = (uint)0x10000 - PTR_OFF(src);
  468.         memcpy(data, src, left);
  469.         set_pixel_read_ptr(src, fb_dev, left, y);
  470.         memcpy(data + left, src, bytes_per_row - left);
  471.        }
  472.     if ( actual_data != 0 )
  473.         *actual_data = data;
  474.     return 0;
  475. }
  476.  
  477. /* ------ The VESA device ------ */
  478.  
  479. private dev_proc_open_device(vesa_open);
  480. private gx_device_procs vesa_procs = svga_procs(vesa_open);
  481. int vesa_get_mode(P0());
  482. void vesa_set_mode(P1(int));
  483. private void vesa_set_page(P3(gx_device_svga *, int, int));
  484. gx_device_svga gs_vesa_device =
  485.     svga_device(vesa_procs, "vesa", vesa_get_mode, vesa_set_mode, vesa_set_page);
  486.  
  487. /* Define the structures for information returned by the BIOS. */
  488. #define bits_include(a, m) !(~(a) & (m))
  489. /* Information about the BIOS capabilities. */
  490. typedef struct {
  491.     byte vesa_signature[4];        /* "VESA" */
  492.     ushort vesa_version;
  493.     char *product_info;        /* product name string */
  494.     byte capabilities[4];        /* (undefined) */
  495.     ushort *mode_list;        /* supported video modes, -1 ends */
  496. } vga_bios_info;
  497. /* Information about an individual VESA mode. */
  498. typedef enum {
  499.     m_supported = 1,
  500.     m_graphics = 0x10
  501. } mode_attribute;
  502. typedef enum {
  503.     w_supported = 1,
  504.     w_readable = 2,
  505.     w_writable = 4
  506. } win_attribute;
  507. typedef struct {
  508.     ushort mode_attributes;
  509.     byte win_a_attributes;
  510.     byte win_b_attributes;
  511.     ushort win_granularity;
  512.     ushort win_size;
  513.     ushort win_a_segment;
  514.     ushort win_b_segment;
  515.     void (*win_func_ptr)(P2(int, int));
  516.     ushort bytes_per_line;
  517.         /* Optional information */
  518.     ushort x_resolution;
  519.     ushort y_resolution;
  520.     byte x_char_size;
  521.     byte y_char_size;
  522.     byte number_of_planes;
  523.     byte bits_per_pixel;
  524.     byte number_of_banks;
  525.     byte memory_model;
  526.     byte bank_size;
  527.         /* Padding to 256 bytes */
  528.     byte _padding[256-29];
  529. } vesa_info;
  530.  
  531. /* Read the device mode */
  532. int
  533. vesa_get_mode(void)
  534. {    registers regs;
  535.     regs.h.ah = 0x4f;
  536.     regs.h.al = 0x03;
  537.     int86(0x10, ®s, ®s);
  538.     return regs.rshort.bx;
  539. }
  540.  
  541. /* Set the device mode */
  542. void
  543. vesa_set_mode(int mode)
  544. {    registers regs;
  545.     regs.h.ah = 0x4f;
  546.     regs.h.al = 0x02;
  547.     regs.rshort.bx = mode;
  548.     int86(0x10, ®s, ®s);
  549. }
  550.  
  551. /* Read information about a device mode */
  552. private int
  553. vesa_get_info(int mode, vesa_info _ss *info)
  554. {    registers regs;
  555.     struct SREGS sregs;
  556.     regs.h.ah = 0x4f;
  557.     regs.h.al = 0x01;
  558.     regs.rshort.cx = mode;
  559.     segread(&sregs);
  560.     sregs.es = sregs.ss;
  561.     regs.rshort.di = PTR_OFF(info);
  562.     int86x(0x10, ®s, ®s, &sregs);
  563. #ifdef DEBUG
  564.     if ( regs.h.ah == 0 && regs.h.al == 0x4f )
  565.         dprintf8("vesa_get_info(%x): ma=%x wa=%x/%x wg=%x ws=%x wseg=%x/%x\n",
  566.              mode, info->mode_attributes,
  567.              info->win_a_attributes, info->win_b_attributes,
  568.              info->win_granularity, info->win_size,
  569.              info->win_a_segment, info->win_b_segment);
  570.     else
  571.         dprintf3("vesa_get_info(%x) failed: ah=%x al=%x\n",
  572.              mode, regs.h.ah, regs.h.al);
  573. #endif
  574.     return (regs.h.ah == 0 && regs.h.al == 0x4f ? 0 : -1);
  575. }
  576.  
  577. /* Initialize the graphics mode. */
  578. /* Shared routine to look up a VESA-compatible BIOS mode. */
  579. private int
  580. vesa_find_mode(gx_device *dev, const mode_info _ds *mode_table)
  581. {    /* Select the proper video mode */
  582.     vesa_info info;
  583.     const mode_info _ds *mip;
  584.     for ( mip = mode_table; mip->mode >= 0; mip++ )
  585.        {    if ( mip->width >= fb_dev->width &&
  586.              mip->height >= fb_dev->height &&
  587.              vesa_get_info(mip->mode, &info) >= 0 &&
  588.              bits_include(info.mode_attributes,
  589.             m_supported | m_graphics) &&
  590.              info.win_granularity <= 64 &&
  591.              (info.win_granularity & (info.win_granularity - 1)) == 0 &&
  592.              info.win_size == 64 &&
  593.              bits_include(info.win_a_attributes,
  594.             w_supported) &&
  595.              info.win_a_segment == regen
  596.            )
  597.            {    /* Make sure we can both read & write. */
  598.             /* Initialize for the default case. */
  599.             fb_dev->wnum_read = 0;
  600.             fb_dev->wnum_write = 0;
  601.             if ( bits_include(info.win_a_attributes,
  602.                 w_readable | w_writable)
  603.                )
  604.                 break;
  605.             else if ( info.win_b_segment == regen &&
  606.                 bits_include(info.win_b_attributes,
  607.                     w_supported) &&
  608.                 bits_include(info.win_a_attributes |
  609.                     info.win_b_attributes,
  610.                     w_readable | w_writable)
  611.                )
  612.                {    /* Two superimposed windows. */
  613.                 if ( !bits_include(info.win_a_attributes,
  614.                     w_writable)
  615.                    )
  616.                     fb_dev->wnum_write = 1;
  617.                 else
  618.                     fb_dev->wnum_read = 1;
  619.                }
  620.             break;
  621.            }
  622.        }
  623.     if ( mip->mode < 0 )
  624.         return_error(gs_error_rangecheck);    /* mode not available */
  625.     fb_dev->mode = mip;
  626.     gx_device_adjust_resolution(dev, mip->width, mip->height, 1);
  627.     fb_dev->info.vesa.bios_set_page = info.win_func_ptr;
  628.     fb_dev->info.vesa.pn_shift = small_exact_log2(64 / info.win_granularity);
  629.     /* Reset the raster per the VESA info. */
  630.     fb_dev->raster = info.bytes_per_line;
  631.     return 0;
  632. }
  633. private int
  634. vesa_open(gx_device *dev)
  635. {    static const mode_info mode_table[] = {
  636.        {     640,  400, 0x100    },
  637.        {     640,  480, 0x101    },
  638.        {     800,  600, 0x103    },
  639.        {    1024,  768, 0x105    },
  640.        {    1280, 1024, 0x107    },
  641.        {    -1, -1, -1    }
  642.     };
  643.     int code = vesa_find_mode(dev, mode_table);
  644.     if ( code < 0 ) return code;
  645.     return svga_open(dev);
  646. }
  647.  
  648. /* Set the current display page. */
  649. private void
  650. vesa_set_page(gx_device_svga *dev, int pn, int wnum)
  651. {
  652. #if USE_ASM
  653. extern void vesa_call_set_page(P3(void (*)(P2(int, int)), int, int));
  654.     if ( dev->info.vesa.bios_set_page != NULL )
  655.         vesa_call_set_page(dev->info.vesa.bios_set_page, pn << dev->info.vesa.pn_shift, wnum);
  656.     else
  657. #endif
  658.        {    registers regs;
  659.         regs.rshort.dx = pn << dev->info.vesa.pn_shift;
  660.         regs.h.ah = 0x4f;
  661.         regs.h.al = 5;
  662.         regs.rshort.bx = wnum;
  663.         int86(0x10, ®s, ®s);
  664.        }
  665. }
  666.  
  667. /* ------ The ATI Wonder device ------ */
  668.  
  669. private dev_proc_open_device(atiw_open);
  670. private gx_device_procs atiw_procs = svga_procs(atiw_open);
  671. private int atiw_get_mode(P0());
  672. private void atiw_set_mode(P1(int));
  673. private void atiw_set_page(P3(gx_device_svga *, int, int));
  674. gx_device_svga gs_atiw_device =
  675.     svga_device(atiw_procs, "atiw", atiw_get_mode, atiw_set_mode, atiw_set_page);
  676.  
  677. /* Read the device mode */
  678. private int
  679. atiw_get_mode(void)
  680. {    registers regs;
  681.     regs.h.ah = 0xf;
  682.     int86(0x10, ®s, ®s);
  683.     return regs.h.al;
  684. }
  685.  
  686. /* Set the device mode */
  687. private void
  688. atiw_set_mode(int mode)
  689. {    registers regs;
  690.     regs.h.ah = 0;
  691.     regs.h.al = mode;
  692.     int86(0x10, ®s, ®s);
  693. }
  694.  
  695. /* Initialize the graphics mode. */
  696. private int
  697. atiw_open(gx_device *dev)
  698. {    /* Select the proper video mode */
  699.        {    static const mode_info mode_table[] = {
  700.            {     640,  400, 0x61    },
  701.            {     640,  480, 0x62    },
  702.            {     800,  600, 0x63    },
  703.            {    1024,  768, 0x64    },
  704.            {    -1, -1, -1    }
  705.         };
  706.         int code = svga_find_mode(dev, mode_table);
  707.         if ( code < 0 ) return code;    /* mode not available */
  708.         fb_dev->info.atiw.select_reg = *(int *)MK_PTR(0xc000, 0x10);
  709.         return svga_open(dev);
  710.        }
  711. }
  712.  
  713. /* Set the current display page. */
  714. private void
  715. atiw_set_page(gx_device_svga *dev, int pn, int wnum)
  716. {    int select_reg = dev->info.atiw.select_reg;
  717.     byte reg;
  718.     disable();
  719.     outportb(select_reg, 0xb2);
  720.     reg = inportb(select_reg + 1);
  721.     outportb(select_reg, 0xb2);
  722.     outportb(select_reg + 1, (reg & 0xe1) + (pn << 1));
  723.     enable();
  724. }
  725.  
  726. /* ------ The Trident device ------ */
  727.  
  728. private dev_proc_open_device(tvga_open);
  729. private gx_device_procs tvga_procs = svga_procs(tvga_open);
  730. /* We can use the atiw_get/set_mode procedures. */
  731. private void tvga_set_page(P3(gx_device_svga *, int, int));
  732. gx_device_svga gs_tvga_device =
  733.     svga_device(tvga_procs, "tvga", atiw_get_mode, atiw_set_mode, tvga_set_page);
  734.  
  735. /* Initialize the graphics mode. */
  736. private int
  737. tvga_open(gx_device *dev)
  738. {    fb_dev->wnum_read = 1;
  739.     fb_dev->wnum_write = 0;
  740.     /* Select the proper video mode */
  741.        {    static const mode_info mode_table[] = {
  742.            {     640,  400, 0x5c    },
  743.            {     640,  480, 0x5d    },
  744.            {     800,  600, 0x5e    },
  745.            {     1024, 768, 0x62    },
  746.            {    -1, -1, -1    }
  747.         };
  748.         int code = svga_find_mode(dev, mode_table);
  749.         if ( code < 0 ) return code;      /* mode not available */
  750.         return svga_open(dev);
  751.        }
  752. }
  753.  
  754. /* Set the current display page. */
  755. private void
  756. tvga_set_page(gx_device_svga *dev, int pn, int wnum)
  757. {
  758.     /* new mode */
  759.     outportb(0x3c4, 0x0b);
  760.     inportb(0x3c4);
  761.  
  762.     outportb(0x3c4, 0x0e);
  763.     outportb(0x3c5, pn ^ 2);
  764. }
  765.  
  766. /* ------ The Tseng Labs ET3000/4000 devices ------ */
  767.  
  768. private dev_proc_open_device(tseng_open);
  769. private gx_device_procs tseng_procs =
  770.     svga_procs(tseng_open);
  771. /* We can use the atiw_get/set_mode procedures. */
  772. private void tseng_set_page(P3(gx_device_svga *, int, int));
  773.  
  774. /* The 256-color Tseng device */
  775. gx_device_svga gs_tseng_device =
  776.     svga_device(tseng_procs, "tseng", atiw_get_mode, atiw_set_mode, tseng_set_page);
  777.  
  778. /* The 8-color Tseng device */
  779. private dev_proc_map_rgb_color(tseng8_map_rgb_color);
  780. private gx_device_procs tseng8_procs =
  781.     svga_procs_with_map(tseng_open, tseng8_map_rgb_color);
  782. gx_device_svga gs_tseng8_device =
  783.     svga_color_device(tseng8_procs, "tseng8", 8, 1, 2, atiw_get_mode, atiw_set_mode, tseng_set_page);
  784.  
  785. /* Initialize the graphics mode. */
  786. private int
  787. tseng_open(gx_device *dev)
  788. {    fb_dev->wnum_read = 1;
  789.     fb_dev->wnum_write = 0;
  790.     /* Select the proper video mode */
  791.        {    static const mode_info mode_table[] = {
  792.            {     640,  350, 0x2d    },
  793.            {     640,  480, 0x2e    },
  794.            {     800,  600, 0x30    },
  795.            {     1024, 768, 0x38    },
  796.            {    -1, -1, -1    }
  797.         };
  798.         int code = svga_find_mode(dev, mode_table);
  799.         volatile_fb_ptr p0 = (volatile_fb_ptr)MK_PTR(regen, 0);
  800.         if ( code < 0 ) return code;    /* mode not available */
  801.         code = svga_open(dev);
  802.         if ( code < 0 ) return 0;
  803.         /* Figure out whether we have an ET3000 or an ET4000 */
  804.         /* by playing with the segment register. */
  805.         outportb(0x3cd, 0x44);
  806.         *p0 = 4;        /* byte 0, page 4 */
  807.         outportb(0x3cd, 0x40);
  808.         *p0 = 3;        /* byte 0, page 0 */
  809.         fb_dev->info.tseng.et_model = *p0;
  810.                     /* read page 0 if ET3000, */
  811.                     /* page 4 if ET4000 */
  812.         return 0;
  813.        }
  814. }
  815.  
  816. /* Map RGB to 1-bit color. */
  817. gx_color_index
  818. tseng8_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  819.   gx_color_value b)
  820. {    return (gx_color_index)
  821.         ((r > gx_max_color_value / 2 ? 044 : 0) +
  822.          (g > gx_max_color_value / 2 ? 022 : 0) +
  823.          (b > gx_max_color_value / 2 ? 011 : 0));
  824. }
  825.  
  826. /* Set the current display page. */
  827. private void
  828. tseng_set_page(gx_device_svga *dev, int pn, int wnum)
  829. {    /* The ET3000 has read page = 5:3, write page = 2:0; */
  830.     /* the ET4000 has read page = 7:4, write page = 3:0. */
  831.     int shift = dev->info.tseng.et_model;
  832.     int mask = (1 << shift) - 1;
  833.     if ( wnum ) pn <<= shift, mask <<= shift;
  834.     outportb(0x3cd, (inportb(0x3cd) & ~mask) + pn);
  835. }
  836.