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, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "pixel_ops.h"
29 #define DPRINTF(fmt, args...) \
30 do { printf("g364: " fmt , ##args); } while (0)
32 #define DPRINTF(fmt, args...) do {} while (0)
34 #define BADF(fmt, args...) \
35 do { fprintf(stderr, "g364 ERROR: " fmt , ##args);} while (0)
37 typedef struct G364State
{
40 ram_addr_t vram_offset
;
44 uint8_t color_palette
[256][3];
45 uint8_t cursor_palette
[3][3];
47 uint32_t cursor_position
;
49 uint32_t top_of_screen
;
50 uint32_t width
, height
; /* in pixels */
51 /* display refresh support */
57 #define REG_ID 0x000000
58 #define REG_BOOT 0x080000
59 #define REG_DISPLAY 0x080118
60 #define REG_VDISPLAY 0x080150
61 #define REG_CTLA 0x080300
62 #define REG_TOP 0x080400
63 #define REG_CURS_PAL 0x080508
64 #define REG_CURS_POS 0x080638
65 #define REG_CLR_PAL 0x080800
66 #define REG_CURS_PAT 0x081000
67 #define REG_RESET 0x180000
69 #define CTLA_FORCE_BLANK 0x00000400
70 #define CTLA_NO_CURSOR 0x00800000
72 static inline int check_dirty(ram_addr_t page
)
74 return cpu_physical_memory_get_dirty(page
, VGA_DIRTY_FLAG
);
77 static inline void reset_dirty(G364State
*s
,
78 ram_addr_t page_min
, ram_addr_t page_max
)
80 cpu_physical_memory_reset_dirty(page_min
, page_max
+ TARGET_PAGE_SIZE
- 1,
84 static void g364fb_draw_graphic8(G364State
*s
)
88 uint8_t *data_display
, *dd
;
89 ram_addr_t page
, page_min
, page_max
;
94 unsigned int (*rgb_to_pixel
)(unsigned int r
, unsigned int g
, unsigned int b
);
96 switch (ds_get_bits_per_pixel(s
->ds
)) {
98 rgb_to_pixel
= rgb_to_pixel8
;
102 rgb_to_pixel
= rgb_to_pixel15
;
106 rgb_to_pixel
= rgb_to_pixel16
;
110 rgb_to_pixel
= rgb_to_pixel32
;
114 BADF("unknown host depth %d\n", ds_get_bits_per_pixel(s
->ds
));
118 page
= s
->vram_offset
;
119 page_min
= (ram_addr_t
)-1;
128 if (!(s
->ctla
& CTLA_NO_CURSOR
)) {
129 xcursor
= s
->cursor_position
>> 12;
130 ycursor
= s
->cursor_position
& 0xfff;
132 xcursor
= ycursor
= -65;
135 vram
= s
->vram
+ s
->top_of_screen
;
136 /* XXX: out of range in vram? */
137 data_display
= dd
= ds_get_data(s
->ds
);
138 while (y
< s
->height
) {
139 if (check_dirty(page
)) {
142 if (page_min
== (ram_addr_t
)-1)
147 for (i
= 0; i
< TARGET_PAGE_SIZE
; i
++) {
150 if (unlikely((y
>= ycursor
&& y
< ycursor
+ 64) &&
151 (x
>= xcursor
&& x
< xcursor
+ 64))) {
153 int xdiff
= x
- xcursor
;
154 uint16_t curs
= s
->cursor
[(y
- ycursor
) * 8 + xdiff
/ 8];
155 int op
= (curs
>> ((xdiff
& 7) * 2)) & 3;
156 if (likely(op
== 0)) {
159 color
= (*rgb_to_pixel
)(
160 s
->color_palette
[index
][0],
161 s
->color_palette
[index
][1],
162 s
->color_palette
[index
][2]);
164 /* get cursor color */
166 color
= (*rgb_to_pixel
)(
167 s
->cursor_palette
[index
][0],
168 s
->cursor_palette
[index
][1],
169 s
->cursor_palette
[index
][2]);
174 color
= (*rgb_to_pixel
)(
175 s
->color_palette
[index
][0],
176 s
->color_palette
[index
][1],
177 s
->color_palette
[index
][2]);
179 memcpy(dd
, &color
, w
);
186 if (y
== s
->height
) {
187 ymax
= s
->height
- 1;
190 data_display
= dd
= data_display
+ ds_get_linesize(s
->ds
);
201 if (page_min
!= (ram_addr_t
)-1) {
202 reset_dirty(s
, page_min
, page_max
);
203 page_min
= (ram_addr_t
)-1;
205 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
211 x
+= TARGET_PAGE_SIZE
;
215 vram
+= TARGET_PAGE_SIZE
;
216 data_display
+= dy
* ds_get_linesize(s
->ds
);
217 dd
= data_display
+ x
* w
;
219 page
+= TARGET_PAGE_SIZE
;
223 if (page_min
!= (ram_addr_t
)-1) {
224 dpy_update(s
->ds
, xmin
, ymin
, xmax
- xmin
+ 1, ymax
- ymin
+ 1);
225 reset_dirty(s
, page_min
, page_max
);
229 static void g364fb_draw_blank(G364State
*s
)
235 /* Screen is already blank. No need to redraw it */
239 w
= s
->width
* ((ds_get_bits_per_pixel(s
->ds
) + 7) >> 3);
240 d
= ds_get_data(s
->ds
);
241 for (i
= 0; i
< s
->height
; i
++) {
243 d
+= ds_get_linesize(s
->ds
);
246 dpy_update(s
->ds
, 0, 0, s
->width
, s
->height
);
250 static void g364fb_update_display(void *opaque
)
252 G364State
*s
= opaque
;
254 if (s
->width
== 0 || s
->height
== 0)
257 if (s
->width
!= ds_get_width(s
->ds
) || s
->height
!= ds_get_height(s
->ds
)) {
258 qemu_console_resize(s
->ds
, s
->width
, s
->height
);
261 if (s
->ctla
& CTLA_FORCE_BLANK
) {
262 g364fb_draw_blank(s
);
263 } else if (s
->depth
== 8) {
264 g364fb_draw_graphic8(s
);
266 BADF("unknown guest depth %d\n", s
->depth
);
269 qemu_irq_raise(s
->irq
);
272 static void inline g364fb_invalidate_display(void *opaque
)
274 G364State
*s
= opaque
;
278 for (i
= 0; i
< s
->vram_size
; i
+= TARGET_PAGE_SIZE
) {
279 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
283 static void g364fb_reset(void *opaque
)
285 G364State
*s
= opaque
;
286 qemu_irq_lower(s
->irq
);
288 memset(s
->color_palette
, 0, sizeof(s
->color_palette
));
289 memset(s
->cursor_palette
, 0, sizeof(s
->cursor_palette
));
290 memset(s
->cursor
, 0, sizeof(s
->cursor
));
291 s
->cursor_position
= 0;
293 s
->top_of_screen
= 0;
294 s
->width
= s
->height
= 0;
295 memset(s
->vram
, 0, s
->vram_size
);
296 g364fb_invalidate_display(opaque
);
299 static void g364fb_screen_dump(void *opaque
, const char *filename
)
301 G364State
*s
= opaque
;
304 uint8_t *data_buffer
;
308 BADF("unknown guest depth %d\n", s
->depth
);
312 f
= fopen(filename
, "wb");
316 if (s
->ctla
& CTLA_FORCE_BLANK
) {
318 fprintf(f
, "P4\n%d %d\n",
319 s
->width
, s
->height
);
320 for (y
= 0; y
< s
->height
; y
++)
321 for (x
= 0; x
< s
->width
; x
++)
324 data_buffer
= s
->vram
+ s
->top_of_screen
;
325 fprintf(f
, "P6\n%d %d\n%d\n",
326 s
->width
, s
->height
, 255);
327 for (y
= 0; y
< s
->height
; y
++)
328 for (x
= 0; x
< s
->width
; x
++, data_buffer
++) {
329 index
= *data_buffer
;
330 fputc(s
->color_palette
[index
][0], f
);
331 fputc(s
->color_palette
[index
][1], f
);
332 fputc(s
->color_palette
[index
][2], f
);
339 /* called for accesses to io ports */
340 static uint32_t g364fb_ctrl_readl(void *opaque
, target_phys_addr_t addr
)
342 G364State
*s
= opaque
;
345 if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
347 int idx
= (addr
- REG_CURS_PAT
) >> 3;
348 val
= s
->cursor
[idx
];
349 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
351 int idx
= (addr
- REG_CURS_PAL
) >> 3;
352 val
= ((uint32_t)s
->cursor_palette
[idx
][0] << 16);
353 val
|= ((uint32_t)s
->cursor_palette
[idx
][1] << 8);
354 val
|= ((uint32_t)s
->cursor_palette
[idx
][2] << 0);
358 val
= 0x10; /* Mips G364 */
371 BADF("invalid read at [" TARGET_FMT_plx
"]\n", addr
);
378 DPRINTF("read 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
383 static uint32_t g364fb_ctrl_readw(void *opaque
, target_phys_addr_t addr
)
385 uint32_t v
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
392 static uint32_t g364fb_ctrl_readb(void *opaque
, target_phys_addr_t addr
)
394 uint32_t v
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
395 return (v
>> (8 * (addr
& 0x3))) & 0xff;
398 static void g364fb_update_depth(G364State
*s
)
400 const static int depths
[8] = { 1, 2, 4, 8, 15, 16, 0 };
401 s
->depth
= depths
[(s
->ctla
& 0x00700000) >> 20];
404 static void g364_invalidate_cursor_position(G364State
*s
)
406 int ymin
, ymax
, start
, end
, i
;
408 /* invalidate only near the cursor */
409 ymin
= s
->cursor_position
& 0xfff;
410 ymax
= MIN(s
->height
, ymin
+ 64);
411 start
= ymin
* ds_get_linesize(s
->ds
);
412 end
= (ymax
+ 1) * ds_get_linesize(s
->ds
);
414 for (i
= start
; i
< end
; i
+= TARGET_PAGE_SIZE
) {
415 cpu_physical_memory_set_dirty(s
->vram_offset
+ i
);
419 static void g364fb_ctrl_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
421 G364State
*s
= opaque
;
423 DPRINTF("write 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
425 if (addr
>= REG_CLR_PAL
&& addr
< REG_CLR_PAL
+ 0x800) {
427 int idx
= (addr
- REG_CLR_PAL
) >> 3;
428 s
->color_palette
[idx
][0] = (val
>> 16) & 0xff;
429 s
->color_palette
[idx
][1] = (val
>> 8) & 0xff;
430 s
->color_palette
[idx
][2] = val
& 0xff;
431 g364fb_invalidate_display(s
);
432 } else if (addr
>= REG_CURS_PAT
&& addr
< REG_CURS_PAT
+ 0x1000) {
434 int idx
= (addr
- REG_CURS_PAT
) >> 3;
435 s
->cursor
[idx
] = val
;
436 g364fb_invalidate_display(s
);
437 } else if (addr
>= REG_CURS_PAL
&& addr
< REG_CURS_PAL
+ 0x18) {
439 int idx
= (addr
- REG_CURS_PAL
) >> 3;
440 s
->cursor_palette
[idx
][0] = (val
>> 16) & 0xff;
441 s
->cursor_palette
[idx
][1] = (val
>> 8) & 0xff;
442 s
->cursor_palette
[idx
][2] = val
& 0xff;
443 g364fb_invalidate_display(s
);
446 case REG_ID
: /* Card identifier; read-only */
447 case REG_BOOT
: /* Boot timing */
448 case 0x80108: /* Line timing: half sync */
449 case 0x80110: /* Line timing: back porch */
450 case 0x80120: /* Line timing: short display */
451 case 0x80128: /* Frame timing: broad pulse */
452 case 0x80130: /* Frame timing: v sync */
453 case 0x80138: /* Frame timing: v preequalise */
454 case 0x80140: /* Frame timing: v postequalise */
455 case 0x80148: /* Frame timing: v blank */
456 case 0x80158: /* Line timing: line time */
457 case 0x80160: /* Frame store: line start */
458 case 0x80168: /* vram cycle: mem init */
459 case 0x80170: /* vram cycle: transfer delay */
460 case 0x80200: /* vram cycle: mask register */
464 s
->top_of_screen
= val
;
465 g364fb_invalidate_display(s
);
475 g364fb_update_depth(s
);
476 g364fb_invalidate_display(s
);
479 g364_invalidate_cursor_position(s
);
480 s
->cursor_position
= val
;
481 g364_invalidate_cursor_position(s
);
487 BADF("invalid write of 0x%08x at [" TARGET_FMT_plx
"]\n", val
, addr
);
491 qemu_irq_lower(s
->irq
);
494 static void g364fb_ctrl_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
496 uint32_t old_val
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
499 val
= (val
<< 16) | (old_val
& 0x0000ffff);
501 val
= val
| (old_val
& 0xffff0000);
502 g364fb_ctrl_writel(opaque
, addr
& ~0x3, val
);
505 static void g364fb_ctrl_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
507 uint32_t old_val
= g364fb_ctrl_readl(opaque
, addr
& ~0x3);
511 val
= val
| (old_val
& 0xffffff00);
514 val
= (val
<< 8) | (old_val
& 0xffff00ff);
517 val
= (val
<< 16) | (old_val
& 0xff00ffff);
520 val
= (val
<< 24) | (old_val
& 0x00ffffff);
523 g364fb_ctrl_writel(opaque
, addr
& ~0x3, val
);
526 static CPUReadMemoryFunc
*g364fb_ctrl_read
[3] = {
532 static CPUWriteMemoryFunc
*g364fb_ctrl_write
[3] = {
538 static int g364fb_load(QEMUFile
*f
, void *opaque
, int version_id
)
540 G364State
*s
= opaque
;
541 unsigned int i
, vram_size
;
546 vram_size
= qemu_get_be32(f
);
547 if (vram_size
< s
->vram_size
)
549 qemu_get_buffer(f
, s
->vram
, s
->vram_size
);
550 for (i
= 0; i
< 256; i
++)
551 qemu_get_buffer(f
, s
->color_palette
[i
], 3);
552 for (i
= 0; i
< 3; i
++)
553 qemu_get_buffer(f
, s
->cursor_palette
[i
], 3);
554 qemu_get_buffer(f
, (uint8_t *)s
->cursor
, sizeof(s
->cursor
));
555 s
->cursor_position
= qemu_get_be32(f
);
556 s
->ctla
= qemu_get_be32(f
);
557 s
->top_of_screen
= qemu_get_be32(f
);
558 s
->width
= qemu_get_be32(f
);
559 s
->height
= qemu_get_be32(f
);
562 g364fb_update_depth(s
);
563 g364fb_invalidate_display(s
);
568 static void g364fb_save(QEMUFile
*f
, void *opaque
)
570 G364State
*s
= opaque
;
573 qemu_put_be32(f
, s
->vram_size
);
574 qemu_put_buffer(f
, s
->vram
, s
->vram_size
);
575 for (i
= 0; i
< 256; i
++)
576 qemu_put_buffer(f
, s
->color_palette
[i
], 3);
577 for (i
= 0; i
< 3; i
++)
578 qemu_put_buffer(f
, s
->cursor_palette
[i
], 3);
579 qemu_put_buffer(f
, (uint8_t *)s
->cursor
, sizeof(s
->cursor
));
580 qemu_put_be32(f
, s
->cursor_position
);
581 qemu_put_be32(f
, s
->ctla
);
582 qemu_put_be32(f
, s
->top_of_screen
);
583 qemu_put_be32(f
, s
->width
);
584 qemu_put_be32(f
, s
->height
);
587 int g364fb_mm_init(int vram_size
, target_phys_addr_t vram_base
,
588 target_phys_addr_t ctrl_base
, int it_shift
,
594 s
= qemu_mallocz(sizeof(G364State
));
596 s
->vram_offset
= qemu_ram_alloc(vram_size
);
597 s
->vram
= qemu_get_ram_ptr(s
->vram_offset
);
598 s
->vram_size
= vram_size
;
601 qemu_register_reset(g364fb_reset
, s
);
602 register_savevm("g364fb", 0, 1, g364fb_save
, g364fb_load
, s
);
605 s
->ds
= graphic_console_init(g364fb_update_display
,
606 g364fb_invalidate_display
,
607 g364fb_screen_dump
, NULL
, s
);
609 cpu_register_physical_memory(vram_base
, s
->vram_size
, s
->vram_offset
);
611 io_ctrl
= cpu_register_io_memory(0, g364fb_ctrl_read
, g364fb_ctrl_write
, s
);
612 cpu_register_physical_memory(ctrl_base
, 0x200000, io_ctrl
);