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