Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/display/vga.c
Line
Count
Source
1
/*
2
 * QEMU VGA Emulator.
3
 *
4
 * Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
#include "qemu/osdep.h"
26
#include "qemu/units.h"
27
#include "system/reset.h"
28
#include "system/ramblock.h"
29
#include "qapi/error.h"
30
#include "qemu/target-info.h"
31
#include "hw/display/vga.h"
32
#include "hw/i386/x86.h"
33
#include "hw/pci/pci.h"
34
#include "vga_int.h"
35
#include "vga_regs.h"
36
#include "ui/pixel_ops.h"
37
#include "ui/console.h"
38
#include "qemu/timer.h"
39
#include "hw/xen/xen.h"
40
#include "migration/vmstate.h"
41
#include "trace.h"
42
43
//#define DEBUG_VGA_MEM
44
//#define DEBUG_VGA_REG
45
46
bool have_vga = true;
47
48
/* frame counter bit 4: cursor blink toggles every 16 frames @60 Hz */
49
0
#define VGA_TEXT_CURSOR_PERIOD_MS       (1000 * 2 * 16 / 60)
50
/* frame counter bit 5: character blink toggles every 32 frames @60 Hz */
51
0
#define VGA_TEXT_BLINK_PERIOD_MS        (1000 * 2 * 32 / 60)
52
53
/* Address mask for non-VESA modes.  */
54
0
#define VGA_VRAM_SIZE                   (256 * KiB)
55
56
/* This value corresponds to a shift of zero pixels
57
 * in 9-dot text mode.  In other modes, bit 3 is undefined;
58
 * we just ignore it, so that 8 corresponds to zero pixels
59
 * in all modes.
60
 */
61
0
#define VGA_HPEL_NEUTRAL    8
62
63
/*
64
 * Video Graphics Array (VGA)
65
 *
66
 * Chipset docs for original IBM VGA:
67
 * http://www.mcamafia.de/pdf/ibm_vgaxga_trm2.pdf
68
 *
69
 * FreeVGA site:
70
 * http://www.osdever.net/FreeVGA/home.htm
71
 *
72
 * Standard VGA features and Bochs VBE extensions are implemented.
73
 */
74
75
/* force some bits to zero */
76
const uint8_t sr_mask[8] = {
77
    0x03,
78
    0x3d,
79
    0x0f,
80
    0x3f,
81
    0x0e,
82
    0x00,
83
    0x00,
84
    0xff,
85
};
86
87
const uint8_t gr_mask[16] = {
88
    0x0f, /* 0x00 */
89
    0x0f, /* 0x01 */
90
    0x0f, /* 0x02 */
91
    0x1f, /* 0x03 */
92
    0x03, /* 0x04 */
93
    0x7b, /* 0x05 */
94
    0x0f, /* 0x06 */
95
    0x0f, /* 0x07 */
96
    0xff, /* 0x08 */
97
    0x00, /* 0x09 */
98
    0x00, /* 0x0a */
99
    0x00, /* 0x0b */
100
    0x00, /* 0x0c */
101
    0x00, /* 0x0d */
102
    0x00, /* 0x0e */
103
    0x00, /* 0x0f */
104
};
105
106
7.02M
#define GET_PLANE(data, p) ((cpu_to_le32(data) >> ((p) * 8)) & 0xff)
107
108
static const uint32_t mask16[16] = {
109
    const_le32(0x00000000),
110
    const_le32(0x000000ff),
111
    const_le32(0x0000ff00),
112
    const_le32(0x0000ffff),
113
    const_le32(0x00ff0000),
114
    const_le32(0x00ff00ff),
115
    const_le32(0x00ffff00),
116
    const_le32(0x00ffffff),
117
    const_le32(0xff000000),
118
    const_le32(0xff0000ff),
119
    const_le32(0xff00ff00),
120
    const_le32(0xff00ffff),
121
    const_le32(0xffff0000),
122
    const_le32(0xffff00ff),
123
    const_le32(0xffffff00),
124
    const_le32(0xffffffff),
125
};
126
127
static uint32_t expand4[256];
128
static uint16_t expand2[256];
129
static uint8_t expand4to8[16];
130
131
static void vbe_update_vgaregs(VGACommonState *s);
132
133
static inline bool vbe_enabled(VGACommonState *s)
134
7.71M
{
135
7.71M
    return s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED;
136
7.71M
}
137
138
static inline uint8_t sr(VGACommonState *s, int idx)
139
7.55M
{
140
7.55M
    return vbe_enabled(s) ? s->sr_vbe[idx] : s->sr[idx];
141
7.55M
}
142
143
static void vga_update_memory_access(VGACommonState *s)
144
92.7k
{
145
92.7k
    hwaddr base, offset, size;
146
147
92.7k
    if (s->legacy_address_space == NULL) {
148
24.1k
        return;
149
24.1k
    }
150
151
68.5k
    if (s->has_chain4_alias) {
152
48.4k
        memory_region_del_subregion(s->legacy_address_space, &s->chain4_alias);
153
48.4k
        object_unparent(OBJECT(&s->chain4_alias));
154
48.4k
        s->has_chain4_alias = false;
155
48.4k
        s->plane_updated = 0xf;
156
48.4k
    }
157
68.5k
    if ((sr(s, VGA_SEQ_PLANE_WRITE) & VGA_SR02_ALL_PLANES) ==
158
68.5k
        VGA_SR02_ALL_PLANES && sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
159
48.4k
        offset = 0;
160
48.4k
        switch ((s->gr[VGA_GFX_MISC] >> 2) & 3) {
161
671
        case 0:
162
671
            base = 0xa0000;
163
671
            size = 0x20000;
164
671
            break;
165
47.2k
        case 1:
166
47.2k
            base = 0xa0000;
167
47.2k
            size = 0x10000;
168
47.2k
            offset = s->bank_offset;
169
47.2k
            break;
170
291
        case 2:
171
291
            base = 0xb0000;
172
291
            size = 0x8000;
173
291
            break;
174
195
        case 3:
175
195
        default:
176
195
            base = 0xb8000;
177
195
            size = 0x8000;
178
195
            break;
179
48.4k
        }
180
48.4k
        assert(offset + size <= s->vram_size);
181
48.4k
        memory_region_init_alias(&s->chain4_alias, memory_region_owner(&s->vram),
182
48.4k
                                 "vga.chain4", &s->vram, offset, size);
183
48.4k
        memory_region_add_subregion_overlap(s->legacy_address_space, base,
184
48.4k
                                            &s->chain4_alias, 2);
185
48.4k
        s->has_chain4_alias = true;
186
48.4k
    }
187
68.5k
}
188
189
static void vga_dumb_update_retrace_info(VGACommonState *s)
190
18.9k
{
191
18.9k
    (void) s;
192
18.9k
}
193
194
static void vga_precise_update_retrace_info(VGACommonState *s)
195
0
{
196
0
    int htotal_chars;
197
0
    int hretr_start_char;
198
0
    int hretr_skew_chars;
199
0
    int hretr_end_char;
200
201
0
    int vtotal_lines;
202
0
    int vretr_start_line;
203
0
    int vretr_end_line;
204
205
0
    int dots;
206
#if 0
207
    int div2, sldiv2;
208
#endif
209
0
    int clocking_mode;
210
0
    int clock_sel;
211
0
    const int clk_hz[] = {25175000, 28322000, 25175000, 25175000};
212
0
    int64_t chars_per_sec;
213
0
    struct vga_precise_retrace *r = &s->retrace_info.precise;
214
215
0
    htotal_chars = s->cr[VGA_CRTC_H_TOTAL] + 5;
216
0
    hretr_start_char = s->cr[VGA_CRTC_H_SYNC_START];
217
0
    hretr_skew_chars = (s->cr[VGA_CRTC_H_SYNC_END] >> 5) & 3;
218
0
    hretr_end_char = s->cr[VGA_CRTC_H_SYNC_END] & 0x1f;
219
220
0
    vtotal_lines = (s->cr[VGA_CRTC_V_TOTAL] |
221
0
                    (((s->cr[VGA_CRTC_OVERFLOW] & 1) |
222
0
                      ((s->cr[VGA_CRTC_OVERFLOW] >> 4) & 2)) << 8)) + 2;
223
0
    vretr_start_line = s->cr[VGA_CRTC_V_SYNC_START] |
224
0
        ((((s->cr[VGA_CRTC_OVERFLOW] >> 2) & 1) |
225
0
          ((s->cr[VGA_CRTC_OVERFLOW] >> 6) & 2)) << 8);
226
0
    vretr_end_line = s->cr[VGA_CRTC_V_SYNC_END] & 0xf;
227
228
0
    clocking_mode = (sr(s, VGA_SEQ_CLOCK_MODE) >> 3) & 1;
229
0
    clock_sel = (s->msr >> 2) & 3;
230
0
    dots = (s->msr & 1) ? 8 : 9;
231
232
0
    chars_per_sec = clk_hz[clock_sel] / dots;
233
234
0
    htotal_chars <<= clocking_mode;
235
236
0
    r->total_chars = vtotal_lines * htotal_chars;
237
0
    if (r->freq) {
238
0
        r->ticks_per_char = NANOSECONDS_PER_SECOND / (r->total_chars * r->freq);
239
0
    } else {
240
0
        r->ticks_per_char = NANOSECONDS_PER_SECOND / chars_per_sec;
241
0
    }
242
243
0
    r->vstart = vretr_start_line;
244
0
    r->vend = r->vstart + vretr_end_line + 1;
245
246
0
    r->hstart = hretr_start_char + hretr_skew_chars;
247
0
    r->hend = r->hstart + hretr_end_char + 1;
248
0
    r->htotal = htotal_chars;
249
250
#if 0
251
    div2 = (s->cr[VGA_CRTC_MODE] >> 2) & 1;
252
    sldiv2 = (s->cr[VGA_CRTC_MODE] >> 3) & 1;
253
    printf (
254
        "hz=%f\n"
255
        "htotal = %d\n"
256
        "hretr_start = %d\n"
257
        "hretr_skew = %d\n"
258
        "hretr_end = %d\n"
259
        "vtotal = %d\n"
260
        "vretr_start = %d\n"
261
        "vretr_end = %d\n"
262
        "div2 = %d sldiv2 = %d\n"
263
        "clocking_mode = %d\n"
264
        "clock_sel = %d %d\n"
265
        "dots = %d\n"
266
        "ticks/char = %" PRId64 "\n"
267
        "\n",
268
        (double) NANOSECONDS_PER_SECOND / (r->ticks_per_char * r->total_chars),
269
        htotal_chars,
270
        hretr_start_char,
271
        hretr_skew_chars,
272
        hretr_end_char,
273
        vtotal_lines,
274
        vretr_start_line,
275
        vretr_end_line,
276
        div2, sldiv2,
277
        clocking_mode,
278
        clock_sel,
279
        clk_hz[clock_sel],
280
        dots,
281
        r->ticks_per_char
282
        );
283
#endif
284
0
}
285
286
static uint8_t vga_precise_retrace(VGACommonState *s)
287
0
{
288
0
    struct vga_precise_retrace *r = &s->retrace_info.precise;
289
0
    uint8_t val = s->st01 & ~(ST01_V_RETRACE | ST01_DISP_ENABLE);
290
291
0
    if (r->total_chars) {
292
0
        int cur_line, cur_line_char, cur_char;
293
0
        int64_t cur_tick;
294
295
0
        cur_tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
296
297
0
        cur_char = (cur_tick / r->ticks_per_char) % r->total_chars;
298
0
        cur_line = cur_char / r->htotal;
299
300
0
        if (cur_line >= r->vstart && cur_line <= r->vend) {
301
0
            val |= ST01_V_RETRACE | ST01_DISP_ENABLE;
302
0
        } else {
303
0
            cur_line_char = cur_char % r->htotal;
304
0
            if (cur_line_char >= r->hstart && cur_line_char <= r->hend) {
305
0
                val |= ST01_DISP_ENABLE;
306
0
            }
307
0
        }
308
309
0
        return val;
310
0
    } else {
311
0
        return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
312
0
    }
313
0
}
314
315
static uint8_t vga_dumb_retrace(VGACommonState *s)
316
2.14k
{
317
2.14k
    return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
318
2.14k
}
319
320
int vga_ioport_invalid(VGACommonState *s, uint32_t addr)
321
851k
{
322
851k
    if (s->msr & VGA_MIS_COLOR) {
323
        /* Color */
324
129k
        return (addr >= 0x3b0 && addr <= 0x3bf);
325
722k
    } else {
326
        /* Monochrome */
327
722k
        return (addr >= 0x3d0 && addr <= 0x3df);
328
722k
    }
329
851k
}
330
331
uint32_t vga_ioport_read(void *opaque, uint32_t addr)
332
19.7k
{
333
19.7k
    VGACommonState *s = opaque;
334
19.7k
    int val, index;
335
336
19.7k
    if (vga_ioport_invalid(s, addr)) {
337
2.85k
        val = 0xff;
338
16.9k
    } else {
339
16.9k
        switch(addr) {
340
794
        case VGA_ATT_W:
341
794
            if (s->ar_flip_flop == 0) {
342
527
                val = s->ar_index;
343
527
            } else {
344
267
                val = 0;
345
267
            }
346
794
            break;
347
1.99k
        case VGA_ATT_R:
348
1.99k
            index = s->ar_index & 0x1f;
349
1.99k
            if (index < VGA_ATT_C) {
350
1.53k
                val = s->ar[index];
351
1.53k
            } else {
352
458
                val = 0;
353
458
            }
354
1.99k
            break;
355
1.13k
        case VGA_MIS_W:
356
1.13k
            val = s->st00;
357
1.13k
            break;
358
491
        case VGA_SEQ_I:
359
491
            val = s->sr_index;
360
491
            break;
361
374
        case VGA_SEQ_D:
362
374
            val = s->sr[s->sr_index];
363
#ifdef DEBUG_VGA_REG
364
            printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
365
#endif
366
374
            break;
367
670
        case VGA_PEL_IR:
368
670
            val = s->dac_state;
369
670
            break;
370
522
        case VGA_PEL_IW:
371
522
            val = s->dac_write_index;
372
522
            break;
373
2.59k
        case VGA_PEL_D:
374
2.59k
            val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
375
2.59k
            if (++s->dac_sub_index == 3) {
376
818
                s->dac_sub_index = 0;
377
818
                s->dac_read_index++;
378
818
            }
379
2.59k
            break;
380
2.40k
        case VGA_FTC_R:
381
2.40k
            val = s->fcr;
382
2.40k
            break;
383
377
        case VGA_MIS_R:
384
377
            val = s->msr;
385
377
            break;
386
447
        case VGA_GFX_I:
387
447
            val = s->gr_index;
388
447
            break;
389
489
        case VGA_GFX_D:
390
489
            val = s->gr[s->gr_index];
391
#ifdef DEBUG_VGA_REG
392
            printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
393
#endif
394
489
            break;
395
478
        case VGA_CRT_IM:
396
730
        case VGA_CRT_IC:
397
730
            val = s->cr_index;
398
730
            break;
399
582
        case VGA_CRT_DM:
400
829
        case VGA_CRT_DC:
401
829
            val = s->cr[s->cr_index];
402
#ifdef DEBUG_VGA_REG
403
            printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
404
#endif
405
829
            break;
406
769
        case VGA_IS1_RM:
407
1.11k
        case VGA_IS1_RC:
408
            /* just toggle to fool polling */
409
1.11k
            val = s->st01 = s->retrace(s);
410
1.11k
            s->ar_flip_flop = 0;
411
1.11k
            break;
412
1.95k
        default:
413
1.95k
            val = 0x00;
414
1.95k
            break;
415
16.9k
        }
416
16.9k
    }
417
19.7k
    trace_vga_std_read_io(addr, val);
418
19.7k
    return val;
419
19.7k
}
420
421
void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
422
51.1k
{
423
51.1k
    VGACommonState *s = opaque;
424
51.1k
    int index;
425
426
    /* check port range access depending on color/monochrome mode */
427
51.1k
    if (vga_ioport_invalid(s, addr)) {
428
3.63k
        return;
429
3.63k
    }
430
47.5k
    trace_vga_std_write_io(addr, val);
431
432
47.5k
    switch(addr) {
433
16.2k
    case VGA_ATT_W:
434
16.2k
        if (s->ar_flip_flop == 0) {
435
8.20k
            val &= 0x3f;
436
8.20k
            s->ar_index = val;
437
8.20k
        } else {
438
8.06k
            index = s->ar_index & 0x1f;
439
8.06k
            switch(index) {
440
6.55k
            case VGA_ATC_PALETTE0 ... VGA_ATC_PALETTEF:
441
6.55k
                s->ar[index] = val & 0x3f;
442
6.55k
                break;
443
198
            case VGA_ATC_MODE:
444
198
                s->ar[index] = val & ~0x10;
445
198
                break;
446
286
            case VGA_ATC_OVERSCAN:
447
286
                s->ar[index] = val;
448
286
                break;
449
221
            case VGA_ATC_PLANE_ENABLE:
450
221
                s->ar[index] = val & ~0xc0;
451
221
                break;
452
206
            case VGA_ATC_PEL:
453
206
                s->ar[index] = val & ~0xf0;
454
206
                break;
455
222
            case VGA_ATC_COLOR_PAGE:
456
222
                s->ar[index] = val & ~0xf0;
457
222
                break;
458
374
            default:
459
374
                break;
460
8.06k
            }
461
8.06k
        }
462
16.2k
        s->ar_flip_flop ^= 1;
463
16.2k
        break;
464
794
    case VGA_MIS_W:
465
794
        s->msr = val & ~0x10;
466
794
        s->update_retrace_info(s);
467
794
        break;
468
2.69k
    case VGA_SEQ_I:
469
2.69k
        s->sr_index = val & 7;
470
2.69k
        break;
471
2.61k
    case VGA_SEQ_D:
472
#ifdef DEBUG_VGA_REG
473
        printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
474
#endif
475
2.61k
        s->sr[s->sr_index] = val & sr_mask[s->sr_index];
476
2.61k
        if (s->sr_index == VGA_SEQ_CLOCK_MODE) {
477
768
            s->update_retrace_info(s);
478
768
        }
479
2.61k
        vga_update_memory_access(s);
480
2.61k
        break;
481
760
    case VGA_PEL_IR:
482
760
        s->dac_read_index = val;
483
760
        s->dac_sub_index = 0;
484
760
        s->dac_state = 3;
485
760
        break;
486
715
    case VGA_PEL_IW:
487
715
        s->dac_write_index = val;
488
715
        s->dac_sub_index = 0;
489
715
        s->dac_state = 0;
490
715
        break;
491
963
    case VGA_PEL_D:
492
963
        s->dac_cache[s->dac_sub_index] = val;
493
963
        if (++s->dac_sub_index == 3) {
494
195
            memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
495
195
            s->dac_sub_index = 0;
496
195
            s->dac_write_index++;
497
195
        }
498
963
        break;
499
907
    case VGA_GFX_I:
500
907
        s->gr_index = val & 0x0f;
501
907
        break;
502
922
    case VGA_GFX_D:
503
#ifdef DEBUG_VGA_REG
504
        printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
505
#endif
506
922
        s->gr[s->gr_index] = val & gr_mask[s->gr_index];
507
922
        vbe_update_vgaregs(s);
508
922
        vga_update_memory_access(s);
509
922
        break;
510
2.50k
    case VGA_CRT_IM:
511
2.91k
    case VGA_CRT_IC:
512
2.91k
        s->cr_index = val;
513
2.91k
        break;
514
2.71k
    case VGA_CRT_DM:
515
3.17k
    case VGA_CRT_DC:
516
#ifdef DEBUG_VGA_REG
517
        printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
518
#endif
519
        /* handle CR0-7 protection */
520
3.17k
        if ((s->cr[VGA_CRTC_V_SYNC_END] & VGA_CR11_LOCK_CR0_CR7) &&
521
794
            s->cr_index <= VGA_CRTC_OVERFLOW) {
522
            /* can always write bit 4 of CR7 */
523
401
            if (s->cr_index == VGA_CRTC_OVERFLOW) {
524
197
                s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x10) |
525
197
                    (val & 0x10);
526
197
                vbe_update_vgaregs(s);
527
197
            }
528
401
            return;
529
401
        }
530
2.77k
        s->cr[s->cr_index] = val;
531
2.77k
        vbe_update_vgaregs(s);
532
533
2.77k
        switch(s->cr_index) {
534
413
        case VGA_CRTC_H_TOTAL:
535
609
        case VGA_CRTC_H_SYNC_START:
536
805
        case VGA_CRTC_H_SYNC_END:
537
1.29k
        case VGA_CRTC_V_TOTAL:
538
1.60k
        case VGA_CRTC_OVERFLOW:
539
2.05k
        case VGA_CRTC_V_SYNC_END:
540
2.28k
        case VGA_CRTC_MODE:
541
2.28k
            s->update_retrace_info(s);
542
2.28k
            break;
543
2.77k
        }
544
2.77k
        break;
545
2.77k
    case VGA_IS1_RM:
546
569
    case VGA_IS1_RC:
547
569
        s->fcr = val & 0x10;
548
569
        break;
549
47.5k
    }
550
47.5k
}
551
552
/*
553
 * Sanity check vbe register writes.
554
 *
555
 * As we don't have a way to signal errors to the guest in the bochs
556
 * dispi interface we'll go adjust the registers to the closest valid
557
 * value.
558
 */
559
static void vbe_fixup_regs(VGACommonState *s)
560
78.6k
{
561
78.6k
    uint16_t *r = s->vbe_regs;
562
78.6k
    uint32_t bits, linelength, maxy, offset;
563
564
78.6k
    if (!vbe_enabled(s)) {
565
        /* vbe is turned off -- nothing to do */
566
12.2k
        return;
567
12.2k
    }
568
569
    /* check depth */
570
66.3k
    switch (r[VBE_DISPI_INDEX_BPP]) {
571
337
    case 4:
572
40.1k
    case 8:
573
40.7k
    case 16:
574
43.4k
    case 24:
575
43.6k
    case 32:
576
43.6k
        bits = r[VBE_DISPI_INDEX_BPP];
577
43.6k
        break;
578
237
    case 15:
579
237
        bits = 16;
580
237
        break;
581
22.5k
    default:
582
22.5k
        bits = r[VBE_DISPI_INDEX_BPP] = 8;
583
22.5k
        break;
584
66.3k
    }
585
586
    /* check width */
587
66.3k
    r[VBE_DISPI_INDEX_XRES] &= ~7u;
588
66.3k
    if (r[VBE_DISPI_INDEX_XRES] == 0) {
589
766
        r[VBE_DISPI_INDEX_XRES] = 8;
590
766
    }
591
66.3k
    if (r[VBE_DISPI_INDEX_XRES] > VBE_DISPI_MAX_XRES) {
592
1.20k
        r[VBE_DISPI_INDEX_XRES] = VBE_DISPI_MAX_XRES;
593
1.20k
    }
594
66.3k
    r[VBE_DISPI_INDEX_VIRT_WIDTH] &= ~7u;
595
66.3k
    if (r[VBE_DISPI_INDEX_VIRT_WIDTH] > VBE_DISPI_MAX_XRES) {
596
5.79k
        r[VBE_DISPI_INDEX_VIRT_WIDTH] = VBE_DISPI_MAX_XRES;
597
5.79k
    }
598
66.3k
    if (r[VBE_DISPI_INDEX_VIRT_WIDTH] < r[VBE_DISPI_INDEX_XRES]) {
599
9.76k
        r[VBE_DISPI_INDEX_VIRT_WIDTH] = r[VBE_DISPI_INDEX_XRES];
600
9.76k
    }
601
602
    /* check height */
603
66.3k
    linelength = r[VBE_DISPI_INDEX_VIRT_WIDTH] * bits / 8;
604
66.3k
    maxy = s->vbe_size / linelength;
605
66.3k
    if (r[VBE_DISPI_INDEX_YRES] == 0) {
606
1.93k
        r[VBE_DISPI_INDEX_YRES] = 1;
607
1.93k
    }
608
66.3k
    if (r[VBE_DISPI_INDEX_YRES] > VBE_DISPI_MAX_YRES) {
609
3.60k
        r[VBE_DISPI_INDEX_YRES] = VBE_DISPI_MAX_YRES;
610
3.60k
    }
611
66.3k
    if (r[VBE_DISPI_INDEX_YRES] > maxy) {
612
2.87k
        r[VBE_DISPI_INDEX_YRES] = maxy;
613
2.87k
    }
614
615
    /* check offset */
616
66.3k
    if (r[VBE_DISPI_INDEX_X_OFFSET] > VBE_DISPI_MAX_XRES) {
617
1.67k
        r[VBE_DISPI_INDEX_X_OFFSET] = VBE_DISPI_MAX_XRES;
618
1.67k
    }
619
66.3k
    if (r[VBE_DISPI_INDEX_Y_OFFSET] > VBE_DISPI_MAX_YRES) {
620
871
        r[VBE_DISPI_INDEX_Y_OFFSET] = VBE_DISPI_MAX_YRES;
621
871
    }
622
66.3k
    offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
623
66.3k
    offset += r[VBE_DISPI_INDEX_Y_OFFSET] * linelength;
624
66.3k
    if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
625
1.80k
        r[VBE_DISPI_INDEX_Y_OFFSET] = 0;
626
1.80k
        offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
627
1.80k
        if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
628
805
            r[VBE_DISPI_INDEX_X_OFFSET] = 0;
629
805
            offset = 0;
630
805
        }
631
1.80k
    }
632
633
    /* update vga state */
634
66.3k
    r[VBE_DISPI_INDEX_VIRT_HEIGHT] = maxy;
635
66.3k
    s->vbe_line_offset = linelength;
636
66.3k
    s->vbe_start_addr  = offset / 4;
637
66.3k
}
638
639
/* we initialize the VGA graphic mode */
640
static void vbe_update_vgaregs(VGACommonState *s)
641
82.5k
{
642
82.5k
    int h, shift_control;
643
644
82.5k
    if (!vbe_enabled(s)) {
645
        /* vbe is turned off -- nothing to do */
646
15.4k
        return;
647
15.4k
    }
648
649
    /* graphic mode + memory map 1 */
650
67.0k
    s->gr[VGA_GFX_MISC] = (s->gr[VGA_GFX_MISC] & ~0x0c) | 0x04 |
651
67.0k
        VGA_GR06_GRAPHICS_MODE;
652
67.0k
    s->cr[VGA_CRTC_MODE] |= 3; /* no CGA modes */
653
67.0k
    s->cr[VGA_CRTC_OFFSET] = s->vbe_line_offset >> 3;
654
    /* width */
655
67.0k
    s->cr[VGA_CRTC_H_DISP] =
656
67.0k
        (s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
657
    /* height (only meaningful if < 1024) */
658
67.0k
    h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
659
67.0k
    s->cr[VGA_CRTC_V_DISP_END] = h;
660
67.0k
    s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x42) |
661
67.0k
        ((h >> 7) & 0x02) | ((h >> 3) & 0x40);
662
    /* line compare to 1023 */
663
67.0k
    s->cr[VGA_CRTC_LINE_COMPARE] = 0xff;
664
67.0k
    s->cr[VGA_CRTC_OVERFLOW] |= 0x10;
665
67.0k
    s->cr[VGA_CRTC_MAX_SCAN] |= 0x40;
666
667
67.0k
    if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
668
353
        shift_control = 0;
669
353
        s->sr_vbe[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
670
66.6k
    } else {
671
66.6k
        shift_control = 2;
672
        /* set chain 4 mode */
673
66.6k
        s->sr_vbe[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
674
        /* activate all planes */
675
66.6k
        s->sr_vbe[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
676
66.6k
    }
677
67.0k
    s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
678
67.0k
        (shift_control << 5);
679
67.0k
    s->cr[VGA_CRTC_MAX_SCAN] &= ~0x9f; /* no double scan */
680
67.0k
}
681
682
static uint32_t vbe_ioport_read_index(void *opaque, uint32_t addr)
683
569
{
684
569
    VGACommonState *s = opaque;
685
569
    return s->vbe_index;
686
569
}
687
688
uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
689
14.2k
{
690
14.2k
    VGACommonState *s = opaque;
691
14.2k
    uint32_t val;
692
693
14.2k
    if (s->vbe_index < VBE_DISPI_INDEX_NB) {
694
13.6k
        if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_GETCAPS) {
695
7.73k
            switch(s->vbe_index) {
696
                /* XXX: do not hardcode ? */
697
334
            case VBE_DISPI_INDEX_XRES:
698
334
                val = VBE_DISPI_MAX_XRES;
699
334
                break;
700
1.49k
            case VBE_DISPI_INDEX_YRES:
701
1.49k
                val = VBE_DISPI_MAX_YRES;
702
1.49k
                break;
703
1.50k
            case VBE_DISPI_INDEX_BPP:
704
1.50k
                val = VBE_DISPI_MAX_BPP;
705
1.50k
                break;
706
4.39k
            default:
707
4.39k
                val = s->vbe_regs[s->vbe_index];
708
4.39k
                break;
709
7.73k
            }
710
7.73k
        } else {
711
5.92k
            val = s->vbe_regs[s->vbe_index];
712
5.92k
        }
713
13.6k
    } else if (s->vbe_index == VBE_DISPI_INDEX_VIDEO_MEMORY_64K) {
714
274
        val = s->vbe_size / (64 * KiB);
715
282
    } else {
716
282
        val = 0;
717
282
    }
718
14.2k
    trace_vga_vbe_read(s->vbe_index, val);
719
14.2k
    return val;
720
14.2k
}
721
722
void vbe_ioport_write_index(void *opaque, uint32_t addr, uint32_t val)
723
151k
{
724
151k
    VGACommonState *s = opaque;
725
151k
    s->vbe_index = val;
726
151k
}
727
728
void vbe_ioport_write_data(void *opaque, uint32_t addr, uint32_t val)
729
145k
{
730
145k
    VGACommonState *s = opaque;
731
732
145k
    if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
733
145k
        trace_vga_vbe_write(s->vbe_index, val);
734
145k
        switch(s->vbe_index) {
735
3.06k
        case VBE_DISPI_INDEX_ID:
736
3.06k
            if (val == VBE_DISPI_ID0 ||
737
2.86k
                val == VBE_DISPI_ID1 ||
738
2.67k
                val == VBE_DISPI_ID2 ||
739
2.47k
                val == VBE_DISPI_ID3 ||
740
2.12k
                val == VBE_DISPI_ID4 ||
741
1.91k
                val == VBE_DISPI_ID5) {
742
1.34k
                s->vbe_regs[s->vbe_index] = val;
743
1.34k
            }
744
3.06k
            break;
745
6.76k
        case VBE_DISPI_INDEX_XRES:
746
23.2k
        case VBE_DISPI_INDEX_YRES:
747
47.9k
        case VBE_DISPI_INDEX_BPP:
748
69.7k
        case VBE_DISPI_INDEX_VIRT_WIDTH:
749
73.8k
        case VBE_DISPI_INDEX_X_OFFSET:
750
76.1k
        case VBE_DISPI_INDEX_Y_OFFSET:
751
76.1k
            s->vbe_regs[s->vbe_index] = val;
752
76.1k
            vbe_fixup_regs(s);
753
76.1k
            vbe_update_vgaregs(s);
754
76.1k
            break;
755
25.3k
        case VBE_DISPI_INDEX_BANK:
756
25.3k
            val &= s->vbe_bank_mask;
757
25.3k
            s->vbe_regs[s->vbe_index] = val;
758
25.3k
            s->bank_offset = (val << 16);
759
25.3k
            vga_update_memory_access(s);
760
25.3k
            break;
761
26.6k
        case VBE_DISPI_INDEX_ENABLE:
762
26.6k
            if ((val & VBE_DISPI_ENABLED) &&
763
23.6k
                !(s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) {
764
765
2.41k
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] = 0;
766
2.41k
                s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET] = 0;
767
2.41k
                s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET] = 0;
768
2.41k
                s->vbe_regs[VBE_DISPI_INDEX_ENABLE] |= VBE_DISPI_ENABLED;
769
2.41k
                vbe_fixup_regs(s);
770
2.41k
                vbe_update_vgaregs(s);
771
772
                /* clear the screen */
773
2.41k
                if (!(val & VBE_DISPI_NOCLEARMEM)) {
774
1.21k
                    memset(s->vram_ptr, 0,
775
1.21k
                           s->vbe_regs[VBE_DISPI_INDEX_YRES] * s->vbe_line_offset);
776
1.21k
                }
777
24.2k
            } else {
778
24.2k
                s->bank_offset = 0;
779
24.2k
            }
780
26.6k
            s->dac_8bit = (val & VBE_DISPI_8BIT_DAC) > 0;
781
26.6k
            s->vbe_regs[s->vbe_index] = val;
782
26.6k
            vga_update_memory_access(s);
783
26.6k
            break;
784
13.9k
        default:
785
13.9k
            break;
786
145k
        }
787
145k
    }
788
145k
}
789
790
/* called for accesses between 0xa0000 and 0xc0000 */
791
uint32_t vga_mem_readb(VGACommonState *s, hwaddr addr)
792
7.03M
{
793
7.03M
    int memory_map_mode, plane;
794
7.03M
    uint32_t ret;
795
796
    /* convert to VGA memory offset */
797
7.03M
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
798
7.03M
    addr &= 0x1ffff;
799
7.03M
    switch(memory_map_mode) {
800
7.02M
    case 0:
801
7.02M
        break;
802
5.34k
    case 1:
803
5.34k
        if (addr >= 0x10000)
804
1.85k
            return 0xff;
805
3.48k
        addr += s->bank_offset;
806
3.48k
        break;
807
997
    case 2:
808
997
        addr -= 0x10000;
809
997
        if (addr >= 0x8000)
810
541
            return 0xff;
811
456
        break;
812
456
    default:
813
1.48k
    case 3:
814
1.48k
        addr -= 0x18000;
815
1.48k
        if (addr >= 0x8000)
816
726
            return 0xff;
817
759
        break;
818
7.03M
    }
819
820
7.03M
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
821
        /* chain4 mode */
822
706
        plane = addr & 3;
823
706
        addr &= ~3;
824
7.02M
    } else if (s->gr[VGA_GFX_MODE] & VGA_GR05_HOST_ODD_EVEN) {
825
        /* odd/even mode (aka text mode mapping) */
826
1.45k
        plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
827
7.02M
    } else {
828
        /* standard VGA latched access */
829
7.02M
        plane = s->gr[VGA_GFX_PLANE_READ];
830
7.02M
    }
831
832
7.03M
    if (s->gr[VGA_GFX_MISC] & VGA_GR06_CHAIN_ODD_EVEN) {
833
1.50k
        addr &= ~1;
834
1.50k
    }
835
836
    /* Doubleword/word mode.  See comment in vga_mem_writeb */
837
7.03M
    if (s->cr[VGA_CRTC_UNDERLINE] & VGA_CR14_DW) {
838
388
        addr >>= 2;
839
7.02M
    } else if ((s->gr[VGA_GFX_MODE] & VGA_GR05_HOST_ODD_EVEN) &&
840
1.45k
               (s->cr[VGA_CRTC_MODE] & VGA_CR17_WORD_BYTE) == 0) {
841
1.05k
        addr >>= 1;
842
1.05k
    }
843
844
7.03M
    if (addr * sizeof(uint32_t) >= s->vram_size) {
845
2.90k
        return 0xff;
846
2.90k
    }
847
848
7.02M
    if (s->sr[VGA_SEQ_MEMORY_MODE] & VGA_SR04_CHN_4M) {
849
        /* chain 4 mode: simplified access (but it should use the same
850
         * algorithms as below, see e.g. vga_mem_writeb's plane mask check).
851
         */
852
706
        return s->vram_ptr[(addr << 2) | plane];
853
706
    }
854
855
7.02M
    s->latch = ((uint32_t *)s->vram_ptr)[addr];
856
7.02M
    if (!(s->gr[VGA_GFX_MODE] & 0x08)) {
857
        /* read mode 0 */
858
7.02M
        ret = GET_PLANE(s->latch, plane);
859
7.02M
    } else {
860
        /* read mode 1 */
861
1.69k
        ret = (s->latch ^ mask16[s->gr[VGA_GFX_COMPARE_VALUE]]) &
862
1.69k
            mask16[s->gr[VGA_GFX_COMPARE_MASK]];
863
1.69k
        ret |= ret >> 16;
864
1.69k
        ret |= ret >> 8;
865
1.69k
        ret = (~ret) & 0xff;
866
1.69k
    }
867
868
7.02M
    return ret;
869
7.02M
}
870
871
/* called for accesses between 0xa0000 and 0xc0000 */
872
void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
873
188k
{
874
188k
    int memory_map_mode, write_mode, b, func_select, mask;
875
188k
    uint32_t write_mask, bit_mask, set_mask;
876
188k
    int plane = 0;
877
878
#ifdef DEBUG_VGA_MEM
879
    printf("vga: [0x" HWADDR_FMT_plx "] = 0x%02x\n", addr, val);
880
#endif
881
    /* convert to VGA memory offset */
882
188k
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
883
188k
    addr &= 0x1ffff;
884
188k
    switch(memory_map_mode) {
885
71.1k
    case 0:
886
71.1k
        break;
887
111k
    case 1:
888
111k
        if (addr >= 0x10000)
889
101k
            return;
890
9.42k
        addr += s->bank_offset;
891
9.42k
        break;
892
3.51k
    case 2:
893
3.51k
        addr -= 0x10000;
894
3.51k
        if (addr >= 0x8000)
895
2.45k
            return;
896
1.06k
        break;
897
1.06k
    default:
898
2.27k
    case 3:
899
2.27k
        addr -= 0x18000;
900
2.27k
        if (addr >= 0x8000)
901
1.54k
            return;
902
731
        break;
903
188k
    }
904
905
82.3k
    mask = sr(s, VGA_SEQ_PLANE_WRITE);
906
82.3k
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
907
        /* chain 4 mode : simplest access */
908
4.74k
        plane = addr & 3;
909
4.74k
        mask &= (1 << plane);
910
4.74k
        addr &= ~3;
911
77.6k
    } else {
912
77.6k
        if ((sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_SEQ_MODE) == 0) {
913
77.1k
            mask &= (addr & 1) ? 0x0a : 0x05;
914
77.1k
        }
915
77.6k
        if (s->gr[VGA_GFX_MISC] & VGA_GR06_CHAIN_ODD_EVEN) {
916
1.72k
            addr &= ~1;
917
1.72k
        }
918
77.6k
    }
919
920
    /* Doubleword/word mode.  These should be honored when displaying,
921
     * not when reading/writing to memory!  For example, chain4 modes
922
     * use double-word mode and, on real hardware, would fetch bytes
923
     * 0,1,2,3, 16,17,18,19, 32,33,34,35, etc.  Text modes use word
924
     * mode and, on real hardware, would fetch bytes 0,1, 8,9, etc.
925
     *
926
     * QEMU instead shifted addresses on memory accesses because it
927
     * allows more optimizations (e.g. chain4_alias) and simplifies
928
     * the draw_line handlers. Unfortunately, there is one case where
929
     * the difference shows.  When fetching font data, accesses are
930
     * always in consecutive bytes, even if the text/attribute pairs
931
     * are done in word mode.  Hence, doing a right shift when operating
932
     * on font data is wrong.  So check the odd/even mode bits together with
933
     * word mode bit.  The odd/even read bit is 0 when reading font data,
934
     * and the odd/even write bit is 1 when writing it.
935
     */
936
82.3k
    if (s->cr[VGA_CRTC_UNDERLINE] & VGA_CR14_DW) {
937
406
        addr >>= 2;
938
81.9k
    } else if ((sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_SEQ_MODE) == 0 &&
939
77.4k
               (s->cr[VGA_CRTC_MODE] & VGA_CR17_WORD_BYTE) == 0) {
940
77.0k
        addr >>= 1;
941
77.0k
    }
942
943
82.3k
    if (addr * sizeof(uint32_t) >= s->vram_size) {
944
367
        return;
945
367
    }
946
947
81.9k
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
948
4.73k
        if (mask) {
949
2.68k
            s->vram_ptr[(addr << 2) | plane] = val;
950
#ifdef DEBUG_VGA_MEM
951
            printf("vga: chain4: [0x" HWADDR_FMT_plx "]\n", addr);
952
#endif
953
2.68k
            s->plane_updated |= mask; /* only used to detect font change */
954
2.68k
            memory_region_set_dirty(&s->vram, addr, 1);
955
2.68k
        }
956
4.73k
        return;
957
4.73k
    }
958
959
    /* standard VGA latched access */
960
77.2k
    write_mode = s->gr[VGA_GFX_MODE] & 3;
961
77.2k
    switch(write_mode) {
962
0
    default:
963
62.4k
    case 0:
964
        /* rotate */
965
62.4k
        b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
966
62.4k
        val = ((val >> b) | (val << (8 - b))) & 0xff;
967
62.4k
        val |= val << 8;
968
62.4k
        val |= val << 16;
969
970
        /* apply set/reset mask */
971
62.4k
        set_mask = mask16[s->gr[VGA_GFX_SR_ENABLE]];
972
62.4k
        val = (val & ~set_mask) |
973
62.4k
            (mask16[s->gr[VGA_GFX_SR_VALUE]] & set_mask);
974
62.4k
        bit_mask = s->gr[VGA_GFX_BIT_MASK];
975
62.4k
        break;
976
3.75k
    case 1:
977
3.75k
        val = s->latch;
978
3.75k
        goto do_write;
979
2.35k
    case 2:
980
2.35k
        val = mask16[val & 0x0f];
981
2.35k
        bit_mask = s->gr[VGA_GFX_BIT_MASK];
982
2.35k
        break;
983
8.73k
    case 3:
984
        /* rotate */
985
8.73k
        b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
986
8.73k
        val = (val >> b) | (val << (8 - b));
987
988
8.73k
        bit_mask = s->gr[VGA_GFX_BIT_MASK] & val;
989
8.73k
        val = mask16[s->gr[VGA_GFX_SR_VALUE]];
990
8.73k
        break;
991
77.2k
    }
992
993
    /* apply logical operation */
994
73.4k
    func_select = s->gr[VGA_GFX_DATA_ROTATE] >> 3;
995
73.4k
    switch(func_select) {
996
71.9k
    case 0:
997
71.9k
    default:
998
        /* nothing to do */
999
71.9k
        break;
1000
71.9k
    case 1:
1001
        /* and */
1002
394
        val &= s->latch;
1003
394
        break;
1004
416
    case 2:
1005
        /* or */
1006
416
        val |= s->latch;
1007
416
        break;
1008
690
    case 3:
1009
        /* xor */
1010
690
        val ^= s->latch;
1011
690
        break;
1012
73.4k
    }
1013
1014
    /* apply bit mask */
1015
73.4k
    bit_mask |= bit_mask << 8;
1016
73.4k
    bit_mask |= bit_mask << 16;
1017
73.4k
    val = (val & bit_mask) | (s->latch & ~bit_mask);
1018
1019
77.2k
do_write:
1020
    /* mask data according to sr[2] */
1021
77.2k
    s->plane_updated |= mask; /* only used to detect font change */
1022
77.2k
    write_mask = mask16[mask];
1023
77.2k
    ((uint32_t *)s->vram_ptr)[addr] =
1024
77.2k
        (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
1025
77.2k
        (val & write_mask);
1026
#ifdef DEBUG_VGA_MEM
1027
    printf("vga: latch: [0x" HWADDR_FMT_plx "] mask=0x%08x val=0x%08x\n",
1028
           addr * 4, write_mask, val);
1029
#endif
1030
77.2k
    memory_region_set_dirty(&s->vram, addr << 2, sizeof(uint32_t));
1031
77.2k
}
1032
1033
typedef void *vga_draw_line_func(VGACommonState *s1, uint8_t *d,
1034
                                 uint32_t srcaddr, int width, int hpel);
1035
1036
#include "vga-access.h"
1037
#include "vga-helpers.h"
1038
1039
/* return true if the palette was modified */
1040
static int update_palette16(VGACommonState *s)
1041
0
{
1042
0
    int full_update, i;
1043
0
    uint32_t v, col, *palette;
1044
1045
0
    full_update = 0;
1046
0
    palette = s->last_palette;
1047
0
    for(i = 0; i < 16; i++) {
1048
0
        v = s->ar[i];
1049
0
        if (s->ar[VGA_ATC_MODE] & 0x80) {
1050
0
            v = ((s->ar[VGA_ATC_COLOR_PAGE] & 0xf) << 4) | (v & 0xf);
1051
0
        } else {
1052
0
            v = ((s->ar[VGA_ATC_COLOR_PAGE] & 0xc) << 4) | (v & 0x3f);
1053
0
        }
1054
0
        v = v * 3;
1055
0
        col = rgb_to_pixel32(c6_to_8(s->palette[v]),
1056
0
                             c6_to_8(s->palette[v + 1]),
1057
0
                             c6_to_8(s->palette[v + 2]));
1058
0
        if (col != palette[i]) {
1059
0
            full_update = 1;
1060
0
            palette[i] = col;
1061
0
        }
1062
0
    }
1063
0
    return full_update;
1064
0
}
1065
1066
/* return true if the palette was modified */
1067
static int update_palette256(VGACommonState *s)
1068
0
{
1069
0
    int full_update, i;
1070
0
    uint32_t v, col, *palette;
1071
1072
0
    full_update = 0;
1073
0
    palette = s->last_palette;
1074
0
    v = 0;
1075
0
    for(i = 0; i < 256; i++) {
1076
0
        if (s->dac_8bit) {
1077
0
            col = rgb_to_pixel32(s->palette[v],
1078
0
                                 s->palette[v + 1],
1079
0
                                 s->palette[v + 2]);
1080
0
        } else {
1081
0
            col = rgb_to_pixel32(c6_to_8(s->palette[v]),
1082
0
                                 c6_to_8(s->palette[v + 1]),
1083
0
                                 c6_to_8(s->palette[v + 2]));
1084
0
        }
1085
0
        if (col != palette[i]) {
1086
0
            full_update = 1;
1087
0
            palette[i] = col;
1088
0
        }
1089
0
        v += 3;
1090
0
    }
1091
0
    return full_update;
1092
0
}
1093
1094
static void vga_get_params(VGACommonState *s,
1095
                           VGADisplayParams *params)
1096
0
{
1097
0
    if (vbe_enabled(s)) {
1098
0
        params->line_offset = s->vbe_line_offset;
1099
0
        params->start_addr = s->vbe_start_addr;
1100
0
        params->line_compare = 65535;
1101
0
        params->hpel = VGA_HPEL_NEUTRAL;
1102
0
        params->hpel_split = false;
1103
0
    } else {
1104
        /* compute line_offset in bytes */
1105
0
        params->line_offset = s->cr[VGA_CRTC_OFFSET] << 3;
1106
1107
        /* starting address */
1108
0
        params->start_addr = s->cr[VGA_CRTC_START_LO] |
1109
0
            (s->cr[VGA_CRTC_START_HI] << 8);
1110
1111
        /* line compare */
1112
0
        params->line_compare = s->cr[VGA_CRTC_LINE_COMPARE] |
1113
0
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x10) << 4) |
1114
0
            ((s->cr[VGA_CRTC_MAX_SCAN] & 0x40) << 3);
1115
1116
0
        params->hpel = s->ar[VGA_ATC_PEL];
1117
0
        params->hpel_split = s->ar[VGA_ATC_MODE] & 0x20;
1118
0
    }
1119
0
}
1120
1121
/* update start_addr and line_offset. Return TRUE if modified */
1122
static int update_basic_params(VGACommonState *s)
1123
0
{
1124
0
    int full_update;
1125
0
    VGADisplayParams current;
1126
1127
0
    full_update = 0;
1128
1129
0
    s->get_params(s, &current);
1130
1131
0
    if (memcmp(&current, &s->params, sizeof(current))) {
1132
0
        s->params = current;
1133
0
        full_update = 1;
1134
0
    }
1135
0
    return full_update;
1136
0
}
1137
1138
1139
static const uint8_t cursor_glyph[32 * 4] = {
1140
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1141
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1142
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1143
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1144
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1145
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1146
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1147
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1148
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1149
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1150
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1151
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1152
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1153
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1154
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1155
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1156
};
1157
1158
static void vga_get_text_resolution(VGACommonState *s, int *pwidth, int *pheight,
1159
                                    int *pcwidth, int *pcheight)
1160
0
{
1161
0
    int width, cwidth, height, cheight;
1162
1163
    /* total width & height */
1164
0
    cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
1165
0
    cwidth = 8;
1166
0
    if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
1167
0
        cwidth = 9;
1168
0
    }
1169
0
    if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
1170
0
        cwidth = 16; /* NOTE: no 18 pixel wide */
1171
0
    }
1172
0
    width = (s->cr[VGA_CRTC_H_DISP] + 1);
1173
0
    if (s->cr[VGA_CRTC_V_TOTAL] == 100) {
1174
        /* ugly hack for CGA 160x100x16 - explain me the logic */
1175
0
        height = 100;
1176
0
    } else {
1177
0
        height = s->cr[VGA_CRTC_V_DISP_END] |
1178
0
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
1179
0
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
1180
0
        height = (height + 1) / cheight;
1181
0
    }
1182
1183
0
    *pwidth = width;
1184
0
    *pheight = height;
1185
0
    *pcwidth = cwidth;
1186
0
    *pcheight = cheight;
1187
0
}
1188
1189
/*
1190
 * Text mode update
1191
 * Missing:
1192
 * - double scan
1193
 * - double width
1194
 * - underline
1195
 */
1196
static void vga_draw_text(VGACommonState *s, int full_update)
1197
0
{
1198
0
    DisplaySurface *surface = qemu_console_surface(s->con);
1199
0
    int cx, cy, cheight, cw, ch, cattr, height, width, ch_attr;
1200
0
    int cx_min, cx_max, linesize, x_incr, line, line1;
1201
0
    uint32_t offset, fgcol, bgcol, v, cursor_offset;
1202
0
    uint8_t *d1, *d, *src, *dest, *cursor_ptr;
1203
0
    const uint8_t *font_ptr, *font_base[2];
1204
0
    int dup9, line_offset;
1205
0
    uint32_t *palette;
1206
0
    uint32_t *ch_attr_ptr;
1207
0
    int64_t now = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
1208
1209
    /* compute font data address (in plane 2) */
1210
0
    v = sr(s, VGA_SEQ_CHARACTER_MAP);
1211
0
    offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
1212
0
    if (offset != s->font_offsets[0]) {
1213
0
        s->font_offsets[0] = offset;
1214
0
        full_update = 1;
1215
0
    }
1216
0
    font_base[0] = s->vram_ptr + offset;
1217
1218
0
    offset = (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
1219
0
    font_base[1] = s->vram_ptr + offset;
1220
0
    if (offset != s->font_offsets[1]) {
1221
0
        s->font_offsets[1] = offset;
1222
0
        full_update = 1;
1223
0
    }
1224
0
    if (s->plane_updated & (1 << 2) || s->has_chain4_alias) {
1225
        /* if the plane 2 was modified since the last display, it
1226
           indicates the font may have been modified */
1227
0
        s->plane_updated = 0;
1228
0
        full_update = 1;
1229
0
    }
1230
0
    full_update |= update_basic_params(s);
1231
1232
0
    line_offset = s->params.line_offset;
1233
1234
0
    vga_get_text_resolution(s, &width, &height, &cw, &cheight);
1235
0
    if ((height * width) <= 1) {
1236
        /* better than nothing: exit if transient size is too small */
1237
0
        return;
1238
0
    }
1239
0
    if ((height * width) > CH_ATTR_SIZE) {
1240
        /* better than nothing: exit if transient size is too big */
1241
0
        return;
1242
0
    }
1243
1244
0
    if (surface == NULL ||
1245
0
        surface_width(surface) != width * cw ||
1246
0
        surface_height(surface) != height * cheight ||
1247
0
        width != s->last_text_width || height != s->last_text_height ||
1248
0
        cw != s->last_cw || cheight != s->last_ch || s->last_depth) {
1249
0
        s->last_scr_width = width * cw;
1250
0
        s->last_scr_height = height * cheight;
1251
0
        qemu_console_resize(s->con, s->last_scr_width, s->last_scr_height);
1252
0
        surface = qemu_console_surface(s->con);
1253
0
        qemu_console_text_resize(s->con, width, height);
1254
0
        s->last_depth = 0;
1255
0
        s->last_text_width = width;
1256
0
        s->last_text_height = height;
1257
0
        s->last_ch = cheight;
1258
0
        s->last_cw = cw;
1259
0
        full_update = 1;
1260
0
    }
1261
0
    full_update |= update_palette16(s);
1262
0
    palette = s->last_palette;
1263
0
    x_incr = cw * surface_bytes_per_pixel(surface);
1264
1265
0
    if (full_update) {
1266
0
        s->full_update_text = 1;
1267
0
    }
1268
0
    if (s->full_update_gfx) {
1269
0
        s->full_update_gfx = 0;
1270
0
        full_update |= 1;
1271
0
    }
1272
1273
0
    cursor_offset = ((s->cr[VGA_CRTC_CURSOR_HI] << 8) |
1274
0
                     s->cr[VGA_CRTC_CURSOR_LO]) - s->params.start_addr;
1275
0
    if (cursor_offset != s->cursor_offset ||
1276
0
        s->cr[VGA_CRTC_CURSOR_START] != s->cursor_start ||
1277
0
        s->cr[VGA_CRTC_CURSOR_END] != s->cursor_end) {
1278
      /* if the cursor position changed, we update the old and new
1279
         chars */
1280
0
        if (s->cursor_offset < CH_ATTR_SIZE)
1281
0
            s->last_ch_attr[s->cursor_offset] = -1;
1282
0
        if (cursor_offset < CH_ATTR_SIZE)
1283
0
            s->last_ch_attr[cursor_offset] = -1;
1284
0
        s->cursor_offset = cursor_offset;
1285
0
        s->cursor_start = s->cr[VGA_CRTC_CURSOR_START];
1286
0
        s->cursor_end = s->cr[VGA_CRTC_CURSOR_END];
1287
0
    }
1288
0
    cursor_ptr = s->vram_ptr + (s->params.start_addr + cursor_offset) * 4;
1289
0
    if (now >= s->cursor_blink_time) {
1290
0
        s->cursor_blink_time = now + VGA_TEXT_CURSOR_PERIOD_MS / 2;
1291
0
        s->cursor_visible_phase = !s->cursor_visible_phase;
1292
0
    }
1293
0
    if (now >= s->blink_time) {
1294
0
        s->blink_time = now + VGA_TEXT_BLINK_PERIOD_MS / 2;
1295
0
        s->blink_visible_phase = !s->blink_visible_phase;
1296
0
        if (s->ar[VGA_ATC_MODE] & 0x08) {
1297
0
            full_update = 1;
1298
0
        }
1299
0
    }
1300
1301
0
    dest = surface_data(surface);
1302
0
    linesize = surface_stride(surface);
1303
0
    ch_attr_ptr = s->last_ch_attr;
1304
0
    line = 0;
1305
0
    offset = s->params.start_addr * 4;
1306
0
    for(cy = 0; cy < height; cy++) {
1307
0
        d1 = dest;
1308
0
        src = s->vram_ptr + offset;
1309
0
        cx_min = width;
1310
0
        cx_max = -1;
1311
0
        for(cx = 0; cx < width; cx++) {
1312
0
            if (src + sizeof(uint16_t) > s->vram_ptr + s->vram_size) {
1313
0
                break;
1314
0
            }
1315
0
            ch_attr = *(uint16_t *)src;
1316
0
            if (full_update || ch_attr != *ch_attr_ptr || src == cursor_ptr) {
1317
0
                if (cx < cx_min)
1318
0
                    cx_min = cx;
1319
0
                if (cx > cx_max)
1320
0
                    cx_max = cx;
1321
0
                *ch_attr_ptr = ch_attr;
1322
#if HOST_BIG_ENDIAN
1323
                ch = ch_attr >> 8;
1324
                cattr = ch_attr & 0xff;
1325
#else
1326
0
                ch = ch_attr & 0xff;
1327
0
                cattr = ch_attr >> 8;
1328
0
#endif
1329
0
                font_ptr = font_base[(cattr >> 3) & 1];
1330
0
                font_ptr += 32 * 4 * ch;
1331
0
                if (s->ar[VGA_ATC_MODE] & 0x08) {
1332
0
                    bgcol = palette[(cattr >> 4) & 0x07];
1333
0
                    if ((cattr & 0x80) && !s->blink_visible_phase) {
1334
0
                        fgcol = bgcol;
1335
0
                    } else {
1336
0
                        fgcol = palette[cattr & 0x0f];
1337
0
                    }
1338
0
                } else {
1339
0
                    bgcol = palette[cattr >> 4];
1340
0
                    fgcol = palette[cattr & 0x0f];
1341
0
                }
1342
0
                if (cw == 16) {
1343
0
                    vga_draw_glyph16(d1, linesize,
1344
0
                                     font_ptr, cheight, fgcol, bgcol);
1345
0
                } else if (cw != 9) {
1346
0
                    vga_draw_glyph8(d1, linesize,
1347
0
                                    font_ptr, cheight, fgcol, bgcol);
1348
0
                } else {
1349
0
                    dup9 = 0;
1350
0
                    if (ch >= 0xb0 && ch <= 0xdf &&
1351
0
                        (s->ar[VGA_ATC_MODE] & 0x04)) {
1352
0
                        dup9 = 1;
1353
0
                    }
1354
0
                    vga_draw_glyph9(d1, linesize,
1355
0
                                    font_ptr, cheight, fgcol, bgcol, dup9);
1356
0
                }
1357
0
                if (src == cursor_ptr &&
1358
0
                    !(s->cr[VGA_CRTC_CURSOR_START] & 0x20) &&
1359
0
                    s->cursor_visible_phase) {
1360
0
                    int line_start, line_last, h;
1361
                    /* draw the cursor */
1362
0
                    line_start = s->cr[VGA_CRTC_CURSOR_START] & 0x1f;
1363
0
                    line_last = s->cr[VGA_CRTC_CURSOR_END] & 0x1f;
1364
                    /* XXX: check that */
1365
0
                    if (line_last > cheight - 1)
1366
0
                        line_last = cheight - 1;
1367
0
                    if (line_last >= line_start && line_start < cheight) {
1368
0
                        h = line_last - line_start + 1;
1369
0
                        d = d1 + linesize * line_start;
1370
0
                        if (cw == 16) {
1371
0
                            vga_draw_glyph16(d, linesize,
1372
0
                                             cursor_glyph, h, fgcol, bgcol);
1373
0
                        } else if (cw != 9) {
1374
0
                            vga_draw_glyph8(d, linesize,
1375
0
                                            cursor_glyph, h, fgcol, bgcol);
1376
0
                        } else {
1377
0
                            vga_draw_glyph9(d, linesize,
1378
0
                                            cursor_glyph, h, fgcol, bgcol, 1);
1379
0
                        }
1380
0
                    }
1381
0
                }
1382
0
            }
1383
0
            d1 += x_incr;
1384
0
            src += 4;
1385
0
            ch_attr_ptr++;
1386
0
        }
1387
0
        if (cx_max != -1) {
1388
0
            qemu_console_update(s->con, cx_min * cw, cy * cheight,
1389
0
                                (cx_max - cx_min + 1) * cw, cheight);
1390
0
        }
1391
0
        dest += linesize * cheight;
1392
0
        line1 = line + cheight;
1393
0
        offset += line_offset;
1394
0
        if (line < s->params.line_compare && line1 >= s->params.line_compare) {
1395
0
            offset = 0;
1396
0
        }
1397
0
        line = line1;
1398
0
    }
1399
0
}
1400
1401
enum {
1402
    VGA_DRAW_LINE2,
1403
    VGA_DRAW_LINE2D2,
1404
    VGA_DRAW_LINE4,
1405
    VGA_DRAW_LINE4D2,
1406
    VGA_DRAW_LINE8D2,
1407
    VGA_DRAW_LINE8,
1408
    VGA_DRAW_LINE15_LE,
1409
    VGA_DRAW_LINE16_LE,
1410
    VGA_DRAW_LINE24_LE,
1411
    VGA_DRAW_LINE32_LE,
1412
    VGA_DRAW_LINE15_BE,
1413
    VGA_DRAW_LINE16_BE,
1414
    VGA_DRAW_LINE24_BE,
1415
    VGA_DRAW_LINE32_BE,
1416
    VGA_DRAW_LINE_NB,
1417
};
1418
1419
static vga_draw_line_func * const vga_draw_line_table[VGA_DRAW_LINE_NB] = {
1420
    vga_draw_line2,
1421
    vga_draw_line2d2,
1422
    vga_draw_line4,
1423
    vga_draw_line4d2,
1424
    vga_draw_line8d2,
1425
    vga_draw_line8,
1426
    vga_draw_line15_le,
1427
    vga_draw_line16_le,
1428
    vga_draw_line24_le,
1429
    vga_draw_line32_le,
1430
    vga_draw_line15_be,
1431
    vga_draw_line16_be,
1432
    vga_draw_line24_be,
1433
    vga_draw_line32_be,
1434
};
1435
1436
static int vga_get_bpp(VGACommonState *s)
1437
0
{
1438
0
    int ret;
1439
1440
0
    if (vbe_enabled(s)) {
1441
0
        ret = s->vbe_regs[VBE_DISPI_INDEX_BPP];
1442
0
    } else {
1443
0
        ret = 0;
1444
0
    }
1445
0
    return ret;
1446
0
}
1447
1448
static void vga_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
1449
0
{
1450
0
    int width, height;
1451
1452
0
    if (vbe_enabled(s)) {
1453
0
        width = s->vbe_regs[VBE_DISPI_INDEX_XRES];
1454
0
        height = s->vbe_regs[VBE_DISPI_INDEX_YRES];
1455
0
    } else {
1456
0
        width = (s->cr[VGA_CRTC_H_DISP] + 1) * 8;
1457
0
        height = s->cr[VGA_CRTC_V_DISP_END] |
1458
0
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
1459
0
            ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
1460
0
        height = (height + 1);
1461
0
    }
1462
0
    *pwidth = width;
1463
0
    *pheight = height;
1464
0
}
1465
1466
void vga_invalidate_scanlines(VGACommonState *s, int y1, int y2)
1467
0
{
1468
0
    int y;
1469
0
    if (y1 >= VGA_MAX_HEIGHT)
1470
0
        return;
1471
0
    if (y2 >= VGA_MAX_HEIGHT)
1472
0
        y2 = VGA_MAX_HEIGHT;
1473
0
    for(y = y1; y < y2; y++) {
1474
0
        s->invalidated_y_table[y >> 5] |= 1 << (y & 0x1f);
1475
0
    }
1476
0
}
1477
1478
static bool vga_scanline_invalidated(VGACommonState *s, int y)
1479
0
{
1480
0
    if (y >= VGA_MAX_HEIGHT) {
1481
0
        return false;
1482
0
    }
1483
0
    return s->invalidated_y_table[y >> 5] & (1 << (y & 0x1f));
1484
0
}
1485
1486
void vga_dirty_log_start(VGACommonState *s)
1487
6.41k
{
1488
6.41k
    memory_region_set_log(&s->vram, true, DIRTY_MEMORY_VGA);
1489
6.41k
}
1490
1491
void vga_dirty_log_stop(VGACommonState *s)
1492
0
{
1493
0
    memory_region_set_log(&s->vram, false, DIRTY_MEMORY_VGA);
1494
0
}
1495
1496
/*
1497
 * graphic modes
1498
 */
1499
static void vga_draw_graphic(VGACommonState *s, int full_update)
1500
0
{
1501
0
    DisplaySurface *surface = qemu_console_surface(s->con);
1502
0
    int y1, y, update, linesize, y_start, double_scan, mask, depth;
1503
0
    int width, height, shift_control, bwidth, bits;
1504
0
    ram_addr_t page0, page1, region_start, region_end;
1505
0
    DirtyBitmapSnapshot *snap = NULL;
1506
0
    int disp_width, multi_scan, multi_run;
1507
0
    int hpel;
1508
0
    uint8_t *d;
1509
0
    uint32_t v, addr1, addr;
1510
0
    vga_draw_line_func *vga_draw_line = NULL;
1511
0
    bool allocate_surface, force_shadow = false;
1512
0
    pixman_format_code_t format;
1513
#if HOST_BIG_ENDIAN
1514
    bool byteswap = !s->big_endian_fb;
1515
#else
1516
0
    bool byteswap = s->big_endian_fb;
1517
0
#endif
1518
1519
0
    full_update |= update_basic_params(s);
1520
1521
0
    s->get_resolution(s, &width, &height);
1522
0
    disp_width = width;
1523
0
    depth = s->get_bpp(s);
1524
1525
    /* bits 5-6: 0 = 16-color mode, 1 = 4-color mode, 2 = 256-color mode.  */
1526
0
    shift_control = (s->gr[VGA_GFX_MODE] >> 5) & 3;
1527
0
    double_scan = (s->cr[VGA_CRTC_MAX_SCAN] >> 7);
1528
0
    if (s->cr[VGA_CRTC_MODE] & 1) {
1529
0
        multi_scan = (((s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1) << double_scan)
1530
0
            - 1;
1531
0
    } else {
1532
        /* in CGA modes, multi_scan is ignored */
1533
        /* XXX: is it correct ? */
1534
0
        multi_scan = double_scan;
1535
0
    }
1536
0
    multi_run = multi_scan;
1537
0
    if (shift_control != s->shift_control ||
1538
0
        double_scan != s->double_scan) {
1539
0
        full_update = 1;
1540
0
        s->shift_control = shift_control;
1541
0
        s->double_scan = double_scan;
1542
0
    }
1543
1544
0
    if (shift_control == 0) {
1545
0
        full_update |= update_palette16(s);
1546
0
        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
1547
0
            disp_width <<= 1;
1548
0
            v = VGA_DRAW_LINE4D2;
1549
0
        } else {
1550
0
            v = VGA_DRAW_LINE4;
1551
0
        }
1552
0
        bits = 4;
1553
1554
0
    } else if (shift_control == 1) {
1555
0
        full_update |= update_palette16(s);
1556
0
        if (sr(s, VGA_SEQ_CLOCK_MODE) & 8) {
1557
0
            disp_width <<= 1;
1558
0
            v = VGA_DRAW_LINE2D2;
1559
0
        } else {
1560
0
            v = VGA_DRAW_LINE2;
1561
0
        }
1562
0
        bits = 4;
1563
1564
0
    } else {
1565
0
        switch (depth) {
1566
0
        default:
1567
0
        case 0:
1568
0
            full_update |= update_palette256(s);
1569
0
            v = VGA_DRAW_LINE8D2;
1570
0
            bits = 4;
1571
0
            break;
1572
0
        case 8:
1573
0
            full_update |= update_palette256(s);
1574
0
            v = VGA_DRAW_LINE8;
1575
0
            bits = 8;
1576
0
            break;
1577
0
        case 15:
1578
0
            v = s->big_endian_fb ? VGA_DRAW_LINE15_BE : VGA_DRAW_LINE15_LE;
1579
0
            bits = 16;
1580
0
            break;
1581
0
        case 16:
1582
0
            v = s->big_endian_fb ? VGA_DRAW_LINE16_BE : VGA_DRAW_LINE16_LE;
1583
0
            bits = 16;
1584
0
            break;
1585
0
        case 24:
1586
0
            v = s->big_endian_fb ? VGA_DRAW_LINE24_BE : VGA_DRAW_LINE24_LE;
1587
0
            bits = 24;
1588
0
            break;
1589
0
        case 32:
1590
0
            v = s->big_endian_fb ? VGA_DRAW_LINE32_BE : VGA_DRAW_LINE32_LE;
1591
0
            bits = 32;
1592
0
            break;
1593
0
        }
1594
0
    }
1595
1596
    /* Horizontal pel panning bit 3 is only used in text mode.  */
1597
0
    hpel = bits <= 8 ? s->params.hpel & 7 : 0;
1598
0
    bwidth = DIV_ROUND_UP(width * bits, 8); /* scanline length */
1599
0
    if (hpel) {
1600
0
        bwidth += 4;
1601
0
    }
1602
1603
0
    region_start = (s->params.start_addr * 4);
1604
0
    region_end = region_start + (ram_addr_t)s->params.line_offset * (height - 1) + bwidth;
1605
0
    if (region_end > s->vbe_size) {
1606
        /*
1607
         * On wrap around take the safe and slow route:
1608
         *   - create a dirty bitmap snapshot for all vga memory.
1609
         *   - force shadowing (so all vga memory access goes
1610
         *     through vga_read_*() helpers).
1611
         *
1612
         * Given this affects only vga features which are pretty much
1613
         * unused by modern guests there should be no performance
1614
         * impact.
1615
         */
1616
0
        region_start = 0;
1617
0
        region_end = s->vbe_size;
1618
0
        force_shadow = true;
1619
0
    }
1620
0
    if (s->params.line_compare < height) {
1621
        /* split screen mode */
1622
0
        region_start = 0;
1623
0
    }
1624
1625
    /*
1626
     * Check whether we can share the surface with the backend
1627
     * or whether we need a shadow surface. We share native
1628
     * endian surfaces for 15bpp and above and byteswapped
1629
     * surfaces for 24bpp and above.
1630
     */
1631
0
    format = qemu_default_pixman_format(depth, !byteswap);
1632
0
    if (format) {
1633
0
        allocate_surface = !qemu_console_check_format(s->con, format)
1634
0
            || s->force_shadow || force_shadow;
1635
0
    } else {
1636
0
        allocate_surface = true;
1637
0
    }
1638
1639
0
    if (s->params.line_offset != s->last_line_offset ||
1640
0
        disp_width != s->last_width ||
1641
0
        height != s->last_height ||
1642
0
        s->last_depth != depth ||
1643
0
        s->last_byteswap != byteswap ||
1644
0
        allocate_surface != surface_is_allocated(surface) ||
1645
0
        disp_width != surface_width(surface) ||
1646
0
        height != surface_height(surface)) {
1647
        /* display parameters changed -> need new display surface */
1648
0
        s->last_scr_width = disp_width;
1649
0
        s->last_scr_height = height;
1650
0
        s->last_width = disp_width;
1651
0
        s->last_height = height;
1652
0
        s->last_line_offset = s->params.line_offset;
1653
0
        s->last_depth = depth;
1654
0
        s->last_byteswap = byteswap;
1655
0
        full_update = 1;
1656
0
    }
1657
1658
    /* 16 extra pixels are needed for double-width planar modes. */
1659
0
    s->panning_buf = g_realloc(s->panning_buf,
1660
0
                               (disp_width + 16) * sizeof(uint32_t));
1661
0
    if (surface_data(surface) != s->vram_ptr + (s->params.start_addr * 4)
1662
0
        && !surface_is_allocated(surface)) {
1663
        /* base address changed (page flip) -> shared display surfaces
1664
         * must be updated with the new base address */
1665
0
        full_update = 1;
1666
0
    }
1667
1668
0
    if (full_update) {
1669
0
        if (!allocate_surface) {
1670
0
            surface = qemu_create_displaysurface_from(disp_width,
1671
0
                    height, format, s->params.line_offset,
1672
0
                    s->vram_ptr + (s->params.start_addr * 4));
1673
0
            qemu_console_set_surface(s->con, surface);
1674
0
        } else {
1675
0
            qemu_console_resize(s->con, disp_width, height);
1676
0
            surface = qemu_console_surface(s->con);
1677
0
        }
1678
0
    }
1679
1680
0
    vga_draw_line = vga_draw_line_table[v];
1681
1682
0
    if (surface_is_allocated(surface) && s->cursor_invalidate) {
1683
0
        s->cursor_invalidate(s);
1684
0
    }
1685
1686
#if 0
1687
    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",
1688
           width, height, v, s->params.line_offset, s->cr[9], s->cr[VGA_CRTC_MODE],
1689
           s->params.line_compare, sr(s, VGA_SEQ_CLOCK_MODE));
1690
#endif
1691
0
    addr1 = (s->params.start_addr * 4);
1692
0
    y_start = -1;
1693
0
    d = surface_data(surface);
1694
0
    linesize = surface_stride(surface);
1695
0
    y1 = 0;
1696
1697
0
    if (!full_update) {
1698
0
        snap = memory_region_snapshot_and_clear_dirty(&s->vram, region_start,
1699
0
                                                      region_end - region_start,
1700
0
                                                      DIRTY_MEMORY_VGA);
1701
0
    }
1702
1703
0
    for(y = 0; y < height; y++) {
1704
0
        addr = addr1;
1705
0
        if (!(s->cr[VGA_CRTC_MODE] & 1)) {
1706
0
            int shift;
1707
            /* CGA compatibility handling */
1708
0
            shift = 14 + ((s->cr[VGA_CRTC_MODE] >> 6) & 1);
1709
0
            addr = (addr & ~(1 << shift)) | ((y1 & 1) << shift);
1710
0
        }
1711
0
        if (!(s->cr[VGA_CRTC_MODE] & 2)) {
1712
0
            addr = (addr & ~0x8000) | ((y1 & 2) << 14);
1713
0
        }
1714
0
        page0 = addr & s->vbe_size_mask;
1715
0
        page1 = (addr + bwidth - 1) & s->vbe_size_mask;
1716
0
        if (full_update) {
1717
0
            update = 1;
1718
0
        } else if (page1 < page0) {
1719
            /* scanline wraps from end of video memory to the start */
1720
0
            assert(force_shadow);
1721
0
            update = memory_region_snapshot_get_dirty(&s->vram, snap,
1722
0
                                                      page0, s->vbe_size - page0);
1723
0
            update |= memory_region_snapshot_get_dirty(&s->vram, snap,
1724
0
                                                       0, page1);
1725
0
        } else {
1726
0
            update = memory_region_snapshot_get_dirty(&s->vram, snap,
1727
0
                                                      page0, page1 - page0);
1728
0
        }
1729
        /* explicit invalidation for the hardware cursor (cirrus only) */
1730
0
        update |= vga_scanline_invalidated(s, y);
1731
0
        if (update) {
1732
0
            if (y_start < 0)
1733
0
                y_start = y;
1734
0
            if (surface_is_allocated(surface)) {
1735
0
                uint8_t *p;
1736
0
                p = vga_draw_line(s, d, addr, width, hpel);
1737
0
                if (p) {
1738
0
                    memcpy(d, p, disp_width * sizeof(uint32_t));
1739
0
                }
1740
0
                if (s->cursor_draw_line)
1741
0
                    s->cursor_draw_line(s, d, y);
1742
0
            }
1743
0
        } else {
1744
0
            if (y_start >= 0) {
1745
                /* flush to display */
1746
0
                qemu_console_update(s->con, 0, y_start, disp_width, y - y_start);
1747
0
                y_start = -1;
1748
0
            }
1749
0
        }
1750
0
        if (!multi_run) {
1751
0
            mask = (s->cr[VGA_CRTC_MODE] & 3) ^ 3;
1752
0
            if ((y1 & mask) == mask)
1753
0
                addr1 += s->params.line_offset;
1754
0
            y1++;
1755
0
            multi_run = multi_scan;
1756
0
        } else {
1757
0
            multi_run--;
1758
0
        }
1759
        /* line compare acts on the displayed lines */
1760
0
        if (y == s->params.line_compare) {
1761
0
            if (s->params.hpel_split) {
1762
0
                hpel = VGA_HPEL_NEUTRAL;
1763
0
            }
1764
0
            addr1 = 0;
1765
0
        }
1766
0
        d += linesize;
1767
0
    }
1768
0
    if (y_start >= 0) {
1769
        /* flush to display */
1770
0
        qemu_console_update(s->con, 0, y_start, disp_width, y - y_start);
1771
0
    }
1772
0
    g_free(snap);
1773
0
    memset(s->invalidated_y_table, 0, sizeof(s->invalidated_y_table));
1774
0
}
1775
1776
static void vga_draw_blank(VGACommonState *s, int full_update)
1777
0
{
1778
0
    DisplaySurface *surface = qemu_console_surface(s->con);
1779
0
    int i, w;
1780
0
    uint8_t *d;
1781
1782
0
    if (!full_update)
1783
0
        return;
1784
0
    if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
1785
0
        return;
1786
1787
0
    if (!surface_is_allocated(surface)) {
1788
        /* unshare buffer, otherwise the blanking corrupts vga vram */
1789
0
        surface = qemu_create_displaysurface(s->last_scr_width,
1790
0
                                             s->last_scr_height);
1791
0
        qemu_console_set_surface(s->con, surface);
1792
0
    }
1793
1794
0
    w = surface_width(surface) * surface_bytes_per_pixel(surface);
1795
0
    d = surface_data(surface);
1796
0
    for (i = 0; i < surface_height(surface); i++) {
1797
0
        memset(d, 0, w);
1798
0
        d += surface_stride(surface);
1799
0
    }
1800
0
    qemu_console_update_full(s->con);
1801
0
}
1802
1803
0
#define GMODE_TEXT     0
1804
0
#define GMODE_GRAPH    1
1805
0
#define GMODE_BLANK 2
1806
1807
static bool vga_update_display(void *opaque)
1808
0
{
1809
0
    VGACommonState *s = opaque;
1810
0
    DisplaySurface *surface = qemu_console_surface(s->con);
1811
0
    int full_update, graphic_mode;
1812
1813
0
    qemu_flush_coalesced_mmio_buffer();
1814
1815
0
    if (surface_bits_per_pixel(surface) == 0) {
1816
        /* nothing to do */
1817
0
    } else {
1818
0
        full_update = 0;
1819
0
        if (!(s->ar_index & 0x20)) {
1820
0
            graphic_mode = GMODE_BLANK;
1821
0
        } else {
1822
0
            graphic_mode = s->gr[VGA_GFX_MISC] & VGA_GR06_GRAPHICS_MODE;
1823
0
        }
1824
0
        if (graphic_mode != s->graphic_mode) {
1825
0
            s->graphic_mode = graphic_mode;
1826
0
            s->cursor_blink_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
1827
0
            full_update = 1;
1828
0
        }
1829
0
        switch(graphic_mode) {
1830
0
        case GMODE_TEXT:
1831
0
            vga_draw_text(s, full_update);
1832
0
            break;
1833
0
        case GMODE_GRAPH:
1834
0
            vga_draw_graphic(s, full_update);
1835
0
            break;
1836
0
        case GMODE_BLANK:
1837
0
        default:
1838
0
            vga_draw_blank(s, full_update);
1839
0
            break;
1840
0
        }
1841
0
    }
1842
1843
0
    return true;
1844
0
}
1845
1846
/* force a full display refresh */
1847
static void vga_invalidate_display(void *opaque)
1848
0
{
1849
0
    VGACommonState *s = opaque;
1850
1851
0
    s->last_width = -1;
1852
0
    s->last_height = -1;
1853
0
    s->last_text_width = -1;
1854
0
    s->last_text_height = -1;
1855
0
}
1856
1857
void vga_common_reset(VGACommonState *s)
1858
37.2k
{
1859
37.2k
    s->sr_index = 0;
1860
37.2k
    memset(s->sr, '\0', sizeof(s->sr));
1861
37.2k
    memset(s->sr_vbe, '\0', sizeof(s->sr_vbe));
1862
37.2k
    s->gr_index = 0;
1863
37.2k
    memset(s->gr, '\0', sizeof(s->gr));
1864
37.2k
    s->ar_index = 0;
1865
37.2k
    memset(s->ar, '\0', sizeof(s->ar));
1866
37.2k
    s->ar_flip_flop = 0;
1867
37.2k
    s->cr_index = 0;
1868
37.2k
    memset(s->cr, '\0', sizeof(s->cr));
1869
37.2k
    s->msr = 0;
1870
37.2k
    s->fcr = 0;
1871
37.2k
    s->st00 = 0;
1872
37.2k
    s->st01 = 0;
1873
37.2k
    s->dac_state = 0;
1874
37.2k
    s->dac_sub_index = 0;
1875
37.2k
    s->dac_read_index = 0;
1876
37.2k
    s->dac_write_index = 0;
1877
37.2k
    memset(s->dac_cache, '\0', sizeof(s->dac_cache));
1878
37.2k
    s->dac_8bit = 0;
1879
37.2k
    memset(s->palette, '\0', sizeof(s->palette));
1880
37.2k
    s->bank_offset = 0;
1881
37.2k
    s->vbe_index = 0;
1882
37.2k
    memset(s->vbe_regs, '\0', sizeof(s->vbe_regs));
1883
37.2k
    s->vbe_regs[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
1884
37.2k
    s->vbe_start_addr = 0;
1885
37.2k
    s->vbe_line_offset = 0;
1886
37.2k
    s->vbe_bank_mask = (s->vram_size >> 16) - 1;
1887
37.2k
    memset(s->font_offsets, '\0', sizeof(s->font_offsets));
1888
37.2k
    s->graphic_mode = -1; /* force full update */
1889
37.2k
    s->shift_control = 0;
1890
37.2k
    s->double_scan = 0;
1891
37.2k
    memset(&s->params, '\0', sizeof(s->params));
1892
37.2k
    s->plane_updated = 0;
1893
37.2k
    s->last_cw = 0;
1894
37.2k
    s->last_ch = 0;
1895
37.2k
    s->last_width = 0;
1896
37.2k
    s->last_height = 0;
1897
37.2k
    s->last_text_width = 0;
1898
37.2k
    s->last_text_height = 0;
1899
37.2k
    s->last_scr_width = 0;
1900
37.2k
    s->last_scr_height = 0;
1901
37.2k
    s->cursor_start = 0;
1902
37.2k
    s->cursor_end = 0;
1903
37.2k
    s->cursor_offset = 0;
1904
37.2k
    memset(s->invalidated_y_table, '\0', sizeof(s->invalidated_y_table));
1905
37.2k
    memset(s->last_palette, '\0', sizeof(s->last_palette));
1906
37.2k
    memset(s->last_ch_attr, '\0', sizeof(s->last_ch_attr));
1907
37.2k
    switch (vga_retrace_method) {
1908
37.2k
    case VGA_RETRACE_DUMB:
1909
37.2k
        break;
1910
0
    case VGA_RETRACE_PRECISE:
1911
0
        memset(&s->retrace_info, 0, sizeof (s->retrace_info));
1912
0
        break;
1913
37.2k
    }
1914
37.2k
    vga_update_memory_access(s);
1915
37.2k
}
1916
1917
static void vga_reset(void *opaque)
1918
6.65k
{
1919
6.65k
    VGACommonState *s =  opaque;
1920
6.65k
    vga_common_reset(s);
1921
6.65k
}
1922
1923
0
#define TEXTMODE_X(x) ((x) % width)
1924
0
#define TEXTMODE_Y(x) ((x) / width)
1925
0
#define VMEM2CHTYPE(v)  ((v & 0xff0007ff) | \
1926
0
        ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
1927
/* relay text rendering to the display driver
1928
 * instead of doing a full vga_update_display() */
1929
static void vga_update_text(void *opaque, uint32_t *chardata)
1930
0
{
1931
0
    VGACommonState *s =  opaque;
1932
0
    int graphic_mode, i, cursor_offset, cursor_visible;
1933
0
    int cw, cheight, width, height, size, c_min, c_max;
1934
0
    uint32_t *src;
1935
0
    uint32_t *dst, val;
1936
0
    char msg_buffer[80];
1937
0
    int full_update = 0;
1938
1939
0
    qemu_flush_coalesced_mmio_buffer();
1940
1941
0
    if (!(s->ar_index & 0x20)) {
1942
0
        graphic_mode = GMODE_BLANK;
1943
0
    } else {
1944
0
        graphic_mode = s->gr[VGA_GFX_MISC] & VGA_GR06_GRAPHICS_MODE;
1945
0
    }
1946
0
    if (graphic_mode != s->graphic_mode) {
1947
0
        s->graphic_mode = graphic_mode;
1948
0
        full_update = 1;
1949
0
    }
1950
0
    if (s->last_text_width == -1) {
1951
0
        s->last_text_width = 0;
1952
0
        full_update = 1;
1953
0
    }
1954
1955
0
    switch (graphic_mode) {
1956
0
    case GMODE_TEXT:
1957
        /* TODO: update palette */
1958
0
        full_update |= update_basic_params(s);
1959
1960
        /* total width & height */
1961
0
        cheight = (s->cr[VGA_CRTC_MAX_SCAN] & 0x1f) + 1;
1962
0
        cw = 8;
1963
0
        if (!(sr(s, VGA_SEQ_CLOCK_MODE) & VGA_SR01_CHAR_CLK_8DOTS)) {
1964
0
            cw = 9;
1965
0
        }
1966
0
        if (sr(s, VGA_SEQ_CLOCK_MODE) & 0x08) {
1967
0
            cw = 16; /* NOTE: no 18 pixel wide */
1968
0
        }
1969
0
        width = (s->cr[VGA_CRTC_H_DISP] + 1);
1970
0
        if (s->cr[VGA_CRTC_V_TOTAL] == 100) {
1971
            /* ugly hack for CGA 160x100x16 - explain me the logic */
1972
0
            height = 100;
1973
0
        } else {
1974
0
            height = s->cr[VGA_CRTC_V_DISP_END] |
1975
0
                ((s->cr[VGA_CRTC_OVERFLOW] & 0x02) << 7) |
1976
0
                ((s->cr[VGA_CRTC_OVERFLOW] & 0x40) << 3);
1977
0
            height = (height + 1) / cheight;
1978
0
        }
1979
1980
0
        size = (height * width);
1981
0
        if (size > CH_ATTR_SIZE) {
1982
0
            if (!full_update)
1983
0
                return;
1984
1985
0
            snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Text mode",
1986
0
                     width, height);
1987
0
            break;
1988
0
        }
1989
1990
0
        if (width != s->last_text_width || height != s->last_text_height ||
1991
0
            cw != s->last_cw || cheight != s->last_ch) {
1992
0
            s->last_scr_width = width * cw;
1993
0
            s->last_scr_height = height * cheight;
1994
0
            qemu_console_resize(s->con, s->last_scr_width, s->last_scr_height);
1995
0
            qemu_console_text_resize(s->con, width, height);
1996
0
            s->last_depth = 0;
1997
0
            s->last_text_width = width;
1998
0
            s->last_text_height = height;
1999
0
            s->last_ch = cheight;
2000
0
            s->last_cw = cw;
2001
0
            full_update = 1;
2002
0
        }
2003
2004
0
        if (full_update) {
2005
0
            s->full_update_gfx = 1;
2006
0
        }
2007
0
        if (s->full_update_text) {
2008
0
            s->full_update_text = 0;
2009
0
            full_update |= 1;
2010
0
        }
2011
2012
        /* Update "hardware" cursor */
2013
0
        cursor_offset = ((s->cr[VGA_CRTC_CURSOR_HI] << 8) |
2014
0
                         s->cr[VGA_CRTC_CURSOR_LO]) - s->params.start_addr;
2015
0
        if (cursor_offset != s->cursor_offset ||
2016
0
            s->cr[VGA_CRTC_CURSOR_START] != s->cursor_start ||
2017
0
            s->cr[VGA_CRTC_CURSOR_END] != s->cursor_end || full_update) {
2018
0
            cursor_visible = !(s->cr[VGA_CRTC_CURSOR_START] & 0x20);
2019
0
            if (cursor_visible && cursor_offset < size && cursor_offset >= 0)
2020
0
                qemu_console_text_set_cursor(s->con,
2021
0
                                             TEXTMODE_X(cursor_offset),
2022
0
                                             TEXTMODE_Y(cursor_offset));
2023
0
            else
2024
0
                qemu_console_text_set_cursor(s->con, -1, -1);
2025
0
            s->cursor_offset = cursor_offset;
2026
0
            s->cursor_start = s->cr[VGA_CRTC_CURSOR_START];
2027
0
            s->cursor_end = s->cr[VGA_CRTC_CURSOR_END];
2028
0
        }
2029
2030
0
        src = (uint32_t *) s->vram_ptr + s->params.start_addr;
2031
0
        dst = chardata;
2032
2033
0
        if (full_update) {
2034
0
            for (i = 0; i < size; src ++, dst ++, i ++)
2035
0
                *dst = VMEM2CHTYPE(le32_to_cpu(*src));
2036
2037
0
            qemu_console_text_update(s->con, 0, 0, width, height);
2038
0
        } else {
2039
0
            c_max = 0;
2040
2041
0
            for (i = 0; i < size; src ++, dst ++, i ++) {
2042
0
                val = VMEM2CHTYPE(le32_to_cpu(*src));
2043
0
                if (*dst != val) {
2044
0
                    *dst = val;
2045
0
                    c_max = i;
2046
0
                    break;
2047
0
                }
2048
0
            }
2049
0
            c_min = i;
2050
0
            for (; i < size; src ++, dst ++, i ++) {
2051
0
                val = VMEM2CHTYPE(le32_to_cpu(*src));
2052
0
                if (*dst != val) {
2053
0
                    *dst = val;
2054
0
                    c_max = i;
2055
0
                }
2056
0
            }
2057
2058
0
            if (c_min <= c_max) {
2059
0
                i = TEXTMODE_Y(c_min);
2060
0
                qemu_console_text_update(s->con, 0, i, width, TEXTMODE_Y(c_max) - i + 1);
2061
0
            }
2062
0
        }
2063
2064
0
        return;
2065
0
    case GMODE_GRAPH:
2066
0
        if (!full_update)
2067
0
            return;
2068
2069
0
        s->get_resolution(s, &width, &height);
2070
0
        snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Graphic mode",
2071
0
                 width, height);
2072
0
        break;
2073
0
    case GMODE_BLANK:
2074
0
    default:
2075
0
        if (!full_update)
2076
0
            return;
2077
2078
0
        snprintf(msg_buffer, sizeof(msg_buffer), "VGA Blank mode");
2079
0
        break;
2080
0
    }
2081
2082
    /* Display a message */
2083
0
    s->last_text_width = 60;
2084
0
    s->last_text_height = height = 3;
2085
0
    qemu_console_text_set_cursor(s->con, -1, -1);
2086
0
    qemu_console_text_resize(s->con, s->last_text_width, height);
2087
2088
0
    for (dst = chardata, i = 0; i < s->last_text_width * height; i ++)
2089
0
        *dst++ = ' ';
2090
2091
0
    size = strlen(msg_buffer);
2092
0
    width = (s->last_text_width - size) / 2;
2093
0
    dst = chardata + s->last_text_width + width;
2094
0
    for (i = 0; i < size; i ++)
2095
0
        *dst++ = ATTR2CHTYPE(msg_buffer[i], QEMU_COLOR_BLUE,
2096
0
                             QEMU_COLOR_BLACK, 1);
2097
2098
0
    qemu_console_text_update(s->con, 0, 0, s->last_text_width, height);
2099
0
}
2100
2101
static uint64_t vga_mem_read(void *opaque, hwaddr addr,
2102
                             unsigned size)
2103
7.02M
{
2104
7.02M
    VGACommonState *s = opaque;
2105
2106
7.02M
    return vga_mem_readb(s, addr);
2107
7.02M
}
2108
2109
static void vga_mem_write(void *opaque, hwaddr addr,
2110
                          uint64_t data, unsigned size)
2111
153k
{
2112
153k
    VGACommonState *s = opaque;
2113
2114
153k
    vga_mem_writeb(s, addr, data);
2115
153k
}
2116
2117
const MemoryRegionOps vga_mem_ops = {
2118
    .read = vga_mem_read,
2119
    .write = vga_mem_write,
2120
    .endianness = DEVICE_LITTLE_ENDIAN,
2121
    .impl = {
2122
        .min_access_size = 1,
2123
        .max_access_size = 1,
2124
    },
2125
};
2126
2127
static int vga_common_post_load(void *opaque, int version_id)
2128
0
{
2129
0
    VGACommonState *s = opaque;
2130
2131
    /* force refresh */
2132
0
    s->graphic_mode = -1;
2133
0
    vbe_update_vgaregs(s);
2134
0
    vga_update_memory_access(s);
2135
0
    return 0;
2136
0
}
2137
2138
static bool vga_endian_state_needed(void *opaque)
2139
0
{
2140
0
    VGACommonState *s = opaque;
2141
2142
    /*
2143
     * Only send the endian state if it's different from the
2144
     * default one, thus ensuring backward compatibility for
2145
     * migration of the common case
2146
     */
2147
0
    return s->default_endian_fb != s->big_endian_fb;
2148
0
}
2149
2150
static const VMStateDescription vmstate_vga_endian = {
2151
    .name = "vga.endian",
2152
    .version_id = 1,
2153
    .minimum_version_id = 1,
2154
    .needed = vga_endian_state_needed,
2155
    .fields = (const VMStateField[]) {
2156
        VMSTATE_BOOL(big_endian_fb, VGACommonState),
2157
        VMSTATE_END_OF_LIST()
2158
    }
2159
};
2160
2161
const VMStateDescription vmstate_vga_common = {
2162
    .name = "vga",
2163
    .version_id = 2,
2164
    .minimum_version_id = 2,
2165
    .post_load = vga_common_post_load,
2166
    .fields = (const VMStateField[]) {
2167
        VMSTATE_UINT32(latch, VGACommonState),
2168
        VMSTATE_UINT8(sr_index, VGACommonState),
2169
        VMSTATE_PARTIAL_BUFFER(sr, VGACommonState, 8),
2170
        VMSTATE_UINT8(gr_index, VGACommonState),
2171
        VMSTATE_PARTIAL_BUFFER(gr, VGACommonState, 16),
2172
        VMSTATE_UINT8(ar_index, VGACommonState),
2173
        VMSTATE_BUFFER(ar, VGACommonState),
2174
        VMSTATE_INT32(ar_flip_flop, VGACommonState),
2175
        VMSTATE_UINT8(cr_index, VGACommonState),
2176
        VMSTATE_BUFFER(cr, VGACommonState),
2177
        VMSTATE_UINT8(msr, VGACommonState),
2178
        VMSTATE_UINT8(fcr, VGACommonState),
2179
        VMSTATE_UINT8(st00, VGACommonState),
2180
        VMSTATE_UINT8(st01, VGACommonState),
2181
2182
        VMSTATE_UINT8(dac_state, VGACommonState),
2183
        VMSTATE_UINT8(dac_sub_index, VGACommonState),
2184
        VMSTATE_UINT8(dac_read_index, VGACommonState),
2185
        VMSTATE_UINT8(dac_write_index, VGACommonState),
2186
        VMSTATE_BUFFER(dac_cache, VGACommonState),
2187
        VMSTATE_BUFFER(palette, VGACommonState),
2188
2189
        VMSTATE_INT32(bank_offset, VGACommonState),
2190
        VMSTATE_UINT8_EQUAL(is_vbe_vmstate, VGACommonState),
2191
        VMSTATE_UINT16(vbe_index, VGACommonState),
2192
        VMSTATE_UINT16_ARRAY(vbe_regs, VGACommonState, VBE_DISPI_INDEX_NB),
2193
        VMSTATE_UINT32(vbe_start_addr, VGACommonState),
2194
        VMSTATE_UINT32(vbe_line_offset, VGACommonState),
2195
        VMSTATE_UINT32(vbe_bank_mask, VGACommonState),
2196
        VMSTATE_END_OF_LIST()
2197
    },
2198
    .subsections = (const VMStateDescription * const []) {
2199
        &vmstate_vga_endian,
2200
        NULL
2201
    }
2202
};
2203
2204
static const GraphicHwOps vga_ops = {
2205
    .invalidate  = vga_invalidate_display,
2206
    .gfx_update  = vga_update_display,
2207
    .text_update = vga_update_text,
2208
};
2209
2210
static inline uint32_t uint_clamp(uint32_t val, uint32_t vmin, uint32_t vmax)
2211
9
{
2212
9
    if (val < vmin) {
2213
0
        return vmin;
2214
0
    }
2215
9
    if (val > vmax) {
2216
0
        return vmax;
2217
0
    }
2218
9
    return val;
2219
9
}
2220
2221
bool vga_common_init(VGACommonState *s, Object *obj, Error **errp)
2222
9
{
2223
9
    int i, j, v, b;
2224
9
    Error *local_err = NULL;
2225
2226
2.31k
    for(i = 0;i < 256; i++) {
2227
2.30k
        v = 0;
2228
20.7k
        for(j = 0; j < 8; j++) {
2229
18.4k
            v |= ((i >> j) & 1) << (j * 4);
2230
18.4k
        }
2231
2.30k
        expand4[i] = v;
2232
2233
2.30k
        v = 0;
2234
11.5k
        for(j = 0; j < 4; j++) {
2235
9.21k
            v |= ((i >> (2 * j)) & 3) << (j * 4);
2236
9.21k
        }
2237
2.30k
        expand2[i] = v;
2238
2.30k
    }
2239
153
    for(i = 0; i < 16; i++) {
2240
144
        v = 0;
2241
720
        for(j = 0; j < 4; j++) {
2242
576
            b = ((i >> j) & 1);
2243
576
            v |= b << (2 * j);
2244
576
            v |= b << (2 * j + 1);
2245
576
        }
2246
144
        expand4to8[i] = v;
2247
144
    }
2248
2249
9
    s->vram_size_mb = uint_clamp(s->vram_size_mb, 1, 512);
2250
9
    s->vram_size_mb = pow2ceil(s->vram_size_mb);
2251
9
    s->vram_size = s->vram_size_mb * MiB;
2252
2253
9
    if (!s->vbe_size) {
2254
9
        s->vbe_size = s->vram_size;
2255
9
    }
2256
9
    s->vbe_size_mask = s->vbe_size - 1;
2257
2258
9
    s->is_vbe_vmstate = 1;
2259
2260
9
    if (s->global_vmstate && qemu_ram_block_by_name("vga.vram")) {
2261
0
        error_setg(errp, "Only one global VGA device can be used at a time");
2262
0
        return false;
2263
0
    }
2264
2265
9
    memory_region_init_ram_flags_nomigrate(&s->vram, obj, "vga.vram",
2266
9
                                           s->vram_size, 0, &local_err);
2267
9
    if (local_err) {
2268
0
        error_propagate(errp, local_err);
2269
0
        return false;
2270
0
    }
2271
9
    vmstate_register_ram(&s->vram, s->global_vmstate ? NULL : DEVICE(obj));
2272
9
    xen_register_framebuffer(&s->vram);
2273
9
    s->vram_ptr = memory_region_get_ram_ptr(&s->vram);
2274
9
    s->get_bpp = vga_get_bpp;
2275
9
    s->get_params = vga_get_params;
2276
9
    s->get_resolution = vga_get_resolution;
2277
9
    s->hw_ops = &vga_ops;
2278
9
    switch (vga_retrace_method) {
2279
9
    case VGA_RETRACE_DUMB:
2280
9
        s->retrace = vga_dumb_retrace;
2281
9
        s->update_retrace_info = vga_dumb_update_retrace_info;
2282
9
        break;
2283
2284
0
    case VGA_RETRACE_PRECISE:
2285
0
        s->retrace = vga_precise_retrace;
2286
0
        s->update_retrace_info = vga_precise_update_retrace_info;
2287
0
        break;
2288
9
    }
2289
2290
    /*
2291
     * Set default fb endian based on target, could probably be turned
2292
     * into a device attribute set by the machine/platform to remove
2293
     * all target endian dependencies from this file.
2294
     */
2295
9
    s->default_endian_fb = target_big_endian();
2296
9
    s->big_endian_fb = s->default_endian_fb;
2297
2298
9
    vga_dirty_log_start(s);
2299
2300
9
    return true;
2301
9
}
2302
2303
static const MemoryRegionPortio vga_portio_list[] = {
2304
    { 0x04,  2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3b4 */
2305
    { 0x0a,  1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3ba */
2306
    { 0x10, 16, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3c0 */
2307
    { 0x24,  2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3d4 */
2308
    { 0x2a,  1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3da */
2309
    PORTIO_END_OF_LIST(),
2310
};
2311
2312
static const MemoryRegionPortio vbe_portio_list_x86[] = {
2313
    { 0, 1, 2, .read = vbe_ioport_read_index, .write = vbe_ioport_write_index },
2314
    { 1, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
2315
    { 2, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
2316
    PORTIO_END_OF_LIST(),
2317
};
2318
2319
static const MemoryRegionPortio vbe_portio_list_no_x86[] = {
2320
    { 0, 1, 2, .read = vbe_ioport_read_index, .write = vbe_ioport_write_index },
2321
    { 2, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
2322
    PORTIO_END_OF_LIST(),
2323
};
2324
2325
/* Used by both ISA and PCI */
2326
MemoryRegion *vga_init_io(VGACommonState *s, Object *obj,
2327
                          const MemoryRegionPortio **vga_ports,
2328
                          const MemoryRegionPortio **vbe_ports)
2329
7
{
2330
7
    MemoryRegion *vga_mem;
2331
7
    MachineState *ms = MACHINE(qdev_get_machine());
2332
2333
    /*
2334
     * We unfortunately need two VBE lists since non-x86 machines might
2335
     * not be able to do 16-bit accesses at unaligned addresses (0x1cf)
2336
     */
2337
7
    if (object_dynamic_cast(OBJECT(ms), TYPE_X86_MACHINE)) {
2338
7
        *vbe_ports = vbe_portio_list_x86;
2339
7
    } else {
2340
0
        *vbe_ports = vbe_portio_list_no_x86;
2341
0
    }
2342
2343
7
    *vga_ports = vga_portio_list;
2344
2345
7
    vga_mem = g_malloc(sizeof(*vga_mem));
2346
7
    memory_region_init_io(vga_mem, obj, &vga_mem_ops, s,
2347
7
                          "vga-lowmem", 0x20000);
2348
7
    memory_region_set_flush_coalesced(vga_mem);
2349
2350
7
    return vga_mem;
2351
7
}
2352
2353
void vga_init(VGACommonState *s, Object *obj, MemoryRegion *address_space,
2354
              MemoryRegion *address_space_io, bool init_vga_ports)
2355
7
{
2356
7
    MemoryRegion *vga_io_memory;
2357
7
    const MemoryRegionPortio *vga_ports, *vbe_ports;
2358
2359
7
    qemu_register_reset(vga_reset, s);
2360
2361
7
    s->bank_offset = 0;
2362
2363
7
    s->legacy_address_space = address_space;
2364
2365
7
    vga_io_memory = vga_init_io(s, obj, &vga_ports, &vbe_ports);
2366
7
    memory_region_add_subregion_overlap(address_space,
2367
7
                                        0x000a0000,
2368
7
                                        vga_io_memory,
2369
7
                                        1);
2370
7
    memory_region_set_coalescing(vga_io_memory);
2371
7
    if (init_vga_ports) {
2372
7
        portio_list_init(&s->vga_port_list, obj, vga_ports, s, "vga");
2373
7
        portio_list_set_flush_coalesced(&s->vga_port_list);
2374
7
        portio_list_add(&s->vga_port_list, address_space_io, 0x3b0);
2375
7
    }
2376
7
    if (vbe_ports) {
2377
7
        portio_list_init(&s->vbe_port_list, obj, vbe_ports, s, "vbe");
2378
7
        portio_list_add(&s->vbe_port_list, address_space_io, 0x1ce);
2379
7
    }
2380
7
}