xref: /openbmc/qemu/hw/display/cg3.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
19eb08a43SMark Cave-Ayland /*
29eb08a43SMark Cave-Ayland  * QEMU CG3 Frame buffer
39eb08a43SMark Cave-Ayland  *
49eb08a43SMark Cave-Ayland  * Copyright (c) 2012 Bob Breuer
59eb08a43SMark Cave-Ayland  * Copyright (c) 2013 Mark Cave-Ayland
69eb08a43SMark Cave-Ayland  *
79eb08a43SMark Cave-Ayland  * Permission is hereby granted, free of charge, to any person obtaining a copy
89eb08a43SMark Cave-Ayland  * of this software and associated documentation files (the "Software"), to deal
99eb08a43SMark Cave-Ayland  * in the Software without restriction, including without limitation the rights
109eb08a43SMark Cave-Ayland  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
119eb08a43SMark Cave-Ayland  * copies of the Software, and to permit persons to whom the Software is
129eb08a43SMark Cave-Ayland  * furnished to do so, subject to the following conditions:
139eb08a43SMark Cave-Ayland  *
149eb08a43SMark Cave-Ayland  * The above copyright notice and this permission notice shall be included in
159eb08a43SMark Cave-Ayland  * all copies or substantial portions of the Software.
169eb08a43SMark Cave-Ayland  *
179eb08a43SMark Cave-Ayland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
189eb08a43SMark Cave-Ayland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
199eb08a43SMark Cave-Ayland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
209eb08a43SMark Cave-Ayland  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
219eb08a43SMark Cave-Ayland  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
229eb08a43SMark Cave-Ayland  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
239eb08a43SMark Cave-Ayland  * THE SOFTWARE.
249eb08a43SMark Cave-Ayland  */
259eb08a43SMark Cave-Ayland 
2647df5154SPeter Maydell #include "qemu/osdep.h"
272c65db5eSPaolo Bonzini #include "qemu/datadir.h"
28da34e65cSMarkus Armbruster #include "qapi/error.h"
299eb08a43SMark Cave-Ayland #include "qemu/error-report.h"
309eb08a43SMark Cave-Ayland #include "ui/console.h"
319eb08a43SMark Cave-Ayland #include "hw/sysbus.h"
32d6454270SMarkus Armbruster #include "migration/vmstate.h"
3364552b6bSMarkus Armbruster #include "hw/irq.h"
349eb08a43SMark Cave-Ayland #include "hw/loader.h"
35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3603dd024fSPaolo Bonzini #include "qemu/log.h"
370b8fa32fSMarkus Armbruster #include "qemu/module.h"
3885664cf0SPhilippe Mathieu-Daudé #include "trace.h"
39db1015e9SEduardo Habkost #include "qom/object.h"
409eb08a43SMark Cave-Ayland 
419eb08a43SMark Cave-Ayland /* Change to 1 to enable debugging */
429eb08a43SMark Cave-Ayland #define DEBUG_CG3 0
439eb08a43SMark Cave-Ayland 
449eb08a43SMark Cave-Ayland #define CG3_ROM_FILE  "QEMU,cgthree.bin"
459eb08a43SMark Cave-Ayland #define FCODE_MAX_ROM_SIZE 0x10000
469eb08a43SMark Cave-Ayland 
479eb08a43SMark Cave-Ayland #define CG3_REG_SIZE            0x20
489eb08a43SMark Cave-Ayland 
499eb08a43SMark Cave-Ayland #define CG3_REG_BT458_ADDR      0x0
509eb08a43SMark Cave-Ayland #define CG3_REG_BT458_COLMAP    0x4
519eb08a43SMark Cave-Ayland #define CG3_REG_FBC_CTRL        0x10
529eb08a43SMark Cave-Ayland #define CG3_REG_FBC_STATUS      0x11
539eb08a43SMark Cave-Ayland #define CG3_REG_FBC_CURSTART    0x12
549eb08a43SMark Cave-Ayland #define CG3_REG_FBC_CUREND      0x13
559eb08a43SMark Cave-Ayland #define CG3_REG_FBC_VCTRL       0x14
569eb08a43SMark Cave-Ayland 
579eb08a43SMark Cave-Ayland /* Control register flags */
589eb08a43SMark Cave-Ayland #define CG3_CR_ENABLE_INTS      0x80
599eb08a43SMark Cave-Ayland 
609eb08a43SMark Cave-Ayland /* Status register flags */
619eb08a43SMark Cave-Ayland #define CG3_SR_PENDING_INT      0x80
629eb08a43SMark Cave-Ayland #define CG3_SR_1152_900_76_B    0x60
639eb08a43SMark Cave-Ayland #define CG3_SR_ID_COLOR         0x01
649eb08a43SMark Cave-Ayland 
659eb08a43SMark Cave-Ayland #define CG3_VRAM_SIZE 0x100000
669eb08a43SMark Cave-Ayland #define CG3_VRAM_OFFSET 0x800000
679eb08a43SMark Cave-Ayland 
689eb08a43SMark Cave-Ayland #define TYPE_CG3 "cgthree"
698063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(CG3State, CG3)
709eb08a43SMark Cave-Ayland 
71db1015e9SEduardo Habkost struct CG3State {
729eb08a43SMark Cave-Ayland     SysBusDevice parent_obj;
739eb08a43SMark Cave-Ayland 
749eb08a43SMark Cave-Ayland     QemuConsole *con;
759eb08a43SMark Cave-Ayland     qemu_irq irq;
769eb08a43SMark Cave-Ayland     hwaddr prom_addr;
779eb08a43SMark Cave-Ayland     MemoryRegion vram_mem;
789eb08a43SMark Cave-Ayland     MemoryRegion rom;
799eb08a43SMark Cave-Ayland     MemoryRegion reg;
809eb08a43SMark Cave-Ayland     uint32_t vram_size;
819eb08a43SMark Cave-Ayland     int full_update;
829eb08a43SMark Cave-Ayland     uint8_t regs[16];
839eb08a43SMark Cave-Ayland     uint8_t r[256], g[256], b[256];
849eb08a43SMark Cave-Ayland     uint16_t width, height, depth;
859eb08a43SMark Cave-Ayland     uint8_t dac_index, dac_state;
86db1015e9SEduardo Habkost };
879eb08a43SMark Cave-Ayland 
cg3_update_display(void * opaque)889eb08a43SMark Cave-Ayland static void cg3_update_display(void *opaque)
899eb08a43SMark Cave-Ayland {
909eb08a43SMark Cave-Ayland     CG3State *s = opaque;
919eb08a43SMark Cave-Ayland     DisplaySurface *surface = qemu_console_surface(s->con);
929eb08a43SMark Cave-Ayland     const uint8_t *pix;
939eb08a43SMark Cave-Ayland     uint32_t *data;
949eb08a43SMark Cave-Ayland     uint32_t dval;
959eb08a43SMark Cave-Ayland     int x, y, y_start;
969eb08a43SMark Cave-Ayland     unsigned int width, height;
97344a68bfSMark Cave-Ayland     ram_addr_t page;
98344a68bfSMark Cave-Ayland     DirtyBitmapSnapshot *snap = NULL;
999eb08a43SMark Cave-Ayland 
1009eb08a43SMark Cave-Ayland     if (surface_bits_per_pixel(surface) != 32) {
1019eb08a43SMark Cave-Ayland         return;
1029eb08a43SMark Cave-Ayland     }
1039eb08a43SMark Cave-Ayland     width = s->width;
1049eb08a43SMark Cave-Ayland     height = s->height;
1059eb08a43SMark Cave-Ayland 
1069eb08a43SMark Cave-Ayland     y_start = -1;
1079eb08a43SMark Cave-Ayland     pix = memory_region_get_ram_ptr(&s->vram_mem);
1089eb08a43SMark Cave-Ayland     data = (uint32_t *)surface_data(surface);
1099eb08a43SMark Cave-Ayland 
110344a68bfSMark Cave-Ayland     if (!s->full_update) {
111344a68bfSMark Cave-Ayland         snap = memory_region_snapshot_and_clear_dirty(&s->vram_mem, 0x0,
112344a68bfSMark Cave-Ayland                                               memory_region_size(&s->vram_mem),
113344a68bfSMark Cave-Ayland                                               DIRTY_MEMORY_VGA);
114344a68bfSMark Cave-Ayland     }
115344a68bfSMark Cave-Ayland 
1169eb08a43SMark Cave-Ayland     for (y = 0; y < height; y++) {
117344a68bfSMark Cave-Ayland         int update;
1189eb08a43SMark Cave-Ayland 
1198eb57ae3SMark Cave-Ayland         page = (ram_addr_t)y * width;
120344a68bfSMark Cave-Ayland 
121344a68bfSMark Cave-Ayland         if (s->full_update) {
122344a68bfSMark Cave-Ayland             update = 1;
123344a68bfSMark Cave-Ayland         } else {
124344a68bfSMark Cave-Ayland             update = memory_region_snapshot_get_dirty(&s->vram_mem, snap, page,
125344a68bfSMark Cave-Ayland                                                       width);
126344a68bfSMark Cave-Ayland         }
127344a68bfSMark Cave-Ayland 
1289eb08a43SMark Cave-Ayland         if (update) {
1299eb08a43SMark Cave-Ayland             if (y_start < 0) {
1309eb08a43SMark Cave-Ayland                 y_start = y;
1319eb08a43SMark Cave-Ayland             }
1329eb08a43SMark Cave-Ayland 
1339eb08a43SMark Cave-Ayland             for (x = 0; x < width; x++) {
1349eb08a43SMark Cave-Ayland                 dval = *pix++;
1359eb08a43SMark Cave-Ayland                 dval = (s->r[dval] << 16) | (s->g[dval] << 8) | s->b[dval];
1369eb08a43SMark Cave-Ayland                 *data++ = dval;
1379eb08a43SMark Cave-Ayland             }
1389eb08a43SMark Cave-Ayland         } else {
1399eb08a43SMark Cave-Ayland             if (y_start >= 0) {
140344a68bfSMark Cave-Ayland                 dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
1419eb08a43SMark Cave-Ayland                 y_start = -1;
1429eb08a43SMark Cave-Ayland             }
1439eb08a43SMark Cave-Ayland             pix += width;
1449eb08a43SMark Cave-Ayland             data += width;
1459eb08a43SMark Cave-Ayland         }
1469eb08a43SMark Cave-Ayland     }
1479eb08a43SMark Cave-Ayland     s->full_update = 0;
1489eb08a43SMark Cave-Ayland     if (y_start >= 0) {
149344a68bfSMark Cave-Ayland         dpy_gfx_update(s->con, 0, y_start, width, y - y_start);
1509eb08a43SMark Cave-Ayland     }
1519eb08a43SMark Cave-Ayland     /* vsync interrupt? */
1529eb08a43SMark Cave-Ayland     if (s->regs[0] & CG3_CR_ENABLE_INTS) {
1539eb08a43SMark Cave-Ayland         s->regs[1] |= CG3_SR_PENDING_INT;
1549eb08a43SMark Cave-Ayland         qemu_irq_raise(s->irq);
1559eb08a43SMark Cave-Ayland     }
156344a68bfSMark Cave-Ayland     g_free(snap);
1579eb08a43SMark Cave-Ayland }
1589eb08a43SMark Cave-Ayland 
cg3_invalidate_display(void * opaque)1599eb08a43SMark Cave-Ayland static void cg3_invalidate_display(void *opaque)
1609eb08a43SMark Cave-Ayland {
1619eb08a43SMark Cave-Ayland     CG3State *s = opaque;
1629eb08a43SMark Cave-Ayland 
1639eb08a43SMark Cave-Ayland     memory_region_set_dirty(&s->vram_mem, 0, CG3_VRAM_SIZE);
1649eb08a43SMark Cave-Ayland }
1659eb08a43SMark Cave-Ayland 
cg3_reg_read(void * opaque,hwaddr addr,unsigned size)1669eb08a43SMark Cave-Ayland static uint64_t cg3_reg_read(void *opaque, hwaddr addr, unsigned size)
1679eb08a43SMark Cave-Ayland {
1689eb08a43SMark Cave-Ayland     CG3State *s = opaque;
1699eb08a43SMark Cave-Ayland     int val;
1709eb08a43SMark Cave-Ayland 
1719eb08a43SMark Cave-Ayland     switch (addr) {
1729eb08a43SMark Cave-Ayland     case CG3_REG_BT458_ADDR:
1739eb08a43SMark Cave-Ayland     case CG3_REG_BT458_COLMAP:
1749eb08a43SMark Cave-Ayland         val = 0;
1759eb08a43SMark Cave-Ayland         break;
1769eb08a43SMark Cave-Ayland     case CG3_REG_FBC_CTRL:
1779eb08a43SMark Cave-Ayland         val = s->regs[0];
1789eb08a43SMark Cave-Ayland         break;
1799eb08a43SMark Cave-Ayland     case CG3_REG_FBC_STATUS:
1809eb08a43SMark Cave-Ayland         /* monitor ID 6, board type = 1 (color) */
1819eb08a43SMark Cave-Ayland         val = s->regs[1] | CG3_SR_1152_900_76_B | CG3_SR_ID_COLOR;
1829eb08a43SMark Cave-Ayland         break;
183366d4f7eSMark Cave-Ayland     case CG3_REG_FBC_CURSTART ... CG3_REG_SIZE - 1:
1849eb08a43SMark Cave-Ayland         val = s->regs[addr - 0x10];
1859eb08a43SMark Cave-Ayland         break;
1869eb08a43SMark Cave-Ayland     default:
1879eb08a43SMark Cave-Ayland         qemu_log_mask(LOG_UNIMP,
1889eb08a43SMark Cave-Ayland                   "cg3: Unimplemented register read "
1899eb08a43SMark Cave-Ayland                   "reg 0x%" HWADDR_PRIx " size 0x%x\n",
1909eb08a43SMark Cave-Ayland                   addr, size);
1919eb08a43SMark Cave-Ayland         val = 0;
1929eb08a43SMark Cave-Ayland         break;
1939eb08a43SMark Cave-Ayland     }
19485664cf0SPhilippe Mathieu-Daudé     trace_cg3_read(addr, val, size);
19585664cf0SPhilippe Mathieu-Daudé 
1969eb08a43SMark Cave-Ayland     return val;
1979eb08a43SMark Cave-Ayland }
1989eb08a43SMark Cave-Ayland 
cg3_reg_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)1999eb08a43SMark Cave-Ayland static void cg3_reg_write(void *opaque, hwaddr addr, uint64_t val,
2009eb08a43SMark Cave-Ayland                           unsigned size)
2019eb08a43SMark Cave-Ayland {
2029eb08a43SMark Cave-Ayland     CG3State *s = opaque;
2039eb08a43SMark Cave-Ayland     uint8_t regval;
2049eb08a43SMark Cave-Ayland     int i;
2059eb08a43SMark Cave-Ayland 
20685664cf0SPhilippe Mathieu-Daudé     trace_cg3_write(addr, val, size);
2079eb08a43SMark Cave-Ayland     switch (addr) {
2089eb08a43SMark Cave-Ayland     case CG3_REG_BT458_ADDR:
2099eb08a43SMark Cave-Ayland         s->dac_index = val;
2109eb08a43SMark Cave-Ayland         s->dac_state = 0;
2119eb08a43SMark Cave-Ayland         break;
2129eb08a43SMark Cave-Ayland     case CG3_REG_BT458_COLMAP:
2139eb08a43SMark Cave-Ayland         /* This register can be written to as either a long word or a byte */
2149eb08a43SMark Cave-Ayland         if (size == 1) {
2159eb08a43SMark Cave-Ayland             val <<= 24;
2169eb08a43SMark Cave-Ayland         }
2179eb08a43SMark Cave-Ayland 
2189eb08a43SMark Cave-Ayland         for (i = 0; i < size; i++) {
2199eb08a43SMark Cave-Ayland             regval = val >> 24;
2209eb08a43SMark Cave-Ayland 
2219eb08a43SMark Cave-Ayland             switch (s->dac_state) {
2229eb08a43SMark Cave-Ayland             case 0:
2239eb08a43SMark Cave-Ayland                 s->r[s->dac_index] = regval;
2249eb08a43SMark Cave-Ayland                 s->dac_state++;
2259eb08a43SMark Cave-Ayland                 break;
2269eb08a43SMark Cave-Ayland             case 1:
2279eb08a43SMark Cave-Ayland                 s->g[s->dac_index] = regval;
2289eb08a43SMark Cave-Ayland                 s->dac_state++;
2299eb08a43SMark Cave-Ayland                 break;
2309eb08a43SMark Cave-Ayland             case 2:
2319eb08a43SMark Cave-Ayland                 s->b[s->dac_index] = regval;
2329eb08a43SMark Cave-Ayland                 /* Index autoincrement */
2339eb08a43SMark Cave-Ayland                 s->dac_index = (s->dac_index + 1) & 0xff;
234edd7541bSPaolo Bonzini                 /* fall through */
2359eb08a43SMark Cave-Ayland             default:
2369eb08a43SMark Cave-Ayland                 s->dac_state = 0;
2379eb08a43SMark Cave-Ayland                 break;
2389eb08a43SMark Cave-Ayland             }
2399eb08a43SMark Cave-Ayland             val <<= 8;
2409eb08a43SMark Cave-Ayland         }
2419eb08a43SMark Cave-Ayland         s->full_update = 1;
2429eb08a43SMark Cave-Ayland         break;
2439eb08a43SMark Cave-Ayland     case CG3_REG_FBC_CTRL:
2449eb08a43SMark Cave-Ayland         s->regs[0] = val;
2459eb08a43SMark Cave-Ayland         break;
2469eb08a43SMark Cave-Ayland     case CG3_REG_FBC_STATUS:
2479eb08a43SMark Cave-Ayland         if (s->regs[1] & CG3_SR_PENDING_INT) {
2489eb08a43SMark Cave-Ayland             /* clear interrupt */
2499eb08a43SMark Cave-Ayland             s->regs[1] &= ~CG3_SR_PENDING_INT;
2509eb08a43SMark Cave-Ayland             qemu_irq_lower(s->irq);
2519eb08a43SMark Cave-Ayland         }
2529eb08a43SMark Cave-Ayland         break;
253366d4f7eSMark Cave-Ayland     case CG3_REG_FBC_CURSTART ... CG3_REG_SIZE - 1:
2549eb08a43SMark Cave-Ayland         s->regs[addr - 0x10] = val;
2559eb08a43SMark Cave-Ayland         break;
2569eb08a43SMark Cave-Ayland     default:
2579eb08a43SMark Cave-Ayland         qemu_log_mask(LOG_UNIMP,
2589eb08a43SMark Cave-Ayland                   "cg3: Unimplemented register write "
2599eb08a43SMark Cave-Ayland                   "reg 0x%" HWADDR_PRIx " size 0x%x value 0x%" PRIx64 "\n",
2609eb08a43SMark Cave-Ayland                   addr, size, val);
2619eb08a43SMark Cave-Ayland         break;
2629eb08a43SMark Cave-Ayland     }
2639eb08a43SMark Cave-Ayland }
2649eb08a43SMark Cave-Ayland 
2659eb08a43SMark Cave-Ayland static const MemoryRegionOps cg3_reg_ops = {
2669eb08a43SMark Cave-Ayland     .read = cg3_reg_read,
2679eb08a43SMark Cave-Ayland     .write = cg3_reg_write,
2689eb08a43SMark Cave-Ayland     .endianness = DEVICE_NATIVE_ENDIAN,
2699eb08a43SMark Cave-Ayland     .valid = {
2709eb08a43SMark Cave-Ayland         .min_access_size = 1,
2719eb08a43SMark Cave-Ayland         .max_access_size = 4,
2729eb08a43SMark Cave-Ayland     },
2739eb08a43SMark Cave-Ayland };
2749eb08a43SMark Cave-Ayland 
2759eb08a43SMark Cave-Ayland static const GraphicHwOps cg3_ops = {
2769eb08a43SMark Cave-Ayland     .invalidate = cg3_invalidate_display,
2779eb08a43SMark Cave-Ayland     .gfx_update = cg3_update_display,
2789eb08a43SMark Cave-Ayland };
2799eb08a43SMark Cave-Ayland 
cg3_initfn(Object * obj)280e09c49f4SMark Cave-Ayland static void cg3_initfn(Object *obj)
281e09c49f4SMark Cave-Ayland {
282e09c49f4SMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
283e09c49f4SMark Cave-Ayland     CG3State *s = CG3(obj);
284e09c49f4SMark Cave-Ayland 
28552013bceSPhilippe Mathieu-Daudé     memory_region_init_rom_nomigrate(&s->rom, obj, "cg3.prom",
28652013bceSPhilippe Mathieu-Daudé                                      FCODE_MAX_ROM_SIZE, &error_fatal);
287e09c49f4SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->rom);
288e09c49f4SMark Cave-Ayland 
28981e0ab48SPaolo Bonzini     memory_region_init_io(&s->reg, obj, &cg3_reg_ops, s, "cg3.reg",
290e09c49f4SMark Cave-Ayland                           CG3_REG_SIZE);
291e09c49f4SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->reg);
292e09c49f4SMark Cave-Ayland }
293e09c49f4SMark Cave-Ayland 
cg3_realizefn(DeviceState * dev,Error ** errp)2949eb08a43SMark Cave-Ayland static void cg3_realizefn(DeviceState *dev, Error **errp)
2959eb08a43SMark Cave-Ayland {
2969eb08a43SMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
2979eb08a43SMark Cave-Ayland     CG3State *s = CG3(dev);
2989eb08a43SMark Cave-Ayland     int ret;
2999eb08a43SMark Cave-Ayland     char *fcode_filename;
3009eb08a43SMark Cave-Ayland 
3019eb08a43SMark Cave-Ayland     /* FCode ROM */
3029eb08a43SMark Cave-Ayland     vmstate_register_ram_global(&s->rom);
3039eb08a43SMark Cave-Ayland     fcode_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, CG3_ROM_FILE);
3049eb08a43SMark Cave-Ayland     if (fcode_filename) {
3058c95e1f2SMark Cave-Ayland         ret = load_image_mr(fcode_filename, &s->rom);
30622b2aeb8SShannon Zhao         g_free(fcode_filename);
3079eb08a43SMark Cave-Ayland         if (ret < 0 || ret > FCODE_MAX_ROM_SIZE) {
3080765691eSMarkus Armbruster             warn_report("cg3: could not load prom '%s'", CG3_ROM_FILE);
3099eb08a43SMark Cave-Ayland         }
3109eb08a43SMark Cave-Ayland     }
3119eb08a43SMark Cave-Ayland 
31298a99ce0SPeter Maydell     memory_region_init_ram(&s->vram_mem, NULL, "cg3.vram", s->vram_size,
313f8ed85acSMarkus Armbruster                            &error_fatal);
31474259ae5SPaolo Bonzini     memory_region_set_log(&s->vram_mem, true, DIRTY_MEMORY_VGA);
3159eb08a43SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->vram_mem);
3169eb08a43SMark Cave-Ayland 
3179eb08a43SMark Cave-Ayland     sysbus_init_irq(sbd, &s->irq);
3189eb08a43SMark Cave-Ayland 
3198e5c952bSPhilippe Mathieu-Daudé     s->con = graphic_console_init(dev, 0, &cg3_ops, s);
3209eb08a43SMark Cave-Ayland     qemu_console_resize(s->con, s->width, s->height);
3219eb08a43SMark Cave-Ayland }
3229eb08a43SMark Cave-Ayland 
vmstate_cg3_post_load(void * opaque,int version_id)3239eb08a43SMark Cave-Ayland static int vmstate_cg3_post_load(void *opaque, int version_id)
3249eb08a43SMark Cave-Ayland {
3259eb08a43SMark Cave-Ayland     CG3State *s = opaque;
3269eb08a43SMark Cave-Ayland 
3279eb08a43SMark Cave-Ayland     cg3_invalidate_display(s);
3289eb08a43SMark Cave-Ayland 
3299eb08a43SMark Cave-Ayland     return 0;
3309eb08a43SMark Cave-Ayland }
3319eb08a43SMark Cave-Ayland 
3329eb08a43SMark Cave-Ayland static const VMStateDescription vmstate_cg3 = {
3339eb08a43SMark Cave-Ayland     .name = "cg3",
3349eb08a43SMark Cave-Ayland     .version_id = 1,
3359eb08a43SMark Cave-Ayland     .minimum_version_id = 1,
3369eb08a43SMark Cave-Ayland     .post_load = vmstate_cg3_post_load,
337f0613160SRichard Henderson     .fields = (const VMStateField[]) {
3389eb08a43SMark Cave-Ayland         VMSTATE_UINT16(height, CG3State),
3399eb08a43SMark Cave-Ayland         VMSTATE_UINT16(width, CG3State),
3409eb08a43SMark Cave-Ayland         VMSTATE_UINT16(depth, CG3State),
3419eb08a43SMark Cave-Ayland         VMSTATE_BUFFER(r, CG3State),
3429eb08a43SMark Cave-Ayland         VMSTATE_BUFFER(g, CG3State),
3439eb08a43SMark Cave-Ayland         VMSTATE_BUFFER(b, CG3State),
3449eb08a43SMark Cave-Ayland         VMSTATE_UINT8(dac_index, CG3State),
3459eb08a43SMark Cave-Ayland         VMSTATE_UINT8(dac_state, CG3State),
3469eb08a43SMark Cave-Ayland         VMSTATE_END_OF_LIST()
3479eb08a43SMark Cave-Ayland     }
3489eb08a43SMark Cave-Ayland };
3499eb08a43SMark Cave-Ayland 
cg3_reset(DeviceState * d)3509eb08a43SMark Cave-Ayland static void cg3_reset(DeviceState *d)
3519eb08a43SMark Cave-Ayland {
3529eb08a43SMark Cave-Ayland     CG3State *s = CG3(d);
3539eb08a43SMark Cave-Ayland 
3549eb08a43SMark Cave-Ayland     /* Initialize palette */
3559eb08a43SMark Cave-Ayland     memset(s->r, 0, 256);
3569eb08a43SMark Cave-Ayland     memset(s->g, 0, 256);
3579eb08a43SMark Cave-Ayland     memset(s->b, 0, 256);
3589eb08a43SMark Cave-Ayland 
3599eb08a43SMark Cave-Ayland     s->dac_state = 0;
3609eb08a43SMark Cave-Ayland     s->full_update = 1;
3619eb08a43SMark Cave-Ayland     qemu_irq_lower(s->irq);
3629eb08a43SMark Cave-Ayland }
3639eb08a43SMark Cave-Ayland 
3649eb08a43SMark Cave-Ayland static Property cg3_properties[] = {
3659eb08a43SMark Cave-Ayland     DEFINE_PROP_UINT32("vram-size",    CG3State, vram_size, -1),
3669eb08a43SMark Cave-Ayland     DEFINE_PROP_UINT16("width",        CG3State, width,     -1),
3679eb08a43SMark Cave-Ayland     DEFINE_PROP_UINT16("height",       CG3State, height,    -1),
3689eb08a43SMark Cave-Ayland     DEFINE_PROP_UINT16("depth",        CG3State, depth,     -1),
3699eb08a43SMark Cave-Ayland     DEFINE_PROP_END_OF_LIST(),
3709eb08a43SMark Cave-Ayland };
3719eb08a43SMark Cave-Ayland 
cg3_class_init(ObjectClass * klass,void * data)3729eb08a43SMark Cave-Ayland static void cg3_class_init(ObjectClass *klass, void *data)
3739eb08a43SMark Cave-Ayland {
3749eb08a43SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(klass);
3759eb08a43SMark Cave-Ayland 
3769eb08a43SMark Cave-Ayland     dc->realize = cg3_realizefn;
377*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, cg3_reset);
3789eb08a43SMark Cave-Ayland     dc->vmsd = &vmstate_cg3;
3794f67d30bSMarc-André Lureau     device_class_set_props(dc, cg3_properties);
3809eb08a43SMark Cave-Ayland }
3819eb08a43SMark Cave-Ayland 
3829eb08a43SMark Cave-Ayland static const TypeInfo cg3_info = {
3839eb08a43SMark Cave-Ayland     .name          = TYPE_CG3,
3849eb08a43SMark Cave-Ayland     .parent        = TYPE_SYS_BUS_DEVICE,
3859eb08a43SMark Cave-Ayland     .instance_size = sizeof(CG3State),
386e09c49f4SMark Cave-Ayland     .instance_init = cg3_initfn,
3879eb08a43SMark Cave-Ayland     .class_init    = cg3_class_init,
3889eb08a43SMark Cave-Ayland };
3899eb08a43SMark Cave-Ayland 
cg3_register_types(void)3909eb08a43SMark Cave-Ayland static void cg3_register_types(void)
3919eb08a43SMark Cave-Ayland {
3929eb08a43SMark Cave-Ayland     type_register_static(&cg3_info);
3939eb08a43SMark Cave-Ayland }
3949eb08a43SMark Cave-Ayland 
3959eb08a43SMark Cave-Ayland type_init(cg3_register_types)
396