Statistics
| Branch: | Revision:

root / hw / cirrus_vga.c @ dbdd2506

History | View | Annotate | Download (95.9 kB)

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