2 * QEMU G364 framebuffer Emulator.
4 * Copyright (c) 2007-2008 Hervé 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"
28 typedef struct G364State {
29 unsigned int vram_size;
32 uint8_t palette[256][3];
33 /* display refresh support */
37 uint32_t scr_width, scr_height; /* in pixels */
45 #include "g364fb_template.h"
50 #define PIXEL_WIDTH 16
51 #include "g364fb_template.h"
56 #define PIXEL_WIDTH 16
57 #include "g364fb_template.h"
62 #define PIXEL_WIDTH 32
63 #include "g364fb_template.h"
67 #define REG_DISPLAYX 0x0918
68 #define REG_DISPLAYY 0x0940
70 #define CTLA_FORCE_BLANK 0x400
72 static void g364fb_draw_graphic(G364State *s, int full_update)
74 switch (ds_get_bits_per_pixel(s->ds)) {
76 g364fb_draw_graphic8(s, full_update);
79 g364fb_draw_graphic15(s, full_update);
82 g364fb_draw_graphic16(s, full_update);
85 g364fb_draw_graphic32(s, full_update);
88 printf("g364fb: unknown depth %d\n", ds_get_bits_per_pixel(s->ds));
92 dpy_update(s->ds, 0, 0, s->scr_width, s->scr_height);
95 static void g364fb_draw_blank(G364State *s, int full_update)
103 w = s->scr_width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
104 d = ds_get_data(s->ds);
105 for(i = 0; i < s->scr_height; i++) {
107 d += ds_get_linesize(s->ds);
110 dpy_update(s->ds, 0, 0, s->scr_width, s->scr_height);
113 #define GMODE_GRAPH 0
114 #define GMODE_BLANK 1
116 static void g364fb_update_display(void *opaque)
118 G364State *s = opaque;
119 int full_update, graphic_mode;
121 if (s->scr_width == 0 || s->scr_height == 0)
124 if (s->ctla & CTLA_FORCE_BLANK)
125 graphic_mode = GMODE_BLANK;
127 graphic_mode = GMODE_GRAPH;
129 if (graphic_mode != s->graphic_mode) {
130 s->graphic_mode = graphic_mode;
133 if (s->scr_width != ds_get_width(s->ds) || s->scr_height != ds_get_height(s->ds)) {
134 qemu_console_resize(s->console, s->scr_width, s->scr_height);
137 switch(graphic_mode) {
139 g364fb_draw_graphic(s, full_update);
143 g364fb_draw_blank(s, full_update);
148 /* force a full display refresh */
149 static void g364fb_invalidate_display(void *opaque)
151 G364State *s = opaque;
152 s->graphic_mode = -1; /* force full update */
155 static void g364fb_reset(void *opaque)
157 G364State *s = opaque;
159 memset(s->palette, 0, sizeof(s->palette));
160 s->scr_width = s->scr_height = 0;
161 memset(s->vram_buffer, 0, s->vram_size);
162 s->graphic_mode = -1; /* force full update */
165 static void g364fb_screen_dump(void *opaque, const char *filename)
167 G364State *s = opaque;
170 uint8_t *data_buffer;
173 f = fopen(filename, "wb");
177 data_buffer = s->vram_buffer;
178 fprintf(f, "P6\n%d %d\n%d\n",
179 s->scr_width, s->scr_height, 255);
180 for(y = 0; y < s->scr_height; y++)
181 for(x = 0; x < s->scr_width; x++, data_buffer++) {
182 index = *data_buffer;
183 fputc(s->palette[index][0], f);
184 fputc(s->palette[index][1], f);
185 fputc(s->palette[index][2], f);
190 /* called for accesses to io ports */
191 static uint32_t g364fb_ctrl_readb(void *opaque, target_phys_addr_t addr)
193 //G364State *s = opaque;
201 printf("g364fb/ctrl: invalid read at [" TARGET_FMT_lx "]\n", addr);
208 printf("g364fb/ctrl: read 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
214 static uint32_t g364fb_ctrl_readw(void *opaque, target_phys_addr_t addr)
217 v = g364fb_ctrl_readb(opaque, addr);
218 v |= g364fb_ctrl_readb(opaque, addr + 1) << 8;
222 static uint32_t g364fb_ctrl_readl(void *opaque, target_phys_addr_t addr)
225 v = g364fb_ctrl_readb(opaque, addr);
226 v |= g364fb_ctrl_readb(opaque, addr + 1) << 8;
227 v |= g364fb_ctrl_readb(opaque, addr + 2) << 16;
228 v |= g364fb_ctrl_readb(opaque, addr + 3) << 24;
232 static void g364fb_ctrl_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
234 G364State *s = opaque;
239 printf("g364fb/ctrl: write 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
247 s->palette[idx][c] = (uint8_t)val;
251 s->scr_width = (s->scr_width & 0xfffffc03) | (val << 2);
253 case REG_DISPLAYX + 1:
254 s->scr_width = (s->scr_width & 0xfffc03ff) | (val << 10);
257 s->scr_height = (s->scr_height & 0xffffff80) | (val >> 1);
259 case REG_DISPLAYY + 1:
260 s->scr_height = (s->scr_height & 0xffff801f) | (val << 7);
264 printf("g364fb/ctrl: invalid write of 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
269 s->graphic_mode = -1; /* force full update */
272 static void g364fb_ctrl_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
274 g364fb_ctrl_writeb(opaque, addr, val & 0xff);
275 g364fb_ctrl_writeb(opaque, addr + 1, (val >> 8) & 0xff);
278 static void g364fb_ctrl_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
280 g364fb_ctrl_writeb(opaque, addr, val & 0xff);
281 g364fb_ctrl_writeb(opaque, addr + 1, (val >> 8) & 0xff);
282 g364fb_ctrl_writeb(opaque, addr + 2, (val >> 16) & 0xff);
283 g364fb_ctrl_writeb(opaque, addr + 3, (val >> 24) & 0xff);
286 static CPUReadMemoryFunc *g364fb_ctrl_read[3] = {
292 static CPUWriteMemoryFunc *g364fb_ctrl_write[3] = {
298 /* called for accesses to video ram */
299 static uint32_t g364fb_mem_readb(void *opaque, target_phys_addr_t addr)
301 G364State *s = opaque;
303 return s->vram_buffer[addr];
306 static uint32_t g364fb_mem_readw(void *opaque, target_phys_addr_t addr)
309 v = g364fb_mem_readb(opaque, addr);
310 v |= g364fb_mem_readb(opaque, addr + 1) << 8;
314 static uint32_t g364fb_mem_readl(void *opaque, target_phys_addr_t addr)
317 v = g364fb_mem_readb(opaque, addr);
318 v |= g364fb_mem_readb(opaque, addr + 1) << 8;
319 v |= g364fb_mem_readb(opaque, addr + 2) << 16;
320 v |= g364fb_mem_readb(opaque, addr + 3) << 24;
324 static void g364fb_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
326 G364State *s = opaque;
328 s->vram_buffer[addr] = val;
331 static void g364fb_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
333 g364fb_mem_writeb(opaque, addr, val & 0xff);
334 g364fb_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
337 static void g364fb_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
339 g364fb_mem_writeb(opaque, addr, val & 0xff);
340 g364fb_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
341 g364fb_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
342 g364fb_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
345 static CPUReadMemoryFunc *g364fb_mem_read[3] = {
351 static CPUWriteMemoryFunc *g364fb_mem_write[3] = {
357 int g364fb_mm_init(DisplayState *ds,
358 int vram_size, int it_shift,
359 target_phys_addr_t vram_base, target_phys_addr_t ctrl_base)
362 int io_vram, io_ctrl;
364 s = qemu_mallocz(sizeof(G364State));
368 s->vram_size = vram_size;
369 s->vram_buffer = qemu_mallocz(s->vram_size);
371 qemu_register_reset(g364fb_reset, s);
376 s->console = graphic_console_init(ds, g364fb_update_display,
377 g364fb_invalidate_display,
378 g364fb_screen_dump, NULL, s);
380 io_vram = cpu_register_io_memory(0, g364fb_mem_read, g364fb_mem_write, s);
381 cpu_register_physical_memory(vram_base, vram_size, io_vram);
383 io_ctrl = cpu_register_io_memory(0, g364fb_ctrl_read, g364fb_ctrl_write, s);
384 cpu_register_physical_memory(ctrl_base, 0x10000, io_ctrl);