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.33 by gbeauche, 2003-05-22T22:13:56Z

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines