kvm: external module: fix anon_inodes for 2.6.22
[qemu-kvm/fedora.git] / hw / vga.c
blob95d603356935f0c505712e2ad7558514d4472b1f
1 /*
2 * QEMU VGA Emulator.
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "console.h"
26 #include "pc.h"
27 #include "pci.h"
28 #include "vga_int.h"
29 #include "pixel_ops.h"
30 #include "qemu-kvm.h"
32 #include <sys/mman.h>
34 //#define DEBUG_VGA
35 //#define DEBUG_VGA_MEM
36 //#define DEBUG_VGA_REG
38 //#define DEBUG_BOCHS_VBE
40 /* force some bits to zero */
41 const uint8_t sr_mask[8] = {
42 (uint8_t)~0xfc,
43 (uint8_t)~0xc2,
44 (uint8_t)~0xf0,
45 (uint8_t)~0xc0,
46 (uint8_t)~0xf1,
47 (uint8_t)~0xff,
48 (uint8_t)~0xff,
49 (uint8_t)~0x00,
52 const uint8_t gr_mask[16] = {
53 (uint8_t)~0xf0, /* 0x00 */
54 (uint8_t)~0xf0, /* 0x01 */
55 (uint8_t)~0xf0, /* 0x02 */
56 (uint8_t)~0xe0, /* 0x03 */
57 (uint8_t)~0xfc, /* 0x04 */
58 (uint8_t)~0x84, /* 0x05 */
59 (uint8_t)~0xf0, /* 0x06 */
60 (uint8_t)~0xf0, /* 0x07 */
61 (uint8_t)~0x00, /* 0x08 */
62 (uint8_t)~0xff, /* 0x09 */
63 (uint8_t)~0xff, /* 0x0a */
64 (uint8_t)~0xff, /* 0x0b */
65 (uint8_t)~0xff, /* 0x0c */
66 (uint8_t)~0xff, /* 0x0d */
67 (uint8_t)~0xff, /* 0x0e */
68 (uint8_t)~0xff, /* 0x0f */
71 #define cbswap_32(__x) \
72 ((uint32_t)( \
73 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
74 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
75 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
76 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
78 #ifdef WORDS_BIGENDIAN
79 #define PAT(x) cbswap_32(x)
80 #else
81 #define PAT(x) (x)
82 #endif
84 #ifdef WORDS_BIGENDIAN
85 #define BIG 1
86 #else
87 #define BIG 0
88 #endif
90 #ifdef WORDS_BIGENDIAN
91 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
92 #else
93 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
94 #endif
96 static const uint32_t mask16[16] = {
97 PAT(0x00000000),
98 PAT(0x000000ff),
99 PAT(0x0000ff00),
100 PAT(0x0000ffff),
101 PAT(0x00ff0000),
102 PAT(0x00ff00ff),
103 PAT(0x00ffff00),
104 PAT(0x00ffffff),
105 PAT(0xff000000),
106 PAT(0xff0000ff),
107 PAT(0xff00ff00),
108 PAT(0xff00ffff),
109 PAT(0xffff0000),
110 PAT(0xffff00ff),
111 PAT(0xffffff00),
112 PAT(0xffffffff),
115 #undef PAT
117 #ifdef WORDS_BIGENDIAN
118 #define PAT(x) (x)
119 #else
120 #define PAT(x) cbswap_32(x)
121 #endif
123 static const uint32_t dmask16[16] = {
124 PAT(0x00000000),
125 PAT(0x000000ff),
126 PAT(0x0000ff00),
127 PAT(0x0000ffff),
128 PAT(0x00ff0000),
129 PAT(0x00ff00ff),
130 PAT(0x00ffff00),
131 PAT(0x00ffffff),
132 PAT(0xff000000),
133 PAT(0xff0000ff),
134 PAT(0xff00ff00),
135 PAT(0xff00ffff),
136 PAT(0xffff0000),
137 PAT(0xffff00ff),
138 PAT(0xffffff00),
139 PAT(0xffffffff),
142 static const uint32_t dmask4[4] = {
143 PAT(0x00000000),
144 PAT(0x0000ffff),
145 PAT(0xffff0000),
146 PAT(0xffffffff),
149 static uint32_t expand4[256];
150 static uint16_t expand2[256];
151 static uint8_t expand4to8[16];
153 static void vga_screen_dump(void *opaque, const char *filename);
155 static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
157 VGAState *s = opaque;
158 int val, index;
160 /* check port range access depending on color/monochrome mode */
161 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION)) ||
162 (addr >= 0x3d0 && addr <= 0x3df && !(s->msr & MSR_COLOR_EMULATION))) {
163 val = 0xff;
164 } else {
165 switch(addr) {
166 case 0x3c0:
167 if (s->ar_flip_flop == 0) {
168 val = s->ar_index;
169 } else {
170 val = 0;
172 break;
173 case 0x3c1:
174 index = s->ar_index & 0x1f;
175 if (index < 21)
176 val = s->ar[index];
177 else
178 val = 0;
179 break;
180 case 0x3c2:
181 val = s->st00;
182 break;
183 case 0x3c4:
184 val = s->sr_index;
185 break;
186 case 0x3c5:
187 val = s->sr[s->sr_index];
188 #ifdef DEBUG_VGA_REG
189 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
190 #endif
191 break;
192 case 0x3c7:
193 val = s->dac_state;
194 break;
195 case 0x3c8:
196 val = s->dac_write_index;
197 break;
198 case 0x3c9:
199 val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
200 if (++s->dac_sub_index == 3) {
201 s->dac_sub_index = 0;
202 s->dac_read_index++;
204 break;
205 case 0x3ca:
206 val = s->fcr;
207 break;
208 case 0x3cc:
209 val = s->msr;
210 break;
211 case 0x3ce:
212 val = s->gr_index;
213 break;
214 case 0x3cf:
215 val = s->gr[s->gr_index];
216 #ifdef DEBUG_VGA_REG
217 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
218 #endif
219 break;
220 case 0x3b4:
221 case 0x3d4:
222 val = s->cr_index;
223 break;
224 case 0x3b5:
225 case 0x3d5:
226 val = s->cr[s->cr_index];
227 #ifdef DEBUG_VGA_REG
228 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
229 #endif
230 break;
231 case 0x3ba:
232 case 0x3da:
233 /* just toggle to fool polling */
234 s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
235 val = s->st01;
236 s->ar_flip_flop = 0;
237 break;
238 default:
239 val = 0x00;
240 break;
243 #if defined(DEBUG_VGA)
244 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
245 #endif
246 return val;
249 static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
251 VGAState *s = opaque;
252 int index;
254 /* check port range access depending on color/monochrome mode */
255 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION)) ||
256 (addr >= 0x3d0 && addr <= 0x3df && !(s->msr & MSR_COLOR_EMULATION)))
257 return;
259 #ifdef DEBUG_VGA
260 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
261 #endif
263 switch(addr) {
264 case 0x3c0:
265 if (s->ar_flip_flop == 0) {
266 val &= 0x3f;
267 s->ar_index = val;
268 } else {
269 index = s->ar_index & 0x1f;
270 switch(index) {
271 case 0x00 ... 0x0f:
272 s->ar[index] = val & 0x3f;
273 break;
274 case 0x10:
275 s->ar[index] = val & ~0x10;
276 break;
277 case 0x11:
278 s->ar[index] = val;
279 break;
280 case 0x12:
281 s->ar[index] = val & ~0xc0;
282 break;
283 case 0x13:
284 s->ar[index] = val & ~0xf0;
285 break;
286 case 0x14:
287 s->ar[index] = val & ~0xf0;
288 break;
289 default:
290 break;
293 s->ar_flip_flop ^= 1;
294 break;
295 case 0x3c2:
296 s->msr = val & ~0x10;
297 break;
298 case 0x3c4:
299 s->sr_index = val & 7;
300 break;
301 case 0x3c5:
302 #ifdef DEBUG_VGA_REG
303 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
304 #endif
305 s->sr[s->sr_index] = val & sr_mask[s->sr_index];
306 break;
307 case 0x3c7:
308 s->dac_read_index = val;
309 s->dac_sub_index = 0;
310 s->dac_state = 3;
311 break;
312 case 0x3c8:
313 s->dac_write_index = val;
314 s->dac_sub_index = 0;
315 s->dac_state = 0;
316 break;
317 case 0x3c9:
318 s->dac_cache[s->dac_sub_index] = val;
319 if (++s->dac_sub_index == 3) {
320 memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
321 s->dac_sub_index = 0;
322 s->dac_write_index++;
324 break;
325 case 0x3ce:
326 s->gr_index = val & 0x0f;
327 break;
328 case 0x3cf:
329 #ifdef DEBUG_VGA_REG
330 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
331 #endif
332 s->gr[s->gr_index] = val & gr_mask[s->gr_index];
333 break;
334 case 0x3b4:
335 case 0x3d4:
336 s->cr_index = val;
337 break;
338 case 0x3b5:
339 case 0x3d5:
340 #ifdef DEBUG_VGA_REG
341 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
342 #endif
343 /* handle CR0-7 protection */
344 if ((s->cr[0x11] & 0x80) && s->cr_index <= 7) {
345 /* can always write bit 4 of CR7 */
346 if (s->cr_index == 7)
347 s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
348 return;
350 switch(s->cr_index) {
351 case 0x01: /* horizontal display end */
352 case 0x07:
353 case 0x09:
354 case 0x0c:
355 case 0x0d:
356 case 0x12: /* vertical display end */
357 s->cr[s->cr_index] = val;
358 break;
359 default:
360 s->cr[s->cr_index] = val;
361 break;
363 break;
364 case 0x3ba:
365 case 0x3da:
366 s->fcr = val & 0x10;
367 break;
371 #ifdef CONFIG_BOCHS_VBE
372 static uint32_t vbe_ioport_read_index(void *opaque, uint32_t addr)
374 VGAState *s = opaque;
375 uint32_t val;
376 val = s->vbe_index;
377 return val;
380 static uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
382 VGAState *s = opaque;
383 uint32_t val;
385 if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
386 if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_GETCAPS) {
387 switch(s->vbe_index) {
388 /* XXX: do not hardcode ? */
389 case VBE_DISPI_INDEX_XRES:
390 val = VBE_DISPI_MAX_XRES;
391 break;
392 case VBE_DISPI_INDEX_YRES:
393 val = VBE_DISPI_MAX_YRES;
394 break;
395 case VBE_DISPI_INDEX_BPP:
396 val = VBE_DISPI_MAX_BPP;
397 break;
398 default:
399 val = s->vbe_regs[s->vbe_index];
400 break;
402 } else {
403 val = s->vbe_regs[s->vbe_index];
405 } else {
406 val = 0;
408 #ifdef DEBUG_BOCHS_VBE
409 printf("VBE: read index=0x%x val=0x%x\n", s->vbe_index, val);
410 #endif
411 return val;
414 static void vbe_ioport_write_index(void *opaque, uint32_t addr, uint32_t val)
416 VGAState *s = opaque;
417 s->vbe_index = val;
420 static void vbe_ioport_write_data(void *opaque, uint32_t addr, uint32_t val)
422 VGAState *s = opaque;
424 if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
425 #ifdef DEBUG_BOCHS_VBE
426 printf("VBE: write index=0x%x val=0x%x\n", s->vbe_index, val);
427 #endif
428 switch(s->vbe_index) {
429 case VBE_DISPI_INDEX_ID:
430 if (val == VBE_DISPI_ID0 ||
431 val == VBE_DISPI_ID1 ||
432 val == VBE_DISPI_ID2 ||
433 val == VBE_DISPI_ID3 ||
434 val == VBE_DISPI_ID4) {
435 s->vbe_regs[s->vbe_index] = val;
437 break;
438 case VBE_DISPI_INDEX_XRES:
439 if ((val <= VBE_DISPI_MAX_XRES) && ((val & 7) == 0)) {
440 s->vbe_regs[s->vbe_index] = val;
442 break;
443 case VBE_DISPI_INDEX_YRES:
444 if (val <= VBE_DISPI_MAX_YRES) {
445 s->vbe_regs[s->vbe_index] = val;
447 break;
448 case VBE_DISPI_INDEX_BPP:
449 if (val == 0)
450 val = 8;
451 if (val == 4 || val == 8 || val == 15 ||
452 val == 16 || val == 24 || val == 32) {
453 s->vbe_regs[s->vbe_index] = val;
455 break;
456 case VBE_DISPI_INDEX_BANK:
457 if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
458 val &= (s->vbe_bank_mask >> 2);
459 } else {
460 val &= s->vbe_bank_mask;
462 s->vbe_regs[s->vbe_index] = val;
463 s->bank_offset = (val << 16);
464 break;
465 case VBE_DISPI_INDEX_ENABLE:
466 if ((val & VBE_DISPI_ENABLED) &&
467 !(s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) {
468 int h, shift_control;
470 s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] =
471 s->vbe_regs[VBE_DISPI_INDEX_XRES];
472 s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] =
473 s->vbe_regs[VBE_DISPI_INDEX_YRES];
474 s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET] = 0;
475 s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET] = 0;
477 if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
478 s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 1;
479 else
480 s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] *
481 ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
482 s->vbe_start_addr = 0;
484 /* clear the screen (should be done in BIOS) */
485 if (!(val & VBE_DISPI_NOCLEARMEM)) {
486 memset(s->vram_ptr, 0,
487 s->vbe_regs[VBE_DISPI_INDEX_YRES] * s->vbe_line_offset);
490 /* we initialize the VGA graphic mode (should be done
491 in BIOS) */
492 s->gr[0x06] = (s->gr[0x06] & ~0x0c) | 0x05; /* graphic mode + memory map 1 */
493 s->cr[0x17] |= 3; /* no CGA modes */
494 s->cr[0x13] = s->vbe_line_offset >> 3;
495 /* width */
496 s->cr[0x01] = (s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
497 /* height (only meaningful if < 1024) */
498 h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
499 s->cr[0x12] = h;
500 s->cr[0x07] = (s->cr[0x07] & ~0x42) |
501 ((h >> 7) & 0x02) | ((h >> 3) & 0x40);
502 /* line compare to 1023 */
503 s->cr[0x18] = 0xff;
504 s->cr[0x07] |= 0x10;
505 s->cr[0x09] |= 0x40;
507 if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
508 shift_control = 0;
509 s->sr[0x01] &= ~8; /* no double line */
510 } else {
511 shift_control = 2;
512 s->sr[4] |= 0x08; /* set chain 4 mode */
513 s->sr[2] |= 0x0f; /* activate all planes */
515 s->gr[0x05] = (s->gr[0x05] & ~0x60) | (shift_control << 5);
516 s->cr[0x09] &= ~0x9f; /* no double scan */
517 } else {
518 /* XXX: the bios should do that */
519 s->bank_offset = 0;
521 s->dac_8bit = (val & VBE_DISPI_8BIT_DAC) > 0;
522 s->vbe_regs[s->vbe_index] = val;
523 break;
524 case VBE_DISPI_INDEX_VIRT_WIDTH:
526 int w, h, line_offset;
528 if (val < s->vbe_regs[VBE_DISPI_INDEX_XRES])
529 return;
530 w = val;
531 if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
532 line_offset = w >> 1;
533 else
534 line_offset = w * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
535 h = s->vram_size / line_offset;
536 /* XXX: support weird bochs semantics ? */
537 if (h < s->vbe_regs[VBE_DISPI_INDEX_YRES])
538 return;
539 s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] = w;
540 s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] = h;
541 s->vbe_line_offset = line_offset;
543 break;
544 case VBE_DISPI_INDEX_X_OFFSET:
545 case VBE_DISPI_INDEX_Y_OFFSET:
547 int x;
548 s->vbe_regs[s->vbe_index] = val;
549 s->vbe_start_addr = s->vbe_line_offset * s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET];
550 x = s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET];
551 if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
552 s->vbe_start_addr += x >> 1;
553 else
554 s->vbe_start_addr += x * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
555 s->vbe_start_addr >>= 2;
557 break;
558 default:
559 break;
563 #endif
565 /* called for accesses between 0xa0000 and 0xc0000 */
566 uint32_t vga_mem_readb(void *opaque, target_phys_addr_t addr)
568 VGAState *s = opaque;
569 int memory_map_mode, plane;
570 uint32_t ret;
572 /* convert to VGA memory offset */
573 memory_map_mode = (s->gr[6] >> 2) & 3;
574 addr &= 0x1ffff;
575 switch(memory_map_mode) {
576 case 0:
577 break;
578 case 1:
579 if (addr >= 0x10000)
580 return 0xff;
581 addr += s->bank_offset;
582 break;
583 case 2:
584 addr -= 0x10000;
585 if (addr >= 0x8000)
586 return 0xff;
587 break;
588 default:
589 case 3:
590 addr -= 0x18000;
591 if (addr >= 0x8000)
592 return 0xff;
593 break;
596 if (s->sr[4] & 0x08) {
597 /* chain 4 mode : simplest access */
598 ret = s->vram_ptr[addr];
599 } else if (s->gr[5] & 0x10) {
600 /* odd/even mode (aka text mode mapping) */
601 plane = (s->gr[4] & 2) | (addr & 1);
602 ret = s->vram_ptr[((addr & ~1) << 1) | plane];
603 } else {
604 /* standard VGA latched access */
605 s->latch = ((uint32_t *)s->vram_ptr)[addr];
607 if (!(s->gr[5] & 0x08)) {
608 /* read mode 0 */
609 plane = s->gr[4];
610 ret = GET_PLANE(s->latch, plane);
611 } else {
612 /* read mode 1 */
613 ret = (s->latch ^ mask16[s->gr[2]]) & mask16[s->gr[7]];
614 ret |= ret >> 16;
615 ret |= ret >> 8;
616 ret = (~ret) & 0xff;
619 return ret;
622 static uint32_t vga_mem_readw(void *opaque, target_phys_addr_t addr)
624 uint32_t v;
625 #ifdef TARGET_WORDS_BIGENDIAN
626 v = vga_mem_readb(opaque, addr) << 8;
627 v |= vga_mem_readb(opaque, addr + 1);
628 #else
629 v = vga_mem_readb(opaque, addr);
630 v |= vga_mem_readb(opaque, addr + 1) << 8;
631 #endif
632 return v;
635 static uint32_t vga_mem_readl(void *opaque, target_phys_addr_t addr)
637 uint32_t v;
638 #ifdef TARGET_WORDS_BIGENDIAN
639 v = vga_mem_readb(opaque, addr) << 24;
640 v |= vga_mem_readb(opaque, addr + 1) << 16;
641 v |= vga_mem_readb(opaque, addr + 2) << 8;
642 v |= vga_mem_readb(opaque, addr + 3);
643 #else
644 v = vga_mem_readb(opaque, addr);
645 v |= vga_mem_readb(opaque, addr + 1) << 8;
646 v |= vga_mem_readb(opaque, addr + 2) << 16;
647 v |= vga_mem_readb(opaque, addr + 3) << 24;
648 #endif
649 return v;
652 /* called for accesses between 0xa0000 and 0xc0000 */
653 void vga_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
655 VGAState *s = opaque;
656 int memory_map_mode, plane, write_mode, b, func_select, mask;
657 uint32_t write_mask, bit_mask, set_mask;
659 #ifdef DEBUG_VGA_MEM
660 printf("vga: [0x%x] = 0x%02x\n", addr, val);
661 #endif
662 /* convert to VGA memory offset */
663 memory_map_mode = (s->gr[6] >> 2) & 3;
664 addr &= 0x1ffff;
665 switch(memory_map_mode) {
666 case 0:
667 break;
668 case 1:
669 if (addr >= 0x10000)
670 return;
671 addr += s->bank_offset;
672 break;
673 case 2:
674 addr -= 0x10000;
675 if (addr >= 0x8000)
676 return;
677 break;
678 default:
679 case 3:
680 addr -= 0x18000;
681 if (addr >= 0x8000)
682 return;
683 break;
686 if (s->sr[4] & 0x08) {
687 /* chain 4 mode : simplest access */
688 plane = addr & 3;
689 mask = (1 << plane);
690 if (s->sr[2] & mask) {
691 s->vram_ptr[addr] = val;
692 #ifdef DEBUG_VGA_MEM
693 printf("vga: chain4: [0x%x]\n", addr);
694 #endif
695 s->plane_updated |= mask; /* only used to detect font change */
696 cpu_physical_memory_set_dirty(s->vram_offset + addr);
698 } else if (s->gr[5] & 0x10) {
699 /* odd/even mode (aka text mode mapping) */
700 plane = (s->gr[4] & 2) | (addr & 1);
701 mask = (1 << plane);
702 if (s->sr[2] & mask) {
703 addr = ((addr & ~1) << 1) | plane;
704 s->vram_ptr[addr] = val;
705 #ifdef DEBUG_VGA_MEM
706 printf("vga: odd/even: [0x%x]\n", addr);
707 #endif
708 s->plane_updated |= mask; /* only used to detect font change */
709 cpu_physical_memory_set_dirty(s->vram_offset + addr);
711 } else {
712 /* standard VGA latched access */
713 write_mode = s->gr[5] & 3;
714 switch(write_mode) {
715 default:
716 case 0:
717 /* rotate */
718 b = s->gr[3] & 7;
719 val = ((val >> b) | (val << (8 - b))) & 0xff;
720 val |= val << 8;
721 val |= val << 16;
723 /* apply set/reset mask */
724 set_mask = mask16[s->gr[1]];
725 val = (val & ~set_mask) | (mask16[s->gr[0]] & set_mask);
726 bit_mask = s->gr[8];
727 break;
728 case 1:
729 val = s->latch;
730 goto do_write;
731 case 2:
732 val = mask16[val & 0x0f];
733 bit_mask = s->gr[8];
734 break;
735 case 3:
736 /* rotate */
737 b = s->gr[3] & 7;
738 val = (val >> b) | (val << (8 - b));
740 bit_mask = s->gr[8] & val;
741 val = mask16[s->gr[0]];
742 break;
745 /* apply logical operation */
746 func_select = s->gr[3] >> 3;
747 switch(func_select) {
748 case 0:
749 default:
750 /* nothing to do */
751 break;
752 case 1:
753 /* and */
754 val &= s->latch;
755 break;
756 case 2:
757 /* or */
758 val |= s->latch;
759 break;
760 case 3:
761 /* xor */
762 val ^= s->latch;
763 break;
766 /* apply bit mask */
767 bit_mask |= bit_mask << 8;
768 bit_mask |= bit_mask << 16;
769 val = (val & bit_mask) | (s->latch & ~bit_mask);
771 do_write:
772 /* mask data according to sr[2] */
773 mask = s->sr[2];
774 s->plane_updated |= mask; /* only used to detect font change */
775 write_mask = mask16[mask];
776 ((uint32_t *)s->vram_ptr)[addr] =
777 (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
778 (val & write_mask);
779 #ifdef DEBUG_VGA_MEM
780 printf("vga: latch: [0x%x] mask=0x%08x val=0x%08x\n",
781 addr * 4, write_mask, val);
782 #endif
783 cpu_physical_memory_set_dirty(s->vram_offset + (addr << 2));
787 static void vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
789 #ifdef TARGET_WORDS_BIGENDIAN
790 vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
791 vga_mem_writeb(opaque, addr + 1, val & 0xff);
792 #else
793 vga_mem_writeb(opaque, addr, val & 0xff);
794 vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
795 #endif
798 static void vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
800 #ifdef TARGET_WORDS_BIGENDIAN
801 vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
802 vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
803 vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
804 vga_mem_writeb(opaque, addr + 3, val & 0xff);
805 #else
806 vga_mem_writeb(opaque, addr, val & 0xff);
807 vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
808 vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
809 vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
810 #endif
813 typedef void vga_draw_glyph8_func(uint8_t *d, int linesize,
814 const uint8_t *font_ptr, int h,
815 uint32_t fgcol, uint32_t bgcol);
816 typedef void vga_draw_glyph9_func(uint8_t *d, int linesize,
817 const uint8_t *font_ptr, int h,
818 uint32_t fgcol, uint32_t bgcol, int dup9);
819 typedef void vga_draw_line_func(VGAState *s1, uint8_t *d,
820 const uint8_t *s, int width);
822 #define DEPTH 8
823 #include "vga_template.h"
825 #define DEPTH 15
826 #include "vga_template.h"
828 #define BGR_FORMAT
829 #define DEPTH 15
830 #include "vga_template.h"
832 #define DEPTH 16
833 #include "vga_template.h"
835 #define BGR_FORMAT
836 #define DEPTH 16
837 #include "vga_template.h"
839 #define DEPTH 32
840 #include "vga_template.h"
842 #define BGR_FORMAT
843 #define DEPTH 32
844 #include "vga_template.h"
846 static unsigned int rgb_to_pixel8_dup(unsigned int r, unsigned int g, unsigned b)
848 unsigned int col;
849 col = rgb_to_pixel8(r, g, b);
850 col |= col << 8;
851 col |= col << 16;
852 return col;
855 static unsigned int rgb_to_pixel15_dup(unsigned int r, unsigned int g, unsigned b)
857 unsigned int col;
858 col = rgb_to_pixel15(r, g, b);
859 col |= col << 16;
860 return col;
863 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r, unsigned int g,
864 unsigned int b)
866 unsigned int col;
867 col = rgb_to_pixel15bgr(r, g, b);
868 col |= col << 16;
869 return col;
872 static unsigned int rgb_to_pixel16_dup(unsigned int r, unsigned int g, unsigned b)
874 unsigned int col;
875 col = rgb_to_pixel16(r, g, b);
876 col |= col << 16;
877 return col;
880 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r, unsigned int g,
881 unsigned int b)
883 unsigned int col;
884 col = rgb_to_pixel16bgr(r, g, b);
885 col |= col << 16;
886 return col;
889 static unsigned int rgb_to_pixel32_dup(unsigned int r, unsigned int g, unsigned b)
891 unsigned int col;
892 col = rgb_to_pixel32(r, g, b);
893 return col;
896 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r, unsigned int g, unsigned b)
898 unsigned int col;
899 col = rgb_to_pixel32bgr(r, g, b);
900 return col;
903 /* return true if the palette was modified */
904 static int update_palette16(VGAState *s)
906 int full_update, i;
907 uint32_t v, col, *palette;
909 full_update = 0;
910 palette = s->last_palette;
911 for(i = 0; i < 16; i++) {
912 v = s->ar[i];
913 if (s->ar[0x10] & 0x80)
914 v = ((s->ar[0x14] & 0xf) << 4) | (v & 0xf);
915 else
916 v = ((s->ar[0x14] & 0xc) << 4) | (v & 0x3f);
917 v = v * 3;
918 col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
919 c6_to_8(s->palette[v + 1]),
920 c6_to_8(s->palette[v + 2]));
921 if (col != palette[i]) {
922 full_update = 1;
923 palette[i] = col;
926 return full_update;
929 /* return true if the palette was modified */
930 static int update_palette256(VGAState *s)
932 int full_update, i;
933 uint32_t v, col, *palette;
935 full_update = 0;
936 palette = s->last_palette;
937 v = 0;
938 for(i = 0; i < 256; i++) {
939 if (s->dac_8bit) {
940 col = s->rgb_to_pixel(s->palette[v],
941 s->palette[v + 1],
942 s->palette[v + 2]);
943 } else {
944 col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
945 c6_to_8(s->palette[v + 1]),
946 c6_to_8(s->palette[v + 2]));
948 if (col != palette[i]) {
949 full_update = 1;
950 palette[i] = col;
952 v += 3;
954 return full_update;
957 static void vga_get_offsets(VGAState *s,
958 uint32_t *pline_offset,
959 uint32_t *pstart_addr,
960 uint32_t *pline_compare)
962 uint32_t start_addr, line_offset, line_compare;
963 #ifdef CONFIG_BOCHS_VBE
964 if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
965 line_offset = s->vbe_line_offset;
966 start_addr = s->vbe_start_addr;
967 line_compare = 65535;
968 } else
969 #endif
971 /* compute line_offset in bytes */
972 line_offset = s->cr[0x13];
973 line_offset <<= 3;
975 /* starting address */
976 start_addr = s->cr[0x0d] | (s->cr[0x0c] << 8);
978 /* line compare */
979 line_compare = s->cr[0x18] |
980 ((s->cr[0x07] & 0x10) << 4) |
981 ((s->cr[0x09] & 0x40) << 3);
983 *pline_offset = line_offset;
984 *pstart_addr = start_addr;
985 *pline_compare = line_compare;
988 /* update start_addr and line_offset. Return TRUE if modified */
989 static int update_basic_params(VGAState *s)
991 int full_update;
992 uint32_t start_addr, line_offset, line_compare;
994 full_update = 0;
996 s->get_offsets(s, &line_offset, &start_addr, &line_compare);
998 if (line_offset != s->line_offset ||
999 start_addr != s->start_addr ||
1000 line_compare != s->line_compare) {
1001 s->line_offset = line_offset;
1002 s->start_addr = start_addr;
1003 s->line_compare = line_compare;
1004 full_update = 1;
1006 return full_update;
1009 #define NB_DEPTHS 7
1011 static inline int get_depth_index(DisplayState *s)
1013 switch(s->depth) {
1014 default:
1015 case 8:
1016 return 0;
1017 case 15:
1018 if (s->bgr)
1019 return 5;
1020 else
1021 return 1;
1022 case 16:
1023 if (s->bgr)
1024 return 6;
1025 else
1026 return 2;
1027 case 32:
1028 if (s->bgr)
1029 return 4;
1030 else
1031 return 3;
1035 static vga_draw_glyph8_func *vga_draw_glyph8_table[NB_DEPTHS] = {
1036 vga_draw_glyph8_8,
1037 vga_draw_glyph8_16,
1038 vga_draw_glyph8_16,
1039 vga_draw_glyph8_32,
1040 vga_draw_glyph8_32,
1041 vga_draw_glyph8_16,
1042 vga_draw_glyph8_16,
1045 static vga_draw_glyph8_func *vga_draw_glyph16_table[NB_DEPTHS] = {
1046 vga_draw_glyph16_8,
1047 vga_draw_glyph16_16,
1048 vga_draw_glyph16_16,
1049 vga_draw_glyph16_32,
1050 vga_draw_glyph16_32,
1051 vga_draw_glyph16_16,
1052 vga_draw_glyph16_16,
1055 static vga_draw_glyph9_func *vga_draw_glyph9_table[NB_DEPTHS] = {
1056 vga_draw_glyph9_8,
1057 vga_draw_glyph9_16,
1058 vga_draw_glyph9_16,
1059 vga_draw_glyph9_32,
1060 vga_draw_glyph9_32,
1061 vga_draw_glyph9_16,
1062 vga_draw_glyph9_16,
1065 static const uint8_t cursor_glyph[32 * 4] = {
1066 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1067 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1068 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1069 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1070 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1071 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1072 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1073 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1074 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1075 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1076 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1077 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1078 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1079 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1080 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1081 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1085 * Text mode update
1086 * Missing:
1087 * - double scan
1088 * - double width
1089 * - underline
1090 * - flashing
1092 static void vga_draw_text(VGAState *s, int full_update)
1094 int cx, cy, cheight, cw, ch, cattr, height, width, ch_attr;
1095 int cx_min, cx_max, linesize, x_incr;
1096 uint32_t offset, fgcol, bgcol, v, cursor_offset;
1097 uint8_t *d1, *d, *src, *s1, *dest, *cursor_ptr;
1098 const uint8_t *font_ptr, *font_base[2];
1099 int dup9, line_offset, depth_index;
1100 uint32_t *palette;
1101 uint32_t *ch_attr_ptr;
1102 vga_draw_glyph8_func *vga_draw_glyph8;
1103 vga_draw_glyph9_func *vga_draw_glyph9;
1105 full_update |= update_palette16(s);
1106 palette = s->last_palette;
1108 /* compute font data address (in plane 2) */
1109 v = s->sr[3];
1110 offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
1111 if (offset != s->font_offsets[0]) {
1112 s->font_offsets[0] = offset;
1113 full_update = 1;
1115 font_base[0] = s->vram_ptr + offset;
1117 offset = (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
1118 font_base[1] = s->vram_ptr + offset;
1119 if (offset != s->font_offsets[1]) {
1120 s->font_offsets[1] = offset;
1121 full_update = 1;
1123 if (s->plane_updated & (1 << 2)) {
1124 /* if the plane 2 was modified since the last display, it
1125 indicates the font may have been modified */
1126 s->plane_updated = 0;
1127 full_update = 1;
1129 full_update |= update_basic_params(s);
1131 line_offset = s->line_offset;
1132 s1 = s->vram_ptr + (s->start_addr * 4);
1134 /* total width & height */
1135 cheight = (s->cr[9] & 0x1f) + 1;
1136 cw = 8;
1137 if (!(s->sr[1] & 0x01))
1138 cw = 9;
1139 if (s->sr[1] & 0x08)
1140 cw = 16; /* NOTE: no 18 pixel wide */
1141 x_incr = cw * ((s->ds->depth + 7) >> 3);
1142 width = (s->cr[0x01] + 1);
1143 if (s->cr[0x06] == 100) {
1144 /* ugly hack for CGA 160x100x16 - explain me the logic */
1145 height = 100;
1146 } else {
1147 height = s->cr[0x12] |
1148 ((s->cr[0x07] & 0x02) << 7) |
1149 ((s->cr[0x07] & 0x40) << 3);
1150 height = (height + 1) / cheight;
1152 if ((height * width) > CH_ATTR_SIZE) {
1153 /* better than nothing: exit if transient size is too big */
1154 return;
1157 if (width != s->last_width || height != s->last_height ||
1158 cw != s->last_cw || cheight != s->last_ch) {
1159 s->last_scr_width = width * cw;
1160 s->last_scr_height = height * cheight;
1161 qemu_console_resize(s->console, s->last_scr_width, s->last_scr_height);
1162 s->last_width = width;
1163 s->last_height = height;
1164 s->last_ch = cheight;
1165 s->last_cw = cw;
1166 full_update = 1;
1168 cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
1169 if (cursor_offset != s->cursor_offset ||
1170 s->cr[0xa] != s->cursor_start ||
1171 s->cr[0xb] != s->cursor_end) {
1172 /* if the cursor position changed, we update the old and new
1173 chars */
1174 if (s->cursor_offset < CH_ATTR_SIZE)
1175 s->last_ch_attr[s->cursor_offset] = -1;
1176 if (cursor_offset < CH_ATTR_SIZE)
1177 s->last_ch_attr[cursor_offset] = -1;
1178 s->cursor_offset = cursor_offset;
1179 s->cursor_start = s->cr[0xa];
1180 s->cursor_end = s->cr[0xb];
1182 cursor_ptr = s->vram_ptr + (s->start_addr + cursor_offset) * 4;
1184 depth_index = get_depth_index(s->ds);
1185 if (cw == 16)
1186 vga_draw_glyph8 = vga_draw_glyph16_table[depth_index];
1187 else
1188 vga_draw_glyph8 = vga_draw_glyph8_table[depth_index];
1189 vga_draw_glyph9 = vga_draw_glyph9_table[depth_index];
1191 dest = s->ds->data;
1192 linesize = s->ds->linesize;
1193 ch_attr_ptr = s->last_ch_attr;
1194 for(cy = 0; cy < height; cy++) {
1195 d1 = dest;
1196 src = s1;
1197 cx_min = width;
1198 cx_max = -1;
1199 for(cx = 0; cx < width; cx++) {
1200 ch_attr = *(uint16_t *)src;
1201 if (full_update || ch_attr != *ch_attr_ptr) {
1202 if (cx < cx_min)
1203 cx_min = cx;
1204 if (cx > cx_max)
1205 cx_max = cx;
1206 *ch_attr_ptr = ch_attr;
1207 #ifdef WORDS_BIGENDIAN
1208 ch = ch_attr >> 8;
1209 cattr = ch_attr & 0xff;
1210 #else
1211 ch = ch_attr & 0xff;
1212 cattr = ch_attr >> 8;
1213 #endif
1214 font_ptr = font_base[(cattr >> 3) & 1];
1215 font_ptr += 32 * 4 * ch;
1216 bgcol = palette[cattr >> 4];
1217 fgcol = palette[cattr & 0x0f];
1218 if (cw != 9) {
1219 vga_draw_glyph8(d1, linesize,
1220 font_ptr, cheight, fgcol, bgcol);
1221 } else {
1222 dup9 = 0;
1223 if (ch >= 0xb0 && ch <= 0xdf && (s->ar[0x10] & 0x04))
1224 dup9 = 1;
1225 vga_draw_glyph9(d1, linesize,
1226 font_ptr, cheight, fgcol, bgcol, dup9);
1228 if (src == cursor_ptr &&
1229 !(s->cr[0x0a] & 0x20)) {
1230 int line_start, line_last, h;
1231 /* draw the cursor */
1232 line_start = s->cr[0x0a] & 0x1f;
1233 line_last = s->cr[0x0b] & 0x1f;
1234 /* XXX: check that */
1235 if (line_last > cheight - 1)
1236 line_last = cheight - 1;
1237 if (line_last >= line_start && line_start < cheight) {
1238 h = line_last - line_start + 1;
1239 d = d1 + linesize * line_start;
1240 if (cw != 9) {
1241 vga_draw_glyph8(d, linesize,
1242 cursor_glyph, h, fgcol, bgcol);
1243 } else {
1244 vga_draw_glyph9(d, linesize,
1245 cursor_glyph, h, fgcol, bgcol, 1);
1250 d1 += x_incr;
1251 src += 4;
1252 ch_attr_ptr++;
1254 if (cx_max != -1) {
1255 dpy_update(s->ds, cx_min * cw, cy * cheight,
1256 (cx_max - cx_min + 1) * cw, cheight);
1258 dest += linesize * cheight;
1259 s1 += line_offset;
1263 enum {
1264 VGA_DRAW_LINE2,
1265 VGA_DRAW_LINE2D2,
1266 VGA_DRAW_LINE4,
1267 VGA_DRAW_LINE4D2,
1268 VGA_DRAW_LINE8D2,
1269 VGA_DRAW_LINE8,
1270 VGA_DRAW_LINE15,
1271 VGA_DRAW_LINE16,
1272 VGA_DRAW_LINE24,
1273 VGA_DRAW_LINE32,
1274 VGA_DRAW_LINE_NB,
1277 static vga_draw_line_func *vga_draw_line_table[NB_DEPTHS * VGA_DRAW_LINE_NB] = {
1278 vga_draw_line2_8,
1279 vga_draw_line2_16,
1280 vga_draw_line2_16,
1281 vga_draw_line2_32,
1282 vga_draw_line2_32,
1283 vga_draw_line2_16,
1284 vga_draw_line2_16,
1286 vga_draw_line2d2_8,
1287 vga_draw_line2d2_16,
1288 vga_draw_line2d2_16,
1289 vga_draw_line2d2_32,
1290 vga_draw_line2d2_32,
1291 vga_draw_line2d2_16,
1292 vga_draw_line2d2_16,
1294 vga_draw_line4_8,
1295 vga_draw_line4_16,
1296 vga_draw_line4_16,
1297 vga_draw_line4_32,
1298 vga_draw_line4_32,
1299 vga_draw_line4_16,
1300 vga_draw_line4_16,
1302 vga_draw_line4d2_8,
1303 vga_draw_line4d2_16,
1304 vga_draw_line4d2_16,
1305 vga_draw_line4d2_32,
1306 vga_draw_line4d2_32,
1307 vga_draw_line4d2_16,
1308 vga_draw_line4d2_16,
1310 vga_draw_line8d2_8,
1311 vga_draw_line8d2_16,
1312 vga_draw_line8d2_16,
1313 vga_draw_line8d2_32,
1314 vga_draw_line8d2_32,
1315 vga_draw_line8d2_16,
1316 vga_draw_line8d2_16,
1318 vga_draw_line8_8,
1319 vga_draw_line8_16,
1320 vga_draw_line8_16,
1321 vga_draw_line8_32,
1322 vga_draw_line8_32,
1323 vga_draw_line8_16,
1324 vga_draw_line8_16,
1326 vga_draw_line15_8,
1327 vga_draw_line15_15,
1328 vga_draw_line15_16,
1329 vga_draw_line15_32,
1330 vga_draw_line15_32bgr,
1331 vga_draw_line15_15bgr,
1332 vga_draw_line15_16bgr,
1334 vga_draw_line16_8,
1335 vga_draw_line16_15,
1336 vga_draw_line16_16,
1337 vga_draw_line16_32,
1338 vga_draw_line16_32bgr,
1339 vga_draw_line16_15bgr,
1340 vga_draw_line16_16bgr,
1342 vga_draw_line24_8,
1343 vga_draw_line24_15,
1344 vga_draw_line24_16,
1345 vga_draw_line24_32,
1346 vga_draw_line24_32bgr,
1347 vga_draw_line24_15bgr,
1348 vga_draw_line24_16bgr,
1350 vga_draw_line32_8,
1351 vga_draw_line32_15,
1352 vga_draw_line32_16,
1353 vga_draw_line32_32,
1354 vga_draw_line32_32bgr,
1355 vga_draw_line32_15bgr,
1356 vga_draw_line32_16bgr,
1359 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r, unsigned int g, unsigned b);
1361 static rgb_to_pixel_dup_func *rgb_to_pixel_dup_table[NB_DEPTHS] = {
1362 rgb_to_pixel8_dup,
1363 rgb_to_pixel15_dup,
1364 rgb_to_pixel16_dup,
1365 rgb_to_pixel32_dup,
1366 rgb_to_pixel32bgr_dup,
1367 rgb_to_pixel15bgr_dup,
1368 rgb_to_pixel16bgr_dup,
1371 static int vga_get_bpp(VGAState *s)
1373 int ret;
1374 #ifdef CONFIG_BOCHS_VBE
1375 if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
1376 ret = s->vbe_regs[VBE_DISPI_INDEX_BPP];
1377 } else
1378 #endif
1380 ret = 0;
1382 return ret;
1385 static void vga_get_resolution(VGAState *s, int *pwidth, int *pheight)
1387 int width, height;
1389 #ifdef CONFIG_BOCHS_VBE
1390 if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
1391 width = s->vbe_regs[VBE_DISPI_INDEX_XRES];
1392 height = s->vbe_regs[VBE_DISPI_INDEX_YRES];
1393 } else
1394 #endif
1396 width = (s->cr[0x01] + 1) * 8;
1397 height = s->cr[0x12] |
1398 ((s->cr[0x07] & 0x02) << 7) |
1399 ((s->cr[0x07] & 0x40) << 3);
1400 height = (height + 1);
1402 *pwidth = width;
1403 *pheight = height;
1406 void vga_invalidate_scanlines(VGAState *s, int y1, int y2)
1408 int y;
1409 if (y1 >= VGA_MAX_HEIGHT)
1410 return;
1411 if (y2 >= VGA_MAX_HEIGHT)
1412 y2 = VGA_MAX_HEIGHT;
1413 for(y = y1; y < y2; y++) {
1414 s->invalidated_y_table[y >> 5] |= 1 << (y & 0x1f);
1418 static int bitmap_get_dirty(unsigned long *bitmap, unsigned nr)
1420 unsigned word = nr / ((sizeof bitmap[0]) * 8);
1421 unsigned bit = nr % ((sizeof bitmap[0]) * 8);
1423 //printf("%x -> %ld\n", nr, (bitmap[word] >> bit) & 1);
1424 return (bitmap[word] >> bit) & 1;
1429 * graphic modes
1431 static void vga_draw_graphic(VGAState *s, int full_update)
1433 int y1, y, update, linesize, y_start, double_scan, mask;
1434 int width, height, shift_control, line_offset, bwidth, bits;
1435 int disp_width, multi_scan, multi_run;
1436 uint8_t *d;
1437 uint32_t v, addr1, addr;
1438 long page0, page1, page_min, page_max;
1439 vga_draw_line_func *vga_draw_line;
1440 /* HACK ALERT */
1441 #define VGA_BITMAP_SIZE (VGA_RAM_SIZE / 4096 / 8 / sizeof(long))
1442 unsigned long bitmap[VGA_BITMAP_SIZE];
1443 #ifndef TARGET_IA64
1444 int r;
1445 if (kvm_enabled()) {
1446 r = qemu_kvm_get_dirty_pages(s->map_addr, &bitmap);
1447 if (r < 0)
1448 fprintf(stderr, "kvm: get_dirty_pages returned %d\n", r);
1450 #else
1451 memset(bitmap, 0xff, VGA_BITMAP_SIZE*sizeof(long));
1452 //FIXME:Always flush full screen before log dirty ready!!
1453 #endif
1455 full_update |= update_basic_params(s);
1457 s->get_resolution(s, &width, &height);
1458 disp_width = width;
1460 shift_control = (s->gr[0x05] >> 5) & 3;
1461 double_scan = (s->cr[0x09] >> 7);
1462 if (shift_control != 1) {
1463 multi_scan = (((s->cr[0x09] & 0x1f) + 1) << double_scan) - 1;
1464 } else {
1465 /* in CGA modes, multi_scan is ignored */
1466 /* XXX: is it correct ? */
1467 multi_scan = double_scan;
1469 multi_run = multi_scan;
1470 if (shift_control != s->shift_control ||
1471 double_scan != s->double_scan) {
1472 full_update = 1;
1473 s->shift_control = shift_control;
1474 s->double_scan = double_scan;
1477 if (shift_control == 0) {
1478 full_update |= update_palette16(s);
1479 if (s->sr[0x01] & 8) {
1480 v = VGA_DRAW_LINE4D2;
1481 disp_width <<= 1;
1482 } else {
1483 v = VGA_DRAW_LINE4;
1485 bits = 4;
1486 } else if (shift_control == 1) {
1487 full_update |= update_palette16(s);
1488 if (s->sr[0x01] & 8) {
1489 v = VGA_DRAW_LINE2D2;
1490 disp_width <<= 1;
1491 } else {
1492 v = VGA_DRAW_LINE2;
1494 bits = 4;
1495 } else {
1496 switch(s->get_bpp(s)) {
1497 default:
1498 case 0:
1499 full_update |= update_palette256(s);
1500 v = VGA_DRAW_LINE8D2;
1501 bits = 4;
1502 break;
1503 case 8:
1504 full_update |= update_palette256(s);
1505 v = VGA_DRAW_LINE8;
1506 bits = 8;
1507 break;
1508 case 15:
1509 v = VGA_DRAW_LINE15;
1510 bits = 16;
1511 break;
1512 case 16:
1513 v = VGA_DRAW_LINE16;
1514 bits = 16;
1515 break;
1516 case 24:
1517 v = VGA_DRAW_LINE24;
1518 bits = 24;
1519 break;
1520 case 32:
1521 v = VGA_DRAW_LINE32;
1522 bits = 32;
1523 break;
1526 vga_draw_line = vga_draw_line_table[v * NB_DEPTHS + get_depth_index(s->ds)];
1528 if (disp_width != s->last_width ||
1529 height != s->last_height) {
1530 qemu_console_resize(s->console, disp_width, height);
1531 s->last_scr_width = disp_width;
1532 s->last_scr_height = height;
1533 s->last_width = disp_width;
1534 s->last_height = height;
1535 full_update = 1;
1537 if (s->cursor_invalidate)
1538 s->cursor_invalidate(s);
1540 line_offset = s->line_offset;
1541 #if 0
1542 printf("w=%d h=%d v=%d line_offset=%d cr[0x09]=0x%02x cr[0x17]=0x%02x linecmp=%d sr[0x01]=0x%02x\n",
1543 width, height, v, line_offset, s->cr[9], s->cr[0x17], s->line_compare, s->sr[0x01]);
1544 #endif
1545 addr1 = (s->start_addr * 4);
1546 bwidth = (width * bits + 7) / 8;
1547 y_start = -1;
1548 page_min = 0x7fffffff;
1549 page_max = -1;
1550 d = s->ds->data;
1551 linesize = s->ds->linesize;
1552 y1 = 0;
1553 for(y = 0; y < height; y++) {
1554 addr = addr1;
1555 if (!(s->cr[0x17] & 1)) {
1556 int shift;
1557 /* CGA compatibility handling */
1558 shift = 14 + ((s->cr[0x17] >> 6) & 1);
1559 addr = (addr & ~(1 << shift)) | ((y1 & 1) << shift);
1561 if (!(s->cr[0x17] & 2)) {
1562 addr = (addr & ~0x8000) | ((y1 & 2) << 14);
1564 page0 = s->vram_offset + (addr & TARGET_PAGE_MASK);
1565 page1 = s->vram_offset + ((addr + bwidth - 1) & TARGET_PAGE_MASK);
1566 update = full_update |
1567 cpu_physical_memory_get_dirty(page0, VGA_DIRTY_FLAG) |
1568 cpu_physical_memory_get_dirty(page1, VGA_DIRTY_FLAG);
1569 if (kvm_enabled()) {
1570 update |= bitmap_get_dirty(bitmap, (page0 - s->vram_offset) >> TARGET_PAGE_BITS);
1571 update |= bitmap_get_dirty(bitmap, (page1 - s->vram_offset) >> TARGET_PAGE_BITS);
1574 if ((page1 - page0) > TARGET_PAGE_SIZE) {
1575 /* if wide line, can use another page */
1576 update |= cpu_physical_memory_get_dirty(page0 + TARGET_PAGE_SIZE,
1577 VGA_DIRTY_FLAG);
1578 if (kvm_enabled())
1579 update |= bitmap_get_dirty(bitmap, (page0 + TARGET_PAGE_SIZE - s->vram_offset) >> TARGET_PAGE_BITS);
1581 /* explicit invalidation for the hardware cursor */
1582 update |= (s->invalidated_y_table[y >> 5] >> (y & 0x1f)) & 1;
1583 if (update) {
1584 if (y_start < 0)
1585 y_start = y;
1586 if (page0 < page_min)
1587 page_min = page0;
1588 if (page1 > page_max)
1589 page_max = page1;
1590 vga_draw_line(s, d, s->vram_ptr + addr, width);
1591 if (s->cursor_draw_line)
1592 s->cursor_draw_line(s, d, y);
1593 } else {
1594 if (y_start >= 0) {
1595 /* flush to display */
1596 dpy_update(s->ds, 0, y_start,
1597 disp_width, y - y_start);
1598 y_start = -1;
1601 if (!multi_run) {
1602 mask = (s->cr[0x17] & 3) ^ 3;
1603 if ((y1 & mask) == mask)
1604 addr1 += line_offset;
1605 y1++;
1606 multi_run = multi_scan;
1607 } else {
1608 multi_run--;
1610 /* line compare acts on the displayed lines */
1611 if (y == s->line_compare)
1612 addr1 = 0;
1613 d += linesize;
1615 if (y_start >= 0) {
1616 /* flush to display */
1617 dpy_update(s->ds, 0, y_start,
1618 disp_width, y - y_start);
1620 /* reset modified pages */
1621 if (page_max != -1) {
1622 cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
1623 VGA_DIRTY_FLAG);
1625 memset(s->invalidated_y_table, 0, ((height + 31) >> 5) * 4);
1628 static void vga_draw_blank(VGAState *s, int full_update)
1630 int i, w, val;
1631 uint8_t *d;
1633 if (!full_update)
1634 return;
1635 if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
1636 return;
1637 if (s->ds->depth == 8)
1638 val = s->rgb_to_pixel(0, 0, 0);
1639 else
1640 val = 0;
1641 w = s->last_scr_width * ((s->ds->depth + 7) >> 3);
1642 d = s->ds->data;
1643 for(i = 0; i < s->last_scr_height; i++) {
1644 memset(d, val, w);
1645 d += s->ds->linesize;
1647 dpy_update(s->ds, 0, 0,
1648 s->last_scr_width, s->last_scr_height);
1651 #define GMODE_TEXT 0
1652 #define GMODE_GRAPH 1
1653 #define GMODE_BLANK 2
1655 static void vga_update_display(void *opaque)
1657 VGAState *s = (VGAState *)opaque;
1658 int full_update, graphic_mode;
1660 if (s->ds->depth == 0) {
1661 /* nothing to do */
1662 } else {
1663 s->rgb_to_pixel =
1664 rgb_to_pixel_dup_table[get_depth_index(s->ds)];
1666 full_update = 0;
1667 if (!(s->ar_index & 0x20)) {
1668 graphic_mode = GMODE_BLANK;
1669 } else {
1670 graphic_mode = s->gr[6] & 1;
1672 if (graphic_mode != s->graphic_mode) {
1673 s->graphic_mode = graphic_mode;
1674 full_update = 1;
1676 switch(graphic_mode) {
1677 case GMODE_TEXT:
1678 vga_draw_text(s, full_update);
1679 break;
1680 case GMODE_GRAPH:
1681 vga_draw_graphic(s, full_update);
1682 break;
1683 case GMODE_BLANK:
1684 default:
1685 vga_draw_blank(s, full_update);
1686 break;
1691 /* force a full display refresh */
1692 static void vga_invalidate_display(void *opaque)
1694 VGAState *s = (VGAState *)opaque;
1696 s->last_width = -1;
1697 s->last_height = -1;
1700 static void vga_reset(VGAState *s)
1702 memset(s, 0, sizeof(VGAState));
1703 s->graphic_mode = -1; /* force full update */
1706 #define TEXTMODE_X(x) ((x) % width)
1707 #define TEXTMODE_Y(x) ((x) / width)
1708 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1709 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1710 /* relay text rendering to the display driver
1711 * instead of doing a full vga_update_display() */
1712 static void vga_update_text(void *opaque, console_ch_t *chardata)
1714 VGAState *s = (VGAState *) opaque;
1715 int graphic_mode, i, cursor_offset, cursor_visible;
1716 int cw, cheight, width, height, size, c_min, c_max;
1717 uint32_t *src;
1718 console_ch_t *dst, val;
1719 char msg_buffer[80];
1720 int full_update = 0;
1722 if (!(s->ar_index & 0x20)) {
1723 graphic_mode = GMODE_BLANK;
1724 } else {
1725 graphic_mode = s->gr[6] & 1;
1727 if (graphic_mode != s->graphic_mode) {
1728 s->graphic_mode = graphic_mode;
1729 full_update = 1;
1731 if (s->last_width == -1) {
1732 s->last_width = 0;
1733 full_update = 1;
1736 switch (graphic_mode) {
1737 case GMODE_TEXT:
1738 /* TODO: update palette */
1739 full_update |= update_basic_params(s);
1741 /* total width & height */
1742 cheight = (s->cr[9] & 0x1f) + 1;
1743 cw = 8;
1744 if (!(s->sr[1] & 0x01))
1745 cw = 9;
1746 if (s->sr[1] & 0x08)
1747 cw = 16; /* NOTE: no 18 pixel wide */
1748 width = (s->cr[0x01] + 1);
1749 if (s->cr[0x06] == 100) {
1750 /* ugly hack for CGA 160x100x16 - explain me the logic */
1751 height = 100;
1752 } else {
1753 height = s->cr[0x12] |
1754 ((s->cr[0x07] & 0x02) << 7) |
1755 ((s->cr[0x07] & 0x40) << 3);
1756 height = (height + 1) / cheight;
1759 size = (height * width);
1760 if (size > CH_ATTR_SIZE) {
1761 if (!full_update)
1762 return;
1764 sprintf(msg_buffer, "%i x %i Text mode", width, height);
1765 break;
1768 if (width != s->last_width || height != s->last_height ||
1769 cw != s->last_cw || cheight != s->last_ch) {
1770 s->last_scr_width = width * cw;
1771 s->last_scr_height = height * cheight;
1772 qemu_console_resize(s->console, width, height);
1773 s->last_width = width;
1774 s->last_height = height;
1775 s->last_ch = cheight;
1776 s->last_cw = cw;
1777 full_update = 1;
1780 /* Update "hardware" cursor */
1781 cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
1782 if (cursor_offset != s->cursor_offset ||
1783 s->cr[0xa] != s->cursor_start ||
1784 s->cr[0xb] != s->cursor_end || full_update) {
1785 cursor_visible = !(s->cr[0xa] & 0x20);
1786 if (cursor_visible && cursor_offset < size && cursor_offset >= 0)
1787 dpy_cursor(s->ds,
1788 TEXTMODE_X(cursor_offset),
1789 TEXTMODE_Y(cursor_offset));
1790 else
1791 dpy_cursor(s->ds, -1, -1);
1792 s->cursor_offset = cursor_offset;
1793 s->cursor_start = s->cr[0xa];
1794 s->cursor_end = s->cr[0xb];
1797 src = (uint32_t *) s->vram_ptr + s->start_addr;
1798 dst = chardata;
1800 if (full_update) {
1801 for (i = 0; i < size; src ++, dst ++, i ++)
1802 console_write_ch(dst, VMEM2CHTYPE(*src));
1804 dpy_update(s->ds, 0, 0, width, height);
1805 } else {
1806 c_max = 0;
1808 for (i = 0; i < size; src ++, dst ++, i ++) {
1809 console_write_ch(&val, VMEM2CHTYPE(*src));
1810 if (*dst != val) {
1811 *dst = val;
1812 c_max = i;
1813 break;
1816 c_min = i;
1817 for (; i < size; src ++, dst ++, i ++) {
1818 console_write_ch(&val, VMEM2CHTYPE(*src));
1819 if (*dst != val) {
1820 *dst = val;
1821 c_max = i;
1825 if (c_min <= c_max) {
1826 i = TEXTMODE_Y(c_min);
1827 dpy_update(s->ds, 0, i, width, TEXTMODE_Y(c_max) - i + 1);
1831 return;
1832 case GMODE_GRAPH:
1833 if (!full_update)
1834 return;
1836 s->get_resolution(s, &width, &height);
1837 sprintf(msg_buffer, "%i x %i Graphic mode", width, height);
1838 break;
1839 case GMODE_BLANK:
1840 default:
1841 if (!full_update)
1842 return;
1844 sprintf(msg_buffer, "VGA Blank mode");
1845 break;
1848 /* Display a message */
1849 s->last_width = 60;
1850 s->last_height = height = 3;
1851 dpy_cursor(s->ds, -1, -1);
1852 qemu_console_resize(s->console, s->last_width, height);
1854 for (dst = chardata, i = 0; i < s->last_width * height; i ++)
1855 console_write_ch(dst ++, ' ');
1857 size = strlen(msg_buffer);
1858 width = (s->last_width - size) / 2;
1859 dst = chardata + s->last_width + width;
1860 for (i = 0; i < size; i ++)
1861 console_write_ch(dst ++, 0x00200100 | msg_buffer[i]);
1863 dpy_update(s->ds, 0, 0, s->last_width, height);
1866 static CPUReadMemoryFunc *vga_mem_read[3] = {
1867 vga_mem_readb,
1868 vga_mem_readw,
1869 vga_mem_readl,
1872 static CPUWriteMemoryFunc *vga_mem_write[3] = {
1873 vga_mem_writeb,
1874 vga_mem_writew,
1875 vga_mem_writel,
1878 static void vga_save(QEMUFile *f, void *opaque)
1880 VGAState *s = opaque;
1881 int i;
1883 if (s->pci_dev)
1884 pci_device_save(s->pci_dev, f);
1886 qemu_put_be32s(f, &s->latch);
1887 qemu_put_8s(f, &s->sr_index);
1888 qemu_put_buffer(f, s->sr, 8);
1889 qemu_put_8s(f, &s->gr_index);
1890 qemu_put_buffer(f, s->gr, 16);
1891 qemu_put_8s(f, &s->ar_index);
1892 qemu_put_buffer(f, s->ar, 21);
1893 qemu_put_be32(f, s->ar_flip_flop);
1894 qemu_put_8s(f, &s->cr_index);
1895 qemu_put_buffer(f, s->cr, 256);
1896 qemu_put_8s(f, &s->msr);
1897 qemu_put_8s(f, &s->fcr);
1898 qemu_put_byte(f, s->st00);
1899 qemu_put_8s(f, &s->st01);
1901 qemu_put_8s(f, &s->dac_state);
1902 qemu_put_8s(f, &s->dac_sub_index);
1903 qemu_put_8s(f, &s->dac_read_index);
1904 qemu_put_8s(f, &s->dac_write_index);
1905 qemu_put_buffer(f, s->dac_cache, 3);
1906 qemu_put_buffer(f, s->palette, 768);
1908 qemu_put_be32(f, s->bank_offset);
1909 #ifdef CONFIG_BOCHS_VBE
1910 qemu_put_byte(f, 1);
1911 qemu_put_be16s(f, &s->vbe_index);
1912 for(i = 0; i < VBE_DISPI_INDEX_NB; i++)
1913 qemu_put_be16s(f, &s->vbe_regs[i]);
1914 qemu_put_be32s(f, &s->vbe_start_addr);
1915 qemu_put_be32s(f, &s->vbe_line_offset);
1916 qemu_put_be32s(f, &s->vbe_bank_mask);
1917 #else
1918 qemu_put_byte(f, 0);
1919 #endif
1922 static int vga_load(QEMUFile *f, void *opaque, int version_id)
1924 VGAState *s = opaque;
1925 int is_vbe, i, ret;
1927 if (version_id > 2)
1928 return -EINVAL;
1930 if (s->pci_dev && version_id >= 2) {
1931 ret = pci_device_load(s->pci_dev, f);
1932 if (ret < 0)
1933 return ret;
1936 qemu_get_be32s(f, &s->latch);
1937 qemu_get_8s(f, &s->sr_index);
1938 qemu_get_buffer(f, s->sr, 8);
1939 qemu_get_8s(f, &s->gr_index);
1940 qemu_get_buffer(f, s->gr, 16);
1941 qemu_get_8s(f, &s->ar_index);
1942 qemu_get_buffer(f, s->ar, 21);
1943 s->ar_flip_flop=qemu_get_be32(f);
1944 qemu_get_8s(f, &s->cr_index);
1945 qemu_get_buffer(f, s->cr, 256);
1946 qemu_get_8s(f, &s->msr);
1947 qemu_get_8s(f, &s->fcr);
1948 qemu_get_8s(f, &s->st00);
1949 qemu_get_8s(f, &s->st01);
1951 qemu_get_8s(f, &s->dac_state);
1952 qemu_get_8s(f, &s->dac_sub_index);
1953 qemu_get_8s(f, &s->dac_read_index);
1954 qemu_get_8s(f, &s->dac_write_index);
1955 qemu_get_buffer(f, s->dac_cache, 3);
1956 qemu_get_buffer(f, s->palette, 768);
1958 s->bank_offset=qemu_get_be32(f);
1959 is_vbe = qemu_get_byte(f);
1960 #ifdef CONFIG_BOCHS_VBE
1961 if (!is_vbe)
1962 return -EINVAL;
1963 qemu_get_be16s(f, &s->vbe_index);
1964 for(i = 0; i < VBE_DISPI_INDEX_NB; i++)
1965 qemu_get_be16s(f, &s->vbe_regs[i]);
1966 qemu_get_be32s(f, &s->vbe_start_addr);
1967 qemu_get_be32s(f, &s->vbe_line_offset);
1968 qemu_get_be32s(f, &s->vbe_bank_mask);
1969 #else
1970 if (is_vbe)
1971 return -EINVAL;
1972 #endif
1974 /* force refresh */
1975 s->graphic_mode = -1;
1976 return 0;
1979 typedef struct PCIVGAState {
1980 PCIDevice dev;
1981 VGAState vga_state;
1982 } PCIVGAState;
1984 void vga_update_vram_mapping(VGAState *s, unsigned long vga_ram_begin,
1985 unsigned long vga_ram_end)
1987 void *vram_pointer, *old_vram;
1989 if (vga_ram_begin == s->map_addr &&
1990 vga_ram_end == s->map_end) {
1991 return;
1994 if (s->map_addr && s->map_end)
1995 unset_vram_mapping(s->map_addr, s->map_end);
1997 vram_pointer = set_vram_mapping(vga_ram_begin, vga_ram_end);
1998 if (!vram_pointer) {
1999 fprintf(stderr, "set_vram_mapping failed\n");
2000 s->map_addr = s->map_end = 0;
2002 else {
2003 old_vram = vga_update_vram((VGAState *)s, vram_pointer,
2004 VGA_RAM_SIZE);
2005 if (s->map_addr && s->map_end)
2006 munmap(old_vram, s->map_end - s->map_addr);
2007 else
2008 qemu_free(old_vram);
2009 s->map_addr = vga_ram_begin;
2010 s->map_end = vga_ram_end;
2014 static void vga_map(PCIDevice *pci_dev, int region_num,
2015 uint32_t addr, uint32_t size, int type)
2017 PCIVGAState *d = (PCIVGAState *)pci_dev;
2018 VGAState *s = &d->vga_state;
2019 if (region_num == PCI_ROM_SLOT) {
2020 cpu_register_physical_memory(addr, s->bios_size, s->bios_offset);
2021 } else {
2022 cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
2023 if (kvm_enabled())
2024 vga_update_vram_mapping(s, addr, addr + VGA_RAM_SIZE);
2028 #ifdef TARGET_IA64
2029 /* do the same job as vgabios before vgabios get ready - yeah */
2030 void vga_bios_init(VGAState *s)
2032 uint8_t palette_model[192] = {
2033 0, 0, 0, 0, 0, 170, 0, 170,
2034 0, 0, 170, 170, 170, 0, 0, 170,
2035 0, 170, 170, 85, 0, 170, 170, 170,
2036 85, 85, 85, 85, 85, 255, 85, 255,
2037 85, 85, 255, 255, 255, 85, 85, 255,
2038 85, 255, 255, 255, 85, 255, 255, 255,
2039 0, 21, 0, 0, 21, 42, 0, 63,
2040 0, 0, 63, 42, 42, 21, 0, 42,
2041 21, 42, 42, 63, 0, 42, 63, 42,
2042 0, 21, 21, 0, 21, 63, 0, 63,
2043 21, 0, 63, 63, 42, 21, 21, 42,
2044 21, 63, 42, 63, 21, 42, 63, 63,
2045 21, 0, 0, 21, 0, 42, 21, 42,
2046 0, 21, 42, 42, 63, 0, 0, 63,
2047 0, 42, 63, 42, 0, 63, 42, 42,
2048 21, 0, 21, 21, 0, 63, 21, 42,
2049 21, 21, 42, 63, 63, 0, 21, 63,
2050 0, 63, 63, 42, 21, 63, 42, 63,
2051 21, 21, 0, 21, 21, 42, 21, 63,
2052 0, 21, 63, 42, 63, 21, 0, 63,
2053 21, 42, 63, 63, 0, 63, 63, 42,
2054 21, 21, 21, 21, 21, 63, 21, 63,
2055 21, 21, 63, 63, 63, 21, 21, 63,
2056 21, 63, 63, 63, 21, 63, 63, 63
2059 s->latch = 0;
2061 s->sr_index = 3;
2062 s->sr[0] = 3;
2063 s->sr[1] = 0;
2064 s->sr[2] = 3;
2065 s->sr[3] = 0;
2066 s->sr[4] = 2;
2067 s->sr[5] = 0;
2068 s->sr[6] = 0;
2069 s->sr[7] = 0;
2071 s->gr_index = 5;
2072 s->gr[0] = 0;
2073 s->gr[1] = 0;
2074 s->gr[2] = 0;
2075 s->gr[3] = 0;
2076 s->gr[4] = 0;
2077 s->gr[5] = 16;
2078 s->gr[6] = 14;
2079 s->gr[7] = 15;
2080 s->gr[8] = 255;
2082 /* changed by out 0x03c0 */
2083 s->ar_index = 32;
2084 s->ar[0] = 0;
2085 s->ar[1] = 1;
2086 s->ar[2] = 2;
2087 s->ar[3] = 3;
2088 s->ar[4] = 4;
2089 s->ar[5] = 5;
2090 s->ar[6] = 6;
2091 s->ar[7] = 7;
2092 s->ar[8] = 8;
2093 s->ar[9] = 9;
2094 s->ar[10] = 10;
2095 s->ar[11] = 11;
2096 s->ar[12] = 12;
2097 s->ar[13] = 13;
2098 s->ar[14] = 14;
2099 s->ar[15] = 15;
2100 s->ar[16] = 12;
2101 s->ar[17] = 0;
2102 s->ar[18] = 15;
2103 s->ar[19] = 8;
2104 s->ar[20] = 0;
2106 s->ar_flip_flop = 1;
2108 s->cr_index = 15;
2109 s->cr[0] = 95;
2110 s->cr[1] = 79;
2111 s->cr[2] = 80;
2112 s->cr[3] = 130;
2113 s->cr[4] = 85;
2114 s->cr[5] = 129;
2115 s->cr[6] = 191;
2116 s->cr[7] = 31;
2117 s->cr[8] = 0;
2118 s->cr[9] = 79;
2119 s->cr[10] = 14;
2120 s->cr[11] = 15;
2121 s->cr[12] = 0;
2122 s->cr[13] = 0;
2123 s->cr[14] = 5;
2124 s->cr[15] = 160;
2125 s->cr[16] = 156;
2126 s->cr[17] = 142;
2127 s->cr[18] = 143;
2128 s->cr[19] = 40;
2129 s->cr[20] = 31;
2130 s->cr[21] = 150;
2131 s->cr[22] = 185;
2132 s->cr[23] = 163;
2133 s->cr[24] = 255;
2135 s->msr = 103;
2136 s->fcr = 0;
2137 s->st00 = 0;
2138 s->st01 = 0;
2140 /* dac_* & palette will be initialized by os through out 0x03c8 &
2141 * out 0c03c9(1:3) */
2142 s->dac_state = 0;
2143 s->dac_sub_index = 0;
2144 s->dac_read_index = 0;
2145 s->dac_write_index = 16;
2146 s->dac_cache[0] = 255;
2147 s->dac_cache[1] = 255;
2148 s->dac_cache[2] = 255;
2150 /* palette */
2151 memcpy(s->palette, palette_model, 192);
2153 s->bank_offset = 0;
2154 s->graphic_mode = -1;
2156 /* TODO: add vbe support if enabled */
2158 #endif
2160 /* when used on xen/kvm environment, the vga_ram_base is not used */
2161 void vga_common_init(VGAState *s, DisplayState *ds, uint8_t *vga_ram_base,
2162 unsigned long vga_ram_offset, int vga_ram_size)
2164 int i, j, v, b;
2166 for(i = 0;i < 256; i++) {
2167 v = 0;
2168 for(j = 0; j < 8; j++) {
2169 v |= ((i >> j) & 1) << (j * 4);
2171 expand4[i] = v;
2173 v = 0;
2174 for(j = 0; j < 4; j++) {
2175 v |= ((i >> (2 * j)) & 3) << (j * 4);
2177 expand2[i] = v;
2179 for(i = 0; i < 16; i++) {
2180 v = 0;
2181 for(j = 0; j < 4; j++) {
2182 b = ((i >> j) & 1);
2183 v |= b << (2 * j);
2184 v |= b << (2 * j + 1);
2186 expand4to8[i] = v;
2189 vga_reset(s);
2191 if (kvm_enabled())
2192 s->vram_ptr = qemu_malloc(vga_ram_size);
2193 else
2194 s->vram_ptr = vga_ram_base;
2195 s->vram_offset = vga_ram_offset;
2196 s->vram_size = vga_ram_size;
2197 s->ds = ds;
2198 s->get_bpp = vga_get_bpp;
2199 s->get_offsets = vga_get_offsets;
2200 s->get_resolution = vga_get_resolution;
2201 s->update = vga_update_display;
2202 s->invalidate = vga_invalidate_display;
2203 s->screen_dump = vga_screen_dump;
2204 s->text_update = vga_update_text;
2205 #ifdef TARGET_IA64
2206 vga_bios_init(s);
2207 #endif
2210 /* used by both ISA and PCI */
2211 void vga_init(VGAState *s)
2213 int vga_io_memory;
2215 register_savevm("vga", 0, 2, vga_save, vga_load, s);
2217 register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
2219 register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
2220 register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
2221 register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
2222 register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
2224 register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
2226 register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
2227 register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
2228 register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
2229 register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
2230 s->bank_offset = 0;
2232 #ifdef CONFIG_BOCHS_VBE
2233 s->vbe_regs[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID0;
2234 s->vbe_bank_mask = ((s->vram_size >> 16) - 1);
2235 #if defined (TARGET_I386)
2236 register_ioport_read(0x1ce, 1, 2, vbe_ioport_read_index, s);
2237 register_ioport_read(0x1cf, 1, 2, vbe_ioport_read_data, s);
2239 register_ioport_write(0x1ce, 1, 2, vbe_ioport_write_index, s);
2240 register_ioport_write(0x1cf, 1, 2, vbe_ioport_write_data, s);
2242 /* old Bochs IO ports */
2243 register_ioport_read(0xff80, 1, 2, vbe_ioport_read_index, s);
2244 register_ioport_read(0xff81, 1, 2, vbe_ioport_read_data, s);
2246 register_ioport_write(0xff80, 1, 2, vbe_ioport_write_index, s);
2247 register_ioport_write(0xff81, 1, 2, vbe_ioport_write_data, s);
2248 #else
2249 register_ioport_read(0x1ce, 1, 2, vbe_ioport_read_index, s);
2250 register_ioport_read(0x1d0, 1, 2, vbe_ioport_read_data, s);
2252 register_ioport_write(0x1ce, 1, 2, vbe_ioport_write_index, s);
2253 register_ioport_write(0x1d0, 1, 2, vbe_ioport_write_data, s);
2254 #endif
2255 #endif /* CONFIG_BOCHS_VBE */
2257 vga_io_memory = cpu_register_io_memory(0, vga_mem_read, vga_mem_write, s);
2258 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
2259 vga_io_memory);
2260 if (kvm_enabled())
2261 qemu_kvm_register_coalesced_mmio(isa_mem_base + 0x000a0000, 0x20000);
2264 /* Memory mapped interface */
2265 static uint32_t vga_mm_readb (void *opaque, target_phys_addr_t addr)
2267 VGAState *s = opaque;
2269 return vga_ioport_read(s, (addr - s->base_ctrl) >> s->it_shift) & 0xff;
2272 static void vga_mm_writeb (void *opaque,
2273 target_phys_addr_t addr, uint32_t value)
2275 VGAState *s = opaque;
2277 vga_ioport_write(s, (addr - s->base_ctrl) >> s->it_shift, value & 0xff);
2280 static uint32_t vga_mm_readw (void *opaque, target_phys_addr_t addr)
2282 VGAState *s = opaque;
2284 return vga_ioport_read(s, (addr - s->base_ctrl) >> s->it_shift) & 0xffff;
2287 static void vga_mm_writew (void *opaque,
2288 target_phys_addr_t addr, uint32_t value)
2290 VGAState *s = opaque;
2292 vga_ioport_write(s, (addr - s->base_ctrl) >> s->it_shift, value & 0xffff);
2295 static uint32_t vga_mm_readl (void *opaque, target_phys_addr_t addr)
2297 VGAState *s = opaque;
2299 return vga_ioport_read(s, (addr - s->base_ctrl) >> s->it_shift);
2302 static void vga_mm_writel (void *opaque,
2303 target_phys_addr_t addr, uint32_t value)
2305 VGAState *s = opaque;
2307 vga_ioport_write(s, (addr - s->base_ctrl) >> s->it_shift, value);
2310 static CPUReadMemoryFunc *vga_mm_read_ctrl[] = {
2311 &vga_mm_readb,
2312 &vga_mm_readw,
2313 &vga_mm_readl,
2316 static CPUWriteMemoryFunc *vga_mm_write_ctrl[] = {
2317 &vga_mm_writeb,
2318 &vga_mm_writew,
2319 &vga_mm_writel,
2322 static void vga_mm_init(VGAState *s, target_phys_addr_t vram_base,
2323 target_phys_addr_t ctrl_base, int it_shift)
2325 int s_ioport_ctrl, vga_io_memory;
2327 s->base_ctrl = ctrl_base;
2328 s->it_shift = it_shift;
2329 s_ioport_ctrl = cpu_register_io_memory(0, vga_mm_read_ctrl, vga_mm_write_ctrl, s);
2330 vga_io_memory = cpu_register_io_memory(0, vga_mem_read, vga_mem_write, s);
2332 register_savevm("vga", 0, 2, vga_save, vga_load, s);
2334 cpu_register_physical_memory(ctrl_base, 0x100000, s_ioport_ctrl);
2335 s->bank_offset = 0;
2336 cpu_register_physical_memory(vram_base + 0x000a0000, 0x20000, vga_io_memory);
2337 if (kvm_enabled())
2338 qemu_kvm_register_coalesced_mmio(vram_base + 0x000a0000, 0x20000);
2341 int isa_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2342 unsigned long vga_ram_offset, int vga_ram_size)
2344 VGAState *s;
2346 s = qemu_mallocz(sizeof(VGAState));
2347 if (!s)
2348 return -1;
2350 vga_common_init(s, ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2351 vga_init(s);
2353 s->console = graphic_console_init(s->ds, s->update, s->invalidate,
2354 s->screen_dump, s->text_update, s);
2356 #ifdef CONFIG_BOCHS_VBE
2357 /* XXX: use optimized standard vga accesses */
2358 cpu_register_physical_memory(VBE_DISPI_LFB_PHYSICAL_ADDRESS,
2359 vga_ram_size, vga_ram_offset);
2360 #endif
2361 return 0;
2364 int isa_vga_mm_init(DisplayState *ds, uint8_t *vga_ram_base,
2365 unsigned long vga_ram_offset, int vga_ram_size,
2366 target_phys_addr_t vram_base, target_phys_addr_t ctrl_base,
2367 int it_shift)
2369 VGAState *s;
2371 s = qemu_mallocz(sizeof(VGAState));
2372 if (!s)
2373 return -1;
2375 vga_common_init(s, ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2376 vga_mm_init(s, vram_base, ctrl_base, it_shift);
2378 s->console = graphic_console_init(s->ds, s->update, s->invalidate,
2379 s->screen_dump, s->text_update, s);
2381 #ifdef CONFIG_BOCHS_VBE
2382 /* XXX: use optimized standard vga accesses */
2383 cpu_register_physical_memory(VBE_DISPI_LFB_PHYSICAL_ADDRESS,
2384 vga_ram_size, vga_ram_offset);
2385 #endif
2386 return 0;
2389 int pci_vga_init(PCIBus *bus, DisplayState *ds, uint8_t *vga_ram_base,
2390 unsigned long vga_ram_offset, int vga_ram_size,
2391 unsigned long vga_bios_offset, int vga_bios_size)
2393 PCIVGAState *d;
2394 VGAState *s;
2395 uint8_t *pci_conf;
2397 d = (PCIVGAState *)pci_register_device(bus, "VGA",
2398 sizeof(PCIVGAState),
2399 -1, NULL, NULL);
2400 if (!d)
2401 return -1;
2402 s = &d->vga_state;
2404 vga_common_init(s, ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2405 vga_init(s);
2407 s->console = graphic_console_init(s->ds, s->update, s->invalidate,
2408 s->screen_dump, s->text_update, s);
2410 s->pci_dev = &d->dev;
2412 pci_conf = d->dev.config;
2413 pci_conf[0x00] = 0x34; // dummy VGA (same as Bochs ID)
2414 pci_conf[0x01] = 0x12;
2415 pci_conf[0x02] = 0x11;
2416 pci_conf[0x03] = 0x11;
2417 pci_conf[0x0a] = 0x00; // VGA controller
2418 pci_conf[0x0b] = 0x03;
2419 pci_conf[0x0e] = 0x00; // header_type
2421 /* XXX: vga_ram_size must be a power of two */
2422 pci_register_io_region(&d->dev, 0, vga_ram_size,
2423 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
2424 if (vga_bios_size != 0) {
2425 unsigned int bios_total_size;
2426 s->bios_offset = vga_bios_offset;
2427 s->bios_size = vga_bios_size;
2428 /* must be a power of two */
2429 bios_total_size = 1;
2430 while (bios_total_size < vga_bios_size)
2431 bios_total_size <<= 1;
2432 pci_register_io_region(&d->dev, PCI_ROM_SLOT, bios_total_size,
2433 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
2435 return 0;
2438 void *vga_update_vram(VGAState *s, void *vga_ram_base, int vga_ram_size)
2440 uint8_t *old_pointer;
2442 if (s->vram_size != vga_ram_size) {
2443 fprintf(stderr, "No support to change vga_ram_size\n");
2444 return NULL;
2447 if (!vga_ram_base) {
2448 vga_ram_base = qemu_malloc(vga_ram_size);
2449 if (!vga_ram_base) {
2450 fprintf(stderr, "reallocate error\n");
2451 return NULL;
2455 /* XXX lock needed? */
2456 memcpy(vga_ram_base, s->vram_ptr, vga_ram_size);
2457 old_pointer = s->vram_ptr;
2458 s->vram_ptr = vga_ram_base;
2460 return old_pointer;
2463 /********************************************************/
2464 /* vga screen dump */
2466 static int vga_save_w, vga_save_h;
2468 static void vga_save_dpy_update(DisplayState *s,
2469 int x, int y, int w, int h)
2473 static void vga_save_dpy_resize(DisplayState *s, int w, int h)
2475 s->linesize = w * 4;
2476 s->data = qemu_malloc(h * s->linesize);
2477 vga_save_w = w;
2478 vga_save_h = h;
2481 static void vga_save_dpy_refresh(DisplayState *s)
2485 int ppm_save(const char *filename, uint8_t *data,
2486 int w, int h, int linesize)
2488 FILE *f;
2489 uint8_t *d, *d1;
2490 unsigned int v;
2491 int y, x;
2493 f = fopen(filename, "wb");
2494 if (!f)
2495 return -1;
2496 fprintf(f, "P6\n%d %d\n%d\n",
2497 w, h, 255);
2498 d1 = data;
2499 for(y = 0; y < h; y++) {
2500 d = d1;
2501 for(x = 0; x < w; x++) {
2502 v = *(uint32_t *)d;
2503 fputc((v >> 16) & 0xff, f);
2504 fputc((v >> 8) & 0xff, f);
2505 fputc((v) & 0xff, f);
2506 d += 4;
2508 d1 += linesize;
2510 fclose(f);
2511 return 0;
2514 /* save the vga display in a PPM image even if no display is
2515 available */
2516 static void vga_screen_dump(void *opaque, const char *filename)
2518 VGAState *s = (VGAState *)opaque;
2519 DisplayState saved_ds, *ds = s->ds;
2521 /* XXX: this is a little hackish */
2522 vga_invalidate_display(s);
2523 saved_ds = *s->ds;
2525 memset(ds, 0, sizeof(DisplayState));
2526 ds->dpy_update = vga_save_dpy_update;
2527 ds->dpy_resize = vga_save_dpy_resize;
2528 ds->dpy_refresh = vga_save_dpy_refresh;
2529 ds->depth = 32;
2531 s->graphic_mode = -1;
2532 vga_update_display(s);
2534 if (ds->data) {
2535 ppm_save(filename, ds->data, vga_save_w, vga_save_h,
2536 s->ds->linesize);
2537 qemu_free(ds->data);
2539 *s->ds = saved_ds;