/[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 10 - (show annotations)
Mon Oct 8 16:18:27 2007 UTC (11 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 30285 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.815 2005/06/27 23:04:35 debug Exp $
20050617	Experimenting some more with netbooting OpenBSD/sgi. Adding
		a hack which allows emulated ethernet networks to be
		distributed across multiple emulator processes.
20050618	Minor updates (documentation, dummy YAMON emulation, etc).
20050620	strcpy/strcat -> strlcpy/strlcat updates.
		Some more progress on evbmips (Malta).
20050621	Adding a section to doc/configfiles.html about ethernet
		emulation across multiple hosts.
		Beginning the work on the ARM translation engine (using the
		dynamic-but-not-binary translation method).
		Fixing a bintrans bug: 0x9fc00000 should always be treated as
		PROM area, just as 0xbfc00000 is.
		Minor progress on Malta emulation (the PCI-ISA bus).
20050622	NetBSD/evbmips can now be installed (using another emulated
		machine) and run (including userland and so on). :-)
		Spliting up the bintrans haddr_entry field into two (one for
		read, one for write). Probably not much of a speed increase,
		though.
		Updating some NetBSD 2.0 -> 2.0.2 in the documentation.
20050623	Minor updates (documentation, the TODO file, etc).
		gzipped kernels are now always automagically gunzipped when
		loaded.
20050624	Adding a dummy Playstation Portable (PSP) mode, just barely
		enough to run Hello World (in weird colors :-).
		Removing the -b command line option; old bintrans is enabled
		by default instead. It makes more sense.
		Trying to finally fix the non-working performance measurement
		thing (instr/second etc).
20050625	Continuing on the essential basics for ARM emulation. Two
		instructions seem to work, a branch and a simple "mov". (The
		mov arguments are not correct yet.) Performance is definitely
		reasonable.
		Various other minor updates.
		Adding the ARM "bl" instruction.
		Adding support for combining multiple ARM instructions into one
		function call. ("mov" + "mov" is the only one implemented so
		far, but it seems to work.)
		Cleaning up some IP32 interrupt things (crime/mace); disabling
		the PS/2 keyboard controller on IP32, so that NetBSD/sgimips
		boots into userland again.
20050626	Finally! NetBSD/sgimips netboots. Adding instructions to
		doc/guestoses.html on how to set up an nfs server etc.
		Various other minor fixes.
		Playstation Portable ".pbp" files can now be used directly.
		(The ELF part of the .pbp is extracted transparently.)
		Converting some sprintf -> snprintf.
		Adding some more instructions to the ARM disassembler.
20050627	More ARM updates. Adding some simple ldr(b), str(b),
		cmps, and conditional branch instructions, enough to run
		a simple Hello World program.
		All ARM instructions are now inlined/generated for all possible
		condition codes.
		Adding add and sub, and more load/store instructions.
		Removing dummy files: cpu_alpha.c, cpu_hppa.c, and cpu_sparc.c.
		Some minor documentation updates; preparing for a 0.3.4
		release. Updating some URLs.

==============  RELEASE 0.3.4  ==============


1 /*
2 * Copyright (C) 2003-2005 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.99 2005/06/26 13:49:06 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 * HPCmips framebuffer
36 * Playstation 2 (24-bit color)
37 * generic (any resolution, several bit depths possible)
38 *
39 *
40 * TODO: There is still a bug when redrawing the cursor. The underlying
41 * image is moved 1 pixel (?), or something like that.
42 *
43 * TODO: This should actually be independant of X11, but that
44 * might be too hard to do right now.
45 *
46 * TODO: playstation 2 pixels are stored in another format, actually
47 */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "console.h"
54 #include "cpu.h"
55 #include "devices.h"
56 #include "machine.h"
57 #include "memory.h"
58 #include "misc.h"
59 #include "x11.h"
60
61 #ifdef WITH_X11
62 #include <X11/Xlib.h>
63 #include <X11/Xos.h>
64 #include <X11/Xutil.h>
65 #endif
66
67
68 #define FB_TICK_SHIFT 18
69
70 #define LOGO_XSIZE 256
71 #define LOGO_YSIZE 256
72 #define LOGO_BOTTOM_MARGIN 60
73 /* This must be a 256*256 pixels P4 ppm: */
74 #include "fb_logo.c"
75 unsigned char *fb_logo = fb_logo_ppm + 11;
76
77
78 /* #define FB_DEBUG */
79
80 /*
81 * set_grayscale_palette():
82 *
83 * Fill d->rgb_palette with grayscale values. ncolors should
84 * be something like 2, 4, 16, or 256.
85 */
86 void set_grayscale_palette(struct vfb_data *d, int ncolors)
87 {
88 int i, gray;
89
90 for (i=0; i<256; i++) {
91 gray = 255*i/(ncolors-1);
92 d->rgb_palette[i*3 + 0] = gray;
93 d->rgb_palette[i*3 + 1] = gray;
94 d->rgb_palette[i*3 + 2] = gray;
95 }
96 }
97
98
99 /*
100 * set_blackwhite_palette():
101 *
102 * Set color 0 = black, all others to white.
103 */
104 void set_blackwhite_palette(struct vfb_data *d, int ncolors)
105 {
106 int i, gray;
107
108 for (i=0; i<256; i++) {
109 gray = i==0? 0 : 255;
110 d->rgb_palette[i*3 + 0] = gray;
111 d->rgb_palette[i*3 + 1] = gray;
112 d->rgb_palette[i*3 + 2] = gray;
113 }
114 }
115
116
117 /*
118 * dev_fb_resize():
119 *
120 * Resize a framebuffer window. (This functionality is probably a bit buggy,
121 * because I didn't think of including it from the start.)
122 */
123 void dev_fb_resize(struct vfb_data *d, int new_xsize, int new_ysize)
124 {
125 unsigned char *new_framebuffer;
126 int y, new_bytes_per_line;
127 size_t size;
128
129 if (d == NULL) {
130 fatal("dev_fb_resize(): d == NULL\n");
131 return;
132 }
133
134 new_bytes_per_line = new_xsize * d->bit_depth / 8;
135 size = new_ysize * new_bytes_per_line;
136
137 new_framebuffer = malloc(size);
138 if (new_framebuffer == NULL) {
139 fprintf(stderr, "dev_fb_resize(): out of memory\n");
140 exit(1);
141 }
142
143 /* Copy the old framebuffer to the new: */
144 if (d->framebuffer != NULL) {
145 for (y=0; y<new_ysize; y++) {
146 size_t fromofs = d->bytes_per_line * y;
147 size_t toofs = new_bytes_per_line * y;
148 size_t len_to_copy = d->bytes_per_line <
149 new_bytes_per_line? d->bytes_per_line
150 : new_bytes_per_line;
151 memset(new_framebuffer + toofs, 0, new_bytes_per_line);
152 if (y < d->x11_ysize)
153 memmove(new_framebuffer + toofs,
154 d->framebuffer + fromofs, len_to_copy);
155 }
156
157 free(d->framebuffer);
158 }
159
160 d->framebuffer = new_framebuffer;
161 d->framebuffer_size = size;
162
163 if (new_xsize > d->x11_xsize || new_ysize > d->x11_ysize) {
164 d->update_x1 = d->update_y1 = 0;
165 d->update_x2 = new_xsize - 1;
166 d->update_y2 = new_ysize - 1;
167 }
168
169 d->bytes_per_line = new_bytes_per_line;
170 d->x11_xsize = d->visible_xsize = new_xsize;
171 d->x11_ysize = d->visible_ysize = new_ysize;
172
173 #ifdef WITH_X11
174 if (d->fb_window != NULL)
175 x11_fb_resize(d->fb_window, new_xsize, new_ysize);
176 #endif
177 }
178
179
180 /*
181 * dev_fb_setcursor():
182 */
183 void dev_fb_setcursor(struct vfb_data *d, int cursor_x, int cursor_y, int on,
184 int cursor_xsize, int cursor_ysize)
185 {
186 if (cursor_x < 0)
187 cursor_x = 0;
188 if (cursor_y < 0)
189 cursor_y = 0;
190 if (cursor_x + cursor_xsize >= d->xsize)
191 cursor_x = d->xsize - cursor_xsize;
192 if (cursor_y + cursor_ysize >= d->ysize)
193 cursor_y = d->ysize - cursor_ysize;
194
195 #ifdef WITH_X11
196 if (d->fb_window != NULL) {
197 d->fb_window->cursor_x = cursor_x;
198 d->fb_window->cursor_y = cursor_y;
199 d->fb_window->cursor_on = on;
200 d->fb_window->cursor_xsize = cursor_xsize;
201 d->fb_window->cursor_ysize = cursor_ysize;
202 }
203 #endif
204
205 if (d->fb_window != NULL)
206 console_set_framebuffer_mouse(cursor_x, cursor_y,
207 d->fb_window->fb_number);
208
209 /* debug("dev_fb_setcursor(%i,%i, size %i,%i, on=%i)\n",
210 cursor_x, cursor_y, cursor_xsize, cursor_ysize, on); */
211 }
212
213
214 /*
215 * framebuffer_blockcopyfill():
216 *
217 * This function should be used by devices that are capable of doing
218 * block copy/fill.
219 *
220 * If fillflag is non-zero, then fill_[rgb] should contain the color
221 * with which to fill.
222 *
223 * If fillflag is zero, copy mode is used, and from_[xy] should contain
224 * the offset on the framebuffer where we should copy from.
225 *
226 * NOTE: Overlapping copies are undefined!
227 */
228 void framebuffer_blockcopyfill(struct vfb_data *d, int fillflag, int fill_r,
229 int fill_g, int fill_b, int x1, int y1, int x2, int y2,
230 int from_x, int from_y)
231 {
232 int y;
233 long from_ofs, dest_ofs, linelen;
234
235 if (fillflag)
236 debug("framebuffer_blockcopyfill(FILL, %i,%i, %i,%i, "
237 "color %i,%i,%i)\n", x1,y1, x2,y2, fill_r, fill_g, fill_b);
238 else
239 debug("framebuffer_blockcopyfill(COPY, %i,%i, %i,%i, from "
240 "%i,%i)\n", x1,y1, x2,y2, from_x,from_y);
241
242 /* Clip x: */
243 if (x1 < 0) x1 = 0;
244 if (x1 >= d->xsize) x1 = d->xsize-1;
245 if (x2 < 0) x2 = 0;
246 if (x2 >= d->xsize) x2 = d->xsize-1;
247
248 dest_ofs = d->bytes_per_line * y1 + (d->bit_depth/8) * x1;
249 linelen = (x2-x1 + 1) * (d->bit_depth/8);
250 /* NOTE: linelen is nr of bytes, not pixels */
251
252 if (fillflag) {
253 for (y=y1; y<=y2; y++) {
254 if (y>=0 && y<d->ysize) {
255 int x;
256 char buf[8192 * 3];
257 if (d->bit_depth == 24)
258 for (x=0; x<linelen; x+=3) {
259 buf[x] = fill_r;
260 buf[x+1] = fill_g;
261 buf[x+2] = fill_b;
262 }
263 else
264 printf("TODO: fill for non-24-bit"
265 " modes\n");
266
267 memmove(d->framebuffer + dest_ofs, buf,
268 linelen);
269 }
270
271 dest_ofs += d->bytes_per_line;
272 }
273 } else {
274 from_ofs = d->bytes_per_line * from_y +
275 (d->bit_depth/8) * from_x;
276
277 for (y=y1; y<=y2; y++) {
278 if (y>=0 && y<d->ysize)
279 memmove(d->framebuffer + dest_ofs,
280 d->framebuffer + from_ofs, linelen);
281
282 from_ofs += d->bytes_per_line;
283 dest_ofs += d->bytes_per_line;
284 }
285 }
286
287 if (x1 < d->update_x1 || d->update_x1 == -1) d->update_x1 = x1;
288 if (x1 > d->update_x2 || d->update_x2 == -1) d->update_x2 = x1;
289 if (x2 < d->update_x1 || d->update_x1 == -1) d->update_x1 = x2;
290 if (x2 > d->update_x2 || d->update_x2 == -1) d->update_x2 = x2;
291
292 if (y1 < d->update_y1 || d->update_y1 == -1) d->update_y1 = y1;
293 if (y1 > d->update_y2 || d->update_y2 == -1) d->update_y2 = y1;
294 if (y2 < d->update_y1 || d->update_y1 == -1) d->update_y1 = y2;
295 if (y2 > d->update_y2 || d->update_y2 == -1) d->update_y2 = y2;
296 }
297
298
299 #ifdef WITH_X11
300 #define macro_put_pixel() { \
301 /* Combine the color into an X11 long and display it: */ \
302 /* TODO: construct color in a more portable way: */ \
303 switch (d->fb_window->x11_screen_depth) { \
304 case 24: \
305 if (d->fb_window->fb_ximage->byte_order) \
306 color = (b << 16) + (g << 8) + r; \
307 else \
308 color = (r << 16) + (g << 8) + b; \
309 break; \
310 case 16: \
311 r >>= 3; g >>= 2; b >>= 3; \
312 if (d->fb_window->fb_ximage->byte_order) { \
313 /* Big endian 16-bit X server: */ \
314 static int first = 1; \
315 if (first) { \
316 fprintf(stderr, "\n*** Please report to the author whether 16-bit X11 colors are rendered correctly or not!\n\n"); \
317 first = 0; \
318 } \
319 color = (b << 11) + (g << 5) + r; \
320 } else { \
321 /* Little endian (eg PC) X servers: */ \
322 color = (r << 11) + (g << 5) + b; \
323 } \
324 break; \
325 case 15: \
326 r >>= 3; g >>= 3; b >>= 3; \
327 if (d->fb_window->fb_ximage->byte_order) { \
328 /* Big endian 15-bit X server: */ \
329 static int first = 1; \
330 if (first) { \
331 fprintf(stderr, "\n*** Please report to the author whether 15-bit X11 colors are rendered correctly or not!\n\n"); \
332 first = 0; \
333 } \
334 color = (b << 10) + (g << 5) + r; \
335 } else { \
336 /* Little endian (eg PC) X servers: */ \
337 color = (r << 10) + (g << 5) + b; \
338 } \
339 break; \
340 default: \
341 color = d->fb_window->x11_graycolor[15 * (r + g + b) / (255 * 3)].pixel; \
342 } \
343 if (x>=0 && x<d->x11_xsize && y>=0 && y<d->x11_ysize) \
344 XPutPixel(d->fb_window->fb_ximage, x, y, color); \
345 }
346 #else
347 /* If not WITH_X11: */
348 #define macro_put_pixel() { }
349 #endif
350
351
352 /*
353 * update_framebuffer():
354 *
355 * The framebuffer memory has been updated. This function tries to make
356 * sure that the XImage is also updated (1 or more pixels).
357 */
358 void update_framebuffer(struct vfb_data *d, int addr, int len)
359 {
360 int x, y, pixel, npixels;
361 long color_r, color_g, color_b;
362 #ifdef WITH_X11
363 long color;
364 #endif
365 int scaledown = d->vfb_scaledown;
366 int scaledownXscaledown = 1;
367
368 if (scaledown == 1) {
369 /* Which framebuffer pixel does addr correspond to? */
370 pixel = addr * 8 / d->bit_depth;
371 y = pixel / d->xsize;
372 x = pixel % d->xsize;
373
374 /* How many framebuffer pixels? */
375 npixels = len * 8 / d->bit_depth;
376 if (npixels == 0)
377 npixels = 1;
378
379 if (d->bit_depth < 8) {
380 for (pixel=0; pixel<npixels; pixel++) {
381 int fb_addr, c, r, g, b;
382 color_r = color_g = color_b = 0;
383
384 fb_addr = (y * d->xsize + x) * d->bit_depth;
385 /* fb_addr is now which _bit_ in
386 the framebuffer */
387
388 c = d->framebuffer[fb_addr >> 3];
389 fb_addr &= 7;
390
391 /* HPCmips is reverse: */
392 if (d->vfb_type == VFB_HPCMIPS)
393 fb_addr = 8 - d->bit_depth - fb_addr;
394
395 c = (c >> fb_addr) & ((1<<d->bit_depth) - 1);
396 /* c <<= (8 - d->bit_depth); */
397
398 r = d->rgb_palette[c*3 + 0];
399 g = d->rgb_palette[c*3 + 1];
400 b = d->rgb_palette[c*3 + 2];
401
402 macro_put_pixel();
403 x++;
404 }
405 } else if (d->bit_depth == 8) {
406 for (pixel=0; pixel<npixels; pixel++) {
407 int fb_addr, c, r, g, b;
408 color_r = color_g = color_b = 0;
409
410 fb_addr = y * d->xsize + x;
411 /* fb_addr is now which byte in framebuffer */
412 c = d->framebuffer[fb_addr];
413 r = d->rgb_palette[c*3 + 0];
414 g = d->rgb_palette[c*3 + 1];
415 b = d->rgb_palette[c*3 + 2];
416
417 macro_put_pixel();
418 x++;
419 }
420 } else { /* d->bit_depth > 8 */
421 for (pixel=0; pixel<npixels; pixel++) {
422 int fb_addr, r, g, b;
423 color_r = color_g = color_b = 0;
424
425 fb_addr = (y * d->xsize + x) * d->bit_depth;
426 /* fb_addr is now which byte in framebuffer */
427
428 /* > 8 bits color. */
429 fb_addr >>= 3;
430 switch (d->bit_depth) {
431 case 24:
432 r = d->framebuffer[fb_addr];
433 g = d->framebuffer[fb_addr + 1];
434 b = d->framebuffer[fb_addr + 2];
435 break;
436 /* TODO: copy to the scaledown code below */
437 case 16:
438 if (d->vfb_type == VFB_HPCMIPS) {
439 b = d->framebuffer[fb_addr] +
440 (d->framebuffer[fb_addr+1] << 8);
441
442 if (d->color32k) {
443 r = b >> 11;
444 g = b >> 5;
445 r = r & 31;
446 g = (g & 31) * 2;
447 b = b & 31;
448 } else if (d->psp_15bit) {
449 int tmp;
450 r = (b >> 10) & 0x1f;
451 g = (b >> 5) & 0x1f;
452 b = b & 0x1f;
453 g <<= 1;
454 tmp = r; r = b; b = tmp;
455 } else {
456 r = (b >> 11) & 0x1f;
457 g = (b >> 5) & 0x3f;
458 b = b & 0x1f;
459 }
460 } else {
461 r = d->framebuffer[fb_addr] >> 3;
462 g = (d->framebuffer[fb_addr] << 5) +
463 (d->framebuffer[fb_addr + 1] >> 5);
464 b = d->framebuffer[fb_addr + 1] & 0x1f;
465 }
466
467 r *= 8;
468 g *= 4;
469 b *= 8;
470 break;
471 default:
472 r = g = b = random() & 255;
473 }
474
475 macro_put_pixel();
476 x++;
477 }
478 }
479
480 return;
481 }
482
483 /* scaledown != 1: */
484
485 scaledown = d->vfb_scaledown;
486 scaledownXscaledown = scaledown * scaledown;
487
488 /* Which framebuffer pixel does addr correspond to? */
489 pixel = addr * 8 / d->bit_depth;
490 y = pixel / d->xsize;
491 x = pixel % d->xsize;
492
493 /* How many framebuffer pixels? */
494 npixels = len * 8 / d->bit_depth;
495
496 /* Which x11 pixel? */
497 x /= scaledown;
498 y /= scaledown;
499
500 /* How many x11 pixels: */
501 npixels /= scaledown;
502 if (npixels == 0)
503 npixels = 1;
504
505 if (d->bit_depth < 8) {
506 for (pixel=0; pixel<npixels; pixel++) {
507 int subx, suby, r, g, b;
508 color_r = color_g = color_b = 0;
509 for (suby=0; suby<scaledown; suby++)
510 for (subx=0; subx<scaledown; subx++) {
511 int fb_x, fb_y, fb_addr, c;
512
513 fb_x = x * scaledown + subx;
514 fb_y = y * scaledown + suby;
515 fb_addr = fb_y * d->xsize + fb_x;
516 fb_addr = fb_addr * d->bit_depth;
517 /* fb_addr is now which _bit_ in
518 the framebuffer */
519
520 c = d->framebuffer[fb_addr >> 3];
521 fb_addr &= 7;
522
523 /* HPCmips is reverse: */
524 if (d->vfb_type == VFB_HPCMIPS)
525 fb_addr = 8 - d->bit_depth - fb_addr;
526
527 c = (c >> fb_addr) & ((1<<d->bit_depth) - 1);
528 /* c <<= (8 - d->bit_depth); */
529
530 r = d->rgb_palette[c*3 + 0];
531 g = d->rgb_palette[c*3 + 1];
532 b = d->rgb_palette[c*3 + 2];
533
534 color_r += r;
535 color_g += g;
536 color_b += b;
537 }
538
539 r = color_r / scaledownXscaledown;
540 g = color_g / scaledownXscaledown;
541 b = color_b / scaledownXscaledown;
542 macro_put_pixel();
543 x++;
544 }
545 } else if (d->bit_depth == 8) {
546 for (pixel=0; pixel<npixels; pixel++) {
547 int subx, suby, r, g, b;
548 color_r = color_g = color_b = 0;
549 for (suby=0; suby<scaledown; suby++)
550 for (subx=0; subx<scaledown; subx++) {
551 int fb_x, fb_y, fb_addr, c;
552
553 fb_x = x * scaledown + subx;
554 fb_y = y * scaledown + suby;
555 fb_addr = fb_y * d->xsize + fb_x;
556 /* fb_addr is which _byte_ in framebuffer */
557 c = d->framebuffer[fb_addr] * 3;
558 r = d->rgb_palette[c + 0];
559 g = d->rgb_palette[c + 1];
560 b = d->rgb_palette[c + 2];
561 color_r += r;
562 color_g += g;
563 color_b += b;
564 }
565
566 r = color_r / scaledownXscaledown;
567 g = color_g / scaledownXscaledown;
568 b = color_b / scaledownXscaledown;
569 macro_put_pixel();
570 x++;
571 }
572 } else {
573 /* Generic > 8 bit bit-depth: */
574 for (pixel=0; pixel<npixels; pixel++) {
575 int subx, suby, r, g, b;
576 color_r = color_g = color_b = 0;
577 for (suby=0; suby<scaledown; suby++)
578 for (subx=0; subx<scaledown; subx++) {
579 int fb_x, fb_y, fb_addr;
580
581 fb_x = x * scaledown + subx;
582 fb_y = y * scaledown + suby;
583 fb_addr = fb_y * d->xsize + fb_x;
584 fb_addr = (fb_addr * d->bit_depth) >> 3;
585 /* fb_addr is which _byte_ in framebuffer */
586
587 /* > 8 bits color. */
588 switch (d->bit_depth) {
589 case 24:
590 r = d->framebuffer[fb_addr];
591 g = d->framebuffer[fb_addr + 1];
592 b = d->framebuffer[fb_addr + 2];
593 break;
594 default:
595 r = g = b = random() & 255;
596 }
597 color_r += r;
598 color_g += g;
599 color_b += b;
600 }
601 r = color_r / scaledownXscaledown;
602 g = color_g / scaledownXscaledown;
603 b = color_b / scaledownXscaledown;
604 macro_put_pixel();
605 x++;
606 }
607 }
608 }
609
610
611 /*
612 * dev_fb_tick():
613 *
614 */
615 void dev_fb_tick(struct cpu *cpu, void *extra)
616 {
617 struct vfb_data *d = extra;
618 #ifdef WITH_X11
619 int need_to_flush_x11 = 0;
620 int need_to_redraw_cursor = 0;
621 #endif
622
623 if (!cpu->machine->use_x11)
624 return;
625
626 #ifdef BINTRANS
627 do {
628 uint64_t low = -1, high;
629 int x, y;
630
631 memory_device_bintrans_access(cpu, cpu->mem,
632 extra, &low, &high);
633 if ((int64_t)low == -1)
634 break;
635
636 /* printf("low=%016llx high=%016llx\n",
637 (long long)low, (long long)high); */
638
639 x = (low % d->bytes_per_line) * 8 / d->bit_depth;
640 y = low / d->bytes_per_line;
641 if (x < d->update_x1 || d->update_x1 == -1)
642 d->update_x1 = x;
643 if (x > d->update_x2 || d->update_x2 == -1)
644 d->update_x2 = x;
645 if (y < d->update_y1 || d->update_y1 == -1)
646 d->update_y1 = y;
647 if (y > d->update_y2 || d->update_y2 == -1)
648 d->update_y2 = y;
649
650 x = ((low+7) % d->bytes_per_line) * 8 / d->bit_depth;
651 y = (low+7) / d->bytes_per_line;
652 if (x < d->update_x1 || d->update_x1 == -1)
653 d->update_x1 = x;
654 if (x > d->update_x2 || d->update_x2 == -1)
655 d->update_x2 = x;
656 if (y < d->update_y1 || d->update_y1 == -1)
657 d->update_y1 = y;
658 if (y > d->update_y2 || d->update_y2 == -1)
659 d->update_y2 = y;
660
661 x = (high % d->bytes_per_line) * 8 / d->bit_depth;
662 y = high / d->bytes_per_line;
663 if (x < d->update_x1 || d->update_x1 == -1)
664 d->update_x1 = x;
665 if (x > d->update_x2 || d->update_x2 == -1)
666 d->update_x2 = x;
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 x = ((high+7) % d->bytes_per_line) * 8 / d->bit_depth;
673 y = (high+7) / d->bytes_per_line;
674 if (x < d->update_x1 || d->update_x1 == -1)
675 d->update_x1 = x;
676 if (x > d->update_x2 || d->update_x2 == -1)
677 d->update_x2 = x;
678 if (y < d->update_y1 || d->update_y1 == -1)
679 d->update_y1 = y;
680 if (y > d->update_y2 || d->update_y2 == -1)
681 d->update_y2 = y;
682
683 /*
684 * An update covering more than one line will automatically
685 * force an update of all the affected lines:
686 */
687 if (d->update_y1 != d->update_y2) {
688 d->update_x1 = 0;
689 d->update_x2 = d->xsize-1;
690 }
691 } while (0);
692 #endif
693
694 #ifdef WITH_X11
695 /* Do we need to redraw the cursor? */
696 if (d->fb_window->cursor_on != d->fb_window->OLD_cursor_on ||
697 d->fb_window->cursor_x != d->fb_window->OLD_cursor_x ||
698 d->fb_window->cursor_y != d->fb_window->OLD_cursor_y ||
699 d->fb_window->cursor_xsize != d->fb_window->OLD_cursor_xsize ||
700 d->fb_window->cursor_ysize != d->fb_window->OLD_cursor_ysize)
701 need_to_redraw_cursor = 1;
702
703 if (d->update_x2 != -1) {
704 if ( (d->update_x1 >= d->fb_window->OLD_cursor_x &&
705 d->update_x1 < (d->fb_window->OLD_cursor_x + d->fb_window->OLD_cursor_xsize)) ||
706 (d->update_x2 >= d->fb_window->OLD_cursor_x &&
707 d->update_x2 < (d->fb_window->OLD_cursor_x + d->fb_window->OLD_cursor_xsize)) ||
708 (d->update_x1 < d->fb_window->OLD_cursor_x &&
709 d->update_x2 >= (d->fb_window->OLD_cursor_x + d->fb_window->OLD_cursor_xsize)) ) {
710 if ( (d->update_y1 >= d->fb_window->OLD_cursor_y &&
711 d->update_y1 < (d->fb_window->OLD_cursor_y + d->fb_window->OLD_cursor_ysize)) ||
712 (d->update_y2 >= d->fb_window->OLD_cursor_y &&
713 d->update_y2 < (d->fb_window->OLD_cursor_y + d->fb_window->OLD_cursor_ysize)) ||
714 (d->update_y1 < d->fb_window->OLD_cursor_y &&
715 d->update_y2 >= (d->fb_window->OLD_cursor_y + d->fb_window->OLD_cursor_ysize)) )
716 need_to_redraw_cursor = 1;
717 }
718 }
719
720 if (need_to_redraw_cursor) {
721 /* Remove old cursor, if any: */
722 if (d->fb_window->OLD_cursor_on) {
723 XPutImage(d->fb_window->x11_display,
724 d->fb_window->x11_fb_window,
725 d->fb_window->x11_fb_gc, d->fb_window->fb_ximage,
726 d->fb_window->OLD_cursor_x/d->vfb_scaledown,
727 d->fb_window->OLD_cursor_y/d->vfb_scaledown,
728 d->fb_window->OLD_cursor_x/d->vfb_scaledown,
729 d->fb_window->OLD_cursor_y/d->vfb_scaledown,
730 d->fb_window->OLD_cursor_xsize/d->vfb_scaledown + 1,
731 d->fb_window->OLD_cursor_ysize/d->vfb_scaledown + 1);
732 }
733 }
734 #endif
735
736 if (d->update_x2 != -1) {
737 int y, addr, addr2, q = d->vfb_scaledown;
738
739 if (d->update_x1 >= d->visible_xsize) d->update_x1 = d->visible_xsize - 1;
740 if (d->update_x2 >= d->visible_xsize) d->update_x2 = d->visible_xsize - 1;
741 if (d->update_y1 >= d->visible_ysize) d->update_y1 = d->visible_ysize - 1;
742 if (d->update_y2 >= d->visible_ysize) d->update_y2 = d->visible_ysize - 1;
743
744 /* Without these, we might miss the right most / bottom pixel: */
745 d->update_x2 += (q - 1);
746 d->update_y2 += (q - 1);
747
748 d->update_x1 = d->update_x1 / q * q;
749 d->update_x2 = d->update_x2 / q * q;
750 d->update_y1 = d->update_y1 / q * q;
751 d->update_y2 = d->update_y2 / q * q;
752
753 addr = d->update_y1 * d->bytes_per_line + d->update_x1 * d->bit_depth / 8;
754 addr2 = d->update_y1 * d->bytes_per_line + d->update_x2 * d->bit_depth / 8;
755
756 for (y=d->update_y1; y<=d->update_y2; y+=q) {
757 update_framebuffer(d, addr, addr2 - addr);
758 addr += d->bytes_per_line * q;
759 addr2 += d->bytes_per_line * q;
760 }
761
762 #ifdef WITH_X11
763 XPutImage(d->fb_window->x11_display, d->fb_window->x11_fb_window, d->fb_window->x11_fb_gc, d->fb_window->fb_ximage,
764 d->update_x1/d->vfb_scaledown, d->update_y1/d->vfb_scaledown,
765 d->update_x1/d->vfb_scaledown, d->update_y1/d->vfb_scaledown,
766 (d->update_x2 - d->update_x1)/d->vfb_scaledown + 1,
767 (d->update_y2 - d->update_y1)/d->vfb_scaledown + 1);
768
769 need_to_flush_x11 = 1;
770 #endif
771
772 d->update_x1 = d->update_y1 = 99999;
773 d->update_x2 = d->update_y2 = -1;
774 }
775
776 #ifdef WITH_X11
777 if (need_to_redraw_cursor) {
778 /* Paint new cursor: */
779 if (d->fb_window->cursor_on) {
780 x11_redraw_cursor(cpu->machine, d->fb_window->fb_number);
781 d->fb_window->OLD_cursor_on = d->fb_window->cursor_on;
782 d->fb_window->OLD_cursor_x = d->fb_window->cursor_x;
783 d->fb_window->OLD_cursor_y = d->fb_window->cursor_y;
784 d->fb_window->OLD_cursor_xsize = d->fb_window->cursor_xsize;
785 d->fb_window->OLD_cursor_ysize = d->fb_window->cursor_ysize;
786 }
787 }
788 #endif
789
790 #ifdef WITH_X11
791 if (need_to_flush_x11)
792 XFlush(d->fb_window->x11_display);
793 #endif
794 }
795
796
797 /*
798 * dev_fb_access():
799 */
800 int dev_fb_access(struct cpu *cpu, struct memory *mem,
801 uint64_t relative_addr, unsigned char *data, size_t len,
802 int writeflag, void *extra)
803 {
804 struct vfb_data *d = extra;
805 int i;
806
807 #ifdef FB_DEBUG
808 if (writeflag == MEM_WRITE) { if (data[0]) {
809 fatal("[ dev_fb: write to addr=%08lx, data = ",
810 (long)relative_addr);
811 for (i=0; i<len; i++)
812 fatal("%02x ", data[i]);
813 fatal("]\n");
814 } else {
815 fatal("[ dev_fb: read from addr=%08lx, data = ",
816 (long)relative_addr);
817 for (i=0; i<len; i++)
818 fatal("%02x ", d->framebuffer[relative_addr + i]);
819 fatal("]\n");
820 }
821 #endif
822
823 if (relative_addr >= d->framebuffer_size)
824 return 0;
825
826 /* See if a write actually modifies the framebuffer contents: */
827 if (writeflag == MEM_WRITE) {
828 for (i=0; i<len; i++) {
829 if (data[i] != d->framebuffer[relative_addr + i])
830 break;
831
832 /* If all bytes are equal to what is already stored
833 in the framebuffer, then simply return: */
834 if (i==len-1)
835 return 1;
836 }
837 }
838
839 /*
840 * If the framebuffer is modified, then we should keep a track
841 * of which area(s) we modify, so that the display isn't updated
842 * unnecessarily.
843 */
844 if (writeflag == MEM_WRITE && cpu->machine->use_x11) {
845 int x, y, x2,y2;
846
847 x = (relative_addr % d->bytes_per_line) * 8 / d->bit_depth;
848 y = relative_addr / d->bytes_per_line;
849 x2 = ((relative_addr + len) % d->bytes_per_line)
850 * 8 / d->bit_depth;
851 y2 = (relative_addr + len) / d->bytes_per_line;
852
853 if (x < d->update_x1 || d->update_x1 == -1)
854 d->update_x1 = x;
855 if (x > d->update_x2 || d->update_x2 == -1)
856 d->update_x2 = x;
857
858 if (y < d->update_y1 || d->update_y1 == -1)
859 d->update_y1 = y;
860 if (y > d->update_y2 || d->update_y2 == -1)
861 d->update_y2 = y;
862
863 if (x2 < d->update_x1 || d->update_x1 == -1)
864 d->update_x1 = x2;
865 if (x2 > d->update_x2 || d->update_x2 == -1)
866 d->update_x2 = x2;
867
868 if (y2 < d->update_y1 || d->update_y1 == -1)
869 d->update_y1 = y2;
870 if (y2 > d->update_y2 || d->update_y2 == -1)
871 d->update_y2 = y2;
872
873 /*
874 * An update covering more than one line will automatically
875 * force an update of all the affected lines:
876 */
877 if (y != y2) {
878 d->update_x1 = 0;
879 d->update_x2 = d->xsize-1;
880 }
881 }
882
883 /*
884 * Read from/write to the framebuffer:
885 * (TODO: take the color_plane_mask into account)
886 *
887 * Calling memcpy() is probably overkill, as it usually is just one
888 * or a few bytes that are read/written at a time.
889 */
890 if (writeflag == MEM_WRITE) {
891 if (len > 8)
892 memcpy(d->framebuffer + relative_addr, data, len);
893 else
894 for (i=0; i<len; i++)
895 d->framebuffer[relative_addr + i] = data[i];
896 } else {
897 if (len > 8)
898 memcpy(data, d->framebuffer + relative_addr, len);
899 else
900 for (i=0; i<len; i++)
901 data[i] = d->framebuffer[relative_addr + i];
902 }
903
904 return 1;
905 }
906
907
908 /*
909 * dev_fb_init():
910 *
911 * This function is big and ugly, but the point is to initialize a framebuffer
912 * device. :-)
913 *
914 * visible_xsize and visible_ysize are the sizes of the visible display area.
915 * xsize and ysize tell how much memory is actually allocated (for example
916 * visible_xsize could be 640, but xsize could be 1024, for better alignment).
917 *
918 * vfb_type is useful for selecting special features.
919 *
920 * type = VFB_GENERIC is the most useful type, especially when bit_depth = 24.
921 *
922 * VFB_DEC_VFB01, _VFB02, and VFB_DEC_MAXINE are DECstation specific.
923 *
924 * If type is VFB_HPCMIPS, then color encoding differs from the generic case.
925 *
926 * If bit_depth = -15 (note the minus sign), then a special hack is used for
927 * the Playstation Portable's 5-bit R, 5-bit G, 5-bit B.
928 */
929 struct vfb_data *dev_fb_init(struct machine *machine, struct memory *mem,
930 uint64_t baseaddr, int vfb_type, int visible_xsize, int visible_ysize,
931 int xsize, int ysize, int bit_depth, char *name, int logo)
932 {
933 struct vfb_data *d;
934 size_t size, nlen;
935 int x, y, flags;
936 char title[400];
937 char *name2;
938
939 d = malloc(sizeof(struct vfb_data));
940 if (d == NULL) {
941 fprintf(stderr, "out of memory\n");
942 exit(1);
943 }
944 memset(d, 0, sizeof(struct vfb_data));
945
946 d->vfb_type = vfb_type;
947
948 /* Defaults: */
949 d->xsize = xsize; d->visible_xsize = visible_xsize;
950 d->ysize = ysize; d->visible_ysize = visible_ysize;
951
952 d->bit_depth = bit_depth;
953
954 if (bit_depth == 15) {
955 d->color32k = 1;
956 bit_depth = d->bit_depth = 16;
957 } else if (bit_depth == -15) {
958 d->psp_15bit = 1;
959 bit_depth = d->bit_depth = 16;
960 }
961
962 /* Specific types: */
963 switch (vfb_type) {
964 case VFB_DEC_VFB01:
965 /* DECstation VFB01 (monochrome) */
966 d->xsize = 2048; d->visible_xsize = 1024;
967 d->ysize = 1024; d->visible_ysize = 864;
968 d->bit_depth = 1;
969 break;
970 case VFB_DEC_VFB02:
971 /* DECstation VFB02 (color) */
972 d->xsize = 1024; d->visible_xsize = 1024;
973 d->ysize = 1024; d->visible_ysize = 864;
974 d->bit_depth = 8;
975 break;
976 case VFB_DEC_MAXINE:
977 /* DECstation Maxine (1024x768x8) */
978 d->xsize = 1024; d->visible_xsize = d->xsize;
979 d->ysize = 768; d->visible_ysize = d->ysize;
980 d->bit_depth = 8;
981 break;
982 case VFB_PLAYSTATION2:
983 /* Playstation 2 */
984 d->xsize = xsize; d->visible_xsize = d->xsize;
985 d->ysize = ysize; d->visible_ysize = d->ysize;
986 d->bit_depth = 24;
987 break;
988 default:
989 ;
990 }
991
992 if (d->bit_depth == 2 || d->bit_depth == 4)
993 set_grayscale_palette(d, 1 << d->bit_depth);
994 else if (d->bit_depth == 8 || d->bit_depth == 1)
995 set_blackwhite_palette(d, 1 << d->bit_depth);
996
997 d->vfb_scaledown = machine->x11_scaledown;
998
999 d->bytes_per_line = d->xsize * d->bit_depth / 8;
1000 size = d->ysize * d->bytes_per_line;
1001
1002 d->framebuffer = malloc(size);
1003 if (d->framebuffer == NULL) {
1004 fprintf(stderr, "out of memory\n");
1005 exit(1);
1006 }
1007
1008 /* Clear the framebuffer (all black pixels): */
1009 d->framebuffer_size = size;
1010 memset(d->framebuffer, 0, size);
1011
1012 d->x11_xsize = d->visible_xsize / d->vfb_scaledown;
1013 d->x11_ysize = d->visible_ysize / d->vfb_scaledown;
1014
1015 d->update_x1 = d->update_y1 = 99999;
1016 d->update_x2 = d->update_y2 = -1;
1017
1018
1019 /* A nice bootup logo: */
1020 if (logo) {
1021 int logo_bottom_margin = LOGO_BOTTOM_MARGIN;
1022
1023 d->update_x1 = 0;
1024 d->update_x2 = LOGO_XSIZE-1;
1025 d->update_y1 = d->visible_ysize-LOGO_YSIZE-logo_bottom_margin;
1026 d->update_y2 = d->visible_ysize-logo_bottom_margin;
1027 for (y=0; y<LOGO_YSIZE; y++)
1028 for (x=0; x<LOGO_XSIZE; x++) {
1029 int s, a = ((y + d->visible_ysize - LOGO_YSIZE
1030 - logo_bottom_margin)*d->xsize + x)
1031 * d->bit_depth / 8;
1032 int b = fb_logo[(y*LOGO_XSIZE+x) / 8] &
1033 (128 >> (x&7));
1034 for (s=0; s<d->bit_depth / 8; s++)
1035 if (a+s >= 0 && a+s < size)
1036 d->framebuffer[a+s] = b? 0:255;
1037 }
1038 }
1039
1040 /* Don't set the title to include the size of the framebuffer for
1041 VGA, since then the resolution might change during runtime. */
1042 if (strcmp(name, "VGA") == 0)
1043 snprintf(title, sizeof(title),"GXemul: %s framebuffer", name);
1044 else
1045 snprintf(title, sizeof(title),"GXemul: %ix%ix%i %s framebuffer",
1046 d->visible_xsize, d->visible_ysize, d->bit_depth, name);
1047 title[sizeof(title)-1] = '\0';
1048
1049 #ifdef WITH_X11
1050 if (machine->use_x11)
1051 d->fb_window = x11_fb_init(d->x11_xsize, d->x11_ysize,
1052 title, machine->x11_scaledown, machine);
1053 else
1054 #endif
1055 d->fb_window = NULL;
1056
1057 nlen = strlen(name) + 10;
1058 name2 = malloc(nlen);
1059 if (name2 == NULL) {
1060 fprintf(stderr, "out of memory in dev_fb_init()\n");
1061 exit(1);
1062 }
1063 snprintf(name2, nlen, "fb [%s]", name);
1064
1065 flags = MEM_DEFAULT;
1066 if ((baseaddr & 0xfff) == 0)
1067 flags = MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK;
1068
1069 flags |= MEM_READING_HAS_NO_SIDE_EFFECTS;
1070
1071 memory_device_register(mem, name2, baseaddr, size, dev_fb_access,
1072 d, flags, d->framebuffer);
1073
1074 machine_add_tickfunction(machine, dev_fb_tick, d, FB_TICK_SHIFT);
1075 return d;
1076 }
1077

  ViewVC Help
Powered by ViewVC 1.1.26