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