]> git.proxmox.com Git - qemu.git/blame - hw/tcx.c
new bochs BIOS - 16 bit APM support (initial patch by Struan Bartlett)
[qemu.git] / hw / tcx.c
CommitLineData
420557e8 1/*
6f7e9aec 2 * QEMU TCX Frame buffer
420557e8 3 *
6f7e9aec 4 * Copyright (c) 2003-2005 Fabrice Bellard
420557e8
FB
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
420557e8
FB
26#define MAXX 1024
27#define MAXY 768
6f7e9aec 28#define TCX_DAC_NREGS 16
420557e8 29
420557e8 30typedef struct TCXState {
8d5f07fa 31 uint32_t addr;
420557e8 32 DisplayState *ds;
8d5f07fa 33 uint8_t *vram;
e80cfcfc 34 unsigned long vram_offset;
6f7e9aec 35 uint16_t width, height;
e80cfcfc 36 uint8_t r[256], g[256], b[256];
6f7e9aec 37 uint8_t dac_index, dac_state;
420557e8
FB
38} TCXState;
39
e80cfcfc
FB
40static void tcx_draw_line32(TCXState *s1, uint8_t *d,
41 const uint8_t *s, int width)
420557e8 42{
e80cfcfc
FB
43 int x;
44 uint8_t val;
45
46 for(x = 0; x < width; x++) {
47 val = *s++;
e80cfcfc 48 *d++ = s1->b[val];
6f7e9aec
FB
49 *d++ = s1->g[val];
50 *d++ = s1->r[val];
e80cfcfc
FB
51 d++;
52 }
420557e8
FB
53}
54
e80cfcfc
FB
55static void tcx_draw_line24(TCXState *s1, uint8_t *d,
56 const uint8_t *s, int width)
57{
58 int x;
59 uint8_t val;
8d5f07fa 60
e80cfcfc
FB
61 for(x = 0; x < width; x++) {
62 val = *s++;
e80cfcfc 63 *d++ = s1->b[val];
6f7e9aec
FB
64 *d++ = s1->g[val];
65 *d++ = s1->r[val];
e80cfcfc
FB
66 }
67}
68
69static void tcx_draw_line8(TCXState *s1, uint8_t *d,
70 const uint8_t *s, int width)
420557e8 71{
e80cfcfc
FB
72 int x;
73 uint8_t val;
74
75 for(x = 0; x < width; x++) {
76 val = *s++;
77 /* XXX translate between palettes? */
78 *d++ = val;
420557e8 79 }
420557e8
FB
80}
81
e80cfcfc
FB
82/* Fixed line length 1024 allows us to do nice tricks not possible on
83 VGA... */
84void tcx_update_display(void *opaque)
420557e8 85{
e80cfcfc
FB
86 TCXState *ts = opaque;
87 uint32_t page;
88 int y, page_min, page_max, y_start, dd, ds;
89 uint8_t *d, *s;
90 void (*f)(TCXState *s1, uint8_t *d, const uint8_t *s, int width);
91
92 if (ts->ds->depth == 0)
93 return;
6f7e9aec 94 page = ts->vram_offset;
e80cfcfc
FB
95 y_start = -1;
96 page_min = 0x7fffffff;
97 page_max = -1;
98 d = ts->ds->data;
6f7e9aec 99 s = ts->vram;
e80cfcfc
FB
100 dd = ts->ds->linesize;
101 ds = 1024;
102
103 switch (ts->ds->depth) {
104 case 32:
105 f = tcx_draw_line32;
106 break;
107 case 24:
108 f = tcx_draw_line24;
109 break;
110 default:
111 case 8:
112 f = tcx_draw_line8;
113 break;
114 case 0:
115 return;
116 }
662f3c86 117
6f7e9aec 118 for(y = 0; y < ts->height; y += 4, page += TARGET_PAGE_SIZE) {
0a962c02 119 if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG)) {
e80cfcfc
FB
120 if (y_start < 0)
121 y_start = y;
122 if (page < page_min)
123 page_min = page;
124 if (page > page_max)
125 page_max = page;
6f7e9aec 126 f(ts, d, s, ts->width);
e80cfcfc
FB
127 d += dd;
128 s += ds;
6f7e9aec 129 f(ts, d, s, ts->width);
e80cfcfc
FB
130 d += dd;
131 s += ds;
6f7e9aec 132 f(ts, d, s, ts->width);
e80cfcfc
FB
133 d += dd;
134 s += ds;
6f7e9aec 135 f(ts, d, s, ts->width);
e80cfcfc
FB
136 d += dd;
137 s += ds;
138 } else {
139 if (y_start >= 0) {
140 /* flush to display */
141 dpy_update(ts->ds, 0, y_start,
6f7e9aec 142 ts->width, y - y_start);
e80cfcfc
FB
143 y_start = -1;
144 }
145 d += dd * 4;
146 s += ds * 4;
147 }
148 }
149 if (y_start >= 0) {
150 /* flush to display */
151 dpy_update(ts->ds, 0, y_start,
6f7e9aec 152 ts->width, y - y_start);
e80cfcfc
FB
153 }
154 /* reset modified pages */
155 if (page_max != -1) {
0a962c02
FB
156 cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
157 VGA_DIRTY_FLAG);
e80cfcfc 158 }
420557e8
FB
159}
160
e80cfcfc 161void tcx_invalidate_display(void *opaque)
420557e8 162{
e80cfcfc
FB
163 TCXState *s = opaque;
164 int i;
165
166 for (i = 0; i < MAXX*MAXY; i += TARGET_PAGE_SIZE) {
e80cfcfc 167 cpu_physical_memory_set_dirty(s->vram_offset + i);
e80cfcfc 168 }
420557e8
FB
169}
170
e80cfcfc 171static void tcx_save(QEMUFile *f, void *opaque)
420557e8
FB
172{
173 TCXState *s = opaque;
e80cfcfc
FB
174
175 qemu_put_be32s(f, (uint32_t *)&s->addr);
176 qemu_put_be32s(f, (uint32_t *)&s->vram);
6f7e9aec
FB
177 qemu_put_be16s(f, (uint16_t *)&s->height);
178 qemu_put_be16s(f, (uint16_t *)&s->width);
e80cfcfc
FB
179 qemu_put_buffer(f, s->r, 256);
180 qemu_put_buffer(f, s->g, 256);
181 qemu_put_buffer(f, s->b, 256);
6f7e9aec
FB
182 qemu_put_8s(f, &s->dac_index);
183 qemu_put_8s(f, &s->dac_state);
420557e8
FB
184}
185
e80cfcfc 186static int tcx_load(QEMUFile *f, void *opaque, int version_id)
420557e8 187{
e80cfcfc
FB
188 TCXState *s = opaque;
189
190 if (version_id != 1)
191 return -EINVAL;
192
193 qemu_get_be32s(f, (uint32_t *)&s->addr);
194 qemu_get_be32s(f, (uint32_t *)&s->vram);
6f7e9aec
FB
195 qemu_get_be16s(f, (uint16_t *)&s->height);
196 qemu_get_be16s(f, (uint16_t *)&s->width);
e80cfcfc
FB
197 qemu_get_buffer(f, s->r, 256);
198 qemu_get_buffer(f, s->g, 256);
199 qemu_get_buffer(f, s->b, 256);
6f7e9aec
FB
200 qemu_get_8s(f, &s->dac_index);
201 qemu_get_8s(f, &s->dac_state);
e80cfcfc 202 return 0;
420557e8
FB
203}
204
e80cfcfc 205static void tcx_reset(void *opaque)
420557e8 206{
e80cfcfc
FB
207 TCXState *s = opaque;
208
209 /* Initialize palette */
210 memset(s->r, 0, 256);
211 memset(s->g, 0, 256);
212 memset(s->b, 0, 256);
213 s->r[255] = s->g[255] = s->b[255] = 255;
214 memset(s->vram, 0, MAXX*MAXY);
0a962c02
FB
215 cpu_physical_memory_reset_dirty(s->vram_offset, s->vram_offset + MAXX*MAXY,
216 VGA_DIRTY_FLAG);
6f7e9aec
FB
217 s->dac_index = 0;
218 s->dac_state = 0;
219}
220
221static uint32_t tcx_dac_readl(void *opaque, target_phys_addr_t addr)
222{
223 return 0;
224}
225
226static void tcx_dac_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
227{
228 TCXState *s = opaque;
229 uint32_t saddr;
230
231 saddr = (addr & (TCX_DAC_NREGS - 1)) >> 2;
232 switch (saddr) {
233 case 0:
234 s->dac_index = val >> 24;
235 s->dac_state = 0;
236 break;
237 case 1:
238 switch (s->dac_state) {
239 case 0:
240 s->r[s->dac_index] = val >> 24;
241 s->dac_state++;
242 break;
243 case 1:
244 s->g[s->dac_index] = val >> 24;
245 s->dac_state++;
246 break;
247 case 2:
248 s->b[s->dac_index] = val >> 24;
249 default:
250 s->dac_state = 0;
251 break;
252 }
253 break;
254 default:
255 break;
256 }
257 return;
420557e8
FB
258}
259
6f7e9aec
FB
260static CPUReadMemoryFunc *tcx_dac_read[3] = {
261 tcx_dac_readl,
262 tcx_dac_readl,
263 tcx_dac_readl,
264};
265
266static CPUWriteMemoryFunc *tcx_dac_write[3] = {
267 tcx_dac_writel,
268 tcx_dac_writel,
269 tcx_dac_writel,
270};
271
e80cfcfc 272void *tcx_init(DisplayState *ds, uint32_t addr, uint8_t *vram_base,
6f7e9aec 273 unsigned long vram_offset, int vram_size, int width, int height)
420557e8
FB
274{
275 TCXState *s;
6f7e9aec 276 int io_memory;
420557e8
FB
277
278 s = qemu_mallocz(sizeof(TCXState));
279 if (!s)
e80cfcfc 280 return NULL;
420557e8 281 s->ds = ds;
8d5f07fa 282 s->addr = addr;
e80cfcfc
FB
283 s->vram = vram_base;
284 s->vram_offset = vram_offset;
6f7e9aec
FB
285 s->width = width;
286 s->height = height;
e80cfcfc 287
6f7e9aec
FB
288 cpu_register_physical_memory(addr + 0x800000, vram_size, vram_offset);
289 io_memory = cpu_register_io_memory(0, tcx_dac_read, tcx_dac_write, s);
290 cpu_register_physical_memory(addr + 0x200000, TCX_DAC_NREGS, io_memory);
e80cfcfc
FB
291
292 register_savevm("tcx", addr, 1, tcx_save, tcx_load, s);
293 qemu_register_reset(tcx_reset, s);
294 tcx_reset(s);
6f7e9aec 295 dpy_resize(s->ds, width, height);
e80cfcfc 296 return s;
420557e8
FB
297}
298
e80cfcfc 299void tcx_screen_dump(void *opaque, const char *filename)
8d5f07fa 300{
e80cfcfc 301 TCXState *s = opaque;
8d5f07fa 302 FILE *f;
e80cfcfc 303 uint8_t *d, *d1, v;
8d5f07fa
FB
304 int y, x;
305
306 f = fopen(filename, "wb");
307 if (!f)
e80cfcfc 308 return;
6f7e9aec
FB
309 fprintf(f, "P6\n%d %d\n%d\n", s->width, s->height, 255);
310 d1 = s->vram;
311 for(y = 0; y < s->height; y++) {
8d5f07fa 312 d = d1;
6f7e9aec 313 for(x = 0; x < s->width; x++) {
8d5f07fa 314 v = *d;
e80cfcfc
FB
315 fputc(s->r[v], f);
316 fputc(s->g[v], f);
317 fputc(s->b[v], f);
8d5f07fa
FB
318 d++;
319 }
e80cfcfc 320 d1 += MAXX;
8d5f07fa
FB
321 }
322 fclose(f);
323 return;
324}
325
326
327