xref: /openbmc/qemu/hw/display/jazz_led.c (revision f7160f32)
1 /*
2  * QEMU JAZZ LED emulator.
3  *
4  * Copyright (c) 2007-2012 Herve Poussineau
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 
25 #include "qemu/osdep.h"
26 #include "qemu/module.h"
27 #include "ui/console.h"
28 #include "ui/pixel_ops.h"
29 #include "trace.h"
30 #include "hw/sysbus.h"
31 #include "migration/vmstate.h"
32 
33 typedef enum {
34     REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
35 } screen_state_t;
36 
37 #define TYPE_JAZZ_LED "jazz-led"
38 #define JAZZ_LED(obj) OBJECT_CHECK(LedState, (obj), TYPE_JAZZ_LED)
39 
40 typedef struct LedState {
41     SysBusDevice parent_obj;
42 
43     MemoryRegion iomem;
44     uint8_t segments;
45     QemuConsole *con;
46     screen_state_t state;
47 } LedState;
48 
49 static uint64_t jazz_led_read(void *opaque, hwaddr addr,
50                               unsigned int size)
51 {
52     LedState *s = opaque;
53     uint8_t val;
54 
55     val = s->segments;
56     trace_jazz_led_read(addr, val);
57 
58     return val;
59 }
60 
61 static void jazz_led_write(void *opaque, hwaddr addr,
62                            uint64_t val, unsigned int size)
63 {
64     LedState *s = opaque;
65     uint8_t new_val = val & 0xff;
66 
67     trace_jazz_led_write(addr, new_val);
68 
69     s->segments = new_val;
70     s->state |= REDRAW_SEGMENTS;
71 }
72 
73 static const MemoryRegionOps led_ops = {
74     .read = jazz_led_read,
75     .write = jazz_led_write,
76     .endianness = DEVICE_NATIVE_ENDIAN,
77     .impl.min_access_size = 1,
78     .impl.max_access_size = 1,
79 };
80 
81 /***********************************************************/
82 /* jazz_led display */
83 
84 static void draw_horizontal_line(DisplaySurface *ds,
85                                  int posy, int posx1, int posx2,
86                                  uint32_t color)
87 {
88     uint8_t *d;
89     int x, bpp;
90 
91     bpp = (surface_bits_per_pixel(ds) + 7) >> 3;
92     d = surface_data(ds) + surface_stride(ds) * posy + bpp * posx1;
93     switch (bpp) {
94     case 1:
95         for (x = posx1; x <= posx2; x++) {
96             *((uint8_t *)d) = color;
97             d++;
98         }
99         break;
100     case 2:
101         for (x = posx1; x <= posx2; x++) {
102             *((uint16_t *)d) = color;
103             d += 2;
104         }
105         break;
106     case 4:
107         for (x = posx1; x <= posx2; x++) {
108             *((uint32_t *)d) = color;
109             d += 4;
110         }
111         break;
112     }
113 }
114 
115 static void draw_vertical_line(DisplaySurface *ds,
116                                int posx, int posy1, int posy2,
117                                uint32_t color)
118 {
119     uint8_t *d;
120     int y, bpp;
121 
122     bpp = (surface_bits_per_pixel(ds) + 7) >> 3;
123     d = surface_data(ds) + surface_stride(ds) * posy1 + bpp * posx;
124     switch (bpp) {
125     case 1:
126         for (y = posy1; y <= posy2; y++) {
127             *((uint8_t *)d) = color;
128             d += surface_stride(ds);
129         }
130         break;
131     case 2:
132         for (y = posy1; y <= posy2; y++) {
133             *((uint16_t *)d) = color;
134             d += surface_stride(ds);
135         }
136         break;
137     case 4:
138         for (y = posy1; y <= posy2; y++) {
139             *((uint32_t *)d) = color;
140             d += surface_stride(ds);
141         }
142         break;
143     }
144 }
145 
146 static void jazz_led_update_display(void *opaque)
147 {
148     LedState *s = opaque;
149     DisplaySurface *surface = qemu_console_surface(s->con);
150     uint8_t *d1;
151     uint32_t color_segment, color_led;
152     int y, bpp;
153 
154     if (s->state & REDRAW_BACKGROUND) {
155         /* clear screen */
156         bpp = (surface_bits_per_pixel(surface) + 7) >> 3;
157         d1 = surface_data(surface);
158         for (y = 0; y < surface_height(surface); y++) {
159             memset(d1, 0x00, surface_width(surface) * bpp);
160             d1 += surface_stride(surface);
161         }
162     }
163 
164     if (s->state & REDRAW_SEGMENTS) {
165         /* set colors according to bpp */
166         switch (surface_bits_per_pixel(surface)) {
167         case 8:
168             color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
169             color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
170             break;
171         case 15:
172             color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
173             color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
174             break;
175         case 16:
176             color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
177             color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
178             break;
179         case 24:
180             color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
181             color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
182             break;
183         case 32:
184             color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
185             color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
186             break;
187         default:
188             return;
189         }
190 
191         /* display segments */
192         draw_horizontal_line(surface, 40, 10, 40,
193                              (s->segments & 0x02) ? color_segment : 0);
194         draw_vertical_line(surface, 10, 10, 40,
195                            (s->segments & 0x04) ? color_segment : 0);
196         draw_vertical_line(surface, 10, 40, 70,
197                            (s->segments & 0x08) ? color_segment : 0);
198         draw_horizontal_line(surface, 70, 10, 40,
199                              (s->segments & 0x10) ? color_segment : 0);
200         draw_vertical_line(surface, 40, 40, 70,
201                            (s->segments & 0x20) ? color_segment : 0);
202         draw_vertical_line(surface, 40, 10, 40,
203                            (s->segments & 0x40) ? color_segment : 0);
204         draw_horizontal_line(surface, 10, 10, 40,
205                              (s->segments & 0x80) ? color_segment : 0);
206 
207         /* display led */
208         if (!(s->segments & 0x01)) {
209             color_led = 0; /* black */
210         }
211         draw_horizontal_line(surface, 68, 50, 50, color_led);
212         draw_horizontal_line(surface, 69, 49, 51, color_led);
213         draw_horizontal_line(surface, 70, 48, 52, color_led);
214         draw_horizontal_line(surface, 71, 49, 51, color_led);
215         draw_horizontal_line(surface, 72, 50, 50, color_led);
216     }
217 
218     s->state = REDRAW_NONE;
219     dpy_gfx_update_full(s->con);
220 }
221 
222 static void jazz_led_invalidate_display(void *opaque)
223 {
224     LedState *s = opaque;
225     s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
226 }
227 
228 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
229 {
230     LedState *s = opaque;
231     char buf[3];
232 
233     dpy_text_cursor(s->con, -1, -1);
234     qemu_console_resize(s->con, 2, 1);
235 
236     /* TODO: draw the segments */
237     snprintf(buf, 3, "%02hhx", s->segments);
238     console_write_ch(chardata++, ATTR2CHTYPE(buf[0], QEMU_COLOR_BLUE,
239                                              QEMU_COLOR_BLACK, 1));
240     console_write_ch(chardata++, ATTR2CHTYPE(buf[1], QEMU_COLOR_BLUE,
241                                              QEMU_COLOR_BLACK, 1));
242 
243     dpy_text_update(s->con, 0, 0, 2, 1);
244 }
245 
246 static int jazz_led_post_load(void *opaque, int version_id)
247 {
248     /* force refresh */
249     jazz_led_invalidate_display(opaque);
250 
251     return 0;
252 }
253 
254 static const VMStateDescription vmstate_jazz_led = {
255     .name = "jazz-led",
256     .version_id = 0,
257     .minimum_version_id = 0,
258     .post_load = jazz_led_post_load,
259     .fields = (VMStateField[]) {
260         VMSTATE_UINT8(segments, LedState),
261         VMSTATE_END_OF_LIST()
262     }
263 };
264 
265 static const GraphicHwOps jazz_led_ops = {
266     .invalidate  = jazz_led_invalidate_display,
267     .gfx_update  = jazz_led_update_display,
268     .text_update = jazz_led_text_update,
269 };
270 
271 static void jazz_led_init(Object *obj)
272 {
273     LedState *s = JAZZ_LED(obj);
274     SysBusDevice *dev = SYS_BUS_DEVICE(obj);
275 
276     memory_region_init_io(&s->iomem, obj, &led_ops, s, "led", 1);
277     sysbus_init_mmio(dev, &s->iomem);
278 }
279 
280 static void jazz_led_realize(DeviceState *dev, Error **errp)
281 {
282     LedState *s = JAZZ_LED(dev);
283 
284     s->con = graphic_console_init(dev, 0, &jazz_led_ops, s);
285 }
286 
287 static void jazz_led_reset(DeviceState *d)
288 {
289     LedState *s = JAZZ_LED(d);
290 
291     s->segments = 0;
292     s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
293     qemu_console_resize(s->con, 60, 80);
294 }
295 
296 static void jazz_led_class_init(ObjectClass *klass, void *data)
297 {
298     DeviceClass *dc = DEVICE_CLASS(klass);
299 
300     dc->desc = "Jazz LED display",
301     dc->vmsd = &vmstate_jazz_led;
302     dc->reset = jazz_led_reset;
303     dc->realize = jazz_led_realize;
304 }
305 
306 static const TypeInfo jazz_led_info = {
307     .name          = TYPE_JAZZ_LED,
308     .parent        = TYPE_SYS_BUS_DEVICE,
309     .instance_size = sizeof(LedState),
310     .instance_init = jazz_led_init,
311     .class_init    = jazz_led_class_init,
312 };
313 
314 static void jazz_led_register(void)
315 {
316     type_register_static(&jazz_led_info);
317 }
318 
319 type_init(jazz_led_register);
320