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 {
208 /* XXX: we use the anonymous struct/union gcc 3.x extension */
209 __extension__ struct VGAState;
211 int cirrus_linear_io_addr;
212 int cirrus_mmio_io_addr;
213 uint32_t cirrus_addr_mask;
214 uint8_t cirrus_shadow_gr0;
215 uint8_t cirrus_shadow_gr1;
216 uint8_t cirrus_hidden_dac_lockindex;
217 uint8_t cirrus_hidden_dac_data;
218 uint32_t cirrus_bank_base[2];
219 uint32_t cirrus_bank_limit[2];
220 uint8_t cirrus_hidden_palette[48];
221 uint32_t cirrus_hw_cursor_x;
222 uint32_t cirrus_hw_cursor_y;
223 int cirrus_blt_pixelwidth;
224 int cirrus_blt_width;
225 int cirrus_blt_height;
226 int cirrus_blt_dstpitch;
227 int cirrus_blt_srcpitch;
228 uint32_t cirrus_blt_dstaddr;
229 uint32_t cirrus_blt_srcaddr;
230 uint8_t cirrus_blt_mode;
231 cirrus_bitblt_rop_t cirrus_rop;
232 #define CIRRUS_BLTBUFSIZE 256
233 uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
234 uint8_t *cirrus_srcptr;
235 uint8_t *cirrus_srcptr_end;
236 uint32_t cirrus_srccounter;
237 uint8_t *cirrus_dstptr;
238 uint8_t *cirrus_dstptr_end;
239 uint32_t cirrus_dstcounter;
240 cirrus_bitblt_handler_t cirrus_blt_handler;
241 int cirrus_blt_horz_counter;
244 typedef struct PCICirrusVGAState {
246 CirrusVGAState cirrus_vga;
249 /***************************************
253 ***************************************/
256 static void cirrus_bitblt_reset(CirrusVGAState * s);
258 /***************************************
262 ***************************************/
264 #define IMPLEMENT_FORWARD_BITBLT(name,opline) \
266 cirrus_bitblt_rop_fwd_##name( \
267 uint8_t *dst,const uint8_t *src, \
268 int dstpitch,int srcpitch, \
269 int bltwidth,int bltheight) \
272 dstpitch -= bltwidth; \
273 srcpitch -= bltwidth; \
274 for (y = 0; y < bltheight; y++) { \
275 for (x = 0; x < bltwidth; x++) { \
285 #define IMPLEMENT_BACKWARD_BITBLT(name,opline) \
287 cirrus_bitblt_rop_bkwd_##name( \
288 uint8_t *dst,const uint8_t *src, \
289 int dstpitch,int srcpitch, \
290 int bltwidth,int bltheight) \
293 dstpitch += bltwidth; \
294 srcpitch += bltwidth; \
295 for (y = 0; y < bltheight; y++) { \
296 for (x = 0; x < bltwidth; x++) { \
306 IMPLEMENT_FORWARD_BITBLT(0, *dst = 0)
307 IMPLEMENT_FORWARD_BITBLT(src_and_dst, *dst = (*src) & (*dst))
308 IMPLEMENT_FORWARD_BITBLT(nop, (void) 0)
309 IMPLEMENT_FORWARD_BITBLT(src_and_notdst, *dst = (*src) & (~(*dst)))
310 IMPLEMENT_FORWARD_BITBLT(notdst, *dst = ~(*dst))
311 IMPLEMENT_FORWARD_BITBLT(src, *dst = *src)
312 IMPLEMENT_FORWARD_BITBLT(1, *dst = 0xff)
313 IMPLEMENT_FORWARD_BITBLT(notsrc_and_dst, *dst = (~(*src)) & (*dst))
314 IMPLEMENT_FORWARD_BITBLT(src_xor_dst, *dst = (*src) ^ (*dst))
315 IMPLEMENT_FORWARD_BITBLT(src_or_dst, *dst = (*src) | (*dst))
316 IMPLEMENT_FORWARD_BITBLT(notsrc_or_notdst, *dst = (~(*src)) | (~(*dst)))
317 IMPLEMENT_FORWARD_BITBLT(src_notxor_dst, *dst = ~((*src) ^ (*dst)))
318 IMPLEMENT_FORWARD_BITBLT(src_or_notdst, *dst = (*src) | (~(*dst)))
319 IMPLEMENT_FORWARD_BITBLT(notsrc, *dst = (~(*src)))
320 IMPLEMENT_FORWARD_BITBLT(notsrc_or_dst, *dst = (~(*src)) | (*dst))
321 IMPLEMENT_FORWARD_BITBLT(notsrc_and_notdst, *dst = (~(*src)) & (~(*dst)))
323 IMPLEMENT_BACKWARD_BITBLT(0, *dst = 0)
324 IMPLEMENT_BACKWARD_BITBLT(src_and_dst, *dst = (*src) & (*dst))
325 IMPLEMENT_BACKWARD_BITBLT(nop, (void) 0)
326 IMPLEMENT_BACKWARD_BITBLT(src_and_notdst, *dst = (*src) & (~(*dst)))
327 IMPLEMENT_BACKWARD_BITBLT(notdst, *dst = ~(*dst))
328 IMPLEMENT_BACKWARD_BITBLT(src, *dst = *src)
329 IMPLEMENT_BACKWARD_BITBLT(1, *dst = 0xff)
330 IMPLEMENT_BACKWARD_BITBLT(notsrc_and_dst, *dst = (~(*src)) & (*dst))
331 IMPLEMENT_BACKWARD_BITBLT(src_xor_dst, *dst = (*src) ^ (*dst))
332 IMPLEMENT_BACKWARD_BITBLT(src_or_dst, *dst = (*src) | (*dst))
333 IMPLEMENT_BACKWARD_BITBLT(notsrc_or_notdst, *dst = (~(*src)) | (~(*dst)))
334 IMPLEMENT_BACKWARD_BITBLT(src_notxor_dst, *dst = ~((*src) ^ (*dst)))
335 IMPLEMENT_BACKWARD_BITBLT(src_or_notdst, *dst = (*src) | (~(*dst)))
336 IMPLEMENT_BACKWARD_BITBLT(notsrc, *dst = (~(*src)))
337 IMPLEMENT_BACKWARD_BITBLT(notsrc_or_dst, *dst = (~(*src)) | (*dst))
338 IMPLEMENT_BACKWARD_BITBLT(notsrc_and_notdst, *dst = (~(*src)) & (~(*dst)))
340 static cirrus_bitblt_rop_t cirrus_get_fwd_rop_handler(uint8_t rop)
342 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_fwd_nop;
346 rop_handler = cirrus_bitblt_rop_fwd_0;
348 case CIRRUS_ROP_SRC_AND_DST:
349 rop_handler = cirrus_bitblt_rop_fwd_src_and_dst;
352 rop_handler = cirrus_bitblt_rop_fwd_nop;
354 case CIRRUS_ROP_SRC_AND_NOTDST:
355 rop_handler = cirrus_bitblt_rop_fwd_src_and_notdst;
357 case CIRRUS_ROP_NOTDST:
358 rop_handler = cirrus_bitblt_rop_fwd_notdst;
361 rop_handler = cirrus_bitblt_rop_fwd_src;
364 rop_handler = cirrus_bitblt_rop_fwd_1;
366 case CIRRUS_ROP_NOTSRC_AND_DST:
367 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_dst;
369 case CIRRUS_ROP_SRC_XOR_DST:
370 rop_handler = cirrus_bitblt_rop_fwd_src_xor_dst;
372 case CIRRUS_ROP_SRC_OR_DST:
373 rop_handler = cirrus_bitblt_rop_fwd_src_or_dst;
375 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
376 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_notdst;
378 case CIRRUS_ROP_SRC_NOTXOR_DST:
379 rop_handler = cirrus_bitblt_rop_fwd_src_notxor_dst;
381 case CIRRUS_ROP_SRC_OR_NOTDST:
382 rop_handler = cirrus_bitblt_rop_fwd_src_or_notdst;
384 case CIRRUS_ROP_NOTSRC:
385 rop_handler = cirrus_bitblt_rop_fwd_notsrc;
387 case CIRRUS_ROP_NOTSRC_OR_DST:
388 rop_handler = cirrus_bitblt_rop_fwd_notsrc_or_dst;
390 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
391 rop_handler = cirrus_bitblt_rop_fwd_notsrc_and_notdst;
395 printf("unknown ROP %02x\n", rop);
403 static cirrus_bitblt_rop_t cirrus_get_bkwd_rop_handler(uint8_t rop)
405 cirrus_bitblt_rop_t rop_handler = cirrus_bitblt_rop_bkwd_nop;
409 rop_handler = cirrus_bitblt_rop_bkwd_0;
411 case CIRRUS_ROP_SRC_AND_DST:
412 rop_handler = cirrus_bitblt_rop_bkwd_src_and_dst;
415 rop_handler = cirrus_bitblt_rop_bkwd_nop;
417 case CIRRUS_ROP_SRC_AND_NOTDST:
418 rop_handler = cirrus_bitblt_rop_bkwd_src_and_notdst;
420 case CIRRUS_ROP_NOTDST:
421 rop_handler = cirrus_bitblt_rop_bkwd_notdst;
424 rop_handler = cirrus_bitblt_rop_bkwd_src;
427 rop_handler = cirrus_bitblt_rop_bkwd_1;
429 case CIRRUS_ROP_NOTSRC_AND_DST:
430 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_dst;
432 case CIRRUS_ROP_SRC_XOR_DST:
433 rop_handler = cirrus_bitblt_rop_bkwd_src_xor_dst;
435 case CIRRUS_ROP_SRC_OR_DST:
436 rop_handler = cirrus_bitblt_rop_bkwd_src_or_dst;
438 case CIRRUS_ROP_NOTSRC_OR_NOTDST:
439 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_notdst;
441 case CIRRUS_ROP_SRC_NOTXOR_DST:
442 rop_handler = cirrus_bitblt_rop_bkwd_src_notxor_dst;
444 case CIRRUS_ROP_SRC_OR_NOTDST:
445 rop_handler = cirrus_bitblt_rop_bkwd_src_or_notdst;
447 case CIRRUS_ROP_NOTSRC:
448 rop_handler = cirrus_bitblt_rop_bkwd_notsrc;
450 case CIRRUS_ROP_NOTSRC_OR_DST:
451 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_or_dst;
453 case CIRRUS_ROP_NOTSRC_AND_NOTDST:
454 rop_handler = cirrus_bitblt_rop_bkwd_notsrc_and_notdst;
458 printf("unknown ROP %02x\n", rop);
466 /***************************************
470 ***************************************/
473 cirrus_colorexpand_8(CirrusVGAState * s, uint8_t * dst,
474 const uint8_t * src, int count)
482 colors[0] = s->gr[0x00];
483 colors[1] = s->gr[0x01];
485 bitmask = 0x80 >> srcskipleft;
487 for (x = 0; x < count; x++) {
488 if ((bitmask & 0xff) == 0) {
492 *dst++ = colors[!!(bits & bitmask)];
498 cirrus_colorexpand_16(CirrusVGAState * s, uint8_t * dst,
499 const uint8_t * src, int count)
502 uint8_t colors[2][2];
508 colors[0][0] = s->gr[0x00];
509 colors[0][1] = s->gr[0x10];
510 colors[1][0] = s->gr[0x01];
511 colors[1][1] = s->gr[0x11];
513 bitmask = 0x80 >> srcskipleft;
515 for (x = 0; x < count; x++) {
516 if ((bitmask & 0xff) == 0) {
520 index = !!(bits & bitmask);
521 *dst++ = colors[index][0];
522 *dst++ = colors[index][1];
528 cirrus_colorexpand_24(CirrusVGAState * s, uint8_t * dst,
529 const uint8_t * src, int count)
532 uint8_t colors[2][3];
538 colors[0][0] = s->gr[0x00];
539 colors[0][1] = s->gr[0x10];
540 colors[0][2] = s->gr[0x12];
541 colors[1][0] = s->gr[0x01];
542 colors[1][1] = s->gr[0x11];
543 colors[1][2] = s->gr[0x13];
545 bitmask = 0x80 << srcskipleft;
547 for (x = 0; x < count; x++) {
548 if ((bitmask & 0xff) == 0) {
552 index = !!(bits & bitmask);
553 *dst++ = colors[index][0];
554 *dst++ = colors[index][1];
555 *dst++ = colors[index][2];
561 cirrus_colorexpand_32(CirrusVGAState * s, uint8_t * dst,
562 const uint8_t * src, int count)
565 uint8_t colors[2][4];
571 colors[0][0] = s->gr[0x00];
572 colors[0][1] = s->gr[0x10];
573 colors[0][2] = s->gr[0x12];
574 colors[0][3] = s->gr[0x14];
575 colors[1][0] = s->gr[0x01];
576 colors[1][1] = s->gr[0x11];
577 colors[1][2] = s->gr[0x13];
578 colors[1][3] = s->gr[0x15];
580 bitmask = 0x80 << srcskipleft;
582 for (x = 0; x < count; x++) {
583 if ((bitmask & 0xff) == 0) {
587 index = !!(bits & bitmask);
588 *dst++ = colors[index][0];
589 *dst++ = colors[index][1];
590 *dst++ = colors[index][2];
591 *dst++ = colors[index][3];
597 cirrus_colorexpand(CirrusVGAState * s, uint8_t * dst, const uint8_t * src,
600 switch (s->cirrus_blt_pixelwidth) {
602 cirrus_colorexpand_8(s, dst, src, count);
605 cirrus_colorexpand_16(s, dst, src, count);
608 cirrus_colorexpand_24(s, dst, src, count);
611 cirrus_colorexpand_32(s, dst, src, count);
615 printf("cirrus: COLOREXPAND pixelwidth %d - unimplemented\n",
616 s->cirrus_blt_pixelwidth);
622 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
623 int off_pitch, int bytesperline,
630 for (y = 0; y < lines; y++) {
632 off_cur_end = off_cur + bytesperline;
633 off_cur &= TARGET_PAGE_MASK;
634 while (off_cur < off_cur_end) {
635 cpu_physical_memory_set_dirty(s->vram_offset + off_cur);
636 off_cur += TARGET_PAGE_SIZE;
638 off_begin += off_pitch;
644 static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
647 uint8_t work_colorexp[256];
651 int tilewidth, tileheight;
652 int patternbytes = s->cirrus_blt_pixelwidth * 8;
654 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
655 cirrus_colorexpand(s, work_colorexp, src, 8 * 8);
657 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_COLOREXPAND;
659 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_PATTERNCOPY) {
661 printf("cirrus: blt mode %02x (pattercopy) - unimplemented\n",
667 dst = s->vram_ptr + s->cirrus_blt_dstaddr;
668 for (y = 0; y < s->cirrus_blt_height; y += 8) {
670 tileheight = qemu_MIN(8, s->cirrus_blt_height - y);
671 for (x = 0; x < s->cirrus_blt_width; x += patternbytes) {
672 tilewidth = qemu_MIN(patternbytes, s->cirrus_blt_width - x);
673 (*s->cirrus_rop) (dstc, src,
674 s->cirrus_blt_dstpitch, patternbytes,
675 tilewidth, tileheight);
676 dstc += patternbytes;
678 dst += s->cirrus_blt_dstpitch * 8;
680 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
681 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
682 s->cirrus_blt_height);
686 /***************************************
688 * bitblt (video-to-video)
690 ***************************************/
692 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
694 return cirrus_bitblt_common_patterncopy(s,
696 s->cirrus_blt_srcaddr);
699 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
701 if ((s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) != 0) {
703 printf("cirrus: CIRRUS_BLTMODE_COLOREXPAND - unimplemented\n");
707 if ((s->cirrus_blt_mode & (~CIRRUS_BLTMODE_BACKWARDS)) != 0) {
709 printf("cirrus: blt mode %02x - unimplemented\n",
715 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
716 s->vram_ptr + s->cirrus_blt_srcaddr,
717 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
718 s->cirrus_blt_width, s->cirrus_blt_height);
719 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
720 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
721 s->cirrus_blt_height);
725 /***************************************
727 * bitblt (cpu-to-video)
729 ***************************************/
731 static void cirrus_bitblt_cputovideo_patterncopy(void *opaque)
733 CirrusVGAState *s = (CirrusVGAState *) opaque;
736 data_count = s->cirrus_srcptr - &s->cirrus_bltbuf[0];
738 if (data_count > 0) {
739 if (data_count != s->cirrus_srccounter) {
741 printf("cirrus: internal error\n");
744 cirrus_bitblt_common_patterncopy(s, &s->cirrus_bltbuf[0]);
746 cirrus_bitblt_reset(s);
750 static void cirrus_bitblt_cputovideo_copy(void *opaque)
752 CirrusVGAState *s = (CirrusVGAState *) opaque;
755 uint8_t work_colorexp[256];
756 uint8_t *src_ptr = NULL;
761 if (s->cirrus_blt_height <= 0) {
762 s->cirrus_srcptr = s->cirrus_srcptr_end;
766 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
768 /* get BLT source. */
769 if (src_avail <= 0) {
770 data_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
774 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
775 if (s->cirrus_blt_mode & ~CIRRUS_BLTMODE_COLOREXPAND) {
777 printf("cirrus: unsupported\n");
779 cirrus_bitblt_reset(s);
782 data_avail = qemu_MIN(data_count, 256 / 32);
783 cirrus_colorexpand(s, work_colorexp, s->cirrus_srcptr,
785 src_ptr = &work_colorexp[0];
786 src_avail = data_avail * 8 * s->cirrus_blt_pixelwidth;
787 s->cirrus_srcptr += data_avail;
789 ((s->cirrus_blt_width + 7) / 8) * 8 -
791 src_linepad *= s->cirrus_blt_pixelwidth;
793 if (s->cirrus_blt_mode != 0) {
795 printf("cirrus: unsupported\n");
797 cirrus_bitblt_reset(s);
800 src_ptr = s->cirrus_srcptr;
802 data_count / s->cirrus_blt_pixelwidth *
803 s->cirrus_blt_pixelwidth;
804 s->cirrus_srcptr += src_avail;
812 s->cirrus_blt_srcpitch - s->cirrus_blt_horz_counter;
813 src_processing = qemu_MIN(src_avail, src_processing);
814 (*s->cirrus_rop) (s->vram_ptr + s->cirrus_blt_dstaddr,
815 src_ptr, 0, 0, src_processing, 1);
816 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
819 s->cirrus_blt_dstaddr += src_processing;
820 src_ptr += src_processing;
821 src_avail -= src_processing;
822 s->cirrus_blt_horz_counter += src_processing;
823 if (s->cirrus_blt_horz_counter >= s->cirrus_blt_srcpitch) {
824 src_ptr += src_linepad;
825 src_avail -= src_linepad;
826 s->cirrus_blt_dstaddr +=
827 s->cirrus_blt_dstpitch - s->cirrus_blt_srcpitch;
828 s->cirrus_blt_horz_counter = 0;
829 s->cirrus_blt_height--;
830 if (s->cirrus_blt_height <= 0) {
831 s->cirrus_srcptr = s->cirrus_srcptr_end;
838 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
843 s->cirrus_blt_handler(s);
845 if (s->cirrus_srccounter > 0) {
846 s->cirrus_srccounter -= s->cirrus_srcptr - &s->cirrus_bltbuf[0];
847 copy_count = s->cirrus_srcptr_end - s->cirrus_srcptr;
848 memmove(&s->cirrus_bltbuf[0], s->cirrus_srcptr, copy_count);
849 avail_count = qemu_MIN(CIRRUS_BLTBUFSIZE, s->cirrus_srccounter);
850 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
851 s->cirrus_srcptr_end = s->cirrus_srcptr + avail_count;
852 if (s->cirrus_srccounter <= 0) {
853 cirrus_bitblt_reset(s);
858 /***************************************
862 ***************************************/
864 static void cirrus_bitblt_reset(CirrusVGAState * s)
867 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
868 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
869 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
870 s->cirrus_srccounter = 0;
871 s->cirrus_dstptr = &s->cirrus_bltbuf[0];
872 s->cirrus_dstptr_end = &s->cirrus_bltbuf[0];
873 s->cirrus_dstcounter = 0;
874 s->cirrus_blt_handler = NULL;
877 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
879 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
880 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
881 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
883 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
884 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
885 s->cirrus_srccounter = 8;
887 s->cirrus_srccounter = 8 * 8 * s->cirrus_blt_pixelwidth;
889 s->cirrus_blt_srcpitch = 0;
890 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_patterncopy;
892 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
893 s->cirrus_srccounter =
894 ((s->cirrus_blt_width + 7) / 8) * s->cirrus_blt_height;
895 s->cirrus_blt_srcpitch =
896 s->cirrus_blt_width * s->cirrus_blt_pixelwidth;
898 s->cirrus_srccounter =
899 s->cirrus_blt_width * s->cirrus_blt_height;
900 s->cirrus_blt_srcpitch = s->cirrus_blt_width;
902 /* 4-byte alignment */
903 s->cirrus_srccounter = (s->cirrus_srccounter + 3) & (~3);
905 s->cirrus_blt_handler = cirrus_bitblt_cputovideo_copy;
906 s->cirrus_blt_horz_counter = 0;
909 cirrus_bitblt_cputovideo_next(s);
913 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
917 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
922 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
926 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
927 ret = cirrus_bitblt_videotovideo_patterncopy(s);
929 ret = cirrus_bitblt_videotovideo_copy(s);
933 cirrus_bitblt_reset(s);
937 static void cirrus_bitblt_start(CirrusVGAState * s)
941 s->cirrus_blt_width = (s->gr[0x20] | (s->gr[0x21] << 8)) + 1;
942 s->cirrus_blt_height = (s->gr[0x22] | (s->gr[0x23] << 8)) + 1;
943 s->cirrus_blt_dstpitch = (s->gr[0x24] | (s->gr[0x25] << 8));
944 s->cirrus_blt_srcpitch = (s->gr[0x26] | (s->gr[0x27] << 8));
945 s->cirrus_blt_dstaddr =
946 (s->gr[0x28] | (s->gr[0x29] << 8) | (s->gr[0x2a] << 16));
947 s->cirrus_blt_srcaddr =
948 (s->gr[0x2c] | (s->gr[0x2d] << 8) | (s->gr[0x2e] << 16));
949 s->cirrus_blt_mode = s->gr[0x30];
950 blt_rop = s->gr[0x32];
952 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
953 case CIRRUS_BLTMODE_PIXELWIDTH8:
954 s->cirrus_blt_pixelwidth = 1;
956 case CIRRUS_BLTMODE_PIXELWIDTH16:
957 s->cirrus_blt_pixelwidth = 2;
959 case CIRRUS_BLTMODE_PIXELWIDTH24:
960 s->cirrus_blt_pixelwidth = 3;
962 case CIRRUS_BLTMODE_PIXELWIDTH32:
963 s->cirrus_blt_pixelwidth = 4;
967 printf("cirrus: bitblt - pixel width is unknown\n");
971 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
974 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
975 CIRRUS_BLTMODE_MEMSYSDEST))
976 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
978 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
983 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
984 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
985 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
986 s->cirrus_rop = cirrus_get_bkwd_rop_handler(blt_rop);
988 s->cirrus_rop = cirrus_get_fwd_rop_handler(blt_rop);
991 // setup bitblt engine.
992 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
993 if (!cirrus_bitblt_cputovideo(s))
995 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
996 if (!cirrus_bitblt_videotocpu(s))
999 if (!cirrus_bitblt_videotovideo(s))
1005 cirrus_bitblt_reset(s);
1008 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1012 old_value = s->gr[0x31];
1013 s->gr[0x31] = reg_value;
1015 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1016 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1017 cirrus_bitblt_reset(s);
1018 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1019 ((reg_value & CIRRUS_BLT_START) != 0)) {
1020 s->gr[0x31] |= CIRRUS_BLT_BUSY;
1021 cirrus_bitblt_start(s);
1026 /***************************************
1030 ***************************************/
1032 static void cirrus_get_offsets(VGAState *s1,
1033 uint32_t *pline_offset,
1034 uint32_t *pstart_addr)
1036 CirrusVGAState * s = (CirrusVGAState *)s1;
1037 uint32_t start_addr;
1038 uint32_t line_offset;
1040 line_offset = s->cr[0x13]
1041 | ((s->cr[0x1b] & 0x10) << 4);
1043 *pline_offset = line_offset;
1045 start_addr = (s->cr[0x0c] << 8)
1047 | ((s->cr[0x1b] & 0x01) << 16)
1048 | ((s->cr[0x1b] & 0x0c) << 15)
1049 | ((s->cr[0x1d] & 0x80) << 12);
1050 *pstart_addr = start_addr;
1053 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1057 switch (s->cirrus_hidden_dac_data & 0xf) {
1060 break; /* Sierra HiColor */
1063 break; /* XGA HiColor */
1066 printf("cirrus: invalid DAC value %x in 16bpp\n",
1067 (s->cirrus_hidden_dac_data & 0xf));
1075 static int cirrus_get_bpp(VGAState *s1)
1077 CirrusVGAState * s = (CirrusVGAState *)s1;
1080 if ((s->sr[0x07] & 0x01) != 0) {
1082 switch (s->sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1083 case CIRRUS_SR7_BPP_8:
1086 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1087 ret = cirrus_get_bpp16_depth(s);
1089 case CIRRUS_SR7_BPP_24:
1092 case CIRRUS_SR7_BPP_16:
1093 ret = cirrus_get_bpp16_depth(s);
1095 case CIRRUS_SR7_BPP_32:
1100 printf("cirrus: unknown bpp - sr7=%x\n", s->sr[0x7]);
1113 /***************************************
1117 ***************************************/
1119 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1124 if ((s->gr[0x0b] & 0x01) != 0) /* dual bank */
1125 offset = s->gr[0x09 + bank_index];
1126 else /* single bank */
1127 offset = s->gr[0x09];
1129 if ((s->gr[0x0b] & 0x20) != 0)
1134 if (s->vram_size <= offset)
1137 limit = s->vram_size - offset;
1139 if (((s->gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1140 if (limit > 0x8000) {
1149 s->cirrus_bank_base[bank_index] = offset;
1150 s->cirrus_bank_limit[bank_index] = limit;
1152 s->cirrus_bank_base[bank_index] = 0;
1153 s->cirrus_bank_limit[bank_index] = 0;
1157 /***************************************
1159 * I/O access between 0x3c4-0x3c5
1161 ***************************************/
1164 cirrus_hook_read_sr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1166 switch (reg_index) {
1167 case 0x00: // Standard VGA
1168 case 0x01: // Standard VGA
1169 case 0x02: // Standard VGA
1170 case 0x03: // Standard VGA
1171 case 0x04: // Standard VGA
1172 return CIRRUS_HOOK_NOT_HANDLED;
1173 case 0x06: // Unlock Cirrus extensions
1174 *reg_value = s->sr[reg_index];
1177 case 0x07: // Extended Sequencer Mode
1178 case 0x08: // EEPROM Control
1179 case 0x09: // Scratch Register 0
1180 case 0x0a: // Scratch Register 1
1181 case 0x0b: // VCLK 0
1182 case 0x0c: // VCLK 1
1183 case 0x0d: // VCLK 2
1184 case 0x0e: // VCLK 3
1185 case 0x0f: // DRAM Control
1189 case 0x70: // Graphics Cursor X
1193 case 0xf0: // Graphics Cursor X
1197 case 0x71: // Graphics Cursor Y
1201 case 0xf1: // Graphics Cursor Y
1202 case 0x12: // Graphics Cursor Attribute
1203 case 0x13: // Graphics Cursor Pattern Address
1204 case 0x14: // Scratch Register 2
1205 case 0x15: // Scratch Register 3
1206 case 0x16: // Performance Tuning Register
1207 case 0x17: // Configuration Readback and Extended Control
1208 case 0x18: // Signature Generator Control
1209 case 0x19: // Signal Generator Result
1210 case 0x1a: // Signal Generator Result
1211 case 0x1b: // VCLK 0 Denominator & Post
1212 case 0x1c: // VCLK 1 Denominator & Post
1213 case 0x1d: // VCLK 2 Denominator & Post
1214 case 0x1e: // VCLK 3 Denominator & Post
1215 case 0x1f: // BIOS Write Enable and MCLK select
1217 printf("cirrus: handled inport sr_index %02x\n", reg_index);
1219 *reg_value = s->sr[reg_index];
1223 printf("cirrus: inport sr_index %02x\n", reg_index);
1229 return CIRRUS_HOOK_HANDLED;
1233 cirrus_hook_write_sr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1235 switch (reg_index) {
1236 case 0x00: // Standard VGA
1237 case 0x01: // Standard VGA
1238 case 0x02: // Standard VGA
1239 case 0x03: // Standard VGA
1240 case 0x04: // Standard VGA
1241 return CIRRUS_HOOK_NOT_HANDLED;
1242 case 0x06: // Unlock Cirrus extensions
1244 if (reg_value == 0x12) {
1245 s->sr[reg_index] = 0x12;
1247 s->sr[reg_index] = 0x0f;
1253 case 0x70: // Graphics Cursor X
1257 case 0xf0: // Graphics Cursor X
1258 s->sr[0x10] = reg_value;
1259 s->cirrus_hw_cursor_x = ((reg_index << 3) & 0x700) | reg_value;
1264 case 0x71: // Graphics Cursor Y
1268 case 0xf1: // Graphics Cursor Y
1269 s->sr[0x11] = reg_value;
1270 s->cirrus_hw_cursor_y = ((reg_index << 3) & 0x700) | reg_value;
1272 case 0x07: // Extended Sequencer Mode
1273 case 0x08: // EEPROM Control
1274 case 0x09: // Scratch Register 0
1275 case 0x0a: // Scratch Register 1
1276 case 0x0b: // VCLK 0
1277 case 0x0c: // VCLK 1
1278 case 0x0d: // VCLK 2
1279 case 0x0e: // VCLK 3
1280 case 0x0f: // DRAM Control
1281 case 0x12: // Graphics Cursor Attribute
1282 case 0x13: // Graphics Cursor Pattern Address
1283 case 0x14: // Scratch Register 2
1284 case 0x15: // Scratch Register 3
1285 case 0x16: // Performance Tuning Register
1286 case 0x17: // Configuration Readback and Extended Control
1287 case 0x18: // Signature Generator Control
1288 case 0x19: // Signature Generator Result
1289 case 0x1a: // Signature Generator Result
1290 case 0x1b: // VCLK 0 Denominator & Post
1291 case 0x1c: // VCLK 1 Denominator & Post
1292 case 0x1d: // VCLK 2 Denominator & Post
1293 case 0x1e: // VCLK 3 Denominator & Post
1294 case 0x1f: // BIOS Write Enable and MCLK select
1295 s->sr[reg_index] = reg_value;
1297 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1298 reg_index, reg_value);
1303 printf("cirrus: outport sr_index %02x, sr_value %02x\n", reg_index,
1309 return CIRRUS_HOOK_HANDLED;
1312 /***************************************
1314 * I/O access at 0x3c6
1316 ***************************************/
1318 static void cirrus_read_hidden_dac(CirrusVGAState * s, int *reg_value)
1321 if (s->cirrus_hidden_dac_lockindex < 5) {
1322 if (s->cirrus_hidden_dac_lockindex == 4) {
1323 *reg_value = s->cirrus_hidden_dac_data;
1325 s->cirrus_hidden_dac_lockindex++;
1329 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1331 if (s->cirrus_hidden_dac_lockindex == 4) {
1332 s->cirrus_hidden_dac_data = reg_value;
1334 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1337 s->cirrus_hidden_dac_lockindex = 0;
1340 /***************************************
1342 * I/O access at 0x3c9
1344 ***************************************/
1346 static int cirrus_hook_read_palette(CirrusVGAState * s, int *reg_value)
1348 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1349 return CIRRUS_HOOK_NOT_HANDLED;
1350 if (s->dac_read_index < 0x10) {
1352 s->cirrus_hidden_palette[s->dac_read_index * 3 +
1355 *reg_value = 0xff; /* XXX */
1357 if (++s->dac_sub_index == 3) {
1358 s->dac_sub_index = 0;
1359 s->dac_read_index++;
1361 return CIRRUS_HOOK_HANDLED;
1364 static int cirrus_hook_write_palette(CirrusVGAState * s, int reg_value)
1366 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1367 return CIRRUS_HOOK_NOT_HANDLED;
1368 s->dac_cache[s->dac_sub_index] = reg_value;
1369 if (++s->dac_sub_index == 3) {
1370 if (s->dac_read_index < 0x10) {
1371 memcpy(&s->cirrus_hidden_palette[s->dac_write_index * 3],
1373 /* XXX update cursor */
1375 s->dac_sub_index = 0;
1376 s->dac_write_index++;
1378 return CIRRUS_HOOK_HANDLED;
1381 /***************************************
1383 * I/O access between 0x3ce-0x3cf
1385 ***************************************/
1388 cirrus_hook_read_gr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1390 switch (reg_index) {
1391 case 0x02: // Standard VGA
1392 case 0x03: // Standard VGA
1393 case 0x04: // Standard VGA
1394 case 0x06: // Standard VGA
1395 case 0x07: // Standard VGA
1396 case 0x08: // Standard VGA
1397 return CIRRUS_HOOK_NOT_HANDLED;
1398 case 0x05: // Standard VGA, Cirrus extended mode
1403 if (reg_index < 0x3a) {
1404 *reg_value = s->gr[reg_index];
1407 printf("cirrus: inport gr_index %02x\n", reg_index);
1412 return CIRRUS_HOOK_HANDLED;
1416 cirrus_hook_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1418 switch (reg_index) {
1419 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1420 s->gr[0x00] = reg_value;
1421 return CIRRUS_HOOK_NOT_HANDLED;
1422 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1423 s->gr[0x01] = reg_value;
1424 return CIRRUS_HOOK_NOT_HANDLED;
1425 case 0x02: // Standard VGA
1426 case 0x03: // Standard VGA
1427 case 0x04: // Standard VGA
1428 case 0x06: // Standard VGA
1429 case 0x07: // Standard VGA
1430 case 0x08: // Standard VGA
1431 return CIRRUS_HOOK_NOT_HANDLED;
1432 case 0x05: // Standard VGA, Cirrus extended mode
1433 s->gr[reg_index] = reg_value & 0x7f;
1435 case 0x09: // bank offset #0
1436 case 0x0A: // bank offset #1
1438 s->gr[reg_index] = reg_value;
1439 cirrus_update_bank_ptr(s, 0);
1440 cirrus_update_bank_ptr(s, 1);
1442 case 0x10: // BGCOLOR 0x0000ff00
1443 case 0x11: // FGCOLOR 0x0000ff00
1444 case 0x12: // BGCOLOR 0x00ff0000
1445 case 0x13: // FGCOLOR 0x00ff0000
1446 case 0x14: // BGCOLOR 0xff000000
1447 case 0x15: // FGCOLOR 0xff000000
1448 case 0x20: // BLT WIDTH 0x0000ff
1449 case 0x22: // BLT HEIGHT 0x0000ff
1450 case 0x24: // BLT DEST PITCH 0x0000ff
1451 case 0x26: // BLT SRC PITCH 0x0000ff
1452 case 0x28: // BLT DEST ADDR 0x0000ff
1453 case 0x29: // BLT DEST ADDR 0x00ff00
1454 case 0x2c: // BLT SRC ADDR 0x0000ff
1455 case 0x2d: // BLT SRC ADDR 0x00ff00
1456 case 0x30: // BLT MODE
1457 case 0x32: // RASTER OP
1458 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1459 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1460 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1461 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1462 s->gr[reg_index] = reg_value;
1464 case 0x21: // BLT WIDTH 0x001f00
1465 case 0x23: // BLT HEIGHT 0x001f00
1466 case 0x25: // BLT DEST PITCH 0x001f00
1467 case 0x27: // BLT SRC PITCH 0x001f00
1468 s->gr[reg_index] = reg_value & 0x1f;
1470 case 0x2a: // BLT DEST ADDR 0x3f0000
1471 case 0x2e: // BLT SRC ADDR 0x3f0000
1472 s->gr[reg_index] = reg_value & 0x3f;
1474 case 0x31: // BLT STATUS/START
1475 cirrus_write_bitblt(s, reg_value);
1479 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1485 return CIRRUS_HOOK_HANDLED;
1488 /***************************************
1490 * I/O access between 0x3d4-0x3d5
1492 ***************************************/
1495 cirrus_hook_read_cr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1497 switch (reg_index) {
1498 case 0x00: // Standard VGA
1499 case 0x01: // Standard VGA
1500 case 0x02: // Standard VGA
1501 case 0x03: // Standard VGA
1502 case 0x04: // Standard VGA
1503 case 0x05: // Standard VGA
1504 case 0x06: // Standard VGA
1505 case 0x07: // Standard VGA
1506 case 0x08: // Standard VGA
1507 case 0x09: // Standard VGA
1508 case 0x0a: // Standard VGA
1509 case 0x0b: // Standard VGA
1510 case 0x0c: // Standard VGA
1511 case 0x0d: // Standard VGA
1512 case 0x0e: // Standard VGA
1513 case 0x0f: // Standard VGA
1514 case 0x10: // Standard VGA
1515 case 0x11: // Standard VGA
1516 case 0x12: // Standard VGA
1517 case 0x13: // Standard VGA
1518 case 0x14: // Standard VGA
1519 case 0x15: // Standard VGA
1520 case 0x16: // Standard VGA
1521 case 0x17: // Standard VGA
1522 case 0x18: // Standard VGA
1523 return CIRRUS_HOOK_NOT_HANDLED;
1524 case 0x19: // Interlace End
1525 case 0x1a: // Miscellaneous Control
1526 case 0x1b: // Extended Display Control
1527 case 0x1c: // Sync Adjust and Genlock
1528 case 0x1d: // Overlay Extended Control
1529 case 0x22: // Graphics Data Latches Readback (R)
1530 case 0x24: // Attribute Controller Toggle Readback (R)
1531 case 0x25: // Part Status
1532 case 0x27: // Part ID (R)
1533 *reg_value = s->cr[reg_index];
1535 case 0x26: // Attribute Controller Index Readback (R)
1536 *reg_value = s->ar_index & 0x3f;
1540 printf("cirrus: inport cr_index %02x\n", reg_index);
1546 return CIRRUS_HOOK_HANDLED;
1550 cirrus_hook_write_cr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1552 switch (reg_index) {
1553 case 0x00: // Standard VGA
1554 case 0x01: // Standard VGA
1555 case 0x02: // Standard VGA
1556 case 0x03: // Standard VGA
1557 case 0x04: // Standard VGA
1558 case 0x05: // Standard VGA
1559 case 0x06: // Standard VGA
1560 case 0x07: // Standard VGA
1561 case 0x08: // Standard VGA
1562 case 0x09: // Standard VGA
1563 case 0x0a: // Standard VGA
1564 case 0x0b: // Standard VGA
1565 case 0x0c: // Standard VGA
1566 case 0x0d: // Standard VGA
1567 case 0x0e: // Standard VGA
1568 case 0x0f: // Standard VGA
1569 case 0x10: // Standard VGA
1570 case 0x11: // Standard VGA
1571 case 0x12: // Standard VGA
1572 case 0x13: // Standard VGA
1573 case 0x14: // Standard VGA
1574 case 0x15: // Standard VGA
1575 case 0x16: // Standard VGA
1576 case 0x17: // Standard VGA
1577 case 0x18: // Standard VGA
1578 return CIRRUS_HOOK_NOT_HANDLED;
1579 case 0x19: // Interlace End
1580 case 0x1a: // Miscellaneous Control
1581 case 0x1b: // Extended Display Control
1582 case 0x1c: // Sync Adjust and Genlock
1583 s->cr[reg_index] = reg_value;
1585 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1586 reg_index, reg_value);
1589 case 0x22: // Graphics Data Latches Readback (R)
1590 case 0x24: // Attribute Controller Toggle Readback (R)
1591 case 0x26: // Attribute Controller Index Readback (R)
1592 case 0x27: // Part ID (R)
1594 case 0x1d: // Overlay Extended Control
1595 case 0x25: // Part Status
1598 printf("cirrus: outport cr_index %02x, cr_value %02x\n", reg_index,
1604 return CIRRUS_HOOK_HANDLED;
1607 /***************************************
1609 * memory-mapped I/O (bitblt)
1611 ***************************************/
1613 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1618 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1619 cirrus_hook_read_gr(s, 0x00, &value);
1621 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1622 cirrus_hook_read_gr(s, 0x10, &value);
1624 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1625 cirrus_hook_read_gr(s, 0x12, &value);
1627 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1628 cirrus_hook_read_gr(s, 0x14, &value);
1630 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1631 cirrus_hook_read_gr(s, 0x01, &value);
1633 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1634 cirrus_hook_read_gr(s, 0x11, &value);
1636 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1637 cirrus_hook_read_gr(s, 0x13, &value);
1639 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1640 cirrus_hook_read_gr(s, 0x15, &value);
1642 case (CIRRUS_MMIO_BLTWIDTH + 0):
1643 cirrus_hook_read_gr(s, 0x20, &value);
1645 case (CIRRUS_MMIO_BLTWIDTH + 1):
1646 cirrus_hook_read_gr(s, 0x21, &value);
1648 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1649 cirrus_hook_read_gr(s, 0x22, &value);
1651 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1652 cirrus_hook_read_gr(s, 0x23, &value);
1654 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1655 cirrus_hook_read_gr(s, 0x24, &value);
1657 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1658 cirrus_hook_read_gr(s, 0x25, &value);
1660 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1661 cirrus_hook_read_gr(s, 0x26, &value);
1663 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1664 cirrus_hook_read_gr(s, 0x27, &value);
1666 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1667 cirrus_hook_read_gr(s, 0x28, &value);
1669 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1670 cirrus_hook_read_gr(s, 0x29, &value);
1672 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1673 cirrus_hook_read_gr(s, 0x2a, &value);
1675 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1676 cirrus_hook_read_gr(s, 0x2c, &value);
1678 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1679 cirrus_hook_read_gr(s, 0x2d, &value);
1681 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1682 cirrus_hook_read_gr(s, 0x2e, &value);
1684 case CIRRUS_MMIO_BLTWRITEMASK:
1685 cirrus_hook_read_gr(s, 0x2f, &value);
1687 case CIRRUS_MMIO_BLTMODE:
1688 cirrus_hook_read_gr(s, 0x30, &value);
1690 case CIRRUS_MMIO_BLTROP:
1691 cirrus_hook_read_gr(s, 0x32, &value);
1693 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1694 cirrus_hook_read_gr(s, 0x34, &value);
1696 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1697 cirrus_hook_read_gr(s, 0x35, &value);
1699 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1700 cirrus_hook_read_gr(s, 0x38, &value);
1702 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1703 cirrus_hook_read_gr(s, 0x39, &value);
1705 case CIRRUS_MMIO_BLTSTATUS:
1706 cirrus_hook_read_gr(s, 0x31, &value);
1710 printf("cirrus: mmio read - address 0x%04x\n", address);
1715 return (uint8_t) value;
1718 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1722 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1723 cirrus_hook_write_gr(s, 0x00, value);
1725 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1726 cirrus_hook_write_gr(s, 0x10, value);
1728 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1729 cirrus_hook_write_gr(s, 0x12, value);
1731 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1732 cirrus_hook_write_gr(s, 0x14, value);
1734 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1735 cirrus_hook_write_gr(s, 0x01, value);
1737 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1738 cirrus_hook_write_gr(s, 0x11, value);
1740 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1741 cirrus_hook_write_gr(s, 0x13, value);
1743 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1744 cirrus_hook_write_gr(s, 0x15, value);
1746 case (CIRRUS_MMIO_BLTWIDTH + 0):
1747 cirrus_hook_write_gr(s, 0x20, value);
1749 case (CIRRUS_MMIO_BLTWIDTH + 1):
1750 cirrus_hook_write_gr(s, 0x21, value);
1752 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1753 cirrus_hook_write_gr(s, 0x22, value);
1755 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1756 cirrus_hook_write_gr(s, 0x23, value);
1758 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1759 cirrus_hook_write_gr(s, 0x24, value);
1761 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1762 cirrus_hook_write_gr(s, 0x25, value);
1764 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1765 cirrus_hook_write_gr(s, 0x26, value);
1767 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1768 cirrus_hook_write_gr(s, 0x27, value);
1770 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1771 cirrus_hook_write_gr(s, 0x28, value);
1773 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1774 cirrus_hook_write_gr(s, 0x29, value);
1776 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1777 cirrus_hook_write_gr(s, 0x2a, value);
1779 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1782 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1783 cirrus_hook_write_gr(s, 0x2c, value);
1785 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1786 cirrus_hook_write_gr(s, 0x2d, value);
1788 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1789 cirrus_hook_write_gr(s, 0x2e, value);
1791 case CIRRUS_MMIO_BLTWRITEMASK:
1792 cirrus_hook_write_gr(s, 0x2f, value);
1794 case CIRRUS_MMIO_BLTMODE:
1795 cirrus_hook_write_gr(s, 0x30, value);
1797 case CIRRUS_MMIO_BLTROP:
1798 cirrus_hook_write_gr(s, 0x32, value);
1800 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1801 cirrus_hook_write_gr(s, 0x34, value);
1803 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1804 cirrus_hook_write_gr(s, 0x35, value);
1806 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1807 cirrus_hook_write_gr(s, 0x38, value);
1809 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1810 cirrus_hook_write_gr(s, 0x39, value);
1812 case CIRRUS_MMIO_BLTSTATUS:
1813 cirrus_hook_write_gr(s, 0x31, value);
1817 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1824 /***************************************
1828 * assume TARGET_PAGE_SIZE >= 16
1830 ***************************************/
1832 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1838 unsigned val = mem_value;
1841 dst = s->vram_ptr + offset;
1842 for (x = 0; x < 8; x++) {
1844 *dst++ = s->gr[0x01];
1845 } else if (mode == 5) {
1846 *dst++ = s->gr[0x00];
1850 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1851 cpu_physical_memory_set_dirty(s->vram_offset + offset + 7);
1854 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1860 unsigned val = mem_value;
1863 dst = s->vram_ptr + offset;
1864 for (x = 0; x < 8; x++) {
1866 *dst++ = s->gr[0x01];
1867 *dst++ = s->gr[0x11];
1868 } else if (mode == 5) {
1869 *dst++ = s->gr[0x00];
1870 *dst++ = s->gr[0x10];
1874 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1875 cpu_physical_memory_set_dirty(s->vram_offset + offset + 15);
1878 /***************************************
1880 * memory access between 0xa0000-0xbffff
1882 ***************************************/
1884 static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
1886 CirrusVGAState *s = opaque;
1887 unsigned bank_index;
1888 unsigned bank_offset;
1891 if ((s->sr[0x07] & 0x01) == 0) {
1892 return vga_mem_readb(s, addr);
1895 if (addr < 0x10000) {
1896 /* XXX handle bitblt */
1898 bank_index = addr >> 15;
1899 bank_offset = addr & 0x7fff;
1900 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1901 bank_offset += s->cirrus_bank_base[bank_index];
1902 if ((s->gr[0x0B] & 0x14) == 0x14) {
1904 } else if (s->gr[0x0B] & 0x02) {
1907 bank_offset &= s->cirrus_addr_mask;
1908 val = *(s->vram_ptr + bank_offset);
1911 } else if (addr >= 0x18000 && addr < 0x18100) {
1912 /* memory-mapped I/O */
1914 if ((s->sr[0x17] & 0x44) == 0x04) {
1915 val = cirrus_mmio_blt_read(s, addr & 0xff);
1920 printf("cirrus: mem_readb %06x\n", addr);
1926 static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
1929 #ifdef TARGET_WORDS_BIGENDIAN
1930 v = cirrus_vga_mem_readb(opaque, addr) << 8;
1931 v |= cirrus_vga_mem_readb(opaque, addr + 1);
1933 v = cirrus_vga_mem_readb(opaque, addr);
1934 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1939 static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
1942 #ifdef TARGET_WORDS_BIGENDIAN
1943 v = cirrus_vga_mem_readb(opaque, addr) << 24;
1944 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
1945 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
1946 v |= cirrus_vga_mem_readb(opaque, addr + 3);
1948 v = cirrus_vga_mem_readb(opaque, addr);
1949 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1950 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
1951 v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
1956 static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr,
1959 CirrusVGAState *s = opaque;
1960 unsigned bank_index;
1961 unsigned bank_offset;
1964 if ((s->sr[0x07] & 0x01) == 0) {
1965 vga_mem_writeb(s, addr, mem_value);
1969 if (addr < 0x10000) {
1970 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
1972 *s->cirrus_srcptr++ = (uint8_t) mem_value;
1973 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
1974 cirrus_bitblt_cputovideo_next(s);
1978 bank_index = addr >> 15;
1979 bank_offset = addr & 0x7fff;
1980 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1981 bank_offset += s->cirrus_bank_base[bank_index];
1982 if ((s->gr[0x0B] & 0x14) == 0x14) {
1984 } else if (s->gr[0x0B] & 0x02) {
1987 bank_offset &= s->cirrus_addr_mask;
1988 mode = s->gr[0x05] & 0x7;
1989 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
1990 *(s->vram_ptr + bank_offset) = mem_value;
1991 cpu_physical_memory_set_dirty(s->vram_offset +
1994 if ((s->gr[0x0B] & 0x14) != 0x14) {
1995 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
1999 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2006 } else if (addr >= 0x18000 && addr < 0x18100) {
2007 /* memory-mapped I/O */
2008 if ((s->sr[0x17] & 0x44) == 0x04) {
2009 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2013 printf("cirrus: mem_writeb %06x value %02x\n", addr, mem_value);
2018 static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2020 #ifdef TARGET_WORDS_BIGENDIAN
2021 cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
2022 cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
2024 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2025 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2029 static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2031 #ifdef TARGET_WORDS_BIGENDIAN
2032 cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
2033 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2034 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2035 cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
2037 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2038 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2039 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2040 cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2044 static CPUReadMemoryFunc *cirrus_vga_mem_read[3] = {
2045 cirrus_vga_mem_readb,
2046 cirrus_vga_mem_readw,
2047 cirrus_vga_mem_readl,
2050 static CPUWriteMemoryFunc *cirrus_vga_mem_write[3] = {
2051 cirrus_vga_mem_writeb,
2052 cirrus_vga_mem_writew,
2053 cirrus_vga_mem_writel,
2056 /***************************************
2060 ***************************************/
2062 static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2064 CirrusVGAState *s = (CirrusVGAState *) opaque;
2067 /* XXX: s->vram_size must be a power of two */
2068 addr &= s->cirrus_addr_mask;
2070 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2071 /* memory-mapped I/O */
2072 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2074 /* XXX handle bitblt */
2078 if ((s->gr[0x0B] & 0x14) == 0x14) {
2080 } else if (s->gr[0x0B] & 0x02) {
2083 addr &= s->cirrus_addr_mask;
2084 ret = *(s->vram_ptr + addr);
2090 static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2093 #ifdef TARGET_WORDS_BIGENDIAN
2094 v = cirrus_linear_readb(opaque, addr) << 8;
2095 v |= cirrus_linear_readb(opaque, addr + 1);
2097 v = cirrus_linear_readb(opaque, addr);
2098 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2103 static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2106 #ifdef TARGET_WORDS_BIGENDIAN
2107 v = cirrus_linear_readb(opaque, addr) << 24;
2108 v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2109 v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2110 v |= cirrus_linear_readb(opaque, addr + 3);
2112 v = cirrus_linear_readb(opaque, addr);
2113 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2114 v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2115 v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2120 static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2123 CirrusVGAState *s = (CirrusVGAState *) opaque;
2126 addr &= s->cirrus_addr_mask;
2128 if (((s->sr[0x17] & 0x44) == 0x44) && ((addr & 0x1fff00) == 0x1fff00)) {
2129 /* memory-mapped I/O */
2130 cirrus_mmio_blt_write(s, addr & 0xff, val);
2131 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2133 *s->cirrus_srcptr++ = (uint8_t) val;
2134 if (s->cirrus_srcptr == s->cirrus_srcptr_end) {
2135 cirrus_bitblt_cputovideo_next(s);
2139 if ((s->gr[0x0B] & 0x14) == 0x14) {
2141 } else if (s->gr[0x0B] & 0x02) {
2144 addr &= s->cirrus_addr_mask;
2146 mode = s->gr[0x05] & 0x7;
2147 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2148 *(s->vram_ptr + addr) = (uint8_t) val;
2149 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2151 if ((s->gr[0x0B] & 0x14) != 0x14) {
2152 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2154 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2160 static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2163 #ifdef TARGET_WORDS_BIGENDIAN
2164 cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2165 cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2167 cirrus_linear_writeb(opaque, addr, val & 0xff);
2168 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2172 static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2175 #ifdef TARGET_WORDS_BIGENDIAN
2176 cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2177 cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2178 cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2179 cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2181 cirrus_linear_writeb(opaque, addr, val & 0xff);
2182 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2183 cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2184 cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2189 static CPUReadMemoryFunc *cirrus_linear_read[3] = {
2190 cirrus_linear_readb,
2191 cirrus_linear_readw,
2192 cirrus_linear_readl,
2195 static CPUWriteMemoryFunc *cirrus_linear_write[3] = {
2196 cirrus_linear_writeb,
2197 cirrus_linear_writew,
2198 cirrus_linear_writel,
2203 static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
2205 CirrusVGAState *s = opaque;
2208 /* check port range access depending on color/monochrome mode */
2209 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2210 || (addr >= 0x3d0 && addr <= 0x3df
2211 && !(s->msr & MSR_COLOR_EMULATION))) {
2216 if (s->ar_flip_flop == 0) {
2223 index = s->ar_index & 0x1f;
2236 if (cirrus_hook_read_sr(s, s->sr_index, &val))
2238 val = s->sr[s->sr_index];
2239 #ifdef DEBUG_VGA_REG
2240 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2244 cirrus_read_hidden_dac(s, &val);
2250 if (cirrus_hook_read_palette(s, &val))
2252 val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
2253 if (++s->dac_sub_index == 3) {
2254 s->dac_sub_index = 0;
2255 s->dac_read_index++;
2268 if (cirrus_hook_read_gr(s, s->gr_index, &val))
2270 val = s->gr[s->gr_index];
2271 #ifdef DEBUG_VGA_REG
2272 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2281 if (cirrus_hook_read_cr(s, s->cr_index, &val))
2283 val = s->cr[s->cr_index];
2284 #ifdef DEBUG_VGA_REG
2285 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2288 if (s->cr_index >= 0x20)
2289 printf("S3: CR read index=0x%x val=0x%x\n",
2295 /* just toggle to fool polling */
2296 s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
2298 s->ar_flip_flop = 0;
2305 #if defined(DEBUG_VGA)
2306 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2311 static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2313 CirrusVGAState *s = opaque;
2316 /* check port range access depending on color/monochrome mode */
2317 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2318 || (addr >= 0x3d0 && addr <= 0x3df
2319 && !(s->msr & MSR_COLOR_EMULATION)))
2323 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2328 if (s->ar_flip_flop == 0) {
2332 index = s->ar_index & 0x1f;
2335 s->ar[index] = val & 0x3f;
2338 s->ar[index] = val & ~0x10;
2344 s->ar[index] = val & ~0xc0;
2347 s->ar[index] = val & ~0xf0;
2350 s->ar[index] = val & ~0xf0;
2356 s->ar_flip_flop ^= 1;
2359 s->msr = val & ~0x10;
2365 if (cirrus_hook_write_sr(s, s->sr_index, val))
2367 #ifdef DEBUG_VGA_REG
2368 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2370 s->sr[s->sr_index] = val & sr_mask[s->sr_index];
2373 cirrus_write_hidden_dac(s, val);
2376 s->dac_read_index = val;
2377 s->dac_sub_index = 0;
2381 s->dac_write_index = val;
2382 s->dac_sub_index = 0;
2386 if (cirrus_hook_write_palette(s, val))
2388 s->dac_cache[s->dac_sub_index] = val;
2389 if (++s->dac_sub_index == 3) {
2390 memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
2391 s->dac_sub_index = 0;
2392 s->dac_write_index++;
2399 if (cirrus_hook_write_gr(s, s->gr_index, val))
2401 #ifdef DEBUG_VGA_REG
2402 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2404 s->gr[s->gr_index] = val & gr_mask[s->gr_index];
2412 if (cirrus_hook_write_cr(s, s->cr_index, val))
2414 #ifdef DEBUG_VGA_REG
2415 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2417 /* handle CR0-7 protection */
2418 if ((s->cr[11] & 0x80) && s->cr_index <= 7) {
2419 /* can always write bit 4 of CR7 */
2420 if (s->cr_index == 7)
2421 s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
2424 switch (s->cr_index) {
2425 case 0x01: /* horizontal display end */
2430 case 0x12: /* veritcal display end */
2431 s->cr[s->cr_index] = val;
2435 s->cr[s->cr_index] = val;
2441 s->fcr = val & 0x10;
2446 /***************************************
2448 * memory-mapped I/O access
2450 ***************************************/
2452 static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2454 CirrusVGAState *s = (CirrusVGAState *) opaque;
2456 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2458 if (addr >= 0x100) {
2459 return cirrus_mmio_blt_read(s, addr - 0x100);
2461 return vga_ioport_read(s, addr + 0x3c0);
2465 static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2468 #ifdef TARGET_WORDS_BIGENDIAN
2469 v = cirrus_mmio_readb(opaque, addr) << 8;
2470 v |= cirrus_mmio_readb(opaque, addr + 1);
2472 v = cirrus_mmio_readb(opaque, addr);
2473 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2478 static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2481 #ifdef TARGET_WORDS_BIGENDIAN
2482 v = cirrus_mmio_readb(opaque, addr) << 24;
2483 v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2484 v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2485 v |= cirrus_mmio_readb(opaque, addr + 3);
2487 v = cirrus_mmio_readb(opaque, addr);
2488 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2489 v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2490 v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2495 static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2498 CirrusVGAState *s = (CirrusVGAState *) opaque;
2500 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2502 if (addr >= 0x100) {
2503 cirrus_mmio_blt_write(s, addr - 0x100, val);
2505 vga_ioport_write(s, addr + 0x3c0, val);
2509 static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2512 #ifdef TARGET_WORDS_BIGENDIAN
2513 cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2514 cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2516 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2517 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2521 static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2524 #ifdef TARGET_WORDS_BIGENDIAN
2525 cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2526 cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2527 cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2528 cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2530 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2531 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2532 cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2533 cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2538 static CPUReadMemoryFunc *cirrus_mmio_read[3] = {
2544 static CPUWriteMemoryFunc *cirrus_mmio_write[3] = {
2550 /***************************************
2554 ***************************************/
2556 static void cirrus_init_common(CirrusVGAState * s)
2560 register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
2562 register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
2563 register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
2564 register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
2565 register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
2567 register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
2569 register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
2570 register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
2571 register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
2572 register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
2574 vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read,
2575 cirrus_vga_mem_write, s);
2576 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
2580 s->sr[0x0F] = CIRRUS_MEMSIZE_2M;
2581 s->sr[0x1F] = 0x22; // MemClock
2583 s->cr[0x27] = CIRRUS_ID_CLGD5430;
2585 s->cirrus_hidden_dac_lockindex = 5;
2586 s->cirrus_hidden_dac_data = 0;
2588 /* I/O handler for LFB */
2589 s->cirrus_linear_io_addr =
2590 cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,
2592 /* I/O handler for memory-mapped I/O */
2593 s->cirrus_mmio_io_addr =
2594 cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);
2596 /* XXX: s->vram_size must be a power of two */
2597 s->cirrus_addr_mask = s->vram_size - 1;
2599 s->get_bpp = cirrus_get_bpp;
2600 s->get_offsets = cirrus_get_offsets;
2603 /***************************************
2607 ***************************************/
2609 void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2610 unsigned long vga_ram_offset, int vga_ram_size)
2614 s = qemu_mallocz(sizeof(CirrusVGAState));
2616 vga_common_init((VGAState *)s,
2617 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2618 cirrus_init_common(s);
2619 s->sr[0x17] = CIRRUS_BUSTYPE_ISA;
2620 /* XXX ISA-LFB support */
2623 /***************************************
2627 ***************************************/
2629 static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
2630 uint32_t addr, uint32_t size, int type)
2632 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2634 cpu_register_physical_memory(addr, s->vram_size,
2635 s->cirrus_linear_io_addr);
2638 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
2639 uint32_t addr, uint32_t size, int type)
2641 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
2643 cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
2644 s->cirrus_mmio_io_addr);
2647 void pci_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
2648 unsigned long vga_ram_offset, int vga_ram_size)
2650 PCICirrusVGAState *d;
2654 /* setup PCI configuration registers */
2655 d = (PCICirrusVGAState *)pci_register_device("Cirrus VGA",
2656 sizeof(PCICirrusVGAState),
2658 pci_conf = d->dev.config;
2659 pci_conf[0x00] = (uint8_t) (PCI_VENDOR_CIRRUS & 0xff);
2660 pci_conf[0x01] = (uint8_t) (PCI_VENDOR_CIRRUS >> 8);
2661 pci_conf[0x02] = (uint8_t) (PCI_DEVICE_CLGD5430 & 0xff);
2662 pci_conf[0x03] = (uint8_t) (PCI_DEVICE_CLGD5430 >> 8);
2663 pci_conf[0x04] = PCI_COMMAND_IOACCESS | PCI_COMMAND_MEMACCESS;
2664 pci_conf[0x0a] = PCI_CLASS_SUB_VGA;
2665 pci_conf[0x0b] = PCI_CLASS_BASE_DISPLAY;
2666 pci_conf[0x0e] = PCI_CLASS_HEADERTYPE_00h;
2670 vga_common_init((VGAState *)s,
2671 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
2672 cirrus_init_common(s);
2673 s->sr[0x17] = CIRRUS_BUSTYPE_PCI;
2675 /* setup memory space */
2677 /* memory #1 memory-mapped I/O */
2678 /* XXX: s->vram_size must be a power of two */
2679 pci_register_io_region((PCIDevice *)d, 0, s->vram_size,
2680 PCI_ADDRESS_SPACE_MEM, cirrus_pci_lfb_map);
2681 pci_register_io_region((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
2682 PCI_ADDRESS_SPACE_MEM, cirrus_pci_mmio_map);