Statistics
| Branch: | Revision:

root / hw / tcx.c @ a350e694

History | View | Annotate | Download (17.3 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 eee0b836 blueswir1
static inline int check_dirty(TCXState *ts, 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 6f7e9aec bellard
    page = ts->vram_offset;
190 e80cfcfc bellard
    y_start = -1;
191 550be127 bellard
    page_min = 0xffffffff;
192 550be127 bellard
    page_max = 0;
193 e80cfcfc bellard
    d = ts->ds->data;
194 6f7e9aec bellard
    s = ts->vram;
195 e80cfcfc bellard
    dd = ts->ds->linesize;
196 e80cfcfc bellard
    ds = 1024;
197 e80cfcfc bellard
198 e80cfcfc bellard
    switch (ts->ds->depth) {
199 e80cfcfc bellard
    case 32:
200 f930d07e blueswir1
        f = tcx_draw_line32;
201 f930d07e blueswir1
        break;
202 21206a10 bellard
    case 15:
203 21206a10 bellard
    case 16:
204 f930d07e blueswir1
        f = tcx_draw_line16;
205 f930d07e blueswir1
        break;
206 e80cfcfc bellard
    default:
207 e80cfcfc bellard
    case 8:
208 f930d07e blueswir1
        f = tcx_draw_line8;
209 f930d07e blueswir1
        break;
210 e80cfcfc bellard
    case 0:
211 f930d07e blueswir1
        return;
212 e80cfcfc bellard
    }
213 3b46e624 ths
214 6f7e9aec bellard
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
215 f930d07e blueswir1
        if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
216 f930d07e blueswir1
            if (y_start < 0)
217 e80cfcfc bellard
                y_start = y;
218 e80cfcfc bellard
            if (page < page_min)
219 e80cfcfc bellard
                page_min = page;
220 e80cfcfc bellard
            if (page > page_max)
221 e80cfcfc bellard
                page_max = page;
222 f930d07e blueswir1
            f(ts, d, s, ts->width);
223 f930d07e blueswir1
            d += dd;
224 f930d07e blueswir1
            s += ds;
225 f930d07e blueswir1
            f(ts, d, s, ts->width);
226 f930d07e blueswir1
            d += dd;
227 f930d07e blueswir1
            s += ds;
228 f930d07e blueswir1
            f(ts, d, s, ts->width);
229 f930d07e blueswir1
            d += dd;
230 f930d07e blueswir1
            s += ds;
231 f930d07e blueswir1
            f(ts, d, s, ts->width);
232 f930d07e blueswir1
            d += dd;
233 f930d07e blueswir1
            s += ds;
234 f930d07e blueswir1
        } else {
235 e80cfcfc bellard
            if (y_start >= 0) {
236 e80cfcfc bellard
                /* flush to display */
237 5fafdf24 ths
                dpy_update(ts->ds, 0, y_start,
238 6f7e9aec bellard
                           ts->width, y - y_start);
239 e80cfcfc bellard
                y_start = -1;
240 e80cfcfc bellard
            }
241 f930d07e blueswir1
            d += dd * 4;
242 f930d07e blueswir1
            s += ds * 4;
243 f930d07e blueswir1
        }
244 e80cfcfc bellard
    }
245 e80cfcfc bellard
    if (y_start >= 0) {
246 f930d07e blueswir1
        /* flush to display */
247 f930d07e blueswir1
        dpy_update(ts->ds, 0, y_start,
248 f930d07e blueswir1
                   ts->width, y - y_start);
249 e80cfcfc bellard
    }
250 e80cfcfc bellard
    /* reset modified pages */
251 550be127 bellard
    if (page_min <= page_max) {
252 0a962c02 bellard
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
253 0a962c02 bellard
                                        VGA_DIRTY_FLAG);
254 e80cfcfc bellard
    }
255 420557e8 bellard
}
256 420557e8 bellard
257 eee0b836 blueswir1
static void tcx24_update_display(void *opaque)
258 eee0b836 blueswir1
{
259 eee0b836 blueswir1
    TCXState *ts = opaque;
260 eee0b836 blueswir1
    ram_addr_t page, page_min, page_max, cpage, page24;
261 eee0b836 blueswir1
    int y, y_start, dd, ds;
262 eee0b836 blueswir1
    uint8_t *d, *s;
263 eee0b836 blueswir1
    uint32_t *cptr, *s24;
264 eee0b836 blueswir1
265 eee0b836 blueswir1
    if (ts->ds->depth != 32)
266 eee0b836 blueswir1
            return;
267 eee0b836 blueswir1
    page = ts->vram_offset;
268 eee0b836 blueswir1
    page24 = ts->vram24_offset;
269 eee0b836 blueswir1
    cpage = ts->cplane_offset;
270 eee0b836 blueswir1
    y_start = -1;
271 eee0b836 blueswir1
    page_min = 0xffffffff;
272 eee0b836 blueswir1
    page_max = 0;
273 eee0b836 blueswir1
    d = ts->ds->data;
274 eee0b836 blueswir1
    s = ts->vram;
275 eee0b836 blueswir1
    s24 = ts->vram24;
276 eee0b836 blueswir1
    cptr = ts->cplane;
277 eee0b836 blueswir1
    dd = ts->ds->linesize;
278 eee0b836 blueswir1
    ds = 1024;
279 eee0b836 blueswir1
280 eee0b836 blueswir1
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
281 eee0b836 blueswir1
            page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
282 eee0b836 blueswir1
        if (check_dirty(ts, page, page24, cpage)) {
283 eee0b836 blueswir1
            if (y_start < 0)
284 eee0b836 blueswir1
                y_start = y;
285 eee0b836 blueswir1
            if (page < page_min)
286 eee0b836 blueswir1
                page_min = page;
287 eee0b836 blueswir1
            if (page > page_max)
288 eee0b836 blueswir1
                page_max = page;
289 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
290 eee0b836 blueswir1
            d += dd;
291 eee0b836 blueswir1
            s += ds;
292 eee0b836 blueswir1
            cptr += ds;
293 eee0b836 blueswir1
            s24 += ds;
294 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
295 eee0b836 blueswir1
            d += dd;
296 eee0b836 blueswir1
            s += ds;
297 eee0b836 blueswir1
            cptr += ds;
298 eee0b836 blueswir1
            s24 += ds;
299 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
300 eee0b836 blueswir1
            d += dd;
301 eee0b836 blueswir1
            s += ds;
302 eee0b836 blueswir1
            cptr += ds;
303 eee0b836 blueswir1
            s24 += ds;
304 eee0b836 blueswir1
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
305 eee0b836 blueswir1
            d += dd;
306 eee0b836 blueswir1
            s += ds;
307 eee0b836 blueswir1
            cptr += ds;
308 eee0b836 blueswir1
            s24 += ds;
309 eee0b836 blueswir1
        } else {
310 eee0b836 blueswir1
            if (y_start >= 0) {
311 eee0b836 blueswir1
                /* flush to display */
312 eee0b836 blueswir1
                dpy_update(ts->ds, 0, y_start,
313 eee0b836 blueswir1
                           ts->width, y - y_start);
314 eee0b836 blueswir1
                y_start = -1;
315 eee0b836 blueswir1
            }
316 eee0b836 blueswir1
            d += dd * 4;
317 eee0b836 blueswir1
            s += ds * 4;
318 eee0b836 blueswir1
            cptr += ds * 4;
319 eee0b836 blueswir1
            s24 += ds * 4;
320 eee0b836 blueswir1
        }
321 eee0b836 blueswir1
    }
322 eee0b836 blueswir1
    if (y_start >= 0) {
323 eee0b836 blueswir1
        /* flush to display */
324 eee0b836 blueswir1
        dpy_update(ts->ds, 0, y_start,
325 eee0b836 blueswir1
                   ts->width, y - y_start);
326 eee0b836 blueswir1
    }
327 eee0b836 blueswir1
    /* reset modified pages */
328 eee0b836 blueswir1
    if (page_min <= page_max) {
329 eee0b836 blueswir1
        reset_dirty(ts, page_min, page_max, page24, cpage);
330 eee0b836 blueswir1
    }
331 eee0b836 blueswir1
}
332 eee0b836 blueswir1
333 95219897 pbrook
static void tcx_invalidate_display(void *opaque)
334 420557e8 bellard
{
335 e80cfcfc bellard
    TCXState *s = opaque;
336 e80cfcfc bellard
    int i;
337 e80cfcfc bellard
338 e80cfcfc bellard
    for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
339 f930d07e blueswir1
        cpu_physical_memory_set_dirty(s->vram_offset + i);
340 e80cfcfc bellard
    }
341 420557e8 bellard
}
342 420557e8 bellard
343 eee0b836 blueswir1
static void tcx24_invalidate_display(void *opaque)
344 eee0b836 blueswir1
{
345 eee0b836 blueswir1
    TCXState *s = opaque;
346 eee0b836 blueswir1
    int i;
347 eee0b836 blueswir1
348 eee0b836 blueswir1
    tcx_invalidate_display(s);
349 eee0b836 blueswir1
    for (i = 0; i < MAXX*MAXY * 4; i += TARGET_PAGE_SIZE) {
350 eee0b836 blueswir1
        cpu_physical_memory_set_dirty(s->vram24_offset + i);
351 eee0b836 blueswir1
        cpu_physical_memory_set_dirty(s->cplane_offset + i);
352 eee0b836 blueswir1
    }
353 eee0b836 blueswir1
}
354 eee0b836 blueswir1
355 e80cfcfc bellard
static void tcx_save(QEMUFile *f, void *opaque)
356 420557e8 bellard
{
357 420557e8 bellard
    TCXState *s = opaque;
358 3b46e624 ths
359 6f7e9aec bellard
    qemu_put_be16s(f, (uint16_t *)&s->height);
360 6f7e9aec bellard
    qemu_put_be16s(f, (uint16_t *)&s->width);
361 eee0b836 blueswir1
    qemu_put_be16s(f, (uint16_t *)&s->depth);
362 e80cfcfc bellard
    qemu_put_buffer(f, s->r, 256);
363 e80cfcfc bellard
    qemu_put_buffer(f, s->g, 256);
364 e80cfcfc bellard
    qemu_put_buffer(f, s->b, 256);
365 6f7e9aec bellard
    qemu_put_8s(f, &s->dac_index);
366 6f7e9aec bellard
    qemu_put_8s(f, &s->dac_state);
367 420557e8 bellard
}
368 420557e8 bellard
369 e80cfcfc bellard
static int tcx_load(QEMUFile *f, void *opaque, int version_id)
370 420557e8 bellard
{
371 e80cfcfc bellard
    TCXState *s = opaque;
372 fda77c2d blueswir1
    uint32_t dummy;
373 fda77c2d blueswir1
374 fda77c2d blueswir1
    if (version_id != 3 && version_id != 4)
375 e80cfcfc bellard
        return -EINVAL;
376 e80cfcfc bellard
377 fda77c2d blueswir1
    if (version_id == 3) {
378 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
379 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
380 fda77c2d blueswir1
        qemu_get_be32s(f, (uint32_t *)&dummy);
381 fda77c2d blueswir1
    }
382 6f7e9aec bellard
    qemu_get_be16s(f, (uint16_t *)&s->height);
383 6f7e9aec bellard
    qemu_get_be16s(f, (uint16_t *)&s->width);
384 eee0b836 blueswir1
    qemu_get_be16s(f, (uint16_t *)&s->depth);
385 e80cfcfc bellard
    qemu_get_buffer(f, s->r, 256);
386 e80cfcfc bellard
    qemu_get_buffer(f, s->g, 256);
387 e80cfcfc bellard
    qemu_get_buffer(f, s->b, 256);
388 6f7e9aec bellard
    qemu_get_8s(f, &s->dac_index);
389 6f7e9aec bellard
    qemu_get_8s(f, &s->dac_state);
390 21206a10 bellard
    update_palette_entries(s, 0, 256);
391 97e7df27 blueswir1
    if (s->depth == 24)
392 97e7df27 blueswir1
        tcx24_invalidate_display(s);
393 97e7df27 blueswir1
    else
394 97e7df27 blueswir1
        tcx_invalidate_display(s);
395 5425a216 blueswir1
396 e80cfcfc bellard
    return 0;
397 420557e8 bellard
}
398 420557e8 bellard
399 e80cfcfc bellard
static void tcx_reset(void *opaque)
400 420557e8 bellard
{
401 e80cfcfc bellard
    TCXState *s = opaque;
402 e80cfcfc bellard
403 e80cfcfc bellard
    /* Initialize palette */
404 e80cfcfc bellard
    memset(s->r, 0, 256);
405 e80cfcfc bellard
    memset(s->g, 0, 256);
406 e80cfcfc bellard
    memset(s->b, 0, 256);
407 e80cfcfc bellard
    s->r[255] = s->g[255] = s->b[255] = 255;
408 21206a10 bellard
    update_palette_entries(s, 0, 256);
409 e80cfcfc bellard
    memset(s->vram, 0, MAXX*MAXY);
410 eee0b836 blueswir1
    cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset +
411 eee0b836 blueswir1
                                    MAXX * MAXY * (1 + 4 + 4), VGA_DIRTY_FLAG);
412 6f7e9aec bellard
    s->dac_index = 0;
413 6f7e9aec bellard
    s->dac_state = 0;
414 6f7e9aec bellard
}
415 6f7e9aec bellard
416 6f7e9aec bellard
static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
417 6f7e9aec bellard
{
418 6f7e9aec bellard
    return 0;
419 6f7e9aec bellard
}
420 6f7e9aec bellard
421 6f7e9aec bellard
static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
422 6f7e9aec bellard
{
423 6f7e9aec bellard
    TCXState *s = opaque;
424 6f7e9aec bellard
    uint32_t saddr;
425 6f7e9aec bellard
426 6f7e9aec bellard
    saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
427 6f7e9aec bellard
    switch (saddr) {
428 6f7e9aec bellard
    case 0:
429 f930d07e blueswir1
        s->dac_index = val >> 24;
430 f930d07e blueswir1
        s->dac_state = 0;
431 f930d07e blueswir1
        break;
432 6f7e9aec bellard
    case 1:
433 f930d07e blueswir1
        switch (s->dac_state) {
434 f930d07e blueswir1
        case 0:
435 f930d07e blueswir1
            s->r[s->dac_index] = val >> 24;
436 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
437 f930d07e blueswir1
            s->dac_state++;
438 f930d07e blueswir1
            break;
439 f930d07e blueswir1
        case 1:
440 f930d07e blueswir1
            s->g[s->dac_index] = val >> 24;
441 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
442 f930d07e blueswir1
            s->dac_state++;
443 f930d07e blueswir1
            break;
444 f930d07e blueswir1
        case 2:
445 f930d07e blueswir1
            s->b[s->dac_index] = val >> 24;
446 21206a10 bellard
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
447 5c8cdbf8 blueswir1
            s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
448 f930d07e blueswir1
        default:
449 f930d07e blueswir1
            s->dac_state = 0;
450 f930d07e blueswir1
            break;
451 f930d07e blueswir1
        }
452 f930d07e blueswir1
        break;
453 6f7e9aec bellard
    default:
454 f930d07e blueswir1
        break;
455 6f7e9aec bellard
    }
456 6f7e9aec bellard
    return;
457 420557e8 bellard
}
458 420557e8 bellard
459 6f7e9aec bellard
static CPUReadMemoryFunc *tcx_dac_read[3] = {
460 7c560456 blueswir1
    NULL,
461 7c560456 blueswir1
    NULL,
462 6f7e9aec bellard
    tcx_dac_readl,
463 6f7e9aec bellard
};
464 6f7e9aec bellard
465 6f7e9aec bellard
static CPUWriteMemoryFunc *tcx_dac_write[3] = {
466 7c560456 blueswir1
    NULL,
467 7c560456 blueswir1
    NULL,
468 6f7e9aec bellard
    tcx_dac_writel,
469 6f7e9aec bellard
};
470 6f7e9aec bellard
471 8508b89e blueswir1
static uint32_t tcx_dummy_readl(void *opaque, target_phys_addr_t addr)
472 8508b89e blueswir1
{
473 8508b89e blueswir1
    return 0;
474 8508b89e blueswir1
}
475 8508b89e blueswir1
476 8508b89e blueswir1
static void tcx_dummy_writel(void *opaque, target_phys_addr_t addr,
477 8508b89e blueswir1
                             uint32_t val)
478 8508b89e blueswir1
{
479 8508b89e blueswir1
}
480 8508b89e blueswir1
481 8508b89e blueswir1
static CPUReadMemoryFunc *tcx_dummy_read[3] = {
482 7c560456 blueswir1
    NULL,
483 7c560456 blueswir1
    NULL,
484 8508b89e blueswir1
    tcx_dummy_readl,
485 8508b89e blueswir1
};
486 8508b89e blueswir1
487 8508b89e blueswir1
static CPUWriteMemoryFunc *tcx_dummy_write[3] = {
488 7c560456 blueswir1
    NULL,
489 7c560456 blueswir1
    NULL,
490 8508b89e blueswir1
    tcx_dummy_writel,
491 8508b89e blueswir1
};
492 8508b89e blueswir1
493 5dcb6b91 blueswir1
void tcx_init(DisplayState *ds, target_phys_addr_t addr, uint8_t *vram_base,
494 eee0b836 blueswir1
              unsigned long vram_offset, int vram_size, int width, int height,
495 eee0b836 blueswir1
              int depth)
496 420557e8 bellard
{
497 420557e8 bellard
    TCXState *s;
498 8508b89e blueswir1
    int io_memory, dummy_memory;
499 eee0b836 blueswir1
    int size;
500 420557e8 bellard
501 420557e8 bellard
    s = qemu_mallocz(sizeof(TCXState));
502 420557e8 bellard
    if (!s)
503 95219897 pbrook
        return;
504 420557e8 bellard
    s->ds = ds;
505 8d5f07fa bellard
    s->addr = addr;
506 e80cfcfc bellard
    s->vram_offset = vram_offset;
507 6f7e9aec bellard
    s->width = width;
508 6f7e9aec bellard
    s->height = height;
509 eee0b836 blueswir1
    s->depth = depth;
510 eee0b836 blueswir1
511 eee0b836 blueswir1
    // 8-bit plane
512 eee0b836 blueswir1
    s->vram = vram_base;
513 eee0b836 blueswir1
    size = vram_size;
514 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00800000ULL, size, vram_offset);
515 eee0b836 blueswir1
    vram_offset += size;
516 eee0b836 blueswir1
    vram_base += size;
517 e80cfcfc bellard
518 6f7e9aec bellard
    io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
519 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00200000ULL, TCX_DAC_NREGS, io_memory);
520 eee0b836 blueswir1
521 8508b89e blueswir1
    dummy_memory = cpu_register_io_memory(0, tcx_dummy_read, tcx_dummy_write,
522 8508b89e blueswir1
                                          s);
523 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00700000ULL, TCX_TEC_NREGS,
524 8508b89e blueswir1
                                 dummy_memory);
525 eee0b836 blueswir1
    if (depth == 24) {
526 eee0b836 blueswir1
        // 24-bit plane
527 eee0b836 blueswir1
        size = vram_size * 4;
528 eee0b836 blueswir1
        s->vram24 = (uint32_t *)vram_base;
529 eee0b836 blueswir1
        s->vram24_offset = vram_offset;
530 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x02000000ULL, size, vram_offset);
531 eee0b836 blueswir1
        vram_offset += size;
532 eee0b836 blueswir1
        vram_base += size;
533 eee0b836 blueswir1
534 eee0b836 blueswir1
        // Control plane
535 eee0b836 blueswir1
        size = vram_size * 4;
536 eee0b836 blueswir1
        s->cplane = (uint32_t *)vram_base;
537 eee0b836 blueswir1
        s->cplane_offset = vram_offset;
538 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x0a000000ULL, size, vram_offset);
539 8508b89e blueswir1
        graphic_console_init(s->ds, tcx24_update_display,
540 4d3b6f6e balrog
                             tcx24_invalidate_display,
541 4d3b6f6e balrog
                             tcx24_screen_dump, NULL, s);
542 eee0b836 blueswir1
    } else {
543 5dcb6b91 blueswir1
        cpu_register_physical_memory(addr + 0x00300000ULL, TCX_THC_NREGS_8,
544 8508b89e blueswir1
                                     dummy_memory);
545 eee0b836 blueswir1
        graphic_console_init(s->ds, tcx_update_display, tcx_invalidate_display,
546 4d3b6f6e balrog
                             tcx_screen_dump, NULL, s);
547 eee0b836 blueswir1
    }
548 f96f4c9d blueswir1
    // NetBSD writes here even with 8-bit display
549 5dcb6b91 blueswir1
    cpu_register_physical_memory(addr + 0x00301000ULL, TCX_THC_NREGS_24,
550 f96f4c9d blueswir1
                                 dummy_memory);
551 e80cfcfc bellard
552 fda77c2d blueswir1
    register_savevm("tcx", addr, 4, tcx_save, tcx_load, s);
553 e80cfcfc bellard
    qemu_register_reset(tcx_reset, s);
554 e80cfcfc bellard
    tcx_reset(s);
555 6f7e9aec bellard
    dpy_resize(s->ds, width, height);
556 420557e8 bellard
}
557 420557e8 bellard
558 95219897 pbrook
static void tcx_screen_dump(void *opaque, const char *filename)
559 8d5f07fa bellard
{
560 e80cfcfc bellard
    TCXState *s = opaque;
561 8d5f07fa bellard
    FILE *f;
562 e80cfcfc bellard
    uint8_t *d, *d1, v;
563 8d5f07fa bellard
    int y, x;
564 8d5f07fa bellard
565 8d5f07fa bellard
    f = fopen(filename, "wb");
566 8d5f07fa bellard
    if (!f)
567 e80cfcfc bellard
        return;
568 6f7e9aec bellard
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
569 6f7e9aec bellard
    d1 = s->vram;
570 6f7e9aec bellard
    for(y = 0; y < s->height; y++) {
571 8d5f07fa bellard
        d = d1;
572 6f7e9aec bellard
        for(x = 0; x < s->width; x++) {
573 8d5f07fa bellard
            v = *d;
574 e80cfcfc bellard
            fputc(s->r[v], f);
575 e80cfcfc bellard
            fputc(s->g[v], f);
576 e80cfcfc bellard
            fputc(s->b[v], f);
577 8d5f07fa bellard
            d++;
578 8d5f07fa bellard
        }
579 e80cfcfc bellard
        d1 += MAXX;
580 8d5f07fa bellard
    }
581 8d5f07fa bellard
    fclose(f);
582 8d5f07fa bellard
    return;
583 8d5f07fa bellard
}
584 8d5f07fa bellard
585 eee0b836 blueswir1
static void tcx24_screen_dump(void *opaque, const char *filename)
586 eee0b836 blueswir1
{
587 eee0b836 blueswir1
    TCXState *s = opaque;
588 eee0b836 blueswir1
    FILE *f;
589 eee0b836 blueswir1
    uint8_t *d, *d1, v;
590 eee0b836 blueswir1
    uint32_t *s24, *cptr, dval;
591 eee0b836 blueswir1
    int y, x;
592 8d5f07fa bellard
593 eee0b836 blueswir1
    f = fopen(filename, "wb");
594 eee0b836 blueswir1
    if (!f)
595 eee0b836 blueswir1
        return;
596 eee0b836 blueswir1
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
597 eee0b836 blueswir1
    d1 = s->vram;
598 eee0b836 blueswir1
    s24 = s->vram24;
599 eee0b836 blueswir1
    cptr = s->cplane;
600 eee0b836 blueswir1
    for(y = 0; y < s->height; y++) {
601 eee0b836 blueswir1
        d = d1;
602 eee0b836 blueswir1
        for(x = 0; x < s->width; x++, d++, s24++) {
603 eee0b836 blueswir1
            if ((*cptr++ & 0xff000000) == 0x03000000) { // 24-bit direct
604 eee0b836 blueswir1
                dval = *s24 & 0x00ffffff;
605 eee0b836 blueswir1
                fputc((dval >> 16) & 0xff, f);
606 eee0b836 blueswir1
                fputc((dval >> 8) & 0xff, f);
607 eee0b836 blueswir1
                fputc(dval & 0xff, f);
608 eee0b836 blueswir1
            } else {
609 eee0b836 blueswir1
                v = *d;
610 eee0b836 blueswir1
                fputc(s->r[v], f);
611 eee0b836 blueswir1
                fputc(s->g[v], f);
612 eee0b836 blueswir1
                fputc(s->b[v], f);
613 eee0b836 blueswir1
            }
614 eee0b836 blueswir1
        }
615 eee0b836 blueswir1
        d1 += MAXX;
616 eee0b836 blueswir1
    }
617 eee0b836 blueswir1
    fclose(f);
618 eee0b836 blueswir1
    return;
619 eee0b836 blueswir1
}