]> git.proxmox.com Git - mirror_qemu.git/blobdiff - hw/tcx.c
VM load/save support for PPC devices
[mirror_qemu.git] / hw / tcx.c
index 675c74d9fe8f1bdcd6f4d99605cc3ec382cc47b8..004685bc2b295e63c4105e72bc841a5a97d3e64f 100644 (file)
--- a/hw/tcx.c
+++ b/hw/tcx.c
@@ -1,8 +1,8 @@
 /*
  * QEMU TCX Frame buffer
- * 
+ *
  * Copyright (c) 2003-2005 Fabrice Bellard
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
@@ -21,7 +21,9 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include "vl.h"
+#include "hw.h"
+#include "sun4m.h"
+#include "console.h"
 #include "pixel_ops.h"
 
 #define MAXX 1024
@@ -34,6 +36,7 @@
 typedef struct TCXState {
     target_phys_addr_t addr;
     DisplayState *ds;
+    QEMUConsole *console;
     uint8_t *vram;
     uint32_t *vram24, *cplane;
     ram_addr_t vram_offset, vram24_offset, cplane_offset;
@@ -52,7 +55,7 @@ static void update_palette_entries(TCXState *s, int start, int end)
 {
     int i;
     for(i = start; i < end; i++) {
-        switch(s->ds->depth) {
+        switch(ds_get_bits_per_pixel(s->ds)) {
         default:
         case 8:
             s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
@@ -83,57 +86,72 @@ static void update_palette_entries(TCXState *s, int start, int end)
         tcx_invalidate_display(s);
 }
 
-static void tcx_draw_line32(TCXState *s1, uint8_t *d, 
-                           const uint8_t *s, int width)
+static void tcx_draw_line32(TCXState *s1, uint8_t *d,
+                            const uint8_t *s, int width)
 {
     int x;
     uint8_t val;
     uint32_t *p = (uint32_t *)d;
 
     for(x = 0; x < width; x++) {
-       val = *s++;
+        val = *s++;
         *p++ = s1->palette[val];
     }
 }
 
-static void tcx_draw_line16(TCXState *s1, uint8_t *d, 
-                           const uint8_t *s, int width)
+static void tcx_draw_line16(TCXState *s1, uint8_t *d,
+                            const uint8_t *s, int width)
 {
     int x;
     uint8_t val;
     uint16_t *p = (uint16_t *)d;
 
     for(x = 0; x < width; x++) {
-       val = *s++;
+        val = *s++;
         *p++ = s1->palette[val];
     }
 }
 
-static void tcx_draw_line8(TCXState *s1, uint8_t *d, 
-                          const uint8_t *s, int width)
+static void tcx_draw_line8(TCXState *s1, uint8_t *d,
+                           const uint8_t *s, int width)
 {
     int x;
     uint8_t val;
 
     for(x = 0; x < width; x++) {
-       val = *s++;
+        val = *s++;
         *d++ = s1->palette[val];
     }
 }
 
+/*
+  XXX Could be much more optimal:
+  * detect if line/page/whole screen is in 24 bit mode
+  * if destination is also BGR, use memcpy
+  */
 static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
                                      const uint8_t *s, int width,
                                      const uint32_t *cplane,
                                      const uint32_t *s24)
 {
-    int x;
-    uint8_t val;
+    int x, bgr, r, g, b;
+    uint8_t val, *p8;
     uint32_t *p = (uint32_t *)d;
     uint32_t dval;
 
+    bgr = s1->ds->bgr;
     for(x = 0; x < width; x++, s++, s24++) {
-        if ((bswap32(*cplane++) & 0xff000000) == 0x03000000) { // 24-bit direct
-            dval = bswap32(*s24) & 0x00ffffff;
+        if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
+            // 24-bit direct, BGR order
+            p8 = (uint8_t *)s24;
+            p8++;
+            b = *p8++;
+            g = *p8++;
+            r = *p8++;
+            if (bgr)
+                dval = rgb_to_pixel32bgr(r, g, b);
+            else
+                dval = rgb_to_pixel32(r, g, b);
         } else {
             val = *s;
             dval = s1->palette[val];
@@ -142,7 +160,7 @@ static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
     }
 }
 
-static inline int check_dirty(TCXState *ts, ram_addr_t page, ram_addr_t page24,
+static inline int check_dirty(ram_addr_t page, ram_addr_t page24,
                               ram_addr_t cpage)
 {
     int ret;
@@ -180,70 +198,70 @@ static void tcx_update_display(void *opaque)
     ram_addr_t page, page_min, page_max;
     int y, y_start, dd, ds;
     uint8_t *d, *s;
-    void (*f)(TCXState *s1, uint8_t *d, const uint8_t *s, int width);
+    void (*f)(TCXState *s1, uint8_t *dst, const uint8_t *src, int width);
 
-    if (ts->ds->depth == 0)
-       return;
+    if (ds_get_bits_per_pixel(ts->ds) == 0)
+        return;
     page = ts->vram_offset;
     y_start = -1;
     page_min = 0xffffffff;
     page_max = 0;
-    d = ts->ds->data;
+    d = ds_get_data(ts->ds);
     s = ts->vram;
-    dd = ts->ds->linesize;
+    dd = ds_get_linesize(ts->ds);
     ds = 1024;
 
-    switch (ts->ds->depth) {
+    switch (ds_get_bits_per_pixel(ts->ds)) {
     case 32:
-       f = tcx_draw_line32;
-       break;
+        f = tcx_draw_line32;
+        break;
     case 15:
     case 16:
-       f = tcx_draw_line16;
-       break;
+        f = tcx_draw_line16;
+        break;
     default:
     case 8:
-       f = tcx_draw_line8;
-       break;
+        f = tcx_draw_line8;
+        break;
     case 0:
-       return;
+        return;
     }
-    
+
     for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
-       if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
-           if (y_start < 0)
+        if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
+            if (y_start < 0)
                 y_start = y;
             if (page < page_min)
                 page_min = page;
             if (page > page_max)
                 page_max = page;
-           f(ts, d, s, ts->width);
-           d += dd;
-           s += ds;
-           f(ts, d, s, ts->width);
-           d += dd;
-           s += ds;
-           f(ts, d, s, ts->width);
-           d += dd;
-           s += ds;
-           f(ts, d, s, ts->width);
-           d += dd;
-           s += ds;
-       } else {
+            f(ts, d, s, ts->width);
+            d += dd;
+            s += ds;
+            f(ts, d, s, ts->width);
+            d += dd;
+            s += ds;
+            f(ts, d, s, ts->width);
+            d += dd;
+            s += ds;
+            f(ts, d, s, ts->width);
+            d += dd;
+            s += ds;
+        } else {
             if (y_start >= 0) {
                 /* flush to display */
-                dpy_update(ts->ds, 0, y_start, 
+                dpy_update(ts->ds, 0, y_start,
                            ts->width, y - y_start);
                 y_start = -1;
             }
-           d += dd * 4;
-           s += ds * 4;
-       }
+            d += dd * 4;
+            s += ds * 4;
+        }
     }
     if (y_start >= 0) {
-       /* flush to display */
-       dpy_update(ts->ds, 0, y_start, 
-                  ts->width, y - y_start);
+        /* flush to display */
+        dpy_update(ts->ds, 0, y_start,
+                   ts->width, y - y_start);
     }
     /* reset modified pages */
     if (page_min <= page_max) {
@@ -260,7 +278,7 @@ static void tcx24_update_display(void *opaque)
     uint8_t *d, *s;
     uint32_t *cptr, *s24;
 
-    if (ts->ds->depth != 32)
+    if (ds_get_bits_per_pixel(ts->ds) != 32)
             return;
     page = ts->vram_offset;
     page24 = ts->vram24_offset;
@@ -268,16 +286,16 @@ static void tcx24_update_display(void *opaque)
     y_start = -1;
     page_min = 0xffffffff;
     page_max = 0;
-    d = ts->ds->data;
+    d = ds_get_data(ts->ds);
     s = ts->vram;
     s24 = ts->vram24;
     cptr = ts->cplane;
-    dd = ts->ds->linesize;
+    dd = ds_get_linesize(ts->ds);
     ds = 1024;
 
     for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE,
             page24 += TARGET_PAGE_SIZE, cpage += TARGET_PAGE_SIZE) {
-        if (check_dirty(ts, page, page24, cpage)) {
+        if (check_dirty(page, page24, cpage)) {
             if (y_start < 0)
                 y_start = y;
             if (page < page_min)
@@ -334,7 +352,7 @@ static void tcx_invalidate_display(void *opaque)
     int i;
 
     for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
-       cpu_physical_memory_set_dirty(s->vram_offset + i);
+        cpu_physical_memory_set_dirty(s->vram_offset + i);
     }
 }
 
@@ -353,13 +371,10 @@ static void tcx24_invalidate_display(void *opaque)
 static void tcx_save(QEMUFile *f, void *opaque)
 {
     TCXState *s = opaque;
-    
-    qemu_put_be32s(f, (uint32_t *)&s->vram);
-    qemu_put_be32s(f, (uint32_t *)&s->vram24);
-    qemu_put_be32s(f, (uint32_t *)&s->cplane);
-    qemu_put_be16s(f, (uint16_t *)&s->height);
-    qemu_put_be16s(f, (uint16_t *)&s->width);
-    qemu_put_be16s(f, (uint16_t *)&s->depth);
+
+    qemu_put_be16s(f, &s->height);
+    qemu_put_be16s(f, &s->width);
+    qemu_put_be16s(f, &s->depth);
     qemu_put_buffer(f, s->r, 256);
     qemu_put_buffer(f, s->g, 256);
     qemu_put_buffer(f, s->b, 256);
@@ -370,16 +385,19 @@ static void tcx_save(QEMUFile *f, void *opaque)
 static int tcx_load(QEMUFile *f, void *opaque, int version_id)
 {
     TCXState *s = opaque;
-    
-    if (version_id != 3)
+    uint32_t dummy;
+
+    if (version_id != 3 && version_id != 4)
         return -EINVAL;
 
-    qemu_get_be32s(f, (uint32_t *)&s->vram);
-    qemu_get_be32s(f, (uint32_t *)&s->vram24);
-    qemu_get_be32s(f, (uint32_t *)&s->cplane);
-    qemu_get_be16s(f, (uint16_t *)&s->height);
-    qemu_get_be16s(f, (uint16_t *)&s->width);
-    qemu_get_be16s(f, (uint16_t *)&s->depth);
+    if (version_id == 3) {
+        qemu_get_be32s(f, &dummy);
+        qemu_get_be32s(f, &dummy);
+        qemu_get_be32s(f, &dummy);
+    }
+    qemu_get_be16s(f, &s->height);
+    qemu_get_be16s(f, &s->width);
+    qemu_get_be16s(f, &s->depth);
     qemu_get_buffer(f, s->r, 256);
     qemu_get_buffer(f, s->g, 256);
     qemu_get_buffer(f, s->b, 256);
@@ -419,50 +437,48 @@ static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
 static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
 {
     TCXState *s = opaque;
-    uint32_t saddr;
 
-    saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
-    switch (saddr) {
+    switch (addr) {
     case 0:
-       s->dac_index = val >> 24;
-       s->dac_state = 0;
-       break;
-    case 1:
-       switch (s->dac_state) {
-       case 0:
-           s->r[s->dac_index] = val >> 24;
+        s->dac_index = val >> 24;
+        s->dac_state = 0;
+        break;
+    case 4:
+        switch (s->dac_state) {
+        case 0:
+            s->r[s->dac_index] = val >> 24;
             update_palette_entries(s, s->dac_index, s->dac_index + 1);
-           s->dac_state++;
-           break;
-       case 1:
-           s->g[s->dac_index] = val >> 24;
+            s->dac_state++;
+            break;
+        case 1:
+            s->g[s->dac_index] = val >> 24;
             update_palette_entries(s, s->dac_index, s->dac_index + 1);
-           s->dac_state++;
-           break;
-       case 2:
-           s->b[s->dac_index] = val >> 24;
+            s->dac_state++;
+            break;
+        case 2:
+            s->b[s->dac_index] = val >> 24;
             update_palette_entries(s, s->dac_index, s->dac_index + 1);
             s->dac_index = (s->dac_index + 1) & 255; // Index autoincrement
-       default:
-           s->dac_state = 0;
-           break;
-       }
-       break;
+        default:
+            s->dac_state = 0;
+            break;
+        }
+        break;
     default:
-       break;
+        break;
     }
     return;
 }
 
 static CPUReadMemoryFunc *tcx_dac_read[3] = {
-    tcx_dac_readl,
-    tcx_dac_readl,
+    NULL,
+    NULL,
     tcx_dac_readl,
 };
 
 static CPUWriteMemoryFunc *tcx_dac_write[3] = {
-    tcx_dac_writel,
-    tcx_dac_writel,
+    NULL,
+    NULL,
     tcx_dac_writel,
 };
 
@@ -477,14 +493,14 @@ static void tcx_dummy_writel(void *opaque, target_phys_addr_t addr,
 }
 
 static CPUReadMemoryFunc *tcx_dummy_read[3] = {
-    tcx_dummy_readl,
-    tcx_dummy_readl,
+    NULL,
+    NULL,
     tcx_dummy_readl,
 };
 
 static CPUWriteMemoryFunc *tcx_dummy_write[3] = {
-    tcx_dummy_writel,
-    tcx_dummy_writel,
+    NULL,
+    NULL,
     tcx_dummy_writel,
 };
 
@@ -514,7 +530,8 @@ void tcx_init(DisplayState *ds, target_phys_addr_t addr, uint8_t *vram_base,
     vram_base += size;
 
     io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
-    cpu_register_physical_memory(addr + 0x00200000ULL, TCX_DAC_NREGS, io_memory);
+    cpu_register_physical_memory(addr + 0x00200000ULL, TCX_DAC_NREGS,
+                                 io_memory);
 
     dummy_memory = cpu_register_io_memory(0, tcx_dummy_read, tcx_dummy_write,
                                           s);
@@ -534,22 +551,24 @@ void tcx_init(DisplayState *ds, target_phys_addr_t addr, uint8_t *vram_base,
         s->cplane = (uint32_t *)vram_base;
         s->cplane_offset = vram_offset;
         cpu_register_physical_memory(addr + 0x0a000000ULL, size, vram_offset);
-        graphic_console_init(s->ds, tcx24_update_display,
-                             tcx24_invalidate_display, tcx24_screen_dump, s);
+        s->console = graphic_console_init(s->ds, tcx24_update_display,
+                                          tcx24_invalidate_display,
+                                          tcx24_screen_dump, NULL, s);
     } else {
         cpu_register_physical_memory(addr + 0x00300000ULL, TCX_THC_NREGS_8,
                                      dummy_memory);
-        graphic_console_init(s->ds, tcx_update_display, tcx_invalidate_display,
-                             tcx_screen_dump, s);
+        s->console = graphic_console_init(s->ds, tcx_update_display,
+                                          tcx_invalidate_display,
+                                          tcx_screen_dump, NULL, s);
     }
     // NetBSD writes here even with 8-bit display
     cpu_register_physical_memory(addr + 0x00301000ULL, TCX_THC_NREGS_24,
                                  dummy_memory);
 
-    register_savevm("tcx", addr, 3, tcx_save, tcx_load, s);
+    register_savevm("tcx", addr, 4, tcx_save, tcx_load, s);
     qemu_register_reset(tcx_reset, s);
     tcx_reset(s);
-    dpy_resize(s->ds, width, height);
+    qemu_console_resize(s->console, width, height);
 }
 
 static void tcx_screen_dump(void *opaque, const char *filename)