/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Mon Oct 8 16:19:56 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 23698 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1256 2006/06/23 20:43:44 debug Exp $
20060219	Various minor updates. Removing the old MIPS16 skeleton code,
		because it will need to be rewritten for dyntrans anyway.
20060220-22	Removing the non-working dyntrans backend support.
		Continuing on the 64-bit dyntrans virtual memory generalization.
20060223	More work on the 64-bit vm generalization.
20060225	Beginning on MIPS dyntrans load/store instructions.
		Minor PPC updates (64-bit load/store, etc).
		Fixes for the variable-instruction-length framework, some
		minor AVR updates (a simple Hello World program works!).
		Beginning on a skeleton for automatically generating documen-
		tation (for devices etc.).
20060226	PPC updates (adding some more 64-bit instructions, etc).
		AVR updates (more instructions).
		FINALLY found and fixed the zs bug, making NetBSD/macppc
		accept the serial console.
20060301	Adding more AVR instructions.
20060304	Continuing on AVR-related stuff. Beginning on a framework for
		cycle-accurate device emulation. Adding an experimental "PAL
		TV" device (just a dummy so far).
20060305	Adding more AVR instructions.
		Adding a dummy epcom serial controller (for TS7200 emulation).
20060310	Removing the emul() command from configuration files, so only
		net() and machine() are supported.
		Minor progress on the MIPS dyntrans rewrite.
20060311	Continuing on the MIPS dyntrans rewrite (adding more
		instructions, etc).
20060315	Adding more instructions (sllv, srav, srlv, bgtz[l], blez[l],
		beql, bnel, slti[u], various loads and stores).
20060316	Removing the ALWAYS_SIGNEXTEND_32 option, since it was rarely
		used.
		Adding more MIPS dyntrans instructions, and fixing bugs.
20060318	Implementing fast loads/stores for MIPS dyntrans (big/little
		endian, 32-bit and 64-bit modes).
20060320	Making MIPS dyntrans the default configure option; use
		"--enable-oldmips" to use the old bintrans system.
		Adding MIPS dyntrans dmult[u]; minor updates.
20060322	Continuing... adding some more instructions.
		Adding a simple skeleton for demangling C++ "_ZN" symbols.
20060323	Moving src/debugger.c into a new directory (src/debugger/).
20060324	Fixing the hack used to load PPC ELFs (useful for relocated
		Linux/ppc kernels), and adding a dummy G3 machine mode.
20060325-26	Beginning to experiment with GDB remote serial protocol
		connections; adding a -G command line option for selecting
		which TCP port to listen to.
20060330	Beginning a major cleanup to replace things like "0x%016llx"
		with more correct "0x%016"PRIx64, etc.
		Continuing on the GDB remote serial protocol support.
20060331	More cleanup, and some minor GDB remote progress.
20060402	Adding a hack to the configure script, to allow compilation
		on systems that lack PRIx64 etc.
20060406	Removing the temporary FreeBSD/arm hack in dev_ns16550.c and
		replacing it with a better fix from Olivier Houchard.
20060407	A remote debugger (gdb or ddd) can now start and stop the
		emulator using the GDB remote serial protocol, and registers
		and memory can be read. MIPS only for now.
20060408	More GDB progress: single-stepping also works, and also adding
		support for ARM, PowerPC, and Alpha targets.
		Continuing on the delay-slot-across-page-boundary issue.
20060412	Minor update: beginning to add support for the SPARC target
		to the remote GDB functionality.
20060414	Various MIPS updates: adding more instructions for dyntrans
		(eret, add), and making some exceptions work. Fixing a bug
		in dmult[u].
		Implementing the first SPARC instructions (sethi, or).
20060415	Adding "magic trap" instructions so that PROM calls can be
		software emulated in MIPS dyntrans.
		Adding more MIPS dyntrans instructions (ddiv, dadd) and
		fixing another bug in dmult.
20060416	More MIPS dyntrans progress: adding [d]addi, movn, movz, dsllv,
		rfi, an ugly hack for supporting R2000/R3000 style faked caches,
		preliminary interrupt support, and various other updates and
		bugfixes.
20060417	Adding more SPARC instructions (add, sub, sll[x], sra[x],
		srl[x]), and useful SPARC header definitions.
		Adding the first (trivial) x86/AMD64 dyntrans instructions (nop,
		cli/sti, stc/clc, std/cld, simple mov, inc ax). Various other
		x86 updates related to variable instruction length stuff.
		Adding unaligned loads/stores to the MIPS dyntrans mode (but
		still using the pre-dyntrans (slow) imlementation).
20060419	Fixing a MIPS dyntrans exception-in-delay-slot bug.
		Removing the old "show opcode statistics" functionality, since
		it wasn't really useful and isn't implemented for dyntrans.
		Single-stepping (or running with instruction trace) now looks
		ok with dyntrans with delay-slot architectures.
20060420	Minor hacks (removing the -B command line option when compiled
		for non-bintrans, and some other very minor updates).
		Adding (slow) MIPS dyntrans load-linked/store-conditional.
20060422	Applying fixes for bugs discovered by Nils Weller's nwcc
		(static DEC memmap => now per machine, and adding an extern
		keyword in cpu_arm_instr.c).
		Finally found one of the MIPS dyntrans bugs that I've been
		looking for (copy/paste spelling error BIG vs LITTLE endian in
		cpu_mips_instr_loadstore.c for 16-bit fast stores).
		FINALLY found the major MIPS dyntrans bug: slti vs sltiu
		signed/unsigned code in cpu_mips_instr.c. :-)
		Adding more MIPS dyntrans instructions (lwc1, swc1, bgezal[l],
		ctc1, tlt[u], tge[u], tne, beginning on rdhwr).
		NetBSD/hpcmips can now reach userland when using dyntrans :-)
		Adding some more x86 dyntrans instructions.
		Finally removed the old Alpha-specific virtual memory code,
		and replaced it with the generic 64-bit version.
		Beginning to add disassembly support for SPECIAL3 MIPS opcodes.
20060423	Continuing on the delay-slot-across-page-boundary issue;
		adding an end_of_page2 ic slot (like I had planned before, but
		had removed for some reason).
		Adding a quick-and-dirty fallback to legacy coprocessor 1
		code (i.e. skipping dyntrans implementation for now).
		NetBSD/hpcmips and NetBSD/pmax (when running on an emulated
		R4400) can now be installed and run. :-)  (Many bugs left
		to fix, though.)
		Adding more MIPS dyntrans instructions: madd[u], msub[u].
		Cleaning up the SPECIAL2 vs R5900/TX79/C790 "MMI" opcode
		maps somewhat (disassembly and dyntrans instruction decoding).
20060424	Adding an isa_revision field to mips_cpu_types.h, and making
		sure that SPECIAL3 opcodes cause Reserved Instruction
		exceptions on MIPS32/64 revisions lower than 2.
		Adding the SPARC 'ba', 'call', 'jmpl/retl', 'and', and 'xor'
		instructions.
20060425	Removing the -m command line option ("run at most x 
		instructions") and -T ("single_step_on_bad_addr"), because
		they never worked correctly with dyntrans anyway.
		Freshening up the man page.
20060428	Adding more MIPS dyntrans instructions: bltzal[l], idle.
		Enabling MIPS dyntrans compare interrupts.
20060429	FINALLY found the weird dyntrans bug, causing NetBSD etc. to
		behave strangely: some floating point code (conditional
		coprocessor branches) could not be reused from the old
		non-dyntrans code. The "quick-and-dirty fallback" only appeared
		to work. Fixing by implementing bc1* for MIPS dyntrans.
		More MIPS instructions: [d]sub, sdc1, ldc1, dmtc1, dmfc1, cfc0.
		Freshening up MIPS floating point disassembly appearance.
20060430	Continuing on C790/R5900/TX79 disassembly; implementing 128-bit
		"por" and "pextlw".
20060504	Disabling -u (userland emulation) unless compiled as unstable
		development version.
		Beginning on freshening up the testmachine include files,
		to make it easier to reuse those files (placing them in
		src/include/testmachine/), and beginning on a set of "demos"
		or "tutorials" for the testmachine functionality.
		Minor updates to the MIPS GDB remote protocol stub.
		Refreshing doc/experiments.html and gdb_remote.html.
		Enabling Alpha emulation in the stable release configuration,
		even though no guest OSes for Alpha can run yet.
20060505	Adding a generic 'settings' object, which will contain
		references to settable variables (which will later be possible
		to access using the debugger).
20060506	Updating dev_disk and corresponding demo/documentation (and
		switching from SCSI to IDE disk types, so it actually works
		with current test machines :-).
20060510	Adding a -D_LARGEFILE_SOURCE hack for 64-bit Linux hosts,
		so that fseeko() doesn't give a warning.
		Updating the section about how dyntrans works (the "runnable
		IR") in doc/intro.html.
		Instruction updates (some x64=1 checks, some more R5900
		dyntrans stuff: better mul/mult separation from MIPS32/64,
		adding ei and di).
		Updating MIPS cpuregs.h to a newer one (from NetBSD).
		Adding more MIPS dyntrans instructions: deret, ehb.
20060514	Adding disassembly and beginning implementation of SPARC wr
		and wrpr instructions.
20060515	Adding a SUN SPARC machine mode, with dummy SS20 and Ultra1
		machines. Adding the 32-bit "rd psr" instruction.
20060517	Disassembly support for the general SPARC rd instruction.
		Partial implementation of the cmp (subcc) instruction.
		Some other minor updates (making sure that R5900 processors
		start up with the EIE bit enabled, otherwise Linux/playstation2
		receives no interrupts).
20060519	Minor MIPS updates/cleanups.
20060521	Moving the MeshCube machine into evbmips; this seems to work
		reasonably well with a snapshot of a NetBSD MeshCube kernel.
		Cleanup/fix of MIPS config0 register initialization.
20060529	Minor MIPS fixes, including a sign-extension fix to the
		unaligned load/store code, which makes NetBSD/pmax on R3000
		work better with dyntrans. (Ultrix and Linux/DECstation still
		don't work, though.)
20060530	Minor updates to the Alpha machine mode: adding an AlphaBook
		mode, an LCA bus (forwarding accesses to an ISA bus), etc.
20060531	Applying a bugfix for the MIPS dyntrans sc[d] instruction from
		Ondrej Palkovsky. (Many thanks.)
20060601	Minifix to allow ARM immediate msr instruction to not give
		an error for some valid values.
		More Alpha updates.
20060602	Some minor Alpha updates.
20060603	Adding the Alpha cmpbge instruction. NetBSD/alpha prints its
		first boot messages :-) on an emulated Alphabook 1.
20060612	Minor updates; adding a dev_ether.h include file for the
		testmachine ether device. Continuing the hunt for the dyntrans
		bug which makes Linux and Ultrix on DECstation behave
		strangely... FINALLY found it! It seems to be related to
		invalidation of the translation cache, on tlbw{r,i}. There
		also seems to be some remaining interrupt-related problems.
20060614	Correcting the implementation of ldc1/sdc1 for MIPS dyntrans
		(so that it uses 16 32-bit registers if the FR bit in the
		status register is not set).
20060616	REMOVING BINTRANS COMPLETELY!
		Removing the old MIPS interpretation mode.
		Removing the MFHILO_DELAY and instruction delay stuff, because
		they wouldn't work with dyntrans anyway.
20060617	Some documentation updates (adding "NetBSD-archive" to some
		URLs, and new Debian/DECstation installation screenshots).
		Removing the "tracenull" and "enable-caches" configure options.
		Improving MIPS dyntrans performance somewhat (only invalidate
		translations if necessary, on writes to the entryhi register,
		instead of doing it for all cop0 writes).
20060618	More cleanup after the removal of the old MIPS emulation.
		Trying to fix the MIPS dyntrans performance bugs/bottlenecks;
		only semi-successful so far (for R3000).
20060620	Minor update to allow clean compilation again on Tru64/Alpha.
20060622	MIPS cleanup and fixes (removing the pc_last stuff, which
		doesn't make sense with dyntrans anyway, and fixing a cross-
		page-delay-slot-with-exception case in end_of_page).
		Removing the old max_random_cycles_per_chunk stuff, and the
		concept of cycles vs instructions for MIPS emulation.
		FINALLY found and fixed the bug which caused NetBSD/pmax
		clocks to behave strangely (it was a load to the zero register,
		which was treated as a NOP; now it is treated as a load to a
		dummy scratch register).
20060623	Increasing the dyntrans chunk size back to
		N_SAFE_DYNTRANS_LIMIT, instead of N_SAFE_DYNTRANS_LIMIT/2.
		Preparing for a quick release, even though there are known
		bugs, and performance for non-R3000 MIPS emulation is very
		poor. :-/
		Reverting to half the dyntrans chunk size again, because
		NetBSD/cats seemed less stable with full size chunks. :(
		NetBSD/sgimips 3.0 can now run :-)  (With release 0.3.8, only
		NetBSD/sgimips 2.1 worked, not 3.0.)

==============  RELEASE 0.4.0  ==============


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

  ViewVC Help
Powered by ViewVC 1.1.26