initial commit
[vidsys] / drv_vga.c
1 #include <string.h>
2 #include <conio.h>
3 #include <i86.h>
4 #include "vidsys.h"
5 #include "drv.h"
6 #include "vga.h"
7
8 void vga_vsync(void);
9
10 static int init(void);
11 static void cleanup(void);
12 static int setmode(int mode);
13 static int curmode(void);
14
15 static void setpal4(int idx, int count, const struct vid_color *col);
16 static void getpal4(int idx, int count, struct vid_color *col);
17 static void clear4(uint32_t color);
18 static void blitfb4(void *fb, int pitch);
19 static void fill4(int x, int y, int w, int h, uint32_t color);
20
21 static void clear8(uint32_t color);
22 static void blitfb8(void *fb, int pitch);
23 static void fill8(int x, int y, int w, int h, uint32_t color);
24
25
26 static struct vid_driver drv;
27 static struct vid_drvops drvops = {init, cleanup, setmode, curmode};
28 static struct vid_modeinfo modes[] = {
29         {3, 80, 25, 4},
30         {0x12, 640, 480, 4},
31         {0x13, 320, 200, 8}
32 };
33
34 static struct vid_gfxops gfxops_mode12h = {
35         0, 0, setpal4, getpal4, vga_vsync, clear4, blitfb4, 0, fill4 };
36 static struct vid_gfxops gfxops_mode13h = {
37         0, 0, vga_setpal, vga_getpal, vga_vsync, clear8, blitfb8, 0, fill8 };
38
39 void vid_register_vga(void)
40 {
41         int i;
42
43         drv.name = "vga";
44         drv.prio = 1;
45         drv.ops = &drvops;
46         drv.modes = modes;
47         drv.num_modes = sizeof modes / sizeof *modes;
48
49         for(i=0; i<drv.num_modes; i++) {
50                 modes[i].drv = &drv;
51                 modes[i].vmem_addr = 0xa0000;
52
53                 switch(modes[i].modeno) {
54                 case 0x3:
55                         modes[i].vmem_addr = 0xb8000;
56                         break;
57                                 
58                 case 0x13:
59                         modes[i].ops = gfxops_mode13h;
60                         break;
61
62                 case 0x12:
63                         modes[i].ops = gfxops_mode12h;
64                         break;
65                 }
66         }
67
68         vid_drvlist[vid_numdrv++] = &drv;
69 }
70
71 void vga_vsync(void)
72 {
73         while(inp(VGA_STAT1_PORT) & 8);
74         while((inp(VGA_STAT1_PORT) & 8) == 0);
75 }
76
77 void vga_setpal(int idx, int count, const struct vid_color *col)
78 {
79         int i;
80         outp(VGA_DAC_WADDR_PORT, idx);
81         for(i=0; i<count; i++) {
82                 outp(VGA_DAC_DATA_PORT, col->r >> 2);
83                 outp(VGA_DAC_DATA_PORT, col->g >> 2);
84                 outp(VGA_DAC_DATA_PORT, col->b >> 2);
85                 col++;
86         }
87 }
88
89 void vga_getpal(int idx, int count, struct vid_color *col)
90 {
91         int i;
92         outp(VGA_DAC_RADDR_PORT, idx);
93         for(i=0; i<count; i++) {
94                 col->r = inp(VGA_DAC_DATA_PORT) << 2;
95                 col->g = inp(VGA_DAC_DATA_PORT) << 2;
96                 col->b = inp(VGA_DAC_DATA_PORT) << 2;
97                 col++;
98         }
99 }
100
101
102 static int init(void)
103 {
104         return 0;
105 }
106
107 static void cleanup(void)
108 {
109 }
110
111 static int setmode(int mode)
112 {
113         union REGS regs = {0};
114         regs.w.ax = mode;
115         int386(0x10, &regs, &regs);
116         return 0;
117 }
118
119 static int curmode(void)
120 {
121         union REGS regs = {0};
122         regs.w.ax = 0xf00;
123         int386(0x10, &regs, &regs);
124         return regs.x.eax & 0xff;
125 }
126
127 static void setpal4(int idx, int count, const struct vid_color *col)
128 {
129 }
130
131 static void getpal4(int idx, int count, struct vid_color *col)
132 {
133 }
134
135 static void clear4(uint32_t color)
136 {
137 }
138
139 static void blitfb4(void *fb, int pitch)
140 {
141 }
142
143 static void fill4(int x, int y, int w, int h, uint32_t color)
144 {
145 }
146
147 static void clear8(uint32_t color)
148 {
149         memset((void*)0xa0000, color, 64000);
150 }
151
152 static void blitfb8(void *fb, int pitch)
153 {
154         int i;
155         unsigned char *src = fb;
156         unsigned char *dest = (unsigned char*)0xa0000;
157         for(i=0; i<200; i++) {
158                 memcpy(dest, src, 320);
159                 dest += 320;
160                 src += pitch;
161         }
162 }
163
164 static void fill8(int x, int y, int w, int h, uint32_t color)
165 {
166         int i;
167         unsigned char *fbptr = (unsigned char*)0xa0000;
168
169         fbptr += y * 320 + x;
170         for(i=0; i<h; i++) {
171                 memset(fbptr, color, w);
172                 fbptr += 320;
173         }
174 }