mirror of https://github.com/YosysHQ/icestorm.git
Cleanup icecompr code
This commit is contained in:
parent
b1c4784c8e
commit
31cc8a1ab9
|
|
@ -1,5 +1,6 @@
|
|||
icecompr
|
||||
iceuncompr
|
||||
example_?k.compr
|
||||
example_?k.compr_py
|
||||
example_?k.ok
|
||||
example_?k.uncompr
|
||||
|
|
|
|||
|
|
@ -11,18 +11,22 @@ iceuncompr: iceuncompr.c
|
|||
|
||||
%.compr: %.bin icecompr
|
||||
./icecompr -v $< $@
|
||||
|
||||
|
||||
%.compr_py: %.bin icecompr.py
|
||||
./icecompr.py < $< > $@
|
||||
|
||||
%.uncompr: %.compr iceuncompr
|
||||
./iceuncompr $< $@
|
||||
|
||||
%.ok: %.uncompr %.bin
|
||||
cmp $^
|
||||
%.ok: %.compr %.compr_py %.uncompr %.bin
|
||||
cmp $(basename $@).compr $(basename $@).compr_py
|
||||
cmp $(basename $@).uncompr $(basename $@).bin
|
||||
touch $@
|
||||
|
||||
clean:
|
||||
rm -f icecompr iceuncompr *.d
|
||||
rm -f example_1k.compr example_8k.compr
|
||||
rm -f example_1k.compr_py example_8k.compr_py
|
||||
rm -f example_1k.uncompr example_8k.uncompr
|
||||
rm -f example_1k.ok example_8k.ok
|
||||
|
||||
|
|
|
|||
|
|
@ -7,14 +7,13 @@
|
|||
# binary, for any purpose, commercial or non-commercial, and by any
|
||||
# means.
|
||||
|
||||
|
||||
def make_int_bits(value, nbits):
|
||||
bits = list()
|
||||
for i in range(nbits-1, -1, -1):
|
||||
bits.append((value & (1 << i)) != 0)
|
||||
return bits
|
||||
|
||||
def ice_compress(inbits):
|
||||
def ice_compress_bits(inbits):
|
||||
outbits = list()
|
||||
outbits += make_int_bits(0x49434543, 32)
|
||||
outbits += make_int_bits(0x4f4d5052, 32)
|
||||
|
|
@ -107,23 +106,30 @@ def ice_compress(inbits):
|
|||
|
||||
return outbits
|
||||
|
||||
def ice_compress_bytes(inbytes):
|
||||
inbits = list()
|
||||
for byte in inbytes:
|
||||
for i in range(7, -1, -1):
|
||||
inbits.append((byte & (1 << i)) != 0)
|
||||
|
||||
outbits = ice_compress_bits(inbits)
|
||||
|
||||
outbytes = list()
|
||||
for i in range(0, len(outbits), 8):
|
||||
byte = 0
|
||||
for k in range(i, min(i+8, len(outbits))):
|
||||
if outbits[k]: byte |= 1 << (7-(k-i))
|
||||
outbytes.append(byte)
|
||||
|
||||
return bytes(outbytes)
|
||||
|
||||
# ------------------------------------------------------
|
||||
# Usage example:
|
||||
# python3 icecompr.py < example_8k.bin > example_8k.compr
|
||||
|
||||
import sys
|
||||
|
||||
inbits = list()
|
||||
for byte in sys.stdin.buffer.read():
|
||||
for i in range(7, -1, -1):
|
||||
inbits.append((byte & (1 << i)) != 0)
|
||||
|
||||
outbits = ice_compress(inbits)
|
||||
|
||||
for i in range(0, len(outbits), 8):
|
||||
byte = 0
|
||||
for k in range(i, min(i+8, len(outbits))):
|
||||
if outbits[k]: byte |= 1 << (7-(k-i))
|
||||
sys.stdout.buffer.write(bytes([byte]))
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
inbytes = sys.stdin.buffer.read()
|
||||
outbytes = ice_compress_bytes(inbytes)
|
||||
sys.stdout.buffer.write(outbytes)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue