--- BasiliskII/src/video.cpp 2001/07/01 21:09:26 1.19 +++ BasiliskII/src/video.cpp 2005/01/30 21:42:13 1.28 @@ -1,8 +1,8 @@ /* * video.cpp - Video/graphics emulation * - * Basilisk II (C) 1997-2001 Christian Bauer - * Portions (C) 1997-1999 Marc Hellwig + * Basilisk II (C) 1997-2005 Christian Bauer + * Portions written by Marc Hellwig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,8 @@ * SEE ALSO * Inside Macintosh: Devices, chapter 1 "Device Manager" * Designing Cards and Drivers for the Macintosh Family, Second Edition + * Designing PCI Cards and Drivers for Power Macintosh Computers + * Display Device Driver Guide */ #include @@ -39,36 +41,111 @@ #include "debug.h" -// List of supported video modes -vector VideoModes; +// Next available NuBus slot ID +uint8 monitor_desc::next_slot_id = 0x80; -// Description of the main monitor -monitor_desc VideoMonitor; +// Vector of pointers to available monitor descriptions, filled by VideoInit() +vector VideoMonitors; -// Local variables (per monitor) -struct { - monitor_desc *desc; // Pointer to description of monitor handled by this instance of the driver - uint8 palette[256 * 3]; // Color palette, 256 entries, RGB - bool luminance_mapping; // Luminance mapping on/off - bool interrupts_enabled; // VBL interrupts on/off - uint32 gamma_table; // Mac address of gamma table - int alloc_gamma_table_size; // Allocated size of gamma table - uint16 current_mode; // Currently selected depth/resolution - uint32 current_id; - uint16 preferred_mode; // Preferred depth/resolution - uint32 preferred_id; - uint32 slot_param; // Mac address of Slot Manager parameter block -} VidLocal; + +/* + * Find palette size for given color depth + */ + +static int palette_size(video_depth depth) +{ + switch (depth) { + case VDEPTH_1BIT: return 2; + case VDEPTH_2BIT: return 4; + case VDEPTH_4BIT: return 16; + case VDEPTH_8BIT: return 256; + case VDEPTH_16BIT: return 32; + case VDEPTH_32BIT: return 256; + default: return 0; + } +} + + +/* + * Find pointer to monitor_desc for given slot ID (or NULL if not found) + */ + +static monitor_desc *find_monitor(uint8 id) +{ + vector::const_iterator i, end = VideoMonitors.end(); + for (i = VideoMonitors.begin(); i != end; ++i) { + if ((*i)->get_slot_id() == id) + return *i; + } + return NULL; +} + + +/* + * monitor_desc constructor + */ + +monitor_desc::monitor_desc(const vector &available_modes, video_depth default_depth, uint32 default_id) : modes(available_modes) +{ + // Assign the next slot ID on construction + slot_id = next_slot_id++; + + // Initialize Apple mode list + uint16 mode = 0x80; + for (int depth = VDEPTH_1BIT; depth <= VDEPTH_32BIT; depth++) { + if (has_depth(video_depth(depth))) + apple_mode_for_depth[depth] = mode++; + else + apple_mode_for_depth[depth] = 0; + } + + // Set default mode + current_mode = find_mode(depth_to_apple_mode(default_depth), default_id); +} + + +/* + * Get bytes-per-row value for specified resolution/depth + * (if the mode isn't supported, make a good guess) + */ + +uint32 monitor_desc::get_bytes_per_row(video_depth depth, uint32 id) const +{ + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { + if (i->depth == depth && i->resolution_id == id) + return i->bytes_per_row; + } + uint32 x, y; + get_size_of_resolution(id, x, y); + return TrivialBytesPerRow(x, depth); +} + + +/* + * Check whether a mode with the specified depth exists on this display + */ + +bool monitor_desc::has_depth(video_depth depth) const +{ + vector::const_iterator i = modes.begin(), end = modes.end(); + while (i != end) { + if (i->depth == depth) + return true; + ++i; + } + return false; +} /* - * Check whether specified resolution ID is one of the supported resolutions + * Check whether the specified resolution ID is one of the supported resolutions */ -static bool has_resolution(uint32 id) +bool monitor_desc::has_resolution(uint32 id) const { - vector::const_iterator i, end = VideoModes.end(); - for (i = VideoModes.begin(); i != end; ++i) { + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { if (i->resolution_id == id) return true; } @@ -77,14 +154,14 @@ static bool has_resolution(uint32 id) /* - * Find specified mode (depth/resolution) (or VideoModes.end() if not found) + * Find specified mode (depth/resolution) (or invalid_mode() if not found) */ -static vector::const_iterator find_mode(uint16 mode, uint32 id) +vector::const_iterator monitor_desc::find_mode(uint16 apple_mode, uint32 id) const { - vector::const_iterator i, end = VideoModes.end(); - for (i = VideoModes.begin(); i != end; ++i) { - if (i->resolution_id == id && DepthToAppleMode(i->depth) == mode) + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { + if (i->resolution_id == id && depth_to_apple_mode(i->depth) == apple_mode) return i; } return i; @@ -95,11 +172,11 @@ static vector::const_iterato * Find maximum supported depth for given resolution ID */ -static video_depth max_depth_of_resolution(uint32 id) +video_depth monitor_desc::max_depth_of_resolution(uint32 id) const { video_depth m = VDEPTH_1BIT; - vector::const_iterator i, end = VideoModes.end(); - for (i = VideoModes.begin(); i != end; ++i) { + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { if (i->depth > m) m = i->depth; } @@ -111,34 +188,17 @@ static video_depth max_depth_of_resoluti * Get X/Y size of specified resolution */ -static void get_size_of_resolution(uint32 id, uint32 &x, uint32 &y) +void monitor_desc::get_size_of_resolution(uint32 id, uint32 &x, uint32 &y) const { - vector::const_iterator i, end = VideoModes.end(); - for (i = VideoModes.begin(); i != end; ++i) { + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { if (i->resolution_id == id) { x = i->x; y = i->y; return; } } -} - - -/* - * Find palette size for given color depth - */ - -static int palette_size(video_depth depth) -{ - switch (depth) { - case VDEPTH_1BIT: return 2; - case VDEPTH_2BIT: return 4; - case VDEPTH_4BIT: return 16; - case VDEPTH_8BIT: return 256; - case VDEPTH_16BIT: return 32; - case VDEPTH_32BIT: return 256; - default: return 0; - } + x = y = 0; } @@ -146,14 +206,14 @@ static int palette_size(video_depth dept * Set palette to 50% gray */ -static void set_gray_palette(void) +void monitor_desc::set_gray_palette(void) { for (int i=0; i<256; i++) { - VidLocal.palette[i * 3 + 0] = 127; - VidLocal.palette[i * 3 + 1] = 127; - VidLocal.palette[i * 3 + 2] = 127; + palette[i * 3 + 0] = 127; + palette[i * 3 + 1] = 127; + palette[i * 3 + 2] = 127; } - video_set_palette(VidLocal.palette, 256); + set_palette(palette, 256); } @@ -161,14 +221,14 @@ static void set_gray_palette(void) * Load gamma-corrected black-to-white ramp to palette for direct-color mode */ -static void load_ramp_palette(void) +void monitor_desc::load_ramp_palette(void) { // Find tables for gamma correction - uint8 *red_gamma, *green_gamma, *blue_gamma; + uint8 *red_gamma = NULL, *green_gamma = NULL, *blue_gamma = NULL; bool have_gamma = false; int data_width = 0; - if (VidLocal.gamma_table) { - uint32 table = VidLocal.gamma_table; + if (gamma_table) { + uint32 table = gamma_table; red_gamma = Mac2HostAddr(table + gFormulaData + ReadMacInt16(table + gFormulaSize)); int chan_cnt = ReadMacInt16(table + gChanCnt); if (chan_cnt == 1) @@ -182,8 +242,8 @@ static void load_ramp_palette(void) have_gamma = true; } - int num = (VidLocal.desc->mode.depth == VDEPTH_16BIT ? 32 : 256); - uint8 *p = VidLocal.palette; + int num = (current_mode->depth == VDEPTH_16BIT ? 32 : 256); + uint8 *p = palette; for (int i=0; i VidLocal.alloc_gamma_table_size) { - if (VidLocal.gamma_table) { - r.a[0] = VidLocal.gamma_table; + if (size > alloc_gamma_table_size) { + if (gamma_table) { + r.a[0] = gamma_table; Execute68kTrap(0xa01f, &r); // DisposePtr() - VidLocal.gamma_table = 0; - VidLocal.alloc_gamma_table_size = 0; + gamma_table = 0; + alloc_gamma_table_size = 0; } r.d[0] = size; Execute68kTrap(0xa71e, &r); // NewPtrSysClear() if (r.a[0] == 0) return false; - VidLocal.gamma_table = r.a[0]; - VidLocal.alloc_gamma_table_size = size; + gamma_table = r.a[0]; + alloc_gamma_table_size = size; } return true; } @@ -230,25 +290,24 @@ static bool allocate_gamma_table(int siz * Set gamma table (0 = build linear ramp) */ -static bool set_gamma_table(uint32 user_table) +bool monitor_desc::set_gamma_table(uint32 user_table) { if (user_table == 0) { // Build linear ramp, 256 entries // Allocate new table, if necessary if (!allocate_gamma_table(SIZEOF_GammaTbl + 256)) return memFullErr; - uint32 table = VidLocal.gamma_table; // Initialize header - WriteMacInt16(table + gVersion, 0); - WriteMacInt16(table + gType, 0); - WriteMacInt16(table + gFormulaSize, 0); - WriteMacInt16(table + gChanCnt, 1); - WriteMacInt16(table + gDataCnt, 256); - WriteMacInt16(table + gDataWidth, 8); + WriteMacInt16(gamma_table + gVersion, 0); + WriteMacInt16(gamma_table + gType, 0); + WriteMacInt16(gamma_table + gFormulaSize, 0); + WriteMacInt16(gamma_table + gChanCnt, 1); + WriteMacInt16(gamma_table + gDataCnt, 256); + WriteMacInt16(gamma_table + gDataWidth, 8); // Build ramp - uint32 p = table + gFormulaData; + uint32 p = gamma_table + gFormulaData; for (int i=0; i<256; i++) WriteMacInt8(p + i, i); @@ -273,13 +332,12 @@ static bool set_gamma_table(uint32 user_ int size = SIZEOF_GammaTbl + ReadMacInt16(user_table + gFormulaSize) + chan_cnt * data_cnt; if (!allocate_gamma_table(size)) return memFullErr; - uint32 table = VidLocal.gamma_table; // Copy table - Mac2Mac_memcpy(table, user_table, size); + Mac2Mac_memcpy(gamma_table, user_table, size); } - if (IsDirectMode(VidLocal.current_mode)) + if (IsDirectMode(*current_mode)) load_ramp_palette(); return true; @@ -290,49 +348,49 @@ static bool set_gamma_table(uint32 user_ * Switch video mode */ -static void switch_mode(const video_mode &mode, uint32 param, uint32 dce) +void monitor_desc::switch_mode(vector::const_iterator it, uint32 param, uint32 dce) { + const video_mode &mode = *it; + // Switch mode set_gray_palette(); - video_switch_to_mode(mode); + current_mode = it; + switch_to_current_mode(); - // Update VidLocal - VidLocal.current_mode = DepthToAppleMode(mode.depth); - VidLocal.current_id = mode.resolution_id; - - uint32 frame_base = VidLocal.desc->mac_frame_base; + // Update variables + current_apple_mode = depth_to_apple_mode(mode.depth); + current_id = mode.resolution_id; M68kRegisters r; - uint32 sp = VidLocal.slot_param; - r.a[0] = sp; + r.a[0] = slot_param; // Find functional sResource for this display - WriteMacInt8(sp + spSlot, ReadMacInt8(dce + dCtlSlot)); - WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId)); - WriteMacInt8(sp + spExtDev, 0); + WriteMacInt8(slot_param + spSlot, ReadMacInt8(dce + dCtlSlot)); + WriteMacInt8(slot_param + spID, ReadMacInt8(dce + dCtlSlotId)); + WriteMacInt8(slot_param + spExtDev, 0); r.d[0] = 0x0016; Execute68kTrap(0xa06e, &r); // SRsrcInfo() - uint32 rsrc = ReadMacInt32(sp + spPointer); + uint32 rsrc = ReadMacInt32(slot_param + spPointer); // Patch minorBase (otherwise rebooting won't work) - WriteMacInt8(sp + spID, 0x0a); // minorBase + WriteMacInt8(slot_param + spID, 0x0a); // minorBase r.d[0] = 0x0006; Execute68kTrap(0xa06e, &r); // SFindStruct() - uint32 minor_base = ReadMacInt32(sp + spPointer) - ROMBaseMac; - ROMBaseHost[minor_base + 0] = frame_base >> 24; - ROMBaseHost[minor_base + 1] = frame_base >> 16; - ROMBaseHost[minor_base + 2] = frame_base >> 8; - ROMBaseHost[minor_base + 3] = frame_base; + uint32 minor_base = ReadMacInt32(slot_param + spPointer) - ROMBaseMac; + ROMBaseHost[minor_base + 0] = mac_frame_base >> 24; + ROMBaseHost[minor_base + 1] = mac_frame_base >> 16; + ROMBaseHost[minor_base + 2] = mac_frame_base >> 8; + ROMBaseHost[minor_base + 3] = mac_frame_base; // Patch video mode parameter table - WriteMacInt32(sp + spPointer, rsrc); - WriteMacInt8(sp + spID, DepthToAppleMode(mode.depth)); + WriteMacInt32(slot_param + spPointer, rsrc); + WriteMacInt8(slot_param + spID, depth_to_apple_mode(mode.depth)); r.d[0] = 0x0006; Execute68kTrap(0xa06e, &r); // SFindStruct() - WriteMacInt8(sp + spID, 0x01); + WriteMacInt8(slot_param + spID, 0x01); r.d[0] = 0x0006; Execute68kTrap(0xa06e, &r); // SFindStruct() - uint32 p = ReadMacInt32(sp + spPointer) - ROMBaseMac; + uint32 p = ReadMacInt32(slot_param + spPointer) - ROMBaseMac; ROMBaseHost[p + 8] = mode.bytes_per_row >> 8; ROMBaseHost[p + 9] = mode.bytes_per_row; ROMBaseHost[p + 14] = mode.y >> 8; @@ -344,13 +402,33 @@ static void switch_mode(const video_mode ChecksumSlotROM(); // Update sResource - WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId)); + WriteMacInt8(slot_param + spID, ReadMacInt8(dce + dCtlSlotId)); r.d[0] = 0x002b; Execute68kTrap(0xa06e, &r); // SUpdateSRT() // Update frame buffer base in DCE and param block - WriteMacInt32(dce + dCtlDevBase, frame_base); - WriteMacInt32(param + csBaseAddr, frame_base); + WriteMacInt32(dce + dCtlDevBase, mac_frame_base); + WriteMacInt32(param + csBaseAddr, mac_frame_base); + + // Patch frame buffer base address for MacOS versions <7.6 + if (!dm_present) { // Only do this when no Display Manager seems to be present; otherwise, the screen will not get redrawn + D(bug("No Display Manager, patching frame buffer base\n")); + WriteMacInt32(0x824, mac_frame_base); // ScrnBase + WriteMacInt32(0x898, mac_frame_base); // CrsrBase + uint32 gdev = ReadMacInt32(0x8a4); // MainDevice + D(bug("MainDevice handle at %08lx\n", gdev)); + if (gdev != 0 && gdev != 0xffffffff) { + gdev = ReadMacInt32(gdev); + D(bug(" pointer at %08lx\n", gdev)); + uint32 pmap = ReadMacInt32(gdev + 0x16); // gdPMap + D(bug(" PixMap handle at %08lx\n", pmap)); + pmap = ReadMacInt32(pmap); + D(bug(" pointer at %08lx\n", pmap)); + WriteMacInt32(pmap, mac_frame_base); // baseAddr + } + gdev = ReadMacInt32(0xcc8); // TheGDevice + D(bug("TheGDevice handle at %08lx\n", gdev)); + } } @@ -358,22 +436,18 @@ static void switch_mode(const video_mode * Driver Open() routine */ -int16 VideoDriverOpen(uint32 pb, uint32 dce) +int16 monitor_desc::driver_open(void) { - D(bug("VideoDriverOpen\n")); - // This shouldn't happen unless the platform-specific video code is broken - if (VideoModes.empty()) + if (modes.empty()) fprintf(stderr, "No valid video modes found (broken video driver?)\n"); // Init local variables - VidLocal.desc = &VideoMonitor; - VidLocal.luminance_mapping = false; - VidLocal.interrupts_enabled = false; - VidLocal.current_mode = DepthToAppleMode(VidLocal.desc->mode.depth); - VidLocal.current_id = VidLocal.desc->mode.resolution_id; - VidLocal.preferred_mode = VidLocal.current_mode; - VidLocal.preferred_id = VidLocal.current_id; + luminance_mapping = false; + interrupts_enabled = false; + current_apple_mode = preferred_apple_mode = depth_to_apple_mode(current_mode->depth); + current_id = preferred_id = current_mode->resolution_id; + dm_present = false; // Allocate Slot Manager parameter block in Mac RAM M68kRegisters r; @@ -381,12 +455,12 @@ int16 VideoDriverOpen(uint32 pb, uint32 Execute68kTrap(0xa71e, &r); // NewPtrSysClear() if (r.a[0] == 0) return memFullErr; - VidLocal.slot_param = r.a[0]; - D(bug("SPBlock at %08x\n", VidLocal.slot_param)); + slot_param = r.a[0]; + D(bug("SPBlock at %08x\n", slot_param)); // Find and set default gamma table - VidLocal.gamma_table = 0; - VidLocal.alloc_gamma_table_size = 0; + gamma_table = 0; + alloc_gamma_table_size = 0; set_gamma_table(0); // Init color palette (solid gray) @@ -394,16 +468,25 @@ int16 VideoDriverOpen(uint32 pb, uint32 return noErr; } +int16 VideoDriverOpen(uint32 pb, uint32 dce) +{ + uint8 slot_id = ReadMacInt8(dce + dCtlSlotId); + D(bug("VideoDriverOpen slot %02x\n", slot_id)); + + monitor_desc *m = find_monitor(slot_id); + if (m) + return m->driver_open(); + else + return nsDrvErr; +} + /* * Driver Control() routine */ -int16 VideoDriverControl(uint32 pb, uint32 dce) +int16 monitor_desc::driver_control(uint16 code, uint32 param, uint32 dce) { - uint16 code = ReadMacInt16(pb + csCode); - uint32 param = ReadMacInt32(pb + csParam); - D(bug("VideoDriverControl %d\n", code)); switch (code) { case cscSetMode: { // Set color depth @@ -411,25 +494,25 @@ int16 VideoDriverControl(uint32 pb, uint D(bug(" SetMode %04x\n", mode)); // Set old base address in case the switch fails - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + WriteMacInt32(param + csBaseAddr, mac_frame_base); if (ReadMacInt16(param + csPage)) return paramErr; - if (mode != VidLocal.current_mode) { - vector::const_iterator i = find_mode(mode, VidLocal.current_id); - if (i == VideoModes.end()) + if (mode != current_apple_mode) { + vector::const_iterator i = find_mode(mode, current_id); + if (i == invalid_mode()) return paramErr; - switch_mode(*i, param, dce); + switch_mode(i, param, dce); } - D(bug(" base %08x\n", VidLocal.desc->mac_frame_base)); + D(bug(" base %08x\n", mac_frame_base)); return noErr; } case cscSetEntries: // Set palette case cscDirectSetEntries: { D(bug(" (Direct)SetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart))); - bool is_direct = IsDirectMode(VidLocal.current_mode); + bool is_direct = IsDirectMode(*current_mode); if (code == cscSetEntries && is_direct) return controlErr; if (code == cscDirectSetEntries && !is_direct) @@ -443,32 +526,31 @@ int16 VideoDriverControl(uint32 pb, uint return paramErr; // Find tables for gamma correction - uint8 *red_gamma, *green_gamma, *blue_gamma; + uint8 *red_gamma = NULL, *green_gamma = NULL, *blue_gamma = NULL; bool have_gamma = false; int data_width = 0; - if (VidLocal.gamma_table) { - uint32 table = VidLocal.gamma_table; - red_gamma = Mac2HostAddr(table + gFormulaData + ReadMacInt16(table + gFormulaSize)); - int chan_cnt = ReadMacInt16(table + gChanCnt); + if (gamma_table) { + red_gamma = Mac2HostAddr(gamma_table + gFormulaData + ReadMacInt16(gamma_table + gFormulaSize)); + int chan_cnt = ReadMacInt16(gamma_table + gChanCnt); if (chan_cnt == 1) green_gamma = blue_gamma = red_gamma; else { - int ofs = ReadMacInt16(table + gDataCnt); + int ofs = ReadMacInt16(gamma_table + gDataCnt); green_gamma = red_gamma + ofs; blue_gamma = green_gamma + ofs; } - data_width = ReadMacInt16(table + gDataWidth); + data_width = ReadMacInt16(gamma_table + gDataWidth); have_gamma = true; } // Convert palette if (start == 0xffff) { // Indexed for (uint32 i=0; i<=count; i++) { - d_pal = VidLocal.palette + (ReadMacInt16(s_pal) & 0xff) * 3; + d_pal = palette + (ReadMacInt16(s_pal) & 0xff) * 3; uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8; uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8; uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8; - if (VidLocal.luminance_mapping && !is_direct) + if (luminance_mapping && !is_direct) red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16; if (have_gamma) { red = red_gamma[red >> (8 - data_width)]; @@ -483,12 +565,12 @@ int16 VideoDriverControl(uint32 pb, uint } else { // Sequential if (start + count > 255) return paramErr; - d_pal = VidLocal.palette + start * 3; + d_pal = palette + start * 3; for (uint32 i=0; i<=count; i++) { uint8 red = (uint16)ReadMacInt16(s_pal + 2) >> 8; uint8 green = (uint16)ReadMacInt16(s_pal + 4) >> 8; uint8 blue = (uint16)ReadMacInt16(s_pal + 6) >> 8; - if (VidLocal.luminance_mapping && !is_direct) + if (luminance_mapping && !is_direct) red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16; if (have_gamma) { red = red_gamma[red >> (8 - data_width)]; @@ -501,7 +583,7 @@ int16 VideoDriverControl(uint32 pb, uint s_pal += 8; } } - video_set_palette(VidLocal.palette, palette_size(VidLocal.desc->mode.depth)); + set_palette(palette, palette_size(current_mode->depth)); return noErr; } @@ -524,20 +606,20 @@ int16 VideoDriverControl(uint32 pb, uint 0xffff0000, // 16 bpp 0xffffffff // 32 bpp }; - uint32 p = VidLocal.desc->mac_frame_base; - uint32 pat = pattern[VidLocal.desc->mode.depth]; - bool invert = (VidLocal.desc->mode.depth == VDEPTH_32BIT); - for (uint32 y=0; ymode.y; y++) { - for (uint32 x=0; xmode.bytes_per_row; x+=4) { + uint32 p = mac_frame_base; + uint32 pat = pattern[current_mode->depth]; + bool invert = (current_mode->depth == VDEPTH_32BIT); + for (uint32 y=0; yy; y++) { + for (uint32 x=0; xbytes_per_row; x+=4) { WriteMacInt32(p + x, pat); if (invert) pat = ~pat; } - p += VidLocal.desc->mode.bytes_per_row; + p += current_mode->bytes_per_row; pat = ~pat; } - if (IsDirectMode(VidLocal.current_mode)) + if (IsDirectMode(*current_mode)) load_ramp_palette(); return noErr; @@ -545,18 +627,18 @@ int16 VideoDriverControl(uint32 pb, uint case cscSetGray: // Enable/disable luminance mapping D(bug(" SetGray %02x\n", ReadMacInt8(param + csMode))); - VidLocal.luminance_mapping = ReadMacInt8(param + csMode); + luminance_mapping = ReadMacInt8(param + csMode); return noErr; case cscSetInterrupt: // Enable/disable VBL D(bug(" SetInterrupt %02x\n", ReadMacInt8(param + csMode))); - VidLocal.interrupts_enabled = (ReadMacInt8(param + csMode) == 0); + interrupts_enabled = (ReadMacInt8(param + csMode) == 0); return noErr; case cscSetDefaultMode: { // Set default color depth - uint16 mode = ReadMacInt16(param + csMode); - D(bug(" SetDefaultMode %04x\n", mode)); - VidLocal.preferred_mode = mode; + uint16 mode = ReadMacInt8(param + csMode); + D(bug(" SetDefaultMode %02x\n", mode)); + preferred_apple_mode = mode; return noErr; } @@ -566,18 +648,18 @@ int16 VideoDriverControl(uint32 pb, uint D(bug(" SwitchMode %04x, %08x\n", mode, id)); // Set old base address in case the switch fails - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + WriteMacInt32(param + csBaseAddr, mac_frame_base); if (ReadMacInt16(param + csPage)) return paramErr; - if (mode != VidLocal.current_mode || id != VidLocal.current_id) { + if (mode != current_apple_mode || id != current_id) { vector::const_iterator i = find_mode(mode, id); - if (i == VideoModes.end()) + if (i == invalid_mode()) return paramErr; - switch_mode(*i, param, dce); + switch_mode(i, param, dce); } - D(bug(" base %08x\n", VidLocal.desc->mac_frame_base)); + D(bug(" base %08x\n", mac_frame_base)); return noErr; } @@ -585,8 +667,8 @@ int16 VideoDriverControl(uint32 pb, uint uint16 mode = ReadMacInt16(param + csMode); uint32 id = ReadMacInt32(param + csData); D(bug(" SavePreferredConfiguration %04x, %08x\n", mode, id)); - VidLocal.preferred_mode = mode; - VidLocal.preferred_id = id; + preferred_apple_mode = mode; + preferred_id = id; return noErr; } @@ -596,23 +678,34 @@ int16 VideoDriverControl(uint32 pb, uint } } +int16 VideoDriverControl(uint32 pb, uint32 dce) +{ + uint8 slot_id = ReadMacInt8(dce + dCtlSlotId); + uint16 code = ReadMacInt16(pb + csCode); + uint32 param = ReadMacInt32(pb + csParam); + D(bug("VideoDriverControl slot %02x, code %d\n", slot_id, code)); + + monitor_desc *m = find_monitor(slot_id); + if (m) + return m->driver_control(code, param, dce); + else + return nsDrvErr; +} + /* * Driver Status() routine */ -int16 VideoDriverStatus(uint32 pb, uint32 dce) +int16 monitor_desc::driver_status(uint16 code, uint32 param) { - uint16 code = ReadMacInt16(pb + csCode); - uint32 param = ReadMacInt32(pb + csParam); - D(bug("VideoDriverStatus %d\n", code)); switch (code) { case cscGetMode: // Get current color depth - D(bug(" GetMode -> %04x, base %08x\n", VidLocal.current_mode, VidLocal.desc->mac_frame_base)); - WriteMacInt16(param + csMode, VidLocal.current_mode); + D(bug(" GetMode -> %04x, base %08x\n", current_apple_mode, mac_frame_base)); + WriteMacInt16(param + csMode, current_apple_mode); WriteMacInt16(param + csPage, 0); - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + WriteMacInt32(param + csBaseAddr, mac_frame_base); return noErr; case cscGetEntries: { // Read palette @@ -627,7 +720,7 @@ int16 VideoDriverStatus(uint32 pb, uint3 if (start == 0xffff) { // Indexed for (uint32 i=0; i<=count; i++) { - s_pal = VidLocal.palette + (ReadMacInt16(d_pal) & 0xff) * 3; + s_pal = palette + (ReadMacInt16(d_pal) & 0xff) * 3; uint8 red = *s_pal++; uint8 green = *s_pal++; uint8 blue = *s_pal++; @@ -639,7 +732,7 @@ int16 VideoDriverStatus(uint32 pb, uint3 } else { // Sequential if (start + count > 255) return paramErr; - s_pal = VidLocal.palette + start * 3; + s_pal = palette + start * 3; for (uint32 i=0; i<=count; i++) { uint8 red = *s_pal++; uint8 green = *s_pal++; @@ -659,39 +752,39 @@ int16 VideoDriverStatus(uint32 pb, uint3 return noErr; case cscGetBaseAddress: // Get page base address - D(bug(" GetBaseAddress -> %08x\n", VidLocal.desc->mac_frame_base)); - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + D(bug(" GetBaseAddress -> %08x\n", mac_frame_base)); + WriteMacInt32(param + csBaseAddr, mac_frame_base); if (ReadMacInt16(param + csPage)) return paramErr; else return noErr; case cscGetGray: // Get luminance mapping flag - D(bug(" GetGray -> %d\n", VidLocal.luminance_mapping)); - WriteMacInt16(param, VidLocal.luminance_mapping ? 0x0100 : 0); + D(bug(" GetGray -> %d\n", luminance_mapping)); + WriteMacInt8(param, luminance_mapping ? 1 : 0); return noErr; case cscGetInterrupt: // Get interrupt disable flag - D(bug(" GetInterrupt -> %d\n", VidLocal.interrupts_enabled)); - WriteMacInt16(param, VidLocal.interrupts_enabled ? 0 : 0x0100); + D(bug(" GetInterrupt -> %d\n", interrupts_enabled)); + WriteMacInt8(param, interrupts_enabled ? 0 : 1); return noErr; case cscGetGamma: - D(bug(" GetGamma -> %08x\n", VidLocal.gamma_table)); - WriteMacInt32(param + csGTable, VidLocal.gamma_table); + D(bug(" GetGamma -> %08x\n", gamma_table)); + WriteMacInt32(param + csGTable, gamma_table); return noErr; case cscGetDefaultMode: // Get default color depth - D(bug(" GetDefaultMode -> %04x\n", VidLocal.preferred_mode)); - WriteMacInt16(param + csMode, VidLocal.preferred_mode); + D(bug(" GetDefaultMode -> %02x\n", preferred_apple_mode)); + WriteMacInt8(param + csMode, preferred_apple_mode); return noErr; case cscGetCurrentMode: // Get current video mode (depth and resolution) - D(bug(" GetCurMode -> %04x/%08x, base %08x\n", VidLocal.current_mode, VidLocal.current_id, VidLocal.desc->mac_frame_base)); - WriteMacInt16(param + csMode, VidLocal.current_mode); - WriteMacInt32(param + csData, VidLocal.current_id); + D(bug(" GetCurMode -> %04x/%08x, base %08x\n", current_apple_mode, current_id, mac_frame_base)); + WriteMacInt16(param + csMode, current_apple_mode); + WriteMacInt32(param + csData, current_id); WriteMacInt16(param + csPage, 0); - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + WriteMacInt32(param + csBaseAddr, mac_frame_base); return noErr; case cscGetConnection: // Get monitor information @@ -712,21 +805,21 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(param + csTimingFormat, FOURCC('d', 'e', 'c', 'l')); WriteMacInt32(param + csTimingData, 0); // unknown uint32 flags = 0xb; // mode valid, safe and shown in Monitors panel - if (id == VidLocal.preferred_id) + if (id == preferred_id) flags |= 4; // default mode WriteMacInt32(param + csTimingFlags, flags); return noErr; } case cscGetModeBaseAddress: // Get frame buffer base address - D(bug(" GetModeBaseAddress -> base %08x\n", VidLocal.desc->mac_frame_base)); - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); + D(bug(" GetModeBaseAddress -> base %08x\n", mac_frame_base)); + WriteMacInt32(param + csBaseAddr, mac_frame_base); return noErr; case cscGetPreferredConfiguration: // Get default video mode (depth and resolution) - D(bug(" GetPreferredConfiguration -> %04x/%08x\n", VidLocal.preferred_mode, VidLocal.preferred_id)); - WriteMacInt16(param + csMode, VidLocal.preferred_mode); - WriteMacInt32(param + csData, VidLocal.preferred_id); + D(bug(" GetPreferredConfiguration -> %04x/%08x\n", preferred_apple_mode, preferred_id)); + WriteMacInt16(param + csMode, preferred_apple_mode); + WriteMacInt32(param + csData, preferred_id); return noErr; case cscGetNextResolution: { // Called iteratively to obtain a list of all supported resolutions @@ -736,7 +829,7 @@ int16 VideoDriverStatus(uint32 pb, uint3 switch (id) { case 0: // Return current resolution - id = VidLocal.current_id; + id = current_id; break; case 0xfffffffe: @@ -766,7 +859,8 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(param + csHorizontalPixels, x); WriteMacInt32(param + csVerticalLines, y); WriteMacInt32(param + csRefreshRate, 75 << 16); - WriteMacInt16(param + csMaxDepthMode, DepthToAppleMode(max_depth_of_resolution(id))); + WriteMacInt16(param + csMaxDepthMode, depth_to_apple_mode(max_depth_of_resolution(id))); + WriteMacInt32(param + csResolutionFlags, 0); return noErr; } @@ -774,10 +868,12 @@ int16 VideoDriverStatus(uint32 pb, uint3 uint32 id = ReadMacInt32(param + csDisplayModeID); uint16 mode = ReadMacInt16(param + csDepthMode); D(bug(" GetVideoParameters %04x/%08x\n", mode, id)); + dm_present = true; // Display Manager seems to be present + D(bug(" Display Manager detected\n")); - vector::const_iterator i, end = VideoModes.end(); - for (i = VideoModes.begin(); i != end; ++i) { - if (DepthToAppleMode(i->depth) == mode && i->resolution_id == id) { + vector::const_iterator i, end = modes.end(); + for (i = modes.begin(); i != end; ++i) { + if (depth_to_apple_mode(i->depth) == mode && i->resolution_id == id) { uint32 vp = ReadMacInt32(param + csVPBlockPtr); WriteMacInt32(vp + vpBaseOffset, 0); WriteMacInt16(vp + vpRowBytes, i->bytes_per_row); @@ -792,26 +888,6 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(vp + vpVRes, 0x00480000); uint32 pix_type, pix_size, cmp_count, cmp_size, dev_type; switch (i->depth) { - case VDEPTH_1BIT: - pix_type = 0; pix_size = 1; - cmp_count = 1; cmp_size = 1; - dev_type = 0; // CLUT - break; - case VDEPTH_2BIT: - pix_type = 0; pix_size = 2; - cmp_count = 1; cmp_size = 2; - dev_type = 0; // CLUT - break; - case VDEPTH_4BIT: - pix_type = 0; pix_size = 4; - cmp_count = 1; cmp_size = 4; - dev_type = 0; // CLUT - break; - case VDEPTH_8BIT: - pix_type = 0; pix_size = 8; - cmp_count = 1; cmp_size = 8; - dev_type = 0; // CLUT - break; case VDEPTH_16BIT: pix_type = 0x10; pix_size = 16; cmp_count = 3; cmp_size = 5; @@ -822,6 +898,11 @@ int16 VideoDriverStatus(uint32 pb, uint3 cmp_count = 3; cmp_size = 8; dev_type = 2; // direct break; + default: + pix_type = 0; pix_size = 1 << i->depth; + cmp_count = 1; cmp_size = 1 << i->depth; + dev_type = 0; // CLUT + break; } WriteMacInt16(vp + vpPixelType, pix_type); WriteMacInt16(vp + vpPixelSize, pix_size); @@ -835,8 +916,39 @@ int16 VideoDriverStatus(uint32 pb, uint3 return paramErr; // specified resolution/depth not supported } + case cscGetMultiConnect: { + uint32 conn = ReadMacInt32(param + csDisplayCountOrNumber); + D(bug(" GetMultiConnect %08x\n", conn)); + if (conn == 0xffffffff) { // Get number of connections + WriteMacInt32(param + csDisplayCountOrNumber, 1); // Single-headed + return noErr; + } else if (conn == 1) { // Get information about first connection + WriteMacInt16(param + csConnectInfo + csDisplayType, 8); // Modeless connection + WriteMacInt8(param + csConnectInfo + csConnectTaggedType, 0); + WriteMacInt8(param + csConnectInfo + csConnectTaggedData, 0); + WriteMacInt32(param + csConnectInfo + csConnectFlags, 0x43); // All modes valid and safe, non-standard tagging + WriteMacInt32(param + csConnectInfo + csDisplayComponent, 0); + return noErr; + } else + return paramErr; + } + default: printf("WARNING: Unknown VideoDriverStatus(%d)\n", code); return statusErr; } } + +int16 VideoDriverStatus(uint32 pb, uint32 dce) +{ + uint8 slot_id = ReadMacInt8(dce + dCtlSlotId); + uint16 code = ReadMacInt16(pb + csCode); + uint32 param = ReadMacInt32(pb + csParam); + D(bug("VideoDriverStatus slot %02x, code %d\n", slot_id, code)); + + monitor_desc *m = find_monitor(slot_id); + if (m) + return m->driver_status(code, param); + else + return nsDrvErr; +}