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
30 #include "pixel_ops.h"
31 #include "qemu-timer.h"
35 //#define DEBUG_VGA_MEM
36 //#define DEBUG_VGA_REG
38 //#define DEBUG_BOCHS_VBE
41 * Video Graphics Array (VGA)
43 * Chipset docs for original IBM VGA:
44 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
47 * http://www.osdever.net/FreeVGA/home.htm
49 * Standard VGA features and Bochs VBE extensions are implemented.
52 /* force some bits to zero */
53 const uint8_t sr_mask
[8] = {
64 const uint8_t gr_mask
[16] = {
83 #define cbswap_32(__x) \
85 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
86 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
87 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
88 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
90 #ifdef HOST_WORDS_BIGENDIAN
91 #define PAT(x) cbswap_32(x)
96 #ifdef HOST_WORDS_BIGENDIAN
102 #ifdef HOST_WORDS_BIGENDIAN
103 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
105 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
108 static const uint32_t mask16
[16] = {
129 #ifdef HOST_WORDS_BIGENDIAN
132 #define PAT(x) cbswap_32(x)
135 static const uint32_t dmask16
[16] = {
154 static const uint32_t dmask4
[4] = {
161 static uint32_t expand4
[256];
162 static uint16_t expand2
[256];
163 static uint8_t expand4to8
[16];
165 static void vga_screen_dump(void *opaque
, const char *filename
);
167 static void vga_update_memory_access(VGACommonState
*s
)
169 MemoryRegion
*region
, *old_region
= s
->chain4_alias
;
170 target_phys_addr_t base
, offset
, size
;
172 s
->chain4_alias
= NULL
;
174 if ((s
->sr
[VGA_SEQ_PLANE_WRITE
] & VGA_SR02_ALL_PLANES
) ==
175 VGA_SR02_ALL_PLANES
&& s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
177 switch ((s
->gr
[VGA_GFX_MISC
] >> 2) & 3) {
185 offset
= s
->bank_offset
;
197 base
+= isa_mem_base
;
198 region
= g_malloc(sizeof(*region
));
199 memory_region_init_alias(region
, "vga.chain4", &s
->vram
, offset
, size
);
200 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
202 s
->chain4_alias
= region
;
205 memory_region_del_subregion(s
->legacy_address_space
, old_region
);
206 memory_region_destroy(old_region
);
208 s
->plane_updated
= 0xf;
212 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
217 static void vga_precise_update_retrace_info(VGACommonState
*s
)
220 int hretr_start_char
;
221 int hretr_skew_chars
;
225 int vretr_start_line
;
234 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
235 int64_t chars_per_sec
;
236 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
238 htotal_chars
= s
->cr
[VGA_CRTC_H_TOTAL
] + 5;
239 hretr_start_char
= s
->cr
[VGA_CRTC_H_SYNC_START
];
240 hretr_skew_chars
= (s
->cr
[VGA_CRTC_H_SYNC_END
] >> 5) & 3;
241 hretr_end_char
= s
->cr
[VGA_CRTC_H_SYNC_END
] & 0x1f;
243 vtotal_lines
= (s
->cr
[VGA_CRTC_V_TOTAL
] |
244 (((s
->cr
[VGA_CRTC_OVERFLOW
] & 1) |
245 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 4) & 2)) << 8)) + 2;
246 vretr_start_line
= s
->cr
[VGA_CRTC_V_SYNC_START
] |
247 ((((s
->cr
[VGA_CRTC_OVERFLOW
] >> 2) & 1) |
248 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 6) & 2)) << 8);
249 vretr_end_line
= s
->cr
[VGA_CRTC_V_SYNC_END
] & 0xf;
251 clocking_mode
= (s
->sr
[VGA_SEQ_CLOCK_MODE
] >> 3) & 1;
252 clock_sel
= (s
->msr
>> 2) & 3;
253 dots
= (s
->msr
& 1) ? 8 : 9;
255 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
257 htotal_chars
<<= clocking_mode
;
259 r
->total_chars
= vtotal_lines
* htotal_chars
;
261 r
->ticks_per_char
= get_ticks_per_sec() / (r
->total_chars
* r
->freq
);
263 r
->ticks_per_char
= get_ticks_per_sec() / chars_per_sec
;
266 r
->vstart
= vretr_start_line
;
267 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
269 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
270 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
271 r
->htotal
= htotal_chars
;
274 div2
= (s
->cr
[VGA_CRTC_MODE
] >> 2) & 1;
275 sldiv2
= (s
->cr
[VGA_CRTC_MODE
] >> 3) & 1;
285 "div2 = %d sldiv2 = %d\n"
286 "clocking_mode = %d\n"
287 "clock_sel = %d %d\n"
289 "ticks/char = %" PRId64
"\n"
291 (double) get_ticks_per_sec() / (r
->ticks_per_char
* r
->total_chars
),
309 static uint8_t vga_precise_retrace(VGACommonState
*s
)
311 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
312 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
314 if (r
->total_chars
) {
315 int cur_line
, cur_line_char
, cur_char
;
318 cur_tick
= qemu_get_clock_ns(vm_clock
);
320 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
321 cur_line
= cur_char
/ r
->htotal
;
323 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
324 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
326 cur_line_char
= cur_char
% r
->htotal
;
327 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
328 val
|= ST01_DISP_ENABLE
;
334 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
338 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
340 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
343 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
345 if (s
->msr
& VGA_MIS_COLOR
) {
347 return (addr
>= 0x3b0 && addr
<= 0x3bf);
350 return (addr
>= 0x3d0 && addr
<= 0x3df);
354 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
356 VGACommonState
*s
= opaque
;
359 if (vga_ioport_invalid(s
, addr
)) {
364 if (s
->ar_flip_flop
== 0) {
371 index
= s
->ar_index
& 0x1f;
372 if (index
< VGA_ATT_C
) {
385 val
= s
->sr
[s
->sr_index
];
387 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
394 val
= s
->dac_write_index
;
397 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
398 if (++s
->dac_sub_index
== 3) {
399 s
->dac_sub_index
= 0;
413 val
= s
->gr
[s
->gr_index
];
415 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
424 val
= s
->cr
[s
->cr_index
];
426 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
431 /* just toggle to fool polling */
432 val
= s
->st01
= s
->retrace(s
);
440 #if defined(DEBUG_VGA)
441 printf("VGA: read addr=0x%04x data=0x%02x\n", addr
, val
);
446 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
448 VGACommonState
*s
= opaque
;
451 /* check port range access depending on color/monochrome mode */
452 if (vga_ioport_invalid(s
, addr
)) {
456 printf("VGA: write addr=0x%04x data=0x%02x\n", addr
, val
);
461 if (s
->ar_flip_flop
== 0) {
465 index
= s
->ar_index
& 0x1f;
467 case VGA_ATC_PALETTE0
... VGA_ATC_PALETTEF
:
468 s
->ar
[index
] = val
& 0x3f;
471 s
->ar
[index
] = val
& ~0x10;
473 case VGA_ATC_OVERSCAN
:
476 case VGA_ATC_PLANE_ENABLE
:
477 s
->ar
[index
] = val
& ~0xc0;
480 s
->ar
[index
] = val
& ~0xf0;
482 case VGA_ATC_COLOR_PAGE
:
483 s
->ar
[index
] = val
& ~0xf0;
489 s
->ar_flip_flop
^= 1;
492 s
->msr
= val
& ~0x10;
493 s
->update_retrace_info(s
);
496 s
->sr_index
= val
& 7;
500 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
502 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
503 if (s
->sr_index
== VGA_SEQ_CLOCK_MODE
) {
504 s
->update_retrace_info(s
);
506 vga_update_memory_access(s
);
509 s
->dac_read_index
= val
;
510 s
->dac_sub_index
= 0;
514 s
->dac_write_index
= val
;
515 s
->dac_sub_index
= 0;
519 s
->dac_cache
[s
->dac_sub_index
] = val
;
520 if (++s
->dac_sub_index
== 3) {
521 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
522 s
->dac_sub_index
= 0;
523 s
->dac_write_index
++;
527 s
->gr_index
= val
& 0x0f;
531 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
533 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
534 vga_update_memory_access(s
);
543 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
545 /* handle CR0-7 protection */
546 if ((s
->cr
[VGA_CRTC_V_SYNC_END
] & VGA_CR11_LOCK_CR0_CR7
) &&
547 s
->cr_index
<= VGA_CRTC_OVERFLOW
) {
548 /* can always write bit 4 of CR7 */
549 if (s
->cr_index
== VGA_CRTC_OVERFLOW
) {
550 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x10) |
555 s
->cr
[s
->cr_index
] = val
;
557 switch(s
->cr_index
) {
558 case VGA_CRTC_H_TOTAL
:
559 case VGA_CRTC_H_SYNC_START
:
560 case VGA_CRTC_H_SYNC_END
:
561 case VGA_CRTC_V_TOTAL
:
562 case VGA_CRTC_OVERFLOW
:
563 case VGA_CRTC_V_SYNC_END
:
565 s
->update_retrace_info(s
);
576 #ifdef CONFIG_BOCHS_VBE
577 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
579 VGACommonState
*s
= opaque
;
585 static uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
587 VGACommonState
*s
= opaque
;
590 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
591 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
592 switch(s
->vbe_index
) {
593 /* XXX: do not hardcode ? */
594 case VBE_DISPI_INDEX_XRES
:
595 val
= VBE_DISPI_MAX_XRES
;
597 case VBE_DISPI_INDEX_YRES
:
598 val
= VBE_DISPI_MAX_YRES
;
600 case VBE_DISPI_INDEX_BPP
:
601 val
= VBE_DISPI_MAX_BPP
;
604 val
= s
->vbe_regs
[s
->vbe_index
];
608 val
= s
->vbe_regs
[s
->vbe_index
];
610 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
611 val
= s
->vram_size
/ (64 * 1024);
615 #ifdef DEBUG_BOCHS_VBE
616 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
621 static void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
623 VGACommonState
*s
= opaque
;
627 static void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
629 VGACommonState
*s
= opaque
;
631 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
632 #ifdef DEBUG_BOCHS_VBE
633 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
635 switch(s
->vbe_index
) {
636 case VBE_DISPI_INDEX_ID
:
637 if (val
== VBE_DISPI_ID0
||
638 val
== VBE_DISPI_ID1
||
639 val
== VBE_DISPI_ID2
||
640 val
== VBE_DISPI_ID3
||
641 val
== VBE_DISPI_ID4
) {
642 s
->vbe_regs
[s
->vbe_index
] = val
;
645 case VBE_DISPI_INDEX_XRES
:
646 if ((val
<= VBE_DISPI_MAX_XRES
) && ((val
& 7) == 0)) {
647 s
->vbe_regs
[s
->vbe_index
] = val
;
650 case VBE_DISPI_INDEX_YRES
:
651 if (val
<= VBE_DISPI_MAX_YRES
) {
652 s
->vbe_regs
[s
->vbe_index
] = val
;
655 case VBE_DISPI_INDEX_BPP
:
658 if (val
== 4 || val
== 8 || val
== 15 ||
659 val
== 16 || val
== 24 || val
== 32) {
660 s
->vbe_regs
[s
->vbe_index
] = val
;
663 case VBE_DISPI_INDEX_BANK
:
664 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
665 val
&= (s
->vbe_bank_mask
>> 2);
667 val
&= s
->vbe_bank_mask
;
669 s
->vbe_regs
[s
->vbe_index
] = val
;
670 s
->bank_offset
= (val
<< 16);
671 vga_update_memory_access(s
);
673 case VBE_DISPI_INDEX_ENABLE
:
674 if ((val
& VBE_DISPI_ENABLED
) &&
675 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
676 int h
, shift_control
;
678 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] =
679 s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
680 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] =
681 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
682 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
683 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
685 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
686 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 1;
688 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] *
689 ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
690 s
->vbe_start_addr
= 0;
692 /* clear the screen (should be done in BIOS) */
693 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
694 memset(s
->vram_ptr
, 0,
695 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
698 /* we initialize the VGA graphic mode (should be done
700 /* graphic mode + memory map 1 */
701 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
702 VGA_GR06_GRAPHICS_MODE
;
703 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
704 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
706 s
->cr
[VGA_CRTC_H_DISP
] =
707 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
708 /* height (only meaningful if < 1024) */
709 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
710 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
711 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
712 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
713 /* line compare to 1023 */
714 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
715 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
716 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
718 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
720 s
->sr
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
723 /* set chain 4 mode */
724 s
->sr
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
725 /* activate all planes */
726 s
->sr
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
728 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
729 (shift_control
<< 5);
730 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
732 /* XXX: the bios should do that */
735 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
736 s
->vbe_regs
[s
->vbe_index
] = val
;
737 vga_update_memory_access(s
);
739 case VBE_DISPI_INDEX_VIRT_WIDTH
:
741 int w
, h
, line_offset
;
743 if (val
< s
->vbe_regs
[VBE_DISPI_INDEX_XRES
])
746 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
747 line_offset
= w
>> 1;
749 line_offset
= w
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
750 h
= s
->vram_size
/ line_offset
;
751 /* XXX: support weird bochs semantics ? */
752 if (h
< s
->vbe_regs
[VBE_DISPI_INDEX_YRES
])
754 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = w
;
755 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = h
;
756 s
->vbe_line_offset
= line_offset
;
759 case VBE_DISPI_INDEX_X_OFFSET
:
760 case VBE_DISPI_INDEX_Y_OFFSET
:
763 s
->vbe_regs
[s
->vbe_index
] = val
;
764 s
->vbe_start_addr
= s
->vbe_line_offset
* s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
];
765 x
= s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
];
766 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
767 s
->vbe_start_addr
+= x
>> 1;
769 s
->vbe_start_addr
+= x
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
770 s
->vbe_start_addr
>>= 2;
780 /* called for accesses between 0xa0000 and 0xc0000 */
781 uint32_t vga_mem_readb(VGACommonState
*s
, target_phys_addr_t addr
)
783 int memory_map_mode
, plane
;
786 /* convert to VGA memory offset */
787 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
789 switch(memory_map_mode
) {
795 addr
+= s
->bank_offset
;
810 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
811 /* chain 4 mode : simplest access */
812 ret
= s
->vram_ptr
[addr
];
813 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
814 /* odd/even mode (aka text mode mapping) */
815 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
816 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
818 /* standard VGA latched access */
819 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
821 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
823 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
824 ret
= GET_PLANE(s
->latch
, plane
);
827 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
828 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
837 /* called for accesses between 0xa0000 and 0xc0000 */
838 void vga_mem_writeb(VGACommonState
*s
, target_phys_addr_t addr
, uint32_t val
)
840 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
841 uint32_t write_mask
, bit_mask
, set_mask
;
844 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
846 /* convert to VGA memory offset */
847 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
849 switch(memory_map_mode
) {
855 addr
+= s
->bank_offset
;
870 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
871 /* chain 4 mode : simplest access */
874 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
875 s
->vram_ptr
[addr
] = val
;
877 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
879 s
->plane_updated
|= mask
; /* only used to detect font change */
880 memory_region_set_dirty(&s
->vram
, addr
, 1);
882 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
883 /* odd/even mode (aka text mode mapping) */
884 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
886 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
887 addr
= ((addr
& ~1) << 1) | plane
;
888 s
->vram_ptr
[addr
] = val
;
890 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
892 s
->plane_updated
|= mask
; /* only used to detect font change */
893 memory_region_set_dirty(&s
->vram
, addr
, 1);
896 /* standard VGA latched access */
897 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
902 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
903 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
907 /* apply set/reset mask */
908 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
909 val
= (val
& ~set_mask
) |
910 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
911 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
917 val
= mask16
[val
& 0x0f];
918 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
922 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
923 val
= (val
>> b
) | (val
<< (8 - b
));
925 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
926 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
930 /* apply logical operation */
931 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
932 switch(func_select
) {
952 bit_mask
|= bit_mask
<< 8;
953 bit_mask
|= bit_mask
<< 16;
954 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
957 /* mask data according to sr[2] */
958 mask
= s
->sr
[VGA_SEQ_PLANE_WRITE
];
959 s
->plane_updated
|= mask
; /* only used to detect font change */
960 write_mask
= mask16
[mask
];
961 ((uint32_t *)s
->vram_ptr
)[addr
] =
962 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
965 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
966 addr
* 4, write_mask
, val
);
968 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
972 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
973 const uint8_t *font_ptr
, int h
,
974 uint32_t fgcol
, uint32_t bgcol
);
975 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
976 const uint8_t *font_ptr
, int h
,
977 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
978 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
979 const uint8_t *s
, int width
);
982 #include "vga_template.h"
985 #include "vga_template.h"
989 #include "vga_template.h"
992 #include "vga_template.h"
996 #include "vga_template.h"
999 #include "vga_template.h"
1003 #include "vga_template.h"
1005 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
1008 col
= rgb_to_pixel8(r
, g
, b
);
1014 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
1017 col
= rgb_to_pixel15(r
, g
, b
);
1022 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
1026 col
= rgb_to_pixel15bgr(r
, g
, b
);
1031 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
1034 col
= rgb_to_pixel16(r
, g
, b
);
1039 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
1043 col
= rgb_to_pixel16bgr(r
, g
, b
);
1048 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
1051 col
= rgb_to_pixel32(r
, g
, b
);
1055 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
1058 col
= rgb_to_pixel32bgr(r
, g
, b
);
1062 /* return true if the palette was modified */
1063 static int update_palette16(VGACommonState
*s
)
1066 uint32_t v
, col
, *palette
;
1069 palette
= s
->last_palette
;
1070 for(i
= 0; i
< 16; i
++) {
1072 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1073 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1075 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1078 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1079 c6_to_8(s
->palette
[v
+ 1]),
1080 c6_to_8(s
->palette
[v
+ 2]));
1081 if (col
!= palette
[i
]) {
1089 /* return true if the palette was modified */
1090 static int update_palette256(VGACommonState
*s
)
1093 uint32_t v
, col
, *palette
;
1096 palette
= s
->last_palette
;
1098 for(i
= 0; i
< 256; i
++) {
1100 col
= s
->rgb_to_pixel(s
->palette
[v
],
1104 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1105 c6_to_8(s
->palette
[v
+ 1]),
1106 c6_to_8(s
->palette
[v
+ 2]));
1108 if (col
!= palette
[i
]) {
1117 static void vga_get_offsets(VGACommonState
*s
,
1118 uint32_t *pline_offset
,
1119 uint32_t *pstart_addr
,
1120 uint32_t *pline_compare
)
1122 uint32_t start_addr
, line_offset
, line_compare
;
1123 #ifdef CONFIG_BOCHS_VBE
1124 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1125 line_offset
= s
->vbe_line_offset
;
1126 start_addr
= s
->vbe_start_addr
;
1127 line_compare
= 65535;
1131 /* compute line_offset in bytes */
1132 line_offset
= s
->cr
[VGA_CRTC_OFFSET
];
1135 /* starting address */
1136 start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1137 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1140 line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1141 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1142 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1144 *pline_offset
= line_offset
;
1145 *pstart_addr
= start_addr
;
1146 *pline_compare
= line_compare
;
1149 /* update start_addr and line_offset. Return TRUE if modified */
1150 static int update_basic_params(VGACommonState
*s
)
1153 uint32_t start_addr
, line_offset
, line_compare
;
1157 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1159 if (line_offset
!= s
->line_offset
||
1160 start_addr
!= s
->start_addr
||
1161 line_compare
!= s
->line_compare
) {
1162 s
->line_offset
= line_offset
;
1163 s
->start_addr
= start_addr
;
1164 s
->line_compare
= line_compare
;
1172 static inline int get_depth_index(DisplayState
*s
)
1174 switch(ds_get_bits_per_pixel(s
)) {
1183 if (is_surface_bgr(s
->surface
))
1190 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1200 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1202 vga_draw_glyph16_16
,
1203 vga_draw_glyph16_16
,
1204 vga_draw_glyph16_32
,
1205 vga_draw_glyph16_32
,
1206 vga_draw_glyph16_16
,
1207 vga_draw_glyph16_16
,
1210 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1220 static const uint8_t cursor_glyph
[32 * 4] = {
1221 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1222 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1223 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1225 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1226 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1227 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1228 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1229 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1230 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1231 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1232 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1233 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1235 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1236 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1239 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1240 int *pcwidth
, int *pcheight
)
1242 int width
, cwidth
, height
, cheight
;
1244 /* total width & height */
1245 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1247 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
1250 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
1251 cwidth
= 16; /* NOTE: no 18 pixel wide */
1253 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1254 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1255 /* ugly hack for CGA 160x100x16 - explain me the logic */
1258 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1259 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1260 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1261 height
= (height
+ 1) / cheight
;
1267 *pcheight
= cheight
;
1270 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1272 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1277 rgb_to_pixel32bgr_dup
,
1278 rgb_to_pixel15bgr_dup
,
1279 rgb_to_pixel16bgr_dup
,
1290 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1292 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1293 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1294 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1295 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1296 const uint8_t *font_ptr
, *font_base
[2];
1297 int dup9
, line_offset
, depth_index
;
1299 uint32_t *ch_attr_ptr
;
1300 vga_draw_glyph8_func
*vga_draw_glyph8
;
1301 vga_draw_glyph9_func
*vga_draw_glyph9
;
1303 /* compute font data address (in plane 2) */
1304 v
= s
->sr
[VGA_SEQ_CHARACTER_MAP
];
1305 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1306 if (offset
!= s
->font_offsets
[0]) {
1307 s
->font_offsets
[0] = offset
;
1310 font_base
[0] = s
->vram_ptr
+ offset
;
1312 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1313 font_base
[1] = s
->vram_ptr
+ offset
;
1314 if (offset
!= s
->font_offsets
[1]) {
1315 s
->font_offsets
[1] = offset
;
1318 if (s
->plane_updated
& (1 << 2) || s
->chain4_alias
) {
1319 /* if the plane 2 was modified since the last display, it
1320 indicates the font may have been modified */
1321 s
->plane_updated
= 0;
1324 full_update
|= update_basic_params(s
);
1326 line_offset
= s
->line_offset
;
1328 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1329 if ((height
* width
) > CH_ATTR_SIZE
) {
1330 /* better than nothing: exit if transient size is too big */
1334 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1335 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1336 s
->last_scr_width
= width
* cw
;
1337 s
->last_scr_height
= height
* cheight
;
1338 qemu_console_resize(s
->ds
, s
->last_scr_width
, s
->last_scr_height
);
1340 s
->last_width
= width
;
1341 s
->last_height
= height
;
1342 s
->last_ch
= cheight
;
1347 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1348 full_update
|= update_palette16(s
);
1349 palette
= s
->last_palette
;
1350 x_incr
= cw
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1352 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1353 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1354 if (cursor_offset
!= s
->cursor_offset
||
1355 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1356 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1357 /* if the cursor position changed, we update the old and new
1359 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1360 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1361 if (cursor_offset
< CH_ATTR_SIZE
)
1362 s
->last_ch_attr
[cursor_offset
] = -1;
1363 s
->cursor_offset
= cursor_offset
;
1364 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1365 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1367 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1369 depth_index
= get_depth_index(s
->ds
);
1371 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1373 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1374 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1376 dest
= ds_get_data(s
->ds
);
1377 linesize
= ds_get_linesize(s
->ds
);
1378 ch_attr_ptr
= s
->last_ch_attr
;
1380 offset
= s
->start_addr
* 4;
1381 for(cy
= 0; cy
< height
; cy
++) {
1383 src
= s
->vram_ptr
+ offset
;
1386 for(cx
= 0; cx
< width
; cx
++) {
1387 ch_attr
= *(uint16_t *)src
;
1388 if (full_update
|| ch_attr
!= *ch_attr_ptr
) {
1393 *ch_attr_ptr
= ch_attr
;
1394 #ifdef HOST_WORDS_BIGENDIAN
1396 cattr
= ch_attr
& 0xff;
1398 ch
= ch_attr
& 0xff;
1399 cattr
= ch_attr
>> 8;
1401 font_ptr
= font_base
[(cattr
>> 3) & 1];
1402 font_ptr
+= 32 * 4 * ch
;
1403 bgcol
= palette
[cattr
>> 4];
1404 fgcol
= palette
[cattr
& 0x0f];
1406 vga_draw_glyph8(d1
, linesize
,
1407 font_ptr
, cheight
, fgcol
, bgcol
);
1410 if (ch
>= 0xb0 && ch
<= 0xdf &&
1411 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1414 vga_draw_glyph9(d1
, linesize
,
1415 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1417 if (src
== cursor_ptr
&&
1418 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20)) {
1419 int line_start
, line_last
, h
;
1420 /* draw the cursor */
1421 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1422 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1423 /* XXX: check that */
1424 if (line_last
> cheight
- 1)
1425 line_last
= cheight
- 1;
1426 if (line_last
>= line_start
&& line_start
< cheight
) {
1427 h
= line_last
- line_start
+ 1;
1428 d
= d1
+ linesize
* line_start
;
1430 vga_draw_glyph8(d
, linesize
,
1431 cursor_glyph
, h
, fgcol
, bgcol
);
1433 vga_draw_glyph9(d
, linesize
,
1434 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1444 dpy_update(s
->ds
, cx_min
* cw
, cy
* cheight
,
1445 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1447 dest
+= linesize
* cheight
;
1448 line1
= line
+ cheight
;
1449 offset
+= line_offset
;
1450 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1471 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1481 vga_draw_line2d2_16
,
1482 vga_draw_line2d2_16
,
1483 vga_draw_line2d2_32
,
1484 vga_draw_line2d2_32
,
1485 vga_draw_line2d2_16
,
1486 vga_draw_line2d2_16
,
1497 vga_draw_line4d2_16
,
1498 vga_draw_line4d2_16
,
1499 vga_draw_line4d2_32
,
1500 vga_draw_line4d2_32
,
1501 vga_draw_line4d2_16
,
1502 vga_draw_line4d2_16
,
1505 vga_draw_line8d2_16
,
1506 vga_draw_line8d2_16
,
1507 vga_draw_line8d2_32
,
1508 vga_draw_line8d2_32
,
1509 vga_draw_line8d2_16
,
1510 vga_draw_line8d2_16
,
1524 vga_draw_line15_32bgr
,
1525 vga_draw_line15_15bgr
,
1526 vga_draw_line15_16bgr
,
1532 vga_draw_line16_32bgr
,
1533 vga_draw_line16_15bgr
,
1534 vga_draw_line16_16bgr
,
1540 vga_draw_line24_32bgr
,
1541 vga_draw_line24_15bgr
,
1542 vga_draw_line24_16bgr
,
1548 vga_draw_line32_32bgr
,
1549 vga_draw_line32_15bgr
,
1550 vga_draw_line32_16bgr
,
1553 static int vga_get_bpp(VGACommonState
*s
)
1556 #ifdef CONFIG_BOCHS_VBE
1557 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1558 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1567 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1571 #ifdef CONFIG_BOCHS_VBE
1572 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1573 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1574 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1578 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1579 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1580 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1581 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1582 height
= (height
+ 1);
1588 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1591 if (y1
>= VGA_MAX_HEIGHT
)
1593 if (y2
>= VGA_MAX_HEIGHT
)
1594 y2
= VGA_MAX_HEIGHT
;
1595 for(y
= y1
; y
< y2
; y
++) {
1596 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1600 static void vga_sync_dirty_bitmap(VGACommonState
*s
)
1602 memory_region_sync_dirty_bitmap(&s
->vram
);
1605 void vga_dirty_log_start(VGACommonState
*s
)
1607 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1610 void vga_dirty_log_stop(VGACommonState
*s
)
1612 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1618 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1620 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1621 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1622 ram_addr_t page0
, page1
, page_min
, page_max
;
1623 int disp_width
, multi_scan
, multi_run
;
1625 uint32_t v
, addr1
, addr
;
1626 vga_draw_line_func
*vga_draw_line
;
1628 full_update
|= update_basic_params(s
);
1631 vga_sync_dirty_bitmap(s
);
1633 s
->get_resolution(s
, &width
, &height
);
1636 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1637 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1638 if (shift_control
!= 1) {
1639 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1642 /* in CGA modes, multi_scan is ignored */
1643 /* XXX: is it correct ? */
1644 multi_scan
= double_scan
;
1646 multi_run
= multi_scan
;
1647 if (shift_control
!= s
->shift_control
||
1648 double_scan
!= s
->double_scan
) {
1650 s
->shift_control
= shift_control
;
1651 s
->double_scan
= double_scan
;
1654 if (shift_control
== 0) {
1655 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1658 } else if (shift_control
== 1) {
1659 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1664 depth
= s
->get_bpp(s
);
1665 if (s
->line_offset
!= s
->last_line_offset
||
1666 disp_width
!= s
->last_width
||
1667 height
!= s
->last_height
||
1668 s
->last_depth
!= depth
) {
1669 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1670 if (depth
== 16 || depth
== 32) {
1674 qemu_free_displaysurface(s
->ds
);
1675 s
->ds
->surface
= qemu_create_displaysurface_from(disp_width
, height
, depth
,
1677 s
->vram_ptr
+ (s
->start_addr
* 4));
1678 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
1679 s
->ds
->surface
->pf
= qemu_different_endianness_pixelformat(depth
);
1683 qemu_console_resize(s
->ds
, disp_width
, height
);
1685 s
->last_scr_width
= disp_width
;
1686 s
->last_scr_height
= height
;
1687 s
->last_width
= disp_width
;
1688 s
->last_height
= height
;
1689 s
->last_line_offset
= s
->line_offset
;
1690 s
->last_depth
= depth
;
1692 } else if (is_buffer_shared(s
->ds
->surface
) &&
1693 (full_update
|| s
->ds
->surface
->data
!= s
->vram_ptr
+ (s
->start_addr
* 4))) {
1694 s
->ds
->surface
->data
= s
->vram_ptr
+ (s
->start_addr
* 4);
1699 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1701 if (shift_control
== 0) {
1702 full_update
|= update_palette16(s
);
1703 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1704 v
= VGA_DRAW_LINE4D2
;
1709 } else if (shift_control
== 1) {
1710 full_update
|= update_palette16(s
);
1711 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1712 v
= VGA_DRAW_LINE2D2
;
1718 switch(s
->get_bpp(s
)) {
1721 full_update
|= update_palette256(s
);
1722 v
= VGA_DRAW_LINE8D2
;
1726 full_update
|= update_palette256(s
);
1731 v
= VGA_DRAW_LINE15
;
1735 v
= VGA_DRAW_LINE16
;
1739 v
= VGA_DRAW_LINE24
;
1743 v
= VGA_DRAW_LINE32
;
1748 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+ get_depth_index(s
->ds
)];
1750 if (!is_buffer_shared(s
->ds
->surface
) && s
->cursor_invalidate
)
1751 s
->cursor_invalidate(s
);
1753 line_offset
= s
->line_offset
;
1755 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",
1756 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1757 s
->line_compare
, s
->sr
[VGA_SEQ_CLOCK_MODE
]);
1759 addr1
= (s
->start_addr
* 4);
1760 bwidth
= (width
* bits
+ 7) / 8;
1764 d
= ds_get_data(s
->ds
);
1765 linesize
= ds_get_linesize(s
->ds
);
1767 for(y
= 0; y
< height
; y
++) {
1769 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1771 /* CGA compatibility handling */
1772 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1773 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1775 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1776 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1778 update
= full_update
;
1780 page1
= addr
+ bwidth
- 1;
1781 update
|= memory_region_get_dirty(&s
->vram
, page0
, page1
- page0
,
1783 /* explicit invalidation for the hardware cursor */
1784 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1788 if (page0
< page_min
)
1790 if (page1
> page_max
)
1792 if (!(is_buffer_shared(s
->ds
->surface
))) {
1793 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1794 if (s
->cursor_draw_line
)
1795 s
->cursor_draw_line(s
, d
, y
);
1799 /* flush to display */
1800 dpy_update(s
->ds
, 0, y_start
,
1801 disp_width
, y
- y_start
);
1806 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1807 if ((y1
& mask
) == mask
)
1808 addr1
+= line_offset
;
1810 multi_run
= multi_scan
;
1814 /* line compare acts on the displayed lines */
1815 if (y
== s
->line_compare
)
1820 /* flush to display */
1821 dpy_update(s
->ds
, 0, y_start
,
1822 disp_width
, y
- y_start
);
1824 /* reset modified pages */
1825 if (page_max
>= page_min
) {
1826 memory_region_reset_dirty(&s
->vram
,
1828 page_max
- page_min
,
1831 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1834 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1841 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1845 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1846 if (ds_get_bits_per_pixel(s
->ds
) == 8)
1847 val
= s
->rgb_to_pixel(0, 0, 0);
1850 w
= s
->last_scr_width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1851 d
= ds_get_data(s
->ds
);
1852 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1854 d
+= ds_get_linesize(s
->ds
);
1856 dpy_update(s
->ds
, 0, 0,
1857 s
->last_scr_width
, s
->last_scr_height
);
1860 #define GMODE_TEXT 0
1861 #define GMODE_GRAPH 1
1862 #define GMODE_BLANK 2
1864 static void vga_update_display(void *opaque
)
1866 VGACommonState
*s
= opaque
;
1867 int full_update
, graphic_mode
;
1869 qemu_flush_coalesced_mmio_buffer();
1871 if (ds_get_bits_per_pixel(s
->ds
) == 0) {
1875 if (!(s
->ar_index
& 0x20)) {
1876 graphic_mode
= GMODE_BLANK
;
1878 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1880 if (graphic_mode
!= s
->graphic_mode
) {
1881 s
->graphic_mode
= graphic_mode
;
1884 switch(graphic_mode
) {
1886 vga_draw_text(s
, full_update
);
1889 vga_draw_graphic(s
, full_update
);
1893 vga_draw_blank(s
, full_update
);
1899 /* force a full display refresh */
1900 static void vga_invalidate_display(void *opaque
)
1902 VGACommonState
*s
= opaque
;
1905 s
->last_height
= -1;
1908 void vga_common_reset(VGACommonState
*s
)
1911 memset(s
->sr
, '\0', sizeof(s
->sr
));
1913 memset(s
->gr
, '\0', sizeof(s
->gr
));
1915 memset(s
->ar
, '\0', sizeof(s
->ar
));
1916 s
->ar_flip_flop
= 0;
1918 memset(s
->cr
, '\0', sizeof(s
->cr
));
1924 s
->dac_sub_index
= 0;
1925 s
->dac_read_index
= 0;
1926 s
->dac_write_index
= 0;
1927 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1929 memset(s
->palette
, '\0', sizeof(s
->palette
));
1931 #ifdef CONFIG_BOCHS_VBE
1933 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1934 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1935 s
->vbe_start_addr
= 0;
1936 s
->vbe_line_offset
= 0;
1937 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1939 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1940 s
->graphic_mode
= -1; /* force full update */
1941 s
->shift_control
= 0;
1944 s
->line_compare
= 0;
1946 s
->plane_updated
= 0;
1951 s
->last_scr_width
= 0;
1952 s
->last_scr_height
= 0;
1953 s
->cursor_start
= 0;
1955 s
->cursor_offset
= 0;
1956 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1957 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1958 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1959 switch (vga_retrace_method
) {
1960 case VGA_RETRACE_DUMB
:
1962 case VGA_RETRACE_PRECISE
:
1963 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1966 vga_update_memory_access(s
);
1969 static void vga_reset(void *opaque
)
1971 VGACommonState
*s
= opaque
;
1972 vga_common_reset(s
);
1975 #define TEXTMODE_X(x) ((x) % width)
1976 #define TEXTMODE_Y(x) ((x) / width)
1977 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1978 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1979 /* relay text rendering to the display driver
1980 * instead of doing a full vga_update_display() */
1981 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
1983 VGACommonState
*s
= opaque
;
1984 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
1985 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
1987 console_ch_t
*dst
, val
;
1988 char msg_buffer
[80];
1989 int full_update
= 0;
1991 qemu_flush_coalesced_mmio_buffer();
1993 if (!(s
->ar_index
& 0x20)) {
1994 graphic_mode
= GMODE_BLANK
;
1996 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1998 if (graphic_mode
!= s
->graphic_mode
) {
1999 s
->graphic_mode
= graphic_mode
;
2002 if (s
->last_width
== -1) {
2007 switch (graphic_mode
) {
2009 /* TODO: update palette */
2010 full_update
|= update_basic_params(s
);
2012 /* total width & height */
2013 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
2015 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
2018 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
2019 cw
= 16; /* NOTE: no 18 pixel wide */
2021 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
2022 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
2023 /* ugly hack for CGA 160x100x16 - explain me the logic */
2026 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
2027 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
2028 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
2029 height
= (height
+ 1) / cheight
;
2032 size
= (height
* width
);
2033 if (size
> CH_ATTR_SIZE
) {
2037 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
2042 if (width
!= s
->last_width
|| height
!= s
->last_height
||
2043 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
2044 s
->last_scr_width
= width
* cw
;
2045 s
->last_scr_height
= height
* cheight
;
2046 s
->ds
->surface
->width
= width
;
2047 s
->ds
->surface
->height
= height
;
2049 s
->last_width
= width
;
2050 s
->last_height
= height
;
2051 s
->last_ch
= cheight
;
2056 /* Update "hardware" cursor */
2057 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
2058 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
2059 if (cursor_offset
!= s
->cursor_offset
||
2060 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
2061 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
2062 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
2063 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
2065 TEXTMODE_X(cursor_offset
),
2066 TEXTMODE_Y(cursor_offset
));
2068 dpy_cursor(s
->ds
, -1, -1);
2069 s
->cursor_offset
= cursor_offset
;
2070 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
2071 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
2074 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2078 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2079 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2081 dpy_update(s
->ds
, 0, 0, width
, height
);
2085 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2086 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2094 for (; i
< size
; src
++, dst
++, i
++) {
2095 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2102 if (c_min
<= c_max
) {
2103 i
= TEXTMODE_Y(c_min
);
2104 dpy_update(s
->ds
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2113 s
->get_resolution(s
, &width
, &height
);
2114 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2122 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2126 /* Display a message */
2128 s
->last_height
= height
= 3;
2129 dpy_cursor(s
->ds
, -1, -1);
2130 s
->ds
->surface
->width
= s
->last_width
;
2131 s
->ds
->surface
->height
= height
;
2134 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2135 console_write_ch(dst
++, ' ');
2137 size
= strlen(msg_buffer
);
2138 width
= (s
->last_width
- size
) / 2;
2139 dst
= chardata
+ s
->last_width
+ width
;
2140 for (i
= 0; i
< size
; i
++)
2141 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2143 dpy_update(s
->ds
, 0, 0, s
->last_width
, height
);
2146 static uint64_t vga_mem_read(void *opaque
, target_phys_addr_t addr
,
2149 VGACommonState
*s
= opaque
;
2151 return vga_mem_readb(s
, addr
);
2154 static void vga_mem_write(void *opaque
, target_phys_addr_t addr
,
2155 uint64_t data
, unsigned size
)
2157 VGACommonState
*s
= opaque
;
2159 return vga_mem_writeb(s
, addr
, data
);
2162 const MemoryRegionOps vga_mem_ops
= {
2163 .read
= vga_mem_read
,
2164 .write
= vga_mem_write
,
2165 .endianness
= DEVICE_LITTLE_ENDIAN
,
2167 .min_access_size
= 1,
2168 .max_access_size
= 1,
2172 static int vga_common_post_load(void *opaque
, int version_id
)
2174 VGACommonState
*s
= opaque
;
2177 s
->graphic_mode
= -1;
2181 const VMStateDescription vmstate_vga_common
= {
2184 .minimum_version_id
= 2,
2185 .minimum_version_id_old
= 2,
2186 .post_load
= vga_common_post_load
,
2187 .fields
= (VMStateField
[]) {
2188 VMSTATE_UINT32(latch
, VGACommonState
),
2189 VMSTATE_UINT8(sr_index
, VGACommonState
),
2190 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2191 VMSTATE_UINT8(gr_index
, VGACommonState
),
2192 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2193 VMSTATE_UINT8(ar_index
, VGACommonState
),
2194 VMSTATE_BUFFER(ar
, VGACommonState
),
2195 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2196 VMSTATE_UINT8(cr_index
, VGACommonState
),
2197 VMSTATE_BUFFER(cr
, VGACommonState
),
2198 VMSTATE_UINT8(msr
, VGACommonState
),
2199 VMSTATE_UINT8(fcr
, VGACommonState
),
2200 VMSTATE_UINT8(st00
, VGACommonState
),
2201 VMSTATE_UINT8(st01
, VGACommonState
),
2203 VMSTATE_UINT8(dac_state
, VGACommonState
),
2204 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2205 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2206 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2207 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2208 VMSTATE_BUFFER(palette
, VGACommonState
),
2210 VMSTATE_INT32(bank_offset
, VGACommonState
),
2211 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2212 #ifdef CONFIG_BOCHS_VBE
2213 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2214 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2215 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2216 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2217 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2219 VMSTATE_END_OF_LIST()
2223 void vga_common_init(VGACommonState
*s
, int vga_ram_size
)
2227 for(i
= 0;i
< 256; i
++) {
2229 for(j
= 0; j
< 8; j
++) {
2230 v
|= ((i
>> j
) & 1) << (j
* 4);
2235 for(j
= 0; j
< 4; j
++) {
2236 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2240 for(i
= 0; i
< 16; i
++) {
2242 for(j
= 0; j
< 4; j
++) {
2245 v
|= b
<< (2 * j
+ 1);
2250 #ifdef CONFIG_BOCHS_VBE
2251 s
->is_vbe_vmstate
= 1;
2253 s
->is_vbe_vmstate
= 0;
2255 memory_region_init_ram(&s
->vram
, "vga.vram", vga_ram_size
);
2256 vmstate_register_ram_global(&s
->vram
);
2257 xen_register_framebuffer(&s
->vram
);
2258 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2259 s
->vram_size
= vga_ram_size
;
2260 s
->get_bpp
= vga_get_bpp
;
2261 s
->get_offsets
= vga_get_offsets
;
2262 s
->get_resolution
= vga_get_resolution
;
2263 s
->update
= vga_update_display
;
2264 s
->invalidate
= vga_invalidate_display
;
2265 s
->screen_dump
= vga_screen_dump
;
2266 s
->text_update
= vga_update_text
;
2267 switch (vga_retrace_method
) {
2268 case VGA_RETRACE_DUMB
:
2269 s
->retrace
= vga_dumb_retrace
;
2270 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2273 case VGA_RETRACE_PRECISE
:
2274 s
->retrace
= vga_precise_retrace
;
2275 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2278 vga_dirty_log_start(s
);
2281 static const MemoryRegionPortio vga_portio_list
[] = {
2282 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2283 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2284 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2285 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2286 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2287 PORTIO_END_OF_LIST(),
2290 #ifdef CONFIG_BOCHS_VBE
2291 static const MemoryRegionPortio vbe_portio_list
[] = {
2292 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2294 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2296 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2298 PORTIO_END_OF_LIST(),
2300 #endif /* CONFIG_BOCHS_VBE */
2302 /* Used by both ISA and PCI */
2303 MemoryRegion
*vga_init_io(VGACommonState
*s
,
2304 const MemoryRegionPortio
**vga_ports
,
2305 const MemoryRegionPortio
**vbe_ports
)
2307 MemoryRegion
*vga_mem
;
2309 *vga_ports
= vga_portio_list
;
2311 #ifdef CONFIG_BOCHS_VBE
2312 *vbe_ports
= vbe_portio_list
;
2315 vga_mem
= g_malloc(sizeof(*vga_mem
));
2316 memory_region_init_io(vga_mem
, &vga_mem_ops
, s
,
2317 "vga-lowmem", 0x20000);
2322 void vga_init(VGACommonState
*s
, MemoryRegion
*address_space
,
2323 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2325 MemoryRegion
*vga_io_memory
;
2326 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2327 PortioList
*vga_port_list
= g_new(PortioList
, 1);
2328 PortioList
*vbe_port_list
= g_new(PortioList
, 1);
2330 qemu_register_reset(vga_reset
, s
);
2334 s
->legacy_address_space
= address_space
;
2336 vga_io_memory
= vga_init_io(s
, &vga_ports
, &vbe_ports
);
2337 memory_region_add_subregion_overlap(address_space
,
2338 isa_mem_base
+ 0x000a0000,
2341 memory_region_set_coalescing(vga_io_memory
);
2342 if (init_vga_ports
) {
2343 portio_list_init(vga_port_list
, vga_ports
, s
, "vga");
2344 portio_list_add(vga_port_list
, address_space_io
, 0x3b0);
2347 portio_list_init(vbe_port_list
, vbe_ports
, s
, "vbe");
2348 portio_list_add(vbe_port_list
, address_space_io
, 0x1ce);
2352 void vga_init_vbe(VGACommonState
*s
, MemoryRegion
*system_memory
)
2354 #ifdef CONFIG_BOCHS_VBE
2355 /* XXX: use optimized standard vga accesses */
2356 memory_region_add_subregion(system_memory
,
2357 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,
2362 /********************************************************/
2363 /* vga screen dump */
2365 int ppm_save(const char *filename
, struct DisplaySurface
*ds
)
2373 char *linebuf
, *pbuf
;
2375 f
= fopen(filename
, "wb");
2378 fprintf(f
, "P6\n%d %d\n%d\n",
2379 ds
->width
, ds
->height
, 255);
2380 linebuf
= g_malloc(ds
->width
* 3);
2382 for(y
= 0; y
< ds
->height
; y
++) {
2385 for(x
= 0; x
< ds
->width
; x
++) {
2386 if (ds
->pf
.bits_per_pixel
== 32)
2389 v
= (uint32_t) (*(uint16_t *)d
);
2390 /* Limited to 8 or fewer bits per channel: */
2391 r
= ((v
>> ds
->pf
.rshift
) & ds
->pf
.rmax
) << (8 - ds
->pf
.rbits
);
2392 g
= ((v
>> ds
->pf
.gshift
) & ds
->pf
.gmax
) << (8 - ds
->pf
.gbits
);
2393 b
= ((v
>> ds
->pf
.bshift
) & ds
->pf
.bmax
) << (8 - ds
->pf
.bbits
);
2397 d
+= ds
->pf
.bytes_per_pixel
;
2400 ret
= fwrite(linebuf
, 1, pbuf
- linebuf
, f
);
2408 /* save the vga display in a PPM image even if no display is
2410 static void vga_screen_dump(void *opaque
, const char *filename
)
2412 VGACommonState
*s
= opaque
;
2414 vga_invalidate_display(s
);
2416 ppm_save(filename
, s
->ds
->surface
);