--- BasiliskII/src/video.cpp 2001/06/28 21:36:46 1.12 +++ BasiliskII/src/video.cpp 2001/07/01 21:09:26 1.19 @@ -31,6 +31,7 @@ #include "cpu_emulation.h" #include "main.h" #include "macos_util.h" +#include "slot_rom.h" #include "video.h" #include "video_defs.h" @@ -39,7 +40,7 @@ // List of supported video modes -std::vector VideoModes; +vector VideoModes; // Description of the main monitor monitor_desc VideoMonitor; @@ -51,11 +52,12 @@ struct { 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 sp; // Mac address of Slot Manager parameter block + uint32 slot_param; // Mac address of Slot Manager parameter block } VidLocal; @@ -65,11 +67,10 @@ struct { 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; } @@ -79,13 +80,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; } @@ -98,11 +98,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; } @@ -114,14 +113,31 @@ 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; + } +} + + +/* + * 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; } } @@ -132,14 +148,209 @@ static void get_size_of_resolution(uint3 static void set_gray_palette(void) { - if (!IsDirectMode(VidLocal.current_mode)) { - 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; + 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; + } + video_set_palette(VidLocal.palette, 256); +} + + +/* + * Load gamma-corrected black-to-white ramp to palette for direct-color mode + */ + +static void load_ramp_palette(void) +{ + // Find tables for gamma correction + uint8 *red_gamma, *green_gamma, *blue_gamma; + 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 (chan_cnt == 1) + green_gamma = blue_gamma = red_gamma; + else { + int ofs = ReadMacInt16(table + gDataCnt); + green_gamma = red_gamma + ofs; + blue_gamma = green_gamma + ofs; } - video_set_palette(VidLocal.palette); + data_width = ReadMacInt16(table + gDataWidth); + have_gamma = true; + } + + int num = (VidLocal.desc->mode.depth == VDEPTH_16BIT ? 32 : 256); + uint8 *p = VidLocal.palette; + for (int i=0; i> (8 - data_width)]; + green = green_gamma[green >> (8 - data_width)]; + blue = blue_gamma[blue >> (8 - data_width)]; + } + *p++ = red; + *p++ = green; + *p++ = blue; + } + + video_set_palette(VidLocal.palette, num); +} + + +/* + * Allocate gamma table of specified size + */ + +static bool allocate_gamma_table(int size) +{ + M68kRegisters r; + + if (size > VidLocal.alloc_gamma_table_size) { + if (VidLocal.gamma_table) { + r.a[0] = VidLocal.gamma_table; + Execute68kTrap(0xa01f, &r); // DisposePtr() + VidLocal.gamma_table = 0; + VidLocal.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; + } + return true; +} + + +/* + * Set gamma table (0 = build linear ramp) + */ + +static bool 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); + + // Build ramp + uint32 p = table + gFormulaData; + for (int i=0; i<256; i++) + WriteMacInt8(p + i, i); + + } else { // User-supplied gamma table + + // Validate header + if (ReadMacInt16(user_table + gVersion)) + return paramErr; + if (ReadMacInt16(user_table + gType)) + return paramErr; + int chan_cnt = ReadMacInt16(user_table + gChanCnt); + if (chan_cnt != 1 && chan_cnt != 3) + return paramErr; + int data_width = ReadMacInt16(user_table + gDataWidth); + if (data_width > 8) + return paramErr; + int data_cnt = ReadMacInt16(user_table + gDataCnt); + if (data_cnt != (1 << data_width)) + return paramErr; + + // Allocate new table, if necessary + 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); } + + if (IsDirectMode(VidLocal.current_mode)) + load_ramp_palette(); + + return true; +} + + +/* + * 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); } @@ -170,11 +381,13 @@ 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; //!! + VidLocal.gamma_table = 0; + VidLocal.alloc_gamma_table_size = 0; + set_gamma_table(0); // Init color palette (solid gray) set_gray_palette(); @@ -204,22 +417,22 @@ 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; } - case cscSetEntries: { // Set palette - D(bug(" SetEntries table %08x, count %d, start %d\n", ReadMacInt32(param + csTable), ReadMacInt16(param + csCount), ReadMacInt16(param + csStart))); - if (IsDirectMode(VidLocal.current_mode)) + 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); + if (code == cscSetEntries && is_direct) + return controlErr; + if (code == cscDirectSetEntries && !is_direct) return controlErr; uint32 s_pal = ReadMacInt32(param + csTable); // Source palette @@ -229,15 +442,39 @@ int16 VideoDriverControl(uint32 pb, uint if (s_pal == 0 || count > 255) return paramErr; + // Find tables for gamma correction + uint8 *red_gamma, *green_gamma, *blue_gamma; + 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 (chan_cnt == 1) + green_gamma = blue_gamma = red_gamma; + else { + int ofs = ReadMacInt16(table + gDataCnt); + green_gamma = red_gamma + ofs; + blue_gamma = green_gamma + ofs; + } + data_width = ReadMacInt16(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; 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) + if (VidLocal.luminance_mapping && !is_direct) red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16; - //!! gamma correction + if (have_gamma) { + red = red_gamma[red >> (8 - data_width)]; + green = green_gamma[green >> (8 - data_width)]; + blue = blue_gamma[blue >> (8 - data_width)]; + } *d_pal++ = red; *d_pal++ = green; *d_pal++ = blue; @@ -251,23 +488,28 @@ int16 VideoDriverControl(uint32 pb, uint 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) + if (VidLocal.luminance_mapping && !is_direct) red = green = blue = (red * 0x4ccc + green * 0x970a + blue * 0x1c29) >> 16; - //!! gamma correction + if (have_gamma) { + red = red_gamma[red >> (8 - data_width)]; + green = green_gamma[green >> (8 - data_width)]; + blue = blue_gamma[blue >> (8 - data_width)]; + } *d_pal++ = red; *d_pal++ = green; *d_pal++ = blue; s_pal += 8; } } - video_set_palette(VidLocal.palette); + video_set_palette(VidLocal.palette, palette_size(VidLocal.desc->mode.depth)); return noErr; } - case cscSetGamma: // Set gamma table - D(bug(" SetGamma\n")); - //!! - return controlErr; + case cscSetGamma: { // Set gamma table + uint32 user_table = ReadMacInt32(param + csGTable); + D(bug(" SetGamma %08x\n", user_table)); + return set_gamma_table(user_table) ? noErr : memFullErr; + } case cscGrayPage: { // Fill page with dithered gray pattern D(bug(" GrayPage %d\n", ReadMacInt16(param + csPage))); @@ -294,7 +536,10 @@ int16 VideoDriverControl(uint32 pb, uint p += VidLocal.desc->mode.bytes_per_row; pat = ~pat; } - //!! if direct mode, load gamma table to CLUT + + if (IsDirectMode(VidLocal.current_mode)) + load_ramp_palette(); + return noErr; } @@ -327,61 +572,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 - 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; - - // 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; @@ -483,9 +677,9 @@ int16 VideoDriverStatus(uint32 pb, uint3 return noErr; case cscGetGamma: - D(bug(" GetGamma -> \n")); - //!! - return statusErr; + D(bug(" GetGamma -> %08x\n", VidLocal.gamma_table)); + WriteMacInt32(param + csGTable, VidLocal.gamma_table); + return noErr; case cscGetDefaultMode: // Get default color depth D(bug(" GetDefaultMode -> %04x\n", VidLocal.preferred_mode)); @@ -581,8 +775,8 @@ int16 VideoDriverStatus(uint32 pb, uint3 uint16 mode = ReadMacInt16(param + csDepthMode); D(bug(" GetVideoParameters %04x/%08x\n", mode, 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 (DepthToAppleMode(i->depth) == mode && i->resolution_id == id) { uint32 vp = ReadMacInt32(param + csVPBlockPtr); WriteMacInt32(vp + vpBaseOffset, 0); @@ -637,7 +831,6 @@ int16 VideoDriverStatus(uint32 pb, uint3 WriteMacInt32(param + csDeviceType, dev_type); return noErr; } - ++i; } return paramErr; // specified resolution/depth not supported }