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
29 #include "pixel_ops.h"
30 #include "qemu-timer.h"
33 //#define DEBUG_VGA_MEM
34 //#define DEBUG_VGA_REG
36 //#define DEBUG_BOCHS_VBE
38 /* force some bits to zero */
39 const uint8_t sr_mask
[8] = {
50 const uint8_t gr_mask
[16] = {
69 #define cbswap_32(__x) \
71 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
72 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
73 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
74 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
76 #ifdef HOST_WORDS_BIGENDIAN
77 #define PAT(x) cbswap_32(x)
82 #ifdef HOST_WORDS_BIGENDIAN
88 #ifdef HOST_WORDS_BIGENDIAN
89 #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
91 #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
94 static const uint32_t mask16
[16] = {
115 #ifdef HOST_WORDS_BIGENDIAN
118 #define PAT(x) cbswap_32(x)
121 static const uint32_t dmask16
[16] = {
140 static const uint32_t dmask4
[4] = {
147 static uint32_t expand4
[256];
148 static uint16_t expand2
[256];
149 static uint8_t expand4to8
[16];
151 static void vga_screen_dump(void *opaque
, const char *filename
);
152 static const char *screen_dump_filename
;
153 static DisplayChangeListener
*screen_dump_dcl
;
155 static void vga_update_memory_access(VGACommonState
*s
)
157 MemoryRegion
*region
, *old_region
= s
->chain4_alias
;
158 target_phys_addr_t base
, offset
, size
;
160 s
->chain4_alias
= NULL
;
162 if ((s
->sr
[0x02] & 0xf) == 0xf && s
->sr
[0x04] & 0x08) {
164 switch ((s
->gr
[6] >> 2) & 3) {
172 offset
= s
->bank_offset
;
184 base
+= isa_mem_base
;
185 region
= g_malloc(sizeof(*region
));
186 memory_region_init_alias(region
, "vga.chain4", &s
->vram
, offset
, size
);
187 memory_region_add_subregion_overlap(s
->legacy_address_space
, base
,
189 s
->chain4_alias
= region
;
192 memory_region_del_subregion(s
->legacy_address_space
, old_region
);
193 memory_region_destroy(old_region
);
195 s
->plane_updated
= 0xf;
199 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
204 static void vga_precise_update_retrace_info(VGACommonState
*s
)
207 int hretr_start_char
;
208 int hretr_skew_chars
;
212 int vretr_start_line
;
221 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
222 int64_t chars_per_sec
;
223 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
225 htotal_chars
= s
->cr
[0x00] + 5;
226 hretr_start_char
= s
->cr
[0x04];
227 hretr_skew_chars
= (s
->cr
[0x05] >> 5) & 3;
228 hretr_end_char
= s
->cr
[0x05] & 0x1f;
230 vtotal_lines
= (s
->cr
[0x06]
231 | (((s
->cr
[0x07] & 1) | ((s
->cr
[0x07] >> 4) & 2)) << 8)) + 2
233 vretr_start_line
= s
->cr
[0x10]
234 | ((((s
->cr
[0x07] >> 2) & 1) | ((s
->cr
[0x07] >> 6) & 2)) << 8)
236 vretr_end_line
= s
->cr
[0x11] & 0xf;
240 clocking_mode
= (s
->sr
[0x01] >> 3) & 1;
241 clock_sel
= (s
->msr
>> 2) & 3;
242 dots
= (s
->msr
& 1) ? 8 : 9;
244 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
246 htotal_chars
<<= clocking_mode
;
248 r
->total_chars
= vtotal_lines
* htotal_chars
;
250 r
->ticks_per_char
= get_ticks_per_sec() / (r
->total_chars
* r
->freq
);
252 r
->ticks_per_char
= get_ticks_per_sec() / chars_per_sec
;
255 r
->vstart
= vretr_start_line
;
256 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
258 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
259 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
260 r
->htotal
= htotal_chars
;
263 div2
= (s
->cr
[0x17] >> 2) & 1;
264 sldiv2
= (s
->cr
[0x17] >> 3) & 1;
274 "div2 = %d sldiv2 = %d\n"
275 "clocking_mode = %d\n"
276 "clock_sel = %d %d\n"
278 "ticks/char = %" PRId64
"\n"
280 (double) get_ticks_per_sec() / (r
->ticks_per_char
* r
->total_chars
),
298 static uint8_t vga_precise_retrace(VGACommonState
*s
)
300 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
301 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
303 if (r
->total_chars
) {
304 int cur_line
, cur_line_char
, cur_char
;
307 cur_tick
= qemu_get_clock_ns(vm_clock
);
309 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
310 cur_line
= cur_char
/ r
->htotal
;
312 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
313 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
315 cur_line_char
= cur_char
% r
->htotal
;
316 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
317 val
|= ST01_DISP_ENABLE
;
323 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
327 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
329 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
332 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
334 if (s
->msr
& MSR_COLOR_EMULATION
) {
336 return (addr
>= 0x3b0 && addr
<= 0x3bf);
339 return (addr
>= 0x3d0 && addr
<= 0x3df);
343 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
345 VGACommonState
*s
= opaque
;
348 if (vga_ioport_invalid(s
, addr
)) {
353 if (s
->ar_flip_flop
== 0) {
360 index
= s
->ar_index
& 0x1f;
373 val
= s
->sr
[s
->sr_index
];
375 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
382 val
= s
->dac_write_index
;
385 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
386 if (++s
->dac_sub_index
== 3) {
387 s
->dac_sub_index
= 0;
401 val
= s
->gr
[s
->gr_index
];
403 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
412 val
= s
->cr
[s
->cr_index
];
414 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
419 /* just toggle to fool polling */
420 val
= s
->st01
= s
->retrace(s
);
428 #if defined(DEBUG_VGA)
429 printf("VGA: read addr=0x%04x data=0x%02x\n", addr
, val
);
434 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
436 VGACommonState
*s
= opaque
;
439 /* check port range access depending on color/monochrome mode */
440 if (vga_ioport_invalid(s
, addr
)) {
444 printf("VGA: write addr=0x%04x data=0x%02x\n", addr
, val
);
449 if (s
->ar_flip_flop
== 0) {
453 index
= s
->ar_index
& 0x1f;
456 s
->ar
[index
] = val
& 0x3f;
459 s
->ar
[index
] = val
& ~0x10;
465 s
->ar
[index
] = val
& ~0xc0;
468 s
->ar
[index
] = val
& ~0xf0;
471 s
->ar
[index
] = val
& ~0xf0;
477 s
->ar_flip_flop
^= 1;
480 s
->msr
= val
& ~0x10;
481 s
->update_retrace_info(s
);
484 s
->sr_index
= val
& 7;
488 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
490 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
491 if (s
->sr_index
== 1) s
->update_retrace_info(s
);
492 vga_update_memory_access(s
);
495 s
->dac_read_index
= val
;
496 s
->dac_sub_index
= 0;
500 s
->dac_write_index
= val
;
501 s
->dac_sub_index
= 0;
505 s
->dac_cache
[s
->dac_sub_index
] = val
;
506 if (++s
->dac_sub_index
== 3) {
507 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
508 s
->dac_sub_index
= 0;
509 s
->dac_write_index
++;
513 s
->gr_index
= val
& 0x0f;
517 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
519 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
520 vga_update_memory_access(s
);
529 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
531 /* handle CR0-7 protection */
532 if ((s
->cr
[0x11] & 0x80) && s
->cr_index
<= 7) {
533 /* can always write bit 4 of CR7 */
534 if (s
->cr_index
== 7)
535 s
->cr
[7] = (s
->cr
[7] & ~0x10) | (val
& 0x10);
538 s
->cr
[s
->cr_index
] = val
;
540 switch(s
->cr_index
) {
548 s
->update_retrace_info(s
);
559 #ifdef CONFIG_BOCHS_VBE
560 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
562 VGACommonState
*s
= opaque
;
568 static uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
570 VGACommonState
*s
= opaque
;
573 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
574 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
575 switch(s
->vbe_index
) {
576 /* XXX: do not hardcode ? */
577 case VBE_DISPI_INDEX_XRES
:
578 val
= VBE_DISPI_MAX_XRES
;
580 case VBE_DISPI_INDEX_YRES
:
581 val
= VBE_DISPI_MAX_YRES
;
583 case VBE_DISPI_INDEX_BPP
:
584 val
= VBE_DISPI_MAX_BPP
;
587 val
= s
->vbe_regs
[s
->vbe_index
];
591 val
= s
->vbe_regs
[s
->vbe_index
];
593 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
594 val
= s
->vram_size
/ (64 * 1024);
598 #ifdef DEBUG_BOCHS_VBE
599 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
604 static void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
606 VGACommonState
*s
= opaque
;
610 static void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
612 VGACommonState
*s
= opaque
;
614 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
615 #ifdef DEBUG_BOCHS_VBE
616 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
618 switch(s
->vbe_index
) {
619 case VBE_DISPI_INDEX_ID
:
620 if (val
== VBE_DISPI_ID0
||
621 val
== VBE_DISPI_ID1
||
622 val
== VBE_DISPI_ID2
||
623 val
== VBE_DISPI_ID3
||
624 val
== VBE_DISPI_ID4
) {
625 s
->vbe_regs
[s
->vbe_index
] = val
;
628 case VBE_DISPI_INDEX_XRES
:
629 if ((val
<= VBE_DISPI_MAX_XRES
) && ((val
& 7) == 0)) {
630 s
->vbe_regs
[s
->vbe_index
] = val
;
633 case VBE_DISPI_INDEX_YRES
:
634 if (val
<= VBE_DISPI_MAX_YRES
) {
635 s
->vbe_regs
[s
->vbe_index
] = val
;
638 case VBE_DISPI_INDEX_BPP
:
641 if (val
== 4 || val
== 8 || val
== 15 ||
642 val
== 16 || val
== 24 || val
== 32) {
643 s
->vbe_regs
[s
->vbe_index
] = val
;
646 case VBE_DISPI_INDEX_BANK
:
647 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
648 val
&= (s
->vbe_bank_mask
>> 2);
650 val
&= s
->vbe_bank_mask
;
652 s
->vbe_regs
[s
->vbe_index
] = val
;
653 s
->bank_offset
= (val
<< 16);
654 vga_update_memory_access(s
);
656 case VBE_DISPI_INDEX_ENABLE
:
657 if ((val
& VBE_DISPI_ENABLED
) &&
658 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
659 int h
, shift_control
;
661 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] =
662 s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
663 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] =
664 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
665 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
666 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
668 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
669 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 1;
671 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] *
672 ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
673 s
->vbe_start_addr
= 0;
675 /* clear the screen (should be done in BIOS) */
676 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
677 memset(s
->vram_ptr
, 0,
678 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
681 /* we initialize the VGA graphic mode (should be done
683 s
->gr
[0x06] = (s
->gr
[0x06] & ~0x0c) | 0x05; /* graphic mode + memory map 1 */
684 s
->cr
[0x17] |= 3; /* no CGA modes */
685 s
->cr
[0x13] = s
->vbe_line_offset
>> 3;
687 s
->cr
[0x01] = (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
688 /* height (only meaningful if < 1024) */
689 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
691 s
->cr
[0x07] = (s
->cr
[0x07] & ~0x42) |
692 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
693 /* line compare to 1023 */
698 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
700 s
->sr
[0x01] &= ~8; /* no double line */
703 s
->sr
[4] |= 0x08; /* set chain 4 mode */
704 s
->sr
[2] |= 0x0f; /* activate all planes */
706 s
->gr
[0x05] = (s
->gr
[0x05] & ~0x60) | (shift_control
<< 5);
707 s
->cr
[0x09] &= ~0x9f; /* no double scan */
709 /* XXX: the bios should do that */
712 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
713 s
->vbe_regs
[s
->vbe_index
] = val
;
714 vga_update_memory_access(s
);
716 case VBE_DISPI_INDEX_VIRT_WIDTH
:
718 int w
, h
, line_offset
;
720 if (val
< s
->vbe_regs
[VBE_DISPI_INDEX_XRES
])
723 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
724 line_offset
= w
>> 1;
726 line_offset
= w
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
727 h
= s
->vram_size
/ line_offset
;
728 /* XXX: support weird bochs semantics ? */
729 if (h
< s
->vbe_regs
[VBE_DISPI_INDEX_YRES
])
731 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = w
;
732 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = h
;
733 s
->vbe_line_offset
= line_offset
;
736 case VBE_DISPI_INDEX_X_OFFSET
:
737 case VBE_DISPI_INDEX_Y_OFFSET
:
740 s
->vbe_regs
[s
->vbe_index
] = val
;
741 s
->vbe_start_addr
= s
->vbe_line_offset
* s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
];
742 x
= s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
];
743 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
744 s
->vbe_start_addr
+= x
>> 1;
746 s
->vbe_start_addr
+= x
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
747 s
->vbe_start_addr
>>= 2;
757 /* called for accesses between 0xa0000 and 0xc0000 */
758 uint32_t vga_mem_readb(VGACommonState
*s
, target_phys_addr_t addr
)
760 int memory_map_mode
, plane
;
763 /* convert to VGA memory offset */
764 memory_map_mode
= (s
->gr
[6] >> 2) & 3;
766 switch(memory_map_mode
) {
772 addr
+= s
->bank_offset
;
787 if (s
->sr
[4] & 0x08) {
788 /* chain 4 mode : simplest access */
789 ret
= s
->vram_ptr
[addr
];
790 } else if (s
->gr
[5] & 0x10) {
791 /* odd/even mode (aka text mode mapping) */
792 plane
= (s
->gr
[4] & 2) | (addr
& 1);
793 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
795 /* standard VGA latched access */
796 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
798 if (!(s
->gr
[5] & 0x08)) {
801 ret
= GET_PLANE(s
->latch
, plane
);
804 ret
= (s
->latch
^ mask16
[s
->gr
[2]]) & mask16
[s
->gr
[7]];
813 /* called for accesses between 0xa0000 and 0xc0000 */
814 void vga_mem_writeb(VGACommonState
*s
, target_phys_addr_t addr
, uint32_t val
)
816 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
817 uint32_t write_mask
, bit_mask
, set_mask
;
820 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
822 /* convert to VGA memory offset */
823 memory_map_mode
= (s
->gr
[6] >> 2) & 3;
825 switch(memory_map_mode
) {
831 addr
+= s
->bank_offset
;
846 if (s
->sr
[4] & 0x08) {
847 /* chain 4 mode : simplest access */
850 if (s
->sr
[2] & mask
) {
851 s
->vram_ptr
[addr
] = val
;
853 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
855 s
->plane_updated
|= mask
; /* only used to detect font change */
856 memory_region_set_dirty(&s
->vram
, addr
);
858 } else if (s
->gr
[5] & 0x10) {
859 /* odd/even mode (aka text mode mapping) */
860 plane
= (s
->gr
[4] & 2) | (addr
& 1);
862 if (s
->sr
[2] & mask
) {
863 addr
= ((addr
& ~1) << 1) | plane
;
864 s
->vram_ptr
[addr
] = val
;
866 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
868 s
->plane_updated
|= mask
; /* only used to detect font change */
869 memory_region_set_dirty(&s
->vram
, addr
);
872 /* standard VGA latched access */
873 write_mode
= s
->gr
[5] & 3;
879 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
883 /* apply set/reset mask */
884 set_mask
= mask16
[s
->gr
[1]];
885 val
= (val
& ~set_mask
) | (mask16
[s
->gr
[0]] & set_mask
);
892 val
= mask16
[val
& 0x0f];
898 val
= (val
>> b
) | (val
<< (8 - b
));
900 bit_mask
= s
->gr
[8] & val
;
901 val
= mask16
[s
->gr
[0]];
905 /* apply logical operation */
906 func_select
= s
->gr
[3] >> 3;
907 switch(func_select
) {
927 bit_mask
|= bit_mask
<< 8;
928 bit_mask
|= bit_mask
<< 16;
929 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
932 /* mask data according to sr[2] */
934 s
->plane_updated
|= mask
; /* only used to detect font change */
935 write_mask
= mask16
[mask
];
936 ((uint32_t *)s
->vram_ptr
)[addr
] =
937 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
940 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
941 addr
* 4, write_mask
, val
);
943 memory_region_set_dirty(&s
->vram
, addr
<< 2);
947 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
948 const uint8_t *font_ptr
, int h
,
949 uint32_t fgcol
, uint32_t bgcol
);
950 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
951 const uint8_t *font_ptr
, int h
,
952 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
953 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
954 const uint8_t *s
, int width
);
957 #include "vga_template.h"
960 #include "vga_template.h"
964 #include "vga_template.h"
967 #include "vga_template.h"
971 #include "vga_template.h"
974 #include "vga_template.h"
978 #include "vga_template.h"
980 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
983 col
= rgb_to_pixel8(r
, g
, b
);
989 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
992 col
= rgb_to_pixel15(r
, g
, b
);
997 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
1001 col
= rgb_to_pixel15bgr(r
, g
, b
);
1006 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
1009 col
= rgb_to_pixel16(r
, g
, b
);
1014 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
1018 col
= rgb_to_pixel16bgr(r
, g
, b
);
1023 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
1026 col
= rgb_to_pixel32(r
, g
, b
);
1030 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
1033 col
= rgb_to_pixel32bgr(r
, g
, b
);
1037 /* return true if the palette was modified */
1038 static int update_palette16(VGACommonState
*s
)
1041 uint32_t v
, col
, *palette
;
1044 palette
= s
->last_palette
;
1045 for(i
= 0; i
< 16; i
++) {
1047 if (s
->ar
[0x10] & 0x80)
1048 v
= ((s
->ar
[0x14] & 0xf) << 4) | (v
& 0xf);
1050 v
= ((s
->ar
[0x14] & 0xc) << 4) | (v
& 0x3f);
1052 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1053 c6_to_8(s
->palette
[v
+ 1]),
1054 c6_to_8(s
->palette
[v
+ 2]));
1055 if (col
!= palette
[i
]) {
1063 /* return true if the palette was modified */
1064 static int update_palette256(VGACommonState
*s
)
1067 uint32_t v
, col
, *palette
;
1070 palette
= s
->last_palette
;
1072 for(i
= 0; i
< 256; i
++) {
1074 col
= s
->rgb_to_pixel(s
->palette
[v
],
1078 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1079 c6_to_8(s
->palette
[v
+ 1]),
1080 c6_to_8(s
->palette
[v
+ 2]));
1082 if (col
!= palette
[i
]) {
1091 static void vga_get_offsets(VGACommonState
*s
,
1092 uint32_t *pline_offset
,
1093 uint32_t *pstart_addr
,
1094 uint32_t *pline_compare
)
1096 uint32_t start_addr
, line_offset
, line_compare
;
1097 #ifdef CONFIG_BOCHS_VBE
1098 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1099 line_offset
= s
->vbe_line_offset
;
1100 start_addr
= s
->vbe_start_addr
;
1101 line_compare
= 65535;
1105 /* compute line_offset in bytes */
1106 line_offset
= s
->cr
[0x13];
1109 /* starting address */
1110 start_addr
= s
->cr
[0x0d] | (s
->cr
[0x0c] << 8);
1113 line_compare
= s
->cr
[0x18] |
1114 ((s
->cr
[0x07] & 0x10) << 4) |
1115 ((s
->cr
[0x09] & 0x40) << 3);
1117 *pline_offset
= line_offset
;
1118 *pstart_addr
= start_addr
;
1119 *pline_compare
= line_compare
;
1122 /* update start_addr and line_offset. Return TRUE if modified */
1123 static int update_basic_params(VGACommonState
*s
)
1126 uint32_t start_addr
, line_offset
, line_compare
;
1130 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1132 if (line_offset
!= s
->line_offset
||
1133 start_addr
!= s
->start_addr
||
1134 line_compare
!= s
->line_compare
) {
1135 s
->line_offset
= line_offset
;
1136 s
->start_addr
= start_addr
;
1137 s
->line_compare
= line_compare
;
1145 static inline int get_depth_index(DisplayState
*s
)
1147 switch(ds_get_bits_per_pixel(s
)) {
1156 if (is_surface_bgr(s
->surface
))
1163 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1173 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1175 vga_draw_glyph16_16
,
1176 vga_draw_glyph16_16
,
1177 vga_draw_glyph16_32
,
1178 vga_draw_glyph16_32
,
1179 vga_draw_glyph16_16
,
1180 vga_draw_glyph16_16
,
1183 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1193 static const uint8_t cursor_glyph
[32 * 4] = {
1194 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1195 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1196 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1197 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1198 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1199 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1200 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1202 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1203 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1204 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1205 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1206 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1207 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1208 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1209 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1212 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1213 int *pcwidth
, int *pcheight
)
1215 int width
, cwidth
, height
, cheight
;
1217 /* total width & height */
1218 cheight
= (s
->cr
[9] & 0x1f) + 1;
1220 if (!(s
->sr
[1] & 0x01))
1222 if (s
->sr
[1] & 0x08)
1223 cwidth
= 16; /* NOTE: no 18 pixel wide */
1224 width
= (s
->cr
[0x01] + 1);
1225 if (s
->cr
[0x06] == 100) {
1226 /* ugly hack for CGA 160x100x16 - explain me the logic */
1229 height
= s
->cr
[0x12] |
1230 ((s
->cr
[0x07] & 0x02) << 7) |
1231 ((s
->cr
[0x07] & 0x40) << 3);
1232 height
= (height
+ 1) / cheight
;
1238 *pcheight
= cheight
;
1241 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1243 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1248 rgb_to_pixel32bgr_dup
,
1249 rgb_to_pixel15bgr_dup
,
1250 rgb_to_pixel16bgr_dup
,
1261 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1263 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1264 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1265 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1266 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1267 const uint8_t *font_ptr
, *font_base
[2];
1268 int dup9
, line_offset
, depth_index
;
1270 uint32_t *ch_attr_ptr
;
1271 vga_draw_glyph8_func
*vga_draw_glyph8
;
1272 vga_draw_glyph9_func
*vga_draw_glyph9
;
1274 /* compute font data address (in plane 2) */
1276 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1277 if (offset
!= s
->font_offsets
[0]) {
1278 s
->font_offsets
[0] = offset
;
1281 font_base
[0] = s
->vram_ptr
+ offset
;
1283 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1284 font_base
[1] = s
->vram_ptr
+ offset
;
1285 if (offset
!= s
->font_offsets
[1]) {
1286 s
->font_offsets
[1] = offset
;
1289 if (s
->plane_updated
& (1 << 2) || s
->chain4_alias
) {
1290 /* if the plane 2 was modified since the last display, it
1291 indicates the font may have been modified */
1292 s
->plane_updated
= 0;
1295 full_update
|= update_basic_params(s
);
1297 line_offset
= s
->line_offset
;
1299 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1300 if ((height
* width
) > CH_ATTR_SIZE
) {
1301 /* better than nothing: exit if transient size is too big */
1305 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1306 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1307 s
->last_scr_width
= width
* cw
;
1308 s
->last_scr_height
= height
* cheight
;
1309 qemu_console_resize(s
->ds
, s
->last_scr_width
, s
->last_scr_height
);
1311 s
->last_width
= width
;
1312 s
->last_height
= height
;
1313 s
->last_ch
= cheight
;
1318 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1319 full_update
|= update_palette16(s
);
1320 palette
= s
->last_palette
;
1321 x_incr
= cw
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1323 cursor_offset
= ((s
->cr
[0x0e] << 8) | s
->cr
[0x0f]) - s
->start_addr
;
1324 if (cursor_offset
!= s
->cursor_offset
||
1325 s
->cr
[0xa] != s
->cursor_start
||
1326 s
->cr
[0xb] != s
->cursor_end
) {
1327 /* if the cursor position changed, we update the old and new
1329 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1330 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1331 if (cursor_offset
< CH_ATTR_SIZE
)
1332 s
->last_ch_attr
[cursor_offset
] = -1;
1333 s
->cursor_offset
= cursor_offset
;
1334 s
->cursor_start
= s
->cr
[0xa];
1335 s
->cursor_end
= s
->cr
[0xb];
1337 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1339 depth_index
= get_depth_index(s
->ds
);
1341 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1343 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1344 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1346 dest
= ds_get_data(s
->ds
);
1347 linesize
= ds_get_linesize(s
->ds
);
1348 ch_attr_ptr
= s
->last_ch_attr
;
1350 offset
= s
->start_addr
* 4;
1351 for(cy
= 0; cy
< height
; cy
++) {
1353 src
= s
->vram_ptr
+ offset
;
1356 for(cx
= 0; cx
< width
; cx
++) {
1357 ch_attr
= *(uint16_t *)src
;
1358 if (full_update
|| ch_attr
!= *ch_attr_ptr
) {
1363 *ch_attr_ptr
= ch_attr
;
1364 #ifdef HOST_WORDS_BIGENDIAN
1366 cattr
= ch_attr
& 0xff;
1368 ch
= ch_attr
& 0xff;
1369 cattr
= ch_attr
>> 8;
1371 font_ptr
= font_base
[(cattr
>> 3) & 1];
1372 font_ptr
+= 32 * 4 * ch
;
1373 bgcol
= palette
[cattr
>> 4];
1374 fgcol
= palette
[cattr
& 0x0f];
1376 vga_draw_glyph8(d1
, linesize
,
1377 font_ptr
, cheight
, fgcol
, bgcol
);
1380 if (ch
>= 0xb0 && ch
<= 0xdf && (s
->ar
[0x10] & 0x04))
1382 vga_draw_glyph9(d1
, linesize
,
1383 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1385 if (src
== cursor_ptr
&&
1386 !(s
->cr
[0x0a] & 0x20)) {
1387 int line_start
, line_last
, h
;
1388 /* draw the cursor */
1389 line_start
= s
->cr
[0x0a] & 0x1f;
1390 line_last
= s
->cr
[0x0b] & 0x1f;
1391 /* XXX: check that */
1392 if (line_last
> cheight
- 1)
1393 line_last
= cheight
- 1;
1394 if (line_last
>= line_start
&& line_start
< cheight
) {
1395 h
= line_last
- line_start
+ 1;
1396 d
= d1
+ linesize
* line_start
;
1398 vga_draw_glyph8(d
, linesize
,
1399 cursor_glyph
, h
, fgcol
, bgcol
);
1401 vga_draw_glyph9(d
, linesize
,
1402 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1412 dpy_update(s
->ds
, cx_min
* cw
, cy
* cheight
,
1413 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1415 dest
+= linesize
* cheight
;
1416 line1
= line
+ cheight
;
1417 offset
+= line_offset
;
1418 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1439 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1449 vga_draw_line2d2_16
,
1450 vga_draw_line2d2_16
,
1451 vga_draw_line2d2_32
,
1452 vga_draw_line2d2_32
,
1453 vga_draw_line2d2_16
,
1454 vga_draw_line2d2_16
,
1465 vga_draw_line4d2_16
,
1466 vga_draw_line4d2_16
,
1467 vga_draw_line4d2_32
,
1468 vga_draw_line4d2_32
,
1469 vga_draw_line4d2_16
,
1470 vga_draw_line4d2_16
,
1473 vga_draw_line8d2_16
,
1474 vga_draw_line8d2_16
,
1475 vga_draw_line8d2_32
,
1476 vga_draw_line8d2_32
,
1477 vga_draw_line8d2_16
,
1478 vga_draw_line8d2_16
,
1492 vga_draw_line15_32bgr
,
1493 vga_draw_line15_15bgr
,
1494 vga_draw_line15_16bgr
,
1500 vga_draw_line16_32bgr
,
1501 vga_draw_line16_15bgr
,
1502 vga_draw_line16_16bgr
,
1508 vga_draw_line24_32bgr
,
1509 vga_draw_line24_15bgr
,
1510 vga_draw_line24_16bgr
,
1516 vga_draw_line32_32bgr
,
1517 vga_draw_line32_15bgr
,
1518 vga_draw_line32_16bgr
,
1521 static int vga_get_bpp(VGACommonState
*s
)
1524 #ifdef CONFIG_BOCHS_VBE
1525 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1526 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1535 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1539 #ifdef CONFIG_BOCHS_VBE
1540 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1541 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1542 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1546 width
= (s
->cr
[0x01] + 1) * 8;
1547 height
= s
->cr
[0x12] |
1548 ((s
->cr
[0x07] & 0x02) << 7) |
1549 ((s
->cr
[0x07] & 0x40) << 3);
1550 height
= (height
+ 1);
1556 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1559 if (y1
>= VGA_MAX_HEIGHT
)
1561 if (y2
>= VGA_MAX_HEIGHT
)
1562 y2
= VGA_MAX_HEIGHT
;
1563 for(y
= y1
; y
< y2
; y
++) {
1564 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1568 static void vga_sync_dirty_bitmap(VGACommonState
*s
)
1570 memory_region_sync_dirty_bitmap(&s
->vram
);
1573 void vga_dirty_log_start(VGACommonState
*s
)
1575 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1578 void vga_dirty_log_stop(VGACommonState
*s
)
1580 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1586 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1588 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1589 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1590 ram_addr_t page0
, page1
, page_min
, page_max
;
1591 int disp_width
, multi_scan
, multi_run
;
1593 uint32_t v
, addr1
, addr
;
1594 vga_draw_line_func
*vga_draw_line
;
1596 full_update
|= update_basic_params(s
);
1599 vga_sync_dirty_bitmap(s
);
1601 s
->get_resolution(s
, &width
, &height
);
1604 shift_control
= (s
->gr
[0x05] >> 5) & 3;
1605 double_scan
= (s
->cr
[0x09] >> 7);
1606 if (shift_control
!= 1) {
1607 multi_scan
= (((s
->cr
[0x09] & 0x1f) + 1) << double_scan
) - 1;
1609 /* in CGA modes, multi_scan is ignored */
1610 /* XXX: is it correct ? */
1611 multi_scan
= double_scan
;
1613 multi_run
= multi_scan
;
1614 if (shift_control
!= s
->shift_control
||
1615 double_scan
!= s
->double_scan
) {
1617 s
->shift_control
= shift_control
;
1618 s
->double_scan
= double_scan
;
1621 if (shift_control
== 0) {
1622 if (s
->sr
[0x01] & 8) {
1625 } else if (shift_control
== 1) {
1626 if (s
->sr
[0x01] & 8) {
1631 depth
= s
->get_bpp(s
);
1632 if (s
->line_offset
!= s
->last_line_offset
||
1633 disp_width
!= s
->last_width
||
1634 height
!= s
->last_height
||
1635 s
->last_depth
!= depth
) {
1636 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1637 if (depth
== 16 || depth
== 32) {
1641 qemu_free_displaysurface(s
->ds
);
1642 s
->ds
->surface
= qemu_create_displaysurface_from(disp_width
, height
, depth
,
1644 s
->vram_ptr
+ (s
->start_addr
* 4));
1645 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
1646 s
->ds
->surface
->pf
= qemu_different_endianness_pixelformat(depth
);
1650 qemu_console_resize(s
->ds
, disp_width
, height
);
1652 s
->last_scr_width
= disp_width
;
1653 s
->last_scr_height
= height
;
1654 s
->last_width
= disp_width
;
1655 s
->last_height
= height
;
1656 s
->last_line_offset
= s
->line_offset
;
1657 s
->last_depth
= depth
;
1659 } else if (is_buffer_shared(s
->ds
->surface
) &&
1660 (full_update
|| s
->ds
->surface
->data
!= s
->vram_ptr
+ (s
->start_addr
* 4))) {
1661 s
->ds
->surface
->data
= s
->vram_ptr
+ (s
->start_addr
* 4);
1666 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1668 if (shift_control
== 0) {
1669 full_update
|= update_palette16(s
);
1670 if (s
->sr
[0x01] & 8) {
1671 v
= VGA_DRAW_LINE4D2
;
1676 } else if (shift_control
== 1) {
1677 full_update
|= update_palette16(s
);
1678 if (s
->sr
[0x01] & 8) {
1679 v
= VGA_DRAW_LINE2D2
;
1685 switch(s
->get_bpp(s
)) {
1688 full_update
|= update_palette256(s
);
1689 v
= VGA_DRAW_LINE8D2
;
1693 full_update
|= update_palette256(s
);
1698 v
= VGA_DRAW_LINE15
;
1702 v
= VGA_DRAW_LINE16
;
1706 v
= VGA_DRAW_LINE24
;
1710 v
= VGA_DRAW_LINE32
;
1715 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+ get_depth_index(s
->ds
)];
1717 if (!is_buffer_shared(s
->ds
->surface
) && s
->cursor_invalidate
)
1718 s
->cursor_invalidate(s
);
1720 line_offset
= s
->line_offset
;
1722 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",
1723 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[0x17], s
->line_compare
, s
->sr
[0x01]);
1725 addr1
= (s
->start_addr
* 4);
1726 bwidth
= (width
* bits
+ 7) / 8;
1730 d
= ds_get_data(s
->ds
);
1731 linesize
= ds_get_linesize(s
->ds
);
1733 for(y
= 0; y
< height
; y
++) {
1735 if (!(s
->cr
[0x17] & 1)) {
1737 /* CGA compatibility handling */
1738 shift
= 14 + ((s
->cr
[0x17] >> 6) & 1);
1739 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1741 if (!(s
->cr
[0x17] & 2)) {
1742 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1744 page0
= addr
& TARGET_PAGE_MASK
;
1745 page1
= (addr
+ bwidth
- 1) & TARGET_PAGE_MASK
;
1746 update
= full_update
|
1747 memory_region_get_dirty(&s
->vram
, page0
, DIRTY_MEMORY_VGA
) |
1748 memory_region_get_dirty(&s
->vram
, page1
, DIRTY_MEMORY_VGA
);
1749 if ((page1
- page0
) > TARGET_PAGE_SIZE
) {
1750 /* if wide line, can use another page */
1751 update
|= memory_region_get_dirty(&s
->vram
,
1752 page0
+ TARGET_PAGE_SIZE
,
1755 /* explicit invalidation for the hardware cursor */
1756 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1760 if (page0
< page_min
)
1762 if (page1
> page_max
)
1764 if (!(is_buffer_shared(s
->ds
->surface
))) {
1765 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1766 if (s
->cursor_draw_line
)
1767 s
->cursor_draw_line(s
, d
, y
);
1771 /* flush to display */
1772 dpy_update(s
->ds
, 0, y_start
,
1773 disp_width
, y
- y_start
);
1778 mask
= (s
->cr
[0x17] & 3) ^ 3;
1779 if ((y1
& mask
) == mask
)
1780 addr1
+= line_offset
;
1782 multi_run
= multi_scan
;
1786 /* line compare acts on the displayed lines */
1787 if (y
== s
->line_compare
)
1792 /* flush to display */
1793 dpy_update(s
->ds
, 0, y_start
,
1794 disp_width
, y
- y_start
);
1796 /* reset modified pages */
1797 if (page_max
>= page_min
) {
1798 memory_region_reset_dirty(&s
->vram
,
1800 page_max
+ TARGET_PAGE_SIZE
- page_min
,
1803 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1806 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1813 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1817 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1818 if (ds_get_bits_per_pixel(s
->ds
) == 8)
1819 val
= s
->rgb_to_pixel(0, 0, 0);
1822 w
= s
->last_scr_width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1823 d
= ds_get_data(s
->ds
);
1824 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1826 d
+= ds_get_linesize(s
->ds
);
1828 dpy_update(s
->ds
, 0, 0,
1829 s
->last_scr_width
, s
->last_scr_height
);
1832 #define GMODE_TEXT 0
1833 #define GMODE_GRAPH 1
1834 #define GMODE_BLANK 2
1836 static void vga_update_display(void *opaque
)
1838 VGACommonState
*s
= opaque
;
1839 int full_update
, graphic_mode
;
1841 if (ds_get_bits_per_pixel(s
->ds
) == 0) {
1845 if (!(s
->ar_index
& 0x20)) {
1846 graphic_mode
= GMODE_BLANK
;
1848 graphic_mode
= s
->gr
[6] & 1;
1850 if (graphic_mode
!= s
->graphic_mode
) {
1851 s
->graphic_mode
= graphic_mode
;
1854 switch(graphic_mode
) {
1856 vga_draw_text(s
, full_update
);
1859 vga_draw_graphic(s
, full_update
);
1863 vga_draw_blank(s
, full_update
);
1869 /* force a full display refresh */
1870 static void vga_invalidate_display(void *opaque
)
1872 VGACommonState
*s
= opaque
;
1875 s
->last_height
= -1;
1878 void vga_common_reset(VGACommonState
*s
)
1881 memset(s
->sr
, '\0', sizeof(s
->sr
));
1883 memset(s
->gr
, '\0', sizeof(s
->gr
));
1885 memset(s
->ar
, '\0', sizeof(s
->ar
));
1886 s
->ar_flip_flop
= 0;
1888 memset(s
->cr
, '\0', sizeof(s
->cr
));
1894 s
->dac_sub_index
= 0;
1895 s
->dac_read_index
= 0;
1896 s
->dac_write_index
= 0;
1897 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1899 memset(s
->palette
, '\0', sizeof(s
->palette
));
1901 #ifdef CONFIG_BOCHS_VBE
1903 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1904 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1905 s
->vbe_start_addr
= 0;
1906 s
->vbe_line_offset
= 0;
1907 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1909 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1910 s
->graphic_mode
= -1; /* force full update */
1911 s
->shift_control
= 0;
1914 s
->line_compare
= 0;
1916 s
->plane_updated
= 0;
1921 s
->last_scr_width
= 0;
1922 s
->last_scr_height
= 0;
1923 s
->cursor_start
= 0;
1925 s
->cursor_offset
= 0;
1926 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1927 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1928 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1929 switch (vga_retrace_method
) {
1930 case VGA_RETRACE_DUMB
:
1932 case VGA_RETRACE_PRECISE
:
1933 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1936 vga_update_memory_access(s
);
1939 static void vga_reset(void *opaque
)
1941 VGACommonState
*s
= opaque
;
1942 vga_common_reset(s
);
1945 #define TEXTMODE_X(x) ((x) % width)
1946 #define TEXTMODE_Y(x) ((x) / width)
1947 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1948 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1949 /* relay text rendering to the display driver
1950 * instead of doing a full vga_update_display() */
1951 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
1953 VGACommonState
*s
= opaque
;
1954 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
1955 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
1957 console_ch_t
*dst
, val
;
1958 char msg_buffer
[80];
1959 int full_update
= 0;
1961 if (!(s
->ar_index
& 0x20)) {
1962 graphic_mode
= GMODE_BLANK
;
1964 graphic_mode
= s
->gr
[6] & 1;
1966 if (graphic_mode
!= s
->graphic_mode
) {
1967 s
->graphic_mode
= graphic_mode
;
1970 if (s
->last_width
== -1) {
1975 switch (graphic_mode
) {
1977 /* TODO: update palette */
1978 full_update
|= update_basic_params(s
);
1980 /* total width & height */
1981 cheight
= (s
->cr
[9] & 0x1f) + 1;
1983 if (!(s
->sr
[1] & 0x01))
1985 if (s
->sr
[1] & 0x08)
1986 cw
= 16; /* NOTE: no 18 pixel wide */
1987 width
= (s
->cr
[0x01] + 1);
1988 if (s
->cr
[0x06] == 100) {
1989 /* ugly hack for CGA 160x100x16 - explain me the logic */
1992 height
= s
->cr
[0x12] |
1993 ((s
->cr
[0x07] & 0x02) << 7) |
1994 ((s
->cr
[0x07] & 0x40) << 3);
1995 height
= (height
+ 1) / cheight
;
1998 size
= (height
* width
);
1999 if (size
> CH_ATTR_SIZE
) {
2003 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
2008 if (width
!= s
->last_width
|| height
!= s
->last_height
||
2009 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
2010 s
->last_scr_width
= width
* cw
;
2011 s
->last_scr_height
= height
* cheight
;
2012 s
->ds
->surface
->width
= width
;
2013 s
->ds
->surface
->height
= height
;
2015 s
->last_width
= width
;
2016 s
->last_height
= height
;
2017 s
->last_ch
= cheight
;
2022 /* Update "hardware" cursor */
2023 cursor_offset
= ((s
->cr
[0x0e] << 8) | s
->cr
[0x0f]) - s
->start_addr
;
2024 if (cursor_offset
!= s
->cursor_offset
||
2025 s
->cr
[0xa] != s
->cursor_start
||
2026 s
->cr
[0xb] != s
->cursor_end
|| full_update
) {
2027 cursor_visible
= !(s
->cr
[0xa] & 0x20);
2028 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
2030 TEXTMODE_X(cursor_offset
),
2031 TEXTMODE_Y(cursor_offset
));
2033 dpy_cursor(s
->ds
, -1, -1);
2034 s
->cursor_offset
= cursor_offset
;
2035 s
->cursor_start
= s
->cr
[0xa];
2036 s
->cursor_end
= s
->cr
[0xb];
2039 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2043 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2044 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2046 dpy_update(s
->ds
, 0, 0, width
, height
);
2050 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2051 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2059 for (; i
< size
; src
++, dst
++, i
++) {
2060 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2067 if (c_min
<= c_max
) {
2068 i
= TEXTMODE_Y(c_min
);
2069 dpy_update(s
->ds
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2078 s
->get_resolution(s
, &width
, &height
);
2079 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2087 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2091 /* Display a message */
2093 s
->last_height
= height
= 3;
2094 dpy_cursor(s
->ds
, -1, -1);
2095 s
->ds
->surface
->width
= s
->last_width
;
2096 s
->ds
->surface
->height
= height
;
2099 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2100 console_write_ch(dst
++, ' ');
2102 size
= strlen(msg_buffer
);
2103 width
= (s
->last_width
- size
) / 2;
2104 dst
= chardata
+ s
->last_width
+ width
;
2105 for (i
= 0; i
< size
; i
++)
2106 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2108 dpy_update(s
->ds
, 0, 0, s
->last_width
, height
);
2111 static uint64_t vga_mem_read(void *opaque
, target_phys_addr_t addr
,
2114 VGACommonState
*s
= opaque
;
2116 return vga_mem_readb(s
, addr
);
2119 static void vga_mem_write(void *opaque
, target_phys_addr_t addr
,
2120 uint64_t data
, unsigned size
)
2122 VGACommonState
*s
= opaque
;
2124 return vga_mem_writeb(s
, addr
, data
);
2127 const MemoryRegionOps vga_mem_ops
= {
2128 .read
= vga_mem_read
,
2129 .write
= vga_mem_write
,
2130 .endianness
= DEVICE_LITTLE_ENDIAN
,
2132 .min_access_size
= 1,
2133 .max_access_size
= 1,
2137 static int vga_common_post_load(void *opaque
, int version_id
)
2139 VGACommonState
*s
= opaque
;
2142 s
->graphic_mode
= -1;
2146 const VMStateDescription vmstate_vga_common
= {
2149 .minimum_version_id
= 2,
2150 .minimum_version_id_old
= 2,
2151 .post_load
= vga_common_post_load
,
2152 .fields
= (VMStateField
[]) {
2153 VMSTATE_UINT32(latch
, VGACommonState
),
2154 VMSTATE_UINT8(sr_index
, VGACommonState
),
2155 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2156 VMSTATE_UINT8(gr_index
, VGACommonState
),
2157 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2158 VMSTATE_UINT8(ar_index
, VGACommonState
),
2159 VMSTATE_BUFFER(ar
, VGACommonState
),
2160 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2161 VMSTATE_UINT8(cr_index
, VGACommonState
),
2162 VMSTATE_BUFFER(cr
, VGACommonState
),
2163 VMSTATE_UINT8(msr
, VGACommonState
),
2164 VMSTATE_UINT8(fcr
, VGACommonState
),
2165 VMSTATE_UINT8(st00
, VGACommonState
),
2166 VMSTATE_UINT8(st01
, VGACommonState
),
2168 VMSTATE_UINT8(dac_state
, VGACommonState
),
2169 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2170 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2171 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2172 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2173 VMSTATE_BUFFER(palette
, VGACommonState
),
2175 VMSTATE_INT32(bank_offset
, VGACommonState
),
2176 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2177 #ifdef CONFIG_BOCHS_VBE
2178 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2179 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2180 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2181 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2182 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2184 VMSTATE_END_OF_LIST()
2188 void vga_common_init(VGACommonState
*s
, int vga_ram_size
)
2192 for(i
= 0;i
< 256; i
++) {
2194 for(j
= 0; j
< 8; j
++) {
2195 v
|= ((i
>> j
) & 1) << (j
* 4);
2200 for(j
= 0; j
< 4; j
++) {
2201 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2205 for(i
= 0; i
< 16; i
++) {
2207 for(j
= 0; j
< 4; j
++) {
2210 v
|= b
<< (2 * j
+ 1);
2215 #ifdef CONFIG_BOCHS_VBE
2216 s
->is_vbe_vmstate
= 1;
2218 s
->is_vbe_vmstate
= 0;
2220 memory_region_init_ram(&s
->vram
, NULL
, "vga.vram", vga_ram_size
);
2221 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2222 s
->vram_size
= vga_ram_size
;
2223 s
->get_bpp
= vga_get_bpp
;
2224 s
->get_offsets
= vga_get_offsets
;
2225 s
->get_resolution
= vga_get_resolution
;
2226 s
->update
= vga_update_display
;
2227 s
->invalidate
= vga_invalidate_display
;
2228 s
->screen_dump
= vga_screen_dump
;
2229 s
->text_update
= vga_update_text
;
2230 switch (vga_retrace_method
) {
2231 case VGA_RETRACE_DUMB
:
2232 s
->retrace
= vga_dumb_retrace
;
2233 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2236 case VGA_RETRACE_PRECISE
:
2237 s
->retrace
= vga_precise_retrace
;
2238 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2241 vga_dirty_log_start(s
);
2244 static const MemoryRegionPortio vga_portio_list
[] = {
2245 { 0x04, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3b4 */
2246 { 0x0a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3ba */
2247 { 0x10, 16, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3c0 */
2248 { 0x24, 2, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3d4 */
2249 { 0x2a, 1, 1, .read
= vga_ioport_read
, .write
= vga_ioport_write
}, /* 3da */
2250 PORTIO_END_OF_LIST(),
2253 #ifdef CONFIG_BOCHS_VBE
2254 static const MemoryRegionPortio vbe_portio_list
[] = {
2255 { 0, 1, 2, .read
= vbe_ioport_read_index
, .write
= vbe_ioport_write_index
},
2257 { 1, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2259 { 2, 1, 2, .read
= vbe_ioport_read_data
, .write
= vbe_ioport_write_data
},
2261 PORTIO_END_OF_LIST(),
2263 #endif /* CONFIG_BOCHS_VBE */
2265 /* Used by both ISA and PCI */
2266 MemoryRegion
*vga_init_io(VGACommonState
*s
,
2267 const MemoryRegionPortio
**vga_ports
,
2268 const MemoryRegionPortio
**vbe_ports
)
2270 MemoryRegion
*vga_mem
;
2272 *vga_ports
= vga_portio_list
;
2274 #ifdef CONFIG_BOCHS_VBE
2275 *vbe_ports
= vbe_portio_list
;
2278 vga_mem
= g_malloc(sizeof(*vga_mem
));
2279 memory_region_init_io(vga_mem
, &vga_mem_ops
, s
,
2280 "vga-lowmem", 0x20000);
2285 void vga_init(VGACommonState
*s
, MemoryRegion
*address_space
,
2286 MemoryRegion
*address_space_io
, bool init_vga_ports
)
2288 MemoryRegion
*vga_io_memory
;
2289 const MemoryRegionPortio
*vga_ports
, *vbe_ports
;
2290 PortioList
*vga_port_list
= g_new(PortioList
, 1);
2291 PortioList
*vbe_port_list
= g_new(PortioList
, 1);
2293 qemu_register_reset(vga_reset
, s
);
2297 s
->legacy_address_space
= address_space
;
2299 vga_io_memory
= vga_init_io(s
, &vga_ports
, &vbe_ports
);
2300 memory_region_add_subregion_overlap(address_space
,
2301 isa_mem_base
+ 0x000a0000,
2304 memory_region_set_coalescing(vga_io_memory
);
2305 if (init_vga_ports
) {
2306 portio_list_init(vga_port_list
, vga_ports
, s
, "vga");
2307 portio_list_add(vga_port_list
, address_space_io
, 0x3b0);
2310 portio_list_init(vbe_port_list
, vbe_ports
, s
, "vbe");
2311 portio_list_add(vbe_port_list
, address_space_io
, 0x1ce);
2315 void vga_init_vbe(VGACommonState
*s
, MemoryRegion
*system_memory
)
2317 #ifdef CONFIG_BOCHS_VBE
2318 /* XXX: use optimized standard vga accesses */
2319 memory_region_add_subregion(system_memory
,
2320 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,
2325 /********************************************************/
2326 /* vga screen dump */
2328 static void vga_save_dpy_update(DisplayState
*ds
,
2329 int x
, int y
, int w
, int h
)
2331 if (screen_dump_filename
) {
2332 ppm_save(screen_dump_filename
, ds
->surface
);
2336 static void vga_save_dpy_resize(DisplayState
*s
)
2340 static void vga_save_dpy_refresh(DisplayState
*s
)
2344 int ppm_save(const char *filename
, struct DisplaySurface
*ds
)
2352 char *linebuf
, *pbuf
;
2354 f
= fopen(filename
, "wb");
2357 fprintf(f
, "P6\n%d %d\n%d\n",
2358 ds
->width
, ds
->height
, 255);
2359 linebuf
= g_malloc(ds
->width
* 3);
2361 for(y
= 0; y
< ds
->height
; y
++) {
2364 for(x
= 0; x
< ds
->width
; x
++) {
2365 if (ds
->pf
.bits_per_pixel
== 32)
2368 v
= (uint32_t) (*(uint16_t *)d
);
2369 r
= ((v
>> ds
->pf
.rshift
) & ds
->pf
.rmax
) * 256 /
2371 g
= ((v
>> ds
->pf
.gshift
) & ds
->pf
.gmax
) * 256 /
2373 b
= ((v
>> ds
->pf
.bshift
) & ds
->pf
.bmax
) * 256 /
2378 d
+= ds
->pf
.bytes_per_pixel
;
2381 ret
= fwrite(linebuf
, 1, pbuf
- linebuf
, f
);
2389 static DisplayChangeListener
* vga_screen_dump_init(DisplayState
*ds
)
2391 DisplayChangeListener
*dcl
;
2393 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
2394 dcl
->dpy_update
= vga_save_dpy_update
;
2395 dcl
->dpy_resize
= vga_save_dpy_resize
;
2396 dcl
->dpy_refresh
= vga_save_dpy_refresh
;
2397 register_displaychangelistener(ds
, dcl
);
2401 /* save the vga display in a PPM image even if no display is
2403 static void vga_screen_dump(void *opaque
, const char *filename
)
2405 VGACommonState
*s
= opaque
;
2407 if (!screen_dump_dcl
)
2408 screen_dump_dcl
= vga_screen_dump_init(s
->ds
);
2410 screen_dump_filename
= filename
;
2411 vga_invalidate_display(s
);
2413 screen_dump_filename
= NULL
;