2 * QEMU TCX Frame buffer
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #define TCX_DAC_NREGS 16
30 typedef struct TCXState {
34 ram_addr_t vram_offset;
35 uint16_t width, height;
36 uint8_t r[256], g[256], b[256];
37 uint32_t palette[256];
38 uint8_t dac_index, dac_state;
41 static void tcx_screen_dump(void *opaque, const char *filename);
43 /* XXX: unify with vga draw line functions */
44 static inline unsigned int rgb_to_pixel8(unsigned int r, unsigned int g, unsigned b)
46 return ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6);
49 static inline unsigned int rgb_to_pixel15(unsigned int r, unsigned int g, unsigned b)
51 return ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
54 static inline unsigned int rgb_to_pixel16(unsigned int r, unsigned int g, unsigned b)
56 return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
59 static inline unsigned int rgb_to_pixel32(unsigned int r, unsigned int g, unsigned b)
61 return (r << 16) | (g << 8) | b;
64 static void update_palette_entries(TCXState *s, int start, int end)
67 for(i = start; i < end; i++) {
68 switch(s->ds->depth) {
71 s->palette[i] = rgb_to_pixel8(s->r[i], s->g[i], s->b[i]);
74 s->palette[i] = rgb_to_pixel15(s->r[i], s->g[i], s->b[i]);
77 s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
80 s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
86 static void tcx_draw_line32(TCXState *s1, uint8_t *d,
87 const uint8_t *s, int width)
91 uint32_t *p = (uint32_t *)d;
93 for(x = 0; x < width; x++) {
95 *p++ = s1->palette[val];
99 static void tcx_draw_line16(TCXState *s1, uint8_t *d,
100 const uint8_t *s, int width)
104 uint16_t *p = (uint16_t *)d;
106 for(x = 0; x < width; x++) {
108 *p++ = s1->palette[val];
112 static void tcx_draw_line8(TCXState *s1, uint8_t *d,
113 const uint8_t *s, int width)
118 for(x = 0; x < width; x++) {
120 *d++ = s1->palette[val];
124 /* Fixed line length 1024 allows us to do nice tricks not possible on
126 static void tcx_update_display(void *opaque)
128 TCXState *ts = opaque;
129 ram_addr_t page, page_min, page_max;
130 int y, y_start, dd, ds;
132 void (*f)(TCXState *s1, uint8_t *d, const uint8_t *s, int width);
134 if (ts->ds->depth == 0)
136 page = ts->vram_offset;
138 page_min = 0xffffffff;
142 dd = ts->ds->linesize;
145 switch (ts->ds->depth) {
161 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
162 if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
169 f(ts, d, s, ts->width);
172 f(ts, d, s, ts->width);
175 f(ts, d, s, ts->width);
178 f(ts, d, s, ts->width);
183 /* flush to display */
184 dpy_update(ts->ds, 0, y_start,
185 ts->width, y - y_start);
193 /* flush to display */
194 dpy_update(ts->ds, 0, y_start,
195 ts->width, y - y_start);
197 /* reset modified pages */
198 if (page_min <= page_max) {
199 cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
204 static void tcx_invalidate_display(void *opaque)
206 TCXState *s = opaque;
209 for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
210 cpu_physical_memory_set_dirty(s->vram_offset + i);
214 static void tcx_save(QEMUFile *f, void *opaque)
216 TCXState *s = opaque;
218 qemu_put_be32s(f, (uint32_t *)&s->addr);
219 qemu_put_be32s(f, (uint32_t *)&s->vram);
220 qemu_put_be16s(f, (uint16_t *)&s->height);
221 qemu_put_be16s(f, (uint16_t *)&s->width);
222 qemu_put_buffer(f, s->r, 256);
223 qemu_put_buffer(f, s->g, 256);
224 qemu_put_buffer(f, s->b, 256);
225 qemu_put_8s(f, &s->dac_index);
226 qemu_put_8s(f, &s->dac_state);
229 static int tcx_load(QEMUFile *f, void *opaque, int version_id)
231 TCXState *s = opaque;
236 qemu_get_be32s(f, (uint32_t *)&s->addr);
237 qemu_get_be32s(f, (uint32_t *)&s->vram);
238 qemu_get_be16s(f, (uint16_t *)&s->height);
239 qemu_get_be16s(f, (uint16_t *)&s->width);
240 qemu_get_buffer(f, s->r, 256);
241 qemu_get_buffer(f, s->g, 256);
242 qemu_get_buffer(f, s->b, 256);
243 qemu_get_8s(f, &s->dac_index);
244 qemu_get_8s(f, &s->dac_state);
245 update_palette_entries(s, 0, 256);
249 static void tcx_reset(void *opaque)
251 TCXState *s = opaque;
253 /* Initialize palette */
254 memset(s->r, 0, 256);
255 memset(s->g, 0, 256);
256 memset(s->b, 0, 256);
257 s->r[255] = s->g[255] = s->b[255] = 255;
258 update_palette_entries(s, 0, 256);
259 memset(s->vram, 0, MAXX*MAXY);
260 cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset + MAXX*MAXY,
266 static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
271 static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
273 TCXState *s = opaque;
276 saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
279 s->dac_index = val >> 24;
283 switch (s->dac_state) {
285 s->r[s->dac_index] = val >> 24;
286 update_palette_entries(s, s->dac_index, s->dac_index + 1);
290 s->g[s->dac_index] = val >> 24;
291 update_palette_entries(s, s->dac_index, s->dac_index + 1);
295 s->b[s->dac_index] = val >> 24;
296 update_palette_entries(s, s->dac_index, s->dac_index + 1);
308 static CPUReadMemoryFunc *tcx_dac_read[3] = {
314 static CPUWriteMemoryFunc *tcx_dac_write[3] = {
320 void tcx_init(DisplayState *ds, uint32_t addr, uint8_t *vram_base,
321 unsigned long vram_offset, int vram_size, int width, int height)
326 s = qemu_mallocz(sizeof(TCXState));
332 s->vram_offset = vram_offset;
336 cpu_register_physical_memory(addr + 0x800000, vram_size, vram_offset);
337 io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
338 cpu_register_physical_memory(addr + 0x200000, TCX_DAC_NREGS, io_memory);
340 graphic_console_init(s->ds, tcx_update_display, tcx_invalidate_display,
342 register_savevm("tcx", addr, 1, tcx_save, tcx_load, s);
343 qemu_register_reset(tcx_reset, s);
345 dpy_resize(s->ds, width, height);
348 static void tcx_screen_dump(void *opaque, const char *filename)
350 TCXState *s = opaque;
355 f = fopen(filename, "wb");
358 fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
360 for(y = 0; y < s->height; y++) {
362 for(x = 0; x < s->width; x++) {