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
26 #include "ui/console.h"
27 #include "hw/i386/pc.h"
28 #include "hw/pci/pci.h"
30 #include "ui/pixel_ops.h"
31 #include "qemu/timer.h"
32 #include "hw/xen/xen.h"
36 //#define DEBUG_VGA_MEM
37 //#define DEBUG_VGA_REG
39 //#define DEBUG_BOCHS_VBE
41 /* 16 state changes per vertical frame @60 Hz */
42 #define VGA_TEXT_CURSOR_PERIOD_MS (1000 * 2 * 16 / 60)
45 * Video Graphics Array (VGA)
47 * Chipset docs for original IBM VGA:
48 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
51 * http://www.osdever.net/FreeVGA/home.htm
53 * Standard VGA features and Bochs VBE extensions are implemented.
56 /* force some bits to zero */
57 const uint8_t sr_mask
[8] = {
68 const uint8_t gr_mask
[16] = {
87 #define cbswap_32(__x) \
89 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
90 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
91 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
92 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
94 #ifdef HOST_WORDS_BIGENDIAN
95 #define PAT(x) cbswap_32(x)
100 #ifdef HOST_WORDS_BIGENDIAN
106 #ifdef HOST_WORDS_BIGENDIAN
107 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
109 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
112 static const uint32_t mask16
[16] = {
133 #ifdef HOST_WORDS_BIGENDIAN
136 #define PAT(x) cbswap_32(x)
139 static const uint32_t dmask16
[16] = {
158 static const uint32_t dmask4
[4] = {
165 static uint32_t expand4
[256];
166 static uint16_t expand2
[256];
167 static uint8_t expand4to8
[16];
169 static void vga_update_memory_access(VGACommonState
*s
)
171 hwaddr base
, offset
, size
;
173 if (s
->legacy_address_space
== NULL
) {
177 if (s
->has_chain4_alias
) {
178 memory_region_del_subregion(s
->legacy_address_space
, &s
->chain4_alias
);
179 object_unparent(OBJECT(&s
->chain4_alias
));
180 s
->has_chain4_alias
= false;
181 s
->plane_updated
= 0xf;
183 if ((s
->sr
[VGA_SEQ_PLANE_WRITE
] & VGA_SR02_ALL_PLANES
) ==
184 VGA_SR02_ALL_PLANES
&& s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
186 switch ((s
->gr
[VGA_GFX_MISC
] >> 2) & 3) {
194 offset
= s
->bank_offset
;
206 base
+= isa_mem_base
;
207 memory_region_init_alias(&s
->chain4_alias
, memory_region_owner(&s
->vram
),
208 "vga.chain4", &s
->vram
, offset
, size
);
209 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
210 &s
->chain4_alias
, 2);
211 s
->has_chain4_alias
= true;
215 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
220 static void vga_precise_update_retrace_info(VGACommonState
*s
)
223 int hretr_start_char
;
224 int hretr_skew_chars
;
228 int vretr_start_line
;
237 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
238 int64_t chars_per_sec
;
239 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
241 htotal_chars
= s
->cr
[VGA_CRTC_H_TOTAL
] + 5;
242 hretr_start_char
= s
->cr
[VGA_CRTC_H_SYNC_START
];
243 hretr_skew_chars
= (s
->cr
[VGA_CRTC_H_SYNC_END
] >> 5) & 3;
244 hretr_end_char
= s
->cr
[VGA_CRTC_H_SYNC_END
] & 0x1f;
246 vtotal_lines
= (s
->cr
[VGA_CRTC_V_TOTAL
] |
247 (((s
->cr
[VGA_CRTC_OVERFLOW
] & 1) |
248 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 4) & 2)) << 8)) + 2;
249 vretr_start_line
= s
->cr
[VGA_CRTC_V_SYNC_START
] |
250 ((((s
->cr
[VGA_CRTC_OVERFLOW
] >> 2) & 1) |
251 ((s
->cr
[VGA_CRTC_OVERFLOW
] >> 6) & 2)) << 8);
252 vretr_end_line
= s
->cr
[VGA_CRTC_V_SYNC_END
] & 0xf;
254 clocking_mode
= (s
->sr
[VGA_SEQ_CLOCK_MODE
] >> 3) & 1;
255 clock_sel
= (s
->msr
>> 2) & 3;
256 dots
= (s
->msr
& 1) ? 8 : 9;
258 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
260 htotal_chars
<<= clocking_mode
;
262 r
->total_chars
= vtotal_lines
* htotal_chars
;
264 r
->ticks_per_char
= get_ticks_per_sec() / (r
->total_chars
* r
->freq
);
266 r
->ticks_per_char
= get_ticks_per_sec() / chars_per_sec
;
269 r
->vstart
= vretr_start_line
;
270 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
272 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
273 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
274 r
->htotal
= htotal_chars
;
277 div2
= (s
->cr
[VGA_CRTC_MODE
] >> 2) & 1;
278 sldiv2
= (s
->cr
[VGA_CRTC_MODE
] >> 3) & 1;
288 "div2 = %d sldiv2 = %d\n"
289 "clocking_mode = %d\n"
290 "clock_sel = %d %d\n"
292 "ticks/char = %" PRId64
"\n"
294 (double) get_ticks_per_sec() / (r
->ticks_per_char
* r
->total_chars
),
312 static uint8_t vga_precise_retrace(VGACommonState
*s
)
314 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
315 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
317 if (r
->total_chars
) {
318 int cur_line
, cur_line_char
, cur_char
;
321 cur_tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
323 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
324 cur_line
= cur_char
/ r
->htotal
;
326 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
327 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
329 cur_line_char
= cur_char
% r
->htotal
;
330 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
331 val
|= ST01_DISP_ENABLE
;
337 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
341 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
343 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
346 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
348 if (s
->msr
& VGA_MIS_COLOR
) {
350 return (addr
>= 0x3b0 && addr
<= 0x3bf);
353 return (addr
>= 0x3d0 && addr
<= 0x3df);
357 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
359 VGACommonState
*s
= opaque
;
362 if (vga_ioport_invalid(s
, addr
)) {
367 if (s
->ar_flip_flop
== 0) {
374 index
= s
->ar_index
& 0x1f;
375 if (index
< VGA_ATT_C
) {
388 val
= s
->sr
[s
->sr_index
];
390 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
397 val
= s
->dac_write_index
;
400 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
401 if (++s
->dac_sub_index
== 3) {
402 s
->dac_sub_index
= 0;
416 val
= s
->gr
[s
->gr_index
];
418 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
427 val
= s
->cr
[s
->cr_index
];
429 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
434 /* just toggle to fool polling */
435 val
= s
->st01
= s
->retrace(s
);
443 #if defined(DEBUG_VGA)
444 printf("VGA: read addr=0x%04x data=0x%02x\n", addr
, val
);
449 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
451 VGACommonState
*s
= opaque
;
454 /* check port range access depending on color/monochrome mode */
455 if (vga_ioport_invalid(s
, addr
)) {
459 printf("VGA: write addr=0x%04x data=0x%02x\n", addr
, val
);
464 if (s
->ar_flip_flop
== 0) {
468 index
= s
->ar_index
& 0x1f;
470 case VGA_ATC_PALETTE0
... VGA_ATC_PALETTEF
:
471 s
->ar
[index
] = val
& 0x3f;
474 s
->ar
[index
] = val
& ~0x10;
476 case VGA_ATC_OVERSCAN
:
479 case VGA_ATC_PLANE_ENABLE
:
480 s
->ar
[index
] = val
& ~0xc0;
483 s
->ar
[index
] = val
& ~0xf0;
485 case VGA_ATC_COLOR_PAGE
:
486 s
->ar
[index
] = val
& ~0xf0;
492 s
->ar_flip_flop
^= 1;
495 s
->msr
= val
& ~0x10;
496 s
->update_retrace_info(s
);
499 s
->sr_index
= val
& 7;
503 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
505 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
506 if (s
->sr_index
== VGA_SEQ_CLOCK_MODE
) {
507 s
->update_retrace_info(s
);
509 vga_update_memory_access(s
);
512 s
->dac_read_index
= val
;
513 s
->dac_sub_index
= 0;
517 s
->dac_write_index
= val
;
518 s
->dac_sub_index
= 0;
522 s
->dac_cache
[s
->dac_sub_index
] = val
;
523 if (++s
->dac_sub_index
== 3) {
524 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
525 s
->dac_sub_index
= 0;
526 s
->dac_write_index
++;
530 s
->gr_index
= val
& 0x0f;
534 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
536 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
537 vga_update_memory_access(s
);
546 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
548 /* handle CR0-7 protection */
549 if ((s
->cr
[VGA_CRTC_V_SYNC_END
] & VGA_CR11_LOCK_CR0_CR7
) &&
550 s
->cr_index
<= VGA_CRTC_OVERFLOW
) {
551 /* can always write bit 4 of CR7 */
552 if (s
->cr_index
== VGA_CRTC_OVERFLOW
) {
553 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x10) |
558 s
->cr
[s
->cr_index
] = val
;
560 switch(s
->cr_index
) {
561 case VGA_CRTC_H_TOTAL
:
562 case VGA_CRTC_H_SYNC_START
:
563 case VGA_CRTC_H_SYNC_END
:
564 case VGA_CRTC_V_TOTAL
:
565 case VGA_CRTC_OVERFLOW
:
566 case VGA_CRTC_V_SYNC_END
:
568 s
->update_retrace_info(s
);
579 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
581 VGACommonState
*s
= opaque
;
587 uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
589 VGACommonState
*s
= opaque
;
592 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
593 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
594 switch(s
->vbe_index
) {
595 /* XXX: do not hardcode ? */
596 case VBE_DISPI_INDEX_XRES
:
597 val
= VBE_DISPI_MAX_XRES
;
599 case VBE_DISPI_INDEX_YRES
:
600 val
= VBE_DISPI_MAX_YRES
;
602 case VBE_DISPI_INDEX_BPP
:
603 val
= VBE_DISPI_MAX_BPP
;
606 val
= s
->vbe_regs
[s
->vbe_index
];
610 val
= s
->vbe_regs
[s
->vbe_index
];
612 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
613 val
= s
->vbe_size
/ (64 * 1024);
617 #ifdef DEBUG_BOCHS_VBE
618 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
623 void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
625 VGACommonState
*s
= opaque
;
629 void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
631 VGACommonState
*s
= opaque
;
633 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
634 #ifdef DEBUG_BOCHS_VBE
635 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
637 switch(s
->vbe_index
) {
638 case VBE_DISPI_INDEX_ID
:
639 if (val
== VBE_DISPI_ID0
||
640 val
== VBE_DISPI_ID1
||
641 val
== VBE_DISPI_ID2
||
642 val
== VBE_DISPI_ID3
||
643 val
== VBE_DISPI_ID4
) {
644 s
->vbe_regs
[s
->vbe_index
] = val
;
647 case VBE_DISPI_INDEX_XRES
:
648 if ((val
<= VBE_DISPI_MAX_XRES
) && ((val
& 7) == 0)) {
649 s
->vbe_regs
[s
->vbe_index
] = val
;
652 case VBE_DISPI_INDEX_YRES
:
653 if (val
<= VBE_DISPI_MAX_YRES
) {
654 s
->vbe_regs
[s
->vbe_index
] = val
;
657 case VBE_DISPI_INDEX_BPP
:
660 if (val
== 4 || val
== 8 || val
== 15 ||
661 val
== 16 || val
== 24 || val
== 32) {
662 s
->vbe_regs
[s
->vbe_index
] = val
;
665 case VBE_DISPI_INDEX_BANK
:
666 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
667 val
&= (s
->vbe_bank_mask
>> 2);
669 val
&= s
->vbe_bank_mask
;
671 s
->vbe_regs
[s
->vbe_index
] = val
;
672 s
->bank_offset
= (val
<< 16);
673 vga_update_memory_access(s
);
675 case VBE_DISPI_INDEX_ENABLE
:
676 if ((val
& VBE_DISPI_ENABLED
) &&
677 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
678 int h
, shift_control
;
680 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] =
681 s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
682 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] =
683 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
684 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
685 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
687 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
688 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 1;
690 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] *
691 ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
692 s
->vbe_start_addr
= 0;
694 /* clear the screen (should be done in BIOS) */
695 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
696 memset(s
->vram_ptr
, 0,
697 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
700 /* we initialize the VGA graphic mode (should be done
702 /* graphic mode + memory map 1 */
703 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
704 VGA_GR06_GRAPHICS_MODE
;
705 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
706 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
708 s
->cr
[VGA_CRTC_H_DISP
] =
709 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
710 /* height (only meaningful if < 1024) */
711 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
712 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
713 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
714 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
715 /* line compare to 1023 */
716 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
717 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
718 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
720 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
722 s
->sr
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
725 /* set chain 4 mode */
726 s
->sr
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
727 /* activate all planes */
728 s
->sr
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
730 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
731 (shift_control
<< 5);
732 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
734 /* XXX: the bios should do that */
737 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
738 s
->vbe_regs
[s
->vbe_index
] = val
;
739 vga_update_memory_access(s
);
741 case VBE_DISPI_INDEX_VIRT_WIDTH
:
743 int w
, h
, line_offset
;
745 if (val
< s
->vbe_regs
[VBE_DISPI_INDEX_XRES
])
748 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
749 line_offset
= w
>> 1;
751 line_offset
= w
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
752 h
= s
->vbe_size
/ line_offset
;
753 /* XXX: support weird bochs semantics ? */
754 if (h
< s
->vbe_regs
[VBE_DISPI_INDEX_YRES
])
756 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = w
;
757 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = h
;
758 s
->vbe_line_offset
= line_offset
;
761 case VBE_DISPI_INDEX_X_OFFSET
:
762 case VBE_DISPI_INDEX_Y_OFFSET
:
765 s
->vbe_regs
[s
->vbe_index
] = val
;
766 s
->vbe_start_addr
= s
->vbe_line_offset
* s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
];
767 x
= s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
];
768 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
769 s
->vbe_start_addr
+= x
>> 1;
771 s
->vbe_start_addr
+= x
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
772 s
->vbe_start_addr
>>= 2;
781 /* called for accesses between 0xa0000 and 0xc0000 */
782 uint32_t vga_mem_readb(VGACommonState
*s
, hwaddr addr
)
784 int memory_map_mode
, plane
;
787 /* convert to VGA memory offset */
788 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
790 switch(memory_map_mode
) {
796 addr
+= s
->bank_offset
;
811 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
812 /* chain 4 mode : simplest access */
813 ret
= s
->vram_ptr
[addr
];
814 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
815 /* odd/even mode (aka text mode mapping) */
816 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
817 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
819 /* standard VGA latched access */
820 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
822 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
824 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
825 ret
= GET_PLANE(s
->latch
, plane
);
828 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
829 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
838 /* called for accesses between 0xa0000 and 0xc0000 */
839 void vga_mem_writeb(VGACommonState
*s
, hwaddr addr
, uint32_t val
)
841 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
842 uint32_t write_mask
, bit_mask
, set_mask
;
845 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
847 /* convert to VGA memory offset */
848 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
850 switch(memory_map_mode
) {
856 addr
+= s
->bank_offset
;
871 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
872 /* chain 4 mode : simplest access */
875 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
876 s
->vram_ptr
[addr
] = val
;
878 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
880 s
->plane_updated
|= mask
; /* only used to detect font change */
881 memory_region_set_dirty(&s
->vram
, addr
, 1);
883 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
884 /* odd/even mode (aka text mode mapping) */
885 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
887 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
888 addr
= ((addr
& ~1) << 1) | plane
;
889 s
->vram_ptr
[addr
] = val
;
891 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
893 s
->plane_updated
|= mask
; /* only used to detect font change */
894 memory_region_set_dirty(&s
->vram
, addr
, 1);
897 /* standard VGA latched access */
898 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
903 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
904 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
908 /* apply set/reset mask */
909 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
910 val
= (val
& ~set_mask
) |
911 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
912 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
918 val
= mask16
[val
& 0x0f];
919 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
923 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
924 val
= (val
>> b
) | (val
<< (8 - b
));
926 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
927 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
931 /* apply logical operation */
932 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
933 switch(func_select
) {
953 bit_mask
|= bit_mask
<< 8;
954 bit_mask
|= bit_mask
<< 16;
955 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
958 /* mask data according to sr[2] */
959 mask
= s
->sr
[VGA_SEQ_PLANE_WRITE
];
960 s
->plane_updated
|= mask
; /* only used to detect font change */
961 write_mask
= mask16
[mask
];
962 ((uint32_t *)s
->vram_ptr
)[addr
] =
963 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
966 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
967 addr
* 4, write_mask
, val
);
969 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
973 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
974 const uint8_t *font_ptr
, int h
,
975 uint32_t fgcol
, uint32_t bgcol
);
976 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
977 const uint8_t *font_ptr
, int h
,
978 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
979 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
980 const uint8_t *s
, int width
);
983 #include "vga_template.h"
986 #include "vga_template.h"
990 #include "vga_template.h"
993 #include "vga_template.h"
997 #include "vga_template.h"
1000 #include "vga_template.h"
1004 #include "vga_template.h"
1006 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
1009 col
= rgb_to_pixel8(r
, g
, b
);
1015 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
1018 col
= rgb_to_pixel15(r
, g
, b
);
1023 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
1027 col
= rgb_to_pixel15bgr(r
, g
, b
);
1032 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
1035 col
= rgb_to_pixel16(r
, g
, b
);
1040 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
1044 col
= rgb_to_pixel16bgr(r
, g
, b
);
1049 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
1052 col
= rgb_to_pixel32(r
, g
, b
);
1056 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
1059 col
= rgb_to_pixel32bgr(r
, g
, b
);
1063 /* return true if the palette was modified */
1064 static int update_palette16(VGACommonState
*s
)
1067 uint32_t v
, col
, *palette
;
1070 palette
= s
->last_palette
;
1071 for(i
= 0; i
< 16; i
++) {
1073 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1074 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1076 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1079 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1080 c6_to_8(s
->palette
[v
+ 1]),
1081 c6_to_8(s
->palette
[v
+ 2]));
1082 if (col
!= palette
[i
]) {
1090 /* return true if the palette was modified */
1091 static int update_palette256(VGACommonState
*s
)
1094 uint32_t v
, col
, *palette
;
1097 palette
= s
->last_palette
;
1099 for(i
= 0; i
< 256; i
++) {
1101 col
= s
->rgb_to_pixel(s
->palette
[v
],
1105 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1106 c6_to_8(s
->palette
[v
+ 1]),
1107 c6_to_8(s
->palette
[v
+ 2]));
1109 if (col
!= palette
[i
]) {
1118 static void vga_get_offsets(VGACommonState
*s
,
1119 uint32_t *pline_offset
,
1120 uint32_t *pstart_addr
,
1121 uint32_t *pline_compare
)
1123 uint32_t start_addr
, line_offset
, line_compare
;
1125 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1126 line_offset
= s
->vbe_line_offset
;
1127 start_addr
= s
->vbe_start_addr
;
1128 line_compare
= 65535;
1130 /* compute line_offset in bytes */
1131 line_offset
= s
->cr
[VGA_CRTC_OFFSET
];
1134 /* starting address */
1135 start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1136 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1139 line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1140 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1141 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1143 *pline_offset
= line_offset
;
1144 *pstart_addr
= start_addr
;
1145 *pline_compare
= line_compare
;
1148 /* update start_addr and line_offset. Return TRUE if modified */
1149 static int update_basic_params(VGACommonState
*s
)
1152 uint32_t start_addr
, line_offset
, line_compare
;
1156 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1158 if (line_offset
!= s
->line_offset
||
1159 start_addr
!= s
->start_addr
||
1160 line_compare
!= s
->line_compare
) {
1161 s
->line_offset
= line_offset
;
1162 s
->start_addr
= start_addr
;
1163 s
->line_compare
= line_compare
;
1171 static inline int get_depth_index(DisplaySurface
*s
)
1173 switch (surface_bits_per_pixel(s
)) {
1182 if (is_surface_bgr(s
)) {
1190 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1200 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1202 vga_draw_glyph16_16
,
1203 vga_draw_glyph16_16
,
1204 vga_draw_glyph16_32
,
1205 vga_draw_glyph16_32
,
1206 vga_draw_glyph16_16
,
1207 vga_draw_glyph16_16
,
1210 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1220 static const uint8_t cursor_glyph
[32 * 4] = {
1221 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1222 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1223 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1225 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1226 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1227 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1228 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1229 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1230 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1231 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1232 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1233 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1235 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1236 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1239 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1240 int *pcwidth
, int *pcheight
)
1242 int width
, cwidth
, height
, cheight
;
1244 /* total width & height */
1245 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1247 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
1250 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
1251 cwidth
= 16; /* NOTE: no 18 pixel wide */
1253 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1254 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1255 /* ugly hack for CGA 160x100x16 - explain me the logic */
1258 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1259 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1260 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1261 height
= (height
+ 1) / cheight
;
1267 *pcheight
= cheight
;
1270 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1272 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1277 rgb_to_pixel32bgr_dup
,
1278 rgb_to_pixel15bgr_dup
,
1279 rgb_to_pixel16bgr_dup
,
1290 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1292 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1293 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1294 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1295 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1296 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1297 const uint8_t *font_ptr
, *font_base
[2];
1298 int dup9
, line_offset
, depth_index
;
1300 uint32_t *ch_attr_ptr
;
1301 vga_draw_glyph8_func
*vga_draw_glyph8
;
1302 vga_draw_glyph9_func
*vga_draw_glyph9
;
1303 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1305 /* compute font data address (in plane 2) */
1306 v
= s
->sr
[VGA_SEQ_CHARACTER_MAP
];
1307 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1308 if (offset
!= s
->font_offsets
[0]) {
1309 s
->font_offsets
[0] = offset
;
1312 font_base
[0] = s
->vram_ptr
+ offset
;
1314 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1315 font_base
[1] = s
->vram_ptr
+ offset
;
1316 if (offset
!= s
->font_offsets
[1]) {
1317 s
->font_offsets
[1] = offset
;
1320 if (s
->plane_updated
& (1 << 2) || s
->has_chain4_alias
) {
1321 /* if the plane 2 was modified since the last display, it
1322 indicates the font may have been modified */
1323 s
->plane_updated
= 0;
1326 full_update
|= update_basic_params(s
);
1328 line_offset
= s
->line_offset
;
1330 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1331 if ((height
* width
) <= 1) {
1332 /* better than nothing: exit if transient size is too small */
1335 if ((height
* width
) > CH_ATTR_SIZE
) {
1336 /* better than nothing: exit if transient size is too big */
1340 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1341 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1342 s
->last_scr_width
= width
* cw
;
1343 s
->last_scr_height
= height
* cheight
;
1344 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1345 surface
= qemu_console_surface(s
->con
);
1346 dpy_text_resize(s
->con
, width
, height
);
1348 s
->last_width
= width
;
1349 s
->last_height
= height
;
1350 s
->last_ch
= cheight
;
1355 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1356 full_update
|= update_palette16(s
);
1357 palette
= s
->last_palette
;
1358 x_incr
= cw
* surface_bytes_per_pixel(surface
);
1361 s
->full_update_text
= 1;
1363 if (s
->full_update_gfx
) {
1364 s
->full_update_gfx
= 0;
1368 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1369 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1370 if (cursor_offset
!= s
->cursor_offset
||
1371 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1372 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1373 /* if the cursor position changed, we update the old and new
1375 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1376 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1377 if (cursor_offset
< CH_ATTR_SIZE
)
1378 s
->last_ch_attr
[cursor_offset
] = -1;
1379 s
->cursor_offset
= cursor_offset
;
1380 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1381 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1383 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1384 if (now
>= s
->cursor_blink_time
) {
1385 s
->cursor_blink_time
= now
+ VGA_TEXT_CURSOR_PERIOD_MS
/ 2;
1386 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1389 depth_index
= get_depth_index(surface
);
1391 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1393 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1394 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1396 dest
= surface_data(surface
);
1397 linesize
= surface_stride(surface
);
1398 ch_attr_ptr
= s
->last_ch_attr
;
1400 offset
= s
->start_addr
* 4;
1401 for(cy
= 0; cy
< height
; cy
++) {
1403 src
= s
->vram_ptr
+ offset
;
1406 for(cx
= 0; cx
< width
; cx
++) {
1407 ch_attr
= *(uint16_t *)src
;
1408 if (full_update
|| ch_attr
!= *ch_attr_ptr
|| src
== cursor_ptr
) {
1413 *ch_attr_ptr
= ch_attr
;
1414 #ifdef HOST_WORDS_BIGENDIAN
1416 cattr
= ch_attr
& 0xff;
1418 ch
= ch_attr
& 0xff;
1419 cattr
= ch_attr
>> 8;
1421 font_ptr
= font_base
[(cattr
>> 3) & 1];
1422 font_ptr
+= 32 * 4 * ch
;
1423 bgcol
= palette
[cattr
>> 4];
1424 fgcol
= palette
[cattr
& 0x0f];
1426 vga_draw_glyph8(d1
, linesize
,
1427 font_ptr
, cheight
, fgcol
, bgcol
);
1430 if (ch
>= 0xb0 && ch
<= 0xdf &&
1431 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1434 vga_draw_glyph9(d1
, linesize
,
1435 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1437 if (src
== cursor_ptr
&&
1438 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20) &&
1439 s
->cursor_visible_phase
) {
1440 int line_start
, line_last
, h
;
1441 /* draw the cursor */
1442 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1443 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1444 /* XXX: check that */
1445 if (line_last
> cheight
- 1)
1446 line_last
= cheight
- 1;
1447 if (line_last
>= line_start
&& line_start
< cheight
) {
1448 h
= line_last
- line_start
+ 1;
1449 d
= d1
+ linesize
* line_start
;
1451 vga_draw_glyph8(d
, linesize
,
1452 cursor_glyph
, h
, fgcol
, bgcol
);
1454 vga_draw_glyph9(d
, linesize
,
1455 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1465 dpy_gfx_update(s
->con
, cx_min
* cw
, cy
* cheight
,
1466 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1468 dest
+= linesize
* cheight
;
1469 line1
= line
+ cheight
;
1470 offset
+= line_offset
;
1471 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1492 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1502 vga_draw_line2d2_16
,
1503 vga_draw_line2d2_16
,
1504 vga_draw_line2d2_32
,
1505 vga_draw_line2d2_32
,
1506 vga_draw_line2d2_16
,
1507 vga_draw_line2d2_16
,
1518 vga_draw_line4d2_16
,
1519 vga_draw_line4d2_16
,
1520 vga_draw_line4d2_32
,
1521 vga_draw_line4d2_32
,
1522 vga_draw_line4d2_16
,
1523 vga_draw_line4d2_16
,
1526 vga_draw_line8d2_16
,
1527 vga_draw_line8d2_16
,
1528 vga_draw_line8d2_32
,
1529 vga_draw_line8d2_32
,
1530 vga_draw_line8d2_16
,
1531 vga_draw_line8d2_16
,
1545 vga_draw_line15_32bgr
,
1546 vga_draw_line15_15bgr
,
1547 vga_draw_line15_16bgr
,
1553 vga_draw_line16_32bgr
,
1554 vga_draw_line16_15bgr
,
1555 vga_draw_line16_16bgr
,
1561 vga_draw_line24_32bgr
,
1562 vga_draw_line24_15bgr
,
1563 vga_draw_line24_16bgr
,
1569 vga_draw_line32_32bgr
,
1570 vga_draw_line32_15bgr
,
1571 vga_draw_line32_16bgr
,
1574 static int vga_get_bpp(VGACommonState
*s
)
1578 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1579 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1586 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1590 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1591 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1592 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1594 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1595 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1596 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1597 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1598 height
= (height
+ 1);
1604 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1607 if (y1
>= VGA_MAX_HEIGHT
)
1609 if (y2
>= VGA_MAX_HEIGHT
)
1610 y2
= VGA_MAX_HEIGHT
;
1611 for(y
= y1
; y
< y2
; y
++) {
1612 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1616 void vga_sync_dirty_bitmap(VGACommonState
*s
)
1618 memory_region_sync_dirty_bitmap(&s
->vram
);
1621 void vga_dirty_log_start(VGACommonState
*s
)
1623 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1626 void vga_dirty_log_stop(VGACommonState
*s
)
1628 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1634 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1636 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1637 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1638 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1639 ram_addr_t page0
, page1
, page_min
, page_max
;
1640 int disp_width
, multi_scan
, multi_run
;
1642 uint32_t v
, addr1
, addr
;
1643 vga_draw_line_func
*vga_draw_line
;
1644 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1645 static const bool byteswap
= false;
1647 static const bool byteswap
= true;
1650 full_update
|= update_basic_params(s
);
1653 vga_sync_dirty_bitmap(s
);
1655 s
->get_resolution(s
, &width
, &height
);
1658 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1659 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1660 if (shift_control
!= 1) {
1661 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1664 /* in CGA modes, multi_scan is ignored */
1665 /* XXX: is it correct ? */
1666 multi_scan
= double_scan
;
1668 multi_run
= multi_scan
;
1669 if (shift_control
!= s
->shift_control
||
1670 double_scan
!= s
->double_scan
) {
1672 s
->shift_control
= shift_control
;
1673 s
->double_scan
= double_scan
;
1676 if (shift_control
== 0) {
1677 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1680 } else if (shift_control
== 1) {
1681 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1686 depth
= s
->get_bpp(s
);
1687 if (s
->line_offset
!= s
->last_line_offset
||
1688 disp_width
!= s
->last_width
||
1689 height
!= s
->last_height
||
1690 s
->last_depth
!= depth
) {
1691 if (depth
== 32 || (depth
== 16 && !byteswap
)) {
1692 surface
= qemu_create_displaysurface_from(disp_width
,
1693 height
, depth
, s
->line_offset
,
1694 s
->vram_ptr
+ (s
->start_addr
* 4), byteswap
);
1695 dpy_gfx_replace_surface(s
->con
, surface
);
1697 qemu_console_resize(s
->con
, disp_width
, height
);
1698 surface
= qemu_console_surface(s
->con
);
1700 s
->last_scr_width
= disp_width
;
1701 s
->last_scr_height
= height
;
1702 s
->last_width
= disp_width
;
1703 s
->last_height
= height
;
1704 s
->last_line_offset
= s
->line_offset
;
1705 s
->last_depth
= depth
;
1707 } else if (is_buffer_shared(surface
) &&
1708 (full_update
|| surface_data(surface
) != s
->vram_ptr
1709 + (s
->start_addr
* 4))) {
1710 surface
= qemu_create_displaysurface_from(disp_width
,
1711 height
, depth
, s
->line_offset
,
1712 s
->vram_ptr
+ (s
->start_addr
* 4), byteswap
);
1713 dpy_gfx_replace_surface(s
->con
, surface
);
1717 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1719 if (shift_control
== 0) {
1720 full_update
|= update_palette16(s
);
1721 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1722 v
= VGA_DRAW_LINE4D2
;
1727 } else if (shift_control
== 1) {
1728 full_update
|= update_palette16(s
);
1729 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1730 v
= VGA_DRAW_LINE2D2
;
1736 switch(s
->get_bpp(s
)) {
1739 full_update
|= update_palette256(s
);
1740 v
= VGA_DRAW_LINE8D2
;
1744 full_update
|= update_palette256(s
);
1749 v
= VGA_DRAW_LINE15
;
1753 v
= VGA_DRAW_LINE16
;
1757 v
= VGA_DRAW_LINE24
;
1761 v
= VGA_DRAW_LINE32
;
1766 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+
1767 get_depth_index(surface
)];
1769 if (!is_buffer_shared(surface
) && s
->cursor_invalidate
) {
1770 s
->cursor_invalidate(s
);
1773 line_offset
= s
->line_offset
;
1775 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",
1776 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1777 s
->line_compare
, s
->sr
[VGA_SEQ_CLOCK_MODE
]);
1779 addr1
= (s
->start_addr
* 4);
1780 bwidth
= (width
* bits
+ 7) / 8;
1784 d
= surface_data(surface
);
1785 linesize
= surface_stride(surface
);
1787 for(y
= 0; y
< height
; y
++) {
1789 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1791 /* CGA compatibility handling */
1792 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1793 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1795 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1796 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1798 update
= full_update
;
1800 page1
= addr
+ bwidth
- 1;
1801 update
|= memory_region_get_dirty(&s
->vram
, page0
, page1
- page0
,
1803 /* explicit invalidation for the hardware cursor */
1804 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1808 if (page0
< page_min
)
1810 if (page1
> page_max
)
1812 if (!(is_buffer_shared(surface
))) {
1813 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1814 if (s
->cursor_draw_line
)
1815 s
->cursor_draw_line(s
, d
, y
);
1819 /* flush to display */
1820 dpy_gfx_update(s
->con
, 0, y_start
,
1821 disp_width
, y
- y_start
);
1826 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1827 if ((y1
& mask
) == mask
)
1828 addr1
+= line_offset
;
1830 multi_run
= multi_scan
;
1834 /* line compare acts on the displayed lines */
1835 if (y
== s
->line_compare
)
1840 /* flush to display */
1841 dpy_gfx_update(s
->con
, 0, y_start
,
1842 disp_width
, y
- y_start
);
1844 /* reset modified pages */
1845 if (page_max
>= page_min
) {
1846 memory_region_reset_dirty(&s
->vram
,
1848 page_max
- page_min
,
1851 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1854 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1856 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1862 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1866 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1867 if (surface_bits_per_pixel(surface
) == 8) {
1868 val
= s
->rgb_to_pixel(0, 0, 0);
1872 w
= s
->last_scr_width
* surface_bytes_per_pixel(surface
);
1873 d
= surface_data(surface
);
1874 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1876 d
+= surface_stride(surface
);
1878 dpy_gfx_update(s
->con
, 0, 0,
1879 s
->last_scr_width
, s
->last_scr_height
);
1882 #define GMODE_TEXT 0
1883 #define GMODE_GRAPH 1
1884 #define GMODE_BLANK 2
1886 static void vga_update_display(void *opaque
)
1888 VGACommonState
*s
= opaque
;
1889 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1890 int full_update
, graphic_mode
;
1892 qemu_flush_coalesced_mmio_buffer();
1894 if (surface_bits_per_pixel(surface
) == 0) {
1898 if (!(s
->ar_index
& 0x20)) {
1899 graphic_mode
= GMODE_BLANK
;
1901 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1903 if (graphic_mode
!= s
->graphic_mode
) {
1904 s
->graphic_mode
= graphic_mode
;
1905 s
->cursor_blink_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1908 switch(graphic_mode
) {
1910 vga_draw_text(s
, full_update
);
1913 vga_draw_graphic(s
, full_update
);
1917 vga_draw_blank(s
, full_update
);
1923 /* force a full display refresh */
1924 static void vga_invalidate_display(void *opaque
)
1926 VGACommonState
*s
= opaque
;
1929 s
->last_height
= -1;
1932 void vga_common_reset(VGACommonState
*s
)
1935 memset(s
->sr
, '\0', sizeof(s
->sr
));
1937 memset(s
->gr
, '\0', sizeof(s
->gr
));
1939 memset(s
->ar
, '\0', sizeof(s
->ar
));
1940 s
->ar_flip_flop
= 0;
1942 memset(s
->cr
, '\0', sizeof(s
->cr
));
1948 s
->dac_sub_index
= 0;
1949 s
->dac_read_index
= 0;
1950 s
->dac_write_index
= 0;
1951 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1953 memset(s
->palette
, '\0', sizeof(s
->palette
));
1956 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1957 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1958 s
->vbe_start_addr
= 0;
1959 s
->vbe_line_offset
= 0;
1960 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1961 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1962 s
->graphic_mode
= -1; /* force full update */
1963 s
->shift_control
= 0;
1966 s
->line_compare
= 0;
1968 s
->plane_updated
= 0;
1973 s
->last_scr_width
= 0;
1974 s
->last_scr_height
= 0;
1975 s
->cursor_start
= 0;
1977 s
->cursor_offset
= 0;
1978 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1979 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1980 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1981 switch (vga_retrace_method
) {
1982 case VGA_RETRACE_DUMB
:
1984 case VGA_RETRACE_PRECISE
:
1985 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1988 vga_update_memory_access(s
);
1991 static void vga_reset(void *opaque
)
1993 VGACommonState
*s
= opaque
;
1994 vga_common_reset(s
);
1997 #define TEXTMODE_X(x) ((x) % width)
1998 #define TEXTMODE_Y(x) ((x) / width)
1999 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
2000 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
2001 /* relay text rendering to the display driver
2002 * instead of doing a full vga_update_display() */
2003 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
2005 VGACommonState
*s
= opaque
;
2006 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
2007 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
2009 console_ch_t
*dst
, val
;
2010 char msg_buffer
[80];
2011 int full_update
= 0;
2013 qemu_flush_coalesced_mmio_buffer();
2015 if (!(s
->ar_index
& 0x20)) {
2016 graphic_mode
= GMODE_BLANK
;
2018 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
2020 if (graphic_mode
!= s
->graphic_mode
) {
2021 s
->graphic_mode
= graphic_mode
;
2024 if (s
->last_width
== -1) {
2029 switch (graphic_mode
) {
2031 /* TODO: update palette */
2032 full_update
|= update_basic_params(s
);
2034 /* total width & height */
2035 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
2037 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
2040 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
2041 cw
= 16; /* NOTE: no 18 pixel wide */
2043 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
2044 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
2045 /* ugly hack for CGA 160x100x16 - explain me the logic */
2048 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
2049 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
2050 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
2051 height
= (height
+ 1) / cheight
;
2054 size
= (height
* width
);
2055 if (size
> CH_ATTR_SIZE
) {
2059 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
2064 if (width
!= s
->last_width
|| height
!= s
->last_height
||
2065 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
2066 s
->last_scr_width
= width
* cw
;
2067 s
->last_scr_height
= height
* cheight
;
2068 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
2069 dpy_text_resize(s
->con
, width
, height
);
2071 s
->last_width
= width
;
2072 s
->last_height
= height
;
2073 s
->last_ch
= cheight
;
2079 s
->full_update_gfx
= 1;
2081 if (s
->full_update_text
) {
2082 s
->full_update_text
= 0;
2086 /* Update "hardware" cursor */
2087 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
2088 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
2089 if (cursor_offset
!= s
->cursor_offset
||
2090 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
2091 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
2092 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
2093 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
2094 dpy_text_cursor(s
->con
,
2095 TEXTMODE_X(cursor_offset
),
2096 TEXTMODE_Y(cursor_offset
));
2098 dpy_text_cursor(s
->con
, -1, -1);
2099 s
->cursor_offset
= cursor_offset
;
2100 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
2101 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
2104 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2108 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2109 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2111 dpy_text_update(s
->con
, 0, 0, width
, height
);
2115 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2116 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2124 for (; i
< size
; src
++, dst
++, i
++) {
2125 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2132 if (c_min
<= c_max
) {
2133 i
= TEXTMODE_Y(c_min
);
2134 dpy_text_update(s
->con
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2143 s
->get_resolution(s
, &width
, &height
);
2144 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2152 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2156 /* Display a message */
2158 s
->last_height
= height
= 3;
2159 dpy_text_cursor(s
->con
, -1, -1);
2160 dpy_text_resize(s
->con
, s
->last_width
, height
);
2162 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2163 console_write_ch(dst
++, ' ');
2165 size
= strlen(msg_buffer
);
2166 width
= (s
->last_width
- size
) / 2;
2167 dst
= chardata
+ s
->last_width
+ width
;
2168 for (i
= 0; i
< size
; i
++)
2169 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2171 dpy_text_update(s
->con
, 0, 0, s
->last_width
, height
);
2174 static uint64_t vga_mem_read(void *opaque
, hwaddr addr
,
2177 VGACommonState
*s
= opaque
;
2179 return vga_mem_readb(s
, addr
);
2182 static void vga_mem_write(void *opaque
, hwaddr addr
,
2183 uint64_t data
, unsigned size
)
2185 VGACommonState
*s
= opaque
;
2187 return vga_mem_writeb(s
, addr
, data
);
2190 const MemoryRegionOps vga_mem_ops
= {
2191 .read
= vga_mem_read
,
2192 .write
= vga_mem_write
,
2193 .endianness
= DEVICE_LITTLE_ENDIAN
,
2195 .min_access_size
= 1,
2196 .max_access_size
= 1,
2200 static int vga_common_post_load(void *opaque
, int version_id
)
2202 VGACommonState
*s
= opaque
;
2205 s
->graphic_mode
= -1;
2209 const VMStateDescription vmstate_vga_common
= {
2212 .minimum_version_id
= 2,
2213 .post_load
= vga_common_post_load
,
2214 .fields
= (VMStateField
[]) {
2215 VMSTATE_UINT32(latch
, VGACommonState
),
2216 VMSTATE_UINT8(sr_index
, VGACommonState
),
2217 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2218 VMSTATE_UINT8(gr_index
, VGACommonState
),
2219 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2220 VMSTATE_UINT8(ar_index
, VGACommonState
),
2221 VMSTATE_BUFFER(ar
, VGACommonState
),
2222 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2223 VMSTATE_UINT8(cr_index
, VGACommonState
),
2224 VMSTATE_BUFFER(cr
, VGACommonState
),
2225 VMSTATE_UINT8(msr
, VGACommonState
),
2226 VMSTATE_UINT8(fcr
, VGACommonState
),
2227 VMSTATE_UINT8(st00
, VGACommonState
),
2228 VMSTATE_UINT8(st01
, VGACommonState
),
2230 VMSTATE_UINT8(dac_state
, VGACommonState
),
2231 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2232 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2233 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2234 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2235 VMSTATE_BUFFER(palette
, VGACommonState
),
2237 VMSTATE_INT32(bank_offset
, VGACommonState
),
2238 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2239 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2240 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2241 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2242 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2243 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2244 VMSTATE_END_OF_LIST()
2248 static const GraphicHwOps vga_ops
= {
2249 .invalidate
= vga_invalidate_display
,
2250 .gfx_update
= vga_update_display
,
2251 .text_update
= vga_update_text
,
2254 void vga_common_init(VGACommonState
*s
, Object
*obj
, bool global_vmstate
)
2258 for(i
= 0;i
< 256; i
++) {
2260 for(j
= 0; j
< 8; j
++) {
2261 v
|= ((i
>> j
) & 1) << (j
* 4);
2266 for(j
= 0; j
< 4; j
++) {
2267 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2271 for(i
= 0; i
< 16; i
++) {
2273 for(j
= 0; j
< 4; j
++) {
2276 v
|= b
<< (2 * j
+ 1);
2281 /* valid range: 1 MB -> 256 MB */
2282 s
->vram_size
= 1024 * 1024;
2283 while (s
->vram_size
< (s
->vram_size_mb
<< 20) &&
2284 s
->vram_size
< (256 << 20)) {
2287 s
->vram_size_mb
= s
->vram_size
>> 20;
2289 s
->vbe_size
= s
->vram_size
;
2292 s
->is_vbe_vmstate
= 1;
2293 memory_region_init_ram(&s
->vram
, obj
, "vga.vram", s
->vram_size
);
2294 vmstate_register_ram(&s
->vram
, global_vmstate
? NULL
: DEVICE(obj
));
2295 xen_register_framebuffer(&s
->vram
);
2296 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2297 s
->get_bpp
= vga_get_bpp
;
2298 s
->get_offsets
= vga_get_offsets
;
2299 s
->get_resolution
= vga_get_resolution
;
2300 s
->hw_ops
= &vga_ops
;
2301 switch (vga_retrace_method
) {
2302 case VGA_RETRACE_DUMB
:
2303 s
->retrace
= vga_dumb_retrace
;
2304 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2307 case VGA_RETRACE_PRECISE
:
2308 s
->retrace
= vga_precise_retrace
;
2309 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2312 vga_dirty_log_start(s
);
2315 static const MemoryRegionPortio vga_portio_list
[] = {
2316 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2317 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2318 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2319 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2320 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2321 PORTIO_END_OF_LIST(),
2324 static const MemoryRegionPortio vbe_portio_list
[] = {
2325 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2327 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2329 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2330 PORTIO_END_OF_LIST(),
2333 /* Used by both ISA and PCI */
2334 MemoryRegion
*vga_init_io(VGACommonState
*s
, Object
*obj
,
2335 const MemoryRegionPortio
**vga_ports
,
2336 const MemoryRegionPortio
**vbe_ports
)
2338 MemoryRegion
*vga_mem
;
2340 *vga_ports
= vga_portio_list
;
2341 *vbe_ports
= vbe_portio_list
;
2343 vga_mem
= g_malloc(sizeof(*vga_mem
));
2344 memory_region_init_io(vga_mem
, obj
, &vga_mem_ops
, s
,
2345 "vga-lowmem", 0x20000);
2346 memory_region_set_flush_coalesced(vga_mem
);
2351 void vga_init(VGACommonState
*s
, Object
*obj
, MemoryRegion
*address_space
,
2352 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2354 MemoryRegion
*vga_io_memory
;
2355 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2357 qemu_register_reset(vga_reset
, s
);
2361 s
->legacy_address_space
= address_space
;
2363 vga_io_memory
= vga_init_io(s
, obj
, &vga_ports
, &vbe_ports
);
2364 memory_region_add_subregion_overlap(address_space
,
2365 isa_mem_base
+ 0x000a0000,
2368 memory_region_set_coalescing(vga_io_memory
);
2369 if (init_vga_ports
) {
2370 portio_list_init(&s
->vga_port_list
, obj
, vga_ports
, s
, "vga");
2371 portio_list_set_flush_coalesced(&s
->vga_port_list
);
2372 portio_list_add(&s
->vga_port_list
, address_space_io
, 0x3b0);
2375 portio_list_init(&s
->vbe_port_list
, obj
, vbe_ports
, s
, "vbe");
2376 portio_list_add(&s
->vbe_port_list
, address_space_io
, 0x1ce);
2380 void vga_init_vbe(VGACommonState
*s
, Object
*obj
, MemoryRegion
*system_memory
)
2382 /* With pc-0.12 and below we map both the PCI BAR and the fixed VBE region,
2383 * so use an alias to avoid double-mapping the same region.
2385 memory_region_init_alias(&s
->vram_vbe
, obj
, "vram.vbe",
2386 &s
->vram
, 0, memory_region_size(&s
->vram
));
2387 /* XXX: use optimized standard vga accesses */
2388 memory_region_add_subregion(system_memory
,
2389 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,