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"
36 //#define DEBUG_VGA_MEM
37 //#define DEBUG_VGA_REG
39 //#define DEBUG_BOCHS_VBE
41 /* 16 state changes per vertical frame @60 Hz */
42 #define VGA_TEXT_CURSOR_PERIOD_MS (1000 * 2 * 16 / 60)
45 * Video Graphics Array (VGA)
47 * Chipset docs for original IBM VGA:
48 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
51 * http://www.osdever.net/FreeVGA/home.htm
53 * Standard VGA features and Bochs VBE extensions are implemented.
56 /* force some bits to zero */
57 const uint8_t sr_mask
[8] = {
68 const uint8_t gr_mask
[16] = {
87 #define cbswap_32(__x) \
89 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
90 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
91 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
92 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
94 #ifdef HOST_WORDS_BIGENDIAN
95 #define PAT(x) cbswap_32(x)
100 #ifdef HOST_WORDS_BIGENDIAN
106 #ifdef HOST_WORDS_BIGENDIAN
107 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
109 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
112 static const uint32_t mask16
[16] = {
133 #ifdef HOST_WORDS_BIGENDIAN
136 #define PAT(x) cbswap_32(x)
139 static const uint32_t dmask16
[16] = {
158 static const uint32_t dmask4
[4] = {
165 static uint32_t expand4
[256];
166 static uint16_t expand2
[256];
167 static uint8_t expand4to8
[16];
169 static void vga_screen_dump(void *opaque
, const char *filename
, bool cswitch
,
172 static void vga_update_memory_access(VGACommonState
*s
)
174 MemoryRegion
*region
, *old_region
= s
->chain4_alias
;
175 target_phys_addr_t base
, offset
, size
;
177 s
->chain4_alias
= NULL
;
179 if ((s
->sr
[VGA_SEQ_PLANE_WRITE
] & VGA_SR02_ALL_PLANES
) ==
180 VGA_SR02_ALL_PLANES
&& s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
182 switch ((s
->gr
[VGA_GFX_MISC
] >> 2) & 3) {
190 offset
= s
->bank_offset
;
202 base
+= isa_mem_base
;
203 region
= g_malloc(sizeof(*region
));
204 memory_region_init_alias(region
, "vga.chain4", &s
->vram
, offset
, size
);
205 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
207 s
->chain4_alias
= region
;
210 memory_region_del_subregion(s
->legacy_address_space
, old_region
);
211 memory_region_destroy(old_region
);
213 s
->plane_updated
= 0xf;
217 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
222 static void vga_precise_update_retrace_info(VGACommonState
*s
)
225 int hretr_start_char
;
226 int hretr_skew_chars
;
230 int vretr_start_line
;
239 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
240 int64_t chars_per_sec
;
241 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
243 htotal_chars
= s
->cr
[VGA_CRTC_H_TOTAL
] + 5;
244 hretr_start_char
= s
->cr
[VGA_CRTC_H_SYNC_START
];
245 hretr_skew_chars
= (s
->cr
[VGA_CRTC_H_SYNC_END
] >> 5) & 3;
246 hretr_end_char
= s
->cr
[VGA_CRTC_H_SYNC_END
] & 0x1f;
248 vtotal_lines
= (s
->cr
[VGA_CRTC_V_TOTAL
] |
249 (((s
->cr
[VGA_CRTC_OVERFLOW
] & 1) |
250 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 4) & 2)) << 8)) + 2;
251 vretr_start_line
= s
->cr
[VGA_CRTC_V_SYNC_START
] |
252 ((((s
->cr
[VGA_CRTC_OVERFLOW
] >> 2) & 1) |
253 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 6) & 2)) << 8);
254 vretr_end_line
= s
->cr
[VGA_CRTC_V_SYNC_END
] & 0xf;
256 clocking_mode
= (s
->sr
[VGA_SEQ_CLOCK_MODE
] >> 3) & 1;
257 clock_sel
= (s
->msr
>> 2) & 3;
258 dots
= (s
->msr
& 1) ? 8 : 9;
260 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
262 htotal_chars
<<= clocking_mode
;
264 r
->total_chars
= vtotal_lines
* htotal_chars
;
266 r
->ticks_per_char
= get_ticks_per_sec() / (r
->total_chars
* r
->freq
);
268 r
->ticks_per_char
= get_ticks_per_sec() / chars_per_sec
;
271 r
->vstart
= vretr_start_line
;
272 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
274 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
275 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
276 r
->htotal
= htotal_chars
;
279 div2
= (s
->cr
[VGA_CRTC_MODE
] >> 2) & 1;
280 sldiv2
= (s
->cr
[VGA_CRTC_MODE
] >> 3) & 1;
290 "div2 = %d sldiv2 = %d\n"
291 "clocking_mode = %d\n"
292 "clock_sel = %d %d\n"
294 "ticks/char = %" PRId64
"\n"
296 (double) get_ticks_per_sec() / (r
->ticks_per_char
* r
->total_chars
),
314 static uint8_t vga_precise_retrace(VGACommonState
*s
)
316 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
317 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
319 if (r
->total_chars
) {
320 int cur_line
, cur_line_char
, cur_char
;
323 cur_tick
= qemu_get_clock_ns(vm_clock
);
325 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
326 cur_line
= cur_char
/ r
->htotal
;
328 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
329 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
331 cur_line_char
= cur_char
% r
->htotal
;
332 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
333 val
|= ST01_DISP_ENABLE
;
339 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
343 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
345 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
348 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
350 if (s
->msr
& VGA_MIS_COLOR
) {
352 return (addr
>= 0x3b0 && addr
<= 0x3bf);
355 return (addr
>= 0x3d0 && addr
<= 0x3df);
359 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
361 VGACommonState
*s
= opaque
;
364 qemu_flush_coalesced_mmio_buffer();
366 if (vga_ioport_invalid(s
, addr
)) {
371 if (s
->ar_flip_flop
== 0) {
378 index
= s
->ar_index
& 0x1f;
379 if (index
< VGA_ATT_C
) {
392 val
= s
->sr
[s
->sr_index
];
394 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
401 val
= s
->dac_write_index
;
404 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
405 if (++s
->dac_sub_index
== 3) {
406 s
->dac_sub_index
= 0;
420 val
= s
->gr
[s
->gr_index
];
422 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
431 val
= s
->cr
[s
->cr_index
];
433 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
438 /* just toggle to fool polling */
439 val
= s
->st01
= s
->retrace(s
);
447 #if defined(DEBUG_VGA)
448 printf("VGA: read addr=0x%04x data=0x%02x\n", addr
, val
);
453 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
455 VGACommonState
*s
= opaque
;
458 qemu_flush_coalesced_mmio_buffer();
460 /* check port range access depending on color/monochrome mode */
461 if (vga_ioport_invalid(s
, addr
)) {
465 printf("VGA: write addr=0x%04x data=0x%02x\n", addr
, val
);
470 if (s
->ar_flip_flop
== 0) {
474 index
= s
->ar_index
& 0x1f;
476 case VGA_ATC_PALETTE0
... VGA_ATC_PALETTEF
:
477 s
->ar
[index
] = val
& 0x3f;
480 s
->ar
[index
] = val
& ~0x10;
482 case VGA_ATC_OVERSCAN
:
485 case VGA_ATC_PLANE_ENABLE
:
486 s
->ar
[index
] = val
& ~0xc0;
489 s
->ar
[index
] = val
& ~0xf0;
491 case VGA_ATC_COLOR_PAGE
:
492 s
->ar
[index
] = val
& ~0xf0;
498 s
->ar_flip_flop
^= 1;
501 s
->msr
= val
& ~0x10;
502 s
->update_retrace_info(s
);
505 s
->sr_index
= val
& 7;
509 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
511 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
512 if (s
->sr_index
== VGA_SEQ_CLOCK_MODE
) {
513 s
->update_retrace_info(s
);
515 vga_update_memory_access(s
);
518 s
->dac_read_index
= val
;
519 s
->dac_sub_index
= 0;
523 s
->dac_write_index
= val
;
524 s
->dac_sub_index
= 0;
528 s
->dac_cache
[s
->dac_sub_index
] = val
;
529 if (++s
->dac_sub_index
== 3) {
530 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
531 s
->dac_sub_index
= 0;
532 s
->dac_write_index
++;
536 s
->gr_index
= val
& 0x0f;
540 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
542 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
543 vga_update_memory_access(s
);
552 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
554 /* handle CR0-7 protection */
555 if ((s
->cr
[VGA_CRTC_V_SYNC_END
] & VGA_CR11_LOCK_CR0_CR7
) &&
556 s
->cr_index
<= VGA_CRTC_OVERFLOW
) {
557 /* can always write bit 4 of CR7 */
558 if (s
->cr_index
== VGA_CRTC_OVERFLOW
) {
559 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x10) |
564 s
->cr
[s
->cr_index
] = val
;
566 switch(s
->cr_index
) {
567 case VGA_CRTC_H_TOTAL
:
568 case VGA_CRTC_H_SYNC_START
:
569 case VGA_CRTC_H_SYNC_END
:
570 case VGA_CRTC_V_TOTAL
:
571 case VGA_CRTC_OVERFLOW
:
572 case VGA_CRTC_V_SYNC_END
:
574 s
->update_retrace_info(s
);
585 #ifdef CONFIG_BOCHS_VBE
586 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
588 VGACommonState
*s
= opaque
;
594 static uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
596 VGACommonState
*s
= opaque
;
599 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
600 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
601 switch(s
->vbe_index
) {
602 /* XXX: do not hardcode ? */
603 case VBE_DISPI_INDEX_XRES
:
604 val
= VBE_DISPI_MAX_XRES
;
606 case VBE_DISPI_INDEX_YRES
:
607 val
= VBE_DISPI_MAX_YRES
;
609 case VBE_DISPI_INDEX_BPP
:
610 val
= VBE_DISPI_MAX_BPP
;
613 val
= s
->vbe_regs
[s
->vbe_index
];
617 val
= s
->vbe_regs
[s
->vbe_index
];
619 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
620 val
= s
->vram_size
/ (64 * 1024);
624 #ifdef DEBUG_BOCHS_VBE
625 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
630 static void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
632 VGACommonState
*s
= opaque
;
636 static void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
638 VGACommonState
*s
= opaque
;
640 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
641 #ifdef DEBUG_BOCHS_VBE
642 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
644 switch(s
->vbe_index
) {
645 case VBE_DISPI_INDEX_ID
:
646 if (val
== VBE_DISPI_ID0
||
647 val
== VBE_DISPI_ID1
||
648 val
== VBE_DISPI_ID2
||
649 val
== VBE_DISPI_ID3
||
650 val
== VBE_DISPI_ID4
) {
651 s
->vbe_regs
[s
->vbe_index
] = val
;
654 case VBE_DISPI_INDEX_XRES
:
655 if ((val
<= VBE_DISPI_MAX_XRES
) && ((val
& 7) == 0)) {
656 s
->vbe_regs
[s
->vbe_index
] = val
;
659 case VBE_DISPI_INDEX_YRES
:
660 if (val
<= VBE_DISPI_MAX_YRES
) {
661 s
->vbe_regs
[s
->vbe_index
] = val
;
664 case VBE_DISPI_INDEX_BPP
:
667 if (val
== 4 || val
== 8 || val
== 15 ||
668 val
== 16 || val
== 24 || val
== 32) {
669 s
->vbe_regs
[s
->vbe_index
] = val
;
672 case VBE_DISPI_INDEX_BANK
:
673 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
674 val
&= (s
->vbe_bank_mask
>> 2);
676 val
&= s
->vbe_bank_mask
;
678 s
->vbe_regs
[s
->vbe_index
] = val
;
679 s
->bank_offset
= (val
<< 16);
680 vga_update_memory_access(s
);
682 case VBE_DISPI_INDEX_ENABLE
:
683 if ((val
& VBE_DISPI_ENABLED
) &&
684 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
685 int h
, shift_control
;
687 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] =
688 s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
689 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] =
690 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
691 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
692 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
694 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
695 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 1;
697 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] *
698 ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
699 s
->vbe_start_addr
= 0;
701 /* clear the screen (should be done in BIOS) */
702 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
703 memset(s
->vram_ptr
, 0,
704 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
707 /* we initialize the VGA graphic mode (should be done
709 /* graphic mode + memory map 1 */
710 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
711 VGA_GR06_GRAPHICS_MODE
;
712 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
713 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
715 s
->cr
[VGA_CRTC_H_DISP
] =
716 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
717 /* height (only meaningful if < 1024) */
718 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
719 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
720 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
721 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
722 /* line compare to 1023 */
723 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
724 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
725 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
727 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
729 s
->sr
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
732 /* set chain 4 mode */
733 s
->sr
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
734 /* activate all planes */
735 s
->sr
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
737 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
738 (shift_control
<< 5);
739 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
741 /* XXX: the bios should do that */
744 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
745 s
->vbe_regs
[s
->vbe_index
] = val
;
746 vga_update_memory_access(s
);
748 case VBE_DISPI_INDEX_VIRT_WIDTH
:
750 int w
, h
, line_offset
;
752 if (val
< s
->vbe_regs
[VBE_DISPI_INDEX_XRES
])
755 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
756 line_offset
= w
>> 1;
758 line_offset
= w
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
759 h
= s
->vram_size
/ line_offset
;
760 /* XXX: support weird bochs semantics ? */
761 if (h
< s
->vbe_regs
[VBE_DISPI_INDEX_YRES
])
763 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = w
;
764 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = h
;
765 s
->vbe_line_offset
= line_offset
;
768 case VBE_DISPI_INDEX_X_OFFSET
:
769 case VBE_DISPI_INDEX_Y_OFFSET
:
772 s
->vbe_regs
[s
->vbe_index
] = val
;
773 s
->vbe_start_addr
= s
->vbe_line_offset
* s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
];
774 x
= s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
];
775 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
776 s
->vbe_start_addr
+= x
>> 1;
778 s
->vbe_start_addr
+= x
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
779 s
->vbe_start_addr
>>= 2;
789 /* called for accesses between 0xa0000 and 0xc0000 */
790 uint32_t vga_mem_readb(VGACommonState
*s
, target_phys_addr_t addr
)
792 int memory_map_mode
, plane
;
795 /* convert to VGA memory offset */
796 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
798 switch(memory_map_mode
) {
804 addr
+= s
->bank_offset
;
819 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
820 /* chain 4 mode : simplest access */
821 ret
= s
->vram_ptr
[addr
];
822 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
823 /* odd/even mode (aka text mode mapping) */
824 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
825 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
827 /* standard VGA latched access */
828 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
830 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
832 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
833 ret
= GET_PLANE(s
->latch
, plane
);
836 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
837 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
846 /* called for accesses between 0xa0000 and 0xc0000 */
847 void vga_mem_writeb(VGACommonState
*s
, target_phys_addr_t addr
, uint32_t val
)
849 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
850 uint32_t write_mask
, bit_mask
, set_mask
;
853 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
855 /* convert to VGA memory offset */
856 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
858 switch(memory_map_mode
) {
864 addr
+= s
->bank_offset
;
879 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
880 /* chain 4 mode : simplest access */
883 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
884 s
->vram_ptr
[addr
] = val
;
886 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
888 s
->plane_updated
|= mask
; /* only used to detect font change */
889 memory_region_set_dirty(&s
->vram
, addr
, 1);
891 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
892 /* odd/even mode (aka text mode mapping) */
893 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
895 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
896 addr
= ((addr
& ~1) << 1) | plane
;
897 s
->vram_ptr
[addr
] = val
;
899 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
901 s
->plane_updated
|= mask
; /* only used to detect font change */
902 memory_region_set_dirty(&s
->vram
, addr
, 1);
905 /* standard VGA latched access */
906 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
911 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
912 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
916 /* apply set/reset mask */
917 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
918 val
= (val
& ~set_mask
) |
919 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
920 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
926 val
= mask16
[val
& 0x0f];
927 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
931 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
932 val
= (val
>> b
) | (val
<< (8 - b
));
934 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
935 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
939 /* apply logical operation */
940 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
941 switch(func_select
) {
961 bit_mask
|= bit_mask
<< 8;
962 bit_mask
|= bit_mask
<< 16;
963 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
966 /* mask data according to sr[2] */
967 mask
= s
->sr
[VGA_SEQ_PLANE_WRITE
];
968 s
->plane_updated
|= mask
; /* only used to detect font change */
969 write_mask
= mask16
[mask
];
970 ((uint32_t *)s
->vram_ptr
)[addr
] =
971 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
974 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
975 addr
* 4, write_mask
, val
);
977 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
981 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
982 const uint8_t *font_ptr
, int h
,
983 uint32_t fgcol
, uint32_t bgcol
);
984 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
985 const uint8_t *font_ptr
, int h
,
986 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
987 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
988 const uint8_t *s
, int width
);
991 #include "vga_template.h"
994 #include "vga_template.h"
998 #include "vga_template.h"
1001 #include "vga_template.h"
1005 #include "vga_template.h"
1008 #include "vga_template.h"
1012 #include "vga_template.h"
1014 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
1017 col
= rgb_to_pixel8(r
, g
, b
);
1023 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
1026 col
= rgb_to_pixel15(r
, g
, b
);
1031 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
1035 col
= rgb_to_pixel15bgr(r
, g
, b
);
1040 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
1043 col
= rgb_to_pixel16(r
, g
, b
);
1048 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
1052 col
= rgb_to_pixel16bgr(r
, g
, b
);
1057 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
1060 col
= rgb_to_pixel32(r
, g
, b
);
1064 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
1067 col
= rgb_to_pixel32bgr(r
, g
, b
);
1071 /* return true if the palette was modified */
1072 static int update_palette16(VGACommonState
*s
)
1075 uint32_t v
, col
, *palette
;
1078 palette
= s
->last_palette
;
1079 for(i
= 0; i
< 16; i
++) {
1081 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1082 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1084 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1087 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1088 c6_to_8(s
->palette
[v
+ 1]),
1089 c6_to_8(s
->palette
[v
+ 2]));
1090 if (col
!= palette
[i
]) {
1098 /* return true if the palette was modified */
1099 static int update_palette256(VGACommonState
*s
)
1102 uint32_t v
, col
, *palette
;
1105 palette
= s
->last_palette
;
1107 for(i
= 0; i
< 256; i
++) {
1109 col
= s
->rgb_to_pixel(s
->palette
[v
],
1113 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1114 c6_to_8(s
->palette
[v
+ 1]),
1115 c6_to_8(s
->palette
[v
+ 2]));
1117 if (col
!= palette
[i
]) {
1126 static void vga_get_offsets(VGACommonState
*s
,
1127 uint32_t *pline_offset
,
1128 uint32_t *pstart_addr
,
1129 uint32_t *pline_compare
)
1131 uint32_t start_addr
, line_offset
, line_compare
;
1132 #ifdef CONFIG_BOCHS_VBE
1133 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1134 line_offset
= s
->vbe_line_offset
;
1135 start_addr
= s
->vbe_start_addr
;
1136 line_compare
= 65535;
1140 /* compute line_offset in bytes */
1141 line_offset
= s
->cr
[VGA_CRTC_OFFSET
];
1144 /* starting address */
1145 start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1146 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1149 line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1150 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1151 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1153 *pline_offset
= line_offset
;
1154 *pstart_addr
= start_addr
;
1155 *pline_compare
= line_compare
;
1158 /* update start_addr and line_offset. Return TRUE if modified */
1159 static int update_basic_params(VGACommonState
*s
)
1162 uint32_t start_addr
, line_offset
, line_compare
;
1166 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1168 if (line_offset
!= s
->line_offset
||
1169 start_addr
!= s
->start_addr
||
1170 line_compare
!= s
->line_compare
) {
1171 s
->line_offset
= line_offset
;
1172 s
->start_addr
= start_addr
;
1173 s
->line_compare
= line_compare
;
1181 static inline int get_depth_index(DisplayState
*s
)
1183 switch(ds_get_bits_per_pixel(s
)) {
1192 if (is_surface_bgr(s
->surface
))
1199 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1209 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1211 vga_draw_glyph16_16
,
1212 vga_draw_glyph16_16
,
1213 vga_draw_glyph16_32
,
1214 vga_draw_glyph16_32
,
1215 vga_draw_glyph16_16
,
1216 vga_draw_glyph16_16
,
1219 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1229 static const uint8_t cursor_glyph
[32 * 4] = {
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,
1237 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1238 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1240 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1241 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1242 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1248 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1249 int *pcwidth
, int *pcheight
)
1251 int width
, cwidth
, height
, cheight
;
1253 /* total width & height */
1254 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1256 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
1259 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
1260 cwidth
= 16; /* NOTE: no 18 pixel wide */
1262 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1263 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1264 /* ugly hack for CGA 160x100x16 - explain me the logic */
1267 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1268 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1269 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1270 height
= (height
+ 1) / cheight
;
1276 *pcheight
= cheight
;
1279 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1281 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1286 rgb_to_pixel32bgr_dup
,
1287 rgb_to_pixel15bgr_dup
,
1288 rgb_to_pixel16bgr_dup
,
1299 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1301 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1302 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1303 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1304 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1305 const uint8_t *font_ptr
, *font_base
[2];
1306 int dup9
, line_offset
, depth_index
;
1308 uint32_t *ch_attr_ptr
;
1309 vga_draw_glyph8_func
*vga_draw_glyph8
;
1310 vga_draw_glyph9_func
*vga_draw_glyph9
;
1311 int64_t now
= qemu_get_clock_ms(vm_clock
);
1313 /* compute font data address (in plane 2) */
1314 v
= s
->sr
[VGA_SEQ_CHARACTER_MAP
];
1315 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1316 if (offset
!= s
->font_offsets
[0]) {
1317 s
->font_offsets
[0] = offset
;
1320 font_base
[0] = s
->vram_ptr
+ offset
;
1322 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1323 font_base
[1] = s
->vram_ptr
+ offset
;
1324 if (offset
!= s
->font_offsets
[1]) {
1325 s
->font_offsets
[1] = offset
;
1328 if (s
->plane_updated
& (1 << 2) || s
->chain4_alias
) {
1329 /* if the plane 2 was modified since the last display, it
1330 indicates the font may have been modified */
1331 s
->plane_updated
= 0;
1334 full_update
|= update_basic_params(s
);
1336 line_offset
= s
->line_offset
;
1338 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1339 if ((height
* width
) <= 1) {
1340 /* better than nothing: exit if transient size is too small */
1343 if ((height
* width
) > CH_ATTR_SIZE
) {
1344 /* better than nothing: exit if transient size is too big */
1348 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1349 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1350 s
->last_scr_width
= width
* cw
;
1351 s
->last_scr_height
= height
* cheight
;
1352 qemu_console_resize(s
->ds
, s
->last_scr_width
, s
->last_scr_height
);
1354 s
->last_width
= width
;
1355 s
->last_height
= height
;
1356 s
->last_ch
= cheight
;
1361 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1362 full_update
|= update_palette16(s
);
1363 palette
= s
->last_palette
;
1364 x_incr
= cw
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1366 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1367 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1368 if (cursor_offset
!= s
->cursor_offset
||
1369 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1370 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1371 /* if the cursor position changed, we update the old and new
1373 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1374 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1375 if (cursor_offset
< CH_ATTR_SIZE
)
1376 s
->last_ch_attr
[cursor_offset
] = -1;
1377 s
->cursor_offset
= cursor_offset
;
1378 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1379 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1381 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1382 if (now
>= s
->cursor_blink_time
) {
1383 s
->cursor_blink_time
= now
+ VGA_TEXT_CURSOR_PERIOD_MS
/ 2;
1384 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1387 depth_index
= get_depth_index(s
->ds
);
1389 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1391 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1392 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1394 dest
= ds_get_data(s
->ds
);
1395 linesize
= ds_get_linesize(s
->ds
);
1396 ch_attr_ptr
= s
->last_ch_attr
;
1398 offset
= s
->start_addr
* 4;
1399 for(cy
= 0; cy
< height
; cy
++) {
1401 src
= s
->vram_ptr
+ offset
;
1404 for(cx
= 0; cx
< width
; cx
++) {
1405 ch_attr
= *(uint16_t *)src
;
1406 if (full_update
|| ch_attr
!= *ch_attr_ptr
|| src
== cursor_ptr
) {
1411 *ch_attr_ptr
= ch_attr
;
1412 #ifdef HOST_WORDS_BIGENDIAN
1414 cattr
= ch_attr
& 0xff;
1416 ch
= ch_attr
& 0xff;
1417 cattr
= ch_attr
>> 8;
1419 font_ptr
= font_base
[(cattr
>> 3) & 1];
1420 font_ptr
+= 32 * 4 * ch
;
1421 bgcol
= palette
[cattr
>> 4];
1422 fgcol
= palette
[cattr
& 0x0f];
1424 vga_draw_glyph8(d1
, linesize
,
1425 font_ptr
, cheight
, fgcol
, bgcol
);
1428 if (ch
>= 0xb0 && ch
<= 0xdf &&
1429 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1432 vga_draw_glyph9(d1
, linesize
,
1433 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1435 if (src
== cursor_ptr
&&
1436 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20) &&
1437 s
->cursor_visible_phase
) {
1438 int line_start
, line_last
, h
;
1439 /* draw the cursor */
1440 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1441 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1442 /* XXX: check that */
1443 if (line_last
> cheight
- 1)
1444 line_last
= cheight
- 1;
1445 if (line_last
>= line_start
&& line_start
< cheight
) {
1446 h
= line_last
- line_start
+ 1;
1447 d
= d1
+ linesize
* line_start
;
1449 vga_draw_glyph8(d
, linesize
,
1450 cursor_glyph
, h
, fgcol
, bgcol
);
1452 vga_draw_glyph9(d
, linesize
,
1453 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1463 dpy_update(s
->ds
, cx_min
* cw
, cy
* cheight
,
1464 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1466 dest
+= linesize
* cheight
;
1467 line1
= line
+ cheight
;
1468 offset
+= line_offset
;
1469 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1490 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1500 vga_draw_line2d2_16
,
1501 vga_draw_line2d2_16
,
1502 vga_draw_line2d2_32
,
1503 vga_draw_line2d2_32
,
1504 vga_draw_line2d2_16
,
1505 vga_draw_line2d2_16
,
1516 vga_draw_line4d2_16
,
1517 vga_draw_line4d2_16
,
1518 vga_draw_line4d2_32
,
1519 vga_draw_line4d2_32
,
1520 vga_draw_line4d2_16
,
1521 vga_draw_line4d2_16
,
1524 vga_draw_line8d2_16
,
1525 vga_draw_line8d2_16
,
1526 vga_draw_line8d2_32
,
1527 vga_draw_line8d2_32
,
1528 vga_draw_line8d2_16
,
1529 vga_draw_line8d2_16
,
1543 vga_draw_line15_32bgr
,
1544 vga_draw_line15_15bgr
,
1545 vga_draw_line15_16bgr
,
1551 vga_draw_line16_32bgr
,
1552 vga_draw_line16_15bgr
,
1553 vga_draw_line16_16bgr
,
1559 vga_draw_line24_32bgr
,
1560 vga_draw_line24_15bgr
,
1561 vga_draw_line24_16bgr
,
1567 vga_draw_line32_32bgr
,
1568 vga_draw_line32_15bgr
,
1569 vga_draw_line32_16bgr
,
1572 static int vga_get_bpp(VGACommonState
*s
)
1575 #ifdef CONFIG_BOCHS_VBE
1576 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1577 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1586 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1590 #ifdef CONFIG_BOCHS_VBE
1591 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1592 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1593 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1597 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1598 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1599 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1600 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1601 height
= (height
+ 1);
1607 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1610 if (y1
>= VGA_MAX_HEIGHT
)
1612 if (y2
>= VGA_MAX_HEIGHT
)
1613 y2
= VGA_MAX_HEIGHT
;
1614 for(y
= y1
; y
< y2
; y
++) {
1615 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1619 static void vga_sync_dirty_bitmap(VGACommonState
*s
)
1621 memory_region_sync_dirty_bitmap(&s
->vram
);
1624 void vga_dirty_log_start(VGACommonState
*s
)
1626 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1629 void vga_dirty_log_stop(VGACommonState
*s
)
1631 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1637 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1639 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1640 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1641 ram_addr_t page0
, page1
, page_min
, page_max
;
1642 int disp_width
, multi_scan
, multi_run
;
1644 uint32_t v
, addr1
, addr
;
1645 vga_draw_line_func
*vga_draw_line
;
1647 full_update
|= update_basic_params(s
);
1650 vga_sync_dirty_bitmap(s
);
1652 s
->get_resolution(s
, &width
, &height
);
1655 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1656 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1657 if (shift_control
!= 1) {
1658 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1661 /* in CGA modes, multi_scan is ignored */
1662 /* XXX: is it correct ? */
1663 multi_scan
= double_scan
;
1665 multi_run
= multi_scan
;
1666 if (shift_control
!= s
->shift_control
||
1667 double_scan
!= s
->double_scan
) {
1669 s
->shift_control
= shift_control
;
1670 s
->double_scan
= double_scan
;
1673 if (shift_control
== 0) {
1674 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1677 } else if (shift_control
== 1) {
1678 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1683 depth
= s
->get_bpp(s
);
1684 if (s
->line_offset
!= s
->last_line_offset
||
1685 disp_width
!= s
->last_width
||
1686 height
!= s
->last_height
||
1687 s
->last_depth
!= depth
) {
1688 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1689 if (depth
== 16 || depth
== 32) {
1693 qemu_free_displaysurface(s
->ds
);
1694 s
->ds
->surface
= qemu_create_displaysurface_from(disp_width
, height
, depth
,
1696 s
->vram_ptr
+ (s
->start_addr
* 4));
1697 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
1698 s
->ds
->surface
->pf
= qemu_different_endianness_pixelformat(depth
);
1702 qemu_console_resize(s
->ds
, disp_width
, height
);
1704 s
->last_scr_width
= disp_width
;
1705 s
->last_scr_height
= height
;
1706 s
->last_width
= disp_width
;
1707 s
->last_height
= height
;
1708 s
->last_line_offset
= s
->line_offset
;
1709 s
->last_depth
= depth
;
1711 } else if (is_buffer_shared(s
->ds
->surface
) &&
1712 (full_update
|| s
->ds
->surface
->data
!= s
->vram_ptr
+ (s
->start_addr
* 4))) {
1713 s
->ds
->surface
->data
= s
->vram_ptr
+ (s
->start_addr
* 4);
1718 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1720 if (shift_control
== 0) {
1721 full_update
|= update_palette16(s
);
1722 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1723 v
= VGA_DRAW_LINE4D2
;
1728 } else if (shift_control
== 1) {
1729 full_update
|= update_palette16(s
);
1730 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1731 v
= VGA_DRAW_LINE2D2
;
1737 switch(s
->get_bpp(s
)) {
1740 full_update
|= update_palette256(s
);
1741 v
= VGA_DRAW_LINE8D2
;
1745 full_update
|= update_palette256(s
);
1750 v
= VGA_DRAW_LINE15
;
1754 v
= VGA_DRAW_LINE16
;
1758 v
= VGA_DRAW_LINE24
;
1762 v
= VGA_DRAW_LINE32
;
1767 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+ get_depth_index(s
->ds
)];
1769 if (!is_buffer_shared(s
->ds
->surface
) && s
->cursor_invalidate
)
1770 s
->cursor_invalidate(s
);
1772 line_offset
= s
->line_offset
;
1774 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",
1775 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1776 s
->line_compare
, s
->sr
[VGA_SEQ_CLOCK_MODE
]);
1778 addr1
= (s
->start_addr
* 4);
1779 bwidth
= (width
* bits
+ 7) / 8;
1783 d
= ds_get_data(s
->ds
);
1784 linesize
= ds_get_linesize(s
->ds
);
1786 for(y
= 0; y
< height
; y
++) {
1788 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1790 /* CGA compatibility handling */
1791 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1792 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1794 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1795 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1797 update
= full_update
;
1799 page1
= addr
+ bwidth
- 1;
1800 update
|= memory_region_get_dirty(&s
->vram
, page0
, page1
- page0
,
1802 /* explicit invalidation for the hardware cursor */
1803 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1807 if (page0
< page_min
)
1809 if (page1
> page_max
)
1811 if (!(is_buffer_shared(s
->ds
->surface
))) {
1812 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1813 if (s
->cursor_draw_line
)
1814 s
->cursor_draw_line(s
, d
, y
);
1818 /* flush to display */
1819 dpy_update(s
->ds
, 0, y_start
,
1820 disp_width
, y
- y_start
);
1825 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1826 if ((y1
& mask
) == mask
)
1827 addr1
+= line_offset
;
1829 multi_run
= multi_scan
;
1833 /* line compare acts on the displayed lines */
1834 if (y
== s
->line_compare
)
1839 /* flush to display */
1840 dpy_update(s
->ds
, 0, y_start
,
1841 disp_width
, y
- y_start
);
1843 /* reset modified pages */
1844 if (page_max
>= page_min
) {
1845 memory_region_reset_dirty(&s
->vram
,
1847 page_max
- page_min
,
1850 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1853 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1860 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1864 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1865 if (ds_get_bits_per_pixel(s
->ds
) == 8)
1866 val
= s
->rgb_to_pixel(0, 0, 0);
1869 w
= s
->last_scr_width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1870 d
= ds_get_data(s
->ds
);
1871 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1873 d
+= ds_get_linesize(s
->ds
);
1875 dpy_update(s
->ds
, 0, 0,
1876 s
->last_scr_width
, s
->last_scr_height
);
1879 #define GMODE_TEXT 0
1880 #define GMODE_GRAPH 1
1881 #define GMODE_BLANK 2
1883 static void vga_update_display(void *opaque
)
1885 VGACommonState
*s
= opaque
;
1886 int full_update
, graphic_mode
;
1888 qemu_flush_coalesced_mmio_buffer();
1890 if (ds_get_bits_per_pixel(s
->ds
) == 0) {
1894 if (!(s
->ar_index
& 0x20)) {
1895 graphic_mode
= GMODE_BLANK
;
1897 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1899 if (graphic_mode
!= s
->graphic_mode
) {
1900 s
->graphic_mode
= graphic_mode
;
1901 s
->cursor_blink_time
= qemu_get_clock_ms(vm_clock
);
1904 switch(graphic_mode
) {
1906 vga_draw_text(s
, full_update
);
1909 vga_draw_graphic(s
, full_update
);
1913 vga_draw_blank(s
, full_update
);
1919 /* force a full display refresh */
1920 static void vga_invalidate_display(void *opaque
)
1922 VGACommonState
*s
= opaque
;
1925 s
->last_height
= -1;
1928 void vga_common_reset(VGACommonState
*s
)
1931 memset(s
->sr
, '\0', sizeof(s
->sr
));
1933 memset(s
->gr
, '\0', sizeof(s
->gr
));
1935 memset(s
->ar
, '\0', sizeof(s
->ar
));
1936 s
->ar_flip_flop
= 0;
1938 memset(s
->cr
, '\0', sizeof(s
->cr
));
1944 s
->dac_sub_index
= 0;
1945 s
->dac_read_index
= 0;
1946 s
->dac_write_index
= 0;
1947 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1949 memset(s
->palette
, '\0', sizeof(s
->palette
));
1951 #ifdef CONFIG_BOCHS_VBE
1953 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1954 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1955 s
->vbe_start_addr
= 0;
1956 s
->vbe_line_offset
= 0;
1957 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1959 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1960 s
->graphic_mode
= -1; /* force full update */
1961 s
->shift_control
= 0;
1964 s
->line_compare
= 0;
1966 s
->plane_updated
= 0;
1971 s
->last_scr_width
= 0;
1972 s
->last_scr_height
= 0;
1973 s
->cursor_start
= 0;
1975 s
->cursor_offset
= 0;
1976 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1977 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1978 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1979 switch (vga_retrace_method
) {
1980 case VGA_RETRACE_DUMB
:
1982 case VGA_RETRACE_PRECISE
:
1983 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1986 vga_update_memory_access(s
);
1989 static void vga_reset(void *opaque
)
1991 VGACommonState
*s
= opaque
;
1992 vga_common_reset(s
);
1995 #define TEXTMODE_X(x) ((x) % width)
1996 #define TEXTMODE_Y(x) ((x) / width)
1997 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1998 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1999 /* relay text rendering to the display driver
2000 * instead of doing a full vga_update_display() */
2001 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
2003 VGACommonState
*s
= opaque
;
2004 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
2005 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
2007 console_ch_t
*dst
, val
;
2008 char msg_buffer
[80];
2009 int full_update
= 0;
2011 qemu_flush_coalesced_mmio_buffer();
2013 if (!(s
->ar_index
& 0x20)) {
2014 graphic_mode
= GMODE_BLANK
;
2016 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
2018 if (graphic_mode
!= s
->graphic_mode
) {
2019 s
->graphic_mode
= graphic_mode
;
2022 if (s
->last_width
== -1) {
2027 switch (graphic_mode
) {
2029 /* TODO: update palette */
2030 full_update
|= update_basic_params(s
);
2032 /* total width & height */
2033 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
2035 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
2038 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
2039 cw
= 16; /* NOTE: no 18 pixel wide */
2041 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
2042 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
2043 /* ugly hack for CGA 160x100x16 - explain me the logic */
2046 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
2047 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
2048 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
2049 height
= (height
+ 1) / cheight
;
2052 size
= (height
* width
);
2053 if (size
> CH_ATTR_SIZE
) {
2057 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
2062 if (width
!= s
->last_width
|| height
!= s
->last_height
||
2063 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
2064 s
->last_scr_width
= width
* cw
;
2065 s
->last_scr_height
= height
* cheight
;
2066 s
->ds
->surface
->width
= width
;
2067 s
->ds
->surface
->height
= height
;
2069 s
->last_width
= width
;
2070 s
->last_height
= height
;
2071 s
->last_ch
= cheight
;
2076 /* Update "hardware" cursor */
2077 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
2078 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
2079 if (cursor_offset
!= s
->cursor_offset
||
2080 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
2081 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
2082 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
2083 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
2085 TEXTMODE_X(cursor_offset
),
2086 TEXTMODE_Y(cursor_offset
));
2088 dpy_cursor(s
->ds
, -1, -1);
2089 s
->cursor_offset
= cursor_offset
;
2090 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
2091 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
2094 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2098 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2099 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2101 dpy_update(s
->ds
, 0, 0, width
, height
);
2105 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2106 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2114 for (; i
< size
; src
++, dst
++, i
++) {
2115 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2122 if (c_min
<= c_max
) {
2123 i
= TEXTMODE_Y(c_min
);
2124 dpy_update(s
->ds
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2133 s
->get_resolution(s
, &width
, &height
);
2134 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2142 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2146 /* Display a message */
2148 s
->last_height
= height
= 3;
2149 dpy_cursor(s
->ds
, -1, -1);
2150 s
->ds
->surface
->width
= s
->last_width
;
2151 s
->ds
->surface
->height
= height
;
2154 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2155 console_write_ch(dst
++, ' ');
2157 size
= strlen(msg_buffer
);
2158 width
= (s
->last_width
- size
) / 2;
2159 dst
= chardata
+ s
->last_width
+ width
;
2160 for (i
= 0; i
< size
; i
++)
2161 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2163 dpy_update(s
->ds
, 0, 0, s
->last_width
, height
);
2166 static uint64_t vga_mem_read(void *opaque
, target_phys_addr_t addr
,
2169 VGACommonState
*s
= opaque
;
2171 return vga_mem_readb(s
, addr
);
2174 static void vga_mem_write(void *opaque
, target_phys_addr_t addr
,
2175 uint64_t data
, unsigned size
)
2177 VGACommonState
*s
= opaque
;
2179 return vga_mem_writeb(s
, addr
, data
);
2182 const MemoryRegionOps vga_mem_ops
= {
2183 .read
= vga_mem_read
,
2184 .write
= vga_mem_write
,
2185 .endianness
= DEVICE_LITTLE_ENDIAN
,
2187 .min_access_size
= 1,
2188 .max_access_size
= 1,
2192 static int vga_common_post_load(void *opaque
, int version_id
)
2194 VGACommonState
*s
= opaque
;
2197 s
->graphic_mode
= -1;
2201 const VMStateDescription vmstate_vga_common
= {
2204 .minimum_version_id
= 2,
2205 .minimum_version_id_old
= 2,
2206 .post_load
= vga_common_post_load
,
2207 .fields
= (VMStateField
[]) {
2208 VMSTATE_UINT32(latch
, VGACommonState
),
2209 VMSTATE_UINT8(sr_index
, VGACommonState
),
2210 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2211 VMSTATE_UINT8(gr_index
, VGACommonState
),
2212 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2213 VMSTATE_UINT8(ar_index
, VGACommonState
),
2214 VMSTATE_BUFFER(ar
, VGACommonState
),
2215 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2216 VMSTATE_UINT8(cr_index
, VGACommonState
),
2217 VMSTATE_BUFFER(cr
, VGACommonState
),
2218 VMSTATE_UINT8(msr
, VGACommonState
),
2219 VMSTATE_UINT8(fcr
, VGACommonState
),
2220 VMSTATE_UINT8(st00
, VGACommonState
),
2221 VMSTATE_UINT8(st01
, VGACommonState
),
2223 VMSTATE_UINT8(dac_state
, VGACommonState
),
2224 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2225 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2226 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2227 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2228 VMSTATE_BUFFER(palette
, VGACommonState
),
2230 VMSTATE_INT32(bank_offset
, VGACommonState
),
2231 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2232 #ifdef CONFIG_BOCHS_VBE
2233 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2234 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2235 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2236 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2237 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2239 VMSTATE_END_OF_LIST()
2243 void vga_common_init(VGACommonState
*s
)
2247 for(i
= 0;i
< 256; i
++) {
2249 for(j
= 0; j
< 8; j
++) {
2250 v
|= ((i
>> j
) & 1) << (j
* 4);
2255 for(j
= 0; j
< 4; j
++) {
2256 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2260 for(i
= 0; i
< 16; i
++) {
2262 for(j
= 0; j
< 4; j
++) {
2265 v
|= b
<< (2 * j
+ 1);
2270 /* valid range: 1 MB -> 256 MB */
2271 s
->vram_size
= 1024 * 1024;
2272 while (s
->vram_size
< (s
->vram_size_mb
<< 20) &&
2273 s
->vram_size
< (256 << 20)) {
2276 s
->vram_size_mb
= s
->vram_size
>> 20;
2278 #ifdef CONFIG_BOCHS_VBE
2279 s
->is_vbe_vmstate
= 1;
2281 s
->is_vbe_vmstate
= 0;
2283 memory_region_init_ram(&s
->vram
, "vga.vram", s
->vram_size
);
2284 vmstate_register_ram_global(&s
->vram
);
2285 xen_register_framebuffer(&s
->vram
);
2286 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2287 s
->get_bpp
= vga_get_bpp
;
2288 s
->get_offsets
= vga_get_offsets
;
2289 s
->get_resolution
= vga_get_resolution
;
2290 s
->update
= vga_update_display
;
2291 s
->invalidate
= vga_invalidate_display
;
2292 s
->screen_dump
= vga_screen_dump
;
2293 s
->text_update
= vga_update_text
;
2294 switch (vga_retrace_method
) {
2295 case VGA_RETRACE_DUMB
:
2296 s
->retrace
= vga_dumb_retrace
;
2297 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2300 case VGA_RETRACE_PRECISE
:
2301 s
->retrace
= vga_precise_retrace
;
2302 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2305 vga_dirty_log_start(s
);
2308 static const MemoryRegionPortio vga_portio_list
[] = {
2309 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2310 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2311 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2312 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2313 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2314 PORTIO_END_OF_LIST(),
2317 #ifdef CONFIG_BOCHS_VBE
2318 static const MemoryRegionPortio vbe_portio_list
[] = {
2319 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2321 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2323 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2325 PORTIO_END_OF_LIST(),
2327 #endif /* CONFIG_BOCHS_VBE */
2329 /* Used by both ISA and PCI */
2330 MemoryRegion
*vga_init_io(VGACommonState
*s
,
2331 const MemoryRegionPortio
**vga_ports
,
2332 const MemoryRegionPortio
**vbe_ports
)
2334 MemoryRegion
*vga_mem
;
2336 *vga_ports
= vga_portio_list
;
2338 #ifdef CONFIG_BOCHS_VBE
2339 *vbe_ports
= vbe_portio_list
;
2342 vga_mem
= g_malloc(sizeof(*vga_mem
));
2343 memory_region_init_io(vga_mem
, &vga_mem_ops
, s
,
2344 "vga-lowmem", 0x20000);
2345 memory_region_set_flush_coalesced(vga_mem
);
2350 void vga_init(VGACommonState
*s
, MemoryRegion
*address_space
,
2351 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2353 MemoryRegion
*vga_io_memory
;
2354 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2355 PortioList
*vga_port_list
= g_new(PortioList
, 1);
2356 PortioList
*vbe_port_list
= g_new(PortioList
, 1);
2358 qemu_register_reset(vga_reset
, s
);
2362 s
->legacy_address_space
= address_space
;
2364 vga_io_memory
= vga_init_io(s
, &vga_ports
, &vbe_ports
);
2365 memory_region_add_subregion_overlap(address_space
,
2366 isa_mem_base
+ 0x000a0000,
2369 memory_region_set_coalescing(vga_io_memory
);
2370 if (init_vga_ports
) {
2371 portio_list_init(vga_port_list
, vga_ports
, s
, "vga");
2372 portio_list_add(vga_port_list
, address_space_io
, 0x3b0);
2375 portio_list_init(vbe_port_list
, vbe_ports
, s
, "vbe");
2376 portio_list_add(vbe_port_list
, address_space_io
, 0x1ce);
2380 void vga_init_vbe(VGACommonState
*s
, MemoryRegion
*system_memory
)
2382 #ifdef CONFIG_BOCHS_VBE
2383 /* With pc-0.12 and below we map both the PCI BAR and the fixed VBE region,
2384 * so use an alias to avoid double-mapping the same region.
2386 memory_region_init_alias(&s
->vram_vbe
, "vram.vbe",
2387 &s
->vram
, 0, memory_region_size(&s
->vram
));
2388 /* XXX: use optimized standard vga accesses */
2389 memory_region_add_subregion(system_memory
,
2390 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,
2395 /********************************************************/
2396 /* vga screen dump */
2398 void ppm_save(const char *filename
, struct DisplaySurface
*ds
, Error
**errp
)
2406 char *linebuf
, *pbuf
;
2408 trace_ppm_save(filename
, ds
);
2409 f
= fopen(filename
, "wb");
2411 error_setg(errp
, "failed to open file '%s': %s", filename
,
2415 ret
= fprintf(f
, "P6\n%d %d\n%d\n", ds
->width
, ds
->height
, 255);
2420 linebuf
= g_malloc(ds
->width
* 3);
2422 for(y
= 0; y
< ds
->height
; y
++) {
2425 for(x
= 0; x
< ds
->width
; x
++) {
2426 if (ds
->pf
.bits_per_pixel
== 32)
2429 v
= (uint32_t) (*(uint16_t *)d
);
2430 /* Limited to 8 or fewer bits per channel: */
2431 r
= ((v
>> ds
->pf
.rshift
) & ds
->pf
.rmax
) << (8 - ds
->pf
.rbits
);
2432 g
= ((v
>> ds
->pf
.gshift
) & ds
->pf
.gmax
) << (8 - ds
->pf
.gbits
);
2433 b
= ((v
>> ds
->pf
.bshift
) & ds
->pf
.bmax
) << (8 - ds
->pf
.bbits
);
2437 d
+= ds
->pf
.bytes_per_pixel
;
2441 ret
= fwrite(linebuf
, 1, pbuf
- linebuf
, f
);
2454 error_setg(errp
, "failed to write to file '%s': %s", filename
,
2460 /* save the vga display in a PPM image even if no display is
2462 static void vga_screen_dump(void *opaque
, const char *filename
, bool cswitch
,
2465 VGACommonState
*s
= opaque
;
2468 vga_invalidate_display(s
);
2471 ppm_save(filename
, s
->ds
->surface
, errp
);