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
27 #include "pixel_ops.h"
29 #include "qdev-addr.h"
33 #define TCX_DAC_NREGS 16
34 #define TCX_THC_NREGS_8 0x081c
35 #define TCX_THC_NREGS_24 0x1000
36 #define TCX_TEC_NREGS 0x1000
38 typedef struct TCXState
{
40 target_phys_addr_t addr
;
43 uint32_t *vram24
, *cplane
;
44 ram_addr_t vram_offset
, vram24_offset
, cplane_offset
;
46 uint16_t width
, height
, depth
;
47 uint8_t r
[256], g
[256], b
[256];
48 uint32_t palette
[256];
49 uint8_t dac_index
, dac_state
;
52 static void tcx_screen_dump(void *opaque
, const char *filename
);
53 static void tcx24_screen_dump(void *opaque
, const char *filename
);
55 static void tcx_set_dirty(TCXState
*s
)
59 for (i
= 0; i
< MAXX
* MAXY
; i
+= TARGET_PAGE_SIZE
) {
60 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
64 static void tcx24_set_dirty(TCXState
*s
)
68 for (i
= 0; i
< MAXX
* MAXY
* 4; i
+= TARGET_PAGE_SIZE
) {
69 cpu_physical_memory_set_dirty(s
->vram24_offset
+ i
);
70 cpu_physical_memory_set_dirty(s
->cplane_offset
+ i
);
74 static void update_palette_entries(TCXState
*s
, int start
, int end
)
77 for(i
= start
; i
< end
; i
++) {
78 switch(ds_get_bits_per_pixel(s
->ds
)) {
81 s
->palette
[i
] = rgb_to_pixel8(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
84 s
->palette
[i
] = rgb_to_pixel15(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
87 s
->palette
[i
] = rgb_to_pixel16(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
90 if (is_surface_bgr(s
->ds
->surface
))
91 s
->palette
[i
] = rgb_to_pixel32bgr(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
93 s
->palette
[i
] = rgb_to_pixel32(s
->r
[i
], s
->g
[i
], s
->b
[i
]);
104 static void tcx_draw_line32(TCXState
*s1
, uint8_t *d
,
105 const uint8_t *s
, int width
)
109 uint32_t *p
= (uint32_t *)d
;
111 for(x
= 0; x
< width
; x
++) {
113 *p
++ = s1
->palette
[val
];
117 static void tcx_draw_line16(TCXState
*s1
, uint8_t *d
,
118 const uint8_t *s
, int width
)
122 uint16_t *p
= (uint16_t *)d
;
124 for(x
= 0; x
< width
; x
++) {
126 *p
++ = s1
->palette
[val
];
130 static void tcx_draw_line8(TCXState
*s1
, uint8_t *d
,
131 const uint8_t *s
, int width
)
136 for(x
= 0; x
< width
; x
++) {
138 *d
++ = s1
->palette
[val
];
143 XXX Could be much more optimal:
144 * detect if line/page/whole screen is in 24 bit mode
145 * if destination is also BGR, use memcpy
147 static inline void tcx24_draw_line32(TCXState
*s1
, uint8_t *d
,
148 const uint8_t *s
, int width
,
149 const uint32_t *cplane
,
154 uint32_t *p
= (uint32_t *)d
;
157 bgr
= is_surface_bgr(s1
->ds
->surface
);
158 for(x
= 0; x
< width
; x
++, s
++, s24
++) {
159 if ((be32_to_cpu(*cplane
++) & 0xff000000) == 0x03000000) {
160 // 24-bit direct, BGR order
167 dval
= rgb_to_pixel32bgr(r
, g
, b
);
169 dval
= rgb_to_pixel32(r
, g
, b
);
172 dval
= s1
->palette
[val
];
178 static inline int check_dirty(ram_addr_t page
, ram_addr_t page24
,
184 ret
= cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
185 for (off
= 0; off
< TARGET_PAGE_SIZE
* 4; off
+= TARGET_PAGE_SIZE
) {
186 ret
|= cpu_physical_memory_get_dirty(page24
+ off
, VGA_DIRTY_FLAG
);
187 ret
|= cpu_physical_memory_get_dirty(cpage
+ off
, VGA_DIRTY_FLAG
);
192 static inline void reset_dirty(TCXState
*ts
, ram_addr_t page_min
,
193 ram_addr_t page_max
, ram_addr_t page24
,
196 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
198 page_min
-= ts
->vram_offset
;
199 page_max
-= ts
->vram_offset
;
200 cpu_physical_memory_reset_dirty(page24
+ page_min
* 4,
201 page24
+ page_max
* 4 + TARGET_PAGE_SIZE
,
203 cpu_physical_memory_reset_dirty(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)
220 page
= ts
->vram_offset
;
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 (cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
)) {
253 f(ts
, d
, s
, ts
->width
);
256 f(ts
, d
, s
, ts
->width
);
259 f(ts
, d
, s
, ts
->width
);
262 f(ts
, d
, s
, ts
->width
);
267 /* flush to display */
268 dpy_update(ts
->ds
, 0, y_start
,
269 ts
->width
, y
- y_start
);
277 /* flush to display */
278 dpy_update(ts
->ds
, 0, y_start
,
279 ts
->width
, y
- y_start
);
281 /* reset modified pages */
282 if (page_max
>= page_min
) {
283 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
,
288 static void tcx24_update_display(void *opaque
)
290 TCXState
*ts
= opaque
;
291 ram_addr_t page
, page_min
, page_max
, cpage
, page24
;
292 int y
, y_start
, dd
, ds
;
294 uint32_t *cptr
, *s24
;
296 if (ds_get_bits_per_pixel(ts
->ds
) != 32)
298 page
= ts
->vram_offset
;
299 page24
= ts
->vram24_offset
;
300 cpage
= ts
->cplane_offset
;
304 d
= ds_get_data(ts
->ds
);
308 dd
= ds_get_linesize(ts
->ds
);
311 for(y
= 0; y
< ts
->height
; y
+= 4, page
+= TARGET_PAGE_SIZE
,
312 page24
+= TARGET_PAGE_SIZE
, cpage
+= TARGET_PAGE_SIZE
) {
313 if (check_dirty(page
, page24
, cpage
)) {
320 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
325 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
330 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
335 tcx24_draw_line32(ts
, d
, s
, ts
->width
, cptr
, s24
);
342 /* flush to display */
343 dpy_update(ts
->ds
, 0, y_start
,
344 ts
->width
, y
- y_start
);
354 /* flush to display */
355 dpy_update(ts
->ds
, 0, y_start
,
356 ts
->width
, y
- y_start
);
358 /* reset modified pages */
359 if (page_max
>= page_min
) {
360 reset_dirty(ts
, page_min
, page_max
, page24
, cpage
);
364 static void tcx_invalidate_display(void *opaque
)
366 TCXState
*s
= opaque
;
369 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
372 static void tcx24_invalidate_display(void *opaque
)
374 TCXState
*s
= opaque
;
378 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
381 static int vmstate_tcx_post_load(void *opaque
, int version_id
)
383 TCXState
*s
= opaque
;
385 update_palette_entries(s
, 0, 256);
386 if (s
->depth
== 24) {
395 static const VMStateDescription vmstate_tcx
= {
398 .minimum_version_id
= 4,
399 .minimum_version_id_old
= 4,
400 .post_load
= vmstate_tcx_post_load
,
401 .fields
= (VMStateField
[]) {
402 VMSTATE_UINT16(height
, TCXState
),
403 VMSTATE_UINT16(width
, TCXState
),
404 VMSTATE_UINT16(depth
, TCXState
),
405 VMSTATE_BUFFER(r
, TCXState
),
406 VMSTATE_BUFFER(g
, TCXState
),
407 VMSTATE_BUFFER(b
, TCXState
),
408 VMSTATE_UINT8(dac_index
, TCXState
),
409 VMSTATE_UINT8(dac_state
, TCXState
),
410 VMSTATE_END_OF_LIST()
414 static void tcx_reset(DeviceState
*d
)
416 TCXState
*s
= container_of(d
, TCXState
, busdev
.qdev
);
418 /* Initialize palette */
419 memset(s
->r
, 0, 256);
420 memset(s
->g
, 0, 256);
421 memset(s
->b
, 0, 256);
422 s
->r
[255] = s
->g
[255] = s
->b
[255] = 255;
423 update_palette_entries(s
, 0, 256);
424 memset(s
->vram
, 0, MAXX
*MAXY
);
425 cpu_physical_memory_reset_dirty(s
->vram_offset
, s
->vram_offset
+
426 MAXX
* MAXY
* (1 + 4 + 4), VGA_DIRTY_FLAG
);
431 static uint32_t tcx_dac_readl(void *opaque
, target_phys_addr_t addr
)
436 static void tcx_dac_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
438 TCXState
*s
= opaque
;
442 s
->dac_index
= val
>> 24;
446 switch (s
->dac_state
) {
448 s
->r
[s
->dac_index
] = val
>> 24;
449 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
453 s
->g
[s
->dac_index
] = val
>> 24;
454 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
458 s
->b
[s
->dac_index
] = val
>> 24;
459 update_palette_entries(s
, s
->dac_index
, s
->dac_index
+ 1);
460 s
->dac_index
= (s
->dac_index
+ 1) & 255; // Index autoincrement
472 static CPUReadMemoryFunc
* const tcx_dac_read
[3] = {
478 static CPUWriteMemoryFunc
* const tcx_dac_write
[3] = {
484 static uint32_t tcx_dummy_readl(void *opaque
, target_phys_addr_t addr
)
489 static void tcx_dummy_writel(void *opaque
, target_phys_addr_t addr
,
494 static CPUReadMemoryFunc
* const tcx_dummy_read
[3] = {
500 static CPUWriteMemoryFunc
* const tcx_dummy_write
[3] = {
506 static int tcx_init1(SysBusDevice
*dev
)
508 TCXState
*s
= FROM_SYSBUS(TCXState
, dev
);
509 int io_memory
, dummy_memory
;
510 ram_addr_t vram_offset
;
514 vram_offset
= qemu_ram_alloc(s
->vram_size
* (1 + 4 + 4));
515 vram_base
= qemu_get_ram_ptr(vram_offset
);
516 s
->vram_offset
= vram_offset
;
521 sysbus_init_mmio(dev
, size
, s
->vram_offset
);
526 io_memory
= cpu_register_io_memory(tcx_dac_read
, tcx_dac_write
, s
);
527 sysbus_init_mmio(dev
, TCX_DAC_NREGS
, io_memory
);
530 dummy_memory
= cpu_register_io_memory(tcx_dummy_read
, tcx_dummy_write
,
532 sysbus_init_mmio(dev
, TCX_TEC_NREGS
, dummy_memory
);
533 /* THC: NetBSD writes here even with 8-bit display: dummy */
534 sysbus_init_mmio(dev
, TCX_THC_NREGS_24
, dummy_memory
);
536 if (s
->depth
== 24) {
538 size
= s
->vram_size
* 4;
539 s
->vram24
= (uint32_t *)vram_base
;
540 s
->vram24_offset
= vram_offset
;
541 sysbus_init_mmio(dev
, size
, vram_offset
);
546 size
= s
->vram_size
* 4;
547 s
->cplane
= (uint32_t *)vram_base
;
548 s
->cplane_offset
= vram_offset
;
549 sysbus_init_mmio(dev
, size
, vram_offset
);
551 s
->ds
= graphic_console_init(tcx24_update_display
,
552 tcx24_invalidate_display
,
553 tcx24_screen_dump
, NULL
, s
);
555 /* THC 8 bit (dummy) */
556 sysbus_init_mmio(dev
, TCX_THC_NREGS_8
, dummy_memory
);
558 s
->ds
= graphic_console_init(tcx_update_display
,
559 tcx_invalidate_display
,
560 tcx_screen_dump
, NULL
, s
);
563 tcx_reset(&s
->busdev
.qdev
);
564 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
568 static void tcx_screen_dump(void *opaque
, const char *filename
)
570 TCXState
*s
= opaque
;
575 f
= fopen(filename
, "wb");
578 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
580 for(y
= 0; y
< s
->height
; y
++) {
582 for(x
= 0; x
< s
->width
; x
++) {
595 static void tcx24_screen_dump(void *opaque
, const char *filename
)
597 TCXState
*s
= opaque
;
600 uint32_t *s24
, *cptr
, dval
;
603 f
= fopen(filename
, "wb");
606 fprintf(f
, "P6\n%d %d\n%d\n", s
->width
, s
->height
, 255);
610 for(y
= 0; y
< s
->height
; y
++) {
612 for(x
= 0; x
< s
->width
; x
++, d
++, s24
++) {
613 if ((*cptr
++ & 0xff000000) == 0x03000000) { // 24-bit direct
614 dval
= *s24
& 0x00ffffff;
615 fputc((dval
>> 16) & 0xff, f
);
616 fputc((dval
>> 8) & 0xff, f
);
617 fputc(dval
& 0xff, f
);
631 static SysBusDeviceInfo tcx_info
= {
633 .qdev
.name
= "SUNW,tcx",
634 .qdev
.size
= sizeof(TCXState
),
635 .qdev
.reset
= tcx_reset
,
636 .qdev
.vmsd
= &vmstate_tcx
,
637 .qdev
.props
= (Property
[]) {
638 DEFINE_PROP_TADDR("addr", TCXState
, addr
, -1),
639 DEFINE_PROP_HEX32("vram_size", TCXState
, vram_size
, -1),
640 DEFINE_PROP_UINT16("width", TCXState
, width
, -1),
641 DEFINE_PROP_UINT16("height", TCXState
, height
, -1),
642 DEFINE_PROP_UINT16("depth", TCXState
, depth
, -1),
643 DEFINE_PROP_END_OF_LIST(),
647 static void tcx_register_devices(void)
649 sysbus_register_withprop(&tcx_info
);
652 device_init(tcx_register_devices
)