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
25 static inline void vga_draw_glyph_line(uint8_t *d
, uint32_t font_data
,
26 uint32_t xorcol
, uint32_t bgcol
)
28 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
29 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
30 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
31 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
32 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
33 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
34 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
35 ((uint32_t *)d
)[7] = (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
38 static void vga_draw_glyph8(uint8_t *d
, int linesize
,
39 const uint8_t *font_ptr
, int h
,
40 uint32_t fgcol
, uint32_t bgcol
)
42 uint32_t font_data
, xorcol
;
44 xorcol
= bgcol
^ fgcol
;
46 font_data
= font_ptr
[0];
47 vga_draw_glyph_line(d
, font_data
, xorcol
, bgcol
);
53 static void vga_draw_glyph16(uint8_t *d
, int linesize
,
54 const uint8_t *font_ptr
, int h
,
55 uint32_t fgcol
, uint32_t bgcol
)
57 uint32_t font_data
, xorcol
;
59 xorcol
= bgcol
^ fgcol
;
61 font_data
= font_ptr
[0];
62 vga_draw_glyph_line(d
, expand4to8
[font_data
>> 4],
64 vga_draw_glyph_line(d
+ 32, expand4to8
[font_data
& 0x0f],
71 static void vga_draw_glyph9(uint8_t *d
, int linesize
,
72 const uint8_t *font_ptr
, int h
,
73 uint32_t fgcol
, uint32_t bgcol
, int dup9
)
75 uint32_t font_data
, xorcol
, v
;
77 xorcol
= bgcol
^ fgcol
;
79 font_data
= font_ptr
[0];
80 ((uint32_t *)d
)[0] = (-((font_data
>> 7)) & xorcol
) ^ bgcol
;
81 ((uint32_t *)d
)[1] = (-((font_data
>> 6) & 1) & xorcol
) ^ bgcol
;
82 ((uint32_t *)d
)[2] = (-((font_data
>> 5) & 1) & xorcol
) ^ bgcol
;
83 ((uint32_t *)d
)[3] = (-((font_data
>> 4) & 1) & xorcol
) ^ bgcol
;
84 ((uint32_t *)d
)[4] = (-((font_data
>> 3) & 1) & xorcol
) ^ bgcol
;
85 ((uint32_t *)d
)[5] = (-((font_data
>> 2) & 1) & xorcol
) ^ bgcol
;
86 ((uint32_t *)d
)[6] = (-((font_data
>> 1) & 1) & xorcol
) ^ bgcol
;
87 v
= (-((font_data
>> 0) & 1) & xorcol
) ^ bgcol
;
88 ((uint32_t *)d
)[7] = v
;
90 ((uint32_t *)d
)[8] = v
;
92 ((uint32_t *)d
)[8] = bgcol
;
98 static inline uint8_t vga_read_byte(VGACommonState
*vga
, uint32_t addr
)
100 return vga
->vram_ptr
[addr
& vga
->vbe_size_mask
];
103 static inline uint16_t vga_read_word_le(VGACommonState
*vga
, uint32_t addr
)
105 uint32_t offset
= addr
& vga
->vbe_size_mask
& ~1;
106 uint16_t *ptr
= (uint16_t *)(vga
->vram_ptr
+ offset
);
107 return lduw_le_p(ptr
);
110 static inline uint16_t vga_read_word_be(VGACommonState
*vga
, uint32_t addr
)
112 uint32_t offset
= addr
& vga
->vbe_size_mask
& ~1;
113 uint16_t *ptr
= (uint16_t *)(vga
->vram_ptr
+ offset
);
114 return lduw_be_p(ptr
);
117 static inline uint32_t vga_read_dword_le(VGACommonState
*vga
, uint32_t addr
)
119 uint32_t offset
= addr
& vga
->vbe_size_mask
& ~3;
120 uint32_t *ptr
= (uint32_t *)(vga
->vram_ptr
+ offset
);
121 return ldl_le_p(ptr
);
127 static void vga_draw_line2(VGACommonState
*vga
, uint8_t *d
,
128 uint32_t addr
, int width
)
130 uint32_t plane_mask
, *palette
, data
, v
;
133 palette
= vga
->last_palette
;
134 plane_mask
= mask16
[vga
->ar
[VGA_ATC_PLANE_ENABLE
] & 0xf];
136 for(x
= 0; x
< width
; x
++) {
137 data
= vga_read_dword_le(vga
, addr
);
139 v
= expand2
[GET_PLANE(data
, 0)];
140 v
|= expand2
[GET_PLANE(data
, 2)] << 2;
141 ((uint32_t *)d
)[0] = palette
[v
>> 12];
142 ((uint32_t *)d
)[1] = palette
[(v
>> 8) & 0xf];
143 ((uint32_t *)d
)[2] = palette
[(v
>> 4) & 0xf];
144 ((uint32_t *)d
)[3] = palette
[(v
>> 0) & 0xf];
146 v
= expand2
[GET_PLANE(data
, 1)];
147 v
|= expand2
[GET_PLANE(data
, 3)] << 2;
148 ((uint32_t *)d
)[4] = palette
[v
>> 12];
149 ((uint32_t *)d
)[5] = palette
[(v
>> 8) & 0xf];
150 ((uint32_t *)d
)[6] = palette
[(v
>> 4) & 0xf];
151 ((uint32_t *)d
)[7] = palette
[(v
>> 0) & 0xf];
157 #define PUT_PIXEL2(d, n, v) \
158 ((uint32_t *)d)[2*(n)] = ((uint32_t *)d)[2*(n)+1] = (v)
161 * 4 color mode, dup2 horizontal
163 static void vga_draw_line2d2(VGACommonState
*vga
, uint8_t *d
,
164 uint32_t addr
, int width
)
166 uint32_t plane_mask
, *palette
, data
, v
;
169 palette
= vga
->last_palette
;
170 plane_mask
= mask16
[vga
->ar
[VGA_ATC_PLANE_ENABLE
] & 0xf];
172 for(x
= 0; x
< width
; x
++) {
173 data
= vga_read_dword_le(vga
, addr
);
175 v
= expand2
[GET_PLANE(data
, 0)];
176 v
|= expand2
[GET_PLANE(data
, 2)] << 2;
177 PUT_PIXEL2(d
, 0, palette
[v
>> 12]);
178 PUT_PIXEL2(d
, 1, palette
[(v
>> 8) & 0xf]);
179 PUT_PIXEL2(d
, 2, palette
[(v
>> 4) & 0xf]);
180 PUT_PIXEL2(d
, 3, palette
[(v
>> 0) & 0xf]);
182 v
= expand2
[GET_PLANE(data
, 1)];
183 v
|= expand2
[GET_PLANE(data
, 3)] << 2;
184 PUT_PIXEL2(d
, 4, palette
[v
>> 12]);
185 PUT_PIXEL2(d
, 5, palette
[(v
>> 8) & 0xf]);
186 PUT_PIXEL2(d
, 6, palette
[(v
>> 4) & 0xf]);
187 PUT_PIXEL2(d
, 7, palette
[(v
>> 0) & 0xf]);
196 static void vga_draw_line4(VGACommonState
*vga
, uint8_t *d
,
197 uint32_t addr
, int width
)
199 uint32_t plane_mask
, data
, v
, *palette
;
202 palette
= vga
->last_palette
;
203 plane_mask
= mask16
[vga
->ar
[VGA_ATC_PLANE_ENABLE
] & 0xf];
205 for(x
= 0; x
< width
; x
++) {
206 data
= vga_read_dword_le(vga
, addr
);
208 v
= expand4
[GET_PLANE(data
, 0)];
209 v
|= expand4
[GET_PLANE(data
, 1)] << 1;
210 v
|= expand4
[GET_PLANE(data
, 2)] << 2;
211 v
|= expand4
[GET_PLANE(data
, 3)] << 3;
212 ((uint32_t *)d
)[0] = palette
[v
>> 28];
213 ((uint32_t *)d
)[1] = palette
[(v
>> 24) & 0xf];
214 ((uint32_t *)d
)[2] = palette
[(v
>> 20) & 0xf];
215 ((uint32_t *)d
)[3] = palette
[(v
>> 16) & 0xf];
216 ((uint32_t *)d
)[4] = palette
[(v
>> 12) & 0xf];
217 ((uint32_t *)d
)[5] = palette
[(v
>> 8) & 0xf];
218 ((uint32_t *)d
)[6] = palette
[(v
>> 4) & 0xf];
219 ((uint32_t *)d
)[7] = palette
[(v
>> 0) & 0xf];
226 * 16 color mode, dup2 horizontal
228 static void vga_draw_line4d2(VGACommonState
*vga
, uint8_t *d
,
229 uint32_t addr
, int width
)
231 uint32_t plane_mask
, data
, v
, *palette
;
234 palette
= vga
->last_palette
;
235 plane_mask
= mask16
[vga
->ar
[VGA_ATC_PLANE_ENABLE
] & 0xf];
237 for(x
= 0; x
< width
; x
++) {
238 data
= vga_read_dword_le(vga
, addr
);
240 v
= expand4
[GET_PLANE(data
, 0)];
241 v
|= expand4
[GET_PLANE(data
, 1)] << 1;
242 v
|= expand4
[GET_PLANE(data
, 2)] << 2;
243 v
|= expand4
[GET_PLANE(data
, 3)] << 3;
244 PUT_PIXEL2(d
, 0, palette
[v
>> 28]);
245 PUT_PIXEL2(d
, 1, palette
[(v
>> 24) & 0xf]);
246 PUT_PIXEL2(d
, 2, palette
[(v
>> 20) & 0xf]);
247 PUT_PIXEL2(d
, 3, palette
[(v
>> 16) & 0xf]);
248 PUT_PIXEL2(d
, 4, palette
[(v
>> 12) & 0xf]);
249 PUT_PIXEL2(d
, 5, palette
[(v
>> 8) & 0xf]);
250 PUT_PIXEL2(d
, 6, palette
[(v
>> 4) & 0xf]);
251 PUT_PIXEL2(d
, 7, palette
[(v
>> 0) & 0xf]);
258 * 256 color mode, double pixels
260 * XXX: add plane_mask support (never used in standard VGA modes)
262 static void vga_draw_line8d2(VGACommonState
*vga
, uint8_t *d
,
263 uint32_t addr
, int width
)
268 palette
= vga
->last_palette
;
270 for(x
= 0; x
< width
; x
++) {
271 PUT_PIXEL2(d
, 0, palette
[vga_read_byte(vga
, addr
+ 0)]);
272 PUT_PIXEL2(d
, 1, palette
[vga_read_byte(vga
, addr
+ 1)]);
273 PUT_PIXEL2(d
, 2, palette
[vga_read_byte(vga
, addr
+ 2)]);
274 PUT_PIXEL2(d
, 3, palette
[vga_read_byte(vga
, addr
+ 3)]);
281 * standard 256 color mode
283 * XXX: add plane_mask support (never used in standard VGA modes)
285 static void vga_draw_line8(VGACommonState
*vga
, uint8_t *d
,
286 uint32_t addr
, int width
)
291 palette
= vga
->last_palette
;
293 for(x
= 0; x
< width
; x
++) {
294 ((uint32_t *)d
)[0] = palette
[vga_read_byte(vga
, addr
+ 0)];
295 ((uint32_t *)d
)[1] = palette
[vga_read_byte(vga
, addr
+ 1)];
296 ((uint32_t *)d
)[2] = palette
[vga_read_byte(vga
, addr
+ 2)];
297 ((uint32_t *)d
)[3] = palette
[vga_read_byte(vga
, addr
+ 3)];
298 ((uint32_t *)d
)[4] = palette
[vga_read_byte(vga
, addr
+ 4)];
299 ((uint32_t *)d
)[5] = palette
[vga_read_byte(vga
, addr
+ 5)];
300 ((uint32_t *)d
)[6] = palette
[vga_read_byte(vga
, addr
+ 6)];
301 ((uint32_t *)d
)[7] = palette
[vga_read_byte(vga
, addr
+ 7)];
310 static void vga_draw_line15_le(VGACommonState
*vga
, uint8_t *d
,
311 uint32_t addr
, int width
)
318 v
= vga_read_word_le(vga
, addr
);
322 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
328 static void vga_draw_line15_be(VGACommonState
*vga
, uint8_t *d
,
329 uint32_t addr
, int width
)
336 v
= vga_read_word_be(vga
, addr
);
340 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
349 static void vga_draw_line16_le(VGACommonState
*vga
, uint8_t *d
,
350 uint32_t addr
, int width
)
357 v
= vga_read_word_le(vga
, addr
);
361 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
367 static void vga_draw_line16_be(VGACommonState
*vga
, uint8_t *d
,
368 uint32_t addr
, int width
)
375 v
= vga_read_word_be(vga
, addr
);
379 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
388 static void vga_draw_line24_le(VGACommonState
*vga
, uint8_t *d
,
389 uint32_t addr
, int width
)
396 b
= vga_read_byte(vga
, addr
+ 0);
397 g
= vga_read_byte(vga
, addr
+ 1);
398 r
= vga_read_byte(vga
, addr
+ 2);
399 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
405 static void vga_draw_line24_be(VGACommonState
*vga
, uint8_t *d
,
406 uint32_t addr
, int width
)
413 r
= vga_read_byte(vga
, addr
+ 0);
414 g
= vga_read_byte(vga
, addr
+ 1);
415 b
= vga_read_byte(vga
, addr
+ 2);
416 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
425 static void vga_draw_line32_le(VGACommonState
*vga
, uint8_t *d
,
426 uint32_t addr
, int width
)
433 b
= vga_read_byte(vga
, addr
+ 0);
434 g
= vga_read_byte(vga
, addr
+ 1);
435 r
= vga_read_byte(vga
, addr
+ 2);
436 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);
442 static void vga_draw_line32_be(VGACommonState
*vga
, uint8_t *d
,
443 uint32_t addr
, int width
)
450 r
= vga_read_byte(vga
, addr
+ 1);
451 g
= vga_read_byte(vga
, addr
+ 2);
452 b
= vga_read_byte(vga
, addr
+ 3);
453 ((uint32_t *)d
)[0] = rgb_to_pixel32(r
, g
, b
);