2 * QEMU VGA Emulator templates
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #define PIXEL_TYPE uint8_t
28 #elif DEPTH == 15 || DEPTH == 16
30 #define PIXEL_TYPE uint16_t
33 #define PIXEL_TYPE uint32_t
35 #error unsupport depth
40 static inline void glue(vga_draw_glyph_line_, DEPTH)(uint8_t *d,
46 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
47 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
49 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
50 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
51 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
52 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
54 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
55 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
56 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
57 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
58 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
59 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
60 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
61 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
65 static void glue(vga_draw_glyph8_, DEPTH)(uint8_t *d, int linesize,
66 const uint8_t *font_ptr, int h,
67 uint32_t fgcol, uint32_t bgcol)
69 uint32_t font_data, xorcol;
71 xorcol = bgcol ^ fgcol;
73 font_data = font_ptr[0];
74 glue(vga_draw_glyph_line_, DEPTH)(d, font_data, xorcol, bgcol);
80 static void glue(vga_draw_glyph16_, DEPTH)(uint8_t *d, int linesize,
81 const uint8_t *font_ptr, int h,
82 uint32_t fgcol, uint32_t bgcol)
84 uint32_t font_data, xorcol;
86 xorcol = bgcol ^ fgcol;
88 font_data = font_ptr[0];
89 glue(vga_draw_glyph_line_, DEPTH)(d,
90 expand4to8[font_data >> 4],
92 glue(vga_draw_glyph_line_, DEPTH)(d + 8 * BPP,
93 expand4to8[font_data & 0x0f],
100 static void glue(vga_draw_glyph9_, DEPTH)(uint8_t *d, int linesize,
101 const uint8_t *font_ptr, int h,
102 uint32_t fgcol, uint32_t bgcol, int dup9)
104 uint32_t font_data, xorcol, v;
106 xorcol = bgcol ^ fgcol;
108 font_data = font_ptr[0];
110 cpu_to_32wu((uint32_t *)d, (dmask16[(font_data >> 4)] & xorcol) ^ bgcol);
111 v = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
112 cpu_to_32wu(((uint32_t *)d)+1, v);
114 ((uint8_t *)d)[8] = v >> (24 * (1 - BIG));
116 ((uint8_t *)d)[8] = bgcol;
119 cpu_to_32wu(((uint32_t *)d)+0, (dmask4[(font_data >> 6)] & xorcol) ^ bgcol);
120 cpu_to_32wu(((uint32_t *)d)+1, (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol);
121 cpu_to_32wu(((uint32_t *)d)+2, (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol);
122 v = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
123 cpu_to_32wu(((uint32_t *)d)+3, v);
125 ((uint16_t *)d)[8] = v >> (16 * (1 - BIG));
127 ((uint16_t *)d)[8] = bgcol;
129 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
130 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
131 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
132 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
133 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
134 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
135 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
136 v = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
137 ((uint32_t *)d)[7] = v;
139 ((uint32_t *)d)[8] = v;
141 ((uint32_t *)d)[8] = bgcol;
151 static void glue(vga_draw_line2_, DEPTH)(VGAState *s1, uint8_t *d,
152 const uint8_t *s, int width)
154 uint32_t plane_mask, *palette, data, v;
157 palette = s1->last_palette;
158 plane_mask = mask16[s1->ar[0x12] & 0xf];
160 for(x = 0; x < width; x++) {
161 data = ((uint32_t *)s)[0];
163 v = expand2[GET_PLANE(data, 0)];
164 v |= expand2[GET_PLANE(data, 2)] << 2;
165 ((PIXEL_TYPE *)d)[0] = palette[v >> 12];
166 ((PIXEL_TYPE *)d)[1] = palette[(v >> 8) & 0xf];
167 ((PIXEL_TYPE *)d)[2] = palette[(v >> 4) & 0xf];
168 ((PIXEL_TYPE *)d)[3] = palette[(v >> 0) & 0xf];
170 v = expand2[GET_PLANE(data, 1)];
171 v |= expand2[GET_PLANE(data, 3)] << 2;
172 ((PIXEL_TYPE *)d)[4] = palette[v >> 12];
173 ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
174 ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
175 ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
182 #define PUT_PIXEL2(d, n, v) ((uint16_t *)d)[(n)] = (v)
184 #define PUT_PIXEL2(d, n, v) ((uint32_t *)d)[(n)] = (v)
186 #define PUT_PIXEL2(d, n, v) \
187 ((uint32_t *)d)[2*(n)] = ((uint32_t *)d)[2*(n)+1] = (v)
191 * 4 color mode, dup2 horizontal
193 static void glue(vga_draw_line2d2_, DEPTH)(VGAState *s1, uint8_t *d,
194 const uint8_t *s, int width)
196 uint32_t plane_mask, *palette, data, v;
199 palette = s1->last_palette;
200 plane_mask = mask16[s1->ar[0x12] & 0xf];
202 for(x = 0; x < width; x++) {
203 data = ((uint32_t *)s)[0];
205 v = expand2[GET_PLANE(data, 0)];
206 v |= expand2[GET_PLANE(data, 2)] << 2;
207 PUT_PIXEL2(d, 0, palette[v >> 12]);
208 PUT_PIXEL2(d, 1, palette[(v >> 8) & 0xf]);
209 PUT_PIXEL2(d, 2, palette[(v >> 4) & 0xf]);
210 PUT_PIXEL2(d, 3, palette[(v >> 0) & 0xf]);
212 v = expand2[GET_PLANE(data, 1)];
213 v |= expand2[GET_PLANE(data, 3)] << 2;
214 PUT_PIXEL2(d, 4, palette[v >> 12]);
215 PUT_PIXEL2(d, 5, palette[(v >> 8) & 0xf]);
216 PUT_PIXEL2(d, 6, palette[(v >> 4) & 0xf]);
217 PUT_PIXEL2(d, 7, palette[(v >> 0) & 0xf]);
226 static void glue(vga_draw_line4_, DEPTH)(VGAState *s1, uint8_t *d,
227 const uint8_t *s, int width)
229 uint32_t plane_mask, data, v, *palette;
232 palette = s1->last_palette;
233 plane_mask = mask16[s1->ar[0x12] & 0xf];
235 for(x = 0; x < width; x++) {
236 data = ((uint32_t *)s)[0];
238 v = expand4[GET_PLANE(data, 0)];
239 v |= expand4[GET_PLANE(data, 1)] << 1;
240 v |= expand4[GET_PLANE(data, 2)] << 2;
241 v |= expand4[GET_PLANE(data, 3)] << 3;
242 ((PIXEL_TYPE *)d)[0] = palette[v >> 28];
243 ((PIXEL_TYPE *)d)[1] = palette[(v >> 24) & 0xf];
244 ((PIXEL_TYPE *)d)[2] = palette[(v >> 20) & 0xf];
245 ((PIXEL_TYPE *)d)[3] = palette[(v >> 16) & 0xf];
246 ((PIXEL_TYPE *)d)[4] = palette[(v >> 12) & 0xf];
247 ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
248 ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
249 ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
256 * 16 color mode, dup2 horizontal
258 static void glue(vga_draw_line4d2_, DEPTH)(VGAState *s1, uint8_t *d,
259 const uint8_t *s, int width)
261 uint32_t plane_mask, data, v, *palette;
264 palette = s1->last_palette;
265 plane_mask = mask16[s1->ar[0x12] & 0xf];
267 for(x = 0; x < width; x++) {
268 data = ((uint32_t *)s)[0];
270 v = expand4[GET_PLANE(data, 0)];
271 v |= expand4[GET_PLANE(data, 1)] << 1;
272 v |= expand4[GET_PLANE(data, 2)] << 2;
273 v |= expand4[GET_PLANE(data, 3)] << 3;
274 PUT_PIXEL2(d, 0, palette[v >> 28]);
275 PUT_PIXEL2(d, 1, palette[(v >> 24) & 0xf]);
276 PUT_PIXEL2(d, 2, palette[(v >> 20) & 0xf]);
277 PUT_PIXEL2(d, 3, palette[(v >> 16) & 0xf]);
278 PUT_PIXEL2(d, 4, palette[(v >> 12) & 0xf]);
279 PUT_PIXEL2(d, 5, palette[(v >> 8) & 0xf]);
280 PUT_PIXEL2(d, 6, palette[(v >> 4) & 0xf]);
281 PUT_PIXEL2(d, 7, palette[(v >> 0) & 0xf]);
288 * 256 color mode, double pixels
290 * XXX: add plane_mask support (never used in standard VGA modes)
292 static void glue(vga_draw_line8d2_, DEPTH)(VGAState *s1, uint8_t *d,
293 const uint8_t *s, int width)
298 palette = s1->last_palette;
300 for(x = 0; x < width; x++) {
301 PUT_PIXEL2(d, 0, palette[s[0]]);
302 PUT_PIXEL2(d, 1, palette[s[1]]);
303 PUT_PIXEL2(d, 2, palette[s[2]]);
304 PUT_PIXEL2(d, 3, palette[s[3]]);
311 * standard 256 color mode
313 * XXX: add plane_mask support (never used in standard VGA modes)
315 static void glue(vga_draw_line8_, DEPTH)(VGAState *s1, uint8_t *d,
316 const uint8_t *s, int width)
321 palette = s1->last_palette;
323 for(x = 0; x < width; x++) {
324 ((PIXEL_TYPE *)d)[0] = palette[s[0]];
325 ((PIXEL_TYPE *)d)[1] = palette[s[1]];
326 ((PIXEL_TYPE *)d)[2] = palette[s[2]];
327 ((PIXEL_TYPE *)d)[3] = palette[s[3]];
328 ((PIXEL_TYPE *)d)[4] = palette[s[4]];
329 ((PIXEL_TYPE *)d)[5] = palette[s[5]];
330 ((PIXEL_TYPE *)d)[6] = palette[s[6]];
331 ((PIXEL_TYPE *)d)[7] = palette[s[7]];
337 #endif /* DEPTH != 15 */
345 static void glue(vga_draw_line15_, DEPTH)(VGAState *s1, uint8_t *d,
346 const uint8_t *s, int width)
348 #if DEPTH == 15 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
349 memcpy(d, s, width * 2);
356 v = lduw_raw((void *)s);
360 ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
370 static void glue(vga_draw_line16_, DEPTH)(VGAState *s1, uint8_t *d,
371 const uint8_t *s, int width)
373 #if DEPTH == 16 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
374 memcpy(d, s, width * 2);
381 v = lduw_raw((void *)s);
385 ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
395 static void glue(vga_draw_line24_, DEPTH)(VGAState *s1, uint8_t *d,
396 const uint8_t *s, int width)
403 #if defined(TARGET_WORDS_BIGENDIAN)
412 ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
421 static void glue(vga_draw_line32_, DEPTH)(VGAState *s1, uint8_t *d,
422 const uint8_t *s, int width)
424 #if DEPTH == 32 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
425 memcpy(d, s, width * 4);
432 #if defined(TARGET_WORDS_BIGENDIAN)
441 ((PIXEL_TYPE *)d)[0] = glue(rgb_to_pixel, DEPTH)(r, g, b);
449 void glue(vga_draw_cursor_line_, DEPTH)(uint8_t *d1,
454 unsigned int color_xor)
456 const uint8_t *plane0, *plane1;
462 plane1 = src1 + poffset;
463 for(x = 0; x < w; x++) {
464 b0 = (plane0[x >> 3] >> (7 - (x & 7))) & 1;
465 b1 = (plane1[x >> 3] >> (7 - (x & 7))) & 1;
467 switch(b0 | (b1 << 1)) {
481 switch(b0 | (b1 << 1)) {
485 ((uint16_t *)d)[0] ^= color_xor;
488 ((uint16_t *)d)[0] = color0;
491 ((uint16_t *)d)[0] = color1;
495 switch(b0 | (b1 << 1)) {
499 ((uint32_t *)d)[0] ^= color_xor;
502 ((uint32_t *)d)[0] = color0;
505 ((uint32_t *)d)[0] = color1;
509 #error unsupported depth