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.7 by cebix, 2000-10-13T16:47:52Z vs.
Revision 1.19 by cebix, 2001-06-28T21:20:00Z

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines