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"
22 #include "qemu/error-report.h"
23 #include "ui/console.h"
24 #include "ui/pixel_ops.h"
26 #include "hw/sysbus.h"
28 typedef struct G364State
{
33 MemoryRegion mem_vram
;
34 MemoryRegion mem_ctrl
;
36 uint8_t color_palette
[256][3];
37 uint8_t cursor_palette
[3][3];
39 uint32_t cursor_position
;
41 uint32_t top_of_screen
;
42 uint32_t width
, height
; /* in pixels */
43 /* display refresh support */
49 #define REG_BOOT 0x000000
50 #define REG_DISPLAY 0x000118
51 #define REG_VDISPLAY 0x000150
52 #define REG_CTLA 0x000300
53 #define REG_TOP 0x000400
54 #define REG_CURS_PAL 0x000508
55 #define REG_CURS_POS 0x000638
56 #define REG_CLR_PAL 0x000800
57 #define REG_CURS_PAT 0x001000
58 #define REG_RESET 0x100000
60 #define CTLA_FORCE_BLANK 0x00000400
61 #define CTLA_NO_CURSOR 0x00800000
63 #define G364_PAGE_SIZE 4096
65 static inline int check_dirty(G364State
*s
, ram_addr_t page
)
67 return memory_region_get_dirty(&s
->mem_vram
, page
, G364_PAGE_SIZE
,
71 static inline void reset_dirty(G364State
*s
,
72 ram_addr_t page_min
, ram_addr_t page_max
)
74 memory_region_reset_dirty(&s
->mem_vram
,
76 page_max
+ G364_PAGE_SIZE
- page_min
- 1,
80 static void g364fb_draw_graphic8(G364State
*s
)
82 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
85 uint8_t *data_display
, *dd
;
86 ram_addr_t page
, page_min
, page_max
;
91 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
93 switch (surface_bits_per_pixel(surface
)) {
95 rgb_to_pixel
= rgb_to_pixel8
;
99 rgb_to_pixel
= rgb_to_pixel15
;
103 rgb_to_pixel
= rgb_to_pixel16
;
107 rgb_to_pixel
= rgb_to_pixel32
;
111 hw_error("g364: unknown host depth %d",
112 surface_bits_per_pixel(surface
));
117 page_min
= (ram_addr_t
)-1;
126 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
127 xcursor
= s
->cursor_position
>> 12;
128 ycursor
= s
->cursor_position
& 0xfff;
130 xcursor
= ycursor
= -65;
133 vram
= s
->vram
+ s
->top_of_screen
;
134 /* XXX: out of range in vram? */
135 data_display
= dd
= surface_data(surface
);
136 while (y
< s
->height
) {
137 if (check_dirty(s
, page
)) {
140 if (page_min
== (ram_addr_t
)-1)
145 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
148 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
149 (x
>= xcursor
&& x
< xcursor
+ 64))) {
151 int xdiff
= x
- xcursor
;
152 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
153 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
154 if (likely(op
== 0)) {
157 color
= (*rgb_to_pixel
)(
158 s
->color_palette
[index
][0],
159 s
->color_palette
[index
][1],
160 s
->color_palette
[index
][2]);
162 /* get cursor color */
164 color
= (*rgb_to_pixel
)(
165 s
->cursor_palette
[index
][0],
166 s
->cursor_palette
[index
][1],
167 s
->cursor_palette
[index
][2]);
172 color
= (*rgb_to_pixel
)(
173 s
->color_palette
[index
][0],
174 s
->color_palette
[index
][1],
175 s
->color_palette
[index
][2]);
177 memcpy(dd
, &color
, w
);
184 if (y
== s
->height
) {
185 ymax
= s
->height
- 1;
188 data_display
= dd
= data_display
+ surface_stride(surface
);
199 if (page_min
!= (ram_addr_t
)-1) {
200 reset_dirty(s
, page_min
, page_max
);
201 page_min
= (ram_addr_t
)-1;
203 dpy_gfx_update(s
->con
, xmin
, ymin
,
204 xmax
- xmin
+ 1, ymax
- ymin
+ 1);
214 vram
+= G364_PAGE_SIZE
;
215 data_display
+= dy
* surface_stride(surface
);
216 dd
= data_display
+ x
* w
;
218 page
+= G364_PAGE_SIZE
;
222 if (page_min
!= (ram_addr_t
)-1) {
223 dpy_gfx_update(s
->con
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
224 reset_dirty(s
, page_min
, page_max
);
228 static void g364fb_draw_blank(G364State
*s
)
230 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
235 /* Screen is already blank. No need to redraw it */
239 w
= s
->width
* surface_bytes_per_pixel(surface
);
240 d
= surface_data(surface
);
241 for (i
= 0; i
< s
->height
; i
++) {
243 d
+= surface_stride(surface
);
246 dpy_gfx_update(s
->con
, 0, 0, s
->width
, s
->height
);
250 static void g364fb_update_display(void *opaque
)
252 G364State
*s
= opaque
;
253 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
255 qemu_flush_coalesced_mmio_buffer();
257 if (s
->width
== 0 || s
->height
== 0)
260 if (s
->width
!= surface_width(surface
) ||
261 s
->height
!= surface_height(surface
)) {
262 qemu_console_resize(s
->con
, s
->width
, s
->height
);
265 memory_region_sync_dirty_bitmap(&s
->mem_vram
);
266 if (s
->ctla
& CTLA_FORCE_BLANK
) {
267 g364fb_draw_blank(s
);
268 } else if (s
->depth
== 8) {
269 g364fb_draw_graphic8(s
);
271 error_report("g364: unknown guest depth %d", s
->depth
);
274 qemu_irq_raise(s
->irq
);
277 static inline void g364fb_invalidate_display(void *opaque
)
279 G364State
*s
= opaque
;
282 memory_region_set_dirty(&s
->mem_vram
, 0, s
->vram_size
);
285 static void g364fb_reset(G364State
*s
)
287 qemu_irq_lower(s
->irq
);
289 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
290 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
291 memset(s
->cursor
, 0, sizeof(s
->cursor
));
292 s
->cursor_position
= 0;
294 s
->top_of_screen
= 0;
295 s
->width
= s
->height
= 0;
296 memset(s
->vram
, 0, s
->vram_size
);
297 g364fb_invalidate_display(s
);
300 /* called for accesses to io ports */
301 static uint64_t g364fb_ctrl_read(void *opaque
,
305 G364State
*s
= opaque
;
308 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
310 int idx
= (addr
- REG_CURS_PAT
) >> 3;
311 val
= s
->cursor
[idx
];
312 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
314 int idx
= (addr
- REG_CURS_PAL
) >> 3;
315 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
316 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
317 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
331 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
339 trace_g364fb_read(addr
, val
);
344 static void g364fb_update_depth(G364State
*s
)
346 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
347 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
350 static void g364_invalidate_cursor_position(G364State
*s
)
352 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
353 int ymin
, ymax
, start
, end
;
355 /* invalidate only near the cursor */
356 ymin
= s
->cursor_position
& 0xfff;
357 ymax
= MIN(s
->height
, ymin
+ 64);
358 start
= ymin
* surface_stride(surface
);
359 end
= (ymax
+ 1) * surface_stride(surface
);
361 memory_region_set_dirty(&s
->mem_vram
, start
, end
- start
);
364 static void g364fb_ctrl_write(void *opaque
,
369 G364State
*s
= opaque
;
371 trace_g364fb_write(addr
, val
);
373 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
375 int idx
= (addr
- REG_CLR_PAL
) >> 3;
376 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
377 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
378 s
->color_palette
[idx
][2] = val
& 0xff;
379 g364fb_invalidate_display(s
);
380 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
382 int idx
= (addr
- REG_CURS_PAT
) >> 3;
383 s
->cursor
[idx
] = val
;
384 g364fb_invalidate_display(s
);
385 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
387 int idx
= (addr
- REG_CURS_PAL
) >> 3;
388 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
389 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
390 s
->cursor_palette
[idx
][2] = val
& 0xff;
391 g364fb_invalidate_display(s
);
394 case REG_BOOT
: /* Boot timing */
395 case 0x00108: /* Line timing: half sync */
396 case 0x00110: /* Line timing: back porch */
397 case 0x00120: /* Line timing: short display */
398 case 0x00128: /* Frame timing: broad pulse */
399 case 0x00130: /* Frame timing: v sync */
400 case 0x00138: /* Frame timing: v preequalise */
401 case 0x00140: /* Frame timing: v postequalise */
402 case 0x00148: /* Frame timing: v blank */
403 case 0x00158: /* Line timing: line time */
404 case 0x00160: /* Frame store: line start */
405 case 0x00168: /* vram cycle: mem init */
406 case 0x00170: /* vram cycle: transfer delay */
407 case 0x00200: /* vram cycle: mask register */
411 s
->top_of_screen
= val
;
412 g364fb_invalidate_display(s
);
422 g364fb_update_depth(s
);
423 g364fb_invalidate_display(s
);
426 g364_invalidate_cursor_position(s
);
427 s
->cursor_position
= val
;
428 g364_invalidate_cursor_position(s
);
434 error_report("g364: invalid write of 0x%" PRIx64
435 " at [" TARGET_FMT_plx
"]", val
, addr
);
439 qemu_irq_lower(s
->irq
);
442 static const MemoryRegionOps g364fb_ctrl_ops
= {
443 .read
= g364fb_ctrl_read
,
444 .write
= g364fb_ctrl_write
,
445 .endianness
= DEVICE_LITTLE_ENDIAN
,
446 .impl
.min_access_size
= 4,
447 .impl
.max_access_size
= 4,
450 static int g364fb_post_load(void *opaque
, int version_id
)
452 G364State
*s
= opaque
;
455 g364fb_update_depth(s
);
456 g364fb_invalidate_display(s
);
461 static const VMStateDescription vmstate_g364fb
= {
464 .minimum_version_id
= 1,
465 .post_load
= g364fb_post_load
,
466 .fields
= (VMStateField
[]) {
467 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, 0, vram_size
),
468 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
469 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
470 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
471 VMSTATE_UINT32(cursor_position
, G364State
),
472 VMSTATE_UINT32(ctla
, G364State
),
473 VMSTATE_UINT32(top_of_screen
, G364State
),
474 VMSTATE_UINT32(width
, G364State
),
475 VMSTATE_UINT32(height
, G364State
),
476 VMSTATE_END_OF_LIST()
480 static const GraphicHwOps g364fb_ops
= {
481 .invalidate
= g364fb_invalidate_display
,
482 .gfx_update
= g364fb_update_display
,
485 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
487 s
->vram
= g_malloc0(s
->vram_size
);
489 s
->con
= graphic_console_init(dev
, 0, &g364fb_ops
, s
);
491 memory_region_init_io(&s
->mem_ctrl
, NULL
, &g364fb_ctrl_ops
, s
, "ctrl", 0x180000);
492 memory_region_init_ram_ptr(&s
->mem_vram
, NULL
, "vram",
493 s
->vram_size
, s
->vram
);
494 vmstate_register_ram(&s
->mem_vram
, dev
);
495 memory_region_set_log(&s
->mem_vram
, true, DIRTY_MEMORY_VGA
);
498 #define TYPE_G364 "sysbus-g364"
499 #define G364(obj) OBJECT_CHECK(G364SysBusState, (obj), TYPE_G364)
502 SysBusDevice parent_obj
;
507 static int g364fb_sysbus_init(SysBusDevice
*sbd
)
509 DeviceState
*dev
= DEVICE(sbd
);
510 G364SysBusState
*sbs
= G364(dev
);
511 G364State
*s
= &sbs
->g364
;
514 sysbus_init_irq(sbd
, &s
->irq
);
515 sysbus_init_mmio(sbd
, &s
->mem_ctrl
);
516 sysbus_init_mmio(sbd
, &s
->mem_vram
);
521 static void g364fb_sysbus_reset(DeviceState
*d
)
523 G364SysBusState
*s
= G364(d
);
525 g364fb_reset(&s
->g364
);
528 static Property g364fb_sysbus_properties
[] = {
529 DEFINE_PROP_UINT32("vram_size", G364SysBusState
, g364
.vram_size
,
531 DEFINE_PROP_END_OF_LIST(),
534 static void g364fb_sysbus_class_init(ObjectClass
*klass
, void *data
)
536 DeviceClass
*dc
= DEVICE_CLASS(klass
);
537 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
539 k
->init
= g364fb_sysbus_init
;
540 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
541 dc
->desc
= "G364 framebuffer";
542 dc
->reset
= g364fb_sysbus_reset
;
543 dc
->vmsd
= &vmstate_g364fb
;
544 dc
->props
= g364fb_sysbus_properties
;
547 static const TypeInfo g364fb_sysbus_info
= {
549 .parent
= TYPE_SYS_BUS_DEVICE
,
550 .instance_size
= sizeof(G364SysBusState
),
551 .class_init
= g364fb_sysbus_class_init
,
554 static void g364fb_register_types(void)
556 type_register_static(&g364fb_sysbus_info
);
559 type_init(g364fb_register_types
)