Blame view

hw/tcx.c 17.5 KB
1
/*
bellard authored
2
 * QEMU TCX Frame buffer
3
 *
bellard authored
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
pbrook authored
24
25
26
#include "hw.h"
#include "sun4m.h"
#include "console.h"
27
#include "pixel_ops.h"
28
29
30

#define MAXX 1024
#define MAXY 768
bellard authored
31
#define TCX_DAC_NREGS 16
32
33
34
#define TCX_THC_NREGS_8  0x081c
#define TCX_THC_NREGS_24 0x1000
#define TCX_TEC_NREGS    0x1000
35
36

typedef struct TCXState {
37
    target_phys_addr_t addr;
38
    DisplayState *ds;
bellard authored
39
    uint8_t *vram;
blueswir1 authored
40
41
42
    uint32_t *vram24, *cplane;
    ram_addr_t vram_offset, vram24_offset, cplane_offset;
    uint16_t width, height, depth;
bellard authored
43
    uint8_t r[256], g[256], b[256];
44
    uint32_t palette[256];
bellard authored
45
    uint8_t dac_index, dac_state;
46
47
} TCXState;
48
static void tcx_screen_dump(void *opaque, const char *filename);
blueswir1 authored
49
static void tcx24_screen_dump(void *opaque, const char *filename);
50
51
static void tcx_invalidate_display(void *opaque);
static void tcx24_invalidate_display(void *opaque);
52
53
54
55
56
static void update_palette_entries(TCXState *s, int start, int end)
{
    int i;
    for(i = start; i < end; i++) {
57
        switch(ds_get_bits_per_pixel(s->ds)) {
58
59
60
61
62
        default:
        case 8:
            s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
            break;
        case 15:
63
            s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
64
65
            break;
        case 16:
66
            s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
67
68
            break;
        case 32:
69
70
71
72
            if (is_surface_bgr(s->ds->surface))
                s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
            else
                s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
73
74
75
            break;
        }
    }
76
77
78
79
    if (s->depth == 24)
        tcx24_invalidate_display(s);
    else
        tcx_invalidate_display(s);
80
81
}
82
static void tcx_draw_line32(TCXState *s1, uint8_t *d,
blueswir1 authored
83
                            const uint8_t *s, int width)
84
{
bellard authored
85
86
    int x;
    uint8_t val;
87
    uint32_t *p = (uint32_t *)d;
bellard authored
88
89

    for(x = 0; x < width; x++) {
blueswir1 authored
90
        val = *s++;
91
        *p++ = s1->palette[val];
bellard authored
92
    }
93
94
}
95
static void tcx_draw_line16(TCXState *s1, uint8_t *d,
blueswir1 authored
96
                            const uint8_t *s, int width)
bellard authored
97
98
99
{
    int x;
    uint8_t val;
100
    uint16_t *p = (uint16_t *)d;
bellard authored
101
bellard authored
102
    for(x = 0; x < width; x++) {
blueswir1 authored
103
        val = *s++;
104
        *p++ = s1->palette[val];
bellard authored
105
106
107
    }
}
108
static void tcx_draw_line8(TCXState *s1, uint8_t *d,
blueswir1 authored
109
                           const uint8_t *s, int width)
110
{
bellard authored
111
112
113
114
    int x;
    uint8_t val;

    for(x = 0; x < width; x++) {
blueswir1 authored
115
        val = *s++;
116
        *d++ = s1->palette[val];
117
118
119
    }
}
blueswir1 authored
120
121
122
123
124
/*
  XXX Could be much more optimal:
  * detect if line/page/whole screen is in 24 bit mode
  * if destination is also BGR, use memcpy
  */
blueswir1 authored
125
126
127
128
129
static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
                                     const uint8_t *s, int width,
                                     const uint32_t *cplane,
                                     const uint32_t *s24)
{
130
    int x, bgr, r, g, b;
blueswir1 authored
131
    uint8_t val, *p8;
blueswir1 authored
132
133
134
    uint32_t *p = (uint32_t *)d;
    uint32_t dval;
135
    bgr = is_surface_bgr(s1->ds->surface);
blueswir1 authored
136
    for(x = 0; x < width; x++, s++, s24++) {
blueswir1 authored
137
138
139
140
141
142
143
        if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
            // 24-bit direct, BGR order
            p8 = (uint8_t *)s24;
            p8++;
            b = *p8++;
            g = *p8++;
            r = *p8++;
144
145
146
147
            if (bgr)
                dval = rgb_to_pixel32bgr(r, g, b);
            else
                dval = rgb_to_pixel32(r, g, b);
blueswir1 authored
148
149
150
151
152
153
154
155
        } else {
            val = *s;
            dval = s1->palette[val];
        }
        *p++ = dval;
    }
}
blueswir1 authored
156
static inline int check_dirty(ram_addr_t page, ram_addr_t page24,
blueswir1 authored
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                              ram_addr_t cpage)
{
    int ret;
    unsigned int off;

    ret = cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG);
    for (off = 0; off < TARGET_PAGE_SIZE * 4; off += TARGET_PAGE_SIZE) {
        ret |= cpu_physical_memory_get_dirty(page24 + off, VGA_DIRTY_FLAG);
        ret |= cpu_physical_memory_get_dirty(cpage + off, VGA_DIRTY_FLAG);
    }
    return ret;
}

static inline void reset_dirty(TCXState *ts, ram_addr_t page_min,
                               ram_addr_t page_max, ram_addr_t page24,
                              ram_addr_t cpage)
{
    cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
                                    VGA_DIRTY_FLAG);
    page_min -= ts->vram_offset;
    page_max -= ts->vram_offset;
    cpu_physical_memory_reset_dirty(page24 + page_min * 4,
                                    page24 + page_max * 4 + TARGET_PAGE_SIZE,
                                    VGA_DIRTY_FLAG);
    cpu_physical_memory_reset_dirty(cpage + page_min * 4,
                                    cpage + page_max * 4 + TARGET_PAGE_SIZE,
                                    VGA_DIRTY_FLAG);
}
bellard authored
186
187
/* Fixed line length 1024 allows us to do nice tricks not possible on
   VGA... */
188
static void tcx_update_display(void *opaque)
189
{
bellard authored
190
    TCXState *ts = opaque;
191
192
    ram_addr_t page, page_min, page_max;
    int y, y_start, dd, ds;
bellard authored
193
    uint8_t *d, *s;
194
    void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
bellard authored
195
196
    if (ds_get_bits_per_pixel(ts->ds) == 0)
blueswir1 authored
197
        return;
bellard authored
198
    page = ts->vram_offset;
bellard authored
199
    y_start = -1;
Blue Swirl authored
200
    page_min = -1;
201
    page_max = 0;
202
    d = ds_get_data(ts->ds);
bellard authored
203
    s = ts->vram;
204
    dd = ds_get_linesize(ts->ds);
bellard authored
205
206
    ds = 1024;
207
    switch (ds_get_bits_per_pixel(ts->ds)) {
bellard authored
208
    case 32:
blueswir1 authored
209
210
        f = tcx_draw_line32;
        break;
211
212
    case 15:
    case 16:
blueswir1 authored
213
214
        f = tcx_draw_line16;
        break;
bellard authored
215
216
    default:
    case 8:
blueswir1 authored
217
218
        f = tcx_draw_line8;
        break;
bellard authored
219
    case 0:
blueswir1 authored
220
        return;
bellard authored
221
    }
222
bellard authored
223
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
blueswir1 authored
224
225
        if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
            if (y_start < 0)
bellard authored
226
227
228
229
230
                y_start = y;
            if (page < page_min)
                page_min = page;
            if (page > page_max)
                page_max = page;
blueswir1 authored
231
232
233
234
235
236
237
238
239
240
241
242
243
            f(ts, d, s, ts->width);
            d += dd;
            s += ds;
            f(ts, d, s, ts->width);
            d += dd;
            s += ds;
            f(ts, d, s, ts->width);
            d += dd;
            s += ds;
            f(ts, d, s, ts->width);
            d += dd;
            s += ds;
        } else {
bellard authored
244
245
            if (y_start >= 0) {
                /* flush to display */
246
                dpy_update(ts->ds, 0, y_start,
bellard authored
247
                           ts->width, y - y_start);
bellard authored
248
249
                y_start = -1;
            }
blueswir1 authored
250
251
252
            d += dd * 4;
            s += ds * 4;
        }
bellard authored
253
254
    }
    if (y_start >= 0) {
blueswir1 authored
255
256
257
        /* flush to display */
        dpy_update(ts->ds, 0, y_start,
                   ts->width, y - y_start);
bellard authored
258
259
    }
    /* reset modified pages */
Blue Swirl authored
260
    if (page_max >= page_min) {
bellard authored
261
262
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
                                        VGA_DIRTY_FLAG);
bellard authored
263
    }
264
265
}
blueswir1 authored
266
267
268
269
270
271
272
273
static void tcx24_update_display(void *opaque)
{
    TCXState *ts = opaque;
    ram_addr_t page, page_min, page_max, cpage, page24;
    int y, y_start, dd, ds;
    uint8_t *d, *s;
    uint32_t *cptr, *s24;
274
    if (ds_get_bits_per_pixel(ts->ds) != 32)
blueswir1 authored
275
276
277
278
279
            return;
    page = ts->vram_offset;
    page24 = ts->vram24_offset;
    cpage = ts->cplane_offset;
    y_start = -1;
Blue Swirl authored
280
    page_min = -1;
blueswir1 authored
281
    page_max = 0;
282
    d = ds_get_data(ts->ds);
blueswir1 authored
283
284
285
    s = ts->vram;
    s24 = ts->vram24;
    cptr = ts->cplane;
286
    dd = ds_get_linesize(ts->ds);
blueswir1 authored
287
288
289
290
    ds = 1024;

    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
            page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
blueswir1 authored
291
        if (check_dirty(page, page24, cpage)) {
blueswir1 authored
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
            if (y_start < 0)
                y_start = y;
            if (page < page_min)
                page_min = page;
            if (page > page_max)
                page_max = page;
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
            d += dd;
            s += ds;
            cptr += ds;
            s24 += ds;
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
            d += dd;
            s += ds;
            cptr += ds;
            s24 += ds;
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
            d += dd;
            s += ds;
            cptr += ds;
            s24 += ds;
            tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
            d += dd;
            s += ds;
            cptr += ds;
            s24 += ds;
        } else {
            if (y_start >= 0) {
                /* flush to display */
                dpy_update(ts->ds, 0, y_start,
                           ts->width, y - y_start);
                y_start = -1;
            }
            d += dd * 4;
            s += ds * 4;
            cptr += ds * 4;
            s24 += ds * 4;
        }
    }
    if (y_start >= 0) {
        /* flush to display */
        dpy_update(ts->ds, 0, y_start,
                   ts->width, y - y_start);
    }
    /* reset modified pages */
Blue Swirl authored
337
    if (page_max >= page_min) {
blueswir1 authored
338
339
340
341
        reset_dirty(ts, page_min, page_max, page24, cpage);
    }
}
342
static void tcx_invalidate_display(void *opaque)
343
{
bellard authored
344
345
346
347
    TCXState *s = opaque;
    int i;

    for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
blueswir1 authored
348
        cpu_physical_memory_set_dirty(s->vram_offset + i);
bellard authored
349
    }
350
351
}
blueswir1 authored
352
353
354
355
356
357
358
359
360
361
362
363
static void tcx24_invalidate_display(void *opaque)
{
    TCXState *s = opaque;
    int i;

    tcx_invalidate_display(s);
    for (i = 0; i < MAXX*MAXY * 4; i += TARGET_PAGE_SIZE) {
        cpu_physical_memory_set_dirty(s->vram24_offset + i);
        cpu_physical_memory_set_dirty(s->cplane_offset + i);
    }
}
bellard authored
364
static void tcx_save(QEMUFile *f, void *opaque)
365
366
{
    TCXState *s = opaque;
367
368
369
370
    qemu_put_be16s(f, &s->height);
    qemu_put_be16s(f, &s->width);
    qemu_put_be16s(f, &s->depth);
bellard authored
371
372
373
    qemu_put_buffer(f, s->r, 256);
    qemu_put_buffer(f, s->g, 256);
    qemu_put_buffer(f, s->b, 256);
bellard authored
374
375
    qemu_put_8s(f, &s->dac_index);
    qemu_put_8s(f, &s->dac_state);
376
377
}
bellard authored
378
static int tcx_load(QEMUFile *f, void *opaque, int version_id)
379
{
bellard authored
380
    TCXState *s = opaque;
blueswir1 authored
381
382
383
    uint32_t dummy;

    if (version_id != 3 && version_id != 4)
bellard authored
384
385
        return -EINVAL;
blueswir1 authored
386
    if (version_id == 3) {
387
388
389
        qemu_get_be32s(f, &dummy);
        qemu_get_be32s(f, &dummy);
        qemu_get_be32s(f, &dummy);
blueswir1 authored
390
    }
391
392
393
    qemu_get_be16s(f, &s->height);
    qemu_get_be16s(f, &s->width);
    qemu_get_be16s(f, &s->depth);
bellard authored
394
395
396
    qemu_get_buffer(f, s->r, 256);
    qemu_get_buffer(f, s->g, 256);
    qemu_get_buffer(f, s->b, 256);
bellard authored
397
398
    qemu_get_8s(f, &s->dac_index);
    qemu_get_8s(f, &s->dac_state);
399
    update_palette_entries(s, 0, 256);
400
401
402
403
    if (s->depth == 24)
        tcx24_invalidate_display(s);
    else
        tcx_invalidate_display(s);
404
bellard authored
405
    return 0;
406
407
}
bellard authored
408
static void tcx_reset(void *opaque)
409
{
bellard authored
410
411
412
413
414
415
416
    TCXState *s = opaque;

    /* Initialize palette */
    memset(s->r, 0, 256);
    memset(s->g, 0, 256);
    memset(s->b, 0, 256);
    s->r[255] = s->g[255] = s->b[255] = 255;
417
    update_palette_entries(s, 0, 256);
bellard authored
418
    memset(s->vram, 0, MAXX*MAXY);
blueswir1 authored
419
420
    cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset +
                                    MAXX * MAXY * (1 + 4 + 4), VGA_DIRTY_FLAG);
bellard authored
421
422
423
424
425
426
427
428
429
430
431
432
433
    s->dac_index = 0;
    s->dac_state = 0;
}

static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
{
    return 0;
}

static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
{
    TCXState *s = opaque;
blueswir1 authored
434
    switch (addr) {
bellard authored
435
    case 0:
blueswir1 authored
436
437
438
        s->dac_index = val >> 24;
        s->dac_state = 0;
        break;
blueswir1 authored
439
    case 4:
blueswir1 authored
440
441
442
        switch (s->dac_state) {
        case 0:
            s->r[s->dac_index] = val >> 24;
443
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
444
445
446
447
            s->dac_state++;
            break;
        case 1:
            s->g[s->dac_index] = val >> 24;
448
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
449
450
451
452
            s->dac_state++;
            break;
        case 2:
            s->b[s->dac_index] = val >> 24;
453
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
454
            s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
blueswir1 authored
455
456
457
458
459
        default:
            s->dac_state = 0;
            break;
        }
        break;
bellard authored
460
    default:
blueswir1 authored
461
        break;
bellard authored
462
463
    }
    return;
464
465
}
bellard authored
466
static CPUReadMemoryFunc *tcx_dac_read[3] = {
467
468
    NULL,
    NULL,
bellard authored
469
470
471
472
    tcx_dac_readl,
};

static CPUWriteMemoryFunc *tcx_dac_write[3] = {
473
474
    NULL,
    NULL,
bellard authored
475
476
477
    tcx_dac_writel,
};
478
479
480
481
482
483
484
485
486
487
488
static uint32_t tcx_dummy_readl(void *opaque, target_phys_addr_t addr)
{
    return 0;
}

static void tcx_dummy_writel(void *opaque, target_phys_addr_t addr,
                             uint32_t val)
{
}

static CPUReadMemoryFunc *tcx_dummy_read[3] = {
489
490
    NULL,
    NULL,
491
492
493
494
    tcx_dummy_readl,
};

static CPUWriteMemoryFunc *tcx_dummy_write[3] = {
495
496
    NULL,
    NULL,
497
498
499
    tcx_dummy_writel,
};
500
void tcx_init(target_phys_addr_t addr, int vram_size, int width, int height,
blueswir1 authored
501
              int depth)
502
503
{
    TCXState *s;
504
    int io_memory, dummy_memory;
505
    ram_addr_t vram_offset;
blueswir1 authored
506
    int size;
507
508
    uint8_t *vram_base;
509
    vram_offset = qemu_ram_alloc(vram_size * (1 + 4 + 4));
510
    vram_base = qemu_get_ram_ptr(vram_offset);
511
512

    s = qemu_mallocz(sizeof(TCXState));
bellard authored
513
    s->addr = addr;
bellard authored
514
    s->vram_offset = vram_offset;
bellard authored
515
516
    s->width = width;
    s->height = height;
blueswir1 authored
517
518
519
520
521
    s->depth = depth;

    // 8-bit plane
    s->vram = vram_base;
    size = vram_size;
522
    cpu_register_physical_memory(addr + 0x00800000ULL, size, vram_offset);
blueswir1 authored
523
524
    vram_offset += size;
    vram_base += size;
bellard authored
525
bellard authored
526
    io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
blueswir1 authored
527
528
    cpu_register_physical_memory(addr + 0x00200000ULL, TCX_DAC_NREGS,
                                 io_memory);
blueswir1 authored
529
530
531
    dummy_memory = cpu_register_io_memory(0, tcx_dummy_read, tcx_dummy_write,
                                          s);
532
    cpu_register_physical_memory(addr + 0x00700000ULL, TCX_TEC_NREGS,
533
                                 dummy_memory);
blueswir1 authored
534
535
536
537
538
    if (depth == 24) {
        // 24-bit plane
        size = vram_size * 4;
        s->vram24 = (uint32_t *)vram_base;
        s->vram24_offset = vram_offset;
539
        cpu_register_physical_memory(addr + 0x02000000ULL, size, vram_offset);
blueswir1 authored
540
541
542
543
544
545
546
        vram_offset += size;
        vram_base += size;

        // Control plane
        size = vram_size * 4;
        s->cplane = (uint32_t *)vram_base;
        s->cplane_offset = vram_offset;
547
        cpu_register_physical_memory(addr + 0x0a000000ULL, size, vram_offset);
548
549
550
        s->ds = graphic_console_init(tcx24_update_display,
                                     tcx24_invalidate_display,
                                     tcx24_screen_dump, NULL, s);
blueswir1 authored
551
    } else {
552
        cpu_register_physical_memory(addr + 0x00300000ULL, TCX_THC_NREGS_8,
553
                                     dummy_memory);
554
555
556
        s->ds = graphic_console_init(tcx_update_display,
                                     tcx_invalidate_display,
                                     tcx_screen_dump, NULL, s);
blueswir1 authored
557
    }
558
    // NetBSD writes here even with 8-bit display
559
    cpu_register_physical_memory(addr + 0x00301000ULL, TCX_THC_NREGS_24,
560
                                 dummy_memory);
bellard authored
561
blueswir1 authored
562
    register_savevm("tcx", addr, 4, tcx_save, tcx_load, s);
563
    qemu_register_reset(tcx_reset, 0, s);
bellard authored
564
    tcx_reset(s);
565
    qemu_console_resize(s->ds, width, height);
566
567
}
568
static void tcx_screen_dump(void *opaque, const char *filename)
bellard authored
569
{
bellard authored
570
    TCXState *s = opaque;
bellard authored
571
    FILE *f;
bellard authored
572
    uint8_t *d, *d1, v;
bellard authored
573
574
575
576
    int y, x;

    f = fopen(filename, "wb");
    if (!f)
bellard authored
577
        return;
bellard authored
578
579
580
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
    d1 = s->vram;
    for(y = 0; y < s->height; y++) {
bellard authored
581
        d = d1;
bellard authored
582
        for(x = 0; x < s->width; x++) {
bellard authored
583
            v = *d;
bellard authored
584
585
586
            fputc(s->r[v], f);
            fputc(s->g[v], f);
            fputc(s->b[v], f);
bellard authored
587
588
            d++;
        }
bellard authored
589
        d1 += MAXX;
bellard authored
590
591
592
593
594
    }
    fclose(f);
    return;
}
blueswir1 authored
595
596
597
598
599
600
601
static void tcx24_screen_dump(void *opaque, const char *filename)
{
    TCXState *s = opaque;
    FILE *f;
    uint8_t *d, *d1, v;
    uint32_t *s24, *cptr, dval;
    int y, x;
bellard authored
602
blueswir1 authored
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    f = fopen(filename, "wb");
    if (!f)
        return;
    fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
    d1 = s->vram;
    s24 = s->vram24;
    cptr = s->cplane;
    for(y = 0; y < s->height; y++) {
        d = d1;
        for(x = 0; x < s->width; x++, d++, s24++) {
            if ((*cptr++ & 0xff000000) == 0x03000000) { // 24-bit direct
                dval = *s24 & 0x00ffffff;
                fputc((dval >> 16) & 0xff, f);
                fputc((dval >> 8) & 0xff, f);
                fputc(dval & 0xff, f);
            } else {
                v = *d;
                fputc(s->r[v], f);
                fputc(s->g[v], f);
                fputc(s->b[v], f);
            }
        }
        d1 += MAXX;
    }
    fclose(f);
    return;
}