2 * QEMU JAZZ LED emulator.
4 * Copyright (c) 2007-2012 Herve Poussineau
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "ui/console.h"
28 #include "ui/pixel_ops.h"
30 #include "hw/sysbus.h"
33 REDRAW_NONE
= 0, REDRAW_SEGMENTS
= 1, REDRAW_BACKGROUND
= 2,
36 #define TYPE_JAZZ_LED "jazz-led"
37 #define JAZZ_LED(obj) OBJECT_CHECK(LedState, (obj), TYPE_JAZZ_LED)
39 typedef struct LedState
{
40 SysBusDevice parent_obj
;
48 static uint64_t jazz_led_read(void *opaque
, hwaddr addr
,
55 trace_jazz_led_read(addr
, val
);
60 static void jazz_led_write(void *opaque
, hwaddr addr
,
61 uint64_t val
, unsigned int size
)
64 uint8_t new_val
= val
& 0xff;
66 trace_jazz_led_write(addr
, new_val
);
68 s
->segments
= new_val
;
69 s
->state
|= REDRAW_SEGMENTS
;
72 static const MemoryRegionOps led_ops
= {
73 .read
= jazz_led_read
,
74 .write
= jazz_led_write
,
75 .endianness
= DEVICE_NATIVE_ENDIAN
,
76 .impl
.min_access_size
= 1,
77 .impl
.max_access_size
= 1,
80 /***********************************************************/
81 /* jazz_led display */
83 static void draw_horizontal_line(DisplaySurface
*ds
,
84 int posy
, int posx1
, int posx2
,
90 bpp
= (surface_bits_per_pixel(ds
) + 7) >> 3;
91 d
= surface_data(ds
) + surface_stride(ds
) * posy
+ bpp
* posx1
;
94 for (x
= posx1
; x
<= posx2
; x
++) {
95 *((uint8_t *)d
) = color
;
100 for (x
= posx1
; x
<= posx2
; x
++) {
101 *((uint16_t *)d
) = color
;
106 for (x
= posx1
; x
<= posx2
; x
++) {
107 *((uint32_t *)d
) = color
;
114 static void draw_vertical_line(DisplaySurface
*ds
,
115 int posx
, int posy1
, int posy2
,
121 bpp
= (surface_bits_per_pixel(ds
) + 7) >> 3;
122 d
= surface_data(ds
) + surface_stride(ds
) * posy1
+ bpp
* posx
;
125 for (y
= posy1
; y
<= posy2
; y
++) {
126 *((uint8_t *)d
) = color
;
127 d
+= surface_stride(ds
);
131 for (y
= posy1
; y
<= posy2
; y
++) {
132 *((uint16_t *)d
) = color
;
133 d
+= surface_stride(ds
);
137 for (y
= posy1
; y
<= posy2
; y
++) {
138 *((uint32_t *)d
) = color
;
139 d
+= surface_stride(ds
);
145 static void jazz_led_update_display(void *opaque
)
147 LedState
*s
= opaque
;
148 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
150 uint32_t color_segment
, color_led
;
153 if (s
->state
& REDRAW_BACKGROUND
) {
155 bpp
= (surface_bits_per_pixel(surface
) + 7) >> 3;
156 d1
= surface_data(surface
);
157 for (y
= 0; y
< surface_height(surface
); y
++) {
158 memset(d1
, 0x00, surface_width(surface
) * bpp
);
159 d1
+= surface_stride(surface
);
163 if (s
->state
& REDRAW_SEGMENTS
) {
164 /* set colors according to bpp */
165 switch (surface_bits_per_pixel(surface
)) {
167 color_segment
= rgb_to_pixel8(0xaa, 0xaa, 0xaa);
168 color_led
= rgb_to_pixel8(0x00, 0xff, 0x00);
171 color_segment
= rgb_to_pixel15(0xaa, 0xaa, 0xaa);
172 color_led
= rgb_to_pixel15(0x00, 0xff, 0x00);
175 color_segment
= rgb_to_pixel16(0xaa, 0xaa, 0xaa);
176 color_led
= rgb_to_pixel16(0x00, 0xff, 0x00);
179 color_segment
= rgb_to_pixel24(0xaa, 0xaa, 0xaa);
180 color_led
= rgb_to_pixel24(0x00, 0xff, 0x00);
183 color_segment
= rgb_to_pixel32(0xaa, 0xaa, 0xaa);
184 color_led
= rgb_to_pixel32(0x00, 0xff, 0x00);
190 /* display segments */
191 draw_horizontal_line(surface
, 40, 10, 40,
192 (s
->segments
& 0x02) ? color_segment
: 0);
193 draw_vertical_line(surface
, 10, 10, 40,
194 (s
->segments
& 0x04) ? color_segment
: 0);
195 draw_vertical_line(surface
, 10, 40, 70,
196 (s
->segments
& 0x08) ? color_segment
: 0);
197 draw_horizontal_line(surface
, 70, 10, 40,
198 (s
->segments
& 0x10) ? color_segment
: 0);
199 draw_vertical_line(surface
, 40, 40, 70,
200 (s
->segments
& 0x20) ? color_segment
: 0);
201 draw_vertical_line(surface
, 40, 10, 40,
202 (s
->segments
& 0x40) ? color_segment
: 0);
203 draw_horizontal_line(surface
, 10, 10, 40,
204 (s
->segments
& 0x80) ? color_segment
: 0);
207 if (!(s
->segments
& 0x01))
208 color_led
= 0; /* black */
209 draw_horizontal_line(surface
, 68, 50, 50, color_led
);
210 draw_horizontal_line(surface
, 69, 49, 51, color_led
);
211 draw_horizontal_line(surface
, 70, 48, 52, color_led
);
212 draw_horizontal_line(surface
, 71, 49, 51, color_led
);
213 draw_horizontal_line(surface
, 72, 50, 50, color_led
);
216 s
->state
= REDRAW_NONE
;
217 dpy_gfx_update(s
->con
, 0, 0,
218 surface_width(surface
), surface_height(surface
));
221 static void jazz_led_invalidate_display(void *opaque
)
223 LedState
*s
= opaque
;
224 s
->state
|= REDRAW_SEGMENTS
| REDRAW_BACKGROUND
;
227 static void jazz_led_text_update(void *opaque
, console_ch_t
*chardata
)
229 LedState
*s
= opaque
;
232 dpy_text_cursor(s
->con
, -1, -1);
233 qemu_console_resize(s
->con
, 2, 1);
235 /* TODO: draw the segments */
236 snprintf(buf
, 3, "%02hhx", s
->segments
);
237 console_write_ch(chardata
++, ATTR2CHTYPE(buf
[0], QEMU_COLOR_BLUE
,
238 QEMU_COLOR_BLACK
, 1));
239 console_write_ch(chardata
++, ATTR2CHTYPE(buf
[1], QEMU_COLOR_BLUE
,
240 QEMU_COLOR_BLACK
, 1));
242 dpy_text_update(s
->con
, 0, 0, 2, 1);
245 static int jazz_led_post_load(void *opaque
, int version_id
)
248 jazz_led_invalidate_display(opaque
);
253 static const VMStateDescription vmstate_jazz_led
= {
256 .minimum_version_id
= 0,
257 .post_load
= jazz_led_post_load
,
258 .fields
= (VMStateField
[]) {
259 VMSTATE_UINT8(segments
, LedState
),
260 VMSTATE_END_OF_LIST()
264 static const GraphicHwOps jazz_led_ops
= {
265 .invalidate
= jazz_led_invalidate_display
,
266 .gfx_update
= jazz_led_update_display
,
267 .text_update
= jazz_led_text_update
,
270 static void jazz_led_init(Object
*obj
)
272 LedState
*s
= JAZZ_LED(obj
);
273 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
275 memory_region_init_io(&s
->iomem
, obj
, &led_ops
, s
, "led", 1);
276 sysbus_init_mmio(dev
, &s
->iomem
);
279 static void jazz_led_realize(DeviceState
*dev
, Error
**errp
)
281 LedState
*s
= JAZZ_LED(dev
);
283 s
->con
= graphic_console_init(dev
, 0, &jazz_led_ops
, s
);
286 static void jazz_led_reset(DeviceState
*d
)
288 LedState
*s
= JAZZ_LED(d
);
291 s
->state
= REDRAW_SEGMENTS
| REDRAW_BACKGROUND
;
292 qemu_console_resize(s
->con
, 60, 80);
295 static void jazz_led_class_init(ObjectClass
*klass
, void *data
)
297 DeviceClass
*dc
= DEVICE_CLASS(klass
);
299 dc
->desc
= "Jazz LED display",
300 dc
->vmsd
= &vmstate_jazz_led
;
301 dc
->reset
= jazz_led_reset
;
302 dc
->realize
= jazz_led_realize
;
305 static const TypeInfo jazz_led_info
= {
306 .name
= TYPE_JAZZ_LED
,
307 .parent
= TYPE_SYS_BUS_DEVICE
,
308 .instance_size
= sizeof(LedState
),
309 .instance_init
= jazz_led_init
,
310 .class_init
= jazz_led_class_init
,
313 static void jazz_led_register(void)
315 type_register_static(&jazz_led_info
);
318 type_init(jazz_led_register
);