/[gxemul]/trunk/src/devices/dev_fb.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/devices/dev_fb.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations)
Mon Oct 8 16:21:17 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 24064 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1480 2007/02/19 01:34:42 debug Exp $
20061029	Changing usleep(1) calls in the debugger to usleep(10000)
20061107	Adding a new disk image option (-d o...) which sets the ISO9660
		filesystem base offset; also making some other hacks to allow
		NetBSD/dreamcast and homebrew demos/games to boot directly
		from a filesystem image.
		Moving Dreamcast-specific stuff in the documentation to its
		own page (dreamcast.html).
		Adding a border to the Dreamcast PVR framebuffer.
20061108	Adding a -T command line option (again?), for halting the
		emulator on unimplemented memory accesses.
20061109	Continuing on various SH4 and Dreamcast related things.
		The emulator should now halt on more unimplemented device
		accesses, instead of just printing a warning, forcing me to
		actually implement missing stuff :)
20061111	Continuing on SH4 and Dreamcast stuff.
		Adding a bogus Landisk (SH4) machine mode.
20061112	Implementing some parts of the Dreamcast GDROM device. With
		some ugly hacks, NetBSD can (barely) mount an ISO image.
20061113	NetBSD/dreamcast now starts booting from the Live CD image,
		but crashes randomly quite early on in the boot process.
20061122	Beginning on a skeleton interrupt.h and interrupt.c for the
		new interrupt subsystem.
20061124	Continuing on the new interrupt system; taking the first steps
		to attempt to connect CPUs (SuperH and MIPS) and devices
		(dev_cons and SH4 timer interrupts) to it. Many things will
		probably break from now on.
20061125	Converting dev_ns16550, dev_8253 to the new interrupt system.
		Attempting to begin to convert the ISA bus.
20061130	Incorporating a patch from Brian Foley for the configure
		script, which checks for X11 libs in /usr/X11R6/lib64 (which
		is used on some Linux systems).
20061227	Adding a note in the man page about booting from Dreamcast
		CDROM images (i.e. that no external kernel is needed).
20061229	Continuing on the interrupt system rewrite: beginning to
		convert more devices, adding abort() calls for legacy interrupt
		system calls so that everything now _has_ to be rewritten!
		Almost all machine modes are now completely broken.
20061230	More progress on removing old interrupt code, mostly related
		to the ISA bus + devices, the LCA bus (on AlphaBook1), and
		the Footbridge bus (for CATS). And some minor PCI stuff.
		Connecting the ARM cpu to the new interrupt system.
		The CATS, NetWinder, and QEMU_MIPS machine modes now work with
		the new interrupt system :)
20061231	Connecting PowerPC CPUs to the new interrupt system.
		Making PReP machines (IBM 6050) work again.
		Beginning to convert the GT PCI controller (for e.g. Malta
		and Cobalt emulation). Some things work, but not everything.
		Updating Copyright notices for 2007.
20070101	Converting dev_kn02 from legacy style to devinit; the 3max
		machine mode now works with the new interrupt system :-]
20070105	Beginning to convert the SGI O2 machine to the new interrupt
		system; finally converting O2 (IP32) devices to devinit, etc.
20070106	Continuing on the interrupt system redesign/rewrite; KN01
		(PMAX), KN230, and Dreamcast ASIC interrupts should work again,
		moving out stuff from machine.h and devices.h into the
		corresponding devices, beginning the rewrite of i80321
		interrupts, etc.
20070107	Beginning on the rewrite of Eagle interrupt stuff (PReP, etc).
20070117	Beginning the rewrite of Algor (V3) interrupts (finally
		changing dev_v3 into devinit style).
20070118	Removing the "bus" registry concept from machine.h, because
		it was practically meaningless.
		Continuing on the rewrite of Algor V3 ISA interrupts.
20070121	More work on Algor interrupts; they are now working again,
		well enough to run NetBSD/algor. :-)
20070122	Converting VR41xx (HPCmips) interrupts. NetBSD/hpcmips
		can be installed using the new interrupt system :-)
20070123	Making the testmips mode work with the new interrupt system.
20070127	Beginning to convert DEC5800 devices to devinit, and to the
		new interrupt system.
		Converting Playstation 2 devices to devinit, and converting
		the interrupt system. Also fixing a severe bug: the interrupt
		mask register on Playstation 2 is bitwise _toggled_ on writes.
20070128	Removing the dummy NetGear machine mode and the 8250 device
		(which was only used by the NetGear machine).
		Beginning to convert the MacPPC GC (Grand Central) interrupt
		controller to the new interrupt system.
		Converting Jazz interrupts (PICA61 etc.) to the new interrupt
		system. NetBSD/arc can be installed again :-)
		Fixing the JAZZ timer (hardcoding it at 100 Hz, works with
		NetBSD and it is better than a completely dummy timer as it
		was before).
		Converting dev_mp to the new interrupt system, although I
		haven't had time to actually test it yet.
		Completely removing src/machines/interrupts.c, cpu_interrupt
		and cpu_interrupt_ack in src/cpu.c, and
		src/include/machine_interrupts.h! Adding fatal error messages
		+ abort() in the few places that are left to fix.
		Converting dev_z8530 to the new interrupt system.
		FINALLY removing the md_int struct completely from the
		machine struct.
		SH4 fixes (adding a PADDR invalidation in the ITLB replacement
		code in memory_sh.c); the NetBSD/dreamcast LiveCD now runs
		all the way to the login prompt, and can be interacted with :-)
		Converting the CPC700 controller (PCI and interrupt controller
		for PM/PPC) to the new interrupt system.
20070129	Fixing MACE ISA interrupts (SGI IP32 emulation). Both NetBSD/
		sgimips' and OpenBSD/sgi's ramdisk kernels can now be
		interacted with again.
20070130	Moving out the MIPS multi_lw and _sw instruction combinations
		so that they are auto-generated at compile time instead.
20070131	Adding detection of amd64/x86_64 hosts in the configure script,
		for doing initial experiments (again :-) with native code
		generation.
		Adding a -k command line option to set the size of the dyntrans
		cache, and a -B command line option to disable native code
		generation, even if GXemul was compiled with support for
		native code generation for the specific host CPU architecture.
20070201	Experimenting with a skeleton for native code generation.
		Changing the default behaviour, so that native code generation
		is now disabled by default, and has to be enabled by using
		-b on the command line.
20070202	Continuing the native code generation experiments.
		Making PCI interrupts work for Footbridge again.
20070203	More native code generation experiments.
		Removing most of the native code generation experimental code,
		it does not make sense to include any quick hacks like this.
		Minor cleanup/removal of some more legacy MIPS interrupt code.
20070204	Making i80321 interrupts work again (for NetBSD/evbarm etc.),
		and fixing the timer at 100 Hz.
20070206	Experimenting with removing the wdc interrupt slowness hack.
20070207	Lowering the number of dyntrans TLB entries for MIPS from
		192 to 128, resulting in a minor speed improvement.
		Minor optimization to the code invalidation routine in
		cpu_dyntrans.c.
20070208	Increasing (experimentally) the nr of dyntrans instructions per
		loop from 60 to 120.
20070210	Commenting out (experimentally) the dyntrans_device_danger
		detection in memory_rw.c.
		Changing the testmips and baremips machines to use a revision 2
		MIPS64 CPU by default, instead of revision 1.
		Removing the dummy i960, IA64, x86, AVR32, and HP PA-RISC
		files, the PC bios emulation, and the Olivetti M700 (ARC) and
		db64360 emulation modes.
20070211	Adding an "mp" demo to the demos directory, which tests the
		SMP functionality of the testmips machine.
		Fixing PReP interrupts some more. NetBSD/prep now boots again.
20070216	Adding a "nop workaround" for booting Mach/PMAX to the
		documentation; thanks to Artur Bujdoso for the values.
		Converting more of the MacPPC interrupt stuff to the new
		system.
		Beginning to convert BeBox interrupts to the new system.
		PPC603e should NOT have the PPC_NO_DEC flag! Removing it.
		Correcting BeBox clock speed (it was set to 100 in the NetBSD
		bootinfo block, but should be 33000000/4), allowing NetBSD
		to start without using the (incorrect) PPC_NO_DEC hack.
20070217	Implementing (slow) AltiVec vector loads and stores, allowing
		NetBSD/macppc to finally boot using the GENERIC kernel :-)
		Updating the documentation with install instructions for
		NetBSD/macppc.
20070218-19	Regression testing for the release.

==============  RELEASE 0.4.4  ==============


1 /*
2 * Copyright (C) 2003-2007 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: dev_fb.c,v 1.128 2006/12/30 13:30:57 debug Exp $
29 *
30 * Generic framebuffer device.
31 *
32 * DECstation VFB01 monochrome framebuffer, 1024x864
33 * DECstation VFB02 8-bit color framebuffer, 1024x864
34 * DECstation Maxine, 1024x768 8-bit color
35 * Playstation 2 (24-bit color)
36 * Generic (any resolution, several bit depths possible, useful for
37 * testmachines)
38 *
39 *
40 * TODO: This should actually be independent of X11, but that
41 * might be too hard to do right now.
42 *
43 * TODO: playstation 2 pixels are stored in another format, actually
44 */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "console.h"
51 #include "cpu.h"
52 #include "devices.h"
53 #include "machine.h"
54 #include "memory.h"
55 #include "misc.h"
56 #include "x11.h"
57
58 #ifdef WITH_X11
59 #include <X11/Xlib.h>
60 #include <X11/Xos.h>
61 #include <X11/Xutil.h>
62 #endif
63
64
65 #define FB_TICK_SHIFT 19
66
67
68 /* #define FB_DEBUG */
69
70 /*
71 * set_grayscale_palette():
72 *
73 * Fill d->rgb_palette with grayscale values. ncolors should
74 * be something like 2, 4, 16, or 256.
75 */
76 void set_grayscale_palette(struct vfb_data *d, int ncolors)
77 {
78 int i, gray;
79
80 for (i=0; i<256; i++) {
81 gray = 255*i/(ncolors-1);
82 d->rgb_palette[i*3 + 0] = gray;
83 d->rgb_palette[i*3 + 1] = gray;
84 d->rgb_palette[i*3 + 2] = gray;
85 }
86 }
87
88
89 /*
90 * set_blackwhite_palette():
91 *
92 * Set color 0 = black, all others to white.
93 */
94 void set_blackwhite_palette(struct vfb_data *d, int ncolors)
95 {
96 int i, gray;
97
98 for (i=0; i<256; i++) {
99 gray = i==0? 0 : 255;
100 d->rgb_palette[i*3 + 0] = gray;
101 d->rgb_palette[i*3 + 1] = gray;
102 d->rgb_palette[i*3 + 2] = gray;
103 }
104 }
105
106
107 static void set_title(struct vfb_data *d)
108 {
109 snprintf(d->title, sizeof(d->title),"GXemul: %ix%ix%i %s framebuffer",
110 d->visible_xsize, d->visible_ysize, d->bit_depth, d->name);
111 d->title[sizeof(d->title)-1] = '\0';
112 }
113
114
115 /*
116 * dev_fb_resize():
117 *
118 * Resize a framebuffer window. (This functionality is probably a bit buggy,
119 * because I didn't think of including it from the start.)
120 *
121 * SUPER-IMPORTANT: Anyone who resizes a framebuffer by calling this function
122 * must also clear all dyntrans address translations manually, in all cpus
123 * which might have access to the framebuffer!
124 */
125 void dev_fb_resize(struct vfb_data *d, int new_xsize, int new_ysize)
126 {
127 unsigned char *new_framebuffer;
128 int y, new_bytes_per_line;
129 size_t size;
130
131 if (d == NULL) {
132 fatal("dev_fb_resize(): d == NULL\n");
133 return;
134 }
135
136 if (new_xsize < 10 || new_ysize < 10) {
137 fatal("dev_fb_resize(): size too small.\n");
138 exit(1);
139 }
140
141 new_bytes_per_line = new_xsize * d->bit_depth / 8;
142 size = new_ysize * new_bytes_per_line;
143
144 new_framebuffer = malloc(size);
145 if (new_framebuffer == NULL) {
146 fprintf(stderr, "dev_fb_resize(): out of memory\n");
147 exit(1);
148 }
149
150 /* Copy the old framebuffer to the new: */
151 if (d->framebuffer != NULL) {
152 for (y=0; y<new_ysize; y++) {
153 size_t fromofs = d->bytes_per_line * y;
154 size_t toofs = new_bytes_per_line * y;
155 size_t len_to_copy = d->bytes_per_line <
156 new_bytes_per_line? d->bytes_per_line
157 : new_bytes_per_line;
158 memset(new_framebuffer + toofs, 0, new_bytes_per_line);
159 if (y < d->x11_ysize)
160 memmove(new_framebuffer + toofs,
161 d->framebuffer + fromofs, len_to_copy);
162 }
163
164 free(d->framebuffer);
165 }
166
167 d->framebuffer = new_framebuffer;
168 d->framebuffer_size = size;
169
170 if (new_xsize > d->xsize || new_ysize > d->ysize) {
171 d->update_x1 = d->update_y1 = 0;
172 d->update_x2 = new_xsize - 1;
173 d->update_y2 = new_ysize - 1;
174 }
175
176 d->bytes_per_line = new_bytes_per_line;
177 d->xsize = d->visible_xsize = new_xsize;
178 d->ysize = d->visible_ysize = new_ysize;
179
180 d->x11_xsize = d->xsize / d->vfb_scaledown;
181 d->x11_ysize = d->ysize / d->vfb_scaledown;
182
183 memory_device_update_data(d->memory, d, d->framebuffer);
184
185 set_title(d);
186
187 #ifdef WITH_X11
188 if (d->fb_window != NULL) {
189 x11_fb_resize(d->fb_window, new_xsize, new_ysize);
190 x11_set_standard_properties(d->fb_window, d->title);
191 }
192 #endif
193 }
194
195
196 /*
197 * dev_fb_setcursor():
198 */
199 void dev_fb_setcursor(struct vfb_data *d, int cursor_x, int cursor_y, int on,
200 int cursor_xsize, int cursor_ysize)
201 {
202 if (cursor_x < 0)
203 cursor_x = 0;
204 if (cursor_y < 0)
205 cursor_y = 0;
206 if (cursor_x + cursor_xsize >= d->xsize)
207 cursor_x = d->xsize - cursor_xsize;
208 if (cursor_y + cursor_ysize >= d->ysize)
209 cursor_y = d->ysize - cursor_ysize;
210
211 #ifdef WITH_X11
212 if (d->fb_window != NULL) {
213 d->fb_window->cursor_x = cursor_x;
214 d->fb_window->cursor_y = cursor_y;
215 d->fb_window->cursor_on = on;
216 d->fb_window->cursor_xsize = cursor_xsize;
217 d->fb_window->cursor_ysize = cursor_ysize;
218 }
219 #endif
220
221 /* debug("dev_fb_setcursor(%i,%i, size %i,%i, on=%i)\n",
222 cursor_x, cursor_y, cursor_xsize, cursor_ysize, on); */
223 }
224
225
226 /*
227 * framebuffer_blockcopyfill():
228 *
229 * This function should be used by devices that are capable of doing
230 * block copy/fill.
231 *
232 * If fillflag is non-zero, then fill_[rgb] should contain the color
233 * with which to fill. (In 8-bit mode, only fill_r is used.)
234 *
235 * If fillflag is zero, copy mode is used, and from_[xy] should contain
236 * the offset on the framebuffer where we should copy from.
237 *
238 * NOTE: Overlapping copies are undefined!
239 */
240 void framebuffer_blockcopyfill(struct vfb_data *d, int fillflag, int fill_r,
241 int fill_g, int fill_b, int x1, int y1, int x2, int y2,
242 int from_x, int from_y)
243 {
244 int x, y;
245 long from_ofs, dest_ofs, linelen;
246
247 if (fillflag)
248 debug("framebuffer_blockcopyfill(FILL, %i,%i, %i,%i, "
249 "color %i,%i,%i)\n", x1,y1, x2,y2, fill_r, fill_g, fill_b);
250 else
251 debug("framebuffer_blockcopyfill(COPY, %i,%i, %i,%i, from "
252 "%i,%i)\n", x1,y1, x2,y2, from_x,from_y);
253
254 /* Clip x: */
255 if (x1 < 0) x1 = 0;
256 if (x1 >= d->xsize) x1 = d->xsize-1;
257 if (x2 < 0) x2 = 0;
258 if (x2 >= d->xsize) x2 = d->xsize-1;
259
260 dest_ofs = d->bytes_per_line * y1 + (d->bit_depth/8) * x1;
261 linelen = (x2-x1 + 1) * (d->bit_depth/8);
262 /* NOTE: linelen is nr of bytes, not pixels */
263
264 if (fillflag) {
265 for (y=y1; y<=y2; y++) {
266 if (y>=0 && y<d->ysize) {
267 unsigned char *buf =
268 d->framebuffer + dest_ofs;
269
270 if (d->bit_depth == 24) {
271 for (x=0; x<linelen && x<sizeof(buf);
272 x += 3) {
273 buf[x] = fill_r;
274 buf[x+1] = fill_g;
275 buf[x+2] = fill_b;
276 }
277 } else if (d->bit_depth == 8) {
278 memset(buf, fill_r, linelen);
279 } else {
280 fatal("Unimplemented bit-depth (%i)"
281 " for fb fill\n", d->bit_depth);
282 exit(1);
283 }
284 }
285
286 dest_ofs += d->bytes_per_line;
287 }
288 } else {
289 from_ofs = d->bytes_per_line * from_y +
290 (d->bit_depth/8) * from_x;
291 for (y=y1; y<=y2; y++) {
292 if (y >= 0 && y < d->ysize) {
293 if (from_y >= 0 && from_y < d->ysize)
294 memmove(d->framebuffer + dest_ofs,
295 d->framebuffer + from_ofs, linelen);
296 else
297 memset(d->framebuffer + dest_ofs,
298 0, linelen);
299 }
300 from_y ++;
301 from_ofs += d->bytes_per_line;
302 dest_ofs += d->bytes_per_line;
303 }
304 }
305
306 if (x1 < d->update_x1 || d->update_x1 == -1) d->update_x1 = x1;
307 if (x1 > d->update_x2 || d->update_x2 == -1) d->update_x2 = x1;
308 if (x2 < d->update_x1 || d->update_x1 == -1) d->update_x1 = x2;
309 if (x2 > d->update_x2 || d->update_x2 == -1) d->update_x2 = x2;
310
311 if (y1 < d->update_y1 || d->update_y1 == -1) d->update_y1 = y1;
312 if (y1 > d->update_y2 || d->update_y2 == -1) d->update_y2 = y1;
313 if (y2 < d->update_y1 || d->update_y1 == -1) d->update_y1 = y2;
314 if (y2 > d->update_y2 || d->update_y2 == -1) d->update_y2 = y2;
315 }
316
317
318 #ifdef WITH_X11
319
320 #define REDRAW redraw_fallback
321 #include "fb_include.c"
322 #undef REDRAW
323
324 #define FB_24
325 #define REDRAW redraw_24
326 #include "fb_include.c"
327 #undef REDRAW
328 #undef FB_24
329 #define FB_16
330 #define REDRAW redraw_16
331 #include "fb_include.c"
332 #undef FB_16
333 #undef REDRAW
334 #define FB_15
335 #define REDRAW redraw_15
336 #include "fb_include.c"
337 #undef REDRAW
338 #undef FB_15
339
340 #define FB_BO
341 #define FB_24
342 #define REDRAW redraw_24_bo
343 #include "fb_include.c"
344 #undef REDRAW
345 #undef FB_24
346 #define FB_16
347 #define REDRAW redraw_16_bo
348 #include "fb_include.c"
349 #undef FB_16
350 #undef REDRAW
351 #define FB_15
352 #define REDRAW redraw_15_bo
353 #include "fb_include.c"
354 #undef REDRAW
355 #undef FB_15
356 #undef FB_BO
357
358 #define FB_SCALEDOWN
359
360 #define REDRAW redraw_fallback_sd
361 #include "fb_include.c"
362 #undef REDRAW
363
364 #define FB_24
365 #define REDRAW redraw_24_sd
366 #include "fb_include.c"
367 #undef REDRAW
368 #undef FB_24
369 #define FB_16
370 #define REDRAW redraw_16_sd
371 #include "fb_include.c"
372 #undef FB_16
373 #undef REDRAW
374 #define FB_15
375 #define REDRAW redraw_15_sd
376 #include "fb_include.c"
377 #undef REDRAW
378 #undef FB_15
379
380 #define FB_BO
381 #define FB_24
382 #define REDRAW redraw_24_bo_sd
383 #include "fb_include.c"
384 #undef REDRAW
385 #undef FB_24
386 #define FB_16
387 #define REDRAW redraw_16_bo_sd
388 #include "fb_include.c"
389 #undef FB_16
390 #undef REDRAW
391 #define FB_15
392 #define REDRAW redraw_15_bo_sd
393 #include "fb_include.c"
394 #undef REDRAW
395 #undef FB_15
396 #undef FB_BO
397
398 void (*redraw[2 * 4 * 2])(struct vfb_data *, int, int) = {
399 redraw_fallback, redraw_fallback,
400 redraw_15, redraw_15_bo,
401 redraw_16, redraw_16_bo,
402 redraw_24, redraw_24_bo,
403 redraw_fallback_sd, redraw_fallback_sd,
404 redraw_15_sd, redraw_15_bo_sd,
405 redraw_16_sd, redraw_16_bo_sd,
406 redraw_24_sd, redraw_24_bo_sd };
407
408 #endif /* WITH_X11 */
409
410
411 DEVICE_TICK(fb)
412 {
413 struct vfb_data *d = extra;
414 #ifdef WITH_X11
415 int need_to_flush_x11 = 0;
416 int need_to_redraw_cursor = 0;
417 #endif
418
419 if (!cpu->machine->use_x11)
420 return;
421
422 do {
423 uint64_t high, low = (uint64_t)(int64_t) -1;
424 int x, y;
425
426 memory_device_dyntrans_access(cpu, cpu->mem,
427 extra, &low, &high);
428 if ((int64_t)low == -1)
429 break;
430
431 /* printf("low=%016llx high=%016llx\n",
432 (long long)low, (long long)high); */
433
434 x = (low % d->bytes_per_line) * 8 / d->bit_depth;
435 y = low / d->bytes_per_line;
436 if (x < d->update_x1 || d->update_x1 == -1)
437 d->update_x1 = x;
438 if (x > d->update_x2 || d->update_x2 == -1)
439 d->update_x2 = x;
440 if (y < d->update_y1 || d->update_y1 == -1)
441 d->update_y1 = y;
442 if (y > d->update_y2 || d->update_y2 == -1)
443 d->update_y2 = y;
444
445 x = ((low+7) % d->bytes_per_line) * 8 / d->bit_depth;
446 y = (low+7) / d->bytes_per_line;
447 if (x < d->update_x1 || d->update_x1 == -1)
448 d->update_x1 = x;
449 if (x > d->update_x2 || d->update_x2 == -1)
450 d->update_x2 = x;
451 if (y < d->update_y1 || d->update_y1 == -1)
452 d->update_y1 = y;
453 if (y > d->update_y2 || d->update_y2 == -1)
454 d->update_y2 = y;
455
456 x = (high % d->bytes_per_line) * 8 / d->bit_depth;
457 y = high / d->bytes_per_line;
458 if (x < d->update_x1 || d->update_x1 == -1)
459 d->update_x1 = x;
460 if (x > d->update_x2 || d->update_x2 == -1)
461 d->update_x2 = x;
462 if (y < d->update_y1 || d->update_y1 == -1)
463 d->update_y1 = y;
464 if (y > d->update_y2 || d->update_y2 == -1)
465 d->update_y2 = y;
466
467 x = ((high+7) % d->bytes_per_line) * 8 / d->bit_depth;
468 y = (high+7) / d->bytes_per_line;
469 if (x < d->update_x1 || d->update_x1 == -1)
470 d->update_x1 = x;
471 if (x > d->update_x2 || d->update_x2 == -1)
472 d->update_x2 = x;
473 if (y < d->update_y1 || d->update_y1 == -1)
474 d->update_y1 = y;
475 if (y > d->update_y2 || d->update_y2 == -1)
476 d->update_y2 = y;
477
478 /*
479 * An update covering more than one line will automatically
480 * force an update of all the affected lines:
481 */
482 if (d->update_y1 != d->update_y2) {
483 d->update_x1 = 0;
484 d->update_x2 = d->xsize-1;
485 }
486 } while (0);
487
488 #ifdef WITH_X11
489 /* Do we need to redraw the cursor? */
490 if (d->fb_window->cursor_on != d->fb_window->OLD_cursor_on ||
491 d->fb_window->cursor_x != d->fb_window->OLD_cursor_x ||
492 d->fb_window->cursor_y != d->fb_window->OLD_cursor_y ||
493 d->fb_window->cursor_xsize != d->fb_window->OLD_cursor_xsize ||
494 d->fb_window->cursor_ysize != d->fb_window->OLD_cursor_ysize)
495 need_to_redraw_cursor = 1;
496
497 if (d->update_x2 != -1) {
498 if (((d->update_x1 >= d->fb_window->OLD_cursor_x &&
499 d->update_x1 < (d->fb_window->OLD_cursor_x +
500 d->fb_window->OLD_cursor_xsize)) ||
501 (d->update_x2 >= d->fb_window->OLD_cursor_x &&
502 d->update_x2 < (d->fb_window->OLD_cursor_x +
503 d->fb_window->OLD_cursor_xsize)) ||
504 (d->update_x1 < d->fb_window->OLD_cursor_x &&
505 d->update_x2 >= (d->fb_window->OLD_cursor_x +
506 d->fb_window->OLD_cursor_xsize)) ) &&
507 ( (d->update_y1 >= d->fb_window->OLD_cursor_y &&
508 d->update_y1 < (d->fb_window->OLD_cursor_y +
509 d->fb_window->OLD_cursor_ysize)) ||
510 (d->update_y2 >= d->fb_window->OLD_cursor_y &&
511 d->update_y2 < (d->fb_window->OLD_cursor_y +
512 d->fb_window->OLD_cursor_ysize)) ||
513 (d->update_y1 < d->fb_window->OLD_cursor_y &&
514 d->update_y2 >= (d->fb_window->OLD_cursor_y +
515 d->fb_window->OLD_cursor_ysize)) ) )
516 need_to_redraw_cursor = 1;
517 }
518
519 if (need_to_redraw_cursor) {
520 /* Remove old cursor, if any: */
521 if (d->fb_window->OLD_cursor_on) {
522 XPutImage(d->fb_window->x11_display,
523 d->fb_window->x11_fb_window,
524 d->fb_window->x11_fb_gc, d->fb_window->fb_ximage,
525 d->fb_window->OLD_cursor_x/d->vfb_scaledown,
526 d->fb_window->OLD_cursor_y/d->vfb_scaledown,
527 d->fb_window->OLD_cursor_x/d->vfb_scaledown,
528 d->fb_window->OLD_cursor_y/d->vfb_scaledown,
529 d->fb_window->OLD_cursor_xsize/d->vfb_scaledown + 1,
530 d->fb_window->OLD_cursor_ysize/d->vfb_scaledown +1);
531 }
532 }
533 #endif
534
535 if (d->update_x2 != -1) {
536 #ifdef WITH_X11
537 int y;
538 #endif
539 int addr, addr2, q = d->vfb_scaledown;
540
541 if (d->update_x1 >= d->visible_xsize)
542 d->update_x1 = d->visible_xsize - 1;
543 if (d->update_x2 >= d->visible_xsize)
544 d->update_x2 = d->visible_xsize - 1;
545 if (d->update_y1 >= d->visible_ysize)
546 d->update_y1 = d->visible_ysize - 1;
547 if (d->update_y2 >= d->visible_ysize)
548 d->update_y2 = d->visible_ysize - 1;
549
550 /* Without these, we might miss the rightmost/bottom pixel: */
551 d->update_x2 += (q - 1);
552 d->update_y2 += (q - 1);
553
554 d->update_x1 = d->update_x1 / q * q;
555 d->update_x2 = d->update_x2 / q * q;
556 d->update_y1 = d->update_y1 / q * q;
557 d->update_y2 = d->update_y2 / q * q;
558
559 addr = d->update_y1 * d->bytes_per_line +
560 d->update_x1 * d->bit_depth / 8;
561 addr2 = d->update_y1 * d->bytes_per_line +
562 d->update_x2 * d->bit_depth / 8;
563
564 #ifdef WITH_X11
565 for (y=d->update_y1; y<=d->update_y2; y+=q) {
566 d->redraw_func(d, addr, addr2 - addr);
567 addr += d->bytes_per_line * q;
568 addr2 += d->bytes_per_line * q;
569 }
570
571 XPutImage(d->fb_window->x11_display, d->fb_window->
572 x11_fb_window, d->fb_window->x11_fb_gc, d->fb_window->
573 fb_ximage, d->update_x1/d->vfb_scaledown, d->update_y1/
574 d->vfb_scaledown, d->update_x1/d->vfb_scaledown,
575 d->update_y1/d->vfb_scaledown,
576 (d->update_x2 - d->update_x1)/d->vfb_scaledown + 1,
577 (d->update_y2 - d->update_y1)/d->vfb_scaledown + 1);
578
579 need_to_flush_x11 = 1;
580 #endif
581
582 d->update_x1 = d->update_y1 = 99999;
583 d->update_x2 = d->update_y2 = -1;
584 }
585
586 #ifdef WITH_X11
587 if (need_to_redraw_cursor) {
588 /* Paint new cursor: */
589 if (d->fb_window->cursor_on) {
590 x11_redraw_cursor(cpu->machine,
591 d->fb_window->fb_number);
592 d->fb_window->OLD_cursor_on = d->fb_window->cursor_on;
593 d->fb_window->OLD_cursor_x = d->fb_window->cursor_x;
594 d->fb_window->OLD_cursor_y = d->fb_window->cursor_y;
595 d->fb_window->OLD_cursor_xsize = d->fb_window->
596 cursor_xsize;
597 d->fb_window->OLD_cursor_ysize = d->fb_window->
598 cursor_ysize;
599 need_to_flush_x11 = 1;
600 }
601 }
602 #endif
603
604 #ifdef WITH_X11
605 if (need_to_flush_x11)
606 XFlush(d->fb_window->x11_display);
607 #endif
608 }
609
610
611 DEVICE_ACCESS(fb)
612 {
613 struct vfb_data *d = extra;
614 size_t i;
615
616 #ifdef FB_DEBUG
617 if (writeflag == MEM_WRITE) { if (data[0]) {
618 fatal("[ dev_fb: write to addr=%08lx, data = ",
619 (long)relative_addr);
620 for (i=0; i<len; i++)
621 fatal("%02x ", data[i]);
622 fatal("]\n");
623 } else {
624 fatal("[ dev_fb: read from addr=%08lx, data = ",
625 (long)relative_addr);
626 for (i=0; i<len; i++)
627 fatal("%02x ", d->framebuffer[relative_addr + i]);
628 fatal("]\n");
629 }
630 #endif
631
632 if (relative_addr >= d->framebuffer_size)
633 return 0;
634
635 /* See if a write actually modifies the framebuffer contents: */
636 if (writeflag == MEM_WRITE) {
637 for (i=0; i<len; i++) {
638 if (data[i] != d->framebuffer[relative_addr + i])
639 break;
640
641 /* If all bytes are equal to what is already stored
642 in the framebuffer, then simply return: */
643 if (i == len-1)
644 return 1;
645 }
646 }
647
648 /*
649 * If the framebuffer is modified, then we should keep a track
650 * of which area(s) we modify, so that the display isn't updated
651 * unnecessarily.
652 */
653 if (writeflag == MEM_WRITE && cpu->machine->use_x11) {
654 int x, y, x2,y2;
655
656 x = (relative_addr % d->bytes_per_line) * 8 / d->bit_depth;
657 y = relative_addr / d->bytes_per_line;
658 x2 = ((relative_addr + len) % d->bytes_per_line)
659 * 8 / d->bit_depth;
660 y2 = (relative_addr + len) / d->bytes_per_line;
661
662 if (x < d->update_x1 || d->update_x1 == -1)
663 d->update_x1 = x;
664 if (x > d->update_x2 || d->update_x2 == -1)
665 d->update_x2 = x;
666
667 if (y < d->update_y1 || d->update_y1 == -1)
668 d->update_y1 = y;
669 if (y > d->update_y2 || d->update_y2 == -1)
670 d->update_y2 = y;
671
672 if (x2 < d->update_x1 || d->update_x1 == -1)
673 d->update_x1 = x2;
674 if (x2 > d->update_x2 || d->update_x2 == -1)
675 d->update_x2 = x2;
676
677 if (y2 < d->update_y1 || d->update_y1 == -1)
678 d->update_y1 = y2;
679 if (y2 > d->update_y2 || d->update_y2 == -1)
680 d->update_y2 = y2;
681
682 /*
683 * An update covering more than one line will automatically
684 * force an update of all the affected lines:
685 */
686 if (y != y2) {
687 d->update_x1 = 0;
688 d->update_x2 = d->xsize-1;
689 }
690 }
691
692 /*
693 * Read from/write to the framebuffer:
694 * (TODO: take the color_plane_mask into account)
695 *
696 * Calling memcpy() is probably overkill, as it usually is just one
697 * or a few bytes that are read/written at a time.
698 */
699 if (writeflag == MEM_WRITE) {
700 if (len > 8)
701 memcpy(d->framebuffer + relative_addr, data, len);
702 else {
703 for (i=0; i<len; i++)
704 d->framebuffer[relative_addr + i] = data[i];
705 }
706 } else {
707 if (len > 8)
708 memcpy(data, d->framebuffer + relative_addr, len);
709 else {
710 for (i=0; i<len; i++)
711 data[i] = d->framebuffer[relative_addr + i];
712 }
713 }
714
715 return 1;
716 }
717
718
719 /*
720 * dev_fb_init():
721 *
722 * This function is big and ugly, but the point is to initialize a framebuffer
723 * device. :-)
724 *
725 * visible_xsize and visible_ysize are the sizes of the visible display area.
726 * xsize and ysize tell how much memory is actually allocated (for example
727 * visible_xsize could be 640, but xsize could be 1024, for better alignment).
728 *
729 * vfb_type is useful for selecting special features.
730 *
731 * type = VFB_GENERIC is the most useful type, especially when bit_depth = 24.
732 *
733 * VFB_DEC_VFB01, _VFB02, and VFB_DEC_MAXINE are DECstation specific.
734 *
735 * VFB_HPC is like generic, but the color encoding is done as on HPCmips
736 * and Dreamcast.
737 *
738 * If bit_depth = -15 (note the minus sign), then a special hack is used for
739 * the Playstation Portable's 5-bit R, 5-bit G, 5-bit B.
740 */
741 struct vfb_data *dev_fb_init(struct machine *machine, struct memory *mem,
742 uint64_t baseaddr, int vfb_type, int visible_xsize, int visible_ysize,
743 int xsize, int ysize, int bit_depth, char *name)
744 {
745 struct vfb_data *d;
746 size_t size, nlen;
747 int flags;
748 int reverse_start = 0;
749 char *name2;
750
751 d = malloc(sizeof(struct vfb_data));
752 if (d == NULL) {
753 fprintf(stderr, "out of memory\n");
754 exit(1);
755 }
756 memset(d, 0, sizeof(struct vfb_data));
757
758 if (vfb_type & VFB_REVERSE_START) {
759 vfb_type &= ~VFB_REVERSE_START;
760 reverse_start = 1;
761 }
762
763 d->memory = mem;
764 d->vfb_type = vfb_type;
765
766 /* Defaults: */
767 d->xsize = xsize; d->visible_xsize = visible_xsize;
768 d->ysize = ysize; d->visible_ysize = visible_ysize;
769
770 d->bit_depth = bit_depth;
771
772 if (bit_depth == 15) {
773 d->color32k = 1;
774 bit_depth = d->bit_depth = 16;
775 } else if (bit_depth == -15) {
776 d->psp_15bit = 1;
777 bit_depth = d->bit_depth = 16;
778 }
779
780 /* Specific types: */
781 switch (vfb_type) {
782 case VFB_DEC_VFB01:
783 /* DECstation VFB01 (monochrome) */
784 d->xsize = 2048; d->visible_xsize = 1024;
785 d->ysize = 1024; d->visible_ysize = 864;
786 d->bit_depth = 1;
787 break;
788 case VFB_DEC_VFB02:
789 /* DECstation VFB02 (color) */
790 d->xsize = 1024; d->visible_xsize = 1024;
791 d->ysize = 1024; d->visible_ysize = 864;
792 d->bit_depth = 8;
793 break;
794 case VFB_DEC_MAXINE:
795 /* DECstation Maxine (1024x768x8) */
796 d->xsize = 1024; d->visible_xsize = d->xsize;
797 d->ysize = 768; d->visible_ysize = d->ysize;
798 d->bit_depth = 8;
799 break;
800 case VFB_PLAYSTATION2:
801 /* Playstation 2 */
802 d->xsize = xsize; d->visible_xsize = d->xsize;
803 d->ysize = ysize; d->visible_ysize = d->ysize;
804 d->bit_depth = 24;
805 break;
806 }
807
808 if (d->bit_depth == 2 || d->bit_depth == 4)
809 set_grayscale_palette(d, 1 << d->bit_depth);
810 else if (d->bit_depth == 8 || d->bit_depth == 1)
811 set_blackwhite_palette(d, 1 << d->bit_depth);
812
813 d->vfb_scaledown = machine->x11_scaledown;
814
815 d->bytes_per_line = d->xsize * d->bit_depth / 8;
816 size = d->ysize * d->bytes_per_line;
817
818 d->framebuffer = malloc(size);
819 if (d->framebuffer == NULL) {
820 fprintf(stderr, "out of memory\n");
821 exit(1);
822 }
823
824 /* Clear the framebuffer (all black pixels): */
825 d->framebuffer_size = size;
826 memset(d->framebuffer, reverse_start? 255 : 0, size);
827
828 d->x11_xsize = d->visible_xsize / d->vfb_scaledown;
829 d->x11_ysize = d->visible_ysize / d->vfb_scaledown;
830
831 /* Only "update" from the start if we need to fill with white. */
832 /* (The Ximage will be black from the start anyway.) */
833 if (reverse_start) {
834 d->update_x1 = d->update_y1 = 0;
835 d->update_x2 = d->xsize - 1;
836 d->update_y2 = d->ysize - 1;
837 } else {
838 d->update_x1 = d->update_y1 = 99999;
839 d->update_x2 = d->update_y2 = -1;
840 }
841
842 d->name = strdup(name);
843 set_title(d);
844
845 #ifdef WITH_X11
846 if (machine->use_x11) {
847 int i = 0;
848 d->fb_window = x11_fb_init(d->x11_xsize, d->x11_ysize,
849 d->title, machine->x11_scaledown, machine);
850 switch (d->fb_window->x11_screen_depth) {
851 case 15: i = 2; break;
852 case 16: i = 4; break;
853 case 24: i = 6; break;
854 }
855 if (d->fb_window->fb_ximage->byte_order)
856 i ++;
857 if (d->vfb_scaledown > 1)
858 i += 8;
859 d->redraw_func = redraw[i];
860 } else
861 #endif
862 d->fb_window = NULL;
863
864 nlen = strlen(name) + 10;
865 name2 = malloc(nlen);
866 if (name2 == NULL) {
867 fprintf(stderr, "out of memory in dev_fb_init()\n");
868 exit(1);
869 }
870 snprintf(name2, nlen, "fb [%s]", name);
871
872 flags = DM_DEFAULT;
873 if ((baseaddr & 0xfff) == 0)
874 flags = DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK;
875
876 flags |= DM_READS_HAVE_NO_SIDE_EFFECTS;
877
878 memory_device_register(mem, name2, baseaddr, size, dev_fb_access,
879 d, flags, d->framebuffer);
880
881 machine_add_tickfunction(machine, dev_fb_tick, d, FB_TICK_SHIFT, 0.0);
882 return d;
883 }
884

  ViewVC Help
Powered by ViewVC 1.1.26