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
39 #define PIXEL_NAME glue(DEPTH, bgr)
41 #define PIXEL_NAME DEPTH
42 #endif /* BGR_FORMAT */
44 #if DEPTH != 15 && !defined(BGR_FORMAT)
46 static inline void glue(vga_draw_glyph_line_
, DEPTH
)(uint8_t *d
,
52 ((uint32_t *)d
)[0] = (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
;
53 ((uint32_t *)d
)[1] = (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
55 ((uint32_t *)d
)[0] = (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
;
56 ((uint32_t *)d
)[1] = (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
;
57 ((uint32_t *)d
)[2] = (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
;
58 ((uint32_t *)d
)[3] = (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
60 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
61 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
62 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
63 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
64 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
65 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
66 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
67 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
71 static void glue(vga_draw_glyph8_
, DEPTH
)(uint8_t *d
, int linesize
,
72 const uint8_t *font_ptr
, int h
,
73 uint32_t fgcol
, uint32_t bgcol
)
75 uint32_t font_data
, xorcol
;
77 xorcol
= bgcol
^ fgcol
;
79 font_data
= font_ptr
[0];
80 glue(vga_draw_glyph_line_
, DEPTH
)(d
, font_data
, xorcol
, bgcol
);
86 static void glue(vga_draw_glyph16_
, DEPTH
)(uint8_t *d
, int linesize
,
87 const uint8_t *font_ptr
, int h
,
88 uint32_t fgcol
, uint32_t bgcol
)
90 uint32_t font_data
, xorcol
;
92 xorcol
= bgcol
^ fgcol
;
94 font_data
= font_ptr
[0];
95 glue(vga_draw_glyph_line_
, DEPTH
)(d
,
96 expand4to8
[font_data
>> 4],
98 glue(vga_draw_glyph_line_
, DEPTH
)(d
+ 8 * BPP
,
99 expand4to8
[font_data
& 0x0f],
106 static void glue(vga_draw_glyph9_
, DEPTH
)(uint8_t *d
, int linesize
,
107 const uint8_t *font_ptr
, int h
,
108 uint32_t fgcol
, uint32_t bgcol
, int dup9
)
110 uint32_t font_data
, xorcol
, v
;
112 xorcol
= bgcol
^ fgcol
;
114 font_data
= font_ptr
[0];
116 cpu_to_32wu((uint32_t *)d
, (dmask16
[(font_data
>> 4)] & xorcol
) ^ bgcol
);
117 v
= (dmask16
[(font_data
>> 0) & 0xf] & xorcol
) ^ bgcol
;
118 cpu_to_32wu(((uint32_t *)d
)+1, v
);
120 ((uint8_t *)d
)[8] = v
>> (24 * (1 - BIG
));
122 ((uint8_t *)d
)[8] = bgcol
;
125 cpu_to_32wu(((uint32_t *)d
)+0, (dmask4
[(font_data
>> 6)] & xorcol
) ^ bgcol
);
126 cpu_to_32wu(((uint32_t *)d
)+1, (dmask4
[(font_data
>> 4) & 3] & xorcol
) ^ bgcol
);
127 cpu_to_32wu(((uint32_t *)d
)+2, (dmask4
[(font_data
>> 2) & 3] & xorcol
) ^ bgcol
);
128 v
= (dmask4
[(font_data
>> 0) & 3] & xorcol
) ^ bgcol
;
129 cpu_to_32wu(((uint32_t *)d
)+3, v
);
131 ((uint16_t *)d
)[8] = v
>> (16 * (1 - BIG
));
133 ((uint16_t *)d
)[8] = bgcol
;
135 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
136 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
137 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
138 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
139 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
140 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
141 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
142 v
= (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
143 ((uint32_t *)d
)[7] = v
;
145 ((uint32_t *)d
)[8] = v
;
147 ((uint32_t *)d
)[8] = bgcol
;
157 static void glue(vga_draw_line2_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
158 const uint8_t *s
, int width
)
160 uint32_t plane_mask
, *palette
, data
, v
;
163 palette
= s1
->last_palette
;
164 plane_mask
= mask16
[s1
->ar
[0x12] & 0xf];
166 for(x
= 0; x
< width
; x
++) {
167 data
= ((uint32_t *)s
)[0];
169 v
= expand2
[GET_PLANE(data
, 0)];
170 v
|= expand2
[GET_PLANE(data
, 2)] << 2;
171 ((PIXEL_TYPE
*)d
)[0] = palette
[v
>> 12];
172 ((PIXEL_TYPE
*)d
)[1] = palette
[(v
>> 8) & 0xf];
173 ((PIXEL_TYPE
*)d
)[2] = palette
[(v
>> 4) & 0xf];
174 ((PIXEL_TYPE
*)d
)[3] = palette
[(v
>> 0) & 0xf];
176 v
= expand2
[GET_PLANE(data
, 1)];
177 v
|= expand2
[GET_PLANE(data
, 3)] << 2;
178 ((PIXEL_TYPE
*)d
)[4] = palette
[v
>> 12];
179 ((PIXEL_TYPE
*)d
)[5] = palette
[(v
>> 8) & 0xf];
180 ((PIXEL_TYPE
*)d
)[6] = palette
[(v
>> 4) & 0xf];
181 ((PIXEL_TYPE
*)d
)[7] = palette
[(v
>> 0) & 0xf];
188 #define PUT_PIXEL2(d, n, v) ((uint16_t *)d)[(n)] = (v)
190 #define PUT_PIXEL2(d, n, v) ((uint32_t *)d)[(n)] = (v)
192 #define PUT_PIXEL2(d, n, v) \
193 ((uint32_t *)d)[2*(n)] = ((uint32_t *)d)[2*(n)+1] = (v)
197 * 4 color mode, dup2 horizontal
199 static void glue(vga_draw_line2d2_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
200 const uint8_t *s
, int width
)
202 uint32_t plane_mask
, *palette
, data
, v
;
205 palette
= s1
->last_palette
;
206 plane_mask
= mask16
[s1
->ar
[0x12] & 0xf];
208 for(x
= 0; x
< width
; x
++) {
209 data
= ((uint32_t *)s
)[0];
211 v
= expand2
[GET_PLANE(data
, 0)];
212 v
|= expand2
[GET_PLANE(data
, 2)] << 2;
213 PUT_PIXEL2(d
, 0, palette
[v
>> 12]);
214 PUT_PIXEL2(d
, 1, palette
[(v
>> 8) & 0xf]);
215 PUT_PIXEL2(d
, 2, palette
[(v
>> 4) & 0xf]);
216 PUT_PIXEL2(d
, 3, palette
[(v
>> 0) & 0xf]);
218 v
= expand2
[GET_PLANE(data
, 1)];
219 v
|= expand2
[GET_PLANE(data
, 3)] << 2;
220 PUT_PIXEL2(d
, 4, palette
[v
>> 12]);
221 PUT_PIXEL2(d
, 5, palette
[(v
>> 8) & 0xf]);
222 PUT_PIXEL2(d
, 6, palette
[(v
>> 4) & 0xf]);
223 PUT_PIXEL2(d
, 7, palette
[(v
>> 0) & 0xf]);
232 static void glue(vga_draw_line4_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
233 const uint8_t *s
, int width
)
235 uint32_t plane_mask
, data
, v
, *palette
;
238 palette
= s1
->last_palette
;
239 plane_mask
= mask16
[s1
->ar
[0x12] & 0xf];
241 for(x
= 0; x
< width
; x
++) {
242 data
= ((uint32_t *)s
)[0];
244 v
= expand4
[GET_PLANE(data
, 0)];
245 v
|= expand4
[GET_PLANE(data
, 1)] << 1;
246 v
|= expand4
[GET_PLANE(data
, 2)] << 2;
247 v
|= expand4
[GET_PLANE(data
, 3)] << 3;
248 ((PIXEL_TYPE
*)d
)[0] = palette
[v
>> 28];
249 ((PIXEL_TYPE
*)d
)[1] = palette
[(v
>> 24) & 0xf];
250 ((PIXEL_TYPE
*)d
)[2] = palette
[(v
>> 20) & 0xf];
251 ((PIXEL_TYPE
*)d
)[3] = palette
[(v
>> 16) & 0xf];
252 ((PIXEL_TYPE
*)d
)[4] = palette
[(v
>> 12) & 0xf];
253 ((PIXEL_TYPE
*)d
)[5] = palette
[(v
>> 8) & 0xf];
254 ((PIXEL_TYPE
*)d
)[6] = palette
[(v
>> 4) & 0xf];
255 ((PIXEL_TYPE
*)d
)[7] = palette
[(v
>> 0) & 0xf];
262 * 16 color mode, dup2 horizontal
264 static void glue(vga_draw_line4d2_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
265 const uint8_t *s
, int width
)
267 uint32_t plane_mask
, data
, v
, *palette
;
270 palette
= s1
->last_palette
;
271 plane_mask
= mask16
[s1
->ar
[0x12] & 0xf];
273 for(x
= 0; x
< width
; x
++) {
274 data
= ((uint32_t *)s
)[0];
276 v
= expand4
[GET_PLANE(data
, 0)];
277 v
|= expand4
[GET_PLANE(data
, 1)] << 1;
278 v
|= expand4
[GET_PLANE(data
, 2)] << 2;
279 v
|= expand4
[GET_PLANE(data
, 3)] << 3;
280 PUT_PIXEL2(d
, 0, palette
[v
>> 28]);
281 PUT_PIXEL2(d
, 1, palette
[(v
>> 24) & 0xf]);
282 PUT_PIXEL2(d
, 2, palette
[(v
>> 20) & 0xf]);
283 PUT_PIXEL2(d
, 3, palette
[(v
>> 16) & 0xf]);
284 PUT_PIXEL2(d
, 4, palette
[(v
>> 12) & 0xf]);
285 PUT_PIXEL2(d
, 5, palette
[(v
>> 8) & 0xf]);
286 PUT_PIXEL2(d
, 6, palette
[(v
>> 4) & 0xf]);
287 PUT_PIXEL2(d
, 7, palette
[(v
>> 0) & 0xf]);
294 * 256 color mode, double pixels
296 * XXX: add plane_mask support (never used in standard VGA modes)
298 static void glue(vga_draw_line8d2_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
299 const uint8_t *s
, int width
)
304 palette
= s1
->last_palette
;
306 for(x
= 0; x
< width
; x
++) {
307 PUT_PIXEL2(d
, 0, palette
[s
[0]]);
308 PUT_PIXEL2(d
, 1, palette
[s
[1]]);
309 PUT_PIXEL2(d
, 2, palette
[s
[2]]);
310 PUT_PIXEL2(d
, 3, palette
[s
[3]]);
317 * standard 256 color mode
319 * XXX: add plane_mask support (never used in standard VGA modes)
321 static void glue(vga_draw_line8_
, DEPTH
)(VGAState
*s1
, uint8_t *d
,
322 const uint8_t *s
, int width
)
327 palette
= s1
->last_palette
;
329 for(x
= 0; x
< width
; x
++) {
330 ((PIXEL_TYPE
*)d
)[0] = palette
[s
[0]];
331 ((PIXEL_TYPE
*)d
)[1] = palette
[s
[1]];
332 ((PIXEL_TYPE
*)d
)[2] = palette
[s
[2]];
333 ((PIXEL_TYPE
*)d
)[3] = palette
[s
[3]];
334 ((PIXEL_TYPE
*)d
)[4] = palette
[s
[4]];
335 ((PIXEL_TYPE
*)d
)[5] = palette
[s
[5]];
336 ((PIXEL_TYPE
*)d
)[6] = palette
[s
[6]];
337 ((PIXEL_TYPE
*)d
)[7] = palette
[s
[7]];
343 void glue(vga_draw_cursor_line_
, DEPTH
)(uint8_t *d1
,
348 unsigned int color_xor
)
350 const uint8_t *plane0
, *plane1
;
356 plane1
= src1
+ poffset
;
357 for(x
= 0; x
< w
; x
++) {
358 b0
= (plane0
[x
>> 3] >> (7 - (x
& 7))) & 1;
359 b1
= (plane1
[x
>> 3] >> (7 - (x
& 7))) & 1;
361 switch(b0
| (b1
<< 1)) {
375 switch(b0
| (b1
<< 1)) {
379 ((uint16_t *)d
)[0] ^= color_xor
;
382 ((uint16_t *)d
)[0] = color0
;
385 ((uint16_t *)d
)[0] = color1
;
389 switch(b0
| (b1
<< 1)) {
393 ((uint32_t *)d
)[0] ^= color_xor
;
396 ((uint32_t *)d
)[0] = color0
;
399 ((uint32_t *)d
)[0] = color1
;
403 #error unsupported depth
409 #endif /* DEPTH != 15 */
417 static void glue(vga_draw_line15_
, PIXEL_NAME
)(VGAState
*s1
, uint8_t *d
,
418 const uint8_t *s
, int width
)
420 #if DEPTH == 15 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
421 memcpy(d
, s
, width
* 2);
428 v
= lduw_raw((void *)s
);
432 ((PIXEL_TYPE
*)d
)[0] = glue(rgb_to_pixel
, PIXEL_NAME
)(r
, g
, b
);
442 static void glue(vga_draw_line16_
, PIXEL_NAME
)(VGAState
*s1
, uint8_t *d
,
443 const uint8_t *s
, int width
)
445 #if DEPTH == 16 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
446 memcpy(d
, s
, width
* 2);
453 v
= lduw_raw((void *)s
);
457 ((PIXEL_TYPE
*)d
)[0] = glue(rgb_to_pixel
, PIXEL_NAME
)(r
, g
, b
);
467 static void glue(vga_draw_line24_
, PIXEL_NAME
)(VGAState
*s1
, uint8_t *d
,
468 const uint8_t *s
, int width
)
475 #if defined(TARGET_WORDS_BIGENDIAN)
484 ((PIXEL_TYPE
*)d
)[0] = glue(rgb_to_pixel
, PIXEL_NAME
)(r
, g
, b
);
493 static void glue(vga_draw_line32_
, PIXEL_NAME
)(VGAState
*s1
, uint8_t *d
,
494 const uint8_t *s
, int width
)
496 #if DEPTH == 32 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN) && !defined(BGR_FORMAT)
497 memcpy(d
, s
, width
* 4);
504 #if defined(TARGET_WORDS_BIGENDIAN)
513 ((PIXEL_TYPE
*)d
)[0] = glue(rgb_to_pixel
, PIXEL_NAME
)(r
, g
, b
);