Skip to content

Instantly share code, notes, and snippets.

@adamsmasher
Last active August 29, 2015 14:04
Show Gist options
  • Save adamsmasher/1c5d779e37078f24d682 to your computer and use it in GitHub Desktop.
Save adamsmasher/1c5d779e37078f24d682 to your computer and use it in GitHub Desktop.
LZSS (Sharp LR35902 ASM)
;; HL = src
;; DE = dest
Decompress::
.chunk LD A, [HLI] ; get flags
LD B, A ; put flags in B
LD C, 8 ; 8 commands per chunk
.command LD A, [HLI] ; get next byte
BIT 0, B ; is this flag hi?
JR NZ, .literal ; if so, we have a literal
AND A ; otherwise, check for EOF
RET Z
LDH [$80], A ; store length to copy
LD A, [HLI] ; get -offset
PUSH HL ; backup read location
ADD E ; newLoc = E + (-offset)
LD L, A
LD H, D
JR c, .nowrap
DEC H
.nowrap LDH A, [$80] ; get length
PUSH BC ; backup flags
LD B, A
CALL Copy ; HL = SRC, DE = DEST, B = LEN
POP BC
POP HL ; get pointer to compressed data
JR .nextbyte
.literal LD [DE], A
INC DE
.nextbyte SRL B ; move to next flag
DEC C ; one less command
JR NZ, .command
JR .chunk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment