ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/video.cpp
Revision: 1.24
Committed: 2002-01-15T14:58:32Z (22 years, 10 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-15012002
Changes since 1.23: +2 -2 lines
Log Message:
- documentation updates
- 2001 -> 2002
- version 0.9 -> 1.0

File Contents

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