2 * QEMU TCX Frame buffer
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "pixel_ops.h"
32 #define TCX_DAC_NREGS 16
33 #define TCX_THC_NREGS_8 0x081c
34 #define TCX_THC_NREGS_24 0x1000
35 #define TCX_TEC_NREGS 0x1000
37 typedef struct TCXState
{
39 target_phys_addr_t addr
;
42 uint32_t *vram24
, *cplane
;
43 ram_addr_t vram_offset
, vram24_offset
, cplane_offset
;
44 uint16_t width
, height
, depth
;
45 uint8_t r
[256], g
[256], b
[256];
46 uint32_t palette
[256];
47 uint8_t dac_index
, dac_state
;
50 static void tcx_screen_dump(void *opaque
, const char *filename
);
51 static void tcx24_screen_dump(void *opaque
, const char *filename
);
53 static void tcx_set_dirty(TCXState
*s
)
57 for (i
= 0; i
< MAXX
* MAXY
; i
+= TARGET_PAGE_SIZE
) {
58 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
62 static void tcx24_set_dirty(TCXState
*s
)
66 for (i
= 0; i
< MAXX
* MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
67 cpu_physical_memory_set_dirty(s
->vram24_offset
+ i
);
68 cpu_physical_memory_set_dirty(s
->cplane_offset
+ i
);
72 static void update_palette_entries(TCXState
*s
, int start
, int end
)
75 for(i
= start
; i
< end
; i
++) {
76 switch(ds_get_bits_per_pixel(s
->ds
)) {
79 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
82 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
85 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
88 if (is_surface_bgr(s
->ds
->surface
))
89 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
91 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
102 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
103 const uint8_t *s
, int width
)
107 uint32_t *p
= (uint32_t *)d
;
109 for(x
= 0; x
< width
; x
++) {
111 *p
++ = s1
->palette
[val
];
115 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
116 const uint8_t *s
, int width
)
120 uint16_t *p
= (uint16_t *)d
;
122 for(x
= 0; x
< width
; x
++) {
124 *p
++ = s1
->palette
[val
];
128 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
129 const uint8_t *s
, int width
)
134 for(x
= 0; x
< width
; x
++) {
136 *d
++ = s1
->palette
[val
];
141 XXX Could be much more optimal:
142 * detect if line/page/whole screen is in 24 bit mode
143 * if destination is also BGR, use memcpy
145 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
146 const uint8_t *s
, int width
,
147 const uint32_t *cplane
,
152 uint32_t *p
= (uint32_t *)d
;
155 bgr
= is_surface_bgr(s1
->ds
->surface
);
156 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
157 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
158 // 24-bit direct, BGR order
165 dval
= rgb_to_pixel32bgr(r
, g
, b
);
167 dval
= rgb_to_pixel32(r
, g
, b
);
170 dval
= s1
->palette
[val
];
176 static inline int check_dirty(ram_addr_t page
, ram_addr_t page24
,
182 ret
= cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
183 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
184 ret
|= cpu_physical_memory_get_dirty(page24
+ off
, VGA_DIRTY_FLAG
);
185 ret
|= cpu_physical_memory_get_dirty(cpage
+ off
, VGA_DIRTY_FLAG
);
190 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
191 ram_addr_t page_max
, ram_addr_t page24
,
194 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
196 page_min
-= ts
->vram_offset
;
197 page_max
-= ts
->vram_offset
;
198 cpu_physical_memory_reset_dirty(page24
+ page_min
* 4,
199 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
201 cpu_physical_memory_reset_dirty(cpage
+ page_min
* 4,
202 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
206 /* Fixed line length 1024 allows us to do nice tricks not possible on
208 static void tcx_update_display(void *opaque
)
210 TCXState
*ts
= opaque
;
211 ram_addr_t page
, page_min
, page_max
;
212 int y
, y_start
, dd
, ds
;
214 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
216 if (ds_get_bits_per_pixel(ts
->ds
) == 0)
218 page
= ts
->vram_offset
;
222 d
= ds_get_data(ts
->ds
);
224 dd
= ds_get_linesize(ts
->ds
);
227 switch (ds_get_bits_per_pixel(ts
->ds
)) {
243 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
244 if (cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
)) {
251 f(ts
, d
, s
, ts
->width
);
254 f(ts
, d
, s
, ts
->width
);
257 f(ts
, d
, s
, ts
->width
);
260 f(ts
, d
, s
, ts
->width
);
265 /* flush to display */
266 dpy_update(ts
->ds
, 0, y_start
,
267 ts
->width
, y
- y_start
);
275 /* flush to display */
276 dpy_update(ts
->ds
, 0, y_start
,
277 ts
->width
, y
- y_start
);
279 /* reset modified pages */
280 if (page_max
>= page_min
) {
281 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
286 static void tcx24_update_display(void *opaque
)
288 TCXState
*ts
= opaque
;
289 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
290 int y
, y_start
, dd
, ds
;
292 uint32_t *cptr
, *s24
;
294 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
296 page
= ts
->vram_offset
;
297 page24
= ts
->vram24_offset
;
298 cpage
= ts
->cplane_offset
;
302 d
= ds_get_data(ts
->ds
);
306 dd
= ds_get_linesize(ts
->ds
);
309 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
310 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
311 if (check_dirty(page
, page24
, cpage
)) {
318 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
323 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
328 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
333 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
340 /* flush to display */
341 dpy_update(ts
->ds
, 0, y_start
,
342 ts
->width
, y
- y_start
);
352 /* flush to display */
353 dpy_update(ts
->ds
, 0, y_start
,
354 ts
->width
, y
- y_start
);
356 /* reset modified pages */
357 if (page_max
>= page_min
) {
358 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
362 static void tcx_invalidate_display(void *opaque
)
364 TCXState
*s
= opaque
;
367 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
370 static void tcx24_invalidate_display(void *opaque
)
372 TCXState
*s
= opaque
;
376 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
379 static void tcx_save(QEMUFile
*f
, void *opaque
)
381 TCXState
*s
= opaque
;
383 qemu_put_be16s(f
, &s
->height
);
384 qemu_put_be16s(f
, &s
->width
);
385 qemu_put_be16s(f
, &s
->depth
);
386 qemu_put_buffer(f
, s
->r
, 256);
387 qemu_put_buffer(f
, s
->g
, 256);
388 qemu_put_buffer(f
, s
->b
, 256);
389 qemu_put_8s(f
, &s
->dac_index
);
390 qemu_put_8s(f
, &s
->dac_state
);
393 static int tcx_load(QEMUFile
*f
, void *opaque
, int version_id
)
395 TCXState
*s
= opaque
;
398 if (version_id
!= 3 && version_id
!= 4)
401 if (version_id
== 3) {
402 qemu_get_be32s(f
, &dummy
);
403 qemu_get_be32s(f
, &dummy
);
404 qemu_get_be32s(f
, &dummy
);
406 qemu_get_be16s(f
, &s
->height
);
407 qemu_get_be16s(f
, &s
->width
);
408 qemu_get_be16s(f
, &s
->depth
);
409 qemu_get_buffer(f
, s
->r
, 256);
410 qemu_get_buffer(f
, s
->g
, 256);
411 qemu_get_buffer(f
, s
->b
, 256);
412 qemu_get_8s(f
, &s
->dac_index
);
413 qemu_get_8s(f
, &s
->dac_state
);
414 update_palette_entries(s
, 0, 256);
415 if (s
->depth
== 24) {
424 static void tcx_reset(void *opaque
)
426 TCXState
*s
= opaque
;
428 /* Initialize palette */
429 memset(s
->r
, 0, 256);
430 memset(s
->g
, 0, 256);
431 memset(s
->b
, 0, 256);
432 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
433 update_palette_entries(s
, 0, 256);
434 memset(s
->vram
, 0, MAXX
*MAXY
);
435 cpu_physical_memory_reset_dirty(s
->vram_offset
, s
->vram_offset
+
436 MAXX
* MAXY
* (1 + 4 + 4), VGA_DIRTY_FLAG
);
441 static uint32_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
)
446 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
448 TCXState
*s
= opaque
;
452 s
->dac_index
= val
>> 24;
456 switch (s
->dac_state
) {
458 s
->r
[s
->dac_index
] = val
>> 24;
459 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
463 s
->g
[s
->dac_index
] = val
>> 24;
464 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
468 s
->b
[s
->dac_index
] = val
>> 24;
469 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
470 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
482 static CPUReadMemoryFunc
*tcx_dac_read
[3] = {
488 static CPUWriteMemoryFunc
*tcx_dac_write
[3] = {
494 static uint32_t tcx_dummy_readl(void *opaque
, target_phys_addr_t addr
)
499 static void tcx_dummy_writel(void *opaque
, target_phys_addr_t addr
,
504 static CPUReadMemoryFunc
*tcx_dummy_read
[3] = {
510 static CPUWriteMemoryFunc
*tcx_dummy_write
[3] = {
516 void tcx_init(target_phys_addr_t addr
, int vram_size
, int width
, int height
,
522 dev
= qdev_create(NULL
, "SUNW,tcx");
523 qdev_set_prop_int(dev
, "addr", addr
);
524 qdev_set_prop_int(dev
, "vram_size", vram_size
);
525 qdev_set_prop_int(dev
, "width", width
);
526 qdev_set_prop_int(dev
, "height", height
);
527 qdev_set_prop_int(dev
, "depth", depth
);
529 s
= sysbus_from_qdev(dev
);
531 sysbus_mmio_map(s
, 0, addr
+ 0x00800000ULL
);
533 sysbus_mmio_map(s
, 1, addr
+ 0x00200000ULL
);
535 sysbus_mmio_map(s
, 2, addr
+ 0x00700000ULL
);
536 /* THC 24 bit: NetBSD writes here even with 8-bit display: dummy */
537 sysbus_mmio_map(s
, 3, addr
+ 0x00301000ULL
);
540 sysbus_mmio_map(s
, 4, addr
+ 0x02000000ULL
);
542 sysbus_mmio_map(s
, 5, addr
+ 0x0a000000ULL
);
544 /* THC 8 bit (dummy) */
545 sysbus_mmio_map(s
, 4, addr
+ 0x00300000ULL
);
549 static void tcx_init1(SysBusDevice
*dev
)
551 TCXState
*s
= FROM_SYSBUS(TCXState
, dev
);
552 int io_memory
, dummy_memory
;
553 ram_addr_t vram_offset
;
557 vram_size
= qdev_get_prop_int(&dev
->qdev
, "vram_size", -1);
559 vram_offset
= qemu_ram_alloc(vram_size
* (1 + 4 + 4));
560 vram_base
= qemu_get_ram_ptr(vram_offset
);
561 s
->addr
= qdev_get_prop_int(&dev
->qdev
, "addr", -1);
562 s
->vram_offset
= vram_offset
;
563 s
->width
= qdev_get_prop_int(&dev
->qdev
, "width", -1);
564 s
->height
= qdev_get_prop_int(&dev
->qdev
, "height", -1);
565 s
->depth
= qdev_get_prop_int(&dev
->qdev
, "depth", -1);
570 sysbus_init_mmio(dev
, size
, s
->vram_offset
);
575 io_memory
= cpu_register_io_memory(tcx_dac_read
, tcx_dac_write
, s
);
576 sysbus_init_mmio(dev
, TCX_DAC_NREGS
, io_memory
);
579 dummy_memory
= cpu_register_io_memory(tcx_dummy_read
, tcx_dummy_write
,
581 sysbus_init_mmio(dev
, TCX_TEC_NREGS
, dummy_memory
);
582 /* THC: NetBSD writes here even with 8-bit display: dummy */
583 sysbus_init_mmio(dev
, TCX_THC_NREGS_24
, dummy_memory
);
585 if (s
->depth
== 24) {
587 size
= vram_size
* 4;
588 s
->vram24
= (uint32_t *)vram_base
;
589 s
->vram24_offset
= vram_offset
;
590 sysbus_init_mmio(dev
, size
, vram_offset
);
595 size
= vram_size
* 4;
596 s
->cplane
= (uint32_t *)vram_base
;
597 s
->cplane_offset
= vram_offset
;
598 sysbus_init_mmio(dev
, size
, vram_offset
);
600 s
->ds
= graphic_console_init(tcx24_update_display
,
601 tcx24_invalidate_display
,
602 tcx24_screen_dump
, NULL
, s
);
604 /* THC 8 bit (dummy) */
605 sysbus_init_mmio(dev
, TCX_THC_NREGS_8
, dummy_memory
);
607 s
->ds
= graphic_console_init(tcx_update_display
,
608 tcx_invalidate_display
,
609 tcx_screen_dump
, NULL
, s
);
612 register_savevm("tcx", -1, 4, tcx_save
, tcx_load
, s
);
613 qemu_register_reset(tcx_reset
, s
);
615 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
618 static void tcx_screen_dump(void *opaque
, const char *filename
)
620 TCXState
*s
= opaque
;
625 f
= fopen(filename
, "wb");
628 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
630 for(y
= 0; y
< s
->height
; y
++) {
632 for(x
= 0; x
< s
->width
; x
++) {
645 static void tcx24_screen_dump(void *opaque
, const char *filename
)
647 TCXState
*s
= opaque
;
650 uint32_t *s24
, *cptr
, dval
;
653 f
= fopen(filename
, "wb");
656 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
660 for(y
= 0; y
< s
->height
; y
++) {
662 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
663 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
664 dval
= *s24
& 0x00ffffff;
665 fputc((dval
>> 16) & 0xff, f
);
666 fputc((dval
>> 8) & 0xff, f
);
667 fputc(dval
& 0xff, f
);
681 static void tcx_register_devices(void)
683 sysbus_register_dev("SUNW,tcx", sizeof(TCXState
), tcx_init1
);
686 device_init(tcx_register_devices
)