move vga_io_address to VGA State (Glauber Costa)
[qemu/qemu-JZ.git] / hw / cirrus_vga.c
blob01f3b6a631b52ea9761ede424030932c73f0f844
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"
36 * TODO:
37 * - destination write mask support not complete (bits 5..7)
38 * - optimize linear mappings
39 * - optimize bitblt functions
42 //#define DEBUG_CIRRUS
43 //#define DEBUG_BITBLT
45 /***************************************
47 * definitions
49 ***************************************/
51 #define qemu_MIN(a,b) ((a) < (b) ? (a) : (b))
53 // ID
54 #define CIRRUS_ID_CLGD5422 (0x23<<2)
55 #define CIRRUS_ID_CLGD5426 (0x24<<2)
56 #define CIRRUS_ID_CLGD5424 (0x25<<2)
57 #define CIRRUS_ID_CLGD5428 (0x26<<2)
58 #define CIRRUS_ID_CLGD5430 (0x28<<2)
59 #define CIRRUS_ID_CLGD5434 (0x2A<<2)
60 #define CIRRUS_ID_CLGD5436 (0x2B<<2)
61 #define CIRRUS_ID_CLGD5446 (0x2E<<2)
63 // sequencer 0x07
64 #define CIRRUS_SR7_BPP_VGA 0x00
65 #define CIRRUS_SR7_BPP_SVGA 0x01
66 #define CIRRUS_SR7_BPP_MASK 0x0e
67 #define CIRRUS_SR7_BPP_8 0x00
68 #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
69 #define CIRRUS_SR7_BPP_24 0x04
70 #define CIRRUS_SR7_BPP_16 0x06
71 #define CIRRUS_SR7_BPP_32 0x08
72 #define CIRRUS_SR7_ISAADDR_MASK 0xe0
74 // sequencer 0x0f
75 #define CIRRUS_MEMSIZE_512k 0x08
76 #define CIRRUS_MEMSIZE_1M 0x10
77 #define CIRRUS_MEMSIZE_2M 0x18
78 #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
80 // sequencer 0x12
81 #define CIRRUS_CURSOR_SHOW 0x01
82 #define CIRRUS_CURSOR_HIDDENPEL 0x02
83 #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
85 // sequencer 0x17
86 #define CIRRUS_BUSTYPE_VLBFAST 0x10
87 #define CIRRUS_BUSTYPE_PCI 0x20
88 #define CIRRUS_BUSTYPE_VLBSLOW 0x30
89 #define CIRRUS_BUSTYPE_ISA 0x38
90 #define CIRRUS_MMIO_ENABLE 0x04
91 #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
92 #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
94 // control 0x0b
95 #define CIRRUS_BANKING_DUAL 0x01
96 #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
98 // control 0x30
99 #define CIRRUS_BLTMODE_BACKWARDS 0x01
100 #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
101 #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
102 #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
103 #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
104 #define CIRRUS_BLTMODE_COLOREXPAND 0x80
105 #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
106 #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
107 #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
108 #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
109 #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
111 // control 0x31
112 #define CIRRUS_BLT_BUSY 0x01
113 #define CIRRUS_BLT_START 0x02
114 #define CIRRUS_BLT_RESET 0x04
115 #define CIRRUS_BLT_FIFOUSED 0x10
116 #define CIRRUS_BLT_AUTOSTART 0x80
118 // control 0x32
119 #define CIRRUS_ROP_0 0x00
120 #define CIRRUS_ROP_SRC_AND_DST 0x05
121 #define CIRRUS_ROP_NOP 0x06
122 #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
123 #define CIRRUS_ROP_NOTDST 0x0b
124 #define CIRRUS_ROP_SRC 0x0d
125 #define CIRRUS_ROP_1 0x0e
126 #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
127 #define CIRRUS_ROP_SRC_XOR_DST 0x59
128 #define CIRRUS_ROP_SRC_OR_DST 0x6d
129 #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
130 #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
131 #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
132 #define CIRRUS_ROP_NOTSRC 0xd0
133 #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
134 #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
136 #define CIRRUS_ROP_NOP_INDEX 2
137 #define CIRRUS_ROP_SRC_INDEX 5
139 // control 0x33
140 #define CIRRUS_BLTMODEEXT_SOLIDFILL 0x04
141 #define CIRRUS_BLTMODEEXT_COLOREXPINV 0x02
142 #define CIRRUS_BLTMODEEXT_DWORDGRANULARITY 0x01
144 // memory-mapped IO
145 #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
146 #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
147 #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
148 #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
149 #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
150 #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
151 #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
152 #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
153 #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
154 #define CIRRUS_MMIO_BLTMODE 0x18 // byte
155 #define CIRRUS_MMIO_BLTROP 0x1a // byte
156 #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
157 #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
158 #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
159 #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
160 #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
161 #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
162 #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
163 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
164 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
165 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
166 #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
167 #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
168 #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
169 #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
170 #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
171 #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
172 #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
173 #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
175 // PCI 0x00: vendor, 0x02: device
176 #define PCI_VENDOR_CIRRUS 0x1013
177 #define PCI_DEVICE_CLGD5462 0x00d0
178 #define PCI_DEVICE_CLGD5465 0x00d6
180 // PCI 0x04: command(word), 0x06(word): status
181 #define PCI_COMMAND_IOACCESS 0x0001
182 #define PCI_COMMAND_MEMACCESS 0x0002
183 #define PCI_COMMAND_BUSMASTER 0x0004
184 #define PCI_COMMAND_SPECIALCYCLE 0x0008
185 #define PCI_COMMAND_MEMWRITEINVALID 0x0010
186 #define PCI_COMMAND_PALETTESNOOPING 0x0020
187 #define PCI_COMMAND_PARITYDETECTION 0x0040
188 #define PCI_COMMAND_ADDRESSDATASTEPPING 0x0080
189 #define PCI_COMMAND_SERR 0x0100
190 #define PCI_COMMAND_BACKTOBACKTRANS 0x0200
191 // PCI 0x08, 0xff000000 (0x09-0x0b:class,0x08:rev)
192 #define PCI_CLASS_BASE_DISPLAY 0x03
193 // PCI 0x08, 0x00ff0000
194 #define PCI_CLASS_SUB_VGA 0x00
195 // PCI 0x0c, 0x00ff0000 (0x0c:cacheline,0x0d:latency,0x0e:headertype,0x0f:Built-in self test)
196 #define PCI_CLASS_HEADERTYPE_00h 0x00
197 // 0x10-0x3f (headertype 00h)
198 // PCI 0x10,0x14,0x18,0x1c,0x20,0x24: base address mapping registers
199 // 0x10: MEMBASE, 0x14: IOBASE(hard-coded in XFree86 3.x)
200 #define PCI_MAP_MEM 0x0
201 #define PCI_MAP_IO 0x1
202 #define PCI_MAP_MEM_ADDR_MASK (~0xf)
203 #define PCI_MAP_IO_ADDR_MASK (~0x3)
204 #define PCI_MAP_MEMFLAGS_32BIT 0x0
205 #define PCI_MAP_MEMFLAGS_32BIT_1M 0x1
206 #define PCI_MAP_MEMFLAGS_64BIT 0x4
207 #define PCI_MAP_MEMFLAGS_CACHEABLE 0x8
208 // PCI 0x28: cardbus CIS pointer
209 // PCI 0x2c: subsystem vendor id, 0x2e: subsystem id
210 // PCI 0x30: expansion ROM base address
211 #define PCI_ROMBIOS_ENABLED 0x1
212 // PCI 0x34: 0xffffff00=reserved, 0x000000ff=capabilities pointer
213 // PCI 0x38: reserved
214 // PCI 0x3c: 0x3c=int-line, 0x3d=int-pin, 0x3e=min-gnt, 0x3f=maax-lat
216 #define CIRRUS_PNPMMIO_SIZE 0x1000
219 /* I/O and memory hook */
220 #define CIRRUS_HOOK_NOT_HANDLED 0
221 #define CIRRUS_HOOK_HANDLED 1
223 #define BLTUNSAFE(s) \
225 ( /* check dst is within bounds */ \
226 (s)->cirrus_blt_height * (s)->cirrus_blt_dstpitch \
227 + ((s)->cirrus_blt_dstaddr & (s)->cirrus_addr_mask) > \
228 (s)->vram_size \
229 ) || \
230 ( /* check src is within bounds */ \
231 (s)->cirrus_blt_height * (s)->cirrus_blt_srcpitch \
232 + ((s)->cirrus_blt_srcaddr & (s)->cirrus_addr_mask) > \
233 (s)->vram_size \
237 struct CirrusVGAState;
238 typedef void (*cirrus_bitblt_rop_t) (struct CirrusVGAState *s,
239 uint8_t * dst, const uint8_t * src,
240 int dstpitch, int srcpitch,
241 int bltwidth, int bltheight);
242 typedef void (*cirrus_fill_t)(struct CirrusVGAState *s,
243 uint8_t *dst, int dst_pitch, int width, int height);
245 typedef struct CirrusVGAState {
246 VGA_STATE_COMMON
248 int cirrus_linear_io_addr;
249 int cirrus_linear_bitblt_io_addr;
250 int cirrus_mmio_io_addr;
251 uint32_t cirrus_addr_mask;
252 uint32_t linear_mmio_mask;
253 uint8_t cirrus_shadow_gr0;
254 uint8_t cirrus_shadow_gr1;
255 uint8_t cirrus_hidden_dac_lockindex;
256 uint8_t cirrus_hidden_dac_data;
257 uint32_t cirrus_bank_base[2];
258 uint32_t cirrus_bank_limit[2];
259 uint8_t cirrus_hidden_palette[48];
260 uint32_t hw_cursor_x;
261 uint32_t hw_cursor_y;
262 int cirrus_blt_pixelwidth;
263 int cirrus_blt_width;
264 int cirrus_blt_height;
265 int cirrus_blt_dstpitch;
266 int cirrus_blt_srcpitch;
267 uint32_t cirrus_blt_fgcol;
268 uint32_t cirrus_blt_bgcol;
269 uint32_t cirrus_blt_dstaddr;
270 uint32_t cirrus_blt_srcaddr;
271 uint8_t cirrus_blt_mode;
272 uint8_t cirrus_blt_modeext;
273 cirrus_bitblt_rop_t cirrus_rop;
274 #define CIRRUS_BLTBUFSIZE (2048 * 4) /* one line width */
275 uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
276 uint8_t *cirrus_srcptr;
277 uint8_t *cirrus_srcptr_end;
278 uint32_t cirrus_srccounter;
279 /* hwcursor display state */
280 int last_hw_cursor_size;
281 int last_hw_cursor_x;
282 int last_hw_cursor_y;
283 int last_hw_cursor_y_start;
284 int last_hw_cursor_y_end;
285 int real_vram_size; /* XXX: suppress that */
286 CPUWriteMemoryFunc **cirrus_linear_write;
287 } CirrusVGAState;
289 typedef struct PCICirrusVGAState {
290 PCIDevice dev;
291 CirrusVGAState cirrus_vga;
292 } PCICirrusVGAState;
294 static uint8_t rop_to_index[256];
296 /***************************************
298 * prototypes.
300 ***************************************/
303 static void cirrus_bitblt_reset(CirrusVGAState *s);
304 static void cirrus_update_memory_access(CirrusVGAState *s);
306 /***************************************
308 * raster operations
310 ***************************************/
312 static void cirrus_bitblt_rop_nop(CirrusVGAState *s,
313 uint8_t *dst,const uint8_t *src,
314 int dstpitch,int srcpitch,
315 int bltwidth,int bltheight)
319 static void cirrus_bitblt_fill_nop(CirrusVGAState *s,
320 uint8_t *dst,
321 int dstpitch, int bltwidth,int bltheight)
325 #define ROP_NAME 0
326 #define ROP_OP(d, s) d = 0
327 #include "cirrus_vga_rop.h"
329 #define ROP_NAME src_and_dst
330 #define ROP_OP(d, s) d = (s) & (d)
331 #include "cirrus_vga_rop.h"
333 #define ROP_NAME src_and_notdst
334 #define ROP_OP(d, s) d = (s) & (~(d))
335 #include "cirrus_vga_rop.h"
337 #define ROP_NAME notdst
338 #define ROP_OP(d, s) d = ~(d)
339 #include "cirrus_vga_rop.h"
341 #define ROP_NAME src
342 #define ROP_OP(d, s) d = s
343 #include "cirrus_vga_rop.h"
345 #define ROP_NAME 1
346 #define ROP_OP(d, s) d = ~0
347 #include "cirrus_vga_rop.h"
349 #define ROP_NAME notsrc_and_dst
350 #define ROP_OP(d, s) d = (~(s)) & (d)
351 #include "cirrus_vga_rop.h"
353 #define ROP_NAME src_xor_dst
354 #define ROP_OP(d, s) d = (s) ^ (d)
355 #include "cirrus_vga_rop.h"
357 #define ROP_NAME src_or_dst
358 #define ROP_OP(d, s) d = (s) | (d)
359 #include "cirrus_vga_rop.h"
361 #define ROP_NAME notsrc_or_notdst
362 #define ROP_OP(d, s) d = (~(s)) | (~(d))
363 #include "cirrus_vga_rop.h"
365 #define ROP_NAME src_notxor_dst
366 #define ROP_OP(d, s) d = ~((s) ^ (d))
367 #include "cirrus_vga_rop.h"
369 #define ROP_NAME src_or_notdst
370 #define ROP_OP(d, s) d = (s) | (~(d))
371 #include "cirrus_vga_rop.h"
373 #define ROP_NAME notsrc
374 #define ROP_OP(d, s) d = (~(s))
375 #include "cirrus_vga_rop.h"
377 #define ROP_NAME notsrc_or_dst
378 #define ROP_OP(d, s) d = (~(s)) | (d)
379 #include "cirrus_vga_rop.h"
381 #define ROP_NAME notsrc_and_notdst
382 #define ROP_OP(d, s) d = (~(s)) & (~(d))
383 #include "cirrus_vga_rop.h"
385 static const cirrus_bitblt_rop_t cirrus_fwd_rop[16] = {
386 cirrus_bitblt_rop_fwd_0,
387 cirrus_bitblt_rop_fwd_src_and_dst,
388 cirrus_bitblt_rop_nop,
389 cirrus_bitblt_rop_fwd_src_and_notdst,
390 cirrus_bitblt_rop_fwd_notdst,
391 cirrus_bitblt_rop_fwd_src,
392 cirrus_bitblt_rop_fwd_1,
393 cirrus_bitblt_rop_fwd_notsrc_and_dst,
394 cirrus_bitblt_rop_fwd_src_xor_dst,
395 cirrus_bitblt_rop_fwd_src_or_dst,
396 cirrus_bitblt_rop_fwd_notsrc_or_notdst,
397 cirrus_bitblt_rop_fwd_src_notxor_dst,
398 cirrus_bitblt_rop_fwd_src_or_notdst,
399 cirrus_bitblt_rop_fwd_notsrc,
400 cirrus_bitblt_rop_fwd_notsrc_or_dst,
401 cirrus_bitblt_rop_fwd_notsrc_and_notdst,
404 static const cirrus_bitblt_rop_t cirrus_bkwd_rop[16] = {
405 cirrus_bitblt_rop_bkwd_0,
406 cirrus_bitblt_rop_bkwd_src_and_dst,
407 cirrus_bitblt_rop_nop,
408 cirrus_bitblt_rop_bkwd_src_and_notdst,
409 cirrus_bitblt_rop_bkwd_notdst,
410 cirrus_bitblt_rop_bkwd_src,
411 cirrus_bitblt_rop_bkwd_1,
412 cirrus_bitblt_rop_bkwd_notsrc_and_dst,
413 cirrus_bitblt_rop_bkwd_src_xor_dst,
414 cirrus_bitblt_rop_bkwd_src_or_dst,
415 cirrus_bitblt_rop_bkwd_notsrc_or_notdst,
416 cirrus_bitblt_rop_bkwd_src_notxor_dst,
417 cirrus_bitblt_rop_bkwd_src_or_notdst,
418 cirrus_bitblt_rop_bkwd_notsrc,
419 cirrus_bitblt_rop_bkwd_notsrc_or_dst,
420 cirrus_bitblt_rop_bkwd_notsrc_and_notdst,
423 #define TRANSP_ROP(name) {\
424 name ## _8,\
425 name ## _16,\
427 #define TRANSP_NOP(func) {\
428 func,\
429 func,\
432 static const cirrus_bitblt_rop_t cirrus_fwd_transp_rop[16][2] = {
433 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_0),
434 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_dst),
435 TRANSP_NOP(cirrus_bitblt_rop_nop),
436 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_notdst),
437 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notdst),
438 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src),
439 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_1),
440 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_dst),
441 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_xor_dst),
442 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_dst),
443 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_notdst),
444 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_notxor_dst),
445 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_notdst),
446 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc),
447 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_dst),
448 TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_notdst),
451 static const cirrus_bitblt_rop_t cirrus_bkwd_transp_rop[16][2] = {
452 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_0),
453 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_dst),
454 TRANSP_NOP(cirrus_bitblt_rop_nop),
455 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_notdst),
456 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notdst),
457 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src),
458 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_1),
459 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_dst),
460 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_xor_dst),
461 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_dst),
462 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_notdst),
463 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_notxor_dst),
464 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_notdst),
465 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc),
466 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_dst),
467 TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_notdst),
470 #define ROP2(name) {\
471 name ## _8,\
472 name ## _16,\
473 name ## _24,\
474 name ## _32,\
477 #define ROP_NOP2(func) {\
478 func,\
479 func,\
480 func,\
481 func,\
484 static const cirrus_bitblt_rop_t cirrus_patternfill[16][4] = {
485 ROP2(cirrus_patternfill_0),
486 ROP2(cirrus_patternfill_src_and_dst),
487 ROP_NOP2(cirrus_bitblt_rop_nop),
488 ROP2(cirrus_patternfill_src_and_notdst),
489 ROP2(cirrus_patternfill_notdst),
490 ROP2(cirrus_patternfill_src),
491 ROP2(cirrus_patternfill_1),
492 ROP2(cirrus_patternfill_notsrc_and_dst),
493 ROP2(cirrus_patternfill_src_xor_dst),
494 ROP2(cirrus_patternfill_src_or_dst),
495 ROP2(cirrus_patternfill_notsrc_or_notdst),
496 ROP2(cirrus_patternfill_src_notxor_dst),
497 ROP2(cirrus_patternfill_src_or_notdst),
498 ROP2(cirrus_patternfill_notsrc),
499 ROP2(cirrus_patternfill_notsrc_or_dst),
500 ROP2(cirrus_patternfill_notsrc_and_notdst),
503 static const cirrus_bitblt_rop_t cirrus_colorexpand_transp[16][4] = {
504 ROP2(cirrus_colorexpand_transp_0),
505 ROP2(cirrus_colorexpand_transp_src_and_dst),
506 ROP_NOP2(cirrus_bitblt_rop_nop),
507 ROP2(cirrus_colorexpand_transp_src_and_notdst),
508 ROP2(cirrus_colorexpand_transp_notdst),
509 ROP2(cirrus_colorexpand_transp_src),
510 ROP2(cirrus_colorexpand_transp_1),
511 ROP2(cirrus_colorexpand_transp_notsrc_and_dst),
512 ROP2(cirrus_colorexpand_transp_src_xor_dst),
513 ROP2(cirrus_colorexpand_transp_src_or_dst),
514 ROP2(cirrus_colorexpand_transp_notsrc_or_notdst),
515 ROP2(cirrus_colorexpand_transp_src_notxor_dst),
516 ROP2(cirrus_colorexpand_transp_src_or_notdst),
517 ROP2(cirrus_colorexpand_transp_notsrc),
518 ROP2(cirrus_colorexpand_transp_notsrc_or_dst),
519 ROP2(cirrus_colorexpand_transp_notsrc_and_notdst),
522 static const cirrus_bitblt_rop_t cirrus_colorexpand[16][4] = {
523 ROP2(cirrus_colorexpand_0),
524 ROP2(cirrus_colorexpand_src_and_dst),
525 ROP_NOP2(cirrus_bitblt_rop_nop),
526 ROP2(cirrus_colorexpand_src_and_notdst),
527 ROP2(cirrus_colorexpand_notdst),
528 ROP2(cirrus_colorexpand_src),
529 ROP2(cirrus_colorexpand_1),
530 ROP2(cirrus_colorexpand_notsrc_and_dst),
531 ROP2(cirrus_colorexpand_src_xor_dst),
532 ROP2(cirrus_colorexpand_src_or_dst),
533 ROP2(cirrus_colorexpand_notsrc_or_notdst),
534 ROP2(cirrus_colorexpand_src_notxor_dst),
535 ROP2(cirrus_colorexpand_src_or_notdst),
536 ROP2(cirrus_colorexpand_notsrc),
537 ROP2(cirrus_colorexpand_notsrc_or_dst),
538 ROP2(cirrus_colorexpand_notsrc_and_notdst),
541 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern_transp[16][4] = {
542 ROP2(cirrus_colorexpand_pattern_transp_0),
543 ROP2(cirrus_colorexpand_pattern_transp_src_and_dst),
544 ROP_NOP2(cirrus_bitblt_rop_nop),
545 ROP2(cirrus_colorexpand_pattern_transp_src_and_notdst),
546 ROP2(cirrus_colorexpand_pattern_transp_notdst),
547 ROP2(cirrus_colorexpand_pattern_transp_src),
548 ROP2(cirrus_colorexpand_pattern_transp_1),
549 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_dst),
550 ROP2(cirrus_colorexpand_pattern_transp_src_xor_dst),
551 ROP2(cirrus_colorexpand_pattern_transp_src_or_dst),
552 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_notdst),
553 ROP2(cirrus_colorexpand_pattern_transp_src_notxor_dst),
554 ROP2(cirrus_colorexpand_pattern_transp_src_or_notdst),
555 ROP2(cirrus_colorexpand_pattern_transp_notsrc),
556 ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_dst),
557 ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_notdst),
560 static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern[16][4] = {
561 ROP2(cirrus_colorexpand_pattern_0),
562 ROP2(cirrus_colorexpand_pattern_src_and_dst),
563 ROP_NOP2(cirrus_bitblt_rop_nop),
564 ROP2(cirrus_colorexpand_pattern_src_and_notdst),
565 ROP2(cirrus_colorexpand_pattern_notdst),
566 ROP2(cirrus_colorexpand_pattern_src),
567 ROP2(cirrus_colorexpand_pattern_1),
568 ROP2(cirrus_colorexpand_pattern_notsrc_and_dst),
569 ROP2(cirrus_colorexpand_pattern_src_xor_dst),
570 ROP2(cirrus_colorexpand_pattern_src_or_dst),
571 ROP2(cirrus_colorexpand_pattern_notsrc_or_notdst),
572 ROP2(cirrus_colorexpand_pattern_src_notxor_dst),
573 ROP2(cirrus_colorexpand_pattern_src_or_notdst),
574 ROP2(cirrus_colorexpand_pattern_notsrc),
575 ROP2(cirrus_colorexpand_pattern_notsrc_or_dst),
576 ROP2(cirrus_colorexpand_pattern_notsrc_and_notdst),
579 static const cirrus_fill_t cirrus_fill[16][4] = {
580 ROP2(cirrus_fill_0),
581 ROP2(cirrus_fill_src_and_dst),
582 ROP_NOP2(cirrus_bitblt_fill_nop),
583 ROP2(cirrus_fill_src_and_notdst),
584 ROP2(cirrus_fill_notdst),
585 ROP2(cirrus_fill_src),
586 ROP2(cirrus_fill_1),
587 ROP2(cirrus_fill_notsrc_and_dst),
588 ROP2(cirrus_fill_src_xor_dst),
589 ROP2(cirrus_fill_src_or_dst),
590 ROP2(cirrus_fill_notsrc_or_notdst),
591 ROP2(cirrus_fill_src_notxor_dst),
592 ROP2(cirrus_fill_src_or_notdst),
593 ROP2(cirrus_fill_notsrc),
594 ROP2(cirrus_fill_notsrc_or_dst),
595 ROP2(cirrus_fill_notsrc_and_notdst),
598 static inline void cirrus_bitblt_fgcol(CirrusVGAState *s)
600 unsigned int color;
601 switch (s->cirrus_blt_pixelwidth) {
602 case 1:
603 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1;
604 break;
605 case 2:
606 color = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8);
607 s->cirrus_blt_fgcol = le16_to_cpu(color);
608 break;
609 case 3:
610 s->cirrus_blt_fgcol = s->cirrus_shadow_gr1 |
611 (s->gr[0x11] << 8) | (s->gr[0x13] << 16);
612 break;
613 default:
614 case 4:
615 color = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8) |
616 (s->gr[0x13] << 16) | (s->gr[0x15] << 24);
617 s->cirrus_blt_fgcol = le32_to_cpu(color);
618 break;
622 static inline void cirrus_bitblt_bgcol(CirrusVGAState *s)
624 unsigned int color;
625 switch (s->cirrus_blt_pixelwidth) {
626 case 1:
627 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0;
628 break;
629 case 2:
630 color = s->cirrus_shadow_gr0 | (s->gr[0x10] << 8);
631 s->cirrus_blt_bgcol = le16_to_cpu(color);
632 break;
633 case 3:
634 s->cirrus_blt_bgcol = s->cirrus_shadow_gr0 |
635 (s->gr[0x10] << 8) | (s->gr[0x12] << 16);
636 break;
637 default:
638 case 4:
639 color = s->cirrus_shadow_gr0 | (s->gr[0x10] << 8) |
640 (s->gr[0x12] << 16) | (s->gr[0x14] << 24);
641 s->cirrus_blt_bgcol = le32_to_cpu(color);
642 break;
646 static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
647 int off_pitch, int bytesperline,
648 int lines)
650 int y;
651 int off_cur;
652 int off_cur_end;
654 for (y = 0; y < lines; y++) {
655 off_cur = off_begin;
656 off_cur_end = (off_cur + bytesperline) & s->cirrus_addr_mask;
657 off_cur &= TARGET_PAGE_MASK;
658 while (off_cur < off_cur_end) {
659 cpu_physical_memory_set_dirty(s->vram_offset + off_cur);
660 off_cur += TARGET_PAGE_SIZE;
662 off_begin += off_pitch;
666 static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
667 const uint8_t * src)
669 uint8_t *dst;
671 dst = s->vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask);
673 if (BLTUNSAFE(s))
674 return 0;
676 (*s->cirrus_rop) (s, dst, src,
677 s->cirrus_blt_dstpitch, 0,
678 s->cirrus_blt_width, s->cirrus_blt_height);
679 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
680 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
681 s->cirrus_blt_height);
682 return 1;
685 /* fill */
687 static int cirrus_bitblt_solidfill(CirrusVGAState *s, int blt_rop)
689 cirrus_fill_t rop_func;
691 if (BLTUNSAFE(s))
692 return 0;
693 rop_func = cirrus_fill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
694 rop_func(s, s->vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
695 s->cirrus_blt_dstpitch,
696 s->cirrus_blt_width, s->cirrus_blt_height);
697 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
698 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
699 s->cirrus_blt_height);
700 cirrus_bitblt_reset(s);
701 return 1;
704 /***************************************
706 * bitblt (video-to-video)
708 ***************************************/
710 static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
712 return cirrus_bitblt_common_patterncopy(s,
713 s->vram_ptr + ((s->cirrus_blt_srcaddr & ~7) &
714 s->cirrus_addr_mask));
717 static void cirrus_do_copy(CirrusVGAState *s, int dst, int src, int w, int h)
719 int sx, sy;
720 int dx, dy;
721 int width, height;
722 int depth;
723 int notify = 0;
725 depth = s->get_bpp((VGAState *)s) / 8;
726 s->get_resolution((VGAState *)s, &width, &height);
728 /* extra x, y */
729 sx = (src % (width * depth)) / depth;
730 sy = (src / (width * depth));
731 dx = (dst % (width *depth)) / depth;
732 dy = (dst / (width * depth));
734 /* normalize width */
735 w /= depth;
737 /* if we're doing a backward copy, we have to adjust
738 our x/y to be the upper left corner (instead of the lower
739 right corner) */
740 if (s->cirrus_blt_dstpitch < 0) {
741 sx -= (s->cirrus_blt_width / depth) - 1;
742 dx -= (s->cirrus_blt_width / depth) - 1;
743 sy -= s->cirrus_blt_height - 1;
744 dy -= s->cirrus_blt_height - 1;
747 /* are we in the visible portion of memory? */
748 if (sx >= 0 && sy >= 0 && dx >= 0 && dy >= 0 &&
749 (sx + w) <= width && (sy + h) <= height &&
750 (dx + w) <= width && (dy + h) <= height) {
751 notify = 1;
754 /* make to sure only copy if it's a plain copy ROP */
755 if (*s->cirrus_rop != cirrus_bitblt_rop_fwd_src &&
756 *s->cirrus_rop != cirrus_bitblt_rop_bkwd_src)
757 notify = 0;
759 /* we have to flush all pending changes so that the copy
760 is generated at the appropriate moment in time */
761 if (notify)
762 vga_hw_update();
764 (*s->cirrus_rop) (s, s->vram_ptr +
765 (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
766 s->vram_ptr +
767 (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
768 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
769 s->cirrus_blt_width, s->cirrus_blt_height);
771 if (notify)
772 qemu_console_copy(s->console,
773 sx, sy, dx, dy,
774 s->cirrus_blt_width / depth,
775 s->cirrus_blt_height);
777 /* we don't have to notify the display that this portion has
778 changed since qemu_console_copy implies this */
780 if (!notify)
781 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
782 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
783 s->cirrus_blt_height);
786 static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
788 if (BLTUNSAFE(s))
789 return 0;
791 if (s->ds->dpy_copy) {
792 cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->start_addr,
793 s->cirrus_blt_srcaddr - s->start_addr,
794 s->cirrus_blt_width, s->cirrus_blt_height);
795 } else {
796 (*s->cirrus_rop) (s, s->vram_ptr +
797 (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
798 s->vram_ptr +
799 (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
800 s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
801 s->cirrus_blt_width, s->cirrus_blt_height);
803 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
804 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
805 s->cirrus_blt_height);
808 return 1;
811 /***************************************
813 * bitblt (cpu-to-video)
815 ***************************************/
817 static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
819 int copy_count;
820 uint8_t *end_ptr;
822 if (s->cirrus_srccounter > 0) {
823 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
824 cirrus_bitblt_common_patterncopy(s, s->cirrus_bltbuf);
825 the_end:
826 s->cirrus_srccounter = 0;
827 cirrus_bitblt_reset(s);
828 } else {
829 /* at least one scan line */
830 do {
831 (*s->cirrus_rop)(s, s->vram_ptr +
832 (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
833 s->cirrus_bltbuf, 0, 0, s->cirrus_blt_width, 1);
834 cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
835 s->cirrus_blt_width, 1);
836 s->cirrus_blt_dstaddr += s->cirrus_blt_dstpitch;
837 s->cirrus_srccounter -= s->cirrus_blt_srcpitch;
838 if (s->cirrus_srccounter <= 0)
839 goto the_end;
840 /* more bytes than needed can be transfered because of
841 word alignment, so we keep them for the next line */
842 /* XXX: keep alignment to speed up transfer */
843 end_ptr = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
844 copy_count = s->cirrus_srcptr_end - end_ptr;
845 memmove(s->cirrus_bltbuf, end_ptr, copy_count);
846 s->cirrus_srcptr = s->cirrus_bltbuf + copy_count;
847 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
848 } while (s->cirrus_srcptr >= s->cirrus_srcptr_end);
853 /***************************************
855 * bitblt wrapper
857 ***************************************/
859 static void cirrus_bitblt_reset(CirrusVGAState * s)
861 s->gr[0x31] &=
862 ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
863 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
864 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
865 s->cirrus_srccounter = 0;
866 cirrus_update_memory_access(s);
869 static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
871 int w;
873 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
874 s->cirrus_srcptr = &s->cirrus_bltbuf[0];
875 s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
877 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
878 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
879 s->cirrus_blt_srcpitch = 8;
880 } else {
881 /* XXX: check for 24 bpp */
882 s->cirrus_blt_srcpitch = 8 * 8 * s->cirrus_blt_pixelwidth;
884 s->cirrus_srccounter = s->cirrus_blt_srcpitch;
885 } else {
886 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
887 w = s->cirrus_blt_width / s->cirrus_blt_pixelwidth;
888 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_DWORDGRANULARITY)
889 s->cirrus_blt_srcpitch = ((w + 31) >> 5);
890 else
891 s->cirrus_blt_srcpitch = ((w + 7) >> 3);
892 } else {
893 /* always align input size to 32 bits */
894 s->cirrus_blt_srcpitch = (s->cirrus_blt_width + 3) & ~3;
896 s->cirrus_srccounter = s->cirrus_blt_srcpitch * s->cirrus_blt_height;
898 s->cirrus_srcptr = s->cirrus_bltbuf;
899 s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
900 cirrus_update_memory_access(s);
901 return 1;
904 static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
906 /* XXX */
907 #ifdef DEBUG_BITBLT
908 printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
909 #endif
910 return 0;
913 static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
915 int ret;
917 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
918 ret = cirrus_bitblt_videotovideo_patterncopy(s);
919 } else {
920 ret = cirrus_bitblt_videotovideo_copy(s);
922 if (ret)
923 cirrus_bitblt_reset(s);
924 return ret;
927 static void cirrus_bitblt_start(CirrusVGAState * s)
929 uint8_t blt_rop;
931 s->gr[0x31] |= CIRRUS_BLT_BUSY;
933 s->cirrus_blt_width = (s->gr[0x20] | (s->gr[0x21] << 8)) + 1;
934 s->cirrus_blt_height = (s->gr[0x22] | (s->gr[0x23] << 8)) + 1;
935 s->cirrus_blt_dstpitch = (s->gr[0x24] | (s->gr[0x25] << 8));
936 s->cirrus_blt_srcpitch = (s->gr[0x26] | (s->gr[0x27] << 8));
937 s->cirrus_blt_dstaddr =
938 (s->gr[0x28] | (s->gr[0x29] << 8) | (s->gr[0x2a] << 16));
939 s->cirrus_blt_srcaddr =
940 (s->gr[0x2c] | (s->gr[0x2d] << 8) | (s->gr[0x2e] << 16));
941 s->cirrus_blt_mode = s->gr[0x30];
942 s->cirrus_blt_modeext = s->gr[0x33];
943 blt_rop = s->gr[0x32];
945 #ifdef DEBUG_BITBLT
946 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",
947 blt_rop,
948 s->cirrus_blt_mode,
949 s->cirrus_blt_modeext,
950 s->cirrus_blt_width,
951 s->cirrus_blt_height,
952 s->cirrus_blt_dstpitch,
953 s->cirrus_blt_srcpitch,
954 s->cirrus_blt_dstaddr,
955 s->cirrus_blt_srcaddr,
956 s->gr[0x2f]);
957 #endif
959 switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
960 case CIRRUS_BLTMODE_PIXELWIDTH8:
961 s->cirrus_blt_pixelwidth = 1;
962 break;
963 case CIRRUS_BLTMODE_PIXELWIDTH16:
964 s->cirrus_blt_pixelwidth = 2;
965 break;
966 case CIRRUS_BLTMODE_PIXELWIDTH24:
967 s->cirrus_blt_pixelwidth = 3;
968 break;
969 case CIRRUS_BLTMODE_PIXELWIDTH32:
970 s->cirrus_blt_pixelwidth = 4;
971 break;
972 default:
973 #ifdef DEBUG_BITBLT
974 printf("cirrus: bitblt - pixel width is unknown\n");
975 #endif
976 goto bitblt_ignore;
978 s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
980 if ((s->
981 cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
982 CIRRUS_BLTMODE_MEMSYSDEST))
983 == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
984 #ifdef DEBUG_BITBLT
985 printf("cirrus: bitblt - memory-to-memory copy is requested\n");
986 #endif
987 goto bitblt_ignore;
990 if ((s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
991 (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST |
992 CIRRUS_BLTMODE_TRANSPARENTCOMP |
993 CIRRUS_BLTMODE_PATTERNCOPY |
994 CIRRUS_BLTMODE_COLOREXPAND)) ==
995 (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
996 cirrus_bitblt_fgcol(s);
997 cirrus_bitblt_solidfill(s, blt_rop);
998 } else {
999 if ((s->cirrus_blt_mode & (CIRRUS_BLTMODE_COLOREXPAND |
1000 CIRRUS_BLTMODE_PATTERNCOPY)) ==
1001 CIRRUS_BLTMODE_COLOREXPAND) {
1003 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1004 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
1005 cirrus_bitblt_bgcol(s);
1006 else
1007 cirrus_bitblt_fgcol(s);
1008 s->cirrus_rop = cirrus_colorexpand_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1009 } else {
1010 cirrus_bitblt_fgcol(s);
1011 cirrus_bitblt_bgcol(s);
1012 s->cirrus_rop = cirrus_colorexpand[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1014 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
1015 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
1016 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1017 if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
1018 cirrus_bitblt_bgcol(s);
1019 else
1020 cirrus_bitblt_fgcol(s);
1021 s->cirrus_rop = cirrus_colorexpand_pattern_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1022 } else {
1023 cirrus_bitblt_fgcol(s);
1024 cirrus_bitblt_bgcol(s);
1025 s->cirrus_rop = cirrus_colorexpand_pattern[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1027 } else {
1028 s->cirrus_rop = cirrus_patternfill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1030 } else {
1031 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
1032 if (s->cirrus_blt_pixelwidth > 2) {
1033 printf("src transparent without colorexpand must be 8bpp or 16bpp\n");
1034 goto bitblt_ignore;
1036 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
1037 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
1038 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
1039 s->cirrus_rop = cirrus_bkwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1040 } else {
1041 s->cirrus_rop = cirrus_fwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
1043 } else {
1044 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
1045 s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
1046 s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
1047 s->cirrus_rop = cirrus_bkwd_rop[rop_to_index[blt_rop]];
1048 } else {
1049 s->cirrus_rop = cirrus_fwd_rop[rop_to_index[blt_rop]];
1053 // setup bitblt engine.
1054 if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
1055 if (!cirrus_bitblt_cputovideo(s))
1056 goto bitblt_ignore;
1057 } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
1058 if (!cirrus_bitblt_videotocpu(s))
1059 goto bitblt_ignore;
1060 } else {
1061 if (!cirrus_bitblt_videotovideo(s))
1062 goto bitblt_ignore;
1065 return;
1066 bitblt_ignore:;
1067 cirrus_bitblt_reset(s);
1070 static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
1072 unsigned old_value;
1074 old_value = s->gr[0x31];
1075 s->gr[0x31] = reg_value;
1077 if (((old_value & CIRRUS_BLT_RESET) != 0) &&
1078 ((reg_value & CIRRUS_BLT_RESET) == 0)) {
1079 cirrus_bitblt_reset(s);
1080 } else if (((old_value & CIRRUS_BLT_START) == 0) &&
1081 ((reg_value & CIRRUS_BLT_START) != 0)) {
1082 cirrus_bitblt_start(s);
1087 /***************************************
1089 * basic parameters
1091 ***************************************/
1093 static void cirrus_get_offsets(VGAState *s1,
1094 uint32_t *pline_offset,
1095 uint32_t *pstart_addr,
1096 uint32_t *pline_compare)
1098 CirrusVGAState * s = (CirrusVGAState *)s1;
1099 uint32_t start_addr, line_offset, line_compare;
1101 line_offset = s->cr[0x13]
1102 | ((s->cr[0x1b] & 0x10) << 4);
1103 line_offset <<= 3;
1104 *pline_offset = line_offset;
1106 start_addr = (s->cr[0x0c] << 8)
1107 | s->cr[0x0d]
1108 | ((s->cr[0x1b] & 0x01) << 16)
1109 | ((s->cr[0x1b] & 0x0c) << 15)
1110 | ((s->cr[0x1d] & 0x80) << 12);
1111 *pstart_addr = start_addr;
1113 line_compare = s->cr[0x18] |
1114 ((s->cr[0x07] & 0x10) << 4) |
1115 ((s->cr[0x09] & 0x40) << 3);
1116 *pline_compare = line_compare;
1119 static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
1121 uint32_t ret = 16;
1123 switch (s->cirrus_hidden_dac_data & 0xf) {
1124 case 0:
1125 ret = 15;
1126 break; /* Sierra HiColor */
1127 case 1:
1128 ret = 16;
1129 break; /* XGA HiColor */
1130 default:
1131 #ifdef DEBUG_CIRRUS
1132 printf("cirrus: invalid DAC value %x in 16bpp\n",
1133 (s->cirrus_hidden_dac_data & 0xf));
1134 #endif
1135 ret = 15; /* XXX */
1136 break;
1138 return ret;
1141 static int cirrus_get_bpp(VGAState *s1)
1143 CirrusVGAState * s = (CirrusVGAState *)s1;
1144 uint32_t ret = 8;
1146 if ((s->sr[0x07] & 0x01) != 0) {
1147 /* Cirrus SVGA */
1148 switch (s->sr[0x07] & CIRRUS_SR7_BPP_MASK) {
1149 case CIRRUS_SR7_BPP_8:
1150 ret = 8;
1151 break;
1152 case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
1153 ret = cirrus_get_bpp16_depth(s);
1154 break;
1155 case CIRRUS_SR7_BPP_24:
1156 ret = 24;
1157 break;
1158 case CIRRUS_SR7_BPP_16:
1159 ret = cirrus_get_bpp16_depth(s);
1160 break;
1161 case CIRRUS_SR7_BPP_32:
1162 ret = 32;
1163 break;
1164 default:
1165 #ifdef DEBUG_CIRRUS
1166 printf("cirrus: unknown bpp - sr7=%x\n", s->sr[0x7]);
1167 #endif
1168 ret = 8;
1169 break;
1171 } else {
1172 /* VGA */
1173 ret = 0;
1176 return ret;
1179 static void cirrus_get_resolution(VGAState *s, int *pwidth, int *pheight)
1181 int width, height;
1183 width = (s->cr[0x01] + 1) * 8;
1184 height = s->cr[0x12] |
1185 ((s->cr[0x07] & 0x02) << 7) |
1186 ((s->cr[0x07] & 0x40) << 3);
1187 height = (height + 1);
1188 /* interlace support */
1189 if (s->cr[0x1a] & 0x01)
1190 height = height * 2;
1191 *pwidth = width;
1192 *pheight = height;
1195 /***************************************
1197 * bank memory
1199 ***************************************/
1201 static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1203 unsigned offset;
1204 unsigned limit;
1206 if ((s->gr[0x0b] & 0x01) != 0) /* dual bank */
1207 offset = s->gr[0x09 + bank_index];
1208 else /* single bank */
1209 offset = s->gr[0x09];
1211 if ((s->gr[0x0b] & 0x20) != 0)
1212 offset <<= 14;
1213 else
1214 offset <<= 12;
1216 if (s->real_vram_size <= offset)
1217 limit = 0;
1218 else
1219 limit = s->real_vram_size - offset;
1221 if (((s->gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1222 if (limit > 0x8000) {
1223 offset += 0x8000;
1224 limit -= 0x8000;
1225 } else {
1226 limit = 0;
1230 if (limit > 0) {
1231 s->cirrus_bank_base[bank_index] = offset;
1232 s->cirrus_bank_limit[bank_index] = limit;
1233 } else {
1234 s->cirrus_bank_base[bank_index] = 0;
1235 s->cirrus_bank_limit[bank_index] = 0;
1239 /***************************************
1241 * I/O access between 0x3c4-0x3c5
1243 ***************************************/
1245 static int
1246 cirrus_hook_read_sr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1248 switch (reg_index) {
1249 case 0x00: // Standard VGA
1250 case 0x01: // Standard VGA
1251 case 0x02: // Standard VGA
1252 case 0x03: // Standard VGA
1253 case 0x04: // Standard VGA
1254 return CIRRUS_HOOK_NOT_HANDLED;
1255 case 0x06: // Unlock Cirrus extensions
1256 *reg_value = s->sr[reg_index];
1257 break;
1258 case 0x10:
1259 case 0x30:
1260 case 0x50:
1261 case 0x70: // Graphics Cursor X
1262 case 0x90:
1263 case 0xb0:
1264 case 0xd0:
1265 case 0xf0: // Graphics Cursor X
1266 *reg_value = s->sr[0x10];
1267 break;
1268 case 0x11:
1269 case 0x31:
1270 case 0x51:
1271 case 0x71: // Graphics Cursor Y
1272 case 0x91:
1273 case 0xb1:
1274 case 0xd1:
1275 case 0xf1: // Graphics Cursor Y
1276 *reg_value = s->sr[0x11];
1277 break;
1278 case 0x05: // ???
1279 case 0x07: // Extended Sequencer Mode
1280 case 0x08: // EEPROM Control
1281 case 0x09: // Scratch Register 0
1282 case 0x0a: // Scratch Register 1
1283 case 0x0b: // VCLK 0
1284 case 0x0c: // VCLK 1
1285 case 0x0d: // VCLK 2
1286 case 0x0e: // VCLK 3
1287 case 0x0f: // DRAM Control
1288 case 0x12: // Graphics Cursor Attribute
1289 case 0x13: // Graphics Cursor Pattern Address
1290 case 0x14: // Scratch Register 2
1291 case 0x15: // Scratch Register 3
1292 case 0x16: // Performance Tuning Register
1293 case 0x17: // Configuration Readback and Extended Control
1294 case 0x18: // Signature Generator Control
1295 case 0x19: // Signal Generator Result
1296 case 0x1a: // Signal Generator Result
1297 case 0x1b: // VCLK 0 Denominator & Post
1298 case 0x1c: // VCLK 1 Denominator & Post
1299 case 0x1d: // VCLK 2 Denominator & Post
1300 case 0x1e: // VCLK 3 Denominator & Post
1301 case 0x1f: // BIOS Write Enable and MCLK select
1302 #ifdef DEBUG_CIRRUS
1303 printf("cirrus: handled inport sr_index %02x\n", reg_index);
1304 #endif
1305 *reg_value = s->sr[reg_index];
1306 break;
1307 default:
1308 #ifdef DEBUG_CIRRUS
1309 printf("cirrus: inport sr_index %02x\n", reg_index);
1310 #endif
1311 *reg_value = 0xff;
1312 break;
1315 return CIRRUS_HOOK_HANDLED;
1318 static int
1319 cirrus_hook_write_sr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1321 switch (reg_index) {
1322 case 0x00: // Standard VGA
1323 case 0x01: // Standard VGA
1324 case 0x02: // Standard VGA
1325 case 0x03: // Standard VGA
1326 case 0x04: // Standard VGA
1327 return CIRRUS_HOOK_NOT_HANDLED;
1328 case 0x06: // Unlock Cirrus extensions
1329 reg_value &= 0x17;
1330 if (reg_value == 0x12) {
1331 s->sr[reg_index] = 0x12;
1332 } else {
1333 s->sr[reg_index] = 0x0f;
1335 break;
1336 case 0x10:
1337 case 0x30:
1338 case 0x50:
1339 case 0x70: // Graphics Cursor X
1340 case 0x90:
1341 case 0xb0:
1342 case 0xd0:
1343 case 0xf0: // Graphics Cursor X
1344 s->sr[0x10] = reg_value;
1345 s->hw_cursor_x = (reg_value << 3) | (reg_index >> 5);
1346 break;
1347 case 0x11:
1348 case 0x31:
1349 case 0x51:
1350 case 0x71: // Graphics Cursor Y
1351 case 0x91:
1352 case 0xb1:
1353 case 0xd1:
1354 case 0xf1: // Graphics Cursor Y
1355 s->sr[0x11] = reg_value;
1356 s->hw_cursor_y = (reg_value << 3) | (reg_index >> 5);
1357 break;
1358 case 0x07: // Extended Sequencer Mode
1359 case 0x08: // EEPROM Control
1360 case 0x09: // Scratch Register 0
1361 case 0x0a: // Scratch Register 1
1362 case 0x0b: // VCLK 0
1363 case 0x0c: // VCLK 1
1364 case 0x0d: // VCLK 2
1365 case 0x0e: // VCLK 3
1366 case 0x0f: // DRAM Control
1367 case 0x12: // Graphics Cursor Attribute
1368 case 0x13: // Graphics Cursor Pattern Address
1369 case 0x14: // Scratch Register 2
1370 case 0x15: // Scratch Register 3
1371 case 0x16: // Performance Tuning Register
1372 case 0x18: // Signature Generator Control
1373 case 0x19: // Signature Generator Result
1374 case 0x1a: // Signature Generator Result
1375 case 0x1b: // VCLK 0 Denominator & Post
1376 case 0x1c: // VCLK 1 Denominator & Post
1377 case 0x1d: // VCLK 2 Denominator & Post
1378 case 0x1e: // VCLK 3 Denominator & Post
1379 case 0x1f: // BIOS Write Enable and MCLK select
1380 s->sr[reg_index] = reg_value;
1381 #ifdef DEBUG_CIRRUS
1382 printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1383 reg_index, reg_value);
1384 #endif
1385 break;
1386 case 0x17: // Configuration Readback and Extended Control
1387 s->sr[reg_index] = (s->sr[reg_index] & 0x38) | (reg_value & 0xc7);
1388 cirrus_update_memory_access(s);
1389 break;
1390 default:
1391 #ifdef DEBUG_CIRRUS
1392 printf("cirrus: outport sr_index %02x, sr_value %02x\n", reg_index,
1393 reg_value);
1394 #endif
1395 break;
1398 return CIRRUS_HOOK_HANDLED;
1401 /***************************************
1403 * I/O access at 0x3c6
1405 ***************************************/
1407 static void cirrus_read_hidden_dac(CirrusVGAState * s, int *reg_value)
1409 *reg_value = 0xff;
1410 if (++s->cirrus_hidden_dac_lockindex == 5) {
1411 *reg_value = s->cirrus_hidden_dac_data;
1412 s->cirrus_hidden_dac_lockindex = 0;
1416 static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1418 if (s->cirrus_hidden_dac_lockindex == 4) {
1419 s->cirrus_hidden_dac_data = reg_value;
1420 #if defined(DEBUG_CIRRUS)
1421 printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1422 #endif
1424 s->cirrus_hidden_dac_lockindex = 0;
1427 /***************************************
1429 * I/O access at 0x3c9
1431 ***************************************/
1433 static int cirrus_hook_read_palette(CirrusVGAState * s, int *reg_value)
1435 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1436 return CIRRUS_HOOK_NOT_HANDLED;
1437 *reg_value =
1438 s->cirrus_hidden_palette[(s->dac_read_index & 0x0f) * 3 +
1439 s->dac_sub_index];
1440 if (++s->dac_sub_index == 3) {
1441 s->dac_sub_index = 0;
1442 s->dac_read_index++;
1444 return CIRRUS_HOOK_HANDLED;
1447 static int cirrus_hook_write_palette(CirrusVGAState * s, int reg_value)
1449 if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1450 return CIRRUS_HOOK_NOT_HANDLED;
1451 s->dac_cache[s->dac_sub_index] = reg_value;
1452 if (++s->dac_sub_index == 3) {
1453 memcpy(&s->cirrus_hidden_palette[(s->dac_write_index & 0x0f) * 3],
1454 s->dac_cache, 3);
1455 /* XXX update cursor */
1456 s->dac_sub_index = 0;
1457 s->dac_write_index++;
1459 return CIRRUS_HOOK_HANDLED;
1462 /***************************************
1464 * I/O access between 0x3ce-0x3cf
1466 ***************************************/
1468 static int
1469 cirrus_hook_read_gr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1471 switch (reg_index) {
1472 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1473 *reg_value = s->cirrus_shadow_gr0;
1474 return CIRRUS_HOOK_HANDLED;
1475 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1476 *reg_value = s->cirrus_shadow_gr1;
1477 return CIRRUS_HOOK_HANDLED;
1478 case 0x02: // Standard VGA
1479 case 0x03: // Standard VGA
1480 case 0x04: // Standard VGA
1481 case 0x06: // Standard VGA
1482 case 0x07: // Standard VGA
1483 case 0x08: // Standard VGA
1484 return CIRRUS_HOOK_NOT_HANDLED;
1485 case 0x05: // Standard VGA, Cirrus extended mode
1486 default:
1487 break;
1490 if (reg_index < 0x3a) {
1491 *reg_value = s->gr[reg_index];
1492 } else {
1493 #ifdef DEBUG_CIRRUS
1494 printf("cirrus: inport gr_index %02x\n", reg_index);
1495 #endif
1496 *reg_value = 0xff;
1499 return CIRRUS_HOOK_HANDLED;
1502 static int
1503 cirrus_hook_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1505 #if defined(DEBUG_BITBLT) && 0
1506 printf("gr%02x: %02x\n", reg_index, reg_value);
1507 #endif
1508 switch (reg_index) {
1509 case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1510 s->cirrus_shadow_gr0 = reg_value;
1511 return CIRRUS_HOOK_NOT_HANDLED;
1512 case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1513 s->cirrus_shadow_gr1 = reg_value;
1514 return CIRRUS_HOOK_NOT_HANDLED;
1515 case 0x02: // Standard VGA
1516 case 0x03: // Standard VGA
1517 case 0x04: // Standard VGA
1518 case 0x06: // Standard VGA
1519 case 0x07: // Standard VGA
1520 case 0x08: // Standard VGA
1521 return CIRRUS_HOOK_NOT_HANDLED;
1522 case 0x05: // Standard VGA, Cirrus extended mode
1523 s->gr[reg_index] = reg_value & 0x7f;
1524 cirrus_update_memory_access(s);
1525 break;
1526 case 0x09: // bank offset #0
1527 case 0x0A: // bank offset #1
1528 s->gr[reg_index] = reg_value;
1529 cirrus_update_bank_ptr(s, 0);
1530 cirrus_update_bank_ptr(s, 1);
1531 break;
1532 case 0x0B:
1533 s->gr[reg_index] = reg_value;
1534 cirrus_update_bank_ptr(s, 0);
1535 cirrus_update_bank_ptr(s, 1);
1536 cirrus_update_memory_access(s);
1537 break;
1538 case 0x10: // BGCOLOR 0x0000ff00
1539 case 0x11: // FGCOLOR 0x0000ff00
1540 case 0x12: // BGCOLOR 0x00ff0000
1541 case 0x13: // FGCOLOR 0x00ff0000
1542 case 0x14: // BGCOLOR 0xff000000
1543 case 0x15: // FGCOLOR 0xff000000
1544 case 0x20: // BLT WIDTH 0x0000ff
1545 case 0x22: // BLT HEIGHT 0x0000ff
1546 case 0x24: // BLT DEST PITCH 0x0000ff
1547 case 0x26: // BLT SRC PITCH 0x0000ff
1548 case 0x28: // BLT DEST ADDR 0x0000ff
1549 case 0x29: // BLT DEST ADDR 0x00ff00
1550 case 0x2c: // BLT SRC ADDR 0x0000ff
1551 case 0x2d: // BLT SRC ADDR 0x00ff00
1552 case 0x2f: // BLT WRITEMASK
1553 case 0x30: // BLT MODE
1554 case 0x32: // RASTER OP
1555 case 0x33: // BLT MODEEXT
1556 case 0x34: // BLT TRANSPARENT COLOR 0x00ff
1557 case 0x35: // BLT TRANSPARENT COLOR 0xff00
1558 case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
1559 case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
1560 s->gr[reg_index] = reg_value;
1561 break;
1562 case 0x21: // BLT WIDTH 0x001f00
1563 case 0x23: // BLT HEIGHT 0x001f00
1564 case 0x25: // BLT DEST PITCH 0x001f00
1565 case 0x27: // BLT SRC PITCH 0x001f00
1566 s->gr[reg_index] = reg_value & 0x1f;
1567 break;
1568 case 0x2a: // BLT DEST ADDR 0x3f0000
1569 s->gr[reg_index] = reg_value & 0x3f;
1570 /* if auto start mode, starts bit blt now */
1571 if (s->gr[0x31] & CIRRUS_BLT_AUTOSTART) {
1572 cirrus_bitblt_start(s);
1574 break;
1575 case 0x2e: // BLT SRC ADDR 0x3f0000
1576 s->gr[reg_index] = reg_value & 0x3f;
1577 break;
1578 case 0x31: // BLT STATUS/START
1579 cirrus_write_bitblt(s, reg_value);
1580 break;
1581 default:
1582 #ifdef DEBUG_CIRRUS
1583 printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1584 reg_value);
1585 #endif
1586 break;
1589 return CIRRUS_HOOK_HANDLED;
1592 /***************************************
1594 * I/O access between 0x3d4-0x3d5
1596 ***************************************/
1598 static int
1599 cirrus_hook_read_cr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1601 switch (reg_index) {
1602 case 0x00: // Standard VGA
1603 case 0x01: // Standard VGA
1604 case 0x02: // Standard VGA
1605 case 0x03: // Standard VGA
1606 case 0x04: // Standard VGA
1607 case 0x05: // Standard VGA
1608 case 0x06: // Standard VGA
1609 case 0x07: // Standard VGA
1610 case 0x08: // Standard VGA
1611 case 0x09: // Standard VGA
1612 case 0x0a: // Standard VGA
1613 case 0x0b: // Standard VGA
1614 case 0x0c: // Standard VGA
1615 case 0x0d: // Standard VGA
1616 case 0x0e: // Standard VGA
1617 case 0x0f: // Standard VGA
1618 case 0x10: // Standard VGA
1619 case 0x11: // Standard VGA
1620 case 0x12: // Standard VGA
1621 case 0x13: // Standard VGA
1622 case 0x14: // Standard VGA
1623 case 0x15: // Standard VGA
1624 case 0x16: // Standard VGA
1625 case 0x17: // Standard VGA
1626 case 0x18: // Standard VGA
1627 return CIRRUS_HOOK_NOT_HANDLED;
1628 case 0x24: // Attribute Controller Toggle Readback (R)
1629 *reg_value = (s->ar_flip_flop << 7);
1630 break;
1631 case 0x19: // Interlace End
1632 case 0x1a: // Miscellaneous Control
1633 case 0x1b: // Extended Display Control
1634 case 0x1c: // Sync Adjust and Genlock
1635 case 0x1d: // Overlay Extended Control
1636 case 0x22: // Graphics Data Latches Readback (R)
1637 case 0x25: // Part Status
1638 case 0x27: // Part ID (R)
1639 *reg_value = s->cr[reg_index];
1640 break;
1641 case 0x26: // Attribute Controller Index Readback (R)
1642 *reg_value = s->ar_index & 0x3f;
1643 break;
1644 default:
1645 #ifdef DEBUG_CIRRUS
1646 printf("cirrus: inport cr_index %02x\n", reg_index);
1647 *reg_value = 0xff;
1648 #endif
1649 break;
1652 return CIRRUS_HOOK_HANDLED;
1655 static int
1656 cirrus_hook_write_cr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1658 switch (reg_index) {
1659 case 0x00: // Standard VGA
1660 case 0x01: // Standard VGA
1661 case 0x02: // Standard VGA
1662 case 0x03: // Standard VGA
1663 case 0x04: // Standard VGA
1664 case 0x05: // Standard VGA
1665 case 0x06: // Standard VGA
1666 case 0x07: // Standard VGA
1667 case 0x08: // Standard VGA
1668 case 0x09: // Standard VGA
1669 case 0x0a: // Standard VGA
1670 case 0x0b: // Standard VGA
1671 case 0x0c: // Standard VGA
1672 case 0x0d: // Standard VGA
1673 case 0x0e: // Standard VGA
1674 case 0x0f: // Standard VGA
1675 case 0x10: // Standard VGA
1676 case 0x11: // Standard VGA
1677 case 0x12: // Standard VGA
1678 case 0x13: // Standard VGA
1679 case 0x14: // Standard VGA
1680 case 0x15: // Standard VGA
1681 case 0x16: // Standard VGA
1682 case 0x17: // Standard VGA
1683 case 0x18: // Standard VGA
1684 return CIRRUS_HOOK_NOT_HANDLED;
1685 case 0x19: // Interlace End
1686 case 0x1a: // Miscellaneous Control
1687 case 0x1b: // Extended Display Control
1688 case 0x1c: // Sync Adjust and Genlock
1689 case 0x1d: // Overlay Extended Control
1690 s->cr[reg_index] = reg_value;
1691 #ifdef DEBUG_CIRRUS
1692 printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1693 reg_index, reg_value);
1694 #endif
1695 break;
1696 case 0x22: // Graphics Data Latches Readback (R)
1697 case 0x24: // Attribute Controller Toggle Readback (R)
1698 case 0x26: // Attribute Controller Index Readback (R)
1699 case 0x27: // Part ID (R)
1700 break;
1701 case 0x25: // Part Status
1702 default:
1703 #ifdef DEBUG_CIRRUS
1704 printf("cirrus: outport cr_index %02x, cr_value %02x\n", reg_index,
1705 reg_value);
1706 #endif
1707 break;
1710 return CIRRUS_HOOK_HANDLED;
1713 /***************************************
1715 * memory-mapped I/O (bitblt)
1717 ***************************************/
1719 static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1721 int value = 0xff;
1723 switch (address) {
1724 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1725 cirrus_hook_read_gr(s, 0x00, &value);
1726 break;
1727 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1728 cirrus_hook_read_gr(s, 0x10, &value);
1729 break;
1730 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1731 cirrus_hook_read_gr(s, 0x12, &value);
1732 break;
1733 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1734 cirrus_hook_read_gr(s, 0x14, &value);
1735 break;
1736 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1737 cirrus_hook_read_gr(s, 0x01, &value);
1738 break;
1739 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1740 cirrus_hook_read_gr(s, 0x11, &value);
1741 break;
1742 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1743 cirrus_hook_read_gr(s, 0x13, &value);
1744 break;
1745 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1746 cirrus_hook_read_gr(s, 0x15, &value);
1747 break;
1748 case (CIRRUS_MMIO_BLTWIDTH + 0):
1749 cirrus_hook_read_gr(s, 0x20, &value);
1750 break;
1751 case (CIRRUS_MMIO_BLTWIDTH + 1):
1752 cirrus_hook_read_gr(s, 0x21, &value);
1753 break;
1754 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1755 cirrus_hook_read_gr(s, 0x22, &value);
1756 break;
1757 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1758 cirrus_hook_read_gr(s, 0x23, &value);
1759 break;
1760 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1761 cirrus_hook_read_gr(s, 0x24, &value);
1762 break;
1763 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1764 cirrus_hook_read_gr(s, 0x25, &value);
1765 break;
1766 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1767 cirrus_hook_read_gr(s, 0x26, &value);
1768 break;
1769 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1770 cirrus_hook_read_gr(s, 0x27, &value);
1771 break;
1772 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1773 cirrus_hook_read_gr(s, 0x28, &value);
1774 break;
1775 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1776 cirrus_hook_read_gr(s, 0x29, &value);
1777 break;
1778 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1779 cirrus_hook_read_gr(s, 0x2a, &value);
1780 break;
1781 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1782 cirrus_hook_read_gr(s, 0x2c, &value);
1783 break;
1784 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1785 cirrus_hook_read_gr(s, 0x2d, &value);
1786 break;
1787 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1788 cirrus_hook_read_gr(s, 0x2e, &value);
1789 break;
1790 case CIRRUS_MMIO_BLTWRITEMASK:
1791 cirrus_hook_read_gr(s, 0x2f, &value);
1792 break;
1793 case CIRRUS_MMIO_BLTMODE:
1794 cirrus_hook_read_gr(s, 0x30, &value);
1795 break;
1796 case CIRRUS_MMIO_BLTROP:
1797 cirrus_hook_read_gr(s, 0x32, &value);
1798 break;
1799 case CIRRUS_MMIO_BLTMODEEXT:
1800 cirrus_hook_read_gr(s, 0x33, &value);
1801 break;
1802 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1803 cirrus_hook_read_gr(s, 0x34, &value);
1804 break;
1805 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1806 cirrus_hook_read_gr(s, 0x35, &value);
1807 break;
1808 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1809 cirrus_hook_read_gr(s, 0x38, &value);
1810 break;
1811 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1812 cirrus_hook_read_gr(s, 0x39, &value);
1813 break;
1814 case CIRRUS_MMIO_BLTSTATUS:
1815 cirrus_hook_read_gr(s, 0x31, &value);
1816 break;
1817 default:
1818 #ifdef DEBUG_CIRRUS
1819 printf("cirrus: mmio read - address 0x%04x\n", address);
1820 #endif
1821 break;
1824 return (uint8_t) value;
1827 static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1828 uint8_t value)
1830 switch (address) {
1831 case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1832 cirrus_hook_write_gr(s, 0x00, value);
1833 break;
1834 case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1835 cirrus_hook_write_gr(s, 0x10, value);
1836 break;
1837 case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1838 cirrus_hook_write_gr(s, 0x12, value);
1839 break;
1840 case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1841 cirrus_hook_write_gr(s, 0x14, value);
1842 break;
1843 case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1844 cirrus_hook_write_gr(s, 0x01, value);
1845 break;
1846 case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1847 cirrus_hook_write_gr(s, 0x11, value);
1848 break;
1849 case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1850 cirrus_hook_write_gr(s, 0x13, value);
1851 break;
1852 case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1853 cirrus_hook_write_gr(s, 0x15, value);
1854 break;
1855 case (CIRRUS_MMIO_BLTWIDTH + 0):
1856 cirrus_hook_write_gr(s, 0x20, value);
1857 break;
1858 case (CIRRUS_MMIO_BLTWIDTH + 1):
1859 cirrus_hook_write_gr(s, 0x21, value);
1860 break;
1861 case (CIRRUS_MMIO_BLTHEIGHT + 0):
1862 cirrus_hook_write_gr(s, 0x22, value);
1863 break;
1864 case (CIRRUS_MMIO_BLTHEIGHT + 1):
1865 cirrus_hook_write_gr(s, 0x23, value);
1866 break;
1867 case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1868 cirrus_hook_write_gr(s, 0x24, value);
1869 break;
1870 case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1871 cirrus_hook_write_gr(s, 0x25, value);
1872 break;
1873 case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1874 cirrus_hook_write_gr(s, 0x26, value);
1875 break;
1876 case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1877 cirrus_hook_write_gr(s, 0x27, value);
1878 break;
1879 case (CIRRUS_MMIO_BLTDESTADDR + 0):
1880 cirrus_hook_write_gr(s, 0x28, value);
1881 break;
1882 case (CIRRUS_MMIO_BLTDESTADDR + 1):
1883 cirrus_hook_write_gr(s, 0x29, value);
1884 break;
1885 case (CIRRUS_MMIO_BLTDESTADDR + 2):
1886 cirrus_hook_write_gr(s, 0x2a, value);
1887 break;
1888 case (CIRRUS_MMIO_BLTDESTADDR + 3):
1889 /* ignored */
1890 break;
1891 case (CIRRUS_MMIO_BLTSRCADDR + 0):
1892 cirrus_hook_write_gr(s, 0x2c, value);
1893 break;
1894 case (CIRRUS_MMIO_BLTSRCADDR + 1):
1895 cirrus_hook_write_gr(s, 0x2d, value);
1896 break;
1897 case (CIRRUS_MMIO_BLTSRCADDR + 2):
1898 cirrus_hook_write_gr(s, 0x2e, value);
1899 break;
1900 case CIRRUS_MMIO_BLTWRITEMASK:
1901 cirrus_hook_write_gr(s, 0x2f, value);
1902 break;
1903 case CIRRUS_MMIO_BLTMODE:
1904 cirrus_hook_write_gr(s, 0x30, value);
1905 break;
1906 case CIRRUS_MMIO_BLTROP:
1907 cirrus_hook_write_gr(s, 0x32, value);
1908 break;
1909 case CIRRUS_MMIO_BLTMODEEXT:
1910 cirrus_hook_write_gr(s, 0x33, value);
1911 break;
1912 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1913 cirrus_hook_write_gr(s, 0x34, value);
1914 break;
1915 case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1916 cirrus_hook_write_gr(s, 0x35, value);
1917 break;
1918 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1919 cirrus_hook_write_gr(s, 0x38, value);
1920 break;
1921 case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1922 cirrus_hook_write_gr(s, 0x39, value);
1923 break;
1924 case CIRRUS_MMIO_BLTSTATUS:
1925 cirrus_hook_write_gr(s, 0x31, value);
1926 break;
1927 default:
1928 #ifdef DEBUG_CIRRUS
1929 printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1930 address, value);
1931 #endif
1932 break;
1936 /***************************************
1938 * write mode 4/5
1940 * assume TARGET_PAGE_SIZE >= 16
1942 ***************************************/
1944 static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1945 unsigned mode,
1946 unsigned offset,
1947 uint32_t mem_value)
1949 int x;
1950 unsigned val = mem_value;
1951 uint8_t *dst;
1953 dst = s->vram_ptr + (offset &= s->cirrus_addr_mask);
1954 for (x = 0; x < 8; x++) {
1955 if (val & 0x80) {
1956 *dst = s->cirrus_shadow_gr1;
1957 } else if (mode == 5) {
1958 *dst = s->cirrus_shadow_gr0;
1960 val <<= 1;
1961 dst++;
1963 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1964 cpu_physical_memory_set_dirty(s->vram_offset + offset + 7);
1967 static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1968 unsigned mode,
1969 unsigned offset,
1970 uint32_t mem_value)
1972 int x;
1973 unsigned val = mem_value;
1974 uint8_t *dst;
1976 dst = s->vram_ptr + (offset &= s->cirrus_addr_mask);
1977 for (x = 0; x < 8; x++) {
1978 if (val & 0x80) {
1979 *dst = s->cirrus_shadow_gr1;
1980 *(dst + 1) = s->gr[0x11];
1981 } else if (mode == 5) {
1982 *dst = s->cirrus_shadow_gr0;
1983 *(dst + 1) = s->gr[0x10];
1985 val <<= 1;
1986 dst += 2;
1988 cpu_physical_memory_set_dirty(s->vram_offset + offset);
1989 cpu_physical_memory_set_dirty(s->vram_offset + offset + 15);
1992 /***************************************
1994 * memory access between 0xa0000-0xbffff
1996 ***************************************/
1998 static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
2000 CirrusVGAState *s = opaque;
2001 unsigned bank_index;
2002 unsigned bank_offset;
2003 uint32_t val;
2005 if ((s->sr[0x07] & 0x01) == 0) {
2006 return vga_mem_readb(s, addr);
2009 addr &= 0x1ffff;
2011 if (addr < 0x10000) {
2012 /* XXX handle bitblt */
2013 /* video memory */
2014 bank_index = addr >> 15;
2015 bank_offset = addr & 0x7fff;
2016 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2017 bank_offset += s->cirrus_bank_base[bank_index];
2018 if ((s->gr[0x0B] & 0x14) == 0x14) {
2019 bank_offset <<= 4;
2020 } else if (s->gr[0x0B] & 0x02) {
2021 bank_offset <<= 3;
2023 bank_offset &= s->cirrus_addr_mask;
2024 val = *(s->vram_ptr + bank_offset);
2025 } else
2026 val = 0xff;
2027 } else if (addr >= 0x18000 && addr < 0x18100) {
2028 /* memory-mapped I/O */
2029 val = 0xff;
2030 if ((s->sr[0x17] & 0x44) == 0x04) {
2031 val = cirrus_mmio_blt_read(s, addr & 0xff);
2033 } else {
2034 val = 0xff;
2035 #ifdef DEBUG_CIRRUS
2036 printf("cirrus: mem_readb %06x\n", addr);
2037 #endif
2039 return val;
2042 static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
2044 uint32_t v;
2045 #ifdef TARGET_WORDS_BIGENDIAN
2046 v = cirrus_vga_mem_readb(opaque, addr) << 8;
2047 v |= cirrus_vga_mem_readb(opaque, addr + 1);
2048 #else
2049 v = cirrus_vga_mem_readb(opaque, addr);
2050 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2051 #endif
2052 return v;
2055 static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
2057 uint32_t v;
2058 #ifdef TARGET_WORDS_BIGENDIAN
2059 v = cirrus_vga_mem_readb(opaque, addr) << 24;
2060 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
2061 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
2062 v |= cirrus_vga_mem_readb(opaque, addr + 3);
2063 #else
2064 v = cirrus_vga_mem_readb(opaque, addr);
2065 v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
2066 v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
2067 v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
2068 #endif
2069 return v;
2072 static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr,
2073 uint32_t mem_value)
2075 CirrusVGAState *s = opaque;
2076 unsigned bank_index;
2077 unsigned bank_offset;
2078 unsigned mode;
2080 if ((s->sr[0x07] & 0x01) == 0) {
2081 vga_mem_writeb(s, addr, mem_value);
2082 return;
2085 addr &= 0x1ffff;
2087 if (addr < 0x10000) {
2088 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2089 /* bitblt */
2090 *s->cirrus_srcptr++ = (uint8_t) mem_value;
2091 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2092 cirrus_bitblt_cputovideo_next(s);
2094 } else {
2095 /* video memory */
2096 bank_index = addr >> 15;
2097 bank_offset = addr & 0x7fff;
2098 if (bank_offset < s->cirrus_bank_limit[bank_index]) {
2099 bank_offset += s->cirrus_bank_base[bank_index];
2100 if ((s->gr[0x0B] & 0x14) == 0x14) {
2101 bank_offset <<= 4;
2102 } else if (s->gr[0x0B] & 0x02) {
2103 bank_offset <<= 3;
2105 bank_offset &= s->cirrus_addr_mask;
2106 mode = s->gr[0x05] & 0x7;
2107 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2108 *(s->vram_ptr + bank_offset) = mem_value;
2109 cpu_physical_memory_set_dirty(s->vram_offset +
2110 bank_offset);
2111 } else {
2112 if ((s->gr[0x0B] & 0x14) != 0x14) {
2113 cirrus_mem_writeb_mode4and5_8bpp(s, mode,
2114 bank_offset,
2115 mem_value);
2116 } else {
2117 cirrus_mem_writeb_mode4and5_16bpp(s, mode,
2118 bank_offset,
2119 mem_value);
2124 } else if (addr >= 0x18000 && addr < 0x18100) {
2125 /* memory-mapped I/O */
2126 if ((s->sr[0x17] & 0x44) == 0x04) {
2127 cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
2129 } else {
2130 #ifdef DEBUG_CIRRUS
2131 printf("cirrus: mem_writeb %06x value %02x\n", addr, mem_value);
2132 #endif
2136 static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2138 #ifdef TARGET_WORDS_BIGENDIAN
2139 cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
2140 cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
2141 #else
2142 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2143 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2144 #endif
2147 static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2149 #ifdef TARGET_WORDS_BIGENDIAN
2150 cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
2151 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2152 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2153 cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
2154 #else
2155 cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
2156 cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2157 cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2158 cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2159 #endif
2162 static CPUReadMemoryFunc *cirrus_vga_mem_read[3] = {
2163 cirrus_vga_mem_readb,
2164 cirrus_vga_mem_readw,
2165 cirrus_vga_mem_readl,
2168 static CPUWriteMemoryFunc *cirrus_vga_mem_write[3] = {
2169 cirrus_vga_mem_writeb,
2170 cirrus_vga_mem_writew,
2171 cirrus_vga_mem_writel,
2174 /***************************************
2176 * hardware cursor
2178 ***************************************/
2180 static inline void invalidate_cursor1(CirrusVGAState *s)
2182 if (s->last_hw_cursor_size) {
2183 vga_invalidate_scanlines((VGAState *)s,
2184 s->last_hw_cursor_y + s->last_hw_cursor_y_start,
2185 s->last_hw_cursor_y + s->last_hw_cursor_y_end);
2189 static inline void cirrus_cursor_compute_yrange(CirrusVGAState *s)
2191 const uint8_t *src;
2192 uint32_t content;
2193 int y, y_min, y_max;
2195 src = s->vram_ptr + s->real_vram_size - 16 * 1024;
2196 if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2197 src += (s->sr[0x13] & 0x3c) * 256;
2198 y_min = 64;
2199 y_max = -1;
2200 for(y = 0; y < 64; y++) {
2201 content = ((uint32_t *)src)[0] |
2202 ((uint32_t *)src)[1] |
2203 ((uint32_t *)src)[2] |
2204 ((uint32_t *)src)[3];
2205 if (content) {
2206 if (y < y_min)
2207 y_min = y;
2208 if (y > y_max)
2209 y_max = y;
2211 src += 16;
2213 } else {
2214 src += (s->sr[0x13] & 0x3f) * 256;
2215 y_min = 32;
2216 y_max = -1;
2217 for(y = 0; y < 32; y++) {
2218 content = ((uint32_t *)src)[0] |
2219 ((uint32_t *)(src + 128))[0];
2220 if (content) {
2221 if (y < y_min)
2222 y_min = y;
2223 if (y > y_max)
2224 y_max = y;
2226 src += 4;
2229 if (y_min > y_max) {
2230 s->last_hw_cursor_y_start = 0;
2231 s->last_hw_cursor_y_end = 0;
2232 } else {
2233 s->last_hw_cursor_y_start = y_min;
2234 s->last_hw_cursor_y_end = y_max + 1;
2238 /* NOTE: we do not currently handle the cursor bitmap change, so we
2239 update the cursor only if it moves. */
2240 static void cirrus_cursor_invalidate(VGAState *s1)
2242 CirrusVGAState *s = (CirrusVGAState *)s1;
2243 int size;
2245 if (!s->sr[0x12] & CIRRUS_CURSOR_SHOW) {
2246 size = 0;
2247 } else {
2248 if (s->sr[0x12] & CIRRUS_CURSOR_LARGE)
2249 size = 64;
2250 else
2251 size = 32;
2253 /* invalidate last cursor and new cursor if any change */
2254 if (s->last_hw_cursor_size != size ||
2255 s->last_hw_cursor_x != s->hw_cursor_x ||
2256 s->last_hw_cursor_y != s->hw_cursor_y) {
2258 invalidate_cursor1(s);
2260 s->last_hw_cursor_size = size;
2261 s->last_hw_cursor_x = s->hw_cursor_x;
2262 s->last_hw_cursor_y = s->hw_cursor_y;
2263 /* compute the real cursor min and max y */
2264 cirrus_cursor_compute_yrange(s);
2265 invalidate_cursor1(s);
2269 static void cirrus_cursor_draw_line(VGAState *s1, uint8_t *d1, int scr_y)
2271 CirrusVGAState *s = (CirrusVGAState *)s1;
2272 int w, h, bpp, x1, x2, poffset;
2273 unsigned int color0, color1;
2274 const uint8_t *palette, *src;
2275 uint32_t content;
2277 if (!(s->sr[0x12] & CIRRUS_CURSOR_SHOW))
2278 return;
2279 /* fast test to see if the cursor intersects with the scan line */
2280 if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2281 h = 64;
2282 } else {
2283 h = 32;
2285 if (scr_y < s->hw_cursor_y ||
2286 scr_y >= (s->hw_cursor_y + h))
2287 return;
2289 src = s->vram_ptr + s->real_vram_size - 16 * 1024;
2290 if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2291 src += (s->sr[0x13] & 0x3c) * 256;
2292 src += (scr_y - s->hw_cursor_y) * 16;
2293 poffset = 8;
2294 content = ((uint32_t *)src)[0] |
2295 ((uint32_t *)src)[1] |
2296 ((uint32_t *)src)[2] |
2297 ((uint32_t *)src)[3];
2298 } else {
2299 src += (s->sr[0x13] & 0x3f) * 256;
2300 src += (scr_y - s->hw_cursor_y) * 4;
2301 poffset = 128;
2302 content = ((uint32_t *)src)[0] |
2303 ((uint32_t *)(src + 128))[0];
2305 /* if nothing to draw, no need to continue */
2306 if (!content)
2307 return;
2308 w = h;
2310 x1 = s->hw_cursor_x;
2311 if (x1 >= s->last_scr_width)
2312 return;
2313 x2 = s->hw_cursor_x + w;
2314 if (x2 > s->last_scr_width)
2315 x2 = s->last_scr_width;
2316 w = x2 - x1;
2317 palette = s->cirrus_hidden_palette;
2318 color0 = s->rgb_to_pixel(c6_to_8(palette[0x0 * 3]),
2319 c6_to_8(palette[0x0 * 3 + 1]),
2320 c6_to_8(palette[0x0 * 3 + 2]));
2321 color1 = s->rgb_to_pixel(c6_to_8(palette[0xf * 3]),
2322 c6_to_8(palette[0xf * 3 + 1]),
2323 c6_to_8(palette[0xf * 3 + 2]));
2324 bpp = ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
2325 d1 += x1 * bpp;
2326 switch(ds_get_bits_per_pixel(s->ds)) {
2327 default:
2328 break;
2329 case 8:
2330 vga_draw_cursor_line_8(d1, src, poffset, w, color0, color1, 0xff);
2331 break;
2332 case 15:
2333 vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0x7fff);
2334 break;
2335 case 16:
2336 vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0xffff);
2337 break;
2338 case 32:
2339 vga_draw_cursor_line_32(d1, src, poffset, w, color0, color1, 0xffffff);
2340 break;
2344 /***************************************
2346 * LFB memory access
2348 ***************************************/
2350 static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2352 CirrusVGAState *s = (CirrusVGAState *) opaque;
2353 uint32_t ret;
2355 addr &= s->cirrus_addr_mask;
2357 if (((s->sr[0x17] & 0x44) == 0x44) &&
2358 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2359 /* memory-mapped I/O */
2360 ret = cirrus_mmio_blt_read(s, addr & 0xff);
2361 } else if (0) {
2362 /* XXX handle bitblt */
2363 ret = 0xff;
2364 } else {
2365 /* video memory */
2366 if ((s->gr[0x0B] & 0x14) == 0x14) {
2367 addr <<= 4;
2368 } else if (s->gr[0x0B] & 0x02) {
2369 addr <<= 3;
2371 addr &= s->cirrus_addr_mask;
2372 ret = *(s->vram_ptr + addr);
2375 return ret;
2378 static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2380 uint32_t v;
2381 #ifdef TARGET_WORDS_BIGENDIAN
2382 v = cirrus_linear_readb(opaque, addr) << 8;
2383 v |= cirrus_linear_readb(opaque, addr + 1);
2384 #else
2385 v = cirrus_linear_readb(opaque, addr);
2386 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2387 #endif
2388 return v;
2391 static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2393 uint32_t v;
2394 #ifdef TARGET_WORDS_BIGENDIAN
2395 v = cirrus_linear_readb(opaque, addr) << 24;
2396 v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2397 v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2398 v |= cirrus_linear_readb(opaque, addr + 3);
2399 #else
2400 v = cirrus_linear_readb(opaque, addr);
2401 v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2402 v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2403 v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2404 #endif
2405 return v;
2408 static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2409 uint32_t val)
2411 CirrusVGAState *s = (CirrusVGAState *) opaque;
2412 unsigned mode;
2414 addr &= s->cirrus_addr_mask;
2416 if (((s->sr[0x17] & 0x44) == 0x44) &&
2417 ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2418 /* memory-mapped I/O */
2419 cirrus_mmio_blt_write(s, addr & 0xff, val);
2420 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2421 /* bitblt */
2422 *s->cirrus_srcptr++ = (uint8_t) val;
2423 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2424 cirrus_bitblt_cputovideo_next(s);
2426 } else {
2427 /* video memory */
2428 if ((s->gr[0x0B] & 0x14) == 0x14) {
2429 addr <<= 4;
2430 } else if (s->gr[0x0B] & 0x02) {
2431 addr <<= 3;
2433 addr &= s->cirrus_addr_mask;
2435 mode = s->gr[0x05] & 0x7;
2436 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2437 *(s->vram_ptr + addr) = (uint8_t) val;
2438 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2439 } else {
2440 if ((s->gr[0x0B] & 0x14) != 0x14) {
2441 cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2442 } else {
2443 cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2449 static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2450 uint32_t val)
2452 #ifdef TARGET_WORDS_BIGENDIAN
2453 cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2454 cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2455 #else
2456 cirrus_linear_writeb(opaque, addr, val & 0xff);
2457 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2458 #endif
2461 static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2462 uint32_t val)
2464 #ifdef TARGET_WORDS_BIGENDIAN
2465 cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2466 cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2467 cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2468 cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2469 #else
2470 cirrus_linear_writeb(opaque, addr, val & 0xff);
2471 cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2472 cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2473 cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2474 #endif
2478 static CPUReadMemoryFunc *cirrus_linear_read[3] = {
2479 cirrus_linear_readb,
2480 cirrus_linear_readw,
2481 cirrus_linear_readl,
2484 static CPUWriteMemoryFunc *cirrus_linear_write[3] = {
2485 cirrus_linear_writeb,
2486 cirrus_linear_writew,
2487 cirrus_linear_writel,
2490 static void cirrus_linear_mem_writeb(void *opaque, target_phys_addr_t addr,
2491 uint32_t val)
2493 CirrusVGAState *s = (CirrusVGAState *) opaque;
2495 addr &= s->cirrus_addr_mask;
2496 *(s->vram_ptr + addr) = val;
2497 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2500 static void cirrus_linear_mem_writew(void *opaque, target_phys_addr_t addr,
2501 uint32_t val)
2503 CirrusVGAState *s = (CirrusVGAState *) opaque;
2505 addr &= s->cirrus_addr_mask;
2506 cpu_to_le16w((uint16_t *)(s->vram_ptr + addr), val);
2507 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2510 static void cirrus_linear_mem_writel(void *opaque, target_phys_addr_t addr,
2511 uint32_t val)
2513 CirrusVGAState *s = (CirrusVGAState *) opaque;
2515 addr &= s->cirrus_addr_mask;
2516 cpu_to_le32w((uint32_t *)(s->vram_ptr + addr), val);
2517 cpu_physical_memory_set_dirty(s->vram_offset + addr);
2520 /***************************************
2522 * system to screen memory access
2524 ***************************************/
2527 static uint32_t cirrus_linear_bitblt_readb(void *opaque, target_phys_addr_t addr)
2529 uint32_t ret;
2531 /* XXX handle bitblt */
2532 ret = 0xff;
2533 return ret;
2536 static uint32_t cirrus_linear_bitblt_readw(void *opaque, target_phys_addr_t addr)
2538 uint32_t v;
2539 #ifdef TARGET_WORDS_BIGENDIAN
2540 v = cirrus_linear_bitblt_readb(opaque, addr) << 8;
2541 v |= cirrus_linear_bitblt_readb(opaque, addr + 1);
2542 #else
2543 v = cirrus_linear_bitblt_readb(opaque, addr);
2544 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2545 #endif
2546 return v;
2549 static uint32_t cirrus_linear_bitblt_readl(void *opaque, target_phys_addr_t addr)
2551 uint32_t v;
2552 #ifdef TARGET_WORDS_BIGENDIAN
2553 v = cirrus_linear_bitblt_readb(opaque, addr) << 24;
2554 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 16;
2555 v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 8;
2556 v |= cirrus_linear_bitblt_readb(opaque, addr + 3);
2557 #else
2558 v = cirrus_linear_bitblt_readb(opaque, addr);
2559 v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2560 v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 16;
2561 v |= cirrus_linear_bitblt_readb(opaque, addr + 3) << 24;
2562 #endif
2563 return v;
2566 static void cirrus_linear_bitblt_writeb(void *opaque, target_phys_addr_t addr,
2567 uint32_t val)
2569 CirrusVGAState *s = (CirrusVGAState *) opaque;
2571 if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2572 /* bitblt */
2573 *s->cirrus_srcptr++ = (uint8_t) val;
2574 if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2575 cirrus_bitblt_cputovideo_next(s);
2580 static void cirrus_linear_bitblt_writew(void *opaque, target_phys_addr_t addr,
2581 uint32_t val)
2583 #ifdef TARGET_WORDS_BIGENDIAN
2584 cirrus_linear_bitblt_writeb(opaque, addr, (val >> 8) & 0xff);
2585 cirrus_linear_bitblt_writeb(opaque, addr + 1, val & 0xff);
2586 #else
2587 cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2588 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2589 #endif
2592 static void cirrus_linear_bitblt_writel(void *opaque, target_phys_addr_t addr,
2593 uint32_t val)
2595 #ifdef TARGET_WORDS_BIGENDIAN
2596 cirrus_linear_bitblt_writeb(opaque, addr, (val >> 24) & 0xff);
2597 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2598 cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2599 cirrus_linear_bitblt_writeb(opaque, addr + 3, val & 0xff);
2600 #else
2601 cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2602 cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2603 cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2604 cirrus_linear_bitblt_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2605 #endif
2609 static CPUReadMemoryFunc *cirrus_linear_bitblt_read[3] = {
2610 cirrus_linear_bitblt_readb,
2611 cirrus_linear_bitblt_readw,
2612 cirrus_linear_bitblt_readl,
2615 static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = {
2616 cirrus_linear_bitblt_writeb,
2617 cirrus_linear_bitblt_writew,
2618 cirrus_linear_bitblt_writel,
2621 /* Compute the memory access functions */
2622 static void cirrus_update_memory_access(CirrusVGAState *s)
2624 unsigned mode;
2626 if ((s->sr[0x17] & 0x44) == 0x44) {
2627 goto generic_io;
2628 } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2629 goto generic_io;
2630 } else {
2631 if ((s->gr[0x0B] & 0x14) == 0x14) {
2632 goto generic_io;
2633 } else if (s->gr[0x0B] & 0x02) {
2634 goto generic_io;
2637 mode = s->gr[0x05] & 0x7;
2638 if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2639 s->cirrus_linear_write[0] = cirrus_linear_mem_writeb;
2640 s->cirrus_linear_write[1] = cirrus_linear_mem_writew;
2641 s->cirrus_linear_write[2] = cirrus_linear_mem_writel;
2642 } else {
2643 generic_io:
2644 s->cirrus_linear_write[0] = cirrus_linear_writeb;
2645 s->cirrus_linear_write[1] = cirrus_linear_writew;
2646 s->cirrus_linear_write[2] = cirrus_linear_writel;
2652 /* I/O ports */
2654 static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
2656 CirrusVGAState *s = opaque;
2657 int val, index;
2659 /* check port range access depending on color/monochrome mode */
2660 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2661 || (addr >= 0x3d0 && addr <= 0x3df
2662 && !(s->msr & MSR_COLOR_EMULATION))) {
2663 val = 0xff;
2664 } else {
2665 switch (addr) {
2666 case 0x3c0:
2667 if (s->ar_flip_flop == 0) {
2668 val = s->ar_index;
2669 } else {
2670 val = 0;
2672 break;
2673 case 0x3c1:
2674 index = s->ar_index & 0x1f;
2675 if (index < 21)
2676 val = s->ar[index];
2677 else
2678 val = 0;
2679 break;
2680 case 0x3c2:
2681 val = s->st00;
2682 break;
2683 case 0x3c4:
2684 val = s->sr_index;
2685 break;
2686 case 0x3c5:
2687 if (cirrus_hook_read_sr(s, s->sr_index, &val))
2688 break;
2689 val = s->sr[s->sr_index];
2690 #ifdef DEBUG_VGA_REG
2691 printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2692 #endif
2693 break;
2694 case 0x3c6:
2695 cirrus_read_hidden_dac(s, &val);
2696 break;
2697 case 0x3c7:
2698 val = s->dac_state;
2699 break;
2700 case 0x3c8:
2701 val = s->dac_write_index;
2702 s->cirrus_hidden_dac_lockindex = 0;
2703 break;
2704 case 0x3c9:
2705 if (cirrus_hook_read_palette(s, &val))
2706 break;
2707 val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
2708 if (++s->dac_sub_index == 3) {
2709 s->dac_sub_index = 0;
2710 s->dac_read_index++;
2712 break;
2713 case 0x3ca:
2714 val = s->fcr;
2715 break;
2716 case 0x3cc:
2717 val = s->msr;
2718 break;
2719 case 0x3ce:
2720 val = s->gr_index;
2721 break;
2722 case 0x3cf:
2723 if (cirrus_hook_read_gr(s, s->gr_index, &val))
2724 break;
2725 val = s->gr[s->gr_index];
2726 #ifdef DEBUG_VGA_REG
2727 printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2728 #endif
2729 break;
2730 case 0x3b4:
2731 case 0x3d4:
2732 val = s->cr_index;
2733 break;
2734 case 0x3b5:
2735 case 0x3d5:
2736 if (cirrus_hook_read_cr(s, s->cr_index, &val))
2737 break;
2738 val = s->cr[s->cr_index];
2739 #ifdef DEBUG_VGA_REG
2740 printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2741 #endif
2742 break;
2743 case 0x3ba:
2744 case 0x3da:
2745 /* just toggle to fool polling */
2746 val = s->st01 = s->retrace((VGAState *) s);
2747 s->ar_flip_flop = 0;
2748 break;
2749 default:
2750 val = 0x00;
2751 break;
2754 #if defined(DEBUG_VGA)
2755 printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2756 #endif
2757 return val;
2760 static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2762 CirrusVGAState *s = opaque;
2763 int index;
2765 /* check port range access depending on color/monochrome mode */
2766 if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2767 || (addr >= 0x3d0 && addr <= 0x3df
2768 && !(s->msr & MSR_COLOR_EMULATION)))
2769 return;
2771 #ifdef DEBUG_VGA
2772 printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2773 #endif
2775 switch (addr) {
2776 case 0x3c0:
2777 if (s->ar_flip_flop == 0) {
2778 val &= 0x3f;
2779 s->ar_index = val;
2780 } else {
2781 index = s->ar_index & 0x1f;
2782 switch (index) {
2783 case 0x00 ... 0x0f:
2784 s->ar[index] = val & 0x3f;
2785 break;
2786 case 0x10:
2787 s->ar[index] = val & ~0x10;
2788 break;
2789 case 0x11:
2790 s->ar[index] = val;
2791 break;
2792 case 0x12:
2793 s->ar[index] = val & ~0xc0;
2794 break;
2795 case 0x13:
2796 s->ar[index] = val & ~0xf0;
2797 break;
2798 case 0x14:
2799 s->ar[index] = val & ~0xf0;
2800 break;
2801 default:
2802 break;
2805 s->ar_flip_flop ^= 1;
2806 break;
2807 case 0x3c2:
2808 s->msr = val & ~0x10;
2809 s->update_retrace_info((VGAState *) s);
2810 break;
2811 case 0x3c4:
2812 s->sr_index = val;
2813 break;
2814 case 0x3c5:
2815 if (cirrus_hook_write_sr(s, s->sr_index, val))
2816 break;
2817 #ifdef DEBUG_VGA_REG
2818 printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2819 #endif
2820 s->sr[s->sr_index] = val & sr_mask[s->sr_index];
2821 if (s->sr_index == 1) s->update_retrace_info((VGAState *) s);
2822 break;
2823 case 0x3c6:
2824 cirrus_write_hidden_dac(s, val);
2825 break;
2826 case 0x3c7:
2827 s->dac_read_index = val;
2828 s->dac_sub_index = 0;
2829 s->dac_state = 3;
2830 break;
2831 case 0x3c8:
2832 s->dac_write_index = val;
2833 s->dac_sub_index = 0;
2834 s->dac_state = 0;
2835 break;
2836 case 0x3c9:
2837 if (cirrus_hook_write_palette(s, val))
2838 break;
2839 s->dac_cache[s->dac_sub_index] = val;
2840 if (++s->dac_sub_index == 3) {
2841 memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
2842 s->dac_sub_index = 0;
2843 s->dac_write_index++;
2845 break;
2846 case 0x3ce:
2847 s->gr_index = val;
2848 break;
2849 case 0x3cf:
2850 if (cirrus_hook_write_gr(s, s->gr_index, val))
2851 break;
2852 #ifdef DEBUG_VGA_REG
2853 printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2854 #endif
2855 s->gr[s->gr_index] = val & gr_mask[s->gr_index];
2856 break;
2857 case 0x3b4:
2858 case 0x3d4:
2859 s->cr_index = val;
2860 break;
2861 case 0x3b5:
2862 case 0x3d5:
2863 if (cirrus_hook_write_cr(s, s->cr_index, val))
2864 break;
2865 #ifdef DEBUG_VGA_REG
2866 printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2867 #endif
2868 /* handle CR0-7 protection */
2869 if ((s->cr[0x11] & 0x80) && s->cr_index <= 7) {
2870 /* can always write bit 4 of CR7 */
2871 if (s->cr_index == 7)
2872 s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
2873 return;
2875 switch (s->cr_index) {
2876 case 0x01: /* horizontal display end */
2877 case 0x07:
2878 case 0x09:
2879 case 0x0c:
2880 case 0x0d:
2881 case 0x12: /* vertical display end */
2882 s->cr[s->cr_index] = val;
2883 break;
2885 default:
2886 s->cr[s->cr_index] = val;
2887 break;
2890 switch(s->cr_index) {
2891 case 0x00:
2892 case 0x04:
2893 case 0x05:
2894 case 0x06:
2895 case 0x07:
2896 case 0x11:
2897 case 0x17:
2898 s->update_retrace_info((VGAState *) s);
2899 break;
2901 break;
2902 case 0x3ba:
2903 case 0x3da:
2904 s->fcr = val & 0x10;
2905 break;
2909 /***************************************
2911 * memory-mapped I/O access
2913 ***************************************/
2915 static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2917 CirrusVGAState *s = (CirrusVGAState *) opaque;
2919 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2921 if (addr >= 0x100) {
2922 return cirrus_mmio_blt_read(s, addr - 0x100);
2923 } else {
2924 return vga_ioport_read(s, addr + 0x3c0);
2928 static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2930 uint32_t v;
2931 #ifdef TARGET_WORDS_BIGENDIAN
2932 v = cirrus_mmio_readb(opaque, addr) << 8;
2933 v |= cirrus_mmio_readb(opaque, addr + 1);
2934 #else
2935 v = cirrus_mmio_readb(opaque, addr);
2936 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2937 #endif
2938 return v;
2941 static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2943 uint32_t v;
2944 #ifdef TARGET_WORDS_BIGENDIAN
2945 v = cirrus_mmio_readb(opaque, addr) << 24;
2946 v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2947 v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2948 v |= cirrus_mmio_readb(opaque, addr + 3);
2949 #else
2950 v = cirrus_mmio_readb(opaque, addr);
2951 v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2952 v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2953 v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2954 #endif
2955 return v;
2958 static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2959 uint32_t val)
2961 CirrusVGAState *s = (CirrusVGAState *) opaque;
2963 addr &= CIRRUS_PNPMMIO_SIZE - 1;
2965 if (addr >= 0x100) {
2966 cirrus_mmio_blt_write(s, addr - 0x100, val);
2967 } else {
2968 vga_ioport_write(s, addr + 0x3c0, val);
2972 static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2973 uint32_t val)
2975 #ifdef TARGET_WORDS_BIGENDIAN
2976 cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2977 cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2978 #else
2979 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2980 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2981 #endif
2984 static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2985 uint32_t val)
2987 #ifdef TARGET_WORDS_BIGENDIAN
2988 cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2989 cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2990 cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2991 cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2992 #else
2993 cirrus_mmio_writeb(opaque, addr, val & 0xff);
2994 cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2995 cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2996 cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2997 #endif
3001 static CPUReadMemoryFunc *cirrus_mmio_read[3] = {
3002 cirrus_mmio_readb,
3003 cirrus_mmio_readw,
3004 cirrus_mmio_readl,
3007 static CPUWriteMemoryFunc *cirrus_mmio_write[3] = {
3008 cirrus_mmio_writeb,
3009 cirrus_mmio_writew,
3010 cirrus_mmio_writel,
3013 /* load/save state */
3015 static void cirrus_vga_save(QEMUFile *f, void *opaque)
3017 CirrusVGAState *s = opaque;
3019 if (s->pci_dev)
3020 pci_device_save(s->pci_dev, f);
3022 qemu_put_be32s(f, &s->latch);
3023 qemu_put_8s(f, &s->sr_index);
3024 qemu_put_buffer(f, s->sr, 256);
3025 qemu_put_8s(f, &s->gr_index);
3026 qemu_put_8s(f, &s->cirrus_shadow_gr0);
3027 qemu_put_8s(f, &s->cirrus_shadow_gr1);
3028 qemu_put_buffer(f, s->gr + 2, 254);
3029 qemu_put_8s(f, &s->ar_index);
3030 qemu_put_buffer(f, s->ar, 21);
3031 qemu_put_be32(f, s->ar_flip_flop);
3032 qemu_put_8s(f, &s->cr_index);
3033 qemu_put_buffer(f, s->cr, 256);
3034 qemu_put_8s(f, &s->msr);
3035 qemu_put_8s(f, &s->fcr);
3036 qemu_put_8s(f, &s->st00);
3037 qemu_put_8s(f, &s->st01);
3039 qemu_put_8s(f, &s->dac_state);
3040 qemu_put_8s(f, &s->dac_sub_index);
3041 qemu_put_8s(f, &s->dac_read_index);
3042 qemu_put_8s(f, &s->dac_write_index);
3043 qemu_put_buffer(f, s->dac_cache, 3);
3044 qemu_put_buffer(f, s->palette, 768);
3046 qemu_put_be32(f, s->bank_offset);
3048 qemu_put_8s(f, &s->cirrus_hidden_dac_lockindex);
3049 qemu_put_8s(f, &s->cirrus_hidden_dac_data);
3051 qemu_put_be32s(f, &s->hw_cursor_x);
3052 qemu_put_be32s(f, &s->hw_cursor_y);
3053 /* XXX: we do not save the bitblt state - we assume we do not save
3054 the state when the blitter is active */
3057 static int cirrus_vga_load(QEMUFile *f, void *opaque, int version_id)
3059 CirrusVGAState *s = opaque;
3060 int ret;
3062 if (version_id > 2)
3063 return -EINVAL;
3065 if (s->pci_dev && version_id >= 2) {
3066 ret = pci_device_load(s->pci_dev, f);
3067 if (ret < 0)
3068 return ret;
3071 qemu_get_be32s(f, &s->latch);
3072 qemu_get_8s(f, &s->sr_index);
3073 qemu_get_buffer(f, s->sr, 256);
3074 qemu_get_8s(f, &s->gr_index);
3075 qemu_get_8s(f, &s->cirrus_shadow_gr0);
3076 qemu_get_8s(f, &s->cirrus_shadow_gr1);
3077 s->gr[0x00] = s->cirrus_shadow_gr0 & 0x0f;
3078 s->gr[0x01] = s->cirrus_shadow_gr1 & 0x0f;
3079 qemu_get_buffer(f, s->gr + 2, 254);
3080 qemu_get_8s(f, &s->ar_index);
3081 qemu_get_buffer(f, s->ar, 21);
3082 s->ar_flip_flop=qemu_get_be32(f);
3083 qemu_get_8s(f, &s->cr_index);
3084 qemu_get_buffer(f, s->cr, 256);
3085 qemu_get_8s(f, &s->msr);
3086 qemu_get_8s(f, &s->fcr);
3087 qemu_get_8s(f, &s->st00);
3088 qemu_get_8s(f, &s->st01);
3090 qemu_get_8s(f, &s->dac_state);
3091 qemu_get_8s(f, &s->dac_sub_index);
3092 qemu_get_8s(f, &s->dac_read_index);
3093 qemu_get_8s(f, &s->dac_write_index);
3094 qemu_get_buffer(f, s->dac_cache, 3);
3095 qemu_get_buffer(f, s->palette, 768);
3097 s->bank_offset=qemu_get_be32(f);
3099 qemu_get_8s(f, &s->cirrus_hidden_dac_lockindex);
3100 qemu_get_8s(f, &s->cirrus_hidden_dac_data);
3102 qemu_get_be32s(f, &s->hw_cursor_x);
3103 qemu_get_be32s(f, &s->hw_cursor_y);
3105 /* force refresh */
3106 s->graphic_mode = -1;
3107 cirrus_update_bank_ptr(s, 0);
3108 cirrus_update_bank_ptr(s, 1);
3109 return 0;
3112 /***************************************
3114 * initialize
3116 ***************************************/
3118 static void cirrus_init_common(CirrusVGAState * s, int device_id, int is_pci)
3120 int i;
3121 static int inited;
3123 if (!inited) {
3124 inited = 1;
3125 for(i = 0;i < 256; i++)
3126 rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */
3127 rop_to_index[CIRRUS_ROP_0] = 0;
3128 rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;
3129 rop_to_index[CIRRUS_ROP_NOP] = 2;
3130 rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;
3131 rop_to_index[CIRRUS_ROP_NOTDST] = 4;
3132 rop_to_index[CIRRUS_ROP_SRC] = 5;
3133 rop_to_index[CIRRUS_ROP_1] = 6;
3134 rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;
3135 rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;
3136 rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;
3137 rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;
3138 rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;
3139 rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;
3140 rop_to_index[CIRRUS_ROP_NOTSRC] = 13;
3141 rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;
3142 rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;
3145 register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
3147 register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
3148 register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
3149 register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
3150 register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
3152 register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
3154 register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
3155 register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
3156 register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
3157 register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
3159 s->vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read,
3160 cirrus_vga_mem_write, s);
3161 cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
3162 s->vga_io_memory);
3164 s->sr[0x06] = 0x0f;
3165 if (device_id == CIRRUS_ID_CLGD5446) {
3166 /* 4MB 64 bit memory config, always PCI */
3167 s->sr[0x1F] = 0x2d; // MemClock
3168 s->gr[0x18] = 0x0f; // fastest memory configuration
3169 #if 1
3170 s->sr[0x0f] = 0x98;
3171 s->sr[0x17] = 0x20;
3172 s->sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */
3173 s->real_vram_size = 4096 * 1024;
3174 #else
3175 s->sr[0x0f] = 0x18;
3176 s->sr[0x17] = 0x20;
3177 s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
3178 s->real_vram_size = 2048 * 1024;
3179 #endif
3180 } else {
3181 s->sr[0x1F] = 0x22; // MemClock
3182 s->sr[0x0F] = CIRRUS_MEMSIZE_2M;
3183 if (is_pci)
3184 s->sr[0x17] = CIRRUS_BUSTYPE_PCI;
3185 else
3186 s->sr[0x17] = CIRRUS_BUSTYPE_ISA;
3187 s->real_vram_size = 2048 * 1024;
3188 s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
3190 s->cr[0x27] = device_id;
3192 /* Win2K seems to assume that the pattern buffer is at 0xff
3193 initially ! */
3194 memset(s->vram_ptr, 0xff, s->real_vram_size);
3196 s->cirrus_hidden_dac_lockindex = 5;
3197 s->cirrus_hidden_dac_data = 0;
3199 /* I/O handler for LFB */
3200 s->cirrus_linear_io_addr =
3201 cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,
3203 s->cirrus_linear_write = cpu_get_io_memory_write(s->cirrus_linear_io_addr);
3205 /* I/O handler for LFB */
3206 s->cirrus_linear_bitblt_io_addr =
3207 cpu_register_io_memory(0, cirrus_linear_bitblt_read, cirrus_linear_bitblt_write,
3210 /* I/O handler for memory-mapped I/O */
3211 s->cirrus_mmio_io_addr =
3212 cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);
3214 /* XXX: s->vram_size must be a power of two */
3215 s->cirrus_addr_mask = s->real_vram_size - 1;
3216 s->linear_mmio_mask = s->real_vram_size - 256;
3218 s->get_bpp = cirrus_get_bpp;
3219 s->get_offsets = cirrus_get_offsets;
3220 s->get_resolution = cirrus_get_resolution;
3221 s->cursor_invalidate = cirrus_cursor_invalidate;
3222 s->cursor_draw_line = cirrus_cursor_draw_line;
3224 register_savevm("cirrus_vga", 0, 2, cirrus_vga_save, cirrus_vga_load, s);
3227 /***************************************
3229 * ISA bus support
3231 ***************************************/
3233 void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base,
3234 ram_addr_t vga_ram_offset, int vga_ram_size)
3236 CirrusVGAState *s;
3238 s = qemu_mallocz(sizeof(CirrusVGAState));
3240 vga_common_init((VGAState *)s,
3241 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
3242 cirrus_init_common(s, CIRRUS_ID_CLGD5430, 0);
3243 s->console = graphic_console_init(s->ds, s->update, s->invalidate,
3244 s->screen_dump, s->text_update, s);
3245 /* XXX ISA-LFB support */
3248 /***************************************
3250 * PCI bus support
3252 ***************************************/
3254 static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
3255 uint32_t addr, uint32_t size, int type)
3257 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
3259 /* XXX: add byte swapping apertures */
3260 cpu_register_physical_memory(addr, s->vram_size,
3261 s->cirrus_linear_io_addr);
3262 cpu_register_physical_memory(addr + 0x1000000, 0x400000,
3263 s->cirrus_linear_bitblt_io_addr);
3266 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
3267 uint32_t addr, uint32_t size, int type)
3269 CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
3271 cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
3272 s->cirrus_mmio_io_addr);
3275 void pci_cirrus_vga_init(PCIBus *bus, DisplayState *ds, uint8_t *vga_ram_base,
3276 ram_addr_t vga_ram_offset, int vga_ram_size)
3278 PCICirrusVGAState *d;
3279 uint8_t *pci_conf;
3280 CirrusVGAState *s;
3281 int device_id;
3283 device_id = CIRRUS_ID_CLGD5446;
3285 /* setup PCI configuration registers */
3286 d = (PCICirrusVGAState *)pci_register_device(bus, "Cirrus VGA",
3287 sizeof(PCICirrusVGAState),
3288 -1, NULL, NULL);
3289 pci_conf = d->dev.config;
3290 pci_conf[0x00] = (uint8_t) (PCI_VENDOR_CIRRUS & 0xff);
3291 pci_conf[0x01] = (uint8_t) (PCI_VENDOR_CIRRUS >> 8);
3292 pci_conf[0x02] = (uint8_t) (device_id & 0xff);
3293 pci_conf[0x03] = (uint8_t) (device_id >> 8);
3294 pci_conf[0x04] = PCI_COMMAND_IOACCESS | PCI_COMMAND_MEMACCESS;
3295 pci_conf[0x0a] = PCI_CLASS_SUB_VGA;
3296 pci_conf[0x0b] = PCI_CLASS_BASE_DISPLAY;
3297 pci_conf[0x0e] = PCI_CLASS_HEADERTYPE_00h;
3299 /* setup VGA */
3300 s = &d->cirrus_vga;
3301 vga_common_init((VGAState *)s,
3302 ds, vga_ram_base, vga_ram_offset, vga_ram_size);
3303 cirrus_init_common(s, device_id, 1);
3305 s->console = graphic_console_init(s->ds, s->update, s->invalidate,
3306 s->screen_dump, s->text_update, s);
3308 s->pci_dev = (PCIDevice *)d;
3310 /* setup memory space */
3311 /* memory #0 LFB */
3312 /* memory #1 memory-mapped I/O */
3313 /* XXX: s->vram_size must be a power of two */
3314 pci_register_io_region((PCIDevice *)d, 0, 0x2000000,
3315 PCI_ADDRESS_SPACE_MEM_PREFETCH, cirrus_pci_lfb_map);
3316 if (device_id == CIRRUS_ID_CLGD5446) {
3317 pci_register_io_region((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
3318 PCI_ADDRESS_SPACE_MEM, cirrus_pci_mmio_map);
3320 /* XXX: ROM BIOS */