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