2 * QEMU Cirrus VGA Emulator.
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2004 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
28 //#define DEBUG_CIRRUS
30 /***************************************
34 ***************************************/
36 #define qemu_MIN(a,b) ((a) < (b) ? (a) : (b))
39 #define CIRRUS_ID_CLGD5422 (0x23<<2)
40 #define CIRRUS_ID_CLGD5426 (0x24<<2)
41 #define CIRRUS_ID_CLGD5424 (0x25<<2)
42 #define CIRRUS_ID_CLGD5428 (0x26<<2)
43 #define CIRRUS_ID_CLGD5430 (0x28<<2)
44 #define CIRRUS_ID_CLGD5434 (0x2A<<2)
45 #define CIRRUS_ID_CLGD5446 (0x2E<<2)
48 #define CIRRUS_SR7_BPP_VGA 0x00
49 #define CIRRUS_SR7_BPP_SVGA 0x01
50 #define CIRRUS_SR7_BPP_MASK 0x0e
51 #define CIRRUS_SR7_BPP_8 0x00
52 #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
53 #define CIRRUS_SR7_BPP_24 0x04
54 #define CIRRUS_SR7_BPP_16 0x06
55 #define CIRRUS_SR7_BPP_32 0x08
56 #define CIRRUS_SR7_ISAADDR_MASK 0xe0
59 #define CIRRUS_MEMSIZE_512k 0x08
60 #define CIRRUS_MEMSIZE_1M 0x10
61 #define CIRRUS_MEMSIZE_2M 0x18
62 #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
65 #define CIRRUS_CURSOR_SHOW 0x01
66 #define CIRRUS_CURSOR_HIDDENPEL 0x02
67 #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
70 #define CIRRUS_BUSTYPE_VLBFAST 0x10
71 #define CIRRUS_BUSTYPE_PCI 0x20
72 #define CIRRUS_BUSTYPE_VLBSLOW 0x30
73 #define CIRRUS_BUSTYPE_ISA 0x38
74 #define CIRRUS_MMIO_ENABLE 0x04
75 #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
76 #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
79 #define CIRRUS_BANKING_DUAL 0x01
80 #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
83 #define CIRRUS_BLTMODE_BACKWARDS 0x01
84 #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
85 #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
86 #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
87 #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
88 #define CIRRUS_BLTMODE_COLOREXPAND 0x80
89 #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
90 #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
91 #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
92 #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
93 #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
96 #define CIRRUS_BLT_BUSY 0x01
97 #define CIRRUS_BLT_START 0x02
98 #define CIRRUS_BLT_RESET 0x04
99 #define CIRRUS_BLT_FIFOUSED 0x10
102 #define CIRRUS_ROP_0 0x00
103 #define CIRRUS_ROP_SRC_AND_DST 0x05
104 #define CIRRUS_ROP_NOP 0x06
105 #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
106 #define CIRRUS_ROP_NOTDST 0x0b
107 #define CIRRUS_ROP_SRC 0x0d
108 #define CIRRUS_ROP_1 0x0e
109 #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
110 #define CIRRUS_ROP_SRC_XOR_DST 0x59
111 #define CIRRUS_ROP_SRC_OR_DST 0x6d
112 #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
113 #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
114 #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
115 #define CIRRUS_ROP_NOTSRC 0xd0
116 #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
117 #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
120 #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
121 #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
122 #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
123 #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
124 #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
125 #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
126 #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
127 #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
128 #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
129 #define CIRRUS_MMIO_BLTMODE 0x18 // byte
130 #define CIRRUS_MMIO_BLTROP 0x1a // byte
131 #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
132 #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
133 #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
134 #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
135 #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
136 #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
137 #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
138 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
139 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
140 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
141 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
142 #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
143 #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
144 #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
145 #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
146 #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
147 #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
148 #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
150 // PCI 0x00: vendor, 0x02: device
151 #define PCI_VENDOR_CIRRUS 0x1013
152 #define PCI_DEVICE_CLGD5430 0x00a0 // CLGD5430 or CLGD5440
153 #define PCI_DEVICE_CLGD5434 0x00a8
154 #define PCI_DEVICE_CLGD5436 0x00ac
155 #define PCI_DEVICE_CLGD5446 0x00b8
156 #define PCI_DEVICE_CLGD5462 0x00d0
157 #define PCI_DEVICE_CLGD5465 0x00d6
158 // PCI 0x04: command(word), 0x06(word): status
159 #define PCI_COMMAND_IOACCESS 0x0001
160 #define PCI_COMMAND_MEMACCESS 0x0002
161 #define PCI_COMMAND_BUSMASTER 0x0004
162 #define PCI_COMMAND_SPECIALCYCLE 0x0008
163 #define PCI_COMMAND_MEMWRITEINVALID 0x0010
164 #define PCI_COMMAND_PALETTESNOOPING 0x0020
165 #define PCI_COMMAND_PARITYDETECTION 0x0040
166 #define PCI_COMMAND_ADDRESSDATASTEPPING 0x0080
167 #define PCI_COMMAND_SERR 0x0100
168 #define PCI_COMMAND_BACKTOBACKTRANS 0x0200
169 // PCI 0x08, 0xff000000 (0x09-0x0b:class,0x08:rev)
170 #define PCI_CLASS_BASE_DISPLAY 0x03
171 // PCI 0x08, 0x00ff0000
172 #define PCI_CLASS_SUB_VGA 0x00
173 // PCI 0x0c, 0x00ff0000 (0x0c:cacheline,0x0d:latency,0x0e:headertype,0x0f:Built-in self test)
174 #define PCI_CLASS_HEADERTYPE_00h 0x00
175 // 0x10-0x3f (headertype 00h)
176 // PCI 0x10,0x14,0x18,0x1c,0x20,0x24: base address mapping registers
177 // 0x10: MEMBASE, 0x14: IOBASE(hard-coded in XFree86 3.x)
178 #define PCI_MAP_MEM 0x0
179 #define PCI_MAP_IO 0x1
180 #define PCI_MAP_MEM_ADDR_MASK (~0xf)
181 #define PCI_MAP_IO_ADDR_MASK (~0x3)
182 #define PCI_MAP_MEMFLAGS_32BIT 0x0
183 #define PCI_MAP_MEMFLAGS_32BIT_1M 0x1
184 #define PCI_MAP_MEMFLAGS_64BIT 0x4
185 #define PCI_MAP_MEMFLAGS_CACHEABLE 0x8
186 // PCI 0x28: cardbus CIS pointer
187 // PCI 0x2c: subsystem vendor id, 0x2e: subsystem id
188 // PCI 0x30: expansion ROM base address
189 #define PCI_ROMBIOS_ENABLED 0x1
190 // PCI 0x34: 0xffffff00=reserved, 0x000000ff=capabilities pointer
191 // PCI 0x38: reserved
192 // PCI 0x3c: 0x3c=int-line, 0x3d=int-pin, 0x3e=min-gnt, 0x3f=maax-lat
194 #define CIRRUS_PNPMMIO_SIZE 0x800
197 /* I/O and memory hook */
198 #define CIRRUS_HOOK_NOT_HANDLED 0
199 #define CIRRUS_HOOK_HANDLED 1
201 typedef void (*cirrus_bitblt_rop_t) (uint8_t * dst, const uint8_t * src,
202 int dstpitch, int srcpitch,
203 int bltwidth, int bltheight);
205 typedef void (*cirrus_bitblt_handler_t) (void *opaque);
207 typedef struct CirrusVGAState {
210 int cirrus_linear_io_addr;
211 int cirrus_mmio_io_addr;
212 uint32_t cirrus_addr_mask;
213 uint8_t cirrus_shadow_gr0;
214 uint8_t cirrus_shadow_gr1;
215 uint8_t cirrus_hidden_dac_lockindex;
216 uint8_t cirrus_hidden_dac_data;
217 uint32_t cirrus_bank_base[2];
218 uint32_t cirrus_bank_limit[2];
219 uint8_t cirrus_hidden_palette[48];
220 uint32_t cirrus_hw_cursor_x;
221 uint32_t cirrus_hw_cursor_y;
222 int cirrus_blt_pixelwidth;
223 int cirrus_blt_width;
224 int cirrus_blt_height;
225 int cirrus_blt_dstpitch;
226 int cirrus_blt_srcpitch;
227 uint32_t cirrus_blt_dstaddr;
228 uint32_t cirrus_blt_srcaddr;
229 uint8_t cirrus_blt_mode;
230 cirrus_bitblt_rop_t cirrus_rop;
231 #define CIRRUS_BLTBUFSIZE 256
232 uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
233 uint8_t *cirrus_srcptr;
234 uint8_t *cirrus_srcptr_end;
235 uint32_t cirrus_srccounter;
236 uint8_t *cirrus_dstptr;
237 uint8_t *cirrus_dstptr_end;
238 uint32_t cirrus_dstcounter;
239 cirrus_bitblt_handler_t cirrus_blt_handler;
240 int cirrus_blt_horz_counter;
243 typedef struct PCICirrusVGAState {
245 CirrusVGAState cirrus_vga;
248 /***************************************
252 ***************************************/
255 static void cirrus_bitblt_reset(CirrusVGAState * s);
257 /***************************************
261 ***************************************/
263 #define IMPLEMENT_FORWARD_BITBLT(name,opline) \
265 cirrus_bitblt_rop_fwd_##name( \
266 uint8_t *dst,const uint8_t *src, \
267 int dstpitch,int srcpitch, \
268 int bltwidth,int bltheight) \
271 dstpitch -= bltwidth; \
272 srcpitch -= bltwidth; \
273 for (y = 0; y < bltheight; y++) { \
274 for (x = 0; x < bltwidth; x++) { \
284 #define IMPLEMENT_BACKWARD_BITBLT(name,opline) \
286 cirrus_bitblt_rop_bkwd_##name( \
287 uint8_t *dst,const uint8_t *src, \
288 int dstpitch,int srcpitch, \
289 int bltwidth,int bltheight) \
292 dstpitch += bltwidth; \
293 srcpitch += bltwidth; \
294 for (y = 0; y < bltheight; y++) { \
295 for (x = 0; x < bltwidth; x++) { \
305 IMPLEMENT_FORWARD_BITBLT(0, *dst = 0)
306 IMPLEMENT_FORWARD_BITBLT(src_and_dst, *dst = (*src) & (*dst))
307 IMPLEMENT_FORWARD_BITBLT(nop, (void) 0)
308 IMPLEMENT_FORWARD_BITBLT(src_and_notdst, *dst = (*src) & (~(*dst)))
309 IMPLEMENT_FORWARD_BITBLT(notdst, *dst = ~(*dst))
310 IMPLEMENT_FORWARD_BITBLT(src, *dst = *src)
311 IMPLEMENT_FORWARD_BITBLT(1, *dst = 0xff)
312 IMPLEMENT_FORWARD_BITBLT(notsrc_and_dst, *dst = (~(*src)) & (*dst))
313 IMPLEMENT_FORWARD_BITBLT(src_xor_dst, *dst = (*src) ^ (*dst))
314 IMPLEMENT_FORWARD_BITBLT(src_or_dst, *dst = (*src) | (*dst))
315 IMPLEMENT_FORWARD_BITBLT(notsrc_or_notdst, *dst = (~(*src)) | (~(*dst)))
316 IMPLEMENT_FORWARD_BITBLT(src_notxor_dst, *dst = ~((*src) ^ (*dst)))
317 IMPLEMENT_FORWARD_BITBLT(src_or_notdst, *dst = (*src) | (~(*dst)))
318 IMPLEMENT_FORWARD_BITBLT(notsrc, *dst = (~(*src)))
319 IMPLEMENT_FORWARD_BITBLT(notsrc_or_dst, *dst = (~(*src)) | (*dst))
320 IMPLEMENT_FORWARD_BITBLT(notsrc_and_notdst, *dst = (~(*src)) & (~(*dst)))
322 IMPLEMENT_BACKWARD_BITBLT(0, *dst = 0)
323 IMPLEMENT_BACKWARD_BITBLT(src_and_dst, *dst = (*src) & (*dst))
324 IMPLEMENT_BACKWARD_BITBLT(nop, (void) 0)
325 IMPLEMENT_BACKWARD_BITBLT(src_and_notdst, *dst = (*src) & (~(*dst)))
326 IMPLEMENT_BACKWARD_BITBLT(notdst, *dst = ~(*dst))
327 IMPLEMENT_BACKWARD_BITBLT(src, *dst = *src)
328 IMPLEMENT_BACKWARD_BITBLT(1, *dst = 0xff)
329 IMPLEMENT_BACKWARD_BITBLT(notsrc_and_dst, *dst = (~(*src)) & (*dst))
330 IMPLEMENT_BACKWARD_BITBLT(src_xor_dst, *dst = (*src) ^ (*dst))
331 IMPLEMENT_BACKWARD_BITBLT(src_or_dst, *dst = (*src) | (*dst))
332 IMPLEMENT_BACKWARD_BITBLT(notsrc_or_notdst, *dst = (~(*src)) | (~(*dst)))
333 IMPLEMENT_BACKWARD_BITBLT(src_notxor_dst, *dst = ~((*src) ^ (*dst)))
334 IMPLEMENT_BACKWARD_BITBLT(src_or_notdst, *dst = (*src) | (~(*dst)))
335 IMPLEMENT_BACKWARD_BITBLT(notsrc, *dst = (~(*src)))
336 IMPLEMENT_BACKWARD_BITBLT(notsrc_or_dst, *dst = (~(*src)) | (*dst))
337 IMPLEMENT_BACKWARD_BITBLT(notsrc_and_notdst, *dst = (~(*src)) & (~(*dst)))
339 static cirrus_bitblt_rop_t cirrus_get_fwd_rop_handler(uint8_t rop)
341 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_fwd_nop;
345 rop_handler = cirrus_bitblt_rop_fwd_0;
347 case CIRRUS_ROP_SRC_AND_DST:
348 rop_handler = cirrus_bitblt_rop_fwd_src_and_dst;
351 rop_handler = cirrus_bitblt_rop_fwd_nop;
353 case CIRRUS_ROP_SRC_AND_NOTDST:
354 rop_handler = cirrus_bitblt_rop_fwd_src_and_notdst;
356 case CIRRUS_ROP_NOTDST:
357 rop_handler = cirrus_bitblt_rop_fwd_notdst;
360 rop_handler = cirrus_bitblt_rop_fwd_src;
363 rop_handler = cirrus_bitblt_rop_fwd_1;
365 case CIRRUS_ROP_NOTSRC_AND_DST:
366 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_dst;
368 case CIRRUS_ROP_SRC_XOR_DST:
369 rop_handler = cirrus_bitblt_rop_fwd_src_xor_dst;
371 case CIRRUS_ROP_SRC_OR_DST:
372 rop_handler = cirrus_bitblt_rop_fwd_src_or_dst;
374 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
375 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_notdst;
377 case CIRRUS_ROP_SRC_NOTXOR_DST:
378 rop_handler = cirrus_bitblt_rop_fwd_src_notxor_dst;
380 case CIRRUS_ROP_SRC_OR_NOTDST:
381 rop_handler = cirrus_bitblt_rop_fwd_src_or_notdst;
383 case CIRRUS_ROP_NOTSRC:
384 rop_handler = cirrus_bitblt_rop_fwd_notsrc;
386 case CIRRUS_ROP_NOTSRC_OR_DST:
387 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_dst;
389 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
390 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_notdst;
394 printf("unknown ROP %02x\n", rop);
402 static cirrus_bitblt_rop_t cirrus_get_bkwd_rop_handler(uint8_t rop)
404 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_bkwd_nop;
408 rop_handler = cirrus_bitblt_rop_bkwd_0;
410 case CIRRUS_ROP_SRC_AND_DST:
411 rop_handler = cirrus_bitblt_rop_bkwd_src_and_dst;
414 rop_handler = cirrus_bitblt_rop_bkwd_nop;
416 case CIRRUS_ROP_SRC_AND_NOTDST:
417 rop_handler = cirrus_bitblt_rop_bkwd_src_and_notdst;
419 case CIRRUS_ROP_NOTDST:
420 rop_handler = cirrus_bitblt_rop_bkwd_notdst;
423 rop_handler = cirrus_bitblt_rop_bkwd_src;
426 rop_handler = cirrus_bitblt_rop_bkwd_1;
428 case CIRRUS_ROP_NOTSRC_AND_DST:
429 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_dst;
431 case CIRRUS_ROP_SRC_XOR_DST:
432 rop_handler = cirrus_bitblt_rop_bkwd_src_xor_dst;
434 case CIRRUS_ROP_SRC_OR_DST:
435 rop_handler = cirrus_bitblt_rop_bkwd_src_or_dst;
437 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
438 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_notdst;
440 case CIRRUS_ROP_SRC_NOTXOR_DST:
441 rop_handler = cirrus_bitblt_rop_bkwd_src_notxor_dst;
443 case CIRRUS_ROP_SRC_OR_NOTDST:
444 rop_handler = cirrus_bitblt_rop_bkwd_src_or_notdst;
446 case CIRRUS_ROP_NOTSRC:
447 rop_handler = cirrus_bitblt_rop_bkwd_notsrc;
449 case CIRRUS_ROP_NOTSRC_OR_DST:
450 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_dst;
452 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
453 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_notdst;
457 printf("unknown ROP %02x\n", rop);
465 /***************************************
469 ***************************************/
472 cirrus_colorexpand_8(CirrusVGAState * s, uint8_t * dst,
473 const uint8_t * src, int count)
481 colors[0] = s->gr[0x00];
482 colors[1] = s->gr[0x01];
484 bitmask = 0x80 >> srcskipleft;
486 for (x = 0; x < count; x++) {
487 if ((bitmask & 0xff) == 0) {
491 *dst++ = colors[!!(bits & bitmask)];
497 cirrus_colorexpand_16(CirrusVGAState * s, uint8_t * dst,
498 const uint8_t * src, int count)
501 uint8_t colors[2][2];
507 colors[0][0] = s->gr[0x00];
508 colors[0][1] = s->gr[0x10];
509 colors[1][0] = s->gr[0x01];
510 colors[1][1] = s->gr[0x11];
512 bitmask = 0x80 >> srcskipleft;
514 for (x = 0; x < count; x++) {
515 if ((bitmask & 0xff) == 0) {
519 index = !!(bits & bitmask);
520 *dst++ = colors[index][0];
521 *dst++ = colors[index][1];
527 cirrus_colorexpand_24(CirrusVGAState * s, uint8_t * dst,
528 const uint8_t * src, int count)
531 uint8_t colors[2][3];
537 colors[0][0] = s->gr[0x00];
538 colors[0][1] = s->gr[0x10];
539 colors[0][2] = s->gr[0x12];
540 colors[1][0] = s->gr[0x01];
541 colors[1][1] = s->gr[0x11];
542 colors[1][2] = s->gr[0x13];
544 bitmask = 0x80 << srcskipleft;
546 for (x = 0; x < count; x++) {
547 if ((bitmask & 0xff) == 0) {
551 index = !!(bits & bitmask);
552 *dst++ = colors[index][0];
553 *dst++ = colors[index][1];
554 *dst++ = colors[index][2];
560 cirrus_colorexpand_32(CirrusVGAState * s, uint8_t * dst,
561 const uint8_t * src, int count)
564 uint8_t colors[2][4];
570 colors[0][0] = s->gr[0x00];
571 colors[0][1] = s->gr[0x10];
572 colors[0][2] = s->gr[0x12];
573 colors[0][3] = s->gr[0x14];
574 colors[1][0] = s->gr[0x01];
575 colors[1][1] = s->gr[0x11];
576 colors[1][2] = s->gr[0x13];
577 colors[1][3] = s->gr[0x15];
579 bitmask = 0x80 << srcskipleft;
581 for (x = 0; x < count; x++) {
582 if ((bitmask & 0xff) == 0) {
586 index = !!(bits & bitmask);
587 *dst++ = colors[index][0];
588 *dst++ = colors[index][1];
589 *dst++ = colors[index][2];
590 *dst++ = colors[index][3];
596 cirrus_colorexpand(CirrusVGAState * s, uint8_t * dst, const uint8_t * src,
599 switch (s->cirrus_blt_pixelwidth) {
601 cirrus_colorexpand_8(s, dst, src, count);
604 cirrus_colorexpand_16(s, dst, src, count);
607 cirrus_colorexpand_24(s, dst, src, count);
610 cirrus_colorexpand_32(s, dst, src, count);
614 printf("cirrus: COLOREXPAND pixelwidth %d - unimplemented\n",
615 s->cirrus_blt_pixelwidth);
621 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
622 int off_pitch, int bytesperline,
629 for (y = 0; y < lines; y++) {
631 off_cur_end = off_cur + bytesperline;
632 off_cur &= TARGET_PAGE_MASK;
633 while (off_cur < off_cur_end) {
634 cpu_physical_memory_set_dirty(s->vram_offset + off_cur);
635 off_cur += TARGET_PAGE_SIZE;
637 off_begin += off_pitch;
643 static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
646 uint8_t work_colorexp[256];
650 int tilewidth, tileheight;
651 int patternbytes = s->cirrus_blt_pixelwidth * 8;
653 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
654 cirrus_colorexpand(s, work_colorexp, src, 8 * 8);
656 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_COLOREXPAND;
658 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_PATTERNCOPY) {
660 printf("cirrus: blt mode %02x (pattercopy) - unimplemented\n",
666 dst = s->vram_ptr + s->cirrus_blt_dstaddr;
667 for (y = 0; y < s->cirrus_blt_height; y += 8) {
669 tileheight = qemu_MIN(8, s->cirrus_blt_height - y);
670 for (x = 0; x < s->cirrus_blt_width; x += patternbytes) {
671 tilewidth = qemu_MIN(patternbytes, s->cirrus_blt_width - x);
672 (*s->cirrus_rop) (dstc, src,
673 s->cirrus_blt_dstpitch, patternbytes,
674 tilewidth, tileheight);
675 dstc += patternbytes;
677 dst += s->cirrus_blt_dstpitch * 8;
679 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
680 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
681 s->cirrus_blt_height);
685 /***************************************
687 * bitblt (video-to-video)
689 ***************************************/
691 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
693 return cirrus_bitblt_common_patterncopy(s,
695 s->cirrus_blt_srcaddr);
698 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
700 if ((s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) != 0) {
702 printf("cirrus: CIRRUS_BLTMODE_COLOREXPAND - unimplemented\n");
706 if ((s->cirrus_blt_mode & (~CIRRUS_BLTMODE_BACKWARDS)) != 0) {
708 printf("cirrus: blt mode %02x - unimplemented\n",
714 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
715 s->vram_ptr + s->cirrus_blt_srcaddr,
716 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
717 s->cirrus_blt_width, s->cirrus_blt_height);
718 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
719 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
720 s->cirrus_blt_height);
724 /***************************************
726 * bitblt (cpu-to-video)
728 ***************************************/
730 static void cirrus_bitblt_cputovideo_patterncopy(void *opaque)
732 CirrusVGAState *s = (CirrusVGAState *) opaque;
735 data_count = s->cirrus_srcptr - &s->cirrus_bltbuf[0];
737 if (data_count > 0) {
738 if (data_count != s->cirrus_srccounter) {
740 printf("cirrus: internal error\n");
743 cirrus_bitblt_common_patterncopy(s, &s->cirrus_bltbuf[0]);
745 cirrus_bitblt_reset(s);
749 static void cirrus_bitblt_cputovideo_copy(void *opaque)
751 CirrusVGAState *s = (CirrusVGAState *) opaque;
754 uint8_t work_colorexp[256];
755 uint8_t *src_ptr = NULL;
760 if (s->cirrus_blt_height <= 0) {
761 s->cirrus_srcptr = s->cirrus_srcptr_end;
765 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
767 /* get BLT source. */
768 if (src_avail <= 0) {
769 data_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
773 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
774 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_COLOREXPAND) {
776 printf("cirrus: unsupported\n");
778 cirrus_bitblt_reset(s);
781 data_avail = qemu_MIN(data_count, 256 / 32);
782 cirrus_colorexpand(s, work_colorexp, s->cirrus_srcptr,
784 src_ptr = &work_colorexp[0];
785 src_avail = data_avail * 8 * s->cirrus_blt_pixelwidth;
786 s->cirrus_srcptr += data_avail;
788 ((s->cirrus_blt_width + 7) / 8) * 8 -
790 src_linepad *= s->cirrus_blt_pixelwidth;
792 if (s->cirrus_blt_mode != 0) {
794 printf("cirrus: unsupported\n");
796 cirrus_bitblt_reset(s);
799 src_ptr = s->cirrus_srcptr;
801 data_count / s->cirrus_blt_pixelwidth *
802 s->cirrus_blt_pixelwidth;
803 s->cirrus_srcptr += src_avail;
811 s->cirrus_blt_srcpitch - s->cirrus_blt_horz_counter;
812 src_processing = qemu_MIN(src_avail, src_processing);
813 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
814 src_ptr, 0, 0, src_processing, 1);
815 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
818 s->cirrus_blt_dstaddr += src_processing;
819 src_ptr += src_processing;
820 src_avail -= src_processing;
821 s->cirrus_blt_horz_counter += src_processing;
822 if (s->cirrus_blt_horz_counter >= s->cirrus_blt_srcpitch) {
823 src_ptr += src_linepad;
824 src_avail -= src_linepad;
825 s->cirrus_blt_dstaddr +=
826 s->cirrus_blt_dstpitch - s->cirrus_blt_srcpitch;
827 s->cirrus_blt_horz_counter = 0;
828 s->cirrus_blt_height--;
829 if (s->cirrus_blt_height <= 0) {
830 s->cirrus_srcptr = s->cirrus_srcptr_end;
837 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
842 s->cirrus_blt_handler(s);
844 if (s->cirrus_srccounter > 0) {
845 s->cirrus_srccounter -= s->cirrus_srcptr - &s->cirrus_bltbuf[0];
846 copy_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
847 memmove(&s->cirrus_bltbuf[0], s->cirrus_srcptr, copy_count);
848 avail_count = qemu_MIN(CIRRUS_BLTBUFSIZE, s->cirrus_srccounter);
849 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
850 s->cirrus_srcptr_end = s->cirrus_srcptr + avail_count;
851 if (s->cirrus_srccounter <= 0) {
852 cirrus_bitblt_reset(s);
857 /***************************************
861 ***************************************/
863 static void cirrus_bitblt_reset(CirrusVGAState * s)
866 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
867 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
868 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
869 s->cirrus_srccounter = 0;
870 s->cirrus_dstptr = &s->cirrus_bltbuf[0];
871 s->cirrus_dstptr_end = &s->cirrus_bltbuf[0];
872 s->cirrus_dstcounter = 0;
873 s->cirrus_blt_handler = NULL;
876 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
878 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
879 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
880 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
882 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
883 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
884 s->cirrus_srccounter = 8;
886 s->cirrus_srccounter = 8 * 8 * s->cirrus_blt_pixelwidth;
888 s->cirrus_blt_srcpitch = 0;
889 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_patterncopy;
891 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
892 s->cirrus_srccounter =
893 ((s->cirrus_blt_width + 7) / 8) * s->cirrus_blt_height;
894 s->cirrus_blt_srcpitch =
895 s->cirrus_blt_width * s->cirrus_blt_pixelwidth;
897 s->cirrus_srccounter =
898 s->cirrus_blt_width * s->cirrus_blt_height;
899 s->cirrus_blt_srcpitch = s->cirrus_blt_width;
901 /* 4-byte alignment */
902 s->cirrus_srccounter = (s->cirrus_srccounter + 3) & (~3);
904 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_copy;
905 s->cirrus_blt_horz_counter = 0;
908 cirrus_bitblt_cputovideo_next(s);
912 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
916 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
921 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
925 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
926 ret = cirrus_bitblt_videotovideo_patterncopy(s);
928 ret = cirrus_bitblt_videotovideo_copy(s);
932 cirrus_bitblt_reset(s);
936 static void cirrus_bitblt_start(CirrusVGAState * s)
940 s->cirrus_blt_width = (s->gr[0x20] | (s->gr[0x21] << 8)) + 1;
941 s->cirrus_blt_height = (s->gr[0x22] | (s->gr[0x23] << 8)) + 1;
942 s->cirrus_blt_dstpitch = (s->gr[0x24] | (s->gr[0x25] << 8));
943 s->cirrus_blt_srcpitch = (s->gr[0x26] | (s->gr[0x27] << 8));
944 s->cirrus_blt_dstaddr =
945 (s->gr[0x28] | (s->gr[0x29] << 8) | (s->gr[0x2a] << 16));
946 s->cirrus_blt_srcaddr =
947 (s->gr[0x2c] | (s->gr[0x2d] << 8) | (s->gr[0x2e] << 16));
948 s->cirrus_blt_mode = s->gr[0x30];
949 blt_rop = s->gr[0x32];
951 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
952 case CIRRUS_BLTMODE_PIXELWIDTH8:
953 s->cirrus_blt_pixelwidth = 1;
955 case CIRRUS_BLTMODE_PIXELWIDTH16:
956 s->cirrus_blt_pixelwidth = 2;
958 case CIRRUS_BLTMODE_PIXELWIDTH24:
959 s->cirrus_blt_pixelwidth = 3;
961 case CIRRUS_BLTMODE_PIXELWIDTH32:
962 s->cirrus_blt_pixelwidth = 4;
966 printf("cirrus: bitblt - pixel width is unknown\n");
970 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
973 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
974 CIRRUS_BLTMODE_MEMSYSDEST))
975 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
977 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
982 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
983 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
984 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
985 s->cirrus_rop = cirrus_get_bkwd_rop_handler(blt_rop);
987 s->cirrus_rop = cirrus_get_fwd_rop_handler(blt_rop);
990 // setup bitblt engine.
991 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
992 if (!cirrus_bitblt_cputovideo(s))
994 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
995 if (!cirrus_bitblt_videotocpu(s))
998 if (!cirrus_bitblt_videotovideo(s))
1004 cirrus_bitblt_reset(s);
1007 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1011 old_value = s->gr[0x31];
1012 s->gr[0x31] = reg_value;
1014 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1015 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1016 cirrus_bitblt_reset(s);
1017 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1018 ((reg_value & CIRRUS_BLT_START) != 0)) {
1019 s->gr[0x31] |= CIRRUS_BLT_BUSY;
1020 cirrus_bitblt_start(s);
1025 /***************************************
1029 ***************************************/
1031 static void cirrus_get_offsets(VGAState *s1,
1032 uint32_t *pline_offset,
1033 uint32_t *pstart_addr)
1035 CirrusVGAState * s = (CirrusVGAState *)s1;
1036 uint32_t start_addr;
1037 uint32_t line_offset;
1039 line_offset = s->cr[0x13]
1040 | ((s->cr[0x1b] & 0x10) << 4);
1042 *pline_offset = line_offset;
1044 start_addr = (s->cr[0x0c] << 8)
1046 | ((s->cr[0x1b] & 0x01) << 16)
1047 | ((s->cr[0x1b] & 0x0c) << 15)
1048 | ((s->cr[0x1d] & 0x80) << 12);
1049 *pstart_addr = start_addr;
1052 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1056 switch (s->cirrus_hidden_dac_data & 0xf) {
1059 break; /* Sierra HiColor */
1062 break; /* XGA HiColor */
1065 printf("cirrus: invalid DAC value %x in 16bpp\n",
1066 (s->cirrus_hidden_dac_data & 0xf));
1074 static int cirrus_get_bpp(VGAState *s1)
1076 CirrusVGAState * s = (CirrusVGAState *)s1;
1079 if ((s->sr[0x07] & 0x01) != 0) {
1081 switch (s->sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1082 case CIRRUS_SR7_BPP_8:
1085 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1086 ret = cirrus_get_bpp16_depth(s);
1088 case CIRRUS_SR7_BPP_24:
1091 case CIRRUS_SR7_BPP_16:
1092 ret = cirrus_get_bpp16_depth(s);
1094 case CIRRUS_SR7_BPP_32:
1099 printf("cirrus: unknown bpp - sr7=%x\n", s->sr[0x7]);
1112 /***************************************
1116 ***************************************/
1118 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1123 if ((s->gr[0x0b] & 0x01) != 0) /* dual bank */
1124 offset = s->gr[0x09 + bank_index];
1125 else /* single bank */
1126 offset = s->gr[0x09];
1128 if ((s->gr[0x0b] & 0x20) != 0)
1133 if (s->vram_size <= offset)
1136 limit = s->vram_size - offset;
1138 if (((s->gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1139 if (limit > 0x8000) {
1148 s->cirrus_bank_base[bank_index] = offset;
1149 s->cirrus_bank_limit[bank_index] = limit;
1151 s->cirrus_bank_base[bank_index] = 0;
1152 s->cirrus_bank_limit[bank_index] = 0;
1156 /***************************************
1158 * I/O access between 0x3c4-0x3c5
1160 ***************************************/
1163 cirrus_hook_read_sr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1165 switch (reg_index) {
1166 case 0x00: // Standard VGA
1167 case 0x01: // Standard VGA
1168 case 0x02: // Standard VGA
1169 case 0x03: // Standard VGA
1170 case 0x04: // Standard VGA
1171 return CIRRUS_HOOK_NOT_HANDLED;
1172 case 0x06: // Unlock Cirrus extensions
1173 *reg_value = s->sr[reg_index];
1176 case 0x07: // Extended Sequencer Mode
1177 case 0x08: // EEPROM Control
1178 case 0x09: // Scratch Register 0
1179 case 0x0a: // Scratch Register 1
1180 case 0x0b: // VCLK 0
1181 case 0x0c: // VCLK 1
1182 case 0x0d: // VCLK 2
1183 case 0x0e: // VCLK 3
1184 case 0x0f: // DRAM Control
1188 case 0x70: // Graphics Cursor X
1192 case 0xf0: // Graphics Cursor X
1196 case 0x71: // Graphics Cursor Y
1200 case 0xf1: // Graphics Cursor Y
1201 case 0x12: // Graphics Cursor Attribute
1202 case 0x13: // Graphics Cursor Pattern Address
1203 case 0x14: // Scratch Register 2
1204 case 0x15: // Scratch Register 3
1205 case 0x16: // Performance Tuning Register
1206 case 0x17: // Configuration Readback and Extended Control
1207 case 0x18: // Signature Generator Control
1208 case 0x19: // Signal Generator Result
1209 case 0x1a: // Signal Generator Result
1210 case 0x1b: // VCLK 0 Denominator & Post
1211 case 0x1c: // VCLK 1 Denominator & Post
1212 case 0x1d: // VCLK 2 Denominator & Post
1213 case 0x1e: // VCLK 3 Denominator & Post
1214 case 0x1f: // BIOS Write Enable and MCLK select
1216 printf("cirrus: handled inport sr_index %02x\n", reg_index);
1218 *reg_value = s->sr[reg_index];
1222 printf("cirrus: inport sr_index %02x\n", reg_index);
1228 return CIRRUS_HOOK_HANDLED;
1232 cirrus_hook_write_sr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1234 switch (reg_index) {
1235 case 0x00: // Standard VGA
1236 case 0x01: // Standard VGA
1237 case 0x02: // Standard VGA
1238 case 0x03: // Standard VGA
1239 case 0x04: // Standard VGA
1240 return CIRRUS_HOOK_NOT_HANDLED;
1241 case 0x06: // Unlock Cirrus extensions
1243 if (reg_value == 0x12) {
1244 s->sr[reg_index] = 0x12;
1246 s->sr[reg_index] = 0x0f;
1252 case 0x70: // Graphics Cursor X
1256 case 0xf0: // Graphics Cursor X
1257 s->sr[0x10] = reg_value;
1258 s->cirrus_hw_cursor_x = ((reg_index << 3) & 0x700) | reg_value;
1263 case 0x71: // Graphics Cursor Y
1267 case 0xf1: // Graphics Cursor Y
1268 s->sr[0x11] = reg_value;
1269 s->cirrus_hw_cursor_y = ((reg_index << 3) & 0x700) | reg_value;
1271 case 0x07: // Extended Sequencer Mode
1272 case 0x08: // EEPROM Control
1273 case 0x09: // Scratch Register 0
1274 case 0x0a: // Scratch Register 1
1275 case 0x0b: // VCLK 0
1276 case 0x0c: // VCLK 1
1277 case 0x0d: // VCLK 2
1278 case 0x0e: // VCLK 3
1279 case 0x0f: // DRAM Control
1280 case 0x12: // Graphics Cursor Attribute
1281 case 0x13: // Graphics Cursor Pattern Address
1282 case 0x14: // Scratch Register 2
1283 case 0x15: // Scratch Register 3
1284 case 0x16: // Performance Tuning Register
1285 case 0x17: // Configuration Readback and Extended Control
1286 case 0x18: // Signature Generator Control
1287 case 0x19: // Signature Generator Result
1288 case 0x1a: // Signature Generator Result
1289 case 0x1b: // VCLK 0 Denominator & Post
1290 case 0x1c: // VCLK 1 Denominator & Post
1291 case 0x1d: // VCLK 2 Denominator & Post
1292 case 0x1e: // VCLK 3 Denominator & Post
1293 case 0x1f: // BIOS Write Enable and MCLK select
1294 s->sr[reg_index] = reg_value;
1296 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1297 reg_index, reg_value);
1302 printf("cirrus: outport sr_index %02x, sr_value %02x\n", reg_index,
1308 return CIRRUS_HOOK_HANDLED;
1311 /***************************************
1313 * I/O access at 0x3c6
1315 ***************************************/
1317 static void cirrus_read_hidden_dac(CirrusVGAState * s, int *reg_value)
1320 if (s->cirrus_hidden_dac_lockindex < 5) {
1321 if (s->cirrus_hidden_dac_lockindex == 4) {
1322 *reg_value = s->cirrus_hidden_dac_data;
1324 s->cirrus_hidden_dac_lockindex++;
1328 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1330 if (s->cirrus_hidden_dac_lockindex == 4) {
1331 s->cirrus_hidden_dac_data = reg_value;
1333 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1336 s->cirrus_hidden_dac_lockindex = 0;
1339 /***************************************
1341 * I/O access at 0x3c9
1343 ***************************************/
1345 static int cirrus_hook_read_palette(CirrusVGAState * s, int *reg_value)
1347 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1348 return CIRRUS_HOOK_NOT_HANDLED;
1349 if (s->dac_read_index < 0x10) {
1351 s->cirrus_hidden_palette[s->dac_read_index * 3 +
1354 *reg_value = 0xff; /* XXX */
1356 if (++s->dac_sub_index == 3) {
1357 s->dac_sub_index = 0;
1358 s->dac_read_index++;
1360 return CIRRUS_HOOK_HANDLED;
1363 static int cirrus_hook_write_palette(CirrusVGAState * s, int reg_value)
1365 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1366 return CIRRUS_HOOK_NOT_HANDLED;
1367 s->dac_cache[s->dac_sub_index] = reg_value;
1368 if (++s->dac_sub_index == 3) {
1369 if (s->dac_read_index < 0x10) {
1370 memcpy(&s->cirrus_hidden_palette[s->dac_write_index * 3],
1372 /* XXX update cursor */
1374 s->dac_sub_index = 0;
1375 s->dac_write_index++;
1377 return CIRRUS_HOOK_HANDLED;
1380 /***************************************
1382 * I/O access between 0x3ce-0x3cf
1384 ***************************************/
1387 cirrus_hook_read_gr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1389 switch (reg_index) {
1390 case 0x02: // Standard VGA
1391 case 0x03: // Standard VGA
1392 case 0x04: // Standard VGA
1393 case 0x06: // Standard VGA
1394 case 0x07: // Standard VGA
1395 case 0x08: // Standard VGA
1396 return CIRRUS_HOOK_NOT_HANDLED;
1397 case 0x05: // Standard VGA, Cirrus extended mode
1402 if (reg_index < 0x3a) {
1403 *reg_value = s->gr[reg_index];
1406 printf("cirrus: inport gr_index %02x\n", reg_index);
1411 return CIRRUS_HOOK_HANDLED;
1415 cirrus_hook_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1417 switch (reg_index) {
1418 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1419 s->gr[0x00] = reg_value;
1420 return CIRRUS_HOOK_NOT_HANDLED;
1421 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1422 s->gr[0x01] = reg_value;
1423 return CIRRUS_HOOK_NOT_HANDLED;
1424 case 0x02: // Standard VGA
1425 case 0x03: // Standard VGA
1426 case 0x04: // Standard VGA
1427 case 0x06: // Standard VGA
1428 case 0x07: // Standard VGA
1429 case 0x08: // Standard VGA
1430 return CIRRUS_HOOK_NOT_HANDLED;
1431 case 0x05: // Standard VGA, Cirrus extended mode
1432 s->gr[reg_index] = reg_value & 0x7f;
1434 case 0x09: // bank offset #0
1435 case 0x0A: // bank offset #1
1437 s->gr[reg_index] = reg_value;
1438 cirrus_update_bank_ptr(s, 0);
1439 cirrus_update_bank_ptr(s, 1);
1441 case 0x10: // BGCOLOR 0x0000ff00
1442 case 0x11: // FGCOLOR 0x0000ff00
1443 case 0x12: // BGCOLOR 0x00ff0000
1444 case 0x13: // FGCOLOR 0x00ff0000
1445 case 0x14: // BGCOLOR 0xff000000
1446 case 0x15: // FGCOLOR 0xff000000
1447 case 0x20: // BLT WIDTH 0x0000ff
1448 case 0x22: // BLT HEIGHT 0x0000ff
1449 case 0x24: // BLT DEST PITCH 0x0000ff
1450 case 0x26: // BLT SRC PITCH 0x0000ff
1451 case 0x28: // BLT DEST ADDR 0x0000ff
1452 case 0x29: // BLT DEST ADDR 0x00ff00
1453 case 0x2c: // BLT SRC ADDR 0x0000ff
1454 case 0x2d: // BLT SRC ADDR 0x00ff00
1455 case 0x30: // BLT MODE
1456 case 0x32: // RASTER OP
1457 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1458 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1459 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1460 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1461 s->gr[reg_index] = reg_value;
1463 case 0x21: // BLT WIDTH 0x001f00
1464 case 0x23: // BLT HEIGHT 0x001f00
1465 case 0x25: // BLT DEST PITCH 0x001f00
1466 case 0x27: // BLT SRC PITCH 0x001f00
1467 s->gr[reg_index] = reg_value & 0x1f;
1469 case 0x2a: // BLT DEST ADDR 0x3f0000
1470 case 0x2e: // BLT SRC ADDR 0x3f0000
1471 s->gr[reg_index] = reg_value & 0x3f;
1473 case 0x31: // BLT STATUS/START
1474 cirrus_write_bitblt(s, reg_value);
1478 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1484 return CIRRUS_HOOK_HANDLED;
1487 /***************************************
1489 * I/O access between 0x3d4-0x3d5
1491 ***************************************/
1494 cirrus_hook_read_cr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1496 switch (reg_index) {
1497 case 0x00: // Standard VGA
1498 case 0x01: // Standard VGA
1499 case 0x02: // Standard VGA
1500 case 0x03: // Standard VGA
1501 case 0x04: // Standard VGA
1502 case 0x05: // Standard VGA
1503 case 0x06: // Standard VGA
1504 case 0x07: // Standard VGA
1505 case 0x08: // Standard VGA
1506 case 0x09: // Standard VGA
1507 case 0x0a: // Standard VGA
1508 case 0x0b: // Standard VGA
1509 case 0x0c: // Standard VGA
1510 case 0x0d: // Standard VGA
1511 case 0x0e: // Standard VGA
1512 case 0x0f: // Standard VGA
1513 case 0x10: // Standard VGA
1514 case 0x11: // Standard VGA
1515 case 0x12: // Standard VGA
1516 case 0x13: // Standard VGA
1517 case 0x14: // Standard VGA
1518 case 0x15: // Standard VGA
1519 case 0x16: // Standard VGA
1520 case 0x17: // Standard VGA
1521 case 0x18: // Standard VGA
1522 return CIRRUS_HOOK_NOT_HANDLED;
1523 case 0x19: // Interlace End
1524 case 0x1a: // Miscellaneous Control
1525 case 0x1b: // Extended Display Control
1526 case 0x1c: // Sync Adjust and Genlock
1527 case 0x1d: // Overlay Extended Control
1528 case 0x22: // Graphics Data Latches Readback (R)
1529 case 0x24: // Attribute Controller Toggle Readback (R)
1530 case 0x25: // Part Status
1531 case 0x27: // Part ID (R)
1532 *reg_value = s->cr[reg_index];
1534 case 0x26: // Attribute Controller Index Readback (R)
1535 *reg_value = s->ar_index & 0x3f;
1539 printf("cirrus: inport cr_index %02x\n", reg_index);
1545 return CIRRUS_HOOK_HANDLED;
1549 cirrus_hook_write_cr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1551 switch (reg_index) {
1552 case 0x00: // Standard VGA
1553 case 0x01: // Standard VGA
1554 case 0x02: // Standard VGA
1555 case 0x03: // Standard VGA
1556 case 0x04: // Standard VGA
1557 case 0x05: // Standard VGA
1558 case 0x06: // Standard VGA
1559 case 0x07: // Standard VGA
1560 case 0x08: // Standard VGA
1561 case 0x09: // Standard VGA
1562 case 0x0a: // Standard VGA
1563 case 0x0b: // Standard VGA
1564 case 0x0c: // Standard VGA
1565 case 0x0d: // Standard VGA
1566 case 0x0e: // Standard VGA
1567 case 0x0f: // Standard VGA
1568 case 0x10: // Standard VGA
1569 case 0x11: // Standard VGA
1570 case 0x12: // Standard VGA
1571 case 0x13: // Standard VGA
1572 case 0x14: // Standard VGA
1573 case 0x15: // Standard VGA
1574 case 0x16: // Standard VGA
1575 case 0x17: // Standard VGA
1576 case 0x18: // Standard VGA
1577 return CIRRUS_HOOK_NOT_HANDLED;
1578 case 0x19: // Interlace End
1579 case 0x1a: // Miscellaneous Control
1580 case 0x1b: // Extended Display Control
1581 case 0x1c: // Sync Adjust and Genlock
1582 s->cr[reg_index] = reg_value;
1584 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1585 reg_index, reg_value);
1588 case 0x22: // Graphics Data Latches Readback (R)
1589 case 0x24: // Attribute Controller Toggle Readback (R)
1590 case 0x26: // Attribute Controller Index Readback (R)
1591 case 0x27: // Part ID (R)
1593 case 0x1d: // Overlay Extended Control
1594 case 0x25: // Part Status
1597 printf("cirrus: outport cr_index %02x, cr_value %02x\n", reg_index,
1603 return CIRRUS_HOOK_HANDLED;
1606 /***************************************
1608 * memory-mapped I/O (bitblt)
1610 ***************************************/
1612 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1617 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1618 cirrus_hook_read_gr(s, 0x00, &value);
1620 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1621 cirrus_hook_read_gr(s, 0x10, &value);
1623 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1624 cirrus_hook_read_gr(s, 0x12, &value);
1626 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1627 cirrus_hook_read_gr(s, 0x14, &value);
1629 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1630 cirrus_hook_read_gr(s, 0x01, &value);
1632 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1633 cirrus_hook_read_gr(s, 0x11, &value);
1635 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1636 cirrus_hook_read_gr(s, 0x13, &value);
1638 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1639 cirrus_hook_read_gr(s, 0x15, &value);
1641 case (CIRRUS_MMIO_BLTWIDTH + 0):
1642 cirrus_hook_read_gr(s, 0x20, &value);
1644 case (CIRRUS_MMIO_BLTWIDTH + 1):
1645 cirrus_hook_read_gr(s, 0x21, &value);
1647 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1648 cirrus_hook_read_gr(s, 0x22, &value);
1650 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1651 cirrus_hook_read_gr(s, 0x23, &value);
1653 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1654 cirrus_hook_read_gr(s, 0x24, &value);
1656 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1657 cirrus_hook_read_gr(s, 0x25, &value);
1659 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1660 cirrus_hook_read_gr(s, 0x26, &value);
1662 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1663 cirrus_hook_read_gr(s, 0x27, &value);
1665 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1666 cirrus_hook_read_gr(s, 0x28, &value);
1668 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1669 cirrus_hook_read_gr(s, 0x29, &value);
1671 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1672 cirrus_hook_read_gr(s, 0x2a, &value);
1674 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1675 cirrus_hook_read_gr(s, 0x2c, &value);
1677 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1678 cirrus_hook_read_gr(s, 0x2d, &value);
1680 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1681 cirrus_hook_read_gr(s, 0x2e, &value);
1683 case CIRRUS_MMIO_BLTWRITEMASK:
1684 cirrus_hook_read_gr(s, 0x2f, &value);
1686 case CIRRUS_MMIO_BLTMODE:
1687 cirrus_hook_read_gr(s, 0x30, &value);
1689 case CIRRUS_MMIO_BLTROP:
1690 cirrus_hook_read_gr(s, 0x32, &value);
1692 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1693 cirrus_hook_read_gr(s, 0x34, &value);
1695 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1696 cirrus_hook_read_gr(s, 0x35, &value);
1698 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1699 cirrus_hook_read_gr(s, 0x38, &value);
1701 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1702 cirrus_hook_read_gr(s, 0x39, &value);
1704 case CIRRUS_MMIO_BLTSTATUS:
1705 cirrus_hook_read_gr(s, 0x31, &value);
1709 printf("cirrus: mmio read - address 0x%04x\n", address);
1714 return (uint8_t) value;
1717 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1721 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1722 cirrus_hook_write_gr(s, 0x00, value);
1724 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1725 cirrus_hook_write_gr(s, 0x10, value);
1727 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1728 cirrus_hook_write_gr(s, 0x12, value);
1730 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1731 cirrus_hook_write_gr(s, 0x14, value);
1733 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1734 cirrus_hook_write_gr(s, 0x01, value);
1736 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1737 cirrus_hook_write_gr(s, 0x11, value);
1739 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1740 cirrus_hook_write_gr(s, 0x13, value);
1742 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1743 cirrus_hook_write_gr(s, 0x15, value);
1745 case (CIRRUS_MMIO_BLTWIDTH + 0):
1746 cirrus_hook_write_gr(s, 0x20, value);
1748 case (CIRRUS_MMIO_BLTWIDTH + 1):
1749 cirrus_hook_write_gr(s, 0x21, value);
1751 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1752 cirrus_hook_write_gr(s, 0x22, value);
1754 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1755 cirrus_hook_write_gr(s, 0x23, value);
1757 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1758 cirrus_hook_write_gr(s, 0x24, value);
1760 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1761 cirrus_hook_write_gr(s, 0x25, value);
1763 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1764 cirrus_hook_write_gr(s, 0x26, value);
1766 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1767 cirrus_hook_write_gr(s, 0x27, value);
1769 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1770 cirrus_hook_write_gr(s, 0x28, value);
1772 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1773 cirrus_hook_write_gr(s, 0x29, value);
1775 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1776 cirrus_hook_write_gr(s, 0x2a, value);
1778 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1781 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1782 cirrus_hook_write_gr(s, 0x2c, value);
1784 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1785 cirrus_hook_write_gr(s, 0x2d, value);
1787 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1788 cirrus_hook_write_gr(s, 0x2e, value);
1790 case CIRRUS_MMIO_BLTWRITEMASK:
1791 cirrus_hook_write_gr(s, 0x2f, value);
1793 case CIRRUS_MMIO_BLTMODE:
1794 cirrus_hook_write_gr(s, 0x30, value);
1796 case CIRRUS_MMIO_BLTROP:
1797 cirrus_hook_write_gr(s, 0x32, value);
1799 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1800 cirrus_hook_write_gr(s, 0x34, value);
1802 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1803 cirrus_hook_write_gr(s, 0x35, value);
1805 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1806 cirrus_hook_write_gr(s, 0x38, value);
1808 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1809 cirrus_hook_write_gr(s, 0x39, value);
1811 case CIRRUS_MMIO_BLTSTATUS:
1812 cirrus_hook_write_gr(s, 0x31, value);
1816 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1823 /***************************************
1827 * assume TARGET_PAGE_SIZE >= 16
1829 ***************************************/
1831 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1837 unsigned val = mem_value;
1840 dst = s->vram_ptr + offset;
1841 for (x = 0; x < 8; x++) {
1843 *dst++ = s->gr[0x01];
1844 } else if (mode == 5) {
1845 *dst++ = s->gr[0x00];
1849 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1850 cpu_physical_memory_set_dirty(s->vram_offset + offset + 7);
1853 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1859 unsigned val = mem_value;
1862 dst = s->vram_ptr + offset;
1863 for (x = 0; x < 8; x++) {
1865 *dst++ = s->gr[0x01];
1866 *dst++ = s->gr[0x11];
1867 } else if (mode == 5) {
1868 *dst++ = s->gr[0x00];
1869 *dst++ = s->gr[0x10];
1873 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1874 cpu_physical_memory_set_dirty(s->vram_offset + offset + 15);
1877 /***************************************
1879 * memory access between 0xa0000-0xbffff
1881 ***************************************/
1883 static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
1885 CirrusVGAState *s = opaque;
1886 unsigned bank_index;
1887 unsigned bank_offset;
1890 if ((s->sr[0x07] & 0x01) == 0) {
1891 return vga_mem_readb(s, addr);
1894 if (addr < 0x10000) {
1895 /* XXX handle bitblt */
1897 bank_index = addr >> 15;
1898 bank_offset = addr & 0x7fff;
1899 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1900 bank_offset += s->cirrus_bank_base[bank_index];
1901 if ((s->gr[0x0B] & 0x14) == 0x14) {
1903 } else if (s->gr[0x0B] & 0x02) {
1906 bank_offset &= s->cirrus_addr_mask;
1907 val = *(s->vram_ptr + bank_offset);
1910 } else if (addr >= 0x18000 && addr < 0x18100) {
1911 /* memory-mapped I/O */
1913 if ((s->sr[0x17] & 0x44) == 0x04) {
1914 val = cirrus_mmio_blt_read(s, addr & 0xff);
1919 printf("cirrus: mem_readb %06x\n", addr);
1925 static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
1928 #ifdef TARGET_WORDS_BIGENDIAN
1929 v = cirrus_vga_mem_readb(opaque, addr) << 8;
1930 v |= cirrus_vga_mem_readb(opaque, addr + 1);
1932 v = cirrus_vga_mem_readb(opaque, addr);
1933 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1938 static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
1941 #ifdef TARGET_WORDS_BIGENDIAN
1942 v = cirrus_vga_mem_readb(opaque, addr) << 24;
1943 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
1944 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
1945 v |= cirrus_vga_mem_readb(opaque, addr + 3);
1947 v = cirrus_vga_mem_readb(opaque, addr);
1948 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1949 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
1950 v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
1955 static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr,
1958 CirrusVGAState *s = opaque;
1959 unsigned bank_index;
1960 unsigned bank_offset;
1963 if ((s->sr[0x07] & 0x01) == 0) {
1964 vga_mem_writeb(s, addr, mem_value);
1968 if (addr < 0x10000) {
1969 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
1971 *s->cirrus_srcptr++ = (uint8_t) mem_value;
1972 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
1973 cirrus_bitblt_cputovideo_next(s);
1977 bank_index = addr >> 15;
1978 bank_offset = addr & 0x7fff;
1979 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1980 bank_offset += s->cirrus_bank_base[bank_index];
1981 if ((s->gr[0x0B] & 0x14) == 0x14) {
1983 } else if (s->gr[0x0B] & 0x02) {
1986 bank_offset &= s->cirrus_addr_mask;
1987 mode = s->gr[0x05] & 0x7;
1988 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
1989 *(s->vram_ptr + bank_offset) = mem_value;
1990 cpu_physical_memory_set_dirty(s->vram_offset +
1993 if ((s->gr[0x0B] & 0x14) != 0x14) {
1994 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
1998 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2005 } else if (addr >= 0x18000 && addr < 0x18100) {
2006 /* memory-mapped I/O */
2007 if ((s->sr[0x17] & 0x44) == 0x04) {
2008 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2012 printf("cirrus: mem_writeb %06x value %02x\n", addr, mem_value);
2017 static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2019 #ifdef TARGET_WORDS_BIGENDIAN
2020 cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
2021 cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
2023 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2024 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2028 static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2030 #ifdef TARGET_WORDS_BIGENDIAN
2031 cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
2032 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2033 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2034 cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
2036 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2037 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2038 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2039 cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2043 static CPUReadMemoryFunc *cirrus_vga_mem_read[3] = {
2044 cirrus_vga_mem_readb,
2045 cirrus_vga_mem_readw,
2046 cirrus_vga_mem_readl,
2049 static CPUWriteMemoryFunc *cirrus_vga_mem_write[3] = {
2050 cirrus_vga_mem_writeb,
2051 cirrus_vga_mem_writew,
2052 cirrus_vga_mem_writel,
2055 /***************************************
2059 ***************************************/
2061 static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2063 CirrusVGAState *s = (CirrusVGAState *) opaque;
2066 /* XXX: s->vram_size must be a power of two */
2067 addr &= s->cirrus_addr_mask;
2069 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2070 /* memory-mapped I/O */
2071 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2073 /* XXX handle bitblt */
2077 if ((s->gr[0x0B] & 0x14) == 0x14) {
2079 } else if (s->gr[0x0B] & 0x02) {
2082 addr &= s->cirrus_addr_mask;
2083 ret = *(s->vram_ptr + addr);
2089 static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2092 #ifdef TARGET_WORDS_BIGENDIAN
2093 v = cirrus_linear_readb(opaque, addr) << 8;
2094 v |= cirrus_linear_readb(opaque, addr + 1);
2096 v = cirrus_linear_readb(opaque, addr);
2097 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2102 static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2105 #ifdef TARGET_WORDS_BIGENDIAN
2106 v = cirrus_linear_readb(opaque, addr) << 24;
2107 v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2108 v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2109 v |= cirrus_linear_readb(opaque, addr + 3);
2111 v = cirrus_linear_readb(opaque, addr);
2112 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2113 v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2114 v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2119 static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2122 CirrusVGAState *s = (CirrusVGAState *) opaque;
2125 addr &= s->cirrus_addr_mask;
2127 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2128 /* memory-mapped I/O */
2129 cirrus_mmio_blt_write(s, addr & 0xff, val);
2130 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2132 *s->cirrus_srcptr++ = (uint8_t) val;
2133 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
2134 cirrus_bitblt_cputovideo_next(s);
2138 if ((s->gr[0x0B] & 0x14) == 0x14) {
2140 } else if (s->gr[0x0B] & 0x02) {
2143 addr &= s->cirrus_addr_mask;
2145 mode = s->gr[0x05] & 0x7;
2146 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2147 *(s->vram_ptr + addr) = (uint8_t) val;
2148 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2150 if ((s->gr[0x0B] & 0x14) != 0x14) {
2151 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2153 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2159 static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2162 #ifdef TARGET_WORDS_BIGENDIAN
2163 cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2164 cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2166 cirrus_linear_writeb(opaque, addr, val & 0xff);
2167 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2171 static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2174 #ifdef TARGET_WORDS_BIGENDIAN
2175 cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2176 cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2177 cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2178 cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2180 cirrus_linear_writeb(opaque, addr, val & 0xff);
2181 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2182 cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2183 cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2188 static CPUReadMemoryFunc *cirrus_linear_read[3] = {
2189 cirrus_linear_readb,
2190 cirrus_linear_readw,
2191 cirrus_linear_readl,
2194 static CPUWriteMemoryFunc *cirrus_linear_write[3] = {
2195 cirrus_linear_writeb,
2196 cirrus_linear_writew,
2197 cirrus_linear_writel,
2202 static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
2204 CirrusVGAState *s = opaque;
2207 /* check port range access depending on color/monochrome mode */
2208 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2209 || (addr >= 0x3d0 && addr <= 0x3df
2210 && !(s->msr & MSR_COLOR_EMULATION))) {
2215 if (s->ar_flip_flop == 0) {
2222 index = s->ar_index & 0x1f;
2235 if (cirrus_hook_read_sr(s, s->sr_index, &val))
2237 val = s->sr[s->sr_index];
2238 #ifdef DEBUG_VGA_REG
2239 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2243 cirrus_read_hidden_dac(s, &val);
2249 if (cirrus_hook_read_palette(s, &val))
2251 val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
2252 if (++s->dac_sub_index == 3) {
2253 s->dac_sub_index = 0;
2254 s->dac_read_index++;
2267 if (cirrus_hook_read_gr(s, s->gr_index, &val))
2269 val = s->gr[s->gr_index];
2270 #ifdef DEBUG_VGA_REG
2271 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2280 if (cirrus_hook_read_cr(s, s->cr_index, &val))
2282 val = s->cr[s->cr_index];
2283 #ifdef DEBUG_VGA_REG
2284 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2287 if (s->cr_index >= 0x20)
2288 printf("S3: CR read index=0x%x val=0x%x\n",
2294 /* just toggle to fool polling */
2295 s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
2297 s->ar_flip_flop = 0;
2304 #if defined(DEBUG_VGA)
2305 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2310 static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2312 CirrusVGAState *s = opaque;
2315 /* check port range access depending on color/monochrome mode */
2316 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2317 || (addr >= 0x3d0 && addr <= 0x3df
2318 && !(s->msr & MSR_COLOR_EMULATION)))
2322 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2327 if (s->ar_flip_flop == 0) {
2331 index = s->ar_index & 0x1f;
2334 s->ar[index] = val & 0x3f;
2337 s->ar[index] = val & ~0x10;
2343 s->ar[index] = val & ~0xc0;
2346 s->ar[index] = val & ~0xf0;
2349 s->ar[index] = val & ~0xf0;
2355 s->ar_flip_flop ^= 1;
2358 s->msr = val & ~0x10;
2364 if (cirrus_hook_write_sr(s, s->sr_index, val))
2366 #ifdef DEBUG_VGA_REG
2367 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2369 s->sr[s->sr_index] = val & sr_mask[s->sr_index];
2372 cirrus_write_hidden_dac(s, val);
2375 s->dac_read_index = val;
2376 s->dac_sub_index = 0;
2380 s->dac_write_index = val;
2381 s->dac_sub_index = 0;
2385 if (cirrus_hook_write_palette(s, val))
2387 s->dac_cache[s->dac_sub_index] = val;
2388 if (++s->dac_sub_index == 3) {
2389 memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
2390 s->dac_sub_index = 0;
2391 s->dac_write_index++;
2398 if (cirrus_hook_write_gr(s, s->gr_index, val))
2400 #ifdef DEBUG_VGA_REG
2401 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2403 s->gr[s->gr_index] = val & gr_mask[s->gr_index];
2411 if (cirrus_hook_write_cr(s, s->cr_index, val))
2413 #ifdef DEBUG_VGA_REG
2414 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2416 /* handle CR0-7 protection */
2417 if ((s->cr[11] & 0x80) && s->cr_index <= 7) {
2418 /* can always write bit 4 of CR7 */
2419 if (s->cr_index == 7)
2420 s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
2423 switch (s->cr_index) {
2424 case 0x01: /* horizontal display end */
2429 case 0x12: /* veritcal display end */
2430 s->cr[s->cr_index] = val;
2434 s->cr[s->cr_index] = val;
2440 s->fcr = val & 0x10;
2445 /***************************************
2447 * memory-mapped I/O access
2449 ***************************************/
2451 static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2453 CirrusVGAState *s = (CirrusVGAState *) opaque;
2455 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2457 if (addr >= 0x100) {
2458 return cirrus_mmio_blt_read(s, addr - 0x100);
2460 return vga_ioport_read(s, addr + 0x3c0);
2464 static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2467 #ifdef TARGET_WORDS_BIGENDIAN
2468 v = cirrus_mmio_readb(opaque, addr) << 8;
2469 v |= cirrus_mmio_readb(opaque, addr + 1);
2471 v = cirrus_mmio_readb(opaque, addr);
2472 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2477 static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2480 #ifdef TARGET_WORDS_BIGENDIAN
2481 v = cirrus_mmio_readb(opaque, addr) << 24;
2482 v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2483 v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2484 v |= cirrus_mmio_readb(opaque, addr + 3);
2486 v = cirrus_mmio_readb(opaque, addr);
2487 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2488 v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2489 v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2494 static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2497 CirrusVGAState *s = (CirrusVGAState *) opaque;
2499 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2501 if (addr >= 0x100) {
2502 cirrus_mmio_blt_write(s, addr - 0x100, val);
2504 vga_ioport_write(s, addr + 0x3c0, val);
2508 static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2511 #ifdef TARGET_WORDS_BIGENDIAN
2512 cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2513 cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2515 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2516 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2520 static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2523 #ifdef TARGET_WORDS_BIGENDIAN
2524 cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2525 cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2526 cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2527 cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2529 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2530 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2531 cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2532 cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2537 static CPUReadMemoryFunc *cirrus_mmio_read[3] = {
2543 static CPUWriteMemoryFunc *cirrus_mmio_write[3] = {
2549 /***************************************
2553 ***************************************/
2555 static void cirrus_init_common(CirrusVGAState * s)
2559 register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
2561 register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
2562 register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
2563 register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
2564 register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
2566 register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
2568 register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
2569 register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
2570 register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
2571 register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
2573 vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read,
2574 cirrus_vga_mem_write, s);
2575 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
2579 s->sr[0x0F] = CIRRUS_MEMSIZE_2M;
2580 s->sr[0x1F] = 0x22; // MemClock
2582 s->cr[0x27] = CIRRUS_ID_CLGD5430;
2584 s->cirrus_hidden_dac_lockindex = 5;
2585 s->cirrus_hidden_dac_data = 0;
2587 /* I/O handler for LFB */
2588 s->cirrus_linear_io_addr =
2589 cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,
2591 /* I/O handler for memory-mapped I/O */
2592 s->cirrus_mmio_io_addr =
2593 cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);
2595 /* XXX: s->vram_size must be a power of two */
2596 s->cirrus_addr_mask = s->vram_size - 1;
2598 s->get_bpp = cirrus_get_bpp;
2599 s->get_offsets = cirrus_get_offsets;
2602 /***************************************
2606 ***************************************/
2608 void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2609 unsigned long vga_ram_offset, int vga_ram_size)
2613 s = qemu_mallocz(sizeof(CirrusVGAState));
2615 vga_common_init((VGAState *)s,
2616 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2617 cirrus_init_common(s);
2618 s->sr[0x17] = CIRRUS_BUSTYPE_ISA;
2619 /* XXX ISA-LFB support */
2622 /***************************************
2626 ***************************************/
2628 static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
2629 uint32_t addr, uint32_t size, int type)
2631 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2633 cpu_register_physical_memory(addr, s->vram_size,
2634 s->cirrus_linear_io_addr);
2637 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
2638 uint32_t addr, uint32_t size, int type)
2640 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2642 cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
2643 s->cirrus_mmio_io_addr);
2646 void pci_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2647 unsigned long vga_ram_offset, int vga_ram_size)
2649 PCICirrusVGAState *d;
2653 /* setup PCI configuration registers */
2654 d = (PCICirrusVGAState *)pci_register_device("Cirrus VGA",
2655 sizeof(PCICirrusVGAState),
2657 pci_conf = d->dev.config;
2658 pci_conf[0x00] = (uint8_t) (PCI_VENDOR_CIRRUS & 0xff);
2659 pci_conf[0x01] = (uint8_t) (PCI_VENDOR_CIRRUS >> 8);
2660 pci_conf[0x02] = (uint8_t) (PCI_DEVICE_CLGD5430 & 0xff);
2661 pci_conf[0x03] = (uint8_t) (PCI_DEVICE_CLGD5430 >> 8);
2662 pci_conf[0x04] = PCI_COMMAND_IOACCESS | PCI_COMMAND_MEMACCESS;
2663 pci_conf[0x0a] = PCI_CLASS_SUB_VGA;
2664 pci_conf[0x0b] = PCI_CLASS_BASE_DISPLAY;
2665 pci_conf[0x0e] = PCI_CLASS_HEADERTYPE_00h;
2669 vga_common_init((VGAState *)s,
2670 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2671 cirrus_init_common(s);
2672 s->sr[0x17] = CIRRUS_BUSTYPE_PCI;
2674 /* setup memory space */
2676 /* memory #1 memory-mapped I/O */
2677 /* XXX: s->vram_size must be a power of two */
2678 pci_register_io_region((PCIDevice *)d, 0, s->vram_size,
2679 PCI_ADDRESS_SPACE_MEM, cirrus_pci_lfb_map);
2680 pci_register_io_region((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
2681 PCI_ADDRESS_SPACE_MEM, cirrus_pci_mmio_map);