home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVSVGA.C < prev    next >
C/C++ Source or Header  |  1992-09-18  |  20KB  |  686 lines

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