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/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
28 #include "ui/console.h"
29 #include "ui/pixel_ops.h"
30 #include "hw/loader.h"
31 #include "hw/sysbus.h"
32 #include "qemu/error-report.h"
34 #define TCX_ROM_FILE "QEMU,tcx.bin"
35 #define FCODE_MAX_ROM_SIZE 0x10000
39 #define TCX_DAC_NREGS 16
40 #define TCX_THC_NREGS 0x1000
41 #define TCX_DHC_NREGS 0x4000
42 #define TCX_TEC_NREGS 0x1000
43 #define TCX_ALT_NREGS 0x8000
44 #define TCX_STIP_NREGS 0x800000
45 #define TCX_BLIT_NREGS 0x800000
46 #define TCX_RSTIP_NREGS 0x800000
47 #define TCX_RBLIT_NREGS 0x800000
49 #define TCX_THC_MISC 0x818
50 #define TCX_THC_CURSXY 0x8fc
51 #define TCX_THC_CURSMASK 0x900
52 #define TCX_THC_CURSBITS 0x980
54 #define TYPE_TCX "SUNW,tcx"
55 #define TCX(obj) OBJECT_CHECK(TCXState, (obj), TYPE_TCX)
57 typedef struct TCXState {
58 SysBusDevice parent_obj;
63 uint32_t *vram24, *cplane;
66 MemoryRegion vram_mem;
67 MemoryRegion vram_8bit;
68 MemoryRegion vram_24bit;
71 MemoryRegion vram_cplane;
81 ram_addr_t vram24_offset, cplane_offset;
84 uint32_t palette[260];
85 uint8_t r[260], g[260], b[260];
86 uint16_t width, height, depth;
87 uint8_t dac_index, dac_state;
89 uint32_t cursmask[32];
90 uint32_t cursbits[32];
95 static void tcx_set_dirty(TCXState *s, ram_addr_t addr, int len)
97 memory_region_set_dirty(&s->vram_mem, addr, len);
100 memory_region_set_dirty(&s->vram_mem, s->vram24_offset + addr * 4,
102 memory_region_set_dirty(&s->vram_mem, s->cplane_offset + addr * 4,
107 static int tcx_check_dirty(TCXState *s, DirtyBitmapSnapshot *snap,
108 ram_addr_t addr, int len)
112 ret = memory_region_snapshot_get_dirty(&s->vram_mem, snap, addr, len);
114 if (s->depth == 24) {
115 ret |= memory_region_snapshot_get_dirty(&s->vram_mem, snap,
116 s->vram24_offset + addr * 4, len * 4);
117 ret |= memory_region_snapshot_get_dirty(&s->vram_mem, snap,
118 s->cplane_offset + addr * 4, len * 4);
124 static void update_palette_entries(TCXState *s, int start, int end)
126 DisplaySurface *surface = qemu_console_surface(s->con);
129 for (i = start; i < end; i++) {
130 if (is_surface_bgr(surface)) {
131 s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
133 s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
136 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
139 static void tcx_draw_line32(TCXState *s1, uint8_t *d,
140 const uint8_t *s, int width)
144 uint32_t *p = (uint32_t *)d;
146 for (x = 0; x < width; x++) {
148 *p++ = s1->palette[val];
152 static void tcx_draw_cursor32(TCXState *s1, uint8_t *d,
157 uint32_t *p = (uint32_t *)d;
160 mask = s1->cursmask[y];
161 bits = s1->cursbits[y];
162 len = MIN(width - s1->cursx, 32);
164 for (x = 0; x < len; x++) {
165 if (mask & 0x80000000) {
166 if (bits & 0x80000000) {
167 *p = s1->palette[259];
169 *p = s1->palette[258];
179 XXX Could be much more optimal:
180 * detect if line/page/whole screen is in 24 bit mode
181 * if destination is also BGR, use memcpy
183 static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
184 const uint8_t *s, int width,
185 const uint32_t *cplane,
188 DisplaySurface *surface = qemu_console_surface(s1->con);
191 uint32_t *p = (uint32_t *)d;
193 bgr = is_surface_bgr(surface);
194 for(x = 0; x < width; x++, s++, s24++) {
195 if (be32_to_cpu(*cplane) & 0x03000000) {
196 /* 24-bit direct, BGR order */
203 dval = rgb_to_pixel32bgr(r, g, b);
205 dval = rgb_to_pixel32(r, g, b);
207 /* 8-bit pseudocolor */
209 dval = s1->palette[val];
216 /* Fixed line length 1024 allows us to do nice tricks not possible on
219 static void tcx_update_display(void *opaque)
221 TCXState *ts = opaque;
222 DisplaySurface *surface = qemu_console_surface(ts->con);
224 DirtyBitmapSnapshot *snap = NULL;
225 int y, y_start, dd, ds;
228 if (surface_bits_per_pixel(surface) != 32) {
234 d = surface_data(surface);
236 dd = surface_stride(surface);
239 snap = memory_region_snapshot_and_clear_dirty(&ts->vram_mem, 0x0,
240 memory_region_size(&ts->vram_mem),
243 for (y = 0; y < ts->height; y++, page += ds) {
244 if (tcx_check_dirty(ts, snap, page, ds)) {
248 tcx_draw_line32(ts, d, s, ts->width);
249 if (y >= ts->cursy && y < ts->cursy + 32 && ts->cursx < ts->width) {
250 tcx_draw_cursor32(ts, d, y, ts->width);
254 /* flush to display */
255 dpy_gfx_update(ts->con, 0, y_start,
256 ts->width, y - y_start);
264 /* flush to display */
265 dpy_gfx_update(ts->con, 0, y_start,
266 ts->width, y - y_start);
271 static void tcx24_update_display(void *opaque)
273 TCXState *ts = opaque;
274 DisplaySurface *surface = qemu_console_surface(ts->con);
276 DirtyBitmapSnapshot *snap = NULL;
277 int y, y_start, dd, ds;
279 uint32_t *cptr, *s24;
281 if (surface_bits_per_pixel(surface) != 32) {
287 d = surface_data(surface);
291 dd = surface_stride(surface);
294 snap = memory_region_snapshot_and_clear_dirty(&ts->vram_mem, 0x0,
295 memory_region_size(&ts->vram_mem),
298 for (y = 0; y < ts->height; y++, page += ds) {
299 if (tcx_check_dirty(ts, snap, page, ds)) {
303 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
304 if (y >= ts->cursy && y < ts->cursy+32 && ts->cursx < ts->width) {
305 tcx_draw_cursor32(ts, d, y, ts->width);
309 /* flush to display */
310 dpy_gfx_update(ts->con, 0, y_start,
311 ts->width, y - y_start);
321 /* flush to display */
322 dpy_gfx_update(ts->con, 0, y_start,
323 ts->width, y - y_start);
328 static void tcx_invalidate_display(void *opaque)
330 TCXState *s = opaque;
332 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
333 qemu_console_resize(s->con, s->width, s->height);
336 static void tcx24_invalidate_display(void *opaque)
338 TCXState *s = opaque;
340 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
341 qemu_console_resize(s->con, s->width, s->height);
344 static int vmstate_tcx_post_load(void *opaque, int version_id)
346 TCXState *s = opaque;
348 update_palette_entries(s, 0, 256);
349 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
353 static const VMStateDescription vmstate_tcx = {
356 .minimum_version_id = 4,
357 .post_load = vmstate_tcx_post_load,
358 .fields = (VMStateField[]) {
359 VMSTATE_UINT16(height, TCXState),
360 VMSTATE_UINT16(width, TCXState),
361 VMSTATE_UINT16(depth, TCXState),
362 VMSTATE_BUFFER(r, TCXState),
363 VMSTATE_BUFFER(g, TCXState),
364 VMSTATE_BUFFER(b, TCXState),
365 VMSTATE_UINT8(dac_index, TCXState),
366 VMSTATE_UINT8(dac_state, TCXState),
367 VMSTATE_END_OF_LIST()
371 static void tcx_reset(DeviceState *d)
373 TCXState *s = TCX(d);
375 /* Initialize palette */
376 memset(s->r, 0, 260);
377 memset(s->g, 0, 260);
378 memset(s->b, 0, 260);
379 s->r[255] = s->g[255] = s->b[255] = 255;
380 s->r[256] = s->g[256] = s->b[256] = 255;
381 s->r[258] = s->g[258] = s->b[258] = 255;
382 update_palette_entries(s, 0, 260);
383 memset(s->vram, 0, MAXX*MAXY);
384 memory_region_reset_dirty(&s->vram_mem, 0, MAXX * MAXY * (1 + 4 + 4),
388 s->cursx = 0xf000; /* Put cursor off screen */
392 static uint64_t tcx_dac_readl(void *opaque, hwaddr addr,
395 TCXState *s = opaque;
398 switch (s->dac_state) {
400 val = s->r[s->dac_index] << 24;
404 val = s->g[s->dac_index] << 24;
408 val = s->b[s->dac_index] << 24;
409 s->dac_index = (s->dac_index + 1) & 0xff; /* Index autoincrement */
418 static void tcx_dac_writel(void *opaque, hwaddr addr, uint64_t val,
421 TCXState *s = opaque;
425 case 0: /* Address */
426 s->dac_index = val >> 24;
429 case 4: /* Pixel colours */
430 case 12: /* Overlay (cursor) colours */
432 index = (s->dac_index & 3) + 256;
434 index = s->dac_index;
436 switch (s->dac_state) {
438 s->r[index] = val >> 24;
439 update_palette_entries(s, index, index + 1);
443 s->g[index] = val >> 24;
444 update_palette_entries(s, index, index + 1);
448 s->b[index] = val >> 24;
449 update_palette_entries(s, index, index + 1);
450 s->dac_index = (s->dac_index + 1) & 0xff; /* Index autoincrement */
456 default: /* Control registers */
461 static const MemoryRegionOps tcx_dac_ops = {
462 .read = tcx_dac_readl,
463 .write = tcx_dac_writel,
464 .endianness = DEVICE_NATIVE_ENDIAN,
466 .min_access_size = 4,
467 .max_access_size = 4,
471 static uint64_t tcx_stip_readl(void *opaque, hwaddr addr,
477 static void tcx_stip_writel(void *opaque, hwaddr addr,
478 uint64_t val, unsigned size)
480 TCXState *s = opaque;
487 addr = (addr >> 3) & 0xfffff;
488 col = cpu_to_be32(s->tmpblit);
489 if (s->depth == 24) {
490 for (i = 0; i < 32; i++) {
491 if (val & 0x80000000) {
492 s->vram[addr + i] = s->tmpblit;
493 s->vram24[addr + i] = col;
498 for (i = 0; i < 32; i++) {
499 if (val & 0x80000000) {
500 s->vram[addr + i] = s->tmpblit;
505 tcx_set_dirty(s, addr, 32);
509 static void tcx_rstip_writel(void *opaque, hwaddr addr,
510 uint64_t val, unsigned size)
512 TCXState *s = opaque;
519 addr = (addr >> 3) & 0xfffff;
520 col = cpu_to_be32(s->tmpblit);
521 if (s->depth == 24) {
522 for (i = 0; i < 32; i++) {
523 if (val & 0x80000000) {
524 s->vram[addr + i] = s->tmpblit;
525 s->vram24[addr + i] = col;
526 s->cplane[addr + i] = col;
531 for (i = 0; i < 32; i++) {
532 if (val & 0x80000000) {
533 s->vram[addr + i] = s->tmpblit;
538 tcx_set_dirty(s, addr, 32);
542 static const MemoryRegionOps tcx_stip_ops = {
543 .read = tcx_stip_readl,
544 .write = tcx_stip_writel,
545 .endianness = DEVICE_NATIVE_ENDIAN,
547 .min_access_size = 4,
548 .max_access_size = 4,
552 static const MemoryRegionOps tcx_rstip_ops = {
553 .read = tcx_stip_readl,
554 .write = tcx_rstip_writel,
555 .endianness = DEVICE_NATIVE_ENDIAN,
557 .min_access_size = 4,
558 .max_access_size = 4,
562 static uint64_t tcx_blit_readl(void *opaque, hwaddr addr,
568 static void tcx_blit_writel(void *opaque, hwaddr addr,
569 uint64_t val, unsigned size)
571 TCXState *s = opaque;
578 addr = (addr >> 3) & 0xfffff;
579 adsr = val & 0xffffff;
580 len = ((val >> 24) & 0x1f) + 1;
581 if (adsr == 0xffffff) {
582 memset(&s->vram[addr], s->tmpblit, len);
583 if (s->depth == 24) {
584 val = s->tmpblit & 0xffffff;
585 val = cpu_to_be32(val);
586 for (i = 0; i < len; i++) {
587 s->vram24[addr + i] = val;
591 memcpy(&s->vram[addr], &s->vram[adsr], len);
592 if (s->depth == 24) {
593 memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
596 tcx_set_dirty(s, addr, len);
600 static void tcx_rblit_writel(void *opaque, hwaddr addr,
601 uint64_t val, unsigned size)
603 TCXState *s = opaque;
610 addr = (addr >> 3) & 0xfffff;
611 adsr = val & 0xffffff;
612 len = ((val >> 24) & 0x1f) + 1;
613 if (adsr == 0xffffff) {
614 memset(&s->vram[addr], s->tmpblit, len);
615 if (s->depth == 24) {
616 val = s->tmpblit & 0xffffff;
617 val = cpu_to_be32(val);
618 for (i = 0; i < len; i++) {
619 s->vram24[addr + i] = val;
620 s->cplane[addr + i] = val;
624 memcpy(&s->vram[addr], &s->vram[adsr], len);
625 if (s->depth == 24) {
626 memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
627 memcpy(&s->cplane[addr], &s->cplane[adsr], len * 4);
630 tcx_set_dirty(s, addr, len);
634 static const MemoryRegionOps tcx_blit_ops = {
635 .read = tcx_blit_readl,
636 .write = tcx_blit_writel,
637 .endianness = DEVICE_NATIVE_ENDIAN,
639 .min_access_size = 4,
640 .max_access_size = 4,
644 static const MemoryRegionOps tcx_rblit_ops = {
645 .read = tcx_blit_readl,
646 .write = tcx_rblit_writel,
647 .endianness = DEVICE_NATIVE_ENDIAN,
649 .min_access_size = 4,
650 .max_access_size = 4,
654 static void tcx_invalidate_cursor_position(TCXState *s)
656 int ymin, ymax, start, end;
658 /* invalidate only near the cursor */
660 if (ymin >= s->height) {
663 ymax = MIN(s->height, ymin + 32);
667 tcx_set_dirty(s, start, end - start);
670 static uint64_t tcx_thc_readl(void *opaque, hwaddr addr,
673 TCXState *s = opaque;
676 if (addr == TCX_THC_MISC) {
677 val = s->thcmisc | 0x02000000;
684 static void tcx_thc_writel(void *opaque, hwaddr addr,
685 uint64_t val, unsigned size)
687 TCXState *s = opaque;
689 if (addr == TCX_THC_CURSXY) {
690 tcx_invalidate_cursor_position(s);
691 s->cursx = val >> 16;
693 tcx_invalidate_cursor_position(s);
694 } else if (addr >= TCX_THC_CURSMASK && addr < TCX_THC_CURSMASK + 128) {
695 s->cursmask[(addr - TCX_THC_CURSMASK) >> 2] = val;
696 tcx_invalidate_cursor_position(s);
697 } else if (addr >= TCX_THC_CURSBITS && addr < TCX_THC_CURSBITS + 128) {
698 s->cursbits[(addr - TCX_THC_CURSBITS) >> 2] = val;
699 tcx_invalidate_cursor_position(s);
700 } else if (addr == TCX_THC_MISC) {
706 static const MemoryRegionOps tcx_thc_ops = {
707 .read = tcx_thc_readl,
708 .write = tcx_thc_writel,
709 .endianness = DEVICE_NATIVE_ENDIAN,
711 .min_access_size = 4,
712 .max_access_size = 4,
716 static uint64_t tcx_dummy_readl(void *opaque, hwaddr addr,
722 static void tcx_dummy_writel(void *opaque, hwaddr addr,
723 uint64_t val, unsigned size)
728 static const MemoryRegionOps tcx_dummy_ops = {
729 .read = tcx_dummy_readl,
730 .write = tcx_dummy_writel,
731 .endianness = DEVICE_NATIVE_ENDIAN,
733 .min_access_size = 4,
734 .max_access_size = 4,
738 static const GraphicHwOps tcx_ops = {
739 .invalidate = tcx_invalidate_display,
740 .gfx_update = tcx_update_display,
743 static const GraphicHwOps tcx24_ops = {
744 .invalidate = tcx24_invalidate_display,
745 .gfx_update = tcx24_update_display,
748 static void tcx_initfn(Object *obj)
750 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
751 TCXState *s = TCX(obj);
753 memory_region_init_ram_nomigrate(&s->rom, obj, "tcx.prom", FCODE_MAX_ROM_SIZE,
755 memory_region_set_readonly(&s->rom, true);
756 sysbus_init_mmio(sbd, &s->rom);
758 /* 2/STIP : Stippler */
759 memory_region_init_io(&s->stip, obj, &tcx_stip_ops, s, "tcx.stip",
761 sysbus_init_mmio(sbd, &s->stip);
763 /* 3/BLIT : Blitter */
764 memory_region_init_io(&s->blit, obj, &tcx_blit_ops, s, "tcx.blit",
766 sysbus_init_mmio(sbd, &s->blit);
768 /* 5/RSTIP : Raw Stippler */
769 memory_region_init_io(&s->rstip, obj, &tcx_rstip_ops, s, "tcx.rstip",
771 sysbus_init_mmio(sbd, &s->rstip);
773 /* 6/RBLIT : Raw Blitter */
774 memory_region_init_io(&s->rblit, obj, &tcx_rblit_ops, s, "tcx.rblit",
776 sysbus_init_mmio(sbd, &s->rblit);
779 memory_region_init_io(&s->tec, obj, &tcx_dummy_ops, s, "tcx.tec",
781 sysbus_init_mmio(sbd, &s->tec);
784 memory_region_init_io(&s->dac, obj, &tcx_dac_ops, s, "tcx.dac",
786 sysbus_init_mmio(sbd, &s->dac);
789 memory_region_init_io(&s->thc, obj, &tcx_thc_ops, s, "tcx.thc",
791 sysbus_init_mmio(sbd, &s->thc);
794 memory_region_init_io(&s->dhc, obj, &tcx_dummy_ops, s, "tcx.dhc",
796 sysbus_init_mmio(sbd, &s->dhc);
799 memory_region_init_io(&s->alt, obj, &tcx_dummy_ops, s, "tcx.alt",
801 sysbus_init_mmio(sbd, &s->alt);
804 static void tcx_realizefn(DeviceState *dev, Error **errp)
806 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
807 TCXState *s = TCX(dev);
808 ram_addr_t vram_offset = 0;
811 char *fcode_filename;
813 memory_region_init_ram_nomigrate(&s->vram_mem, OBJECT(s), "tcx.vram",
814 s->vram_size * (1 + 4 + 4), &error_fatal);
815 vmstate_register_ram_global(&s->vram_mem);
816 memory_region_set_log(&s->vram_mem, true, DIRTY_MEMORY_VGA);
817 vram_base = memory_region_get_ram_ptr(&s->vram_mem);
819 /* 10/ROM : FCode ROM */
820 vmstate_register_ram_global(&s->rom);
821 fcode_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, TCX_ROM_FILE);
822 if (fcode_filename) {
823 ret = load_image_mr(fcode_filename, &s->rom);
824 g_free(fcode_filename);
825 if (ret < 0 || ret > FCODE_MAX_ROM_SIZE) {
826 warn_report("tcx: could not load prom '%s'", TCX_ROM_FILE);
830 /* 0/DFB8 : 8-bit plane */
833 memory_region_init_alias(&s->vram_8bit, OBJECT(s), "tcx.vram.8bit",
834 &s->vram_mem, vram_offset, size);
835 sysbus_init_mmio(sbd, &s->vram_8bit);
839 /* 1/DFB24 : 24bit plane */
840 size = s->vram_size * 4;
841 s->vram24 = (uint32_t *)vram_base;
842 s->vram24_offset = vram_offset;
843 memory_region_init_alias(&s->vram_24bit, OBJECT(s), "tcx.vram.24bit",
844 &s->vram_mem, vram_offset, size);
845 sysbus_init_mmio(sbd, &s->vram_24bit);
849 /* 4/RDFB32 : Raw Framebuffer */
850 size = s->vram_size * 4;
851 s->cplane = (uint32_t *)vram_base;
852 s->cplane_offset = vram_offset;
853 memory_region_init_alias(&s->vram_cplane, OBJECT(s), "tcx.vram.cplane",
854 &s->vram_mem, vram_offset, size);
855 sysbus_init_mmio(sbd, &s->vram_cplane);
857 /* 9/THC24bits : NetBSD writes here even with 8-bit display: dummy */
859 memory_region_init_io(&s->thc24, OBJECT(s), &tcx_dummy_ops, s,
860 "tcx.thc24", TCX_THC_NREGS);
861 sysbus_init_mmio(sbd, &s->thc24);
864 sysbus_init_irq(sbd, &s->irq);
867 s->con = graphic_console_init(DEVICE(dev), 0, &tcx_ops, s);
869 s->con = graphic_console_init(DEVICE(dev), 0, &tcx24_ops, s);
873 qemu_console_resize(s->con, s->width, s->height);
876 static Property tcx_properties[] = {
877 DEFINE_PROP_UINT32("vram_size", TCXState, vram_size, -1),
878 DEFINE_PROP_UINT16("width", TCXState, width, -1),
879 DEFINE_PROP_UINT16("height", TCXState, height, -1),
880 DEFINE_PROP_UINT16("depth", TCXState, depth, -1),
881 DEFINE_PROP_END_OF_LIST(),
884 static void tcx_class_init(ObjectClass *klass, void *data)
886 DeviceClass *dc = DEVICE_CLASS(klass);
888 dc->realize = tcx_realizefn;
889 dc->reset = tcx_reset;
890 dc->vmsd = &vmstate_tcx;
891 dc->props = tcx_properties;
894 static const TypeInfo tcx_info = {
896 .parent = TYPE_SYS_BUS_DEVICE,
897 .instance_size = sizeof(TCXState),
898 .instance_init = tcx_initfn,
899 .class_init = tcx_class_init,
902 static void tcx_register_types(void)
904 type_register_static(&tcx_info);
907 type_init(tcx_register_types)