Statistics
| Branch: | Revision:

root / hw / cirrus_vga.c @ c9c0eae8

History | View | Annotate | Download (90.7 kB)

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 "vl.h"
30
#include "vga_int.h"
31

    
32
/*
33
 * TODO:
34
 *    - add support for WRITEMASK (GR2F)
35
 *    - optimize linear mappings
36
 *    - optimize bitblt functions
37
 */
38

    
39
//#define DEBUG_CIRRUS
40
//#define DEBUG_BITBLT
41

    
42
/***************************************
43
 *
44
 *  definitions
45
 *
46
 ***************************************/
47

    
48
#define qemu_MIN(a,b) ((a) < (b) ? (a) : (b))
49

    
50
// ID
51
#define CIRRUS_ID_CLGD5422  (0x23<<2)
52
#define CIRRUS_ID_CLGD5426  (0x24<<2)
53
#define CIRRUS_ID_CLGD5424  (0x25<<2)
54
#define CIRRUS_ID_CLGD5428  (0x26<<2)
55
#define CIRRUS_ID_CLGD5430  (0x28<<2)
56
#define CIRRUS_ID_CLGD5434  (0x2A<<2)
57
#define CIRRUS_ID_CLGD5436  (0x2B<<2)
58
#define CIRRUS_ID_CLGD5446  (0x2E<<2)
59

    
60
// sequencer 0x07
61
#define CIRRUS_SR7_BPP_VGA            0x00
62
#define CIRRUS_SR7_BPP_SVGA           0x01
63
#define CIRRUS_SR7_BPP_MASK           0x0e
64
#define CIRRUS_SR7_BPP_8              0x00
65
#define CIRRUS_SR7_BPP_16_DOUBLEVCLK  0x02
66
#define CIRRUS_SR7_BPP_24             0x04
67
#define CIRRUS_SR7_BPP_16             0x06
68
#define CIRRUS_SR7_BPP_32             0x08
69
#define CIRRUS_SR7_ISAADDR_MASK       0xe0
70

    
71
// sequencer 0x0f
72
#define CIRRUS_MEMSIZE_512k        0x08
73
#define CIRRUS_MEMSIZE_1M          0x10
74
#define CIRRUS_MEMSIZE_2M          0x18
75
#define CIRRUS_MEMFLAGS_BANKSWITCH 0x80        // bank switching is enabled.
76

    
77
// sequencer 0x12
78
#define CIRRUS_CURSOR_SHOW         0x01
79
#define CIRRUS_CURSOR_HIDDENPEL    0x02
80
#define CIRRUS_CURSOR_LARGE        0x04        // 64x64 if set, 32x32 if clear
81

    
82
// sequencer 0x17
83
#define CIRRUS_BUSTYPE_VLBFAST   0x10
84
#define CIRRUS_BUSTYPE_PCI       0x20
85
#define CIRRUS_BUSTYPE_VLBSLOW   0x30
86
#define CIRRUS_BUSTYPE_ISA       0x38
87
#define CIRRUS_MMIO_ENABLE       0x04
88
#define CIRRUS_MMIO_USE_PCIADDR  0x40        // 0xb8000 if cleared.
89
#define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
90

    
91
// control 0x0b
92
#define CIRRUS_BANKING_DUAL             0x01
93
#define CIRRUS_BANKING_GRANULARITY_16K  0x20        // set:16k, clear:4k
94

    
95
// control 0x30
96
#define CIRRUS_BLTMODE_BACKWARDS        0x01
97
#define CIRRUS_BLTMODE_MEMSYSDEST       0x02
98
#define CIRRUS_BLTMODE_MEMSYSSRC        0x04
99
#define CIRRUS_BLTMODE_TRANSPARENTCOMP  0x08
100
#define CIRRUS_BLTMODE_PATTERNCOPY      0x40
101
#define CIRRUS_BLTMODE_COLOREXPAND      0x80
102
#define CIRRUS_BLTMODE_PIXELWIDTHMASK   0x30
103
#define CIRRUS_BLTMODE_PIXELWIDTH8      0x00
104
#define CIRRUS_BLTMODE_PIXELWIDTH16     0x10
105
#define CIRRUS_BLTMODE_PIXELWIDTH24     0x20
106
#define CIRRUS_BLTMODE_PIXELWIDTH32     0x30
107

    
108
// control 0x31
109
#define CIRRUS_BLT_BUSY                 0x01
110
#define CIRRUS_BLT_START                0x02
111
#define CIRRUS_BLT_RESET                0x04
112
#define CIRRUS_BLT_FIFOUSED             0x10
113
#define CIRRUS_BLT_AUTOSTART            0x80
114

    
115
// control 0x32
116
#define CIRRUS_ROP_0                    0x00
117
#define CIRRUS_ROP_SRC_AND_DST          0x05
118
#define CIRRUS_ROP_NOP                  0x06
119
#define CIRRUS_ROP_SRC_AND_NOTDST       0x09
120
#define CIRRUS_ROP_NOTDST               0x0b
121
#define CIRRUS_ROP_SRC                  0x0d
122
#define CIRRUS_ROP_1                    0x0e
123
#define CIRRUS_ROP_NOTSRC_AND_DST       0x50
124
#define CIRRUS_ROP_SRC_XOR_DST          0x59
125
#define CIRRUS_ROP_SRC_OR_DST           0x6d
126
#define CIRRUS_ROP_NOTSRC_OR_NOTDST     0x90
127
#define CIRRUS_ROP_SRC_NOTXOR_DST       0x95
128
#define CIRRUS_ROP_SRC_OR_NOTDST        0xad
129
#define CIRRUS_ROP_NOTSRC               0xd0
130
#define CIRRUS_ROP_NOTSRC_OR_DST        0xd6
131
#define CIRRUS_ROP_NOTSRC_AND_NOTDST    0xda
132

    
133
#define CIRRUS_ROP_NOP_INDEX 2
134
#define CIRRUS_ROP_SRC_INDEX 5
135

    
136
// control 0x33
137
#define CIRRUS_BLTMODEEXT_SOLIDFILL        0x04
138
#define CIRRUS_BLTMODEEXT_COLOREXPINV      0x02
139
#define CIRRUS_BLTMODEEXT_DWORDGRANULARITY 0x01
140

    
141
// memory-mapped IO
142
#define CIRRUS_MMIO_BLTBGCOLOR        0x00        // dword
143
#define CIRRUS_MMIO_BLTFGCOLOR        0x04        // dword
144
#define CIRRUS_MMIO_BLTWIDTH          0x08        // word
145
#define CIRRUS_MMIO_BLTHEIGHT         0x0a        // word
146
#define CIRRUS_MMIO_BLTDESTPITCH      0x0c        // word
147
#define CIRRUS_MMIO_BLTSRCPITCH       0x0e        // word
148
#define CIRRUS_MMIO_BLTDESTADDR       0x10        // dword
149
#define CIRRUS_MMIO_BLTSRCADDR        0x14        // dword
150
#define CIRRUS_MMIO_BLTWRITEMASK      0x17        // byte
151
#define CIRRUS_MMIO_BLTMODE           0x18        // byte
152
#define CIRRUS_MMIO_BLTROP            0x1a        // byte
153
#define CIRRUS_MMIO_BLTMODEEXT        0x1b        // byte
154
#define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c        // word?
155
#define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20        // word?
156
#define CIRRUS_MMIO_LINEARDRAW_START_X 0x24        // word
157
#define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26        // word
158
#define CIRRUS_MMIO_LINEARDRAW_END_X  0x28        // word
159
#define CIRRUS_MMIO_LINEARDRAW_END_Y  0x2a        // word
160
#define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c        // byte
161
#define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d        // byte
162
#define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e        // byte
163
#define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f        // byte
164
#define CIRRUS_MMIO_BRESENHAM_K1      0x30        // word
165
#define CIRRUS_MMIO_BRESENHAM_K3      0x32        // word
166
#define CIRRUS_MMIO_BRESENHAM_ERROR   0x34        // word
167
#define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36        // word
168
#define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38        // byte
169
#define CIRRUS_MMIO_LINEDRAW_MODE     0x39        // byte
170
#define CIRRUS_MMIO_BLTSTATUS         0x40        // byte
171

    
172
// PCI 0x00: vendor, 0x02: device
173
#define PCI_VENDOR_CIRRUS             0x1013
174
#define PCI_DEVICE_CLGD5462           0x00d0
175
#define PCI_DEVICE_CLGD5465           0x00d6
176

    
177
// PCI 0x04: command(word), 0x06(word): status
178
#define PCI_COMMAND_IOACCESS                0x0001
179
#define PCI_COMMAND_MEMACCESS               0x0002
180
#define PCI_COMMAND_BUSMASTER               0x0004
181
#define PCI_COMMAND_SPECIALCYCLE            0x0008
182
#define PCI_COMMAND_MEMWRITEINVALID         0x0010
183
#define PCI_COMMAND_PALETTESNOOPING         0x0020
184
#define PCI_COMMAND_PARITYDETECTION         0x0040
185
#define PCI_COMMAND_ADDRESSDATASTEPPING     0x0080
186
#define PCI_COMMAND_SERR                    0x0100
187
#define PCI_COMMAND_BACKTOBACKTRANS         0x0200
188
// PCI 0x08, 0xff000000 (0x09-0x0b:class,0x08:rev)
189
#define PCI_CLASS_BASE_DISPLAY        0x03
190
// PCI 0x08, 0x00ff0000
191
#define PCI_CLASS_SUB_VGA             0x00
192
// PCI 0x0c, 0x00ff0000 (0x0c:cacheline,0x0d:latency,0x0e:headertype,0x0f:Built-in self test)
193
#define PCI_CLASS_HEADERTYPE_00h  0x00
194
// 0x10-0x3f (headertype 00h)
195
// PCI 0x10,0x14,0x18,0x1c,0x20,0x24: base address mapping registers
196
//   0x10: MEMBASE, 0x14: IOBASE(hard-coded in XFree86 3.x)
197
#define PCI_MAP_MEM                 0x0
198
#define PCI_MAP_IO                  0x1
199
#define PCI_MAP_MEM_ADDR_MASK       (~0xf)
200
#define PCI_MAP_IO_ADDR_MASK        (~0x3)
201
#define PCI_MAP_MEMFLAGS_32BIT      0x0
202
#define PCI_MAP_MEMFLAGS_32BIT_1M   0x1
203
#define PCI_MAP_MEMFLAGS_64BIT      0x4
204
#define PCI_MAP_MEMFLAGS_CACHEABLE  0x8
205
// PCI 0x28: cardbus CIS pointer
206
// PCI 0x2c: subsystem vendor id, 0x2e: subsystem id
207
// PCI 0x30: expansion ROM base address
208
#define PCI_ROMBIOS_ENABLED         0x1
209
// PCI 0x34: 0xffffff00=reserved, 0x000000ff=capabilities pointer
210
// PCI 0x38: reserved
211
// PCI 0x3c: 0x3c=int-line, 0x3d=int-pin, 0x3e=min-gnt, 0x3f=maax-lat
212

    
213
#define CIRRUS_PNPMMIO_SIZE         0x1000
214

    
215

    
216
/* I/O and memory hook */
217
#define CIRRUS_HOOK_NOT_HANDLED 0
218
#define CIRRUS_HOOK_HANDLED 1
219

    
220
struct CirrusVGAState;
221
typedef void (*cirrus_bitblt_rop_t) (struct CirrusVGAState *s,
222
                                     uint8_t * dst, const uint8_t * src,
223
                                     int dstpitch, int srcpitch,
224
                                     int bltwidth, int bltheight);
225
typedef void (*cirrus_fill_t)(struct CirrusVGAState *s,
226
                              uint8_t *dst, int dst_pitch, int width, int height);
227

    
228
typedef struct CirrusVGAState {
229
    VGA_STATE_COMMON
230

    
231
    int cirrus_linear_io_addr;
232
    int cirrus_linear_bitblt_io_addr;
233
    int cirrus_mmio_io_addr;
234
    uint32_t cirrus_addr_mask;
235
    uint32_t linear_mmio_mask;
236
    uint8_t cirrus_shadow_gr0;
237
    uint8_t cirrus_shadow_gr1;
238
    uint8_t cirrus_hidden_dac_lockindex;
239
    uint8_t cirrus_hidden_dac_data;
240
    uint32_t cirrus_bank_base[2];
241
    uint32_t cirrus_bank_limit[2];
242
    uint8_t cirrus_hidden_palette[48];
243
    uint32_t hw_cursor_x;
244
    uint32_t hw_cursor_y;
245
    int cirrus_blt_pixelwidth;
246
    int cirrus_blt_width;
247
    int cirrus_blt_height;
248
    int cirrus_blt_dstpitch;
249
    int cirrus_blt_srcpitch;
250
    uint32_t cirrus_blt_fgcol;
251
    uint32_t cirrus_blt_bgcol;
252
    uint32_t cirrus_blt_dstaddr;
253
    uint32_t cirrus_blt_srcaddr;
254
    uint8_t cirrus_blt_mode;
255
    uint8_t cirrus_blt_modeext;
256
    cirrus_bitblt_rop_t cirrus_rop;
257
#define CIRRUS_BLTBUFSIZE (2048 * 4) /* one line width */
258
    uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
259
    uint8_t *cirrus_srcptr;
260
    uint8_t *cirrus_srcptr_end;
261
    uint32_t cirrus_srccounter;
262
    /* hwcursor display state */
263
    int last_hw_cursor_size;
264
    int last_hw_cursor_x;
265
    int last_hw_cursor_y;
266
    int last_hw_cursor_y_start;
267
    int last_hw_cursor_y_end;
268
    int real_vram_size; /* XXX: suppress that */
269
    CPUWriteMemoryFunc **cirrus_linear_write;
270
} CirrusVGAState;
271

    
272
typedef struct PCICirrusVGAState {
273
    PCIDevice dev;
274
    CirrusVGAState cirrus_vga;
275
} PCICirrusVGAState;
276

    
277
static uint8_t rop_to_index[256];
278
    
279
/***************************************
280
 *
281
 *  prototypes.
282
 *
283
 ***************************************/
284

    
285

    
286
static void cirrus_bitblt_reset(CirrusVGAState *s);
287
static void cirrus_update_memory_access(CirrusVGAState *s);
288

    
289
/***************************************
290
 *
291
 *  raster operations
292
 *
293
 ***************************************/
294

    
295
static void cirrus_bitblt_rop_nop(CirrusVGAState *s,
296
                                  uint8_t *dst,const uint8_t *src,
297
                                  int dstpitch,int srcpitch,
298
                                  int bltwidth,int bltheight)
299
{
300
}
301

    
302
static void cirrus_bitblt_fill_nop(CirrusVGAState *s,
303
                                   uint8_t *dst,
304
                                   int dstpitch, int bltwidth,int bltheight)
305
{
306
}
307

    
308
#define ROP_NAME 0
309
#define ROP_OP(d, s) d = 0
310
#include "cirrus_vga_rop.h"
311

    
312
#define ROP_NAME src_and_dst
313
#define ROP_OP(d, s) d = (s) & (d)
314
#include "cirrus_vga_rop.h"
315

    
316
#define ROP_NAME src_and_notdst
317
#define ROP_OP(d, s) d = (s) & (~(d))
318
#include "cirrus_vga_rop.h"
319

    
320
#define ROP_NAME notdst
321
#define ROP_OP(d, s) d = ~(d)
322
#include "cirrus_vga_rop.h"
323

    
324
#define ROP_NAME src
325
#define ROP_OP(d, s) d = s
326
#include "cirrus_vga_rop.h"
327

    
328
#define ROP_NAME 1
329
#define ROP_OP(d, s) d = ~0
330
#include "cirrus_vga_rop.h"
331

    
332
#define ROP_NAME notsrc_and_dst
333
#define ROP_OP(d, s) d = (~(s)) & (d)
334
#include "cirrus_vga_rop.h"
335

    
336
#define ROP_NAME src_xor_dst
337
#define ROP_OP(d, s) d = (s) ^ (d)
338
#include "cirrus_vga_rop.h"
339

    
340
#define ROP_NAME src_or_dst
341
#define ROP_OP(d, s) d = (s) | (d)
342
#include "cirrus_vga_rop.h"
343

    
344
#define ROP_NAME notsrc_or_notdst
345
#define ROP_OP(d, s) d = (~(s)) | (~(d))
346
#include "cirrus_vga_rop.h"
347

    
348
#define ROP_NAME src_notxor_dst
349
#define ROP_OP(d, s) d = ~((s) ^ (d))
350
#include "cirrus_vga_rop.h"
351

    
352
#define ROP_NAME src_or_notdst
353
#define ROP_OP(d, s) d = (s) | (~(d))
354
#include "cirrus_vga_rop.h"
355

    
356
#define ROP_NAME notsrc
357
#define ROP_OP(d, s) d = (~(s))
358
#include "cirrus_vga_rop.h"
359

    
360
#define ROP_NAME notsrc_or_dst
361
#define ROP_OP(d, s) d = (~(s)) | (d)
362
#include "cirrus_vga_rop.h"
363

    
364
#define ROP_NAME notsrc_and_notdst
365
#define ROP_OP(d, s) d = (~(s)) & (~(d))
366
#include "cirrus_vga_rop.h"
367

    
368
static const cirrus_bitblt_rop_t cirrus_fwd_rop[16] = {
369
    cirrus_bitblt_rop_fwd_0,
370
    cirrus_bitblt_rop_fwd_src_and_dst,
371
    cirrus_bitblt_rop_nop,
372
    cirrus_bitblt_rop_fwd_src_and_notdst,
373
    cirrus_bitblt_rop_fwd_notdst,
374
    cirrus_bitblt_rop_fwd_src,
375
    cirrus_bitblt_rop_fwd_1,
376
    cirrus_bitblt_rop_fwd_notsrc_and_dst,
377
    cirrus_bitblt_rop_fwd_src_xor_dst,
378
    cirrus_bitblt_rop_fwd_src_or_dst,
379
    cirrus_bitblt_rop_fwd_notsrc_or_notdst,
380
    cirrus_bitblt_rop_fwd_src_notxor_dst,
381
    cirrus_bitblt_rop_fwd_src_or_notdst,
382
    cirrus_bitblt_rop_fwd_notsrc,
383
    cirrus_bitblt_rop_fwd_notsrc_or_dst,
384
    cirrus_bitblt_rop_fwd_notsrc_and_notdst,
385
};
386

    
387
static const cirrus_bitblt_rop_t cirrus_bkwd_rop[16] = {
388
    cirrus_bitblt_rop_bkwd_0,
389
    cirrus_bitblt_rop_bkwd_src_and_dst,
390
    cirrus_bitblt_rop_nop,
391
    cirrus_bitblt_rop_bkwd_src_and_notdst,
392
    cirrus_bitblt_rop_bkwd_notdst,
393
    cirrus_bitblt_rop_bkwd_src,
394
    cirrus_bitblt_rop_bkwd_1,
395
    cirrus_bitblt_rop_bkwd_notsrc_and_dst,
396
    cirrus_bitblt_rop_bkwd_src_xor_dst,
397
    cirrus_bitblt_rop_bkwd_src_or_dst,
398
    cirrus_bitblt_rop_bkwd_notsrc_or_notdst,
399
    cirrus_bitblt_rop_bkwd_src_notxor_dst,
400
    cirrus_bitblt_rop_bkwd_src_or_notdst,
401
    cirrus_bitblt_rop_bkwd_notsrc,
402
    cirrus_bitblt_rop_bkwd_notsrc_or_dst,
403
    cirrus_bitblt_rop_bkwd_notsrc_and_notdst,
404
};
405
    
406
#define ROP2(name) {\
407
    name ## _8,\
408
    name ## _16,\
409
    name ## _24,\
410
    name ## _32,\
411
        }
412

    
413
#define ROP_NOP2(func) {\
414
    func,\
415
    func,\
416
    func,\
417
    func,\
418
        }
419

    
420
static const cirrus_bitblt_rop_t cirrus_patternfill[16][4] = {
421
    ROP2(cirrus_patternfill_0),
422
    ROP2(cirrus_patternfill_src_and_dst),
423
    ROP_NOP2(cirrus_bitblt_rop_nop),
424
    ROP2(cirrus_patternfill_src_and_notdst),
425
    ROP2(cirrus_patternfill_notdst),
426
    ROP2(cirrus_patternfill_src),
427
    ROP2(cirrus_patternfill_1),
428
    ROP2(cirrus_patternfill_notsrc_and_dst),
429
    ROP2(cirrus_patternfill_src_xor_dst),
430
    ROP2(cirrus_patternfill_src_or_dst),
431
    ROP2(cirrus_patternfill_notsrc_or_notdst),
432
    ROP2(cirrus_patternfill_src_notxor_dst),
433
    ROP2(cirrus_patternfill_src_or_notdst),
434
    ROP2(cirrus_patternfill_notsrc),
435
    ROP2(cirrus_patternfill_notsrc_or_dst),
436
    ROP2(cirrus_patternfill_notsrc_and_notdst),
437
};
438

    
439
static const cirrus_bitblt_rop_t cirrus_colorexpand_transp[16][4] = {
440
    ROP2(cirrus_colorexpand_transp_0),
441
    ROP2(cirrus_colorexpand_transp_src_and_dst),
442
    ROP_NOP2(cirrus_bitblt_rop_nop),
443
    ROP2(cirrus_colorexpand_transp_src_and_notdst),
444
    ROP2(cirrus_colorexpand_transp_notdst),
445
    ROP2(cirrus_colorexpand_transp_src),
446
    ROP2(cirrus_colorexpand_transp_1),
447
    ROP2(cirrus_colorexpand_transp_notsrc_and_dst),
448
    ROP2(cirrus_colorexpand_transp_src_xor_dst),
449
    ROP2(cirrus_colorexpand_transp_src_or_dst),
450
    ROP2(cirrus_colorexpand_transp_notsrc_or_notdst),
451
    ROP2(cirrus_colorexpand_transp_src_notxor_dst),
452
    ROP2(cirrus_colorexpand_transp_src_or_notdst),
453
    ROP2(cirrus_colorexpand_transp_notsrc),
454
    ROP2(cirrus_colorexpand_transp_notsrc_or_dst),
455
    ROP2(cirrus_colorexpand_transp_notsrc_and_notdst),
456
};
457

    
458
static const cirrus_bitblt_rop_t cirrus_colorexpand[16][4] = {
459
    ROP2(cirrus_colorexpand_0),
460
    ROP2(cirrus_colorexpand_src_and_dst),
461
    ROP_NOP2(cirrus_bitblt_rop_nop),
462
    ROP2(cirrus_colorexpand_src_and_notdst),
463
    ROP2(cirrus_colorexpand_notdst),
464
    ROP2(cirrus_colorexpand_src),
465
    ROP2(cirrus_colorexpand_1),
466
    ROP2(cirrus_colorexpand_notsrc_and_dst),
467
    ROP2(cirrus_colorexpand_src_xor_dst),
468
    ROP2(cirrus_colorexpand_src_or_dst),
469
    ROP2(cirrus_colorexpand_notsrc_or_notdst),
470
    ROP2(cirrus_colorexpand_src_notxor_dst),
471
    ROP2(cirrus_colorexpand_src_or_notdst),
472
    ROP2(cirrus_colorexpand_notsrc),
473
    ROP2(cirrus_colorexpand_notsrc_or_dst),
474
    ROP2(cirrus_colorexpand_notsrc_and_notdst),
475
};
476

    
477
static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern_transp[16][4] = {
478
    ROP2(cirrus_colorexpand_pattern_transp_0),
479
    ROP2(cirrus_colorexpand_pattern_transp_src_and_dst),
480
    ROP_NOP2(cirrus_bitblt_rop_nop),
481
    ROP2(cirrus_colorexpand_pattern_transp_src_and_notdst),
482
    ROP2(cirrus_colorexpand_pattern_transp_notdst),
483
    ROP2(cirrus_colorexpand_pattern_transp_src),
484
    ROP2(cirrus_colorexpand_pattern_transp_1),
485
    ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_dst),
486
    ROP2(cirrus_colorexpand_pattern_transp_src_xor_dst),
487
    ROP2(cirrus_colorexpand_pattern_transp_src_or_dst),
488
    ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_notdst),
489
    ROP2(cirrus_colorexpand_pattern_transp_src_notxor_dst),
490
    ROP2(cirrus_colorexpand_pattern_transp_src_or_notdst),
491
    ROP2(cirrus_colorexpand_pattern_transp_notsrc),
492
    ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_dst),
493
    ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_notdst),
494
};
495

    
496
static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern[16][4] = {
497
    ROP2(cirrus_colorexpand_pattern_0),
498
    ROP2(cirrus_colorexpand_pattern_src_and_dst),
499
    ROP_NOP2(cirrus_bitblt_rop_nop),
500
    ROP2(cirrus_colorexpand_pattern_src_and_notdst),
501
    ROP2(cirrus_colorexpand_pattern_notdst),
502
    ROP2(cirrus_colorexpand_pattern_src),
503
    ROP2(cirrus_colorexpand_pattern_1),
504
    ROP2(cirrus_colorexpand_pattern_notsrc_and_dst),
505
    ROP2(cirrus_colorexpand_pattern_src_xor_dst),
506
    ROP2(cirrus_colorexpand_pattern_src_or_dst),
507
    ROP2(cirrus_colorexpand_pattern_notsrc_or_notdst),
508
    ROP2(cirrus_colorexpand_pattern_src_notxor_dst),
509
    ROP2(cirrus_colorexpand_pattern_src_or_notdst),
510
    ROP2(cirrus_colorexpand_pattern_notsrc),
511
    ROP2(cirrus_colorexpand_pattern_notsrc_or_dst),
512
    ROP2(cirrus_colorexpand_pattern_notsrc_and_notdst),
513
};
514

    
515
static const cirrus_fill_t cirrus_fill[16][4] = {
516
    ROP2(cirrus_fill_0),
517
    ROP2(cirrus_fill_src_and_dst),
518
    ROP_NOP2(cirrus_bitblt_fill_nop),
519
    ROP2(cirrus_fill_src_and_notdst),
520
    ROP2(cirrus_fill_notdst),
521
    ROP2(cirrus_fill_src),
522
    ROP2(cirrus_fill_1),
523
    ROP2(cirrus_fill_notsrc_and_dst),
524
    ROP2(cirrus_fill_src_xor_dst),
525
    ROP2(cirrus_fill_src_or_dst),
526
    ROP2(cirrus_fill_notsrc_or_notdst),
527
    ROP2(cirrus_fill_src_notxor_dst),
528
    ROP2(cirrus_fill_src_or_notdst),
529
    ROP2(cirrus_fill_notsrc),
530
    ROP2(cirrus_fill_notsrc_or_dst),
531
    ROP2(cirrus_fill_notsrc_and_notdst),
532
};
533

    
534
static inline void cirrus_bitblt_fgcol(CirrusVGAState *s)
535
{
536
    unsigned int color;
537
    switch (s->cirrus_blt_pixelwidth) {
538
    case 1:
539
        s->cirrus_blt_fgcol = s->cirrus_shadow_gr1;
540
        break;
541
    case 2:
542
        color = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8);
543
        s->cirrus_blt_fgcol = le16_to_cpu(color);
544
        break;
545
    case 3:
546
        s->cirrus_blt_fgcol = s->cirrus_shadow_gr1 | 
547
            (s->gr[0x11] << 8) | (s->gr[0x13] << 16);
548
        break;
549
    default:
550
    case 4:
551
        color = s->cirrus_shadow_gr1 | (s->gr[0x11] << 8) |
552
            (s->gr[0x13] << 16) | (s->gr[0x15] << 24);
553
        s->cirrus_blt_fgcol = le32_to_cpu(color);
554
        break;
555
    }
556
}
557

    
558
static inline void cirrus_bitblt_bgcol(CirrusVGAState *s)
559
{
560
    unsigned int color;
561
    switch (s->cirrus_blt_pixelwidth) {
562
    case 1:
563
        s->cirrus_blt_bgcol = s->cirrus_shadow_gr0;
564
        break;
565
    case 2:
566
        color = s->cirrus_shadow_gr0 | (s->gr[0x10] << 8);
567
        s->cirrus_blt_bgcol = le16_to_cpu(color);
568
        break;
569
    case 3:
570
        s->cirrus_blt_bgcol = s->cirrus_shadow_gr0 | 
571
            (s->gr[0x10] << 8) | (s->gr[0x12] << 16);
572
        break;
573
    default:
574
    case 4:
575
        color = s->cirrus_shadow_gr0 | (s->gr[0x10] << 8) |
576
            (s->gr[0x12] << 16) | (s->gr[0x14] << 24);
577
        s->cirrus_blt_bgcol = le32_to_cpu(color);
578
        break;
579
    }
580
}
581

    
582
static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
583
                                     int off_pitch, int bytesperline,
584
                                     int lines)
585
{
586
    int y;
587
    int off_cur;
588
    int off_cur_end;
589

    
590
    for (y = 0; y < lines; y++) {
591
        off_cur = off_begin;
592
        off_cur_end = off_cur + bytesperline;
593
        off_cur &= TARGET_PAGE_MASK;
594
        while (off_cur < off_cur_end) {
595
            cpu_physical_memory_set_dirty(s->vram_offset + off_cur);
596
            off_cur += TARGET_PAGE_SIZE;
597
        }
598
        off_begin += off_pitch;
599
    }
600
}
601

    
602
static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
603
                                            const uint8_t * src)
604
{
605
    uint8_t *dst;
606

    
607
    dst = s->vram_ptr + s->cirrus_blt_dstaddr;
608
    (*s->cirrus_rop) (s, dst, src,
609
                      s->cirrus_blt_dstpitch, 0, 
610
                      s->cirrus_blt_width, s->cirrus_blt_height);
611
    cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
612
                             s->cirrus_blt_dstpitch, s->cirrus_blt_width,
613
                             s->cirrus_blt_height);
614
    return 1;
615
}
616

    
617
/* fill */
618

    
619
static int cirrus_bitblt_solidfill(CirrusVGAState *s, int blt_rop)
620
{
621
    cirrus_fill_t rop_func;
622

    
623
    rop_func = cirrus_fill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
624
    rop_func(s, s->vram_ptr + s->cirrus_blt_dstaddr, 
625
             s->cirrus_blt_dstpitch,
626
             s->cirrus_blt_width, s->cirrus_blt_height);
627
    cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
628
                             s->cirrus_blt_dstpitch, s->cirrus_blt_width,
629
                             s->cirrus_blt_height);
630
    cirrus_bitblt_reset(s);
631
    return 1;
632
}
633

    
634
/***************************************
635
 *
636
 *  bitblt (video-to-video)
637
 *
638
 ***************************************/
639

    
640
static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
641
{
642
    return cirrus_bitblt_common_patterncopy(s,
643
                                            s->vram_ptr + 
644
                                            (s->cirrus_blt_srcaddr & ~7));
645
}
646

    
647
static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
648
{
649
    (*s->cirrus_rop) (s, s->vram_ptr + s->cirrus_blt_dstaddr,
650
                      s->vram_ptr + s->cirrus_blt_srcaddr,
651
                      s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
652
                      s->cirrus_blt_width, s->cirrus_blt_height);
653
    cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
654
                             s->cirrus_blt_dstpitch, s->cirrus_blt_width,
655
                             s->cirrus_blt_height);
656
    return 1;
657
}
658

    
659
/***************************************
660
 *
661
 *  bitblt (cpu-to-video)
662
 *
663
 ***************************************/
664

    
665
static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
666
{
667
    int copy_count;
668
    uint8_t *end_ptr;
669
    
670
    if (s->cirrus_srccounter > 0) {
671
        if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
672
            cirrus_bitblt_common_patterncopy(s, s->cirrus_bltbuf);
673
        the_end:
674
            s->cirrus_srccounter = 0;
675
            cirrus_bitblt_reset(s);
676
        } else {
677
            /* at least one scan line */
678
            do {
679
                (*s->cirrus_rop)(s, s->vram_ptr + s->cirrus_blt_dstaddr,
680
                                 s->cirrus_bltbuf, 0, 0, s->cirrus_blt_width, 1);
681
                cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
682
                                         s->cirrus_blt_width, 1);
683
                s->cirrus_blt_dstaddr += s->cirrus_blt_dstpitch;
684
                s->cirrus_srccounter -= s->cirrus_blt_srcpitch;
685
                if (s->cirrus_srccounter <= 0)
686
                    goto the_end;
687
                /* more bytes than needed can be transfered because of
688
                   word alignment, so we keep them for the next line */
689
                /* XXX: keep alignment to speed up transfer */
690
                end_ptr = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
691
                copy_count = s->cirrus_srcptr_end - end_ptr;
692
                memmove(s->cirrus_bltbuf, end_ptr, copy_count);
693
                s->cirrus_srcptr = s->cirrus_bltbuf + copy_count;
694
                s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
695
            } while (s->cirrus_srcptr >= s->cirrus_srcptr_end);
696
        }
697
    }
698
}
699

    
700
/***************************************
701
 *
702
 *  bitblt wrapper
703
 *
704
 ***************************************/
705

    
706
static void cirrus_bitblt_reset(CirrusVGAState * s)
707
{
708
    s->gr[0x31] &=
709
        ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
710
    s->cirrus_srcptr = &s->cirrus_bltbuf[0];
711
    s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
712
    s->cirrus_srccounter = 0;
713
    cirrus_update_memory_access(s);
714
}
715

    
716
static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
717
{
718
    int w;
719

    
720
    s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
721
    s->cirrus_srcptr = &s->cirrus_bltbuf[0];
722
    s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
723

    
724
    if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
725
        if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
726
            s->cirrus_blt_srcpitch = 8;
727
        } else {
728
            /* XXX: check for 24 bpp */
729
            s->cirrus_blt_srcpitch = 8 * 8 * s->cirrus_blt_pixelwidth;
730
        }
731
        s->cirrus_srccounter = s->cirrus_blt_srcpitch;
732
    } else {
733
        if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
734
            w = s->cirrus_blt_width / s->cirrus_blt_pixelwidth;
735
            if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_DWORDGRANULARITY) 
736
                s->cirrus_blt_srcpitch = ((w + 31) >> 5);
737
            else
738
                s->cirrus_blt_srcpitch = ((w + 7) >> 3);
739
        } else {
740
            /* always align input size to 32 bits */
741
            s->cirrus_blt_srcpitch = (s->cirrus_blt_width + 3) & ~3;
742
        }
743
        s->cirrus_srccounter = s->cirrus_blt_srcpitch * s->cirrus_blt_height;
744
    }
745
    s->cirrus_srcptr = s->cirrus_bltbuf;
746
    s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
747
    cirrus_update_memory_access(s);
748
    return 1;
749
}
750

    
751
static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
752
{
753
    /* XXX */
754
#ifdef DEBUG_BITBLT
755
    printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
756
#endif
757
    return 0;
758
}
759

    
760
static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
761
{
762
    int ret;
763

    
764
    if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
765
        ret = cirrus_bitblt_videotovideo_patterncopy(s);
766
    } else {
767
        ret = cirrus_bitblt_videotovideo_copy(s);
768
    }
769
    if (ret)
770
        cirrus_bitblt_reset(s);
771
    return ret;
772
}
773

    
774
static void cirrus_bitblt_start(CirrusVGAState * s)
775
{
776
    uint8_t blt_rop;
777

    
778
    s->gr[0x31] |= CIRRUS_BLT_BUSY;
779

    
780
    s->cirrus_blt_width = (s->gr[0x20] | (s->gr[0x21] << 8)) + 1;
781
    s->cirrus_blt_height = (s->gr[0x22] | (s->gr[0x23] << 8)) + 1;
782
    s->cirrus_blt_dstpitch = (s->gr[0x24] | (s->gr[0x25] << 8));
783
    s->cirrus_blt_srcpitch = (s->gr[0x26] | (s->gr[0x27] << 8));
784
    s->cirrus_blt_dstaddr =
785
        (s->gr[0x28] | (s->gr[0x29] << 8) | (s->gr[0x2a] << 16));
786
    s->cirrus_blt_srcaddr =
787
        (s->gr[0x2c] | (s->gr[0x2d] << 8) | (s->gr[0x2e] << 16));
788
    s->cirrus_blt_mode = s->gr[0x30];
789
    s->cirrus_blt_modeext = s->gr[0x33];
790
    blt_rop = s->gr[0x32];
791

    
792
#ifdef DEBUG_BITBLT
793
    printf("rop=0x%02x mode=0x%02x modeext=0x%02x w=%d h=%d dpitch=%d spicth=%d daddr=0x%08x saddr=0x%08x writemask=0x%02x\n",
794
           blt_rop, 
795
           s->cirrus_blt_mode,
796
           s->cirrus_blt_modeext,
797
           s->cirrus_blt_width,
798
           s->cirrus_blt_height,
799
           s->cirrus_blt_dstpitch,
800
           s->cirrus_blt_srcpitch,
801
           s->cirrus_blt_dstaddr,
802
           s->cirrus_blt_srcaddr,
803
           s->sr[0x2f]);
804
#endif
805

    
806
    switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
807
    case CIRRUS_BLTMODE_PIXELWIDTH8:
808
        s->cirrus_blt_pixelwidth = 1;
809
        break;
810
    case CIRRUS_BLTMODE_PIXELWIDTH16:
811
        s->cirrus_blt_pixelwidth = 2;
812
        break;
813
    case CIRRUS_BLTMODE_PIXELWIDTH24:
814
        s->cirrus_blt_pixelwidth = 3;
815
        break;
816
    case CIRRUS_BLTMODE_PIXELWIDTH32:
817
        s->cirrus_blt_pixelwidth = 4;
818
        break;
819
    default:
820
#ifdef DEBUG_BITBLT
821
        printf("cirrus: bitblt - pixel width is unknown\n");
822
#endif
823
        goto bitblt_ignore;
824
    }
825
    s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
826

    
827
    if ((s->
828
         cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
829
                            CIRRUS_BLTMODE_MEMSYSDEST))
830
        == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
831
#ifdef DEBUG_BITBLT
832
        printf("cirrus: bitblt - memory-to-memory copy is requested\n");
833
#endif
834
        goto bitblt_ignore;
835
    }
836

    
837
    if ((s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
838
        (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST | 
839
                               CIRRUS_BLTMODE_TRANSPARENTCOMP |
840
                               CIRRUS_BLTMODE_PATTERNCOPY | 
841
                               CIRRUS_BLTMODE_COLOREXPAND)) == 
842
         (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
843
        cirrus_bitblt_fgcol(s);
844
        cirrus_bitblt_solidfill(s, blt_rop);
845
    } else {
846
        if ((s->cirrus_blt_mode & (CIRRUS_BLTMODE_COLOREXPAND | 
847
                                   CIRRUS_BLTMODE_PATTERNCOPY)) == 
848
            CIRRUS_BLTMODE_COLOREXPAND) {
849

    
850
            if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
851
                if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
852
                    cirrus_bitblt_bgcol(s);
853
                else
854
                    cirrus_bitblt_fgcol(s);
855
                s->cirrus_rop = cirrus_colorexpand_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
856
            } else {
857
                cirrus_bitblt_fgcol(s);
858
                cirrus_bitblt_bgcol(s);
859
                s->cirrus_rop = cirrus_colorexpand[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
860
            }
861
        } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
862
            if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
863
                if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
864
                    if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
865
                        cirrus_bitblt_bgcol(s);
866
                    else
867
                        cirrus_bitblt_fgcol(s);
868
                    s->cirrus_rop = cirrus_colorexpand_pattern_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
869
                } else {
870
                    cirrus_bitblt_fgcol(s);
871
                    cirrus_bitblt_bgcol(s);
872
                    s->cirrus_rop = cirrus_colorexpand_pattern[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
873
                }
874
            } else {
875
                s->cirrus_rop = cirrus_patternfill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
876
            }
877
        } else {
878
            if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
879
                s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
880
                s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
881
                s->cirrus_rop = cirrus_bkwd_rop[rop_to_index[blt_rop]];
882
            } else {
883
                s->cirrus_rop = cirrus_fwd_rop[rop_to_index[blt_rop]];
884
            }
885
        }
886
        
887
        // setup bitblt engine.
888
        if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
889
            if (!cirrus_bitblt_cputovideo(s))
890
                goto bitblt_ignore;
891
        } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
892
            if (!cirrus_bitblt_videotocpu(s))
893
                goto bitblt_ignore;
894
        } else {
895
            if (!cirrus_bitblt_videotovideo(s))
896
                goto bitblt_ignore;
897
        }
898
    }
899
    return;
900
  bitblt_ignore:;
901
    cirrus_bitblt_reset(s);
902
}
903

    
904
static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
905
{
906
    unsigned old_value;
907

    
908
    old_value = s->gr[0x31];
909
    s->gr[0x31] = reg_value;
910

    
911
    if (((old_value & CIRRUS_BLT_RESET) != 0) &&
912
        ((reg_value & CIRRUS_BLT_RESET) == 0)) {
913
        cirrus_bitblt_reset(s);
914
    } else if (((old_value & CIRRUS_BLT_START) == 0) &&
915
               ((reg_value & CIRRUS_BLT_START) != 0)) {
916
        cirrus_bitblt_start(s);
917
    }
918
}
919

    
920

    
921
/***************************************
922
 *
923
 *  basic parameters
924
 *
925
 ***************************************/
926

    
927
static void cirrus_get_offsets(VGAState *s1, 
928
                                   uint32_t *pline_offset,
929
                                   uint32_t *pstart_addr)
930
{
931
    CirrusVGAState * s = (CirrusVGAState *)s1;
932
    uint32_t start_addr;
933
    uint32_t line_offset;
934

    
935
    line_offset = s->cr[0x13]
936
        | ((s->cr[0x1b] & 0x10) << 4);
937
    line_offset <<= 3;
938
    *pline_offset = line_offset;
939

    
940
    start_addr = (s->cr[0x0c] << 8)
941
        | s->cr[0x0d]
942
        | ((s->cr[0x1b] & 0x01) << 16)
943
        | ((s->cr[0x1b] & 0x0c) << 15)
944
        | ((s->cr[0x1d] & 0x80) << 12);
945
    *pstart_addr = start_addr;
946
}
947

    
948
static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
949
{
950
    uint32_t ret = 16;
951

    
952
    switch (s->cirrus_hidden_dac_data & 0xf) {
953
    case 0:
954
        ret = 15;
955
        break;                        /* Sierra HiColor */
956
    case 1:
957
        ret = 16;
958
        break;                        /* XGA HiColor */
959
    default:
960
#ifdef DEBUG_CIRRUS
961
        printf("cirrus: invalid DAC value %x in 16bpp\n",
962
               (s->cirrus_hidden_dac_data & 0xf));
963
#endif
964
        ret = 15;                /* XXX */
965
        break;
966
    }
967
    return ret;
968
}
969

    
970
static int cirrus_get_bpp(VGAState *s1)
971
{
972
    CirrusVGAState * s = (CirrusVGAState *)s1;
973
    uint32_t ret = 8;
974

    
975
    if ((s->sr[0x07] & 0x01) != 0) {
976
        /* Cirrus SVGA */
977
        switch (s->sr[0x07] & CIRRUS_SR7_BPP_MASK) {
978
        case CIRRUS_SR7_BPP_8:
979
            ret = 8;
980
            break;
981
        case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
982
            ret = cirrus_get_bpp16_depth(s);
983
            break;
984
        case CIRRUS_SR7_BPP_24:
985
            ret = 24;
986
            break;
987
        case CIRRUS_SR7_BPP_16:
988
            ret = cirrus_get_bpp16_depth(s);
989
            break;
990
        case CIRRUS_SR7_BPP_32:
991
            ret = 32;
992
            break;
993
        default:
994
#ifdef DEBUG_CIRRUS
995
            printf("cirrus: unknown bpp - sr7=%x\n", s->sr[0x7]);
996
#endif
997
            ret = 8;
998
            break;
999
        }
1000
    } else {
1001
        /* VGA */
1002
        ret = 0;
1003
    }
1004

    
1005
    return ret;
1006
}
1007

    
1008
static void cirrus_get_resolution(VGAState *s, int *pwidth, int *pheight)
1009
{
1010
    int width, height;
1011
    
1012
    width = (s->cr[0x01] + 1) * 8;
1013
    height = s->cr[0x12] | 
1014
        ((s->cr[0x07] & 0x02) << 7) | 
1015
        ((s->cr[0x07] & 0x40) << 3);
1016
    height = (height + 1);
1017
    /* interlace support */
1018
    if (s->cr[0x1a] & 0x01)
1019
        height = height * 2;
1020
    *pwidth = width;
1021
    *pheight = height;
1022
}
1023

    
1024
/***************************************
1025
 *
1026
 * bank memory
1027
 *
1028
 ***************************************/
1029

    
1030
static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
1031
{
1032
    unsigned offset;
1033
    unsigned limit;
1034

    
1035
    if ((s->gr[0x0b] & 0x01) != 0)        /* dual bank */
1036
        offset = s->gr[0x09 + bank_index];
1037
    else                        /* single bank */
1038
        offset = s->gr[0x09];
1039

    
1040
    if ((s->gr[0x0b] & 0x20) != 0)
1041
        offset <<= 14;
1042
    else
1043
        offset <<= 12;
1044

    
1045
    if (s->vram_size <= offset)
1046
        limit = 0;
1047
    else
1048
        limit = s->vram_size - offset;
1049

    
1050
    if (((s->gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
1051
        if (limit > 0x8000) {
1052
            offset += 0x8000;
1053
            limit -= 0x8000;
1054
        } else {
1055
            limit = 0;
1056
        }
1057
    }
1058

    
1059
    if (limit > 0) {
1060
        s->cirrus_bank_base[bank_index] = offset;
1061
        s->cirrus_bank_limit[bank_index] = limit;
1062
    } else {
1063
        s->cirrus_bank_base[bank_index] = 0;
1064
        s->cirrus_bank_limit[bank_index] = 0;
1065
    }
1066
}
1067

    
1068
/***************************************
1069
 *
1070
 *  I/O access between 0x3c4-0x3c5
1071
 *
1072
 ***************************************/
1073

    
1074
static int
1075
cirrus_hook_read_sr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1076
{
1077
    switch (reg_index) {
1078
    case 0x00:                        // Standard VGA
1079
    case 0x01:                        // Standard VGA
1080
    case 0x02:                        // Standard VGA
1081
    case 0x03:                        // Standard VGA
1082
    case 0x04:                        // Standard VGA
1083
        return CIRRUS_HOOK_NOT_HANDLED;
1084
    case 0x06:                        // Unlock Cirrus extensions
1085
        *reg_value = s->sr[reg_index];
1086
        break;
1087
    case 0x10:
1088
    case 0x30:
1089
    case 0x50:
1090
    case 0x70:                        // Graphics Cursor X
1091
    case 0x90:
1092
    case 0xb0:
1093
    case 0xd0:
1094
    case 0xf0:                        // Graphics Cursor X
1095
        *reg_value = s->sr[0x10];
1096
        break;
1097
    case 0x11:
1098
    case 0x31:
1099
    case 0x51:
1100
    case 0x71:                        // Graphics Cursor Y
1101
    case 0x91:
1102
    case 0xb1:
1103
    case 0xd1:
1104
    case 0xf1:                        // Graphics Cursor Y
1105
        *reg_value = s->sr[0x11];
1106
        break;
1107
    case 0x05:                        // ???
1108
    case 0x07:                        // Extended Sequencer Mode
1109
    case 0x08:                        // EEPROM Control
1110
    case 0x09:                        // Scratch Register 0
1111
    case 0x0a:                        // Scratch Register 1
1112
    case 0x0b:                        // VCLK 0
1113
    case 0x0c:                        // VCLK 1
1114
    case 0x0d:                        // VCLK 2
1115
    case 0x0e:                        // VCLK 3
1116
    case 0x0f:                        // DRAM Control
1117
    case 0x12:                        // Graphics Cursor Attribute
1118
    case 0x13:                        // Graphics Cursor Pattern Address
1119
    case 0x14:                        // Scratch Register 2
1120
    case 0x15:                        // Scratch Register 3
1121
    case 0x16:                        // Performance Tuning Register
1122
    case 0x17:                        // Configuration Readback and Extended Control
1123
    case 0x18:                        // Signature Generator Control
1124
    case 0x19:                        // Signal Generator Result
1125
    case 0x1a:                        // Signal Generator Result
1126
    case 0x1b:                        // VCLK 0 Denominator & Post
1127
    case 0x1c:                        // VCLK 1 Denominator & Post
1128
    case 0x1d:                        // VCLK 2 Denominator & Post
1129
    case 0x1e:                        // VCLK 3 Denominator & Post
1130
    case 0x1f:                        // BIOS Write Enable and MCLK select
1131
#ifdef DEBUG_CIRRUS
1132
        printf("cirrus: handled inport sr_index %02x\n", reg_index);
1133
#endif
1134
        *reg_value = s->sr[reg_index];
1135
        break;
1136
    default:
1137
#ifdef DEBUG_CIRRUS
1138
        printf("cirrus: inport sr_index %02x\n", reg_index);
1139
#endif
1140
        *reg_value = 0xff;
1141
        break;
1142
    }
1143

    
1144
    return CIRRUS_HOOK_HANDLED;
1145
}
1146

    
1147
static int
1148
cirrus_hook_write_sr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1149
{
1150
    switch (reg_index) {
1151
    case 0x00:                        // Standard VGA
1152
    case 0x01:                        // Standard VGA
1153
    case 0x02:                        // Standard VGA
1154
    case 0x03:                        // Standard VGA
1155
    case 0x04:                        // Standard VGA
1156
        return CIRRUS_HOOK_NOT_HANDLED;
1157
    case 0x06:                        // Unlock Cirrus extensions
1158
        reg_value &= 0x17;
1159
        if (reg_value == 0x12) {
1160
            s->sr[reg_index] = 0x12;
1161
        } else {
1162
            s->sr[reg_index] = 0x0f;
1163
        }
1164
        break;
1165
    case 0x10:
1166
    case 0x30:
1167
    case 0x50:
1168
    case 0x70:                        // Graphics Cursor X
1169
    case 0x90:
1170
    case 0xb0:
1171
    case 0xd0:
1172
    case 0xf0:                        // Graphics Cursor X
1173
        s->sr[0x10] = reg_value;
1174
        s->hw_cursor_x = (reg_value << 3) | (reg_index >> 5);
1175
        break;
1176
    case 0x11:
1177
    case 0x31:
1178
    case 0x51:
1179
    case 0x71:                        // Graphics Cursor Y
1180
    case 0x91:
1181
    case 0xb1:
1182
    case 0xd1:
1183
    case 0xf1:                        // Graphics Cursor Y
1184
        s->sr[0x11] = reg_value;
1185
        s->hw_cursor_y = (reg_value << 3) | (reg_index >> 5);
1186
        break;
1187
    case 0x07:                        // Extended Sequencer Mode
1188
    case 0x08:                        // EEPROM Control
1189
    case 0x09:                        // Scratch Register 0
1190
    case 0x0a:                        // Scratch Register 1
1191
    case 0x0b:                        // VCLK 0
1192
    case 0x0c:                        // VCLK 1
1193
    case 0x0d:                        // VCLK 2
1194
    case 0x0e:                        // VCLK 3
1195
    case 0x0f:                        // DRAM Control
1196
    case 0x12:                        // Graphics Cursor Attribute
1197
    case 0x13:                        // Graphics Cursor Pattern Address
1198
    case 0x14:                        // Scratch Register 2
1199
    case 0x15:                        // Scratch Register 3
1200
    case 0x16:                        // Performance Tuning Register
1201
    case 0x18:                        // Signature Generator Control
1202
    case 0x19:                        // Signature Generator Result
1203
    case 0x1a:                        // Signature Generator Result
1204
    case 0x1b:                        // VCLK 0 Denominator & Post
1205
    case 0x1c:                        // VCLK 1 Denominator & Post
1206
    case 0x1d:                        // VCLK 2 Denominator & Post
1207
    case 0x1e:                        // VCLK 3 Denominator & Post
1208
    case 0x1f:                        // BIOS Write Enable and MCLK select
1209
        s->sr[reg_index] = reg_value;
1210
#ifdef DEBUG_CIRRUS
1211
        printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
1212
               reg_index, reg_value);
1213
#endif
1214
        break;
1215
    case 0x17:                        // Configuration Readback and Extended Control
1216
        s->sr[reg_index] = reg_value;
1217
        cirrus_update_memory_access(s);
1218
        break;
1219
    default:
1220
#ifdef DEBUG_CIRRUS
1221
        printf("cirrus: outport sr_index %02x, sr_value %02x\n", reg_index,
1222
               reg_value);
1223
#endif
1224
        break;
1225
    }
1226

    
1227
    return CIRRUS_HOOK_HANDLED;
1228
}
1229

    
1230
/***************************************
1231
 *
1232
 *  I/O access at 0x3c6
1233
 *
1234
 ***************************************/
1235

    
1236
static void cirrus_read_hidden_dac(CirrusVGAState * s, int *reg_value)
1237
{
1238
    *reg_value = 0xff;
1239
    if (++s->cirrus_hidden_dac_lockindex == 5) {
1240
        *reg_value = s->cirrus_hidden_dac_data;
1241
        s->cirrus_hidden_dac_lockindex = 0;
1242
    }
1243
}
1244

    
1245
static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
1246
{
1247
    if (s->cirrus_hidden_dac_lockindex == 4) {
1248
        s->cirrus_hidden_dac_data = reg_value;
1249
#if defined(DEBUG_CIRRUS)
1250
        printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
1251
#endif
1252
    }
1253
    s->cirrus_hidden_dac_lockindex = 0;
1254
}
1255

    
1256
/***************************************
1257
 *
1258
 *  I/O access at 0x3c9
1259
 *
1260
 ***************************************/
1261

    
1262
static int cirrus_hook_read_palette(CirrusVGAState * s, int *reg_value)
1263
{
1264
    if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1265
        return CIRRUS_HOOK_NOT_HANDLED;
1266
    *reg_value =
1267
        s->cirrus_hidden_palette[(s->dac_read_index & 0x0f) * 3 +
1268
                                 s->dac_sub_index];
1269
    if (++s->dac_sub_index == 3) {
1270
        s->dac_sub_index = 0;
1271
        s->dac_read_index++;
1272
    }
1273
    return CIRRUS_HOOK_HANDLED;
1274
}
1275

    
1276
static int cirrus_hook_write_palette(CirrusVGAState * s, int reg_value)
1277
{
1278
    if (!(s->sr[0x12] & CIRRUS_CURSOR_HIDDENPEL))
1279
        return CIRRUS_HOOK_NOT_HANDLED;
1280
    s->dac_cache[s->dac_sub_index] = reg_value;
1281
    if (++s->dac_sub_index == 3) {
1282
        memcpy(&s->cirrus_hidden_palette[(s->dac_write_index & 0x0f) * 3],
1283
               s->dac_cache, 3);
1284
        /* XXX update cursor */
1285
        s->dac_sub_index = 0;
1286
        s->dac_write_index++;
1287
    }
1288
    return CIRRUS_HOOK_HANDLED;
1289
}
1290

    
1291
/***************************************
1292
 *
1293
 *  I/O access between 0x3ce-0x3cf
1294
 *
1295
 ***************************************/
1296

    
1297
static int
1298
cirrus_hook_read_gr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1299
{
1300
    switch (reg_index) {
1301
    case 0x00: // Standard VGA, BGCOLOR 0x000000ff
1302
      *reg_value = s->cirrus_shadow_gr0;
1303
      return CIRRUS_HOOK_HANDLED;
1304
    case 0x01: // Standard VGA, FGCOLOR 0x000000ff
1305
      *reg_value = s->cirrus_shadow_gr1;
1306
      return CIRRUS_HOOK_HANDLED;
1307
    case 0x02:                        // Standard VGA
1308
    case 0x03:                        // Standard VGA
1309
    case 0x04:                        // Standard VGA
1310
    case 0x06:                        // Standard VGA
1311
    case 0x07:                        // Standard VGA
1312
    case 0x08:                        // Standard VGA
1313
        return CIRRUS_HOOK_NOT_HANDLED;
1314
    case 0x05:                        // Standard VGA, Cirrus extended mode
1315
    default:
1316
        break;
1317
    }
1318

    
1319
    if (reg_index < 0x3a) {
1320
        *reg_value = s->gr[reg_index];
1321
    } else {
1322
#ifdef DEBUG_CIRRUS
1323
        printf("cirrus: inport gr_index %02x\n", reg_index);
1324
#endif
1325
        *reg_value = 0xff;
1326
    }
1327

    
1328
    return CIRRUS_HOOK_HANDLED;
1329
}
1330

    
1331
static int
1332
cirrus_hook_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1333
{
1334
#if defined(DEBUG_BITBLT) && 0
1335
    printf("gr%02x: %02x\n", reg_index, reg_value);
1336
#endif
1337
    switch (reg_index) {
1338
    case 0x00:                        // Standard VGA, BGCOLOR 0x000000ff
1339
        s->cirrus_shadow_gr0 = reg_value;
1340
        return CIRRUS_HOOK_NOT_HANDLED;
1341
    case 0x01:                        // Standard VGA, FGCOLOR 0x000000ff
1342
        s->cirrus_shadow_gr1 = reg_value;
1343
        return CIRRUS_HOOK_NOT_HANDLED;
1344
    case 0x02:                        // Standard VGA
1345
    case 0x03:                        // Standard VGA
1346
    case 0x04:                        // Standard VGA
1347
    case 0x06:                        // Standard VGA
1348
    case 0x07:                        // Standard VGA
1349
    case 0x08:                        // Standard VGA
1350
        return CIRRUS_HOOK_NOT_HANDLED;
1351
    case 0x05:                        // Standard VGA, Cirrus extended mode
1352
        s->gr[reg_index] = reg_value & 0x7f;
1353
        cirrus_update_memory_access(s);
1354
        break;
1355
    case 0x09:                        // bank offset #0
1356
    case 0x0A:                        // bank offset #1
1357
        s->gr[reg_index] = reg_value;
1358
        cirrus_update_bank_ptr(s, 0);
1359
        cirrus_update_bank_ptr(s, 1);
1360
        break;
1361
    case 0x0B:
1362
        s->gr[reg_index] = reg_value;
1363
        cirrus_update_bank_ptr(s, 0);
1364
        cirrus_update_bank_ptr(s, 1);
1365
        cirrus_update_memory_access(s);
1366
        break;
1367
    case 0x10:                        // BGCOLOR 0x0000ff00
1368
    case 0x11:                        // FGCOLOR 0x0000ff00
1369
    case 0x12:                        // BGCOLOR 0x00ff0000
1370
    case 0x13:                        // FGCOLOR 0x00ff0000
1371
    case 0x14:                        // BGCOLOR 0xff000000
1372
    case 0x15:                        // FGCOLOR 0xff000000
1373
    case 0x20:                        // BLT WIDTH 0x0000ff
1374
    case 0x22:                        // BLT HEIGHT 0x0000ff
1375
    case 0x24:                        // BLT DEST PITCH 0x0000ff
1376
    case 0x26:                        // BLT SRC PITCH 0x0000ff
1377
    case 0x28:                        // BLT DEST ADDR 0x0000ff
1378
    case 0x29:                        // BLT DEST ADDR 0x00ff00
1379
    case 0x2c:                        // BLT SRC ADDR 0x0000ff
1380
    case 0x2d:                        // BLT SRC ADDR 0x00ff00
1381
    case 0x2f:                  // BLT WRITEMASK
1382
    case 0x30:                        // BLT MODE
1383
    case 0x32:                        // RASTER OP
1384
    case 0x33:                        // BLT MODEEXT
1385
    case 0x34:                        // BLT TRANSPARENT COLOR 0x00ff
1386
    case 0x35:                        // BLT TRANSPARENT COLOR 0xff00
1387
    case 0x38:                        // BLT TRANSPARENT COLOR MASK 0x00ff
1388
    case 0x39:                        // BLT TRANSPARENT COLOR MASK 0xff00
1389
        s->gr[reg_index] = reg_value;
1390
        break;
1391
    case 0x21:                        // BLT WIDTH 0x001f00
1392
    case 0x23:                        // BLT HEIGHT 0x001f00
1393
    case 0x25:                        // BLT DEST PITCH 0x001f00
1394
    case 0x27:                        // BLT SRC PITCH 0x001f00
1395
        s->gr[reg_index] = reg_value & 0x1f;
1396
        break;
1397
    case 0x2a:                        // BLT DEST ADDR 0x3f0000
1398
        s->gr[reg_index] = reg_value & 0x3f;
1399
        /* if auto start mode, starts bit blt now */
1400
        if (s->gr[0x31] & CIRRUS_BLT_AUTOSTART) {
1401
            cirrus_bitblt_start(s);
1402
        }
1403
        break;
1404
    case 0x2e:                        // BLT SRC ADDR 0x3f0000
1405
        s->gr[reg_index] = reg_value & 0x3f;
1406
        break;
1407
    case 0x31:                        // BLT STATUS/START
1408
        cirrus_write_bitblt(s, reg_value);
1409
        break;
1410
    default:
1411
#ifdef DEBUG_CIRRUS
1412
        printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
1413
               reg_value);
1414
#endif
1415
        break;
1416
    }
1417

    
1418
    return CIRRUS_HOOK_HANDLED;
1419
}
1420

    
1421
/***************************************
1422
 *
1423
 *  I/O access between 0x3d4-0x3d5
1424
 *
1425
 ***************************************/
1426

    
1427
static int
1428
cirrus_hook_read_cr(CirrusVGAState * s, unsigned reg_index, int *reg_value)
1429
{
1430
    switch (reg_index) {
1431
    case 0x00:                        // Standard VGA
1432
    case 0x01:                        // Standard VGA
1433
    case 0x02:                        // Standard VGA
1434
    case 0x03:                        // Standard VGA
1435
    case 0x04:                        // Standard VGA
1436
    case 0x05:                        // Standard VGA
1437
    case 0x06:                        // Standard VGA
1438
    case 0x07:                        // Standard VGA
1439
    case 0x08:                        // Standard VGA
1440
    case 0x09:                        // Standard VGA
1441
    case 0x0a:                        // Standard VGA
1442
    case 0x0b:                        // Standard VGA
1443
    case 0x0c:                        // Standard VGA
1444
    case 0x0d:                        // Standard VGA
1445
    case 0x0e:                        // Standard VGA
1446
    case 0x0f:                        // Standard VGA
1447
    case 0x10:                        // Standard VGA
1448
    case 0x11:                        // Standard VGA
1449
    case 0x12:                        // Standard VGA
1450
    case 0x13:                        // Standard VGA
1451
    case 0x14:                        // Standard VGA
1452
    case 0x15:                        // Standard VGA
1453
    case 0x16:                        // Standard VGA
1454
    case 0x17:                        // Standard VGA
1455
    case 0x18:                        // Standard VGA
1456
        return CIRRUS_HOOK_NOT_HANDLED;
1457
    case 0x19:                        // Interlace End
1458
    case 0x1a:                        // Miscellaneous Control
1459
    case 0x1b:                        // Extended Display Control
1460
    case 0x1c:                        // Sync Adjust and Genlock
1461
    case 0x1d:                        // Overlay Extended Control
1462
    case 0x22:                        // Graphics Data Latches Readback (R)
1463
    case 0x24:                        // Attribute Controller Toggle Readback (R)
1464
    case 0x25:                        // Part Status
1465
    case 0x27:                        // Part ID (R)
1466
        *reg_value = s->cr[reg_index];
1467
        break;
1468
    case 0x26:                        // Attribute Controller Index Readback (R)
1469
        *reg_value = s->ar_index & 0x3f;
1470
        break;
1471
    default:
1472
#ifdef DEBUG_CIRRUS
1473
        printf("cirrus: inport cr_index %02x\n", reg_index);
1474
        *reg_value = 0xff;
1475
#endif
1476
        break;
1477
    }
1478

    
1479
    return CIRRUS_HOOK_HANDLED;
1480
}
1481

    
1482
static int
1483
cirrus_hook_write_cr(CirrusVGAState * s, unsigned reg_index, int reg_value)
1484
{
1485
    switch (reg_index) {
1486
    case 0x00:                        // Standard VGA
1487
    case 0x01:                        // Standard VGA
1488
    case 0x02:                        // Standard VGA
1489
    case 0x03:                        // Standard VGA
1490
    case 0x04:                        // Standard VGA
1491
    case 0x05:                        // Standard VGA
1492
    case 0x06:                        // Standard VGA
1493
    case 0x07:                        // Standard VGA
1494
    case 0x08:                        // Standard VGA
1495
    case 0x09:                        // Standard VGA
1496
    case 0x0a:                        // Standard VGA
1497
    case 0x0b:                        // Standard VGA
1498
    case 0x0c:                        // Standard VGA
1499
    case 0x0d:                        // Standard VGA
1500
    case 0x0e:                        // Standard VGA
1501
    case 0x0f:                        // Standard VGA
1502
    case 0x10:                        // Standard VGA
1503
    case 0x11:                        // Standard VGA
1504
    case 0x12:                        // Standard VGA
1505
    case 0x13:                        // Standard VGA
1506
    case 0x14:                        // Standard VGA
1507
    case 0x15:                        // Standard VGA
1508
    case 0x16:                        // Standard VGA
1509
    case 0x17:                        // Standard VGA
1510
    case 0x18:                        // Standard VGA
1511
        return CIRRUS_HOOK_NOT_HANDLED;
1512
    case 0x19:                        // Interlace End
1513
    case 0x1a:                        // Miscellaneous Control
1514
    case 0x1b:                        // Extended Display Control
1515
    case 0x1c:                        // Sync Adjust and Genlock
1516
    case 0x1d:                        // Overlay Extended Control
1517
        s->cr[reg_index] = reg_value;
1518
#ifdef DEBUG_CIRRUS
1519
        printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
1520
               reg_index, reg_value);
1521
#endif
1522
        break;
1523
    case 0x22:                        // Graphics Data Latches Readback (R)
1524
    case 0x24:                        // Attribute Controller Toggle Readback (R)
1525
    case 0x26:                        // Attribute Controller Index Readback (R)
1526
    case 0x27:                        // Part ID (R)
1527
        break;
1528
    case 0x25:                        // Part Status
1529
    default:
1530
#ifdef DEBUG_CIRRUS
1531
        printf("cirrus: outport cr_index %02x, cr_value %02x\n", reg_index,
1532
               reg_value);
1533
#endif
1534
        break;
1535
    }
1536

    
1537
    return CIRRUS_HOOK_HANDLED;
1538
}
1539

    
1540
/***************************************
1541
 *
1542
 *  memory-mapped I/O (bitblt)
1543
 *
1544
 ***************************************/
1545

    
1546
static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
1547
{
1548
    int value = 0xff;
1549

    
1550
    switch (address) {
1551
    case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1552
        cirrus_hook_read_gr(s, 0x00, &value);
1553
        break;
1554
    case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1555
        cirrus_hook_read_gr(s, 0x10, &value);
1556
        break;
1557
    case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1558
        cirrus_hook_read_gr(s, 0x12, &value);
1559
        break;
1560
    case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1561
        cirrus_hook_read_gr(s, 0x14, &value);
1562
        break;
1563
    case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1564
        cirrus_hook_read_gr(s, 0x01, &value);
1565
        break;
1566
    case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1567
        cirrus_hook_read_gr(s, 0x11, &value);
1568
        break;
1569
    case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1570
        cirrus_hook_read_gr(s, 0x13, &value);
1571
        break;
1572
    case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1573
        cirrus_hook_read_gr(s, 0x15, &value);
1574
        break;
1575
    case (CIRRUS_MMIO_BLTWIDTH + 0):
1576
        cirrus_hook_read_gr(s, 0x20, &value);
1577
        break;
1578
    case (CIRRUS_MMIO_BLTWIDTH + 1):
1579
        cirrus_hook_read_gr(s, 0x21, &value);
1580
        break;
1581
    case (CIRRUS_MMIO_BLTHEIGHT + 0):
1582
        cirrus_hook_read_gr(s, 0x22, &value);
1583
        break;
1584
    case (CIRRUS_MMIO_BLTHEIGHT + 1):
1585
        cirrus_hook_read_gr(s, 0x23, &value);
1586
        break;
1587
    case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1588
        cirrus_hook_read_gr(s, 0x24, &value);
1589
        break;
1590
    case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1591
        cirrus_hook_read_gr(s, 0x25, &value);
1592
        break;
1593
    case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1594
        cirrus_hook_read_gr(s, 0x26, &value);
1595
        break;
1596
    case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1597
        cirrus_hook_read_gr(s, 0x27, &value);
1598
        break;
1599
    case (CIRRUS_MMIO_BLTDESTADDR + 0):
1600
        cirrus_hook_read_gr(s, 0x28, &value);
1601
        break;
1602
    case (CIRRUS_MMIO_BLTDESTADDR + 1):
1603
        cirrus_hook_read_gr(s, 0x29, &value);
1604
        break;
1605
    case (CIRRUS_MMIO_BLTDESTADDR + 2):
1606
        cirrus_hook_read_gr(s, 0x2a, &value);
1607
        break;
1608
    case (CIRRUS_MMIO_BLTSRCADDR + 0):
1609
        cirrus_hook_read_gr(s, 0x2c, &value);
1610
        break;
1611
    case (CIRRUS_MMIO_BLTSRCADDR + 1):
1612
        cirrus_hook_read_gr(s, 0x2d, &value);
1613
        break;
1614
    case (CIRRUS_MMIO_BLTSRCADDR + 2):
1615
        cirrus_hook_read_gr(s, 0x2e, &value);
1616
        break;
1617
    case CIRRUS_MMIO_BLTWRITEMASK:
1618
        cirrus_hook_read_gr(s, 0x2f, &value);
1619
        break;
1620
    case CIRRUS_MMIO_BLTMODE:
1621
        cirrus_hook_read_gr(s, 0x30, &value);
1622
        break;
1623
    case CIRRUS_MMIO_BLTROP:
1624
        cirrus_hook_read_gr(s, 0x32, &value);
1625
        break;
1626
    case CIRRUS_MMIO_BLTMODEEXT:
1627
        cirrus_hook_read_gr(s, 0x33, &value);
1628
        break;
1629
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1630
        cirrus_hook_read_gr(s, 0x34, &value);
1631
        break;
1632
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1633
        cirrus_hook_read_gr(s, 0x35, &value);
1634
        break;
1635
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1636
        cirrus_hook_read_gr(s, 0x38, &value);
1637
        break;
1638
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1639
        cirrus_hook_read_gr(s, 0x39, &value);
1640
        break;
1641
    case CIRRUS_MMIO_BLTSTATUS:
1642
        cirrus_hook_read_gr(s, 0x31, &value);
1643
        break;
1644
    default:
1645
#ifdef DEBUG_CIRRUS
1646
        printf("cirrus: mmio read - address 0x%04x\n", address);
1647
#endif
1648
        break;
1649
    }
1650

    
1651
    return (uint8_t) value;
1652
}
1653

    
1654
static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
1655
                                  uint8_t value)
1656
{
1657
    switch (address) {
1658
    case (CIRRUS_MMIO_BLTBGCOLOR + 0):
1659
        cirrus_hook_write_gr(s, 0x00, value);
1660
        break;
1661
    case (CIRRUS_MMIO_BLTBGCOLOR + 1):
1662
        cirrus_hook_write_gr(s, 0x10, value);
1663
        break;
1664
    case (CIRRUS_MMIO_BLTBGCOLOR + 2):
1665
        cirrus_hook_write_gr(s, 0x12, value);
1666
        break;
1667
    case (CIRRUS_MMIO_BLTBGCOLOR + 3):
1668
        cirrus_hook_write_gr(s, 0x14, value);
1669
        break;
1670
    case (CIRRUS_MMIO_BLTFGCOLOR + 0):
1671
        cirrus_hook_write_gr(s, 0x01, value);
1672
        break;
1673
    case (CIRRUS_MMIO_BLTFGCOLOR + 1):
1674
        cirrus_hook_write_gr(s, 0x11, value);
1675
        break;
1676
    case (CIRRUS_MMIO_BLTFGCOLOR + 2):
1677
        cirrus_hook_write_gr(s, 0x13, value);
1678
        break;
1679
    case (CIRRUS_MMIO_BLTFGCOLOR + 3):
1680
        cirrus_hook_write_gr(s, 0x15, value);
1681
        break;
1682
    case (CIRRUS_MMIO_BLTWIDTH + 0):
1683
        cirrus_hook_write_gr(s, 0x20, value);
1684
        break;
1685
    case (CIRRUS_MMIO_BLTWIDTH + 1):
1686
        cirrus_hook_write_gr(s, 0x21, value);
1687
        break;
1688
    case (CIRRUS_MMIO_BLTHEIGHT + 0):
1689
        cirrus_hook_write_gr(s, 0x22, value);
1690
        break;
1691
    case (CIRRUS_MMIO_BLTHEIGHT + 1):
1692
        cirrus_hook_write_gr(s, 0x23, value);
1693
        break;
1694
    case (CIRRUS_MMIO_BLTDESTPITCH + 0):
1695
        cirrus_hook_write_gr(s, 0x24, value);
1696
        break;
1697
    case (CIRRUS_MMIO_BLTDESTPITCH + 1):
1698
        cirrus_hook_write_gr(s, 0x25, value);
1699
        break;
1700
    case (CIRRUS_MMIO_BLTSRCPITCH + 0):
1701
        cirrus_hook_write_gr(s, 0x26, value);
1702
        break;
1703
    case (CIRRUS_MMIO_BLTSRCPITCH + 1):
1704
        cirrus_hook_write_gr(s, 0x27, value);
1705
        break;
1706
    case (CIRRUS_MMIO_BLTDESTADDR + 0):
1707
        cirrus_hook_write_gr(s, 0x28, value);
1708
        break;
1709
    case (CIRRUS_MMIO_BLTDESTADDR + 1):
1710
        cirrus_hook_write_gr(s, 0x29, value);
1711
        break;
1712
    case (CIRRUS_MMIO_BLTDESTADDR + 2):
1713
        cirrus_hook_write_gr(s, 0x2a, value);
1714
        break;
1715
    case (CIRRUS_MMIO_BLTDESTADDR + 3):
1716
        /* ignored */
1717
        break;
1718
    case (CIRRUS_MMIO_BLTSRCADDR + 0):
1719
        cirrus_hook_write_gr(s, 0x2c, value);
1720
        break;
1721
    case (CIRRUS_MMIO_BLTSRCADDR + 1):
1722
        cirrus_hook_write_gr(s, 0x2d, value);
1723
        break;
1724
    case (CIRRUS_MMIO_BLTSRCADDR + 2):
1725
        cirrus_hook_write_gr(s, 0x2e, value);
1726
        break;
1727
    case CIRRUS_MMIO_BLTWRITEMASK:
1728
        cirrus_hook_write_gr(s, 0x2f, value);
1729
        break;
1730
    case CIRRUS_MMIO_BLTMODE:
1731
        cirrus_hook_write_gr(s, 0x30, value);
1732
        break;
1733
    case CIRRUS_MMIO_BLTROP:
1734
        cirrus_hook_write_gr(s, 0x32, value);
1735
        break;
1736
    case CIRRUS_MMIO_BLTMODEEXT:
1737
        cirrus_hook_write_gr(s, 0x33, value);
1738
        break;
1739
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
1740
        cirrus_hook_write_gr(s, 0x34, value);
1741
        break;
1742
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
1743
        cirrus_hook_write_gr(s, 0x35, value);
1744
        break;
1745
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
1746
        cirrus_hook_write_gr(s, 0x38, value);
1747
        break;
1748
    case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
1749
        cirrus_hook_write_gr(s, 0x39, value);
1750
        break;
1751
    case CIRRUS_MMIO_BLTSTATUS:
1752
        cirrus_hook_write_gr(s, 0x31, value);
1753
        break;
1754
    default:
1755
#ifdef DEBUG_CIRRUS
1756
        printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
1757
               address, value);
1758
#endif
1759
        break;
1760
    }
1761
}
1762

    
1763
/***************************************
1764
 *
1765
 *  write mode 4/5
1766
 *
1767
 * assume TARGET_PAGE_SIZE >= 16
1768
 *
1769
 ***************************************/
1770

    
1771
static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
1772
                                             unsigned mode,
1773
                                             unsigned offset,
1774
                                             uint32_t mem_value)
1775
{
1776
    int x;
1777
    unsigned val = mem_value;
1778
    uint8_t *dst;
1779

    
1780
    dst = s->vram_ptr + offset;
1781
    for (x = 0; x < 8; x++) {
1782
        if (val & 0x80) {
1783
            *dst++ = s->cirrus_shadow_gr1;
1784
        } else if (mode == 5) {
1785
            *dst++ = s->cirrus_shadow_gr0;
1786
        }
1787
        val <<= 1;
1788
    }
1789
    cpu_physical_memory_set_dirty(s->vram_offset + offset);
1790
    cpu_physical_memory_set_dirty(s->vram_offset + offset + 7);
1791
}
1792

    
1793
static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
1794
                                              unsigned mode,
1795
                                              unsigned offset,
1796
                                              uint32_t mem_value)
1797
{
1798
    int x;
1799
    unsigned val = mem_value;
1800
    uint8_t *dst;
1801

    
1802
    dst = s->vram_ptr + offset;
1803
    for (x = 0; x < 8; x++) {
1804
        if (val & 0x80) {
1805
            *dst++ = s->cirrus_shadow_gr1;
1806
            *dst++ = s->gr[0x11];
1807
        } else if (mode == 5) {
1808
            *dst++ = s->cirrus_shadow_gr0;
1809
            *dst++ = s->gr[0x10];
1810
        }
1811
        val <<= 1;
1812
    }
1813
    cpu_physical_memory_set_dirty(s->vram_offset + offset);
1814
    cpu_physical_memory_set_dirty(s->vram_offset + offset + 15);
1815
}
1816

    
1817
/***************************************
1818
 *
1819
 *  memory access between 0xa0000-0xbffff
1820
 *
1821
 ***************************************/
1822

    
1823
static uint32_t cirrus_vga_mem_readb(void *opaque, target_phys_addr_t addr)
1824
{
1825
    CirrusVGAState *s = opaque;
1826
    unsigned bank_index;
1827
    unsigned bank_offset;
1828
    uint32_t val;
1829

    
1830
    if ((s->sr[0x07] & 0x01) == 0) {
1831
        return vga_mem_readb(s, addr);
1832
    }
1833

    
1834
    addr &= 0x1ffff;
1835

    
1836
    if (addr < 0x10000) {
1837
        /* XXX handle bitblt */
1838
        /* video memory */
1839
        bank_index = addr >> 15;
1840
        bank_offset = addr & 0x7fff;
1841
        if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1842
            bank_offset += s->cirrus_bank_base[bank_index];
1843
            if ((s->gr[0x0B] & 0x14) == 0x14) {
1844
                bank_offset <<= 4;
1845
            } else if (s->gr[0x0B] & 0x02) {
1846
                bank_offset <<= 3;
1847
            }
1848
            bank_offset &= s->cirrus_addr_mask;
1849
            val = *(s->vram_ptr + bank_offset);
1850
        } else
1851
            val = 0xff;
1852
    } else if (addr >= 0x18000 && addr < 0x18100) {
1853
        /* memory-mapped I/O */
1854
        val = 0xff;
1855
        if ((s->sr[0x17] & 0x44) == 0x04) {
1856
            val = cirrus_mmio_blt_read(s, addr & 0xff);
1857
        }
1858
    } else {
1859
        val = 0xff;
1860
#ifdef DEBUG_CIRRUS
1861
        printf("cirrus: mem_readb %06x\n", addr);
1862
#endif
1863
    }
1864
    return val;
1865
}
1866

    
1867
static uint32_t cirrus_vga_mem_readw(void *opaque, target_phys_addr_t addr)
1868
{
1869
    uint32_t v;
1870
#ifdef TARGET_WORDS_BIGENDIAN
1871
    v = cirrus_vga_mem_readb(opaque, addr) << 8;
1872
    v |= cirrus_vga_mem_readb(opaque, addr + 1);
1873
#else
1874
    v = cirrus_vga_mem_readb(opaque, addr);
1875
    v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1876
#endif
1877
    return v;
1878
}
1879

    
1880
static uint32_t cirrus_vga_mem_readl(void *opaque, target_phys_addr_t addr)
1881
{
1882
    uint32_t v;
1883
#ifdef TARGET_WORDS_BIGENDIAN
1884
    v = cirrus_vga_mem_readb(opaque, addr) << 24;
1885
    v |= cirrus_vga_mem_readb(opaque, addr + 1) << 16;
1886
    v |= cirrus_vga_mem_readb(opaque, addr + 2) << 8;
1887
    v |= cirrus_vga_mem_readb(opaque, addr + 3);
1888
#else
1889
    v = cirrus_vga_mem_readb(opaque, addr);
1890
    v |= cirrus_vga_mem_readb(opaque, addr + 1) << 8;
1891
    v |= cirrus_vga_mem_readb(opaque, addr + 2) << 16;
1892
    v |= cirrus_vga_mem_readb(opaque, addr + 3) << 24;
1893
#endif
1894
    return v;
1895
}
1896

    
1897
static void cirrus_vga_mem_writeb(void *opaque, target_phys_addr_t addr, 
1898
                                  uint32_t mem_value)
1899
{
1900
    CirrusVGAState *s = opaque;
1901
    unsigned bank_index;
1902
    unsigned bank_offset;
1903
    unsigned mode;
1904

    
1905
    if ((s->sr[0x07] & 0x01) == 0) {
1906
        vga_mem_writeb(s, addr, mem_value);
1907
        return;
1908
    }
1909

    
1910
    addr &= 0x1ffff;
1911

    
1912
    if (addr < 0x10000) {
1913
        if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
1914
            /* bitblt */
1915
            *s->cirrus_srcptr++ = (uint8_t) mem_value;
1916
            if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
1917
                cirrus_bitblt_cputovideo_next(s);
1918
            }
1919
        } else {
1920
            /* video memory */
1921
            bank_index = addr >> 15;
1922
            bank_offset = addr & 0x7fff;
1923
            if (bank_offset < s->cirrus_bank_limit[bank_index]) {
1924
                bank_offset += s->cirrus_bank_base[bank_index];
1925
                if ((s->gr[0x0B] & 0x14) == 0x14) {
1926
                    bank_offset <<= 4;
1927
                } else if (s->gr[0x0B] & 0x02) {
1928
                    bank_offset <<= 3;
1929
                }
1930
                bank_offset &= s->cirrus_addr_mask;
1931
                mode = s->gr[0x05] & 0x7;
1932
                if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
1933
                    *(s->vram_ptr + bank_offset) = mem_value;
1934
                    cpu_physical_memory_set_dirty(s->vram_offset +
1935
                                                  bank_offset);
1936
                } else {
1937
                    if ((s->gr[0x0B] & 0x14) != 0x14) {
1938
                        cirrus_mem_writeb_mode4and5_8bpp(s, mode,
1939
                                                         bank_offset,
1940
                                                         mem_value);
1941
                    } else {
1942
                        cirrus_mem_writeb_mode4and5_16bpp(s, mode,
1943
                                                          bank_offset,
1944
                                                          mem_value);
1945
                    }
1946
                }
1947
            }
1948
        }
1949
    } else if (addr >= 0x18000 && addr < 0x18100) {
1950
        /* memory-mapped I/O */
1951
        if ((s->sr[0x17] & 0x44) == 0x04) {
1952
            cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
1953
        }
1954
    } else {
1955
#ifdef DEBUG_CIRRUS
1956
        printf("cirrus: mem_writeb %06x value %02x\n", addr, mem_value);
1957
#endif
1958
    }
1959
}
1960

    
1961
static void cirrus_vga_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1962
{
1963
#ifdef TARGET_WORDS_BIGENDIAN
1964
    cirrus_vga_mem_writeb(opaque, addr, (val >> 8) & 0xff);
1965
    cirrus_vga_mem_writeb(opaque, addr + 1, val & 0xff);
1966
#else
1967
    cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
1968
    cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
1969
#endif
1970
}
1971

    
1972
static void cirrus_vga_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1973
{
1974
#ifdef TARGET_WORDS_BIGENDIAN
1975
    cirrus_vga_mem_writeb(opaque, addr, (val >> 24) & 0xff);
1976
    cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 16) & 0xff);
1977
    cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 8) & 0xff);
1978
    cirrus_vga_mem_writeb(opaque, addr + 3, val & 0xff);
1979
#else
1980
    cirrus_vga_mem_writeb(opaque, addr, val & 0xff);
1981
    cirrus_vga_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
1982
    cirrus_vga_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
1983
    cirrus_vga_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
1984
#endif
1985
}
1986

    
1987
static CPUReadMemoryFunc *cirrus_vga_mem_read[3] = {
1988
    cirrus_vga_mem_readb,
1989
    cirrus_vga_mem_readw,
1990
    cirrus_vga_mem_readl,
1991
};
1992

    
1993
static CPUWriteMemoryFunc *cirrus_vga_mem_write[3] = {
1994
    cirrus_vga_mem_writeb,
1995
    cirrus_vga_mem_writew,
1996
    cirrus_vga_mem_writel,
1997
};
1998

    
1999
/***************************************
2000
 *
2001
 *  hardware cursor
2002
 *
2003
 ***************************************/
2004

    
2005
static inline void invalidate_cursor1(CirrusVGAState *s)
2006
{
2007
    if (s->last_hw_cursor_size) {
2008
        vga_invalidate_scanlines((VGAState *)s, 
2009
                                 s->last_hw_cursor_y + s->last_hw_cursor_y_start,
2010
                                 s->last_hw_cursor_y + s->last_hw_cursor_y_end);
2011
    }
2012
}
2013

    
2014
static inline void cirrus_cursor_compute_yrange(CirrusVGAState *s)
2015
{
2016
    const uint8_t *src;
2017
    uint32_t content;
2018
    int y, y_min, y_max;
2019

    
2020
    src = s->vram_ptr + s->real_vram_size - 16 * 1024;
2021
    if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2022
        src += (s->sr[0x13] & 0x3c) * 256;
2023
        y_min = 64;
2024
        y_max = -1;
2025
        for(y = 0; y < 64; y++) {
2026
            content = ((uint32_t *)src)[0] |
2027
                ((uint32_t *)src)[1] |
2028
                ((uint32_t *)src)[2] |
2029
                ((uint32_t *)src)[3];
2030
            if (content) {
2031
                if (y < y_min)
2032
                    y_min = y;
2033
                if (y > y_max)
2034
                    y_max = y;
2035
            }
2036
            src += 16;
2037
        }
2038
    } else {
2039
        src += (s->sr[0x13] & 0x3f) * 256;
2040
        y_min = 32;
2041
        y_max = -1;
2042
        for(y = 0; y < 32; y++) {
2043
            content = ((uint32_t *)src)[0] |
2044
                ((uint32_t *)(src + 128))[0];
2045
            if (content) {
2046
                if (y < y_min)
2047
                    y_min = y;
2048
                if (y > y_max)
2049
                    y_max = y;
2050
            }
2051
            src += 4;
2052
        }
2053
    }
2054
    if (y_min > y_max) {
2055
        s->last_hw_cursor_y_start = 0;
2056
        s->last_hw_cursor_y_end = 0;
2057
    } else {
2058
        s->last_hw_cursor_y_start = y_min;
2059
        s->last_hw_cursor_y_end = y_max + 1;
2060
    }
2061
}
2062

    
2063
/* NOTE: we do not currently handle the cursor bitmap change, so we
2064
   update the cursor only if it moves. */
2065
static void cirrus_cursor_invalidate(VGAState *s1)
2066
{
2067
    CirrusVGAState *s = (CirrusVGAState *)s1;
2068
    int size;
2069

    
2070
    if (!s->sr[0x12] & CIRRUS_CURSOR_SHOW) {
2071
        size = 0;
2072
    } else {
2073
        if (s->sr[0x12] & CIRRUS_CURSOR_LARGE)
2074
            size = 64;
2075
        else
2076
            size = 32;
2077
    }
2078
    /* invalidate last cursor and new cursor if any change */
2079
    if (s->last_hw_cursor_size != size ||
2080
        s->last_hw_cursor_x != s->hw_cursor_x ||
2081
        s->last_hw_cursor_y != s->hw_cursor_y) {
2082

    
2083
        invalidate_cursor1(s);
2084
        
2085
        s->last_hw_cursor_size = size;
2086
        s->last_hw_cursor_x = s->hw_cursor_x;
2087
        s->last_hw_cursor_y = s->hw_cursor_y;
2088
        /* compute the real cursor min and max y */
2089
        cirrus_cursor_compute_yrange(s);
2090
        invalidate_cursor1(s);
2091
    }
2092
}
2093

    
2094
static void cirrus_cursor_draw_line(VGAState *s1, uint8_t *d1, int scr_y)
2095
{
2096
    CirrusVGAState *s = (CirrusVGAState *)s1;
2097
    int w, h, bpp, x1, x2, poffset;
2098
    unsigned int color0, color1;
2099
    const uint8_t *palette, *src;
2100
    uint32_t content;
2101
    
2102
    if (!(s->sr[0x12] & CIRRUS_CURSOR_SHOW)) 
2103
        return;
2104
    /* fast test to see if the cursor intersects with the scan line */
2105
    if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2106
        h = 64;
2107
    } else {
2108
        h = 32;
2109
    }
2110
    if (scr_y < s->hw_cursor_y ||
2111
        scr_y >= (s->hw_cursor_y + h))
2112
        return;
2113
    
2114
    src = s->vram_ptr + s->real_vram_size - 16 * 1024;
2115
    if (s->sr[0x12] & CIRRUS_CURSOR_LARGE) {
2116
        src += (s->sr[0x13] & 0x3c) * 256;
2117
        src += (scr_y - s->hw_cursor_y) * 16;
2118
        poffset = 8;
2119
        content = ((uint32_t *)src)[0] |
2120
            ((uint32_t *)src)[1] |
2121
            ((uint32_t *)src)[2] |
2122
            ((uint32_t *)src)[3];
2123
    } else {
2124
        src += (s->sr[0x13] & 0x3f) * 256;
2125
        src += (scr_y - s->hw_cursor_y) * 4;
2126
        poffset = 128;
2127
        content = ((uint32_t *)src)[0] |
2128
            ((uint32_t *)(src + 128))[0];
2129
    }
2130
    /* if nothing to draw, no need to continue */
2131
    if (!content)
2132
        return;
2133
    w = h;
2134

    
2135
    x1 = s->hw_cursor_x;
2136
    if (x1 >= s->last_scr_width)
2137
        return;
2138
    x2 = s->hw_cursor_x + w;
2139
    if (x2 > s->last_scr_width)
2140
        x2 = s->last_scr_width;
2141
    w = x2 - x1;
2142
    palette = s->cirrus_hidden_palette;
2143
    color0 = s->rgb_to_pixel(c6_to_8(palette[0x0 * 3]), 
2144
                             c6_to_8(palette[0x0 * 3 + 1]), 
2145
                             c6_to_8(palette[0x0 * 3 + 2]));
2146
    color1 = s->rgb_to_pixel(c6_to_8(palette[0xf * 3]), 
2147
                             c6_to_8(palette[0xf * 3 + 1]), 
2148
                             c6_to_8(palette[0xf * 3 + 2]));
2149
    bpp = ((s->ds->depth + 7) >> 3);
2150
    d1 += x1 * bpp;
2151
    switch(s->ds->depth) {
2152
    default:
2153
        break;
2154
    case 8:
2155
        vga_draw_cursor_line_8(d1, src, poffset, w, color0, color1, 0xff);
2156
        break;
2157
    case 15:
2158
        vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0x7fff);
2159
        break;
2160
    case 16:
2161
        vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0xffff);
2162
        break;
2163
    case 32:
2164
        vga_draw_cursor_line_32(d1, src, poffset, w, color0, color1, 0xffffff);
2165
        break;
2166
    }
2167
}
2168

    
2169
/***************************************
2170
 *
2171
 *  LFB memory access
2172
 *
2173
 ***************************************/
2174

    
2175
static uint32_t cirrus_linear_readb(void *opaque, target_phys_addr_t addr)
2176
{
2177
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2178
    uint32_t ret;
2179

    
2180
    addr &= s->cirrus_addr_mask;
2181

    
2182
    if (((s->sr[0x17] & 0x44) == 0x44) && 
2183
        ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
2184
        /* memory-mapped I/O */
2185
        ret = cirrus_mmio_blt_read(s, addr & 0xff);
2186
    } else if (0) {
2187
        /* XXX handle bitblt */
2188
        ret = 0xff;
2189
    } else {
2190
        /* video memory */
2191
        if ((s->gr[0x0B] & 0x14) == 0x14) {
2192
            addr <<= 4;
2193
        } else if (s->gr[0x0B] & 0x02) {
2194
            addr <<= 3;
2195
        }
2196
        addr &= s->cirrus_addr_mask;
2197
        ret = *(s->vram_ptr + addr);
2198
    }
2199

    
2200
    return ret;
2201
}
2202

    
2203
static uint32_t cirrus_linear_readw(void *opaque, target_phys_addr_t addr)
2204
{
2205
    uint32_t v;
2206
#ifdef TARGET_WORDS_BIGENDIAN
2207
    v = cirrus_linear_readb(opaque, addr) << 8;
2208
    v |= cirrus_linear_readb(opaque, addr + 1);
2209
#else
2210
    v = cirrus_linear_readb(opaque, addr);
2211
    v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2212
#endif
2213
    return v;
2214
}
2215

    
2216
static uint32_t cirrus_linear_readl(void *opaque, target_phys_addr_t addr)
2217
{
2218
    uint32_t v;
2219
#ifdef TARGET_WORDS_BIGENDIAN
2220
    v = cirrus_linear_readb(opaque, addr) << 24;
2221
    v |= cirrus_linear_readb(opaque, addr + 1) << 16;
2222
    v |= cirrus_linear_readb(opaque, addr + 2) << 8;
2223
    v |= cirrus_linear_readb(opaque, addr + 3);
2224
#else
2225
    v = cirrus_linear_readb(opaque, addr);
2226
    v |= cirrus_linear_readb(opaque, addr + 1) << 8;
2227
    v |= cirrus_linear_readb(opaque, addr + 2) << 16;
2228
    v |= cirrus_linear_readb(opaque, addr + 3) << 24;
2229
#endif
2230
    return v;
2231
}
2232

    
2233
static void cirrus_linear_writeb(void *opaque, target_phys_addr_t addr,
2234
                                 uint32_t val)
2235
{
2236
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2237
    unsigned mode;
2238

    
2239
    addr &= s->cirrus_addr_mask;
2240
        
2241
    if (((s->sr[0x17] & 0x44) == 0x44) && 
2242
        ((addr & s->linear_mmio_mask) ==  s->linear_mmio_mask)) {
2243
        /* memory-mapped I/O */
2244
        cirrus_mmio_blt_write(s, addr & 0xff, val);
2245
    } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2246
        /* bitblt */
2247
        *s->cirrus_srcptr++ = (uint8_t) val;
2248
        if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2249
            cirrus_bitblt_cputovideo_next(s);
2250
        }
2251
    } else {
2252
        /* video memory */
2253
        if ((s->gr[0x0B] & 0x14) == 0x14) {
2254
            addr <<= 4;
2255
        } else if (s->gr[0x0B] & 0x02) {
2256
            addr <<= 3;
2257
        }
2258
        addr &= s->cirrus_addr_mask;
2259

    
2260
        mode = s->gr[0x05] & 0x7;
2261
        if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2262
            *(s->vram_ptr + addr) = (uint8_t) val;
2263
            cpu_physical_memory_set_dirty(s->vram_offset + addr);
2264
        } else {
2265
            if ((s->gr[0x0B] & 0x14) != 0x14) {
2266
                cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
2267
            } else {
2268
                cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
2269
            }
2270
        }
2271
    }
2272
}
2273

    
2274
static void cirrus_linear_writew(void *opaque, target_phys_addr_t addr,
2275
                                 uint32_t val)
2276
{
2277
#ifdef TARGET_WORDS_BIGENDIAN
2278
    cirrus_linear_writeb(opaque, addr, (val >> 8) & 0xff);
2279
    cirrus_linear_writeb(opaque, addr + 1, val & 0xff);
2280
#else
2281
    cirrus_linear_writeb(opaque, addr, val & 0xff);
2282
    cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2283
#endif
2284
}
2285

    
2286
static void cirrus_linear_writel(void *opaque, target_phys_addr_t addr,
2287
                                 uint32_t val)
2288
{
2289
#ifdef TARGET_WORDS_BIGENDIAN
2290
    cirrus_linear_writeb(opaque, addr, (val >> 24) & 0xff);
2291
    cirrus_linear_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2292
    cirrus_linear_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2293
    cirrus_linear_writeb(opaque, addr + 3, val & 0xff);
2294
#else
2295
    cirrus_linear_writeb(opaque, addr, val & 0xff);
2296
    cirrus_linear_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2297
    cirrus_linear_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2298
    cirrus_linear_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2299
#endif
2300
}
2301

    
2302

    
2303
static CPUReadMemoryFunc *cirrus_linear_read[3] = {
2304
    cirrus_linear_readb,
2305
    cirrus_linear_readw,
2306
    cirrus_linear_readl,
2307
};
2308

    
2309
static CPUWriteMemoryFunc *cirrus_linear_write[3] = {
2310
    cirrus_linear_writeb,
2311
    cirrus_linear_writew,
2312
    cirrus_linear_writel,
2313
};
2314

    
2315
static void cirrus_linear_mem_writeb(void *opaque, target_phys_addr_t addr,
2316
                                     uint32_t val)
2317
{
2318
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2319

    
2320
    addr &= s->cirrus_addr_mask;
2321
    *(s->vram_ptr + addr) = val;
2322
    cpu_physical_memory_set_dirty(s->vram_offset + addr);
2323
}
2324

    
2325
static void cirrus_linear_mem_writew(void *opaque, target_phys_addr_t addr,
2326
                                     uint32_t val)
2327
{
2328
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2329

    
2330
    addr &= s->cirrus_addr_mask;
2331
    cpu_to_le16w((uint16_t *)(s->vram_ptr + addr), val);
2332
    cpu_physical_memory_set_dirty(s->vram_offset + addr);
2333
}
2334

    
2335
static void cirrus_linear_mem_writel(void *opaque, target_phys_addr_t addr,
2336
                                     uint32_t val)
2337
{
2338
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2339

    
2340
    addr &= s->cirrus_addr_mask;
2341
    cpu_to_le32w((uint32_t *)(s->vram_ptr + addr), val);
2342
    cpu_physical_memory_set_dirty(s->vram_offset + addr);
2343
}
2344

    
2345
/***************************************
2346
 *
2347
 *  system to screen memory access
2348
 *
2349
 ***************************************/
2350

    
2351

    
2352
static uint32_t cirrus_linear_bitblt_readb(void *opaque, target_phys_addr_t addr)
2353
{
2354
    uint32_t ret;
2355

    
2356
    /* XXX handle bitblt */
2357
    ret = 0xff;
2358
    return ret;
2359
}
2360

    
2361
static uint32_t cirrus_linear_bitblt_readw(void *opaque, target_phys_addr_t addr)
2362
{
2363
    uint32_t v;
2364
#ifdef TARGET_WORDS_BIGENDIAN
2365
    v = cirrus_linear_bitblt_readb(opaque, addr) << 8;
2366
    v |= cirrus_linear_bitblt_readb(opaque, addr + 1);
2367
#else
2368
    v = cirrus_linear_bitblt_readb(opaque, addr);
2369
    v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2370
#endif
2371
    return v;
2372
}
2373

    
2374
static uint32_t cirrus_linear_bitblt_readl(void *opaque, target_phys_addr_t addr)
2375
{
2376
    uint32_t v;
2377
#ifdef TARGET_WORDS_BIGENDIAN
2378
    v = cirrus_linear_bitblt_readb(opaque, addr) << 24;
2379
    v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 16;
2380
    v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 8;
2381
    v |= cirrus_linear_bitblt_readb(opaque, addr + 3);
2382
#else
2383
    v = cirrus_linear_bitblt_readb(opaque, addr);
2384
    v |= cirrus_linear_bitblt_readb(opaque, addr + 1) << 8;
2385
    v |= cirrus_linear_bitblt_readb(opaque, addr + 2) << 16;
2386
    v |= cirrus_linear_bitblt_readb(opaque, addr + 3) << 24;
2387
#endif
2388
    return v;
2389
}
2390

    
2391
static void cirrus_linear_bitblt_writeb(void *opaque, target_phys_addr_t addr,
2392
                                 uint32_t val)
2393
{
2394
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2395

    
2396
    if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2397
        /* bitblt */
2398
        *s->cirrus_srcptr++ = (uint8_t) val;
2399
        if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
2400
            cirrus_bitblt_cputovideo_next(s);
2401
        }
2402
    }
2403
}
2404

    
2405
static void cirrus_linear_bitblt_writew(void *opaque, target_phys_addr_t addr,
2406
                                 uint32_t val)
2407
{
2408
#ifdef TARGET_WORDS_BIGENDIAN
2409
    cirrus_linear_bitblt_writeb(opaque, addr, (val >> 8) & 0xff);
2410
    cirrus_linear_bitblt_writeb(opaque, addr + 1, val & 0xff);
2411
#else
2412
    cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2413
    cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2414
#endif
2415
}
2416

    
2417
static void cirrus_linear_bitblt_writel(void *opaque, target_phys_addr_t addr,
2418
                                 uint32_t val)
2419
{
2420
#ifdef TARGET_WORDS_BIGENDIAN
2421
    cirrus_linear_bitblt_writeb(opaque, addr, (val >> 24) & 0xff);
2422
    cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2423
    cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2424
    cirrus_linear_bitblt_writeb(opaque, addr + 3, val & 0xff);
2425
#else
2426
    cirrus_linear_bitblt_writeb(opaque, addr, val & 0xff);
2427
    cirrus_linear_bitblt_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2428
    cirrus_linear_bitblt_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2429
    cirrus_linear_bitblt_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2430
#endif
2431
}
2432

    
2433

    
2434
static CPUReadMemoryFunc *cirrus_linear_bitblt_read[3] = {
2435
    cirrus_linear_bitblt_readb,
2436
    cirrus_linear_bitblt_readw,
2437
    cirrus_linear_bitblt_readl,
2438
};
2439

    
2440
static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = {
2441
    cirrus_linear_bitblt_writeb,
2442
    cirrus_linear_bitblt_writew,
2443
    cirrus_linear_bitblt_writel,
2444
};
2445

    
2446
/* Compute the memory access functions */
2447
static void cirrus_update_memory_access(CirrusVGAState *s)
2448
{
2449
    unsigned mode;
2450

    
2451
    if ((s->sr[0x17] & 0x44) == 0x44) {
2452
        goto generic_io;
2453
    } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
2454
        goto generic_io;
2455
    } else {
2456
        if ((s->gr[0x0B] & 0x14) == 0x14) {
2457
            goto generic_io;
2458
        } else if (s->gr[0x0B] & 0x02) {
2459
            goto generic_io;
2460
        }
2461
        
2462
        mode = s->gr[0x05] & 0x7;
2463
        if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
2464
            s->cirrus_linear_write[0] = cirrus_linear_mem_writeb;
2465
            s->cirrus_linear_write[1] = cirrus_linear_mem_writew;
2466
            s->cirrus_linear_write[2] = cirrus_linear_mem_writel;
2467
        } else {
2468
        generic_io:
2469
            s->cirrus_linear_write[0] = cirrus_linear_writeb;
2470
            s->cirrus_linear_write[1] = cirrus_linear_writew;
2471
            s->cirrus_linear_write[2] = cirrus_linear_writel;
2472
        }
2473
    }
2474
}
2475

    
2476

    
2477
/* I/O ports */
2478

    
2479
static uint32_t vga_ioport_read(void *opaque, uint32_t addr)
2480
{
2481
    CirrusVGAState *s = opaque;
2482
    int val, index;
2483

    
2484
    /* check port range access depending on color/monochrome mode */
2485
    if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2486
        || (addr >= 0x3d0 && addr <= 0x3df
2487
            && !(s->msr & MSR_COLOR_EMULATION))) {
2488
        val = 0xff;
2489
    } else {
2490
        switch (addr) {
2491
        case 0x3c0:
2492
            if (s->ar_flip_flop == 0) {
2493
                val = s->ar_index;
2494
            } else {
2495
                val = 0;
2496
            }
2497
            break;
2498
        case 0x3c1:
2499
            index = s->ar_index & 0x1f;
2500
            if (index < 21)
2501
                val = s->ar[index];
2502
            else
2503
                val = 0;
2504
            break;
2505
        case 0x3c2:
2506
            val = s->st00;
2507
            break;
2508
        case 0x3c4:
2509
            val = s->sr_index;
2510
            break;
2511
        case 0x3c5:
2512
            if (cirrus_hook_read_sr(s, s->sr_index, &val))
2513
                break;
2514
            val = s->sr[s->sr_index];
2515
#ifdef DEBUG_VGA_REG
2516
            printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
2517
#endif
2518
            break;
2519
        case 0x3c6:
2520
            cirrus_read_hidden_dac(s, &val);
2521
            break;
2522
        case 0x3c7:
2523
            val = s->dac_state;
2524
            break;
2525
        case 0x3c8:
2526
            val = s->dac_write_index;
2527
            s->cirrus_hidden_dac_lockindex = 0;
2528
            break;
2529
        case 0x3c9:
2530
            if (cirrus_hook_read_palette(s, &val))
2531
                break;
2532
            val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
2533
            if (++s->dac_sub_index == 3) {
2534
                s->dac_sub_index = 0;
2535
                s->dac_read_index++;
2536
            }
2537
            break;
2538
        case 0x3ca:
2539
            val = s->fcr;
2540
            break;
2541
        case 0x3cc:
2542
            val = s->msr;
2543
            break;
2544
        case 0x3ce:
2545
            val = s->gr_index;
2546
            break;
2547
        case 0x3cf:
2548
            if (cirrus_hook_read_gr(s, s->gr_index, &val))
2549
                break;
2550
            val = s->gr[s->gr_index];
2551
#ifdef DEBUG_VGA_REG
2552
            printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
2553
#endif
2554
            break;
2555
        case 0x3b4:
2556
        case 0x3d4:
2557
            val = s->cr_index;
2558
            break;
2559
        case 0x3b5:
2560
        case 0x3d5:
2561
            if (cirrus_hook_read_cr(s, s->cr_index, &val))
2562
                break;
2563
            val = s->cr[s->cr_index];
2564
#ifdef DEBUG_VGA_REG
2565
            printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
2566
#endif
2567
            break;
2568
        case 0x3ba:
2569
        case 0x3da:
2570
            /* just toggle to fool polling */
2571
            s->st01 ^= ST01_V_RETRACE | ST01_DISP_ENABLE;
2572
            val = s->st01;
2573
            s->ar_flip_flop = 0;
2574
            break;
2575
        default:
2576
            val = 0x00;
2577
            break;
2578
        }
2579
    }
2580
#if defined(DEBUG_VGA)
2581
    printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
2582
#endif
2583
    return val;
2584
}
2585

    
2586
static void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
2587
{
2588
    CirrusVGAState *s = opaque;
2589
    int index;
2590

    
2591
    /* check port range access depending on color/monochrome mode */
2592
    if ((addr >= 0x3b0 && addr <= 0x3bf && (s->msr & MSR_COLOR_EMULATION))
2593
        || (addr >= 0x3d0 && addr <= 0x3df
2594
            && !(s->msr & MSR_COLOR_EMULATION)))
2595
        return;
2596

    
2597
#ifdef DEBUG_VGA
2598
    printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
2599
#endif
2600

    
2601
    switch (addr) {
2602
    case 0x3c0:
2603
        if (s->ar_flip_flop == 0) {
2604
            val &= 0x3f;
2605
            s->ar_index = val;
2606
        } else {
2607
            index = s->ar_index & 0x1f;
2608
            switch (index) {
2609
            case 0x00 ... 0x0f:
2610
                s->ar[index] = val & 0x3f;
2611
                break;
2612
            case 0x10:
2613
                s->ar[index] = val & ~0x10;
2614
                break;
2615
            case 0x11:
2616
                s->ar[index] = val;
2617
                break;
2618
            case 0x12:
2619
                s->ar[index] = val & ~0xc0;
2620
                break;
2621
            case 0x13:
2622
                s->ar[index] = val & ~0xf0;
2623
                break;
2624
            case 0x14:
2625
                s->ar[index] = val & ~0xf0;
2626
                break;
2627
            default:
2628
                break;
2629
            }
2630
        }
2631
        s->ar_flip_flop ^= 1;
2632
        break;
2633
    case 0x3c2:
2634
        s->msr = val & ~0x10;
2635
        break;
2636
    case 0x3c4:
2637
        s->sr_index = val;
2638
        break;
2639
    case 0x3c5:
2640
        if (cirrus_hook_write_sr(s, s->sr_index, val))
2641
            break;
2642
#ifdef DEBUG_VGA_REG
2643
        printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
2644
#endif
2645
        s->sr[s->sr_index] = val & sr_mask[s->sr_index];
2646
        break;
2647
    case 0x3c6:
2648
        cirrus_write_hidden_dac(s, val);
2649
        break;
2650
    case 0x3c7:
2651
        s->dac_read_index = val;
2652
        s->dac_sub_index = 0;
2653
        s->dac_state = 3;
2654
        break;
2655
    case 0x3c8:
2656
        s->dac_write_index = val;
2657
        s->dac_sub_index = 0;
2658
        s->dac_state = 0;
2659
        break;
2660
    case 0x3c9:
2661
        if (cirrus_hook_write_palette(s, val))
2662
            break;
2663
        s->dac_cache[s->dac_sub_index] = val;
2664
        if (++s->dac_sub_index == 3) {
2665
            memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
2666
            s->dac_sub_index = 0;
2667
            s->dac_write_index++;
2668
        }
2669
        break;
2670
    case 0x3ce:
2671
        s->gr_index = val;
2672
        break;
2673
    case 0x3cf:
2674
        if (cirrus_hook_write_gr(s, s->gr_index, val))
2675
            break;
2676
#ifdef DEBUG_VGA_REG
2677
        printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
2678
#endif
2679
        s->gr[s->gr_index] = val & gr_mask[s->gr_index];
2680
        break;
2681
    case 0x3b4:
2682
    case 0x3d4:
2683
        s->cr_index = val;
2684
        break;
2685
    case 0x3b5:
2686
    case 0x3d5:
2687
        if (cirrus_hook_write_cr(s, s->cr_index, val))
2688
            break;
2689
#ifdef DEBUG_VGA_REG
2690
        printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
2691
#endif
2692
        /* handle CR0-7 protection */
2693
        if ((s->cr[0x11] & 0x80) && s->cr_index <= 7) {
2694
            /* can always write bit 4 of CR7 */
2695
            if (s->cr_index == 7)
2696
                s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
2697
            return;
2698
        }
2699
        switch (s->cr_index) {
2700
        case 0x01:                /* horizontal display end */
2701
        case 0x07:
2702
        case 0x09:
2703
        case 0x0c:
2704
        case 0x0d:
2705
        case 0x12:                /* veritcal display end */
2706
            s->cr[s->cr_index] = val;
2707
            break;
2708

    
2709
        default:
2710
            s->cr[s->cr_index] = val;
2711
            break;
2712
        }
2713
        break;
2714
    case 0x3ba:
2715
    case 0x3da:
2716
        s->fcr = val & 0x10;
2717
        break;
2718
    }
2719
}
2720

    
2721
/***************************************
2722
 *
2723
 *  memory-mapped I/O access
2724
 *
2725
 ***************************************/
2726

    
2727
static uint32_t cirrus_mmio_readb(void *opaque, target_phys_addr_t addr)
2728
{
2729
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2730

    
2731
    addr &= CIRRUS_PNPMMIO_SIZE - 1;
2732

    
2733
    if (addr >= 0x100) {
2734
        return cirrus_mmio_blt_read(s, addr - 0x100);
2735
    } else {
2736
        return vga_ioport_read(s, addr + 0x3c0);
2737
    }
2738
}
2739

    
2740
static uint32_t cirrus_mmio_readw(void *opaque, target_phys_addr_t addr)
2741
{
2742
    uint32_t v;
2743
#ifdef TARGET_WORDS_BIGENDIAN
2744
    v = cirrus_mmio_readb(opaque, addr) << 8;
2745
    v |= cirrus_mmio_readb(opaque, addr + 1);
2746
#else
2747
    v = cirrus_mmio_readb(opaque, addr);
2748
    v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2749
#endif
2750
    return v;
2751
}
2752

    
2753
static uint32_t cirrus_mmio_readl(void *opaque, target_phys_addr_t addr)
2754
{
2755
    uint32_t v;
2756
#ifdef TARGET_WORDS_BIGENDIAN
2757
    v = cirrus_mmio_readb(opaque, addr) << 24;
2758
    v |= cirrus_mmio_readb(opaque, addr + 1) << 16;
2759
    v |= cirrus_mmio_readb(opaque, addr + 2) << 8;
2760
    v |= cirrus_mmio_readb(opaque, addr + 3);
2761
#else
2762
    v = cirrus_mmio_readb(opaque, addr);
2763
    v |= cirrus_mmio_readb(opaque, addr + 1) << 8;
2764
    v |= cirrus_mmio_readb(opaque, addr + 2) << 16;
2765
    v |= cirrus_mmio_readb(opaque, addr + 3) << 24;
2766
#endif
2767
    return v;
2768
}
2769

    
2770
static void cirrus_mmio_writeb(void *opaque, target_phys_addr_t addr,
2771
                               uint32_t val)
2772
{
2773
    CirrusVGAState *s = (CirrusVGAState *) opaque;
2774

    
2775
    addr &= CIRRUS_PNPMMIO_SIZE - 1;
2776

    
2777
    if (addr >= 0x100) {
2778
        cirrus_mmio_blt_write(s, addr - 0x100, val);
2779
    } else {
2780
        vga_ioport_write(s, addr + 0x3c0, val);
2781
    }
2782
}
2783

    
2784
static void cirrus_mmio_writew(void *opaque, target_phys_addr_t addr,
2785
                               uint32_t val)
2786
{
2787
#ifdef TARGET_WORDS_BIGENDIAN
2788
    cirrus_mmio_writeb(opaque, addr, (val >> 8) & 0xff);
2789
    cirrus_mmio_writeb(opaque, addr + 1, val & 0xff);
2790
#else
2791
    cirrus_mmio_writeb(opaque, addr, val & 0xff);
2792
    cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2793
#endif
2794
}
2795

    
2796
static void cirrus_mmio_writel(void *opaque, target_phys_addr_t addr,
2797
                               uint32_t val)
2798
{
2799
#ifdef TARGET_WORDS_BIGENDIAN
2800
    cirrus_mmio_writeb(opaque, addr, (val >> 24) & 0xff);
2801
    cirrus_mmio_writeb(opaque, addr + 1, (val >> 16) & 0xff);
2802
    cirrus_mmio_writeb(opaque, addr + 2, (val >> 8) & 0xff);
2803
    cirrus_mmio_writeb(opaque, addr + 3, val & 0xff);
2804
#else
2805
    cirrus_mmio_writeb(opaque, addr, val & 0xff);
2806
    cirrus_mmio_writeb(opaque, addr + 1, (val >> 8) & 0xff);
2807
    cirrus_mmio_writeb(opaque, addr + 2, (val >> 16) & 0xff);
2808
    cirrus_mmio_writeb(opaque, addr + 3, (val >> 24) & 0xff);
2809
#endif
2810
}
2811

    
2812

    
2813
static CPUReadMemoryFunc *cirrus_mmio_read[3] = {
2814
    cirrus_mmio_readb,
2815
    cirrus_mmio_readw,
2816
    cirrus_mmio_readl,
2817
};
2818

    
2819
static CPUWriteMemoryFunc *cirrus_mmio_write[3] = {
2820
    cirrus_mmio_writeb,
2821
    cirrus_mmio_writew,
2822
    cirrus_mmio_writel,
2823
};
2824

    
2825
/* load/save state */
2826

    
2827
static void cirrus_vga_save(QEMUFile *f, void *opaque)
2828
{
2829
    CirrusVGAState *s = opaque;
2830

    
2831
    qemu_put_be32s(f, &s->latch);
2832
    qemu_put_8s(f, &s->sr_index);
2833
    qemu_put_buffer(f, s->sr, 256);
2834
    qemu_put_8s(f, &s->gr_index);
2835
    qemu_put_8s(f, &s->cirrus_shadow_gr0);
2836
    qemu_put_8s(f, &s->cirrus_shadow_gr1);
2837
    qemu_put_buffer(f, s->gr + 2, 254);
2838
    qemu_put_8s(f, &s->ar_index);
2839
    qemu_put_buffer(f, s->ar, 21);
2840
    qemu_put_be32s(f, &s->ar_flip_flop);
2841
    qemu_put_8s(f, &s->cr_index);
2842
    qemu_put_buffer(f, s->cr, 256);
2843
    qemu_put_8s(f, &s->msr);
2844
    qemu_put_8s(f, &s->fcr);
2845
    qemu_put_8s(f, &s->st00);
2846
    qemu_put_8s(f, &s->st01);
2847

    
2848
    qemu_put_8s(f, &s->dac_state);
2849
    qemu_put_8s(f, &s->dac_sub_index);
2850
    qemu_put_8s(f, &s->dac_read_index);
2851
    qemu_put_8s(f, &s->dac_write_index);
2852
    qemu_put_buffer(f, s->dac_cache, 3);
2853
    qemu_put_buffer(f, s->palette, 768);
2854

    
2855
    qemu_put_be32s(f, &s->bank_offset);
2856

    
2857
    qemu_put_8s(f, &s->cirrus_hidden_dac_lockindex);
2858
    qemu_put_8s(f, &s->cirrus_hidden_dac_data);
2859

    
2860
    qemu_put_be32s(f, &s->hw_cursor_x);
2861
    qemu_put_be32s(f, &s->hw_cursor_y);
2862
    /* XXX: we do not save the bitblt state - we assume we do not save
2863
       the state when the blitter is active */
2864
}
2865

    
2866
static int cirrus_vga_load(QEMUFile *f, void *opaque, int version_id)
2867
{
2868
    CirrusVGAState *s = opaque;
2869

    
2870
    if (version_id != 1)
2871
        return -EINVAL;
2872

    
2873
    qemu_get_be32s(f, &s->latch);
2874
    qemu_get_8s(f, &s->sr_index);
2875
    qemu_get_buffer(f, s->sr, 256);
2876
    qemu_get_8s(f, &s->gr_index);
2877
    qemu_get_8s(f, &s->cirrus_shadow_gr0);
2878
    qemu_get_8s(f, &s->cirrus_shadow_gr1);
2879
    s->gr[0x00] = s->cirrus_shadow_gr0 & 0x0f;
2880
    s->gr[0x01] = s->cirrus_shadow_gr1 & 0x0f;
2881
    qemu_get_buffer(f, s->gr + 2, 254);
2882
    qemu_get_8s(f, &s->ar_index);
2883
    qemu_get_buffer(f, s->ar, 21);
2884
    qemu_get_be32s(f, &s->ar_flip_flop);
2885
    qemu_get_8s(f, &s->cr_index);
2886
    qemu_get_buffer(f, s->cr, 256);
2887
    qemu_get_8s(f, &s->msr);
2888
    qemu_get_8s(f, &s->fcr);
2889
    qemu_get_8s(f, &s->st00);
2890
    qemu_get_8s(f, &s->st01);
2891

    
2892
    qemu_get_8s(f, &s->dac_state);
2893
    qemu_get_8s(f, &s->dac_sub_index);
2894
    qemu_get_8s(f, &s->dac_read_index);
2895
    qemu_get_8s(f, &s->dac_write_index);
2896
    qemu_get_buffer(f, s->dac_cache, 3);
2897
    qemu_get_buffer(f, s->palette, 768);
2898

    
2899
    qemu_get_be32s(f, &s->bank_offset);
2900

    
2901
    qemu_get_8s(f, &s->cirrus_hidden_dac_lockindex);
2902
    qemu_get_8s(f, &s->cirrus_hidden_dac_data);
2903

    
2904
    qemu_get_be32s(f, &s->hw_cursor_x);
2905
    qemu_get_be32s(f, &s->hw_cursor_y);
2906

    
2907
    /* force refresh */
2908
    s->graphic_mode = -1;
2909
    cirrus_update_bank_ptr(s, 0);
2910
    cirrus_update_bank_ptr(s, 1);
2911
    return 0;
2912
}
2913

    
2914
/***************************************
2915
 *
2916
 *  initialize
2917
 *
2918
 ***************************************/
2919

    
2920
static void cirrus_init_common(CirrusVGAState * s, int device_id, int is_pci)
2921
{
2922
    int vga_io_memory, i;
2923
    static int inited;
2924

    
2925
    if (!inited) {
2926
        inited = 1;
2927
        for(i = 0;i < 256; i++)
2928
            rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */
2929
        rop_to_index[CIRRUS_ROP_0] = 0;
2930
        rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;
2931
        rop_to_index[CIRRUS_ROP_NOP] = 2;
2932
        rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;
2933
        rop_to_index[CIRRUS_ROP_NOTDST] = 4;
2934
        rop_to_index[CIRRUS_ROP_SRC] = 5;
2935
        rop_to_index[CIRRUS_ROP_1] = 6;
2936
        rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;
2937
        rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;
2938
        rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;
2939
        rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;
2940
        rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;
2941
        rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;
2942
        rop_to_index[CIRRUS_ROP_NOTSRC] = 13;
2943
        rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;
2944
        rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;
2945
    }
2946

    
2947
    register_ioport_write(0x3c0, 16, 1, vga_ioport_write, s);
2948

    
2949
    register_ioport_write(0x3b4, 2, 1, vga_ioport_write, s);
2950
    register_ioport_write(0x3d4, 2, 1, vga_ioport_write, s);
2951
    register_ioport_write(0x3ba, 1, 1, vga_ioport_write, s);
2952
    register_ioport_write(0x3da, 1, 1, vga_ioport_write, s);
2953

    
2954
    register_ioport_read(0x3c0, 16, 1, vga_ioport_read, s);
2955

    
2956
    register_ioport_read(0x3b4, 2, 1, vga_ioport_read, s);
2957
    register_ioport_read(0x3d4, 2, 1, vga_ioport_read, s);
2958
    register_ioport_read(0x3ba, 1, 1, vga_ioport_read, s);
2959
    register_ioport_read(0x3da, 1, 1, vga_ioport_read, s);
2960

    
2961
    vga_io_memory = cpu_register_io_memory(0, cirrus_vga_mem_read, 
2962
                                           cirrus_vga_mem_write, s);
2963
    cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000, 
2964
                                 vga_io_memory);
2965

    
2966
    s->sr[0x06] = 0x0f;
2967
    if (device_id == CIRRUS_ID_CLGD5446) {
2968
        /* 4MB 64 bit memory config, always PCI */
2969
        s->sr[0x1F] = 0x2d;                // MemClock
2970
        s->gr[0x18] = 0x0f;             // fastest memory configuration
2971
#if 1
2972
        s->sr[0x0f] = 0x98;
2973
        s->sr[0x17] = 0x20;
2974
        s->sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */
2975
        s->real_vram_size = 4096 * 1024;
2976
#else
2977
        s->sr[0x0f] = 0x18;
2978
        s->sr[0x17] = 0x20;
2979
        s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
2980
        s->real_vram_size = 2048 * 1024;
2981
#endif
2982
    } else {
2983
        s->sr[0x1F] = 0x22;                // MemClock
2984
        s->sr[0x0F] = CIRRUS_MEMSIZE_2M;
2985
        if (is_pci) 
2986
            s->sr[0x17] = CIRRUS_BUSTYPE_PCI;
2987
        else
2988
            s->sr[0x17] = CIRRUS_BUSTYPE_ISA;
2989
        s->real_vram_size = 2048 * 1024;
2990
        s->sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
2991
    }
2992
    s->cr[0x27] = device_id;
2993

    
2994
    /* Win2K seems to assume that the pattern buffer is at 0xff
2995
       initially ! */
2996
    memset(s->vram_ptr, 0xff, s->real_vram_size);
2997

    
2998
    s->cirrus_hidden_dac_lockindex = 5;
2999
    s->cirrus_hidden_dac_data = 0;
3000

    
3001
    /* I/O handler for LFB */
3002
    s->cirrus_linear_io_addr =
3003
        cpu_register_io_memory(0, cirrus_linear_read, cirrus_linear_write,
3004
                               s);
3005
    s->cirrus_linear_write = cpu_get_io_memory_write(s->cirrus_linear_io_addr);
3006

    
3007
    /* I/O handler for LFB */
3008
    s->cirrus_linear_bitblt_io_addr =
3009
        cpu_register_io_memory(0, cirrus_linear_bitblt_read, cirrus_linear_bitblt_write,
3010
                               s);
3011

    
3012
    /* I/O handler for memory-mapped I/O */
3013
    s->cirrus_mmio_io_addr =
3014
        cpu_register_io_memory(0, cirrus_mmio_read, cirrus_mmio_write, s);
3015

    
3016
    /* XXX: s->vram_size must be a power of two */
3017
    s->cirrus_addr_mask = s->real_vram_size - 1;
3018
    s->linear_mmio_mask = s->real_vram_size - 256;
3019

    
3020
    s->get_bpp = cirrus_get_bpp;
3021
    s->get_offsets = cirrus_get_offsets;
3022
    s->get_resolution = cirrus_get_resolution;
3023
    s->cursor_invalidate = cirrus_cursor_invalidate;
3024
    s->cursor_draw_line = cirrus_cursor_draw_line;
3025

    
3026
    register_savevm("cirrus_vga", 0, 1, cirrus_vga_save, cirrus_vga_load, s);
3027
}
3028

    
3029
/***************************************
3030
 *
3031
 *  ISA bus support
3032
 *
3033
 ***************************************/
3034

    
3035
void isa_cirrus_vga_init(DisplayState *ds, uint8_t *vga_ram_base, 
3036
                         unsigned long vga_ram_offset, int vga_ram_size)
3037
{
3038
    CirrusVGAState *s;
3039

    
3040
    s = qemu_mallocz(sizeof(CirrusVGAState));
3041
    
3042
    vga_common_init((VGAState *)s, 
3043
                    ds, vga_ram_base, vga_ram_offset, vga_ram_size);
3044
    cirrus_init_common(s, CIRRUS_ID_CLGD5430, 0);
3045
    /* XXX ISA-LFB support */
3046
}
3047

    
3048
/***************************************
3049
 *
3050
 *  PCI bus support
3051
 *
3052
 ***************************************/
3053

    
3054
static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
3055
                               uint32_t addr, uint32_t size, int type)
3056
{
3057
    CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
3058

    
3059
    /* XXX: add byte swapping apertures */
3060
    cpu_register_physical_memory(addr, s->vram_size,
3061
                                 s->cirrus_linear_io_addr);
3062
    cpu_register_physical_memory(addr + 0x1000000, 0x400000,
3063
                                 s->cirrus_linear_bitblt_io_addr);
3064
}
3065

    
3066
static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
3067
                                uint32_t addr, uint32_t size, int type)
3068
{
3069
    CirrusVGAState *s = &((PCICirrusVGAState *)d)->cirrus_vga;
3070

    
3071
    cpu_register_physical_memory(addr, CIRRUS_PNPMMIO_SIZE,
3072
                                 s->cirrus_mmio_io_addr);
3073
}
3074

    
3075
void pci_cirrus_vga_init(PCIBus *bus, DisplayState *ds, uint8_t *vga_ram_base, 
3076
                         unsigned long vga_ram_offset, int vga_ram_size)
3077
{
3078
    PCICirrusVGAState *d;
3079
    uint8_t *pci_conf;
3080
    CirrusVGAState *s;
3081
    int device_id;
3082
    
3083
    device_id = CIRRUS_ID_CLGD5446;
3084

    
3085
    /* setup PCI configuration registers */
3086
    d = (PCICirrusVGAState *)pci_register_device(bus, "Cirrus VGA", 
3087
                                                 sizeof(PCICirrusVGAState), 
3088
                                                 -1, NULL, NULL);
3089
    pci_conf = d->dev.config;
3090
    pci_conf[0x00] = (uint8_t) (PCI_VENDOR_CIRRUS & 0xff);
3091
    pci_conf[0x01] = (uint8_t) (PCI_VENDOR_CIRRUS >> 8);
3092
    pci_conf[0x02] = (uint8_t) (device_id & 0xff);
3093
    pci_conf[0x03] = (uint8_t) (device_id >> 8);
3094
    pci_conf[0x04] = PCI_COMMAND_IOACCESS | PCI_COMMAND_MEMACCESS;
3095
    pci_conf[0x0a] = PCI_CLASS_SUB_VGA;
3096
    pci_conf[0x0b] = PCI_CLASS_BASE_DISPLAY;
3097
    pci_conf[0x0e] = PCI_CLASS_HEADERTYPE_00h;
3098

    
3099
    /* setup VGA */
3100
    s = &d->cirrus_vga;
3101
    vga_common_init((VGAState *)s, 
3102
                    ds, vga_ram_base, vga_ram_offset, vga_ram_size);
3103
    cirrus_init_common(s, device_id, 1);
3104

    
3105
    /* setup memory space */
3106
    /* memory #0 LFB */
3107
    /* memory #1 memory-mapped I/O */
3108
    /* XXX: s->vram_size must be a power of two */
3109
    pci_register_io_region((PCIDevice *)d, 0, 0x2000000,
3110
                           PCI_ADDRESS_SPACE_MEM_PREFETCH, cirrus_pci_lfb_map);
3111
    if (device_id == CIRRUS_ID_CLGD5446) {
3112
        pci_register_io_region((PCIDevice *)d, 1, CIRRUS_PNPMMIO_SIZE,
3113
                               PCI_ADDRESS_SPACE_MEM, cirrus_pci_mmio_map);
3114
    }
3115
    /* XXX: ROM BIOS */
3116
}