Crunch - Data Compression Tool
The crunch tool is a command-line utility for compressing binary data for Amstrad CPC programs using various Z80-optimized compression algorithms.
Installation
The crunch tool is built as part of the cpclib toolchain:
Overview
Crunch provides access to multiple compression algorithms specifically optimized for Z80-based systems like the Amstrad CPC. Each algorithm offers different trade-offs between compression ratio, decompression speed, and memory usage.
Supported Compression Algorithms
- Apultra - High compression ratio with good decompression speed
- Exomizer - Excellent compression, widely used in retro computing
- Lz4 - Very fast decompression, moderate compression
- Lz48 - Lightweight LZ-based compression
- Lz49 - Enhanced version of LZ48
- Lzsa1 - Fast decompression with good compression ratio
- Lzsa2 - Better compression than LZSA1, slightly slower decompression
- Shrinkler - Extreme compression for size-critical applications
- Upkr - Efficient general-purpose compression
- Zx0 - Modern, efficient compression for Z80 systems
Quick Start
Compress a binary file:
Extract Z80 decompression source code:
Usage
Options
Integration with BndBuild
Crunch is available as a standalone crunch binary but can also be used within BndBuild build scripts using the crunch or compress command aliases. See BndBuild Commands for integration details.
Options
-c, --cruncher <CRUNCHER>- Select the compression algorithm to use (required)-
Choices:
apultra,exomizer,lz4,lz48,lz49,lzsa1,lzsa2,shrinkler,upkr,zx0 -
-i, --input <INPUT>- Input file to compress - Can be a binary file (e.g.,
my_file.o) - Can be an Amsdos file (e.g.,
FILE.BIN) -
Can be a file within a disc image (e.g.,
my_disc.dsk#FILE.BIN) -
-o, --output <OUTPUT>- Compressed output file (requires--input) - Can be a binary file
- Can be an Amsdos file
-
Can be a file within a disc image
-
-k, --keep-header- Also compress the Amsdos header -
Useful for binary files where the first bytes contain a valid Amsdos header
-
-H, --header- Add an Amsdos header when storing the file on the host -
-z, --z80- Display the Z80 decompression source code - Cannot be used with
--input - Outputs the assembly code for decompressing data
Examples
Basic Compression
Compress a file using Apultra:
Compress with Amsdos Header
Compress a file and add an Amsdos header:
Keep Original Header
Compress a file while preserving its Amsdos header:
Compress File in DSK
Compress a file and store it directly in a disc image:
Get Decompression Code
Extract the Z80 assembly decompression routine:
This outputs the Z80 decompression source code that can be included in your assembly projects.
Choosing a Compression Algorithm
Different algorithms are suited for different use cases:
For Maximum Compression
- Shrinkler - Best compression ratio, but slower decompression
- Exomizer - Excellent compression, widely tested and reliable
For Fast Decompression
- Lz4 - Very fast decompression, ideal for realtime applications
- Lzsa1 - Good balance of speed and compression
- Zx0 - Modern algorithm with fast decompression
For General Use
- Apultra - Good all-around choice
- Lzsa2 - Better compression than LZSA1, still reasonably fast
Lightweight Options
- Lz48/Lz49 - Small decompressor code, good for size-constrained projects
- Upkr - Efficient general-purpose compression
File Format Support
Binary Files
Standard binary files without headers:
Amsdos Files
Files with Amsdos headers (load address, execution address):
Disc Images
Files stored within DSK disc images:
Using Compressed Data
After compressing your data, you need to include the corresponding decompression routine in your Z80 code:
-
Get the decompression source:
-
Include it in your assembly project:
Each decompressor has its own calling convention - check the generated source code for specific details.
Integration with BASM
The crunch tool can be used directly in BASM assembly projects:
Or use the CRUNCH directive in BASM for automatic compression:
Notes
- Compression time may vary significantly between algorithms
- Shrinkler provides the best compression but can be slow to compress
- Always test decompression speed on actual hardware for timing-critical code
- The
-k, --keep-headerflag is useful when the header contains important data
See Also
- BASM Documentation - Assembly language support with compression directives
- BndBuild Documentation - Build system integration
- Disc Management - Working with DSK disc images