Electronics > Projects, Designs, and Technical Stuff
Swapping bits in bytes in a file
<< < (2/2)
mariush:
Here's a php script that does it, just wrote it right now in the last 5 minutes :

Save code below in a folder with the .php extension (paste in notepad, save as and select "All Files" at file type, enter name with .php extension) then put the file you want modified in same folder as input.bin 
Download php from php.net and run  php.exe  c:\path\to\script.php  and it will produce

   $byte = flipbits($byte,0,7);

that line swaps bit 0 with bit 7 ... you can change the 0 and 7 with whatever you want. You can also have consecutive lines (ex swap 0,7 then swap 1,6 ... to have 2 pairs of bits swapped.


--- Code: ---<?php

//  Opens input.bin , flips bits in every byte and saves to output.bin

function flipbits($value, $first, $second) {
$bits = array();
$bitstring = str_pad(decbin($value),8,'0',STR_PAD_LEFT);
for ($i=0;$i<8;$i++) $bits[$i] = substr($bitstring,$i,1);
$temp = $bits[$first];
$bits[$first] = $bits[$second];
$bits[$second] = $temp;
$bitstring = '';
for ($i=0;$i<8;$i++) $bitstring .= $bits[$i];
return bindec($bitstring);
}

$input = file_get_contents(__DIR__ .'/input.bin');
$h = fopen(__DIR__ .'/output.bin','wb');

for ($i=0;$i<strlen($input);$i++) {
$byte = ord(substr($input,$i,1));
// call the function every time you need to flip a bits
// ex flip bit 0 with bit 7
$byte = flipbits($byte,0,7);
// repeat as needed

fwrite($h,chr($byte));
}

fclose($h);
echo "Done. Saved to output.bin";

?>


--- End code ---
Alex Eisenhut:
Thanks, I have studied the drive more closely and it came in at least two variants, one with 16K of ROM in one chip and the other split across two 8K chips.
I have the two chip version, and splitting the 16K ROM into two 8K images doesn't work.
 It turns out the data bits are scrambled differently across the ROMs in the two chip version.
I'll try that PHP thing soon.
Alex Eisenhut:

--- Quote from: Benta on January 11, 2020, 05:47:33 pm ---Wouldn't it be easier to swap the two lines to the ROM on the PCB? (cut 'n jumper).

--- End quote ---

It might get to that, the PCB actually has solder jumpers already there but I don't know what they do yet. They mixed up the data bits differently across the two ROMs.
Nominal Animal:
Here's a quick Python3 skeleton for bit mapping:

--- Code: ---#!/usr/bin/env python3 -B
# -*- coding: utf-8 -*-
from sys import stdin, stdout, stderr, argv, exit

def bytefilter(source, output, mapping, chunk=2097152):
    while True:
        try:
            data = source.read(chunk)
        except BlockingIOError:
            return

        if len(data) < 1:
            return

        output.write(data.translate(mapping))

def bitremap(byte, form):
    result = 0
    for i in range(0, 8):
        dstbit = 7 - i
        srcbit = int(form[i])
        if (byte & (1 << srcbit)):
            result |= 1 << dstbit
    return result

if __name__ == '__main__':
    if len(argv) != 4:
        stderr.write("\n")
        stderr.write("Usage: %s INPUTFILE OUTPUTFILE BITPATTERN\n" % argv[0])
        stderr.write("\n")
        stderr.write("For no translation, use bitpattern 76543210.\n")
        stderr.write("\n")
        exit(1)

    if len(argv[3]) != 8 or len(argv[3].rstrip("01234567")) != 0:
        stderr.write("%s: Invalid bit pattern.\n" % argv[0])
        exit(1)

    mapping = b''
    for i in range(0, 256):
        mapping += b'%c' % bitremap(i, argv[3])

    try:
        source = open(argv[1], mode='rb')
    except FileNotFoundError:
        stderr.write("%s: No such file.\n" % argv[1])
        exit(1)

    output = open(argv[2], mode='wb')

    bytefilter(source, output, mapping)

--- End code ---

Run it without parameters to see usage.  Simply put, it takes an input filename, and output filename, and the 8-digit bit pattern to use.
Normal bit pattern is 76543210.  If you wish to swap the second bit (bit 1) and fourth bit (bit 3), use 76541230.
Essentially, the third parameter tells how the bits in the input bytes are mapped to the output file bytes.

It does not stop you from doing silly things, like 77777701, which uses the highest bit in input bytes for all but the two lowest bits, and swaps those two lowest bits.

The way this Python program works, is by constructing a 256-byte lookup table.  The bitremap() function transforms one numeric value using the form string; it is called once for each possible byte value, to construct the lookup table.  The Python .translate() method does the lookup efficiently.
Alex Eisenhut:
Looks like I'm not the only one who came across this scrambling problem  :-DD

https://www.commodoreserver.com/PublicDiskDetails.asp?DID=DFF943F2D52D40EE8D73DD950E5F02EB

Someone wrote a scrambler program that runs on the 64... Hilarious.
Navigation
Message Index
Previous page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod