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
25 #include "qemu-common.h"
26 #include "ui/console.h"
27 #include "ui/pixel_ops.h"
28 #include "hw/loader.h"
29 #include "hw/sysbus.h"
31 #define TCX_ROM_FILE "QEMU,tcx.bin"
32 #define FCODE_MAX_ROM_SIZE 0x10000
36 #define TCX_DAC_NREGS 16
37 #define TCX_THC_NREGS_8 0x081c
38 #define TCX_THC_NREGS_24 0x1000
39 #define TCX_TEC_NREGS 0x1000
41 #define TYPE_TCX "SUNW,tcx"
42 #define TCX(obj) OBJECT_CHECK(TCXState, (obj), TYPE_TCX)
44 typedef struct TCXState
{
45 SysBusDevice parent_obj
;
49 uint32_t *vram24
, *cplane
;
52 MemoryRegion vram_mem
;
53 MemoryRegion vram_8bit
;
54 MemoryRegion vram_24bit
;
55 MemoryRegion vram_cplane
;
60 ram_addr_t vram24_offset
, cplane_offset
;
62 uint32_t palette
[256];
63 uint8_t r
[256], g
[256], b
[256];
64 uint16_t width
, height
, depth
;
65 uint8_t dac_index
, dac_state
;
68 static void tcx_set_dirty(TCXState
*s
)
70 memory_region_set_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
);
73 static void tcx24_set_dirty(TCXState
*s
)
75 memory_region_set_dirty(&s
->vram_mem
, s
->vram24_offset
, MAXX
* MAXY
* 4);
76 memory_region_set_dirty(&s
->vram_mem
, s
->cplane_offset
, MAXX
* MAXY
* 4);
79 static void update_palette_entries(TCXState
*s
, int start
, int end
)
81 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
84 for (i
= start
; i
< end
; i
++) {
85 switch (surface_bits_per_pixel(surface
)) {
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(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
]);
105 if (s
->depth
== 24) {
112 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
113 const uint8_t *s
, int width
)
117 uint32_t *p
= (uint32_t *)d
;
119 for(x
= 0; x
< width
; x
++) {
121 *p
++ = s1
->palette
[val
];
125 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
126 const uint8_t *s
, int width
)
130 uint16_t *p
= (uint16_t *)d
;
132 for(x
= 0; x
< width
; x
++) {
134 *p
++ = s1
->palette
[val
];
138 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
139 const uint8_t *s
, int width
)
144 for(x
= 0; x
< width
; x
++) {
146 *d
++ = s1
->palette
[val
];
151 XXX Could be much more optimal:
152 * detect if line/page/whole screen is in 24 bit mode
153 * if destination is also BGR, use memcpy
155 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
156 const uint8_t *s
, int width
,
157 const uint32_t *cplane
,
160 DisplaySurface
*surface
= qemu_console_surface(s1
->con
);
163 uint32_t *p
= (uint32_t *)d
;
166 bgr
= is_surface_bgr(surface
);
167 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
168 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
169 // 24-bit direct, BGR order
176 dval
= rgb_to_pixel32bgr(r
, g
, b
);
178 dval
= rgb_to_pixel32(r
, g
, b
);
181 dval
= s1
->palette
[val
];
187 static inline int check_dirty(TCXState
*s
, ram_addr_t page
, ram_addr_t page24
,
192 ret
= memory_region_get_dirty(&s
->vram_mem
, page
, TARGET_PAGE_SIZE
,
194 ret
|= memory_region_get_dirty(&s
->vram_mem
, page24
, TARGET_PAGE_SIZE
* 4,
196 ret
|= memory_region_get_dirty(&s
->vram_mem
, cpage
, TARGET_PAGE_SIZE
* 4,
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
,
207 (page_max
- page_min
) + TARGET_PAGE_SIZE
,
209 memory_region_reset_dirty(&ts
->vram_mem
,
210 page24
+ page_min
* 4,
211 (page_max
- page_min
) * 4 + TARGET_PAGE_SIZE
,
213 memory_region_reset_dirty(&ts
->vram_mem
,
214 cpage
+ page_min
* 4,
215 (page_max
- page_min
) * 4 + TARGET_PAGE_SIZE
,
219 /* Fixed line length 1024 allows us to do nice tricks not possible on
221 static void tcx_update_display(void *opaque
)
223 TCXState
*ts
= opaque
;
224 DisplaySurface
*surface
= qemu_console_surface(ts
->con
);
225 ram_addr_t page
, page_min
, page_max
;
226 int y
, y_start
, dd
, ds
;
228 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
230 if (surface_bits_per_pixel(surface
) == 0) {
238 d
= surface_data(surface
);
240 dd
= surface_stride(surface
);
243 switch (surface_bits_per_pixel(surface
)) {
259 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
260 if (memory_region_get_dirty(&ts
->vram_mem
, page
, TARGET_PAGE_SIZE
,
268 f(ts
, d
, s
, ts
->width
);
271 f(ts
, d
, s
, ts
->width
);
274 f(ts
, d
, s
, ts
->width
);
277 f(ts
, d
, s
, ts
->width
);
282 /* flush to display */
283 dpy_gfx_update(ts
->con
, 0, y_start
,
284 ts
->width
, y
- y_start
);
292 /* flush to display */
293 dpy_gfx_update(ts
->con
, 0, y_start
,
294 ts
->width
, y
- y_start
);
296 /* reset modified pages */
297 if (page_max
>= page_min
) {
298 memory_region_reset_dirty(&ts
->vram_mem
,
300 (page_max
- page_min
) + TARGET_PAGE_SIZE
,
305 static void tcx24_update_display(void *opaque
)
307 TCXState
*ts
= opaque
;
308 DisplaySurface
*surface
= qemu_console_surface(ts
->con
);
309 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
310 int y
, y_start
, dd
, ds
;
312 uint32_t *cptr
, *s24
;
314 if (surface_bits_per_pixel(surface
) != 32) {
319 page24
= ts
->vram24_offset
;
320 cpage
= ts
->cplane_offset
;
324 d
= surface_data(surface
);
328 dd
= surface_stride(surface
);
331 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
332 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
333 if (check_dirty(ts
, page
, page24
, cpage
)) {
340 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
345 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
350 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
355 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
362 /* flush to display */
363 dpy_gfx_update(ts
->con
, 0, y_start
,
364 ts
->width
, y
- y_start
);
374 /* flush to display */
375 dpy_gfx_update(ts
->con
, 0, y_start
,
376 ts
->width
, y
- y_start
);
378 /* reset modified pages */
379 if (page_max
>= page_min
) {
380 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
384 static void tcx_invalidate_display(void *opaque
)
386 TCXState
*s
= opaque
;
389 qemu_console_resize(s
->con
, s
->width
, s
->height
);
392 static void tcx24_invalidate_display(void *opaque
)
394 TCXState
*s
= opaque
;
398 qemu_console_resize(s
->con
, s
->width
, s
->height
);
401 static int vmstate_tcx_post_load(void *opaque
, int version_id
)
403 TCXState
*s
= opaque
;
405 update_palette_entries(s
, 0, 256);
406 if (s
->depth
== 24) {
415 static const VMStateDescription vmstate_tcx
= {
418 .minimum_version_id
= 4,
419 .post_load
= vmstate_tcx_post_load
,
420 .fields
= (VMStateField
[]) {
421 VMSTATE_UINT16(height
, TCXState
),
422 VMSTATE_UINT16(width
, TCXState
),
423 VMSTATE_UINT16(depth
, TCXState
),
424 VMSTATE_BUFFER(r
, TCXState
),
425 VMSTATE_BUFFER(g
, TCXState
),
426 VMSTATE_BUFFER(b
, TCXState
),
427 VMSTATE_UINT8(dac_index
, TCXState
),
428 VMSTATE_UINT8(dac_state
, TCXState
),
429 VMSTATE_END_OF_LIST()
433 static void tcx_reset(DeviceState
*d
)
435 TCXState
*s
= TCX(d
);
437 /* Initialize palette */
438 memset(s
->r
, 0, 256);
439 memset(s
->g
, 0, 256);
440 memset(s
->b
, 0, 256);
441 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
442 update_palette_entries(s
, 0, 256);
443 memset(s
->vram
, 0, MAXX
*MAXY
);
444 memory_region_reset_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
* (1 + 4 + 4),
450 static uint64_t tcx_dac_readl(void *opaque
, hwaddr addr
,
456 static void tcx_dac_writel(void *opaque
, hwaddr addr
, uint64_t val
,
459 TCXState
*s
= opaque
;
463 s
->dac_index
= val
>> 24;
467 switch (s
->dac_state
) {
469 s
->r
[s
->dac_index
] = val
>> 24;
470 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
474 s
->g
[s
->dac_index
] = val
>> 24;
475 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
479 s
->b
[s
->dac_index
] = val
>> 24;
480 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
481 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
492 static const MemoryRegionOps tcx_dac_ops
= {
493 .read
= tcx_dac_readl
,
494 .write
= tcx_dac_writel
,
495 .endianness
= DEVICE_NATIVE_ENDIAN
,
497 .min_access_size
= 4,
498 .max_access_size
= 4,
502 static uint64_t dummy_readl(void *opaque
, hwaddr addr
,
508 static void dummy_writel(void *opaque
, hwaddr addr
,
509 uint64_t val
, unsigned size
)
513 static const MemoryRegionOps dummy_ops
= {
515 .write
= dummy_writel
,
516 .endianness
= DEVICE_NATIVE_ENDIAN
,
518 .min_access_size
= 4,
519 .max_access_size
= 4,
523 static const GraphicHwOps tcx_ops
= {
524 .invalidate
= tcx_invalidate_display
,
525 .gfx_update
= tcx_update_display
,
528 static const GraphicHwOps tcx24_ops
= {
529 .invalidate
= tcx24_invalidate_display
,
530 .gfx_update
= tcx24_update_display
,
533 static void tcx_initfn(Object
*obj
)
535 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
536 TCXState
*s
= TCX(obj
);
538 memory_region_init_ram(&s
->rom
, NULL
, "tcx.prom", FCODE_MAX_ROM_SIZE
);
539 memory_region_set_readonly(&s
->rom
, true);
540 sysbus_init_mmio(sbd
, &s
->rom
);
543 memory_region_init_io(&s
->dac
, OBJECT(s
), &tcx_dac_ops
, s
,
544 "tcx.dac", TCX_DAC_NREGS
);
545 sysbus_init_mmio(sbd
, &s
->dac
);
548 memory_region_init_io(&s
->tec
, OBJECT(s
), &dummy_ops
, s
,
549 "tcx.tec", TCX_TEC_NREGS
);
550 sysbus_init_mmio(sbd
, &s
->tec
);
552 /* THC: NetBSD writes here even with 8-bit display: dummy */
553 memory_region_init_io(&s
->thc24
, OBJECT(s
), &dummy_ops
, s
, "tcx.thc24",
555 sysbus_init_mmio(sbd
, &s
->thc24
);
560 static void tcx_realizefn(DeviceState
*dev
, Error
**errp
)
562 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
563 TCXState
*s
= TCX(dev
);
564 ram_addr_t vram_offset
= 0;
567 char *fcode_filename
;
569 memory_region_init_ram(&s
->vram_mem
, OBJECT(s
), "tcx.vram",
570 s
->vram_size
* (1 + 4 + 4));
571 vmstate_register_ram_global(&s
->vram_mem
);
572 vram_base
= memory_region_get_ram_ptr(&s
->vram_mem
);
575 vmstate_register_ram_global(&s
->rom
);
576 fcode_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, TCX_ROM_FILE
);
577 if (fcode_filename
) {
578 ret
= load_image_targphys(fcode_filename
, s
->prom_addr
,
580 if (ret
< 0 || ret
> FCODE_MAX_ROM_SIZE
) {
581 error_report("tcx: could not load prom '%s'", TCX_ROM_FILE
);
588 memory_region_init_alias(&s
->vram_8bit
, OBJECT(s
), "tcx.vram.8bit",
589 &s
->vram_mem
, vram_offset
, size
);
590 sysbus_init_mmio(sbd
, &s
->vram_8bit
);
594 if (s
->depth
== 24) {
596 size
= s
->vram_size
* 4;
597 s
->vram24
= (uint32_t *)vram_base
;
598 s
->vram24_offset
= vram_offset
;
599 memory_region_init_alias(&s
->vram_24bit
, OBJECT(s
), "tcx.vram.24bit",
600 &s
->vram_mem
, vram_offset
, size
);
601 sysbus_init_mmio(sbd
, &s
->vram_24bit
);
606 size
= s
->vram_size
* 4;
607 s
->cplane
= (uint32_t *)vram_base
;
608 s
->cplane_offset
= vram_offset
;
609 memory_region_init_alias(&s
->vram_cplane
, OBJECT(s
), "tcx.vram.cplane",
610 &s
->vram_mem
, vram_offset
, size
);
611 sysbus_init_mmio(sbd
, &s
->vram_cplane
);
613 s
->con
= graphic_console_init(DEVICE(dev
), 0, &tcx24_ops
, s
);
615 /* THC 8 bit (dummy) */
616 memory_region_init_io(&s
->thc8
, OBJECT(s
), &dummy_ops
, s
, "tcx.thc8",
618 sysbus_init_mmio(sbd
, &s
->thc8
);
620 s
->con
= graphic_console_init(DEVICE(dev
), 0, &tcx_ops
, s
);
623 qemu_console_resize(s
->con
, s
->width
, s
->height
);
626 static Property tcx_properties
[] = {
627 DEFINE_PROP_UINT32("vram_size", TCXState
, vram_size
, -1),
628 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
629 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
630 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
631 DEFINE_PROP_UINT64("prom_addr", TCXState
, prom_addr
, -1),
632 DEFINE_PROP_END_OF_LIST(),
635 static void tcx_class_init(ObjectClass
*klass
, void *data
)
637 DeviceClass
*dc
= DEVICE_CLASS(klass
);
639 dc
->realize
= tcx_realizefn
;
640 dc
->reset
= tcx_reset
;
641 dc
->vmsd
= &vmstate_tcx
;
642 dc
->props
= tcx_properties
;
645 static const TypeInfo tcx_info
= {
647 .parent
= TYPE_SYS_BUS_DEVICE
,
648 .instance_size
= sizeof(TCXState
),
649 .instance_init
= tcx_initfn
,
650 .class_init
= tcx_class_init
,
653 static void tcx_register_types(void)
655 type_register_static(&tcx_info
);
658 type_init(tcx_register_types
)