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

Comparing BasiliskII/src/video.cpp (file contents):
Revision 1.5 by jlachmann, 2000-08-20T14:08:40Z vs.
Revision 1.21 by cebix, 2001-07-04T11:12:19Z

# Line 1 | Line 1
1   /*
2   *  video.cpp - Video/graphics emulation
3   *
4 < *  Basilisk II (C) 1997-2000 Christian Bauer
4 > *  Basilisk II (C) 1997-2001 Christian Bauer
5   *  Portions (C) 1997-1999 Marc Hellwig
6   *
7   *  This program is free software; you can redistribute it and/or modify
# Line 31 | Line 31
31   #include "cpu_emulation.h"
32   #include "main.h"
33   #include "macos_util.h"
34 + #include "slot_rom.h"
35   #include "video.h"
36   #include "video_defs.h"
37  
# Line 38 | Line 39
39   #include "debug.h"
40  
41  
42 + // List of supported video modes
43 + vector<video_mode> VideoModes;
44 +
45   // Description of the main monitor
46 < video_desc VideoMonitor;
46 > monitor_desc VideoMonitor;
47 >
48 > // Depth code -> Apple mode mapping
49 > uint16 apple_mode_for_depth[6];
50  
51   // Local variables (per monitor)
52   struct {
53 <        video_desc *desc;                       // Pointer to monitor description
53 >        monitor_desc *desc;                     // Pointer to description of monitor handled by this instance of the driver
54          uint8 palette[256 * 3];         // Color palette, 256 entries, RGB
55          bool luminance_mapping;         // Luminance mapping on/off
56          bool interrupts_enabled;        // VBL interrupts on/off
57 +        bool dm_present;                        // We received a GetVideoParameters call, so the Display Manager seems to be present
58 +        uint32 gamma_table;                     // Mac address of gamma table
59 +        int alloc_gamma_table_size;     // Allocated size of gamma table
60 +        uint16 current_mode;            // Currently selected depth/resolution
61 +        uint32 current_id;
62 +        uint16 preferred_mode;          // Preferred depth/resolution
63 +        uint32 preferred_id;
64 +        uint32 slot_param;                      // Mac address of Slot Manager parameter block
65   } VidLocal;
66  
67  
68   /*
69 + *  Initialize apple_mode_for_depth[] array from VideoModes list
70 + */
71 +
72 + void video_init_depth_list(void)
73 + {
74 +        uint16 mode = 0x80;
75 +        for (int depth = VDEPTH_1BIT; depth <= VDEPTH_32BIT; depth++) {
76 +                if (video_has_depth(video_depth(depth)))
77 +                        apple_mode_for_depth[depth] = mode++;
78 +                else
79 +                        apple_mode_for_depth[depth] = 0;
80 +        }
81 + }
82 +
83 +
84 + /*
85 + *  Check whether a mode with the specified depth exists
86 + */
87 +
88 + bool video_has_depth(video_depth depth)
89 + {
90 +        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
91 +        while (i != end) {
92 +                if (i->depth == depth)
93 +                        return true;
94 +                ++i;
95 +        }
96 +        return false;
97 + }
98 +
99 +
100 + /*
101 + *  Check whether specified resolution ID is one of the supported resolutions
102 + */
103 +
104 + static bool has_resolution(uint32 id)
105 + {
106 +        vector<video_mode>::const_iterator i, end = VideoModes.end();
107 +        for (i = VideoModes.begin(); i != end; ++i) {
108 +                if (i->resolution_id == id)
109 +                        return true;
110 +        }
111 +        return false;
112 + }
113 +
114 +
115 + /*
116 + *  Find specified mode (depth/resolution) (or VideoModes.end() if not found)
117 + */
118 +
119 + static vector<video_mode>::const_iterator find_mode(uint16 mode, uint32 id)
120 + {
121 +        vector<video_mode>::const_iterator i, end = VideoModes.end();
122 +        for (i = VideoModes.begin(); i != end; ++i) {
123 +                if (i->resolution_id == id && DepthToAppleMode(i->depth) == mode)
124 +                        return i;
125 +        }
126 +        return i;
127 + }
128 +
129 +
130 + /*
131 + *  Find maximum supported depth for given resolution ID
132 + */
133 +
134 + static video_depth max_depth_of_resolution(uint32 id)
135 + {
136 +        video_depth m = VDEPTH_1BIT;
137 +        vector<video_mode>::const_iterator i, end = VideoModes.end();
138 +        for (i = VideoModes.begin(); i != end; ++i) {
139 +                if (i->depth > m)
140 +                        m = i->depth;
141 +        }
142 +        return m;
143 + }
144 +
145 +
146 + /*
147 + *  Get X/Y size of specified resolution
148 + */
149 +
150 + static void get_size_of_resolution(uint32 id, uint32 &x, uint32 &y)
151 + {
152 +        vector<video_mode>::const_iterator i, end = VideoModes.end();
153 +        for (i = VideoModes.begin(); i != end; ++i) {
154 +                if (i->resolution_id == id) {
155 +                        x = i->x;
156 +                        y = i->y;
157 +                        return;
158 +                }
159 +        }
160 +        x = y = 0;
161 + }
162 +
163 +
164 + /*
165 + *  Get bytes-per-row value for specified resolution/depth
166 + */
167 +
168 + uint32 video_bytes_per_row(video_depth depth, uint32 id)
169 + {
170 +        vector<video_mode>::const_iterator i, end = VideoModes.end();
171 +        for (i = VideoModes.begin(); i != end; ++i) {
172 +                if (i->depth == depth && i->resolution_id == id)
173 +                        return i->bytes_per_row;
174 +        }
175 +        uint32 x, y;
176 +        get_size_of_resolution(id, x, y);
177 +        return TrivialBytesPerRow(x, depth);
178 + }
179 +
180 +
181 + /*
182 + *  Find palette size for given color depth
183 + */
184 +
185 + static int palette_size(video_depth depth)
186 + {
187 +        switch (depth) {
188 +                case VDEPTH_1BIT: return 2;
189 +                case VDEPTH_2BIT: return 4;
190 +                case VDEPTH_4BIT: return 16;
191 +                case VDEPTH_8BIT: return 256;
192 +                case VDEPTH_16BIT: return 32;
193 +                case VDEPTH_32BIT: return 256;
194 +                default: return 0;
195 +        }
196 + }
197 +
198 +
199 + /*
200 + *  Set palette to 50% gray
201 + */
202 +
203 + static void set_gray_palette(void)
204 + {
205 +        for (int i=0; i<256; i++) {
206 +                VidLocal.palette[i * 3 + 0] = 127;
207 +                VidLocal.palette[i * 3 + 1] = 127;
208 +                VidLocal.palette[i * 3 + 2] = 127;
209 +        }
210 +        video_set_palette(VidLocal.palette, 256);
211 + }
212 +
213 +
214 + /*
215 + *  Load gamma-corrected black-to-white ramp to palette for direct-color mode
216 + */
217 +
218 + static void load_ramp_palette(void)
219 + {
220 +        // Find tables for gamma correction
221 +        uint8 *red_gamma, *green_gamma, *blue_gamma;
222 +        bool have_gamma = false;
223 +        int data_width = 0;
224 +        if (VidLocal.gamma_table) {
225 +                uint32 table = VidLocal.gamma_table;
226 +                red_gamma = Mac2HostAddr(table + gFormulaData + ReadMacInt16(table + gFormulaSize));
227 +                int chan_cnt = ReadMacInt16(table + gChanCnt);
228 +                if (chan_cnt == 1)
229 +                        green_gamma = blue_gamma = red_gamma;
230 +                else {
231 +                        int ofs = ReadMacInt16(table + gDataCnt);
232 +                        green_gamma = red_gamma + ofs;
233 +                        blue_gamma = green_gamma + ofs;
234 +                }
235 +                data_width = ReadMacInt16(table + gDataWidth);
236 +                have_gamma = true;
237 +        }
238 +
239 +        int num = (VidLocal.desc->mode.depth == VDEPTH_16BIT ? 32 : 256);
240 +        uint8 *p = VidLocal.palette;
241 +        for (int i=0; i<num; i++) {
242 +                uint8 red = (i * 256 / num), green = red, blue = red;
243 +                if (have_gamma) {
244 +                        red = red_gamma[red >> (8 - data_width)];
245 +                        green = green_gamma[green >> (8 - data_width)];
246 +                        blue = blue_gamma[blue >> (8 - data_width)];
247 +                }
248 +                *p++ = red;
249 +                *p++ = green;
250 +                *p++ = blue;
251 +        }
252 +
253 +        video_set_palette(VidLocal.palette, num);
254 + }
255 +
256 +
257 + /*
258 + *  Allocate gamma table of specified size
259 + */
260 +
261 + static bool allocate_gamma_table(int size)
262 + {
263 +        M68kRegisters r;
264 +
265 +        if (size > VidLocal.alloc_gamma_table_size) {
266 +                if (VidLocal.gamma_table) {
267 +                        r.a[0] = VidLocal.gamma_table;
268 +                        Execute68kTrap(0xa01f, &r);     // DisposePtr()
269 +                        VidLocal.gamma_table = 0;
270 +                        VidLocal.alloc_gamma_table_size = 0;
271 +                }
272 +                r.d[0] = size;
273 +                Execute68kTrap(0xa71e, &r);     // NewPtrSysClear()
274 +                if (r.a[0] == 0)
275 +                        return false;
276 +                VidLocal.gamma_table = r.a[0];
277 +                VidLocal.alloc_gamma_table_size = size;
278 +        }
279 +        return true;
280 + }
281 +
282 +
283 + /*
284 + *  Set gamma table (0 = build linear ramp)
285 + */
286 +
287 + static bool set_gamma_table(uint32 user_table)
288 + {
289 +        if (user_table == 0) { // Build linear ramp, 256 entries
290 +
291 +                // Allocate new table, if necessary
292 +                if (!allocate_gamma_table(SIZEOF_GammaTbl + 256))
293 +                        return memFullErr;
294 +                uint32 table = VidLocal.gamma_table;
295 +
296 +                // Initialize header
297 +                WriteMacInt16(table + gVersion, 0);
298 +                WriteMacInt16(table + gType, 0);
299 +                WriteMacInt16(table + gFormulaSize, 0);
300 +                WriteMacInt16(table + gChanCnt, 1);
301 +                WriteMacInt16(table + gDataCnt, 256);
302 +                WriteMacInt16(table + gDataWidth, 8);
303 +
304 +                // Build ramp
305 +                uint32 p = table + gFormulaData;
306 +                for (int i=0; i<256; i++)
307 +                        WriteMacInt8(p + i, i);
308 +
309 +        } else { // User-supplied gamma table
310 +
311 +                // Validate header
312 +                if (ReadMacInt16(user_table + gVersion))
313 +                        return paramErr;
314 +                if (ReadMacInt16(user_table + gType))
315 +                        return paramErr;
316 +                int chan_cnt = ReadMacInt16(user_table + gChanCnt);
317 +                if (chan_cnt != 1 && chan_cnt != 3)
318 +                        return paramErr;
319 +                int data_width = ReadMacInt16(user_table + gDataWidth);
320 +                if (data_width > 8)
321 +                        return paramErr;
322 +                int data_cnt = ReadMacInt16(user_table + gDataCnt);
323 +                if (data_cnt != (1 << data_width))
324 +                        return paramErr;
325 +
326 +                // Allocate new table, if necessary
327 +                int size = SIZEOF_GammaTbl + ReadMacInt16(user_table + gFormulaSize) + chan_cnt * data_cnt;
328 +                if (!allocate_gamma_table(size))
329 +                        return memFullErr;
330 +                uint32 table = VidLocal.gamma_table;
331 +
332 +                // Copy table
333 +                Mac2Mac_memcpy(table, user_table, size);
334 +        }
335 +
336 +        if (IsDirectMode(VidLocal.desc->mode))
337 +                load_ramp_palette();
338 +
339 +        return true;
340 + }
341 +
342 +
343 + /*
344 + *  Switch video mode
345 + */
346 +
347 + static void switch_mode(const video_mode &mode, uint32 param, uint32 dce)
348 + {
349 +        // Switch mode
350 +        set_gray_palette();
351 +        video_switch_to_mode(mode);
352 +
353 +        // Update VidLocal
354 +        VidLocal.current_mode = DepthToAppleMode(mode.depth);
355 +        VidLocal.current_id = mode.resolution_id;
356 +
357 +        uint32 frame_base = VidLocal.desc->mac_frame_base;
358 +
359 +        M68kRegisters r;
360 +        uint32 sp = VidLocal.slot_param;
361 +        r.a[0] = sp;
362 +
363 +        // Find functional sResource for this display
364 +        WriteMacInt8(sp + spSlot, ReadMacInt8(dce + dCtlSlot));
365 +        WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId));
366 +        WriteMacInt8(sp + spExtDev, 0);
367 +        r.d[0] = 0x0016;
368 +        Execute68kTrap(0xa06e, &r);     // SRsrcInfo()
369 +        uint32 rsrc = ReadMacInt32(sp + spPointer);
370 +
371 +        // Patch minorBase (otherwise rebooting won't work)
372 +        WriteMacInt8(sp + spID, 0x0a); // minorBase
373 +        r.d[0] = 0x0006;
374 +        Execute68kTrap(0xa06e, &r); // SFindStruct()
375 +        uint32 minor_base = ReadMacInt32(sp + spPointer) - ROMBaseMac;
376 +        ROMBaseHost[minor_base + 0] = frame_base >> 24;
377 +        ROMBaseHost[minor_base + 1] = frame_base >> 16;
378 +        ROMBaseHost[minor_base + 2] = frame_base >> 8;
379 +        ROMBaseHost[minor_base + 3] = frame_base;
380 +
381 +        // Patch video mode parameter table
382 +        WriteMacInt32(sp + spPointer, rsrc);
383 +        WriteMacInt8(sp + spID, DepthToAppleMode(mode.depth));
384 +        r.d[0] = 0x0006;
385 +        Execute68kTrap(0xa06e, &r); // SFindStruct()
386 +        WriteMacInt8(sp + spID, 0x01);
387 +        r.d[0] = 0x0006;
388 +        Execute68kTrap(0xa06e, &r); // SFindStruct()
389 +        uint32 p = ReadMacInt32(sp + spPointer) - ROMBaseMac;
390 +        ROMBaseHost[p +  8] = mode.bytes_per_row >> 8;
391 +        ROMBaseHost[p +  9] = mode.bytes_per_row;
392 +        ROMBaseHost[p + 14] = mode.y >> 8;
393 +        ROMBaseHost[p + 15] = mode.y;
394 +        ROMBaseHost[p + 16] = mode.x >> 8;
395 +        ROMBaseHost[p + 17] = mode.x;
396 +
397 +        // Recalculate slot ROM checksum
398 +        ChecksumSlotROM();
399 +
400 +        // Update sResource
401 +        WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId));
402 +        r.d[0] = 0x002b;
403 +        Execute68kTrap(0xa06e, &r); // SUpdateSRT()
404 +
405 +        // Update frame buffer base in DCE and param block
406 +        WriteMacInt32(dce + dCtlDevBase, frame_base);
407 +        WriteMacInt32(param + csBaseAddr, frame_base);
408 +
409 +        // Patch frame buffer base address for MacOS versions <7.6
410 +        if (!VidLocal.dm_present) { // Only do this when no Display Manager seems to be present; otherwise, the screen will not get redrawn
411 +                WriteMacInt32(0x824, frame_base);                       // ScrnBase
412 +                WriteMacInt32(0x898, frame_base);                       // CrsrBase
413 +                uint32 gdev = ReadMacInt32(0x8a4);                      // MainDevice
414 +                gdev = ReadMacInt32(gdev);
415 +                uint32 pmap = ReadMacInt32(gdev + 0x16);        // gdPMap
416 +                pmap = ReadMacInt32(pmap);
417 +                WriteMacInt32(pmap, frame_base);                        // baseAddr
418 +        }
419 + }
420 +
421 +
422 + /*
423   *  Driver Open() routine
424   */
425  
# Line 58 | Line 427 | int16 VideoDriverOpen(uint32 pb, uint32
427   {
428          D(bug("VideoDriverOpen\n"));
429  
430 +        // This shouldn't happen unless the platform-specific video code is broken
431 +        if (VideoModes.empty())
432 +                fprintf(stderr, "No valid video modes found (broken video driver?)\n");
433 +
434          // Init local variables
435          VidLocal.desc = &VideoMonitor;
436          VidLocal.luminance_mapping = false;
437          VidLocal.interrupts_enabled = false;
438 +        VidLocal.current_mode = DepthToAppleMode(VidLocal.desc->mode.depth);
439 +        VidLocal.current_id = VidLocal.desc->mode.resolution_id;
440 +        VidLocal.preferred_mode = VidLocal.current_mode;
441 +        VidLocal.preferred_id = VidLocal.current_id;
442 +        VidLocal.dm_present = false;
443 +
444 +        // Allocate Slot Manager parameter block in Mac RAM
445 +        M68kRegisters r;
446 +        r.d[0] = SIZEOF_SPBlock;
447 +        Execute68kTrap(0xa71e, &r);     // NewPtrSysClear()
448 +        if (r.a[0] == 0)
449 +                return memFullErr;
450 +        VidLocal.slot_param = r.a[0];
451 +        D(bug("SPBlock at %08x\n", VidLocal.slot_param));
452 +
453 +        // Find and set default gamma table
454 +        VidLocal.gamma_table = 0;
455 +        VidLocal.alloc_gamma_table_size = 0;
456 +        set_gamma_table(0);
457  
458          // Init color palette (solid gray)
459 <        if (!IsDirectMode(VidLocal.desc->mode)) {
68 <                for (int i=0; i<256; i++) {
69 <                        VidLocal.palette[i * 3 + 0] = 127;
70 <                        VidLocal.palette[i * 3 + 1] = 127;
71 <                        VidLocal.palette[i * 3 + 2] = 127;
72 <                }
73 <                video_set_palette(VidLocal.palette);
74 <        }
459 >        set_gray_palette();
460          return noErr;
461   }
462  
# Line 87 | Line 472 | int16 VideoDriverControl(uint32 pb, uint
472          D(bug("VideoDriverControl %d\n", code));
473          switch (code) {
474  
475 <                case cscSetMode:                // Set color depth
476 <                        D(bug(" SetMode %04x\n", ReadMacInt16(param + csMode)));
475 >                case cscSetMode: {              // Set color depth
476 >                        uint16 mode = ReadMacInt16(param + csMode);
477 >                        D(bug(" SetMode %04x\n", mode));
478 >
479 >                        // Set old base address in case the switch fails
480                          WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
481 +
482 +                        if (ReadMacInt16(param + csPage))
483 +                                return paramErr;
484 +
485 +                        if (mode != VidLocal.current_mode) {
486 +                                vector<video_mode>::const_iterator i = find_mode(mode, VidLocal.current_id);
487 +                                if (i == VideoModes.end())
488 +                                        return paramErr;
489 +                                switch_mode(*i, param, dce);
490 +                        }
491 +                        D(bug("  base %08x\n", VidLocal.desc->mac_frame_base));
492                          return noErr;
493 +                }
494  
495 <                case cscSetEntries: {   // Set palette
496 <                        D(bug(" SetEntries table %08lx, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart)));
497 <                        if (IsDirectMode(VidLocal.desc->mode))
495 >                case cscSetEntries:             // Set palette
496 >                case cscDirectSetEntries: {
497 >                        D(bug(" (Direct)SetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart)));
498 >                        bool is_direct = IsDirectMode(VidLocal.desc->mode);
499 >                        if (code == cscSetEntries && is_direct)
500 >                                return controlErr;
501 >                        if (code == cscDirectSetEntries && !is_direct)
502                                  return controlErr;
503  
504                          uint32 s_pal = ReadMacInt32(param + csTable);   // Source palette
505                          uint8 *d_pal;                                                                   // Destination palette
506 +                        uint16 start = ReadMacInt16(param + csStart);
507                          uint16 count = ReadMacInt16(param + csCount);
508 <                        if (!s_pal || count > 255)
508 >                        if (s_pal == 0 || count > 255)
509                                  return paramErr;
510  
511 <                        if (ReadMacInt16(param + csStart) == 0xffff) {  // Indexed
511 >                        // Find tables for gamma correction
512 >                        uint8 *red_gamma, *green_gamma, *blue_gamma;
513 >                        bool have_gamma = false;
514 >                        int data_width = 0;
515 >                        if (VidLocal.gamma_table) {
516 >                                uint32 table = VidLocal.gamma_table;
517 >                                red_gamma = Mac2HostAddr(table + gFormulaData + ReadMacInt16(table + gFormulaSize));
518 >                                int chan_cnt = ReadMacInt16(table + gChanCnt);
519 >                                if (chan_cnt == 1)
520 >                                        green_gamma = blue_gamma = red_gamma;
521 >                                else {
522 >                                        int ofs = ReadMacInt16(table + gDataCnt);
523 >                                        green_gamma = red_gamma + ofs;
524 >                                        blue_gamma = green_gamma + ofs;
525 >                                }
526 >                                data_width = ReadMacInt16(table + gDataWidth);
527 >                                have_gamma = true;
528 >                        }
529 >
530 >                        // Convert palette
531 >                        if (start == 0xffff) {  // Indexed
532                                  for (uint32 i=0; i<=count; i++) {
533 <                                        d_pal = VidLocal.palette + ReadMacInt16(s_pal) * 3;
533 >                                        d_pal = VidLocal.palette + (ReadMacInt16(s_pal) & 0xff) * 3;
534                                          uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8;
535                                          uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8;
536                                          uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8;
537 <                                        if (VidLocal.luminance_mapping)
537 >                                        if (VidLocal.luminance_mapping && !is_direct)
538                                                  red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16;
539 +                                        if (have_gamma) {
540 +                                                red = red_gamma[red >> (8 - data_width)];
541 +                                                green = green_gamma[green >> (8 - data_width)];
542 +                                                blue = blue_gamma[blue >> (8 - data_width)];
543 +                                        }
544                                          *d_pal++ = red;
545                                          *d_pal++ = green;
546                                          *d_pal++ = blue;
547                                          s_pal += 8;
548                                  }
549 <                        } else {                                                                                // Sequential
550 <                                d_pal = VidLocal.palette + ReadMacInt16(param + csStart) * 3;
549 >                        } else {                                // Sequential
550 >                                if (start + count > 255)
551 >                                        return paramErr;
552 >                                d_pal = VidLocal.palette + start * 3;
553                                  for (uint32 i=0; i<=count; i++) {
554                                          uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8;
555                                          uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8;
556                                          uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8;
557 <                                        if (VidLocal.luminance_mapping)
557 >                                        if (VidLocal.luminance_mapping && !is_direct)
558                                                  red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16;
559 +                                        if (have_gamma) {
560 +                                                red = red_gamma[red >> (8 - data_width)];
561 +                                                green = green_gamma[green >> (8 - data_width)];
562 +                                                blue = blue_gamma[blue >> (8 - data_width)];
563 +                                        }
564                                          *d_pal++ = red;
565                                          *d_pal++ = green;
566                                          *d_pal++ = blue;
567                                          s_pal += 8;
568                                  }
569                          }
570 <                        video_set_palette(VidLocal.palette);
570 >                        video_set_palette(VidLocal.palette, palette_size(VidLocal.desc->mode.depth));
571                          return noErr;
572                  }
573  
574 <                case cscSetGamma:               // Set gamma table
575 <                        D(bug(" SetGamma\n"));
576 <                        return noErr;
574 >                case cscSetGamma: {             // Set gamma table
575 >                        uint32 user_table = ReadMacInt32(param + csGTable);
576 >                        D(bug(" SetGamma %08x\n", user_table));
577 >                        return set_gamma_table(user_table) ? noErr : memFullErr;
578 >                }
579  
580                  case cscGrayPage: {             // Fill page with dithered gray pattern
581                          D(bug(" GrayPage %d\n", ReadMacInt16(param + csPage)));
# Line 151 | Line 590 | int16 VideoDriverControl(uint32 pb, uint
590                                  0xffff0000,             // 16 bpp
591                                  0xffffffff              // 32 bpp
592                          };
593 <                        uint32 p = VidLocal.desc->mac_frame_base;
594 <                        uint32 pat = pattern[VidLocal.desc->mode];
595 <                        for (uint32 y=0; y<VidLocal.desc->y; y++) {
596 <                                uint32 p2 = p;
597 <                                for (uint32 x=0; x<VidLocal.desc->bytes_per_row / 4; x++) {
598 <                                        WriteMacInt32(p2, pat);
599 <                                        p2 += 4;
161 <                                        if (VidLocal.desc->mode == VMODE_32BIT)
593 >                        uint32 frame_base = VidLocal.desc->mac_frame_base;
594 >                        uint32 pat = pattern[VidLocal.desc->mode.depth];
595 >                        bool invert = (VidLocal.desc->mode.depth == VDEPTH_32BIT);
596 >                        for (uint32 y=0; y<VidLocal.desc->mode.y; y++) {
597 >                                for (uint32 x=0; x<VidLocal.desc->mode.bytes_per_row; x+=4) {
598 >                                        WriteMacInt32(frame_base + x, pat);
599 >                                        if (invert)
600                                                  pat = ~pat;
601                                  }
602 <                                p += VidLocal.desc->bytes_per_row;
602 >                                frame_base += VidLocal.desc->mode.bytes_per_row;
603                                  pat = ~pat;
604                          }
605 +
606 +                        if (IsDirectMode(VidLocal.desc->mode))
607 +                                load_ramp_palette();
608 +
609                          return noErr;
610                  }
611  
# Line 172 | Line 614 | int16 VideoDriverControl(uint32 pb, uint
614                          VidLocal.luminance_mapping = ReadMacInt8(param + csMode);
615                          return noErr;
616  
175                case cscSwitchMode:             // Switch video mode
176                        D(bug(" SwitchMode %04x, %08lx\n", ReadMacInt16(param + csMode), ReadMacInt32(param + csData)));
177                        WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
178                        return noErr;
179
617                  case cscSetInterrupt:   // Enable/disable VBL
618                          D(bug(" SetInterrupt %02x\n", ReadMacInt8(param + csMode)));
619                          VidLocal.interrupts_enabled = (ReadMacInt8(param + csMode) == 0);
620                          return noErr;
621  
622 +                case cscSetDefaultMode: { // Set default color depth
623 +                        uint16 mode = ReadMacInt8(param + csMode);
624 +                        D(bug(" SetDefaultMode %02x\n", mode));
625 +                        VidLocal.preferred_mode = mode;
626 +                        return noErr;
627 +                }
628 +
629 +                case cscSwitchMode: {   // Switch video mode (depth and resolution)
630 +                        uint16 mode = ReadMacInt16(param + csMode);
631 +                        uint32 id = ReadMacInt32(param + csData);
632 +                        D(bug(" SwitchMode %04x, %08x\n", mode, id));
633 +
634 +                        // Set old base address in case the switch fails
635 +                        WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
636 +
637 +                        if (ReadMacInt16(param + csPage))
638 +                                return paramErr;
639 +
640 +                        if (mode != VidLocal.current_mode || id != VidLocal.current_id) {
641 +                                vector<video_mode>::const_iterator i = find_mode(mode, id);
642 +                                if (i == VideoModes.end())
643 +                                        return paramErr;
644 +                                switch_mode(*i, param, dce);
645 +                        }
646 +                        D(bug("  base %08x\n", VidLocal.desc->mac_frame_base));
647 +                        return noErr;
648 +                }
649 +
650 +                case cscSavePreferredConfiguration: {
651 +                        uint16 mode = ReadMacInt16(param + csMode);
652 +                        uint32 id = ReadMacInt32(param + csData);
653 +                        D(bug(" SavePreferredConfiguration %04x, %08x\n", mode, id));
654 +                        VidLocal.preferred_mode = mode;
655 +                        VidLocal.preferred_id = id;
656 +                        return noErr;
657 +                }
658 +
659                  default:
660                          printf("WARNING: Unknown VideoDriverControl(%d)\n", code);
661                          return controlErr;
# Line 200 | Line 674 | int16 VideoDriverStatus(uint32 pb, uint3
674          D(bug("VideoDriverStatus %d\n", code));
675          switch (code) {
676  
677 <                case cscGetPageCnt:                     // Get number of pages
678 <                        D(bug(" GetPageCnt\n"));
677 >                case cscGetMode:                        // Get current color depth
678 >                        D(bug(" GetMode -> %04x, base %08x\n", VidLocal.current_mode, VidLocal.desc->mac_frame_base));
679 >                        WriteMacInt16(param + csMode, VidLocal.current_mode);
680 >                        WriteMacInt16(param + csPage, 0);
681 >                        WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
682 >                        return noErr;
683 >
684 >                case cscGetEntries: {           // Read palette
685 >                        D(bug(" GetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart)));
686 >
687 >                        uint8 *s_pal;                                                                   // Source palette
688 >                        uint32 d_pal = ReadMacInt32(param + csTable);   // Destination palette
689 >                        uint16 start = ReadMacInt16(param + csStart);
690 >                        uint16 count = ReadMacInt16(param + csCount);
691 >                        if (d_pal == 0 || count > 255)
692 >                                return paramErr;
693 >
694 >                        if (start == 0xffff) {  // Indexed
695 >                                for (uint32 i=0; i<=count; i++) {
696 >                                        s_pal = VidLocal.palette + (ReadMacInt16(d_pal) & 0xff) * 3;
697 >                                        uint8 red = *s_pal++;
698 >                                        uint8 green = *s_pal++;
699 >                                        uint8 blue = *s_pal++;
700 >                                        WriteMacInt16(d_pal + 2, red * 0x0101);
701 >                                        WriteMacInt16(d_pal + 4, green * 0x0101);
702 >                                        WriteMacInt16(d_pal + 6, blue * 0x0101);
703 >                                        d_pal += 8;
704 >                                }
705 >                        } else {                                // Sequential
706 >                                if (start + count > 255)
707 >                                        return paramErr;
708 >                                s_pal = VidLocal.palette + start * 3;
709 >                                for (uint32 i=0; i<=count; i++) {
710 >                                        uint8 red = *s_pal++;
711 >                                        uint8 green = *s_pal++;
712 >                                        uint8 blue = *s_pal++;
713 >                                        WriteMacInt16(d_pal + 2, red * 0x0101);
714 >                                        WriteMacInt16(d_pal + 4, green * 0x0101);
715 >                                        WriteMacInt16(d_pal + 6, blue * 0x0101);
716 >                                        d_pal += 8;
717 >                                }
718 >                        }
719 >                        return noErr;
720 >                }
721 >
722 >                case cscGetPages:                       // Get number of pages
723 >                        D(bug(" GetPages -> 1\n"));
724                          WriteMacInt16(param + csPage, 1);
725                          return noErr;
726  
727 <                case cscGetPageBase:            // Get page base address
728 <                        D(bug(" GetPageBase\n"));
727 >                case cscGetBaseAddress:         // Get page base address
728 >                        D(bug(" GetBaseAddress -> %08x\n", VidLocal.desc->mac_frame_base));
729                          WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
730 <                        return noErr;
730 >                        if (ReadMacInt16(param + csPage))
731 >                                return paramErr;
732 >                        else
733 >                                return noErr;
734  
735                  case cscGetGray:                        // Get luminance mapping flag
736 <                        D(bug(" GetGray\n"));
737 <                        WriteMacInt8(param + csMode, VidLocal.luminance_mapping ? 1 : 0);
736 >                        D(bug(" GetGray -> %d\n", VidLocal.luminance_mapping));
737 >                        WriteMacInt8(param, VidLocal.luminance_mapping ? 1 : 0);
738                          return noErr;
739  
740                  case cscGetInterrupt:           // Get interrupt disable flag
741 <                        D(bug(" GetInterrupt\n"));
742 <                        WriteMacInt8(param + csMode, VidLocal.interrupts_enabled ? 0 : 1);
741 >                        D(bug(" GetInterrupt -> %d\n", VidLocal.interrupts_enabled));
742 >                        WriteMacInt8(param, VidLocal.interrupts_enabled ? 0 : 1);
743 >                        return noErr;
744 >
745 >                case cscGetGamma:
746 >                        D(bug(" GetGamma -> %08x\n", VidLocal.gamma_table));
747 >                        WriteMacInt32(param + csGTable, VidLocal.gamma_table);
748                          return noErr;
749  
750 <                case cscGetDefaultMode:         // Get default video mode
751 <                        D(bug(" GetDefaultMode\n"));
752 <                        WriteMacInt8(param + csMode, 0x80);
750 >                case cscGetDefaultMode:         // Get default color depth
751 >                        D(bug(" GetDefaultMode -> %02x\n", VidLocal.preferred_mode));
752 >                        WriteMacInt8(param + csMode, VidLocal.preferred_mode);
753                          return noErr;
754  
755 <                case cscGetCurMode:                     // Get current video mode
756 <                        D(bug(" GetCurMode\n"));
757 <                        WriteMacInt16(param + csMode, 0x80);
758 <                        WriteMacInt32(param + csData, 0x80);
755 >                case cscGetCurrentMode:         // Get current video mode (depth and resolution)
756 >                        D(bug(" GetCurMode -> %04x/%08x, base %08x\n", VidLocal.current_mode, VidLocal.current_id, VidLocal.desc->mac_frame_base));
757 >                        WriteMacInt16(param + csMode, VidLocal.current_mode);
758 >                        WriteMacInt32(param + csData, VidLocal.current_id);
759                          WriteMacInt16(param + csPage, 0);
760                          WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
761                          return noErr;
762  
763                  case cscGetConnection:          // Get monitor information
764                          D(bug(" GetConnection\n"));
765 <                        WriteMacInt16(param + csDisplayType, 6);                // 21" Multiscan
766 <                        WriteMacInt8(param + csConnectTaggedType, 6);
767 <                        WriteMacInt8(param + csConnectTaggedData, 0x23);
768 <                        WriteMacInt32(param + csConnectFlags, 0x03);    // All modes valid and safe
765 >                        WriteMacInt16(param + csDisplayType, 8);                // Modeless connection
766 >                        WriteMacInt8(param + csConnectTaggedType, 0);
767 >                        WriteMacInt8(param + csConnectTaggedData, 0);
768 >                        WriteMacInt32(param + csConnectFlags, 0x43);    // All modes valid and safe, non-standard tagging
769                          WriteMacInt32(param + csDisplayComponent, 0);
770                          return noErr;
771  
772 <                case cscGetModeTiming:          // Get video timing for mode
773 <                        D(bug(" GetModeTiming mode %08lx\n", ReadMacInt32(param + csTimingMode)));
774 <                        WriteMacInt32(param + csTimingFormat, 'decl');
775 <                        WriteMacInt32(param + csTimingData, 220);               // 21" Multiscan
776 <                        WriteMacInt32(param + csTimingFlags, 0x0f);             // Mode valid, safe, default and shown in Monitors panel
772 >                case cscGetModeTiming: {        // Get video timing for specified resolution
773 >                        uint32 id = ReadMacInt32(param + csTimingMode);
774 >                        D(bug(" GetModeTiming %08x\n", id));
775 >                        if (!has_resolution(id))
776 >                                return paramErr;
777 >
778 >                        WriteMacInt32(param + csTimingFormat, FOURCC('d', 'e', 'c', 'l'));
779 >                        WriteMacInt32(param + csTimingData, 0); // unknown
780 >                        uint32 flags = 0xb; // mode valid, safe and shown in Monitors panel
781 >                        if (id == VidLocal.preferred_id)
782 >                                flags |= 4; // default mode
783 >                        WriteMacInt32(param + csTimingFlags, flags);
784                          return noErr;
785 +                }
786  
787                  case cscGetModeBaseAddress:     // Get frame buffer base address
788 <                        D(bug(" GetModeBaseAddress\n"));
789 <                        WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);       // Base address of video RAM for the current DisplayModeID and relative bit depth
788 >                        D(bug(" GetModeBaseAddress -> base %08x\n", VidLocal.desc->mac_frame_base));
789 >                        WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
790                          return noErr;
791  
792 <                case cscGetMode:                // REQUIRED for MacsBug
793 <                        D(bug(" GetMode\n"));
794 <                        WriteMacInt16(param + csPageMode, 0x80);
795 <                        WriteMacInt32(param + csPageData, 0x80);        // Unused
796 <                        WriteMacInt16(param + csPagePage, 0);   // Current display page
797 <                        WriteMacInt32(param + csPageBaseAddr, VidLocal.desc->mac_frame_base);
792 >                case cscGetPreferredConfiguration: // Get default video mode (depth and resolution)
793 >                        D(bug(" GetPreferredConfiguration -> %04x/%08x\n", VidLocal.preferred_mode, VidLocal.preferred_id));
794 >                        WriteMacInt16(param + csMode, VidLocal.preferred_mode);
795 >                        WriteMacInt32(param + csData, VidLocal.preferred_id);
796 >                        return noErr;
797 >
798 >                case cscGetNextResolution: {    // Called iteratively to obtain a list of all supported resolutions
799 >                        uint32 id = ReadMacInt32(param + csPreviousDisplayModeID);
800 >                        D(bug(" GetNextResolution %08x\n", id));
801 >
802 >                        switch (id) {
803 >                                case 0:
804 >                                        // Return current resolution
805 >                                        id = VidLocal.current_id;
806 >                                        break;
807 >
808 >                                case 0xfffffffe:
809 >                                        // Return first supported resolution
810 >                                        id = 0x80;
811 >                                        while (!has_resolution(id))
812 >                                                id++;
813 >                                        break;
814 >
815 >                                default:
816 >                                        // Get next resolution
817 >                                        if (!has_resolution(id))
818 >                                                return paramErr;
819 >                                        id++;
820 >                                        while (!has_resolution(id) && id < 0x100)
821 >                                                id++;
822 >                                        if (id == 0x100) { // No more resolutions
823 >                                                WriteMacInt32(param + csRIDisplayModeID, 0xfffffffd);
824 >                                                return noErr;
825 >                                        }
826 >                                        break;
827 >                        }
828 >
829 >                        WriteMacInt32(param + csRIDisplayModeID, id);
830 >                        uint32 x, y;
831 >                        get_size_of_resolution(id, x, y);
832 >                        WriteMacInt32(param + csHorizontalPixels, x);
833 >                        WriteMacInt32(param + csVerticalLines, y);
834 >                        WriteMacInt32(param + csRefreshRate, 75 << 16);
835 >                        WriteMacInt16(param + csMaxDepthMode, DepthToAppleMode(max_depth_of_resolution(id)));
836 >                        WriteMacInt32(param + csResolutionFlags, 0);
837                          return noErr;
838 +                }
839 +
840 +                case cscGetVideoParameters: {   // Get information about specified resolution/depth
841 +                        uint32 id = ReadMacInt32(param + csDisplayModeID);
842 +                        uint16 mode = ReadMacInt16(param + csDepthMode);
843 +                        D(bug(" GetVideoParameters %04x/%08x\n", mode, id));
844 +                        VidLocal.dm_present = true;     // Display Manager seems to be present
845 +
846 +                        vector<video_mode>::const_iterator i, end = VideoModes.end();
847 +                        for (i = VideoModes.begin(); i != end; ++i) {
848 +                                if (DepthToAppleMode(i->depth) == mode && i->resolution_id == id) {
849 +                                        uint32 vp = ReadMacInt32(param + csVPBlockPtr);
850 +                                        WriteMacInt32(vp + vpBaseOffset, 0);
851 +                                        WriteMacInt16(vp + vpRowBytes, i->bytes_per_row);
852 +                                        WriteMacInt16(vp + vpBounds, 0);
853 +                                        WriteMacInt16(vp + vpBounds + 2, 0);
854 +                                        WriteMacInt16(vp + vpBounds + 4, i->y);
855 +                                        WriteMacInt16(vp + vpBounds + 6, i->x);
856 +                                        WriteMacInt16(vp + vpVersion, 0);
857 +                                        WriteMacInt16(vp + vpPackType, 0);
858 +                                        WriteMacInt32(vp + vpPackSize, 0);
859 +                                        WriteMacInt32(vp + vpHRes, 0x00480000); // 72 dpi
860 +                                        WriteMacInt32(vp + vpVRes, 0x00480000);
861 +                                        uint32 pix_type, pix_size, cmp_count, cmp_size, dev_type;
862 +                                        switch (i->depth) {
863 +                                                case VDEPTH_1BIT:
864 +                                                        pix_type = 0; pix_size = 1;
865 +                                                        cmp_count = 1; cmp_size = 1;
866 +                                                        dev_type = 0; // CLUT
867 +                                                        break;
868 +                                                case VDEPTH_2BIT:
869 +                                                        pix_type = 0; pix_size = 2;
870 +                                                        cmp_count = 1; cmp_size = 2;
871 +                                                        dev_type = 0; // CLUT
872 +                                                        break;
873 +                                                case VDEPTH_4BIT:
874 +                                                        pix_type = 0; pix_size = 4;
875 +                                                        cmp_count = 1; cmp_size = 4;
876 +                                                        dev_type = 0; // CLUT
877 +                                                        break;
878 +                                                case VDEPTH_8BIT:
879 +                                                        pix_type = 0; pix_size = 8;
880 +                                                        cmp_count = 1; cmp_size = 8;
881 +                                                        dev_type = 0; // CLUT
882 +                                                        break;
883 +                                                case VDEPTH_16BIT:
884 +                                                        pix_type = 0x10; pix_size = 16;
885 +                                                        cmp_count = 3; cmp_size = 5;
886 +                                                        dev_type = 2; // direct
887 +                                                        break;
888 +                                                case VDEPTH_32BIT:
889 +                                                        pix_type = 0x10; pix_size = 32;
890 +                                                        cmp_count = 3; cmp_size = 8;
891 +                                                        dev_type = 2; // direct
892 +                                                        break;
893 +                                        }
894 +                                        WriteMacInt16(vp + vpPixelType, pix_type);
895 +                                        WriteMacInt16(vp + vpPixelSize, pix_size);
896 +                                        WriteMacInt16(vp + vpCmpCount, cmp_count);
897 +                                        WriteMacInt16(vp + vpCmpSize, cmp_size);
898 +                                        WriteMacInt32(param + csPageCount, 1);
899 +                                        WriteMacInt32(param + csDeviceType, dev_type);
900 +                                        return noErr;
901 +                                }
902 +                        }
903 +                        return paramErr; // specified resolution/depth not supported
904 +                }
905 +
906 +                case cscGetMultiConnect: {
907 +                        uint32 conn = ReadMacInt32(param + csDisplayCountOrNumber);
908 +                        D(bug(" GetMultiConnect %08x\n", conn));
909 +                        if (conn == 0xffffffff) {       // Get number of connections
910 +                                WriteMacInt32(param + csDisplayCountOrNumber, 1); // Single-headed
911 +                                return noErr;
912 +                        } else if (conn == 1) {         // Get information about first connection
913 +                                WriteMacInt16(param + csConnectInfo + csDisplayType, 8);                // Modeless connection
914 +                                WriteMacInt8(param + csConnectInfo + csConnectTaggedType, 0);
915 +                                WriteMacInt8(param + csConnectInfo + csConnectTaggedData, 0);
916 +                                WriteMacInt32(param + csConnectInfo + csConnectFlags, 0x43);    // All modes valid and safe, non-standard tagging
917 +                                WriteMacInt32(param + csConnectInfo + csDisplayComponent, 0);
918 +                                return noErr;
919 +                        } else
920 +                                return paramErr;
921 +                }
922  
923                  default:
924                          printf("WARNING: Unknown VideoDriverStatus(%d)\n", code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines