ide: Update command code definitions as per ACS-2 Table B.2
[qemu/ar7.git] / hw / g364fb.c
blob5e7bcfa278e80795de39fa9d6ec422a8f508a65a
1 /*
2 * QEMU G364 framebuffer Emulator.
4 * Copyright (c) 2007-2011 Herve Poussineau
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw.h"
21 #include "console.h"
22 #include "pixel_ops.h"
23 #include "trace.h"
24 #include "sysbus.h"
26 typedef struct G364State {
27 /* hardware */
28 uint8_t *vram;
29 uint32_t vram_size;
30 qemu_irq irq;
31 MemoryRegion mem_vram;
32 MemoryRegion mem_ctrl;
33 /* registers */
34 uint8_t color_palette[256][3];
35 uint8_t cursor_palette[3][3];
36 uint16_t cursor[512];
37 uint32_t cursor_position;
38 uint32_t ctla;
39 uint32_t top_of_screen;
40 uint32_t width, height; /* in pixels */
41 /* display refresh support */
42 DisplayState *ds;
43 int depth;
44 int blanked;
45 } G364State;
47 #define REG_BOOT 0x000000
48 #define REG_DISPLAY 0x000118
49 #define REG_VDISPLAY 0x000150
50 #define REG_CTLA 0x000300
51 #define REG_TOP 0x000400
52 #define REG_CURS_PAL 0x000508
53 #define REG_CURS_POS 0x000638
54 #define REG_CLR_PAL 0x000800
55 #define REG_CURS_PAT 0x001000
56 #define REG_RESET 0x100000
58 #define CTLA_FORCE_BLANK 0x00000400
59 #define CTLA_NO_CURSOR 0x00800000
61 static inline int check_dirty(G364State *s, ram_addr_t page)
63 return memory_region_get_dirty(&s->mem_vram, page, DIRTY_MEMORY_VGA);
66 static inline void reset_dirty(G364State *s,
67 ram_addr_t page_min, ram_addr_t page_max)
69 memory_region_reset_dirty(&s->mem_vram,
70 page_min,
71 page_max + TARGET_PAGE_SIZE - page_min - 1,
72 DIRTY_MEMORY_VGA);
75 static void g364fb_draw_graphic8(G364State *s)
77 int i, w;
78 uint8_t *vram;
79 uint8_t *data_display, *dd;
80 ram_addr_t page, page_min, page_max;
81 int x, y;
82 int xmin, xmax;
83 int ymin, ymax;
84 int xcursor, ycursor;
85 unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned int b);
87 switch (ds_get_bits_per_pixel(s->ds)) {
88 case 8:
89 rgb_to_pixel = rgb_to_pixel8;
90 w = 1;
91 break;
92 case 15:
93 rgb_to_pixel = rgb_to_pixel15;
94 w = 2;
95 break;
96 case 16:
97 rgb_to_pixel = rgb_to_pixel16;
98 w = 2;
99 break;
100 case 32:
101 rgb_to_pixel = rgb_to_pixel32;
102 w = 4;
103 break;
104 default:
105 hw_error("g364: unknown host depth %d",
106 ds_get_bits_per_pixel(s->ds));
107 return;
110 page = 0;
111 page_min = (ram_addr_t)-1;
112 page_max = 0;
114 x = y = 0;
115 xmin = s->width;
116 xmax = 0;
117 ymin = s->height;
118 ymax = 0;
120 if (!(s->ctla & CTLA_NO_CURSOR)) {
121 xcursor = s->cursor_position >> 12;
122 ycursor = s->cursor_position & 0xfff;
123 } else {
124 xcursor = ycursor = -65;
127 vram = s->vram + s->top_of_screen;
128 /* XXX: out of range in vram? */
129 data_display = dd = ds_get_data(s->ds);
130 while (y < s->height) {
131 if (check_dirty(s, page)) {
132 if (y < ymin)
133 ymin = ymax = y;
134 if (page_min == (ram_addr_t)-1)
135 page_min = page;
136 page_max = page;
137 if (x < xmin)
138 xmin = x;
139 for (i = 0; i < TARGET_PAGE_SIZE; i++) {
140 uint8_t index;
141 unsigned int color;
142 if (unlikely((y >= ycursor && y < ycursor + 64) &&
143 (x >= xcursor && x < xcursor + 64))) {
144 /* pointer area */
145 int xdiff = x - xcursor;
146 uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
147 int op = (curs >> ((xdiff & 7) * 2)) & 3;
148 if (likely(op == 0)) {
149 /* transparent */
150 index = *vram;
151 color = (*rgb_to_pixel)(
152 s->color_palette[index][0],
153 s->color_palette[index][1],
154 s->color_palette[index][2]);
155 } else {
156 /* get cursor color */
157 index = op - 1;
158 color = (*rgb_to_pixel)(
159 s->cursor_palette[index][0],
160 s->cursor_palette[index][1],
161 s->cursor_palette[index][2]);
163 } else {
164 /* normal area */
165 index = *vram;
166 color = (*rgb_to_pixel)(
167 s->color_palette[index][0],
168 s->color_palette[index][1],
169 s->color_palette[index][2]);
171 memcpy(dd, &color, w);
172 dd += w;
173 x++;
174 vram++;
175 if (x == s->width) {
176 xmax = s->width - 1;
177 y++;
178 if (y == s->height) {
179 ymax = s->height - 1;
180 goto done;
182 data_display = dd = data_display + ds_get_linesize(s->ds);
183 xmin = 0;
184 x = 0;
187 if (x > xmax)
188 xmax = x;
189 if (y > ymax)
190 ymax = y;
191 } else {
192 int dy;
193 if (page_min != (ram_addr_t)-1) {
194 reset_dirty(s, page_min, page_max);
195 page_min = (ram_addr_t)-1;
196 page_max = 0;
197 dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
198 xmin = s->width;
199 xmax = 0;
200 ymin = s->height;
201 ymax = 0;
203 x += TARGET_PAGE_SIZE;
204 dy = x / s->width;
205 x = x % s->width;
206 y += dy;
207 vram += TARGET_PAGE_SIZE;
208 data_display += dy * ds_get_linesize(s->ds);
209 dd = data_display + x * w;
211 page += TARGET_PAGE_SIZE;
214 done:
215 if (page_min != (ram_addr_t)-1) {
216 dpy_update(s->ds, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
217 reset_dirty(s, page_min, page_max);
221 static void g364fb_draw_blank(G364State *s)
223 int i, w;
224 uint8_t *d;
226 if (s->blanked) {
227 /* Screen is already blank. No need to redraw it */
228 return;
231 w = s->width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
232 d = ds_get_data(s->ds);
233 for (i = 0; i < s->height; i++) {
234 memset(d, 0, w);
235 d += ds_get_linesize(s->ds);
238 dpy_update(s->ds, 0, 0, s->width, s->height);
239 s->blanked = 1;
242 static void g364fb_update_display(void *opaque)
244 G364State *s = opaque;
246 if (s->width == 0 || s->height == 0)
247 return;
249 if (s->width != ds_get_width(s->ds) || s->height != ds_get_height(s->ds)) {
250 qemu_console_resize(s->ds, s->width, s->height);
253 if (s->ctla & CTLA_FORCE_BLANK) {
254 g364fb_draw_blank(s);
255 } else if (s->depth == 8) {
256 g364fb_draw_graphic8(s);
257 } else {
258 error_report("g364: unknown guest depth %d", s->depth);
261 qemu_irq_raise(s->irq);
264 static inline void g364fb_invalidate_display(void *opaque)
266 G364State *s = opaque;
267 int i;
269 s->blanked = 0;
270 for (i = 0; i < s->vram_size; i += TARGET_PAGE_SIZE) {
271 memory_region_set_dirty(&s->mem_vram, i);
275 static void g364fb_reset(G364State *s)
277 qemu_irq_lower(s->irq);
279 memset(s->color_palette, 0, sizeof(s->color_palette));
280 memset(s->cursor_palette, 0, sizeof(s->cursor_palette));
281 memset(s->cursor, 0, sizeof(s->cursor));
282 s->cursor_position = 0;
283 s->ctla = 0;
284 s->top_of_screen = 0;
285 s->width = s->height = 0;
286 memset(s->vram, 0, s->vram_size);
287 g364fb_invalidate_display(s);
290 static void g364fb_screen_dump(void *opaque, const char *filename)
292 G364State *s = opaque;
293 int y, x;
294 uint8_t index;
295 uint8_t *data_buffer;
296 FILE *f;
298 if (s->depth != 8) {
299 error_report("g364: unknown guest depth %d", s->depth);
300 return;
303 f = fopen(filename, "wb");
304 if (!f)
305 return;
307 if (s->ctla & CTLA_FORCE_BLANK) {
308 /* blank screen */
309 fprintf(f, "P4\n%d %d\n",
310 s->width, s->height);
311 for (y = 0; y < s->height; y++)
312 for (x = 0; x < s->width; x++)
313 fputc(0, f);
314 } else {
315 data_buffer = s->vram + s->top_of_screen;
316 fprintf(f, "P6\n%d %d\n%d\n",
317 s->width, s->height, 255);
318 for (y = 0; y < s->height; y++)
319 for (x = 0; x < s->width; x++, data_buffer++) {
320 index = *data_buffer;
321 fputc(s->color_palette[index][0], f);
322 fputc(s->color_palette[index][1], f);
323 fputc(s->color_palette[index][2], f);
327 fclose(f);
330 /* called for accesses to io ports */
331 static uint64_t g364fb_ctrl_read(void *opaque,
332 target_phys_addr_t addr,
333 unsigned int size)
335 G364State *s = opaque;
336 uint32_t val;
338 if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
339 /* cursor pattern */
340 int idx = (addr - REG_CURS_PAT) >> 3;
341 val = s->cursor[idx];
342 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
343 /* cursor palette */
344 int idx = (addr - REG_CURS_PAL) >> 3;
345 val = ((uint32_t)s->cursor_palette[idx][0] << 16);
346 val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
347 val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
348 } else {
349 switch (addr) {
350 case REG_DISPLAY:
351 val = s->width / 4;
352 break;
353 case REG_VDISPLAY:
354 val = s->height * 2;
355 break;
356 case REG_CTLA:
357 val = s->ctla;
358 break;
359 default:
361 error_report("g364: invalid read at [" TARGET_FMT_plx "]",
362 addr);
363 val = 0;
364 break;
369 trace_g364fb_read(addr, val);
371 return val;
374 static void g364fb_update_depth(G364State *s)
376 static const int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
377 s->depth = depths[(s->ctla & 0x00700000) >> 20];
380 static void g364_invalidate_cursor_position(G364State *s)
382 int ymin, ymax, start, end, i;
384 /* invalidate only near the cursor */
385 ymin = s->cursor_position & 0xfff;
386 ymax = MIN(s->height, ymin + 64);
387 start = ymin * ds_get_linesize(s->ds);
388 end = (ymax + 1) * ds_get_linesize(s->ds);
390 for (i = start; i < end; i += TARGET_PAGE_SIZE) {
391 memory_region_set_dirty(&s->mem_vram, i);
395 static void g364fb_ctrl_write(void *opaque,
396 target_phys_addr_t addr,
397 uint64_t val,
398 unsigned int size)
400 G364State *s = opaque;
402 trace_g364fb_write(addr, val);
404 if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
405 /* color palette */
406 int idx = (addr - REG_CLR_PAL) >> 3;
407 s->color_palette[idx][0] = (val >> 16) & 0xff;
408 s->color_palette[idx][1] = (val >> 8) & 0xff;
409 s->color_palette[idx][2] = val & 0xff;
410 g364fb_invalidate_display(s);
411 } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
412 /* cursor pattern */
413 int idx = (addr - REG_CURS_PAT) >> 3;
414 s->cursor[idx] = val;
415 g364fb_invalidate_display(s);
416 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
417 /* cursor palette */
418 int idx = (addr - REG_CURS_PAL) >> 3;
419 s->cursor_palette[idx][0] = (val >> 16) & 0xff;
420 s->cursor_palette[idx][1] = (val >> 8) & 0xff;
421 s->cursor_palette[idx][2] = val & 0xff;
422 g364fb_invalidate_display(s);
423 } else {
424 switch (addr) {
425 case REG_BOOT: /* Boot timing */
426 case 0x00108: /* Line timing: half sync */
427 case 0x00110: /* Line timing: back porch */
428 case 0x00120: /* Line timing: short display */
429 case 0x00128: /* Frame timing: broad pulse */
430 case 0x00130: /* Frame timing: v sync */
431 case 0x00138: /* Frame timing: v preequalise */
432 case 0x00140: /* Frame timing: v postequalise */
433 case 0x00148: /* Frame timing: v blank */
434 case 0x00158: /* Line timing: line time */
435 case 0x00160: /* Frame store: line start */
436 case 0x00168: /* vram cycle: mem init */
437 case 0x00170: /* vram cycle: transfer delay */
438 case 0x00200: /* vram cycle: mask register */
439 /* ignore */
440 break;
441 case REG_TOP:
442 s->top_of_screen = val;
443 g364fb_invalidate_display(s);
444 break;
445 case REG_DISPLAY:
446 s->width = val * 4;
447 break;
448 case REG_VDISPLAY:
449 s->height = val / 2;
450 break;
451 case REG_CTLA:
452 s->ctla = val;
453 g364fb_update_depth(s);
454 g364fb_invalidate_display(s);
455 break;
456 case REG_CURS_POS:
457 g364_invalidate_cursor_position(s);
458 s->cursor_position = val;
459 g364_invalidate_cursor_position(s);
460 break;
461 case REG_RESET:
462 g364fb_reset(s);
463 break;
464 default:
465 error_report("g364: invalid write of 0x%" PRIx64
466 " at [" TARGET_FMT_plx "]", val, addr);
467 break;
470 qemu_irq_lower(s->irq);
473 static const MemoryRegionOps g364fb_ctrl_ops = {
474 .read = g364fb_ctrl_read,
475 .write = g364fb_ctrl_write,
476 .endianness = DEVICE_LITTLE_ENDIAN,
477 .impl.min_access_size = 4,
478 .impl.max_access_size = 4,
481 static int g364fb_post_load(void *opaque, int version_id)
483 G364State *s = opaque;
485 /* force refresh */
486 g364fb_update_depth(s);
487 g364fb_invalidate_display(s);
489 return 0;
492 static const VMStateDescription vmstate_g364fb = {
493 .name = "g364fb",
494 .version_id = 1,
495 .minimum_version_id = 1,
496 .minimum_version_id_old = 1,
497 .post_load = g364fb_post_load,
498 .fields = (VMStateField[]) {
499 VMSTATE_VBUFFER_UINT32(vram, G364State, 1, NULL, 0, vram_size),
500 VMSTATE_BUFFER_UNSAFE(color_palette, G364State, 0, 256 * 3),
501 VMSTATE_BUFFER_UNSAFE(cursor_palette, G364State, 0, 9),
502 VMSTATE_UINT16_ARRAY(cursor, G364State, 512),
503 VMSTATE_UINT32(cursor_position, G364State),
504 VMSTATE_UINT32(ctla, G364State),
505 VMSTATE_UINT32(top_of_screen, G364State),
506 VMSTATE_UINT32(width, G364State),
507 VMSTATE_UINT32(height, G364State),
508 VMSTATE_END_OF_LIST()
512 static void g364fb_init(DeviceState *dev, G364State *s)
514 s->vram = g_malloc0(s->vram_size);
516 s->ds = graphic_console_init(g364fb_update_display,
517 g364fb_invalidate_display,
518 g364fb_screen_dump, NULL, s);
520 memory_region_init_io(&s->mem_ctrl, &g364fb_ctrl_ops, s, "ctrl", 0x180000);
521 memory_region_init_ram_ptr(&s->mem_vram, dev, "vram",
522 s->vram_size, s->vram);
523 memory_region_set_coalescing(&s->mem_vram);
526 typedef struct {
527 SysBusDevice busdev;
528 G364State g364;
529 } G364SysBusState;
531 static int g364fb_sysbus_init(SysBusDevice *dev)
533 G364State *s = &FROM_SYSBUS(G364SysBusState, dev)->g364;
535 g364fb_init(&dev->qdev, s);
536 sysbus_init_irq(dev, &s->irq);
537 sysbus_init_mmio_region(dev, &s->mem_ctrl);
538 sysbus_init_mmio_region(dev, &s->mem_vram);
540 return 0;
543 static void g364fb_sysbus_reset(DeviceState *d)
545 G364SysBusState *s = DO_UPCAST(G364SysBusState, busdev.qdev, d);
546 g364fb_reset(&s->g364);
549 static SysBusDeviceInfo g364fb_sysbus_info = {
550 .init = g364fb_sysbus_init,
551 .qdev.name = "sysbus-g364",
552 .qdev.desc = "G364 framebuffer",
553 .qdev.size = sizeof(G364SysBusState),
554 .qdev.vmsd = &vmstate_g364fb,
555 .qdev.reset = g364fb_sysbus_reset,
556 .qdev.props = (Property[]) {
557 DEFINE_PROP_HEX32("vram_size", G364SysBusState, g364.vram_size,
558 8 * 1024 * 1024),
559 DEFINE_PROP_END_OF_LIST(),
563 static void g364fb_register(void)
565 sysbus_register_withprop(&g364fb_sysbus_info);
568 device_init(g364fb_register);