Загрузка данных


import sys

def fix_lattice_bitstream(input_file, output_file):
    # Standard non-reversed preamble for Lattice Nexus
    PREAMBLE = b'\x1c\x2b\xb8\xed'
    
    with open(input_file, 'rb') as f:
        content = f.read()
        
    # Find the exact start of the configuration bitstream
    idx = content.find(PREAMBLE)
    if idx == -1:
        # Try finding it in big-endian swap format
        idx = content.find(b'\xed\xb8\x2b\x1c')
        if idx == -1:
            print("Error: Could not find Lattice Preamble signature in .bit file!")
            sys.exit(1)
            
    # Cut off text metadata and padding headers
    bitstream = content[idx:]
    print(f"Detected preamble at byte offset {idx}. Extracting clean payload...")
    
    # Perform strict hardware-compliant Byte Bit-Mirroring
    # Every single byte must be flipped horizontally (bit 0 becomes bit 7, etc.)
    fixed_bytes = bytearray()
    for byte in bitstream:
        flipped = int('{:08b}'.format(byte)[::-1], 2)
        fixed_bytes.append(flipped)
        
    with open(output_file, 'wb') as f:
        f.write(fixed_bytes)
        
    print(f"Successfully generated hardware-ready I2C file: {output_file}")

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("Usage: python3 lpc_convert.py <input_original.bit> <output_ready.bin>")
        sys.exit(1)
    fix_lattice_bitstream(sys.argv[1], sys.argv[2])