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/>.
21 #include "ui/console.h"
22 #include "ui/pixel_ops.h"
24 #include "hw/sysbus.h"
26 typedef struct G364State
{
31 MemoryRegion mem_vram
;
32 MemoryRegion mem_ctrl
;
34 uint8_t color_palette
[256][3];
35 uint8_t cursor_palette
[3][3];
37 uint32_t cursor_position
;
39 uint32_t top_of_screen
;
40 uint32_t width
, height
; /* in pixels */
41 /* display refresh support */
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 #define G364_PAGE_SIZE 4096
63 static inline int check_dirty(G364State
*s
, ram_addr_t page
)
65 return memory_region_get_dirty(&s
->mem_vram
, page
, G364_PAGE_SIZE
,
69 static inline void reset_dirty(G364State
*s
,
70 ram_addr_t page_min
, ram_addr_t page_max
)
72 memory_region_reset_dirty(&s
->mem_vram
,
74 page_max
+ G364_PAGE_SIZE
- page_min
- 1,
78 static void g364fb_draw_graphic8(G364State
*s
)
80 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
83 uint8_t *data_display
, *dd
;
84 ram_addr_t page
, page_min
, page_max
;
89 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
91 switch (surface_bits_per_pixel(surface
)) {
93 rgb_to_pixel
= rgb_to_pixel8
;
97 rgb_to_pixel
= rgb_to_pixel15
;
101 rgb_to_pixel
= rgb_to_pixel16
;
105 rgb_to_pixel
= rgb_to_pixel32
;
109 hw_error("g364: unknown host depth %d",
110 surface_bits_per_pixel(surface
));
115 page_min
= (ram_addr_t
)-1;
124 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
125 xcursor
= s
->cursor_position
>> 12;
126 ycursor
= s
->cursor_position
& 0xfff;
128 xcursor
= ycursor
= -65;
131 vram
= s
->vram
+ s
->top_of_screen
;
132 /* XXX: out of range in vram? */
133 data_display
= dd
= surface_data(surface
);
134 while (y
< s
->height
) {
135 if (check_dirty(s
, page
)) {
138 if (page_min
== (ram_addr_t
)-1)
143 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
146 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
147 (x
>= xcursor
&& x
< xcursor
+ 64))) {
149 int xdiff
= x
- xcursor
;
150 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
151 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
152 if (likely(op
== 0)) {
155 color
= (*rgb_to_pixel
)(
156 s
->color_palette
[index
][0],
157 s
->color_palette
[index
][1],
158 s
->color_palette
[index
][2]);
160 /* get cursor color */
162 color
= (*rgb_to_pixel
)(
163 s
->cursor_palette
[index
][0],
164 s
->cursor_palette
[index
][1],
165 s
->cursor_palette
[index
][2]);
170 color
= (*rgb_to_pixel
)(
171 s
->color_palette
[index
][0],
172 s
->color_palette
[index
][1],
173 s
->color_palette
[index
][2]);
175 memcpy(dd
, &color
, w
);
182 if (y
== s
->height
) {
183 ymax
= s
->height
- 1;
186 data_display
= dd
= data_display
+ surface_stride(surface
);
197 if (page_min
!= (ram_addr_t
)-1) {
198 reset_dirty(s
, page_min
, page_max
);
199 page_min
= (ram_addr_t
)-1;
201 dpy_gfx_update(s
->con
, xmin
, ymin
,
202 xmax
- xmin
+ 1, ymax
- ymin
+ 1);
212 vram
+= G364_PAGE_SIZE
;
213 data_display
+= dy
* surface_stride(surface
);
214 dd
= data_display
+ x
* w
;
216 page
+= G364_PAGE_SIZE
;
220 if (page_min
!= (ram_addr_t
)-1) {
221 dpy_gfx_update(s
->con
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
222 reset_dirty(s
, page_min
, page_max
);
226 static void g364fb_draw_blank(G364State
*s
)
228 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
233 /* Screen is already blank. No need to redraw it */
237 w
= s
->width
* surface_bytes_per_pixel(surface
);
238 d
= surface_data(surface
);
239 for (i
= 0; i
< s
->height
; i
++) {
241 d
+= surface_stride(surface
);
244 dpy_gfx_update(s
->con
, 0, 0, s
->width
, s
->height
);
248 static void g364fb_update_display(void *opaque
)
250 G364State
*s
= opaque
;
251 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
253 qemu_flush_coalesced_mmio_buffer();
255 if (s
->width
== 0 || s
->height
== 0)
258 if (s
->width
!= surface_width(surface
) ||
259 s
->height
!= surface_height(surface
)) {
260 qemu_console_resize(s
->con
, s
->width
, s
->height
);
263 if (s
->ctla
& CTLA_FORCE_BLANK
) {
264 g364fb_draw_blank(s
);
265 } else if (s
->depth
== 8) {
266 g364fb_draw_graphic8(s
);
268 error_report("g364: unknown guest depth %d", s
->depth
);
271 qemu_irq_raise(s
->irq
);
274 static inline void g364fb_invalidate_display(void *opaque
)
276 G364State
*s
= opaque
;
279 memory_region_set_dirty(&s
->mem_vram
, 0, s
->vram_size
);
282 static void g364fb_reset(G364State
*s
)
284 qemu_irq_lower(s
->irq
);
286 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
287 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
288 memset(s
->cursor
, 0, sizeof(s
->cursor
));
289 s
->cursor_position
= 0;
291 s
->top_of_screen
= 0;
292 s
->width
= s
->height
= 0;
293 memset(s
->vram
, 0, s
->vram_size
);
294 g364fb_invalidate_display(s
);
297 /* called for accesses to io ports */
298 static uint64_t g364fb_ctrl_read(void *opaque
,
302 G364State
*s
= opaque
;
305 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
307 int idx
= (addr
- REG_CURS_PAT
) >> 3;
308 val
= s
->cursor
[idx
];
309 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
311 int idx
= (addr
- REG_CURS_PAL
) >> 3;
312 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
313 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
314 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
328 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
336 trace_g364fb_read(addr
, val
);
341 static void g364fb_update_depth(G364State
*s
)
343 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
344 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
347 static void g364_invalidate_cursor_position(G364State
*s
)
349 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
350 int ymin
, ymax
, start
, end
;
352 /* invalidate only near the cursor */
353 ymin
= s
->cursor_position
& 0xfff;
354 ymax
= MIN(s
->height
, ymin
+ 64);
355 start
= ymin
* surface_stride(surface
);
356 end
= (ymax
+ 1) * surface_stride(surface
);
358 memory_region_set_dirty(&s
->mem_vram
, start
, end
- start
);
361 static void g364fb_ctrl_write(void *opaque
,
366 G364State
*s
= opaque
;
368 trace_g364fb_write(addr
, val
);
370 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
372 int idx
= (addr
- REG_CLR_PAL
) >> 3;
373 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
374 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
375 s
->color_palette
[idx
][2] = val
& 0xff;
376 g364fb_invalidate_display(s
);
377 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
379 int idx
= (addr
- REG_CURS_PAT
) >> 3;
380 s
->cursor
[idx
] = val
;
381 g364fb_invalidate_display(s
);
382 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
384 int idx
= (addr
- REG_CURS_PAL
) >> 3;
385 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
386 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
387 s
->cursor_palette
[idx
][2] = val
& 0xff;
388 g364fb_invalidate_display(s
);
391 case REG_BOOT
: /* Boot timing */
392 case 0x00108: /* Line timing: half sync */
393 case 0x00110: /* Line timing: back porch */
394 case 0x00120: /* Line timing: short display */
395 case 0x00128: /* Frame timing: broad pulse */
396 case 0x00130: /* Frame timing: v sync */
397 case 0x00138: /* Frame timing: v preequalise */
398 case 0x00140: /* Frame timing: v postequalise */
399 case 0x00148: /* Frame timing: v blank */
400 case 0x00158: /* Line timing: line time */
401 case 0x00160: /* Frame store: line start */
402 case 0x00168: /* vram cycle: mem init */
403 case 0x00170: /* vram cycle: transfer delay */
404 case 0x00200: /* vram cycle: mask register */
408 s
->top_of_screen
= val
;
409 g364fb_invalidate_display(s
);
419 g364fb_update_depth(s
);
420 g364fb_invalidate_display(s
);
423 g364_invalidate_cursor_position(s
);
424 s
->cursor_position
= val
;
425 g364_invalidate_cursor_position(s
);
431 error_report("g364: invalid write of 0x%" PRIx64
432 " at [" TARGET_FMT_plx
"]", val
, addr
);
436 qemu_irq_lower(s
->irq
);
439 static const MemoryRegionOps g364fb_ctrl_ops
= {
440 .read
= g364fb_ctrl_read
,
441 .write
= g364fb_ctrl_write
,
442 .endianness
= DEVICE_LITTLE_ENDIAN
,
443 .impl
.min_access_size
= 4,
444 .impl
.max_access_size
= 4,
447 static int g364fb_post_load(void *opaque
, int version_id
)
449 G364State
*s
= opaque
;
452 g364fb_update_depth(s
);
453 g364fb_invalidate_display(s
);
458 static const VMStateDescription vmstate_g364fb
= {
461 .minimum_version_id
= 1,
462 .post_load
= g364fb_post_load
,
463 .fields
= (VMStateField
[]) {
464 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, 0, vram_size
),
465 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
466 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
467 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
468 VMSTATE_UINT32(cursor_position
, G364State
),
469 VMSTATE_UINT32(ctla
, G364State
),
470 VMSTATE_UINT32(top_of_screen
, G364State
),
471 VMSTATE_UINT32(width
, G364State
),
472 VMSTATE_UINT32(height
, G364State
),
473 VMSTATE_END_OF_LIST()
477 static const GraphicHwOps g364fb_ops
= {
478 .invalidate
= g364fb_invalidate_display
,
479 .gfx_update
= g364fb_update_display
,
482 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
484 s
->vram
= g_malloc0(s
->vram_size
);
486 s
->con
= graphic_console_init(dev
, 0, &g364fb_ops
, s
);
488 memory_region_init_io(&s
->mem_ctrl
, NULL
, &g364fb_ctrl_ops
, s
, "ctrl", 0x180000);
489 memory_region_init_ram_ptr(&s
->mem_vram
, NULL
, "vram",
490 s
->vram_size
, s
->vram
);
491 vmstate_register_ram(&s
->mem_vram
, dev
);
492 memory_region_set_coalescing(&s
->mem_vram
);
495 #define TYPE_G364 "sysbus-g364"
496 #define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
499 SysBusDevice parent_obj
;
504 static int g364fb_sysbus_init(SysBusDevice
*sbd
)
506 DeviceState
*dev
= DEVICE(sbd
);
507 G364SysBusState
*sbs
= G364(dev
);
508 G364State
*s
= &sbs
->g364
;
511 sysbus_init_irq(sbd
, &s
->irq
);
512 sysbus_init_mmio(sbd
, &s
->mem_ctrl
);
513 sysbus_init_mmio(sbd
, &s
->mem_vram
);
518 static void g364fb_sysbus_reset(DeviceState
*d
)
520 G364SysBusState
*s
= G364(d
);
522 g364fb_reset(&s
->g364
);
525 static Property g364fb_sysbus_properties
[] = {
526 DEFINE_PROP_UINT32("vram_size", G364SysBusState
, g364
.vram_size
,
528 DEFINE_PROP_END_OF_LIST(),
531 static void g364fb_sysbus_class_init(ObjectClass
*klass
, void *data
)
533 DeviceClass
*dc
= DEVICE_CLASS(klass
);
534 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
536 k
->init
= g364fb_sysbus_init
;
537 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
538 dc
->desc
= "G364 framebuffer";
539 dc
->reset
= g364fb_sysbus_reset
;
540 dc
->vmsd
= &vmstate_g364fb
;
541 dc
->props
= g364fb_sysbus_properties
;
544 static const TypeInfo g364fb_sysbus_info
= {
546 .parent
= TYPE_SYS_BUS_DEVICE
,
547 .instance_size
= sizeof(G364SysBusState
),
548 .class_init
= g364fb_sysbus_class_init
,
551 static void g364fb_register_types(void)
553 type_register_static(&g364fb_sysbus_info
);
556 type_init(g364fb_register_types
)