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
26 #include "pixel_ops.h"
28 #include "qdev-addr.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 MemoryRegion vram_mem
;
44 MemoryRegion vram_8bit
;
45 MemoryRegion vram_24bit
;
46 MemoryRegion vram_cplane
;
51 ram_addr_t vram24_offset
, cplane_offset
;
53 uint32_t palette
[256];
54 uint8_t r
[256], g
[256], b
[256];
55 uint16_t width
, height
, depth
;
56 uint8_t dac_index
, dac_state
;
59 static void tcx_screen_dump(void *opaque
, const char *filename
);
60 static void tcx24_screen_dump(void *opaque
, const char *filename
);
62 static void tcx_set_dirty(TCXState
*s
)
66 for (i
= 0; i
< MAXX
* MAXY
; i
+= TARGET_PAGE_SIZE
) {
67 memory_region_set_dirty(&s
->vram_mem
, i
);
71 static void tcx24_set_dirty(TCXState
*s
)
75 for (i
= 0; i
< MAXX
* MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
76 memory_region_set_dirty(&s
->vram_mem
, s
->vram24_offset
+ i
);
77 memory_region_set_dirty(&s
->vram_mem
, s
->cplane_offset
+ i
);
81 static void update_palette_entries(TCXState
*s
, int start
, int end
)
84 for(i
= start
; i
< end
; i
++) {
85 switch(ds_get_bits_per_pixel(s
->ds
)) {
88 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
91 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
94 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
97 if (is_surface_bgr(s
->ds
->surface
))
98 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
100 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
104 if (s
->depth
== 24) {
111 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
112 const uint8_t *s
, int width
)
116 uint32_t *p
= (uint32_t *)d
;
118 for(x
= 0; x
< width
; x
++) {
120 *p
++ = s1
->palette
[val
];
124 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
125 const uint8_t *s
, int width
)
129 uint16_t *p
= (uint16_t *)d
;
131 for(x
= 0; x
< width
; x
++) {
133 *p
++ = s1
->palette
[val
];
137 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
138 const uint8_t *s
, int width
)
143 for(x
= 0; x
< width
; x
++) {
145 *d
++ = s1
->palette
[val
];
150 XXX Could be much more optimal:
151 * detect if line/page/whole screen is in 24 bit mode
152 * if destination is also BGR, use memcpy
154 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
155 const uint8_t *s
, int width
,
156 const uint32_t *cplane
,
161 uint32_t *p
= (uint32_t *)d
;
164 bgr
= is_surface_bgr(s1
->ds
->surface
);
165 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
166 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
167 // 24-bit direct, BGR order
174 dval
= rgb_to_pixel32bgr(r
, g
, b
);
176 dval
= rgb_to_pixel32(r
, g
, b
);
179 dval
= s1
->palette
[val
];
185 static inline int check_dirty(TCXState
*s
, ram_addr_t page
, ram_addr_t page24
,
191 ret
= memory_region_get_dirty(&s
->vram_mem
, page
, DIRTY_MEMORY_VGA
);
192 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
193 ret
|= memory_region_get_dirty(&s
->vram_mem
, page24
+ off
,
195 ret
|= memory_region_get_dirty(&s
->vram_mem
, cpage
+ off
,
201 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
202 ram_addr_t page_max
, ram_addr_t page24
,
205 memory_region_reset_dirty(&ts
->vram_mem
,
206 page_min
, page_max
+ TARGET_PAGE_SIZE
,
208 memory_region_reset_dirty(&ts
->vram_mem
,
209 page24
+ page_min
* 4,
210 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
212 memory_region_reset_dirty(&ts
->vram_mem
,
213 cpage
+ page_min
* 4,
214 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
218 /* Fixed line length 1024 allows us to do nice tricks not possible on
220 static void tcx_update_display(void *opaque
)
222 TCXState
*ts
= opaque
;
223 ram_addr_t page
, page_min
, page_max
;
224 int y
, y_start
, dd
, ds
;
226 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
228 if (ds_get_bits_per_pixel(ts
->ds
) == 0)
234 d
= ds_get_data(ts
->ds
);
236 dd
= ds_get_linesize(ts
->ds
);
239 switch (ds_get_bits_per_pixel(ts
->ds
)) {
255 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
256 if (memory_region_get_dirty(&ts
->vram_mem
, page
, DIRTY_MEMORY_VGA
)) {
263 f(ts
, d
, s
, ts
->width
);
266 f(ts
, d
, s
, ts
->width
);
269 f(ts
, d
, s
, ts
->width
);
272 f(ts
, d
, s
, ts
->width
);
277 /* flush to display */
278 dpy_update(ts
->ds
, 0, y_start
,
279 ts
->width
, y
- y_start
);
287 /* flush to display */
288 dpy_update(ts
->ds
, 0, y_start
,
289 ts
->width
, y
- y_start
);
291 /* reset modified pages */
292 if (page_max
>= page_min
) {
293 memory_region_reset_dirty(&ts
->vram_mem
,
294 page_min
, page_max
+ TARGET_PAGE_SIZE
,
299 static void tcx24_update_display(void *opaque
)
301 TCXState
*ts
= opaque
;
302 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
303 int y
, y_start
, dd
, ds
;
305 uint32_t *cptr
, *s24
;
307 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
310 page24
= ts
->vram24_offset
;
311 cpage
= ts
->cplane_offset
;
315 d
= ds_get_data(ts
->ds
);
319 dd
= ds_get_linesize(ts
->ds
);
322 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
323 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
324 if (check_dirty(ts
, page
, page24
, cpage
)) {
331 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
336 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
341 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
346 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
353 /* flush to display */
354 dpy_update(ts
->ds
, 0, y_start
,
355 ts
->width
, y
- y_start
);
365 /* flush to display */
366 dpy_update(ts
->ds
, 0, y_start
,
367 ts
->width
, y
- y_start
);
369 /* reset modified pages */
370 if (page_max
>= page_min
) {
371 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
375 static void tcx_invalidate_display(void *opaque
)
377 TCXState
*s
= opaque
;
380 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
383 static void tcx24_invalidate_display(void *opaque
)
385 TCXState
*s
= opaque
;
389 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
392 static int vmstate_tcx_post_load(void *opaque
, int version_id
)
394 TCXState
*s
= opaque
;
396 update_palette_entries(s
, 0, 256);
397 if (s
->depth
== 24) {
406 static const VMStateDescription vmstate_tcx
= {
409 .minimum_version_id
= 4,
410 .minimum_version_id_old
= 4,
411 .post_load
= vmstate_tcx_post_load
,
412 .fields
= (VMStateField
[]) {
413 VMSTATE_UINT16(height
, TCXState
),
414 VMSTATE_UINT16(width
, TCXState
),
415 VMSTATE_UINT16(depth
, TCXState
),
416 VMSTATE_BUFFER(r
, TCXState
),
417 VMSTATE_BUFFER(g
, TCXState
),
418 VMSTATE_BUFFER(b
, TCXState
),
419 VMSTATE_UINT8(dac_index
, TCXState
),
420 VMSTATE_UINT8(dac_state
, TCXState
),
421 VMSTATE_END_OF_LIST()
425 static void tcx_reset(DeviceState
*d
)
427 TCXState
*s
= container_of(d
, TCXState
, busdev
.qdev
);
429 /* Initialize palette */
430 memset(s
->r
, 0, 256);
431 memset(s
->g
, 0, 256);
432 memset(s
->b
, 0, 256);
433 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
434 update_palette_entries(s
, 0, 256);
435 memset(s
->vram
, 0, MAXX
*MAXY
);
436 memory_region_reset_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
* (1 + 4 + 4),
442 static uint64_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
,
448 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint64_t val
,
451 TCXState
*s
= opaque
;
455 s
->dac_index
= val
>> 24;
459 switch (s
->dac_state
) {
461 s
->r
[s
->dac_index
] = val
>> 24;
462 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
466 s
->g
[s
->dac_index
] = val
>> 24;
467 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
471 s
->b
[s
->dac_index
] = val
>> 24;
472 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
473 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
485 static const MemoryRegionOps tcx_dac_ops
= {
486 .read
= tcx_dac_readl
,
487 .write
= tcx_dac_writel
,
488 .endianness
= DEVICE_NATIVE_ENDIAN
,
490 .min_access_size
= 4,
491 .max_access_size
= 4,
495 static uint64_t dummy_readl(void *opaque
, target_phys_addr_t addr
,
501 static void dummy_writel(void *opaque
, target_phys_addr_t addr
,
502 uint64_t val
, unsigned size
)
506 static const MemoryRegionOps dummy_ops
= {
508 .write
= dummy_writel
,
509 .endianness
= DEVICE_NATIVE_ENDIAN
,
511 .min_access_size
= 4,
512 .max_access_size
= 4,
516 static int tcx_init1(SysBusDevice
*dev
)
518 TCXState
*s
= FROM_SYSBUS(TCXState
, dev
);
519 ram_addr_t vram_offset
= 0;
523 memory_region_init_ram(&s
->vram_mem
, NULL
, "tcx.vram",
524 s
->vram_size
* (1 + 4 + 4));
525 vram_base
= memory_region_get_ram_ptr(&s
->vram_mem
);
530 memory_region_init_alias(&s
->vram_8bit
, "tcx.vram.8bit",
531 &s
->vram_mem
, vram_offset
, size
);
532 sysbus_init_mmio(dev
, &s
->vram_8bit
);
537 memory_region_init_io(&s
->dac
, &tcx_dac_ops
, s
, "tcx.dac", TCX_DAC_NREGS
);
538 sysbus_init_mmio(dev
, &s
->dac
);
541 memory_region_init_io(&s
->tec
, &dummy_ops
, s
, "tcx.tec", TCX_TEC_NREGS
);
542 sysbus_init_mmio(dev
, &s
->tec
);
543 /* THC: NetBSD writes here even with 8-bit display: dummy */
544 memory_region_init_io(&s
->thc24
, &dummy_ops
, s
, "tcx.thc24",
546 sysbus_init_mmio(dev
, &s
->thc24
);
548 if (s
->depth
== 24) {
550 size
= s
->vram_size
* 4;
551 s
->vram24
= (uint32_t *)vram_base
;
552 s
->vram24_offset
= vram_offset
;
553 memory_region_init_alias(&s
->vram_24bit
, "tcx.vram.24bit",
554 &s
->vram_mem
, vram_offset
, size
);
555 sysbus_init_mmio(dev
, &s
->vram_24bit
);
560 size
= s
->vram_size
* 4;
561 s
->cplane
= (uint32_t *)vram_base
;
562 s
->cplane_offset
= vram_offset
;
563 memory_region_init_alias(&s
->vram_cplane
, "tcx.vram.cplane",
564 &s
->vram_mem
, vram_offset
, size
);
565 sysbus_init_mmio(dev
, &s
->vram_cplane
);
567 s
->ds
= graphic_console_init(tcx24_update_display
,
568 tcx24_invalidate_display
,
569 tcx24_screen_dump
, NULL
, s
);
571 /* THC 8 bit (dummy) */
572 memory_region_init_io(&s
->thc8
, &dummy_ops
, s
, "tcx.thc8",
574 sysbus_init_mmio(dev
, &s
->thc8
);
576 s
->ds
= graphic_console_init(tcx_update_display
,
577 tcx_invalidate_display
,
578 tcx_screen_dump
, NULL
, s
);
581 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
585 static void tcx_screen_dump(void *opaque
, const char *filename
)
587 TCXState
*s
= opaque
;
592 f
= fopen(filename
, "wb");
595 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
597 for(y
= 0; y
< s
->height
; y
++) {
599 for(x
= 0; x
< s
->width
; x
++) {
612 static void tcx24_screen_dump(void *opaque
, const char *filename
)
614 TCXState
*s
= opaque
;
617 uint32_t *s24
, *cptr
, dval
;
620 f
= fopen(filename
, "wb");
623 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
627 for(y
= 0; y
< s
->height
; y
++) {
629 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
630 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
631 dval
= *s24
& 0x00ffffff;
632 fputc((dval
>> 16) & 0xff, f
);
633 fputc((dval
>> 8) & 0xff, f
);
634 fputc(dval
& 0xff, f
);
648 static SysBusDeviceInfo tcx_info
= {
650 .qdev
.name
= "SUNW,tcx",
651 .qdev
.size
= sizeof(TCXState
),
652 .qdev
.reset
= tcx_reset
,
653 .qdev
.vmsd
= &vmstate_tcx
,
654 .qdev
.props
= (Property
[]) {
655 DEFINE_PROP_TADDR("addr", TCXState
, addr
, -1),
656 DEFINE_PROP_HEX32("vram_size", TCXState
, vram_size
, -1),
657 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
658 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
659 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
660 DEFINE_PROP_END_OF_LIST(),
664 static void tcx_register_devices(void)
666 sysbus_register_withprop(&tcx_info
);
669 device_init(tcx_register_devices
)