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