Param (
        [Parameter(Mandatory=$true)][String]$SourcePath,
        [Parameter(Mandatory=$true)][String]$DestinationPath
)

function ReadByteOfData {
        $Value = [Int]($Script:SourceBytes[$Script:SourcePosition])
        $Script:SourcePosition += 1
        return $Value
}

function ReadWordOfData {
        $Value = [Int]($Script:SourceBytes[$Script:SourcePosition]) + ([Int]($Script:SourceBytes[$Script:SourcePosition + 1]) -shl 8)
        $Script:SourcePosition += 2
        return $Value
}

function ReadDWordOfData {
        $Value = [Int]($Script:SourceBytes[$Script:SourcePosition]) + ([Int]($Script:SourceBytes[$Script:SourcePosition + 1]) -shl 8) + ([Int]($Script:SourceBytes[$Script:SourcePosition + 2]) -shl 16) + ([Int]($Script:SourceBytes[$Script:SourcePosition + 3]) -shl 24)
        $Script:SourcePosition += 4
        return $Value
}



if (-not (Test-Path -Path $SourcePath -PathType Leaf)) {
        Write-Host "[$SourcePath]: file not found" -ForegroundColor Red -BackgroundColor Black
        exit
}
if (-not (Test-Path -Path $DestinationPath -PathType Container)) {
        Write-Host "[$DestinationPath]: folder not found" -ForegroundColor Red -BackgroundColor Black
        exit
}

$OutputFileNamePrefix = "$DestinationPath\$($SourcePath.Substring($SourcePath.LastIndexOf("\") + 1))"

$SourceBytes = Get-Content -Encoding Byte -Path $SourcePath
$SourcePosition = 0

$DumpFile = New-Object -TypeName System.Text.StringBuilder
$HeaderFile = New-Object -TypeName System.Text.StringBuilder
$RelocFile = New-Object -TypeName System.Text.StringBuilder

$null = $DumpFile.AppendLine("longBYTECNT`tlongRESSPC`tlongLENGTH`tbyteUndefined`tbyteLABLEN`tbyteNUMLEN`tbyteVERSION`tlongBANKSIZE`twordKIND`twordUndefined`tlongORG`tlongALIGN`tbyteNUMSEX`tbyteUndefined2`twordSEGNUM`tlongENTRY`twordDISPNAME`twordDISPDATA`thexLOADNAME`tstrSEGNAME")
$null = $HeaderFile.AppendLine("longBYTECNT`tlongRESSPC`tlongLENGTH`tbyteUndefined`tbyteLABLEN`tbyteNUMLEN`tbyteVERSION`tlongBANKSIZE`twordKIND`twordUndefined`tlongORG`tlongALIGN`tbyteNUMSEX`tbyteUndefined2`twordSEGNUM`tlongENTRY`twordDISPNAME`twordDISPDATA`thexLOADNAME`tstrSEGNAME")

$LCONSTBytes = New-Object Byte[] (65536)

$RelocationStartedOnce = $false
$SegmentIndex = 0
while ($SourcePosition -lt $SourceBytes.Count) {
        $SegmentIndex = $SegmentIndex + 1
        $SegmentOffset = $SourcePosition
        $LCONSTOffset = 0
        $RelocationStarted = $false
        $ReachedSegmentEnd = $false
        
        $longBYTECNT = ReadDWordOfData
        $longRESSPC = ReadDWordOfData
        $longLENGTH = ReadDWordOfData
        $byteUndefined = ReadByteOfData
        $byteLABLEN = ReadByteOfData
        $byteNUMLEN = ReadByteOfData
        $byteVERSION = ReadByteOfData
        $longBANKSIZE = ReadDWordOfData
        $wordKIND = ReadWordOfData
        $wordUndefined = ReadWordOfData
        $longORG = ReadDWordOfData
        $longALIGN = ReadDWordOfData
        $byteNUMSEX = ReadByteOfData
        $byteUndefined2 = ReadByteOfData
        $wordSEGNUM = ReadWordOfData
        $longENTRY = ReadDWordOfData
        $wordDISPNAME = ReadWordOfData
        $wordDISPDATA = ReadWordOfData
        
        if ($byteVERSION -eq 1) {
                $longBYTECNT = $longBYTECNT * 512
        }
        
        $null = $DumpFile.AppendLine("---------- Segment #" + $SegmentIndex + "----------")
        $null = $DumpFile.Append(("{0:X8}" -f $longBYTECNT) + "`t" + ("{0:X8}" -f $longRESSPC) + "`t" + ("{0:X8}" -f $longLENGTH) + "`t" + ("{0:X2}" -f $byteUndefined) + "`t" + ("{0:X2}" -f $byteLABLEN) + "`t" + ("{0:X2}" -f $byteNUMLEN) + "`t" + ("{0:X2}" -f $byteVERSION) + "`t" + ("{0:X8}" -f $longBANKSIZE) + "`t" + ("{0:X4}" -f $wordKIND) + "`t" + ("{0:X4}" -f $wordUndefined) + "`t" + ("{0:X8}" -f $longORG) + "`t" + ("{0:X8}" -f $longALIGN) + "`t" + ("{0:X2}" -f $byteNUMSEX) + "`t" + ("{0:X2}" -f $byteUndefined2) + "`t" + ("{0:X4}" -f $wordSEGNUM) + "`t" + ("{0:X8}" -f $longENTRY) + "`t" + ("{0:X4}" -f $wordDISPNAME) + "`t" + ("{0:X4}" -f $wordDISPDATA) + "`t")
        $null = $HeaderFile.Append(("{0:X8}" -f $longBYTECNT) + "`t" + ("{0:X8}" -f $longRESSPC) + "`t" + ("{0:X8}" -f $longLENGTH) + "`t" + ("{0:X2}" -f $byteUndefined) + "`t" + ("{0:X2}" -f $byteLABLEN) + "`t" + ("{0:X2}" -f $byteNUMLEN) + "`t" + ("{0:X2}" -f $byteVERSION) + "`t" + ("{0:X8}" -f $longBANKSIZE) + "`t" + ("{0:X4}" -f $wordKIND) + "`t" + ("{0:X4}" -f $wordUndefined) + "`t" + ("{0:X8}" -f $longORG) + "`t" + ("{0:X8}" -f $longALIGN) + "`t" + ("{0:X2}" -f $byteNUMSEX) + "`t" + ("{0:X2}" -f $byteUndefined2) + "`t" + ("{0:X4}" -f $wordSEGNUM) + "`t" + ("{0:X8}" -f $longENTRY) + "`t" + ("{0:X4}" -f $wordDISPNAME) + "`t" + ("{0:X4}" -f $wordDISPDATA) + "`t")
        $null = $DumpFile.Append("[")
        $null = $HeaderFile.Append("[")
        for ($ByteIndex = 1; $ByteIndex -le 10; $ByteIndex++) {
                $Byte = ReadByteOfData
                if (($Byte -ge 32) -and ($Byte -le 127)) {
                        $null = $DumpFile.Append([char]$Byte)
                        $null = $HeaderFile.Append([char]$Byte)
                } else {
                        $null = $DumpFile.Append(("{0:X2}" -f $Byte) + " ")
                        $null = $HeaderFile.Append(("{0:X2}" -f $Byte) + " ")
                }
        }
        $null = $DumpFile.Append("]")
        $null = $HeaderFile.Append("]")
        $null = $DumpFile.Append("`t")
        $null = $HeaderFile.Append("`t")
        if ($byteLABLEN -eq 0) {
                $SegmentNameSize = ReadByteOfData
        } else {
                $SegmentNameSize = $byteLABLEN
        }
        $null = $DumpFile.Append("[")
        $null = $HeaderFile.Append("[")
        for ($ByteIndex = 1; $ByteIndex -le $SegmentNameSize; $ByteIndex++) {
                $Byte = ReadByteOfData
                $null = $DumpFile.Append([char]$Byte)
                $null = $HeaderFile.Append([char]$Byte)
        }
        $null = $DumpFile.Append("]")
        $null = $HeaderFile.Append("]")
        $null = $DumpFile.AppendLine("")
        $null = $HeaderFile.AppendLine("")

        while ($SourcePosition -lt $SegmentOffset + $longBYTECNT) {
                $OMFRecordType = ReadByteOfData
                if (($OMFRecordType -eq 0xE2) -or ($OMFRecordType -eq 0xE3) -or ($OMFRecordType -eq 0xF5) -or ($OMFRecordType -eq 0xF6) -or ($OMFRecordType -eq 0xF7)) { # Start of relocation data
                        if (-not $RelocationStarted) {
                                $null = $DumpFile.AppendLine("OMFRecordType`tType`tInSegment`tRelocateByteCount`tBitShift`tRelocationOffset`tFileNumber`tSegmentNumber`tReferencedOffset`tComputed_RelocationAbsoluteAddress`tComputed_ReferenceAbsoluteAddress")
                                if (-not $RelocationStartedOnce) { # Output header only before the first segment
                                        $null = $RelocFile.AppendLine("OMFRecordType`tType`tInSegment`tRelocateByteCount`tBitShift`tRelocationOffset`tFileNumber`tSegmentNumber`tReferencedOffset`tComputed_RelocationAbsoluteAddress`tComputed_ReferenceAbsoluteAddress")
                                        $RelocationStartedOnce = $true
                                }
                                $RelocationStarted = $true
                        }
                } else {
                        if (-not $ReachedSegmentEnd) {
                                $null = $DumpFile.Append("OMFRecordType: " + ("{0:X2}" -f $OMFRecordType) + " ")
                        }
                }

                switch ($OMFRecordType) {
                        0x00 {
                                if (-not $ReachedSegmentEnd) {
                                        $null = $DumpFile.AppendLine("END")
                                        $ReachedSegmentEnd = $true
                                }
                        }
                        0xF1 {
                                $ByteCount = ReadDWordOfData
                                $null = $DumpFile.AppendLine("DS Size=" + ("{0:X8}" -f $ByteCount))
                                for ($ByteIndex = 1; $ByteIndex -le $ByteCount; $ByteIndex++) {
                                        $LCONSTBytes[$LCONSTOffset] = 0
                                        $LCONSTOffset = $LCONSTOffset + 1
                                        $null = $DumpFile.Append(("{0:X2}" -f 0) + " ")
                                }
                                $null = $DumpFile.AppendLine("")
                        }
                        0xF2 {
                                $ByteCount = ReadDWordOfData
                                $null = $DumpFile.AppendLine("LCONST Size=" + ("{0:X8}" -f $ByteCount))
                                for ($ByteIndex = 0; $ByteIndex -le $ByteCount - 1; $ByteIndex++) {
                                        $LCONSTBytes[$LCONSTOffset] = ReadByteOfData
                                        $null = $DumpFile.Append(("{0:X2}" -f $LCONSTBytes[$LCONSTOffset]) + " ")
                                        $LCONSTOffset = $LCONSTOffset + 1
                                }
                                $null = $DumpFile.AppendLine("")
                        }
                        0xE2 {
                                $RelocateByteCount = ReadByteOfData
                                $BitShift = ReadByteOfData
                                #                         OMFRecordType                        Type                  InSegment                            RelocateByteCount                         BitShift
                                $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "RELOC"     + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "RELOC"     + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $RelocationOffset = ReadDWordOfData
                                $ReferencedOffset = ReadDWordOfData
                                #                         RelocationOffset                        FileNumber                        SegmentNumber                       ReferencedOffset                         Computed_RelocationAbsoluteAddress                                                 Computed_ReferenceAbsoluteAddress
                                $null = $DumpFile.Append(("{0:X8}" -f $RelocationOffset) + "`t"                            + "`t"                             + "`t" + ("{0:X8}" -f $ReferencedOffset) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($ReferencedOffset -band 65535))))
                                $null = $RelocFile.Append(("{0:X8}" -f $RelocationOffset) + "`t"                            + "`t"                             + "`t" + ("{0:X8}" -f $ReferencedOffset) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($ReferencedOffset -band 65535))))
                                $null = $DumpFile.AppendLine("")
                                $null = $RelocFile.AppendLine("")
                        }
                        0xE3 {
                                $RelocateByteCount = ReadByteOfData
                                $BitShift = ReadByteOfData
                                #                         OMFRecordType                        Type                  InSegment                            RelocateByteCount                         BitShift
                                $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "INTERSEG"  + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "INTERSEG"  + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $RelocationOffset = ReadDWordOfData
                                $FileNumber = ReadWordOfData
                                $SegmentNumber = ReadWordOfData
                                $ReferencedOffset = ReadDWordOfData
                                #                         RelocationOffset                        FileNumber                        SegmentNumber                       ReferencedOffset                         Computed_RelocationAbsoluteAddress                                                 Computed_ReferenceAbsoluteAddress
                                $null = $DumpFile.Append(("{0:X8}" -f $RelocationOffset) + "`t" + ("{0:X4}" -f $FileNumber) + "`t" + ("{0:X4}" -f $SegmentNumber) + "`t" + ("{0:X8}" -f $ReferencedOffset) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentNumber * 65536) + ($ReferencedOffset -band 65535))))
                                $null = $RelocFile.Append(("{0:X8}" -f $RelocationOffset) + "`t" + ("{0:X4}" -f $FileNumber) + "`t" + ("{0:X4}" -f $SegmentNumber) + "`t" + ("{0:X8}" -f $ReferencedOffset) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentNumber * 65536) + ($ReferencedOffset -band 65535))))
                                $null = $DumpFile.AppendLine("")
                                $null = $RelocFile.AppendLine("")
                        }
                        0xF5 {
                                $RelocateByteCount = ReadByteOfData
                                $BitShift = ReadByteOfData
                                #                         OMFRecordType                        Type                  InSegment                            RelocateByteCount                         BitShift
                                $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "cRELOC"    + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "cRELOC"    + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $RelocationOffset = ReadWordOfData
                                $ReferencedOffset = ReadWordOfData
                                #                         RelocationOffset                        FileNumber                        SegmentNumber                       ReferencedOffset                         Computed_RelocationAbsoluteAddress                                                 Computed_ReferenceAbsoluteAddress
                                $null = $DumpFile.Append(("{0:X4}" -f $RelocationOffset)  + "`t"                            + "`t"                            + "`t" + ("{0:X4}" -f $ReferencedOffset)  + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + $ReferencedOffset)))
                                $null = $RelocFile.Append(("{0:X4}" -f $RelocationOffset)  + "`t"                            + "`t"                            + "`t" + ("{0:X4}" -f $ReferencedOffset)  + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + $ReferencedOffset)))
                                $null = $DumpFile.AppendLine("")
                                $null = $RelocFile.AppendLine("")
                        }
                        0xF6 {
                                $RelocateByteCount = ReadByteOfData
                                $BitShift = ReadByteOfData
                                #                         OMFRecordType                        Type                  InSegment                            RelocateByteCount                         BitShift
                                $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "cINTERSEG" + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "cINTERSEG" + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + ("{0:X2}" -f $BitShift) + "`t")
                                $RelocationOffset = ReadWordOfData
                                $SegmentNumber = ReadByteOfData
                                $ReferencedOffset = ReadWordOfData
                                #                         RelocationOffset                        FileNumber                        SegmentNumber                       ReferencedOffset                         Computed_RelocationAbsoluteAddress                                                 Computed_ReferenceAbsoluteAddress
                                $null = $DumpFile.Append(("{0:X4}" -f $RelocationOffset)  + "`t"                            + "`t" + ("{0:X4}" -f $SegmentNumber) + "`t" + ("{0:X4}" -f $ReferencedOffset)  + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentNumber * 65536) + $ReferencedOffset)))
                                $null = $RelocFile.Append(("{0:X4}" -f $RelocationOffset)  + "`t"                            + "`t" + ("{0:X4}" -f $SegmentNumber) + "`t" + ("{0:X4}" -f $ReferencedOffset)  + "`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t" + ("{0:X6}" -f (($SegmentNumber * 65536) + $ReferencedOffset)))
                                $null = $DumpFile.AppendLine("")
                                $null = $RelocFile.AppendLine("")
                        }
                        0xF7 {
                                $ByteCount = ReadDWordOfData
                                $SuperType = ReadByteOfData
                                $SuperByteReadCount = 0
                                $SuperPage = 0
                                while ($SuperByteReadCount -lt $ByteCount - 1) {
                                        $Count = ReadByteOfData
                                        $SuperByteReadCount++
                                        if ($Count -ge 0x80) {
                                                $SuperPage += [Int]($Count -band 0x7F)
                                        } else {
                                                for ($ByteIndex = 1; $ByteIndex -le $Count + 1; $ByteIndex++) {
                                                        $RelocationOffset = [Int](ReadByteOfData) + ([Int]$SuperPage * 256)
                                                        switch ($SuperType) {
                                                                { $_ -in (0, 1) } {
                                                                        $RelocateByteCount = $SuperType + 2
                                                                        #                         OMFRecordType                        Type                                             InSegment                            RelocateByteCount                         BitShift      RelocationOffset                        FileNumber                 SegmentNumber
                                                                        $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t"                               + "`t")
                                                                        $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t"                               + "`t")
                                                                }
                                                                2 {
                                                                        $RelocateByteCount = 3
                                                                        $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t"                               + "`t")
                                                                        $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t"                               + "`t")
                                                                }
                                                                { $_ -in (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) } {
                                                                        $RelocateByteCount = 3
                                                                        $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t" + $SuperType - 1 + "`t"                               + "`t")
                                                                        $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t" + $SuperType - 1 + "`t"                               + "`t")
                                                                }
                                                                { $_ -in (14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25) } {
                                                                        $RelocateByteCount = 2
                                                                        $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t" + ("{0:X4}" -f ($SuperType - 13)) + "`t")
                                                                        $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "00" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t" + ("{0:X4}" -f ($SuperType - 13)) + "`t")
                                                                }
                                                                { $_ -in (26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37) } {
                                                                        $RelocateByteCount = 2
                                                                        $null = $DumpFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "F0" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t" + ("{0:X4}" -f ($SuperType - 25)) + "`t")
                                                                        $null = $RelocFile.Append(("{0:X2}" -f $OMFRecordType) + "`t" + "SUPER_Type" + ("{0:X2}" -f $SuperType) + "`t" + ("{0:X2}" -f $SegmentIndex) + "`t" + ("{0:X2}" -f $RelocateByteCount) + "`t" + "F0" + "`t" + ("{0:X4}" -f $RelocationOffset) + "`t"                    + "`t" + ("{0:X4}" -f ($SuperType - 25)) + "`t")
                                                                }
                                                        }
                                                        $ReferencedOffset = 0
                                                        for ($Index = $RelocateByteCount - 1; $Index -ge 0; $Index--) {
                                                                #                 ReferencedOffset
                                                                $null = $DumpFile.Append("{0:X2}" -f $LCONSTBytes[$RelocationOffset + $Index])
                                                                $null = $RelocFile.Append("{0:X2}" -f $LCONSTBytes[$RelocationOffset + $Index])
                                                                $ReferencedOffset = $ReferencedOffset * 256
                                                                $ReferencedOffset = $ReferencedOffset + $LCONSTBytes[$RelocationOffset + $Index]
                                                        }
                                                        # Computed_RelocationAbsoluteAddress
                                                        $null = $DumpFile.Append("`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t")
                                                        $null = $RelocFile.Append("`t" + ("{0:X6}" -f (($SegmentIndex * 65536) + ($RelocationOffset -band 65535))) + "`t")
                                                        # Computed_ReferenceAbsoluteAddress
                                                        switch ($SuperType) {
                                                                { $_ -in (0, 1) } {
                                                                        $null = $DumpFile.Append("{0:X6}" -f (($SegmentIndex * 65536) + ($ReferencedOffset -band 65535)))
                                                                        $null = $RelocFile.Append("{0:X6}" -f (($SegmentIndex * 65536) + ($ReferencedOffset -band 65535)))
                                                                }
                                                                2 {
                                                                        $null = $DumpFile.Append("{0:X6}" -f $ReferencedOffset)
                                                                        $null = $RelocFile.Append("{0:X6}" -f $ReferencedOffset)
                                                                }
                                                                { $_ -in (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25) } {
                                                                        $null = $DumpFile.Append("{0:X6}" -f ((($SuperType - 13) * 65536) + $ReferencedOffset))
                                                                        $null = $RelocFile.Append("{0:X6}" -f ((($SuperType - 13) * 65536) + $ReferencedOffset))
                                                                }
                                                                { $_ -in (26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37) } {
                                                                        $null = $DumpFile.Append("{0:X6}" -f ((($SuperType - 25) * 65536) + $ReferencedOffset))
                                                                        $null = $RelocFile.Append("{0:X6}" -f ((($SuperType - 25) * 65536) + $ReferencedOffset))
                                                                }
                                                        }
                                                        $null = $DumpFile.AppendLine("")
                                                        $null = $RelocFile.AppendLine("")

                                                        $SuperByteReadCount++
                                                }
                                                $SuperPage++
                                        }
                                }
                        }
                        default {
                                if (($OMFRecordType -ge 0x01) -and ($OMFRecordType -le 0xDF)) {
                                        $ByteCount = $OMFRecordType
                                        $null = $DumpFile.AppendLine("CONST Size=" + ("{0:X2}" -f $ByteCount))
                                        for ($ByteIndex = 0; $ByteIndex -le $ByteCount - 1; $ByteIndex++) {
                                                $LCONSTBytes[$LCONSTOffset] = ReadByteOfData
                                                $null = $DumpFile.Append(("{0:X2}" -f $LCONSTBytes[$LCONSTOffset]) + " ")
                                                $LCONSTOffset = $LCONSTOffset + 1
                                        }
                                        $null = $DumpFile.AppendLine("")
                                } else {
                                        $null = $DumpFile.AppendLine("Unknown OMFRecordType Code " + ("{0:X2}" -f $OMFRecordType) + " found in Segment # " + $SegmentIndex + ", Offset " + $SourcePosition - 1)
                                }
                        }
                }
        }

        $ByteCount = $longRESSPC
        $null = $DumpFile.AppendLine("RESSPC (Reserved Space) Size=" + ("{0:X8}" -f $ByteCount))
        for ($ByteIndex = 1; $ByteIndex -le $ByteCount; $ByteIndex++) {
                $LCONSTBytes[$LCONSTOffset] = 0
                $LCONSTOffset = $LCONSTOffset + 1
                $null = $DumpFile.Append(("{0:X2}" -f 0) + " ")
        }
        $null = $DumpFile.AppendLine("")

        Set-Content -Encoding Byte -Path ("$($OutputFileNamePrefix)_DumpContent" + ("{0:X2}" -f $SegmentIndex) + ".bin") -Value $LCONSTBytes[0..$($LCONSTOffset - 1)]
}

Set-Content -Path ("$($OutputFileNamePrefix)_DumpAll.txt") -Value $DumpFile.ToString(0, $DumpFile.Length - 2)
Set-Content -Path ("$($OutputFileNamePrefix)_DumpHeader.txt") -Value $HeaderFile.ToString(0, $HeaderFile.Length - 2)
Set-Content -Path ("$($OutputFileNamePrefix)_DumpReloc.txt") -Value $RelocFile.ToString(0, $RelocFile.Length - 2)
