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/>.
22 #include "pixel_ops.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 static inline int check_dirty(G364State
*s
, ram_addr_t page
)
63 return memory_region_get_dirty(&s
->mem_vram
, page
, DIRTY_MEMORY_VGA
);
66 static inline void reset_dirty(G364State
*s
,
67 ram_addr_t page_min
, ram_addr_t page_max
)
69 memory_region_reset_dirty(&s
->mem_vram
,
71 page_max
+ TARGET_PAGE_SIZE
- page_min
- 1,
75 static void g364fb_draw_graphic8(G364State
*s
)
79 uint8_t *data_display
, *dd
;
80 ram_addr_t page
, page_min
, page_max
;
85 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
87 switch (ds_get_bits_per_pixel(s
->ds
)) {
89 rgb_to_pixel
= rgb_to_pixel8
;
93 rgb_to_pixel
= rgb_to_pixel15
;
97 rgb_to_pixel
= rgb_to_pixel16
;
101 rgb_to_pixel
= rgb_to_pixel32
;
105 hw_error("g364: unknown host depth %d",
106 ds_get_bits_per_pixel(s
->ds
));
111 page_min
= (ram_addr_t
)-1;
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
= ds_get_data(s
->ds
);
130 while (y
< s
->height
) {
131 if (check_dirty(s
, page
)) {
134 if (page_min
== (ram_addr_t
)-1)
139 for (i
= 0; i
< TARGET_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
+ ds_get_linesize(s
->ds
);
193 if (page_min
!= (ram_addr_t
)-1) {
194 reset_dirty(s
, page_min
, page_max
);
195 page_min
= (ram_addr_t
)-1;
197 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
203 x
+= TARGET_PAGE_SIZE
;
207 vram
+= TARGET_PAGE_SIZE
;
208 data_display
+= dy
* ds_get_linesize(s
->ds
);
209 dd
= data_display
+ x
* w
;
211 page
+= TARGET_PAGE_SIZE
;
215 if (page_min
!= (ram_addr_t
)-1) {
216 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
217 reset_dirty(s
, page_min
, page_max
);
221 static void g364fb_draw_blank(G364State
*s
)
227 /* Screen is already blank. No need to redraw it */
231 w
= s
->width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
232 d
= ds_get_data(s
->ds
);
233 for (i
= 0; i
< s
->height
; i
++) {
235 d
+= ds_get_linesize(s
->ds
);
238 dpy_update(s
->ds
, 0, 0, s
->width
, s
->height
);
242 static void g364fb_update_display(void *opaque
)
244 G364State
*s
= opaque
;
246 if (s
->width
== 0 || s
->height
== 0)
249 if (s
->width
!= ds_get_width(s
->ds
) || s
->height
!= ds_get_height(s
->ds
)) {
250 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
253 if (s
->ctla
& CTLA_FORCE_BLANK
) {
254 g364fb_draw_blank(s
);
255 } else if (s
->depth
== 8) {
256 g364fb_draw_graphic8(s
);
258 error_report("g364: unknown guest depth %d", s
->depth
);
261 qemu_irq_raise(s
->irq
);
264 static inline void g364fb_invalidate_display(void *opaque
)
266 G364State
*s
= opaque
;
270 for (i
= 0; i
< s
->vram_size
; i
+= TARGET_PAGE_SIZE
) {
271 memory_region_set_dirty(&s
->mem_vram
, i
);
275 static void g364fb_reset(G364State
*s
)
277 qemu_irq_lower(s
->irq
);
279 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
280 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
281 memset(s
->cursor
, 0, sizeof(s
->cursor
));
282 s
->cursor_position
= 0;
284 s
->top_of_screen
= 0;
285 s
->width
= s
->height
= 0;
286 memset(s
->vram
, 0, s
->vram_size
);
287 g364fb_invalidate_display(s
);
290 static void g364fb_screen_dump(void *opaque
, const char *filename
)
292 G364State
*s
= opaque
;
295 uint8_t *data_buffer
;
299 error_report("g364: unknown guest depth %d", s
->depth
);
303 f
= fopen(filename
, "wb");
307 if (s
->ctla
& CTLA_FORCE_BLANK
) {
309 fprintf(f
, "P4\n%d %d\n",
310 s
->width
, s
->height
);
311 for (y
= 0; y
< s
->height
; y
++)
312 for (x
= 0; x
< s
->width
; x
++)
315 data_buffer
= s
->vram
+ s
->top_of_screen
;
316 fprintf(f
, "P6\n%d %d\n%d\n",
317 s
->width
, s
->height
, 255);
318 for (y
= 0; y
< s
->height
; y
++)
319 for (x
= 0; x
< s
->width
; x
++, data_buffer
++) {
320 index
= *data_buffer
;
321 fputc(s
->color_palette
[index
][0], f
);
322 fputc(s
->color_palette
[index
][1], f
);
323 fputc(s
->color_palette
[index
][2], f
);
330 /* called for accesses to io ports */
331 static uint64_t g364fb_ctrl_read(void *opaque
,
332 target_phys_addr_t addr
,
335 G364State
*s
= opaque
;
338 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
340 int idx
= (addr
- REG_CURS_PAT
) >> 3;
341 val
= s
->cursor
[idx
];
342 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
344 int idx
= (addr
- REG_CURS_PAL
) >> 3;
345 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
346 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
347 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
361 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
369 trace_g364fb_read(addr
, val
);
374 static void g364fb_update_depth(G364State
*s
)
376 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
377 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
380 static void g364_invalidate_cursor_position(G364State
*s
)
382 int ymin
, ymax
, start
, end
, i
;
384 /* invalidate only near the cursor */
385 ymin
= s
->cursor_position
& 0xfff;
386 ymax
= MIN(s
->height
, ymin
+ 64);
387 start
= ymin
* ds_get_linesize(s
->ds
);
388 end
= (ymax
+ 1) * ds_get_linesize(s
->ds
);
390 for (i
= start
; i
< end
; i
+= TARGET_PAGE_SIZE
) {
391 memory_region_set_dirty(&s
->mem_vram
, i
);
395 static void g364fb_ctrl_write(void *opaque
,
396 target_phys_addr_t addr
,
400 G364State
*s
= opaque
;
402 trace_g364fb_write(addr
, val
);
404 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
406 int idx
= (addr
- REG_CLR_PAL
) >> 3;
407 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
408 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
409 s
->color_palette
[idx
][2] = val
& 0xff;
410 g364fb_invalidate_display(s
);
411 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
413 int idx
= (addr
- REG_CURS_PAT
) >> 3;
414 s
->cursor
[idx
] = val
;
415 g364fb_invalidate_display(s
);
416 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
418 int idx
= (addr
- REG_CURS_PAL
) >> 3;
419 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
420 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
421 s
->cursor_palette
[idx
][2] = val
& 0xff;
422 g364fb_invalidate_display(s
);
425 case REG_BOOT
: /* Boot timing */
426 case 0x00108: /* Line timing: half sync */
427 case 0x00110: /* Line timing: back porch */
428 case 0x00120: /* Line timing: short display */
429 case 0x00128: /* Frame timing: broad pulse */
430 case 0x00130: /* Frame timing: v sync */
431 case 0x00138: /* Frame timing: v preequalise */
432 case 0x00140: /* Frame timing: v postequalise */
433 case 0x00148: /* Frame timing: v blank */
434 case 0x00158: /* Line timing: line time */
435 case 0x00160: /* Frame store: line start */
436 case 0x00168: /* vram cycle: mem init */
437 case 0x00170: /* vram cycle: transfer delay */
438 case 0x00200: /* vram cycle: mask register */
442 s
->top_of_screen
= val
;
443 g364fb_invalidate_display(s
);
453 g364fb_update_depth(s
);
454 g364fb_invalidate_display(s
);
457 g364_invalidate_cursor_position(s
);
458 s
->cursor_position
= val
;
459 g364_invalidate_cursor_position(s
);
465 error_report("g364: invalid write of 0x%" PRIx64
466 " at [" TARGET_FMT_plx
"]", val
, addr
);
470 qemu_irq_lower(s
->irq
);
473 static const MemoryRegionOps g364fb_ctrl_ops
= {
474 .read
= g364fb_ctrl_read
,
475 .write
= g364fb_ctrl_write
,
476 .endianness
= DEVICE_LITTLE_ENDIAN
,
477 .impl
.min_access_size
= 4,
478 .impl
.max_access_size
= 4,
481 static int g364fb_post_load(void *opaque
, int version_id
)
483 G364State
*s
= opaque
;
486 g364fb_update_depth(s
);
487 g364fb_invalidate_display(s
);
492 static const VMStateDescription vmstate_g364fb
= {
495 .minimum_version_id
= 1,
496 .minimum_version_id_old
= 1,
497 .post_load
= g364fb_post_load
,
498 .fields
= (VMStateField
[]) {
499 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, 0, vram_size
),
500 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
501 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
502 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
503 VMSTATE_UINT32(cursor_position
, G364State
),
504 VMSTATE_UINT32(ctla
, G364State
),
505 VMSTATE_UINT32(top_of_screen
, G364State
),
506 VMSTATE_UINT32(width
, G364State
),
507 VMSTATE_UINT32(height
, G364State
),
508 VMSTATE_END_OF_LIST()
512 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
514 s
->vram
= g_malloc0(s
->vram_size
);
516 s
->ds
= graphic_console_init(g364fb_update_display
,
517 g364fb_invalidate_display
,
518 g364fb_screen_dump
, NULL
, s
);
520 memory_region_init_io(&s
->mem_ctrl
, &g364fb_ctrl_ops
, s
, "ctrl", 0x180000);
521 memory_region_init_ram_ptr(&s
->mem_vram
, dev
, "vram",
522 s
->vram_size
, s
->vram
);
523 memory_region_set_coalescing(&s
->mem_vram
);
531 static int g364fb_sysbus_init(SysBusDevice
*dev
)
533 G364State
*s
= &FROM_SYSBUS(G364SysBusState
, dev
)->g364
;
535 g364fb_init(&dev
->qdev
, s
);
536 sysbus_init_irq(dev
, &s
->irq
);
537 sysbus_init_mmio_region(dev
, &s
->mem_ctrl
);
538 sysbus_init_mmio_region(dev
, &s
->mem_vram
);
543 static void g364fb_sysbus_reset(DeviceState
*d
)
545 G364SysBusState
*s
= DO_UPCAST(G364SysBusState
, busdev
.qdev
, d
);
546 g364fb_reset(&s
->g364
);
549 static SysBusDeviceInfo g364fb_sysbus_info
= {
550 .init
= g364fb_sysbus_init
,
551 .qdev
.name
= "sysbus-g364",
552 .qdev
.desc
= "G364 framebuffer",
553 .qdev
.size
= sizeof(G364SysBusState
),
554 .qdev
.vmsd
= &vmstate_g364fb
,
555 .qdev
.reset
= g364fb_sysbus_reset
,
556 .qdev
.props
= (Property
[]) {
557 DEFINE_PROP_HEX32("vram_size", G364SysBusState
, g364
.vram_size
,
559 DEFINE_PROP_END_OF_LIST(),
563 static void g364fb_register(void)
565 sysbus_register_withprop(&g364fb_sysbus_info
);
568 device_init(g364fb_register
);