zlib6502/README.md

121 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2021-06-16 07:04:55 +00:00
[![Build Status](https://travis-ci.com/pfusik/zlib6502.svg?branch=master)](https://travis-ci.com/github/pfusik/zlib6502)
2020-02-18 21:02:50 +00:00
2013-10-01 17:10:40 +00:00
6502 inflate routine
====================
2019-11-07 10:32:22 +00:00
[DEFLATE](https://en.wikipedia.org/wiki/DEFLATE) is a popular compression format,
2013-10-01 17:10:40 +00:00
used in ZIP, gzip, PNG and many other formats.
In 2000 I wrote a DEFLATE decompression routine (called "inflate")
2019-11-07 10:32:22 +00:00
in the [6502](https://en.wikipedia.org/wiki/6502) assembly language.
2013-10-01 17:10:40 +00:00
In 2007 I optimized it so it is about 30% shorter and 10% faster than before.
2019-11-07 10:32:11 +00:00
In 2017 I fixed bugs causing invalid expansion of some streams.
These were edge cases, unlikely to encounter unless intentionally triggerred.
2013-10-01 17:10:40 +00:00
Compilation
-----------
2017-02-07 10:18:27 +00:00
Use [xasm](https://github.com/pfusik/xasm).
2013-10-01 17:10:40 +00:00
The routine uses three memory areas:
* `inflate` - code and constants (508 bytes)
* `inflate_data` - uninitialized data (765 bytes)
2019-11-07 10:31:22 +00:00
* `inflate_zp` - variables on zero page (10 bytes)
2013-10-01 17:10:40 +00:00
You must select these locations at compile time, for example:
xasm -d inflate=$b700 -d inflate_data=$b900 -d inflate_zp=$f0 inflate.asx
(escape the dollars if in Unix shell or Makefile).
2013-10-01 17:10:40 +00:00
Usage
-----
2017-02-07 10:18:27 +00:00
The `inflate` routine assumes that the compressed and the uncompressed data
fit in the memory. Before calling `inflate`, set the locations
of the compressed and the uncompressed data in the zero-page variables:
2013-10-01 17:10:40 +00:00
mwa #compressedData inflate_zp
mwa #uncompressedData inflate_zp+2
jsr inflate
As the compressed data is read sequentially and only once, it is possible
to overlap the compressed and uncompressed data. That is, the data being
uncompressed can be stored in place of some compressed data which has been
already read.
It is also possible to get the compressed data from any forward-only stream.
In this case, modify the `getBit` routine to use your `readByte`:
2013-10-01 17:10:40 +00:00
getBit
lsr getBit_buffer
bne getBit_return
pha
stx getBit_buffer
jsr readByte
ldx getBit_buffer
ldy #0
sec
ror @
sta getBit_buffer
pla
getBit_return
rts
Compression
-----------
There are several ways to get DEFLATE compressed data.
If you are looking for maximum compression, use [Zopfli](https://github.com/google/zopfli).
For example:
2013-10-01 17:10:40 +00:00
2021-03-27 13:48:44 +00:00
zopfli --deflate --i1000 INPUT_FILE
2013-10-01 17:10:40 +00:00
will compress to `INPUT_FILE.deflate`.
I have compiled a [Windows exe](http://pfusik.github.io/zlib6502/zopfli.exe.gz) for you.
2021-03-27 13:48:44 +00:00
Increasing the number passed to the `--i` option can improve the compression by a few bytes
at the cost of increased compression time.
2013-10-01 17:10:40 +00:00
Historically, I have used:
2013-10-01 17:10:40 +00:00
2019-11-07 10:32:22 +00:00
* my programs using the [Deflater](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/zip/Deflater.html) Java class
* my `deflater` program written with the [zlib library](http://www.zlib.net/).
* my `gzip2deflate` program that extracted the DEFLATE stream from a GZ file created with gzip or [7-Zip](http://7-zip.org/)
* my `zip2deflate` program that extracted the DEFLATE stream from a ZIP created with [KZIP](http://advsys.net/ken/utils.htm)
2013-10-01 17:10:40 +00:00
zlib?
-----
This project is named zlib6502, but only supports DEFLATE decompression.
Someday I'm going to include more functions, including compression.
2017-02-07 10:18:27 +00:00
Meanwhile, you may look at [cc65](https://github.com/cc65/cc65) `zlib.h`.
In addition to `inflate`, it supports zlib-compatible `uncompress`, `adler32` and `crc32`.
2013-10-01 17:10:40 +00:00
License
-------
This code is licensed under the standard zlib license.
2017-02-07 10:18:27 +00:00
Copyright (C) 2000-2017 Piotr Fusik
2013-10-01 17:10:40 +00:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.