4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "sysemu/reset.h"
28 #include "qapi/error.h"
29 #include "hw/display/vga.h"
30 #include "hw/pci/pci.h"
33 #include "ui/pixel_ops.h"
34 #include "qemu/timer.h"
35 #include "hw/xen/xen.h"
36 #include "migration/vmstate.h"
39 //#define DEBUG_VGA_MEM
40 //#define DEBUG_VGA_REG
44 /* 16 state changes per vertical frame @60 Hz */
45 #define VGA_TEXT_CURSOR_PERIOD_MS (1000 * 2 * 16 / 60)
48 * Video Graphics Array (VGA)
50 * Chipset docs for original IBM VGA:
51 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
54 * http://www.osdever.net/FreeVGA/home.htm
56 * Standard VGA features and Bochs VBE extensions are implemented.
59 /* force some bits to zero */
60 const uint8_t sr_mask
[8] = {
71 const uint8_t gr_mask
[16] = {
90 #define cbswap_32(__x) \
92 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
93 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
94 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
95 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
97 #ifdef HOST_WORDS_BIGENDIAN
98 #define PAT(x) cbswap_32(x)
103 #ifdef HOST_WORDS_BIGENDIAN
109 #ifdef HOST_WORDS_BIGENDIAN
110 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
112 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
115 static const uint32_t mask16
[16] = {
136 #ifdef HOST_WORDS_BIGENDIAN
139 #define PAT(x) cbswap_32(x)
142 static uint32_t expand4
[256];
143 static uint16_t expand2
[256];
144 static uint8_t expand4to8
[16];
146 static void vbe_update_vgaregs(VGACommonState
*s
);
148 static inline bool vbe_enabled(VGACommonState
*s
)
150 return s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
;
153 static inline uint8_t sr(VGACommonState
*s
, int idx
)
155 return vbe_enabled(s
) ? s
->sr_vbe
[idx
] : s
->sr
[idx
];
158 static void vga_update_memory_access(VGACommonState
*s
)
160 hwaddr base
, offset
, size
;
162 if (s
->legacy_address_space
== NULL
) {
166 if (s
->has_chain4_alias
) {
167 memory_region_del_subregion(s
->legacy_address_space
, &s
->chain4_alias
);
168 object_unparent(OBJECT(&s
->chain4_alias
));
169 s
->has_chain4_alias
= false;
170 s
->plane_updated
= 0xf;
172 if ((sr(s
, VGA_SEQ_PLANE_WRITE
) & VGA_SR02_ALL_PLANES
) ==
173 VGA_SR02_ALL_PLANES
&& sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
175 switch ((s
->gr
[VGA_GFX_MISC
] >> 2) & 3) {
183 offset
= s
->bank_offset
;
195 assert(offset
+ size
<= s
->vram_size
);
196 memory_region_init_alias(&s
->chain4_alias
, memory_region_owner(&s
->vram
),
197 "vga.chain4", &s
->vram
, offset
, size
);
198 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
199 &s
->chain4_alias
, 2);
200 s
->has_chain4_alias
= true;
204 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
209 static void vga_precise_update_retrace_info(VGACommonState
*s
)
212 int hretr_start_char
;
213 int hretr_skew_chars
;
217 int vretr_start_line
;
226 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
227 int64_t chars_per_sec
;
228 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
230 htotal_chars
= s
->cr
[VGA_CRTC_H_TOTAL
] + 5;
231 hretr_start_char
= s
->cr
[VGA_CRTC_H_SYNC_START
];
232 hretr_skew_chars
= (s
->cr
[VGA_CRTC_H_SYNC_END
] >> 5) & 3;
233 hretr_end_char
= s
->cr
[VGA_CRTC_H_SYNC_END
] & 0x1f;
235 vtotal_lines
= (s
->cr
[VGA_CRTC_V_TOTAL
] |
236 (((s
->cr
[VGA_CRTC_OVERFLOW
] & 1) |
237 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 4) & 2)) << 8)) + 2;
238 vretr_start_line
= s
->cr
[VGA_CRTC_V_SYNC_START
] |
239 ((((s
->cr
[VGA_CRTC_OVERFLOW
] >> 2) & 1) |
240 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 6) & 2)) << 8);
241 vretr_end_line
= s
->cr
[VGA_CRTC_V_SYNC_END
] & 0xf;
243 clocking_mode
= (sr(s
, VGA_SEQ_CLOCK_MODE
) >> 3) & 1;
244 clock_sel
= (s
->msr
>> 2) & 3;
245 dots
= (s
->msr
& 1) ? 8 : 9;
247 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
249 htotal_chars
<<= clocking_mode
;
251 r
->total_chars
= vtotal_lines
* htotal_chars
;
253 r
->ticks_per_char
= NANOSECONDS_PER_SECOND
/ (r
->total_chars
* r
->freq
);
255 r
->ticks_per_char
= NANOSECONDS_PER_SECOND
/ chars_per_sec
;
258 r
->vstart
= vretr_start_line
;
259 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
261 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
262 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
263 r
->htotal
= htotal_chars
;
266 div2
= (s
->cr
[VGA_CRTC_MODE
] >> 2) & 1;
267 sldiv2
= (s
->cr
[VGA_CRTC_MODE
] >> 3) & 1;
277 "div2 = %d sldiv2 = %d\n"
278 "clocking_mode = %d\n"
279 "clock_sel = %d %d\n"
281 "ticks/char = %" PRId64
"\n"
283 (double) NANOSECONDS_PER_SECOND
/ (r
->ticks_per_char
* r
->total_chars
),
301 static uint8_t vga_precise_retrace(VGACommonState
*s
)
303 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
304 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
306 if (r
->total_chars
) {
307 int cur_line
, cur_line_char
, cur_char
;
310 cur_tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
312 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
313 cur_line
= cur_char
/ r
->htotal
;
315 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
316 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
318 cur_line_char
= cur_char
% r
->htotal
;
319 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
320 val
|= ST01_DISP_ENABLE
;
326 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
330 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
332 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
335 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
337 if (s
->msr
& VGA_MIS_COLOR
) {
339 return (addr
>= 0x3b0 && addr
<= 0x3bf);
342 return (addr
>= 0x3d0 && addr
<= 0x3df);
346 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
348 VGACommonState
*s
= opaque
;
351 if (vga_ioport_invalid(s
, addr
)) {
356 if (s
->ar_flip_flop
== 0) {
363 index
= s
->ar_index
& 0x1f;
364 if (index
< VGA_ATT_C
) {
377 val
= s
->sr
[s
->sr_index
];
379 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
386 val
= s
->dac_write_index
;
389 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
390 if (++s
->dac_sub_index
== 3) {
391 s
->dac_sub_index
= 0;
405 val
= s
->gr
[s
->gr_index
];
407 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
416 val
= s
->cr
[s
->cr_index
];
418 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
423 /* just toggle to fool polling */
424 val
= s
->st01
= s
->retrace(s
);
432 trace_vga_std_read_io(addr
, val
);
436 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
438 VGACommonState
*s
= opaque
;
441 /* check port range access depending on color/monochrome mode */
442 if (vga_ioport_invalid(s
, addr
)) {
445 trace_vga_std_write_io(addr
, val
);
449 if (s
->ar_flip_flop
== 0) {
453 index
= s
->ar_index
& 0x1f;
455 case VGA_ATC_PALETTE0
... VGA_ATC_PALETTEF
:
456 s
->ar
[index
] = val
& 0x3f;
459 s
->ar
[index
] = val
& ~0x10;
461 case VGA_ATC_OVERSCAN
:
464 case VGA_ATC_PLANE_ENABLE
:
465 s
->ar
[index
] = val
& ~0xc0;
468 s
->ar
[index
] = val
& ~0xf0;
470 case VGA_ATC_COLOR_PAGE
:
471 s
->ar
[index
] = val
& ~0xf0;
477 s
->ar_flip_flop
^= 1;
480 s
->msr
= val
& ~0x10;
481 s
->update_retrace_info(s
);
484 s
->sr_index
= val
& 7;
488 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
490 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
491 if (s
->sr_index
== VGA_SEQ_CLOCK_MODE
) {
492 s
->update_retrace_info(s
);
494 vga_update_memory_access(s
);
497 s
->dac_read_index
= val
;
498 s
->dac_sub_index
= 0;
502 s
->dac_write_index
= val
;
503 s
->dac_sub_index
= 0;
507 s
->dac_cache
[s
->dac_sub_index
] = val
;
508 if (++s
->dac_sub_index
== 3) {
509 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
510 s
->dac_sub_index
= 0;
511 s
->dac_write_index
++;
515 s
->gr_index
= val
& 0x0f;
519 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
521 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
522 vbe_update_vgaregs(s
);
523 vga_update_memory_access(s
);
532 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
534 /* handle CR0-7 protection */
535 if ((s
->cr
[VGA_CRTC_V_SYNC_END
] & VGA_CR11_LOCK_CR0_CR7
) &&
536 s
->cr_index
<= VGA_CRTC_OVERFLOW
) {
537 /* can always write bit 4 of CR7 */
538 if (s
->cr_index
== VGA_CRTC_OVERFLOW
) {
539 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x10) |
541 vbe_update_vgaregs(s
);
545 s
->cr
[s
->cr_index
] = val
;
546 vbe_update_vgaregs(s
);
548 switch(s
->cr_index
) {
549 case VGA_CRTC_H_TOTAL
:
550 case VGA_CRTC_H_SYNC_START
:
551 case VGA_CRTC_H_SYNC_END
:
552 case VGA_CRTC_V_TOTAL
:
553 case VGA_CRTC_OVERFLOW
:
554 case VGA_CRTC_V_SYNC_END
:
556 s
->update_retrace_info(s
);
568 * Sanity check vbe register writes.
570 * As we don't have a way to signal errors to the guest in the bochs
571 * dispi interface we'll go adjust the registers to the closest valid
574 static void vbe_fixup_regs(VGACommonState
*s
)
576 uint16_t *r
= s
->vbe_regs
;
577 uint32_t bits
, linelength
, maxy
, offset
;
579 if (!vbe_enabled(s
)) {
580 /* vbe is turned off -- nothing to do */
585 switch (r
[VBE_DISPI_INDEX_BPP
]) {
591 bits
= r
[VBE_DISPI_INDEX_BPP
];
597 bits
= r
[VBE_DISPI_INDEX_BPP
] = 8;
602 r
[VBE_DISPI_INDEX_XRES
] &= ~7u;
603 if (r
[VBE_DISPI_INDEX_XRES
] == 0) {
604 r
[VBE_DISPI_INDEX_XRES
] = 8;
606 if (r
[VBE_DISPI_INDEX_XRES
] > VBE_DISPI_MAX_XRES
) {
607 r
[VBE_DISPI_INDEX_XRES
] = VBE_DISPI_MAX_XRES
;
609 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] &= ~7u;
610 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] > VBE_DISPI_MAX_XRES
) {
611 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = VBE_DISPI_MAX_XRES
;
613 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] < r
[VBE_DISPI_INDEX_XRES
]) {
614 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = r
[VBE_DISPI_INDEX_XRES
];
618 linelength
= r
[VBE_DISPI_INDEX_VIRT_WIDTH
] * bits
/ 8;
619 maxy
= s
->vbe_size
/ linelength
;
620 if (r
[VBE_DISPI_INDEX_YRES
] == 0) {
621 r
[VBE_DISPI_INDEX_YRES
] = 1;
623 if (r
[VBE_DISPI_INDEX_YRES
] > VBE_DISPI_MAX_YRES
) {
624 r
[VBE_DISPI_INDEX_YRES
] = VBE_DISPI_MAX_YRES
;
626 if (r
[VBE_DISPI_INDEX_YRES
] > maxy
) {
627 r
[VBE_DISPI_INDEX_YRES
] = maxy
;
631 if (r
[VBE_DISPI_INDEX_X_OFFSET
] > VBE_DISPI_MAX_XRES
) {
632 r
[VBE_DISPI_INDEX_X_OFFSET
] = VBE_DISPI_MAX_XRES
;
634 if (r
[VBE_DISPI_INDEX_Y_OFFSET
] > VBE_DISPI_MAX_YRES
) {
635 r
[VBE_DISPI_INDEX_Y_OFFSET
] = VBE_DISPI_MAX_YRES
;
637 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
638 offset
+= r
[VBE_DISPI_INDEX_Y_OFFSET
] * linelength
;
639 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
640 r
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
641 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
642 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
643 r
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
648 /* update vga state */
649 r
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = maxy
;
650 s
->vbe_line_offset
= linelength
;
651 s
->vbe_start_addr
= offset
/ 4;
654 /* we initialize the VGA graphic mode */
655 static void vbe_update_vgaregs(VGACommonState
*s
)
657 int h
, shift_control
;
659 if (!vbe_enabled(s
)) {
660 /* vbe is turned off -- nothing to do */
664 /* graphic mode + memory map 1 */
665 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
666 VGA_GR06_GRAPHICS_MODE
;
667 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
668 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
670 s
->cr
[VGA_CRTC_H_DISP
] =
671 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
672 /* height (only meaningful if < 1024) */
673 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
674 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
675 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
676 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
677 /* line compare to 1023 */
678 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
679 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
680 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
682 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
684 s
->sr_vbe
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
687 /* set chain 4 mode */
688 s
->sr_vbe
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
689 /* activate all planes */
690 s
->sr_vbe
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
692 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
693 (shift_control
<< 5);
694 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
697 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
699 VGACommonState
*s
= opaque
;
703 uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
705 VGACommonState
*s
= opaque
;
708 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
709 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
710 switch(s
->vbe_index
) {
711 /* XXX: do not hardcode ? */
712 case VBE_DISPI_INDEX_XRES
:
713 val
= VBE_DISPI_MAX_XRES
;
715 case VBE_DISPI_INDEX_YRES
:
716 val
= VBE_DISPI_MAX_YRES
;
718 case VBE_DISPI_INDEX_BPP
:
719 val
= VBE_DISPI_MAX_BPP
;
722 val
= s
->vbe_regs
[s
->vbe_index
];
726 val
= s
->vbe_regs
[s
->vbe_index
];
728 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
729 val
= s
->vbe_size
/ (64 * KiB
);
733 trace_vga_vbe_read(s
->vbe_index
, val
);
737 void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
739 VGACommonState
*s
= opaque
;
743 void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
745 VGACommonState
*s
= opaque
;
747 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
748 trace_vga_vbe_write(s
->vbe_index
, val
);
749 switch(s
->vbe_index
) {
750 case VBE_DISPI_INDEX_ID
:
751 if (val
== VBE_DISPI_ID0
||
752 val
== VBE_DISPI_ID1
||
753 val
== VBE_DISPI_ID2
||
754 val
== VBE_DISPI_ID3
||
755 val
== VBE_DISPI_ID4
||
756 val
== VBE_DISPI_ID5
) {
757 s
->vbe_regs
[s
->vbe_index
] = val
;
760 case VBE_DISPI_INDEX_XRES
:
761 case VBE_DISPI_INDEX_YRES
:
762 case VBE_DISPI_INDEX_BPP
:
763 case VBE_DISPI_INDEX_VIRT_WIDTH
:
764 case VBE_DISPI_INDEX_X_OFFSET
:
765 case VBE_DISPI_INDEX_Y_OFFSET
:
766 s
->vbe_regs
[s
->vbe_index
] = val
;
768 vbe_update_vgaregs(s
);
770 case VBE_DISPI_INDEX_BANK
:
771 val
&= s
->vbe_bank_mask
;
772 s
->vbe_regs
[s
->vbe_index
] = val
;
773 s
->bank_offset
= (val
<< 16);
774 vga_update_memory_access(s
);
776 case VBE_DISPI_INDEX_ENABLE
:
777 if ((val
& VBE_DISPI_ENABLED
) &&
778 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
780 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = 0;
781 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
782 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
783 s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] |= VBE_DISPI_ENABLED
;
785 vbe_update_vgaregs(s
);
787 /* clear the screen */
788 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
789 memset(s
->vram_ptr
, 0,
790 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
795 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
796 s
->vbe_regs
[s
->vbe_index
] = val
;
797 vga_update_memory_access(s
);
805 /* called for accesses between 0xa0000 and 0xc0000 */
806 uint32_t vga_mem_readb(VGACommonState
*s
, hwaddr addr
)
808 int memory_map_mode
, plane
;
811 /* convert to VGA memory offset */
812 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
814 switch(memory_map_mode
) {
820 addr
+= s
->bank_offset
;
835 if (sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
836 /* chain 4 mode : simplest access */
837 assert(addr
< s
->vram_size
);
838 ret
= s
->vram_ptr
[addr
];
839 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
840 /* odd/even mode (aka text mode mapping) */
841 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
842 addr
= ((addr
& ~1) << 1) | plane
;
843 if (addr
>= s
->vram_size
) {
846 ret
= s
->vram_ptr
[addr
];
848 /* standard VGA latched access */
849 if (addr
* sizeof(uint32_t) >= s
->vram_size
) {
852 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
854 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
856 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
857 ret
= GET_PLANE(s
->latch
, plane
);
860 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
861 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
870 /* called for accesses between 0xa0000 and 0xc0000 */
871 void vga_mem_writeb(VGACommonState
*s
, hwaddr addr
, uint32_t val
)
873 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
874 uint32_t write_mask
, bit_mask
, set_mask
;
877 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
879 /* convert to VGA memory offset */
880 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
882 switch(memory_map_mode
) {
888 addr
+= s
->bank_offset
;
903 if (sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
904 /* chain 4 mode : simplest access */
907 if (sr(s
, VGA_SEQ_PLANE_WRITE
) & mask
) {
908 assert(addr
< s
->vram_size
);
909 s
->vram_ptr
[addr
] = val
;
911 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
913 s
->plane_updated
|= mask
; /* only used to detect font change */
914 memory_region_set_dirty(&s
->vram
, addr
, 1);
916 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
917 /* odd/even mode (aka text mode mapping) */
918 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
920 if (sr(s
, VGA_SEQ_PLANE_WRITE
) & mask
) {
921 addr
= ((addr
& ~1) << 1) | plane
;
922 if (addr
>= s
->vram_size
) {
925 s
->vram_ptr
[addr
] = val
;
927 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
929 s
->plane_updated
|= mask
; /* only used to detect font change */
930 memory_region_set_dirty(&s
->vram
, addr
, 1);
933 /* standard VGA latched access */
934 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
939 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
940 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
944 /* apply set/reset mask */
945 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
946 val
= (val
& ~set_mask
) |
947 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
948 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
954 val
= mask16
[val
& 0x0f];
955 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
959 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
960 val
= (val
>> b
) | (val
<< (8 - b
));
962 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
963 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
967 /* apply logical operation */
968 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
969 switch(func_select
) {
989 bit_mask
|= bit_mask
<< 8;
990 bit_mask
|= bit_mask
<< 16;
991 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
994 /* mask data according to sr[2] */
995 mask
= sr(s
, VGA_SEQ_PLANE_WRITE
);
996 s
->plane_updated
|= mask
; /* only used to detect font change */
997 write_mask
= mask16
[mask
];
998 if (addr
* sizeof(uint32_t) >= s
->vram_size
) {
1001 ((uint32_t *)s
->vram_ptr
)[addr
] =
1002 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
1004 #ifdef DEBUG_VGA_MEM
1005 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
1006 addr
* 4, write_mask
, val
);
1008 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
1012 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
1013 uint32_t srcaddr
, int width
);
1015 #include "vga-access.h"
1016 #include "vga-helpers.h"
1018 /* return true if the palette was modified */
1019 static int update_palette16(VGACommonState
*s
)
1022 uint32_t v
, col
, *palette
;
1025 palette
= s
->last_palette
;
1026 for(i
= 0; i
< 16; i
++) {
1028 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1029 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1031 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1034 col
= rgb_to_pixel32(c6_to_8(s
->palette
[v
]),
1035 c6_to_8(s
->palette
[v
+ 1]),
1036 c6_to_8(s
->palette
[v
+ 2]));
1037 if (col
!= palette
[i
]) {
1045 /* return true if the palette was modified */
1046 static int update_palette256(VGACommonState
*s
)
1049 uint32_t v
, col
, *palette
;
1052 palette
= s
->last_palette
;
1054 for(i
= 0; i
< 256; i
++) {
1056 col
= rgb_to_pixel32(s
->palette
[v
],
1060 col
= rgb_to_pixel32(c6_to_8(s
->palette
[v
]),
1061 c6_to_8(s
->palette
[v
+ 1]),
1062 c6_to_8(s
->palette
[v
+ 2]));
1064 if (col
!= palette
[i
]) {
1073 static void vga_get_offsets(VGACommonState
*s
,
1074 uint32_t *pline_offset
,
1075 uint32_t *pstart_addr
,
1076 uint32_t *pline_compare
)
1078 uint32_t start_addr
, line_offset
, line_compare
;
1080 if (vbe_enabled(s
)) {
1081 line_offset
= s
->vbe_line_offset
;
1082 start_addr
= s
->vbe_start_addr
;
1083 line_compare
= 65535;
1085 /* compute line_offset in bytes */
1086 line_offset
= s
->cr
[VGA_CRTC_OFFSET
];
1089 /* starting address */
1090 start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1091 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1094 line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1095 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1096 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1098 *pline_offset
= line_offset
;
1099 *pstart_addr
= start_addr
;
1100 *pline_compare
= line_compare
;
1103 /* update start_addr and line_offset. Return TRUE if modified */
1104 static int update_basic_params(VGACommonState
*s
)
1107 uint32_t start_addr
, line_offset
, line_compare
;
1111 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1113 if (line_offset
!= s
->line_offset
||
1114 start_addr
!= s
->start_addr
||
1115 line_compare
!= s
->line_compare
) {
1116 s
->line_offset
= line_offset
;
1117 s
->start_addr
= start_addr
;
1118 s
->line_compare
= line_compare
;
1125 static const uint8_t cursor_glyph
[32 * 4] = {
1126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1140 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1141 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1144 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1145 int *pcwidth
, int *pcheight
)
1147 int width
, cwidth
, height
, cheight
;
1149 /* total width & height */
1150 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1152 if (!(sr(s
, VGA_SEQ_CLOCK_MODE
) & VGA_SR01_CHAR_CLK_8DOTS
)) {
1155 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 0x08) {
1156 cwidth
= 16; /* NOTE: no 18 pixel wide */
1158 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1159 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1160 /* ugly hack for CGA 160x100x16 - explain me the logic */
1163 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1164 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1165 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1166 height
= (height
+ 1) / cheight
;
1172 *pcheight
= cheight
;
1183 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1185 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1186 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1187 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1188 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1189 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1190 const uint8_t *font_ptr
, *font_base
[2];
1191 int dup9
, line_offset
;
1193 uint32_t *ch_attr_ptr
;
1194 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1196 /* compute font data address (in plane 2) */
1197 v
= sr(s
, VGA_SEQ_CHARACTER_MAP
);
1198 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1199 if (offset
!= s
->font_offsets
[0]) {
1200 s
->font_offsets
[0] = offset
;
1203 font_base
[0] = s
->vram_ptr
+ offset
;
1205 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1206 font_base
[1] = s
->vram_ptr
+ offset
;
1207 if (offset
!= s
->font_offsets
[1]) {
1208 s
->font_offsets
[1] = offset
;
1211 if (s
->plane_updated
& (1 << 2) || s
->has_chain4_alias
) {
1212 /* if the plane 2 was modified since the last display, it
1213 indicates the font may have been modified */
1214 s
->plane_updated
= 0;
1217 full_update
|= update_basic_params(s
);
1219 line_offset
= s
->line_offset
;
1221 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1222 if ((height
* width
) <= 1) {
1223 /* better than nothing: exit if transient size is too small */
1226 if ((height
* width
) > CH_ATTR_SIZE
) {
1227 /* better than nothing: exit if transient size is too big */
1231 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1232 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1233 s
->last_scr_width
= width
* cw
;
1234 s
->last_scr_height
= height
* cheight
;
1235 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1236 surface
= qemu_console_surface(s
->con
);
1237 dpy_text_resize(s
->con
, width
, height
);
1239 s
->last_width
= width
;
1240 s
->last_height
= height
;
1241 s
->last_ch
= cheight
;
1245 full_update
|= update_palette16(s
);
1246 palette
= s
->last_palette
;
1247 x_incr
= cw
* surface_bytes_per_pixel(surface
);
1250 s
->full_update_text
= 1;
1252 if (s
->full_update_gfx
) {
1253 s
->full_update_gfx
= 0;
1257 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1258 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1259 if (cursor_offset
!= s
->cursor_offset
||
1260 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1261 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1262 /* if the cursor position changed, we update the old and new
1264 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1265 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1266 if (cursor_offset
< CH_ATTR_SIZE
)
1267 s
->last_ch_attr
[cursor_offset
] = -1;
1268 s
->cursor_offset
= cursor_offset
;
1269 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1270 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1272 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1273 if (now
>= s
->cursor_blink_time
) {
1274 s
->cursor_blink_time
= now
+ VGA_TEXT_CURSOR_PERIOD_MS
/ 2;
1275 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1278 dest
= surface_data(surface
);
1279 linesize
= surface_stride(surface
);
1280 ch_attr_ptr
= s
->last_ch_attr
;
1282 offset
= s
->start_addr
* 4;
1283 for(cy
= 0; cy
< height
; cy
++) {
1285 src
= s
->vram_ptr
+ offset
;
1288 for(cx
= 0; cx
< width
; cx
++) {
1289 if (src
+ sizeof(uint16_t) > s
->vram_ptr
+ s
->vram_size
) {
1292 ch_attr
= *(uint16_t *)src
;
1293 if (full_update
|| ch_attr
!= *ch_attr_ptr
|| src
== cursor_ptr
) {
1298 *ch_attr_ptr
= ch_attr
;
1299 #ifdef HOST_WORDS_BIGENDIAN
1301 cattr
= ch_attr
& 0xff;
1303 ch
= ch_attr
& 0xff;
1304 cattr
= ch_attr
>> 8;
1306 font_ptr
= font_base
[(cattr
>> 3) & 1];
1307 font_ptr
+= 32 * 4 * ch
;
1308 bgcol
= palette
[cattr
>> 4];
1309 fgcol
= palette
[cattr
& 0x0f];
1311 vga_draw_glyph16(d1
, linesize
,
1312 font_ptr
, cheight
, fgcol
, bgcol
);
1313 } else if (cw
!= 9) {
1314 vga_draw_glyph8(d1
, linesize
,
1315 font_ptr
, cheight
, fgcol
, bgcol
);
1318 if (ch
>= 0xb0 && ch
<= 0xdf &&
1319 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1322 vga_draw_glyph9(d1
, linesize
,
1323 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1325 if (src
== cursor_ptr
&&
1326 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20) &&
1327 s
->cursor_visible_phase
) {
1328 int line_start
, line_last
, h
;
1329 /* draw the cursor */
1330 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1331 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1332 /* XXX: check that */
1333 if (line_last
> cheight
- 1)
1334 line_last
= cheight
- 1;
1335 if (line_last
>= line_start
&& line_start
< cheight
) {
1336 h
= line_last
- line_start
+ 1;
1337 d
= d1
+ linesize
* line_start
;
1339 vga_draw_glyph16(d
, linesize
,
1340 cursor_glyph
, h
, fgcol
, bgcol
);
1341 } else if (cw
!= 9) {
1342 vga_draw_glyph8(d
, linesize
,
1343 cursor_glyph
, h
, fgcol
, bgcol
);
1345 vga_draw_glyph9(d
, linesize
,
1346 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1356 dpy_gfx_update(s
->con
, cx_min
* cw
, cy
* cheight
,
1357 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1359 dest
+= linesize
* cheight
;
1360 line1
= line
+ cheight
;
1361 offset
+= line_offset
;
1362 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1387 static vga_draw_line_func
* const vga_draw_line_table
[VGA_DRAW_LINE_NB
] = {
1404 static int vga_get_bpp(VGACommonState
*s
)
1408 if (vbe_enabled(s
)) {
1409 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1416 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1420 if (vbe_enabled(s
)) {
1421 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1422 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1424 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1425 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1426 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1427 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1428 height
= (height
+ 1);
1434 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1437 if (y1
>= VGA_MAX_HEIGHT
)
1439 if (y2
>= VGA_MAX_HEIGHT
)
1440 y2
= VGA_MAX_HEIGHT
;
1441 for(y
= y1
; y
< y2
; y
++) {
1442 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1446 static bool vga_scanline_invalidated(VGACommonState
*s
, int y
)
1448 if (y
>= VGA_MAX_HEIGHT
) {
1451 return s
->invalidated_y_table
[y
>> 5] & (1 << (y
& 0x1f));
1454 void vga_dirty_log_start(VGACommonState
*s
)
1456 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1459 void vga_dirty_log_stop(VGACommonState
*s
)
1461 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1467 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1469 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1470 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1471 int width
, height
, shift_control
, bwidth
, bits
;
1472 ram_addr_t page0
, page1
, region_start
, region_end
;
1473 DirtyBitmapSnapshot
*snap
= NULL
;
1474 int disp_width
, multi_scan
, multi_run
;
1476 uint32_t v
, addr1
, addr
;
1477 vga_draw_line_func
*vga_draw_line
= NULL
;
1478 bool share_surface
, force_shadow
= false;
1479 pixman_format_code_t format
;
1480 #ifdef HOST_WORDS_BIGENDIAN
1481 bool byteswap
= !s
->big_endian_fb
;
1483 bool byteswap
= s
->big_endian_fb
;
1486 full_update
|= update_basic_params(s
);
1488 s
->get_resolution(s
, &width
, &height
);
1490 depth
= s
->get_bpp(s
);
1492 region_start
= (s
->start_addr
* 4);
1493 region_end
= region_start
+ (ram_addr_t
)s
->line_offset
* height
;
1494 region_end
+= width
* depth
/ 8; /* scanline length */
1495 region_end
-= s
->line_offset
;
1496 if (region_end
> s
->vbe_size
|| depth
== 0 || depth
== 15) {
1499 * - wraps around (can happen with cirrus vbe modes)
1500 * - depth == 0 (256 color palette video mode)
1503 * Take the safe and slow route:
1504 * - create a dirty bitmap snapshot for all vga memory.
1505 * - force shadowing (so all vga memory access goes
1506 * through vga_read_*() helpers).
1508 * Given this affects only vga features which are pretty much
1509 * unused by modern guests there should be no performance
1513 region_end
= s
->vbe_size
;
1514 force_shadow
= true;
1517 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1518 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1519 if (shift_control
!= 1) {
1520 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1523 /* in CGA modes, multi_scan is ignored */
1524 /* XXX: is it correct ? */
1525 multi_scan
= double_scan
;
1527 multi_run
= multi_scan
;
1528 if (shift_control
!= s
->shift_control
||
1529 double_scan
!= s
->double_scan
) {
1531 s
->shift_control
= shift_control
;
1532 s
->double_scan
= double_scan
;
1535 if (shift_control
== 0) {
1536 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1539 } else if (shift_control
== 1) {
1540 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1546 * Check whether we can share the surface with the backend
1547 * or whether we need a shadow surface. We share native
1548 * endian surfaces for 15bpp and above and byteswapped
1549 * surfaces for 24bpp and above.
1551 format
= qemu_default_pixman_format(depth
, !byteswap
);
1553 share_surface
= dpy_gfx_check_format(s
->con
, format
)
1554 && !s
->force_shadow
&& !force_shadow
;
1556 share_surface
= false;
1559 if (s
->line_offset
!= s
->last_line_offset
||
1560 disp_width
!= s
->last_width
||
1561 height
!= s
->last_height
||
1562 s
->last_depth
!= depth
||
1563 s
->last_byteswap
!= byteswap
||
1564 share_surface
!= is_buffer_shared(surface
)) {
1565 /* display parameters changed -> need new display surface */
1566 s
->last_scr_width
= disp_width
;
1567 s
->last_scr_height
= height
;
1568 s
->last_width
= disp_width
;
1569 s
->last_height
= height
;
1570 s
->last_line_offset
= s
->line_offset
;
1571 s
->last_depth
= depth
;
1572 s
->last_byteswap
= byteswap
;
1575 if (surface_data(surface
) != s
->vram_ptr
+ (s
->start_addr
* 4)
1576 && is_buffer_shared(surface
)) {
1577 /* base address changed (page flip) -> shared display surfaces
1578 * must be updated with the new base address */
1583 if (share_surface
) {
1584 surface
= qemu_create_displaysurface_from(disp_width
,
1585 height
, format
, s
->line_offset
,
1586 s
->vram_ptr
+ (s
->start_addr
* 4));
1587 dpy_gfx_replace_surface(s
->con
, surface
);
1589 qemu_console_resize(s
->con
, disp_width
, height
);
1590 surface
= qemu_console_surface(s
->con
);
1594 if (shift_control
== 0) {
1595 full_update
|= update_palette16(s
);
1596 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1597 v
= VGA_DRAW_LINE4D2
;
1602 } else if (shift_control
== 1) {
1603 full_update
|= update_palette16(s
);
1604 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1605 v
= VGA_DRAW_LINE2D2
;
1611 switch(s
->get_bpp(s
)) {
1614 full_update
|= update_palette256(s
);
1615 v
= VGA_DRAW_LINE8D2
;
1619 full_update
|= update_palette256(s
);
1624 v
= s
->big_endian_fb
? VGA_DRAW_LINE15_BE
: VGA_DRAW_LINE15_LE
;
1628 v
= s
->big_endian_fb
? VGA_DRAW_LINE16_BE
: VGA_DRAW_LINE16_LE
;
1632 v
= s
->big_endian_fb
? VGA_DRAW_LINE24_BE
: VGA_DRAW_LINE24_LE
;
1636 v
= s
->big_endian_fb
? VGA_DRAW_LINE32_BE
: VGA_DRAW_LINE32_LE
;
1641 vga_draw_line
= vga_draw_line_table
[v
];
1643 if (!is_buffer_shared(surface
) && s
->cursor_invalidate
) {
1644 s
->cursor_invalidate(s
);
1648 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",
1649 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1650 s
->line_compare
, sr(s
, VGA_SEQ_CLOCK_MODE
));
1652 addr1
= (s
->start_addr
* 4);
1653 bwidth
= DIV_ROUND_UP(width
* bits
, 8);
1655 d
= surface_data(surface
);
1656 linesize
= surface_stride(surface
);
1660 if (s
->line_compare
< height
) {
1661 /* split screen mode */
1664 snap
= memory_region_snapshot_and_clear_dirty(&s
->vram
, region_start
,
1665 region_end
- region_start
,
1669 for(y
= 0; y
< height
; y
++) {
1671 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1673 /* CGA compatibility handling */
1674 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1675 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1677 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1678 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1680 page0
= addr
& s
->vbe_size_mask
;
1681 page1
= (addr
+ bwidth
- 1) & s
->vbe_size_mask
;
1684 } else if (page1
< page0
) {
1685 /* scanline wraps from end of video memory to the start */
1686 assert(force_shadow
);
1687 update
= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1688 page0
, s
->vbe_size
- page0
);
1689 update
|= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1692 update
= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1693 page0
, page1
- page0
);
1695 /* explicit invalidation for the hardware cursor (cirrus only) */
1696 update
|= vga_scanline_invalidated(s
, y
);
1700 if (!(is_buffer_shared(surface
))) {
1701 vga_draw_line(s
, d
, addr
, width
);
1702 if (s
->cursor_draw_line
)
1703 s
->cursor_draw_line(s
, d
, y
);
1707 /* flush to display */
1708 dpy_gfx_update(s
->con
, 0, y_start
,
1709 disp_width
, y
- y_start
);
1714 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1715 if ((y1
& mask
) == mask
)
1716 addr1
+= s
->line_offset
;
1718 multi_run
= multi_scan
;
1722 /* line compare acts on the displayed lines */
1723 if (y
== s
->line_compare
)
1728 /* flush to display */
1729 dpy_gfx_update(s
->con
, 0, y_start
,
1730 disp_width
, y
- y_start
);
1733 memset(s
->invalidated_y_table
, 0, sizeof(s
->invalidated_y_table
));
1736 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1738 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1744 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1747 w
= s
->last_scr_width
* surface_bytes_per_pixel(surface
);
1748 d
= surface_data(surface
);
1749 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1751 d
+= surface_stride(surface
);
1753 dpy_gfx_update_full(s
->con
);
1756 #define GMODE_TEXT 0
1757 #define GMODE_GRAPH 1
1758 #define GMODE_BLANK 2
1760 static void vga_update_display(void *opaque
)
1762 VGACommonState
*s
= opaque
;
1763 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1764 int full_update
, graphic_mode
;
1766 qemu_flush_coalesced_mmio_buffer();
1768 if (surface_bits_per_pixel(surface
) == 0) {
1772 if (!(s
->ar_index
& 0x20)) {
1773 graphic_mode
= GMODE_BLANK
;
1775 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1777 if (graphic_mode
!= s
->graphic_mode
) {
1778 s
->graphic_mode
= graphic_mode
;
1779 s
->cursor_blink_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1782 switch(graphic_mode
) {
1784 vga_draw_text(s
, full_update
);
1787 vga_draw_graphic(s
, full_update
);
1791 vga_draw_blank(s
, full_update
);
1797 /* force a full display refresh */
1798 static void vga_invalidate_display(void *opaque
)
1800 VGACommonState
*s
= opaque
;
1803 s
->last_height
= -1;
1806 void vga_common_reset(VGACommonState
*s
)
1809 memset(s
->sr
, '\0', sizeof(s
->sr
));
1810 memset(s
->sr_vbe
, '\0', sizeof(s
->sr_vbe
));
1812 memset(s
->gr
, '\0', sizeof(s
->gr
));
1814 memset(s
->ar
, '\0', sizeof(s
->ar
));
1815 s
->ar_flip_flop
= 0;
1817 memset(s
->cr
, '\0', sizeof(s
->cr
));
1823 s
->dac_sub_index
= 0;
1824 s
->dac_read_index
= 0;
1825 s
->dac_write_index
= 0;
1826 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1828 memset(s
->palette
, '\0', sizeof(s
->palette
));
1831 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1832 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1833 s
->vbe_start_addr
= 0;
1834 s
->vbe_line_offset
= 0;
1835 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1836 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1837 s
->graphic_mode
= -1; /* force full update */
1838 s
->shift_control
= 0;
1841 s
->line_compare
= 0;
1843 s
->plane_updated
= 0;
1848 s
->last_scr_width
= 0;
1849 s
->last_scr_height
= 0;
1850 s
->cursor_start
= 0;
1852 s
->cursor_offset
= 0;
1853 s
->big_endian_fb
= s
->default_endian_fb
;
1854 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1855 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1856 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1857 switch (vga_retrace_method
) {
1858 case VGA_RETRACE_DUMB
:
1860 case VGA_RETRACE_PRECISE
:
1861 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1864 vga_update_memory_access(s
);
1867 static void vga_reset(void *opaque
)
1869 VGACommonState
*s
= opaque
;
1870 vga_common_reset(s
);
1873 #define TEXTMODE_X(x) ((x) % width)
1874 #define TEXTMODE_Y(x) ((x) / width)
1875 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1876 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1877 /* relay text rendering to the display driver
1878 * instead of doing a full vga_update_display() */
1879 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
1881 VGACommonState
*s
= opaque
;
1882 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
1883 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
1885 console_ch_t
*dst
, val
;
1886 char msg_buffer
[80];
1887 int full_update
= 0;
1889 qemu_flush_coalesced_mmio_buffer();
1891 if (!(s
->ar_index
& 0x20)) {
1892 graphic_mode
= GMODE_BLANK
;
1894 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1896 if (graphic_mode
!= s
->graphic_mode
) {
1897 s
->graphic_mode
= graphic_mode
;
1900 if (s
->last_width
== -1) {
1905 switch (graphic_mode
) {
1907 /* TODO: update palette */
1908 full_update
|= update_basic_params(s
);
1910 /* total width & height */
1911 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1913 if (!(sr(s
, VGA_SEQ_CLOCK_MODE
) & VGA_SR01_CHAR_CLK_8DOTS
)) {
1916 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 0x08) {
1917 cw
= 16; /* NOTE: no 18 pixel wide */
1919 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1920 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1921 /* ugly hack for CGA 160x100x16 - explain me the logic */
1924 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1925 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1926 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1927 height
= (height
+ 1) / cheight
;
1930 size
= (height
* width
);
1931 if (size
> CH_ATTR_SIZE
) {
1935 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
1940 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1941 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
1942 s
->last_scr_width
= width
* cw
;
1943 s
->last_scr_height
= height
* cheight
;
1944 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1945 dpy_text_resize(s
->con
, width
, height
);
1947 s
->last_width
= width
;
1948 s
->last_height
= height
;
1949 s
->last_ch
= cheight
;
1955 s
->full_update_gfx
= 1;
1957 if (s
->full_update_text
) {
1958 s
->full_update_text
= 0;
1962 /* Update "hardware" cursor */
1963 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1964 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1965 if (cursor_offset
!= s
->cursor_offset
||
1966 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1967 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
1968 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
1969 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
1970 dpy_text_cursor(s
->con
,
1971 TEXTMODE_X(cursor_offset
),
1972 TEXTMODE_Y(cursor_offset
));
1974 dpy_text_cursor(s
->con
, -1, -1);
1975 s
->cursor_offset
= cursor_offset
;
1976 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1977 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1980 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
1984 for (i
= 0; i
< size
; src
++, dst
++, i
++)
1985 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
1987 dpy_text_update(s
->con
, 0, 0, width
, height
);
1991 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
1992 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2000 for (; i
< size
; src
++, dst
++, i
++) {
2001 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2008 if (c_min
<= c_max
) {
2009 i
= TEXTMODE_Y(c_min
);
2010 dpy_text_update(s
->con
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2019 s
->get_resolution(s
, &width
, &height
);
2020 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2028 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2032 /* Display a message */
2034 s
->last_height
= height
= 3;
2035 dpy_text_cursor(s
->con
, -1, -1);
2036 dpy_text_resize(s
->con
, s
->last_width
, height
);
2038 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2039 console_write_ch(dst
++, ' ');
2041 size
= strlen(msg_buffer
);
2042 width
= (s
->last_width
- size
) / 2;
2043 dst
= chardata
+ s
->last_width
+ width
;
2044 for (i
= 0; i
< size
; i
++)
2045 console_write_ch(dst
++, ATTR2CHTYPE(msg_buffer
[i
], QEMU_COLOR_BLUE
,
2046 QEMU_COLOR_BLACK
, 1));
2048 dpy_text_update(s
->con
, 0, 0, s
->last_width
, height
);
2051 static uint64_t vga_mem_read(void *opaque
, hwaddr addr
,
2054 VGACommonState
*s
= opaque
;
2056 return vga_mem_readb(s
, addr
);
2059 static void vga_mem_write(void *opaque
, hwaddr addr
,
2060 uint64_t data
, unsigned size
)
2062 VGACommonState
*s
= opaque
;
2064 vga_mem_writeb(s
, addr
, data
);
2067 const MemoryRegionOps vga_mem_ops
= {
2068 .read
= vga_mem_read
,
2069 .write
= vga_mem_write
,
2070 .endianness
= DEVICE_LITTLE_ENDIAN
,
2072 .min_access_size
= 1,
2073 .max_access_size
= 1,
2077 static int vga_common_post_load(void *opaque
, int version_id
)
2079 VGACommonState
*s
= opaque
;
2082 s
->graphic_mode
= -1;
2083 vbe_update_vgaregs(s
);
2084 vga_update_memory_access(s
);
2088 static bool vga_endian_state_needed(void *opaque
)
2090 VGACommonState
*s
= opaque
;
2093 * Only send the endian state if it's different from the
2094 * default one, thus ensuring backward compatibility for
2095 * migration of the common case
2097 return s
->default_endian_fb
!= s
->big_endian_fb
;
2100 static const VMStateDescription vmstate_vga_endian
= {
2101 .name
= "vga.endian",
2103 .minimum_version_id
= 1,
2104 .needed
= vga_endian_state_needed
,
2105 .fields
= (VMStateField
[]) {
2106 VMSTATE_BOOL(big_endian_fb
, VGACommonState
),
2107 VMSTATE_END_OF_LIST()
2111 const VMStateDescription vmstate_vga_common
= {
2114 .minimum_version_id
= 2,
2115 .post_load
= vga_common_post_load
,
2116 .fields
= (VMStateField
[]) {
2117 VMSTATE_UINT32(latch
, VGACommonState
),
2118 VMSTATE_UINT8(sr_index
, VGACommonState
),
2119 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2120 VMSTATE_UINT8(gr_index
, VGACommonState
),
2121 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2122 VMSTATE_UINT8(ar_index
, VGACommonState
),
2123 VMSTATE_BUFFER(ar
, VGACommonState
),
2124 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2125 VMSTATE_UINT8(cr_index
, VGACommonState
),
2126 VMSTATE_BUFFER(cr
, VGACommonState
),
2127 VMSTATE_UINT8(msr
, VGACommonState
),
2128 VMSTATE_UINT8(fcr
, VGACommonState
),
2129 VMSTATE_UINT8(st00
, VGACommonState
),
2130 VMSTATE_UINT8(st01
, VGACommonState
),
2132 VMSTATE_UINT8(dac_state
, VGACommonState
),
2133 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2134 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2135 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2136 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2137 VMSTATE_BUFFER(palette
, VGACommonState
),
2139 VMSTATE_INT32(bank_offset
, VGACommonState
),
2140 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
, NULL
),
2141 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2142 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2143 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2144 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2145 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2146 VMSTATE_END_OF_LIST()
2148 .subsections
= (const VMStateDescription
*[]) {
2149 &vmstate_vga_endian
,
2154 static const GraphicHwOps vga_ops
= {
2155 .invalidate
= vga_invalidate_display
,
2156 .gfx_update
= vga_update_display
,
2157 .text_update
= vga_update_text
,
2160 static inline uint32_t uint_clamp(uint32_t val
, uint32_t vmin
, uint32_t vmax
)
2171 void vga_common_init(VGACommonState
*s
, Object
*obj
)
2175 for(i
= 0;i
< 256; i
++) {
2177 for(j
= 0; j
< 8; j
++) {
2178 v
|= ((i
>> j
) & 1) << (j
* 4);
2183 for(j
= 0; j
< 4; j
++) {
2184 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2188 for(i
= 0; i
< 16; i
++) {
2190 for(j
= 0; j
< 4; j
++) {
2193 v
|= b
<< (2 * j
+ 1);
2198 s
->vram_size_mb
= uint_clamp(s
->vram_size_mb
, 1, 512);
2199 s
->vram_size_mb
= pow2ceil(s
->vram_size_mb
);
2200 s
->vram_size
= s
->vram_size_mb
* MiB
;
2203 s
->vbe_size
= s
->vram_size
;
2205 s
->vbe_size_mask
= s
->vbe_size
- 1;
2207 s
->is_vbe_vmstate
= 1;
2208 memory_region_init_ram_nomigrate(&s
->vram
, obj
, "vga.vram", s
->vram_size
,
2210 vmstate_register_ram(&s
->vram
, s
->global_vmstate
? NULL
: DEVICE(obj
));
2211 xen_register_framebuffer(&s
->vram
);
2212 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2213 s
->get_bpp
= vga_get_bpp
;
2214 s
->get_offsets
= vga_get_offsets
;
2215 s
->get_resolution
= vga_get_resolution
;
2216 s
->hw_ops
= &vga_ops
;
2217 switch (vga_retrace_method
) {
2218 case VGA_RETRACE_DUMB
:
2219 s
->retrace
= vga_dumb_retrace
;
2220 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2223 case VGA_RETRACE_PRECISE
:
2224 s
->retrace
= vga_precise_retrace
;
2225 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2230 * Set default fb endian based on target, could probably be turned
2231 * into a device attribute set by the machine/platform to remove
2232 * all target endian dependencies from this file.
2234 #ifdef TARGET_WORDS_BIGENDIAN
2235 s
->default_endian_fb
= true;
2237 s
->default_endian_fb
= false;
2239 vga_dirty_log_start(s
);
2242 static const MemoryRegionPortio vga_portio_list
[] = {
2243 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2244 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2245 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2246 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2247 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2248 PORTIO_END_OF_LIST(),
2251 static const MemoryRegionPortio vbe_portio_list
[] = {
2252 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2254 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2256 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2257 PORTIO_END_OF_LIST(),
2260 /* Used by both ISA and PCI */
2261 MemoryRegion
*vga_init_io(VGACommonState
*s
, Object
*obj
,
2262 const MemoryRegionPortio
**vga_ports
,
2263 const MemoryRegionPortio
**vbe_ports
)
2265 MemoryRegion
*vga_mem
;
2267 *vga_ports
= vga_portio_list
;
2268 *vbe_ports
= vbe_portio_list
;
2270 vga_mem
= g_malloc(sizeof(*vga_mem
));
2271 memory_region_init_io(vga_mem
, obj
, &vga_mem_ops
, s
,
2272 "vga-lowmem", 0x20000);
2273 memory_region_set_flush_coalesced(vga_mem
);
2278 void vga_init(VGACommonState
*s
, Object
*obj
, MemoryRegion
*address_space
,
2279 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2281 MemoryRegion
*vga_io_memory
;
2282 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2284 qemu_register_reset(vga_reset
, s
);
2288 s
->legacy_address_space
= address_space
;
2290 vga_io_memory
= vga_init_io(s
, obj
, &vga_ports
, &vbe_ports
);
2291 memory_region_add_subregion_overlap(address_space
,
2295 memory_region_set_coalescing(vga_io_memory
);
2296 if (init_vga_ports
) {
2297 portio_list_init(&s
->vga_port_list
, obj
, vga_ports
, s
, "vga");
2298 portio_list_set_flush_coalesced(&s
->vga_port_list
);
2299 portio_list_add(&s
->vga_port_list
, address_space_io
, 0x3b0);
2302 portio_list_init(&s
->vbe_port_list
, obj
, vbe_ports
, s
, "vbe");
2303 portio_list_add(&s
->vbe_port_list
, address_space_io
, 0x1ce);