i8259: Factor out base class for KVM reuse
[qemu/ar7.git] / hw / jazz_led.c
blobf8a218252cfd7a7b3cd7b3862aff6f01bf6c657b
1 /*
2 * QEMU JAZZ LED emulator.
4 * Copyright (c) 2007 Hervé Poussineau
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "hw.h"
26 #include "mips.h"
27 #include "console.h"
28 #include "pixel_ops.h"
30 //#define DEBUG_LED
32 #ifdef DEBUG_LED
33 #define DPRINTF(fmt, ...) \
34 do { printf("jazz led: " fmt , ## __VA_ARGS__); } while (0)
35 #else
36 #define DPRINTF(fmt, ...) do {} while (0)
37 #endif
38 #define BADF(fmt, ...) \
39 do { fprintf(stderr, "jazz led ERROR: " fmt , ## __VA_ARGS__);} while (0)
41 typedef enum {
42 REDRAW_NONE = 0, REDRAW_SEGMENTS = 1, REDRAW_BACKGROUND = 2,
43 } screen_state_t;
45 typedef struct LedState {
46 MemoryRegion iomem;
47 uint8_t segments;
48 DisplayState *ds;
49 screen_state_t state;
50 } LedState;
52 static uint32_t led_readb(void *opaque, target_phys_addr_t addr)
54 LedState *s = opaque;
55 uint32_t val;
57 switch (addr) {
58 case 0:
59 val = s->segments;
60 break;
61 default:
62 BADF("invalid read at [" TARGET_FMT_plx "]\n", addr);
63 val = 0;
66 DPRINTF("read addr=" TARGET_FMT_plx " val=0x%02x\n", addr, val);
68 return val;
71 static uint32_t led_readw(void *opaque, target_phys_addr_t addr)
73 uint32_t v;
74 #ifdef TARGET_WORDS_BIGENDIAN
75 v = led_readb(opaque, addr) << 8;
76 v |= led_readb(opaque, addr + 1);
77 #else
78 v = led_readb(opaque, addr);
79 v |= led_readb(opaque, addr + 1) << 8;
80 #endif
81 return v;
84 static uint32_t led_readl(void *opaque, target_phys_addr_t addr)
86 uint32_t v;
87 #ifdef TARGET_WORDS_BIGENDIAN
88 v = led_readb(opaque, addr) << 24;
89 v |= led_readb(opaque, addr + 1) << 16;
90 v |= led_readb(opaque, addr + 2) << 8;
91 v |= led_readb(opaque, addr + 3);
92 #else
93 v = led_readb(opaque, addr);
94 v |= led_readb(opaque, addr + 1) << 8;
95 v |= led_readb(opaque, addr + 2) << 16;
96 v |= led_readb(opaque, addr + 3) << 24;
97 #endif
98 return v;
101 static void led_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
103 LedState *s = opaque;
105 DPRINTF("write addr=" TARGET_FMT_plx " val=0x%02x\n", addr, val);
107 switch (addr) {
108 case 0:
109 s->segments = val;
110 s->state |= REDRAW_SEGMENTS;
111 break;
112 default:
113 BADF("invalid write of 0x%08x at [" TARGET_FMT_plx "]\n", val, addr);
114 break;
118 static void led_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
120 #ifdef TARGET_WORDS_BIGENDIAN
121 led_writeb(opaque, addr, (val >> 8) & 0xff);
122 led_writeb(opaque, addr + 1, val & 0xff);
123 #else
124 led_writeb(opaque, addr, val & 0xff);
125 led_writeb(opaque, addr + 1, (val >> 8) & 0xff);
126 #endif
129 static void led_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
131 #ifdef TARGET_WORDS_BIGENDIAN
132 led_writeb(opaque, addr, (val >> 24) & 0xff);
133 led_writeb(opaque, addr + 1, (val >> 16) & 0xff);
134 led_writeb(opaque, addr + 2, (val >> 8) & 0xff);
135 led_writeb(opaque, addr + 3, val & 0xff);
136 #else
137 led_writeb(opaque, addr, val & 0xff);
138 led_writeb(opaque, addr + 1, (val >> 8) & 0xff);
139 led_writeb(opaque, addr + 2, (val >> 16) & 0xff);
140 led_writeb(opaque, addr + 3, (val >> 24) & 0xff);
141 #endif
144 static const MemoryRegionOps led_ops = {
145 .old_mmio = {
146 .read = { led_readb, led_readw, led_readl, },
147 .write = { led_writeb, led_writew, led_writel, },
149 .endianness = DEVICE_NATIVE_ENDIAN,
152 /***********************************************************/
153 /* jazz_led display */
155 static void draw_horizontal_line(DisplayState *ds, int posy, int posx1, int posx2, uint32_t color)
157 uint8_t *d;
158 int x, bpp;
160 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
161 d = ds_get_data(ds) + ds_get_linesize(ds) * posy + bpp * posx1;
162 switch(bpp) {
163 case 1:
164 for (x = posx1; x <= posx2; x++) {
165 *((uint8_t *)d) = color;
166 d++;
168 break;
169 case 2:
170 for (x = posx1; x <= posx2; x++) {
171 *((uint16_t *)d) = color;
172 d += 2;
174 break;
175 case 4:
176 for (x = posx1; x <= posx2; x++) {
177 *((uint32_t *)d) = color;
178 d += 4;
180 break;
184 static void draw_vertical_line(DisplayState *ds, int posx, int posy1, int posy2, uint32_t color)
186 uint8_t *d;
187 int y, bpp;
189 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
190 d = ds_get_data(ds) + ds_get_linesize(ds) * posy1 + bpp * posx;
191 switch(bpp) {
192 case 1:
193 for (y = posy1; y <= posy2; y++) {
194 *((uint8_t *)d) = color;
195 d += ds_get_linesize(ds);
197 break;
198 case 2:
199 for (y = posy1; y <= posy2; y++) {
200 *((uint16_t *)d) = color;
201 d += ds_get_linesize(ds);
203 break;
204 case 4:
205 for (y = posy1; y <= posy2; y++) {
206 *((uint32_t *)d) = color;
207 d += ds_get_linesize(ds);
209 break;
213 static void jazz_led_update_display(void *opaque)
215 LedState *s = opaque;
216 DisplayState *ds = s->ds;
217 uint8_t *d1;
218 uint32_t color_segment, color_led;
219 int y, bpp;
221 if (s->state & REDRAW_BACKGROUND) {
222 /* clear screen */
223 bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
224 d1 = ds_get_data(ds);
225 for (y = 0; y < ds_get_height(ds); y++) {
226 memset(d1, 0x00, ds_get_width(ds) * bpp);
227 d1 += ds_get_linesize(ds);
231 if (s->state & REDRAW_SEGMENTS) {
232 /* set colors according to bpp */
233 switch (ds_get_bits_per_pixel(ds)) {
234 case 8:
235 color_segment = rgb_to_pixel8(0xaa, 0xaa, 0xaa);
236 color_led = rgb_to_pixel8(0x00, 0xff, 0x00);
237 break;
238 case 15:
239 color_segment = rgb_to_pixel15(0xaa, 0xaa, 0xaa);
240 color_led = rgb_to_pixel15(0x00, 0xff, 0x00);
241 break;
242 case 16:
243 color_segment = rgb_to_pixel16(0xaa, 0xaa, 0xaa);
244 color_led = rgb_to_pixel16(0x00, 0xff, 0x00);
245 case 24:
246 color_segment = rgb_to_pixel24(0xaa, 0xaa, 0xaa);
247 color_led = rgb_to_pixel24(0x00, 0xff, 0x00);
248 break;
249 case 32:
250 color_segment = rgb_to_pixel32(0xaa, 0xaa, 0xaa);
251 color_led = rgb_to_pixel32(0x00, 0xff, 0x00);
252 break;
253 default:
254 return;
257 /* display segments */
258 draw_horizontal_line(ds, 40, 10, 40, (s->segments & 0x02) ? color_segment : 0);
259 draw_vertical_line(ds, 10, 10, 40, (s->segments & 0x04) ? color_segment : 0);
260 draw_vertical_line(ds, 10, 40, 70, (s->segments & 0x08) ? color_segment : 0);
261 draw_horizontal_line(ds, 70, 10, 40, (s->segments & 0x10) ? color_segment : 0);
262 draw_vertical_line(ds, 40, 40, 70, (s->segments & 0x20) ? color_segment : 0);
263 draw_vertical_line(ds, 40, 10, 40, (s->segments & 0x40) ? color_segment : 0);
264 draw_horizontal_line(ds, 10, 10, 40, (s->segments & 0x80) ? color_segment : 0);
266 /* display led */
267 if (!(s->segments & 0x01))
268 color_led = 0; /* black */
269 draw_horizontal_line(ds, 68, 50, 50, color_led);
270 draw_horizontal_line(ds, 69, 49, 51, color_led);
271 draw_horizontal_line(ds, 70, 48, 52, color_led);
272 draw_horizontal_line(ds, 71, 49, 51, color_led);
273 draw_horizontal_line(ds, 72, 50, 50, color_led);
276 s->state = REDRAW_NONE;
277 dpy_update(ds, 0, 0, ds_get_width(ds), ds_get_height(ds));
280 static void jazz_led_invalidate_display(void *opaque)
282 LedState *s = opaque;
283 s->state |= REDRAW_SEGMENTS | REDRAW_BACKGROUND;
286 static void jazz_led_screen_dump(void *opaque, const char *filename)
288 printf("jazz_led_screen_dump() not implemented\n");
291 static void jazz_led_text_update(void *opaque, console_ch_t *chardata)
293 LedState *s = opaque;
294 char buf[2];
296 dpy_cursor(s->ds, -1, -1);
297 qemu_console_resize(s->ds, 2, 1);
299 /* TODO: draw the segments */
300 snprintf(buf, 2, "%02hhx\n", s->segments);
301 console_write_ch(chardata++, 0x00200100 | buf[0]);
302 console_write_ch(chardata++, 0x00200100 | buf[1]);
304 dpy_update(s->ds, 0, 0, 2, 1);
307 void jazz_led_init(MemoryRegion *address_space, target_phys_addr_t base)
309 LedState *s;
311 s = g_malloc0(sizeof(LedState));
313 s->state = REDRAW_SEGMENTS | REDRAW_BACKGROUND;
315 memory_region_init_io(&s->iomem, &led_ops, s, "led", 1);
316 memory_region_add_subregion(address_space, base, &s->iomem);
318 s->ds = graphic_console_init(jazz_led_update_display,
319 jazz_led_invalidate_display,
320 jazz_led_screen_dump,
321 jazz_led_text_update, s);
322 qemu_console_resize(s->ds, 60, 80);