Function GetSnesRomTitle(Filename As String) As String 'The ROM title will be manipulated within this variable Dim RomTitle As String 'Used for all For...Next loops, etc. Dim ArrayPos As Integer, Array2Pos As Integer 'The length of SNES ROM titles is 21 characters Const TITLELENGTH = 21 'For readablity, 65535 is FFFF in hex Const FFFF = 65535 'Fill 'RomTitle' with 21 spaces to tell ' GET to read 21 bytes RomTitle = Space$(TITLELENGTH) Dim HeaderPos(2) As Long HeaderPos(0) = 33216 'Default LoROM Header Position HeaderPos(1) = 32704 'Alternate LoROM Header Position HeaderPos(2) = 65984 'Default HiROM Header Position 'Integer for the next free file number Dim FreeFileNum As Integer 'Find the next free file number and assign it to FreeFileNum FreeFileNum = FreeFile 'Open the ROM for binary reading Open Filename For Binary As #FreeFileNum 'Inverse Checksum array for HiROM/LoROM detection Dim InvChecksumByte(1) As Byte Dim InverseChecksum As Long Dim InvChecksumHex(1) As String 'Checksum array for HiROM/LoROM detection Dim ChecksumByte(1) As Byte Dim Checksum As Long Dim ChecksumHex(1) As String 'Cycle through each header pos in the 'HeaderPos' array ' to find where the header is located in the ROM For ArrayPos = 0 To 2 'Read each byte of the inversed checksum ' the inversed checksum is located 29 & 30 bytes ' from the ROM header Get #FreeFileNum, HeaderPos(ArrayPos) + 29, InvChecksumByte(0) Get #FreeFileNum, HeaderPos(ArrayPos) + 30, InvChecksumByte(1) 'Read each byte of the checksum ' the inversed checksum is located 31 & 32 bytes ' from the ROM header Get #FreeFileNum, HeaderPos(ArrayPos) + 31, ChecksumByte(0) Get #FreeFileNum, HeaderPos(ArrayPos) + 32, ChecksumByte(1) 'Get hex values of inverse checksum InvChecksumHex(0) = CStr(Hex(InvChecksumByte(0))) InvChecksumHex(1) = CStr(Hex(InvChecksumByte(1))) 'Get hex values of checksum ChecksumHex(0) = CStr(Hex(ChecksumByte(0))) ChecksumHex(1) = CStr(Hex(ChecksumByte(1))) 'Cycle through checksum arrays For Array2Pos = 0 To 1 'If hex value of the inverse checksum is not ' 2 digits affix a "0" to the front: e.g. C = 0C If Len(InvChecksumHex(Array2Pos)) = 1 Then InvChecksumHex(Array2Pos) = "0" + InvChecksumHex(Array2Pos) End If 'If hex value of the checksum is not 2 digits ' affix a "0" to the front: e.g. E = 0E If Len(ChecksumHex(Array2Pos)) = 1 Then ChecksumHex(Array2Pos) = "0" + ChecksumHex(Array2Pos) End If Next Array2Pos 'Put both bytes of the inverse checksum together, ' then convert them to a Long InverseChecksum = CLng("&H" + InvChecksumHex(0) + InvChecksumHex(1)) 'Put both bytes of the checksum together, ' then convert them to a Long Checksum = CLng("&H" + ChecksumHex(0) + ChecksumHex(1)) 'ROMs with both the inverse checksum and checksum ' equaling FFFF are either invalid or HiROM If InverseChecksum = FFFF And Checksum = FFFF Then 'If the header position is already set at the ' HiROM value then the header is invalid If HeaderPos(ArrayPos) = 65984 Then 'Assign invalid notice to the function GetSnesRomTitle = "*Bad or missing ROM header" 'Close the ROM file Close #FreeFileNum 'Exit the function Exit Function End If 'ROM is assumed HiROM, when ArrayPos is incremented ' up 1, it will go to the NEXT statement which will ' set the header position to 65984 (HiROM) ArrayPos = ArrayPos + 1 Else 'Continue to check for correct header position 'The header position is found by 'Or'ing the ' inverse checksum and checksum, If the result ' equals FFFF then the header has been found If (InverseChecksum Or Checksum) = FFFF Then Exit For End If Next ArrayPos 'If the For...Next loop cycled through each ' possible header position but did not find ' the correct header then default to LoROM ' HeaderPos(0)=33216 (which is LoROM) If ArrayPos = 3 Then ArrayPos = 0 'Read the ROM title from the determined header position ' (add 1 to start at the beginning of the title) Get #FreeFileNum, HeaderPos(ArrayPos) + 1, RomTitle 'Close the ROM file Close #FreeFileNum 'Assign the ROM title to the function GetSnesRomTitle = Trim$(RomTitle) End Function