ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_vosf.h
Revision: 1.7
Committed: 2000-10-13T16:47:52Z (24 years, 1 month ago) by cebix
Content type: text/plain
Branch: MAIN
Changes since 1.6: +7 -2 lines
Log Message:
- replaced floating-point page shift calculation by integer routine, fixing
  the VOSF problems under NetBSD/m68k
- fixed off-by-7 error in 1-bit window update routines

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * video_vosf.h - Video/graphics emulation, video on SEGV signals support
3     *
4     * Basilisk II (C) 1997-2000 Christian Bauer
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #ifndef VIDEO_VOSF_H
22     #define VIDEO_VOSF_H
23    
24     // Note: this file is #include'd in video_x.cpp
25     #ifdef ENABLE_VOSF
26    
27     /*
28     * Page-aligned memory allocation
29     */
30    
31     // Align on page boundaries
32 gbeauche 1.4 static uintptr align_on_page_boundary(uintptr size)
33 gbeauche 1.1 {
34     const uint32 page_size = getpagesize();
35     const uint32 page_mask = page_size - 1;
36     return (size + page_mask) & ~page_mask;
37     }
38    
39     // Allocate memory on page boundary
40     static void * allocate_framebuffer(uint32 size, uint8 * hint = 0)
41     {
42     // Remind that the system can allocate at 0x00000000...
43     return mmap((caddr_t)hint, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
44     }
45    
46    
47     /*
48     * Screen depth identification
49     */
50    
51     enum {
52     ID_DEPTH_UNKNOWN = -1,
53     ID_DEPTH_1,
54     ID_DEPTH_8,
55     ID_DEPTH_15,
56     ID_DEPTH_16,
57     ID_DEPTH_24,
58     ID_DEPTH_32 = ID_DEPTH_24,
59     ID_DEPTH_COUNT
60     };
61    
62     static int depth_id(int depth)
63     {
64     int id;
65     switch (depth) {
66     case 1 : id = ID_DEPTH_1; break;
67     case 8 : id = ID_DEPTH_8; break;
68     case 15 : id = ID_DEPTH_15; break;
69     case 16 : id = ID_DEPTH_16; break;
70     case 24 : id = ID_DEPTH_24; break;
71     case 32 : id = ID_DEPTH_32; break;
72     default : id = ID_DEPTH_UNKNOWN;
73     }
74     return id;
75     }
76    
77    
78     /*
79     * Frame buffer copy function templates
80     */
81    
82     // No conversion required
83    
84     #define MEMCPY_PROFITABLE
85     #ifdef MEMCPY_PROFITABLE
86     static void do_fbcopy_raw(uint8 * dest, const uint8 * source, uint32 length)
87     {
88     memcpy(dest, source, length);
89     }
90     #else
91     #define FB_BLIT_1(dst, src) (dst = (src))
92     #define FB_BLIT_2(dst, src) (dst = (src))
93 gbeauche 1.2 #define FB_DEPTH 0
94 gbeauche 1.1 #define FB_FUNC_NAME do_fbcopy_raw
95     #include "video_blit.h"
96     #endif
97    
98    
99     // RGB 555
100    
101 gbeauche 1.2 #ifdef WORDS_BIGENDIAN
102     # define FB_FUNC_NAME do_fbcopy_15_obo
103     #else
104     # define FB_FUNC_NAME do_fbcopy_15_nbo
105     #endif
106    
107 gbeauche 1.1 #define FB_BLIT_1(dst, src) \
108     (dst = (((src) >> 8) & 0xff) | (((src) & 0xff) << 8))
109    
110     #define FB_BLIT_2(dst, src) \
111     (dst = (((src) >> 8) & 0x00ff00ff) | (((src) & 0x00ff00ff) << 8))
112    
113     #define FB_DEPTH 15
114     #include "video_blit.h"
115    
116    
117     // RGB 565
118    
119 gbeauche 1.2 #ifdef WORDS_BIGENDIAN
120    
121     // native byte order
122    
123     #define FB_BLIT_1(dst, src) \
124     (dst = (((src) & 0x1f) | (((src) << 1) & 0xffc0)))
125    
126     #define FB_BLIT_2(dst, src) \
127     (dst = (((src) & 0x001f001f) | (((src) << 1) & 0xffc0ffc0)))
128    
129     #define FB_DEPTH 16
130     #define FB_FUNC_NAME do_fbcopy_16_nbo
131     #include "video_blit.h"
132    
133     // opposite byte order (untested)
134    
135     #define FB_BLIT_1(dst, src) \
136     (dst = ((((src) >> 6) & 0xff) | (((src) & 0x60) << 9)))
137    
138     #define FB_BLIT_2(dst, src) \
139     (dst = ((((src) >> 6) & 0x00ff00ff) | (((src) & 0x00600060) << 9)))
140    
141     #define FB_DEPTH 16
142     #define FB_FUNC_NAME do_fbcopy_16_obo
143     #include "video_blit.h"
144    
145     #else
146    
147     // native byte order
148    
149 gbeauche 1.1 #define FB_BLIT_1(dst, src) \
150     (dst = (((src) >> 8) & 0x001f) | (((src) << 9) & 0xfe00) | (((src) >> 7) & 0x01c0))
151    
152     #define FB_BLIT_2(dst, src) \
153     (dst = (((src) >> 8) & 0x001f001f) | (((src) << 9) & 0xfe00fe00) | (((src) >> 7) & 0x01c001c0))
154    
155     #define FB_DEPTH 16
156 gbeauche 1.2 #define FB_FUNC_NAME do_fbcopy_16_nbo
157 gbeauche 1.1 #include "video_blit.h"
158    
159 gbeauche 1.2 // opposite byte order (untested)
160    
161     #define FB_BLIT_1(dst, src) \
162     (dst = (((src) & 0x1f00) | (((src) << 1) & 0xe0fe) | (((src) >> 15) & 1)))
163    
164     #define FB_BLIT_2(dst, src) \
165     (dst = (((src) & 0x1f001f00) | (((src) << 1) & 0xe0fee0fe) | (((src) >> 15) & 0x10001)))
166    
167     #define FB_DEPTH 16
168     #define FB_FUNC_NAME do_fbcopy_16_obo
169     #include "video_blit.h"
170    
171     #endif
172 gbeauche 1.1
173     // RGB 888
174    
175 gbeauche 1.2 #ifdef WORDS_BIGENDIAN
176     # define FB_FUNC_NAME do_fbcopy_24_obo
177     #else
178     # define FB_FUNC_NAME do_fbcopy_24_nbo
179     #endif
180    
181 gbeauche 1.1 #define FB_BLIT_1(dst, src) \
182     (dst = (src))
183    
184     #define FB_BLIT_2(dst, src) \
185 gbeauche 1.4 (dst = (((src) >> 24) & 0xff) | (((src) >> 8) & 0xff00) | (((src) & 0xff00) << 8) | (((src) & 0xff) << 24))
186 gbeauche 1.1
187     #define FB_DEPTH 24
188     #include "video_blit.h"
189    
190    
191     /*
192     * Frame buffer copy functions map table
193     */
194    
195     typedef void (*fbcopy_func)(uint8 *, const uint8 *, uint32);
196     static fbcopy_func do_update_framebuffer;
197    
198     #define FBCOPY_FUNC(aHandler) do_ ## aHandler
199    
200     #if REAL_ADDRESSING || DIRECT_ADDRESSING
201     #define WD(X) { FBCOPY_FUNC(X), FBCOPY_FUNC(X) }
202     #else
203     #define WD(X) { FBCOPY_FUNC(fbcopy_raw), FBCOPY_FUNC(fbcopy_raw) }
204     #endif
205    
206     // fb_copy_funcs[depth_id][native_byte_order][dga_mode]
207     // NT : not tested
208     // OK : has been successfully tested
209     // NBO : native byte order
210 gbeauche 1.2 // OBO : opposite byte order
211 gbeauche 1.1 static fbcopy_func fbcopy_funcs[ID_DEPTH_COUNT][2][2] = {
212     #ifdef WORDS_BIGENDIAN
213 gbeauche 1.2 /* opposite byte order native byte order */
214     /* 1 bpp */ { WD(fbcopy_raw) , WD(fbcopy_raw) }, // NT
215     /* 8 bpp */ { WD(fbcopy_raw) , WD(fbcopy_raw) }, // OK (NBO)
216     /* 15 bpp */ { WD(fbcopy_15_obo) , WD(fbcopy_raw) }, // NT
217     /* 16 bpp */ { WD(fbcopy_16_obo) , WD(fbcopy_16_nbo) }, // NT
218     /* 24 bpp */ { WD(fbcopy_24_obo) , WD(fbcopy_raw) } // NT
219 gbeauche 1.1 #else
220 gbeauche 1.2 /* opposite byte order native byte order */
221     /* 1 bpp */ { WD(fbcopy_raw) , WD(fbcopy_raw) }, // NT
222     /* 8 bpp */ { WD(fbcopy_raw) , WD(fbcopy_raw) }, // OK (NBO)
223     /* 15 bpp */ { WD(fbcopy_raw) , WD(fbcopy_15_nbo) }, // OK (NBO)
224     /* 16 bpp */ { WD(fbcopy_16_obo) , WD(fbcopy_16_nbo) }, // OK (NBO)
225     /* 24 bpp */ { WD(fbcopy_raw) , WD(fbcopy_24_nbo) } // NT
226 gbeauche 1.1 #endif
227     };
228    
229     #undef WD
230    
231     #define FBCOPY_FUNC_ERROR \
232     ErrorAlert("Invalid screen depth")
233    
234     #define GET_FBCOPY_FUNC(aDepth, aNativeByteOrder, aDisplay) \
235     ((depth_id(aDepth) == ID_DEPTH_UNKNOWN) ? ( FBCOPY_FUNC_ERROR, (fbcopy_func)0 ) : \
236     fbcopy_funcs[depth_id(aDepth)][(aNativeByteOrder)][(aDisplay) == DISPLAY_DGA ? 1 : 0])
237    
238    
239     /*
240     * Screen fault handler
241     */
242    
243 gbeauche 1.4 static inline void do_handle_screen_fault(uintptr addr)
244 gbeauche 1.1 {
245     if ((addr < mainBuffer.memStart) || (addr >= mainBuffer.memEnd)) {
246     fprintf(stderr, "Segmentation fault at 0x%08X\n", addr);
247     abort();
248     }
249    
250     const int page = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
251     caddr_t page_ad = (caddr_t)(addr & ~(mainBuffer.pageSize - 1));
252     #ifdef HAVE_PTHREADS
253     pthread_mutex_lock(&Screen_draw_lock);
254     #endif
255     PFLAG_SET(page);
256     mprotect(page_ad, mainBuffer.pageSize, PROT_READ | PROT_WRITE);
257     #ifdef HAVE_PTHREADS
258     pthread_mutex_unlock(&Screen_draw_lock);
259     #endif
260     }
261    
262     #if defined(HAVE_SIGINFO_T)
263 cebix 1.6
264 gbeauche 1.1 static void Screen_fault_handler(int, siginfo_t * sip, void *)
265     {
266     D(bug("Screen_fault_handler: ADDR=0x%08X\n", sip->si_addr));
267 gbeauche 1.4 do_handle_screen_fault((uintptr)sip->si_addr);
268 gbeauche 1.1 }
269 cebix 1.6
270 gbeauche 1.1 #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
271 cebix 1.6
272 gbeauche 1.1 # if defined(__i386__) && defined(__linux__)
273     static void Screen_fault_handler(int, struct sigcontext scs)
274     {
275     D(bug("Screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", scs.cr2, scs.eip));
276 gbeauche 1.4 do_handle_screen_fault((uintptr)scs.cr2);
277 gbeauche 1.1 }
278 cebix 1.6
279     # elif defined(__m68k__) && defined(__NetBSD__)
280    
281     # include <m68k/frame.h>
282     static void Screen_fault_handler(int, int code, struct sigcontext *scp)
283     {
284     D(bug("Screen_fault_handler: ADDR=0x%08X\n", code));
285     struct sigstate {
286     int ss_flags;
287     struct frame ss_frame;
288     };
289     struct sigstate *state = (struct sigstate *)scp->sc_ap;
290     uintptr fault_addr;
291     switch (state->ss_frame.f_format) {
292     case 7: // 68040 access error
293     // "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown
294     fault_addr = state->ss_frame.f_fmt7.f_fa;
295     break;
296     default:
297     fault_addr = (uintptr)code;
298     break;
299     }
300     do_handle_screen_fault(fault_addr);
301     }
302    
303 gbeauche 1.1 # else
304     # error "No suitable subterfuge for Video on SEGV signals"
305     # endif
306     #else
307     # error "Can't do Video on SEGV signals"
308     #endif
309    
310    
311     /*
312     * Screen fault handler initialization
313     */
314    
315     #if defined(HAVE_SIGINFO_T)
316     static bool Screen_fault_handler_init()
317     {
318     // Setup SIGSEGV handler to process writes to frame buffer
319     sigemptyset(&vosf_sa.sa_mask);
320     vosf_sa.sa_sigaction = Screen_fault_handler;
321 cebix 1.5 vosf_sa.sa_flags = SA_SIGINFO;
322 gbeauche 1.1 return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
323     }
324     #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
325     static bool Screen_fault_handler_init()
326     {
327     // Setup SIGSEGV handler to process writes to frame buffer
328     sigemptyset(&vosf_sa.sa_mask);
329     vosf_sa.sa_handler = (void (*)(int)) Screen_fault_handler;
330 cebix 1.7 #if !EMULATED_68K && defined(__NetBSD__)
331     sigaddset(&vosf_sa.sa_mask, SIGALRM);
332     vosf_sa.sa_flags = SA_ONSTACK;
333     #else
334 gbeauche 1.1 vosf_sa.sa_flags = 0;
335 cebix 1.7 #endif
336 gbeauche 1.1 return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
337     }
338     #endif
339    
340    
341     /*
342     * Update display for Windowed mode and VOSF
343     */
344    
345     static inline void update_display_window_vosf(void)
346     {
347     int page = 0;
348     for (;;) {
349     while (PFLAG_ISCLEAR_4(page))
350     page += 4;
351    
352     while (PFLAG_ISCLEAR(page))
353     page++;
354    
355     if (page >= mainBuffer.pageCount)
356     break;
357    
358     const int first_page = page;
359 gbeauche 1.4 while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
360 gbeauche 1.1 PFLAG_CLEAR(page);
361 gbeauche 1.4 ++page;
362     }
363 cebix 1.7
364 gbeauche 1.1 // Make the dirty pages read-only again
365     const int32 offset = first_page << mainBuffer.pageBits;
366     const uint32 length = (page - first_page) << mainBuffer.pageBits;
367     mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
368    
369     // There is at least one line to update
370     const int y1 = mainBuffer.pageInfo[first_page].top;
371     const int y2 = mainBuffer.pageInfo[page - 1].bottom;
372     const int height = y2 - y1 + 1;
373    
374     const int bytes_per_row = VideoMonitor.bytes_per_row;
375     const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
376     int i, j;
377    
378     // Check for first column from left and first column
379     // from right that have changed
380 cebix 1.6 int x1, x2, width;
381     if (depth == 1) {
382    
383     x1 = VideoMonitor.x - 1;
384     for (j = y1; j <= y2; j++) {
385     uint8 * const p1 = &the_buffer[j * bytes_per_row];
386     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
387     for (i = 0; i < (x1>>3); i++) {
388     if (p1[i] != p2[i]) {
389     x1 = i << 3;
390     break;
391     }
392 gbeauche 1.1 }
393     }
394 cebix 1.6
395     x2 = x1;
396     for (j = y2; j >= y1; j--) {
397     uint8 * const p1 = &the_buffer[j * bytes_per_row];
398     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
399     for (i = (VideoMonitor.x>>3) - 1; i > (x2>>3); i--) {
400     if (p1[i] != p2[i]) {
401 cebix 1.7 x2 = (i << 3) + 7;
402 cebix 1.6 break;
403     }
404     }
405     }
406     width = x2 - x1 + 1;
407    
408     // Update the_host_buffer and copy of the_buffer
409     i = y1 * bytes_per_row + (x1 >> 3);
410     for (j = y1; j <= y2; j++) {
411     do_update_framebuffer(the_host_buffer + i, the_buffer + i, width >> 3);
412     memcpy(the_buffer_copy + i, the_buffer + i, width >> 3);
413     i += bytes_per_row;
414     }
415    
416     } else {
417    
418     x1 = VideoMonitor.x * bytes_per_pixel - 1;
419     for (j = y1; j <= y2; j++) {
420     uint8 * const p1 = &the_buffer[j * bytes_per_row];
421     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
422     for (i = 0; i < x1; i++) {
423     if (p1[i] != p2[i]) {
424     x1 = i;
425     break;
426     }
427     }
428     }
429     x1 /= bytes_per_pixel;
430 gbeauche 1.1
431 cebix 1.6 x2 = x1 * bytes_per_pixel;
432     for (j = y2; j >= y1; j--) {
433     uint8 * const p1 = &the_buffer[j * bytes_per_row];
434     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
435     for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
436     if (p1[i] != p2[i]) {
437     x2 = i;
438     break;
439     }
440 gbeauche 1.1 }
441     }
442 cebix 1.6 x2 /= bytes_per_pixel;
443     width = x2 - x1 + 1;
444    
445     // Update the_host_buffer and copy of the_buffer
446     i = y1 * bytes_per_row + x1 * bytes_per_pixel;
447     for (j = y1; j <= y2; j++) {
448     do_update_framebuffer(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
449     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
450     i += bytes_per_row;
451     }
452 gbeauche 1.1 }
453    
454     if (have_shm)
455     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, width, height, 0);
456     else
457     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, width, height);
458     }
459     }
460    
461    
462     /*
463     * Update display for DGA mode and VOSF
464     * (only in Direct Addressing mode)
465     */
466    
467     #if REAL_ADDRESSING || DIRECT_ADDRESSING
468     static inline void update_display_dga_vosf(void)
469     {
470     int page = 0;
471     for (;;) {
472     while (PFLAG_ISCLEAR_4(page))
473     page += 4;
474    
475     while (PFLAG_ISCLEAR(page))
476     page++;
477    
478     if (page >= mainBuffer.pageCount)
479     break;
480    
481     const int first_page = page;
482 gbeauche 1.4 while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
483 gbeauche 1.1 PFLAG_CLEAR(page);
484 gbeauche 1.4 ++page;
485     }
486 gbeauche 1.1
487     // Make the dirty pages read-only again
488     const int32 offset = first_page << mainBuffer.pageBits;
489     const uint32 length = (page - first_page) << mainBuffer.pageBits;
490     mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
491    
492     // I am sure that y2 >= y1 and depth != 1
493     const int y1 = mainBuffer.pageInfo[first_page].top;
494     const int y2 = mainBuffer.pageInfo[page - 1].bottom;
495    
496     const int bytes_per_row = VideoMonitor.bytes_per_row;
497     const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
498     int i, j;
499    
500     // Check for first column from left and first column
501     // from right that have changed
502     int x1 = VideoMonitor.x * bytes_per_pixel - 1;
503     for (j = y1; j <= y2; j++) {
504     uint8 * const p1 = &the_buffer[j * bytes_per_row];
505     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
506     for (i = 0; i < x1; i++) {
507     if (p1[i] != p2[i]) {
508     x1 = i;
509     break;
510     }
511     }
512     }
513     x1 /= bytes_per_pixel;
514    
515     int x2 = x1 * bytes_per_pixel;
516     for (j = y2; j >= y1; j--) {
517     uint8 * const p1 = &the_buffer[j * bytes_per_row];
518     uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
519     for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
520     if (p1[i] != p2[i]) {
521     x2 = i;
522     break;
523     }
524     }
525     }
526     x2 /= bytes_per_pixel;
527    
528     // Update the_host_buffer and copy of the_buffer
529     // There should be at least one pixel to copy
530     const int width = x2 - x1 + 1;
531     i = y1 * bytes_per_row + x1 * bytes_per_pixel;
532     for (j = y1; j <= y2; j++) {
533     do_update_framebuffer(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
534     memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
535     i += bytes_per_row;
536     }
537     }
538     }
539     #endif
540    
541     #endif /* ENABLE_VOSF */
542    
543     #endif /* VIDEO_VOSF_H */