Statistics
| Branch: | Revision:

root / hw / jazz_led.c @ c3203fa5

History | View | Annotate | Download (8.6 kB)

1 31211df1 ths
/*
2 31211df1 ths
 * QEMU JAZZ LED emulator.
3 5fafdf24 ths
 *
4 b39506e4 Hervé Poussineau
 * Copyright (c) 2007-2012 Herve Poussineau
5 5fafdf24 ths
 *
6 31211df1 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 31211df1 ths
 * of this software and associated documentation files (the "Software"), to deal
8 31211df1 ths
 * in the Software without restriction, including without limitation the rights
9 31211df1 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 31211df1 ths
 * copies of the Software, and to permit persons to whom the Software is
11 31211df1 ths
 * furnished to do so, subject to the following conditions:
12 31211df1 ths
 *
13 31211df1 ths
 * The above copyright notice and this permission notice shall be included in
14 31211df1 ths
 * all copies or substantial portions of the Software.
15 31211df1 ths
 *
16 31211df1 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 31211df1 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 31211df1 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 31211df1 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 31211df1 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 31211df1 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 31211df1 ths
 * THE SOFTWARE.
23 31211df1 ths
 */
24 31211df1 ths
25 077805fa Paolo Bonzini
#include "qemu-common.h"
26 28ecbaee Paolo Bonzini
#include "ui/console.h"
27 28ecbaee Paolo Bonzini
#include "ui/pixel_ops.h"
28 63b9932d Hervé Poussineau
#include "trace.h"
29 b39506e4 Hervé Poussineau
#include "sysbus.h"
30 14414da4 Hervé Poussineau
31 31211df1 ths
typedef enum {
32 31211df1 ths
    REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
33 c227f099 Anthony Liguori
} screen_state_t;
34 31211df1 ths
35 31211df1 ths
typedef struct LedState {
36 b39506e4 Hervé Poussineau
    SysBusDevice busdev;
37 c6017850 Avi Kivity
    MemoryRegion iomem;
38 31211df1 ths
    uint8_t segments;
39 31211df1 ths
    DisplayState *ds;
40 c227f099 Anthony Liguori
    screen_state_t state;
41 31211df1 ths
} LedState;
42 31211df1 ths
43 a8170e5e Avi Kivity
static uint64_t jazz_led_read(void *opaque, hwaddr addr,
44 b39506e4 Hervé Poussineau
                              unsigned int size)
45 31211df1 ths
{
46 31211df1 ths
    LedState *s = opaque;
47 63b9932d Hervé Poussineau
    uint8_t val;
48 31211df1 ths
49 b39506e4 Hervé Poussineau
    val = s->segments;
50 63b9932d Hervé Poussineau
    trace_jazz_led_read(addr, val);
51 14414da4 Hervé Poussineau
52 31211df1 ths
    return val;
53 31211df1 ths
}
54 31211df1 ths
55 a8170e5e Avi Kivity
static void jazz_led_write(void *opaque, hwaddr addr,
56 b39506e4 Hervé Poussineau
                           uint64_t val, unsigned int size)
57 31211df1 ths
{
58 31211df1 ths
    LedState *s = opaque;
59 63b9932d Hervé Poussineau
    uint8_t new_val = val & 0xff;
60 31211df1 ths
61 63b9932d Hervé Poussineau
    trace_jazz_led_write(addr, new_val);
62 14414da4 Hervé Poussineau
63 b39506e4 Hervé Poussineau
    s->segments = new_val;
64 b39506e4 Hervé Poussineau
    s->state |= REDRAW_SEGMENTS;
65 31211df1 ths
}
66 31211df1 ths
67 c6017850 Avi Kivity
static const MemoryRegionOps led_ops = {
68 b39506e4 Hervé Poussineau
    .read = jazz_led_read,
69 b39506e4 Hervé Poussineau
    .write = jazz_led_write,
70 c6017850 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
71 b39506e4 Hervé Poussineau
    .impl.min_access_size = 1,
72 b39506e4 Hervé Poussineau
    .impl.max_access_size = 1,
73 31211df1 ths
};
74 31211df1 ths
75 31211df1 ths
/***********************************************************/
76 31211df1 ths
/* jazz_led display */
77 31211df1 ths
78 31211df1 ths
static void draw_horizontal_line(DisplayState *ds, int posy, int posx1, int posx2, uint32_t color)
79 31211df1 ths
{
80 31211df1 ths
    uint8_t *d;
81 31211df1 ths
    int x, bpp;
82 31211df1 ths
83 0e1f5a0c aliguori
    bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
84 0e1f5a0c aliguori
    d = ds_get_data(ds) + ds_get_linesize(ds) * posy + bpp * posx1;
85 31211df1 ths
    switch(bpp) {
86 31211df1 ths
        case 1:
87 31211df1 ths
            for (x = posx1; x <= posx2; x++) {
88 31211df1 ths
                *((uint8_t *)d) = color;
89 31211df1 ths
                d++;
90 31211df1 ths
            }
91 31211df1 ths
            break;
92 31211df1 ths
        case 2:
93 31211df1 ths
            for (x = posx1; x <= posx2; x++) {
94 31211df1 ths
                *((uint16_t *)d) = color;
95 31211df1 ths
                d += 2;
96 31211df1 ths
            }
97 31211df1 ths
            break;
98 31211df1 ths
        case 4:
99 31211df1 ths
            for (x = posx1; x <= posx2; x++) {
100 31211df1 ths
                *((uint32_t *)d) = color;
101 31211df1 ths
                d += 4;
102 31211df1 ths
            }
103 31211df1 ths
            break;
104 31211df1 ths
    }
105 31211df1 ths
}
106 31211df1 ths
107 31211df1 ths
static void draw_vertical_line(DisplayState *ds, int posx, int posy1, int posy2, uint32_t color)
108 31211df1 ths
{
109 31211df1 ths
    uint8_t *d;
110 31211df1 ths
    int y, bpp;
111 31211df1 ths
112 0e1f5a0c aliguori
    bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
113 0e1f5a0c aliguori
    d = ds_get_data(ds) + ds_get_linesize(ds) * posy1 + bpp * posx;
114 31211df1 ths
    switch(bpp) {
115 31211df1 ths
        case 1:
116 31211df1 ths
            for (y = posy1; y <= posy2; y++) {
117 31211df1 ths
                *((uint8_t *)d) = color;
118 0e1f5a0c aliguori
                d += ds_get_linesize(ds);
119 31211df1 ths
            }
120 31211df1 ths
            break;
121 31211df1 ths
        case 2:
122 31211df1 ths
            for (y = posy1; y <= posy2; y++) {
123 31211df1 ths
                *((uint16_t *)d) = color;
124 0e1f5a0c aliguori
                d += ds_get_linesize(ds);
125 31211df1 ths
            }
126 31211df1 ths
            break;
127 31211df1 ths
        case 4:
128 31211df1 ths
            for (y = posy1; y <= posy2; y++) {
129 31211df1 ths
                *((uint32_t *)d) = color;
130 0e1f5a0c aliguori
                d += ds_get_linesize(ds);
131 31211df1 ths
            }
132 31211df1 ths
            break;
133 31211df1 ths
    }
134 31211df1 ths
}
135 31211df1 ths
136 31211df1 ths
static void jazz_led_update_display(void *opaque)
137 31211df1 ths
{
138 31211df1 ths
    LedState *s = opaque;
139 31211df1 ths
    DisplayState *ds = s->ds;
140 31211df1 ths
    uint8_t *d1;
141 31211df1 ths
    uint32_t color_segment, color_led;
142 31211df1 ths
    int y, bpp;
143 31211df1 ths
144 31211df1 ths
    if (s->state & REDRAW_BACKGROUND) {
145 31211df1 ths
        /* clear screen */
146 0e1f5a0c aliguori
        bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
147 0e1f5a0c aliguori
        d1 = ds_get_data(ds);
148 0e1f5a0c aliguori
        for (y = 0; y < ds_get_height(ds); y++) {
149 0e1f5a0c aliguori
            memset(d1, 0x00, ds_get_width(ds) * bpp);
150 0e1f5a0c aliguori
            d1 += ds_get_linesize(ds);
151 31211df1 ths
        }
152 31211df1 ths
    }
153 31211df1 ths
154 31211df1 ths
    if (s->state & REDRAW_SEGMENTS) {
155 31211df1 ths
        /* set colors according to bpp */
156 0e1f5a0c aliguori
        switch (ds_get_bits_per_pixel(ds)) {
157 31211df1 ths
            case 8:
158 31211df1 ths
                color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
159 31211df1 ths
                color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
160 31211df1 ths
                break;
161 31211df1 ths
            case 15:
162 31211df1 ths
                color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
163 31211df1 ths
                color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
164 31211df1 ths
                break;
165 31211df1 ths
            case 16:
166 31211df1 ths
                color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
167 31211df1 ths
                color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
168 31211df1 ths
            case 24:
169 31211df1 ths
                color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
170 31211df1 ths
                color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
171 31211df1 ths
                break;
172 31211df1 ths
            case 32:
173 31211df1 ths
                color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
174 31211df1 ths
                color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
175 31211df1 ths
                break;
176 31211df1 ths
            default:
177 31211df1 ths
                return;
178 31211df1 ths
        }
179 31211df1 ths
180 31211df1 ths
        /* display segments */
181 31211df1 ths
        draw_horizontal_line(ds, 40, 10, 40, (s->segments & 0x02) ? color_segment : 0);
182 31211df1 ths
        draw_vertical_line(ds, 10, 10, 40, (s->segments & 0x04) ? color_segment : 0);
183 31211df1 ths
        draw_vertical_line(ds, 10, 40, 70, (s->segments & 0x08) ? color_segment : 0);
184 31211df1 ths
        draw_horizontal_line(ds, 70, 10, 40, (s->segments & 0x10) ? color_segment : 0);
185 31211df1 ths
        draw_vertical_line(ds, 40, 40, 70, (s->segments & 0x20) ? color_segment : 0);
186 31211df1 ths
        draw_vertical_line(ds, 40, 10, 40, (s->segments & 0x40) ? color_segment : 0);
187 31211df1 ths
        draw_horizontal_line(ds, 10, 10, 40, (s->segments & 0x80) ? color_segment : 0);
188 31211df1 ths
189 31211df1 ths
        /* display led */
190 31211df1 ths
        if (!(s->segments & 0x01))
191 31211df1 ths
            color_led = 0; /* black */
192 31211df1 ths
        draw_horizontal_line(ds, 68, 50, 50, color_led);
193 31211df1 ths
        draw_horizontal_line(ds, 69, 49, 51, color_led);
194 31211df1 ths
        draw_horizontal_line(ds, 70, 48, 52, color_led);
195 31211df1 ths
        draw_horizontal_line(ds, 71, 49, 51, color_led);
196 31211df1 ths
        draw_horizontal_line(ds, 72, 50, 50, color_led);
197 31211df1 ths
    }
198 31211df1 ths
199 31211df1 ths
    s->state = REDRAW_NONE;
200 a93a4a22 Gerd Hoffmann
    dpy_gfx_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
201 31211df1 ths
}
202 31211df1 ths
203 31211df1 ths
static void jazz_led_invalidate_display(void *opaque)
204 31211df1 ths
{
205 31211df1 ths
    LedState *s = opaque;
206 31211df1 ths
    s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
207 31211df1 ths
}
208 31211df1 ths
209 c227f099 Anthony Liguori
static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
210 4d3b6f6e balrog
{
211 4d3b6f6e balrog
    LedState *s = opaque;
212 4d3b6f6e balrog
    char buf[2];
213 4d3b6f6e balrog
214 bf2fde70 Gerd Hoffmann
    dpy_text_cursor(s->ds, -1, -1);
215 3023f332 aliguori
    qemu_console_resize(s->ds, 2, 1);
216 4d3b6f6e balrog
217 4d3b6f6e balrog
    /* TODO: draw the segments */
218 4d3b6f6e balrog
    snprintf(buf, 2, "%02hhx\n", s->segments);
219 4d3b6f6e balrog
    console_write_ch(chardata++, 0x00200100 | buf[0]);
220 4d3b6f6e balrog
    console_write_ch(chardata++, 0x00200100 | buf[1]);
221 4d3b6f6e balrog
222 a93a4a22 Gerd Hoffmann
    dpy_text_update(s->ds, 0, 0, 2, 1);
223 4d3b6f6e balrog
}
224 4d3b6f6e balrog
225 b39506e4 Hervé Poussineau
static int jazz_led_post_load(void *opaque, int version_id)
226 31211df1 ths
{
227 b39506e4 Hervé Poussineau
    /* force refresh */
228 b39506e4 Hervé Poussineau
    jazz_led_invalidate_display(opaque);
229 31211df1 ths
230 b39506e4 Hervé Poussineau
    return 0;
231 b39506e4 Hervé Poussineau
}
232 31211df1 ths
233 b39506e4 Hervé Poussineau
static const VMStateDescription vmstate_jazz_led = {
234 b39506e4 Hervé Poussineau
    .name = "jazz-led",
235 b39506e4 Hervé Poussineau
    .version_id = 0,
236 b39506e4 Hervé Poussineau
    .minimum_version_id = 0,
237 b39506e4 Hervé Poussineau
    .minimum_version_id_old = 0,
238 b39506e4 Hervé Poussineau
    .post_load = jazz_led_post_load,
239 b39506e4 Hervé Poussineau
    .fields = (VMStateField[]) {
240 b39506e4 Hervé Poussineau
        VMSTATE_UINT8(segments, LedState),
241 b39506e4 Hervé Poussineau
        VMSTATE_END_OF_LIST()
242 b39506e4 Hervé Poussineau
    }
243 b39506e4 Hervé Poussineau
};
244 b39506e4 Hervé Poussineau
245 b39506e4 Hervé Poussineau
static int jazz_led_init(SysBusDevice *dev)
246 b39506e4 Hervé Poussineau
{
247 b39506e4 Hervé Poussineau
    LedState *s = FROM_SYSBUS(LedState, dev);
248 31211df1 ths
249 c6017850 Avi Kivity
    memory_region_init_io(&s->iomem, &led_ops, s, "led", 1);
250 b39506e4 Hervé Poussineau
    sysbus_init_mmio(dev, &s->iomem);
251 31211df1 ths
252 3023f332 aliguori
    s->ds = graphic_console_init(jazz_led_update_display,
253 3023f332 aliguori
                                 jazz_led_invalidate_display,
254 16735102 Gerd Hoffmann
                                 NULL,
255 3023f332 aliguori
                                 jazz_led_text_update, s);
256 b39506e4 Hervé Poussineau
257 b39506e4 Hervé Poussineau
    return 0;
258 b39506e4 Hervé Poussineau
}
259 b39506e4 Hervé Poussineau
260 b39506e4 Hervé Poussineau
static void jazz_led_reset(DeviceState *d)
261 b39506e4 Hervé Poussineau
{
262 b39506e4 Hervé Poussineau
    LedState *s = DO_UPCAST(LedState, busdev.qdev, d);
263 b39506e4 Hervé Poussineau
264 b39506e4 Hervé Poussineau
    s->segments = 0;
265 b39506e4 Hervé Poussineau
    s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
266 3023f332 aliguori
    qemu_console_resize(s->ds, 60, 80);
267 31211df1 ths
}
268 b39506e4 Hervé Poussineau
269 b39506e4 Hervé Poussineau
static void jazz_led_class_init(ObjectClass *klass, void *data)
270 b39506e4 Hervé Poussineau
{
271 b39506e4 Hervé Poussineau
    DeviceClass *dc = DEVICE_CLASS(klass);
272 b39506e4 Hervé Poussineau
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
273 b39506e4 Hervé Poussineau
274 b39506e4 Hervé Poussineau
    k->init = jazz_led_init;
275 b39506e4 Hervé Poussineau
    dc->desc = "Jazz LED display",
276 b39506e4 Hervé Poussineau
    dc->vmsd = &vmstate_jazz_led;
277 b39506e4 Hervé Poussineau
    dc->reset = jazz_led_reset;
278 b39506e4 Hervé Poussineau
}
279 b39506e4 Hervé Poussineau
280 b39506e4 Hervé Poussineau
static TypeInfo jazz_led_info = {
281 b39506e4 Hervé Poussineau
    .name          = "jazz-led",
282 b39506e4 Hervé Poussineau
    .parent        = TYPE_SYS_BUS_DEVICE,
283 b39506e4 Hervé Poussineau
    .instance_size = sizeof(LedState),
284 b39506e4 Hervé Poussineau
    .class_init    = jazz_led_class_init,
285 b39506e4 Hervé Poussineau
};
286 b39506e4 Hervé Poussineau
287 b39506e4 Hervé Poussineau
static void jazz_led_register(void)
288 b39506e4 Hervé Poussineau
{
289 b39506e4 Hervé Poussineau
    type_register_static(&jazz_led_info);
290 b39506e4 Hervé Poussineau
}
291 b39506e4 Hervé Poussineau
292 b39506e4 Hervé Poussineau
type_init(jazz_led_register);