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
, bool cswitch
);
60 static void tcx24_screen_dump(void *opaque
, const char *filename
, bool cswitch
);
62 static void tcx_set_dirty(TCXState
*s
)
64 memory_region_set_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
);
67 static void tcx24_set_dirty(TCXState
*s
)
69 memory_region_set_dirty(&s
->vram_mem
, s
->vram24_offset
, MAXX
* MAXY
* 4);
70 memory_region_set_dirty(&s
->vram_mem
, s
->cplane_offset
, MAXX
* MAXY
* 4);
73 static void update_palette_entries(TCXState
*s
, int start
, int end
)
76 for(i
= start
; i
< end
; i
++) {
77 switch(ds_get_bits_per_pixel(s
->ds
)) {
80 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
83 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
86 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
89 if (is_surface_bgr(s
->ds
->surface
))
90 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
92 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
103 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
104 const uint8_t *s
, int width
)
108 uint32_t *p
= (uint32_t *)d
;
110 for(x
= 0; x
< width
; x
++) {
112 *p
++ = s1
->palette
[val
];
116 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
117 const uint8_t *s
, int width
)
121 uint16_t *p
= (uint16_t *)d
;
123 for(x
= 0; x
< width
; x
++) {
125 *p
++ = s1
->palette
[val
];
129 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
130 const uint8_t *s
, int width
)
135 for(x
= 0; x
< width
; x
++) {
137 *d
++ = s1
->palette
[val
];
142 XXX Could be much more optimal:
143 * detect if line/page/whole screen is in 24 bit mode
144 * if destination is also BGR, use memcpy
146 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
147 const uint8_t *s
, int width
,
148 const uint32_t *cplane
,
153 uint32_t *p
= (uint32_t *)d
;
156 bgr
= is_surface_bgr(s1
->ds
->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 ram_addr_t page
, page_min
, page_max
;
214 int y
, y_start
, dd
, ds
;
216 void (*f
)(TCXState
*s1
, uint8_t *dst
, const uint8_t *src
, int width
);
218 if (ds_get_bits_per_pixel(ts
->ds
) == 0)
224 d
= ds_get_data(ts
->ds
);
226 dd
= ds_get_linesize(ts
->ds
);
229 switch (ds_get_bits_per_pixel(ts
->ds
)) {
245 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
) {
246 if (memory_region_get_dirty(&ts
->vram_mem
, page
, TARGET_PAGE_SIZE
,
254 f(ts
, d
, s
, ts
->width
);
257 f(ts
, d
, s
, ts
->width
);
260 f(ts
, d
, s
, ts
->width
);
263 f(ts
, d
, s
, ts
->width
);
268 /* flush to display */
269 dpy_update(ts
->ds
, 0, y_start
,
270 ts
->width
, y
- y_start
);
278 /* flush to display */
279 dpy_update(ts
->ds
, 0, y_start
,
280 ts
->width
, y
- y_start
);
282 /* reset modified pages */
283 if (page_max
>= page_min
) {
284 memory_region_reset_dirty(&ts
->vram_mem
,
285 page_min
, page_max
+ TARGET_PAGE_SIZE
,
290 static void tcx24_update_display(void *opaque
)
292 TCXState
*ts
= opaque
;
293 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
294 int y
, y_start
, dd
, ds
;
296 uint32_t *cptr
, *s24
;
298 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
301 page24
= ts
->vram24_offset
;
302 cpage
= ts
->cplane_offset
;
306 d
= ds_get_data(ts
->ds
);
310 dd
= ds_get_linesize(ts
->ds
);
313 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
314 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
315 if (check_dirty(ts
, page
, page24
, cpage
)) {
322 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
327 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
332 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
337 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
344 /* flush to display */
345 dpy_update(ts
->ds
, 0, y_start
,
346 ts
->width
, y
- y_start
);
356 /* flush to display */
357 dpy_update(ts
->ds
, 0, y_start
,
358 ts
->width
, y
- y_start
);
360 /* reset modified pages */
361 if (page_max
>= page_min
) {
362 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
366 static void tcx_invalidate_display(void *opaque
)
368 TCXState
*s
= opaque
;
371 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
374 static void tcx24_invalidate_display(void *opaque
)
376 TCXState
*s
= opaque
;
380 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
383 static int vmstate_tcx_post_load(void *opaque
, int version_id
)
385 TCXState
*s
= opaque
;
387 update_palette_entries(s
, 0, 256);
388 if (s
->depth
== 24) {
397 static const VMStateDescription vmstate_tcx
= {
400 .minimum_version_id
= 4,
401 .minimum_version_id_old
= 4,
402 .post_load
= vmstate_tcx_post_load
,
403 .fields
= (VMStateField
[]) {
404 VMSTATE_UINT16(height
, TCXState
),
405 VMSTATE_UINT16(width
, TCXState
),
406 VMSTATE_UINT16(depth
, TCXState
),
407 VMSTATE_BUFFER(r
, TCXState
),
408 VMSTATE_BUFFER(g
, TCXState
),
409 VMSTATE_BUFFER(b
, TCXState
),
410 VMSTATE_UINT8(dac_index
, TCXState
),
411 VMSTATE_UINT8(dac_state
, TCXState
),
412 VMSTATE_END_OF_LIST()
416 static void tcx_reset(DeviceState
*d
)
418 TCXState
*s
= container_of(d
, TCXState
, busdev
.qdev
);
420 /* Initialize palette */
421 memset(s
->r
, 0, 256);
422 memset(s
->g
, 0, 256);
423 memset(s
->b
, 0, 256);
424 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
425 update_palette_entries(s
, 0, 256);
426 memset(s
->vram
, 0, MAXX
*MAXY
);
427 memory_region_reset_dirty(&s
->vram_mem
, 0, MAXX
* MAXY
* (1 + 4 + 4),
433 static uint64_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
,
439 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint64_t val
,
442 TCXState
*s
= opaque
;
446 s
->dac_index
= val
>> 24;
450 switch (s
->dac_state
) {
452 s
->r
[s
->dac_index
] = val
>> 24;
453 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
457 s
->g
[s
->dac_index
] = val
>> 24;
458 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
462 s
->b
[s
->dac_index
] = val
>> 24;
463 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
464 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
476 static const MemoryRegionOps tcx_dac_ops
= {
477 .read
= tcx_dac_readl
,
478 .write
= tcx_dac_writel
,
479 .endianness
= DEVICE_NATIVE_ENDIAN
,
481 .min_access_size
= 4,
482 .max_access_size
= 4,
486 static uint64_t dummy_readl(void *opaque
, target_phys_addr_t addr
,
492 static void dummy_writel(void *opaque
, target_phys_addr_t addr
,
493 uint64_t val
, unsigned size
)
497 static const MemoryRegionOps dummy_ops
= {
499 .write
= dummy_writel
,
500 .endianness
= DEVICE_NATIVE_ENDIAN
,
502 .min_access_size
= 4,
503 .max_access_size
= 4,
507 static int tcx_init1(SysBusDevice
*dev
)
509 TCXState
*s
= FROM_SYSBUS(TCXState
, dev
);
510 ram_addr_t vram_offset
= 0;
514 memory_region_init_ram(&s
->vram_mem
, "tcx.vram",
515 s
->vram_size
* (1 + 4 + 4));
516 vmstate_register_ram_global(&s
->vram_mem
);
517 vram_base
= memory_region_get_ram_ptr(&s
->vram_mem
);
522 memory_region_init_alias(&s
->vram_8bit
, "tcx.vram.8bit",
523 &s
->vram_mem
, vram_offset
, size
);
524 sysbus_init_mmio(dev
, &s
->vram_8bit
);
529 memory_region_init_io(&s
->dac
, &tcx_dac_ops
, s
, "tcx.dac", TCX_DAC_NREGS
);
530 sysbus_init_mmio(dev
, &s
->dac
);
533 memory_region_init_io(&s
->tec
, &dummy_ops
, s
, "tcx.tec", TCX_TEC_NREGS
);
534 sysbus_init_mmio(dev
, &s
->tec
);
535 /* THC: NetBSD writes here even with 8-bit display: dummy */
536 memory_region_init_io(&s
->thc24
, &dummy_ops
, s
, "tcx.thc24",
538 sysbus_init_mmio(dev
, &s
->thc24
);
540 if (s
->depth
== 24) {
542 size
= s
->vram_size
* 4;
543 s
->vram24
= (uint32_t *)vram_base
;
544 s
->vram24_offset
= vram_offset
;
545 memory_region_init_alias(&s
->vram_24bit
, "tcx.vram.24bit",
546 &s
->vram_mem
, vram_offset
, size
);
547 sysbus_init_mmio(dev
, &s
->vram_24bit
);
552 size
= s
->vram_size
* 4;
553 s
->cplane
= (uint32_t *)vram_base
;
554 s
->cplane_offset
= vram_offset
;
555 memory_region_init_alias(&s
->vram_cplane
, "tcx.vram.cplane",
556 &s
->vram_mem
, vram_offset
, size
);
557 sysbus_init_mmio(dev
, &s
->vram_cplane
);
559 s
->ds
= graphic_console_init(tcx24_update_display
,
560 tcx24_invalidate_display
,
561 tcx24_screen_dump
, NULL
, s
);
563 /* THC 8 bit (dummy) */
564 memory_region_init_io(&s
->thc8
, &dummy_ops
, s
, "tcx.thc8",
566 sysbus_init_mmio(dev
, &s
->thc8
);
568 s
->ds
= graphic_console_init(tcx_update_display
,
569 tcx_invalidate_display
,
570 tcx_screen_dump
, NULL
, s
);
573 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
577 static void tcx_screen_dump(void *opaque
, const char *filename
, bool cswitch
)
579 TCXState
*s
= opaque
;
584 f
= fopen(filename
, "wb");
587 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
589 for(y
= 0; y
< s
->height
; y
++) {
591 for(x
= 0; x
< s
->width
; x
++) {
604 static void tcx24_screen_dump(void *opaque
, const char *filename
, bool cswitch
)
606 TCXState
*s
= opaque
;
609 uint32_t *s24
, *cptr
, dval
;
612 f
= fopen(filename
, "wb");
615 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
619 for(y
= 0; y
< s
->height
; y
++) {
621 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
622 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
623 dval
= *s24
& 0x00ffffff;
624 fputc((dval
>> 16) & 0xff, f
);
625 fputc((dval
>> 8) & 0xff, f
);
626 fputc(dval
& 0xff, f
);
640 static Property tcx_properties
[] = {
641 DEFINE_PROP_TADDR("addr", TCXState
, addr
, -1),
642 DEFINE_PROP_HEX32("vram_size", TCXState
, vram_size
, -1),
643 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
644 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
645 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
646 DEFINE_PROP_END_OF_LIST(),
649 static void tcx_class_init(ObjectClass
*klass
, void *data
)
651 DeviceClass
*dc
= DEVICE_CLASS(klass
);
652 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
655 dc
->reset
= tcx_reset
;
656 dc
->vmsd
= &vmstate_tcx
;
657 dc
->props
= tcx_properties
;
660 static TypeInfo tcx_info
= {
662 .parent
= TYPE_SYS_BUS_DEVICE
,
663 .instance_size
= sizeof(TCXState
),
664 .class_init
= tcx_class_init
,
667 static void tcx_register_types(void)
669 type_register_static(&tcx_info
);
672 type_init(tcx_register_types
)