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.22 by cebix, 2001-07-01T14:38:03Z vs.
Revision 1.36 by gbeauche, 2004-01-04T06:11:49Z

# Line 1 | Line 1
1   /*
2   *  video_vosf.h - Video/graphics emulation, video on SEGV signals support
3   *
4 < *  Basilisk II (C) 1997-2001 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 21 | Line 21
21   #ifndef VIDEO_VOSF_H
22   #define VIDEO_VOSF_H
23  
24 < // Note: this file is #include'd in video_x.cpp
24 > // Note: this file must be #include'd only in video_x.cpp
25   #ifdef ENABLE_VOSF
26  
27   #include <fcntl.h>
# Line 29 | Line 29
29   #include "sigsegv.h"
30   #include "vm_alloc.h"
31  
32 < #ifdef ENABLE_MON
33 < # include "mon.h"
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 >  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 > };
53 > #else
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   // Variables for Video on SEGV support
76   static uint8 *the_host_buffer;  // Host frame buffer in VOSF mode
38 static uint32 the_buffer_size;  // Size of allocated the_buffer
77  
78   struct ScreenPageInfo {
79      int top, bottom;                    // Mapping between this virtual page and Mac scanlines
80   };
81  
82   struct ScreenInfo {
45    uintptr memBase;                    // Real start address
83      uintptr memStart;                   // Start address aligned to page boundary
47    uintptr memEnd;                             // Address of one-past-the-end of the screen
84      uint32 memLength;                   // Length of the memory addressed by the screen pages
85      
86 <    uint32 pageSize;                    // Size of a page
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      
# Line 131 | Line 167 | static inline int find_next_page_clear(i
167   #endif
168   }
169  
170 < #ifdef HAVE_PTHREADS
170 > #ifdef HAVE_SPINLOCKS
171 > static spinlock_t vosf_lock = SPIN_LOCK_UNLOCKED;                               // Mutex to protect frame buffer (dirtyPages in fact)
172 > #define LOCK_VOSF spin_lock(&vosf_lock)
173 > #define UNLOCK_VOSF spin_unlock(&vosf_lock)
174 > #elif defined(HAVE_PTHREADS)
175   static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
176   #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
177   #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
# Line 161 | Line 201 | static uint32 page_extend(uint32 size)
201  
202  
203   /*
204 < *  Initialize mainBuffer structure
204 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
205   */
206  
207 < static bool video_init_buffer(void)
207 > static bool video_vosf_init(X11_MONITOR_INIT)
208   {
209 <        if (use_vosf) {
170 <                const uint32 page_size  = getpagesize();
171 <                const uint32 page_mask  = page_size - 1;
172 <                
173 <                mainBuffer.memBase      = (uintptr) the_buffer;
174 <                // Round up frame buffer base to page boundary
175 <                mainBuffer.memStart             = (uintptr)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
176 <                mainBuffer.memLength    = the_buffer_size;
177 <                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
178 <
179 <                mainBuffer.pageSize     = page_size;
180 <                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
181 <                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
182 <
183 <                if (mainBuffer.dirtyPages) {
184 <                        free(mainBuffer.dirtyPages);
185 <                        mainBuffer.dirtyPages = NULL;
186 <                }
209 >        VIDEO_MODE_INIT;
210  
211 <                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
212 <
213 <                if (mainBuffer.pageInfo) {
214 <                        free(mainBuffer.pageInfo);
215 <                        mainBuffer.pageInfo = NULL;
216 <                }
211 >        const uintptr page_size = getpagesize();
212 >        const uintptr page_mask = page_size - 1;
213 >        
214 >        // Round up frame buffer base to page boundary
215 >        mainBuffer.memStart = (((uintptr) the_buffer) + page_mask) & ~page_mask;
216 >        
217 >        // The frame buffer size shall already be aligned to page boundary (use page_extend)
218 >        mainBuffer.memLength = the_buffer_size;
219 >        
220 >        mainBuffer.pageSize = page_size;
221 >        mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
222 >        mainBuffer.pageCount =  (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
223 >        
224 >        // The "2" more bytes requested are a safety net to insure the
225 >        // loops in the update routines will terminate.
226 >        // See "How can we deal with array overrun conditions ?" hereunder for further details.
227 >        mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
228 >        if (mainBuffer.dirtyPages == NULL)
229 >                return false;
230 >                
231 >        PFLAG_CLEAR_ALL;
232 >        PFLAG_CLEAR(mainBuffer.pageCount);
233 >        PFLAG_SET(mainBuffer.pageCount+1);
234 >        
235 >        // Allocate and fill in pageInfo with start and end (inclusive) row in number of bytes
236 >        mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
237 >        if (mainBuffer.pageInfo == NULL)
238 >                return false;
239 >        
240 >        uint32 a = 0;
241 >        for (unsigned i = 0; i < mainBuffer.pageCount; i++) {
242 >                unsigned y1 = a / VIDEO_MODE_ROW_BYTES;
243 >                if (y1 >= VIDEO_MODE_Y)
244 >                        y1 = VIDEO_MODE_Y - 1;
245 >
246 >                unsigned y2 = (a + mainBuffer.pageSize) / VIDEO_MODE_ROW_BYTES;
247 >                if (y2 >= VIDEO_MODE_Y)
248 >                        y2 = VIDEO_MODE_Y - 1;
249 >
250 >                mainBuffer.pageInfo[i].top = y1;
251 >                mainBuffer.pageInfo[i].bottom = y2;
252 >
253 >                a += mainBuffer.pageSize;
254 >                if (a > mainBuffer.memLength)
255 >                        a = mainBuffer.memLength;
256 >        }
257 >        
258 >        // We can now write-protect the frame buffer
259 >        if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
260 >                return false;
261 >        
262 >        // The frame buffer is sane, i.e. there is no write to it yet
263 >        mainBuffer.dirty = false;
264 >        return true;
265 > }
266  
195                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
267  
268 <                if ((mainBuffer.dirtyPages == NULL) || (mainBuffer.pageInfo == NULL))
269 <                        return false;
270 <                
200 <                mainBuffer.dirty = false;
268 > /*
269 > * Deinitialize VOSF system
270 > */
271  
272 <                PFLAG_CLEAR_ALL;
273 <                // Safety net to insure the loops in the update routines will terminate
274 <                // See "How can we deal with array overrun conditions ?" hereunder for further details
275 <                PFLAG_CLEAR(mainBuffer.pageCount);
276 <                PFLAG_SET(mainBuffer.pageCount+1);
277 <
278 <                uint32 a = 0;
279 <                for (int i = 0; i < mainBuffer.pageCount; i++) {
280 <                        int y1 = a / VideoMonitor.mode.bytes_per_row;
211 <                        if (y1 >= VideoMonitor.mode.y)
212 <                                y1 = VideoMonitor.mode.y - 1;
213 <
214 <                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.mode.bytes_per_row;
215 <                        if (y2 >= VideoMonitor.mode.y)
216 <                                y2 = VideoMonitor.mode.y - 1;
217 <
218 <                        mainBuffer.pageInfo[i].top = y1;
219 <                        mainBuffer.pageInfo[i].bottom = y2;
220 <
221 <                        a += mainBuffer.pageSize;
222 <                        if (a > mainBuffer.memLength)
223 <                                a = mainBuffer.memLength;
224 <                }
225 <                
226 <                // We can now write-protect the frame buffer
227 <                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
228 <                        return false;
272 > static void video_vosf_exit(void)
273 > {
274 >        if (mainBuffer.pageInfo) {
275 >                free(mainBuffer.pageInfo);
276 >                mainBuffer.pageInfo = NULL;
277 >        }
278 >        if (mainBuffer.dirtyPages) {
279 >                free(mainBuffer.dirtyPages);
280 >                mainBuffer.dirtyPages = NULL;
281          }
230        return true;
282   }
283  
284  
# Line 235 | Line 286 | static bool video_init_buffer(void)
286   * Screen fault handler
287   */
288  
289 < static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
289 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
290   {
240        D(bug("screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", fault_address, fault_instruction));
291          const uintptr addr = (uintptr)fault_address;
292          
293          /* Someone attempted to write to the frame buffer. Make it writeable
294           * now so that the data could actually be written to. It will be made
295           * read-only back in one of the screen update_*() functions.
296           */
297 <        if ((addr >= mainBuffer.memStart) && (addr < mainBuffer.memEnd)) {
298 <                const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
249 <                caddr_t page_ad = (caddr_t)(addr & -mainBuffer.pageSize);
297 >        if (((uintptr)addr - mainBuffer.memStart) < mainBuffer.memLength) {
298 >                const int page  = ((uintptr)addr - mainBuffer.memStart) >> mainBuffer.pageBits;
299                  LOCK_VOSF;
300                  PFLAG_SET(page);
301 <                vm_protect((char *)page_ad, mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
301 >                vm_protect((char *)(addr & -mainBuffer.pageSize), mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
302                  mainBuffer.dirty = true;
303                  UNLOCK_VOSF;
304                  return true;
305          }
306          
307          /* Otherwise, we don't know how to handle the fault, let it crash */
259        fprintf(stderr, "do_handle_screen_fault: unhandled address 0x%08X", addr);
260        if (fault_instruction != SIGSEGV_INVALID_PC)
261                fprintf(stderr, " [IP=0x%08X]", fault_instruction);
262        fprintf(stderr, "\n");
263 #if EMULATED_68K
264        uaecptr nextpc;
265        extern void m68k_dumpstate(uaecptr *nextpc);
266        m68k_dumpstate(&nextpc);
267 #endif
268 #ifdef ENABLE_MON
269        char *arg[4] = {"mon", "-m", "-r", NULL};
270        mon(3, arg);
271        QuitEmulator();
272 #endif
308          return false;
309   }
310  
# Line 280 | Line 315 | static bool screen_fault_handler(sigsegv
315  
316   // From video_blit.cpp
317   extern void (*Screen_blit)(uint8 * dest, const uint8 * source, uint32 length);
318 < extern bool Screen_blitter_init(XVisualInfo * visual_info, bool native_byte_order, video_depth mac_depth);
318 > extern bool Screen_blitter_init(XVisualInfo * visual_info, bool native_byte_order, int mac_depth);
319 > extern uint32 ExpandMap[256];
320  
321   /*      How can we deal with array overrun conditions ?
322          
# Line 315 | Line 351 | There are two cases to check:
351          than pageCount.
352   */
353  
354 < static inline void update_display_window_vosf(driver_window *drv)
354 > static inline void update_display_window_vosf(VIDEO_DRV_INIT)
355   {
356 +        VIDEO_MODE_INIT;
357 +
358          int page = 0;
359          for (;;) {
360 <                const int first_page = find_next_page_set(page);
360 >                const unsigned first_page = find_next_page_set(page);
361                  if (first_page >= mainBuffer.pageCount)
362                          break;
363  
# Line 336 | Line 374 | static inline void update_display_window
374                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
375                  const int height = y2 - y1 + 1;
376                  
377 <                const int bytes_per_row = VideoMonitor.mode.bytes_per_row;
340 <                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
341 <                int i = y1 * bytes_per_row, j;
342 <                
343 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
377 >                if (VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
378  
379                          // Update the_host_buffer and copy of the_buffer
380 +                        const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
381 +                        const int dst_bytes_per_row = VIDEO_DRV_IMAGE->bytes_per_line;
382 +                        const int pixels_per_byte = VIDEO_MODE_X / src_bytes_per_row;
383 +                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
384                          for (j = y1; j <= y2; j++) {
385 <                                Screen_blit(the_host_buffer + i, the_buffer + i, VideoMonitor.mode.x >> 3);
386 <                                i += bytes_per_row;
385 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VIDEO_MODE_X / pixels_per_byte);
386 >                                i1 += src_bytes_per_row;
387 >                                i2 += dst_bytes_per_row;
388                          }
389  
390                  } else {
391  
392                          // Update the_host_buffer and copy of the_buffer
393 +                        const int src_bytes_per_row = VIDEO_MODE_ROW_BYTES;
394 +                        const int dst_bytes_per_row = VIDEO_DRV_IMAGE->bytes_per_line;
395 +                        const int bytes_per_pixel = src_bytes_per_row / VIDEO_MODE_X;
396 +                        int i1 = y1 * src_bytes_per_row, i2 = y1 * dst_bytes_per_row, j;
397                          for (j = y1; j <= y2; j++) {
398 <                                Screen_blit(the_host_buffer + i, the_buffer + i, bytes_per_pixel * VideoMonitor.mode.x);
399 <                                i += bytes_per_row;
398 >                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VIDEO_MODE_X);
399 >                                i1 += src_bytes_per_row;
400 >                                i2 += dst_bytes_per_row;
401                          }
402                  }
403  
404 <                if (drv->have_shm)
405 <                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height, 0);
404 >                if (VIDEO_DRV_HAVE_SHM)
405 >                        XShmPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height, 0);
406                  else
407 <                        XPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height);
407 >                        XPutImage(x_display, VIDEO_DRV_WINDOW, VIDEO_DRV_GC, VIDEO_DRV_IMAGE, 0, y1, 0, y1, VIDEO_MODE_X, height);
408          }
409          mainBuffer.dirty = false;
410   }
# Line 374 | Line 418 | static inline void update_display_window
418   #if REAL_ADDRESSING || DIRECT_ADDRESSING
419   static inline void update_display_dga_vosf(void)
420   {
421 +        VIDEO_MODE_INIT;
422 +
423          int page = 0;
424          for (;;) {
425 <                const int first_page = find_next_page_set(page);
425 >                const unsigned first_page = find_next_page_set(page);
426                  if (first_page >= mainBuffer.pageCount)
427                          break;
428  
# Line 392 | Line 438 | static inline void update_display_dga_vo
438                  const int y1 = mainBuffer.pageInfo[first_page].top;
439                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
440                  
441 <                const int bytes_per_row = VideoMonitor.mode.bytes_per_row;
442 <                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
441 >                const int bytes_per_row = VIDEO_MODE_ROW_BYTES;
442 >                const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
443                  int i, j;
444                  
445                  // Check for first column from left and first column
446                  // from right that have changed
447 <                int x1 = VideoMonitor.mode.x * bytes_per_pixel - 1;
447 >                int x1 = VIDEO_MODE_X * bytes_per_pixel - 1;
448                  for (j = y1; j <= y2; j++) {
449                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
450                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
# Line 415 | Line 461 | static inline void update_display_dga_vo
461                  for (j = y2; j >= y1; j--) {
462                          uint8 * const p1 = &the_buffer[j * bytes_per_row];
463                          uint8 * const p2 = &the_buffer_copy[j * bytes_per_row];
464 <                        for (i = VideoMonitor.mode.x * bytes_per_pixel - 1; i > x2; i--) {
464 >                        for (i = VIDEO_MODE_X * bytes_per_pixel - 1; i > x2; i--) {
465                                  if (p1[i] != p2[i]) {
466                                          x2 = i;
467                                          break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines