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 */
36 uint32_t scr_width, scr_height; /* in pixels */
44 #include "g364fb_template.h"
49 #define PIXEL_WIDTH 16
50 #include "g364fb_template.h"
55 #define PIXEL_WIDTH 16
56 #include "g364fb_template.h"
61 #define PIXEL_WIDTH 32
62 #include "g364fb_template.h"
66 #define REG_DISPLAYX 0x0918
67 #define REG_DISPLAYY 0x0940
69 #define CTLA_FORCE_BLANK 0x400
71 static void g364fb_draw_graphic(G364State *s, int full_update)
73 switch (ds_get_bits_per_pixel(s->ds)) {
75 g364fb_draw_graphic8(s, full_update);
78 g364fb_draw_graphic15(s, full_update);
81 g364fb_draw_graphic16(s, full_update);
84 g364fb_draw_graphic32(s, full_update);
87 printf("g364fb: unknown depth %d\n", ds_get_bits_per_pixel(s->ds));
91 dpy_update(s->ds, 0, 0, s->scr_width, s->scr_height);
94 static void g364fb_draw_blank(G364State *s, int full_update)
102 w = s->scr_width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
103 d = ds_get_data(s->ds);
104 for(i = 0; i < s->scr_height; i++) {
106 d += ds_get_linesize(s->ds);
109 dpy_update(s->ds, 0, 0, s->scr_width, s->scr_height);
112 #define GMODE_GRAPH 0
113 #define GMODE_BLANK 1
115 static void g364fb_update_display(void *opaque)
117 G364State *s = opaque;
118 int full_update, graphic_mode;
120 if (s->scr_width == 0 || s->scr_height == 0)
123 if (s->ctla & CTLA_FORCE_BLANK)
124 graphic_mode = GMODE_BLANK;
126 graphic_mode = GMODE_GRAPH;
128 if (graphic_mode != s->graphic_mode) {
129 s->graphic_mode = graphic_mode;
132 if (s->scr_width != ds_get_width(s->ds) || s->scr_height != ds_get_height(s->ds)) {
133 qemu_console_resize(s->ds, s->scr_width, s->scr_height);
136 switch(graphic_mode) {
138 g364fb_draw_graphic(s, full_update);
142 g364fb_draw_blank(s, full_update);
147 /* force a full display refresh */
148 static void g364fb_invalidate_display(void *opaque)
150 G364State *s = opaque;
151 s->graphic_mode = -1; /* force full update */
154 static void g364fb_reset(void *opaque)
156 G364State *s = opaque;
158 memset(s->palette, 0, sizeof(s->palette));
159 s->scr_width = s->scr_height = 0;
160 memset(s->vram_buffer, 0, s->vram_size);
161 s->graphic_mode = -1; /* force full update */
164 static void g364fb_screen_dump(void *opaque, const char *filename)
166 G364State *s = opaque;
169 uint8_t *data_buffer;
172 f = fopen(filename, "wb");
176 data_buffer = s->vram_buffer;
177 fprintf(f, "P6\n%d %d\n%d\n",
178 s->scr_width, s->scr_height, 255);
179 for(y = 0; y < s->scr_height; y++)
180 for(x = 0; x < s->scr_width; x++, data_buffer++) {
181 index = *data_buffer;
182 fputc(s->palette[index][0], f);
183 fputc(s->palette[index][1], f);
184 fputc(s->palette[index][2], f);
189 /* called for accesses to io ports */
190 static uint32_t g364fb_ctrl_readb(void *opaque, target_phys_addr_t addr)
192 //G364State *s = opaque;
200 printf("g364fb/ctrl: invalid read at [" TARGET_FMT_lx "]\n", addr);
207 printf("g364fb/ctrl: read 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
213 static uint32_t g364fb_ctrl_readw(void *opaque, target_phys_addr_t addr)
216 v = g364fb_ctrl_readb(opaque, addr);
217 v |= g364fb_ctrl_readb(opaque, addr + 1) << 8;
221 static uint32_t g364fb_ctrl_readl(void *opaque, target_phys_addr_t addr)
224 v = g364fb_ctrl_readb(opaque, addr);
225 v |= g364fb_ctrl_readb(opaque, addr + 1) << 8;
226 v |= g364fb_ctrl_readb(opaque, addr + 2) << 16;
227 v |= g364fb_ctrl_readb(opaque, addr + 3) << 24;
231 static void g364fb_ctrl_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
233 G364State *s = opaque;
238 printf("g364fb/ctrl: write 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
246 s->palette[idx][c] = (uint8_t)val;
250 s->scr_width = (s->scr_width & 0xfffffc03) | (val << 2);
252 case REG_DISPLAYX + 1:
253 s->scr_width = (s->scr_width & 0xfffc03ff) | (val << 10);
256 s->scr_height = (s->scr_height & 0xffffff80) | (val >> 1);
258 case REG_DISPLAYY + 1:
259 s->scr_height = (s->scr_height & 0xffff801f) | (val << 7);
263 printf("g364fb/ctrl: invalid write of 0x%02x at [" TARGET_FMT_lx "]\n", val, addr);
268 s->graphic_mode = -1; /* force full update */
271 static void g364fb_ctrl_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
273 g364fb_ctrl_writeb(opaque, addr, val & 0xff);
274 g364fb_ctrl_writeb(opaque, addr + 1, (val >> 8) & 0xff);
277 static void g364fb_ctrl_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
279 g364fb_ctrl_writeb(opaque, addr, val & 0xff);
280 g364fb_ctrl_writeb(opaque, addr + 1, (val >> 8) & 0xff);
281 g364fb_ctrl_writeb(opaque, addr + 2, (val >> 16) & 0xff);
282 g364fb_ctrl_writeb(opaque, addr + 3, (val >> 24) & 0xff);
285 static CPUReadMemoryFunc *g364fb_ctrl_read[3] = {
291 static CPUWriteMemoryFunc *g364fb_ctrl_write[3] = {
297 /* called for accesses to video ram */
298 static uint32_t g364fb_mem_readb(void *opaque, target_phys_addr_t addr)
300 G364State *s = opaque;
302 return s->vram_buffer[addr];
305 static uint32_t g364fb_mem_readw(void *opaque, target_phys_addr_t addr)
308 v = g364fb_mem_readb(opaque, addr);
309 v |= g364fb_mem_readb(opaque, addr + 1) << 8;
313 static uint32_t g364fb_mem_readl(void *opaque, target_phys_addr_t addr)
316 v = g364fb_mem_readb(opaque, addr);
317 v |= g364fb_mem_readb(opaque, addr + 1) << 8;
318 v |= g364fb_mem_readb(opaque, addr + 2) << 16;
319 v |= g364fb_mem_readb(opaque, addr + 3) << 24;
323 static void g364fb_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
325 G364State *s = opaque;
327 s->vram_buffer[addr] = val;
330 static void g364fb_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
332 g364fb_mem_writeb(opaque, addr, val & 0xff);
333 g364fb_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
336 static void g364fb_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
338 g364fb_mem_writeb(opaque, addr, val & 0xff);
339 g364fb_mem_writeb(opaque, addr + 1, (val >> 8) & 0xff);
340 g364fb_mem_writeb(opaque, addr + 2, (val >> 16) & 0xff);
341 g364fb_mem_writeb(opaque, addr + 3, (val >> 24) & 0xff);
344 static CPUReadMemoryFunc *g364fb_mem_read[3] = {
350 static CPUWriteMemoryFunc *g364fb_mem_write[3] = {
356 int g364fb_mm_init(int vram_size, int it_shift,
357 target_phys_addr_t vram_base, target_phys_addr_t ctrl_base)
360 int io_vram, io_ctrl;
362 s = qemu_mallocz(sizeof(G364State));
366 s->vram_size = vram_size;
367 s->vram_buffer = qemu_mallocz(s->vram_size);
369 qemu_register_reset(g364fb_reset, s);
372 s->ds = graphic_console_init(g364fb_update_display,
373 g364fb_invalidate_display,
374 g364fb_screen_dump, NULL, s);
376 io_vram = cpu_register_io_memory(0, g364fb_mem_read, g364fb_mem_write, s);
377 cpu_register_physical_memory(vram_base, vram_size, io_vram);
379 io_ctrl = cpu_register_io_memory(0, g364fb_ctrl_read, g364fb_ctrl_write, s);
380 cpu_register_physical_memory(ctrl_base, 0x10000, io_ctrl);