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