Blame view

hw/tcx.c 19.6 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.
 */
24
pbrook authored
25
26
#include "sun4m.h"
#include "console.h"
27
#include "pixel_ops.h"
28
#include "sysbus.h"
29
#include "qdev-addr.h"
30
31
32

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

typedef struct TCXState {
39
    SysBusDevice busdev;
40
    target_phys_addr_t addr;
41
    DisplayState *ds;
bellard authored
42
    uint8_t *vram;
blueswir1 authored
43
44
    uint32_t *vram24, *cplane;
    ram_addr_t vram_offset, vram24_offset, cplane_offset;
45
    uint32_t vram_size;
blueswir1 authored
46
    uint16_t width, height, depth;
bellard authored
47
    uint8_t r[256], g[256], b[256];
48
    uint32_t palette[256];
bellard authored
49
    uint8_t dac_index, dac_state;
50
51
} TCXState;
52
static void tcx_screen_dump(void *opaque, const char *filename);
blueswir1 authored
53
static void tcx24_screen_dump(void *opaque, const char *filename);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

static void tcx_set_dirty(TCXState *s)
{
    unsigned int i;

    for (i = 0; i < MAXX * MAXY; i += TARGET_PAGE_SIZE) {
        cpu_physical_memory_set_dirty(s->vram_offset + i);
    }
}

static void tcx24_set_dirty(TCXState *s)
{
    unsigned int i;

    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);
    }
}
73
74
75
76
77
static void update_palette_entries(TCXState *s, int start, int end)
{
    int i;
    for(i = start; i < end; i++) {
78
        switch(ds_get_bits_per_pixel(s->ds)) {
79
80
81
82
83
        default:
        case 8:
            s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
            break;
        case 15:
84
            s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
85
86
            break;
        case 16:
87
            s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
88
89
            break;
        case 32:
90
91
92
93
            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]);
94
95
96
            break;
        }
    }
97
98
99
100
101
    if (s->depth == 24) {
        tcx24_set_dirty(s);
    } else {
        tcx_set_dirty(s);
    }
102
103
}
104
static void tcx_draw_line32(TCXState *s1, uint8_t *d,
blueswir1 authored
105
                            const uint8_t *s, int width)
106
{
bellard authored
107
108
    int x;
    uint8_t val;
109
    uint32_t *p = (uint32_t *)d;
bellard authored
110
111

    for(x = 0; x < width; x++) {
blueswir1 authored
112
        val = *s++;
113
        *p++ = s1->palette[val];
bellard authored
114
    }
115
116
}
117
static void tcx_draw_line16(TCXState *s1, uint8_t *d,
blueswir1 authored
118
                            const uint8_t *s, int width)
bellard authored
119
120
121
{
    int x;
    uint8_t val;
122
    uint16_t *p = (uint16_t *)d;
bellard authored
123
bellard authored
124
    for(x = 0; x < width; x++) {
blueswir1 authored
125
        val = *s++;
126
        *p++ = s1->palette[val];
bellard authored
127
128
129
    }
}
130
static void tcx_draw_line8(TCXState *s1, uint8_t *d,
blueswir1 authored
131
                           const uint8_t *s, int width)
132
{
bellard authored
133
134
135
136
    int x;
    uint8_t val;

    for(x = 0; x < width; x++) {
blueswir1 authored
137
        val = *s++;
138
        *d++ = s1->palette[val];
139
140
141
    }
}
blueswir1 authored
142
143
144
145
146
/*
  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
147
148
149
150
151
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)
{
152
    int x, bgr, r, g, b;
blueswir1 authored
153
    uint8_t val, *p8;
blueswir1 authored
154
155
156
    uint32_t *p = (uint32_t *)d;
    uint32_t dval;
157
    bgr = is_surface_bgr(s1->ds->surface);
blueswir1 authored
158
    for(x = 0; x < width; x++, s++, s24++) {
blueswir1 authored
159
160
161
162
163
164
165
        if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
            // 24-bit direct, BGR order
            p8 = (uint8_t *)s24;
            p8++;
            b = *p8++;
            g = *p8++;
            r = *p8++;
166
167
168
169
            if (bgr)
                dval = rgb_to_pixel32bgr(r, g, b);
            else
                dval = rgb_to_pixel32(r, g, b);
blueswir1 authored
170
171
172
173
174
175
176
177
        } else {
            val = *s;
            dval = s1->palette[val];
        }
        *p++ = dval;
    }
}
blueswir1 authored
178
static inline int check_dirty(ram_addr_t page, ram_addr_t page24,
blueswir1 authored
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                              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
208
209
/* Fixed line length 1024 allows us to do nice tricks not possible on
   VGA... */
210
static void tcx_update_display(void *opaque)
211
{
bellard authored
212
    TCXState *ts = opaque;
213
214
    ram_addr_t page, page_min, page_max;
    int y, y_start, dd, ds;
bellard authored
215
    uint8_t *d, *s;
216
    void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
bellard authored
217
218
    if (ds_get_bits_per_pixel(ts->ds) == 0)
blueswir1 authored
219
        return;
bellard authored
220
    page = ts->vram_offset;
bellard authored
221
    y_start = -1;
Blue Swirl authored
222
    page_min = -1;
223
    page_max = 0;
224
    d = ds_get_data(ts->ds);
bellard authored
225
    s = ts->vram;
226
    dd = ds_get_linesize(ts->ds);
bellard authored
227
228
    ds = 1024;
229
    switch (ds_get_bits_per_pixel(ts->ds)) {
bellard authored
230
    case 32:
blueswir1 authored
231
232
        f = tcx_draw_line32;
        break;
233
234
    case 15:
    case 16:
blueswir1 authored
235
236
        f = tcx_draw_line16;
        break;
bellard authored
237
238
    default:
    case 8:
blueswir1 authored
239
240
        f = tcx_draw_line8;
        break;
bellard authored
241
    case 0:
blueswir1 authored
242
        return;
bellard authored
243
    }
244
bellard authored
245
    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
blueswir1 authored
246
247
        if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
            if (y_start < 0)
bellard authored
248
249
250
251
252
                y_start = y;
            if (page < page_min)
                page_min = page;
            if (page > page_max)
                page_max = page;
blueswir1 authored
253
254
255
256
257
258
259
260
261
262
263
264
265
            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
266
267
            if (y_start >= 0) {
                /* flush to display */
268
                dpy_update(ts->ds, 0, y_start,
bellard authored
269
                           ts->width, y - y_start);
bellard authored
270
271
                y_start = -1;
            }
blueswir1 authored
272
273
274
            d += dd * 4;
            s += ds * 4;
        }
bellard authored
275
276
    }
    if (y_start >= 0) {
blueswir1 authored
277
278
279
        /* flush to display */
        dpy_update(ts->ds, 0, y_start,
                   ts->width, y - y_start);
bellard authored
280
281
    }
    /* reset modified pages */
Blue Swirl authored
282
    if (page_max >= page_min) {
bellard authored
283
284
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
                                        VGA_DIRTY_FLAG);
bellard authored
285
    }
286
287
}
blueswir1 authored
288
289
290
291
292
293
294
295
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;
296
    if (ds_get_bits_per_pixel(ts->ds) != 32)
blueswir1 authored
297
298
299
300
301
            return;
    page = ts->vram_offset;
    page24 = ts->vram24_offset;
    cpage = ts->cplane_offset;
    y_start = -1;
Blue Swirl authored
302
    page_min = -1;
blueswir1 authored
303
    page_max = 0;
304
    d = ds_get_data(ts->ds);
blueswir1 authored
305
306
307
    s = ts->vram;
    s24 = ts->vram24;
    cptr = ts->cplane;
308
    dd = ds_get_linesize(ts->ds);
blueswir1 authored
309
310
311
312
    ds = 1024;

    for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
            page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
blueswir1 authored
313
        if (check_dirty(page, page24, cpage)) {
blueswir1 authored
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
            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
359
    if (page_max >= page_min) {
blueswir1 authored
360
361
362
363
        reset_dirty(ts, page_min, page_max, page24, cpage);
    }
}
364
static void tcx_invalidate_display(void *opaque)
365
{
bellard authored
366
367
    TCXState *s = opaque;
368
369
    tcx_set_dirty(s);
    qemu_console_resize(s->ds, s->width, s->height);
370
371
}
blueswir1 authored
372
373
374
375
static void tcx24_invalidate_display(void *opaque)
{
    TCXState *s = opaque;
376
377
378
    tcx_set_dirty(s);
    tcx24_set_dirty(s);
    qemu_console_resize(s->ds, s->width, s->height);
blueswir1 authored
379
380
}
bellard authored
381
static void tcx_save(QEMUFile *f, void *opaque)
382
383
{
    TCXState *s = opaque;
384
385
386
387
    qemu_put_be16s(f, &s->height);
    qemu_put_be16s(f, &s->width);
    qemu_put_be16s(f, &s->depth);
bellard authored
388
389
390
    qemu_put_buffer(f, s->r, 256);
    qemu_put_buffer(f, s->g, 256);
    qemu_put_buffer(f, s->b, 256);
bellard authored
391
392
    qemu_put_8s(f, &s->dac_index);
    qemu_put_8s(f, &s->dac_state);
393
394
}
bellard authored
395
static int tcx_load(QEMUFile *f, void *opaque, int version_id)
396
{
bellard authored
397
    TCXState *s = opaque;
blueswir1 authored
398
399
400
    uint32_t dummy;

    if (version_id != 3 && version_id != 4)
bellard authored
401
402
        return -EINVAL;
blueswir1 authored
403
    if (version_id == 3) {
404
405
406
        qemu_get_be32s(f, &dummy);
        qemu_get_be32s(f, &dummy);
        qemu_get_be32s(f, &dummy);
blueswir1 authored
407
    }
408
409
410
    qemu_get_be16s(f, &s->height);
    qemu_get_be16s(f, &s->width);
    qemu_get_be16s(f, &s->depth);
bellard authored
411
412
413
    qemu_get_buffer(f, s->r, 256);
    qemu_get_buffer(f, s->g, 256);
    qemu_get_buffer(f, s->b, 256);
bellard authored
414
415
    qemu_get_8s(f, &s->dac_index);
    qemu_get_8s(f, &s->dac_state);
416
    update_palette_entries(s, 0, 256);
417
418
419
420
421
    if (s->depth == 24) {
        tcx24_set_dirty(s);
    } else {
        tcx_set_dirty(s);
    }
422
bellard authored
423
    return 0;
424
425
}
bellard authored
426
static void tcx_reset(void *opaque)
427
{
bellard authored
428
429
430
431
432
433
434
    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;
435
    update_palette_entries(s, 0, 256);
bellard authored
436
    memset(s->vram, 0, MAXX*MAXY);
blueswir1 authored
437
438
    cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset +
                                    MAXX * MAXY * (1 + 4 + 4), VGA_DIRTY_FLAG);
bellard authored
439
440
441
442
443
444
445
446
447
448
449
450
451
    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
452
    switch (addr) {
bellard authored
453
    case 0:
blueswir1 authored
454
455
456
        s->dac_index = val >> 24;
        s->dac_state = 0;
        break;
blueswir1 authored
457
    case 4:
blueswir1 authored
458
459
460
        switch (s->dac_state) {
        case 0:
            s->r[s->dac_index] = val >> 24;
461
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
462
463
464
465
            s->dac_state++;
            break;
        case 1:
            s->g[s->dac_index] = val >> 24;
466
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
467
468
469
470
            s->dac_state++;
            break;
        case 2:
            s->b[s->dac_index] = val >> 24;
471
            update_palette_entries(s, s->dac_index, s->dac_index + 1);
blueswir1 authored
472
            s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
blueswir1 authored
473
474
475
476
477
        default:
            s->dac_state = 0;
            break;
        }
        break;
bellard authored
478
    default:
blueswir1 authored
479
        break;
bellard authored
480
481
    }
    return;
482
483
}
bellard authored
484
static CPUReadMemoryFunc *tcx_dac_read[3] = {
485
486
    NULL,
    NULL,
bellard authored
487
488
489
490
    tcx_dac_readl,
};

static CPUWriteMemoryFunc *tcx_dac_write[3] = {
491
492
    NULL,
    NULL,
bellard authored
493
494
495
    tcx_dac_writel,
};
496
497
498
499
500
501
502
503
504
505
506
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] = {
507
508
    NULL,
    NULL,
509
510
511
512
    tcx_dummy_readl,
};

static CPUWriteMemoryFunc *tcx_dummy_write[3] = {
513
514
    NULL,
    NULL,
515
516
517
    tcx_dummy_writel,
};
518
void tcx_init(target_phys_addr_t addr, int vram_size, int width, int height,
blueswir1 authored
519
              int depth)
520
{
521
522
523
524
    DeviceState *dev;
    SysBusDevice *s;

    dev = qdev_create(NULL, "SUNW,tcx");
525
526
527
528
529
    qdev_prop_set_taddr(dev, "addr", addr);
    qdev_prop_set_uint32(dev, "vram_size", vram_size);
    qdev_prop_set_uint16(dev, "width", width);
    qdev_prop_set_uint16(dev, "height", height);
    qdev_prop_set_uint16(dev, "depth", depth);
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    qdev_init(dev);
    s = sysbus_from_qdev(dev);
    /* 8-bit plane */
    sysbus_mmio_map(s, 0, addr + 0x00800000ULL);
    /* DAC */
    sysbus_mmio_map(s, 1, addr + 0x00200000ULL);
    /* TEC (dummy) */
    sysbus_mmio_map(s, 2, addr + 0x00700000ULL);
    /* THC 24 bit: NetBSD writes here even with 8-bit display: dummy */
    sysbus_mmio_map(s, 3, addr + 0x00301000ULL);
    if (depth == 24) {
        /* 24-bit plane */
        sysbus_mmio_map(s, 4, addr + 0x02000000ULL);
        /* Control plane */
        sysbus_mmio_map(s, 5, addr + 0x0a000000ULL);
    } else {
        /* THC 8 bit (dummy) */
        sysbus_mmio_map(s, 4, addr + 0x00300000ULL);
    }
}

static void tcx_init1(SysBusDevice *dev)
{
    TCXState *s = FROM_SYSBUS(TCXState, dev);
554
    int io_memory, dummy_memory;
555
    ram_addr_t vram_offset;
556
    int size;
557
558
    uint8_t *vram_base;
559
    vram_offset = qemu_ram_alloc(s->vram_size * (1 + 4 + 4));
560
    vram_base = qemu_get_ram_ptr(vram_offset);
bellard authored
561
    s->vram_offset = vram_offset;
blueswir1 authored
562
563
    /* 8-bit plane */
blueswir1 authored
564
    s->vram = vram_base;
565
    size = s->vram_size;
566
    sysbus_init_mmio(dev, size, s->vram_offset);
blueswir1 authored
567
568
    vram_offset += size;
    vram_base += size;
bellard authored
569
570
    /* DAC */
571
    io_memory = cpu_register_io_memory(tcx_dac_read, tcx_dac_write, s);
572
    sysbus_init_mmio(dev, TCX_DAC_NREGS, io_memory);
blueswir1 authored
573
574
    /* TEC (dummy) */
575
    dummy_memory = cpu_register_io_memory(tcx_dummy_read, tcx_dummy_write,
576
                                          s);
577
578
579
580
581
582
    sysbus_init_mmio(dev, TCX_TEC_NREGS, dummy_memory);
    /* THC: NetBSD writes here even with 8-bit display: dummy */
    sysbus_init_mmio(dev, TCX_THC_NREGS_24, dummy_memory);

    if (s->depth == 24) {
        /* 24-bit plane */
583
        size = s->vram_size * 4;
blueswir1 authored
584
585
        s->vram24 = (uint32_t *)vram_base;
        s->vram24_offset = vram_offset;
586
        sysbus_init_mmio(dev, size, vram_offset);
blueswir1 authored
587
588
589
        vram_offset += size;
        vram_base += size;
590
        /* Control plane */
591
        size = s->vram_size * 4;
blueswir1 authored
592
593
        s->cplane = (uint32_t *)vram_base;
        s->cplane_offset = vram_offset;
594
595
        sysbus_init_mmio(dev, size, vram_offset);
596
597
598
        s->ds = graphic_console_init(tcx24_update_display,
                                     tcx24_invalidate_display,
                                     tcx24_screen_dump, NULL, s);
blueswir1 authored
599
    } else {
600
601
602
        /* THC 8 bit (dummy) */
        sysbus_init_mmio(dev, TCX_THC_NREGS_8, dummy_memory);
603
604
605
        s->ds = graphic_console_init(tcx_update_display,
                                     tcx_invalidate_display,
                                     tcx_screen_dump, NULL, s);
blueswir1 authored
606
    }
bellard authored
607
608
    register_savevm("tcx", -1, 4, tcx_save, tcx_load, s);
609
    qemu_register_reset(tcx_reset, s);
bellard authored
610
    tcx_reset(s);
611
    qemu_console_resize(s->ds, s->width, s->height);
612
613
}
614
static void tcx_screen_dump(void *opaque, const char *filename)
bellard authored
615
{
bellard authored
616
    TCXState *s = opaque;
bellard authored
617
    FILE *f;
bellard authored
618
    uint8_t *d, *d1, v;
bellard authored
619
620
621
622
    int y, x;

    f = fopen(filename, "wb");
    if (!f)
bellard authored
623
        return;
bellard authored
624
625
626
    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
627
        d = d1;
bellard authored
628
        for(x = 0; x < s->width; x++) {
bellard authored
629
            v = *d;
bellard authored
630
631
632
            fputc(s->r[v], f);
            fputc(s->g[v], f);
            fputc(s->b[v], f);
bellard authored
633
634
            d++;
        }
bellard authored
635
        d1 += MAXX;
bellard authored
636
637
638
639
640
    }
    fclose(f);
    return;
}
blueswir1 authored
641
642
643
644
645
646
647
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
648
blueswir1 authored
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
    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;
}
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
static SysBusDeviceInfo tcx_info = {
    .init = tcx_init1,
    .qdev.name  = "SUNW,tcx",
    .qdev.size  = sizeof(TCXState),
    .qdev.props = (Property[]) {
        {
            .name   = "addr",
            .info   = &qdev_prop_taddr,
            .offset = offsetof(TCXState, addr),
            .defval = (target_phys_addr_t[]) { -1 },
        },{
            .name   = "vram_size",
            .info   = &qdev_prop_hex32,
            .offset = offsetof(TCXState, vram_size),
            .defval = (uint32_t[]) { -1 },
        },{
            .name   = "width",
            .info   = &qdev_prop_uint16,
            .offset = offsetof(TCXState, width),
            .defval = (uint16_t[]) { -1 },
        },{
            .name   = "height",
            .info   = &qdev_prop_uint16,
            .offset = offsetof(TCXState, height),
            .defval = (uint16_t[]) { -1 },
        },{
            .name   = "depth",
            .info   = &qdev_prop_uint16,
            .offset = offsetof(TCXState, depth),
            .defval = (uint16_t[]) { -1 },
        },
        {/* end of list */}
    }
};
712
713
static void tcx_register_devices(void)
{
714
    sysbus_register_withprop(&tcx_info);
715
716
717
}

device_init(tcx_register_devices)