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