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"
29 #include "hw/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 {
43 uint32_t *vram24, *cplane;
44 MemoryRegion vram_mem;
45 MemoryRegion vram_8bit;
46 MemoryRegion vram_24bit;
47 MemoryRegion vram_cplane;
52 ram_addr_t vram24_offset, cplane_offset;
54 uint32_t palette[256];
55 uint8_t r[256], g[256], b[256];
56 uint16_t width, height, depth;
57 uint8_t dac_index, dac_state;
60 static void tcx_screen_dump(void *opaque, const char *filename, bool cswitch,
62 static void tcx24_screen_dump(void *opaque, const char *filename, bool cswitch,
65 static void tcx_set_dirty(TCXState *s)
67 memory_region_set_dirty(&s->vram_mem, 0, MAXX * MAXY);
70 static void tcx24_set_dirty(TCXState *s)
72 memory_region_set_dirty(&s->vram_mem, s->vram24_offset, MAXX * MAXY * 4);
73 memory_region_set_dirty(&s->vram_mem, s->cplane_offset, MAXX * MAXY * 4);
76 static void update_palette_entries(TCXState *s, int start, int end)
78 DisplaySurface *surface = qemu_console_surface(s->con);
81 for (i = start; i < end; i++) {
82 switch (surface_bits_per_pixel(surface)) {
85 s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
88 s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
91 s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
94 if (is_surface_bgr(surface)) {
95 s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
97 s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
102 if (s->depth == 24) {
109 static void tcx_draw_line32(TCXState *s1, uint8_t *d,
110 const uint8_t *s, int width)
114 uint32_t *p = (uint32_t *)d;
116 for(x = 0; x < width; x++) {
118 *p++ = s1->palette[val];
122 static void tcx_draw_line16(TCXState *s1, uint8_t *d,
123 const uint8_t *s, int width)
127 uint16_t *p = (uint16_t *)d;
129 for(x = 0; x < width; x++) {
131 *p++ = s1->palette[val];
135 static void tcx_draw_line8(TCXState *s1, uint8_t *d,
136 const uint8_t *s, int width)
141 for(x = 0; x < width; x++) {
143 *d++ = s1->palette[val];
148 XXX Could be much more optimal:
149 * detect if line/page/whole screen is in 24 bit mode
150 * if destination is also BGR, use memcpy
152 static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
153 const uint8_t *s, int width,
154 const uint32_t *cplane,
157 DisplaySurface *surface = qemu_console_surface(s1->con);
160 uint32_t *p = (uint32_t *)d;
163 bgr = is_surface_bgr(surface);
164 for(x = 0; x < width; x++, s++, s24++) {
165 if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
166 // 24-bit direct, BGR order
173 dval = rgb_to_pixel32bgr(r, g, b);
175 dval = rgb_to_pixel32(r, g, b);
178 dval = s1->palette[val];
184 static inline int check_dirty(TCXState *s, ram_addr_t page, ram_addr_t page24,
189 ret = memory_region_get_dirty(&s->vram_mem, page, TARGET_PAGE_SIZE,
191 ret |= memory_region_get_dirty(&s->vram_mem, page24, TARGET_PAGE_SIZE * 4,
193 ret |= memory_region_get_dirty(&s->vram_mem, cpage, TARGET_PAGE_SIZE * 4,
198 static inline void reset_dirty(TCXState *ts, ram_addr_t page_min,
199 ram_addr_t page_max, ram_addr_t page24,
202 memory_region_reset_dirty(&ts->vram_mem,
203 page_min, page_max + TARGET_PAGE_SIZE,
205 memory_region_reset_dirty(&ts->vram_mem,
206 page24 + page_min * 4,
207 page24 + page_max * 4 + TARGET_PAGE_SIZE,
209 memory_region_reset_dirty(&ts->vram_mem,
210 cpage + page_min * 4,
211 cpage + page_max * 4 + TARGET_PAGE_SIZE,
215 /* Fixed line length 1024 allows us to do nice tricks not possible on
217 static void tcx_update_display(void *opaque)
219 TCXState *ts = opaque;
220 DisplaySurface *surface = qemu_console_surface(ts->con);
221 ram_addr_t page, page_min, page_max;
222 int y, y_start, dd, ds;
224 void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
226 if (surface_bits_per_pixel(surface) == 0) {
234 d = surface_data(surface);
236 dd = surface_stride(surface);
239 switch (surface_bits_per_pixel(surface)) {
255 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
256 if (memory_region_get_dirty(&ts->vram_mem, page, TARGET_PAGE_SIZE,
264 f(ts, d, s, ts->width);
267 f(ts, d, s, ts->width);
270 f(ts, d, s, ts->width);
273 f(ts, d, s, ts->width);
278 /* flush to display */
279 dpy_gfx_update(ts->con, 0, y_start,
280 ts->width, y - y_start);
288 /* flush to display */
289 dpy_gfx_update(ts->con, 0, y_start,
290 ts->width, y - y_start);
292 /* reset modified pages */
293 if (page_max >= page_min) {
294 memory_region_reset_dirty(&ts->vram_mem,
295 page_min, page_max + TARGET_PAGE_SIZE,
300 static void tcx24_update_display(void *opaque)
302 TCXState *ts = opaque;
303 DisplaySurface *surface = qemu_console_surface(ts->con);
304 ram_addr_t page, page_min, page_max, cpage, page24;
305 int y, y_start, dd, ds;
307 uint32_t *cptr, *s24;
309 if (surface_bits_per_pixel(surface) != 32) {
314 page24 = ts->vram24_offset;
315 cpage = ts->cplane_offset;
319 d = surface_data(surface);
323 dd = surface_stride(surface);
326 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
327 page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
328 if (check_dirty(ts, page, page24, cpage)) {
335 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
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);
357 /* flush to display */
358 dpy_gfx_update(ts->con, 0, y_start,
359 ts->width, y - y_start);
369 /* flush to display */
370 dpy_gfx_update(ts->con, 0, y_start,
371 ts->width, y - y_start);
373 /* reset modified pages */
374 if (page_max >= page_min) {
375 reset_dirty(ts, page_min, page_max, page24, cpage);
379 static void tcx_invalidate_display(void *opaque)
381 TCXState *s = opaque;
384 qemu_console_resize(s->con, s->width, s->height);
387 static void tcx24_invalidate_display(void *opaque)
389 TCXState *s = opaque;
393 qemu_console_resize(s->con, s->width, s->height);
396 static int vmstate_tcx_post_load(void *opaque, int version_id)
398 TCXState *s = opaque;
400 update_palette_entries(s, 0, 256);
401 if (s->depth == 24) {
410 static const VMStateDescription vmstate_tcx = {
413 .minimum_version_id = 4,
414 .minimum_version_id_old = 4,
415 .post_load = vmstate_tcx_post_load,
416 .fields = (VMStateField []) {
417 VMSTATE_UINT16(height, TCXState),
418 VMSTATE_UINT16(width, TCXState),
419 VMSTATE_UINT16(depth, TCXState),
420 VMSTATE_BUFFER(r, TCXState),
421 VMSTATE_BUFFER(g, TCXState),
422 VMSTATE_BUFFER(b, TCXState),
423 VMSTATE_UINT8(dac_index, TCXState),
424 VMSTATE_UINT8(dac_state, TCXState),
425 VMSTATE_END_OF_LIST()
429 static void tcx_reset(DeviceState *d)
431 TCXState *s = container_of(d, TCXState, busdev.qdev);
433 /* Initialize palette */
434 memset(s->r, 0, 256);
435 memset(s->g, 0, 256);
436 memset(s->b, 0, 256);
437 s->r[255] = s->g[255] = s->b[255] = 255;
438 update_palette_entries(s, 0, 256);
439 memset(s->vram, 0, MAXX*MAXY);
440 memory_region_reset_dirty(&s->vram_mem, 0, MAXX * MAXY * (1 + 4 + 4),
446 static uint64_t tcx_dac_readl(void *opaque, hwaddr addr,
452 static void tcx_dac_writel(void *opaque, hwaddr addr, uint64_t val,
455 TCXState *s = opaque;
459 s->dac_index = val >> 24;
463 switch (s->dac_state) {
465 s->r[s->dac_index] = val >> 24;
466 update_palette_entries(s, s->dac_index, s->dac_index + 1);
470 s->g[s->dac_index] = val >> 24;
471 update_palette_entries(s, s->dac_index, s->dac_index + 1);
475 s->b[s->dac_index] = val >> 24;
476 update_palette_entries(s, s->dac_index, s->dac_index + 1);
477 s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
488 static const MemoryRegionOps tcx_dac_ops = {
489 .read = tcx_dac_readl,
490 .write = tcx_dac_writel,
491 .endianness = DEVICE_NATIVE_ENDIAN,
493 .min_access_size = 4,
494 .max_access_size = 4,
498 static uint64_t dummy_readl(void *opaque, hwaddr addr,
504 static void dummy_writel(void *opaque, hwaddr addr,
505 uint64_t val, unsigned size)
509 static const MemoryRegionOps dummy_ops = {
511 .write = dummy_writel,
512 .endianness = DEVICE_NATIVE_ENDIAN,
514 .min_access_size = 4,
515 .max_access_size = 4,
519 static int tcx_init1(SysBusDevice *dev)
521 TCXState *s = FROM_SYSBUS(TCXState, dev);
522 ram_addr_t vram_offset = 0;
526 memory_region_init_ram(&s->vram_mem, "tcx.vram",
527 s->vram_size * (1 + 4 + 4));
528 vmstate_register_ram_global(&s->vram_mem);
529 vram_base = memory_region_get_ram_ptr(&s->vram_mem);
534 memory_region_init_alias(&s->vram_8bit, "tcx.vram.8bit",
535 &s->vram_mem, vram_offset, size);
536 sysbus_init_mmio(dev, &s->vram_8bit);
541 memory_region_init_io(&s->dac, &tcx_dac_ops, s, "tcx.dac", TCX_DAC_NREGS);
542 sysbus_init_mmio(dev, &s->dac);
545 memory_region_init_io(&s->tec, &dummy_ops, s, "tcx.tec", TCX_TEC_NREGS);
546 sysbus_init_mmio(dev, &s->tec);
547 /* THC: NetBSD writes here even with 8-bit display: dummy */
548 memory_region_init_io(&s->thc24, &dummy_ops, s, "tcx.thc24",
550 sysbus_init_mmio(dev, &s->thc24);
552 if (s->depth == 24) {
554 size = s->vram_size * 4;
555 s->vram24 = (uint32_t *)vram_base;
556 s->vram24_offset = vram_offset;
557 memory_region_init_alias(&s->vram_24bit, "tcx.vram.24bit",
558 &s->vram_mem, vram_offset, size);
559 sysbus_init_mmio(dev, &s->vram_24bit);
564 size = s->vram_size * 4;
565 s->cplane = (uint32_t *)vram_base;
566 s->cplane_offset = vram_offset;
567 memory_region_init_alias(&s->vram_cplane, "tcx.vram.cplane",
568 &s->vram_mem, vram_offset, size);
569 sysbus_init_mmio(dev, &s->vram_cplane);
571 s->con = graphic_console_init(tcx24_update_display,
572 tcx24_invalidate_display,
573 tcx24_screen_dump, NULL, s);
575 /* THC 8 bit (dummy) */
576 memory_region_init_io(&s->thc8, &dummy_ops, s, "tcx.thc8",
578 sysbus_init_mmio(dev, &s->thc8);
580 s->con = graphic_console_init(tcx_update_display,
581 tcx_invalidate_display,
582 tcx_screen_dump, NULL, s);
585 qemu_console_resize(s->con, s->width, s->height);
589 static void tcx_screen_dump(void *opaque, const char *filename, bool cswitch,
592 TCXState *s = opaque;
597 f = fopen(filename, "wb");
599 error_setg(errp, "failed to open file '%s': %s", filename,
603 ret = fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
608 for(y = 0; y < s->height; y++) {
610 for(x = 0; x < s->width; x++) {
612 ret = fputc(s->r[v], f);
616 ret = fputc(s->g[v], f);
620 ret = fputc(s->b[v], f);
634 error_setg(errp, "failed to write to file '%s': %s", filename,
640 static void tcx24_screen_dump(void *opaque, const char *filename, bool cswitch,
643 TCXState *s = opaque;
646 uint32_t *s24, *cptr, dval;
649 f = fopen(filename, "wb");
651 error_setg(errp, "failed to open file '%s': %s", filename,
655 ret = fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
662 for(y = 0; y < s->height; y++) {
664 for(x = 0; x < s->width; x++, d++, s24++) {
665 if ((*cptr++ & 0xff000000) == 0x03000000) { // 24-bit direct
666 dval = *s24 & 0x00ffffff;
667 ret = fputc((dval >> 16) & 0xff, f);
671 ret = fputc((dval >> 8) & 0xff, f);
675 ret = fputc(dval & 0xff, f);
681 ret = fputc(s->r[v], f);
685 ret = fputc(s->g[v], f);
689 ret = fputc(s->b[v], f);
703 error_setg(errp, "failed to write to file '%s': %s", filename,
709 static Property tcx_properties[] = {
710 DEFINE_PROP_TADDR("addr", TCXState, addr, -1),
711 DEFINE_PROP_HEX32("vram_size", TCXState, vram_size, -1),
712 DEFINE_PROP_UINT16("width", TCXState, width, -1),
713 DEFINE_PROP_UINT16("height", TCXState, height, -1),
714 DEFINE_PROP_UINT16("depth", TCXState, depth, -1),
715 DEFINE_PROP_END_OF_LIST(),
718 static void tcx_class_init(ObjectClass *klass, void *data)
720 DeviceClass *dc = DEVICE_CLASS(klass);
721 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
724 dc->reset = tcx_reset;
725 dc->vmsd = &vmstate_tcx;
726 dc->props = tcx_properties;
729 static const TypeInfo tcx_info = {
731 .parent = TYPE_SYS_BUS_DEVICE,
732 .instance_size = sizeof(TCXState),
733 .class_init = tcx_class_init,
736 static void tcx_register_types(void)
738 type_register_static(&tcx_info);
741 type_init(tcx_register_types)