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
);
580 * Sanity check vbe register writes.
582 * As we don't have a way to signal errors to the guest in the bochs
583 * dispi interface we'll go adjust the registers to the closest valid
586 static void vbe_fixup_regs(VGACommonState
*s
)
588 uint16_t *r
= s
->vbe_regs
;
589 uint32_t bits
, linelength
, maxy
, offset
;
591 if (!(r
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
592 /* vbe is turned off -- nothing to do */
597 switch (r
[VBE_DISPI_INDEX_BPP
]) {
603 bits
= r
[VBE_DISPI_INDEX_BPP
];
609 bits
= r
[VBE_DISPI_INDEX_BPP
] = 8;
614 r
[VBE_DISPI_INDEX_XRES
] &= ~7u;
615 if (r
[VBE_DISPI_INDEX_XRES
] == 0) {
616 r
[VBE_DISPI_INDEX_XRES
] = 8;
618 if (r
[VBE_DISPI_INDEX_XRES
] > VBE_DISPI_MAX_XRES
) {
619 r
[VBE_DISPI_INDEX_XRES
] = VBE_DISPI_MAX_XRES
;
621 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] &= ~7u;
622 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] > VBE_DISPI_MAX_XRES
) {
623 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = VBE_DISPI_MAX_XRES
;
625 if (r
[VBE_DISPI_INDEX_VIRT_WIDTH
] < r
[VBE_DISPI_INDEX_XRES
]) {
626 r
[VBE_DISPI_INDEX_VIRT_WIDTH
] = r
[VBE_DISPI_INDEX_XRES
];
630 linelength
= r
[VBE_DISPI_INDEX_VIRT_WIDTH
] * bits
/ 8;
631 maxy
= s
->vbe_size
/ linelength
;
632 if (r
[VBE_DISPI_INDEX_YRES
] == 0) {
633 r
[VBE_DISPI_INDEX_YRES
] = 1;
635 if (r
[VBE_DISPI_INDEX_YRES
] > VBE_DISPI_MAX_YRES
) {
636 r
[VBE_DISPI_INDEX_YRES
] = VBE_DISPI_MAX_YRES
;
638 if (r
[VBE_DISPI_INDEX_YRES
] > maxy
) {
639 r
[VBE_DISPI_INDEX_YRES
] = maxy
;
643 if (r
[VBE_DISPI_INDEX_X_OFFSET
] > VBE_DISPI_MAX_XRES
) {
644 r
[VBE_DISPI_INDEX_X_OFFSET
] = VBE_DISPI_MAX_XRES
;
646 if (r
[VBE_DISPI_INDEX_Y_OFFSET
] > VBE_DISPI_MAX_YRES
) {
647 r
[VBE_DISPI_INDEX_Y_OFFSET
] = VBE_DISPI_MAX_YRES
;
649 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
650 offset
+= r
[VBE_DISPI_INDEX_Y_OFFSET
] * linelength
;
651 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
652 r
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
653 offset
= r
[VBE_DISPI_INDEX_X_OFFSET
] * bits
/ 8;
654 if (offset
+ r
[VBE_DISPI_INDEX_YRES
] * linelength
> s
->vbe_size
) {
655 r
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
660 /* update vga state */
661 r
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = maxy
;
662 s
->vbe_line_offset
= linelength
;
663 s
->vbe_start_addr
= offset
/ 4;
666 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
668 VGACommonState
*s
= opaque
;
674 uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
676 VGACommonState
*s
= opaque
;
679 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
680 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
681 switch(s
->vbe_index
) {
682 /* XXX: do not hardcode ? */
683 case VBE_DISPI_INDEX_XRES
:
684 val
= VBE_DISPI_MAX_XRES
;
686 case VBE_DISPI_INDEX_YRES
:
687 val
= VBE_DISPI_MAX_YRES
;
689 case VBE_DISPI_INDEX_BPP
:
690 val
= VBE_DISPI_MAX_BPP
;
693 val
= s
->vbe_regs
[s
->vbe_index
];
697 val
= s
->vbe_regs
[s
->vbe_index
];
699 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
700 val
= s
->vbe_size
/ (64 * 1024);
704 #ifdef DEBUG_BOCHS_VBE
705 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
710 void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
712 VGACommonState
*s
= opaque
;
716 void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
718 VGACommonState
*s
= opaque
;
720 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
721 #ifdef DEBUG_BOCHS_VBE
722 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
724 switch(s
->vbe_index
) {
725 case VBE_DISPI_INDEX_ID
:
726 if (val
== VBE_DISPI_ID0
||
727 val
== VBE_DISPI_ID1
||
728 val
== VBE_DISPI_ID2
||
729 val
== VBE_DISPI_ID3
||
730 val
== VBE_DISPI_ID4
) {
731 s
->vbe_regs
[s
->vbe_index
] = val
;
734 case VBE_DISPI_INDEX_XRES
:
735 case VBE_DISPI_INDEX_YRES
:
736 case VBE_DISPI_INDEX_BPP
:
737 case VBE_DISPI_INDEX_VIRT_WIDTH
:
738 case VBE_DISPI_INDEX_X_OFFSET
:
739 case VBE_DISPI_INDEX_Y_OFFSET
:
740 s
->vbe_regs
[s
->vbe_index
] = val
;
743 case VBE_DISPI_INDEX_BANK
:
744 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
745 val
&= (s
->vbe_bank_mask
>> 2);
747 val
&= s
->vbe_bank_mask
;
749 s
->vbe_regs
[s
->vbe_index
] = val
;
750 s
->bank_offset
= (val
<< 16);
751 vga_update_memory_access(s
);
753 case VBE_DISPI_INDEX_ENABLE
:
754 if ((val
& VBE_DISPI_ENABLED
) &&
755 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
756 int h
, shift_control
;
758 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = 0;
759 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
760 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
761 s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] |= VBE_DISPI_ENABLED
;
764 /* clear the screen (should be done in BIOS) */
765 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
766 memset(s
->vram_ptr
, 0,
767 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
770 /* we initialize the VGA graphic mode (should be done
772 /* graphic mode + memory map 1 */
773 s
->gr
[VGA_GFX_MISC
] = (s
->gr
[VGA_GFX_MISC
] & ~0x0c) | 0x04 |
774 VGA_GR06_GRAPHICS_MODE
;
775 s
->cr
[VGA_CRTC_MODE
] |= 3; /* no CGA modes */
776 s
->cr
[VGA_CRTC_OFFSET
] = s
->vbe_line_offset
>> 3;
778 s
->cr
[VGA_CRTC_H_DISP
] =
779 (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
780 /* height (only meaningful if < 1024) */
781 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
782 s
->cr
[VGA_CRTC_V_DISP_END
] = h
;
783 s
->cr
[VGA_CRTC_OVERFLOW
] = (s
->cr
[VGA_CRTC_OVERFLOW
] & ~0x42) |
784 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
785 /* line compare to 1023 */
786 s
->cr
[VGA_CRTC_LINE_COMPARE
] = 0xff;
787 s
->cr
[VGA_CRTC_OVERFLOW
] |= 0x10;
788 s
->cr
[VGA_CRTC_MAX_SCAN
] |= 0x40;
790 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
792 s
->sr
[VGA_SEQ_CLOCK_MODE
] &= ~8; /* no double line */
795 /* set chain 4 mode */
796 s
->sr
[VGA_SEQ_MEMORY_MODE
] |= VGA_SR04_CHN_4M
;
797 /* activate all planes */
798 s
->sr
[VGA_SEQ_PLANE_WRITE
] |= VGA_SR02_ALL_PLANES
;
800 s
->gr
[VGA_GFX_MODE
] = (s
->gr
[VGA_GFX_MODE
] & ~0x60) |
801 (shift_control
<< 5);
802 s
->cr
[VGA_CRTC_MAX_SCAN
] &= ~0x9f; /* no double scan */
804 /* XXX: the bios should do that */
807 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
808 s
->vbe_regs
[s
->vbe_index
] = val
;
809 vga_update_memory_access(s
);
817 /* called for accesses between 0xa0000 and 0xc0000 */
818 uint32_t vga_mem_readb(VGACommonState
*s
, hwaddr addr
)
820 int memory_map_mode
, plane
;
823 /* convert to VGA memory offset */
824 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
826 switch(memory_map_mode
) {
832 addr
+= s
->bank_offset
;
847 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
848 /* chain 4 mode : simplest access */
849 ret
= s
->vram_ptr
[addr
];
850 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
851 /* odd/even mode (aka text mode mapping) */
852 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
853 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
855 /* standard VGA latched access */
856 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
858 if (!(s
->gr
[VGA_GFX_MODE
] & 0x08)) {
860 plane
= s
->gr
[VGA_GFX_PLANE_READ
];
861 ret
= GET_PLANE(s
->latch
, plane
);
864 ret
= (s
->latch
^ mask16
[s
->gr
[VGA_GFX_COMPARE_VALUE
]]) &
865 mask16
[s
->gr
[VGA_GFX_COMPARE_MASK
]];
874 /* called for accesses between 0xa0000 and 0xc0000 */
875 void vga_mem_writeb(VGACommonState
*s
, hwaddr addr
, uint32_t val
)
877 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
878 uint32_t write_mask
, bit_mask
, set_mask
;
881 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
883 /* convert to VGA memory offset */
884 memory_map_mode
= (s
->gr
[VGA_GFX_MISC
] >> 2) & 3;
886 switch(memory_map_mode
) {
892 addr
+= s
->bank_offset
;
907 if (s
->sr
[VGA_SEQ_MEMORY_MODE
] & VGA_SR04_CHN_4M
) {
908 /* chain 4 mode : simplest access */
911 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
912 s
->vram_ptr
[addr
] = val
;
914 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
916 s
->plane_updated
|= mask
; /* only used to detect font change */
917 memory_region_set_dirty(&s
->vram
, addr
, 1);
919 } else if (s
->gr
[VGA_GFX_MODE
] & 0x10) {
920 /* odd/even mode (aka text mode mapping) */
921 plane
= (s
->gr
[VGA_GFX_PLANE_READ
] & 2) | (addr
& 1);
923 if (s
->sr
[VGA_SEQ_PLANE_WRITE
] & mask
) {
924 addr
= ((addr
& ~1) << 1) | plane
;
925 s
->vram_ptr
[addr
] = val
;
927 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
929 s
->plane_updated
|= mask
; /* only used to detect font change */
930 memory_region_set_dirty(&s
->vram
, addr
, 1);
933 /* standard VGA latched access */
934 write_mode
= s
->gr
[VGA_GFX_MODE
] & 3;
939 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
940 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
944 /* apply set/reset mask */
945 set_mask
= mask16
[s
->gr
[VGA_GFX_SR_ENABLE
]];
946 val
= (val
& ~set_mask
) |
947 (mask16
[s
->gr
[VGA_GFX_SR_VALUE
]] & set_mask
);
948 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
954 val
= mask16
[val
& 0x0f];
955 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
];
959 b
= s
->gr
[VGA_GFX_DATA_ROTATE
] & 7;
960 val
= (val
>> b
) | (val
<< (8 - b
));
962 bit_mask
= s
->gr
[VGA_GFX_BIT_MASK
] & val
;
963 val
= mask16
[s
->gr
[VGA_GFX_SR_VALUE
]];
967 /* apply logical operation */
968 func_select
= s
->gr
[VGA_GFX_DATA_ROTATE
] >> 3;
969 switch(func_select
) {
989 bit_mask
|= bit_mask
<< 8;
990 bit_mask
|= bit_mask
<< 16;
991 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
994 /* mask data according to sr[2] */
995 mask
= s
->sr
[VGA_SEQ_PLANE_WRITE
];
996 s
->plane_updated
|= mask
; /* only used to detect font change */
997 write_mask
= mask16
[mask
];
998 ((uint32_t *)s
->vram_ptr
)[addr
] =
999 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
1001 #ifdef DEBUG_VGA_MEM
1002 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
1003 addr
* 4, write_mask
, val
);
1005 memory_region_set_dirty(&s
->vram
, addr
<< 2, sizeof(uint32_t));
1009 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
1010 const uint8_t *font_ptr
, int h
,
1011 uint32_t fgcol
, uint32_t bgcol
);
1012 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
1013 const uint8_t *font_ptr
, int h
,
1014 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
1015 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
1016 const uint8_t *s
, int width
);
1019 #include "vga_template.h"
1022 #include "vga_template.h"
1026 #include "vga_template.h"
1029 #include "vga_template.h"
1033 #include "vga_template.h"
1036 #include "vga_template.h"
1040 #include "vga_template.h"
1042 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
1045 col
= rgb_to_pixel8(r
, g
, b
);
1051 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
1054 col
= rgb_to_pixel15(r
, g
, b
);
1059 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
1063 col
= rgb_to_pixel15bgr(r
, g
, b
);
1068 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
1071 col
= rgb_to_pixel16(r
, g
, b
);
1076 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
1080 col
= rgb_to_pixel16bgr(r
, g
, b
);
1085 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
1088 col
= rgb_to_pixel32(r
, g
, b
);
1092 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
1095 col
= rgb_to_pixel32bgr(r
, g
, b
);
1099 /* return true if the palette was modified */
1100 static int update_palette16(VGACommonState
*s
)
1103 uint32_t v
, col
, *palette
;
1106 palette
= s
->last_palette
;
1107 for(i
= 0; i
< 16; i
++) {
1109 if (s
->ar
[VGA_ATC_MODE
] & 0x80) {
1110 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xf) << 4) | (v
& 0xf);
1112 v
= ((s
->ar
[VGA_ATC_COLOR_PAGE
] & 0xc) << 4) | (v
& 0x3f);
1115 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1116 c6_to_8(s
->palette
[v
+ 1]),
1117 c6_to_8(s
->palette
[v
+ 2]));
1118 if (col
!= palette
[i
]) {
1126 /* return true if the palette was modified */
1127 static int update_palette256(VGACommonState
*s
)
1130 uint32_t v
, col
, *palette
;
1133 palette
= s
->last_palette
;
1135 for(i
= 0; i
< 256; i
++) {
1137 col
= s
->rgb_to_pixel(s
->palette
[v
],
1141 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1142 c6_to_8(s
->palette
[v
+ 1]),
1143 c6_to_8(s
->palette
[v
+ 2]));
1145 if (col
!= palette
[i
]) {
1154 static void vga_get_offsets(VGACommonState
*s
,
1155 uint32_t *pline_offset
,
1156 uint32_t *pstart_addr
,
1157 uint32_t *pline_compare
)
1159 uint32_t start_addr
, line_offset
, line_compare
;
1161 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1162 line_offset
= s
->vbe_line_offset
;
1163 start_addr
= s
->vbe_start_addr
;
1164 line_compare
= 65535;
1166 /* compute line_offset in bytes */
1167 line_offset
= s
->cr
[VGA_CRTC_OFFSET
];
1170 /* starting address */
1171 start_addr
= s
->cr
[VGA_CRTC_START_LO
] |
1172 (s
->cr
[VGA_CRTC_START_HI
] << 8);
1175 line_compare
= s
->cr
[VGA_CRTC_LINE_COMPARE
] |
1176 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x10) << 4) |
1177 ((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x40) << 3);
1179 *pline_offset
= line_offset
;
1180 *pstart_addr
= start_addr
;
1181 *pline_compare
= line_compare
;
1184 /* update start_addr and line_offset. Return TRUE if modified */
1185 static int update_basic_params(VGACommonState
*s
)
1188 uint32_t start_addr
, line_offset
, line_compare
;
1192 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1194 if (line_offset
!= s
->line_offset
||
1195 start_addr
!= s
->start_addr
||
1196 line_compare
!= s
->line_compare
) {
1197 s
->line_offset
= line_offset
;
1198 s
->start_addr
= start_addr
;
1199 s
->line_compare
= line_compare
;
1207 static inline int get_depth_index(DisplaySurface
*s
)
1209 switch (surface_bits_per_pixel(s
)) {
1218 if (is_surface_bgr(s
)) {
1226 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1236 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1238 vga_draw_glyph16_16
,
1239 vga_draw_glyph16_16
,
1240 vga_draw_glyph16_32
,
1241 vga_draw_glyph16_32
,
1242 vga_draw_glyph16_16
,
1243 vga_draw_glyph16_16
,
1246 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1256 static const uint8_t cursor_glyph
[32 * 4] = {
1257 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1258 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1259 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1260 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1261 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1267 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1268 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1269 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1270 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1271 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1272 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1275 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1276 int *pcwidth
, int *pcheight
)
1278 int width
, cwidth
, height
, cheight
;
1280 /* total width & height */
1281 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
1283 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
1286 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
1287 cwidth
= 16; /* NOTE: no 18 pixel wide */
1289 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
1290 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
1291 /* ugly hack for CGA 160x100x16 - explain me the logic */
1294 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1295 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1296 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1297 height
= (height
+ 1) / cheight
;
1303 *pcheight
= cheight
;
1306 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1308 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1313 rgb_to_pixel32bgr_dup
,
1314 rgb_to_pixel15bgr_dup
,
1315 rgb_to_pixel16bgr_dup
,
1326 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1328 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1329 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1330 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1331 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1332 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1333 const uint8_t *font_ptr
, *font_base
[2];
1334 int dup9
, line_offset
, depth_index
;
1336 uint32_t *ch_attr_ptr
;
1337 vga_draw_glyph8_func
*vga_draw_glyph8
;
1338 vga_draw_glyph9_func
*vga_draw_glyph9
;
1339 int64_t now
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1341 /* compute font data address (in plane 2) */
1342 v
= s
->sr
[VGA_SEQ_CHARACTER_MAP
];
1343 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1344 if (offset
!= s
->font_offsets
[0]) {
1345 s
->font_offsets
[0] = offset
;
1348 font_base
[0] = s
->vram_ptr
+ offset
;
1350 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1351 font_base
[1] = s
->vram_ptr
+ offset
;
1352 if (offset
!= s
->font_offsets
[1]) {
1353 s
->font_offsets
[1] = offset
;
1356 if (s
->plane_updated
& (1 << 2) || s
->has_chain4_alias
) {
1357 /* if the plane 2 was modified since the last display, it
1358 indicates the font may have been modified */
1359 s
->plane_updated
= 0;
1362 full_update
|= update_basic_params(s
);
1364 line_offset
= s
->line_offset
;
1366 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1367 if ((height
* width
) <= 1) {
1368 /* better than nothing: exit if transient size is too small */
1371 if ((height
* width
) > CH_ATTR_SIZE
) {
1372 /* better than nothing: exit if transient size is too big */
1376 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1377 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1378 s
->last_scr_width
= width
* cw
;
1379 s
->last_scr_height
= height
* cheight
;
1380 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
1381 surface
= qemu_console_surface(s
->con
);
1382 dpy_text_resize(s
->con
, width
, height
);
1384 s
->last_width
= width
;
1385 s
->last_height
= height
;
1386 s
->last_ch
= cheight
;
1391 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1392 full_update
|= update_palette16(s
);
1393 palette
= s
->last_palette
;
1394 x_incr
= cw
* surface_bytes_per_pixel(surface
);
1397 s
->full_update_text
= 1;
1399 if (s
->full_update_gfx
) {
1400 s
->full_update_gfx
= 0;
1404 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
1405 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
1406 if (cursor_offset
!= s
->cursor_offset
||
1407 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
1408 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
) {
1409 /* if the cursor position changed, we update the old and new
1411 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1412 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1413 if (cursor_offset
< CH_ATTR_SIZE
)
1414 s
->last_ch_attr
[cursor_offset
] = -1;
1415 s
->cursor_offset
= cursor_offset
;
1416 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
1417 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
1419 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1420 if (now
>= s
->cursor_blink_time
) {
1421 s
->cursor_blink_time
= now
+ VGA_TEXT_CURSOR_PERIOD_MS
/ 2;
1422 s
->cursor_visible_phase
= !s
->cursor_visible_phase
;
1425 depth_index
= get_depth_index(surface
);
1427 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1429 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1430 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1432 dest
= surface_data(surface
);
1433 linesize
= surface_stride(surface
);
1434 ch_attr_ptr
= s
->last_ch_attr
;
1436 offset
= s
->start_addr
* 4;
1437 for(cy
= 0; cy
< height
; cy
++) {
1439 src
= s
->vram_ptr
+ offset
;
1442 for(cx
= 0; cx
< width
; cx
++) {
1443 ch_attr
= *(uint16_t *)src
;
1444 if (full_update
|| ch_attr
!= *ch_attr_ptr
|| src
== cursor_ptr
) {
1449 *ch_attr_ptr
= ch_attr
;
1450 #ifdef HOST_WORDS_BIGENDIAN
1452 cattr
= ch_attr
& 0xff;
1454 ch
= ch_attr
& 0xff;
1455 cattr
= ch_attr
>> 8;
1457 font_ptr
= font_base
[(cattr
>> 3) & 1];
1458 font_ptr
+= 32 * 4 * ch
;
1459 bgcol
= palette
[cattr
>> 4];
1460 fgcol
= palette
[cattr
& 0x0f];
1462 vga_draw_glyph8(d1
, linesize
,
1463 font_ptr
, cheight
, fgcol
, bgcol
);
1466 if (ch
>= 0xb0 && ch
<= 0xdf &&
1467 (s
->ar
[VGA_ATC_MODE
] & 0x04)) {
1470 vga_draw_glyph9(d1
, linesize
,
1471 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1473 if (src
== cursor_ptr
&&
1474 !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20) &&
1475 s
->cursor_visible_phase
) {
1476 int line_start
, line_last
, h
;
1477 /* draw the cursor */
1478 line_start
= s
->cr
[VGA_CRTC_CURSOR_START
] & 0x1f;
1479 line_last
= s
->cr
[VGA_CRTC_CURSOR_END
] & 0x1f;
1480 /* XXX: check that */
1481 if (line_last
> cheight
- 1)
1482 line_last
= cheight
- 1;
1483 if (line_last
>= line_start
&& line_start
< cheight
) {
1484 h
= line_last
- line_start
+ 1;
1485 d
= d1
+ linesize
* line_start
;
1487 vga_draw_glyph8(d
, linesize
,
1488 cursor_glyph
, h
, fgcol
, bgcol
);
1490 vga_draw_glyph9(d
, linesize
,
1491 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1501 dpy_gfx_update(s
->con
, cx_min
* cw
, cy
* cheight
,
1502 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1504 dest
+= linesize
* cheight
;
1505 line1
= line
+ cheight
;
1506 offset
+= line_offset
;
1507 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1528 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1538 vga_draw_line2d2_16
,
1539 vga_draw_line2d2_16
,
1540 vga_draw_line2d2_32
,
1541 vga_draw_line2d2_32
,
1542 vga_draw_line2d2_16
,
1543 vga_draw_line2d2_16
,
1554 vga_draw_line4d2_16
,
1555 vga_draw_line4d2_16
,
1556 vga_draw_line4d2_32
,
1557 vga_draw_line4d2_32
,
1558 vga_draw_line4d2_16
,
1559 vga_draw_line4d2_16
,
1562 vga_draw_line8d2_16
,
1563 vga_draw_line8d2_16
,
1564 vga_draw_line8d2_32
,
1565 vga_draw_line8d2_32
,
1566 vga_draw_line8d2_16
,
1567 vga_draw_line8d2_16
,
1581 vga_draw_line15_32bgr
,
1582 vga_draw_line15_15bgr
,
1583 vga_draw_line15_16bgr
,
1589 vga_draw_line16_32bgr
,
1590 vga_draw_line16_15bgr
,
1591 vga_draw_line16_16bgr
,
1597 vga_draw_line24_32bgr
,
1598 vga_draw_line24_15bgr
,
1599 vga_draw_line24_16bgr
,
1605 vga_draw_line32_32bgr
,
1606 vga_draw_line32_15bgr
,
1607 vga_draw_line32_16bgr
,
1610 static int vga_get_bpp(VGACommonState
*s
)
1614 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1615 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1622 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1626 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1627 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1628 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1630 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1) * 8;
1631 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
1632 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
1633 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
1634 height
= (height
+ 1);
1640 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1643 if (y1
>= VGA_MAX_HEIGHT
)
1645 if (y2
>= VGA_MAX_HEIGHT
)
1646 y2
= VGA_MAX_HEIGHT
;
1647 for(y
= y1
; y
< y2
; y
++) {
1648 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1652 void vga_sync_dirty_bitmap(VGACommonState
*s
)
1654 memory_region_sync_dirty_bitmap(&s
->vram
);
1657 void vga_dirty_log_start(VGACommonState
*s
)
1659 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1662 void vga_dirty_log_stop(VGACommonState
*s
)
1664 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1670 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1672 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1673 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1674 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1675 ram_addr_t page0
, page1
, page_min
, page_max
;
1676 int disp_width
, multi_scan
, multi_run
;
1678 uint32_t v
, addr1
, addr
;
1679 vga_draw_line_func
*vga_draw_line
;
1680 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1681 static const bool byteswap
= false;
1683 static const bool byteswap
= true;
1686 full_update
|= update_basic_params(s
);
1689 vga_sync_dirty_bitmap(s
);
1691 s
->get_resolution(s
, &width
, &height
);
1694 shift_control
= (s
->gr
[VGA_GFX_MODE
] >> 5) & 3;
1695 double_scan
= (s
->cr
[VGA_CRTC_MAX_SCAN
] >> 7);
1696 if (shift_control
!= 1) {
1697 multi_scan
= (((s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1) << double_scan
)
1700 /* in CGA modes, multi_scan is ignored */
1701 /* XXX: is it correct ? */
1702 multi_scan
= double_scan
;
1704 multi_run
= multi_scan
;
1705 if (shift_control
!= s
->shift_control
||
1706 double_scan
!= s
->double_scan
) {
1708 s
->shift_control
= shift_control
;
1709 s
->double_scan
= double_scan
;
1712 if (shift_control
== 0) {
1713 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1716 } else if (shift_control
== 1) {
1717 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1722 depth
= s
->get_bpp(s
);
1723 if (s
->line_offset
!= s
->last_line_offset
||
1724 disp_width
!= s
->last_width
||
1725 height
!= s
->last_height
||
1726 s
->last_depth
!= depth
) {
1727 if (depth
== 32 || (depth
== 16 && !byteswap
)) {
1728 pixman_format_code_t format
=
1729 qemu_default_pixman_format(depth
, !byteswap
);
1730 surface
= qemu_create_displaysurface_from(disp_width
,
1731 height
, format
, s
->line_offset
,
1732 s
->vram_ptr
+ (s
->start_addr
* 4));
1733 dpy_gfx_replace_surface(s
->con
, surface
);
1735 qemu_console_resize(s
->con
, disp_width
, height
);
1736 surface
= qemu_console_surface(s
->con
);
1738 s
->last_scr_width
= disp_width
;
1739 s
->last_scr_height
= height
;
1740 s
->last_width
= disp_width
;
1741 s
->last_height
= height
;
1742 s
->last_line_offset
= s
->line_offset
;
1743 s
->last_depth
= depth
;
1745 } else if (is_buffer_shared(surface
) &&
1746 (full_update
|| surface_data(surface
) != s
->vram_ptr
1747 + (s
->start_addr
* 4))) {
1748 pixman_format_code_t format
=
1749 qemu_default_pixman_format(depth
, !byteswap
);
1750 surface
= qemu_create_displaysurface_from(disp_width
,
1751 height
, format
, s
->line_offset
,
1752 s
->vram_ptr
+ (s
->start_addr
* 4));
1753 dpy_gfx_replace_surface(s
->con
, surface
);
1757 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1759 if (shift_control
== 0) {
1760 full_update
|= update_palette16(s
);
1761 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1762 v
= VGA_DRAW_LINE4D2
;
1767 } else if (shift_control
== 1) {
1768 full_update
|= update_palette16(s
);
1769 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 8) {
1770 v
= VGA_DRAW_LINE2D2
;
1776 switch(s
->get_bpp(s
)) {
1779 full_update
|= update_palette256(s
);
1780 v
= VGA_DRAW_LINE8D2
;
1784 full_update
|= update_palette256(s
);
1789 v
= VGA_DRAW_LINE15
;
1793 v
= VGA_DRAW_LINE16
;
1797 v
= VGA_DRAW_LINE24
;
1801 v
= VGA_DRAW_LINE32
;
1806 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+
1807 get_depth_index(surface
)];
1809 if (!is_buffer_shared(surface
) && s
->cursor_invalidate
) {
1810 s
->cursor_invalidate(s
);
1813 line_offset
= s
->line_offset
;
1815 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",
1816 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[VGA_CRTC_MODE
],
1817 s
->line_compare
, s
->sr
[VGA_SEQ_CLOCK_MODE
]);
1819 addr1
= (s
->start_addr
* 4);
1820 bwidth
= (width
* bits
+ 7) / 8;
1824 d
= surface_data(surface
);
1825 linesize
= surface_stride(surface
);
1827 for(y
= 0; y
< height
; y
++) {
1829 if (!(s
->cr
[VGA_CRTC_MODE
] & 1)) {
1831 /* CGA compatibility handling */
1832 shift
= 14 + ((s
->cr
[VGA_CRTC_MODE
] >> 6) & 1);
1833 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1835 if (!(s
->cr
[VGA_CRTC_MODE
] & 2)) {
1836 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1838 update
= full_update
;
1840 page1
= addr
+ bwidth
- 1;
1841 update
|= memory_region_get_dirty(&s
->vram
, page0
, page1
- page0
,
1843 /* explicit invalidation for the hardware cursor */
1844 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1848 if (page0
< page_min
)
1850 if (page1
> page_max
)
1852 if (!(is_buffer_shared(surface
))) {
1853 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1854 if (s
->cursor_draw_line
)
1855 s
->cursor_draw_line(s
, d
, y
);
1859 /* flush to display */
1860 dpy_gfx_update(s
->con
, 0, y_start
,
1861 disp_width
, y
- y_start
);
1866 mask
= (s
->cr
[VGA_CRTC_MODE
] & 3) ^ 3;
1867 if ((y1
& mask
) == mask
)
1868 addr1
+= line_offset
;
1870 multi_run
= multi_scan
;
1874 /* line compare acts on the displayed lines */
1875 if (y
== s
->line_compare
)
1880 /* flush to display */
1881 dpy_gfx_update(s
->con
, 0, y_start
,
1882 disp_width
, y
- y_start
);
1884 /* reset modified pages */
1885 if (page_max
>= page_min
) {
1886 memory_region_reset_dirty(&s
->vram
,
1888 page_max
- page_min
,
1891 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1894 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1896 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1902 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1906 rgb_to_pixel_dup_table
[get_depth_index(surface
)];
1907 if (surface_bits_per_pixel(surface
) == 8) {
1908 val
= s
->rgb_to_pixel(0, 0, 0);
1912 w
= s
->last_scr_width
* surface_bytes_per_pixel(surface
);
1913 d
= surface_data(surface
);
1914 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1916 d
+= surface_stride(surface
);
1918 dpy_gfx_update(s
->con
, 0, 0,
1919 s
->last_scr_width
, s
->last_scr_height
);
1922 #define GMODE_TEXT 0
1923 #define GMODE_GRAPH 1
1924 #define GMODE_BLANK 2
1926 static void vga_update_display(void *opaque
)
1928 VGACommonState
*s
= opaque
;
1929 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
1930 int full_update
, graphic_mode
;
1932 qemu_flush_coalesced_mmio_buffer();
1934 if (surface_bits_per_pixel(surface
) == 0) {
1938 if (!(s
->ar_index
& 0x20)) {
1939 graphic_mode
= GMODE_BLANK
;
1941 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
1943 if (graphic_mode
!= s
->graphic_mode
) {
1944 s
->graphic_mode
= graphic_mode
;
1945 s
->cursor_blink_time
= qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
);
1948 switch(graphic_mode
) {
1950 vga_draw_text(s
, full_update
);
1953 vga_draw_graphic(s
, full_update
);
1957 vga_draw_blank(s
, full_update
);
1963 /* force a full display refresh */
1964 static void vga_invalidate_display(void *opaque
)
1966 VGACommonState
*s
= opaque
;
1969 s
->last_height
= -1;
1972 void vga_common_reset(VGACommonState
*s
)
1975 memset(s
->sr
, '\0', sizeof(s
->sr
));
1977 memset(s
->gr
, '\0', sizeof(s
->gr
));
1979 memset(s
->ar
, '\0', sizeof(s
->ar
));
1980 s
->ar_flip_flop
= 0;
1982 memset(s
->cr
, '\0', sizeof(s
->cr
));
1988 s
->dac_sub_index
= 0;
1989 s
->dac_read_index
= 0;
1990 s
->dac_write_index
= 0;
1991 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1993 memset(s
->palette
, '\0', sizeof(s
->palette
));
1996 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1997 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1998 s
->vbe_start_addr
= 0;
1999 s
->vbe_line_offset
= 0;
2000 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
2001 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
2002 s
->graphic_mode
= -1; /* force full update */
2003 s
->shift_control
= 0;
2006 s
->line_compare
= 0;
2008 s
->plane_updated
= 0;
2013 s
->last_scr_width
= 0;
2014 s
->last_scr_height
= 0;
2015 s
->cursor_start
= 0;
2017 s
->cursor_offset
= 0;
2018 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
2019 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
2020 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
2021 switch (vga_retrace_method
) {
2022 case VGA_RETRACE_DUMB
:
2024 case VGA_RETRACE_PRECISE
:
2025 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
2028 vga_update_memory_access(s
);
2031 static void vga_reset(void *opaque
)
2033 VGACommonState
*s
= opaque
;
2034 vga_common_reset(s
);
2037 #define TEXTMODE_X(x) ((x) % width)
2038 #define TEXTMODE_Y(x) ((x) / width)
2039 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
2040 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
2041 /* relay text rendering to the display driver
2042 * instead of doing a full vga_update_display() */
2043 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
2045 VGACommonState
*s
= opaque
;
2046 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
2047 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
2049 console_ch_t
*dst
, val
;
2050 char msg_buffer
[80];
2051 int full_update
= 0;
2053 qemu_flush_coalesced_mmio_buffer();
2055 if (!(s
->ar_index
& 0x20)) {
2056 graphic_mode
= GMODE_BLANK
;
2058 graphic_mode
= s
->gr
[VGA_GFX_MISC
] & VGA_GR06_GRAPHICS_MODE
;
2060 if (graphic_mode
!= s
->graphic_mode
) {
2061 s
->graphic_mode
= graphic_mode
;
2064 if (s
->last_width
== -1) {
2069 switch (graphic_mode
) {
2071 /* TODO: update palette */
2072 full_update
|= update_basic_params(s
);
2074 /* total width & height */
2075 cheight
= (s
->cr
[VGA_CRTC_MAX_SCAN
] & 0x1f) + 1;
2077 if (!(s
->sr
[VGA_SEQ_CLOCK_MODE
] & VGA_SR01_CHAR_CLK_8DOTS
)) {
2080 if (s
->sr
[VGA_SEQ_CLOCK_MODE
] & 0x08) {
2081 cw
= 16; /* NOTE: no 18 pixel wide */
2083 width
= (s
->cr
[VGA_CRTC_H_DISP
] + 1);
2084 if (s
->cr
[VGA_CRTC_V_TOTAL
] == 100) {
2085 /* ugly hack for CGA 160x100x16 - explain me the logic */
2088 height
= s
->cr
[VGA_CRTC_V_DISP_END
] |
2089 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x02) << 7) |
2090 ((s
->cr
[VGA_CRTC_OVERFLOW
] & 0x40) << 3);
2091 height
= (height
+ 1) / cheight
;
2094 size
= (height
* width
);
2095 if (size
> CH_ATTR_SIZE
) {
2099 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
2104 if (width
!= s
->last_width
|| height
!= s
->last_height
||
2105 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
2106 s
->last_scr_width
= width
* cw
;
2107 s
->last_scr_height
= height
* cheight
;
2108 qemu_console_resize(s
->con
, s
->last_scr_width
, s
->last_scr_height
);
2109 dpy_text_resize(s
->con
, width
, height
);
2111 s
->last_width
= width
;
2112 s
->last_height
= height
;
2113 s
->last_ch
= cheight
;
2119 s
->full_update_gfx
= 1;
2121 if (s
->full_update_text
) {
2122 s
->full_update_text
= 0;
2126 /* Update "hardware" cursor */
2127 cursor_offset
= ((s
->cr
[VGA_CRTC_CURSOR_HI
] << 8) |
2128 s
->cr
[VGA_CRTC_CURSOR_LO
]) - s
->start_addr
;
2129 if (cursor_offset
!= s
->cursor_offset
||
2130 s
->cr
[VGA_CRTC_CURSOR_START
] != s
->cursor_start
||
2131 s
->cr
[VGA_CRTC_CURSOR_END
] != s
->cursor_end
|| full_update
) {
2132 cursor_visible
= !(s
->cr
[VGA_CRTC_CURSOR_START
] & 0x20);
2133 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
2134 dpy_text_cursor(s
->con
,
2135 TEXTMODE_X(cursor_offset
),
2136 TEXTMODE_Y(cursor_offset
));
2138 dpy_text_cursor(s
->con
, -1, -1);
2139 s
->cursor_offset
= cursor_offset
;
2140 s
->cursor_start
= s
->cr
[VGA_CRTC_CURSOR_START
];
2141 s
->cursor_end
= s
->cr
[VGA_CRTC_CURSOR_END
];
2144 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2148 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2149 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2151 dpy_text_update(s
->con
, 0, 0, width
, height
);
2155 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2156 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2164 for (; i
< size
; src
++, dst
++, i
++) {
2165 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2172 if (c_min
<= c_max
) {
2173 i
= TEXTMODE_Y(c_min
);
2174 dpy_text_update(s
->con
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2183 s
->get_resolution(s
, &width
, &height
);
2184 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2192 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2196 /* Display a message */
2198 s
->last_height
= height
= 3;
2199 dpy_text_cursor(s
->con
, -1, -1);
2200 dpy_text_resize(s
->con
, s
->last_width
, height
);
2202 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2203 console_write_ch(dst
++, ' ');
2205 size
= strlen(msg_buffer
);
2206 width
= (s
->last_width
- size
) / 2;
2207 dst
= chardata
+ s
->last_width
+ width
;
2208 for (i
= 0; i
< size
; i
++)
2209 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2211 dpy_text_update(s
->con
, 0, 0, s
->last_width
, height
);
2214 static uint64_t vga_mem_read(void *opaque
, hwaddr addr
,
2217 VGACommonState
*s
= opaque
;
2219 return vga_mem_readb(s
, addr
);
2222 static void vga_mem_write(void *opaque
, hwaddr addr
,
2223 uint64_t data
, unsigned size
)
2225 VGACommonState
*s
= opaque
;
2227 return vga_mem_writeb(s
, addr
, data
);
2230 const MemoryRegionOps vga_mem_ops
= {
2231 .read
= vga_mem_read
,
2232 .write
= vga_mem_write
,
2233 .endianness
= DEVICE_LITTLE_ENDIAN
,
2235 .min_access_size
= 1,
2236 .max_access_size
= 1,
2240 static int vga_common_post_load(void *opaque
, int version_id
)
2242 VGACommonState
*s
= opaque
;
2245 s
->graphic_mode
= -1;
2249 const VMStateDescription vmstate_vga_common
= {
2252 .minimum_version_id
= 2,
2253 .post_load
= vga_common_post_load
,
2254 .fields
= (VMStateField
[]) {
2255 VMSTATE_UINT32(latch
, VGACommonState
),
2256 VMSTATE_UINT8(sr_index
, VGACommonState
),
2257 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2258 VMSTATE_UINT8(gr_index
, VGACommonState
),
2259 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2260 VMSTATE_UINT8(ar_index
, VGACommonState
),
2261 VMSTATE_BUFFER(ar
, VGACommonState
),
2262 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2263 VMSTATE_UINT8(cr_index
, VGACommonState
),
2264 VMSTATE_BUFFER(cr
, VGACommonState
),
2265 VMSTATE_UINT8(msr
, VGACommonState
),
2266 VMSTATE_UINT8(fcr
, VGACommonState
),
2267 VMSTATE_UINT8(st00
, VGACommonState
),
2268 VMSTATE_UINT8(st01
, VGACommonState
),
2270 VMSTATE_UINT8(dac_state
, VGACommonState
),
2271 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2272 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2273 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2274 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2275 VMSTATE_BUFFER(palette
, VGACommonState
),
2277 VMSTATE_INT32(bank_offset
, VGACommonState
),
2278 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2279 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2280 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2281 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2282 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2283 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2284 VMSTATE_END_OF_LIST()
2288 static const GraphicHwOps vga_ops
= {
2289 .invalidate
= vga_invalidate_display
,
2290 .gfx_update
= vga_update_display
,
2291 .text_update
= vga_update_text
,
2294 void vga_common_init(VGACommonState
*s
, Object
*obj
, bool global_vmstate
)
2298 for(i
= 0;i
< 256; i
++) {
2300 for(j
= 0; j
< 8; j
++) {
2301 v
|= ((i
>> j
) & 1) << (j
* 4);
2306 for(j
= 0; j
< 4; j
++) {
2307 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2311 for(i
= 0; i
< 16; i
++) {
2313 for(j
= 0; j
< 4; j
++) {
2316 v
|= b
<< (2 * j
+ 1);
2321 /* valid range: 1 MB -> 256 MB */
2322 s
->vram_size
= 1024 * 1024;
2323 while (s
->vram_size
< (s
->vram_size_mb
<< 20) &&
2324 s
->vram_size
< (256 << 20)) {
2327 s
->vram_size_mb
= s
->vram_size
>> 20;
2329 s
->vbe_size
= s
->vram_size
;
2332 s
->is_vbe_vmstate
= 1;
2333 memory_region_init_ram(&s
->vram
, obj
, "vga.vram", s
->vram_size
,
2335 vmstate_register_ram(&s
->vram
, global_vmstate
? NULL
: DEVICE(obj
));
2336 xen_register_framebuffer(&s
->vram
);
2337 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2338 s
->get_bpp
= vga_get_bpp
;
2339 s
->get_offsets
= vga_get_offsets
;
2340 s
->get_resolution
= vga_get_resolution
;
2341 s
->hw_ops
= &vga_ops
;
2342 switch (vga_retrace_method
) {
2343 case VGA_RETRACE_DUMB
:
2344 s
->retrace
= vga_dumb_retrace
;
2345 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2348 case VGA_RETRACE_PRECISE
:
2349 s
->retrace
= vga_precise_retrace
;
2350 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2353 vga_dirty_log_start(s
);
2356 static const MemoryRegionPortio vga_portio_list
[] = {
2357 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2358 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2359 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2360 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2361 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2362 PORTIO_END_OF_LIST(),
2365 static const MemoryRegionPortio vbe_portio_list
[] = {
2366 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2368 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2370 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2371 PORTIO_END_OF_LIST(),
2374 /* Used by both ISA and PCI */
2375 MemoryRegion
*vga_init_io(VGACommonState
*s
, Object
*obj
,
2376 const MemoryRegionPortio
**vga_ports
,
2377 const MemoryRegionPortio
**vbe_ports
)
2379 MemoryRegion
*vga_mem
;
2381 *vga_ports
= vga_portio_list
;
2382 *vbe_ports
= vbe_portio_list
;
2384 vga_mem
= g_malloc(sizeof(*vga_mem
));
2385 memory_region_init_io(vga_mem
, obj
, &vga_mem_ops
, s
,
2386 "vga-lowmem", 0x20000);
2387 memory_region_set_flush_coalesced(vga_mem
);
2392 void vga_init(VGACommonState
*s
, Object
*obj
, MemoryRegion
*address_space
,
2393 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2395 MemoryRegion
*vga_io_memory
;
2396 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2398 qemu_register_reset(vga_reset
, s
);
2402 s
->legacy_address_space
= address_space
;
2404 vga_io_memory
= vga_init_io(s
, obj
, &vga_ports
, &vbe_ports
);
2405 memory_region_add_subregion_overlap(address_space
,
2406 isa_mem_base
+ 0x000a0000,
2409 memory_region_set_coalescing(vga_io_memory
);
2410 if (init_vga_ports
) {
2411 portio_list_init(&s
->vga_port_list
, obj
, vga_ports
, s
, "vga");
2412 portio_list_set_flush_coalesced(&s
->vga_port_list
);
2413 portio_list_add(&s
->vga_port_list
, address_space_io
, 0x3b0);
2416 portio_list_init(&s
->vbe_port_list
, obj
, vbe_ports
, s
, "vbe");
2417 portio_list_add(&s
->vbe_port_list
, address_space_io
, 0x1ce);
2421 void vga_init_vbe(VGACommonState
*s
, Object
*obj
, MemoryRegion
*system_memory
)
2423 /* With pc-0.12 and below we map both the PCI BAR and the fixed VBE region,
2424 * so use an alias to avoid double-mapping the same region.
2426 memory_region_init_alias(&s
->vram_vbe
, obj
, "vram.vbe",
2427 &s
->vram
, 0, memory_region_size(&s
->vram
));
2428 /* XXX: use optimized standard vga accesses */
2429 memory_region_add_subregion(system_memory
,
2430 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,