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 char *screen_dump_filename
;
153 static DisplayChangeListener
*screen_dump_dcl
;
155 static void vga_dumb_update_retrace_info(VGACommonState
*s
)
160 static void vga_precise_update_retrace_info(VGACommonState
*s
)
163 int hretr_start_char
;
164 int hretr_skew_chars
;
168 int vretr_start_line
;
177 const int clk_hz
[] = {25175000, 28322000, 25175000, 25175000};
178 int64_t chars_per_sec
;
179 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
181 htotal_chars
= s
->cr
[0x00] + 5;
182 hretr_start_char
= s
->cr
[0x04];
183 hretr_skew_chars
= (s
->cr
[0x05] >> 5) & 3;
184 hretr_end_char
= s
->cr
[0x05] & 0x1f;
186 vtotal_lines
= (s
->cr
[0x06]
187 | (((s
->cr
[0x07] & 1) | ((s
->cr
[0x07] >> 4) & 2)) << 8)) + 2
189 vretr_start_line
= s
->cr
[0x10]
190 | ((((s
->cr
[0x07] >> 2) & 1) | ((s
->cr
[0x07] >> 6) & 2)) << 8)
192 vretr_end_line
= s
->cr
[0x11] & 0xf;
196 clocking_mode
= (s
->sr
[0x01] >> 3) & 1;
197 clock_sel
= (s
->msr
>> 2) & 3;
198 dots
= (s
->msr
& 1) ? 8 : 9;
200 chars_per_sec
= clk_hz
[clock_sel
] / dots
;
202 htotal_chars
<<= clocking_mode
;
204 r
->total_chars
= vtotal_lines
* htotal_chars
;
206 r
->ticks_per_char
= get_ticks_per_sec() / (r
->total_chars
* r
->freq
);
208 r
->ticks_per_char
= get_ticks_per_sec() / chars_per_sec
;
211 r
->vstart
= vretr_start_line
;
212 r
->vend
= r
->vstart
+ vretr_end_line
+ 1;
214 r
->hstart
= hretr_start_char
+ hretr_skew_chars
;
215 r
->hend
= r
->hstart
+ hretr_end_char
+ 1;
216 r
->htotal
= htotal_chars
;
219 div2
= (s
->cr
[0x17] >> 2) & 1;
220 sldiv2
= (s
->cr
[0x17] >> 3) & 1;
230 "div2 = %d sldiv2 = %d\n"
231 "clocking_mode = %d\n"
232 "clock_sel = %d %d\n"
234 "ticks/char = %" PRId64
"\n"
236 (double) get_ticks_per_sec() / (r
->ticks_per_char
* r
->total_chars
),
254 static uint8_t vga_precise_retrace(VGACommonState
*s
)
256 struct vga_precise_retrace
*r
= &s
->retrace_info
.precise
;
257 uint8_t val
= s
->st01
& ~(ST01_V_RETRACE
| ST01_DISP_ENABLE
);
259 if (r
->total_chars
) {
260 int cur_line
, cur_line_char
, cur_char
;
263 cur_tick
= qemu_get_clock_ns(vm_clock
);
265 cur_char
= (cur_tick
/ r
->ticks_per_char
) % r
->total_chars
;
266 cur_line
= cur_char
/ r
->htotal
;
268 if (cur_line
>= r
->vstart
&& cur_line
<= r
->vend
) {
269 val
|= ST01_V_RETRACE
| ST01_DISP_ENABLE
;
271 cur_line_char
= cur_char
% r
->htotal
;
272 if (cur_line_char
>= r
->hstart
&& cur_line_char
<= r
->hend
) {
273 val
|= ST01_DISP_ENABLE
;
279 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
283 static uint8_t vga_dumb_retrace(VGACommonState
*s
)
285 return s
->st01
^ (ST01_V_RETRACE
| ST01_DISP_ENABLE
);
288 int vga_ioport_invalid(VGACommonState
*s
, uint32_t addr
)
290 if (s
->msr
& MSR_COLOR_EMULATION
) {
292 return (addr
>= 0x3b0 && addr
<= 0x3bf);
295 return (addr
>= 0x3d0 && addr
<= 0x3df);
299 uint32_t vga_ioport_read(void *opaque
, uint32_t addr
)
301 VGACommonState
*s
= opaque
;
304 if (vga_ioport_invalid(s
, addr
)) {
309 if (s
->ar_flip_flop
== 0) {
316 index
= s
->ar_index
& 0x1f;
329 val
= s
->sr
[s
->sr_index
];
331 printf("vga: read SR%x = 0x%02x\n", s
->sr_index
, val
);
338 val
= s
->dac_write_index
;
341 val
= s
->palette
[s
->dac_read_index
* 3 + s
->dac_sub_index
];
342 if (++s
->dac_sub_index
== 3) {
343 s
->dac_sub_index
= 0;
357 val
= s
->gr
[s
->gr_index
];
359 printf("vga: read GR%x = 0x%02x\n", s
->gr_index
, val
);
368 val
= s
->cr
[s
->cr_index
];
370 printf("vga: read CR%x = 0x%02x\n", s
->cr_index
, val
);
375 /* just toggle to fool polling */
376 val
= s
->st01
= s
->retrace(s
);
384 #if defined(DEBUG_VGA)
385 printf("VGA: read addr=0x%04x data=0x%02x\n", addr
, val
);
390 void vga_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
392 VGACommonState
*s
= opaque
;
395 /* check port range access depending on color/monochrome mode */
396 if (vga_ioport_invalid(s
, addr
)) {
400 printf("VGA: write addr=0x%04x data=0x%02x\n", addr
, val
);
405 if (s
->ar_flip_flop
== 0) {
409 index
= s
->ar_index
& 0x1f;
412 s
->ar
[index
] = val
& 0x3f;
415 s
->ar
[index
] = val
& ~0x10;
421 s
->ar
[index
] = val
& ~0xc0;
424 s
->ar
[index
] = val
& ~0xf0;
427 s
->ar
[index
] = val
& ~0xf0;
433 s
->ar_flip_flop
^= 1;
436 s
->msr
= val
& ~0x10;
437 s
->update_retrace_info(s
);
440 s
->sr_index
= val
& 7;
444 printf("vga: write SR%x = 0x%02x\n", s
->sr_index
, val
);
446 s
->sr
[s
->sr_index
] = val
& sr_mask
[s
->sr_index
];
447 if (s
->sr_index
== 1) s
->update_retrace_info(s
);
450 s
->dac_read_index
= val
;
451 s
->dac_sub_index
= 0;
455 s
->dac_write_index
= val
;
456 s
->dac_sub_index
= 0;
460 s
->dac_cache
[s
->dac_sub_index
] = val
;
461 if (++s
->dac_sub_index
== 3) {
462 memcpy(&s
->palette
[s
->dac_write_index
* 3], s
->dac_cache
, 3);
463 s
->dac_sub_index
= 0;
464 s
->dac_write_index
++;
468 s
->gr_index
= val
& 0x0f;
472 printf("vga: write GR%x = 0x%02x\n", s
->gr_index
, val
);
474 s
->gr
[s
->gr_index
] = val
& gr_mask
[s
->gr_index
];
483 printf("vga: write CR%x = 0x%02x\n", s
->cr_index
, val
);
485 /* handle CR0-7 protection */
486 if ((s
->cr
[0x11] & 0x80) && s
->cr_index
<= 7) {
487 /* can always write bit 4 of CR7 */
488 if (s
->cr_index
== 7)
489 s
->cr
[7] = (s
->cr
[7] & ~0x10) | (val
& 0x10);
492 s
->cr
[s
->cr_index
] = val
;
494 switch(s
->cr_index
) {
502 s
->update_retrace_info(s
);
513 #ifdef CONFIG_BOCHS_VBE
514 static uint32_t vbe_ioport_read_index(void *opaque
, uint32_t addr
)
516 VGACommonState
*s
= opaque
;
522 static uint32_t vbe_ioport_read_data(void *opaque
, uint32_t addr
)
524 VGACommonState
*s
= opaque
;
527 if (s
->vbe_index
< VBE_DISPI_INDEX_NB
) {
528 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_GETCAPS
) {
529 switch(s
->vbe_index
) {
530 /* XXX: do not hardcode ? */
531 case VBE_DISPI_INDEX_XRES
:
532 val
= VBE_DISPI_MAX_XRES
;
534 case VBE_DISPI_INDEX_YRES
:
535 val
= VBE_DISPI_MAX_YRES
;
537 case VBE_DISPI_INDEX_BPP
:
538 val
= VBE_DISPI_MAX_BPP
;
541 val
= s
->vbe_regs
[s
->vbe_index
];
545 val
= s
->vbe_regs
[s
->vbe_index
];
547 } else if (s
->vbe_index
== VBE_DISPI_INDEX_VIDEO_MEMORY_64K
) {
548 val
= s
->vram_size
/ (64 * 1024);
552 #ifdef DEBUG_BOCHS_VBE
553 printf("VBE: read index=0x%x val=0x%x\n", s
->vbe_index
, val
);
558 static void vbe_ioport_write_index(void *opaque
, uint32_t addr
, uint32_t val
)
560 VGACommonState
*s
= opaque
;
564 static void vbe_ioport_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
566 VGACommonState
*s
= opaque
;
568 if (s
->vbe_index
<= VBE_DISPI_INDEX_NB
) {
569 #ifdef DEBUG_BOCHS_VBE
570 printf("VBE: write index=0x%x val=0x%x\n", s
->vbe_index
, val
);
572 switch(s
->vbe_index
) {
573 case VBE_DISPI_INDEX_ID
:
574 if (val
== VBE_DISPI_ID0
||
575 val
== VBE_DISPI_ID1
||
576 val
== VBE_DISPI_ID2
||
577 val
== VBE_DISPI_ID3
||
578 val
== VBE_DISPI_ID4
) {
579 s
->vbe_regs
[s
->vbe_index
] = val
;
582 case VBE_DISPI_INDEX_XRES
:
583 if ((val
<= VBE_DISPI_MAX_XRES
) && ((val
& 7) == 0)) {
584 s
->vbe_regs
[s
->vbe_index
] = val
;
587 case VBE_DISPI_INDEX_YRES
:
588 if (val
<= VBE_DISPI_MAX_YRES
) {
589 s
->vbe_regs
[s
->vbe_index
] = val
;
592 case VBE_DISPI_INDEX_BPP
:
595 if (val
== 4 || val
== 8 || val
== 15 ||
596 val
== 16 || val
== 24 || val
== 32) {
597 s
->vbe_regs
[s
->vbe_index
] = val
;
600 case VBE_DISPI_INDEX_BANK
:
601 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
602 val
&= (s
->vbe_bank_mask
>> 2);
604 val
&= s
->vbe_bank_mask
;
606 s
->vbe_regs
[s
->vbe_index
] = val
;
607 s
->bank_offset
= (val
<< 16);
609 case VBE_DISPI_INDEX_ENABLE
:
610 if ((val
& VBE_DISPI_ENABLED
) &&
611 !(s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
)) {
612 int h
, shift_control
;
614 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] =
615 s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
616 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] =
617 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
618 s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
] = 0;
619 s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
] = 0;
621 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
622 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 1;
624 s
->vbe_line_offset
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] *
625 ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
626 s
->vbe_start_addr
= 0;
628 /* clear the screen (should be done in BIOS) */
629 if (!(val
& VBE_DISPI_NOCLEARMEM
)) {
630 memset(s
->vram_ptr
, 0,
631 s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] * s
->vbe_line_offset
);
634 /* we initialize the VGA graphic mode (should be done
636 s
->gr
[0x06] = (s
->gr
[0x06] & ~0x0c) | 0x05; /* graphic mode + memory map 1 */
637 s
->cr
[0x17] |= 3; /* no CGA modes */
638 s
->cr
[0x13] = s
->vbe_line_offset
>> 3;
640 s
->cr
[0x01] = (s
->vbe_regs
[VBE_DISPI_INDEX_XRES
] >> 3) - 1;
641 /* height (only meaningful if < 1024) */
642 h
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
] - 1;
644 s
->cr
[0x07] = (s
->cr
[0x07] & ~0x42) |
645 ((h
>> 7) & 0x02) | ((h
>> 3) & 0x40);
646 /* line compare to 1023 */
651 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4) {
653 s
->sr
[0x01] &= ~8; /* no double line */
656 s
->sr
[4] |= 0x08; /* set chain 4 mode */
657 s
->sr
[2] |= 0x0f; /* activate all planes */
659 s
->gr
[0x05] = (s
->gr
[0x05] & ~0x60) | (shift_control
<< 5);
660 s
->cr
[0x09] &= ~0x9f; /* no double scan */
662 /* XXX: the bios should do that */
665 s
->dac_8bit
= (val
& VBE_DISPI_8BIT_DAC
) > 0;
666 s
->vbe_regs
[s
->vbe_index
] = val
;
668 case VBE_DISPI_INDEX_VIRT_WIDTH
:
670 int w
, h
, line_offset
;
672 if (val
< s
->vbe_regs
[VBE_DISPI_INDEX_XRES
])
675 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
676 line_offset
= w
>> 1;
678 line_offset
= w
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
679 h
= s
->vram_size
/ line_offset
;
680 /* XXX: support weird bochs semantics ? */
681 if (h
< s
->vbe_regs
[VBE_DISPI_INDEX_YRES
])
683 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_WIDTH
] = w
;
684 s
->vbe_regs
[VBE_DISPI_INDEX_VIRT_HEIGHT
] = h
;
685 s
->vbe_line_offset
= line_offset
;
688 case VBE_DISPI_INDEX_X_OFFSET
:
689 case VBE_DISPI_INDEX_Y_OFFSET
:
692 s
->vbe_regs
[s
->vbe_index
] = val
;
693 s
->vbe_start_addr
= s
->vbe_line_offset
* s
->vbe_regs
[VBE_DISPI_INDEX_Y_OFFSET
];
694 x
= s
->vbe_regs
[VBE_DISPI_INDEX_X_OFFSET
];
695 if (s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] == 4)
696 s
->vbe_start_addr
+= x
>> 1;
698 s
->vbe_start_addr
+= x
* ((s
->vbe_regs
[VBE_DISPI_INDEX_BPP
] + 7) >> 3);
699 s
->vbe_start_addr
>>= 2;
709 /* called for accesses between 0xa0000 and 0xc0000 */
710 uint32_t vga_mem_readb(VGACommonState
*s
, target_phys_addr_t addr
)
712 int memory_map_mode
, plane
;
715 /* convert to VGA memory offset */
716 memory_map_mode
= (s
->gr
[6] >> 2) & 3;
718 switch(memory_map_mode
) {
724 addr
+= s
->bank_offset
;
739 if (s
->sr
[4] & 0x08) {
740 /* chain 4 mode : simplest access */
741 ret
= s
->vram_ptr
[addr
];
742 } else if (s
->gr
[5] & 0x10) {
743 /* odd/even mode (aka text mode mapping) */
744 plane
= (s
->gr
[4] & 2) | (addr
& 1);
745 ret
= s
->vram_ptr
[((addr
& ~1) << 1) | plane
];
747 /* standard VGA latched access */
748 s
->latch
= ((uint32_t *)s
->vram_ptr
)[addr
];
750 if (!(s
->gr
[5] & 0x08)) {
753 ret
= GET_PLANE(s
->latch
, plane
);
756 ret
= (s
->latch
^ mask16
[s
->gr
[2]]) & mask16
[s
->gr
[7]];
765 /* called for accesses between 0xa0000 and 0xc0000 */
766 void vga_mem_writeb(VGACommonState
*s
, target_phys_addr_t addr
, uint32_t val
)
768 int memory_map_mode
, plane
, write_mode
, b
, func_select
, mask
;
769 uint32_t write_mask
, bit_mask
, set_mask
;
772 printf("vga: [0x" TARGET_FMT_plx
"] = 0x%02x\n", addr
, val
);
774 /* convert to VGA memory offset */
775 memory_map_mode
= (s
->gr
[6] >> 2) & 3;
777 switch(memory_map_mode
) {
783 addr
+= s
->bank_offset
;
798 if (s
->sr
[4] & 0x08) {
799 /* chain 4 mode : simplest access */
802 if (s
->sr
[2] & mask
) {
803 s
->vram_ptr
[addr
] = val
;
805 printf("vga: chain4: [0x" TARGET_FMT_plx
"]\n", addr
);
807 s
->plane_updated
|= mask
; /* only used to detect font change */
808 memory_region_set_dirty(&s
->vram
, addr
);
810 } else if (s
->gr
[5] & 0x10) {
811 /* odd/even mode (aka text mode mapping) */
812 plane
= (s
->gr
[4] & 2) | (addr
& 1);
814 if (s
->sr
[2] & mask
) {
815 addr
= ((addr
& ~1) << 1) | plane
;
816 s
->vram_ptr
[addr
] = val
;
818 printf("vga: odd/even: [0x" TARGET_FMT_plx
"]\n", addr
);
820 s
->plane_updated
|= mask
; /* only used to detect font change */
821 memory_region_set_dirty(&s
->vram
, addr
);
824 /* standard VGA latched access */
825 write_mode
= s
->gr
[5] & 3;
831 val
= ((val
>> b
) | (val
<< (8 - b
))) & 0xff;
835 /* apply set/reset mask */
836 set_mask
= mask16
[s
->gr
[1]];
837 val
= (val
& ~set_mask
) | (mask16
[s
->gr
[0]] & set_mask
);
844 val
= mask16
[val
& 0x0f];
850 val
= (val
>> b
) | (val
<< (8 - b
));
852 bit_mask
= s
->gr
[8] & val
;
853 val
= mask16
[s
->gr
[0]];
857 /* apply logical operation */
858 func_select
= s
->gr
[3] >> 3;
859 switch(func_select
) {
879 bit_mask
|= bit_mask
<< 8;
880 bit_mask
|= bit_mask
<< 16;
881 val
= (val
& bit_mask
) | (s
->latch
& ~bit_mask
);
884 /* mask data according to sr[2] */
886 s
->plane_updated
|= mask
; /* only used to detect font change */
887 write_mask
= mask16
[mask
];
888 ((uint32_t *)s
->vram_ptr
)[addr
] =
889 (((uint32_t *)s
->vram_ptr
)[addr
] & ~write_mask
) |
892 printf("vga: latch: [0x" TARGET_FMT_plx
"] mask=0x%08x val=0x%08x\n",
893 addr
* 4, write_mask
, val
);
895 memory_region_set_dirty(&s
->vram
, addr
<< 2);
899 typedef void vga_draw_glyph8_func(uint8_t *d
, int linesize
,
900 const uint8_t *font_ptr
, int h
,
901 uint32_t fgcol
, uint32_t bgcol
);
902 typedef void vga_draw_glyph9_func(uint8_t *d
, int linesize
,
903 const uint8_t *font_ptr
, int h
,
904 uint32_t fgcol
, uint32_t bgcol
, int dup9
);
905 typedef void vga_draw_line_func(VGACommonState
*s1
, uint8_t *d
,
906 const uint8_t *s
, int width
);
909 #include "vga_template.h"
912 #include "vga_template.h"
916 #include "vga_template.h"
919 #include "vga_template.h"
923 #include "vga_template.h"
926 #include "vga_template.h"
930 #include "vga_template.h"
932 static unsigned int rgb_to_pixel8_dup(unsigned int r
, unsigned int g
, unsigned b
)
935 col
= rgb_to_pixel8(r
, g
, b
);
941 static unsigned int rgb_to_pixel15_dup(unsigned int r
, unsigned int g
, unsigned b
)
944 col
= rgb_to_pixel15(r
, g
, b
);
949 static unsigned int rgb_to_pixel15bgr_dup(unsigned int r
, unsigned int g
,
953 col
= rgb_to_pixel15bgr(r
, g
, b
);
958 static unsigned int rgb_to_pixel16_dup(unsigned int r
, unsigned int g
, unsigned b
)
961 col
= rgb_to_pixel16(r
, g
, b
);
966 static unsigned int rgb_to_pixel16bgr_dup(unsigned int r
, unsigned int g
,
970 col
= rgb_to_pixel16bgr(r
, g
, b
);
975 static unsigned int rgb_to_pixel32_dup(unsigned int r
, unsigned int g
, unsigned b
)
978 col
= rgb_to_pixel32(r
, g
, b
);
982 static unsigned int rgb_to_pixel32bgr_dup(unsigned int r
, unsigned int g
, unsigned b
)
985 col
= rgb_to_pixel32bgr(r
, g
, b
);
989 /* return true if the palette was modified */
990 static int update_palette16(VGACommonState
*s
)
993 uint32_t v
, col
, *palette
;
996 palette
= s
->last_palette
;
997 for(i
= 0; i
< 16; i
++) {
999 if (s
->ar
[0x10] & 0x80)
1000 v
= ((s
->ar
[0x14] & 0xf) << 4) | (v
& 0xf);
1002 v
= ((s
->ar
[0x14] & 0xc) << 4) | (v
& 0x3f);
1004 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1005 c6_to_8(s
->palette
[v
+ 1]),
1006 c6_to_8(s
->palette
[v
+ 2]));
1007 if (col
!= palette
[i
]) {
1015 /* return true if the palette was modified */
1016 static int update_palette256(VGACommonState
*s
)
1019 uint32_t v
, col
, *palette
;
1022 palette
= s
->last_palette
;
1024 for(i
= 0; i
< 256; i
++) {
1026 col
= s
->rgb_to_pixel(s
->palette
[v
],
1030 col
= s
->rgb_to_pixel(c6_to_8(s
->palette
[v
]),
1031 c6_to_8(s
->palette
[v
+ 1]),
1032 c6_to_8(s
->palette
[v
+ 2]));
1034 if (col
!= palette
[i
]) {
1043 static void vga_get_offsets(VGACommonState
*s
,
1044 uint32_t *pline_offset
,
1045 uint32_t *pstart_addr
,
1046 uint32_t *pline_compare
)
1048 uint32_t start_addr
, line_offset
, line_compare
;
1049 #ifdef CONFIG_BOCHS_VBE
1050 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1051 line_offset
= s
->vbe_line_offset
;
1052 start_addr
= s
->vbe_start_addr
;
1053 line_compare
= 65535;
1057 /* compute line_offset in bytes */
1058 line_offset
= s
->cr
[0x13];
1061 /* starting address */
1062 start_addr
= s
->cr
[0x0d] | (s
->cr
[0x0c] << 8);
1065 line_compare
= s
->cr
[0x18] |
1066 ((s
->cr
[0x07] & 0x10) << 4) |
1067 ((s
->cr
[0x09] & 0x40) << 3);
1069 *pline_offset
= line_offset
;
1070 *pstart_addr
= start_addr
;
1071 *pline_compare
= line_compare
;
1074 /* update start_addr and line_offset. Return TRUE if modified */
1075 static int update_basic_params(VGACommonState
*s
)
1078 uint32_t start_addr
, line_offset
, line_compare
;
1082 s
->get_offsets(s
, &line_offset
, &start_addr
, &line_compare
);
1084 if (line_offset
!= s
->line_offset
||
1085 start_addr
!= s
->start_addr
||
1086 line_compare
!= s
->line_compare
) {
1087 s
->line_offset
= line_offset
;
1088 s
->start_addr
= start_addr
;
1089 s
->line_compare
= line_compare
;
1097 static inline int get_depth_index(DisplayState
*s
)
1099 switch(ds_get_bits_per_pixel(s
)) {
1108 if (is_surface_bgr(s
->surface
))
1115 static vga_draw_glyph8_func
* const vga_draw_glyph8_table
[NB_DEPTHS
] = {
1125 static vga_draw_glyph8_func
* const vga_draw_glyph16_table
[NB_DEPTHS
] = {
1127 vga_draw_glyph16_16
,
1128 vga_draw_glyph16_16
,
1129 vga_draw_glyph16_32
,
1130 vga_draw_glyph16_32
,
1131 vga_draw_glyph16_16
,
1132 vga_draw_glyph16_16
,
1135 static vga_draw_glyph9_func
* const vga_draw_glyph9_table
[NB_DEPTHS
] = {
1145 static const uint8_t cursor_glyph
[32 * 4] = {
1146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1151 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1152 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1153 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1154 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1155 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1156 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1157 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1158 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1160 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1164 static void vga_get_text_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
,
1165 int *pcwidth
, int *pcheight
)
1167 int width
, cwidth
, height
, cheight
;
1169 /* total width & height */
1170 cheight
= (s
->cr
[9] & 0x1f) + 1;
1172 if (!(s
->sr
[1] & 0x01))
1174 if (s
->sr
[1] & 0x08)
1175 cwidth
= 16; /* NOTE: no 18 pixel wide */
1176 width
= (s
->cr
[0x01] + 1);
1177 if (s
->cr
[0x06] == 100) {
1178 /* ugly hack for CGA 160x100x16 - explain me the logic */
1181 height
= s
->cr
[0x12] |
1182 ((s
->cr
[0x07] & 0x02) << 7) |
1183 ((s
->cr
[0x07] & 0x40) << 3);
1184 height
= (height
+ 1) / cheight
;
1190 *pcheight
= cheight
;
1193 typedef unsigned int rgb_to_pixel_dup_func(unsigned int r
, unsigned int g
, unsigned b
);
1195 static rgb_to_pixel_dup_func
* const rgb_to_pixel_dup_table
[NB_DEPTHS
] = {
1200 rgb_to_pixel32bgr_dup
,
1201 rgb_to_pixel15bgr_dup
,
1202 rgb_to_pixel16bgr_dup
,
1213 static void vga_draw_text(VGACommonState
*s
, int full_update
)
1215 int cx
, cy
, cheight
, cw
, ch
, cattr
, height
, width
, ch_attr
;
1216 int cx_min
, cx_max
, linesize
, x_incr
, line
, line1
;
1217 uint32_t offset
, fgcol
, bgcol
, v
, cursor_offset
;
1218 uint8_t *d1
, *d
, *src
, *dest
, *cursor_ptr
;
1219 const uint8_t *font_ptr
, *font_base
[2];
1220 int dup9
, line_offset
, depth_index
;
1222 uint32_t *ch_attr_ptr
;
1223 vga_draw_glyph8_func
*vga_draw_glyph8
;
1224 vga_draw_glyph9_func
*vga_draw_glyph9
;
1226 /* compute font data address (in plane 2) */
1228 offset
= (((v
>> 4) & 1) | ((v
<< 1) & 6)) * 8192 * 4 + 2;
1229 if (offset
!= s
->font_offsets
[0]) {
1230 s
->font_offsets
[0] = offset
;
1233 font_base
[0] = s
->vram_ptr
+ offset
;
1235 offset
= (((v
>> 5) & 1) | ((v
>> 1) & 6)) * 8192 * 4 + 2;
1236 font_base
[1] = s
->vram_ptr
+ offset
;
1237 if (offset
!= s
->font_offsets
[1]) {
1238 s
->font_offsets
[1] = offset
;
1241 if (s
->plane_updated
& (1 << 2)) {
1242 /* if the plane 2 was modified since the last display, it
1243 indicates the font may have been modified */
1244 s
->plane_updated
= 0;
1247 full_update
|= update_basic_params(s
);
1249 line_offset
= s
->line_offset
;
1251 vga_get_text_resolution(s
, &width
, &height
, &cw
, &cheight
);
1252 if ((height
* width
) > CH_ATTR_SIZE
) {
1253 /* better than nothing: exit if transient size is too big */
1257 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1258 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
|| s
->last_depth
) {
1259 s
->last_scr_width
= width
* cw
;
1260 s
->last_scr_height
= height
* cheight
;
1261 qemu_console_resize(s
->ds
, s
->last_scr_width
, s
->last_scr_height
);
1263 s
->last_width
= width
;
1264 s
->last_height
= height
;
1265 s
->last_ch
= cheight
;
1270 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1271 full_update
|= update_palette16(s
);
1272 palette
= s
->last_palette
;
1273 x_incr
= cw
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1275 cursor_offset
= ((s
->cr
[0x0e] << 8) | s
->cr
[0x0f]) - s
->start_addr
;
1276 if (cursor_offset
!= s
->cursor_offset
||
1277 s
->cr
[0xa] != s
->cursor_start
||
1278 s
->cr
[0xb] != s
->cursor_end
) {
1279 /* if the cursor position changed, we update the old and new
1281 if (s
->cursor_offset
< CH_ATTR_SIZE
)
1282 s
->last_ch_attr
[s
->cursor_offset
] = -1;
1283 if (cursor_offset
< CH_ATTR_SIZE
)
1284 s
->last_ch_attr
[cursor_offset
] = -1;
1285 s
->cursor_offset
= cursor_offset
;
1286 s
->cursor_start
= s
->cr
[0xa];
1287 s
->cursor_end
= s
->cr
[0xb];
1289 cursor_ptr
= s
->vram_ptr
+ (s
->start_addr
+ cursor_offset
) * 4;
1291 depth_index
= get_depth_index(s
->ds
);
1293 vga_draw_glyph8
= vga_draw_glyph16_table
[depth_index
];
1295 vga_draw_glyph8
= vga_draw_glyph8_table
[depth_index
];
1296 vga_draw_glyph9
= vga_draw_glyph9_table
[depth_index
];
1298 dest
= ds_get_data(s
->ds
);
1299 linesize
= ds_get_linesize(s
->ds
);
1300 ch_attr_ptr
= s
->last_ch_attr
;
1302 offset
= s
->start_addr
* 4;
1303 for(cy
= 0; cy
< height
; cy
++) {
1305 src
= s
->vram_ptr
+ offset
;
1308 for(cx
= 0; cx
< width
; cx
++) {
1309 ch_attr
= *(uint16_t *)src
;
1310 if (full_update
|| ch_attr
!= *ch_attr_ptr
) {
1315 *ch_attr_ptr
= ch_attr
;
1316 #ifdef HOST_WORDS_BIGENDIAN
1318 cattr
= ch_attr
& 0xff;
1320 ch
= ch_attr
& 0xff;
1321 cattr
= ch_attr
>> 8;
1323 font_ptr
= font_base
[(cattr
>> 3) & 1];
1324 font_ptr
+= 32 * 4 * ch
;
1325 bgcol
= palette
[cattr
>> 4];
1326 fgcol
= palette
[cattr
& 0x0f];
1328 vga_draw_glyph8(d1
, linesize
,
1329 font_ptr
, cheight
, fgcol
, bgcol
);
1332 if (ch
>= 0xb0 && ch
<= 0xdf && (s
->ar
[0x10] & 0x04))
1334 vga_draw_glyph9(d1
, linesize
,
1335 font_ptr
, cheight
, fgcol
, bgcol
, dup9
);
1337 if (src
== cursor_ptr
&&
1338 !(s
->cr
[0x0a] & 0x20)) {
1339 int line_start
, line_last
, h
;
1340 /* draw the cursor */
1341 line_start
= s
->cr
[0x0a] & 0x1f;
1342 line_last
= s
->cr
[0x0b] & 0x1f;
1343 /* XXX: check that */
1344 if (line_last
> cheight
- 1)
1345 line_last
= cheight
- 1;
1346 if (line_last
>= line_start
&& line_start
< cheight
) {
1347 h
= line_last
- line_start
+ 1;
1348 d
= d1
+ linesize
* line_start
;
1350 vga_draw_glyph8(d
, linesize
,
1351 cursor_glyph
, h
, fgcol
, bgcol
);
1353 vga_draw_glyph9(d
, linesize
,
1354 cursor_glyph
, h
, fgcol
, bgcol
, 1);
1364 dpy_update(s
->ds
, cx_min
* cw
, cy
* cheight
,
1365 (cx_max
- cx_min
+ 1) * cw
, cheight
);
1367 dest
+= linesize
* cheight
;
1368 line1
= line
+ cheight
;
1369 offset
+= line_offset
;
1370 if (line
< s
->line_compare
&& line1
>= s
->line_compare
) {
1391 static vga_draw_line_func
* const vga_draw_line_table
[NB_DEPTHS
* VGA_DRAW_LINE_NB
] = {
1401 vga_draw_line2d2_16
,
1402 vga_draw_line2d2_16
,
1403 vga_draw_line2d2_32
,
1404 vga_draw_line2d2_32
,
1405 vga_draw_line2d2_16
,
1406 vga_draw_line2d2_16
,
1417 vga_draw_line4d2_16
,
1418 vga_draw_line4d2_16
,
1419 vga_draw_line4d2_32
,
1420 vga_draw_line4d2_32
,
1421 vga_draw_line4d2_16
,
1422 vga_draw_line4d2_16
,
1425 vga_draw_line8d2_16
,
1426 vga_draw_line8d2_16
,
1427 vga_draw_line8d2_32
,
1428 vga_draw_line8d2_32
,
1429 vga_draw_line8d2_16
,
1430 vga_draw_line8d2_16
,
1444 vga_draw_line15_32bgr
,
1445 vga_draw_line15_15bgr
,
1446 vga_draw_line15_16bgr
,
1452 vga_draw_line16_32bgr
,
1453 vga_draw_line16_15bgr
,
1454 vga_draw_line16_16bgr
,
1460 vga_draw_line24_32bgr
,
1461 vga_draw_line24_15bgr
,
1462 vga_draw_line24_16bgr
,
1468 vga_draw_line32_32bgr
,
1469 vga_draw_line32_15bgr
,
1470 vga_draw_line32_16bgr
,
1473 static int vga_get_bpp(VGACommonState
*s
)
1476 #ifdef CONFIG_BOCHS_VBE
1477 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1478 ret
= s
->vbe_regs
[VBE_DISPI_INDEX_BPP
];
1487 static void vga_get_resolution(VGACommonState
*s
, int *pwidth
, int *pheight
)
1491 #ifdef CONFIG_BOCHS_VBE
1492 if (s
->vbe_regs
[VBE_DISPI_INDEX_ENABLE
] & VBE_DISPI_ENABLED
) {
1493 width
= s
->vbe_regs
[VBE_DISPI_INDEX_XRES
];
1494 height
= s
->vbe_regs
[VBE_DISPI_INDEX_YRES
];
1498 width
= (s
->cr
[0x01] + 1) * 8;
1499 height
= s
->cr
[0x12] |
1500 ((s
->cr
[0x07] & 0x02) << 7) |
1501 ((s
->cr
[0x07] & 0x40) << 3);
1502 height
= (height
+ 1);
1508 void vga_invalidate_scanlines(VGACommonState
*s
, int y1
, int y2
)
1511 if (y1
>= VGA_MAX_HEIGHT
)
1513 if (y2
>= VGA_MAX_HEIGHT
)
1514 y2
= VGA_MAX_HEIGHT
;
1515 for(y
= y1
; y
< y2
; y
++) {
1516 s
->invalidated_y_table
[y
>> 5] |= 1 << (y
& 0x1f);
1520 static void vga_sync_dirty_bitmap(VGACommonState
*s
)
1522 memory_region_sync_dirty_bitmap(&s
->vram
);
1525 void vga_dirty_log_start(VGACommonState
*s
)
1527 memory_region_set_log(&s
->vram
, true, DIRTY_MEMORY_VGA
);
1530 void vga_dirty_log_stop(VGACommonState
*s
)
1532 memory_region_set_log(&s
->vram
, false, DIRTY_MEMORY_VGA
);
1535 void vga_dirty_log_restart(VGACommonState
*s
)
1537 vga_dirty_log_stop(s
);
1538 vga_dirty_log_start(s
);
1544 static void vga_draw_graphic(VGACommonState
*s
, int full_update
)
1546 int y1
, y
, update
, linesize
, y_start
, double_scan
, mask
, depth
;
1547 int width
, height
, shift_control
, line_offset
, bwidth
, bits
;
1548 ram_addr_t page0
, page1
, page_min
, page_max
;
1549 int disp_width
, multi_scan
, multi_run
;
1551 uint32_t v
, addr1
, addr
;
1552 vga_draw_line_func
*vga_draw_line
;
1554 full_update
|= update_basic_params(s
);
1557 vga_sync_dirty_bitmap(s
);
1559 s
->get_resolution(s
, &width
, &height
);
1562 shift_control
= (s
->gr
[0x05] >> 5) & 3;
1563 double_scan
= (s
->cr
[0x09] >> 7);
1564 if (shift_control
!= 1) {
1565 multi_scan
= (((s
->cr
[0x09] & 0x1f) + 1) << double_scan
) - 1;
1567 /* in CGA modes, multi_scan is ignored */
1568 /* XXX: is it correct ? */
1569 multi_scan
= double_scan
;
1571 multi_run
= multi_scan
;
1572 if (shift_control
!= s
->shift_control
||
1573 double_scan
!= s
->double_scan
) {
1575 s
->shift_control
= shift_control
;
1576 s
->double_scan
= double_scan
;
1579 if (shift_control
== 0) {
1580 if (s
->sr
[0x01] & 8) {
1583 } else if (shift_control
== 1) {
1584 if (s
->sr
[0x01] & 8) {
1589 depth
= s
->get_bpp(s
);
1590 if (s
->line_offset
!= s
->last_line_offset
||
1591 disp_width
!= s
->last_width
||
1592 height
!= s
->last_height
||
1593 s
->last_depth
!= depth
) {
1594 #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1595 if (depth
== 16 || depth
== 32) {
1599 qemu_free_displaysurface(s
->ds
);
1600 s
->ds
->surface
= qemu_create_displaysurface_from(disp_width
, height
, depth
,
1602 s
->vram_ptr
+ (s
->start_addr
* 4));
1603 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
1604 s
->ds
->surface
->pf
= qemu_different_endianness_pixelformat(depth
);
1608 qemu_console_resize(s
->ds
, disp_width
, height
);
1610 s
->last_scr_width
= disp_width
;
1611 s
->last_scr_height
= height
;
1612 s
->last_width
= disp_width
;
1613 s
->last_height
= height
;
1614 s
->last_line_offset
= s
->line_offset
;
1615 s
->last_depth
= depth
;
1617 } else if (is_buffer_shared(s
->ds
->surface
) &&
1618 (full_update
|| s
->ds
->surface
->data
!= s
->vram_ptr
+ (s
->start_addr
* 4))) {
1619 s
->ds
->surface
->data
= s
->vram_ptr
+ (s
->start_addr
* 4);
1624 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1626 if (shift_control
== 0) {
1627 full_update
|= update_palette16(s
);
1628 if (s
->sr
[0x01] & 8) {
1629 v
= VGA_DRAW_LINE4D2
;
1634 } else if (shift_control
== 1) {
1635 full_update
|= update_palette16(s
);
1636 if (s
->sr
[0x01] & 8) {
1637 v
= VGA_DRAW_LINE2D2
;
1643 switch(s
->get_bpp(s
)) {
1646 full_update
|= update_palette256(s
);
1647 v
= VGA_DRAW_LINE8D2
;
1651 full_update
|= update_palette256(s
);
1656 v
= VGA_DRAW_LINE15
;
1660 v
= VGA_DRAW_LINE16
;
1664 v
= VGA_DRAW_LINE24
;
1668 v
= VGA_DRAW_LINE32
;
1673 vga_draw_line
= vga_draw_line_table
[v
* NB_DEPTHS
+ get_depth_index(s
->ds
)];
1675 if (!is_buffer_shared(s
->ds
->surface
) && s
->cursor_invalidate
)
1676 s
->cursor_invalidate(s
);
1678 line_offset
= s
->line_offset
;
1680 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",
1681 width
, height
, v
, line_offset
, s
->cr
[9], s
->cr
[0x17], s
->line_compare
, s
->sr
[0x01]);
1683 addr1
= (s
->start_addr
* 4);
1684 bwidth
= (width
* bits
+ 7) / 8;
1688 d
= ds_get_data(s
->ds
);
1689 linesize
= ds_get_linesize(s
->ds
);
1691 for(y
= 0; y
< height
; y
++) {
1693 if (!(s
->cr
[0x17] & 1)) {
1695 /* CGA compatibility handling */
1696 shift
= 14 + ((s
->cr
[0x17] >> 6) & 1);
1697 addr
= (addr
& ~(1 << shift
)) | ((y1
& 1) << shift
);
1699 if (!(s
->cr
[0x17] & 2)) {
1700 addr
= (addr
& ~0x8000) | ((y1
& 2) << 14);
1702 page0
= addr
& TARGET_PAGE_MASK
;
1703 page1
= (addr
+ bwidth
- 1) & TARGET_PAGE_MASK
;
1704 update
= full_update
|
1705 memory_region_get_dirty(&s
->vram
, page0
, DIRTY_MEMORY_VGA
) |
1706 memory_region_get_dirty(&s
->vram
, page1
, DIRTY_MEMORY_VGA
);
1707 if ((page1
- page0
) > TARGET_PAGE_SIZE
) {
1708 /* if wide line, can use another page */
1709 update
|= memory_region_get_dirty(&s
->vram
,
1710 page0
+ TARGET_PAGE_SIZE
,
1713 /* explicit invalidation for the hardware cursor */
1714 update
|= (s
->invalidated_y_table
[y
>> 5] >> (y
& 0x1f)) & 1;
1718 if (page0
< page_min
)
1720 if (page1
> page_max
)
1722 if (!(is_buffer_shared(s
->ds
->surface
))) {
1723 vga_draw_line(s
, d
, s
->vram_ptr
+ addr
, width
);
1724 if (s
->cursor_draw_line
)
1725 s
->cursor_draw_line(s
, d
, y
);
1729 /* flush to display */
1730 dpy_update(s
->ds
, 0, y_start
,
1731 disp_width
, y
- y_start
);
1736 mask
= (s
->cr
[0x17] & 3) ^ 3;
1737 if ((y1
& mask
) == mask
)
1738 addr1
+= line_offset
;
1740 multi_run
= multi_scan
;
1744 /* line compare acts on the displayed lines */
1745 if (y
== s
->line_compare
)
1750 /* flush to display */
1751 dpy_update(s
->ds
, 0, y_start
,
1752 disp_width
, y
- y_start
);
1754 /* reset modified pages */
1755 if (page_max
>= page_min
) {
1756 memory_region_reset_dirty(&s
->vram
,
1758 page_max
+ TARGET_PAGE_SIZE
- page_min
,
1761 memset(s
->invalidated_y_table
, 0, ((height
+ 31) >> 5) * 4);
1764 static void vga_draw_blank(VGACommonState
*s
, int full_update
)
1771 if (s
->last_scr_width
<= 0 || s
->last_scr_height
<= 0)
1775 rgb_to_pixel_dup_table
[get_depth_index(s
->ds
)];
1776 if (ds_get_bits_per_pixel(s
->ds
) == 8)
1777 val
= s
->rgb_to_pixel(0, 0, 0);
1780 w
= s
->last_scr_width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
1781 d
= ds_get_data(s
->ds
);
1782 for(i
= 0; i
< s
->last_scr_height
; i
++) {
1784 d
+= ds_get_linesize(s
->ds
);
1786 dpy_update(s
->ds
, 0, 0,
1787 s
->last_scr_width
, s
->last_scr_height
);
1790 #define GMODE_TEXT 0
1791 #define GMODE_GRAPH 1
1792 #define GMODE_BLANK 2
1794 static void vga_update_display(void *opaque
)
1796 VGACommonState
*s
= opaque
;
1797 int full_update
, graphic_mode
;
1799 if (ds_get_bits_per_pixel(s
->ds
) == 0) {
1803 if (!(s
->ar_index
& 0x20)) {
1804 graphic_mode
= GMODE_BLANK
;
1806 graphic_mode
= s
->gr
[6] & 1;
1808 if (graphic_mode
!= s
->graphic_mode
) {
1809 s
->graphic_mode
= graphic_mode
;
1812 switch(graphic_mode
) {
1814 vga_draw_text(s
, full_update
);
1817 vga_draw_graphic(s
, full_update
);
1821 vga_draw_blank(s
, full_update
);
1827 /* force a full display refresh */
1828 static void vga_invalidate_display(void *opaque
)
1830 VGACommonState
*s
= opaque
;
1833 s
->last_height
= -1;
1836 void vga_common_reset(VGACommonState
*s
)
1839 memset(s
->sr
, '\0', sizeof(s
->sr
));
1841 memset(s
->gr
, '\0', sizeof(s
->gr
));
1843 memset(s
->ar
, '\0', sizeof(s
->ar
));
1844 s
->ar_flip_flop
= 0;
1846 memset(s
->cr
, '\0', sizeof(s
->cr
));
1852 s
->dac_sub_index
= 0;
1853 s
->dac_read_index
= 0;
1854 s
->dac_write_index
= 0;
1855 memset(s
->dac_cache
, '\0', sizeof(s
->dac_cache
));
1857 memset(s
->palette
, '\0', sizeof(s
->palette
));
1859 #ifdef CONFIG_BOCHS_VBE
1861 memset(s
->vbe_regs
, '\0', sizeof(s
->vbe_regs
));
1862 s
->vbe_regs
[VBE_DISPI_INDEX_ID
] = VBE_DISPI_ID5
;
1863 s
->vbe_start_addr
= 0;
1864 s
->vbe_line_offset
= 0;
1865 s
->vbe_bank_mask
= (s
->vram_size
>> 16) - 1;
1867 memset(s
->font_offsets
, '\0', sizeof(s
->font_offsets
));
1868 s
->graphic_mode
= -1; /* force full update */
1869 s
->shift_control
= 0;
1872 s
->line_compare
= 0;
1874 s
->plane_updated
= 0;
1879 s
->last_scr_width
= 0;
1880 s
->last_scr_height
= 0;
1881 s
->cursor_start
= 0;
1883 s
->cursor_offset
= 0;
1884 memset(s
->invalidated_y_table
, '\0', sizeof(s
->invalidated_y_table
));
1885 memset(s
->last_palette
, '\0', sizeof(s
->last_palette
));
1886 memset(s
->last_ch_attr
, '\0', sizeof(s
->last_ch_attr
));
1887 switch (vga_retrace_method
) {
1888 case VGA_RETRACE_DUMB
:
1890 case VGA_RETRACE_PRECISE
:
1891 memset(&s
->retrace_info
, 0, sizeof (s
->retrace_info
));
1896 static void vga_reset(void *opaque
)
1898 VGACommonState
*s
= opaque
;
1899 vga_common_reset(s
);
1902 #define TEXTMODE_X(x) ((x) % width)
1903 #define TEXTMODE_Y(x) ((x) / width)
1904 #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
1905 ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1906 /* relay text rendering to the display driver
1907 * instead of doing a full vga_update_display() */
1908 static void vga_update_text(void *opaque
, console_ch_t
*chardata
)
1910 VGACommonState
*s
= opaque
;
1911 int graphic_mode
, i
, cursor_offset
, cursor_visible
;
1912 int cw
, cheight
, width
, height
, size
, c_min
, c_max
;
1914 console_ch_t
*dst
, val
;
1915 char msg_buffer
[80];
1916 int full_update
= 0;
1918 if (!(s
->ar_index
& 0x20)) {
1919 graphic_mode
= GMODE_BLANK
;
1921 graphic_mode
= s
->gr
[6] & 1;
1923 if (graphic_mode
!= s
->graphic_mode
) {
1924 s
->graphic_mode
= graphic_mode
;
1927 if (s
->last_width
== -1) {
1932 switch (graphic_mode
) {
1934 /* TODO: update palette */
1935 full_update
|= update_basic_params(s
);
1937 /* total width & height */
1938 cheight
= (s
->cr
[9] & 0x1f) + 1;
1940 if (!(s
->sr
[1] & 0x01))
1942 if (s
->sr
[1] & 0x08)
1943 cw
= 16; /* NOTE: no 18 pixel wide */
1944 width
= (s
->cr
[0x01] + 1);
1945 if (s
->cr
[0x06] == 100) {
1946 /* ugly hack for CGA 160x100x16 - explain me the logic */
1949 height
= s
->cr
[0x12] |
1950 ((s
->cr
[0x07] & 0x02) << 7) |
1951 ((s
->cr
[0x07] & 0x40) << 3);
1952 height
= (height
+ 1) / cheight
;
1955 size
= (height
* width
);
1956 if (size
> CH_ATTR_SIZE
) {
1960 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Text mode",
1965 if (width
!= s
->last_width
|| height
!= s
->last_height
||
1966 cw
!= s
->last_cw
|| cheight
!= s
->last_ch
) {
1967 s
->last_scr_width
= width
* cw
;
1968 s
->last_scr_height
= height
* cheight
;
1969 s
->ds
->surface
->width
= width
;
1970 s
->ds
->surface
->height
= height
;
1972 s
->last_width
= width
;
1973 s
->last_height
= height
;
1974 s
->last_ch
= cheight
;
1979 /* Update "hardware" cursor */
1980 cursor_offset
= ((s
->cr
[0x0e] << 8) | s
->cr
[0x0f]) - s
->start_addr
;
1981 if (cursor_offset
!= s
->cursor_offset
||
1982 s
->cr
[0xa] != s
->cursor_start
||
1983 s
->cr
[0xb] != s
->cursor_end
|| full_update
) {
1984 cursor_visible
= !(s
->cr
[0xa] & 0x20);
1985 if (cursor_visible
&& cursor_offset
< size
&& cursor_offset
>= 0)
1987 TEXTMODE_X(cursor_offset
),
1988 TEXTMODE_Y(cursor_offset
));
1990 dpy_cursor(s
->ds
, -1, -1);
1991 s
->cursor_offset
= cursor_offset
;
1992 s
->cursor_start
= s
->cr
[0xa];
1993 s
->cursor_end
= s
->cr
[0xb];
1996 src
= (uint32_t *) s
->vram_ptr
+ s
->start_addr
;
2000 for (i
= 0; i
< size
; src
++, dst
++, i
++)
2001 console_write_ch(dst
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2003 dpy_update(s
->ds
, 0, 0, width
, height
);
2007 for (i
= 0; i
< size
; src
++, dst
++, i
++) {
2008 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2016 for (; i
< size
; src
++, dst
++, i
++) {
2017 console_write_ch(&val
, VMEM2CHTYPE(le32_to_cpu(*src
)));
2024 if (c_min
<= c_max
) {
2025 i
= TEXTMODE_Y(c_min
);
2026 dpy_update(s
->ds
, 0, i
, width
, TEXTMODE_Y(c_max
) - i
+ 1);
2035 s
->get_resolution(s
, &width
, &height
);
2036 snprintf(msg_buffer
, sizeof(msg_buffer
), "%i x %i Graphic mode",
2044 snprintf(msg_buffer
, sizeof(msg_buffer
), "VGA Blank mode");
2048 /* Display a message */
2050 s
->last_height
= height
= 3;
2051 dpy_cursor(s
->ds
, -1, -1);
2052 s
->ds
->surface
->width
= s
->last_width
;
2053 s
->ds
->surface
->height
= height
;
2056 for (dst
= chardata
, i
= 0; i
< s
->last_width
* height
; i
++)
2057 console_write_ch(dst
++, ' ');
2059 size
= strlen(msg_buffer
);
2060 width
= (s
->last_width
- size
) / 2;
2061 dst
= chardata
+ s
->last_width
+ width
;
2062 for (i
= 0; i
< size
; i
++)
2063 console_write_ch(dst
++, 0x00200100 | msg_buffer
[i
]);
2065 dpy_update(s
->ds
, 0, 0, s
->last_width
, height
);
2068 static uint64_t vga_mem_read(void *opaque
, target_phys_addr_t addr
,
2071 VGACommonState
*s
= opaque
;
2073 return vga_mem_readb(s
, addr
);
2076 static void vga_mem_write(void *opaque
, target_phys_addr_t addr
,
2077 uint64_t data
, unsigned size
)
2079 VGACommonState
*s
= opaque
;
2081 return vga_mem_writeb(s
, addr
, data
);
2084 const MemoryRegionOps vga_mem_ops
= {
2085 .read
= vga_mem_read
,
2086 .write
= vga_mem_write
,
2087 .endianness
= DEVICE_LITTLE_ENDIAN
,
2089 .min_access_size
= 1,
2090 .max_access_size
= 1,
2094 static int vga_common_post_load(void *opaque
, int version_id
)
2096 VGACommonState
*s
= opaque
;
2099 s
->graphic_mode
= -1;
2103 const VMStateDescription vmstate_vga_common
= {
2106 .minimum_version_id
= 2,
2107 .minimum_version_id_old
= 2,
2108 .post_load
= vga_common_post_load
,
2109 .fields
= (VMStateField
[]) {
2110 VMSTATE_UINT32(latch
, VGACommonState
),
2111 VMSTATE_UINT8(sr_index
, VGACommonState
),
2112 VMSTATE_PARTIAL_BUFFER(sr
, VGACommonState
, 8),
2113 VMSTATE_UINT8(gr_index
, VGACommonState
),
2114 VMSTATE_PARTIAL_BUFFER(gr
, VGACommonState
, 16),
2115 VMSTATE_UINT8(ar_index
, VGACommonState
),
2116 VMSTATE_BUFFER(ar
, VGACommonState
),
2117 VMSTATE_INT32(ar_flip_flop
, VGACommonState
),
2118 VMSTATE_UINT8(cr_index
, VGACommonState
),
2119 VMSTATE_BUFFER(cr
, VGACommonState
),
2120 VMSTATE_UINT8(msr
, VGACommonState
),
2121 VMSTATE_UINT8(fcr
, VGACommonState
),
2122 VMSTATE_UINT8(st00
, VGACommonState
),
2123 VMSTATE_UINT8(st01
, VGACommonState
),
2125 VMSTATE_UINT8(dac_state
, VGACommonState
),
2126 VMSTATE_UINT8(dac_sub_index
, VGACommonState
),
2127 VMSTATE_UINT8(dac_read_index
, VGACommonState
),
2128 VMSTATE_UINT8(dac_write_index
, VGACommonState
),
2129 VMSTATE_BUFFER(dac_cache
, VGACommonState
),
2130 VMSTATE_BUFFER(palette
, VGACommonState
),
2132 VMSTATE_INT32(bank_offset
, VGACommonState
),
2133 VMSTATE_UINT8_EQUAL(is_vbe_vmstate
, VGACommonState
),
2134 #ifdef CONFIG_BOCHS_VBE
2135 VMSTATE_UINT16(vbe_index
, VGACommonState
),
2136 VMSTATE_UINT16_ARRAY(vbe_regs
, VGACommonState
, VBE_DISPI_INDEX_NB
),
2137 VMSTATE_UINT32(vbe_start_addr
, VGACommonState
),
2138 VMSTATE_UINT32(vbe_line_offset
, VGACommonState
),
2139 VMSTATE_UINT32(vbe_bank_mask
, VGACommonState
),
2141 VMSTATE_END_OF_LIST()
2145 void vga_common_init(VGACommonState
*s
, int vga_ram_size
)
2149 for(i
= 0;i
< 256; i
++) {
2151 for(j
= 0; j
< 8; j
++) {
2152 v
|= ((i
>> j
) & 1) << (j
* 4);
2157 for(j
= 0; j
< 4; j
++) {
2158 v
|= ((i
>> (2 * j
)) & 3) << (j
* 4);
2162 for(i
= 0; i
< 16; i
++) {
2164 for(j
= 0; j
< 4; j
++) {
2167 v
|= b
<< (2 * j
+ 1);
2172 #ifdef CONFIG_BOCHS_VBE
2173 s
->is_vbe_vmstate
= 1;
2175 s
->is_vbe_vmstate
= 0;
2177 memory_region_init_ram(&s
->vram
, NULL
, "vga.vram", vga_ram_size
);
2178 s
->vram_ptr
= memory_region_get_ram_ptr(&s
->vram
);
2179 s
->vram_size
= vga_ram_size
;
2180 s
->get_bpp
= vga_get_bpp
;
2181 s
->get_offsets
= vga_get_offsets
;
2182 s
->get_resolution
= vga_get_resolution
;
2183 s
->update
= vga_update_display
;
2184 s
->invalidate
= vga_invalidate_display
;
2185 s
->screen_dump
= vga_screen_dump
;
2186 s
->text_update
= vga_update_text
;
2187 switch (vga_retrace_method
) {
2188 case VGA_RETRACE_DUMB
:
2189 s
->retrace
= vga_dumb_retrace
;
2190 s
->update_retrace_info
= vga_dumb_update_retrace_info
;
2193 case VGA_RETRACE_PRECISE
:
2194 s
->retrace
= vga_precise_retrace
;
2195 s
->update_retrace_info
= vga_precise_update_retrace_info
;
2198 vga_dirty_log_start(s
);
2201 /* used by both ISA and PCI */
2202 MemoryRegion
*vga_init_io(VGACommonState
*s
)
2204 MemoryRegion
*vga_mem
;
2206 register_ioport_write(0x3c0, 16, 1, vga_ioport_write
, s
);
2208 register_ioport_write(0x3b4, 2, 1, vga_ioport_write
, s
);
2209 register_ioport_write(0x3d4, 2, 1, vga_ioport_write
, s
);
2210 register_ioport_write(0x3ba, 1, 1, vga_ioport_write
, s
);
2211 register_ioport_write(0x3da, 1, 1, vga_ioport_write
, s
);
2213 register_ioport_read(0x3c0, 16, 1, vga_ioport_read
, s
);
2215 register_ioport_read(0x3b4, 2, 1, vga_ioport_read
, s
);
2216 register_ioport_read(0x3d4, 2, 1, vga_ioport_read
, s
);
2217 register_ioport_read(0x3ba, 1, 1, vga_ioport_read
, s
);
2218 register_ioport_read(0x3da, 1, 1, vga_ioport_read
, s
);
2220 #ifdef CONFIG_BOCHS_VBE
2221 #if defined (TARGET_I386)
2222 register_ioport_read(0x1ce, 1, 2, vbe_ioport_read_index
, s
);
2223 register_ioport_read(0x1cf, 1, 2, vbe_ioport_read_data
, s
);
2225 register_ioport_write(0x1ce, 1, 2, vbe_ioport_write_index
, s
);
2226 register_ioport_write(0x1cf, 1, 2, vbe_ioport_write_data
, s
);
2228 register_ioport_read(0x1ce, 1, 2, vbe_ioport_read_index
, s
);
2229 register_ioport_read(0x1d0, 1, 2, vbe_ioport_read_data
, s
);
2231 register_ioport_write(0x1ce, 1, 2, vbe_ioport_write_index
, s
);
2232 register_ioport_write(0x1d0, 1, 2, vbe_ioport_write_data
, s
);
2234 #endif /* CONFIG_BOCHS_VBE */
2236 vga_mem
= g_malloc(sizeof(*vga_mem
));
2237 memory_region_init_io(vga_mem
, &vga_mem_ops
, s
,
2238 "vga-lowmem", 0x20000);
2243 void vga_init(VGACommonState
*s
, MemoryRegion
*address_space
)
2245 MemoryRegion
*vga_io_memory
;
2247 qemu_register_reset(vga_reset
, s
);
2251 vga_io_memory
= vga_init_io(s
);
2252 memory_region_add_subregion_overlap(address_space
,
2253 isa_mem_base
+ 0x000a0000,
2256 memory_region_set_coalescing(vga_io_memory
);
2259 void vga_init_vbe(VGACommonState
*s
, MemoryRegion
*system_memory
)
2261 #ifdef CONFIG_BOCHS_VBE
2262 /* XXX: use optimized standard vga accesses */
2263 memory_region_add_subregion(system_memory
,
2264 VBE_DISPI_LFB_PHYSICAL_ADDRESS
,
2269 /********************************************************/
2270 /* vga screen dump */
2272 static void vga_save_dpy_update(DisplayState
*ds
,
2273 int x
, int y
, int w
, int h
)
2275 if (screen_dump_filename
) {
2276 ppm_save(screen_dump_filename
, ds
->surface
);
2277 screen_dump_filename
= NULL
;
2281 static void vga_save_dpy_resize(DisplayState
*s
)
2285 static void vga_save_dpy_refresh(DisplayState
*s
)
2289 int ppm_save(const char *filename
, struct DisplaySurface
*ds
)
2297 char *linebuf
, *pbuf
;
2299 f
= fopen(filename
, "wb");
2302 fprintf(f
, "P6\n%d %d\n%d\n",
2303 ds
->width
, ds
->height
, 255);
2304 linebuf
= g_malloc(ds
->width
* 3);
2306 for(y
= 0; y
< ds
->height
; y
++) {
2309 for(x
= 0; x
< ds
->width
; x
++) {
2310 if (ds
->pf
.bits_per_pixel
== 32)
2313 v
= (uint32_t) (*(uint16_t *)d
);
2314 r
= ((v
>> ds
->pf
.rshift
) & ds
->pf
.rmax
) * 256 /
2316 g
= ((v
>> ds
->pf
.gshift
) & ds
->pf
.gmax
) * 256 /
2318 b
= ((v
>> ds
->pf
.bshift
) & ds
->pf
.bmax
) * 256 /
2323 d
+= ds
->pf
.bytes_per_pixel
;
2326 ret
= fwrite(linebuf
, 1, pbuf
- linebuf
, f
);
2334 static DisplayChangeListener
* vga_screen_dump_init(DisplayState
*ds
)
2336 DisplayChangeListener
*dcl
;
2338 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
2339 dcl
->dpy_update
= vga_save_dpy_update
;
2340 dcl
->dpy_resize
= vga_save_dpy_resize
;
2341 dcl
->dpy_refresh
= vga_save_dpy_refresh
;
2342 register_displaychangelistener(ds
, dcl
);
2346 /* save the vga display in a PPM image even if no display is
2348 static void vga_screen_dump(void *opaque
, const char *filename
)
2350 VGACommonState
*s
= opaque
;
2352 if (!screen_dump_dcl
)
2353 screen_dump_dcl
= vga_screen_dump_init(s
->ds
);
2355 screen_dump_filename
= (char *)filename
;
2356 vga_invalidate_display(s
);