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