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 "qemu/datadir.h"
27 #include "qapi/error.h"
28 #include "ui/console.h"
29 #include "ui/pixel_ops.h"
30 #include "hw/loader.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/sysbus.h"
33 #include "migration/vmstate.h"
34 #include "qemu/error-report.h"
35 #include "qemu/module.h"
36 #include "qom/object.h"
38 #define TCX_ROM_FILE "QEMU,tcx.bin"
39 #define FCODE_MAX_ROM_SIZE 0x10000
43 #define TCX_DAC_NREGS 16
44 #define TCX_THC_NREGS 0x1000
45 #define TCX_DHC_NREGS 0x4000
46 #define TCX_TEC_NREGS 0x1000
47 #define TCX_ALT_NREGS 0x8000
48 #define TCX_STIP_NREGS 0x800000
49 #define TCX_BLIT_NREGS 0x800000
50 #define TCX_RSTIP_NREGS 0x800000
51 #define TCX_RBLIT_NREGS 0x800000
53 #define TCX_THC_MISC 0x818
54 #define TCX_THC_CURSXY 0x8fc
55 #define TCX_THC_CURSMASK 0x900
56 #define TCX_THC_CURSBITS 0x980
58 #define TYPE_TCX "sun-tcx"
59 OBJECT_DECLARE_SIMPLE_TYPE(TCXState, TCX)
62 SysBusDevice parent_obj;
67 uint32_t *vram24, *cplane;
70 MemoryRegion vram_mem;
71 MemoryRegion vram_8bit;
72 MemoryRegion vram_24bit;
75 MemoryRegion vram_cplane;
85 ram_addr_t vram24_offset, cplane_offset;
88 uint32_t palette[260];
89 uint8_t r[260], g[260], b[260];
90 uint16_t width, height, depth;
91 uint8_t dac_index, dac_state;
93 uint32_t cursmask[32];
94 uint32_t cursbits[32];
99 static void tcx_set_dirty(TCXState *s, ram_addr_t addr, int len)
101 memory_region_set_dirty(&s->vram_mem, addr, len);
103 if (s->depth == 24) {
104 memory_region_set_dirty(&s->vram_mem, s->vram24_offset + addr * 4,
106 memory_region_set_dirty(&s->vram_mem, s->cplane_offset + addr * 4,
111 static int tcx_check_dirty(TCXState *s, DirtyBitmapSnapshot *snap,
112 ram_addr_t addr, int len)
116 ret = memory_region_snapshot_get_dirty(&s->vram_mem, snap, addr, len);
118 if (s->depth == 24) {
119 ret |= memory_region_snapshot_get_dirty(&s->vram_mem, snap,
120 s->vram24_offset + addr * 4, len * 4);
121 ret |= memory_region_snapshot_get_dirty(&s->vram_mem, snap,
122 s->cplane_offset + addr * 4, len * 4);
128 static void update_palette_entries(TCXState *s, int start, int end)
132 for (i = start; i < end; i++) {
133 s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
135 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
138 static void tcx_draw_line32(TCXState *s1, uint8_t *d,
139 const uint8_t *s, int width)
143 uint32_t *p = (uint32_t *)d;
145 for (x = 0; x < width; x++) {
147 *p++ = s1->palette[val];
151 static void tcx_draw_cursor32(TCXState *s1, uint8_t *d,
156 uint32_t *p = (uint32_t *)d;
159 mask = s1->cursmask[y];
160 bits = s1->cursbits[y];
161 len = MIN(width - s1->cursx, 32);
163 for (x = 0; x < len; x++) {
164 if (mask & 0x80000000) {
165 if (bits & 0x80000000) {
166 *p = s1->palette[259];
168 *p = s1->palette[258];
178 * XXX Could be much more optimal:
179 * detect if line/page/whole screen is in 24 bit mode
181 static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
182 const uint8_t *s, int width,
183 const uint32_t *cplane,
188 uint32_t *p = (uint32_t *)d;
190 for(x = 0; x < width; x++, s++, s24++) {
191 if (be32_to_cpu(*cplane) & 0x03000000) {
192 /* 24-bit direct, BGR order */
198 dval = rgb_to_pixel32(r, g, b);
200 /* 8-bit pseudocolor */
202 dval = s1->palette[val];
209 /* Fixed line length 1024 allows us to do nice tricks not possible on
212 static void tcx_update_display(void *opaque)
214 TCXState *ts = opaque;
215 DisplaySurface *surface = qemu_console_surface(ts->con);
217 DirtyBitmapSnapshot *snap = NULL;
218 int y, y_start, dd, ds;
221 assert(surface_bits_per_pixel(surface) == 32);
225 d = surface_data(surface);
227 dd = surface_stride(surface);
230 snap = memory_region_snapshot_and_clear_dirty(&ts->vram_mem, 0x0,
231 memory_region_size(&ts->vram_mem),
234 for (y = 0; y < ts->height; y++, page += ds) {
235 if (tcx_check_dirty(ts, snap, page, ds)) {
239 tcx_draw_line32(ts, d, s, ts->width);
240 if (y >= ts->cursy && y < ts->cursy + 32 && ts->cursx < ts->width) {
241 tcx_draw_cursor32(ts, d, y, ts->width);
245 /* flush to display */
246 dpy_gfx_update(ts->con, 0, y_start,
247 ts->width, y - y_start);
255 /* flush to display */
256 dpy_gfx_update(ts->con, 0, y_start,
257 ts->width, y - y_start);
262 static void tcx24_update_display(void *opaque)
264 TCXState *ts = opaque;
265 DisplaySurface *surface = qemu_console_surface(ts->con);
267 DirtyBitmapSnapshot *snap = NULL;
268 int y, y_start, dd, ds;
270 uint32_t *cptr, *s24;
272 assert(surface_bits_per_pixel(surface) == 32);
276 d = surface_data(surface);
280 dd = surface_stride(surface);
283 snap = memory_region_snapshot_and_clear_dirty(&ts->vram_mem, 0x0,
284 memory_region_size(&ts->vram_mem),
287 for (y = 0; y < ts->height; y++, page += ds) {
288 if (tcx_check_dirty(ts, snap, page, ds)) {
292 tcx24_draw_line32(ts, d, s, ts->width, cptr, s24);
293 if (y >= ts->cursy && y < ts->cursy+32 && ts->cursx < ts->width) {
294 tcx_draw_cursor32(ts, d, y, ts->width);
298 /* flush to display */
299 dpy_gfx_update(ts->con, 0, y_start,
300 ts->width, y - y_start);
310 /* flush to display */
311 dpy_gfx_update(ts->con, 0, y_start,
312 ts->width, y - y_start);
317 static void tcx_invalidate_display(void *opaque)
319 TCXState *s = opaque;
321 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
322 qemu_console_resize(s->con, s->width, s->height);
325 static void tcx24_invalidate_display(void *opaque)
327 TCXState *s = opaque;
329 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
330 qemu_console_resize(s->con, s->width, s->height);
333 static int vmstate_tcx_post_load(void *opaque, int version_id)
335 TCXState *s = opaque;
337 update_palette_entries(s, 0, 256);
338 tcx_set_dirty(s, 0, memory_region_size(&s->vram_mem));
342 static const VMStateDescription vmstate_tcx = {
345 .minimum_version_id = 4,
346 .post_load = vmstate_tcx_post_load,
347 .fields = (VMStateField[]) {
348 VMSTATE_UINT16(height, TCXState),
349 VMSTATE_UINT16(width, TCXState),
350 VMSTATE_UINT16(depth, TCXState),
351 VMSTATE_BUFFER(r, TCXState),
352 VMSTATE_BUFFER(g, TCXState),
353 VMSTATE_BUFFER(b, TCXState),
354 VMSTATE_UINT8(dac_index, TCXState),
355 VMSTATE_UINT8(dac_state, TCXState),
356 VMSTATE_END_OF_LIST()
360 static void tcx_reset(DeviceState *d)
362 TCXState *s = TCX(d);
364 /* Initialize palette */
365 memset(s->r, 0, 260);
366 memset(s->g, 0, 260);
367 memset(s->b, 0, 260);
368 s->r[255] = s->g[255] = s->b[255] = 255;
369 s->r[256] = s->g[256] = s->b[256] = 255;
370 s->r[258] = s->g[258] = s->b[258] = 255;
371 update_palette_entries(s, 0, 260);
372 memset(s->vram, 0, MAXX*MAXY);
373 memory_region_reset_dirty(&s->vram_mem, 0, MAXX * MAXY * (1 + 4 + 4),
377 s->cursx = 0xf000; /* Put cursor off screen */
381 static uint64_t tcx_dac_readl(void *opaque, hwaddr addr,
384 TCXState *s = opaque;
387 switch (s->dac_state) {
389 val = s->r[s->dac_index] << 24;
393 val = s->g[s->dac_index] << 24;
397 val = s->b[s->dac_index] << 24;
398 s->dac_index = (s->dac_index + 1) & 0xff; /* Index autoincrement */
408 static void tcx_dac_writel(void *opaque, hwaddr addr, uint64_t val,
411 TCXState *s = opaque;
415 case 0: /* Address */
416 s->dac_index = val >> 24;
419 case 4: /* Pixel colours */
420 case 12: /* Overlay (cursor) colours */
422 index = (s->dac_index & 3) + 256;
424 index = s->dac_index;
426 switch (s->dac_state) {
428 s->r[index] = val >> 24;
429 update_palette_entries(s, index, index + 1);
433 s->g[index] = val >> 24;
434 update_palette_entries(s, index, index + 1);
438 s->b[index] = val >> 24;
439 update_palette_entries(s, index, index + 1);
440 s->dac_index = (s->dac_index + 1) & 0xff; /* Index autoincrement */
447 default: /* Control registers */
452 static const MemoryRegionOps tcx_dac_ops = {
453 .read = tcx_dac_readl,
454 .write = tcx_dac_writel,
455 .endianness = DEVICE_NATIVE_ENDIAN,
457 .min_access_size = 4,
458 .max_access_size = 4,
462 static uint64_t tcx_stip_readl(void *opaque, hwaddr addr,
468 static void tcx_stip_writel(void *opaque, hwaddr addr,
469 uint64_t val, unsigned size)
471 TCXState *s = opaque;
478 addr = (addr >> 3) & 0xfffff;
479 col = cpu_to_be32(s->tmpblit);
480 if (s->depth == 24) {
481 for (i = 0; i < 32; i++) {
482 if (val & 0x80000000) {
483 s->vram[addr + i] = s->tmpblit;
484 s->vram24[addr + i] = col;
489 for (i = 0; i < 32; i++) {
490 if (val & 0x80000000) {
491 s->vram[addr + i] = s->tmpblit;
496 tcx_set_dirty(s, addr, 32);
500 static void tcx_rstip_writel(void *opaque, hwaddr addr,
501 uint64_t val, unsigned size)
503 TCXState *s = opaque;
510 addr = (addr >> 3) & 0xfffff;
511 col = cpu_to_be32(s->tmpblit);
512 if (s->depth == 24) {
513 for (i = 0; i < 32; i++) {
514 if (val & 0x80000000) {
515 s->vram[addr + i] = s->tmpblit;
516 s->vram24[addr + i] = col;
517 s->cplane[addr + i] = col;
522 for (i = 0; i < 32; i++) {
523 if (val & 0x80000000) {
524 s->vram[addr + i] = s->tmpblit;
529 tcx_set_dirty(s, addr, 32);
533 static const MemoryRegionOps tcx_stip_ops = {
534 .read = tcx_stip_readl,
535 .write = tcx_stip_writel,
536 .endianness = DEVICE_NATIVE_ENDIAN,
538 .min_access_size = 4,
539 .max_access_size = 4,
542 .min_access_size = 4,
543 .max_access_size = 8,
547 static const MemoryRegionOps tcx_rstip_ops = {
548 .read = tcx_stip_readl,
549 .write = tcx_rstip_writel,
550 .endianness = DEVICE_NATIVE_ENDIAN,
552 .min_access_size = 4,
553 .max_access_size = 4,
556 .min_access_size = 4,
557 .max_access_size = 8,
561 static uint64_t tcx_blit_readl(void *opaque, hwaddr addr,
567 static void tcx_blit_writel(void *opaque, hwaddr addr,
568 uint64_t val, unsigned size)
570 TCXState *s = opaque;
577 addr = (addr >> 3) & 0xfffff;
578 adsr = val & 0xffffff;
579 len = ((val >> 24) & 0x1f) + 1;
580 if (adsr == 0xffffff) {
581 memset(&s->vram[addr], s->tmpblit, len);
582 if (s->depth == 24) {
583 val = s->tmpblit & 0xffffff;
584 val = cpu_to_be32(val);
585 for (i = 0; i < len; i++) {
586 s->vram24[addr + i] = val;
590 memcpy(&s->vram[addr], &s->vram[adsr], len);
591 if (s->depth == 24) {
592 memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
595 tcx_set_dirty(s, addr, len);
599 static void tcx_rblit_writel(void *opaque, hwaddr addr,
600 uint64_t val, unsigned size)
602 TCXState *s = opaque;
609 addr = (addr >> 3) & 0xfffff;
610 adsr = val & 0xffffff;
611 len = ((val >> 24) & 0x1f) + 1;
612 if (adsr == 0xffffff) {
613 memset(&s->vram[addr], s->tmpblit, len);
614 if (s->depth == 24) {
615 val = s->tmpblit & 0xffffff;
616 val = cpu_to_be32(val);
617 for (i = 0; i < len; i++) {
618 s->vram24[addr + i] = val;
619 s->cplane[addr + i] = val;
623 memcpy(&s->vram[addr], &s->vram[adsr], len);
624 if (s->depth == 24) {
625 memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
626 memcpy(&s->cplane[addr], &s->cplane[adsr], len * 4);
629 tcx_set_dirty(s, addr, len);
633 static const MemoryRegionOps tcx_blit_ops = {
634 .read = tcx_blit_readl,
635 .write = tcx_blit_writel,
636 .endianness = DEVICE_NATIVE_ENDIAN,
638 .min_access_size = 4,
639 .max_access_size = 4,
642 .min_access_size = 4,
643 .max_access_size = 8,
647 static const MemoryRegionOps tcx_rblit_ops = {
648 .read = tcx_blit_readl,
649 .write = tcx_rblit_writel,
650 .endianness = DEVICE_NATIVE_ENDIAN,
652 .min_access_size = 4,
653 .max_access_size = 4,
656 .min_access_size = 4,
657 .max_access_size = 8,
661 static void tcx_invalidate_cursor_position(TCXState *s)
663 int ymin, ymax, start, end;
665 /* invalidate only near the cursor */
667 if (ymin >= s->height) {
670 ymax = MIN(s->height, ymin + 32);
674 tcx_set_dirty(s, start, end - start);
677 static uint64_t tcx_thc_readl(void *opaque, hwaddr addr,
680 TCXState *s = opaque;
683 if (addr == TCX_THC_MISC) {
684 val = s->thcmisc | 0x02000000;
691 static void tcx_thc_writel(void *opaque, hwaddr addr,
692 uint64_t val, unsigned size)
694 TCXState *s = opaque;
696 if (addr == TCX_THC_CURSXY) {
697 tcx_invalidate_cursor_position(s);
698 s->cursx = val >> 16;
700 tcx_invalidate_cursor_position(s);
701 } else if (addr >= TCX_THC_CURSMASK && addr < TCX_THC_CURSMASK + 128) {
702 s->cursmask[(addr - TCX_THC_CURSMASK) >> 2] = val;
703 tcx_invalidate_cursor_position(s);
704 } else if (addr >= TCX_THC_CURSBITS && addr < TCX_THC_CURSBITS + 128) {
705 s->cursbits[(addr - TCX_THC_CURSBITS) >> 2] = val;
706 tcx_invalidate_cursor_position(s);
707 } else if (addr == TCX_THC_MISC) {
713 static const MemoryRegionOps tcx_thc_ops = {
714 .read = tcx_thc_readl,
715 .write = tcx_thc_writel,
716 .endianness = DEVICE_NATIVE_ENDIAN,
718 .min_access_size = 4,
719 .max_access_size = 4,
723 static uint64_t tcx_dummy_readl(void *opaque, hwaddr addr,
729 static void tcx_dummy_writel(void *opaque, hwaddr addr,
730 uint64_t val, unsigned size)
735 static const MemoryRegionOps tcx_dummy_ops = {
736 .read = tcx_dummy_readl,
737 .write = tcx_dummy_writel,
738 .endianness = DEVICE_NATIVE_ENDIAN,
740 .min_access_size = 4,
741 .max_access_size = 4,
745 static const GraphicHwOps tcx_ops = {
746 .invalidate = tcx_invalidate_display,
747 .gfx_update = tcx_update_display,
750 static const GraphicHwOps tcx24_ops = {
751 .invalidate = tcx24_invalidate_display,
752 .gfx_update = tcx24_update_display,
755 static void tcx_initfn(Object *obj)
757 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
758 TCXState *s = TCX(obj);
760 memory_region_init_rom_nomigrate(&s->rom, obj, "tcx.prom",
761 FCODE_MAX_ROM_SIZE, &error_fatal);
762 sysbus_init_mmio(sbd, &s->rom);
764 /* 2/STIP : Stippler */
765 memory_region_init_io(&s->stip, obj, &tcx_stip_ops, s, "tcx.stip",
767 sysbus_init_mmio(sbd, &s->stip);
769 /* 3/BLIT : Blitter */
770 memory_region_init_io(&s->blit, obj, &tcx_blit_ops, s, "tcx.blit",
772 sysbus_init_mmio(sbd, &s->blit);
774 /* 5/RSTIP : Raw Stippler */
775 memory_region_init_io(&s->rstip, obj, &tcx_rstip_ops, s, "tcx.rstip",
777 sysbus_init_mmio(sbd, &s->rstip);
779 /* 6/RBLIT : Raw Blitter */
780 memory_region_init_io(&s->rblit, obj, &tcx_rblit_ops, s, "tcx.rblit",
782 sysbus_init_mmio(sbd, &s->rblit);
785 memory_region_init_io(&s->tec, obj, &tcx_dummy_ops, s, "tcx.tec",
787 sysbus_init_mmio(sbd, &s->tec);
790 memory_region_init_io(&s->dac, obj, &tcx_dac_ops, s, "tcx.dac",
792 sysbus_init_mmio(sbd, &s->dac);
795 memory_region_init_io(&s->thc, obj, &tcx_thc_ops, s, "tcx.thc",
797 sysbus_init_mmio(sbd, &s->thc);
800 memory_region_init_io(&s->dhc, obj, &tcx_dummy_ops, s, "tcx.dhc",
802 sysbus_init_mmio(sbd, &s->dhc);
805 memory_region_init_io(&s->alt, obj, &tcx_dummy_ops, s, "tcx.alt",
807 sysbus_init_mmio(sbd, &s->alt);
810 static void tcx_realizefn(DeviceState *dev, Error **errp)
812 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
813 TCXState *s = TCX(dev);
814 ram_addr_t vram_offset = 0;
817 char *fcode_filename;
819 memory_region_init_ram_nomigrate(&s->vram_mem, OBJECT(s), "tcx.vram",
820 s->vram_size * (1 + 4 + 4), &error_fatal);
821 vmstate_register_ram_global(&s->vram_mem);
822 memory_region_set_log(&s->vram_mem, true, DIRTY_MEMORY_VGA);
823 vram_base = memory_region_get_ram_ptr(&s->vram_mem);
825 /* 10/ROM : FCode ROM */
826 vmstate_register_ram_global(&s->rom);
827 fcode_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, TCX_ROM_FILE);
828 if (fcode_filename) {
829 ret = load_image_mr(fcode_filename, &s->rom);
830 g_free(fcode_filename);
831 if (ret < 0 || ret > FCODE_MAX_ROM_SIZE) {
832 warn_report("tcx: could not load prom '%s'", TCX_ROM_FILE);
836 /* 0/DFB8 : 8-bit plane */
839 memory_region_init_alias(&s->vram_8bit, OBJECT(s), "tcx.vram.8bit",
840 &s->vram_mem, vram_offset, size);
841 sysbus_init_mmio(sbd, &s->vram_8bit);
845 /* 1/DFB24 : 24bit plane */
846 size = s->vram_size * 4;
847 s->vram24 = (uint32_t *)vram_base;
848 s->vram24_offset = vram_offset;
849 memory_region_init_alias(&s->vram_24bit, OBJECT(s), "tcx.vram.24bit",
850 &s->vram_mem, vram_offset, size);
851 sysbus_init_mmio(sbd, &s->vram_24bit);
855 /* 4/RDFB32 : Raw Framebuffer */
856 size = s->vram_size * 4;
857 s->cplane = (uint32_t *)vram_base;
858 s->cplane_offset = vram_offset;
859 memory_region_init_alias(&s->vram_cplane, OBJECT(s), "tcx.vram.cplane",
860 &s->vram_mem, vram_offset, size);
861 sysbus_init_mmio(sbd, &s->vram_cplane);
863 /* 9/THC24bits : NetBSD writes here even with 8-bit display: dummy */
865 memory_region_init_io(&s->thc24, OBJECT(s), &tcx_dummy_ops, s,
866 "tcx.thc24", TCX_THC_NREGS);
867 sysbus_init_mmio(sbd, &s->thc24);
870 sysbus_init_irq(sbd, &s->irq);
873 s->con = graphic_console_init(dev, 0, &tcx_ops, s);
875 s->con = graphic_console_init(dev, 0, &tcx24_ops, s);
879 qemu_console_resize(s->con, s->width, s->height);
882 static Property tcx_properties[] = {
883 DEFINE_PROP_UINT32("vram_size", TCXState, vram_size, -1),
884 DEFINE_PROP_UINT16("width", TCXState, width, -1),
885 DEFINE_PROP_UINT16("height", TCXState, height, -1),
886 DEFINE_PROP_UINT16("depth", TCXState, depth, -1),
887 DEFINE_PROP_END_OF_LIST(),
890 static void tcx_class_init(ObjectClass *klass, void *data)
892 DeviceClass *dc = DEVICE_CLASS(klass);
894 dc->realize = tcx_realizefn;
895 dc->reset = tcx_reset;
896 dc->vmsd = &vmstate_tcx;
897 device_class_set_props(dc, tcx_properties);
900 static const TypeInfo tcx_info = {
902 .parent = TYPE_SYS_BUS_DEVICE,
903 .instance_size = sizeof(TCXState),
904 .instance_init = tcx_initfn,
905 .class_init = tcx_class_init,
908 static void tcx_register_types(void)
910 type_register_static(&tcx_info);
913 type_init(tcx_register_types)