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