openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

vga.c (5812B)


      1 /* $Id: //depot/blt/srv/fish/vga.c#3 $
      2 **
      3 ** Copyright 1998 Brian J. Swetland
      4 ** All rights reserved.
      5 **
      6 ** Redistribution and use in source and binary forms, with or without
      7 ** modification, are permitted provided that the following conditions
      8 ** are met:
      9 ** 1. Redistributions of source code must retain the above copyright
     10 **    notice, this list of conditions, and the following disclaimer.
     11 ** 2. Redistributions in binary form must reproduce the above copyright
     12 **    notice, this list of conditions, and the following disclaimer in the
     13 **    documentation and/or other materials provided with the distribution.
     14 ** 3. The name of the author may not be used to endorse or promote products
     15 **    derived from this software without specific prior written permission.
     16 **
     17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 */
     28 #include "vga.h"
     29 
     30 #include <i386/io.h>
     31 
     32 const int MiscOutputReg = 0x3c2;
     33 const int DataReg = 0x3c0;
     34 const int AddressReg = 0x3c0;
     35 
     36 const char mode13[][32] = {
     37   { 0x03, 0x01, 0x0f, 0x00, 0x0e },            /* 0x3c4, index 0-4*/
     38   { 0x5f, 0x4f, 0x50, 0x82, 0x54, 0x80, 0xbf, 0x1f,
     39     0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     40     0x9c, 0x0e, 0x8f, 0x28, 0x40, 0x96, 0xb9, 0xa3,
     41     0xff },                                    /* 0x3d4, index 0-0x18*/
     42   { 0, 0, 0, 0, 0, 0x40, 0x05, 0x0f, 0xff },   /* 0x3ce, index 0-8*/ 
     43   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
     44     0x41, 0, 0x0f, 0, 0 }                      /* 0x3c0, index 0-0x14*/
     45 };
     46 
     47 #define Vga256                   0x13   // 320x200x256
     48 #define TextMode                 0x03   // 80x25 text mode
     49 #define PaletteMask              0x3C6  // bit mask register
     50 #define PaletteRegisterRead      0x3C7  // read index
     51 #define PaletteRegisterWrite     0x3C8  // write index
     52 #define PaletteData              0x3C9  // send/receive data here
     53 #define RomCharSegment           0xF000 
     54 #define RomCharOffset            0xFA6E 
     55 #define CharWidth                8
     56 #define CharHeight               8
     57 
     58 
     59 #define pixel(x,y,c) (((unsigned char *)VRAM)[(y)*320+(x)] = c)
     60 
     61 
     62 void vga_set_palette(int color, int red, int green, int blue)
     63 {
     64 
     65   outb(0xFF, PaletteMask);
     66   outb(color, PaletteRegisterWrite);
     67   outb(red, PaletteData);
     68   outb(green, PaletteData);
     69   outb(blue, PaletteData);
     70 }      
     71 
     72 int vga_set_mode(int xres, int yres, int bitdepth)
     73 {
     74   int i;
     75 
     76   outb_p(0x63, MiscOutputReg);
     77   outb_p(0x00, 0x3da);
     78 
     79   for (i=0; i < 5; i++) {
     80     outb_p(i, 0x3c4);
     81     outb_p(mode13[0][i], 0x3c4+1);
     82   }
     83 
     84   outw_p(0x0e11, 0x3d4);
     85 
     86   for (i=0; i < 0x19; i++) {
     87     outb_p(i, 0x3d4);
     88     outb_p(mode13[1][i], (0x3d4 + 1));
     89   }  
     90 
     91   for (i=0; i < 0x9; i++) {
     92     outb_p(i, 0x3ce);
     93     outb_p(mode13[2][i], (0x3ce + 1));
     94   }
     95 
     96   inb_p(0x3da);
     97 
     98   for (i=0; i < 0x15; i++) {
     99     inw(DataReg);
    100     outb_p(i, AddressReg);
    101     outb_p(mode13[3][i], DataReg);
    102   }
    103 
    104   outb_p(0x20, 0x3c0);
    105 
    106   return 1;
    107 }
    108 
    109 #include "font.h"
    110 
    111 static unsigned int VRAM = 0xA0000;
    112 static unsigned int SRAM = 0xA0000;
    113 
    114 void vga_set_sram(void *addr)
    115 {
    116     SRAM = (unsigned int) addr;
    117 }
    118 
    119 void vga_set_vram(void *addr)
    120 {
    121     VRAM = (unsigned int) addr;
    122 }
    123 
    124 void vga_swap_buffers(void)
    125 {
    126     int i;
    127     unsigned int *vram = (unsigned int *) VRAM;
    128     unsigned int *disp = (unsigned int *) SRAM;
    129 
    130     i = 16000;
    131     while(i--) *disp++ = *vram++;
    132 }
    133 
    134 
    135 void vga_blit(char *bitmap, int x, int y, int w, int h)
    136 {
    137     int i, j;
    138     char *edi = (char *) (VRAM + y*320 + x);
    139     
    140     for (i = 0; i < h; i++, edi += 320 - w) {
    141       for (j = 0; j < w; j++, edi++, bitmap++)
    142 	*edi = *bitmap;
    143     }
    144 }
    145 
    146 void vga_blit_trans(char *bitmap, int x, int y, int w, int h)
    147 {
    148     int i, j;
    149     char *edi = (char *) (VRAM + y*320 + x);
    150     
    151     for (i = 0; i < h; i++, edi += 320 - w) {
    152       for (j = 0; j < w; j++, edi++, bitmap++)
    153 	if (*bitmap != '.') *edi = *bitmap;
    154     }
    155 }
    156 
    157 void vga_blit_trans_r(char *bitmap, int x, int y, int w, int h)
    158 {
    159     int i, j;
    160     char *edi = (char *) (VRAM + y*320 + x + w);
    161     
    162     for (i = 0; i < h; i++, edi += 320 + w) {
    163       for (j = w; j >0; j--, edi--, bitmap++)
    164 	if (*bitmap != '.') *edi = *bitmap;
    165     }
    166 }
    167 
    168 void vga_blit_str(char *s, int x, int y, int c)
    169 {
    170     int w, h, i, j;
    171     char *edi;
    172     char *bitmap;
    173     w = 5;
    174     h = 8;
    175     while(*s) {
    176         bitmap = font[(*s) & 0x7F];
    177         edi = (char *) (VRAM + y*320 + x);
    178         for (i = 0; i < h; i++, edi += 320 - w) {
    179             for (j = 0; j < w; j++, edi++, bitmap++){
    180                 if (*bitmap != '.') *edi = c;
    181             }
    182         }
    183         x += 5;
    184         s++;
    185     }
    186 }
    187 
    188 
    189 
    190 void vga_fill(int w, int h, int x, int y, int c)
    191 {
    192     int tx,ty;
    193     
    194     for(ty = y; ty < y+h; ty++){
    195         for(tx = x; tx < x+w; tx++){
    196             pixel(tx,ty,c);
    197         }
    198     }
    199 
    200 }
    201 
    202 void vga_fillX(void)
    203 {
    204     unsigned int f,i,j=50,n=128+49;
    205     unsigned int *addr = (unsigned int *) VRAM;
    206 
    207     while(j--){
    208         f = (n << 24) | (n << 16) | (n << 8) | n;
    209         n--;
    210         i = 320;
    211         while(i--) *addr++ = f;
    212     }
    213 }
    214 
    215 void vga_fill_grad(int w, int h, int x, int y)
    216 {
    217     int tx,ty;
    218     
    219     for(ty = y; ty < y+h; ty++){
    220         for(tx = x; tx < x+w; tx++){
    221             pixel(tx,ty,128 + ty/4);
    222         }
    223     }
    224 
    225 }