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"
23 #include "qemu/error-report.h"
24 #include "qemu/module.h"
25 #include "ui/console.h"
26 #include "ui/pixel_ops.h"
28 #include "hw/sysbus.h"
30 typedef struct G364State
{
35 MemoryRegion mem_vram
;
36 MemoryRegion mem_ctrl
;
38 uint8_t color_palette
[256][3];
39 uint8_t cursor_palette
[3][3];
41 uint32_t cursor_position
;
43 uint32_t top_of_screen
;
44 uint32_t width
, height
; /* in pixels */
45 /* display refresh support */
51 #define REG_BOOT 0x000000
52 #define REG_DISPLAY 0x000118
53 #define REG_VDISPLAY 0x000150
54 #define REG_CTLA 0x000300
55 #define REG_TOP 0x000400
56 #define REG_CURS_PAL 0x000508
57 #define REG_CURS_POS 0x000638
58 #define REG_CLR_PAL 0x000800
59 #define REG_CURS_PAT 0x001000
60 #define REG_RESET 0x100000
62 #define CTLA_FORCE_BLANK 0x00000400
63 #define CTLA_NO_CURSOR 0x00800000
65 #define G364_PAGE_SIZE 4096
67 static inline int check_dirty(G364State
*s
, DirtyBitmapSnapshot
*snap
, ram_addr_t page
)
69 return memory_region_snapshot_get_dirty(&s
->mem_vram
, snap
, page
, G364_PAGE_SIZE
);
72 static void g364fb_draw_graphic8(G364State
*s
)
74 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
75 DirtyBitmapSnapshot
*snap
;
78 uint8_t *data_display
, *dd
;
84 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
86 switch (surface_bits_per_pixel(surface
)) {
88 rgb_to_pixel
= rgb_to_pixel8
;
92 rgb_to_pixel
= rgb_to_pixel15
;
96 rgb_to_pixel
= rgb_to_pixel16
;
100 rgb_to_pixel
= rgb_to_pixel32
;
104 hw_error("g364: unknown host depth %d",
105 surface_bits_per_pixel(surface
));
117 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
118 xcursor
= s
->cursor_position
>> 12;
119 ycursor
= s
->cursor_position
& 0xfff;
121 xcursor
= ycursor
= -65;
124 vram
= s
->vram
+ s
->top_of_screen
;
125 /* XXX: out of range in vram? */
126 data_display
= dd
= surface_data(surface
);
127 snap
= memory_region_snapshot_and_clear_dirty(&s
->mem_vram
, 0, s
->vram_size
,
129 while (y
< s
->height
) {
130 if (check_dirty(s
, snap
, page
)) {
135 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
138 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
139 (x
>= xcursor
&& x
< xcursor
+ 64))) {
141 int xdiff
= x
- xcursor
;
142 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
143 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
144 if (likely(op
== 0)) {
147 color
= (*rgb_to_pixel
)(
148 s
->color_palette
[index
][0],
149 s
->color_palette
[index
][1],
150 s
->color_palette
[index
][2]);
152 /* get cursor color */
154 color
= (*rgb_to_pixel
)(
155 s
->cursor_palette
[index
][0],
156 s
->cursor_palette
[index
][1],
157 s
->cursor_palette
[index
][2]);
162 color
= (*rgb_to_pixel
)(
163 s
->color_palette
[index
][0],
164 s
->color_palette
[index
][1],
165 s
->color_palette
[index
][2]);
167 memcpy(dd
, &color
, w
);
174 if (y
== s
->height
) {
175 ymax
= s
->height
- 1;
178 data_display
= dd
= data_display
+ surface_stride(surface
);
190 dpy_gfx_update(s
->con
, xmin
, ymin
,
191 xmax
- xmin
+ 1, ymax
- ymin
+ 1);
201 vram
+= G364_PAGE_SIZE
;
202 data_display
+= dy
* surface_stride(surface
);
203 dd
= data_display
+ x
* w
;
205 page
+= G364_PAGE_SIZE
;
210 dpy_gfx_update(s
->con
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
215 static void g364fb_draw_blank(G364State
*s
)
217 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
222 /* Screen is already blank. No need to redraw it */
226 w
= s
->width
* surface_bytes_per_pixel(surface
);
227 d
= surface_data(surface
);
228 for (i
= 0; i
< s
->height
; i
++) {
230 d
+= surface_stride(surface
);
233 dpy_gfx_update_full(s
->con
);
237 static void g364fb_update_display(void *opaque
)
239 G364State
*s
= opaque
;
240 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
242 qemu_flush_coalesced_mmio_buffer();
244 if (s
->width
== 0 || s
->height
== 0)
247 if (s
->width
!= surface_width(surface
) ||
248 s
->height
!= surface_height(surface
)) {
249 qemu_console_resize(s
->con
, s
->width
, s
->height
);
252 if (s
->ctla
& CTLA_FORCE_BLANK
) {
253 g364fb_draw_blank(s
);
254 } else if (s
->depth
== 8) {
255 g364fb_draw_graphic8(s
);
257 error_report("g364: unknown guest depth %d", s
->depth
);
260 qemu_irq_raise(s
->irq
);
263 static inline void g364fb_invalidate_display(void *opaque
)
265 G364State
*s
= opaque
;
268 memory_region_set_dirty(&s
->mem_vram
, 0, s
->vram_size
);
271 static void g364fb_reset(G364State
*s
)
273 qemu_irq_lower(s
->irq
);
275 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
276 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
277 memset(s
->cursor
, 0, sizeof(s
->cursor
));
278 s
->cursor_position
= 0;
280 s
->top_of_screen
= 0;
281 s
->width
= s
->height
= 0;
282 memset(s
->vram
, 0, s
->vram_size
);
283 g364fb_invalidate_display(s
);
286 /* called for accesses to io ports */
287 static uint64_t g364fb_ctrl_read(void *opaque
,
291 G364State
*s
= opaque
;
294 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
296 int idx
= (addr
- REG_CURS_PAT
) >> 3;
297 val
= s
->cursor
[idx
];
298 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
300 int idx
= (addr
- REG_CURS_PAL
) >> 3;
301 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
302 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
303 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
317 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
325 trace_g364fb_read(addr
, val
);
330 static void g364fb_update_depth(G364State
*s
)
332 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
333 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
336 static void g364_invalidate_cursor_position(G364State
*s
)
338 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
339 int ymin
, ymax
, start
, end
;
341 /* invalidate only near the cursor */
342 ymin
= s
->cursor_position
& 0xfff;
343 ymax
= MIN(s
->height
, ymin
+ 64);
344 start
= ymin
* surface_stride(surface
);
345 end
= (ymax
+ 1) * surface_stride(surface
);
347 memory_region_set_dirty(&s
->mem_vram
, start
, end
- start
);
350 static void g364fb_ctrl_write(void *opaque
,
355 G364State
*s
= opaque
;
357 trace_g364fb_write(addr
, val
);
359 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
361 int idx
= (addr
- REG_CLR_PAL
) >> 3;
362 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
363 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
364 s
->color_palette
[idx
][2] = val
& 0xff;
365 g364fb_invalidate_display(s
);
366 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
368 int idx
= (addr
- REG_CURS_PAT
) >> 3;
369 s
->cursor
[idx
] = val
;
370 g364fb_invalidate_display(s
);
371 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
373 int idx
= (addr
- REG_CURS_PAL
) >> 3;
374 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
375 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
376 s
->cursor_palette
[idx
][2] = val
& 0xff;
377 g364fb_invalidate_display(s
);
380 case REG_BOOT
: /* Boot timing */
381 case 0x00108: /* Line timing: half sync */
382 case 0x00110: /* Line timing: back porch */
383 case 0x00120: /* Line timing: short display */
384 case 0x00128: /* Frame timing: broad pulse */
385 case 0x00130: /* Frame timing: v sync */
386 case 0x00138: /* Frame timing: v preequalise */
387 case 0x00140: /* Frame timing: v postequalise */
388 case 0x00148: /* Frame timing: v blank */
389 case 0x00158: /* Line timing: line time */
390 case 0x00160: /* Frame store: line start */
391 case 0x00168: /* vram cycle: mem init */
392 case 0x00170: /* vram cycle: transfer delay */
393 case 0x00200: /* vram cycle: mask register */
397 s
->top_of_screen
= val
;
398 g364fb_invalidate_display(s
);
408 g364fb_update_depth(s
);
409 g364fb_invalidate_display(s
);
412 g364_invalidate_cursor_position(s
);
413 s
->cursor_position
= val
;
414 g364_invalidate_cursor_position(s
);
420 error_report("g364: invalid write of 0x%" PRIx64
421 " at [" TARGET_FMT_plx
"]", val
, addr
);
425 qemu_irq_lower(s
->irq
);
428 static const MemoryRegionOps g364fb_ctrl_ops
= {
429 .read
= g364fb_ctrl_read
,
430 .write
= g364fb_ctrl_write
,
431 .endianness
= DEVICE_LITTLE_ENDIAN
,
432 .impl
.min_access_size
= 4,
433 .impl
.max_access_size
= 4,
436 static int g364fb_post_load(void *opaque
, int version_id
)
438 G364State
*s
= opaque
;
441 g364fb_update_depth(s
);
442 g364fb_invalidate_display(s
);
447 static const VMStateDescription vmstate_g364fb
= {
450 .minimum_version_id
= 1,
451 .post_load
= g364fb_post_load
,
452 .fields
= (VMStateField
[]) {
453 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, vram_size
),
454 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
455 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
456 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
457 VMSTATE_UINT32(cursor_position
, G364State
),
458 VMSTATE_UINT32(ctla
, G364State
),
459 VMSTATE_UINT32(top_of_screen
, G364State
),
460 VMSTATE_UINT32(width
, G364State
),
461 VMSTATE_UINT32(height
, G364State
),
462 VMSTATE_END_OF_LIST()
466 static const GraphicHwOps g364fb_ops
= {
467 .invalidate
= g364fb_invalidate_display
,
468 .gfx_update
= g364fb_update_display
,
471 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
473 s
->vram
= g_malloc0(s
->vram_size
);
475 s
->con
= graphic_console_init(dev
, 0, &g364fb_ops
, s
);
477 memory_region_init_io(&s
->mem_ctrl
, NULL
, &g364fb_ctrl_ops
, s
, "ctrl", 0x180000);
478 memory_region_init_ram_ptr(&s
->mem_vram
, NULL
, "vram",
479 s
->vram_size
, s
->vram
);
480 vmstate_register_ram(&s
->mem_vram
, dev
);
481 memory_region_set_log(&s
->mem_vram
, true, DIRTY_MEMORY_VGA
);
484 #define TYPE_G364 "sysbus-g364"
485 #define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
488 SysBusDevice parent_obj
;
493 static void g364fb_sysbus_realize(DeviceState
*dev
, Error
**errp
)
495 G364SysBusState
*sbs
= G364(dev
);
496 G364State
*s
= &sbs
->g364
;
497 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
500 sysbus_init_irq(sbd
, &s
->irq
);
501 sysbus_init_mmio(sbd
, &s
->mem_ctrl
);
502 sysbus_init_mmio(sbd
, &s
->mem_vram
);
505 static void g364fb_sysbus_reset(DeviceState
*d
)
507 G364SysBusState
*s
= G364(d
);
509 g364fb_reset(&s
->g364
);
512 static Property g364fb_sysbus_properties
[] = {
513 DEFINE_PROP_UINT32("vram_size", G364SysBusState
, g364
.vram_size
, 8 * MiB
),
514 DEFINE_PROP_END_OF_LIST(),
517 static void g364fb_sysbus_class_init(ObjectClass
*klass
, void *data
)
519 DeviceClass
*dc
= DEVICE_CLASS(klass
);
521 dc
->realize
= g364fb_sysbus_realize
;
522 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
523 dc
->desc
= "G364 framebuffer";
524 dc
->reset
= g364fb_sysbus_reset
;
525 dc
->vmsd
= &vmstate_g364fb
;
526 dc
->props
= g364fb_sysbus_properties
;
529 static const TypeInfo g364fb_sysbus_info
= {
531 .parent
= TYPE_SYS_BUS_DEVICE
,
532 .instance_size
= sizeof(G364SysBusState
),
533 .class_init
= g364fb_sysbus_class_init
,
536 static void g364fb_register_types(void)
538 type_register_static(&g364fb_sysbus_info
);
541 type_init(g364fb_register_types
)