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 {
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,
61 static void tcx24_screen_dump(void *opaque, const char *filename, bool cswitch,
64 static void tcx_set_dirty(TCXState *s)
66 memory_region_set_dirty(&s->vram_mem, 0, MAXX * MAXY);
69 static void tcx24_set_dirty(TCXState *s)
71 memory_region_set_dirty(&s->vram_mem, s->vram24_offset, MAXX * MAXY * 4);
72 memory_region_set_dirty(&s->vram_mem, s->cplane_offset, MAXX * MAXY * 4);
75 static void update_palette_entries(TCXState *s, int start, int end)
77 DisplaySurface *surface = qemu_console_surface(s->con);
80 for (i = start; i < end; i++) {
81 switch (surface_bits_per_pixel(surface)) {
84 s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
87 s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
90 s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
93 if (is_surface_bgr(surface)) {
94 s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
96 s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
101 if (s->depth == 24) {
108 static void tcx_draw_line32(TCXState *s1, uint8_t *d,
109 const uint8_t *s, int width)
113 uint32_t *p = (uint32_t *)d;
115 for(x = 0; x < width; x++) {
117 *p++ = s1->palette[val];
121 static void tcx_draw_line16(TCXState *s1, uint8_t *d,
122 const uint8_t *s, int width)
126 uint16_t *p = (uint16_t *)d;
128 for(x = 0; x < width; x++) {
130 *p++ = s1->palette[val];
134 static void tcx_draw_line8(TCXState *s1, uint8_t *d,
135 const uint8_t *s, int width)
140 for(x = 0; x < width; x++) {
142 *d++ = s1->palette[val];
147 XXX Could be much more optimal:
148 * detect if line/page/whole screen is in 24 bit mode
149 * if destination is also BGR, use memcpy
151 static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
152 const uint8_t *s, int width,
153 const uint32_t *cplane,
156 DisplaySurface *surface = qemu_console_surface(s1->con);
159 uint32_t *p = (uint32_t *)d;
162 bgr = is_surface_bgr(surface);
163 for(x = 0; x < width; x++, s++, s24++) {
164 if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
165 // 24-bit direct, BGR order
172 dval = rgb_to_pixel32bgr(r, g, b);
174 dval = rgb_to_pixel32(r, g, b);
177 dval = s1->palette[val];
183 static inline int check_dirty(TCXState *s, ram_addr_t page, ram_addr_t page24,
188 ret = memory_region_get_dirty(&s->vram_mem, page, TARGET_PAGE_SIZE,
190 ret |= memory_region_get_dirty(&s->vram_mem, page24, TARGET_PAGE_SIZE * 4,
192 ret |= memory_region_get_dirty(&s->vram_mem, cpage, TARGET_PAGE_SIZE * 4,
197 static inline void reset_dirty(TCXState *ts, ram_addr_t page_min,
198 ram_addr_t page_max, ram_addr_t page24,
201 memory_region_reset_dirty(&ts->vram_mem,
202 page_min, page_max + TARGET_PAGE_SIZE,
204 memory_region_reset_dirty(&ts->vram_mem,
205 page24 + page_min * 4,
206 page24 + page_max * 4 + TARGET_PAGE_SIZE,
208 memory_region_reset_dirty(&ts->vram_mem,
209 cpage + page_min * 4,
210 cpage + page_max * 4 + TARGET_PAGE_SIZE,
214 /* Fixed line length 1024 allows us to do nice tricks not possible on
216 static void tcx_update_display(void *opaque)
218 TCXState *ts = opaque;
219 DisplaySurface *surface = qemu_console_surface(ts->con);
220 ram_addr_t page, page_min, page_max;
221 int y, y_start, dd, ds;
223 void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
225 if (surface_bits_per_pixel(surface) == 0) {
233 d = surface_data(surface);
235 dd = surface_stride(surface);
238 switch (surface_bits_per_pixel(surface)) {
254 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
255 if (memory_region_get_dirty(&ts->vram_mem, page, TARGET_PAGE_SIZE,
263 f(ts, d, s, ts->width);
266 f(ts, d, s, ts->width);
269 f(ts, d, s, ts->width);
272 f(ts, d, s, ts->width);
277 /* flush to display */
278 dpy_gfx_update(ts->con, 0, y_start,
279 ts->width, y - y_start);
287 /* flush to display */
288 dpy_gfx_update(ts->con, 0, y_start,
289 ts->width, y - y_start);
291 /* reset modified pages */
292 if (page_max >= page_min) {
293 memory_region_reset_dirty(&ts->vram_mem,
294 page_min, page_max + TARGET_PAGE_SIZE,
299 static void tcx24_update_display(void *opaque)
301 TCXState *ts = opaque;
302 DisplaySurface *surface = qemu_console_surface(ts->con);
303 ram_addr_t page, page_min, page_max, cpage, page24;
304 int y, y_start, dd, ds;
306 uint32_t *cptr, *s24;
308 if (surface_bits_per_pixel(surface) != 32) {
313 page24 = ts->vram24_offset;
314 cpage = ts->cplane_offset;
318 d = surface_data(surface);
322 dd = surface_stride(surface);
325 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
326 page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
327 if (check_dirty(ts, page, page24, cpage)) {
334 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
339 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
344 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
349 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
356 /* flush to display */
357 dpy_gfx_update(ts->con, 0, y_start,
358 ts->width, y - y_start);
368 /* flush to display */
369 dpy_gfx_update(ts->con, 0, y_start,
370 ts->width, y - y_start);
372 /* reset modified pages */
373 if (page_max >= page_min) {
374 reset_dirty(ts, page_min, page_max, page24, cpage);
378 static void tcx_invalidate_display(void *opaque)
380 TCXState *s = opaque;
383 qemu_console_resize(s->con, s->width, s->height);
386 static void tcx24_invalidate_display(void *opaque)
388 TCXState *s = opaque;
392 qemu_console_resize(s->con, s->width, s->height);
395 static int vmstate_tcx_post_load(void *opaque, int version_id)
397 TCXState *s = opaque;
399 update_palette_entries(s, 0, 256);
400 if (s->depth == 24) {
409 static const VMStateDescription vmstate_tcx = {
412 .minimum_version_id = 4,
413 .minimum_version_id_old = 4,
414 .post_load = vmstate_tcx_post_load,
415 .fields = (VMStateField []) {
416 VMSTATE_UINT16(height, TCXState),
417 VMSTATE_UINT16(width, TCXState),
418 VMSTATE_UINT16(depth, TCXState),
419 VMSTATE_BUFFER(r, TCXState),
420 VMSTATE_BUFFER(g, TCXState),
421 VMSTATE_BUFFER(b, TCXState),
422 VMSTATE_UINT8(dac_index, TCXState),
423 VMSTATE_UINT8(dac_state, TCXState),
424 VMSTATE_END_OF_LIST()
428 static void tcx_reset(DeviceState *d)
430 TCXState *s = container_of(d, TCXState, busdev.qdev);
432 /* Initialize palette */
433 memset(s->r, 0, 256);
434 memset(s->g, 0, 256);
435 memset(s->b, 0, 256);
436 s->r[255] = s->g[255] = s->b[255] = 255;
437 update_palette_entries(s, 0, 256);
438 memset(s->vram, 0, MAXX*MAXY);
439 memory_region_reset_dirty(&s->vram_mem, 0, MAXX * MAXY * (1 + 4 + 4),
445 static uint64_t tcx_dac_readl(void *opaque, hwaddr addr,
451 static void tcx_dac_writel(void *opaque, hwaddr addr, uint64_t val,
454 TCXState *s = opaque;
458 s->dac_index = val >> 24;
462 switch (s->dac_state) {
464 s->r[s->dac_index] = val >> 24;
465 update_palette_entries(s, s->dac_index, s->dac_index + 1);
469 s->g[s->dac_index] = val >> 24;
470 update_palette_entries(s, s->dac_index, s->dac_index + 1);
474 s->b[s->dac_index] = val >> 24;
475 update_palette_entries(s, s->dac_index, s->dac_index + 1);
476 s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
487 static const MemoryRegionOps tcx_dac_ops = {
488 .read = tcx_dac_readl,
489 .write = tcx_dac_writel,
490 .endianness = DEVICE_NATIVE_ENDIAN,
492 .min_access_size = 4,
493 .max_access_size = 4,
497 static uint64_t dummy_readl(void *opaque, hwaddr addr,
503 static void dummy_writel(void *opaque, hwaddr addr,
504 uint64_t val, unsigned size)
508 static const MemoryRegionOps dummy_ops = {
510 .write = dummy_writel,
511 .endianness = DEVICE_NATIVE_ENDIAN,
513 .min_access_size = 4,
514 .max_access_size = 4,
518 static int tcx_init1(SysBusDevice *dev)
520 TCXState *s = FROM_SYSBUS(TCXState, dev);
521 ram_addr_t vram_offset = 0;
525 memory_region_init_ram(&s->vram_mem, "tcx.vram",
526 s->vram_size * (1 + 4 + 4));
527 vmstate_register_ram_global(&s->vram_mem);
528 vram_base = memory_region_get_ram_ptr(&s->vram_mem);
533 memory_region_init_alias(&s->vram_8bit, "tcx.vram.8bit",
534 &s->vram_mem, vram_offset, size);
535 sysbus_init_mmio(dev, &s->vram_8bit);
540 memory_region_init_io(&s->dac, &tcx_dac_ops, s, "tcx.dac", TCX_DAC_NREGS);
541 sysbus_init_mmio(dev, &s->dac);
544 memory_region_init_io(&s->tec, &dummy_ops, s, "tcx.tec", TCX_TEC_NREGS);
545 sysbus_init_mmio(dev, &s->tec);
546 /* THC: NetBSD writes here even with 8-bit display: dummy */
547 memory_region_init_io(&s->thc24, &dummy_ops, s, "tcx.thc24",
549 sysbus_init_mmio(dev, &s->thc24);
551 if (s->depth == 24) {
553 size = s->vram_size * 4;
554 s->vram24 = (uint32_t *)vram_base;
555 s->vram24_offset = vram_offset;
556 memory_region_init_alias(&s->vram_24bit, "tcx.vram.24bit",
557 &s->vram_mem, vram_offset, size);
558 sysbus_init_mmio(dev, &s->vram_24bit);
563 size = s->vram_size * 4;
564 s->cplane = (uint32_t *)vram_base;
565 s->cplane_offset = vram_offset;
566 memory_region_init_alias(&s->vram_cplane, "tcx.vram.cplane",
567 &s->vram_mem, vram_offset, size);
568 sysbus_init_mmio(dev, &s->vram_cplane);
570 s->con = graphic_console_init(tcx24_update_display,
571 tcx24_invalidate_display,
572 tcx24_screen_dump, NULL, s);
574 /* THC 8 bit (dummy) */
575 memory_region_init_io(&s->thc8, &dummy_ops, s, "tcx.thc8",
577 sysbus_init_mmio(dev, &s->thc8);
579 s->con = graphic_console_init(tcx_update_display,
580 tcx_invalidate_display,
581 tcx_screen_dump, NULL, s);
584 qemu_console_resize(s->con, s->width, s->height);
588 static void tcx_screen_dump(void *opaque, const char *filename, bool cswitch,
591 TCXState *s = opaque;
596 f = fopen(filename, "wb");
598 error_setg(errp, "failed to open file '%s': %s", filename,
602 ret = fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
607 for(y = 0; y < s->height; y++) {
609 for(x = 0; x < s->width; x++) {
611 ret = fputc(s->r[v], f);
615 ret = fputc(s->g[v], f);
619 ret = fputc(s->b[v], f);
633 error_setg(errp, "failed to write to file '%s': %s", filename,
639 static void tcx24_screen_dump(void *opaque, const char *filename, bool cswitch,
642 TCXState *s = opaque;
645 uint32_t *s24, *cptr, dval;
648 f = fopen(filename, "wb");
650 error_setg(errp, "failed to open file '%s': %s", filename,
654 ret = fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
661 for(y = 0; y < s->height; y++) {
663 for(x = 0; x < s->width; x++, d++, s24++) {
664 if ((*cptr++ & 0xff000000) == 0x03000000) { // 24-bit direct
665 dval = *s24 & 0x00ffffff;
666 ret = fputc((dval >> 16) & 0xff, f);
670 ret = fputc((dval >> 8) & 0xff, f);
674 ret = fputc(dval & 0xff, f);
680 ret = fputc(s->r[v], f);
684 ret = fputc(s->g[v], f);
688 ret = fputc(s->b[v], f);
702 error_setg(errp, "failed to write to file '%s': %s", filename,
708 static Property tcx_properties[] = {
709 DEFINE_PROP_HEX32("vram_size", TCXState, vram_size, -1),
710 DEFINE_PROP_UINT16("width", TCXState, width, -1),
711 DEFINE_PROP_UINT16("height", TCXState, height, -1),
712 DEFINE_PROP_UINT16("depth", TCXState, depth, -1),
713 DEFINE_PROP_END_OF_LIST(),
716 static void tcx_class_init(ObjectClass *klass, void *data)
718 DeviceClass *dc = DEVICE_CLASS(klass);
719 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
722 dc->reset = tcx_reset;
723 dc->vmsd = &vmstate_tcx;
724 dc->props = tcx_properties;
727 static const TypeInfo tcx_info = {
729 .parent = TYPE_SYS_BUS_DEVICE,
730 .instance_size = sizeof(TCXState),
731 .class_init = tcx_class_init,
734 static void tcx_register_types(void)
736 type_register_static(&tcx_info);
739 type_init(tcx_register_types)