| 1 | /* |
| 2 | * NeXT Cube/Station Framebuffer Emulation |
| 3 | * |
| 4 | * Copyright (c) 2011 Bryce Lanham |
| 5 | * |
| 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 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 |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #include "qemu/osdep.h" |
| 25 | #include "qapi/error.h" |
| 26 | #include "ui/console.h" |
| 27 | #include "hw/core/loader.h" |
| 28 | #include "framebuffer.h" |
| 29 | #include "ui/pixel_ops.h" |
| 30 | #include "hw/m68k/next-cube.h" |
| 31 | #include "qom/object.h" |
| 32 | |
| 33 | OBJECT_DECLARE_SIMPLE_TYPE(NeXTFbState, NEXTFB) |
| 34 | |
| 35 | struct NeXTFbState { |
| 36 | SysBusDevice parent_obj; |
| 37 | |
| 38 | MemoryRegion fb_mr; |
| 39 | MemoryRegionSection fbsection; |
| 40 | QemuConsole *con; |
| 41 | |
| 42 | uint32_t cols; |
| 43 | uint32_t rows; |
| 44 | int invalidate; |
| 45 | }; |
| 46 | |
| 47 | static void nextfb_draw_line(void *opaque, uint8_t *d, const uint8_t *s, |
| 48 | int width, int pitch) |
| 49 | { |
| 50 | NeXTFbState *nfbstate = NEXTFB(opaque); |
| 51 | static const uint32_t pal[4] = { |
| 52 | 0xFFFFFFFF, 0xFFAAAAAA, 0xFF555555, 0xFF000000 |
| 53 | }; |
| 54 | uint32_t *buf = (uint32_t *)d; |
| 55 | int i = 0; |
| 56 | |
| 57 | for (i = 0; i < nfbstate->cols / 4; i++) { |
| 58 | int j = i * 4; |
| 59 | uint8_t src = s[i]; |
| 60 | buf[j + 3] = pal[src & 0x3]; |
| 61 | src >>= 2; |
| 62 | buf[j + 2] = pal[src & 0x3]; |
| 63 | src >>= 2; |
| 64 | buf[j + 1] = pal[src & 0x3]; |
| 65 | src >>= 2; |
| 66 | buf[j + 0] = pal[src & 0x3]; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static bool nextfb_update(void *opaque) |
| 71 | { |
| 72 | NeXTFbState *s = NEXTFB(opaque); |
| 73 | int dest_width = 4; |
| 74 | int src_width; |
| 75 | int first = 0; |
| 76 | int last = 0; |
| 77 | DisplaySurface *surface = qemu_console_surface(s->con); |
| 78 | |
| 79 | src_width = s->cols / 4 + 8; |
| 80 | dest_width = s->cols * 4; |
| 81 | |
| 82 | if (s->invalidate) { |
| 83 | framebuffer_update_memory_section(&s->fbsection, &s->fb_mr, 0, |
| 84 | s->cols, src_width); |
| 85 | s->invalidate = 0; |
| 86 | } |
| 87 | |
| 88 | framebuffer_update_display(surface, &s->fbsection, s->cols, s->rows, |
| 89 | src_width, dest_width, 0, 1, nextfb_draw_line, |
| 90 | s, &first, &last); |
| 91 | |
| 92 | qemu_console_update(s->con, 0, 0, s->cols, s->rows); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static void nextfb_invalidate(void *opaque) |
| 98 | { |
| 99 | NeXTFbState *s = NEXTFB(opaque); |
| 100 | s->invalidate = 1; |
| 101 | } |
| 102 | |
| 103 | static const GraphicHwOps nextfb_ops = { |
| 104 | .invalidate = nextfb_invalidate, |
| 105 | .gfx_update = nextfb_update, |
| 106 | }; |
| 107 | |
| 108 | static void nextfb_realize(DeviceState *dev, Error **errp) |
| 109 | { |
| 110 | NeXTFbState *s = NEXTFB(dev); |
| 111 | |
| 112 | memory_region_init_ram(&s->fb_mr, OBJECT(dev), "next-video", 0x1CB100, |
| 113 | &error_fatal); |
| 114 | sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->fb_mr); |
| 115 | |
| 116 | s->invalidate = 1; |
| 117 | s->cols = 1120; |
| 118 | s->rows = 832; |
| 119 | |
| 120 | s->con = qemu_graphic_console_create(dev, 0, &nextfb_ops, s); |
| 121 | qemu_console_resize(s->con, s->cols, s->rows); |
| 122 | } |
| 123 | |
| 124 | static void nextfb_class_init(ObjectClass *oc, const void *data) |
| 125 | { |
| 126 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 127 | |
| 128 | set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories); |
| 129 | dc->realize = nextfb_realize; |
| 130 | |
| 131 | /* Note: This device does not have any state that we have to reset or migrate */ |
| 132 | } |
| 133 | |
| 134 | static const TypeInfo nextfb_info = { |
| 135 | .name = TYPE_NEXTFB, |
| 136 | .parent = TYPE_SYS_BUS_DEVICE, |
| 137 | .instance_size = sizeof(NeXTFbState), |
| 138 | .class_init = nextfb_class_init, |
| 139 | }; |
| 140 | |
| 141 | static void nextfb_register_types(void) |
| 142 | { |
| 143 | type_register_static(&nextfb_info); |
| 144 | } |
| 145 | |
| 146 | type_init(nextfb_register_types) |