Skip to content

Commit 53acb79

Browse files
Add RLE compression and decompression macros/subs.
1 parent 2da8e14 commit 53acb79

File tree

8 files changed

+219
-1
lines changed

8 files changed

+219
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Check our [User's Manual](https://c64lib.github.io/user-manual/#_common) for mor
88

99
## Change log
1010

11+
### Changes in version 0.4.0
12+
13+
* New macro: `compress.asm\compressRLE`.
14+
* New subroutine: `decompress_rle.asm`.
15+
1116
### Changes in version 0.3.0
1217

1318
* New macro: `math.asm/mulAndAdd` - multiple two numbers and add result to memory location.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ retroProject {
2828
dialect = "KickAssembler"
2929
dialectVersion = "5.25"
3030
libDirs = [".ra/deps/c64lib"]
31-
verbose = true
31+
srcDirs = ["lib"]
3232
libFromGitHub "c64lib/64spec", "0.7.0pr"
3333
}
3434

lib/common-global.asm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* SOFTWARE.
2424
*/
2525
#import "common.asm"
26+
#importonce
27+
2628
.filenamespace c64lib
2729

2830
.macro @c64lib_ch(data) { ch(data) }

lib/compress-global.asm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2023 c64lib
5+
* Copyright (c) 2017-2023 Maciej Małecki
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
#import "compress.asm"
26+
#importonce
27+
28+
.filenamespace c64lib
29+
30+
.macro @c64lib_compressRLE(data) { compressRLE(data) }

lib/compress.asm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2023 c64lib
5+
* Copyright (c) 2017-2023 Maciej Małecki
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
#importonce
26+
.filenamespace c64lib
27+
28+
/*
29+
* Performs RLE (Running Length Encoding) compression of given binary data (kick assembler data type).
30+
* The compressed result is placed as is in the result file starting from the place where this macro is called.
31+
*/
32+
.macro compressRLE(binaryData) {
33+
.var runLength = 0
34+
.var runValue = 0
35+
.var crunchedLength = 0
36+
.for(var i = 0; i < binaryData.getSize(); i++) {
37+
.if(runLength > 0 && (binaryData.get(i) != runValue || runLength == $ff)) {
38+
.byte runLength
39+
.byte runValue
40+
.eval runLength = 0
41+
.eval crunchedLength = crunchedLength + 2
42+
}
43+
.if(runLength == 0) {
44+
.eval runValue = binaryData.get(i)
45+
.eval runLength = 1
46+
} else {
47+
.eval runLength++
48+
}
49+
}
50+
.byte runLength
51+
.byte runValue
52+
.byte $00 // end mark
53+
.eval crunchedLength++
54+
.print "Crunched from " + binaryData.getSize() + " to " + crunchedLength + " (" + round((crunchedLength/binaryData.getSize())*100) + "%)"
55+
}

lib/sub/decompress-rle.asm

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2023 c64lib
5+
* Copyright (c) 2017-2023 Maciej Małecki
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
#import "../invoke.asm"
26+
27+
.namespace c64lib {
28+
29+
/*
30+
* Stack:
31+
* source address (2 bytes)
32+
* dest address (2 bytes)
33+
*/
34+
decompressRLE: {
35+
invokeStackBegin(returnPtr)
36+
pullParamW(destination)
37+
pullParamW(source)
38+
invokeStackEnd(returnPtr)
39+
40+
nextSequence:
41+
jsr loadSource
42+
cmp #0
43+
beq end // end mark
44+
tax // x <- run length
45+
jsr loadSource // a <- run value
46+
decrunch:
47+
sta destination:$ffff
48+
inc destination
49+
bne !+
50+
inc destination + 1
51+
!:
52+
dex
53+
bne decrunch
54+
jmp nextSequence
55+
56+
end:
57+
rts
58+
loadSource:
59+
lda source:$ffff
60+
inc source
61+
bne !+
62+
inc source + 1
63+
!:
64+
rts
65+
// locals
66+
returnPtr: .word 0
67+
}
68+
}

spec/decompress-rle.spec.asm

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2023 c64lib
5+
* Copyright (c) 2017-2023 Maciej Małecki
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
#import "64spec/lib/64spec.asm"
26+
#import "../lib/compress-global.asm"
27+
#import "../lib/invoke-global.asm"
28+
29+
.var testData = LoadBinary("decompress-rle.test-data.txt")
30+
31+
sfspec: init_spec()
32+
describe("decompressRLE")
33+
34+
it("decompress data"); {
35+
c64lib_pushParamW(compressedData)
36+
c64lib_pushParamW(targetData)
37+
jsr decompressRLE
38+
39+
assert_bytes_equal testData.getSize(): targetData: originalData
40+
}
41+
finish_spec()
42+
43+
* = * "Data"
44+
decompressRLE:
45+
#import "../lib/sub/decompress-rle.asm"
46+
47+
originalData:
48+
.fill testData.getSize(), testData.get(i)
49+
originalDataEnd:
50+
51+
compressedData:
52+
c64lib_compressRLE(testData)
53+
compressedDataEnd:
54+
55+
targetData:
56+
.fill testData.getSize(), 0
57+
targetDataEnd:

spec/decompress-rle.test-data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
wegiel drogi to i atari nie dziala

0 commit comments

Comments
 (0)