--- BasiliskII/src/video.cpp 2001/06/30 22:23:43 1.15 +++ BasiliskII/src/video.cpp 2001/07/04 11:12:19 1.21 @@ -40,38 +40,73 @@ // List of supported video modes -std::vector VideoModes; +vector VideoModes; // Description of the main monitor monitor_desc VideoMonitor; +// Depth code -> Apple mode mapping +uint16 apple_mode_for_depth[6]; + // 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 + bool dm_present; // We received a GetVideoParameters call, so the Display Manager seems to be present 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 sp; // Mac address of Slot Manager parameter block + uint32 slot_param; // Mac address of Slot Manager parameter block } VidLocal; /* + * Initialize apple_mode_for_depth[] array from VideoModes list + */ + +void video_init_depth_list(void) +{ + uint16 mode = 0x80; + for (int depth = VDEPTH_1BIT; depth <= VDEPTH_32BIT; depth++) { + if (video_has_depth(video_depth(depth))) + apple_mode_for_depth[depth] = mode++; + else + apple_mode_for_depth[depth] = 0; + } +} + + +/* + * Check whether a mode with the specified depth exists + */ + +bool video_has_depth(video_depth depth) +{ + vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); + while (i != end) { + if (i->depth == depth) + return true; + ++i; + } + return false; +} + + +/* * Check whether specified resolution ID is one of the supported resolutions */ static bool has_resolution(uint32 id) { - std::vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { if (i->resolution_id == id) return true; - ++i; } return false; } @@ -81,13 +116,12 @@ static bool has_resolution(uint32 id) * Find specified mode (depth/resolution) (or VideoModes.end() if not found) */ -static std::vector::const_iterator find_mode(uint16 mode, uint32 id) +static vector::const_iterator find_mode(uint16 mode, uint32 id) { - std::vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { if (i->resolution_id == id && DepthToAppleMode(i->depth) == mode) return i; - ++i; } return i; } @@ -100,11 +134,10 @@ static std::vector::const_it static video_depth max_depth_of_resolution(uint32 id) { video_depth m = VDEPTH_1BIT; - std::vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { if (i->depth > m) m = i->depth; - ++i; } return m; } @@ -116,14 +149,49 @@ static video_depth max_depth_of_resoluti static void get_size_of_resolution(uint32 id, uint32 &x, uint32 &y) { - std::vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { if (i->resolution_id == id) { x = i->x; y = i->y; return; } - ++i; + } + x = y = 0; +} + + +/* + * Get bytes-per-row value for specified resolution/depth + */ + +uint32 video_bytes_per_row(video_depth depth, uint32 id) +{ + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.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); +} + + +/* + * 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; } } @@ -139,7 +207,7 @@ static void set_gray_palette(void) VidLocal.palette[i * 3 + 1] = 127; VidLocal.palette[i * 3 + 2] = 127; } - video_set_palette(VidLocal.palette); + video_set_palette(VidLocal.palette, 256); } @@ -182,7 +250,7 @@ static void load_ramp_palette(void) *p++ = blue; } - video_set_palette(VidLocal.palette); + video_set_palette(VidLocal.palette, num); } @@ -265,7 +333,7 @@ static bool set_gamma_table(uint32 user_ Mac2Mac_memcpy(table, user_table, size); } - if (IsDirectMode(VidLocal.current_mode)) + if (IsDirectMode(VidLocal.desc->mode)) load_ramp_palette(); return true; @@ -273,6 +341,85 @@ static bool set_gamma_table(uint32 user_ /* + * Switch video mode + */ + +static void switch_mode(const video_mode &mode, uint32 param, uint32 dce) +{ + // Switch mode + set_gray_palette(); + video_switch_to_mode(mode); + + // Update VidLocal + VidLocal.current_mode = DepthToAppleMode(mode.depth); + VidLocal.current_id = mode.resolution_id; + + uint32 frame_base = VidLocal.desc->mac_frame_base; + + M68kRegisters r; + uint32 sp = VidLocal.slot_param; + r.a[0] = sp; + + // Find functional sResource for this display + WriteMacInt8(sp + spSlot, ReadMacInt8(dce + dCtlSlot)); + WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId)); + WriteMacInt8(sp + spExtDev, 0); + r.d[0] = 0x0016; + Execute68kTrap(0xa06e, &r); // SRsrcInfo() + uint32 rsrc = ReadMacInt32(sp + spPointer); + + // Patch minorBase (otherwise rebooting won't work) + WriteMacInt8(sp + 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; + + // Patch video mode parameter table + WriteMacInt32(sp + spPointer, rsrc); + WriteMacInt8(sp + spID, DepthToAppleMode(mode.depth)); + r.d[0] = 0x0006; + Execute68kTrap(0xa06e, &r); // SFindStruct() + WriteMacInt8(sp + spID, 0x01); + r.d[0] = 0x0006; + Execute68kTrap(0xa06e, &r); // SFindStruct() + uint32 p = ReadMacInt32(sp + spPointer) - ROMBaseMac; + ROMBaseHost[p + 8] = mode.bytes_per_row >> 8; + ROMBaseHost[p + 9] = mode.bytes_per_row; + ROMBaseHost[p + 14] = mode.y >> 8; + ROMBaseHost[p + 15] = mode.y; + ROMBaseHost[p + 16] = mode.x >> 8; + ROMBaseHost[p + 17] = mode.x; + + // Recalculate slot ROM checksum + ChecksumSlotROM(); + + // Update sResource + WriteMacInt8(sp + 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); + + // Patch frame buffer base address for MacOS versions <7.6 + if (!VidLocal.dm_present) { // Only do this when no Display Manager seems to be present; otherwise, the screen will not get redrawn + WriteMacInt32(0x824, frame_base); // ScrnBase + WriteMacInt32(0x898, frame_base); // CrsrBase + uint32 gdev = ReadMacInt32(0x8a4); // MainDevice + gdev = ReadMacInt32(gdev); + uint32 pmap = ReadMacInt32(gdev + 0x16); // gdPMap + pmap = ReadMacInt32(pmap); + WriteMacInt32(pmap, frame_base); // baseAddr + } +} + + +/* * Driver Open() routine */ @@ -292,6 +439,7 @@ int16 VideoDriverOpen(uint32 pb, uint32 VidLocal.current_id = VidLocal.desc->mode.resolution_id; VidLocal.preferred_mode = VidLocal.current_mode; VidLocal.preferred_id = VidLocal.current_id; + VidLocal.dm_present = false; // Allocate Slot Manager parameter block in Mac RAM M68kRegisters r; @@ -299,8 +447,8 @@ int16 VideoDriverOpen(uint32 pb, uint32 Execute68kTrap(0xa71e, &r); // NewPtrSysClear() if (r.a[0] == 0) return memFullErr; - VidLocal.sp = r.a[0]; - D(bug("SPBlock at %08x\n", VidLocal.sp)); + VidLocal.slot_param = r.a[0]; + D(bug("SPBlock at %08x\n", VidLocal.slot_param)); // Find and set default gamma table VidLocal.gamma_table = 0; @@ -335,14 +483,10 @@ int16 VideoDriverControl(uint32 pb, uint return paramErr; if (mode != VidLocal.current_mode) { - std::vector::const_iterator i = find_mode(mode, VidLocal.current_id); + vector::const_iterator i = find_mode(mode, VidLocal.current_id); if (i == VideoModes.end()) return paramErr; - set_gray_palette(); - video_switch_to_mode(*i); - VidLocal.current_mode = mode; - WriteMacInt32(param + csBaseAddr, VidLocal.desc->mac_frame_base); - WriteMacInt32(dce + dCtlDevBase, VidLocal.desc->mac_frame_base); + switch_mode(*i, param, dce); } D(bug(" base %08x\n", VidLocal.desc->mac_frame_base)); return noErr; @@ -351,7 +495,7 @@ int16 VideoDriverControl(uint32 pb, uint 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(VidLocal.desc->mode); if (code == cscSetEntries && is_direct) return controlErr; if (code == cscDirectSetEntries && !is_direct) @@ -423,7 +567,7 @@ int16 VideoDriverControl(uint32 pb, uint s_pal += 8; } } - video_set_palette(VidLocal.palette); + video_set_palette(VidLocal.palette, palette_size(VidLocal.desc->mode.depth)); return noErr; } @@ -446,20 +590,20 @@ int16 VideoDriverControl(uint32 pb, uint 0xffff0000, // 16 bpp 0xffffffff // 32 bpp }; - uint32 p = VidLocal.desc->mac_frame_base; + uint32 frame_base = 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) { - WriteMacInt32(p + x, pat); + WriteMacInt32(frame_base + x, pat); if (invert) pat = ~pat; } - p += VidLocal.desc->mode.bytes_per_row; + frame_base += VidLocal.desc->mode.bytes_per_row; pat = ~pat; } - if (IsDirectMode(VidLocal.current_mode)) + if (IsDirectMode(VidLocal.desc->mode)) load_ramp_palette(); return noErr; @@ -476,8 +620,8 @@ int16 VideoDriverControl(uint32 pb, uint return noErr; case cscSetDefaultMode: { // Set default color depth - uint16 mode = ReadMacInt16(param + csMode); - D(bug(" SetDefaultMode %04x\n", mode)); + uint16 mode = ReadMacInt8(param + csMode); + D(bug(" SetDefaultMode %02x\n", mode)); VidLocal.preferred_mode = mode; return noErr; } @@ -494,64 +638,10 @@ int16 VideoDriverControl(uint32 pb, uint return paramErr; if (mode != VidLocal.current_mode || id != VidLocal.current_id) { - std::vector::const_iterator i = find_mode(mode, id); + vector::const_iterator i = find_mode(mode, id); if (i == VideoModes.end()) return paramErr; - set_gray_palette(); - video_switch_to_mode(*i); - VidLocal.current_mode = mode; - VidLocal.current_id = id; - uint32 frame_base = VidLocal.desc->mac_frame_base; - WriteMacInt32(param + csBaseAddr, frame_base); - - M68kRegisters r; - uint32 sp = VidLocal.sp; - r.a[0] = sp; - - // Find functional sResource for this display - WriteMacInt8(sp + spSlot, ReadMacInt8(dce + dCtlSlot)); - WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId)); - WriteMacInt8(sp + spExtDev, 0); - r.d[0] = 0x0016; - Execute68kTrap(0xa06e, &r); // SRsrcInfo() - uint32 rsrc = ReadMacInt32(sp + spPointer); - - // Patch minorBase (otherwise rebooting won't work) - WriteMacInt8(sp + 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; - - // Patch video mode parameter table - WriteMacInt32(sp + spPointer, rsrc); - WriteMacInt8(sp + spID, mode); - r.d[0] = 0x0006; - Execute68kTrap(0xa06e, &r); // SFindStruct() - WriteMacInt8(sp + spID, 0x01); - r.d[0] = 0x0006; - Execute68kTrap(0xa06e, &r); // SFindStruct() - uint32 p = ReadMacInt32(sp + spPointer) - ROMBaseMac; - ROMBaseHost[p + 8] = i->bytes_per_row >> 8; - ROMBaseHost[p + 9] = i->bytes_per_row; - ROMBaseHost[p + 14] = i->y >> 8; - ROMBaseHost[p + 15] = i->y; - ROMBaseHost[p + 16] = i->x >> 8; - ROMBaseHost[p + 17] = i->x; - - // Recalculate slot ROM checksum - ChecksumSlotROM(); - - // Update sResource - WriteMacInt8(sp + spID, ReadMacInt8(dce + dCtlSlotId)); - r.d[0] = 0x002b; - Execute68kTrap(0xa06e, &r); // SUpdateSRT() - - // Update frame buffer base in DCE - WriteMacInt32(dce + dCtlDevBase, frame_base); + switch_mode(*i, param, dce); } D(bug(" base %08x\n", VidLocal.desc->mac_frame_base)); return noErr; @@ -644,12 +734,12 @@ int16 VideoDriverStatus(uint32 pb, uint3 case cscGetGray: // Get luminance mapping flag D(bug(" GetGray -> %d\n", VidLocal.luminance_mapping)); - WriteMacInt16(param, VidLocal.luminance_mapping ? 0x0100 : 0); + WriteMacInt8(param, VidLocal.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); + WriteMacInt8(param, VidLocal.interrupts_enabled ? 0 : 1); return noErr; case cscGetGamma: @@ -658,8 +748,8 @@ int16 VideoDriverStatus(uint32 pb, uint3 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", VidLocal.preferred_mode)); + WriteMacInt8(param + csMode, VidLocal.preferred_mode); return noErr; case cscGetCurrentMode: // Get current video mode (depth and resolution) @@ -743,6 +833,7 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(param + csVerticalLines, y); WriteMacInt32(param + csRefreshRate, 75 << 16); WriteMacInt16(param + csMaxDepthMode, DepthToAppleMode(max_depth_of_resolution(id))); + WriteMacInt32(param + csResolutionFlags, 0); return noErr; } @@ -750,9 +841,10 @@ int16 VideoDriverStatus(uint32 pb, uint3 uint32 id = ReadMacInt32(param + csDisplayModeID); uint16 mode = ReadMacInt16(param + csDepthMode); D(bug(" GetVideoParameters %04x/%08x\n", mode, id)); + VidLocal.dm_present = true; // Display Manager seems to be present - std::vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { if (DepthToAppleMode(i->depth) == mode && i->resolution_id == id) { uint32 vp = ReadMacInt32(param + csVPBlockPtr); WriteMacInt32(vp + vpBaseOffset, 0); @@ -807,11 +899,27 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(param + csDeviceType, dev_type); return noErr; } - ++i; } 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;