Statistics
| Branch: | Revision:

root / hw / cirrus_vga.c @ c1ded3dc

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