From 29ca840557a6080167f31e21fe2948328fd34019 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sat, 7 Nov 2026 08:54:13 +0100 Subject: [PATCH 1/4] reimplement: SHC_3BB0A8C1_0x00451E00 92.5% gynt's asm with the zoomed path's surface pointer fixed Draft. renderUnitAnimationWithBlendingUnk blends a unit animation's TGX picture into the surface with the player's colour palette; four asm loops (RGB555 and RGB565, zoomed and not). From gynt's renderUnitAnimationWithBlendingUnk.cpp in 3c9bcdb72 (origin/reimpl/TextureRenderCore, 2026-10-04, 91.5% there, "Four asm loops recovered, stack slot allocation differs"). Fixed: his two zoomed-path asm blocks load the surface pointer from width, which that path never sets (only the unzoomed path assigns width = the surface pointer), so they draw through the picture width as a pointer; all four blocks now read DAT_TextureRenderCoreObject::instance.currentRenderSurface, as the original does, and the two width assignments are gone. Also: main's colorPaletteOverride for his mbr_0x70; 32, 32 * 512, 512 and 4056 * 2 for 0x20, 0x4000, 0x200 and 0x1fb0; the two unused usings dropped. Left: the stack slots. The asm-only _columnOffset always gets -4 in our listing where the original has it at -16, between colorPalette and dstFilter, which moves the C locals by one slot each; its declaration position, a block scope and a (void) reference do not move it. The C part also keeps height in another register (the original holds it in edi from the first test on). Measured in a clean alone build of the branch on main 1c294596c: 92.5% raw, 84.8% normalized. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../renderUnitAnimationWithBlendingUnk.cpp | 932 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 933 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp new file mode 100644 index 0000000000..a958b97596 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/renderUnitAnimationWithBlendingUnk.cpp @@ -0,0 +1,932 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/UI/Rendering.func.hpp" +#include "OpenSHC/Rendering/ColorMode.hpp" +#include "OpenSHC/Rendering/Enums/RenderTarget.hpp" + +#include "OpenSHC/Globals/DAT_BlendFilterArrays.hpp" +#include "OpenSHC/Globals/DAT_BlendingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_CurrentlyRenderedSpriteID.hpp" +#include "OpenSHC/Globals/DAT_RenderedUnitOwner.hpp" +#include "OpenSHC/Globals/DAT_TextureRenderCoreObject.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // NOTE: The four drawing loops are hand-written assembly in the original: they materialise + // zero with mov reg,0, compare with cmp reg,0 rather than test, load the same token byte + // twice into two registers, and use mul for the row offset. Only the owner swap, the + // palette and blend filter setup, the surface selection and the height clamp are + // ordinary C++. + // + // Each loop walks TGX tokens whose pixels are byte indices into the sprite's player + // colour palette, and blends the looked-up colour over the destination through a pair of + // 512-byte filter rows of DAT_BlendFilterArrays picked by blendStrengthUnk: row + // [blendStrengthUnk] weights the destination and row [0x20 - blendStrengthUnk] the + // source. The row start is kept on the stack with push edi so a newline token can + // restore it and step one whole line. There are four loops because the component masks + // differ between RGB555 and RGB565, and because the zoomed-out surface stores only every + // second column, so those two loops track a running column offset seeded from xPos & 1 + // and skip odd columns with test ebx,1. + + // FUNCTION: STRONGHOLDCRUSADER 0x00451E00 + void TextureRenderCore::renderUnitAnimationWithBlendingUnk( + int xPos, int yPos, int width, int height, byte* imageSource, int blendStrengthUnk) + { + int _lineByteWidth; + int _heightStart; + short* _colorPalette; + int _columnOffset; + short* _dstFilter; + short* _srcFilter; + int _startColumnParity; + int _heightEnd; + if (DAT_RenderedUnitOwner::instance == 1) { + DAT_RenderedUnitOwner::instance = 4; + } else if (DAT_RenderedUnitOwner::instance == 4) { + DAT_RenderedUnitOwner::instance = 1; + } + _colorPalette = this->gmFileHeaderColorpaletteArray[DAT_CurrentlyRenderedSpriteID::instance] + .colorPalette[DAT_BlendingDefinedData::instance + .PlayerSlotUnitColor[DAT_RenderedUnitOwner::instance]]; + if (height <= 0) { + return; + } + if (this->colorPaletteOverride != 0) { + _colorPalette = (short*)this->colorPaletteOverride; + } + if (blendStrengthUnk >= 32) { + return; + } + if (DAT_BlendFilterArrays::instance[32][31][0] == 0) { + MACRO_CALL(OpenSHC::UI::Rendering_Func::InitBlendFilterArraysUnk)(); + } + _dstFilter = (short*)((char*)DAT_BlendFilterArrays::instance + blendStrengthUnk * 512); + _srcFilter = (short*)((char*)DAT_BlendFilterArrays::instance + (32 * 512 - blendStrengthUnk * 512)); + if (DAT_TextureRenderCoreObject::instance.isZoom2 == 0) { + if (this->drawBufferChoiceValue != OpenSHC::Rendering::Enums::RT_SCREEN_MENU) { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + _heightStart = this->mapGameSurfaceHeightRange.start; + _heightEnd = this->mapGameSurfaceHeightRange.end; + blendStrengthUnk = 4056 * 2; + _lineByteWidth = 4056 * 2; + } else { + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu; + blendStrengthUnk = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + _lineByteWidth = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine; + _heightStart = this->screenMenuSurfaceHeightRange.start; + _heightEnd = this->screenMenuSurfaceHeightRange.end; + } + if (height + yPos > _heightEnd) { + height = _heightEnd - yPos; + if (height <= 0) { + return; + } + } + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_555) { + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl a5done + add eax, eax + add edi, eax + mov eax, yPos + mov ebx, _heightStart + cmp eax, ebx + jge a5addRowOffset + sub ebx, eax + mov eax, ebx + cmp height, eax + jle a5done + sub height, eax + a5skip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je a5skip + cmp bl, 0 + je a5skipStream + cmp bl, 40h + je a5skipRepeat + jmp a5skipNewline + a5skipStream: + add ecx, 1 + add esi, ecx + jmp a5skip + a5skipRepeat: + add esi, 1 + jmp a5skip + a5skipNewline: + sub eax, 1 + cmp eax, 0 + jg a5skip + mov eax, _heightStart + a5addRowOffset: + mov edx, _lineByteWidth + mul edx + add edi, eax + mov edx, _colorPalette + a5row: + push edi + a5token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, 0E0h + and cl, 1Fh + add esi, 1 + cmp al, 20h + je a5transparent + cmp al, 0 + je a5stream + cmp al, 40h + je a5repeat + pop edi + add edi, blendStrengthUnk + sub height, 1 + cmp height, 0 + jg a5row + jmp a5done + a5transparent: + add ecx, 1 + add ecx, ecx + add edi, ecx + jmp a5token + a5repeat: + mov ebx, 0 + mov eax, 0 + mov bl, byte ptr [esi] + mov ax, word ptr [edx + ebx*2] + add esi, 1 + add ecx, 1 + push edx + mov edx, _srcFilter + mov ebx, 0 + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0FC1Fh + or ax, bx + mov bx, ax + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 3FFh + or ax, bx + a5repeatLoop: + mov dx, word ptr [edi] + push ax + mov eax, _dstFilter + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0FC1Fh + or dx, bx + mov bx, dx + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 83FFh + or dx, bx + pop ax + add dx, ax + mov word ptr [edi], dx + add edi, 2 + sub ecx, 1 + jne a5repeatLoop + pop edx + jmp a5token + a5stream: + mov ebx, 0 + add ecx, 1 + a5streamLoop: + mov eax, 0 + mov al, byte ptr [esi] + mov ax, word ptr [edx + eax*2] + push edx + mov edx, _srcFilter + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0FC1Fh + or ax, bx + mov bx, ax + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 3FFh + or ax, bx + mov dx, word ptr [edi] + mov word ptr [edi], ax + mov eax, _dstFilter + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0FC1Fh + or dx, bx + mov bx, dx + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 83FFh + or dx, bx + add word ptr [edi], dx + pop edx + add esi, 1 + add edi, 2 + sub ecx, 1 + jne a5streamLoop + jmp a5token + a5done: + } + return; + } + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl a6done + add eax, eax + add edi, eax + mov eax, yPos + mov ebx, _heightStart + cmp eax, ebx + jge a6addRowOffset + sub ebx, eax + mov eax, ebx + cmp height, eax + jle a6done + sub height, eax + a6skip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je a6skip + cmp bl, 0 + je a6skipStream + cmp bl, 40h + je a6skipRepeat + jmp a6skipNewline + a6skipStream: + add ecx, 1 + add esi, ecx + jmp a6skip + a6skipRepeat: + add esi, 1 + jmp a6skip + a6skipNewline: + sub eax, 1 + cmp eax, 0 + jg a6skip + mov eax, _heightStart + a6addRowOffset: + mov edx, _lineByteWidth + mul edx + add edi, eax + mov edx, _colorPalette + a6row: + push edi + a6token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, 0E0h + and cl, 1Fh + add esi, 1 + cmp al, 20h + je a6transparent + cmp al, 0 + je a6stream + cmp al, 40h + je a6repeat + pop edi + add edi, blendStrengthUnk + sub height, 1 + cmp height, 0 + jg a6row + jmp a6done + a6transparent: + add ecx, 1 + add ecx, ecx + add edi, ecx + jmp a6token + a6repeat: + mov ebx, 0 + mov eax, 0 + mov bl, byte ptr [esi] + mov ax, word ptr [edx + ebx*2] + add esi, 1 + push esi + add ecx, 1 + push edx + mov esi, _srcFilter + mov ebx, 0 + mov bx, ax + and bx, 1Fh + mov dx, word ptr [esi + ebx*8] + mov bx, ax + shr bx, 5 + and bx, 3Fh + or dx, word ptr [esi + ebx*8 + 2] + mov bx, ax + shr bx, 0Bh + and bx, 1Fh + or dx, word ptr [esi + ebx*8 + 4] + mov ax, dx + mov esi, _dstFilter + a6repeatLoop: + mov dx, word ptr [edi] + push ax + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov ax, word ptr [esi + ebx*8] + mov bx, dx + shr bx, 5 + and bx, 3Fh + or ax, word ptr [esi + ebx*8 + 2] + mov bx, dx + shr bx, 0Bh + and bx, 1Fh + or ax, word ptr [esi + ebx*8 + 4] + mov dx, ax + pop ax + add dx, ax + mov word ptr [edi], dx + add edi, 2 + sub ecx, 1 + jne a6repeatLoop + pop edx + pop esi + jmp a6token + a6stream: + add ecx, 1 + a6streamLoop: + mov eax, 0 + mov al, byte ptr [esi] + mov ax, word ptr [edx + eax*2] + push ecx + push edx + mov edx, _srcFilter + mov ebx, 0 + mov bx, ax + and bx, 1Fh + mov cx, word ptr [edx + ebx*8] + mov bx, ax + shr bx, 5 + and bx, 3Fh + or cx, word ptr [edx + ebx*8 + 2] + mov bx, ax + shr bx, 0Bh + and bx, 1Fh + or cx, word ptr [edx + ebx*8 + 4] + mov dx, word ptr [edi] + mov eax, _dstFilter + mov ebx, 0 + mov bx, dx + and bx, 1Fh + add cx, word ptr [eax + ebx*8] + mov bx, dx + shr bx, 5 + and bx, 3Fh + add cx, word ptr [eax + ebx*8 + 2] + mov bx, dx + shr bx, 0Bh + and bx, 1Fh + add cx, word ptr [eax + ebx*8 + 4] + mov word ptr [edi], cx + pop edx + pop ecx + add esi, 1 + add edi, 2 + sub ecx, 1 + jne a6streamLoop + jmp a6token + a6done: + } + return; + } + DAT_TextureRenderCoreObject::instance.currentRenderSurface + = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame; + _heightStart = this->mapGameSurfaceHeightRange.start; + _heightEnd = this->mapGameSurfaceHeightRange.end; + _startColumnParity = xPos & 1; + blendStrengthUnk = 4056 * 2; + _lineByteWidth = 4056 * 2; + if (height + yPos > _heightEnd) { + height = _heightEnd - yPos; + if (height <= 0) { + return; + } + } + if (DAT_WindowAndDirectDraw::instance.colorBitMode == OpenSHC::Rendering::RGB_555) { + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl b5done + and eax, 0FFFFFFFEh + add edi, eax + mov eax, yPos + mov ebx, _heightStart + cmp eax, ebx + jge b5addRowOffset + sub ebx, eax + mov eax, ebx + cmp height, eax + jle b5done + sub height, eax + b5skip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je b5skip + cmp bl, 0 + je b5skipStream + cmp bl, 40h + je b5skipRepeat + jmp b5skipNewline + b5skipStream: + add ecx, 1 + add esi, ecx + jmp b5skip + b5skipRepeat: + add esi, 1 + jmp b5skip + b5skipNewline: + sub eax, 1 + cmp eax, 0 + jg b5skip + mov eax, _heightStart + b5addRowOffset: + mov edx, _lineByteWidth + mov ecx, eax + shr eax, 1 + mul edx + add edi, eax + mov edx, _colorPalette + b5row: + mov ebx, _startColumnParity + mov _columnOffset, ebx + push edi + b5token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, 0E0h + and cl, 1Fh + add esi, 1 + cmp al, 20h + je b5transparent + cmp al, 0 + je b5stream + cmp al, 40h + je b5repeat + pop edi + add edi, blendStrengthUnk + sub height, 1 + cmp height, 0 + jle b5done + b5rowSkip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je b5rowSkip + cmp bl, 0 + je b5rowSkipStream + cmp bl, 40h + je b5rowSkipRepeat + jmp b5rowSkipDone + b5rowSkipStream: + add ecx, 1 + add esi, ecx + jmp b5rowSkip + b5rowSkipRepeat: + add esi, 1 + jmp b5rowSkip + b5rowSkipDone: + sub height, 1 + cmp height, 0 + jg b5row + jmp b5done + b5transparent: + add ecx, 1 + add _columnOffset, ecx + jmp b5token + b5repeat: + mov ebx, 0 + mov eax, 0 + mov bl, byte ptr [esi] + mov ax, word ptr [edx + ebx*2] + add esi, 1 + push edx + mov edx, _srcFilter + mov ebx, 0 + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0FC1Fh + or ax, bx + mov bx, ax + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 3FFh + or ax, bx + add ecx, 1 + mov ebx, _columnOffset + add _columnOffset, ecx + b5repeatLoop: + test ebx, 1 + jne b5repeatNext + mov dx, word ptr [edi + ebx] + push ax + push ebx + mov eax, _dstFilter + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0FC1Fh + or dx, bx + mov bx, dx + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 83FFh + or dx, bx + pop ebx + pop ax + add dx, ax + mov word ptr [edi + ebx], dx + b5repeatNext: + add ebx, 1 + sub ecx, 1 + jne b5repeatLoop + pop edx + jmp b5token + b5stream: + mov eax, 0 + add ecx, 1 + mov ebx, _columnOffset + add _columnOffset, ecx + b5streamLoop: + test ebx, 1 + jne b5streamNext + push edx + push ebx + mov eax, 0 + mov al, byte ptr [esi] + mov ax, word ptr [edx + eax*2] + mov edx, _srcFilter + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0FC1Fh + or ax, bx + mov bx, ax + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 3FFh + or ax, bx + pop ebx + mov dx, word ptr [edi + ebx] + mov word ptr [edi + ebx], ax + mov eax, _dstFilter + push ebx + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0FC1Fh + or dx, bx + mov bx, dx + shr bx, 0Ah + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 83FFh + or dx, bx + pop ebx + add word ptr [edi + ebx], dx + pop edx + b5streamNext: + add esi, 1 + add ebx, 1 + sub ecx, 1 + jne b5streamLoop + jmp b5token + b5done: + } + return; + } + __asm { + mov esi, imageSource + mov edi, DAT_TextureRenderCoreObject::instance.currentRenderSurface + mov eax, xPos + cmp eax, 0 + jl b6done + and eax, 0FFFFFFFEh + add edi, eax + mov eax, yPos + mov ebx, _heightStart + cmp eax, ebx + jge b6addRowOffset + sub ebx, eax + mov eax, ebx + cmp height, eax + jle b6done + sub height, eax + b6skip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je b6skip + cmp bl, 0 + je b6skipStream + cmp bl, 40h + je b6skipRepeat + jmp b6skipNewline + b6skipStream: + add ecx, 1 + add esi, ecx + jmp b6skip + b6skipRepeat: + add esi, 1 + jmp b6skip + b6skipNewline: + sub eax, 1 + cmp eax, 0 + jg b6skip + mov eax, _heightStart + b6addRowOffset: + mov edx, _lineByteWidth + mov ecx, eax + shr eax, 1 + mul edx + add edi, eax + mov edx, _colorPalette + b6row: + mov ebx, _startColumnParity + mov _columnOffset, ebx + push edi + b6token: + mov ecx, 0 + mov al, byte ptr [esi] + mov cl, byte ptr [esi] + and al, 0E0h + and cl, 1Fh + add esi, 1 + cmp al, 20h + je b6transparent + cmp al, 0 + je b6stream + cmp al, 40h + je b6repeat + pop edi + add edi, blendStrengthUnk + sub height, 1 + cmp height, 0 + jle b6done + b6rowSkip: + mov ecx, 0 + mov bl, byte ptr [esi] + mov cl, byte ptr [esi] + and bl, 0E0h + and cl, 1Fh + add esi, 1 + cmp bl, 20h + je b6rowSkip + cmp bl, 0 + je b6rowSkipStream + cmp bl, 40h + je b6rowSkipRepeat + jmp b6rowSkipDone + b6rowSkipStream: + add ecx, 1 + add esi, ecx + jmp b6rowSkip + b6rowSkipRepeat: + add esi, 1 + jmp b6rowSkip + b6rowSkipDone: + sub height, 1 + cmp height, 0 + jg b6row + jmp b6done + b6transparent: + add ecx, 1 + add _columnOffset, ecx + jmp b6token + b6repeat: + mov ebx, 0 + mov eax, 0 + mov bl, byte ptr [esi] + mov ax, word ptr [edx + ebx*2] + add esi, 1 + push edx + mov edx, _srcFilter + mov ebx, 0 + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 3Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0F81Fh + or ax, bx + mov bx, ax + shr bx, 0Bh + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 7FFh + or ax, bx + add ecx, 1 + mov ebx, _columnOffset + add _columnOffset, ecx + b6repeatLoop: + test ebx, 1 + jne b6repeatNext + mov dx, word ptr [edi + ebx] + push ax + push ebx + mov eax, _dstFilter + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 3Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0F81Fh + or dx, bx + mov bx, dx + shr bx, 0Bh + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 7FFh + or dx, bx + pop ebx + pop ax + add dx, ax + mov word ptr [edi + ebx], dx + b6repeatNext: + add ebx, 1 + sub ecx, 1 + jne b6repeatLoop + pop edx + jmp b6token + b6stream: + mov eax, 0 + add ecx, 1 + mov ebx, _columnOffset + add _columnOffset, ecx + b6streamLoop: + test ebx, 1 + jne b6streamNext + push edx + push ebx + mov eax, 0 + mov al, byte ptr [esi] + mov ax, word ptr [edx + eax*2] + mov edx, _srcFilter + mov bx, ax + and bx, 1Fh + mov bx, word ptr [edx + ebx*8] + and ax, 0FFE0h + or ax, bx + mov bx, ax + shr bx, 5 + and bx, 3Fh + mov bx, word ptr [edx + ebx*8 + 2] + and ax, 0F81Fh + or ax, bx + mov bx, ax + shr bx, 0Bh + and bx, 1Fh + mov bx, word ptr [edx + ebx*8 + 4] + and ax, 7FFh + or ax, bx + pop ebx + mov dx, word ptr [edi + ebx] + mov word ptr [edi + ebx], ax + mov eax, _dstFilter + push ebx + mov ebx, 0 + mov bx, dx + and bx, 1Fh + mov bx, word ptr [eax + ebx*8] + and dx, 0FFE0h + or dx, bx + mov bx, dx + shr bx, 5 + and bx, 3Fh + mov bx, word ptr [eax + ebx*8 + 2] + and dx, 0F81Fh + or dx, bx + mov bx, dx + shr bx, 0Bh + and bx, 1Fh + mov bx, word ptr [eax + ebx*8 + 4] + and dx, 7FFh + or dx, bx + pop ebx + add word ptr [edi + ebx], dx + pop edx + b6streamNext: + add esi, 1 + add ebx, 1 + sub ecx, 1 + jne b6streamLoop + jmp b6token + b6done: + } + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 6e036f9340..dddbaed7f2 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -16963,7 +16963,7 @@ SHC_3BB0A8C1_0x0044F850 | 0.0% | Pending SHC_3BB0A8C1_0x0044FBF0 | 0.0% | Pending -SHC_3BB0A8C1_0x00451E00 | 0.0% | Pending +SHC_3BB0A8C1_0x00451E00 | 92.5% | gynt's asm with the zoomed path's surface pointer fixed; stack slots of the asm-only column offset and the C locals SHC_3BB0A8C1_0x00452900 | 100.0% | Reimplemented From adec92b2b23163552aab4eebef09da369bff635c Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sat, 7 Nov 2026 08:54:13 +0100 Subject: [PATCH 2/4] reimplement: SHC_3BB0A8C1_0x00454F00 84.8% gynt's asm without his two extra locals Draft. moveOverlappingMenuPartsToMapSurface copies the parts of the open menu tab that reach over the map from the menu surface onto the map surface, all but the magenta pixels; the copy loop is handwritten assembly in the original. From gynt's asm version in c2e547420 (origin/reimpl/TextureRenderCore, 2026-10-04, 68.0% there). Changed: the colour key read from COL_MAGENTA in the asm and the record index inline, which drops his two extra stack slots (the original has ten); the tab in a local, read once as in the original; the line jumps computed before the width is read again, which gives the original's store order before the asm; main's field_0x3014 rows ([55][8], so (tab - 1) * 5) for his field166_0x2dfc; 4056 for 0xfd8. Left: the original computes the record offset, lea [eax + eax*4 - 5] and shl 5, twice and keeps the - 5 in the lea; ours computes it once and folds - 160 into the addresses. Index spellings, the pointers computed before the -1 test and a [11][40] table shape do not change that. Measured in a clean alone build of the branch on main 1c294596c: 84.8% raw, 67.4% normalized. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../moveOverlappingMenuPartsToMapSurface.cpp | 94 +++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp new file mode 100644 index 0000000000..36420e351b --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp @@ -0,0 +1,94 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/Rendering/ScreenResolutionEnum.hpp" + +#include "OpenSHC/Globals/COL_MAGENTA.hpp" +#include "OpenSHC/Globals/DAT_BlendingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_MenuHandlerState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/DAT_WindowAndDirectDraw.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // The copy loop is handwritten assembly in the original, transcribed here from the binary. + + // FUNCTION: STRONGHOLDCRUSADER 0x00454F00 + void TextureRenderCore::moveOverlappingMenuPartsToMapSurface() + { + // copies the parts of the open menu tab that reach over the map from the menu surface onto the map + // surface, all but the magenta pixels; the tab's records hold x, y, width and height, -1 ends them and + // -2 marks the ones that only apply above 800x600 + int const tab = this->activeMenuTabIndex; + if (tab == 0) { + return; + } + if (DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][0] == -1) { + return; + } + // the asm below names COL_MAGENTA, which inline asm can only do once C++ code has instantiated it + (void)sizeof(COL_MAGENTA::instance); + int* width = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][2]; + int* y = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][1]; + int* x = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][0]; + int* height = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][3]; + do { + if (*x == -2) { + if (DAT_WindowAndDirectDraw::instance.currentGameResolution == OpenSHC::Rendering::SRE_800x600) { + return; + } + y += 4; + height += 4; + width += 4; + x += 4; + } + ushort* const screen = (ushort*)((byte*)DAT_WindowAndDirectDraw::instance.surfacePointer_screenMenu + + (*y + DAT_MenuHandlerState::instance.y) + * DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine + + (DAT_MenuHandlerState::instance.x + *x) * 2); + // the map surface is 4056 pixels wide + ushort* const map = DAT_WindowAndDirectDraw::instance.surfacePointer_mapGame + + (*y + DAT_ViewportRenderState::instance.viewportState.currentCameraOffsetY + + DAT_MenuHandlerState::instance.y) + * 4056 + + *x + DAT_ViewportRenderState::instance.viewportState.currentCameraOffsetX + + DAT_MenuHandlerState::instance.x; + int const mapLineJump = (4056 - *width) * 2; + int const screenLineJump = DAT_WindowAndDirectDraw::instance.byteSizeOfOneHorizontalLine - *width * 2; + int const partHeight = *height; + int const partWidth = *width; + // clang-format off + __asm { + mov esi, screen + mov edi, map + mov ecx, partHeight + mov bx, COL_MAGENTA::instance.shortValue + rowLoop: + mov edx, partWidth + pixelLoop: + mov ax, word ptr [esi] + cmp ax, bx + je skipPixel + mov word ptr [edi], ax + skipPixel: + add esi, 2 + add edi, 2 + sub edx, 1 + jne pixelLoop + add esi, screenLineJump + add edi, mapLineJump + sub ecx, 1 + jne rowLoop + } + // clang-format on + y += 4; + height += 4; + width += 4; + x += 4; + } while (*x != -1); + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index dddbaed7f2..ab300d26ff 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17003,7 +17003,7 @@ SHC_3BB0A8C1_0x00454EE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454EF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454F00 | 0.0% | Pending +SHC_3BB0A8C1_0x00454F00 | 84.8% | gynt's asm without his two extra locals; the record offset folded into the addresses SHC_3BB0A8C1_0x00455050 | 0.0% | Pending From d29d51d29702b36d08ab0a503c11eb844b205075 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Sat, 7 Nov 2026 21:03:14 +0100 Subject: [PATCH 3/4] reimplement: SHC_3BB0A8C1_0x004559B0 93.1% one sharing condition for the original's shared blocks Draft. loadGMFile reads a .gm1 file, its header, the offsets, sizes and headers of its pictures and their data, and copies each picture's data behind the ones processed so far, or points it at the previous picture's or another one's data. Written from the original's instructions, with gynt's draft (63cc49fb3 on his fork's reimpl/UI-Rendering-development, 84.5% there) for the reads. The two-way sharing test is one condition, which gives the original's one copy block and one sharing block (the draft's gotos measure lower: quick_diff 0.9326 -> 0.9091; nested if / else without them writes both blocks twice: 0.7759). DAT_GMImageSizes[picture + 115999] in the draft is DAT_GMImageOffsets[picture - 1], the previous picture's offset (the original reads [picture * 4 + 0x00D0BA0C]). Left: the original walks the picture headers with a pointer to alternativeImageIndexUnk and does not re-test (animatedColor & 4) after animatedColor == 0; ours points at animatedColor and re-tests. This file was measured as it stands in a clean alone build of the branch on main eef6f667a: 93.1% raw, 79.7% normalized. Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../TextureRenderCore/loadGMFile.cpp | 100 ++++++++++++++++++ status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/OpenSHC/UI/Rendering/TextureRenderCore/loadGMFile.cpp diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGMFile.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGMFile.cpp new file mode 100644 index 0000000000..c476ec7767 --- /dev/null +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/loadGMFile.cpp @@ -0,0 +1,100 @@ +#include "../TextureRenderCore.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/IO/ResourceManager.func.hpp" +#include "OpenSHC/IO/FileResourceType.hpp" +#include "OpenSHC/IO/Graphics/GmImageType.hpp" +#include "OpenSHC/string-literals.hpp" + +#include "OpenSHC/Globals/DAT_GMImageHeaders.hpp" +#include "OpenSHC/Globals/DAT_GMImageOffsets.hpp" +#include "OpenSHC/Globals/DAT_GMImageSizes.hpp" +#include "OpenSHC/Globals/DAT_LoadingBarProgress.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PictureNumToGmIDArray_UNUSED.hpp" +#include "OpenSHC/Globals/DAT_ResourceManager.hpp" + +namespace OpenSHC { +namespace UI { + namespace Rendering { + + // FUNCTION: STRONGHOLDCRUSADER 0x004559B0 + void TextureRenderCore::loadGMFile(GmID gmID, char* gmFileName) + { + // keep the window responsive while the files load + MSG message; + if (PeekMessageA(&message, NULL, 0, 0, 0) && GetMessageA(&message, NULL, 0, 0)) { + TranslateMessage(&message); + DispatchMessageA(&message); + } + BOOLEnum const flag = this->unknownSfxAndGmRelatedFlag; + int totalSize = 0; + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(this->gmFileHeaderColorpaletteArray[gmID]), 0, &this->gmFileHeaderColorpaletteArray[gmID]); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 1000, 0, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]); + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::copyData, DAT_LowLevelMemory::ptr)( + strlen(gmFileName), gmFileName, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]); + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::resolveResourceFileName, DAT_ResourceManager::ptr)( + OpenSHC::IO::FRT_GM, this->gmFileNameArray_UNUSEDUnk_0x13179c[gmID]); + + // the header, the offsets, sizes and headers of its pictures, then their data + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readFirstPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(&this->gmFileHeaderColorpaletteArray[gmID], + sizeof(this->gmFileHeaderColorpaletteArray[gmID]), (char*)s_gm1_005a5068); + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(&DAT_GMImageOffsets::instance[this->gmNumberOfProcessedPictures], + this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 4, (char*)s_gm1_005a5068); + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(&DAT_GMImageSizes::instance[this->gmNumberOfProcessedPictures], + this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 4, (char*)s_gm1_005a5068); + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(&DAT_GMImageHeaders::instance.imh[this->gmNumberOfProcessedPictures], + this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile * 16, (char*)s_gm1_005a5068); + MACRO_CALL_MEMBER(OpenSHC::IO::ResourceManager_Func::readNextPartOfCurrentResourceIntoMemory, + DAT_ResourceManager::ptr)(this->gmAndGfxImageDataBuffer, + this->gmFileHeaderColorpaletteArray[gmID].dataSize, (char*)s_gm1_005a5068); + if (this->gmFileHeaderColorpaletteArray[gmID].ImageType == OpenSHC::IO::Graphics::GIT_Animation) { + MACRO_CALL_MEMBER( + OpenSHC::UI::Rendering::TextureRenderCore_Func::transformGmColorTableFromRGB555To565IfRequired, + this)(gmID); + } + + // copy each picture's data behind the ones processed so far; a picture that shares another one's data + // points at it instead + int offset = DAT_GMImageOffsets::instance[this->gmNumberOfProcessedPictures]; + for (int picture = this->gmNumberOfProcessedPictures; picture + < this->gmFileHeaderColorpaletteArray[gmID].numberOfPicturesInFile + this->gmNumberOfProcessedPictures; + ++picture) { + DAT_PictureNumToGmIDArray_UNUSED::instance[picture] = gmID; + if ((flag && DAT_GMImageHeaders::instance.imh[picture].animatedColor != 0) + || (DAT_GMImageHeaders::instance.imh[picture].animatedColor & 4) != 0) { + if ((DAT_GMImageHeaders::instance.imh[picture].animatedColor & 4) != 0) { + DAT_GMImageOffsets::instance[picture] = DAT_GMImageOffsets::instance[picture - 1]; + } else { + DAT_GMImageOffsets::instance[picture] = DAT_GMImageOffsets::instance + [DAT_GMImageHeaders::instance.imh[picture].alternativeImageIndexUnk + picture]; + } + DAT_GMImageSizes::instance[picture] + = DAT_GMImageSizes::instance[DAT_GMImageHeaders::instance.imh[picture].alternativeImageIndexUnk + + picture]; + } else { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::copyData, DAT_LowLevelMemory::ptr)( + DAT_GMImageSizes::instance[picture], + this->gmAndGfxImageDataBuffer + DAT_GMImageOffsets::instance[picture], + this->gmProcessedImageData + this->gmSizeOfProcessedPictures_0x44 + offset); + DAT_GMImageOffsets::instance[picture] = this->gmSizeOfProcessedPictures_0x44 + offset; + totalSize += DAT_GMImageSizes::instance[picture]; + offset += DAT_GMImageSizes::instance[picture]; + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::adaptGmColorsToRGB565IfRequired, + this)(gmID, picture); + } + } + this->gmSizeOfProcessedPictures_0x44 += totalSize; + MACRO_CALL_MEMBER(OpenSHC::UI::Rendering::TextureRenderCore_Func::drawLoadingBarUnk, this)( + gmID, DAT_LoadingBarProgress::instance + 180); + } + + } +} +} diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index ab300d26ff..1b4fb37500 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17029,7 +17029,7 @@ SHC_3BB0A8C1_0x004558E0 | 0.0% | Pending SHC_3BB0A8C1_0x00455930 | 0.0% | Pending -SHC_3BB0A8C1_0x004559B0 | 0.0% | Pending +SHC_3BB0A8C1_0x004559B0 | 93.1% | one sharing condition for the original's shared blocks; the header pointer's base and one re-test SHC_3BB0A8C1_0x00455B70 | 0.0% | Pending From 31715517f7a74f2470919aa07715ac892e07315d Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Mon, 9 Nov 2026 00:41:17 +0100 Subject: [PATCH 4/4] reimplement: SHC_3BB0A8C1_0x00454F00 96.5% the record index as a local Draft. moveOverlappingMenuPartsToMapSurface (adec92b2b, gynt's asm without his two extra locals) now takes the tab's record index, (tab - 1) * 5, into a local of its own for the four pointers. The original computes that index a second time after the -1 test (lea and shl again) instead of reusing the test's, and keeps the - 5 in the index rather than folding it into the addresses; with the index inline MSVC shares one computation, 98 instructions to the original's 100. The pointers are set up in the original's order (width, x, height, y). PR #276's structural_diff.py now reads kind 1.000 with no instruction-mix delta (was 0.980, delta 2), and slot_map.py shows every stack slot at the original's offset. Left: the -1 test's own index still has its - 5 folded into the address (as a local, cast to a row pointer, as tab * 5 - 5, or as a second local, it folds the same). Measured in a build of the changed draft files alone on the branch (main e32319b21): 96.5% raw, 88.1% normalized (was 84.8% and 67.4%). Co-authored-by: Gynt Co-Authored-By: Claude Opus 5.5 --- .../moveOverlappingMenuPartsToMapSurface.cpp | 10 ++++++---- status/addresses-SHC-3BB0A8C1.txt | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp b/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp index 36420e351b..9b223576c0 100644 --- a/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp +++ b/src/OpenSHC/UI/Rendering/TextureRenderCore/moveOverlappingMenuPartsToMapSurface.cpp @@ -29,10 +29,12 @@ namespace UI { } // the asm below names COL_MAGENTA, which inline asm can only do once C++ code has instantiated it (void)sizeof(COL_MAGENTA::instance); - int* width = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][2]; - int* y = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][1]; - int* x = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][0]; - int* height = &DAT_BlendingDefinedData::instance.field_0x3014[(tab - 1) * 5][3]; + // the index in a local: the original computes it again here (inline: 98 instructions to its 100) + int const record = (tab - 1) * 5; + int* width = &DAT_BlendingDefinedData::instance.field_0x3014[record][2]; + int* x = &DAT_BlendingDefinedData::instance.field_0x3014[record][0]; + int* height = &DAT_BlendingDefinedData::instance.field_0x3014[record][3]; + int* y = &DAT_BlendingDefinedData::instance.field_0x3014[record][1]; do { if (*x == -2) { if (DAT_WindowAndDirectDraw::instance.currentGameResolution == OpenSHC::Rendering::SRE_800x600) { diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 9f4f2f23aa..81f2da623e 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -17003,7 +17003,7 @@ SHC_3BB0A8C1_0x00454EE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00454EF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00454F00 | 84.8% | gynt's asm without his two extra locals; the record offset folded into the addresses +SHC_3BB0A8C1_0x00454F00 | 96.5% | the record index as a local, the pointers in the original's order; the test's index folded into its address SHC_3BB0A8C1_0x00455050 | 100.0% | Reimplemented