test: export vm helpers
[qemu-kvm/amd-iommu.git] / hw / cirrus_vga.c
blobbb3d2a5142d835a83275c9967f4ffb8ada0fc20a
1 /*
2 * QEMU Cirrus CLGD 54xx VGA Emulator.
4 * Copyright (c) 2004 Fabrice Bellard
5 * Copyright (c) 2004 Makoto Suzuki (suzu)
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 * Reference: Finn Thogersons' VGADOC4b
27 * available at http://home.worldonline.dk/~finth/
29 #include "hw.h"
30 #include "pc.h"
31 #include "pci.h"
32 #include "console.h"
33 #include "vga_int.h"
34 #include "kvm.h"
35 #include "qemu-kvm.h"
36 #include "loader.h"
39 * TODO:
40 * - destination write mask support not complete (bits 5..7)
41 * - optimize linear mappings
42 * - optimize bitblt functions
45 //#define DEBUG_CIRRUS
46 //#define DEBUG_BITBLT
48 /***************************************
50 * definitions
52 ***************************************/
54 // ID
55 #define CIRRUS_ID_CLGD5422 (0x23<<2)
56 #define CIRRUS_ID_CLGD5426 (0x24<<2)
57 #define CIRRUS_ID_CLGD5424 (0x25<<2)
58 #define CIRRUS_ID_CLGD5428 (0x26<<2)
59 #define CIRRUS_ID_CLGD5430 (0x28<<2)
60 #define CIRRUS_ID_CLGD5434 (0x2A<<2)
61 #define CIRRUS_ID_CLGD5436 (0x2B<<2)
62 #define CIRRUS_ID_CLGD5446 (0x2E<<2)
64 // sequencer 0x07
65 #define CIRRUS_SR7_BPP_VGA 0x00
66 #define CIRRUS_SR7_BPP_SVGA 0x01
67 #define CIRRUS_SR7_BPP_MASK 0x0e
68 #define CIRRUS_SR7_BPP_8 0x00
69 #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
70 #define CIRRUS_SR7_BPP_24 0x04
71 #define CIRRUS_SR7_BPP_16 0x06
72 #define CIRRUS_SR7_BPP_32 0x08
73 #define CIRRUS_SR7_ISAADDR_MASK 0xe0
75 // sequencer 0x0f
76 #define CIRRUS_MEMSIZE_512k 0x08
77 #define CIRRUS_MEMSIZE_1M 0x10
78 #define CIRRUS_MEMSIZE_2M 0x18
79 #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
81 // sequencer 0x12
82 #define CIRRUS_CURSOR_SHOW 0x01
83 #define CIRRUS_CURSOR_HIDDENPEL 0x02
84 #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
86 // sequencer 0x17
87 #define CIRRUS_BUSTYPE_VLBFAST 0x10
88 #define CIRRUS_BUSTYPE_PCI 0x20
89 #define CIRRUS_BUSTYPE_VLBSLOW 0x30
90 #define CIRRUS_BUSTYPE_ISA 0x38
91 #define CIRRUS_MMIO_ENABLE 0x04
92 #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
93 #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
95 // control 0x0b
96 #define CIRRUS_BANKING_DUAL 0x01
97 #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
99 // control 0x30
100 #define CIRRUS_BLTMODE_BACKWARDS 0x01
101 #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
102 #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
103 #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
104 #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
105 #define CIRRUS_BLTMODE_COLOREXPAND 0x80
106 #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
107 #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
108 #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
109 #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
110 #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
112 // control 0x31
113 #define CIRRUS_BLT_BUSY 0x01
114 #define CIRRUS_BLT_START 0x02
115 #define CIRRUS_BLT_RESET 0x04
116 #define CIRRUS_BLT_FIFOUSED 0x10
117 #define CIRRUS_BLT_AUTOSTART 0x80
119 // control 0x32
120 #define CIRRUS_ROP_0 0x00
121 #define CIRRUS_ROP_SRC_AND_DST 0x05
122 #define CIRRUS_ROP_NOP 0x06
123 #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
124 #define CIRRUS_ROP_NOTDST 0x0b
125 #define CIRRUS_ROP_SRC 0x0d
126 #define CIRRUS_ROP_1 0x0e
127 #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
128 #define CIRRUS_ROP_SRC_XOR_DST 0x59
129 #define CIRRUS_ROP_SRC_OR_DST 0x6d
130 #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
131 #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
132 #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
133 #define CIRRUS_ROP_NOTSRC 0xd0
134 #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
135 #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
137 #define CIRRUS_ROP_NOP_INDEX 2
138 #define CIRRUS_ROP_SRC_INDEX 5
140 // control 0x33
141 #define CIRRUS_BLTMODEEXT_SOLIDFILL 0x04
142 #define CIRRUS_BLTMODEEXT_COLOREXPINV 0x02
143 #define CIRRUS_BLTMODEEXT_DWORDGRANULARITY 0x01
145 // memory-mapped IO
146 #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
147 #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
148 #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
149 #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
150 #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
151 #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
152 #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
153 #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
154 #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
155 #define CIRRUS_MMIO_BLTMODE 0x18 // byte
156 #define CIRRUS_MMIO_BLTROP 0x1a // byte
157 #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
158 #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
159 #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
160 #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
161 #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
162 #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
163 #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
164 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
165 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
166 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
167 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
168 #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
169 #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
170 #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
171 #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
172 #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
173 #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
174 #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
176 #define CIRRUS_PNPMMIO_SIZE 0x1000
178 #define ABS(a) ((signed)(a) > 0 ? a : -a)
180 #define BLTUNSAFE(s) \
182 ( /* check dst is within bounds */ \
183 (s)->cirrus_blt_height * ABS((s)->cirrus_blt_dstpitch) \
184 + ((s)->cirrus_blt_dstaddr & (s)->cirrus_addr_mask) > \
185 (s)->vga.vram_size \
186 ) || \
187 ( /* check src is within bounds */ \
188 (s)->cirrus_blt_height * ABS((s)->cirrus_blt_srcpitch) \
189 + ((s)->cirrus_blt_srcaddr & (s)->cirrus_addr_mask) > \
190 (s)->vga.vram_size \
194 struct CirrusVGAState;
195 typedef void (*cirrus_bitblt_rop_t) (struct CirrusVGAState *s,
196 uint8_t * dst, const uint8_t * src,
197 int dstpitch, int srcpitch,
198 int bltwidth, int bltheight);
199 typedef void (*cirrus_fill_t)(struct CirrusVGAState *s,
200 uint8_t *dst, int dst_pitch, int width, int height);
202 typedef struct CirrusVGAState {
203 VGACommonState vga;
205 int cirrus_linear_io_addr;
206 int cirrus_linear_bitblt_io_addr;
207 int cirrus_mmio_io_addr;
208 uint32_t cirrus_addr_mask;
209 uint32_t linear_mmio_mask;
210 uint8_t cirrus_shadow_gr0;
211 uint8_t cirrus_shadow_gr1;
212 uint8_t cirrus_hidden_dac_lockindex;
213 uint8_t cirrus_hidden_dac_data;
214 uint32_t cirrus_bank_base[2];
215 uint32_t cirrus_bank_limit[2];
216 uint8_t cirrus_hidden_palette[48];
217 uint32_t hw_cursor_x;
218 uint32_t hw_cursor_y;
219 int cirrus_blt_pixelwidth;
220 int cirrus_blt_width;
221 int cirrus_blt_height;
222 int cirrus_blt_dstpitch;
223 int cirrus_blt_srcpitch;
224 uint32_t cirrus_blt_fgcol;
225 uint32_t cirrus_blt_bgcol;
226 uint32_t cirrus_blt_dstaddr;
227 uint32_t cirrus_blt_srcaddr;
228 uint8_t cirrus_blt_mode;
229 uint8_t cirrus_blt_modeext;
230 cirrus_bitblt_rop_t cirrus_rop;
231 #define CIRRUS_BLTBUFSIZE (2048 * 4) /* one line width */
232 uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
233 uint8_t *cirrus_srcptr;
234 uint8_t *cirrus_srcptr_end;
235 uint32_t cirrus_srccounter;
236 /* hwcursor display state */
237 int last_hw_cursor_size;
238 int last_hw_cursor_x;
239 int last_hw_cursor_y;
240 int last_hw_cursor_y_start;
241 int last_hw_cursor_y_end;
242 int real_vram_size; /* XXX: suppress that */
243 int device_id;
244 int bustype;
245 } CirrusVGAState;
247 typedef struct PCICirrusVGAState {
248 PCIDevice dev;
249 CirrusVGAState cirrus_vga;
250 } PCICirrusVGAState;
252 static uint8_t rop_to_index[256];
254 /***************************************
256 * prototypes.
258 ***************************************/
261 static void cirrus_bitblt_reset(CirrusVGAState *s);
262 static void cirrus_update_memory_access(CirrusVGAState *s);
264 /***************************************
266 * raster operations
268 ***************************************/
270 static void cirrus_bitblt_rop_nop(CirrusVGAState *s,
271 uint8_t *dst,const uint8_t *src,
272 int dstpitch,int srcpitch,
273 int bltwidth,int bltheight)
277 static void cirrus_bitblt_fill_nop(CirrusVGAState *s,
278 uint8_t *dst,
279 int dstpitch, int bltwidth,int bltheight)
283 #define ROP_NAME 0
284 #define ROP_OP(d, s) d = 0
285 #include "cirrus_vga_rop.h"
287 #define ROP_NAME src_and_dst
288 #define ROP_OP(d, s) d = (s) & (d)
289 #include "cirrus_vga_rop.h"
291 #define ROP_NAME src_and_notdst
292 #define ROP_OP(d, s) d = (s) & (~(d))
293 #include "cirrus_vga_rop.h"
295 #define ROP_NAME notdst
296 #define ROP_OP(d, s) d = ~(d)
297 #include "cirrus_vga_rop.h"
299 #define ROP_NAME src
300 #define ROP_OP(d, s) d = s
301 #include "cirrus_vga_rop.h"
303 #define ROP_NAME 1
304 #define ROP_OP(d, s) d = ~0
305 #include "cirrus_vga_rop.h"
307 #define ROP_NAME notsrc_and_dst
308 #define ROP_OP(d, s) d = (~(s)) & (d)
309 #include "cirrus_vga_rop.h"
311 #define ROP_NAME src_xor_dst
312 #define ROP_OP(d, s) d = (s) ^ (d)
313 #include "cirrus_vga_rop.h"
315 #define ROP_NAME src_or_dst
316 #define ROP_OP(d, s) d = (s) | (d)
317 #include "cirrus_vga_rop.h"
319 #define ROP_NAME notsrc_or_notdst
320 #define ROP_OP(d, s) d = (~(s)) | (~(d))
321 #include "cirrus_vga_rop.h"
323 #define ROP_NAME src_notxor_dst
324 #define ROP_OP(d, s) d = ~((s) ^ (d))
325 #include "cirrus_vga_rop.h"
327 #define ROP_NAME src_or_notdst
328 #define ROP_OP(d, s) d = (s) | (~(d))
329 #include "cirrus_vga_rop.h"
331 #define ROP_NAME notsrc
332 #define ROP_OP(d, s) d = (~(s))
333 #include "cirrus_vga_rop.h"
335 #define ROP_NAME notsrc_or_dst
336 #define ROP_OP(d, s) d = (~(s)) | (d)
337 #include "cirrus_vga_rop.h"
339 #define ROP_NAME notsrc_and_notdst
340 #define ROP_OP(d, s) d = (~(s)) & (~(d))
341 #include "cirrus_vga_rop.h"
343 static const cirrus_bitblt_rop_t cirrus_fwd_rop[16] = {
344 cirrus_bitblt_rop_fwd_0,
345 cirrus_bitblt_rop_fwd_src_and_dst,
346 cirrus_bitblt_rop_nop,
347 cirrus_bitblt_rop_fwd_src_and_notdst,
348 cirrus_bitblt_rop_fwd_notdst,
349 cirrus_bitblt_rop_fwd_src,
350 cirrus_bitblt_rop_fwd_1,
351 cirrus_bitblt_rop_fwd_notsrc_and_dst,
352 cirrus_bitblt_rop_fwd_src_xor_dst,
353 cirrus_bitblt_rop_fwd_src_or_dst,
354 cirrus_bitblt_rop_fwd_notsrc_or_notdst,
355 cirrus_bitblt_rop_fwd_src_notxor_dst,
356 cirrus_bitblt_rop_fwd_src_or_notdst,
357 cirrus_bitblt_rop_fwd_notsrc,
358 cirrus_bitblt_rop_fwd_notsrc_or_dst,
359 cirrus_bitblt_rop_fwd_notsrc_and_notdst,
362 static const cirrus_bitblt_rop_t cirrus_bkwd_rop[16] = {
363 cirrus_bitblt_rop_bkwd_0,
364 cirrus_bitblt_rop_bkwd_src_and_dst,
365 cirrus_bitblt_rop_nop,
366 cirrus_bitblt_rop_bkwd_src_and_notdst,
367 cirrus_bitblt_rop_bkwd_notdst,
368 cirrus_bitblt_rop_bkwd_src,
369 cirrus_bitblt_rop_bkwd_1,
370 cirrus_bitblt_rop_bkwd_notsrc_and_dst,
371 cirrus_bitblt_rop_bkwd_src_xor_dst,
372 cirrus_bitblt_rop_bkwd_src_or_dst,
373 cirrus_bitblt_rop_bkwd_notsrc_or_notdst,
374 cirrus_bitblt_rop_bkwd_src_notxor_dst,
375 cirrus_bitblt_rop_bkwd_src_or_notdst,
376 cirrus_bitblt_rop_bkwd_notsrc,
377 cirrus_bitblt_rop_bkwd_notsrc_or_dst,
378 cirrus_bitblt_rop_bkwd_notsrc_and_notdst,
381 #define TRANSP_ROP(name) {\
382 name ## _8,\
383 name ## _16,\
385 #define TRANSP_NOP(func) {\
386 func,\
387 func,\
390 static const cirrus_bitblt_rop_t cirrus_fwd_transp_rop[16][2] = {
391 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_0),
392 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_dst),
393 TRANSP_NOP(cirrus_bitblt_rop_nop),
394 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_notdst),
395 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notdst),
396 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src),
397 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_1),
398 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_dst),
399 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_xor_dst),
400 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_dst),
401 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_notdst),
402 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_notxor_dst),
403 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_notdst),
404 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc),
405 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_dst),
406 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_notdst),
409 static const cirrus_bitblt_rop_t cirrus_bkwd_transp_rop[16][2] = {
410 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_0),
411 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_dst),
412 TRANSP_NOP(cirrus_bitblt_rop_nop),
413 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_notdst),
414 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notdst),
415 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src),
416 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_1),
417 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_dst),
418 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_xor_dst),
419 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_dst),
420 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_notdst),
421 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_notxor_dst),
422 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_notdst),
423 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc),
424 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_dst),
425 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_notdst),
428 #define ROP2(name) {\
429 name ## _8,\
430 name ## _16,\
431 name ## _24,\
432 name ## _32,\
435 #define ROP_NOP2(func) {\
436 func,\
437 func,\
438 func,\
439 func,\
442 static const cirrus_bitblt_rop_t cirrus_patternfill[16][4] = {
443 ROP2(cirrus_patternfill_0),
444 ROP2(cirrus_patternfill_src_and_dst),
445 ROP_NOP2(cirrus_bitblt_rop_nop),
446 ROP2(cirrus_patternfill_src_and_notdst),
447 ROP2(cirrus_patternfill_notdst),
448 ROP2(cirrus_patternfill_src),
449 ROP2(cirrus_patternfill_1),
450 ROP2(cirrus_patternfill_notsrc_and_dst),
451 ROP2(cirrus_patternfill_src_xor_dst),
452 ROP2(cirrus_patternfill_src_or_dst),
453 ROP2(cirrus_patternfill_notsrc_or_notdst),
454 ROP2(cirrus_patternfill_src_notxor_dst),
455 ROP2(cirrus_patternfill_src_or_notdst),
456 ROP2(cirrus_patternfill_notsrc),
457 ROP2(cirrus_patternfill_notsrc_or_dst),
458 ROP2(cirrus_patternfill_notsrc_and_notdst),
461 static const cirrus_bitblt_rop_t cirrus_colorexpand_transp[16][4] = {
462 ROP2(cirrus_colorexpand_transp_0),
463 ROP2(cirrus_colorexpand_transp_src_and_dst),
464 ROP_NOP2(cirrus_bitblt_rop_nop),
465 ROP2(cirrus_colorexpand_transp_src_and_notdst),
466 ROP2(cirrus_colorexpand_transp_notdst),
467 ROP2(cirrus_colorexpand_transp_src),
468 ROP2(cirrus_colorexpand_transp_1),
469 ROP2(cirrus_colorexpand_transp_notsrc_and_dst),
470 ROP2(cirrus_colorexpand_transp_src_xor_dst),
471 ROP2(cirrus_colorexpand_transp_src_or_dst),
472 ROP2(cirrus_colorexpand_transp_notsrc_or_notdst),
473 ROP2(cirrus_colorexpand_transp_src_notxor_dst),
474 ROP2(cirrus_colorexpand_transp_src_or_notdst),
475 ROP2(cirrus_colorexpand_transp_notsrc),
476 ROP2(cirrus_colorexpand_transp_notsrc_or_dst),
477 ROP2(cirrus_colorexpand_transp_notsrc_and_notdst),
480 static const cirrus_bitblt_rop_t cirrus_colorexpand[16][4] = {
481 ROP2(cirrus_colorexpand_0),
482 ROP2(cirrus_colorexpand_src_and_dst),
483 ROP_NOP2(cirrus_bitblt_rop_nop),
484 ROP2(cirrus_colorexpand_src_and_notdst),
485 ROP2(cirrus_colorexpand_notdst),
486 ROP2(cirrus_colorexpand_src),
487 ROP2(cirrus_colorexpand_1),
488 ROP2(cirrus_colorexpand_notsrc_and_dst),
489 ROP2(cirrus_colorexpand_src_xor_dst),
490 ROP2(cirrus_colorexpand_src_or_dst),
491 ROP2(cirrus_colorexpand_notsrc_or_notdst),
492 ROP2(cirrus_colorexpand_src_notxor_dst),
493 ROP2(cirrus_colorexpand_src_or_notdst),
494 ROP2(cirrus_colorexpand_notsrc),
495 ROP2(cirrus_colorexpand_notsrc_or_dst),
496 ROP2(cirrus_colorexpand_notsrc_and_notdst),
499 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern_transp[16][4] = {
500 ROP2(cirrus_colorexpand_pattern_transp_0),
501 ROP2(cirrus_colorexpand_pattern_transp_src_and_dst),
502 ROP_NOP2(cirrus_bitblt_rop_nop),
503 ROP2(cirrus_colorexpand_pattern_transp_src_and_notdst),
504 ROP2(cirrus_colorexpand_pattern_transp_notdst),
505 ROP2(cirrus_colorexpand_pattern_transp_src),
506 ROP2(cirrus_colorexpand_pattern_transp_1),
507 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_dst),
508 ROP2(cirrus_colorexpand_pattern_transp_src_xor_dst),
509 ROP2(cirrus_colorexpand_pattern_transp_src_or_dst),
510 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_notdst),
511 ROP2(cirrus_colorexpand_pattern_transp_src_notxor_dst),
512 ROP2(cirrus_colorexpand_pattern_transp_src_or_notdst),
513 ROP2(cirrus_colorexpand_pattern_transp_notsrc),
514 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_dst),
515 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_notdst),
518 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern[16][4] = {
519 ROP2(cirrus_colorexpand_pattern_0),
520 ROP2(cirrus_colorexpand_pattern_src_and_dst),
521 ROP_NOP2(cirrus_bitblt_rop_nop),
522 ROP2(cirrus_colorexpand_pattern_src_and_notdst),
523 ROP2(cirrus_colorexpand_pattern_notdst),
524 ROP2(cirrus_colorexpand_pattern_src),
525 ROP2(cirrus_colorexpand_pattern_1),
526 ROP2(cirrus_colorexpand_pattern_notsrc_and_dst),
527 ROP2(cirrus_colorexpand_pattern_src_xor_dst),
528 ROP2(cirrus_colorexpand_pattern_src_or_dst),
529 ROP2(cirrus_colorexpand_pattern_notsrc_or_notdst),
530 ROP2(cirrus_colorexpand_pattern_src_notxor_dst),
531 ROP2(cirrus_colorexpand_pattern_src_or_notdst),
532 ROP2(cirrus_colorexpand_pattern_notsrc),
533 ROP2(cirrus_colorexpand_pattern_notsrc_or_dst),
534 ROP2(cirrus_colorexpand_pattern_notsrc_and_notdst),
537 static const cirrus_fill_t cirrus_fill[16][4] = {
538 ROP2(cirrus_fill_0),
539 ROP2(cirrus_fill_src_and_dst),
540 ROP_NOP2(cirrus_bitblt_fill_nop),
541 ROP2(cirrus_fill_src_and_notdst),
542 ROP2(cirrus_fill_notdst),
543 ROP2(cirrus_fill_src),
544 ROP2(cirrus_fill_1),
545 ROP2(cirrus_fill_notsrc_and_dst),
546 ROP2(cirrus_fill_src_xor_dst),
547 ROP2(cirrus_fill_src_or_dst),
548 ROP2(cirrus_fill_notsrc_or_notdst),
549 ROP2(cirrus_fill_src_notxor_dst),
550 ROP2(cirrus_fill_src_or_notdst),
551 ROP2(cirrus_fill_notsrc),
552 ROP2(cirrus_fill_notsrc_or_dst),
553 ROP2(cirrus_fill_notsrc_and_notdst),
556 static inline void cirrus_bitblt_fgcol(CirrusVGAState *s)
558 unsigned int color;
559 switch (s->cirrus_blt_pixelwidth) {
560 case 1:
561 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1;
562 break;
563 case 2:
564 color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8);
565 s->cirrus_blt_fgcol = le16_to_cpu(color);
566 break;
567 case 3:
568 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1 |
569 (s->vga.gr[0x11] << 8) | (s->vga.gr[0x13] << 16);
570 break;
571 default:
572 case 4:
573 color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8) |
574 (s->vga.gr[0x13] << 16) | (s->vga.gr[0x15] << 24);
575 s->cirrus_blt_fgcol = le32_to_cpu(color);
576 break;
580 static inline void cirrus_bitblt_bgcol(CirrusVGAState *s)
582 unsigned int color;
583 switch (s->cirrus_blt_pixelwidth) {
584 case 1:
585 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0;
586 break;
587 case 2:
588 color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8);
589 s->cirrus_blt_bgcol = le16_to_cpu(color);
590 break;
591 case 3:
592 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0 |
593 (s->vga.gr[0x10] << 8) | (s->vga.gr[0x12] << 16);
594 break;
595 default:
596 case 4:
597 color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8) |
598 (s->vga.gr[0x12] << 16) | (s->vga.gr[0x14] << 24);
599 s->cirrus_blt_bgcol = le32_to_cpu(color);
600 break;
604 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
605 int off_pitch, int bytesperline,
606 int lines)
608 int y;
609 int off_cur;
610 int off_cur_end;
612 for (y = 0; y < lines; y++) {
613 off_cur = off_begin;
614 off_cur_end = (off_cur + bytesperline) & s->cirrus_addr_mask;
615 off_cur &= TARGET_PAGE_MASK;
616 while (off_cur < off_cur_end) {
617 cpu_physical_memory_set_dirty(s->vga.vram_offset + off_cur);
618 off_cur += TARGET_PAGE_SIZE;
620 off_begin += off_pitch;
624 static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
625 const uint8_t * src)
627 uint8_t *dst;
629 dst = s->vga.vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask);
631 if (BLTUNSAFE(s))
632 return 0;
634 (*s->cirrus_rop) (s, dst, src,
635 s->cirrus_blt_dstpitch, 0,
636 s->cirrus_blt_width, s->cirrus_blt_height);
637 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
638 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
639 s->cirrus_blt_height);
640 return 1;
643 /* fill */
645 static int cirrus_bitblt_solidfill(CirrusVGAState *s, int blt_rop)
647 cirrus_fill_t rop_func;
649 if (BLTUNSAFE(s))
650 return 0;
651 rop_func = cirrus_fill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
652 rop_func(s, s->vga.vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
653 s->cirrus_blt_dstpitch,
654 s->cirrus_blt_width, s->cirrus_blt_height);
655 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
656 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
657 s->cirrus_blt_height);
658 cirrus_bitblt_reset(s);
659 return 1;
662 /***************************************
664 * bitblt (video-to-video)
666 ***************************************/
668 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
670 return cirrus_bitblt_common_patterncopy(s,
671 s->vga.vram_ptr + ((s->cirrus_blt_srcaddr & ~7) &
672 s->cirrus_addr_mask));
675 static void cirrus_do_copy(CirrusVGAState *s, int dst, int src, int w, int h)
677 int sx, sy;
678 int dx, dy;
679 int width, height;
680 int depth;
681 int notify = 0;
683 depth = s->vga.get_bpp(&s->vga) / 8;
684 s->vga.get_resolution(&s->vga, &width, &height);
686 /* extra x, y */
687 sx = (src % ABS(s->cirrus_blt_srcpitch)) / depth;
688 sy = (src / ABS(s->cirrus_blt_srcpitch));
689 dx = (dst % ABS(s->cirrus_blt_dstpitch)) / depth;
690 dy = (dst / ABS(s->cirrus_blt_dstpitch));
692 /* normalize width */
693 w /= depth;
695 /* if we're doing a backward copy, we have to adjust
696 our x/y to be the upper left corner (instead of the lower
697 right corner) */
698 if (s->cirrus_blt_dstpitch < 0) {
699 sx -= (s->cirrus_blt_width / depth) - 1;
700 dx -= (s->cirrus_blt_width / depth) - 1;
701 sy -= s->cirrus_blt_height - 1;
702 dy -= s->cirrus_blt_height - 1;
705 /* are we in the visible portion of memory? */
706 if (sx >= 0 && sy >= 0 && dx >= 0 && dy >= 0 &&
707 (sx + w) <= width && (sy + h) <= height &&
708 (dx + w) <= width && (dy + h) <= height) {
709 notify = 1;
712 /* make to sure only copy if it's a plain copy ROP */
713 if (*s->cirrus_rop != cirrus_bitblt_rop_fwd_src &&
714 *s->cirrus_rop != cirrus_bitblt_rop_bkwd_src)
715 notify = 0;
717 /* we have to flush all pending changes so that the copy
718 is generated at the appropriate moment in time */
719 if (notify)
720 vga_hw_update();
722 (*s->cirrus_rop) (s, s->vga.vram_ptr +
723 (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
724 s->vga.vram_ptr +
725 (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
726 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
727 s->cirrus_blt_width, s->cirrus_blt_height);
729 if (notify)
730 qemu_console_copy(s->vga.ds,
731 sx, sy, dx, dy,
732 s->cirrus_blt_width / depth,
733 s->cirrus_blt_height);
735 /* we don't have to notify the display that this portion has
736 changed since qemu_console_copy implies this */
738 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
739 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
740 s->cirrus_blt_height);
743 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
745 if (BLTUNSAFE(s))
746 return 0;
748 cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->vga.start_addr,
749 s->cirrus_blt_srcaddr - s->vga.start_addr,
750 s->cirrus_blt_width, s->cirrus_blt_height);
752 return 1;
755 /***************************************
757 * bitblt (cpu-to-video)
759 ***************************************/
761 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
763 int copy_count;
764 uint8_t *end_ptr;
766 if (s->cirrus_srccounter > 0) {
767 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
768 cirrus_bitblt_common_patterncopy(s, s->cirrus_bltbuf);
769 the_end:
770 s->cirrus_srccounter = 0;
771 cirrus_bitblt_reset(s);
772 } else {
773 /* at least one scan line */
774 do {
775 (*s->cirrus_rop)(s, s->vga.vram_ptr +
776 (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
777 s->cirrus_bltbuf, 0, 0, s->cirrus_blt_width, 1);
778 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
779 s->cirrus_blt_width, 1);
780 s->cirrus_blt_dstaddr += s->cirrus_blt_dstpitch;
781 s->cirrus_srccounter -= s->cirrus_blt_srcpitch;
782 if (s->cirrus_srccounter <= 0)
783 goto the_end;
784 /* more bytes than needed can be transfered because of
785 word alignment, so we keep them for the next line */
786 /* XXX: keep alignment to speed up transfer */
787 end_ptr = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
788 copy_count = s->cirrus_srcptr_end - end_ptr;
789 memmove(s->cirrus_bltbuf, end_ptr, copy_count);
790 s->cirrus_srcptr = s->cirrus_bltbuf + copy_count;
791 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
792 } while (s->cirrus_srcptr >= s->cirrus_srcptr_end);
797 /***************************************
799 * bitblt wrapper
801 ***************************************/
803 static void cirrus_bitblt_reset(CirrusVGAState * s)
805 int need_update;
807 s->vga.gr[0x31] &=
808 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
809 need_update = s->cirrus_srcptr != &s->cirrus_bltbuf[0]
810 || s->cirrus_srcptr_end != &s->cirrus_bltbuf[0];
811 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
812 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
813 s->cirrus_srccounter = 0;
814 if (!need_update)
815 return;
816 cirrus_update_memory_access(s);
819 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
821 int w;
823 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
824 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
825 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
827 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
828 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
829 s->cirrus_blt_srcpitch = 8;
830 } else {
831 /* XXX: check for 24 bpp */
832 s->cirrus_blt_srcpitch = 8 * 8 * s->cirrus_blt_pixelwidth;
834 s->cirrus_srccounter = s->cirrus_blt_srcpitch;
835 } else {
836 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
837 w = s->cirrus_blt_width / s->cirrus_blt_pixelwidth;
838 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_DWORDGRANULARITY)
839 s->cirrus_blt_srcpitch = ((w + 31) >> 5);
840 else
841 s->cirrus_blt_srcpitch = ((w + 7) >> 3);
842 } else {
843 /* always align input size to 32 bits */
844 s->cirrus_blt_srcpitch = (s->cirrus_blt_width + 3) & ~3;
846 s->cirrus_srccounter = s->cirrus_blt_srcpitch * s->cirrus_blt_height;
848 s->cirrus_srcptr = s->cirrus_bltbuf;
849 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
850 cirrus_update_memory_access(s);
851 return 1;
854 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
856 /* XXX */
857 #ifdef DEBUG_BITBLT
858 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
859 #endif
860 return 0;
863 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
865 int ret;
867 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
868 ret = cirrus_bitblt_videotovideo_patterncopy(s);
869 } else {
870 ret = cirrus_bitblt_videotovideo_copy(s);
872 if (ret)
873 cirrus_bitblt_reset(s);
874 return ret;
877 static void cirrus_bitblt_start(CirrusVGAState * s)
879 uint8_t blt_rop;
881 s->vga.gr[0x31] |= CIRRUS_BLT_BUSY;
883 s->cirrus_blt_width = (s->vga.gr[0x20] | (s->vga.gr[0x21] << 8)) + 1;
884 s->cirrus_blt_height = (s->vga.gr[0x22] | (s->vga.gr[0x23] << 8)) + 1;
885 s->cirrus_blt_dstpitch = (s->vga.gr[0x24] | (s->vga.gr[0x25] << 8));
886 s->cirrus_blt_srcpitch = (s->vga.gr[0x26] | (s->vga.gr[0x27] << 8));
887 s->cirrus_blt_dstaddr =
888 (s->vga.gr[0x28] | (s->vga.gr[0x29] << 8) | (s->vga.gr[0x2a] << 16));
889 s->cirrus_blt_srcaddr =
890 (s->vga.gr[0x2c] | (s->vga.gr[0x2d] << 8) | (s->vga.gr[0x2e] << 16));
891 s->cirrus_blt_mode = s->vga.gr[0x30];
892 s->cirrus_blt_modeext = s->vga.gr[0x33];
893 blt_rop = s->vga.gr[0x32];
895 #ifdef DEBUG_BITBLT
896 printf("rop=0x%02x mode=0x%02x modeext=0x%02x w=%d h=%d dpitch=%d spitch=%d daddr=0x%08x saddr=0x%08x writemask=0x%02x\n",
897 blt_rop,
898 s->cirrus_blt_mode,
899 s->cirrus_blt_modeext,
900 s->cirrus_blt_width,
901 s->cirrus_blt_height,
902 s->cirrus_blt_dstpitch,
903 s->cirrus_blt_srcpitch,
904 s->cirrus_blt_dstaddr,
905 s->cirrus_blt_srcaddr,
906 s->vga.gr[0x2f]);
907 #endif
909 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
910 case CIRRUS_BLTMODE_PIXELWIDTH8:
911 s->cirrus_blt_pixelwidth = 1;
912 break;
913 case CIRRUS_BLTMODE_PIXELWIDTH16:
914 s->cirrus_blt_pixelwidth = 2;
915 break;
916 case CIRRUS_BLTMODE_PIXELWIDTH24:
917 s->cirrus_blt_pixelwidth = 3;
918 break;
919 case CIRRUS_BLTMODE_PIXELWIDTH32:
920 s->cirrus_blt_pixelwidth = 4;
921 break;
922 default:
923 #ifdef DEBUG_BITBLT
924 printf("cirrus: bitblt - pixel width is unknown\n");
925 #endif
926 goto bitblt_ignore;
928 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
930 if ((s->
931 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
932 CIRRUS_BLTMODE_MEMSYSDEST))
933 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
934 #ifdef DEBUG_BITBLT
935 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
936 #endif
937 goto bitblt_ignore;
940 if ((s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
941 (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST |
942 CIRRUS_BLTMODE_TRANSPARENTCOMP |
943 CIRRUS_BLTMODE_PATTERNCOPY |
944 CIRRUS_BLTMODE_COLOREXPAND)) ==
945 (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
946 cirrus_bitblt_fgcol(s);
947 cirrus_bitblt_solidfill(s, blt_rop);
948 } else {
949 if ((s->cirrus_blt_mode & (CIRRUS_BLTMODE_COLOREXPAND |
950 CIRRUS_BLTMODE_PATTERNCOPY)) ==
951 CIRRUS_BLTMODE_COLOREXPAND) {
953 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
954 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
955 cirrus_bitblt_bgcol(s);
956 else
957 cirrus_bitblt_fgcol(s);
958 s->cirrus_rop = cirrus_colorexpand_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
959 } else {
960 cirrus_bitblt_fgcol(s);
961 cirrus_bitblt_bgcol(s);
962 s->cirrus_rop = cirrus_colorexpand[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
964 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
965 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
966 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
967 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
968 cirrus_bitblt_bgcol(s);
969 else
970 cirrus_bitblt_fgcol(s);
971 s->cirrus_rop = cirrus_colorexpand_pattern_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
972 } else {
973 cirrus_bitblt_fgcol(s);
974 cirrus_bitblt_bgcol(s);
975 s->cirrus_rop = cirrus_colorexpand_pattern[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
977 } else {
978 s->cirrus_rop = cirrus_patternfill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
980 } else {
981 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
982 if (s->cirrus_blt_pixelwidth > 2) {
983 printf("src transparent without colorexpand must be 8bpp or 16bpp\n");
984 goto bitblt_ignore;
986 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
987 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
988 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
989 s->cirrus_rop = cirrus_bkwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
990 } else {
991 s->cirrus_rop = cirrus_fwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
993 } else {
994 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
995 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
996 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
997 s->cirrus_rop = cirrus_bkwd_rop[rop_to_index[blt_rop]];
998 } else {
999 s->cirrus_rop = cirrus_fwd_rop[rop_to_index[blt_rop]];
1003 // setup bitblt engine.
1004 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
1005 if (!cirrus_bitblt_cputovideo(s))
1006 goto bitblt_ignore;
1007 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
1008 if (!cirrus_bitblt_videotocpu(s))
1009 goto bitblt_ignore;
1010 } else {
1011 if (!cirrus_bitblt_videotovideo(s))
1012 goto bitblt_ignore;
1015 return;
1016 bitblt_ignore:;
1017 cirrus_bitblt_reset(s);
1020 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1022 unsigned old_value;
1024 old_value = s->vga.gr[0x31];
1025 s->vga.gr[0x31] = reg_value;
1027 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1028 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1029 cirrus_bitblt_reset(s);
1030 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1031 ((reg_value & CIRRUS_BLT_START) != 0)) {
1032 cirrus_bitblt_start(s);
1037 /***************************************
1039 * basic parameters
1041 ***************************************/
1043 static void cirrus_get_offsets(VGACommonState *s1,
1044 uint32_t *pline_offset,
1045 uint32_t *pstart_addr,
1046 uint32_t *pline_compare)
1048 CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
1049 uint32_t start_addr, line_offset, line_compare;
1051 line_offset = s->vga.cr[0x13]
1052 | ((s->vga.cr[0x1b] & 0x10) << 4);
1053 line_offset <<= 3;
1054 *pline_offset = line_offset;
1056 start_addr = (s->vga.cr[0x0c] << 8)
1057 | s->vga.cr[0x0d]
1058 | ((s->vga.cr[0x1b] & 0x01) << 16)
1059 | ((s->vga.cr[0x1b] & 0x0c) << 15)
1060 | ((s->vga.cr[0x1d] & 0x80) << 12);
1061 *pstart_addr = start_addr;
1063 line_compare = s->vga.cr[0x18] |
1064 ((s->vga.cr[0x07] & 0x10) << 4) |
1065 ((s->vga.cr[0x09] & 0x40) << 3);
1066 *pline_compare = line_compare;
1069 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1071 uint32_t ret = 16;
1073 switch (s->cirrus_hidden_dac_data & 0xf) {
1074 case 0:
1075 ret = 15;
1076 break; /* Sierra HiColor */
1077 case 1:
1078 ret = 16;
1079 break; /* XGA HiColor */
1080 default:
1081 #ifdef DEBUG_CIRRUS
1082 printf("cirrus: invalid DAC value %x in 16bpp\n",
1083 (s->cirrus_hidden_dac_data & 0xf));
1084 #endif
1085 ret = 15; /* XXX */
1086 break;
1088 return ret;
1091 static int cirrus_get_bpp(VGACommonState *s1)
1093 CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
1094 uint32_t ret = 8;
1096 if ((s->vga.sr[0x07] & 0x01) != 0) {
1097 /* Cirrus SVGA */
1098 switch (s->vga.sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1099 case CIRRUS_SR7_BPP_8:
1100 ret = 8;
1101 break;
1102 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1103 ret = cirrus_get_bpp16_depth(s);
1104 break;
1105 case CIRRUS_SR7_BPP_24:
1106 ret = 24;
1107 break;
1108 case CIRRUS_SR7_BPP_16:
1109 ret = cirrus_get_bpp16_depth(s);
1110 break;
1111 case CIRRUS_SR7_BPP_32:
1112 ret = 32;
1113 break;
1114 default:
1115 #ifdef DEBUG_CIRRUS
1116 printf("cirrus: unknown bpp - sr7=%x\n", s->vga.sr[0x7]);
1117 #endif
1118 ret = 8;
1119 break;
1121 } else {
1122 /* VGA */
1123 ret = 0;
1126 return ret;
1129 static void cirrus_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
1131 int width, height;
1133 width = (s->cr[0x01] + 1) * 8;
1134 height = s->cr[0x12] |
1135 ((s->cr[0x07] & 0x02) << 7) |
1136 ((s->cr[0x07] & 0x40) << 3);
1137 height = (height + 1);
1138 /* interlace support */
1139 if (s->cr[0x1a] & 0x01)
1140 height = height * 2;
1141 *pwidth = width;
1142 *pheight = height;
1145 /***************************************
1147 * bank memory
1149 ***************************************/
1151 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1153 unsigned offset;
1154 unsigned limit;
1156 if ((s->vga.gr[0x0b] & 0x01) != 0) /* dual bank */
1157 offset = s->vga.gr[0x09 + bank_index];
1158 else /* single bank */
1159 offset = s->vga.gr[0x09];
1161 if ((s->vga.gr[0x0b] & 0x20) != 0)
1162 offset <<= 14;
1163 else
1164 offset <<= 12;
1166 if (s->real_vram_size <= offset)
1167 limit = 0;
1168 else
1169 limit = s->real_vram_size - offset;
1171 if (((s->vga.gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1172 if (limit > 0x8000) {
1173 offset += 0x8000;
1174 limit -= 0x8000;
1175 } else {
1176 limit = 0;
1180 if (limit > 0) {
1181 /* Thinking about changing bank base? First, drop the dirty bitmap information
1182 * on the current location, otherwise we lose this pointer forever */
1183 if (s->vga.lfb_vram_mapped) {
1184 target_phys_addr_t base_addr = isa_mem_base + 0xa0000 + bank_index * 0x8000;
1185 cpu_physical_sync_dirty_bitmap(base_addr, base_addr + 0x8000);
1187 s->cirrus_bank_base[bank_index] = offset;
1188 s->cirrus_bank_limit[bank_index] = limit;
1189 } else {
1190 s->cirrus_bank_base[bank_index] = 0;
1191 s->cirrus_bank_limit[bank_index] = 0;
1195 /***************************************
1197 * I/O access between 0x3c4-0x3c5
1199 ***************************************/
1201 static int cirrus_vga_read_sr(CirrusVGAState * s)
1203 switch (s->vga.sr_index) {
1204 case 0x00: // Standard VGA
1205 case 0x01: // Standard VGA
1206 case 0x02: // Standard VGA
1207 case 0x03: // Standard VGA
1208 case 0x04: // Standard VGA
1209 return s->vga.sr[s->vga.sr_index];
1210 case 0x06: // Unlock Cirrus extensions
1211 return s->vga.sr[s->vga.sr_index];
1212 case 0x10:
1213 case 0x30:
1214 case 0x50:
1215 case 0x70: // Graphics Cursor X
1216 case 0x90:
1217 case 0xb0:
1218 case 0xd0:
1219 case 0xf0: // Graphics Cursor X
1220 return s->vga.sr[0x10];
1221 case 0x11:
1222 case 0x31:
1223 case 0x51:
1224 case 0x71: // Graphics Cursor Y
1225 case 0x91:
1226 case 0xb1:
1227 case 0xd1:
1228 case 0xf1: // Graphics Cursor Y
1229 return s->vga.sr[0x11];
1230 case 0x05: // ???
1231 case 0x07: // Extended Sequencer Mode
1232 case 0x08: // EEPROM Control
1233 case 0x09: // Scratch Register 0
1234 case 0x0a: // Scratch Register 1
1235 case 0x0b: // VCLK 0
1236 case 0x0c: // VCLK 1
1237 case 0x0d: // VCLK 2
1238 case 0x0e: // VCLK 3
1239 case 0x0f: // DRAM Control
1240 case 0x12: // Graphics Cursor Attribute
1241 case 0x13: // Graphics Cursor Pattern Address
1242 case 0x14: // Scratch Register 2
1243 case 0x15: // Scratch Register 3
1244 case 0x16: // Performance Tuning Register
1245 case 0x17: // Configuration Readback and Extended Control
1246 case 0x18: // Signature Generator Control
1247 case 0x19: // Signal Generator Result
1248 case 0x1a: // Signal Generator Result
1249 case 0x1b: // VCLK 0 Denominator & Post
1250 case 0x1c: // VCLK 1 Denominator & Post
1251 case 0x1d: // VCLK 2 Denominator & Post
1252 case 0x1e: // VCLK 3 Denominator & Post
1253 case 0x1f: // BIOS Write Enable and MCLK select
1254 #ifdef DEBUG_CIRRUS
1255 printf("cirrus: handled inport sr_index %02x\n", s->vga.sr_index);
1256 #endif
1257 return s->vga.sr[s->vga.sr_index];
1258 default:
1259 #ifdef DEBUG_CIRRUS
1260 printf("cirrus: inport sr_index %02x\n", s->vga.sr_index);
1261 #endif
1262 return 0xff;
1263 break;
1267 static void cirrus_vga_write_sr(CirrusVGAState * s, uint32_t val)
1269 switch (s->vga.sr_index) {
1270 case 0x00: // Standard VGA
1271 case 0x01: // Standard VGA
1272 case 0x02: // Standard VGA
1273 case 0x03: // Standard VGA
1274 case 0x04: // Standard VGA
1275 s->vga.sr[s->vga.sr_index] = val & sr_mask[s->vga.sr_index];
1276 if (s->vga.sr_index == 1)
1277 s->vga.update_retrace_info(&s->vga);
1278 break;
1279 case 0x06: // Unlock Cirrus extensions
1280 val &= 0x17;
1281 if (val == 0x12) {
1282 s->vga.sr[s->vga.sr_index] = 0x12;
1283 } else {
1284 s->vga.sr[s->vga.sr_index] = 0x0f;
1286 break;
1287 case 0x10:
1288 case 0x30:
1289 case 0x50:
1290 case 0x70: // Graphics Cursor X
1291 case 0x90:
1292 case 0xb0:
1293 case 0xd0:
1294 case 0xf0: // Graphics Cursor X
1295 s->vga.sr[0x10] = val;
1296 s->hw_cursor_x = (val << 3) | (s->vga.sr_index >> 5);
1297 break;
1298 case 0x11:
1299 case 0x31:
1300 case 0x51:
1301 case 0x71: // Graphics Cursor Y
1302 case 0x91:
1303 case 0xb1:
1304 case 0xd1:
1305 case 0xf1: // Graphics Cursor Y
1306 s->vga.sr[0x11] = val;
1307 s->hw_cursor_y = (val << 3) | (s->vga.sr_index >> 5);
1308 break;
1309 case 0x07: // Extended Sequencer Mode
1310 cirrus_update_memory_access(s);
1311 case 0x08: // EEPROM Control
1312 case 0x09: // Scratch Register 0
1313 case 0x0a: // Scratch Register 1
1314 case 0x0b: // VCLK 0
1315 case 0x0c: // VCLK 1
1316 case 0x0d: // VCLK 2
1317 case 0x0e: // VCLK 3
1318 case 0x0f: // DRAM Control
1319 case 0x12: // Graphics Cursor Attribute
1320 case 0x13: // Graphics Cursor Pattern Address
1321 case 0x14: // Scratch Register 2
1322 case 0x15: // Scratch Register 3
1323 case 0x16: // Performance Tuning Register
1324 case 0x18: // Signature Generator Control
1325 case 0x19: // Signature Generator Result
1326 case 0x1a: // Signature Generator Result
1327 case 0x1b: // VCLK 0 Denominator & Post
1328 case 0x1c: // VCLK 1 Denominator & Post
1329 case 0x1d: // VCLK 2 Denominator & Post
1330 case 0x1e: // VCLK 3 Denominator & Post
1331 case 0x1f: // BIOS Write Enable and MCLK select
1332 s->vga.sr[s->vga.sr_index] = val;
1333 #ifdef DEBUG_CIRRUS
1334 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1335 s->vga.sr_index, val);
1336 #endif
1337 break;
1338 case 0x17: // Configuration Readback and Extended Control
1339 s->vga.sr[s->vga.sr_index] = (s->vga.sr[s->vga.sr_index] & 0x38)
1340 | (val & 0xc7);
1341 cirrus_update_memory_access(s);
1342 break;
1343 default:
1344 #ifdef DEBUG_CIRRUS
1345 printf("cirrus: outport sr_index %02x, sr_value %02x\n",
1346 s->vga.sr_index, val);
1347 #endif
1348 break;
1352 /***************************************
1354 * I/O access at 0x3c6
1356 ***************************************/
1358 static int cirrus_read_hidden_dac(CirrusVGAState * s)
1360 if (++s->cirrus_hidden_dac_lockindex == 5) {
1361 s->cirrus_hidden_dac_lockindex = 0;
1362 return s->cirrus_hidden_dac_data;
1364 return 0xff;
1367 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1369 if (s->cirrus_hidden_dac_lockindex == 4) {
1370 s->cirrus_hidden_dac_data = reg_value;
1371 #if defined(DEBUG_CIRRUS)
1372 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1373 #endif
1375 s->cirrus_hidden_dac_lockindex = 0;
1378 /***************************************
1380 * I/O access at 0x3c9
1382 ***************************************/
1384 static int cirrus_vga_read_palette(CirrusVGAState * s)
1386 int val;
1388 if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
1389 val = s->cirrus_hidden_palette[(s->vga.dac_read_index & 0x0f) * 3 +
1390 s->vga.dac_sub_index];
1391 } else {
1392 val = s->vga.palette[s->vga.dac_read_index * 3 + s->vga.dac_sub_index];
1394 if (++s->vga.dac_sub_index == 3) {
1395 s->vga.dac_sub_index = 0;
1396 s->vga.dac_read_index++;
1398 return val;
1401 static void cirrus_vga_write_palette(CirrusVGAState * s, int reg_value)
1403 s->vga.dac_cache[s->vga.dac_sub_index] = reg_value;
1404 if (++s->vga.dac_sub_index == 3) {
1405 if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
1406 memcpy(&s->cirrus_hidden_palette[(s->vga.dac_write_index & 0x0f) * 3],
1407 s->vga.dac_cache, 3);
1408 } else {
1409 memcpy(&s->vga.palette[s->vga.dac_write_index * 3], s->vga.dac_cache, 3);
1411 /* XXX update cursor */
1412 s->vga.dac_sub_index = 0;
1413 s->vga.dac_write_index++;
1417 /***************************************
1419 * I/O access between 0x3ce-0x3cf
1421 ***************************************/
1423 static int cirrus_vga_read_gr(CirrusVGAState * s, unsigned reg_index)
1425 switch (reg_index) {
1426 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1427 return s->cirrus_shadow_gr0;
1428 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1429 return s->cirrus_shadow_gr1;
1430 case 0x02: // Standard VGA
1431 case 0x03: // Standard VGA
1432 case 0x04: // Standard VGA
1433 case 0x06: // Standard VGA
1434 case 0x07: // Standard VGA
1435 case 0x08: // Standard VGA
1436 return s->vga.gr[s->vga.gr_index];
1437 case 0x05: // Standard VGA, Cirrus extended mode
1438 default:
1439 break;
1442 if (reg_index < 0x3a) {
1443 return s->vga.gr[reg_index];
1444 } else {
1445 #ifdef DEBUG_CIRRUS
1446 printf("cirrus: inport gr_index %02x\n", reg_index);
1447 #endif
1448 return 0xff;
1452 static void
1453 cirrus_vga_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1455 #if defined(DEBUG_BITBLT) && 0
1456 printf("gr%02x: %02x\n", reg_index, reg_value);
1457 #endif
1458 switch (reg_index) {
1459 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1460 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1461 s->cirrus_shadow_gr0 = reg_value;
1462 break;
1463 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1464 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1465 s->cirrus_shadow_gr1 = reg_value;
1466 break;
1467 case 0x02: // Standard VGA
1468 case 0x03: // Standard VGA
1469 case 0x04: // Standard VGA
1470 case 0x06: // Standard VGA
1471 case 0x07: // Standard VGA
1472 case 0x08: // Standard VGA
1473 s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
1474 break;
1475 case 0x05: // Standard VGA, Cirrus extended mode
1476 s->vga.gr[reg_index] = reg_value & 0x7f;
1477 cirrus_update_memory_access(s);
1478 break;
1479 case 0x09: // bank offset #0
1480 case 0x0A: // bank offset #1
1481 s->vga.gr[reg_index] = reg_value;
1482 cirrus_update_bank_ptr(s, 0);
1483 cirrus_update_bank_ptr(s, 1);
1484 cirrus_update_memory_access(s);
1485 break;
1486 case 0x0B:
1487 s->vga.gr[reg_index] = reg_value;
1488 cirrus_update_bank_ptr(s, 0);
1489 cirrus_update_bank_ptr(s, 1);
1490 cirrus_update_memory_access(s);
1491 break;
1492 case 0x10: // BGCOLOR 0x0000ff00
1493 case 0x11: // FGCOLOR 0x0000ff00
1494 case 0x12: // BGCOLOR 0x00ff0000
1495 case 0x13: // FGCOLOR 0x00ff0000
1496 case 0x14: // BGCOLOR 0xff000000
1497 case 0x15: // FGCOLOR 0xff000000
1498 case 0x20: // BLT WIDTH 0x0000ff
1499 case 0x22: // BLT HEIGHT 0x0000ff
1500 case 0x24: // BLT DEST PITCH 0x0000ff
1501 case 0x26: // BLT SRC PITCH 0x0000ff
1502 case 0x28: // BLT DEST ADDR 0x0000ff
1503 case 0x29: // BLT DEST ADDR 0x00ff00
1504 case 0x2c: // BLT SRC ADDR 0x0000ff
1505 case 0x2d: // BLT SRC ADDR 0x00ff00
1506 case 0x2f: // BLT WRITEMASK
1507 case 0x30: // BLT MODE
1508 case 0x32: // RASTER OP
1509 case 0x33: // BLT MODEEXT
1510 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1511 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1512 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1513 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1514 s->vga.gr[reg_index] = reg_value;
1515 break;
1516 case 0x21: // BLT WIDTH 0x001f00
1517 case 0x23: // BLT HEIGHT 0x001f00
1518 case 0x25: // BLT DEST PITCH 0x001f00
1519 case 0x27: // BLT SRC PITCH 0x001f00
1520 s->vga.gr[reg_index] = reg_value & 0x1f;
1521 break;
1522 case 0x2a: // BLT DEST ADDR 0x3f0000
1523 s->vga.gr[reg_index] = reg_value & 0x3f;
1524 /* if auto start mode, starts bit blt now */
1525 if (s->vga.gr[0x31] & CIRRUS_BLT_AUTOSTART) {
1526 cirrus_bitblt_start(s);
1528 break;
1529 case 0x2e: // BLT SRC ADDR 0x3f0000
1530 s->vga.gr[reg_index] = reg_value & 0x3f;
1531 break;
1532 case 0x31: // BLT STATUS/START
1533 cirrus_write_bitblt(s, reg_value);
1534 break;
1535 default:
1536 #ifdef DEBUG_CIRRUS
1537 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1538 reg_value);
1539 #endif
1540 break;
1544 /***************************************
1546 * I/O access between 0x3d4-0x3d5
1548 ***************************************/
1550 static int cirrus_vga_read_cr(CirrusVGAState * s, unsigned reg_index)
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 s->vga.cr[s->vga.cr_index];
1579 case 0x24: // Attribute Controller Toggle Readback (R)
1580 return (s->vga.ar_flip_flop << 7);
1581 case 0x19: // Interlace End
1582 case 0x1a: // Miscellaneous Control
1583 case 0x1b: // Extended Display Control
1584 case 0x1c: // Sync Adjust and Genlock
1585 case 0x1d: // Overlay Extended Control
1586 case 0x22: // Graphics Data Latches Readback (R)
1587 case 0x25: // Part Status
1588 case 0x27: // Part ID (R)
1589 return s->vga.cr[s->vga.cr_index];
1590 case 0x26: // Attribute Controller Index Readback (R)
1591 return s->vga.ar_index & 0x3f;
1592 break;
1593 default:
1594 #ifdef DEBUG_CIRRUS
1595 printf("cirrus: inport cr_index %02x\n", reg_index);
1596 #endif
1597 return 0xff;
1601 static void cirrus_vga_write_cr(CirrusVGAState * s, int reg_value)
1603 switch (s->vga.cr_index) {
1604 case 0x00: // Standard VGA
1605 case 0x01: // Standard VGA
1606 case 0x02: // Standard VGA
1607 case 0x03: // Standard VGA
1608 case 0x04: // Standard VGA
1609 case 0x05: // Standard VGA
1610 case 0x06: // Standard VGA
1611 case 0x07: // Standard VGA
1612 case 0x08: // Standard VGA
1613 case 0x09: // Standard VGA
1614 case 0x0a: // Standard VGA
1615 case 0x0b: // Standard VGA
1616 case 0x0c: // Standard VGA
1617 case 0x0d: // Standard VGA
1618 case 0x0e: // Standard VGA
1619 case 0x0f: // Standard VGA
1620 case 0x10: // Standard VGA
1621 case 0x11: // Standard VGA
1622 case 0x12: // Standard VGA
1623 case 0x13: // Standard VGA
1624 case 0x14: // Standard VGA
1625 case 0x15: // Standard VGA
1626 case 0x16: // Standard VGA
1627 case 0x17: // Standard VGA
1628 case 0x18: // Standard VGA
1629 /* handle CR0-7 protection */
1630 if ((s->vga.cr[0x11] & 0x80) && s->vga.cr_index <= 7) {
1631 /* can always write bit 4 of CR7 */
1632 if (s->vga.cr_index == 7)
1633 s->vga.cr[7] = (s->vga.cr[7] & ~0x10) | (reg_value & 0x10);
1634 return;
1636 s->vga.cr[s->vga.cr_index] = reg_value;
1637 switch(s->vga.cr_index) {
1638 case 0x00:
1639 case 0x04:
1640 case 0x05:
1641 case 0x06:
1642 case 0x07:
1643 case 0x11:
1644 case 0x17:
1645 s->vga.update_retrace_info(&s->vga);
1646 break;
1648 break;
1649 case 0x19: // Interlace End
1650 case 0x1a: // Miscellaneous Control
1651 case 0x1b: // Extended Display Control
1652 case 0x1c: // Sync Adjust and Genlock
1653 case 0x1d: // Overlay Extended Control
1654 s->vga.cr[s->vga.cr_index] = reg_value;
1655 #ifdef DEBUG_CIRRUS
1656 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1657 s->vga.cr_index, reg_value);
1658 #endif
1659 break;
1660 case 0x22: // Graphics Data Latches Readback (R)
1661 case 0x24: // Attribute Controller Toggle Readback (R)
1662 case 0x26: // Attribute Controller Index Readback (R)
1663 case 0x27: // Part ID (R)
1664 break;
1665 case 0x25: // Part Status
1666 default:
1667 #ifdef DEBUG_CIRRUS
1668 printf("cirrus: outport cr_index %02x, cr_value %02x\n",
1669 s->vga.cr_index, reg_value);
1670 #endif
1671 break;
1675 /***************************************
1677 * memory-mapped I/O (bitblt)
1679 ***************************************/
1681 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1683 int value = 0xff;
1685 switch (address) {
1686 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1687 value = cirrus_vga_read_gr(s, 0x00);
1688 break;
1689 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1690 value = cirrus_vga_read_gr(s, 0x10);
1691 break;
1692 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1693 value = cirrus_vga_read_gr(s, 0x12);
1694 break;
1695 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1696 value = cirrus_vga_read_gr(s, 0x14);
1697 break;
1698 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1699 value = cirrus_vga_read_gr(s, 0x01);
1700 break;
1701 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1702 value = cirrus_vga_read_gr(s, 0x11);
1703 break;
1704 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1705 value = cirrus_vga_read_gr(s, 0x13);
1706 break;
1707 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1708 value = cirrus_vga_read_gr(s, 0x15);
1709 break;
1710 case (CIRRUS_MMIO_BLTWIDTH + 0):
1711 value = cirrus_vga_read_gr(s, 0x20);
1712 break;
1713 case (CIRRUS_MMIO_BLTWIDTH + 1):
1714 value = cirrus_vga_read_gr(s, 0x21);
1715 break;
1716 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1717 value = cirrus_vga_read_gr(s, 0x22);
1718 break;
1719 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1720 value = cirrus_vga_read_gr(s, 0x23);
1721 break;
1722 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1723 value = cirrus_vga_read_gr(s, 0x24);
1724 break;
1725 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1726 value = cirrus_vga_read_gr(s, 0x25);
1727 break;
1728 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1729 value = cirrus_vga_read_gr(s, 0x26);
1730 break;
1731 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1732 value = cirrus_vga_read_gr(s, 0x27);
1733 break;
1734 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1735 value = cirrus_vga_read_gr(s, 0x28);
1736 break;
1737 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1738 value = cirrus_vga_read_gr(s, 0x29);
1739 break;
1740 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1741 value = cirrus_vga_read_gr(s, 0x2a);
1742 break;
1743 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1744 value = cirrus_vga_read_gr(s, 0x2c);
1745 break;
1746 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1747 value = cirrus_vga_read_gr(s, 0x2d);
1748 break;
1749 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1750 value = cirrus_vga_read_gr(s, 0x2e);
1751 break;
1752 case CIRRUS_MMIO_BLTWRITEMASK:
1753 value = cirrus_vga_read_gr(s, 0x2f);
1754 break;
1755 case CIRRUS_MMIO_BLTMODE:
1756 value = cirrus_vga_read_gr(s, 0x30);
1757 break;
1758 case CIRRUS_MMIO_BLTROP:
1759 value = cirrus_vga_read_gr(s, 0x32);
1760 break;
1761 case CIRRUS_MMIO_BLTMODEEXT:
1762 value = cirrus_vga_read_gr(s, 0x33);
1763 break;
1764 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1765 value = cirrus_vga_read_gr(s, 0x34);
1766 break;
1767 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1768 value = cirrus_vga_read_gr(s, 0x35);
1769 break;
1770 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1771 value = cirrus_vga_read_gr(s, 0x38);
1772 break;
1773 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1774 value = cirrus_vga_read_gr(s, 0x39);
1775 break;
1776 case CIRRUS_MMIO_BLTSTATUS:
1777 value = cirrus_vga_read_gr(s, 0x31);
1778 break;
1779 default:
1780 #ifdef DEBUG_CIRRUS
1781 printf("cirrus: mmio read - address 0x%04x\n", address);
1782 #endif
1783 break;
1786 return (uint8_t) value;
1789 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1790 uint8_t value)
1792 switch (address) {
1793 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1794 cirrus_vga_write_gr(s, 0x00, value);
1795 break;
1796 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1797 cirrus_vga_write_gr(s, 0x10, value);
1798 break;
1799 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1800 cirrus_vga_write_gr(s, 0x12, value);
1801 break;
1802 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1803 cirrus_vga_write_gr(s, 0x14, value);
1804 break;
1805 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1806 cirrus_vga_write_gr(s, 0x01, value);
1807 break;
1808 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1809 cirrus_vga_write_gr(s, 0x11, value);
1810 break;
1811 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1812 cirrus_vga_write_gr(s, 0x13, value);
1813 break;
1814 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1815 cirrus_vga_write_gr(s, 0x15, value);
1816 break;
1817 case (CIRRUS_MMIO_BLTWIDTH + 0):
1818 cirrus_vga_write_gr(s, 0x20, value);
1819 break;
1820 case (CIRRUS_MMIO_BLTWIDTH + 1):
1821 cirrus_vga_write_gr(s, 0x21, value);
1822 break;
1823 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1824 cirrus_vga_write_gr(s, 0x22, value);
1825 break;
1826 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1827 cirrus_vga_write_gr(s, 0x23, value);
1828 break;
1829 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1830 cirrus_vga_write_gr(s, 0x24, value);
1831 break;
1832 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1833 cirrus_vga_write_gr(s, 0x25, value);
1834 break;
1835 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1836 cirrus_vga_write_gr(s, 0x26, value);
1837 break;
1838 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1839 cirrus_vga_write_gr(s, 0x27, value);
1840 break;
1841 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1842 cirrus_vga_write_gr(s, 0x28, value);
1843 break;
1844 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1845 cirrus_vga_write_gr(s, 0x29, value);
1846 break;
1847 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1848 cirrus_vga_write_gr(s, 0x2a, value);
1849 break;
1850 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1851 /* ignored */
1852 break;
1853 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1854 cirrus_vga_write_gr(s, 0x2c, value);
1855 break;
1856 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1857 cirrus_vga_write_gr(s, 0x2d, value);
1858 break;
1859 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1860 cirrus_vga_write_gr(s, 0x2e, value);
1861 break;
1862 case CIRRUS_MMIO_BLTWRITEMASK:
1863 cirrus_vga_write_gr(s, 0x2f, value);
1864 break;
1865 case CIRRUS_MMIO_BLTMODE:
1866 cirrus_vga_write_gr(s, 0x30, value);
1867 break;
1868 case CIRRUS_MMIO_BLTROP:
1869 cirrus_vga_write_gr(s, 0x32, value);
1870 break;
1871 case CIRRUS_MMIO_BLTMODEEXT:
1872 cirrus_vga_write_gr(s, 0x33, value);
1873 break;
1874 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1875 cirrus_vga_write_gr(s, 0x34, value);
1876 break;
1877 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1878 cirrus_vga_write_gr(s, 0x35, value);
1879 break;
1880 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1881 cirrus_vga_write_gr(s, 0x38, value);
1882 break;
1883 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1884 cirrus_vga_write_gr(s, 0x39, value);
1885 break;
1886 case CIRRUS_MMIO_BLTSTATUS:
1887 cirrus_vga_write_gr(s, 0x31, value);
1888 break;
1889 default:
1890 #ifdef DEBUG_CIRRUS
1891 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1892 address, value);
1893 #endif
1894 break;
1898 /***************************************
1900 * write mode 4/5
1902 * assume TARGET_PAGE_SIZE >= 16
1904 ***************************************/
1906 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1907 unsigned mode,
1908 unsigned offset,
1909 uint32_t mem_value)
1911 int x;
1912 unsigned val = mem_value;
1913 uint8_t *dst;
1915 dst = s->vga.vram_ptr + (offset &= s->cirrus_addr_mask);
1916 for (x = 0; x < 8; x++) {
1917 if (val & 0x80) {
1918 *dst = s->cirrus_shadow_gr1;
1919 } else if (mode == 5) {
1920 *dst = s->cirrus_shadow_gr0;
1922 val <<= 1;
1923 dst++;
1925 cpu_physical_memory_set_dirty(s->vga.vram_offset + offset);
1926 cpu_physical_memory_set_dirty(s->vga.vram_offset + offset + 7);
1929 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1930 unsigned mode,
1931 unsigned offset,
1932 uint32_t mem_value)
1934 int x;
1935 unsigned val = mem_value;
1936 uint8_t *dst;
1938 dst = s->vga.vram_ptr + (offset &= s->cirrus_addr_mask);
1939 for (x = 0; x < 8; x++) {
1940 if (val & 0x80) {
1941 *dst = s->cirrus_shadow_gr1;
1942 *(dst + 1) = s->vga.gr[0x11];
1943 } else if (mode == 5) {
1944 *dst = s->cirrus_shadow_gr0;
1945 *(dst + 1) = s->vga.gr[0x10];
1947 val <<= 1;
1948 dst += 2;
1950 cpu_physical_memory_set_dirty(s->vga.vram_offset + offset);
1951 cpu_physical_memory_set_dirty(s->vga.vram_offset + offset + 15);
1954 /***************************************
1956 * memory access between 0xa0000-0xbffff
1958 ***************************************/
1960 static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
1962 CirrusVGAState *s = opaque;
1963 unsigned bank_index;
1964 unsigned bank_offset;
1965 uint32_t val;
1967 if ((s->vga.sr[0x07] & 0x01) == 0) {
1968 return vga_mem_readb(s, addr);
1971 addr &= 0x1ffff;
1973 if (addr < 0x10000) {
1974 /* XXX handle bitblt */
1975 /* video memory */
1976 bank_index = addr >> 15;
1977 bank_offset = addr & 0x7fff;
1978 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1979 bank_offset += s->cirrus_bank_base[bank_index];
1980 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
1981 bank_offset <<= 4;
1982 } else if (s->vga.gr[0x0B] & 0x02) {
1983 bank_offset <<= 3;
1985 bank_offset &= s->cirrus_addr_mask;
1986 val = *(s->vga.vram_ptr + bank_offset);
1987 } else
1988 val = 0xff;
1989 } else if (addr >= 0x18000 && addr < 0x18100) {
1990 /* memory-mapped I/O */
1991 val = 0xff;
1992 if ((s->vga.sr[0x17] & 0x44) == 0x04) {
1993 val = cirrus_mmio_blt_read(s, addr & 0xff);
1995 } else {
1996 val = 0xff;
1997 #ifdef DEBUG_CIRRUS
1998 printf("cirrus: mem_readb " TARGET_FMT_plx "\n", addr);
1999 #endif
2001 return val;
2004 static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
2006 uint32_t v;
2007 #ifdef TARGET_WORDS_BIGENDIAN
2008 v = cirrus_vga_mem_readb(opaque, addr) << 8;
2009 v |= cirrus_vga_mem_readb(opaque, addr + 1);
2010 #else
2011 v = cirrus_vga_mem_readb(opaque, addr);
2012 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2013 #endif
2014 return v;
2017 static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
2019 uint32_t v;
2020 #ifdef TARGET_WORDS_BIGENDIAN
2021 v = cirrus_vga_mem_readb(opaque, addr) << 24;
2022 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
2023 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
2024 v |= cirrus_vga_mem_readb(opaque, addr + 3);
2025 #else
2026 v = cirrus_vga_mem_readb(opaque, addr);
2027 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2028 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
2029 v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
2030 #endif
2031 return v;
2034 static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr,
2035 uint32_t mem_value)
2037 CirrusVGAState *s = opaque;
2038 unsigned bank_index;
2039 unsigned bank_offset;
2040 unsigned mode;
2042 if ((s->vga.sr[0x07] & 0x01) == 0) {
2043 vga_mem_writeb(s, addr, mem_value);
2044 return;
2047 addr &= 0x1ffff;
2049 if (addr < 0x10000) {
2050 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2051 /* bitblt */
2052 *s->cirrus_srcptr++ = (uint8_t) mem_value;
2053 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2054 cirrus_bitblt_cputovideo_next(s);
2056 } else {
2057 /* video memory */
2058 bank_index = addr >> 15;
2059 bank_offset = addr & 0x7fff;
2060 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2061 bank_offset += s->cirrus_bank_base[bank_index];
2062 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2063 bank_offset <<= 4;
2064 } else if (s->vga.gr[0x0B] & 0x02) {
2065 bank_offset <<= 3;
2067 bank_offset &= s->cirrus_addr_mask;
2068 mode = s->vga.gr[0x05] & 0x7;
2069 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2070 *(s->vga.vram_ptr + bank_offset) = mem_value;
2071 cpu_physical_memory_set_dirty(s->vga.vram_offset +
2072 bank_offset);
2073 } else {
2074 if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
2075 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
2076 bank_offset,
2077 mem_value);
2078 } else {
2079 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2080 bank_offset,
2081 mem_value);
2086 } else if (addr >= 0x18000 && addr < 0x18100) {
2087 /* memory-mapped I/O */
2088 if ((s->vga.sr[0x17] & 0x44) == 0x04) {
2089 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2091 } else {
2092 #ifdef DEBUG_CIRRUS
2093 printf("cirrus: mem_writeb " TARGET_FMT_plx " value %02x\n", addr,
2094 mem_value);
2095 #endif
2099 static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2101 #ifdef TARGET_WORDS_BIGENDIAN
2102 cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
2103 cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
2104 #else
2105 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2106 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2107 #endif
2110 static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2112 #ifdef TARGET_WORDS_BIGENDIAN
2113 cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
2114 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2115 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2116 cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
2117 #else
2118 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2119 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2120 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2121 cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2122 #endif
2125 static CPUReadMemoryFunc * const cirrus_vga_mem_read[3] = {
2126 cirrus_vga_mem_readb,
2127 cirrus_vga_mem_readw,
2128 cirrus_vga_mem_readl,
2131 static CPUWriteMemoryFunc * const cirrus_vga_mem_write[3] = {
2132 cirrus_vga_mem_writeb,
2133 cirrus_vga_mem_writew,
2134 cirrus_vga_mem_writel,
2137 /***************************************
2139 * hardware cursor
2141 ***************************************/
2143 static inline void invalidate_cursor1(CirrusVGAState *s)
2145 if (s->last_hw_cursor_size) {
2146 vga_invalidate_scanlines(&s->vga,
2147 s->last_hw_cursor_y + s->last_hw_cursor_y_start,
2148 s->last_hw_cursor_y + s->last_hw_cursor_y_end);
2152 static inline void cirrus_cursor_compute_yrange(CirrusVGAState *s)
2154 const uint8_t *src;
2155 uint32_t content;
2156 int y, y_min, y_max;
2158 src = s->vga.vram_ptr + s->real_vram_size - 16 * 1024;
2159 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2160 src += (s->vga.sr[0x13] & 0x3c) * 256;
2161 y_min = 64;
2162 y_max = -1;
2163 for(y = 0; y < 64; y++) {
2164 content = ((uint32_t *)src)[0] |
2165 ((uint32_t *)src)[1] |
2166 ((uint32_t *)src)[2] |
2167 ((uint32_t *)src)[3];
2168 if (content) {
2169 if (y < y_min)
2170 y_min = y;
2171 if (y > y_max)
2172 y_max = y;
2174 src += 16;
2176 } else {
2177 src += (s->vga.sr[0x13] & 0x3f) * 256;
2178 y_min = 32;
2179 y_max = -1;
2180 for(y = 0; y < 32; y++) {
2181 content = ((uint32_t *)src)[0] |
2182 ((uint32_t *)(src + 128))[0];
2183 if (content) {
2184 if (y < y_min)
2185 y_min = y;
2186 if (y > y_max)
2187 y_max = y;
2189 src += 4;
2192 if (y_min > y_max) {
2193 s->last_hw_cursor_y_start = 0;
2194 s->last_hw_cursor_y_end = 0;
2195 } else {
2196 s->last_hw_cursor_y_start = y_min;
2197 s->last_hw_cursor_y_end = y_max + 1;
2201 /* NOTE: we do not currently handle the cursor bitmap change, so we
2202 update the cursor only if it moves. */
2203 static void cirrus_cursor_invalidate(VGACommonState *s1)
2205 CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
2206 int size;
2208 if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW)) {
2209 size = 0;
2210 } else {
2211 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE)
2212 size = 64;
2213 else
2214 size = 32;
2216 /* invalidate last cursor and new cursor if any change */
2217 if (s->last_hw_cursor_size != size ||
2218 s->last_hw_cursor_x != s->hw_cursor_x ||
2219 s->last_hw_cursor_y != s->hw_cursor_y) {
2221 invalidate_cursor1(s);
2223 s->last_hw_cursor_size = size;
2224 s->last_hw_cursor_x = s->hw_cursor_x;
2225 s->last_hw_cursor_y = s->hw_cursor_y;
2226 /* compute the real cursor min and max y */
2227 cirrus_cursor_compute_yrange(s);
2228 invalidate_cursor1(s);
2232 static void cirrus_cursor_draw_line(VGACommonState *s1, uint8_t *d1, int scr_y)
2234 CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
2235 int w, h, bpp, x1, x2, poffset;
2236 unsigned int color0, color1;
2237 const uint8_t *palette, *src;
2238 uint32_t content;
2240 if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW))
2241 return;
2242 /* fast test to see if the cursor intersects with the scan line */
2243 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2244 h = 64;
2245 } else {
2246 h = 32;
2248 if (scr_y < s->hw_cursor_y ||
2249 scr_y >= (s->hw_cursor_y + h))
2250 return;
2252 src = s->vga.vram_ptr + s->real_vram_size - 16 * 1024;
2253 if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
2254 src += (s->vga.sr[0x13] & 0x3c) * 256;
2255 src += (scr_y - s->hw_cursor_y) * 16;
2256 poffset = 8;
2257 content = ((uint32_t *)src)[0] |
2258 ((uint32_t *)src)[1] |
2259 ((uint32_t *)src)[2] |
2260 ((uint32_t *)src)[3];
2261 } else {
2262 src += (s->vga.sr[0x13] & 0x3f) * 256;
2263 src += (scr_y - s->hw_cursor_y) * 4;
2264 poffset = 128;
2265 content = ((uint32_t *)src)[0] |
2266 ((uint32_t *)(src + 128))[0];
2268 /* if nothing to draw, no need to continue */
2269 if (!content)
2270 return;
2271 w = h;
2273 x1 = s->hw_cursor_x;
2274 if (x1 >= s->vga.last_scr_width)
2275 return;
2276 x2 = s->hw_cursor_x + w;
2277 if (x2 > s->vga.last_scr_width)
2278 x2 = s->vga.last_scr_width;
2279 w = x2 - x1;
2280 palette = s->cirrus_hidden_palette;
2281 color0 = s->vga.rgb_to_pixel(c6_to_8(palette[0x0 * 3]),
2282 c6_to_8(palette[0x0 * 3 + 1]),
2283 c6_to_8(palette[0x0 * 3 + 2]));
2284 color1 = s->vga.rgb_to_pixel(c6_to_8(palette[0xf * 3]),
2285 c6_to_8(palette[0xf * 3 + 1]),
2286 c6_to_8(palette[0xf * 3 + 2]));
2287 bpp = ((ds_get_bits_per_pixel(s->vga.ds) + 7) >> 3);
2288 d1 += x1 * bpp;
2289 switch(ds_get_bits_per_pixel(s->vga.ds)) {
2290 default:
2291 break;
2292 case 8:
2293 vga_draw_cursor_line_8(d1, src, poffset, w, color0, color1, 0xff);
2294 break;
2295 case 15:
2296 vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0x7fff);
2297 break;
2298 case 16:
2299 vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0xffff);
2300 break;
2301 case 32:
2302 vga_draw_cursor_line_32(d1, src, poffset, w, color0, color1, 0xffffff);
2303 break;
2307 /***************************************
2309 * LFB memory access
2311 ***************************************/
2313 static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2315 CirrusVGAState *s = opaque;
2316 uint32_t ret;
2318 addr &= s->cirrus_addr_mask;
2320 if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
2321 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2322 /* memory-mapped I/O */
2323 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2324 } else if (0) {
2325 /* XXX handle bitblt */
2326 ret = 0xff;
2327 } else {
2328 /* video memory */
2329 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2330 addr <<= 4;
2331 } else if (s->vga.gr[0x0B] & 0x02) {
2332 addr <<= 3;
2334 addr &= s->cirrus_addr_mask;
2335 ret = *(s->vga.vram_ptr + addr);
2338 return ret;
2341 static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2343 uint32_t v;
2344 #ifdef TARGET_WORDS_BIGENDIAN
2345 v = cirrus_linear_readb(opaque, addr) << 8;
2346 v |= cirrus_linear_readb(opaque, addr + 1);
2347 #else
2348 v = cirrus_linear_readb(opaque, addr);
2349 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2350 #endif
2351 return v;
2354 static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2356 uint32_t v;
2357 #ifdef TARGET_WORDS_BIGENDIAN
2358 v = cirrus_linear_readb(opaque, addr) << 24;
2359 v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2360 v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2361 v |= cirrus_linear_readb(opaque, addr + 3);
2362 #else
2363 v = cirrus_linear_readb(opaque, addr);
2364 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2365 v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2366 v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2367 #endif
2368 return v;
2371 static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2372 uint32_t val)
2374 CirrusVGAState *s = opaque;
2375 unsigned mode;
2377 addr &= s->cirrus_addr_mask;
2379 if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
2380 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2381 /* memory-mapped I/O */
2382 cirrus_mmio_blt_write(s, addr & 0xff, val);
2383 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2384 /* bitblt */
2385 *s->cirrus_srcptr++ = (uint8_t) val;
2386 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2387 cirrus_bitblt_cputovideo_next(s);
2389 } else {
2390 /* video memory */
2391 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2392 addr <<= 4;
2393 } else if (s->vga.gr[0x0B] & 0x02) {
2394 addr <<= 3;
2396 addr &= s->cirrus_addr_mask;
2398 mode = s->vga.gr[0x05] & 0x7;
2399 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2400 *(s->vga.vram_ptr + addr) = (uint8_t) val;
2401 cpu_physical_memory_set_dirty(s->vga.vram_offset + addr);
2402 } else {
2403 if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
2404 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2405 } else {
2406 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2412 static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2413 uint32_t val)
2415 #ifdef TARGET_WORDS_BIGENDIAN
2416 cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2417 cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2418 #else
2419 cirrus_linear_writeb(opaque, addr, val & 0xff);
2420 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2421 #endif
2424 static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2425 uint32_t val)
2427 #ifdef TARGET_WORDS_BIGENDIAN
2428 cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2429 cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2430 cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2431 cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2432 #else
2433 cirrus_linear_writeb(opaque, addr, val & 0xff);
2434 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2435 cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2436 cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2437 #endif
2441 static CPUReadMemoryFunc * const cirrus_linear_read[3] = {
2442 cirrus_linear_readb,
2443 cirrus_linear_readw,
2444 cirrus_linear_readl,
2447 static CPUWriteMemoryFunc * const cirrus_linear_write[3] = {
2448 cirrus_linear_writeb,
2449 cirrus_linear_writew,
2450 cirrus_linear_writel,
2453 /***************************************
2455 * system to screen memory access
2457 ***************************************/
2460 static uint32_t cirrus_linear_bitblt_readb(void *opaque, target_phys_addr_t addr)
2462 uint32_t ret;
2464 /* XXX handle bitblt */
2465 ret = 0xff;
2466 return ret;
2469 static uint32_t cirrus_linear_bitblt_readw(void *opaque, target_phys_addr_t addr)
2471 uint32_t v;
2472 #ifdef TARGET_WORDS_BIGENDIAN
2473 v = cirrus_linear_bitblt_readb(opaque, addr) << 8;
2474 v |= cirrus_linear_bitblt_readb(opaque, addr + 1);
2475 #else
2476 v = cirrus_linear_bitblt_readb(opaque, addr);
2477 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2478 #endif
2479 return v;
2482 static uint32_t cirrus_linear_bitblt_readl(void *opaque, target_phys_addr_t addr)
2484 uint32_t v;
2485 #ifdef TARGET_WORDS_BIGENDIAN
2486 v = cirrus_linear_bitblt_readb(opaque, addr) << 24;
2487 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 16;
2488 v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 8;
2489 v |= cirrus_linear_bitblt_readb(opaque, addr + 3);
2490 #else
2491 v = cirrus_linear_bitblt_readb(opaque, addr);
2492 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2493 v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 16;
2494 v |= cirrus_linear_bitblt_readb(opaque, addr + 3) << 24;
2495 #endif
2496 return v;
2499 static void cirrus_linear_bitblt_writeb(void *opaque, target_phys_addr_t addr,
2500 uint32_t val)
2502 CirrusVGAState *s = opaque;
2504 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2505 /* bitblt */
2506 *s->cirrus_srcptr++ = (uint8_t) val;
2507 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2508 cirrus_bitblt_cputovideo_next(s);
2513 static void cirrus_linear_bitblt_writew(void *opaque, target_phys_addr_t addr,
2514 uint32_t val)
2516 #ifdef TARGET_WORDS_BIGENDIAN
2517 cirrus_linear_bitblt_writeb(opaque, addr, (val >> 8) & 0xff);
2518 cirrus_linear_bitblt_writeb(opaque, addr + 1, val & 0xff);
2519 #else
2520 cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2521 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2522 #endif
2525 static void cirrus_linear_bitblt_writel(void *opaque, target_phys_addr_t addr,
2526 uint32_t val)
2528 #ifdef TARGET_WORDS_BIGENDIAN
2529 cirrus_linear_bitblt_writeb(opaque, addr, (val >> 24) & 0xff);
2530 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2531 cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2532 cirrus_linear_bitblt_writeb(opaque, addr + 3, val & 0xff);
2533 #else
2534 cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2535 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2536 cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2537 cirrus_linear_bitblt_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2538 #endif
2542 static CPUReadMemoryFunc * const cirrus_linear_bitblt_read[3] = {
2543 cirrus_linear_bitblt_readb,
2544 cirrus_linear_bitblt_readw,
2545 cirrus_linear_bitblt_readl,
2548 static CPUWriteMemoryFunc * const cirrus_linear_bitblt_write[3] = {
2549 cirrus_linear_bitblt_writeb,
2550 cirrus_linear_bitblt_writew,
2551 cirrus_linear_bitblt_writel,
2554 static void map_linear_vram(CirrusVGAState *s)
2556 vga_dirty_log_stop(&s->vga);
2557 if (!s->vga.map_addr && s->vga.lfb_addr && s->vga.lfb_end) {
2558 s->vga.map_addr = s->vga.lfb_addr;
2559 s->vga.map_end = s->vga.lfb_end;
2560 cpu_register_physical_memory(s->vga.map_addr, s->vga.map_end - s->vga.map_addr, s->vga.vram_offset);
2563 if (!s->vga.map_addr)
2564 return;
2566 #ifndef TARGET_IA64
2567 s->vga.lfb_vram_mapped = 0;
2569 cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x8000,
2570 (s->vga.vram_offset + s->cirrus_bank_base[0]) | IO_MEM_UNASSIGNED);
2571 cpu_register_physical_memory(isa_mem_base + 0xa8000, 0x8000,
2572 (s->vga.vram_offset + s->cirrus_bank_base[1]) | IO_MEM_UNASSIGNED);
2573 if (!(s->cirrus_srcptr != s->cirrus_srcptr_end)
2574 && !((s->vga.sr[0x07] & 0x01) == 0)
2575 && !((s->vga.gr[0x0B] & 0x14) == 0x14)
2576 && !(s->vga.gr[0x0B] & 0x02)) {
2578 vga_dirty_log_stop(&s->vga);
2579 cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x8000,
2580 (s->vga.vram_offset + s->cirrus_bank_base[0]) | IO_MEM_RAM);
2581 cpu_register_physical_memory(isa_mem_base + 0xa8000, 0x8000,
2582 (s->vga.vram_offset + s->cirrus_bank_base[1]) | IO_MEM_RAM);
2584 s->vga.lfb_vram_mapped = 1;
2586 else {
2587 cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x20000,
2588 s->vga.vga_io_memory);
2590 #endif
2592 vga_dirty_log_start(&s->vga);
2595 static void unmap_linear_vram(CirrusVGAState *s)
2597 vga_dirty_log_stop(&s->vga);
2598 if (s->vga.map_addr && s->vga.lfb_addr && s->vga.lfb_end) {
2599 s->vga.map_addr = s->vga.map_end = 0;
2600 cpu_register_physical_memory(s->vga.lfb_addr, s->vga.vram_size,
2601 s->cirrus_linear_io_addr);
2603 cpu_register_physical_memory(isa_mem_base + 0xa0000, 0x20000,
2604 s->vga.vga_io_memory);
2606 vga_dirty_log_start(&s->vga);
2609 /* Compute the memory access functions */
2610 static void cirrus_update_memory_access(CirrusVGAState *s)
2612 unsigned mode;
2614 if ((s->vga.sr[0x17] & 0x44) == 0x44) {
2615 goto generic_io;
2616 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2617 goto generic_io;
2618 } else {
2619 if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
2620 goto generic_io;
2621 } else if (s->vga.gr[0x0B] & 0x02) {
2622 goto generic_io;
2625 mode = s->vga.gr[0x05] & 0x7;
2626 if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
2627 map_linear_vram(s);
2628 } else {
2629 generic_io:
2630 unmap_linear_vram(s);
2636 /* I/O ports */
2638 static uint32_t cirrus_vga_ioport_read(void *opaque, uint32_t addr)
2640 CirrusVGAState *c = opaque;
2641 VGACommonState *s = &c->vga;
2642 int val, index;
2644 if (vga_ioport_invalid(s, addr)) {
2645 val = 0xff;
2646 } else {
2647 switch (addr) {
2648 case 0x3c0:
2649 if (s->ar_flip_flop == 0) {
2650 val = s->ar_index;
2651 } else {
2652 val = 0;
2654 break;
2655 case 0x3c1:
2656 index = s->ar_index & 0x1f;
2657 if (index < 21)
2658 val = s->ar[index];
2659 else
2660 val = 0;
2661 break;
2662 case 0x3c2:
2663 val = s->st00;
2664 break;
2665 case 0x3c4:
2666 val = s->sr_index;
2667 break;
2668 case 0x3c5:
2669 val = cirrus_vga_read_sr(c);
2670 break;
2671 #ifdef DEBUG_VGA_REG
2672 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2673 #endif
2674 break;
2675 case 0x3c6:
2676 val = cirrus_read_hidden_dac(c);
2677 break;
2678 case 0x3c7:
2679 val = s->dac_state;
2680 break;
2681 case 0x3c8:
2682 val = s->dac_write_index;
2683 c->cirrus_hidden_dac_lockindex = 0;
2684 break;
2685 case 0x3c9:
2686 val = cirrus_vga_read_palette(c);
2687 break;
2688 case 0x3ca:
2689 val = s->fcr;
2690 break;
2691 case 0x3cc:
2692 val = s->msr;
2693 break;
2694 case 0x3ce:
2695 val = s->gr_index;
2696 break;
2697 case 0x3cf:
2698 val = cirrus_vga_read_gr(c, s->gr_index);
2699 #ifdef DEBUG_VGA_REG
2700 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2701 #endif
2702 break;
2703 case 0x3b4:
2704 case 0x3d4:
2705 val = s->cr_index;
2706 break;
2707 case 0x3b5:
2708 case 0x3d5:
2709 val = cirrus_vga_read_cr(c, s->cr_index);
2710 #ifdef DEBUG_VGA_REG
2711 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2712 #endif
2713 break;
2714 case 0x3ba:
2715 case 0x3da:
2716 /* just toggle to fool polling */
2717 val = s->st01 = s->retrace(s);
2718 s->ar_flip_flop = 0;
2719 break;
2720 default:
2721 val = 0x00;
2722 break;
2725 #if defined(DEBUG_VGA)
2726 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2727 #endif
2728 return val;
2731 static void cirrus_vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2733 CirrusVGAState *c = opaque;
2734 VGACommonState *s = &c->vga;
2735 int index;
2737 /* check port range access depending on color/monochrome mode */
2738 if (vga_ioport_invalid(s, addr)) {
2739 return;
2741 #ifdef DEBUG_VGA
2742 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2743 #endif
2745 switch (addr) {
2746 case 0x3c0:
2747 if (s->ar_flip_flop == 0) {
2748 val &= 0x3f;
2749 s->ar_index = val;
2750 } else {
2751 index = s->ar_index & 0x1f;
2752 switch (index) {
2753 case 0x00 ... 0x0f:
2754 s->ar[index] = val & 0x3f;
2755 break;
2756 case 0x10:
2757 s->ar[index] = val & ~0x10;
2758 break;
2759 case 0x11:
2760 s->ar[index] = val;
2761 break;
2762 case 0x12:
2763 s->ar[index] = val & ~0xc0;
2764 break;
2765 case 0x13:
2766 s->ar[index] = val & ~0xf0;
2767 break;
2768 case 0x14:
2769 s->ar[index] = val & ~0xf0;
2770 break;
2771 default:
2772 break;
2775 s->ar_flip_flop ^= 1;
2776 break;
2777 case 0x3c2:
2778 s->msr = val & ~0x10;
2779 s->update_retrace_info(s);
2780 break;
2781 case 0x3c4:
2782 s->sr_index = val;
2783 break;
2784 case 0x3c5:
2785 #ifdef DEBUG_VGA_REG
2786 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2787 #endif
2788 cirrus_vga_write_sr(c, val);
2789 break;
2790 break;
2791 case 0x3c6:
2792 cirrus_write_hidden_dac(c, val);
2793 break;
2794 case 0x3c7:
2795 s->dac_read_index = val;
2796 s->dac_sub_index = 0;
2797 s->dac_state = 3;
2798 break;
2799 case 0x3c8:
2800 s->dac_write_index = val;
2801 s->dac_sub_index = 0;
2802 s->dac_state = 0;
2803 break;
2804 case 0x3c9:
2805 cirrus_vga_write_palette(c, val);
2806 break;
2807 case 0x3ce:
2808 s->gr_index = val;
2809 break;
2810 case 0x3cf:
2811 #ifdef DEBUG_VGA_REG
2812 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2813 #endif
2814 cirrus_vga_write_gr(c, s->gr_index, val);
2815 break;
2816 case 0x3b4:
2817 case 0x3d4:
2818 s->cr_index = val;
2819 break;
2820 case 0x3b5:
2821 case 0x3d5:
2822 #ifdef DEBUG_VGA_REG
2823 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2824 #endif
2825 cirrus_vga_write_cr(c, val);
2826 break;
2827 case 0x3ba:
2828 case 0x3da:
2829 s->fcr = val & 0x10;
2830 break;
2834 /***************************************
2836 * memory-mapped I/O access
2838 ***************************************/
2840 static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2842 CirrusVGAState *s = opaque;
2844 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2846 if (addr >= 0x100) {
2847 return cirrus_mmio_blt_read(s, addr - 0x100);
2848 } else {
2849 return cirrus_vga_ioport_read(s, addr + 0x3c0);
2853 static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2855 uint32_t v;
2856 #ifdef TARGET_WORDS_BIGENDIAN
2857 v = cirrus_mmio_readb(opaque, addr) << 8;
2858 v |= cirrus_mmio_readb(opaque, addr + 1);
2859 #else
2860 v = cirrus_mmio_readb(opaque, addr);
2861 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2862 #endif
2863 return v;
2866 static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2868 uint32_t v;
2869 #ifdef TARGET_WORDS_BIGENDIAN
2870 v = cirrus_mmio_readb(opaque, addr) << 24;
2871 v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2872 v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2873 v |= cirrus_mmio_readb(opaque, addr + 3);
2874 #else
2875 v = cirrus_mmio_readb(opaque, addr);
2876 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2877 v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2878 v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2879 #endif
2880 return v;
2883 static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2884 uint32_t val)
2886 CirrusVGAState *s = opaque;
2888 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2890 if (addr >= 0x100) {
2891 cirrus_mmio_blt_write(s, addr - 0x100, val);
2892 } else {
2893 cirrus_vga_ioport_write(s, addr + 0x3c0, val);
2897 static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2898 uint32_t val)
2900 #ifdef TARGET_WORDS_BIGENDIAN
2901 cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2902 cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2903 #else
2904 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2905 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2906 #endif
2909 static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2910 uint32_t val)
2912 #ifdef TARGET_WORDS_BIGENDIAN
2913 cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2914 cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2915 cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2916 cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2917 #else
2918 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2919 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2920 cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2921 cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2922 #endif
2926 static CPUReadMemoryFunc * const cirrus_mmio_read[3] = {
2927 cirrus_mmio_readb,
2928 cirrus_mmio_readw,
2929 cirrus_mmio_readl,
2932 static CPUWriteMemoryFunc * const cirrus_mmio_write[3] = {
2933 cirrus_mmio_writeb,
2934 cirrus_mmio_writew,
2935 cirrus_mmio_writel,
2938 /* load/save state */
2940 static int cirrus_post_load(void *opaque, int version_id)
2942 CirrusVGAState *s = opaque;
2944 s->vga.gr[0x00] = s->cirrus_shadow_gr0 & 0x0f;
2945 s->vga.gr[0x01] = s->cirrus_shadow_gr1 & 0x0f;
2947 cirrus_update_memory_access(s);
2948 /* force refresh */
2949 s->vga.graphic_mode = -1;
2950 cirrus_update_bank_ptr(s, 0);
2951 cirrus_update_bank_ptr(s, 1);
2952 return 0;
2955 static const VMStateDescription vmstate_cirrus_vga = {
2956 .name = "cirrus_vga",
2957 .version_id = 2,
2958 .minimum_version_id = 1,
2959 .minimum_version_id_old = 1,
2960 .post_load = cirrus_post_load,
2961 .fields = (VMStateField []) {
2962 VMSTATE_UINT32(vga.latch, CirrusVGAState),
2963 VMSTATE_UINT8(vga.sr_index, CirrusVGAState),
2964 VMSTATE_BUFFER(vga.sr, CirrusVGAState),
2965 VMSTATE_UINT8(vga.gr_index, CirrusVGAState),
2966 VMSTATE_UINT8(cirrus_shadow_gr0, CirrusVGAState),
2967 VMSTATE_UINT8(cirrus_shadow_gr1, CirrusVGAState),
2968 VMSTATE_BUFFER_START_MIDDLE(vga.gr, CirrusVGAState, 2),
2969 VMSTATE_UINT8(vga.ar_index, CirrusVGAState),
2970 VMSTATE_BUFFER(vga.ar, CirrusVGAState),
2971 VMSTATE_INT32(vga.ar_flip_flop, CirrusVGAState),
2972 VMSTATE_UINT8(vga.cr_index, CirrusVGAState),
2973 VMSTATE_BUFFER(vga.cr, CirrusVGAState),
2974 VMSTATE_UINT8(vga.msr, CirrusVGAState),
2975 VMSTATE_UINT8(vga.fcr, CirrusVGAState),
2976 VMSTATE_UINT8(vga.st00, CirrusVGAState),
2977 VMSTATE_UINT8(vga.st01, CirrusVGAState),
2978 VMSTATE_UINT8(vga.dac_state, CirrusVGAState),
2979 VMSTATE_UINT8(vga.dac_sub_index, CirrusVGAState),
2980 VMSTATE_UINT8(vga.dac_read_index, CirrusVGAState),
2981 VMSTATE_UINT8(vga.dac_write_index, CirrusVGAState),
2982 VMSTATE_BUFFER(vga.dac_cache, CirrusVGAState),
2983 VMSTATE_BUFFER(vga.palette, CirrusVGAState),
2984 VMSTATE_INT32(vga.bank_offset, CirrusVGAState),
2985 VMSTATE_UINT8(cirrus_hidden_dac_lockindex, CirrusVGAState),
2986 VMSTATE_UINT8(cirrus_hidden_dac_data, CirrusVGAState),
2987 VMSTATE_UINT32(hw_cursor_x, CirrusVGAState),
2988 VMSTATE_UINT32(hw_cursor_y, CirrusVGAState),
2989 /* XXX: we do not save the bitblt state - we assume we do not save
2990 the state when the blitter is active */
2991 VMSTATE_END_OF_LIST()
2995 static const VMStateDescription vmstate_pci_cirrus_vga = {
2996 .name = "cirrus_vga",
2997 .version_id = 2,
2998 .minimum_version_id = 2,
2999 .minimum_version_id_old = 2,
3000 .fields = (VMStateField []) {
3001 VMSTATE_PCI_DEVICE(dev, PCICirrusVGAState),
3002 VMSTATE_STRUCT(cirrus_vga, PCICirrusVGAState, 0,
3003 vmstate_cirrus_vga, CirrusVGAState),
3004 VMSTATE_END_OF_LIST()
3008 /***************************************
3010 * initialize
3012 ***************************************/
3014 static void cirrus_reset(void *opaque)
3016 CirrusVGAState *s = opaque;
3018 vga_common_reset(&s->vga);
3019 unmap_linear_vram(s);
3020 s->vga.sr[0x06] = 0x0f;
3021 if (s->device_id == CIRRUS_ID_CLGD5446) {
3022 /* 4MB 64 bit memory config, always PCI */
3023 s->vga.sr[0x1F] = 0x2d; // MemClock
3024 s->vga.gr[0x18] = 0x0f; // fastest memory configuration
3025 s->vga.sr[0x0f] = 0x98;
3026 s->vga.sr[0x17] = 0x20;
3027 s->vga.sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */
3028 } else {
3029 s->vga.sr[0x1F] = 0x22; // MemClock
3030 s->vga.sr[0x0F] = CIRRUS_MEMSIZE_2M;
3031 s->vga.sr[0x17] = s->bustype;
3032 s->vga.sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
3034 s->vga.cr[0x27] = s->device_id;
3036 /* Win2K seems to assume that the pattern buffer is at 0xff
3037 initially ! */
3038 memset(s->vga.vram_ptr, 0xff, s->real_vram_size);
3040 s->cirrus_hidden_dac_lockindex = 5;
3041 s->cirrus_hidden_dac_data = 0;
3044 static void cirrus_init_common(CirrusVGAState * s, int device_id, int is_pci)
3046 int i;
3047 static int inited;
3049 if (!inited) {
3050 inited = 1;
3051 for(i = 0;i < 256; i++)
3052 rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */
3053 rop_to_index[CIRRUS_ROP_0] = 0;
3054 rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;
3055 rop_to_index[CIRRUS_ROP_NOP] = 2;
3056 rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;
3057 rop_to_index[CIRRUS_ROP_NOTDST] = 4;
3058 rop_to_index[CIRRUS_ROP_SRC] = 5;
3059 rop_to_index[CIRRUS_ROP_1] = 6;
3060 rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;
3061 rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;
3062 rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;
3063 rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;
3064 rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;
3065 rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;
3066 rop_to_index[CIRRUS_ROP_NOTSRC] = 13;
3067 rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;
3068 rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;
3069 s->device_id = device_id;
3070 if (is_pci)
3071 s->bustype = CIRRUS_BUSTYPE_PCI;
3072 else
3073 s->bustype = CIRRUS_BUSTYPE_ISA;
3076 register_ioport_write(0x3c0, 16, 1, cirrus_vga_ioport_write, s);
3078 register_ioport_write(0x3b4, 2, 1, cirrus_vga_ioport_write, s);
3079 register_ioport_write(0x3d4, 2, 1, cirrus_vga_ioport_write, s);
3080 register_ioport_write(0x3ba, 1, 1, cirrus_vga_ioport_write, s);
3081 register_ioport_write(0x3da, 1, 1, cirrus_vga_ioport_write, s);
3083 register_ioport_read(0x3c0, 16, 1, cirrus_vga_ioport_read, s);
3085 register_ioport_read(0x3b4, 2, 1, cirrus_vga_ioport_read, s);
3086 register_ioport_read(0x3d4, 2, 1, cirrus_vga_ioport_read, s);
3087 register_ioport_read(0x3ba, 1, 1, cirrus_vga_ioport_read, s);
3088 register_ioport_read(0x3da, 1, 1, cirrus_vga_ioport_read, s);
3090 s->vga.vga_io_memory = cpu_register_io_memory(cirrus_vga_mem_read,
3091 cirrus_vga_mem_write, s);
3092 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
3093 s->vga.vga_io_memory);
3094 qemu_register_coalesced_mmio(isa_mem_base + 0x000a0000, 0x20000);
3096 /* I/O handler for LFB */
3097 s->cirrus_linear_io_addr =
3098 cpu_register_io_memory(cirrus_linear_read, cirrus_linear_write, s);
3100 /* I/O handler for LFB */
3101 s->cirrus_linear_bitblt_io_addr =
3102 cpu_register_io_memory(cirrus_linear_bitblt_read,
3103 cirrus_linear_bitblt_write, s);
3105 /* I/O handler for memory-mapped I/O */
3106 s->cirrus_mmio_io_addr =
3107 cpu_register_io_memory(cirrus_mmio_read, cirrus_mmio_write, s);
3109 s->real_vram_size =
3110 (s->device_id == CIRRUS_ID_CLGD5446) ? 4096 * 1024 : 2048 * 1024;
3112 /* XXX: s->vga.vram_size must be a power of two */
3113 s->cirrus_addr_mask = s->real_vram_size - 1;
3114 s->linear_mmio_mask = s->real_vram_size - 256;
3116 s->vga.get_bpp = cirrus_get_bpp;
3117 s->vga.get_offsets = cirrus_get_offsets;
3118 s->vga.get_resolution = cirrus_get_resolution;
3119 s->vga.cursor_invalidate = cirrus_cursor_invalidate;
3120 s->vga.cursor_draw_line = cirrus_cursor_draw_line;
3122 qemu_register_reset(cirrus_reset, s);
3123 cirrus_reset(s);
3126 /***************************************
3128 * ISA bus support
3130 ***************************************/
3132 void isa_cirrus_vga_init(void)
3134 CirrusVGAState *s;
3136 s = qemu_mallocz(sizeof(CirrusVGAState));
3138 vga_common_init(&s->vga, VGA_RAM_SIZE);
3139 cirrus_init_common(s, CIRRUS_ID_CLGD5430, 0);
3140 s->vga.ds = graphic_console_init(s->vga.update, s->vga.invalidate,
3141 s->vga.screen_dump, s->vga.text_update,
3142 &s->vga);
3143 vmstate_register(0, &vmstate_cirrus_vga, s);
3144 rom_add_vga(VGABIOS_CIRRUS_FILENAME);
3145 /* XXX ISA-LFB support */
3148 /***************************************
3150 * PCI bus support
3152 ***************************************/
3154 static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
3155 pcibus_t addr, pcibus_t size, int type)
3157 CirrusVGAState *s = &DO_UPCAST(PCICirrusVGAState, dev, d)->cirrus_vga;
3159 vga_dirty_log_stop(&s->vga);
3161 /* XXX: add byte swapping apertures */
3162 cpu_register_physical_memory(addr, s->vga.vram_size,
3163 s->cirrus_linear_io_addr);
3164 cpu_register_physical_memory(addr + 0x1000000, 0x400000,
3165 s->cirrus_linear_bitblt_io_addr);
3167 s->vga.map_addr = s->vga.map_end = 0;
3168 s->vga.lfb_addr = addr & TARGET_PAGE_MASK;
3169 s->vga.lfb_end = ((addr + VGA_RAM_SIZE) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
3170 /* account for overflow */
3171 if (s->vga.lfb_end < addr + VGA_RAM_SIZE)
3172 s->vga.lfb_end = addr + VGA_RAM_SIZE;
3174 vga_dirty_log_start(&s->vga);
3177 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
3178 pcibus_t addr, pcibus_t size, int type)
3180 CirrusVGAState *s = &DO_UPCAST(PCICirrusVGAState, dev, d)->cirrus_vga;
3182 cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
3183 s->cirrus_mmio_io_addr);
3186 static void pci_cirrus_write_config(PCIDevice *d,
3187 uint32_t address, uint32_t val, int len)
3189 PCICirrusVGAState *pvs = DO_UPCAST(PCICirrusVGAState, dev, d);
3190 CirrusVGAState *s = &pvs->cirrus_vga;
3192 vga_dirty_log_stop(&s->vga);
3194 pci_default_write_config(d, address, val, len);
3195 if (s->vga.map_addr && d->io_regions[0].addr == PCI_BAR_UNMAPPED)
3196 s->vga.map_addr = 0;
3197 cirrus_update_memory_access(s);
3199 vga_dirty_log_start(&s->vga);
3202 static int pci_cirrus_vga_initfn(PCIDevice *dev)
3204 PCICirrusVGAState *d = DO_UPCAST(PCICirrusVGAState, dev, dev);
3205 CirrusVGAState *s = &d->cirrus_vga;
3206 uint8_t *pci_conf = d->dev.config;
3207 int device_id = CIRRUS_ID_CLGD5446;
3209 /* setup VGA */
3210 vga_common_init(&s->vga, VGA_RAM_SIZE);
3211 cirrus_init_common(s, device_id, 1);
3212 s->vga.ds = graphic_console_init(s->vga.update, s->vga.invalidate,
3213 s->vga.screen_dump, s->vga.text_update,
3214 &s->vga);
3216 /* setup PCI */
3217 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_CIRRUS);
3218 pci_config_set_device_id(pci_conf, device_id);
3219 pci_config_set_class(pci_conf, PCI_CLASS_DISPLAY_VGA);
3221 /* setup memory space */
3222 /* memory #0 LFB */
3223 /* memory #1 memory-mapped I/O */
3224 /* XXX: s->vga.vram_size must be a power of two */
3225 pci_register_bar((PCIDevice *)d, 0, 0x2000000,
3226 PCI_BASE_ADDRESS_MEM_PREFETCH, cirrus_pci_lfb_map);
3227 if (device_id == CIRRUS_ID_CLGD5446) {
3228 pci_register_bar((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
3229 PCI_BASE_ADDRESS_SPACE_MEMORY, cirrus_pci_mmio_map);
3231 return 0;
3234 void pci_cirrus_vga_init(PCIBus *bus)
3236 pci_create_simple(bus, -1, "cirrus-vga");
3239 static PCIDeviceInfo cirrus_vga_info = {
3240 .qdev.name = "cirrus-vga",
3241 .qdev.desc = "Cirrus CLGD 54xx VGA",
3242 .qdev.size = sizeof(PCICirrusVGAState),
3243 .qdev.vmsd = &vmstate_pci_cirrus_vga,
3244 .init = pci_cirrus_vga_initfn,
3245 .romfile = VGABIOS_CIRRUS_FILENAME,
3246 .config_write = pci_cirrus_write_config,
3249 static void cirrus_vga_register(void)
3251 pci_qdev_register(&cirrus_vga_info);
3253 device_init(cirrus_vga_register);