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 "qemu/osdep.h"
21 #include "qemu/units.h"
24 #include "hw/qdev-properties.h"
25 #include "qemu/error-report.h"
26 #include "qemu/module.h"
27 #include "ui/console.h"
28 #include "ui/pixel_ops.h"
30 #include "hw/sysbus.h"
31 #include "migration/vmstate.h"
33 typedef struct G364State
{
38 MemoryRegion mem_vram
;
39 MemoryRegion mem_ctrl
;
41 uint8_t color_palette
[256][3];
42 uint8_t cursor_palette
[3][3];
44 uint32_t cursor_position
;
46 uint32_t top_of_screen
;
47 uint32_t width
, height
; /* in pixels */
48 /* display refresh support */
54 #define REG_BOOT 0x000000
55 #define REG_DISPLAY 0x000118
56 #define REG_VDISPLAY 0x000150
57 #define REG_CTLA 0x000300
58 #define REG_TOP 0x000400
59 #define REG_CURS_PAL 0x000508
60 #define REG_CURS_POS 0x000638
61 #define REG_CLR_PAL 0x000800
62 #define REG_CURS_PAT 0x001000
63 #define REG_RESET 0x100000
65 #define CTLA_FORCE_BLANK 0x00000400
66 #define CTLA_NO_CURSOR 0x00800000
68 #define G364_PAGE_SIZE 4096
70 static inline int check_dirty(G364State
*s
, DirtyBitmapSnapshot
*snap
, ram_addr_t page
)
72 return memory_region_snapshot_get_dirty(&s
->mem_vram
, snap
, page
, G364_PAGE_SIZE
);
75 static void g364fb_draw_graphic8(G364State
*s
)
77 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
78 DirtyBitmapSnapshot
*snap
;
81 uint8_t *data_display
, *dd
;
87 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
89 switch (surface_bits_per_pixel(surface
)) {
91 rgb_to_pixel
= rgb_to_pixel8
;
95 rgb_to_pixel
= rgb_to_pixel15
;
99 rgb_to_pixel
= rgb_to_pixel16
;
103 rgb_to_pixel
= rgb_to_pixel32
;
107 hw_error("g364: unknown host depth %d",
108 surface_bits_per_pixel(surface
));
120 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
121 xcursor
= s
->cursor_position
>> 12;
122 ycursor
= s
->cursor_position
& 0xfff;
124 xcursor
= ycursor
= -65;
127 vram
= s
->vram
+ s
->top_of_screen
;
128 /* XXX: out of range in vram? */
129 data_display
= dd
= surface_data(surface
);
130 snap
= memory_region_snapshot_and_clear_dirty(&s
->mem_vram
, 0, s
->vram_size
,
132 while (y
< s
->height
) {
133 if (check_dirty(s
, snap
, page
)) {
138 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
141 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
142 (x
>= xcursor
&& x
< xcursor
+ 64))) {
144 int xdiff
= x
- xcursor
;
145 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
146 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
147 if (likely(op
== 0)) {
150 color
= (*rgb_to_pixel
)(
151 s
->color_palette
[index
][0],
152 s
->color_palette
[index
][1],
153 s
->color_palette
[index
][2]);
155 /* get cursor color */
157 color
= (*rgb_to_pixel
)(
158 s
->cursor_palette
[index
][0],
159 s
->cursor_palette
[index
][1],
160 s
->cursor_palette
[index
][2]);
165 color
= (*rgb_to_pixel
)(
166 s
->color_palette
[index
][0],
167 s
->color_palette
[index
][1],
168 s
->color_palette
[index
][2]);
170 memcpy(dd
, &color
, w
);
177 if (y
== s
->height
) {
178 ymax
= s
->height
- 1;
181 data_display
= dd
= data_display
+ surface_stride(surface
);
193 dpy_gfx_update(s
->con
, xmin
, ymin
,
194 xmax
- xmin
+ 1, ymax
- ymin
+ 1);
204 vram
+= G364_PAGE_SIZE
;
205 data_display
+= dy
* surface_stride(surface
);
206 dd
= data_display
+ x
* w
;
208 page
+= G364_PAGE_SIZE
;
213 dpy_gfx_update(s
->con
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
218 static void g364fb_draw_blank(G364State
*s
)
220 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
225 /* Screen is already blank. No need to redraw it */
229 w
= s
->width
* surface_bytes_per_pixel(surface
);
230 d
= surface_data(surface
);
231 for (i
= 0; i
< s
->height
; i
++) {
233 d
+= surface_stride(surface
);
236 dpy_gfx_update_full(s
->con
);
240 static void g364fb_update_display(void *opaque
)
242 G364State
*s
= opaque
;
243 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
245 qemu_flush_coalesced_mmio_buffer();
247 if (s
->width
== 0 || s
->height
== 0)
250 if (s
->width
!= surface_width(surface
) ||
251 s
->height
!= surface_height(surface
)) {
252 qemu_console_resize(s
->con
, s
->width
, s
->height
);
255 if (s
->ctla
& CTLA_FORCE_BLANK
) {
256 g364fb_draw_blank(s
);
257 } else if (s
->depth
== 8) {
258 g364fb_draw_graphic8(s
);
260 error_report("g364: unknown guest depth %d", s
->depth
);
263 qemu_irq_raise(s
->irq
);
266 static inline void g364fb_invalidate_display(void *opaque
)
268 G364State
*s
= opaque
;
271 memory_region_set_dirty(&s
->mem_vram
, 0, s
->vram_size
);
274 static void g364fb_reset(G364State
*s
)
276 qemu_irq_lower(s
->irq
);
278 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
279 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
280 memset(s
->cursor
, 0, sizeof(s
->cursor
));
281 s
->cursor_position
= 0;
283 s
->top_of_screen
= 0;
284 s
->width
= s
->height
= 0;
285 memset(s
->vram
, 0, s
->vram_size
);
286 g364fb_invalidate_display(s
);
289 /* called for accesses to io ports */
290 static uint64_t g364fb_ctrl_read(void *opaque
,
294 G364State
*s
= opaque
;
297 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
299 int idx
= (addr
- REG_CURS_PAT
) >> 3;
300 val
= s
->cursor
[idx
];
301 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
303 int idx
= (addr
- REG_CURS_PAL
) >> 3;
304 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
305 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
306 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
320 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
328 trace_g364fb_read(addr
, val
);
333 static void g364fb_update_depth(G364State
*s
)
335 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
336 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
339 static void g364_invalidate_cursor_position(G364State
*s
)
341 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
342 int ymin
, ymax
, start
, end
;
344 /* invalidate only near the cursor */
345 ymin
= s
->cursor_position
& 0xfff;
346 ymax
= MIN(s
->height
, ymin
+ 64);
347 start
= ymin
* surface_stride(surface
);
348 end
= (ymax
+ 1) * surface_stride(surface
);
350 memory_region_set_dirty(&s
->mem_vram
, start
, end
- start
);
353 static void g364fb_ctrl_write(void *opaque
,
358 G364State
*s
= opaque
;
360 trace_g364fb_write(addr
, val
);
362 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
364 int idx
= (addr
- REG_CLR_PAL
) >> 3;
365 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
366 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
367 s
->color_palette
[idx
][2] = val
& 0xff;
368 g364fb_invalidate_display(s
);
369 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
371 int idx
= (addr
- REG_CURS_PAT
) >> 3;
372 s
->cursor
[idx
] = val
;
373 g364fb_invalidate_display(s
);
374 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
376 int idx
= (addr
- REG_CURS_PAL
) >> 3;
377 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
378 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
379 s
->cursor_palette
[idx
][2] = val
& 0xff;
380 g364fb_invalidate_display(s
);
383 case REG_BOOT
: /* Boot timing */
384 case 0x00108: /* Line timing: half sync */
385 case 0x00110: /* Line timing: back porch */
386 case 0x00120: /* Line timing: short display */
387 case 0x00128: /* Frame timing: broad pulse */
388 case 0x00130: /* Frame timing: v sync */
389 case 0x00138: /* Frame timing: v preequalise */
390 case 0x00140: /* Frame timing: v postequalise */
391 case 0x00148: /* Frame timing: v blank */
392 case 0x00158: /* Line timing: line time */
393 case 0x00160: /* Frame store: line start */
394 case 0x00168: /* vram cycle: mem init */
395 case 0x00170: /* vram cycle: transfer delay */
396 case 0x00200: /* vram cycle: mask register */
400 s
->top_of_screen
= val
;
401 g364fb_invalidate_display(s
);
411 g364fb_update_depth(s
);
412 g364fb_invalidate_display(s
);
415 g364_invalidate_cursor_position(s
);
416 s
->cursor_position
= val
;
417 g364_invalidate_cursor_position(s
);
423 error_report("g364: invalid write of 0x%" PRIx64
424 " at [" TARGET_FMT_plx
"]", val
, addr
);
428 qemu_irq_lower(s
->irq
);
431 static const MemoryRegionOps g364fb_ctrl_ops
= {
432 .read
= g364fb_ctrl_read
,
433 .write
= g364fb_ctrl_write
,
434 .endianness
= DEVICE_LITTLE_ENDIAN
,
435 .impl
.min_access_size
= 4,
436 .impl
.max_access_size
= 4,
439 static int g364fb_post_load(void *opaque
, int version_id
)
441 G364State
*s
= opaque
;
444 g364fb_update_depth(s
);
445 g364fb_invalidate_display(s
);
450 static const VMStateDescription vmstate_g364fb
= {
453 .minimum_version_id
= 1,
454 .post_load
= g364fb_post_load
,
455 .fields
= (VMStateField
[]) {
456 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, vram_size
),
457 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
458 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
459 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
460 VMSTATE_UINT32(cursor_position
, G364State
),
461 VMSTATE_UINT32(ctla
, G364State
),
462 VMSTATE_UINT32(top_of_screen
, G364State
),
463 VMSTATE_UINT32(width
, G364State
),
464 VMSTATE_UINT32(height
, G364State
),
465 VMSTATE_END_OF_LIST()
469 static const GraphicHwOps g364fb_ops
= {
470 .invalidate
= g364fb_invalidate_display
,
471 .gfx_update
= g364fb_update_display
,
474 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
476 s
->vram
= g_malloc0(s
->vram_size
);
478 s
->con
= graphic_console_init(dev
, 0, &g364fb_ops
, s
);
480 memory_region_init_io(&s
->mem_ctrl
, OBJECT(dev
), &g364fb_ctrl_ops
, s
,
482 memory_region_init_ram_ptr(&s
->mem_vram
, NULL
, "vram",
483 s
->vram_size
, s
->vram
);
484 vmstate_register_ram(&s
->mem_vram
, dev
);
485 memory_region_set_log(&s
->mem_vram
, true, DIRTY_MEMORY_VGA
);
488 #define TYPE_G364 "sysbus-g364"
489 #define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
492 SysBusDevice parent_obj
;
497 static void g364fb_sysbus_realize(DeviceState
*dev
, Error
**errp
)
499 G364SysBusState
*sbs
= G364(dev
);
500 G364State
*s
= &sbs
->g364
;
501 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
504 sysbus_init_irq(sbd
, &s
->irq
);
505 sysbus_init_mmio(sbd
, &s
->mem_ctrl
);
506 sysbus_init_mmio(sbd
, &s
->mem_vram
);
509 static void g364fb_sysbus_reset(DeviceState
*d
)
511 G364SysBusState
*s
= G364(d
);
513 g364fb_reset(&s
->g364
);
516 static Property g364fb_sysbus_properties
[] = {
517 DEFINE_PROP_UINT32("vram_size", G364SysBusState
, g364
.vram_size
, 8 * MiB
),
518 DEFINE_PROP_END_OF_LIST(),
521 static void g364fb_sysbus_class_init(ObjectClass
*klass
, void *data
)
523 DeviceClass
*dc
= DEVICE_CLASS(klass
);
525 dc
->realize
= g364fb_sysbus_realize
;
526 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
527 dc
->desc
= "G364 framebuffer";
528 dc
->reset
= g364fb_sysbus_reset
;
529 dc
->vmsd
= &vmstate_g364fb
;
530 device_class_set_props(dc
, g364fb_sysbus_properties
);
533 static const TypeInfo g364fb_sysbus_info
= {
535 .parent
= TYPE_SYS_BUS_DEVICE
,
536 .instance_size
= sizeof(G364SysBusState
),
537 .class_init
= g364fb_sysbus_class_init
,
540 static void g364fb_register_types(void)
542 type_register_static(&g364fb_sysbus_info
);
545 type_init(g364fb_register_types
)