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
, "tcx.vram",
524 s
->vram_size
* (1 + 4 + 4));
525 vmstate_register_ram_global(&s
->vram_mem
);
526 vram_base
= memory_region_get_ram_ptr(&s
->vram_mem
);
531 memory_region_init_alias(&s
->vram_8bit
, "tcx.vram.8bit",
532 &s
->vram_mem
, vram_offset
, size
);
533 sysbus_init_mmio(dev
, &s
->vram_8bit
);
538 memory_region_init_io(&s
->dac
, &tcx_dac_ops
, s
, "tcx.dac", TCX_DAC_NREGS
);
539 sysbus_init_mmio(dev
, &s
->dac
);
542 memory_region_init_io(&s
->tec
, &dummy_ops
, s
, "tcx.tec", TCX_TEC_NREGS
);
543 sysbus_init_mmio(dev
, &s
->tec
);
544 /* THC: NetBSD writes here even with 8-bit display: dummy */
545 memory_region_init_io(&s
->thc24
, &dummy_ops
, s
, "tcx.thc24",
547 sysbus_init_mmio(dev
, &s
->thc24
);
549 if (s
->depth
== 24) {
551 size
= s
->vram_size
* 4;
552 s
->vram24
= (uint32_t *)vram_base
;
553 s
->vram24_offset
= vram_offset
;
554 memory_region_init_alias(&s
->vram_24bit
, "tcx.vram.24bit",
555 &s
->vram_mem
, vram_offset
, size
);
556 sysbus_init_mmio(dev
, &s
->vram_24bit
);
561 size
= s
->vram_size
* 4;
562 s
->cplane
= (uint32_t *)vram_base
;
563 s
->cplane_offset
= vram_offset
;
564 memory_region_init_alias(&s
->vram_cplane
, "tcx.vram.cplane",
565 &s
->vram_mem
, vram_offset
, size
);
566 sysbus_init_mmio(dev
, &s
->vram_cplane
);
568 s
->ds
= graphic_console_init(tcx24_update_display
,
569 tcx24_invalidate_display
,
570 tcx24_screen_dump
, NULL
, s
);
572 /* THC 8 bit (dummy) */
573 memory_region_init_io(&s
->thc8
, &dummy_ops
, s
, "tcx.thc8",
575 sysbus_init_mmio(dev
, &s
->thc8
);
577 s
->ds
= graphic_console_init(tcx_update_display
,
578 tcx_invalidate_display
,
579 tcx_screen_dump
, NULL
, s
);
582 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
586 static void tcx_screen_dump(void *opaque
, const char *filename
)
588 TCXState
*s
= opaque
;
593 f
= fopen(filename
, "wb");
596 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
598 for(y
= 0; y
< s
->height
; y
++) {
600 for(x
= 0; x
< s
->width
; x
++) {
613 static void tcx24_screen_dump(void *opaque
, const char *filename
)
615 TCXState
*s
= opaque
;
618 uint32_t *s24
, *cptr
, dval
;
621 f
= fopen(filename
, "wb");
624 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
628 for(y
= 0; y
< s
->height
; y
++) {
630 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
631 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
632 dval
= *s24
& 0x00ffffff;
633 fputc((dval
>> 16) & 0xff, f
);
634 fputc((dval
>> 8) & 0xff, f
);
635 fputc(dval
& 0xff, f
);
649 static SysBusDeviceInfo tcx_info
= {
651 .qdev
.name
= "SUNW,tcx",
652 .qdev
.size
= sizeof(TCXState
),
653 .qdev
.reset
= tcx_reset
,
654 .qdev
.vmsd
= &vmstate_tcx
,
655 .qdev
.props
= (Property
[]) {
656 DEFINE_PROP_TADDR("addr", TCXState
, addr
, -1),
657 DEFINE_PROP_HEX32("vram_size", TCXState
, vram_size
, -1),
658 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
659 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
660 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
661 DEFINE_PROP_END_OF_LIST(),
665 static void tcx_register_devices(void)
667 sysbus_register_withprop(&tcx_info
);
670 device_init(tcx_register_devices
)