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
27 * available at http://home.worldonline.dk/~finth/
32 //#define DEBUG_CIRRUS
33 //#define DEBUG_BITBLT
35 /***************************************
39 ***************************************/
41 #define qemu_MIN(a,b) ((a) < (b) ? (a) : (b))
44 #define CIRRUS_ID_CLGD5422 (0x23<<2)
45 #define CIRRUS_ID_CLGD5426 (0x24<<2)
46 #define CIRRUS_ID_CLGD5424 (0x25<<2)
47 #define CIRRUS_ID_CLGD5428 (0x26<<2)
48 #define CIRRUS_ID_CLGD5430 (0x28<<2)
49 #define CIRRUS_ID_CLGD5434 (0x2A<<2)
50 #define CIRRUS_ID_CLGD5436 (0x2B<<2)
51 #define CIRRUS_ID_CLGD5446 (0x2E<<2)
53 /* this define is used to select the exact CLGD implementation we
55 //#define CIRRUS_ID CIRRUS_ID_CLGD5430
56 #define CIRRUS_ID CIRRUS_ID_CLGD5446
59 #define CIRRUS_SR7_BPP_VGA 0x00
60 #define CIRRUS_SR7_BPP_SVGA 0x01
61 #define CIRRUS_SR7_BPP_MASK 0x0e
62 #define CIRRUS_SR7_BPP_8 0x00
63 #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
64 #define CIRRUS_SR7_BPP_24 0x04
65 #define CIRRUS_SR7_BPP_16 0x06
66 #define CIRRUS_SR7_BPP_32 0x08
67 #define CIRRUS_SR7_ISAADDR_MASK 0xe0
70 #define CIRRUS_MEMSIZE_512k 0x08
71 #define CIRRUS_MEMSIZE_1M 0x10
72 #define CIRRUS_MEMSIZE_2M 0x18
73 #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
76 #define CIRRUS_CURSOR_SHOW 0x01
77 #define CIRRUS_CURSOR_HIDDENPEL 0x02
78 #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
81 #define CIRRUS_BUSTYPE_VLBFAST 0x10
82 #define CIRRUS_BUSTYPE_PCI 0x20
83 #define CIRRUS_BUSTYPE_VLBSLOW 0x30
84 #define CIRRUS_BUSTYPE_ISA 0x38
85 #define CIRRUS_MMIO_ENABLE 0x04
86 #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
87 #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
90 #define CIRRUS_BANKING_DUAL 0x01
91 #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
94 #define CIRRUS_BLTMODE_BACKWARDS 0x01
95 #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
96 #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
97 #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
98 #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
99 #define CIRRUS_BLTMODE_COLOREXPAND 0x80
100 #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
101 #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
102 #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
103 #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
104 #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
107 #define CIRRUS_BLT_BUSY 0x01
108 #define CIRRUS_BLT_START 0x02
109 #define CIRRUS_BLT_RESET 0x04
110 #define CIRRUS_BLT_FIFOUSED 0x10
113 #define CIRRUS_ROP_0 0x00
114 #define CIRRUS_ROP_SRC_AND_DST 0x05
115 #define CIRRUS_ROP_NOP 0x06
116 #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
117 #define CIRRUS_ROP_NOTDST 0x0b
118 #define CIRRUS_ROP_SRC 0x0d
119 #define CIRRUS_ROP_1 0x0e
120 #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
121 #define CIRRUS_ROP_SRC_XOR_DST 0x59
122 #define CIRRUS_ROP_SRC_OR_DST 0x6d
123 #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
124 #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
125 #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
126 #define CIRRUS_ROP_NOTSRC 0xd0
127 #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
128 #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
131 #define CIRRUS_BLTMODEEXT_SOLIDFILL 0x04
134 #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
135 #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
136 #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
137 #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
138 #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
139 #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
140 #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
141 #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
142 #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
143 #define CIRRUS_MMIO_BLTMODE 0x18 // byte
144 #define CIRRUS_MMIO_BLTROP 0x1a // byte
145 #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
146 #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
147 #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
148 #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
149 #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
150 #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
151 #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
152 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
153 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
154 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
155 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
156 #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
157 #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
158 #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
159 #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
160 #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
161 #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
162 #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
164 // PCI 0x00: vendor, 0x02: device
165 #define PCI_VENDOR_CIRRUS 0x1013
166 #define PCI_DEVICE_ID CIRRUS_ID
167 #define PCI_DEVICE_CLGD5462 0x00d0
168 #define PCI_DEVICE_CLGD5465 0x00d6
170 // PCI 0x04: command(word), 0x06(word): status
171 #define PCI_COMMAND_IOACCESS 0x0001
172 #define PCI_COMMAND_MEMACCESS 0x0002
173 #define PCI_COMMAND_BUSMASTER 0x0004
174 #define PCI_COMMAND_SPECIALCYCLE 0x0008
175 #define PCI_COMMAND_MEMWRITEINVALID 0x0010
176 #define PCI_COMMAND_PALETTESNOOPING 0x0020
177 #define PCI_COMMAND_PARITYDETECTION 0x0040
178 #define PCI_COMMAND_ADDRESSDATASTEPPING 0x0080
179 #define PCI_COMMAND_SERR 0x0100
180 #define PCI_COMMAND_BACKTOBACKTRANS 0x0200
181 // PCI 0x08, 0xff000000 (0x09-0x0b:class,0x08:rev)
182 #define PCI_CLASS_BASE_DISPLAY 0x03
183 // PCI 0x08, 0x00ff0000
184 #define PCI_CLASS_SUB_VGA 0x00
185 // PCI 0x0c, 0x00ff0000 (0x0c:cacheline,0x0d:latency,0x0e:headertype,0x0f:Built-in self test)
186 #define PCI_CLASS_HEADERTYPE_00h 0x00
187 // 0x10-0x3f (headertype 00h)
188 // PCI 0x10,0x14,0x18,0x1c,0x20,0x24: base address mapping registers
189 // 0x10: MEMBASE, 0x14: IOBASE(hard-coded in XFree86 3.x)
190 #define PCI_MAP_MEM 0x0
191 #define PCI_MAP_IO 0x1
192 #define PCI_MAP_MEM_ADDR_MASK (~0xf)
193 #define PCI_MAP_IO_ADDR_MASK (~0x3)
194 #define PCI_MAP_MEMFLAGS_32BIT 0x0
195 #define PCI_MAP_MEMFLAGS_32BIT_1M 0x1
196 #define PCI_MAP_MEMFLAGS_64BIT 0x4
197 #define PCI_MAP_MEMFLAGS_CACHEABLE 0x8
198 // PCI 0x28: cardbus CIS pointer
199 // PCI 0x2c: subsystem vendor id, 0x2e: subsystem id
200 // PCI 0x30: expansion ROM base address
201 #define PCI_ROMBIOS_ENABLED 0x1
202 // PCI 0x34: 0xffffff00=reserved, 0x000000ff=capabilities pointer
203 // PCI 0x38: reserved
204 // PCI 0x3c: 0x3c=int-line, 0x3d=int-pin, 0x3e=min-gnt, 0x3f=maax-lat
206 #define CIRRUS_PNPMMIO_SIZE 0x1000
209 /* I/O and memory hook */
210 #define CIRRUS_HOOK_NOT_HANDLED 0
211 #define CIRRUS_HOOK_HANDLED 1
213 typedef void (*cirrus_bitblt_rop_t) (uint8_t * dst, const uint8_t * src,
214 int dstpitch, int srcpitch,
215 int bltwidth, int bltheight);
217 typedef void (*cirrus_bitblt_handler_t) (void *opaque);
219 typedef struct CirrusVGAState {
222 int cirrus_linear_io_addr;
223 int cirrus_mmio_io_addr;
224 uint32_t cirrus_addr_mask;
225 uint8_t cirrus_shadow_gr0;
226 uint8_t cirrus_shadow_gr1;
227 uint8_t cirrus_hidden_dac_lockindex;
228 uint8_t cirrus_hidden_dac_data;
229 uint32_t cirrus_bank_base[2];
230 uint32_t cirrus_bank_limit[2];
231 uint8_t cirrus_hidden_palette[48];
232 uint32_t cirrus_hw_cursor_x;
233 uint32_t cirrus_hw_cursor_y;
234 int cirrus_blt_pixelwidth;
235 int cirrus_blt_width;
236 int cirrus_blt_height;
237 int cirrus_blt_dstpitch;
238 int cirrus_blt_srcpitch;
239 uint32_t cirrus_blt_dstaddr;
240 uint32_t cirrus_blt_srcaddr;
241 uint8_t cirrus_blt_mode;
242 cirrus_bitblt_rop_t cirrus_rop;
243 #define CIRRUS_BLTBUFSIZE 256
244 uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
245 uint8_t *cirrus_srcptr;
246 uint8_t *cirrus_srcptr_end;
247 uint32_t cirrus_srccounter;
248 uint8_t *cirrus_dstptr;
249 uint8_t *cirrus_dstptr_end;
250 uint32_t cirrus_dstcounter;
251 cirrus_bitblt_handler_t cirrus_blt_handler;
252 int cirrus_blt_horz_counter;
255 typedef struct PCICirrusVGAState {
257 CirrusVGAState cirrus_vga;
260 /***************************************
264 ***************************************/
267 static void cirrus_bitblt_reset(CirrusVGAState * s);
269 /***************************************
273 ***************************************/
275 #define IMPLEMENT_BITBLT(name,opline) \
277 cirrus_bitblt_rop_fwd_##name( \
278 uint8_t *dst,const uint8_t *src, \
279 int dstpitch,int srcpitch, \
280 int bltwidth,int bltheight) \
283 dstpitch -= bltwidth; \
284 srcpitch -= bltwidth; \
285 for (y = 0; y < bltheight; y++) { \
286 for (x = 0; x < bltwidth; x++) { \
297 cirrus_bitblt_rop_bkwd_##name( \
298 uint8_t *dst,const uint8_t *src, \
299 int dstpitch,int srcpitch, \
300 int bltwidth,int bltheight) \
303 dstpitch += bltwidth; \
304 srcpitch += bltwidth; \
305 for (y = 0; y < bltheight; y++) { \
306 for (x = 0; x < bltwidth; x++) { \
316 IMPLEMENT_BITBLT(0, *dst = 0)
317 IMPLEMENT_BITBLT(src_and_dst, *dst = (*src) & (*dst))
318 IMPLEMENT_BITBLT(nop, (void) 0)
319 IMPLEMENT_BITBLT(src_and_notdst, *dst = (*src) & (~(*dst)))
320 IMPLEMENT_BITBLT(notdst, *dst = ~(*dst))
321 IMPLEMENT_BITBLT(src, *dst = *src)
322 IMPLEMENT_BITBLT(1, *dst = 0xff)
323 IMPLEMENT_BITBLT(notsrc_and_dst, *dst = (~(*src)) & (*dst))
324 IMPLEMENT_BITBLT(src_xor_dst, *dst = (*src) ^ (*dst))
325 IMPLEMENT_BITBLT(src_or_dst, *dst = (*src) | (*dst))
326 IMPLEMENT_BITBLT(notsrc_or_notdst, *dst = (~(*src)) | (~(*dst)))
327 IMPLEMENT_BITBLT(src_notxor_dst, *dst = ~((*src) ^ (*dst)))
328 IMPLEMENT_BITBLT(src_or_notdst, *dst = (*src) | (~(*dst)))
329 IMPLEMENT_BITBLT(notsrc, *dst = (~(*src)))
330 IMPLEMENT_BITBLT(notsrc_or_dst, *dst = (~(*src)) | (*dst))
331 IMPLEMENT_BITBLT(notsrc_and_notdst, *dst = (~(*src)) & (~(*dst)))
333 static cirrus_bitblt_rop_t cirrus_get_fwd_rop_handler(uint8_t rop)
335 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_fwd_nop;
339 rop_handler = cirrus_bitblt_rop_fwd_0;
341 case CIRRUS_ROP_SRC_AND_DST:
342 rop_handler = cirrus_bitblt_rop_fwd_src_and_dst;
345 rop_handler = cirrus_bitblt_rop_fwd_nop;
347 case CIRRUS_ROP_SRC_AND_NOTDST:
348 rop_handler = cirrus_bitblt_rop_fwd_src_and_notdst;
350 case CIRRUS_ROP_NOTDST:
351 rop_handler = cirrus_bitblt_rop_fwd_notdst;
354 rop_handler = cirrus_bitblt_rop_fwd_src;
357 rop_handler = cirrus_bitblt_rop_fwd_1;
359 case CIRRUS_ROP_NOTSRC_AND_DST:
360 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_dst;
362 case CIRRUS_ROP_SRC_XOR_DST:
363 rop_handler = cirrus_bitblt_rop_fwd_src_xor_dst;
365 case CIRRUS_ROP_SRC_OR_DST:
366 rop_handler = cirrus_bitblt_rop_fwd_src_or_dst;
368 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
369 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_notdst;
371 case CIRRUS_ROP_SRC_NOTXOR_DST:
372 rop_handler = cirrus_bitblt_rop_fwd_src_notxor_dst;
374 case CIRRUS_ROP_SRC_OR_NOTDST:
375 rop_handler = cirrus_bitblt_rop_fwd_src_or_notdst;
377 case CIRRUS_ROP_NOTSRC:
378 rop_handler = cirrus_bitblt_rop_fwd_notsrc;
380 case CIRRUS_ROP_NOTSRC_OR_DST:
381 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_dst;
383 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
384 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_notdst;
388 printf("unknown ROP %02x\n", rop);
396 static cirrus_bitblt_rop_t cirrus_get_bkwd_rop_handler(uint8_t rop)
398 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_bkwd_nop;
402 rop_handler = cirrus_bitblt_rop_bkwd_0;
404 case CIRRUS_ROP_SRC_AND_DST:
405 rop_handler = cirrus_bitblt_rop_bkwd_src_and_dst;
408 rop_handler = cirrus_bitblt_rop_bkwd_nop;
410 case CIRRUS_ROP_SRC_AND_NOTDST:
411 rop_handler = cirrus_bitblt_rop_bkwd_src_and_notdst;
413 case CIRRUS_ROP_NOTDST:
414 rop_handler = cirrus_bitblt_rop_bkwd_notdst;
417 rop_handler = cirrus_bitblt_rop_bkwd_src;
420 rop_handler = cirrus_bitblt_rop_bkwd_1;
422 case CIRRUS_ROP_NOTSRC_AND_DST:
423 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_dst;
425 case CIRRUS_ROP_SRC_XOR_DST:
426 rop_handler = cirrus_bitblt_rop_bkwd_src_xor_dst;
428 case CIRRUS_ROP_SRC_OR_DST:
429 rop_handler = cirrus_bitblt_rop_bkwd_src_or_dst;
431 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
432 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_notdst;
434 case CIRRUS_ROP_SRC_NOTXOR_DST:
435 rop_handler = cirrus_bitblt_rop_bkwd_src_notxor_dst;
437 case CIRRUS_ROP_SRC_OR_NOTDST:
438 rop_handler = cirrus_bitblt_rop_bkwd_src_or_notdst;
440 case CIRRUS_ROP_NOTSRC:
441 rop_handler = cirrus_bitblt_rop_bkwd_notsrc;
443 case CIRRUS_ROP_NOTSRC_OR_DST:
444 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_dst;
446 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
447 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_notdst;
451 printf("unknown ROP %02x\n", rop);
459 /***************************************
463 ***************************************/
466 cirrus_colorexpand_8(CirrusVGAState * s, uint8_t * dst,
467 const uint8_t * src, int count)
475 colors[0] = s->cirrus_shadow_gr0;
476 colors[1] = s->cirrus_shadow_gr1;
478 bitmask = 0x80 >> srcskipleft;
480 for (x = 0; x < count; x++) {
481 if ((bitmask & 0xff) == 0) {
485 *dst++ = colors[!!(bits & bitmask)];
491 cirrus_colorexpand_16(CirrusVGAState * s, uint8_t * dst,
492 const uint8_t * src, int count)
495 uint8_t colors[2][2];
501 colors[0][0] = s->cirrus_shadow_gr0;
502 colors[0][1] = s->gr[0x10];
503 colors[1][0] = s->cirrus_shadow_gr1;
504 colors[1][1] = s->gr[0x11];
506 bitmask = 0x80 >> srcskipleft;
508 for (x = 0; x < count; x++) {
509 if ((bitmask & 0xff) == 0) {
513 index = !!(bits & bitmask);
514 *dst++ = colors[index][0];
515 *dst++ = colors[index][1];
521 cirrus_colorexpand_24(CirrusVGAState * s, uint8_t * dst,
522 const uint8_t * src, int count)
525 uint8_t colors[2][3];
531 colors[0][0] = s->cirrus_shadow_gr0;
532 colors[0][1] = s->gr[0x10];
533 colors[0][2] = s->gr[0x12];
534 colors[1][0] = s->cirrus_shadow_gr1;
535 colors[1][1] = s->gr[0x11];
536 colors[1][2] = s->gr[0x13];
538 bitmask = 0x80 << srcskipleft;
540 for (x = 0; x < count; x++) {
541 if ((bitmask & 0xff) == 0) {
545 index = !!(bits & bitmask);
546 *dst++ = colors[index][0];
547 *dst++ = colors[index][1];
548 *dst++ = colors[index][2];
554 cirrus_colorexpand_32(CirrusVGAState * s, uint8_t * dst,
555 const uint8_t * src, int count)
558 uint8_t colors[2][4];
564 colors[0][0] = s->cirrus_shadow_gr0;
565 colors[0][1] = s->gr[0x10];
566 colors[0][2] = s->gr[0x12];
567 colors[0][3] = s->gr[0x14];
568 colors[1][0] = s->cirrus_shadow_gr1;
569 colors[1][1] = s->gr[0x11];
570 colors[1][2] = s->gr[0x13];
571 colors[1][3] = s->gr[0x15];
573 bitmask = 0x80 << srcskipleft;
575 for (x = 0; x < count; x++) {
576 if ((bitmask & 0xff) == 0) {
580 index = !!(bits & bitmask);
581 *dst++ = colors[index][0];
582 *dst++ = colors[index][1];
583 *dst++ = colors[index][2];
584 *dst++ = colors[index][3];
590 cirrus_colorexpand(CirrusVGAState * s, uint8_t * dst, const uint8_t * src,
593 switch (s->cirrus_blt_pixelwidth) {
595 cirrus_colorexpand_8(s, dst, src, count);
598 cirrus_colorexpand_16(s, dst, src, count);
601 cirrus_colorexpand_24(s, dst, src, count);
604 cirrus_colorexpand_32(s, dst, src, count);
608 printf("cirrus: COLOREXPAND pixelwidth %d - unimplemented\n",
609 s->cirrus_blt_pixelwidth);
615 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
616 int off_pitch, int bytesperline,
623 for (y = 0; y < lines; y++) {
625 off_cur_end = off_cur + bytesperline;
626 off_cur &= TARGET_PAGE_MASK;
627 while (off_cur < off_cur_end) {
628 cpu_physical_memory_set_dirty(s->vram_offset + off_cur);
629 off_cur += TARGET_PAGE_SIZE;
631 off_begin += off_pitch;
637 static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
640 uint8_t work_colorexp[256];
644 int tilewidth, tileheight;
645 int patternbytes = s->cirrus_blt_pixelwidth * 8;
647 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
648 cirrus_colorexpand(s, work_colorexp, src, 8 * 8);
650 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_COLOREXPAND;
652 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_PATTERNCOPY) {
654 printf("cirrus: blt mode %02x (pattercopy) - unimplemented\n",
660 dst = s->vram_ptr + s->cirrus_blt_dstaddr;
661 for (y = 0; y < s->cirrus_blt_height; y += 8) {
663 tileheight = qemu_MIN(8, s->cirrus_blt_height - y);
664 for (x = 0; x < s->cirrus_blt_width; x += patternbytes) {
665 tilewidth = qemu_MIN(patternbytes, s->cirrus_blt_width - x);
666 (*s->cirrus_rop) (dstc, src,
667 s->cirrus_blt_dstpitch, patternbytes,
668 tilewidth, tileheight);
669 dstc += patternbytes;
671 dst += s->cirrus_blt_dstpitch * 8;
673 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
674 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
675 s->cirrus_blt_height);
681 static void cirrus_fill_8(CirrusVGAState *s,
682 uint8_t *dst, int dst_pitch, int width, int height)
688 val = s->cirrus_shadow_gr1;
691 for(y = 0; y < height; y++) {
693 for(x = 0; x < width; x++) {
700 static void cirrus_fill_16(CirrusVGAState *s,
701 uint8_t *dst, int dst_pitch, int width, int height)
707 val = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8);
708 val = le16_to_cpu(val);
712 for(y = 0; y < height; y++) {
714 for(x = 0; x < width; x++) {
715 ((uint16_t *)d)[0] = val;
722 static void cirrus_fill_24(CirrusVGAState *s,
723 uint8_t *dst, int dst_pitch, int width, int height)
729 for(y = 0; y < height; y++) {
731 for(x = 0; x < width; x += 3) {
732 *d++ = s->cirrus_shadow_gr1;
740 static void cirrus_fill_32(CirrusVGAState *s,
741 uint8_t *dst, int dst_pitch, int width, int height)
747 val = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8) |
748 (s->gr[0x13] << 8) | (s->gr[0x15] << 8);
749 val = le32_to_cpu(val);
753 for(y = 0; y < height; y++) {
755 for(x = 0; x < width; x++) {
756 ((uint32_t *)d)[0] = val;
763 static int cirrus_bitblt_solidfill(CirrusVGAState *s)
766 dst = s->vram_ptr + s->cirrus_blt_dstaddr;
767 switch (s->cirrus_blt_pixelwidth) {
769 cirrus_fill_8(s, dst, s->cirrus_blt_dstpitch,
770 s->cirrus_blt_width, s->cirrus_blt_height);
773 cirrus_fill_16(s, dst, s->cirrus_blt_dstpitch,
774 s->cirrus_blt_width, s->cirrus_blt_height);
777 cirrus_fill_24(s, dst, s->cirrus_blt_dstpitch,
778 s->cirrus_blt_width, s->cirrus_blt_height);
782 cirrus_fill_32(s, dst, s->cirrus_blt_dstpitch,
783 s->cirrus_blt_width, s->cirrus_blt_height);
786 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
787 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
788 s->cirrus_blt_height);
789 cirrus_bitblt_reset(s);
793 /***************************************
795 * bitblt (video-to-video)
797 ***************************************/
799 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
801 return cirrus_bitblt_common_patterncopy(s,
803 s->cirrus_blt_srcaddr);
806 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
808 if ((s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) != 0) {
810 printf("cirrus: CIRRUS_BLTMODE_COLOREXPAND - unimplemented\n");
814 if ((s->cirrus_blt_mode & (~CIRRUS_BLTMODE_BACKWARDS)) != 0) {
816 printf("cirrus: blt mode %02x - unimplemented\n",
822 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
823 s->vram_ptr + s->cirrus_blt_srcaddr,
824 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
825 s->cirrus_blt_width, s->cirrus_blt_height);
826 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
827 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
828 s->cirrus_blt_height);
832 /***************************************
834 * bitblt (cpu-to-video)
836 ***************************************/
838 static void cirrus_bitblt_cputovideo_patterncopy(void *opaque)
840 CirrusVGAState *s = (CirrusVGAState *) opaque;
843 data_count = s->cirrus_srcptr - &s->cirrus_bltbuf[0];
845 if (data_count > 0) {
846 if (data_count != s->cirrus_srccounter) {
848 printf("cirrus: internal error\n");
851 cirrus_bitblt_common_patterncopy(s, &s->cirrus_bltbuf[0]);
853 cirrus_bitblt_reset(s);
857 static void cirrus_bitblt_cputovideo_copy(void *opaque)
859 CirrusVGAState *s = (CirrusVGAState *) opaque;
862 uint8_t work_colorexp[256];
863 uint8_t *src_ptr = NULL;
868 if (s->cirrus_blt_height <= 0) {
869 s->cirrus_srcptr = s->cirrus_srcptr_end;
873 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
875 /* get BLT source. */
876 if (src_avail <= 0) {
877 data_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
881 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
882 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_COLOREXPAND) {
884 printf("cirrus: unsupported\n");
886 cirrus_bitblt_reset(s);
889 data_avail = qemu_MIN(data_count, 256 / 32);
890 cirrus_colorexpand(s, work_colorexp, s->cirrus_srcptr,
892 src_ptr = &work_colorexp[0];
893 src_avail = data_avail * 8 * s->cirrus_blt_pixelwidth;
894 s->cirrus_srcptr += data_avail;
896 ((s->cirrus_blt_width + 7) / 8) * 8 -
898 src_linepad *= s->cirrus_blt_pixelwidth;
900 if (s->cirrus_blt_mode != 0) {
902 printf("cirrus: unsupported\n");
904 cirrus_bitblt_reset(s);
907 src_ptr = s->cirrus_srcptr;
909 data_count / s->cirrus_blt_pixelwidth *
910 s->cirrus_blt_pixelwidth;
911 s->cirrus_srcptr += src_avail;
919 s->cirrus_blt_srcpitch - s->cirrus_blt_horz_counter;
920 src_processing = qemu_MIN(src_avail, src_processing);
921 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
922 src_ptr, 0, 0, src_processing, 1);
923 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
926 s->cirrus_blt_dstaddr += src_processing;
927 src_ptr += src_processing;
928 src_avail -= src_processing;
929 s->cirrus_blt_horz_counter += src_processing;
930 if (s->cirrus_blt_horz_counter >= s->cirrus_blt_srcpitch) {
931 src_ptr += src_linepad;
932 src_avail -= src_linepad;
933 s->cirrus_blt_dstaddr +=
934 s->cirrus_blt_dstpitch - s->cirrus_blt_srcpitch;
935 s->cirrus_blt_horz_counter = 0;
936 s->cirrus_blt_height--;
937 if (s->cirrus_blt_height <= 0) {
938 s->cirrus_srcptr = s->cirrus_srcptr_end;
945 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
950 s->cirrus_blt_handler(s);
952 if (s->cirrus_srccounter > 0) {
953 s->cirrus_srccounter -= s->cirrus_srcptr - &s->cirrus_bltbuf[0];
954 copy_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
955 memmove(&s->cirrus_bltbuf[0], s->cirrus_srcptr, copy_count);
956 avail_count = qemu_MIN(CIRRUS_BLTBUFSIZE, s->cirrus_srccounter);
957 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
958 s->cirrus_srcptr_end = s->cirrus_srcptr + avail_count;
959 if (s->cirrus_srccounter <= 0) {
960 cirrus_bitblt_reset(s);
965 /***************************************
969 ***************************************/
971 static void cirrus_bitblt_reset(CirrusVGAState * s)
974 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
975 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
976 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
977 s->cirrus_srccounter = 0;
978 s->cirrus_dstptr = &s->cirrus_bltbuf[0];
979 s->cirrus_dstptr_end = &s->cirrus_bltbuf[0];
980 s->cirrus_dstcounter = 0;
981 s->cirrus_blt_handler = NULL;
984 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
986 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
987 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
988 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
990 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
991 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
992 s->cirrus_srccounter = 8;
994 s->cirrus_srccounter = 8 * 8 * s->cirrus_blt_pixelwidth;
996 s->cirrus_blt_srcpitch = 0;
997 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_patterncopy;
999 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
1000 s->cirrus_srccounter =
1001 ((s->cirrus_blt_width + 7) / 8) * s->cirrus_blt_height;
1002 s->cirrus_blt_srcpitch =
1003 s->cirrus_blt_width * s->cirrus_blt_pixelwidth;
1005 s->cirrus_srccounter =
1006 s->cirrus_blt_width * s->cirrus_blt_height;
1007 s->cirrus_blt_srcpitch = s->cirrus_blt_width;
1009 /* 4-byte alignment */
1010 s->cirrus_srccounter = (s->cirrus_srccounter + 3) & (~3);
1012 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_copy;
1013 s->cirrus_blt_horz_counter = 0;
1016 cirrus_bitblt_cputovideo_next(s);
1020 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
1024 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
1029 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
1033 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
1034 ret = cirrus_bitblt_videotovideo_patterncopy(s);
1036 ret = cirrus_bitblt_videotovideo_copy(s);
1040 cirrus_bitblt_reset(s);
1044 static void cirrus_bitblt_start(CirrusVGAState * s)
1048 s->cirrus_blt_width = (s->gr[0x20] | (s->gr[0x21] << 8)) + 1;
1049 s->cirrus_blt_height = (s->gr[0x22] | (s->gr[0x23] << 8)) + 1;
1050 s->cirrus_blt_dstpitch = (s->gr[0x24] | (s->gr[0x25] << 8));
1051 s->cirrus_blt_srcpitch = (s->gr[0x26] | (s->gr[0x27] << 8));
1052 s->cirrus_blt_dstaddr =
1053 (s->gr[0x28] | (s->gr[0x29] << 8) | (s->gr[0x2a] << 16));
1054 s->cirrus_blt_srcaddr =
1055 (s->gr[0x2c] | (s->gr[0x2d] << 8) | (s->gr[0x2e] << 16));
1056 s->cirrus_blt_mode = s->gr[0x30];
1057 blt_rop = s->gr[0x32];
1060 printf("rop=%02x mode=%02x modeext=%02x w=%d h=%d dpitch=%d spicth=%d daddr=%08x saddr=%08x\n",
1064 s->cirrus_blt_width,
1065 s->cirrus_blt_height,
1066 s->cirrus_blt_dstpitch,
1067 s->cirrus_blt_srcpitch,
1068 s->cirrus_blt_dstaddr,
1069 s->cirrus_blt_srcaddr);
1072 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
1073 case CIRRUS_BLTMODE_PIXELWIDTH8:
1074 s->cirrus_blt_pixelwidth = 1;
1076 case CIRRUS_BLTMODE_PIXELWIDTH16:
1077 s->cirrus_blt_pixelwidth = 2;
1079 case CIRRUS_BLTMODE_PIXELWIDTH24:
1080 s->cirrus_blt_pixelwidth = 3;
1082 case CIRRUS_BLTMODE_PIXELWIDTH32:
1083 s->cirrus_blt_pixelwidth = 4;
1087 printf("cirrus: bitblt - pixel width is unknown\n");
1091 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
1094 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
1095 CIRRUS_BLTMODE_MEMSYSDEST))
1096 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
1098 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
1103 if ((s->gr[0x33] & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
1104 (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST |
1105 CIRRUS_BLTMODE_TRANSPARENTCOMP |
1106 CIRRUS_BLTMODE_PATTERNCOPY |
1107 CIRRUS_BLTMODE_COLOREXPAND)) ==
1108 (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
1109 cirrus_bitblt_solidfill(s);
1111 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
1112 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
1113 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
1114 s->cirrus_rop = cirrus_get_bkwd_rop_handler(blt_rop);
1116 s->cirrus_rop = cirrus_get_fwd_rop_handler(blt_rop);
1119 // setup bitblt engine.
1120 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
1121 if (!cirrus_bitblt_cputovideo(s))
1123 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
1124 if (!cirrus_bitblt_videotocpu(s))
1127 if (!cirrus_bitblt_videotovideo(s))
1133 cirrus_bitblt_reset(s);
1136 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1140 old_value = s->gr[0x31];
1141 s->gr[0x31] = reg_value;
1143 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1144 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1145 cirrus_bitblt_reset(s);
1146 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1147 ((reg_value & CIRRUS_BLT_START) != 0)) {
1148 s->gr[0x31] |= CIRRUS_BLT_BUSY;
1149 cirrus_bitblt_start(s);
1154 /***************************************
1158 ***************************************/
1160 static void cirrus_get_offsets(VGAState *s1,
1161 uint32_t *pline_offset,
1162 uint32_t *pstart_addr)
1164 CirrusVGAState * s = (CirrusVGAState *)s1;
1165 uint32_t start_addr;
1166 uint32_t line_offset;
1168 line_offset = s->cr[0x13]
1169 | ((s->cr[0x1b] & 0x10) << 4);
1171 *pline_offset = line_offset;
1173 start_addr = (s->cr[0x0c] << 8)
1175 | ((s->cr[0x1b] & 0x01) << 16)
1176 | ((s->cr[0x1b] & 0x0c) << 15)
1177 | ((s->cr[0x1d] & 0x80) << 12);
1178 *pstart_addr = start_addr;
1181 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1185 switch (s->cirrus_hidden_dac_data & 0xf) {
1188 break; /* Sierra HiColor */
1191 break; /* XGA HiColor */
1194 printf("cirrus: invalid DAC value %x in 16bpp\n",
1195 (s->cirrus_hidden_dac_data & 0xf));
1203 static int cirrus_get_bpp(VGAState *s1)
1205 CirrusVGAState * s = (CirrusVGAState *)s1;
1208 if ((s->sr[0x07] & 0x01) != 0) {
1210 switch (s->sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1211 case CIRRUS_SR7_BPP_8:
1214 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1215 ret = cirrus_get_bpp16_depth(s);
1217 case CIRRUS_SR7_BPP_24:
1220 case CIRRUS_SR7_BPP_16:
1221 ret = cirrus_get_bpp16_depth(s);
1223 case CIRRUS_SR7_BPP_32:
1228 printf("cirrus: unknown bpp - sr7=%x\n", s->sr[0x7]);
1241 /***************************************
1245 ***************************************/
1247 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1252 if ((s->gr[0x0b] & 0x01) != 0) /* dual bank */
1253 offset = s->gr[0x09 + bank_index];
1254 else /* single bank */
1255 offset = s->gr[0x09];
1257 if ((s->gr[0x0b] & 0x20) != 0)
1262 if (s->vram_size <= offset)
1265 limit = s->vram_size - offset;
1267 if (((s->gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1268 if (limit > 0x8000) {
1277 s->cirrus_bank_base[bank_index] = offset;
1278 s->cirrus_bank_limit[bank_index] = limit;
1280 s->cirrus_bank_base[bank_index] = 0;
1281 s->cirrus_bank_limit[bank_index] = 0;
1285 /***************************************
1287 * I/O access between 0x3c4-0x3c5
1289 ***************************************/
1292 cirrus_hook_read_sr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1294 switch (reg_index) {
1295 case 0x00: // Standard VGA
1296 case 0x01: // Standard VGA
1297 case 0x02: // Standard VGA
1298 case 0x03: // Standard VGA
1299 case 0x04: // Standard VGA
1300 return CIRRUS_HOOK_NOT_HANDLED;
1301 case 0x06: // Unlock Cirrus extensions
1302 *reg_value = s->sr[reg_index];
1307 case 0x70: // Graphics Cursor X
1311 case 0xf0: // Graphics Cursor X
1312 *reg_value = s->sr[0x10];
1317 case 0x71: // Graphics Cursor Y
1321 *reg_value = s->sr[0x11];
1324 case 0x07: // Extended Sequencer Mode
1325 case 0x08: // EEPROM Control
1326 case 0x09: // Scratch Register 0
1327 case 0x0a: // Scratch Register 1
1328 case 0x0b: // VCLK 0
1329 case 0x0c: // VCLK 1
1330 case 0x0d: // VCLK 2
1331 case 0x0e: // VCLK 3
1332 case 0x0f: // DRAM Control
1333 case 0xf1: // Graphics Cursor Y
1334 case 0x12: // Graphics Cursor Attribute
1335 case 0x13: // Graphics Cursor Pattern Address
1336 case 0x14: // Scratch Register 2
1337 case 0x15: // Scratch Register 3
1338 case 0x16: // Performance Tuning Register
1339 case 0x17: // Configuration Readback and Extended Control
1340 case 0x18: // Signature Generator Control
1341 case 0x19: // Signal Generator Result
1342 case 0x1a: // Signal Generator Result
1343 case 0x1b: // VCLK 0 Denominator & Post
1344 case 0x1c: // VCLK 1 Denominator & Post
1345 case 0x1d: // VCLK 2 Denominator & Post
1346 case 0x1e: // VCLK 3 Denominator & Post
1347 case 0x1f: // BIOS Write Enable and MCLK select
1349 printf("cirrus: handled inport sr_index %02x\n", reg_index);
1351 *reg_value = s->sr[reg_index];
1355 printf("cirrus: inport sr_index %02x\n", reg_index);
1361 return CIRRUS_HOOK_HANDLED;
1365 cirrus_hook_write_sr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1367 switch (reg_index) {
1368 case 0x00: // Standard VGA
1369 case 0x01: // Standard VGA
1370 case 0x02: // Standard VGA
1371 case 0x03: // Standard VGA
1372 case 0x04: // Standard VGA
1373 return CIRRUS_HOOK_NOT_HANDLED;
1374 case 0x06: // Unlock Cirrus extensions
1376 if (reg_value == 0x12) {
1377 s->sr[reg_index] = 0x12;
1379 s->sr[reg_index] = 0x0f;
1385 case 0x70: // Graphics Cursor X
1389 case 0xf0: // Graphics Cursor X
1390 s->sr[0x10] = reg_value;
1391 s->cirrus_hw_cursor_x = ((reg_index << 3) & 0x700) | reg_value;
1396 case 0x71: // Graphics Cursor Y
1400 case 0xf1: // Graphics Cursor Y
1401 s->sr[0x11] = reg_value;
1402 s->cirrus_hw_cursor_y = ((reg_index << 3) & 0x700) | reg_value;
1404 case 0x07: // Extended Sequencer Mode
1405 case 0x08: // EEPROM Control
1406 case 0x09: // Scratch Register 0
1407 case 0x0a: // Scratch Register 1
1408 case 0x0b: // VCLK 0
1409 case 0x0c: // VCLK 1
1410 case 0x0d: // VCLK 2
1411 case 0x0e: // VCLK 3
1412 case 0x0f: // DRAM Control
1413 case 0x12: // Graphics Cursor Attribute
1414 case 0x13: // Graphics Cursor Pattern Address
1415 case 0x14: // Scratch Register 2
1416 case 0x15: // Scratch Register 3
1417 case 0x16: // Performance Tuning Register
1418 case 0x17: // Configuration Readback and Extended Control
1419 case 0x18: // Signature Generator Control
1420 case 0x19: // Signature Generator Result
1421 case 0x1a: // Signature Generator Result
1422 case 0x1b: // VCLK 0 Denominator & Post
1423 case 0x1c: // VCLK 1 Denominator & Post
1424 case 0x1d: // VCLK 2 Denominator & Post
1425 case 0x1e: // VCLK 3 Denominator & Post
1426 case 0x1f: // BIOS Write Enable and MCLK select
1427 s->sr[reg_index] = reg_value;
1429 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1430 reg_index, reg_value);
1435 printf("cirrus: outport sr_index %02x, sr_value %02x\n", reg_index,
1441 return CIRRUS_HOOK_HANDLED;
1444 /***************************************
1446 * I/O access at 0x3c6
1448 ***************************************/
1450 static void cirrus_read_hidden_dac(CirrusVGAState * s, int *reg_value)
1453 if (++s->cirrus_hidden_dac_lockindex == 5) {
1454 *reg_value = s->cirrus_hidden_dac_data;
1455 s->cirrus_hidden_dac_lockindex = 0;
1459 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1461 if (s->cirrus_hidden_dac_lockindex == 4) {
1462 s->cirrus_hidden_dac_data = reg_value;
1463 #if defined(DEBUG_CIRRUS)
1464 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1467 s->cirrus_hidden_dac_lockindex = 0;
1470 /***************************************
1472 * I/O access at 0x3c9
1474 ***************************************/
1476 static int cirrus_hook_read_palette(CirrusVGAState * s, int *reg_value)
1478 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1479 return CIRRUS_HOOK_NOT_HANDLED;
1480 if (s->dac_read_index < 0x10) {
1482 s->cirrus_hidden_palette[s->dac_read_index * 3 +
1485 *reg_value = 0xff; /* XXX */
1487 if (++s->dac_sub_index == 3) {
1488 s->dac_sub_index = 0;
1489 s->dac_read_index++;
1491 return CIRRUS_HOOK_HANDLED;
1494 static int cirrus_hook_write_palette(CirrusVGAState * s, int reg_value)
1496 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1497 return CIRRUS_HOOK_NOT_HANDLED;
1498 s->dac_cache[s->dac_sub_index] = reg_value;
1499 if (++s->dac_sub_index == 3) {
1500 if (s->dac_read_index < 0x10) {
1501 memcpy(&s->cirrus_hidden_palette[s->dac_write_index * 3],
1503 /* XXX update cursor */
1505 s->dac_sub_index = 0;
1506 s->dac_write_index++;
1508 return CIRRUS_HOOK_HANDLED;
1511 /***************************************
1513 * I/O access between 0x3ce-0x3cf
1515 ***************************************/
1518 cirrus_hook_read_gr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1520 switch (reg_index) {
1521 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1522 *reg_value = s->cirrus_shadow_gr0;
1523 return CIRRUS_HOOK_HANDLED;
1524 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1525 *reg_value = s->cirrus_shadow_gr1;
1526 return CIRRUS_HOOK_HANDLED;
1527 case 0x02: // Standard VGA
1528 case 0x03: // Standard VGA
1529 case 0x04: // Standard VGA
1530 case 0x06: // Standard VGA
1531 case 0x07: // Standard VGA
1532 case 0x08: // Standard VGA
1533 return CIRRUS_HOOK_NOT_HANDLED;
1534 case 0x05: // Standard VGA, Cirrus extended mode
1539 if (reg_index < 0x3a) {
1540 *reg_value = s->gr[reg_index];
1543 printf("cirrus: inport gr_index %02x\n", reg_index);
1548 return CIRRUS_HOOK_HANDLED;
1552 cirrus_hook_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1554 switch (reg_index) {
1555 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1556 s->cirrus_shadow_gr0 = reg_value;
1557 return CIRRUS_HOOK_NOT_HANDLED;
1558 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1559 s->cirrus_shadow_gr1 = reg_value;
1560 return CIRRUS_HOOK_NOT_HANDLED;
1561 case 0x02: // Standard VGA
1562 case 0x03: // Standard VGA
1563 case 0x04: // Standard VGA
1564 case 0x06: // Standard VGA
1565 case 0x07: // Standard VGA
1566 case 0x08: // Standard VGA
1567 return CIRRUS_HOOK_NOT_HANDLED;
1568 case 0x05: // Standard VGA, Cirrus extended mode
1569 s->gr[reg_index] = reg_value & 0x7f;
1571 case 0x09: // bank offset #0
1572 case 0x0A: // bank offset #1
1574 s->gr[reg_index] = reg_value;
1575 cirrus_update_bank_ptr(s, 0);
1576 cirrus_update_bank_ptr(s, 1);
1578 case 0x10: // BGCOLOR 0x0000ff00
1579 case 0x11: // FGCOLOR 0x0000ff00
1580 case 0x12: // BGCOLOR 0x00ff0000
1581 case 0x13: // FGCOLOR 0x00ff0000
1582 case 0x14: // BGCOLOR 0xff000000
1583 case 0x15: // FGCOLOR 0xff000000
1584 case 0x20: // BLT WIDTH 0x0000ff
1585 case 0x22: // BLT HEIGHT 0x0000ff
1586 case 0x24: // BLT DEST PITCH 0x0000ff
1587 case 0x26: // BLT SRC PITCH 0x0000ff
1588 case 0x28: // BLT DEST ADDR 0x0000ff
1589 case 0x29: // BLT DEST ADDR 0x00ff00
1590 case 0x2c: // BLT SRC ADDR 0x0000ff
1591 case 0x2d: // BLT SRC ADDR 0x00ff00
1592 case 0x30: // BLT MODE
1593 case 0x32: // RASTER OP
1594 case 0x33: // BLT MODEEXT
1595 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1596 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1597 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1598 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1599 s->gr[reg_index] = reg_value;
1601 case 0x21: // BLT WIDTH 0x001f00
1602 case 0x23: // BLT HEIGHT 0x001f00
1603 case 0x25: // BLT DEST PITCH 0x001f00
1604 case 0x27: // BLT SRC PITCH 0x001f00
1605 s->gr[reg_index] = reg_value & 0x1f;
1607 case 0x2a: // BLT DEST ADDR 0x3f0000
1608 case 0x2e: // BLT SRC ADDR 0x3f0000
1609 s->gr[reg_index] = reg_value & 0x3f;
1611 case 0x31: // BLT STATUS/START
1612 cirrus_write_bitblt(s, reg_value);
1616 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1622 return CIRRUS_HOOK_HANDLED;
1625 /***************************************
1627 * I/O access between 0x3d4-0x3d5
1629 ***************************************/
1632 cirrus_hook_read_cr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1634 switch (reg_index) {
1635 case 0x00: // Standard VGA
1636 case 0x01: // Standard VGA
1637 case 0x02: // Standard VGA
1638 case 0x03: // Standard VGA
1639 case 0x04: // Standard VGA
1640 case 0x05: // Standard VGA
1641 case 0x06: // Standard VGA
1642 case 0x07: // Standard VGA
1643 case 0x08: // Standard VGA
1644 case 0x09: // Standard VGA
1645 case 0x0a: // Standard VGA
1646 case 0x0b: // Standard VGA
1647 case 0x0c: // Standard VGA
1648 case 0x0d: // Standard VGA
1649 case 0x0e: // Standard VGA
1650 case 0x0f: // Standard VGA
1651 case 0x10: // Standard VGA
1652 case 0x11: // Standard VGA
1653 case 0x12: // Standard VGA
1654 case 0x13: // Standard VGA
1655 case 0x14: // Standard VGA
1656 case 0x15: // Standard VGA
1657 case 0x16: // Standard VGA
1658 case 0x17: // Standard VGA
1659 case 0x18: // Standard VGA
1660 return CIRRUS_HOOK_NOT_HANDLED;
1661 case 0x19: // Interlace End
1662 case 0x1a: // Miscellaneous Control
1663 case 0x1b: // Extended Display Control
1664 case 0x1c: // Sync Adjust and Genlock
1665 case 0x1d: // Overlay Extended Control
1666 case 0x22: // Graphics Data Latches Readback (R)
1667 case 0x24: // Attribute Controller Toggle Readback (R)
1668 case 0x25: // Part Status
1669 case 0x27: // Part ID (R)
1670 *reg_value = s->cr[reg_index];
1672 case 0x26: // Attribute Controller Index Readback (R)
1673 *reg_value = s->ar_index & 0x3f;
1677 printf("cirrus: inport cr_index %02x\n", reg_index);
1683 return CIRRUS_HOOK_HANDLED;
1687 cirrus_hook_write_cr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1689 switch (reg_index) {
1690 case 0x00: // Standard VGA
1691 case 0x01: // Standard VGA
1692 case 0x02: // Standard VGA
1693 case 0x03: // Standard VGA
1694 case 0x04: // Standard VGA
1695 case 0x05: // Standard VGA
1696 case 0x06: // Standard VGA
1697 case 0x07: // Standard VGA
1698 case 0x08: // Standard VGA
1699 case 0x09: // Standard VGA
1700 case 0x0a: // Standard VGA
1701 case 0x0b: // Standard VGA
1702 case 0x0c: // Standard VGA
1703 case 0x0d: // Standard VGA
1704 case 0x0e: // Standard VGA
1705 case 0x0f: // Standard VGA
1706 case 0x10: // Standard VGA
1707 case 0x11: // Standard VGA
1708 case 0x12: // Standard VGA
1709 case 0x13: // Standard VGA
1710 case 0x14: // Standard VGA
1711 case 0x15: // Standard VGA
1712 case 0x16: // Standard VGA
1713 case 0x17: // Standard VGA
1714 case 0x18: // Standard VGA
1715 return CIRRUS_HOOK_NOT_HANDLED;
1716 case 0x19: // Interlace End
1717 case 0x1a: // Miscellaneous Control
1718 case 0x1b: // Extended Display Control
1719 case 0x1c: // Sync Adjust and Genlock
1720 s->cr[reg_index] = reg_value;
1722 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1723 reg_index, reg_value);
1726 case 0x22: // Graphics Data Latches Readback (R)
1727 case 0x24: // Attribute Controller Toggle Readback (R)
1728 case 0x26: // Attribute Controller Index Readback (R)
1729 case 0x27: // Part ID (R)
1731 case 0x1d: // Overlay Extended Control
1732 case 0x25: // Part Status
1735 printf("cirrus: outport cr_index %02x, cr_value %02x\n", reg_index,
1741 return CIRRUS_HOOK_HANDLED;
1744 /***************************************
1746 * memory-mapped I/O (bitblt)
1748 ***************************************/
1750 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1755 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1756 cirrus_hook_read_gr(s, 0x00, &value);
1758 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1759 cirrus_hook_read_gr(s, 0x10, &value);
1761 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1762 cirrus_hook_read_gr(s, 0x12, &value);
1764 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1765 cirrus_hook_read_gr(s, 0x14, &value);
1767 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1768 cirrus_hook_read_gr(s, 0x01, &value);
1770 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1771 cirrus_hook_read_gr(s, 0x11, &value);
1773 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1774 cirrus_hook_read_gr(s, 0x13, &value);
1776 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1777 cirrus_hook_read_gr(s, 0x15, &value);
1779 case (CIRRUS_MMIO_BLTWIDTH + 0):
1780 cirrus_hook_read_gr(s, 0x20, &value);
1782 case (CIRRUS_MMIO_BLTWIDTH + 1):
1783 cirrus_hook_read_gr(s, 0x21, &value);
1785 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1786 cirrus_hook_read_gr(s, 0x22, &value);
1788 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1789 cirrus_hook_read_gr(s, 0x23, &value);
1791 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1792 cirrus_hook_read_gr(s, 0x24, &value);
1794 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1795 cirrus_hook_read_gr(s, 0x25, &value);
1797 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1798 cirrus_hook_read_gr(s, 0x26, &value);
1800 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1801 cirrus_hook_read_gr(s, 0x27, &value);
1803 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1804 cirrus_hook_read_gr(s, 0x28, &value);
1806 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1807 cirrus_hook_read_gr(s, 0x29, &value);
1809 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1810 cirrus_hook_read_gr(s, 0x2a, &value);
1812 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1813 cirrus_hook_read_gr(s, 0x2c, &value);
1815 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1816 cirrus_hook_read_gr(s, 0x2d, &value);
1818 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1819 cirrus_hook_read_gr(s, 0x2e, &value);
1821 case CIRRUS_MMIO_BLTWRITEMASK:
1822 cirrus_hook_read_gr(s, 0x2f, &value);
1824 case CIRRUS_MMIO_BLTMODE:
1825 cirrus_hook_read_gr(s, 0x30, &value);
1827 case CIRRUS_MMIO_BLTROP:
1828 cirrus_hook_read_gr(s, 0x32, &value);
1830 case CIRRUS_MMIO_BLTMODEEXT:
1831 cirrus_hook_read_gr(s, 0x33, &value);
1833 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1834 cirrus_hook_read_gr(s, 0x34, &value);
1836 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1837 cirrus_hook_read_gr(s, 0x35, &value);
1839 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1840 cirrus_hook_read_gr(s, 0x38, &value);
1842 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1843 cirrus_hook_read_gr(s, 0x39, &value);
1845 case CIRRUS_MMIO_BLTSTATUS:
1846 cirrus_hook_read_gr(s, 0x31, &value);
1850 printf("cirrus: mmio read - address 0x%04x\n", address);
1855 return (uint8_t) value;
1858 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1862 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1863 cirrus_hook_write_gr(s, 0x00, value);
1865 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1866 cirrus_hook_write_gr(s, 0x10, value);
1868 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1869 cirrus_hook_write_gr(s, 0x12, value);
1871 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1872 cirrus_hook_write_gr(s, 0x14, value);
1874 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1875 cirrus_hook_write_gr(s, 0x01, value);
1877 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1878 cirrus_hook_write_gr(s, 0x11, value);
1880 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1881 cirrus_hook_write_gr(s, 0x13, value);
1883 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1884 cirrus_hook_write_gr(s, 0x15, value);
1886 case (CIRRUS_MMIO_BLTWIDTH + 0):
1887 cirrus_hook_write_gr(s, 0x20, value);
1889 case (CIRRUS_MMIO_BLTWIDTH + 1):
1890 cirrus_hook_write_gr(s, 0x21, value);
1892 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1893 cirrus_hook_write_gr(s, 0x22, value);
1895 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1896 cirrus_hook_write_gr(s, 0x23, value);
1898 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1899 cirrus_hook_write_gr(s, 0x24, value);
1901 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1902 cirrus_hook_write_gr(s, 0x25, value);
1904 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1905 cirrus_hook_write_gr(s, 0x26, value);
1907 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1908 cirrus_hook_write_gr(s, 0x27, value);
1910 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1911 cirrus_hook_write_gr(s, 0x28, value);
1913 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1914 cirrus_hook_write_gr(s, 0x29, value);
1916 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1917 cirrus_hook_write_gr(s, 0x2a, value);
1919 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1922 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1923 cirrus_hook_write_gr(s, 0x2c, value);
1925 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1926 cirrus_hook_write_gr(s, 0x2d, value);
1928 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1929 cirrus_hook_write_gr(s, 0x2e, value);
1931 case CIRRUS_MMIO_BLTWRITEMASK:
1932 cirrus_hook_write_gr(s, 0x2f, value);
1934 case CIRRUS_MMIO_BLTMODE:
1935 cirrus_hook_write_gr(s, 0x30, value);
1937 case CIRRUS_MMIO_BLTROP:
1938 cirrus_hook_write_gr(s, 0x32, value);
1940 case CIRRUS_MMIO_BLTMODEEXT:
1941 cirrus_hook_write_gr(s, 0x33, value);
1943 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1944 cirrus_hook_write_gr(s, 0x34, value);
1946 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1947 cirrus_hook_write_gr(s, 0x35, value);
1949 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1950 cirrus_hook_write_gr(s, 0x38, value);
1952 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1953 cirrus_hook_write_gr(s, 0x39, value);
1955 case CIRRUS_MMIO_BLTSTATUS:
1956 cirrus_hook_write_gr(s, 0x31, value);
1960 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1967 /***************************************
1971 * assume TARGET_PAGE_SIZE >= 16
1973 ***************************************/
1975 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1981 unsigned val = mem_value;
1984 dst = s->vram_ptr + offset;
1985 for (x = 0; x < 8; x++) {
1987 *dst++ = s->cirrus_shadow_gr1;
1988 } else if (mode == 5) {
1989 *dst++ = s->cirrus_shadow_gr0;
1993 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1994 cpu_physical_memory_set_dirty(s->vram_offset + offset + 7);
1997 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
2003 unsigned val = mem_value;
2006 dst = s->vram_ptr + offset;
2007 for (x = 0; x < 8; x++) {
2009 *dst++ = s->cirrus_shadow_gr1;
2010 *dst++ = s->gr[0x11];
2011 } else if (mode == 5) {
2012 *dst++ = s->cirrus_shadow_gr0;
2013 *dst++ = s->gr[0x10];
2017 cpu_physical_memory_set_dirty(s->vram_offset + offset);
2018 cpu_physical_memory_set_dirty(s->vram_offset + offset + 15);
2021 /***************************************
2023 * memory access between 0xa0000-0xbffff
2025 ***************************************/
2027 static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
2029 CirrusVGAState *s = opaque;
2030 unsigned bank_index;
2031 unsigned bank_offset;
2034 if ((s->sr[0x07] & 0x01) == 0) {
2035 return vga_mem_readb(s, addr);
2040 if (addr < 0x10000) {
2041 /* XXX handle bitblt */
2043 bank_index = addr >> 15;
2044 bank_offset = addr & 0x7fff;
2045 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2046 bank_offset += s->cirrus_bank_base[bank_index];
2047 if ((s->gr[0x0B] & 0x14) == 0x14) {
2049 } else if (s->gr[0x0B] & 0x02) {
2052 bank_offset &= s->cirrus_addr_mask;
2053 val = *(s->vram_ptr + bank_offset);
2056 } else if (addr >= 0x18000 && addr < 0x18100) {
2057 /* memory-mapped I/O */
2059 if ((s->sr[0x17] & 0x44) == 0x04) {
2060 val = cirrus_mmio_blt_read(s, addr & 0xff);
2065 printf("cirrus: mem_readb %06x\n", addr);
2071 static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
2074 #ifdef TARGET_WORDS_BIGENDIAN
2075 v = cirrus_vga_mem_readb(opaque, addr) << 8;
2076 v |= cirrus_vga_mem_readb(opaque, addr + 1);
2078 v = cirrus_vga_mem_readb(opaque, addr);
2079 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2084 static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
2087 #ifdef TARGET_WORDS_BIGENDIAN
2088 v = cirrus_vga_mem_readb(opaque, addr) << 24;
2089 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
2090 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
2091 v |= cirrus_vga_mem_readb(opaque, addr + 3);
2093 v = cirrus_vga_mem_readb(opaque, addr);
2094 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2095 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
2096 v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
2101 static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr,
2104 CirrusVGAState *s = opaque;
2105 unsigned bank_index;
2106 unsigned bank_offset;
2109 if ((s->sr[0x07] & 0x01) == 0) {
2110 vga_mem_writeb(s, addr, mem_value);
2116 if (addr < 0x10000) {
2117 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2119 *s->cirrus_srcptr++ = (uint8_t) mem_value;
2120 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
2121 cirrus_bitblt_cputovideo_next(s);
2125 bank_index = addr >> 15;
2126 bank_offset = addr & 0x7fff;
2127 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2128 bank_offset += s->cirrus_bank_base[bank_index];
2129 if ((s->gr[0x0B] & 0x14) == 0x14) {
2131 } else if (s->gr[0x0B] & 0x02) {
2134 bank_offset &= s->cirrus_addr_mask;
2135 mode = s->gr[0x05] & 0x7;
2136 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2137 *(s->vram_ptr + bank_offset) = mem_value;
2138 cpu_physical_memory_set_dirty(s->vram_offset +
2141 if ((s->gr[0x0B] & 0x14) != 0x14) {
2142 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
2146 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2153 } else if (addr >= 0x18000 && addr < 0x18100) {
2154 /* memory-mapped I/O */
2155 if ((s->sr[0x17] & 0x44) == 0x04) {
2156 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2160 printf("cirrus: mem_writeb %06x value %02x\n", addr, mem_value);
2165 static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2167 #ifdef TARGET_WORDS_BIGENDIAN
2168 cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
2169 cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
2171 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2172 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2176 static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2178 #ifdef TARGET_WORDS_BIGENDIAN
2179 cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
2180 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2181 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2182 cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
2184 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2185 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2186 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2187 cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2191 static CPUReadMemoryFunc *cirrus_vga_mem_read[3] = {
2192 cirrus_vga_mem_readb,
2193 cirrus_vga_mem_readw,
2194 cirrus_vga_mem_readl,
2197 static CPUWriteMemoryFunc *cirrus_vga_mem_write[3] = {
2198 cirrus_vga_mem_writeb,
2199 cirrus_vga_mem_writew,
2200 cirrus_vga_mem_writel,
2203 /***************************************
2207 ***************************************/
2209 static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2211 CirrusVGAState *s = (CirrusVGAState *) opaque;
2214 /* XXX: s->vram_size must be a power of two */
2215 addr &= s->cirrus_addr_mask;
2217 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2218 /* memory-mapped I/O */
2219 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2221 /* XXX handle bitblt */
2225 if ((s->gr[0x0B] & 0x14) == 0x14) {
2227 } else if (s->gr[0x0B] & 0x02) {
2230 addr &= s->cirrus_addr_mask;
2231 ret = *(s->vram_ptr + addr);
2237 static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2240 #ifdef TARGET_WORDS_BIGENDIAN
2241 v = cirrus_linear_readb(opaque, addr) << 8;
2242 v |= cirrus_linear_readb(opaque, addr + 1);
2244 v = cirrus_linear_readb(opaque, addr);
2245 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2250 static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2253 #ifdef TARGET_WORDS_BIGENDIAN
2254 v = cirrus_linear_readb(opaque, addr) << 24;
2255 v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2256 v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2257 v |= cirrus_linear_readb(opaque, addr + 3);
2259 v = cirrus_linear_readb(opaque, addr);
2260 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2261 v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2262 v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2267 static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2270 CirrusVGAState *s = (CirrusVGAState *) opaque;
2273 addr &= s->cirrus_addr_mask;
2275 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2276 /* memory-mapped I/O */
2277 cirrus_mmio_blt_write(s, addr & 0xff, val);
2278 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2280 *s->cirrus_srcptr++ = (uint8_t) val;
2281 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
2282 cirrus_bitblt_cputovideo_next(s);
2286 if ((s->gr[0x0B] & 0x14) == 0x14) {
2288 } else if (s->gr[0x0B] & 0x02) {
2291 addr &= s->cirrus_addr_mask;
2293 mode = s->gr[0x05] & 0x7;
2294 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2295 *(s->vram_ptr + addr) = (uint8_t) val;
2296 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2298 if ((s->gr[0x0B] & 0x14) != 0x14) {
2299 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2301 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2307 static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2310 #ifdef TARGET_WORDS_BIGENDIAN
2311 cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2312 cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2314 cirrus_linear_writeb(opaque, addr, val & 0xff);
2315 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2319 static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2322 #ifdef TARGET_WORDS_BIGENDIAN
2323 cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2324 cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2325 cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2326 cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2328 cirrus_linear_writeb(opaque, addr, val & 0xff);
2329 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2330 cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2331 cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2336 static CPUReadMemoryFunc *cirrus_linear_read[3] = {
2337 cirrus_linear_readb,
2338 cirrus_linear_readw,
2339 cirrus_linear_readl,
2342 static CPUWriteMemoryFunc *cirrus_linear_write[3] = {
2343 cirrus_linear_writeb,
2344 cirrus_linear_writew,
2345 cirrus_linear_writel,
2350 static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
2352 CirrusVGAState *s = opaque;
2355 /* check port range access depending on color/monochrome mode */
2356 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2357 || (addr >= 0x3d0 && addr <= 0x3df
2358 && !(s->msr & MSR_COLOR_EMULATION))) {
2363 if (s->ar_flip_flop == 0) {
2370 index = s->ar_index & 0x1f;
2383 if (cirrus_hook_read_sr(s, s->sr_index, &val))
2385 val = s->sr[s->sr_index];
2386 #ifdef DEBUG_VGA_REG
2387 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2391 cirrus_read_hidden_dac(s, &val);
2397 if (cirrus_hook_read_palette(s, &val))
2399 val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
2400 if (++s->dac_sub_index == 3) {
2401 s->dac_sub_index = 0;
2402 s->dac_read_index++;
2415 if (cirrus_hook_read_gr(s, s->gr_index, &val))
2417 val = s->gr[s->gr_index];
2418 #ifdef DEBUG_VGA_REG
2419 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2428 if (cirrus_hook_read_cr(s, s->cr_index, &val))
2430 val = s->cr[s->cr_index];
2431 #ifdef DEBUG_VGA_REG
2432 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2437 /* just toggle to fool polling */
2438 s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
2440 s->ar_flip_flop = 0;
2447 #if defined(DEBUG_VGA)
2448 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2453 static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2455 CirrusVGAState *s = opaque;
2458 /* check port range access depending on color/monochrome mode */
2459 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2460 || (addr >= 0x3d0 && addr <= 0x3df
2461 && !(s->msr & MSR_COLOR_EMULATION)))
2465 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2470 if (s->ar_flip_flop == 0) {
2474 index = s->ar_index & 0x1f;
2477 s->ar[index] = val & 0x3f;
2480 s->ar[index] = val & ~0x10;
2486 s->ar[index] = val & ~0xc0;
2489 s->ar[index] = val & ~0xf0;
2492 s->ar[index] = val & ~0xf0;
2498 s->ar_flip_flop ^= 1;
2501 s->msr = val & ~0x10;
2507 if (cirrus_hook_write_sr(s, s->sr_index, val))
2509 #ifdef DEBUG_VGA_REG
2510 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2512 s->sr[s->sr_index] = val & sr_mask[s->sr_index];
2515 cirrus_write_hidden_dac(s, val);
2518 s->dac_read_index = val;
2519 s->dac_sub_index = 0;
2523 s->dac_write_index = val;
2524 s->dac_sub_index = 0;
2528 if (cirrus_hook_write_palette(s, val))
2530 s->dac_cache[s->dac_sub_index] = val;
2531 if (++s->dac_sub_index == 3) {
2532 memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
2533 s->dac_sub_index = 0;
2534 s->dac_write_index++;
2541 if (cirrus_hook_write_gr(s, s->gr_index, val))
2543 #ifdef DEBUG_VGA_REG
2544 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2546 s->gr[s->gr_index] = val & gr_mask[s->gr_index];
2554 if (cirrus_hook_write_cr(s, s->cr_index, val))
2556 #ifdef DEBUG_VGA_REG
2557 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2559 /* handle CR0-7 protection */
2560 if ((s->cr[11] & 0x80) && s->cr_index <= 7) {
2561 /* can always write bit 4 of CR7 */
2562 if (s->cr_index == 7)
2563 s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
2566 switch (s->cr_index) {
2567 case 0x01: /* horizontal display end */
2572 case 0x12: /* veritcal display end */
2573 s->cr[s->cr_index] = val;
2577 s->cr[s->cr_index] = val;
2583 s->fcr = val & 0x10;
2588 /***************************************
2590 * memory-mapped I/O access
2592 ***************************************/
2594 static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2596 CirrusVGAState *s = (CirrusVGAState *) opaque;
2598 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2600 if (addr >= 0x100) {
2601 return cirrus_mmio_blt_read(s, addr - 0x100);
2603 return vga_ioport_read(s, addr + 0x3c0);
2607 static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2610 #ifdef TARGET_WORDS_BIGENDIAN
2611 v = cirrus_mmio_readb(opaque, addr) << 8;
2612 v |= cirrus_mmio_readb(opaque, addr + 1);
2614 v = cirrus_mmio_readb(opaque, addr);
2615 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2620 static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2623 #ifdef TARGET_WORDS_BIGENDIAN
2624 v = cirrus_mmio_readb(opaque, addr) << 24;
2625 v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2626 v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2627 v |= cirrus_mmio_readb(opaque, addr + 3);
2629 v = cirrus_mmio_readb(opaque, addr);
2630 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2631 v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2632 v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2637 static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2640 CirrusVGAState *s = (CirrusVGAState *) opaque;
2642 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2644 if (addr >= 0x100) {
2645 cirrus_mmio_blt_write(s, addr - 0x100, val);
2647 vga_ioport_write(s, addr + 0x3c0, val);
2651 static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2654 #ifdef TARGET_WORDS_BIGENDIAN
2655 cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2656 cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2658 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2659 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2663 static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2666 #ifdef TARGET_WORDS_BIGENDIAN
2667 cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2668 cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2669 cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2670 cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2672 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2673 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2674 cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2675 cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2680 static CPUReadMemoryFunc *cirrus_mmio_read[3] = {
2686 static CPUWriteMemoryFunc *cirrus_mmio_write[3] = {
2692 /***************************************
2696 ***************************************/
2698 static void cirrus_init_common(CirrusVGAState * s)
2702 register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
2704 register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
2705 register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
2706 register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
2707 register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
2709 register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
2711 register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
2712 register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
2713 register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
2714 register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
2716 vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read,
2717 cirrus_vga_mem_write, s);
2718 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
2722 s->sr[0x0F] = CIRRUS_MEMSIZE_2M;
2723 s->sr[0x1F] = 0x22; // MemClock
2725 s->cr[0x27] = CIRRUS_ID;
2727 s->cirrus_hidden_dac_lockindex = 5;
2728 s->cirrus_hidden_dac_data = 0;
2730 /* I/O handler for LFB */
2731 s->cirrus_linear_io_addr =
2732 cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,
2734 /* I/O handler for memory-mapped I/O */
2735 s->cirrus_mmio_io_addr =
2736 cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);
2738 /* XXX: s->vram_size must be a power of two */
2739 s->cirrus_addr_mask = s->vram_size - 1;
2741 s->get_bpp = cirrus_get_bpp;
2742 s->get_offsets = cirrus_get_offsets;
2745 /***************************************
2749 ***************************************/
2751 void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2752 unsigned long vga_ram_offset, int vga_ram_size)
2756 s = qemu_mallocz(sizeof(CirrusVGAState));
2758 vga_common_init((VGAState *)s,
2759 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2760 cirrus_init_common(s);
2761 s->sr[0x17] = CIRRUS_BUSTYPE_ISA;
2762 /* XXX ISA-LFB support */
2765 /***************************************
2769 ***************************************/
2771 static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
2772 uint32_t addr, uint32_t size, int type)
2774 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2776 cpu_register_physical_memory(addr, s->vram_size,
2777 s->cirrus_linear_io_addr);
2780 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
2781 uint32_t addr, uint32_t size, int type)
2783 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2785 cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
2786 s->cirrus_mmio_io_addr);
2789 void pci_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2790 unsigned long vga_ram_offset, int vga_ram_size)
2792 PCICirrusVGAState *d;
2796 /* setup PCI configuration registers */
2797 d = (PCICirrusVGAState *)pci_register_device("Cirrus VGA",
2798 sizeof(PCICirrusVGAState),
2800 pci_conf = d->dev.config;
2801 pci_conf[0x00] = (uint8_t) (PCI_VENDOR_CIRRUS & 0xff);
2802 pci_conf[0x01] = (uint8_t) (PCI_VENDOR_CIRRUS >> 8);
2803 pci_conf[0x02] = (uint8_t) (PCI_DEVICE_ID & 0xff);
2804 pci_conf[0x03] = (uint8_t) (PCI_DEVICE_ID >> 8);
2805 pci_conf[0x04] = PCI_COMMAND_IOACCESS | PCI_COMMAND_MEMACCESS;
2806 pci_conf[0x0a] = PCI_CLASS_SUB_VGA;
2807 pci_conf[0x0b] = PCI_CLASS_BASE_DISPLAY;
2808 pci_conf[0x0e] = PCI_CLASS_HEADERTYPE_00h;
2812 vga_common_init((VGAState *)s,
2813 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2814 cirrus_init_common(s);
2815 s->sr[0x17] = CIRRUS_BUSTYPE_PCI;
2817 /* setup memory space */
2819 /* memory #1 memory-mapped I/O */
2820 /* XXX: s->vram_size must be a power of two */
2821 pci_register_io_region((PCIDevice *)d, 0, s->vram_size,
2822 PCI_ADDRESS_SPACE_MEM_PREFETCH, cirrus_pci_lfb_map);
2823 if (CIRRUS_ID == CIRRUS_ID_CLGD5446) {
2824 pci_register_io_region((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
2825 PCI_ADDRESS_SPACE_MEM, cirrus_pci_mmio_map);