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/sysbus.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
{
41 uint32_t *vram24
, *cplane
;
42 MemoryRegion vram_mem
;
43 MemoryRegion vram_8bit
;
44 MemoryRegion vram_24bit
;
45 MemoryRegion vram_cplane
;
50 ram_addr_t vram24_offset
, cplane_offset
;
52 uint32_t palette
[256];
53 uint8_t r
[256], g
[256], b
[256];
54 uint16_t width
, height
, depth
;
55 uint8_t dac_index
, dac_state
;
58 static void tcx_set_dirty(TCXState
*s
)
60 memory_region_set_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
);
63 static void tcx24_set_dirty(TCXState
*s
)
65 memory_region_set_dirty(&s
->vram_mem
, s
->vram24_offset
, MAXX
* MAXY
* 4);
66 memory_region_set_dirty(&s
->vram_mem
, s
->cplane_offset
, MAXX
* MAXY
* 4);
69 static void update_palette_entries(TCXState
*s
, int start
, int end
)
71 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
74 for (i
= start
; i
< end
; i
++) {
75 switch (surface_bits_per_pixel(surface
)) {
78 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
81 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
84 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
87 if (is_surface_bgr(surface
)) {
88 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
90 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
,
150 DisplaySurface
*surface
= qemu_console_surface(s1
->con
);
153 uint32_t *p
= (uint32_t *)d
;
156 bgr
= is_surface_bgr(surface
);
157 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
158 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
159 // 24-bit direct, BGR order
166 dval
= rgb_to_pixel32bgr(r
, g
, b
);
168 dval
= rgb_to_pixel32(r
, g
, b
);
171 dval
= s1
->palette
[val
];
177 static inline int check_dirty(TCXState
*s
, ram_addr_t page
, ram_addr_t page24
,
182 ret
= memory_region_get_dirty(&s
->vram_mem
, page
, TARGET_PAGE_SIZE
,
184 ret
|= memory_region_get_dirty(&s
->vram_mem
, page24
, TARGET_PAGE_SIZE
* 4,
186 ret
|= memory_region_get_dirty(&s
->vram_mem
, cpage
, TARGET_PAGE_SIZE
* 4,
191 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
192 ram_addr_t page_max
, ram_addr_t page24
,
195 memory_region_reset_dirty(&ts
->vram_mem
,
196 page_min
, page_max
+ TARGET_PAGE_SIZE
,
198 memory_region_reset_dirty(&ts
->vram_mem
,
199 page24
+ page_min
* 4,
200 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
202 memory_region_reset_dirty(&ts
->vram_mem
,
203 cpage
+ page_min
* 4,
204 cpage
+ page_max
* 4 + TARGET_PAGE_SIZE
,
208 /* Fixed line length 1024 allows us to do nice tricks not possible on
210 static void tcx_update_display(void *opaque
)
212 TCXState
*ts
= opaque
;
213 DisplaySurface
*surface
= qemu_console_surface(ts
->con
);
214 ram_addr_t page
, page_min
, page_max
;
215 int y
, y_start
, dd
, ds
;
217 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
219 if (surface_bits_per_pixel(surface
) == 0) {
227 d
= surface_data(surface
);
229 dd
= surface_stride(surface
);
232 switch (surface_bits_per_pixel(surface
)) {
248 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
249 if (memory_region_get_dirty(&ts
->vram_mem
, page
, TARGET_PAGE_SIZE
,
257 f(ts
, d
, s
, ts
->width
);
260 f(ts
, d
, s
, ts
->width
);
263 f(ts
, d
, s
, ts
->width
);
266 f(ts
, d
, s
, ts
->width
);
271 /* flush to display */
272 dpy_gfx_update(ts
->con
, 0, y_start
,
273 ts
->width
, y
- y_start
);
281 /* flush to display */
282 dpy_gfx_update(ts
->con
, 0, y_start
,
283 ts
->width
, y
- y_start
);
285 /* reset modified pages */
286 if (page_max
>= page_min
) {
287 memory_region_reset_dirty(&ts
->vram_mem
,
288 page_min
, page_max
+ TARGET_PAGE_SIZE
,
293 static void tcx24_update_display(void *opaque
)
295 TCXState
*ts
= opaque
;
296 DisplaySurface
*surface
= qemu_console_surface(ts
->con
);
297 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
298 int y
, y_start
, dd
, ds
;
300 uint32_t *cptr
, *s24
;
302 if (surface_bits_per_pixel(surface
) != 32) {
307 page24
= ts
->vram24_offset
;
308 cpage
= ts
->cplane_offset
;
312 d
= surface_data(surface
);
316 dd
= surface_stride(surface
);
319 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
320 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
321 if (check_dirty(ts
, page
, page24
, cpage
)) {
328 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
333 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
338 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
343 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
350 /* flush to display */
351 dpy_gfx_update(ts
->con
, 0, y_start
,
352 ts
->width
, y
- y_start
);
362 /* flush to display */
363 dpy_gfx_update(ts
->con
, 0, y_start
,
364 ts
->width
, y
- y_start
);
366 /* reset modified pages */
367 if (page_max
>= page_min
) {
368 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
372 static void tcx_invalidate_display(void *opaque
)
374 TCXState
*s
= opaque
;
377 qemu_console_resize(s
->con
, s
->width
, s
->height
);
380 static void tcx24_invalidate_display(void *opaque
)
382 TCXState
*s
= opaque
;
386 qemu_console_resize(s
->con
, s
->width
, s
->height
);
389 static int vmstate_tcx_post_load(void *opaque
, int version_id
)
391 TCXState
*s
= opaque
;
393 update_palette_entries(s
, 0, 256);
394 if (s
->depth
== 24) {
403 static const VMStateDescription vmstate_tcx
= {
406 .minimum_version_id
= 4,
407 .minimum_version_id_old
= 4,
408 .post_load
= vmstate_tcx_post_load
,
409 .fields
= (VMStateField
[]) {
410 VMSTATE_UINT16(height
, TCXState
),
411 VMSTATE_UINT16(width
, TCXState
),
412 VMSTATE_UINT16(depth
, TCXState
),
413 VMSTATE_BUFFER(r
, TCXState
),
414 VMSTATE_BUFFER(g
, TCXState
),
415 VMSTATE_BUFFER(b
, TCXState
),
416 VMSTATE_UINT8(dac_index
, TCXState
),
417 VMSTATE_UINT8(dac_state
, TCXState
),
418 VMSTATE_END_OF_LIST()
422 static void tcx_reset(DeviceState
*d
)
424 TCXState
*s
= container_of(d
, TCXState
, busdev
.qdev
);
426 /* Initialize palette */
427 memset(s
->r
, 0, 256);
428 memset(s
->g
, 0, 256);
429 memset(s
->b
, 0, 256);
430 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
431 update_palette_entries(s
, 0, 256);
432 memset(s
->vram
, 0, MAXX
*MAXY
);
433 memory_region_reset_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
* (1 + 4 + 4),
439 static uint64_t tcx_dac_readl(void *opaque
, hwaddr addr
,
445 static void tcx_dac_writel(void *opaque
, hwaddr addr
, uint64_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
481 static const MemoryRegionOps tcx_dac_ops
= {
482 .read
= tcx_dac_readl
,
483 .write
= tcx_dac_writel
,
484 .endianness
= DEVICE_NATIVE_ENDIAN
,
486 .min_access_size
= 4,
487 .max_access_size
= 4,
491 static uint64_t dummy_readl(void *opaque
, hwaddr addr
,
497 static void dummy_writel(void *opaque
, hwaddr addr
,
498 uint64_t val
, unsigned size
)
502 static const MemoryRegionOps dummy_ops
= {
504 .write
= dummy_writel
,
505 .endianness
= DEVICE_NATIVE_ENDIAN
,
507 .min_access_size
= 4,
508 .max_access_size
= 4,
512 static const GraphicHwOps tcx_ops
= {
513 .invalidate
= tcx_invalidate_display
,
514 .gfx_update
= tcx_update_display
,
517 static const GraphicHwOps tcx24_ops
= {
518 .invalidate
= tcx24_invalidate_display
,
519 .gfx_update
= tcx24_update_display
,
522 static int tcx_init1(SysBusDevice
*dev
)
524 TCXState
*s
= FROM_SYSBUS(TCXState
, dev
);
525 ram_addr_t vram_offset
= 0;
529 memory_region_init_ram(&s
->vram_mem
, "tcx.vram",
530 s
->vram_size
* (1 + 4 + 4));
531 vmstate_register_ram_global(&s
->vram_mem
);
532 vram_base
= memory_region_get_ram_ptr(&s
->vram_mem
);
537 memory_region_init_alias(&s
->vram_8bit
, "tcx.vram.8bit",
538 &s
->vram_mem
, vram_offset
, size
);
539 sysbus_init_mmio(dev
, &s
->vram_8bit
);
544 memory_region_init_io(&s
->dac
, &tcx_dac_ops
, s
, "tcx.dac", TCX_DAC_NREGS
);
545 sysbus_init_mmio(dev
, &s
->dac
);
548 memory_region_init_io(&s
->tec
, &dummy_ops
, s
, "tcx.tec", TCX_TEC_NREGS
);
549 sysbus_init_mmio(dev
, &s
->tec
);
550 /* THC: NetBSD writes here even with 8-bit display: dummy */
551 memory_region_init_io(&s
->thc24
, &dummy_ops
, s
, "tcx.thc24",
553 sysbus_init_mmio(dev
, &s
->thc24
);
555 if (s
->depth
== 24) {
557 size
= s
->vram_size
* 4;
558 s
->vram24
= (uint32_t *)vram_base
;
559 s
->vram24_offset
= vram_offset
;
560 memory_region_init_alias(&s
->vram_24bit
, "tcx.vram.24bit",
561 &s
->vram_mem
, vram_offset
, size
);
562 sysbus_init_mmio(dev
, &s
->vram_24bit
);
567 size
= s
->vram_size
* 4;
568 s
->cplane
= (uint32_t *)vram_base
;
569 s
->cplane_offset
= vram_offset
;
570 memory_region_init_alias(&s
->vram_cplane
, "tcx.vram.cplane",
571 &s
->vram_mem
, vram_offset
, size
);
572 sysbus_init_mmio(dev
, &s
->vram_cplane
);
574 s
->con
= graphic_console_init(DEVICE(dev
), &tcx24_ops
, s
);
576 /* THC 8 bit (dummy) */
577 memory_region_init_io(&s
->thc8
, &dummy_ops
, s
, "tcx.thc8",
579 sysbus_init_mmio(dev
, &s
->thc8
);
581 s
->con
= graphic_console_init(DEVICE(dev
), &tcx_ops
, s
);
584 qemu_console_resize(s
->con
, s
->width
, s
->height
);
588 static Property tcx_properties
[] = {
589 DEFINE_PROP_HEX32("vram_size", TCXState
, vram_size
, -1),
590 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
591 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
592 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
593 DEFINE_PROP_END_OF_LIST(),
596 static void tcx_class_init(ObjectClass
*klass
, void *data
)
598 DeviceClass
*dc
= DEVICE_CLASS(klass
);
599 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
602 dc
->reset
= tcx_reset
;
603 dc
->vmsd
= &vmstate_tcx
;
604 dc
->props
= tcx_properties
;
607 static const TypeInfo tcx_info
= {
609 .parent
= TYPE_SYS_BUS_DEVICE
,
610 .instance_size
= sizeof(TCXState
),
611 .class_init
= tcx_class_init
,
614 static void tcx_register_types(void)
616 type_register_static(&tcx_info
);
619 type_init(tcx_register_types
)