2 * QEMU G364 framebuffer Emulator.
4 * Copyright (c) 2007-2009 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/>.
23 #include "pixel_ops.h"
28 #define DPRINTF(fmt, ...) \
29 do { printf("g364: " fmt , ## __VA_ARGS__); } while (0)
31 #define DPRINTF(fmt, ...) do {} while (0)
33 #define BADF(fmt, ...) \
34 do { fprintf(stderr, "g364 ERROR: " fmt , ## __VA_ARGS__);} while (0)
36 typedef struct G364State
{
39 ram_addr_t vram_offset
;
43 uint8_t color_palette
[256][3];
44 uint8_t cursor_palette
[3][3];
46 uint32_t cursor_position
;
48 uint32_t top_of_screen
;
49 uint32_t width
, height
; /* in pixels */
50 /* display refresh support */
56 #define REG_ID 0x000000
57 #define REG_BOOT 0x080000
58 #define REG_DISPLAY 0x080118
59 #define REG_VDISPLAY 0x080150
60 #define REG_CTLA 0x080300
61 #define REG_TOP 0x080400
62 #define REG_CURS_PAL 0x080508
63 #define REG_CURS_POS 0x080638
64 #define REG_CLR_PAL 0x080800
65 #define REG_CURS_PAT 0x081000
66 #define REG_RESET 0x180000
68 #define CTLA_FORCE_BLANK 0x00000400
69 #define CTLA_NO_CURSOR 0x00800000
71 static inline int check_dirty(ram_addr_t page
)
73 return cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
76 static inline void reset_dirty(G364State
*s
,
77 ram_addr_t page_min
, ram_addr_t page_max
)
79 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
- 1,
83 static void g364fb_draw_graphic8(G364State
*s
)
87 uint8_t *data_display
, *dd
;
88 ram_addr_t page
, page_min
, page_max
;
93 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
95 switch (ds_get_bits_per_pixel(s
->ds
)) {
97 rgb_to_pixel
= rgb_to_pixel8
;
101 rgb_to_pixel
= rgb_to_pixel15
;
105 rgb_to_pixel
= rgb_to_pixel16
;
109 rgb_to_pixel
= rgb_to_pixel32
;
113 BADF("unknown host depth %d\n", ds_get_bits_per_pixel(s
->ds
));
117 page
= s
->vram_offset
;
118 page_min
= (ram_addr_t
)-1;
127 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
128 xcursor
= s
->cursor_position
>> 12;
129 ycursor
= s
->cursor_position
& 0xfff;
131 xcursor
= ycursor
= -65;
134 vram
= s
->vram
+ s
->top_of_screen
;
135 /* XXX: out of range in vram? */
136 data_display
= dd
= ds_get_data(s
->ds
);
137 while (y
< s
->height
) {
138 if (check_dirty(page
)) {
141 if (page_min
== (ram_addr_t
)-1)
146 for (i
= 0; i
< TARGET_PAGE_SIZE
; i
++) {
149 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
150 (x
>= xcursor
&& x
< xcursor
+ 64))) {
152 int xdiff
= x
- xcursor
;
153 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
154 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
155 if (likely(op
== 0)) {
158 color
= (*rgb_to_pixel
)(
159 s
->color_palette
[index
][0],
160 s
->color_palette
[index
][1],
161 s
->color_palette
[index
][2]);
163 /* get cursor color */
165 color
= (*rgb_to_pixel
)(
166 s
->cursor_palette
[index
][0],
167 s
->cursor_palette
[index
][1],
168 s
->cursor_palette
[index
][2]);
173 color
= (*rgb_to_pixel
)(
174 s
->color_palette
[index
][0],
175 s
->color_palette
[index
][1],
176 s
->color_palette
[index
][2]);
178 memcpy(dd
, &color
, w
);
185 if (y
== s
->height
) {
186 ymax
= s
->height
- 1;
189 data_display
= dd
= data_display
+ ds_get_linesize(s
->ds
);
200 if (page_min
!= (ram_addr_t
)-1) {
201 reset_dirty(s
, page_min
, page_max
);
202 page_min
= (ram_addr_t
)-1;
204 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
210 x
+= TARGET_PAGE_SIZE
;
214 vram
+= TARGET_PAGE_SIZE
;
215 data_display
+= dy
* ds_get_linesize(s
->ds
);
216 dd
= data_display
+ x
* w
;
218 page
+= TARGET_PAGE_SIZE
;
222 if (page_min
!= (ram_addr_t
)-1) {
223 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
224 reset_dirty(s
, page_min
, page_max
);
228 static void g364fb_draw_blank(G364State
*s
)
234 /* Screen is already blank. No need to redraw it */
238 w
= s
->width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
239 d
= ds_get_data(s
->ds
);
240 for (i
= 0; i
< s
->height
; i
++) {
242 d
+= ds_get_linesize(s
->ds
);
245 dpy_update(s
->ds
, 0, 0, s
->width
, s
->height
);
249 static void g364fb_update_display(void *opaque
)
251 G364State
*s
= opaque
;
253 if (s
->width
== 0 || s
->height
== 0)
256 if (s
->width
!= ds_get_width(s
->ds
) || s
->height
!= ds_get_height(s
->ds
)) {
257 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
260 if (s
->ctla
& CTLA_FORCE_BLANK
) {
261 g364fb_draw_blank(s
);
262 } else if (s
->depth
== 8) {
263 g364fb_draw_graphic8(s
);
265 BADF("unknown guest depth %d\n", s
->depth
);
268 qemu_irq_raise(s
->irq
);
271 static inline void g364fb_invalidate_display(void *opaque
)
273 G364State
*s
= opaque
;
277 for (i
= 0; i
< s
->vram_size
; i
+= TARGET_PAGE_SIZE
) {
278 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
282 static void g364fb_reset(void *opaque
)
284 G364State
*s
= opaque
;
285 qemu_irq_lower(s
->irq
);
287 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
288 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
289 memset(s
->cursor
, 0, sizeof(s
->cursor
));
290 s
->cursor_position
= 0;
292 s
->top_of_screen
= 0;
293 s
->width
= s
->height
= 0;
294 memset(s
->vram
, 0, s
->vram_size
);
295 g364fb_invalidate_display(opaque
);
298 static void g364fb_screen_dump(void *opaque
, const char *filename
)
300 G364State
*s
= opaque
;
303 uint8_t *data_buffer
;
307 BADF("unknown guest depth %d\n", s
->depth
);
311 f
= fopen(filename
, "wb");
315 if (s
->ctla
& CTLA_FORCE_BLANK
) {
317 fprintf(f
, "P4\n%d %d\n",
318 s
->width
, s
->height
);
319 for (y
= 0; y
< s
->height
; y
++)
320 for (x
= 0; x
< s
->width
; x
++)
323 data_buffer
= s
->vram
+ s
->top_of_screen
;
324 fprintf(f
, "P6\n%d %d\n%d\n",
325 s
->width
, s
->height
, 255);
326 for (y
= 0; y
< s
->height
; y
++)
327 for (x
= 0; x
< s
->width
; x
++, data_buffer
++) {
328 index
= *data_buffer
;
329 fputc(s
->color_palette
[index
][0], f
);
330 fputc(s
->color_palette
[index
][1], f
);
331 fputc(s
->color_palette
[index
][2], f
);
338 /* called for accesses to io ports */
339 static uint32_t g364fb_ctrl_readl(void *opaque
, target_phys_addr_t addr
)
341 G364State
*s
= opaque
;
344 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
346 int idx
= (addr
- REG_CURS_PAT
) >> 3;
347 val
= s
->cursor
[idx
];
348 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
350 int idx
= (addr
- REG_CURS_PAL
) >> 3;
351 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
352 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
353 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
357 val
= 0x10; /* Mips G364 */
370 BADF("invalid read at [" TARGET_FMT_plx
"]\n", addr
);
377 DPRINTF("read 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
382 static uint32_t g364fb_ctrl_readw(void *opaque
, target_phys_addr_t addr
)
384 uint32_t v
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
391 static uint32_t g364fb_ctrl_readb(void *opaque
, target_phys_addr_t addr
)
393 uint32_t v
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
394 return (v
>> (8 * (addr
& 0x3))) & 0xff;
397 static void g364fb_update_depth(G364State
*s
)
399 static const int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
400 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
403 static void g364_invalidate_cursor_position(G364State
*s
)
405 int ymin
, ymax
, start
, end
, i
;
407 /* invalidate only near the cursor */
408 ymin
= s
->cursor_position
& 0xfff;
409 ymax
= MIN(s
->height
, ymin
+ 64);
410 start
= ymin
* ds_get_linesize(s
->ds
);
411 end
= (ymax
+ 1) * ds_get_linesize(s
->ds
);
413 for (i
= start
; i
< end
; i
+= TARGET_PAGE_SIZE
) {
414 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
418 static void g364fb_ctrl_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
420 G364State
*s
= opaque
;
422 DPRINTF("write 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
424 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
426 int idx
= (addr
- REG_CLR_PAL
) >> 3;
427 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
428 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
429 s
->color_palette
[idx
][2] = val
& 0xff;
430 g364fb_invalidate_display(s
);
431 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
433 int idx
= (addr
- REG_CURS_PAT
) >> 3;
434 s
->cursor
[idx
] = val
;
435 g364fb_invalidate_display(s
);
436 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
438 int idx
= (addr
- REG_CURS_PAL
) >> 3;
439 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
440 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
441 s
->cursor_palette
[idx
][2] = val
& 0xff;
442 g364fb_invalidate_display(s
);
445 case REG_ID
: /* Card identifier; read-only */
446 case REG_BOOT
: /* Boot timing */
447 case 0x80108: /* Line timing: half sync */
448 case 0x80110: /* Line timing: back porch */
449 case 0x80120: /* Line timing: short display */
450 case 0x80128: /* Frame timing: broad pulse */
451 case 0x80130: /* Frame timing: v sync */
452 case 0x80138: /* Frame timing: v preequalise */
453 case 0x80140: /* Frame timing: v postequalise */
454 case 0x80148: /* Frame timing: v blank */
455 case 0x80158: /* Line timing: line time */
456 case 0x80160: /* Frame store: line start */
457 case 0x80168: /* vram cycle: mem init */
458 case 0x80170: /* vram cycle: transfer delay */
459 case 0x80200: /* vram cycle: mask register */
463 s
->top_of_screen
= val
;
464 g364fb_invalidate_display(s
);
474 g364fb_update_depth(s
);
475 g364fb_invalidate_display(s
);
478 g364_invalidate_cursor_position(s
);
479 s
->cursor_position
= val
;
480 g364_invalidate_cursor_position(s
);
486 BADF("invalid write of 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
490 qemu_irq_lower(s
->irq
);
493 static void g364fb_ctrl_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
495 uint32_t old_val
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
498 val
= (val
<< 16) | (old_val
& 0x0000ffff);
500 val
= val
| (old_val
& 0xffff0000);
501 g364fb_ctrl_writel(opaque
, addr
& ~0x3, val
);
504 static void g364fb_ctrl_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
506 uint32_t old_val
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
510 val
= val
| (old_val
& 0xffffff00);
513 val
= (val
<< 8) | (old_val
& 0xffff00ff);
516 val
= (val
<< 16) | (old_val
& 0xff00ffff);
519 val
= (val
<< 24) | (old_val
& 0x00ffffff);
522 g364fb_ctrl_writel(opaque
, addr
& ~0x3, val
);
525 static CPUReadMemoryFunc
* const g364fb_ctrl_read
[3] = {
531 static CPUWriteMemoryFunc
* const g364fb_ctrl_write
[3] = {
537 static int g364fb_load(QEMUFile
*f
, void *opaque
, int version_id
)
539 G364State
*s
= opaque
;
540 unsigned int i
, vram_size
;
545 vram_size
= qemu_get_be32(f
);
546 if (vram_size
< s
->vram_size
)
548 qemu_get_buffer(f
, s
->vram
, s
->vram_size
);
549 for (i
= 0; i
< 256; i
++)
550 qemu_get_buffer(f
, s
->color_palette
[i
], 3);
551 for (i
= 0; i
< 3; i
++)
552 qemu_get_buffer(f
, s
->cursor_palette
[i
], 3);
553 qemu_get_buffer(f
, (uint8_t *)s
->cursor
, sizeof(s
->cursor
));
554 s
->cursor_position
= qemu_get_be32(f
);
555 s
->ctla
= qemu_get_be32(f
);
556 s
->top_of_screen
= qemu_get_be32(f
);
557 s
->width
= qemu_get_be32(f
);
558 s
->height
= qemu_get_be32(f
);
561 g364fb_update_depth(s
);
562 g364fb_invalidate_display(s
);
567 static void g364fb_save(QEMUFile
*f
, void *opaque
)
569 G364State
*s
= opaque
;
572 qemu_put_be32(f
, s
->vram_size
);
573 qemu_put_buffer(f
, s
->vram
, s
->vram_size
);
574 for (i
= 0; i
< 256; i
++)
575 qemu_put_buffer(f
, s
->color_palette
[i
], 3);
576 for (i
= 0; i
< 3; i
++)
577 qemu_put_buffer(f
, s
->cursor_palette
[i
], 3);
578 qemu_put_buffer(f
, (uint8_t *)s
->cursor
, sizeof(s
->cursor
));
579 qemu_put_be32(f
, s
->cursor_position
);
580 qemu_put_be32(f
, s
->ctla
);
581 qemu_put_be32(f
, s
->top_of_screen
);
582 qemu_put_be32(f
, s
->width
);
583 qemu_put_be32(f
, s
->height
);
586 int g364fb_mm_init(target_phys_addr_t vram_base
,
587 target_phys_addr_t ctrl_base
, int it_shift
,
593 s
= g_malloc0(sizeof(G364State
));
595 s
->vram_size
= 8 * 1024 * 1024;
596 s
->vram_offset
= qemu_ram_alloc(NULL
, "g364fb.vram", s
->vram_size
);
597 s
->vram
= qemu_get_ram_ptr(s
->vram_offset
);
600 qemu_register_reset(g364fb_reset
, s
);
601 register_savevm(NULL
, "g364fb", 0, 1, g364fb_save
, g364fb_load
, s
);
604 s
->ds
= graphic_console_init(g364fb_update_display
,
605 g364fb_invalidate_display
,
606 g364fb_screen_dump
, NULL
, s
);
608 cpu_register_physical_memory(vram_base
, s
->vram_size
, s
->vram_offset
);
610 io_ctrl
= cpu_register_io_memory(g364fb_ctrl_read
, g364fb_ctrl_write
, s
,
611 DEVICE_NATIVE_ENDIAN
);
612 cpu_register_physical_memory(ctrl_base
, 0x200000, io_ctrl
);