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/core/cpu.h"
30 #include "hw/display/vga.h"
31 #include "hw/i386/x86.h"
32 #include "hw/pci/pci.h"
35 #include "ui/pixel_ops.h"
36 #include "ui/console.h"
37 #include "qemu/timer.h"
38 #include "hw/xen/xen.h"
39 #include "migration/vmstate.h"
42 //#define DEBUG_VGA_MEM
43 //#define DEBUG_VGA_REG
47 /* 16 state changes per vertical frame @60 Hz */
48 #define VGA_TEXT_CURSOR_PERIOD_MS (1000 * 2 * 16 / 60)
50 /* Address mask for non-VESA modes. */
51 #define VGA_VRAM_SIZE (256 * KiB)
53 /* This value corresponds to a shift of zero pixels
54 * in 9-dot text mode. In other modes, bit 3 is undefined;
55 * we just ignore it, so that 8 corresponds to zero pixels
58 #define VGA_HPEL_NEUTRAL 8
61 * Video Graphics Array (VGA)
63 * Chipset docs for original IBM VGA:
64 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
67 * http://www.osdever.net/FreeVGA/home.htm
69 * Standard VGA features and Bochs VBE extensions are implemented.
72 /* force some bits to zero */
73 const uint8_t sr_mask
[8] = {
84 const uint8_t gr_mask
[16] = {
103 #define GET_PLANE(data, p) ((cpu_to_le32(data) >> ((p) * 8)) & 0xff)
105 static const uint32_t mask16
[16] = {
106 const_le32(0x00000000),
107 const_le32(0x000000ff),
108 const_le32(0x0000ff00),
109 const_le32(0x0000ffff),
110 const_le32(0x00ff0000),
111 const_le32(0x00ff00ff),
112 const_le32(0x00ffff00),
113 const_le32(0x00ffffff),
114 const_le32(0xff000000),
115 const_le32(0xff0000ff),
116 const_le32(0xff00ff00),
117 const_le32(0xff00ffff),
118 const_le32(0xffff0000),
119 const_le32(0xffff00ff),
120 const_le32(0xffffff00),
121 const_le32(0xffffffff),
124 static uint32_t expand4
[256];
125 static uint16_t expand2
[256];
126 static uint8_t expand4to8
[16];
128 static void vbe_update_vgaregs(VGACommonState
*s
);
130 static inline bool vbe_enabled(VGACommonState
*s
)
132 return s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
;
135 static inline uint8_t sr(VGACommonState
*s
, int idx
)
137 return vbe_enabled(s
) ? s
->sr_vbe
[idx
] : s
->sr
[idx
];
140 static void vga_update_memory_access(VGACommonState
*s
)
142 hwaddr base
, offset
, size
;
144 if (s
->legacy_address_space
== NULL
) {
148 if (s
->has_chain4_alias
) {
149 memory_region_del_subregion(s
->legacy_address_space
, &s
->chain4_alias
);
150 object_unparent(OBJECT(&s
->chain4_alias
));
151 s
->has_chain4_alias
= false;
152 s
->plane_updated
= 0xf;
154 if ((sr(s
, VGA_SEQ_PLANE_WRITE
) & VGA_SR02_ALL_PLANES
) ==
155 VGA_SR02_ALL_PLANES
&& sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
157 switch ((s
->gr
[VGA_GFX_MISC
] >> 2) & 3) {
165 offset
= s
->bank_offset
;
177 assert(offset
+ size
<= s
->vram_size
);
178 memory_region_init_alias(&s
->chain4_alias
, memory_region_owner(&s
->vram
),
179 "vga.chain4", &s
->vram
, offset
, size
);
180 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
181 &s
->chain4_alias
, 2);
182 s
->has_chain4_alias
= true;
186 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
191 static void vga_precise_update_retrace_info(VGACommonState
*s
)
194 int hretr_start_char
;
195 int hretr_skew_chars
;
199 int vretr_start_line
;
208 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
209 int64_t chars_per_sec
;
210 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
212 htotal_chars
= s
->cr
[VGA_CRTC_H_TOTAL
] + 5;
213 hretr_start_char
= s
->cr
[VGA_CRTC_H_SYNC_START
];
214 hretr_skew_chars
= (s
->cr
[VGA_CRTC_H_SYNC_END
] >> 5) & 3;
215 hretr_end_char
= s
->cr
[VGA_CRTC_H_SYNC_END
] & 0x1f;
217 vtotal_lines
= (s
->cr
[VGA_CRTC_V_TOTAL
] |
218 (((s
->cr
[VGA_CRTC_OVERFLOW
] & 1) |
219 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 4) & 2)) << 8)) + 2;
220 vretr_start_line
= s
->cr
[VGA_CRTC_V_SYNC_START
] |
221 ((((s
->cr
[VGA_CRTC_OVERFLOW
] >> 2) & 1) |
222 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 6) & 2)) << 8);
223 vretr_end_line
= s
->cr
[VGA_CRTC_V_SYNC_END
] & 0xf;
225 clocking_mode
= (sr(s
, VGA_SEQ_CLOCK_MODE
) >> 3) & 1;
226 clock_sel
= (s
->msr
>> 2) & 3;
227 dots
= (s
->msr
& 1) ? 8 : 9;
229 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
231 htotal_chars
<<= clocking_mode
;
233 r
->total_chars
= vtotal_lines
* htotal_chars
;
235 r
->ticks_per_char
= NANOSECONDS_PER_SECOND
/ (r
->total_chars
* r
->freq
);
237 r
->ticks_per_char
= NANOSECONDS_PER_SECOND
/ chars_per_sec
;
240 r
->vstart
= vretr_start_line
;
241 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
243 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
244 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
245 r
->htotal
= htotal_chars
;
248 div2
= (s
->cr
[VGA_CRTC_MODE
] >> 2) & 1;
249 sldiv2
= (s
->cr
[VGA_CRTC_MODE
] >> 3) & 1;
259 "div2 = %d sldiv2 = %d\n"
260 "clocking_mode = %d\n"
261 "clock_sel = %d %d\n"
263 "ticks/char = %" PRId64
"\n"
265 (double) NANOSECONDS_PER_SECOND
/ (r
->ticks_per_char
* r
->total_chars
),
283 static uint8_t vga_precise_retrace(VGACommonState
*s
)
285 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
286 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
288 if (r
->total_chars
) {
289 int cur_line
, cur_line_char
, cur_char
;
292 cur_tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
294 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
295 cur_line
= cur_char
/ r
->htotal
;
297 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
298 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
300 cur_line_char
= cur_char
% r
->htotal
;
301 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
302 val
|= ST01_DISP_ENABLE
;
308 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
312 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
314 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
317 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
319 if (s
->msr
& VGA_MIS_COLOR
) {
321 return (addr
>= 0x3b0 && addr
<= 0x3bf);
324 return (addr
>= 0x3d0 && addr
<= 0x3df);
328 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
330 VGACommonState
*s
= opaque
;
333 if (vga_ioport_invalid(s
, addr
)) {
338 if (s
->ar_flip_flop
== 0) {
345 index
= s
->ar_index
& 0x1f;
346 if (index
< VGA_ATT_C
) {
359 val
= s
->sr
[s
->sr_index
];
361 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
368 val
= s
->dac_write_index
;
371 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
372 if (++s
->dac_sub_index
== 3) {
373 s
->dac_sub_index
= 0;
387 val
= s
->gr
[s
->gr_index
];
389 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
398 val
= s
->cr
[s
->cr_index
];
400 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
405 /* just toggle to fool polling */
406 val
= s
->st01
= s
->retrace(s
);
414 trace_vga_std_read_io(addr
, val
);
418 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
420 VGACommonState
*s
= opaque
;
423 /* check port range access depending on color/monochrome mode */
424 if (vga_ioport_invalid(s
, addr
)) {
427 trace_vga_std_write_io(addr
, val
);
431 if (s
->ar_flip_flop
== 0) {
435 index
= s
->ar_index
& 0x1f;
437 case VGA_ATC_PALETTE0
... VGA_ATC_PALETTEF
:
438 s
->ar
[index
] = val
& 0x3f;
441 s
->ar
[index
] = val
& ~0x10;
443 case VGA_ATC_OVERSCAN
:
446 case VGA_ATC_PLANE_ENABLE
:
447 s
->ar
[index
] = val
& ~0xc0;
450 s
->ar
[index
] = val
& ~0xf0;
452 case VGA_ATC_COLOR_PAGE
:
453 s
->ar
[index
] = val
& ~0xf0;
459 s
->ar_flip_flop
^= 1;
462 s
->msr
= val
& ~0x10;
463 s
->update_retrace_info(s
);
466 s
->sr_index
= val
& 7;
470 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
472 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
473 if (s
->sr_index
== VGA_SEQ_CLOCK_MODE
) {
474 s
->update_retrace_info(s
);
476 vga_update_memory_access(s
);
479 s
->dac_read_index
= val
;
480 s
->dac_sub_index
= 0;
484 s
->dac_write_index
= val
;
485 s
->dac_sub_index
= 0;
489 s
->dac_cache
[s
->dac_sub_index
] = val
;
490 if (++s
->dac_sub_index
== 3) {
491 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
492 s
->dac_sub_index
= 0;
493 s
->dac_write_index
++;
497 s
->gr_index
= val
& 0x0f;
501 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
503 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
504 vbe_update_vgaregs(s
);
505 vga_update_memory_access(s
);
514 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
516 /* handle CR0-7 protection */
517 if ((s
->cr
[VGA_CRTC_V_SYNC_END
] & VGA_CR11_LOCK_CR0_CR7
) &&
518 s
->cr_index
<= VGA_CRTC_OVERFLOW
) {
519 /* can always write bit 4 of CR7 */
520 if (s
->cr_index
== VGA_CRTC_OVERFLOW
) {
521 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x10) |
523 vbe_update_vgaregs(s
);
527 s
->cr
[s
->cr_index
] = val
;
528 vbe_update_vgaregs(s
);
530 switch(s
->cr_index
) {
531 case VGA_CRTC_H_TOTAL
:
532 case VGA_CRTC_H_SYNC_START
:
533 case VGA_CRTC_H_SYNC_END
:
534 case VGA_CRTC_V_TOTAL
:
535 case VGA_CRTC_OVERFLOW
:
536 case VGA_CRTC_V_SYNC_END
:
538 s
->update_retrace_info(s
);
550 * Sanity check vbe register writes.
552 * As we don't have a way to signal errors to the guest in the bochs
553 * dispi interface we'll go adjust the registers to the closest valid
556 static void vbe_fixup_regs(VGACommonState
*s
)
558 uint16_t *r
= s
->vbe_regs
;
559 uint32_t bits
, linelength
, maxy
, offset
;
561 if (!vbe_enabled(s
)) {
562 /* vbe is turned off -- nothing to do */
567 switch (r
[VBE_DISPI_INDEX_BPP
]) {
573 bits
= r
[VBE_DISPI_INDEX_BPP
];
579 bits
= r
[VBE_DISPI_INDEX_BPP
] = 8;
584 r
[VBE_DISPI_INDEX_XRES
] &= ~7u;
585 if (r
[VBE_DISPI_INDEX_XRES
] == 0) {
586 r
[VBE_DISPI_INDEX_XRES
] = 8;
588 if (r
[VBE_DISPI_INDEX_XRES
] > VBE_DISPI_MAX_XRES
) {
589 r
[VBE_DISPI_INDEX_XRES
] = VBE_DISPI_MAX_XRES
;
591 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] &= ~7u;
592 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] > VBE_DISPI_MAX_XRES
) {
593 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = VBE_DISPI_MAX_XRES
;
595 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] < r
[VBE_DISPI_INDEX_XRES
]) {
596 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = r
[VBE_DISPI_INDEX_XRES
];
600 linelength
= r
[VBE_DISPI_INDEX_VIRT_WIDTH
] * bits
/ 8;
601 maxy
= s
->vbe_size
/ linelength
;
602 if (r
[VBE_DISPI_INDEX_YRES
] == 0) {
603 r
[VBE_DISPI_INDEX_YRES
] = 1;
605 if (r
[VBE_DISPI_INDEX_YRES
] > VBE_DISPI_MAX_YRES
) {
606 r
[VBE_DISPI_INDEX_YRES
] = VBE_DISPI_MAX_YRES
;
608 if (r
[VBE_DISPI_INDEX_YRES
] > maxy
) {
609 r
[VBE_DISPI_INDEX_YRES
] = maxy
;
613 if (r
[VBE_DISPI_INDEX_X_OFFSET
] > VBE_DISPI_MAX_XRES
) {
614 r
[VBE_DISPI_INDEX_X_OFFSET
] = VBE_DISPI_MAX_XRES
;
616 if (r
[VBE_DISPI_INDEX_Y_OFFSET
] > VBE_DISPI_MAX_YRES
) {
617 r
[VBE_DISPI_INDEX_Y_OFFSET
] = VBE_DISPI_MAX_YRES
;
619 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
620 offset
+= r
[VBE_DISPI_INDEX_Y_OFFSET
] * linelength
;
621 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
622 r
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
623 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
624 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
625 r
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
630 /* update vga state */
631 r
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = maxy
;
632 s
->vbe_line_offset
= linelength
;
633 s
->vbe_start_addr
= offset
/ 4;
636 /* we initialize the VGA graphic mode */
637 static void vbe_update_vgaregs(VGACommonState
*s
)
639 int h
, shift_control
;
641 if (!vbe_enabled(s
)) {
642 /* vbe is turned off -- nothing to do */
646 /* graphic mode + memory map 1 */
647 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
648 VGA_GR06_GRAPHICS_MODE
;
649 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
650 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
652 s
->cr
[VGA_CRTC_H_DISP
] =
653 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
654 /* height (only meaningful if < 1024) */
655 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
656 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
657 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
658 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
659 /* line compare to 1023 */
660 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
661 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
662 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
664 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
666 s
->sr_vbe
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
669 /* set chain 4 mode */
670 s
->sr_vbe
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
671 /* activate all planes */
672 s
->sr_vbe
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
674 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
675 (shift_control
<< 5);
676 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
679 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
681 VGACommonState
*s
= opaque
;
685 uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
687 VGACommonState
*s
= opaque
;
690 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
691 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
692 switch(s
->vbe_index
) {
693 /* XXX: do not hardcode ? */
694 case VBE_DISPI_INDEX_XRES
:
695 val
= VBE_DISPI_MAX_XRES
;
697 case VBE_DISPI_INDEX_YRES
:
698 val
= VBE_DISPI_MAX_YRES
;
700 case VBE_DISPI_INDEX_BPP
:
701 val
= VBE_DISPI_MAX_BPP
;
704 val
= s
->vbe_regs
[s
->vbe_index
];
708 val
= s
->vbe_regs
[s
->vbe_index
];
710 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
711 val
= s
->vbe_size
/ (64 * KiB
);
715 trace_vga_vbe_read(s
->vbe_index
, val
);
719 void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
721 VGACommonState
*s
= opaque
;
725 void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
727 VGACommonState
*s
= opaque
;
729 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
730 trace_vga_vbe_write(s
->vbe_index
, val
);
731 switch(s
->vbe_index
) {
732 case VBE_DISPI_INDEX_ID
:
733 if (val
== VBE_DISPI_ID0
||
734 val
== VBE_DISPI_ID1
||
735 val
== VBE_DISPI_ID2
||
736 val
== VBE_DISPI_ID3
||
737 val
== VBE_DISPI_ID4
||
738 val
== VBE_DISPI_ID5
) {
739 s
->vbe_regs
[s
->vbe_index
] = val
;
742 case VBE_DISPI_INDEX_XRES
:
743 case VBE_DISPI_INDEX_YRES
:
744 case VBE_DISPI_INDEX_BPP
:
745 case VBE_DISPI_INDEX_VIRT_WIDTH
:
746 case VBE_DISPI_INDEX_X_OFFSET
:
747 case VBE_DISPI_INDEX_Y_OFFSET
:
748 s
->vbe_regs
[s
->vbe_index
] = val
;
750 vbe_update_vgaregs(s
);
752 case VBE_DISPI_INDEX_BANK
:
753 val
&= s
->vbe_bank_mask
;
754 s
->vbe_regs
[s
->vbe_index
] = val
;
755 s
->bank_offset
= (val
<< 16);
756 vga_update_memory_access(s
);
758 case VBE_DISPI_INDEX_ENABLE
:
759 if ((val
& VBE_DISPI_ENABLED
) &&
760 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
762 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = 0;
763 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
764 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
765 s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] |= VBE_DISPI_ENABLED
;
767 vbe_update_vgaregs(s
);
769 /* clear the screen */
770 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
771 memset(s
->vram_ptr
, 0,
772 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
777 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
778 s
->vbe_regs
[s
->vbe_index
] = val
;
779 vga_update_memory_access(s
);
787 /* called for accesses between 0xa0000 and 0xc0000 */
788 uint32_t vga_mem_readb(VGACommonState
*s
, hwaddr addr
)
790 int memory_map_mode
, plane
;
793 /* convert to VGA memory offset */
794 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
796 switch(memory_map_mode
) {
802 addr
+= s
->bank_offset
;
817 if (sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
821 } else if (s
->gr
[VGA_GFX_MODE
] & VGA_GR05_HOST_ODD_EVEN
) {
822 /* odd/even mode (aka text mode mapping) */
823 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
825 /* standard VGA latched access */
826 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
829 if (s
->gr
[VGA_GFX_MISC
] & VGA_GR06_CHAIN_ODD_EVEN
) {
833 /* Doubleword/word mode. See comment in vga_mem_writeb */
834 if (s
->cr
[VGA_CRTC_UNDERLINE
] & VGA_CR14_DW
) {
836 } else if ((s
->gr
[VGA_GFX_MODE
] & VGA_GR05_HOST_ODD_EVEN
) &&
837 (s
->cr
[VGA_CRTC_MODE
] & VGA_CR17_WORD_BYTE
) == 0) {
841 if (addr
* sizeof(uint32_t) >= s
->vram_size
) {
845 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
846 /* chain 4 mode: simplified access (but it should use the same
847 * algorithms as below, see e.g. vga_mem_writeb's plane mask check).
849 return s
->vram_ptr
[(addr
<< 2) | plane
];
852 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
853 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
855 ret
= GET_PLANE(s
->latch
, plane
);
858 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
859 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
868 /* called for accesses between 0xa0000 and 0xc0000 */
869 void vga_mem_writeb(VGACommonState
*s
, hwaddr addr
, uint32_t val
)
871 int memory_map_mode
, write_mode
, b
, func_select
, mask
;
872 uint32_t write_mask
, bit_mask
, set_mask
;
876 printf("vga: [0x" HWADDR_FMT_plx
"] = 0x%02x\n", addr
, val
);
878 /* convert to VGA memory offset */
879 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
881 switch(memory_map_mode
) {
887 addr
+= s
->bank_offset
;
902 mask
= sr(s
, VGA_SEQ_PLANE_WRITE
);
903 if (sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
904 /* chain 4 mode : simplest access */
906 mask
&= (1 << plane
);
909 if ((sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_SEQ_MODE
) == 0) {
910 mask
&= (addr
& 1) ? 0x0a : 0x05;
912 if (s
->gr
[VGA_GFX_MISC
] & VGA_GR06_CHAIN_ODD_EVEN
) {
917 /* Doubleword/word mode. These should be honored when displaying,
918 * not when reading/writing to memory! For example, chain4 modes
919 * use double-word mode and, on real hardware, would fetch bytes
920 * 0,1,2,3, 16,17,18,19, 32,33,34,35, etc. Text modes use word
921 * mode and, on real hardware, would fetch bytes 0,1, 8,9, etc.
923 * QEMU instead shifted addresses on memory accesses because it
924 * allows more optimizations (e.g. chain4_alias) and simplifies
925 * the draw_line handlers. Unfortunately, there is one case where
926 * the difference shows. When fetching font data, accesses are
927 * always in consecutive bytes, even if the text/attribute pairs
928 * are done in word mode. Hence, doing a right shift when operating
929 * on font data is wrong. So check the odd/even mode bits together with
930 * word mode bit. The odd/even read bit is 0 when reading font data,
931 * and the odd/even write bit is 1 when writing it.
933 if (s
->cr
[VGA_CRTC_UNDERLINE
] & VGA_CR14_DW
) {
935 } else if ((sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_SEQ_MODE
) == 0 &&
936 (s
->cr
[VGA_CRTC_MODE
] & VGA_CR17_WORD_BYTE
) == 0) {
940 if (addr
* sizeof(uint32_t) >= s
->vram_size
) {
944 if (sr(s
, VGA_SEQ_MEMORY_MODE
) & VGA_SR04_CHN_4M
) {
946 s
->vram_ptr
[(addr
<< 2) | plane
] = val
;
948 printf("vga: chain4: [0x" HWADDR_FMT_plx
"]\n", addr
);
950 s
->plane_updated
|= mask
; /* only used to detect font change */
951 memory_region_set_dirty(&s
->vram
, addr
, 1);
956 /* standard VGA latched access */
957 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
962 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
963 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
967 /* apply set/reset mask */
968 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
969 val
= (val
& ~set_mask
) |
970 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
971 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
977 val
= mask16
[val
& 0x0f];
978 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
982 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
983 val
= (val
>> b
) | (val
<< (8 - b
));
985 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
986 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
990 /* apply logical operation */
991 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
992 switch(func_select
) {
1011 /* apply bit mask */
1012 bit_mask
|= bit_mask
<< 8;
1013 bit_mask
|= bit_mask
<< 16;
1014 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
1017 /* mask data according to sr[2] */
1018 s
->plane_updated
|= mask
; /* only used to detect font change */
1019 write_mask
= mask16
[mask
];
1020 ((uint32_t *)s
->vram_ptr
)[addr
] =
1021 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
1023 #ifdef DEBUG_VGA_MEM
1024 printf("vga: latch: [0x" HWADDR_FMT_plx
"] mask=0x%08x val=0x%08x\n",
1025 addr
* 4, write_mask
, val
);
1027 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
1030 typedef void *vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
1031 uint32_t srcaddr
, int width
, int hpel
);
1033 #include "vga-access.h"
1034 #include "vga-helpers.h"
1036 /* return true if the palette was modified */
1037 static int update_palette16(VGACommonState
*s
)
1040 uint32_t v
, col
, *palette
;
1043 palette
= s
->last_palette
;
1044 for(i
= 0; i
< 16; i
++) {
1046 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1047 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1049 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1052 col
= rgb_to_pixel32(c6_to_8(s
->palette
[v
]),
1053 c6_to_8(s
->palette
[v
+ 1]),
1054 c6_to_8(s
->palette
[v
+ 2]));
1055 if (col
!= palette
[i
]) {
1063 /* return true if the palette was modified */
1064 static int update_palette256(VGACommonState
*s
)
1067 uint32_t v
, col
, *palette
;
1070 palette
= s
->last_palette
;
1072 for(i
= 0; i
< 256; i
++) {
1074 col
= rgb_to_pixel32(s
->palette
[v
],
1078 col
= rgb_to_pixel32(c6_to_8(s
->palette
[v
]),
1079 c6_to_8(s
->palette
[v
+ 1]),
1080 c6_to_8(s
->palette
[v
+ 2]));
1082 if (col
!= palette
[i
]) {
1091 static void vga_get_params(VGACommonState
*s
,
1092 VGADisplayParams
*params
)
1094 if (vbe_enabled(s
)) {
1095 params
->line_offset
= s
->vbe_line_offset
;
1096 params
->start_addr
= s
->vbe_start_addr
;
1097 params
->line_compare
= 65535;
1098 params
->hpel
= VGA_HPEL_NEUTRAL
;
1099 params
->hpel_split
= false;
1101 /* compute line_offset in bytes */
1102 params
->line_offset
= s
->cr
[VGA_CRTC_OFFSET
] << 3;
1104 /* starting address */
1105 params
->start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1106 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1109 params
->line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1110 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1111 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1113 params
->hpel
= s
->ar
[VGA_ATC_PEL
];
1114 params
->hpel_split
= s
->ar
[VGA_ATC_MODE
] & 0x20;
1118 /* update start_addr and line_offset. Return TRUE if modified */
1119 static int update_basic_params(VGACommonState
*s
)
1122 VGADisplayParams current
;
1126 s
->get_params(s
, ¤t
);
1128 if (memcmp(¤t
, &s
->params
, sizeof(current
))) {
1129 s
->params
= current
;
1136 static const uint8_t cursor_glyph
[32 * 4] = {
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,
1142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1151 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1152 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1155 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1156 int *pcwidth
, int *pcheight
)
1158 int width
, cwidth
, height
, cheight
;
1160 /* total width & height */
1161 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1163 if (!(sr(s
, VGA_SEQ_CLOCK_MODE
) & VGA_SR01_CHAR_CLK_8DOTS
)) {
1166 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 0x08) {
1167 cwidth
= 16; /* NOTE: no 18 pixel wide */
1169 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1170 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1171 /* ugly hack for CGA 160x100x16 - explain me the logic */
1174 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1175 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1176 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1177 height
= (height
+ 1) / cheight
;
1183 *pcheight
= cheight
;
1194 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1196 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1197 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1198 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1199 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1200 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1201 const uint8_t *font_ptr
, *font_base
[2];
1202 int dup9
, line_offset
;
1204 uint32_t *ch_attr_ptr
;
1205 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1207 /* compute font data address (in plane 2) */
1208 v
= sr(s
, VGA_SEQ_CHARACTER_MAP
);
1209 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1210 if (offset
!= s
->font_offsets
[0]) {
1211 s
->font_offsets
[0] = offset
;
1214 font_base
[0] = s
->vram_ptr
+ offset
;
1216 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1217 font_base
[1] = s
->vram_ptr
+ offset
;
1218 if (offset
!= s
->font_offsets
[1]) {
1219 s
->font_offsets
[1] = offset
;
1222 if (s
->plane_updated
& (1 << 2) || s
->has_chain4_alias
) {
1223 /* if the plane 2 was modified since the last display, it
1224 indicates the font may have been modified */
1225 s
->plane_updated
= 0;
1228 full_update
|= update_basic_params(s
);
1230 line_offset
= s
->params
.line_offset
;
1232 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1233 if ((height
* width
) <= 1) {
1234 /* better than nothing: exit if transient size is too small */
1237 if ((height
* width
) > CH_ATTR_SIZE
) {
1238 /* better than nothing: exit if transient size is too big */
1242 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1243 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1244 s
->last_scr_width
= width
* cw
;
1245 s
->last_scr_height
= height
* cheight
;
1246 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1247 surface
= qemu_console_surface(s
->con
);
1248 dpy_text_resize(s
->con
, width
, height
);
1250 s
->last_width
= width
;
1251 s
->last_height
= height
;
1252 s
->last_ch
= cheight
;
1256 full_update
|= update_palette16(s
);
1257 palette
= s
->last_palette
;
1258 x_incr
= cw
* surface_bytes_per_pixel(surface
);
1261 s
->full_update_text
= 1;
1263 if (s
->full_update_gfx
) {
1264 s
->full_update_gfx
= 0;
1268 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1269 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->params
.start_addr
;
1270 if (cursor_offset
!= s
->cursor_offset
||
1271 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1272 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1273 /* if the cursor position changed, we update the old and new
1275 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1276 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1277 if (cursor_offset
< CH_ATTR_SIZE
)
1278 s
->last_ch_attr
[cursor_offset
] = -1;
1279 s
->cursor_offset
= cursor_offset
;
1280 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1281 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1283 cursor_ptr
= s
->vram_ptr
+ (s
->params
.start_addr
+ cursor_offset
) * 4;
1284 if (now
>= s
->cursor_blink_time
) {
1285 s
->cursor_blink_time
= now
+ VGA_TEXT_CURSOR_PERIOD_MS
/ 2;
1286 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1289 dest
= surface_data(surface
);
1290 linesize
= surface_stride(surface
);
1291 ch_attr_ptr
= s
->last_ch_attr
;
1293 offset
= s
->params
.start_addr
* 4;
1294 for(cy
= 0; cy
< height
; cy
++) {
1296 src
= s
->vram_ptr
+ offset
;
1299 for(cx
= 0; cx
< width
; cx
++) {
1300 if (src
+ sizeof(uint16_t) > s
->vram_ptr
+ s
->vram_size
) {
1303 ch_attr
= *(uint16_t *)src
;
1304 if (full_update
|| ch_attr
!= *ch_attr_ptr
|| src
== cursor_ptr
) {
1309 *ch_attr_ptr
= ch_attr
;
1312 cattr
= ch_attr
& 0xff;
1314 ch
= ch_attr
& 0xff;
1315 cattr
= ch_attr
>> 8;
1317 font_ptr
= font_base
[(cattr
>> 3) & 1];
1318 font_ptr
+= 32 * 4 * ch
;
1319 bgcol
= palette
[cattr
>> 4];
1320 fgcol
= palette
[cattr
& 0x0f];
1322 vga_draw_glyph16(d1
, linesize
,
1323 font_ptr
, cheight
, fgcol
, bgcol
);
1324 } else if (cw
!= 9) {
1325 vga_draw_glyph8(d1
, linesize
,
1326 font_ptr
, cheight
, fgcol
, bgcol
);
1329 if (ch
>= 0xb0 && ch
<= 0xdf &&
1330 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1333 vga_draw_glyph9(d1
, linesize
,
1334 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1336 if (src
== cursor_ptr
&&
1337 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20) &&
1338 s
->cursor_visible_phase
) {
1339 int line_start
, line_last
, h
;
1340 /* draw the cursor */
1341 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1342 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1343 /* XXX: check that */
1344 if (line_last
> cheight
- 1)
1345 line_last
= cheight
- 1;
1346 if (line_last
>= line_start
&& line_start
< cheight
) {
1347 h
= line_last
- line_start
+ 1;
1348 d
= d1
+ linesize
* line_start
;
1350 vga_draw_glyph16(d
, linesize
,
1351 cursor_glyph
, h
, fgcol
, bgcol
);
1352 } else if (cw
!= 9) {
1353 vga_draw_glyph8(d
, linesize
,
1354 cursor_glyph
, h
, fgcol
, bgcol
);
1356 vga_draw_glyph9(d
, linesize
,
1357 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1367 dpy_gfx_update(s
->con
, cx_min
* cw
, cy
* cheight
,
1368 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1370 dest
+= linesize
* cheight
;
1371 line1
= line
+ cheight
;
1372 offset
+= line_offset
;
1373 if (line
< s
->params
.line_compare
&& line1
>= s
->params
.line_compare
) {
1398 static vga_draw_line_func
* const vga_draw_line_table
[VGA_DRAW_LINE_NB
] = {
1415 static int vga_get_bpp(VGACommonState
*s
)
1419 if (vbe_enabled(s
)) {
1420 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1427 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1431 if (vbe_enabled(s
)) {
1432 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1433 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1435 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1436 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1437 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1438 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1439 height
= (height
+ 1);
1445 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1448 if (y1
>= VGA_MAX_HEIGHT
)
1450 if (y2
>= VGA_MAX_HEIGHT
)
1451 y2
= VGA_MAX_HEIGHT
;
1452 for(y
= y1
; y
< y2
; y
++) {
1453 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1457 static bool vga_scanline_invalidated(VGACommonState
*s
, int y
)
1459 if (y
>= VGA_MAX_HEIGHT
) {
1462 return s
->invalidated_y_table
[y
>> 5] & (1 << (y
& 0x1f));
1465 void vga_dirty_log_start(VGACommonState
*s
)
1467 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1470 void vga_dirty_log_stop(VGACommonState
*s
)
1472 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1478 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1480 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1481 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1482 int width
, height
, shift_control
, bwidth
, bits
;
1483 ram_addr_t page0
, page1
, region_start
, region_end
;
1484 DirtyBitmapSnapshot
*snap
= NULL
;
1485 int disp_width
, multi_scan
, multi_run
;
1488 uint32_t v
, addr1
, addr
;
1489 vga_draw_line_func
*vga_draw_line
= NULL
;
1490 bool share_surface
, force_shadow
= false;
1491 pixman_format_code_t format
;
1493 bool byteswap
= !s
->big_endian_fb
;
1495 bool byteswap
= s
->big_endian_fb
;
1498 full_update
|= update_basic_params(s
);
1500 s
->get_resolution(s
, &width
, &height
);
1502 depth
= s
->get_bpp(s
);
1504 /* bits 5-6: 0 = 16-color mode, 1 = 4-color mode, 2 = 256-color mode. */
1505 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1506 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1507 if (s
->cr
[VGA_CRTC_MODE
] & 1) {
1508 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1511 /* in CGA modes, multi_scan is ignored */
1512 /* XXX: is it correct ? */
1513 multi_scan
= double_scan
;
1515 multi_run
= multi_scan
;
1516 if (shift_control
!= s
->shift_control
||
1517 double_scan
!= s
->double_scan
) {
1519 s
->shift_control
= shift_control
;
1520 s
->double_scan
= double_scan
;
1523 if (shift_control
== 0) {
1524 full_update
|= update_palette16(s
);
1525 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1527 v
= VGA_DRAW_LINE4D2
;
1533 } else if (shift_control
== 1) {
1534 full_update
|= update_palette16(s
);
1535 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 8) {
1537 v
= VGA_DRAW_LINE2D2
;
1547 full_update
|= update_palette256(s
);
1548 v
= VGA_DRAW_LINE8D2
;
1552 full_update
|= update_palette256(s
);
1557 v
= s
->big_endian_fb
? VGA_DRAW_LINE15_BE
: VGA_DRAW_LINE15_LE
;
1561 v
= s
->big_endian_fb
? VGA_DRAW_LINE16_BE
: VGA_DRAW_LINE16_LE
;
1565 v
= s
->big_endian_fb
? VGA_DRAW_LINE24_BE
: VGA_DRAW_LINE24_LE
;
1569 v
= s
->big_endian_fb
? VGA_DRAW_LINE32_BE
: VGA_DRAW_LINE32_LE
;
1575 /* Horizontal pel panning bit 3 is only used in text mode. */
1576 hpel
= bits
<= 8 ? s
->params
.hpel
& 7 : 0;
1578 region_start
= (s
->params
.start_addr
* 4);
1579 region_end
= region_start
+ (ram_addr_t
)s
->params
.line_offset
* height
;
1580 region_end
+= width
* depth
/ 8; /* scanline length */
1581 region_end
-= s
->params
.line_offset
;
1585 if (region_end
> s
->vbe_size
|| depth
== 0 || depth
== 15) {
1588 * - wraps around (can happen with cirrus vbe modes)
1589 * - depth == 0 (256 color palette video mode)
1592 * Take the safe and slow route:
1593 * - create a dirty bitmap snapshot for all vga memory.
1594 * - force shadowing (so all vga memory access goes
1595 * through vga_read_*() helpers).
1597 * Given this affects only vga features which are pretty much
1598 * unused by modern guests there should be no performance
1602 region_end
= s
->vbe_size
;
1603 force_shadow
= true;
1607 * Check whether we can share the surface with the backend
1608 * or whether we need a shadow surface. We share native
1609 * endian surfaces for 15bpp and above and byteswapped
1610 * surfaces for 24bpp and above.
1612 format
= qemu_default_pixman_format(depth
, !byteswap
);
1614 share_surface
= dpy_gfx_check_format(s
->con
, format
)
1615 && !s
->force_shadow
&& !force_shadow
;
1617 share_surface
= false;
1620 if (s
->params
.line_offset
!= s
->last_line_offset
||
1621 disp_width
!= s
->last_width
||
1622 height
!= s
->last_height
||
1623 s
->last_depth
!= depth
||
1624 s
->last_byteswap
!= byteswap
||
1625 share_surface
!= is_buffer_shared(surface
)) {
1626 /* display parameters changed -> need new display surface */
1627 s
->last_scr_width
= disp_width
;
1628 s
->last_scr_height
= height
;
1629 s
->last_width
= disp_width
;
1630 s
->last_height
= height
;
1631 s
->last_line_offset
= s
->params
.line_offset
;
1632 s
->last_depth
= depth
;
1633 s
->last_byteswap
= byteswap
;
1634 /* 16 extra pixels are needed for double-width planar modes. */
1635 s
->panning_buf
= g_realloc(s
->panning_buf
,
1636 (disp_width
+ 16) * sizeof(uint32_t));
1639 if (surface_data(surface
) != s
->vram_ptr
+ (s
->params
.start_addr
* 4)
1640 && is_buffer_shared(surface
)) {
1641 /* base address changed (page flip) -> shared display surfaces
1642 * must be updated with the new base address */
1647 if (share_surface
) {
1648 surface
= qemu_create_displaysurface_from(disp_width
,
1649 height
, format
, s
->params
.line_offset
,
1650 s
->vram_ptr
+ (s
->params
.start_addr
* 4));
1651 dpy_gfx_replace_surface(s
->con
, surface
);
1653 qemu_console_resize(s
->con
, disp_width
, height
);
1654 surface
= qemu_console_surface(s
->con
);
1658 vga_draw_line
= vga_draw_line_table
[v
];
1660 if (!is_buffer_shared(surface
) && s
->cursor_invalidate
) {
1661 s
->cursor_invalidate(s
);
1665 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",
1666 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1667 s
->params
.line_compare
, sr(s
, VGA_SEQ_CLOCK_MODE
));
1669 addr1
= (s
->params
.start_addr
* 4);
1670 bwidth
= DIV_ROUND_UP(width
* bits
, 8);
1675 d
= surface_data(surface
);
1676 linesize
= surface_stride(surface
);
1680 if (s
->params
.line_compare
< height
) {
1681 /* split screen mode */
1684 snap
= memory_region_snapshot_and_clear_dirty(&s
->vram
, region_start
,
1685 region_end
- region_start
,
1689 for(y
= 0; y
< height
; y
++) {
1691 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1693 /* CGA compatibility handling */
1694 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1695 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1697 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1698 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1700 page0
= addr
& s
->vbe_size_mask
;
1701 page1
= (addr
+ bwidth
- 1) & s
->vbe_size_mask
;
1704 } else if (page1
< page0
) {
1705 /* scanline wraps from end of video memory to the start */
1706 assert(force_shadow
);
1707 update
= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1708 page0
, s
->vbe_size
- page0
);
1709 update
|= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1712 update
= memory_region_snapshot_get_dirty(&s
->vram
, snap
,
1713 page0
, page1
- page0
);
1715 /* explicit invalidation for the hardware cursor (cirrus only) */
1716 update
|= vga_scanline_invalidated(s
, y
);
1720 if (!(is_buffer_shared(surface
))) {
1722 p
= vga_draw_line(s
, d
, addr
, width
, hpel
);
1724 memcpy(d
, p
, disp_width
* sizeof(uint32_t));
1726 if (s
->cursor_draw_line
)
1727 s
->cursor_draw_line(s
, d
, y
);
1731 /* flush to display */
1732 dpy_gfx_update(s
->con
, 0, y_start
,
1733 disp_width
, y
- y_start
);
1738 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1739 if ((y1
& mask
) == mask
)
1740 addr1
+= s
->params
.line_offset
;
1742 multi_run
= multi_scan
;
1746 /* line compare acts on the displayed lines */
1747 if (y
== s
->params
.line_compare
) {
1748 if (s
->params
.hpel_split
) {
1749 hpel
= VGA_HPEL_NEUTRAL
;
1756 /* flush to display */
1757 dpy_gfx_update(s
->con
, 0, y_start
,
1758 disp_width
, y
- y_start
);
1761 memset(s
->invalidated_y_table
, 0, sizeof(s
->invalidated_y_table
));
1764 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1766 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1772 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1775 w
= s
->last_scr_width
* surface_bytes_per_pixel(surface
);
1776 d
= surface_data(surface
);
1777 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1779 d
+= surface_stride(surface
);
1781 dpy_gfx_update_full(s
->con
);
1784 #define GMODE_TEXT 0
1785 #define GMODE_GRAPH 1
1786 #define GMODE_BLANK 2
1788 static void vga_update_display(void *opaque
)
1790 VGACommonState
*s
= opaque
;
1791 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1792 int full_update
, graphic_mode
;
1794 qemu_flush_coalesced_mmio_buffer();
1796 if (surface_bits_per_pixel(surface
) == 0) {
1800 if (!(s
->ar_index
& 0x20)) {
1801 graphic_mode
= GMODE_BLANK
;
1803 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1805 if (graphic_mode
!= s
->graphic_mode
) {
1806 s
->graphic_mode
= graphic_mode
;
1807 s
->cursor_blink_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1810 switch(graphic_mode
) {
1812 vga_draw_text(s
, full_update
);
1815 vga_draw_graphic(s
, full_update
);
1819 vga_draw_blank(s
, full_update
);
1825 /* force a full display refresh */
1826 static void vga_invalidate_display(void *opaque
)
1828 VGACommonState
*s
= opaque
;
1831 s
->last_height
= -1;
1834 void vga_common_reset(VGACommonState
*s
)
1837 memset(s
->sr
, '\0', sizeof(s
->sr
));
1838 memset(s
->sr_vbe
, '\0', sizeof(s
->sr_vbe
));
1840 memset(s
->gr
, '\0', sizeof(s
->gr
));
1842 memset(s
->ar
, '\0', sizeof(s
->ar
));
1843 s
->ar_flip_flop
= 0;
1845 memset(s
->cr
, '\0', sizeof(s
->cr
));
1851 s
->dac_sub_index
= 0;
1852 s
->dac_read_index
= 0;
1853 s
->dac_write_index
= 0;
1854 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1856 memset(s
->palette
, '\0', sizeof(s
->palette
));
1859 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1860 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1861 s
->vbe_start_addr
= 0;
1862 s
->vbe_line_offset
= 0;
1863 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1864 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1865 s
->graphic_mode
= -1; /* force full update */
1866 s
->shift_control
= 0;
1868 memset(&s
->params
, '\0', sizeof(s
->params
));
1869 s
->plane_updated
= 0;
1874 s
->last_scr_width
= 0;
1875 s
->last_scr_height
= 0;
1876 s
->cursor_start
= 0;
1878 s
->cursor_offset
= 0;
1879 s
->big_endian_fb
= s
->default_endian_fb
;
1880 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1881 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1882 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1883 switch (vga_retrace_method
) {
1884 case VGA_RETRACE_DUMB
:
1886 case VGA_RETRACE_PRECISE
:
1887 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1890 vga_update_memory_access(s
);
1893 static void vga_reset(void *opaque
)
1895 VGACommonState
*s
= opaque
;
1896 vga_common_reset(s
);
1899 #define TEXTMODE_X(x) ((x) % width)
1900 #define TEXTMODE_Y(x) ((x) / width)
1901 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1902 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1903 /* relay text rendering to the display driver
1904 * instead of doing a full vga_update_display() */
1905 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
1907 VGACommonState
*s
= opaque
;
1908 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
1909 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
1911 console_ch_t
*dst
, val
;
1912 char msg_buffer
[80];
1913 int full_update
= 0;
1915 qemu_flush_coalesced_mmio_buffer();
1917 if (!(s
->ar_index
& 0x20)) {
1918 graphic_mode
= GMODE_BLANK
;
1920 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1922 if (graphic_mode
!= s
->graphic_mode
) {
1923 s
->graphic_mode
= graphic_mode
;
1926 if (s
->last_width
== -1) {
1931 switch (graphic_mode
) {
1933 /* TODO: update palette */
1934 full_update
|= update_basic_params(s
);
1936 /* total width & height */
1937 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1939 if (!(sr(s
, VGA_SEQ_CLOCK_MODE
) & VGA_SR01_CHAR_CLK_8DOTS
)) {
1942 if (sr(s
, VGA_SEQ_CLOCK_MODE
) & 0x08) {
1943 cw
= 16; /* NOTE: no 18 pixel wide */
1945 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1946 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1947 /* ugly hack for CGA 160x100x16 - explain me the logic */
1950 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1951 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1952 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1953 height
= (height
+ 1) / cheight
;
1956 size
= (height
* width
);
1957 if (size
> CH_ATTR_SIZE
) {
1961 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
1966 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1967 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
1968 s
->last_scr_width
= width
* cw
;
1969 s
->last_scr_height
= height
* cheight
;
1970 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1971 dpy_text_resize(s
->con
, width
, height
);
1973 s
->last_width
= width
;
1974 s
->last_height
= height
;
1975 s
->last_ch
= cheight
;
1981 s
->full_update_gfx
= 1;
1983 if (s
->full_update_text
) {
1984 s
->full_update_text
= 0;
1988 /* Update "hardware" cursor */
1989 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1990 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->params
.start_addr
;
1991 if (cursor_offset
!= s
->cursor_offset
||
1992 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1993 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
1994 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
1995 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
1996 dpy_text_cursor(s
->con
,
1997 TEXTMODE_X(cursor_offset
),
1998 TEXTMODE_Y(cursor_offset
));
2000 dpy_text_cursor(s
->con
, -1, -1);
2001 s
->cursor_offset
= cursor_offset
;
2002 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
2003 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
2006 src
= (uint32_t *) s
->vram_ptr
+ s
->params
.start_addr
;
2010 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2011 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2013 dpy_text_update(s
->con
, 0, 0, width
, height
);
2017 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2018 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2026 for (; i
< size
; src
++, dst
++, i
++) {
2027 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2034 if (c_min
<= c_max
) {
2035 i
= TEXTMODE_Y(c_min
);
2036 dpy_text_update(s
->con
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2045 s
->get_resolution(s
, &width
, &height
);
2046 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2054 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2058 /* Display a message */
2060 s
->last_height
= height
= 3;
2061 dpy_text_cursor(s
->con
, -1, -1);
2062 dpy_text_resize(s
->con
, s
->last_width
, height
);
2064 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2065 console_write_ch(dst
++, ' ');
2067 size
= strlen(msg_buffer
);
2068 width
= (s
->last_width
- size
) / 2;
2069 dst
= chardata
+ s
->last_width
+ width
;
2070 for (i
= 0; i
< size
; i
++)
2071 console_write_ch(dst
++, ATTR2CHTYPE(msg_buffer
[i
], QEMU_COLOR_BLUE
,
2072 QEMU_COLOR_BLACK
, 1));
2074 dpy_text_update(s
->con
, 0, 0, s
->last_width
, height
);
2077 static uint64_t vga_mem_read(void *opaque
, hwaddr addr
,
2080 VGACommonState
*s
= opaque
;
2082 return vga_mem_readb(s
, addr
);
2085 static void vga_mem_write(void *opaque
, hwaddr addr
,
2086 uint64_t data
, unsigned size
)
2088 VGACommonState
*s
= opaque
;
2090 vga_mem_writeb(s
, addr
, data
);
2093 const MemoryRegionOps vga_mem_ops
= {
2094 .read
= vga_mem_read
,
2095 .write
= vga_mem_write
,
2096 .endianness
= DEVICE_LITTLE_ENDIAN
,
2098 .min_access_size
= 1,
2099 .max_access_size
= 1,
2103 static int vga_common_post_load(void *opaque
, int version_id
)
2105 VGACommonState
*s
= opaque
;
2108 s
->graphic_mode
= -1;
2109 vbe_update_vgaregs(s
);
2110 vga_update_memory_access(s
);
2114 static bool vga_endian_state_needed(void *opaque
)
2116 VGACommonState
*s
= opaque
;
2119 * Only send the endian state if it's different from the
2120 * default one, thus ensuring backward compatibility for
2121 * migration of the common case
2123 return s
->default_endian_fb
!= s
->big_endian_fb
;
2126 static const VMStateDescription vmstate_vga_endian
= {
2127 .name
= "vga.endian",
2129 .minimum_version_id
= 1,
2130 .needed
= vga_endian_state_needed
,
2131 .fields
= (const VMStateField
[]) {
2132 VMSTATE_BOOL(big_endian_fb
, VGACommonState
),
2133 VMSTATE_END_OF_LIST()
2137 const VMStateDescription vmstate_vga_common
= {
2140 .minimum_version_id
= 2,
2141 .post_load
= vga_common_post_load
,
2142 .fields
= (const VMStateField
[]) {
2143 VMSTATE_UINT32(latch
, VGACommonState
),
2144 VMSTATE_UINT8(sr_index
, VGACommonState
),
2145 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2146 VMSTATE_UINT8(gr_index
, VGACommonState
),
2147 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2148 VMSTATE_UINT8(ar_index
, VGACommonState
),
2149 VMSTATE_BUFFER(ar
, VGACommonState
),
2150 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2151 VMSTATE_UINT8(cr_index
, VGACommonState
),
2152 VMSTATE_BUFFER(cr
, VGACommonState
),
2153 VMSTATE_UINT8(msr
, VGACommonState
),
2154 VMSTATE_UINT8(fcr
, VGACommonState
),
2155 VMSTATE_UINT8(st00
, VGACommonState
),
2156 VMSTATE_UINT8(st01
, VGACommonState
),
2158 VMSTATE_UINT8(dac_state
, VGACommonState
),
2159 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2160 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2161 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2162 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2163 VMSTATE_BUFFER(palette
, VGACommonState
),
2165 VMSTATE_INT32(bank_offset
, VGACommonState
),
2166 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
, NULL
),
2167 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2168 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2169 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2170 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2171 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2172 VMSTATE_END_OF_LIST()
2174 .subsections
= (const VMStateDescription
* const []) {
2175 &vmstate_vga_endian
,
2180 static const GraphicHwOps vga_ops
= {
2181 .invalidate
= vga_invalidate_display
,
2182 .gfx_update
= vga_update_display
,
2183 .text_update
= vga_update_text
,
2186 static inline uint32_t uint_clamp(uint32_t val
, uint32_t vmin
, uint32_t vmax
)
2197 bool vga_common_init(VGACommonState
*s
, Object
*obj
, Error
**errp
)
2200 Error
*local_err
= NULL
;
2202 for(i
= 0;i
< 256; i
++) {
2204 for(j
= 0; j
< 8; j
++) {
2205 v
|= ((i
>> j
) & 1) << (j
* 4);
2210 for(j
= 0; j
< 4; j
++) {
2211 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2215 for(i
= 0; i
< 16; i
++) {
2217 for(j
= 0; j
< 4; j
++) {
2220 v
|= b
<< (2 * j
+ 1);
2225 s
->vram_size_mb
= uint_clamp(s
->vram_size_mb
, 1, 512);
2226 s
->vram_size_mb
= pow2ceil(s
->vram_size_mb
);
2227 s
->vram_size
= s
->vram_size_mb
* MiB
;
2230 s
->vbe_size
= s
->vram_size
;
2232 s
->vbe_size_mask
= s
->vbe_size
- 1;
2234 s
->is_vbe_vmstate
= 1;
2236 if (s
->global_vmstate
&& qemu_ram_block_by_name("vga.vram")) {
2237 error_setg(errp
, "Only one global VGA device can be used at a time");
2241 memory_region_init_ram_nomigrate(&s
->vram
, obj
, "vga.vram", s
->vram_size
,
2244 error_propagate(errp
, local_err
);
2247 vmstate_register_ram(&s
->vram
, s
->global_vmstate
? NULL
: DEVICE(obj
));
2248 xen_register_framebuffer(&s
->vram
);
2249 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2250 s
->get_bpp
= vga_get_bpp
;
2251 s
->get_params
= vga_get_params
;
2252 s
->get_resolution
= vga_get_resolution
;
2253 s
->hw_ops
= &vga_ops
;
2254 switch (vga_retrace_method
) {
2255 case VGA_RETRACE_DUMB
:
2256 s
->retrace
= vga_dumb_retrace
;
2257 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2260 case VGA_RETRACE_PRECISE
:
2261 s
->retrace
= vga_precise_retrace
;
2262 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2267 * Set default fb endian based on target, could probably be turned
2268 * into a device attribute set by the machine/platform to remove
2269 * all target endian dependencies from this file.
2271 s
->default_endian_fb
= target_words_bigendian();
2273 vga_dirty_log_start(s
);
2278 static const MemoryRegionPortio vga_portio_list
[] = {
2279 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2280 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2281 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2282 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2283 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2284 PORTIO_END_OF_LIST(),
2287 static const MemoryRegionPortio vbe_portio_list_x86
[] = {
2288 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2289 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2290 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2291 PORTIO_END_OF_LIST(),
2294 static const MemoryRegionPortio vbe_portio_list_no_x86
[] = {
2295 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2296 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2297 PORTIO_END_OF_LIST(),
2300 /* Used by both ISA and PCI */
2301 MemoryRegion
*vga_init_io(VGACommonState
*s
, Object
*obj
,
2302 const MemoryRegionPortio
**vga_ports
,
2303 const MemoryRegionPortio
**vbe_ports
)
2305 MemoryRegion
*vga_mem
;
2306 MachineState
*ms
= MACHINE(qdev_get_machine());
2309 * We unfortunately need two VBE lists since non-x86 machines might
2310 * not be able to do 16-bit accesses at unaligned addresses (0x1cf)
2312 if (object_dynamic_cast(OBJECT(ms
), TYPE_X86_MACHINE
)) {
2313 *vbe_ports
= vbe_portio_list_x86
;
2315 *vbe_ports
= vbe_portio_list_no_x86
;
2318 *vga_ports
= vga_portio_list
;
2320 vga_mem
= g_malloc(sizeof(*vga_mem
));
2321 memory_region_init_io(vga_mem
, obj
, &vga_mem_ops
, s
,
2322 "vga-lowmem", 0x20000);
2323 memory_region_set_flush_coalesced(vga_mem
);
2328 void vga_init(VGACommonState
*s
, Object
*obj
, MemoryRegion
*address_space
,
2329 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2331 MemoryRegion
*vga_io_memory
;
2332 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2334 qemu_register_reset(vga_reset
, s
);
2338 s
->legacy_address_space
= address_space
;
2340 vga_io_memory
= vga_init_io(s
, obj
, &vga_ports
, &vbe_ports
);
2341 memory_region_add_subregion_overlap(address_space
,
2345 memory_region_set_coalescing(vga_io_memory
);
2346 if (init_vga_ports
) {
2347 portio_list_init(&s
->vga_port_list
, obj
, vga_ports
, s
, "vga");
2348 portio_list_set_flush_coalesced(&s
->vga_port_list
);
2349 portio_list_add(&s
->vga_port_list
, address_space_io
, 0x3b0);
2352 portio_list_init(&s
->vbe_port_list
, obj
, vbe_ports
, s
, "vbe");
2353 portio_list_add(&s
->vbe_port_list
, address_space_io
, 0x1ce);