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.26 by cebix, 2001-07-06T20:49:48Z 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-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 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 161 | Line 197 | static uint32 page_extend(uint32 size)
197  
198  
199   /*
200 < *  Initialize mainBuffer structure
200 > *  Initialize the VOSF system (mainBuffer structure, SIGSEGV handler)
201   */
202  
203 < static bool video_init_buffer(void)
204 < {
205 <        if (use_vosf) {
206 <                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 <                }
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 <                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
208 > static bool video_vosf_init(X11_MONITOR_INIT)
209 > {
210 >        VIDEO_MODE_INIT;
211  
212 <                if (mainBuffer.pageInfo) {
213 <                        free(mainBuffer.pageInfo);
214 <                        mainBuffer.pageInfo = NULL;
215 <                }
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 >        // 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  
195                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
274  
275 <                if ((mainBuffer.dirtyPages == NULL) || (mainBuffer.pageInfo == NULL))
276 <                        return false;
277 <                
200 <                mainBuffer.dirty = false;
275 > /*
276 > * Deinitialize VOSF system
277 > */
278  
279 <                PFLAG_CLEAR_ALL;
280 <                // Safety net to insure the loops in the update routines will terminate
281 <                // See "How can we deal with array overrun conditions ?" hereunder for further details
282 <                PFLAG_CLEAR(mainBuffer.pageCount);
283 <                PFLAG_SET(mainBuffer.pageCount+1);
284 <
285 <                uint32 a = 0;
286 <                for (int i = 0; i < mainBuffer.pageCount; i++) {
287 <                        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;
279 > static void video_vosf_exit(void)
280 > {
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          }
230        return true;
289   }
290  
291  
# Line 235 | Line 293 | static bool video_init_buffer(void)
293   * Screen fault handler
294   */
295  
296 < static bool screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
296 > bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
297   {
240 //      D(bug("screen_fault_handler: ADDR=0x%08X from IP=0x%08X\n", fault_address, fault_instruction));
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 ((addr >= mainBuffer.memStart) && (addr < mainBuffer.memEnd)) {
305 <                const int page  = (addr - mainBuffer.memStart) >> mainBuffer.pageBits;
249 <                caddr_t page_ad = (caddr_t)(addr & -mainBuffer.pageSize);
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 *)page_ad, mainBuffer.pageSize, VM_PAGE_READ | VM_PAGE_WRITE);
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 */
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        VideoQuitFullScreen();
269 #ifdef ENABLE_MON
270        char *arg[4] = {"mon", "-m", "-r", NULL};
271        mon(3, arg);
272        QuitEmulator();
273 #endif
315          return false;
316   }
317  
# Line 281 | Line 322 | static bool screen_fault_handler(sigsegv
322  
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, video_depth mac_depth);
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 ?
# Line 317 | Line 358 | There are two cases to check:
358          than pageCount.
359   */
360  
361 < static inline void update_display_window_vosf(driver_window *drv)
361 > static inline void update_display_window_vosf(VIDEO_DRV_INIT)
362   {
363 +        VIDEO_MODE_INIT;
364 +
365          int page = 0;
366          for (;;) {
367 <                const int first_page = find_next_page_set(page);
367 >                const unsigned first_page = find_next_page_set(page);
368                  if (first_page >= mainBuffer.pageCount)
369                          break;
370  
# Line 338 | Line 381 | static inline void update_display_window
381                  const int y2 = mainBuffer.pageInfo[page - 1].bottom;
382                  const int height = y2 - y1 + 1;
383                  
384 <                if (VideoMonitor.mode.depth < VDEPTH_8BIT) {
384 >                if (VIDEO_MODE_DEPTH < VIDEO_DEPTH_8BIT) {
385  
386                          // Update the_host_buffer and copy of the_buffer
387 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
388 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
389 <                        const int pixels_per_byte = VideoMonitor.mode.x / src_bytes_per_row;
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 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, VideoMonitor.mode.x / pixels_per_byte);
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                          }
# Line 354 | Line 397 | static inline void update_display_window
397                  } else {
398  
399                          // Update the_host_buffer and copy of the_buffer
400 <                        const int src_bytes_per_row = VideoMonitor.mode.bytes_per_row;
401 <                        const int dst_bytes_per_row = drv->img->bytes_per_line;
402 <                        const int bytes_per_pixel = src_bytes_per_row / VideoMonitor.mode.x;
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 <                                Screen_blit(the_host_buffer + i2, the_buffer + i1, bytes_per_pixel * VideoMonitor.mode.x);
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 (drv->have_shm)
412 <                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, height, 0);
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, drv->w, drv->gc, drv->img, 0, y1, 0, y1, VideoMonitor.mode.x, 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   }
# Line 382 | Line 425 | static inline void update_display_window
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 <                const int first_page = find_next_page_set(page);
432 >                const unsigned first_page = find_next_page_set(page);
433                  if (first_page >= mainBuffer.pageCount)
434                          break;
435  
# Line 400 | Line 445 | static inline void update_display_dga_vo
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.mode.bytes_per_row;
449 <                const int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.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.mode.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 423 | 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.mode.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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines