home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / src / vgapix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-16  |  4.8 KB  |  176 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. /* partially copyrighted (C) 1993 by Hartmut Schirmer */
  9. /* HH: Added 4bpp support, and use bytesperpixel. */
  10.  
  11. #include <stdio.h>
  12. #include "vga.h"
  13. #include "libvga.h"
  14.  
  15.  
  16. static inline void read_write(char *p)
  17. {
  18.   ++(*p); 
  19. }
  20.  
  21. int vga_drawpixel(int x, int y)
  22. {
  23.     unsigned long offset;
  24.  
  25.     if (MODEX) {
  26.             /* select plane */ 
  27.         port_out(0x02, SEQ_I); 
  28.             port_out(1 << (x & 3), SEQ_D);
  29.             /* write color to pixel */
  30.         GM[y*CI.xbytes + (x>>2)] = COL;  
  31.             return 0;
  32.     }
  33.  
  34.     switch (__svgalib_cur_info.bytesperpixel) {
  35.       case 0 :    /* Must be 2 or 16 color mode. */
  36.         /* set up video page */
  37.         offset = y*CI.xbytes + (x>>3);
  38.             vga_setpage(offset>>16);
  39.         offset &= 0xffff;
  40.  
  41.         /* select bit */
  42.         port_out(8, GRA_I);
  43.         port_out(0x80 >> (x & 7), GRA_D);   
  44.  
  45.             /* read into latch and write dummy back */
  46.         read_write(GM+offset);
  47.             break;
  48.       case 1 :
  49.         offset = y*CI.xbytes+x; 
  50.  
  51.         /* select segment */
  52.         vga_setpage(offset >> 16);
  53.  
  54.             /* write color to pixel */
  55.         GM[offset & 0xFFFF] = COL;  
  56.             break;
  57.         case 2 :
  58.             offset = y * CI.xbytes + x * 2;
  59.             vga_setpage(offset >> 16);
  60.             *(unsigned short *)(GM + (offset & 0xffff)) = COL;
  61.             break;
  62.         case 3 :
  63.             offset = y * CI.xbytes + x * 3;
  64.             vga_setpage(offset >> 16);
  65.             switch (offset & 0xffff) {
  66.                 case 0xfffe :
  67.                     *(unsigned short *)(GM+0xfffe) = COL;
  68.                     vga_setpage((offset >> 16) + 1);
  69.                     GM[0] = COL >> 16;
  70.                     break; 
  71.                 case 0xffff :
  72.                     GM[0xffff] = COL;
  73.                     vga_setpage((offset >> 16) + 1);
  74.                     *(unsigned short *)(GM) = COL>>8;
  75.                     break;
  76.                 default :
  77.                 offset &= 0xffff;
  78.                     *(unsigned short *)(GM+offset) = COL;
  79.                     GM[offset+2] = COL>>16;
  80.                     break;
  81.             }
  82.             break;
  83.         case 4 :
  84.             offset = y * __svgalib_cur_info.xbytes + x * 4;
  85.             vga_setpage(offset >> 16);
  86.         *(unsigned *)(GM + (offset & 0xffff)) =
  87.             (MODEFLAGS & RGB_MISORDERED) ? (COL << 8) : COL;
  88.             break;
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94. int vga_getpixel(int x, int y)
  95. {
  96.     unsigned long offset;
  97.     unsigned char mask;
  98.     int pix;
  99.  
  100.     if (MODEX) {
  101.             /* select plane */ 
  102.         port_out(0x02, SEQ_I); 
  103.             port_out(1 << (x & 3), SEQ_D);
  104.             return __svgalib_graph_mem[y*CI.xbytes + (x>>2)];  
  105.     }
  106.  
  107.     switch (__svgalib_cur_info.bytesperpixel) {
  108.       case 0 :    /* Must be 2 or 16 color mode. */
  109.         /* set up video page */
  110.         offset = y*CI.xbytes + (x>>3);
  111.             vga_setpage(offset>>16);
  112.         offset &= 0xffff;
  113.  
  114.         /* select bit */
  115.         port_out(8, GRA_I);
  116.         port_out(0x80 >> (x & 7), GRA_D);   
  117.         mask = 0x80 >> (x & 7);
  118.         pix = 0;
  119.         port_out(4, GRA_I);
  120.         port_out(0, GRA_D);        /* Select plane 0. */
  121.         if ((volatile)__svgalib_graph_mem[offset] & mask)
  122.             pix |= 0x01;
  123.         port_out(4, GRA_I);
  124.         port_out(1, GRA_D);        /* Select plane 1. */
  125.         if ((volatile)__svgalib_graph_mem[offset] & mask)
  126.             pix |= 0x02;
  127.         port_out(4, GRA_I);
  128.         port_out(2, GRA_D);        /* Select plane 2. */
  129.         if ((volatile)__svgalib_graph_mem[offset] & mask)
  130.             pix |= 0x04;
  131.         port_out(4, GRA_I);
  132.         port_out(3, GRA_D);        /* Select plane 3. */
  133.         if ((volatile)__svgalib_graph_mem[offset] & mask)
  134.             pix |= 0x08;
  135.         return pix;
  136.       case 1 :
  137.         offset = y*CI.xbytes+x; 
  138.  
  139.         /* select segment */
  140.         vga_setpage(offset >> 16);
  141.  
  142.             return __svgalib_graph_mem[offset & 0xFFFF];
  143.         case 2 :
  144.             offset = y * CI.xbytes + x * 2;
  145.             vga_setpage(offset >> 16);
  146.             return *(unsigned short *)(GM + (offset & 0xffff));
  147.         case 3 :
  148.             offset = y * CI.xbytes + x * 3;
  149.             vga_setpage(offset >> 16);
  150.             switch (offset & 0xffff) {
  151.                 case 0xfffe :
  152.                     pix = *(unsigned short *)(GM+0xfffe);
  153.                     vga_setpage((offset >> 16) + 1);
  154.                     return pix + (__svgalib_graph_mem[0] << 16);
  155.                 case 0xffff :
  156.                     pix = __svgalib_graph_mem[0xffff];
  157.                     vga_setpage((offset >> 16) + 1);
  158.                     return pix + (*(unsigned short *)
  159.                         __svgalib_graph_mem << 8);
  160.                 default :
  161.                 offset &= 0xffff;
  162.                 return *(unsigned short *)(__svgalib_graph_mem
  163.                     + offset) + (__svgalib_graph_mem[
  164.                     offset + 2] << 16);
  165.             }
  166.             break;
  167.         case 4 :
  168.             offset = y * __svgalib_cur_info.xbytes + x * 4;
  169.             vga_setpage(offset >> 16);
  170.             return *(unsigned *)(__svgalib_graph_mem + (offset & 0xffff));
  171.     }
  172.  
  173.     return 0;
  174. }
  175.  
  176.