/[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 32 - (show annotations)
Mon Oct 8 16:20:58 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 24064 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1421 2006/11/06 05:32:37 debug Exp $
20060816	Adding a framework for emulated/virtual timers (src/timer.c),
		using only setitimer().
		Rewriting the mc146818 to use the new timer framework.
20060817	Adding a call to gettimeofday() every now and then (once every
		second, at the moment) to resynch the timer if it drifts.
		Beginning to convert the ISA timer interrupt mechanism (8253
		and 8259) to use the new timer framework.
		Removing the -I command line option.
20060819	Adding the -I command line option again, with new semantics.
		Working on Footbridge timer interrupts; NetBSD/NetWinder and
		NetBSD/CATS now run at correct speed, but unfortunately with
		HUGE delays during bootup.
20060821	Some minor m68k updates. Adding the first instruction: nop. :)
		Minor Alpha emulation updates.
20060822	Adding a FreeBSD development specific YAMON environment
		variable ("khz") (as suggested by Bruce M. Simpson).
		Moving YAMON environment variable initialization from
		machine_evbmips.c into promemul/yamon.c, and adding some more
		variables.
		Continuing on the LCA PCI bus controller (for Alpha machines).
20060823	Continuing on the timer stuff: experimenting with MIPS count/
		compare interrupts connected to the timer framework.
20060825	Adding bogus SCSI commands 0x51 (SCSICDROM_READ_DISCINFO) and
		0x52 (SCSICDROM_READ_TRACKINFO) to the SCSI emulation layer,
		to allow NetBSD/pmax 4.0_BETA to be installed from CDROM.
		Minor updates to the LCA PCI controller.
20060827	Implementing a CHIP8 cpu mode, and a corresponding CHIP8
		machine, for fun. Disassembly support for all instructions,
		and most of the common instructions have been implemented: mvi,
		mov_imm, add_imm, jmp, rand, cls, sprite, skeq_imm, jsr,
		skne_imm, bcd, rts, ldr, str, mov, or, and, xor, add, sub,
		font, ssound, sdelay, gdelay, bogus skup/skpr, skeq, skne.
20060828	Beginning to convert the CHIP8 cpu in the CHIP8 machine to a
		(more correct) RCA 180x cpu. (Disassembly for all 1802
		instructions has been implemented, but no execution yet, and
		no 1805 extended instructions.)
20060829	Minor Alpha emulation updates.
20060830	Beginning to experiment a little with PCI IDE for SGI O2.
		Fixing the cursor key mappings for MobilePro 770 emulation.
		Fixing the LK201 warning caused by recent NetBSD/pmax.
		The MIPS R41xx standby, suspend, and hibernate instructions now
		behave like the RM52xx/MIPS32/MIPS64 wait instruction.
		Fixing dev_wdc so it calculates correct (64-bit) offsets before
		giving them to diskimage_access().
20060831	Continuing on Alpha emulation (OSF1 PALcode).
20060901	Minor Alpha updates; beginning on virtual memory pagetables.
		Removed the limit for max nr of devices (in preparation for
		allowing devices' base addresses to be changed during runtime).
		Adding a hack for MIPS [d]mfc0 select 0 (except the count
		register), so that the coproc register is simply copied.
		The MIPS suspend instruction now exits the emulator, instead
		of being treated as a wait instruction (this causes NetBSD/
		hpcmips to get correct 'halt' behavior).
		The VR41xx RTC now returns correct time.
		Connecting the VR41xx timer to the timer framework (fixed at
		128 Hz, for now).
		Continuing on SPARC emulation, adding more instructions:
		restore, ba_xcc, ble. The rectangle drawing demo works :)
		Removing the last traces of the old ENABLE_CACHE_EMULATION
		MIPS stuff (not usable with dyntrans anyway).
20060902	Splitting up src/net.c into several smaller files in its own
		subdirectory (src/net/).
20060903	Cleanup of the files in src/net/, to make them less ugly.
20060904	Continuing on the 'settings' subsystem.
		Minor progress on the SPARC emulation mode.
20060905	Cleanup of various things, and connecting the settings
		infrastructure to various subsystems (emul, machine, cpu, etc).
		Changing the lk201 mouse update routine to not rely on any
		emulated hardware framebuffer cursor coordinates, but instead
		always do (semi-usable) relative movements.
20060906	Continuing on the lk201 mouse stuff. Mouse behaviour with
		multiple framebuffers (which was working in Ultrix) is now
		semi-broken (but it still works, in a way).
		Moving the documentation about networking into its own file
		(networking.html), and refreshing it a bit. Adding an example
		of how to use ethernet frame direct-access (udp_snoop).
20060907	Continuing on the settings infrastructure.
20060908	Minor updates to SH emulation: for 32-bit emulation: delay
		slots and the 'jsr @Rn' instruction. I'm putting 64-bit SH5 on
		ice, for now.
20060909-10	Implementing some more 32-bit SH instructions. Removing the
		64-bit mode completely. Enough has now been implemented to run
		the rectangle drawing demo. :-)
20060912	Adding more SH instructions.
20060916	Continuing on SH emulation (some more instructions: div0u,
		div1, rotcl/rotcr, more mov instructions, dt, braf, sets, sett,
		tst_imm, dmuls.l, subc, ldc_rm_vbr, movt, clrt, clrs, clrmac).
		Continuing on the settings subsystem (beginning on reading/
		writing settings, removing bugs, and connecting more cpus to
		the framework).
20060919	More work on SH emulation; adding an ldc banked instruction,
		and attaching a 640x480 framebuffer to the Dreamcast machine
		mode (NetBSD/dreamcast prints the NetBSD copyright banner :-),
		and then panics).
20060920	Continuing on the settings subsystem.
20060921	Fixing the Footbridge timer stuff so that NetBSD/cats and
		NetBSD/netwinder boot up without the delays.
20060922	Temporarily hardcoding MIPS timer interrupt to 100 Hz. With
		'wait' support disabled, NetBSD/malta and Linux/malta run at
		correct speed.
20060923	Connecting dev_gt to the timer framework, so that NetBSD/cobalt
		runs at correct speed.
		Moving SH4-specific memory mapped registers into its own
		device (dev_sh4.c).
		Running with -N now prints "idling" instead of bogus nr of
		instrs/second (which isn't valid anyway) while idling.
20060924	Algor emulation should now run at correct speed.
		Adding disassembly support for some MIPS64 revision 2
		instructions: ext, dext, dextm, dextu.
20060926	The timer framework now works also when the MIPS wait
		instruction is used.
20060928	Re-implementing checks for coprocessor availability for MIPS
		cop0 instructions. (Thanks to Carl van Schaik for noticing the
		lack of cop0 availability checks.)
20060929	Implementing an instruction combination hack which treats
		NetBSD/pmax' idle loop as a wait-like instruction.
20060930	The ENTRYHI_R_MASK was missing in (at least) memory_mips_v2p.c,
		causing TLB lookups to sometimes succeed when they should have
		failed. (A big thank you to Juli Mallett for noticing the
		problem.)
		Adding disassembly support for more MIPS64 revision 2 opcodes
		(seb, seh, wsbh, jalr.hb, jr.hb, synci, ins, dins, dinsu,
		dinsm, dsbh, dshd, ror, dror, rorv, drorv, dror32). Also
		implementing seb, seh, dsbh, dshd, and wsbh.
		Implementing an instruction combination hack for Linux/pmax'
		idle loop, similar to the NetBSD/pmax case.
20061001	Changing the NetBSD/sgimips install instructions to extract
		files from an iso image, instead of downloading them via ftp.
20061002	More-than-31-bit userland addresses in memory_mips_v2p.c were
		not actually working; applying a fix from Carl van Schaik to
		enable them to work + making some other updates (adding kuseg
		support).
		Fixing hpcmips (vr41xx) timer initialization.
		Experimenting with O(n)->O(1) reduction in the MIPS TLB lookup
		loop. Seems to work both for R3000 and non-R3000.
20061003	Continuing a little on SH emulation (adding more control
		registers; mini-cleanup of memory_sh.c).
20061004	Beginning on a dev_rtc, a clock/timer device for the test
		machines; also adding a demo, and some documentation.
		Fixing a bug in SH "mov.w @(disp,pc),Rn" (the result wasn't
		sign-extended), and adding the addc and ldtlb instructions.
20061005	Contining on SH emulation: virtual to physical address
		translation, and a skeleton exception mechanism.
20061006	Adding more SH instructions (various loads and stores, rte,
		negc, muls.w, various privileged register-move instructions).
20061007	More SH instructions: various move instructions, trapa, div0s,
		float, fdiv, ftrc.
		Continuing on dev_rtc; removing the rtc demo.
20061008	Adding a dummy Dreamcast PROM module. (Homebrew Dreamcast
		programs using KOS libs need this.)
		Adding more SH instructions: "stc vbr,rn", rotl, rotr, fsca,
		fmul, fadd, various floating-point moves, etc. A 256-byte
		demo for Dreamcast runs :-)
20061012	Adding the SH "lds Rm,pr" and bsr instructions.
20061013	More SH instructions: "sts fpscr,rn", tas.b, and some more
		floating point instructions, cmp/str, and more moves.
		Adding a dummy dev_pvr (Dreamcast graphics controller).
20061014	Generalizing the expression evaluator (used in the built-in
		debugger) to support parentheses and +-*/%^&|.
20061015	Removing the experimental tlb index hint code in
		mips_memory_v2p.c, since it didn't really have any effect.
20061017	Minor SH updates; adding the "sts pr,Rn", fcmp/gt, fneg,
		frchg, and some other instructions. Fixing missing sign-
		extension in an 8-bit load instruction.
20061019	Adding a simple dev_dreamcast_rtc.
		Implementing memory-mapped access to the SH ITLB/UTLB arrays.
20061021	Continuing on various SH and Dreamcast things: sh4 timers,
		debug messages for dev_pvr, fixing some virtual address
		translation bugs, adding the bsrf instruction.
		The NetBSD/dreamcast GENERIC_MD kernel now reaches userland :)
		Adding a dummy dev_dreamcast_asic.c (not really useful yet).
		Implementing simple support for Store Queues.
		Beginning on the PVR Tile Accelerator.
20061022	Generalizing the PVR framebuffer to support off-screen drawing,
		multiple bit-depths, etc. (A small speed penalty, but most
		likely worth it.)
		Adding more SH instructions (mulu.w, fcmp/eq, fsub, fmac,
		fschg, and some more); correcting bugs in "fsca" and "float".
20061024	Adding the SH ftrv (matrix * vector) instruction. Marcus
		Comstedt's "tatest" example runs :) (wireframe only).
		Correcting disassembly for SH floating point instructions that
		use the xd* registers.
		Adding the SH fsts instruction.
		In memory_device_dyntrans_access(), only the currently used
		range is now invalidated, and not the entire device range.
20061025	Adding a dummy AVR32 cpu mode skeleton.
20061026	Various Dreamcast updates; beginning on a Maple bus controller.
20061027	Continuing on the Maple bus. A bogus Controller, Keyboard, and
		Mouse can now be detected by NetBSD and KOS homebrew programs.
		Cleaning up the SH4 Timer Management Unit, and beginning on
		SH4 interrupts.
		Implementing the Dreamcast SYSASIC.
20061028	Continuing on the SYSASIC.
		Adding the SH fsqrt instruction.
		memory_sh.c now actually scans the ITLB.
		Fixing a bug in dev_sh4.c, related to associative writes into
		the memory-mapped UTLB array. NetBSD/dreamcast now reaches
		userland stably, and prints the "Terminal type?" message :-]
		Implementing enough of the Dreamcast keyboard to make NetBSD
		accept it for input.
		Enabling SuperH for stable (non-development) builds.
		Adding NetBSD/dreamcast to the documentation, although it
		doesn't support root-on-nfs yet.
20061029	Changing usleep(1) calls in the debugger to to usleep(10000)
		(according to Brian Foley, this makes GXemul run better on
		MacOS X).
		Making the Maple "Controller" do something (enough to barely
		interact with dcircus.elf).
20061030-31	Some progress on the PVR. More test programs start running (but
		with strange output).
		Various other SH4-related updates.
20061102	Various Dreamcast and SH4 updates; more KOS demos run now.
20061104	Adding a skeleton dev_mb8696x.c (the Dreamcast's LAN adapter).
20061105	Continuing on the MB8696x; NetBSD/dreamcast detects it as mbe0.
		Testing for the release.

==============  RELEASE 0.4.3  ==============


1 /*
2 * Copyright (C) 2003-2006 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.127 2006/10/22 04:20:53 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