ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_vosf.h
(Generate patch)

Comparing BasiliskII/src/Unix/video_vosf.h (file contents):
Revision 1.8 by cebix, 2000-11-03T18:21:42Z vs.
Revision 1.29 by gbeauche, 2001-07-14T18:41:01Z

# Line 1 | Line 1
1   /*
2   *  video_vosf.h - Video/graphics emulation, video on SEGV signals support
3   *
4 < *  Basilisk II (C) 1997-2000 Christian Bauer
4 > *  Basilisk II (C) 1997-2001 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
# Line 24 | Line 24
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 < static uintptr align_on_page_boundary(uintptr size)
33 < {
34 <        const uint32 page_size = getpagesize();
35 <        const uint32 page_mask = page_size - 1;
36 <        return (size + page_mask) & ~page_mask;
37 < }
27 > #include <fcntl.h>
28 > #include <sys/mman.h>
29 > #include "sigsegv.h"
30 > #include "vm_alloc.h"
31  
32 < // Allocate memory on page boundary
33 < static void * allocate_framebuffer(uint32 size, uint8 * hint = 0)
34 < {
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 < }
32 > #ifdef ENABLE_MON
33 > # include "mon.h"
34 > #endif
35  
36 + // Variables for Video on SEGV support
37 + static uint8 *the_host_buffer;  // Host frame buffer in VOSF mode
38  
39 < /*
40 < *      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
39 > struct ScreenPageInfo {
40 >    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
41   };
42  
43 < static int depth_id(int depth)
44 < {
45 <        int id;
46 <        switch (depth) {
47 <                case 1  : id = ID_DEPTH_1;      break;
48 <                case 8  : id = ID_DEPTH_8;      break;
49 <                case 15 : id = ID_DEPTH_15;     break;
50 <                case 16 : id = ID_DEPTH_16;     break;
51 <                case 24 : id = ID_DEPTH_24;     break;
52 <                case 32 : id = ID_DEPTH_32;     break;
53 <                default : id = ID_DEPTH_UNKNOWN;
54 <        }
74 <        return id;
75 < }
76 <
77 <
78 < /*
79 < *      Frame buffer copy function templates
80 < */
43 > struct ScreenInfo {
44 >    uintptr memStart;                   // Start address aligned to page boundary
45 >    uint32 memLength;                   // Length of the memory addressed by the screen pages
46 >    
47 >    uintptr pageSize;                   // Size of a page
48 >    int pageBits;                               // Shift count to get the page number
49 >    uint32 pageCount;                   // Number of pages allocated to the screen
50 >    
51 >        bool dirty;                                     // Flag: set if the frame buffer was touched
52 >    char * dirtyPages;                  // Table of flags set if page was altered
53 >    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
54 > };
55  
56 < // No conversion required
56 > static ScreenInfo mainBuffer;
57  
58 < #define MEMCPY_PROFITABLE
59 < #ifdef MEMCPY_PROFITABLE
60 < static void do_fbcopy_raw(uint8 * dest, const uint8 * source, uint32 length)
61 < {
62 <        memcpy(dest, source, length);
63 < }
58 > #define PFLAG_SET_VALUE                 0x00
59 > #define PFLAG_CLEAR_VALUE               0x01
60 > #define PFLAG_SET_VALUE_4               0x00000000
61 > #define PFLAG_CLEAR_VALUE_4             0x01010101
62 > #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
63 > #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
64 > #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
65 > #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
66 >
67 > #ifdef UNALIGNED_PROFITABLE
68 > # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
69 > # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
70   #else
71 < #define FB_BLIT_1(dst, src)     (dst = (src))
72 < #define FB_BLIT_2(dst, src)     (dst = (src))
73 < #define FB_DEPTH                        0
74 < #define FB_FUNC_NAME            do_fbcopy_raw
75 < #include "video_blit.h"
71 > # define PFLAG_ISSET_4(page) \
72 >                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
73 >        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
74 > # define PFLAG_ISCLEAR_4(page) \
75 >                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
76 >        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
77   #endif
78  
79 <
80 < // RGB 555
81 <
82 < #ifdef WORDS_BIGENDIAN
83 < # define FB_FUNC_NAME do_fbcopy_15_obo
79 > // Set the selected page range [ first_page, last_page [ into the SET state
80 > #define PFLAG_SET_RANGE(first_page, last_page) \
81 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
82 >                (last_page) - (first_page))
83 >
84 > // Set the selected page range [ first_page, last_page [ into the CLEAR state
85 > #define PFLAG_CLEAR_RANGE(first_page, last_page) \
86 >        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
87 >                (last_page) - (first_page))
88 >
89 > #define PFLAG_SET_ALL do { \
90 >        PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
91 >        mainBuffer.dirty = true; \
92 > } while (0)
93 >
94 > #define PFLAG_CLEAR_ALL do { \
95 >        PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
96 >        mainBuffer.dirty = false; \
97 > } while (0)
98 >
99 > // Set the following macro definition to 1 if your system
100 > // provides a really fast strchr() implementation
101 > //#define HAVE_FAST_STRCHR 0
102 >
103 > static inline int find_next_page_set(int page)
104 > {
105 > #if HAVE_FAST_STRCHR
106 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
107 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
108   #else
109 < # define FB_FUNC_NAME do_fbcopy_15_nbo
109 >        while (PFLAG_ISCLEAR_4(page))
110 >                page += 4;
111 >        while (PFLAG_ISCLEAR(page))
112 >                page++;
113 >        return page;
114   #endif
115 + }
116  
117 < #define FB_BLIT_1(dst, src) \
118 <        (dst = (((src) >> 8) & 0xff) | (((src) & 0xff) << 8))
119 <        
120 < #define FB_BLIT_2(dst, src) \
121 <        (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 < #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 <
117 > static inline int find_next_page_clear(int page)
118 > {
119 > #if HAVE_FAST_STRCHR
120 >        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
121 >        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
122   #else
123 <
124 < // native byte order
125 <
126 < #define FB_BLIT_1(dst, src) \
127 <        (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 < #define FB_FUNC_NAME do_fbcopy_16_nbo
157 < #include "video_blit.h"
158 <
159 < // 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 <
123 >        while (PFLAG_ISSET_4(page))
124 >                page += 4;
125 >        while (PFLAG_ISSET(page))
126 >                page++;
127 >        return page;
128   #endif
129 + }
130  
131 < // RGB 888
132 <
133 < #ifdef WORDS_BIGENDIAN
134 < # define FB_FUNC_NAME do_fbcopy_24_obo
131 > #ifdef HAVE_PTHREADS
132 > static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
133 > #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
134 > #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
135   #else
136 < # define FB_FUNC_NAME do_fbcopy_24_nbo
136 > #define LOCK_VOSF
137 > #define UNLOCK_VOSF
138   #endif
139  
140 < #define FB_BLIT_1(dst, src) \
141 <        (dst = (src))
142 <
143 < #define FB_BLIT_2(dst, src) \
144 <        (dst = (((src) >> 24) & 0xff) | (((src) >> 8) & 0xff00) | (((src) & 0xff00) << 8) | (((src) & 0xff) << 24))
140 > static int log_base_2(uint32 x)
141 > {
142 >        uint32 mask = 0x80000000;
143 >        int l = 31;
144 >        while (l >= 0 && (x & mask) == 0) {
145 >                mask >>= 1;
146 >                l--;
147 >        }
148 >        return l;
149 > }
150  
151 < #define FB_DEPTH 24
152 < #include "video_blit.h"
151 > // Extend size to page boundary
152 > static uint32 page_extend(uint32 size)
153 > {
154 >        const uint32 page_size = getpagesize();
155 >        const uint32 page_mask = page_size - 1;
156 >        return (size + page_mask) & ~page_mask;
157 > }
158  
159  
160   /*
161 < *      Frame buffer copy functions map table
161 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
162   */
163  
164 < 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 < // OBO : opposite byte order
211 < static fbcopy_func fbcopy_funcs[ID_DEPTH_COUNT][2][2] = {
212 < #ifdef WORDS_BIGENDIAN
213 <                                /*      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 < #else
220 <                                /*      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 < #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 <
164 > static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
165  
166 < /*
240 < *      Screen fault handler
241 < */
242 <
243 < static inline void do_handle_screen_fault(uintptr addr)
166 > static bool video_vosf_init(void)
167   {
168 <        if ((addr < mainBuffer.memStart) || (addr >= mainBuffer.memEnd)) {
169 <                fprintf(stderr, "Segmentation fault at 0x%08X\n", addr);
170 <                abort();
168 >        const uintptr page_size = getpagesize();
169 >        const uintptr page_mask = page_size - 1;
170 >        
171 >        // Round up frame buffer base to page boundary
172 >        mainBuffer.memStart = (((uintptr) the_buffer) + page_mask) & ~page_mask;
173 >        
174 >        // The frame buffer size shall already be aligned to page boundary (use page_extend)
175 >        mainBuffer.memLength = the_buffer_size;
176 >        
177 >        mainBuffer.pageSize = page_size;
178 >        mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
179 >        mainBuffer.pageCount =  (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
180 >        
181 >        // The "2" more bytes requested are a safety net to insure the
182 >        // loops in the update routines will terminate.
183 >        // See "How can we deal with array overrun conditions ?" hereunder for further details.
184 >        mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
185 >        if (mainBuffer.dirtyPages == NULL)
186 >                return false;
187 >                
188 >        PFLAG_CLEAR_ALL;
189 >        PFLAG_CLEAR(mainBuffer.pageCount);
190 >        PFLAG_SET(mainBuffer.pageCount+1);
191 >        
192 >        // Allocate and fill in pageInfo with start and end (inclusive) row in number of bytes
193 >        mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
194 >        if (mainBuffer.pageInfo == NULL)
195 >                return false;
196 >        
197 >        uint32 a = 0;
198 >        for (unsigned i = 0; i < mainBuffer.pageCount; i++) {
199 >                unsigned y1 = a / VideoMonitor.mode.bytes_per_row;
200 >                if (y1 >= VideoMonitor.mode.y)
201 >                        y1 = VideoMonitor.mode.y - 1;
202 >
203 >                unsigned y2 = (a + mainBuffer.pageSize) / VideoMonitor.mode.bytes_per_row;
204 >                if (y2 >= VideoMonitor.mode.y)
205 >                        y2 = VideoMonitor.mode.y - 1;
206 >
207 >                mainBuffer.pageInfo[i].top = y1;
208 >                mainBuffer.pageInfo[i].bottom = y2;
209 >
210 >                a += mainBuffer.pageSize;
211 >                if (a > mainBuffer.memLength)
212 >                        a = mainBuffer.memLength;
213          }
214          
215 <        const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
216 <        caddr_t page_ad = (caddr_t)(addr & ~(mainBuffer.pageSize - 1));
217 <        LOCK_VOSF;
218 <        PFLAG_SET(page);
219 <        mprotect(page_ad, mainBuffer.pageSize, PROT_READ | PROT_WRITE);
220 <        UNLOCK_VOSF;
221 < }
222 <
223 < #if defined(HAVE_SIGINFO_T)
224 <
225 < static void Screen_fault_handler(int, siginfo_t * sip, void *)
261 < {
262 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", sip->si_addr));
263 <        do_handle_screen_fault((uintptr)sip->si_addr);
215 >        // We can now write-protect the frame buffer
216 >        if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
217 >                return false;
218 >        
219 >        // Initialize the handler for SIGSEGV
220 >        if (!sigsegv_install_handler(screen_fault_handler))
221 >                return false;
222 >        
223 >        // The frame buffer is sane, i.e. there is no write to it yet
224 >        mainBuffer.dirty = false;
225 >        return true;
226   }
227  
266 #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
228  
229 < # if defined(__i386__) && defined(__linux__)
230 < static void Screen_fault_handler(int, struct sigcontext scs)
231 < {
271 <        D(bug("Screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", scs.cr2, scs.eip));
272 <        do_handle_screen_fault((uintptr)scs.cr2);
273 < }
274 <
275 < # elif defined(__m68k__) && defined(__NetBSD__)
229 > /*
230 > * Deinitialize VOSF system
231 > */
232  
233 < # include <m68k/frame.h>
278 < static void Screen_fault_handler(int, int code, struct sigcontext *scp)
233 > static void video_vosf_exit(void)
234   {
235 <        D(bug("Screen_fault_handler: ADDR=0x%08X\n", code));
236 <        struct sigstate {
237 <                int ss_flags;
238 <                struct frame ss_frame;
239 <        };
240 <        struct sigstate *state = (struct sigstate *)scp->sc_ap;
241 <        uintptr fault_addr;
287 <        switch (state->ss_frame.f_format) {
288 <                case 7:         // 68040 access error
289 <                        // "code" is sometimes unreliable (i.e. contains NULL or a bogus address), reason unknown
290 <                        fault_addr = state->ss_frame.f_fmt7.f_fa;
291 <                        break;
292 <                default:
293 <                        fault_addr = (uintptr)code;
294 <                        break;
235 >        if (mainBuffer.pageInfo) {
236 >                free(mainBuffer.pageInfo);
237 >                mainBuffer.pageInfo = NULL;
238 >        }
239 >        if (mainBuffer.dirtyPages) {
240 >                free(mainBuffer.dirtyPages);
241 >                mainBuffer.dirtyPages = NULL;
242          }
296        do_handle_screen_fault(fault_addr);
243   }
244  
299 # else
300 #  error "No suitable subterfuge for Video on SEGV signals"
301 # endif
302 #else
303 # error "Can't do Video on SEGV signals"
304 #endif
305
245  
246   /*
247 < *      Screen fault handler initialization
247 > * Screen fault handler
248   */
249  
250 < #if defined(HAVE_SIGINFO_T)
312 < static bool Screen_fault_handler_init()
313 < {
314 <        // Setup SIGSEGV handler to process writes to frame buffer
315 <        sigemptyset(&vosf_sa.sa_mask);
316 <        vosf_sa.sa_sigaction = Screen_fault_handler;
317 <        vosf_sa.sa_flags = SA_SIGINFO;
318 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
319 < }
320 < #elif defined(HAVE_SIGCONTEXT_SUBTERFUGE)
321 < static bool Screen_fault_handler_init()
250 > static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
251   {
252 <        // Setup SIGSEGV handler to process writes to frame buffer
253 <        sigemptyset(&vosf_sa.sa_mask);
254 <        vosf_sa.sa_handler = (void (*)(int)) Screen_fault_handler;
255 < #if !EMULATED_68K && defined(__NetBSD__)
256 <        sigaddset(&vosf_sa.sa_mask, SIGALRM);
257 <        vosf_sa.sa_flags = SA_ONSTACK;
258 < #else
259 <        vosf_sa.sa_flags = 0;
252 > //      D(bug("screen_fault_handler: ADDR=%p from IP=%p\n", fault_address, fault_instruction));
253 >        const uintptr addr = (uintptr)fault_address;
254 >        
255 >        /* Someone attempted to write to the frame buffer. Make it writeable
256 >         * now so that the data could actually be written to. It will be made
257 >         * read-only back in one of the screen update_*() functions.
258 >         */
259 >        if (((uintptr)addr - mainBuffer.memStart) < mainBuffer.memLength) {
260 >                const int page  = ((uintptr)addr - mainBuffer.memStart) >> mainBuffer.pageBits;
261 >                LOCK_VOSF;
262 >                PFLAG_SET(page);
263 >                vm_protect((char *)(addr & -mainBuffer.pageSize), mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
264 >                mainBuffer.dirty = true;
265 >                UNLOCK_VOSF;
266 >                return true;
267 >        }
268 >        
269 >        /* Otherwise, we don't know how to handle the fault, let it crash */
270 >        fprintf(stderr, "do_handle_screen_fault: unhandled address %p", fault_address);
271 >        if (fault_instruction != SIGSEGV_INVALID_PC)
272 >                fprintf(stderr, " [IP=%p]", fault_instruction);
273 >        fprintf(stderr, "\n");
274 > #if EMULATED_68K
275 >        uaecptr nextpc;
276 >        extern void m68k_dumpstate(uaecptr *nextpc);
277 >        m68k_dumpstate(&nextpc);
278   #endif
279 <        return (sigaction(SIGSEGV, &vosf_sa, NULL) == 0);
280 < }
279 >        VideoQuitFullScreen();
280 > #ifdef ENABLE_MON
281 >        char *arg[4] = {"mon", "-m", "-r", NULL};
282 >        mon(3, arg);
283 >        QuitEmulator();
284   #endif
285 +        return false;
286 + }
287  
288  
289   /*
290   *      Update display for Windowed mode and VOSF
291   */
292  
293 < static inline void update_display_window_vosf(void)
293 > // From video_blit.cpp
294 > extern void (*Screen_blit)(uint8 * dest, const uint8 * source, uint32 length);
295 > extern bool Screen_blitter_init(XVisualInfo * visual_info, bool native_byte_order, video_depth mac_depth);
296 > extern uint32 ExpandMap[256];
297 >
298 > /*      How can we deal with array overrun conditions ?
299 >        
300 >        The state of the framebuffer pages that have been touched are maintained
301 >        in the dirtyPages[] table. That table is (pageCount + 2) bytes long.
302 >
303 > Terminology
304 >        
305 >        "Last Page" denotes the pageCount-nth page, i.e. dirtyPages[pageCount - 1].
306 >        "CLEAR Page Guard" refers to the page following the Last Page but is always
307 >        in the CLEAR state. "SET Page Guard" refers to the page following the CLEAR
308 >        Page Guard but is always in the SET state.
309 >
310 > Rough process
311 >        
312 >        The update routines must determine which pages have to be blitted to the
313 >        screen. This job consists in finding the first_page that was touched.
314 >        i.e. find the next page that is SET. Then, finding how many pages were
315 >        touched starting from first_page. i.e. find the next page that is CLEAR.
316 >
317 > There are two cases to check:
318 >
319 >        - Last Page is CLEAR: find_next_page_set() will reach the SET Page Guard
320 >        but it is beyond the valid pageCount value. Therefore, we exit from the
321 >        update routine.
322 >        
323 >        - Last Page is SET: first_page equals (pageCount - 1) and
324 >        find_next_page_clear() will reach the CLEAR Page Guard. We blit the last
325 >        page to the screen. On the next iteration, page equals pageCount and
326 >        find_next_page_set() will reach the SET Page Guard. We still safely exit
327 >        from the update routine because the SET Page Guard position is greater
328 >        than pageCount.
329 > */
330 >
331 > static inline void update_display_window_vosf(driver_window *drv)
332   {
333          int page = 0;
334          for (;;) {
335 <                while (PFLAG_ISCLEAR_4(page))
336 <                        page += 4;
347 <                
348 <                while (PFLAG_ISCLEAR(page))
349 <                        page++;
350 <                
351 <                if (page >= mainBuffer.pageCount)
335 >                const unsigned first_page = find_next_page_set(page);
336 >                if (first_page >= mainBuffer.pageCount)
337                          break;
338 <                
339 <                const int first_page = page;
340 <                while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
356 <                        PFLAG_CLEAR(page);
357 <                        ++page;
358 <                }
338 >
339 >                page = find_next_page_clear(first_page);
340 >                PFLAG_CLEAR_RANGE(first_page, page);
341  
342                  // Make the dirty pages read-only again
343                  const int32 offset  = first_page << mainBuffer.pageBits;
344                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
345 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
345 >                vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
346                  
347                  // There is at least one line to update
348                  const int y1 = mainBuffer.pageInfo[first_page].top;
349                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
350                  const int height = y2 - y1 + 1;
351                  
352 <                const int bytes_per_row = VideoMonitor.bytes_per_row;
371 <                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
372 <                int i, j;
373 <                
374 <                // Check for first column from left and first column
375 <                // from right that have changed
376 <                int x1, x2, width;
377 <                if (depth == 1) {
378 <
379 <                        x1 = VideoMonitor.x - 1;
380 <                        for (j = y1; j <= y2; j++) {
381 <                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
382 <                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
383 <                                for (i = 0; i < (x1>>3); i++) {
384 <                                        if (p1[i] != p2[i]) {
385 <                                                x1 = i << 3;
386 <                                                break;
387 <                                        }
388 <                                }
389 <                        }
390 <
391 <                        x2 = x1;
392 <                        for (j = y2; j >= y1; j--) {
393 <                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
394 <                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
395 <                                for (i = (VideoMonitor.x>>3) - 1; i > (x2>>3); i--) {
396 <                                        if (p1[i] != p2[i]) {
397 <                                                x2 = (i << 3) + 7;
398 <                                                break;
399 <                                        }
400 <                                }
401 <                        }
402 <                        width = x2 - x1 + 1;
352 >                if (VideoMonitor.mode.depth < VDEPTH_8BIT) {
353  
354                          // Update the_host_buffer and copy of the_buffer
355 <                        i = y1 * bytes_per_row + (x1 >> 3);
355 >                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
356 >                        const int dst_bytes_per_row = drv->img->bytes_per_line;
357 >                        const int pixels_per_byte = VideoMonitor.mode.x / src_bytes_per_row;
358 >                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
359                          for (j = y1; j <= y2; j++) {
360 <                                do_update_framebuffer(the_host_buffer + i, the_buffer + i, width >> 3);
361 <                                memcpy(the_buffer_copy + i, the_buffer + i, width >> 3);
362 <                                i += bytes_per_row;
360 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VideoMonitor.mode.x / pixels_per_byte);
361 >                                i1 += src_bytes_per_row;
362 >                                i2 += dst_bytes_per_row;
363                          }
364  
365                  } else {
366  
414                        x1 = VideoMonitor.x * bytes_per_pixel - 1;
415                        for (j = y1; j <= y2; j++) {
416                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
417                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
418                                for (i = 0; i < x1; i++) {
419                                        if (p1[i] != p2[i]) {
420                                                x1 = i;
421                                                break;
422                                        }
423                                }
424                        }
425                        x1 /= bytes_per_pixel;
426                
427                        x2 = x1 * bytes_per_pixel;
428                        for (j = y2; j >= y1; j--) {
429                                uint8 * const p1 = &the_buffer[j * bytes_per_row];
430                                uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
431                                for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
432                                        if (p1[i] != p2[i]) {
433                                                x2 = i;
434                                                break;
435                                        }
436                                }
437                        }
438                        x2 /= bytes_per_pixel;
439                        width = x2 - x1 + 1;
440
367                          // Update the_host_buffer and copy of the_buffer
368 <                        i = y1 * bytes_per_row + x1 * bytes_per_pixel;
368 >                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
369 >                        const int dst_bytes_per_row = drv->img->bytes_per_line;
370 >                        const int bytes_per_pixel = src_bytes_per_row / VideoMonitor.mode.x;
371 >                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
372                          for (j = y1; j <= y2; j++) {
373 <                                do_update_framebuffer(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
374 <                                memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
375 <                                i += bytes_per_row;
373 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VideoMonitor.mode.x);
374 >                                i1 += src_bytes_per_row;
375 >                                i2 += dst_bytes_per_row;
376                          }
377                  }
378 <                
379 <                if (have_shm)
380 <                        XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, width, height, 0);
378 >
379 >                if (drv->have_shm)
380 >                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height, 0);
381                  else
382 <                        XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, width, height);
382 >                        XPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height);
383          }
384 +        mainBuffer.dirty = false;
385   }
386  
387  
388   /*
389   *      Update display for DGA mode and VOSF
390 < *      (only in Direct Addressing mode)
390 > *      (only in Real or Direct Addressing mode)
391   */
392  
393   #if REAL_ADDRESSING || DIRECT_ADDRESSING
# Line 465 | Line 395 | static inline void update_display_dga_vo
395   {
396          int page = 0;
397          for (;;) {
398 <                while (PFLAG_ISCLEAR_4(page))
399 <                        page += 4;
470 <                
471 <                while (PFLAG_ISCLEAR(page))
472 <                        page++;
473 <                
474 <                if (page >= mainBuffer.pageCount)
398 >                const unsigned first_page = find_next_page_set(page);
399 >                if (first_page >= mainBuffer.pageCount)
400                          break;
401 <                
402 <                const int first_page = page;
403 <                while ((page < mainBuffer.pageCount) && PFLAG_ISSET(page)) {
404 <                        PFLAG_CLEAR(page);
480 <                        ++page;
481 <                }
482 <                
401 >
402 >                page = find_next_page_clear(first_page);
403 >                PFLAG_CLEAR_RANGE(first_page, page);
404 >
405                  // Make the dirty pages read-only again
406                  const int32 offset  = first_page << mainBuffer.pageBits;
407                  const uint32 length = (page - first_page) << mainBuffer.pageBits;
408 <                mprotect((caddr_t)(mainBuffer.memStart + offset), length, PROT_READ);
408 >                vm_protect((char *)mainBuffer.memStart + offset, length, VM_PAGE_READ);
409                  
410                  // I am sure that y2 >= y1 and depth != 1
411                  const int y1 = mainBuffer.pageInfo[first_page].top;
412                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
413                  
414 <                const int bytes_per_row = VideoMonitor.bytes_per_row;
415 <                const int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
414 >                const int bytes_per_row = VideoMonitor.mode.bytes_per_row;
415 >                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
416                  int i, j;
417                  
418                  // Check for first column from left and first column
419                  // from right that have changed
420 <                int x1 = VideoMonitor.x * bytes_per_pixel - 1;
420 >                int x1 = VideoMonitor.mode.x * bytes_per_pixel - 1;
421                  for (j = y1; j <= y2; j++) {
422                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
423                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
# Line 512 | Line 434 | static inline void update_display_dga_vo
434                  for (j = y2; j >= y1; j--) {
435                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
436                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
437 <                        for (i = VideoMonitor.x * bytes_per_pixel - 1; i > x2; i--) {
437 >                        for (i = VideoMonitor.mode.x * bytes_per_pixel - 1; i > x2; i--) {
438                                  if (p1[i] != p2[i]) {
439                                          x2 = i;
440                                          break;
# Line 526 | Line 448 | static inline void update_display_dga_vo
448                  const int width = x2 - x1 + 1;
449                  i = y1 * bytes_per_row + x1 * bytes_per_pixel;
450                  for (j = y1; j <= y2; j++) {
451 <                        do_update_framebuffer(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
451 >                        Screen_blit(the_host_buffer + i, the_buffer + i, bytes_per_pixel * width);
452                          memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * width);
453                          i += bytes_per_row;
454                  }
455          }
456 +        mainBuffer.dirty = false;
457   }
458   #endif
459  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines