Attribute VB_Name = "modSMD"
Option Explicit

Sub sConvertSMDtoBIN(ByRef bytFileSMD() As Byte)
On Error GoTo Err_

'good practice, constants for commonly used numbers.... hmmmm?
Const c16384 = 16384
Const c8192 = 8192
Const c512 = 512
Const c2 = 2
Const c1 = 1
Const c0 = 0

Dim bytFileBIN() As Byte

Dim lngDo As Long
Dim lngBlock As Long
Dim lngBlocks As Long
Dim lngExpectedFileSize As Long

lngBlocks = (UBound(bytFileSMD) - c512 + c1) \ c16384 'c1s?
lngExpectedFileSize = (lngBlocks * c16384 + c512)

If Not lngExpectedFileSize = (UBound(bytFileSMD) + c1) Then
    ReDim Preserve bytFileSMD(c0 To lngExpectedFileSize - c1) 'Trim rubbish before conversion
End If

ReDim bytFileBIN(c0 To UBound(bytFileSMD) - c512)  'remove SMD header bytes

lngBlock = c0
Do While lngBlock < lngBlocks
    lngDo = c0
    Do While lngDo < c8192 'De-interlace block here!
        bytFileBIN(lngBlock * c16384 + lngDo * c2 + c0) = bytFileSMD(c512 + lngBlock * c16384 + c8192 + lngDo)
        bytFileBIN(lngBlock * c16384 + lngDo * c2 + c1) = bytFileSMD(c512 + lngBlock * c16384 + lngDo)
        lngDo = lngDo + c1
    Loop
    lngBlock = lngBlock + c1
Loop

'Return BIN
bytFileSMD() = bytFileBIN()
ReDim bytFileBIN(0)

Exit_:
Exit Sub

Err_:
MsgBox vbNewLine & Err.Description & vbTab, vbExclamation, Err.Number & "SMD2BIN"
'bytFileBIN() = bytFileSMD() 'Error, pass back original file!
Resume Exit_

End Sub

'/* convert to standard BIN file format */'

'    for (i = 0; i < blocks; i++) {
'      for (x = 0; x < 8192; x++) {
'        new[i * 16384 + x * 2 + 0] = cpu68k_rom[512 + i * 16384 + 8192 + x];
'        new[i * 16384 + x * 2 + 1] = cpu68k_rom[512 + i * 16384 + x];
'      }
'    }
'    free(cpu68k_rom);
'    cpu68k_rom = new;
'    cpu68k_romlen -= 512;
'    break;
'default:
'    return ("Unknown image type");
'    break;
'  }

Function fROMFormatBIN(ByRef bytFile() As Byte) As Boolean
On Error GoTo Err_

Dim blnBIN As Boolean

If UBound(bytFile) > &H103 Then 'Ensure file is big enough to be a rom
    blnBIN = bytFile(&H100) = &H53 And _
             bytFile(&H101) = &H45 And _
             bytFile(&H102) = &H47 And _
             bytFile(&H103) = &H41 'SEGA - 5345 4741?
Else
    blnBIN = False
End If

Exit_:
fROMFormatBIN = blnBIN
Exit Function

Err_:
MsgBox vbNewLine & Err.Description & vbTab, vbExclamation, Err.Number & "chkBIN"
blnBIN = False
Resume Exit_

End Function

Function fROMFormatSMD(ByRef bytFile() As Byte) As Boolean
On Error GoTo Err_

'*** SMD file format check - Richard Bannister ***

Dim blnSMD As Boolean


If UBound(bytFile) > &H2281 Then 'Ensure file is big enough to be a rom
    
    'Check for typical SMD Header
    blnSMD = bytFile(&H8) = &HAA And _
             bytFile(&H9) = &HBB And _
             bytFile(&HA) = &H6
         
    If Not blnSMD Then 'Non standard Header found, check for interlaced SEGA text in ROM
        blnSMD = bytFile(&H2280) = &H53 And _
                 bytFile(&H280) = &H45 And _
                 bytFile(&H2281) = &H47 And _
                 bytFile(&H281) = &H41 'SEGA - Interlaced 8192?
    End If
Else
    blnSMD = False
End If

Exit_:
fROMFormatSMD = blnSMD
Exit Function

Err_:
MsgBox vbNewLine & Err.Description & vbTab, vbExclamation, Err.Number & "SMD2BIN"
Resume Exit_

End Function
