ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/video.cpp
Revision: 1.20
Committed: 2001-07-03T19:20:38Z (23 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.19: +72 -6 lines
Log Message:
- improved compatibility of multiple depth/resolution handling with versions
  of MacOS before 7.6:
   - Apple mode IDs are now allocated contiguously from 0x80 (the video_*.cpp
     module must call video_init_depth_list() after adding all modes)
   - if the video driver didn't receive a GetVideoParameters call, it patches
     ScrnBase and the main GDevice upon a video mode switch (otherwise MacOS
     will continue to use the old frame buffer base)
   - the rowBytes values in the video parameters slot resources are correct
     for all bit depths

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video.cpp - Video/graphics emulation
3     *
4 cebix 1.7 * Basilisk II (C) 1997-2001 Christian Bauer
5 cebix 1.1 * Portions (C) 1997-1999 Marc Hellwig
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     /*
23     * SEE ALSO
24     * Inside Macintosh: Devices, chapter 1 "Device Manager"
25     * Designing Cards and Drivers for the Macintosh Family, Second Edition
26     */
27    
28     #include <stdio.h>
29    
30     #include "sysdeps.h"
31     #include "cpu_emulation.h"
32     #include "main.h"
33     #include "macos_util.h"
34 cebix 1.13 #include "slot_rom.h"
35 cebix 1.1 #include "video.h"
36     #include "video_defs.h"
37    
38 cebix 1.11 #define DEBUG 0
39 cebix 1.1 #include "debug.h"
40    
41    
42 cebix 1.9 // List of supported video modes
43 cebix 1.17 vector<video_mode> VideoModes;
44 cebix 1.9
45 cebix 1.1 // Description of the main monitor
46 cebix 1.9 monitor_desc VideoMonitor;
47 cebix 1.1
48 cebix 1.20 // Depth code -> Apple mode mapping
49     uint16 apple_mode_for_depth[6];
50    
51 cebix 1.1 // Local variables (per monitor)
52     struct {
53 cebix 1.9 monitor_desc *desc; // Pointer to description of monitor handled by this instance of the driver
54 cebix 1.1 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 cebix 1.20 bool dm_present; // We received a GetVideoParameters call, so the Display Manager seems to be present
58 cebix 1.11 uint32 gamma_table; // Mac address of gamma table
59 cebix 1.14 int alloc_gamma_table_size; // Allocated size of gamma table
60 cebix 1.9 uint16 current_mode; // Currently selected depth/resolution
61     uint32 current_id;
62     uint16 preferred_mode; // Preferred depth/resolution
63     uint32 preferred_id;
64 cebix 1.16 uint32 slot_param; // Mac address of Slot Manager parameter block
65 cebix 1.1 } VidLocal;
66    
67    
68     /*
69 cebix 1.20 * 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 cebix 1.9 * Check whether specified resolution ID is one of the supported resolutions
102     */
103    
104     static bool has_resolution(uint32 id)
105     {
106 cebix 1.18 vector<video_mode>::const_iterator i, end = VideoModes.end();
107     for (i = VideoModes.begin(); i != end; ++i) {
108 cebix 1.9 if (i->resolution_id == id)
109     return true;
110     }
111     return false;
112     }
113    
114    
115     /*
116 cebix 1.10 * Find specified mode (depth/resolution) (or VideoModes.end() if not found)
117     */
118    
119 cebix 1.17 static vector<video_mode>::const_iterator find_mode(uint16 mode, uint32 id)
120 cebix 1.10 {
121 cebix 1.18 vector<video_mode>::const_iterator i, end = VideoModes.end();
122     for (i = VideoModes.begin(); i != end; ++i) {
123 cebix 1.10 if (i->resolution_id == id && DepthToAppleMode(i->depth) == mode)
124     return i;
125     }
126     return i;
127     }
128    
129    
130     /*
131 cebix 1.9 * 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 cebix 1.18 vector<video_mode>::const_iterator i, end = VideoModes.end();
138     for (i = VideoModes.begin(); i != end; ++i) {
139 cebix 1.9 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 cebix 1.18 vector<video_mode>::const_iterator i, end = VideoModes.end();
153     for (i = VideoModes.begin(); i != end; ++i) {
154 cebix 1.9 if (i->resolution_id == id) {
155     x = i->x;
156     y = i->y;
157     return;
158     }
159     }
160 cebix 1.20 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 cebix 1.9 }
179    
180    
181     /*
182 cebix 1.19 * 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 cebix 1.11 * Set palette to 50% gray
201     */
202    
203     static void set_gray_palette(void)
204     {
205 cebix 1.14 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 cebix 1.19 video_set_palette(VidLocal.palette, 256);
211 cebix 1.14 }
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 cebix 1.15 uint8 red = (i * 256 / num), green = red, blue = red;
243 cebix 1.14 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 cebix 1.19 video_set_palette(VidLocal.palette, num);
254 cebix 1.14 }
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 cebix 1.11 }
272 cebix 1.14 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 cebix 1.11 }
279 cebix 1.14 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 cebix 1.20 if (IsDirectMode(VidLocal.desc->mode))
337 cebix 1.14 load_ramp_palette();
338    
339     return true;
340 cebix 1.11 }
341    
342    
343     /*
344 cebix 1.18 * 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 cebix 1.20
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     uint32 gdev = ReadMacInt32(0x8a4); // MainDevice
413     gdev = ReadMacInt32(gdev);
414     uint32 pmap = ReadMacInt32(gdev + 0x16); // gdPMap
415     pmap = ReadMacInt32(pmap);
416     WriteMacInt32(pmap, frame_base); // baseAddr
417     }
418 cebix 1.18 }
419    
420    
421     /*
422 cebix 1.1 * Driver Open() routine
423     */
424    
425 cebix 1.2 int16 VideoDriverOpen(uint32 pb, uint32 dce)
426 cebix 1.1 {
427 cebix 1.2 D(bug("VideoDriverOpen\n"));
428 cebix 1.1
429 cebix 1.9 // This shouldn't happen unless the platform-specific video code is broken
430     if (VideoModes.empty())
431     fprintf(stderr, "No valid video modes found (broken video driver?)\n");
432    
433 cebix 1.1 // Init local variables
434     VidLocal.desc = &VideoMonitor;
435     VidLocal.luminance_mapping = false;
436     VidLocal.interrupts_enabled = false;
437 cebix 1.9 VidLocal.current_mode = DepthToAppleMode(VidLocal.desc->mode.depth);
438     VidLocal.current_id = VidLocal.desc->mode.resolution_id;
439     VidLocal.preferred_mode = VidLocal.current_mode;
440     VidLocal.preferred_id = VidLocal.current_id;
441 cebix 1.20 VidLocal.dm_present = false;
442 cebix 1.1
443 cebix 1.11 // Allocate Slot Manager parameter block in Mac RAM
444     M68kRegisters r;
445     r.d[0] = SIZEOF_SPBlock;
446     Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
447     if (r.a[0] == 0)
448     return memFullErr;
449 cebix 1.16 VidLocal.slot_param = r.a[0];
450     D(bug("SPBlock at %08x\n", VidLocal.slot_param));
451 cebix 1.11
452     // Find and set default gamma table
453 cebix 1.14 VidLocal.gamma_table = 0;
454     VidLocal.alloc_gamma_table_size = 0;
455     set_gamma_table(0);
456 cebix 1.11
457 cebix 1.1 // Init color palette (solid gray)
458 cebix 1.11 set_gray_palette();
459 cebix 1.1 return noErr;
460     }
461    
462    
463     /*
464     * Driver Control() routine
465     */
466    
467 cebix 1.2 int16 VideoDriverControl(uint32 pb, uint32 dce)
468 cebix 1.1 {
469     uint16 code = ReadMacInt16(pb + csCode);
470     uint32 param = ReadMacInt32(pb + csParam);
471 cebix 1.2 D(bug("VideoDriverControl %d\n", code));
472 cebix 1.1 switch (code) {
473    
474 cebix 1.9 case cscSetMode: { // Set color depth
475     uint16 mode = ReadMacInt16(param + csMode);
476     D(bug(" SetMode %04x\n", mode));
477 cebix 1.10
478 cebix 1.11 // Set old base address in case the switch fails
479     WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
480    
481     if (ReadMacInt16(param + csPage))
482     return paramErr;
483    
484 cebix 1.10 if (mode != VidLocal.current_mode) {
485 cebix 1.17 vector<video_mode>::const_iterator i = find_mode(mode, VidLocal.current_id);
486 cebix 1.11 if (i == VideoModes.end())
487 cebix 1.10 return paramErr;
488 cebix 1.18 switch_mode(*i, param, dce);
489 cebix 1.10 }
490 cebix 1.11 D(bug(" base %08x\n", VidLocal.desc->mac_frame_base));
491 cebix 1.10 return noErr;
492 cebix 1.9 }
493 cebix 1.1
494 cebix 1.14 case cscSetEntries: // Set palette
495     case cscDirectSetEntries: {
496     D(bug(" (Direct)SetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart)));
497 cebix 1.20 bool is_direct = IsDirectMode(VidLocal.desc->mode);
498 cebix 1.14 if (code == cscSetEntries && is_direct)
499     return controlErr;
500     if (code == cscDirectSetEntries && !is_direct)
501 cebix 1.1 return controlErr;
502    
503     uint32 s_pal = ReadMacInt32(param + csTable); // Source palette
504     uint8 *d_pal; // Destination palette
505 cebix 1.11 uint16 start = ReadMacInt16(param + csStart);
506 cebix 1.1 uint16 count = ReadMacInt16(param + csCount);
507 cebix 1.11 if (s_pal == 0 || count > 255)
508 cebix 1.1 return paramErr;
509    
510 cebix 1.14 // Find tables for gamma correction
511     uint8 *red_gamma, *green_gamma, *blue_gamma;
512     bool have_gamma = false;
513     int data_width = 0;
514     if (VidLocal.gamma_table) {
515     uint32 table = VidLocal.gamma_table;
516     red_gamma = Mac2HostAddr(table + gFormulaData + ReadMacInt16(table + gFormulaSize));
517     int chan_cnt = ReadMacInt16(table + gChanCnt);
518     if (chan_cnt == 1)
519     green_gamma = blue_gamma = red_gamma;
520     else {
521     int ofs = ReadMacInt16(table + gDataCnt);
522     green_gamma = red_gamma + ofs;
523     blue_gamma = green_gamma + ofs;
524     }
525     data_width = ReadMacInt16(table + gDataWidth);
526     have_gamma = true;
527     }
528    
529     // Convert palette
530 cebix 1.11 if (start == 0xffff) { // Indexed
531 cebix 1.1 for (uint32 i=0; i<=count; i++) {
532 cebix 1.11 d_pal = VidLocal.palette + (ReadMacInt16(s_pal) & 0xff) * 3;
533 cebix 1.1 uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8;
534     uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8;
535     uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8;
536 cebix 1.14 if (VidLocal.luminance_mapping && !is_direct)
537 cebix 1.1 red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16;
538 cebix 1.14 if (have_gamma) {
539     red = red_gamma[red >> (8 - data_width)];
540     green = green_gamma[green >> (8 - data_width)];
541     blue = blue_gamma[blue >> (8 - data_width)];
542     }
543 cebix 1.1 *d_pal++ = red;
544     *d_pal++ = green;
545     *d_pal++ = blue;
546     s_pal += 8;
547     }
548 cebix 1.11 } else { // Sequential
549     if (start + count > 255)
550     return paramErr;
551     d_pal = VidLocal.palette + start * 3;
552 cebix 1.1 for (uint32 i=0; i<=count; i++) {
553     uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8;
554     uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8;
555     uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8;
556 cebix 1.14 if (VidLocal.luminance_mapping && !is_direct)
557 cebix 1.1 red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16;
558 cebix 1.14 if (have_gamma) {
559     red = red_gamma[red >> (8 - data_width)];
560     green = green_gamma[green >> (8 - data_width)];
561     blue = blue_gamma[blue >> (8 - data_width)];
562     }
563 cebix 1.1 *d_pal++ = red;
564     *d_pal++ = green;
565     *d_pal++ = blue;
566     s_pal += 8;
567     }
568     }
569 cebix 1.19 video_set_palette(VidLocal.palette, palette_size(VidLocal.desc->mode.depth));
570 cebix 1.1 return noErr;
571     }
572    
573 cebix 1.14 case cscSetGamma: { // Set gamma table
574     uint32 user_table = ReadMacInt32(param + csGTable);
575     D(bug(" SetGamma %08x\n", user_table));
576     return set_gamma_table(user_table) ? noErr : memFullErr;
577     }
578 cebix 1.1
579     case cscGrayPage: { // Fill page with dithered gray pattern
580     D(bug(" GrayPage %d\n", ReadMacInt16(param + csPage)));
581     if (ReadMacInt16(param + csPage))
582     return paramErr;
583    
584     uint32 pattern[6] = {
585     0xaaaaaaaa, // 1 bpp
586     0xcccccccc, // 2 bpp
587     0xf0f0f0f0, // 4 bpp
588     0xff00ff00, // 8 bpp
589     0xffff0000, // 16 bpp
590     0xffffffff // 32 bpp
591     };
592 cebix 1.20 uint32 frame_base = VidLocal.desc->mac_frame_base;
593 cebix 1.9 uint32 pat = pattern[VidLocal.desc->mode.depth];
594 cebix 1.11 bool invert = (VidLocal.desc->mode.depth == VDEPTH_32BIT);
595 cebix 1.9 for (uint32 y=0; y<VidLocal.desc->mode.y; y++) {
596     for (uint32 x=0; x<VidLocal.desc->mode.bytes_per_row; x+=4) {
597 cebix 1.20 WriteMacInt32(frame_base + x, pat);
598 cebix 1.11 if (invert)
599 cebix 1.1 pat = ~pat;
600     }
601 cebix 1.20 frame_base += VidLocal.desc->mode.bytes_per_row;
602 cebix 1.1 pat = ~pat;
603     }
604 cebix 1.14
605 cebix 1.20 if (IsDirectMode(VidLocal.desc->mode))
606 cebix 1.14 load_ramp_palette();
607    
608 cebix 1.1 return noErr;
609     }
610    
611     case cscSetGray: // Enable/disable luminance mapping
612     D(bug(" SetGray %02x\n", ReadMacInt8(param + csMode)));
613     VidLocal.luminance_mapping = ReadMacInt8(param + csMode);
614     return noErr;
615    
616     case cscSetInterrupt: // Enable/disable VBL
617     D(bug(" SetInterrupt %02x\n", ReadMacInt8(param + csMode)));
618     VidLocal.interrupts_enabled = (ReadMacInt8(param + csMode) == 0);
619     return noErr;
620    
621 cebix 1.9 case cscSetDefaultMode: { // Set default color depth
622     uint16 mode = ReadMacInt16(param + csMode);
623     D(bug(" SetDefaultMode %04x\n", mode));
624     VidLocal.preferred_mode = mode;
625     return noErr;
626     }
627    
628     case cscSwitchMode: { // Switch video mode (depth and resolution)
629     uint16 mode = ReadMacInt16(param + csMode);
630     uint32 id = ReadMacInt32(param + csData);
631     D(bug(" SwitchMode %04x, %08x\n", mode, id));
632 cebix 1.10
633 cebix 1.11 // Set old base address in case the switch fails
634     WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
635    
636     if (ReadMacInt16(param + csPage))
637     return paramErr;
638    
639 cebix 1.10 if (mode != VidLocal.current_mode || id != VidLocal.current_id) {
640 cebix 1.17 vector<video_mode>::const_iterator i = find_mode(mode, id);
641 cebix 1.11 if (i == VideoModes.end())
642 cebix 1.10 return paramErr;
643 cebix 1.18 switch_mode(*i, param, dce);
644 cebix 1.10 }
645 cebix 1.11 D(bug(" base %08x\n", VidLocal.desc->mac_frame_base));
646 cebix 1.10 return noErr;
647 cebix 1.9 }
648    
649     case cscSavePreferredConfiguration: {
650     uint16 mode = ReadMacInt16(param + csMode);
651     uint32 id = ReadMacInt32(param + csData);
652     D(bug(" SavePreferredConfiguration %04x, %08x\n", mode, id));
653     VidLocal.preferred_mode = mode;
654     VidLocal.preferred_id = id;
655     return noErr;
656     }
657    
658 cebix 1.1 default:
659 cebix 1.2 printf("WARNING: Unknown VideoDriverControl(%d)\n", code);
660 cebix 1.1 return controlErr;
661     }
662     }
663    
664    
665     /*
666     * Driver Status() routine
667     */
668    
669 cebix 1.2 int16 VideoDriverStatus(uint32 pb, uint32 dce)
670 cebix 1.1 {
671     uint16 code = ReadMacInt16(pb + csCode);
672     uint32 param = ReadMacInt32(pb + csParam);
673 cebix 1.2 D(bug("VideoDriverStatus %d\n", code));
674 cebix 1.1 switch (code) {
675    
676 cebix 1.9 case cscGetMode: // Get current color depth
677     D(bug(" GetMode -> %04x, base %08x\n", VidLocal.current_mode, VidLocal.desc->mac_frame_base));
678     WriteMacInt16(param + csMode, VidLocal.current_mode);
679     WriteMacInt16(param + csPage, 0);
680     WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
681     return noErr;
682    
683 cebix 1.11 case cscGetEntries: { // Read palette
684     D(bug(" GetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart)));
685 cebix 1.9
686 cebix 1.11 uint8 *s_pal; // Source palette
687     uint32 d_pal = ReadMacInt32(param + csTable); // Destination palette
688     uint16 start = ReadMacInt16(param + csStart);
689     uint16 count = ReadMacInt16(param + csCount);
690     if (d_pal == 0 || count > 255)
691     return paramErr;
692    
693     if (start == 0xffff) { // Indexed
694     for (uint32 i=0; i<=count; i++) {
695     s_pal = VidLocal.palette + (ReadMacInt16(d_pal) & 0xff) * 3;
696     uint8 red = *s_pal++;
697     uint8 green = *s_pal++;
698     uint8 blue = *s_pal++;
699     WriteMacInt16(d_pal + 2, red * 0x0101);
700     WriteMacInt16(d_pal + 4, green * 0x0101);
701     WriteMacInt16(d_pal + 6, blue * 0x0101);
702     d_pal += 8;
703     }
704     } else { // Sequential
705     if (start + count > 255)
706     return paramErr;
707     s_pal = VidLocal.palette + start * 3;
708     for (uint32 i=0; i<=count; i++) {
709     uint8 red = *s_pal++;
710     uint8 green = *s_pal++;
711     uint8 blue = *s_pal++;
712     WriteMacInt16(d_pal + 2, red * 0x0101);
713     WriteMacInt16(d_pal + 4, green * 0x0101);
714     WriteMacInt16(d_pal + 6, blue * 0x0101);
715     d_pal += 8;
716     }
717     }
718     return noErr;
719     }
720    
721     case cscGetPages: // Get number of pages
722     D(bug(" GetPages -> 1\n"));
723 cebix 1.1 WriteMacInt16(param + csPage, 1);
724     return noErr;
725    
726 cebix 1.11 case cscGetBaseAddress: // Get page base address
727     D(bug(" GetBaseAddress -> %08x\n", VidLocal.desc->mac_frame_base));
728 cebix 1.1 WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
729 cebix 1.11 if (ReadMacInt16(param + csPage))
730     return paramErr;
731     else
732     return noErr;
733 cebix 1.1
734     case cscGetGray: // Get luminance mapping flag
735 cebix 1.9 D(bug(" GetGray -> %d\n", VidLocal.luminance_mapping));
736 cebix 1.11 WriteMacInt16(param, VidLocal.luminance_mapping ? 0x0100 : 0);
737 cebix 1.1 return noErr;
738    
739     case cscGetInterrupt: // Get interrupt disable flag
740 cebix 1.9 D(bug(" GetInterrupt -> %d\n", VidLocal.interrupts_enabled));
741 cebix 1.11 WriteMacInt16(param, VidLocal.interrupts_enabled ? 0 : 0x0100);
742 cebix 1.1 return noErr;
743    
744 cebix 1.11 case cscGetGamma:
745 cebix 1.14 D(bug(" GetGamma -> %08x\n", VidLocal.gamma_table));
746     WriteMacInt32(param + csGTable, VidLocal.gamma_table);
747     return noErr;
748 cebix 1.9
749     case cscGetDefaultMode: // Get default color depth
750     D(bug(" GetDefaultMode -> %04x\n", VidLocal.preferred_mode));
751     WriteMacInt16(param + csMode, VidLocal.preferred_mode);
752 cebix 1.1 return noErr;
753    
754 cebix 1.11 case cscGetCurrentMode: // Get current video mode (depth and resolution)
755     D(bug(" GetCurMode -> %04x/%08x, base %08x\n", VidLocal.current_mode, VidLocal.current_id, VidLocal.desc->mac_frame_base));
756 cebix 1.9 WriteMacInt16(param + csMode, VidLocal.current_mode);
757     WriteMacInt32(param + csData, VidLocal.current_id);
758 cebix 1.1 WriteMacInt16(param + csPage, 0);
759     WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
760     return noErr;
761    
762     case cscGetConnection: // Get monitor information
763     D(bug(" GetConnection\n"));
764 cebix 1.11 WriteMacInt16(param + csDisplayType, 8); // Modeless connection
765     WriteMacInt8(param + csConnectTaggedType, 0);
766     WriteMacInt8(param + csConnectTaggedData, 0);
767     WriteMacInt32(param + csConnectFlags, 0x43); // All modes valid and safe, non-standard tagging
768 cebix 1.1 WriteMacInt32(param + csDisplayComponent, 0);
769     return noErr;
770    
771 cebix 1.11 case cscGetModeTiming: { // Get video timing for specified resolution
772     uint32 id = ReadMacInt32(param + csTimingMode);
773     D(bug(" GetModeTiming %08x\n", id));
774     if (!has_resolution(id))
775     return paramErr;
776    
777     WriteMacInt32(param + csTimingFormat, FOURCC('d', 'e', 'c', 'l'));
778     WriteMacInt32(param + csTimingData, 0); // unknown
779     uint32 flags = 0xb; // mode valid, safe and shown in Monitors panel
780     if (id == VidLocal.preferred_id)
781     flags |= 4; // default mode
782     WriteMacInt32(param + csTimingFlags, flags);
783     return noErr;
784     }
785    
786 cebix 1.9 case cscGetModeBaseAddress: // Get frame buffer base address
787 cebix 1.11 D(bug(" GetModeBaseAddress -> base %08x\n", VidLocal.desc->mac_frame_base));
788     WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base);
789 cebix 1.1 return noErr;
790    
791 cebix 1.9 case cscGetPreferredConfiguration: // Get default video mode (depth and resolution)
792     D(bug(" GetPreferredConfiguration -> %04x/%08x\n", VidLocal.preferred_mode, VidLocal.preferred_id));
793     WriteMacInt16(param + csMode, VidLocal.preferred_mode);
794     WriteMacInt32(param + csData, VidLocal.preferred_id);
795     return noErr;
796    
797     case cscGetNextResolution: { // Called iteratively to obtain a list of all supported resolutions
798     uint32 id = ReadMacInt32(param + csPreviousDisplayModeID);
799     D(bug(" GetNextResolution %08x\n", id));
800    
801     switch (id) {
802     case 0:
803     // Return current resolution
804     id = VidLocal.current_id;
805     break;
806    
807     case 0xfffffffe:
808     // Return first supported resolution
809     id = 0x80;
810     while (!has_resolution(id))
811     id++;
812     break;
813    
814     default:
815     // Get next resolution
816     if (!has_resolution(id))
817     return paramErr;
818     id++;
819     while (!has_resolution(id) && id < 0x100)
820     id++;
821     if (id == 0x100) { // No more resolutions
822     WriteMacInt32(param + csRIDisplayModeID, 0xfffffffd);
823     return noErr;
824     }
825     break;
826     }
827    
828     WriteMacInt32(param + csRIDisplayModeID, id);
829     uint32 x, y;
830     get_size_of_resolution(id, x, y);
831     WriteMacInt32(param + csHorizontalPixels, x);
832     WriteMacInt32(param + csVerticalLines, y);
833     WriteMacInt32(param + csRefreshRate, 75 << 16);
834     WriteMacInt16(param + csMaxDepthMode, DepthToAppleMode(max_depth_of_resolution(id)));
835 jlachmann 1.5 return noErr;
836 cebix 1.9 }
837 jlachmann 1.5
838 cebix 1.9 case cscGetVideoParameters: { // Get information about specified resolution/depth
839     uint32 id = ReadMacInt32(param + csDisplayModeID);
840     uint16 mode = ReadMacInt16(param + csDepthMode);
841     D(bug(" GetVideoParameters %04x/%08x\n", mode, id));
842 cebix 1.20 VidLocal.dm_present = true; // Display Manager seems to be present
843 cebix 1.9
844 cebix 1.18 vector<video_mode>::const_iterator i, end = VideoModes.end();
845     for (i = VideoModes.begin(); i != end; ++i) {
846 cebix 1.9 if (DepthToAppleMode(i->depth) == mode && i->resolution_id == id) {
847     uint32 vp = ReadMacInt32(param + csVPBlockPtr);
848     WriteMacInt32(vp + vpBaseOffset, 0);
849     WriteMacInt16(vp + vpRowBytes, i->bytes_per_row);
850     WriteMacInt16(vp + vpBounds, 0);
851     WriteMacInt16(vp + vpBounds + 2, 0);
852     WriteMacInt16(vp + vpBounds + 4, i->y);
853     WriteMacInt16(vp + vpBounds + 6, i->x);
854     WriteMacInt16(vp + vpVersion, 0);
855     WriteMacInt16(vp + vpPackType, 0);
856     WriteMacInt32(vp + vpPackSize, 0);
857 cebix 1.11 WriteMacInt32(vp + vpHRes, 0x00480000); // 72 dpi
858 cebix 1.9 WriteMacInt32(vp + vpVRes, 0x00480000);
859     uint32 pix_type, pix_size, cmp_count, cmp_size, dev_type;
860     switch (i->depth) {
861     case VDEPTH_1BIT:
862     pix_type = 0; pix_size = 1;
863     cmp_count = 1; cmp_size = 1;
864     dev_type = 0; // CLUT
865     break;
866     case VDEPTH_2BIT:
867     pix_type = 0; pix_size = 2;
868     cmp_count = 1; cmp_size = 2;
869     dev_type = 0; // CLUT
870     break;
871     case VDEPTH_4BIT:
872     pix_type = 0; pix_size = 4;
873     cmp_count = 1; cmp_size = 4;
874     dev_type = 0; // CLUT
875     break;
876     case VDEPTH_8BIT:
877     pix_type = 0; pix_size = 8;
878     cmp_count = 1; cmp_size = 8;
879     dev_type = 0; // CLUT
880     break;
881     case VDEPTH_16BIT:
882     pix_type = 0x10; pix_size = 16;
883     cmp_count = 3; cmp_size = 5;
884     dev_type = 2; // direct
885     break;
886     case VDEPTH_32BIT:
887     pix_type = 0x10; pix_size = 32;
888     cmp_count = 3; cmp_size = 8;
889     dev_type = 2; // direct
890     break;
891     }
892     WriteMacInt16(vp + vpPixelType, pix_type);
893     WriteMacInt16(vp + vpPixelSize, pix_size);
894     WriteMacInt16(vp + vpCmpCount, cmp_count);
895     WriteMacInt16(vp + vpCmpSize, cmp_size);
896     WriteMacInt32(param + csPageCount, 1);
897     WriteMacInt32(param + csDeviceType, dev_type);
898     return noErr;
899     }
900     }
901     return paramErr; // specified resolution/depth not supported
902     }
903 cebix 1.1
904     default:
905 cebix 1.2 printf("WARNING: Unknown VideoDriverStatus(%d)\n", code);
906 cebix 1.1 return statusErr;
907     }
908     }