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 "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/module.h"
28 #include "ui/console.h"
29 #include "ui/pixel_ops.h"
31 #include "hw/sysbus.h"
32 #include "migration/vmstate.h"
33 #include "qom/object.h"
35 typedef struct G364State
{
39 MemoryRegion mem_vram
;
40 MemoryRegion mem_ctrl
;
42 uint8_t color_palette
[256][3];
43 uint8_t cursor_palette
[3][3];
45 uint32_t cursor_position
;
47 uint32_t top_of_screen
;
48 uint32_t width
, height
; /* in pixels */
49 /* display refresh support */
55 #define REG_BOOT 0x000000
56 #define REG_DISPLAY 0x000118
57 #define REG_VDISPLAY 0x000150
58 #define REG_CTLA 0x000300
59 #define REG_TOP 0x000400
60 #define REG_CURS_PAL 0x000508
61 #define REG_CURS_POS 0x000638
62 #define REG_CLR_PAL 0x000800
63 #define REG_CURS_PAT 0x001000
64 #define REG_RESET 0x100000
66 #define CTLA_FORCE_BLANK 0x00000400
67 #define CTLA_NO_CURSOR 0x00800000
69 #define G364_PAGE_SIZE 4096
71 static inline int check_dirty(G364State
*s
, DirtyBitmapSnapshot
*snap
, ram_addr_t page
)
73 return memory_region_snapshot_get_dirty(&s
->mem_vram
, snap
, page
, G364_PAGE_SIZE
);
76 static void g364fb_draw_graphic8(G364State
*s
)
78 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
79 DirtyBitmapSnapshot
*snap
;
82 uint8_t *data_display
, *dd
;
88 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
90 switch (surface_bits_per_pixel(surface
)) {
92 rgb_to_pixel
= rgb_to_pixel8
;
96 rgb_to_pixel
= rgb_to_pixel15
;
100 rgb_to_pixel
= rgb_to_pixel16
;
104 rgb_to_pixel
= rgb_to_pixel32
;
108 hw_error("g364: unknown host depth %d",
109 surface_bits_per_pixel(surface
));
121 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
122 xcursor
= s
->cursor_position
>> 12;
123 ycursor
= s
->cursor_position
& 0xfff;
125 xcursor
= ycursor
= -65;
128 vram
= memory_region_get_ram_ptr(&s
->mem_vram
) + s
->top_of_screen
;
129 /* XXX: out of range in vram? */
130 data_display
= dd
= surface_data(surface
);
131 snap
= memory_region_snapshot_and_clear_dirty(&s
->mem_vram
, 0, s
->vram_size
,
133 while (y
< s
->height
) {
134 if (check_dirty(s
, snap
, page
)) {
139 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
142 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
143 (x
>= xcursor
&& x
< xcursor
+ 64))) {
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)) {
151 color
= (*rgb_to_pixel
)(
152 s
->color_palette
[index
][0],
153 s
->color_palette
[index
][1],
154 s
->color_palette
[index
][2]);
156 /* get cursor color */
158 color
= (*rgb_to_pixel
)(
159 s
->cursor_palette
[index
][0],
160 s
->cursor_palette
[index
][1],
161 s
->cursor_palette
[index
][2]);
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
);
178 if (y
== s
->height
) {
179 ymax
= s
->height
- 1;
182 data_display
= dd
= data_display
+ surface_stride(surface
);
194 dpy_gfx_update(s
->con
, xmin
, ymin
,
195 xmax
- xmin
+ 1, ymax
- ymin
+ 1);
205 vram
+= G364_PAGE_SIZE
;
206 data_display
+= dy
* surface_stride(surface
);
207 dd
= data_display
+ x
* w
;
209 page
+= G364_PAGE_SIZE
;
214 dpy_gfx_update(s
->con
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
219 static void g364fb_draw_blank(G364State
*s
)
221 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
226 /* Screen is already blank. No need to redraw it */
230 w
= s
->width
* surface_bytes_per_pixel(surface
);
231 d
= surface_data(surface
);
232 for (i
= 0; i
< s
->height
; i
++) {
234 d
+= surface_stride(surface
);
237 dpy_gfx_update_full(s
->con
);
241 static void g364fb_update_display(void *opaque
)
243 G364State
*s
= opaque
;
244 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
246 qemu_flush_coalesced_mmio_buffer();
248 if (s
->width
== 0 || s
->height
== 0)
251 if (s
->width
!= surface_width(surface
) ||
252 s
->height
!= surface_height(surface
)) {
253 qemu_console_resize(s
->con
, s
->width
, s
->height
);
256 if (s
->ctla
& CTLA_FORCE_BLANK
) {
257 g364fb_draw_blank(s
);
258 } else if (s
->depth
== 8) {
259 g364fb_draw_graphic8(s
);
261 error_report("g364: unknown guest depth %d", s
->depth
);
264 qemu_irq_raise(s
->irq
);
267 static inline void g364fb_invalidate_display(void *opaque
)
269 G364State
*s
= opaque
;
272 memory_region_set_dirty(&s
->mem_vram
, 0, s
->vram_size
);
275 static void g364fb_reset(G364State
*s
)
277 uint8_t *vram
= memory_region_get_ram_ptr(&s
->mem_vram
);
279 qemu_irq_lower(s
->irq
);
281 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
282 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
283 memset(s
->cursor
, 0, sizeof(s
->cursor
));
284 s
->cursor_position
= 0;
286 s
->top_of_screen
= 0;
287 s
->width
= s
->height
= 0;
288 memset(vram
, 0, s
->vram_size
);
289 g364fb_invalidate_display(s
);
292 /* called for accesses to io ports */
293 static uint64_t g364fb_ctrl_read(void *opaque
,
297 G364State
*s
= opaque
;
300 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
302 int idx
= (addr
- REG_CURS_PAT
) >> 3;
303 val
= s
->cursor
[idx
];
304 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
306 int idx
= (addr
- REG_CURS_PAL
) >> 3;
307 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
308 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
309 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
323 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
331 trace_g364fb_read(addr
, val
);
336 static void g364fb_update_depth(G364State
*s
)
338 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
339 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
342 static void g364_invalidate_cursor_position(G364State
*s
)
344 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
345 int ymin
, ymax
, start
, end
;
347 /* invalidate only near the cursor */
348 ymin
= s
->cursor_position
& 0xfff;
349 ymax
= MIN(s
->height
, ymin
+ 64);
350 start
= ymin
* surface_stride(surface
);
351 end
= (ymax
+ 1) * surface_stride(surface
);
353 memory_region_set_dirty(&s
->mem_vram
, start
, end
- start
);
356 static void g364fb_ctrl_write(void *opaque
,
361 G364State
*s
= opaque
;
363 trace_g364fb_write(addr
, val
);
365 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
367 int idx
= (addr
- REG_CLR_PAL
) >> 3;
368 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
369 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
370 s
->color_palette
[idx
][2] = val
& 0xff;
371 g364fb_invalidate_display(s
);
372 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
374 int idx
= (addr
- REG_CURS_PAT
) >> 3;
375 s
->cursor
[idx
] = val
;
376 g364fb_invalidate_display(s
);
377 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
379 int idx
= (addr
- REG_CURS_PAL
) >> 3;
380 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
381 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
382 s
->cursor_palette
[idx
][2] = val
& 0xff;
383 g364fb_invalidate_display(s
);
386 case REG_BOOT
: /* Boot timing */
387 case 0x00108: /* Line timing: half sync */
388 case 0x00110: /* Line timing: back porch */
389 case 0x00120: /* Line timing: short display */
390 case 0x00128: /* Frame timing: broad pulse */
391 case 0x00130: /* Frame timing: v sync */
392 case 0x00138: /* Frame timing: v preequalise */
393 case 0x00140: /* Frame timing: v postequalise */
394 case 0x00148: /* Frame timing: v blank */
395 case 0x00158: /* Line timing: line time */
396 case 0x00160: /* Frame store: line start */
397 case 0x00168: /* vram cycle: mem init */
398 case 0x00170: /* vram cycle: transfer delay */
399 case 0x00200: /* vram cycle: mask register */
403 s
->top_of_screen
= val
;
404 g364fb_invalidate_display(s
);
414 g364fb_update_depth(s
);
415 g364fb_invalidate_display(s
);
418 g364_invalidate_cursor_position(s
);
419 s
->cursor_position
= val
;
420 g364_invalidate_cursor_position(s
);
426 error_report("g364: invalid write of 0x%" PRIx64
427 " at [" TARGET_FMT_plx
"]", val
, addr
);
431 qemu_irq_lower(s
->irq
);
434 static const MemoryRegionOps g364fb_ctrl_ops
= {
435 .read
= g364fb_ctrl_read
,
436 .write
= g364fb_ctrl_write
,
437 .endianness
= DEVICE_LITTLE_ENDIAN
,
438 .impl
.min_access_size
= 4,
439 .impl
.max_access_size
= 4,
442 static int g364fb_post_load(void *opaque
, int version_id
)
444 G364State
*s
= opaque
;
447 g364fb_update_depth(s
);
448 g364fb_invalidate_display(s
);
453 static const VMStateDescription vmstate_g364fb
= {
456 .minimum_version_id
= 2,
457 .post_load
= g364fb_post_load
,
458 .fields
= (VMStateField
[]) {
459 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
460 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
461 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
462 VMSTATE_UINT32(cursor_position
, G364State
),
463 VMSTATE_UINT32(ctla
, G364State
),
464 VMSTATE_UINT32(top_of_screen
, G364State
),
465 VMSTATE_UINT32(width
, G364State
),
466 VMSTATE_UINT32(height
, G364State
),
467 VMSTATE_END_OF_LIST()
471 static const GraphicHwOps g364fb_ops
= {
472 .invalidate
= g364fb_invalidate_display
,
473 .gfx_update
= g364fb_update_display
,
476 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
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(&s
->mem_vram
, NULL
, "g364fb.vram", s
->vram_size
,
484 memory_region_set_log(&s
->mem_vram
, true, DIRTY_MEMORY_VGA
);
487 #define TYPE_G364 "sysbus-g364"
488 OBJECT_DECLARE_SIMPLE_TYPE(G364SysBusState
, G364
)
490 struct G364SysBusState
{
491 SysBusDevice parent_obj
;
496 static void g364fb_sysbus_realize(DeviceState
*dev
, Error
**errp
)
498 G364SysBusState
*sbs
= G364(dev
);
499 G364State
*s
= &sbs
->g364
;
500 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
503 sysbus_init_irq(sbd
, &s
->irq
);
504 sysbus_init_mmio(sbd
, &s
->mem_ctrl
);
505 sysbus_init_mmio(sbd
, &s
->mem_vram
);
508 static void g364fb_sysbus_reset(DeviceState
*d
)
510 G364SysBusState
*s
= G364(d
);
512 g364fb_reset(&s
->g364
);
515 static Property g364fb_sysbus_properties
[] = {
516 DEFINE_PROP_UINT32("vram_size", G364SysBusState
, g364
.vram_size
, 8 * MiB
),
517 DEFINE_PROP_END_OF_LIST(),
520 static const VMStateDescription vmstate_g364fb_sysbus
= {
521 .name
= "g364fb-sysbus",
523 .minimum_version_id
= 2,
524 .fields
= (VMStateField
[]) {
525 VMSTATE_STRUCT(g364
, G364SysBusState
, 2, vmstate_g364fb
, G364State
),
526 VMSTATE_END_OF_LIST()
530 static void g364fb_sysbus_class_init(ObjectClass
*klass
, void *data
)
532 DeviceClass
*dc
= DEVICE_CLASS(klass
);
534 dc
->realize
= g364fb_sysbus_realize
;
535 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
536 dc
->desc
= "G364 framebuffer";
537 dc
->reset
= g364fb_sysbus_reset
;
538 dc
->vmsd
= &vmstate_g364fb_sysbus
;
539 device_class_set_props(dc
, g364fb_sysbus_properties
);
542 static const TypeInfo g364fb_sysbus_info
= {
544 .parent
= TYPE_SYS_BUS_DEVICE
,
545 .instance_size
= sizeof(G364SysBusState
),
546 .class_init
= g364fb_sysbus_class_init
,
549 static void g364fb_register_types(void)
551 type_register_static(&g364fb_sysbus_info
);
554 type_init(g364fb_register_types
)