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