Coverage Report

Created: 2026-10-31 08:12

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
5.39M
#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
6.78M
{
135
6.78M
    return s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED;
136
6.78M
}
137
138
static inline uint8_t sr(VGACommonState *s, int idx)
139
6.56M
{
140
6.56M
    return vbe_enabled(s) ? s->sr_vbe[idx] : s->sr[idx];
141
6.56M
}
142
143
static void vga_update_memory_access(VGACommonState *s)
144
122k
{
145
122k
    hwaddr base, offset, size;
146
147
122k
    if (s->legacy_address_space == NULL) {
148
23.7k
        return;
149
23.7k
    }
150
151
98.7k
    if (s->has_chain4_alias) {
152
60.6k
        memory_region_del_subregion(s->legacy_address_space, &s->chain4_alias);
153
60.6k
        object_unparent(OBJECT(&s->chain4_alias));
154
60.6k
        s->has_chain4_alias = false;
155
60.6k
        s->plane_updated = 0xf;
156
60.6k
    }
157
98.7k
    if ((sr(s, VGA_SEQ_PLANE_WRITE) & VGA_SR02_ALL_PLANES) ==
158
98.7k
        VGA_SR02_ALL_PLANES && sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
159
60.6k
        offset = 0;
160
60.6k
        switch ((s->gr[VGA_GFX_MISC] >> 2) & 3) {
161
518
        case 0:
162
518
            base = 0xa0000;
163
518
            size = 0x20000;
164
518
            break;
165
59.6k
        case 1:
166
59.6k
            base = 0xa0000;
167
59.6k
            size = 0x10000;
168
59.6k
            offset = s->bank_offset;
169
59.6k
            break;
170
326
        case 2:
171
326
            base = 0xb0000;
172
326
            size = 0x8000;
173
326
            break;
174
194
        case 3:
175
194
        default:
176
194
            base = 0xb8000;
177
194
            size = 0x8000;
178
194
            break;
179
60.6k
        }
180
60.6k
        assert(offset + size <= s->vram_size);
181
60.6k
        memory_region_init_alias(&s->chain4_alias, memory_region_owner(&s->vram),
182
60.6k
                                 "vga.chain4", &s->vram, offset, size);
183
60.6k
        memory_region_add_subregion_overlap(s->legacy_address_space, base,
184
60.6k
                                            &s->chain4_alias, 2);
185
60.6k
        s->has_chain4_alias = true;
186
60.6k
    }
187
98.7k
}
188
189
static void vga_dumb_update_retrace_info(VGACommonState *s)
190
21.1k
{
191
21.1k
    (void) s;
192
21.1k
}
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.45k
{
317
2.45k
    return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
318
2.45k
}
319
320
int vga_ioport_invalid(VGACommonState *s, uint32_t addr)
321
865k
{
322
865k
    if (s->msr & VGA_MIS_COLOR) {
323
        /* Color */
324
159k
        return (addr >= 0x3b0 && addr <= 0x3bf);
325
706k
    } else {
326
        /* Monochrome */
327
706k
        return (addr >= 0x3d0 && addr <= 0x3df);
328
706k
    }
329
865k
}
330
331
uint32_t vga_ioport_read(void *opaque, uint32_t addr)
332
17.6k
{
333
17.6k
    VGACommonState *s = opaque;
334
17.6k
    int val, index;
335
336
17.6k
    if (vga_ioport_invalid(s, addr)) {
337
2.61k
        val = 0xff;
338
15.0k
    } else {
339
15.0k
        switch(addr) {
340
645
        case VGA_ATT_W:
341
645
            if (s->ar_flip_flop == 0) {
342
398
                val = s->ar_index;
343
398
            } else {
344
247
                val = 0;
345
247
            }
346
645
            break;
347
1.48k
        case VGA_ATT_R:
348
1.48k
            index = s->ar_index & 0x1f;
349
1.48k
            if (index < VGA_ATT_C) {
350
934
                val = s->ar[index];
351
934
            } else {
352
549
                val = 0;
353
549
            }
354
1.48k
            break;
355
630
        case VGA_MIS_W:
356
630
            val = s->st00;
357
630
            break;
358
437
        case VGA_SEQ_I:
359
437
            val = s->sr_index;
360
437
            break;
361
315
        case VGA_SEQ_D:
362
315
            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
315
            break;
367
326
        case VGA_PEL_IR:
368
326
            val = s->dac_state;
369
326
            break;
370
244
        case VGA_PEL_IW:
371
244
            val = s->dac_write_index;
372
244
            break;
373
2.74k
        case VGA_PEL_D:
374
2.74k
            val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
375
2.74k
            if (++s->dac_sub_index == 3) {
376
874
                s->dac_sub_index = 0;
377
874
                s->dac_read_index++;
378
874
            }
379
2.74k
            break;
380
2.54k
        case VGA_FTC_R:
381
2.54k
            val = s->fcr;
382
2.54k
            break;
383
395
        case VGA_MIS_R:
384
395
            val = s->msr;
385
395
            break;
386
493
        case VGA_GFX_I:
387
493
            val = s->gr_index;
388
493
            break;
389
519
        case VGA_GFX_D:
390
519
            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
519
            break;
395
503
        case VGA_CRT_IM:
396
868
        case VGA_CRT_IC:
397
868
            val = s->cr_index;
398
868
            break;
399
553
        case VGA_CRT_DM:
400
803
        case VGA_CRT_DC:
401
803
            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
803
            break;
406
568
        case VGA_IS1_RM:
407
1.00k
        case VGA_IS1_RC:
408
            /* just toggle to fool polling */
409
1.00k
            val = s->st01 = s->retrace(s);
410
1.00k
            s->ar_flip_flop = 0;
411
1.00k
            break;
412
1.55k
        default:
413
1.55k
            val = 0x00;
414
1.55k
            break;
415
15.0k
        }
416
15.0k
    }
417
17.6k
    trace_vga_std_read_io(addr, val);
418
17.6k
    return val;
419
17.6k
}
420
421
void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
422
53.0k
{
423
53.0k
    VGACommonState *s = opaque;
424
53.0k
    int index;
425
426
    /* check port range access depending on color/monochrome mode */
427
53.0k
    if (vga_ioport_invalid(s, addr)) {
428
2.88k
        return;
429
2.88k
    }
430
50.1k
    trace_vga_std_write_io(addr, val);
431
432
50.1k
    switch(addr) {
433
14.8k
    case VGA_ATT_W:
434
14.8k
        if (s->ar_flip_flop == 0) {
435
7.49k
            val &= 0x3f;
436
7.49k
            s->ar_index = val;
437
7.49k
        } else {
438
7.33k
            index = s->ar_index & 0x1f;
439
7.33k
            switch(index) {
440
5.78k
            case VGA_ATC_PALETTE0 ... VGA_ATC_PALETTEF:
441
5.78k
                s->ar[index] = val & 0x3f;
442
5.78k
                break;
443
200
            case VGA_ATC_MODE:
444
200
                s->ar[index] = val & ~0x10;
445
200
                break;
446
282
            case VGA_ATC_OVERSCAN:
447
282
                s->ar[index] = val;
448
282
                break;
449
221
            case VGA_ATC_PLANE_ENABLE:
450
221
                s->ar[index] = val & ~0xc0;
451
221
                break;
452
226
            case VGA_ATC_PEL:
453
226
                s->ar[index] = val & ~0xf0;
454
226
                break;
455
199
            case VGA_ATC_COLOR_PAGE:
456
199
                s->ar[index] = val & ~0xf0;
457
199
                break;
458
419
            default:
459
419
                break;
460
7.33k
            }
461
7.33k
        }
462
14.8k
        s->ar_flip_flop ^= 1;
463
14.8k
        break;
464
1.24k
    case VGA_MIS_W:
465
1.24k
        s->msr = val & ~0x10;
466
1.24k
        s->update_retrace_info(s);
467
1.24k
        break;
468
3.33k
    case VGA_SEQ_I:
469
3.33k
        s->sr_index = val & 7;
470
3.33k
        break;
471
3.22k
    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
3.22k
        s->sr[s->sr_index] = val & sr_mask[s->sr_index];
476
3.22k
        if (s->sr_index == VGA_SEQ_CLOCK_MODE) {
477
775
            s->update_retrace_info(s);
478
775
        }
479
3.22k
        vga_update_memory_access(s);
480
3.22k
        break;
481
1.33k
    case VGA_PEL_IR:
482
1.33k
        s->dac_read_index = val;
483
1.33k
        s->dac_sub_index = 0;
484
1.33k
        s->dac_state = 3;
485
1.33k
        break;
486
844
    case VGA_PEL_IW:
487
844
        s->dac_write_index = val;
488
844
        s->dac_sub_index = 0;
489
844
        s->dac_state = 0;
490
844
        break;
491
1.13k
    case VGA_PEL_D:
492
1.13k
        s->dac_cache[s->dac_sub_index] = val;
493
1.13k
        if (++s->dac_sub_index == 3) {
494
206
            memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
495
206
            s->dac_sub_index = 0;
496
206
            s->dac_write_index++;
497
206
        }
498
1.13k
        break;
499
728
    case VGA_GFX_I:
500
728
        s->gr_index = val & 0x0f;
501
728
        break;
502
757
    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
757
        s->gr[s->gr_index] = val & gr_mask[s->gr_index];
507
757
        vbe_update_vgaregs(s);
508
757
        vga_update_memory_access(s);
509
757
        break;
510
3.11k
    case VGA_CRT_IM:
511
4.05k
    case VGA_CRT_IC:
512
4.05k
        s->cr_index = val;
513
4.05k
        break;
514
3.29k
    case VGA_CRT_DM:
515
4.25k
    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
4.25k
        if ((s->cr[VGA_CRTC_V_SYNC_END] & VGA_CR11_LOCK_CR0_CR7) &&
521
904
            s->cr_index <= VGA_CRTC_OVERFLOW) {
522
            /* can always write bit 4 of CR7 */
523
419
            if (s->cr_index == VGA_CRTC_OVERFLOW) {
524
204
                s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x10) |
525
204
                    (val & 0x10);
526
204
                vbe_update_vgaregs(s);
527
204
            }
528
419
            return;
529
419
        }
530
3.83k
        s->cr[s->cr_index] = val;
531
3.83k
        vbe_update_vgaregs(s);
532
533
3.83k
        switch(s->cr_index) {
534
1.00k
        case VGA_CRTC_H_TOTAL:
535
1.21k
        case VGA_CRTC_H_SYNC_START:
536
1.40k
        case VGA_CRTC_H_SYNC_END:
537
2.25k
        case VGA_CRTC_V_TOTAL:
538
2.51k
        case VGA_CRTC_OVERFLOW:
539
3.06k
        case VGA_CRTC_V_SYNC_END:
540
3.35k
        case VGA_CRTC_MODE:
541
3.35k
            s->update_retrace_info(s);
542
3.35k
            break;
543
3.83k
        }
544
3.83k
        break;
545
3.83k
    case VGA_IS1_RM:
546
634
    case VGA_IS1_RC:
547
634
        s->fcr = val & 0x10;
548
634
        break;
549
50.1k
    }
550
50.1k
}
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
106k
{
561
106k
    uint16_t *r = s->vbe_regs;
562
106k
    uint32_t bits, linelength, maxy, offset;
563
564
106k
    if (!vbe_enabled(s)) {
565
        /* vbe is turned off -- nothing to do */
566
36.4k
        return;
567
36.4k
    }
568
569
    /* check depth */
570
69.8k
    switch (r[VBE_DISPI_INDEX_BPP]) {
571
311
    case 4:
572
41.0k
    case 8:
573
41.5k
    case 16:
574
43.9k
    case 24:
575
44.1k
    case 32:
576
44.1k
        bits = r[VBE_DISPI_INDEX_BPP];
577
44.1k
        break;
578
211
    case 15:
579
211
        bits = 16;
580
211
        break;
581
25.5k
    default:
582
25.5k
        bits = r[VBE_DISPI_INDEX_BPP] = 8;
583
25.5k
        break;
584
69.8k
    }
585
586
    /* check width */
587
69.8k
    r[VBE_DISPI_INDEX_XRES] &= ~7u;
588
69.8k
    if (r[VBE_DISPI_INDEX_XRES] == 0) {
589
781
        r[VBE_DISPI_INDEX_XRES] = 8;
590
781
    }
591
69.8k
    if (r[VBE_DISPI_INDEX_XRES] > VBE_DISPI_MAX_XRES) {
592
2.01k
        r[VBE_DISPI_INDEX_XRES] = VBE_DISPI_MAX_XRES;
593
2.01k
    }
594
69.8k
    r[VBE_DISPI_INDEX_VIRT_WIDTH] &= ~7u;
595
69.8k
    if (r[VBE_DISPI_INDEX_VIRT_WIDTH] > VBE_DISPI_MAX_XRES) {
596
3.30k
        r[VBE_DISPI_INDEX_VIRT_WIDTH] = VBE_DISPI_MAX_XRES;
597
3.30k
    }
598
69.8k
    if (r[VBE_DISPI_INDEX_VIRT_WIDTH] < r[VBE_DISPI_INDEX_XRES]) {
599
13.9k
        r[VBE_DISPI_INDEX_VIRT_WIDTH] = r[VBE_DISPI_INDEX_XRES];
600
13.9k
    }
601
602
    /* check height */
603
69.8k
    linelength = r[VBE_DISPI_INDEX_VIRT_WIDTH] * bits / 8;
604
69.8k
    maxy = s->vbe_size / linelength;
605
69.8k
    if (r[VBE_DISPI_INDEX_YRES] == 0) {
606
788
        r[VBE_DISPI_INDEX_YRES] = 1;
607
788
    }
608
69.8k
    if (r[VBE_DISPI_INDEX_YRES] > VBE_DISPI_MAX_YRES) {
609
3.90k
        r[VBE_DISPI_INDEX_YRES] = VBE_DISPI_MAX_YRES;
610
3.90k
    }
611
69.8k
    if (r[VBE_DISPI_INDEX_YRES] > maxy) {
612
3.78k
        r[VBE_DISPI_INDEX_YRES] = maxy;
613
3.78k
    }
614
615
    /* check offset */
616
69.8k
    if (r[VBE_DISPI_INDEX_X_OFFSET] > VBE_DISPI_MAX_XRES) {
617
1.00k
        r[VBE_DISPI_INDEX_X_OFFSET] = VBE_DISPI_MAX_XRES;
618
1.00k
    }
619
69.8k
    if (r[VBE_DISPI_INDEX_Y_OFFSET] > VBE_DISPI_MAX_YRES) {
620
612
        r[VBE_DISPI_INDEX_Y_OFFSET] = VBE_DISPI_MAX_YRES;
621
612
    }
622
69.8k
    offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
623
69.8k
    offset += r[VBE_DISPI_INDEX_Y_OFFSET] * linelength;
624
69.8k
    if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
625
1.16k
        r[VBE_DISPI_INDEX_Y_OFFSET] = 0;
626
1.16k
        offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
627
1.16k
        if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
628
416
            r[VBE_DISPI_INDEX_X_OFFSET] = 0;
629
416
            offset = 0;
630
416
        }
631
1.16k
    }
632
633
    /* update vga state */
634
69.8k
    r[VBE_DISPI_INDEX_VIRT_HEIGHT] = maxy;
635
69.8k
    s->vbe_line_offset = linelength;
636
69.8k
    s->vbe_start_addr  = offset / 4;
637
69.8k
}
638
639
/* we initialize the VGA graphic mode */
640
static void vbe_update_vgaregs(VGACommonState *s)
641
111k
{
642
111k
    int h, shift_control;
643
644
111k
    if (!vbe_enabled(s)) {
645
        /* vbe is turned off -- nothing to do */
646
39.9k
        return;
647
39.9k
    }
648
649
    /* graphic mode + memory map 1 */
650
71.0k
    s->gr[VGA_GFX_MISC] = (s->gr[VGA_GFX_MISC] & ~0x0c) | 0x04 |
651
71.0k
        VGA_GR06_GRAPHICS_MODE;
652
71.0k
    s->cr[VGA_CRTC_MODE] |= 3; /* no CGA modes */
653
71.0k
    s->cr[VGA_CRTC_OFFSET] = s->vbe_line_offset >> 3;
654
    /* width */
655
71.0k
    s->cr[VGA_CRTC_H_DISP] =
656
71.0k
        (s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
657
    /* height (only meaningful if < 1024) */
658
71.0k
    h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
659
71.0k
    s->cr[VGA_CRTC_V_DISP_END] = h;
660
71.0k
    s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x42) |
661
71.0k
        ((h >> 7) & 0x02) | ((h >> 3) & 0x40);
662
    /* line compare to 1023 */
663
71.0k
    s->cr[VGA_CRTC_LINE_COMPARE] = 0xff;
664
71.0k
    s->cr[VGA_CRTC_OVERFLOW] |= 0x10;
665
71.0k
    s->cr[VGA_CRTC_MAX_SCAN] |= 0x40;
666
667
71.0k
    if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
668
314
        shift_control = 0;
669
314
        s->sr_vbe[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
670
70.7k
    } else {
671
70.7k
        shift_control = 2;
672
        /* set chain 4 mode */
673
70.7k
        s->sr_vbe[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
674
        /* activate all planes */
675
70.7k
        s->sr_vbe[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
676
70.7k
    }
677
71.0k
    s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
678
71.0k
        (shift_control << 5);
679
71.0k
    s->cr[VGA_CRTC_MAX_SCAN] &= ~0x9f; /* no double scan */
680
71.0k
}
681
682
static uint32_t vbe_ioport_read_index(void *opaque, uint32_t addr)
683
316
{
684
316
    VGACommonState *s = opaque;
685
316
    return s->vbe_index;
686
316
}
687
688
uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
689
6.75k
{
690
6.75k
    VGACommonState *s = opaque;
691
6.75k
    uint32_t val;
692
693
6.75k
    if (s->vbe_index < VBE_DISPI_INDEX_NB) {
694
6.27k
        if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_GETCAPS) {
695
1.59k
            switch(s->vbe_index) {
696
                /* XXX: do not hardcode ? */
697
89
            case VBE_DISPI_INDEX_XRES:
698
89
                val = VBE_DISPI_MAX_XRES;
699
89
                break;
700
255
            case VBE_DISPI_INDEX_YRES:
701
255
                val = VBE_DISPI_MAX_YRES;
702
255
                break;
703
274
            case VBE_DISPI_INDEX_BPP:
704
274
                val = VBE_DISPI_MAX_BPP;
705
274
                break;
706
981
            default:
707
981
                val = s->vbe_regs[s->vbe_index];
708
981
                break;
709
1.59k
            }
710
4.67k
        } else {
711
4.67k
            val = s->vbe_regs[s->vbe_index];
712
4.67k
        }
713
6.27k
    } else if (s->vbe_index == VBE_DISPI_INDEX_VIDEO_MEMORY_64K) {
714
270
        val = s->vbe_size / (64 * KiB);
715
270
    } else {
716
210
        val = 0;
717
210
    }
718
6.75k
    trace_vga_vbe_read(s->vbe_index, val);
719
6.75k
    return val;
720
6.75k
}
721
722
void vbe_ioport_write_index(void *opaque, uint32_t addr, uint32_t val)
723
199k
{
724
199k
    VGACommonState *s = opaque;
725
199k
    s->vbe_index = val;
726
199k
}
727
728
void vbe_ioport_write_data(void *opaque, uint32_t addr, uint32_t val)
729
199k
{
730
199k
    VGACommonState *s = opaque;
731
732
199k
    if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
733
199k
        trace_vga_vbe_write(s->vbe_index, val);
734
199k
        switch(s->vbe_index) {
735
7.33k
        case VBE_DISPI_INDEX_ID:
736
7.33k
            if (val == VBE_DISPI_ID0 ||
737
7.13k
                val == VBE_DISPI_ID1 ||
738
6.94k
                val == VBE_DISPI_ID2 ||
739
6.74k
                val == VBE_DISPI_ID3 ||
740
6.38k
                val == VBE_DISPI_ID4 ||
741
6.16k
                val == VBE_DISPI_ID5) {
742
1.36k
                s->vbe_regs[s->vbe_index] = val;
743
1.36k
            }
744
7.33k
            break;
745
13.7k
        case VBE_DISPI_INDEX_XRES:
746
34.9k
        case VBE_DISPI_INDEX_YRES:
747
67.2k
        case VBE_DISPI_INDEX_BPP:
748
94.9k
        case VBE_DISPI_INDEX_VIRT_WIDTH:
749
100k
        case VBE_DISPI_INDEX_X_OFFSET:
750
103k
        case VBE_DISPI_INDEX_Y_OFFSET:
751
103k
            s->vbe_regs[s->vbe_index] = val;
752
103k
            vbe_fixup_regs(s);
753
103k
            vbe_update_vgaregs(s);
754
103k
            break;
755
32.2k
        case VBE_DISPI_INDEX_BANK:
756
32.2k
            val &= s->vbe_bank_mask;
757
32.2k
            s->vbe_regs[s->vbe_index] = val;
758
32.2k
            s->bank_offset = (val << 16);
759
32.2k
            vga_update_memory_access(s);
760
32.2k
            break;
761
35.4k
        case VBE_DISPI_INDEX_ENABLE:
762
35.4k
            if ((val & VBE_DISPI_ENABLED) &&
763
26.7k
                !(s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) {
764
765
3.06k
                s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] = 0;
766
3.06k
                s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET] = 0;
767
3.06k
                s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET] = 0;
768
3.06k
                s->vbe_regs[VBE_DISPI_INDEX_ENABLE] |= VBE_DISPI_ENABLED;
769
3.06k
                vbe_fixup_regs(s);
770
3.06k
                vbe_update_vgaregs(s);
771
772
                /* clear the screen */
773
3.06k
                if (!(val & VBE_DISPI_NOCLEARMEM)) {
774
2.13k
                    memset(s->vram_ptr, 0,
775
2.13k
                           s->vbe_regs[VBE_DISPI_INDEX_YRES] * s->vbe_line_offset);
776
2.13k
                }
777
32.3k
            } else {
778
32.3k
                s->bank_offset = 0;
779
32.3k
            }
780
35.4k
            s->dac_8bit = (val & VBE_DISPI_8BIT_DAC) > 0;
781
35.4k
            s->vbe_regs[s->vbe_index] = val;
782
35.4k
            vga_update_memory_access(s);
783
35.4k
            break;
784
21.5k
        default:
785
21.5k
            break;
786
199k
        }
787
199k
    }
788
199k
}
789
790
/* called for accesses between 0xa0000 and 0xc0000 */
791
uint32_t vga_mem_readb(VGACommonState *s, hwaddr addr)
792
5.39M
{
793
5.39M
    int memory_map_mode, plane;
794
5.39M
    uint32_t ret;
795
796
    /* convert to VGA memory offset */
797
5.39M
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
798
5.39M
    addr &= 0x1ffff;
799
5.39M
    switch(memory_map_mode) {
800
5.39M
    case 0:
801
5.39M
        break;
802
2.52k
    case 1:
803
2.52k
        if (addr >= 0x10000)
804
1.57k
            return 0xff;
805
942
        addr += s->bank_offset;
806
942
        break;
807
960
    case 2:
808
960
        addr -= 0x10000;
809
960
        if (addr >= 0x8000)
810
534
            return 0xff;
811
426
        break;
812
426
    default:
813
1.38k
    case 3:
814
1.38k
        addr -= 0x18000;
815
1.38k
        if (addr >= 0x8000)
816
720
            return 0xff;
817
668
        break;
818
5.39M
    }
819
820
5.39M
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
821
        /* chain4 mode */
822
627
        plane = addr & 3;
823
627
        addr &= ~3;
824
5.39M
    } else if (s->gr[VGA_GFX_MODE] & VGA_GR05_HOST_ODD_EVEN) {
825
        /* odd/even mode (aka text mode mapping) */
826
1.16k
        plane = (s->gr[VGA_GFX_PLANE_READ] & 2) | (addr & 1);
827
5.39M
    } else {
828
        /* standard VGA latched access */
829
5.39M
        plane = s->gr[VGA_GFX_PLANE_READ];
830
5.39M
    }
831
832
5.39M
    if (s->gr[VGA_GFX_MISC] & VGA_GR06_CHAIN_ODD_EVEN) {
833
1.49k
        addr &= ~1;
834
1.49k
    }
835
836
    /* Doubleword/word mode.  See comment in vga_mem_writeb */
837
5.39M
    if (s->cr[VGA_CRTC_UNDERLINE] & VGA_CR14_DW) {
838
392
        addr >>= 2;
839
5.39M
    } else if ((s->gr[VGA_GFX_MODE] & VGA_GR05_HOST_ODD_EVEN) &&
840
1.16k
               (s->cr[VGA_CRTC_MODE] & VGA_CR17_WORD_BYTE) == 0) {
841
898
        addr >>= 1;
842
898
    }
843
844
5.39M
    if (addr * sizeof(uint32_t) >= s->vram_size) {
845
273
        return 0xff;
846
273
    }
847
848
5.39M
    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
627
        return s->vram_ptr[(addr << 2) | plane];
853
627
    }
854
855
5.39M
    s->latch = ((uint32_t *)s->vram_ptr)[addr];
856
5.39M
    if (!(s->gr[VGA_GFX_MODE] & 0x08)) {
857
        /* read mode 0 */
858
5.39M
        ret = GET_PLANE(s->latch, plane);
859
5.39M
    } else {
860
        /* read mode 1 */
861
2.11k
        ret = (s->latch ^ mask16[s->gr[VGA_GFX_COMPARE_VALUE]]) &
862
2.11k
            mask16[s->gr[VGA_GFX_COMPARE_MASK]];
863
2.11k
        ret |= ret >> 16;
864
2.11k
        ret |= ret >> 8;
865
2.11k
        ret = (~ret) & 0xff;
866
2.11k
    }
867
868
5.39M
    return ret;
869
5.39M
}
870
871
/* called for accesses between 0xa0000 and 0xc0000 */
872
void vga_mem_writeb(VGACommonState *s, hwaddr addr, uint32_t val)
873
229k
{
874
229k
    int memory_map_mode, write_mode, b, func_select, mask;
875
229k
    uint32_t write_mask, bit_mask, set_mask;
876
229k
    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
229k
    memory_map_mode = (s->gr[VGA_GFX_MISC] >> 2) & 3;
883
229k
    addr &= 0x1ffff;
884
229k
    switch(memory_map_mode) {
885
196k
    case 0:
886
196k
        break;
887
28.4k
    case 1:
888
28.4k
        if (addr >= 0x10000)
889
23.4k
            return;
890
5.00k
        addr += s->bank_offset;
891
5.00k
        break;
892
2.02k
    case 2:
893
2.02k
        addr -= 0x10000;
894
2.02k
        if (addr >= 0x8000)
895
1.05k
            return;
896
967
        break;
897
967
    default:
898
2.10k
    case 3:
899
2.10k
        addr -= 0x18000;
900
2.10k
        if (addr >= 0x8000)
901
1.39k
            return;
902
704
        break;
903
229k
    }
904
905
203k
    mask = sr(s, VGA_SEQ_PLANE_WRITE);
906
203k
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
907
        /* chain 4 mode : simplest access */
908
5.66k
        plane = addr & 3;
909
5.66k
        mask &= (1 << plane);
910
5.66k
        addr &= ~3;
911
197k
    } else {
912
197k
        if ((sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_SEQ_MODE) == 0) {
913
197k
            mask &= (addr & 1) ? 0x0a : 0x05;
914
197k
        }
915
197k
        if (s->gr[VGA_GFX_MISC] & VGA_GR06_CHAIN_ODD_EVEN) {
916
1.31k
            addr &= ~1;
917
1.31k
        }
918
197k
    }
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
203k
    if (s->cr[VGA_CRTC_UNDERLINE] & VGA_CR14_DW) {
937
433
        addr >>= 2;
938
202k
    } else if ((sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_SEQ_MODE) == 0 &&
939
197k
               (s->cr[VGA_CRTC_MODE] & VGA_CR17_WORD_BYTE) == 0) {
940
197k
        addr >>= 1;
941
197k
    }
942
943
203k
    if (addr * sizeof(uint32_t) >= s->vram_size) {
944
647
        return;
945
647
    }
946
947
202k
    if (sr(s, VGA_SEQ_MEMORY_MODE) & VGA_SR04_CHN_4M) {
948
5.66k
        if (mask) {
949
3.46k
            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
3.46k
            s->plane_updated |= mask; /* only used to detect font change */
954
3.46k
            memory_region_set_dirty(&s->vram, addr, 1);
955
3.46k
        }
956
5.66k
        return;
957
5.66k
    }
958
959
    /* standard VGA latched access */
960
197k
    write_mode = s->gr[VGA_GFX_MODE] & 3;
961
197k
    switch(write_mode) {
962
0
    default:
963
191k
    case 0:
964
        /* rotate */
965
191k
        b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
966
191k
        val = ((val >> b) | (val << (8 - b))) & 0xff;
967
191k
        val |= val << 8;
968
191k
        val |= val << 16;
969
970
        /* apply set/reset mask */
971
191k
        set_mask = mask16[s->gr[VGA_GFX_SR_ENABLE]];
972
191k
        val = (val & ~set_mask) |
973
191k
            (mask16[s->gr[VGA_GFX_SR_VALUE]] & set_mask);
974
191k
        bit_mask = s->gr[VGA_GFX_BIT_MASK];
975
191k
        break;
976
3.76k
    case 1:
977
3.76k
        val = s->latch;
978
3.76k
        goto do_write;
979
1.69k
    case 2:
980
1.69k
        val = mask16[val & 0x0f];
981
1.69k
        bit_mask = s->gr[VGA_GFX_BIT_MASK];
982
1.69k
        break;
983
627
    case 3:
984
        /* rotate */
985
627
        b = s->gr[VGA_GFX_DATA_ROTATE] & 7;
986
627
        val = (val >> b) | (val << (8 - b));
987
988
627
        bit_mask = s->gr[VGA_GFX_BIT_MASK] & val;
989
627
        val = mask16[s->gr[VGA_GFX_SR_VALUE]];
990
627
        break;
991
197k
    }
992
993
    /* apply logical operation */
994
193k
    func_select = s->gr[VGA_GFX_DATA_ROTATE] >> 3;
995
193k
    switch(func_select) {
996
191k
    case 0:
997
191k
    default:
998
        /* nothing to do */
999
191k
        break;
1000
191k
    case 1:
1001
        /* and */
1002
394
        val &= s->latch;
1003
394
        break;
1004
413
    case 2:
1005
        /* or */
1006
413
        val |= s->latch;
1007
413
        break;
1008
672
    case 3:
1009
        /* xor */
1010
672
        val ^= s->latch;
1011
672
        break;
1012
193k
    }
1013
1014
    /* apply bit mask */
1015
193k
    bit_mask |= bit_mask << 8;
1016
193k
    bit_mask |= bit_mask << 16;
1017
193k
    val = (val & bit_mask) | (s->latch & ~bit_mask);
1018
1019
197k
do_write:
1020
    /* mask data according to sr[2] */
1021
197k
    s->plane_updated |= mask; /* only used to detect font change */
1022
197k
    write_mask = mask16[mask];
1023
197k
    ((uint32_t *)s->vram_ptr)[addr] =
1024
197k
        (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
1025
197k
        (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
197k
    memory_region_set_dirty(&s->vram, addr << 2, sizeof(uint32_t));
1031
197k
}
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.25k
{
1488
6.25k
    memory_region_set_log(&s->vram, true, DIRTY_MEMORY_VGA);
1489
6.25k
}
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
50.7k
{
1859
50.7k
    s->sr_index = 0;
1860
50.7k
    memset(s->sr, '\0', sizeof(s->sr));
1861
50.7k
    memset(s->sr_vbe, '\0', sizeof(s->sr_vbe));
1862
50.7k
    s->gr_index = 0;
1863
50.7k
    memset(s->gr, '\0', sizeof(s->gr));
1864
50.7k
    s->ar_index = 0;
1865
50.7k
    memset(s->ar, '\0', sizeof(s->ar));
1866
50.7k
    s->ar_flip_flop = 0;
1867
50.7k
    s->cr_index = 0;
1868
50.7k
    memset(s->cr, '\0', sizeof(s->cr));
1869
50.7k
    s->msr = 0;
1870
50.7k
    s->fcr = 0;
1871
50.7k
    s->st00 = 0;
1872
50.7k
    s->st01 = 0;
1873
50.7k
    s->dac_state = 0;
1874
50.7k
    s->dac_sub_index = 0;
1875
50.7k
    s->dac_read_index = 0;
1876
50.7k
    s->dac_write_index = 0;
1877
50.7k
    memset(s->dac_cache, '\0', sizeof(s->dac_cache));
1878
50.7k
    s->dac_8bit = 0;
1879
50.7k
    memset(s->palette, '\0', sizeof(s->palette));
1880
50.7k
    s->bank_offset = 0;
1881
50.7k
    s->vbe_index = 0;
1882
50.7k
    memset(s->vbe_regs, '\0', sizeof(s->vbe_regs));
1883
50.7k
    s->vbe_regs[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
1884
50.7k
    s->vbe_start_addr = 0;
1885
50.7k
    s->vbe_line_offset = 0;
1886
50.7k
    s->vbe_bank_mask = (s->vram_size >> 16) - 1;
1887
50.7k
    memset(s->font_offsets, '\0', sizeof(s->font_offsets));
1888
50.7k
    s->graphic_mode = -1; /* force full update */
1889
50.7k
    s->shift_control = 0;
1890
50.7k
    s->double_scan = 0;
1891
50.7k
    memset(&s->params, '\0', sizeof(s->params));
1892
50.7k
    s->plane_updated = 0;
1893
50.7k
    s->last_cw = 0;
1894
50.7k
    s->last_ch = 0;
1895
50.7k
    s->last_width = 0;
1896
50.7k
    s->last_height = 0;
1897
50.7k
    s->last_text_width = 0;
1898
50.7k
    s->last_text_height = 0;
1899
50.7k
    s->last_scr_width = 0;
1900
50.7k
    s->last_scr_height = 0;
1901
50.7k
    s->cursor_start = 0;
1902
50.7k
    s->cursor_end = 0;
1903
50.7k
    s->cursor_offset = 0;
1904
50.7k
    memset(s->invalidated_y_table, '\0', sizeof(s->invalidated_y_table));
1905
50.7k
    memset(s->last_palette, '\0', sizeof(s->last_palette));
1906
50.7k
    memset(s->last_ch_attr, '\0', sizeof(s->last_ch_attr));
1907
50.7k
    switch (vga_retrace_method) {
1908
50.7k
    case VGA_RETRACE_DUMB:
1909
50.7k
        break;
1910
0
    case VGA_RETRACE_PRECISE:
1911
0
        memset(&s->retrace_info, 0, sizeof (s->retrace_info));
1912
0
        break;
1913
50.7k
    }
1914
50.7k
    vga_update_memory_access(s);
1915
50.7k
}
1916
1917
static void vga_reset(void *opaque)
1918
20.8k
{
1919
20.8k
    VGACommonState *s =  opaque;
1920
20.8k
    vga_common_reset(s);
1921
20.8k
}
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
5.38M
{
2104
5.38M
    VGACommonState *s = opaque;
2105
2106
5.38M
    return vga_mem_readb(s, addr);
2107
5.38M
}
2108
2109
static void vga_mem_write(void *opaque, hwaddr addr,
2110
                          uint64_t data, unsigned size)
2111
180k
{
2112
180k
    VGACommonState *s = opaque;
2113
2114
180k
    vga_mem_writeb(s, addr, data);
2115
180k
}
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
}