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