jazz_led.c 9.1 KB
Newer Older
1 2
/*
 * QEMU JAZZ LED emulator.
3
 *
H
Hervé Poussineau 已提交
4
 * Copyright (c) 2007-2012 Herve Poussineau
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

25
#include "qemu-common.h"
26 27
#include "ui/console.h"
#include "ui/pixel_ops.h"
28
#include "trace.h"
29
#include "hw/sysbus.h"
H
Hervé Poussineau 已提交
30

31 32
typedef enum {
    REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
A
Anthony Liguori 已提交
33
} screen_state_t;
34 35

typedef struct LedState {
H
Hervé Poussineau 已提交
36
    SysBusDevice busdev;
A
Avi Kivity 已提交
37
    MemoryRegion iomem;
38
    uint8_t segments;
39
    QemuConsole *con;
A
Anthony Liguori 已提交
40
    screen_state_t state;
41 42
} LedState;

A
Avi Kivity 已提交
43
static uint64_t jazz_led_read(void *opaque, hwaddr addr,
H
Hervé Poussineau 已提交
44
                              unsigned int size)
45 46
{
    LedState *s = opaque;
47
    uint8_t val;
48

H
Hervé Poussineau 已提交
49
    val = s->segments;
50
    trace_jazz_led_read(addr, val);
H
Hervé Poussineau 已提交
51

52 53 54
    return val;
}

A
Avi Kivity 已提交
55
static void jazz_led_write(void *opaque, hwaddr addr,
H
Hervé Poussineau 已提交
56
                           uint64_t val, unsigned int size)
57 58
{
    LedState *s = opaque;
59
    uint8_t new_val = val & 0xff;
60

61
    trace_jazz_led_write(addr, new_val);
H
Hervé Poussineau 已提交
62

H
Hervé Poussineau 已提交
63 64
    s->segments = new_val;
    s->state |= REDRAW_SEGMENTS;
65 66
}

A
Avi Kivity 已提交
67
static const MemoryRegionOps led_ops = {
H
Hervé Poussineau 已提交
68 69
    .read = jazz_led_read,
    .write = jazz_led_write,
A
Avi Kivity 已提交
70
    .endianness = DEVICE_NATIVE_ENDIAN,
H
Hervé Poussineau 已提交
71 72
    .impl.min_access_size = 1,
    .impl.max_access_size = 1,
73 74 75 76 77
};

/***********************************************************/
/* jazz_led display */

78 79 80
static void draw_horizontal_line(DisplaySurface *ds,
                                 int posy, int posx1, int posx2,
                                 uint32_t color)
81 82 83 84
{
    uint8_t *d;
    int x, bpp;

85 86
    bpp = (surface_bits_per_pixel(ds) + 7) >> 3;
    d = surface_data(ds) + surface_stride(ds) * posy + bpp * posx1;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    switch(bpp) {
        case 1:
            for (x = posx1; x <= posx2; x++) {
                *((uint8_t *)d) = color;
                d++;
            }
            break;
        case 2:
            for (x = posx1; x <= posx2; x++) {
                *((uint16_t *)d) = color;
                d += 2;
            }
            break;
        case 4:
            for (x = posx1; x <= posx2; x++) {
                *((uint32_t *)d) = color;
                d += 4;
            }
            break;
    }
}

109 110 111
static void draw_vertical_line(DisplaySurface *ds,
                               int posx, int posy1, int posy2,
                               uint32_t color)
112 113 114 115
{
    uint8_t *d;
    int y, bpp;

116 117
    bpp = (surface_bits_per_pixel(ds) + 7) >> 3;
    d = surface_data(ds) + surface_stride(ds) * posy1 + bpp * posx;
118 119 120 121
    switch(bpp) {
        case 1:
            for (y = posy1; y <= posy2; y++) {
                *((uint8_t *)d) = color;
122
                d += surface_stride(ds);
123 124 125 126 127
            }
            break;
        case 2:
            for (y = posy1; y <= posy2; y++) {
                *((uint16_t *)d) = color;
128
                d += surface_stride(ds);
129 130 131 132 133
            }
            break;
        case 4:
            for (y = posy1; y <= posy2; y++) {
                *((uint32_t *)d) = color;
134
                d += surface_stride(ds);
135 136 137 138 139 140 141 142
            }
            break;
    }
}

static void jazz_led_update_display(void *opaque)
{
    LedState *s = opaque;
143
    DisplaySurface *surface = qemu_console_surface(s->con);
144 145 146 147 148 149
    uint8_t *d1;
    uint32_t color_segment, color_led;
    int y, bpp;

    if (s->state & REDRAW_BACKGROUND) {
        /* clear screen */
150 151 152 153 154
        bpp = (surface_bits_per_pixel(surface) + 7) >> 3;
        d1 = surface_data(surface);
        for (y = 0; y < surface_height(surface); y++) {
            memset(d1, 0x00, surface_width(surface) * bpp);
            d1 += surface_stride(surface);
155 156 157 158 159
        }
    }

    if (s->state & REDRAW_SEGMENTS) {
        /* set colors according to bpp */
160
        switch (surface_bits_per_pixel(surface)) {
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
            case 8:
                color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
                color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
                break;
            case 15:
                color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
                color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
                break;
            case 16:
                color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
                color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
            case 24:
                color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
                color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
                break;
            case 32:
                color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
                color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
                break;
            default:
                return;
        }

        /* display segments */
185 186 187 188 189 190 191 192 193 194 195 196 197 198
        draw_horizontal_line(surface, 40, 10, 40,
                             (s->segments & 0x02) ? color_segment : 0);
        draw_vertical_line(surface, 10, 10, 40,
                           (s->segments & 0x04) ? color_segment : 0);
        draw_vertical_line(surface, 10, 40, 70,
                           (s->segments & 0x08) ? color_segment : 0);
        draw_horizontal_line(surface, 70, 10, 40,
                             (s->segments & 0x10) ? color_segment : 0);
        draw_vertical_line(surface, 40, 40, 70,
                           (s->segments & 0x20) ? color_segment : 0);
        draw_vertical_line(surface, 40, 10, 40,
                           (s->segments & 0x40) ? color_segment : 0);
        draw_horizontal_line(surface, 10, 10, 40,
                             (s->segments & 0x80) ? color_segment : 0);
199 200 201 202

        /* display led */
        if (!(s->segments & 0x01))
            color_led = 0; /* black */
203 204 205 206 207
        draw_horizontal_line(surface, 68, 50, 50, color_led);
        draw_horizontal_line(surface, 69, 49, 51, color_led);
        draw_horizontal_line(surface, 70, 48, 52, color_led);
        draw_horizontal_line(surface, 71, 49, 51, color_led);
        draw_horizontal_line(surface, 72, 50, 50, color_led);
208 209 210
    }

    s->state = REDRAW_NONE;
211 212
    dpy_gfx_update(s->con, 0, 0,
                   surface_width(surface), surface_height(surface));
213 214 215 216 217 218 219 220
}

static void jazz_led_invalidate_display(void *opaque)
{
    LedState *s = opaque;
    s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
}

A
Anthony Liguori 已提交
221
static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
B
balrog 已提交
222 223 224 225
{
    LedState *s = opaque;
    char buf[2];

226 227
    dpy_text_cursor(s->con, -1, -1);
    qemu_console_resize(s->con, 2, 1);
B
balrog 已提交
228 229 230 231 232 233

    /* TODO: draw the segments */
    snprintf(buf, 2, "%02hhx\n", s->segments);
    console_write_ch(chardata++, 0x00200100 | buf[0]);
    console_write_ch(chardata++, 0x00200100 | buf[1]);

234
    dpy_text_update(s->con, 0, 0, 2, 1);
B
balrog 已提交
235 236
}

H
Hervé Poussineau 已提交
237
static int jazz_led_post_load(void *opaque, int version_id)
238
{
H
Hervé Poussineau 已提交
239 240
    /* force refresh */
    jazz_led_invalidate_display(opaque);
241

H
Hervé Poussineau 已提交
242 243
    return 0;
}
244

H
Hervé Poussineau 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static const VMStateDescription vmstate_jazz_led = {
    .name = "jazz-led",
    .version_id = 0,
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .post_load = jazz_led_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UINT8(segments, LedState),
        VMSTATE_END_OF_LIST()
    }
};

static int jazz_led_init(SysBusDevice *dev)
{
    LedState *s = FROM_SYSBUS(LedState, dev);
260

A
Avi Kivity 已提交
261
    memory_region_init_io(&s->iomem, &led_ops, s, "led", 1);
H
Hervé Poussineau 已提交
262
    sysbus_init_mmio(dev, &s->iomem);
263

264 265 266 267
    s->con = graphic_console_init(jazz_led_update_display,
                                  jazz_led_invalidate_display,
                                  NULL,
                                  jazz_led_text_update, s);
H
Hervé Poussineau 已提交
268 269 270 271 272 273 274 275 276 277

    return 0;
}

static void jazz_led_reset(DeviceState *d)
{
    LedState *s = DO_UPCAST(LedState, busdev.qdev, d);

    s->segments = 0;
    s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
278
    qemu_console_resize(s->con, 60, 80);
279
}
H
Hervé Poussineau 已提交
280 281 282 283 284 285 286 287 288 289 290 291

static void jazz_led_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = jazz_led_init;
    dc->desc = "Jazz LED display",
    dc->vmsd = &vmstate_jazz_led;
    dc->reset = jazz_led_reset;
}

292
static const TypeInfo jazz_led_info = {
H
Hervé Poussineau 已提交
293 294 295 296 297 298 299 300 301 302 303 304
    .name          = "jazz-led",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(LedState),
    .class_init    = jazz_led_class_init,
};

static void jazz_led_register(void)
{
    type_register_static(&jazz_led_info);
}

type_init(jazz_led_register);