2 * QEMU Cirrus CLGD 54xx VGA Emulator.
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2004 Makoto Suzuki (suzu)
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * Reference: Finn Thogersons' VGADOC4b:
28 * http://web.archive.org/web/20021019054927/http://home.worldonline.dk/finth/
30 * VGADOC4b.ZIP content available at:
32 * https://pdos.csail.mit.edu/6.828/2005/readings/hardware/vgadoc
34 #include "qemu/osdep.h"
35 #include "qemu/units.h"
36 #include "qapi/error.h"
39 #include "hw/pci/pci.h"
40 #include "ui/pixel_ops.h"
41 #include "cirrus_vga_internal.h"
45 * - destination write mask support not complete (bits 5..7)
46 * - optimize linear mappings
47 * - optimize bitblt functions
50 //#define DEBUG_CIRRUS
51 //#define DEBUG_BITBLT
53 /***************************************
57 ***************************************/
60 #define CIRRUS_SR7_BPP_VGA 0x00
61 #define CIRRUS_SR7_BPP_SVGA 0x01
62 #define CIRRUS_SR7_BPP_MASK 0x0e
63 #define CIRRUS_SR7_BPP_8 0x00
64 #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
65 #define CIRRUS_SR7_BPP_24 0x04
66 #define CIRRUS_SR7_BPP_16 0x06
67 #define CIRRUS_SR7_BPP_32 0x08
68 #define CIRRUS_SR7_ISAADDR_MASK 0xe0
71 #define CIRRUS_MEMSIZE_512k 0x08
72 #define CIRRUS_MEMSIZE_1M 0x10
73 #define CIRRUS_MEMSIZE_2M 0x18
74 #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
77 #define CIRRUS_CURSOR_SHOW 0x01
78 #define CIRRUS_CURSOR_HIDDENPEL 0x02
79 #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
82 #define CIRRUS_BUSTYPE_VLBFAST 0x10
83 #define CIRRUS_BUSTYPE_PCI 0x20
84 #define CIRRUS_BUSTYPE_VLBSLOW 0x30
85 #define CIRRUS_BUSTYPE_ISA 0x38
86 #define CIRRUS_MMIO_ENABLE 0x04
87 #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
88 #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
91 #define CIRRUS_BANKING_DUAL 0x01
92 #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
95 #define CIRRUS_BLTMODE_BACKWARDS 0x01
96 #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
97 #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
98 #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
99 #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
100 #define CIRRUS_BLTMODE_COLOREXPAND 0x80
101 #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
102 #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
103 #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
104 #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
105 #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
108 #define CIRRUS_BLT_BUSY 0x01
109 #define CIRRUS_BLT_START 0x02
110 #define CIRRUS_BLT_RESET 0x04
111 #define CIRRUS_BLT_FIFOUSED 0x10
112 #define CIRRUS_BLT_AUTOSTART 0x80
115 #define CIRRUS_ROP_0 0x00
116 #define CIRRUS_ROP_SRC_AND_DST 0x05
117 #define CIRRUS_ROP_NOP 0x06
118 #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
119 #define CIRRUS_ROP_NOTDST 0x0b
120 #define CIRRUS_ROP_SRC 0x0d
121 #define CIRRUS_ROP_1 0x0e
122 #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
123 #define CIRRUS_ROP_SRC_XOR_DST 0x59
124 #define CIRRUS_ROP_SRC_OR_DST 0x6d
125 #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
126 #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
127 #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
128 #define CIRRUS_ROP_NOTSRC 0xd0
129 #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
130 #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
132 #define CIRRUS_ROP_NOP_INDEX 2
133 #define CIRRUS_ROP_SRC_INDEX 5
136 #define CIRRUS_BLTMODEEXT_SOLIDFILL 0x04
137 #define CIRRUS_BLTMODEEXT_COLOREXPINV 0x02
138 #define CIRRUS_BLTMODEEXT_DWORDGRANULARITY 0x01
141 #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
142 #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
143 #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
144 #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
145 #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
146 #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
147 #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
148 #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
149 #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
150 #define CIRRUS_MMIO_BLTMODE 0x18 // byte
151 #define CIRRUS_MMIO_BLTROP 0x1a // byte
152 #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
153 #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
154 #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
155 #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
156 #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
157 #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
158 #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
159 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
160 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
161 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
162 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
163 #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
164 #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
165 #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
166 #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
167 #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
168 #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
169 #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
171 #define CIRRUS_PNPMMIO_SIZE 0x1000
173 typedef void (*cirrus_fill_t)(struct CirrusVGAState *s,
174 uint32_t dstaddr, int dst_pitch,
175 int width, int height);
177 typedef struct PCICirrusVGAState {
179 CirrusVGAState cirrus_vga;
182 #define TYPE_PCI_CIRRUS_VGA "cirrus-vga"
183 #define PCI_CIRRUS_VGA(obj) \
184 OBJECT_CHECK(PCICirrusVGAState, (obj), TYPE_PCI_CIRRUS_VGA)
186 static uint8_t rop_to_index[256];
188 /***************************************
192 ***************************************/
195 static void cirrus_bitblt_reset(CirrusVGAState *s);
196 static void cirrus_update_memory_access(CirrusVGAState *s);
198 /***************************************
202 ***************************************/
204 static bool blit_region_is_unsafe(struct CirrusVGAState *s,
205 int32_t pitch, int32_t addr)
212 + ((int64_t)s->cirrus_blt_height - 1) * pitch
213 - s->cirrus_blt_width;
214 if (min < -1 || addr >= s->vga.vram_size) {
219 + ((int64_t)s->cirrus_blt_height-1) * pitch
220 + s->cirrus_blt_width;
221 if (max > s->vga.vram_size) {
228 static bool blit_is_unsafe(struct CirrusVGAState *s, bool dst_only)
230 /* should be the case, see cirrus_bitblt_start */
231 assert(s->cirrus_blt_width > 0);
232 assert(s->cirrus_blt_height > 0);
234 if (s->cirrus_blt_width > CIRRUS_BLTBUFSIZE) {
238 if (blit_region_is_unsafe(s, s->cirrus_blt_dstpitch,
239 s->cirrus_blt_dstaddr)) {
245 if (blit_region_is_unsafe(s, s->cirrus_blt_srcpitch,
246 s->cirrus_blt_srcaddr)) {
253 static void cirrus_bitblt_rop_nop(CirrusVGAState *s,
254 uint32_t dstaddr, uint32_t srcaddr,
255 int dstpitch,int srcpitch,
256 int bltwidth,int bltheight)
260 static void cirrus_bitblt_fill_nop(CirrusVGAState *s,
262 int dstpitch, int bltwidth,int bltheight)
266 static inline uint8_t cirrus_src(CirrusVGAState *s, uint32_t srcaddr)
268 if (s->cirrus_srccounter) {
270 return s->cirrus_bltbuf[srcaddr & (CIRRUS_BLTBUFSIZE - 1)];
273 return s->vga.vram_ptr[srcaddr & s->cirrus_addr_mask];
277 static inline uint16_t cirrus_src16(CirrusVGAState *s, uint32_t srcaddr)
281 if (s->cirrus_srccounter) {
283 src = (void *)&s->cirrus_bltbuf[srcaddr & (CIRRUS_BLTBUFSIZE - 1) & ~1];
286 src = (void *)&s->vga.vram_ptr[srcaddr & s->cirrus_addr_mask & ~1];
291 static inline uint32_t cirrus_src32(CirrusVGAState *s, uint32_t srcaddr)
295 if (s->cirrus_srccounter) {
297 src = (void *)&s->cirrus_bltbuf[srcaddr & (CIRRUS_BLTBUFSIZE - 1) & ~3];
300 src = (void *)&s->vga.vram_ptr[srcaddr & s->cirrus_addr_mask & ~3];
306 #define ROP_FN(d, s) 0
307 #include "cirrus_vga_rop.h"
309 #define ROP_NAME src_and_dst
310 #define ROP_FN(d, s) (s) & (d)
311 #include "cirrus_vga_rop.h"
313 #define ROP_NAME src_and_notdst
314 #define ROP_FN(d, s) (s) & (~(d))
315 #include "cirrus_vga_rop.h"
317 #define ROP_NAME notdst
318 #define ROP_FN(d, s) ~(d)
319 #include "cirrus_vga_rop.h"
322 #define ROP_FN(d, s) s
323 #include "cirrus_vga_rop.h"
326 #define ROP_FN(d, s) ~0
327 #include "cirrus_vga_rop.h"
329 #define ROP_NAME notsrc_and_dst
330 #define ROP_FN(d, s) (~(s)) & (d)
331 #include "cirrus_vga_rop.h"
333 #define ROP_NAME src_xor_dst
334 #define ROP_FN(d, s) (s) ^ (d)
335 #include "cirrus_vga_rop.h"
337 #define ROP_NAME src_or_dst
338 #define ROP_FN(d, s) (s) | (d)
339 #include "cirrus_vga_rop.h"
341 #define ROP_NAME notsrc_or_notdst
342 #define ROP_FN(d, s) (~(s)) | (~(d))
343 #include "cirrus_vga_rop.h"
345 #define ROP_NAME src_notxor_dst
346 #define ROP_FN(d, s) ~((s) ^ (d))
347 #include "cirrus_vga_rop.h"
349 #define ROP_NAME src_or_notdst
350 #define ROP_FN(d, s) (s) | (~(d))
351 #include "cirrus_vga_rop.h"
353 #define ROP_NAME notsrc
354 #define ROP_FN(d, s) (~(s))
355 #include "cirrus_vga_rop.h"
357 #define ROP_NAME notsrc_or_dst
358 #define ROP_FN(d, s) (~(s)) | (d)
359 #include "cirrus_vga_rop.h"
361 #define ROP_NAME notsrc_and_notdst
362 #define ROP_FN(d, s) (~(s)) & (~(d))
363 #include "cirrus_vga_rop.h"
365 static const cirrus_bitblt_rop_t cirrus_fwd_rop[16] = {
366 cirrus_bitblt_rop_fwd_0,
367 cirrus_bitblt_rop_fwd_src_and_dst,
368 cirrus_bitblt_rop_nop,
369 cirrus_bitblt_rop_fwd_src_and_notdst,
370 cirrus_bitblt_rop_fwd_notdst,
371 cirrus_bitblt_rop_fwd_src,
372 cirrus_bitblt_rop_fwd_1,
373 cirrus_bitblt_rop_fwd_notsrc_and_dst,
374 cirrus_bitblt_rop_fwd_src_xor_dst,
375 cirrus_bitblt_rop_fwd_src_or_dst,
376 cirrus_bitblt_rop_fwd_notsrc_or_notdst,
377 cirrus_bitblt_rop_fwd_src_notxor_dst,
378 cirrus_bitblt_rop_fwd_src_or_notdst,
379 cirrus_bitblt_rop_fwd_notsrc,
380 cirrus_bitblt_rop_fwd_notsrc_or_dst,
381 cirrus_bitblt_rop_fwd_notsrc_and_notdst,
384 static const cirrus_bitblt_rop_t cirrus_bkwd_rop[16] = {
385 cirrus_bitblt_rop_bkwd_0,
386 cirrus_bitblt_rop_bkwd_src_and_dst,
387 cirrus_bitblt_rop_nop,
388 cirrus_bitblt_rop_bkwd_src_and_notdst,
389 cirrus_bitblt_rop_bkwd_notdst,
390 cirrus_bitblt_rop_bkwd_src,
391 cirrus_bitblt_rop_bkwd_1,
392 cirrus_bitblt_rop_bkwd_notsrc_and_dst,
393 cirrus_bitblt_rop_bkwd_src_xor_dst,
394 cirrus_bitblt_rop_bkwd_src_or_dst,
395 cirrus_bitblt_rop_bkwd_notsrc_or_notdst,
396 cirrus_bitblt_rop_bkwd_src_notxor_dst,
397 cirrus_bitblt_rop_bkwd_src_or_notdst,
398 cirrus_bitblt_rop_bkwd_notsrc,
399 cirrus_bitblt_rop_bkwd_notsrc_or_dst,
400 cirrus_bitblt_rop_bkwd_notsrc_and_notdst,
403 #define TRANSP_ROP(name) {\
407 #define TRANSP_NOP(func) {\
412 static const cirrus_bitblt_rop_t cirrus_fwd_transp_rop[16][2] = {
413 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_0),
414 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_dst),
415 TRANSP_NOP(cirrus_bitblt_rop_nop),
416 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_notdst),
417 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notdst),
418 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src),
419 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_1),
420 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_dst),
421 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_xor_dst),
422 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_dst),
423 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_notdst),
424 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_notxor_dst),
425 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_notdst),
426 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc),
427 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_dst),
428 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_notdst),
431 static const cirrus_bitblt_rop_t cirrus_bkwd_transp_rop[16][2] = {
432 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_0),
433 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_dst),
434 TRANSP_NOP(cirrus_bitblt_rop_nop),
435 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_notdst),
436 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notdst),
437 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src),
438 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_1),
439 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_dst),
440 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_xor_dst),
441 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_dst),
442 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_notdst),
443 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_notxor_dst),
444 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_notdst),
445 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc),
446 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_dst),
447 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_notdst),
450 #define ROP2(name) {\
457 #define ROP_NOP2(func) {\
464 static const cirrus_bitblt_rop_t cirrus_patternfill[16][4] = {
465 ROP2(cirrus_patternfill_0),
466 ROP2(cirrus_patternfill_src_and_dst),
467 ROP_NOP2(cirrus_bitblt_rop_nop),
468 ROP2(cirrus_patternfill_src_and_notdst),
469 ROP2(cirrus_patternfill_notdst),
470 ROP2(cirrus_patternfill_src),
471 ROP2(cirrus_patternfill_1),
472 ROP2(cirrus_patternfill_notsrc_and_dst),
473 ROP2(cirrus_patternfill_src_xor_dst),
474 ROP2(cirrus_patternfill_src_or_dst),
475 ROP2(cirrus_patternfill_notsrc_or_notdst),
476 ROP2(cirrus_patternfill_src_notxor_dst),
477 ROP2(cirrus_patternfill_src_or_notdst),
478 ROP2(cirrus_patternfill_notsrc),
479 ROP2(cirrus_patternfill_notsrc_or_dst),
480 ROP2(cirrus_patternfill_notsrc_and_notdst),
483 static const cirrus_bitblt_rop_t cirrus_colorexpand_transp[16][4] = {
484 ROP2(cirrus_colorexpand_transp_0),
485 ROP2(cirrus_colorexpand_transp_src_and_dst),
486 ROP_NOP2(cirrus_bitblt_rop_nop),
487 ROP2(cirrus_colorexpand_transp_src_and_notdst),
488 ROP2(cirrus_colorexpand_transp_notdst),
489 ROP2(cirrus_colorexpand_transp_src),
490 ROP2(cirrus_colorexpand_transp_1),
491 ROP2(cirrus_colorexpand_transp_notsrc_and_dst),
492 ROP2(cirrus_colorexpand_transp_src_xor_dst),
493 ROP2(cirrus_colorexpand_transp_src_or_dst),
494 ROP2(cirrus_colorexpand_transp_notsrc_or_notdst),
495 ROP2(cirrus_colorexpand_transp_src_notxor_dst),
496 ROP2(cirrus_colorexpand_transp_src_or_notdst),
497 ROP2(cirrus_colorexpand_transp_notsrc),
498 ROP2(cirrus_colorexpand_transp_notsrc_or_dst),
499 ROP2(cirrus_colorexpand_transp_notsrc_and_notdst),
502 static const cirrus_bitblt_rop_t cirrus_colorexpand[16][4] = {
503 ROP2(cirrus_colorexpand_0),
504 ROP2(cirrus_colorexpand_src_and_dst),
505 ROP_NOP2(cirrus_bitblt_rop_nop),
506 ROP2(cirrus_colorexpand_src_and_notdst),
507 ROP2(cirrus_colorexpand_notdst),
508 ROP2(cirrus_colorexpand_src),
509 ROP2(cirrus_colorexpand_1),
510 ROP2(cirrus_colorexpand_notsrc_and_dst),
511 ROP2(cirrus_colorexpand_src_xor_dst),
512 ROP2(cirrus_colorexpand_src_or_dst),
513 ROP2(cirrus_colorexpand_notsrc_or_notdst),
514 ROP2(cirrus_colorexpand_src_notxor_dst),
515 ROP2(cirrus_colorexpand_src_or_notdst),
516 ROP2(cirrus_colorexpand_notsrc),
517 ROP2(cirrus_colorexpand_notsrc_or_dst),
518 ROP2(cirrus_colorexpand_notsrc_and_notdst),
521 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern_transp[16][4] = {
522 ROP2(cirrus_colorexpand_pattern_transp_0),
523 ROP2(cirrus_colorexpand_pattern_transp_src_and_dst),
524 ROP_NOP2(cirrus_bitblt_rop_nop),
525 ROP2(cirrus_colorexpand_pattern_transp_src_and_notdst),
526 ROP2(cirrus_colorexpand_pattern_transp_notdst),
527 ROP2(cirrus_colorexpand_pattern_transp_src),
528 ROP2(cirrus_colorexpand_pattern_transp_1),
529 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_dst),
530 ROP2(cirrus_colorexpand_pattern_transp_src_xor_dst),
531 ROP2(cirrus_colorexpand_pattern_transp_src_or_dst),
532 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_notdst),
533 ROP2(cirrus_colorexpand_pattern_transp_src_notxor_dst),
534 ROP2(cirrus_colorexpand_pattern_transp_src_or_notdst),
535 ROP2(cirrus_colorexpand_pattern_transp_notsrc),
536 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_dst),
537 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_notdst),
540 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern[16][4] = {
541 ROP2(cirrus_colorexpand_pattern_0),
542 ROP2(cirrus_colorexpand_pattern_src_and_dst),
543 ROP_NOP2(cirrus_bitblt_rop_nop),
544 ROP2(cirrus_colorexpand_pattern_src_and_notdst),
545 ROP2(cirrus_colorexpand_pattern_notdst),
546 ROP2(cirrus_colorexpand_pattern_src),
547 ROP2(cirrus_colorexpand_pattern_1),
548 ROP2(cirrus_colorexpand_pattern_notsrc_and_dst),
549 ROP2(cirrus_colorexpand_pattern_src_xor_dst),
550 ROP2(cirrus_colorexpand_pattern_src_or_dst),
551 ROP2(cirrus_colorexpand_pattern_notsrc_or_notdst),
552 ROP2(cirrus_colorexpand_pattern_src_notxor_dst),
553 ROP2(cirrus_colorexpand_pattern_src_or_notdst),
554 ROP2(cirrus_colorexpand_pattern_notsrc),
555 ROP2(cirrus_colorexpand_pattern_notsrc_or_dst),
556 ROP2(cirrus_colorexpand_pattern_notsrc_and_notdst),
559 static const cirrus_fill_t cirrus_fill[16][4] = {
561 ROP2(cirrus_fill_src_and_dst),
562 ROP_NOP2(cirrus_bitblt_fill_nop),
563 ROP2(cirrus_fill_src_and_notdst),
564 ROP2(cirrus_fill_notdst),
565 ROP2(cirrus_fill_src),
567 ROP2(cirrus_fill_notsrc_and_dst),
568 ROP2(cirrus_fill_src_xor_dst),
569 ROP2(cirrus_fill_src_or_dst),
570 ROP2(cirrus_fill_notsrc_or_notdst),
571 ROP2(cirrus_fill_src_notxor_dst),
572 ROP2(cirrus_fill_src_or_notdst),
573 ROP2(cirrus_fill_notsrc),
574 ROP2(cirrus_fill_notsrc_or_dst),
575 ROP2(cirrus_fill_notsrc_and_notdst),
578 static inline void cirrus_bitblt_fgcol(CirrusVGAState *s)
581 switch (s->cirrus_blt_pixelwidth) {
583 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1;
586 color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8);
587 s->cirrus_blt_fgcol = le16_to_cpu(color);
590 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1 |
591 (s->vga.gr[0x11] << 8) | (s->vga.gr[0x13] << 16);
595 color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8) |
596 (s->vga.gr[0x13] << 16) | (s->vga.gr[0x15] << 24);
597 s->cirrus_blt_fgcol = le32_to_cpu(color);
602 static inline void cirrus_bitblt_bgcol(CirrusVGAState *s)
605 switch (s->cirrus_blt_pixelwidth) {
607 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0;
610 color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8);
611 s->cirrus_blt_bgcol = le16_to_cpu(color);
614 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0 |
615 (s->vga.gr[0x10] << 8) | (s->vga.gr[0x12] << 16);
619 color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8) |
620 (s->vga.gr[0x12] << 16) | (s->vga.gr[0x14] << 24);
621 s->cirrus_blt_bgcol = le32_to_cpu(color);
626 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
627 int off_pitch, int bytesperline,
635 off_begin -= bytesperline - 1;
638 for (y = 0; y < lines; y++) {
640 off_cur_end = ((off_cur + bytesperline - 1) & s->cirrus_addr_mask) + 1;
641 assert(off_cur_end >= off_cur);
642 memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
643 off_begin += off_pitch;
647 static int cirrus_bitblt_common_patterncopy(CirrusVGAState *s)
649 uint32_t patternsize;
650 bool videosrc = !s->cirrus_srccounter;
653 switch (s->vga.get_bpp(&s->vga)) {
667 s->cirrus_blt_srcaddr &= ~(patternsize - 1);
668 if (s->cirrus_blt_srcaddr + patternsize > s->vga.vram_size) {
673 if (blit_is_unsafe(s, true)) {
677 (*s->cirrus_rop) (s, s->cirrus_blt_dstaddr,
678 videosrc ? s->cirrus_blt_srcaddr : 0,
679 s->cirrus_blt_dstpitch, 0,
680 s->cirrus_blt_width, s->cirrus_blt_height);
681 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
682 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
683 s->cirrus_blt_height);
689 static int cirrus_bitblt_solidfill(CirrusVGAState *s, int blt_rop)
691 cirrus_fill_t rop_func;
693 if (blit_is_unsafe(s, true)) {
696 rop_func = cirrus_fill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
697 rop_func(s, s->cirrus_blt_dstaddr,
698 s->cirrus_blt_dstpitch,
699 s->cirrus_blt_width, s->cirrus_blt_height);
700 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
701 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
702 s->cirrus_blt_height);
703 cirrus_bitblt_reset(s);
707 /***************************************
709 * bitblt (video-to-video)
711 ***************************************/
713 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
715 return cirrus_bitblt_common_patterncopy(s);
718 static int cirrus_do_copy(CirrusVGAState *s, int dst, int src, int w, int h)
725 /* make sure to only copy if it's a plain copy ROP */
726 if (*s->cirrus_rop == cirrus_bitblt_rop_fwd_src ||
727 *s->cirrus_rop == cirrus_bitblt_rop_bkwd_src) {
731 depth = s->vga.get_bpp(&s->vga) / 8;
735 s->vga.get_resolution(&s->vga, &width, &height);
738 sx = (src % ABS(s->cirrus_blt_srcpitch)) / depth;
739 sy = (src / ABS(s->cirrus_blt_srcpitch));
740 dx = (dst % ABS(s->cirrus_blt_dstpitch)) / depth;
741 dy = (dst / ABS(s->cirrus_blt_dstpitch));
743 /* normalize width */
746 /* if we're doing a backward copy, we have to adjust
747 our x/y to be the upper left corner (instead of the lower
749 if (s->cirrus_blt_dstpitch < 0) {
750 sx -= (s->cirrus_blt_width / depth) - 1;
751 dx -= (s->cirrus_blt_width / depth) - 1;
752 sy -= s->cirrus_blt_height - 1;
753 dy -= s->cirrus_blt_height - 1;
756 /* are we in the visible portion of memory? */
757 if (sx >= 0 && sy >= 0 && dx >= 0 && dy >= 0 &&
758 (sx + w) <= width && (sy + h) <= height &&
759 (dx + w) <= width && (dy + h) <= height) {
764 (*s->cirrus_rop) (s, s->cirrus_blt_dstaddr,
765 s->cirrus_blt_srcaddr,
766 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
767 s->cirrus_blt_width, s->cirrus_blt_height);
770 dpy_gfx_update(s->vga.con, dx, dy,
771 s->cirrus_blt_width / depth,
772 s->cirrus_blt_height);
775 /* we don't have to notify the display that this portion has
776 changed since qemu_console_copy implies this */
778 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
779 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
780 s->cirrus_blt_height);
785 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
787 if (blit_is_unsafe(s, false))
790 return cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->vga.start_addr,
791 s->cirrus_blt_srcaddr - s->vga.start_addr,
792 s->cirrus_blt_width, s->cirrus_blt_height);
795 /***************************************
797 * bitblt (cpu-to-video)
799 ***************************************/
801 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
806 if (s->cirrus_srccounter > 0) {
807 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
808 cirrus_bitblt_common_patterncopy(s);
810 s->cirrus_srccounter = 0;
811 cirrus_bitblt_reset(s);
813 /* at least one scan line */
815 (*s->cirrus_rop)(s, s->cirrus_blt_dstaddr,
816 0, 0, 0, s->cirrus_blt_width, 1);
817 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
818 s->cirrus_blt_width, 1);
819 s->cirrus_blt_dstaddr += s->cirrus_blt_dstpitch;
820 s->cirrus_srccounter -= s->cirrus_blt_srcpitch;
821 if (s->cirrus_srccounter <= 0)
823 /* more bytes than needed can be transferred because of
824 word alignment, so we keep them for the next line */
825 /* XXX: keep alignment to speed up transfer */
826 end_ptr = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
827 copy_count = s->cirrus_srcptr_end - end_ptr;
828 memmove(s->cirrus_bltbuf, end_ptr, copy_count);
829 s->cirrus_srcptr = s->cirrus_bltbuf + copy_count;
830 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
831 } while (s->cirrus_srcptr >= s->cirrus_srcptr_end);
836 /***************************************
840 ***************************************/
842 static void cirrus_bitblt_reset(CirrusVGAState * s)
847 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
848 need_update = s->cirrus_srcptr != &s->cirrus_bltbuf[0]
849 || s->cirrus_srcptr_end != &s->cirrus_bltbuf[0];
850 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
851 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
852 s->cirrus_srccounter = 0;
855 cirrus_update_memory_access(s);
858 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
862 if (blit_is_unsafe(s, true)) {
866 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
867 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
868 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
870 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
871 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
872 s->cirrus_blt_srcpitch = 8;
874 /* XXX: check for 24 bpp */
875 s->cirrus_blt_srcpitch = 8 * 8 * s->cirrus_blt_pixelwidth;
877 s->cirrus_srccounter = s->cirrus_blt_srcpitch;
879 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
880 w = s->cirrus_blt_width / s->cirrus_blt_pixelwidth;
881 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_DWORDGRANULARITY)
882 s->cirrus_blt_srcpitch = ((w + 31) >> 5);
884 s->cirrus_blt_srcpitch = ((w + 7) >> 3);
886 /* always align input size to 32 bits */
887 s->cirrus_blt_srcpitch = (s->cirrus_blt_width + 3) & ~3;
889 s->cirrus_srccounter = s->cirrus_blt_srcpitch * s->cirrus_blt_height;
892 /* the blit_is_unsafe call above should catch this */
893 assert(s->cirrus_blt_srcpitch <= CIRRUS_BLTBUFSIZE);
895 s->cirrus_srcptr = s->cirrus_bltbuf;
896 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
897 cirrus_update_memory_access(s);
901 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
905 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
910 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
914 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
915 ret = cirrus_bitblt_videotovideo_patterncopy(s);
917 ret = cirrus_bitblt_videotovideo_copy(s);
920 cirrus_bitblt_reset(s);
924 static void cirrus_bitblt_start(CirrusVGAState * s)
928 if (!s->enable_blitter) {
932 s->vga.gr[0x31] |= CIRRUS_BLT_BUSY;
934 s->cirrus_blt_width = (s->vga.gr[0x20] | (s->vga.gr[0x21] << 8)) + 1;
935 s->cirrus_blt_height = (s->vga.gr[0x22] | (s->vga.gr[0x23] << 8)) + 1;
936 s->cirrus_blt_dstpitch = (s->vga.gr[0x24] | (s->vga.gr[0x25] << 8));
937 s->cirrus_blt_srcpitch = (s->vga.gr[0x26] | (s->vga.gr[0x27] << 8));
938 s->cirrus_blt_dstaddr =
939 (s->vga.gr[0x28] | (s->vga.gr[0x29] << 8) | (s->vga.gr[0x2a] << 16));
940 s->cirrus_blt_srcaddr =
941 (s->vga.gr[0x2c] | (s->vga.gr[0x2d] << 8) | (s->vga.gr[0x2e] << 16));
942 s->cirrus_blt_mode = s->vga.gr[0x30];
943 s->cirrus_blt_modeext = s->vga.gr[0x33];
944 blt_rop = s->vga.gr[0x32];
946 s->cirrus_blt_dstaddr &= s->cirrus_addr_mask;
947 s->cirrus_blt_srcaddr &= s->cirrus_addr_mask;
950 printf("rop=0x%02x mode=0x%02x modeext=0x%02x w=%d h=%d dpitch=%d spitch=%d daddr=0x%08x saddr=0x%08x writemask=0x%02x\n",
953 s->cirrus_blt_modeext,
955 s->cirrus_blt_height,
956 s->cirrus_blt_dstpitch,
957 s->cirrus_blt_srcpitch,
958 s->cirrus_blt_dstaddr,
959 s->cirrus_blt_srcaddr,
963 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
964 case CIRRUS_BLTMODE_PIXELWIDTH8:
965 s->cirrus_blt_pixelwidth = 1;
967 case CIRRUS_BLTMODE_PIXELWIDTH16:
968 s->cirrus_blt_pixelwidth = 2;
970 case CIRRUS_BLTMODE_PIXELWIDTH24:
971 s->cirrus_blt_pixelwidth = 3;
973 case CIRRUS_BLTMODE_PIXELWIDTH32:
974 s->cirrus_blt_pixelwidth = 4;
978 printf("cirrus: bitblt - pixel width is unknown\n");
982 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
985 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
986 CIRRUS_BLTMODE_MEMSYSDEST))
987 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
989 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
994 if ((s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
995 (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST |
996 CIRRUS_BLTMODE_TRANSPARENTCOMP |
997 CIRRUS_BLTMODE_PATTERNCOPY |
998 CIRRUS_BLTMODE_COLOREXPAND)) ==
999 (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
1000 cirrus_bitblt_fgcol(s);
1001 cirrus_bitblt_solidfill(s, blt_rop);
1003 if ((s->cirrus_blt_mode & (CIRRUS_BLTMODE_COLOREXPAND |
1004 CIRRUS_BLTMODE_PATTERNCOPY)) ==
1005 CIRRUS_BLTMODE_COLOREXPAND) {
1007 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1008 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
1009 cirrus_bitblt_bgcol(s);
1011 cirrus_bitblt_fgcol(s);
1012 s->cirrus_rop = cirrus_colorexpand_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1014 cirrus_bitblt_fgcol(s);
1015 cirrus_bitblt_bgcol(s);
1016 s->cirrus_rop = cirrus_colorexpand[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1018 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
1019 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
1020 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1021 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
1022 cirrus_bitblt_bgcol(s);
1024 cirrus_bitblt_fgcol(s);
1025 s->cirrus_rop = cirrus_colorexpand_pattern_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1027 cirrus_bitblt_fgcol(s);
1028 cirrus_bitblt_bgcol(s);
1029 s->cirrus_rop = cirrus_colorexpand_pattern[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1032 s->cirrus_rop = cirrus_patternfill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1035 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1036 if (s->cirrus_blt_pixelwidth > 2) {
1037 printf("src transparent without colorexpand must be 8bpp or 16bpp\n");
1040 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
1041 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
1042 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
1043 s->cirrus_rop = cirrus_bkwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1045 s->cirrus_rop = cirrus_fwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1048 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
1049 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
1050 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
1051 s->cirrus_rop = cirrus_bkwd_rop[rop_to_index[blt_rop]];
1053 s->cirrus_rop = cirrus_fwd_rop[rop_to_index[blt_rop]];
1057 // setup bitblt engine.
1058 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
1059 if (!cirrus_bitblt_cputovideo(s))
1061 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
1062 if (!cirrus_bitblt_videotocpu(s))
1065 if (!cirrus_bitblt_videotovideo(s))
1071 cirrus_bitblt_reset(s);
1074 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1078 old_value = s->vga.gr[0x31];
1079 s->vga.gr[0x31] = reg_value;
1081 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1082 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1083 cirrus_bitblt_reset(s);
1084 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1085 ((reg_value & CIRRUS_BLT_START) != 0)) {
1086 cirrus_bitblt_start(s);
1091 /***************************************
1095 ***************************************/
1097 static void cirrus_get_offsets(VGACommonState *s1,
1098 uint32_t *pline_offset,
1099 uint32_t *pstart_addr,
1100 uint32_t *pline_compare)
1102 CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
1103 uint32_t start_addr, line_offset, line_compare;
1105 line_offset = s->vga.cr[0x13]
1106 | ((s->vga.cr[0x1b] & 0x10) << 4);
1108 *pline_offset = line_offset;
1110 start_addr = (s->vga.cr[0x0c] << 8)
1112 | ((s->vga.cr[0x1b] & 0x01) << 16)
1113 | ((s->vga.cr[0x1b] & 0x0c) << 15)
1114 | ((s->vga.cr[0x1d] & 0x80) << 12);
1115 *pstart_addr = start_addr;
1117 line_compare = s->vga.cr[0x18] |
1118 ((s->vga.cr[0x07] & 0x10) << 4) |
1119 ((s->vga.cr[0x09] & 0x40) << 3);
1120 *pline_compare = line_compare;
1123 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1127 switch (s->cirrus_hidden_dac_data & 0xf) {
1130 break; /* Sierra HiColor */
1133 break; /* XGA HiColor */
1136 printf("cirrus: invalid DAC value %x in 16bpp\n",
1137 (s->cirrus_hidden_dac_data & 0xf));
1145 static int cirrus_get_bpp(VGACommonState *s1)
1147 CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
1150 if ((s->vga.sr[0x07] & 0x01) != 0) {
1152 switch (s->vga.sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1153 case CIRRUS_SR7_BPP_8:
1156 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1157 ret = cirrus_get_bpp16_depth(s);
1159 case CIRRUS_SR7_BPP_24:
1162 case CIRRUS_SR7_BPP_16:
1163 ret = cirrus_get_bpp16_depth(s);
1165 case CIRRUS_SR7_BPP_32:
1170 printf("cirrus: unknown bpp - sr7=%x\n", s->vga.sr[0x7]);
1183 static void cirrus_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
1187 width = (s->cr[0x01] + 1) * 8;
1188 height = s->cr[0x12] |
1189 ((s->cr[0x07] & 0x02) << 7) |
1190 ((s->cr[0x07] & 0x40) << 3);
1191 height = (height + 1);
1192 /* interlace support */
1193 if (s->cr[0x1a] & 0x01)
1194 height = height * 2;
1199 /***************************************
1203 ***************************************/
1205 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1210 if ((s->vga.gr[0x0b] & 0x01) != 0) /* dual bank */
1211 offset = s->vga.gr[0x09 + bank_index];
1212 else /* single bank */
1213 offset = s->vga.gr[0x09];
1215 if ((s->vga.gr[0x0b] & 0x20) != 0)
1220 if (s->real_vram_size <= offset)
1223 limit = s->real_vram_size - offset;
1225 if (((s->vga.gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1226 if (limit > 0x8000) {
1235 s->cirrus_bank_base[bank_index] = offset;
1236 s->cirrus_bank_limit[bank_index] = limit;
1238 s->cirrus_bank_base[bank_index] = 0;
1239 s->cirrus_bank_limit[bank_index] = 0;
1243 /***************************************
1245 * I/O access between 0x3c4-0x3c5
1247 ***************************************/
1249 static int cirrus_vga_read_sr(CirrusVGAState * s)
1251 switch (s->vga.sr_index) {
1252 case 0x00: // Standard VGA
1253 case 0x01: // Standard VGA
1254 case 0x02: // Standard VGA
1255 case 0x03: // Standard VGA
1256 case 0x04: // Standard VGA
1257 return s->vga.sr[s->vga.sr_index];
1258 case 0x06: // Unlock Cirrus extensions
1259 return s->vga.sr[s->vga.sr_index];
1263 case 0x70: // Graphics Cursor X
1267 case 0xf0: // Graphics Cursor X
1268 return s->vga.sr[0x10];
1272 case 0x71: // Graphics Cursor Y
1276 case 0xf1: // Graphics Cursor Y
1277 return s->vga.sr[0x11];
1279 case 0x07: // Extended Sequencer Mode
1280 case 0x08: // EEPROM Control
1281 case 0x09: // Scratch Register 0
1282 case 0x0a: // Scratch Register 1
1283 case 0x0b: // VCLK 0
1284 case 0x0c: // VCLK 1
1285 case 0x0d: // VCLK 2
1286 case 0x0e: // VCLK 3
1287 case 0x0f: // DRAM Control
1288 case 0x12: // Graphics Cursor Attribute
1289 case 0x13: // Graphics Cursor Pattern Address
1290 case 0x14: // Scratch Register 2
1291 case 0x15: // Scratch Register 3
1292 case 0x16: // Performance Tuning Register
1293 case 0x17: // Configuration Readback and Extended Control
1294 case 0x18: // Signature Generator Control
1295 case 0x19: // Signal Generator Result
1296 case 0x1a: // Signal Generator Result
1297 case 0x1b: // VCLK 0 Denominator & Post
1298 case 0x1c: // VCLK 1 Denominator & Post
1299 case 0x1d: // VCLK 2 Denominator & Post
1300 case 0x1e: // VCLK 3 Denominator & Post
1301 case 0x1f: // BIOS Write Enable and MCLK select
1303 printf("cirrus: handled inport sr_index %02x\n", s->vga.sr_index);
1305 return s->vga.sr[s->vga.sr_index];
1308 printf("cirrus: inport sr_index %02x\n", s->vga.sr_index);
1315 static void cirrus_vga_write_sr(CirrusVGAState * s, uint32_t val)
1317 switch (s->vga.sr_index) {
1318 case 0x00: // Standard VGA
1319 case 0x01: // Standard VGA
1320 case 0x02: // Standard VGA
1321 case 0x03: // Standard VGA
1322 case 0x04: // Standard VGA
1323 s->vga.sr[s->vga.sr_index] = val & sr_mask[s->vga.sr_index];
1324 if (s->vga.sr_index == 1)
1325 s->vga.update_retrace_info(&s->vga);
1327 case 0x06: // Unlock Cirrus extensions
1330 s->vga.sr[s->vga.sr_index] = 0x12;
1332 s->vga.sr[s->vga.sr_index] = 0x0f;
1338 case 0x70: // Graphics Cursor X
1342 case 0xf0: // Graphics Cursor X
1343 s->vga.sr[0x10] = val;
1344 s->vga.hw_cursor_x = (val << 3) | (s->vga.sr_index >> 5);
1349 case 0x71: // Graphics Cursor Y
1353 case 0xf1: // Graphics Cursor Y
1354 s->vga.sr[0x11] = val;
1355 s->vga.hw_cursor_y = (val << 3) | (s->vga.sr_index >> 5);
1357 case 0x07: // Extended Sequencer Mode
1358 cirrus_update_memory_access(s);
1360 case 0x08: // EEPROM Control
1361 case 0x09: // Scratch Register 0
1362 case 0x0a: // Scratch Register 1
1363 case 0x0b: // VCLK 0
1364 case 0x0c: // VCLK 1
1365 case 0x0d: // VCLK 2
1366 case 0x0e: // VCLK 3
1367 case 0x0f: // DRAM Control
1368 case 0x13: // Graphics Cursor Pattern Address
1369 case 0x14: // Scratch Register 2
1370 case 0x15: // Scratch Register 3
1371 case 0x16: // Performance Tuning Register
1372 case 0x18: // Signature Generator Control
1373 case 0x19: // Signature Generator Result
1374 case 0x1a: // Signature Generator Result
1375 case 0x1b: // VCLK 0 Denominator & Post
1376 case 0x1c: // VCLK 1 Denominator & Post
1377 case 0x1d: // VCLK 2 Denominator & Post
1378 case 0x1e: // VCLK 3 Denominator & Post
1379 case 0x1f: // BIOS Write Enable and MCLK select
1380 s->vga.sr[s->vga.sr_index] = val;
1382 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1383 s->vga.sr_index, val);
1386 case 0x12: // Graphics Cursor Attribute
1387 s->vga.sr[0x12] = val;
1388 s->vga.force_shadow = !!(val & CIRRUS_CURSOR_SHOW);
1390 printf("cirrus: cursor ctl SR12=%02x (force shadow: %d)\n",
1391 val, s->vga.force_shadow);
1394 case 0x17: // Configuration Readback and Extended Control
1395 s->vga.sr[s->vga.sr_index] = (s->vga.sr[s->vga.sr_index] & 0x38)
1397 cirrus_update_memory_access(s);
1401 printf("cirrus: outport sr_index %02x, sr_value %02x\n",
1402 s->vga.sr_index, val);
1408 /***************************************
1410 * I/O access at 0x3c6
1412 ***************************************/
1414 static int cirrus_read_hidden_dac(CirrusVGAState * s)
1416 if (++s->cirrus_hidden_dac_lockindex == 5) {
1417 s->cirrus_hidden_dac_lockindex = 0;
1418 return s->cirrus_hidden_dac_data;
1423 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1425 if (s->cirrus_hidden_dac_lockindex == 4) {
1426 s->cirrus_hidden_dac_data = reg_value;
1427 #if defined(DEBUG_CIRRUS)
1428 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1431 s->cirrus_hidden_dac_lockindex = 0;
1434 /***************************************
1436 * I/O access at 0x3c9
1438 ***************************************/
1440 static int cirrus_vga_read_palette(CirrusVGAState * s)
1444 if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
1445 val = s->cirrus_hidden_palette[(s->vga.dac_read_index & 0x0f) * 3 +
1446 s->vga.dac_sub_index];
1448 val = s->vga.palette[s->vga.dac_read_index * 3 + s->vga.dac_sub_index];
1450 if (++s->vga.dac_sub_index == 3) {
1451 s->vga.dac_sub_index = 0;
1452 s->vga.dac_read_index++;
1457 static void cirrus_vga_write_palette(CirrusVGAState * s, int reg_value)
1459 s->vga.dac_cache[s->vga.dac_sub_index] = reg_value;
1460 if (++s->vga.dac_sub_index == 3) {
1461 if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
1462 memcpy(&s->cirrus_hidden_palette[(s->vga.dac_write_index & 0x0f) * 3],
1463 s->vga.dac_cache, 3);
1465 memcpy(&s->vga.palette[s->vga.dac_write_index * 3], s->vga.dac_cache, 3);
1467 /* XXX update cursor */
1468 s->vga.dac_sub_index = 0;
1469 s->vga.dac_write_index++;
1473 /***************************************
1475 * I/O access between 0x3ce-0x3cf
1477 ***************************************/
1479 static int cirrus_vga_read_gr(CirrusVGAState * s, unsigned reg_index)
1481 switch (reg_index) {
1482 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1483 return s->cirrus_shadow_gr0;
1484 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1485 return s->cirrus_shadow_gr1;
1486 case 0x02: // Standard VGA
1487 case 0x03: // Standard VGA
1488 case 0x04: // Standard VGA
1489 case 0x06: // Standard VGA
1490 case 0x07: // Standard VGA
1491 case 0x08: // Standard VGA
1492 return s->vga.gr[s->vga.gr_index];
1493 case 0x05: // Standard VGA, Cirrus extended mode
1498 if (reg_index < 0x3a) {
1499 return s->vga.gr[reg_index];
1502 printf("cirrus: inport gr_index %02x\n", reg_index);
1509 cirrus_vga_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1511 #if defined(DEBUG_BITBLT) && 0
1512 printf("gr%02x: %02x\n", reg_index, reg_value);
1514 switch (reg_index) {
1515 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1516 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1517 s->cirrus_shadow_gr0 = reg_value;
1519 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1520 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1521 s->cirrus_shadow_gr1 = reg_value;
1523 case 0x02: // Standard VGA
1524 case 0x03: // Standard VGA
1525 case 0x04: // Standard VGA
1526 case 0x06: // Standard VGA
1527 case 0x07: // Standard VGA
1528 case 0x08: // Standard VGA
1529 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1531 case 0x05: // Standard VGA, Cirrus extended mode
1532 s->vga.gr[reg_index] = reg_value & 0x7f;
1533 cirrus_update_memory_access(s);
1535 case 0x09: // bank offset #0
1536 case 0x0A: // bank offset #1
1537 s->vga.gr[reg_index] = reg_value;
1538 cirrus_update_bank_ptr(s, 0);
1539 cirrus_update_bank_ptr(s, 1);
1540 cirrus_update_memory_access(s);
1543 s->vga.gr[reg_index] = reg_value;
1544 cirrus_update_bank_ptr(s, 0);
1545 cirrus_update_bank_ptr(s, 1);
1546 cirrus_update_memory_access(s);
1548 case 0x10: // BGCOLOR 0x0000ff00
1549 case 0x11: // FGCOLOR 0x0000ff00
1550 case 0x12: // BGCOLOR 0x00ff0000
1551 case 0x13: // FGCOLOR 0x00ff0000
1552 case 0x14: // BGCOLOR 0xff000000
1553 case 0x15: // FGCOLOR 0xff000000
1554 case 0x20: // BLT WIDTH 0x0000ff
1555 case 0x22: // BLT HEIGHT 0x0000ff
1556 case 0x24: // BLT DEST PITCH 0x0000ff
1557 case 0x26: // BLT SRC PITCH 0x0000ff
1558 case 0x28: // BLT DEST ADDR 0x0000ff
1559 case 0x29: // BLT DEST ADDR 0x00ff00
1560 case 0x2c: // BLT SRC ADDR 0x0000ff
1561 case 0x2d: // BLT SRC ADDR 0x00ff00
1562 case 0x2f: // BLT WRITEMASK
1563 case 0x30: // BLT MODE
1564 case 0x32: // RASTER OP
1565 case 0x33: // BLT MODEEXT
1566 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1567 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1568 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1569 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1570 s->vga.gr[reg_index] = reg_value;
1572 case 0x21: // BLT WIDTH 0x001f00
1573 case 0x23: // BLT HEIGHT 0x001f00
1574 case 0x25: // BLT DEST PITCH 0x001f00
1575 case 0x27: // BLT SRC PITCH 0x001f00
1576 s->vga.gr[reg_index] = reg_value & 0x1f;
1578 case 0x2a: // BLT DEST ADDR 0x3f0000
1579 s->vga.gr[reg_index] = reg_value & 0x3f;
1580 /* if auto start mode, starts bit blt now */
1581 if (s->vga.gr[0x31] & CIRRUS_BLT_AUTOSTART) {
1582 cirrus_bitblt_start(s);
1585 case 0x2e: // BLT SRC ADDR 0x3f0000
1586 s->vga.gr[reg_index] = reg_value & 0x3f;
1588 case 0x31: // BLT STATUS/START
1589 cirrus_write_bitblt(s, reg_value);
1593 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1600 /***************************************
1602 * I/O access between 0x3d4-0x3d5
1604 ***************************************/
1606 static int cirrus_vga_read_cr(CirrusVGAState * s, unsigned reg_index)
1608 switch (reg_index) {
1609 case 0x00: // Standard VGA
1610 case 0x01: // Standard VGA
1611 case 0x02: // Standard VGA
1612 case 0x03: // Standard VGA
1613 case 0x04: // Standard VGA
1614 case 0x05: // Standard VGA
1615 case 0x06: // Standard VGA
1616 case 0x07: // Standard VGA
1617 case 0x08: // Standard VGA
1618 case 0x09: // Standard VGA
1619 case 0x0a: // Standard VGA
1620 case 0x0b: // Standard VGA
1621 case 0x0c: // Standard VGA
1622 case 0x0d: // Standard VGA
1623 case 0x0e: // Standard VGA
1624 case 0x0f: // Standard VGA
1625 case 0x10: // Standard VGA
1626 case 0x11: // Standard VGA
1627 case 0x12: // Standard VGA
1628 case 0x13: // Standard VGA
1629 case 0x14: // Standard VGA
1630 case 0x15: // Standard VGA
1631 case 0x16: // Standard VGA
1632 case 0x17: // Standard VGA
1633 case 0x18: // Standard VGA
1634 return s->vga.cr[s->vga.cr_index];
1635 case 0x24: // Attribute Controller Toggle Readback (R)
1636 return (s->vga.ar_flip_flop << 7);
1637 case 0x19: // Interlace End
1638 case 0x1a: // Miscellaneous Control
1639 case 0x1b: // Extended Display Control
1640 case 0x1c: // Sync Adjust and Genlock
1641 case 0x1d: // Overlay Extended Control
1642 case 0x22: // Graphics Data Latches Readback (R)
1643 case 0x25: // Part Status
1644 case 0x27: // Part ID (R)
1645 return s->vga.cr[s->vga.cr_index];
1646 case 0x26: // Attribute Controller Index Readback (R)
1647 return s->vga.ar_index & 0x3f;
1651 printf("cirrus: inport cr_index %02x\n", reg_index);
1657 static void cirrus_vga_write_cr(CirrusVGAState * s, int reg_value)
1659 switch (s->vga.cr_index) {
1660 case 0x00: // Standard VGA
1661 case 0x01: // Standard VGA
1662 case 0x02: // Standard VGA
1663 case 0x03: // Standard VGA
1664 case 0x04: // Standard VGA
1665 case 0x05: // Standard VGA
1666 case 0x06: // Standard VGA
1667 case 0x07: // Standard VGA
1668 case 0x08: // Standard VGA
1669 case 0x09: // Standard VGA
1670 case 0x0a: // Standard VGA
1671 case 0x0b: // Standard VGA
1672 case 0x0c: // Standard VGA
1673 case 0x0d: // Standard VGA
1674 case 0x0e: // Standard VGA
1675 case 0x0f: // Standard VGA
1676 case 0x10: // Standard VGA
1677 case 0x11: // Standard VGA
1678 case 0x12: // Standard VGA
1679 case 0x13: // Standard VGA
1680 case 0x14: // Standard VGA
1681 case 0x15: // Standard VGA
1682 case 0x16: // Standard VGA
1683 case 0x17: // Standard VGA
1684 case 0x18: // Standard VGA
1685 /* handle CR0-7 protection */
1686 if ((s->vga.cr[0x11] & 0x80) && s->vga.cr_index <= 7) {
1687 /* can always write bit 4 of CR7 */
1688 if (s->vga.cr_index == 7)
1689 s->vga.cr[7] = (s->vga.cr[7] & ~0x10) | (reg_value & 0x10);
1692 s->vga.cr[s->vga.cr_index] = reg_value;
1693 switch(s->vga.cr_index) {
1701 s->vga.update_retrace_info(&s->vga);
1705 case 0x19: // Interlace End
1706 case 0x1a: // Miscellaneous Control
1707 case 0x1b: // Extended Display Control
1708 case 0x1c: // Sync Adjust and Genlock
1709 case 0x1d: // Overlay Extended Control
1710 s->vga.cr[s->vga.cr_index] = reg_value;
1712 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1713 s->vga.cr_index, reg_value);
1716 case 0x22: // Graphics Data Latches Readback (R)
1717 case 0x24: // Attribute Controller Toggle Readback (R)
1718 case 0x26: // Attribute Controller Index Readback (R)
1719 case 0x27: // Part ID (R)
1721 case 0x25: // Part Status
1724 printf("cirrus: outport cr_index %02x, cr_value %02x\n",
1725 s->vga.cr_index, reg_value);
1731 /***************************************
1733 * memory-mapped I/O (bitblt)
1735 ***************************************/
1737 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1742 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1743 value = cirrus_vga_read_gr(s, 0x00);
1745 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1746 value = cirrus_vga_read_gr(s, 0x10);
1748 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1749 value = cirrus_vga_read_gr(s, 0x12);
1751 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1752 value = cirrus_vga_read_gr(s, 0x14);
1754 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1755 value = cirrus_vga_read_gr(s, 0x01);
1757 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1758 value = cirrus_vga_read_gr(s, 0x11);
1760 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1761 value = cirrus_vga_read_gr(s, 0x13);
1763 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1764 value = cirrus_vga_read_gr(s, 0x15);
1766 case (CIRRUS_MMIO_BLTWIDTH + 0):
1767 value = cirrus_vga_read_gr(s, 0x20);
1769 case (CIRRUS_MMIO_BLTWIDTH + 1):
1770 value = cirrus_vga_read_gr(s, 0x21);
1772 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1773 value = cirrus_vga_read_gr(s, 0x22);
1775 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1776 value = cirrus_vga_read_gr(s, 0x23);
1778 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1779 value = cirrus_vga_read_gr(s, 0x24);
1781 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1782 value = cirrus_vga_read_gr(s, 0x25);
1784 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1785 value = cirrus_vga_read_gr(s, 0x26);
1787 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1788 value = cirrus_vga_read_gr(s, 0x27);
1790 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1791 value = cirrus_vga_read_gr(s, 0x28);
1793 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1794 value = cirrus_vga_read_gr(s, 0x29);
1796 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1797 value = cirrus_vga_read_gr(s, 0x2a);
1799 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1800 value = cirrus_vga_read_gr(s, 0x2c);
1802 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1803 value = cirrus_vga_read_gr(s, 0x2d);
1805 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1806 value = cirrus_vga_read_gr(s, 0x2e);
1808 case CIRRUS_MMIO_BLTWRITEMASK:
1809 value = cirrus_vga_read_gr(s, 0x2f);
1811 case CIRRUS_MMIO_BLTMODE:
1812 value = cirrus_vga_read_gr(s, 0x30);
1814 case CIRRUS_MMIO_BLTROP:
1815 value = cirrus_vga_read_gr(s, 0x32);
1817 case CIRRUS_MMIO_BLTMODEEXT:
1818 value = cirrus_vga_read_gr(s, 0x33);
1820 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1821 value = cirrus_vga_read_gr(s, 0x34);
1823 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1824 value = cirrus_vga_read_gr(s, 0x35);
1826 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1827 value = cirrus_vga_read_gr(s, 0x38);
1829 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1830 value = cirrus_vga_read_gr(s, 0x39);
1832 case CIRRUS_MMIO_BLTSTATUS:
1833 value = cirrus_vga_read_gr(s, 0x31);
1837 printf("cirrus: mmio read - address 0x%04x\n", address);
1842 trace_vga_cirrus_write_blt(address, value);
1843 return (uint8_t) value;
1846 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1849 trace_vga_cirrus_write_blt(address, value);
1851 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1852 cirrus_vga_write_gr(s, 0x00, value);
1854 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1855 cirrus_vga_write_gr(s, 0x10, value);
1857 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1858 cirrus_vga_write_gr(s, 0x12, value);
1860 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1861 cirrus_vga_write_gr(s, 0x14, value);
1863 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1864 cirrus_vga_write_gr(s, 0x01, value);
1866 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1867 cirrus_vga_write_gr(s, 0x11, value);
1869 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1870 cirrus_vga_write_gr(s, 0x13, value);
1872 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1873 cirrus_vga_write_gr(s, 0x15, value);
1875 case (CIRRUS_MMIO_BLTWIDTH + 0):
1876 cirrus_vga_write_gr(s, 0x20, value);
1878 case (CIRRUS_MMIO_BLTWIDTH + 1):
1879 cirrus_vga_write_gr(s, 0x21, value);
1881 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1882 cirrus_vga_write_gr(s, 0x22, value);
1884 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1885 cirrus_vga_write_gr(s, 0x23, value);
1887 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1888 cirrus_vga_write_gr(s, 0x24, value);
1890 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1891 cirrus_vga_write_gr(s, 0x25, value);
1893 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1894 cirrus_vga_write_gr(s, 0x26, value);
1896 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1897 cirrus_vga_write_gr(s, 0x27, value);
1899 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1900 cirrus_vga_write_gr(s, 0x28, value);
1902 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1903 cirrus_vga_write_gr(s, 0x29, value);
1905 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1906 cirrus_vga_write_gr(s, 0x2a, value);
1908 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1911 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1912 cirrus_vga_write_gr(s, 0x2c, value);
1914 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1915 cirrus_vga_write_gr(s, 0x2d, value);
1917 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1918 cirrus_vga_write_gr(s, 0x2e, value);
1920 case CIRRUS_MMIO_BLTWRITEMASK:
1921 cirrus_vga_write_gr(s, 0x2f, value);
1923 case CIRRUS_MMIO_BLTMODE:
1924 cirrus_vga_write_gr(s, 0x30, value);
1926 case CIRRUS_MMIO_BLTROP:
1927 cirrus_vga_write_gr(s, 0x32, value);
1929 case CIRRUS_MMIO_BLTMODEEXT:
1930 cirrus_vga_write_gr(s, 0x33, value);
1932 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1933 cirrus_vga_write_gr(s, 0x34, value);
1935 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1936 cirrus_vga_write_gr(s, 0x35, value);
1938 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1939 cirrus_vga_write_gr(s, 0x38, value);
1941 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1942 cirrus_vga_write_gr(s, 0x39, value);
1944 case CIRRUS_MMIO_BLTSTATUS:
1945 cirrus_vga_write_gr(s, 0x31, value);
1949 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1956 /***************************************
1960 ***************************************/
1962 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1968 unsigned val = mem_value;
1971 for (x = 0; x < 8; x++) {
1972 dst = s->vga.vram_ptr + ((offset + x) & s->cirrus_addr_mask);
1974 *dst = s->cirrus_shadow_gr1;
1975 } else if (mode == 5) {
1976 *dst = s->cirrus_shadow_gr0;
1980 memory_region_set_dirty(&s->vga.vram, offset, 8);
1983 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1989 unsigned val = mem_value;
1992 for (x = 0; x < 8; x++) {
1993 dst = s->vga.vram_ptr + ((offset + 2 * x) & s->cirrus_addr_mask & ~1);
1995 *dst = s->cirrus_shadow_gr1;
1996 *(dst + 1) = s->vga.gr[0x11];
1997 } else if (mode == 5) {
1998 *dst = s->cirrus_shadow_gr0;
1999 *(dst + 1) = s->vga.gr[0x10];
2003 memory_region_set_dirty(&s->vga.vram, offset, 16);
2006 /***************************************
2008 * memory access between 0xa0000-0xbffff
2010 ***************************************/
2012 static uint64_t cirrus_vga_mem_read(void *opaque,
2016 CirrusVGAState *s = opaque;
2017 unsigned bank_index;
2018 unsigned bank_offset;
2021 if ((s->vga.sr[0x07] & 0x01) == 0) {
2022 return vga_mem_readb(&s->vga, addr);
2025 if (addr < 0x10000) {
2026 /* XXX handle bitblt */
2028 bank_index = addr >> 15;
2029 bank_offset = addr & 0x7fff;
2030 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2031 bank_offset += s->cirrus_bank_base[bank_index];
2032 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2034 } else if (s->vga.gr[0x0B] & 0x02) {
2037 bank_offset &= s->cirrus_addr_mask;
2038 val = *(s->vga.vram_ptr + bank_offset);
2041 } else if (addr >= 0x18000 && addr < 0x18100) {
2042 /* memory-mapped I/O */
2044 if ((s->vga.sr[0x17] & 0x44) == 0x04) {
2045 val = cirrus_mmio_blt_read(s, addr & 0xff);
2050 printf("cirrus: mem_readb " TARGET_FMT_plx "\n", addr);
2056 static void cirrus_vga_mem_write(void *opaque,
2061 CirrusVGAState *s = opaque;
2062 unsigned bank_index;
2063 unsigned bank_offset;
2066 if ((s->vga.sr[0x07] & 0x01) == 0) {
2067 vga_mem_writeb(&s->vga, addr, mem_value);
2071 if (addr < 0x10000) {
2072 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2074 *s->cirrus_srcptr++ = (uint8_t) mem_value;
2075 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2076 cirrus_bitblt_cputovideo_next(s);
2080 bank_index = addr >> 15;
2081 bank_offset = addr & 0x7fff;
2082 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2083 bank_offset += s->cirrus_bank_base[bank_index];
2084 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2086 } else if (s->vga.gr[0x0B] & 0x02) {
2089 bank_offset &= s->cirrus_addr_mask;
2090 mode = s->vga.gr[0x05] & 0x7;
2091 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2092 *(s->vga.vram_ptr + bank_offset) = mem_value;
2093 memory_region_set_dirty(&s->vga.vram, bank_offset,
2096 if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
2097 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
2101 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2108 } else if (addr >= 0x18000 && addr < 0x18100) {
2109 /* memory-mapped I/O */
2110 if ((s->vga.sr[0x17] & 0x44) == 0x04) {
2111 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2115 printf("cirrus: mem_writeb " TARGET_FMT_plx " value 0x%02" PRIu64 "\n", addr,
2121 static const MemoryRegionOps cirrus_vga_mem_ops = {
2122 .read = cirrus_vga_mem_read,
2123 .write = cirrus_vga_mem_write,
2124 .endianness = DEVICE_LITTLE_ENDIAN,
2126 .min_access_size = 1,
2127 .max_access_size = 1,
2131 /***************************************
2135 ***************************************/
2137 static inline void invalidate_cursor1(CirrusVGAState *s)
2139 if (s->last_hw_cursor_size) {
2140 vga_invalidate_scanlines(&s->vga,
2141 s->last_hw_cursor_y + s->last_hw_cursor_y_start,
2142 s->last_hw_cursor_y + s->last_hw_cursor_y_end);
2146 static inline void cirrus_cursor_compute_yrange(CirrusVGAState *s)
2150 int y, y_min, y_max;
2152 src = s->vga.vram_ptr + s->real_vram_size - 16 * KiB;
2153 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2154 src += (s->vga.sr[0x13] & 0x3c) * 256;
2157 for(y = 0; y < 64; y++) {
2158 content = ((uint32_t *)src)[0] |
2159 ((uint32_t *)src)[1] |
2160 ((uint32_t *)src)[2] |
2161 ((uint32_t *)src)[3];
2171 src += (s->vga.sr[0x13] & 0x3f) * 256;
2174 for(y = 0; y < 32; y++) {
2175 content = ((uint32_t *)src)[0] |
2176 ((uint32_t *)(src + 128))[0];
2186 if (y_min > y_max) {
2187 s->last_hw_cursor_y_start = 0;
2188 s->last_hw_cursor_y_end = 0;
2190 s->last_hw_cursor_y_start = y_min;
2191 s->last_hw_cursor_y_end = y_max + 1;
2195 /* NOTE: we do not currently handle the cursor bitmap change, so we
2196 update the cursor only if it moves. */
2197 static void cirrus_cursor_invalidate(VGACommonState *s1)
2199 CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
2202 if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW)) {
2205 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE)
2210 /* invalidate last cursor and new cursor if any change */
2211 if (s->last_hw_cursor_size != size ||
2212 s->last_hw_cursor_x != s->vga.hw_cursor_x ||
2213 s->last_hw_cursor_y != s->vga.hw_cursor_y) {
2215 invalidate_cursor1(s);
2217 s->last_hw_cursor_size = size;
2218 s->last_hw_cursor_x = s->vga.hw_cursor_x;
2219 s->last_hw_cursor_y = s->vga.hw_cursor_y;
2220 /* compute the real cursor min and max y */
2221 cirrus_cursor_compute_yrange(s);
2222 invalidate_cursor1(s);
2226 static void vga_draw_cursor_line(uint8_t *d1,
2227 const uint8_t *src1,
2229 unsigned int color0,
2230 unsigned int color1,
2231 unsigned int color_xor)
2233 const uint8_t *plane0, *plane1;
2239 plane1 = src1 + poffset;
2240 for (x = 0; x < w; x++) {
2241 b0 = (plane0[x >> 3] >> (7 - (x & 7))) & 1;
2242 b1 = (plane1[x >> 3] >> (7 - (x & 7))) & 1;
2243 switch (b0 | (b1 << 1)) {
2247 ((uint32_t *)d)[0] ^= color_xor;
2250 ((uint32_t *)d)[0] = color0;
2253 ((uint32_t *)d)[0] = color1;
2260 static void cirrus_cursor_draw_line(VGACommonState *s1, uint8_t *d1, int scr_y)
2262 CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
2263 int w, h, x1, x2, poffset;
2264 unsigned int color0, color1;
2265 const uint8_t *palette, *src;
2268 if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW))
2270 /* fast test to see if the cursor intersects with the scan line */
2271 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2276 if (scr_y < s->vga.hw_cursor_y ||
2277 scr_y >= (s->vga.hw_cursor_y + h)) {
2281 src = s->vga.vram_ptr + s->real_vram_size - 16 * KiB;
2282 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2283 src += (s->vga.sr[0x13] & 0x3c) * 256;
2284 src += (scr_y - s->vga.hw_cursor_y) * 16;
2286 content = ((uint32_t *)src)[0] |
2287 ((uint32_t *)src)[1] |
2288 ((uint32_t *)src)[2] |
2289 ((uint32_t *)src)[3];
2291 src += (s->vga.sr[0x13] & 0x3f) * 256;
2292 src += (scr_y - s->vga.hw_cursor_y) * 4;
2296 content = ((uint32_t *)src)[0] |
2297 ((uint32_t *)(src + 128))[0];
2299 /* if nothing to draw, no need to continue */
2304 x1 = s->vga.hw_cursor_x;
2305 if (x1 >= s->vga.last_scr_width)
2307 x2 = s->vga.hw_cursor_x + w;
2308 if (x2 > s->vga.last_scr_width)
2309 x2 = s->vga.last_scr_width;
2311 palette = s->cirrus_hidden_palette;
2312 color0 = rgb_to_pixel32(c6_to_8(palette[0x0 * 3]),
2313 c6_to_8(palette[0x0 * 3 + 1]),
2314 c6_to_8(palette[0x0 * 3 + 2]));
2315 color1 = rgb_to_pixel32(c6_to_8(palette[0xf * 3]),
2316 c6_to_8(palette[0xf * 3 + 1]),
2317 c6_to_8(palette[0xf * 3 + 2]));
2319 vga_draw_cursor_line(d1, src, poffset, w, color0, color1, 0xffffff);
2322 /***************************************
2326 ***************************************/
2328 static uint64_t cirrus_linear_read(void *opaque, hwaddr addr,
2331 CirrusVGAState *s = opaque;
2334 addr &= s->cirrus_addr_mask;
2336 if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
2337 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2338 /* memory-mapped I/O */
2339 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2341 /* XXX handle bitblt */
2345 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2347 } else if (s->vga.gr[0x0B] & 0x02) {
2350 addr &= s->cirrus_addr_mask;
2351 ret = *(s->vga.vram_ptr + addr);
2357 static void cirrus_linear_write(void *opaque, hwaddr addr,
2358 uint64_t val, unsigned size)
2360 CirrusVGAState *s = opaque;
2363 addr &= s->cirrus_addr_mask;
2365 if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
2366 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2367 /* memory-mapped I/O */
2368 cirrus_mmio_blt_write(s, addr & 0xff, val);
2369 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2371 *s->cirrus_srcptr++ = (uint8_t) val;
2372 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2373 cirrus_bitblt_cputovideo_next(s);
2377 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2379 } else if (s->vga.gr[0x0B] & 0x02) {
2382 addr &= s->cirrus_addr_mask;
2384 mode = s->vga.gr[0x05] & 0x7;
2385 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2386 *(s->vga.vram_ptr + addr) = (uint8_t) val;
2387 memory_region_set_dirty(&s->vga.vram, addr, 1);
2389 if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
2390 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2392 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2398 /***************************************
2400 * system to screen memory access
2402 ***************************************/
2405 static uint64_t cirrus_linear_bitblt_read(void *opaque,
2409 CirrusVGAState *s = opaque;
2412 /* XXX handle bitblt */
2418 static void cirrus_linear_bitblt_write(void *opaque,
2423 CirrusVGAState *s = opaque;
2425 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2427 *s->cirrus_srcptr++ = (uint8_t) val;
2428 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2429 cirrus_bitblt_cputovideo_next(s);
2434 static const MemoryRegionOps cirrus_linear_bitblt_io_ops = {
2435 .read = cirrus_linear_bitblt_read,
2436 .write = cirrus_linear_bitblt_write,
2437 .endianness = DEVICE_LITTLE_ENDIAN,
2439 .min_access_size = 1,
2440 .max_access_size = 1,
2444 static void map_linear_vram_bank(CirrusVGAState *s, unsigned bank)
2446 MemoryRegion *mr = &s->cirrus_bank[bank];
2447 bool enabled = !(s->cirrus_srcptr != s->cirrus_srcptr_end)
2448 && !((s->vga.sr[0x07] & 0x01) == 0)
2449 && !((s->vga.gr[0x0B] & 0x14) == 0x14)
2450 && !(s->vga.gr[0x0B] & 0x02);
2452 memory_region_set_enabled(mr, enabled);
2453 memory_region_set_alias_offset(mr, s->cirrus_bank_base[bank]);
2456 static void map_linear_vram(CirrusVGAState *s)
2458 if (s->bustype == CIRRUS_BUSTYPE_PCI && !s->linear_vram) {
2459 s->linear_vram = true;
2460 memory_region_add_subregion_overlap(&s->pci_bar, 0, &s->vga.vram, 1);
2462 map_linear_vram_bank(s, 0);
2463 map_linear_vram_bank(s, 1);
2466 static void unmap_linear_vram(CirrusVGAState *s)
2468 if (s->bustype == CIRRUS_BUSTYPE_PCI && s->linear_vram) {
2469 s->linear_vram = false;
2470 memory_region_del_subregion(&s->pci_bar, &s->vga.vram);
2472 memory_region_set_enabled(&s->cirrus_bank[0], false);
2473 memory_region_set_enabled(&s->cirrus_bank[1], false);
2476 /* Compute the memory access functions */
2477 static void cirrus_update_memory_access(CirrusVGAState *s)
2481 memory_region_transaction_begin();
2482 if ((s->vga.sr[0x17] & 0x44) == 0x44) {
2484 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2487 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2489 } else if (s->vga.gr[0x0B] & 0x02) {
2493 mode = s->vga.gr[0x05] & 0x7;
2494 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2498 unmap_linear_vram(s);
2501 memory_region_transaction_commit();
2507 static uint64_t cirrus_vga_ioport_read(void *opaque, hwaddr addr,
2510 CirrusVGAState *c = opaque;
2511 VGACommonState *s = &c->vga;
2516 if (vga_ioport_invalid(s, addr)) {
2521 if (s->ar_flip_flop == 0) {
2528 index = s->ar_index & 0x1f;
2541 val = cirrus_vga_read_sr(c);
2543 #ifdef DEBUG_VGA_REG
2544 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2548 val = cirrus_read_hidden_dac(c);
2554 val = s->dac_write_index;
2555 c->cirrus_hidden_dac_lockindex = 0;
2558 val = cirrus_vga_read_palette(c);
2570 val = cirrus_vga_read_gr(c, s->gr_index);
2571 #ifdef DEBUG_VGA_REG
2572 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2581 val = cirrus_vga_read_cr(c, s->cr_index);
2582 #ifdef DEBUG_VGA_REG
2583 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2588 /* just toggle to fool polling */
2589 val = s->st01 = s->retrace(s);
2590 s->ar_flip_flop = 0;
2597 trace_vga_cirrus_read_io(addr, val);
2601 static void cirrus_vga_ioport_write(void *opaque, hwaddr addr, uint64_t val,
2604 CirrusVGAState *c = opaque;
2605 VGACommonState *s = &c->vga;
2610 /* check port range access depending on color/monochrome mode */
2611 if (vga_ioport_invalid(s, addr)) {
2614 trace_vga_cirrus_write_io(addr, val);
2618 if (s->ar_flip_flop == 0) {
2622 index = s->ar_index & 0x1f;
2625 s->ar[index] = val & 0x3f;
2628 s->ar[index] = val & ~0x10;
2634 s->ar[index] = val & ~0xc0;
2637 s->ar[index] = val & ~0xf0;
2640 s->ar[index] = val & ~0xf0;
2646 s->ar_flip_flop ^= 1;
2649 s->msr = val & ~0x10;
2650 s->update_retrace_info(s);
2656 #ifdef DEBUG_VGA_REG
2657 printf("vga: write SR%x = 0x%02" PRIu64 "\n", s->sr_index, val);
2659 cirrus_vga_write_sr(c, val);
2662 cirrus_write_hidden_dac(c, val);
2665 s->dac_read_index = val;
2666 s->dac_sub_index = 0;
2670 s->dac_write_index = val;
2671 s->dac_sub_index = 0;
2675 cirrus_vga_write_palette(c, val);
2681 #ifdef DEBUG_VGA_REG
2682 printf("vga: write GR%x = 0x%02" PRIu64 "\n", s->gr_index, val);
2684 cirrus_vga_write_gr(c, s->gr_index, val);
2692 #ifdef DEBUG_VGA_REG
2693 printf("vga: write CR%x = 0x%02"PRIu64"\n", s->cr_index, val);
2695 cirrus_vga_write_cr(c, val);
2699 s->fcr = val & 0x10;
2704 /***************************************
2706 * memory-mapped I/O access
2708 ***************************************/
2710 static uint64_t cirrus_mmio_read(void *opaque, hwaddr addr,
2713 CirrusVGAState *s = opaque;
2715 if (addr >= 0x100) {
2716 return cirrus_mmio_blt_read(s, addr - 0x100);
2718 return cirrus_vga_ioport_read(s, addr + 0x10, size);
2722 static void cirrus_mmio_write(void *opaque, hwaddr addr,
2723 uint64_t val, unsigned size)
2725 CirrusVGAState *s = opaque;
2727 if (addr >= 0x100) {
2728 cirrus_mmio_blt_write(s, addr - 0x100, val);
2730 cirrus_vga_ioport_write(s, addr + 0x10, val, size);
2734 static const MemoryRegionOps cirrus_mmio_io_ops = {
2735 .read = cirrus_mmio_read,
2736 .write = cirrus_mmio_write,
2737 .endianness = DEVICE_LITTLE_ENDIAN,
2739 .min_access_size = 1,
2740 .max_access_size = 1,
2744 /* load/save state */
2746 static int cirrus_post_load(void *opaque, int version_id)
2748 CirrusVGAState *s = opaque;
2750 s->vga.gr[0x00] = s->cirrus_shadow_gr0 & 0x0f;
2751 s->vga.gr[0x01] = s->cirrus_shadow_gr1 & 0x0f;
2753 cirrus_update_bank_ptr(s, 0);
2754 cirrus_update_bank_ptr(s, 1);
2755 cirrus_update_memory_access(s);
2757 s->vga.graphic_mode = -1;
2762 const VMStateDescription vmstate_cirrus_vga = {
2763 .name = "cirrus_vga",
2765 .minimum_version_id = 1,
2766 .post_load = cirrus_post_load,
2767 .fields = (VMStateField[]) {
2768 VMSTATE_UINT32(vga.latch, CirrusVGAState),
2769 VMSTATE_UINT8(vga.sr_index, CirrusVGAState),
2770 VMSTATE_BUFFER(vga.sr, CirrusVGAState),
2771 VMSTATE_UINT8(vga.gr_index, CirrusVGAState),
2772 VMSTATE_UINT8(cirrus_shadow_gr0, CirrusVGAState),
2773 VMSTATE_UINT8(cirrus_shadow_gr1, CirrusVGAState),
2774 VMSTATE_BUFFER_START_MIDDLE(vga.gr, CirrusVGAState, 2),
2775 VMSTATE_UINT8(vga.ar_index, CirrusVGAState),
2776 VMSTATE_BUFFER(vga.ar, CirrusVGAState),
2777 VMSTATE_INT32(vga.ar_flip_flop, CirrusVGAState),
2778 VMSTATE_UINT8(vga.cr_index, CirrusVGAState),
2779 VMSTATE_BUFFER(vga.cr, CirrusVGAState),
2780 VMSTATE_UINT8(vga.msr, CirrusVGAState),
2781 VMSTATE_UINT8(vga.fcr, CirrusVGAState),
2782 VMSTATE_UINT8(vga.st00, CirrusVGAState),
2783 VMSTATE_UINT8(vga.st01, CirrusVGAState),
2784 VMSTATE_UINT8(vga.dac_state, CirrusVGAState),
2785 VMSTATE_UINT8(vga.dac_sub_index, CirrusVGAState),
2786 VMSTATE_UINT8(vga.dac_read_index, CirrusVGAState),
2787 VMSTATE_UINT8(vga.dac_write_index, CirrusVGAState),
2788 VMSTATE_BUFFER(vga.dac_cache, CirrusVGAState),
2789 VMSTATE_BUFFER(vga.palette, CirrusVGAState),
2790 VMSTATE_INT32(vga.bank_offset, CirrusVGAState),
2791 VMSTATE_UINT8(cirrus_hidden_dac_lockindex, CirrusVGAState),
2792 VMSTATE_UINT8(cirrus_hidden_dac_data, CirrusVGAState),
2793 VMSTATE_UINT32(vga.hw_cursor_x, CirrusVGAState),
2794 VMSTATE_UINT32(vga.hw_cursor_y, CirrusVGAState),
2795 /* XXX: we do not save the bitblt state - we assume we do not save
2796 the state when the blitter is active */
2797 VMSTATE_END_OF_LIST()
2801 static const VMStateDescription vmstate_pci_cirrus_vga = {
2802 .name = "cirrus_vga",
2804 .minimum_version_id = 2,
2805 .fields = (VMStateField[]) {
2806 VMSTATE_PCI_DEVICE(dev, PCICirrusVGAState),
2807 VMSTATE_STRUCT(cirrus_vga, PCICirrusVGAState, 0,
2808 vmstate_cirrus_vga, CirrusVGAState),
2809 VMSTATE_END_OF_LIST()
2813 /***************************************
2817 ***************************************/
2819 static void cirrus_reset(void *opaque)
2821 CirrusVGAState *s = opaque;
2823 vga_common_reset(&s->vga);
2824 unmap_linear_vram(s);
2825 s->vga.sr[0x06] = 0x0f;
2826 if (s->device_id == CIRRUS_ID_CLGD5446) {
2827 /* 4MB 64 bit memory config, always PCI */
2828 s->vga.sr[0x1F] = 0x2d; // MemClock
2829 s->vga.gr[0x18] = 0x0f; // fastest memory configuration
2830 s->vga.sr[0x0f] = 0x98;
2831 s->vga.sr[0x17] = 0x20;
2832 s->vga.sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */
2834 s->vga.sr[0x1F] = 0x22; // MemClock
2835 s->vga.sr[0x0F] = CIRRUS_MEMSIZE_2M;
2836 s->vga.sr[0x17] = s->bustype;
2837 s->vga.sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
2839 s->vga.cr[0x27] = s->device_id;
2841 s->cirrus_hidden_dac_lockindex = 5;
2842 s->cirrus_hidden_dac_data = 0;
2845 static const MemoryRegionOps cirrus_linear_io_ops = {
2846 .read = cirrus_linear_read,
2847 .write = cirrus_linear_write,
2848 .endianness = DEVICE_LITTLE_ENDIAN,
2850 .min_access_size = 1,
2851 .max_access_size = 1,
2855 static const MemoryRegionOps cirrus_vga_io_ops = {
2856 .read = cirrus_vga_ioport_read,
2857 .write = cirrus_vga_ioport_write,
2858 .endianness = DEVICE_LITTLE_ENDIAN,
2860 .min_access_size = 1,
2861 .max_access_size = 1,
2865 void cirrus_init_common(CirrusVGAState *s, Object *owner,
2866 int device_id, int is_pci,
2867 MemoryRegion *system_memory, MemoryRegion *system_io)
2874 for(i = 0;i < 256; i++)
2875 rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */
2876 rop_to_index[CIRRUS_ROP_0] = 0;
2877 rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;
2878 rop_to_index[CIRRUS_ROP_NOP] = 2;
2879 rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;
2880 rop_to_index[CIRRUS_ROP_NOTDST] = 4;
2881 rop_to_index[CIRRUS_ROP_SRC] = 5;
2882 rop_to_index[CIRRUS_ROP_1] = 6;
2883 rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;
2884 rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;
2885 rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;
2886 rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;
2887 rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;
2888 rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;
2889 rop_to_index[CIRRUS_ROP_NOTSRC] = 13;
2890 rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;
2891 rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;
2892 s->device_id = device_id;
2894 s->bustype = CIRRUS_BUSTYPE_PCI;
2896 s->bustype = CIRRUS_BUSTYPE_ISA;
2899 /* Register ioport 0x3b0 - 0x3df */
2900 memory_region_init_io(&s->cirrus_vga_io, owner, &cirrus_vga_io_ops, s,
2902 memory_region_set_flush_coalesced(&s->cirrus_vga_io);
2903 memory_region_add_subregion(system_io, 0x3b0, &s->cirrus_vga_io);
2905 memory_region_init(&s->low_mem_container, owner,
2906 "cirrus-lowmem-container",
2909 memory_region_init_io(&s->low_mem, owner, &cirrus_vga_mem_ops, s,
2910 "cirrus-low-memory", 0x20000);
2911 memory_region_add_subregion(&s->low_mem_container, 0, &s->low_mem);
2912 for (i = 0; i < 2; ++i) {
2913 static const char *names[] = { "vga.bank0", "vga.bank1" };
2914 MemoryRegion *bank = &s->cirrus_bank[i];
2915 memory_region_init_alias(bank, owner, names[i], &s->vga.vram,
2917 memory_region_set_enabled(bank, false);
2918 memory_region_add_subregion_overlap(&s->low_mem_container, i * 0x8000,
2921 memory_region_add_subregion_overlap(system_memory,
2923 &s->low_mem_container,
2925 memory_region_set_coalescing(&s->low_mem);
2927 /* I/O handler for LFB */
2928 memory_region_init_io(&s->cirrus_linear_io, owner, &cirrus_linear_io_ops, s,
2929 "cirrus-linear-io", s->vga.vram_size_mb * MiB);
2930 memory_region_set_flush_coalesced(&s->cirrus_linear_io);
2932 /* I/O handler for LFB */
2933 memory_region_init_io(&s->cirrus_linear_bitblt_io, owner,
2934 &cirrus_linear_bitblt_io_ops,
2936 "cirrus-bitblt-mmio",
2938 memory_region_set_flush_coalesced(&s->cirrus_linear_bitblt_io);
2940 /* I/O handler for memory-mapped I/O */
2941 memory_region_init_io(&s->cirrus_mmio_io, owner, &cirrus_mmio_io_ops, s,
2942 "cirrus-mmio", CIRRUS_PNPMMIO_SIZE);
2943 memory_region_set_flush_coalesced(&s->cirrus_mmio_io);
2946 (s->device_id == CIRRUS_ID_CLGD5446) ? 4 * MiB : 2 * MiB;
2948 /* XXX: s->vga.vram_size must be a power of two */
2949 s->cirrus_addr_mask = s->real_vram_size - 1;
2950 s->linear_mmio_mask = s->real_vram_size - 256;
2952 s->vga.get_bpp = cirrus_get_bpp;
2953 s->vga.get_offsets = cirrus_get_offsets;
2954 s->vga.get_resolution = cirrus_get_resolution;
2955 s->vga.cursor_invalidate = cirrus_cursor_invalidate;
2956 s->vga.cursor_draw_line = cirrus_cursor_draw_line;
2958 qemu_register_reset(cirrus_reset, s);
2961 /***************************************
2965 ***************************************/
2967 static void pci_cirrus_vga_realize(PCIDevice *dev, Error **errp)
2969 PCICirrusVGAState *d = PCI_CIRRUS_VGA(dev);
2970 CirrusVGAState *s = &d->cirrus_vga;
2971 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
2972 int16_t device_id = pc->device_id;
2974 /* follow real hardware, cirrus card emulated has 4 MB video memory.
2975 Also accept 8 MB/16 MB for backward compatibility. */
2976 if (s->vga.vram_size_mb != 4 && s->vga.vram_size_mb != 8 &&
2977 s->vga.vram_size_mb != 16) {
2978 error_setg(errp, "Invalid cirrus_vga ram size '%u'",
2979 s->vga.vram_size_mb);
2983 vga_common_init(&s->vga, OBJECT(dev));
2984 cirrus_init_common(s, OBJECT(dev), device_id, 1, pci_address_space(dev),
2985 pci_address_space_io(dev));
2986 s->vga.con = graphic_console_init(DEVICE(dev), 0, s->vga.hw_ops, &s->vga);
2990 memory_region_init(&s->pci_bar, OBJECT(dev), "cirrus-pci-bar0", 0x2000000);
2992 /* XXX: add byte swapping apertures */
2993 memory_region_add_subregion(&s->pci_bar, 0, &s->cirrus_linear_io);
2994 memory_region_add_subregion(&s->pci_bar, 0x1000000,
2995 &s->cirrus_linear_bitblt_io);
2997 /* setup memory space */
2999 /* memory #1 memory-mapped I/O */
3000 /* XXX: s->vga.vram_size must be a power of two */
3001 pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->pci_bar);
3002 if (device_id == CIRRUS_ID_CLGD5446) {
3003 pci_register_bar(&d->dev, 1, 0, &s->cirrus_mmio_io);
3007 static Property pci_vga_cirrus_properties[] = {
3008 DEFINE_PROP_UINT32("vgamem_mb", struct PCICirrusVGAState,
3009 cirrus_vga.vga.vram_size_mb, 4),
3010 DEFINE_PROP_BOOL("blitter", struct PCICirrusVGAState,
3011 cirrus_vga.enable_blitter, true),
3012 DEFINE_PROP_BOOL("global-vmstate", struct PCICirrusVGAState,
3013 cirrus_vga.vga.global_vmstate, false),
3014 DEFINE_PROP_END_OF_LIST(),
3017 static void cirrus_vga_class_init(ObjectClass *klass, void *data)
3019 DeviceClass *dc = DEVICE_CLASS(klass);
3020 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
3022 k->realize = pci_cirrus_vga_realize;
3023 k->romfile = VGABIOS_CIRRUS_FILENAME;
3024 k->vendor_id = PCI_VENDOR_ID_CIRRUS;
3025 k->device_id = CIRRUS_ID_CLGD5446;
3026 k->class_id = PCI_CLASS_DISPLAY_VGA;
3027 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
3028 dc->desc = "Cirrus CLGD 54xx VGA";
3029 dc->vmsd = &vmstate_pci_cirrus_vga;
3030 dc->props = pci_vga_cirrus_properties;
3031 dc->hotpluggable = false;
3034 static const TypeInfo cirrus_vga_info = {
3035 .name = TYPE_PCI_CIRRUS_VGA,
3036 .parent = TYPE_PCI_DEVICE,
3037 .instance_size = sizeof(PCICirrusVGAState),
3038 .class_init = cirrus_vga_class_init,
3039 .interfaces = (InterfaceInfo[]) {
3040 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
3045 static void cirrus_vga_register_types(void)
3047 type_register_static(&cirrus_vga_info);
3050 type_init(cirrus_vga_register_types)