home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / src / egadrv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-11  |  5.8 KB  |  258 lines

  1. /* VGAlib version 1.2 - (c) 1993 Tommy Frandsen            */
  2. /*                                   */
  3. /* This library is free software; you can redistribute it and/or   */
  4. /* modify it without any restrictions. This library is distributed */
  5. /* in the hope that it will be useful, but without any warranty.   */
  6.  
  7. /* Multi-chipset support Copyright 1993 Harm Hanemaayer */
  8. /* EGA support from EGAlib by Kapil Paranjape */
  9. /* partially copyrighted (C) 1993 by Hartmut Schirmer */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include "vga.h"
  14. #include "libvga.h"
  15. #include "driver.h"
  16.  
  17. static int ega_init( int force, int par1, int par2);
  18.  
  19. /* BIOS mode 0Dh - 320x200x16 */
  20. static char g320x200x16_regs[60] = {
  21.  0x37,0x27,0x2D,0x37,0x30,0x14,0x04,0x11,0x00,0x00,0x00,0x00,
  22.  0x00,0x00,0x00,0x00,0xE1,0x24,0xC7,0x14,0x00,0xE0,0xF0,0xE3,
  23.  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x10,0x11,0x12,0x13,
  24.  0x14,0x15,0x16,0x17,0x01,0x00,0x0F,0x00,0x00,
  25.  0x00,0x0F,0x00,0x00,0x00,0x00,0x05,0x0F,0xFF,
  26.  0x03,0x0B,0x0F,0x00,0x06,
  27.  0x23
  28. };
  29.  
  30. /* BIOS mode 0Eh - 640x200x16 */
  31. static char g640x200x16_regs[60] = {
  32.  0x70,0x4F,0x59,0x2D,0x5E,0x06,0x04,0x11,0x00,0x00,0x00,0x00,
  33.  0x00,0x00,0x00,0x00,0xE0,0x23,0xC7,0x28,0x00,0xDF,0xEF,0xE3,
  34.  0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x10,0x11,0x12,0x13,
  35.  0x14,0x15,0x16,0x17,0x01,0x00,0x0F,0x00,0x00,
  36.  0x00,0x0F,0x00,0x00,0x00,0x00,0x05,0x0F,0xFF,
  37.  0x03,0x01,0x0F,0x00,0x06,
  38.  0x23
  39. };
  40.  
  41. /* BIOS mode 10h - 640x350x16 */
  42. static char g640x350x16_regs[60] = {
  43.  0x5B,0x4F,0x53,0x37,0x52,0x00,0x6C,0x1F,0x00,0x00,0x00,0x00,
  44.  0x00,0x00,0x00,0x00,0x5E,0x2B,0x5E,0x28,0x0F,0x5F,0x0A,0xE3,
  45.  0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,0x38,0x39,0x3A,0x3B,
  46.  0x3C,0x3D,0x3E,0x3F,0x01,0x00,0x0F,0x00,0x00,
  47.  0x00,0x0F,0x00,0x00,0x00,0x00,0x05,0x0F,0xFF,
  48.  0x03,0x01,0x0F,0x00,0x06,
  49.  0xA7
  50. };
  51.  
  52. /* EGA registers for saved text mode 03* */
  53. static char text_regs[60] = {
  54.  0x5B,0x4F,0x53,0x37,0x51,0x5B,0x6C,0x1F,0x00,0x0D,0x0a,0x0c,
  55.  0x00,0x00,0x00,0x00,0x5E,0x2B,0x5D,0x28,0x0F,0x5E,0x0A,0xA3,
  56.  0x00,0x01,0x02,0x03,0x04,0x05,0x14,0x07,0x38,0x39,0x3A,0x3B,
  57.  0x3C,0x3D,0x3E,0x3F,0x0A,0x00,0x0F,0x00,0x00,
  58.  0x00,0x00,0x00,0x00,0x00,0x10,0x0E,0x00,0xFF,
  59.  0x03,0x01,0x03,0x00,0x03,
  60.  0xA7
  61. };
  62.  
  63. /* Mode table */
  64. static ModeTable __ega_modes[] = {
  65.     OneModeEntry(320x200x16),
  66.     OneModeEntry(640x200x16),
  67.     OneModeEntry(640x350x16),
  68.     END_OF_MODE_TABLE
  69. };
  70.  
  71. static ModeTable *ega_modes = NULL;
  72.  
  73.  
  74. /* Fill in chipset-specific modeinfo */
  75.  
  76. static int getmodeinfo( int mode, vga_modeinfo *modeinfo ) {
  77.     /* Taken from vga 16 colors. Correct ??? */
  78.     modeinfo->maxpixels = 65536 * 8;
  79.     modeinfo->startaddressrange = 0x7ffff;
  80.     modeinfo->maxlogicalwidth = 2040;
  81.     modeinfo->haveblit = 0;
  82.     modeinfo->flags &= ~(IS_INTERLACED | HAVE_RWPAGE);
  83.  
  84.     return 0;
  85. }
  86.  
  87. static int nothing() { return 0; }
  88.  
  89.  
  90. /* Return nonzero if mode available */
  91.  
  92. static int modeavailable( int mode ) {
  93.     const unsigned char *regs;
  94.  
  95.     regs = LOOKUPMODE(ega_modes, mode);
  96.     if (regs != NULL && regs != DISABLE_MODE)
  97.       return STDVGADRV;
  98.     return 0;
  99. }
  100.  
  101. static int lastmode = TEXT;
  102.  
  103. static int saveregs(unsigned char *regs)
  104. {
  105.     /* We can't read the registers from an EGA card. */
  106.     /* We just report the expected values. */
  107.  
  108.     const unsigned char *r;
  109.     int i;
  110.  
  111.     if (lastmode == TEXT) 
  112.     r = text_regs;
  113.     else
  114.     r = LOOKUPMODE(ega_modes, lastmode);
  115.     if (r==NULL) {
  116.     printf("svgalib: egadrv.c/saveregs(): internal error\n");
  117.     exit(-1);
  118.     }
  119.     memcpy(regs, r, CRT_C + ATT_C + GRA_C + SEQ_C + MIS_C);
  120.  
  121.     /* save all readable EGA registers; others are default */
  122.     /* is this correct (even in graphics mode) ?? */
  123.     for (i = 0x0C; i < 0x10; i++) {
  124.      port_out(i, CRT_I); 
  125.      regs[CRT+i] = port_in(CRT_D); 
  126.     }
  127.  
  128.     return CRT_C + ATT_C + GRA_C + SEQ_C + MIS_C;
  129. }
  130.  
  131.  
  132. /* Set chipset-specific registers */
  133.  
  134. static int setregs( const unsigned char regs[], int mode ) {
  135.     /* Enable graphics register modification */
  136.     port_out(0x00, GRA_E0);
  137.     port_out(0x01, GRA_E1); 
  138.  
  139.     __vga_setregs(regs);
  140.     return 0;
  141. }
  142.  
  143. /* Set a mode */
  144.  
  145. static int setmode( int mode, int prv_mode )
  146. /* standard EGA driver: setmode */
  147. {
  148.     const unsigned char *regs;
  149.  
  150.     regs = LOOKUPMODE(ega_modes, mode);
  151.     if (regs == NULL || regs == DISABLE_MODE)
  152.         return 1;
  153.     lastmode = mode;
  154.  
  155.     setregs(regs, mode);
  156.     return 0;
  157. }
  158.  
  159.  
  160. /* Set display start */
  161.  
  162. static int setdisplaystart( int address ) {
  163.     inb(IS1_R);
  164.     __vga_delay();
  165.     outb(ATT_IW, 0x13 + 0x20);
  166.     __vga_delay();
  167.     outb(ATT_IW, (address & 7)); 
  168.         /* write sa0-2 to bits 0-2 */
  169.     address >>= 3;
  170.     outw(CRT_I, 0x0d + (address & 0x00ff) * 256);        /* sa0-sa7 */
  171.     outw(CRT_I, 0x0c + (address & 0xff00));            /* sa8-sa15 */
  172.  
  173.     return 0;
  174. }
  175.  
  176. static int setlogicalwidth( int width ) {
  177.     outw(CRT_I, 0x13 + (width >> 3) * 256);    /* lw3-lw11 */
  178.  
  179.     return 0;
  180. }
  181.  
  182.  
  183. /* Indentify chipset; return non-zero if detected */
  184.  
  185. static int ega_test()
  186. {
  187.     unsigned char save, back;
  188.  
  189.     /* Check if a DAC is present */
  190.     save = inb(PEL_IW);
  191.     __vga_delay();
  192.     outb(PEL_IW, ~save);
  193.     __vga_delay();
  194.     back = inb(PEL_IW);
  195.     __vga_delay();
  196.     outb(PEL_IW, save);
  197.     save = ~save;
  198.     if (back != save) {
  199.         ega_init(0,0,0);
  200.         return 1;
  201.     }
  202.     return 0;
  203. }
  204.  
  205.  
  206. DriverSpecs ega_driverspecs = {        /* EGA */
  207.     saveregs,
  208.     setregs,
  209.     nothing /* unlock */,
  210.     nothing /* lock */,
  211.     ega_test,
  212.     ega_init,
  213.     nothing /* setpage */,
  214.     nothing /* setrdpage */,
  215.     nothing /* setwrpage */,
  216.     setmode,
  217.     modeavailable,
  218.     setdisplaystart,
  219.     setlogicalwidth,
  220.     getmodeinfo,
  221.     0,    /* bitblt */
  222.     0,    /* imageblt */
  223.     0,    /* fillblt */
  224.     0,    /* hlinelistblt */
  225.     0,    /* bltwait */
  226.     0,    /* extset */
  227.     0,
  228.     0,    /* linear */
  229.     NULL    /* accelspecs */
  230. }; 
  231.  
  232. /* Initialize chipset (called after detection) */
  233.  
  234. static int ega_init( int force, int par1, int par2) {
  235.  
  236.     if (__svgalib_driver_report)
  237.         printf("Using EGA driver.\n");
  238.  
  239.     /* Read additional modes from file if available */
  240.     if (ega_modes == NULL) {
  241.       ega_modes = __ega_modes;
  242. #ifdef EGA_REGS
  243.       {
  244.         FILE *regs;
  245.         regs = fopen(EGA_REGS,"r");
  246.         if (regs != 0) {
  247.           __vga_readmodes(regs, &ega_modes, NULL);
  248.           fclose(regs);
  249.         }
  250.       }
  251. #endif
  252.     }
  253.     driverspecs = &ega_driverspecs;
  254.  
  255.     return 0;
  256. }
  257.  
  258.