First import
[xorg_rtime.git] / xorg-server-1.4 / hw / kdrive / src / vga.c
blob862f074a24bba7a984cf6e30e3f99a33ce55974b
1 /*
2 * Copyright © 1999 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Keith Packard makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
23 #ifdef HAVE_CONFIG_H
24 #include <kdrive-config.h>
25 #endif
26 #include "vga.h"
27 #include <stdio.h>
29 #ifdef linux
30 #ifdef __i386__
31 #define extern static
32 #include <sys/io.h>
33 #undef extern
35 #define _VgaInb(r) inb(r)
36 #define _VgaOutb(v,r) outb(v,r)
37 #else
38 #define _VgaInb(r) 0
39 #define _VgaOutb(v,r)
40 #endif
42 #define _VgaByteAddr(a) ((VGAVOL8 *) (a))
43 #define _VgaBytePort(a) (a)
44 #endif
46 #ifdef VXWORKS
47 #define _VgaInb(r) 0
48 #define _VgaOutb(v,r) 0
50 #define _VgaByteAddr(a) ((VGAVOL8 *) ((VGA32) (a) ^ 3))
51 #define _VgaBytePort(a) 0
53 #undef stderr
54 #define stderr stdout
56 #endif
58 #undef VGA_DEBUG_REGISTERS
59 #ifdef VGA_DEBUG_REGISTERS
60 #define VGA_DEBUG(a) fprintf a
61 #else
62 #define VGA_DEBUG(a)
63 #endif
65 VGA8
66 VgaInb (VGA16 r)
68 return _VgaInb(r);
71 void
72 VgaOutb (VGA8 v, VGA16 r)
74 _VgaOutb (v,r);
77 VGA8
78 VgaReadMemb (VGA32 addr)
80 return *_VgaByteAddr(addr);
83 void
84 VgaWriteMemb (VGA8 v, VGA32 addr)
86 *_VgaByteAddr(addr) = v;
89 VGA8
90 VgaFetch (VgaCard *card, VGA16 reg)
92 VgaMap map;
93 VGA8 value = 0;
95 (*card->map) (card, reg, &map, VGAFALSE);
96 switch (map.access) {
97 case VgaAccessMem:
98 value = VgaReadMemb (map.port);
99 VGA_DEBUG ((stderr, "%08x -> %2x\n", map.port, value));
100 break;
101 case VgaAccessIo:
102 value = _VgaInb (map.port);
103 VGA_DEBUG ((stderr, "%4x -> %2x\n", map.port, value));
104 break;
105 case VgaAccessIndMem:
106 VgaWriteMemb (map.index, map.port + map.addr);
107 value = VgaReadMemb (map.port + map.value);
108 VGA_DEBUG ((stderr, "%4x/%2x -> %2x\n", map.port, map.index, value));
109 break;
110 case VgaAccessIndIo:
111 _VgaOutb (map.index, map.port + map.addr);
112 value = _VgaInb (map.port + map.value);
113 VGA_DEBUG ((stderr, "%4x/%2x -> %2x\n", map.port, map.index, value));
114 break;
115 case VgaAccessDone:
116 value = map.value;
117 VGA_DEBUG ((stderr, "direct %4x -> %2x\n", reg, value));
118 break;
120 return value;
123 void
124 VgaStore (VgaCard *card, VGA16 reg, VGA8 value)
126 VgaMap map;
128 map.value = value;
129 (*card->map) (card, reg, &map, VGATRUE);
130 switch (map.access) {
131 case VgaAccessMem:
132 VGA_DEBUG ((stderr, "%8x <- %2x\n", map.port, value));
133 VgaWriteMemb (map.value, map.port);
134 break;
135 case VgaAccessIo:
136 VGA_DEBUG ((stderr, "%4x <- %2x\n", map.port, value));
137 _VgaOutb (value, map.port);
138 break;
139 case VgaAccessIndMem:
140 VgaWriteMemb (map.index, map.port + map.addr);
141 VgaWriteMemb (value, map.port + map.value);
142 VGA_DEBUG ((stderr, "%4x/%2x <- %2x\n", map.port, map.index, value));
143 break;
144 case VgaAccessIndIo:
145 VGA_DEBUG ((stderr, "%4x/%2x <- %2x\n", map.port, map.index, value));
146 _VgaOutb (map.index, map.port + map.addr);
147 _VgaOutb (value, map.port + map.value);
148 break;
149 case VgaAccessDone:
150 VGA_DEBUG ((stderr, "direct %4x <- %2x\n", reg, value));
151 break;
155 void
156 VgaPreserve (VgaCard *card)
158 VgaSave *s;
159 VGA16 id;
161 for (s = card->saves; s->first != VGA_REG_NONE; s++)
163 for (id = s->first; id <= s->last; id++)
165 card->values[id].cur = VgaFetch (card, id);
166 card->values[id].save = card->values[id].cur;
167 card->values[id].flags = VGA_VALUE_VALID | VGA_VALUE_SAVED;
172 void
173 VgaRestore (VgaCard *card)
175 VgaSave *s;
176 VGA16 id;
178 for (s = card->saves; s->first != VGA_REG_NONE; s++)
180 for (id = s->first; id <= s->last; id++)
182 if (card->values[id].flags & VGA_VALUE_SAVED)
184 VgaStore (card, id, card->values[id].save);
185 card->values[id].cur = card->values[id].save;
191 void
192 VgaFinish (VgaCard *card)
194 VGA16 id;
196 for (id = 0; id < card->max; id++)
197 card->values[id].flags = 0;
200 void
201 VgaInvalidate (VgaCard *card)
203 VGA16 id;
205 for (id = 0; id < card->max; id++)
206 card->values[id].flags &= ~VGA_VALUE_VALID;
210 static void
211 _VgaSync (VgaCard *card, VGA16 id)
213 if (!(card->values[id].flags & VGA_VALUE_VALID))
215 card->values[id].cur = VgaFetch (card, id);
216 card->values[id].flags |= VGA_VALUE_VALID;
220 void
221 VgaSet (VgaCard *card, VgaReg *reg, VGA32 value)
223 VGA8 v, mask, new;
225 while (reg->len)
227 if (reg->id != VGA_REG_NONE)
229 _VgaSync (card, reg->id);
230 mask = ((1 << reg->len) - 1);
231 new = value & mask;
232 mask <<= reg->base;
233 new <<= reg->base;
234 v = card->values[reg->id].cur;
235 v = (v & ~mask) | new;
236 card->values[reg->id].cur = v;
237 card->values[reg->id].flags |= VGA_VALUE_MODIFIED|VGA_VALUE_DIRTY;
239 value >>= reg->len;
240 reg++;
244 void
245 VgaFlushReg (VgaCard *card, VgaReg *reg)
247 while (reg->len)
249 if (reg->id != VGA_REG_NONE)
251 if (card->values[reg->id].flags & VGA_VALUE_DIRTY)
253 VgaStore (card, reg->id, card->values[reg->id].cur);
254 card->values[reg->id].flags &= ~VGA_VALUE_DIRTY;
257 reg++;
262 void
263 VgaSetImm (VgaCard *card, VgaReg *reg, VGA32 value)
265 VgaSet (card, reg, value);
266 VgaFlushReg (card, reg);
269 VGA32
270 VgaGet (VgaCard *card, VgaReg *reg)
272 VGA32 value, offset, v;
273 VGA8 mask;
275 value = 0;
276 offset = 0;
277 while (reg->len)
279 if (reg->id != VGA_REG_NONE)
281 _VgaSync (card, reg->id);
282 mask = ((1 << reg->len) - 1);
283 v = (card->values[reg->id].cur >> reg->base) & mask;
284 value |= (v << offset);
286 offset += reg->len;
287 reg++;
289 return value;
292 VGA32
293 VgaGetImm (VgaCard *card, VgaReg *reg)
295 VgaReg *r = reg;
297 while (r->len)
299 if (r->id != VGA_REG_NONE)
300 card->values[r->id].flags &= ~VGA_VALUE_VALID;
301 r++;
303 return VgaGet (card, reg);
306 void
307 VgaFlush (VgaCard *card)
309 VGA16 id;
311 for (id = 0; id < card->max; id++)
313 if (card->values[id].flags & VGA_VALUE_DIRTY)
315 VgaStore (card, id, card->values[id].cur);
316 card->values[id].flags &= ~VGA_VALUE_DIRTY;
321 void
322 VgaFill (VgaCard *card, VGA16 low, VGA16 high)
324 VGA16 id;
326 for (id = low; id < high; id++)
327 _VgaSync (card, id);