Statistics
| Branch: | Revision:

root / hw / tcx.c @ b3c7724c

History | View | Annotate | Download (17.5 kB)

1 420557e8 bellard
/*
2 6f7e9aec bellard
 * QEMU TCX Frame buffer
3 5fafdf24 ths
 *
4 6f7e9aec bellard
 * Copyright (c) 2003-2005 Fabrice Bellard
5 5fafdf24 ths
 *
6 420557e8 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 420557e8 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 420557e8 bellard
 * in the Software without restriction, including without limitation the rights
9 420557e8 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 420557e8 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 420557e8 bellard
 * furnished to do so, subject to the following conditions:
12 420557e8 bellard
 *
13 420557e8 bellard
 * The above copyright notice and this permission notice shall be included in
14 420557e8 bellard
 * all copies or substantial portions of the Software.
15 420557e8 bellard
 *
16 420557e8 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 420557e8 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 420557e8 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 420557e8 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 420557e8 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 420557e8 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 420557e8 bellard
 * THE SOFTWARE.
23 420557e8 bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "sun4m.h"
26 87ecb68b pbrook
#include "console.h"
27 94470844 blueswir1
#include "pixel_ops.h"
28 420557e8 bellard
29 420557e8 bellard
#define MAXX 1024
30 420557e8 bellard
#define MAXY 768
31 6f7e9aec bellard
#define TCX_DAC_NREGS 16
32 8508b89e blueswir1
#define TCX_THC_NREGS_8  0x081c
33 8508b89e blueswir1
#define TCX_THC_NREGS_24 0x1000
34 8508b89e blueswir1
#define TCX_TEC_NREGS    0x1000
35 420557e8 bellard
36 420557e8 bellard
typedef struct TCXState {
37 5dcb6b91 blueswir1
    target_phys_addr_t addr;
38 420557e8 bellard
    DisplayState *ds;
39 8d5f07fa bellard
    uint8_t *vram;
40 eee0b836 blueswir1
    uint32_t *vram24, *cplane;
41 eee0b836 blueswir1
    ram_addr_t vram_offset, vram24_offset, cplane_offset;
42 eee0b836 blueswir1
    uint16_t width, height, depth;
43 e80cfcfc bellard
    uint8_t r[256], g[256], b[256];
44 21206a10 bellard
    uint32_t palette[256];
45 6f7e9aec bellard
    uint8_t dac_index, dac_state;
46 420557e8 bellard
} TCXState;
47 420557e8 bellard
48 95219897 pbrook
static void tcx_screen_dump(void *opaque, const char *filename);
49 eee0b836 blueswir1
static void tcx24_screen_dump(void *opaque, const char *filename);
50 97e7df27 blueswir1
static void tcx_invalidate_display(void *opaque);
51 97e7df27 blueswir1
static void tcx24_invalidate_display(void *opaque);
52 95219897 pbrook
53 21206a10 bellard
static void update_palette_entries(TCXState *s, int start, int end)
54 21206a10 bellard
{
55 21206a10 bellard
    int i;
56 21206a10 bellard
    for(i = start; i < end; i++) {
57 21206a10 bellard
        switch(s->ds->depth) {
58 21206a10 bellard
        default:
59 21206a10 bellard
        case 8:
60 21206a10 bellard
            s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
61 21206a10 bellard
            break;
62 21206a10 bellard
        case 15:
63 b29169d2 blueswir1
            if (s->ds->bgr)
64 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel15bgr(s->r[i], s->g[i], s->b[i]);
65 b29169d2 blueswir1
            else
66 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
67 21206a10 bellard
            break;
68 21206a10 bellard
        case 16:
69 b29169d2 blueswir1
            if (s->ds->bgr)
70 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel16bgr(s->r[i], s->g[i], s->b[i]);
71 b29169d2 blueswir1
            else
72 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
73 21206a10 bellard
            break;
74 21206a10 bellard
        case 32:
75 b29169d2 blueswir1
            if (s->ds->bgr)
76 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
77 b29169d2 blueswir1
            else
78 b29169d2 blueswir1
                s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
79 21206a10 bellard
            break;
80 21206a10 bellard
        }
81 21206a10 bellard
    }
82 97e7df27 blueswir1
    if (s->depth == 24)
83 97e7df27 blueswir1
        tcx24_invalidate_display(s);
84 97e7df27 blueswir1
    else
85 97e7df27 blueswir1
        tcx_invalidate_display(s);
86 21206a10 bellard
}
87 21206a10 bellard
88 5fafdf24 ths
static void tcx_draw_line32(TCXState *s1, uint8_t *d,
89 f930d07e blueswir1
                            const uint8_t *s, int width)
90 420557e8 bellard
{
91 e80cfcfc bellard
    int x;
92 e80cfcfc bellard
    uint8_t val;
93 8bdc2159 ths
    uint32_t *p = (uint32_t *)d;
94 e80cfcfc bellard
95 e80cfcfc bellard
    for(x = 0; x < width; x++) {
96 f930d07e blueswir1
        val = *s++;
97 8bdc2159 ths
        *p++ = s1->palette[val];
98 e80cfcfc bellard
    }
99 420557e8 bellard
}
100 420557e8 bellard
101 5fafdf24 ths
static void tcx_draw_line16(TCXState *s1, uint8_t *d,
102 f930d07e blueswir1
                            const uint8_t *s, int width)
103 e80cfcfc bellard
{
104 e80cfcfc bellard
    int x;
105 e80cfcfc bellard
    uint8_t val;
106 8bdc2159 ths
    uint16_t *p = (uint16_t *)d;
107 8d5f07fa bellard
108 e80cfcfc bellard
    for(x = 0; x < width; x++) {
109 f930d07e blueswir1
        val = *s++;
110 8bdc2159 ths
        *p++ = s1->palette[val];
111 e80cfcfc bellard
    }
112 e80cfcfc bellard
}
113 e80cfcfc bellard
114 5fafdf24 ths
static void tcx_draw_line8(TCXState *s1, uint8_t *d,
115 f930d07e blueswir1
                           const uint8_t *s, int width)
116 420557e8 bellard
{
117 e80cfcfc bellard
    int x;
118 e80cfcfc bellard
    uint8_t val;
119 e80cfcfc bellard
120 e80cfcfc bellard
    for(x = 0; x < width; x++) {
121 f930d07e blueswir1
        val = *s++;
122 21206a10 bellard
        *d++ = s1->palette[val];
123 420557e8 bellard
    }
124 420557e8 bellard
}
125 420557e8 bellard
126 eee0b836 blueswir1
static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
127 eee0b836 blueswir1
                                     const uint8_t *s, int width,
128 eee0b836 blueswir1
                                     const uint32_t *cplane,
129 eee0b836 blueswir1
                                     const uint32_t *s24)
130 eee0b836 blueswir1
{
131 eee0b836 blueswir1
    int x;
132 eee0b836 blueswir1
    uint8_t val;
133 eee0b836 blueswir1
    uint32_t *p = (uint32_t *)d;
134 eee0b836 blueswir1
    uint32_t dval;
135 eee0b836 blueswir1
136 eee0b836 blueswir1
    for(x = 0; x < width; x++, s++, s24++) {
137 eee0b836 blueswir1
        if ((bswap32(*cplane++) & 0xff000000) == 0x03000000) { // 24-bit direct
138 eee0b836 blueswir1
            dval = bswap32(*s24) & 0x00ffffff;
139 eee0b836 blueswir1
        } else {
140 eee0b836 blueswir1
            val = *s;
141 eee0b836 blueswir1
            dval = s1->palette[val];
142 eee0b836 blueswir1
        }
143 eee0b836 blueswir1
        *p++ = dval;
144 eee0b836 blueswir1
    }
145 eee0b836 blueswir1
}
146 eee0b836 blueswir1
147 22548760 blueswir1
static inline int check_dirty(ram_addr_t page, ram_addr_t page24,
148 eee0b836 blueswir1
                              ram_addr_t cpage)
149 eee0b836 blueswir1
{
150 eee0b836 blueswir1
    int ret;
151 eee0b836 blueswir1
    unsigned int off;
152 eee0b836 blueswir1
153 eee0b836 blueswir1
    ret = cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG);
154 eee0b836 blueswir1
    for (off = 0; off < TARGET_PAGE_SIZE * 4; off += TARGET_PAGE_SIZE) {
155 eee0b836 blueswir1
        ret |= cpu_physical_memory_get_dirty(page24 + off, VGA_DIRTY_FLAG);
156 eee0b836 blueswir1
        ret |= cpu_physical_memory_get_dirty(cpage + off, VGA_DIRTY_FLAG);
157 eee0b836 blueswir1
    }
158 eee0b836 blueswir1
    return ret;
159 eee0b836 blueswir1
}
160 eee0b836 blueswir1
161 eee0b836 blueswir1
static inline void reset_dirty(TCXState *ts, ram_addr_t page_min,
162 eee0b836 blueswir1
                               ram_addr_t page_max, ram_addr_t page24,
163 eee0b836 blueswir1
                              ram_addr_t cpage)
164 eee0b836 blueswir1
{
165 eee0b836 blueswir1
    cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
166 eee0b836 blueswir1
                                    VGA_DIRTY_FLAG);
167 eee0b836 blueswir1
    page_min -= ts->vram_offset;
168 eee0b836 blueswir1
    page_max -= ts->vram_offset;
169 eee0b836 blueswir1
    cpu_physical_memory_reset_dirty(page24 + page_min * 4,
170 eee0b836 blueswir1
                                    page24 + page_max * 4 + TARGET_PAGE_SIZE,
171 eee0b836 blueswir1
                                    VGA_DIRTY_FLAG);
172 eee0b836 blueswir1
    cpu_physical_memory_reset_dirty(cpage + page_min * 4,
173 eee0b836 blueswir1
                                    cpage + page_max * 4 + TARGET_PAGE_SIZE,
174 eee0b836 blueswir1
                                    VGA_DIRTY_FLAG);
175 eee0b836 blueswir1
}
176 eee0b836 blueswir1
177 e80cfcfc bellard
/* Fixed line length 1024 allows us to do nice tricks not possible on
178 e80cfcfc bellard
   VGA... */
179 95219897 pbrook
static void tcx_update_display(void *opaque)
180 420557e8 bellard
{
181 e80cfcfc bellard
    TCXState *ts = opaque;
182 550be127 bellard
    ram_addr_t page, page_min, page_max;
183 550be127 bellard
    int y, y_start, dd, ds;
184 e80cfcfc bellard
    uint8_t *d, *s;
185 b3ceef24 blueswir1
    void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
186 e80cfcfc bellard
187 e80cfcfc bellard
    if (ts->ds->depth == 0)
188 f930d07e blueswir1
        return;
189 33b6939f blueswir1
    if (ts->ds->width != ts->width || ts->ds->height != ts->height)
190 33b6939f blueswir1
        dpy_resize(ts->ds, ts->width, ts->height);
191 6f7e9aec bellard
    page = ts->vram_offset;
192 e80cfcfc bellard
    y_start = -1;
193 550be127 bellard
    page_min = 0xffffffff;
194 550be127 bellard
    page_max = 0;
195 e80cfcfc bellard
    d = ts->ds->data;
196 6f7e9aec bellard
    s = ts->vram;
197 e80cfcfc bellard
    dd = ts->ds->linesize;
198 e80cfcfc bellard
    ds = 1024;
199 e80cfcfc bellard
200 e80cfcfc bellard
    switch (ts->ds->depth) {
201 e80cfcfc bellard
    case 32:
202 f930d07e blueswir1
        f = tcx_draw_line32;
203 f930d07e blueswir1
        break;
204 21206a10 bellard
    case 15:
205 21206a10 bellard
    case 16:
206 f930d07e blueswir1
        f = tcx_draw_line16;
207 f930d07e blueswir1
        break;
208 e80cfcfc bellard
    default:
209 e80cfcfc bellard
    case 8:
210 f930d07e blueswir1
        f = tcx_draw_line8;
211 f930d07e blueswir1
        break;
212 e80cfcfc bellard
    case 0:
213 f930d07e blueswir1
        return;
214 e80cfcfc bellard
    }
215 3b46e624 ths
216 6f7e9aec bellard
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
217 f930d07e blueswir1
        if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
218 f930d07e blueswir1
            if (y_start < 0)
219 e80cfcfc bellard
                y_start = y;
220 e80cfcfc bellard
            if (page < page_min)
221 e80cfcfc bellard
                page_min = page;
222 e80cfcfc bellard
            if (page > page_max)
223 e80cfcfc bellard
                page_max = page;
224 f930d07e blueswir1
            f(ts, d, s, ts->width);
225 f930d07e blueswir1
            d += dd;
226 f930d07e blueswir1
            s += ds;
227 f930d07e blueswir1
            f(ts, d, s, ts->width);
228 f930d07e blueswir1
            d += dd;
229 f930d07e blueswir1
            s += ds;
230 f930d07e blueswir1
            f(ts, d, s, ts->width);
231 f930d07e blueswir1
            d += dd;
232 f930d07e blueswir1
            s += ds;
233 f930d07e blueswir1
            f(ts, d, s, ts->width);
234 f930d07e blueswir1
            d += dd;
235 f930d07e blueswir1
            s += ds;
236 f930d07e blueswir1
        } else {
237 e80cfcfc bellard
            if (y_start >= 0) {
238 e80cfcfc bellard
                /* flush to display */
239 5fafdf24 ths
                dpy_update(ts->ds, 0, y_start,
240 6f7e9aec bellard
                           ts->width, y - y_start);
241 e80cfcfc bellard
                y_start = -1;
242 e80cfcfc bellard
            }
243 f930d07e blueswir1
            d += dd * 4;
244 f930d07e blueswir1
            s += ds * 4;
245 f930d07e blueswir1
        }
246 e80cfcfc bellard
    }
247 e80cfcfc bellard
    if (y_start >= 0) {
248 f930d07e blueswir1
        /* flush to display */
249 f930d07e blueswir1
        dpy_update(ts->ds, 0, y_start,
250 f930d07e blueswir1
                   ts->width, y - y_start);
251 e80cfcfc bellard
    }
252 e80cfcfc bellard
    /* reset modified pages */
253 550be127 bellard
    if (page_min <= page_max) {
254 0a962c02 bellard
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
255 0a962c02 bellard
                                        VGA_DIRTY_FLAG);
256 e80cfcfc bellard
    }
257 420557e8 bellard
}
258 420557e8 bellard
259 eee0b836 blueswir1
static void tcx24_update_display(void *opaque)
260 eee0b836 blueswir1
{
261 eee0b836 blueswir1
    TCXState *ts = opaque;
262 eee0b836 blueswir1
    ram_addr_t page, page_min, page_max, cpage, page24;
263 eee0b836 blueswir1
    int y, y_start, dd, ds;
264 eee0b836 blueswir1
    uint8_t *d, *s;
265 eee0b836 blueswir1
    uint32_t *cptr, *s24;
266 eee0b836 blueswir1
267 eee0b836 blueswir1
    if (ts->ds->depth != 32)
268 eee0b836 blueswir1
            return;
269 20483400 blueswir1
    if (ts->ds->width != ts->width || ts->ds->height != ts->height)
270 20483400 blueswir1
        dpy_resize(ts->ds, ts->width, ts->height);
271 eee0b836 blueswir1
    page = ts->vram_offset;
272 eee0b836 blueswir1
    page24 = ts->vram24_offset;
273 eee0b836 blueswir1
    cpage = ts->cplane_offset;
274 eee0b836 blueswir1
    y_start = -1;
275 eee0b836 blueswir1
    page_min = 0xffffffff;
276 eee0b836 blueswir1
    page_max = 0;
277 eee0b836 blueswir1
    d = ts->ds->data;
278 eee0b836 blueswir1
    s = ts->vram;
279 eee0b836 blueswir1
    s24 = ts->vram24;
280 eee0b836 blueswir1
    cptr = ts->cplane;
281 eee0b836 blueswir1
    dd = ts->ds->linesize;
282 eee0b836 blueswir1
    ds = 1024;
283 eee0b836 blueswir1
284 eee0b836 blueswir1
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
285 eee0b836 blueswir1
            page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
286 22548760 blueswir1
        if (check_dirty(page, page24, cpage)) {
287 eee0b836 blueswir1
            if (y_start < 0)
288 eee0b836 blueswir1
                y_start = y;
289 eee0b836 blueswir1
            if (page < page_min)
290 eee0b836 blueswir1
                page_min = page;
291 eee0b836 blueswir1
            if (page > page_max)
292 eee0b836 blueswir1
                page_max = page;
293 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
294 eee0b836 blueswir1
            d += dd;
295 eee0b836 blueswir1
            s += ds;
296 eee0b836 blueswir1
            cptr += ds;
297 eee0b836 blueswir1
            s24 += ds;
298 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
299 eee0b836 blueswir1
            d += dd;
300 eee0b836 blueswir1
            s += ds;
301 eee0b836 blueswir1
            cptr += ds;
302 eee0b836 blueswir1
            s24 += ds;
303 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
304 eee0b836 blueswir1
            d += dd;
305 eee0b836 blueswir1
            s += ds;
306 eee0b836 blueswir1
            cptr += ds;
307 eee0b836 blueswir1
            s24 += ds;
308 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
309 eee0b836 blueswir1
            d += dd;
310 eee0b836 blueswir1
            s += ds;
311 eee0b836 blueswir1
            cptr += ds;
312 eee0b836 blueswir1
            s24 += ds;
313 eee0b836 blueswir1
        } else {
314 eee0b836 blueswir1
            if (y_start >= 0) {
315 eee0b836 blueswir1
                /* flush to display */
316 eee0b836 blueswir1
                dpy_update(ts->ds, 0, y_start,
317 eee0b836 blueswir1
                           ts->width, y - y_start);
318 eee0b836 blueswir1
                y_start = -1;
319 eee0b836 blueswir1
            }
320 eee0b836 blueswir1
            d += dd * 4;
321 eee0b836 blueswir1
            s += ds * 4;
322 eee0b836 blueswir1
            cptr += ds * 4;
323 eee0b836 blueswir1
            s24 += ds * 4;
324 eee0b836 blueswir1
        }
325 eee0b836 blueswir1
    }
326 eee0b836 blueswir1
    if (y_start >= 0) {
327 eee0b836 blueswir1
        /* flush to display */
328 eee0b836 blueswir1
        dpy_update(ts->ds, 0, y_start,
329 eee0b836 blueswir1
                   ts->width, y - y_start);
330 eee0b836 blueswir1
    }
331 eee0b836 blueswir1
    /* reset modified pages */
332 eee0b836 blueswir1
    if (page_min <= page_max) {
333 eee0b836 blueswir1
        reset_dirty(ts, page_min, page_max, page24, cpage);
334 eee0b836 blueswir1
    }
335 eee0b836 blueswir1
}
336 eee0b836 blueswir1
337 95219897 pbrook
static void tcx_invalidate_display(void *opaque)
338 420557e8 bellard
{
339 e80cfcfc bellard
    TCXState *s = opaque;
340 e80cfcfc bellard
    int i;
341 e80cfcfc bellard
342 e80cfcfc bellard
    for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
343 f930d07e blueswir1
        cpu_physical_memory_set_dirty(s->vram_offset + i);
344 e80cfcfc bellard
    }
345 420557e8 bellard
}
346 420557e8 bellard
347 eee0b836 blueswir1
static void tcx24_invalidate_display(void *opaque)
348 eee0b836 blueswir1
{
349 eee0b836 blueswir1
    TCXState *s = opaque;
350 eee0b836 blueswir1
    int i;
351 eee0b836 blueswir1
352 eee0b836 blueswir1
    tcx_invalidate_display(s);
353 eee0b836 blueswir1
    for (i = 0; i < MAXX*MAXY * 4; i += TARGET_PAGE_SIZE) {
354 eee0b836 blueswir1
        cpu_physical_memory_set_dirty(s->vram24_offset + i);
355 eee0b836 blueswir1
        cpu_physical_memory_set_dirty(s->cplane_offset + i);
356 eee0b836 blueswir1
    }
357 eee0b836 blueswir1
}
358 eee0b836 blueswir1
359 e80cfcfc bellard
static void tcx_save(QEMUFile *f, void *opaque)
360 420557e8 bellard
{
361 420557e8 bellard
    TCXState *s = opaque;
362 3b46e624 ths
363 6f7e9aec bellard
    qemu_put_be16s(f, (uint16_t *)&s->height);
364 6f7e9aec bellard
    qemu_put_be16s(f, (uint16_t *)&s->width);
365 eee0b836 blueswir1
    qemu_put_be16s(f, (uint16_t *)&s->depth);
366 e80cfcfc bellard
    qemu_put_buffer(f, s->r, 256);
367 e80cfcfc bellard
    qemu_put_buffer(f, s->g, 256);
368 e80cfcfc bellard
    qemu_put_buffer(f, s->b, 256);
369 6f7e9aec bellard
    qemu_put_8s(f, &s->dac_index);
370 6f7e9aec bellard
    qemu_put_8s(f, &s->dac_state);
371 420557e8 bellard
}
372 420557e8 bellard
373 e80cfcfc bellard
static int tcx_load(QEMUFile *f, void *opaque, int version_id)
374 420557e8 bellard
{
375 e80cfcfc bellard
    TCXState *s = opaque;
376 fda77c2d blueswir1
    uint32_t dummy;
377 fda77c2d blueswir1
378 fda77c2d blueswir1
    if (version_id != 3 && version_id != 4)
379 e80cfcfc bellard
        return -EINVAL;
380 e80cfcfc bellard
381 fda77c2d blueswir1
    if (version_id == 3) {
382 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
383 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
384 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
385 fda77c2d blueswir1
    }
386 6f7e9aec bellard
    qemu_get_be16s(f, (uint16_t *)&s->height);
387 6f7e9aec bellard
    qemu_get_be16s(f, (uint16_t *)&s->width);
388 eee0b836 blueswir1
    qemu_get_be16s(f, (uint16_t *)&s->depth);
389 e80cfcfc bellard
    qemu_get_buffer(f, s->r, 256);
390 e80cfcfc bellard
    qemu_get_buffer(f, s->g, 256);
391 e80cfcfc bellard
    qemu_get_buffer(f, s->b, 256);
392 6f7e9aec bellard
    qemu_get_8s(f, &s->dac_index);
393 6f7e9aec bellard
    qemu_get_8s(f, &s->dac_state);
394 21206a10 bellard
    update_palette_entries(s, 0, 256);
395 97e7df27 blueswir1
    if (s->depth == 24)
396 97e7df27 blueswir1
        tcx24_invalidate_display(s);
397 97e7df27 blueswir1
    else
398 97e7df27 blueswir1
        tcx_invalidate_display(s);
399 5425a216 blueswir1
400 e80cfcfc bellard
    return 0;
401 420557e8 bellard
}
402 420557e8 bellard
403 e80cfcfc bellard
static void tcx_reset(void *opaque)
404 420557e8 bellard
{
405 e80cfcfc bellard
    TCXState *s = opaque;
406 e80cfcfc bellard
407 e80cfcfc bellard
    /* Initialize palette */
408 e80cfcfc bellard
    memset(s->r, 0, 256);
409 e80cfcfc bellard
    memset(s->g, 0, 256);
410 e80cfcfc bellard
    memset(s->b, 0, 256);
411 e80cfcfc bellard
    s->r[255] = s->g[255] = s->b[255] = 255;
412 21206a10 bellard
    update_palette_entries(s, 0, 256);
413 e80cfcfc bellard
    memset(s->vram, 0, MAXX*MAXY);
414 eee0b836 blueswir1
    cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset +
415 eee0b836 blueswir1
                                    MAXX * MAXY * (1 + 4 + 4), VGA_DIRTY_FLAG);
416 6f7e9aec bellard
    s->dac_index = 0;
417 6f7e9aec bellard
    s->dac_state = 0;
418 6f7e9aec bellard
}
419 6f7e9aec bellard
420 6f7e9aec bellard
static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
421 6f7e9aec bellard
{
422 6f7e9aec bellard
    return 0;
423 6f7e9aec bellard
}
424 6f7e9aec bellard
425 6f7e9aec bellard
static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
426 6f7e9aec bellard
{
427 6f7e9aec bellard
    TCXState *s = opaque;
428 6f7e9aec bellard
    uint32_t saddr;
429 6f7e9aec bellard
430 6f7e9aec bellard
    saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
431 6f7e9aec bellard
    switch (saddr) {
432 6f7e9aec bellard
    case 0:
433 f930d07e blueswir1
        s->dac_index = val >> 24;
434 f930d07e blueswir1
        s->dac_state = 0;
435 f930d07e blueswir1
        break;
436 6f7e9aec bellard
    case 1:
437 f930d07e blueswir1
        switch (s->dac_state) {
438 f930d07e blueswir1
        case 0:
439 f930d07e blueswir1
            s->r[s->dac_index] = val >> 24;
440 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
441 f930d07e blueswir1
            s->dac_state++;
442 f930d07e blueswir1
            break;
443 f930d07e blueswir1
        case 1:
444 f930d07e blueswir1
            s->g[s->dac_index] = val >> 24;
445 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
446 f930d07e blueswir1
            s->dac_state++;
447 f930d07e blueswir1
            break;
448 f930d07e blueswir1
        case 2:
449 f930d07e blueswir1
            s->b[s->dac_index] = val >> 24;
450 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
451 5c8cdbf8 blueswir1
            s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
452 f930d07e blueswir1
        default:
453 f930d07e blueswir1
            s->dac_state = 0;
454 f930d07e blueswir1
            break;
455 f930d07e blueswir1
        }
456 f930d07e blueswir1
        break;
457 6f7e9aec bellard
    default:
458 f930d07e blueswir1
        break;
459 6f7e9aec bellard
    }
460 6f7e9aec bellard
    return;
461 420557e8 bellard
}
462 420557e8 bellard
463 6f7e9aec bellard
static CPUReadMemoryFunc *tcx_dac_read[3] = {
464 7c560456 blueswir1
    NULL,
465 7c560456 blueswir1
    NULL,
466 6f7e9aec bellard
    tcx_dac_readl,
467 6f7e9aec bellard
};
468 6f7e9aec bellard
469 6f7e9aec bellard
static CPUWriteMemoryFunc *tcx_dac_write[3] = {
470 7c560456 blueswir1
    NULL,
471 7c560456 blueswir1
    NULL,
472 6f7e9aec bellard
    tcx_dac_writel,
473 6f7e9aec bellard
};
474 6f7e9aec bellard
475 8508b89e blueswir1
static uint32_t tcx_dummy_readl(void *opaque, target_phys_addr_t addr)
476 8508b89e blueswir1
{
477 8508b89e blueswir1
    return 0;
478 8508b89e blueswir1
}
479 8508b89e blueswir1
480 8508b89e blueswir1
static void tcx_dummy_writel(void *opaque, target_phys_addr_t addr,
481 8508b89e blueswir1
                             uint32_t val)
482 8508b89e blueswir1
{
483 8508b89e blueswir1
}
484 8508b89e blueswir1
485 8508b89e blueswir1
static CPUReadMemoryFunc *tcx_dummy_read[3] = {
486 7c560456 blueswir1
    NULL,
487 7c560456 blueswir1
    NULL,
488 8508b89e blueswir1
    tcx_dummy_readl,
489 8508b89e blueswir1
};
490 8508b89e blueswir1
491 8508b89e blueswir1
static CPUWriteMemoryFunc *tcx_dummy_write[3] = {
492 7c560456 blueswir1
    NULL,
493 7c560456 blueswir1
    NULL,
494 8508b89e blueswir1
    tcx_dummy_writel,
495 8508b89e blueswir1
};
496 8508b89e blueswir1
497 5dcb6b91 blueswir1
void tcx_init(DisplayState *ds, target_phys_addr_t addr, uint8_t *vram_base,
498 eee0b836 blueswir1
              unsigned long vram_offset, int vram_size, int width, int height,
499 eee0b836 blueswir1
              int depth)
500 420557e8 bellard
{
501 420557e8 bellard
    TCXState *s;
502 8508b89e blueswir1
    int io_memory, dummy_memory;
503 eee0b836 blueswir1
    int size;
504 420557e8 bellard
505 420557e8 bellard
    s = qemu_mallocz(sizeof(TCXState));
506 420557e8 bellard
    if (!s)
507 95219897 pbrook
        return;
508 420557e8 bellard
    s->ds = ds;
509 8d5f07fa bellard
    s->addr = addr;
510 e80cfcfc bellard
    s->vram_offset = vram_offset;
511 6f7e9aec bellard
    s->width = width;
512 6f7e9aec bellard
    s->height = height;
513 eee0b836 blueswir1
    s->depth = depth;
514 eee0b836 blueswir1
515 eee0b836 blueswir1
    // 8-bit plane
516 eee0b836 blueswir1
    s->vram = vram_base;
517 eee0b836 blueswir1
    size = vram_size;
518 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00800000ULL, size, vram_offset);
519 eee0b836 blueswir1
    vram_offset += size;
520 eee0b836 blueswir1
    vram_base += size;
521 e80cfcfc bellard
522 6f7e9aec bellard
    io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
523 77f193da blueswir1
    cpu_register_physical_memory(addr + 0x00200000ULL, TCX_DAC_NREGS,
524 77f193da blueswir1
                                 io_memory);
525 eee0b836 blueswir1
526 8508b89e blueswir1
    dummy_memory = cpu_register_io_memory(0, tcx_dummy_read, tcx_dummy_write,
527 8508b89e blueswir1
                                          s);
528 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00700000ULL, TCX_TEC_NREGS,
529 8508b89e blueswir1
                                 dummy_memory);
530 eee0b836 blueswir1
    if (depth == 24) {
531 eee0b836 blueswir1
        // 24-bit plane
532 eee0b836 blueswir1
        size = vram_size * 4;
533 eee0b836 blueswir1
        s->vram24 = (uint32_t *)vram_base;
534 eee0b836 blueswir1
        s->vram24_offset = vram_offset;
535 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x02000000ULL, size, vram_offset);
536 eee0b836 blueswir1
        vram_offset += size;
537 eee0b836 blueswir1
        vram_base += size;
538 eee0b836 blueswir1
539 eee0b836 blueswir1
        // Control plane
540 eee0b836 blueswir1
        size = vram_size * 4;
541 eee0b836 blueswir1
        s->cplane = (uint32_t *)vram_base;
542 eee0b836 blueswir1
        s->cplane_offset = vram_offset;
543 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x0a000000ULL, size, vram_offset);
544 8508b89e blueswir1
        graphic_console_init(s->ds, tcx24_update_display,
545 4d3b6f6e balrog
                             tcx24_invalidate_display,
546 4d3b6f6e balrog
                             tcx24_screen_dump, NULL, s);
547 eee0b836 blueswir1
    } else {
548 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x00300000ULL, TCX_THC_NREGS_8,
549 8508b89e blueswir1
                                     dummy_memory);
550 eee0b836 blueswir1
        graphic_console_init(s->ds, tcx_update_display, tcx_invalidate_display,
551 4d3b6f6e balrog
                             tcx_screen_dump, NULL, s);
552 eee0b836 blueswir1
    }
553 f96f4c9d blueswir1
    // NetBSD writes here even with 8-bit display
554 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00301000ULL, TCX_THC_NREGS_24,
555 f96f4c9d blueswir1
                                 dummy_memory);
556 e80cfcfc bellard
557 fda77c2d blueswir1
    register_savevm("tcx", addr, 4, tcx_save, tcx_load, s);
558 e80cfcfc bellard
    qemu_register_reset(tcx_reset, s);
559 e80cfcfc bellard
    tcx_reset(s);
560 6f7e9aec bellard
    dpy_resize(s->ds, width, height);
561 420557e8 bellard
}
562 420557e8 bellard
563 95219897 pbrook
static void tcx_screen_dump(void *opaque, const char *filename)
564 8d5f07fa bellard
{
565 e80cfcfc bellard
    TCXState *s = opaque;
566 8d5f07fa bellard
    FILE *f;
567 e80cfcfc bellard
    uint8_t *d, *d1, v;
568 8d5f07fa bellard
    int y, x;
569 8d5f07fa bellard
570 8d5f07fa bellard
    f = fopen(filename, "wb");
571 8d5f07fa bellard
    if (!f)
572 e80cfcfc bellard
        return;
573 6f7e9aec bellard
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
574 6f7e9aec bellard
    d1 = s->vram;
575 6f7e9aec bellard
    for(y = 0; y < s->height; y++) {
576 8d5f07fa bellard
        d = d1;
577 6f7e9aec bellard
        for(x = 0; x < s->width; x++) {
578 8d5f07fa bellard
            v = *d;
579 e80cfcfc bellard
            fputc(s->r[v], f);
580 e80cfcfc bellard
            fputc(s->g[v], f);
581 e80cfcfc bellard
            fputc(s->b[v], f);
582 8d5f07fa bellard
            d++;
583 8d5f07fa bellard
        }
584 e80cfcfc bellard
        d1 += MAXX;
585 8d5f07fa bellard
    }
586 8d5f07fa bellard
    fclose(f);
587 8d5f07fa bellard
    return;
588 8d5f07fa bellard
}
589 8d5f07fa bellard
590 eee0b836 blueswir1
static void tcx24_screen_dump(void *opaque, const char *filename)
591 eee0b836 blueswir1
{
592 eee0b836 blueswir1
    TCXState *s = opaque;
593 eee0b836 blueswir1
    FILE *f;
594 eee0b836 blueswir1
    uint8_t *d, *d1, v;
595 eee0b836 blueswir1
    uint32_t *s24, *cptr, dval;
596 eee0b836 blueswir1
    int y, x;
597 8d5f07fa bellard
598 eee0b836 blueswir1
    f = fopen(filename, "wb");
599 eee0b836 blueswir1
    if (!f)
600 eee0b836 blueswir1
        return;
601 eee0b836 blueswir1
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
602 eee0b836 blueswir1
    d1 = s->vram;
603 eee0b836 blueswir1
    s24 = s->vram24;
604 eee0b836 blueswir1
    cptr = s->cplane;
605 eee0b836 blueswir1
    for(y = 0; y < s->height; y++) {
606 eee0b836 blueswir1
        d = d1;
607 eee0b836 blueswir1
        for(x = 0; x < s->width; x++, d++, s24++) {
608 eee0b836 blueswir1
            if ((*cptr++ & 0xff000000) == 0x03000000) { // 24-bit direct
609 eee0b836 blueswir1
                dval = *s24 & 0x00ffffff;
610 eee0b836 blueswir1
                fputc((dval >> 16) & 0xff, f);
611 eee0b836 blueswir1
                fputc((dval >> 8) & 0xff, f);
612 eee0b836 blueswir1
                fputc(dval & 0xff, f);
613 eee0b836 blueswir1
            } else {
614 eee0b836 blueswir1
                v = *d;
615 eee0b836 blueswir1
                fputc(s->r[v], f);
616 eee0b836 blueswir1
                fputc(s->g[v], f);
617 eee0b836 blueswir1
                fputc(s->b[v], f);
618 eee0b836 blueswir1
            }
619 eee0b836 blueswir1
        }
620 eee0b836 blueswir1
        d1 += MAXX;
621 eee0b836 blueswir1
    }
622 eee0b836 blueswir1
    fclose(f);
623 eee0b836 blueswir1
    return;
624 eee0b836 blueswir1
}