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 #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
, DIRTY_MEMORY_VGA
);
68 static inline void reset_dirty(G364State
*s
,
69 ram_addr_t page_min
, ram_addr_t page_max
)
71 memory_region_reset_dirty(&s
->mem_vram
,
73 page_max
+ G364_PAGE_SIZE
- page_min
- 1,
77 static void g364fb_draw_graphic8(G364State
*s
)
81 uint8_t *data_display
, *dd
;
82 ram_addr_t page
, page_min
, page_max
;
87 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
89 switch (ds_get_bits_per_pixel(s
->ds
)) {
91 rgb_to_pixel
= rgb_to_pixel8
;
95 rgb_to_pixel
= rgb_to_pixel15
;
99 rgb_to_pixel
= rgb_to_pixel16
;
103 rgb_to_pixel
= rgb_to_pixel32
;
107 hw_error("g364: unknown host depth %d",
108 ds_get_bits_per_pixel(s
->ds
));
113 page_min
= (ram_addr_t
)-1;
122 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
123 xcursor
= s
->cursor_position
>> 12;
124 ycursor
= s
->cursor_position
& 0xfff;
126 xcursor
= ycursor
= -65;
129 vram
= s
->vram
+ s
->top_of_screen
;
130 /* XXX: out of range in vram? */
131 data_display
= dd
= ds_get_data(s
->ds
);
132 while (y
< s
->height
) {
133 if (check_dirty(s
, page
)) {
136 if (page_min
== (ram_addr_t
)-1)
141 for (i
= 0; i
< G364_PAGE_SIZE
; i
++) {
144 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
145 (x
>= xcursor
&& x
< xcursor
+ 64))) {
147 int xdiff
= x
- xcursor
;
148 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
149 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
150 if (likely(op
== 0)) {
153 color
= (*rgb_to_pixel
)(
154 s
->color_palette
[index
][0],
155 s
->color_palette
[index
][1],
156 s
->color_palette
[index
][2]);
158 /* get cursor color */
160 color
= (*rgb_to_pixel
)(
161 s
->cursor_palette
[index
][0],
162 s
->cursor_palette
[index
][1],
163 s
->cursor_palette
[index
][2]);
168 color
= (*rgb_to_pixel
)(
169 s
->color_palette
[index
][0],
170 s
->color_palette
[index
][1],
171 s
->color_palette
[index
][2]);
173 memcpy(dd
, &color
, w
);
180 if (y
== s
->height
) {
181 ymax
= s
->height
- 1;
184 data_display
= dd
= data_display
+ ds_get_linesize(s
->ds
);
195 if (page_min
!= (ram_addr_t
)-1) {
196 reset_dirty(s
, page_min
, page_max
);
197 page_min
= (ram_addr_t
)-1;
199 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
209 vram
+= G364_PAGE_SIZE
;
210 data_display
+= dy
* ds_get_linesize(s
->ds
);
211 dd
= data_display
+ x
* w
;
213 page
+= G364_PAGE_SIZE
;
217 if (page_min
!= (ram_addr_t
)-1) {
218 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
219 reset_dirty(s
, page_min
, page_max
);
223 static void g364fb_draw_blank(G364State
*s
)
229 /* Screen is already blank. No need to redraw it */
233 w
= s
->width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
234 d
= ds_get_data(s
->ds
);
235 for (i
= 0; i
< s
->height
; i
++) {
237 d
+= ds_get_linesize(s
->ds
);
240 dpy_update(s
->ds
, 0, 0, s
->width
, s
->height
);
244 static void g364fb_update_display(void *opaque
)
246 G364State
*s
= opaque
;
248 if (s
->width
== 0 || s
->height
== 0)
251 if (s
->width
!= ds_get_width(s
->ds
) || s
->height
!= ds_get_height(s
->ds
)) {
252 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
255 if (s
->ctla
& CTLA_FORCE_BLANK
) {
256 g364fb_draw_blank(s
);
257 } else if (s
->depth
== 8) {
258 g364fb_draw_graphic8(s
);
260 error_report("g364: unknown guest depth %d", s
->depth
);
263 qemu_irq_raise(s
->irq
);
266 static inline void g364fb_invalidate_display(void *opaque
)
268 G364State
*s
= opaque
;
272 for (i
= 0; i
< s
->vram_size
; i
+= G364_PAGE_SIZE
) {
273 memory_region_set_dirty(&s
->mem_vram
, i
);
277 static void g364fb_reset(G364State
*s
)
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(s
->vram
, 0, s
->vram_size
);
289 g364fb_invalidate_display(s
);
292 static void g364fb_screen_dump(void *opaque
, const char *filename
)
294 G364State
*s
= opaque
;
297 uint8_t *data_buffer
;
301 error_report("g364: unknown guest depth %d", s
->depth
);
305 f
= fopen(filename
, "wb");
309 if (s
->ctla
& CTLA_FORCE_BLANK
) {
311 fprintf(f
, "P4\n%d %d\n",
312 s
->width
, s
->height
);
313 for (y
= 0; y
< s
->height
; y
++)
314 for (x
= 0; x
< s
->width
; x
++)
317 data_buffer
= s
->vram
+ s
->top_of_screen
;
318 fprintf(f
, "P6\n%d %d\n%d\n",
319 s
->width
, s
->height
, 255);
320 for (y
= 0; y
< s
->height
; y
++)
321 for (x
= 0; x
< s
->width
; x
++, data_buffer
++) {
322 index
= *data_buffer
;
323 fputc(s
->color_palette
[index
][0], f
);
324 fputc(s
->color_palette
[index
][1], f
);
325 fputc(s
->color_palette
[index
][2], f
);
332 /* called for accesses to io ports */
333 static uint64_t g364fb_ctrl_read(void *opaque
,
334 target_phys_addr_t addr
,
337 G364State
*s
= opaque
;
340 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
342 int idx
= (addr
- REG_CURS_PAT
) >> 3;
343 val
= s
->cursor
[idx
];
344 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
346 int idx
= (addr
- REG_CURS_PAL
) >> 3;
347 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
348 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
349 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
363 error_report("g364: invalid read at [" TARGET_FMT_plx
"]",
371 trace_g364fb_read(addr
, val
);
376 static void g364fb_update_depth(G364State
*s
)
378 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
379 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
382 static void g364_invalidate_cursor_position(G364State
*s
)
384 int ymin
, ymax
, start
, end
, i
;
386 /* invalidate only near the cursor */
387 ymin
= s
->cursor_position
& 0xfff;
388 ymax
= MIN(s
->height
, ymin
+ 64);
389 start
= ymin
* ds_get_linesize(s
->ds
);
390 end
= (ymax
+ 1) * ds_get_linesize(s
->ds
);
392 for (i
= start
; i
< end
; i
+= G364_PAGE_SIZE
) {
393 memory_region_set_dirty(&s
->mem_vram
, i
);
397 static void g364fb_ctrl_write(void *opaque
,
398 target_phys_addr_t addr
,
402 G364State
*s
= opaque
;
404 trace_g364fb_write(addr
, val
);
406 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
408 int idx
= (addr
- REG_CLR_PAL
) >> 3;
409 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
410 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
411 s
->color_palette
[idx
][2] = val
& 0xff;
412 g364fb_invalidate_display(s
);
413 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
415 int idx
= (addr
- REG_CURS_PAT
) >> 3;
416 s
->cursor
[idx
] = val
;
417 g364fb_invalidate_display(s
);
418 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
420 int idx
= (addr
- REG_CURS_PAL
) >> 3;
421 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
422 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
423 s
->cursor_palette
[idx
][2] = val
& 0xff;
424 g364fb_invalidate_display(s
);
427 case REG_BOOT
: /* Boot timing */
428 case 0x00108: /* Line timing: half sync */
429 case 0x00110: /* Line timing: back porch */
430 case 0x00120: /* Line timing: short display */
431 case 0x00128: /* Frame timing: broad pulse */
432 case 0x00130: /* Frame timing: v sync */
433 case 0x00138: /* Frame timing: v preequalise */
434 case 0x00140: /* Frame timing: v postequalise */
435 case 0x00148: /* Frame timing: v blank */
436 case 0x00158: /* Line timing: line time */
437 case 0x00160: /* Frame store: line start */
438 case 0x00168: /* vram cycle: mem init */
439 case 0x00170: /* vram cycle: transfer delay */
440 case 0x00200: /* vram cycle: mask register */
444 s
->top_of_screen
= val
;
445 g364fb_invalidate_display(s
);
455 g364fb_update_depth(s
);
456 g364fb_invalidate_display(s
);
459 g364_invalidate_cursor_position(s
);
460 s
->cursor_position
= val
;
461 g364_invalidate_cursor_position(s
);
467 error_report("g364: invalid write of 0x%" PRIx64
468 " at [" TARGET_FMT_plx
"]", val
, addr
);
472 qemu_irq_lower(s
->irq
);
475 static const MemoryRegionOps g364fb_ctrl_ops
= {
476 .read
= g364fb_ctrl_read
,
477 .write
= g364fb_ctrl_write
,
478 .endianness
= DEVICE_LITTLE_ENDIAN
,
479 .impl
.min_access_size
= 4,
480 .impl
.max_access_size
= 4,
483 static int g364fb_post_load(void *opaque
, int version_id
)
485 G364State
*s
= opaque
;
488 g364fb_update_depth(s
);
489 g364fb_invalidate_display(s
);
494 static const VMStateDescription vmstate_g364fb
= {
497 .minimum_version_id
= 1,
498 .minimum_version_id_old
= 1,
499 .post_load
= g364fb_post_load
,
500 .fields
= (VMStateField
[]) {
501 VMSTATE_VBUFFER_UINT32(vram
, G364State
, 1, NULL
, 0, vram_size
),
502 VMSTATE_BUFFER_UNSAFE(color_palette
, G364State
, 0, 256 * 3),
503 VMSTATE_BUFFER_UNSAFE(cursor_palette
, G364State
, 0, 9),
504 VMSTATE_UINT16_ARRAY(cursor
, G364State
, 512),
505 VMSTATE_UINT32(cursor_position
, G364State
),
506 VMSTATE_UINT32(ctla
, G364State
),
507 VMSTATE_UINT32(top_of_screen
, G364State
),
508 VMSTATE_UINT32(width
, G364State
),
509 VMSTATE_UINT32(height
, G364State
),
510 VMSTATE_END_OF_LIST()
514 static void g364fb_init(DeviceState
*dev
, G364State
*s
)
516 s
->vram
= g_malloc0(s
->vram_size
);
518 s
->ds
= graphic_console_init(g364fb_update_display
,
519 g364fb_invalidate_display
,
520 g364fb_screen_dump
, NULL
, s
);
522 memory_region_init_io(&s
->mem_ctrl
, &g364fb_ctrl_ops
, s
, "ctrl", 0x180000);
523 memory_region_init_ram_ptr(&s
->mem_vram
, dev
, "vram",
524 s
->vram_size
, s
->vram
);
525 memory_region_set_coalescing(&s
->mem_vram
);
533 static int g364fb_sysbus_init(SysBusDevice
*dev
)
535 G364State
*s
= &FROM_SYSBUS(G364SysBusState
, dev
)->g364
;
537 g364fb_init(&dev
->qdev
, s
);
538 sysbus_init_irq(dev
, &s
->irq
);
539 sysbus_init_mmio_region(dev
, &s
->mem_ctrl
);
540 sysbus_init_mmio_region(dev
, &s
->mem_vram
);
545 static void g364fb_sysbus_reset(DeviceState
*d
)
547 G364SysBusState
*s
= DO_UPCAST(G364SysBusState
, busdev
.qdev
, d
);
548 g364fb_reset(&s
->g364
);
551 static SysBusDeviceInfo g364fb_sysbus_info
= {
552 .init
= g364fb_sysbus_init
,
553 .qdev
.name
= "sysbus-g364",
554 .qdev
.desc
= "G364 framebuffer",
555 .qdev
.size
= sizeof(G364SysBusState
),
556 .qdev
.vmsd
= &vmstate_g364fb
,
557 .qdev
.reset
= g364fb_sysbus_reset
,
558 .qdev
.props
= (Property
[]) {
559 DEFINE_PROP_HEX32("vram_size", G364SysBusState
, g364
.vram_size
,
561 DEFINE_PROP_END_OF_LIST(),
565 static void g364fb_register(void)
567 sysbus_register_withprop(&g364fb_sysbus_info
);
570 device_init(g364fb_register
);