Statistics
| Branch: | Revision:

root / hw / vga.c @ d36b66f7

History | View | Annotate | Download (71.8 kB)

1 e89f66ec bellard
/*
2 4fa0f5d2 bellard
 * QEMU VGA Emulator.
3 5fafdf24 ths
 *
4 e89f66ec bellard
 * Copyright (c) 2003 Fabrice Bellard
5 5fafdf24 ths
 *
6 e89f66ec bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 e89f66ec bellard
 * of this software and associated documentation files (the "Software"), to deal
8 e89f66ec bellard
 * in the Software without restriction, including without limitation the rights
9 e89f66ec bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 e89f66ec bellard
 * copies of the Software, and to permit persons to whom the Software is
11 e89f66ec bellard
 * furnished to do so, subject to the following conditions:
12 e89f66ec bellard
 *
13 e89f66ec bellard
 * The above copyright notice and this permission notice shall be included in
14 e89f66ec bellard
 * all copies or substantial portions of the Software.
15 e89f66ec bellard
 *
16 e89f66ec bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 e89f66ec bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 e89f66ec bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 e89f66ec bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 e89f66ec bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 e89f66ec bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 e89f66ec bellard
 * THE SOFTWARE.
23 e89f66ec bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 5e55efc9 Blue Swirl
#include "vga.h"
26 87ecb68b pbrook
#include "console.h"
27 87ecb68b pbrook
#include "pc.h"
28 87ecb68b pbrook
#include "pci.h"
29 798b0c25 bellard
#include "vga_int.h"
30 94470844 blueswir1
#include "pixel_ops.h"
31 cb5a7aa8 malc
#include "qemu-timer.h"
32 c65adf9b Avi Kivity
#include "xen.h"
33 72750018 Alon Levy
#include "trace.h"
34 e89f66ec bellard
35 e89f66ec bellard
//#define DEBUG_VGA
36 17b0018b bellard
//#define DEBUG_VGA_MEM
37 a41bc9af bellard
//#define DEBUG_VGA_REG
38 a41bc9af bellard
39 4fa0f5d2 bellard
//#define DEBUG_BOCHS_VBE
40 4fa0f5d2 bellard
41 9aa0ff0b Jan Kiszka
/* 16 state changes per vertical frame @60 Hz */
42 9aa0ff0b Jan Kiszka
#define VGA_TEXT_CURSOR_PERIOD_MS       (1000 * 2 * 16 / 60)
43 9aa0ff0b Jan Kiszka
44 47c012e2 Blue Swirl
/*
45 47c012e2 Blue Swirl
 * Video Graphics Array (VGA)
46 47c012e2 Blue Swirl
 *
47 47c012e2 Blue Swirl
 * Chipset docs for original IBM VGA:
48 47c012e2 Blue Swirl
 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
49 47c012e2 Blue Swirl
 *
50 47c012e2 Blue Swirl
 * FreeVGA site:
51 47c012e2 Blue Swirl
 * http://www.osdever.net/FreeVGA/home.htm
52 47c012e2 Blue Swirl
 *
53 47c012e2 Blue Swirl
 * Standard VGA features and Bochs VBE extensions are implemented.
54 47c012e2 Blue Swirl
 */
55 47c012e2 Blue Swirl
56 e89f66ec bellard
/* force some bits to zero */
57 798b0c25 bellard
const uint8_t sr_mask[8] = {
58 9e622b15 blueswir1
    0x03,
59 9e622b15 blueswir1
    0x3d,
60 9e622b15 blueswir1
    0x0f,
61 9e622b15 blueswir1
    0x3f,
62 9e622b15 blueswir1
    0x0e,
63 9e622b15 blueswir1
    0x00,
64 9e622b15 blueswir1
    0x00,
65 9e622b15 blueswir1
    0xff,
66 e89f66ec bellard
};
67 e89f66ec bellard
68 798b0c25 bellard
const uint8_t gr_mask[16] = {
69 9e622b15 blueswir1
    0x0f, /* 0x00 */
70 9e622b15 blueswir1
    0x0f, /* 0x01 */
71 9e622b15 blueswir1
    0x0f, /* 0x02 */
72 9e622b15 blueswir1
    0x1f, /* 0x03 */
73 9e622b15 blueswir1
    0x03, /* 0x04 */
74 9e622b15 blueswir1
    0x7b, /* 0x05 */
75 9e622b15 blueswir1
    0x0f, /* 0x06 */
76 9e622b15 blueswir1
    0x0f, /* 0x07 */
77 9e622b15 blueswir1
    0xff, /* 0x08 */
78 9e622b15 blueswir1
    0x00, /* 0x09 */
79 9e622b15 blueswir1
    0x00, /* 0x0a */
80 9e622b15 blueswir1
    0x00, /* 0x0b */
81 9e622b15 blueswir1
    0x00, /* 0x0c */
82 9e622b15 blueswir1
    0x00, /* 0x0d */
83 9e622b15 blueswir1
    0x00, /* 0x0e */
84 9e622b15 blueswir1
    0x00, /* 0x0f */
85 e89f66ec bellard
};
86 e89f66ec bellard
87 e89f66ec bellard
#define cbswap_32(__x) \
88 e89f66ec bellard
((uint32_t)( \
89 e89f66ec bellard
                (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
90 e89f66ec bellard
                (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) <<  8) | \
91 e89f66ec bellard
                (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >>  8) | \
92 e89f66ec bellard
                (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
93 e89f66ec bellard
94 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
95 e89f66ec bellard
#define PAT(x) cbswap_32(x)
96 e89f66ec bellard
#else
97 e89f66ec bellard
#define PAT(x) (x)
98 e89f66ec bellard
#endif
99 e89f66ec bellard
100 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
101 b8ed223b bellard
#define BIG 1
102 b8ed223b bellard
#else
103 b8ed223b bellard
#define BIG 0
104 b8ed223b bellard
#endif
105 b8ed223b bellard
106 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
107 b8ed223b bellard
#define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
108 b8ed223b bellard
#else
109 b8ed223b bellard
#define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
110 b8ed223b bellard
#endif
111 b8ed223b bellard
112 e89f66ec bellard
static const uint32_t mask16[16] = {
113 e89f66ec bellard
    PAT(0x00000000),
114 e89f66ec bellard
    PAT(0x000000ff),
115 e89f66ec bellard
    PAT(0x0000ff00),
116 e89f66ec bellard
    PAT(0x0000ffff),
117 e89f66ec bellard
    PAT(0x00ff0000),
118 e89f66ec bellard
    PAT(0x00ff00ff),
119 e89f66ec bellard
    PAT(0x00ffff00),
120 e89f66ec bellard
    PAT(0x00ffffff),
121 e89f66ec bellard
    PAT(0xff000000),
122 e89f66ec bellard
    PAT(0xff0000ff),
123 e89f66ec bellard
    PAT(0xff00ff00),
124 e89f66ec bellard
    PAT(0xff00ffff),
125 e89f66ec bellard
    PAT(0xffff0000),
126 e89f66ec bellard
    PAT(0xffff00ff),
127 e89f66ec bellard
    PAT(0xffffff00),
128 e89f66ec bellard
    PAT(0xffffffff),
129 e89f66ec bellard
};
130 e89f66ec bellard
131 e89f66ec bellard
#undef PAT
132 e89f66ec bellard
133 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
134 e89f66ec bellard
#define PAT(x) (x)
135 e89f66ec bellard
#else
136 e89f66ec bellard
#define PAT(x) cbswap_32(x)
137 e89f66ec bellard
#endif
138 e89f66ec bellard
139 e89f66ec bellard
static const uint32_t dmask16[16] = {
140 e89f66ec bellard
    PAT(0x00000000),
141 e89f66ec bellard
    PAT(0x000000ff),
142 e89f66ec bellard
    PAT(0x0000ff00),
143 e89f66ec bellard
    PAT(0x0000ffff),
144 e89f66ec bellard
    PAT(0x00ff0000),
145 e89f66ec bellard
    PAT(0x00ff00ff),
146 e89f66ec bellard
    PAT(0x00ffff00),
147 e89f66ec bellard
    PAT(0x00ffffff),
148 e89f66ec bellard
    PAT(0xff000000),
149 e89f66ec bellard
    PAT(0xff0000ff),
150 e89f66ec bellard
    PAT(0xff00ff00),
151 e89f66ec bellard
    PAT(0xff00ffff),
152 e89f66ec bellard
    PAT(0xffff0000),
153 e89f66ec bellard
    PAT(0xffff00ff),
154 e89f66ec bellard
    PAT(0xffffff00),
155 e89f66ec bellard
    PAT(0xffffffff),
156 e89f66ec bellard
};
157 e89f66ec bellard
158 e89f66ec bellard
static const uint32_t dmask4[4] = {
159 e89f66ec bellard
    PAT(0x00000000),
160 e89f66ec bellard
    PAT(0x0000ffff),
161 e89f66ec bellard
    PAT(0xffff0000),
162 e89f66ec bellard
    PAT(0xffffffff),
163 e89f66ec bellard
};
164 e89f66ec bellard
165 e89f66ec bellard
static uint32_t expand4[256];
166 e89f66ec bellard
static uint16_t expand2[256];
167 17b0018b bellard
static uint8_t expand4to8[16];
168 e89f66ec bellard
169 d7098135 Luiz Capitulino
static void vga_screen_dump(void *opaque, const char *filename, bool cswitch,
170 d7098135 Luiz Capitulino
                            Error **errp);
171 95219897 pbrook
172 80763888 Jan Kiszka
static void vga_update_memory_access(VGACommonState *s)
173 80763888 Jan Kiszka
{
174 80763888 Jan Kiszka
    MemoryRegion *region, *old_region = s->chain4_alias;
175 a8170e5e Avi Kivity
    hwaddr base, offset, size;
176 80763888 Jan Kiszka
177 80763888 Jan Kiszka
    s->chain4_alias = NULL;
178 80763888 Jan Kiszka
179 5e55efc9 Blue Swirl
    if ((s->sr[VGA_SEQ_PLANE_WRITE] & VGA_SR02_ALL_PLANES) ==
180 5e55efc9 Blue Swirl
        VGA_SR02_ALL_PLANES && s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
181 80763888 Jan Kiszka
        offset = 0;
182 5e55efc9 Blue Swirl
        switch ((s->gr[VGA_GFX_MISC] >> 2) & 3) {
183 80763888 Jan Kiszka
        case 0:
184 80763888 Jan Kiszka
            base = 0xa0000;
185 80763888 Jan Kiszka
            size = 0x20000;
186 80763888 Jan Kiszka
            break;
187 80763888 Jan Kiszka
        case 1:
188 80763888 Jan Kiszka
            base = 0xa0000;
189 80763888 Jan Kiszka
            size = 0x10000;
190 80763888 Jan Kiszka
            offset = s->bank_offset;
191 80763888 Jan Kiszka
            break;
192 80763888 Jan Kiszka
        case 2:
193 80763888 Jan Kiszka
            base = 0xb0000;
194 80763888 Jan Kiszka
            size = 0x8000;
195 80763888 Jan Kiszka
            break;
196 80763888 Jan Kiszka
        case 3:
197 f065aa0a Jan Kiszka
        default:
198 80763888 Jan Kiszka
            base = 0xb8000;
199 80763888 Jan Kiszka
            size = 0x8000;
200 80763888 Jan Kiszka
            break;
201 80763888 Jan Kiszka
        }
202 71579cae Jan Kiszka
        base += isa_mem_base;
203 80763888 Jan Kiszka
        region = g_malloc(sizeof(*region));
204 80763888 Jan Kiszka
        memory_region_init_alias(region, "vga.chain4", &s->vram, offset, size);
205 80763888 Jan Kiszka
        memory_region_add_subregion_overlap(s->legacy_address_space, base,
206 80763888 Jan Kiszka
                                            region, 2);
207 80763888 Jan Kiszka
        s->chain4_alias = region;
208 80763888 Jan Kiszka
    }
209 80763888 Jan Kiszka
    if (old_region) {
210 80763888 Jan Kiszka
        memory_region_del_subregion(s->legacy_address_space, old_region);
211 80763888 Jan Kiszka
        memory_region_destroy(old_region);
212 80763888 Jan Kiszka
        g_free(old_region);
213 80763888 Jan Kiszka
        s->plane_updated = 0xf;
214 80763888 Jan Kiszka
    }
215 80763888 Jan Kiszka
}
216 80763888 Jan Kiszka
217 cedd91d2 Juan Quintela
static void vga_dumb_update_retrace_info(VGACommonState *s)
218 cb5a7aa8 malc
{
219 cb5a7aa8 malc
    (void) s;
220 cb5a7aa8 malc
}
221 cb5a7aa8 malc
222 cedd91d2 Juan Quintela
static void vga_precise_update_retrace_info(VGACommonState *s)
223 cb5a7aa8 malc
{
224 cb5a7aa8 malc
    int htotal_chars;
225 cb5a7aa8 malc
    int hretr_start_char;
226 cb5a7aa8 malc
    int hretr_skew_chars;
227 cb5a7aa8 malc
    int hretr_end_char;
228 cb5a7aa8 malc
229 cb5a7aa8 malc
    int vtotal_lines;
230 cb5a7aa8 malc
    int vretr_start_line;
231 cb5a7aa8 malc
    int vretr_end_line;
232 cb5a7aa8 malc
233 7f5b7d3e Blue Swirl
    int dots;
234 7f5b7d3e Blue Swirl
#if 0
235 7f5b7d3e Blue Swirl
    int div2, sldiv2;
236 7f5b7d3e Blue Swirl
#endif
237 cb5a7aa8 malc
    int clocking_mode;
238 cb5a7aa8 malc
    int clock_sel;
239 b0f74c87 balrog
    const int clk_hz[] = {25175000, 28322000, 25175000, 25175000};
240 cb5a7aa8 malc
    int64_t chars_per_sec;
241 cb5a7aa8 malc
    struct vga_precise_retrace *r = &s->retrace_info.precise;
242 cb5a7aa8 malc
243 5e55efc9 Blue Swirl
    htotal_chars = s->cr[VGA_CRTC_H_TOTAL] + 5;
244 5e55efc9 Blue Swirl
    hretr_start_char = s->cr[VGA_CRTC_H_SYNC_START];
245 5e55efc9 Blue Swirl
    hretr_skew_chars = (s->cr[VGA_CRTC_H_SYNC_END] >> 5) & 3;
246 5e55efc9 Blue Swirl
    hretr_end_char = s->cr[VGA_CRTC_H_SYNC_END] & 0x1f;
247 cb5a7aa8 malc
248 5e55efc9 Blue Swirl
    vtotal_lines = (s->cr[VGA_CRTC_V_TOTAL] |
249 5e55efc9 Blue Swirl
                    (((s->cr[VGA_CRTC_OVERFLOW] & 1) |
250 5e55efc9 Blue Swirl
                      ((s->cr[VGA_CRTC_OVERFLOW] >> 4) & 2)) << 8)) + 2;
251 5e55efc9 Blue Swirl
    vretr_start_line = s->cr[VGA_CRTC_V_SYNC_START] |
252 5e55efc9 Blue Swirl
        ((((s->cr[VGA_CRTC_OVERFLOW] >> 2) & 1) |
253 5e55efc9 Blue Swirl
          ((s->cr[VGA_CRTC_OVERFLOW] >> 6) & 2)) << 8);
254 5e55efc9 Blue Swirl
    vretr_end_line = s->cr[VGA_CRTC_V_SYNC_END] & 0xf;
255 cb5a7aa8 malc
256 5e55efc9 Blue Swirl
    clocking_mode = (s->sr[VGA_SEQ_CLOCK_MODE] >> 3) & 1;
257 cb5a7aa8 malc
    clock_sel = (s->msr >> 2) & 3;
258 f87fc09b malc
    dots = (s->msr & 1) ? 8 : 9;
259 cb5a7aa8 malc
260 b0f74c87 balrog
    chars_per_sec = clk_hz[clock_sel] / dots;
261 cb5a7aa8 malc
262 cb5a7aa8 malc
    htotal_chars <<= clocking_mode;
263 cb5a7aa8 malc
264 cb5a7aa8 malc
    r->total_chars = vtotal_lines * htotal_chars;
265 cb5a7aa8 malc
    if (r->freq) {
266 6ee093c9 Juan Quintela
        r->ticks_per_char = get_ticks_per_sec() / (r->total_chars * r->freq);
267 cb5a7aa8 malc
    } else {
268 6ee093c9 Juan Quintela
        r->ticks_per_char = get_ticks_per_sec() / chars_per_sec;
269 cb5a7aa8 malc
    }
270 cb5a7aa8 malc
271 cb5a7aa8 malc
    r->vstart = vretr_start_line;
272 cb5a7aa8 malc
    r->vend = r->vstart + vretr_end_line + 1;
273 cb5a7aa8 malc
274 cb5a7aa8 malc
    r->hstart = hretr_start_char + hretr_skew_chars;
275 cb5a7aa8 malc
    r->hend = r->hstart + hretr_end_char + 1;
276 cb5a7aa8 malc
    r->htotal = htotal_chars;
277 cb5a7aa8 malc
278 f87fc09b malc
#if 0
279 5e55efc9 Blue Swirl
    div2 = (s->cr[VGA_CRTC_MODE] >> 2) & 1;
280 5e55efc9 Blue Swirl
    sldiv2 = (s->cr[VGA_CRTC_MODE] >> 3) & 1;
281 cb5a7aa8 malc
    printf (
282 f87fc09b malc
        "hz=%f\n"
283 cb5a7aa8 malc
        "htotal = %d\n"
284 cb5a7aa8 malc
        "hretr_start = %d\n"
285 cb5a7aa8 malc
        "hretr_skew = %d\n"
286 cb5a7aa8 malc
        "hretr_end = %d\n"
287 cb5a7aa8 malc
        "vtotal = %d\n"
288 cb5a7aa8 malc
        "vretr_start = %d\n"
289 cb5a7aa8 malc
        "vretr_end = %d\n"
290 cb5a7aa8 malc
        "div2 = %d sldiv2 = %d\n"
291 cb5a7aa8 malc
        "clocking_mode = %d\n"
292 cb5a7aa8 malc
        "clock_sel = %d %d\n"
293 cb5a7aa8 malc
        "dots = %d\n"
294 0bfcd599 Blue Swirl
        "ticks/char = %" PRId64 "\n"
295 cb5a7aa8 malc
        "\n",
296 6ee093c9 Juan Quintela
        (double) get_ticks_per_sec() / (r->ticks_per_char * r->total_chars),
297 cb5a7aa8 malc
        htotal_chars,
298 cb5a7aa8 malc
        hretr_start_char,
299 cb5a7aa8 malc
        hretr_skew_chars,
300 cb5a7aa8 malc
        hretr_end_char,
301 cb5a7aa8 malc
        vtotal_lines,
302 cb5a7aa8 malc
        vretr_start_line,
303 cb5a7aa8 malc
        vretr_end_line,
304 cb5a7aa8 malc
        div2, sldiv2,
305 cb5a7aa8 malc
        clocking_mode,
306 cb5a7aa8 malc
        clock_sel,
307 b0f74c87 balrog
        clk_hz[clock_sel],
308 cb5a7aa8 malc
        dots,
309 cb5a7aa8 malc
        r->ticks_per_char
310 cb5a7aa8 malc
        );
311 cb5a7aa8 malc
#endif
312 cb5a7aa8 malc
}
313 cb5a7aa8 malc
314 cedd91d2 Juan Quintela
static uint8_t vga_precise_retrace(VGACommonState *s)
315 cb5a7aa8 malc
{
316 cb5a7aa8 malc
    struct vga_precise_retrace *r = &s->retrace_info.precise;
317 cb5a7aa8 malc
    uint8_t val = s->st01 & ~(ST01_V_RETRACE | ST01_DISP_ENABLE);
318 cb5a7aa8 malc
319 cb5a7aa8 malc
    if (r->total_chars) {
320 cb5a7aa8 malc
        int cur_line, cur_line_char, cur_char;
321 cb5a7aa8 malc
        int64_t cur_tick;
322 cb5a7aa8 malc
323 74475455 Paolo Bonzini
        cur_tick = qemu_get_clock_ns(vm_clock);
324 cb5a7aa8 malc
325 cb5a7aa8 malc
        cur_char = (cur_tick / r->ticks_per_char) % r->total_chars;
326 cb5a7aa8 malc
        cur_line = cur_char / r->htotal;
327 cb5a7aa8 malc
328 cb5a7aa8 malc
        if (cur_line >= r->vstart && cur_line <= r->vend) {
329 cb5a7aa8 malc
            val |= ST01_V_RETRACE | ST01_DISP_ENABLE;
330 f87fc09b malc
        } else {
331 f87fc09b malc
            cur_line_char = cur_char % r->htotal;
332 f87fc09b malc
            if (cur_line_char >= r->hstart && cur_line_char <= r->hend) {
333 f87fc09b malc
                val |= ST01_DISP_ENABLE;
334 f87fc09b malc
            }
335 cb5a7aa8 malc
        }
336 cb5a7aa8 malc
337 cb5a7aa8 malc
        return val;
338 cb5a7aa8 malc
    } else {
339 cb5a7aa8 malc
        return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
340 cb5a7aa8 malc
    }
341 cb5a7aa8 malc
}
342 cb5a7aa8 malc
343 cedd91d2 Juan Quintela
static uint8_t vga_dumb_retrace(VGACommonState *s)
344 cb5a7aa8 malc
{
345 cb5a7aa8 malc
    return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
346 cb5a7aa8 malc
}
347 cb5a7aa8 malc
348 25a18cbd Juan Quintela
int vga_ioport_invalid(VGACommonState *s, uint32_t addr)
349 25a18cbd Juan Quintela
{
350 5e55efc9 Blue Swirl
    if (s->msr & VGA_MIS_COLOR) {
351 25a18cbd Juan Quintela
        /* Color */
352 25a18cbd Juan Quintela
        return (addr >= 0x3b0 && addr <= 0x3bf);
353 25a18cbd Juan Quintela
    } else {
354 25a18cbd Juan Quintela
        /* Monochrome */
355 25a18cbd Juan Quintela
        return (addr >= 0x3d0 && addr <= 0x3df);
356 25a18cbd Juan Quintela
    }
357 25a18cbd Juan Quintela
}
358 25a18cbd Juan Quintela
359 43bf782b Juan Quintela
uint32_t vga_ioport_read(void *opaque, uint32_t addr)
360 e89f66ec bellard
{
361 43bf782b Juan Quintela
    VGACommonState *s = opaque;
362 e89f66ec bellard
    int val, index;
363 e89f66ec bellard
364 bd8f2f5d Jan Kiszka
    qemu_flush_coalesced_mmio_buffer();
365 bd8f2f5d Jan Kiszka
366 25a18cbd Juan Quintela
    if (vga_ioport_invalid(s, addr)) {
367 e89f66ec bellard
        val = 0xff;
368 e89f66ec bellard
    } else {
369 e89f66ec bellard
        switch(addr) {
370 5e55efc9 Blue Swirl
        case VGA_ATT_W:
371 e89f66ec bellard
            if (s->ar_flip_flop == 0) {
372 e89f66ec bellard
                val = s->ar_index;
373 e89f66ec bellard
            } else {
374 e89f66ec bellard
                val = 0;
375 e89f66ec bellard
            }
376 e89f66ec bellard
            break;
377 5e55efc9 Blue Swirl
        case VGA_ATT_R:
378 e89f66ec bellard
            index = s->ar_index & 0x1f;
379 5e55efc9 Blue Swirl
            if (index < VGA_ATT_C) {
380 e89f66ec bellard
                val = s->ar[index];
381 5e55efc9 Blue Swirl
            } else {
382 e89f66ec bellard
                val = 0;
383 5e55efc9 Blue Swirl
            }
384 e89f66ec bellard
            break;
385 5e55efc9 Blue Swirl
        case VGA_MIS_W:
386 e89f66ec bellard
            val = s->st00;
387 e89f66ec bellard
            break;
388 5e55efc9 Blue Swirl
        case VGA_SEQ_I:
389 e89f66ec bellard
            val = s->sr_index;
390 e89f66ec bellard
            break;
391 5e55efc9 Blue Swirl
        case VGA_SEQ_D:
392 e89f66ec bellard
            val = s->sr[s->sr_index];
393 a41bc9af bellard
#ifdef DEBUG_VGA_REG
394 a41bc9af bellard
            printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
395 a41bc9af bellard
#endif
396 e89f66ec bellard
            break;
397 5e55efc9 Blue Swirl
        case VGA_PEL_IR:
398 e89f66ec bellard
            val = s->dac_state;
399 e89f66ec bellard
            break;
400 5e55efc9 Blue Swirl
        case VGA_PEL_IW:
401 e9b43ea3 Juan Quintela
            val = s->dac_write_index;
402 e9b43ea3 Juan Quintela
            break;
403 5e55efc9 Blue Swirl
        case VGA_PEL_D:
404 e89f66ec bellard
            val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
405 e89f66ec bellard
            if (++s->dac_sub_index == 3) {
406 e89f66ec bellard
                s->dac_sub_index = 0;
407 e89f66ec bellard
                s->dac_read_index++;
408 e89f66ec bellard
            }
409 e89f66ec bellard
            break;
410 5e55efc9 Blue Swirl
        case VGA_FTC_R:
411 e89f66ec bellard
            val = s->fcr;
412 e89f66ec bellard
            break;
413 5e55efc9 Blue Swirl
        case VGA_MIS_R:
414 e89f66ec bellard
            val = s->msr;
415 e89f66ec bellard
            break;
416 5e55efc9 Blue Swirl
        case VGA_GFX_I:
417 e89f66ec bellard
            val = s->gr_index;
418 e89f66ec bellard
            break;
419 5e55efc9 Blue Swirl
        case VGA_GFX_D:
420 e89f66ec bellard
            val = s->gr[s->gr_index];
421 a41bc9af bellard
#ifdef DEBUG_VGA_REG
422 a41bc9af bellard
            printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
423 a41bc9af bellard
#endif
424 e89f66ec bellard
            break;
425 5e55efc9 Blue Swirl
        case VGA_CRT_IM:
426 5e55efc9 Blue Swirl
        case VGA_CRT_IC:
427 e89f66ec bellard
            val = s->cr_index;
428 e89f66ec bellard
            break;
429 5e55efc9 Blue Swirl
        case VGA_CRT_DM:
430 5e55efc9 Blue Swirl
        case VGA_CRT_DC:
431 e89f66ec bellard
            val = s->cr[s->cr_index];
432 a41bc9af bellard
#ifdef DEBUG_VGA_REG
433 a41bc9af bellard
            printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
434 a41bc9af bellard
#endif
435 e89f66ec bellard
            break;
436 5e55efc9 Blue Swirl
        case VGA_IS1_RM:
437 5e55efc9 Blue Swirl
        case VGA_IS1_RC:
438 e89f66ec bellard
            /* just toggle to fool polling */
439 cb5a7aa8 malc
            val = s->st01 = s->retrace(s);
440 e89f66ec bellard
            s->ar_flip_flop = 0;
441 e89f66ec bellard
            break;
442 e89f66ec bellard
        default:
443 e89f66ec bellard
            val = 0x00;
444 e89f66ec bellard
            break;
445 e89f66ec bellard
        }
446 e89f66ec bellard
    }
447 4fa0f5d2 bellard
#if defined(DEBUG_VGA)
448 e89f66ec bellard
    printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
449 e89f66ec bellard
#endif
450 e89f66ec bellard
    return val;
451 e89f66ec bellard
}
452 e89f66ec bellard
453 43bf782b Juan Quintela
void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
454 e89f66ec bellard
{
455 43bf782b Juan Quintela
    VGACommonState *s = opaque;
456 5467a722 bellard
    int index;
457 e89f66ec bellard
458 bd8f2f5d Jan Kiszka
    qemu_flush_coalesced_mmio_buffer();
459 bd8f2f5d Jan Kiszka
460 e89f66ec bellard
    /* check port range access depending on color/monochrome mode */
461 25a18cbd Juan Quintela
    if (vga_ioport_invalid(s, addr)) {
462 e89f66ec bellard
        return;
463 25a18cbd Juan Quintela
    }
464 e89f66ec bellard
#ifdef DEBUG_VGA
465 e89f66ec bellard
    printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
466 e89f66ec bellard
#endif
467 e89f66ec bellard
468 e89f66ec bellard
    switch(addr) {
469 5e55efc9 Blue Swirl
    case VGA_ATT_W:
470 e89f66ec bellard
        if (s->ar_flip_flop == 0) {
471 e89f66ec bellard
            val &= 0x3f;
472 e89f66ec bellard
            s->ar_index = val;
473 e89f66ec bellard
        } else {
474 e89f66ec bellard
            index = s->ar_index & 0x1f;
475 e89f66ec bellard
            switch(index) {
476 5e55efc9 Blue Swirl
            case VGA_ATC_PALETTE0 ... VGA_ATC_PALETTEF:
477 e89f66ec bellard
                s->ar[index] = val & 0x3f;
478 e89f66ec bellard
                break;
479 5e55efc9 Blue Swirl
            case VGA_ATC_MODE:
480 e89f66ec bellard
                s->ar[index] = val & ~0x10;
481 e89f66ec bellard
                break;
482 5e55efc9 Blue Swirl
            case VGA_ATC_OVERSCAN:
483 e89f66ec bellard
                s->ar[index] = val;
484 e89f66ec bellard
                break;
485 5e55efc9 Blue Swirl
            case VGA_ATC_PLANE_ENABLE:
486 e89f66ec bellard
                s->ar[index] = val & ~0xc0;
487 e89f66ec bellard
                break;
488 5e55efc9 Blue Swirl
            case VGA_ATC_PEL:
489 e89f66ec bellard
                s->ar[index] = val & ~0xf0;
490 e89f66ec bellard
                break;
491 5e55efc9 Blue Swirl
            case VGA_ATC_COLOR_PAGE:
492 e89f66ec bellard
                s->ar[index] = val & ~0xf0;
493 e89f66ec bellard
                break;
494 e89f66ec bellard
            default:
495 e89f66ec bellard
                break;
496 e89f66ec bellard
            }
497 e89f66ec bellard
        }
498 e89f66ec bellard
        s->ar_flip_flop ^= 1;
499 e89f66ec bellard
        break;
500 5e55efc9 Blue Swirl
    case VGA_MIS_W:
501 e89f66ec bellard
        s->msr = val & ~0x10;
502 cb5a7aa8 malc
        s->update_retrace_info(s);
503 e89f66ec bellard
        break;
504 5e55efc9 Blue Swirl
    case VGA_SEQ_I:
505 e89f66ec bellard
        s->sr_index = val & 7;
506 e89f66ec bellard
        break;
507 5e55efc9 Blue Swirl
    case VGA_SEQ_D:
508 a41bc9af bellard
#ifdef DEBUG_VGA_REG
509 a41bc9af bellard
        printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
510 a41bc9af bellard
#endif
511 e89f66ec bellard
        s->sr[s->sr_index] = val & sr_mask[s->sr_index];
512 5e55efc9 Blue Swirl
        if (s->sr_index == VGA_SEQ_CLOCK_MODE) {
513 5e55efc9 Blue Swirl
            s->update_retrace_info(s);
514 5e55efc9 Blue Swirl
        }
515 80763888 Jan Kiszka
        vga_update_memory_access(s);
516 e89f66ec bellard
        break;
517 5e55efc9 Blue Swirl
    case VGA_PEL_IR:
518 e89f66ec bellard
        s->dac_read_index = val;
519 e89f66ec bellard
        s->dac_sub_index = 0;
520 e89f66ec bellard
        s->dac_state = 3;
521 e89f66ec bellard
        break;
522 5e55efc9 Blue Swirl
    case VGA_PEL_IW:
523 e89f66ec bellard
        s->dac_write_index = val;
524 e89f66ec bellard
        s->dac_sub_index = 0;
525 e89f66ec bellard
        s->dac_state = 0;
526 e89f66ec bellard
        break;
527 5e55efc9 Blue Swirl
    case VGA_PEL_D:
528 e89f66ec bellard
        s->dac_cache[s->dac_sub_index] = val;
529 e89f66ec bellard
        if (++s->dac_sub_index == 3) {
530 e89f66ec bellard
            memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
531 e89f66ec bellard
            s->dac_sub_index = 0;
532 e89f66ec bellard
            s->dac_write_index++;
533 e89f66ec bellard
        }
534 e89f66ec bellard
        break;
535 5e55efc9 Blue Swirl
    case VGA_GFX_I:
536 e89f66ec bellard
        s->gr_index = val & 0x0f;
537 e89f66ec bellard
        break;
538 5e55efc9 Blue Swirl
    case VGA_GFX_D:
539 a41bc9af bellard
#ifdef DEBUG_VGA_REG
540 a41bc9af bellard
        printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
541 a41bc9af bellard
#endif
542 e89f66ec bellard
        s->gr[s->gr_index] = val & gr_mask[s->gr_index];
543 80763888 Jan Kiszka
        vga_update_memory_access(s);
544 e89f66ec bellard
        break;
545 5e55efc9 Blue Swirl
    case VGA_CRT_IM:
546 5e55efc9 Blue Swirl
    case VGA_CRT_IC:
547 e89f66ec bellard
        s->cr_index = val;
548 e89f66ec bellard
        break;
549 5e55efc9 Blue Swirl
    case VGA_CRT_DM:
550 5e55efc9 Blue Swirl
    case VGA_CRT_DC:
551 a41bc9af bellard
#ifdef DEBUG_VGA_REG
552 a41bc9af bellard
        printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
553 a41bc9af bellard
#endif
554 e89f66ec bellard
        /* handle CR0-7 protection */
555 df800210 malc
        if ((s->cr[VGA_CRTC_V_SYNC_END] & VGA_CR11_LOCK_CR0_CR7) &&
556 df800210 malc
            s->cr_index <= VGA_CRTC_OVERFLOW) {
557 df800210 malc
            /* can always write bit 4 of CR7 */
558 df800210 malc
            if (s->cr_index == VGA_CRTC_OVERFLOW) {
559 df800210 malc
                s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x10) |
560 df800210 malc
                    (val & 0x10);
561 5e55efc9 Blue Swirl
            }
562 df800210 malc
            return;
563 e89f66ec bellard
        }
564 a46007a0 Juan Quintela
        s->cr[s->cr_index] = val;
565 cb5a7aa8 malc
566 cb5a7aa8 malc
        switch(s->cr_index) {
567 5e55efc9 Blue Swirl
        case VGA_CRTC_H_TOTAL:
568 5e55efc9 Blue Swirl
        case VGA_CRTC_H_SYNC_START:
569 5e55efc9 Blue Swirl
        case VGA_CRTC_H_SYNC_END:
570 5e55efc9 Blue Swirl
        case VGA_CRTC_V_TOTAL:
571 5e55efc9 Blue Swirl
        case VGA_CRTC_OVERFLOW:
572 5e55efc9 Blue Swirl
        case VGA_CRTC_V_SYNC_END:
573 5e55efc9 Blue Swirl
        case VGA_CRTC_MODE:
574 cb5a7aa8 malc
            s->update_retrace_info(s);
575 cb5a7aa8 malc
            break;
576 cb5a7aa8 malc
        }
577 e89f66ec bellard
        break;
578 5e55efc9 Blue Swirl
    case VGA_IS1_RM:
579 5e55efc9 Blue Swirl
    case VGA_IS1_RC:
580 e89f66ec bellard
        s->fcr = val & 0x10;
581 e89f66ec bellard
        break;
582 e89f66ec bellard
    }
583 e89f66ec bellard
}
584 e89f66ec bellard
585 09a79b49 bellard
static uint32_t vbe_ioport_read_index(void *opaque, uint32_t addr)
586 4fa0f5d2 bellard
{
587 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
588 4fa0f5d2 bellard
    uint32_t val;
589 09a79b49 bellard
    val = s->vbe_index;
590 09a79b49 bellard
    return val;
591 09a79b49 bellard
}
592 4fa0f5d2 bellard
593 803ff052 Gerd Hoffmann
uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
594 09a79b49 bellard
{
595 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
596 09a79b49 bellard
    uint32_t val;
597 09a79b49 bellard
598 af92284b Gerd Hoffmann
    if (s->vbe_index < VBE_DISPI_INDEX_NB) {
599 8454df8b bellard
        if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_GETCAPS) {
600 8454df8b bellard
            switch(s->vbe_index) {
601 8454df8b bellard
                /* XXX: do not hardcode ? */
602 8454df8b bellard
            case VBE_DISPI_INDEX_XRES:
603 8454df8b bellard
                val = VBE_DISPI_MAX_XRES;
604 8454df8b bellard
                break;
605 8454df8b bellard
            case VBE_DISPI_INDEX_YRES:
606 8454df8b bellard
                val = VBE_DISPI_MAX_YRES;
607 8454df8b bellard
                break;
608 8454df8b bellard
            case VBE_DISPI_INDEX_BPP:
609 8454df8b bellard
                val = VBE_DISPI_MAX_BPP;
610 8454df8b bellard
                break;
611 8454df8b bellard
            default:
612 5fafdf24 ths
                val = s->vbe_regs[s->vbe_index];
613 8454df8b bellard
                break;
614 8454df8b bellard
            }
615 8454df8b bellard
        } else {
616 5fafdf24 ths
            val = s->vbe_regs[s->vbe_index];
617 8454df8b bellard
        }
618 af92284b Gerd Hoffmann
    } else if (s->vbe_index == VBE_DISPI_INDEX_VIDEO_MEMORY_64K) {
619 af92284b Gerd Hoffmann
        val = s->vram_size / (64 * 1024);
620 8454df8b bellard
    } else {
621 09a79b49 bellard
        val = 0;
622 8454df8b bellard
    }
623 4fa0f5d2 bellard
#ifdef DEBUG_BOCHS_VBE
624 09a79b49 bellard
    printf("VBE: read index=0x%x val=0x%x\n", s->vbe_index, val);
625 4fa0f5d2 bellard
#endif
626 4fa0f5d2 bellard
    return val;
627 4fa0f5d2 bellard
}
628 4fa0f5d2 bellard
629 803ff052 Gerd Hoffmann
void vbe_ioport_write_index(void *opaque, uint32_t addr, uint32_t val)
630 09a79b49 bellard
{
631 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
632 09a79b49 bellard
    s->vbe_index = val;
633 09a79b49 bellard
}
634 09a79b49 bellard
635 803ff052 Gerd Hoffmann
void vbe_ioport_write_data(void *opaque, uint32_t addr, uint32_t val)
636 4fa0f5d2 bellard
{
637 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
638 4fa0f5d2 bellard
639 09a79b49 bellard
    if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
640 4fa0f5d2 bellard
#ifdef DEBUG_BOCHS_VBE
641 4fa0f5d2 bellard
        printf("VBE: write index=0x%x val=0x%x\n", s->vbe_index, val);
642 4fa0f5d2 bellard
#endif
643 4fa0f5d2 bellard
        switch(s->vbe_index) {
644 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_ID:
645 cae61cef bellard
            if (val == VBE_DISPI_ID0 ||
646 cae61cef bellard
                val == VBE_DISPI_ID1 ||
647 37dd208d bellard
                val == VBE_DISPI_ID2 ||
648 37dd208d bellard
                val == VBE_DISPI_ID3 ||
649 37dd208d bellard
                val == VBE_DISPI_ID4) {
650 cae61cef bellard
                s->vbe_regs[s->vbe_index] = val;
651 cae61cef bellard
            }
652 4fa0f5d2 bellard
            break;
653 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_XRES:
654 cae61cef bellard
            if ((val <= VBE_DISPI_MAX_XRES) && ((val & 7) == 0)) {
655 cae61cef bellard
                s->vbe_regs[s->vbe_index] = val;
656 cae61cef bellard
            }
657 4fa0f5d2 bellard
            break;
658 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_YRES:
659 cae61cef bellard
            if (val <= VBE_DISPI_MAX_YRES) {
660 cae61cef bellard
                s->vbe_regs[s->vbe_index] = val;
661 cae61cef bellard
            }
662 4fa0f5d2 bellard
            break;
663 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_BPP:
664 4fa0f5d2 bellard
            if (val == 0)
665 4fa0f5d2 bellard
                val = 8;
666 5fafdf24 ths
            if (val == 4 || val == 8 || val == 15 ||
667 cae61cef bellard
                val == 16 || val == 24 || val == 32) {
668 cae61cef bellard
                s->vbe_regs[s->vbe_index] = val;
669 cae61cef bellard
            }
670 4fa0f5d2 bellard
            break;
671 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_BANK:
672 42fc925e bellard
            if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
673 42fc925e bellard
              val &= (s->vbe_bank_mask >> 2);
674 42fc925e bellard
            } else {
675 42fc925e bellard
              val &= s->vbe_bank_mask;
676 42fc925e bellard
            }
677 cae61cef bellard
            s->vbe_regs[s->vbe_index] = val;
678 26aa7d72 bellard
            s->bank_offset = (val << 16);
679 80763888 Jan Kiszka
            vga_update_memory_access(s);
680 4fa0f5d2 bellard
            break;
681 4fa0f5d2 bellard
        case VBE_DISPI_INDEX_ENABLE:
682 8454df8b bellard
            if ((val & VBE_DISPI_ENABLED) &&
683 8454df8b bellard
                !(s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) {
684 4fa0f5d2 bellard
                int h, shift_control;
685 4fa0f5d2 bellard
686 5fafdf24 ths
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] =
687 4fa0f5d2 bellard
                    s->vbe_regs[VBE_DISPI_INDEX_XRES];
688 5fafdf24 ths
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] =
689 4fa0f5d2 bellard
                    s->vbe_regs[VBE_DISPI_INDEX_YRES];
690 4fa0f5d2 bellard
                s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET] = 0;
691 4fa0f5d2 bellard
                s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET] = 0;
692 3b46e624 ths
693 4fa0f5d2 bellard
                if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
694 4fa0f5d2 bellard
                    s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 1;
695 4fa0f5d2 bellard
                else
696 5fafdf24 ths
                    s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] *
697 4fa0f5d2 bellard
                        ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
698 4fa0f5d2 bellard
                s->vbe_start_addr = 0;
699 8454df8b bellard
700 4fa0f5d2 bellard
                /* clear the screen (should be done in BIOS) */
701 4fa0f5d2 bellard
                if (!(val & VBE_DISPI_NOCLEARMEM)) {
702 5fafdf24 ths
                    memset(s->vram_ptr, 0,
703 4fa0f5d2 bellard
                           s->vbe_regs[VBE_DISPI_INDEX_YRES] * s->vbe_line_offset);
704 4fa0f5d2 bellard
                }
705 3b46e624 ths
706 cae61cef bellard
                /* we initialize the VGA graphic mode (should be done
707 cae61cef bellard
                   in BIOS) */
708 5e55efc9 Blue Swirl
                /* graphic mode + memory map 1 */
709 5e55efc9 Blue Swirl
                s->gr[VGA_GFX_MISC] = (s->gr[VGA_GFX_MISC] & ~0x0c) | 0x04 |
710 5e55efc9 Blue Swirl
                    VGA_GR06_GRAPHICS_MODE;
711 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_MODE] |= 3; /* no CGA modes */
712 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_OFFSET] = s->vbe_line_offset >> 3;
713 4fa0f5d2 bellard
                /* width */
714 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_H_DISP] =
715 5e55efc9 Blue Swirl
                    (s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
716 8454df8b bellard
                /* height (only meaningful if < 1024) */
717 4fa0f5d2 bellard
                h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
718 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_V_DISP_END] = h;
719 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x42) |
720 4fa0f5d2 bellard
                    ((h >> 7) & 0x02) | ((h >> 3) & 0x40);
721 4fa0f5d2 bellard
                /* line compare to 1023 */
722 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_LINE_COMPARE] = 0xff;
723 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_OVERFLOW] |= 0x10;
724 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_MAX_SCAN] |= 0x40;
725 3b46e624 ths
726 4fa0f5d2 bellard
                if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
727 4fa0f5d2 bellard
                    shift_control = 0;
728 5e55efc9 Blue Swirl
                    s->sr[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
729 4fa0f5d2 bellard
                } else {
730 4fa0f5d2 bellard
                    shift_control = 2;
731 5e55efc9 Blue Swirl
                    /* set chain 4 mode */
732 5e55efc9 Blue Swirl
                    s->sr[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
733 5e55efc9 Blue Swirl
                    /* activate all planes */
734 5e55efc9 Blue Swirl
                    s->sr[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
735 4fa0f5d2 bellard
                }
736 5e55efc9 Blue Swirl
                s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
737 5e55efc9 Blue Swirl
                    (shift_control << 5);
738 5e55efc9 Blue Swirl
                s->cr[VGA_CRTC_MAX_SCAN] &= ~0x9f; /* no double scan */
739 cae61cef bellard
            } else {
740 cae61cef bellard
                /* XXX: the bios should do that */
741 26aa7d72 bellard
                s->bank_offset = 0;
742 cae61cef bellard
            }
743 37dd208d bellard
            s->dac_8bit = (val & VBE_DISPI_8BIT_DAC) > 0;
744 141253b2 bellard
            s->vbe_regs[s->vbe_index] = val;
745 80763888 Jan Kiszka
            vga_update_memory_access(s);
746 cae61cef bellard
            break;
747 cae61cef bellard
        case VBE_DISPI_INDEX_VIRT_WIDTH:
748 cae61cef bellard
            {
749 cae61cef bellard
                int w, h, line_offset;
750 cae61cef bellard
751 cae61cef bellard
                if (val < s->vbe_regs[VBE_DISPI_INDEX_XRES])
752 cae61cef bellard
                    return;
753 cae61cef bellard
                w = val;
754 cae61cef bellard
                if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
755 cae61cef bellard
                    line_offset = w >> 1;
756 cae61cef bellard
                else
757 cae61cef bellard
                    line_offset = w * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
758 cae61cef bellard
                h = s->vram_size / line_offset;
759 cae61cef bellard
                /* XXX: support weird bochs semantics ? */
760 cae61cef bellard
                if (h < s->vbe_regs[VBE_DISPI_INDEX_YRES])
761 cae61cef bellard
                    return;
762 cae61cef bellard
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] = w;
763 cae61cef bellard
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] = h;
764 cae61cef bellard
                s->vbe_line_offset = line_offset;
765 cae61cef bellard
            }
766 cae61cef bellard
            break;
767 cae61cef bellard
        case VBE_DISPI_INDEX_X_OFFSET:
768 cae61cef bellard
        case VBE_DISPI_INDEX_Y_OFFSET:
769 cae61cef bellard
            {
770 cae61cef bellard
                int x;
771 cae61cef bellard
                s->vbe_regs[s->vbe_index] = val;
772 cae61cef bellard
                s->vbe_start_addr = s->vbe_line_offset * s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET];
773 cae61cef bellard
                x = s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET];
774 cae61cef bellard
                if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
775 cae61cef bellard
                    s->vbe_start_addr += x >> 1;
776 cae61cef bellard
                else
777 cae61cef bellard
                    s->vbe_start_addr += x * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
778 cae61cef bellard
                s->vbe_start_addr >>= 2;
779 4fa0f5d2 bellard
            }
780 4fa0f5d2 bellard
            break;
781 4fa0f5d2 bellard
        default:
782 4fa0f5d2 bellard
            break;
783 4fa0f5d2 bellard
        }
784 4fa0f5d2 bellard
    }
785 4fa0f5d2 bellard
}
786 4fa0f5d2 bellard
787 e89f66ec bellard
/* called for accesses between 0xa0000 and 0xc0000 */
788 a8170e5e Avi Kivity
uint32_t vga_mem_readb(VGACommonState *s, hwaddr addr)
789 e89f66ec bellard
{
790 e89f66ec bellard
    int memory_map_mode, plane;
791 e89f66ec bellard
    uint32_t ret;
792 3b46e624 ths
793 e89f66ec bellard
    /* convert to VGA memory offset */
794 5e55efc9 Blue Swirl
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
795 26aa7d72 bellard
    addr &= 0x1ffff;
796 e89f66ec bellard
    switch(memory_map_mode) {
797 e89f66ec bellard
    case 0:
798 e89f66ec bellard
        break;
799 e89f66ec bellard
    case 1:
800 26aa7d72 bellard
        if (addr >= 0x10000)
801 e89f66ec bellard
            return 0xff;
802 cae61cef bellard
        addr += s->bank_offset;
803 e89f66ec bellard
        break;
804 e89f66ec bellard
    case 2:
805 26aa7d72 bellard
        addr -= 0x10000;
806 e89f66ec bellard
        if (addr >= 0x8000)
807 e89f66ec bellard
            return 0xff;
808 e89f66ec bellard
        break;
809 e89f66ec bellard
    default:
810 e89f66ec bellard
    case 3:
811 26aa7d72 bellard
        addr -= 0x18000;
812 c92b2e84 bellard
        if (addr >= 0x8000)
813 c92b2e84 bellard
            return 0xff;
814 e89f66ec bellard
        break;
815 e89f66ec bellard
    }
816 3b46e624 ths
817 5e55efc9 Blue Swirl
    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
818 e89f66ec bellard
        /* chain 4 mode : simplest access */
819 e89f66ec bellard
        ret = s->vram_ptr[addr];
820 5e55efc9 Blue Swirl
    } else if (s->gr[VGA_GFX_MODE] & 0x10) {
821 e89f66ec bellard
        /* odd/even mode (aka text mode mapping) */
822 5e55efc9 Blue Swirl
        plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
823 e89f66ec bellard
        ret = s->vram_ptr[((addr & ~1) << 1) | plane];
824 e89f66ec bellard
    } else {
825 e89f66ec bellard
        /* standard VGA latched access */
826 e89f66ec bellard
        s->latch = ((uint32_t *)s->vram_ptr)[addr];
827 e89f66ec bellard
828 5e55efc9 Blue Swirl
        if (!(s->gr[VGA_GFX_MODE] & 0x08)) {
829 e89f66ec bellard
            /* read mode 0 */
830 5e55efc9 Blue Swirl
            plane = s->gr[VGA_GFX_PLANE_READ];
831 b8ed223b bellard
            ret = GET_PLANE(s->latch, plane);
832 e89f66ec bellard
        } else {
833 e89f66ec bellard
            /* read mode 1 */
834 5e55efc9 Blue Swirl
            ret = (s->latch ^ mask16[s->gr[VGA_GFX_COMPARE_VALUE]]) &
835 5e55efc9 Blue Swirl
                mask16[s->gr[VGA_GFX_COMPARE_MASK]];
836 e89f66ec bellard
            ret |= ret >> 16;
837 e89f66ec bellard
            ret |= ret >> 8;
838 e89f66ec bellard
            ret = (~ret) & 0xff;
839 e89f66ec bellard
        }
840 e89f66ec bellard
    }
841 e89f66ec bellard
    return ret;
842 e89f66ec bellard
}
843 e89f66ec bellard
844 e89f66ec bellard
/* called for accesses between 0xa0000 and 0xc0000 */
845 a8170e5e Avi Kivity
void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
846 e89f66ec bellard
{
847 546fa6ab bellard
    int memory_map_mode, plane, write_mode, b, func_select, mask;
848 e89f66ec bellard
    uint32_t write_mask, bit_mask, set_mask;
849 e89f66ec bellard
850 17b0018b bellard
#ifdef DEBUG_VGA_MEM
851 0bf9e31a Blue Swirl
    printf("vga: [0x" TARGET_FMT_plx "] = 0x%02x\n", addr, val);
852 e89f66ec bellard
#endif
853 e89f66ec bellard
    /* convert to VGA memory offset */
854 5e55efc9 Blue Swirl
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
855 26aa7d72 bellard
    addr &= 0x1ffff;
856 e89f66ec bellard
    switch(memory_map_mode) {
857 e89f66ec bellard
    case 0:
858 e89f66ec bellard
        break;
859 e89f66ec bellard
    case 1:
860 26aa7d72 bellard
        if (addr >= 0x10000)
861 e89f66ec bellard
            return;
862 cae61cef bellard
        addr += s->bank_offset;
863 e89f66ec bellard
        break;
864 e89f66ec bellard
    case 2:
865 26aa7d72 bellard
        addr -= 0x10000;
866 e89f66ec bellard
        if (addr >= 0x8000)
867 e89f66ec bellard
            return;
868 e89f66ec bellard
        break;
869 e89f66ec bellard
    default:
870 e89f66ec bellard
    case 3:
871 26aa7d72 bellard
        addr -= 0x18000;
872 c92b2e84 bellard
        if (addr >= 0x8000)
873 c92b2e84 bellard
            return;
874 e89f66ec bellard
        break;
875 e89f66ec bellard
    }
876 3b46e624 ths
877 5e55efc9 Blue Swirl
    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
878 e89f66ec bellard
        /* chain 4 mode : simplest access */
879 e89f66ec bellard
        plane = addr & 3;
880 546fa6ab bellard
        mask = (1 << plane);
881 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
882 e89f66ec bellard
            s->vram_ptr[addr] = val;
883 17b0018b bellard
#ifdef DEBUG_VGA_MEM
884 0bf9e31a Blue Swirl
            printf("vga: chain4: [0x" TARGET_FMT_plx "]\n", addr);
885 e89f66ec bellard
#endif
886 546fa6ab bellard
            s->plane_updated |= mask; /* only used to detect font change */
887 fd4aa979 Blue Swirl
            memory_region_set_dirty(&s->vram, addr, 1);
888 e89f66ec bellard
        }
889 5e55efc9 Blue Swirl
    } else if (s->gr[VGA_GFX_MODE] & 0x10) {
890 e89f66ec bellard
        /* odd/even mode (aka text mode mapping) */
891 5e55efc9 Blue Swirl
        plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
892 546fa6ab bellard
        mask = (1 << plane);
893 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_PLANE_WRITE] & mask) {
894 e89f66ec bellard
            addr = ((addr & ~1) << 1) | plane;
895 e89f66ec bellard
            s->vram_ptr[addr] = val;
896 17b0018b bellard
#ifdef DEBUG_VGA_MEM
897 0bf9e31a Blue Swirl
            printf("vga: odd/even: [0x" TARGET_FMT_plx "]\n", addr);
898 e89f66ec bellard
#endif
899 546fa6ab bellard
            s->plane_updated |= mask; /* only used to detect font change */
900 fd4aa979 Blue Swirl
            memory_region_set_dirty(&s->vram, addr, 1);
901 e89f66ec bellard
        }
902 e89f66ec bellard
    } else {
903 e89f66ec bellard
        /* standard VGA latched access */
904 5e55efc9 Blue Swirl
        write_mode = s->gr[VGA_GFX_MODE] & 3;
905 e89f66ec bellard
        switch(write_mode) {
906 e89f66ec bellard
        default:
907 e89f66ec bellard
        case 0:
908 e89f66ec bellard
            /* rotate */
909 5e55efc9 Blue Swirl
            b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
910 e89f66ec bellard
            val = ((val >> b) | (val << (8 - b))) & 0xff;
911 e89f66ec bellard
            val |= val << 8;
912 e89f66ec bellard
            val |= val << 16;
913 e89f66ec bellard
914 e89f66ec bellard
            /* apply set/reset mask */
915 5e55efc9 Blue Swirl
            set_mask = mask16[s->gr[VGA_GFX_SR_ENABLE]];
916 5e55efc9 Blue Swirl
            val = (val & ~set_mask) |
917 5e55efc9 Blue Swirl
                (mask16[s->gr[VGA_GFX_SR_VALUE]] & set_mask);
918 5e55efc9 Blue Swirl
            bit_mask = s->gr[VGA_GFX_BIT_MASK];
919 e89f66ec bellard
            break;
920 e89f66ec bellard
        case 1:
921 e89f66ec bellard
            val = s->latch;
922 e89f66ec bellard
            goto do_write;
923 e89f66ec bellard
        case 2:
924 e89f66ec bellard
            val = mask16[val & 0x0f];
925 5e55efc9 Blue Swirl
            bit_mask = s->gr[VGA_GFX_BIT_MASK];
926 e89f66ec bellard
            break;
927 e89f66ec bellard
        case 3:
928 e89f66ec bellard
            /* rotate */
929 5e55efc9 Blue Swirl
            b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
930 a41bc9af bellard
            val = (val >> b) | (val << (8 - b));
931 e89f66ec bellard
932 5e55efc9 Blue Swirl
            bit_mask = s->gr[VGA_GFX_BIT_MASK] & val;
933 5e55efc9 Blue Swirl
            val = mask16[s->gr[VGA_GFX_SR_VALUE]];
934 e89f66ec bellard
            break;
935 e89f66ec bellard
        }
936 e89f66ec bellard
937 e89f66ec bellard
        /* apply logical operation */
938 5e55efc9 Blue Swirl
        func_select = s->gr[VGA_GFX_DATA_ROTATE] >> 3;
939 e89f66ec bellard
        switch(func_select) {
940 e89f66ec bellard
        case 0:
941 e89f66ec bellard
        default:
942 e89f66ec bellard
            /* nothing to do */
943 e89f66ec bellard
            break;
944 e89f66ec bellard
        case 1:
945 e89f66ec bellard
            /* and */
946 e89f66ec bellard
            val &= s->latch;
947 e89f66ec bellard
            break;
948 e89f66ec bellard
        case 2:
949 e89f66ec bellard
            /* or */
950 e89f66ec bellard
            val |= s->latch;
951 e89f66ec bellard
            break;
952 e89f66ec bellard
        case 3:
953 e89f66ec bellard
            /* xor */
954 e89f66ec bellard
            val ^= s->latch;
955 e89f66ec bellard
            break;
956 e89f66ec bellard
        }
957 e89f66ec bellard
958 e89f66ec bellard
        /* apply bit mask */
959 e89f66ec bellard
        bit_mask |= bit_mask << 8;
960 e89f66ec bellard
        bit_mask |= bit_mask << 16;
961 e89f66ec bellard
        val = (val & bit_mask) | (s->latch & ~bit_mask);
962 e89f66ec bellard
963 e89f66ec bellard
    do_write:
964 e89f66ec bellard
        /* mask data according to sr[2] */
965 5e55efc9 Blue Swirl
        mask = s->sr[VGA_SEQ_PLANE_WRITE];
966 546fa6ab bellard
        s->plane_updated |= mask; /* only used to detect font change */
967 546fa6ab bellard
        write_mask = mask16[mask];
968 5fafdf24 ths
        ((uint32_t *)s->vram_ptr)[addr] =
969 5fafdf24 ths
            (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
970 e89f66ec bellard
            (val & write_mask);
971 17b0018b bellard
#ifdef DEBUG_VGA_MEM
972 0bf9e31a Blue Swirl
        printf("vga: latch: [0x" TARGET_FMT_plx "] mask=0x%08x val=0x%08x\n",
973 0bf9e31a Blue Swirl
               addr * 4, write_mask, val);
974 e89f66ec bellard
#endif
975 fd4aa979 Blue Swirl
        memory_region_set_dirty(&s->vram, addr << 2, sizeof(uint32_t));
976 e89f66ec bellard
    }
977 e89f66ec bellard
}
978 e89f66ec bellard
979 e89f66ec bellard
typedef void vga_draw_glyph8_func(uint8_t *d, int linesize,
980 e89f66ec bellard
                             const uint8_t *font_ptr, int h,
981 e89f66ec bellard
                             uint32_t fgcol, uint32_t bgcol);
982 e89f66ec bellard
typedef void vga_draw_glyph9_func(uint8_t *d, int linesize,
983 5fafdf24 ths
                                  const uint8_t *font_ptr, int h,
984 e89f66ec bellard
                                  uint32_t fgcol, uint32_t bgcol, int dup9);
985 cedd91d2 Juan Quintela
typedef void vga_draw_line_func(VGACommonState *s1, uint8_t *d,
986 e89f66ec bellard
                                const uint8_t *s, int width);
987 e89f66ec bellard
988 e89f66ec bellard
#define DEPTH 8
989 e89f66ec bellard
#include "vga_template.h"
990 e89f66ec bellard
991 e89f66ec bellard
#define DEPTH 15
992 e89f66ec bellard
#include "vga_template.h"
993 e89f66ec bellard
994 a2502b58 blueswir1
#define BGR_FORMAT
995 a2502b58 blueswir1
#define DEPTH 15
996 a2502b58 blueswir1
#include "vga_template.h"
997 a2502b58 blueswir1
998 a2502b58 blueswir1
#define DEPTH 16
999 a2502b58 blueswir1
#include "vga_template.h"
1000 a2502b58 blueswir1
1001 a2502b58 blueswir1
#define BGR_FORMAT
1002 e89f66ec bellard
#define DEPTH 16
1003 e89f66ec bellard
#include "vga_template.h"
1004 e89f66ec bellard
1005 e89f66ec bellard
#define DEPTH 32
1006 e89f66ec bellard
#include "vga_template.h"
1007 e89f66ec bellard
1008 d3079cd2 bellard
#define BGR_FORMAT
1009 d3079cd2 bellard
#define DEPTH 32
1010 d3079cd2 bellard
#include "vga_template.h"
1011 d3079cd2 bellard
1012 17b0018b bellard
static unsigned int rgb_to_pixel8_dup(unsigned int r, unsigned int g, unsigned b)
1013 17b0018b bellard
{
1014 17b0018b bellard
    unsigned int col;
1015 17b0018b bellard
    col = rgb_to_pixel8(r, g, b);
1016 17b0018b bellard
    col |= col << 8;
1017 17b0018b bellard
    col |= col << 16;
1018 17b0018b bellard
    return col;
1019 17b0018b bellard
}
1020 17b0018b bellard
1021 17b0018b bellard
static unsigned int rgb_to_pixel15_dup(unsigned int r, unsigned int g, unsigned b)
1022 17b0018b bellard
{
1023 17b0018b bellard
    unsigned int col;
1024 17b0018b bellard
    col = rgb_to_pixel15(r, g, b);
1025 17b0018b bellard
    col |= col << 16;
1026 17b0018b bellard
    return col;
1027 17b0018b bellard
}
1028 17b0018b bellard
1029 b29169d2 blueswir1
static unsigned int rgb_to_pixel15bgr_dup(unsigned int r, unsigned int g,
1030 b29169d2 blueswir1
                                          unsigned int b)
1031 b29169d2 blueswir1
{
1032 b29169d2 blueswir1
    unsigned int col;
1033 b29169d2 blueswir1
    col = rgb_to_pixel15bgr(r, g, b);
1034 b29169d2 blueswir1
    col |= col << 16;
1035 b29169d2 blueswir1
    return col;
1036 b29169d2 blueswir1
}
1037 b29169d2 blueswir1
1038 17b0018b bellard
static unsigned int rgb_to_pixel16_dup(unsigned int r, unsigned int g, unsigned b)
1039 17b0018b bellard
{
1040 17b0018b bellard
    unsigned int col;
1041 17b0018b bellard
    col = rgb_to_pixel16(r, g, b);
1042 17b0018b bellard
    col |= col << 16;
1043 17b0018b bellard
    return col;
1044 17b0018b bellard
}
1045 17b0018b bellard
1046 b29169d2 blueswir1
static unsigned int rgb_to_pixel16bgr_dup(unsigned int r, unsigned int g,
1047 b29169d2 blueswir1
                                          unsigned int b)
1048 b29169d2 blueswir1
{
1049 b29169d2 blueswir1
    unsigned int col;
1050 b29169d2 blueswir1
    col = rgb_to_pixel16bgr(r, g, b);
1051 b29169d2 blueswir1
    col |= col << 16;
1052 b29169d2 blueswir1
    return col;
1053 b29169d2 blueswir1
}
1054 b29169d2 blueswir1
1055 17b0018b bellard
static unsigned int rgb_to_pixel32_dup(unsigned int r, unsigned int g, unsigned b)
1056 17b0018b bellard
{
1057 17b0018b bellard
    unsigned int col;
1058 17b0018b bellard
    col = rgb_to_pixel32(r, g, b);
1059 17b0018b bellard
    return col;
1060 17b0018b bellard
}
1061 17b0018b bellard
1062 d3079cd2 bellard
static unsigned int rgb_to_pixel32bgr_dup(unsigned int r, unsigned int g, unsigned b)
1063 d3079cd2 bellard
{
1064 d3079cd2 bellard
    unsigned int col;
1065 d3079cd2 bellard
    col = rgb_to_pixel32bgr(r, g, b);
1066 d3079cd2 bellard
    return col;
1067 d3079cd2 bellard
}
1068 d3079cd2 bellard
1069 e89f66ec bellard
/* return true if the palette was modified */
1070 cedd91d2 Juan Quintela
static int update_palette16(VGACommonState *s)
1071 e89f66ec bellard
{
1072 17b0018b bellard
    int full_update, i;
1073 e89f66ec bellard
    uint32_t v, col, *palette;
1074 e89f66ec bellard
1075 e89f66ec bellard
    full_update = 0;
1076 e89f66ec bellard
    palette = s->last_palette;
1077 e89f66ec bellard
    for(i = 0; i < 16; i++) {
1078 e89f66ec bellard
        v = s->ar[i];
1079 5e55efc9 Blue Swirl
        if (s->ar[VGA_ATC_MODE] & 0x80) {
1080 5e55efc9 Blue Swirl
            v = ((s->ar[VGA_ATC_COLOR_PAGE] & 0xf) << 4) | (v & 0xf);
1081 5e55efc9 Blue Swirl
        } else {
1082 5e55efc9 Blue Swirl
            v = ((s->ar[VGA_ATC_COLOR_PAGE] & 0xc) << 4) | (v & 0x3f);
1083 5e55efc9 Blue Swirl
        }
1084 e89f66ec bellard
        v = v * 3;
1085 5fafdf24 ths
        col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
1086 5fafdf24 ths
                              c6_to_8(s->palette[v + 1]),
1087 17b0018b bellard
                              c6_to_8(s->palette[v + 2]));
1088 17b0018b bellard
        if (col != palette[i]) {
1089 17b0018b bellard
            full_update = 1;
1090 17b0018b bellard
            palette[i] = col;
1091 e89f66ec bellard
        }
1092 17b0018b bellard
    }
1093 17b0018b bellard
    return full_update;
1094 17b0018b bellard
}
1095 17b0018b bellard
1096 17b0018b bellard
/* return true if the palette was modified */
1097 cedd91d2 Juan Quintela
static int update_palette256(VGACommonState *s)
1098 17b0018b bellard
{
1099 17b0018b bellard
    int full_update, i;
1100 17b0018b bellard
    uint32_t v, col, *palette;
1101 17b0018b bellard
1102 17b0018b bellard
    full_update = 0;
1103 17b0018b bellard
    palette = s->last_palette;
1104 17b0018b bellard
    v = 0;
1105 17b0018b bellard
    for(i = 0; i < 256; i++) {
1106 37dd208d bellard
        if (s->dac_8bit) {
1107 5fafdf24 ths
          col = s->rgb_to_pixel(s->palette[v],
1108 5fafdf24 ths
                                s->palette[v + 1],
1109 37dd208d bellard
                                s->palette[v + 2]);
1110 37dd208d bellard
        } else {
1111 5fafdf24 ths
          col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
1112 5fafdf24 ths
                                c6_to_8(s->palette[v + 1]),
1113 37dd208d bellard
                                c6_to_8(s->palette[v + 2]));
1114 37dd208d bellard
        }
1115 e89f66ec bellard
        if (col != palette[i]) {
1116 e89f66ec bellard
            full_update = 1;
1117 e89f66ec bellard
            palette[i] = col;
1118 e89f66ec bellard
        }
1119 17b0018b bellard
        v += 3;
1120 e89f66ec bellard
    }
1121 e89f66ec bellard
    return full_update;
1122 e89f66ec bellard
}
1123 e89f66ec bellard
1124 cedd91d2 Juan Quintela
static void vga_get_offsets(VGACommonState *s,
1125 5fafdf24 ths
                            uint32_t *pline_offset,
1126 83acc96b bellard
                            uint32_t *pstart_addr,
1127 83acc96b bellard
                            uint32_t *pline_compare)
1128 e89f66ec bellard
{
1129 83acc96b bellard
    uint32_t start_addr, line_offset, line_compare;
1130 a96d8bea Gerd Hoffmann
1131 4fa0f5d2 bellard
    if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
1132 4fa0f5d2 bellard
        line_offset = s->vbe_line_offset;
1133 4fa0f5d2 bellard
        start_addr = s->vbe_start_addr;
1134 83acc96b bellard
        line_compare = 65535;
1135 a96d8bea Gerd Hoffmann
    } else {
1136 4fa0f5d2 bellard
        /* compute line_offset in bytes */
1137 5e55efc9 Blue Swirl
        line_offset = s->cr[VGA_CRTC_OFFSET];
1138 4fa0f5d2 bellard
        line_offset <<= 3;
1139 08e48902 bellard
1140 4fa0f5d2 bellard
        /* starting address */
1141 5e55efc9 Blue Swirl
        start_addr = s->cr[VGA_CRTC_START_LO] |
1142 5e55efc9 Blue Swirl
            (s->cr[VGA_CRTC_START_HI] << 8);
1143 83acc96b bellard
1144 83acc96b bellard
        /* line compare */
1145 5e55efc9 Blue Swirl
        line_compare = s->cr[VGA_CRTC_LINE_COMPARE] |
1146 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x10) << 4) |
1147 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_MAX_SCAN] & 0x40) << 3);
1148 4fa0f5d2 bellard
    }
1149 798b0c25 bellard
    *pline_offset = line_offset;
1150 798b0c25 bellard
    *pstart_addr = start_addr;
1151 83acc96b bellard
    *pline_compare = line_compare;
1152 798b0c25 bellard
}
1153 798b0c25 bellard
1154 798b0c25 bellard
/* update start_addr and line_offset. Return TRUE if modified */
1155 cedd91d2 Juan Quintela
static int update_basic_params(VGACommonState *s)
1156 798b0c25 bellard
{
1157 798b0c25 bellard
    int full_update;
1158 798b0c25 bellard
    uint32_t start_addr, line_offset, line_compare;
1159 3b46e624 ths
1160 798b0c25 bellard
    full_update = 0;
1161 798b0c25 bellard
1162 83acc96b bellard
    s->get_offsets(s, &line_offset, &start_addr, &line_compare);
1163 e89f66ec bellard
1164 e89f66ec bellard
    if (line_offset != s->line_offset ||
1165 e89f66ec bellard
        start_addr != s->start_addr ||
1166 e89f66ec bellard
        line_compare != s->line_compare) {
1167 e89f66ec bellard
        s->line_offset = line_offset;
1168 e89f66ec bellard
        s->start_addr = start_addr;
1169 e89f66ec bellard
        s->line_compare = line_compare;
1170 e89f66ec bellard
        full_update = 1;
1171 e89f66ec bellard
    }
1172 e89f66ec bellard
    return full_update;
1173 e89f66ec bellard
}
1174 e89f66ec bellard
1175 b29169d2 blueswir1
#define NB_DEPTHS 7
1176 d3079cd2 bellard
1177 d3079cd2 bellard
static inline int get_depth_index(DisplayState *s)
1178 e89f66ec bellard
{
1179 0e1f5a0c aliguori
    switch(ds_get_bits_per_pixel(s)) {
1180 e89f66ec bellard
    default:
1181 e89f66ec bellard
    case 8:
1182 e89f66ec bellard
        return 0;
1183 e89f66ec bellard
    case 15:
1184 8927bcfd aliguori
        return 1;
1185 e89f66ec bellard
    case 16:
1186 8927bcfd aliguori
        return 2;
1187 e89f66ec bellard
    case 32:
1188 7b5d76da aliguori
        if (is_surface_bgr(s->surface))
1189 7b5d76da aliguori
            return 4;
1190 7b5d76da aliguori
        else
1191 7b5d76da aliguori
            return 3;
1192 e89f66ec bellard
    }
1193 e89f66ec bellard
}
1194 e89f66ec bellard
1195 68f04a3c Blue Swirl
static vga_draw_glyph8_func * const vga_draw_glyph8_table[NB_DEPTHS] = {
1196 e89f66ec bellard
    vga_draw_glyph8_8,
1197 e89f66ec bellard
    vga_draw_glyph8_16,
1198 e89f66ec bellard
    vga_draw_glyph8_16,
1199 e89f66ec bellard
    vga_draw_glyph8_32,
1200 d3079cd2 bellard
    vga_draw_glyph8_32,
1201 b29169d2 blueswir1
    vga_draw_glyph8_16,
1202 b29169d2 blueswir1
    vga_draw_glyph8_16,
1203 e89f66ec bellard
};
1204 e89f66ec bellard
1205 68f04a3c Blue Swirl
static vga_draw_glyph8_func * const vga_draw_glyph16_table[NB_DEPTHS] = {
1206 17b0018b bellard
    vga_draw_glyph16_8,
1207 17b0018b bellard
    vga_draw_glyph16_16,
1208 17b0018b bellard
    vga_draw_glyph16_16,
1209 17b0018b bellard
    vga_draw_glyph16_32,
1210 d3079cd2 bellard
    vga_draw_glyph16_32,
1211 b29169d2 blueswir1
    vga_draw_glyph16_16,
1212 b29169d2 blueswir1
    vga_draw_glyph16_16,
1213 17b0018b bellard
};
1214 17b0018b bellard
1215 68f04a3c Blue Swirl
static vga_draw_glyph9_func * const vga_draw_glyph9_table[NB_DEPTHS] = {
1216 e89f66ec bellard
    vga_draw_glyph9_8,
1217 e89f66ec bellard
    vga_draw_glyph9_16,
1218 e89f66ec bellard
    vga_draw_glyph9_16,
1219 e89f66ec bellard
    vga_draw_glyph9_32,
1220 d3079cd2 bellard
    vga_draw_glyph9_32,
1221 b29169d2 blueswir1
    vga_draw_glyph9_16,
1222 b29169d2 blueswir1
    vga_draw_glyph9_16,
1223 e89f66ec bellard
};
1224 3b46e624 ths
1225 e89f66ec bellard
static const uint8_t cursor_glyph[32 * 4] = {
1226 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1227 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1228 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1229 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1230 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1231 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1232 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1233 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1234 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1235 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1236 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1237 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1238 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1239 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1240 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1241 e89f66ec bellard
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1242 3b46e624 ths
};
1243 e89f66ec bellard
1244 cedd91d2 Juan Quintela
static void vga_get_text_resolution(VGACommonState *s, int *pwidth, int *pheight,
1245 4c5e8c5c blueswir1
                                    int *pcwidth, int *pcheight)
1246 4c5e8c5c blueswir1
{
1247 4c5e8c5c blueswir1
    int width, cwidth, height, cheight;
1248 4c5e8c5c blueswir1
1249 4c5e8c5c blueswir1
    /* total width & height */
1250 5e55efc9 Blue Swirl
    cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
1251 4c5e8c5c blueswir1
    cwidth = 8;
1252 5e55efc9 Blue Swirl
    if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
1253 4c5e8c5c blueswir1
        cwidth = 9;
1254 5e55efc9 Blue Swirl
    }
1255 5e55efc9 Blue Swirl
    if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
1256 4c5e8c5c blueswir1
        cwidth = 16; /* NOTE: no 18 pixel wide */
1257 5e55efc9 Blue Swirl
    }
1258 5e55efc9 Blue Swirl
    width = (s->cr[VGA_CRTC_H_DISP] + 1);
1259 5e55efc9 Blue Swirl
    if (s->cr[VGA_CRTC_V_TOTAL] == 100) {
1260 4c5e8c5c blueswir1
        /* ugly hack for CGA 160x100x16 - explain me the logic */
1261 4c5e8c5c blueswir1
        height = 100;
1262 4c5e8c5c blueswir1
    } else {
1263 5e55efc9 Blue Swirl
        height = s->cr[VGA_CRTC_V_DISP_END] |
1264 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
1265 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
1266 4c5e8c5c blueswir1
        height = (height + 1) / cheight;
1267 4c5e8c5c blueswir1
    }
1268 4c5e8c5c blueswir1
1269 4c5e8c5c blueswir1
    *pwidth = width;
1270 4c5e8c5c blueswir1
    *pheight = height;
1271 4c5e8c5c blueswir1
    *pcwidth = cwidth;
1272 4c5e8c5c blueswir1
    *pcheight = cheight;
1273 4c5e8c5c blueswir1
}
1274 4c5e8c5c blueswir1
1275 7d957bd8 aliguori
typedef unsigned int rgb_to_pixel_dup_func(unsigned int r, unsigned int g, unsigned b);
1276 7d957bd8 aliguori
1277 68f04a3c Blue Swirl
static rgb_to_pixel_dup_func * const rgb_to_pixel_dup_table[NB_DEPTHS] = {
1278 bdb19571 aliguori
    rgb_to_pixel8_dup,
1279 bdb19571 aliguori
    rgb_to_pixel15_dup,
1280 bdb19571 aliguori
    rgb_to_pixel16_dup,
1281 bdb19571 aliguori
    rgb_to_pixel32_dup,
1282 bdb19571 aliguori
    rgb_to_pixel32bgr_dup,
1283 bdb19571 aliguori
    rgb_to_pixel15bgr_dup,
1284 bdb19571 aliguori
    rgb_to_pixel16bgr_dup,
1285 bdb19571 aliguori
};
1286 7d957bd8 aliguori
1287 5fafdf24 ths
/*
1288 5fafdf24 ths
 * Text mode update
1289 e89f66ec bellard
 * Missing:
1290 e89f66ec bellard
 * - double scan
1291 5fafdf24 ths
 * - double width
1292 e89f66ec bellard
 * - underline
1293 e89f66ec bellard
 * - flashing
1294 e89f66ec bellard
 */
1295 cedd91d2 Juan Quintela
static void vga_draw_text(VGACommonState *s, int full_update)
1296 e89f66ec bellard
{
1297 e89f66ec bellard
    int cx, cy, cheight, cw, ch, cattr, height, width, ch_attr;
1298 cae334cd malc
    int cx_min, cx_max, linesize, x_incr, line, line1;
1299 e89f66ec bellard
    uint32_t offset, fgcol, bgcol, v, cursor_offset;
1300 d1984194 malc
    uint8_t *d1, *d, *src, *dest, *cursor_ptr;
1301 e89f66ec bellard
    const uint8_t *font_ptr, *font_base[2];
1302 e89f66ec bellard
    int dup9, line_offset, depth_index;
1303 e89f66ec bellard
    uint32_t *palette;
1304 e89f66ec bellard
    uint32_t *ch_attr_ptr;
1305 e89f66ec bellard
    vga_draw_glyph8_func *vga_draw_glyph8;
1306 e89f66ec bellard
    vga_draw_glyph9_func *vga_draw_glyph9;
1307 9aa0ff0b Jan Kiszka
    int64_t now = qemu_get_clock_ms(vm_clock);
1308 e89f66ec bellard
1309 e89f66ec bellard
    /* compute font data address (in plane 2) */
1310 5e55efc9 Blue Swirl
    v = s->sr[VGA_SEQ_CHARACTER_MAP];
1311 1078f663 bellard
    offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
1312 e89f66ec bellard
    if (offset != s->font_offsets[0]) {
1313 e89f66ec bellard
        s->font_offsets[0] = offset;
1314 e89f66ec bellard
        full_update = 1;
1315 e89f66ec bellard
    }
1316 e89f66ec bellard
    font_base[0] = s->vram_ptr + offset;
1317 e89f66ec bellard
1318 1078f663 bellard
    offset = (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
1319 e89f66ec bellard
    font_base[1] = s->vram_ptr + offset;
1320 e89f66ec bellard
    if (offset != s->font_offsets[1]) {
1321 e89f66ec bellard
        s->font_offsets[1] = offset;
1322 e89f66ec bellard
        full_update = 1;
1323 e89f66ec bellard
    }
1324 80763888 Jan Kiszka
    if (s->plane_updated & (1 << 2) || s->chain4_alias) {
1325 546fa6ab bellard
        /* if the plane 2 was modified since the last display, it
1326 546fa6ab bellard
           indicates the font may have been modified */
1327 546fa6ab bellard
        s->plane_updated = 0;
1328 546fa6ab bellard
        full_update = 1;
1329 546fa6ab bellard
    }
1330 799e709b aliguori
    full_update |= update_basic_params(s);
1331 e89f66ec bellard
1332 e89f66ec bellard
    line_offset = s->line_offset;
1333 e89f66ec bellard
1334 4c5e8c5c blueswir1
    vga_get_text_resolution(s, &width, &height, &cw, &cheight);
1335 1b296044 Stefan Weil
    if ((height * width) <= 1) {
1336 1b296044 Stefan Weil
        /* better than nothing: exit if transient size is too small */
1337 1b296044 Stefan Weil
        return;
1338 1b296044 Stefan Weil
    }
1339 3294b949 bellard
    if ((height * width) > CH_ATTR_SIZE) {
1340 3294b949 bellard
        /* better than nothing: exit if transient size is too big */
1341 3294b949 bellard
        return;
1342 3294b949 bellard
    }
1343 3294b949 bellard
1344 799e709b aliguori
    if (width != s->last_width || height != s->last_height ||
1345 799e709b aliguori
        cw != s->last_cw || cheight != s->last_ch || s->last_depth) {
1346 799e709b aliguori
        s->last_scr_width = width * cw;
1347 799e709b aliguori
        s->last_scr_height = height * cheight;
1348 799e709b aliguori
        qemu_console_resize(s->ds, s->last_scr_width, s->last_scr_height);
1349 9678aedd Gerd Hoffmann
        dpy_text_resize(s->ds, width, height);
1350 799e709b aliguori
        s->last_depth = 0;
1351 799e709b aliguori
        s->last_width = width;
1352 799e709b aliguori
        s->last_height = height;
1353 799e709b aliguori
        s->last_ch = cheight;
1354 799e709b aliguori
        s->last_cw = cw;
1355 799e709b aliguori
        full_update = 1;
1356 799e709b aliguori
    }
1357 7d957bd8 aliguori
    s->rgb_to_pixel =
1358 7d957bd8 aliguori
        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
1359 7d957bd8 aliguori
    full_update |= update_palette16(s);
1360 7d957bd8 aliguori
    palette = s->last_palette;
1361 7d957bd8 aliguori
    x_incr = cw * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
1362 7d957bd8 aliguori
1363 9678aedd Gerd Hoffmann
    if (full_update) {
1364 9678aedd Gerd Hoffmann
        s->full_update_text = 1;
1365 9678aedd Gerd Hoffmann
    }
1366 9678aedd Gerd Hoffmann
    if (s->full_update_gfx) {
1367 9678aedd Gerd Hoffmann
        s->full_update_gfx = 0;
1368 9678aedd Gerd Hoffmann
        full_update |= 1;
1369 9678aedd Gerd Hoffmann
    }
1370 9678aedd Gerd Hoffmann
1371 5e55efc9 Blue Swirl
    cursor_offset = ((s->cr[VGA_CRTC_CURSOR_HI] << 8) |
1372 5e55efc9 Blue Swirl
                     s->cr[VGA_CRTC_CURSOR_LO]) - s->start_addr;
1373 e89f66ec bellard
    if (cursor_offset != s->cursor_offset ||
1374 5e55efc9 Blue Swirl
        s->cr[VGA_CRTC_CURSOR_START] != s->cursor_start ||
1375 5e55efc9 Blue Swirl
        s->cr[VGA_CRTC_CURSOR_END] != s->cursor_end) {
1376 e89f66ec bellard
      /* if the cursor position changed, we update the old and new
1377 e89f66ec bellard
         chars */
1378 e89f66ec bellard
        if (s->cursor_offset < CH_ATTR_SIZE)
1379 e89f66ec bellard
            s->last_ch_attr[s->cursor_offset] = -1;
1380 e89f66ec bellard
        if (cursor_offset < CH_ATTR_SIZE)
1381 e89f66ec bellard
            s->last_ch_attr[cursor_offset] = -1;
1382 e89f66ec bellard
        s->cursor_offset = cursor_offset;
1383 5e55efc9 Blue Swirl
        s->cursor_start = s->cr[VGA_CRTC_CURSOR_START];
1384 5e55efc9 Blue Swirl
        s->cursor_end = s->cr[VGA_CRTC_CURSOR_END];
1385 e89f66ec bellard
    }
1386 39cf7803 bellard
    cursor_ptr = s->vram_ptr + (s->start_addr + cursor_offset) * 4;
1387 9aa0ff0b Jan Kiszka
    if (now >= s->cursor_blink_time) {
1388 9aa0ff0b Jan Kiszka
        s->cursor_blink_time = now + VGA_TEXT_CURSOR_PERIOD_MS / 2;
1389 9aa0ff0b Jan Kiszka
        s->cursor_visible_phase = !s->cursor_visible_phase;
1390 9aa0ff0b Jan Kiszka
    }
1391 3b46e624 ths
1392 d3079cd2 bellard
    depth_index = get_depth_index(s->ds);
1393 17b0018b bellard
    if (cw == 16)
1394 17b0018b bellard
        vga_draw_glyph8 = vga_draw_glyph16_table[depth_index];
1395 17b0018b bellard
    else
1396 17b0018b bellard
        vga_draw_glyph8 = vga_draw_glyph8_table[depth_index];
1397 e89f66ec bellard
    vga_draw_glyph9 = vga_draw_glyph9_table[depth_index];
1398 3b46e624 ths
1399 0e1f5a0c aliguori
    dest = ds_get_data(s->ds);
1400 0e1f5a0c aliguori
    linesize = ds_get_linesize(s->ds);
1401 e89f66ec bellard
    ch_attr_ptr = s->last_ch_attr;
1402 d1984194 malc
    line = 0;
1403 d1984194 malc
    offset = s->start_addr * 4;
1404 e89f66ec bellard
    for(cy = 0; cy < height; cy++) {
1405 e89f66ec bellard
        d1 = dest;
1406 d1984194 malc
        src = s->vram_ptr + offset;
1407 e89f66ec bellard
        cx_min = width;
1408 e89f66ec bellard
        cx_max = -1;
1409 e89f66ec bellard
        for(cx = 0; cx < width; cx++) {
1410 e89f66ec bellard
            ch_attr = *(uint16_t *)src;
1411 9aa0ff0b Jan Kiszka
            if (full_update || ch_attr != *ch_attr_ptr || src == cursor_ptr) {
1412 e89f66ec bellard
                if (cx < cx_min)
1413 e89f66ec bellard
                    cx_min = cx;
1414 e89f66ec bellard
                if (cx > cx_max)
1415 e89f66ec bellard
                    cx_max = cx;
1416 e89f66ec bellard
                *ch_attr_ptr = ch_attr;
1417 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
1418 e89f66ec bellard
                ch = ch_attr >> 8;
1419 e89f66ec bellard
                cattr = ch_attr & 0xff;
1420 e89f66ec bellard
#else
1421 e89f66ec bellard
                ch = ch_attr & 0xff;
1422 e89f66ec bellard
                cattr = ch_attr >> 8;
1423 e89f66ec bellard
#endif
1424 e89f66ec bellard
                font_ptr = font_base[(cattr >> 3) & 1];
1425 e89f66ec bellard
                font_ptr += 32 * 4 * ch;
1426 e89f66ec bellard
                bgcol = palette[cattr >> 4];
1427 e89f66ec bellard
                fgcol = palette[cattr & 0x0f];
1428 17b0018b bellard
                if (cw != 9) {
1429 5fafdf24 ths
                    vga_draw_glyph8(d1, linesize,
1430 e89f66ec bellard
                                    font_ptr, cheight, fgcol, bgcol);
1431 e89f66ec bellard
                } else {
1432 e89f66ec bellard
                    dup9 = 0;
1433 5e55efc9 Blue Swirl
                    if (ch >= 0xb0 && ch <= 0xdf &&
1434 5e55efc9 Blue Swirl
                        (s->ar[VGA_ATC_MODE] & 0x04)) {
1435 e89f66ec bellard
                        dup9 = 1;
1436 5e55efc9 Blue Swirl
                    }
1437 5fafdf24 ths
                    vga_draw_glyph9(d1, linesize,
1438 e89f66ec bellard
                                    font_ptr, cheight, fgcol, bgcol, dup9);
1439 e89f66ec bellard
                }
1440 e89f66ec bellard
                if (src == cursor_ptr &&
1441 9aa0ff0b Jan Kiszka
                    !(s->cr[VGA_CRTC_CURSOR_START] & 0x20) &&
1442 9aa0ff0b Jan Kiszka
                    s->cursor_visible_phase) {
1443 e89f66ec bellard
                    int line_start, line_last, h;
1444 e89f66ec bellard
                    /* draw the cursor */
1445 5e55efc9 Blue Swirl
                    line_start = s->cr[VGA_CRTC_CURSOR_START] & 0x1f;
1446 5e55efc9 Blue Swirl
                    line_last = s->cr[VGA_CRTC_CURSOR_END] & 0x1f;
1447 e89f66ec bellard
                    /* XXX: check that */
1448 e89f66ec bellard
                    if (line_last > cheight - 1)
1449 e89f66ec bellard
                        line_last = cheight - 1;
1450 e89f66ec bellard
                    if (line_last >= line_start && line_start < cheight) {
1451 e89f66ec bellard
                        h = line_last - line_start + 1;
1452 e89f66ec bellard
                        d = d1 + linesize * line_start;
1453 17b0018b bellard
                        if (cw != 9) {
1454 5fafdf24 ths
                            vga_draw_glyph8(d, linesize,
1455 e89f66ec bellard
                                            cursor_glyph, h, fgcol, bgcol);
1456 e89f66ec bellard
                        } else {
1457 5fafdf24 ths
                            vga_draw_glyph9(d, linesize,
1458 e89f66ec bellard
                                            cursor_glyph, h, fgcol, bgcol, 1);
1459 e89f66ec bellard
                        }
1460 e89f66ec bellard
                    }
1461 e89f66ec bellard
                }
1462 e89f66ec bellard
            }
1463 e89f66ec bellard
            d1 += x_incr;
1464 e89f66ec bellard
            src += 4;
1465 e89f66ec bellard
            ch_attr_ptr++;
1466 e89f66ec bellard
        }
1467 e89f66ec bellard
        if (cx_max != -1) {
1468 a93a4a22 Gerd Hoffmann
            dpy_gfx_update(s->ds, cx_min * cw, cy * cheight,
1469 a93a4a22 Gerd Hoffmann
                           (cx_max - cx_min + 1) * cw, cheight);
1470 e89f66ec bellard
        }
1471 e89f66ec bellard
        dest += linesize * cheight;
1472 cae334cd malc
        line1 = line + cheight;
1473 cae334cd malc
        offset += line_offset;
1474 cae334cd malc
        if (line < s->line_compare && line1 >= s->line_compare) {
1475 d1984194 malc
            offset = 0;
1476 d1984194 malc
        }
1477 cae334cd malc
        line = line1;
1478 e89f66ec bellard
    }
1479 e89f66ec bellard
}
1480 e89f66ec bellard
1481 17b0018b bellard
enum {
1482 17b0018b bellard
    VGA_DRAW_LINE2,
1483 17b0018b bellard
    VGA_DRAW_LINE2D2,
1484 17b0018b bellard
    VGA_DRAW_LINE4,
1485 17b0018b bellard
    VGA_DRAW_LINE4D2,
1486 17b0018b bellard
    VGA_DRAW_LINE8D2,
1487 17b0018b bellard
    VGA_DRAW_LINE8,
1488 17b0018b bellard
    VGA_DRAW_LINE15,
1489 17b0018b bellard
    VGA_DRAW_LINE16,
1490 4fa0f5d2 bellard
    VGA_DRAW_LINE24,
1491 17b0018b bellard
    VGA_DRAW_LINE32,
1492 17b0018b bellard
    VGA_DRAW_LINE_NB,
1493 17b0018b bellard
};
1494 17b0018b bellard
1495 68f04a3c Blue Swirl
static vga_draw_line_func * const vga_draw_line_table[NB_DEPTHS * VGA_DRAW_LINE_NB] = {
1496 e89f66ec bellard
    vga_draw_line2_8,
1497 e89f66ec bellard
    vga_draw_line2_16,
1498 e89f66ec bellard
    vga_draw_line2_16,
1499 e89f66ec bellard
    vga_draw_line2_32,
1500 d3079cd2 bellard
    vga_draw_line2_32,
1501 b29169d2 blueswir1
    vga_draw_line2_16,
1502 b29169d2 blueswir1
    vga_draw_line2_16,
1503 e89f66ec bellard
1504 17b0018b bellard
    vga_draw_line2d2_8,
1505 17b0018b bellard
    vga_draw_line2d2_16,
1506 17b0018b bellard
    vga_draw_line2d2_16,
1507 17b0018b bellard
    vga_draw_line2d2_32,
1508 d3079cd2 bellard
    vga_draw_line2d2_32,
1509 b29169d2 blueswir1
    vga_draw_line2d2_16,
1510 b29169d2 blueswir1
    vga_draw_line2d2_16,
1511 17b0018b bellard
1512 e89f66ec bellard
    vga_draw_line4_8,
1513 e89f66ec bellard
    vga_draw_line4_16,
1514 e89f66ec bellard
    vga_draw_line4_16,
1515 e89f66ec bellard
    vga_draw_line4_32,
1516 d3079cd2 bellard
    vga_draw_line4_32,
1517 b29169d2 blueswir1
    vga_draw_line4_16,
1518 b29169d2 blueswir1
    vga_draw_line4_16,
1519 e89f66ec bellard
1520 17b0018b bellard
    vga_draw_line4d2_8,
1521 17b0018b bellard
    vga_draw_line4d2_16,
1522 17b0018b bellard
    vga_draw_line4d2_16,
1523 17b0018b bellard
    vga_draw_line4d2_32,
1524 d3079cd2 bellard
    vga_draw_line4d2_32,
1525 b29169d2 blueswir1
    vga_draw_line4d2_16,
1526 b29169d2 blueswir1
    vga_draw_line4d2_16,
1527 17b0018b bellard
1528 17b0018b bellard
    vga_draw_line8d2_8,
1529 17b0018b bellard
    vga_draw_line8d2_16,
1530 17b0018b bellard
    vga_draw_line8d2_16,
1531 17b0018b bellard
    vga_draw_line8d2_32,
1532 d3079cd2 bellard
    vga_draw_line8d2_32,
1533 b29169d2 blueswir1
    vga_draw_line8d2_16,
1534 b29169d2 blueswir1
    vga_draw_line8d2_16,
1535 17b0018b bellard
1536 e89f66ec bellard
    vga_draw_line8_8,
1537 e89f66ec bellard
    vga_draw_line8_16,
1538 e89f66ec bellard
    vga_draw_line8_16,
1539 e89f66ec bellard
    vga_draw_line8_32,
1540 d3079cd2 bellard
    vga_draw_line8_32,
1541 b29169d2 blueswir1
    vga_draw_line8_16,
1542 b29169d2 blueswir1
    vga_draw_line8_16,
1543 e89f66ec bellard
1544 e89f66ec bellard
    vga_draw_line15_8,
1545 e89f66ec bellard
    vga_draw_line15_15,
1546 e89f66ec bellard
    vga_draw_line15_16,
1547 e89f66ec bellard
    vga_draw_line15_32,
1548 d3079cd2 bellard
    vga_draw_line15_32bgr,
1549 b29169d2 blueswir1
    vga_draw_line15_15bgr,
1550 b29169d2 blueswir1
    vga_draw_line15_16bgr,
1551 e89f66ec bellard
1552 e89f66ec bellard
    vga_draw_line16_8,
1553 e89f66ec bellard
    vga_draw_line16_15,
1554 e89f66ec bellard
    vga_draw_line16_16,
1555 e89f66ec bellard
    vga_draw_line16_32,
1556 d3079cd2 bellard
    vga_draw_line16_32bgr,
1557 b29169d2 blueswir1
    vga_draw_line16_15bgr,
1558 b29169d2 blueswir1
    vga_draw_line16_16bgr,
1559 e89f66ec bellard
1560 4fa0f5d2 bellard
    vga_draw_line24_8,
1561 4fa0f5d2 bellard
    vga_draw_line24_15,
1562 4fa0f5d2 bellard
    vga_draw_line24_16,
1563 4fa0f5d2 bellard
    vga_draw_line24_32,
1564 d3079cd2 bellard
    vga_draw_line24_32bgr,
1565 b29169d2 blueswir1
    vga_draw_line24_15bgr,
1566 b29169d2 blueswir1
    vga_draw_line24_16bgr,
1567 4fa0f5d2 bellard
1568 e89f66ec bellard
    vga_draw_line32_8,
1569 e89f66ec bellard
    vga_draw_line32_15,
1570 e89f66ec bellard
    vga_draw_line32_16,
1571 e89f66ec bellard
    vga_draw_line32_32,
1572 d3079cd2 bellard
    vga_draw_line32_32bgr,
1573 b29169d2 blueswir1
    vga_draw_line32_15bgr,
1574 b29169d2 blueswir1
    vga_draw_line32_16bgr,
1575 d3079cd2 bellard
};
1576 d3079cd2 bellard
1577 cedd91d2 Juan Quintela
static int vga_get_bpp(VGACommonState *s)
1578 798b0c25 bellard
{
1579 798b0c25 bellard
    int ret;
1580 a96d8bea Gerd Hoffmann
1581 798b0c25 bellard
    if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
1582 798b0c25 bellard
        ret = s->vbe_regs[VBE_DISPI_INDEX_BPP];
1583 a96d8bea Gerd Hoffmann
    } else {
1584 798b0c25 bellard
        ret = 0;
1585 798b0c25 bellard
    }
1586 798b0c25 bellard
    return ret;
1587 798b0c25 bellard
}
1588 798b0c25 bellard
1589 cedd91d2 Juan Quintela
static void vga_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
1590 a130a41e bellard
{
1591 a130a41e bellard
    int width, height;
1592 3b46e624 ths
1593 8454df8b bellard
    if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
1594 8454df8b bellard
        width = s->vbe_regs[VBE_DISPI_INDEX_XRES];
1595 8454df8b bellard
        height = s->vbe_regs[VBE_DISPI_INDEX_YRES];
1596 a96d8bea Gerd Hoffmann
    } else {
1597 5e55efc9 Blue Swirl
        width = (s->cr[VGA_CRTC_H_DISP] + 1) * 8;
1598 5e55efc9 Blue Swirl
        height = s->cr[VGA_CRTC_V_DISP_END] |
1599 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
1600 5e55efc9 Blue Swirl
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
1601 8454df8b bellard
        height = (height + 1);
1602 8454df8b bellard
    }
1603 a130a41e bellard
    *pwidth = width;
1604 a130a41e bellard
    *pheight = height;
1605 a130a41e bellard
}
1606 a130a41e bellard
1607 cedd91d2 Juan Quintela
void vga_invalidate_scanlines(VGACommonState *s, int y1, int y2)
1608 a8aa669b bellard
{
1609 a8aa669b bellard
    int y;
1610 a8aa669b bellard
    if (y1 >= VGA_MAX_HEIGHT)
1611 a8aa669b bellard
        return;
1612 a8aa669b bellard
    if (y2 >= VGA_MAX_HEIGHT)
1613 a8aa669b bellard
        y2 = VGA_MAX_HEIGHT;
1614 a8aa669b bellard
    for(y = y1; y < y2; y++) {
1615 a8aa669b bellard
        s->invalidated_y_table[y >> 5] |= 1 << (y & 0x1f);
1616 a8aa669b bellard
    }
1617 a8aa669b bellard
}
1618 a8aa669b bellard
1619 b51d7b2e BALATON Zoltan
void vga_sync_dirty_bitmap(VGACommonState *s)
1620 2bec46dc aliguori
{
1621 b1950430 Avi Kivity
    memory_region_sync_dirty_bitmap(&s->vram);
1622 2bec46dc aliguori
}
1623 2bec46dc aliguori
1624 50af3246 Juan Quintela
void vga_dirty_log_start(VGACommonState *s)
1625 50af3246 Juan Quintela
{
1626 b1950430 Avi Kivity
    memory_region_set_log(&s->vram, true, DIRTY_MEMORY_VGA);
1627 b5cc6e32 Anthony Liguori
}
1628 b5cc6e32 Anthony Liguori
1629 b5cc6e32 Anthony Liguori
void vga_dirty_log_stop(VGACommonState *s)
1630 b5cc6e32 Anthony Liguori
{
1631 b1950430 Avi Kivity
    memory_region_set_log(&s->vram, false, DIRTY_MEMORY_VGA);
1632 b5cc6e32 Anthony Liguori
}
1633 b5cc6e32 Anthony Liguori
1634 799e709b aliguori
/*
1635 799e709b aliguori
 * graphic modes
1636 799e709b aliguori
 */
1637 cedd91d2 Juan Quintela
static void vga_draw_graphic(VGACommonState *s, int full_update)
1638 e89f66ec bellard
{
1639 12c7e75a Avi Kivity
    int y1, y, update, linesize, y_start, double_scan, mask, depth;
1640 12c7e75a Avi Kivity
    int width, height, shift_control, line_offset, bwidth, bits;
1641 c227f099 Anthony Liguori
    ram_addr_t page0, page1, page_min, page_max;
1642 a07cf92a bellard
    int disp_width, multi_scan, multi_run;
1643 799e709b aliguori
    uint8_t *d;
1644 799e709b aliguori
    uint32_t v, addr1, addr;
1645 799e709b aliguori
    vga_draw_line_func *vga_draw_line;
1646 799e709b aliguori
1647 799e709b aliguori
    full_update |= update_basic_params(s);
1648 799e709b aliguori
1649 799e709b aliguori
    if (!full_update)
1650 799e709b aliguori
        vga_sync_dirty_bitmap(s);
1651 2bec46dc aliguori
1652 a130a41e bellard
    s->get_resolution(s, &width, &height);
1653 17b0018b bellard
    disp_width = width;
1654 09a79b49 bellard
1655 5e55efc9 Blue Swirl
    shift_control = (s->gr[VGA_GFX_MODE] >> 5) & 3;
1656 5e55efc9 Blue Swirl
    double_scan = (s->cr[VGA_CRTC_MAX_SCAN] >> 7);
1657 799e709b aliguori
    if (shift_control != 1) {
1658 5e55efc9 Blue Swirl
        multi_scan = (((s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1) << double_scan)
1659 5e55efc9 Blue Swirl
            - 1;
1660 799e709b aliguori
    } else {
1661 799e709b aliguori
        /* in CGA modes, multi_scan is ignored */
1662 799e709b aliguori
        /* XXX: is it correct ? */
1663 799e709b aliguori
        multi_scan = double_scan;
1664 799e709b aliguori
    }
1665 799e709b aliguori
    multi_run = multi_scan;
1666 17b0018b bellard
    if (shift_control != s->shift_control ||
1667 17b0018b bellard
        double_scan != s->double_scan) {
1668 799e709b aliguori
        full_update = 1;
1669 e89f66ec bellard
        s->shift_control = shift_control;
1670 17b0018b bellard
        s->double_scan = double_scan;
1671 e89f66ec bellard
    }
1672 3b46e624 ths
1673 aba35a6c malc
    if (shift_control == 0) {
1674 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
1675 aba35a6c malc
            disp_width <<= 1;
1676 aba35a6c malc
        }
1677 aba35a6c malc
    } else if (shift_control == 1) {
1678 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
1679 aba35a6c malc
            disp_width <<= 1;
1680 aba35a6c malc
        }
1681 aba35a6c malc
    }
1682 aba35a6c malc
1683 799e709b aliguori
    depth = s->get_bpp(s);
1684 e3697092 aurel32
    if (s->line_offset != s->last_line_offset ||
1685 e3697092 aurel32
        disp_width != s->last_width ||
1686 e3697092 aurel32
        height != s->last_height ||
1687 799e709b aliguori
        s->last_depth != depth) {
1688 e2542fe2 Juan Quintela
#if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
1689 e3697092 aurel32
        if (depth == 16 || depth == 32) {
1690 0da2ea1b malc
#else
1691 0da2ea1b malc
        if (depth == 32) {
1692 0da2ea1b malc
#endif
1693 b8c18e4c aliguori
            qemu_free_displaysurface(s->ds);
1694 b8c18e4c aliguori
            s->ds->surface = qemu_create_displaysurface_from(disp_width, height, depth,
1695 b8c18e4c aliguori
                    s->line_offset,
1696 b8c18e4c aliguori
                    s->vram_ptr + (s->start_addr * 4));
1697 e2542fe2 Juan Quintela
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
1698 b8c18e4c aliguori
            s->ds->surface->pf = qemu_different_endianness_pixelformat(depth);
1699 0da2ea1b malc
#endif
1700 a93a4a22 Gerd Hoffmann
            dpy_gfx_resize(s->ds);
1701 e3697092 aurel32
        } else {
1702 e3697092 aurel32
            qemu_console_resize(s->ds, disp_width, height);
1703 e3697092 aurel32
        }
1704 e3697092 aurel32
        s->last_scr_width = disp_width;
1705 e3697092 aurel32
        s->last_scr_height = height;
1706 e3697092 aurel32
        s->last_width = disp_width;
1707 e3697092 aurel32
        s->last_height = height;
1708 e3697092 aurel32
        s->last_line_offset = s->line_offset;
1709 e3697092 aurel32
        s->last_depth = depth;
1710 799e709b aliguori
        full_update = 1;
1711 799e709b aliguori
    } else if (is_buffer_shared(s->ds->surface) &&
1712 1fd2510a Gerd Hoffmann
               (full_update || ds_get_data(s->ds) != s->vram_ptr
1713 1fd2510a Gerd Hoffmann
                + (s->start_addr * 4))) {
1714 1fd2510a Gerd Hoffmann
        qemu_free_displaysurface(s->ds);
1715 1fd2510a Gerd Hoffmann
        s->ds->surface = qemu_create_displaysurface_from(disp_width,
1716 1fd2510a Gerd Hoffmann
                height, depth,
1717 1fd2510a Gerd Hoffmann
                s->line_offset,
1718 1fd2510a Gerd Hoffmann
                s->vram_ptr + (s->start_addr * 4));
1719 a93a4a22 Gerd Hoffmann
        dpy_gfx_setdata(s->ds);
1720 e3697092 aurel32
    }
1721 e3697092 aurel32
1722 e3697092 aurel32
    s->rgb_to_pixel =
1723 e3697092 aurel32
        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
1724 e3697092 aurel32
1725 799e709b aliguori
    if (shift_control == 0) {
1726 17b0018b bellard
        full_update |= update_palette16(s);
1727 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
1728 17b0018b bellard
            v = VGA_DRAW_LINE4D2;
1729 17b0018b bellard
        } else {
1730 17b0018b bellard
            v = VGA_DRAW_LINE4;
1731 17b0018b bellard
        }
1732 15342721 aurel32
        bits = 4;
1733 799e709b aliguori
    } else if (shift_control == 1) {
1734 17b0018b bellard
        full_update |= update_palette16(s);
1735 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_CLOCK_MODE] & 8) {
1736 17b0018b bellard
            v = VGA_DRAW_LINE2D2;
1737 17b0018b bellard
        } else {
1738 17b0018b bellard
            v = VGA_DRAW_LINE2;
1739 17b0018b bellard
        }
1740 15342721 aurel32
        bits = 4;
1741 17b0018b bellard
    } else {
1742 798b0c25 bellard
        switch(s->get_bpp(s)) {
1743 798b0c25 bellard
        default:
1744 798b0c25 bellard
        case 0:
1745 4fa0f5d2 bellard
            full_update |= update_palette256(s);
1746 4fa0f5d2 bellard
            v = VGA_DRAW_LINE8D2;
1747 15342721 aurel32
            bits = 4;
1748 798b0c25 bellard
            break;
1749 798b0c25 bellard
        case 8:
1750 798b0c25 bellard
            full_update |= update_palette256(s);
1751 798b0c25 bellard
            v = VGA_DRAW_LINE8;
1752 15342721 aurel32
            bits = 8;
1753 798b0c25 bellard
            break;
1754 798b0c25 bellard
        case 15:
1755 798b0c25 bellard
            v = VGA_DRAW_LINE15;
1756 15342721 aurel32
            bits = 16;
1757 798b0c25 bellard
            break;
1758 798b0c25 bellard
        case 16:
1759 798b0c25 bellard
            v = VGA_DRAW_LINE16;
1760 15342721 aurel32
            bits = 16;
1761 798b0c25 bellard
            break;
1762 798b0c25 bellard
        case 24:
1763 798b0c25 bellard
            v = VGA_DRAW_LINE24;
1764 15342721 aurel32
            bits = 24;
1765 798b0c25 bellard
            break;
1766 798b0c25 bellard
        case 32:
1767 798b0c25 bellard
            v = VGA_DRAW_LINE32;
1768 15342721 aurel32
            bits = 32;
1769 798b0c25 bellard
            break;
1770 4fa0f5d2 bellard
        }
1771 17b0018b bellard
    }
1772 d3079cd2 bellard
    vga_draw_line = vga_draw_line_table[v * NB_DEPTHS + get_depth_index(s->ds)];
1773 17b0018b bellard
1774 7d957bd8 aliguori
    if (!is_buffer_shared(s->ds->surface) && s->cursor_invalidate)
1775 a8aa669b bellard
        s->cursor_invalidate(s);
1776 3b46e624 ths
1777 e89f66ec bellard
    line_offset = s->line_offset;
1778 17b0018b bellard
#if 0
1779 f6c958c8 bellard
    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",
1780 5e55efc9 Blue Swirl
           width, height, v, line_offset, s->cr[9], s->cr[VGA_CRTC_MODE],
1781 5e55efc9 Blue Swirl
           s->line_compare, s->sr[VGA_SEQ_CLOCK_MODE]);
1782 17b0018b bellard
#endif
1783 e89f66ec bellard
    addr1 = (s->start_addr * 4);
1784 15342721 aurel32
    bwidth = (width * bits + 7) / 8;
1785 39cf7803 bellard
    y_start = -1;
1786 12c7e75a Avi Kivity
    page_min = -1;
1787 12c7e75a Avi Kivity
    page_max = 0;
1788 0e1f5a0c aliguori
    d = ds_get_data(s->ds);
1789 0e1f5a0c aliguori
    linesize = ds_get_linesize(s->ds);
1790 17b0018b bellard
    y1 = 0;
1791 e89f66ec bellard
    for(y = 0; y < height; y++) {
1792 e89f66ec bellard
        addr = addr1;
1793 5e55efc9 Blue Swirl
        if (!(s->cr[VGA_CRTC_MODE] & 1)) {
1794 17b0018b bellard
            int shift;
1795 e89f66ec bellard
            /* CGA compatibility handling */
1796 5e55efc9 Blue Swirl
            shift = 14 + ((s->cr[VGA_CRTC_MODE] >> 6) & 1);
1797 17b0018b bellard
            addr = (addr & ~(1 << shift)) | ((y1 & 1) << shift);
1798 e89f66ec bellard
        }
1799 5e55efc9 Blue Swirl
        if (!(s->cr[VGA_CRTC_MODE] & 2)) {
1800 17b0018b bellard
            addr = (addr & ~0x8000) | ((y1 & 2) << 14);
1801 e89f66ec bellard
        }
1802 734781c9 Jan Kiszka
        update = full_update;
1803 cd7a45c9 Blue Swirl
        page0 = addr;
1804 cd7a45c9 Blue Swirl
        page1 = addr + bwidth - 1;
1805 734781c9 Jan Kiszka
        update |= memory_region_get_dirty(&s->vram, page0, page1 - page0,
1806 734781c9 Jan Kiszka
                                          DIRTY_MEMORY_VGA);
1807 a8aa669b bellard
        /* explicit invalidation for the hardware cursor */
1808 a8aa669b bellard
        update |= (s->invalidated_y_table[y >> 5] >> (y & 0x1f)) & 1;
1809 e89f66ec bellard
        if (update) {
1810 39cf7803 bellard
            if (y_start < 0)
1811 39cf7803 bellard
                y_start = y;
1812 e89f66ec bellard
            if (page0 < page_min)
1813 e89f66ec bellard
                page_min = page0;
1814 e89f66ec bellard
            if (page1 > page_max)
1815 e89f66ec bellard
                page_max = page1;
1816 7d957bd8 aliguori
            if (!(is_buffer_shared(s->ds->surface))) {
1817 7d957bd8 aliguori
                vga_draw_line(s, d, s->vram_ptr + addr, width);
1818 7d957bd8 aliguori
                if (s->cursor_draw_line)
1819 7d957bd8 aliguori
                    s->cursor_draw_line(s, d, y);
1820 7d957bd8 aliguori
            }
1821 39cf7803 bellard
        } else {
1822 39cf7803 bellard
            if (y_start >= 0) {
1823 39cf7803 bellard
                /* flush to display */
1824 a93a4a22 Gerd Hoffmann
                dpy_gfx_update(s->ds, 0, y_start,
1825 a93a4a22 Gerd Hoffmann
                               disp_width, y - y_start);
1826 39cf7803 bellard
                y_start = -1;
1827 39cf7803 bellard
            }
1828 e89f66ec bellard
        }
1829 a07cf92a bellard
        if (!multi_run) {
1830 5e55efc9 Blue Swirl
            mask = (s->cr[VGA_CRTC_MODE] & 3) ^ 3;
1831 f6c958c8 bellard
            if ((y1 & mask) == mask)
1832 f6c958c8 bellard
                addr1 += line_offset;
1833 f6c958c8 bellard
            y1++;
1834 799e709b aliguori
            multi_run = multi_scan;
1835 a07cf92a bellard
        } else {
1836 a07cf92a bellard
            multi_run--;
1837 e89f66ec bellard
        }
1838 f6c958c8 bellard
        /* line compare acts on the displayed lines */
1839 f6c958c8 bellard
        if (y == s->line_compare)
1840 f6c958c8 bellard
            addr1 = 0;
1841 e89f66ec bellard
        d += linesize;
1842 e89f66ec bellard
    }
1843 39cf7803 bellard
    if (y_start >= 0) {
1844 39cf7803 bellard
        /* flush to display */
1845 a93a4a22 Gerd Hoffmann
        dpy_gfx_update(s->ds, 0, y_start,
1846 a93a4a22 Gerd Hoffmann
                       disp_width, y - y_start);
1847 39cf7803 bellard
    }
1848 e89f66ec bellard
    /* reset modified pages */
1849 12c7e75a Avi Kivity
    if (page_max >= page_min) {
1850 b1950430 Avi Kivity
        memory_region_reset_dirty(&s->vram,
1851 b1950430 Avi Kivity
                                  page_min,
1852 cd7a45c9 Blue Swirl
                                  page_max - page_min,
1853 b1950430 Avi Kivity
                                  DIRTY_MEMORY_VGA);
1854 e89f66ec bellard
    }
1855 a8aa669b bellard
    memset(s->invalidated_y_table, 0, ((height + 31) >> 5) * 4);
1856 e89f66ec bellard
}
1857 e89f66ec bellard
1858 cedd91d2 Juan Quintela
static void vga_draw_blank(VGACommonState *s, int full_update)
1859 2aebb3eb bellard
{
1860 2aebb3eb bellard
    int i, w, val;
1861 2aebb3eb bellard
    uint8_t *d;
1862 2aebb3eb bellard
1863 2aebb3eb bellard
    if (!full_update)
1864 2aebb3eb bellard
        return;
1865 2aebb3eb bellard
    if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
1866 2aebb3eb bellard
        return;
1867 2bec46dc aliguori
1868 7d957bd8 aliguori
    s->rgb_to_pixel =
1869 7d957bd8 aliguori
        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
1870 0e1f5a0c aliguori
    if (ds_get_bits_per_pixel(s->ds) == 8)
1871 2aebb3eb bellard
        val = s->rgb_to_pixel(0, 0, 0);
1872 2aebb3eb bellard
    else
1873 2aebb3eb bellard
        val = 0;
1874 0e1f5a0c aliguori
    w = s->last_scr_width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
1875 0e1f5a0c aliguori
    d = ds_get_data(s->ds);
1876 2aebb3eb bellard
    for(i = 0; i < s->last_scr_height; i++) {
1877 2aebb3eb bellard
        memset(d, val, w);
1878 0e1f5a0c aliguori
        d += ds_get_linesize(s->ds);
1879 2aebb3eb bellard
    }
1880 a93a4a22 Gerd Hoffmann
    dpy_gfx_update(s->ds, 0, 0,
1881 a93a4a22 Gerd Hoffmann
                   s->last_scr_width, s->last_scr_height);
1882 2aebb3eb bellard
}
1883 2aebb3eb bellard
1884 799e709b aliguori
#define GMODE_TEXT     0
1885 799e709b aliguori
#define GMODE_GRAPH    1
1886 799e709b aliguori
#define GMODE_BLANK 2
1887 799e709b aliguori
1888 95219897 pbrook
static void vga_update_display(void *opaque)
1889 e89f66ec bellard
{
1890 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
1891 799e709b aliguori
    int full_update, graphic_mode;
1892 e89f66ec bellard
1893 e9a07334 Jan Kiszka
    qemu_flush_coalesced_mmio_buffer();
1894 e9a07334 Jan Kiszka
1895 0e1f5a0c aliguori
    if (ds_get_bits_per_pixel(s->ds) == 0) {
1896 0f35920c bellard
        /* nothing to do */
1897 59a983b9 bellard
    } else {
1898 3098b9fd Aurelien Jarno
        full_update = 0;
1899 df800210 malc
        if (!(s->ar_index & 0x20)) {
1900 799e709b aliguori
            graphic_mode = GMODE_BLANK;
1901 799e709b aliguori
        } else {
1902 5e55efc9 Blue Swirl
            graphic_mode = s->gr[VGA_GFX_MISC] & VGA_GR06_GRAPHICS_MODE;
1903 799e709b aliguori
        }
1904 799e709b aliguori
        if (graphic_mode != s->graphic_mode) {
1905 799e709b aliguori
            s->graphic_mode = graphic_mode;
1906 9aa0ff0b Jan Kiszka
            s->cursor_blink_time = qemu_get_clock_ms(vm_clock);
1907 799e709b aliguori
            full_update = 1;
1908 799e709b aliguori
        }
1909 799e709b aliguori
        switch(graphic_mode) {
1910 2aebb3eb bellard
        case GMODE_TEXT:
1911 e89f66ec bellard
            vga_draw_text(s, full_update);
1912 2aebb3eb bellard
            break;
1913 2aebb3eb bellard
        case GMODE_GRAPH:
1914 2aebb3eb bellard
            vga_draw_graphic(s, full_update);
1915 2aebb3eb bellard
            break;
1916 2aebb3eb bellard
        case GMODE_BLANK:
1917 2aebb3eb bellard
        default:
1918 2aebb3eb bellard
            vga_draw_blank(s, full_update);
1919 2aebb3eb bellard
            break;
1920 2aebb3eb bellard
        }
1921 e89f66ec bellard
    }
1922 e89f66ec bellard
}
1923 e89f66ec bellard
1924 a130a41e bellard
/* force a full display refresh */
1925 95219897 pbrook
static void vga_invalidate_display(void *opaque)
1926 a130a41e bellard
{
1927 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
1928 3b46e624 ths
1929 3098b9fd Aurelien Jarno
    s->last_width = -1;
1930 3098b9fd Aurelien Jarno
    s->last_height = -1;
1931 a130a41e bellard
}
1932 a130a41e bellard
1933 03a3e7ba Juan Quintela
void vga_common_reset(VGACommonState *s)
1934 e89f66ec bellard
{
1935 6e6b7363 blueswir1
    s->sr_index = 0;
1936 6e6b7363 blueswir1
    memset(s->sr, '\0', sizeof(s->sr));
1937 6e6b7363 blueswir1
    s->gr_index = 0;
1938 6e6b7363 blueswir1
    memset(s->gr, '\0', sizeof(s->gr));
1939 6e6b7363 blueswir1
    s->ar_index = 0;
1940 6e6b7363 blueswir1
    memset(s->ar, '\0', sizeof(s->ar));
1941 6e6b7363 blueswir1
    s->ar_flip_flop = 0;
1942 6e6b7363 blueswir1
    s->cr_index = 0;
1943 6e6b7363 blueswir1
    memset(s->cr, '\0', sizeof(s->cr));
1944 6e6b7363 blueswir1
    s->msr = 0;
1945 6e6b7363 blueswir1
    s->fcr = 0;
1946 6e6b7363 blueswir1
    s->st00 = 0;
1947 6e6b7363 blueswir1
    s->st01 = 0;
1948 6e6b7363 blueswir1
    s->dac_state = 0;
1949 6e6b7363 blueswir1
    s->dac_sub_index = 0;
1950 6e6b7363 blueswir1
    s->dac_read_index = 0;
1951 6e6b7363 blueswir1
    s->dac_write_index = 0;
1952 6e6b7363 blueswir1
    memset(s->dac_cache, '\0', sizeof(s->dac_cache));
1953 6e6b7363 blueswir1
    s->dac_8bit = 0;
1954 6e6b7363 blueswir1
    memset(s->palette, '\0', sizeof(s->palette));
1955 6e6b7363 blueswir1
    s->bank_offset = 0;
1956 6e6b7363 blueswir1
    s->vbe_index = 0;
1957 6e6b7363 blueswir1
    memset(s->vbe_regs, '\0', sizeof(s->vbe_regs));
1958 af92284b Gerd Hoffmann
    s->vbe_regs[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
1959 6e6b7363 blueswir1
    s->vbe_start_addr = 0;
1960 6e6b7363 blueswir1
    s->vbe_line_offset = 0;
1961 6e6b7363 blueswir1
    s->vbe_bank_mask = (s->vram_size >> 16) - 1;
1962 6e6b7363 blueswir1
    memset(s->font_offsets, '\0', sizeof(s->font_offsets));
1963 799e709b aliguori
    s->graphic_mode = -1; /* force full update */
1964 6e6b7363 blueswir1
    s->shift_control = 0;
1965 6e6b7363 blueswir1
    s->double_scan = 0;
1966 6e6b7363 blueswir1
    s->line_offset = 0;
1967 6e6b7363 blueswir1
    s->line_compare = 0;
1968 6e6b7363 blueswir1
    s->start_addr = 0;
1969 6e6b7363 blueswir1
    s->plane_updated = 0;
1970 6e6b7363 blueswir1
    s->last_cw = 0;
1971 6e6b7363 blueswir1
    s->last_ch = 0;
1972 6e6b7363 blueswir1
    s->last_width = 0;
1973 6e6b7363 blueswir1
    s->last_height = 0;
1974 6e6b7363 blueswir1
    s->last_scr_width = 0;
1975 6e6b7363 blueswir1
    s->last_scr_height = 0;
1976 6e6b7363 blueswir1
    s->cursor_start = 0;
1977 6e6b7363 blueswir1
    s->cursor_end = 0;
1978 6e6b7363 blueswir1
    s->cursor_offset = 0;
1979 6e6b7363 blueswir1
    memset(s->invalidated_y_table, '\0', sizeof(s->invalidated_y_table));
1980 6e6b7363 blueswir1
    memset(s->last_palette, '\0', sizeof(s->last_palette));
1981 6e6b7363 blueswir1
    memset(s->last_ch_attr, '\0', sizeof(s->last_ch_attr));
1982 6e6b7363 blueswir1
    switch (vga_retrace_method) {
1983 6e6b7363 blueswir1
    case VGA_RETRACE_DUMB:
1984 6e6b7363 blueswir1
        break;
1985 6e6b7363 blueswir1
    case VGA_RETRACE_PRECISE:
1986 6e6b7363 blueswir1
        memset(&s->retrace_info, 0, sizeof (s->retrace_info));
1987 6e6b7363 blueswir1
        break;
1988 6e6b7363 blueswir1
    }
1989 80763888 Jan Kiszka
    vga_update_memory_access(s);
1990 e89f66ec bellard
}
1991 e89f66ec bellard
1992 03a3e7ba Juan Quintela
static void vga_reset(void *opaque)
1993 03a3e7ba Juan Quintela
{
1994 cedd91d2 Juan Quintela
    VGACommonState *s =  opaque;
1995 03a3e7ba Juan Quintela
    vga_common_reset(s);
1996 03a3e7ba Juan Quintela
}
1997 03a3e7ba Juan Quintela
1998 4d3b6f6e balrog
#define TEXTMODE_X(x)        ((x) % width)
1999 4d3b6f6e balrog
#define TEXTMODE_Y(x)        ((x) / width)
2000 4d3b6f6e balrog
#define VMEM2CHTYPE(v)        ((v & 0xff0007ff) | \
2001 4d3b6f6e balrog
        ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
2002 4d3b6f6e balrog
/* relay text rendering to the display driver
2003 4d3b6f6e balrog
 * instead of doing a full vga_update_display() */
2004 c227f099 Anthony Liguori
static void vga_update_text(void *opaque, console_ch_t *chardata)
2005 4d3b6f6e balrog
{
2006 cedd91d2 Juan Quintela
    VGACommonState *s =  opaque;
2007 799e709b aliguori
    int graphic_mode, i, cursor_offset, cursor_visible;
2008 4d3b6f6e balrog
    int cw, cheight, width, height, size, c_min, c_max;
2009 4d3b6f6e balrog
    uint32_t *src;
2010 c227f099 Anthony Liguori
    console_ch_t *dst, val;
2011 4d3b6f6e balrog
    char msg_buffer[80];
2012 799e709b aliguori
    int full_update = 0;
2013 799e709b aliguori
2014 e9a07334 Jan Kiszka
    qemu_flush_coalesced_mmio_buffer();
2015 e9a07334 Jan Kiszka
2016 799e709b aliguori
    if (!(s->ar_index & 0x20)) {
2017 799e709b aliguori
        graphic_mode = GMODE_BLANK;
2018 799e709b aliguori
    } else {
2019 5e55efc9 Blue Swirl
        graphic_mode = s->gr[VGA_GFX_MISC] & VGA_GR06_GRAPHICS_MODE;
2020 799e709b aliguori
    }
2021 799e709b aliguori
    if (graphic_mode != s->graphic_mode) {
2022 799e709b aliguori
        s->graphic_mode = graphic_mode;
2023 799e709b aliguori
        full_update = 1;
2024 799e709b aliguori
    }
2025 799e709b aliguori
    if (s->last_width == -1) {
2026 799e709b aliguori
        s->last_width = 0;
2027 799e709b aliguori
        full_update = 1;
2028 799e709b aliguori
    }
2029 4d3b6f6e balrog
2030 799e709b aliguori
    switch (graphic_mode) {
2031 4d3b6f6e balrog
    case GMODE_TEXT:
2032 4d3b6f6e balrog
        /* TODO: update palette */
2033 799e709b aliguori
        full_update |= update_basic_params(s);
2034 4d3b6f6e balrog
2035 799e709b aliguori
        /* total width & height */
2036 5e55efc9 Blue Swirl
        cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
2037 799e709b aliguori
        cw = 8;
2038 5e55efc9 Blue Swirl
        if (!(s->sr[VGA_SEQ_CLOCK_MODE] & VGA_SR01_CHAR_CLK_8DOTS)) {
2039 799e709b aliguori
            cw = 9;
2040 5e55efc9 Blue Swirl
        }
2041 5e55efc9 Blue Swirl
        if (s->sr[VGA_SEQ_CLOCK_MODE] & 0x08) {
2042 799e709b aliguori
            cw = 16; /* NOTE: no 18 pixel wide */
2043 5e55efc9 Blue Swirl
        }
2044 5e55efc9 Blue Swirl
        width = (s->cr[VGA_CRTC_H_DISP] + 1);
2045 5e55efc9 Blue Swirl
        if (s->cr[VGA_CRTC_V_TOTAL] == 100) {
2046 799e709b aliguori
            /* ugly hack for CGA 160x100x16 - explain me the logic */
2047 799e709b aliguori
            height = 100;
2048 799e709b aliguori
        } else {
2049 5e55efc9 Blue Swirl
            height = s->cr[VGA_CRTC_V_DISP_END] |
2050 5e55efc9 Blue Swirl
                ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
2051 5e55efc9 Blue Swirl
                ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
2052 799e709b aliguori
            height = (height + 1) / cheight;
2053 4d3b6f6e balrog
        }
2054 4d3b6f6e balrog
2055 4d3b6f6e balrog
        size = (height * width);
2056 4d3b6f6e balrog
        if (size > CH_ATTR_SIZE) {
2057 4d3b6f6e balrog
            if (!full_update)
2058 4d3b6f6e balrog
                return;
2059 4d3b6f6e balrog
2060 363a37d5 blueswir1
            snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Text mode",
2061 363a37d5 blueswir1
                     width, height);
2062 4d3b6f6e balrog
            break;
2063 4d3b6f6e balrog
        }
2064 4d3b6f6e balrog
2065 799e709b aliguori
        if (width != s->last_width || height != s->last_height ||
2066 799e709b aliguori
            cw != s->last_cw || cheight != s->last_ch) {
2067 799e709b aliguori
            s->last_scr_width = width * cw;
2068 799e709b aliguori
            s->last_scr_height = height * cheight;
2069 9678aedd Gerd Hoffmann
            qemu_console_resize(s->ds, s->last_scr_width, s->last_scr_height);
2070 a93a4a22 Gerd Hoffmann
            dpy_text_resize(s->ds, width, height);
2071 9678aedd Gerd Hoffmann
            s->last_depth = 0;
2072 799e709b aliguori
            s->last_width = width;
2073 799e709b aliguori
            s->last_height = height;
2074 799e709b aliguori
            s->last_ch = cheight;
2075 799e709b aliguori
            s->last_cw = cw;
2076 799e709b aliguori
            full_update = 1;
2077 799e709b aliguori
        }
2078 799e709b aliguori
2079 9678aedd Gerd Hoffmann
        if (full_update) {
2080 9678aedd Gerd Hoffmann
            s->full_update_gfx = 1;
2081 9678aedd Gerd Hoffmann
        }
2082 9678aedd Gerd Hoffmann
        if (s->full_update_text) {
2083 9678aedd Gerd Hoffmann
            s->full_update_text = 0;
2084 9678aedd Gerd Hoffmann
            full_update |= 1;
2085 9678aedd Gerd Hoffmann
        }
2086 9678aedd Gerd Hoffmann
2087 4d3b6f6e balrog
        /* Update "hardware" cursor */
2088 5e55efc9 Blue Swirl
        cursor_offset = ((s->cr[VGA_CRTC_CURSOR_HI] << 8) |
2089 5e55efc9 Blue Swirl
                         s->cr[VGA_CRTC_CURSOR_LO]) - s->start_addr;
2090 4d3b6f6e balrog
        if (cursor_offset != s->cursor_offset ||
2091 5e55efc9 Blue Swirl
            s->cr[VGA_CRTC_CURSOR_START] != s->cursor_start ||
2092 5e55efc9 Blue Swirl
            s->cr[VGA_CRTC_CURSOR_END] != s->cursor_end || full_update) {
2093 5e55efc9 Blue Swirl
            cursor_visible = !(s->cr[VGA_CRTC_CURSOR_START] & 0x20);
2094 4d3b6f6e balrog
            if (cursor_visible && cursor_offset < size && cursor_offset >= 0)
2095 bf2fde70 Gerd Hoffmann
                dpy_text_cursor(s->ds,
2096 bf2fde70 Gerd Hoffmann
                                TEXTMODE_X(cursor_offset),
2097 bf2fde70 Gerd Hoffmann
                                TEXTMODE_Y(cursor_offset));
2098 4d3b6f6e balrog
            else
2099 bf2fde70 Gerd Hoffmann
                dpy_text_cursor(s->ds, -1, -1);
2100 4d3b6f6e balrog
            s->cursor_offset = cursor_offset;
2101 5e55efc9 Blue Swirl
            s->cursor_start = s->cr[VGA_CRTC_CURSOR_START];
2102 5e55efc9 Blue Swirl
            s->cursor_end = s->cr[VGA_CRTC_CURSOR_END];
2103 4d3b6f6e balrog
        }
2104 4d3b6f6e balrog
2105 4d3b6f6e balrog
        src = (uint32_t *) s->vram_ptr + s->start_addr;
2106 4d3b6f6e balrog
        dst = chardata;
2107 4d3b6f6e balrog
2108 4d3b6f6e balrog
        if (full_update) {
2109 4d3b6f6e balrog
            for (i = 0; i < size; src ++, dst ++, i ++)
2110 9ae19b65 Aurelien Jarno
                console_write_ch(dst, VMEM2CHTYPE(le32_to_cpu(*src)));
2111 4d3b6f6e balrog
2112 a93a4a22 Gerd Hoffmann
            dpy_text_update(s->ds, 0, 0, width, height);
2113 4d3b6f6e balrog
        } else {
2114 4d3b6f6e balrog
            c_max = 0;
2115 4d3b6f6e balrog
2116 4d3b6f6e balrog
            for (i = 0; i < size; src ++, dst ++, i ++) {
2117 9ae19b65 Aurelien Jarno
                console_write_ch(&val, VMEM2CHTYPE(le32_to_cpu(*src)));
2118 4d3b6f6e balrog
                if (*dst != val) {
2119 4d3b6f6e balrog
                    *dst = val;
2120 4d3b6f6e balrog
                    c_max = i;
2121 4d3b6f6e balrog
                    break;
2122 4d3b6f6e balrog
                }
2123 4d3b6f6e balrog
            }
2124 4d3b6f6e balrog
            c_min = i;
2125 4d3b6f6e balrog
            for (; i < size; src ++, dst ++, i ++) {
2126 9ae19b65 Aurelien Jarno
                console_write_ch(&val, VMEM2CHTYPE(le32_to_cpu(*src)));
2127 4d3b6f6e balrog
                if (*dst != val) {
2128 4d3b6f6e balrog
                    *dst = val;
2129 4d3b6f6e balrog
                    c_max = i;
2130 4d3b6f6e balrog
                }
2131 4d3b6f6e balrog
            }
2132 4d3b6f6e balrog
2133 4d3b6f6e balrog
            if (c_min <= c_max) {
2134 4d3b6f6e balrog
                i = TEXTMODE_Y(c_min);
2135 a93a4a22 Gerd Hoffmann
                dpy_text_update(s->ds, 0, i, width, TEXTMODE_Y(c_max) - i + 1);
2136 4d3b6f6e balrog
            }
2137 4d3b6f6e balrog
        }
2138 4d3b6f6e balrog
2139 4d3b6f6e balrog
        return;
2140 4d3b6f6e balrog
    case GMODE_GRAPH:
2141 4d3b6f6e balrog
        if (!full_update)
2142 4d3b6f6e balrog
            return;
2143 4d3b6f6e balrog
2144 4d3b6f6e balrog
        s->get_resolution(s, &width, &height);
2145 363a37d5 blueswir1
        snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Graphic mode",
2146 363a37d5 blueswir1
                 width, height);
2147 4d3b6f6e balrog
        break;
2148 4d3b6f6e balrog
    case GMODE_BLANK:
2149 4d3b6f6e balrog
    default:
2150 4d3b6f6e balrog
        if (!full_update)
2151 4d3b6f6e balrog
            return;
2152 4d3b6f6e balrog
2153 363a37d5 blueswir1
        snprintf(msg_buffer, sizeof(msg_buffer), "VGA Blank mode");
2154 4d3b6f6e balrog
        break;
2155 4d3b6f6e balrog
    }
2156 4d3b6f6e balrog
2157 4d3b6f6e balrog
    /* Display a message */
2158 5228c2d3 balrog
    s->last_width = 60;
2159 5228c2d3 balrog
    s->last_height = height = 3;
2160 bf2fde70 Gerd Hoffmann
    dpy_text_cursor(s->ds, -1, -1);
2161 a93a4a22 Gerd Hoffmann
    dpy_text_resize(s->ds, s->last_width, height);
2162 4d3b6f6e balrog
2163 5228c2d3 balrog
    for (dst = chardata, i = 0; i < s->last_width * height; i ++)
2164 4d3b6f6e balrog
        console_write_ch(dst ++, ' ');
2165 4d3b6f6e balrog
2166 4d3b6f6e balrog
    size = strlen(msg_buffer);
2167 5228c2d3 balrog
    width = (s->last_width - size) / 2;
2168 5228c2d3 balrog
    dst = chardata + s->last_width + width;
2169 4d3b6f6e balrog
    for (i = 0; i < size; i ++)
2170 4d3b6f6e balrog
        console_write_ch(dst ++, 0x00200100 | msg_buffer[i]);
2171 4d3b6f6e balrog
2172 a93a4a22 Gerd Hoffmann
    dpy_text_update(s->ds, 0, 0, s->last_width, height);
2173 4d3b6f6e balrog
}
2174 4d3b6f6e balrog
2175 a8170e5e Avi Kivity
static uint64_t vga_mem_read(void *opaque, hwaddr addr,
2176 b1950430 Avi Kivity
                             unsigned size)
2177 b1950430 Avi Kivity
{
2178 b1950430 Avi Kivity
    VGACommonState *s = opaque;
2179 b1950430 Avi Kivity
2180 b2a5e761 Avi Kivity
    return vga_mem_readb(s, addr);
2181 b1950430 Avi Kivity
}
2182 e89f66ec bellard
2183 a8170e5e Avi Kivity
static void vga_mem_write(void *opaque, hwaddr addr,
2184 b1950430 Avi Kivity
                          uint64_t data, unsigned size)
2185 b1950430 Avi Kivity
{
2186 b1950430 Avi Kivity
    VGACommonState *s = opaque;
2187 b1950430 Avi Kivity
2188 b2a5e761 Avi Kivity
    return vga_mem_writeb(s, addr, data);
2189 b1950430 Avi Kivity
}
2190 b1950430 Avi Kivity
2191 b1950430 Avi Kivity
const MemoryRegionOps vga_mem_ops = {
2192 b1950430 Avi Kivity
    .read = vga_mem_read,
2193 b1950430 Avi Kivity
    .write = vga_mem_write,
2194 b1950430 Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
2195 b2a5e761 Avi Kivity
    .impl = {
2196 b2a5e761 Avi Kivity
        .min_access_size = 1,
2197 b2a5e761 Avi Kivity
        .max_access_size = 1,
2198 b2a5e761 Avi Kivity
    },
2199 e89f66ec bellard
};
2200 e89f66ec bellard
2201 11b6b345 Juan Quintela
static int vga_common_post_load(void *opaque, int version_id)
2202 b0a21b53 bellard
{
2203 0d65ddc3 Juan Quintela
    VGACommonState *s = opaque;
2204 11b6b345 Juan Quintela
2205 11b6b345 Juan Quintela
    /* force refresh */
2206 11b6b345 Juan Quintela
    s->graphic_mode = -1;
2207 11b6b345 Juan Quintela
    return 0;
2208 11b6b345 Juan Quintela
}
2209 11b6b345 Juan Quintela
2210 11b6b345 Juan Quintela
const VMStateDescription vmstate_vga_common = {
2211 11b6b345 Juan Quintela
    .name = "vga",
2212 11b6b345 Juan Quintela
    .version_id = 2,
2213 11b6b345 Juan Quintela
    .minimum_version_id = 2,
2214 11b6b345 Juan Quintela
    .minimum_version_id_old = 2,
2215 11b6b345 Juan Quintela
    .post_load = vga_common_post_load,
2216 11b6b345 Juan Quintela
    .fields      = (VMStateField []) {
2217 11b6b345 Juan Quintela
        VMSTATE_UINT32(latch, VGACommonState),
2218 11b6b345 Juan Quintela
        VMSTATE_UINT8(sr_index, VGACommonState),
2219 11b6b345 Juan Quintela
        VMSTATE_PARTIAL_BUFFER(sr, VGACommonState, 8),
2220 11b6b345 Juan Quintela
        VMSTATE_UINT8(gr_index, VGACommonState),
2221 11b6b345 Juan Quintela
        VMSTATE_PARTIAL_BUFFER(gr, VGACommonState, 16),
2222 11b6b345 Juan Quintela
        VMSTATE_UINT8(ar_index, VGACommonState),
2223 11b6b345 Juan Quintela
        VMSTATE_BUFFER(ar, VGACommonState),
2224 11b6b345 Juan Quintela
        VMSTATE_INT32(ar_flip_flop, VGACommonState),
2225 11b6b345 Juan Quintela
        VMSTATE_UINT8(cr_index, VGACommonState),
2226 11b6b345 Juan Quintela
        VMSTATE_BUFFER(cr, VGACommonState),
2227 11b6b345 Juan Quintela
        VMSTATE_UINT8(msr, VGACommonState),
2228 11b6b345 Juan Quintela
        VMSTATE_UINT8(fcr, VGACommonState),
2229 11b6b345 Juan Quintela
        VMSTATE_UINT8(st00, VGACommonState),
2230 11b6b345 Juan Quintela
        VMSTATE_UINT8(st01, VGACommonState),
2231 11b6b345 Juan Quintela
2232 11b6b345 Juan Quintela
        VMSTATE_UINT8(dac_state, VGACommonState),
2233 11b6b345 Juan Quintela
        VMSTATE_UINT8(dac_sub_index, VGACommonState),
2234 11b6b345 Juan Quintela
        VMSTATE_UINT8(dac_read_index, VGACommonState),
2235 11b6b345 Juan Quintela
        VMSTATE_UINT8(dac_write_index, VGACommonState),
2236 11b6b345 Juan Quintela
        VMSTATE_BUFFER(dac_cache, VGACommonState),
2237 11b6b345 Juan Quintela
        VMSTATE_BUFFER(palette, VGACommonState),
2238 11b6b345 Juan Quintela
2239 11b6b345 Juan Quintela
        VMSTATE_INT32(bank_offset, VGACommonState),
2240 11b6b345 Juan Quintela
        VMSTATE_UINT8_EQUAL(is_vbe_vmstate, VGACommonState),
2241 11b6b345 Juan Quintela
        VMSTATE_UINT16(vbe_index, VGACommonState),
2242 11b6b345 Juan Quintela
        VMSTATE_UINT16_ARRAY(vbe_regs, VGACommonState, VBE_DISPI_INDEX_NB),
2243 11b6b345 Juan Quintela
        VMSTATE_UINT32(vbe_start_addr, VGACommonState),
2244 11b6b345 Juan Quintela
        VMSTATE_UINT32(vbe_line_offset, VGACommonState),
2245 11b6b345 Juan Quintela
        VMSTATE_UINT32(vbe_bank_mask, VGACommonState),
2246 11b6b345 Juan Quintela
        VMSTATE_END_OF_LIST()
2247 11b6b345 Juan Quintela
    }
2248 11b6b345 Juan Quintela
};
2249 11b6b345 Juan Quintela
2250 4a1e244e Gerd Hoffmann
void vga_common_init(VGACommonState *s)
2251 e89f66ec bellard
{
2252 17b0018b bellard
    int i, j, v, b;
2253 e89f66ec bellard
2254 e89f66ec bellard
    for(i = 0;i < 256; i++) {
2255 e89f66ec bellard
        v = 0;
2256 e89f66ec bellard
        for(j = 0; j < 8; j++) {
2257 e89f66ec bellard
            v |= ((i >> j) & 1) << (j * 4);
2258 e89f66ec bellard
        }
2259 e89f66ec bellard
        expand4[i] = v;
2260 e89f66ec bellard
2261 e89f66ec bellard
        v = 0;
2262 e89f66ec bellard
        for(j = 0; j < 4; j++) {
2263 e89f66ec bellard
            v |= ((i >> (2 * j)) & 3) << (j * 4);
2264 e89f66ec bellard
        }
2265 e89f66ec bellard
        expand2[i] = v;
2266 e89f66ec bellard
    }
2267 17b0018b bellard
    for(i = 0; i < 16; i++) {
2268 17b0018b bellard
        v = 0;
2269 17b0018b bellard
        for(j = 0; j < 4; j++) {
2270 17b0018b bellard
            b = ((i >> j) & 1);
2271 17b0018b bellard
            v |= b << (2 * j);
2272 17b0018b bellard
            v |= b << (2 * j + 1);
2273 17b0018b bellard
        }
2274 17b0018b bellard
        expand4to8[i] = v;
2275 17b0018b bellard
    }
2276 e89f66ec bellard
2277 4a1e244e Gerd Hoffmann
    /* valid range: 1 MB -> 256 MB */
2278 4a1e244e Gerd Hoffmann
    s->vram_size = 1024 * 1024;
2279 4a1e244e Gerd Hoffmann
    while (s->vram_size < (s->vram_size_mb << 20) &&
2280 4a1e244e Gerd Hoffmann
           s->vram_size < (256 << 20)) {
2281 4a1e244e Gerd Hoffmann
        s->vram_size <<= 1;
2282 4a1e244e Gerd Hoffmann
    }
2283 4a1e244e Gerd Hoffmann
    s->vram_size_mb = s->vram_size >> 20;
2284 4a1e244e Gerd Hoffmann
2285 2a3138ab Juan Quintela
    s->is_vbe_vmstate = 1;
2286 4a1e244e Gerd Hoffmann
    memory_region_init_ram(&s->vram, "vga.vram", s->vram_size);
2287 c5705a77 Avi Kivity
    vmstate_register_ram_global(&s->vram);
2288 c65adf9b Avi Kivity
    xen_register_framebuffer(&s->vram);
2289 b1950430 Avi Kivity
    s->vram_ptr = memory_region_get_ram_ptr(&s->vram);
2290 798b0c25 bellard
    s->get_bpp = vga_get_bpp;
2291 798b0c25 bellard
    s->get_offsets = vga_get_offsets;
2292 a130a41e bellard
    s->get_resolution = vga_get_resolution;
2293 d34cab9f ths
    s->update = vga_update_display;
2294 d34cab9f ths
    s->invalidate = vga_invalidate_display;
2295 d34cab9f ths
    s->screen_dump = vga_screen_dump;
2296 4d3b6f6e balrog
    s->text_update = vga_update_text;
2297 cb5a7aa8 malc
    switch (vga_retrace_method) {
2298 cb5a7aa8 malc
    case VGA_RETRACE_DUMB:
2299 cb5a7aa8 malc
        s->retrace = vga_dumb_retrace;
2300 cb5a7aa8 malc
        s->update_retrace_info = vga_dumb_update_retrace_info;
2301 cb5a7aa8 malc
        break;
2302 cb5a7aa8 malc
2303 cb5a7aa8 malc
    case VGA_RETRACE_PRECISE:
2304 cb5a7aa8 malc
        s->retrace = vga_precise_retrace;
2305 cb5a7aa8 malc
        s->update_retrace_info = vga_precise_update_retrace_info;
2306 cb5a7aa8 malc
        break;
2307 cb5a7aa8 malc
    }
2308 b1950430 Avi Kivity
    vga_dirty_log_start(s);
2309 798b0c25 bellard
}
2310 798b0c25 bellard
2311 0a039dc7 Richard Henderson
static const MemoryRegionPortio vga_portio_list[] = {
2312 0a039dc7 Richard Henderson
    { 0x04,  2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3b4 */
2313 0a039dc7 Richard Henderson
    { 0x0a,  1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3ba */
2314 0a039dc7 Richard Henderson
    { 0x10, 16, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3c0 */
2315 0a039dc7 Richard Henderson
    { 0x24,  2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3d4 */
2316 0a039dc7 Richard Henderson
    { 0x2a,  1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3da */
2317 0a039dc7 Richard Henderson
    PORTIO_END_OF_LIST(),
2318 0a039dc7 Richard Henderson
};
2319 e89f66ec bellard
2320 0a039dc7 Richard Henderson
static const MemoryRegionPortio vbe_portio_list[] = {
2321 0a039dc7 Richard Henderson
    { 0, 1, 2, .read = vbe_ioport_read_index, .write = vbe_ioport_write_index },
2322 0a039dc7 Richard Henderson
# ifdef TARGET_I386
2323 0a039dc7 Richard Henderson
    { 1, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
2324 0a039dc7 Richard Henderson
# endif
2325 df9ffb72 Gerd Hoffmann
    { 2, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
2326 0a039dc7 Richard Henderson
    PORTIO_END_OF_LIST(),
2327 0a039dc7 Richard Henderson
};
2328 4fa0f5d2 bellard
2329 0a039dc7 Richard Henderson
/* Used by both ISA and PCI */
2330 0a039dc7 Richard Henderson
MemoryRegion *vga_init_io(VGACommonState *s,
2331 0a039dc7 Richard Henderson
                          const MemoryRegionPortio **vga_ports,
2332 0a039dc7 Richard Henderson
                          const MemoryRegionPortio **vbe_ports)
2333 0a039dc7 Richard Henderson
{
2334 0a039dc7 Richard Henderson
    MemoryRegion *vga_mem;
2335 09a79b49 bellard
2336 0a039dc7 Richard Henderson
    *vga_ports = vga_portio_list;
2337 0a039dc7 Richard Henderson
    *vbe_ports = vbe_portio_list;
2338 4fa0f5d2 bellard
2339 7267c094 Anthony Liguori
    vga_mem = g_malloc(sizeof(*vga_mem));
2340 b1950430 Avi Kivity
    memory_region_init_io(vga_mem, &vga_mem_ops, s,
2341 b1950430 Avi Kivity
                          "vga-lowmem", 0x20000);
2342 bd8f2f5d Jan Kiszka
    memory_region_set_flush_coalesced(vga_mem);
2343 b1950430 Avi Kivity
2344 b1950430 Avi Kivity
    return vga_mem;
2345 7435b791 Blue Swirl
}
2346 7435b791 Blue Swirl
2347 0a039dc7 Richard Henderson
void vga_init(VGACommonState *s, MemoryRegion *address_space,
2348 0a039dc7 Richard Henderson
              MemoryRegion *address_space_io, bool init_vga_ports)
2349 7435b791 Blue Swirl
{
2350 b1950430 Avi Kivity
    MemoryRegion *vga_io_memory;
2351 0a039dc7 Richard Henderson
    const MemoryRegionPortio *vga_ports, *vbe_ports;
2352 0a039dc7 Richard Henderson
    PortioList *vga_port_list = g_new(PortioList, 1);
2353 0a039dc7 Richard Henderson
    PortioList *vbe_port_list = g_new(PortioList, 1);
2354 7435b791 Blue Swirl
2355 7435b791 Blue Swirl
    qemu_register_reset(vga_reset, s);
2356 7435b791 Blue Swirl
2357 7435b791 Blue Swirl
    s->bank_offset = 0;
2358 7435b791 Blue Swirl
2359 80763888 Jan Kiszka
    s->legacy_address_space = address_space;
2360 80763888 Jan Kiszka
2361 0a039dc7 Richard Henderson
    vga_io_memory = vga_init_io(s, &vga_ports, &vbe_ports);
2362 be20f9e9 Avi Kivity
    memory_region_add_subregion_overlap(address_space,
2363 b1950430 Avi Kivity
                                        isa_mem_base + 0x000a0000,
2364 b1950430 Avi Kivity
                                        vga_io_memory,
2365 b1950430 Avi Kivity
                                        1);
2366 b1950430 Avi Kivity
    memory_region_set_coalescing(vga_io_memory);
2367 0a039dc7 Richard Henderson
    if (init_vga_ports) {
2368 0a039dc7 Richard Henderson
        portio_list_init(vga_port_list, vga_ports, s, "vga");
2369 0a039dc7 Richard Henderson
        portio_list_add(vga_port_list, address_space_io, 0x3b0);
2370 0a039dc7 Richard Henderson
    }
2371 0a039dc7 Richard Henderson
    if (vbe_ports) {
2372 0a039dc7 Richard Henderson
        portio_list_init(vbe_port_list, vbe_ports, s, "vbe");
2373 0a039dc7 Richard Henderson
        portio_list_add(vbe_port_list, address_space_io, 0x1ce);
2374 0a039dc7 Richard Henderson
    }
2375 d2269f6f bellard
}
2376 d2269f6f bellard
2377 be20f9e9 Avi Kivity
void vga_init_vbe(VGACommonState *s, MemoryRegion *system_memory)
2378 f0138a63 Anthony Liguori
{
2379 8294a64d Avi Kivity
    /* With pc-0.12 and below we map both the PCI BAR and the fixed VBE region,
2380 8294a64d Avi Kivity
     * so use an alias to avoid double-mapping the same region.
2381 8294a64d Avi Kivity
     */
2382 8294a64d Avi Kivity
    memory_region_init_alias(&s->vram_vbe, "vram.vbe",
2383 8294a64d Avi Kivity
                             &s->vram, 0, memory_region_size(&s->vram));
2384 f0138a63 Anthony Liguori
    /* XXX: use optimized standard vga accesses */
2385 be20f9e9 Avi Kivity
    memory_region_add_subregion(system_memory,
2386 b1950430 Avi Kivity
                                VBE_DISPI_LFB_PHYSICAL_ADDRESS,
2387 8294a64d Avi Kivity
                                &s->vram_vbe);
2388 f0138a63 Anthony Liguori
    s->vbe_mapped = 1;
2389 f0138a63 Anthony Liguori
}
2390 59a983b9 bellard
/********************************************************/
2391 59a983b9 bellard
/* vga screen dump */
2392 59a983b9 bellard
2393 d663174d Luiz Capitulino
void ppm_save(const char *filename, struct DisplaySurface *ds, Error **errp)
2394 59a983b9 bellard
{
2395 b12f32c4 Gerd Hoffmann
    int width = pixman_image_get_width(ds->image);
2396 b12f32c4 Gerd Hoffmann
    int height = pixman_image_get_height(ds->image);
2397 59a983b9 bellard
    FILE *f;
2398 b12f32c4 Gerd Hoffmann
    int y;
2399 f8e378f2 Avi Kivity
    int ret;
2400 b12f32c4 Gerd Hoffmann
    pixman_image_t *linebuf;
2401 59a983b9 bellard
2402 72750018 Alon Levy
    trace_ppm_save(filename, ds);
2403 59a983b9 bellard
    f = fopen(filename, "wb");
2404 d663174d Luiz Capitulino
    if (!f) {
2405 d663174d Luiz Capitulino
        error_setg(errp, "failed to open file '%s': %s", filename,
2406 d663174d Luiz Capitulino
                   strerror(errno));
2407 d663174d Luiz Capitulino
        return;
2408 d663174d Luiz Capitulino
    }
2409 b12f32c4 Gerd Hoffmann
    ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
2410 d663174d Luiz Capitulino
    if (ret < 0) {
2411 d663174d Luiz Capitulino
        linebuf = NULL;
2412 d663174d Luiz Capitulino
        goto write_err;
2413 d663174d Luiz Capitulino
    }
2414 b12f32c4 Gerd Hoffmann
    linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
2415 b12f32c4 Gerd Hoffmann
    for (y = 0; y < height; y++) {
2416 b12f32c4 Gerd Hoffmann
        qemu_pixman_linebuf_fill(linebuf, ds->image, width, y);
2417 d663174d Luiz Capitulino
        clearerr(f);
2418 b12f32c4 Gerd Hoffmann
        ret = fwrite(pixman_image_get_data(linebuf), 1,
2419 b12f32c4 Gerd Hoffmann
                     pixman_image_get_stride(linebuf), f);
2420 f8e378f2 Avi Kivity
        (void)ret;
2421 d663174d Luiz Capitulino
        if (ferror(f)) {
2422 d663174d Luiz Capitulino
            goto write_err;
2423 d663174d Luiz Capitulino
        }
2424 59a983b9 bellard
    }
2425 d663174d Luiz Capitulino
2426 d663174d Luiz Capitulino
out:
2427 b12f32c4 Gerd Hoffmann
    qemu_pixman_image_unref(linebuf);
2428 59a983b9 bellard
    fclose(f);
2429 d663174d Luiz Capitulino
    return;
2430 d663174d Luiz Capitulino
2431 d663174d Luiz Capitulino
write_err:
2432 d663174d Luiz Capitulino
    error_setg(errp, "failed to write to file '%s': %s", filename,
2433 d663174d Luiz Capitulino
               strerror(errno));
2434 d663174d Luiz Capitulino
    unlink(filename);
2435 d663174d Luiz Capitulino
    goto out;
2436 59a983b9 bellard
}
2437 59a983b9 bellard
2438 4c5e8c5c blueswir1
/* save the vga display in a PPM image even if no display is
2439 4c5e8c5c blueswir1
   available */
2440 d7098135 Luiz Capitulino
static void vga_screen_dump(void *opaque, const char *filename, bool cswitch,
2441 d7098135 Luiz Capitulino
                            Error **errp)
2442 4c5e8c5c blueswir1
{
2443 cedd91d2 Juan Quintela
    VGACommonState *s = opaque;
2444 4c5e8c5c blueswir1
2445 45efb161 Gerd Hoffmann
    if (cswitch) {
2446 45efb161 Gerd Hoffmann
        vga_invalidate_display(s);
2447 45efb161 Gerd Hoffmann
    }
2448 08c4ea29 Gerd Hoffmann
    vga_hw_update();
2449 d663174d Luiz Capitulino
    ppm_save(filename, s->ds->surface, errp);
2450 4c5e8c5c blueswir1
}