import sys
def clean_lattice_bitstream(input_file, output_file):
# Search for the exact 24-bit start of the internal header preamble: 0xFF 0xFF 0xBD
# based on your exact hexdump row: "ffff bdff ffb3" -> bytes: FF FF BD FF FF B3
TARGET_PREAMBLE = b'\xff\xff\xbd'
with open(input_file, 'rb') as f:
content = f.read()
idx = content.find(TARGET_PREAMBLE)
if idx == -1:
print("Error: Could not find the core Preamble signature (FFFFBD) in this .bit file!")
sys.exit(1)
# Step 1: Slice the file, removing all text metadata headers before the preamble
clean_payload = content[idx:]
print(f"Text metadata header removed. Preamble found at offset {idx}.")
print(f"Extracted {len(clean_payload)} bytes of clean hardware-ready payload.")
# Step 2: Save the binary data AS IS (STRICTLY NO BIT MIRRORING)
with open(output_file, 'wb') as f:
f.write(clean_payload)
print(f"Success! Output file saved to: {output_file}")
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: python3 lpc_convert.py <input.bit> <output.bin>")
sys.exit(1)
clean_lattice_bitstream(sys.argv[1], sys.argv[2])