1 # Prefix-code encode/decode acceleration
3 This is useful for Huffman codes, and other prefix-codes, which are used a lot in common formats:
5 * DEFLATE (zip, png, gzip, etc.)
10 # Prefix-code decode description
12 `pcdec RT,RA,RB,RC,imm`
14 |0 |6 |11 |16 |21 |26 |31 |
15 | PO | RT | RA | RB | RC | XO | imm |
17 if `imm` is 1, at most one code-word is decoded -- useful for things like DEFLATE that alternate code words with other bits, or use multiple binary code trees. if `imm` is 0, it decodes multiple code-words
19 The binary code tree is encoded in `RA` like so:
22 t[i] = (RA >> i) & 0x1
25 +-----------+-----------+
29 +---t[2]----+ +---t[3]----+
33 +t[4]-+ +t[5]-+ +t[6]-+ +t[7]-+
37 t[8] t[9] t[10] t[11] t[12] t[13] t[14] t[15]
42 Decoding a code word works by walking on the tree from the root to the children, matching each passed 0 or 1 to the next read input bit in RB in LSB to MSB order. When `t[i]` is set, then a valid code word was read and `i` is written to the next byte of output in RT in LSB to MSB order. When no set `t[i]` is encountered, and there are still input bits left, then the code word is >6-bits, so SO/OV/OV32 are set, and decoding stops.
44 # [DRAFT] Prefix-code decode
48 * pcdec RT,RA,RB,RC,imm
55 decoded_in_pos <- in_pos
61 # walk the binary tree in `tree` from parent to the selected child
62 decoded <- decoded * 2 + bitstream[63 - in_pos]
64 if decoded >=u 64 then
67 if tree[63 - decoded] then
68 decoded_in_pos <- in_pos
69 output[56 - 8 * out_byte:63 - 8 * out_byte] <- decoded
71 out_byte <- out_byte + 1
72 if one | (out_byte >=u 8) then
77 Special Registers Altered:
81 # [DRAFT] Prefix-code encode